blob: 76e90046731cad79b4127396d78a26050a9e4a8a [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
Dave Chinner517c2222013-04-24 18:58:55 +10004 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11005 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11008#include "xfs_fs.h"
Dave Chinner632b89e2013-10-29 22:11:58 +11009#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110010#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110013#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110016#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110017#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110019#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_inode_item.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_bmap.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110023#include "xfs_attr_sf.h"
24#include "xfs_attr_remote.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_attr.h"
26#include "xfs_attr_leaf.h"
27#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000028#include "xfs_trace.h"
Dave Chinner517c2222013-04-24 18:58:55 +100029#include "xfs_buf_item.h"
30#include "xfs_cksum.h"
Dave Chinner4bceb182013-10-29 22:11:51 +110031#include "xfs_dir2.h"
Brian Fostera45086e2015-10-12 15:59:25 +110032#include "xfs_log.h"
Dave Chinner517c2222013-04-24 18:58:55 +100033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/*
36 * xfs_attr_leaf.c
37 *
38 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
39 */
40
41/*========================================================================
42 * Function prototypes for the kernel.
43 *========================================================================*/
44
45/*
46 * Routines used for growing the Btree.
47 */
Dave Chinner517c2222013-04-24 18:58:55 +100048STATIC int xfs_attr3_leaf_create(struct xfs_da_args *args,
49 xfs_dablk_t which_block, struct xfs_buf **bpp);
50STATIC int xfs_attr3_leaf_add_work(struct xfs_buf *leaf_buffer,
51 struct xfs_attr3_icleaf_hdr *ichdr,
52 struct xfs_da_args *args, int freemap_index);
53STATIC void xfs_attr3_leaf_compact(struct xfs_da_args *args,
54 struct xfs_attr3_icleaf_hdr *ichdr,
55 struct xfs_buf *leaf_buffer);
56STATIC void xfs_attr3_leaf_rebalance(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 xfs_da_state_blk_t *blk1,
58 xfs_da_state_blk_t *blk2);
Dave Chinner517c2222013-04-24 18:58:55 +100059STATIC int xfs_attr3_leaf_figure_balance(xfs_da_state_t *state,
60 xfs_da_state_blk_t *leaf_blk_1,
61 struct xfs_attr3_icleaf_hdr *ichdr1,
62 xfs_da_state_blk_t *leaf_blk_2,
63 struct xfs_attr3_icleaf_hdr *ichdr2,
64 int *number_entries_in_blk1,
65 int *number_usedbytes_in_blk1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/*
68 * Utility routines.
69 */
Dave Chinnerc2c4c472014-06-06 15:21:45 +100070STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args,
71 struct xfs_attr_leafblock *src_leaf,
Dave Chinner517c2222013-04-24 18:58:55 +100072 struct xfs_attr3_icleaf_hdr *src_ichdr, int src_start,
73 struct xfs_attr_leafblock *dst_leaf,
74 struct xfs_attr3_icleaf_hdr *dst_ichdr, int dst_start,
Dave Chinnerc2c4c472014-06-06 15:21:45 +100075 int move_count);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100076STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
Tim Shimmin726801b2006-09-28 11:01:37 +100077
Brian Fostere87021a2015-04-13 11:27:10 +100078/*
79 * attr3 block 'firstused' conversion helpers.
80 *
81 * firstused refers to the offset of the first used byte of the nameval region
82 * of an attr leaf block. The region starts at the tail of the block and expands
83 * backwards towards the middle. As such, firstused is initialized to the block
84 * size for an empty leaf block and is reduced from there.
85 *
86 * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k.
87 * The in-core firstused field is 32-bit and thus supports the maximum fsb size.
88 * The on-disk field is only 16-bit, however, and overflows at 64k. Since this
89 * only occurs at exactly 64k, we use zero as a magic on-disk value to represent
90 * the attr block size. The following helpers manage the conversion between the
91 * in-core and on-disk formats.
92 */
93
94static void
95xfs_attr3_leaf_firstused_from_disk(
96 struct xfs_da_geometry *geo,
97 struct xfs_attr3_icleaf_hdr *to,
98 struct xfs_attr_leafblock *from)
99{
100 struct xfs_attr3_leaf_hdr *hdr3;
101
102 if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
103 hdr3 = (struct xfs_attr3_leaf_hdr *) from;
104 to->firstused = be16_to_cpu(hdr3->firstused);
105 } else {
106 to->firstused = be16_to_cpu(from->hdr.firstused);
107 }
108
109 /*
110 * Convert from the magic fsb size value to actual blocksize. This
111 * should only occur for empty blocks when the block size overflows
112 * 16-bits.
113 */
114 if (to->firstused == XFS_ATTR3_LEAF_NULLOFF) {
115 ASSERT(!to->count && !to->usedbytes);
116 ASSERT(geo->blksize > USHRT_MAX);
117 to->firstused = geo->blksize;
118 }
119}
120
121static void
122xfs_attr3_leaf_firstused_to_disk(
123 struct xfs_da_geometry *geo,
124 struct xfs_attr_leafblock *to,
125 struct xfs_attr3_icleaf_hdr *from)
126{
127 struct xfs_attr3_leaf_hdr *hdr3;
128 uint32_t firstused;
129
130 /* magic value should only be seen on disk */
131 ASSERT(from->firstused != XFS_ATTR3_LEAF_NULLOFF);
132
133 /*
134 * Scale down the 32-bit in-core firstused value to the 16-bit on-disk
135 * value. This only overflows at the max supported value of 64k. Use the
136 * magic on-disk value to represent block size in this case.
137 */
138 firstused = from->firstused;
139 if (firstused > USHRT_MAX) {
140 ASSERT(from->firstused == geo->blksize);
141 firstused = XFS_ATTR3_LEAF_NULLOFF;
142 }
143
144 if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
145 hdr3 = (struct xfs_attr3_leaf_hdr *) to;
146 hdr3->firstused = cpu_to_be16(firstused);
147 } else {
148 to->hdr.firstused = cpu_to_be16(firstused);
149 }
150}
151
Dave Chinner517c2222013-04-24 18:58:55 +1000152void
153xfs_attr3_leaf_hdr_from_disk(
Brian Foster2f661242015-04-13 11:26:02 +1000154 struct xfs_da_geometry *geo,
Dave Chinner517c2222013-04-24 18:58:55 +1000155 struct xfs_attr3_icleaf_hdr *to,
156 struct xfs_attr_leafblock *from)
157{
158 int i;
159
160 ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
161 from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
162
163 if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
164 struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)from;
165
166 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
167 to->back = be32_to_cpu(hdr3->info.hdr.back);
168 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
169 to->count = be16_to_cpu(hdr3->count);
170 to->usedbytes = be16_to_cpu(hdr3->usedbytes);
Brian Fostere87021a2015-04-13 11:27:10 +1000171 xfs_attr3_leaf_firstused_from_disk(geo, to, from);
Dave Chinner517c2222013-04-24 18:58:55 +1000172 to->holes = hdr3->holes;
173
174 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
175 to->freemap[i].base = be16_to_cpu(hdr3->freemap[i].base);
176 to->freemap[i].size = be16_to_cpu(hdr3->freemap[i].size);
177 }
178 return;
179 }
180 to->forw = be32_to_cpu(from->hdr.info.forw);
181 to->back = be32_to_cpu(from->hdr.info.back);
182 to->magic = be16_to_cpu(from->hdr.info.magic);
183 to->count = be16_to_cpu(from->hdr.count);
184 to->usedbytes = be16_to_cpu(from->hdr.usedbytes);
Brian Fostere87021a2015-04-13 11:27:10 +1000185 xfs_attr3_leaf_firstused_from_disk(geo, to, from);
Dave Chinner517c2222013-04-24 18:58:55 +1000186 to->holes = from->hdr.holes;
187
188 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
189 to->freemap[i].base = be16_to_cpu(from->hdr.freemap[i].base);
190 to->freemap[i].size = be16_to_cpu(from->hdr.freemap[i].size);
191 }
192}
193
194void
195xfs_attr3_leaf_hdr_to_disk(
Brian Foster2f661242015-04-13 11:26:02 +1000196 struct xfs_da_geometry *geo,
Dave Chinner517c2222013-04-24 18:58:55 +1000197 struct xfs_attr_leafblock *to,
198 struct xfs_attr3_icleaf_hdr *from)
199{
Brian Fostere87021a2015-04-13 11:27:10 +1000200 int i;
Dave Chinner517c2222013-04-24 18:58:55 +1000201
202 ASSERT(from->magic == XFS_ATTR_LEAF_MAGIC ||
203 from->magic == XFS_ATTR3_LEAF_MAGIC);
204
205 if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
206 struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)to;
207
208 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
209 hdr3->info.hdr.back = cpu_to_be32(from->back);
210 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
211 hdr3->count = cpu_to_be16(from->count);
212 hdr3->usedbytes = cpu_to_be16(from->usedbytes);
Brian Fostere87021a2015-04-13 11:27:10 +1000213 xfs_attr3_leaf_firstused_to_disk(geo, to, from);
Dave Chinner517c2222013-04-24 18:58:55 +1000214 hdr3->holes = from->holes;
215 hdr3->pad1 = 0;
216
217 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
218 hdr3->freemap[i].base = cpu_to_be16(from->freemap[i].base);
219 hdr3->freemap[i].size = cpu_to_be16(from->freemap[i].size);
220 }
221 return;
222 }
223 to->hdr.info.forw = cpu_to_be32(from->forw);
224 to->hdr.info.back = cpu_to_be32(from->back);
225 to->hdr.info.magic = cpu_to_be16(from->magic);
226 to->hdr.count = cpu_to_be16(from->count);
227 to->hdr.usedbytes = cpu_to_be16(from->usedbytes);
Brian Fostere87021a2015-04-13 11:27:10 +1000228 xfs_attr3_leaf_firstused_to_disk(geo, to, from);
Dave Chinner517c2222013-04-24 18:58:55 +1000229 to->hdr.holes = from->holes;
230 to->hdr.pad1 = 0;
231
232 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
233 to->hdr.freemap[i].base = cpu_to_be16(from->freemap[i].base);
234 to->hdr.freemap[i].size = cpu_to_be16(from->freemap[i].size);
235 }
236}
237
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800238static xfs_failaddr_t
Dave Chinner517c2222013-04-24 18:58:55 +1000239xfs_attr3_leaf_verify(
Darrick J. Wong79a69bf2018-01-16 18:54:12 -0800240 struct xfs_buf *bp)
Dave Chinnerad14c332012-11-12 22:54:16 +1100241{
Darrick J. Wong79a69bf2018-01-16 18:54:12 -0800242 struct xfs_attr3_icleaf_hdr ichdr;
243 struct xfs_mount *mp = bp->b_target->bt_mount;
244 struct xfs_attr_leafblock *leaf = bp->b_addr;
245 struct xfs_perag *pag = bp->b_pag;
246 struct xfs_attr_leaf_entry *entries;
Dave Chinnerad14c332012-11-12 22:54:16 +1100247
Brian Foster2f661242015-04-13 11:26:02 +1000248 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +1000249
250 if (xfs_sb_version_hascrc(&mp->m_sb)) {
251 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
252
253 if (ichdr.magic != XFS_ATTR3_LEAF_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800254 return __this_address;
Dave Chinner517c2222013-04-24 18:58:55 +1000255
Eric Sandeence748ea2015-07-29 11:53:31 +1000256 if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800257 return __this_address;
Dave Chinner517c2222013-04-24 18:58:55 +1000258 if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800259 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +1100260 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->info.lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800261 return __this_address;
Dave Chinner517c2222013-04-24 18:58:55 +1000262 } else {
263 if (ichdr.magic != XFS_ATTR_LEAF_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800264 return __this_address;
Dave Chinner517c2222013-04-24 18:58:55 +1000265 }
Eric Sandeen2e1d2332016-12-09 16:49:47 +1100266 /*
267 * In recovery there is a transient state where count == 0 is valid
268 * because we may have transitioned an empty shortform attr to a leaf
269 * if the attr didn't fit in shortform.
270 */
271 if (pag && pag->pagf_init && ichdr.count == 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800272 return __this_address;
Dave Chinner517c2222013-04-24 18:58:55 +1000273
Darrick J. Wong79a69bf2018-01-16 18:54:12 -0800274 /*
275 * firstused is the block offset of the first name info structure.
276 * Make sure it doesn't go off the block or crash into the header.
277 */
278 if (ichdr.firstused > mp->m_attr_geo->blksize)
279 return __this_address;
280 if (ichdr.firstused < xfs_attr3_leaf_hdr_size(leaf))
281 return __this_address;
282
283 /* Make sure the entries array doesn't crash into the name info. */
284 entries = xfs_attr3_leaf_entryp(bp->b_addr);
285 if ((char *)&entries[ichdr.count] >
286 (char *)bp->b_addr + ichdr.firstused)
287 return __this_address;
288
Dave Chinner517c2222013-04-24 18:58:55 +1000289 /* XXX: need to range check rest of attr header values */
290 /* XXX: hash order check? */
291
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800292 return NULL;
Dave Chinner517c2222013-04-24 18:58:55 +1000293}
294
295static void
296xfs_attr3_leaf_write_verify(
297 struct xfs_buf *bp)
298{
299 struct xfs_mount *mp = bp->b_target->bt_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800300 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinner517c2222013-04-24 18:58:55 +1000301 struct xfs_attr3_leaf_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800302 xfs_failaddr_t fa;
Dave Chinner517c2222013-04-24 18:58:55 +1000303
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800304 fa = xfs_attr3_leaf_verify(bp);
305 if (fa) {
306 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinner517c2222013-04-24 18:58:55 +1000307 return;
308 }
309
310 if (!xfs_sb_version_hascrc(&mp->m_sb))
311 return;
312
313 if (bip)
314 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
315
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100316 xfs_buf_update_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF);
Dave Chinner517c2222013-04-24 18:58:55 +1000317}
318
319/*
320 * leaf/node format detection on trees is sketchy, so a node read can be done on
321 * leaf level blocks when detection identifies the tree as a node format tree
322 * incorrectly. In this case, we need to swap the verifier to match the correct
323 * format of the block being read.
324 */
325static void
326xfs_attr3_leaf_read_verify(
327 struct xfs_buf *bp)
328{
329 struct xfs_mount *mp = bp->b_target->bt_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800330 xfs_failaddr_t fa;
Dave Chinner517c2222013-04-24 18:58:55 +1000331
Eric Sandeence5028c2014-02-27 15:23:10 +1100332 if (xfs_sb_version_hascrc(&mp->m_sb) &&
333 !xfs_buf_verify_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800334 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
335 else {
336 fa = xfs_attr3_leaf_verify(bp);
337 if (fa)
338 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
339 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100340}
Dave Chinnerad14c332012-11-12 22:54:16 +1100341
Dave Chinner517c2222013-04-24 18:58:55 +1000342const struct xfs_buf_ops xfs_attr3_leaf_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100343 .name = "xfs_attr3_leaf",
Dave Chinner517c2222013-04-24 18:58:55 +1000344 .verify_read = xfs_attr3_leaf_read_verify,
345 .verify_write = xfs_attr3_leaf_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800346 .verify_struct = xfs_attr3_leaf_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100347};
Dave Chinner612cfbf2012-11-14 17:52:32 +1100348
Dave Chinnerad14c332012-11-12 22:54:16 +1100349int
Dave Chinner517c2222013-04-24 18:58:55 +1000350xfs_attr3_leaf_read(
Dave Chinnerad14c332012-11-12 22:54:16 +1100351 struct xfs_trans *tp,
352 struct xfs_inode *dp,
353 xfs_dablk_t bno,
354 xfs_daddr_t mappedbno,
355 struct xfs_buf **bpp)
356{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100357 int err;
358
359 err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
Dave Chinner517c2222013-04-24 18:58:55 +1000360 XFS_ATTR_FORK, &xfs_attr3_leaf_buf_ops);
Darrick J. Wongcd87d862017-07-07 18:55:17 -0700361 if (!err && tp && *bpp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100362 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_ATTR_LEAF_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100363 return err;
Dave Chinnerad14c332012-11-12 22:54:16 +1100364}
365
Tim Shimmin726801b2006-09-28 11:01:37 +1000366/*========================================================================
367 * Namespace helper routines
368 *========================================================================*/
369
Tim Shimmin726801b2006-09-28 11:01:37 +1000370/*
371 * If namespace bits don't match return 0.
372 * If all match then return 1.
373 */
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000374STATIC int
Tim Shimmin726801b2006-09-28 11:01:37 +1000375xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
376{
377 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381/*========================================================================
Nathan Scottd8cc8902005-11-02 10:34:53 +1100382 * External routines when attribute fork size < XFS_LITINO(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 *========================================================================*/
384
385/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100386 * Query whether the requested number of additional bytes of extended
387 * attribute space will be able to fit inline.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000388 *
Nathan Scottd8cc8902005-11-02 10:34:53 +1100389 * Returns zero if not, else the di_forkoff fork offset to be used in the
390 * literal area for attribute data once the new bytes have been added.
391 *
392 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
393 * special case for dev/uuid inodes, they have fixed size data forks.
394 */
395int
396xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
397{
398 int offset;
399 int minforkoff; /* lower limit on valid forkoff locations */
400 int maxforkoff; /* upper limit on valid forkoff locations */
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000401 int dsize;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100402 xfs_mount_t *mp = dp->i_mount;
403
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100404 /* rounded down */
405 offset = (XFS_LITINO(mp, dp->i_d.di_version) - bytes) >> 3;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100406
Christoph Hellwig42b67dc2017-10-19 11:07:09 -0700407 if (dp->i_d.di_format == XFS_DINODE_FMT_DEV) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100408 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
409 return (offset >= minforkoff) ? minforkoff : 0;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100410 }
411
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000412 /*
413 * If the requested numbers of bytes is smaller or equal to the
414 * current attribute fork size we can always proceed.
415 *
416 * Note that if_bytes in the data fork might actually be larger than
417 * the current data fork size is due to delalloc extents. In that
418 * case either the extent count will go down when they are converted
419 * to real extents, or the delalloc conversion will take care of the
420 * literal area rebalancing.
421 */
422 if (bytes <= XFS_IFORK_ASIZE(dp))
423 return dp->i_d.di_forkoff;
424
425 /*
426 * For attr2 we can try to move the forkoff if there is space in the
427 * literal area, but for the old format we are done if there is no
428 * space in the fixed attribute fork.
429 */
430 if (!(mp->m_flags & XFS_MOUNT_ATTR2))
Nathan Scottda087ba2005-11-02 15:00:20 +1100431 return 0;
Nathan Scottda087ba2005-11-02 15:00:20 +1100432
Barry Naujoke5889e92007-02-10 18:35:58 +1100433 dsize = dp->i_df.if_bytes;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000434
Barry Naujoke5889e92007-02-10 18:35:58 +1100435 switch (dp->i_d.di_format) {
436 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000437 /*
Barry Naujoke5889e92007-02-10 18:35:58 +1100438 * If there is no attr fork and the data fork is extents,
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000439 * determine if creating the default attr fork will result
440 * in the extents form migrating to btree. If so, the
441 * minimum offset only needs to be the space required for
Barry Naujoke5889e92007-02-10 18:35:58 +1100442 * the btree root.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000443 */
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200444 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
445 xfs_default_attroffset(dp))
Barry Naujoke5889e92007-02-10 18:35:58 +1100446 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
447 break;
Barry Naujoke5889e92007-02-10 18:35:58 +1100448 case XFS_DINODE_FMT_BTREE:
449 /*
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000450 * If we have a data btree then keep forkoff if we have one,
451 * otherwise we are adding a new attr, so then we set
452 * minforkoff to where the btree root can finish so we have
Barry Naujoke5889e92007-02-10 18:35:58 +1100453 * plenty of room for attrs
454 */
455 if (dp->i_d.di_forkoff) {
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000456 if (offset < dp->i_d.di_forkoff)
Barry Naujoke5889e92007-02-10 18:35:58 +1100457 return 0;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000458 return dp->i_d.di_forkoff;
459 }
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500460 dsize = XFS_BMAP_BROOT_SPACE(mp, dp->i_df.if_broot);
Barry Naujoke5889e92007-02-10 18:35:58 +1100461 break;
462 }
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000463
464 /*
465 * A data fork btree root must have space for at least
Barry Naujoke5889e92007-02-10 18:35:58 +1100466 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
467 */
Dave Chinner9bb54cb2018-06-07 07:54:02 -0700468 minforkoff = max(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100469 minforkoff = roundup(minforkoff, 8) >> 3;
470
471 /* attr fork btree root can have at least this many key/ptr pairs */
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100472 maxforkoff = XFS_LITINO(mp, dp->i_d.di_version) -
473 XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100474 maxforkoff = maxforkoff >> 3; /* rounded down */
475
Nathan Scottd8cc8902005-11-02 10:34:53 +1100476 if (offset >= maxforkoff)
477 return maxforkoff;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000478 if (offset >= minforkoff)
479 return offset;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100480 return 0;
481}
482
483/*
484 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
485 */
486STATIC void
487xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
488{
Nathan Scott13059ff2006-01-11 15:32:01 +1100489 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
Eric Sandeen62118702008-03-06 13:44:28 +1100490 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000491 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +1100492 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
493 xfs_sb_version_addattr2(&mp->m_sb);
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000494 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +1100495 xfs_log_sb(tp);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100496 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000497 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100498 }
499}
500
501/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 * Create the initial contents of a shortform attribute list.
503 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100504void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505xfs_attr_shortform_create(xfs_da_args_t *args)
506{
507 xfs_attr_sf_hdr_t *hdr;
508 xfs_inode_t *dp;
509 xfs_ifork_t *ifp;
510
Dave Chinner5a5881c2012-03-22 05:15:13 +0000511 trace_xfs_attr_sf_create(args);
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 dp = args->dp;
514 ASSERT(dp != NULL);
515 ifp = dp->i_afp;
516 ASSERT(ifp != NULL);
517 ASSERT(ifp->if_bytes == 0);
518 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
519 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
520 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
521 ifp->if_flags |= XFS_IFINLINE;
522 } else {
523 ASSERT(ifp->if_flags & XFS_IFINLINE);
524 }
525 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
526 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
527 hdr->count = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100528 hdr->totsize = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532/*
533 * Add a name/value pair to the shortform attribute list.
534 * Overflow from the inode has already been checked for.
535 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100536void
537xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 xfs_attr_shortform_t *sf;
540 xfs_attr_sf_entry_t *sfe;
541 int i, offset, size;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100542 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 xfs_inode_t *dp;
544 xfs_ifork_t *ifp;
545
Dave Chinner5a5881c2012-03-22 05:15:13 +0000546 trace_xfs_attr_sf_add(args);
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100549 mp = dp->i_mount;
550 dp->i_d.di_forkoff = forkoff;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 ifp = dp->i_afp;
553 ASSERT(ifp->if_flags & XFS_IFINLINE);
554 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
555 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100556 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100557#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (sfe->namelen != args->namelen)
559 continue;
560 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
561 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000562 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 continue;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100564 ASSERT(0);
565#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
568 offset = (char *)sfe - (char *)sf;
569 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
570 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
571 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
572 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
573
574 sfe->namelen = args->namelen;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100575 sfe->valuelen = args->valuelen;
Tim Shimmin726801b2006-09-28 11:01:37 +1000576 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 memcpy(sfe->nameval, args->name, args->namelen);
578 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100579 sf->hdr.count++;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800580 be16_add_cpu(&sf->hdr.totsize, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
582
Nathan Scottd8cc8902005-11-02 10:34:53 +1100583 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
586/*
Christoph Hellwige1486de2009-02-04 09:36:00 +0100587 * After the last attribute is removed revert to original inode format,
588 * making all literal area available to the data fork once more.
589 */
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000590void
591xfs_attr_fork_remove(
Christoph Hellwige1486de2009-02-04 09:36:00 +0100592 struct xfs_inode *ip,
593 struct xfs_trans *tp)
594{
595 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
596 ip->i_d.di_forkoff = 0;
597 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
598
599 ASSERT(ip->i_d.di_anextents == 0);
600 ASSERT(ip->i_afp == NULL);
601
Christoph Hellwige1486de2009-02-04 09:36:00 +0100602 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
603}
604
605/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100606 * Remove an attribute from the shortform attribute list structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
608int
609xfs_attr_shortform_remove(xfs_da_args_t *args)
610{
611 xfs_attr_shortform_t *sf;
612 xfs_attr_sf_entry_t *sfe;
613 int base, size=0, end, totsize, i;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100614 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 xfs_inode_t *dp;
616
Dave Chinner5a5881c2012-03-22 05:15:13 +0000617 trace_xfs_attr_sf_remove(args);
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100620 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 base = sizeof(xfs_attr_sf_hdr_t);
622 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
623 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100624 end = sf->hdr.count;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100625 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 base += size, i++) {
627 size = XFS_ATTR_SF_ENTSIZE(sfe);
628 if (sfe->namelen != args->namelen)
629 continue;
630 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
631 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000632 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 continue;
634 break;
635 }
Nathan Scottd8cc8902005-11-02 10:34:53 +1100636 if (i == end)
Dave Chinner24513372014-06-25 14:58:08 +1000637 return -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Nathan Scottd8cc8902005-11-02 10:34:53 +1100639 /*
640 * Fix up the attribute fork data, covering the hole
641 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 end = base + size;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100643 totsize = be16_to_cpu(sf->hdr.totsize);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100644 if (end != totsize)
645 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100646 sf->hdr.count--;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800647 be16_add_cpu(&sf->hdr.totsize, -size);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100648
649 /*
650 * Fix up the start offset of the attribute fork
651 */
652 totsize -= size;
Barry Naujok6a178102008-05-21 16:42:05 +1000653 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
Christoph Hellwige1486de2009-02-04 09:36:00 +0100654 (mp->m_flags & XFS_MOUNT_ATTR2) &&
655 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
656 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000657 xfs_attr_fork_remove(dp, args->trans);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100658 } else {
659 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
660 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
661 ASSERT(dp->i_d.di_forkoff);
Barry Naujok6a178102008-05-21 16:42:05 +1000662 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
663 (args->op_flags & XFS_DA_OP_ADDNAME) ||
664 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
665 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100666 xfs_trans_log_inode(args->trans, dp,
667 XFS_ILOG_CORE | XFS_ILOG_ADATA);
668 }
669
670 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Eric Sandeend99831f2014-06-22 15:03:54 +1000672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675/*
676 * Look up a name in a shortform attribute list structure.
677 */
678/*ARGSUSED*/
679int
680xfs_attr_shortform_lookup(xfs_da_args_t *args)
681{
682 xfs_attr_shortform_t *sf;
683 xfs_attr_sf_entry_t *sfe;
684 int i;
685 xfs_ifork_t *ifp;
686
Dave Chinner5a5881c2012-03-22 05:15:13 +0000687 trace_xfs_attr_sf_lookup(args);
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 ifp = args->dp->i_afp;
690 ASSERT(ifp->if_flags & XFS_IFINLINE);
691 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
692 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100693 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
695 if (sfe->namelen != args->namelen)
696 continue;
697 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
698 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000699 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 continue;
Dave Chinner24513372014-06-25 14:58:08 +1000701 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Dave Chinner24513372014-06-25 14:58:08 +1000703 return -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706/*
707 * Look up a name in a shortform attribute list structure.
708 */
709/*ARGSUSED*/
710int
711xfs_attr_shortform_getvalue(xfs_da_args_t *args)
712{
713 xfs_attr_shortform_t *sf;
714 xfs_attr_sf_entry_t *sfe;
715 int i;
716
Eric Sandeen914ed442012-03-30 11:24:11 -0500717 ASSERT(args->dp->i_afp->if_flags == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
719 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100720 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
722 if (sfe->namelen != args->namelen)
723 continue;
724 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
725 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000726 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 continue;
728 if (args->flags & ATTR_KERNOVAL) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100729 args->valuelen = sfe->valuelen;
Dave Chinner24513372014-06-25 14:58:08 +1000730 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100732 if (args->valuelen < sfe->valuelen) {
733 args->valuelen = sfe->valuelen;
Dave Chinner24513372014-06-25 14:58:08 +1000734 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100736 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 memcpy(args->value, &sfe->nameval[args->namelen],
738 args->valuelen);
Dave Chinner24513372014-06-25 14:58:08 +1000739 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
Dave Chinner24513372014-06-25 14:58:08 +1000741 return -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744/*
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800745 * Convert from using the shortform to the leaf. On success, return the
746 * buffer so that we can keep it locked until we're totally done with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 */
748int
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800749xfs_attr_shortform_to_leaf(
750 struct xfs_da_args *args,
751 struct xfs_buf **leaf_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 xfs_inode_t *dp;
754 xfs_attr_shortform_t *sf;
755 xfs_attr_sf_entry_t *sfe;
756 xfs_da_args_t nargs;
757 char *tmpbuffer;
758 int error, i, size;
759 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000760 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 xfs_ifork_t *ifp;
762
Dave Chinner5a5881c2012-03-22 05:15:13 +0000763 trace_xfs_attr_sf_to_leaf(args);
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 dp = args->dp;
766 ifp = dp->i_afp;
767 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100768 size = be16_to_cpu(sf->hdr.totsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 tmpbuffer = kmem_alloc(size, KM_SLEEP);
770 ASSERT(tmpbuffer != NULL);
771 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
772 sf = (xfs_attr_shortform_t *)tmpbuffer;
773
774 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000775 xfs_bmap_local_to_extents_empty(dp, XFS_ATTR_FORK);
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 bp = NULL;
778 error = xfs_da_grow_inode(args, &blkno);
779 if (error) {
780 /*
781 * If we hit an IO error middle of the transaction inside
782 * grow_inode(), we may have inconsistent data. Bail out.
783 */
Dave Chinner24513372014-06-25 14:58:08 +1000784 if (error == -EIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 goto out;
786 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
787 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
788 goto out;
789 }
790
791 ASSERT(blkno == 0);
Dave Chinner517c2222013-04-24 18:58:55 +1000792 error = xfs_attr3_leaf_create(args, blkno, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (error) {
Eric Sandeenbb3d48d2018-06-08 09:53:49 -0700794 /* xfs_attr3_leaf_create may not have instantiated a block */
795 if (bp && (xfs_da_shrink_inode(args, 0, bp) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 goto out;
797 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
798 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
799 goto out;
800 }
801
802 memset((char *)&nargs, 0, sizeof(nargs));
803 nargs.dp = dp;
Dave Chinner0650b552014-06-06 15:01:58 +1000804 nargs.geo = args->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 nargs.firstblock = args->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000806 nargs.dfops = args->dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 nargs.total = args->total;
808 nargs.whichfork = XFS_ATTR_FORK;
809 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000810 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100813 for (i = 0; i < sf->hdr.count; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100814 nargs.name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 nargs.namelen = sfe->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100816 nargs.value = &sfe->nameval[nargs.namelen];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100817 nargs.valuelen = sfe->valuelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100818 nargs.hashval = xfs_da_hashname(sfe->nameval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 sfe->namelen);
Tim Shimmin726801b2006-09-28 11:01:37 +1000820 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
Dave Chinner517c2222013-04-24 18:58:55 +1000821 error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */
Dave Chinner24513372014-06-25 14:58:08 +1000822 ASSERT(error == -ENOATTR);
Dave Chinner517c2222013-04-24 18:58:55 +1000823 error = xfs_attr3_leaf_add(bp, &nargs);
Dave Chinner24513372014-06-25 14:58:08 +1000824 ASSERT(error != -ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (error)
826 goto out;
827 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
828 }
829 error = 0;
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800830 *leaf_bp = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000832 kmem_free(tmpbuffer);
Eric Sandeend99831f2014-06-22 15:03:54 +1000833 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836/*
837 * Check a leaf attribute block to see if all the entries would fit into
838 * a shortform attribute list.
839 */
840int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000841xfs_attr_shortform_allfit(
Dave Chinnerb38958d2013-05-20 09:51:14 +1000842 struct xfs_buf *bp,
843 struct xfs_inode *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Dave Chinnerb38958d2013-05-20 09:51:14 +1000845 struct xfs_attr_leafblock *leaf;
846 struct xfs_attr_leaf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 xfs_attr_leaf_name_local_t *name_loc;
Dave Chinnerb38958d2013-05-20 09:51:14 +1000848 struct xfs_attr3_icleaf_hdr leafhdr;
849 int bytes;
850 int i;
Brian Foster2f661242015-04-13 11:26:02 +1000851 struct xfs_mount *mp = bp->b_target->bt_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Dave Chinner1d9025e2012-06-22 18:50:14 +1000853 leaf = bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +1000854 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
Dave Chinnerb38958d2013-05-20 09:51:14 +1000855 entry = xfs_attr3_leaf_entryp(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 bytes = sizeof(struct xfs_attr_sf_hdr);
Dave Chinnerb38958d2013-05-20 09:51:14 +1000858 for (i = 0; i < leafhdr.count; entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (entry->flags & XFS_ATTR_INCOMPLETE)
860 continue; /* don't copy partial entries */
861 if (!(entry->flags & XFS_ATTR_LOCAL))
Eric Sandeend99831f2014-06-22 15:03:54 +1000862 return 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000863 name_loc = xfs_attr3_leaf_name_local(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Eric Sandeend99831f2014-06-22 15:03:54 +1000865 return 0;
Nathan Scott053b5752006-03-17 17:29:09 +1100866 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
Eric Sandeend99831f2014-06-22 15:03:54 +1000867 return 0;
Dave Chinnerb38958d2013-05-20 09:51:14 +1000868 bytes += sizeof(struct xfs_attr_sf_entry) - 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 + name_loc->namelen
Nathan Scott053b5752006-03-17 17:29:09 +1100870 + be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Nathan Scott13059ff2006-01-11 15:32:01 +1100872 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
Barry Naujoke5889e92007-02-10 18:35:58 +1100873 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scotte0144ca2005-11-25 16:42:22 +1100874 (bytes == sizeof(struct xfs_attr_sf_hdr)))
Dave Chinnerb38958d2013-05-20 09:51:14 +1000875 return -1;
876 return xfs_attr_shortform_bytesfit(dp, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Darrick J. Wong1e1bbd82018-01-08 10:51:05 -0800879/* Verify the consistency of an inline attribute fork. */
880xfs_failaddr_t
881xfs_attr_shortform_verify(
882 struct xfs_inode *ip)
883{
884 struct xfs_attr_shortform *sfp;
885 struct xfs_attr_sf_entry *sfep;
886 struct xfs_attr_sf_entry *next_sfep;
887 char *endp;
888 struct xfs_ifork *ifp;
889 int i;
890 int size;
891
892 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL);
893 ifp = XFS_IFORK_PTR(ip, XFS_ATTR_FORK);
894 sfp = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
895 size = ifp->if_bytes;
896
897 /*
898 * Give up if the attribute is way too short.
899 */
900 if (size < sizeof(struct xfs_attr_sf_hdr))
901 return __this_address;
902
903 endp = (char *)sfp + size;
904
905 /* Check all reported entries */
906 sfep = &sfp->list[0];
907 for (i = 0; i < sfp->hdr.count; i++) {
908 /*
909 * struct xfs_attr_sf_entry has a variable length.
910 * Check the fixed-offset parts of the structure are
911 * within the data buffer.
912 */
913 if (((char *)sfep + sizeof(*sfep)) >= endp)
914 return __this_address;
915
916 /* Don't allow names with known bad length. */
917 if (sfep->namelen == 0)
918 return __this_address;
919
920 /*
921 * Check that the variable-length part of the structure is
922 * within the data buffer. The next entry starts after the
923 * name component, so nextentry is an acceptable test.
924 */
925 next_sfep = XFS_ATTR_SF_NEXTENTRY(sfep);
926 if ((char *)next_sfep > endp)
927 return __this_address;
928
929 /*
930 * Check for unknown flags. Short form doesn't support
931 * the incomplete or local bits, so we can use the namespace
932 * mask here.
933 */
934 if (sfep->flags & ~XFS_ATTR_NSP_ONDISK_MASK)
935 return __this_address;
936
937 /*
938 * Check for invalid namespace combinations. We only allow
939 * one namespace flag per xattr, so we can just count the
940 * bits (i.e. hweight) here.
941 */
942 if (hweight8(sfep->flags & XFS_ATTR_NSP_ONDISK_MASK) > 1)
943 return __this_address;
944
945 sfep = next_sfep;
946 }
947 if ((void *)sfep != (void *)endp)
948 return __this_address;
949
950 return NULL;
951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953/*
954 * Convert a leaf attribute list to shortform attribute list
955 */
956int
Dave Chinner517c2222013-04-24 18:58:55 +1000957xfs_attr3_leaf_to_shortform(
958 struct xfs_buf *bp,
959 struct xfs_da_args *args,
960 int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Dave Chinner517c2222013-04-24 18:58:55 +1000962 struct xfs_attr_leafblock *leaf;
963 struct xfs_attr3_icleaf_hdr ichdr;
964 struct xfs_attr_leaf_entry *entry;
965 struct xfs_attr_leaf_name_local *name_loc;
966 struct xfs_da_args nargs;
967 struct xfs_inode *dp = args->dp;
968 char *tmpbuffer;
969 int error;
970 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Dave Chinner5a5881c2012-03-22 05:15:13 +0000972 trace_xfs_attr_leaf_to_sf(args);
973
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000974 tmpbuffer = kmem_alloc(args->geo->blksize, KM_SLEEP);
Dave Chinner517c2222013-04-24 18:58:55 +1000975 if (!tmpbuffer)
Dave Chinner24513372014-06-25 14:58:08 +1000976 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000978 memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +1000979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
Brian Foster2f661242015-04-13 11:26:02 +1000981 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +1000982 entry = xfs_attr3_leaf_entryp(leaf);
983
984 /* XXX (dgc): buffer is about to be marked stale - why zero it? */
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000985 memset(bp->b_addr, 0, args->geo->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /*
988 * Clean out the prior contents of the attribute list.
989 */
990 error = xfs_da_shrink_inode(args, 0, bp);
991 if (error)
992 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100993
994 if (forkoff == -1) {
Nathan Scott13059ff2006-01-11 15:32:01 +1100995 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
Barry Naujoke5889e92007-02-10 18:35:58 +1100996 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
Dave Chinner6dfe5a02015-05-29 07:40:08 +1000997 xfs_attr_fork_remove(dp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100999 }
1000
1001 xfs_attr_shortform_create(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 /*
1004 * Copy the attributes
1005 */
1006 memset((char *)&nargs, 0, sizeof(nargs));
Dave Chinner0650b552014-06-06 15:01:58 +10001007 nargs.geo = args->geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 nargs.dp = dp;
1009 nargs.firstblock = args->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001010 nargs.dfops = args->dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 nargs.total = args->total;
1012 nargs.whichfork = XFS_ATTR_FORK;
1013 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +10001014 nargs.op_flags = XFS_DA_OP_OKNOENT;
Dave Chinner517c2222013-04-24 18:58:55 +10001015
1016 for (i = 0; i < ichdr.count; entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (entry->flags & XFS_ATTR_INCOMPLETE)
1018 continue; /* don't copy partial entries */
1019 if (!entry->nameidx)
1020 continue;
1021 ASSERT(entry->flags & XFS_ATTR_LOCAL);
Dave Chinner517c2222013-04-24 18:58:55 +10001022 name_loc = xfs_attr3_leaf_name_local(leaf, i);
Dave Chinnera9273ca2010-01-20 10:47:48 +11001023 nargs.name = name_loc->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 nargs.namelen = name_loc->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +11001025 nargs.value = &name_loc->nameval[nargs.namelen];
Nathan Scott053b5752006-03-17 17:29:09 +11001026 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001027 nargs.hashval = be32_to_cpu(entry->hashval);
Tim Shimmin726801b2006-09-28 11:01:37 +10001028 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
Nathan Scottd8cc8902005-11-02 10:34:53 +11001029 xfs_attr_shortform_add(&nargs, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031 error = 0;
1032
1033out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001034 kmem_free(tmpbuffer);
Dave Chinner517c2222013-04-24 18:58:55 +10001035 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
1038/*
1039 * Convert from using a single leaf to a root node and a leaf.
1040 */
1041int
Dave Chinner517c2222013-04-24 18:58:55 +10001042xfs_attr3_leaf_to_node(
1043 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Dave Chinner517c2222013-04-24 18:58:55 +10001045 struct xfs_attr_leafblock *leaf;
1046 struct xfs_attr3_icleaf_hdr icleafhdr;
1047 struct xfs_attr_leaf_entry *entries;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001048 struct xfs_da_node_entry *btree;
Dave Chinner517c2222013-04-24 18:58:55 +10001049 struct xfs_da3_icnode_hdr icnodehdr;
1050 struct xfs_da_intnode *node;
1051 struct xfs_inode *dp = args->dp;
1052 struct xfs_mount *mp = dp->i_mount;
1053 struct xfs_buf *bp1 = NULL;
1054 struct xfs_buf *bp2 = NULL;
1055 xfs_dablk_t blkno;
1056 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Dave Chinner5a5881c2012-03-22 05:15:13 +00001058 trace_xfs_attr_leaf_to_node(args);
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 error = xfs_da_grow_inode(args, &blkno);
1061 if (error)
1062 goto out;
Dave Chinner517c2222013-04-24 18:58:55 +10001063 error = xfs_attr3_leaf_read(args->trans, dp, 0, -1, &bp1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (error)
1065 goto out;
Dave Chinnerad14c332012-11-12 22:54:16 +11001066
Dave Chinner517c2222013-04-24 18:58:55 +10001067 error = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp2, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (error)
1069 goto out;
Dave Chinner517c2222013-04-24 18:58:55 +10001070
1071 /* copy leaf to new buffer, update identifiers */
Dave Chinner61fe1352013-04-03 16:11:30 +11001072 xfs_trans_buf_set_type(args->trans, bp2, XFS_BLFT_ATTR_LEAF_BUF);
Dave Chinner1813dd62012-11-14 17:54:40 +11001073 bp2->b_ops = bp1->b_ops;
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001074 memcpy(bp2->b_addr, bp1->b_addr, args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10001075 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1076 struct xfs_da3_blkinfo *hdr3 = bp2->b_addr;
1077 hdr3->blkno = cpu_to_be64(bp2->b_bn);
1078 }
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001079 xfs_trans_log_buf(args->trans, bp2, 0, args->geo->blksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 /*
1082 * Set up the new root node.
1083 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001084 error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (error)
1086 goto out;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001087 node = bp1->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001088 dp->d_ops->node_hdr_from_disk(&icnodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001089 btree = dp->d_ops->node_tree_p(node);
Dave Chinner517c2222013-04-24 18:58:55 +10001090
1091 leaf = bp2->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10001092 xfs_attr3_leaf_hdr_from_disk(args->geo, &icleafhdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10001093 entries = xfs_attr3_leaf_entryp(leaf);
1094
1095 /* both on-disk, don't endian-flip twice */
1096 btree[0].hashval = entries[icleafhdr.count - 1].hashval;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001097 btree[0].before = cpu_to_be32(blkno);
Dave Chinner517c2222013-04-24 18:58:55 +10001098 icnodehdr.count = 1;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001099 dp->d_ops->node_hdr_to_disk(node, &icnodehdr);
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001100 xfs_trans_log_buf(args->trans, bp1, 0, args->geo->blksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 error = 0;
1102out:
Dave Chinner517c2222013-04-24 18:58:55 +10001103 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/*========================================================================
1107 * Routines used for growing the Btree.
1108 *========================================================================*/
1109
1110/*
1111 * Create the initial contents of a leaf attribute list
1112 * or a leaf in a node attribute list.
1113 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001114STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001115xfs_attr3_leaf_create(
1116 struct xfs_da_args *args,
1117 xfs_dablk_t blkno,
1118 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Dave Chinner517c2222013-04-24 18:58:55 +10001120 struct xfs_attr_leafblock *leaf;
1121 struct xfs_attr3_icleaf_hdr ichdr;
1122 struct xfs_inode *dp = args->dp;
1123 struct xfs_mount *mp = dp->i_mount;
1124 struct xfs_buf *bp;
1125 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Dave Chinner5a5881c2012-03-22 05:15:13 +00001127 trace_xfs_attr_leaf_create(args);
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
1130 XFS_ATTR_FORK);
1131 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10001132 return error;
1133 bp->b_ops = &xfs_attr3_leaf_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +11001134 xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001135 leaf = bp->b_addr;
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001136 memset(leaf, 0, args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10001137
1138 memset(&ichdr, 0, sizeof(ichdr));
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001139 ichdr.firstused = args->geo->blksize;
Dave Chinner517c2222013-04-24 18:58:55 +10001140
1141 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1142 struct xfs_da3_blkinfo *hdr3 = bp->b_addr;
1143
1144 ichdr.magic = XFS_ATTR3_LEAF_MAGIC;
1145
1146 hdr3->blkno = cpu_to_be64(bp->b_bn);
1147 hdr3->owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +10001148 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinner517c2222013-04-24 18:58:55 +10001149
1150 ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr);
1151 } else {
1152 ichdr.magic = XFS_ATTR_LEAF_MAGIC;
1153 ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Dave Chinner517c2222013-04-24 18:58:55 +10001155 ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Brian Foster2f661242015-04-13 11:26:02 +10001157 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001158 xfs_trans_log_buf(args->trans, bp, 0, args->geo->blksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 *bpp = bp;
Dave Chinner517c2222013-04-24 18:58:55 +10001161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
1164/*
1165 * Split the leaf node, rebalance, then add the new entry.
1166 */
1167int
Dave Chinner517c2222013-04-24 18:58:55 +10001168xfs_attr3_leaf_split(
1169 struct xfs_da_state *state,
1170 struct xfs_da_state_blk *oldblk,
1171 struct xfs_da_state_blk *newblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 xfs_dablk_t blkno;
1174 int error;
1175
Dave Chinner5a5881c2012-03-22 05:15:13 +00001176 trace_xfs_attr_leaf_split(state->args);
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /*
1179 * Allocate space for a new leaf node.
1180 */
1181 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1182 error = xfs_da_grow_inode(state->args, &blkno);
1183 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001184 return error;
Dave Chinner517c2222013-04-24 18:58:55 +10001185 error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001187 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 newblk->blkno = blkno;
1189 newblk->magic = XFS_ATTR_LEAF_MAGIC;
1190
1191 /*
1192 * Rebalance the entries across the two leaves.
1193 * NOTE: rebalance() currently depends on the 2nd block being empty.
1194 */
Dave Chinner517c2222013-04-24 18:58:55 +10001195 xfs_attr3_leaf_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001196 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001198 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 /*
1201 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1202 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1203 * "new" attrs info. Will need the "old" info to remove it later.
1204 *
1205 * Insert the "new" entry in the correct block.
1206 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001207 if (state->inleaf) {
1208 trace_xfs_attr_leaf_add_old(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +10001209 error = xfs_attr3_leaf_add(oldblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001210 } else {
1211 trace_xfs_attr_leaf_add_new(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +10001212 error = xfs_attr3_leaf_add(newblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 /*
1216 * Update last hashval in each block since we added the name.
1217 */
1218 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1219 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
Eric Sandeend99831f2014-06-22 15:03:54 +10001220 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
1223/*
1224 * Add a name to the leaf attribute list structure.
1225 */
1226int
Dave Chinner517c2222013-04-24 18:58:55 +10001227xfs_attr3_leaf_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001228 struct xfs_buf *bp,
1229 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Dave Chinner517c2222013-04-24 18:58:55 +10001231 struct xfs_attr_leafblock *leaf;
1232 struct xfs_attr3_icleaf_hdr ichdr;
1233 int tablesize;
1234 int entsize;
1235 int sum;
1236 int tmp;
1237 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Dave Chinner5a5881c2012-03-22 05:15:13 +00001239 trace_xfs_attr_leaf_add(args);
1240
Dave Chinner1d9025e2012-06-22 18:50:14 +10001241 leaf = bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10001242 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10001243 ASSERT(args->index >= 0 && args->index <= ichdr.count);
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10001244 entsize = xfs_attr_leaf_newentsize(args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 /*
1247 * Search through freemap for first-fit on new name length.
1248 * (may need to figure in size of entry struct too)
1249 */
Dave Chinner517c2222013-04-24 18:58:55 +10001250 tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t)
1251 + xfs_attr3_leaf_hdr_size(leaf);
1252 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) {
1253 if (tablesize > ichdr.firstused) {
1254 sum += ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 continue;
1256 }
Dave Chinner517c2222013-04-24 18:58:55 +10001257 if (!ichdr.freemap[i].size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 continue; /* no space in this map */
1259 tmp = entsize;
Dave Chinner517c2222013-04-24 18:58:55 +10001260 if (ichdr.freemap[i].base < ichdr.firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 tmp += sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10001262 if (ichdr.freemap[i].size >= tmp) {
1263 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, i);
1264 goto out_log_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Dave Chinner517c2222013-04-24 18:58:55 +10001266 sum += ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268
1269 /*
1270 * If there are no holes in the address space of the block,
1271 * and we don't have enough freespace, then compaction will do us
1272 * no good and we should just give up.
1273 */
Dave Chinner517c2222013-04-24 18:58:55 +10001274 if (!ichdr.holes && sum < entsize)
Dave Chinner24513372014-06-25 14:58:08 +10001275 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 /*
1278 * Compact the entries to coalesce free space.
1279 * This may change the hdr->count via dropping INCOMPLETE entries.
1280 */
Dave Chinner517c2222013-04-24 18:58:55 +10001281 xfs_attr3_leaf_compact(args, &ichdr, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 /*
1284 * After compaction, the block is guaranteed to have only one
1285 * free region, in freemap[0]. If it is not big enough, give up.
1286 */
Dave Chinner517c2222013-04-24 18:58:55 +10001287 if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) {
Dave Chinner24513372014-06-25 14:58:08 +10001288 tmp = -ENOSPC;
Dave Chinner517c2222013-04-24 18:58:55 +10001289 goto out_log_hdr;
1290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Dave Chinner517c2222013-04-24 18:58:55 +10001292 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0);
1293
1294out_log_hdr:
Brian Foster2f661242015-04-13 11:26:02 +10001295 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
Dave Chinner517c2222013-04-24 18:58:55 +10001296 xfs_trans_log_buf(args->trans, bp,
1297 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
1298 xfs_attr3_leaf_hdr_size(leaf)));
1299 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
1302/*
1303 * Add a name to a leaf attribute list structure.
1304 */
1305STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001306xfs_attr3_leaf_add_work(
1307 struct xfs_buf *bp,
1308 struct xfs_attr3_icleaf_hdr *ichdr,
1309 struct xfs_da_args *args,
1310 int mapindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Dave Chinner517c2222013-04-24 18:58:55 +10001312 struct xfs_attr_leafblock *leaf;
1313 struct xfs_attr_leaf_entry *entry;
1314 struct xfs_attr_leaf_name_local *name_loc;
1315 struct xfs_attr_leaf_name_remote *name_rmt;
1316 struct xfs_mount *mp;
1317 int tmp;
1318 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Dave Chinneree732592012-11-12 22:53:53 +11001320 trace_xfs_attr_leaf_add_work(args);
1321
Dave Chinner1d9025e2012-06-22 18:50:14 +10001322 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001323 ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE);
1324 ASSERT(args->index >= 0 && args->index <= ichdr->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 /*
1327 * Force open some space in the entry array and fill it in.
1328 */
Dave Chinner517c2222013-04-24 18:58:55 +10001329 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1330 if (args->index < ichdr->count) {
1331 tmp = ichdr->count - args->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 tmp *= sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10001333 memmove(entry + 1, entry, tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001334 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1336 }
Dave Chinner517c2222013-04-24 18:58:55 +10001337 ichdr->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 /*
1340 * Allocate space for the new string (at the end of the run).
1341 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 mp = args->trans->t_mountp;
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001343 ASSERT(ichdr->freemap[mapindex].base < args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10001344 ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0);
1345 ASSERT(ichdr->freemap[mapindex].size >=
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10001346 xfs_attr_leaf_newentsize(args, NULL));
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001347 ASSERT(ichdr->freemap[mapindex].size < args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10001348 ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0);
1349
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10001350 ichdr->freemap[mapindex].size -= xfs_attr_leaf_newentsize(args, &tmp);
Dave Chinner517c2222013-04-24 18:58:55 +10001351
1352 entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base +
1353 ichdr->freemap[mapindex].size);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001354 entry->hashval = cpu_to_be32(args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10001356 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Barry Naujok6a178102008-05-21 16:42:05 +10001357 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 entry->flags |= XFS_ATTR_INCOMPLETE;
1359 if ((args->blkno2 == args->blkno) &&
1360 (args->index2 <= args->index)) {
1361 args->index2++;
1362 }
1363 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001364 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001366 ASSERT((args->index == 0) ||
1367 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
Dave Chinner517c2222013-04-24 18:58:55 +10001368 ASSERT((args->index == ichdr->count - 1) ||
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001369 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 * For "remote" attribute values, simply note that we need to
1373 * allocate space for the "remote" value. We can't actually
1374 * allocate the extents in this transaction, and we can't decide
1375 * which blocks they should be as we might allocate more blocks
1376 * as part of this transaction (a split operation for example).
1377 */
1378 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10001379 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 name_loc->namelen = args->namelen;
Nathan Scott053b5752006-03-17 17:29:09 +11001381 name_loc->valuelen = cpu_to_be16(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1383 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
Nathan Scott053b5752006-03-17 17:29:09 +11001384 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10001386 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 name_rmt->namelen = args->namelen;
1388 memcpy((char *)name_rmt->name, args->name, args->namelen);
1389 entry->flags |= XFS_ATTR_INCOMPLETE;
1390 /* just in case */
1391 name_rmt->valuelen = 0;
1392 name_rmt->valueblk = 0;
1393 args->rmtblkno = 1;
Dave Chinnerad1858d2013-05-21 18:02:08 +10001394 args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
Dave Chinner8275cdd2014-05-06 07:37:31 +10001395 args->rmtvaluelen = args->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001397 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10001398 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 xfs_attr_leaf_entsize(leaf, args->index)));
1400
1401 /*
1402 * Update the control info for this leaf node
1403 */
Dave Chinner517c2222013-04-24 18:58:55 +10001404 if (be16_to_cpu(entry->nameidx) < ichdr->firstused)
1405 ichdr->firstused = be16_to_cpu(entry->nameidx);
1406
1407 ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t)
1408 + xfs_attr3_leaf_hdr_size(leaf));
1409 tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t)
1410 + xfs_attr3_leaf_hdr_size(leaf);
1411
1412 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
1413 if (ichdr->freemap[i].base == tmp) {
1414 ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t);
1415 ichdr->freemap[i].size -= sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417 }
Dave Chinner517c2222013-04-24 18:58:55 +10001418 ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index);
1419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
1422/*
1423 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1424 */
1425STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10001426xfs_attr3_leaf_compact(
Dave Chinneree732592012-11-12 22:53:53 +11001427 struct xfs_da_args *args,
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001428 struct xfs_attr3_icleaf_hdr *ichdr_dst,
Dave Chinneree732592012-11-12 22:53:53 +11001429 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001431 struct xfs_attr_leafblock *leaf_src;
1432 struct xfs_attr_leafblock *leaf_dst;
1433 struct xfs_attr3_icleaf_hdr ichdr_src;
Dave Chinneree732592012-11-12 22:53:53 +11001434 struct xfs_trans *trans = args->trans;
Dave Chinneree732592012-11-12 22:53:53 +11001435 char *tmpbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Dave Chinneree732592012-11-12 22:53:53 +11001437 trace_xfs_attr_leaf_compact(args);
1438
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001439 tmpbuffer = kmem_alloc(args->geo->blksize, KM_SLEEP);
1440 memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
1441 memset(bp->b_addr, 0, args->geo->blksize);
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001442 leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
1443 leaf_dst = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 /*
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001446 * Copy the on-disk header back into the destination buffer to ensure
1447 * all the information in the header that is not part of the incore
1448 * header structure is preserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 */
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001450 memcpy(bp->b_addr, tmpbuffer, xfs_attr3_leaf_hdr_size(leaf_src));
1451
1452 /* Initialise the incore headers */
1453 ichdr_src = *ichdr_dst; /* struct copy */
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001454 ichdr_dst->firstused = args->geo->blksize;
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001455 ichdr_dst->usedbytes = 0;
1456 ichdr_dst->count = 0;
1457 ichdr_dst->holes = 0;
1458 ichdr_dst->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_src);
1459 ichdr_dst->freemap[0].size = ichdr_dst->firstused -
1460 ichdr_dst->freemap[0].base;
1461
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001462 /* write the header back to initialise the underlying buffer */
Brian Foster2f661242015-04-13 11:26:02 +10001463 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf_dst, ichdr_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 /*
1466 * Copy all entry's in the same (sorted) order,
1467 * but allocate name/value pairs packed and in sequence.
1468 */
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001469 xfs_attr3_leaf_moveents(args, leaf_src, &ichdr_src, 0,
1470 leaf_dst, ichdr_dst, 0, ichdr_src.count);
Dave Chinner517c2222013-04-24 18:58:55 +10001471 /*
1472 * this logs the entire buffer, but the caller must write the header
1473 * back to the buffer when it is finished modifying it.
1474 */
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001475 xfs_trans_log_buf(trans, bp, 0, args->geo->blksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001477 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480/*
Dave Chinner517c2222013-04-24 18:58:55 +10001481 * Compare two leaf blocks "order".
1482 * Return 0 unless leaf2 should go before leaf1.
1483 */
1484static int
1485xfs_attr3_leaf_order(
1486 struct xfs_buf *leaf1_bp,
1487 struct xfs_attr3_icleaf_hdr *leaf1hdr,
1488 struct xfs_buf *leaf2_bp,
1489 struct xfs_attr3_icleaf_hdr *leaf2hdr)
1490{
1491 struct xfs_attr_leaf_entry *entries1;
1492 struct xfs_attr_leaf_entry *entries2;
1493
1494 entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr);
1495 entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr);
1496 if (leaf1hdr->count > 0 && leaf2hdr->count > 0 &&
1497 ((be32_to_cpu(entries2[0].hashval) <
1498 be32_to_cpu(entries1[0].hashval)) ||
1499 (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) <
1500 be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) {
1501 return 1;
1502 }
1503 return 0;
1504}
1505
1506int
1507xfs_attr_leaf_order(
1508 struct xfs_buf *leaf1_bp,
1509 struct xfs_buf *leaf2_bp)
1510{
1511 struct xfs_attr3_icleaf_hdr ichdr1;
1512 struct xfs_attr3_icleaf_hdr ichdr2;
Brian Foster2f661242015-04-13 11:26:02 +10001513 struct xfs_mount *mp = leaf1_bp->b_target->bt_mount;
Dave Chinner517c2222013-04-24 18:58:55 +10001514
Brian Foster2f661242015-04-13 11:26:02 +10001515 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr1, leaf1_bp->b_addr);
1516 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr2, leaf2_bp->b_addr);
Dave Chinner517c2222013-04-24 18:58:55 +10001517 return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2);
1518}
1519
1520/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 * Redistribute the attribute list entries between two leaf nodes,
1522 * taking into account the size of the new entry.
1523 *
1524 * NOTE: if new block is empty, then it will get the upper half of the
1525 * old block. At present, all (one) callers pass in an empty second block.
1526 *
1527 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1528 * to match what it is doing in splitting the attribute leaf block. Those
1529 * values are used in "atomic rename" operations on attributes. Note that
1530 * the "new" and "old" values can end up in different blocks.
1531 */
1532STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10001533xfs_attr3_leaf_rebalance(
1534 struct xfs_da_state *state,
1535 struct xfs_da_state_blk *blk1,
1536 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Dave Chinner517c2222013-04-24 18:58:55 +10001538 struct xfs_da_args *args;
1539 struct xfs_attr_leafblock *leaf1;
1540 struct xfs_attr_leafblock *leaf2;
1541 struct xfs_attr3_icleaf_hdr ichdr1;
1542 struct xfs_attr3_icleaf_hdr ichdr2;
1543 struct xfs_attr_leaf_entry *entries1;
1544 struct xfs_attr_leaf_entry *entries2;
1545 int count;
1546 int totallen;
1547 int max;
1548 int space;
1549 int swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /*
1552 * Set up environment.
1553 */
1554 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1555 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001556 leaf1 = blk1->bp->b_addr;
1557 leaf2 = blk2->bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10001558 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr1, leaf1);
1559 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, leaf2);
Dave Chinner517c2222013-04-24 18:58:55 +10001560 ASSERT(ichdr2.count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 args = state->args;
1562
Dave Chinner5a5881c2012-03-22 05:15:13 +00001563 trace_xfs_attr_leaf_rebalance(args);
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /*
1566 * Check ordering of blocks, reverse if it makes things simpler.
1567 *
1568 * NOTE: Given that all (current) callers pass in an empty
1569 * second block, this code should never set "swap".
1570 */
1571 swap = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10001572 if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) {
1573 struct xfs_da_state_blk *tmp_blk;
1574 struct xfs_attr3_icleaf_hdr tmp_ichdr;
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 tmp_blk = blk1;
1577 blk1 = blk2;
1578 blk2 = tmp_blk;
Dave Chinner517c2222013-04-24 18:58:55 +10001579
1580 /* struct copies to swap them rather than reconverting */
1581 tmp_ichdr = ichdr1;
1582 ichdr1 = ichdr2;
1583 ichdr2 = tmp_ichdr;
1584
Dave Chinner1d9025e2012-06-22 18:50:14 +10001585 leaf1 = blk1->bp->b_addr;
1586 leaf2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 swap = 1;
1588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 /*
1591 * Examine entries until we reduce the absolute difference in
1592 * byte usage between the two blocks to a minimum. Then get
1593 * the direction to copy and the number of elements to move.
1594 *
1595 * "inleaf" is true if the new entry should be inserted into blk1.
1596 * If "swap" is also true, then reverse the sense of "inleaf".
1597 */
Dave Chinner517c2222013-04-24 18:58:55 +10001598 state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1,
1599 blk2, &ichdr2,
1600 &count, &totallen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (swap)
1602 state->inleaf = !state->inleaf;
1603
1604 /*
1605 * Move any entries required from leaf to leaf:
1606 */
Dave Chinner517c2222013-04-24 18:58:55 +10001607 if (count < ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 /*
1609 * Figure the total bytes to be added to the destination leaf.
1610 */
1611 /* number entries being moved */
Dave Chinner517c2222013-04-24 18:58:55 +10001612 count = ichdr1.count - count;
1613 space = ichdr1.usedbytes - totallen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 space += count * sizeof(xfs_attr_leaf_entry_t);
1615
1616 /*
1617 * leaf2 is the destination, compact it if it looks tight.
1618 */
Dave Chinner517c2222013-04-24 18:58:55 +10001619 max = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1620 max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001621 if (space > max)
Dave Chinner517c2222013-04-24 18:58:55 +10001622 xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 /*
1625 * Move high entries from leaf1 to low end of leaf2.
1626 */
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001627 xfs_attr3_leaf_moveents(args, leaf1, &ichdr1,
1628 ichdr1.count - count, leaf2, &ichdr2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Dave Chinner517c2222013-04-24 18:58:55 +10001630 } else if (count > ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /*
1632 * I assert that since all callers pass in an empty
1633 * second buffer, this code should never execute.
1634 */
Dave Chinner07428d72012-11-12 22:09:44 +11001635 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 /*
1638 * Figure the total bytes to be added to the destination leaf.
1639 */
1640 /* number entries being moved */
Dave Chinner517c2222013-04-24 18:58:55 +10001641 count -= ichdr1.count;
1642 space = totallen - ichdr1.usedbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 space += count * sizeof(xfs_attr_leaf_entry_t);
1644
1645 /*
1646 * leaf1 is the destination, compact it if it looks tight.
1647 */
Dave Chinner517c2222013-04-24 18:58:55 +10001648 max = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1649 max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001650 if (space > max)
Dave Chinner517c2222013-04-24 18:58:55 +10001651 xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 /*
1654 * Move low entries from leaf2 to high end of leaf1.
1655 */
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001656 xfs_attr3_leaf_moveents(args, leaf2, &ichdr2, 0, leaf1, &ichdr1,
1657 ichdr1.count, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659
Brian Foster2f661242015-04-13 11:26:02 +10001660 xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf1, &ichdr1);
1661 xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf2, &ichdr2);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001662 xfs_trans_log_buf(args->trans, blk1->bp, 0, args->geo->blksize - 1);
1663 xfs_trans_log_buf(args->trans, blk2->bp, 0, args->geo->blksize - 1);
Dave Chinner517c2222013-04-24 18:58:55 +10001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /*
1666 * Copy out last hashval in each block for B-tree code.
1667 */
Dave Chinner517c2222013-04-24 18:58:55 +10001668 entries1 = xfs_attr3_leaf_entryp(leaf1);
1669 entries2 = xfs_attr3_leaf_entryp(leaf2);
1670 blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval);
1671 blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 /*
1674 * Adjust the expected index for insertion.
1675 * NOTE: this code depends on the (current) situation that the
1676 * second block was originally empty.
1677 *
1678 * If the insertion point moved to the 2nd block, we must adjust
1679 * the index. We must also track the entry just following the
1680 * new entry for use in an "atomic rename" operation, that entry
1681 * is always the "old" entry and the "new" entry is what we are
1682 * inserting. The index/blkno fields refer to the "old" entry,
1683 * while the index2/blkno2 fields refer to the "new" entry.
1684 */
Dave Chinner517c2222013-04-24 18:58:55 +10001685 if (blk1->index > ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 ASSERT(state->inleaf == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10001687 blk2->index = blk1->index - ichdr1.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 args->index = args->index2 = blk2->index;
1689 args->blkno = args->blkno2 = blk2->blkno;
Dave Chinner517c2222013-04-24 18:58:55 +10001690 } else if (blk1->index == ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if (state->inleaf) {
1692 args->index = blk1->index;
1693 args->blkno = blk1->blkno;
1694 args->index2 = 0;
1695 args->blkno2 = blk2->blkno;
1696 } else {
Dave Chinner07428d72012-11-12 22:09:44 +11001697 /*
1698 * On a double leaf split, the original attr location
1699 * is already stored in blkno2/index2, so don't
1700 * overwrite it overwise we corrupt the tree.
1701 */
Dave Chinner517c2222013-04-24 18:58:55 +10001702 blk2->index = blk1->index - ichdr1.count;
Dave Chinner07428d72012-11-12 22:09:44 +11001703 args->index = blk2->index;
1704 args->blkno = blk2->blkno;
1705 if (!state->extravalid) {
1706 /*
1707 * set the new attr location to match the old
1708 * one and let the higher level split code
1709 * decide where in the leaf to place it.
1710 */
1711 args->index2 = blk2->index;
1712 args->blkno2 = blk2->blkno;
1713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715 } else {
1716 ASSERT(state->inleaf == 1);
1717 args->index = args->index2 = blk1->index;
1718 args->blkno = args->blkno2 = blk1->blkno;
1719 }
1720}
1721
1722/*
1723 * Examine entries until we reduce the absolute difference in
1724 * byte usage between the two blocks to a minimum.
1725 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1726 * GROT: there will always be enough room in either block for a new entry.
1727 * GROT: Do a double-split for this case?
1728 */
1729STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001730xfs_attr3_leaf_figure_balance(
1731 struct xfs_da_state *state,
1732 struct xfs_da_state_blk *blk1,
1733 struct xfs_attr3_icleaf_hdr *ichdr1,
1734 struct xfs_da_state_blk *blk2,
1735 struct xfs_attr3_icleaf_hdr *ichdr2,
1736 int *countarg,
1737 int *usedbytesarg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Dave Chinner517c2222013-04-24 18:58:55 +10001739 struct xfs_attr_leafblock *leaf1 = blk1->bp->b_addr;
1740 struct xfs_attr_leafblock *leaf2 = blk2->bp->b_addr;
1741 struct xfs_attr_leaf_entry *entry;
1742 int count;
1743 int max;
1744 int index;
1745 int totallen = 0;
1746 int half;
1747 int lastdelta;
1748 int foundit = 0;
1749 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 /*
1752 * Examine entries until we reduce the absolute difference in
1753 * byte usage between the two blocks to a minimum.
1754 */
Dave Chinner517c2222013-04-24 18:58:55 +10001755 max = ichdr1->count + ichdr2->count;
1756 half = (max + 1) * sizeof(*entry);
1757 half += ichdr1->usedbytes + ichdr2->usedbytes +
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10001758 xfs_attr_leaf_newentsize(state->args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 half /= 2;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001760 lastdelta = state->args->geo->blksize;
Dave Chinner517c2222013-04-24 18:58:55 +10001761 entry = xfs_attr3_leaf_entryp(leaf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 for (count = index = 0; count < max; entry++, index++, count++) {
1763
1764#define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1765 /*
1766 * The new entry is in the first block, account for it.
1767 */
1768 if (count == blk1->index) {
1769 tmp = totallen + sizeof(*entry) +
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10001770 xfs_attr_leaf_newentsize(state->args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1772 break;
1773 lastdelta = XFS_ATTR_ABS(half - tmp);
1774 totallen = tmp;
1775 foundit = 1;
1776 }
1777
1778 /*
1779 * Wrap around into the second block if necessary.
1780 */
Dave Chinner517c2222013-04-24 18:58:55 +10001781 if (count == ichdr1->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 leaf1 = leaf2;
Dave Chinner517c2222013-04-24 18:58:55 +10001783 entry = xfs_attr3_leaf_entryp(leaf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 index = 0;
1785 }
1786
1787 /*
1788 * Figure out if next leaf entry would be too much.
1789 */
1790 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1791 index);
1792 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1793 break;
1794 lastdelta = XFS_ATTR_ABS(half - tmp);
1795 totallen = tmp;
1796#undef XFS_ATTR_ABS
1797 }
1798
1799 /*
1800 * Calculate the number of usedbytes that will end up in lower block.
1801 * If new entry not in lower block, fix up the count.
1802 */
1803 totallen -= count * sizeof(*entry);
1804 if (foundit) {
1805 totallen -= sizeof(*entry) +
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10001806 xfs_attr_leaf_newentsize(state->args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808
1809 *countarg = count;
1810 *usedbytesarg = totallen;
Dave Chinner517c2222013-04-24 18:58:55 +10001811 return foundit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
1814/*========================================================================
1815 * Routines used for shrinking the Btree.
1816 *========================================================================*/
1817
1818/*
1819 * Check a leaf block and its neighbors to see if the block should be
1820 * collapsed into one or the other neighbor. Always keep the block
1821 * with the smaller block number.
1822 * If the current block is over 50% full, don't try to join it, return 0.
1823 * If the block is empty, fill in the state structure and return 2.
1824 * If it can be collapsed, fill in the state structure and return 1.
1825 * If nothing can be done, return 0.
1826 *
1827 * GROT: allow for INCOMPLETE entries in calculation.
1828 */
1829int
Dave Chinner517c2222013-04-24 18:58:55 +10001830xfs_attr3_leaf_toosmall(
1831 struct xfs_da_state *state,
1832 int *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Dave Chinner517c2222013-04-24 18:58:55 +10001834 struct xfs_attr_leafblock *leaf;
1835 struct xfs_da_state_blk *blk;
1836 struct xfs_attr3_icleaf_hdr ichdr;
1837 struct xfs_buf *bp;
1838 xfs_dablk_t blkno;
1839 int bytes;
1840 int forward;
1841 int error;
1842 int retval;
1843 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Dave Chinneree732592012-11-12 22:53:53 +11001845 trace_xfs_attr_leaf_toosmall(state->args);
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 /*
1848 * Check for the degenerate case of the block being over 50% full.
1849 * If so, it's not worth even looking to see if we might be able
1850 * to coalesce with a sibling.
1851 */
1852 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner517c2222013-04-24 18:58:55 +10001853 leaf = blk->bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10001854 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10001855 bytes = xfs_attr3_leaf_hdr_size(leaf) +
1856 ichdr.count * sizeof(xfs_attr_leaf_entry_t) +
1857 ichdr.usedbytes;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001858 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 *action = 0; /* blk over 50%, don't try to join */
Eric Sandeend99831f2014-06-22 15:03:54 +10001860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862
1863 /*
1864 * Check for the degenerate case of the block being empty.
1865 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001866 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 * to merge with the forward block unless it is NULL.
1868 */
Dave Chinner517c2222013-04-24 18:58:55 +10001869 if (ichdr.count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /*
1871 * Make altpath point to the block we want to keep and
1872 * path point to the block we want to drop (this one).
1873 */
Dave Chinner517c2222013-04-24 18:58:55 +10001874 forward = (ichdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001876 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 0, &retval);
1878 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001879 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (retval) {
1881 *action = 0;
1882 } else {
1883 *action = 2;
1884 }
Dave Chinner517c2222013-04-24 18:58:55 +10001885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887
1888 /*
1889 * Examine each sibling block to see if we can coalesce with
1890 * at least 25% free space to spare. We need to figure out
1891 * whether to merge with the forward or the backward block.
1892 * We prefer coalescing with the lower numbered sibling so as
1893 * to shrink an attribute list over time.
1894 */
1895 /* start with smaller blk num */
Dave Chinner517c2222013-04-24 18:58:55 +10001896 forward = ichdr.forw < ichdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 for (i = 0; i < 2; forward = !forward, i++) {
Dave Chinner517c2222013-04-24 18:58:55 +10001898 struct xfs_attr3_icleaf_hdr ichdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if (forward)
Dave Chinner517c2222013-04-24 18:58:55 +10001900 blkno = ichdr.forw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 else
Dave Chinner517c2222013-04-24 18:58:55 +10001902 blkno = ichdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (blkno == 0)
1904 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10001905 error = xfs_attr3_leaf_read(state->args->trans, state->args->dp,
Dave Chinnerad14c332012-11-12 22:54:16 +11001906 blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001908 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Brian Foster2f661242015-04-13 11:26:02 +10001910 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, bp->b_addr);
Dave Chinner517c2222013-04-24 18:58:55 +10001911
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001912 bytes = state->args->geo->blksize -
1913 (state->args->geo->blksize >> 2) -
Dave Chinner517c2222013-04-24 18:58:55 +10001914 ichdr.usedbytes - ichdr2.usedbytes -
1915 ((ichdr.count + ichdr2.count) *
1916 sizeof(xfs_attr_leaf_entry_t)) -
1917 xfs_attr3_leaf_hdr_size(leaf);
1918
Dave Chinner1d9025e2012-06-22 18:50:14 +10001919 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (bytes >= 0)
1921 break; /* fits with at least 25% to spare */
1922 }
1923 if (i >= 2) {
1924 *action = 0;
Eric Sandeend99831f2014-06-22 15:03:54 +10001925 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
1927
1928 /*
1929 * Make altpath point to the block we want to keep (the lower
1930 * numbered block) and path point to the block we want to drop.
1931 */
1932 memcpy(&state->altpath, &state->path, sizeof(state->path));
1933 if (blkno < blk->blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001934 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 0, &retval);
1936 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001937 error = xfs_da3_path_shift(state, &state->path, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 0, &retval);
1939 }
1940 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001941 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (retval) {
1943 *action = 0;
1944 } else {
1945 *action = 1;
1946 }
Eric Sandeend99831f2014-06-22 15:03:54 +10001947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
1950/*
1951 * Remove a name from the leaf attribute list structure.
1952 *
1953 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1954 * If two leaves are 37% full, when combined they will leave 25% free.
1955 */
1956int
Dave Chinner517c2222013-04-24 18:58:55 +10001957xfs_attr3_leaf_remove(
1958 struct xfs_buf *bp,
1959 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Dave Chinner517c2222013-04-24 18:58:55 +10001961 struct xfs_attr_leafblock *leaf;
1962 struct xfs_attr3_icleaf_hdr ichdr;
1963 struct xfs_attr_leaf_entry *entry;
Dave Chinner517c2222013-04-24 18:58:55 +10001964 int before;
1965 int after;
1966 int smallest;
1967 int entsize;
1968 int tablesize;
1969 int tmp;
1970 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Dave Chinneree732592012-11-12 22:53:53 +11001972 trace_xfs_attr_leaf_remove(args);
1973
Dave Chinner1d9025e2012-06-22 18:50:14 +10001974 leaf = bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10001975 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10001976
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001977 ASSERT(ichdr.count > 0 && ichdr.count < args->geo->blksize / 8);
Dave Chinner517c2222013-04-24 18:58:55 +10001978 ASSERT(args->index >= 0 && args->index < ichdr.count);
1979 ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) +
1980 xfs_attr3_leaf_hdr_size(leaf));
1981
1982 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1983
1984 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
Dave Chinnerc2c4c472014-06-06 15:21:45 +10001985 ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 /*
1988 * Scan through free region table:
1989 * check for adjacency of free'd entry with an existing one,
1990 * find smallest free region in case we need to replace it,
1991 * adjust any map that borders the entry table,
1992 */
Dave Chinner517c2222013-04-24 18:58:55 +10001993 tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t)
1994 + xfs_attr3_leaf_hdr_size(leaf);
1995 tmp = ichdr.freemap[0].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 before = after = -1;
1997 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1998 entsize = xfs_attr_leaf_entsize(leaf, args->index);
Dave Chinner517c2222013-04-24 18:58:55 +10001999 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002000 ASSERT(ichdr.freemap[i].base < args->geo->blksize);
2001 ASSERT(ichdr.freemap[i].size < args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10002002 if (ichdr.freemap[i].base == tablesize) {
2003 ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t);
2004 ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
2006
Dave Chinner517c2222013-04-24 18:58:55 +10002007 if (ichdr.freemap[i].base + ichdr.freemap[i].size ==
2008 be16_to_cpu(entry->nameidx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 before = i;
Dave Chinner517c2222013-04-24 18:58:55 +10002010 } else if (ichdr.freemap[i].base ==
2011 (be16_to_cpu(entry->nameidx) + entsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 after = i;
Dave Chinner517c2222013-04-24 18:58:55 +10002013 } else if (ichdr.freemap[i].size < tmp) {
2014 tmp = ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 smallest = i;
2016 }
2017 }
2018
2019 /*
2020 * Coalesce adjacent freemap regions,
2021 * or replace the smallest region.
2022 */
2023 if ((before >= 0) || (after >= 0)) {
2024 if ((before >= 0) && (after >= 0)) {
Dave Chinner517c2222013-04-24 18:58:55 +10002025 ichdr.freemap[before].size += entsize;
2026 ichdr.freemap[before].size += ichdr.freemap[after].size;
2027 ichdr.freemap[after].base = 0;
2028 ichdr.freemap[after].size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 } else if (before >= 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002030 ichdr.freemap[before].size += entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002032 ichdr.freemap[after].base = be16_to_cpu(entry->nameidx);
2033 ichdr.freemap[after].size += entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035 } else {
2036 /*
2037 * Replace smallest region (if it is smaller than free'd entry)
2038 */
Dave Chinner517c2222013-04-24 18:58:55 +10002039 if (ichdr.freemap[smallest].size < entsize) {
2040 ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx);
2041 ichdr.freemap[smallest].size = entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043 }
2044
2045 /*
2046 * Did we remove the first entry?
2047 */
Dave Chinner517c2222013-04-24 18:58:55 +10002048 if (be16_to_cpu(entry->nameidx) == ichdr.firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 smallest = 1;
2050 else
2051 smallest = 0;
2052
2053 /*
2054 * Compress the remaining entries and zero out the removed stuff.
2055 */
Dave Chinner517c2222013-04-24 18:58:55 +10002056 memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize);
2057 ichdr.usedbytes -= entsize;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002058 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002059 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 entsize));
2061
Dave Chinner517c2222013-04-24 18:58:55 +10002062 tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t);
2063 memmove(entry, entry + 1, tmp);
2064 ichdr.count--;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002065 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002066 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t)));
2067
2068 entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count];
2069 memset(entry, 0, sizeof(xfs_attr_leaf_entry_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 /*
2072 * If we removed the first entry, re-find the first used byte
2073 * in the name area. Note that if the entry was the "firstused",
2074 * then we don't have a "hole" in our block resulting from
2075 * removing the name.
2076 */
2077 if (smallest) {
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002078 tmp = args->geo->blksize;
Dave Chinner517c2222013-04-24 18:58:55 +10002079 entry = xfs_attr3_leaf_entryp(leaf);
2080 for (i = ichdr.count - 1; i >= 0; entry++, i--) {
2081 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002082 ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002083
2084 if (be16_to_cpu(entry->nameidx) < tmp)
2085 tmp = be16_to_cpu(entry->nameidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
Dave Chinner517c2222013-04-24 18:58:55 +10002087 ichdr.firstused = tmp;
Brian Foster66db8102015-04-13 11:27:59 +10002088 ASSERT(ichdr.firstused != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002090 ichdr.holes = 1; /* mark as needing compaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
Brian Foster2f661242015-04-13 11:26:02 +10002092 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002093 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002094 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
2095 xfs_attr3_leaf_hdr_size(leaf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 /*
2098 * Check if leaf is less than 50% full, caller may want to
2099 * "join" the leaf with a sibling if so.
2100 */
Dave Chinner517c2222013-04-24 18:58:55 +10002101 tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) +
2102 ichdr.count * sizeof(xfs_attr_leaf_entry_t);
2103
Dave Chinnered358c02014-06-06 15:18:10 +10002104 return tmp < args->geo->magicpct; /* leaf is < 37% full */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
2107/*
2108 * Move all the attribute list entries from drop_leaf into save_leaf.
2109 */
2110void
Dave Chinner517c2222013-04-24 18:58:55 +10002111xfs_attr3_leaf_unbalance(
2112 struct xfs_da_state *state,
2113 struct xfs_da_state_blk *drop_blk,
2114 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Dave Chinner517c2222013-04-24 18:58:55 +10002116 struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr;
2117 struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr;
2118 struct xfs_attr3_icleaf_hdr drophdr;
2119 struct xfs_attr3_icleaf_hdr savehdr;
2120 struct xfs_attr_leaf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Dave Chinner5a5881c2012-03-22 05:15:13 +00002122 trace_xfs_attr_leaf_unbalance(state->args);
2123
Dave Chinner1d9025e2012-06-22 18:50:14 +10002124 drop_leaf = drop_blk->bp->b_addr;
2125 save_leaf = save_blk->bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10002126 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &drophdr, drop_leaf);
2127 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &savehdr, save_leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10002128 entry = xfs_attr3_leaf_entryp(drop_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /*
2131 * Save last hashval from dying block for later Btree fixup.
2132 */
Dave Chinner517c2222013-04-24 18:58:55 +10002133 drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 /*
2136 * Check if we need a temp buffer, or can we do it in place.
2137 * Note that we don't check "leaf" for holes because we will
2138 * always be dropping it, toosmall() decided that for us already.
2139 */
Dave Chinner517c2222013-04-24 18:58:55 +10002140 if (savehdr.holes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 /*
2142 * dest leaf has no holes, so we add there. May need
2143 * to make some room in the entry array.
2144 */
Dave Chinner517c2222013-04-24 18:58:55 +10002145 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2146 drop_blk->bp, &drophdr)) {
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002147 xfs_attr3_leaf_moveents(state->args,
2148 drop_leaf, &drophdr, 0,
Dave Chinner517c2222013-04-24 18:58:55 +10002149 save_leaf, &savehdr, 0,
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002150 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 } else {
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002152 xfs_attr3_leaf_moveents(state->args,
2153 drop_leaf, &drophdr, 0,
Dave Chinner517c2222013-04-24 18:58:55 +10002154 save_leaf, &savehdr,
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002155 savehdr.count, drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
2157 } else {
2158 /*
2159 * Destination has holes, so we make a temporary copy
2160 * of the leaf and add them both to that.
2161 */
Dave Chinner517c2222013-04-24 18:58:55 +10002162 struct xfs_attr_leafblock *tmp_leaf;
2163 struct xfs_attr3_icleaf_hdr tmphdr;
2164
Dave Chinnerb2a21e72014-06-06 15:22:04 +10002165 tmp_leaf = kmem_zalloc(state->args->geo->blksize, KM_SLEEP);
Dave Chinner517c2222013-04-24 18:58:55 +10002166
Dave Chinner8517de22013-05-21 18:02:05 +10002167 /*
2168 * Copy the header into the temp leaf so that all the stuff
2169 * not in the incore header is present and gets copied back in
2170 * once we've moved all the entries.
2171 */
2172 memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf));
2173
2174 memset(&tmphdr, 0, sizeof(tmphdr));
Dave Chinner517c2222013-04-24 18:58:55 +10002175 tmphdr.magic = savehdr.magic;
2176 tmphdr.forw = savehdr.forw;
2177 tmphdr.back = savehdr.back;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10002178 tmphdr.firstused = state->args->geo->blksize;
Dave Chinner8517de22013-05-21 18:02:05 +10002179
2180 /* write the header to the temp buffer to initialise it */
Brian Foster2f661242015-04-13 11:26:02 +10002181 xfs_attr3_leaf_hdr_to_disk(state->args->geo, tmp_leaf, &tmphdr);
Dave Chinner8517de22013-05-21 18:02:05 +10002182
Dave Chinner517c2222013-04-24 18:58:55 +10002183 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2184 drop_blk->bp, &drophdr)) {
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002185 xfs_attr3_leaf_moveents(state->args,
2186 drop_leaf, &drophdr, 0,
Dave Chinner517c2222013-04-24 18:58:55 +10002187 tmp_leaf, &tmphdr, 0,
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002188 drophdr.count);
2189 xfs_attr3_leaf_moveents(state->args,
2190 save_leaf, &savehdr, 0,
Dave Chinner517c2222013-04-24 18:58:55 +10002191 tmp_leaf, &tmphdr, tmphdr.count,
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002192 savehdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 } else {
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002194 xfs_attr3_leaf_moveents(state->args,
2195 save_leaf, &savehdr, 0,
Dave Chinner517c2222013-04-24 18:58:55 +10002196 tmp_leaf, &tmphdr, 0,
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002197 savehdr.count);
2198 xfs_attr3_leaf_moveents(state->args,
2199 drop_leaf, &drophdr, 0,
Dave Chinner517c2222013-04-24 18:58:55 +10002200 tmp_leaf, &tmphdr, tmphdr.count,
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002201 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
Dave Chinnerb2a21e72014-06-06 15:22:04 +10002203 memcpy(save_leaf, tmp_leaf, state->args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10002204 savehdr = tmphdr; /* struct copy */
2205 kmem_free(tmp_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
2207
Brian Foster2f661242015-04-13 11:26:02 +10002208 xfs_attr3_leaf_hdr_to_disk(state->args->geo, save_leaf, &savehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002209 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
Dave Chinnerb2a21e72014-06-06 15:22:04 +10002210 state->args->geo->blksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 /*
2213 * Copy out last hashval in each block for B-tree code.
2214 */
Dave Chinner517c2222013-04-24 18:58:55 +10002215 entry = xfs_attr3_leaf_entryp(save_leaf);
2216 save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
2219/*========================================================================
2220 * Routines used for finding things in the Btree.
2221 *========================================================================*/
2222
2223/*
2224 * Look up a name in a leaf attribute list structure.
2225 * This is the internal routine, it uses the caller's buffer.
2226 *
2227 * Note that duplicate keys are allowed, but only check within the
2228 * current leaf node. The Btree code must check in adjacent leaf nodes.
2229 *
2230 * Return in args->index the index into the entry[] array of either
2231 * the found entry, or where the entry should have been (insert before
2232 * that entry).
2233 *
2234 * Don't change the args->value unless we find the attribute.
2235 */
2236int
Dave Chinner517c2222013-04-24 18:58:55 +10002237xfs_attr3_leaf_lookup_int(
2238 struct xfs_buf *bp,
2239 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
Dave Chinner517c2222013-04-24 18:58:55 +10002241 struct xfs_attr_leafblock *leaf;
2242 struct xfs_attr3_icleaf_hdr ichdr;
2243 struct xfs_attr_leaf_entry *entry;
2244 struct xfs_attr_leaf_entry *entries;
2245 struct xfs_attr_leaf_name_local *name_loc;
2246 struct xfs_attr_leaf_name_remote *name_rmt;
2247 xfs_dahash_t hashval;
2248 int probe;
2249 int span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Dave Chinner5a5881c2012-03-22 05:15:13 +00002251 trace_xfs_attr_leaf_lookup(args);
2252
Dave Chinner1d9025e2012-06-22 18:50:14 +10002253 leaf = bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10002254 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10002255 entries = xfs_attr3_leaf_entryp(leaf);
Darrick J. Wong8ba92d42018-01-08 10:51:07 -08002256 if (ichdr.count >= args->geo->blksize / 8)
2257 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259 /*
2260 * Binary search. (note: small blocks will skip this loop)
2261 */
2262 hashval = args->hashval;
Dave Chinner517c2222013-04-24 18:58:55 +10002263 probe = span = ichdr.count / 2;
2264 for (entry = &entries[probe]; span > 4; entry = &entries[probe]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 span /= 2;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002266 if (be32_to_cpu(entry->hashval) < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 probe += span;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002268 else if (be32_to_cpu(entry->hashval) > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 probe -= span;
2270 else
2271 break;
2272 }
Darrick J. Wong8ba92d42018-01-08 10:51:07 -08002273 if (!(probe >= 0 && (!ichdr.count || probe < ichdr.count)))
2274 return -EFSCORRUPTED;
2275 if (!(span <= 4 || be32_to_cpu(entry->hashval) == hashval))
2276 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 /*
2279 * Since we may have duplicate hashval's, find the first matching
2280 * hashval in the leaf.
2281 */
Dave Chinner517c2222013-04-24 18:58:55 +10002282 while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 entry--;
2284 probe--;
2285 }
Dave Chinner517c2222013-04-24 18:58:55 +10002286 while (probe < ichdr.count &&
2287 be32_to_cpu(entry->hashval) < hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 entry++;
2289 probe++;
2290 }
Dave Chinner517c2222013-04-24 18:58:55 +10002291 if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 args->index = probe;
Dave Chinner24513372014-06-25 14:58:08 +10002293 return -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
2295
2296 /*
2297 * Duplicate keys may be present, so search all of them for a match.
2298 */
Dave Chinner517c2222013-04-24 18:58:55 +10002299 for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 entry++, probe++) {
2301/*
2302 * GROT: Add code to remove incomplete entries.
2303 */
2304 /*
2305 * If we are looking for INCOMPLETE entries, show only those.
2306 * If we are looking for complete entries, show only those.
2307 */
2308 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2309 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2310 continue;
2311 }
2312 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002313 name_loc = xfs_attr3_leaf_name_local(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (name_loc->namelen != args->namelen)
2315 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10002316 if (memcmp(args->name, name_loc->nameval,
2317 args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002319 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 continue;
2321 args->index = probe;
Dave Chinner24513372014-06-25 14:58:08 +10002322 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002324 name_rmt = xfs_attr3_leaf_name_remote(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (name_rmt->namelen != args->namelen)
2326 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10002327 if (memcmp(args->name, name_rmt->name,
2328 args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002330 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 continue;
2332 args->index = probe;
Dave Chinner8275cdd2014-05-06 07:37:31 +10002333 args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002334 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Dave Chinnerad1858d2013-05-21 18:02:08 +10002335 args->rmtblkcnt = xfs_attr3_rmt_blocks(
2336 args->dp->i_mount,
Dave Chinner8275cdd2014-05-06 07:37:31 +10002337 args->rmtvaluelen);
Dave Chinner24513372014-06-25 14:58:08 +10002338 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
2340 }
2341 args->index = probe;
Dave Chinner24513372014-06-25 14:58:08 +10002342 return -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
2345/*
2346 * Get the value associated with an attribute name from a leaf attribute
2347 * list structure.
2348 */
2349int
Dave Chinner517c2222013-04-24 18:58:55 +10002350xfs_attr3_leaf_getvalue(
2351 struct xfs_buf *bp,
2352 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
Dave Chinner517c2222013-04-24 18:58:55 +10002354 struct xfs_attr_leafblock *leaf;
2355 struct xfs_attr3_icleaf_hdr ichdr;
2356 struct xfs_attr_leaf_entry *entry;
2357 struct xfs_attr_leaf_name_local *name_loc;
2358 struct xfs_attr_leaf_name_remote *name_rmt;
2359 int valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Dave Chinner1d9025e2012-06-22 18:50:14 +10002361 leaf = bp->b_addr;
Brian Foster2f661242015-04-13 11:26:02 +10002362 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002363 ASSERT(ichdr.count < args->geo->blksize / 8);
Dave Chinner517c2222013-04-24 18:58:55 +10002364 ASSERT(args->index < ichdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Dave Chinner517c2222013-04-24 18:58:55 +10002366 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002368 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 ASSERT(name_loc->namelen == args->namelen);
2370 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
Nathan Scott053b5752006-03-17 17:29:09 +11002371 valuelen = be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if (args->flags & ATTR_KERNOVAL) {
2373 args->valuelen = valuelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376 if (args->valuelen < valuelen) {
2377 args->valuelen = valuelen;
Dave Chinner24513372014-06-25 14:58:08 +10002378 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380 args->valuelen = valuelen;
2381 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2382 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002383 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 ASSERT(name_rmt->namelen == args->namelen);
2385 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
Dave Chinner8275cdd2014-05-06 07:37:31 +10002386 args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002387 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Dave Chinnerad1858d2013-05-21 18:02:08 +10002388 args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount,
Dave Chinner8275cdd2014-05-06 07:37:31 +10002389 args->rmtvaluelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (args->flags & ATTR_KERNOVAL) {
Dave Chinner8275cdd2014-05-06 07:37:31 +10002391 args->valuelen = args->rmtvaluelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
Dave Chinner8275cdd2014-05-06 07:37:31 +10002394 if (args->valuelen < args->rmtvaluelen) {
2395 args->valuelen = args->rmtvaluelen;
Dave Chinner24513372014-06-25 14:58:08 +10002396 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
Dave Chinner8275cdd2014-05-06 07:37:31 +10002398 args->valuelen = args->rmtvaluelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
Dave Chinner517c2222013-04-24 18:58:55 +10002400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401}
2402
2403/*========================================================================
2404 * Utility routines.
2405 *========================================================================*/
2406
2407/*
2408 * Move the indicated entries from one leaf to another.
2409 * NOTE: this routine modifies both source and destination leaves.
2410 */
2411/*ARGSUSED*/
2412STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10002413xfs_attr3_leaf_moveents(
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002414 struct xfs_da_args *args,
Dave Chinner517c2222013-04-24 18:58:55 +10002415 struct xfs_attr_leafblock *leaf_s,
2416 struct xfs_attr3_icleaf_hdr *ichdr_s,
2417 int start_s,
2418 struct xfs_attr_leafblock *leaf_d,
2419 struct xfs_attr3_icleaf_hdr *ichdr_d,
2420 int start_d,
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002421 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Dave Chinner517c2222013-04-24 18:58:55 +10002423 struct xfs_attr_leaf_entry *entry_s;
2424 struct xfs_attr_leaf_entry *entry_d;
2425 int desti;
2426 int tmp;
2427 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
2429 /*
2430 * Check for nothing to do.
2431 */
2432 if (count == 0)
2433 return;
2434
2435 /*
2436 * Set up environment.
2437 */
Dave Chinner517c2222013-04-24 18:58:55 +10002438 ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC ||
2439 ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC);
2440 ASSERT(ichdr_s->magic == ichdr_d->magic);
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002441 ASSERT(ichdr_s->count > 0 && ichdr_s->count < args->geo->blksize / 8);
Dave Chinner517c2222013-04-24 18:58:55 +10002442 ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s))
2443 + xfs_attr3_leaf_hdr_size(leaf_s));
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002444 ASSERT(ichdr_d->count < args->geo->blksize / 8);
Dave Chinner517c2222013-04-24 18:58:55 +10002445 ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d))
2446 + xfs_attr3_leaf_hdr_size(leaf_d));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Dave Chinner517c2222013-04-24 18:58:55 +10002448 ASSERT(start_s < ichdr_s->count);
2449 ASSERT(start_d <= ichdr_d->count);
2450 ASSERT(count <= ichdr_s->count);
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
2453 /*
2454 * Move the entries in the destination leaf up to make a hole?
2455 */
Dave Chinner517c2222013-04-24 18:58:55 +10002456 if (start_d < ichdr_d->count) {
2457 tmp = ichdr_d->count - start_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 tmp *= sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002459 entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2460 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count];
2461 memmove(entry_d, entry_s, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
2463
2464 /*
2465 * Copy all entry's in the same (sorted) order,
2466 * but allocate attribute info packed and in sequence.
2467 */
Dave Chinner517c2222013-04-24 18:58:55 +10002468 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2469 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 desti = start_d;
2471 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
Dave Chinner517c2222013-04-24 18:58:55 +10002472 ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2474#ifdef GROT
2475 /*
2476 * Code to drop INCOMPLETE entries. Difficult to use as we
2477 * may also need to change the insertion index. Code turned
2478 * off for 6.2, should be revisited later.
2479 */
2480 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
Dave Chinner517c2222013-04-24 18:58:55 +10002481 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2482 ichdr_s->usedbytes -= tmp;
2483 ichdr_s->count -= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 entry_d--; /* to compensate for ++ in loop hdr */
2485 desti--;
2486 if ((start_s + i) < offset)
2487 result++; /* insertion index adjustment */
2488 } else {
2489#endif /* GROT */
Dave Chinner517c2222013-04-24 18:58:55 +10002490 ichdr_d->firstused -= tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 /* both on-disk, don't endian flip twice */
2492 entry_d->hashval = entry_s->hashval;
Dave Chinner517c2222013-04-24 18:58:55 +10002493 entry_d->nameidx = cpu_to_be16(ichdr_d->firstused);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 entry_d->flags = entry_s->flags;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002495 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002496 <= args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10002497 memmove(xfs_attr3_leaf_name(leaf_d, desti),
2498 xfs_attr3_leaf_name(leaf_s, start_s + i), tmp);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002499 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002500 <= args->geo->blksize);
Dave Chinner517c2222013-04-24 18:58:55 +10002501 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2502 ichdr_s->usedbytes -= tmp;
2503 ichdr_d->usedbytes += tmp;
2504 ichdr_s->count -= 1;
2505 ichdr_d->count += 1;
2506 tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t)
2507 + xfs_attr3_leaf_hdr_size(leaf_d);
2508 ASSERT(ichdr_d->firstused >= tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509#ifdef GROT
2510 }
2511#endif /* GROT */
2512 }
2513
2514 /*
2515 * Zero out the entries we just copied.
2516 */
Dave Chinner517c2222013-04-24 18:58:55 +10002517 if (start_s == ichdr_s->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002519 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 ASSERT(((char *)entry_s + tmp) <=
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002521 ((char *)leaf_s + args->geo->blksize));
Dave Chinner517c2222013-04-24 18:58:55 +10002522 memset(entry_s, 0, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 } else {
2524 /*
2525 * Move the remaining entries down to fill the hole,
2526 * then zero the entries at the top.
2527 */
Dave Chinner517c2222013-04-24 18:58:55 +10002528 tmp = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t);
2529 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count];
2530 entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2531 memmove(entry_d, entry_s, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002534 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 ASSERT(((char *)entry_s + tmp) <=
Dave Chinnerc2c4c472014-06-06 15:21:45 +10002536 ((char *)leaf_s + args->geo->blksize));
Dave Chinner517c2222013-04-24 18:58:55 +10002537 memset(entry_s, 0, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
2539
2540 /*
2541 * Fill in the freemap information
2542 */
Dave Chinner517c2222013-04-24 18:58:55 +10002543 ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d);
2544 ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t);
2545 ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base;
2546 ichdr_d->freemap[1].base = 0;
2547 ichdr_d->freemap[2].base = 0;
2548 ichdr_d->freemap[1].size = 0;
2549 ichdr_d->freemap[2].size = 0;
2550 ichdr_s->holes = 1; /* leaf may not be compact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
2553/*
2554 * Pick up the last hashvalue from a leaf block.
2555 */
2556xfs_dahash_t
Dave Chinner1d9025e2012-06-22 18:50:14 +10002557xfs_attr_leaf_lasthash(
2558 struct xfs_buf *bp,
2559 int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
Dave Chinner517c2222013-04-24 18:58:55 +10002561 struct xfs_attr3_icleaf_hdr ichdr;
2562 struct xfs_attr_leaf_entry *entries;
Brian Foster2f661242015-04-13 11:26:02 +10002563 struct xfs_mount *mp = bp->b_target->bt_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Brian Foster2f661242015-04-13 11:26:02 +10002565 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, bp->b_addr);
Dave Chinner517c2222013-04-24 18:58:55 +10002566 entries = xfs_attr3_leaf_entryp(bp->b_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 if (count)
Dave Chinner517c2222013-04-24 18:58:55 +10002568 *count = ichdr.count;
2569 if (!ichdr.count)
2570 return 0;
2571 return be32_to_cpu(entries[ichdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
2573
2574/*
2575 * Calculate the number of bytes used to store the indicated attribute
2576 * (whether local or remote only calculate bytes in this block).
2577 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002578STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2580{
Dave Chinner517c2222013-04-24 18:58:55 +10002581 struct xfs_attr_leaf_entry *entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 xfs_attr_leaf_name_local_t *name_loc;
2583 xfs_attr_leaf_name_remote_t *name_rmt;
2584 int size;
2585
Dave Chinner517c2222013-04-24 18:58:55 +10002586 entries = xfs_attr3_leaf_entryp(leaf);
2587 if (entries[index].flags & XFS_ATTR_LOCAL) {
2588 name_loc = xfs_attr3_leaf_name_local(leaf, index);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002589 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
Nathan Scott053b5752006-03-17 17:29:09 +11002590 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002592 name_rmt = xfs_attr3_leaf_name_remote(leaf, index);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002593 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 }
Dave Chinner517c2222013-04-24 18:58:55 +10002595 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
2598/*
2599 * Calculate the number of bytes that would be required to store the new
2600 * attribute (whether local or remote only calculate bytes in this block).
2601 * This routine decides as a side effect whether the attribute will be
2602 * a "local" or a "remote" attribute.
2603 */
2604int
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10002605xfs_attr_leaf_newentsize(
2606 struct xfs_da_args *args,
2607 int *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10002609 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10002611 size = xfs_attr_leaf_entsize_local(args->namelen, args->valuelen);
2612 if (size < xfs_attr_leaf_entsize_local_max(args->geo->blksize)) {
2613 if (local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 *local = 1;
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10002615 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
Dave Chinnerc59f0ad2014-06-06 15:21:27 +10002617 if (local)
2618 *local = 0;
2619 return xfs_attr_leaf_entsize_remote(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620}
2621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623/*========================================================================
2624 * Manage the INCOMPLETE flag in a leaf entry
2625 *========================================================================*/
2626
2627/*
2628 * Clear the INCOMPLETE flag on an entry in a leaf block.
2629 */
2630int
Dave Chinner517c2222013-04-24 18:58:55 +10002631xfs_attr3_leaf_clearflag(
2632 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
Dave Chinner517c2222013-04-24 18:58:55 +10002634 struct xfs_attr_leafblock *leaf;
2635 struct xfs_attr_leaf_entry *entry;
2636 struct xfs_attr_leaf_name_remote *name_rmt;
2637 struct xfs_buf *bp;
2638 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002640 struct xfs_attr3_icleaf_hdr ichdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 xfs_attr_leaf_name_local_t *name_loc;
2642 int namelen;
2643 char *name;
2644#endif /* DEBUG */
2645
Dave Chinner5a5881c2012-03-22 05:15:13 +00002646 trace_xfs_attr_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 /*
2648 * Set up the operation.
2649 */
Dave Chinner517c2222013-04-24 18:58:55 +10002650 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +11002651 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10002652 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Dave Chinner1d9025e2012-06-22 18:50:14 +10002654 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002655 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2657
2658#ifdef DEBUG
Brian Foster2f661242015-04-13 11:26:02 +10002659 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10002660 ASSERT(args->index < ichdr.count);
2661 ASSERT(args->index >= 0);
2662
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002664 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 namelen = name_loc->namelen;
2666 name = (char *)name_loc->nameval;
2667 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002668 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 namelen = name_rmt->namelen;
2670 name = (char *)name_rmt->name;
2671 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002672 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 ASSERT(namelen == args->namelen);
2674 ASSERT(memcmp(name, args->name, namelen) == 0);
2675#endif /* DEBUG */
2676
2677 entry->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002678 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2680
2681 if (args->rmtblkno) {
2682 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002683 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002684 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
Dave Chinner8275cdd2014-05-06 07:37:31 +10002685 name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002686 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
2690 /*
2691 * Commit the flag value change and start the next trans in series.
2692 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07002693 return xfs_trans_roll_inode(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
2696/*
2697 * Set the INCOMPLETE flag on an entry in a leaf block.
2698 */
2699int
Dave Chinner517c2222013-04-24 18:58:55 +10002700xfs_attr3_leaf_setflag(
2701 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
Dave Chinner517c2222013-04-24 18:58:55 +10002703 struct xfs_attr_leafblock *leaf;
2704 struct xfs_attr_leaf_entry *entry;
2705 struct xfs_attr_leaf_name_remote *name_rmt;
2706 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 int error;
Dave Chinner517c2222013-04-24 18:58:55 +10002708#ifdef DEBUG
2709 struct xfs_attr3_icleaf_hdr ichdr;
2710#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Dave Chinner5a5881c2012-03-22 05:15:13 +00002712 trace_xfs_attr_leaf_setflag(args);
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 /*
2715 * Set up the operation.
2716 */
Dave Chinner517c2222013-04-24 18:58:55 +10002717 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +11002718 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10002719 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Dave Chinner1d9025e2012-06-22 18:50:14 +10002721 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002722#ifdef DEBUG
Brian Foster2f661242015-04-13 11:26:02 +10002723 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
Dave Chinner517c2222013-04-24 18:58:55 +10002724 ASSERT(args->index < ichdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 ASSERT(args->index >= 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002726#endif
2727 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2730 entry->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002731 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2733 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002734 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 name_rmt->valueblk = 0;
2736 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002737 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741 /*
2742 * Commit the flag value change and start the next trans in series.
2743 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07002744 return xfs_trans_roll_inode(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
2747/*
2748 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2749 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2750 * entry given by args->blkno2/index2.
2751 *
2752 * Note that they could be in different blocks, or in the same block.
2753 */
2754int
Dave Chinner517c2222013-04-24 18:58:55 +10002755xfs_attr3_leaf_flipflags(
2756 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
Dave Chinner517c2222013-04-24 18:58:55 +10002758 struct xfs_attr_leafblock *leaf1;
2759 struct xfs_attr_leafblock *leaf2;
2760 struct xfs_attr_leaf_entry *entry1;
2761 struct xfs_attr_leaf_entry *entry2;
2762 struct xfs_attr_leaf_name_remote *name_rmt;
2763 struct xfs_buf *bp1;
2764 struct xfs_buf *bp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 int error;
2766#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002767 struct xfs_attr3_icleaf_hdr ichdr1;
2768 struct xfs_attr3_icleaf_hdr ichdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 xfs_attr_leaf_name_local_t *name_loc;
2770 int namelen1, namelen2;
2771 char *name1, *name2;
2772#endif /* DEBUG */
2773
Dave Chinner5a5881c2012-03-22 05:15:13 +00002774 trace_xfs_attr_leaf_flipflags(args);
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 /*
2777 * Read the block containing the "old" attr
2778 */
Dave Chinner517c2222013-04-24 18:58:55 +10002779 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1);
Dave Chinnerad14c332012-11-12 22:54:16 +11002780 if (error)
2781 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 /*
2784 * Read the block containing the "new" attr, if it is different
2785 */
2786 if (args->blkno2 != args->blkno) {
Dave Chinner517c2222013-04-24 18:58:55 +10002787 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno2,
Dave Chinnerad14c332012-11-12 22:54:16 +11002788 -1, &bp2);
2789 if (error)
2790 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 } else {
2792 bp2 = bp1;
2793 }
2794
Dave Chinner1d9025e2012-06-22 18:50:14 +10002795 leaf1 = bp1->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002796 entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Dave Chinner1d9025e2012-06-22 18:50:14 +10002798 leaf2 = bp2->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002799 entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801#ifdef DEBUG
Brian Foster2f661242015-04-13 11:26:02 +10002802 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr1, leaf1);
Dave Chinner517c2222013-04-24 18:58:55 +10002803 ASSERT(args->index < ichdr1.count);
2804 ASSERT(args->index >= 0);
2805
Brian Foster2f661242015-04-13 11:26:02 +10002806 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr2, leaf2);
Dave Chinner517c2222013-04-24 18:58:55 +10002807 ASSERT(args->index2 < ichdr2.count);
2808 ASSERT(args->index2 >= 0);
2809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (entry1->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002811 name_loc = xfs_attr3_leaf_name_local(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 namelen1 = name_loc->namelen;
2813 name1 = (char *)name_loc->nameval;
2814 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002815 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 namelen1 = name_rmt->namelen;
2817 name1 = (char *)name_rmt->name;
2818 }
2819 if (entry2->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002820 name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 namelen2 = name_loc->namelen;
2822 name2 = (char *)name_loc->nameval;
2823 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002824 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 namelen2 = name_rmt->namelen;
2826 name2 = (char *)name_rmt->name;
2827 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002828 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 ASSERT(namelen1 == namelen2);
2830 ASSERT(memcmp(name1, name2, namelen1) == 0);
2831#endif /* DEBUG */
2832
2833 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2834 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2835
2836 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002837 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2839 if (args->rmtblkno) {
2840 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002841 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002842 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
Dave Chinner8275cdd2014-05-06 07:37:31 +10002843 name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002844 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2846 }
2847
2848 entry2->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002849 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2851 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002852 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 name_rmt->valueblk = 0;
2854 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002855 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859 /*
2860 * Commit the flag value change and start the next trans in series.
2861 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07002862 error = xfs_trans_roll_inode(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Dave Chinner517c2222013-04-24 18:58:55 +10002864 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}