xfs: kill __KERNEL__ check for debug code in allocation code
[deliverable/linux.git] / fs / xfs / xfs_dir2_data.c
CommitLineData
1da177e4 1/*
7b718769 2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
f5f3d9b0 3 * Copyright (c) 2013 Red Hat, Inc.
7b718769 4 * All Rights Reserved.
1da177e4 5 *
7b718769
NS
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
1da177e4
LT
8 * published by the Free Software Foundation.
9 *
7b718769
NS
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
1da177e4 14 *
7b718769
NS
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4 18 */
1da177e4 19#include "xfs.h"
a844f451 20#include "xfs_fs.h"
1da177e4 21#include "xfs_types.h"
1da177e4
LT
22#include "xfs_log.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
da353b0d 25#include "xfs_ag.h"
1da177e4 26#include "xfs_mount.h"
a844f451 27#include "xfs_da_btree.h"
1da177e4 28#include "xfs_bmap_btree.h"
1da177e4
LT
29#include "xfs_dinode.h"
30#include "xfs_inode.h"
57926640 31#include "xfs_dir2_format.h"
2b9ab5ab 32#include "xfs_dir2.h"
57926640 33#include "xfs_dir2_priv.h"
1da177e4 34#include "xfs_error.h"
33363fee
DC
35#include "xfs_buf_item.h"
36#include "xfs_cksum.h"
1da177e4 37
1da177e4
LT
38/*
39 * Check the consistency of the data block.
40 * The input can also be a block-format directory.
82025d7f 41 * Return 0 is the buffer is good, otherwise an error.
1da177e4 42 */
82025d7f 43int
33363fee 44__xfs_dir3_data_check(
1d9025e5
DC
45 struct xfs_inode *dp, /* incore inode pointer */
46 struct xfs_buf *bp) /* data block's buffer */
1da177e4
LT
47{
48 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
49 xfs_dir2_data_free_t *bf; /* bestfree table */
50 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
51 int count; /* count of entries found */
c2066e26 52 xfs_dir2_data_hdr_t *hdr; /* data block header */
1da177e4
LT
53 xfs_dir2_data_entry_t *dep; /* data entry */
54 xfs_dir2_data_free_t *dfp; /* bestfree entry */
55 xfs_dir2_data_unused_t *dup; /* unused entry */
56 char *endp; /* end of useful data */
57 int freeseen; /* mask of bestfrees seen */
58 xfs_dahash_t hash; /* hash of current name */
59 int i; /* leaf index */
60 int lastfree; /* last entry was unused */
61 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
62 xfs_mount_t *mp; /* filesystem mount point */
63 char *p; /* current data position */
64 int stale; /* count of stale leaves */
5163f95a 65 struct xfs_name name;
1da177e4 66
82025d7f 67 mp = bp->b_target->bt_mount;
1d9025e5 68 hdr = bp->b_addr;
f5f3d9b0
DC
69 bf = xfs_dir3_data_bestfree_p(hdr);
70 p = (char *)xfs_dir3_data_entry_p(hdr);
c2066e26 71
82025d7f 72 switch (hdr->magic) {
f5f3d9b0 73 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
82025d7f 74 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
c2066e26 75 btp = xfs_dir2_block_tail_p(mp, hdr);
bbaaf538 76 lep = xfs_dir2_block_leaf_p(btp);
1da177e4 77 endp = (char *)lep;
82025d7f 78 break;
33363fee 79 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
82025d7f 80 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
c2066e26 81 endp = (char *)hdr + mp->m_dirblksize;
82025d7f
DC
82 break;
83 default:
84 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
85 return EFSCORRUPTED;
c2066e26
CH
86 }
87
1da177e4
LT
88 count = lastfree = freeseen = 0;
89 /*
90 * Account for zero bestfree entries.
91 */
92 if (!bf[0].length) {
82025d7f 93 XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
1da177e4
LT
94 freeseen |= 1 << 0;
95 }
96 if (!bf[1].length) {
82025d7f 97 XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
1da177e4
LT
98 freeseen |= 1 << 1;
99 }
100 if (!bf[2].length) {
82025d7f 101 XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
1da177e4
LT
102 freeseen |= 1 << 2;
103 }
82025d7f
DC
104
105 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
106 be16_to_cpu(bf[1].length));
107 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
108 be16_to_cpu(bf[2].length));
1da177e4
LT
109 /*
110 * Loop over the data/unused entries.
111 */
112 while (p < endp) {
113 dup = (xfs_dir2_data_unused_t *)p;
114 /*
115 * If it's unused, look for the space in the bestfree table.
116 * If we find it, account for that, else make sure it
117 * doesn't need to be there.
118 */
ad354eb3 119 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
82025d7f
DC
120 XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
121 XFS_WANT_CORRUPTED_RETURN(
122 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
123 (char *)dup - (char *)hdr);
c2066e26 124 dfp = xfs_dir2_data_freefind(hdr, dup);
1da177e4
LT
125 if (dfp) {
126 i = (int)(dfp - bf);
82025d7f
DC
127 XFS_WANT_CORRUPTED_RETURN(
128 (freeseen & (1 << i)) == 0);
1da177e4 129 freeseen |= 1 << i;
70e73f59 130 } else {
82025d7f
DC
131 XFS_WANT_CORRUPTED_RETURN(
132 be16_to_cpu(dup->length) <=
133 be16_to_cpu(bf[2].length));
70e73f59 134 }
ad354eb3 135 p += be16_to_cpu(dup->length);
1da177e4
LT
136 lastfree = 1;
137 continue;
138 }
139 /*
140 * It's a real entry. Validate the fields.
141 * If this is a block directory then make sure it's
142 * in the leaf section of the block.
143 * The linear search is crude but this is DEBUG code.
144 */
145 dep = (xfs_dir2_data_entry_t *)p;
82025d7f
DC
146 XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
147 XFS_WANT_CORRUPTED_RETURN(
148 !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
149 XFS_WANT_CORRUPTED_RETURN(
150 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) ==
151 (char *)dep - (char *)hdr);
1da177e4
LT
152 count++;
153 lastfree = 0;
f5f3d9b0
DC
154 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
155 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
bbaaf538 156 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
1da177e4 157 (xfs_dir2_data_aoff_t)
c2066e26 158 ((char *)dep - (char *)hdr));
5163f95a
BN
159 name.name = dep->name;
160 name.len = dep->namelen;
161 hash = mp->m_dirnameops->hashname(&name);
e922fffa 162 for (i = 0; i < be32_to_cpu(btp->count); i++) {
3c1f9c15
NS
163 if (be32_to_cpu(lep[i].address) == addr &&
164 be32_to_cpu(lep[i].hashval) == hash)
1da177e4
LT
165 break;
166 }
82025d7f 167 XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
1da177e4 168 }
bbaaf538 169 p += xfs_dir2_data_entsize(dep->namelen);
1da177e4
LT
170 }
171 /*
172 * Need to have seen all the entries and all the bestfree slots.
173 */
82025d7f 174 XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
f5f3d9b0
DC
175 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
176 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
e922fffa 177 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
69ef921b
CH
178 if (lep[i].address ==
179 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
1da177e4
LT
180 stale++;
181 if (i > 0)
82025d7f
DC
182 XFS_WANT_CORRUPTED_RETURN(
183 be32_to_cpu(lep[i].hashval) >=
184 be32_to_cpu(lep[i - 1].hashval));
1da177e4 185 }
82025d7f
DC
186 XFS_WANT_CORRUPTED_RETURN(count ==
187 be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
188 XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
1da177e4 189 }
82025d7f 190 return 0;
1da177e4 191}
1da177e4 192
33363fee
DC
193static bool
194xfs_dir3_data_verify(
e4813572
DC
195 struct xfs_buf *bp)
196{
197 struct xfs_mount *mp = bp->b_target->bt_mount;
33363fee 198 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
e4813572 199
33363fee
DC
200 if (xfs_sb_version_hascrc(&mp->m_sb)) {
201 if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
202 return false;
203 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
204 return false;
205 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
206 return false;
207 } else {
208 if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
209 return false;
e4813572 210 }
33363fee
DC
211 if (__xfs_dir3_data_check(NULL, bp))
212 return false;
213 return true;
612cfbfe
DC
214}
215
1813dd64
DC
216/*
217 * Readahead of the first block of the directory when it is opened is completely
218 * oblivious to the format of the directory. Hence we can either get a block
219 * format buffer or a data format buffer on readahead.
220 */
221static void
33363fee 222xfs_dir3_data_reada_verify(
1813dd64
DC
223 struct xfs_buf *bp)
224{
225 struct xfs_mount *mp = bp->b_target->bt_mount;
226 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
227
228 switch (hdr->magic) {
229 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
f5f3d9b0
DC
230 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
231 bp->b_ops = &xfs_dir3_block_buf_ops;
1813dd64
DC
232 bp->b_ops->verify_read(bp);
233 return;
234 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
33363fee
DC
235 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
236 xfs_dir3_data_verify(bp);
1813dd64
DC
237 return;
238 default:
239 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
240 xfs_buf_ioerror(bp, EFSCORRUPTED);
241 break;
242 }
243}
244
245static void
33363fee 246xfs_dir3_data_read_verify(
612cfbfe
DC
247 struct xfs_buf *bp)
248{
33363fee
DC
249 struct xfs_mount *mp = bp->b_target->bt_mount;
250
251 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
252 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
253 XFS_DIR3_DATA_CRC_OFF)) ||
254 !xfs_dir3_data_verify(bp)) {
255 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
256 xfs_buf_ioerror(bp, EFSCORRUPTED);
257 }
612cfbfe 258}
e4813572 259
b0f539de 260static void
33363fee 261xfs_dir3_data_write_verify(
612cfbfe
DC
262 struct xfs_buf *bp)
263{
33363fee
DC
264 struct xfs_mount *mp = bp->b_target->bt_mount;
265 struct xfs_buf_log_item *bip = bp->b_fspriv;
266 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
267
268 if (!xfs_dir3_data_verify(bp)) {
269 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
270 xfs_buf_ioerror(bp, EFSCORRUPTED);
271 return;
272 }
273
274 if (!xfs_sb_version_hascrc(&mp->m_sb))
275 return;
276
277 if (bip)
278 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
279
280 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
e4813572
DC
281}
282
33363fee
DC
283const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
284 .verify_read = xfs_dir3_data_read_verify,
285 .verify_write = xfs_dir3_data_write_verify,
1813dd64
DC
286};
287
33363fee
DC
288static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
289 .verify_read = xfs_dir3_data_reada_verify,
290 .verify_write = xfs_dir3_data_write_verify,
1813dd64
DC
291};
292
612cfbfe 293
e4813572 294int
33363fee 295xfs_dir3_data_read(
e4813572
DC
296 struct xfs_trans *tp,
297 struct xfs_inode *dp,
298 xfs_dablk_t bno,
299 xfs_daddr_t mapped_bno,
300 struct xfs_buf **bpp)
301{
d75afeb3
DC
302 int err;
303
304 err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
33363fee 305 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
d75afeb3 306 if (!err && tp)
61fe135c 307 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
d75afeb3 308 return err;
e4813572
DC
309}
310
da6958c8 311int
33363fee 312xfs_dir3_data_readahead(
da6958c8
DC
313 struct xfs_trans *tp,
314 struct xfs_inode *dp,
315 xfs_dablk_t bno,
316 xfs_daddr_t mapped_bno)
317{
318 return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
33363fee 319 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
da6958c8
DC
320}
321
1da177e4
LT
322/*
323 * Given a data block and an unused entry from that block,
324 * return the bestfree entry if any that corresponds to it.
325 */
2b9ab5ab 326xfs_dir2_data_free_t *
1da177e4 327xfs_dir2_data_freefind(
c2066e26 328 xfs_dir2_data_hdr_t *hdr, /* data block */
1da177e4
LT
329 xfs_dir2_data_unused_t *dup) /* data unused entry */
330{
331 xfs_dir2_data_free_t *dfp; /* bestfree entry */
332 xfs_dir2_data_aoff_t off; /* offset value needed */
f5f3d9b0 333 struct xfs_dir2_data_free *bf;
1da177e4
LT
334#if defined(DEBUG) && defined(__KERNEL__)
335 int matched; /* matched the value */
336 int seenzero; /* saw a 0 bestfree entry */
337#endif
338
c2066e26 339 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
f5f3d9b0
DC
340 bf = xfs_dir3_data_bestfree_p(hdr);
341
1da177e4
LT
342#if defined(DEBUG) && defined(__KERNEL__)
343 /*
344 * Validate some consistency in the bestfree table.
345 * Check order, non-overlapping entries, and if we find the
346 * one we're looking for it has to be exact.
347 */
69ef921b 348 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
33363fee 349 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
f5f3d9b0
DC
350 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
351 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
352 for (dfp = &bf[0], seenzero = matched = 0;
353 dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
1da177e4
LT
354 dfp++) {
355 if (!dfp->offset) {
356 ASSERT(!dfp->length);
357 seenzero = 1;
358 continue;
359 }
360 ASSERT(seenzero == 0);
70e73f59 361 if (be16_to_cpu(dfp->offset) == off) {
1da177e4 362 matched = 1;
ad354eb3 363 ASSERT(dfp->length == dup->length);
70e73f59 364 } else if (off < be16_to_cpu(dfp->offset))
ad354eb3 365 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
1da177e4 366 else
70e73f59 367 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
ad354eb3 368 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
f5f3d9b0 369 if (dfp > &bf[0])
70e73f59 370 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
1da177e4
LT
371 }
372#endif
373 /*
374 * If this is smaller than the smallest bestfree entry,
375 * it can't be there since they're sorted.
376 */
ad354eb3 377 if (be16_to_cpu(dup->length) <
f5f3d9b0 378 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
1da177e4
LT
379 return NULL;
380 /*
381 * Look at the three bestfree entries for our guy.
382 */
f5f3d9b0 383 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
1da177e4
LT
384 if (!dfp->offset)
385 return NULL;
70e73f59 386 if (be16_to_cpu(dfp->offset) == off)
1da177e4
LT
387 return dfp;
388 }
389 /*
390 * Didn't find it. This only happens if there are duplicate lengths.
391 */
392 return NULL;
393}
394
395/*
396 * Insert an unused-space entry into the bestfree table.
397 */
398xfs_dir2_data_free_t * /* entry inserted */
399xfs_dir2_data_freeinsert(
c2066e26 400 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
1da177e4
LT
401 xfs_dir2_data_unused_t *dup, /* unused space */
402 int *loghead) /* log the data header (out) */
403{
404 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
405 xfs_dir2_data_free_t new; /* new bestfree entry */
406
69ef921b 407 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
f5f3d9b0
DC
408 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
409 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
410 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
411
412 dfp = xfs_dir3_data_bestfree_p(hdr);
70e73f59 413 new.length = dup->length;
c2066e26
CH
414 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
415
1da177e4
LT
416 /*
417 * Insert at position 0, 1, or 2; or not at all.
418 */
70e73f59 419 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
1da177e4
LT
420 dfp[2] = dfp[1];
421 dfp[1] = dfp[0];
422 dfp[0] = new;
423 *loghead = 1;
424 return &dfp[0];
425 }
70e73f59 426 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
1da177e4
LT
427 dfp[2] = dfp[1];
428 dfp[1] = new;
429 *loghead = 1;
430 return &dfp[1];
431 }
70e73f59 432 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
1da177e4
LT
433 dfp[2] = new;
434 *loghead = 1;
435 return &dfp[2];
436 }
437 return NULL;
438}
439
440/*
441 * Remove a bestfree entry from the table.
442 */
ba0f32d4 443STATIC void
1da177e4 444xfs_dir2_data_freeremove(
c2066e26 445 xfs_dir2_data_hdr_t *hdr, /* data block header */
1da177e4
LT
446 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
447 int *loghead) /* out: log data header */
448{
f5f3d9b0
DC
449 struct xfs_dir2_data_free *bf;
450
69ef921b 451 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
f5f3d9b0
DC
452 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
453 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
454 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
455
1da177e4
LT
456 /*
457 * It's the first entry, slide the next 2 up.
458 */
f5f3d9b0
DC
459 bf = xfs_dir3_data_bestfree_p(hdr);
460 if (dfp == &bf[0]) {
461 bf[0] = bf[1];
462 bf[1] = bf[2];
1da177e4
LT
463 }
464 /*
465 * It's the second entry, slide the 3rd entry up.
466 */
f5f3d9b0
DC
467 else if (dfp == &bf[1])
468 bf[1] = bf[2];
1da177e4
LT
469 /*
470 * Must be the last entry.
471 */
472 else
f5f3d9b0 473 ASSERT(dfp == &bf[2]);
1da177e4
LT
474 /*
475 * Clear the 3rd entry, must be zero now.
476 */
f5f3d9b0
DC
477 bf[2].length = 0;
478 bf[2].offset = 0;
1da177e4
LT
479 *loghead = 1;
480}
481
482/*
483 * Given a data block, reconstruct its bestfree map.
484 */
485void
486xfs_dir2_data_freescan(
487 xfs_mount_t *mp, /* filesystem mount point */
c2066e26 488 xfs_dir2_data_hdr_t *hdr, /* data block header */
ef497f8a 489 int *loghead) /* out: log data header */
1da177e4
LT
490{
491 xfs_dir2_block_tail_t *btp; /* block tail */
492 xfs_dir2_data_entry_t *dep; /* active data entry */
493 xfs_dir2_data_unused_t *dup; /* unused data entry */
f5f3d9b0 494 struct xfs_dir2_data_free *bf;
1da177e4
LT
495 char *endp; /* end of block's data */
496 char *p; /* current entry pointer */
497
69ef921b 498 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
33363fee 499 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
f5f3d9b0
DC
500 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
501 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
502
1da177e4
LT
503 /*
504 * Start by clearing the table.
505 */
f5f3d9b0
DC
506 bf = xfs_dir3_data_bestfree_p(hdr);
507 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
1da177e4
LT
508 *loghead = 1;
509 /*
510 * Set up pointers.
511 */
f5f3d9b0
DC
512 p = (char *)xfs_dir3_data_entry_p(hdr);
513 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
514 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
c2066e26 515 btp = xfs_dir2_block_tail_p(mp, hdr);
bbaaf538 516 endp = (char *)xfs_dir2_block_leaf_p(btp);
1da177e4 517 } else
c2066e26 518 endp = (char *)hdr + mp->m_dirblksize;
1da177e4
LT
519 /*
520 * Loop over the block's entries.
521 */
522 while (p < endp) {
523 dup = (xfs_dir2_data_unused_t *)p;
524 /*
525 * If it's a free entry, insert it.
526 */
ad354eb3 527 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
c2066e26 528 ASSERT((char *)dup - (char *)hdr ==
bbaaf538 529 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
c2066e26 530 xfs_dir2_data_freeinsert(hdr, dup, loghead);
ad354eb3 531 p += be16_to_cpu(dup->length);
1da177e4
LT
532 }
533 /*
534 * For active entries, check their tags and skip them.
535 */
536 else {
537 dep = (xfs_dir2_data_entry_t *)p;
c2066e26 538 ASSERT((char *)dep - (char *)hdr ==
bbaaf538
CH
539 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
540 p += xfs_dir2_data_entsize(dep->namelen);
1da177e4
LT
541 }
542 }
543}
544
545/*
546 * Initialize a data block at the given block number in the directory.
547 * Give back the buffer for the created block.
548 */
549int /* error */
f5f3d9b0 550xfs_dir3_data_init(
1da177e4
LT
551 xfs_da_args_t *args, /* directory operation args */
552 xfs_dir2_db_t blkno, /* logical dir block number */
1d9025e5 553 struct xfs_buf **bpp) /* output block buffer */
1da177e4 554{
1d9025e5 555 struct xfs_buf *bp; /* block buffer */
c2066e26 556 xfs_dir2_data_hdr_t *hdr; /* data block header */
1da177e4
LT
557 xfs_inode_t *dp; /* incore directory inode */
558 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
f5f3d9b0 559 struct xfs_dir2_data_free *bf;
1da177e4
LT
560 int error; /* error return value */
561 int i; /* bestfree index */
562 xfs_mount_t *mp; /* filesystem mount point */
563 xfs_trans_t *tp; /* transaction pointer */
564 int t; /* temp */
565
566 dp = args->dp;
567 mp = dp->i_mount;
568 tp = args->trans;
569 /*
570 * Get the buffer set up for the block.
571 */
bbaaf538 572 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
1da177e4 573 XFS_DATA_FORK);
b0f539de 574 if (error)
1da177e4 575 return error;
33363fee 576 bp->b_ops = &xfs_dir3_data_buf_ops;
61fe135c 577 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
c2066e26 578
1da177e4
LT
579 /*
580 * Initialize the header.
581 */
1d9025e5 582 hdr = bp->b_addr;
f5f3d9b0
DC
583 if (xfs_sb_version_hascrc(&mp->m_sb)) {
584 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
585
586 memset(hdr3, 0, sizeof(*hdr3));
587 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
588 hdr3->blkno = cpu_to_be64(bp->b_bn);
589 hdr3->owner = cpu_to_be64(dp->i_ino);
590 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
591
592 } else
593 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
594
595 bf = xfs_dir3_data_bestfree_p(hdr);
596 bf[0].offset = cpu_to_be16(xfs_dir3_data_entry_offset(hdr));
1da177e4 597 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
f5f3d9b0
DC
598 bf[i].length = 0;
599 bf[i].offset = 0;
1da177e4 600 }
c2066e26 601
1da177e4
LT
602 /*
603 * Set up an unused entry for the block's body.
604 */
f5f3d9b0 605 dup = xfs_dir3_data_unused_p(hdr);
ad354eb3 606 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1da177e4 607
f5f3d9b0
DC
608 t = mp->m_dirblksize - (uint)xfs_dir3_data_entry_offset(hdr);
609 bf[0].length = cpu_to_be16(t);
ad354eb3 610 dup->length = cpu_to_be16(t);
c2066e26 611 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
1da177e4
LT
612 /*
613 * Log it and return it.
614 */
615 xfs_dir2_data_log_header(tp, bp);
616 xfs_dir2_data_log_unused(tp, bp, dup);
617 *bpp = bp;
618 return 0;
619}
620
621/*
622 * Log an active data entry from the block.
623 */
624void
625xfs_dir2_data_log_entry(
1d9025e5
DC
626 struct xfs_trans *tp,
627 struct xfs_buf *bp,
1da177e4
LT
628 xfs_dir2_data_entry_t *dep) /* data entry pointer */
629{
1d9025e5 630 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
1da177e4 631
69ef921b 632 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
33363fee 633 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
f5f3d9b0
DC
634 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
635 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
c2066e26 636
1d9025e5 637 xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
bbaaf538 638 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
c2066e26 639 (char *)hdr - 1));
1da177e4
LT
640}
641
642/*
643 * Log a data block header.
644 */
645void
646xfs_dir2_data_log_header(
1d9025e5
DC
647 struct xfs_trans *tp,
648 struct xfs_buf *bp)
1da177e4 649{
1d9025e5 650 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
1da177e4 651
69ef921b 652 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
33363fee 653 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
f5f3d9b0
DC
654 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
655 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
c2066e26 656
f5f3d9b0 657 xfs_trans_log_buf(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1);
1da177e4
LT
658}
659
660/*
661 * Log a data unused entry.
662 */
663void
664xfs_dir2_data_log_unused(
1d9025e5
DC
665 struct xfs_trans *tp,
666 struct xfs_buf *bp,
1da177e4
LT
667 xfs_dir2_data_unused_t *dup) /* data unused pointer */
668{
1d9025e5 669 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
c2066e26 670
69ef921b 671 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
33363fee 672 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
f5f3d9b0
DC
673 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
674 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
1da177e4 675
1da177e4
LT
676 /*
677 * Log the first part of the unused entry.
678 */
1d9025e5 679 xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
1da177e4 680 (uint)((char *)&dup->length + sizeof(dup->length) -
c2066e26 681 1 - (char *)hdr));
1da177e4
LT
682 /*
683 * Log the end (tag) of the unused entry.
684 */
1d9025e5 685 xfs_trans_log_buf(tp, bp,
c2066e26
CH
686 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
687 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
1da177e4
LT
688 sizeof(xfs_dir2_data_off_t) - 1));
689}
690
691/*
692 * Make a byte range in the data block unused.
693 * Its current contents are unimportant.
694 */
695void
696xfs_dir2_data_make_free(
1d9025e5
DC
697 struct xfs_trans *tp,
698 struct xfs_buf *bp,
1da177e4
LT
699 xfs_dir2_data_aoff_t offset, /* starting byte offset */
700 xfs_dir2_data_aoff_t len, /* length in bytes */
701 int *needlogp, /* out: log header */
702 int *needscanp) /* out: regen bestfree */
703{
c2066e26 704 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
1da177e4
LT
705 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
706 char *endptr; /* end of data area */
707 xfs_mount_t *mp; /* filesystem mount point */
708 int needscan; /* need to regen bestfree */
709 xfs_dir2_data_unused_t *newdup; /* new unused entry */
710 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
711 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
f5f3d9b0 712 struct xfs_dir2_data_free *bf;
1da177e4
LT
713
714 mp = tp->t_mountp;
1d9025e5 715 hdr = bp->b_addr;
c2066e26 716
1da177e4
LT
717 /*
718 * Figure out where the end of the data area is.
719 */
33363fee
DC
720 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
721 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
c2066e26 722 endptr = (char *)hdr + mp->m_dirblksize;
1da177e4
LT
723 else {
724 xfs_dir2_block_tail_t *btp; /* block tail */
725
f5f3d9b0
DC
726 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
727 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
c2066e26 728 btp = xfs_dir2_block_tail_p(mp, hdr);
bbaaf538 729 endptr = (char *)xfs_dir2_block_leaf_p(btp);
1da177e4
LT
730 }
731 /*
732 * If this isn't the start of the block, then back up to
733 * the previous entry and see if it's free.
734 */
f5f3d9b0 735 if (offset > xfs_dir3_data_entry_offset(hdr)) {
3d693c6e 736 __be16 *tagp; /* tag just before us */
1da177e4 737
c2066e26
CH
738 tagp = (__be16 *)((char *)hdr + offset) - 1;
739 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
ad354eb3 740 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
1da177e4
LT
741 prevdup = NULL;
742 } else
743 prevdup = NULL;
744 /*
745 * If this isn't the end of the block, see if the entry after
746 * us is free.
747 */
c2066e26 748 if ((char *)hdr + offset + len < endptr) {
1da177e4 749 postdup =
c2066e26 750 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
ad354eb3 751 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
1da177e4
LT
752 postdup = NULL;
753 } else
754 postdup = NULL;
755 ASSERT(*needscanp == 0);
756 needscan = 0;
757 /*
758 * Previous and following entries are both free,
759 * merge everything into a single free entry.
760 */
f5f3d9b0 761 bf = xfs_dir3_data_bestfree_p(hdr);
1da177e4
LT
762 if (prevdup && postdup) {
763 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
764
765 /*
766 * See if prevdup and/or postdup are in bestfree table.
767 */
c2066e26
CH
768 dfp = xfs_dir2_data_freefind(hdr, prevdup);
769 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
1da177e4
LT
770 /*
771 * We need a rescan unless there are exactly 2 free entries
772 * namely our two. Then we know what's happening, otherwise
773 * since the third bestfree is there, there might be more
774 * entries.
775 */
f5f3d9b0 776 needscan = (bf[2].length != 0);
1da177e4
LT
777 /*
778 * Fix up the new big freespace.
779 */
413d57c9 780 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
bbaaf538 781 *xfs_dir2_data_unused_tag_p(prevdup) =
c2066e26 782 cpu_to_be16((char *)prevdup - (char *)hdr);
1da177e4
LT
783 xfs_dir2_data_log_unused(tp, bp, prevdup);
784 if (!needscan) {
785 /*
786 * Has to be the case that entries 0 and 1 are
787 * dfp and dfp2 (don't know which is which), and
788 * entry 2 is empty.
789 * Remove entry 1 first then entry 0.
790 */
791 ASSERT(dfp && dfp2);
f5f3d9b0
DC
792 if (dfp == &bf[1]) {
793 dfp = &bf[0];
1da177e4 794 ASSERT(dfp2 == dfp);
f5f3d9b0 795 dfp2 = &bf[1];
1da177e4 796 }
c2066e26
CH
797 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
798 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
1da177e4
LT
799 /*
800 * Now insert the new entry.
801 */
c2066e26 802 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
f5f3d9b0 803 ASSERT(dfp == &bf[0]);
ad354eb3 804 ASSERT(dfp->length == prevdup->length);
1da177e4
LT
805 ASSERT(!dfp[1].length);
806 ASSERT(!dfp[2].length);
807 }
808 }
809 /*
810 * The entry before us is free, merge with it.
811 */
812 else if (prevdup) {
c2066e26 813 dfp = xfs_dir2_data_freefind(hdr, prevdup);
413d57c9 814 be16_add_cpu(&prevdup->length, len);
bbaaf538 815 *xfs_dir2_data_unused_tag_p(prevdup) =
c2066e26 816 cpu_to_be16((char *)prevdup - (char *)hdr);
1da177e4
LT
817 xfs_dir2_data_log_unused(tp, bp, prevdup);
818 /*
819 * If the previous entry was in the table, the new entry
820 * is longer, so it will be in the table too. Remove
821 * the old one and add the new one.
822 */
823 if (dfp) {
c2066e26
CH
824 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
825 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
1da177e4
LT
826 }
827 /*
828 * Otherwise we need a scan if the new entry is big enough.
829 */
70e73f59 830 else {
ad354eb3 831 needscan = be16_to_cpu(prevdup->length) >
f5f3d9b0 832 be16_to_cpu(bf[2].length);
70e73f59 833 }
1da177e4
LT
834 }
835 /*
836 * The following entry is free, merge with it.
837 */
838 else if (postdup) {
c2066e26
CH
839 dfp = xfs_dir2_data_freefind(hdr, postdup);
840 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
ad354eb3
NS
841 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
842 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
bbaaf538 843 *xfs_dir2_data_unused_tag_p(newdup) =
c2066e26 844 cpu_to_be16((char *)newdup - (char *)hdr);
1da177e4
LT
845 xfs_dir2_data_log_unused(tp, bp, newdup);
846 /*
847 * If the following entry was in the table, the new entry
848 * is longer, so it will be in the table too. Remove
849 * the old one and add the new one.
850 */
851 if (dfp) {
c2066e26
CH
852 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
853 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
1da177e4
LT
854 }
855 /*
856 * Otherwise we need a scan if the new entry is big enough.
857 */
70e73f59 858 else {
ad354eb3 859 needscan = be16_to_cpu(newdup->length) >
f5f3d9b0 860 be16_to_cpu(bf[2].length);
70e73f59 861 }
1da177e4
LT
862 }
863 /*
864 * Neither neighbor is free. Make a new entry.
865 */
866 else {
c2066e26 867 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
ad354eb3
NS
868 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
869 newdup->length = cpu_to_be16(len);
bbaaf538 870 *xfs_dir2_data_unused_tag_p(newdup) =
c2066e26 871 cpu_to_be16((char *)newdup - (char *)hdr);
1da177e4 872 xfs_dir2_data_log_unused(tp, bp, newdup);
c2066e26 873 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
1da177e4
LT
874 }
875 *needscanp = needscan;
876}
877
878/*
879 * Take a byte range out of an existing unused space and make it un-free.
880 */
881void
882xfs_dir2_data_use_free(
1d9025e5
DC
883 struct xfs_trans *tp,
884 struct xfs_buf *bp,
1da177e4
LT
885 xfs_dir2_data_unused_t *dup, /* unused entry */
886 xfs_dir2_data_aoff_t offset, /* starting offset to use */
887 xfs_dir2_data_aoff_t len, /* length to use */
888 int *needlogp, /* out: need to log header */
889 int *needscanp) /* out: need regen bestfree */
890{
c2066e26 891 xfs_dir2_data_hdr_t *hdr; /* data block header */
1da177e4
LT
892 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
893 int matchback; /* matches end of freespace */
894 int matchfront; /* matches start of freespace */
895 int needscan; /* need to regen bestfree */
896 xfs_dir2_data_unused_t *newdup; /* new unused entry */
897 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
898 int oldlen; /* old unused entry's length */
f5f3d9b0 899 struct xfs_dir2_data_free *bf;
1da177e4 900
1d9025e5 901 hdr = bp->b_addr;
69ef921b 902 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
33363fee 903 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
f5f3d9b0
DC
904 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
905 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
ad354eb3 906 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
c2066e26
CH
907 ASSERT(offset >= (char *)dup - (char *)hdr);
908 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
909 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
1da177e4
LT
910 /*
911 * Look up the entry in the bestfree table.
912 */
c2066e26 913 dfp = xfs_dir2_data_freefind(hdr, dup);
ad354eb3 914 oldlen = be16_to_cpu(dup->length);
f5f3d9b0
DC
915 bf = xfs_dir3_data_bestfree_p(hdr);
916 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
1da177e4
LT
917 /*
918 * Check for alignment with front and back of the entry.
919 */
c2066e26
CH
920 matchfront = (char *)dup - (char *)hdr == offset;
921 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
1da177e4
LT
922 ASSERT(*needscanp == 0);
923 needscan = 0;
924 /*
925 * If we matched it exactly we just need to get rid of it from
926 * the bestfree table.
927 */
928 if (matchfront && matchback) {
929 if (dfp) {
f5f3d9b0 930 needscan = (bf[2].offset != 0);
1da177e4 931 if (!needscan)
c2066e26 932 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
1da177e4
LT
933 }
934 }
935 /*
936 * We match the first part of the entry.
937 * Make a new entry with the remaining freespace.
938 */
939 else if (matchfront) {
c2066e26 940 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
ad354eb3
NS
941 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
942 newdup->length = cpu_to_be16(oldlen - len);
bbaaf538 943 *xfs_dir2_data_unused_tag_p(newdup) =
c2066e26 944 cpu_to_be16((char *)newdup - (char *)hdr);
1da177e4
LT
945 xfs_dir2_data_log_unused(tp, bp, newdup);
946 /*
947 * If it was in the table, remove it and add the new one.
948 */
949 if (dfp) {
c2066e26
CH
950 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
951 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
1da177e4 952 ASSERT(dfp != NULL);
ad354eb3 953 ASSERT(dfp->length == newdup->length);
c2066e26 954 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
1da177e4
LT
955 /*
956 * If we got inserted at the last slot,
957 * that means we don't know if there was a better
958 * choice for the last slot, or not. Rescan.
959 */
f5f3d9b0 960 needscan = dfp == &bf[2];
1da177e4
LT
961 }
962 }
963 /*
964 * We match the last part of the entry.
965 * Trim the allocated space off the tail of the entry.
966 */
967 else if (matchback) {
968 newdup = dup;
c2066e26 969 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
bbaaf538 970 *xfs_dir2_data_unused_tag_p(newdup) =
c2066e26 971 cpu_to_be16((char *)newdup - (char *)hdr);
1da177e4
LT
972 xfs_dir2_data_log_unused(tp, bp, newdup);
973 /*
974 * If it was in the table, remove it and add the new one.
975 */
976 if (dfp) {
c2066e26
CH
977 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
978 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
1da177e4 979 ASSERT(dfp != NULL);
ad354eb3 980 ASSERT(dfp->length == newdup->length);
c2066e26 981 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
1da177e4
LT
982 /*
983 * If we got inserted at the last slot,
984 * that means we don't know if there was a better
985 * choice for the last slot, or not. Rescan.
986 */
f5f3d9b0 987 needscan = dfp == &bf[2];
1da177e4
LT
988 }
989 }
990 /*
991 * Poking out the middle of an entry.
992 * Make two new entries.
993 */
994 else {
995 newdup = dup;
c2066e26 996 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
bbaaf538 997 *xfs_dir2_data_unused_tag_p(newdup) =
c2066e26 998 cpu_to_be16((char *)newdup - (char *)hdr);
1da177e4 999 xfs_dir2_data_log_unused(tp, bp, newdup);
c2066e26 1000 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
ad354eb3
NS
1001 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1002 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
bbaaf538 1003 *xfs_dir2_data_unused_tag_p(newdup2) =
c2066e26 1004 cpu_to_be16((char *)newdup2 - (char *)hdr);
1da177e4
LT
1005 xfs_dir2_data_log_unused(tp, bp, newdup2);
1006 /*
1007 * If the old entry was in the table, we need to scan
1008 * if the 3rd entry was valid, since these entries
1009 * are smaller than the old one.
1010 * If we don't need to scan that means there were 1 or 2
1011 * entries in the table, and removing the old and adding
1012 * the 2 new will work.
1013 */
1014 if (dfp) {
f5f3d9b0 1015 needscan = (bf[2].length != 0);
1da177e4 1016 if (!needscan) {
c2066e26
CH
1017 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
1018 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
1019 xfs_dir2_data_freeinsert(hdr, newdup2,
1020 needlogp);
1da177e4
LT
1021 }
1022 }
1023 }
1024 *needscanp = needscan;
1025}
This page took 0.68312 seconds and 5 git commands to generate.