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