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