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