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