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