]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 | 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
517c2220 | 3 | * Copyright (c) 2013 Red Hat, Inc. |
7b718769 | 4 | * All Rights Reserved. |
1da177e4 | 5 | * |
7b718769 NS |
6 | * This program is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
8 | * published by the Free Software Foundation. |
9 | * | |
7b718769 NS |
10 | * This program is distributed in the hope that it would be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
1da177e4 | 14 | * |
7b718769 NS |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program; if not, write the Free Software Foundation, | |
17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 18 | */ |
1da177e4 | 19 | #include "xfs.h" |
a844f451 | 20 | #include "xfs_fs.h" |
1da177e4 | 21 | #include "xfs_types.h" |
a844f451 | 22 | #include "xfs_bit.h" |
1da177e4 LT |
23 | #include "xfs_log.h" |
24 | #include "xfs_trans.h" | |
25 | #include "xfs_sb.h" | |
26 | #include "xfs_ag.h" | |
1da177e4 | 27 | #include "xfs_mount.h" |
a844f451 | 28 | #include "xfs_da_btree.h" |
1da177e4 | 29 | #include "xfs_bmap_btree.h" |
a844f451 | 30 | #include "xfs_alloc_btree.h" |
1da177e4 LT |
31 | #include "xfs_ialloc_btree.h" |
32 | #include "xfs_alloc.h" | |
33 | #include "xfs_btree.h" | |
a844f451 | 34 | #include "xfs_attr_sf.h" |
95920cd6 | 35 | #include "xfs_attr_remote.h" |
1da177e4 | 36 | #include "xfs_dinode.h" |
1da177e4 | 37 | #include "xfs_inode.h" |
a844f451 | 38 | #include "xfs_inode_item.h" |
1da177e4 | 39 | #include "xfs_bmap.h" |
1da177e4 LT |
40 | #include "xfs_attr.h" |
41 | #include "xfs_attr_leaf.h" | |
42 | #include "xfs_error.h" | |
0b1b213f | 43 | #include "xfs_trace.h" |
517c2220 DC |
44 | #include "xfs_buf_item.h" |
45 | #include "xfs_cksum.h" | |
46 | ||
1da177e4 LT |
47 | |
48 | /* | |
49 | * xfs_attr_leaf.c | |
50 | * | |
51 | * Routines to implement leaf blocks of attributes as Btrees of hashed names. | |
52 | */ | |
53 | ||
54 | /*======================================================================== | |
55 | * Function prototypes for the kernel. | |
56 | *========================================================================*/ | |
57 | ||
58 | /* | |
59 | * Routines used for growing the Btree. | |
60 | */ | |
517c2220 DC |
61 | STATIC int xfs_attr3_leaf_create(struct xfs_da_args *args, |
62 | xfs_dablk_t which_block, struct xfs_buf **bpp); | |
63 | STATIC int xfs_attr3_leaf_add_work(struct xfs_buf *leaf_buffer, | |
64 | struct xfs_attr3_icleaf_hdr *ichdr, | |
65 | struct xfs_da_args *args, int freemap_index); | |
66 | STATIC void xfs_attr3_leaf_compact(struct xfs_da_args *args, | |
67 | struct xfs_attr3_icleaf_hdr *ichdr, | |
68 | struct xfs_buf *leaf_buffer); | |
69 | STATIC void xfs_attr3_leaf_rebalance(xfs_da_state_t *state, | |
1da177e4 LT |
70 | xfs_da_state_blk_t *blk1, |
71 | xfs_da_state_blk_t *blk2); | |
517c2220 DC |
72 | STATIC int xfs_attr3_leaf_figure_balance(xfs_da_state_t *state, |
73 | xfs_da_state_blk_t *leaf_blk_1, | |
74 | struct xfs_attr3_icleaf_hdr *ichdr1, | |
75 | xfs_da_state_blk_t *leaf_blk_2, | |
76 | struct xfs_attr3_icleaf_hdr *ichdr2, | |
77 | int *number_entries_in_blk1, | |
78 | int *number_usedbytes_in_blk1); | |
1da177e4 | 79 | |
ba0f32d4 CH |
80 | /* |
81 | * Routines used for shrinking the Btree. | |
82 | */ | |
517c2220 | 83 | STATIC int xfs_attr3_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp, |
1d9025e5 | 84 | struct xfs_buf *bp, int level); |
517c2220 | 85 | STATIC int xfs_attr3_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp, |
1d9025e5 | 86 | struct xfs_buf *bp); |
517c2220 | 87 | STATIC int xfs_attr3_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp, |
ba0f32d4 CH |
88 | xfs_dablk_t blkno, int blkcnt); |
89 | ||
1da177e4 LT |
90 | /* |
91 | * Utility routines. | |
92 | */ | |
517c2220 DC |
93 | STATIC void xfs_attr3_leaf_moveents(struct xfs_attr_leafblock *src_leaf, |
94 | struct xfs_attr3_icleaf_hdr *src_ichdr, int src_start, | |
95 | struct xfs_attr_leafblock *dst_leaf, | |
96 | struct xfs_attr3_icleaf_hdr *dst_ichdr, int dst_start, | |
97 | int move_count, struct xfs_mount *mp); | |
ba0f32d4 | 98 | STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index); |
726801ba | 99 | |
517c2220 DC |
100 | void |
101 | xfs_attr3_leaf_hdr_from_disk( | |
102 | struct xfs_attr3_icleaf_hdr *to, | |
103 | struct xfs_attr_leafblock *from) | |
104 | { | |
105 | int i; | |
106 | ||
107 | ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) || | |
108 | from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)); | |
109 | ||
110 | if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) { | |
111 | struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)from; | |
112 | ||
113 | to->forw = be32_to_cpu(hdr3->info.hdr.forw); | |
114 | to->back = be32_to_cpu(hdr3->info.hdr.back); | |
115 | to->magic = be16_to_cpu(hdr3->info.hdr.magic); | |
116 | to->count = be16_to_cpu(hdr3->count); | |
117 | to->usedbytes = be16_to_cpu(hdr3->usedbytes); | |
118 | to->firstused = be16_to_cpu(hdr3->firstused); | |
119 | to->holes = hdr3->holes; | |
120 | ||
121 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { | |
122 | to->freemap[i].base = be16_to_cpu(hdr3->freemap[i].base); | |
123 | to->freemap[i].size = be16_to_cpu(hdr3->freemap[i].size); | |
124 | } | |
125 | return; | |
126 | } | |
127 | to->forw = be32_to_cpu(from->hdr.info.forw); | |
128 | to->back = be32_to_cpu(from->hdr.info.back); | |
129 | to->magic = be16_to_cpu(from->hdr.info.magic); | |
130 | to->count = be16_to_cpu(from->hdr.count); | |
131 | to->usedbytes = be16_to_cpu(from->hdr.usedbytes); | |
132 | to->firstused = be16_to_cpu(from->hdr.firstused); | |
133 | to->holes = from->hdr.holes; | |
134 | ||
135 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { | |
136 | to->freemap[i].base = be16_to_cpu(from->hdr.freemap[i].base); | |
137 | to->freemap[i].size = be16_to_cpu(from->hdr.freemap[i].size); | |
138 | } | |
139 | } | |
140 | ||
141 | void | |
142 | xfs_attr3_leaf_hdr_to_disk( | |
143 | struct xfs_attr_leafblock *to, | |
144 | struct xfs_attr3_icleaf_hdr *from) | |
145 | { | |
146 | int i; | |
147 | ||
148 | ASSERT(from->magic == XFS_ATTR_LEAF_MAGIC || | |
149 | from->magic == XFS_ATTR3_LEAF_MAGIC); | |
150 | ||
151 | if (from->magic == XFS_ATTR3_LEAF_MAGIC) { | |
152 | struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)to; | |
153 | ||
154 | hdr3->info.hdr.forw = cpu_to_be32(from->forw); | |
155 | hdr3->info.hdr.back = cpu_to_be32(from->back); | |
156 | hdr3->info.hdr.magic = cpu_to_be16(from->magic); | |
157 | hdr3->count = cpu_to_be16(from->count); | |
158 | hdr3->usedbytes = cpu_to_be16(from->usedbytes); | |
159 | hdr3->firstused = cpu_to_be16(from->firstused); | |
160 | hdr3->holes = from->holes; | |
161 | hdr3->pad1 = 0; | |
162 | ||
163 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { | |
164 | hdr3->freemap[i].base = cpu_to_be16(from->freemap[i].base); | |
165 | hdr3->freemap[i].size = cpu_to_be16(from->freemap[i].size); | |
166 | } | |
167 | return; | |
168 | } | |
169 | to->hdr.info.forw = cpu_to_be32(from->forw); | |
170 | to->hdr.info.back = cpu_to_be32(from->back); | |
171 | to->hdr.info.magic = cpu_to_be16(from->magic); | |
172 | to->hdr.count = cpu_to_be16(from->count); | |
173 | to->hdr.usedbytes = cpu_to_be16(from->usedbytes); | |
174 | to->hdr.firstused = cpu_to_be16(from->firstused); | |
175 | to->hdr.holes = from->holes; | |
176 | to->hdr.pad1 = 0; | |
177 | ||
178 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { | |
179 | to->hdr.freemap[i].base = cpu_to_be16(from->freemap[i].base); | |
180 | to->hdr.freemap[i].size = cpu_to_be16(from->freemap[i].size); | |
181 | } | |
182 | } | |
183 | ||
184 | static bool | |
185 | xfs_attr3_leaf_verify( | |
ad14c33a DC |
186 | struct xfs_buf *bp) |
187 | { | |
188 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
517c2220 DC |
189 | struct xfs_attr_leafblock *leaf = bp->b_addr; |
190 | struct xfs_attr3_icleaf_hdr ichdr; | |
ad14c33a | 191 | |
517c2220 DC |
192 | xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf); |
193 | ||
194 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
195 | struct xfs_da3_node_hdr *hdr3 = bp->b_addr; | |
196 | ||
197 | if (ichdr.magic != XFS_ATTR3_LEAF_MAGIC) | |
198 | return false; | |
199 | ||
200 | if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_uuid)) | |
201 | return false; | |
202 | if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn) | |
203 | return false; | |
204 | } else { | |
205 | if (ichdr.magic != XFS_ATTR_LEAF_MAGIC) | |
206 | return false; | |
ad14c33a | 207 | } |
517c2220 DC |
208 | if (ichdr.count == 0) |
209 | return false; | |
210 | ||
211 | /* XXX: need to range check rest of attr header values */ | |
212 | /* XXX: hash order check? */ | |
213 | ||
214 | return true; | |
612cfbfe DC |
215 | } |
216 | ||
217 | static void | |
517c2220 | 218 | xfs_attr3_leaf_write_verify( |
612cfbfe DC |
219 | struct xfs_buf *bp) |
220 | { | |
517c2220 DC |
221 | struct xfs_mount *mp = bp->b_target->bt_mount; |
222 | struct xfs_buf_log_item *bip = bp->b_fspriv; | |
223 | struct xfs_attr3_leaf_hdr *hdr3 = bp->b_addr; | |
224 | ||
225 | if (!xfs_attr3_leaf_verify(bp)) { | |
226 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); | |
227 | xfs_buf_ioerror(bp, EFSCORRUPTED); | |
228 | return; | |
229 | } | |
230 | ||
231 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
232 | return; | |
233 | ||
234 | if (bip) | |
235 | hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn); | |
236 | ||
237 | xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_ATTR3_LEAF_CRC_OFF); | |
612cfbfe | 238 | } |
ad14c33a | 239 | |
517c2220 DC |
240 | /* |
241 | * leaf/node format detection on trees is sketchy, so a node read can be done on | |
242 | * leaf level blocks when detection identifies the tree as a node format tree | |
243 | * incorrectly. In this case, we need to swap the verifier to match the correct | |
244 | * format of the block being read. | |
245 | */ | |
1813dd64 | 246 | static void |
517c2220 DC |
247 | xfs_attr3_leaf_read_verify( |
248 | struct xfs_buf *bp) | |
612cfbfe | 249 | { |
517c2220 DC |
250 | struct xfs_mount *mp = bp->b_target->bt_mount; |
251 | ||
252 | if ((xfs_sb_version_hascrc(&mp->m_sb) && | |
253 | !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length), | |
254 | XFS_ATTR3_LEAF_CRC_OFF)) || | |
255 | !xfs_attr3_leaf_verify(bp)) { | |
256 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); | |
257 | xfs_buf_ioerror(bp, EFSCORRUPTED); | |
258 | } | |
ad14c33a DC |
259 | } |
260 | ||
517c2220 DC |
261 | const struct xfs_buf_ops xfs_attr3_leaf_buf_ops = { |
262 | .verify_read = xfs_attr3_leaf_read_verify, | |
263 | .verify_write = xfs_attr3_leaf_write_verify, | |
1813dd64 | 264 | }; |
612cfbfe | 265 | |
ad14c33a | 266 | int |
517c2220 | 267 | xfs_attr3_leaf_read( |
ad14c33a DC |
268 | struct xfs_trans *tp, |
269 | struct xfs_inode *dp, | |
270 | xfs_dablk_t bno, | |
271 | xfs_daddr_t mappedbno, | |
272 | struct xfs_buf **bpp) | |
273 | { | |
d75afeb3 DC |
274 | int err; |
275 | ||
276 | err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp, | |
517c2220 | 277 | XFS_ATTR_FORK, &xfs_attr3_leaf_buf_ops); |
d75afeb3 | 278 | if (!err && tp) |
61fe135c | 279 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_ATTR_LEAF_BUF); |
d75afeb3 | 280 | return err; |
ad14c33a DC |
281 | } |
282 | ||
726801ba TS |
283 | /*======================================================================== |
284 | * Namespace helper routines | |
285 | *========================================================================*/ | |
286 | ||
726801ba TS |
287 | /* |
288 | * If namespace bits don't match return 0. | |
289 | * If all match then return 1. | |
290 | */ | |
b8f82a4a | 291 | STATIC int |
726801ba TS |
292 | xfs_attr_namesp_match(int arg_flags, int ondisk_flags) |
293 | { | |
294 | return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags); | |
295 | } | |
296 | ||
1da177e4 LT |
297 | |
298 | /*======================================================================== | |
d8cc890d | 299 | * External routines when attribute fork size < XFS_LITINO(mp). |
1da177e4 LT |
300 | *========================================================================*/ |
301 | ||
302 | /* | |
d8cc890d NS |
303 | * Query whether the requested number of additional bytes of extended |
304 | * attribute space will be able to fit inline. | |
4c393a60 | 305 | * |
d8cc890d NS |
306 | * Returns zero if not, else the di_forkoff fork offset to be used in the |
307 | * literal area for attribute data once the new bytes have been added. | |
308 | * | |
309 | * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value; | |
310 | * special case for dev/uuid inodes, they have fixed size data forks. | |
1da177e4 LT |
311 | */ |
312 | int | |
d8cc890d NS |
313 | xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes) |
314 | { | |
315 | int offset; | |
316 | int minforkoff; /* lower limit on valid forkoff locations */ | |
317 | int maxforkoff; /* upper limit on valid forkoff locations */ | |
4c393a60 | 318 | int dsize; |
d8cc890d NS |
319 | xfs_mount_t *mp = dp->i_mount; |
320 | ||
56cea2d0 CH |
321 | /* rounded down */ |
322 | offset = (XFS_LITINO(mp, dp->i_d.di_version) - bytes) >> 3; | |
d8cc890d NS |
323 | |
324 | switch (dp->i_d.di_format) { | |
325 | case XFS_DINODE_FMT_DEV: | |
326 | minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; | |
327 | return (offset >= minforkoff) ? minforkoff : 0; | |
328 | case XFS_DINODE_FMT_UUID: | |
329 | minforkoff = roundup(sizeof(uuid_t), 8) >> 3; | |
330 | return (offset >= minforkoff) ? minforkoff : 0; | |
331 | } | |
332 | ||
4c393a60 CH |
333 | /* |
334 | * If the requested numbers of bytes is smaller or equal to the | |
335 | * current attribute fork size we can always proceed. | |
336 | * | |
337 | * Note that if_bytes in the data fork might actually be larger than | |
338 | * the current data fork size is due to delalloc extents. In that | |
339 | * case either the extent count will go down when they are converted | |
340 | * to real extents, or the delalloc conversion will take care of the | |
341 | * literal area rebalancing. | |
342 | */ | |
343 | if (bytes <= XFS_IFORK_ASIZE(dp)) | |
344 | return dp->i_d.di_forkoff; | |
345 | ||
346 | /* | |
347 | * For attr2 we can try to move the forkoff if there is space in the | |
348 | * literal area, but for the old format we are done if there is no | |
349 | * space in the fixed attribute fork. | |
350 | */ | |
351 | if (!(mp->m_flags & XFS_MOUNT_ATTR2)) | |
da087bad | 352 | return 0; |
da087bad | 353 | |
e5889e90 | 354 | dsize = dp->i_df.if_bytes; |
4c393a60 | 355 | |
e5889e90 BN |
356 | switch (dp->i_d.di_format) { |
357 | case XFS_DINODE_FMT_EXTENTS: | |
4c393a60 | 358 | /* |
e5889e90 | 359 | * If there is no attr fork and the data fork is extents, |
4c393a60 CH |
360 | * determine if creating the default attr fork will result |
361 | * in the extents form migrating to btree. If so, the | |
362 | * minimum offset only needs to be the space required for | |
e5889e90 | 363 | * the btree root. |
4c393a60 | 364 | */ |
1a5902c5 CH |
365 | if (!dp->i_d.di_forkoff && dp->i_df.if_bytes > |
366 | xfs_default_attroffset(dp)) | |
e5889e90 BN |
367 | dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
368 | break; | |
e5889e90 BN |
369 | case XFS_DINODE_FMT_BTREE: |
370 | /* | |
4c393a60 CH |
371 | * If we have a data btree then keep forkoff if we have one, |
372 | * otherwise we are adding a new attr, so then we set | |
373 | * minforkoff to where the btree root can finish so we have | |
e5889e90 BN |
374 | * plenty of room for attrs |
375 | */ | |
376 | if (dp->i_d.di_forkoff) { | |
4c393a60 | 377 | if (offset < dp->i_d.di_forkoff) |
e5889e90 | 378 | return 0; |
4c393a60 CH |
379 | return dp->i_d.di_forkoff; |
380 | } | |
ee1a47ab | 381 | dsize = XFS_BMAP_BROOT_SPACE(mp, dp->i_df.if_broot); |
e5889e90 BN |
382 | break; |
383 | } | |
4c393a60 CH |
384 | |
385 | /* | |
386 | * A data fork btree root must have space for at least | |
e5889e90 BN |
387 | * MINDBTPTRS key/ptr pairs if the data fork is small or empty. |
388 | */ | |
389 | minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS)); | |
d8cc890d NS |
390 | minforkoff = roundup(minforkoff, 8) >> 3; |
391 | ||
392 | /* attr fork btree root can have at least this many key/ptr pairs */ | |
56cea2d0 CH |
393 | maxforkoff = XFS_LITINO(mp, dp->i_d.di_version) - |
394 | XFS_BMDR_SPACE_CALC(MINABTPTRS); | |
d8cc890d NS |
395 | maxforkoff = maxforkoff >> 3; /* rounded down */ |
396 | ||
d8cc890d NS |
397 | if (offset >= maxforkoff) |
398 | return maxforkoff; | |
4c393a60 CH |
399 | if (offset >= minforkoff) |
400 | return offset; | |
d8cc890d NS |
401 | return 0; |
402 | } | |
403 | ||
404 | /* | |
405 | * Switch on the ATTR2 superblock bit (implies also FEATURES2) | |
406 | */ | |
407 | STATIC void | |
408 | xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp) | |
409 | { | |
13059ff0 | 410 | if ((mp->m_flags & XFS_MOUNT_ATTR2) && |
62118709 | 411 | !(xfs_sb_version_hasattr2(&mp->m_sb))) { |
3685c2a1 | 412 | spin_lock(&mp->m_sb_lock); |
62118709 ES |
413 | if (!xfs_sb_version_hasattr2(&mp->m_sb)) { |
414 | xfs_sb_version_addattr2(&mp->m_sb); | |
3685c2a1 | 415 | spin_unlock(&mp->m_sb_lock); |
d8cc890d NS |
416 | xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2); |
417 | } else | |
3685c2a1 | 418 | spin_unlock(&mp->m_sb_lock); |
d8cc890d NS |
419 | } |
420 | } | |
421 | ||
422 | /* | |
423 | * Create the initial contents of a shortform attribute list. | |
424 | */ | |
425 | void | |
1da177e4 LT |
426 | xfs_attr_shortform_create(xfs_da_args_t *args) |
427 | { | |
428 | xfs_attr_sf_hdr_t *hdr; | |
429 | xfs_inode_t *dp; | |
430 | xfs_ifork_t *ifp; | |
431 | ||
5a5881cd DC |
432 | trace_xfs_attr_sf_create(args); |
433 | ||
1da177e4 LT |
434 | dp = args->dp; |
435 | ASSERT(dp != NULL); | |
436 | ifp = dp->i_afp; | |
437 | ASSERT(ifp != NULL); | |
438 | ASSERT(ifp->if_bytes == 0); | |
439 | if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) { | |
440 | ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */ | |
441 | dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL; | |
442 | ifp->if_flags |= XFS_IFINLINE; | |
443 | } else { | |
444 | ASSERT(ifp->if_flags & XFS_IFINLINE); | |
445 | } | |
446 | xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK); | |
447 | hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data; | |
448 | hdr->count = 0; | |
3b244aa8 | 449 | hdr->totsize = cpu_to_be16(sizeof(*hdr)); |
1da177e4 | 450 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA); |
1da177e4 LT |
451 | } |
452 | ||
453 | /* | |
454 | * Add a name/value pair to the shortform attribute list. | |
455 | * Overflow from the inode has already been checked for. | |
456 | */ | |
d8cc890d NS |
457 | void |
458 | xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff) | |
1da177e4 LT |
459 | { |
460 | xfs_attr_shortform_t *sf; | |
461 | xfs_attr_sf_entry_t *sfe; | |
462 | int i, offset, size; | |
d8cc890d | 463 | xfs_mount_t *mp; |
1da177e4 LT |
464 | xfs_inode_t *dp; |
465 | xfs_ifork_t *ifp; | |
466 | ||
5a5881cd DC |
467 | trace_xfs_attr_sf_add(args); |
468 | ||
1da177e4 | 469 | dp = args->dp; |
d8cc890d NS |
470 | mp = dp->i_mount; |
471 | dp->i_d.di_forkoff = forkoff; | |
d8cc890d | 472 | |
1da177e4 LT |
473 | ifp = dp->i_afp; |
474 | ASSERT(ifp->if_flags & XFS_IFINLINE); | |
475 | sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data; | |
476 | sfe = &sf->list[0]; | |
3b244aa8 | 477 | for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) { |
d8cc890d | 478 | #ifdef DEBUG |
1da177e4 LT |
479 | if (sfe->namelen != args->namelen) |
480 | continue; | |
481 | if (memcmp(args->name, sfe->nameval, args->namelen) != 0) | |
482 | continue; | |
726801ba | 483 | if (!xfs_attr_namesp_match(args->flags, sfe->flags)) |
1da177e4 | 484 | continue; |
d8cc890d NS |
485 | ASSERT(0); |
486 | #endif | |
1da177e4 LT |
487 | } |
488 | ||
489 | offset = (char *)sfe - (char *)sf; | |
490 | size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen); | |
491 | xfs_idata_realloc(dp, size, XFS_ATTR_FORK); | |
492 | sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data; | |
493 | sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset); | |
494 | ||
495 | sfe->namelen = args->namelen; | |
3b244aa8 | 496 | sfe->valuelen = args->valuelen; |
726801ba | 497 | sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags); |
1da177e4 LT |
498 | memcpy(sfe->nameval, args->name, args->namelen); |
499 | memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen); | |
3b244aa8 | 500 | sf->hdr.count++; |
413d57c9 | 501 | be16_add_cpu(&sf->hdr.totsize, size); |
1da177e4 LT |
502 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA); |
503 | ||
d8cc890d | 504 | xfs_sbversion_add_attr2(mp, args->trans); |
1da177e4 LT |
505 | } |
506 | ||
e1486dea CH |
507 | /* |
508 | * After the last attribute is removed revert to original inode format, | |
509 | * making all literal area available to the data fork once more. | |
510 | */ | |
511 | STATIC void | |
512 | xfs_attr_fork_reset( | |
513 | struct xfs_inode *ip, | |
514 | struct xfs_trans *tp) | |
515 | { | |
516 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); | |
517 | ip->i_d.di_forkoff = 0; | |
518 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; | |
519 | ||
520 | ASSERT(ip->i_d.di_anextents == 0); | |
521 | ASSERT(ip->i_afp == NULL); | |
522 | ||
e1486dea CH |
523 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
524 | } | |
525 | ||
1da177e4 | 526 | /* |
d8cc890d | 527 | * Remove an attribute from the shortform attribute list structure. |
1da177e4 LT |
528 | */ |
529 | int | |
530 | xfs_attr_shortform_remove(xfs_da_args_t *args) | |
531 | { | |
532 | xfs_attr_shortform_t *sf; | |
533 | xfs_attr_sf_entry_t *sfe; | |
534 | int base, size=0, end, totsize, i; | |
d8cc890d | 535 | xfs_mount_t *mp; |
1da177e4 LT |
536 | xfs_inode_t *dp; |
537 | ||
5a5881cd DC |
538 | trace_xfs_attr_sf_remove(args); |
539 | ||
1da177e4 | 540 | dp = args->dp; |
d8cc890d | 541 | mp = dp->i_mount; |
1da177e4 LT |
542 | base = sizeof(xfs_attr_sf_hdr_t); |
543 | sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data; | |
544 | sfe = &sf->list[0]; | |
3b244aa8 | 545 | end = sf->hdr.count; |
d8cc890d | 546 | for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), |
1da177e4 LT |
547 | base += size, i++) { |
548 | size = XFS_ATTR_SF_ENTSIZE(sfe); | |
549 | if (sfe->namelen != args->namelen) | |
550 | continue; | |
551 | if (memcmp(sfe->nameval, args->name, args->namelen) != 0) | |
552 | continue; | |
726801ba | 553 | if (!xfs_attr_namesp_match(args->flags, sfe->flags)) |
1da177e4 LT |
554 | continue; |
555 | break; | |
556 | } | |
d8cc890d | 557 | if (i == end) |
1da177e4 LT |
558 | return(XFS_ERROR(ENOATTR)); |
559 | ||
d8cc890d NS |
560 | /* |
561 | * Fix up the attribute fork data, covering the hole | |
562 | */ | |
1da177e4 | 563 | end = base + size; |
3b244aa8 | 564 | totsize = be16_to_cpu(sf->hdr.totsize); |
d8cc890d NS |
565 | if (end != totsize) |
566 | memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end); | |
3b244aa8 | 567 | sf->hdr.count--; |
413d57c9 | 568 | be16_add_cpu(&sf->hdr.totsize, -size); |
d8cc890d NS |
569 | |
570 | /* | |
571 | * Fix up the start offset of the attribute fork | |
572 | */ | |
573 | totsize -= size; | |
6a178100 | 574 | if (totsize == sizeof(xfs_attr_sf_hdr_t) && |
e1486dea CH |
575 | (mp->m_flags & XFS_MOUNT_ATTR2) && |
576 | (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) && | |
577 | !(args->op_flags & XFS_DA_OP_ADDNAME)) { | |
578 | xfs_attr_fork_reset(dp, args->trans); | |
d8cc890d NS |
579 | } else { |
580 | xfs_idata_realloc(dp, -size, XFS_ATTR_FORK); | |
581 | dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize); | |
582 | ASSERT(dp->i_d.di_forkoff); | |
6a178100 BN |
583 | ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) || |
584 | (args->op_flags & XFS_DA_OP_ADDNAME) || | |
585 | !(mp->m_flags & XFS_MOUNT_ATTR2) || | |
586 | dp->i_d.di_format == XFS_DINODE_FMT_BTREE); | |
d8cc890d NS |
587 | xfs_trans_log_inode(args->trans, dp, |
588 | XFS_ILOG_CORE | XFS_ILOG_ADATA); | |
589 | } | |
590 | ||
591 | xfs_sbversion_add_attr2(mp, args->trans); | |
1da177e4 LT |
592 | |
593 | return(0); | |
594 | } | |
595 | ||
596 | /* | |
597 | * Look up a name in a shortform attribute list structure. | |
598 | */ | |
599 | /*ARGSUSED*/ | |
600 | int | |
601 | xfs_attr_shortform_lookup(xfs_da_args_t *args) | |
602 | { | |
603 | xfs_attr_shortform_t *sf; | |
604 | xfs_attr_sf_entry_t *sfe; | |
605 | int i; | |
606 | xfs_ifork_t *ifp; | |
607 | ||
5a5881cd DC |
608 | trace_xfs_attr_sf_lookup(args); |
609 | ||
1da177e4 LT |
610 | ifp = args->dp->i_afp; |
611 | ASSERT(ifp->if_flags & XFS_IFINLINE); | |
612 | sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data; | |
613 | sfe = &sf->list[0]; | |
3b244aa8 | 614 | for (i = 0; i < sf->hdr.count; |
1da177e4 LT |
615 | sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) { |
616 | if (sfe->namelen != args->namelen) | |
617 | continue; | |
618 | if (memcmp(args->name, sfe->nameval, args->namelen) != 0) | |
619 | continue; | |
726801ba | 620 | if (!xfs_attr_namesp_match(args->flags, sfe->flags)) |
1da177e4 LT |
621 | continue; |
622 | return(XFS_ERROR(EEXIST)); | |
623 | } | |
624 | return(XFS_ERROR(ENOATTR)); | |
625 | } | |
626 | ||
627 | /* | |
628 | * Look up a name in a shortform attribute list structure. | |
629 | */ | |
630 | /*ARGSUSED*/ | |
631 | int | |
632 | xfs_attr_shortform_getvalue(xfs_da_args_t *args) | |
633 | { | |
634 | xfs_attr_shortform_t *sf; | |
635 | xfs_attr_sf_entry_t *sfe; | |
636 | int i; | |
637 | ||
638 | ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE); | |
639 | sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data; | |
640 | sfe = &sf->list[0]; | |
3b244aa8 | 641 | for (i = 0; i < sf->hdr.count; |
1da177e4 LT |
642 | sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) { |
643 | if (sfe->namelen != args->namelen) | |
644 | continue; | |
645 | if (memcmp(args->name, sfe->nameval, args->namelen) != 0) | |
646 | continue; | |
726801ba | 647 | if (!xfs_attr_namesp_match(args->flags, sfe->flags)) |
1da177e4 LT |
648 | continue; |
649 | if (args->flags & ATTR_KERNOVAL) { | |
3b244aa8 | 650 | args->valuelen = sfe->valuelen; |
1da177e4 LT |
651 | return(XFS_ERROR(EEXIST)); |
652 | } | |
3b244aa8 NS |
653 | if (args->valuelen < sfe->valuelen) { |
654 | args->valuelen = sfe->valuelen; | |
1da177e4 LT |
655 | return(XFS_ERROR(ERANGE)); |
656 | } | |
3b244aa8 | 657 | args->valuelen = sfe->valuelen; |
1da177e4 LT |
658 | memcpy(args->value, &sfe->nameval[args->namelen], |
659 | args->valuelen); | |
660 | return(XFS_ERROR(EEXIST)); | |
661 | } | |
662 | return(XFS_ERROR(ENOATTR)); | |
663 | } | |
664 | ||
665 | /* | |
666 | * Convert from using the shortform to the leaf. | |
667 | */ | |
668 | int | |
669 | xfs_attr_shortform_to_leaf(xfs_da_args_t *args) | |
670 | { | |
671 | xfs_inode_t *dp; | |
672 | xfs_attr_shortform_t *sf; | |
673 | xfs_attr_sf_entry_t *sfe; | |
674 | xfs_da_args_t nargs; | |
675 | char *tmpbuffer; | |
676 | int error, i, size; | |
677 | xfs_dablk_t blkno; | |
1d9025e5 | 678 | struct xfs_buf *bp; |
1da177e4 LT |
679 | xfs_ifork_t *ifp; |
680 | ||
5a5881cd DC |
681 | trace_xfs_attr_sf_to_leaf(args); |
682 | ||
1da177e4 LT |
683 | dp = args->dp; |
684 | ifp = dp->i_afp; | |
685 | sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data; | |
3b244aa8 | 686 | size = be16_to_cpu(sf->hdr.totsize); |
1da177e4 LT |
687 | tmpbuffer = kmem_alloc(size, KM_SLEEP); |
688 | ASSERT(tmpbuffer != NULL); | |
689 | memcpy(tmpbuffer, ifp->if_u1.if_data, size); | |
690 | sf = (xfs_attr_shortform_t *)tmpbuffer; | |
691 | ||
692 | xfs_idata_realloc(dp, -size, XFS_ATTR_FORK); | |
693 | bp = NULL; | |
694 | error = xfs_da_grow_inode(args, &blkno); | |
695 | if (error) { | |
696 | /* | |
697 | * If we hit an IO error middle of the transaction inside | |
698 | * grow_inode(), we may have inconsistent data. Bail out. | |
699 | */ | |
700 | if (error == EIO) | |
701 | goto out; | |
702 | xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */ | |
703 | memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */ | |
704 | goto out; | |
705 | } | |
706 | ||
707 | ASSERT(blkno == 0); | |
517c2220 | 708 | error = xfs_attr3_leaf_create(args, blkno, &bp); |
1da177e4 LT |
709 | if (error) { |
710 | error = xfs_da_shrink_inode(args, 0, bp); | |
711 | bp = NULL; | |
712 | if (error) | |
713 | goto out; | |
714 | xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */ | |
715 | memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */ | |
716 | goto out; | |
717 | } | |
718 | ||
719 | memset((char *)&nargs, 0, sizeof(nargs)); | |
720 | nargs.dp = dp; | |
721 | nargs.firstblock = args->firstblock; | |
722 | nargs.flist = args->flist; | |
723 | nargs.total = args->total; | |
724 | nargs.whichfork = XFS_ATTR_FORK; | |
725 | nargs.trans = args->trans; | |
6a178100 | 726 | nargs.op_flags = XFS_DA_OP_OKNOENT; |
1da177e4 LT |
727 | |
728 | sfe = &sf->list[0]; | |
3b244aa8 | 729 | for (i = 0; i < sf->hdr.count; i++) { |
a9273ca5 | 730 | nargs.name = sfe->nameval; |
1da177e4 | 731 | nargs.namelen = sfe->namelen; |
a9273ca5 | 732 | nargs.value = &sfe->nameval[nargs.namelen]; |
3b244aa8 | 733 | nargs.valuelen = sfe->valuelen; |
a9273ca5 | 734 | nargs.hashval = xfs_da_hashname(sfe->nameval, |
1da177e4 | 735 | sfe->namelen); |
726801ba | 736 | nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags); |
517c2220 | 737 | error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */ |
1da177e4 | 738 | ASSERT(error == ENOATTR); |
517c2220 | 739 | error = xfs_attr3_leaf_add(bp, &nargs); |
1da177e4 LT |
740 | ASSERT(error != ENOSPC); |
741 | if (error) | |
742 | goto out; | |
743 | sfe = XFS_ATTR_SF_NEXTENTRY(sfe); | |
744 | } | |
745 | error = 0; | |
746 | ||
747 | out: | |
f0e2d93c | 748 | kmem_free(tmpbuffer); |
1da177e4 LT |
749 | return(error); |
750 | } | |
751 | ||
752 | STATIC int | |
753 | xfs_attr_shortform_compare(const void *a, const void *b) | |
754 | { | |
755 | xfs_attr_sf_sort_t *sa, *sb; | |
756 | ||
757 | sa = (xfs_attr_sf_sort_t *)a; | |
758 | sb = (xfs_attr_sf_sort_t *)b; | |
984a081a | 759 | if (sa->hash < sb->hash) { |
1da177e4 | 760 | return(-1); |
984a081a | 761 | } else if (sa->hash > sb->hash) { |
1da177e4 LT |
762 | return(1); |
763 | } else { | |
764 | return(sa->entno - sb->entno); | |
765 | } | |
766 | } | |
767 | ||
726801ba TS |
768 | |
769 | #define XFS_ISRESET_CURSOR(cursor) \ | |
770 | (!((cursor)->initted) && !((cursor)->hashval) && \ | |
771 | !((cursor)->blkno) && !((cursor)->offset)) | |
1da177e4 LT |
772 | /* |
773 | * Copy out entries of shortform attribute lists for attr_list(). | |
c41564b5 | 774 | * Shortform attribute lists are not stored in hashval sorted order. |
1da177e4 LT |
775 | * If the output buffer is not large enough to hold them all, then we |
776 | * we have to calculate each entries' hashvalue and sort them before | |
777 | * we can begin returning them to the user. | |
778 | */ | |
779 | /*ARGSUSED*/ | |
780 | int | |
781 | xfs_attr_shortform_list(xfs_attr_list_context_t *context) | |
782 | { | |
783 | attrlist_cursor_kern_t *cursor; | |
784 | xfs_attr_sf_sort_t *sbuf, *sbp; | |
785 | xfs_attr_shortform_t *sf; | |
786 | xfs_attr_sf_entry_t *sfe; | |
787 | xfs_inode_t *dp; | |
788 | int sbsize, nsbuf, count, i; | |
726801ba | 789 | int error; |
1da177e4 LT |
790 | |
791 | ASSERT(context != NULL); | |
792 | dp = context->dp; | |
793 | ASSERT(dp != NULL); | |
794 | ASSERT(dp->i_afp != NULL); | |
795 | sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data; | |
796 | ASSERT(sf != NULL); | |
797 | if (!sf->hdr.count) | |
798 | return(0); | |
799 | cursor = context->cursor; | |
800 | ASSERT(cursor != NULL); | |
801 | ||
0b1b213f | 802 | trace_xfs_attr_list_sf(context); |
1da177e4 LT |
803 | |
804 | /* | |
726801ba TS |
805 | * If the buffer is large enough and the cursor is at the start, |
806 | * do not bother with sorting since we will return everything in | |
807 | * one buffer and another call using the cursor won't need to be | |
808 | * made. | |
1da177e4 | 809 | * Note the generous fudge factor of 16 overhead bytes per entry. |
726801ba TS |
810 | * If bufsize is zero then put_listent must be a search function |
811 | * and can just scan through what we have. | |
1da177e4 | 812 | */ |
726801ba TS |
813 | if (context->bufsize == 0 || |
814 | (XFS_ISRESET_CURSOR(cursor) && | |
815 | (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) { | |
3b244aa8 | 816 | for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) { |
726801ba | 817 | error = context->put_listent(context, |
ad9b463a | 818 | sfe->flags, |
a9273ca5 | 819 | sfe->nameval, |
726801ba TS |
820 | (int)sfe->namelen, |
821 | (int)sfe->valuelen, | |
a9273ca5 | 822 | &sfe->nameval[sfe->namelen]); |
726801ba TS |
823 | |
824 | /* | |
825 | * Either search callback finished early or | |
826 | * didn't fit it all in the buffer after all. | |
827 | */ | |
828 | if (context->seen_enough) | |
829 | break; | |
830 | ||
831 | if (error) | |
832 | return error; | |
1da177e4 LT |
833 | sfe = XFS_ATTR_SF_NEXTENTRY(sfe); |
834 | } | |
0b1b213f | 835 | trace_xfs_attr_list_sf_all(context); |
1da177e4 LT |
836 | return(0); |
837 | } | |
838 | ||
726801ba TS |
839 | /* do no more for a search callback */ |
840 | if (context->bufsize == 0) | |
841 | return 0; | |
842 | ||
1da177e4 LT |
843 | /* |
844 | * It didn't all fit, so we have to sort everything on hashval. | |
845 | */ | |
3b244aa8 | 846 | sbsize = sf->hdr.count * sizeof(*sbuf); |
622d8149 | 847 | sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP | KM_NOFS); |
1da177e4 LT |
848 | |
849 | /* | |
850 | * Scan the attribute list for the rest of the entries, storing | |
851 | * the relevant info from only those that match into a buffer. | |
852 | */ | |
853 | nsbuf = 0; | |
3b244aa8 | 854 | for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) { |
1da177e4 LT |
855 | if (unlikely( |
856 | ((char *)sfe < (char *)sf) || | |
857 | ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) { | |
858 | XFS_CORRUPTION_ERROR("xfs_attr_shortform_list", | |
859 | XFS_ERRLEVEL_LOW, | |
860 | context->dp->i_mount, sfe); | |
f0e2d93c | 861 | kmem_free(sbuf); |
1da177e4 LT |
862 | return XFS_ERROR(EFSCORRUPTED); |
863 | } | |
ad9b463a | 864 | |
1da177e4 | 865 | sbp->entno = i; |
a9273ca5 DC |
866 | sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen); |
867 | sbp->name = sfe->nameval; | |
1da177e4 LT |
868 | sbp->namelen = sfe->namelen; |
869 | /* These are bytes, and both on-disk, don't endian-flip */ | |
870 | sbp->valuelen = sfe->valuelen; | |
871 | sbp->flags = sfe->flags; | |
872 | sfe = XFS_ATTR_SF_NEXTENTRY(sfe); | |
873 | sbp++; | |
874 | nsbuf++; | |
875 | } | |
876 | ||
877 | /* | |
878 | * Sort the entries on hash then entno. | |
879 | */ | |
380b5dc0 | 880 | xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare); |
1da177e4 LT |
881 | |
882 | /* | |
883 | * Re-find our place IN THE SORTED LIST. | |
884 | */ | |
885 | count = 0; | |
886 | cursor->initted = 1; | |
887 | cursor->blkno = 0; | |
888 | for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) { | |
984a081a | 889 | if (sbp->hash == cursor->hashval) { |
1da177e4 LT |
890 | if (cursor->offset == count) { |
891 | break; | |
892 | } | |
893 | count++; | |
984a081a | 894 | } else if (sbp->hash > cursor->hashval) { |
1da177e4 LT |
895 | break; |
896 | } | |
897 | } | |
898 | if (i == nsbuf) { | |
f0e2d93c | 899 | kmem_free(sbuf); |
1da177e4 LT |
900 | return(0); |
901 | } | |
902 | ||
903 | /* | |
904 | * Loop putting entries into the user buffer. | |
905 | */ | |
906 | for ( ; i < nsbuf; i++, sbp++) { | |
984a081a NS |
907 | if (cursor->hashval != sbp->hash) { |
908 | cursor->hashval = sbp->hash; | |
1da177e4 LT |
909 | cursor->offset = 0; |
910 | } | |
726801ba | 911 | error = context->put_listent(context, |
ad9b463a | 912 | sbp->flags, |
726801ba TS |
913 | sbp->name, |
914 | sbp->namelen, | |
915 | sbp->valuelen, | |
916 | &sbp->name[sbp->namelen]); | |
917 | if (error) | |
918 | return error; | |
919 | if (context->seen_enough) | |
920 | break; | |
1da177e4 LT |
921 | cursor->offset++; |
922 | } | |
923 | ||
f0e2d93c | 924 | kmem_free(sbuf); |
1da177e4 LT |
925 | return(0); |
926 | } | |
927 | ||
928 | /* | |
929 | * Check a leaf attribute block to see if all the entries would fit into | |
930 | * a shortform attribute list. | |
931 | */ | |
932 | int | |
1d9025e5 | 933 | xfs_attr_shortform_allfit( |
b38958d7 DC |
934 | struct xfs_buf *bp, |
935 | struct xfs_inode *dp) | |
1da177e4 | 936 | { |
b38958d7 DC |
937 | struct xfs_attr_leafblock *leaf; |
938 | struct xfs_attr_leaf_entry *entry; | |
1da177e4 | 939 | xfs_attr_leaf_name_local_t *name_loc; |
b38958d7 DC |
940 | struct xfs_attr3_icleaf_hdr leafhdr; |
941 | int bytes; | |
942 | int i; | |
1da177e4 | 943 | |
1d9025e5 | 944 | leaf = bp->b_addr; |
b38958d7 DC |
945 | xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf); |
946 | entry = xfs_attr3_leaf_entryp(leaf); | |
1da177e4 | 947 | |
1da177e4 | 948 | bytes = sizeof(struct xfs_attr_sf_hdr); |
b38958d7 | 949 | for (i = 0; i < leafhdr.count; entry++, i++) { |
1da177e4 LT |
950 | if (entry->flags & XFS_ATTR_INCOMPLETE) |
951 | continue; /* don't copy partial entries */ | |
952 | if (!(entry->flags & XFS_ATTR_LOCAL)) | |
953 | return(0); | |
517c2220 | 954 | name_loc = xfs_attr3_leaf_name_local(leaf, i); |
1da177e4 LT |
955 | if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX) |
956 | return(0); | |
053b5758 | 957 | if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX) |
1da177e4 | 958 | return(0); |
b38958d7 | 959 | bytes += sizeof(struct xfs_attr_sf_entry) - 1 |
1da177e4 | 960 | + name_loc->namelen |
053b5758 | 961 | + be16_to_cpu(name_loc->valuelen); |
1da177e4 | 962 | } |
13059ff0 | 963 | if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) && |
e5889e90 | 964 | (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) && |
e0144ca5 | 965 | (bytes == sizeof(struct xfs_attr_sf_hdr))) |
b38958d7 DC |
966 | return -1; |
967 | return xfs_attr_shortform_bytesfit(dp, bytes); | |
1da177e4 LT |
968 | } |
969 | ||
970 | /* | |
971 | * Convert a leaf attribute list to shortform attribute list | |
972 | */ | |
973 | int | |
517c2220 DC |
974 | xfs_attr3_leaf_to_shortform( |
975 | struct xfs_buf *bp, | |
976 | struct xfs_da_args *args, | |
977 | int forkoff) | |
1da177e4 | 978 | { |
517c2220 DC |
979 | struct xfs_attr_leafblock *leaf; |
980 | struct xfs_attr3_icleaf_hdr ichdr; | |
981 | struct xfs_attr_leaf_entry *entry; | |
982 | struct xfs_attr_leaf_name_local *name_loc; | |
983 | struct xfs_da_args nargs; | |
984 | struct xfs_inode *dp = args->dp; | |
985 | char *tmpbuffer; | |
986 | int error; | |
987 | int i; | |
1da177e4 | 988 | |
5a5881cd DC |
989 | trace_xfs_attr_leaf_to_sf(args); |
990 | ||
1da177e4 | 991 | tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP); |
517c2220 DC |
992 | if (!tmpbuffer) |
993 | return ENOMEM; | |
1da177e4 | 994 | |
1d9025e5 | 995 | memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(dp->i_mount)); |
517c2220 | 996 | |
1da177e4 | 997 | leaf = (xfs_attr_leafblock_t *)tmpbuffer; |
517c2220 DC |
998 | xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf); |
999 | entry = xfs_attr3_leaf_entryp(leaf); | |
1000 | ||
1001 | /* XXX (dgc): buffer is about to be marked stale - why zero it? */ | |
1d9025e5 | 1002 | memset(bp->b_addr, 0, XFS_LBSIZE(dp->i_mount)); |
1da177e4 LT |
1003 | |
1004 | /* | |
1005 | * Clean out the prior contents of the attribute list. | |
1006 | */ | |
1007 | error = xfs_da_shrink_inode(args, 0, bp); | |
1008 | if (error) | |
1009 | goto out; | |
d8cc890d NS |
1010 | |
1011 | if (forkoff == -1) { | |
13059ff0 | 1012 | ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2); |
e5889e90 | 1013 | ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE); |
e1486dea | 1014 | xfs_attr_fork_reset(dp, args->trans); |
1da177e4 | 1015 | goto out; |
d8cc890d NS |
1016 | } |
1017 | ||
1018 | xfs_attr_shortform_create(args); | |
1da177e4 LT |
1019 | |
1020 | /* | |
1021 | * Copy the attributes | |
1022 | */ | |
1023 | memset((char *)&nargs, 0, sizeof(nargs)); | |
1024 | nargs.dp = dp; | |
1025 | nargs.firstblock = args->firstblock; | |
1026 | nargs.flist = args->flist; | |
1027 | nargs.total = args->total; | |
1028 | nargs.whichfork = XFS_ATTR_FORK; | |
1029 | nargs.trans = args->trans; | |
6a178100 | 1030 | nargs.op_flags = XFS_DA_OP_OKNOENT; |
517c2220 DC |
1031 | |
1032 | for (i = 0; i < ichdr.count; entry++, i++) { | |
1da177e4 LT |
1033 | if (entry->flags & XFS_ATTR_INCOMPLETE) |
1034 | continue; /* don't copy partial entries */ | |
1035 | if (!entry->nameidx) | |
1036 | continue; | |
1037 | ASSERT(entry->flags & XFS_ATTR_LOCAL); | |
517c2220 | 1038 | name_loc = xfs_attr3_leaf_name_local(leaf, i); |
a9273ca5 | 1039 | nargs.name = name_loc->nameval; |
1da177e4 | 1040 | nargs.namelen = name_loc->namelen; |
a9273ca5 | 1041 | nargs.value = &name_loc->nameval[nargs.namelen]; |
053b5758 | 1042 | nargs.valuelen = be16_to_cpu(name_loc->valuelen); |
6b19f2d8 | 1043 | nargs.hashval = be32_to_cpu(entry->hashval); |
726801ba | 1044 | nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags); |
d8cc890d | 1045 | xfs_attr_shortform_add(&nargs, forkoff); |
1da177e4 LT |
1046 | } |
1047 | error = 0; | |
1048 | ||
1049 | out: | |
f0e2d93c | 1050 | kmem_free(tmpbuffer); |
517c2220 | 1051 | return error; |
1da177e4 LT |
1052 | } |
1053 | ||
1054 | /* | |
1055 | * Convert from using a single leaf to a root node and a leaf. | |
1056 | */ | |
1057 | int | |
517c2220 DC |
1058 | xfs_attr3_leaf_to_node( |
1059 | struct xfs_da_args *args) | |
1da177e4 | 1060 | { |
517c2220 DC |
1061 | struct xfs_attr_leafblock *leaf; |
1062 | struct xfs_attr3_icleaf_hdr icleafhdr; | |
1063 | struct xfs_attr_leaf_entry *entries; | |
f5ea1100 | 1064 | struct xfs_da_node_entry *btree; |
517c2220 DC |
1065 | struct xfs_da3_icnode_hdr icnodehdr; |
1066 | struct xfs_da_intnode *node; | |
1067 | struct xfs_inode *dp = args->dp; | |
1068 | struct xfs_mount *mp = dp->i_mount; | |
1069 | struct xfs_buf *bp1 = NULL; | |
1070 | struct xfs_buf *bp2 = NULL; | |
1071 | xfs_dablk_t blkno; | |
1072 | int error; | |
1da177e4 | 1073 | |
5a5881cd DC |
1074 | trace_xfs_attr_leaf_to_node(args); |
1075 | ||
1da177e4 LT |
1076 | error = xfs_da_grow_inode(args, &blkno); |
1077 | if (error) | |
1078 | goto out; | |
517c2220 | 1079 | error = xfs_attr3_leaf_read(args->trans, dp, 0, -1, &bp1); |
1da177e4 LT |
1080 | if (error) |
1081 | goto out; | |
ad14c33a | 1082 | |
517c2220 | 1083 | error = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp2, XFS_ATTR_FORK); |
1da177e4 LT |
1084 | if (error) |
1085 | goto out; | |
517c2220 DC |
1086 | |
1087 | /* copy leaf to new buffer, update identifiers */ | |
61fe135c | 1088 | xfs_trans_buf_set_type(args->trans, bp2, XFS_BLFT_ATTR_LEAF_BUF); |
1813dd64 | 1089 | bp2->b_ops = bp1->b_ops; |
517c2220 DC |
1090 | memcpy(bp2->b_addr, bp1->b_addr, XFS_LBSIZE(mp)); |
1091 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
1092 | struct xfs_da3_blkinfo *hdr3 = bp2->b_addr; | |
1093 | hdr3->blkno = cpu_to_be64(bp2->b_bn); | |
1094 | } | |
1095 | xfs_trans_log_buf(args->trans, bp2, 0, XFS_LBSIZE(mp) - 1); | |
1da177e4 LT |
1096 | |
1097 | /* | |
1098 | * Set up the new root node. | |
1099 | */ | |
f5ea1100 | 1100 | error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK); |
1da177e4 LT |
1101 | if (error) |
1102 | goto out; | |
1d9025e5 | 1103 | node = bp1->b_addr; |
517c2220 DC |
1104 | xfs_da3_node_hdr_from_disk(&icnodehdr, node); |
1105 | btree = xfs_da3_node_tree_p(node); | |
1106 | ||
1d9025e5 | 1107 | leaf = bp2->b_addr; |
517c2220 DC |
1108 | xfs_attr3_leaf_hdr_from_disk(&icleafhdr, leaf); |
1109 | entries = xfs_attr3_leaf_entryp(leaf); | |
1110 | ||
1da177e4 | 1111 | /* both on-disk, don't endian-flip twice */ |
517c2220 | 1112 | btree[0].hashval = entries[icleafhdr.count - 1].hashval; |
f5ea1100 | 1113 | btree[0].before = cpu_to_be32(blkno); |
517c2220 DC |
1114 | icnodehdr.count = 1; |
1115 | xfs_da3_node_hdr_to_disk(node, &icnodehdr); | |
1116 | xfs_trans_log_buf(args->trans, bp1, 0, XFS_LBSIZE(mp) - 1); | |
1da177e4 LT |
1117 | error = 0; |
1118 | out: | |
517c2220 | 1119 | return error; |
1da177e4 LT |
1120 | } |
1121 | ||
1122 | ||
1123 | /*======================================================================== | |
1124 | * Routines used for growing the Btree. | |
1125 | *========================================================================*/ | |
1126 | ||
1127 | /* | |
1128 | * Create the initial contents of a leaf attribute list | |
1129 | * or a leaf in a node attribute list. | |
1130 | */ | |
ba0f32d4 | 1131 | STATIC int |
517c2220 DC |
1132 | xfs_attr3_leaf_create( |
1133 | struct xfs_da_args *args, | |
1134 | xfs_dablk_t blkno, | |
1135 | struct xfs_buf **bpp) | |
1da177e4 | 1136 | { |
517c2220 DC |
1137 | struct xfs_attr_leafblock *leaf; |
1138 | struct xfs_attr3_icleaf_hdr ichdr; | |
1139 | struct xfs_inode *dp = args->dp; | |
1140 | struct xfs_mount *mp = dp->i_mount; | |
1141 | struct xfs_buf *bp; | |
1142 | int error; | |
1da177e4 | 1143 | |
5a5881cd DC |
1144 | trace_xfs_attr_leaf_create(args); |
1145 | ||
1da177e4 LT |
1146 | error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp, |
1147 | XFS_ATTR_FORK); | |
1148 | if (error) | |
517c2220 DC |
1149 | return error; |
1150 | bp->b_ops = &xfs_attr3_leaf_buf_ops; | |
61fe135c | 1151 | xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF); |
1d9025e5 | 1152 | leaf = bp->b_addr; |
517c2220 DC |
1153 | memset(leaf, 0, XFS_LBSIZE(mp)); |
1154 | ||
1155 | memset(&ichdr, 0, sizeof(ichdr)); | |
1156 | ichdr.firstused = XFS_LBSIZE(mp); | |
1157 | ||
1158 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
1159 | struct xfs_da3_blkinfo *hdr3 = bp->b_addr; | |
1160 | ||
1161 | ichdr.magic = XFS_ATTR3_LEAF_MAGIC; | |
1162 | ||
1163 | hdr3->blkno = cpu_to_be64(bp->b_bn); | |
1164 | hdr3->owner = cpu_to_be64(dp->i_ino); | |
1165 | uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid); | |
1da177e4 | 1166 | |
517c2220 DC |
1167 | ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr); |
1168 | } else { | |
1169 | ichdr.magic = XFS_ATTR_LEAF_MAGIC; | |
1170 | ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr); | |
1171 | } | |
1172 | ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base; | |
1da177e4 | 1173 | |
517c2220 DC |
1174 | xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr); |
1175 | xfs_trans_log_buf(args->trans, bp, 0, XFS_LBSIZE(mp) - 1); | |
1da177e4 LT |
1176 | |
1177 | *bpp = bp; | |
517c2220 | 1178 | return 0; |
1da177e4 LT |
1179 | } |
1180 | ||
1181 | /* | |
1182 | * Split the leaf node, rebalance, then add the new entry. | |
1183 | */ | |
1184 | int | |
517c2220 DC |
1185 | xfs_attr3_leaf_split( |
1186 | struct xfs_da_state *state, | |
1187 | struct xfs_da_state_blk *oldblk, | |
1188 | struct xfs_da_state_blk *newblk) | |
1da177e4 LT |
1189 | { |
1190 | xfs_dablk_t blkno; | |
1191 | int error; | |
1192 | ||
5a5881cd DC |
1193 | trace_xfs_attr_leaf_split(state->args); |
1194 | ||
1da177e4 LT |
1195 | /* |
1196 | * Allocate space for a new leaf node. | |
1197 | */ | |
1198 | ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC); | |
1199 | error = xfs_da_grow_inode(state->args, &blkno); | |
1200 | if (error) | |
1201 | return(error); | |
517c2220 | 1202 | error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp); |
1da177e4 LT |
1203 | if (error) |
1204 | return(error); | |
1205 | newblk->blkno = blkno; | |
1206 | newblk->magic = XFS_ATTR_LEAF_MAGIC; | |
1207 | ||
1208 | /* | |
1209 | * Rebalance the entries across the two leaves. | |
1210 | * NOTE: rebalance() currently depends on the 2nd block being empty. | |
1211 | */ | |
517c2220 | 1212 | xfs_attr3_leaf_rebalance(state, oldblk, newblk); |
f5ea1100 | 1213 | error = xfs_da3_blk_link(state, oldblk, newblk); |
1da177e4 LT |
1214 | if (error) |
1215 | return(error); | |
1216 | ||
1217 | /* | |
1218 | * Save info on "old" attribute for "atomic rename" ops, leaf_add() | |
1219 | * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the | |
1220 | * "new" attrs info. Will need the "old" info to remove it later. | |
1221 | * | |
1222 | * Insert the "new" entry in the correct block. | |
1223 | */ | |
5a5881cd DC |
1224 | if (state->inleaf) { |
1225 | trace_xfs_attr_leaf_add_old(state->args); | |
517c2220 | 1226 | error = xfs_attr3_leaf_add(oldblk->bp, state->args); |
5a5881cd DC |
1227 | } else { |
1228 | trace_xfs_attr_leaf_add_new(state->args); | |
517c2220 | 1229 | error = xfs_attr3_leaf_add(newblk->bp, state->args); |
5a5881cd | 1230 | } |
1da177e4 LT |
1231 | |
1232 | /* | |
1233 | * Update last hashval in each block since we added the name. | |
1234 | */ | |
1235 | oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL); | |
1236 | newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL); | |
1237 | return(error); | |
1238 | } | |
1239 | ||
1240 | /* | |
1241 | * Add a name to the leaf attribute list structure. | |
1242 | */ | |
1243 | int | |
517c2220 | 1244 | xfs_attr3_leaf_add( |
1d9025e5 DC |
1245 | struct xfs_buf *bp, |
1246 | struct xfs_da_args *args) | |
1da177e4 | 1247 | { |
517c2220 DC |
1248 | struct xfs_attr_leafblock *leaf; |
1249 | struct xfs_attr3_icleaf_hdr ichdr; | |
1250 | int tablesize; | |
1251 | int entsize; | |
1252 | int sum; | |
1253 | int tmp; | |
1254 | int i; | |
1da177e4 | 1255 | |
5a5881cd DC |
1256 | trace_xfs_attr_leaf_add(args); |
1257 | ||
1d9025e5 | 1258 | leaf = bp->b_addr; |
517c2220 DC |
1259 | xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf); |
1260 | ASSERT(args->index >= 0 && args->index <= ichdr.count); | |
aa82daa0 | 1261 | entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen, |
1da177e4 LT |
1262 | args->trans->t_mountp->m_sb.sb_blocksize, NULL); |
1263 | ||
1264 | /* | |
1265 | * Search through freemap for first-fit on new name length. | |
1266 | * (may need to figure in size of entry struct too) | |
1267 | */ | |
517c2220 DC |
1268 | tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t) |
1269 | + xfs_attr3_leaf_hdr_size(leaf); | |
1270 | for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) { | |
1271 | if (tablesize > ichdr.firstused) { | |
1272 | sum += ichdr.freemap[i].size; | |
1da177e4 LT |
1273 | continue; |
1274 | } | |
517c2220 | 1275 | if (!ichdr.freemap[i].size) |
1da177e4 LT |
1276 | continue; /* no space in this map */ |
1277 | tmp = entsize; | |
517c2220 | 1278 | if (ichdr.freemap[i].base < ichdr.firstused) |
1da177e4 | 1279 | tmp += sizeof(xfs_attr_leaf_entry_t); |
517c2220 DC |
1280 | if (ichdr.freemap[i].size >= tmp) { |
1281 | tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, i); | |
1282 | goto out_log_hdr; | |
1da177e4 | 1283 | } |
517c2220 | 1284 | sum += ichdr.freemap[i].size; |
1da177e4 LT |
1285 | } |
1286 | ||
1287 | /* | |
1288 | * If there are no holes in the address space of the block, | |
1289 | * and we don't have enough freespace, then compaction will do us | |
1290 | * no good and we should just give up. | |
1291 | */ | |
517c2220 DC |
1292 | if (!ichdr.holes && sum < entsize) |
1293 | return XFS_ERROR(ENOSPC); | |
1da177e4 LT |
1294 | |
1295 | /* | |
1296 | * Compact the entries to coalesce free space. | |
1297 | * This may change the hdr->count via dropping INCOMPLETE entries. | |
1298 | */ | |
517c2220 | 1299 | xfs_attr3_leaf_compact(args, &ichdr, bp); |
1da177e4 LT |
1300 | |
1301 | /* | |
1302 | * After compaction, the block is guaranteed to have only one | |
1303 | * free region, in freemap[0]. If it is not big enough, give up. | |
1304 | */ | |
517c2220 DC |
1305 | if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) { |
1306 | tmp = ENOSPC; | |
1307 | goto out_log_hdr; | |
1308 | } | |
1309 | ||
1310 | tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0); | |
1da177e4 | 1311 | |
517c2220 DC |
1312 | out_log_hdr: |
1313 | xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr); | |
1314 | xfs_trans_log_buf(args->trans, bp, | |
1315 | XFS_DA_LOGRANGE(leaf, &leaf->hdr, | |
1316 | xfs_attr3_leaf_hdr_size(leaf))); | |
1317 | return tmp; | |
1da177e4 LT |
1318 | } |
1319 | ||
1320 | /* | |
1321 | * Add a name to a leaf attribute list structure. | |
1322 | */ | |
1323 | STATIC int | |
517c2220 DC |
1324 | xfs_attr3_leaf_add_work( |
1325 | struct xfs_buf *bp, | |
1326 | struct xfs_attr3_icleaf_hdr *ichdr, | |
1327 | struct xfs_da_args *args, | |
1328 | int mapindex) | |
1da177e4 | 1329 | { |
517c2220 DC |
1330 | struct xfs_attr_leafblock *leaf; |
1331 | struct xfs_attr_leaf_entry *entry; | |
1332 | struct xfs_attr_leaf_name_local *name_loc; | |
1333 | struct xfs_attr_leaf_name_remote *name_rmt; | |
1334 | struct xfs_mount *mp; | |
1335 | int tmp; | |
1336 | int i; | |
1da177e4 | 1337 | |
ee73259b DC |
1338 | trace_xfs_attr_leaf_add_work(args); |
1339 | ||
1d9025e5 | 1340 | leaf = bp->b_addr; |
517c2220 DC |
1341 | ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE); |
1342 | ASSERT(args->index >= 0 && args->index <= ichdr->count); | |
1da177e4 LT |
1343 | |
1344 | /* | |
1345 | * Force open some space in the entry array and fill it in. | |
1346 | */ | |
517c2220 DC |
1347 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; |
1348 | if (args->index < ichdr->count) { | |
1349 | tmp = ichdr->count - args->index; | |
1da177e4 | 1350 | tmp *= sizeof(xfs_attr_leaf_entry_t); |
517c2220 | 1351 | memmove(entry + 1, entry, tmp); |
1d9025e5 | 1352 | xfs_trans_log_buf(args->trans, bp, |
1da177e4 LT |
1353 | XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry))); |
1354 | } | |
517c2220 | 1355 | ichdr->count++; |
1da177e4 LT |
1356 | |
1357 | /* | |
1358 | * Allocate space for the new string (at the end of the run). | |
1359 | */ | |
1da177e4 | 1360 | mp = args->trans->t_mountp; |
517c2220 DC |
1361 | ASSERT(ichdr->freemap[mapindex].base < XFS_LBSIZE(mp)); |
1362 | ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0); | |
1363 | ASSERT(ichdr->freemap[mapindex].size >= | |
aa82daa0 NS |
1364 | xfs_attr_leaf_newentsize(args->namelen, args->valuelen, |
1365 | mp->m_sb.sb_blocksize, NULL)); | |
517c2220 DC |
1366 | ASSERT(ichdr->freemap[mapindex].size < XFS_LBSIZE(mp)); |
1367 | ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0); | |
1368 | ||
1369 | ichdr->freemap[mapindex].size -= | |
1370 | xfs_attr_leaf_newentsize(args->namelen, args->valuelen, | |
1371 | mp->m_sb.sb_blocksize, &tmp); | |
1372 | ||
1373 | entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base + | |
1374 | ichdr->freemap[mapindex].size); | |
6b19f2d8 | 1375 | entry->hashval = cpu_to_be32(args->hashval); |
1da177e4 | 1376 | entry->flags = tmp ? XFS_ATTR_LOCAL : 0; |
726801ba | 1377 | entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags); |
6a178100 | 1378 | if (args->op_flags & XFS_DA_OP_RENAME) { |
1da177e4 LT |
1379 | entry->flags |= XFS_ATTR_INCOMPLETE; |
1380 | if ((args->blkno2 == args->blkno) && | |
1381 | (args->index2 <= args->index)) { | |
1382 | args->index2++; | |
1383 | } | |
1384 | } | |
1d9025e5 | 1385 | xfs_trans_log_buf(args->trans, bp, |
1da177e4 | 1386 | XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry))); |
6b19f2d8 NS |
1387 | ASSERT((args->index == 0) || |
1388 | (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval))); | |
517c2220 | 1389 | ASSERT((args->index == ichdr->count - 1) || |
6b19f2d8 | 1390 | (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval))); |
1da177e4 LT |
1391 | |
1392 | /* | |
1da177e4 LT |
1393 | * For "remote" attribute values, simply note that we need to |
1394 | * allocate space for the "remote" value. We can't actually | |
1395 | * allocate the extents in this transaction, and we can't decide | |
1396 | * which blocks they should be as we might allocate more blocks | |
1397 | * as part of this transaction (a split operation for example). | |
1398 | */ | |
1399 | if (entry->flags & XFS_ATTR_LOCAL) { | |
517c2220 | 1400 | name_loc = xfs_attr3_leaf_name_local(leaf, args->index); |
1da177e4 | 1401 | name_loc->namelen = args->namelen; |
053b5758 | 1402 | name_loc->valuelen = cpu_to_be16(args->valuelen); |
1da177e4 LT |
1403 | memcpy((char *)name_loc->nameval, args->name, args->namelen); |
1404 | memcpy((char *)&name_loc->nameval[args->namelen], args->value, | |
053b5758 | 1405 | be16_to_cpu(name_loc->valuelen)); |
1da177e4 | 1406 | } else { |
517c2220 | 1407 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); |
1da177e4 LT |
1408 | name_rmt->namelen = args->namelen; |
1409 | memcpy((char *)name_rmt->name, args->name, args->namelen); | |
1410 | entry->flags |= XFS_ATTR_INCOMPLETE; | |
1411 | /* just in case */ | |
1412 | name_rmt->valuelen = 0; | |
1413 | name_rmt->valueblk = 0; | |
1414 | args->rmtblkno = 1; | |
1415 | args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen); | |
1416 | } | |
1d9025e5 | 1417 | xfs_trans_log_buf(args->trans, bp, |
517c2220 | 1418 | XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index), |
1da177e4 LT |
1419 | xfs_attr_leaf_entsize(leaf, args->index))); |
1420 | ||
1421 | /* | |
1422 | * Update the control info for this leaf node | |
1423 | */ | |
517c2220 DC |
1424 | if (be16_to_cpu(entry->nameidx) < ichdr->firstused) |
1425 | ichdr->firstused = be16_to_cpu(entry->nameidx); | |
1426 | ||
1427 | ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t) | |
1428 | + xfs_attr3_leaf_hdr_size(leaf)); | |
1429 | tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t) | |
1430 | + xfs_attr3_leaf_hdr_size(leaf); | |
1431 | ||
1432 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { | |
1433 | if (ichdr->freemap[i].base == tmp) { | |
1434 | ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t); | |
1435 | ichdr->freemap[i].size -= sizeof(xfs_attr_leaf_entry_t); | |
1da177e4 LT |
1436 | } |
1437 | } | |
517c2220 DC |
1438 | ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index); |
1439 | return 0; | |
1da177e4 LT |
1440 | } |
1441 | ||
1442 | /* | |
1443 | * Garbage collect a leaf attribute list block by copying it to a new buffer. | |
1444 | */ | |
1445 | STATIC void | |
517c2220 | 1446 | xfs_attr3_leaf_compact( |
ee73259b | 1447 | struct xfs_da_args *args, |
517c2220 | 1448 | struct xfs_attr3_icleaf_hdr *ichdr_d, |
ee73259b | 1449 | struct xfs_buf *bp) |
1da177e4 | 1450 | { |
ee73259b | 1451 | xfs_attr_leafblock_t *leaf_s, *leaf_d; |
517c2220 | 1452 | struct xfs_attr3_icleaf_hdr ichdr_s; |
ee73259b DC |
1453 | struct xfs_trans *trans = args->trans; |
1454 | struct xfs_mount *mp = trans->t_mountp; | |
1455 | char *tmpbuffer; | |
1456 | ||
1457 | trace_xfs_attr_leaf_compact(args); | |
1da177e4 | 1458 | |
1da177e4 LT |
1459 | tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP); |
1460 | ASSERT(tmpbuffer != NULL); | |
1d9025e5 DC |
1461 | memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(mp)); |
1462 | memset(bp->b_addr, 0, XFS_LBSIZE(mp)); | |
1da177e4 LT |
1463 | |
1464 | /* | |
1465 | * Copy basic information | |
1466 | */ | |
1467 | leaf_s = (xfs_attr_leafblock_t *)tmpbuffer; | |
1d9025e5 | 1468 | leaf_d = bp->b_addr; |
517c2220 DC |
1469 | ichdr_s = *ichdr_d; /* struct copy */ |
1470 | ichdr_d->firstused = XFS_LBSIZE(mp); | |
1471 | ichdr_d->usedbytes = 0; | |
1472 | ichdr_d->count = 0; | |
1473 | ichdr_d->holes = 0; | |
1474 | ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_s); | |
1475 | ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base; | |
1da177e4 LT |
1476 | |
1477 | /* | |
1478 | * Copy all entry's in the same (sorted) order, | |
1479 | * but allocate name/value pairs packed and in sequence. | |
1480 | */ | |
517c2220 DC |
1481 | xfs_attr3_leaf_moveents(leaf_s, &ichdr_s, 0, leaf_d, ichdr_d, 0, |
1482 | ichdr_s.count, mp); | |
1483 | /* | |
1484 | * this logs the entire buffer, but the caller must write the header | |
1485 | * back to the buffer when it is finished modifying it. | |
1486 | */ | |
1d9025e5 | 1487 | xfs_trans_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1); |
1da177e4 | 1488 | |
f0e2d93c | 1489 | kmem_free(tmpbuffer); |
1da177e4 LT |
1490 | } |
1491 | ||
517c2220 DC |
1492 | /* |
1493 | * Compare two leaf blocks "order". | |
1494 | * Return 0 unless leaf2 should go before leaf1. | |
1495 | */ | |
1496 | static int | |
1497 | xfs_attr3_leaf_order( | |
1498 | struct xfs_buf *leaf1_bp, | |
1499 | struct xfs_attr3_icleaf_hdr *leaf1hdr, | |
1500 | struct xfs_buf *leaf2_bp, | |
1501 | struct xfs_attr3_icleaf_hdr *leaf2hdr) | |
1502 | { | |
1503 | struct xfs_attr_leaf_entry *entries1; | |
1504 | struct xfs_attr_leaf_entry *entries2; | |
1505 | ||
1506 | entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr); | |
1507 | entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr); | |
1508 | if (leaf1hdr->count > 0 && leaf2hdr->count > 0 && | |
1509 | ((be32_to_cpu(entries2[0].hashval) < | |
1510 | be32_to_cpu(entries1[0].hashval)) || | |
1511 | (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) < | |
1512 | be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) { | |
1513 | return 1; | |
1514 | } | |
1515 | return 0; | |
1516 | } | |
1517 | ||
1518 | int | |
1519 | xfs_attr_leaf_order( | |
1520 | struct xfs_buf *leaf1_bp, | |
1521 | struct xfs_buf *leaf2_bp) | |
1522 | { | |
1523 | struct xfs_attr3_icleaf_hdr ichdr1; | |
1524 | struct xfs_attr3_icleaf_hdr ichdr2; | |
1525 | ||
1526 | xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1_bp->b_addr); | |
1527 | xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2_bp->b_addr); | |
1528 | return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2); | |
1529 | } | |
1530 | ||
1da177e4 LT |
1531 | /* |
1532 | * Redistribute the attribute list entries between two leaf nodes, | |
1533 | * taking into account the size of the new entry. | |
1534 | * | |
1535 | * NOTE: if new block is empty, then it will get the upper half of the | |
1536 | * old block. At present, all (one) callers pass in an empty second block. | |
1537 | * | |
1538 | * This code adjusts the args->index/blkno and args->index2/blkno2 fields | |
1539 | * to match what it is doing in splitting the attribute leaf block. Those | |
1540 | * values are used in "atomic rename" operations on attributes. Note that | |
1541 | * the "new" and "old" values can end up in different blocks. | |
1542 | */ | |
1543 | STATIC void | |
517c2220 DC |
1544 | xfs_attr3_leaf_rebalance( |
1545 | struct xfs_da_state *state, | |
1546 | struct xfs_da_state_blk *blk1, | |
1547 | struct xfs_da_state_blk *blk2) | |
1da177e4 | 1548 | { |
517c2220 DC |
1549 | struct xfs_da_args *args; |
1550 | struct xfs_attr_leafblock *leaf1; | |
1551 | struct xfs_attr_leafblock *leaf2; | |
1552 | struct xfs_attr3_icleaf_hdr ichdr1; | |
1553 | struct xfs_attr3_icleaf_hdr ichdr2; | |
1554 | struct xfs_attr_leaf_entry *entries1; | |
1555 | struct xfs_attr_leaf_entry *entries2; | |
1556 | int count; | |
1557 | int totallen; | |
1558 | int max; | |
1559 | int space; | |
1560 | int swap; | |
1da177e4 LT |
1561 | |
1562 | /* | |
1563 | * Set up environment. | |
1564 | */ | |
1565 | ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC); | |
1566 | ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC); | |
1d9025e5 DC |
1567 | leaf1 = blk1->bp->b_addr; |
1568 | leaf2 = blk2->bp->b_addr; | |
517c2220 DC |
1569 | xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1); |
1570 | xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2); | |
1571 | ASSERT(ichdr2.count == 0); | |
1da177e4 LT |
1572 | args = state->args; |
1573 | ||
5a5881cd DC |
1574 | trace_xfs_attr_leaf_rebalance(args); |
1575 | ||
1da177e4 LT |
1576 | /* |
1577 | * Check ordering of blocks, reverse if it makes things simpler. | |
1578 | * | |
1579 | * NOTE: Given that all (current) callers pass in an empty | |
1580 | * second block, this code should never set "swap". | |
1581 | */ | |
1582 | swap = 0; | |
517c2220 DC |
1583 | if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) { |
1584 | struct xfs_da_state_blk *tmp_blk; | |
1585 | struct xfs_attr3_icleaf_hdr tmp_ichdr; | |
1586 | ||
1da177e4 LT |
1587 | tmp_blk = blk1; |
1588 | blk1 = blk2; | |
1589 | blk2 = tmp_blk; | |
517c2220 DC |
1590 | |
1591 | /* struct copies to swap them rather than reconverting */ | |
1592 | tmp_ichdr = ichdr1; | |
1593 | ichdr1 = ichdr2; | |
1594 | ichdr2 = tmp_ichdr; | |
1595 | ||
1d9025e5 DC |
1596 | leaf1 = blk1->bp->b_addr; |
1597 | leaf2 = blk2->bp->b_addr; | |
1da177e4 LT |
1598 | swap = 1; |
1599 | } | |
1da177e4 LT |
1600 | |
1601 | /* | |
1602 | * Examine entries until we reduce the absolute difference in | |
1603 | * byte usage between the two blocks to a minimum. Then get | |
1604 | * the direction to copy and the number of elements to move. | |
1605 | * | |
1606 | * "inleaf" is true if the new entry should be inserted into blk1. | |
1607 | * If "swap" is also true, then reverse the sense of "inleaf". | |
1608 | */ | |
517c2220 DC |
1609 | state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1, |
1610 | blk2, &ichdr2, | |
1611 | &count, &totallen); | |
1da177e4 LT |
1612 | if (swap) |
1613 | state->inleaf = !state->inleaf; | |
1614 | ||
1615 | /* | |
1616 | * Move any entries required from leaf to leaf: | |
1617 | */ | |
517c2220 | 1618 | if (count < ichdr1.count) { |
1da177e4 LT |
1619 | /* |
1620 | * Figure the total bytes to be added to the destination leaf. | |
1621 | */ | |
1622 | /* number entries being moved */ | |
517c2220 DC |
1623 | count = ichdr1.count - count; |
1624 | space = ichdr1.usedbytes - totallen; | |
1da177e4 LT |
1625 | space += count * sizeof(xfs_attr_leaf_entry_t); |
1626 | ||
1627 | /* | |
1628 | * leaf2 is the destination, compact it if it looks tight. | |
1629 | */ | |
517c2220 DC |
1630 | max = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1); |
1631 | max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t); | |
ee73259b | 1632 | if (space > max) |
517c2220 | 1633 | xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp); |
1da177e4 LT |
1634 | |
1635 | /* | |
1636 | * Move high entries from leaf1 to low end of leaf2. | |
1637 | */ | |
517c2220 DC |
1638 | xfs_attr3_leaf_moveents(leaf1, &ichdr1, ichdr1.count - count, |
1639 | leaf2, &ichdr2, 0, count, state->mp); | |
1da177e4 | 1640 | |
517c2220 | 1641 | } else if (count > ichdr1.count) { |
1da177e4 LT |
1642 | /* |
1643 | * I assert that since all callers pass in an empty | |
1644 | * second buffer, this code should never execute. | |
1645 | */ | |
07428d7f | 1646 | ASSERT(0); |
1da177e4 LT |
1647 | |
1648 | /* | |
1649 | * Figure the total bytes to be added to the destination leaf. | |
1650 | */ | |
1651 | /* number entries being moved */ | |
517c2220 DC |
1652 | count -= ichdr1.count; |
1653 | space = totallen - ichdr1.usedbytes; | |
1da177e4 LT |
1654 | space += count * sizeof(xfs_attr_leaf_entry_t); |
1655 | ||
1656 | /* | |
1657 | * leaf1 is the destination, compact it if it looks tight. | |
1658 | */ | |
517c2220 DC |
1659 | max = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1); |
1660 | max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t); | |
ee73259b | 1661 | if (space > max) |
517c2220 | 1662 | xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp); |
1da177e4 LT |
1663 | |
1664 | /* | |
1665 | * Move low entries from leaf2 to high end of leaf1. | |
1666 | */ | |
517c2220 DC |
1667 | xfs_attr3_leaf_moveents(leaf2, &ichdr2, 0, leaf1, &ichdr1, |
1668 | ichdr1.count, count, state->mp); | |
1da177e4 LT |
1669 | } |
1670 | ||
517c2220 DC |
1671 | xfs_attr3_leaf_hdr_to_disk(leaf1, &ichdr1); |
1672 | xfs_attr3_leaf_hdr_to_disk(leaf2, &ichdr2); | |
1673 | xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1); | |
1674 | xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1); | |
1675 | ||
1da177e4 LT |
1676 | /* |
1677 | * Copy out last hashval in each block for B-tree code. | |
1678 | */ | |
517c2220 DC |
1679 | entries1 = xfs_attr3_leaf_entryp(leaf1); |
1680 | entries2 = xfs_attr3_leaf_entryp(leaf2); | |
1681 | blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval); | |
1682 | blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval); | |
1da177e4 LT |
1683 | |
1684 | /* | |
1685 | * Adjust the expected index for insertion. | |
1686 | * NOTE: this code depends on the (current) situation that the | |
1687 | * second block was originally empty. | |
1688 | * | |
1689 | * If the insertion point moved to the 2nd block, we must adjust | |
1690 | * the index. We must also track the entry just following the | |
1691 | * new entry for use in an "atomic rename" operation, that entry | |
1692 | * is always the "old" entry and the "new" entry is what we are | |
1693 | * inserting. The index/blkno fields refer to the "old" entry, | |
1694 | * while the index2/blkno2 fields refer to the "new" entry. | |
1695 | */ | |
517c2220 | 1696 | if (blk1->index > ichdr1.count) { |
1da177e4 | 1697 | ASSERT(state->inleaf == 0); |
517c2220 | 1698 | blk2->index = blk1->index - ichdr1.count; |
1da177e4 LT |
1699 | args->index = args->index2 = blk2->index; |
1700 | args->blkno = args->blkno2 = blk2->blkno; | |
517c2220 | 1701 | } else if (blk1->index == ichdr1.count) { |
1da177e4 LT |
1702 | if (state->inleaf) { |
1703 | args->index = blk1->index; | |
1704 | args->blkno = blk1->blkno; | |
1705 | args->index2 = 0; | |
1706 | args->blkno2 = blk2->blkno; | |
1707 | } else { | |
07428d7f DC |
1708 | /* |
1709 | * On a double leaf split, the original attr location | |
1710 | * is already stored in blkno2/index2, so don't | |
1711 | * overwrite it overwise we corrupt the tree. | |
1712 | */ | |
517c2220 | 1713 | blk2->index = blk1->index - ichdr1.count; |
07428d7f DC |
1714 | args->index = blk2->index; |
1715 | args->blkno = blk2->blkno; | |
1716 | if (!state->extravalid) { | |
1717 | /* | |
1718 | * set the new attr location to match the old | |
1719 | * one and let the higher level split code | |
1720 | * decide where in the leaf to place it. | |
1721 | */ | |
1722 | args->index2 = blk2->index; | |
1723 | args->blkno2 = blk2->blkno; | |
1724 | } | |
1da177e4 LT |
1725 | } |
1726 | } else { | |
1727 | ASSERT(state->inleaf == 1); | |
1728 | args->index = args->index2 = blk1->index; | |
1729 | args->blkno = args->blkno2 = blk1->blkno; | |
1730 | } | |
1731 | } | |
1732 | ||
1733 | /* | |
1734 | * Examine entries until we reduce the absolute difference in | |
1735 | * byte usage between the two blocks to a minimum. | |
1736 | * GROT: Is this really necessary? With other than a 512 byte blocksize, | |
1737 | * GROT: there will always be enough room in either block for a new entry. | |
1738 | * GROT: Do a double-split for this case? | |
1739 | */ | |
1740 | STATIC int | |
517c2220 DC |
1741 | xfs_attr3_leaf_figure_balance( |
1742 | struct xfs_da_state *state, | |
1743 | struct xfs_da_state_blk *blk1, | |
1744 | struct xfs_attr3_icleaf_hdr *ichdr1, | |
1745 | struct xfs_da_state_blk *blk2, | |
1746 | struct xfs_attr3_icleaf_hdr *ichdr2, | |
1747 | int *countarg, | |
1748 | int *usedbytesarg) | |
1da177e4 | 1749 | { |
517c2220 DC |
1750 | struct xfs_attr_leafblock *leaf1 = blk1->bp->b_addr; |
1751 | struct xfs_attr_leafblock *leaf2 = blk2->bp->b_addr; | |
1752 | struct xfs_attr_leaf_entry *entry; | |
1753 | int count; | |
1754 | int max; | |
1755 | int index; | |
1756 | int totallen = 0; | |
1757 | int half; | |
1758 | int lastdelta; | |
1759 | int foundit = 0; | |
1760 | int tmp; | |
1da177e4 LT |
1761 | |
1762 | /* | |
1763 | * Examine entries until we reduce the absolute difference in | |
1764 | * byte usage between the two blocks to a minimum. | |
1765 | */ | |
517c2220 DC |
1766 | max = ichdr1->count + ichdr2->count; |
1767 | half = (max + 1) * sizeof(*entry); | |
1768 | half += ichdr1->usedbytes + ichdr2->usedbytes + | |
1769 | xfs_attr_leaf_newentsize(state->args->namelen, | |
1770 | state->args->valuelen, | |
1771 | state->blocksize, NULL); | |
1da177e4 LT |
1772 | half /= 2; |
1773 | lastdelta = state->blocksize; | |
517c2220 | 1774 | entry = xfs_attr3_leaf_entryp(leaf1); |
1da177e4 LT |
1775 | for (count = index = 0; count < max; entry++, index++, count++) { |
1776 | ||
1777 | #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A)) | |
1778 | /* | |
1779 | * The new entry is in the first block, account for it. | |
1780 | */ | |
1781 | if (count == blk1->index) { | |
1782 | tmp = totallen + sizeof(*entry) + | |
aa82daa0 NS |
1783 | xfs_attr_leaf_newentsize( |
1784 | state->args->namelen, | |
1785 | state->args->valuelen, | |
1786 | state->blocksize, NULL); | |
1da177e4 LT |
1787 | if (XFS_ATTR_ABS(half - tmp) > lastdelta) |
1788 | break; | |
1789 | lastdelta = XFS_ATTR_ABS(half - tmp); | |
1790 | totallen = tmp; | |
1791 | foundit = 1; | |
1792 | } | |
1793 | ||
1794 | /* | |
1795 | * Wrap around into the second block if necessary. | |
1796 | */ | |
517c2220 | 1797 | if (count == ichdr1->count) { |
1da177e4 | 1798 | leaf1 = leaf2; |
517c2220 | 1799 | entry = xfs_attr3_leaf_entryp(leaf1); |
1da177e4 LT |
1800 | index = 0; |
1801 | } | |
1802 | ||
1803 | /* | |
1804 | * Figure out if next leaf entry would be too much. | |
1805 | */ | |
1806 | tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1, | |
1807 | index); | |
1808 | if (XFS_ATTR_ABS(half - tmp) > lastdelta) | |
1809 | break; | |
1810 | lastdelta = XFS_ATTR_ABS(half - tmp); | |
1811 | totallen = tmp; | |
1812 | #undef XFS_ATTR_ABS | |
1813 | } | |
1814 | ||
1815 | /* | |
1816 | * Calculate the number of usedbytes that will end up in lower block. | |
1817 | * If new entry not in lower block, fix up the count. | |
1818 | */ | |
1819 | totallen -= count * sizeof(*entry); | |
1820 | if (foundit) { | |
1821 | totallen -= sizeof(*entry) + | |
aa82daa0 NS |
1822 | xfs_attr_leaf_newentsize( |
1823 | state->args->namelen, | |
1824 | state->args->valuelen, | |
1825 | state->blocksize, NULL); | |
1da177e4 LT |
1826 | } |
1827 | ||
1828 | *countarg = count; | |
1829 | *usedbytesarg = totallen; | |
517c2220 | 1830 | return foundit; |
1da177e4 LT |
1831 | } |
1832 | ||
1833 | /*======================================================================== | |
1834 | * Routines used for shrinking the Btree. | |
1835 | *========================================================================*/ | |
1836 | ||
1837 | /* | |
1838 | * Check a leaf block and its neighbors to see if the block should be | |
1839 | * collapsed into one or the other neighbor. Always keep the block | |
1840 | * with the smaller block number. | |
1841 | * If the current block is over 50% full, don't try to join it, return 0. | |
1842 | * If the block is empty, fill in the state structure and return 2. | |
1843 | * If it can be collapsed, fill in the state structure and return 1. | |
1844 | * If nothing can be done, return 0. | |
1845 | * | |
1846 | * GROT: allow for INCOMPLETE entries in calculation. | |
1847 | */ | |
1848 | int | |
517c2220 DC |
1849 | xfs_attr3_leaf_toosmall( |
1850 | struct xfs_da_state *state, | |
1851 | int *action) | |
1da177e4 | 1852 | { |
517c2220 DC |
1853 | struct xfs_attr_leafblock *leaf; |
1854 | struct xfs_da_state_blk *blk; | |
1855 | struct xfs_attr3_icleaf_hdr ichdr; | |
1856 | struct xfs_buf *bp; | |
1857 | xfs_dablk_t blkno; | |
1858 | int bytes; | |
1859 | int forward; | |
1860 | int error; | |
1861 | int retval; | |
1862 | int i; | |
1da177e4 | 1863 | |
ee73259b DC |
1864 | trace_xfs_attr_leaf_toosmall(state->args); |
1865 | ||
1da177e4 LT |
1866 | /* |
1867 | * Check for the degenerate case of the block being over 50% full. | |
1868 | * If so, it's not worth even looking to see if we might be able | |
1869 | * to coalesce with a sibling. | |
1870 | */ | |
1871 | blk = &state->path.blk[ state->path.active-1 ]; | |
517c2220 DC |
1872 | leaf = blk->bp->b_addr; |
1873 | xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf); | |
1874 | bytes = xfs_attr3_leaf_hdr_size(leaf) + | |
1875 | ichdr.count * sizeof(xfs_attr_leaf_entry_t) + | |
1876 | ichdr.usedbytes; | |
1da177e4 LT |
1877 | if (bytes > (state->blocksize >> 1)) { |
1878 | *action = 0; /* blk over 50%, don't try to join */ | |
1879 | return(0); | |
1880 | } | |
1881 | ||
1882 | /* | |
1883 | * Check for the degenerate case of the block being empty. | |
1884 | * If the block is empty, we'll simply delete it, no need to | |
c41564b5 | 1885 | * coalesce it with a sibling block. We choose (arbitrarily) |
1da177e4 LT |
1886 | * to merge with the forward block unless it is NULL. |
1887 | */ | |
517c2220 | 1888 | if (ichdr.count == 0) { |
1da177e4 LT |
1889 | /* |
1890 | * Make altpath point to the block we want to keep and | |
1891 | * path point to the block we want to drop (this one). | |
1892 | */ | |
517c2220 | 1893 | forward = (ichdr.forw != 0); |
1da177e4 | 1894 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
f5ea1100 | 1895 | error = xfs_da3_path_shift(state, &state->altpath, forward, |
1da177e4 LT |
1896 | 0, &retval); |
1897 | if (error) | |
1898 | return(error); | |
1899 | if (retval) { | |
1900 | *action = 0; | |
1901 | } else { | |
1902 | *action = 2; | |
1903 | } | |
517c2220 | 1904 | return 0; |
1da177e4 LT |
1905 | } |
1906 | ||
1907 | /* | |
1908 | * Examine each sibling block to see if we can coalesce with | |
1909 | * at least 25% free space to spare. We need to figure out | |
1910 | * whether to merge with the forward or the backward block. | |
1911 | * We prefer coalescing with the lower numbered sibling so as | |
1912 | * to shrink an attribute list over time. | |
1913 | */ | |
1914 | /* start with smaller blk num */ | |
517c2220 | 1915 | forward = ichdr.forw < ichdr.back; |
1da177e4 | 1916 | for (i = 0; i < 2; forward = !forward, i++) { |
517c2220 | 1917 | struct xfs_attr3_icleaf_hdr ichdr2; |
1da177e4 | 1918 | if (forward) |
517c2220 | 1919 | blkno = ichdr.forw; |
1da177e4 | 1920 | else |
517c2220 | 1921 | blkno = ichdr.back; |
1da177e4 LT |
1922 | if (blkno == 0) |
1923 | continue; | |
517c2220 | 1924 | error = xfs_attr3_leaf_read(state->args->trans, state->args->dp, |
ad14c33a | 1925 | blkno, -1, &bp); |
1da177e4 LT |
1926 | if (error) |
1927 | return(error); | |
1da177e4 | 1928 | |
517c2220 DC |
1929 | xfs_attr3_leaf_hdr_from_disk(&ichdr2, bp->b_addr); |
1930 | ||
1931 | bytes = state->blocksize - (state->blocksize >> 2) - | |
1932 | ichdr.usedbytes - ichdr2.usedbytes - | |
1933 | ((ichdr.count + ichdr2.count) * | |
1934 | sizeof(xfs_attr_leaf_entry_t)) - | |
1935 | xfs_attr3_leaf_hdr_size(leaf); | |
1936 | ||
1d9025e5 | 1937 | xfs_trans_brelse(state->args->trans, bp); |
1da177e4 LT |
1938 | if (bytes >= 0) |
1939 | break; /* fits with at least 25% to spare */ | |
1940 | } | |
1941 | if (i >= 2) { | |
1942 | *action = 0; | |
1943 | return(0); | |
1944 | } | |
1945 | ||
1946 | /* | |
1947 | * Make altpath point to the block we want to keep (the lower | |
1948 | * numbered block) and path point to the block we want to drop. | |
1949 | */ | |
1950 | memcpy(&state->altpath, &state->path, sizeof(state->path)); | |
1951 | if (blkno < blk->blkno) { | |
f5ea1100 | 1952 | error = xfs_da3_path_shift(state, &state->altpath, forward, |
1da177e4 LT |
1953 | 0, &retval); |
1954 | } else { | |
f5ea1100 | 1955 | error = xfs_da3_path_shift(state, &state->path, forward, |
1da177e4 LT |
1956 | 0, &retval); |
1957 | } | |
1958 | if (error) | |
1959 | return(error); | |
1960 | if (retval) { | |
1961 | *action = 0; | |
1962 | } else { | |
1963 | *action = 1; | |
1964 | } | |
1965 | return(0); | |
1966 | } | |
1967 | ||
1968 | /* | |
1969 | * Remove a name from the leaf attribute list structure. | |
1970 | * | |
1971 | * Return 1 if leaf is less than 37% full, 0 if >= 37% full. | |
1972 | * If two leaves are 37% full, when combined they will leave 25% free. | |
1973 | */ | |
1974 | int | |
517c2220 DC |
1975 | xfs_attr3_leaf_remove( |
1976 | struct xfs_buf *bp, | |
1977 | struct xfs_da_args *args) | |
1da177e4 | 1978 | { |
517c2220 DC |
1979 | struct xfs_attr_leafblock *leaf; |
1980 | struct xfs_attr3_icleaf_hdr ichdr; | |
1981 | struct xfs_attr_leaf_entry *entry; | |
1982 | struct xfs_mount *mp = args->trans->t_mountp; | |
1983 | int before; | |
1984 | int after; | |
1985 | int smallest; | |
1986 | int entsize; | |
1987 | int tablesize; | |
1988 | int tmp; | |
1989 | int i; | |
1da177e4 | 1990 | |
ee73259b DC |
1991 | trace_xfs_attr_leaf_remove(args); |
1992 | ||
1d9025e5 | 1993 | leaf = bp->b_addr; |
517c2220 DC |
1994 | xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf); |
1995 | ||
1996 | ASSERT(ichdr.count > 0 && ichdr.count < XFS_LBSIZE(mp) / 8); | |
1997 | ASSERT(args->index >= 0 && args->index < ichdr.count); | |
1998 | ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) + | |
1999 | xfs_attr3_leaf_hdr_size(leaf)); | |
2000 | ||
2001 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; | |
2002 | ||
2003 | ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused); | |
6b19f2d8 | 2004 | ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp)); |
1da177e4 LT |
2005 | |
2006 | /* | |
2007 | * Scan through free region table: | |
2008 | * check for adjacency of free'd entry with an existing one, | |
2009 | * find smallest free region in case we need to replace it, | |
2010 | * adjust any map that borders the entry table, | |
2011 | */ | |
517c2220 DC |
2012 | tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t) |
2013 | + xfs_attr3_leaf_hdr_size(leaf); | |
2014 | tmp = ichdr.freemap[0].size; | |
1da177e4 LT |
2015 | before = after = -1; |
2016 | smallest = XFS_ATTR_LEAF_MAPSIZE - 1; | |
2017 | entsize = xfs_attr_leaf_entsize(leaf, args->index); | |
517c2220 DC |
2018 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { |
2019 | ASSERT(ichdr.freemap[i].base < XFS_LBSIZE(mp)); | |
2020 | ASSERT(ichdr.freemap[i].size < XFS_LBSIZE(mp)); | |
2021 | if (ichdr.freemap[i].base == tablesize) { | |
2022 | ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t); | |
2023 | ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t); | |
1da177e4 LT |
2024 | } |
2025 | ||
517c2220 DC |
2026 | if (ichdr.freemap[i].base + ichdr.freemap[i].size == |
2027 | be16_to_cpu(entry->nameidx)) { | |
1da177e4 | 2028 | before = i; |
517c2220 DC |
2029 | } else if (ichdr.freemap[i].base == |
2030 | (be16_to_cpu(entry->nameidx) + entsize)) { | |
1da177e4 | 2031 | after = i; |
517c2220 DC |
2032 | } else if (ichdr.freemap[i].size < tmp) { |
2033 | tmp = ichdr.freemap[i].size; | |
1da177e4 LT |
2034 | smallest = i; |
2035 | } | |
2036 | } | |
2037 | ||
2038 | /* | |
2039 | * Coalesce adjacent freemap regions, | |
2040 | * or replace the smallest region. | |
2041 | */ | |
2042 | if ((before >= 0) || (after >= 0)) { | |
2043 | if ((before >= 0) && (after >= 0)) { | |
517c2220 DC |
2044 | ichdr.freemap[before].size += entsize; |
2045 | ichdr.freemap[before].size += ichdr.freemap[after].size; | |
2046 | ichdr.freemap[after].base = 0; | |
2047 | ichdr.freemap[after].size = 0; | |
1da177e4 | 2048 | } else if (before >= 0) { |
517c2220 | 2049 | ichdr.freemap[before].size += entsize; |
1da177e4 | 2050 | } else { |
517c2220 DC |
2051 | ichdr.freemap[after].base = be16_to_cpu(entry->nameidx); |
2052 | ichdr.freemap[after].size += entsize; | |
1da177e4 LT |
2053 | } |
2054 | } else { | |
2055 | /* | |
2056 | * Replace smallest region (if it is smaller than free'd entry) | |
2057 | */ | |
517c2220 DC |
2058 | if (ichdr.freemap[smallest].size < entsize) { |
2059 | ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx); | |
2060 | ichdr.freemap[smallest].size = entsize; | |
1da177e4 LT |
2061 | } |
2062 | } | |
2063 | ||
2064 | /* | |
2065 | * Did we remove the first entry? | |
2066 | */ | |
517c2220 | 2067 | if (be16_to_cpu(entry->nameidx) == ichdr.firstused) |
1da177e4 LT |
2068 | smallest = 1; |
2069 | else | |
2070 | smallest = 0; | |
2071 | ||
2072 | /* | |
2073 | * Compress the remaining entries and zero out the removed stuff. | |
2074 | */ | |
517c2220 DC |
2075 | memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize); |
2076 | ichdr.usedbytes -= entsize; | |
1d9025e5 | 2077 | xfs_trans_log_buf(args->trans, bp, |
517c2220 | 2078 | XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index), |
1da177e4 LT |
2079 | entsize)); |
2080 | ||
517c2220 DC |
2081 | tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t); |
2082 | memmove(entry, entry + 1, tmp); | |
2083 | ichdr.count--; | |
1d9025e5 | 2084 | xfs_trans_log_buf(args->trans, bp, |
517c2220 DC |
2085 | XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t))); |
2086 | ||
2087 | entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count]; | |
2088 | memset(entry, 0, sizeof(xfs_attr_leaf_entry_t)); | |
1da177e4 LT |
2089 | |
2090 | /* | |
2091 | * If we removed the first entry, re-find the first used byte | |
2092 | * in the name area. Note that if the entry was the "firstused", | |
2093 | * then we don't have a "hole" in our block resulting from | |
2094 | * removing the name. | |
2095 | */ | |
2096 | if (smallest) { | |
2097 | tmp = XFS_LBSIZE(mp); | |
517c2220 DC |
2098 | entry = xfs_attr3_leaf_entryp(leaf); |
2099 | for (i = ichdr.count - 1; i >= 0; entry++, i--) { | |
2100 | ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused); | |
6b19f2d8 NS |
2101 | ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp)); |
2102 | ||
2103 | if (be16_to_cpu(entry->nameidx) < tmp) | |
2104 | tmp = be16_to_cpu(entry->nameidx); | |
1da177e4 | 2105 | } |
517c2220 DC |
2106 | ichdr.firstused = tmp; |
2107 | if (!ichdr.firstused) | |
2108 | ichdr.firstused = tmp - XFS_ATTR_LEAF_NAME_ALIGN; | |
1da177e4 | 2109 | } else { |
517c2220 | 2110 | ichdr.holes = 1; /* mark as needing compaction */ |
1da177e4 | 2111 | } |
517c2220 | 2112 | xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr); |
1d9025e5 | 2113 | xfs_trans_log_buf(args->trans, bp, |
517c2220 DC |
2114 | XFS_DA_LOGRANGE(leaf, &leaf->hdr, |
2115 | xfs_attr3_leaf_hdr_size(leaf))); | |
1da177e4 LT |
2116 | |
2117 | /* | |
2118 | * Check if leaf is less than 50% full, caller may want to | |
2119 | * "join" the leaf with a sibling if so. | |
2120 | */ | |
517c2220 DC |
2121 | tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) + |
2122 | ichdr.count * sizeof(xfs_attr_leaf_entry_t); | |
2123 | ||
2124 | return tmp < mp->m_attr_magicpct; /* leaf is < 37% full */ | |
1da177e4 LT |
2125 | } |
2126 | ||
2127 | /* | |
2128 | * Move all the attribute list entries from drop_leaf into save_leaf. | |
2129 | */ | |
2130 | void | |
517c2220 DC |
2131 | xfs_attr3_leaf_unbalance( |
2132 | struct xfs_da_state *state, | |
2133 | struct xfs_da_state_blk *drop_blk, | |
2134 | struct xfs_da_state_blk *save_blk) | |
1da177e4 | 2135 | { |
517c2220 DC |
2136 | struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr; |
2137 | struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr; | |
2138 | struct xfs_attr3_icleaf_hdr drophdr; | |
2139 | struct xfs_attr3_icleaf_hdr savehdr; | |
2140 | struct xfs_attr_leaf_entry *entry; | |
2141 | struct xfs_mount *mp = state->mp; | |
1da177e4 | 2142 | |
5a5881cd DC |
2143 | trace_xfs_attr_leaf_unbalance(state->args); |
2144 | ||
1d9025e5 DC |
2145 | drop_leaf = drop_blk->bp->b_addr; |
2146 | save_leaf = save_blk->bp->b_addr; | |
517c2220 DC |
2147 | xfs_attr3_leaf_hdr_from_disk(&drophdr, drop_leaf); |
2148 | xfs_attr3_leaf_hdr_from_disk(&savehdr, save_leaf); | |
2149 | entry = xfs_attr3_leaf_entryp(drop_leaf); | |
1da177e4 LT |
2150 | |
2151 | /* | |
2152 | * Save last hashval from dying block for later Btree fixup. | |
2153 | */ | |
517c2220 | 2154 | drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval); |
1da177e4 LT |
2155 | |
2156 | /* | |
2157 | * Check if we need a temp buffer, or can we do it in place. | |
2158 | * Note that we don't check "leaf" for holes because we will | |
2159 | * always be dropping it, toosmall() decided that for us already. | |
2160 | */ | |
517c2220 | 2161 | if (savehdr.holes == 0) { |
1da177e4 LT |
2162 | /* |
2163 | * dest leaf has no holes, so we add there. May need | |
2164 | * to make some room in the entry array. | |
2165 | */ | |
517c2220 DC |
2166 | if (xfs_attr3_leaf_order(save_blk->bp, &savehdr, |
2167 | drop_blk->bp, &drophdr)) { | |
2168 | xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0, | |
2169 | save_leaf, &savehdr, 0, | |
2170 | drophdr.count, mp); | |
1da177e4 | 2171 | } else { |
517c2220 DC |
2172 | xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0, |
2173 | save_leaf, &savehdr, | |
2174 | savehdr.count, drophdr.count, mp); | |
1da177e4 LT |
2175 | } |
2176 | } else { | |
2177 | /* | |
2178 | * Destination has holes, so we make a temporary copy | |
2179 | * of the leaf and add them both to that. | |
2180 | */ | |
517c2220 DC |
2181 | struct xfs_attr_leafblock *tmp_leaf; |
2182 | struct xfs_attr3_icleaf_hdr tmphdr; | |
2183 | ||
8517de2a DC |
2184 | tmp_leaf = kmem_zalloc(state->blocksize, KM_SLEEP); |
2185 | ||
2186 | /* | |
2187 | * Copy the header into the temp leaf so that all the stuff | |
2188 | * not in the incore header is present and gets copied back in | |
2189 | * once we've moved all the entries. | |
2190 | */ | |
2191 | memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf)); | |
517c2220 | 2192 | |
8517de2a | 2193 | memset(&tmphdr, 0, sizeof(tmphdr)); |
517c2220 DC |
2194 | tmphdr.magic = savehdr.magic; |
2195 | tmphdr.forw = savehdr.forw; | |
2196 | tmphdr.back = savehdr.back; | |
2197 | tmphdr.firstused = state->blocksize; | |
8517de2a DC |
2198 | |
2199 | /* write the header to the temp buffer to initialise it */ | |
2200 | xfs_attr3_leaf_hdr_to_disk(tmp_leaf, &tmphdr); | |
2201 | ||
517c2220 DC |
2202 | if (xfs_attr3_leaf_order(save_blk->bp, &savehdr, |
2203 | drop_blk->bp, &drophdr)) { | |
2204 | xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0, | |
2205 | tmp_leaf, &tmphdr, 0, | |
2206 | drophdr.count, mp); | |
2207 | xfs_attr3_leaf_moveents(save_leaf, &savehdr, 0, | |
2208 | tmp_leaf, &tmphdr, tmphdr.count, | |
2209 | savehdr.count, mp); | |
1da177e4 | 2210 | } else { |
517c2220 DC |
2211 | xfs_attr3_leaf_moveents(save_leaf, &savehdr, 0, |
2212 | tmp_leaf, &tmphdr, 0, | |
2213 | savehdr.count, mp); | |
2214 | xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0, | |
2215 | tmp_leaf, &tmphdr, tmphdr.count, | |
2216 | drophdr.count, mp); | |
1da177e4 | 2217 | } |
517c2220 DC |
2218 | memcpy(save_leaf, tmp_leaf, state->blocksize); |
2219 | savehdr = tmphdr; /* struct copy */ | |
2220 | kmem_free(tmp_leaf); | |
1da177e4 LT |
2221 | } |
2222 | ||
517c2220 | 2223 | xfs_attr3_leaf_hdr_to_disk(save_leaf, &savehdr); |
1d9025e5 | 2224 | xfs_trans_log_buf(state->args->trans, save_blk->bp, 0, |
1da177e4 LT |
2225 | state->blocksize - 1); |
2226 | ||
2227 | /* | |
2228 | * Copy out last hashval in each block for B-tree code. | |
2229 | */ | |
517c2220 DC |
2230 | entry = xfs_attr3_leaf_entryp(save_leaf); |
2231 | save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval); | |
1da177e4 LT |
2232 | } |
2233 | ||
2234 | /*======================================================================== | |
2235 | * Routines used for finding things in the Btree. | |
2236 | *========================================================================*/ | |
2237 | ||
2238 | /* | |
2239 | * Look up a name in a leaf attribute list structure. | |
2240 | * This is the internal routine, it uses the caller's buffer. | |
2241 | * | |
2242 | * Note that duplicate keys are allowed, but only check within the | |
2243 | * current leaf node. The Btree code must check in adjacent leaf nodes. | |
2244 | * | |
2245 | * Return in args->index the index into the entry[] array of either | |
2246 | * the found entry, or where the entry should have been (insert before | |
2247 | * that entry). | |
2248 | * | |
2249 | * Don't change the args->value unless we find the attribute. | |
2250 | */ | |
2251 | int | |
517c2220 DC |
2252 | xfs_attr3_leaf_lookup_int( |
2253 | struct xfs_buf *bp, | |
2254 | struct xfs_da_args *args) | |
1da177e4 | 2255 | { |
517c2220 DC |
2256 | struct xfs_attr_leafblock *leaf; |
2257 | struct xfs_attr3_icleaf_hdr ichdr; | |
2258 | struct xfs_attr_leaf_entry *entry; | |
2259 | struct xfs_attr_leaf_entry *entries; | |
2260 | struct xfs_attr_leaf_name_local *name_loc; | |
2261 | struct xfs_attr_leaf_name_remote *name_rmt; | |
2262 | xfs_dahash_t hashval; | |
2263 | int probe; | |
2264 | int span; | |
1da177e4 | 2265 | |
5a5881cd DC |
2266 | trace_xfs_attr_leaf_lookup(args); |
2267 | ||
1d9025e5 | 2268 | leaf = bp->b_addr; |
517c2220 DC |
2269 | xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf); |
2270 | entries = xfs_attr3_leaf_entryp(leaf); | |
2271 | ASSERT(ichdr.count < XFS_LBSIZE(args->dp->i_mount) / 8); | |
1da177e4 LT |
2272 | |
2273 | /* | |
2274 | * Binary search. (note: small blocks will skip this loop) | |
2275 | */ | |
2276 | hashval = args->hashval; | |
517c2220 DC |
2277 | probe = span = ichdr.count / 2; |
2278 | for (entry = &entries[probe]; span > 4; entry = &entries[probe]) { | |
1da177e4 | 2279 | span /= 2; |
6b19f2d8 | 2280 | if (be32_to_cpu(entry->hashval) < hashval) |
1da177e4 | 2281 | probe += span; |
6b19f2d8 | 2282 | else if (be32_to_cpu(entry->hashval) > hashval) |
1da177e4 LT |
2283 | probe -= span; |
2284 | else | |
2285 | break; | |
2286 | } | |
517c2220 DC |
2287 | ASSERT(probe >= 0 && (!ichdr.count || probe < ichdr.count)); |
2288 | ASSERT(span <= 4 || be32_to_cpu(entry->hashval) == hashval); | |
1da177e4 LT |
2289 | |
2290 | /* | |
2291 | * Since we may have duplicate hashval's, find the first matching | |
2292 | * hashval in the leaf. | |
2293 | */ | |
517c2220 | 2294 | while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) { |
1da177e4 LT |
2295 | entry--; |
2296 | probe--; | |
2297 | } | |
517c2220 DC |
2298 | while (probe < ichdr.count && |
2299 | be32_to_cpu(entry->hashval) < hashval) { | |
1da177e4 LT |
2300 | entry++; |
2301 | probe++; | |
2302 | } | |
517c2220 | 2303 | if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) { |
1da177e4 | 2304 | args->index = probe; |
517c2220 | 2305 | return XFS_ERROR(ENOATTR); |
1da177e4 LT |
2306 | } |
2307 | ||
2308 | /* | |
2309 | * Duplicate keys may be present, so search all of them for a match. | |
2310 | */ | |
517c2220 | 2311 | for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval); |
1da177e4 LT |
2312 | entry++, probe++) { |
2313 | /* | |
2314 | * GROT: Add code to remove incomplete entries. | |
2315 | */ | |
2316 | /* | |
2317 | * If we are looking for INCOMPLETE entries, show only those. | |
2318 | * If we are looking for complete entries, show only those. | |
2319 | */ | |
2320 | if ((args->flags & XFS_ATTR_INCOMPLETE) != | |
2321 | (entry->flags & XFS_ATTR_INCOMPLETE)) { | |
2322 | continue; | |
2323 | } | |
2324 | if (entry->flags & XFS_ATTR_LOCAL) { | |
517c2220 | 2325 | name_loc = xfs_attr3_leaf_name_local(leaf, probe); |
1da177e4 LT |
2326 | if (name_loc->namelen != args->namelen) |
2327 | continue; | |
517c2220 DC |
2328 | if (memcmp(args->name, name_loc->nameval, |
2329 | args->namelen) != 0) | |
1da177e4 | 2330 | continue; |
726801ba | 2331 | if (!xfs_attr_namesp_match(args->flags, entry->flags)) |
1da177e4 LT |
2332 | continue; |
2333 | args->index = probe; | |
517c2220 | 2334 | return XFS_ERROR(EEXIST); |
1da177e4 | 2335 | } else { |
517c2220 | 2336 | name_rmt = xfs_attr3_leaf_name_remote(leaf, probe); |
1da177e4 LT |
2337 | if (name_rmt->namelen != args->namelen) |
2338 | continue; | |
517c2220 DC |
2339 | if (memcmp(args->name, name_rmt->name, |
2340 | args->namelen) != 0) | |
1da177e4 | 2341 | continue; |
726801ba | 2342 | if (!xfs_attr_namesp_match(args->flags, entry->flags)) |
1da177e4 LT |
2343 | continue; |
2344 | args->index = probe; | |
e461fcb1 | 2345 | args->valuelen = be32_to_cpu(name_rmt->valuelen); |
c0f054e7 | 2346 | args->rmtblkno = be32_to_cpu(name_rmt->valueblk); |
1da177e4 | 2347 | args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, |
e461fcb1 | 2348 | args->valuelen); |
517c2220 | 2349 | return XFS_ERROR(EEXIST); |
1da177e4 LT |
2350 | } |
2351 | } | |
2352 | args->index = probe; | |
517c2220 | 2353 | return XFS_ERROR(ENOATTR); |
1da177e4 LT |
2354 | } |
2355 | ||
2356 | /* | |
2357 | * Get the value associated with an attribute name from a leaf attribute | |
2358 | * list structure. | |
2359 | */ | |
2360 | int | |
517c2220 DC |
2361 | xfs_attr3_leaf_getvalue( |
2362 | struct xfs_buf *bp, | |
2363 | struct xfs_da_args *args) | |
1da177e4 | 2364 | { |
517c2220 DC |
2365 | struct xfs_attr_leafblock *leaf; |
2366 | struct xfs_attr3_icleaf_hdr ichdr; | |
2367 | struct xfs_attr_leaf_entry *entry; | |
2368 | struct xfs_attr_leaf_name_local *name_loc; | |
2369 | struct xfs_attr_leaf_name_remote *name_rmt; | |
2370 | int valuelen; | |
1da177e4 | 2371 | |
1d9025e5 | 2372 | leaf = bp->b_addr; |
517c2220 DC |
2373 | xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf); |
2374 | ASSERT(ichdr.count < XFS_LBSIZE(args->dp->i_mount) / 8); | |
2375 | ASSERT(args->index < ichdr.count); | |
1da177e4 | 2376 | |
517c2220 | 2377 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; |
1da177e4 | 2378 | if (entry->flags & XFS_ATTR_LOCAL) { |
517c2220 | 2379 | name_loc = xfs_attr3_leaf_name_local(leaf, args->index); |
1da177e4 LT |
2380 | ASSERT(name_loc->namelen == args->namelen); |
2381 | ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0); | |
053b5758 | 2382 | valuelen = be16_to_cpu(name_loc->valuelen); |
1da177e4 LT |
2383 | if (args->flags & ATTR_KERNOVAL) { |
2384 | args->valuelen = valuelen; | |
517c2220 | 2385 | return 0; |
1da177e4 LT |
2386 | } |
2387 | if (args->valuelen < valuelen) { | |
2388 | args->valuelen = valuelen; | |
517c2220 | 2389 | return XFS_ERROR(ERANGE); |
1da177e4 LT |
2390 | } |
2391 | args->valuelen = valuelen; | |
2392 | memcpy(args->value, &name_loc->nameval[args->namelen], valuelen); | |
2393 | } else { | |
517c2220 | 2394 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); |
1da177e4 LT |
2395 | ASSERT(name_rmt->namelen == args->namelen); |
2396 | ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0); | |
c0f054e7 NS |
2397 | valuelen = be32_to_cpu(name_rmt->valuelen); |
2398 | args->rmtblkno = be32_to_cpu(name_rmt->valueblk); | |
1da177e4 LT |
2399 | args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen); |
2400 | if (args->flags & ATTR_KERNOVAL) { | |
2401 | args->valuelen = valuelen; | |
517c2220 | 2402 | return 0; |
1da177e4 LT |
2403 | } |
2404 | if (args->valuelen < valuelen) { | |
2405 | args->valuelen = valuelen; | |
517c2220 | 2406 | return XFS_ERROR(ERANGE); |
1da177e4 LT |
2407 | } |
2408 | args->valuelen = valuelen; | |
2409 | } | |
517c2220 | 2410 | return 0; |
1da177e4 LT |
2411 | } |
2412 | ||
2413 | /*======================================================================== | |
2414 | * Utility routines. | |
2415 | *========================================================================*/ | |
2416 | ||
2417 | /* | |
2418 | * Move the indicated entries from one leaf to another. | |
2419 | * NOTE: this routine modifies both source and destination leaves. | |
2420 | */ | |
2421 | /*ARGSUSED*/ | |
2422 | STATIC void | |
517c2220 DC |
2423 | xfs_attr3_leaf_moveents( |
2424 | struct xfs_attr_leafblock *leaf_s, | |
2425 | struct xfs_attr3_icleaf_hdr *ichdr_s, | |
2426 | int start_s, | |
2427 | struct xfs_attr_leafblock *leaf_d, | |
2428 | struct xfs_attr3_icleaf_hdr *ichdr_d, | |
2429 | int start_d, | |
2430 | int count, | |
2431 | struct xfs_mount *mp) | |
1da177e4 | 2432 | { |
517c2220 DC |
2433 | struct xfs_attr_leaf_entry *entry_s; |
2434 | struct xfs_attr_leaf_entry *entry_d; | |
2435 | int desti; | |
2436 | int tmp; | |
2437 | int i; | |
1da177e4 LT |
2438 | |
2439 | /* | |
2440 | * Check for nothing to do. | |
2441 | */ | |
2442 | if (count == 0) | |
2443 | return; | |
2444 | ||
2445 | /* | |
2446 | * Set up environment. | |
2447 | */ | |
517c2220 DC |
2448 | ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC || |
2449 | ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC); | |
2450 | ASSERT(ichdr_s->magic == ichdr_d->magic); | |
2451 | ASSERT(ichdr_s->count > 0 && ichdr_s->count < XFS_LBSIZE(mp) / 8); | |
2452 | ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s)) | |
2453 | + xfs_attr3_leaf_hdr_size(leaf_s)); | |
2454 | ASSERT(ichdr_d->count < XFS_LBSIZE(mp) / 8); | |
2455 | ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d)) | |
2456 | + xfs_attr3_leaf_hdr_size(leaf_d)); | |
2457 | ||
2458 | ASSERT(start_s < ichdr_s->count); | |
2459 | ASSERT(start_d <= ichdr_d->count); | |
2460 | ASSERT(count <= ichdr_s->count); | |
2461 | ||
1da177e4 LT |
2462 | |
2463 | /* | |
2464 | * Move the entries in the destination leaf up to make a hole? | |
2465 | */ | |
517c2220 DC |
2466 | if (start_d < ichdr_d->count) { |
2467 | tmp = ichdr_d->count - start_d; | |
1da177e4 | 2468 | tmp *= sizeof(xfs_attr_leaf_entry_t); |
517c2220 DC |
2469 | entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d]; |
2470 | entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count]; | |
2471 | memmove(entry_d, entry_s, tmp); | |
1da177e4 LT |
2472 | } |
2473 | ||
2474 | /* | |
2475 | * Copy all entry's in the same (sorted) order, | |
2476 | * but allocate attribute info packed and in sequence. | |
2477 | */ | |
517c2220 DC |
2478 | entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s]; |
2479 | entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d]; | |
1da177e4 LT |
2480 | desti = start_d; |
2481 | for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) { | |
517c2220 | 2482 | ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused); |
1da177e4 LT |
2483 | tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i); |
2484 | #ifdef GROT | |
2485 | /* | |
2486 | * Code to drop INCOMPLETE entries. Difficult to use as we | |
2487 | * may also need to change the insertion index. Code turned | |
2488 | * off for 6.2, should be revisited later. | |
2489 | */ | |
2490 | if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */ | |
517c2220 DC |
2491 | memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp); |
2492 | ichdr_s->usedbytes -= tmp; | |
2493 | ichdr_s->count -= 1; | |
1da177e4 LT |
2494 | entry_d--; /* to compensate for ++ in loop hdr */ |
2495 | desti--; | |
2496 | if ((start_s + i) < offset) | |
2497 | result++; /* insertion index adjustment */ | |
2498 | } else { | |
2499 | #endif /* GROT */ | |
517c2220 | 2500 | ichdr_d->firstused -= tmp; |
1da177e4 LT |
2501 | /* both on-disk, don't endian flip twice */ |
2502 | entry_d->hashval = entry_s->hashval; | |
517c2220 | 2503 | entry_d->nameidx = cpu_to_be16(ichdr_d->firstused); |
1da177e4 | 2504 | entry_d->flags = entry_s->flags; |
6b19f2d8 | 2505 | ASSERT(be16_to_cpu(entry_d->nameidx) + tmp |
1da177e4 | 2506 | <= XFS_LBSIZE(mp)); |
517c2220 DC |
2507 | memmove(xfs_attr3_leaf_name(leaf_d, desti), |
2508 | xfs_attr3_leaf_name(leaf_s, start_s + i), tmp); | |
6b19f2d8 | 2509 | ASSERT(be16_to_cpu(entry_s->nameidx) + tmp |
1da177e4 | 2510 | <= XFS_LBSIZE(mp)); |
517c2220 DC |
2511 | memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp); |
2512 | ichdr_s->usedbytes -= tmp; | |
2513 | ichdr_d->usedbytes += tmp; | |
2514 | ichdr_s->count -= 1; | |
2515 | ichdr_d->count += 1; | |
2516 | tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t) | |
2517 | + xfs_attr3_leaf_hdr_size(leaf_d); | |
2518 | ASSERT(ichdr_d->firstused >= tmp); | |
1da177e4 LT |
2519 | #ifdef GROT |
2520 | } | |
2521 | #endif /* GROT */ | |
2522 | } | |
2523 | ||
2524 | /* | |
2525 | * Zero out the entries we just copied. | |
2526 | */ | |
517c2220 | 2527 | if (start_s == ichdr_s->count) { |
1da177e4 | 2528 | tmp = count * sizeof(xfs_attr_leaf_entry_t); |
517c2220 | 2529 | entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s]; |
1da177e4 LT |
2530 | ASSERT(((char *)entry_s + tmp) <= |
2531 | ((char *)leaf_s + XFS_LBSIZE(mp))); | |
517c2220 | 2532 | memset(entry_s, 0, tmp); |
1da177e4 LT |
2533 | } else { |
2534 | /* | |
2535 | * Move the remaining entries down to fill the hole, | |
2536 | * then zero the entries at the top. | |
2537 | */ | |
517c2220 DC |
2538 | tmp = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t); |
2539 | entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count]; | |
2540 | entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s]; | |
2541 | memmove(entry_d, entry_s, tmp); | |
1da177e4 LT |
2542 | |
2543 | tmp = count * sizeof(xfs_attr_leaf_entry_t); | |
517c2220 | 2544 | entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count]; |
1da177e4 LT |
2545 | ASSERT(((char *)entry_s + tmp) <= |
2546 | ((char *)leaf_s + XFS_LBSIZE(mp))); | |
517c2220 | 2547 | memset(entry_s, 0, tmp); |
1da177e4 LT |
2548 | } |
2549 | ||
2550 | /* | |
2551 | * Fill in the freemap information | |
2552 | */ | |
517c2220 DC |
2553 | ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d); |
2554 | ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t); | |
2555 | ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base; | |
2556 | ichdr_d->freemap[1].base = 0; | |
2557 | ichdr_d->freemap[2].base = 0; | |
2558 | ichdr_d->freemap[1].size = 0; | |
2559 | ichdr_d->freemap[2].size = 0; | |
2560 | ichdr_s->holes = 1; /* leaf may not be compact */ | |
1da177e4 LT |
2561 | } |
2562 | ||
2563 | /* | |
2564 | * Pick up the last hashvalue from a leaf block. | |
2565 | */ | |
2566 | xfs_dahash_t | |
1d9025e5 DC |
2567 | xfs_attr_leaf_lasthash( |
2568 | struct xfs_buf *bp, | |
2569 | int *count) | |
1da177e4 | 2570 | { |
517c2220 DC |
2571 | struct xfs_attr3_icleaf_hdr ichdr; |
2572 | struct xfs_attr_leaf_entry *entries; | |
1da177e4 | 2573 | |
517c2220 DC |
2574 | xfs_attr3_leaf_hdr_from_disk(&ichdr, bp->b_addr); |
2575 | entries = xfs_attr3_leaf_entryp(bp->b_addr); | |
1da177e4 | 2576 | if (count) |
517c2220 DC |
2577 | *count = ichdr.count; |
2578 | if (!ichdr.count) | |
2579 | return 0; | |
2580 | return be32_to_cpu(entries[ichdr.count - 1].hashval); | |
1da177e4 LT |
2581 | } |
2582 | ||
2583 | /* | |
2584 | * Calculate the number of bytes used to store the indicated attribute | |
2585 | * (whether local or remote only calculate bytes in this block). | |
2586 | */ | |
ba0f32d4 | 2587 | STATIC int |
1da177e4 LT |
2588 | xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index) |
2589 | { | |
517c2220 | 2590 | struct xfs_attr_leaf_entry *entries; |
1da177e4 LT |
2591 | xfs_attr_leaf_name_local_t *name_loc; |
2592 | xfs_attr_leaf_name_remote_t *name_rmt; | |
2593 | int size; | |
2594 | ||
517c2220 DC |
2595 | entries = xfs_attr3_leaf_entryp(leaf); |
2596 | if (entries[index].flags & XFS_ATTR_LOCAL) { | |
2597 | name_loc = xfs_attr3_leaf_name_local(leaf, index); | |
c9fb86a9 | 2598 | size = xfs_attr_leaf_entsize_local(name_loc->namelen, |
053b5758 | 2599 | be16_to_cpu(name_loc->valuelen)); |
1da177e4 | 2600 | } else { |
517c2220 | 2601 | name_rmt = xfs_attr3_leaf_name_remote(leaf, index); |
c9fb86a9 | 2602 | size = xfs_attr_leaf_entsize_remote(name_rmt->namelen); |
1da177e4 | 2603 | } |
517c2220 | 2604 | return size; |
1da177e4 LT |
2605 | } |
2606 | ||
2607 | /* | |
2608 | * Calculate the number of bytes that would be required to store the new | |
2609 | * attribute (whether local or remote only calculate bytes in this block). | |
2610 | * This routine decides as a side effect whether the attribute will be | |
2611 | * a "local" or a "remote" attribute. | |
2612 | */ | |
2613 | int | |
aa82daa0 | 2614 | xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local) |
1da177e4 LT |
2615 | { |
2616 | int size; | |
2617 | ||
c9fb86a9 ES |
2618 | size = xfs_attr_leaf_entsize_local(namelen, valuelen); |
2619 | if (size < xfs_attr_leaf_entsize_local_max(blocksize)) { | |
1da177e4 LT |
2620 | if (local) { |
2621 | *local = 1; | |
2622 | } | |
2623 | } else { | |
c9fb86a9 | 2624 | size = xfs_attr_leaf_entsize_remote(namelen); |
1da177e4 LT |
2625 | if (local) { |
2626 | *local = 0; | |
2627 | } | |
2628 | } | |
517c2220 | 2629 | return size; |
1da177e4 LT |
2630 | } |
2631 | ||
2632 | /* | |
2633 | * Copy out attribute list entries for attr_list(), for leaf attribute lists. | |
2634 | */ | |
2635 | int | |
517c2220 DC |
2636 | xfs_attr3_leaf_list_int( |
2637 | struct xfs_buf *bp, | |
2638 | struct xfs_attr_list_context *context) | |
1da177e4 | 2639 | { |
517c2220 DC |
2640 | struct attrlist_cursor_kern *cursor; |
2641 | struct xfs_attr_leafblock *leaf; | |
2642 | struct xfs_attr3_icleaf_hdr ichdr; | |
2643 | struct xfs_attr_leaf_entry *entries; | |
2644 | struct xfs_attr_leaf_entry *entry; | |
2645 | int retval; | |
2646 | int i; | |
2647 | ||
2648 | trace_xfs_attr_list_leaf(context); | |
1da177e4 | 2649 | |
1d9025e5 | 2650 | leaf = bp->b_addr; |
517c2220 DC |
2651 | xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf); |
2652 | entries = xfs_attr3_leaf_entryp(leaf); | |
2653 | ||
1da177e4 LT |
2654 | cursor = context->cursor; |
2655 | cursor->initted = 1; | |
2656 | ||
1da177e4 LT |
2657 | /* |
2658 | * Re-find our place in the leaf block if this is a new syscall. | |
2659 | */ | |
2660 | if (context->resynch) { | |
517c2220 DC |
2661 | entry = &entries[0]; |
2662 | for (i = 0; i < ichdr.count; entry++, i++) { | |
6b19f2d8 | 2663 | if (be32_to_cpu(entry->hashval) == cursor->hashval) { |
1da177e4 LT |
2664 | if (cursor->offset == context->dupcnt) { |
2665 | context->dupcnt = 0; | |
2666 | break; | |
2667 | } | |
2668 | context->dupcnt++; | |
6b19f2d8 NS |
2669 | } else if (be32_to_cpu(entry->hashval) > |
2670 | cursor->hashval) { | |
1da177e4 LT |
2671 | context->dupcnt = 0; |
2672 | break; | |
2673 | } | |
2674 | } | |
517c2220 | 2675 | if (i == ichdr.count) { |
0b1b213f | 2676 | trace_xfs_attr_list_notfound(context); |
517c2220 | 2677 | return 0; |
1da177e4 LT |
2678 | } |
2679 | } else { | |
517c2220 | 2680 | entry = &entries[0]; |
1da177e4 LT |
2681 | i = 0; |
2682 | } | |
2683 | context->resynch = 0; | |
2684 | ||
2685 | /* | |
2686 | * We have found our place, start copying out the new attributes. | |
2687 | */ | |
2688 | retval = 0; | |
517c2220 | 2689 | for (; i < ichdr.count; entry++, i++) { |
6b19f2d8 NS |
2690 | if (be32_to_cpu(entry->hashval) != cursor->hashval) { |
2691 | cursor->hashval = be32_to_cpu(entry->hashval); | |
1da177e4 LT |
2692 | cursor->offset = 0; |
2693 | } | |
2694 | ||
2695 | if (entry->flags & XFS_ATTR_INCOMPLETE) | |
2696 | continue; /* skip incomplete entries */ | |
1da177e4 LT |
2697 | |
2698 | if (entry->flags & XFS_ATTR_LOCAL) { | |
726801ba | 2699 | xfs_attr_leaf_name_local_t *name_loc = |
517c2220 | 2700 | xfs_attr3_leaf_name_local(leaf, i); |
726801ba TS |
2701 | |
2702 | retval = context->put_listent(context, | |
ad9b463a | 2703 | entry->flags, |
a9273ca5 | 2704 | name_loc->nameval, |
726801ba TS |
2705 | (int)name_loc->namelen, |
2706 | be16_to_cpu(name_loc->valuelen), | |
a9273ca5 | 2707 | &name_loc->nameval[name_loc->namelen]); |
726801ba TS |
2708 | if (retval) |
2709 | return retval; | |
1da177e4 | 2710 | } else { |
726801ba | 2711 | xfs_attr_leaf_name_remote_t *name_rmt = |
517c2220 | 2712 | xfs_attr3_leaf_name_remote(leaf, i); |
726801ba TS |
2713 | |
2714 | int valuelen = be32_to_cpu(name_rmt->valuelen); | |
2715 | ||
2716 | if (context->put_value) { | |
2717 | xfs_da_args_t args; | |
2718 | ||
2719 | memset((char *)&args, 0, sizeof(args)); | |
2720 | args.dp = context->dp; | |
2721 | args.whichfork = XFS_ATTR_FORK; | |
2722 | args.valuelen = valuelen; | |
622d8149 | 2723 | args.value = kmem_alloc(valuelen, KM_SLEEP | KM_NOFS); |
726801ba TS |
2724 | args.rmtblkno = be32_to_cpu(name_rmt->valueblk); |
2725 | args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen); | |
2726 | retval = xfs_attr_rmtval_get(&args); | |
2727 | if (retval) | |
2728 | return retval; | |
2729 | retval = context->put_listent(context, | |
ad9b463a | 2730 | entry->flags, |
a9273ca5 | 2731 | name_rmt->name, |
726801ba TS |
2732 | (int)name_rmt->namelen, |
2733 | valuelen, | |
a9273ca5 | 2734 | args.value); |
f0e2d93c | 2735 | kmem_free(args.value); |
ad9b463a | 2736 | } else { |
726801ba | 2737 | retval = context->put_listent(context, |
ad9b463a | 2738 | entry->flags, |
a9273ca5 | 2739 | name_rmt->name, |
726801ba TS |
2740 | (int)name_rmt->namelen, |
2741 | valuelen, | |
2742 | NULL); | |
2743 | } | |
2744 | if (retval) | |
2745 | return retval; | |
1da177e4 | 2746 | } |
726801ba TS |
2747 | if (context->seen_enough) |
2748 | break; | |
2749 | cursor->offset++; | |
1da177e4 | 2750 | } |
0b1b213f | 2751 | trace_xfs_attr_list_leaf_end(context); |
517c2220 | 2752 | return retval; |
1da177e4 LT |
2753 | } |
2754 | ||
1da177e4 LT |
2755 | |
2756 | /*======================================================================== | |
2757 | * Manage the INCOMPLETE flag in a leaf entry | |
2758 | *========================================================================*/ | |
2759 | ||
2760 | /* | |
2761 | * Clear the INCOMPLETE flag on an entry in a leaf block. | |
2762 | */ | |
2763 | int | |
517c2220 DC |
2764 | xfs_attr3_leaf_clearflag( |
2765 | struct xfs_da_args *args) | |
1da177e4 | 2766 | { |
517c2220 DC |
2767 | struct xfs_attr_leafblock *leaf; |
2768 | struct xfs_attr_leaf_entry *entry; | |
2769 | struct xfs_attr_leaf_name_remote *name_rmt; | |
2770 | struct xfs_buf *bp; | |
2771 | int error; | |
1da177e4 | 2772 | #ifdef DEBUG |
517c2220 | 2773 | struct xfs_attr3_icleaf_hdr ichdr; |
1da177e4 LT |
2774 | xfs_attr_leaf_name_local_t *name_loc; |
2775 | int namelen; | |
2776 | char *name; | |
2777 | #endif /* DEBUG */ | |
2778 | ||
5a5881cd | 2779 | trace_xfs_attr_leaf_clearflag(args); |
1da177e4 LT |
2780 | /* |
2781 | * Set up the operation. | |
2782 | */ | |
517c2220 | 2783 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp); |
ad14c33a | 2784 | if (error) |
1da177e4 | 2785 | return(error); |
1da177e4 | 2786 | |
1d9025e5 | 2787 | leaf = bp->b_addr; |
517c2220 | 2788 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; |
1da177e4 LT |
2789 | ASSERT(entry->flags & XFS_ATTR_INCOMPLETE); |
2790 | ||
2791 | #ifdef DEBUG | |
517c2220 DC |
2792 | xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf); |
2793 | ASSERT(args->index < ichdr.count); | |
2794 | ASSERT(args->index >= 0); | |
2795 | ||
1da177e4 | 2796 | if (entry->flags & XFS_ATTR_LOCAL) { |
517c2220 | 2797 | name_loc = xfs_attr3_leaf_name_local(leaf, args->index); |
1da177e4 LT |
2798 | namelen = name_loc->namelen; |
2799 | name = (char *)name_loc->nameval; | |
2800 | } else { | |
517c2220 | 2801 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); |
1da177e4 LT |
2802 | namelen = name_rmt->namelen; |
2803 | name = (char *)name_rmt->name; | |
2804 | } | |
6b19f2d8 | 2805 | ASSERT(be32_to_cpu(entry->hashval) == args->hashval); |
1da177e4 LT |
2806 | ASSERT(namelen == args->namelen); |
2807 | ASSERT(memcmp(name, args->name, namelen) == 0); | |
2808 | #endif /* DEBUG */ | |
2809 | ||
2810 | entry->flags &= ~XFS_ATTR_INCOMPLETE; | |
1d9025e5 | 2811 | xfs_trans_log_buf(args->trans, bp, |
1da177e4 LT |
2812 | XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry))); |
2813 | ||
2814 | if (args->rmtblkno) { | |
2815 | ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0); | |
517c2220 | 2816 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); |
c0f054e7 NS |
2817 | name_rmt->valueblk = cpu_to_be32(args->rmtblkno); |
2818 | name_rmt->valuelen = cpu_to_be32(args->valuelen); | |
1d9025e5 | 2819 | xfs_trans_log_buf(args->trans, bp, |
1da177e4 LT |
2820 | XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt))); |
2821 | } | |
1da177e4 LT |
2822 | |
2823 | /* | |
2824 | * Commit the flag value change and start the next trans in series. | |
2825 | */ | |
322ff6b8 | 2826 | return xfs_trans_roll(&args->trans, args->dp); |
1da177e4 LT |
2827 | } |
2828 | ||
2829 | /* | |
2830 | * Set the INCOMPLETE flag on an entry in a leaf block. | |
2831 | */ | |
2832 | int | |
517c2220 DC |
2833 | xfs_attr3_leaf_setflag( |
2834 | struct xfs_da_args *args) | |
1da177e4 | 2835 | { |
517c2220 DC |
2836 | struct xfs_attr_leafblock *leaf; |
2837 | struct xfs_attr_leaf_entry *entry; | |
2838 | struct xfs_attr_leaf_name_remote *name_rmt; | |
2839 | struct xfs_buf *bp; | |
1da177e4 | 2840 | int error; |
517c2220 DC |
2841 | #ifdef DEBUG |
2842 | struct xfs_attr3_icleaf_hdr ichdr; | |
2843 | #endif | |
1da177e4 | 2844 | |
5a5881cd DC |
2845 | trace_xfs_attr_leaf_setflag(args); |
2846 | ||
1da177e4 LT |
2847 | /* |
2848 | * Set up the operation. | |
2849 | */ | |
517c2220 | 2850 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp); |
ad14c33a | 2851 | if (error) |
1da177e4 | 2852 | return(error); |
1da177e4 | 2853 | |
1d9025e5 | 2854 | leaf = bp->b_addr; |
517c2220 DC |
2855 | #ifdef DEBUG |
2856 | xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf); | |
2857 | ASSERT(args->index < ichdr.count); | |
1da177e4 | 2858 | ASSERT(args->index >= 0); |
517c2220 DC |
2859 | #endif |
2860 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; | |
1da177e4 LT |
2861 | |
2862 | ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0); | |
2863 | entry->flags |= XFS_ATTR_INCOMPLETE; | |
1d9025e5 | 2864 | xfs_trans_log_buf(args->trans, bp, |
1da177e4 LT |
2865 | XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry))); |
2866 | if ((entry->flags & XFS_ATTR_LOCAL) == 0) { | |
517c2220 | 2867 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); |
1da177e4 LT |
2868 | name_rmt->valueblk = 0; |
2869 | name_rmt->valuelen = 0; | |
1d9025e5 | 2870 | xfs_trans_log_buf(args->trans, bp, |
1da177e4 LT |
2871 | XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt))); |
2872 | } | |
1da177e4 LT |
2873 | |
2874 | /* | |
2875 | * Commit the flag value change and start the next trans in series. | |
2876 | */ | |
322ff6b8 | 2877 | return xfs_trans_roll(&args->trans, args->dp); |
1da177e4 LT |
2878 | } |
2879 | ||
2880 | /* | |
2881 | * In a single transaction, clear the INCOMPLETE flag on the leaf entry | |
2882 | * given by args->blkno/index and set the INCOMPLETE flag on the leaf | |
2883 | * entry given by args->blkno2/index2. | |
2884 | * | |
2885 | * Note that they could be in different blocks, or in the same block. | |
2886 | */ | |
2887 | int | |
517c2220 DC |
2888 | xfs_attr3_leaf_flipflags( |
2889 | struct xfs_da_args *args) | |
1da177e4 | 2890 | { |
517c2220 DC |
2891 | struct xfs_attr_leafblock *leaf1; |
2892 | struct xfs_attr_leafblock *leaf2; | |
2893 | struct xfs_attr_leaf_entry *entry1; | |
2894 | struct xfs_attr_leaf_entry *entry2; | |
2895 | struct xfs_attr_leaf_name_remote *name_rmt; | |
2896 | struct xfs_buf *bp1; | |
2897 | struct xfs_buf *bp2; | |
1da177e4 LT |
2898 | int error; |
2899 | #ifdef DEBUG | |
517c2220 DC |
2900 | struct xfs_attr3_icleaf_hdr ichdr1; |
2901 | struct xfs_attr3_icleaf_hdr ichdr2; | |
1da177e4 LT |
2902 | xfs_attr_leaf_name_local_t *name_loc; |
2903 | int namelen1, namelen2; | |
2904 | char *name1, *name2; | |
2905 | #endif /* DEBUG */ | |
2906 | ||
5a5881cd DC |
2907 | trace_xfs_attr_leaf_flipflags(args); |
2908 | ||
1da177e4 LT |
2909 | /* |
2910 | * Read the block containing the "old" attr | |
2911 | */ | |
517c2220 | 2912 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1); |
ad14c33a DC |
2913 | if (error) |
2914 | return error; | |
1da177e4 LT |
2915 | |
2916 | /* | |
2917 | * Read the block containing the "new" attr, if it is different | |
2918 | */ | |
2919 | if (args->blkno2 != args->blkno) { | |
517c2220 | 2920 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno2, |
ad14c33a DC |
2921 | -1, &bp2); |
2922 | if (error) | |
2923 | return error; | |
1da177e4 LT |
2924 | } else { |
2925 | bp2 = bp1; | |
2926 | } | |
2927 | ||
1d9025e5 | 2928 | leaf1 = bp1->b_addr; |
517c2220 | 2929 | entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index]; |
1da177e4 | 2930 | |
1d9025e5 | 2931 | leaf2 = bp2->b_addr; |
517c2220 | 2932 | entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2]; |
1da177e4 LT |
2933 | |
2934 | #ifdef DEBUG | |
517c2220 DC |
2935 | xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1); |
2936 | ASSERT(args->index < ichdr1.count); | |
2937 | ASSERT(args->index >= 0); | |
2938 | ||
2939 | xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2); | |
2940 | ASSERT(args->index2 < ichdr2.count); | |
2941 | ASSERT(args->index2 >= 0); | |
2942 | ||
1da177e4 | 2943 | if (entry1->flags & XFS_ATTR_LOCAL) { |
517c2220 | 2944 | name_loc = xfs_attr3_leaf_name_local(leaf1, args->index); |
1da177e4 LT |
2945 | namelen1 = name_loc->namelen; |
2946 | name1 = (char *)name_loc->nameval; | |
2947 | } else { | |
517c2220 | 2948 | name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index); |
1da177e4 LT |
2949 | namelen1 = name_rmt->namelen; |
2950 | name1 = (char *)name_rmt->name; | |
2951 | } | |
2952 | if (entry2->flags & XFS_ATTR_LOCAL) { | |
517c2220 | 2953 | name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2); |
1da177e4 LT |
2954 | namelen2 = name_loc->namelen; |
2955 | name2 = (char *)name_loc->nameval; | |
2956 | } else { | |
517c2220 | 2957 | name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2); |
1da177e4 LT |
2958 | namelen2 = name_rmt->namelen; |
2959 | name2 = (char *)name_rmt->name; | |
2960 | } | |
6b19f2d8 | 2961 | ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval)); |
1da177e4 LT |
2962 | ASSERT(namelen1 == namelen2); |
2963 | ASSERT(memcmp(name1, name2, namelen1) == 0); | |
2964 | #endif /* DEBUG */ | |
2965 | ||
2966 | ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE); | |
2967 | ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0); | |
2968 | ||
2969 | entry1->flags &= ~XFS_ATTR_INCOMPLETE; | |
1d9025e5 | 2970 | xfs_trans_log_buf(args->trans, bp1, |
1da177e4 LT |
2971 | XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1))); |
2972 | if (args->rmtblkno) { | |
2973 | ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0); | |
517c2220 | 2974 | name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index); |
c0f054e7 NS |
2975 | name_rmt->valueblk = cpu_to_be32(args->rmtblkno); |
2976 | name_rmt->valuelen = cpu_to_be32(args->valuelen); | |
1d9025e5 | 2977 | xfs_trans_log_buf(args->trans, bp1, |
1da177e4 LT |
2978 | XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt))); |
2979 | } | |
2980 | ||
2981 | entry2->flags |= XFS_ATTR_INCOMPLETE; | |
1d9025e5 | 2982 | xfs_trans_log_buf(args->trans, bp2, |
1da177e4 LT |
2983 | XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2))); |
2984 | if ((entry2->flags & XFS_ATTR_LOCAL) == 0) { | |
517c2220 | 2985 | name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2); |
1da177e4 LT |
2986 | name_rmt->valueblk = 0; |
2987 | name_rmt->valuelen = 0; | |
1d9025e5 | 2988 | xfs_trans_log_buf(args->trans, bp2, |
1da177e4 LT |
2989 | XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt))); |
2990 | } | |
1da177e4 LT |
2991 | |
2992 | /* | |
2993 | * Commit the flag value change and start the next trans in series. | |
2994 | */ | |
322ff6b8 | 2995 | error = xfs_trans_roll(&args->trans, args->dp); |
1da177e4 | 2996 | |
517c2220 | 2997 | return error; |
1da177e4 LT |
2998 | } |
2999 | ||
3000 | /*======================================================================== | |
3001 | * Indiscriminately delete the entire attribute fork | |
3002 | *========================================================================*/ | |
3003 | ||
3004 | /* | |
3005 | * Recurse (gasp!) through the attribute nodes until we find leaves. | |
3006 | * We're doing a depth-first traversal in order to invalidate everything. | |
3007 | */ | |
3008 | int | |
517c2220 DC |
3009 | xfs_attr3_root_inactive( |
3010 | struct xfs_trans **trans, | |
3011 | struct xfs_inode *dp) | |
1da177e4 | 3012 | { |
517c2220 DC |
3013 | struct xfs_da_blkinfo *info; |
3014 | struct xfs_buf *bp; | |
3015 | xfs_daddr_t blkno; | |
3016 | int error; | |
1da177e4 LT |
3017 | |
3018 | /* | |
3019 | * Read block 0 to see what we have to work with. | |
3020 | * We only get here if we have extents, since we remove | |
3021 | * the extents in reverse order the extent containing | |
3022 | * block 0 must still be there. | |
3023 | */ | |
f5ea1100 | 3024 | error = xfs_da3_node_read(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK); |
1da177e4 | 3025 | if (error) |
517c2220 DC |
3026 | return error; |
3027 | blkno = bp->b_bn; | |
1da177e4 LT |
3028 | |
3029 | /* | |
3030 | * Invalidate the tree, even if the "tree" is only a single leaf block. | |
3031 | * This is a depth-first traversal! | |
3032 | */ | |
1d9025e5 | 3033 | info = bp->b_addr; |
517c2220 DC |
3034 | switch (info->magic) { |
3035 | case cpu_to_be16(XFS_DA_NODE_MAGIC): | |
3036 | case cpu_to_be16(XFS_DA3_NODE_MAGIC): | |
3037 | error = xfs_attr3_node_inactive(trans, dp, bp, 1); | |
3038 | break; | |
3039 | case cpu_to_be16(XFS_ATTR_LEAF_MAGIC): | |
3040 | case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC): | |
3041 | error = xfs_attr3_leaf_inactive(trans, dp, bp); | |
3042 | break; | |
3043 | default: | |
1da177e4 | 3044 | error = XFS_ERROR(EIO); |
1d9025e5 | 3045 | xfs_trans_brelse(*trans, bp); |
517c2220 | 3046 | break; |
1da177e4 LT |
3047 | } |
3048 | if (error) | |
517c2220 | 3049 | return error; |
1da177e4 LT |
3050 | |
3051 | /* | |
3052 | * Invalidate the incore copy of the root block. | |
3053 | */ | |
3054 | error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK); | |
3055 | if (error) | |
517c2220 | 3056 | return error; |
1d9025e5 | 3057 | xfs_trans_binval(*trans, bp); /* remove from cache */ |
1da177e4 LT |
3058 | /* |
3059 | * Commit the invalidate and start the next transaction. | |
3060 | */ | |
322ff6b8 | 3061 | error = xfs_trans_roll(trans, dp); |
1da177e4 | 3062 | |
517c2220 | 3063 | return error; |
1da177e4 LT |
3064 | } |
3065 | ||
3066 | /* | |
3067 | * Recurse (gasp!) through the attribute nodes until we find leaves. | |
3068 | * We're doing a depth-first traversal in order to invalidate everything. | |
3069 | */ | |
ba0f32d4 | 3070 | STATIC int |
517c2220 | 3071 | xfs_attr3_node_inactive( |
1d9025e5 DC |
3072 | struct xfs_trans **trans, |
3073 | struct xfs_inode *dp, | |
3074 | struct xfs_buf *bp, | |
3075 | int level) | |
1da177e4 LT |
3076 | { |
3077 | xfs_da_blkinfo_t *info; | |
3078 | xfs_da_intnode_t *node; | |
3079 | xfs_dablk_t child_fsb; | |
3080 | xfs_daddr_t parent_blkno, child_blkno; | |
517c2220 | 3081 | int error, i; |
1d9025e5 | 3082 | struct xfs_buf *child_bp; |
f5ea1100 | 3083 | struct xfs_da_node_entry *btree; |
517c2220 | 3084 | struct xfs_da3_icnode_hdr ichdr; |
1da177e4 LT |
3085 | |
3086 | /* | |
3087 | * Since this code is recursive (gasp!) we must protect ourselves. | |
3088 | */ | |
3089 | if (level > XFS_DA_NODE_MAXDEPTH) { | |
1d9025e5 | 3090 | xfs_trans_brelse(*trans, bp); /* no locks for later trans */ |
517c2220 | 3091 | return XFS_ERROR(EIO); |
1da177e4 LT |
3092 | } |
3093 | ||
1d9025e5 | 3094 | node = bp->b_addr; |
517c2220 DC |
3095 | xfs_da3_node_hdr_from_disk(&ichdr, node); |
3096 | parent_blkno = bp->b_bn; | |
3097 | if (!ichdr.count) { | |
1d9025e5 | 3098 | xfs_trans_brelse(*trans, bp); |
517c2220 | 3099 | return 0; |
1da177e4 | 3100 | } |
f5ea1100 DC |
3101 | btree = xfs_da3_node_tree_p(node); |
3102 | child_fsb = be32_to_cpu(btree[0].before); | |
1d9025e5 | 3103 | xfs_trans_brelse(*trans, bp); /* no locks for later trans */ |
1da177e4 LT |
3104 | |
3105 | /* | |
3106 | * If this is the node level just above the leaves, simply loop | |
3107 | * over the leaves removing all of them. If this is higher up | |
3108 | * in the tree, recurse downward. | |
3109 | */ | |
517c2220 | 3110 | for (i = 0; i < ichdr.count; i++) { |
1da177e4 LT |
3111 | /* |
3112 | * Read the subsidiary block to see what we have to work with. | |
3113 | * Don't do this in a transaction. This is a depth-first | |
3114 | * traversal of the tree so we may deal with many blocks | |
3115 | * before we come back to this one. | |
3116 | */ | |
f5ea1100 | 3117 | error = xfs_da3_node_read(*trans, dp, child_fsb, -2, &child_bp, |
d9392a4b | 3118 | XFS_ATTR_FORK); |
1da177e4 LT |
3119 | if (error) |
3120 | return(error); | |
3121 | if (child_bp) { | |
3122 | /* save for re-read later */ | |
1d9025e5 | 3123 | child_blkno = XFS_BUF_ADDR(child_bp); |
1da177e4 LT |
3124 | |
3125 | /* | |
3126 | * Invalidate the subtree, however we have to. | |
3127 | */ | |
1d9025e5 | 3128 | info = child_bp->b_addr; |
517c2220 DC |
3129 | switch (info->magic) { |
3130 | case cpu_to_be16(XFS_DA_NODE_MAGIC): | |
3131 | case cpu_to_be16(XFS_DA3_NODE_MAGIC): | |
3132 | error = xfs_attr3_node_inactive(trans, dp, | |
3133 | child_bp, level + 1); | |
3134 | break; | |
3135 | case cpu_to_be16(XFS_ATTR_LEAF_MAGIC): | |
3136 | case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC): | |
3137 | error = xfs_attr3_leaf_inactive(trans, dp, | |
3138 | child_bp); | |
3139 | break; | |
3140 | default: | |
1da177e4 | 3141 | error = XFS_ERROR(EIO); |
1d9025e5 | 3142 | xfs_trans_brelse(*trans, child_bp); |
517c2220 | 3143 | break; |
1da177e4 LT |
3144 | } |
3145 | if (error) | |
517c2220 | 3146 | return error; |
1da177e4 LT |
3147 | |
3148 | /* | |
3149 | * Remove the subsidiary block from the cache | |
3150 | * and from the log. | |
3151 | */ | |
3152 | error = xfs_da_get_buf(*trans, dp, 0, child_blkno, | |
3153 | &child_bp, XFS_ATTR_FORK); | |
3154 | if (error) | |
517c2220 | 3155 | return error; |
1d9025e5 | 3156 | xfs_trans_binval(*trans, child_bp); |
1da177e4 LT |
3157 | } |
3158 | ||
3159 | /* | |
3160 | * If we're not done, re-read the parent to get the next | |
3161 | * child block number. | |
3162 | */ | |
517c2220 | 3163 | if (i + 1 < ichdr.count) { |
f5ea1100 | 3164 | error = xfs_da3_node_read(*trans, dp, 0, parent_blkno, |
d9392a4b | 3165 | &bp, XFS_ATTR_FORK); |
1da177e4 | 3166 | if (error) |
517c2220 DC |
3167 | return error; |
3168 | child_fsb = be32_to_cpu(btree[i + 1].before); | |
1d9025e5 | 3169 | xfs_trans_brelse(*trans, bp); |
1da177e4 LT |
3170 | } |
3171 | /* | |
3172 | * Atomically commit the whole invalidate stuff. | |
3173 | */ | |
322ff6b8 NS |
3174 | error = xfs_trans_roll(trans, dp); |
3175 | if (error) | |
517c2220 | 3176 | return error; |
1da177e4 LT |
3177 | } |
3178 | ||
517c2220 | 3179 | return 0; |
1da177e4 LT |
3180 | } |
3181 | ||
3182 | /* | |
3183 | * Invalidate all of the "remote" value regions pointed to by a particular | |
3184 | * leaf block. | |
3185 | * Note that we must release the lock on the buffer so that we are not | |
3186 | * caught holding something that the logging code wants to flush to disk. | |
3187 | */ | |
ba0f32d4 | 3188 | STATIC int |
517c2220 DC |
3189 | xfs_attr3_leaf_inactive( |
3190 | struct xfs_trans **trans, | |
3191 | struct xfs_inode *dp, | |
3192 | struct xfs_buf *bp) | |
1da177e4 | 3193 | { |
517c2220 DC |
3194 | struct xfs_attr_leafblock *leaf; |
3195 | struct xfs_attr3_icleaf_hdr ichdr; | |
3196 | struct xfs_attr_leaf_entry *entry; | |
3197 | struct xfs_attr_leaf_name_remote *name_rmt; | |
3198 | struct xfs_attr_inactive_list *list; | |
3199 | struct xfs_attr_inactive_list *lp; | |
3200 | int error; | |
3201 | int count; | |
3202 | int size; | |
3203 | int tmp; | |
3204 | int i; | |
1da177e4 | 3205 | |
1d9025e5 | 3206 | leaf = bp->b_addr; |
517c2220 | 3207 | xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf); |
1da177e4 LT |
3208 | |
3209 | /* | |
3210 | * Count the number of "remote" value extents. | |
3211 | */ | |
3212 | count = 0; | |
517c2220 DC |
3213 | entry = xfs_attr3_leaf_entryp(leaf); |
3214 | for (i = 0; i < ichdr.count; entry++, i++) { | |
6b19f2d8 NS |
3215 | if (be16_to_cpu(entry->nameidx) && |
3216 | ((entry->flags & XFS_ATTR_LOCAL) == 0)) { | |
517c2220 | 3217 | name_rmt = xfs_attr3_leaf_name_remote(leaf, i); |
1da177e4 LT |
3218 | if (name_rmt->valueblk) |
3219 | count++; | |
3220 | } | |
3221 | } | |
3222 | ||
3223 | /* | |
3224 | * If there are no "remote" values, we're done. | |
3225 | */ | |
3226 | if (count == 0) { | |
1d9025e5 | 3227 | xfs_trans_brelse(*trans, bp); |
517c2220 | 3228 | return 0; |
1da177e4 LT |
3229 | } |
3230 | ||
3231 | /* | |
3232 | * Allocate storage for a list of all the "remote" value extents. | |
3233 | */ | |
3234 | size = count * sizeof(xfs_attr_inactive_list_t); | |
517c2220 | 3235 | list = kmem_alloc(size, KM_SLEEP); |
1da177e4 LT |
3236 | |
3237 | /* | |
3238 | * Identify each of the "remote" value extents. | |
3239 | */ | |
3240 | lp = list; | |
517c2220 DC |
3241 | entry = xfs_attr3_leaf_entryp(leaf); |
3242 | for (i = 0; i < ichdr.count; entry++, i++) { | |
6b19f2d8 NS |
3243 | if (be16_to_cpu(entry->nameidx) && |
3244 | ((entry->flags & XFS_ATTR_LOCAL) == 0)) { | |
517c2220 | 3245 | name_rmt = xfs_attr3_leaf_name_remote(leaf, i); |
1da177e4 | 3246 | if (name_rmt->valueblk) { |
d7929ff6 NS |
3247 | lp->valueblk = be32_to_cpu(name_rmt->valueblk); |
3248 | lp->valuelen = XFS_B_TO_FSB(dp->i_mount, | |
3249 | be32_to_cpu(name_rmt->valuelen)); | |
1da177e4 LT |
3250 | lp++; |
3251 | } | |
3252 | } | |
3253 | } | |
1d9025e5 | 3254 | xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */ |
1da177e4 LT |
3255 | |
3256 | /* | |
3257 | * Invalidate each of the "remote" value extents. | |
3258 | */ | |
3259 | error = 0; | |
3260 | for (lp = list, i = 0; i < count; i++, lp++) { | |
517c2220 | 3261 | tmp = xfs_attr3_leaf_freextent(trans, dp, |
d7929ff6 NS |
3262 | lp->valueblk, lp->valuelen); |
3263 | ||
1da177e4 LT |
3264 | if (error == 0) |
3265 | error = tmp; /* save only the 1st errno */ | |
3266 | } | |
3267 | ||
517c2220 DC |
3268 | kmem_free(list); |
3269 | return error; | |
1da177e4 LT |
3270 | } |
3271 | ||
3272 | /* | |
3273 | * Look at all the extents for this logical region, | |
3274 | * invalidate any buffers that are incore/in transactions. | |
3275 | */ | |
ba0f32d4 | 3276 | STATIC int |
517c2220 DC |
3277 | xfs_attr3_leaf_freextent( |
3278 | struct xfs_trans **trans, | |
3279 | struct xfs_inode *dp, | |
3280 | xfs_dablk_t blkno, | |
3281 | int blkcnt) | |
1da177e4 | 3282 | { |
517c2220 DC |
3283 | struct xfs_bmbt_irec map; |
3284 | struct xfs_buf *bp; | |
3285 | xfs_dablk_t tblkno; | |
3286 | xfs_daddr_t dblkno; | |
3287 | int tblkcnt; | |
3288 | int dblkcnt; | |
3289 | int nmap; | |
3290 | int error; | |
1da177e4 LT |
3291 | |
3292 | /* | |
3293 | * Roll through the "value", invalidating the attribute value's | |
3294 | * blocks. | |
3295 | */ | |
3296 | tblkno = blkno; | |
3297 | tblkcnt = blkcnt; | |
3298 | while (tblkcnt > 0) { | |
3299 | /* | |
3300 | * Try to remember where we decided to put the value. | |
3301 | */ | |
3302 | nmap = 1; | |
5c8ed202 DC |
3303 | error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt, |
3304 | &map, &nmap, XFS_BMAPI_ATTRFORK); | |
1da177e4 LT |
3305 | if (error) { |
3306 | return(error); | |
3307 | } | |
3308 | ASSERT(nmap == 1); | |
3309 | ASSERT(map.br_startblock != DELAYSTARTBLOCK); | |
3310 | ||
3311 | /* | |
3312 | * If it's a hole, these are already unmapped | |
3313 | * so there's nothing to invalidate. | |
3314 | */ | |
3315 | if (map.br_startblock != HOLESTARTBLOCK) { | |
3316 | ||
3317 | dblkno = XFS_FSB_TO_DADDR(dp->i_mount, | |
3318 | map.br_startblock); | |
3319 | dblkcnt = XFS_FSB_TO_BB(dp->i_mount, | |
3320 | map.br_blockcount); | |
3321 | bp = xfs_trans_get_buf(*trans, | |
3322 | dp->i_mount->m_ddev_targp, | |
a8acad70 | 3323 | dblkno, dblkcnt, 0); |
2a30f36d CS |
3324 | if (!bp) |
3325 | return ENOMEM; | |
1da177e4 LT |
3326 | xfs_trans_binval(*trans, bp); |
3327 | /* | |
3328 | * Roll to next transaction. | |
3329 | */ | |
322ff6b8 NS |
3330 | error = xfs_trans_roll(trans, dp); |
3331 | if (error) | |
1da177e4 LT |
3332 | return (error); |
3333 | } | |
3334 | ||
3335 | tblkno += map.br_blockcount; | |
3336 | tblkcnt -= map.br_blockcount; | |
3337 | } | |
3338 | ||
3339 | return(0); | |
3340 | } |