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