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