]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/xfs/xfs_dir2_node.c
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_types.h"
23 #include "xfs_trans.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_dir2_sf.h"
32 #include "xfs_attr_sf.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
36 #include "xfs_dir2_data.h"
37 #include "xfs_dir2_leaf.h"
38 #include "xfs_dir2_block.h"
39 #include "xfs_dir2_node.h"
40 #include "xfs_dir2_trace.h"
41 #include "xfs_error.h"
44 * Function declarations.
46 static void xfs_dir2_free_log_header(xfs_trans_t
*tp
, xfs_dabuf_t
*bp
);
47 static int xfs_dir2_leafn_add(xfs_dabuf_t
*bp
, xfs_da_args_t
*args
, int index
);
49 static void xfs_dir2_leafn_check(xfs_inode_t
*dp
, xfs_dabuf_t
*bp
);
51 #define xfs_dir2_leafn_check(dp, bp)
53 static void xfs_dir2_leafn_moveents(xfs_da_args_t
*args
, xfs_dabuf_t
*bp_s
,
54 int start_s
, xfs_dabuf_t
*bp_d
, int start_d
,
56 static void xfs_dir2_leafn_rebalance(xfs_da_state_t
*state
,
57 xfs_da_state_blk_t
*blk1
,
58 xfs_da_state_blk_t
*blk2
);
59 static int xfs_dir2_leafn_remove(xfs_da_args_t
*args
, xfs_dabuf_t
*bp
,
60 int index
, xfs_da_state_blk_t
*dblk
,
62 static int xfs_dir2_node_addname_int(xfs_da_args_t
*args
,
63 xfs_da_state_blk_t
*fblk
);
66 * Log entries from a freespace block.
69 xfs_dir2_free_log_bests(
70 xfs_trans_t
*tp
, /* transaction pointer */
71 xfs_dabuf_t
*bp
, /* freespace buffer */
72 int first
, /* first entry to log */
73 int last
) /* last entry to log */
75 xfs_dir2_free_t
*free
; /* freespace structure */
78 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
79 xfs_da_log_buf(tp
, bp
,
80 (uint
)((char *)&free
->bests
[first
] - (char *)free
),
81 (uint
)((char *)&free
->bests
[last
] - (char *)free
+
82 sizeof(free
->bests
[0]) - 1));
86 * Log header from a freespace block.
89 xfs_dir2_free_log_header(
90 xfs_trans_t
*tp
, /* transaction pointer */
91 xfs_dabuf_t
*bp
) /* freespace buffer */
93 xfs_dir2_free_t
*free
; /* freespace structure */
96 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
97 xfs_da_log_buf(tp
, bp
, (uint
)((char *)&free
->hdr
- (char *)free
),
98 (uint
)(sizeof(xfs_dir2_free_hdr_t
) - 1));
102 * Convert a leaf-format directory to a node-format directory.
103 * We need to change the magic number of the leaf block, and copy
104 * the freespace table out of the leaf block into its own block.
107 xfs_dir2_leaf_to_node(
108 xfs_da_args_t
*args
, /* operation arguments */
109 xfs_dabuf_t
*lbp
) /* leaf buffer */
111 xfs_inode_t
*dp
; /* incore directory inode */
112 int error
; /* error return value */
113 xfs_dabuf_t
*fbp
; /* freespace buffer */
114 xfs_dir2_db_t fdb
; /* freespace block number */
115 xfs_dir2_free_t
*free
; /* freespace structure */
116 __be16
*from
; /* pointer to freespace entry */
117 int i
; /* leaf freespace index */
118 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
119 xfs_dir2_leaf_tail_t
*ltp
; /* leaf tail structure */
120 xfs_mount_t
*mp
; /* filesystem mount point */
121 int n
; /* count of live freespc ents */
122 xfs_dir2_data_off_t off
; /* freespace entry value */
123 __be16
*to
; /* pointer to freespace entry */
124 xfs_trans_t
*tp
; /* transaction pointer */
126 xfs_dir2_trace_args_b("leaf_to_node", args
, lbp
);
131 * Add a freespace block to the directory.
133 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
, &fdb
))) {
136 ASSERT(fdb
== XFS_DIR2_FREE_FIRSTDB(mp
));
138 * Get the buffer for the new freespace block.
140 if ((error
= xfs_da_get_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, fdb
), -1, &fbp
,
147 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
149 * Initialize the freespace block header.
151 free
->hdr
.magic
= cpu_to_be32(XFS_DIR2_FREE_MAGIC
);
152 free
->hdr
.firstdb
= 0;
153 ASSERT(be32_to_cpu(ltp
->bestcount
) <= (uint
)dp
->i_d
.di_size
/ mp
->m_dirblksize
);
154 free
->hdr
.nvalid
= ltp
->bestcount
;
156 * Copy freespace entries from the leaf block to the new block.
157 * Count active entries.
159 for (i
= n
= 0, from
= xfs_dir2_leaf_bests_p(ltp
), to
= free
->bests
;
160 i
< be32_to_cpu(ltp
->bestcount
); i
++, from
++, to
++) {
161 if ((off
= be16_to_cpu(*from
)) != NULLDATAOFF
)
163 *to
= cpu_to_be16(off
);
165 free
->hdr
.nused
= cpu_to_be32(n
);
166 leaf
->hdr
.info
.magic
= cpu_to_be16(XFS_DIR2_LEAFN_MAGIC
);
170 xfs_dir2_leaf_log_header(tp
, lbp
);
171 xfs_dir2_free_log_header(tp
, fbp
);
172 xfs_dir2_free_log_bests(tp
, fbp
, 0, be32_to_cpu(free
->hdr
.nvalid
) - 1);
173 xfs_da_buf_done(fbp
);
174 xfs_dir2_leafn_check(dp
, lbp
);
179 * Add a leaf entry to a leaf block in a node-form directory.
180 * The other work necessary is done from the caller.
182 static int /* error */
184 xfs_dabuf_t
*bp
, /* leaf buffer */
185 xfs_da_args_t
*args
, /* operation arguments */
186 int index
) /* insertion pt for new entry */
188 int compact
; /* compacting stale leaves */
189 xfs_inode_t
*dp
; /* incore directory inode */
190 int highstale
; /* next stale entry */
191 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
192 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
193 int lfloghigh
; /* high leaf entry logging */
194 int lfloglow
; /* low leaf entry logging */
195 int lowstale
; /* previous stale entry */
196 xfs_mount_t
*mp
; /* filesystem mount point */
197 xfs_trans_t
*tp
; /* transaction pointer */
199 xfs_dir2_trace_args_sb("leafn_add", args
, index
, bp
);
206 * Quick check just to make sure we are not going to index
207 * into other peoples memory
210 return XFS_ERROR(EFSCORRUPTED
);
213 * If there are already the maximum number of leaf entries in
214 * the block, if there are no stale entries it won't fit.
215 * Caller will do a split. If there are stale entries we'll do
219 if (be16_to_cpu(leaf
->hdr
.count
) == xfs_dir2_max_leaf_ents(mp
)) {
220 if (!leaf
->hdr
.stale
)
221 return XFS_ERROR(ENOSPC
);
222 compact
= be16_to_cpu(leaf
->hdr
.stale
) > 1;
225 ASSERT(index
== 0 || be32_to_cpu(leaf
->ents
[index
- 1].hashval
) <= args
->hashval
);
226 ASSERT(index
== be16_to_cpu(leaf
->hdr
.count
) ||
227 be32_to_cpu(leaf
->ents
[index
].hashval
) >= args
->hashval
);
233 * Compact out all but one stale leaf entry. Leaves behind
234 * the entry closest to index.
237 xfs_dir2_leaf_compact_x1(bp
, &index
, &lowstale
, &highstale
,
238 &lfloglow
, &lfloghigh
);
241 * Set impossible logging indices for this case.
243 else if (leaf
->hdr
.stale
) {
244 lfloglow
= be16_to_cpu(leaf
->hdr
.count
);
248 * No stale entries, just insert a space for the new entry.
250 if (!leaf
->hdr
.stale
) {
251 lep
= &leaf
->ents
[index
];
252 if (index
< be16_to_cpu(leaf
->hdr
.count
))
253 memmove(lep
+ 1, lep
,
254 (be16_to_cpu(leaf
->hdr
.count
) - index
) * sizeof(*lep
));
256 lfloghigh
= be16_to_cpu(leaf
->hdr
.count
);
257 be16_add_cpu(&leaf
->hdr
.count
, 1);
260 * There are stale entries. We'll use one for the new entry.
264 * If we didn't do a compact then we need to figure out
265 * which stale entry will be used.
269 * Find first stale entry before our insertion point.
271 for (lowstale
= index
- 1;
273 be32_to_cpu(leaf
->ents
[lowstale
].address
) !=
274 XFS_DIR2_NULL_DATAPTR
;
278 * Find next stale entry after insertion point.
279 * Stop looking if the answer would be worse than
280 * lowstale already found.
282 for (highstale
= index
;
283 highstale
< be16_to_cpu(leaf
->hdr
.count
) &&
284 be32_to_cpu(leaf
->ents
[highstale
].address
) !=
285 XFS_DIR2_NULL_DATAPTR
&&
287 index
- lowstale
- 1 >= highstale
- index
);
292 * Using the low stale entry.
293 * Shift entries up toward the stale slot.
296 (highstale
== be16_to_cpu(leaf
->hdr
.count
) ||
297 index
- lowstale
- 1 < highstale
- index
)) {
298 ASSERT(be32_to_cpu(leaf
->ents
[lowstale
].address
) ==
299 XFS_DIR2_NULL_DATAPTR
);
300 ASSERT(index
- lowstale
- 1 >= 0);
301 if (index
- lowstale
- 1 > 0)
302 memmove(&leaf
->ents
[lowstale
],
303 &leaf
->ents
[lowstale
+ 1],
304 (index
- lowstale
- 1) * sizeof(*lep
));
305 lep
= &leaf
->ents
[index
- 1];
306 lfloglow
= MIN(lowstale
, lfloglow
);
307 lfloghigh
= MAX(index
- 1, lfloghigh
);
310 * Using the high stale entry.
311 * Shift entries down toward the stale slot.
314 ASSERT(be32_to_cpu(leaf
->ents
[highstale
].address
) ==
315 XFS_DIR2_NULL_DATAPTR
);
316 ASSERT(highstale
- index
>= 0);
317 if (highstale
- index
> 0)
318 memmove(&leaf
->ents
[index
+ 1],
320 (highstale
- index
) * sizeof(*lep
));
321 lep
= &leaf
->ents
[index
];
322 lfloglow
= MIN(index
, lfloglow
);
323 lfloghigh
= MAX(highstale
, lfloghigh
);
325 be16_add_cpu(&leaf
->hdr
.stale
, -1);
328 * Insert the new entry, log everything.
330 lep
->hashval
= cpu_to_be32(args
->hashval
);
331 lep
->address
= cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp
,
332 args
->blkno
, args
->index
));
333 xfs_dir2_leaf_log_header(tp
, bp
);
334 xfs_dir2_leaf_log_ents(tp
, bp
, lfloglow
, lfloghigh
);
335 xfs_dir2_leafn_check(dp
, bp
);
341 * Check internal consistency of a leafn block.
344 xfs_dir2_leafn_check(
345 xfs_inode_t
*dp
, /* incore directory inode */
346 xfs_dabuf_t
*bp
) /* leaf buffer */
348 int i
; /* leaf index */
349 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
350 xfs_mount_t
*mp
; /* filesystem mount point */
351 int stale
; /* count of stale leaves */
355 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
356 ASSERT(be16_to_cpu(leaf
->hdr
.count
) <= xfs_dir2_max_leaf_ents(mp
));
357 for (i
= stale
= 0; i
< be16_to_cpu(leaf
->hdr
.count
); i
++) {
358 if (i
+ 1 < be16_to_cpu(leaf
->hdr
.count
)) {
359 ASSERT(be32_to_cpu(leaf
->ents
[i
].hashval
) <=
360 be32_to_cpu(leaf
->ents
[i
+ 1].hashval
));
362 if (be32_to_cpu(leaf
->ents
[i
].address
) == XFS_DIR2_NULL_DATAPTR
)
365 ASSERT(be16_to_cpu(leaf
->hdr
.stale
) == stale
);
370 * Return the last hash value in the leaf.
371 * Stale entries are ok.
373 xfs_dahash_t
/* hash value */
374 xfs_dir2_leafn_lasthash(
375 xfs_dabuf_t
*bp
, /* leaf buffer */
376 int *count
) /* count of entries in leaf */
378 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
381 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
383 *count
= be16_to_cpu(leaf
->hdr
.count
);
384 if (!leaf
->hdr
.count
)
386 return be32_to_cpu(leaf
->ents
[be16_to_cpu(leaf
->hdr
.count
) - 1].hashval
);
390 * Look up a leaf entry for space to add a name in a node-format leaf block.
391 * The extrablk in state is a freespace block.
394 xfs_dir2_leafn_lookup_for_addname(
395 xfs_dabuf_t
*bp
, /* leaf buffer */
396 xfs_da_args_t
*args
, /* operation arguments */
397 int *indexp
, /* out: leaf entry index */
398 xfs_da_state_t
*state
) /* state to fill in */
400 xfs_dabuf_t
*curbp
= NULL
; /* current data/free buffer */
401 xfs_dir2_db_t curdb
= -1; /* current data block number */
402 xfs_dir2_db_t curfdb
= -1; /* current free block number */
403 xfs_inode_t
*dp
; /* incore directory inode */
404 int error
; /* error return value */
405 int fi
; /* free entry index */
406 xfs_dir2_free_t
*free
= NULL
; /* free block structure */
407 int index
; /* leaf entry index */
408 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
409 int length
; /* length of new data entry */
410 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
411 xfs_mount_t
*mp
; /* filesystem mount point */
412 xfs_dir2_db_t newdb
; /* new data block number */
413 xfs_dir2_db_t newfdb
; /* new free block number */
414 xfs_trans_t
*tp
; /* transaction pointer */
420 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
422 ASSERT(be16_to_cpu(leaf
->hdr
.count
) > 0);
424 xfs_dir2_leafn_check(dp
, bp
);
426 * Look up the hash value in the leaf entries.
428 index
= xfs_dir2_leaf_search_hash(args
, bp
);
430 * Do we have a buffer coming in?
432 if (state
->extravalid
) {
433 /* If so, it's a free block buffer, get the block number. */
434 curbp
= state
->extrablk
.bp
;
435 curfdb
= state
->extrablk
.blkno
;
437 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
439 length
= xfs_dir2_data_entsize(args
->namelen
);
441 * Loop over leaf entries with the right hash value.
443 for (lep
= &leaf
->ents
[index
]; index
< be16_to_cpu(leaf
->hdr
.count
) &&
444 be32_to_cpu(lep
->hashval
) == args
->hashval
;
447 * Skip stale leaf entries.
449 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
452 * Pull the data block number from the entry.
454 newdb
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
456 * For addname, we're looking for a place to put the new entry.
457 * We want to use a data block with an entry of equal
458 * hash value to ours if there is one with room.
460 * If this block isn't the data block we already have
461 * in hand, take a look at it.
463 if (newdb
!= curdb
) {
466 * Convert the data block to the free block
467 * holding its freespace information.
469 newfdb
= xfs_dir2_db_to_fdb(mp
, newdb
);
471 * If it's not the one we have in hand, read it in.
473 if (newfdb
!= curfdb
) {
475 * If we had one before, drop it.
478 xfs_da_brelse(tp
, curbp
);
480 * Read the free block.
482 error
= xfs_da_read_buf(tp
, dp
,
483 xfs_dir2_db_to_da(mp
, newfdb
),
484 -1, &curbp
, XFS_DATA_FORK
);
488 ASSERT(be32_to_cpu(free
->hdr
.magic
) ==
489 XFS_DIR2_FREE_MAGIC
);
490 ASSERT((be32_to_cpu(free
->hdr
.firstdb
) %
491 XFS_DIR2_MAX_FREE_BESTS(mp
)) == 0);
492 ASSERT(be32_to_cpu(free
->hdr
.firstdb
) <= curdb
);
493 ASSERT(curdb
< be32_to_cpu(free
->hdr
.firstdb
) +
494 be32_to_cpu(free
->hdr
.nvalid
));
497 * Get the index for our entry.
499 fi
= xfs_dir2_db_to_fdindex(mp
, curdb
);
501 * If it has room, return it.
503 if (unlikely(be16_to_cpu(free
->bests
[fi
]) == NULLDATAOFF
)) {
504 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
505 XFS_ERRLEVEL_LOW
, mp
);
506 if (curfdb
!= newfdb
)
507 xfs_da_brelse(tp
, curbp
);
508 return XFS_ERROR(EFSCORRUPTED
);
511 if (be16_to_cpu(free
->bests
[fi
]) >= length
)
515 /* Didn't find any space */
518 ASSERT(args
->oknoent
);
520 /* Giving back a free block. */
521 state
->extravalid
= 1;
522 state
->extrablk
.bp
= curbp
;
523 state
->extrablk
.index
= fi
;
524 state
->extrablk
.blkno
= curfdb
;
525 state
->extrablk
.magic
= XFS_DIR2_FREE_MAGIC
;
527 state
->extravalid
= 0;
530 * Return the index, that will be the insertion point.
533 return XFS_ERROR(ENOENT
);
537 * Look up a leaf entry in a node-format leaf block.
538 * The extrablk in state a data block.
541 xfs_dir2_leafn_lookup_for_entry(
542 xfs_dabuf_t
*bp
, /* leaf buffer */
543 xfs_da_args_t
*args
, /* operation arguments */
544 int *indexp
, /* out: leaf entry index */
545 xfs_da_state_t
*state
) /* state to fill in */
547 xfs_dabuf_t
*curbp
= NULL
; /* current data/free buffer */
548 xfs_dir2_db_t curdb
= -1; /* current data block number */
549 xfs_dir2_data_entry_t
*dep
; /* data block entry */
550 xfs_inode_t
*dp
; /* incore directory inode */
551 int error
; /* error return value */
552 int di
; /* data entry index */
553 int index
; /* leaf entry index */
554 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
555 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
556 xfs_mount_t
*mp
; /* filesystem mount point */
557 xfs_dir2_db_t newdb
; /* new data block number */
558 xfs_trans_t
*tp
; /* transaction pointer */
559 enum xfs_dacmp cmp
; /* comparison result */
565 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
567 ASSERT(be16_to_cpu(leaf
->hdr
.count
) > 0);
569 xfs_dir2_leafn_check(dp
, bp
);
571 * Look up the hash value in the leaf entries.
573 index
= xfs_dir2_leaf_search_hash(args
, bp
);
575 * Do we have a buffer coming in?
577 if (state
->extravalid
) {
578 curbp
= state
->extrablk
.bp
;
579 curdb
= state
->extrablk
.blkno
;
582 * Loop over leaf entries with the right hash value.
584 for (lep
= &leaf
->ents
[index
]; index
< be16_to_cpu(leaf
->hdr
.count
) &&
585 be32_to_cpu(lep
->hashval
) == args
->hashval
;
588 * Skip stale leaf entries.
590 if (be32_to_cpu(lep
->address
) == XFS_DIR2_NULL_DATAPTR
)
593 * Pull the data block number from the entry.
595 newdb
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
597 * Not adding a new entry, so we really want to find
598 * the name given to us.
600 * If it's a different data block, go get it.
602 if (newdb
!= curdb
) {
604 * If we had a block before, drop it.
607 xfs_da_brelse(tp
, curbp
);
609 * Read the data block.
611 error
= xfs_da_read_buf(tp
, dp
, xfs_dir2_db_to_da(mp
,
612 newdb
), -1, &curbp
, XFS_DATA_FORK
);
615 xfs_dir2_data_check(dp
, curbp
);
619 * Point to the data entry.
621 dep
= (xfs_dir2_data_entry_t
*)((char *)curbp
->data
+
622 xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
)));
624 * Compare the entry and if it's an exact match, return
625 * EEXIST immediately. If it's the first case-insensitive
626 * match, store the inode number and continue looking.
628 cmp
= mp
->m_dirnameops
->compname(args
, dep
->name
, dep
->namelen
);
629 if (cmp
!= XFS_CMP_DIFFERENT
&& cmp
!= args
->cmpresult
) {
630 args
->cmpresult
= cmp
;
631 args
->inumber
= be64_to_cpu(dep
->inumber
);
632 di
= (int)((char *)dep
- (char *)curbp
->data
);
634 if (cmp
== XFS_CMP_EXACT
)
638 /* Didn't find an exact match. */
641 ASSERT(index
== be16_to_cpu(leaf
->hdr
.count
) || args
->oknoent
);
644 /* Giving back a data block. */
645 state
->extravalid
= 1;
646 state
->extrablk
.bp
= curbp
;
647 state
->extrablk
.index
= di
;
648 state
->extrablk
.blkno
= curdb
;
649 state
->extrablk
.magic
= XFS_DIR2_DATA_MAGIC
;
651 state
->extravalid
= 0;
654 * Return the index, that will be the insertion point.
657 return XFS_ERROR(error
);
661 * Look up a leaf entry in a node-format leaf block.
662 * If this is an addname then the extrablk in state is a freespace block,
663 * otherwise it's a data block.
666 xfs_dir2_leafn_lookup_int(
667 xfs_dabuf_t
*bp
, /* leaf buffer */
668 xfs_da_args_t
*args
, /* operation arguments */
669 int *indexp
, /* out: leaf entry index */
670 xfs_da_state_t
*state
) /* state to fill in */
673 return xfs_dir2_leafn_lookup_for_addname(bp
, args
, indexp
,
675 return xfs_dir2_leafn_lookup_for_entry(bp
, args
, indexp
, state
);
679 * Move count leaf entries from source to destination leaf.
680 * Log entries and headers. Stale entries are preserved.
683 xfs_dir2_leafn_moveents(
684 xfs_da_args_t
*args
, /* operation arguments */
685 xfs_dabuf_t
*bp_s
, /* source leaf buffer */
686 int start_s
, /* source leaf index */
687 xfs_dabuf_t
*bp_d
, /* destination leaf buffer */
688 int start_d
, /* destination leaf index */
689 int count
) /* count of leaves to copy */
691 xfs_dir2_leaf_t
*leaf_d
; /* destination leaf structure */
692 xfs_dir2_leaf_t
*leaf_s
; /* source leaf structure */
693 int stale
; /* count stale leaves copied */
694 xfs_trans_t
*tp
; /* transaction pointer */
696 xfs_dir2_trace_args_bibii("leafn_moveents", args
, bp_s
, start_s
, bp_d
,
699 * Silently return if nothing to do.
708 * If the destination index is not the end of the current
709 * destination leaf entries, open up a hole in the destination
710 * to hold the new entries.
712 if (start_d
< be16_to_cpu(leaf_d
->hdr
.count
)) {
713 memmove(&leaf_d
->ents
[start_d
+ count
], &leaf_d
->ents
[start_d
],
714 (be16_to_cpu(leaf_d
->hdr
.count
) - start_d
) *
715 sizeof(xfs_dir2_leaf_entry_t
));
716 xfs_dir2_leaf_log_ents(tp
, bp_d
, start_d
+ count
,
717 count
+ be16_to_cpu(leaf_d
->hdr
.count
) - 1);
720 * If the source has stale leaves, count the ones in the copy range
721 * so we can update the header correctly.
723 if (leaf_s
->hdr
.stale
) {
724 int i
; /* temp leaf index */
726 for (i
= start_s
, stale
= 0; i
< start_s
+ count
; i
++) {
727 if (be32_to_cpu(leaf_s
->ents
[i
].address
) == XFS_DIR2_NULL_DATAPTR
)
733 * Copy the leaf entries from source to destination.
735 memcpy(&leaf_d
->ents
[start_d
], &leaf_s
->ents
[start_s
],
736 count
* sizeof(xfs_dir2_leaf_entry_t
));
737 xfs_dir2_leaf_log_ents(tp
, bp_d
, start_d
, start_d
+ count
- 1);
739 * If there are source entries after the ones we copied,
740 * delete the ones we copied by sliding the next ones down.
742 if (start_s
+ count
< be16_to_cpu(leaf_s
->hdr
.count
)) {
743 memmove(&leaf_s
->ents
[start_s
], &leaf_s
->ents
[start_s
+ count
],
744 count
* sizeof(xfs_dir2_leaf_entry_t
));
745 xfs_dir2_leaf_log_ents(tp
, bp_s
, start_s
, start_s
+ count
- 1);
748 * Update the headers and log them.
750 be16_add_cpu(&leaf_s
->hdr
.count
, -(count
));
751 be16_add_cpu(&leaf_s
->hdr
.stale
, -(stale
));
752 be16_add_cpu(&leaf_d
->hdr
.count
, count
);
753 be16_add_cpu(&leaf_d
->hdr
.stale
, stale
);
754 xfs_dir2_leaf_log_header(tp
, bp_s
);
755 xfs_dir2_leaf_log_header(tp
, bp_d
);
756 xfs_dir2_leafn_check(args
->dp
, bp_s
);
757 xfs_dir2_leafn_check(args
->dp
, bp_d
);
761 * Determine the sort order of two leaf blocks.
762 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
765 xfs_dir2_leafn_order(
766 xfs_dabuf_t
*leaf1_bp
, /* leaf1 buffer */
767 xfs_dabuf_t
*leaf2_bp
) /* leaf2 buffer */
769 xfs_dir2_leaf_t
*leaf1
; /* leaf1 structure */
770 xfs_dir2_leaf_t
*leaf2
; /* leaf2 structure */
772 leaf1
= leaf1_bp
->data
;
773 leaf2
= leaf2_bp
->data
;
774 ASSERT(be16_to_cpu(leaf1
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
775 ASSERT(be16_to_cpu(leaf2
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
776 if (be16_to_cpu(leaf1
->hdr
.count
) > 0 &&
777 be16_to_cpu(leaf2
->hdr
.count
) > 0 &&
778 (be32_to_cpu(leaf2
->ents
[0].hashval
) < be32_to_cpu(leaf1
->ents
[0].hashval
) ||
779 be32_to_cpu(leaf2
->ents
[be16_to_cpu(leaf2
->hdr
.count
) - 1].hashval
) <
780 be32_to_cpu(leaf1
->ents
[be16_to_cpu(leaf1
->hdr
.count
) - 1].hashval
)))
786 * Rebalance leaf entries between two leaf blocks.
787 * This is actually only called when the second block is new,
788 * though the code deals with the general case.
789 * A new entry will be inserted in one of the blocks, and that
790 * entry is taken into account when balancing.
793 xfs_dir2_leafn_rebalance(
794 xfs_da_state_t
*state
, /* btree cursor */
795 xfs_da_state_blk_t
*blk1
, /* first btree block */
796 xfs_da_state_blk_t
*blk2
) /* second btree block */
798 xfs_da_args_t
*args
; /* operation arguments */
799 int count
; /* count (& direction) leaves */
800 int isleft
; /* new goes in left leaf */
801 xfs_dir2_leaf_t
*leaf1
; /* first leaf structure */
802 xfs_dir2_leaf_t
*leaf2
; /* second leaf structure */
803 int mid
; /* midpoint leaf index */
805 int oldstale
; /* old count of stale leaves */
807 int oldsum
; /* old total leaf count */
808 int swap
; /* swapped leaf blocks */
812 * If the block order is wrong, swap the arguments.
814 if ((swap
= xfs_dir2_leafn_order(blk1
->bp
, blk2
->bp
))) {
815 xfs_da_state_blk_t
*tmp
; /* temp for block swap */
821 leaf1
= blk1
->bp
->data
;
822 leaf2
= blk2
->bp
->data
;
823 oldsum
= be16_to_cpu(leaf1
->hdr
.count
) + be16_to_cpu(leaf2
->hdr
.count
);
825 oldstale
= be16_to_cpu(leaf1
->hdr
.stale
) + be16_to_cpu(leaf2
->hdr
.stale
);
829 * If the old leaf count was odd then the new one will be even,
830 * so we need to divide the new count evenly.
833 xfs_dahash_t midhash
; /* middle entry hash value */
835 if (mid
>= be16_to_cpu(leaf1
->hdr
.count
))
836 midhash
= be32_to_cpu(leaf2
->ents
[mid
- be16_to_cpu(leaf1
->hdr
.count
)].hashval
);
838 midhash
= be32_to_cpu(leaf1
->ents
[mid
].hashval
);
839 isleft
= args
->hashval
<= midhash
;
842 * If the old count is even then the new count is odd, so there's
843 * no preferred side for the new entry.
849 * Calculate moved entry count. Positive means left-to-right,
850 * negative means right-to-left. Then move the entries.
852 count
= be16_to_cpu(leaf1
->hdr
.count
) - mid
+ (isleft
== 0);
854 xfs_dir2_leafn_moveents(args
, blk1
->bp
,
855 be16_to_cpu(leaf1
->hdr
.count
) - count
, blk2
->bp
, 0, count
);
857 xfs_dir2_leafn_moveents(args
, blk2
->bp
, 0, blk1
->bp
,
858 be16_to_cpu(leaf1
->hdr
.count
), count
);
859 ASSERT(be16_to_cpu(leaf1
->hdr
.count
) + be16_to_cpu(leaf2
->hdr
.count
) == oldsum
);
860 ASSERT(be16_to_cpu(leaf1
->hdr
.stale
) + be16_to_cpu(leaf2
->hdr
.stale
) == oldstale
);
862 * Mark whether we're inserting into the old or new leaf.
864 if (be16_to_cpu(leaf1
->hdr
.count
) < be16_to_cpu(leaf2
->hdr
.count
))
865 state
->inleaf
= swap
;
866 else if (be16_to_cpu(leaf1
->hdr
.count
) > be16_to_cpu(leaf2
->hdr
.count
))
867 state
->inleaf
= !swap
;
870 swap
^ (blk1
->index
<= be16_to_cpu(leaf1
->hdr
.count
));
872 * Adjust the expected index for insertion.
875 blk2
->index
= blk1
->index
- be16_to_cpu(leaf1
->hdr
.count
);
878 * Finally sanity check just to make sure we are not returning a
881 if(blk2
->index
< 0) {
885 "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting original leaf: "
892 * Remove an entry from a node directory.
893 * This removes the leaf entry and the data entry,
894 * and updates the free block if necessary.
896 static int /* error */
897 xfs_dir2_leafn_remove(
898 xfs_da_args_t
*args
, /* operation arguments */
899 xfs_dabuf_t
*bp
, /* leaf buffer */
900 int index
, /* leaf entry index */
901 xfs_da_state_blk_t
*dblk
, /* data block */
902 int *rval
) /* resulting block needs join */
904 xfs_dir2_data_t
*data
; /* data block structure */
905 xfs_dir2_db_t db
; /* data block number */
906 xfs_dabuf_t
*dbp
; /* data block buffer */
907 xfs_dir2_data_entry_t
*dep
; /* data block entry */
908 xfs_inode_t
*dp
; /* incore directory inode */
909 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
910 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry */
911 int longest
; /* longest data free entry */
912 int off
; /* data block entry offset */
913 xfs_mount_t
*mp
; /* filesystem mount point */
914 int needlog
; /* need to log data header */
915 int needscan
; /* need to rescan data frees */
916 xfs_trans_t
*tp
; /* transaction pointer */
918 xfs_dir2_trace_args_sb("leafn_remove", args
, index
, bp
);
923 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
925 * Point to the entry we're removing.
927 lep
= &leaf
->ents
[index
];
929 * Extract the data block and offset from the entry.
931 db
= xfs_dir2_dataptr_to_db(mp
, be32_to_cpu(lep
->address
));
932 ASSERT(dblk
->blkno
== db
);
933 off
= xfs_dir2_dataptr_to_off(mp
, be32_to_cpu(lep
->address
));
934 ASSERT(dblk
->index
== off
);
936 * Kill the leaf entry by marking it stale.
937 * Log the leaf block changes.
939 be16_add_cpu(&leaf
->hdr
.stale
, 1);
940 xfs_dir2_leaf_log_header(tp
, bp
);
941 lep
->address
= cpu_to_be32(XFS_DIR2_NULL_DATAPTR
);
942 xfs_dir2_leaf_log_ents(tp
, bp
, index
, index
);
944 * Make the data entry free. Keep track of the longest freespace
945 * in the data block in case it changes.
949 dep
= (xfs_dir2_data_entry_t
*)((char *)data
+ off
);
950 longest
= be16_to_cpu(data
->hdr
.bestfree
[0].length
);
951 needlog
= needscan
= 0;
952 xfs_dir2_data_make_free(tp
, dbp
, off
,
953 xfs_dir2_data_entsize(dep
->namelen
), &needlog
, &needscan
);
955 * Rescan the data block freespaces for bestfree.
956 * Log the data block header if needed.
959 xfs_dir2_data_freescan(mp
, data
, &needlog
);
961 xfs_dir2_data_log_header(tp
, dbp
);
962 xfs_dir2_data_check(dp
, dbp
);
964 * If the longest data block freespace changes, need to update
965 * the corresponding freeblock entry.
967 if (longest
< be16_to_cpu(data
->hdr
.bestfree
[0].length
)) {
968 int error
; /* error return value */
969 xfs_dabuf_t
*fbp
; /* freeblock buffer */
970 xfs_dir2_db_t fdb
; /* freeblock block number */
971 int findex
; /* index in freeblock entries */
972 xfs_dir2_free_t
*free
; /* freeblock structure */
973 int logfree
; /* need to log free entry */
976 * Convert the data block number to a free block,
977 * read in the free block.
979 fdb
= xfs_dir2_db_to_fdb(mp
, db
);
980 if ((error
= xfs_da_read_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, fdb
),
981 -1, &fbp
, XFS_DATA_FORK
))) {
985 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
986 ASSERT(be32_to_cpu(free
->hdr
.firstdb
) ==
987 XFS_DIR2_MAX_FREE_BESTS(mp
) *
988 (fdb
- XFS_DIR2_FREE_FIRSTDB(mp
)));
990 * Calculate which entry we need to fix.
992 findex
= xfs_dir2_db_to_fdindex(mp
, db
);
993 longest
= be16_to_cpu(data
->hdr
.bestfree
[0].length
);
995 * If the data block is now empty we can get rid of it
998 if (longest
== mp
->m_dirblksize
- (uint
)sizeof(data
->hdr
)) {
1000 * Try to punch out the data block.
1002 error
= xfs_dir2_shrink_inode(args
, db
, dbp
);
1008 * We can get ENOSPC if there's no space reservation.
1009 * In this case just drop the buffer and some one else
1010 * will eventually get rid of the empty block.
1012 else if (error
== ENOSPC
&& args
->total
== 0)
1013 xfs_da_buf_done(dbp
);
1018 * If we got rid of the data block, we can eliminate that entry
1019 * in the free block.
1023 * One less used entry in the free table.
1025 be32_add_cpu(&free
->hdr
.nused
, -1);
1026 xfs_dir2_free_log_header(tp
, fbp
);
1028 * If this was the last entry in the table, we can
1029 * trim the table size back. There might be other
1030 * entries at the end referring to non-existent
1031 * data blocks, get those too.
1033 if (findex
== be32_to_cpu(free
->hdr
.nvalid
) - 1) {
1034 int i
; /* free entry index */
1036 for (i
= findex
- 1;
1037 i
>= 0 && be16_to_cpu(free
->bests
[i
]) == NULLDATAOFF
;
1040 free
->hdr
.nvalid
= cpu_to_be32(i
+ 1);
1044 * Not the last entry, just punch it out.
1047 free
->bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1051 * If there are no useful entries left in the block,
1052 * get rid of the block if we can.
1054 if (!free
->hdr
.nused
) {
1055 error
= xfs_dir2_shrink_inode(args
, fdb
, fbp
);
1059 } else if (error
!= ENOSPC
|| args
->total
!= 0)
1062 * It's possible to get ENOSPC if there is no
1063 * space reservation. In this case some one
1064 * else will eventually get rid of this block.
1069 * Data block is not empty, just set the free entry to
1073 free
->bests
[findex
] = cpu_to_be16(longest
);
1077 * Log the free entry that changed, unless we got rid of it.
1080 xfs_dir2_free_log_bests(tp
, fbp
, findex
, findex
);
1082 * Drop the buffer if we still have it.
1085 xfs_da_buf_done(fbp
);
1087 xfs_dir2_leafn_check(dp
, bp
);
1089 * Return indication of whether this leaf block is emtpy enough
1090 * to justify trying to join it with a neighbor.
1093 ((uint
)sizeof(leaf
->hdr
) +
1094 (uint
)sizeof(leaf
->ents
[0]) *
1095 (be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
))) <
1101 * Split the leaf entries in the old block into old and new blocks.
1104 xfs_dir2_leafn_split(
1105 xfs_da_state_t
*state
, /* btree cursor */
1106 xfs_da_state_blk_t
*oldblk
, /* original block */
1107 xfs_da_state_blk_t
*newblk
) /* newly created block */
1109 xfs_da_args_t
*args
; /* operation arguments */
1110 xfs_dablk_t blkno
; /* new leaf block number */
1111 int error
; /* error return value */
1112 xfs_mount_t
*mp
; /* filesystem mount point */
1115 * Allocate space for a new leaf node.
1118 mp
= args
->dp
->i_mount
;
1119 ASSERT(args
!= NULL
);
1120 ASSERT(oldblk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1121 error
= xfs_da_grow_inode(args
, &blkno
);
1126 * Initialize the new leaf block.
1128 error
= xfs_dir2_leaf_init(args
, xfs_dir2_da_to_db(mp
, blkno
),
1129 &newblk
->bp
, XFS_DIR2_LEAFN_MAGIC
);
1133 newblk
->blkno
= blkno
;
1134 newblk
->magic
= XFS_DIR2_LEAFN_MAGIC
;
1136 * Rebalance the entries across the two leaves, link the new
1137 * block into the leaves.
1139 xfs_dir2_leafn_rebalance(state
, oldblk
, newblk
);
1140 error
= xfs_da_blk_link(state
, oldblk
, newblk
);
1145 * Insert the new entry in the correct block.
1148 error
= xfs_dir2_leafn_add(oldblk
->bp
, args
, oldblk
->index
);
1150 error
= xfs_dir2_leafn_add(newblk
->bp
, args
, newblk
->index
);
1152 * Update last hashval in each block since we added the name.
1154 oldblk
->hashval
= xfs_dir2_leafn_lasthash(oldblk
->bp
, NULL
);
1155 newblk
->hashval
= xfs_dir2_leafn_lasthash(newblk
->bp
, NULL
);
1156 xfs_dir2_leafn_check(args
->dp
, oldblk
->bp
);
1157 xfs_dir2_leafn_check(args
->dp
, newblk
->bp
);
1162 * Check a leaf block and its neighbors to see if the block should be
1163 * collapsed into one or the other neighbor. Always keep the block
1164 * with the smaller block number.
1165 * If the current block is over 50% full, don't try to join it, return 0.
1166 * If the block is empty, fill in the state structure and return 2.
1167 * If it can be collapsed, fill in the state structure and return 1.
1168 * If nothing can be done, return 0.
1171 xfs_dir2_leafn_toosmall(
1172 xfs_da_state_t
*state
, /* btree cursor */
1173 int *action
) /* resulting action to take */
1175 xfs_da_state_blk_t
*blk
; /* leaf block */
1176 xfs_dablk_t blkno
; /* leaf block number */
1177 xfs_dabuf_t
*bp
; /* leaf buffer */
1178 int bytes
; /* bytes in use */
1179 int count
; /* leaf live entry count */
1180 int error
; /* error return value */
1181 int forward
; /* sibling block direction */
1182 int i
; /* sibling counter */
1183 xfs_da_blkinfo_t
*info
; /* leaf block header */
1184 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1185 int rval
; /* result from path_shift */
1188 * Check for the degenerate case of the block being over 50% full.
1189 * If so, it's not worth even looking to see if we might be able
1190 * to coalesce with a sibling.
1192 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1193 info
= blk
->bp
->data
;
1194 ASSERT(be16_to_cpu(info
->magic
) == XFS_DIR2_LEAFN_MAGIC
);
1195 leaf
= (xfs_dir2_leaf_t
*)info
;
1196 count
= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1197 bytes
= (uint
)sizeof(leaf
->hdr
) + count
* (uint
)sizeof(leaf
->ents
[0]);
1198 if (bytes
> (state
->blocksize
>> 1)) {
1200 * Blk over 50%, don't try to join.
1206 * Check for the degenerate case of the block being empty.
1207 * If the block is empty, we'll simply delete it, no need to
1208 * coalesce it with a sibling block. We choose (arbitrarily)
1209 * to merge with the forward block unless it is NULL.
1213 * Make altpath point to the block we want to keep and
1214 * path point to the block we want to drop (this one).
1216 forward
= (info
->forw
!= 0);
1217 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1218 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
, 0,
1222 *action
= rval
? 2 : 0;
1226 * Examine each sibling block to see if we can coalesce with
1227 * at least 25% free space to spare. We need to figure out
1228 * whether to merge with the forward or the backward block.
1229 * We prefer coalescing with the lower numbered sibling so as
1230 * to shrink a directory over time.
1232 forward
= be32_to_cpu(info
->forw
) < be32_to_cpu(info
->back
);
1233 for (i
= 0, bp
= NULL
; i
< 2; forward
= !forward
, i
++) {
1234 blkno
= forward
? be32_to_cpu(info
->forw
) : be32_to_cpu(info
->back
);
1238 * Read the sibling leaf block.
1241 xfs_da_read_buf(state
->args
->trans
, state
->args
->dp
, blkno
,
1242 -1, &bp
, XFS_DATA_FORK
))) {
1247 * Count bytes in the two blocks combined.
1249 leaf
= (xfs_dir2_leaf_t
*)info
;
1250 count
= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1251 bytes
= state
->blocksize
- (state
->blocksize
>> 2);
1253 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
1254 count
+= be16_to_cpu(leaf
->hdr
.count
) - be16_to_cpu(leaf
->hdr
.stale
);
1255 bytes
-= count
* (uint
)sizeof(leaf
->ents
[0]);
1257 * Fits with at least 25% to spare.
1261 xfs_da_brelse(state
->args
->trans
, bp
);
1264 * Didn't like either block, give up.
1271 * Done with the sibling leaf block here, drop the dabuf
1272 * so path_shift can get it.
1274 xfs_da_buf_done(bp
);
1276 * Make altpath point to the block we want to keep (the lower
1277 * numbered block) and path point to the block we want to drop.
1279 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1280 if (blkno
< blk
->blkno
)
1281 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
, 0,
1284 error
= xfs_da_path_shift(state
, &state
->path
, forward
, 0,
1289 *action
= rval
? 0 : 1;
1294 * Move all the leaf entries from drop_blk to save_blk.
1295 * This is done as part of a join operation.
1298 xfs_dir2_leafn_unbalance(
1299 xfs_da_state_t
*state
, /* cursor */
1300 xfs_da_state_blk_t
*drop_blk
, /* dead block */
1301 xfs_da_state_blk_t
*save_blk
) /* surviving block */
1303 xfs_da_args_t
*args
; /* operation arguments */
1304 xfs_dir2_leaf_t
*drop_leaf
; /* dead leaf structure */
1305 xfs_dir2_leaf_t
*save_leaf
; /* surviving leaf structure */
1308 ASSERT(drop_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1309 ASSERT(save_blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1310 drop_leaf
= drop_blk
->bp
->data
;
1311 save_leaf
= save_blk
->bp
->data
;
1312 ASSERT(be16_to_cpu(drop_leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
1313 ASSERT(be16_to_cpu(save_leaf
->hdr
.info
.magic
) == XFS_DIR2_LEAFN_MAGIC
);
1315 * If there are any stale leaf entries, take this opportunity
1318 if (drop_leaf
->hdr
.stale
)
1319 xfs_dir2_leaf_compact(args
, drop_blk
->bp
);
1320 if (save_leaf
->hdr
.stale
)
1321 xfs_dir2_leaf_compact(args
, save_blk
->bp
);
1323 * Move the entries from drop to the appropriate end of save.
1325 drop_blk
->hashval
= be32_to_cpu(drop_leaf
->ents
[be16_to_cpu(drop_leaf
->hdr
.count
) - 1].hashval
);
1326 if (xfs_dir2_leafn_order(save_blk
->bp
, drop_blk
->bp
))
1327 xfs_dir2_leafn_moveents(args
, drop_blk
->bp
, 0, save_blk
->bp
, 0,
1328 be16_to_cpu(drop_leaf
->hdr
.count
));
1330 xfs_dir2_leafn_moveents(args
, drop_blk
->bp
, 0, save_blk
->bp
,
1331 be16_to_cpu(save_leaf
->hdr
.count
), be16_to_cpu(drop_leaf
->hdr
.count
));
1332 save_blk
->hashval
= be32_to_cpu(save_leaf
->ents
[be16_to_cpu(save_leaf
->hdr
.count
) - 1].hashval
);
1333 xfs_dir2_leafn_check(args
->dp
, save_blk
->bp
);
1337 * Top-level node form directory addname routine.
1340 xfs_dir2_node_addname(
1341 xfs_da_args_t
*args
) /* operation arguments */
1343 xfs_da_state_blk_t
*blk
; /* leaf block for insert */
1344 int error
; /* error return value */
1345 int rval
; /* sub-return value */
1346 xfs_da_state_t
*state
; /* btree cursor */
1348 xfs_dir2_trace_args("node_addname", args
);
1350 * Allocate and initialize the state (btree cursor).
1352 state
= xfs_da_state_alloc();
1354 state
->mp
= args
->dp
->i_mount
;
1355 state
->blocksize
= state
->mp
->m_dirblksize
;
1356 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1358 * Look up the name. We're not supposed to find it, but
1359 * this gives us the insertion point.
1361 error
= xfs_da_node_lookup_int(state
, &rval
);
1364 if (rval
!= ENOENT
) {
1368 * Add the data entry to a data block.
1369 * Extravalid is set to a freeblock found by lookup.
1371 rval
= xfs_dir2_node_addname_int(args
,
1372 state
->extravalid
? &state
->extrablk
: NULL
);
1376 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1377 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1379 * Add the new leaf entry.
1381 rval
= xfs_dir2_leafn_add(blk
->bp
, args
, blk
->index
);
1384 * It worked, fix the hash values up the btree.
1386 if (!args
->justcheck
)
1387 xfs_da_fixhashpath(state
, &state
->path
);
1390 * It didn't work, we need to split the leaf block.
1392 if (args
->total
== 0) {
1393 ASSERT(rval
== ENOSPC
);
1397 * Split the leaf block and insert the new entry.
1399 rval
= xfs_da_split(state
);
1402 xfs_da_state_free(state
);
1407 * Add the data entry for a node-format directory name addition.
1408 * The leaf entry is added in xfs_dir2_leafn_add.
1409 * We may enter with a freespace block that the lookup found.
1411 static int /* error */
1412 xfs_dir2_node_addname_int(
1413 xfs_da_args_t
*args
, /* operation arguments */
1414 xfs_da_state_blk_t
*fblk
) /* optional freespace block */
1416 xfs_dir2_data_t
*data
; /* data block structure */
1417 xfs_dir2_db_t dbno
; /* data block number */
1418 xfs_dabuf_t
*dbp
; /* data block buffer */
1419 xfs_dir2_data_entry_t
*dep
; /* data entry pointer */
1420 xfs_inode_t
*dp
; /* incore directory inode */
1421 xfs_dir2_data_unused_t
*dup
; /* data unused entry pointer */
1422 int error
; /* error return value */
1423 xfs_dir2_db_t fbno
; /* freespace block number */
1424 xfs_dabuf_t
*fbp
; /* freespace buffer */
1425 int findex
; /* freespace entry index */
1426 xfs_dir2_free_t
*free
=NULL
; /* freespace block structure */
1427 xfs_dir2_db_t ifbno
; /* initial freespace block no */
1428 xfs_dir2_db_t lastfbno
=0; /* highest freespace block no */
1429 int length
; /* length of the new entry */
1430 int logfree
; /* need to log free entry */
1431 xfs_mount_t
*mp
; /* filesystem mount point */
1432 int needlog
; /* need to log data header */
1433 int needscan
; /* need to rescan data frees */
1434 __be16
*tagp
; /* data entry tag pointer */
1435 xfs_trans_t
*tp
; /* transaction pointer */
1440 length
= xfs_dir2_data_entsize(args
->namelen
);
1442 * If we came in with a freespace block that means that lookup
1443 * found an entry with our hash value. This is the freespace
1444 * block for that data entry.
1449 * Remember initial freespace block number.
1451 ifbno
= fblk
->blkno
;
1453 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
1454 findex
= fblk
->index
;
1456 * This means the free entry showed that the data block had
1457 * space for our entry, so we remembered it.
1458 * Use that data block.
1461 ASSERT(findex
< be32_to_cpu(free
->hdr
.nvalid
));
1462 ASSERT(be16_to_cpu(free
->bests
[findex
]) != NULLDATAOFF
);
1463 ASSERT(be16_to_cpu(free
->bests
[findex
]) >= length
);
1464 dbno
= be32_to_cpu(free
->hdr
.firstdb
) + findex
;
1467 * The data block looked at didn't have enough room.
1468 * We'll start at the beginning of the freespace entries.
1476 * Didn't come in with a freespace block, so don't have a data block.
1484 * If we don't have a data block yet, we're going to scan the
1485 * freespace blocks looking for one. Figure out what the
1486 * highest freespace block number is.
1489 xfs_fileoff_t fo
; /* freespace block number */
1491 if ((error
= xfs_bmap_last_offset(tp
, dp
, &fo
, XFS_DATA_FORK
)))
1493 lastfbno
= xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)fo
);
1497 * While we haven't identified a data block, search the freeblock
1498 * data for a good data block. If we find a null freeblock entry,
1499 * indicating a hole in the data blocks, remember that.
1501 while (dbno
== -1) {
1503 * If we don't have a freeblock in hand, get the next one.
1507 * Happens the first time through unless lookup gave
1508 * us a freespace block to start with.
1511 fbno
= XFS_DIR2_FREE_FIRSTDB(mp
);
1513 * If it's ifbno we already looked at it.
1518 * If it's off the end we're done.
1520 if (fbno
>= lastfbno
)
1523 * Read the block. There can be holes in the
1524 * freespace blocks, so this might not succeed.
1525 * This should be really rare, so there's no reason
1528 if ((error
= xfs_da_read_buf(tp
, dp
,
1529 xfs_dir2_db_to_da(mp
, fbno
), -2, &fbp
,
1533 if (unlikely(fbp
== NULL
)) {
1537 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
1541 * Look at the current free entry. Is it good enough?
1543 if (be16_to_cpu(free
->bests
[findex
]) != NULLDATAOFF
&&
1544 be16_to_cpu(free
->bests
[findex
]) >= length
)
1545 dbno
= be32_to_cpu(free
->hdr
.firstdb
) + findex
;
1548 * Are we done with the freeblock?
1550 if (++findex
== be32_to_cpu(free
->hdr
.nvalid
)) {
1554 xfs_da_brelse(tp
, fbp
);
1556 if (fblk
&& fblk
->bp
)
1562 * If we don't have a data block, we need to allocate one and make
1563 * the freespace entries refer to it.
1565 if (unlikely(dbno
== -1)) {
1567 * Not allowed to allocate, return failure.
1569 if (args
->justcheck
|| args
->total
== 0) {
1571 * Drop the freespace buffer unless it came from our
1574 if ((fblk
== NULL
|| fblk
->bp
== NULL
) && fbp
!= NULL
)
1575 xfs_da_buf_done(fbp
);
1576 return XFS_ERROR(ENOSPC
);
1579 * Allocate and initialize the new data block.
1581 if (unlikely((error
= xfs_dir2_grow_inode(args
,
1582 XFS_DIR2_DATA_SPACE
,
1584 (error
= xfs_dir2_data_init(args
, dbno
, &dbp
)))) {
1586 * Drop the freespace buffer unless it came from our
1589 if ((fblk
== NULL
|| fblk
->bp
== NULL
) && fbp
!= NULL
)
1590 xfs_da_buf_done(fbp
);
1594 * If (somehow) we have a freespace block, get rid of it.
1597 xfs_da_brelse(tp
, fbp
);
1598 if (fblk
&& fblk
->bp
)
1602 * Get the freespace block corresponding to the data block
1603 * that was just allocated.
1605 fbno
= xfs_dir2_db_to_fdb(mp
, dbno
);
1606 if (unlikely(error
= xfs_da_read_buf(tp
, dp
,
1607 xfs_dir2_db_to_da(mp
, fbno
), -2, &fbp
,
1609 xfs_da_buf_done(dbp
);
1613 * If there wasn't a freespace block, the read will
1614 * return a NULL fbp. Allocate and initialize a new one.
1617 if ((error
= xfs_dir2_grow_inode(args
, XFS_DIR2_FREE_SPACE
,
1622 if (unlikely(xfs_dir2_db_to_fdb(mp
, dbno
) != fbno
)) {
1624 "xfs_dir2_node_addname_int: dir ino "
1625 "%llu needed freesp block %lld for\n"
1626 " data block %lld, got %lld\n"
1627 " ifbno %llu lastfbno %d\n",
1628 (unsigned long long)dp
->i_ino
,
1629 (long long)xfs_dir2_db_to_fdb(mp
, dbno
),
1630 (long long)dbno
, (long long)fbno
,
1631 (unsigned long long)ifbno
, lastfbno
);
1634 " fblk 0x%p blkno %llu "
1635 "index %d magic 0x%x\n",
1637 (unsigned long long)fblk
->blkno
,
1642 " ... fblk is NULL\n");
1644 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1645 XFS_ERRLEVEL_LOW
, mp
);
1646 return XFS_ERROR(EFSCORRUPTED
);
1650 * Get a buffer for the new block.
1652 if ((error
= xfs_da_get_buf(tp
, dp
,
1653 xfs_dir2_db_to_da(mp
, fbno
),
1654 -1, &fbp
, XFS_DATA_FORK
))) {
1657 ASSERT(fbp
!= NULL
);
1660 * Initialize the new block to be empty, and remember
1661 * its first slot as our empty slot.
1664 free
->hdr
.magic
= cpu_to_be32(XFS_DIR2_FREE_MAGIC
);
1665 free
->hdr
.firstdb
= cpu_to_be32(
1666 (fbno
- XFS_DIR2_FREE_FIRSTDB(mp
)) *
1667 XFS_DIR2_MAX_FREE_BESTS(mp
));
1668 free
->hdr
.nvalid
= 0;
1669 free
->hdr
.nused
= 0;
1672 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
1676 * Set the freespace block index from the data block number.
1678 findex
= xfs_dir2_db_to_fdindex(mp
, dbno
);
1680 * If it's after the end of the current entries in the
1681 * freespace block, extend that table.
1683 if (findex
>= be32_to_cpu(free
->hdr
.nvalid
)) {
1684 ASSERT(findex
< XFS_DIR2_MAX_FREE_BESTS(mp
));
1685 free
->hdr
.nvalid
= cpu_to_be32(findex
+ 1);
1687 * Tag new entry so nused will go up.
1689 free
->bests
[findex
] = cpu_to_be16(NULLDATAOFF
);
1692 * If this entry was for an empty data block
1693 * (this should always be true) then update the header.
1695 if (be16_to_cpu(free
->bests
[findex
]) == NULLDATAOFF
) {
1696 be32_add_cpu(&free
->hdr
.nused
, 1);
1697 xfs_dir2_free_log_header(tp
, fbp
);
1700 * Update the real value in the table.
1701 * We haven't allocated the data entry yet so this will
1705 free
->bests
[findex
] = data
->hdr
.bestfree
[0].length
;
1709 * We had a data block so we don't have to make a new one.
1713 * If just checking, we succeeded.
1715 if (args
->justcheck
) {
1716 if ((fblk
== NULL
|| fblk
->bp
== NULL
) && fbp
!= NULL
)
1717 xfs_da_buf_done(fbp
);
1721 * Read the data block in.
1724 error
= xfs_da_read_buf(tp
, dp
, xfs_dir2_db_to_da(mp
, dbno
),
1725 -1, &dbp
, XFS_DATA_FORK
))) {
1726 if ((fblk
== NULL
|| fblk
->bp
== NULL
) && fbp
!= NULL
)
1727 xfs_da_buf_done(fbp
);
1733 ASSERT(be16_to_cpu(data
->hdr
.bestfree
[0].length
) >= length
);
1735 * Point to the existing unused space.
1737 dup
= (xfs_dir2_data_unused_t
*)
1738 ((char *)data
+ be16_to_cpu(data
->hdr
.bestfree
[0].offset
));
1739 needscan
= needlog
= 0;
1741 * Mark the first part of the unused space, inuse for us.
1743 xfs_dir2_data_use_free(tp
, dbp
, dup
,
1744 (xfs_dir2_data_aoff_t
)((char *)dup
- (char *)data
), length
,
1745 &needlog
, &needscan
);
1747 * Fill in the new entry and log it.
1749 dep
= (xfs_dir2_data_entry_t
*)dup
;
1750 dep
->inumber
= cpu_to_be64(args
->inumber
);
1751 dep
->namelen
= args
->namelen
;
1752 memcpy(dep
->name
, args
->name
, dep
->namelen
);
1753 tagp
= xfs_dir2_data_entry_tag_p(dep
);
1754 *tagp
= cpu_to_be16((char *)dep
- (char *)data
);
1755 xfs_dir2_data_log_entry(tp
, dbp
, dep
);
1757 * Rescan the block for bestfree if needed.
1760 xfs_dir2_data_freescan(mp
, data
, &needlog
);
1762 * Log the data block header if needed.
1765 xfs_dir2_data_log_header(tp
, dbp
);
1767 * If the freespace entry is now wrong, update it.
1769 if (be16_to_cpu(free
->bests
[findex
]) != be16_to_cpu(data
->hdr
.bestfree
[0].length
)) {
1770 free
->bests
[findex
] = data
->hdr
.bestfree
[0].length
;
1774 * Log the freespace entry if needed.
1777 xfs_dir2_free_log_bests(tp
, fbp
, findex
, findex
);
1779 * If the caller didn't hand us the freespace block, drop it.
1781 if ((fblk
== NULL
|| fblk
->bp
== NULL
) && fbp
!= NULL
)
1782 xfs_da_buf_done(fbp
);
1784 * Return the data block and offset in args, then drop the data block.
1786 args
->blkno
= (xfs_dablk_t
)dbno
;
1787 args
->index
= be16_to_cpu(*tagp
);
1788 xfs_da_buf_done(dbp
);
1793 * Lookup an entry in a node-format directory.
1794 * All the real work happens in xfs_da_node_lookup_int.
1795 * The only real output is the inode number of the entry.
1798 xfs_dir2_node_lookup(
1799 xfs_da_args_t
*args
) /* operation arguments */
1801 int error
; /* error return value */
1802 int i
; /* btree level */
1803 int rval
; /* operation return value */
1804 xfs_da_state_t
*state
; /* btree cursor */
1806 xfs_dir2_trace_args("node_lookup", args
);
1808 * Allocate and initialize the btree cursor.
1810 state
= xfs_da_state_alloc();
1812 state
->mp
= args
->dp
->i_mount
;
1813 state
->blocksize
= state
->mp
->m_dirblksize
;
1814 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1816 * Fill in the path to the entry in the cursor.
1818 error
= xfs_da_node_lookup_int(state
, &rval
);
1821 else if (rval
== ENOENT
&& args
->cmpresult
== XFS_CMP_CASE
)
1822 rval
= EEXIST
; /* a case-insensitive match was found */
1824 * Release the btree blocks and leaf block.
1826 for (i
= 0; i
< state
->path
.active
; i
++) {
1827 xfs_da_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1828 state
->path
.blk
[i
].bp
= NULL
;
1831 * Release the data block if we have it.
1833 if (state
->extravalid
&& state
->extrablk
.bp
) {
1834 xfs_da_brelse(args
->trans
, state
->extrablk
.bp
);
1835 state
->extrablk
.bp
= NULL
;
1837 xfs_da_state_free(state
);
1842 * Remove an entry from a node-format directory.
1845 xfs_dir2_node_removename(
1846 xfs_da_args_t
*args
) /* operation arguments */
1848 xfs_da_state_blk_t
*blk
; /* leaf block */
1849 int error
; /* error return value */
1850 int rval
; /* operation return value */
1851 xfs_da_state_t
*state
; /* btree cursor */
1853 xfs_dir2_trace_args("node_removename", args
);
1855 * Allocate and initialize the btree cursor.
1857 state
= xfs_da_state_alloc();
1859 state
->mp
= args
->dp
->i_mount
;
1860 state
->blocksize
= state
->mp
->m_dirblksize
;
1861 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1863 * Look up the entry we're deleting, set up the cursor.
1865 error
= xfs_da_node_lookup_int(state
, &rval
);
1869 * Didn't find it, upper layer screwed up.
1871 if (rval
!= EEXIST
) {
1872 xfs_da_state_free(state
);
1875 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1876 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1877 ASSERT(state
->extravalid
);
1879 * Remove the leaf and data entries.
1880 * Extrablk refers to the data block.
1882 error
= xfs_dir2_leafn_remove(args
, blk
->bp
, blk
->index
,
1883 &state
->extrablk
, &rval
);
1887 * Fix the hash values up the btree.
1889 xfs_da_fixhashpath(state
, &state
->path
);
1891 * If we need to join leaf blocks, do it.
1893 if (rval
&& state
->path
.active
> 1)
1894 error
= xfs_da_join(state
);
1896 * If no errors so far, try conversion to leaf format.
1899 error
= xfs_dir2_node_to_leaf(state
);
1900 xfs_da_state_free(state
);
1905 * Replace an entry's inode number in a node-format directory.
1908 xfs_dir2_node_replace(
1909 xfs_da_args_t
*args
) /* operation arguments */
1911 xfs_da_state_blk_t
*blk
; /* leaf block */
1912 xfs_dir2_data_t
*data
; /* data block structure */
1913 xfs_dir2_data_entry_t
*dep
; /* data entry changed */
1914 int error
; /* error return value */
1915 int i
; /* btree level */
1916 xfs_ino_t inum
; /* new inode number */
1917 xfs_dir2_leaf_t
*leaf
; /* leaf structure */
1918 xfs_dir2_leaf_entry_t
*lep
; /* leaf entry being changed */
1919 int rval
; /* internal return value */
1920 xfs_da_state_t
*state
; /* btree cursor */
1922 xfs_dir2_trace_args("node_replace", args
);
1924 * Allocate and initialize the btree cursor.
1926 state
= xfs_da_state_alloc();
1928 state
->mp
= args
->dp
->i_mount
;
1929 state
->blocksize
= state
->mp
->m_dirblksize
;
1930 state
->node_ents
= state
->mp
->m_dir_node_ents
;
1931 inum
= args
->inumber
;
1933 * Lookup the entry to change in the btree.
1935 error
= xfs_da_node_lookup_int(state
, &rval
);
1940 * It should be found, since the vnodeops layer has looked it up
1941 * and locked it. But paranoia is good.
1943 if (rval
== EEXIST
) {
1945 * Find the leaf entry.
1947 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1948 ASSERT(blk
->magic
== XFS_DIR2_LEAFN_MAGIC
);
1949 leaf
= blk
->bp
->data
;
1950 lep
= &leaf
->ents
[blk
->index
];
1951 ASSERT(state
->extravalid
);
1953 * Point to the data entry.
1955 data
= state
->extrablk
.bp
->data
;
1956 ASSERT(be32_to_cpu(data
->hdr
.magic
) == XFS_DIR2_DATA_MAGIC
);
1957 dep
= (xfs_dir2_data_entry_t
*)
1959 xfs_dir2_dataptr_to_off(state
->mp
, be32_to_cpu(lep
->address
)));
1960 ASSERT(inum
!= be64_to_cpu(dep
->inumber
));
1962 * Fill in the new inode number and log the entry.
1964 dep
->inumber
= cpu_to_be64(inum
);
1965 xfs_dir2_data_log_entry(args
->trans
, state
->extrablk
.bp
, dep
);
1969 * Didn't find it, and we're holding a data block. Drop it.
1971 else if (state
->extravalid
) {
1972 xfs_da_brelse(args
->trans
, state
->extrablk
.bp
);
1973 state
->extrablk
.bp
= NULL
;
1976 * Release all the buffers in the cursor.
1978 for (i
= 0; i
< state
->path
.active
; i
++) {
1979 xfs_da_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1980 state
->path
.blk
[i
].bp
= NULL
;
1982 xfs_da_state_free(state
);
1987 * Trim off a trailing empty freespace block.
1988 * Return (in rvalp) 1 if we did it, 0 if not.
1991 xfs_dir2_node_trim_free(
1992 xfs_da_args_t
*args
, /* operation arguments */
1993 xfs_fileoff_t fo
, /* free block number */
1994 int *rvalp
) /* out: did something */
1996 xfs_dabuf_t
*bp
; /* freespace buffer */
1997 xfs_inode_t
*dp
; /* incore directory inode */
1998 int error
; /* error return code */
1999 xfs_dir2_free_t
*free
; /* freespace structure */
2000 xfs_mount_t
*mp
; /* filesystem mount point */
2001 xfs_trans_t
*tp
; /* transaction pointer */
2007 * Read the freespace block.
2009 if (unlikely(error
= xfs_da_read_buf(tp
, dp
, (xfs_dablk_t
)fo
, -2, &bp
,
2015 * There can be holes in freespace. If fo is a hole, there's
2022 ASSERT(be32_to_cpu(free
->hdr
.magic
) == XFS_DIR2_FREE_MAGIC
);
2024 * If there are used entries, there's nothing to do.
2026 if (be32_to_cpu(free
->hdr
.nused
) > 0) {
2027 xfs_da_brelse(tp
, bp
);
2032 * Blow the block away.
2035 xfs_dir2_shrink_inode(args
, xfs_dir2_da_to_db(mp
, (xfs_dablk_t
)fo
),
2038 * Can't fail with ENOSPC since that only happens with no
2039 * space reservation, when breaking up an extent into two
2040 * pieces. This is the last block of an extent.
2042 ASSERT(error
!= ENOSPC
);
2043 xfs_da_brelse(tp
, bp
);
2047 * Return that we succeeded.