]>
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 | 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 | 43 | int |
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( | |
0cb97766 | 150 | be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp, dep)) == |
82025d7f | 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 | } |
0cb97766 | 169 | p += xfs_dir3_data_entsize(mp, 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 |
193 | static bool |
194 | xfs_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 | */ | |
221 | static void | |
33363fee | 222 | xfs_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 | ||
245 | static void | |
33363fee | 246 | xfs_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 | 260 | static void |
33363fee | 261 | xfs_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 |
283 | const 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 |
288 | static 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 | 294 | int |
33363fee | 295 | xfs_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 | 311 | int |
33363fee | 312 | xfs_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 | 326 | xfs_dir2_data_free_t * |
1da177e4 | 327 | xfs_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; |
b49a0c18 | 334 | #ifdef DEBUG |
1da177e4 LT |
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 | ||
b49a0c18 | 342 | #ifdef DEBUG |
1da177e4 LT |
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 | */ | |
398 | xfs_dir2_data_free_t * /* entry inserted */ | |
399 | xfs_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 | 443 | STATIC void |
1da177e4 | 444 | xfs_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 | */ | |
485 | void | |
486 | xfs_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 == |
0cb97766 DC |
539 | be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp, dep))); |
540 | p += xfs_dir3_data_entsize(mp, 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 | */ | |
549 | int /* error */ | |
f5f3d9b0 | 550 | xfs_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 | */ | |
624 | void | |
625 | xfs_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 | { | |
0cb97766 DC |
630 | struct xfs_dir2_data_hdr *hdr = bp->b_addr; |
631 | struct xfs_mount *mp = tp->t_mountp; | |
1da177e4 | 632 | |
69ef921b | 633 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
33363fee | 634 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
f5f3d9b0 DC |
635 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
636 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); | |
c2066e26 | 637 | |
1d9025e5 | 638 | xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr), |
0cb97766 | 639 | (uint)((char *)(xfs_dir3_data_entry_tag_p(mp, dep) + 1) - |
c2066e26 | 640 | (char *)hdr - 1)); |
1da177e4 LT |
641 | } |
642 | ||
643 | /* | |
644 | * Log a data block header. | |
645 | */ | |
646 | void | |
647 | xfs_dir2_data_log_header( | |
1d9025e5 DC |
648 | struct xfs_trans *tp, |
649 | struct xfs_buf *bp) | |
1da177e4 | 650 | { |
1d9025e5 | 651 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
1da177e4 | 652 | |
69ef921b | 653 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
33363fee | 654 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
f5f3d9b0 DC |
655 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
656 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); | |
c2066e26 | 657 | |
f5f3d9b0 | 658 | xfs_trans_log_buf(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1); |
1da177e4 LT |
659 | } |
660 | ||
661 | /* | |
662 | * Log a data unused entry. | |
663 | */ | |
664 | void | |
665 | xfs_dir2_data_log_unused( | |
1d9025e5 DC |
666 | struct xfs_trans *tp, |
667 | struct xfs_buf *bp, | |
1da177e4 LT |
668 | xfs_dir2_data_unused_t *dup) /* data unused pointer */ |
669 | { | |
1d9025e5 | 670 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
c2066e26 | 671 | |
69ef921b | 672 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
33363fee | 673 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
f5f3d9b0 DC |
674 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
675 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); | |
1da177e4 | 676 | |
1da177e4 LT |
677 | /* |
678 | * Log the first part of the unused entry. | |
679 | */ | |
1d9025e5 | 680 | xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr), |
1da177e4 | 681 | (uint)((char *)&dup->length + sizeof(dup->length) - |
c2066e26 | 682 | 1 - (char *)hdr)); |
1da177e4 LT |
683 | /* |
684 | * Log the end (tag) of the unused entry. | |
685 | */ | |
1d9025e5 | 686 | xfs_trans_log_buf(tp, bp, |
c2066e26 CH |
687 | (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr), |
688 | (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr + | |
1da177e4 LT |
689 | sizeof(xfs_dir2_data_off_t) - 1)); |
690 | } | |
691 | ||
692 | /* | |
693 | * Make a byte range in the data block unused. | |
694 | * Its current contents are unimportant. | |
695 | */ | |
696 | void | |
697 | xfs_dir2_data_make_free( | |
1d9025e5 DC |
698 | struct xfs_trans *tp, |
699 | struct xfs_buf *bp, | |
1da177e4 LT |
700 | xfs_dir2_data_aoff_t offset, /* starting byte offset */ |
701 | xfs_dir2_data_aoff_t len, /* length in bytes */ | |
702 | int *needlogp, /* out: log header */ | |
703 | int *needscanp) /* out: regen bestfree */ | |
704 | { | |
c2066e26 | 705 | xfs_dir2_data_hdr_t *hdr; /* data block pointer */ |
1da177e4 LT |
706 | xfs_dir2_data_free_t *dfp; /* bestfree pointer */ |
707 | char *endptr; /* end of data area */ | |
708 | xfs_mount_t *mp; /* filesystem mount point */ | |
709 | int needscan; /* need to regen bestfree */ | |
710 | xfs_dir2_data_unused_t *newdup; /* new unused entry */ | |
711 | xfs_dir2_data_unused_t *postdup; /* unused entry after us */ | |
712 | xfs_dir2_data_unused_t *prevdup; /* unused entry before us */ | |
f5f3d9b0 | 713 | struct xfs_dir2_data_free *bf; |
1da177e4 LT |
714 | |
715 | mp = tp->t_mountp; | |
1d9025e5 | 716 | hdr = bp->b_addr; |
c2066e26 | 717 | |
1da177e4 LT |
718 | /* |
719 | * Figure out where the end of the data area is. | |
720 | */ | |
33363fee DC |
721 | if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
722 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)) | |
c2066e26 | 723 | endptr = (char *)hdr + mp->m_dirblksize; |
1da177e4 LT |
724 | else { |
725 | xfs_dir2_block_tail_t *btp; /* block tail */ | |
726 | ||
f5f3d9b0 DC |
727 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
728 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); | |
c2066e26 | 729 | btp = xfs_dir2_block_tail_p(mp, hdr); |
bbaaf538 | 730 | endptr = (char *)xfs_dir2_block_leaf_p(btp); |
1da177e4 LT |
731 | } |
732 | /* | |
733 | * If this isn't the start of the block, then back up to | |
734 | * the previous entry and see if it's free. | |
735 | */ | |
f5f3d9b0 | 736 | if (offset > xfs_dir3_data_entry_offset(hdr)) { |
3d693c6e | 737 | __be16 *tagp; /* tag just before us */ |
1da177e4 | 738 | |
c2066e26 CH |
739 | tagp = (__be16 *)((char *)hdr + offset) - 1; |
740 | prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); | |
ad354eb3 | 741 | if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG) |
1da177e4 LT |
742 | prevdup = NULL; |
743 | } else | |
744 | prevdup = NULL; | |
745 | /* | |
746 | * If this isn't the end of the block, see if the entry after | |
747 | * us is free. | |
748 | */ | |
c2066e26 | 749 | if ((char *)hdr + offset + len < endptr) { |
1da177e4 | 750 | postdup = |
c2066e26 | 751 | (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); |
ad354eb3 | 752 | if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG) |
1da177e4 LT |
753 | postdup = NULL; |
754 | } else | |
755 | postdup = NULL; | |
756 | ASSERT(*needscanp == 0); | |
757 | needscan = 0; | |
758 | /* | |
759 | * Previous and following entries are both free, | |
760 | * merge everything into a single free entry. | |
761 | */ | |
f5f3d9b0 | 762 | bf = xfs_dir3_data_bestfree_p(hdr); |
1da177e4 LT |
763 | if (prevdup && postdup) { |
764 | xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */ | |
765 | ||
766 | /* | |
767 | * See if prevdup and/or postdup are in bestfree table. | |
768 | */ | |
c2066e26 CH |
769 | dfp = xfs_dir2_data_freefind(hdr, prevdup); |
770 | dfp2 = xfs_dir2_data_freefind(hdr, postdup); | |
1da177e4 LT |
771 | /* |
772 | * We need a rescan unless there are exactly 2 free entries | |
773 | * namely our two. Then we know what's happening, otherwise | |
774 | * since the third bestfree is there, there might be more | |
775 | * entries. | |
776 | */ | |
f5f3d9b0 | 777 | needscan = (bf[2].length != 0); |
1da177e4 LT |
778 | /* |
779 | * Fix up the new big freespace. | |
780 | */ | |
413d57c9 | 781 | be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length)); |
bbaaf538 | 782 | *xfs_dir2_data_unused_tag_p(prevdup) = |
c2066e26 | 783 | cpu_to_be16((char *)prevdup - (char *)hdr); |
1da177e4 LT |
784 | xfs_dir2_data_log_unused(tp, bp, prevdup); |
785 | if (!needscan) { | |
786 | /* | |
787 | * Has to be the case that entries 0 and 1 are | |
788 | * dfp and dfp2 (don't know which is which), and | |
789 | * entry 2 is empty. | |
790 | * Remove entry 1 first then entry 0. | |
791 | */ | |
792 | ASSERT(dfp && dfp2); | |
f5f3d9b0 DC |
793 | if (dfp == &bf[1]) { |
794 | dfp = &bf[0]; | |
1da177e4 | 795 | ASSERT(dfp2 == dfp); |
f5f3d9b0 | 796 | dfp2 = &bf[1]; |
1da177e4 | 797 | } |
c2066e26 CH |
798 | xfs_dir2_data_freeremove(hdr, dfp2, needlogp); |
799 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); | |
1da177e4 LT |
800 | /* |
801 | * Now insert the new entry. | |
802 | */ | |
c2066e26 | 803 | dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp); |
f5f3d9b0 | 804 | ASSERT(dfp == &bf[0]); |
ad354eb3 | 805 | ASSERT(dfp->length == prevdup->length); |
1da177e4 LT |
806 | ASSERT(!dfp[1].length); |
807 | ASSERT(!dfp[2].length); | |
808 | } | |
809 | } | |
810 | /* | |
811 | * The entry before us is free, merge with it. | |
812 | */ | |
813 | else if (prevdup) { | |
c2066e26 | 814 | dfp = xfs_dir2_data_freefind(hdr, prevdup); |
413d57c9 | 815 | be16_add_cpu(&prevdup->length, len); |
bbaaf538 | 816 | *xfs_dir2_data_unused_tag_p(prevdup) = |
c2066e26 | 817 | cpu_to_be16((char *)prevdup - (char *)hdr); |
1da177e4 LT |
818 | xfs_dir2_data_log_unused(tp, bp, prevdup); |
819 | /* | |
820 | * If the previous entry was in the table, the new entry | |
821 | * is longer, so it will be in the table too. Remove | |
822 | * the old one and add the new one. | |
823 | */ | |
824 | if (dfp) { | |
c2066e26 CH |
825 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
826 | xfs_dir2_data_freeinsert(hdr, prevdup, needlogp); | |
1da177e4 LT |
827 | } |
828 | /* | |
829 | * Otherwise we need a scan if the new entry is big enough. | |
830 | */ | |
70e73f59 | 831 | else { |
ad354eb3 | 832 | needscan = be16_to_cpu(prevdup->length) > |
f5f3d9b0 | 833 | be16_to_cpu(bf[2].length); |
70e73f59 | 834 | } |
1da177e4 LT |
835 | } |
836 | /* | |
837 | * The following entry is free, merge with it. | |
838 | */ | |
839 | else if (postdup) { | |
c2066e26 CH |
840 | dfp = xfs_dir2_data_freefind(hdr, postdup); |
841 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); | |
ad354eb3 NS |
842 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
843 | newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length)); | |
bbaaf538 | 844 | *xfs_dir2_data_unused_tag_p(newdup) = |
c2066e26 | 845 | cpu_to_be16((char *)newdup - (char *)hdr); |
1da177e4 LT |
846 | xfs_dir2_data_log_unused(tp, bp, newdup); |
847 | /* | |
848 | * If the following entry was in the table, the new entry | |
849 | * is longer, so it will be in the table too. Remove | |
850 | * the old one and add the new one. | |
851 | */ | |
852 | if (dfp) { | |
c2066e26 CH |
853 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
854 | xfs_dir2_data_freeinsert(hdr, newdup, needlogp); | |
1da177e4 LT |
855 | } |
856 | /* | |
857 | * Otherwise we need a scan if the new entry is big enough. | |
858 | */ | |
70e73f59 | 859 | else { |
ad354eb3 | 860 | needscan = be16_to_cpu(newdup->length) > |
f5f3d9b0 | 861 | be16_to_cpu(bf[2].length); |
70e73f59 | 862 | } |
1da177e4 LT |
863 | } |
864 | /* | |
865 | * Neither neighbor is free. Make a new entry. | |
866 | */ | |
867 | else { | |
c2066e26 | 868 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); |
ad354eb3 NS |
869 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
870 | newdup->length = cpu_to_be16(len); | |
bbaaf538 | 871 | *xfs_dir2_data_unused_tag_p(newdup) = |
c2066e26 | 872 | cpu_to_be16((char *)newdup - (char *)hdr); |
1da177e4 | 873 | xfs_dir2_data_log_unused(tp, bp, newdup); |
c2066e26 | 874 | xfs_dir2_data_freeinsert(hdr, newdup, needlogp); |
1da177e4 LT |
875 | } |
876 | *needscanp = needscan; | |
877 | } | |
878 | ||
879 | /* | |
880 | * Take a byte range out of an existing unused space and make it un-free. | |
881 | */ | |
882 | void | |
883 | xfs_dir2_data_use_free( | |
1d9025e5 DC |
884 | struct xfs_trans *tp, |
885 | struct xfs_buf *bp, | |
1da177e4 LT |
886 | xfs_dir2_data_unused_t *dup, /* unused entry */ |
887 | xfs_dir2_data_aoff_t offset, /* starting offset to use */ | |
888 | xfs_dir2_data_aoff_t len, /* length to use */ | |
889 | int *needlogp, /* out: need to log header */ | |
890 | int *needscanp) /* out: need regen bestfree */ | |
891 | { | |
c2066e26 | 892 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
1da177e4 LT |
893 | xfs_dir2_data_free_t *dfp; /* bestfree pointer */ |
894 | int matchback; /* matches end of freespace */ | |
895 | int matchfront; /* matches start of freespace */ | |
896 | int needscan; /* need to regen bestfree */ | |
897 | xfs_dir2_data_unused_t *newdup; /* new unused entry */ | |
898 | xfs_dir2_data_unused_t *newdup2; /* another new unused entry */ | |
899 | int oldlen; /* old unused entry's length */ | |
f5f3d9b0 | 900 | struct xfs_dir2_data_free *bf; |
1da177e4 | 901 | |
1d9025e5 | 902 | hdr = bp->b_addr; |
69ef921b | 903 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
33363fee | 904 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
f5f3d9b0 DC |
905 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
906 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); | |
ad354eb3 | 907 | ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG); |
c2066e26 CH |
908 | ASSERT(offset >= (char *)dup - (char *)hdr); |
909 | ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr); | |
910 | ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup))); | |
1da177e4 LT |
911 | /* |
912 | * Look up the entry in the bestfree table. | |
913 | */ | |
c2066e26 | 914 | dfp = xfs_dir2_data_freefind(hdr, dup); |
ad354eb3 | 915 | oldlen = be16_to_cpu(dup->length); |
f5f3d9b0 DC |
916 | bf = xfs_dir3_data_bestfree_p(hdr); |
917 | ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length)); | |
1da177e4 LT |
918 | /* |
919 | * Check for alignment with front and back of the entry. | |
920 | */ | |
c2066e26 CH |
921 | matchfront = (char *)dup - (char *)hdr == offset; |
922 | matchback = (char *)dup + oldlen - (char *)hdr == offset + len; | |
1da177e4 LT |
923 | ASSERT(*needscanp == 0); |
924 | needscan = 0; | |
925 | /* | |
926 | * If we matched it exactly we just need to get rid of it from | |
927 | * the bestfree table. | |
928 | */ | |
929 | if (matchfront && matchback) { | |
930 | if (dfp) { | |
f5f3d9b0 | 931 | needscan = (bf[2].offset != 0); |
1da177e4 | 932 | if (!needscan) |
c2066e26 | 933 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
1da177e4 LT |
934 | } |
935 | } | |
936 | /* | |
937 | * We match the first part of the entry. | |
938 | * Make a new entry with the remaining freespace. | |
939 | */ | |
940 | else if (matchfront) { | |
c2066e26 | 941 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); |
ad354eb3 NS |
942 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
943 | newdup->length = cpu_to_be16(oldlen - len); | |
bbaaf538 | 944 | *xfs_dir2_data_unused_tag_p(newdup) = |
c2066e26 | 945 | cpu_to_be16((char *)newdup - (char *)hdr); |
1da177e4 LT |
946 | xfs_dir2_data_log_unused(tp, bp, newdup); |
947 | /* | |
948 | * If it was in the table, remove it and add the new one. | |
949 | */ | |
950 | if (dfp) { | |
c2066e26 CH |
951 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
952 | dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp); | |
1da177e4 | 953 | ASSERT(dfp != NULL); |
ad354eb3 | 954 | ASSERT(dfp->length == newdup->length); |
c2066e26 | 955 | ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr); |
1da177e4 LT |
956 | /* |
957 | * If we got inserted at the last slot, | |
958 | * that means we don't know if there was a better | |
959 | * choice for the last slot, or not. Rescan. | |
960 | */ | |
f5f3d9b0 | 961 | needscan = dfp == &bf[2]; |
1da177e4 LT |
962 | } |
963 | } | |
964 | /* | |
965 | * We match the last part of the entry. | |
966 | * Trim the allocated space off the tail of the entry. | |
967 | */ | |
968 | else if (matchback) { | |
969 | newdup = dup; | |
c2066e26 | 970 | newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup); |
bbaaf538 | 971 | *xfs_dir2_data_unused_tag_p(newdup) = |
c2066e26 | 972 | cpu_to_be16((char *)newdup - (char *)hdr); |
1da177e4 LT |
973 | xfs_dir2_data_log_unused(tp, bp, newdup); |
974 | /* | |
975 | * If it was in the table, remove it and add the new one. | |
976 | */ | |
977 | if (dfp) { | |
c2066e26 CH |
978 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
979 | dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp); | |
1da177e4 | 980 | ASSERT(dfp != NULL); |
ad354eb3 | 981 | ASSERT(dfp->length == newdup->length); |
c2066e26 | 982 | ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr); |
1da177e4 LT |
983 | /* |
984 | * If we got inserted at the last slot, | |
985 | * that means we don't know if there was a better | |
986 | * choice for the last slot, or not. Rescan. | |
987 | */ | |
f5f3d9b0 | 988 | needscan = dfp == &bf[2]; |
1da177e4 LT |
989 | } |
990 | } | |
991 | /* | |
992 | * Poking out the middle of an entry. | |
993 | * Make two new entries. | |
994 | */ | |
995 | else { | |
996 | newdup = dup; | |
c2066e26 | 997 | newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup); |
bbaaf538 | 998 | *xfs_dir2_data_unused_tag_p(newdup) = |
c2066e26 | 999 | cpu_to_be16((char *)newdup - (char *)hdr); |
1da177e4 | 1000 | xfs_dir2_data_log_unused(tp, bp, newdup); |
c2066e26 | 1001 | newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); |
ad354eb3 NS |
1002 | newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
1003 | newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length)); | |
bbaaf538 | 1004 | *xfs_dir2_data_unused_tag_p(newdup2) = |
c2066e26 | 1005 | cpu_to_be16((char *)newdup2 - (char *)hdr); |
1da177e4 LT |
1006 | xfs_dir2_data_log_unused(tp, bp, newdup2); |
1007 | /* | |
1008 | * If the old entry was in the table, we need to scan | |
1009 | * if the 3rd entry was valid, since these entries | |
1010 | * are smaller than the old one. | |
1011 | * If we don't need to scan that means there were 1 or 2 | |
1012 | * entries in the table, and removing the old and adding | |
1013 | * the 2 new will work. | |
1014 | */ | |
1015 | if (dfp) { | |
f5f3d9b0 | 1016 | needscan = (bf[2].length != 0); |
1da177e4 | 1017 | if (!needscan) { |
c2066e26 CH |
1018 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
1019 | xfs_dir2_data_freeinsert(hdr, newdup, needlogp); | |
1020 | xfs_dir2_data_freeinsert(hdr, newdup2, | |
1021 | needlogp); | |
1da177e4 LT |
1022 | } |
1023 | } | |
1024 | } | |
1025 | *needscanp = needscan; | |
1026 | } |