]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_dir2_node.c
Update copyright/license notices to match SGI legal prefered boilerplate.
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dir2_node.c
CommitLineData
2bd0ea18 1/*
da23017d
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5000d01d 4 *
da23017d
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
2bd0ea18 7 * published by the Free Software Foundation.
5000d01d 8 *
da23017d
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
5000d01d 13 *
da23017d
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2bd0ea18
NS
17 */
18
19/*
20 * xfs_dir2_node.c
21 * XFS directory implementation, version 2, node form files
22 * See data structures in xfs_dir2_node.h and xfs_da_btree.h.
23 */
24
25#include <xfs.h>
26
27/*
28 * Log entries from a freespace block.
29 */
30void
31xfs_dir2_free_log_bests(
32 xfs_trans_t *tp, /* transaction pointer */
33 xfs_dabuf_t *bp, /* freespace buffer */
34 int first, /* first entry to log */
35 int last) /* last entry to log */
36{
37 xfs_dir2_free_t *free; /* freespace structure */
38
39 free = bp->data;
40 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
41 xfs_da_log_buf(tp, bp,
42 (uint)((char *)&free->bests[first] - (char *)free),
43 (uint)((char *)&free->bests[last] - (char *)free +
44 sizeof(free->bests[0]) - 1));
45}
46
47/*
48 * Log header from a freespace block.
49 */
50static void
51xfs_dir2_free_log_header(
52 xfs_trans_t *tp, /* transaction pointer */
53 xfs_dabuf_t *bp) /* freespace buffer */
54{
55 xfs_dir2_free_t *free; /* freespace structure */
56
57 free = bp->data;
58 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
59 xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
60 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
61}
62
63/*
64 * Convert a leaf-format directory to a node-format directory.
65 * We need to change the magic number of the leaf block, and copy
66 * the freespace table out of the leaf block into its own block.
67 */
68int /* error */
69xfs_dir2_leaf_to_node(
70 xfs_da_args_t *args, /* operation arguments */
71 xfs_dabuf_t *lbp) /* leaf buffer */
72{
73 xfs_inode_t *dp; /* incore directory inode */
74 int error; /* error return value */
75 xfs_dabuf_t *fbp; /* freespace buffer */
76 xfs_dir2_db_t fdb; /* freespace block number */
77 xfs_dir2_free_t *free; /* freespace structure */
78 xfs_dir2_data_off_t *from; /* pointer to freespace entry */
79 int i; /* leaf freespace index */
80 xfs_dir2_leaf_t *leaf; /* leaf structure */
81 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
82 xfs_mount_t *mp; /* filesystem mount point */
83 int n; /* count of live freespc ents */
84 xfs_dir2_data_off_t off; /* freespace entry value */
85 xfs_dir2_data_off_t *to; /* pointer to freespace entry */
86 xfs_trans_t *tp; /* transaction pointer */
87
88 xfs_dir2_trace_args_b("leaf_to_node", args, lbp);
89 dp = args->dp;
90 mp = dp->i_mount;
91 tp = args->trans;
92 /*
93 * Add a freespace block to the directory.
94 */
0e266570 95 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
2bd0ea18
NS
96 return error;
97 }
98 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
99 /*
100 * Get the buffer for the new freespace block.
101 */
0e266570
NS
102 if ((error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, fdb), -1, &fbp,
103 XFS_DATA_FORK))) {
2bd0ea18
NS
104 return error;
105 }
106 ASSERT(fbp != NULL);
107 free = fbp->data;
108 leaf = lbp->data;
109 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
110 /*
111 * Initialize the freespace block header.
112 */
113 INT_SET(free->hdr.magic, ARCH_CONVERT, XFS_DIR2_FREE_MAGIC);
46eca962 114 free->hdr.firstdb = 0;
2bd0ea18
NS
115 ASSERT(INT_GET(ltp->bestcount, ARCH_CONVERT) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
116 INT_COPY(free->hdr.nvalid, ltp->bestcount, ARCH_CONVERT);
117 /*
118 * Copy freespace entries from the leaf block to the new block.
119 * Count active entries.
120 */
46eca962 121 for (i = n = 0, from = XFS_DIR2_LEAF_BESTS_P(ltp), to = free->bests;
2bd0ea18
NS
122 i < INT_GET(ltp->bestcount, ARCH_CONVERT); i++, from++, to++) {
123 if ((off = INT_GET(*from, ARCH_CONVERT)) != NULLDATAOFF)
124 n++;
125 INT_SET(*to, ARCH_CONVERT, off);
126 }
127 INT_SET(free->hdr.nused, ARCH_CONVERT, n);
128 INT_SET(leaf->hdr.info.magic, ARCH_CONVERT, XFS_DIR2_LEAFN_MAGIC);
129 /*
130 * Log everything.
131 */
132 xfs_dir2_leaf_log_header(tp, lbp);
133 xfs_dir2_free_log_header(tp, fbp);
134 xfs_dir2_free_log_bests(tp, fbp, 0, INT_GET(free->hdr.nvalid, ARCH_CONVERT) - 1);
135 xfs_da_buf_done(fbp);
136 xfs_dir2_leafn_check(dp, lbp);
137 return 0;
138}
139
140/*
141 * Add a leaf entry to a leaf block in a node-form directory.
142 * The other work necessary is done from the caller.
143 */
144static int /* error */
145xfs_dir2_leafn_add(
146 xfs_dabuf_t *bp, /* leaf buffer */
147 xfs_da_args_t *args, /* operation arguments */
148 int index) /* insertion pt for new entry */
149{
150 int compact; /* compacting stale leaves */
151 xfs_inode_t *dp; /* incore directory inode */
152 int highstale; /* next stale entry */
153 xfs_dir2_leaf_t *leaf; /* leaf structure */
154 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
155 int lfloghigh; /* high leaf entry logging */
156 int lfloglow; /* low leaf entry logging */
157 int lowstale; /* previous stale entry */
158 xfs_mount_t *mp; /* filesystem mount point */
159 xfs_trans_t *tp; /* transaction pointer */
160
161 xfs_dir2_trace_args_sb("leafn_add", args, index, bp);
162 dp = args->dp;
163 mp = dp->i_mount;
164 tp = args->trans;
165 leaf = bp->data;
516de2da
RC
166
167 /*
168 * Quick check just to make sure we are not going to index
169 * into other peoples memory
170 */
171 if (index < 0)
172 return XFS_ERROR(EFSCORRUPTED);
173
2bd0ea18
NS
174 /*
175 * If there are already the maximum number of leaf entries in
176 * the block, if there are no stale entries it won't fit.
177 * Caller will do a split. If there are stale entries we'll do
178 * a compact.
179 */
516de2da 180
2bd0ea18 181 if (INT_GET(leaf->hdr.count, ARCH_CONVERT) == XFS_DIR2_MAX_LEAF_ENTS(mp)) {
46eca962 182 if (!leaf->hdr.stale)
2bd0ea18
NS
183 return XFS_ERROR(ENOSPC);
184 compact = INT_GET(leaf->hdr.stale, ARCH_CONVERT) > 1;
185 } else
186 compact = 0;
187 ASSERT(index == 0 || INT_GET(leaf->ents[index - 1].hashval, ARCH_CONVERT) <= args->hashval);
188 ASSERT(index == INT_GET(leaf->hdr.count, ARCH_CONVERT) ||
189 INT_GET(leaf->ents[index].hashval, ARCH_CONVERT) >= args->hashval);
5000d01d 190
2bd0ea18
NS
191 if (args->justcheck)
192 return 0;
193
194 /*
195 * Compact out all but one stale leaf entry. Leaves behind
196 * the entry closest to index.
197 */
198 if (compact) {
2bd0ea18
NS
199 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
200 &lfloglow, &lfloghigh);
201 }
202 /*
203 * Set impossible logging indices for this case.
204 */
46eca962 205 else if (leaf->hdr.stale) {
2bd0ea18
NS
206 lfloglow = INT_GET(leaf->hdr.count, ARCH_CONVERT);
207 lfloghigh = -1;
208 }
209 /*
210 * No stale entries, just insert a space for the new entry.
211 */
46eca962 212 if (!leaf->hdr.stale) {
2bd0ea18
NS
213 lep = &leaf->ents[index];
214 if (index < INT_GET(leaf->hdr.count, ARCH_CONVERT))
32181a02 215 memmove(lep + 1, lep,
2bd0ea18
NS
216 (INT_GET(leaf->hdr.count, ARCH_CONVERT) - index) * sizeof(*lep));
217 lfloglow = index;
218 lfloghigh = INT_GET(leaf->hdr.count, ARCH_CONVERT);
219 INT_MOD(leaf->hdr.count, ARCH_CONVERT, +1);
220 }
221 /*
222 * There are stale entries. We'll use one for the new entry.
223 */
224 else {
225 /*
226 * If we didn't do a compact then we need to figure out
227 * which stale entry will be used.
228 */
229 if (compact == 0) {
230 /*
231 * Find first stale entry before our insertion point.
232 */
233 for (lowstale = index - 1;
234 lowstale >= 0 &&
235 INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) !=
236 XFS_DIR2_NULL_DATAPTR;
237 lowstale--)
238 continue;
239 /*
240 * Find next stale entry after insertion point.
241 * Stop looking if the answer would be worse than
242 * lowstale already found.
243 */
244 for (highstale = index;
245 highstale < INT_GET(leaf->hdr.count, ARCH_CONVERT) &&
246 INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) !=
247 XFS_DIR2_NULL_DATAPTR &&
248 (lowstale < 0 ||
249 index - lowstale - 1 >= highstale - index);
250 highstale++)
251 continue;
252 }
253 /*
254 * Using the low stale entry.
255 * Shift entries up toward the stale slot.
256 */
257 if (lowstale >= 0 &&
258 (highstale == INT_GET(leaf->hdr.count, ARCH_CONVERT) ||
259 index - lowstale - 1 < highstale - index)) {
260 ASSERT(INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) ==
261 XFS_DIR2_NULL_DATAPTR);
262 ASSERT(index - lowstale - 1 >= 0);
263 if (index - lowstale - 1 > 0)
32181a02
NS
264 memmove(&leaf->ents[lowstale],
265 &leaf->ents[lowstale + 1],
2bd0ea18
NS
266 (index - lowstale - 1) * sizeof(*lep));
267 lep = &leaf->ents[index - 1];
268 lfloglow = MIN(lowstale, lfloglow);
269 lfloghigh = MAX(index - 1, lfloghigh);
270 }
271 /*
272 * Using the high stale entry.
273 * Shift entries down toward the stale slot.
274 */
275 else {
276 ASSERT(INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) ==
277 XFS_DIR2_NULL_DATAPTR);
278 ASSERT(highstale - index >= 0);
279 if (highstale - index > 0)
32181a02
NS
280 memmove(&leaf->ents[index + 1],
281 &leaf->ents[index],
2bd0ea18
NS
282 (highstale - index) * sizeof(*lep));
283 lep = &leaf->ents[index];
284 lfloglow = MIN(index, lfloglow);
285 lfloghigh = MAX(highstale, lfloghigh);
286 }
287 INT_MOD(leaf->hdr.stale, ARCH_CONVERT, -1);
288 }
289 /*
290 * Insert the new entry, log everything.
291 */
292 INT_SET(lep->hashval, ARCH_CONVERT, args->hashval);
293 INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_DB_OFF_TO_DATAPTR(mp, args->blkno, args->index));
294 xfs_dir2_leaf_log_header(tp, bp);
295 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
296 xfs_dir2_leafn_check(dp, bp);
297 return 0;
298}
299
300#ifdef DEBUG
301/*
302 * Check internal consistency of a leafn block.
303 */
304void
305xfs_dir2_leafn_check(
306 xfs_inode_t *dp, /* incore directory inode */
307 xfs_dabuf_t *bp) /* leaf buffer */
308{
309 int i; /* leaf index */
dfc130f3 310 xfs_dir2_leaf_t *leaf; /* leaf structure */
2bd0ea18
NS
311 xfs_mount_t *mp; /* filesystem mount point */
312 int stale; /* count of stale leaves */
313
314 leaf = bp->data;
315 mp = dp->i_mount;
316 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
317 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) <= XFS_DIR2_MAX_LEAF_ENTS(mp));
318 for (i = stale = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); i++) {
319 if (i + 1 < INT_GET(leaf->hdr.count, ARCH_CONVERT)) {
320 ASSERT(INT_GET(leaf->ents[i].hashval, ARCH_CONVERT) <=
321 INT_GET(leaf->ents[i + 1].hashval, ARCH_CONVERT));
5000d01d 322 }
2bd0ea18
NS
323 if (INT_GET(leaf->ents[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
324 stale++;
325 }
326 ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) == stale);
327}
328#endif /* DEBUG */
329
330/*
331 * Return the last hash value in the leaf.
332 * Stale entries are ok.
333 */
334xfs_dahash_t /* hash value */
335xfs_dir2_leafn_lasthash(
336 xfs_dabuf_t *bp, /* leaf buffer */
337 int *count) /* count of entries in leaf */
338{
dfc130f3 339 xfs_dir2_leaf_t *leaf; /* leaf structure */
2bd0ea18
NS
340
341 leaf = bp->data;
342 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
343 if (count)
344 *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
46eca962 345 if (!leaf->hdr.count)
2bd0ea18
NS
346 return 0;
347 return INT_GET(leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
348}
349
350/*
351 * Look up a leaf entry in a node-format leaf block.
352 * If this is an addname then the extrablk in state is a freespace block,
353 * otherwise it's a data block.
354 */
355int
356xfs_dir2_leafn_lookup_int(
357 xfs_dabuf_t *bp, /* leaf buffer */
358 xfs_da_args_t *args, /* operation arguments */
359 int *indexp, /* out: leaf entry index */
360 xfs_da_state_t *state) /* state to fill in */
361{
362 xfs_dabuf_t *curbp; /* current data/free buffer */
363 xfs_dir2_db_t curdb; /* current data block number */
364 xfs_dir2_db_t curfdb; /* current free block number */
365 xfs_dir2_data_entry_t *dep; /* data block entry */
366 xfs_inode_t *dp; /* incore directory inode */
367 int error; /* error return value */
368 int fi; /* free entry index */
0e266570 369 xfs_dir2_free_t *free=NULL; /* free block structure */
2bd0ea18
NS
370 int index; /* leaf entry index */
371 xfs_dir2_leaf_t *leaf; /* leaf structure */
0e266570 372 int length=0; /* length of new data entry */
2bd0ea18
NS
373 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
374 xfs_mount_t *mp; /* filesystem mount point */
375 xfs_dir2_db_t newdb; /* new data block number */
376 xfs_dir2_db_t newfdb; /* new free block number */
377 xfs_trans_t *tp; /* transaction pointer */
378
379 dp = args->dp;
380 tp = args->trans;
381 mp = dp->i_mount;
382 leaf = bp->data;
383 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
384#ifdef __KERNEL__
385 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) > 0);
386#endif
387 xfs_dir2_leafn_check(dp, bp);
388 /*
389 * Look up the hash value in the leaf entries.
390 */
391 index = xfs_dir2_leaf_search_hash(args, bp);
392 /*
393 * Do we have a buffer coming in?
394 */
395 if (state->extravalid)
396 curbp = state->extrablk.bp;
397 else
398 curbp = NULL;
399 /*
400 * For addname, it's a free block buffer, get the block number.
401 */
402 if (args->addname) {
403 curfdb = curbp ? state->extrablk.blkno : -1;
404 curdb = -1;
405 length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
0e266570 406 if ((free = (curbp ? curbp->data : NULL)))
2bd0ea18
NS
407 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
408 }
409 /*
410 * For others, it's a data block buffer, get the block number.
411 */
412 else {
413 curfdb = -1;
414 curdb = curbp ? state->extrablk.blkno : -1;
415 }
416 /*
417 * Loop over leaf entries with the right hash value.
418 */
419 for (lep = &leaf->ents[index];
420 index < INT_GET(leaf->hdr.count, ARCH_CONVERT) && INT_GET(lep->hashval, ARCH_CONVERT) == args->hashval;
421 lep++, index++) {
422 /*
423 * Skip stale leaf entries.
424 */
425 if (INT_GET(lep->address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
426 continue;
427 /*
428 * Pull the data block number from the entry.
429 */
430 newdb = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
431 /*
432 * For addname, we're looking for a place to put the new entry.
433 * We want to use a data block with an entry of equal
434 * hash value to ours if there is one with room.
435 */
436 if (args->addname) {
437 /*
438 * If this block isn't the data block we already have
439 * in hand, take a look at it.
440 */
441 if (newdb != curdb) {
442 curdb = newdb;
443 /*
444 * Convert the data block to the free block
445 * holding its freespace information.
446 */
447 newfdb = XFS_DIR2_DB_TO_FDB(mp, newdb);
448 /*
449 * If it's not the one we have in hand,
450 * read it in.
451 */
452 if (newfdb != curfdb) {
453 /*
454 * If we had one before, drop it.
455 */
456 if (curbp)
457 xfs_da_brelse(tp, curbp);
458 /*
459 * Read the free block.
460 */
0e266570 461 if ((error = xfs_da_read_buf(tp, dp,
2bd0ea18
NS
462 XFS_DIR2_DB_TO_DA(mp,
463 newfdb),
464 -1, &curbp,
0e266570 465 XFS_DATA_FORK))) {
2bd0ea18
NS
466 return error;
467 }
468 curfdb = newfdb;
469 free = curbp->data;
470 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) ==
471 XFS_DIR2_FREE_MAGIC);
472 ASSERT((INT_GET(free->hdr.firstdb, ARCH_CONVERT) %
473 XFS_DIR2_MAX_FREE_BESTS(mp)) ==
474 0);
475 ASSERT(INT_GET(free->hdr.firstdb, ARCH_CONVERT) <= curdb);
476 ASSERT(curdb <
477 INT_GET(free->hdr.firstdb, ARCH_CONVERT) +
478 INT_GET(free->hdr.nvalid, ARCH_CONVERT));
479 }
480 /*
481 * Get the index for our entry.
482 */
483 fi = XFS_DIR2_DB_TO_FDINDEX(mp, curdb);
484 /*
485 * If it has room, return it.
486 */
4ca431fc
NS
487 if (unlikely(INT_GET(free->bests[fi], ARCH_CONVERT) == NULLDATAOFF)) {
488 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
489 XFS_ERRLEVEL_LOW, mp);
2bd0ea18
NS
490 return XFS_ERROR(EFSCORRUPTED);
491 }
492 if (INT_GET(free->bests[fi], ARCH_CONVERT) >= length) {
493 *indexp = index;
494 state->extravalid = 1;
495 state->extrablk.bp = curbp;
496 state->extrablk.blkno = curfdb;
497 state->extrablk.index = fi;
498 state->extrablk.magic =
499 XFS_DIR2_FREE_MAGIC;
500 ASSERT(args->oknoent);
501 return XFS_ERROR(ENOENT);
502 }
503 }
504 }
505 /*
506 * Not adding a new entry, so we really want to find
507 * the name given to us.
508 */
509 else {
510 /*
511 * If it's a different data block, go get it.
512 */
513 if (newdb != curdb) {
514 /*
515 * If we had a block before, drop it.
516 */
517 if (curbp)
518 xfs_da_brelse(tp, curbp);
519 /*
520 * Read the data block.
521 */
0e266570 522 if ((error =
2bd0ea18
NS
523 xfs_da_read_buf(tp, dp,
524 XFS_DIR2_DB_TO_DA(mp, newdb), -1,
0e266570 525 &curbp, XFS_DATA_FORK))) {
2bd0ea18
NS
526 return error;
527 }
528 xfs_dir2_data_check(dp, curbp);
529 curdb = newdb;
530 }
531 /*
532 * Point to the data entry.
533 */
534 dep = (xfs_dir2_data_entry_t *)
535 ((char *)curbp->data +
536 XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT)));
537 /*
538 * Compare the entry, return it if it matches.
539 */
540 if (dep->namelen == args->namelen &&
541 dep->name[0] == args->name[0] &&
32181a02 542 memcmp(dep->name, args->name, args->namelen) == 0) {
2bd0ea18
NS
543 args->inumber = INT_GET(dep->inumber, ARCH_CONVERT);
544 *indexp = index;
545 state->extravalid = 1;
546 state->extrablk.bp = curbp;
547 state->extrablk.blkno = curdb;
548 state->extrablk.index =
549 (int)((char *)dep -
550 (char *)curbp->data);
551 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
552 return XFS_ERROR(EEXIST);
553 }
554 }
555 }
556 /*
557 * Didn't find a match.
558 * If we are holding a buffer, give it back in case our caller
559 * finds it useful.
560 */
0e266570 561 if ((state->extravalid = (curbp != NULL))) {
2bd0ea18
NS
562 state->extrablk.bp = curbp;
563 state->extrablk.index = -1;
564 /*
565 * For addname, giving back a free block.
566 */
567 if (args->addname) {
568 state->extrablk.blkno = curfdb;
569 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
570 }
571 /*
572 * For other callers, giving back a data block.
573 */
574 else {
575 state->extrablk.blkno = curdb;
576 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
577 }
578 }
579 /*
580 * Return the final index, that will be the insertion point.
581 */
582 *indexp = index;
583 ASSERT(index == INT_GET(leaf->hdr.count, ARCH_CONVERT) || args->oknoent);
584 return XFS_ERROR(ENOENT);
585}
586
587/*
588 * Move count leaf entries from source to destination leaf.
589 * Log entries and headers. Stale entries are preserved.
590 */
591static void
592xfs_dir2_leafn_moveents(
593 xfs_da_args_t *args, /* operation arguments */
594 xfs_dabuf_t *bp_s, /* source leaf buffer */
595 int start_s, /* source leaf index */
596 xfs_dabuf_t *bp_d, /* destination leaf buffer */
597 int start_d, /* destination leaf index */
598 int count) /* count of leaves to copy */
599{
dfc130f3
RC
600 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
601 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
2bd0ea18
NS
602 int stale; /* count stale leaves copied */
603 xfs_trans_t *tp; /* transaction pointer */
604
605 xfs_dir2_trace_args_bibii("leafn_moveents", args, bp_s, start_s, bp_d,
606 start_d, count);
607 /*
608 * Silently return if nothing to do.
609 */
610 if (count == 0) {
2bd0ea18
NS
611 return;
612 }
613 tp = args->trans;
614 leaf_s = bp_s->data;
615 leaf_d = bp_d->data;
616 /*
617 * If the destination index is not the end of the current
618 * destination leaf entries, open up a hole in the destination
619 * to hold the new entries.
620 */
621 if (start_d < INT_GET(leaf_d->hdr.count, ARCH_CONVERT)) {
32181a02 622 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
2bd0ea18
NS
623 (INT_GET(leaf_d->hdr.count, ARCH_CONVERT) - start_d) *
624 sizeof(xfs_dir2_leaf_entry_t));
625 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
626 count + INT_GET(leaf_d->hdr.count, ARCH_CONVERT) - 1);
627 }
628 /*
629 * If the source has stale leaves, count the ones in the copy range
630 * so we can update the header correctly.
631 */
46eca962 632 if (leaf_s->hdr.stale) {
2bd0ea18
NS
633 int i; /* temp leaf index */
634
635 for (i = start_s, stale = 0; i < start_s + count; i++) {
636 if (INT_GET(leaf_s->ents[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR)
637 stale++;
638 }
639 } else
640 stale = 0;
641 /*
642 * Copy the leaf entries from source to destination.
643 */
32181a02 644 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
2bd0ea18
NS
645 count * sizeof(xfs_dir2_leaf_entry_t));
646 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
647 /*
648 * If there are source entries after the ones we copied,
649 * delete the ones we copied by sliding the next ones down.
650 */
651 if (start_s + count < INT_GET(leaf_s->hdr.count, ARCH_CONVERT)) {
32181a02 652 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
2bd0ea18
NS
653 count * sizeof(xfs_dir2_leaf_entry_t));
654 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
655 }
656 /*
657 * Update the headers and log them.
658 */
659 INT_MOD(leaf_s->hdr.count, ARCH_CONVERT, -(count));
660 INT_MOD(leaf_s->hdr.stale, ARCH_CONVERT, -(stale));
661 INT_MOD(leaf_d->hdr.count, ARCH_CONVERT, count);
662 INT_MOD(leaf_d->hdr.stale, ARCH_CONVERT, stale);
663 xfs_dir2_leaf_log_header(tp, bp_s);
664 xfs_dir2_leaf_log_header(tp, bp_d);
665 xfs_dir2_leafn_check(args->dp, bp_s);
666 xfs_dir2_leafn_check(args->dp, bp_d);
667}
668
669/*
670 * Determine the sort order of two leaf blocks.
671 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
672 */
673int /* sort order */
674xfs_dir2_leafn_order(
675 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
676 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
677{
dfc130f3
RC
678 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
679 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
2bd0ea18
NS
680
681 leaf1 = leaf1_bp->data;
682 leaf2 = leaf2_bp->data;
683 ASSERT(INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
684 ASSERT(INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
685 if (INT_GET(leaf1->hdr.count, ARCH_CONVERT) > 0 &&
686 INT_GET(leaf2->hdr.count, ARCH_CONVERT) > 0 &&
687 (INT_GET(leaf2->ents[0].hashval, ARCH_CONVERT) < INT_GET(leaf1->ents[0].hashval, ARCH_CONVERT) ||
688 INT_GET(leaf2->ents[INT_GET(leaf2->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT) <
689 INT_GET(leaf1->ents[INT_GET(leaf1->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT)))
690 return 1;
691 return 0;
692}
693
694/*
695 * Rebalance leaf entries between two leaf blocks.
696 * This is actually only called when the second block is new,
697 * though the code deals with the general case.
698 * A new entry will be inserted in one of the blocks, and that
699 * entry is taken into account when balancing.
700 */
701static void
702xfs_dir2_leafn_rebalance(
703 xfs_da_state_t *state, /* btree cursor */
704 xfs_da_state_blk_t *blk1, /* first btree block */
705 xfs_da_state_blk_t *blk2) /* second btree block */
706{
707 xfs_da_args_t *args; /* operation arguments */
708 int count; /* count (& direction) leaves */
709 int isleft; /* new goes in left leaf */
710 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
711 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
712 int mid; /* midpoint leaf index */
713#ifdef DEBUG
714 int oldstale; /* old count of stale leaves */
715#endif
716 int oldsum; /* old total leaf count */
717 int swap; /* swapped leaf blocks */
718
719 args = state->args;
720 /*
721 * If the block order is wrong, swap the arguments.
722 */
0e266570 723 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
2bd0ea18
NS
724 xfs_da_state_blk_t *tmp; /* temp for block swap */
725
726 tmp = blk1;
727 blk1 = blk2;
728 blk2 = tmp;
729 }
730 leaf1 = blk1->bp->data;
731 leaf2 = blk2->bp->data;
732 oldsum = INT_GET(leaf1->hdr.count, ARCH_CONVERT) + INT_GET(leaf2->hdr.count, ARCH_CONVERT);
733#ifdef DEBUG
734 oldstale = INT_GET(leaf1->hdr.stale, ARCH_CONVERT) + INT_GET(leaf2->hdr.stale, ARCH_CONVERT);
735#endif
736 mid = oldsum >> 1;
737 /*
738 * If the old leaf count was odd then the new one will be even,
739 * so we need to divide the new count evenly.
740 */
741 if (oldsum & 1) {
742 xfs_dahash_t midhash; /* middle entry hash value */
743
744 if (mid >= INT_GET(leaf1->hdr.count, ARCH_CONVERT))
745 midhash = INT_GET(leaf2->ents[mid - INT_GET(leaf1->hdr.count, ARCH_CONVERT)].hashval, ARCH_CONVERT);
746 else
747 midhash = INT_GET(leaf1->ents[mid].hashval, ARCH_CONVERT);
748 isleft = args->hashval <= midhash;
749 }
750 /*
751 * If the old count is even then the new count is odd, so there's
752 * no preferred side for the new entry.
753 * Pick the left one.
754 */
755 else
756 isleft = 1;
757 /*
dfc130f3 758 * Calculate moved entry count. Positive means left-to-right,
2bd0ea18
NS
759 * negative means right-to-left. Then move the entries.
760 */
761 count = INT_GET(leaf1->hdr.count, ARCH_CONVERT) - mid + (isleft == 0);
762 if (count > 0)
763 xfs_dir2_leafn_moveents(args, blk1->bp,
764 INT_GET(leaf1->hdr.count, ARCH_CONVERT) - count, blk2->bp, 0, count);
765 else if (count < 0)
766 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
767 INT_GET(leaf1->hdr.count, ARCH_CONVERT), count);
768 ASSERT(INT_GET(leaf1->hdr.count, ARCH_CONVERT) + INT_GET(leaf2->hdr.count, ARCH_CONVERT) == oldsum);
769 ASSERT(INT_GET(leaf1->hdr.stale, ARCH_CONVERT) + INT_GET(leaf2->hdr.stale, ARCH_CONVERT) == oldstale);
770 /*
771 * Mark whether we're inserting into the old or new leaf.
772 */
773 if (INT_GET(leaf1->hdr.count, ARCH_CONVERT) < INT_GET(leaf2->hdr.count, ARCH_CONVERT))
774 state->inleaf = swap;
775 else if (INT_GET(leaf1->hdr.count, ARCH_CONVERT) > INT_GET(leaf2->hdr.count, ARCH_CONVERT))
776 state->inleaf = !swap;
777 else
778 state->inleaf =
516de2da 779 swap ^ (blk1->index <= INT_GET(leaf1->hdr.count, ARCH_CONVERT));
2bd0ea18
NS
780 /*
781 * Adjust the expected index for insertion.
782 */
783 if (!state->inleaf)
784 blk2->index = blk1->index - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
516de2da
RC
785
786 /*
787 * Finally sanity check just to make sure we are not returning a negative index
788 */
789 if(blk2->index < 0) {
790 state->inleaf = 1;
791 blk2->index = 0;
792 cmn_err(CE_ALERT,
793 "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting orignal leaf: "
794 "blk1->index %d\n",
795 blk1->index);
796 }
2bd0ea18
NS
797}
798
799/*
800 * Remove an entry from a node directory.
801 * This removes the leaf entry and the data entry,
802 * and updates the free block if necessary.
803 */
804STATIC int /* error */
805xfs_dir2_leafn_remove(
806 xfs_da_args_t *args, /* operation arguments */
807 xfs_dabuf_t *bp, /* leaf buffer */
808 int index, /* leaf entry index */
809 xfs_da_state_blk_t *dblk, /* data block */
810 int *rval) /* resulting block needs join */
811{
812 xfs_dir2_data_t *data; /* data block structure */
813 xfs_dir2_db_t db; /* data block number */
814 xfs_dabuf_t *dbp; /* data block buffer */
815 xfs_dir2_data_entry_t *dep; /* data block entry */
816 xfs_inode_t *dp; /* incore directory inode */
817 xfs_dir2_leaf_t *leaf; /* leaf structure */
818 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
819 int longest; /* longest data free entry */
820 int off; /* data block entry offset */
821 xfs_mount_t *mp; /* filesystem mount point */
822 int needlog; /* need to log data header */
823 int needscan; /* need to rescan data frees */
824 xfs_trans_t *tp; /* transaction pointer */
825
826 xfs_dir2_trace_args_sb("leafn_remove", args, index, bp);
827 dp = args->dp;
828 tp = args->trans;
829 mp = dp->i_mount;
830 leaf = bp->data;
831 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
832 /*
833 * Point to the entry we're removing.
834 */
835 lep = &leaf->ents[index];
836 /*
837 * Extract the data block and offset from the entry.
838 */
839 db = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT));
840 ASSERT(dblk->blkno == db);
841 off = XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT));
842 ASSERT(dblk->index == off);
843 /*
844 * Kill the leaf entry by marking it stale.
845 * Log the leaf block changes.
846 */
847 INT_MOD(leaf->hdr.stale, ARCH_CONVERT, +1);
848 xfs_dir2_leaf_log_header(tp, bp);
849 INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_NULL_DATAPTR);
850 xfs_dir2_leaf_log_ents(tp, bp, index, index);
851 /*
852 * Make the data entry free. Keep track of the longest freespace
853 * in the data block in case it changes.
854 */
855 dbp = dblk->bp;
856 data = dbp->data;
857 dep = (xfs_dir2_data_entry_t *)((char *)data + off);
858 longest = INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT);
859 needlog = needscan = 0;
860 xfs_dir2_data_make_free(tp, dbp, off,
861 XFS_DIR2_DATA_ENTSIZE(dep->namelen), &needlog, &needscan);
862 /*
863 * Rescan the data block freespaces for bestfree.
864 * Log the data block header if needed.
865 */
866 if (needscan)
867 xfs_dir2_data_freescan(mp, data, &needlog, NULL);
868 if (needlog)
869 xfs_dir2_data_log_header(tp, dbp);
870 xfs_dir2_data_check(dp, dbp);
871 /*
872 * If the longest data block freespace changes, need to update
873 * the corresponding freeblock entry.
874 */
875 if (longest < INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT)) {
876 int error; /* error return value */
877 xfs_dabuf_t *fbp; /* freeblock buffer */
878 xfs_dir2_db_t fdb; /* freeblock block number */
879 int findex; /* index in freeblock entries */
dfc130f3 880 xfs_dir2_free_t *free; /* freeblock structure */
2bd0ea18
NS
881 int logfree; /* need to log free entry */
882
883 /*
884 * Convert the data block number to a free block,
885 * read in the free block.
886 */
887 fdb = XFS_DIR2_DB_TO_FDB(mp, db);
0e266570
NS
888 if ((error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, fdb),
889 -1, &fbp, XFS_DATA_FORK))) {
2bd0ea18
NS
890 return error;
891 }
892 free = fbp->data;
893 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
894 ASSERT(INT_GET(free->hdr.firstdb, ARCH_CONVERT) ==
895 XFS_DIR2_MAX_FREE_BESTS(mp) *
896 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
897 /*
898 * Calculate which entry we need to fix.
899 */
900 findex = XFS_DIR2_DB_TO_FDINDEX(mp, db);
901 longest = INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT);
902 /*
903 * If the data block is now empty we can get rid of it
904 * (usually).
905 */
906 if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) {
2bd0ea18
NS
907 /*
908 * Try to punch out the data block.
909 */
910 error = xfs_dir2_shrink_inode(args, db, dbp);
911 if (error == 0) {
912 dblk->bp = NULL;
913 data = NULL;
914 }
915 /*
916 * We can get ENOSPC if there's no space reservation.
917 * In this case just drop the buffer and some one else
918 * will eventually get rid of the empty block.
919 */
920 else if (error == ENOSPC && args->total == 0)
921 xfs_da_buf_done(dbp);
922 else
923 return error;
924 }
925 /*
926 * If we got rid of the data block, we can eliminate that entry
927 * in the free block.
928 */
929 if (data == NULL) {
2bd0ea18
NS
930 /*
931 * One less used entry in the free table.
932 */
933 INT_MOD(free->hdr.nused, ARCH_CONVERT, -1);
934 xfs_dir2_free_log_header(tp, fbp);
935 /*
936 * If this was the last entry in the table, we can
937 * trim the table size back. There might be other
938 * entries at the end referring to non-existent
939 * data blocks, get those too.
940 */
941 if (findex == INT_GET(free->hdr.nvalid, ARCH_CONVERT) - 1) {
942 int i; /* free entry index */
943
944 for (i = findex - 1;
945 i >= 0 && INT_GET(free->bests[i], ARCH_CONVERT) == NULLDATAOFF;
946 i--)
947 continue;
948 INT_SET(free->hdr.nvalid, ARCH_CONVERT, i + 1);
949 logfree = 0;
950 }
951 /*
952 * Not the last entry, just punch it out.
953 */
954 else {
955 INT_SET(free->bests[findex], ARCH_CONVERT, NULLDATAOFF);
956 logfree = 1;
957 }
958 /*
959 * If there are no useful entries left in the block,
960 * get rid of the block if we can.
961 */
46eca962 962 if (!free->hdr.nused) {
2bd0ea18
NS
963 error = xfs_dir2_shrink_inode(args, fdb, fbp);
964 if (error == 0) {
965 fbp = NULL;
966 logfree = 0;
967 } else if (error != ENOSPC || args->total != 0)
968 return error;
969 /*
970 * It's possible to get ENOSPC if there is no
971 * space reservation. In this case some one
972 * else will eventually get rid of this block.
973 */
974 }
975 }
976 /*
977 * Data block is not empty, just set the free entry to
978 * the new value.
979 */
980 else {
981 INT_SET(free->bests[findex], ARCH_CONVERT, longest);
982 logfree = 1;
983 }
984 /*
985 * Log the free entry that changed, unless we got rid of it.
986 */
987 if (logfree)
988 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
989 /*
990 * Drop the buffer if we still have it.
991 */
992 if (fbp)
993 xfs_da_buf_done(fbp);
994 }
995 xfs_dir2_leafn_check(dp, bp);
996 /*
997 * Return indication of whether this leaf block is emtpy enough
998 * to justify trying to join it with a neighbor.
999 */
1000 *rval =
1001 ((uint)sizeof(leaf->hdr) +
1002 (uint)sizeof(leaf->ents[0]) *
1003 (INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT))) <
1004 mp->m_dir_magicpct;
1005 return 0;
1006}
1007
1008/*
1009 * Split the leaf entries in the old block into old and new blocks.
1010 */
1011int /* error */
1012xfs_dir2_leafn_split(
1013 xfs_da_state_t *state, /* btree cursor */
1014 xfs_da_state_blk_t *oldblk, /* original block */
1015 xfs_da_state_blk_t *newblk) /* newly created block */
1016{
1017 xfs_da_args_t *args; /* operation arguments */
1018 xfs_dablk_t blkno; /* new leaf block number */
1019 int error; /* error return value */
1020 xfs_mount_t *mp; /* filesystem mount point */
1021
1022 /*
1023 * Allocate space for a new leaf node.
1024 */
1025 args = state->args;
1026 mp = args->dp->i_mount;
1027 ASSERT(args != NULL);
1028 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1029 error = xfs_da_grow_inode(args, &blkno);
1030 if (error) {
2bd0ea18
NS
1031 return error;
1032 }
1033 /*
1034 * Initialize the new leaf block.
1035 */
1036 error = xfs_dir2_leaf_init(args, XFS_DIR2_DA_TO_DB(mp, blkno),
1037 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1038 if (error) {
2bd0ea18
NS
1039 return error;
1040 }
1041 newblk->blkno = blkno;
1042 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1043 /*
1044 * Rebalance the entries across the two leaves, link the new
1045 * block into the leaves.
1046 */
1047 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1048 error = xfs_da_blk_link(state, oldblk, newblk);
1049 if (error) {
2bd0ea18
NS
1050 return error;
1051 }
1052 /*
1053 * Insert the new entry in the correct block.
1054 */
1055 if (state->inleaf)
1056 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1057 else
1058 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1059 /*
1060 * Update last hashval in each block since we added the name.
1061 */
1062 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1063 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1064 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1065 xfs_dir2_leafn_check(args->dp, newblk->bp);
1066 return error;
1067}
1068
1069/*
1070 * Check a leaf block and its neighbors to see if the block should be
1071 * collapsed into one or the other neighbor. Always keep the block
1072 * with the smaller block number.
1073 * If the current block is over 50% full, don't try to join it, return 0.
1074 * If the block is empty, fill in the state structure and return 2.
1075 * If it can be collapsed, fill in the state structure and return 1.
1076 * If nothing can be done, return 0.
1077 */
1078int /* error */
1079xfs_dir2_leafn_toosmall(
1080 xfs_da_state_t *state, /* btree cursor */
1081 int *action) /* resulting action to take */
1082{
1083 xfs_da_state_blk_t *blk; /* leaf block */
1084 xfs_dablk_t blkno; /* leaf block number */
1085 xfs_dabuf_t *bp; /* leaf buffer */
1086 int bytes; /* bytes in use */
1087 int count; /* leaf live entry count */
1088 int error; /* error return value */
1089 int forward; /* sibling block direction */
1090 int i; /* sibling counter */
1091 xfs_da_blkinfo_t *info; /* leaf block header */
1092 xfs_dir2_leaf_t *leaf; /* leaf structure */
1093 int rval; /* result from path_shift */
1094
1095 /*
1096 * Check for the degenerate case of the block being over 50% full.
1097 * If so, it's not worth even looking to see if we might be able
1098 * to coalesce with a sibling.
1099 */
1100 blk = &state->path.blk[state->path.active - 1];
1101 info = blk->bp->data;
1102 ASSERT(INT_GET(info->magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1103 leaf = (xfs_dir2_leaf_t *)info;
1104 count = INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT);
1105 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1106 if (bytes > (state->blocksize >> 1)) {
1107 /*
1108 * Blk over 50%, don't try to join.
1109 */
1110 *action = 0;
1111 return 0;
1112 }
1113 /*
1114 * Check for the degenerate case of the block being empty.
1115 * If the block is empty, we'll simply delete it, no need to
1116 * coalesce it with a sibling block. We choose (arbitrarily)
1117 * to merge with the forward block unless it is NULL.
1118 */
1119 if (count == 0) {
2bd0ea18
NS
1120 /*
1121 * Make altpath point to the block we want to keep and
1122 * path point to the block we want to drop (this one).
1123 */
46eca962 1124 forward = info->forw;
32181a02 1125 memcpy(&state->altpath, &state->path, sizeof(state->path));
2bd0ea18
NS
1126 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1127 &rval);
1128 if (error)
1129 return error;
1130 *action = rval ? 2 : 0;
1131 return 0;
1132 }
1133 /*
1134 * Examine each sibling block to see if we can coalesce with
1135 * at least 25% free space to spare. We need to figure out
1136 * whether to merge with the forward or the backward block.
1137 * We prefer coalescing with the lower numbered sibling so as
1138 * to shrink a directory over time.
1139 */
1140 forward = INT_GET(info->forw, ARCH_CONVERT) < INT_GET(info->back, ARCH_CONVERT);
1141 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1142 blkno = forward ?INT_GET( info->forw, ARCH_CONVERT) : INT_GET(info->back, ARCH_CONVERT);
1143 if (blkno == 0)
1144 continue;
1145 /*
1146 * Read the sibling leaf block.
1147 */
0e266570 1148 if ((error =
2bd0ea18 1149 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
0e266570 1150 -1, &bp, XFS_DATA_FORK))) {
2bd0ea18
NS
1151 return error;
1152 }
1153 ASSERT(bp != NULL);
1154 /*
1155 * Count bytes in the two blocks combined.
1156 */
1157 leaf = (xfs_dir2_leaf_t *)info;
1158 count = INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT);
1159 bytes = state->blocksize - (state->blocksize >> 2);
1160 leaf = bp->data;
1161 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1162 count += INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT);
1163 bytes -= count * (uint)sizeof(leaf->ents[0]);
1164 /*
1165 * Fits with at least 25% to spare.
1166 */
1167 if (bytes >= 0)
1168 break;
1169 xfs_da_brelse(state->args->trans, bp);
1170 }
1171 /*
1172 * Didn't like either block, give up.
1173 */
1174 if (i >= 2) {
1175 *action = 0;
1176 return 0;
1177 }
1178 /*
1179 * Done with the sibling leaf block here, drop the dabuf
1180 * so path_shift can get it.
1181 */
1182 xfs_da_buf_done(bp);
1183 /*
1184 * Make altpath point to the block we want to keep (the lower
1185 * numbered block) and path point to the block we want to drop.
1186 */
32181a02 1187 memcpy(&state->altpath, &state->path, sizeof(state->path));
2bd0ea18
NS
1188 if (blkno < blk->blkno)
1189 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1190 &rval);
1191 else
1192 error = xfs_da_path_shift(state, &state->path, forward, 0,
1193 &rval);
1194 if (error) {
2bd0ea18
NS
1195 return error;
1196 }
1197 *action = rval ? 0 : 1;
1198 return 0;
1199}
1200
1201/*
1202 * Move all the leaf entries from drop_blk to save_blk.
1203 * This is done as part of a join operation.
1204 */
1205void
1206xfs_dir2_leafn_unbalance(
1207 xfs_da_state_t *state, /* cursor */
1208 xfs_da_state_blk_t *drop_blk, /* dead block */
1209 xfs_da_state_blk_t *save_blk) /* surviving block */
1210{
1211 xfs_da_args_t *args; /* operation arguments */
1212 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1213 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1214
1215 args = state->args;
1216 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1217 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1218 drop_leaf = drop_blk->bp->data;
1219 save_leaf = save_blk->bp->data;
1220 ASSERT(INT_GET(drop_leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1221 ASSERT(INT_GET(save_leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
1222 /*
1223 * If there are any stale leaf entries, take this opportunity
1224 * to purge them.
1225 */
1226 if (INT_GET(drop_leaf->hdr.stale, ARCH_CONVERT))
1227 xfs_dir2_leaf_compact(args, drop_blk->bp);
1228 if (INT_GET(save_leaf->hdr.stale, ARCH_CONVERT))
1229 xfs_dir2_leaf_compact(args, save_blk->bp);
1230 /*
1231 * Move the entries from drop to the appropriate end of save.
1232 */
1233 drop_blk->hashval = INT_GET(drop_leaf->ents[INT_GET(drop_leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
1234 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1235 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
1236 INT_GET(drop_leaf->hdr.count, ARCH_CONVERT));
1237 else
1238 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
1239 INT_GET(save_leaf->hdr.count, ARCH_CONVERT), INT_GET(drop_leaf->hdr.count, ARCH_CONVERT));
1240 save_blk->hashval = INT_GET(save_leaf->ents[INT_GET(save_leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
1241 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1242}
1243
1244/*
1245 * Top-level node form directory addname routine.
1246 */
1247int /* error */
1248xfs_dir2_node_addname(
1249 xfs_da_args_t *args) /* operation arguments */
1250{
1251 xfs_da_state_blk_t *blk; /* leaf block for insert */
1252 int error; /* error return value */
1253 int rval; /* sub-return value */
1254 xfs_da_state_t *state; /* btree cursor */
1255
1256 xfs_dir2_trace_args("node_addname", args);
1257 /*
1258 * Allocate and initialize the state (btree cursor).
1259 */
1260 state = xfs_da_state_alloc();
1261 state->args = args;
1262 state->mp = args->dp->i_mount;
1263 state->blocksize = state->mp->m_dirblksize;
6bef826c 1264 state->node_ents = state->mp->m_dir_node_ents;
2bd0ea18
NS
1265 /*
1266 * Look up the name. We're not supposed to find it, but
1267 * this gives us the insertion point.
1268 */
1269 error = xfs_da_node_lookup_int(state, &rval);
1270 if (error)
1271 rval = error;
1272 if (rval != ENOENT) {
2bd0ea18
NS
1273 goto done;
1274 }
1275 /*
1276 * Add the data entry to a data block.
1277 * Extravalid is set to a freeblock found by lookup.
1278 */
1279 rval = xfs_dir2_node_addname_int(args,
1280 state->extravalid ? &state->extrablk : NULL);
1281 if (rval) {
2bd0ea18
NS
1282 goto done;
1283 }
1284 blk = &state->path.blk[state->path.active - 1];
1285 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1286 /*
1287 * Add the new leaf entry.
1288 */
1289 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1290 if (rval == 0) {
1291 /*
1292 * It worked, fix the hash values up the btree.
1293 */
1294 if (!args->justcheck)
1295 xfs_da_fixhashpath(state, &state->path);
1296 } else {
2bd0ea18
NS
1297 /*
1298 * It didn't work, we need to split the leaf block.
1299 */
1300 if (args->total == 0) {
1301 ASSERT(rval == ENOSPC);
1302 goto done;
1303 }
1304 /*
1305 * Split the leaf block and insert the new entry.
1306 */
1307 rval = xfs_da_split(state);
1308 }
1309done:
1310 xfs_da_state_free(state);
1311 return rval;
1312}
1313
2bd0ea18
NS
1314/*
1315 * Add the data entry for a node-format directory name addition.
1316 * The leaf entry is added in xfs_dir2_leafn_add.
1317 * We may enter with a freespace block that the lookup found.
1318 */
1319STATIC int /* error */
1320xfs_dir2_node_addname_int(
1321 xfs_da_args_t *args, /* operation arguments */
1322 xfs_da_state_blk_t *fblk) /* optional freespace block */
1323{
1324 xfs_dir2_data_t *data; /* data block structure */
1325 xfs_dir2_db_t dbno; /* data block number */
1326 xfs_dabuf_t *dbp; /* data block buffer */
1327 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1328 xfs_inode_t *dp; /* incore directory inode */
1329 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1330 int error; /* error return value */
1331 xfs_dir2_db_t fbno; /* freespace block number */
1332 xfs_dabuf_t *fbp; /* freespace buffer */
1333 int findex; /* freespace entry index */
0e266570 1334 xfs_dir2_free_t *free=NULL; /* freespace block structure */
2bd0ea18 1335 xfs_dir2_db_t ifbno; /* initial freespace block no */
9fe055ab 1336 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
2bd0ea18
NS
1337 int length; /* length of the new entry */
1338 int logfree; /* need to log free entry */
1339 xfs_mount_t *mp; /* filesystem mount point */
1340 int needlog; /* need to log data header */
1341 int needscan; /* need to rescan data frees */
1342 xfs_dir2_data_off_t *tagp; /* data entry tag pointer */
1343 xfs_trans_t *tp; /* transaction pointer */
1344
1345 dp = args->dp;
1346 mp = dp->i_mount;
1347 tp = args->trans;
1348 length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
1349 /*
1350 * If we came in with a freespace block that means that lookup
dfc130f3 1351 * found an entry with our hash value. This is the freespace
2bd0ea18
NS
1352 * block for that data entry.
1353 */
1354 if (fblk) {
1355 fbp = fblk->bp;
1356 /*
1357 * Remember initial freespace block number.
1358 */
1359 ifbno = fblk->blkno;
1360 free = fbp->data;
1361 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1362 findex = fblk->index;
1363 /*
1364 * This means the free entry showed that the data block had
1365 * space for our entry, so we remembered it.
1366 * Use that data block.
1367 */
1368 if (findex >= 0) {
1369 ASSERT(findex < INT_GET(free->hdr.nvalid, ARCH_CONVERT));
1370 ASSERT(INT_GET(free->bests[findex], ARCH_CONVERT) != NULLDATAOFF);
1371 ASSERT(INT_GET(free->bests[findex], ARCH_CONVERT) >= length);
1372 dbno = INT_GET(free->hdr.firstdb, ARCH_CONVERT) + findex;
1373 }
1374 /*
1375 * The data block looked at didn't have enough room.
1376 * We'll start at the beginning of the freespace entries.
1377 */
1378 else {
1379 dbno = -1;
1380 findex = 0;
1381 }
1382 }
1383 /*
1384 * Didn't come in with a freespace block, so don't have a data block.
1385 */
1386 else {
1387 ifbno = dbno = -1;
1388 fbp = NULL;
1389 findex = 0;
1390 }
1391 /*
5000d01d 1392 * If we don't have a data block yet, we're going to scan the
2bd0ea18
NS
1393 * freespace blocks looking for one. Figure out what the
1394 * highest freespace block number is.
1395 */
1396 if (dbno == -1) {
1397 xfs_fileoff_t fo; /* freespace block number */
1398
0e266570 1399 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
2bd0ea18
NS
1400 return error;
1401 lastfbno = XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)fo);
1402 fbno = ifbno;
2bd0ea18
NS
1403 }
1404 /*
1405 * While we haven't identified a data block, search the freeblock
dfc130f3 1406 * data for a good data block. If we find a null freeblock entry,
2bd0ea18
NS
1407 * indicating a hole in the data blocks, remember that.
1408 */
1409 while (dbno == -1) {
1410 /*
1411 * If we don't have a freeblock in hand, get the next one.
1412 */
1413 if (fbp == NULL) {
1414 /*
1415 * Happens the first time through unless lookup gave
1416 * us a freespace block to start with.
1417 */
1418 if (++fbno == 0)
1419 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1420 /*
1421 * If it's ifbno we already looked at it.
1422 */
1423 if (fbno == ifbno)
1424 fbno++;
1425 /*
1426 * If it's off the end we're done.
1427 */
1428 if (fbno >= lastfbno)
1429 break;
1430 /*
1431 * Read the block. There can be holes in the
1432 * freespace blocks, so this might not succeed.
1433 * This should be really rare, so there's no reason
1434 * to avoid it.
1435 */
0e266570 1436 if ((error = xfs_da_read_buf(tp, dp,
4ca431fc 1437 XFS_DIR2_DB_TO_DA(mp, fbno), -2, &fbp,
0e266570 1438 XFS_DATA_FORK))) {
2bd0ea18
NS
1439 return error;
1440 }
4ca431fc 1441 if (unlikely(fbp == NULL)) {
2bd0ea18
NS
1442 continue;
1443 }
1444 free = fbp->data;
1445 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1446 findex = 0;
1447 }
1448 /*
1449 * Look at the current free entry. Is it good enough?
1450 */
1451 if (INT_GET(free->bests[findex], ARCH_CONVERT) != NULLDATAOFF &&
1452 INT_GET(free->bests[findex], ARCH_CONVERT) >= length)
1453 dbno = INT_GET(free->hdr.firstdb, ARCH_CONVERT) + findex;
1454 else {
2bd0ea18
NS
1455 /*
1456 * Are we done with the freeblock?
1457 */
1458 if (++findex == INT_GET(free->hdr.nvalid, ARCH_CONVERT)) {
2bd0ea18
NS
1459 /*
1460 * Drop the block.
1461 */
1462 xfs_da_brelse(tp, fbp);
1463 fbp = NULL;
1464 if (fblk && fblk->bp)
1465 fblk->bp = NULL;
1466 }
1467 }
1468 }
2bd0ea18
NS
1469 /*
1470 * If we don't have a data block, we need to allocate one and make
1471 * the freespace entries refer to it.
1472 */
4ca431fc 1473 if (unlikely(dbno == -1)) {
2bd0ea18
NS
1474 /*
1475 * Not allowed to allocate, return failure.
1476 */
1477 if (args->justcheck || args->total == 0) {
1478 /*
1479 * Drop the freespace buffer unless it came from our
1480 * caller.
1481 */
4ca431fc 1482 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
2bd0ea18
NS
1483 xfs_da_buf_done(fbp);
1484 return XFS_ERROR(ENOSPC);
1485 }
1486 /*
1487 * Allocate and initialize the new data block.
1488 */
23aab3f9
NS
1489 if (unlikely((error = xfs_dir2_grow_inode(args,
1490 XFS_DIR2_DATA_SPACE,
1491 &dbno)) ||
1492 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
2bd0ea18
NS
1493 /*
1494 * Drop the freespace buffer unless it came from our
1495 * caller.
1496 */
4ca431fc 1497 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
2bd0ea18
NS
1498 xfs_da_buf_done(fbp);
1499 return error;
1500 }
1501 /*
23aab3f9 1502 * If (somehow) we have a freespace block, get rid of it.
2bd0ea18 1503 */
23aab3f9
NS
1504 if (fbp)
1505 xfs_da_brelse(tp, fbp);
1506 if (fblk && fblk->bp)
1507 fblk->bp = NULL;
4ca431fc
NS
1508
1509 /*
23aab3f9
NS
1510 * Get the freespace block corresponding to the data block
1511 * that was just allocated.
4ca431fc 1512 */
23aab3f9
NS
1513 fbno = XFS_DIR2_DB_TO_FDB(mp, dbno);
1514 if (unlikely(error = xfs_da_read_buf(tp, dp,
1515 XFS_DIR2_DB_TO_DA(mp, fbno), -2, &fbp,
1516 XFS_DATA_FORK))) {
1517 xfs_da_buf_done(dbp);
1518 return error;
1519 }
1520 /*
1521 * If there wasn't a freespace block, the read will
1522 * return a NULL fbp. Allocate and initialize a new one.
1523 */
1524 if( fbp == NULL ) {
4ca431fc
NS
1525 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1526 &fbno))) {
1527 return error;
1528 }
1529
23aab3f9 1530 if (unlikely(XFS_DIR2_DB_TO_FDB(mp, dbno) != fbno)) {
4ca431fc 1531 cmn_err(CE_ALERT,
23aab3f9
NS
1532 "xfs_dir2_node_addname_int: dir ino "
1533 "%llu needed freesp block %lld for\n"
1534 " data block %lld, got %lld\n"
1535 " ifbno %llu lastfbno %d\n",
33a4da69
NS
1536 (unsigned long long)dp->i_ino,
1537 (long long)XFS_DIR2_DB_TO_FDB(mp, dbno),
1538 (long long)dbno, (long long)fbno,
1539 (unsigned long long)ifbno, lastfbno);
23aab3f9
NS
1540 if (fblk) {
1541 cmn_err(CE_ALERT,
33a4da69 1542 " fblk 0x%p blkno %llu "
23aab3f9 1543 "index %d magic 0x%x\n",
33a4da69
NS
1544 fblk,
1545 (unsigned long long)fblk->blkno,
23aab3f9
NS
1546 fblk->index,
1547 fblk->magic);
1548 } else {
1549 cmn_err(CE_ALERT,
1550 " ... fblk is NULL\n");
1551 }
4ca431fc
NS
1552 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1553 XFS_ERRLEVEL_LOW, mp);
1554 return XFS_ERROR(EFSCORRUPTED);
1555 }
1556
1557 /*
1558 * Get a buffer for the new block.
1559 */
1560 if ((error = xfs_da_get_buf(tp, dp,
1561 XFS_DIR2_DB_TO_DA(mp, fbno),
1562 -1, &fbp, XFS_DATA_FORK))) {
1563 return error;
1564 }
2bd0ea18 1565 ASSERT(fbp != NULL);
4ca431fc
NS
1566
1567 /*
1568 * Initialize the new block to be empty, and remember
1569 * its first slot as our empty slot.
1570 */
2bd0ea18 1571 free = fbp->data;
4ca431fc
NS
1572 INT_SET(free->hdr.magic, ARCH_CONVERT, XFS_DIR2_FREE_MAGIC);
1573 INT_SET(free->hdr.firstdb, ARCH_CONVERT,
1574 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1575 XFS_DIR2_MAX_FREE_BESTS(mp));
46eca962
NS
1576 free->hdr.nvalid = 0;
1577 free->hdr.nused = 0;
23aab3f9
NS
1578 } else {
1579 free = fbp->data;
1580 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
2bd0ea18 1581 }
4ca431fc 1582
2bd0ea18
NS
1583 /*
1584 * Set the freespace block index from the data block number.
1585 */
1586 findex = XFS_DIR2_DB_TO_FDINDEX(mp, dbno);
1587 /*
1588 * If it's after the end of the current entries in the
1589 * freespace block, extend that table.
1590 */
1591 if (findex >= INT_GET(free->hdr.nvalid, ARCH_CONVERT)) {
1592 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
1593 INT_SET(free->hdr.nvalid, ARCH_CONVERT, findex + 1);
1594 /*
1595 * Tag new entry so nused will go up.
1596 */
1597 INT_SET(free->bests[findex], ARCH_CONVERT, NULLDATAOFF);
1598 }
1599 /*
1600 * If this entry was for an empty data block
1601 * (this should always be true) then update the header.
1602 */
1603 if (INT_GET(free->bests[findex], ARCH_CONVERT) == NULLDATAOFF) {
1604 INT_MOD(free->hdr.nused, ARCH_CONVERT, +1);
1605 xfs_dir2_free_log_header(tp, fbp);
1606 }
1607 /*
1608 * Update the real value in the table.
1609 * We haven't allocated the data entry yet so this will
1610 * change again.
1611 */
1612 data = dbp->data;
5000d01d 1613 INT_COPY(free->bests[findex], data->hdr.bestfree[0].length, ARCH_CONVERT);
2bd0ea18
NS
1614 logfree = 1;
1615 }
1616 /*
1617 * We had a data block so we don't have to make a new one.
1618 */
1619 else {
1620 /*
1621 * If just checking, we succeeded.
1622 */
1623 if (args->justcheck) {
4ca431fc 1624 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
2bd0ea18
NS
1625 xfs_da_buf_done(fbp);
1626 return 0;
1627 }
1628 /*
1629 * Read the data block in.
1630 */
4ca431fc
NS
1631 if (unlikely(
1632 error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, dbno),
0e266570 1633 -1, &dbp, XFS_DATA_FORK))) {
4ca431fc 1634 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
2bd0ea18
NS
1635 xfs_da_buf_done(fbp);
1636 return error;
1637 }
1638 data = dbp->data;
1639 logfree = 0;
1640 }
1641 ASSERT(INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) >= length);
1642 /*
1643 * Point to the existing unused space.
1644 */
1645 dup = (xfs_dir2_data_unused_t *)
1646 ((char *)data + INT_GET(data->hdr.bestfree[0].offset, ARCH_CONVERT));
1647 needscan = needlog = 0;
1648 /*
1649 * Mark the first part of the unused space, inuse for us.
1650 */
1651 xfs_dir2_data_use_free(tp, dbp, dup,
1652 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
1653 &needlog, &needscan);
1654 /*
1655 * Fill in the new entry and log it.
1656 */
1657 dep = (xfs_dir2_data_entry_t *)dup;
1658 INT_SET(dep->inumber, ARCH_CONVERT, args->inumber);
1659 dep->namelen = args->namelen;
32181a02 1660 memcpy(dep->name, args->name, dep->namelen);
2bd0ea18
NS
1661 tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
1662 INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)data));
1663 xfs_dir2_data_log_entry(tp, dbp, dep);
1664 /*
1665 * Rescan the block for bestfree if needed.
1666 */
1667 if (needscan)
1668 xfs_dir2_data_freescan(mp, data, &needlog, NULL);
1669 /*
1670 * Log the data block header if needed.
1671 */
1672 if (needlog)
1673 xfs_dir2_data_log_header(tp, dbp);
1674 /*
1675 * If the freespace entry is now wrong, update it.
1676 */
1677 if (INT_GET(free->bests[findex], ARCH_CONVERT) != INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT)) {
1678 INT_COPY(free->bests[findex], data->hdr.bestfree[0].length, ARCH_CONVERT);
1679 logfree = 1;
1680 }
1681 /*
1682 * Log the freespace entry if needed.
1683 */
1684 if (logfree)
1685 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1686 /*
1687 * If the caller didn't hand us the freespace block, drop it.
1688 */
4ca431fc 1689 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
2bd0ea18
NS
1690 xfs_da_buf_done(fbp);
1691 /*
1692 * Return the data block and offset in args, then drop the data block.
1693 */
1694 args->blkno = (xfs_dablk_t)dbno;
1695 args->index = INT_GET(*tagp, ARCH_CONVERT);
1696 xfs_da_buf_done(dbp);
1697 return 0;
1698}
1699
1700/*
1701 * Lookup an entry in a node-format directory.
1702 * All the real work happens in xfs_da_node_lookup_int.
1703 * The only real output is the inode number of the entry.
1704 */
1705int /* error */
1706xfs_dir2_node_lookup(
1707 xfs_da_args_t *args) /* operation arguments */
1708{
1709 int error; /* error return value */
1710 int i; /* btree level */
1711 int rval; /* operation return value */
1712 xfs_da_state_t *state; /* btree cursor */
1713
1714 xfs_dir2_trace_args("node_lookup", args);
1715 /*
1716 * Allocate and initialize the btree cursor.
1717 */
1718 state = xfs_da_state_alloc();
1719 state->args = args;
1720 state->mp = args->dp->i_mount;
1721 state->blocksize = state->mp->m_dirblksize;
6bef826c 1722 state->node_ents = state->mp->m_dir_node_ents;
2bd0ea18
NS
1723 /*
1724 * Fill in the path to the entry in the cursor.
1725 */
1726 error = xfs_da_node_lookup_int(state, &rval);
1727 if (error)
1728 rval = error;
1729 /*
1730 * Release the btree blocks and leaf block.
1731 */
1732 for (i = 0; i < state->path.active; i++) {
1733 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1734 state->path.blk[i].bp = NULL;
1735 }
1736 /*
1737 * Release the data block if we have it.
1738 */
1739 if (state->extravalid && state->extrablk.bp) {
1740 xfs_da_brelse(args->trans, state->extrablk.bp);
1741 state->extrablk.bp = NULL;
1742 }
1743 xfs_da_state_free(state);
1744 return rval;
1745}
1746
1747/*
1748 * Remove an entry from a node-format directory.
1749 */
1750int /* error */
1751xfs_dir2_node_removename(
1752 xfs_da_args_t *args) /* operation arguments */
1753{
1754 xfs_da_state_blk_t *blk; /* leaf block */
1755 int error; /* error return value */
1756 int rval; /* operation return value */
1757 xfs_da_state_t *state; /* btree cursor */
1758
1759 xfs_dir2_trace_args("node_removename", args);
1760 /*
1761 * Allocate and initialize the btree cursor.
1762 */
1763 state = xfs_da_state_alloc();
1764 state->args = args;
1765 state->mp = args->dp->i_mount;
1766 state->blocksize = state->mp->m_dirblksize;
6bef826c 1767 state->node_ents = state->mp->m_dir_node_ents;
2bd0ea18
NS
1768 /*
1769 * Look up the entry we're deleting, set up the cursor.
1770 */
1771 error = xfs_da_node_lookup_int(state, &rval);
1772 if (error) {
2bd0ea18
NS
1773 rval = error;
1774 }
1775 /*
1776 * Didn't find it, upper layer screwed up.
1777 */
1778 if (rval != EEXIST) {
2bd0ea18
NS
1779 xfs_da_state_free(state);
1780 return rval;
1781 }
1782 blk = &state->path.blk[state->path.active - 1];
1783 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1784 ASSERT(state->extravalid);
1785 /*
1786 * Remove the leaf and data entries.
1787 * Extrablk refers to the data block.
1788 */
1789 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1790 &state->extrablk, &rval);
1791 if (error) {
2bd0ea18
NS
1792 return error;
1793 }
1794 /*
1795 * Fix the hash values up the btree.
1796 */
1797 xfs_da_fixhashpath(state, &state->path);
1798 /*
1799 * If we need to join leaf blocks, do it.
1800 */
1801 if (rval && state->path.active > 1)
1802 error = xfs_da_join(state);
1803 /*
1804 * If no errors so far, try conversion to leaf format.
1805 */
1806 if (!error)
1807 error = xfs_dir2_node_to_leaf(state);
1808 xfs_da_state_free(state);
1809 return error;
1810}
1811
1812/*
1813 * Replace an entry's inode number in a node-format directory.
1814 */
1815int /* error */
1816xfs_dir2_node_replace(
1817 xfs_da_args_t *args) /* operation arguments */
1818{
1819 xfs_da_state_blk_t *blk; /* leaf block */
1820 xfs_dir2_data_t *data; /* data block structure */
1821 xfs_dir2_data_entry_t *dep; /* data entry changed */
1822 int error; /* error return value */
1823 int i; /* btree level */
1824 xfs_ino_t inum; /* new inode number */
1825 xfs_dir2_leaf_t *leaf; /* leaf structure */
1826 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1827 int rval; /* internal return value */
1828 xfs_da_state_t *state; /* btree cursor */
1829
1830 xfs_dir2_trace_args("node_replace", args);
1831 /*
1832 * Allocate and initialize the btree cursor.
1833 */
1834 state = xfs_da_state_alloc();
1835 state->args = args;
1836 state->mp = args->dp->i_mount;
1837 state->blocksize = state->mp->m_dirblksize;
6bef826c 1838 state->node_ents = state->mp->m_dir_node_ents;
2bd0ea18
NS
1839 inum = args->inumber;
1840 /*
1841 * Lookup the entry to change in the btree.
1842 */
1843 error = xfs_da_node_lookup_int(state, &rval);
1844 if (error) {
2bd0ea18
NS
1845 rval = error;
1846 }
1847 /*
1848 * It should be found, since the vnodeops layer has looked it up
1849 * and locked it. But paranoia is good.
1850 */
1851 if (rval == EEXIST) {
1852 /*
1853 * Find the leaf entry.
1854 */
1855 blk = &state->path.blk[state->path.active - 1];
1856 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1857 leaf = blk->bp->data;
1858 lep = &leaf->ents[blk->index];
1859 ASSERT(state->extravalid);
1860 /*
1861 * Point to the data entry.
1862 */
1863 data = state->extrablk.bp->data;
1864 ASSERT(INT_GET(data->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC);
1865 dep = (xfs_dir2_data_entry_t *)
1866 ((char *)data +
1867 XFS_DIR2_DATAPTR_TO_OFF(state->mp, INT_GET(lep->address, ARCH_CONVERT)));
1868 ASSERT(inum != INT_GET(dep->inumber, ARCH_CONVERT));
1869 /*
1870 * Fill in the new inode number and log the entry.
1871 */
1872 INT_SET(dep->inumber, ARCH_CONVERT, inum);
1873 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1874 rval = 0;
1875 }
1876 /*
1877 * Didn't find it, and we're holding a data block. Drop it.
1878 */
1879 else if (state->extravalid) {
2bd0ea18
NS
1880 xfs_da_brelse(args->trans, state->extrablk.bp);
1881 state->extrablk.bp = NULL;
1882 }
1883 /*
1884 * Release all the buffers in the cursor.
1885 */
1886 for (i = 0; i < state->path.active; i++) {
1887 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1888 state->path.blk[i].bp = NULL;
1889 }
1890 xfs_da_state_free(state);
1891 return rval;
1892}
1893
1894/*
1895 * Trim off a trailing empty freespace block.
1896 * Return (in rvalp) 1 if we did it, 0 if not.
1897 */
1898int /* error */
1899xfs_dir2_node_trim_free(
1900 xfs_da_args_t *args, /* operation arguments */
1901 xfs_fileoff_t fo, /* free block number */
1902 int *rvalp) /* out: did something */
1903{
1904 xfs_dabuf_t *bp; /* freespace buffer */
1905 xfs_inode_t *dp; /* incore directory inode */
1906 int error; /* error return code */
1907 xfs_dir2_free_t *free; /* freespace structure */
1908 xfs_mount_t *mp; /* filesystem mount point */
1909 xfs_trans_t *tp; /* transaction pointer */
1910
1911 dp = args->dp;
1912 mp = dp->i_mount;
1913 tp = args->trans;
1914 /*
1915 * Read the freespace block.
1916 */
4ca431fc 1917 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
0e266570 1918 XFS_DATA_FORK))) {
2bd0ea18
NS
1919 return error;
1920 }
4ca431fc
NS
1921
1922 /*
1923 * There can be holes in freespace. If fo is a hole, there's
1924 * nothing to do.
1925 */
1926 if (bp == NULL) {
1927 return 0;
1928 }
2bd0ea18
NS
1929 free = bp->data;
1930 ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC);
1931 /*
1932 * If there are used entries, there's nothing to do.
1933 */
1934 if (INT_GET(free->hdr.nused, ARCH_CONVERT) > 0) {
1935 xfs_da_brelse(tp, bp);
1936 *rvalp = 0;
1937 return 0;
1938 }
1939 /*
1940 * Blow the block away.
1941 */
0e266570 1942 if ((error =
2bd0ea18 1943 xfs_dir2_shrink_inode(args, XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)fo),
0e266570 1944 bp))) {
2bd0ea18
NS
1945 /*
1946 * Can't fail with ENOSPC since that only happens with no
1947 * space reservation, when breaking up an extent into two
1948 * pieces. This is the last block of an extent.
1949 */
1950 ASSERT(error != ENOSPC);
1951 xfs_da_brelse(tp, bp);
1952 return error;
1953 }
1954 /*
1955 * Return that we succeeded.
1956 */
1957 *rvalp = 1;
1958 return 0;
1959}