blob: 2135b8e67dcc15fdb969939c5fd6b18d09d0fdda [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"
Dave Chinner632b89e2013-10-29 22:11:58 +110021#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110022#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110023#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_inode_item.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110033#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_bmap.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110035#include "xfs_attr_sf.h"
36#include "xfs_attr_remote.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_attr.h"
38#include "xfs_attr_leaf.h"
39#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000040#include "xfs_trace.h"
Dave Chinner517c2222013-04-24 18:58:55 +100041#include "xfs_buf_item.h"
42#include "xfs_cksum.h"
Dave Chinner4bceb182013-10-29 22:11:51 +110043#include "xfs_dir2.h"
Brian Fostera45086e2015-10-12 15:59:25 +110044#include "xfs_log.h"
Dave Chinner517c2222013-04-24 18:58:55 +100045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
48 * xfs_attr_leaf.c
49 *
50 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
51 */
52
53/*========================================================================
54 * Function prototypes for the kernel.
55 *========================================================================*/
56
57/*
58 * Routines used for growing the Btree.
59 */
Dave Chinner517c2222013-04-24 18:58:55 +100060STATIC int xfs_attr3_leaf_create(struct xfs_da_args *args,
61 xfs_dablk_t which_block, struct xfs_buf **bpp);
62STATIC int xfs_attr3_leaf_add_work(struct xfs_buf *leaf_buffer,
63 struct xfs_attr3_icleaf_hdr *ichdr,
64 struct xfs_da_args *args, int freemap_index);
65STATIC void xfs_attr3_leaf_compact(struct xfs_da_args *args,
66 struct xfs_attr3_icleaf_hdr *ichdr,
67 struct xfs_buf *leaf_buffer);
68STATIC void xfs_attr3_leaf_rebalance(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 xfs_da_state_blk_t *blk1,
70 xfs_da_state_blk_t *blk2);
Dave Chinner517c2222013-04-24 18:58:55 +100071STATIC int xfs_attr3_leaf_figure_balance(xfs_da_state_t *state,
72 xfs_da_state_blk_t *leaf_blk_1,
73 struct xfs_attr3_icleaf_hdr *ichdr1,
74 xfs_da_state_blk_t *leaf_blk_2,
75 struct xfs_attr3_icleaf_hdr *ichdr2,
76 int *number_entries_in_blk1,
77 int *number_usedbytes_in_blk1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * Utility routines.
81 */
Dave Chinnerc2c4c472014-06-06 15:21:45 +100082STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args,
83 struct xfs_attr_leafblock *src_leaf,
Dave Chinner517c2222013-04-24 18:58:55 +100084 struct xfs_attr3_icleaf_hdr *src_ichdr, int src_start,
85 struct xfs_attr_leafblock *dst_leaf,
86 struct xfs_attr3_icleaf_hdr *dst_ichdr, int dst_start,
Dave Chinnerc2c4c472014-06-06 15:21:45 +100087 int move_count);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100088STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
Tim Shimmin726801b2006-09-28 11:01:37 +100089
Brian Fostere87021a2015-04-13 11:27:10 +100090/*
91 * attr3 block 'firstused' conversion helpers.
92 *
93 * firstused refers to the offset of the first used byte of the nameval region
94 * of an attr leaf block. The region starts at the tail of the block and expands
95 * backwards towards the middle. As such, firstused is initialized to the block
96 * size for an empty leaf block and is reduced from there.
97 *
98 * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k.
99 * The in-core firstused field is 32-bit and thus supports the maximum fsb size.
100 * The on-disk field is only 16-bit, however, and overflows at 64k. Since this
101 * only occurs at exactly 64k, we use zero as a magic on-disk value to represent
102 * the attr block size. The following helpers manage the conversion between the
103 * in-core and on-disk formats.
104 */
105
106static void
107xfs_attr3_leaf_firstused_from_disk(
108 struct xfs_da_geometry *geo,
109 struct xfs_attr3_icleaf_hdr *to,
110 struct xfs_attr_leafblock *from)
111{
112 struct xfs_attr3_leaf_hdr *hdr3;
113
114 if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
115 hdr3 = (struct xfs_attr3_leaf_hdr *) from;
116 to->firstused = be16_to_cpu(hdr3->firstused);
117 } else {
118 to->firstused = be16_to_cpu(from->hdr.firstused);
119 }
120
121 /*
122 * Convert from the magic fsb size value to actual blocksize. This
123 * should only occur for empty blocks when the block size overflows
124 * 16-bits.
125 */
126 if (to->firstused == XFS_ATTR3_LEAF_NULLOFF) {
127 ASSERT(!to->count && !to->usedbytes);
128 ASSERT(geo->blksize > USHRT_MAX);
129 to->firstused = geo->blksize;
130 }
131}
132
133static void
134xfs_attr3_leaf_firstused_to_disk(
135 struct xfs_da_geometry *geo,
136 struct xfs_attr_leafblock *to,
137 struct xfs_attr3_icleaf_hdr *from)
138{
139 struct xfs_attr3_leaf_hdr *hdr3;
140 uint32_t firstused;
141
142 /* magic value should only be seen on disk */
143 ASSERT(from->firstused != XFS_ATTR3_LEAF_NULLOFF);
144
145 /*
146 * Scale down the 32-bit in-core firstused value to the 16-bit on-disk
147 * value. This only overflows at the max supported value of 64k. Use the
148 * magic on-disk value to represent block size in this case.
149 */
150 firstused = from->firstused;
151 if (firstused > USHRT_MAX) {
152 ASSERT(from->firstused == geo->blksize);
153 firstused = XFS_ATTR3_LEAF_NULLOFF;
154 }
155
156 if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
157 hdr3 = (struct xfs_attr3_leaf_hdr *) to;
158 hdr3->firstused = cpu_to_be16(firstused);
159 } else {
160 to->hdr.firstused = cpu_to_be16(firstused);
161 }
162}
163
Dave Chinner517c2222013-04-24 18:58:55 +1000164void
165xfs_attr3_leaf_hdr_from_disk(
Brian Foster2f661242015-04-13 11:26:02 +1000166 struct xfs_da_geometry *geo,
Dave Chinner517c2222013-04-24 18:58:55 +1000167 struct xfs_attr3_icleaf_hdr *to,
168 struct xfs_attr_leafblock *from)
169{
170 int i;
171
172 ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
173 from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
174
175 if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
176 struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)from;
177
178 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
179 to->back = be32_to_cpu(hdr3->info.hdr.back);
180 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
181 to->count = be16_to_cpu(hdr3->count);
182 to->usedbytes = be16_to_cpu(hdr3->usedbytes);
Brian Fostere87021a2015-04-13 11:27:10 +1000183 xfs_attr3_leaf_firstused_from_disk(geo, to, from);
Dave Chinner517c2222013-04-24 18:58:55 +1000184 to->holes = hdr3->holes;
185
186 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
187 to->freemap[i].base = be16_to_cpu(hdr3->freemap[i].base);
188 to->freemap[i].size = be16_to_cpu(hdr3->freemap[i].size);
189 }
190 return;
191 }
192 to->forw = be32_to_cpu(from->hdr.info.forw);
193 to->back = be32_to_cpu(from->hdr.info.back);
194 to->magic = be16_to_cpu(from->hdr.info.magic);
195 to->count = be16_to_cpu(from->hdr.count);
196 to->usedbytes = be16_to_cpu(from->hdr.usedbytes);
Brian Fostere87021a2015-04-13 11:27:10 +1000197 xfs_attr3_leaf_firstused_from_disk(geo, to, from);
Dave Chinner517c2222013-04-24 18:58:55 +1000198 to->holes = from->hdr.holes;
199
200 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
201 to->freemap[i].base = be16_to_cpu(from->hdr.freemap[i].base);
202 to->freemap[i].size = be16_to_cpu(from->hdr.freemap[i].size);
203 }
204}
205
206void
207xfs_attr3_leaf_hdr_to_disk(
Brian Foster2f661242015-04-13 11:26:02 +1000208 struct xfs_da_geometry *geo,
Dave Chinner517c2222013-04-24 18:58:55 +1000209 struct xfs_attr_leafblock *to,
210 struct xfs_attr3_icleaf_hdr *from)
211{
Brian Fostere87021a2015-04-13 11:27:10 +1000212 int i;
Dave Chinner517c2222013-04-24 18:58:55 +1000213
214 ASSERT(from->magic == XFS_ATTR_LEAF_MAGIC ||
215 from->magic == XFS_ATTR3_LEAF_MAGIC);
216
217 if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
218 struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)to;
219
220 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
221 hdr3->info.hdr.back = cpu_to_be32(from->back);
222 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
223 hdr3->count = cpu_to_be16(from->count);
224 hdr3->usedbytes = cpu_to_be16(from->usedbytes);
Brian Fostere87021a2015-04-13 11:27:10 +1000225 xfs_attr3_leaf_firstused_to_disk(geo, to, from);
Dave Chinner517c2222013-04-24 18:58:55 +1000226 hdr3->holes = from->holes;
227 hdr3->pad1 = 0;
228
229 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
230 hdr3->freemap[i].base = cpu_to_be16(from->freemap[i].base);
231 hdr3->freemap[i].size = cpu_to_be16(from->freemap[i].size);
232 }
233 return;
234 }
235 to->hdr.info.forw = cpu_to_be32(from->forw);
236 to->hdr.info.back = cpu_to_be32(from->back);
237 to->hdr.info.magic = cpu_to_be16(from->magic);
238 to->hdr.count = cpu_to_be16(from->count);
239 to->hdr.usedbytes = cpu_to_be16(from->usedbytes);
Brian Fostere87021a2015-04-13 11:27:10 +1000240 xfs_attr3_leaf_firstused_to_disk(geo, to, from);
Dave Chinner517c2222013-04-24 18:58:55 +1000241 to->hdr.holes = from->holes;
242 to->hdr.pad1 = 0;
243
244 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
245 to->hdr.freemap[i].base = cpu_to_be16(from->freemap[i].base);
246 to->hdr.freemap[i].size = cpu_to_be16(from->freemap[i].size);
247 }
248}
249
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800250static xfs_failaddr_t
Dave Chinner517c2222013-04-24 18:58:55 +1000251xfs_attr3_leaf_verify(
Darrick J. Wong79a69bf2018-01-16 18:54:12 -0800252 struct xfs_buf *bp)
Dave Chinnerad14c332012-11-12 22:54:16 +1100253{
Darrick J. Wong79a69bf2018-01-16 18:54:12 -0800254 struct xfs_attr3_icleaf_hdr ichdr;
255 struct xfs_mount *mp = bp->b_target->bt_mount;
256 struct xfs_attr_leafblock *leaf = bp->b_addr;
257 struct xfs_perag *pag = bp->b_pag;
258 struct xfs_attr_leaf_entry *entries;
Dave Chinnerad14c332012-11-12 22:54:16 +1100259
Brian Foster2f661242015-04-13 11:26:02 +1000260 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +1000261
262 if (xfs_sb_version_hascrc(&mp->m_sb)) {
263 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
264
265 if (ichdr.magic != XFS_ATTR3_LEAF_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800266 return __this_address;
Dave Chinner517c2222013-04-24 18:58:55 +1000267
Eric Sandeence748ea2015-07-29 11:53:31 +1000268 if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800269 return __this_address;
Dave Chinner517c2222013-04-24 18:58:55 +1000270 if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800271 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +1100272 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->info.lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800273 return __this_address;
Dave Chinner517c2222013-04-24 18:58:55 +1000274 } else {
275 if (ichdr.magic != XFS_ATTR_LEAF_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800276 return __this_address;
Dave Chinner517c2222013-04-24 18:58:55 +1000277 }
Eric Sandeen2e1d2332016-12-09 16:49:47 +1100278 /*
279 * In recovery there is a transient state where count == 0 is valid
280 * because we may have transitioned an empty shortform attr to a leaf
281 * if the attr didn't fit in shortform.
282 */
283 if (pag && pag->pagf_init && ichdr.count == 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800284 return __this_address;
Dave Chinner517c2222013-04-24 18:58:55 +1000285
Darrick J. Wong79a69bf2018-01-16 18:54:12 -0800286 /*
287 * firstused is the block offset of the first name info structure.
288 * Make sure it doesn't go off the block or crash into the header.
289 */
290 if (ichdr.firstused > mp->m_attr_geo->blksize)
291 return __this_address;
292 if (ichdr.firstused < xfs_attr3_leaf_hdr_size(leaf))
293 return __this_address;
294
295 /* Make sure the entries array doesn't crash into the name info. */
296 entries = xfs_attr3_leaf_entryp(bp->b_addr);
297 if ((char *)&entries[ichdr.count] >
298 (char *)bp->b_addr + ichdr.firstused)
299 return __this_address;
300
Dave Chinner517c2222013-04-24 18:58:55 +1000301 /* XXX: need to range check rest of attr header values */
302 /* XXX: hash order check? */
303
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800304 return NULL;
Dave Chinner517c2222013-04-24 18:58:55 +1000305}
306
307static void
308xfs_attr3_leaf_write_verify(
309 struct xfs_buf *bp)
310{
311 struct xfs_mount *mp = bp->b_target->bt_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800312 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinner517c2222013-04-24 18:58:55 +1000313 struct xfs_attr3_leaf_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800314 xfs_failaddr_t fa;
Dave Chinner517c2222013-04-24 18:58:55 +1000315
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800316 fa = xfs_attr3_leaf_verify(bp);
317 if (fa) {
318 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinner517c2222013-04-24 18:58:55 +1000319 return;
320 }
321
322 if (!xfs_sb_version_hascrc(&mp->m_sb))
323 return;
324
325 if (bip)
326 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
327
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100328 xfs_buf_update_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF);
Dave Chinner517c2222013-04-24 18:58:55 +1000329}
330
331/*
332 * leaf/node format detection on trees is sketchy, so a node read can be done on
333 * leaf level blocks when detection identifies the tree as a node format tree
334 * incorrectly. In this case, we need to swap the verifier to match the correct
335 * format of the block being read.
336 */
337static void
338xfs_attr3_leaf_read_verify(
339 struct xfs_buf *bp)
340{
341 struct xfs_mount *mp = bp->b_target->bt_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800342 xfs_failaddr_t fa;
Dave Chinner517c2222013-04-24 18:58:55 +1000343
Eric Sandeence5028c2014-02-27 15:23:10 +1100344 if (xfs_sb_version_hascrc(&mp->m_sb) &&
345 !xfs_buf_verify_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800346 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
347 else {
348 fa = xfs_attr3_leaf_verify(bp);
349 if (fa)
350 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
351 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100352}
Dave Chinnerad14c332012-11-12 22:54:16 +1100353
Dave Chinner517c2222013-04-24 18:58:55 +1000354const struct xfs_buf_ops xfs_attr3_leaf_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100355 .name = "xfs_attr3_leaf",
Dave Chinner517c2222013-04-24 18:58:55 +1000356 .verify_read = xfs_attr3_leaf_read_verify,
357 .verify_write = xfs_attr3_leaf_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800358 .verify_struct = xfs_attr3_leaf_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100359};
Dave Chinner612cfbf2012-11-14 17:52:32 +1100360
Dave Chinnerad14c332012-11-12 22:54:16 +1100361int
Dave Chinner517c2222013-04-24 18:58:55 +1000362xfs_attr3_leaf_read(
Dave Chinnerad14c332012-11-12 22:54:16 +1100363 struct xfs_trans *tp,
364 struct xfs_inode *dp,
365 xfs_dablk_t bno,
366 xfs_daddr_t mappedbno,
367 struct xfs_buf **bpp)
368{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100369 int err;
370
371 err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
Dave Chinner517c2222013-04-24 18:58:55 +1000372 XFS_ATTR_FORK, &xfs_attr3_leaf_buf_ops);
Darrick J. Wongcd87d862017-07-07 18:55:17 -0700373 if (!err && tp && *bpp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100374 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_ATTR_LEAF_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100375 return err;
Dave Chinnerad14c332012-11-12 22:54:16 +1100376}
377
Tim Shimmin726801b2006-09-28 11:01:37 +1000378/*========================================================================
379 * Namespace helper routines
380 *========================================================================*/
381
Tim Shimmin726801b2006-09-28 11:01:37 +1000382/*
383 * If namespace bits don't match return 0.
384 * If all match then return 1.
385 */
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000386STATIC int
Tim Shimmin726801b2006-09-28 11:01:37 +1000387xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
388{
389 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393/*========================================================================
Nathan Scottd8cc8902005-11-02 10:34:53 +1100394 * External routines when attribute fork size < XFS_LITINO(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *========================================================================*/
396
397/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100398 * Query whether the requested number of additional bytes of extended
399 * attribute space will be able to fit inline.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000400 *
Nathan Scottd8cc8902005-11-02 10:34:53 +1100401 * Returns zero if not, else the di_forkoff fork offset to be used in the
402 * literal area for attribute data once the new bytes have been added.
403 *
404 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
405 * special case for dev/uuid inodes, they have fixed size data forks.
406 */
407int
408xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
409{
410 int offset;
411 int minforkoff; /* lower limit on valid forkoff locations */
412 int maxforkoff; /* upper limit on valid forkoff locations */
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000413 int dsize;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100414 xfs_mount_t *mp = dp->i_mount;
415
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100416 /* rounded down */
417 offset = (XFS_LITINO(mp, dp->i_d.di_version) - bytes) >> 3;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100418
Christoph Hellwig42b67dc2017-10-19 11:07:09 -0700419 if (dp->i_d.di_format == XFS_DINODE_FMT_DEV) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100420 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
421 return (offset >= minforkoff) ? minforkoff : 0;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100422 }
423
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000424 /*
425 * If the requested numbers of bytes is smaller or equal to the
426 * current attribute fork size we can always proceed.
427 *
428 * Note that if_bytes in the data fork might actually be larger than
429 * the current data fork size is due to delalloc extents. In that
430 * case either the extent count will go down when they are converted
431 * to real extents, or the delalloc conversion will take care of the
432 * literal area rebalancing.
433 */
434 if (bytes <= XFS_IFORK_ASIZE(dp))
435 return dp->i_d.di_forkoff;
436
437 /*
438 * For attr2 we can try to move the forkoff if there is space in the
439 * literal area, but for the old format we are done if there is no
440 * space in the fixed attribute fork.
441 */
442 if (!(mp->m_flags & XFS_MOUNT_ATTR2))
Nathan Scottda087ba2005-11-02 15:00:20 +1100443 return 0;
Nathan Scottda087ba2005-11-02 15:00:20 +1100444
Barry Naujoke5889e92007-02-10 18:35:58 +1100445 dsize = dp->i_df.if_bytes;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000446
Barry Naujoke5889e92007-02-10 18:35:58 +1100447 switch (dp->i_d.di_format) {
448 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000449 /*
Barry Naujoke5889e92007-02-10 18:35:58 +1100450 * If there is no attr fork and the data fork is extents,
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000451 * determine if creating the default attr fork will result
452 * in the extents form migrating to btree. If so, the
453 * minimum offset only needs to be the space required for
Barry Naujoke5889e92007-02-10 18:35:58 +1100454 * the btree root.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000455 */
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200456 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
457 xfs_default_attroffset(dp))
Barry Naujoke5889e92007-02-10 18:35:58 +1100458 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
459 break;
Barry Naujoke5889e92007-02-10 18:35:58 +1100460 case XFS_DINODE_FMT_BTREE:
461 /*
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000462 * If we have a data btree then keep forkoff if we have one,
463 * otherwise we are adding a new attr, so then we set
464 * minforkoff to where the btree root can finish so we have
Barry Naujoke5889e92007-02-10 18:35:58 +1100465 * plenty of room for attrs
466 */
467 if (dp->i_d.di_forkoff) {
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000468 if (offset < dp->i_d.di_forkoff)
Barry Naujoke5889e92007-02-10 18:35:58 +1100469 return 0;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000470 return dp->i_d.di_forkoff;
471 }
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500472 dsize = XFS_BMAP_BROOT_SPACE(mp, dp->i_df.if_broot);
Barry Naujoke5889e92007-02-10 18:35:58 +1100473 break;
474 }
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000475
476 /*
477 * A data fork btree root must have space for at least
Barry Naujoke5889e92007-02-10 18:35:58 +1100478 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
479 */
480 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100481 minforkoff = roundup(minforkoff, 8) >> 3;
482
483 /* attr fork btree root can have at least this many key/ptr pairs */
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100484 maxforkoff = XFS_LITINO(mp, dp->i_d.di_version) -
485 XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100486 maxforkoff = maxforkoff >> 3; /* rounded down */
487
Nathan Scottd8cc8902005-11-02 10:34:53 +1100488 if (offset >= maxforkoff)
489 return maxforkoff;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000490 if (offset >= minforkoff)
491 return offset;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100492 return 0;
493}
494
495/*
496 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
497 */
498STATIC void
499xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
500{
Nathan Scott13059ff2006-01-11 15:32:01 +1100501 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
Eric Sandeen62118702008-03-06 13:44:28 +1100502 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000503 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +1100504 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
505 xfs_sb_version_addattr2(&mp->m_sb);
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000506 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +1100507 xfs_log_sb(tp);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100508 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000509 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100510 }
511}
512
513/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 * Create the initial contents of a shortform attribute list.
515 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100516void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517xfs_attr_shortform_create(xfs_da_args_t *args)
518{
519 xfs_attr_sf_hdr_t *hdr;
520 xfs_inode_t *dp;
521 xfs_ifork_t *ifp;
522
Dave Chinner5a5881c2012-03-22 05:15:13 +0000523 trace_xfs_attr_sf_create(args);
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 dp = args->dp;
526 ASSERT(dp != NULL);
527 ifp = dp->i_afp;
528 ASSERT(ifp != NULL);
529 ASSERT(ifp->if_bytes == 0);
530 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
531 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
532 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
533 ifp->if_flags |= XFS_IFINLINE;
534 } else {
535 ASSERT(ifp->if_flags & XFS_IFINLINE);
536 }
537 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
538 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
539 hdr->count = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100540 hdr->totsize = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544/*
545 * Add a name/value pair to the shortform attribute list.
546 * Overflow from the inode has already been checked for.
547 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100548void
549xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 xfs_attr_shortform_t *sf;
552 xfs_attr_sf_entry_t *sfe;
553 int i, offset, size;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100554 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 xfs_inode_t *dp;
556 xfs_ifork_t *ifp;
557
Dave Chinner5a5881c2012-03-22 05:15:13 +0000558 trace_xfs_attr_sf_add(args);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100561 mp = dp->i_mount;
562 dp->i_d.di_forkoff = forkoff;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 ifp = dp->i_afp;
565 ASSERT(ifp->if_flags & XFS_IFINLINE);
566 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
567 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100568 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100569#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (sfe->namelen != args->namelen)
571 continue;
572 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
573 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000574 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 continue;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100576 ASSERT(0);
577#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
580 offset = (char *)sfe - (char *)sf;
581 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
582 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
583 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
584 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
585
586 sfe->namelen = args->namelen;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100587 sfe->valuelen = args->valuelen;
Tim Shimmin726801b2006-09-28 11:01:37 +1000588 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 memcpy(sfe->nameval, args->name, args->namelen);
590 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100591 sf->hdr.count++;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800592 be16_add_cpu(&sf->hdr.totsize, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
594
Nathan Scottd8cc8902005-11-02 10:34:53 +1100595 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598/*
Christoph Hellwige1486de2009-02-04 09:36:00 +0100599 * After the last attribute is removed revert to original inode format,
600 * making all literal area available to the data fork once more.
601 */
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000602void
603xfs_attr_fork_remove(
Christoph Hellwige1486de2009-02-04 09:36:00 +0100604 struct xfs_inode *ip,
605 struct xfs_trans *tp)
606{
607 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
608 ip->i_d.di_forkoff = 0;
609 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
610
611 ASSERT(ip->i_d.di_anextents == 0);
612 ASSERT(ip->i_afp == NULL);
613
Christoph Hellwige1486de2009-02-04 09:36:00 +0100614 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
615}
616
617/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100618 * Remove an attribute from the shortform attribute list structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
620int
621xfs_attr_shortform_remove(xfs_da_args_t *args)
622{
623 xfs_attr_shortform_t *sf;
624 xfs_attr_sf_entry_t *sfe;
625 int base, size=0, end, totsize, i;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100626 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 xfs_inode_t *dp;
628
Dave Chinner5a5881c2012-03-22 05:15:13 +0000629 trace_xfs_attr_sf_remove(args);
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100632 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 base = sizeof(xfs_attr_sf_hdr_t);
634 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
635 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100636 end = sf->hdr.count;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100637 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 base += size, i++) {
639 size = XFS_ATTR_SF_ENTSIZE(sfe);
640 if (sfe->namelen != args->namelen)
641 continue;
642 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
643 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000644 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 continue;
646 break;
647 }
Nathan Scottd8cc8902005-11-02 10:34:53 +1100648 if (i == end)
Dave Chinner24513372014-06-25 14:58:08 +1000649 return -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Nathan Scottd8cc8902005-11-02 10:34:53 +1100651 /*
652 * Fix up the attribute fork data, covering the hole
653 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 end = base + size;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100655 totsize = be16_to_cpu(sf->hdr.totsize);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100656 if (end != totsize)
657 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100658 sf->hdr.count--;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800659 be16_add_cpu(&sf->hdr.totsize, -size);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100660
661 /*
662 * Fix up the start offset of the attribute fork
663 */
664 totsize -= size;
Barry Naujok6a178102008-05-21 16:42:05 +1000665 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
Christoph Hellwige1486de2009-02-04 09:36:00 +0100666 (mp->m_flags & XFS_MOUNT_ATTR2) &&
667 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
668 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000669 xfs_attr_fork_remove(dp, args->trans);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100670 } else {
671 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
672 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
673 ASSERT(dp->i_d.di_forkoff);
Barry Naujok6a178102008-05-21 16:42:05 +1000674 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
675 (args->op_flags & XFS_DA_OP_ADDNAME) ||
676 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
677 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100678 xfs_trans_log_inode(args->trans, dp,
679 XFS_ILOG_CORE | XFS_ILOG_ADATA);
680 }
681
682 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Eric Sandeend99831f2014-06-22 15:03:54 +1000684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687/*
688 * Look up a name in a shortform attribute list structure.
689 */
690/*ARGSUSED*/
691int
692xfs_attr_shortform_lookup(xfs_da_args_t *args)
693{
694 xfs_attr_shortform_t *sf;
695 xfs_attr_sf_entry_t *sfe;
696 int i;
697 xfs_ifork_t *ifp;
698
Dave Chinner5a5881c2012-03-22 05:15:13 +0000699 trace_xfs_attr_sf_lookup(args);
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 ifp = args->dp->i_afp;
702 ASSERT(ifp->if_flags & XFS_IFINLINE);
703 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
704 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100705 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
707 if (sfe->namelen != args->namelen)
708 continue;
709 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
710 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000711 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 continue;
Dave Chinner24513372014-06-25 14:58:08 +1000713 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Dave Chinner24513372014-06-25 14:58:08 +1000715 return -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718/*
719 * Look up a name in a shortform attribute list structure.
720 */
721/*ARGSUSED*/
722int
723xfs_attr_shortform_getvalue(xfs_da_args_t *args)
724{
725 xfs_attr_shortform_t *sf;
726 xfs_attr_sf_entry_t *sfe;
727 int i;
728
Eric Sandeen914ed442012-03-30 11:24:11 -0500729 ASSERT(args->dp->i_afp->if_flags == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
731 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100732 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
734 if (sfe->namelen != args->namelen)
735 continue;
736 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
737 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000738 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 continue;
740 if (args->flags & ATTR_KERNOVAL) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100741 args->valuelen = sfe->valuelen;
Dave Chinner24513372014-06-25 14:58:08 +1000742 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100744 if (args->valuelen < sfe->valuelen) {
745 args->valuelen = sfe->valuelen;
Dave Chinner24513372014-06-25 14:58:08 +1000746 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100748 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 memcpy(args->value, &sfe->nameval[args->namelen],
750 args->valuelen);
Dave Chinner24513372014-06-25 14:58:08 +1000751 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Dave Chinner24513372014-06-25 14:58:08 +1000753 return -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756/*
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800757 * Convert from using the shortform to the leaf. On success, return the
758 * buffer so that we can keep it locked until we're totally done with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 */
760int
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800761xfs_attr_shortform_to_leaf(
762 struct xfs_da_args *args,
763 struct xfs_buf **leaf_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 xfs_inode_t *dp;
766 xfs_attr_shortform_t *sf;
767 xfs_attr_sf_entry_t *sfe;
768 xfs_da_args_t nargs;
769 char *tmpbuffer;
770 int error, i, size;
771 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000772 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 xfs_ifork_t *ifp;
774
Dave Chinner5a5881c2012-03-22 05:15:13 +0000775 trace_xfs_attr_sf_to_leaf(args);
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 dp = args->dp;
778 ifp = dp->i_afp;
779 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100780 size = be16_to_cpu(sf->hdr.totsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 tmpbuffer = kmem_alloc(size, KM_SLEEP);
782 ASSERT(tmpbuffer != NULL);
783 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
784 sf = (xfs_attr_shortform_t *)tmpbuffer;
785
786 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000787 xfs_bmap_local_to_extents_empty(dp, XFS_ATTR_FORK);
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 bp = NULL;
790 error = xfs_da_grow_inode(args, &blkno);
791 if (error) {
792 /*
793 * If we hit an IO error middle of the transaction inside
794 * grow_inode(), we may have inconsistent data. Bail out.
795 */
Dave Chinner24513372014-06-25 14:58:08 +1000796 if (error == -EIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 goto out;
798 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
799 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
800 goto out;
801 }
802
803 ASSERT(blkno == 0);
Dave Chinner517c2222013-04-24 18:58:55 +1000804 error = xfs_attr3_leaf_create(args, blkno, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (error) {
806 error = xfs_da_shrink_inode(args, 0, bp);
807 bp = NULL;
808 if (error)
809 goto out;
810 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
811 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
812 goto out;
813 }
814
815 memset((char *)&nargs, 0, sizeof(nargs));
816 nargs.dp = dp;
Dave Chinner0650b552014-06-06 15:01:58 +1000817 nargs.geo = args->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 nargs.firstblock = args->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000819 nargs.dfops = args->dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 nargs.total = args->total;
821 nargs.whichfork = XFS_ATTR_FORK;
822 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000823 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100826 for (i = 0; i < sf->hdr.count; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100827 nargs.name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 nargs.namelen = sfe->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100829 nargs.value = &sfe->nameval[nargs.namelen];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100830 nargs.valuelen = sfe->valuelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100831 nargs.hashval = xfs_da_hashname(sfe->nameval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 sfe->namelen);
Tim Shimmin726801b2006-09-28 11:01:37 +1000833 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
Dave Chinner517c2222013-04-24 18:58:55 +1000834 error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */
Dave Chinner24513372014-06-25 14:58:08 +1000835 ASSERT(error == -ENOATTR);
Dave Chinner517c2222013-04-24 18:58:55 +1000836 error = xfs_attr3_leaf_add(bp, &nargs);
Dave Chinner24513372014-06-25 14:58:08 +1000837 ASSERT(error != -ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (error)
839 goto out;
840 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
841 }
842 error = 0;
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800843 *leaf_bp = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000845 kmem_free(tmpbuffer);
Eric Sandeend99831f2014-06-22 15:03:54 +1000846 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849/*
850 * Check a leaf attribute block to see if all the entries would fit into
851 * a shortform attribute list.
852 */
853int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000854xfs_attr_shortform_allfit(
Dave Chinnerb38958d2013-05-20 09:51:14 +1000855 struct xfs_buf *bp,
856 struct xfs_inode *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Dave Chinnerb38958d2013-05-20 09:51:14 +1000858 struct xfs_attr_leafblock *leaf;
859 struct xfs_attr_leaf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 xfs_attr_leaf_name_local_t *name_loc;
Dave Chinnerb38958d2013-05-20 09:51:14 +1000861 struct xfs_attr3_icleaf_hdr leafhdr;
862 int bytes;
863 int i;
Brian Foster2f661242015-04-13 11:26:02 +1000864 struct xfs_mount *mp = bp->b_target->bt_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Dave Chinner1d9025e2012-06-22 18:50:14 +1000866 leaf = bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +1000867 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
Dave Chinnerb38958d2013-05-20 09:51:14 +1000868 entry = xfs_attr3_leaf_entryp(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 bytes = sizeof(struct xfs_attr_sf_hdr);
Dave Chinnerb38958d2013-05-20 09:51:14 +1000871 for (i = 0; i < leafhdr.count; entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (entry->flags & XFS_ATTR_INCOMPLETE)
873 continue; /* don't copy partial entries */
874 if (!(entry->flags & XFS_ATTR_LOCAL))
Eric Sandeend99831f2014-06-22 15:03:54 +1000875 return 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000876 name_loc = xfs_attr3_leaf_name_local(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Eric Sandeend99831f2014-06-22 15:03:54 +1000878 return 0;
Nathan Scott053b5752006-03-17 17:29:09 +1100879 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
Eric Sandeend99831f2014-06-22 15:03:54 +1000880 return 0;
Dave Chinnerb38958d2013-05-20 09:51:14 +1000881 bytes += sizeof(struct xfs_attr_sf_entry) - 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 + name_loc->namelen
Nathan Scott053b5752006-03-17 17:29:09 +1100883 + be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Nathan Scott13059ff2006-01-11 15:32:01 +1100885 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
Barry Naujoke5889e92007-02-10 18:35:58 +1100886 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scotte0144ca2005-11-25 16:42:22 +1100887 (bytes == sizeof(struct xfs_attr_sf_hdr)))
Dave Chinnerb38958d2013-05-20 09:51:14 +1000888 return -1;
889 return xfs_attr_shortform_bytesfit(dp, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Darrick J. Wong1e1bbd82018-01-08 10:51:05 -0800892/* Verify the consistency of an inline attribute fork. */
893xfs_failaddr_t
894xfs_attr_shortform_verify(
895 struct xfs_inode *ip)
896{
897 struct xfs_attr_shortform *sfp;
898 struct xfs_attr_sf_entry *sfep;
899 struct xfs_attr_sf_entry *next_sfep;
900 char *endp;
901 struct xfs_ifork *ifp;
902 int i;
903 int size;
904
905 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL);
906 ifp = XFS_IFORK_PTR(ip, XFS_ATTR_FORK);
907 sfp = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
908 size = ifp->if_bytes;
909
910 /*
911 * Give up if the attribute is way too short.
912 */
913 if (size < sizeof(struct xfs_attr_sf_hdr))
914 return __this_address;
915
916 endp = (char *)sfp + size;
917
918 /* Check all reported entries */
919 sfep = &sfp->list[0];
920 for (i = 0; i < sfp->hdr.count; i++) {
921 /*
922 * struct xfs_attr_sf_entry has a variable length.
923 * Check the fixed-offset parts of the structure are
924 * within the data buffer.
925 */
926 if (((char *)sfep + sizeof(*sfep)) >= endp)
927 return __this_address;
928
929 /* Don't allow names with known bad length. */
930 if (sfep->namelen == 0)
931 return __this_address;
932
933 /*
934 * Check that the variable-length part of the structure is
935 * within the data buffer. The next entry starts after the
936 * name component, so nextentry is an acceptable test.
937 */
938 next_sfep = XFS_ATTR_SF_NEXTENTRY(sfep);
939 if ((char *)next_sfep > endp)
940 return __this_address;
941
942 /*
943 * Check for unknown flags. Short form doesn't support
944 * the incomplete or local bits, so we can use the namespace
945 * mask here.
946 */
947 if (sfep->flags & ~XFS_ATTR_NSP_ONDISK_MASK)
948 return __this_address;
949
950 /*
951 * Check for invalid namespace combinations. We only allow
952 * one namespace flag per xattr, so we can just count the
953 * bits (i.e. hweight) here.
954 */
955 if (hweight8(sfep->flags & XFS_ATTR_NSP_ONDISK_MASK) > 1)
956 return __this_address;
957
958 sfep = next_sfep;
959 }
960 if ((void *)sfep != (void *)endp)
961 return __this_address;
962
963 return NULL;
964}
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966/*
967 * Convert a leaf attribute list to shortform attribute list
968 */
969int
Dave Chinner517c2222013-04-24 18:58:55 +1000970xfs_attr3_leaf_to_shortform(
971 struct xfs_buf *bp,
972 struct xfs_da_args *args,
973 int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Dave Chinner517c2222013-04-24 18:58:55 +1000975 struct xfs_attr_leafblock *leaf;
976 struct xfs_attr3_icleaf_hdr ichdr;
977 struct xfs_attr_leaf_entry *entry;
978 struct xfs_attr_leaf_name_local *name_loc;
979 struct xfs_da_args nargs;
980 struct xfs_inode *dp = args->dp;
981 char *tmpbuffer;
982 int error;
983 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Dave Chinner5a5881c2012-03-22 05:15:13 +0000985 trace_xfs_attr_leaf_to_sf(args);
986
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000987 tmpbuffer = kmem_alloc(args->geo->blksize, KM_SLEEP);
Dave Chinner517c2222013-04-24 18:58:55 +1000988 if (!tmpbuffer)
Dave Chinner24513372014-06-25 14:58:08 +1000989 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000991 memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +1000992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
Brian Foster2f661242015-04-13 11:26:02 +1000994 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +1000995 entry = xfs_attr3_leaf_entryp(leaf);
996
997 /* XXX (dgc): buffer is about to be marked stale - why zero it? */
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000998 memset(bp->b_addr, 0, args->geo->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 /*
1001 * Clean out the prior contents of the attribute list.
1002 */
1003 error = xfs_da_shrink_inode(args, 0, bp);
1004 if (error)
1005 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001006
1007 if (forkoff == -1) {
Nathan Scott13059ff2006-01-11 15:32:01 +11001008 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
Barry Naujoke5889e92007-02-10 18:35:58 +11001009 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
Dave Chinner6dfe5a02015-05-29 07:40:08 +10001010 xfs_attr_fork_remove(dp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001012 }
1013
1014 xfs_attr_shortform_create(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 /*
1017 * Copy the attributes
1018 */
1019 memset((char *)&nargs, 0, sizeof(nargs));
Dave Chinner0650b552014-06-06 15:01:58 +10001020 nargs.geo = args->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 nargs.dp = dp;
1022 nargs.firstblock = args->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001023 nargs.dfops = args->dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 nargs.total = args->total;
1025 nargs.whichfork = XFS_ATTR_FORK;
1026 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +10001027 nargs.op_flags = XFS_DA_OP_OKNOENT;
Dave Chinner517c2222013-04-24 18:58:55 +10001028
1029 for (i = 0; i < ichdr.count; entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (entry->flags & XFS_ATTR_INCOMPLETE)
1031 continue; /* don't copy partial entries */
1032 if (!entry->nameidx)
1033 continue;
1034 ASSERT(entry->flags & XFS_ATTR_LOCAL);
Dave Chinner517c2222013-04-24 18:58:55 +10001035 name_loc = xfs_attr3_leaf_name_local(leaf, i);
Dave Chinnera9273ca2010-01-20 10:47:48 +11001036 nargs.name = name_loc->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 nargs.namelen = name_loc->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +11001038 nargs.value = &name_loc->nameval[nargs.namelen];
Nathan Scott053b5752006-03-17 17:29:09 +11001039 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001040 nargs.hashval = be32_to_cpu(entry->hashval);
Tim Shimmin726801b2006-09-28 11:01:37 +10001041 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
Nathan Scottd8cc8902005-11-02 10:34:53 +11001042 xfs_attr_shortform_add(&nargs, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044 error = 0;
1045
1046out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001047 kmem_free(tmpbuffer);
Dave Chinner517c2222013-04-24 18:58:55 +10001048 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051/*
1052 * Convert from using a single leaf to a root node and a leaf.
1053 */
1054int
Dave Chinner517c2222013-04-24 18:58:55 +10001055xfs_attr3_leaf_to_node(
1056 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Dave Chinner517c2222013-04-24 18:58:55 +10001058 struct xfs_attr_leafblock *leaf;
1059 struct xfs_attr3_icleaf_hdr icleafhdr;
1060 struct xfs_attr_leaf_entry *entries;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001061 struct xfs_da_node_entry *btree;
Dave Chinner517c2222013-04-24 18:58:55 +10001062 struct xfs_da3_icnode_hdr icnodehdr;
1063 struct xfs_da_intnode *node;
1064 struct xfs_inode *dp = args->dp;
1065 struct xfs_mount *mp = dp->i_mount;
1066 struct xfs_buf *bp1 = NULL;
1067 struct xfs_buf *bp2 = NULL;
1068 xfs_dablk_t blkno;
1069 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Dave Chinner5a5881c2012-03-22 05:15:13 +00001071 trace_xfs_attr_leaf_to_node(args);
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 error = xfs_da_grow_inode(args, &blkno);
1074 if (error)
1075 goto out;
Dave Chinner517c2222013-04-24 18:58:55 +10001076 error = xfs_attr3_leaf_read(args->trans, dp, 0, -1, &bp1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (error)
1078 goto out;
Dave Chinnerad14c332012-11-12 22:54:16 +11001079
Dave Chinner517c2222013-04-24 18:58:55 +10001080 error = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp2, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (error)
1082 goto out;
Dave Chinner517c2222013-04-24 18:58:55 +10001083
1084 /* copy leaf to new buffer, update identifiers */
Dave Chinner61fe1352013-04-03 16:11:30 +11001085 xfs_trans_buf_set_type(args->trans, bp2, XFS_BLFT_ATTR_LEAF_BUF);
Dave Chinner1813dd62012-11-14 17:54:40 +11001086 bp2->b_ops = bp1->b_ops;
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001087 memcpy(bp2->b_addr, bp1->b_addr, args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10001088 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1089 struct xfs_da3_blkinfo *hdr3 = bp2->b_addr;
1090 hdr3->blkno = cpu_to_be64(bp2->b_bn);
1091 }
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001092 xfs_trans_log_buf(args->trans, bp2, 0, args->geo->blksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 /*
1095 * Set up the new root node.
1096 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001097 error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (error)
1099 goto out;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001100 node = bp1->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001101 dp->d_ops->node_hdr_from_disk(&icnodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001102 btree = dp->d_ops->node_tree_p(node);
Dave Chinner517c2222013-04-24 18:58:55 +10001103
1104 leaf = bp2->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10001105 xfs_attr3_leaf_hdr_from_disk(args->geo, &icleafhdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10001106 entries = xfs_attr3_leaf_entryp(leaf);
1107
1108 /* both on-disk, don't endian-flip twice */
1109 btree[0].hashval = entries[icleafhdr.count - 1].hashval;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001110 btree[0].before = cpu_to_be32(blkno);
Dave Chinner517c2222013-04-24 18:58:55 +10001111 icnodehdr.count = 1;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001112 dp->d_ops->node_hdr_to_disk(node, &icnodehdr);
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001113 xfs_trans_log_buf(args->trans, bp1, 0, args->geo->blksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 error = 0;
1115out:
Dave Chinner517c2222013-04-24 18:58:55 +10001116 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119/*========================================================================
1120 * Routines used for growing the Btree.
1121 *========================================================================*/
1122
1123/*
1124 * Create the initial contents of a leaf attribute list
1125 * or a leaf in a node attribute list.
1126 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001127STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001128xfs_attr3_leaf_create(
1129 struct xfs_da_args *args,
1130 xfs_dablk_t blkno,
1131 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Dave Chinner517c2222013-04-24 18:58:55 +10001133 struct xfs_attr_leafblock *leaf;
1134 struct xfs_attr3_icleaf_hdr ichdr;
1135 struct xfs_inode *dp = args->dp;
1136 struct xfs_mount *mp = dp->i_mount;
1137 struct xfs_buf *bp;
1138 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Dave Chinner5a5881c2012-03-22 05:15:13 +00001140 trace_xfs_attr_leaf_create(args);
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
1143 XFS_ATTR_FORK);
1144 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10001145 return error;
1146 bp->b_ops = &xfs_attr3_leaf_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +11001147 xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001148 leaf = bp->b_addr;
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001149 memset(leaf, 0, args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10001150
1151 memset(&ichdr, 0, sizeof(ichdr));
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001152 ichdr.firstused = args->geo->blksize;
Dave Chinner517c2222013-04-24 18:58:55 +10001153
1154 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1155 struct xfs_da3_blkinfo *hdr3 = bp->b_addr;
1156
1157 ichdr.magic = XFS_ATTR3_LEAF_MAGIC;
1158
1159 hdr3->blkno = cpu_to_be64(bp->b_bn);
1160 hdr3->owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +10001161 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinner517c2222013-04-24 18:58:55 +10001162
1163 ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr);
1164 } else {
1165 ichdr.magic = XFS_ATTR_LEAF_MAGIC;
1166 ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
Dave Chinner517c2222013-04-24 18:58:55 +10001168 ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Brian Foster2f661242015-04-13 11:26:02 +10001170 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001171 xfs_trans_log_buf(args->trans, bp, 0, args->geo->blksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 *bpp = bp;
Dave Chinner517c2222013-04-24 18:58:55 +10001174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177/*
1178 * Split the leaf node, rebalance, then add the new entry.
1179 */
1180int
Dave Chinner517c2222013-04-24 18:58:55 +10001181xfs_attr3_leaf_split(
1182 struct xfs_da_state *state,
1183 struct xfs_da_state_blk *oldblk,
1184 struct xfs_da_state_blk *newblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 xfs_dablk_t blkno;
1187 int error;
1188
Dave Chinner5a5881c2012-03-22 05:15:13 +00001189 trace_xfs_attr_leaf_split(state->args);
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /*
1192 * Allocate space for a new leaf node.
1193 */
1194 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1195 error = xfs_da_grow_inode(state->args, &blkno);
1196 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001197 return error;
Dave Chinner517c2222013-04-24 18:58:55 +10001198 error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001200 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 newblk->blkno = blkno;
1202 newblk->magic = XFS_ATTR_LEAF_MAGIC;
1203
1204 /*
1205 * Rebalance the entries across the two leaves.
1206 * NOTE: rebalance() currently depends on the 2nd block being empty.
1207 */
Dave Chinner517c2222013-04-24 18:58:55 +10001208 xfs_attr3_leaf_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001209 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001211 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 /*
1214 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1215 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1216 * "new" attrs info. Will need the "old" info to remove it later.
1217 *
1218 * Insert the "new" entry in the correct block.
1219 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001220 if (state->inleaf) {
1221 trace_xfs_attr_leaf_add_old(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +10001222 error = xfs_attr3_leaf_add(oldblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001223 } else {
1224 trace_xfs_attr_leaf_add_new(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +10001225 error = xfs_attr3_leaf_add(newblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /*
1229 * Update last hashval in each block since we added the name.
1230 */
1231 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1232 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
Eric Sandeend99831f2014-06-22 15:03:54 +10001233 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236/*
1237 * Add a name to the leaf attribute list structure.
1238 */
1239int
Dave Chinner517c2222013-04-24 18:58:55 +10001240xfs_attr3_leaf_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001241 struct xfs_buf *bp,
1242 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Dave Chinner517c2222013-04-24 18:58:55 +10001244 struct xfs_attr_leafblock *leaf;
1245 struct xfs_attr3_icleaf_hdr ichdr;
1246 int tablesize;
1247 int entsize;
1248 int sum;
1249 int tmp;
1250 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Dave Chinner5a5881c2012-03-22 05:15:13 +00001252 trace_xfs_attr_leaf_add(args);
1253
Dave Chinner1d9025e2012-06-22 18:50:14 +10001254 leaf = bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10001255 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10001256 ASSERT(args->index >= 0 && args->index <= ichdr.count);
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10001257 entsize = xfs_attr_leaf_newentsize(args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 /*
1260 * Search through freemap for first-fit on new name length.
1261 * (may need to figure in size of entry struct too)
1262 */
Dave Chinner517c2222013-04-24 18:58:55 +10001263 tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t)
1264 + xfs_attr3_leaf_hdr_size(leaf);
1265 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) {
1266 if (tablesize > ichdr.firstused) {
1267 sum += ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 continue;
1269 }
Dave Chinner517c2222013-04-24 18:58:55 +10001270 if (!ichdr.freemap[i].size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 continue; /* no space in this map */
1272 tmp = entsize;
Dave Chinner517c2222013-04-24 18:58:55 +10001273 if (ichdr.freemap[i].base < ichdr.firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 tmp += sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10001275 if (ichdr.freemap[i].size >= tmp) {
1276 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, i);
1277 goto out_log_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Dave Chinner517c2222013-04-24 18:58:55 +10001279 sum += ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281
1282 /*
1283 * If there are no holes in the address space of the block,
1284 * and we don't have enough freespace, then compaction will do us
1285 * no good and we should just give up.
1286 */
Dave Chinner517c2222013-04-24 18:58:55 +10001287 if (!ichdr.holes && sum < entsize)
Dave Chinner24513372014-06-25 14:58:08 +10001288 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 /*
1291 * Compact the entries to coalesce free space.
1292 * This may change the hdr->count via dropping INCOMPLETE entries.
1293 */
Dave Chinner517c2222013-04-24 18:58:55 +10001294 xfs_attr3_leaf_compact(args, &ichdr, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 /*
1297 * After compaction, the block is guaranteed to have only one
1298 * free region, in freemap[0]. If it is not big enough, give up.
1299 */
Dave Chinner517c2222013-04-24 18:58:55 +10001300 if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) {
Dave Chinner24513372014-06-25 14:58:08 +10001301 tmp = -ENOSPC;
Dave Chinner517c2222013-04-24 18:58:55 +10001302 goto out_log_hdr;
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Dave Chinner517c2222013-04-24 18:58:55 +10001305 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0);
1306
1307out_log_hdr:
Brian Foster2f661242015-04-13 11:26:02 +10001308 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
Dave Chinner517c2222013-04-24 18:58:55 +10001309 xfs_trans_log_buf(args->trans, bp,
1310 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
1311 xfs_attr3_leaf_hdr_size(leaf)));
1312 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
1315/*
1316 * Add a name to a leaf attribute list structure.
1317 */
1318STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001319xfs_attr3_leaf_add_work(
1320 struct xfs_buf *bp,
1321 struct xfs_attr3_icleaf_hdr *ichdr,
1322 struct xfs_da_args *args,
1323 int mapindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Dave Chinner517c2222013-04-24 18:58:55 +10001325 struct xfs_attr_leafblock *leaf;
1326 struct xfs_attr_leaf_entry *entry;
1327 struct xfs_attr_leaf_name_local *name_loc;
1328 struct xfs_attr_leaf_name_remote *name_rmt;
1329 struct xfs_mount *mp;
1330 int tmp;
1331 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Dave Chinneree732592012-11-12 22:53:53 +11001333 trace_xfs_attr_leaf_add_work(args);
1334
Dave Chinner1d9025e2012-06-22 18:50:14 +10001335 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001336 ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE);
1337 ASSERT(args->index >= 0 && args->index <= ichdr->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 /*
1340 * Force open some space in the entry array and fill it in.
1341 */
Dave Chinner517c2222013-04-24 18:58:55 +10001342 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1343 if (args->index < ichdr->count) {
1344 tmp = ichdr->count - args->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 tmp *= sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10001346 memmove(entry + 1, entry, tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001347 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1349 }
Dave Chinner517c2222013-04-24 18:58:55 +10001350 ichdr->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 /*
1353 * Allocate space for the new string (at the end of the run).
1354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 mp = args->trans->t_mountp;
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001356 ASSERT(ichdr->freemap[mapindex].base < args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10001357 ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0);
1358 ASSERT(ichdr->freemap[mapindex].size >=
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10001359 xfs_attr_leaf_newentsize(args, NULL));
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001360 ASSERT(ichdr->freemap[mapindex].size < args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10001361 ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0);
1362
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10001363 ichdr->freemap[mapindex].size -= xfs_attr_leaf_newentsize(args, &tmp);
Dave Chinner517c2222013-04-24 18:58:55 +10001364
1365 entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base +
1366 ichdr->freemap[mapindex].size);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001367 entry->hashval = cpu_to_be32(args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10001369 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Barry Naujok6a178102008-05-21 16:42:05 +10001370 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 entry->flags |= XFS_ATTR_INCOMPLETE;
1372 if ((args->blkno2 == args->blkno) &&
1373 (args->index2 <= args->index)) {
1374 args->index2++;
1375 }
1376 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001377 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001379 ASSERT((args->index == 0) ||
1380 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
Dave Chinner517c2222013-04-24 18:58:55 +10001381 ASSERT((args->index == ichdr->count - 1) ||
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001382 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 * For "remote" attribute values, simply note that we need to
1386 * allocate space for the "remote" value. We can't actually
1387 * allocate the extents in this transaction, and we can't decide
1388 * which blocks they should be as we might allocate more blocks
1389 * as part of this transaction (a split operation for example).
1390 */
1391 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10001392 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 name_loc->namelen = args->namelen;
Nathan Scott053b5752006-03-17 17:29:09 +11001394 name_loc->valuelen = cpu_to_be16(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1396 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
Nathan Scott053b5752006-03-17 17:29:09 +11001397 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10001399 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 name_rmt->namelen = args->namelen;
1401 memcpy((char *)name_rmt->name, args->name, args->namelen);
1402 entry->flags |= XFS_ATTR_INCOMPLETE;
1403 /* just in case */
1404 name_rmt->valuelen = 0;
1405 name_rmt->valueblk = 0;
1406 args->rmtblkno = 1;
Dave Chinnerad1858d2013-05-21 18:02:08 +10001407 args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
Dave Chinner8275cdd2014-05-06 07:37:31 +10001408 args->rmtvaluelen = args->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001410 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10001411 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 xfs_attr_leaf_entsize(leaf, args->index)));
1413
1414 /*
1415 * Update the control info for this leaf node
1416 */
Dave Chinner517c2222013-04-24 18:58:55 +10001417 if (be16_to_cpu(entry->nameidx) < ichdr->firstused)
1418 ichdr->firstused = be16_to_cpu(entry->nameidx);
1419
1420 ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t)
1421 + xfs_attr3_leaf_hdr_size(leaf));
1422 tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t)
1423 + xfs_attr3_leaf_hdr_size(leaf);
1424
1425 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
1426 if (ichdr->freemap[i].base == tmp) {
1427 ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t);
1428 ichdr->freemap[i].size -= sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430 }
Dave Chinner517c2222013-04-24 18:58:55 +10001431 ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index);
1432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
1435/*
1436 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1437 */
1438STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10001439xfs_attr3_leaf_compact(
Dave Chinneree732592012-11-12 22:53:53 +11001440 struct xfs_da_args *args,
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001441 struct xfs_attr3_icleaf_hdr *ichdr_dst,
Dave Chinneree732592012-11-12 22:53:53 +11001442 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001444 struct xfs_attr_leafblock *leaf_src;
1445 struct xfs_attr_leafblock *leaf_dst;
1446 struct xfs_attr3_icleaf_hdr ichdr_src;
Dave Chinneree732592012-11-12 22:53:53 +11001447 struct xfs_trans *trans = args->trans;
Dave Chinneree732592012-11-12 22:53:53 +11001448 char *tmpbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Dave Chinneree732592012-11-12 22:53:53 +11001450 trace_xfs_attr_leaf_compact(args);
1451
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001452 tmpbuffer = kmem_alloc(args->geo->blksize, KM_SLEEP);
1453 memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
1454 memset(bp->b_addr, 0, args->geo->blksize);
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001455 leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
1456 leaf_dst = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 /*
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001459 * Copy the on-disk header back into the destination buffer to ensure
1460 * all the information in the header that is not part of the incore
1461 * header structure is preserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 */
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001463 memcpy(bp->b_addr, tmpbuffer, xfs_attr3_leaf_hdr_size(leaf_src));
1464
1465 /* Initialise the incore headers */
1466 ichdr_src = *ichdr_dst; /* struct copy */
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001467 ichdr_dst->firstused = args->geo->blksize;
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001468 ichdr_dst->usedbytes = 0;
1469 ichdr_dst->count = 0;
1470 ichdr_dst->holes = 0;
1471 ichdr_dst->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_src);
1472 ichdr_dst->freemap[0].size = ichdr_dst->firstused -
1473 ichdr_dst->freemap[0].base;
1474
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001475 /* write the header back to initialise the underlying buffer */
Brian Foster2f661242015-04-13 11:26:02 +10001476 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf_dst, ichdr_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 /*
1479 * Copy all entry's in the same (sorted) order,
1480 * but allocate name/value pairs packed and in sequence.
1481 */
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001482 xfs_attr3_leaf_moveents(args, leaf_src, &ichdr_src, 0,
1483 leaf_dst, ichdr_dst, 0, ichdr_src.count);
Dave Chinner517c2222013-04-24 18:58:55 +10001484 /*
1485 * this logs the entire buffer, but the caller must write the header
1486 * back to the buffer when it is finished modifying it.
1487 */
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001488 xfs_trans_log_buf(trans, bp, 0, args->geo->blksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001490 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
1493/*
Dave Chinner517c2222013-04-24 18:58:55 +10001494 * Compare two leaf blocks "order".
1495 * Return 0 unless leaf2 should go before leaf1.
1496 */
1497static int
1498xfs_attr3_leaf_order(
1499 struct xfs_buf *leaf1_bp,
1500 struct xfs_attr3_icleaf_hdr *leaf1hdr,
1501 struct xfs_buf *leaf2_bp,
1502 struct xfs_attr3_icleaf_hdr *leaf2hdr)
1503{
1504 struct xfs_attr_leaf_entry *entries1;
1505 struct xfs_attr_leaf_entry *entries2;
1506
1507 entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr);
1508 entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr);
1509 if (leaf1hdr->count > 0 && leaf2hdr->count > 0 &&
1510 ((be32_to_cpu(entries2[0].hashval) <
1511 be32_to_cpu(entries1[0].hashval)) ||
1512 (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) <
1513 be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) {
1514 return 1;
1515 }
1516 return 0;
1517}
1518
1519int
1520xfs_attr_leaf_order(
1521 struct xfs_buf *leaf1_bp,
1522 struct xfs_buf *leaf2_bp)
1523{
1524 struct xfs_attr3_icleaf_hdr ichdr1;
1525 struct xfs_attr3_icleaf_hdr ichdr2;
Brian Foster2f661242015-04-13 11:26:02 +10001526 struct xfs_mount *mp = leaf1_bp->b_target->bt_mount;
Dave Chinner517c2222013-04-24 18:58:55 +10001527
Brian Foster2f661242015-04-13 11:26:02 +10001528 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr1, leaf1_bp->b_addr);
1529 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr2, leaf2_bp->b_addr);
Dave Chinner517c2222013-04-24 18:58:55 +10001530 return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2);
1531}
1532
1533/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 * Redistribute the attribute list entries between two leaf nodes,
1535 * taking into account the size of the new entry.
1536 *
1537 * NOTE: if new block is empty, then it will get the upper half of the
1538 * old block. At present, all (one) callers pass in an empty second block.
1539 *
1540 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1541 * to match what it is doing in splitting the attribute leaf block. Those
1542 * values are used in "atomic rename" operations on attributes. Note that
1543 * the "new" and "old" values can end up in different blocks.
1544 */
1545STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10001546xfs_attr3_leaf_rebalance(
1547 struct xfs_da_state *state,
1548 struct xfs_da_state_blk *blk1,
1549 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Dave Chinner517c2222013-04-24 18:58:55 +10001551 struct xfs_da_args *args;
1552 struct xfs_attr_leafblock *leaf1;
1553 struct xfs_attr_leafblock *leaf2;
1554 struct xfs_attr3_icleaf_hdr ichdr1;
1555 struct xfs_attr3_icleaf_hdr ichdr2;
1556 struct xfs_attr_leaf_entry *entries1;
1557 struct xfs_attr_leaf_entry *entries2;
1558 int count;
1559 int totallen;
1560 int max;
1561 int space;
1562 int swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 /*
1565 * Set up environment.
1566 */
1567 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1568 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001569 leaf1 = blk1->bp->b_addr;
1570 leaf2 = blk2->bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10001571 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr1, leaf1);
1572 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, leaf2);
Dave Chinner517c2222013-04-24 18:58:55 +10001573 ASSERT(ichdr2.count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 args = state->args;
1575
Dave Chinner5a5881c2012-03-22 05:15:13 +00001576 trace_xfs_attr_leaf_rebalance(args);
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 /*
1579 * Check ordering of blocks, reverse if it makes things simpler.
1580 *
1581 * NOTE: Given that all (current) callers pass in an empty
1582 * second block, this code should never set "swap".
1583 */
1584 swap = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10001585 if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) {
1586 struct xfs_da_state_blk *tmp_blk;
1587 struct xfs_attr3_icleaf_hdr tmp_ichdr;
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 tmp_blk = blk1;
1590 blk1 = blk2;
1591 blk2 = tmp_blk;
Dave Chinner517c2222013-04-24 18:58:55 +10001592
1593 /* struct copies to swap them rather than reconverting */
1594 tmp_ichdr = ichdr1;
1595 ichdr1 = ichdr2;
1596 ichdr2 = tmp_ichdr;
1597
Dave Chinner1d9025e2012-06-22 18:50:14 +10001598 leaf1 = blk1->bp->b_addr;
1599 leaf2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 swap = 1;
1601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 /*
1604 * Examine entries until we reduce the absolute difference in
1605 * byte usage between the two blocks to a minimum. Then get
1606 * the direction to copy and the number of elements to move.
1607 *
1608 * "inleaf" is true if the new entry should be inserted into blk1.
1609 * If "swap" is also true, then reverse the sense of "inleaf".
1610 */
Dave Chinner517c2222013-04-24 18:58:55 +10001611 state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1,
1612 blk2, &ichdr2,
1613 &count, &totallen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (swap)
1615 state->inleaf = !state->inleaf;
1616
1617 /*
1618 * Move any entries required from leaf to leaf:
1619 */
Dave Chinner517c2222013-04-24 18:58:55 +10001620 if (count < ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 /*
1622 * Figure the total bytes to be added to the destination leaf.
1623 */
1624 /* number entries being moved */
Dave Chinner517c2222013-04-24 18:58:55 +10001625 count = ichdr1.count - count;
1626 space = ichdr1.usedbytes - totallen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 space += count * sizeof(xfs_attr_leaf_entry_t);
1628
1629 /*
1630 * leaf2 is the destination, compact it if it looks tight.
1631 */
Dave Chinner517c2222013-04-24 18:58:55 +10001632 max = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1633 max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001634 if (space > max)
Dave Chinner517c2222013-04-24 18:58:55 +10001635 xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 /*
1638 * Move high entries from leaf1 to low end of leaf2.
1639 */
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001640 xfs_attr3_leaf_moveents(args, leaf1, &ichdr1,
1641 ichdr1.count - count, leaf2, &ichdr2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Dave Chinner517c2222013-04-24 18:58:55 +10001643 } else if (count > ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /*
1645 * I assert that since all callers pass in an empty
1646 * second buffer, this code should never execute.
1647 */
Dave Chinner07428d72012-11-12 22:09:44 +11001648 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 /*
1651 * Figure the total bytes to be added to the destination leaf.
1652 */
1653 /* number entries being moved */
Dave Chinner517c2222013-04-24 18:58:55 +10001654 count -= ichdr1.count;
1655 space = totallen - ichdr1.usedbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 space += count * sizeof(xfs_attr_leaf_entry_t);
1657
1658 /*
1659 * leaf1 is the destination, compact it if it looks tight.
1660 */
Dave Chinner517c2222013-04-24 18:58:55 +10001661 max = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1662 max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001663 if (space > max)
Dave Chinner517c2222013-04-24 18:58:55 +10001664 xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 /*
1667 * Move low entries from leaf2 to high end of leaf1.
1668 */
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001669 xfs_attr3_leaf_moveents(args, leaf2, &ichdr2, 0, leaf1, &ichdr1,
1670 ichdr1.count, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
1672
Brian Foster2f661242015-04-13 11:26:02 +10001673 xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf1, &ichdr1);
1674 xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf2, &ichdr2);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001675 xfs_trans_log_buf(args->trans, blk1->bp, 0, args->geo->blksize - 1);
1676 xfs_trans_log_buf(args->trans, blk2->bp, 0, args->geo->blksize - 1);
Dave Chinner517c2222013-04-24 18:58:55 +10001677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 /*
1679 * Copy out last hashval in each block for B-tree code.
1680 */
Dave Chinner517c2222013-04-24 18:58:55 +10001681 entries1 = xfs_attr3_leaf_entryp(leaf1);
1682 entries2 = xfs_attr3_leaf_entryp(leaf2);
1683 blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval);
1684 blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 /*
1687 * Adjust the expected index for insertion.
1688 * NOTE: this code depends on the (current) situation that the
1689 * second block was originally empty.
1690 *
1691 * If the insertion point moved to the 2nd block, we must adjust
1692 * the index. We must also track the entry just following the
1693 * new entry for use in an "atomic rename" operation, that entry
1694 * is always the "old" entry and the "new" entry is what we are
1695 * inserting. The index/blkno fields refer to the "old" entry,
1696 * while the index2/blkno2 fields refer to the "new" entry.
1697 */
Dave Chinner517c2222013-04-24 18:58:55 +10001698 if (blk1->index > ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 ASSERT(state->inleaf == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10001700 blk2->index = blk1->index - ichdr1.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 args->index = args->index2 = blk2->index;
1702 args->blkno = args->blkno2 = blk2->blkno;
Dave Chinner517c2222013-04-24 18:58:55 +10001703 } else if (blk1->index == ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (state->inleaf) {
1705 args->index = blk1->index;
1706 args->blkno = blk1->blkno;
1707 args->index2 = 0;
1708 args->blkno2 = blk2->blkno;
1709 } else {
Dave Chinner07428d72012-11-12 22:09:44 +11001710 /*
1711 * On a double leaf split, the original attr location
1712 * is already stored in blkno2/index2, so don't
1713 * overwrite it overwise we corrupt the tree.
1714 */
Dave Chinner517c2222013-04-24 18:58:55 +10001715 blk2->index = blk1->index - ichdr1.count;
Dave Chinner07428d72012-11-12 22:09:44 +11001716 args->index = blk2->index;
1717 args->blkno = blk2->blkno;
1718 if (!state->extravalid) {
1719 /*
1720 * set the new attr location to match the old
1721 * one and let the higher level split code
1722 * decide where in the leaf to place it.
1723 */
1724 args->index2 = blk2->index;
1725 args->blkno2 = blk2->blkno;
1726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
1728 } else {
1729 ASSERT(state->inleaf == 1);
1730 args->index = args->index2 = blk1->index;
1731 args->blkno = args->blkno2 = blk1->blkno;
1732 }
1733}
1734
1735/*
1736 * Examine entries until we reduce the absolute difference in
1737 * byte usage between the two blocks to a minimum.
1738 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1739 * GROT: there will always be enough room in either block for a new entry.
1740 * GROT: Do a double-split for this case?
1741 */
1742STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001743xfs_attr3_leaf_figure_balance(
1744 struct xfs_da_state *state,
1745 struct xfs_da_state_blk *blk1,
1746 struct xfs_attr3_icleaf_hdr *ichdr1,
1747 struct xfs_da_state_blk *blk2,
1748 struct xfs_attr3_icleaf_hdr *ichdr2,
1749 int *countarg,
1750 int *usedbytesarg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Dave Chinner517c2222013-04-24 18:58:55 +10001752 struct xfs_attr_leafblock *leaf1 = blk1->bp->b_addr;
1753 struct xfs_attr_leafblock *leaf2 = blk2->bp->b_addr;
1754 struct xfs_attr_leaf_entry *entry;
1755 int count;
1756 int max;
1757 int index;
1758 int totallen = 0;
1759 int half;
1760 int lastdelta;
1761 int foundit = 0;
1762 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 /*
1765 * Examine entries until we reduce the absolute difference in
1766 * byte usage between the two blocks to a minimum.
1767 */
Dave Chinner517c2222013-04-24 18:58:55 +10001768 max = ichdr1->count + ichdr2->count;
1769 half = (max + 1) * sizeof(*entry);
1770 half += ichdr1->usedbytes + ichdr2->usedbytes +
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10001771 xfs_attr_leaf_newentsize(state->args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 half /= 2;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001773 lastdelta = state->args->geo->blksize;
Dave Chinner517c2222013-04-24 18:58:55 +10001774 entry = xfs_attr3_leaf_entryp(leaf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 for (count = index = 0; count < max; entry++, index++, count++) {
1776
1777#define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1778 /*
1779 * The new entry is in the first block, account for it.
1780 */
1781 if (count == blk1->index) {
1782 tmp = totallen + sizeof(*entry) +
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10001783 xfs_attr_leaf_newentsize(state->args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1785 break;
1786 lastdelta = XFS_ATTR_ABS(half - tmp);
1787 totallen = tmp;
1788 foundit = 1;
1789 }
1790
1791 /*
1792 * Wrap around into the second block if necessary.
1793 */
Dave Chinner517c2222013-04-24 18:58:55 +10001794 if (count == ichdr1->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 leaf1 = leaf2;
Dave Chinner517c2222013-04-24 18:58:55 +10001796 entry = xfs_attr3_leaf_entryp(leaf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 index = 0;
1798 }
1799
1800 /*
1801 * Figure out if next leaf entry would be too much.
1802 */
1803 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1804 index);
1805 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1806 break;
1807 lastdelta = XFS_ATTR_ABS(half - tmp);
1808 totallen = tmp;
1809#undef XFS_ATTR_ABS
1810 }
1811
1812 /*
1813 * Calculate the number of usedbytes that will end up in lower block.
1814 * If new entry not in lower block, fix up the count.
1815 */
1816 totallen -= count * sizeof(*entry);
1817 if (foundit) {
1818 totallen -= sizeof(*entry) +
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10001819 xfs_attr_leaf_newentsize(state->args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
1821
1822 *countarg = count;
1823 *usedbytesarg = totallen;
Dave Chinner517c2222013-04-24 18:58:55 +10001824 return foundit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
1827/*========================================================================
1828 * Routines used for shrinking the Btree.
1829 *========================================================================*/
1830
1831/*
1832 * Check a leaf block and its neighbors to see if the block should be
1833 * collapsed into one or the other neighbor. Always keep the block
1834 * with the smaller block number.
1835 * If the current block is over 50% full, don't try to join it, return 0.
1836 * If the block is empty, fill in the state structure and return 2.
1837 * If it can be collapsed, fill in the state structure and return 1.
1838 * If nothing can be done, return 0.
1839 *
1840 * GROT: allow for INCOMPLETE entries in calculation.
1841 */
1842int
Dave Chinner517c2222013-04-24 18:58:55 +10001843xfs_attr3_leaf_toosmall(
1844 struct xfs_da_state *state,
1845 int *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
Dave Chinner517c2222013-04-24 18:58:55 +10001847 struct xfs_attr_leafblock *leaf;
1848 struct xfs_da_state_blk *blk;
1849 struct xfs_attr3_icleaf_hdr ichdr;
1850 struct xfs_buf *bp;
1851 xfs_dablk_t blkno;
1852 int bytes;
1853 int forward;
1854 int error;
1855 int retval;
1856 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Dave Chinneree732592012-11-12 22:53:53 +11001858 trace_xfs_attr_leaf_toosmall(state->args);
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /*
1861 * Check for the degenerate case of the block being over 50% full.
1862 * If so, it's not worth even looking to see if we might be able
1863 * to coalesce with a sibling.
1864 */
1865 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner517c2222013-04-24 18:58:55 +10001866 leaf = blk->bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10001867 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10001868 bytes = xfs_attr3_leaf_hdr_size(leaf) +
1869 ichdr.count * sizeof(xfs_attr_leaf_entry_t) +
1870 ichdr.usedbytes;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001871 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 *action = 0; /* blk over 50%, don't try to join */
Eric Sandeend99831f2014-06-22 15:03:54 +10001873 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875
1876 /*
1877 * Check for the degenerate case of the block being empty.
1878 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001879 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 * to merge with the forward block unless it is NULL.
1881 */
Dave Chinner517c2222013-04-24 18:58:55 +10001882 if (ichdr.count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 /*
1884 * Make altpath point to the block we want to keep and
1885 * path point to the block we want to drop (this one).
1886 */
Dave Chinner517c2222013-04-24 18:58:55 +10001887 forward = (ichdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001889 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 0, &retval);
1891 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001892 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (retval) {
1894 *action = 0;
1895 } else {
1896 *action = 2;
1897 }
Dave Chinner517c2222013-04-24 18:58:55 +10001898 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900
1901 /*
1902 * Examine each sibling block to see if we can coalesce with
1903 * at least 25% free space to spare. We need to figure out
1904 * whether to merge with the forward or the backward block.
1905 * We prefer coalescing with the lower numbered sibling so as
1906 * to shrink an attribute list over time.
1907 */
1908 /* start with smaller blk num */
Dave Chinner517c2222013-04-24 18:58:55 +10001909 forward = ichdr.forw < ichdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 for (i = 0; i < 2; forward = !forward, i++) {
Dave Chinner517c2222013-04-24 18:58:55 +10001911 struct xfs_attr3_icleaf_hdr ichdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (forward)
Dave Chinner517c2222013-04-24 18:58:55 +10001913 blkno = ichdr.forw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 else
Dave Chinner517c2222013-04-24 18:58:55 +10001915 blkno = ichdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (blkno == 0)
1917 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10001918 error = xfs_attr3_leaf_read(state->args->trans, state->args->dp,
Dave Chinnerad14c332012-11-12 22:54:16 +11001919 blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001921 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Brian Foster2f661242015-04-13 11:26:02 +10001923 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, bp->b_addr);
Dave Chinner517c2222013-04-24 18:58:55 +10001924
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001925 bytes = state->args->geo->blksize -
1926 (state->args->geo->blksize >> 2) -
Dave Chinner517c2222013-04-24 18:58:55 +10001927 ichdr.usedbytes - ichdr2.usedbytes -
1928 ((ichdr.count + ichdr2.count) *
1929 sizeof(xfs_attr_leaf_entry_t)) -
1930 xfs_attr3_leaf_hdr_size(leaf);
1931
Dave Chinner1d9025e2012-06-22 18:50:14 +10001932 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (bytes >= 0)
1934 break; /* fits with at least 25% to spare */
1935 }
1936 if (i >= 2) {
1937 *action = 0;
Eric Sandeend99831f2014-06-22 15:03:54 +10001938 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
1940
1941 /*
1942 * Make altpath point to the block we want to keep (the lower
1943 * numbered block) and path point to the block we want to drop.
1944 */
1945 memcpy(&state->altpath, &state->path, sizeof(state->path));
1946 if (blkno < blk->blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001947 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 0, &retval);
1949 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001950 error = xfs_da3_path_shift(state, &state->path, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 0, &retval);
1952 }
1953 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001954 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (retval) {
1956 *action = 0;
1957 } else {
1958 *action = 1;
1959 }
Eric Sandeend99831f2014-06-22 15:03:54 +10001960 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
1963/*
1964 * Remove a name from the leaf attribute list structure.
1965 *
1966 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1967 * If two leaves are 37% full, when combined they will leave 25% free.
1968 */
1969int
Dave Chinner517c2222013-04-24 18:58:55 +10001970xfs_attr3_leaf_remove(
1971 struct xfs_buf *bp,
1972 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Dave Chinner517c2222013-04-24 18:58:55 +10001974 struct xfs_attr_leafblock *leaf;
1975 struct xfs_attr3_icleaf_hdr ichdr;
1976 struct xfs_attr_leaf_entry *entry;
Dave Chinner517c2222013-04-24 18:58:55 +10001977 int before;
1978 int after;
1979 int smallest;
1980 int entsize;
1981 int tablesize;
1982 int tmp;
1983 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Dave Chinneree732592012-11-12 22:53:53 +11001985 trace_xfs_attr_leaf_remove(args);
1986
Dave Chinner1d9025e2012-06-22 18:50:14 +10001987 leaf = bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10001988 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10001989
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001990 ASSERT(ichdr.count > 0 && ichdr.count < args->geo->blksize / 8);
Dave Chinner517c2222013-04-24 18:58:55 +10001991 ASSERT(args->index >= 0 && args->index < ichdr.count);
1992 ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) +
1993 xfs_attr3_leaf_hdr_size(leaf));
1994
1995 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1996
1997 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001998 ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 /*
2001 * Scan through free region table:
2002 * check for adjacency of free'd entry with an existing one,
2003 * find smallest free region in case we need to replace it,
2004 * adjust any map that borders the entry table,
2005 */
Dave Chinner517c2222013-04-24 18:58:55 +10002006 tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t)
2007 + xfs_attr3_leaf_hdr_size(leaf);
2008 tmp = ichdr.freemap[0].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 before = after = -1;
2010 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
2011 entsize = xfs_attr_leaf_entsize(leaf, args->index);
Dave Chinner517c2222013-04-24 18:58:55 +10002012 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002013 ASSERT(ichdr.freemap[i].base < args->geo->blksize);
2014 ASSERT(ichdr.freemap[i].size < args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10002015 if (ichdr.freemap[i].base == tablesize) {
2016 ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t);
2017 ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
2019
Dave Chinner517c2222013-04-24 18:58:55 +10002020 if (ichdr.freemap[i].base + ichdr.freemap[i].size ==
2021 be16_to_cpu(entry->nameidx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 before = i;
Dave Chinner517c2222013-04-24 18:58:55 +10002023 } else if (ichdr.freemap[i].base ==
2024 (be16_to_cpu(entry->nameidx) + entsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 after = i;
Dave Chinner517c2222013-04-24 18:58:55 +10002026 } else if (ichdr.freemap[i].size < tmp) {
2027 tmp = ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 smallest = i;
2029 }
2030 }
2031
2032 /*
2033 * Coalesce adjacent freemap regions,
2034 * or replace the smallest region.
2035 */
2036 if ((before >= 0) || (after >= 0)) {
2037 if ((before >= 0) && (after >= 0)) {
Dave Chinner517c2222013-04-24 18:58:55 +10002038 ichdr.freemap[before].size += entsize;
2039 ichdr.freemap[before].size += ichdr.freemap[after].size;
2040 ichdr.freemap[after].base = 0;
2041 ichdr.freemap[after].size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 } else if (before >= 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002043 ichdr.freemap[before].size += entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002045 ichdr.freemap[after].base = be16_to_cpu(entry->nameidx);
2046 ichdr.freemap[after].size += entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048 } else {
2049 /*
2050 * Replace smallest region (if it is smaller than free'd entry)
2051 */
Dave Chinner517c2222013-04-24 18:58:55 +10002052 if (ichdr.freemap[smallest].size < entsize) {
2053 ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx);
2054 ichdr.freemap[smallest].size = entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056 }
2057
2058 /*
2059 * Did we remove the first entry?
2060 */
Dave Chinner517c2222013-04-24 18:58:55 +10002061 if (be16_to_cpu(entry->nameidx) == ichdr.firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 smallest = 1;
2063 else
2064 smallest = 0;
2065
2066 /*
2067 * Compress the remaining entries and zero out the removed stuff.
2068 */
Dave Chinner517c2222013-04-24 18:58:55 +10002069 memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize);
2070 ichdr.usedbytes -= entsize;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002071 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002072 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 entsize));
2074
Dave Chinner517c2222013-04-24 18:58:55 +10002075 tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t);
2076 memmove(entry, entry + 1, tmp);
2077 ichdr.count--;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002078 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002079 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t)));
2080
2081 entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count];
2082 memset(entry, 0, sizeof(xfs_attr_leaf_entry_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 /*
2085 * If we removed the first entry, re-find the first used byte
2086 * in the name area. Note that if the entry was the "firstused",
2087 * then we don't have a "hole" in our block resulting from
2088 * removing the name.
2089 */
2090 if (smallest) {
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002091 tmp = args->geo->blksize;
Dave Chinner517c2222013-04-24 18:58:55 +10002092 entry = xfs_attr3_leaf_entryp(leaf);
2093 for (i = ichdr.count - 1; i >= 0; entry++, i--) {
2094 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002095 ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002096
2097 if (be16_to_cpu(entry->nameidx) < tmp)
2098 tmp = be16_to_cpu(entry->nameidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
Dave Chinner517c2222013-04-24 18:58:55 +10002100 ichdr.firstused = tmp;
Brian Foster66db8102015-04-13 11:27:59 +10002101 ASSERT(ichdr.firstused != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002103 ichdr.holes = 1; /* mark as needing compaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
Brian Foster2f661242015-04-13 11:26:02 +10002105 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002106 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002107 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
2108 xfs_attr3_leaf_hdr_size(leaf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 /*
2111 * Check if leaf is less than 50% full, caller may want to
2112 * "join" the leaf with a sibling if so.
2113 */
Dave Chinner517c2222013-04-24 18:58:55 +10002114 tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) +
2115 ichdr.count * sizeof(xfs_attr_leaf_entry_t);
2116
Dave Chinnered358c02014-06-06 15:18:10 +10002117 return tmp < args->geo->magicpct; /* leaf is < 37% full */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
2120/*
2121 * Move all the attribute list entries from drop_leaf into save_leaf.
2122 */
2123void
Dave Chinner517c2222013-04-24 18:58:55 +10002124xfs_attr3_leaf_unbalance(
2125 struct xfs_da_state *state,
2126 struct xfs_da_state_blk *drop_blk,
2127 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Dave Chinner517c2222013-04-24 18:58:55 +10002129 struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr;
2130 struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr;
2131 struct xfs_attr3_icleaf_hdr drophdr;
2132 struct xfs_attr3_icleaf_hdr savehdr;
2133 struct xfs_attr_leaf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Dave Chinner5a5881c2012-03-22 05:15:13 +00002135 trace_xfs_attr_leaf_unbalance(state->args);
2136
Dave Chinner1d9025e2012-06-22 18:50:14 +10002137 drop_leaf = drop_blk->bp->b_addr;
2138 save_leaf = save_blk->bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10002139 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &drophdr, drop_leaf);
2140 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &savehdr, save_leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10002141 entry = xfs_attr3_leaf_entryp(drop_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 /*
2144 * Save last hashval from dying block for later Btree fixup.
2145 */
Dave Chinner517c2222013-04-24 18:58:55 +10002146 drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 /*
2149 * Check if we need a temp buffer, or can we do it in place.
2150 * Note that we don't check "leaf" for holes because we will
2151 * always be dropping it, toosmall() decided that for us already.
2152 */
Dave Chinner517c2222013-04-24 18:58:55 +10002153 if (savehdr.holes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /*
2155 * dest leaf has no holes, so we add there. May need
2156 * to make some room in the entry array.
2157 */
Dave Chinner517c2222013-04-24 18:58:55 +10002158 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2159 drop_blk->bp, &drophdr)) {
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002160 xfs_attr3_leaf_moveents(state->args,
2161 drop_leaf, &drophdr, 0,
Dave Chinner517c2222013-04-24 18:58:55 +10002162 save_leaf, &savehdr, 0,
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002163 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 } else {
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002165 xfs_attr3_leaf_moveents(state->args,
2166 drop_leaf, &drophdr, 0,
Dave Chinner517c2222013-04-24 18:58:55 +10002167 save_leaf, &savehdr,
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002168 savehdr.count, drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
2170 } else {
2171 /*
2172 * Destination has holes, so we make a temporary copy
2173 * of the leaf and add them both to that.
2174 */
Dave Chinner517c2222013-04-24 18:58:55 +10002175 struct xfs_attr_leafblock *tmp_leaf;
2176 struct xfs_attr3_icleaf_hdr tmphdr;
2177
Dave Chinnerb2a21e72014-06-06 15:22:04 +10002178 tmp_leaf = kmem_zalloc(state->args->geo->blksize, KM_SLEEP);
Dave Chinner517c2222013-04-24 18:58:55 +10002179
Dave Chinner8517de22013-05-21 18:02:05 +10002180 /*
2181 * Copy the header into the temp leaf so that all the stuff
2182 * not in the incore header is present and gets copied back in
2183 * once we've moved all the entries.
2184 */
2185 memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf));
2186
2187 memset(&tmphdr, 0, sizeof(tmphdr));
Dave Chinner517c2222013-04-24 18:58:55 +10002188 tmphdr.magic = savehdr.magic;
2189 tmphdr.forw = savehdr.forw;
2190 tmphdr.back = savehdr.back;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10002191 tmphdr.firstused = state->args->geo->blksize;
Dave Chinner8517de22013-05-21 18:02:05 +10002192
2193 /* write the header to the temp buffer to initialise it */
Brian Foster2f661242015-04-13 11:26:02 +10002194 xfs_attr3_leaf_hdr_to_disk(state->args->geo, tmp_leaf, &tmphdr);
Dave Chinner8517de22013-05-21 18:02:05 +10002195
Dave Chinner517c2222013-04-24 18:58:55 +10002196 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2197 drop_blk->bp, &drophdr)) {
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002198 xfs_attr3_leaf_moveents(state->args,
2199 drop_leaf, &drophdr, 0,
Dave Chinner517c2222013-04-24 18:58:55 +10002200 tmp_leaf, &tmphdr, 0,
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002201 drophdr.count);
2202 xfs_attr3_leaf_moveents(state->args,
2203 save_leaf, &savehdr, 0,
Dave Chinner517c2222013-04-24 18:58:55 +10002204 tmp_leaf, &tmphdr, tmphdr.count,
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002205 savehdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 } else {
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002207 xfs_attr3_leaf_moveents(state->args,
2208 save_leaf, &savehdr, 0,
Dave Chinner517c2222013-04-24 18:58:55 +10002209 tmp_leaf, &tmphdr, 0,
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002210 savehdr.count);
2211 xfs_attr3_leaf_moveents(state->args,
2212 drop_leaf, &drophdr, 0,
Dave Chinner517c2222013-04-24 18:58:55 +10002213 tmp_leaf, &tmphdr, tmphdr.count,
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002214 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
Dave Chinnerb2a21e72014-06-06 15:22:04 +10002216 memcpy(save_leaf, tmp_leaf, state->args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10002217 savehdr = tmphdr; /* struct copy */
2218 kmem_free(tmp_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 }
2220
Brian Foster2f661242015-04-13 11:26:02 +10002221 xfs_attr3_leaf_hdr_to_disk(state->args->geo, save_leaf, &savehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002222 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
Dave Chinnerb2a21e72014-06-06 15:22:04 +10002223 state->args->geo->blksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 /*
2226 * Copy out last hashval in each block for B-tree code.
2227 */
Dave Chinner517c2222013-04-24 18:58:55 +10002228 entry = xfs_attr3_leaf_entryp(save_leaf);
2229 save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
2232/*========================================================================
2233 * Routines used for finding things in the Btree.
2234 *========================================================================*/
2235
2236/*
2237 * Look up a name in a leaf attribute list structure.
2238 * This is the internal routine, it uses the caller's buffer.
2239 *
2240 * Note that duplicate keys are allowed, but only check within the
2241 * current leaf node. The Btree code must check in adjacent leaf nodes.
2242 *
2243 * Return in args->index the index into the entry[] array of either
2244 * the found entry, or where the entry should have been (insert before
2245 * that entry).
2246 *
2247 * Don't change the args->value unless we find the attribute.
2248 */
2249int
Dave Chinner517c2222013-04-24 18:58:55 +10002250xfs_attr3_leaf_lookup_int(
2251 struct xfs_buf *bp,
2252 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Dave Chinner517c2222013-04-24 18:58:55 +10002254 struct xfs_attr_leafblock *leaf;
2255 struct xfs_attr3_icleaf_hdr ichdr;
2256 struct xfs_attr_leaf_entry *entry;
2257 struct xfs_attr_leaf_entry *entries;
2258 struct xfs_attr_leaf_name_local *name_loc;
2259 struct xfs_attr_leaf_name_remote *name_rmt;
2260 xfs_dahash_t hashval;
2261 int probe;
2262 int span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Dave Chinner5a5881c2012-03-22 05:15:13 +00002264 trace_xfs_attr_leaf_lookup(args);
2265
Dave Chinner1d9025e2012-06-22 18:50:14 +10002266 leaf = bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10002267 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10002268 entries = xfs_attr3_leaf_entryp(leaf);
Darrick J. Wong8ba92d42018-01-08 10:51:07 -08002269 if (ichdr.count >= args->geo->blksize / 8)
2270 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 /*
2273 * Binary search. (note: small blocks will skip this loop)
2274 */
2275 hashval = args->hashval;
Dave Chinner517c2222013-04-24 18:58:55 +10002276 probe = span = ichdr.count / 2;
2277 for (entry = &entries[probe]; span > 4; entry = &entries[probe]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 span /= 2;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002279 if (be32_to_cpu(entry->hashval) < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 probe += span;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002281 else if (be32_to_cpu(entry->hashval) > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 probe -= span;
2283 else
2284 break;
2285 }
Darrick J. Wong8ba92d42018-01-08 10:51:07 -08002286 if (!(probe >= 0 && (!ichdr.count || probe < ichdr.count)))
2287 return -EFSCORRUPTED;
2288 if (!(span <= 4 || be32_to_cpu(entry->hashval) == hashval))
2289 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 /*
2292 * Since we may have duplicate hashval's, find the first matching
2293 * hashval in the leaf.
2294 */
Dave Chinner517c2222013-04-24 18:58:55 +10002295 while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 entry--;
2297 probe--;
2298 }
Dave Chinner517c2222013-04-24 18:58:55 +10002299 while (probe < ichdr.count &&
2300 be32_to_cpu(entry->hashval) < hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 entry++;
2302 probe++;
2303 }
Dave Chinner517c2222013-04-24 18:58:55 +10002304 if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 args->index = probe;
Dave Chinner24513372014-06-25 14:58:08 +10002306 return -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
2308
2309 /*
2310 * Duplicate keys may be present, so search all of them for a match.
2311 */
Dave Chinner517c2222013-04-24 18:58:55 +10002312 for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 entry++, probe++) {
2314/*
2315 * GROT: Add code to remove incomplete entries.
2316 */
2317 /*
2318 * If we are looking for INCOMPLETE entries, show only those.
2319 * If we are looking for complete entries, show only those.
2320 */
2321 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2322 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2323 continue;
2324 }
2325 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002326 name_loc = xfs_attr3_leaf_name_local(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if (name_loc->namelen != args->namelen)
2328 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10002329 if (memcmp(args->name, name_loc->nameval,
2330 args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002332 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 continue;
2334 args->index = probe;
Dave Chinner24513372014-06-25 14:58:08 +10002335 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002337 name_rmt = xfs_attr3_leaf_name_remote(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 if (name_rmt->namelen != args->namelen)
2339 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10002340 if (memcmp(args->name, name_rmt->name,
2341 args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002343 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 continue;
2345 args->index = probe;
Dave Chinner8275cdd2014-05-06 07:37:31 +10002346 args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002347 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Dave Chinnerad1858d2013-05-21 18:02:08 +10002348 args->rmtblkcnt = xfs_attr3_rmt_blocks(
2349 args->dp->i_mount,
Dave Chinner8275cdd2014-05-06 07:37:31 +10002350 args->rmtvaluelen);
Dave Chinner24513372014-06-25 14:58:08 +10002351 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 }
2353 }
2354 args->index = probe;
Dave Chinner24513372014-06-25 14:58:08 +10002355 return -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
2358/*
2359 * Get the value associated with an attribute name from a leaf attribute
2360 * list structure.
2361 */
2362int
Dave Chinner517c2222013-04-24 18:58:55 +10002363xfs_attr3_leaf_getvalue(
2364 struct xfs_buf *bp,
2365 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
Dave Chinner517c2222013-04-24 18:58:55 +10002367 struct xfs_attr_leafblock *leaf;
2368 struct xfs_attr3_icleaf_hdr ichdr;
2369 struct xfs_attr_leaf_entry *entry;
2370 struct xfs_attr_leaf_name_local *name_loc;
2371 struct xfs_attr_leaf_name_remote *name_rmt;
2372 int valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Dave Chinner1d9025e2012-06-22 18:50:14 +10002374 leaf = bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10002375 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002376 ASSERT(ichdr.count < args->geo->blksize / 8);
Dave Chinner517c2222013-04-24 18:58:55 +10002377 ASSERT(args->index < ichdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Dave Chinner517c2222013-04-24 18:58:55 +10002379 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002381 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 ASSERT(name_loc->namelen == args->namelen);
2383 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
Nathan Scott053b5752006-03-17 17:29:09 +11002384 valuelen = be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (args->flags & ATTR_KERNOVAL) {
2386 args->valuelen = valuelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
2389 if (args->valuelen < valuelen) {
2390 args->valuelen = valuelen;
Dave Chinner24513372014-06-25 14:58:08 +10002391 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393 args->valuelen = valuelen;
2394 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2395 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002396 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 ASSERT(name_rmt->namelen == args->namelen);
2398 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
Dave Chinner8275cdd2014-05-06 07:37:31 +10002399 args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002400 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Dave Chinnerad1858d2013-05-21 18:02:08 +10002401 args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount,
Dave Chinner8275cdd2014-05-06 07:37:31 +10002402 args->rmtvaluelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if (args->flags & ATTR_KERNOVAL) {
Dave Chinner8275cdd2014-05-06 07:37:31 +10002404 args->valuelen = args->rmtvaluelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
Dave Chinner8275cdd2014-05-06 07:37:31 +10002407 if (args->valuelen < args->rmtvaluelen) {
2408 args->valuelen = args->rmtvaluelen;
Dave Chinner24513372014-06-25 14:58:08 +10002409 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
Dave Chinner8275cdd2014-05-06 07:37:31 +10002411 args->valuelen = args->rmtvaluelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
Dave Chinner517c2222013-04-24 18:58:55 +10002413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414}
2415
2416/*========================================================================
2417 * Utility routines.
2418 *========================================================================*/
2419
2420/*
2421 * Move the indicated entries from one leaf to another.
2422 * NOTE: this routine modifies both source and destination leaves.
2423 */
2424/*ARGSUSED*/
2425STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10002426xfs_attr3_leaf_moveents(
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002427 struct xfs_da_args *args,
Dave Chinner517c2222013-04-24 18:58:55 +10002428 struct xfs_attr_leafblock *leaf_s,
2429 struct xfs_attr3_icleaf_hdr *ichdr_s,
2430 int start_s,
2431 struct xfs_attr_leafblock *leaf_d,
2432 struct xfs_attr3_icleaf_hdr *ichdr_d,
2433 int start_d,
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002434 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Dave Chinner517c2222013-04-24 18:58:55 +10002436 struct xfs_attr_leaf_entry *entry_s;
2437 struct xfs_attr_leaf_entry *entry_d;
2438 int desti;
2439 int tmp;
2440 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 /*
2443 * Check for nothing to do.
2444 */
2445 if (count == 0)
2446 return;
2447
2448 /*
2449 * Set up environment.
2450 */
Dave Chinner517c2222013-04-24 18:58:55 +10002451 ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC ||
2452 ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC);
2453 ASSERT(ichdr_s->magic == ichdr_d->magic);
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002454 ASSERT(ichdr_s->count > 0 && ichdr_s->count < args->geo->blksize / 8);
Dave Chinner517c2222013-04-24 18:58:55 +10002455 ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s))
2456 + xfs_attr3_leaf_hdr_size(leaf_s));
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002457 ASSERT(ichdr_d->count < args->geo->blksize / 8);
Dave Chinner517c2222013-04-24 18:58:55 +10002458 ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d))
2459 + xfs_attr3_leaf_hdr_size(leaf_d));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Dave Chinner517c2222013-04-24 18:58:55 +10002461 ASSERT(start_s < ichdr_s->count);
2462 ASSERT(start_d <= ichdr_d->count);
2463 ASSERT(count <= ichdr_s->count);
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466 /*
2467 * Move the entries in the destination leaf up to make a hole?
2468 */
Dave Chinner517c2222013-04-24 18:58:55 +10002469 if (start_d < ichdr_d->count) {
2470 tmp = ichdr_d->count - start_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 tmp *= sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002472 entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2473 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count];
2474 memmove(entry_d, entry_s, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
2477 /*
2478 * Copy all entry's in the same (sorted) order,
2479 * but allocate attribute info packed and in sequence.
2480 */
Dave Chinner517c2222013-04-24 18:58:55 +10002481 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2482 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 desti = start_d;
2484 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
Dave Chinner517c2222013-04-24 18:58:55 +10002485 ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2487#ifdef GROT
2488 /*
2489 * Code to drop INCOMPLETE entries. Difficult to use as we
2490 * may also need to change the insertion index. Code turned
2491 * off for 6.2, should be revisited later.
2492 */
2493 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
Dave Chinner517c2222013-04-24 18:58:55 +10002494 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2495 ichdr_s->usedbytes -= tmp;
2496 ichdr_s->count -= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 entry_d--; /* to compensate for ++ in loop hdr */
2498 desti--;
2499 if ((start_s + i) < offset)
2500 result++; /* insertion index adjustment */
2501 } else {
2502#endif /* GROT */
Dave Chinner517c2222013-04-24 18:58:55 +10002503 ichdr_d->firstused -= tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 /* both on-disk, don't endian flip twice */
2505 entry_d->hashval = entry_s->hashval;
Dave Chinner517c2222013-04-24 18:58:55 +10002506 entry_d->nameidx = cpu_to_be16(ichdr_d->firstused);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 entry_d->flags = entry_s->flags;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002508 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002509 <= args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10002510 memmove(xfs_attr3_leaf_name(leaf_d, desti),
2511 xfs_attr3_leaf_name(leaf_s, start_s + i), tmp);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002512 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002513 <= args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10002514 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2515 ichdr_s->usedbytes -= tmp;
2516 ichdr_d->usedbytes += tmp;
2517 ichdr_s->count -= 1;
2518 ichdr_d->count += 1;
2519 tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t)
2520 + xfs_attr3_leaf_hdr_size(leaf_d);
2521 ASSERT(ichdr_d->firstused >= tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522#ifdef GROT
2523 }
2524#endif /* GROT */
2525 }
2526
2527 /*
2528 * Zero out the entries we just copied.
2529 */
Dave Chinner517c2222013-04-24 18:58:55 +10002530 if (start_s == ichdr_s->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002532 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 ASSERT(((char *)entry_s + tmp) <=
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002534 ((char *)leaf_s + args->geo->blksize));
Dave Chinner517c2222013-04-24 18:58:55 +10002535 memset(entry_s, 0, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 } else {
2537 /*
2538 * Move the remaining entries down to fill the hole,
2539 * then zero the entries at the top.
2540 */
Dave Chinner517c2222013-04-24 18:58:55 +10002541 tmp = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t);
2542 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count];
2543 entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2544 memmove(entry_d, entry_s, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
2546 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002547 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 ASSERT(((char *)entry_s + tmp) <=
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002549 ((char *)leaf_s + args->geo->blksize));
Dave Chinner517c2222013-04-24 18:58:55 +10002550 memset(entry_s, 0, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
2552
2553 /*
2554 * Fill in the freemap information
2555 */
Dave Chinner517c2222013-04-24 18:58:55 +10002556 ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d);
2557 ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t);
2558 ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base;
2559 ichdr_d->freemap[1].base = 0;
2560 ichdr_d->freemap[2].base = 0;
2561 ichdr_d->freemap[1].size = 0;
2562 ichdr_d->freemap[2].size = 0;
2563 ichdr_s->holes = 1; /* leaf may not be compact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
2566/*
2567 * Pick up the last hashvalue from a leaf block.
2568 */
2569xfs_dahash_t
Dave Chinner1d9025e2012-06-22 18:50:14 +10002570xfs_attr_leaf_lasthash(
2571 struct xfs_buf *bp,
2572 int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
Dave Chinner517c2222013-04-24 18:58:55 +10002574 struct xfs_attr3_icleaf_hdr ichdr;
2575 struct xfs_attr_leaf_entry *entries;
Brian Foster2f661242015-04-13 11:26:02 +10002576 struct xfs_mount *mp = bp->b_target->bt_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Brian Foster2f661242015-04-13 11:26:02 +10002578 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, bp->b_addr);
Dave Chinner517c2222013-04-24 18:58:55 +10002579 entries = xfs_attr3_leaf_entryp(bp->b_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 if (count)
Dave Chinner517c2222013-04-24 18:58:55 +10002581 *count = ichdr.count;
2582 if (!ichdr.count)
2583 return 0;
2584 return be32_to_cpu(entries[ichdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
2586
2587/*
2588 * Calculate the number of bytes used to store the indicated attribute
2589 * (whether local or remote only calculate bytes in this block).
2590 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002591STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2593{
Dave Chinner517c2222013-04-24 18:58:55 +10002594 struct xfs_attr_leaf_entry *entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 xfs_attr_leaf_name_local_t *name_loc;
2596 xfs_attr_leaf_name_remote_t *name_rmt;
2597 int size;
2598
Dave Chinner517c2222013-04-24 18:58:55 +10002599 entries = xfs_attr3_leaf_entryp(leaf);
2600 if (entries[index].flags & XFS_ATTR_LOCAL) {
2601 name_loc = xfs_attr3_leaf_name_local(leaf, index);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002602 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
Nathan Scott053b5752006-03-17 17:29:09 +11002603 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002605 name_rmt = xfs_attr3_leaf_name_remote(leaf, index);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002606 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
Dave Chinner517c2222013-04-24 18:58:55 +10002608 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609}
2610
2611/*
2612 * Calculate the number of bytes that would be required to store the new
2613 * attribute (whether local or remote only calculate bytes in this block).
2614 * This routine decides as a side effect whether the attribute will be
2615 * a "local" or a "remote" attribute.
2616 */
2617int
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10002618xfs_attr_leaf_newentsize(
2619 struct xfs_da_args *args,
2620 int *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10002622 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10002624 size = xfs_attr_leaf_entsize_local(args->namelen, args->valuelen);
2625 if (size < xfs_attr_leaf_entsize_local_max(args->geo->blksize)) {
2626 if (local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 *local = 1;
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10002628 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 }
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10002630 if (local)
2631 *local = 0;
2632 return xfs_attr_leaf_entsize_remote(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633}
2634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636/*========================================================================
2637 * Manage the INCOMPLETE flag in a leaf entry
2638 *========================================================================*/
2639
2640/*
2641 * Clear the INCOMPLETE flag on an entry in a leaf block.
2642 */
2643int
Dave Chinner517c2222013-04-24 18:58:55 +10002644xfs_attr3_leaf_clearflag(
2645 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Dave Chinner517c2222013-04-24 18:58:55 +10002647 struct xfs_attr_leafblock *leaf;
2648 struct xfs_attr_leaf_entry *entry;
2649 struct xfs_attr_leaf_name_remote *name_rmt;
2650 struct xfs_buf *bp;
2651 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002653 struct xfs_attr3_icleaf_hdr ichdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 xfs_attr_leaf_name_local_t *name_loc;
2655 int namelen;
2656 char *name;
2657#endif /* DEBUG */
2658
Dave Chinner5a5881c2012-03-22 05:15:13 +00002659 trace_xfs_attr_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 /*
2661 * Set up the operation.
2662 */
Dave Chinner517c2222013-04-24 18:58:55 +10002663 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +11002664 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10002665 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Dave Chinner1d9025e2012-06-22 18:50:14 +10002667 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002668 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2670
2671#ifdef DEBUG
Brian Foster2f661242015-04-13 11:26:02 +10002672 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10002673 ASSERT(args->index < ichdr.count);
2674 ASSERT(args->index >= 0);
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002677 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 namelen = name_loc->namelen;
2679 name = (char *)name_loc->nameval;
2680 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002681 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 namelen = name_rmt->namelen;
2683 name = (char *)name_rmt->name;
2684 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002685 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 ASSERT(namelen == args->namelen);
2687 ASSERT(memcmp(name, args->name, namelen) == 0);
2688#endif /* DEBUG */
2689
2690 entry->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002691 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2693
2694 if (args->rmtblkno) {
2695 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002696 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002697 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
Dave Chinner8275cdd2014-05-06 07:37:31 +10002698 name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002699 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 /*
2704 * Commit the flag value change and start the next trans in series.
2705 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07002706 return xfs_trans_roll_inode(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707}
2708
2709/*
2710 * Set the INCOMPLETE flag on an entry in a leaf block.
2711 */
2712int
Dave Chinner517c2222013-04-24 18:58:55 +10002713xfs_attr3_leaf_setflag(
2714 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Dave Chinner517c2222013-04-24 18:58:55 +10002716 struct xfs_attr_leafblock *leaf;
2717 struct xfs_attr_leaf_entry *entry;
2718 struct xfs_attr_leaf_name_remote *name_rmt;
2719 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 int error;
Dave Chinner517c2222013-04-24 18:58:55 +10002721#ifdef DEBUG
2722 struct xfs_attr3_icleaf_hdr ichdr;
2723#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Dave Chinner5a5881c2012-03-22 05:15:13 +00002725 trace_xfs_attr_leaf_setflag(args);
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /*
2728 * Set up the operation.
2729 */
Dave Chinner517c2222013-04-24 18:58:55 +10002730 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +11002731 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10002732 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Dave Chinner1d9025e2012-06-22 18:50:14 +10002734 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002735#ifdef DEBUG
Brian Foster2f661242015-04-13 11:26:02 +10002736 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10002737 ASSERT(args->index < ichdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 ASSERT(args->index >= 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002739#endif
2740 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2743 entry->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002744 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2746 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002747 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 name_rmt->valueblk = 0;
2749 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002750 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 /*
2755 * Commit the flag value change and start the next trans in series.
2756 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07002757 return xfs_trans_roll_inode(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758}
2759
2760/*
2761 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2762 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2763 * entry given by args->blkno2/index2.
2764 *
2765 * Note that they could be in different blocks, or in the same block.
2766 */
2767int
Dave Chinner517c2222013-04-24 18:58:55 +10002768xfs_attr3_leaf_flipflags(
2769 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
Dave Chinner517c2222013-04-24 18:58:55 +10002771 struct xfs_attr_leafblock *leaf1;
2772 struct xfs_attr_leafblock *leaf2;
2773 struct xfs_attr_leaf_entry *entry1;
2774 struct xfs_attr_leaf_entry *entry2;
2775 struct xfs_attr_leaf_name_remote *name_rmt;
2776 struct xfs_buf *bp1;
2777 struct xfs_buf *bp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 int error;
2779#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002780 struct xfs_attr3_icleaf_hdr ichdr1;
2781 struct xfs_attr3_icleaf_hdr ichdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 xfs_attr_leaf_name_local_t *name_loc;
2783 int namelen1, namelen2;
2784 char *name1, *name2;
2785#endif /* DEBUG */
2786
Dave Chinner5a5881c2012-03-22 05:15:13 +00002787 trace_xfs_attr_leaf_flipflags(args);
2788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 /*
2790 * Read the block containing the "old" attr
2791 */
Dave Chinner517c2222013-04-24 18:58:55 +10002792 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1);
Dave Chinnerad14c332012-11-12 22:54:16 +11002793 if (error)
2794 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 /*
2797 * Read the block containing the "new" attr, if it is different
2798 */
2799 if (args->blkno2 != args->blkno) {
Dave Chinner517c2222013-04-24 18:58:55 +10002800 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno2,
Dave Chinnerad14c332012-11-12 22:54:16 +11002801 -1, &bp2);
2802 if (error)
2803 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 } else {
2805 bp2 = bp1;
2806 }
2807
Dave Chinner1d9025e2012-06-22 18:50:14 +10002808 leaf1 = bp1->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002809 entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Dave Chinner1d9025e2012-06-22 18:50:14 +10002811 leaf2 = bp2->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002812 entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814#ifdef DEBUG
Brian Foster2f661242015-04-13 11:26:02 +10002815 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr1, leaf1);
Dave Chinner517c2222013-04-24 18:58:55 +10002816 ASSERT(args->index < ichdr1.count);
2817 ASSERT(args->index >= 0);
2818
Brian Foster2f661242015-04-13 11:26:02 +10002819 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr2, leaf2);
Dave Chinner517c2222013-04-24 18:58:55 +10002820 ASSERT(args->index2 < ichdr2.count);
2821 ASSERT(args->index2 >= 0);
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 if (entry1->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002824 name_loc = xfs_attr3_leaf_name_local(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 namelen1 = name_loc->namelen;
2826 name1 = (char *)name_loc->nameval;
2827 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002828 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 namelen1 = name_rmt->namelen;
2830 name1 = (char *)name_rmt->name;
2831 }
2832 if (entry2->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002833 name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 namelen2 = name_loc->namelen;
2835 name2 = (char *)name_loc->nameval;
2836 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002837 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 namelen2 = name_rmt->namelen;
2839 name2 = (char *)name_rmt->name;
2840 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002841 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 ASSERT(namelen1 == namelen2);
2843 ASSERT(memcmp(name1, name2, namelen1) == 0);
2844#endif /* DEBUG */
2845
2846 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2847 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2848
2849 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002850 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2852 if (args->rmtblkno) {
2853 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002854 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002855 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
Dave Chinner8275cdd2014-05-06 07:37:31 +10002856 name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002857 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2859 }
2860
2861 entry2->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002862 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2864 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002865 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 name_rmt->valueblk = 0;
2867 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002868 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
2872 /*
2873 * Commit the flag value change and start the next trans in series.
2874 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07002875 error = xfs_trans_roll_inode(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Dave Chinner517c2222013-04-24 18:58:55 +10002877 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878}