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