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