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