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