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