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