]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame_incremental - libxfs/xfs_dir2_node.c
xfsprogs: Release v6.15.0
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dir2_node.c
... / ...
CommitLineData
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
5 * All Rights Reserved.
6 */
7#include "libxfs_priv.h"
8#include "xfs_fs.h"
9#include "xfs_shared.h"
10#include "xfs_format.h"
11#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
13#include "xfs_mount.h"
14#include "xfs_inode.h"
15#include "xfs_bmap.h"
16#include "xfs_dir2.h"
17#include "xfs_dir2_priv.h"
18#include "xfs_trace.h"
19#include "xfs_trans.h"
20#include "xfs_health.h"
21
22/*
23 * Function declarations.
24 */
25static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
26 int index);
27static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
28 xfs_da_state_blk_t *blk1,
29 xfs_da_state_blk_t *blk2);
30static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
31 int index, xfs_da_state_blk_t *dblk,
32 int *rval);
33
34/*
35 * Convert data space db to the corresponding free db.
36 */
37static xfs_dir2_db_t
38xfs_dir2_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
39{
40 return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
41 (db / geo->free_max_bests);
42}
43
44/*
45 * Convert data space db to the corresponding index in a free db.
46 */
47static int
48xfs_dir2_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
49{
50 return db % geo->free_max_bests;
51}
52
53/*
54 * Check internal consistency of a leafn block.
55 */
56#ifdef DEBUG
57static xfs_failaddr_t
58xfs_dir3_leafn_check(
59 struct xfs_inode *dp,
60 struct xfs_buf *bp)
61{
62 struct xfs_dir2_leaf *leaf = bp->b_addr;
63 struct xfs_dir3_icleaf_hdr leafhdr;
64
65 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
66
67 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
68 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
69 if (be64_to_cpu(leaf3->info.blkno) != xfs_buf_daddr(bp))
70 return __this_address;
71 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
72 return __this_address;
73
74 return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false);
75}
76
77static inline void
78xfs_dir3_leaf_check(
79 struct xfs_inode *dp,
80 struct xfs_buf *bp)
81{
82 xfs_failaddr_t fa;
83
84 fa = xfs_dir3_leafn_check(dp, bp);
85 if (!fa)
86 return;
87 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
88 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
89 fa);
90 ASSERT(0);
91}
92#else
93#define xfs_dir3_leaf_check(dp, bp)
94#endif
95
96static xfs_failaddr_t
97xfs_dir3_free_verify(
98 struct xfs_buf *bp)
99{
100 struct xfs_mount *mp = bp->b_mount;
101 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
102
103 if (!xfs_verify_magic(bp, hdr->magic))
104 return __this_address;
105
106 if (xfs_has_crc(mp)) {
107 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
108
109 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
110 return __this_address;
111 if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
112 return __this_address;
113 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
114 return __this_address;
115 }
116
117 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
118
119 return NULL;
120}
121
122static void
123xfs_dir3_free_read_verify(
124 struct xfs_buf *bp)
125{
126 struct xfs_mount *mp = bp->b_mount;
127 xfs_failaddr_t fa;
128
129 if (xfs_has_crc(mp) &&
130 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
131 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
132 else {
133 fa = xfs_dir3_free_verify(bp);
134 if (fa)
135 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
136 }
137}
138
139static void
140xfs_dir3_free_write_verify(
141 struct xfs_buf *bp)
142{
143 struct xfs_mount *mp = bp->b_mount;
144 struct xfs_buf_log_item *bip = bp->b_log_item;
145 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
146 xfs_failaddr_t fa;
147
148 fa = xfs_dir3_free_verify(bp);
149 if (fa) {
150 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
151 return;
152 }
153
154 if (!xfs_has_crc(mp))
155 return;
156
157 if (bip)
158 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
159
160 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
161}
162
163const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
164 .name = "xfs_dir3_free",
165 .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC),
166 cpu_to_be32(XFS_DIR3_FREE_MAGIC) },
167 .verify_read = xfs_dir3_free_read_verify,
168 .verify_write = xfs_dir3_free_write_verify,
169 .verify_struct = xfs_dir3_free_verify,
170};
171
172/* Everything ok in the free block header? */
173static xfs_failaddr_t
174xfs_dir3_free_header_check(
175 struct xfs_buf *bp,
176 xfs_ino_t owner,
177 xfs_dablk_t fbno)
178{
179 struct xfs_mount *mp = bp->b_mount;
180 int maxbests = mp->m_dir_geo->free_max_bests;
181 unsigned int firstdb;
182
183 firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
184 xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
185 maxbests;
186 if (xfs_has_crc(mp)) {
187 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
188
189 if (be32_to_cpu(hdr3->firstdb) != firstdb)
190 return __this_address;
191 if (be32_to_cpu(hdr3->nvalid) > maxbests)
192 return __this_address;
193 if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
194 return __this_address;
195 if (be64_to_cpu(hdr3->hdr.owner) != owner)
196 return __this_address;
197 } else {
198 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
199
200 if (be32_to_cpu(hdr->firstdb) != firstdb)
201 return __this_address;
202 if (be32_to_cpu(hdr->nvalid) > maxbests)
203 return __this_address;
204 if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
205 return __this_address;
206 }
207 return NULL;
208}
209
210static int
211__xfs_dir3_free_read(
212 struct xfs_trans *tp,
213 struct xfs_inode *dp,
214 xfs_ino_t owner,
215 xfs_dablk_t fbno,
216 unsigned int flags,
217 struct xfs_buf **bpp)
218{
219 xfs_failaddr_t fa;
220 int err;
221
222 err = xfs_da_read_buf(tp, dp, fbno, flags, bpp, XFS_DATA_FORK,
223 &xfs_dir3_free_buf_ops);
224 if (err || !*bpp)
225 return err;
226
227 /* Check things that we can't do in the verifier. */
228 fa = xfs_dir3_free_header_check(*bpp, owner, fbno);
229 if (fa) {
230 __xfs_buf_mark_corrupt(*bpp, fa);
231 xfs_trans_brelse(tp, *bpp);
232 *bpp = NULL;
233 xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
234 return -EFSCORRUPTED;
235 }
236
237 /* try read returns without an error or *bpp if it lands in a hole */
238 if (tp)
239 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
240
241 return 0;
242}
243
244void
245xfs_dir2_free_hdr_from_disk(
246 struct xfs_mount *mp,
247 struct xfs_dir3_icfree_hdr *to,
248 struct xfs_dir2_free *from)
249{
250 if (xfs_has_crc(mp)) {
251 struct xfs_dir3_free *from3 = (struct xfs_dir3_free *)from;
252
253 to->magic = be32_to_cpu(from3->hdr.hdr.magic);
254 to->firstdb = be32_to_cpu(from3->hdr.firstdb);
255 to->nvalid = be32_to_cpu(from3->hdr.nvalid);
256 to->nused = be32_to_cpu(from3->hdr.nused);
257 to->bests = from3->bests;
258
259 ASSERT(to->magic == XFS_DIR3_FREE_MAGIC);
260 } else {
261 to->magic = be32_to_cpu(from->hdr.magic);
262 to->firstdb = be32_to_cpu(from->hdr.firstdb);
263 to->nvalid = be32_to_cpu(from->hdr.nvalid);
264 to->nused = be32_to_cpu(from->hdr.nused);
265 to->bests = from->bests;
266
267 ASSERT(to->magic == XFS_DIR2_FREE_MAGIC);
268 }
269}
270
271static void
272xfs_dir2_free_hdr_to_disk(
273 struct xfs_mount *mp,
274 struct xfs_dir2_free *to,
275 struct xfs_dir3_icfree_hdr *from)
276{
277 if (xfs_has_crc(mp)) {
278 struct xfs_dir3_free *to3 = (struct xfs_dir3_free *)to;
279
280 ASSERT(from->magic == XFS_DIR3_FREE_MAGIC);
281
282 to3->hdr.hdr.magic = cpu_to_be32(from->magic);
283 to3->hdr.firstdb = cpu_to_be32(from->firstdb);
284 to3->hdr.nvalid = cpu_to_be32(from->nvalid);
285 to3->hdr.nused = cpu_to_be32(from->nused);
286 } else {
287 ASSERT(from->magic == XFS_DIR2_FREE_MAGIC);
288
289 to->hdr.magic = cpu_to_be32(from->magic);
290 to->hdr.firstdb = cpu_to_be32(from->firstdb);
291 to->hdr.nvalid = cpu_to_be32(from->nvalid);
292 to->hdr.nused = cpu_to_be32(from->nused);
293 }
294}
295
296int
297xfs_dir2_free_read(
298 struct xfs_trans *tp,
299 struct xfs_inode *dp,
300 xfs_ino_t owner,
301 xfs_dablk_t fbno,
302 struct xfs_buf **bpp)
303{
304 return __xfs_dir3_free_read(tp, dp, owner, fbno, 0, bpp);
305}
306
307static int
308xfs_dir2_free_try_read(
309 struct xfs_trans *tp,
310 struct xfs_inode *dp,
311 xfs_ino_t owner,
312 xfs_dablk_t fbno,
313 struct xfs_buf **bpp)
314{
315 return __xfs_dir3_free_read(tp, dp, owner, fbno, XFS_DABUF_MAP_HOLE_OK,
316 bpp);
317}
318
319static int
320xfs_dir3_free_get_buf(
321 xfs_da_args_t *args,
322 xfs_dir2_db_t fbno,
323 struct xfs_buf **bpp)
324{
325 struct xfs_trans *tp = args->trans;
326 struct xfs_inode *dp = args->dp;
327 struct xfs_mount *mp = dp->i_mount;
328 struct xfs_buf *bp;
329 int error;
330 struct xfs_dir3_icfree_hdr hdr;
331
332 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
333 &bp, XFS_DATA_FORK);
334 if (error)
335 return error;
336
337 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
338 bp->b_ops = &xfs_dir3_free_buf_ops;
339
340 /*
341 * Initialize the new block to be empty, and remember
342 * its first slot as our empty slot.
343 */
344 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
345 memset(&hdr, 0, sizeof(hdr));
346
347 if (xfs_has_crc(mp)) {
348 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
349
350 hdr.magic = XFS_DIR3_FREE_MAGIC;
351
352 hdr3->hdr.blkno = cpu_to_be64(xfs_buf_daddr(bp));
353 hdr3->hdr.owner = cpu_to_be64(args->owner);
354 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
355 } else
356 hdr.magic = XFS_DIR2_FREE_MAGIC;
357 xfs_dir2_free_hdr_to_disk(mp, bp->b_addr, &hdr);
358 *bpp = bp;
359 return 0;
360}
361
362/*
363 * Log entries from a freespace block.
364 */
365STATIC void
366xfs_dir2_free_log_bests(
367 struct xfs_da_args *args,
368 struct xfs_dir3_icfree_hdr *hdr,
369 struct xfs_buf *bp,
370 int first, /* first entry to log */
371 int last) /* last entry to log */
372{
373 struct xfs_dir2_free *free = bp->b_addr;
374
375 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
376 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
377 xfs_trans_log_buf(args->trans, bp,
378 (char *)&hdr->bests[first] - (char *)free,
379 (char *)&hdr->bests[last] - (char *)free +
380 sizeof(hdr->bests[0]) - 1);
381}
382
383/*
384 * Log header from a freespace block.
385 */
386static void
387xfs_dir2_free_log_header(
388 struct xfs_da_args *args,
389 struct xfs_buf *bp)
390{
391#ifdef DEBUG
392 xfs_dir2_free_t *free; /* freespace structure */
393
394 free = bp->b_addr;
395 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
396 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
397#endif
398 xfs_trans_log_buf(args->trans, bp, 0,
399 args->geo->free_hdr_size - 1);
400}
401
402/*
403 * Convert a leaf-format directory to a node-format directory.
404 * We need to change the magic number of the leaf block, and copy
405 * the freespace table out of the leaf block into its own block.
406 */
407int /* error */
408xfs_dir2_leaf_to_node(
409 xfs_da_args_t *args, /* operation arguments */
410 struct xfs_buf *lbp) /* leaf buffer */
411{
412 xfs_inode_t *dp; /* incore directory inode */
413 int error; /* error return value */
414 struct xfs_buf *fbp; /* freespace buffer */
415 xfs_dir2_db_t fdb; /* freespace block number */
416 __be16 *from; /* pointer to freespace entry */
417 int i; /* leaf freespace index */
418 xfs_dir2_leaf_t *leaf; /* leaf structure */
419 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
420 int n; /* count of live freespc ents */
421 xfs_dir2_data_off_t off; /* freespace entry value */
422 xfs_trans_t *tp; /* transaction pointer */
423 struct xfs_dir3_icfree_hdr freehdr;
424
425 trace_xfs_dir2_leaf_to_node(args);
426
427 dp = args->dp;
428 tp = args->trans;
429 /*
430 * Add a freespace block to the directory.
431 */
432 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
433 return error;
434 }
435 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
436 /*
437 * Get the buffer for the new freespace block.
438 */
439 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
440 if (error)
441 return error;
442
443 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, fbp->b_addr);
444 leaf = lbp->b_addr;
445 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
446 if (be32_to_cpu(ltp->bestcount) >
447 (uint)dp->i_disk_size / args->geo->blksize) {
448 xfs_buf_mark_corrupt(lbp);
449 xfs_da_mark_sick(args);
450 return -EFSCORRUPTED;
451 }
452
453 /*
454 * Copy freespace entries from the leaf block to the new block.
455 * Count active entries.
456 */
457 from = xfs_dir2_leaf_bests_p(ltp);
458 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++) {
459 off = be16_to_cpu(*from);
460 if (off != NULLDATAOFF)
461 n++;
462 freehdr.bests[i] = cpu_to_be16(off);
463 }
464
465 /*
466 * Now initialize the freespace block header.
467 */
468 freehdr.nused = n;
469 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
470
471 xfs_dir2_free_hdr_to_disk(dp->i_mount, fbp->b_addr, &freehdr);
472 xfs_dir2_free_log_bests(args, &freehdr, fbp, 0, freehdr.nvalid - 1);
473 xfs_dir2_free_log_header(args, fbp);
474
475 /*
476 * Converting the leaf to a leafnode is just a matter of changing the
477 * magic number and the ops. Do the change directly to the buffer as
478 * it's less work (and less code) than decoding the header to host
479 * format and back again.
480 */
481 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
482 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
483 else
484 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
485 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
486 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
487 xfs_dir3_leaf_log_header(args, lbp);
488 xfs_dir3_leaf_check(dp, lbp);
489 return 0;
490}
491
492/*
493 * Add a leaf entry to a leaf block in a node-form directory.
494 * The other work necessary is done from the caller.
495 */
496static int /* error */
497xfs_dir2_leafn_add(
498 struct xfs_buf *bp, /* leaf buffer */
499 struct xfs_da_args *args, /* operation arguments */
500 int index) /* insertion pt for new entry */
501{
502 struct xfs_dir3_icleaf_hdr leafhdr;
503 struct xfs_inode *dp = args->dp;
504 struct xfs_dir2_leaf *leaf = bp->b_addr;
505 struct xfs_dir2_leaf_entry *lep;
506 struct xfs_dir2_leaf_entry *ents;
507 int compact; /* compacting stale leaves */
508 int highstale = 0; /* next stale entry */
509 int lfloghigh; /* high leaf entry logging */
510 int lfloglow; /* low leaf entry logging */
511 int lowstale = 0; /* previous stale entry */
512
513 trace_xfs_dir2_leafn_add(args, index);
514
515 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
516 ents = leafhdr.ents;
517
518 /*
519 * Quick check just to make sure we are not going to index
520 * into other peoples memory
521 */
522 if (index < 0) {
523 xfs_buf_mark_corrupt(bp);
524 xfs_da_mark_sick(args);
525 return -EFSCORRUPTED;
526 }
527
528 /*
529 * If there are already the maximum number of leaf entries in
530 * the block, if there are no stale entries it won't fit.
531 * Caller will do a split. If there are stale entries we'll do
532 * a compact.
533 */
534
535 if (leafhdr.count == args->geo->leaf_max_ents) {
536 if (!leafhdr.stale)
537 return -ENOSPC;
538 compact = leafhdr.stale > 1;
539 } else
540 compact = 0;
541 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
542 ASSERT(index == leafhdr.count ||
543 be32_to_cpu(ents[index].hashval) >= args->hashval);
544
545 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
546 return 0;
547
548 /*
549 * Compact out all but one stale leaf entry. Leaves behind
550 * the entry closest to index.
551 */
552 if (compact)
553 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
554 &highstale, &lfloglow, &lfloghigh);
555 else if (leafhdr.stale) {
556 /*
557 * Set impossible logging indices for this case.
558 */
559 lfloglow = leafhdr.count;
560 lfloghigh = -1;
561 }
562
563 /*
564 * Insert the new entry, log everything.
565 */
566 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
567 highstale, &lfloglow, &lfloghigh);
568
569 lep->hashval = cpu_to_be32(args->hashval);
570 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
571 args->blkno, args->index));
572
573 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
574 xfs_dir3_leaf_log_header(args, bp);
575 xfs_dir3_leaf_log_ents(args, &leafhdr, bp, lfloglow, lfloghigh);
576 xfs_dir3_leaf_check(dp, bp);
577 return 0;
578}
579
580#ifdef DEBUG
581static void
582xfs_dir2_free_hdr_check(
583 struct xfs_inode *dp,
584 struct xfs_buf *bp,
585 xfs_dir2_db_t db)
586{
587 struct xfs_dir3_icfree_hdr hdr;
588
589 xfs_dir2_free_hdr_from_disk(dp->i_mount, &hdr, bp->b_addr);
590
591 ASSERT((hdr.firstdb % dp->i_mount->m_dir_geo->free_max_bests) == 0);
592 ASSERT(hdr.firstdb <= db);
593 ASSERT(db < hdr.firstdb + hdr.nvalid);
594}
595#else
596#define xfs_dir2_free_hdr_check(dp, bp, db)
597#endif /* DEBUG */
598
599/*
600 * Return the last hash value in the leaf.
601 * Stale entries are ok.
602 */
603xfs_dahash_t /* hash value */
604xfs_dir2_leaf_lasthash(
605 struct xfs_inode *dp,
606 struct xfs_buf *bp, /* leaf buffer */
607 int *count) /* count of entries in leaf */
608{
609 struct xfs_dir3_icleaf_hdr leafhdr;
610
611 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, bp->b_addr);
612
613 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
614 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
615 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
616 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
617
618 if (count)
619 *count = leafhdr.count;
620 if (!leafhdr.count)
621 return 0;
622 return be32_to_cpu(leafhdr.ents[leafhdr.count - 1].hashval);
623}
624
625/*
626 * Look up a leaf entry for space to add a name in a node-format leaf block.
627 * The extrablk in state is a freespace block.
628 */
629STATIC int
630xfs_dir2_leafn_lookup_for_addname(
631 struct xfs_buf *bp, /* leaf buffer */
632 xfs_da_args_t *args, /* operation arguments */
633 int *indexp, /* out: leaf entry index */
634 xfs_da_state_t *state) /* state to fill in */
635{
636 struct xfs_buf *curbp = NULL; /* current data/free buffer */
637 xfs_dir2_db_t curdb = -1; /* current data block number */
638 xfs_dir2_db_t curfdb = -1; /* current free block number */
639 xfs_inode_t *dp; /* incore directory inode */
640 int error; /* error return value */
641 int fi; /* free entry index */
642 xfs_dir2_free_t *free = NULL; /* free block structure */
643 int index; /* leaf entry index */
644 xfs_dir2_leaf_t *leaf; /* leaf structure */
645 int length; /* length of new data entry */
646 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
647 xfs_mount_t *mp; /* filesystem mount point */
648 xfs_dir2_db_t newdb; /* new data block number */
649 xfs_dir2_db_t newfdb; /* new free block number */
650 xfs_trans_t *tp; /* transaction pointer */
651 struct xfs_dir3_icleaf_hdr leafhdr;
652
653 dp = args->dp;
654 tp = args->trans;
655 mp = dp->i_mount;
656 leaf = bp->b_addr;
657 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
658
659 xfs_dir3_leaf_check(dp, bp);
660 ASSERT(leafhdr.count > 0);
661
662 /*
663 * Look up the hash value in the leaf entries.
664 */
665 index = xfs_dir2_leaf_search_hash(args, bp);
666 /*
667 * Do we have a buffer coming in?
668 */
669 if (state->extravalid) {
670 /* If so, it's a free block buffer, get the block number. */
671 curbp = state->extrablk.bp;
672 curfdb = state->extrablk.blkno;
673 free = curbp->b_addr;
674 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
675 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
676 }
677 length = xfs_dir2_data_entsize(mp, args->namelen);
678 /*
679 * Loop over leaf entries with the right hash value.
680 */
681 for (lep = &leafhdr.ents[index];
682 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
683 lep++, index++) {
684 /*
685 * Skip stale leaf entries.
686 */
687 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
688 continue;
689 /*
690 * Pull the data block number from the entry.
691 */
692 newdb = xfs_dir2_dataptr_to_db(args->geo,
693 be32_to_cpu(lep->address));
694 /*
695 * For addname, we're looking for a place to put the new entry.
696 * We want to use a data block with an entry of equal
697 * hash value to ours if there is one with room.
698 *
699 * If this block isn't the data block we already have
700 * in hand, take a look at it.
701 */
702 if (newdb != curdb) {
703 struct xfs_dir3_icfree_hdr freehdr;
704
705 curdb = newdb;
706 /*
707 * Convert the data block to the free block
708 * holding its freespace information.
709 */
710 newfdb = xfs_dir2_db_to_fdb(args->geo, newdb);
711 /*
712 * If it's not the one we have in hand, read it in.
713 */
714 if (newfdb != curfdb) {
715 /*
716 * If we had one before, drop it.
717 */
718 if (curbp)
719 xfs_trans_brelse(tp, curbp);
720
721 error = xfs_dir2_free_read(tp, dp, args->owner,
722 xfs_dir2_db_to_da(args->geo,
723 newfdb),
724 &curbp);
725 if (error)
726 return error;
727 free = curbp->b_addr;
728
729 xfs_dir2_free_hdr_check(dp, curbp, curdb);
730 }
731 /*
732 * Get the index for our entry.
733 */
734 fi = xfs_dir2_db_to_fdindex(args->geo, curdb);
735 /*
736 * If it has room, return it.
737 */
738 xfs_dir2_free_hdr_from_disk(mp, &freehdr, free);
739 if (XFS_IS_CORRUPT(mp,
740 freehdr.bests[fi] ==
741 cpu_to_be16(NULLDATAOFF))) {
742 if (curfdb != newfdb)
743 xfs_trans_brelse(tp, curbp);
744 xfs_da_mark_sick(args);
745 return -EFSCORRUPTED;
746 }
747 curfdb = newfdb;
748 if (be16_to_cpu(freehdr.bests[fi]) >= length)
749 goto out;
750 }
751 }
752 /* Didn't find any space */
753 fi = -1;
754out:
755 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
756 if (curbp) {
757 /* Giving back a free block. */
758 state->extravalid = 1;
759 state->extrablk.bp = curbp;
760 state->extrablk.index = fi;
761 state->extrablk.blkno = curfdb;
762
763 /*
764 * Important: this magic number is not in the buffer - it's for
765 * buffer type information and therefore only the free/data type
766 * matters here, not whether CRCs are enabled or not.
767 */
768 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
769 } else {
770 state->extravalid = 0;
771 }
772 /*
773 * Return the index, that will be the insertion point.
774 */
775 *indexp = index;
776 return -ENOENT;
777}
778
779/*
780 * Look up a leaf entry in a node-format leaf block.
781 * The extrablk in state a data block.
782 */
783STATIC int
784xfs_dir2_leafn_lookup_for_entry(
785 struct xfs_buf *bp, /* leaf buffer */
786 xfs_da_args_t *args, /* operation arguments */
787 int *indexp, /* out: leaf entry index */
788 xfs_da_state_t *state) /* state to fill in */
789{
790 struct xfs_buf *curbp = NULL; /* current data/free buffer */
791 xfs_dir2_db_t curdb = -1; /* current data block number */
792 xfs_dir2_data_entry_t *dep; /* data block entry */
793 xfs_inode_t *dp; /* incore directory inode */
794 int error; /* error return value */
795 int index; /* leaf entry index */
796 xfs_dir2_leaf_t *leaf; /* leaf structure */
797 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
798 xfs_mount_t *mp; /* filesystem mount point */
799 xfs_dir2_db_t newdb; /* new data block number */
800 xfs_trans_t *tp; /* transaction pointer */
801 enum xfs_dacmp cmp; /* comparison result */
802 struct xfs_dir3_icleaf_hdr leafhdr;
803
804 dp = args->dp;
805 tp = args->trans;
806 mp = dp->i_mount;
807 leaf = bp->b_addr;
808 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
809
810 xfs_dir3_leaf_check(dp, bp);
811 if (leafhdr.count <= 0) {
812 xfs_buf_mark_corrupt(bp);
813 xfs_da_mark_sick(args);
814 return -EFSCORRUPTED;
815 }
816
817 /*
818 * Look up the hash value in the leaf entries.
819 */
820 index = xfs_dir2_leaf_search_hash(args, bp);
821 /*
822 * Do we have a buffer coming in?
823 */
824 if (state->extravalid) {
825 curbp = state->extrablk.bp;
826 curdb = state->extrablk.blkno;
827 }
828 /*
829 * Loop over leaf entries with the right hash value.
830 */
831 for (lep = &leafhdr.ents[index];
832 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
833 lep++, index++) {
834 /*
835 * Skip stale leaf entries.
836 */
837 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
838 continue;
839 /*
840 * Pull the data block number from the entry.
841 */
842 newdb = xfs_dir2_dataptr_to_db(args->geo,
843 be32_to_cpu(lep->address));
844 /*
845 * Not adding a new entry, so we really want to find
846 * the name given to us.
847 *
848 * If it's a different data block, go get it.
849 */
850 if (newdb != curdb) {
851 /*
852 * If we had a block before that we aren't saving
853 * for a CI name, drop it
854 */
855 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
856 curdb != state->extrablk.blkno))
857 xfs_trans_brelse(tp, curbp);
858 /*
859 * If needing the block that is saved with a CI match,
860 * use it otherwise read in the new data block.
861 */
862 if (args->cmpresult != XFS_CMP_DIFFERENT &&
863 newdb == state->extrablk.blkno) {
864 ASSERT(state->extravalid);
865 curbp = state->extrablk.bp;
866 } else {
867 error = xfs_dir3_data_read(tp, dp, args->owner,
868 xfs_dir2_db_to_da(args->geo,
869 newdb),
870 0, &curbp);
871 if (error)
872 return error;
873 }
874 xfs_dir3_data_check(dp, curbp);
875 curdb = newdb;
876 }
877 /*
878 * Point to the data entry.
879 */
880 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
881 xfs_dir2_dataptr_to_off(args->geo,
882 be32_to_cpu(lep->address)));
883 /*
884 * Compare the entry and if it's an exact match, return
885 * EEXIST immediately. If it's the first case-insensitive
886 * match, store the block & inode number and continue looking.
887 */
888 cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
889 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
890 /* If there is a CI match block, drop it */
891 if (args->cmpresult != XFS_CMP_DIFFERENT &&
892 curdb != state->extrablk.blkno)
893 xfs_trans_brelse(tp, state->extrablk.bp);
894 args->cmpresult = cmp;
895 args->inumber = be64_to_cpu(dep->inumber);
896 args->filetype = xfs_dir2_data_get_ftype(mp, dep);
897 *indexp = index;
898 state->extravalid = 1;
899 state->extrablk.bp = curbp;
900 state->extrablk.blkno = curdb;
901 state->extrablk.index = (int)((char *)dep -
902 (char *)curbp->b_addr);
903 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
904 curbp->b_ops = &xfs_dir3_data_buf_ops;
905 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
906 if (cmp == XFS_CMP_EXACT)
907 return -EEXIST;
908 }
909 }
910 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
911 if (curbp) {
912 if (args->cmpresult == XFS_CMP_DIFFERENT) {
913 /* Giving back last used data block. */
914 state->extravalid = 1;
915 state->extrablk.bp = curbp;
916 state->extrablk.index = -1;
917 state->extrablk.blkno = curdb;
918 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
919 curbp->b_ops = &xfs_dir3_data_buf_ops;
920 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
921 } else {
922 /* If the curbp is not the CI match block, drop it */
923 if (state->extrablk.bp != curbp)
924 xfs_trans_brelse(tp, curbp);
925 }
926 } else {
927 state->extravalid = 0;
928 }
929 *indexp = index;
930 return -ENOENT;
931}
932
933/*
934 * Look up a leaf entry in a node-format leaf block.
935 * If this is an addname then the extrablk in state is a freespace block,
936 * otherwise it's a data block.
937 */
938int
939xfs_dir2_leafn_lookup_int(
940 struct xfs_buf *bp, /* leaf buffer */
941 xfs_da_args_t *args, /* operation arguments */
942 int *indexp, /* out: leaf entry index */
943 xfs_da_state_t *state) /* state to fill in */
944{
945 if (args->op_flags & XFS_DA_OP_ADDNAME)
946 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
947 state);
948 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
949}
950
951/*
952 * Move count leaf entries from source to destination leaf.
953 * Log entries and headers. Stale entries are preserved.
954 */
955static void
956xfs_dir3_leafn_moveents(
957 xfs_da_args_t *args, /* operation arguments */
958 struct xfs_buf *bp_s, /* source */
959 struct xfs_dir3_icleaf_hdr *shdr,
960 struct xfs_dir2_leaf_entry *sents,
961 int start_s,/* source leaf index */
962 struct xfs_buf *bp_d, /* destination */
963 struct xfs_dir3_icleaf_hdr *dhdr,
964 struct xfs_dir2_leaf_entry *dents,
965 int start_d,/* destination leaf index */
966 int count) /* count of leaves to copy */
967{
968 int stale; /* count stale leaves copied */
969
970 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
971
972 /*
973 * Silently return if nothing to do.
974 */
975 if (count == 0)
976 return;
977
978 /*
979 * If the destination index is not the end of the current
980 * destination leaf entries, open up a hole in the destination
981 * to hold the new entries.
982 */
983 if (start_d < dhdr->count) {
984 memmove(&dents[start_d + count], &dents[start_d],
985 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
986 xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d + count,
987 count + dhdr->count - 1);
988 }
989 /*
990 * If the source has stale leaves, count the ones in the copy range
991 * so we can update the header correctly.
992 */
993 if (shdr->stale) {
994 int i; /* temp leaf index */
995
996 for (i = start_s, stale = 0; i < start_s + count; i++) {
997 if (sents[i].address ==
998 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
999 stale++;
1000 }
1001 } else
1002 stale = 0;
1003 /*
1004 * Copy the leaf entries from source to destination.
1005 */
1006 memcpy(&dents[start_d], &sents[start_s],
1007 count * sizeof(xfs_dir2_leaf_entry_t));
1008 xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d, start_d + count - 1);
1009
1010 /*
1011 * If there are source entries after the ones we copied,
1012 * delete the ones we copied by sliding the next ones down.
1013 */
1014 if (start_s + count < shdr->count) {
1015 memmove(&sents[start_s], &sents[start_s + count],
1016 count * sizeof(xfs_dir2_leaf_entry_t));
1017 xfs_dir3_leaf_log_ents(args, shdr, bp_s, start_s,
1018 start_s + count - 1);
1019 }
1020
1021 /*
1022 * Update the headers and log them.
1023 */
1024 shdr->count -= count;
1025 shdr->stale -= stale;
1026 dhdr->count += count;
1027 dhdr->stale += stale;
1028}
1029
1030/*
1031 * Determine the sort order of two leaf blocks.
1032 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
1033 */
1034int /* sort order */
1035xfs_dir2_leafn_order(
1036 struct xfs_inode *dp,
1037 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
1038 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
1039{
1040 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
1041 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
1042 struct xfs_dir2_leaf_entry *ents1;
1043 struct xfs_dir2_leaf_entry *ents2;
1044 struct xfs_dir3_icleaf_hdr hdr1;
1045 struct xfs_dir3_icleaf_hdr hdr2;
1046
1047 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1048 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
1049 ents1 = hdr1.ents;
1050 ents2 = hdr2.ents;
1051
1052 if (hdr1.count > 0 && hdr2.count > 0 &&
1053 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
1054 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
1055 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
1056 return 1;
1057 return 0;
1058}
1059
1060/*
1061 * Rebalance leaf entries between two leaf blocks.
1062 * This is actually only called when the second block is new,
1063 * though the code deals with the general case.
1064 * A new entry will be inserted in one of the blocks, and that
1065 * entry is taken into account when balancing.
1066 */
1067static void
1068xfs_dir2_leafn_rebalance(
1069 xfs_da_state_t *state, /* btree cursor */
1070 xfs_da_state_blk_t *blk1, /* first btree block */
1071 xfs_da_state_blk_t *blk2) /* second btree block */
1072{
1073 xfs_da_args_t *args; /* operation arguments */
1074 int count; /* count (& direction) leaves */
1075 int isleft; /* new goes in left leaf */
1076 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1077 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1078 int mid; /* midpoint leaf index */
1079#if defined(DEBUG) || defined(XFS_WARN)
1080 int oldstale; /* old count of stale leaves */
1081#endif
1082 int oldsum; /* old total leaf count */
1083 int swap_blocks; /* swapped leaf blocks */
1084 struct xfs_dir2_leaf_entry *ents1;
1085 struct xfs_dir2_leaf_entry *ents2;
1086 struct xfs_dir3_icleaf_hdr hdr1;
1087 struct xfs_dir3_icleaf_hdr hdr2;
1088 struct xfs_inode *dp = state->args->dp;
1089
1090 args = state->args;
1091 /*
1092 * If the block order is wrong, swap the arguments.
1093 */
1094 swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1095 if (swap_blocks)
1096 swap(blk1, blk2);
1097
1098 leaf1 = blk1->bp->b_addr;
1099 leaf2 = blk2->bp->b_addr;
1100 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1101 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
1102 ents1 = hdr1.ents;
1103 ents2 = hdr2.ents;
1104
1105 oldsum = hdr1.count + hdr2.count;
1106#if defined(DEBUG) || defined(XFS_WARN)
1107 oldstale = hdr1.stale + hdr2.stale;
1108#endif
1109 mid = oldsum >> 1;
1110
1111 /*
1112 * If the old leaf count was odd then the new one will be even,
1113 * so we need to divide the new count evenly.
1114 */
1115 if (oldsum & 1) {
1116 xfs_dahash_t midhash; /* middle entry hash value */
1117
1118 if (mid >= hdr1.count)
1119 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
1120 else
1121 midhash = be32_to_cpu(ents1[mid].hashval);
1122 isleft = args->hashval <= midhash;
1123 }
1124 /*
1125 * If the old count is even then the new count is odd, so there's
1126 * no preferred side for the new entry.
1127 * Pick the left one.
1128 */
1129 else
1130 isleft = 1;
1131 /*
1132 * Calculate moved entry count. Positive means left-to-right,
1133 * negative means right-to-left. Then move the entries.
1134 */
1135 count = hdr1.count - mid + (isleft == 0);
1136 if (count > 0)
1137 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1138 hdr1.count - count, blk2->bp,
1139 &hdr2, ents2, 0, count);
1140 else if (count < 0)
1141 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1142 blk1->bp, &hdr1, ents1,
1143 hdr1.count, count);
1144
1145 ASSERT(hdr1.count + hdr2.count == oldsum);
1146 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1147
1148 /* log the changes made when moving the entries */
1149 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf1, &hdr1);
1150 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf2, &hdr2);
1151 xfs_dir3_leaf_log_header(args, blk1->bp);
1152 xfs_dir3_leaf_log_header(args, blk2->bp);
1153
1154 xfs_dir3_leaf_check(dp, blk1->bp);
1155 xfs_dir3_leaf_check(dp, blk2->bp);
1156
1157 /*
1158 * Mark whether we're inserting into the old or new leaf.
1159 */
1160 if (hdr1.count < hdr2.count)
1161 state->inleaf = swap_blocks;
1162 else if (hdr1.count > hdr2.count)
1163 state->inleaf = !swap_blocks;
1164 else
1165 state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
1166 /*
1167 * Adjust the expected index for insertion.
1168 */
1169 if (!state->inleaf)
1170 blk2->index = blk1->index - hdr1.count;
1171
1172 /*
1173 * Finally sanity check just to make sure we are not returning a
1174 * negative index
1175 */
1176 if (blk2->index < 0) {
1177 state->inleaf = 1;
1178 blk2->index = 0;
1179 xfs_alert(dp->i_mount,
1180 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
1181 __func__, blk1->index);
1182 }
1183}
1184
1185static int
1186xfs_dir3_data_block_free(
1187 xfs_da_args_t *args,
1188 struct xfs_dir2_data_hdr *hdr,
1189 struct xfs_dir2_free *free,
1190 xfs_dir2_db_t fdb,
1191 int findex,
1192 struct xfs_buf *fbp,
1193 int longest)
1194{
1195 int logfree = 0;
1196 struct xfs_dir3_icfree_hdr freehdr;
1197 struct xfs_inode *dp = args->dp;
1198
1199 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
1200 if (hdr) {
1201 /*
1202 * Data block is not empty, just set the free entry to the new
1203 * value.
1204 */
1205 freehdr.bests[findex] = cpu_to_be16(longest);
1206 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
1207 return 0;
1208 }
1209
1210 /* One less used entry in the free table. */
1211 freehdr.nused--;
1212
1213 /*
1214 * If this was the last entry in the table, we can trim the table size
1215 * back. There might be other entries at the end referring to
1216 * non-existent data blocks, get those too.
1217 */
1218 if (findex == freehdr.nvalid - 1) {
1219 int i; /* free entry index */
1220
1221 for (i = findex - 1; i >= 0; i--) {
1222 if (freehdr.bests[i] != cpu_to_be16(NULLDATAOFF))
1223 break;
1224 }
1225 freehdr.nvalid = i + 1;
1226 logfree = 0;
1227 } else {
1228 /* Not the last entry, just punch it out. */
1229 freehdr.bests[findex] = cpu_to_be16(NULLDATAOFF);
1230 logfree = 1;
1231 }
1232
1233 xfs_dir2_free_hdr_to_disk(dp->i_mount, free, &freehdr);
1234 xfs_dir2_free_log_header(args, fbp);
1235
1236 /*
1237 * If there are no useful entries left in the block, get rid of the
1238 * block if we can.
1239 */
1240 if (!freehdr.nused) {
1241 int error;
1242
1243 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1244 if (error == 0) {
1245 fbp = NULL;
1246 logfree = 0;
1247 } else if (error != -ENOSPC || args->total != 0)
1248 return error;
1249 /*
1250 * It's possible to get ENOSPC if there is no
1251 * space reservation. In this case some one
1252 * else will eventually get rid of this block.
1253 */
1254 }
1255
1256 /* Log the free entry that changed, unless we got rid of it. */
1257 if (logfree)
1258 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
1259 return 0;
1260}
1261
1262/*
1263 * Remove an entry from a node directory.
1264 * This removes the leaf entry and the data entry,
1265 * and updates the free block if necessary.
1266 */
1267static int /* error */
1268xfs_dir2_leafn_remove(
1269 xfs_da_args_t *args, /* operation arguments */
1270 struct xfs_buf *bp, /* leaf buffer */
1271 int index, /* leaf entry index */
1272 xfs_da_state_blk_t *dblk, /* data block */
1273 int *rval) /* resulting block needs join */
1274{
1275 struct xfs_da_geometry *geo = args->geo;
1276 xfs_dir2_data_hdr_t *hdr; /* data block header */
1277 xfs_dir2_db_t db; /* data block number */
1278 struct xfs_buf *dbp; /* data block buffer */
1279 xfs_dir2_data_entry_t *dep; /* data block entry */
1280 xfs_inode_t *dp; /* incore directory inode */
1281 xfs_dir2_leaf_t *leaf; /* leaf structure */
1282 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1283 int longest; /* longest data free entry */
1284 int off; /* data block entry offset */
1285 int needlog; /* need to log data header */
1286 int needscan; /* need to rescan data frees */
1287 xfs_trans_t *tp; /* transaction pointer */
1288 struct xfs_dir2_data_free *bf; /* bestfree table */
1289 struct xfs_dir3_icleaf_hdr leafhdr;
1290
1291 trace_xfs_dir2_leafn_remove(args, index);
1292
1293 dp = args->dp;
1294 tp = args->trans;
1295 leaf = bp->b_addr;
1296 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
1297
1298 /*
1299 * Point to the entry we're removing.
1300 */
1301 lep = &leafhdr.ents[index];
1302
1303 /*
1304 * Extract the data block and offset from the entry.
1305 */
1306 db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
1307 ASSERT(dblk->blkno == db);
1308 off = xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address));
1309 ASSERT(dblk->index == off);
1310
1311 /*
1312 * Kill the leaf entry by marking it stale.
1313 * Log the leaf block changes.
1314 */
1315 leafhdr.stale++;
1316 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
1317 xfs_dir3_leaf_log_header(args, bp);
1318
1319 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1320 xfs_dir3_leaf_log_ents(args, &leafhdr, bp, index, index);
1321
1322 /*
1323 * Make the data entry free. Keep track of the longest freespace
1324 * in the data block in case it changes.
1325 */
1326 dbp = dblk->bp;
1327 hdr = dbp->b_addr;
1328 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
1329 bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
1330 longest = be16_to_cpu(bf[0].length);
1331 needlog = needscan = 0;
1332 xfs_dir2_data_make_free(args, dbp, off,
1333 xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
1334 &needscan);
1335 /*
1336 * Rescan the data block freespaces for bestfree.
1337 * Log the data block header if needed.
1338 */
1339 if (needscan)
1340 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1341 if (needlog)
1342 xfs_dir2_data_log_header(args, dbp);
1343 xfs_dir3_data_check(dp, dbp);
1344 /*
1345 * If the longest data block freespace changes, need to update
1346 * the corresponding freeblock entry.
1347 */
1348 if (longest < be16_to_cpu(bf[0].length)) {
1349 int error; /* error return value */
1350 struct xfs_buf *fbp; /* freeblock buffer */
1351 xfs_dir2_db_t fdb; /* freeblock block number */
1352 int findex; /* index in freeblock entries */
1353 xfs_dir2_free_t *free; /* freeblock structure */
1354
1355 /*
1356 * Convert the data block number to a free block,
1357 * read in the free block.
1358 */
1359 fdb = xfs_dir2_db_to_fdb(geo, db);
1360 error = xfs_dir2_free_read(tp, dp, args->owner,
1361 xfs_dir2_db_to_da(geo, fdb), &fbp);
1362 if (error)
1363 return error;
1364 free = fbp->b_addr;
1365#ifdef DEBUG
1366 {
1367 struct xfs_dir3_icfree_hdr freehdr;
1368
1369 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
1370 ASSERT(freehdr.firstdb == geo->free_max_bests *
1371 (fdb - xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET)));
1372 }
1373#endif
1374 /*
1375 * Calculate which entry we need to fix.
1376 */
1377 findex = xfs_dir2_db_to_fdindex(geo, db);
1378 longest = be16_to_cpu(bf[0].length);
1379 /*
1380 * If the data block is now empty we can get rid of it
1381 * (usually).
1382 */
1383 if (longest == geo->blksize - geo->data_entry_offset) {
1384 /*
1385 * Try to punch out the data block.
1386 */
1387 error = xfs_dir2_shrink_inode(args, db, dbp);
1388 if (error == 0) {
1389 dblk->bp = NULL;
1390 hdr = NULL;
1391 }
1392 /*
1393 * We can get ENOSPC if there's no space reservation.
1394 * In this case just drop the buffer and some one else
1395 * will eventually get rid of the empty block.
1396 */
1397 else if (!(error == -ENOSPC && args->total == 0))
1398 return error;
1399 }
1400 /*
1401 * If we got rid of the data block, we can eliminate that entry
1402 * in the free block.
1403 */
1404 error = xfs_dir3_data_block_free(args, hdr, free,
1405 fdb, findex, fbp, longest);
1406 if (error)
1407 return error;
1408 }
1409
1410 xfs_dir3_leaf_check(dp, bp);
1411 /*
1412 * Return indication of whether this leaf block is empty enough
1413 * to justify trying to join it with a neighbor.
1414 */
1415 *rval = (geo->leaf_hdr_size +
1416 (uint)sizeof(leafhdr.ents) * (leafhdr.count - leafhdr.stale)) <
1417 geo->magicpct;
1418 return 0;
1419}
1420
1421/*
1422 * Split the leaf entries in the old block into old and new blocks.
1423 */
1424int /* error */
1425xfs_dir2_leafn_split(
1426 xfs_da_state_t *state, /* btree cursor */
1427 xfs_da_state_blk_t *oldblk, /* original block */
1428 xfs_da_state_blk_t *newblk) /* newly created block */
1429{
1430 xfs_da_args_t *args; /* operation arguments */
1431 xfs_dablk_t blkno; /* new leaf block number */
1432 int error; /* error return value */
1433 struct xfs_inode *dp;
1434
1435 /*
1436 * Allocate space for a new leaf node.
1437 */
1438 args = state->args;
1439 dp = args->dp;
1440 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1441 error = xfs_da_grow_inode(args, &blkno);
1442 if (error) {
1443 return error;
1444 }
1445 /*
1446 * Initialize the new leaf block.
1447 */
1448 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
1449 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1450 if (error)
1451 return error;
1452
1453 newblk->blkno = blkno;
1454 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1455 /*
1456 * Rebalance the entries across the two leaves, link the new
1457 * block into the leaves.
1458 */
1459 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1460 error = xfs_da3_blk_link(state, oldblk, newblk);
1461 if (error) {
1462 return error;
1463 }
1464 /*
1465 * Insert the new entry in the correct block.
1466 */
1467 if (state->inleaf)
1468 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1469 else
1470 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1471 /*
1472 * Update last hashval in each block since we added the name.
1473 */
1474 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1475 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
1476 xfs_dir3_leaf_check(dp, oldblk->bp);
1477 xfs_dir3_leaf_check(dp, newblk->bp);
1478 return error;
1479}
1480
1481/*
1482 * Check a leaf block and its neighbors to see if the block should be
1483 * collapsed into one or the other neighbor. Always keep the block
1484 * with the smaller block number.
1485 * If the current block is over 50% full, don't try to join it, return 0.
1486 * If the block is empty, fill in the state structure and return 2.
1487 * If it can be collapsed, fill in the state structure and return 1.
1488 * If nothing can be done, return 0.
1489 */
1490int /* error */
1491xfs_dir2_leafn_toosmall(
1492 xfs_da_state_t *state, /* btree cursor */
1493 int *action) /* resulting action to take */
1494{
1495 xfs_da_state_blk_t *blk; /* leaf block */
1496 xfs_dablk_t blkno; /* leaf block number */
1497 struct xfs_buf *bp; /* leaf buffer */
1498 int bytes; /* bytes in use */
1499 int count; /* leaf live entry count */
1500 int error; /* error return value */
1501 int forward; /* sibling block direction */
1502 int i; /* sibling counter */
1503 xfs_dir2_leaf_t *leaf; /* leaf structure */
1504 int rval; /* result from path_shift */
1505 struct xfs_dir3_icleaf_hdr leafhdr;
1506 struct xfs_dir2_leaf_entry *ents;
1507 struct xfs_inode *dp = state->args->dp;
1508
1509 /*
1510 * Check for the degenerate case of the block being over 50% full.
1511 * If so, it's not worth even looking to see if we might be able
1512 * to coalesce with a sibling.
1513 */
1514 blk = &state->path.blk[state->path.active - 1];
1515 leaf = blk->bp->b_addr;
1516 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
1517 ents = leafhdr.ents;
1518 xfs_dir3_leaf_check(dp, blk->bp);
1519
1520 count = leafhdr.count - leafhdr.stale;
1521 bytes = state->args->geo->leaf_hdr_size + count * sizeof(ents[0]);
1522 if (bytes > (state->args->geo->blksize >> 1)) {
1523 /*
1524 * Blk over 50%, don't try to join.
1525 */
1526 *action = 0;
1527 return 0;
1528 }
1529 /*
1530 * Check for the degenerate case of the block being empty.
1531 * If the block is empty, we'll simply delete it, no need to
1532 * coalesce it with a sibling block. We choose (arbitrarily)
1533 * to merge with the forward block unless it is NULL.
1534 */
1535 if (count == 0) {
1536 /*
1537 * Make altpath point to the block we want to keep and
1538 * path point to the block we want to drop (this one).
1539 */
1540 forward = (leafhdr.forw != 0);
1541 memcpy(&state->altpath, &state->path, sizeof(state->path));
1542 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1543 &rval);
1544 if (error)
1545 return error;
1546 *action = rval ? 2 : 0;
1547 return 0;
1548 }
1549 /*
1550 * Examine each sibling block to see if we can coalesce with
1551 * at least 25% free space to spare. We need to figure out
1552 * whether to merge with the forward or the backward block.
1553 * We prefer coalescing with the lower numbered sibling so as
1554 * to shrink a directory over time.
1555 */
1556 forward = leafhdr.forw < leafhdr.back;
1557 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1558 struct xfs_dir3_icleaf_hdr hdr2;
1559
1560 blkno = forward ? leafhdr.forw : leafhdr.back;
1561 if (blkno == 0)
1562 continue;
1563 /*
1564 * Read the sibling leaf block.
1565 */
1566 error = xfs_dir3_leafn_read(state->args->trans, dp,
1567 state->args->owner, blkno, &bp);
1568 if (error)
1569 return error;
1570
1571 /*
1572 * Count bytes in the two blocks combined.
1573 */
1574 count = leafhdr.count - leafhdr.stale;
1575 bytes = state->args->geo->blksize -
1576 (state->args->geo->blksize >> 2);
1577
1578 leaf = bp->b_addr;
1579 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf);
1580 ents = hdr2.ents;
1581 count += hdr2.count - hdr2.stale;
1582 bytes -= count * sizeof(ents[0]);
1583
1584 /*
1585 * Fits with at least 25% to spare.
1586 */
1587 if (bytes >= 0)
1588 break;
1589 xfs_trans_brelse(state->args->trans, bp);
1590 }
1591 /*
1592 * Didn't like either block, give up.
1593 */
1594 if (i >= 2) {
1595 *action = 0;
1596 return 0;
1597 }
1598
1599 /*
1600 * Make altpath point to the block we want to keep (the lower
1601 * numbered block) and path point to the block we want to drop.
1602 */
1603 memcpy(&state->altpath, &state->path, sizeof(state->path));
1604 if (blkno < blk->blkno)
1605 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1606 &rval);
1607 else
1608 error = xfs_da3_path_shift(state, &state->path, forward, 0,
1609 &rval);
1610 if (error) {
1611 return error;
1612 }
1613 *action = rval ? 0 : 1;
1614 return 0;
1615}
1616
1617/*
1618 * Move all the leaf entries from drop_blk to save_blk.
1619 * This is done as part of a join operation.
1620 */
1621void
1622xfs_dir2_leafn_unbalance(
1623 xfs_da_state_t *state, /* cursor */
1624 xfs_da_state_blk_t *drop_blk, /* dead block */
1625 xfs_da_state_blk_t *save_blk) /* surviving block */
1626{
1627 xfs_da_args_t *args; /* operation arguments */
1628 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1629 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1630 struct xfs_dir3_icleaf_hdr savehdr;
1631 struct xfs_dir3_icleaf_hdr drophdr;
1632 struct xfs_dir2_leaf_entry *sents;
1633 struct xfs_dir2_leaf_entry *dents;
1634 struct xfs_inode *dp = state->args->dp;
1635
1636 args = state->args;
1637 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1638 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1639 drop_leaf = drop_blk->bp->b_addr;
1640 save_leaf = save_blk->bp->b_addr;
1641
1642 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf);
1643 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf);
1644 sents = savehdr.ents;
1645 dents = drophdr.ents;
1646
1647 /*
1648 * If there are any stale leaf entries, take this opportunity
1649 * to purge them.
1650 */
1651 if (drophdr.stale)
1652 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1653 if (savehdr.stale)
1654 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1655
1656 /*
1657 * Move the entries from drop to the appropriate end of save.
1658 */
1659 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
1660 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
1661 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1662 save_blk->bp, &savehdr, sents, 0,
1663 drophdr.count);
1664 else
1665 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1666 save_blk->bp, &savehdr, sents,
1667 savehdr.count, drophdr.count);
1668 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1669
1670 /* log the changes made when moving the entries */
1671 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, save_leaf, &savehdr);
1672 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, drop_leaf, &drophdr);
1673 xfs_dir3_leaf_log_header(args, save_blk->bp);
1674 xfs_dir3_leaf_log_header(args, drop_blk->bp);
1675
1676 xfs_dir3_leaf_check(dp, save_blk->bp);
1677 xfs_dir3_leaf_check(dp, drop_blk->bp);
1678}
1679
1680/*
1681 * Add a new data block to the directory at the free space index that the caller
1682 * has specified.
1683 */
1684static int
1685xfs_dir2_node_add_datablk(
1686 struct xfs_da_args *args,
1687 struct xfs_da_state_blk *fblk,
1688 xfs_dir2_db_t *dbno,
1689 struct xfs_buf **dbpp,
1690 struct xfs_buf **fbpp,
1691 struct xfs_dir3_icfree_hdr *hdr,
1692 int *findex)
1693{
1694 struct xfs_inode *dp = args->dp;
1695 struct xfs_trans *tp = args->trans;
1696 struct xfs_mount *mp = dp->i_mount;
1697 struct xfs_dir2_data_free *bf;
1698 xfs_dir2_db_t fbno;
1699 struct xfs_buf *fbp;
1700 struct xfs_buf *dbp;
1701 int error;
1702
1703 /* Not allowed to allocate, return failure. */
1704 if (args->total == 0)
1705 return -ENOSPC;
1706
1707 /* Allocate and initialize the new data block. */
1708 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno);
1709 if (error)
1710 return error;
1711 error = xfs_dir3_data_init(args, *dbno, &dbp);
1712 if (error)
1713 return error;
1714
1715 /*
1716 * Get the freespace block corresponding to the data block
1717 * that was just allocated.
1718 */
1719 fbno = xfs_dir2_db_to_fdb(args->geo, *dbno);
1720 error = xfs_dir2_free_try_read(tp, dp, args->owner,
1721 xfs_dir2_db_to_da(args->geo, fbno), &fbp);
1722 if (error)
1723 return error;
1724
1725 /*
1726 * If there wasn't a freespace block, the read will
1727 * return a NULL fbp. Allocate and initialize a new one.
1728 */
1729 if (!fbp) {
1730 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno);
1731 if (error)
1732 return error;
1733
1734 if (XFS_IS_CORRUPT(mp,
1735 xfs_dir2_db_to_fdb(args->geo, *dbno) !=
1736 fbno)) {
1737 xfs_alert(mp,
1738"%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
1739 __func__, (unsigned long long)dp->i_ino,
1740 (long long)xfs_dir2_db_to_fdb(args->geo, *dbno),
1741 (long long)*dbno, (long long)fbno);
1742 if (fblk) {
1743 xfs_alert(mp,
1744 " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
1745 fblk, (unsigned long long)fblk->blkno,
1746 fblk->index, fblk->magic);
1747 } else {
1748 xfs_alert(mp, " ... fblk is NULL");
1749 }
1750 xfs_da_mark_sick(args);
1751 return -EFSCORRUPTED;
1752 }
1753
1754 /* Get a buffer for the new block. */
1755 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1756 if (error)
1757 return error;
1758 xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
1759
1760 /* Remember the first slot as our empty slot. */
1761 hdr->firstdb = (fbno - xfs_dir2_byte_to_db(args->geo,
1762 XFS_DIR2_FREE_OFFSET)) *
1763 args->geo->free_max_bests;
1764 } else {
1765 xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
1766 }
1767
1768 /* Set the freespace block index from the data block number. */
1769 *findex = xfs_dir2_db_to_fdindex(args->geo, *dbno);
1770
1771 /* Extend the freespace table if the new data block is off the end. */
1772 if (*findex >= hdr->nvalid) {
1773 ASSERT(*findex < args->geo->free_max_bests);
1774 hdr->nvalid = *findex + 1;
1775 hdr->bests[*findex] = cpu_to_be16(NULLDATAOFF);
1776 }
1777
1778 /*
1779 * If this entry was for an empty data block (this should always be
1780 * true) then update the header.
1781 */
1782 if (hdr->bests[*findex] == cpu_to_be16(NULLDATAOFF)) {
1783 hdr->nused++;
1784 xfs_dir2_free_hdr_to_disk(mp, fbp->b_addr, hdr);
1785 xfs_dir2_free_log_header(args, fbp);
1786 }
1787
1788 /* Update the freespace value for the new block in the table. */
1789 bf = xfs_dir2_data_bestfree_p(mp, dbp->b_addr);
1790 hdr->bests[*findex] = bf[0].length;
1791
1792 *dbpp = dbp;
1793 *fbpp = fbp;
1794 return 0;
1795}
1796
1797static int
1798xfs_dir2_node_find_freeblk(
1799 struct xfs_da_args *args,
1800 struct xfs_da_state_blk *fblk,
1801 xfs_dir2_db_t *dbnop,
1802 struct xfs_buf **fbpp,
1803 struct xfs_dir3_icfree_hdr *hdr,
1804 int *findexp,
1805 int length)
1806{
1807 struct xfs_inode *dp = args->dp;
1808 struct xfs_trans *tp = args->trans;
1809 struct xfs_buf *fbp = NULL;
1810 xfs_dir2_db_t firstfbno;
1811 xfs_dir2_db_t lastfbno;
1812 xfs_dir2_db_t ifbno = -1;
1813 xfs_dir2_db_t dbno = -1;
1814 xfs_dir2_db_t fbno;
1815 xfs_fileoff_t fo;
1816 int findex = 0;
1817 int error;
1818
1819 /*
1820 * If we came in with a freespace block that means that lookup
1821 * found an entry with our hash value. This is the freespace
1822 * block for that data entry.
1823 */
1824 if (fblk) {
1825 fbp = fblk->bp;
1826 findex = fblk->index;
1827 xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
1828 if (findex >= 0) {
1829 /* caller already found the freespace for us. */
1830 ASSERT(findex < hdr->nvalid);
1831 ASSERT(be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF);
1832 ASSERT(be16_to_cpu(hdr->bests[findex]) >= length);
1833 dbno = hdr->firstdb + findex;
1834 goto found_block;
1835 }
1836
1837 /*
1838 * The data block looked at didn't have enough room.
1839 * We'll start at the beginning of the freespace entries.
1840 */
1841 ifbno = fblk->blkno;
1842 xfs_trans_brelse(tp, fbp);
1843 fbp = NULL;
1844 fblk->bp = NULL;
1845 }
1846
1847 /*
1848 * If we don't have a data block yet, we're going to scan the freespace
1849 * data for a data block with enough free space in it.
1850 */
1851 error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK);
1852 if (error)
1853 return error;
1854 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
1855 firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET);
1856
1857 for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) {
1858 /* If it's ifbno we already looked at it. */
1859 if (fbno == ifbno)
1860 continue;
1861
1862 /*
1863 * Read the block. There can be holes in the freespace blocks,
1864 * so this might not succeed. This should be really rare, so
1865 * there's no reason to avoid it.
1866 */
1867 error = xfs_dir2_free_try_read(tp, dp, args->owner,
1868 xfs_dir2_db_to_da(args->geo, fbno),
1869 &fbp);
1870 if (error)
1871 return error;
1872 if (!fbp)
1873 continue;
1874
1875 xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
1876
1877 /* Scan the free entry array for a large enough free space. */
1878 for (findex = hdr->nvalid - 1; findex >= 0; findex--) {
1879 if (be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF &&
1880 be16_to_cpu(hdr->bests[findex]) >= length) {
1881 dbno = hdr->firstdb + findex;
1882 goto found_block;
1883 }
1884 }
1885
1886 /* Didn't find free space, go on to next free block */
1887 xfs_trans_brelse(tp, fbp);
1888 }
1889
1890found_block:
1891 *dbnop = dbno;
1892 *fbpp = fbp;
1893 *findexp = findex;
1894 return 0;
1895}
1896
1897/*
1898 * Add the data entry for a node-format directory name addition.
1899 * The leaf entry is added in xfs_dir2_leafn_add.
1900 * We may enter with a freespace block that the lookup found.
1901 */
1902static int
1903xfs_dir2_node_addname_int(
1904 struct xfs_da_args *args, /* operation arguments */
1905 struct xfs_da_state_blk *fblk) /* optional freespace block */
1906{
1907 struct xfs_dir2_data_unused *dup; /* data unused entry pointer */
1908 struct xfs_dir2_data_entry *dep; /* data entry pointer */
1909 struct xfs_dir2_data_hdr *hdr; /* data block header */
1910 struct xfs_dir2_data_free *bf;
1911 struct xfs_trans *tp = args->trans;
1912 struct xfs_inode *dp = args->dp;
1913 struct xfs_dir3_icfree_hdr freehdr;
1914 struct xfs_buf *dbp; /* data block buffer */
1915 struct xfs_buf *fbp; /* freespace buffer */
1916 xfs_dir2_data_aoff_t aoff;
1917 xfs_dir2_db_t dbno; /* data block number */
1918 int error; /* error return value */
1919 int findex; /* freespace entry index */
1920 int length; /* length of the new entry */
1921 int logfree = 0; /* need to log free entry */
1922 int needlog = 0; /* need to log data header */
1923 int needscan = 0; /* need to rescan data frees */
1924 __be16 *tagp; /* data entry tag pointer */
1925
1926 length = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
1927 error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &freehdr,
1928 &findex, length);
1929 if (error)
1930 return error;
1931
1932 /*
1933 * Now we know if we must allocate blocks, so if we are checking whether
1934 * we can insert without allocation then we can return now.
1935 */
1936 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1937 if (dbno == -1)
1938 return -ENOSPC;
1939 return 0;
1940 }
1941
1942 /*
1943 * If we don't have a data block, we need to allocate one and make
1944 * the freespace entries refer to it.
1945 */
1946 if (dbno == -1) {
1947 /* we're going to have to log the free block index later */
1948 logfree = 1;
1949 error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp,
1950 &freehdr, &findex);
1951 } else {
1952 /* Read the data block in. */
1953 error = xfs_dir3_data_read(tp, dp, args->owner,
1954 xfs_dir2_db_to_da(args->geo, dbno), 0, &dbp);
1955 }
1956 if (error)
1957 return error;
1958
1959 /* setup for data block up now */
1960 hdr = dbp->b_addr;
1961 bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
1962 ASSERT(be16_to_cpu(bf[0].length) >= length);
1963
1964 /* Point to the existing unused space. */
1965 dup = (xfs_dir2_data_unused_t *)
1966 ((char *)hdr + be16_to_cpu(bf[0].offset));
1967
1968 /* Mark the first part of the unused space, inuse for us. */
1969 aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
1970 error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
1971 &needlog, &needscan);
1972 if (error) {
1973 xfs_trans_brelse(tp, dbp);
1974 return error;
1975 }
1976
1977 /* Fill in the new entry and log it. */
1978 dep = (xfs_dir2_data_entry_t *)dup;
1979 dep->inumber = cpu_to_be64(args->inumber);
1980 dep->namelen = args->namelen;
1981 memcpy(dep->name, args->name, dep->namelen);
1982 xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
1983 tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
1984 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1985 xfs_dir2_data_log_entry(args, dbp, dep);
1986
1987 /* Rescan the freespace and log the data block if needed. */
1988 if (needscan)
1989 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1990 if (needlog)
1991 xfs_dir2_data_log_header(args, dbp);
1992
1993 /* If the freespace block entry is now wrong, update it. */
1994 if (freehdr.bests[findex] != bf[0].length) {
1995 freehdr.bests[findex] = bf[0].length;
1996 logfree = 1;
1997 }
1998
1999 /* Log the freespace entry if needed. */
2000 if (logfree)
2001 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
2002
2003 /* Return the data block and offset in args. */
2004 args->blkno = (xfs_dablk_t)dbno;
2005 args->index = be16_to_cpu(*tagp);
2006 return 0;
2007}
2008
2009/*
2010 * Top-level node form directory addname routine.
2011 */
2012int /* error */
2013xfs_dir2_node_addname(
2014 xfs_da_args_t *args) /* operation arguments */
2015{
2016 xfs_da_state_blk_t *blk; /* leaf block for insert */
2017 int error; /* error return value */
2018 int rval; /* sub-return value */
2019 xfs_da_state_t *state; /* btree cursor */
2020
2021 trace_xfs_dir2_node_addname(args);
2022
2023 /*
2024 * Allocate and initialize the state (btree cursor).
2025 */
2026 state = xfs_da_state_alloc(args);
2027 /*
2028 * Look up the name. We're not supposed to find it, but
2029 * this gives us the insertion point.
2030 */
2031 error = xfs_da3_node_lookup_int(state, &rval);
2032 if (error)
2033 rval = error;
2034 if (rval != -ENOENT) {
2035 goto done;
2036 }
2037 /*
2038 * Add the data entry to a data block.
2039 * Extravalid is set to a freeblock found by lookup.
2040 */
2041 rval = xfs_dir2_node_addname_int(args,
2042 state->extravalid ? &state->extrablk : NULL);
2043 if (rval) {
2044 goto done;
2045 }
2046 blk = &state->path.blk[state->path.active - 1];
2047 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2048 /*
2049 * Add the new leaf entry.
2050 */
2051 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
2052 if (rval == 0) {
2053 /*
2054 * It worked, fix the hash values up the btree.
2055 */
2056 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
2057 xfs_da3_fixhashpath(state, &state->path);
2058 } else {
2059 /*
2060 * It didn't work, we need to split the leaf block.
2061 */
2062 if (args->total == 0) {
2063 ASSERT(rval == -ENOSPC);
2064 goto done;
2065 }
2066 /*
2067 * Split the leaf block and insert the new entry.
2068 */
2069 rval = xfs_da3_split(state);
2070 }
2071done:
2072 xfs_da_state_free(state);
2073 return rval;
2074}
2075
2076/*
2077 * Lookup an entry in a node-format directory.
2078 * All the real work happens in xfs_da3_node_lookup_int.
2079 * The only real output is the inode number of the entry.
2080 */
2081int /* error */
2082xfs_dir2_node_lookup(
2083 xfs_da_args_t *args) /* operation arguments */
2084{
2085 int error; /* error return value */
2086 int i; /* btree level */
2087 int rval; /* operation return value */
2088 xfs_da_state_t *state; /* btree cursor */
2089
2090 trace_xfs_dir2_node_lookup(args);
2091
2092 /*
2093 * Allocate and initialize the btree cursor.
2094 */
2095 state = xfs_da_state_alloc(args);
2096
2097 /*
2098 * Fill in the path to the entry in the cursor.
2099 */
2100 error = xfs_da3_node_lookup_int(state, &rval);
2101 if (error)
2102 rval = error;
2103 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2104 /* If a CI match, dup the actual name and return -EEXIST */
2105 xfs_dir2_data_entry_t *dep;
2106
2107 dep = (xfs_dir2_data_entry_t *)
2108 ((char *)state->extrablk.bp->b_addr +
2109 state->extrablk.index);
2110 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2111 }
2112 /*
2113 * Release the btree blocks and leaf block.
2114 */
2115 for (i = 0; i < state->path.active; i++) {
2116 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2117 state->path.blk[i].bp = NULL;
2118 }
2119 /*
2120 * Release the data block if we have it.
2121 */
2122 if (state->extravalid && state->extrablk.bp) {
2123 xfs_trans_brelse(args->trans, state->extrablk.bp);
2124 state->extrablk.bp = NULL;
2125 }
2126 xfs_da_state_free(state);
2127 return rval;
2128}
2129
2130/*
2131 * Remove an entry from a node-format directory.
2132 */
2133int /* error */
2134xfs_dir2_node_removename(
2135 struct xfs_da_args *args) /* operation arguments */
2136{
2137 struct xfs_da_state_blk *blk; /* leaf block */
2138 int error; /* error return value */
2139 int rval; /* operation return value */
2140 struct xfs_da_state *state; /* btree cursor */
2141
2142 trace_xfs_dir2_node_removename(args);
2143
2144 /*
2145 * Allocate and initialize the btree cursor.
2146 */
2147 state = xfs_da_state_alloc(args);
2148
2149 /* Look up the entry we're deleting, set up the cursor. */
2150 error = xfs_da3_node_lookup_int(state, &rval);
2151 if (error)
2152 goto out_free;
2153
2154 /* Didn't find it, upper layer screwed up. */
2155 if (rval != -EEXIST) {
2156 error = rval;
2157 goto out_free;
2158 }
2159
2160 blk = &state->path.blk[state->path.active - 1];
2161 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2162 ASSERT(state->extravalid);
2163 /*
2164 * Remove the leaf and data entries.
2165 * Extrablk refers to the data block.
2166 */
2167 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2168 &state->extrablk, &rval);
2169 if (error)
2170 goto out_free;
2171 /*
2172 * Fix the hash values up the btree.
2173 */
2174 xfs_da3_fixhashpath(state, &state->path);
2175 /*
2176 * If we need to join leaf blocks, do it.
2177 */
2178 if (rval && state->path.active > 1)
2179 error = xfs_da3_join(state);
2180 /*
2181 * If no errors so far, try conversion to leaf format.
2182 */
2183 if (!error)
2184 error = xfs_dir2_node_to_leaf(state);
2185out_free:
2186 xfs_da_state_free(state);
2187 return error;
2188}
2189
2190/*
2191 * Replace an entry's inode number in a node-format directory.
2192 */
2193int /* error */
2194xfs_dir2_node_replace(
2195 xfs_da_args_t *args) /* operation arguments */
2196{
2197 xfs_da_state_blk_t *blk; /* leaf block */
2198 xfs_dir2_data_hdr_t *hdr; /* data block header */
2199 xfs_dir2_data_entry_t *dep; /* data entry changed */
2200 int error; /* error return value */
2201 int i; /* btree level */
2202 xfs_ino_t inum; /* new inode number */
2203 int ftype; /* new file type */
2204 int rval; /* internal return value */
2205 xfs_da_state_t *state; /* btree cursor */
2206
2207 trace_xfs_dir2_node_replace(args);
2208
2209 /*
2210 * Allocate and initialize the btree cursor.
2211 */
2212 state = xfs_da_state_alloc(args);
2213
2214 /*
2215 * We have to save new inode number and ftype since
2216 * xfs_da3_node_lookup_int() is going to overwrite them
2217 */
2218 inum = args->inumber;
2219 ftype = args->filetype;
2220
2221 /*
2222 * Lookup the entry to change in the btree.
2223 */
2224 error = xfs_da3_node_lookup_int(state, &rval);
2225 if (error) {
2226 rval = error;
2227 }
2228 /*
2229 * It should be found, since the vnodeops layer has looked it up
2230 * and locked it. But paranoia is good.
2231 */
2232 if (rval == -EEXIST) {
2233 struct xfs_dir3_icleaf_hdr leafhdr;
2234
2235 /*
2236 * Find the leaf entry.
2237 */
2238 blk = &state->path.blk[state->path.active - 1];
2239 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2240 ASSERT(state->extravalid);
2241
2242 xfs_dir2_leaf_hdr_from_disk(state->mp, &leafhdr,
2243 blk->bp->b_addr);
2244 /*
2245 * Point to the data entry.
2246 */
2247 hdr = state->extrablk.bp->b_addr;
2248 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2249 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
2250 dep = (xfs_dir2_data_entry_t *)
2251 ((char *)hdr +
2252 xfs_dir2_dataptr_to_off(args->geo,
2253 be32_to_cpu(leafhdr.ents[blk->index].address)));
2254 ASSERT(inum != be64_to_cpu(dep->inumber));
2255 /*
2256 * Fill in the new inode number and log the entry.
2257 */
2258 dep->inumber = cpu_to_be64(inum);
2259 xfs_dir2_data_put_ftype(state->mp, dep, ftype);
2260 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
2261 rval = 0;
2262 }
2263 /*
2264 * Didn't find it, and we're holding a data block. Drop it.
2265 */
2266 else if (state->extravalid) {
2267 xfs_trans_brelse(args->trans, state->extrablk.bp);
2268 state->extrablk.bp = NULL;
2269 }
2270 /*
2271 * Release all the buffers in the cursor.
2272 */
2273 for (i = 0; i < state->path.active; i++) {
2274 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2275 state->path.blk[i].bp = NULL;
2276 }
2277 xfs_da_state_free(state);
2278 return rval;
2279}
2280
2281/*
2282 * Trim off a trailing empty freespace block.
2283 * Return (in rvalp) 1 if we did it, 0 if not.
2284 */
2285int /* error */
2286xfs_dir2_node_trim_free(
2287 xfs_da_args_t *args, /* operation arguments */
2288 xfs_fileoff_t fo, /* free block number */
2289 int *rvalp) /* out: did something */
2290{
2291 struct xfs_buf *bp; /* freespace buffer */
2292 xfs_inode_t *dp; /* incore directory inode */
2293 int error; /* error return code */
2294 xfs_dir2_free_t *free; /* freespace structure */
2295 xfs_trans_t *tp; /* transaction pointer */
2296 struct xfs_dir3_icfree_hdr freehdr;
2297
2298 dp = args->dp;
2299 tp = args->trans;
2300
2301 *rvalp = 0;
2302
2303 /*
2304 * Read the freespace block.
2305 */
2306 error = xfs_dir2_free_try_read(tp, dp, args->owner, fo, &bp);
2307 if (error)
2308 return error;
2309 /*
2310 * There can be holes in freespace. If fo is a hole, there's
2311 * nothing to do.
2312 */
2313 if (!bp)
2314 return 0;
2315 free = bp->b_addr;
2316 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
2317
2318 /*
2319 * If there are used entries, there's nothing to do.
2320 */
2321 if (freehdr.nused > 0) {
2322 xfs_trans_brelse(tp, bp);
2323 return 0;
2324 }
2325 /*
2326 * Blow the block away.
2327 */
2328 error = xfs_dir2_shrink_inode(args,
2329 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2330 if (error) {
2331 /*
2332 * Can't fail with ENOSPC since that only happens with no
2333 * space reservation, when breaking up an extent into two
2334 * pieces. This is the last block of an extent.
2335 */
2336 ASSERT(error != -ENOSPC);
2337 xfs_trans_brelse(tp, bp);
2338 return error;
2339 }
2340 /*
2341 * Return that we succeeded.
2342 */
2343 *rvalp = 1;
2344 return 0;
2345}