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