xfs: factor dir2 block read operations
[deliverable/linux.git] / fs / xfs / xfs_dir2_block.c
1 /*
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_log.h"
22 #include "xfs_trans.h"
23 #include "xfs_sb.h"
24 #include "xfs_ag.h"
25 #include "xfs_mount.h"
26 #include "xfs_da_btree.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_dinode.h"
29 #include "xfs_inode.h"
30 #include "xfs_inode_item.h"
31 #include "xfs_dir2.h"
32 #include "xfs_dir2_format.h"
33 #include "xfs_dir2_priv.h"
34 #include "xfs_error.h"
35 #include "xfs_trace.h"
36
37 /*
38 * Local function prototypes.
39 */
40 static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp,
41 int first, int last);
42 static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp);
43 static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp,
44 int *entno);
45 static int xfs_dir2_block_sort(const void *a, const void *b);
46
47 static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
48
49 /*
50 * One-time startup routine called from xfs_init().
51 */
52 void
53 xfs_dir_startup(void)
54 {
55 xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1);
56 xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2);
57 }
58
59 static int
60 xfs_dir2_block_read(
61 struct xfs_trans *tp,
62 struct xfs_inode *dp,
63 struct xfs_buf **bpp)
64 {
65 struct xfs_mount *mp = dp->i_mount;
66
67 return xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, bpp,
68 XFS_DATA_FORK, NULL);
69 }
70
71 static void
72 xfs_dir2_block_need_space(
73 struct xfs_dir2_data_hdr *hdr,
74 struct xfs_dir2_block_tail *btp,
75 struct xfs_dir2_leaf_entry *blp,
76 __be16 **tagpp,
77 struct xfs_dir2_data_unused **dupp,
78 struct xfs_dir2_data_unused **enddupp,
79 int *compact,
80 int len)
81 {
82 struct xfs_dir2_data_free *bf;
83 __be16 *tagp = NULL;
84 struct xfs_dir2_data_unused *dup = NULL;
85 struct xfs_dir2_data_unused *enddup = NULL;
86
87 *compact = 0;
88 bf = hdr->bestfree;
89
90 /*
91 * If there are stale entries we'll use one for the leaf.
92 */
93 if (btp->stale) {
94 if (be16_to_cpu(bf[0].length) >= len) {
95 /*
96 * The biggest entry enough to avoid compaction.
97 */
98 dup = (xfs_dir2_data_unused_t *)
99 ((char *)hdr + be16_to_cpu(bf[0].offset));
100 goto out;
101 }
102
103 /*
104 * Will need to compact to make this work.
105 * Tag just before the first leaf entry.
106 */
107 *compact = 1;
108 tagp = (__be16 *)blp - 1;
109
110 /* Data object just before the first leaf entry. */
111 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
112
113 /*
114 * If it's not free then the data will go where the
115 * leaf data starts now, if it works at all.
116 */
117 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
118 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
119 (uint)sizeof(*blp) < len)
120 dup = NULL;
121 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
122 dup = NULL;
123 else
124 dup = (xfs_dir2_data_unused_t *)blp;
125 goto out;
126 }
127
128 /*
129 * no stale entries, so just use free space.
130 * Tag just before the first leaf entry.
131 */
132 tagp = (__be16 *)blp - 1;
133
134 /* Data object just before the first leaf entry. */
135 enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
136
137 /*
138 * If it's not free then can't do this add without cleaning up:
139 * the space before the first leaf entry needs to be free so it
140 * can be expanded to hold the pointer to the new entry.
141 */
142 if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
143 /*
144 * Check out the biggest freespace and see if it's the same one.
145 */
146 dup = (xfs_dir2_data_unused_t *)
147 ((char *)hdr + be16_to_cpu(bf[0].offset));
148 if (dup != enddup) {
149 /*
150 * Not the same free entry, just check its length.
151 */
152 if (be16_to_cpu(dup->length) < len)
153 dup = NULL;
154 goto out;
155 }
156
157 /*
158 * It is the biggest freespace, can it hold the leaf too?
159 */
160 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
161 /*
162 * Yes, use the second-largest entry instead if it works.
163 */
164 if (be16_to_cpu(bf[1].length) >= len)
165 dup = (xfs_dir2_data_unused_t *)
166 ((char *)hdr + be16_to_cpu(bf[1].offset));
167 else
168 dup = NULL;
169 }
170 }
171 out:
172 *tagpp = tagp;
173 *dupp = dup;
174 *enddupp = enddup;
175 }
176
177 /*
178 * compact the leaf entries.
179 * Leave the highest-numbered stale entry stale.
180 * XXX should be the one closest to mid but mid is not yet computed.
181 */
182 static void
183 xfs_dir2_block_compact(
184 struct xfs_trans *tp,
185 struct xfs_buf *bp,
186 struct xfs_dir2_data_hdr *hdr,
187 struct xfs_dir2_block_tail *btp,
188 struct xfs_dir2_leaf_entry *blp,
189 int *needlog,
190 int *lfloghigh,
191 int *lfloglow)
192 {
193 int fromidx; /* source leaf index */
194 int toidx; /* target leaf index */
195 int needscan = 0;
196 int highstale; /* high stale index */
197
198 fromidx = toidx = be32_to_cpu(btp->count) - 1;
199 highstale = *lfloghigh = -1;
200 for (; fromidx >= 0; fromidx--) {
201 if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
202 if (highstale == -1)
203 highstale = toidx;
204 else {
205 if (*lfloghigh == -1)
206 *lfloghigh = toidx;
207 continue;
208 }
209 }
210 if (fromidx < toidx)
211 blp[toidx] = blp[fromidx];
212 toidx--;
213 }
214 *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
215 *lfloghigh -= be32_to_cpu(btp->stale) - 1;
216 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
217 xfs_dir2_data_make_free(tp, bp,
218 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
219 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
220 needlog, &needscan);
221 blp += be32_to_cpu(btp->stale) - 1;
222 btp->stale = cpu_to_be32(1);
223 /*
224 * If we now need to rebuild the bestfree map, do so.
225 * This needs to happen before the next call to use_free.
226 */
227 if (needscan)
228 xfs_dir2_data_freescan(tp->t_mountp, hdr, needlog);
229 }
230
231 /*
232 * Add an entry to a block directory.
233 */
234 int /* error */
235 xfs_dir2_block_addname(
236 xfs_da_args_t *args) /* directory op arguments */
237 {
238 xfs_dir2_data_hdr_t *hdr; /* block header */
239 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
240 struct xfs_buf *bp; /* buffer for block */
241 xfs_dir2_block_tail_t *btp; /* block tail */
242 int compact; /* need to compact leaf ents */
243 xfs_dir2_data_entry_t *dep; /* block data entry */
244 xfs_inode_t *dp; /* directory inode */
245 xfs_dir2_data_unused_t *dup; /* block unused entry */
246 int error; /* error return value */
247 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
248 xfs_dahash_t hash; /* hash value of found entry */
249 int high; /* high index for binary srch */
250 int highstale; /* high stale index */
251 int lfloghigh=0; /* last final leaf to log */
252 int lfloglow=0; /* first final leaf to log */
253 int len; /* length of the new entry */
254 int low; /* low index for binary srch */
255 int lowstale; /* low stale index */
256 int mid=0; /* midpoint for binary srch */
257 xfs_mount_t *mp; /* filesystem mount point */
258 int needlog; /* need to log header */
259 int needscan; /* need to rescan freespace */
260 __be16 *tagp; /* pointer to tag value */
261 xfs_trans_t *tp; /* transaction structure */
262
263 trace_xfs_dir2_block_addname(args);
264
265 dp = args->dp;
266 tp = args->trans;
267 mp = dp->i_mount;
268
269 /* Read the (one and only) directory block into bp. */
270 error = xfs_dir2_block_read(tp, dp, &bp);
271 if (error)
272 return error;
273
274 len = xfs_dir2_data_entsize(args->namelen);
275
276 /*
277 * Set up pointers to parts of the block.
278 */
279 hdr = bp->b_addr;
280 btp = xfs_dir2_block_tail_p(mp, hdr);
281 blp = xfs_dir2_block_leaf_p(btp);
282
283 /*
284 * Find out if we can reuse stale entries or whether we need extra
285 * space for entry and new leaf.
286 */
287 xfs_dir2_block_need_space(hdr, btp, blp, &tagp, &dup,
288 &enddup, &compact, len);
289
290 /*
291 * Done everything we need for a space check now.
292 */
293 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
294 xfs_trans_brelse(tp, bp);
295 if (!dup)
296 return XFS_ERROR(ENOSPC);
297 return 0;
298 }
299
300 /*
301 * If we don't have space for the new entry & leaf ...
302 */
303 if (!dup) {
304 /* Don't have a space reservation: return no-space. */
305 if (args->total == 0)
306 return XFS_ERROR(ENOSPC);
307 /*
308 * Convert to the next larger format.
309 * Then add the new entry in that format.
310 */
311 error = xfs_dir2_block_to_leaf(args, bp);
312 if (error)
313 return error;
314 return xfs_dir2_leaf_addname(args);
315 }
316
317 needlog = needscan = 0;
318
319 /*
320 * If need to compact the leaf entries, do it now.
321 */
322 if (compact)
323 xfs_dir2_block_compact(tp, bp, hdr, btp, blp, &needlog,
324 &lfloghigh, &lfloglow);
325 else if (btp->stale) {
326 /*
327 * Set leaf logging boundaries to impossible state.
328 * For the no-stale case they're set explicitly.
329 */
330 lfloglow = be32_to_cpu(btp->count);
331 lfloghigh = -1;
332 }
333
334 /*
335 * Find the slot that's first lower than our hash value, -1 if none.
336 */
337 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
338 mid = (low + high) >> 1;
339 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
340 break;
341 if (hash < args->hashval)
342 low = mid + 1;
343 else
344 high = mid - 1;
345 }
346 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
347 mid--;
348 }
349 /*
350 * No stale entries, will use enddup space to hold new leaf.
351 */
352 if (!btp->stale) {
353 /*
354 * Mark the space needed for the new leaf entry, now in use.
355 */
356 xfs_dir2_data_use_free(tp, bp, enddup,
357 (xfs_dir2_data_aoff_t)
358 ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) -
359 sizeof(*blp)),
360 (xfs_dir2_data_aoff_t)sizeof(*blp),
361 &needlog, &needscan);
362 /*
363 * Update the tail (entry count).
364 */
365 be32_add_cpu(&btp->count, 1);
366 /*
367 * If we now need to rebuild the bestfree map, do so.
368 * This needs to happen before the next call to use_free.
369 */
370 if (needscan) {
371 xfs_dir2_data_freescan(mp, hdr, &needlog);
372 needscan = 0;
373 }
374 /*
375 * Adjust pointer to the first leaf entry, we're about to move
376 * the table up one to open up space for the new leaf entry.
377 * Then adjust our index to match.
378 */
379 blp--;
380 mid++;
381 if (mid)
382 memmove(blp, &blp[1], mid * sizeof(*blp));
383 lfloglow = 0;
384 lfloghigh = mid;
385 }
386 /*
387 * Use a stale leaf for our new entry.
388 */
389 else {
390 for (lowstale = mid;
391 lowstale >= 0 &&
392 blp[lowstale].address !=
393 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
394 lowstale--)
395 continue;
396 for (highstale = mid + 1;
397 highstale < be32_to_cpu(btp->count) &&
398 blp[highstale].address !=
399 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
400 (lowstale < 0 || mid - lowstale > highstale - mid);
401 highstale++)
402 continue;
403 /*
404 * Move entries toward the low-numbered stale entry.
405 */
406 if (lowstale >= 0 &&
407 (highstale == be32_to_cpu(btp->count) ||
408 mid - lowstale <= highstale - mid)) {
409 if (mid - lowstale)
410 memmove(&blp[lowstale], &blp[lowstale + 1],
411 (mid - lowstale) * sizeof(*blp));
412 lfloglow = MIN(lowstale, lfloglow);
413 lfloghigh = MAX(mid, lfloghigh);
414 }
415 /*
416 * Move entries toward the high-numbered stale entry.
417 */
418 else {
419 ASSERT(highstale < be32_to_cpu(btp->count));
420 mid++;
421 if (highstale - mid)
422 memmove(&blp[mid + 1], &blp[mid],
423 (highstale - mid) * sizeof(*blp));
424 lfloglow = MIN(mid, lfloglow);
425 lfloghigh = MAX(highstale, lfloghigh);
426 }
427 be32_add_cpu(&btp->stale, -1);
428 }
429 /*
430 * Point to the new data entry.
431 */
432 dep = (xfs_dir2_data_entry_t *)dup;
433 /*
434 * Fill in the leaf entry.
435 */
436 blp[mid].hashval = cpu_to_be32(args->hashval);
437 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
438 (char *)dep - (char *)hdr));
439 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
440 /*
441 * Mark space for the data entry used.
442 */
443 xfs_dir2_data_use_free(tp, bp, dup,
444 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
445 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
446 /*
447 * Create the new data entry.
448 */
449 dep->inumber = cpu_to_be64(args->inumber);
450 dep->namelen = args->namelen;
451 memcpy(dep->name, args->name, args->namelen);
452 tagp = xfs_dir2_data_entry_tag_p(dep);
453 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
454 /*
455 * Clean up the bestfree array and log the header, tail, and entry.
456 */
457 if (needscan)
458 xfs_dir2_data_freescan(mp, hdr, &needlog);
459 if (needlog)
460 xfs_dir2_data_log_header(tp, bp);
461 xfs_dir2_block_log_tail(tp, bp);
462 xfs_dir2_data_log_entry(tp, bp, dep);
463 xfs_dir2_data_check(dp, bp);
464 return 0;
465 }
466
467 /*
468 * Readdir for block directories.
469 */
470 int /* error */
471 xfs_dir2_block_getdents(
472 xfs_inode_t *dp, /* incore inode */
473 void *dirent,
474 xfs_off_t *offset,
475 filldir_t filldir)
476 {
477 xfs_dir2_data_hdr_t *hdr; /* block header */
478 struct xfs_buf *bp; /* buffer for block */
479 xfs_dir2_block_tail_t *btp; /* block tail */
480 xfs_dir2_data_entry_t *dep; /* block data entry */
481 xfs_dir2_data_unused_t *dup; /* block unused entry */
482 char *endptr; /* end of the data entries */
483 int error; /* error return value */
484 xfs_mount_t *mp; /* filesystem mount point */
485 char *ptr; /* current data entry */
486 int wantoff; /* starting block offset */
487 xfs_off_t cook;
488
489 mp = dp->i_mount;
490 /*
491 * If the block number in the offset is out of range, we're done.
492 */
493 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk)
494 return 0;
495
496 error = xfs_dir2_block_read(NULL, dp, &bp);
497 if (error)
498 return error;
499
500 /*
501 * Extract the byte offset we start at from the seek pointer.
502 * We'll skip entries before this.
503 */
504 wantoff = xfs_dir2_dataptr_to_off(mp, *offset);
505 hdr = bp->b_addr;
506 xfs_dir2_data_check(dp, bp);
507 /*
508 * Set up values for the loop.
509 */
510 btp = xfs_dir2_block_tail_p(mp, hdr);
511 ptr = (char *)(hdr + 1);
512 endptr = (char *)xfs_dir2_block_leaf_p(btp);
513
514 /*
515 * Loop over the data portion of the block.
516 * Each object is a real entry (dep) or an unused one (dup).
517 */
518 while (ptr < endptr) {
519 dup = (xfs_dir2_data_unused_t *)ptr;
520 /*
521 * Unused, skip it.
522 */
523 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
524 ptr += be16_to_cpu(dup->length);
525 continue;
526 }
527
528 dep = (xfs_dir2_data_entry_t *)ptr;
529
530 /*
531 * Bump pointer for the next iteration.
532 */
533 ptr += xfs_dir2_data_entsize(dep->namelen);
534 /*
535 * The entry is before the desired starting point, skip it.
536 */
537 if ((char *)dep - (char *)hdr < wantoff)
538 continue;
539
540 cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
541 (char *)dep - (char *)hdr);
542
543 /*
544 * If it didn't fit, set the final offset to here & return.
545 */
546 if (filldir(dirent, (char *)dep->name, dep->namelen,
547 cook & 0x7fffffff, be64_to_cpu(dep->inumber),
548 DT_UNKNOWN)) {
549 *offset = cook & 0x7fffffff;
550 xfs_trans_brelse(NULL, bp);
551 return 0;
552 }
553 }
554
555 /*
556 * Reached the end of the block.
557 * Set the offset to a non-existent block 1 and return.
558 */
559 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
560 0x7fffffff;
561 xfs_trans_brelse(NULL, bp);
562 return 0;
563 }
564
565 /*
566 * Log leaf entries from the block.
567 */
568 static void
569 xfs_dir2_block_log_leaf(
570 xfs_trans_t *tp, /* transaction structure */
571 struct xfs_buf *bp, /* block buffer */
572 int first, /* index of first logged leaf */
573 int last) /* index of last logged leaf */
574 {
575 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
576 xfs_dir2_leaf_entry_t *blp;
577 xfs_dir2_block_tail_t *btp;
578
579 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
580 blp = xfs_dir2_block_leaf_p(btp);
581 xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
582 (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
583 }
584
585 /*
586 * Log the block tail.
587 */
588 static void
589 xfs_dir2_block_log_tail(
590 xfs_trans_t *tp, /* transaction structure */
591 struct xfs_buf *bp) /* block buffer */
592 {
593 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
594 xfs_dir2_block_tail_t *btp;
595
596 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
597 xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
598 (uint)((char *)(btp + 1) - (char *)hdr - 1));
599 }
600
601 /*
602 * Look up an entry in the block. This is the external routine,
603 * xfs_dir2_block_lookup_int does the real work.
604 */
605 int /* error */
606 xfs_dir2_block_lookup(
607 xfs_da_args_t *args) /* dir lookup arguments */
608 {
609 xfs_dir2_data_hdr_t *hdr; /* block header */
610 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
611 struct xfs_buf *bp; /* block buffer */
612 xfs_dir2_block_tail_t *btp; /* block tail */
613 xfs_dir2_data_entry_t *dep; /* block data entry */
614 xfs_inode_t *dp; /* incore inode */
615 int ent; /* entry index */
616 int error; /* error return value */
617 xfs_mount_t *mp; /* filesystem mount point */
618
619 trace_xfs_dir2_block_lookup(args);
620
621 /*
622 * Get the buffer, look up the entry.
623 * If not found (ENOENT) then return, have no buffer.
624 */
625 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
626 return error;
627 dp = args->dp;
628 mp = dp->i_mount;
629 hdr = bp->b_addr;
630 xfs_dir2_data_check(dp, bp);
631 btp = xfs_dir2_block_tail_p(mp, hdr);
632 blp = xfs_dir2_block_leaf_p(btp);
633 /*
634 * Get the offset from the leaf entry, to point to the data.
635 */
636 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
637 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
638 /*
639 * Fill in inode number, CI name if appropriate, release the block.
640 */
641 args->inumber = be64_to_cpu(dep->inumber);
642 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
643 xfs_trans_brelse(args->trans, bp);
644 return XFS_ERROR(error);
645 }
646
647 /*
648 * Internal block lookup routine.
649 */
650 static int /* error */
651 xfs_dir2_block_lookup_int(
652 xfs_da_args_t *args, /* dir lookup arguments */
653 struct xfs_buf **bpp, /* returned block buffer */
654 int *entno) /* returned entry number */
655 {
656 xfs_dir2_dataptr_t addr; /* data entry address */
657 xfs_dir2_data_hdr_t *hdr; /* block header */
658 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
659 struct xfs_buf *bp; /* block buffer */
660 xfs_dir2_block_tail_t *btp; /* block tail */
661 xfs_dir2_data_entry_t *dep; /* block data entry */
662 xfs_inode_t *dp; /* incore inode */
663 int error; /* error return value */
664 xfs_dahash_t hash; /* found hash value */
665 int high; /* binary search high index */
666 int low; /* binary search low index */
667 int mid; /* binary search current idx */
668 xfs_mount_t *mp; /* filesystem mount point */
669 xfs_trans_t *tp; /* transaction pointer */
670 enum xfs_dacmp cmp; /* comparison result */
671
672 dp = args->dp;
673 tp = args->trans;
674 mp = dp->i_mount;
675
676 error = xfs_dir2_block_read(tp, dp, &bp);
677 if (error)
678 return error;
679
680 hdr = bp->b_addr;
681 xfs_dir2_data_check(dp, bp);
682 btp = xfs_dir2_block_tail_p(mp, hdr);
683 blp = xfs_dir2_block_leaf_p(btp);
684 /*
685 * Loop doing a binary search for our hash value.
686 * Find our entry, ENOENT if it's not there.
687 */
688 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
689 ASSERT(low <= high);
690 mid = (low + high) >> 1;
691 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
692 break;
693 if (hash < args->hashval)
694 low = mid + 1;
695 else
696 high = mid - 1;
697 if (low > high) {
698 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
699 xfs_trans_brelse(tp, bp);
700 return XFS_ERROR(ENOENT);
701 }
702 }
703 /*
704 * Back up to the first one with the right hash value.
705 */
706 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
707 mid--;
708 }
709 /*
710 * Now loop forward through all the entries with the
711 * right hash value looking for our name.
712 */
713 do {
714 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
715 continue;
716 /*
717 * Get pointer to the entry from the leaf.
718 */
719 dep = (xfs_dir2_data_entry_t *)
720 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
721 /*
722 * Compare name and if it's an exact match, return the index
723 * and buffer. If it's the first case-insensitive match, store
724 * the index and buffer and continue looking for an exact match.
725 */
726 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
727 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
728 args->cmpresult = cmp;
729 *bpp = bp;
730 *entno = mid;
731 if (cmp == XFS_CMP_EXACT)
732 return 0;
733 }
734 } while (++mid < be32_to_cpu(btp->count) &&
735 be32_to_cpu(blp[mid].hashval) == hash);
736
737 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
738 /*
739 * Here, we can only be doing a lookup (not a rename or replace).
740 * If a case-insensitive match was found earlier, return success.
741 */
742 if (args->cmpresult == XFS_CMP_CASE)
743 return 0;
744 /*
745 * No match, release the buffer and return ENOENT.
746 */
747 xfs_trans_brelse(tp, bp);
748 return XFS_ERROR(ENOENT);
749 }
750
751 /*
752 * Remove an entry from a block format directory.
753 * If that makes the block small enough to fit in shortform, transform it.
754 */
755 int /* error */
756 xfs_dir2_block_removename(
757 xfs_da_args_t *args) /* directory operation args */
758 {
759 xfs_dir2_data_hdr_t *hdr; /* block header */
760 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
761 struct xfs_buf *bp; /* block buffer */
762 xfs_dir2_block_tail_t *btp; /* block tail */
763 xfs_dir2_data_entry_t *dep; /* block data entry */
764 xfs_inode_t *dp; /* incore inode */
765 int ent; /* block leaf entry index */
766 int error; /* error return value */
767 xfs_mount_t *mp; /* filesystem mount point */
768 int needlog; /* need to log block header */
769 int needscan; /* need to fixup bestfree */
770 xfs_dir2_sf_hdr_t sfh; /* shortform header */
771 int size; /* shortform size */
772 xfs_trans_t *tp; /* transaction pointer */
773
774 trace_xfs_dir2_block_removename(args);
775
776 /*
777 * Look up the entry in the block. Gets the buffer and entry index.
778 * It will always be there, the vnodeops level does a lookup first.
779 */
780 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
781 return error;
782 }
783 dp = args->dp;
784 tp = args->trans;
785 mp = dp->i_mount;
786 hdr = bp->b_addr;
787 btp = xfs_dir2_block_tail_p(mp, hdr);
788 blp = xfs_dir2_block_leaf_p(btp);
789 /*
790 * Point to the data entry using the leaf entry.
791 */
792 dep = (xfs_dir2_data_entry_t *)
793 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
794 /*
795 * Mark the data entry's space free.
796 */
797 needlog = needscan = 0;
798 xfs_dir2_data_make_free(tp, bp,
799 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
800 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
801 /*
802 * Fix up the block tail.
803 */
804 be32_add_cpu(&btp->stale, 1);
805 xfs_dir2_block_log_tail(tp, bp);
806 /*
807 * Remove the leaf entry by marking it stale.
808 */
809 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
810 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
811 /*
812 * Fix up bestfree, log the header if necessary.
813 */
814 if (needscan)
815 xfs_dir2_data_freescan(mp, hdr, &needlog);
816 if (needlog)
817 xfs_dir2_data_log_header(tp, bp);
818 xfs_dir2_data_check(dp, bp);
819 /*
820 * See if the size as a shortform is good enough.
821 */
822 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
823 if (size > XFS_IFORK_DSIZE(dp))
824 return 0;
825
826 /*
827 * If it works, do the conversion.
828 */
829 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
830 }
831
832 /*
833 * Replace an entry in a V2 block directory.
834 * Change the inode number to the new value.
835 */
836 int /* error */
837 xfs_dir2_block_replace(
838 xfs_da_args_t *args) /* directory operation args */
839 {
840 xfs_dir2_data_hdr_t *hdr; /* block header */
841 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
842 struct xfs_buf *bp; /* block buffer */
843 xfs_dir2_block_tail_t *btp; /* block tail */
844 xfs_dir2_data_entry_t *dep; /* block data entry */
845 xfs_inode_t *dp; /* incore inode */
846 int ent; /* leaf entry index */
847 int error; /* error return value */
848 xfs_mount_t *mp; /* filesystem mount point */
849
850 trace_xfs_dir2_block_replace(args);
851
852 /*
853 * Lookup the entry in the directory. Get buffer and entry index.
854 * This will always succeed since the caller has already done a lookup.
855 */
856 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
857 return error;
858 }
859 dp = args->dp;
860 mp = dp->i_mount;
861 hdr = bp->b_addr;
862 btp = xfs_dir2_block_tail_p(mp, hdr);
863 blp = xfs_dir2_block_leaf_p(btp);
864 /*
865 * Point to the data entry we need to change.
866 */
867 dep = (xfs_dir2_data_entry_t *)
868 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
869 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
870 /*
871 * Change the inode number to the new value.
872 */
873 dep->inumber = cpu_to_be64(args->inumber);
874 xfs_dir2_data_log_entry(args->trans, bp, dep);
875 xfs_dir2_data_check(dp, bp);
876 return 0;
877 }
878
879 /*
880 * Qsort comparison routine for the block leaf entries.
881 */
882 static int /* sort order */
883 xfs_dir2_block_sort(
884 const void *a, /* first leaf entry */
885 const void *b) /* second leaf entry */
886 {
887 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
888 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
889
890 la = a;
891 lb = b;
892 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
893 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
894 }
895
896 /*
897 * Convert a V2 leaf directory to a V2 block directory if possible.
898 */
899 int /* error */
900 xfs_dir2_leaf_to_block(
901 xfs_da_args_t *args, /* operation arguments */
902 struct xfs_buf *lbp, /* leaf buffer */
903 struct xfs_buf *dbp) /* data buffer */
904 {
905 __be16 *bestsp; /* leaf bests table */
906 xfs_dir2_data_hdr_t *hdr; /* block header */
907 xfs_dir2_block_tail_t *btp; /* block tail */
908 xfs_inode_t *dp; /* incore directory inode */
909 xfs_dir2_data_unused_t *dup; /* unused data entry */
910 int error; /* error return value */
911 int from; /* leaf from index */
912 xfs_dir2_leaf_t *leaf; /* leaf structure */
913 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
914 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
915 xfs_mount_t *mp; /* file system mount point */
916 int needlog; /* need to log data header */
917 int needscan; /* need to scan for bestfree */
918 xfs_dir2_sf_hdr_t sfh; /* shortform header */
919 int size; /* bytes used */
920 __be16 *tagp; /* end of entry (tag) */
921 int to; /* block/leaf to index */
922 xfs_trans_t *tp; /* transaction pointer */
923
924 trace_xfs_dir2_leaf_to_block(args);
925
926 dp = args->dp;
927 tp = args->trans;
928 mp = dp->i_mount;
929 leaf = lbp->b_addr;
930 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
931 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
932 /*
933 * If there are data blocks other than the first one, take this
934 * opportunity to remove trailing empty data blocks that may have
935 * been left behind during no-space-reservation operations.
936 * These will show up in the leaf bests table.
937 */
938 while (dp->i_d.di_size > mp->m_dirblksize) {
939 bestsp = xfs_dir2_leaf_bests_p(ltp);
940 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
941 mp->m_dirblksize - (uint)sizeof(*hdr)) {
942 if ((error =
943 xfs_dir2_leaf_trim_data(args, lbp,
944 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
945 return error;
946 } else
947 return 0;
948 }
949 /*
950 * Read the data block if we don't already have it, give up if it fails.
951 */
952 if (!dbp) {
953 error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
954 XFS_DATA_FORK, NULL);
955 if (error)
956 return error;
957 }
958 hdr = dbp->b_addr;
959 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
960 /*
961 * Size of the "leaf" area in the block.
962 */
963 size = (uint)sizeof(xfs_dir2_block_tail_t) +
964 (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
965 /*
966 * Look at the last data entry.
967 */
968 tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1;
969 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
970 /*
971 * If it's not free or is too short we can't do it.
972 */
973 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
974 be16_to_cpu(dup->length) < size)
975 return 0;
976
977 /*
978 * Start converting it to block form.
979 */
980 hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
981 needlog = 1;
982 needscan = 0;
983 /*
984 * Use up the space at the end of the block (blp/btp).
985 */
986 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
987 &needlog, &needscan);
988 /*
989 * Initialize the block tail.
990 */
991 btp = xfs_dir2_block_tail_p(mp, hdr);
992 btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
993 btp->stale = 0;
994 xfs_dir2_block_log_tail(tp, dbp);
995 /*
996 * Initialize the block leaf area. We compact out stale entries.
997 */
998 lep = xfs_dir2_block_leaf_p(btp);
999 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
1000 if (leaf->ents[from].address ==
1001 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
1002 continue;
1003 lep[to++] = leaf->ents[from];
1004 }
1005 ASSERT(to == be32_to_cpu(btp->count));
1006 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
1007 /*
1008 * Scan the bestfree if we need it and log the data block header.
1009 */
1010 if (needscan)
1011 xfs_dir2_data_freescan(mp, hdr, &needlog);
1012 if (needlog)
1013 xfs_dir2_data_log_header(tp, dbp);
1014 /*
1015 * Pitch the old leaf block.
1016 */
1017 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
1018 if (error)
1019 return error;
1020
1021 /*
1022 * Now see if the resulting block can be shrunken to shortform.
1023 */
1024 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
1025 if (size > XFS_IFORK_DSIZE(dp))
1026 return 0;
1027
1028 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
1029 }
1030
1031 /*
1032 * Convert the shortform directory to block form.
1033 */
1034 int /* error */
1035 xfs_dir2_sf_to_block(
1036 xfs_da_args_t *args) /* operation arguments */
1037 {
1038 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
1039 xfs_dir2_data_hdr_t *hdr; /* block header */
1040 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1041 struct xfs_buf *bp; /* block buffer */
1042 xfs_dir2_block_tail_t *btp; /* block tail pointer */
1043 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1044 xfs_inode_t *dp; /* incore directory inode */
1045 int dummy; /* trash */
1046 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1047 int endoffset; /* end of data objects */
1048 int error; /* error return value */
1049 int i; /* index */
1050 xfs_mount_t *mp; /* filesystem mount point */
1051 int needlog; /* need to log block header */
1052 int needscan; /* need to scan block freespc */
1053 int newoffset; /* offset from current entry */
1054 int offset; /* target block offset */
1055 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
1056 xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
1057 xfs_dir2_sf_hdr_t *sfp; /* shortform header */
1058 __be16 *tagp; /* end of data entry */
1059 xfs_trans_t *tp; /* transaction pointer */
1060 struct xfs_name name;
1061
1062 trace_xfs_dir2_sf_to_block(args);
1063
1064 dp = args->dp;
1065 tp = args->trans;
1066 mp = dp->i_mount;
1067 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1068 /*
1069 * Bomb out if the shortform directory is way too short.
1070 */
1071 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1072 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1073 return XFS_ERROR(EIO);
1074 }
1075
1076 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1077
1078 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1079 ASSERT(dp->i_df.if_u1.if_data != NULL);
1080 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
1081
1082 /*
1083 * Copy the directory into a temporary buffer.
1084 * Then pitch the incore inode data so we can make extents.
1085 */
1086 sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1087 memcpy(sfp, oldsfp, dp->i_df.if_bytes);
1088
1089 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
1090 dp->i_d.di_size = 0;
1091 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1092
1093 /*
1094 * Add block 0 to the inode.
1095 */
1096 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1097 if (error) {
1098 kmem_free(sfp);
1099 return error;
1100 }
1101 /*
1102 * Initialize the data block.
1103 */
1104 error = xfs_dir2_data_init(args, blkno, &bp);
1105 if (error) {
1106 kmem_free(sfp);
1107 return error;
1108 }
1109 hdr = bp->b_addr;
1110 hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
1111 /*
1112 * Compute size of block "tail" area.
1113 */
1114 i = (uint)sizeof(*btp) +
1115 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
1116 /*
1117 * The whole thing is initialized to free by the init routine.
1118 * Say we're using the leaf and tail area.
1119 */
1120 dup = (xfs_dir2_data_unused_t *)(hdr + 1);
1121 needlog = needscan = 0;
1122 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1123 &needscan);
1124 ASSERT(needscan == 0);
1125 /*
1126 * Fill in the tail.
1127 */
1128 btp = xfs_dir2_block_tail_p(mp, hdr);
1129 btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
1130 btp->stale = 0;
1131 blp = xfs_dir2_block_leaf_p(btp);
1132 endoffset = (uint)((char *)blp - (char *)hdr);
1133 /*
1134 * Remove the freespace, we'll manage it.
1135 */
1136 xfs_dir2_data_use_free(tp, bp, dup,
1137 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
1138 be16_to_cpu(dup->length), &needlog, &needscan);
1139 /*
1140 * Create entry for .
1141 */
1142 dep = (xfs_dir2_data_entry_t *)
1143 ((char *)hdr + XFS_DIR2_DATA_DOT_OFFSET);
1144 dep->inumber = cpu_to_be64(dp->i_ino);
1145 dep->namelen = 1;
1146 dep->name[0] = '.';
1147 tagp = xfs_dir2_data_entry_tag_p(dep);
1148 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1149 xfs_dir2_data_log_entry(tp, bp, dep);
1150 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
1151 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1152 (char *)dep - (char *)hdr));
1153 /*
1154 * Create entry for ..
1155 */
1156 dep = (xfs_dir2_data_entry_t *)
1157 ((char *)hdr + XFS_DIR2_DATA_DOTDOT_OFFSET);
1158 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
1159 dep->namelen = 2;
1160 dep->name[0] = dep->name[1] = '.';
1161 tagp = xfs_dir2_data_entry_tag_p(dep);
1162 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1163 xfs_dir2_data_log_entry(tp, bp, dep);
1164 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
1165 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1166 (char *)dep - (char *)hdr));
1167 offset = XFS_DIR2_DATA_FIRST_OFFSET;
1168 /*
1169 * Loop over existing entries, stuff them in.
1170 */
1171 i = 0;
1172 if (!sfp->count)
1173 sfep = NULL;
1174 else
1175 sfep = xfs_dir2_sf_firstentry(sfp);
1176 /*
1177 * Need to preserve the existing offset values in the sf directory.
1178 * Insert holes (unused entries) where necessary.
1179 */
1180 while (offset < endoffset) {
1181 /*
1182 * sfep is null when we reach the end of the list.
1183 */
1184 if (sfep == NULL)
1185 newoffset = endoffset;
1186 else
1187 newoffset = xfs_dir2_sf_get_offset(sfep);
1188 /*
1189 * There should be a hole here, make one.
1190 */
1191 if (offset < newoffset) {
1192 dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
1193 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1194 dup->length = cpu_to_be16(newoffset - offset);
1195 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
1196 ((char *)dup - (char *)hdr));
1197 xfs_dir2_data_log_unused(tp, bp, dup);
1198 xfs_dir2_data_freeinsert(hdr, dup, &dummy);
1199 offset += be16_to_cpu(dup->length);
1200 continue;
1201 }
1202 /*
1203 * Copy a real entry.
1204 */
1205 dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
1206 dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep));
1207 dep->namelen = sfep->namelen;
1208 memcpy(dep->name, sfep->name, dep->namelen);
1209 tagp = xfs_dir2_data_entry_tag_p(dep);
1210 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1211 xfs_dir2_data_log_entry(tp, bp, dep);
1212 name.name = sfep->name;
1213 name.len = sfep->namelen;
1214 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1215 hashname(&name));
1216 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1217 (char *)dep - (char *)hdr));
1218 offset = (int)((char *)(tagp + 1) - (char *)hdr);
1219 if (++i == sfp->count)
1220 sfep = NULL;
1221 else
1222 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
1223 }
1224 /* Done with the temporary buffer */
1225 kmem_free(sfp);
1226 /*
1227 * Sort the leaf entries by hash value.
1228 */
1229 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
1230 /*
1231 * Log the leaf entry area and tail.
1232 * Already logged the header in data_init, ignore needlog.
1233 */
1234 ASSERT(needscan == 0);
1235 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
1236 xfs_dir2_block_log_tail(tp, bp);
1237 xfs_dir2_data_check(dp, bp);
1238 return 0;
1239 }
This page took 0.063663 seconds and 6 git commands to generate.