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