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