]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_dir2_leaf.c
libxfs: sync attr code with kernel
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dir2_leaf.c
CommitLineData
2bd0ea18 1/*
5e656dbb 2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
65b80c98 3 * Copyright (c) 2013 Red Hat, Inc.
da23017d 4 * All Rights Reserved.
5000d01d 5 *
da23017d
NS
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
2bd0ea18 8 * published by the Free Software Foundation.
5000d01d 9 *
da23017d
NS
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.
5000d01d 14 *
da23017d
NS
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
2bd0ea18
NS
18 */
19
5e656dbb
BN
20#include <xfs.h>
21
2bd0ea18 22/*
5e656dbb 23 * Local function declarations.
2bd0ea18 24 */
a2ceac1f
DC
25static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
26 int *indexp, struct xfs_buf **dbpp);
65b80c98 27static void xfs_dir3_leaf_log_bests(struct xfs_trans *tp, struct xfs_buf *bp,
5e656dbb 28 int first, int last);
65b80c98 29static void xfs_dir3_leaf_log_tail(struct xfs_trans *tp, struct xfs_buf *bp);
2bd0ea18 30
65b80c98
DC
31/*
32 * Check the internal consistency of a leaf1 block.
33 * Pop an assert if something is wrong.
34 */
35#ifdef DEBUG
36#define xfs_dir3_leaf_check(mp, bp) \
37do { \
38 if (!xfs_dir3_leaf1_check((mp), (bp))) \
39 ASSERT(0); \
40} while (0);
41
42STATIC bool
43xfs_dir3_leaf1_check(
44 struct xfs_mount *mp,
45 struct xfs_buf *bp)
46{
47 struct xfs_dir2_leaf *leaf = bp->b_addr;
48 struct xfs_dir3_icleaf_hdr leafhdr;
49
50 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
51
52 if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
53 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
54 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
55 return false;
56 } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
57 return false;
58
59 return xfs_dir3_leaf_check_int(mp, &leafhdr, leaf);
60}
61#else
62#define xfs_dir3_leaf_check(mp, bp)
63#endif
64
65void
66xfs_dir3_leaf_hdr_from_disk(
67 struct xfs_dir3_icleaf_hdr *to,
68 struct xfs_dir2_leaf *from)
69{
70 if (from->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
71 from->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)) {
72 to->forw = be32_to_cpu(from->hdr.info.forw);
73 to->back = be32_to_cpu(from->hdr.info.back);
74 to->magic = be16_to_cpu(from->hdr.info.magic);
75 to->count = be16_to_cpu(from->hdr.count);
76 to->stale = be16_to_cpu(from->hdr.stale);
77 } else {
78 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)from;
79
80 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
81 to->back = be32_to_cpu(hdr3->info.hdr.back);
82 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
83 to->count = be16_to_cpu(hdr3->count);
84 to->stale = be16_to_cpu(hdr3->stale);
85 }
86
87 ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
88 to->magic == XFS_DIR3_LEAF1_MAGIC ||
89 to->magic == XFS_DIR2_LEAFN_MAGIC ||
90 to->magic == XFS_DIR3_LEAFN_MAGIC);
91}
92
93void
94xfs_dir3_leaf_hdr_to_disk(
95 struct xfs_dir2_leaf *to,
96 struct xfs_dir3_icleaf_hdr *from)
97{
98 ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
99 from->magic == XFS_DIR3_LEAF1_MAGIC ||
100 from->magic == XFS_DIR2_LEAFN_MAGIC ||
101 from->magic == XFS_DIR3_LEAFN_MAGIC);
102
103 if (from->magic == XFS_DIR2_LEAF1_MAGIC ||
104 from->magic == XFS_DIR2_LEAFN_MAGIC) {
105 to->hdr.info.forw = cpu_to_be32(from->forw);
106 to->hdr.info.back = cpu_to_be32(from->back);
107 to->hdr.info.magic = cpu_to_be16(from->magic);
108 to->hdr.count = cpu_to_be16(from->count);
109 to->hdr.stale = cpu_to_be16(from->stale);
110 } else {
111 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)to;
112
113 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
114 hdr3->info.hdr.back = cpu_to_be32(from->back);
115 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
116 hdr3->count = cpu_to_be16(from->count);
117 hdr3->stale = cpu_to_be16(from->stale);
118 }
119}
120
121bool
122xfs_dir3_leaf_check_int(
123 struct xfs_mount *mp,
124 struct xfs_dir3_icleaf_hdr *hdr,
125 struct xfs_dir2_leaf *leaf)
126{
127 struct xfs_dir2_leaf_entry *ents;
128 xfs_dir2_leaf_tail_t *ltp;
129 int stale;
130 int i;
131
132 ents = xfs_dir3_leaf_ents_p(leaf);
133 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
134
135 /*
136 * XXX (dgc): This value is not restrictive enough.
137 * Should factor in the size of the bests table as well.
138 * We can deduce a value for that from di_size.
139 */
140 if (hdr->count > xfs_dir3_max_leaf_ents(mp, leaf))
141 return false;
142
143 /* Leaves and bests don't overlap in leaf format. */
144 if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
145 hdr->magic == XFS_DIR3_LEAF1_MAGIC) &&
146 (char *)&ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
147 return false;
148
149 /* Check hash value order, count stale entries. */
150 for (i = stale = 0; i < hdr->count; i++) {
151 if (i + 1 < hdr->count) {
152 if (be32_to_cpu(ents[i].hashval) >
153 be32_to_cpu(ents[i + 1].hashval))
154 return false;
155 }
156 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
157 stale++;
158 }
159 if (hdr->stale != stale)
160 return false;
161 return true;
162}
163
164static bool
165xfs_dir3_leaf_verify(
a2ceac1f 166 struct xfs_buf *bp,
65b80c98 167 __uint16_t magic)
a2ceac1f
DC
168{
169 struct xfs_mount *mp = bp->b_target->bt_mount;
65b80c98
DC
170 struct xfs_dir2_leaf *leaf = bp->b_addr;
171 struct xfs_dir3_icleaf_hdr leafhdr;
172
173 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
174
175 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
176 if (xfs_sb_version_hascrc(&mp->m_sb)) {
177 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
a2ceac1f 178
65b80c98
DC
179 if ((magic == XFS_DIR2_LEAF1_MAGIC &&
180 leafhdr.magic != XFS_DIR3_LEAF1_MAGIC) ||
181 (magic == XFS_DIR2_LEAFN_MAGIC &&
182 leafhdr.magic != XFS_DIR3_LEAFN_MAGIC))
183 return false;
184
185 if (!uuid_equal(&leaf3->info.uuid, &mp->m_sb.sb_uuid))
186 return false;
187 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
188 return false;
189 } else {
190 if (leafhdr.magic != magic)
191 return false;
192 }
193 return xfs_dir3_leaf_check_int(mp, &leafhdr, leaf);
194}
195
196static void
197__read_verify(
198 struct xfs_buf *bp,
199 __uint16_t magic)
200{
201 struct xfs_mount *mp = bp->b_target->bt_mount;
202
203 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
204 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
205 XFS_DIR3_LEAF_CRC_OFF)) ||
206 !xfs_dir3_leaf_verify(bp, magic)) {
207 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
a2ceac1f
DC
208 xfs_buf_ioerror(bp, EFSCORRUPTED);
209 }
210}
211
212static void
65b80c98
DC
213__write_verify(
214 struct xfs_buf *bp,
215 __uint16_t magic)
216{
217 struct xfs_mount *mp = bp->b_target->bt_mount;
218 struct xfs_buf_log_item *bip = bp->b_fspriv;
219 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
220
221 if (!xfs_dir3_leaf_verify(bp, magic)) {
222 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
223 xfs_buf_ioerror(bp, EFSCORRUPTED);
224 return;
225 }
226
227 if (!xfs_sb_version_hascrc(&mp->m_sb))
228 return;
229
230 if (bip)
231 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
232
233 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_LEAF_CRC_OFF);
234}
235
236static void
237xfs_dir3_leaf1_read_verify(
a2ceac1f
DC
238 struct xfs_buf *bp)
239{
65b80c98 240 __read_verify(bp, XFS_DIR2_LEAF1_MAGIC);
a2ceac1f
DC
241}
242
243static void
65b80c98 244xfs_dir3_leaf1_write_verify(
a2ceac1f
DC
245 struct xfs_buf *bp)
246{
65b80c98 247 __write_verify(bp, XFS_DIR2_LEAF1_MAGIC);
a2ceac1f
DC
248}
249
250void
65b80c98 251xfs_dir3_leafn_read_verify(
a2ceac1f
DC
252 struct xfs_buf *bp)
253{
65b80c98 254 __read_verify(bp, XFS_DIR2_LEAFN_MAGIC);
a2ceac1f
DC
255}
256
257void
65b80c98 258xfs_dir3_leafn_write_verify(
a2ceac1f
DC
259 struct xfs_buf *bp)
260{
65b80c98 261 __write_verify(bp, XFS_DIR2_LEAFN_MAGIC);
a2ceac1f
DC
262}
263
65b80c98
DC
264const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
265 .verify_read = xfs_dir3_leaf1_read_verify,
266 .verify_write = xfs_dir3_leaf1_write_verify,
a2ceac1f
DC
267};
268
65b80c98
DC
269const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
270 .verify_read = xfs_dir3_leafn_read_verify,
271 .verify_write = xfs_dir3_leafn_write_verify,
a2ceac1f
DC
272};
273
274static int
65b80c98 275xfs_dir3_leaf_read(
a2ceac1f
DC
276 struct xfs_trans *tp,
277 struct xfs_inode *dp,
278 xfs_dablk_t fbno,
279 xfs_daddr_t mappedbno,
280 struct xfs_buf **bpp)
281{
8b4dc4a9
DC
282 int err;
283
284 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
65b80c98 285 XFS_DATA_FORK, &xfs_dir3_leaf1_buf_ops);
8b4dc4a9 286 if (!err && tp)
bdc16ee5 287 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
8b4dc4a9 288 return err;
a2ceac1f
DC
289}
290
291int
65b80c98 292xfs_dir3_leafn_read(
a2ceac1f
DC
293 struct xfs_trans *tp,
294 struct xfs_inode *dp,
295 xfs_dablk_t fbno,
296 xfs_daddr_t mappedbno,
297 struct xfs_buf **bpp)
298{
8b4dc4a9
DC
299 int err;
300
301 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
65b80c98 302 XFS_DATA_FORK, &xfs_dir3_leafn_buf_ops);
8b4dc4a9 303 if (!err && tp)
bdc16ee5 304 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
8b4dc4a9 305 return err;
65b80c98
DC
306}
307
308/*
309 * Initialize a new leaf block, leaf1 or leafn magic accepted.
310 */
311static void
312xfs_dir3_leaf_init(
313 struct xfs_mount *mp,
8b4dc4a9 314 struct xfs_trans *tp,
65b80c98
DC
315 struct xfs_buf *bp,
316 xfs_ino_t owner,
317 __uint16_t type)
318{
319 struct xfs_dir2_leaf *leaf = bp->b_addr;
320
321 ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
322
323 if (xfs_sb_version_hascrc(&mp->m_sb)) {
324 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
325
326 memset(leaf3, 0, sizeof(*leaf3));
327
328 leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
329 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
330 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
331 leaf3->info.blkno = cpu_to_be64(bp->b_bn);
332 leaf3->info.owner = cpu_to_be64(owner);
333 uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_uuid);
334 } else {
335 memset(leaf, 0, sizeof(*leaf));
336 leaf->hdr.info.magic = cpu_to_be16(type);
337 }
338
339 /*
340 * If it's a leaf-format directory initialize the tail.
341 * Caller is responsible for initialising the bests table.
342 */
343 if (type == XFS_DIR2_LEAF1_MAGIC) {
344 struct xfs_dir2_leaf_tail *ltp;
345
346 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
347 ltp->bestcount = 0;
348 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
bdc16ee5 349 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
8b4dc4a9 350 } else {
65b80c98 351 bp->b_ops = &xfs_dir3_leafn_buf_ops;
bdc16ee5 352 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
8b4dc4a9 353 }
65b80c98
DC
354}
355
356int
357xfs_dir3_leaf_get_buf(
358 xfs_da_args_t *args,
359 xfs_dir2_db_t bno,
360 struct xfs_buf **bpp,
361 __uint16_t magic)
362{
363 struct xfs_inode *dp = args->dp;
364 struct xfs_trans *tp = args->trans;
365 struct xfs_mount *mp = dp->i_mount;
366 struct xfs_buf *bp;
367 int error;
368
369 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
370 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
371 bno < XFS_DIR2_FREE_FIRSTDB(mp));
372
373 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
374 XFS_DATA_FORK);
375 if (error)
376 return error;
377
8b4dc4a9 378 xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
65b80c98
DC
379 xfs_dir3_leaf_log_header(tp, bp);
380 if (magic == XFS_DIR2_LEAF1_MAGIC)
381 xfs_dir3_leaf_log_tail(tp, bp);
382 *bpp = bp;
383 return 0;
a2ceac1f 384}
2bd0ea18
NS
385
386/*
387 * Convert a block form directory to a leaf form directory.
388 */
389int /* error */
390xfs_dir2_block_to_leaf(
391 xfs_da_args_t *args, /* operation arguments */
a2ceac1f 392 struct xfs_buf *dbp) /* input block's buffer */
2bd0ea18 393{
5e656dbb 394 __be16 *bestsp; /* leaf's bestsp entries */
2bd0ea18 395 xfs_dablk_t blkno; /* leaf block's bno */
a2ceac1f 396 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18
NS
397 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
398 xfs_dir2_block_tail_t *btp; /* block's tail */
399 xfs_inode_t *dp; /* incore directory inode */
400 int error; /* error return code */
a2ceac1f 401 struct xfs_buf *lbp; /* leaf block's buffer */
2bd0ea18
NS
402 xfs_dir2_db_t ldb; /* leaf block's bno */
403 xfs_dir2_leaf_t *leaf; /* leaf structure */
404 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
405 xfs_mount_t *mp; /* filesystem mount point */
406 int needlog; /* need to log block header */
407 int needscan; /* need to rescan bestfree */
408 xfs_trans_t *tp; /* transaction pointer */
90ea28c3 409 struct xfs_dir2_data_free *bf;
65b80c98
DC
410 struct xfs_dir2_leaf_entry *ents;
411 struct xfs_dir3_icleaf_hdr leafhdr;
2bd0ea18 412
56b2de80
DC
413 trace_xfs_dir2_block_to_leaf(args);
414
2bd0ea18
NS
415 dp = args->dp;
416 mp = dp->i_mount;
417 tp = args->trans;
418 /*
419 * Add the leaf block to the inode.
420 * This interface will only put blocks in the leaf/node range.
421 * Since that's empty now, we'll get the root (block 0 in range).
422 */
0e266570 423 if ((error = xfs_da_grow_inode(args, &blkno))) {
2bd0ea18
NS
424 return error;
425 }
5e656dbb 426 ldb = xfs_dir2_da_to_db(mp, blkno);
2bd0ea18
NS
427 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
428 /*
429 * Initialize the leaf block, get a buffer for it.
430 */
65b80c98
DC
431 error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
432 if (error)
2bd0ea18 433 return error;
65b80c98 434
a2ceac1f
DC
435 leaf = lbp->b_addr;
436 hdr = dbp->b_addr;
90ea28c3 437 xfs_dir3_data_check(dp, dbp);
a2ceac1f 438 btp = xfs_dir2_block_tail_p(mp, hdr);
5e656dbb 439 blp = xfs_dir2_block_leaf_p(btp);
693fc8f6 440 bf = xfs_dir3_data_bestfree_p(hdr);
65b80c98
DC
441 ents = xfs_dir3_leaf_ents_p(leaf);
442
2bd0ea18
NS
443 /*
444 * Set the counts in the leaf header.
445 */
65b80c98
DC
446 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
447 leafhdr.count = be32_to_cpu(btp->count);
448 leafhdr.stale = be32_to_cpu(btp->stale);
449 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
450 xfs_dir3_leaf_log_header(tp, lbp);
451
2bd0ea18
NS
452 /*
453 * Could compact these but I think we always do the conversion
454 * after squeezing out stale entries.
455 */
65b80c98
DC
456 memcpy(ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
457 xfs_dir3_leaf_log_ents(tp, lbp, 0, leafhdr.count - 1);
2bd0ea18
NS
458 needscan = 0;
459 needlog = 1;
460 /*
461 * Make the space formerly occupied by the leaf entries and block
462 * tail be free.
463 */
464 xfs_dir2_data_make_free(tp, dbp,
a2ceac1f
DC
465 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
466 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
2bd0ea18
NS
467 (char *)blp),
468 &needlog, &needscan);
469 /*
470 * Fix up the block header, make it a data block.
471 */
90ea28c3 472 dbp->b_ops = &xfs_dir3_data_buf_ops;
bdc16ee5 473 xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
90ea28c3
DC
474 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
475 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
476 else
477 hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
478
2bd0ea18 479 if (needscan)
a2ceac1f 480 xfs_dir2_data_freescan(mp, hdr, &needlog);
2bd0ea18
NS
481 /*
482 * Set up leaf tail and bests table.
483 */
5e656dbb
BN
484 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
485 ltp->bestcount = cpu_to_be32(1);
486 bestsp = xfs_dir2_leaf_bests_p(ltp);
693fc8f6 487 bestsp[0] = bf[0].length;
2bd0ea18
NS
488 /*
489 * Log the data header and leaf bests table.
490 */
491 if (needlog)
492 xfs_dir2_data_log_header(tp, dbp);
65b80c98 493 xfs_dir3_leaf_check(mp, lbp);
90ea28c3 494 xfs_dir3_data_check(dp, dbp);
65b80c98 495 xfs_dir3_leaf_log_bests(tp, lbp, 0, 0);
2bd0ea18
NS
496 return 0;
497}
498
a2ceac1f 499STATIC void
65b80c98
DC
500xfs_dir3_leaf_find_stale(
501 struct xfs_dir3_icleaf_hdr *leafhdr,
502 struct xfs_dir2_leaf_entry *ents,
a2ceac1f
DC
503 int index,
504 int *lowstale,
505 int *highstale)
506{
507 /*
508 * Find the first stale entry before our index, if any.
509 */
510 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
65b80c98 511 if (ents[*lowstale].address ==
a2ceac1f
DC
512 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
513 break;
514 }
515
516 /*
517 * Find the first stale entry at or after our index, if any.
518 * Stop if the result would require moving more entries than using
519 * lowstale.
520 */
65b80c98
DC
521 for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
522 if (ents[*highstale].address ==
a2ceac1f
DC
523 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
524 break;
525 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
526 break;
527 }
528}
529
530struct xfs_dir2_leaf_entry *
65b80c98
DC
531xfs_dir3_leaf_find_entry(
532 struct xfs_dir3_icleaf_hdr *leafhdr,
533 struct xfs_dir2_leaf_entry *ents,
a2ceac1f
DC
534 int index, /* leaf table position */
535 int compact, /* need to compact leaves */
536 int lowstale, /* index of prev stale leaf */
537 int highstale, /* index of next stale leaf */
538 int *lfloglow, /* low leaf logging index */
539 int *lfloghigh) /* high leaf logging index */
540{
65b80c98 541 if (!leafhdr->stale) {
a2ceac1f
DC
542 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
543
544 /*
545 * Now we need to make room to insert the leaf entry.
546 *
547 * If there are no stale entries, just insert a hole at index.
548 */
65b80c98
DC
549 lep = &ents[index];
550 if (index < leafhdr->count)
a2ceac1f 551 memmove(lep + 1, lep,
65b80c98 552 (leafhdr->count - index) * sizeof(*lep));
a2ceac1f
DC
553
554 /*
555 * Record low and high logging indices for the leaf.
556 */
557 *lfloglow = index;
65b80c98 558 *lfloghigh = leafhdr->count++;
a2ceac1f
DC
559 return lep;
560 }
561
562 /*
563 * There are stale entries.
564 *
565 * We will use one of them for the new entry. It's probably not at
566 * the right location, so we'll have to shift some up or down first.
567 *
568 * If we didn't compact before, we need to find the nearest stale
569 * entries before and after our insertion point.
570 */
571 if (compact == 0)
65b80c98
DC
572 xfs_dir3_leaf_find_stale(leafhdr, ents, index,
573 &lowstale, &highstale);
a2ceac1f
DC
574
575 /*
576 * If the low one is better, use it.
577 */
578 if (lowstale >= 0 &&
65b80c98 579 (highstale == leafhdr->count ||
a2ceac1f
DC
580 index - lowstale - 1 < highstale - index)) {
581 ASSERT(index - lowstale - 1 >= 0);
65b80c98 582 ASSERT(ents[lowstale].address ==
a2ceac1f
DC
583 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
584
585 /*
586 * Copy entries up to cover the stale entry and make room
587 * for the new entry.
588 */
589 if (index - lowstale - 1 > 0) {
65b80c98 590 memmove(&ents[lowstale], &ents[lowstale + 1],
a2ceac1f 591 (index - lowstale - 1) *
65b80c98 592 sizeof(xfs_dir2_leaf_entry_t));
a2ceac1f
DC
593 }
594 *lfloglow = MIN(lowstale, *lfloglow);
595 *lfloghigh = MAX(index - 1, *lfloghigh);
65b80c98
DC
596 leafhdr->stale--;
597 return &ents[index - 1];
a2ceac1f
DC
598 }
599
600 /*
601 * The high one is better, so use that one.
602 */
603 ASSERT(highstale - index >= 0);
65b80c98 604 ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
a2ceac1f
DC
605
606 /*
607 * Copy entries down to cover the stale entry and make room for the
608 * new entry.
609 */
610 if (highstale - index > 0) {
65b80c98 611 memmove(&ents[index + 1], &ents[index],
a2ceac1f
DC
612 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
613 }
614 *lfloglow = MIN(index, *lfloglow);
615 *lfloghigh = MAX(highstale, *lfloghigh);
65b80c98
DC
616 leafhdr->stale--;
617 return &ents[index];
a2ceac1f
DC
618}
619
2bd0ea18
NS
620/*
621 * Add an entry to a leaf form directory.
622 */
623int /* error */
624xfs_dir2_leaf_addname(
625 xfs_da_args_t *args) /* operation arguments */
626{
5e656dbb 627 __be16 *bestsp; /* freespace table in leaf */
2bd0ea18 628 int compact; /* need to compact leaves */
a2ceac1f
DC
629 xfs_dir2_data_hdr_t *hdr; /* data block header */
630 struct xfs_buf *dbp; /* data block buffer */
2bd0ea18
NS
631 xfs_dir2_data_entry_t *dep; /* data block entry */
632 xfs_inode_t *dp; /* incore directory inode */
633 xfs_dir2_data_unused_t *dup; /* data unused entry */
634 int error; /* error return value */
635 int grown; /* allocated new data block */
636 int highstale; /* index of next stale leaf */
637 int i; /* temporary, index */
638 int index; /* leaf table position */
a2ceac1f 639 struct xfs_buf *lbp; /* leaf's buffer */
2bd0ea18
NS
640 xfs_dir2_leaf_t *leaf; /* leaf structure */
641 int length; /* length of new entry */
642 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
643 int lfloglow; /* low leaf logging index */
644 int lfloghigh; /* high leaf logging index */
645 int lowstale; /* index of prev stale leaf */
646 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
647 xfs_mount_t *mp; /* filesystem mount point */
648 int needbytes; /* leaf block bytes needed */
649 int needlog; /* need to log data header */
650 int needscan; /* need to rescan data free */
5e656dbb 651 __be16 *tagp; /* end of data entry */
2bd0ea18
NS
652 xfs_trans_t *tp; /* transaction pointer */
653 xfs_dir2_db_t use_block; /* data block number */
90ea28c3 654 struct xfs_dir2_data_free *bf; /* bestfree table */
65b80c98
DC
655 struct xfs_dir2_leaf_entry *ents;
656 struct xfs_dir3_icleaf_hdr leafhdr;
2bd0ea18 657
56b2de80
DC
658 trace_xfs_dir2_leaf_addname(args);
659
2bd0ea18
NS
660 dp = args->dp;
661 tp = args->trans;
662 mp = dp->i_mount;
a2ceac1f 663
65b80c98 664 error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
a2ceac1f 665 if (error)
2bd0ea18 666 return error;
a2ceac1f 667
2bd0ea18
NS
668 /*
669 * Look up the entry by hash value and name.
670 * We know it's not there, our caller has already done a lookup.
671 * So the index is of the entry to insert in front of.
672 * But if there are dup hash values the index is of the first of those.
673 */
674 index = xfs_dir2_leaf_search_hash(args, lbp);
a2ceac1f 675 leaf = lbp->b_addr;
5e656dbb 676 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
65b80c98
DC
677 ents = xfs_dir3_leaf_ents_p(leaf);
678 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
5e656dbb
BN
679 bestsp = xfs_dir2_leaf_bests_p(ltp);
680 length = xfs_dir2_data_entsize(args->namelen);
65b80c98 681
2bd0ea18
NS
682 /*
683 * See if there are any entries with the same hash value
684 * and space in their block for the new entry.
685 * This is good because it puts multiple same-hash value entries
686 * in a data block, improving the lookup of those entries.
687 */
65b80c98
DC
688 for (use_block = -1, lep = &ents[index];
689 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
2bd0ea18 690 index++, lep++) {
5e656dbb 691 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
2bd0ea18 692 continue;
5e656dbb
BN
693 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
694 ASSERT(i < be32_to_cpu(ltp->bestcount));
a2ceac1f 695 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
5e656dbb 696 if (be16_to_cpu(bestsp[i]) >= length) {
2bd0ea18
NS
697 use_block = i;
698 break;
699 }
700 }
701 /*
702 * Didn't find a block yet, linear search all the data blocks.
703 */
704 if (use_block == -1) {
5e656dbb 705 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
2bd0ea18
NS
706 /*
707 * Remember a block we see that's missing.
708 */
a2ceac1f
DC
709 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
710 use_block == -1)
2bd0ea18 711 use_block = i;
5e656dbb 712 else if (be16_to_cpu(bestsp[i]) >= length) {
2bd0ea18
NS
713 use_block = i;
714 break;
715 }
716 }
717 }
718 /*
719 * How many bytes do we need in the leaf block?
720 */
a2ceac1f 721 needbytes = 0;
65b80c98 722 if (!leafhdr.stale)
a2ceac1f
DC
723 needbytes += sizeof(xfs_dir2_leaf_entry_t);
724 if (use_block == -1)
725 needbytes += sizeof(xfs_dir2_data_off_t);
726
2bd0ea18
NS
727 /*
728 * Now kill use_block if it refers to a missing block, so we
729 * can use it as an indication of allocation needed.
730 */
a2ceac1f 731 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
2bd0ea18
NS
732 use_block = -1;
733 /*
734 * If we don't have enough free bytes but we can make enough
735 * by compacting out stale entries, we'll do that.
736 */
65b80c98
DC
737 if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
738 leafhdr.stale > 1)
2bd0ea18 739 compact = 1;
65b80c98 740
2bd0ea18
NS
741 /*
742 * Otherwise if we don't have enough free bytes we need to
743 * convert to node form.
744 */
65b80c98 745 else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
2bd0ea18
NS
746 /*
747 * Just checking or no space reservation, give up.
748 */
5e656dbb
BN
749 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
750 args->total == 0) {
a2ceac1f 751 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
752 return XFS_ERROR(ENOSPC);
753 }
754 /*
755 * Convert to node form.
756 */
757 error = xfs_dir2_leaf_to_node(args, lbp);
2bd0ea18
NS
758 if (error)
759 return error;
760 /*
761 * Then add the new entry.
762 */
763 return xfs_dir2_node_addname(args);
764 }
765 /*
766 * Otherwise it will fit without compaction.
767 */
768 else
769 compact = 0;
770 /*
771 * If just checking, then it will fit unless we needed to allocate
772 * a new data block.
773 */
5e656dbb 774 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
a2ceac1f 775 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
776 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
777 }
778 /*
779 * If no allocations are allowed, return now before we've
780 * changed anything.
781 */
782 if (args->total == 0 && use_block == -1) {
a2ceac1f 783 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
784 return XFS_ERROR(ENOSPC);
785 }
786 /*
787 * Need to compact the leaf entries, removing stale ones.
788 * Leave one stale entry behind - the one closest to our
789 * insertion index - and we'll shift that one to our insertion
790 * point later.
791 */
792 if (compact) {
65b80c98
DC
793 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
794 &highstale, &lfloglow, &lfloghigh);
2bd0ea18
NS
795 }
796 /*
797 * There are stale entries, so we'll need log-low and log-high
798 * impossibly bad values later.
799 */
65b80c98
DC
800 else if (leafhdr.stale) {
801 lfloglow = leafhdr.count;
2bd0ea18
NS
802 lfloghigh = -1;
803 }
804 /*
805 * If there was no data block space found, we need to allocate
806 * a new one.
807 */
808 if (use_block == -1) {
2bd0ea18
NS
809 /*
810 * Add the new data block.
811 */
0e266570
NS
812 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
813 &use_block))) {
a2ceac1f 814 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
815 return error;
816 }
817 /*
818 * Initialize the block.
819 */
693fc8f6 820 if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
a2ceac1f 821 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
822 return error;
823 }
824 /*
825 * If we're adding a new data block on the end we need to
826 * extend the bests table. Copy it up one entry.
827 */
5e656dbb 828 if (use_block >= be32_to_cpu(ltp->bestcount)) {
2bd0ea18 829 bestsp--;
32181a02 830 memmove(&bestsp[0], &bestsp[1],
5e656dbb
BN
831 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
832 be32_add_cpu(&ltp->bestcount, 1);
65b80c98
DC
833 xfs_dir3_leaf_log_tail(tp, lbp);
834 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
2bd0ea18
NS
835 }
836 /*
837 * If we're filling in a previously empty block just log it.
838 */
839 else
65b80c98 840 xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
a2ceac1f 841 hdr = dbp->b_addr;
90ea28c3
DC
842 bf = xfs_dir3_data_bestfree_p(hdr);
843 bestsp[use_block] = bf[0].length;
2bd0ea18 844 grown = 1;
a2ceac1f
DC
845 } else {
846 /*
847 * Already had space in some data block.
848 * Just read that one in.
849 */
90ea28c3 850 error = xfs_dir3_data_read(tp, dp,
a2ceac1f
DC
851 xfs_dir2_db_to_da(mp, use_block),
852 -1, &dbp);
853 if (error) {
854 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
855 return error;
856 }
a2ceac1f 857 hdr = dbp->b_addr;
90ea28c3 858 bf = xfs_dir3_data_bestfree_p(hdr);
2bd0ea18
NS
859 grown = 0;
860 }
2bd0ea18
NS
861 /*
862 * Point to the biggest freespace in our data block.
863 */
864 dup = (xfs_dir2_data_unused_t *)
90ea28c3 865 ((char *)hdr + be16_to_cpu(bf[0].offset));
5e656dbb 866 ASSERT(be16_to_cpu(dup->length) >= length);
2bd0ea18
NS
867 needscan = needlog = 0;
868 /*
869 * Mark the initial part of our freespace in use for the new entry.
870 */
871 xfs_dir2_data_use_free(tp, dbp, dup,
a2ceac1f 872 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
2bd0ea18
NS
873 &needlog, &needscan);
874 /*
875 * Initialize our new entry (at last).
876 */
877 dep = (xfs_dir2_data_entry_t *)dup;
5e656dbb 878 dep->inumber = cpu_to_be64(args->inumber);
2bd0ea18 879 dep->namelen = args->namelen;
32181a02 880 memcpy(dep->name, args->name, dep->namelen);
5e656dbb 881 tagp = xfs_dir2_data_entry_tag_p(dep);
a2ceac1f 882 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
2bd0ea18
NS
883 /*
884 * Need to scan fix up the bestfree table.
885 */
886 if (needscan)
a2ceac1f 887 xfs_dir2_data_freescan(mp, hdr, &needlog);
2bd0ea18
NS
888 /*
889 * Need to log the data block's header.
890 */
891 if (needlog)
892 xfs_dir2_data_log_header(tp, dbp);
893 xfs_dir2_data_log_entry(tp, dbp, dep);
894 /*
895 * If the bests table needs to be changed, do it.
896 * Log the change unless we've already done that.
897 */
90ea28c3
DC
898 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
899 bestsp[use_block] = bf[0].length;
2bd0ea18 900 if (!grown)
65b80c98 901 xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
2bd0ea18 902 }
a2ceac1f 903
65b80c98 904 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
a2ceac1f
DC
905 highstale, &lfloglow, &lfloghigh);
906
2bd0ea18
NS
907 /*
908 * Fill in the new leaf entry.
909 */
5e656dbb
BN
910 lep->hashval = cpu_to_be32(args->hashval);
911 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
912 be16_to_cpu(*tagp)));
2bd0ea18
NS
913 /*
914 * Log the leaf fields and give up the buffers.
915 */
65b80c98
DC
916 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
917 xfs_dir3_leaf_log_header(tp, lbp);
918 xfs_dir3_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
919 xfs_dir3_leaf_check(mp, lbp);
90ea28c3 920 xfs_dir3_data_check(dp, dbp);
2bd0ea18
NS
921 return 0;
922}
923
2bd0ea18
NS
924/*
925 * Compact out any stale entries in the leaf.
926 * Log the header and changed leaf entries, if any.
927 */
928void
65b80c98 929xfs_dir3_leaf_compact(
2bd0ea18 930 xfs_da_args_t *args, /* operation arguments */
65b80c98 931 struct xfs_dir3_icleaf_hdr *leafhdr,
a2ceac1f 932 struct xfs_buf *bp) /* leaf buffer */
2bd0ea18
NS
933{
934 int from; /* source leaf index */
dfc130f3 935 xfs_dir2_leaf_t *leaf; /* leaf structure */
2bd0ea18
NS
936 int loglow; /* first leaf entry to log */
937 int to; /* target leaf index */
65b80c98 938 struct xfs_dir2_leaf_entry *ents;
2bd0ea18 939
a2ceac1f 940 leaf = bp->b_addr;
65b80c98 941 if (!leafhdr->stale)
2bd0ea18 942 return;
65b80c98 943
2bd0ea18
NS
944 /*
945 * Compress out the stale entries in place.
946 */
65b80c98
DC
947 ents = xfs_dir3_leaf_ents_p(leaf);
948 for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
949 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
2bd0ea18
NS
950 continue;
951 /*
952 * Only actually copy the entries that are different.
953 */
954 if (from > to) {
955 if (loglow == -1)
956 loglow = to;
65b80c98 957 ents[to] = ents[from];
2bd0ea18
NS
958 }
959 to++;
960 }
961 /*
962 * Update and log the header, log the leaf entries.
963 */
65b80c98
DC
964 ASSERT(leafhdr->stale == from - to);
965 leafhdr->count -= leafhdr->stale;
966 leafhdr->stale = 0;
967
968 xfs_dir3_leaf_hdr_to_disk(leaf, leafhdr);
969 xfs_dir3_leaf_log_header(args->trans, bp);
2bd0ea18 970 if (loglow != -1)
65b80c98 971 xfs_dir3_leaf_log_ents(args->trans, bp, loglow, to - 1);
2bd0ea18
NS
972}
973
974/*
975 * Compact the leaf entries, removing stale ones.
976 * Leave one stale entry behind - the one closest to our
977 * insertion index - and the caller will shift that one to our insertion
978 * point later.
979 * Return new insertion index, where the remaining stale entry is,
980 * and leaf logging indices.
981 */
982void
65b80c98
DC
983xfs_dir3_leaf_compact_x1(
984 struct xfs_dir3_icleaf_hdr *leafhdr,
985 struct xfs_dir2_leaf_entry *ents,
2bd0ea18
NS
986 int *indexp, /* insertion index */
987 int *lowstalep, /* out: stale entry before us */
988 int *highstalep, /* out: stale entry after us */
989 int *lowlogp, /* out: low log index */
990 int *highlogp) /* out: high log index */
991{
992 int from; /* source copy index */
993 int highstale; /* stale entry at/after index */
994 int index; /* insertion index */
995 int keepstale; /* source index of kept stale */
2bd0ea18 996 int lowstale; /* stale entry before index */
0e266570 997 int newindex=0; /* new insertion index */
2bd0ea18
NS
998 int to; /* destination copy index */
999
65b80c98 1000 ASSERT(leafhdr->stale > 1);
2bd0ea18 1001 index = *indexp;
a2ceac1f 1002
65b80c98 1003 xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
a2ceac1f 1004
2bd0ea18
NS
1005 /*
1006 * Pick the better of lowstale and highstale.
1007 */
1008 if (lowstale >= 0 &&
65b80c98 1009 (highstale == leafhdr->count ||
2bd0ea18
NS
1010 index - lowstale <= highstale - index))
1011 keepstale = lowstale;
1012 else
1013 keepstale = highstale;
1014 /*
1015 * Copy the entries in place, removing all the stale entries
1016 * except keepstale.
1017 */
65b80c98 1018 for (from = to = 0; from < leafhdr->count; from++) {
2bd0ea18
NS
1019 /*
1020 * Notice the new value of index.
1021 */
1022 if (index == from)
1023 newindex = to;
1024 if (from != keepstale &&
65b80c98 1025 ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
2bd0ea18
NS
1026 if (from == to)
1027 *lowlogp = to;
1028 continue;
1029 }
1030 /*
1031 * Record the new keepstale value for the insertion.
1032 */
1033 if (from == keepstale)
1034 lowstale = highstale = to;
1035 /*
1036 * Copy only the entries that have moved.
1037 */
1038 if (from > to)
65b80c98 1039 ents[to] = ents[from];
2bd0ea18
NS
1040 to++;
1041 }
1042 ASSERT(from > to);
1043 /*
1044 * If the insertion point was past the last entry,
1045 * set the new insertion point accordingly.
1046 */
1047 if (index == from)
1048 newindex = to;
1049 *indexp = newindex;
1050 /*
1051 * Adjust the leaf header values.
1052 */
65b80c98
DC
1053 leafhdr->count -= from - to;
1054 leafhdr->stale = 1;
2bd0ea18
NS
1055 /*
1056 * Remember the low/high stale value only in the "right"
1057 * direction.
1058 */
1059 if (lowstale >= newindex)
1060 lowstale = -1;
1061 else
65b80c98
DC
1062 highstale = leafhdr->count;
1063 *highlogp = leafhdr->count - 1;
2bd0ea18
NS
1064 *lowstalep = lowstale;
1065 *highstalep = highstale;
1066}
1067
2bd0ea18
NS
1068
1069/*
1070 * Log the bests entries indicated from a leaf1 block.
1071 */
5e656dbb 1072static void
65b80c98 1073xfs_dir3_leaf_log_bests(
2bd0ea18 1074 xfs_trans_t *tp, /* transaction pointer */
a2ceac1f 1075 struct xfs_buf *bp, /* leaf buffer */
2bd0ea18
NS
1076 int first, /* first entry to log */
1077 int last) /* last entry to log */
1078{
5e656dbb
BN
1079 __be16 *firstb; /* pointer to first entry */
1080 __be16 *lastb; /* pointer to last entry */
65b80c98 1081 struct xfs_dir2_leaf *leaf = bp->b_addr;
2bd0ea18
NS
1082 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1083
65b80c98
DC
1084 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1085 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1086
5e656dbb
BN
1087 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1088 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1089 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
a2ceac1f 1090 xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
2bd0ea18
NS
1091 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1092}
1093
1094/*
1095 * Log the leaf entries indicated from a leaf1 or leafn block.
1096 */
1097void
65b80c98 1098xfs_dir3_leaf_log_ents(
2bd0ea18 1099 xfs_trans_t *tp, /* transaction pointer */
a2ceac1f 1100 struct xfs_buf *bp, /* leaf buffer */
2bd0ea18
NS
1101 int first, /* first entry to log */
1102 int last) /* last entry to log */
1103{
1104 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1105 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
65b80c98
DC
1106 struct xfs_dir2_leaf *leaf = bp->b_addr;
1107 struct xfs_dir2_leaf_entry *ents;
2bd0ea18 1108
a2ceac1f 1109 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
65b80c98
DC
1110 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1111 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1112 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1113
1114 ents = xfs_dir3_leaf_ents_p(leaf);
1115 firstlep = &ents[first];
1116 lastlep = &ents[last];
a2ceac1f 1117 xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
2bd0ea18
NS
1118 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1119}
1120
1121/*
1122 * Log the header of the leaf1 or leafn block.
1123 */
1124void
65b80c98 1125xfs_dir3_leaf_log_header(
a2ceac1f
DC
1126 struct xfs_trans *tp,
1127 struct xfs_buf *bp)
2bd0ea18 1128{
65b80c98 1129 struct xfs_dir2_leaf *leaf = bp->b_addr;
2bd0ea18 1130
a2ceac1f 1131 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
65b80c98
DC
1132 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1133 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1134 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1135
a2ceac1f 1136 xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
65b80c98 1137 xfs_dir3_leaf_hdr_size(leaf) - 1);
2bd0ea18
NS
1138}
1139
1140/*
1141 * Log the tail of the leaf1 block.
1142 */
5e656dbb 1143STATIC void
65b80c98 1144xfs_dir3_leaf_log_tail(
a2ceac1f
DC
1145 struct xfs_trans *tp,
1146 struct xfs_buf *bp)
2bd0ea18 1147{
65b80c98 1148 struct xfs_dir2_leaf *leaf = bp->b_addr;
2bd0ea18 1149 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
65b80c98
DC
1150 struct xfs_mount *mp = tp->t_mountp;
1151
1152 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1153 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1154 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1155 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
2bd0ea18 1156
5e656dbb 1157 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
a2ceac1f 1158 xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
2bd0ea18
NS
1159 (uint)(mp->m_dirblksize - 1));
1160}
1161
1162/*
1163 * Look up the entry referred to by args in the leaf format directory.
1164 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1165 * is also used by the node-format code.
1166 */
1167int
1168xfs_dir2_leaf_lookup(
1169 xfs_da_args_t *args) /* operation arguments */
1170{
a2ceac1f 1171 struct xfs_buf *dbp; /* data block buffer */
2bd0ea18
NS
1172 xfs_dir2_data_entry_t *dep; /* data block entry */
1173 xfs_inode_t *dp; /* incore directory inode */
1174 int error; /* error return code */
1175 int index; /* found entry index */
a2ceac1f 1176 struct xfs_buf *lbp; /* leaf buffer */
2bd0ea18
NS
1177 xfs_dir2_leaf_t *leaf; /* leaf structure */
1178 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1179 xfs_trans_t *tp; /* transaction pointer */
65b80c98 1180 struct xfs_dir2_leaf_entry *ents;
2bd0ea18 1181
56b2de80
DC
1182 trace_xfs_dir2_leaf_lookup(args);
1183
2bd0ea18
NS
1184 /*
1185 * Look up name in the leaf block, returning both buffers and index.
1186 */
0e266570 1187 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
2bd0ea18
NS
1188 return error;
1189 }
1190 tp = args->trans;
1191 dp = args->dp;
65b80c98 1192 xfs_dir3_leaf_check(dp->i_mount, lbp);
a2ceac1f 1193 leaf = lbp->b_addr;
65b80c98 1194 ents = xfs_dir3_leaf_ents_p(leaf);
2bd0ea18
NS
1195 /*
1196 * Get to the leaf entry and contained data entry address.
1197 */
65b80c98
DC
1198 lep = &ents[index];
1199
2bd0ea18
NS
1200 /*
1201 * Point to the data entry.
1202 */
1203 dep = (xfs_dir2_data_entry_t *)
a2ceac1f 1204 ((char *)dbp->b_addr +
5e656dbb 1205 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
2bd0ea18 1206 /*
5e656dbb 1207 * Return the found inode number & CI name if appropriate
2bd0ea18 1208 */
5e656dbb
BN
1209 args->inumber = be64_to_cpu(dep->inumber);
1210 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
a2ceac1f
DC
1211 xfs_trans_brelse(tp, dbp);
1212 xfs_trans_brelse(tp, lbp);
5e656dbb 1213 return XFS_ERROR(error);
2bd0ea18
NS
1214}
1215
1216/*
1217 * Look up name/hash in the leaf block.
1218 * Fill in indexp with the found index, and dbpp with the data buffer.
1219 * If not found dbpp will be NULL, and ENOENT comes back.
1220 * lbpp will always be filled in with the leaf buffer unless there's an error.
1221 */
5e656dbb 1222static int /* error */
2bd0ea18
NS
1223xfs_dir2_leaf_lookup_int(
1224 xfs_da_args_t *args, /* operation arguments */
a2ceac1f 1225 struct xfs_buf **lbpp, /* out: leaf buffer */
2bd0ea18 1226 int *indexp, /* out: index in leaf block */
a2ceac1f 1227 struct xfs_buf **dbpp) /* out: data buffer */
2bd0ea18 1228{
5e656dbb 1229 xfs_dir2_db_t curdb = -1; /* current data block number */
a2ceac1f 1230 struct xfs_buf *dbp = NULL; /* data buffer */
2bd0ea18
NS
1231 xfs_dir2_data_entry_t *dep; /* data entry */
1232 xfs_inode_t *dp; /* incore directory inode */
1233 int error; /* error return code */
1234 int index; /* index in leaf block */
a2ceac1f 1235 struct xfs_buf *lbp; /* leaf buffer */
2bd0ea18
NS
1236 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1237 xfs_dir2_leaf_t *leaf; /* leaf structure */
1238 xfs_mount_t *mp; /* filesystem mount point */
1239 xfs_dir2_db_t newdb; /* new data block number */
1240 xfs_trans_t *tp; /* transaction pointer */
5e656dbb
BN
1241 xfs_dir2_db_t cidb = -1; /* case match data block no. */
1242 enum xfs_dacmp cmp; /* name compare result */
65b80c98
DC
1243 struct xfs_dir2_leaf_entry *ents;
1244 struct xfs_dir3_icleaf_hdr leafhdr;
2bd0ea18
NS
1245
1246 dp = args->dp;
1247 tp = args->trans;
1248 mp = dp->i_mount;
a2ceac1f 1249
65b80c98 1250 error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
5e656dbb 1251 if (error)
2bd0ea18 1252 return error;
a2ceac1f 1253
2bd0ea18 1254 *lbpp = lbp;
a2ceac1f 1255 leaf = lbp->b_addr;
65b80c98
DC
1256 xfs_dir3_leaf_check(mp, lbp);
1257 ents = xfs_dir3_leaf_ents_p(leaf);
1258 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1259
2bd0ea18
NS
1260 /*
1261 * Look for the first leaf entry with our hash value.
1262 */
1263 index = xfs_dir2_leaf_search_hash(args, lbp);
1264 /*
1265 * Loop over all the entries with the right hash value
1266 * looking to match the name.
1267 */
65b80c98
DC
1268 for (lep = &ents[index];
1269 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
1270 lep++, index++) {
2bd0ea18
NS
1271 /*
1272 * Skip over stale leaf entries.
1273 */
5e656dbb 1274 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
2bd0ea18
NS
1275 continue;
1276 /*
1277 * Get the new data block number.
1278 */
5e656dbb 1279 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
2bd0ea18
NS
1280 /*
1281 * If it's not the same as the old data block number,
1282 * need to pitch the old one and read the new one.
1283 */
1284 if (newdb != curdb) {
1285 if (dbp)
a2ceac1f 1286 xfs_trans_brelse(tp, dbp);
90ea28c3 1287 error = xfs_dir3_data_read(tp, dp,
a2ceac1f
DC
1288 xfs_dir2_db_to_da(mp, newdb),
1289 -1, &dbp);
5e656dbb 1290 if (error) {
a2ceac1f 1291 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
1292 return error;
1293 }
2bd0ea18
NS
1294 curdb = newdb;
1295 }
1296 /*
1297 * Point to the data entry.
1298 */
a2ceac1f 1299 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
5e656dbb 1300 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
2bd0ea18 1301 /*
5e656dbb
BN
1302 * Compare name and if it's an exact match, return the index
1303 * and buffer. If it's the first case-insensitive match, store
1304 * the index and buffer and continue looking for an exact match.
2bd0ea18 1305 */
5e656dbb
BN
1306 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1307 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1308 args->cmpresult = cmp;
2bd0ea18 1309 *indexp = index;
5e656dbb
BN
1310 /* case exact match: return the current buffer. */
1311 if (cmp == XFS_CMP_EXACT) {
1312 *dbpp = dbp;
1313 return 0;
1314 }
1315 cidb = curdb;
2bd0ea18
NS
1316 }
1317 }
5e656dbb
BN
1318 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1319 /*
1320 * Here, we can only be doing a lookup (not a rename or remove).
1321 * If a case-insensitive match was found earlier, re-read the
1322 * appropriate data block if required and return it.
1323 */
1324 if (args->cmpresult == XFS_CMP_CASE) {
1325 ASSERT(cidb != -1);
1326 if (cidb != curdb) {
a2ceac1f 1327 xfs_trans_brelse(tp, dbp);
90ea28c3 1328 error = xfs_dir3_data_read(tp, dp,
a2ceac1f
DC
1329 xfs_dir2_db_to_da(mp, cidb),
1330 -1, &dbp);
5e656dbb 1331 if (error) {
a2ceac1f 1332 xfs_trans_brelse(tp, lbp);
5e656dbb
BN
1333 return error;
1334 }
1335 }
1336 *dbpp = dbp;
1337 return 0;
1338 }
2bd0ea18
NS
1339 /*
1340 * No match found, return ENOENT.
1341 */
5e656dbb 1342 ASSERT(cidb == -1);
2bd0ea18 1343 if (dbp)
a2ceac1f
DC
1344 xfs_trans_brelse(tp, dbp);
1345 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
1346 return XFS_ERROR(ENOENT);
1347}
1348
1349/*
1350 * Remove an entry from a leaf format directory.
1351 */
1352int /* error */
1353xfs_dir2_leaf_removename(
1354 xfs_da_args_t *args) /* operation arguments */
1355{
5e656dbb 1356 __be16 *bestsp; /* leaf block best freespace */
a2ceac1f 1357 xfs_dir2_data_hdr_t *hdr; /* data block header */
2bd0ea18 1358 xfs_dir2_db_t db; /* data block number */
a2ceac1f 1359 struct xfs_buf *dbp; /* data block buffer */
2bd0ea18
NS
1360 xfs_dir2_data_entry_t *dep; /* data entry structure */
1361 xfs_inode_t *dp; /* incore directory inode */
1362 int error; /* error return code */
1363 xfs_dir2_db_t i; /* temporary data block # */
1364 int index; /* index into leaf entries */
a2ceac1f 1365 struct xfs_buf *lbp; /* leaf buffer */
2bd0ea18
NS
1366 xfs_dir2_leaf_t *leaf; /* leaf structure */
1367 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1368 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1369 xfs_mount_t *mp; /* filesystem mount point */
1370 int needlog; /* need to log data header */
1371 int needscan; /* need to rescan data frees */
1372 xfs_dir2_data_off_t oldbest; /* old value of best free */
1373 xfs_trans_t *tp; /* transaction pointer */
90ea28c3 1374 struct xfs_dir2_data_free *bf; /* bestfree table */
65b80c98
DC
1375 struct xfs_dir2_leaf_entry *ents;
1376 struct xfs_dir3_icleaf_hdr leafhdr;
2bd0ea18 1377
56b2de80
DC
1378 trace_xfs_dir2_leaf_removename(args);
1379
2bd0ea18
NS
1380 /*
1381 * Lookup the leaf entry, get the leaf and data blocks read in.
1382 */
0e266570 1383 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
2bd0ea18
NS
1384 return error;
1385 }
1386 dp = args->dp;
1387 tp = args->trans;
1388 mp = dp->i_mount;
a2ceac1f
DC
1389 leaf = lbp->b_addr;
1390 hdr = dbp->b_addr;
90ea28c3 1391 xfs_dir3_data_check(dp, dbp);
65b80c98
DC
1392 bf = xfs_dir3_data_bestfree_p(hdr);
1393 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1394 ents = xfs_dir3_leaf_ents_p(leaf);
2bd0ea18
NS
1395 /*
1396 * Point to the leaf entry, use that to point to the data entry.
1397 */
65b80c98 1398 lep = &ents[index];
5e656dbb 1399 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
2bd0ea18 1400 dep = (xfs_dir2_data_entry_t *)
a2ceac1f 1401 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
2bd0ea18 1402 needscan = needlog = 0;
90ea28c3 1403 oldbest = be16_to_cpu(bf[0].length);
5e656dbb
BN
1404 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1405 bestsp = xfs_dir2_leaf_bests_p(ltp);
1406 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
2bd0ea18
NS
1407 /*
1408 * Mark the former data entry unused.
1409 */
1410 xfs_dir2_data_make_free(tp, dbp,
a2ceac1f 1411 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
5e656dbb 1412 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
2bd0ea18
NS
1413 /*
1414 * We just mark the leaf entry stale by putting a null in it.
1415 */
65b80c98
DC
1416 leafhdr.stale++;
1417 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
1418 xfs_dir3_leaf_log_header(tp, lbp);
1419
5e656dbb 1420 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
65b80c98
DC
1421 xfs_dir3_leaf_log_ents(tp, lbp, index, index);
1422
2bd0ea18
NS
1423 /*
1424 * Scan the freespace in the data block again if necessary,
1425 * log the data block header if necessary.
1426 */
1427 if (needscan)
a2ceac1f 1428 xfs_dir2_data_freescan(mp, hdr, &needlog);
2bd0ea18
NS
1429 if (needlog)
1430 xfs_dir2_data_log_header(tp, dbp);
1431 /*
1432 * If the longest freespace in the data block has changed,
1433 * put the new value in the bests table and log that.
1434 */
90ea28c3
DC
1435 if (be16_to_cpu(bf[0].length) != oldbest) {
1436 bestsp[db] = bf[0].length;
65b80c98 1437 xfs_dir3_leaf_log_bests(tp, lbp, db, db);
2bd0ea18 1438 }
90ea28c3 1439 xfs_dir3_data_check(dp, dbp);
2bd0ea18
NS
1440 /*
1441 * If the data block is now empty then get rid of the data block.
1442 */
90ea28c3
DC
1443 if (be16_to_cpu(bf[0].length) ==
1444 mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr)) {
2bd0ea18 1445 ASSERT(db != mp->m_dirdatablk);
0e266570 1446 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
2bd0ea18
NS
1447 /*
1448 * Nope, can't get rid of it because it caused
1449 * allocation of a bmap btree block to do so.
1450 * Just go on, returning success, leaving the
1451 * empty block in place.
1452 */
a2ceac1f 1453 if (error == ENOSPC && args->total == 0)
2bd0ea18 1454 error = 0;
65b80c98 1455 xfs_dir3_leaf_check(mp, lbp);
2bd0ea18
NS
1456 return error;
1457 }
1458 dbp = NULL;
1459 /*
1460 * If this is the last data block then compact the
1461 * bests table by getting rid of entries.
1462 */
5e656dbb 1463 if (db == be32_to_cpu(ltp->bestcount) - 1) {
2bd0ea18
NS
1464 /*
1465 * Look for the last active entry (i).
1466 */
1467 for (i = db - 1; i > 0; i--) {
a2ceac1f 1468 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
2bd0ea18
NS
1469 break;
1470 }
1471 /*
1472 * Copy the table down so inactive entries at the
1473 * end are removed.
1474 */
32181a02 1475 memmove(&bestsp[db - i], bestsp,
5e656dbb
BN
1476 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1477 be32_add_cpu(&ltp->bestcount, -(db - i));
65b80c98
DC
1478 xfs_dir3_leaf_log_tail(tp, lbp);
1479 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
2bd0ea18 1480 } else
5e656dbb 1481 bestsp[db] = cpu_to_be16(NULLDATAOFF);
2bd0ea18
NS
1482 }
1483 /*
1484 * If the data block was not the first one, drop it.
1485 */
a2ceac1f 1486 else if (db != mp->m_dirdatablk)
2bd0ea18 1487 dbp = NULL;
a2ceac1f 1488
65b80c98 1489 xfs_dir3_leaf_check(mp, lbp);
2bd0ea18
NS
1490 /*
1491 * See if we can convert to block form.
1492 */
1493 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1494}
1495
1496/*
1497 * Replace the inode number in a leaf format directory entry.
1498 */
1499int /* error */
1500xfs_dir2_leaf_replace(
1501 xfs_da_args_t *args) /* operation arguments */
1502{
a2ceac1f 1503 struct xfs_buf *dbp; /* data block buffer */
2bd0ea18
NS
1504 xfs_dir2_data_entry_t *dep; /* data block entry */
1505 xfs_inode_t *dp; /* incore directory inode */
1506 int error; /* error return code */
1507 int index; /* index of leaf entry */
a2ceac1f 1508 struct xfs_buf *lbp; /* leaf buffer */
2bd0ea18
NS
1509 xfs_dir2_leaf_t *leaf; /* leaf structure */
1510 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1511 xfs_trans_t *tp; /* transaction pointer */
65b80c98 1512 struct xfs_dir2_leaf_entry *ents;
2bd0ea18 1513
56b2de80
DC
1514 trace_xfs_dir2_leaf_replace(args);
1515
2bd0ea18
NS
1516 /*
1517 * Look up the entry.
1518 */
0e266570 1519 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
2bd0ea18
NS
1520 return error;
1521 }
1522 dp = args->dp;
a2ceac1f 1523 leaf = lbp->b_addr;
65b80c98 1524 ents = xfs_dir3_leaf_ents_p(leaf);
2bd0ea18
NS
1525 /*
1526 * Point to the leaf entry, get data address from it.
1527 */
65b80c98 1528 lep = &ents[index];
2bd0ea18
NS
1529 /*
1530 * Point to the data entry.
1531 */
1532 dep = (xfs_dir2_data_entry_t *)
a2ceac1f 1533 ((char *)dbp->b_addr +
5e656dbb
BN
1534 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1535 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
2bd0ea18
NS
1536 /*
1537 * Put the new inode number in, log it.
1538 */
5e656dbb 1539 dep->inumber = cpu_to_be64(args->inumber);
2bd0ea18
NS
1540 tp = args->trans;
1541 xfs_dir2_data_log_entry(tp, dbp, dep);
65b80c98 1542 xfs_dir3_leaf_check(dp->i_mount, lbp);
a2ceac1f 1543 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
1544 return 0;
1545}
1546
1547/*
1548 * Return index in the leaf block (lbp) which is either the first
1549 * one with this hash value, or if there are none, the insert point
1550 * for that hash value.
1551 */
1552int /* index value */
1553xfs_dir2_leaf_search_hash(
1554 xfs_da_args_t *args, /* operation arguments */
a2ceac1f 1555 struct xfs_buf *lbp) /* leaf buffer */
2bd0ea18 1556{
0e266570 1557 xfs_dahash_t hash=0; /* hash from this entry */
2bd0ea18
NS
1558 xfs_dahash_t hashwant; /* hash value looking for */
1559 int high; /* high leaf index */
1560 int low; /* low leaf index */
1561 xfs_dir2_leaf_t *leaf; /* leaf structure */
1562 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
0e266570 1563 int mid=0; /* current leaf index */
65b80c98
DC
1564 struct xfs_dir2_leaf_entry *ents;
1565 struct xfs_dir3_icleaf_hdr leafhdr;
2bd0ea18 1566
a2ceac1f 1567 leaf = lbp->b_addr;
65b80c98
DC
1568 ents = xfs_dir3_leaf_ents_p(leaf);
1569 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1570
2bd0ea18 1571#ifndef __KERNEL__
65b80c98 1572 if (!leafhdr.count)
2bd0ea18
NS
1573 return 0;
1574#endif
1575 /*
1576 * Note, the table cannot be empty, so we have to go through the loop.
1577 * Binary search the leaf entries looking for our hash value.
1578 */
65b80c98 1579 for (lep = ents, low = 0, high = leafhdr.count - 1,
2bd0ea18
NS
1580 hashwant = args->hashval;
1581 low <= high; ) {
1582 mid = (low + high) >> 1;
5e656dbb 1583 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
2bd0ea18
NS
1584 break;
1585 if (hash < hashwant)
1586 low = mid + 1;
1587 else
1588 high = mid - 1;
1589 }
1590 /*
1591 * Found one, back up through all the equal hash values.
1592 */
1593 if (hash == hashwant) {
5e656dbb 1594 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
2bd0ea18
NS
1595 mid--;
1596 }
1597 }
1598 /*
1599 * Need to point to an entry higher than ours.
1600 */
1601 else if (hash < hashwant)
1602 mid++;
1603 return mid;
1604}
1605
1606/*
1607 * Trim off a trailing data block. We know it's empty since the leaf
1608 * freespace table says so.
1609 */
1610int /* error */
1611xfs_dir2_leaf_trim_data(
1612 xfs_da_args_t *args, /* operation arguments */
a2ceac1f 1613 struct xfs_buf *lbp, /* leaf buffer */
2bd0ea18
NS
1614 xfs_dir2_db_t db) /* data block number */
1615{
5e656dbb 1616 __be16 *bestsp; /* leaf bests table */
a2ceac1f 1617 struct xfs_buf *dbp; /* data block buffer */
2bd0ea18
NS
1618 xfs_inode_t *dp; /* incore directory inode */
1619 int error; /* error return value */
1620 xfs_dir2_leaf_t *leaf; /* leaf structure */
1621 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1622 xfs_mount_t *mp; /* filesystem mount point */
1623 xfs_trans_t *tp; /* transaction pointer */
1624
1625 dp = args->dp;
1626 mp = dp->i_mount;
1627 tp = args->trans;
1628 /*
1629 * Read the offending data block. We need its buffer.
1630 */
90ea28c3 1631 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
a2ceac1f 1632 if (error)
2bd0ea18 1633 return error;
2bd0ea18 1634
a2ceac1f 1635 leaf = lbp->b_addr;
5e656dbb 1636 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
a2ceac1f
DC
1637
1638#ifdef DEBUG
1639{
1640 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
90ea28c3 1641 struct xfs_dir2_data_free *bf = xfs_dir3_data_bestfree_p(hdr);
a2ceac1f 1642
90ea28c3
DC
1643 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1644 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1645 ASSERT(be16_to_cpu(bf[0].length) ==
1646 mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr));
5e656dbb 1647 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
a2ceac1f
DC
1648}
1649#endif
1650
2bd0ea18
NS
1651 /*
1652 * Get rid of the data block.
1653 */
0e266570 1654 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
2bd0ea18 1655 ASSERT(error != ENOSPC);
a2ceac1f 1656 xfs_trans_brelse(tp, dbp);
2bd0ea18
NS
1657 return error;
1658 }
1659 /*
1660 * Eliminate the last bests entry from the table.
1661 */
5e656dbb
BN
1662 bestsp = xfs_dir2_leaf_bests_p(ltp);
1663 be32_add_cpu(&ltp->bestcount, -1);
1664 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
65b80c98
DC
1665 xfs_dir3_leaf_log_tail(tp, lbp);
1666 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
2bd0ea18
NS
1667 return 0;
1668}
1669
a2ceac1f 1670static inline size_t
65b80c98
DC
1671xfs_dir3_leaf_size(
1672 struct xfs_dir3_icleaf_hdr *hdr,
a2ceac1f
DC
1673 int counts)
1674{
65b80c98
DC
1675 int entries;
1676 int hdrsize;
1677
1678 entries = hdr->count - hdr->stale;
1679 if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
1680 hdr->magic == XFS_DIR2_LEAFN_MAGIC)
1681 hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
1682 else
1683 hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
a2ceac1f 1684
65b80c98
DC
1685 return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
1686 + counts * sizeof(xfs_dir2_data_off_t)
1687 + sizeof(xfs_dir2_leaf_tail_t);
a2ceac1f
DC
1688}
1689
2bd0ea18
NS
1690/*
1691 * Convert node form directory to leaf form directory.
1692 * The root of the node form dir needs to already be a LEAFN block.
1693 * Just return if we can't do anything.
1694 */
1695int /* error */
1696xfs_dir2_node_to_leaf(
1697 xfs_da_state_t *state) /* directory operation state */
1698{
1699 xfs_da_args_t *args; /* operation arguments */
1700 xfs_inode_t *dp; /* incore directory inode */
1701 int error; /* error return code */
a2ceac1f 1702 struct xfs_buf *fbp; /* buffer for freespace block */
2bd0ea18
NS
1703 xfs_fileoff_t fo; /* freespace file offset */
1704 xfs_dir2_free_t *free; /* freespace structure */
a2ceac1f 1705 struct xfs_buf *lbp; /* buffer for leaf block */
2bd0ea18
NS
1706 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1707 xfs_dir2_leaf_t *leaf; /* leaf structure */
1708 xfs_mount_t *mp; /* filesystem mount point */
1709 int rval; /* successful free trim? */
1710 xfs_trans_t *tp; /* transaction pointer */
65b80c98 1711 struct xfs_dir3_icleaf_hdr leafhdr;
3303b09f 1712 struct xfs_dir3_icfree_hdr freehdr;
2bd0ea18
NS
1713
1714 /*
1715 * There's more than a leaf level in the btree, so there must
1716 * be multiple leafn blocks. Give up.
1717 */
1718 if (state->path.active > 1)
1719 return 0;
1720 args = state->args;
56b2de80
DC
1721
1722 trace_xfs_dir2_node_to_leaf(args);
1723
2bd0ea18
NS
1724 mp = state->mp;
1725 dp = args->dp;
1726 tp = args->trans;
1727 /*
1728 * Get the last offset in the file.
1729 */
0e266570 1730 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
2bd0ea18
NS
1731 return error;
1732 }
1733 fo -= mp->m_dirblkfsbs;
1734 /*
1735 * If there are freespace blocks other than the first one,
1736 * take this opportunity to remove trailing empty freespace blocks
1737 * that may have been left behind during no-space-reservation
1738 * operations.
1739 */
1740 while (fo > mp->m_dirfreeblk) {
0e266570 1741 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
2bd0ea18
NS
1742 return error;
1743 }
1744 if (rval)
1745 fo -= mp->m_dirblkfsbs;
1746 else
1747 return 0;
1748 }
1749 /*
1750 * Now find the block just before the freespace block.
1751 */
0e266570 1752 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
2bd0ea18
NS
1753 return error;
1754 }
1755 /*
1756 * If it's not the single leaf block, give up.
1757 */
1758 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1759 return 0;
1760 lbp = state->path.blk[0].bp;
a2ceac1f 1761 leaf = lbp->b_addr;
65b80c98
DC
1762 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1763
1764 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
1765 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
1766
2bd0ea18
NS
1767 /*
1768 * Read the freespace block.
1769 */
a2ceac1f
DC
1770 error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
1771 if (error)
2bd0ea18 1772 return error;
a2ceac1f 1773 free = fbp->b_addr;
3303b09f
DC
1774 xfs_dir3_free_hdr_from_disk(&freehdr, free);
1775
1776 ASSERT(!freehdr.firstdb);
a2ceac1f 1777
2bd0ea18
NS
1778 /*
1779 * Now see if the leafn and free data will fit in a leaf1.
1780 * If not, release the buffer and give up.
1781 */
65b80c98 1782 if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > mp->m_dirblksize) {
a2ceac1f 1783 xfs_trans_brelse(tp, fbp);
2bd0ea18
NS
1784 return 0;
1785 }
a2ceac1f 1786
2bd0ea18
NS
1787 /*
1788 * If the leaf has any stale entries in it, compress them out.
2bd0ea18 1789 */
65b80c98
DC
1790 if (leafhdr.stale)
1791 xfs_dir3_leaf_compact(args, &leafhdr, lbp);
a2ceac1f 1792
65b80c98 1793 lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
bdc16ee5 1794 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
65b80c98
DC
1795 leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
1796 ? XFS_DIR2_LEAF1_MAGIC
1797 : XFS_DIR3_LEAF1_MAGIC;
a2ceac1f 1798
2bd0ea18
NS
1799 /*
1800 * Set up the leaf tail from the freespace block.
1801 */
5e656dbb 1802 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
3303b09f 1803 ltp->bestcount = cpu_to_be32(freehdr.nvalid);
65b80c98 1804
2bd0ea18
NS
1805 /*
1806 * Set up the leaf bests table.
1807 */
3303b09f
DC
1808 memcpy(xfs_dir2_leaf_bests_p(ltp), xfs_dir3_free_bests_p(mp, free),
1809 freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
65b80c98
DC
1810
1811 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
1812 xfs_dir3_leaf_log_header(tp, lbp);
1813 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1814 xfs_dir3_leaf_log_tail(tp, lbp);
1815 xfs_dir3_leaf_check(mp, lbp);
1816
2bd0ea18
NS
1817 /*
1818 * Get rid of the freespace block.
1819 */
1820 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1821 if (error) {
2bd0ea18
NS
1822 /*
1823 * This can't fail here because it can only happen when
1824 * punching out the middle of an extent, and this is an
1825 * isolated block.
1826 */
1827 ASSERT(error != ENOSPC);
1828 return error;
1829 }
1830 fbp = NULL;
1831 /*
1832 * Now see if we can convert the single-leaf directory
1833 * down to a block form directory.
1834 * This routine always kills the dabuf for the leaf, so
1835 * eliminate it from the path.
1836 */
1837 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1838 state->path.blk[0].bp = NULL;
1839 return error;
1840}