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