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