]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
5e656dbb | 2 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
693fc8f6 | 3 | * Copyright (c) 2013 Red Hat, Inc. |
da23017d | 4 | * All Rights Reserved. |
5000d01d | 5 | * |
da23017d 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 | |
2bd0ea18 | 8 | * published by the Free Software Foundation. |
5000d01d | 9 | * |
da23017d 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. | |
5000d01d | 14 | * |
da23017d 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 | |
2bd0ea18 NS |
18 | */ |
19 | ||
5e656dbb BN |
20 | #include <xfs.h> |
21 | ||
2bd0ea18 | 22 | /* |
5e656dbb | 23 | * Local function prototypes. |
2bd0ea18 | 24 | */ |
a2ceac1f DC |
25 | static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp, |
26 | int first, int last); | |
27 | static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp); | |
28 | static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp, | |
5e656dbb BN |
29 | int *entno); |
30 | static int xfs_dir2_block_sort(const void *a, const void *b); | |
2bd0ea18 | 31 | |
5e656dbb BN |
32 | static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot; |
33 | ||
34 | /* | |
35 | * One-time startup routine called from xfs_init(). | |
36 | */ | |
37 | void | |
38 | xfs_dir_startup(void) | |
39 | { | |
56b2de80 DC |
40 | xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1); |
41 | xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2); | |
5e656dbb | 42 | } |
2bd0ea18 | 43 | |
693fc8f6 DC |
44 | static bool |
45 | xfs_dir3_block_verify( | |
a2ceac1f DC |
46 | struct xfs_buf *bp) |
47 | { | |
48 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
693fc8f6 DC |
49 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
50 | ||
51 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
52 | if (hdr3->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) | |
53 | return false; | |
54 | if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid)) | |
55 | return false; | |
56 | if (be64_to_cpu(hdr3->blkno) != bp->b_bn) | |
57 | return false; | |
58 | } else { | |
59 | if (hdr3->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) | |
60 | return false; | |
a2ceac1f | 61 | } |
90ea28c3 | 62 | if (__xfs_dir3_data_check(NULL, bp)) |
693fc8f6 DC |
63 | return false; |
64 | return true; | |
a2ceac1f DC |
65 | } |
66 | ||
67 | static void | |
693fc8f6 | 68 | xfs_dir3_block_read_verify( |
a2ceac1f DC |
69 | struct xfs_buf *bp) |
70 | { | |
693fc8f6 DC |
71 | struct xfs_mount *mp = bp->b_target->bt_mount; |
72 | ||
45922933 DC |
73 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
74 | !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF)) | |
12b53197 | 75 | xfs_buf_ioerror(bp, -EFSBADCRC); |
45922933 | 76 | else if (!xfs_dir3_block_verify(bp)) |
12b53197 | 77 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
45922933 DC |
78 | |
79 | if (bp->b_error) | |
80 | xfs_verifier_error(bp); | |
a2ceac1f DC |
81 | } |
82 | ||
83 | static void | |
693fc8f6 | 84 | xfs_dir3_block_write_verify( |
a2ceac1f DC |
85 | struct xfs_buf *bp) |
86 | { | |
693fc8f6 DC |
87 | struct xfs_mount *mp = bp->b_target->bt_mount; |
88 | struct xfs_buf_log_item *bip = bp->b_fspriv; | |
89 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; | |
90 | ||
91 | if (!xfs_dir3_block_verify(bp)) { | |
12b53197 | 92 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
45922933 | 93 | xfs_verifier_error(bp); |
693fc8f6 DC |
94 | return; |
95 | } | |
96 | ||
97 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
98 | return; | |
99 | ||
100 | if (bip) | |
101 | hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn); | |
102 | ||
43b5aeed | 103 | xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF); |
a2ceac1f DC |
104 | } |
105 | ||
693fc8f6 DC |
106 | const struct xfs_buf_ops xfs_dir3_block_buf_ops = { |
107 | .verify_read = xfs_dir3_block_read_verify, | |
108 | .verify_write = xfs_dir3_block_write_verify, | |
a2ceac1f DC |
109 | }; |
110 | ||
494434d7 | 111 | int |
693fc8f6 | 112 | xfs_dir3_block_read( |
a2ceac1f DC |
113 | struct xfs_trans *tp, |
114 | struct xfs_inode *dp, | |
115 | struct xfs_buf **bpp) | |
116 | { | |
117 | struct xfs_mount *mp = dp->i_mount; | |
8b4dc4a9 | 118 | int err; |
a2ceac1f | 119 | |
ff105f75 | 120 | err = xfs_da_read_buf(tp, dp, mp->m_dir_geo->datablk, -1, bpp, |
693fc8f6 | 121 | XFS_DATA_FORK, &xfs_dir3_block_buf_ops); |
8b4dc4a9 | 122 | if (!err && tp) |
bdc16ee5 | 123 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_BLOCK_BUF); |
8b4dc4a9 | 124 | return err; |
693fc8f6 DC |
125 | } |
126 | ||
127 | static void | |
128 | xfs_dir3_block_init( | |
129 | struct xfs_mount *mp, | |
8b4dc4a9 | 130 | struct xfs_trans *tp, |
693fc8f6 DC |
131 | struct xfs_buf *bp, |
132 | struct xfs_inode *dp) | |
133 | { | |
134 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; | |
135 | ||
136 | bp->b_ops = &xfs_dir3_block_buf_ops; | |
bdc16ee5 | 137 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_BLOCK_BUF); |
693fc8f6 DC |
138 | |
139 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
140 | memset(hdr3, 0, sizeof(*hdr3)); | |
141 | hdr3->magic = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC); | |
142 | hdr3->blkno = cpu_to_be64(bp->b_bn); | |
143 | hdr3->owner = cpu_to_be64(dp->i_ino); | |
144 | uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid); | |
145 | return; | |
146 | ||
147 | } | |
148 | hdr3->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC); | |
a2ceac1f DC |
149 | } |
150 | ||
151 | static void | |
152 | xfs_dir2_block_need_space( | |
ff105f75 | 153 | struct xfs_inode *dp, |
a2ceac1f DC |
154 | struct xfs_dir2_data_hdr *hdr, |
155 | struct xfs_dir2_block_tail *btp, | |
156 | struct xfs_dir2_leaf_entry *blp, | |
157 | __be16 **tagpp, | |
158 | struct xfs_dir2_data_unused **dupp, | |
159 | struct xfs_dir2_data_unused **enddupp, | |
160 | int *compact, | |
161 | int len) | |
162 | { | |
163 | struct xfs_dir2_data_free *bf; | |
164 | __be16 *tagp = NULL; | |
165 | struct xfs_dir2_data_unused *dup = NULL; | |
166 | struct xfs_dir2_data_unused *enddup = NULL; | |
167 | ||
168 | *compact = 0; | |
ff105f75 | 169 | bf = dp->d_ops->data_bestfree_p(hdr); |
a2ceac1f DC |
170 | |
171 | /* | |
172 | * If there are stale entries we'll use one for the leaf. | |
173 | */ | |
174 | if (btp->stale) { | |
175 | if (be16_to_cpu(bf[0].length) >= len) { | |
176 | /* | |
177 | * The biggest entry enough to avoid compaction. | |
178 | */ | |
179 | dup = (xfs_dir2_data_unused_t *) | |
180 | ((char *)hdr + be16_to_cpu(bf[0].offset)); | |
181 | goto out; | |
182 | } | |
183 | ||
184 | /* | |
185 | * Will need to compact to make this work. | |
186 | * Tag just before the first leaf entry. | |
187 | */ | |
188 | *compact = 1; | |
189 | tagp = (__be16 *)blp - 1; | |
190 | ||
191 | /* Data object just before the first leaf entry. */ | |
192 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); | |
193 | ||
194 | /* | |
195 | * If it's not free then the data will go where the | |
196 | * leaf data starts now, if it works at all. | |
197 | */ | |
198 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { | |
199 | if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) * | |
200 | (uint)sizeof(*blp) < len) | |
201 | dup = NULL; | |
202 | } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len) | |
203 | dup = NULL; | |
204 | else | |
205 | dup = (xfs_dir2_data_unused_t *)blp; | |
206 | goto out; | |
207 | } | |
208 | ||
209 | /* | |
210 | * no stale entries, so just use free space. | |
211 | * Tag just before the first leaf entry. | |
212 | */ | |
213 | tagp = (__be16 *)blp - 1; | |
214 | ||
215 | /* Data object just before the first leaf entry. */ | |
216 | enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); | |
217 | ||
218 | /* | |
219 | * If it's not free then can't do this add without cleaning up: | |
220 | * the space before the first leaf entry needs to be free so it | |
221 | * can be expanded to hold the pointer to the new entry. | |
222 | */ | |
223 | if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) { | |
224 | /* | |
225 | * Check out the biggest freespace and see if it's the same one. | |
226 | */ | |
227 | dup = (xfs_dir2_data_unused_t *) | |
228 | ((char *)hdr + be16_to_cpu(bf[0].offset)); | |
229 | if (dup != enddup) { | |
230 | /* | |
231 | * Not the same free entry, just check its length. | |
232 | */ | |
233 | if (be16_to_cpu(dup->length) < len) | |
234 | dup = NULL; | |
235 | goto out; | |
236 | } | |
237 | ||
238 | /* | |
239 | * It is the biggest freespace, can it hold the leaf too? | |
240 | */ | |
241 | if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) { | |
242 | /* | |
243 | * Yes, use the second-largest entry instead if it works. | |
244 | */ | |
245 | if (be16_to_cpu(bf[1].length) >= len) | |
246 | dup = (xfs_dir2_data_unused_t *) | |
247 | ((char *)hdr + be16_to_cpu(bf[1].offset)); | |
248 | else | |
249 | dup = NULL; | |
250 | } | |
251 | } | |
252 | out: | |
253 | *tagpp = tagp; | |
254 | *dupp = dup; | |
255 | *enddupp = enddup; | |
256 | } | |
257 | ||
258 | /* | |
259 | * compact the leaf entries. | |
260 | * Leave the highest-numbered stale entry stale. | |
261 | * XXX should be the one closest to mid but mid is not yet computed. | |
262 | */ | |
263 | static void | |
264 | xfs_dir2_block_compact( | |
ff105f75 | 265 | struct xfs_da_args *args, |
a2ceac1f DC |
266 | struct xfs_buf *bp, |
267 | struct xfs_dir2_data_hdr *hdr, | |
268 | struct xfs_dir2_block_tail *btp, | |
269 | struct xfs_dir2_leaf_entry *blp, | |
270 | int *needlog, | |
271 | int *lfloghigh, | |
272 | int *lfloglow) | |
273 | { | |
274 | int fromidx; /* source leaf index */ | |
275 | int toidx; /* target leaf index */ | |
276 | int needscan = 0; | |
277 | int highstale; /* high stale index */ | |
278 | ||
279 | fromidx = toidx = be32_to_cpu(btp->count) - 1; | |
280 | highstale = *lfloghigh = -1; | |
281 | for (; fromidx >= 0; fromidx--) { | |
282 | if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) { | |
283 | if (highstale == -1) | |
284 | highstale = toidx; | |
285 | else { | |
286 | if (*lfloghigh == -1) | |
287 | *lfloghigh = toidx; | |
288 | continue; | |
289 | } | |
290 | } | |
291 | if (fromidx < toidx) | |
292 | blp[toidx] = blp[fromidx]; | |
293 | toidx--; | |
294 | } | |
295 | *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1); | |
296 | *lfloghigh -= be32_to_cpu(btp->stale) - 1; | |
297 | be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1)); | |
ff105f75 | 298 | xfs_dir2_data_make_free(args, bp, |
a2ceac1f DC |
299 | (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr), |
300 | (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)), | |
301 | needlog, &needscan); | |
a2ceac1f DC |
302 | btp->stale = cpu_to_be32(1); |
303 | /* | |
304 | * If we now need to rebuild the bestfree map, do so. | |
305 | * This needs to happen before the next call to use_free. | |
306 | */ | |
307 | if (needscan) | |
ff105f75 | 308 | xfs_dir2_data_freescan(args->geo, args->dp->d_ops, hdr, needlog); |
a2ceac1f DC |
309 | } |
310 | ||
2bd0ea18 NS |
311 | /* |
312 | * Add an entry to a block directory. | |
313 | */ | |
314 | int /* error */ | |
315 | xfs_dir2_block_addname( | |
316 | xfs_da_args_t *args) /* directory op arguments */ | |
317 | { | |
a2ceac1f | 318 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 319 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 320 | struct xfs_buf *bp; /* buffer for block */ |
2bd0ea18 NS |
321 | xfs_dir2_block_tail_t *btp; /* block tail */ |
322 | int compact; /* need to compact leaf ents */ | |
323 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
324 | xfs_inode_t *dp; /* directory inode */ | |
325 | xfs_dir2_data_unused_t *dup; /* block unused entry */ | |
326 | int error; /* error return value */ | |
0e266570 | 327 | xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */ |
2bd0ea18 NS |
328 | xfs_dahash_t hash; /* hash value of found entry */ |
329 | int high; /* high index for binary srch */ | |
330 | int highstale; /* high stale index */ | |
0e266570 NS |
331 | int lfloghigh=0; /* last final leaf to log */ |
332 | int lfloglow=0; /* first final leaf to log */ | |
2bd0ea18 NS |
333 | int len; /* length of the new entry */ |
334 | int low; /* low index for binary srch */ | |
335 | int lowstale; /* low stale index */ | |
0e266570 | 336 | int mid=0; /* midpoint for binary srch */ |
2bd0ea18 NS |
337 | int needlog; /* need to log header */ |
338 | int needscan; /* need to rescan freespace */ | |
5e656dbb | 339 | __be16 *tagp; /* pointer to tag value */ |
2bd0ea18 NS |
340 | xfs_trans_t *tp; /* transaction structure */ |
341 | ||
56b2de80 DC |
342 | trace_xfs_dir2_block_addname(args); |
343 | ||
2bd0ea18 NS |
344 | dp = args->dp; |
345 | tp = args->trans; | |
a2ceac1f DC |
346 | |
347 | /* Read the (one and only) directory block into bp. */ | |
693fc8f6 | 348 | error = xfs_dir3_block_read(tp, dp, &bp); |
a2ceac1f | 349 | if (error) |
2bd0ea18 | 350 | return error; |
a2ceac1f | 351 | |
ff105f75 | 352 | len = dp->d_ops->data_entsize(args->namelen); |
a2ceac1f | 353 | |
2bd0ea18 NS |
354 | /* |
355 | * Set up pointers to parts of the block. | |
356 | */ | |
a2ceac1f | 357 | hdr = bp->b_addr; |
ff105f75 | 358 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
5e656dbb | 359 | blp = xfs_dir2_block_leaf_p(btp); |
a2ceac1f | 360 | |
2bd0ea18 | 361 | /* |
a2ceac1f DC |
362 | * Find out if we can reuse stale entries or whether we need extra |
363 | * space for entry and new leaf. | |
2bd0ea18 | 364 | */ |
ff105f75 | 365 | xfs_dir2_block_need_space(dp, hdr, btp, blp, &tagp, &dup, |
a2ceac1f DC |
366 | &enddup, &compact, len); |
367 | ||
2bd0ea18 | 368 | /* |
a2ceac1f | 369 | * Done everything we need for a space check now. |
2bd0ea18 | 370 | */ |
a2ceac1f DC |
371 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) { |
372 | xfs_trans_brelse(tp, bp); | |
373 | if (!dup) | |
12b53197 | 374 | return -ENOSPC; |
a2ceac1f | 375 | return 0; |
2bd0ea18 | 376 | } |
a2ceac1f | 377 | |
2bd0ea18 NS |
378 | /* |
379 | * If we don't have space for the new entry & leaf ... | |
380 | */ | |
381 | if (!dup) { | |
a2ceac1f DC |
382 | /* Don't have a space reservation: return no-space. */ |
383 | if (args->total == 0) | |
12b53197 | 384 | return -ENOSPC; |
2bd0ea18 NS |
385 | /* |
386 | * Convert to the next larger format. | |
387 | * Then add the new entry in that format. | |
388 | */ | |
389 | error = xfs_dir2_block_to_leaf(args, bp); | |
2bd0ea18 NS |
390 | if (error) |
391 | return error; | |
392 | return xfs_dir2_leaf_addname(args); | |
393 | } | |
a2ceac1f | 394 | |
2bd0ea18 | 395 | needlog = needscan = 0; |
a2ceac1f | 396 | |
2bd0ea18 NS |
397 | /* |
398 | * If need to compact the leaf entries, do it now. | |
2bd0ea18 | 399 | */ |
49f693fa | 400 | if (compact) { |
ff105f75 | 401 | xfs_dir2_block_compact(args, bp, hdr, btp, blp, &needlog, |
a2ceac1f | 402 | &lfloghigh, &lfloglow); |
49f693fa DC |
403 | /* recalculate blp post-compaction */ |
404 | blp = xfs_dir2_block_leaf_p(btp); | |
405 | } else if (btp->stale) { | |
2bd0ea18 | 406 | /* |
a2ceac1f DC |
407 | * Set leaf logging boundaries to impossible state. |
408 | * For the no-stale case they're set explicitly. | |
2bd0ea18 | 409 | */ |
5e656dbb | 410 | lfloglow = be32_to_cpu(btp->count); |
2bd0ea18 NS |
411 | lfloghigh = -1; |
412 | } | |
a2ceac1f | 413 | |
2bd0ea18 NS |
414 | /* |
415 | * Find the slot that's first lower than our hash value, -1 if none. | |
416 | */ | |
5e656dbb | 417 | for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) { |
2bd0ea18 | 418 | mid = (low + high) >> 1; |
5e656dbb | 419 | if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval) |
2bd0ea18 NS |
420 | break; |
421 | if (hash < args->hashval) | |
422 | low = mid + 1; | |
423 | else | |
424 | high = mid - 1; | |
425 | } | |
5e656dbb | 426 | while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) { |
2bd0ea18 NS |
427 | mid--; |
428 | } | |
429 | /* | |
430 | * No stale entries, will use enddup space to hold new leaf. | |
431 | */ | |
46eca962 | 432 | if (!btp->stale) { |
2bd0ea18 NS |
433 | /* |
434 | * Mark the space needed for the new leaf entry, now in use. | |
435 | */ | |
ff105f75 | 436 | xfs_dir2_data_use_free(args, bp, enddup, |
2bd0ea18 | 437 | (xfs_dir2_data_aoff_t) |
a2ceac1f | 438 | ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) - |
2bd0ea18 NS |
439 | sizeof(*blp)), |
440 | (xfs_dir2_data_aoff_t)sizeof(*blp), | |
441 | &needlog, &needscan); | |
442 | /* | |
443 | * Update the tail (entry count). | |
444 | */ | |
5e656dbb | 445 | be32_add_cpu(&btp->count, 1); |
2bd0ea18 NS |
446 | /* |
447 | * If we now need to rebuild the bestfree map, do so. | |
448 | * This needs to happen before the next call to use_free. | |
449 | */ | |
450 | if (needscan) { | |
ff105f75 DC |
451 | xfs_dir2_data_freescan(args->geo, dp->d_ops, hdr, |
452 | &needlog); | |
2bd0ea18 NS |
453 | needscan = 0; |
454 | } | |
455 | /* | |
456 | * Adjust pointer to the first leaf entry, we're about to move | |
457 | * the table up one to open up space for the new leaf entry. | |
458 | * Then adjust our index to match. | |
459 | */ | |
460 | blp--; | |
461 | mid++; | |
462 | if (mid) | |
32181a02 | 463 | memmove(blp, &blp[1], mid * sizeof(*blp)); |
2bd0ea18 NS |
464 | lfloglow = 0; |
465 | lfloghigh = mid; | |
466 | } | |
467 | /* | |
468 | * Use a stale leaf for our new entry. | |
469 | */ | |
470 | else { | |
471 | for (lowstale = mid; | |
472 | lowstale >= 0 && | |
a2ceac1f DC |
473 | blp[lowstale].address != |
474 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR); | |
2bd0ea18 NS |
475 | lowstale--) |
476 | continue; | |
477 | for (highstale = mid + 1; | |
5e656dbb | 478 | highstale < be32_to_cpu(btp->count) && |
a2ceac1f DC |
479 | blp[highstale].address != |
480 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR) && | |
2bd0ea18 NS |
481 | (lowstale < 0 || mid - lowstale > highstale - mid); |
482 | highstale++) | |
483 | continue; | |
484 | /* | |
485 | * Move entries toward the low-numbered stale entry. | |
486 | */ | |
487 | if (lowstale >= 0 && | |
5e656dbb | 488 | (highstale == be32_to_cpu(btp->count) || |
2bd0ea18 NS |
489 | mid - lowstale <= highstale - mid)) { |
490 | if (mid - lowstale) | |
32181a02 | 491 | memmove(&blp[lowstale], &blp[lowstale + 1], |
2bd0ea18 NS |
492 | (mid - lowstale) * sizeof(*blp)); |
493 | lfloglow = MIN(lowstale, lfloglow); | |
494 | lfloghigh = MAX(mid, lfloghigh); | |
495 | } | |
496 | /* | |
497 | * Move entries toward the high-numbered stale entry. | |
498 | */ | |
499 | else { | |
5e656dbb | 500 | ASSERT(highstale < be32_to_cpu(btp->count)); |
2bd0ea18 NS |
501 | mid++; |
502 | if (highstale - mid) | |
32181a02 | 503 | memmove(&blp[mid + 1], &blp[mid], |
2bd0ea18 NS |
504 | (highstale - mid) * sizeof(*blp)); |
505 | lfloglow = MIN(mid, lfloglow); | |
506 | lfloghigh = MAX(highstale, lfloghigh); | |
507 | } | |
5e656dbb | 508 | be32_add_cpu(&btp->stale, -1); |
2bd0ea18 NS |
509 | } |
510 | /* | |
511 | * Point to the new data entry. | |
512 | */ | |
513 | dep = (xfs_dir2_data_entry_t *)dup; | |
514 | /* | |
515 | * Fill in the leaf entry. | |
516 | */ | |
5e656dbb | 517 | blp[mid].hashval = cpu_to_be32(args->hashval); |
ff105f75 | 518 | blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr( |
a2ceac1f | 519 | (char *)dep - (char *)hdr)); |
2bd0ea18 NS |
520 | xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh); |
521 | /* | |
522 | * Mark space for the data entry used. | |
523 | */ | |
ff105f75 | 524 | xfs_dir2_data_use_free(args, bp, dup, |
a2ceac1f | 525 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), |
2bd0ea18 NS |
526 | (xfs_dir2_data_aoff_t)len, &needlog, &needscan); |
527 | /* | |
528 | * Create the new data entry. | |
529 | */ | |
5e656dbb | 530 | dep->inumber = cpu_to_be64(args->inumber); |
2bd0ea18 | 531 | dep->namelen = args->namelen; |
32181a02 | 532 | memcpy(dep->name, args->name, args->namelen); |
ff105f75 DC |
533 | dp->d_ops->data_put_ftype(dep, args->filetype); |
534 | tagp = dp->d_ops->data_entry_tag_p(dep); | |
a2ceac1f | 535 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
2bd0ea18 NS |
536 | /* |
537 | * Clean up the bestfree array and log the header, tail, and entry. | |
538 | */ | |
539 | if (needscan) | |
ff105f75 | 540 | xfs_dir2_data_freescan(args->geo, dp->d_ops, hdr, &needlog); |
2bd0ea18 | 541 | if (needlog) |
ff105f75 | 542 | xfs_dir2_data_log_header(args, bp); |
2bd0ea18 | 543 | xfs_dir2_block_log_tail(tp, bp); |
ff105f75 | 544 | xfs_dir2_data_log_entry(args, bp, dep); |
90ea28c3 | 545 | xfs_dir3_data_check(dp, bp); |
2bd0ea18 NS |
546 | return 0; |
547 | } | |
548 | ||
549 | /* | |
550 | * Log leaf entries from the block. | |
551 | */ | |
5e656dbb | 552 | static void |
2bd0ea18 NS |
553 | xfs_dir2_block_log_leaf( |
554 | xfs_trans_t *tp, /* transaction structure */ | |
a2ceac1f | 555 | struct xfs_buf *bp, /* block buffer */ |
2bd0ea18 NS |
556 | int first, /* index of first logged leaf */ |
557 | int last) /* index of last logged leaf */ | |
558 | { | |
a2ceac1f DC |
559 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
560 | xfs_dir2_leaf_entry_t *blp; | |
561 | xfs_dir2_block_tail_t *btp; | |
2bd0ea18 | 562 | |
ff105f75 | 563 | btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr); |
5e656dbb | 564 | blp = xfs_dir2_block_leaf_p(btp); |
a2ceac1f DC |
565 | xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr), |
566 | (uint)((char *)&blp[last + 1] - (char *)hdr - 1)); | |
2bd0ea18 NS |
567 | } |
568 | ||
569 | /* | |
570 | * Log the block tail. | |
571 | */ | |
5e656dbb | 572 | static void |
2bd0ea18 NS |
573 | xfs_dir2_block_log_tail( |
574 | xfs_trans_t *tp, /* transaction structure */ | |
a2ceac1f | 575 | struct xfs_buf *bp) /* block buffer */ |
2bd0ea18 | 576 | { |
a2ceac1f DC |
577 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
578 | xfs_dir2_block_tail_t *btp; | |
2bd0ea18 | 579 | |
ff105f75 | 580 | btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr); |
a2ceac1f DC |
581 | xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr), |
582 | (uint)((char *)(btp + 1) - (char *)hdr - 1)); | |
2bd0ea18 NS |
583 | } |
584 | ||
585 | /* | |
586 | * Look up an entry in the block. This is the external routine, | |
587 | * xfs_dir2_block_lookup_int does the real work. | |
588 | */ | |
589 | int /* error */ | |
590 | xfs_dir2_block_lookup( | |
591 | xfs_da_args_t *args) /* dir lookup arguments */ | |
592 | { | |
a2ceac1f | 593 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 594 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 595 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 NS |
596 | xfs_dir2_block_tail_t *btp; /* block tail */ |
597 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
598 | xfs_inode_t *dp; /* incore inode */ | |
599 | int ent; /* entry index */ | |
600 | int error; /* error return value */ | |
2bd0ea18 | 601 | |
56b2de80 DC |
602 | trace_xfs_dir2_block_lookup(args); |
603 | ||
2bd0ea18 NS |
604 | /* |
605 | * Get the buffer, look up the entry. | |
606 | * If not found (ENOENT) then return, have no buffer. | |
607 | */ | |
0e266570 | 608 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) |
2bd0ea18 NS |
609 | return error; |
610 | dp = args->dp; | |
a2ceac1f | 611 | hdr = bp->b_addr; |
90ea28c3 | 612 | xfs_dir3_data_check(dp, bp); |
ff105f75 | 613 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
5e656dbb | 614 | blp = xfs_dir2_block_leaf_p(btp); |
2bd0ea18 NS |
615 | /* |
616 | * Get the offset from the leaf entry, to point to the data. | |
617 | */ | |
a2ceac1f | 618 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + |
ff105f75 DC |
619 | xfs_dir2_dataptr_to_off(args->geo, |
620 | be32_to_cpu(blp[ent].address))); | |
2bd0ea18 | 621 | /* |
5e656dbb | 622 | * Fill in inode number, CI name if appropriate, release the block. |
2bd0ea18 | 623 | */ |
5e656dbb | 624 | args->inumber = be64_to_cpu(dep->inumber); |
ff105f75 | 625 | args->filetype = dp->d_ops->data_get_ftype(dep); |
5e656dbb | 626 | error = xfs_dir_cilookup_result(args, dep->name, dep->namelen); |
a2ceac1f | 627 | xfs_trans_brelse(args->trans, bp); |
1e68581b | 628 | return error; |
2bd0ea18 NS |
629 | } |
630 | ||
631 | /* | |
632 | * Internal block lookup routine. | |
633 | */ | |
5e656dbb | 634 | static int /* error */ |
2bd0ea18 NS |
635 | xfs_dir2_block_lookup_int( |
636 | xfs_da_args_t *args, /* dir lookup arguments */ | |
a2ceac1f | 637 | struct xfs_buf **bpp, /* returned block buffer */ |
2bd0ea18 NS |
638 | int *entno) /* returned entry number */ |
639 | { | |
640 | xfs_dir2_dataptr_t addr; /* data entry address */ | |
a2ceac1f | 641 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 642 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 643 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 NS |
644 | xfs_dir2_block_tail_t *btp; /* block tail */ |
645 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
646 | xfs_inode_t *dp; /* incore inode */ | |
647 | int error; /* error return value */ | |
648 | xfs_dahash_t hash; /* found hash value */ | |
649 | int high; /* binary search high index */ | |
650 | int low; /* binary search low index */ | |
651 | int mid; /* binary search current idx */ | |
652 | xfs_mount_t *mp; /* filesystem mount point */ | |
653 | xfs_trans_t *tp; /* transaction pointer */ | |
5e656dbb | 654 | enum xfs_dacmp cmp; /* comparison result */ |
2bd0ea18 NS |
655 | |
656 | dp = args->dp; | |
657 | tp = args->trans; | |
658 | mp = dp->i_mount; | |
a2ceac1f | 659 | |
693fc8f6 | 660 | error = xfs_dir3_block_read(tp, dp, &bp); |
a2ceac1f | 661 | if (error) |
2bd0ea18 | 662 | return error; |
a2ceac1f DC |
663 | |
664 | hdr = bp->b_addr; | |
90ea28c3 | 665 | xfs_dir3_data_check(dp, bp); |
ff105f75 | 666 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
5e656dbb | 667 | blp = xfs_dir2_block_leaf_p(btp); |
2bd0ea18 NS |
668 | /* |
669 | * Loop doing a binary search for our hash value. | |
670 | * Find our entry, ENOENT if it's not there. | |
671 | */ | |
5e656dbb | 672 | for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) { |
2bd0ea18 NS |
673 | ASSERT(low <= high); |
674 | mid = (low + high) >> 1; | |
5e656dbb | 675 | if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval) |
2bd0ea18 NS |
676 | break; |
677 | if (hash < args->hashval) | |
678 | low = mid + 1; | |
679 | else | |
680 | high = mid - 1; | |
681 | if (low > high) { | |
5e656dbb | 682 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
a2ceac1f | 683 | xfs_trans_brelse(tp, bp); |
12b53197 | 684 | return -ENOENT; |
2bd0ea18 NS |
685 | } |
686 | } | |
687 | /* | |
688 | * Back up to the first one with the right hash value. | |
689 | */ | |
5e656dbb | 690 | while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) { |
2bd0ea18 NS |
691 | mid--; |
692 | } | |
693 | /* | |
694 | * Now loop forward through all the entries with the | |
695 | * right hash value looking for our name. | |
696 | */ | |
697 | do { | |
5e656dbb | 698 | if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR) |
2bd0ea18 NS |
699 | continue; |
700 | /* | |
701 | * Get pointer to the entry from the leaf. | |
702 | */ | |
703 | dep = (xfs_dir2_data_entry_t *) | |
ff105f75 | 704 | ((char *)hdr + xfs_dir2_dataptr_to_off(args->geo, addr)); |
2bd0ea18 | 705 | /* |
5e656dbb BN |
706 | * Compare name and if it's an exact match, return the index |
707 | * and buffer. If it's the first case-insensitive match, store | |
708 | * the index and buffer and continue looking for an exact match. | |
2bd0ea18 | 709 | */ |
5e656dbb BN |
710 | cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); |
711 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { | |
712 | args->cmpresult = cmp; | |
2bd0ea18 NS |
713 | *bpp = bp; |
714 | *entno = mid; | |
5e656dbb BN |
715 | if (cmp == XFS_CMP_EXACT) |
716 | return 0; | |
2bd0ea18 | 717 | } |
5e656dbb BN |
718 | } while (++mid < be32_to_cpu(btp->count) && |
719 | be32_to_cpu(blp[mid].hashval) == hash); | |
720 | ||
721 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); | |
722 | /* | |
723 | * Here, we can only be doing a lookup (not a rename or replace). | |
724 | * If a case-insensitive match was found earlier, return success. | |
725 | */ | |
726 | if (args->cmpresult == XFS_CMP_CASE) | |
727 | return 0; | |
2bd0ea18 NS |
728 | /* |
729 | * No match, release the buffer and return ENOENT. | |
730 | */ | |
a2ceac1f | 731 | xfs_trans_brelse(tp, bp); |
12b53197 | 732 | return -ENOENT; |
2bd0ea18 NS |
733 | } |
734 | ||
735 | /* | |
736 | * Remove an entry from a block format directory. | |
737 | * If that makes the block small enough to fit in shortform, transform it. | |
738 | */ | |
739 | int /* error */ | |
740 | xfs_dir2_block_removename( | |
741 | xfs_da_args_t *args) /* directory operation args */ | |
742 | { | |
a2ceac1f | 743 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 744 | xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */ |
a2ceac1f | 745 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 NS |
746 | xfs_dir2_block_tail_t *btp; /* block tail */ |
747 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
748 | xfs_inode_t *dp; /* incore inode */ | |
749 | int ent; /* block leaf entry index */ | |
750 | int error; /* error return value */ | |
2bd0ea18 NS |
751 | int needlog; /* need to log block header */ |
752 | int needscan; /* need to fixup bestfree */ | |
753 | xfs_dir2_sf_hdr_t sfh; /* shortform header */ | |
754 | int size; /* shortform size */ | |
755 | xfs_trans_t *tp; /* transaction pointer */ | |
756 | ||
56b2de80 DC |
757 | trace_xfs_dir2_block_removename(args); |
758 | ||
2bd0ea18 NS |
759 | /* |
760 | * Look up the entry in the block. Gets the buffer and entry index. | |
761 | * It will always be there, the vnodeops level does a lookup first. | |
762 | */ | |
0e266570 | 763 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) { |
2bd0ea18 NS |
764 | return error; |
765 | } | |
766 | dp = args->dp; | |
767 | tp = args->trans; | |
a2ceac1f | 768 | hdr = bp->b_addr; |
ff105f75 | 769 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
5e656dbb | 770 | blp = xfs_dir2_block_leaf_p(btp); |
2bd0ea18 NS |
771 | /* |
772 | * Point to the data entry using the leaf entry. | |
773 | */ | |
ff105f75 DC |
774 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + |
775 | xfs_dir2_dataptr_to_off(args->geo, | |
776 | be32_to_cpu(blp[ent].address))); | |
2bd0ea18 NS |
777 | /* |
778 | * Mark the data entry's space free. | |
779 | */ | |
780 | needlog = needscan = 0; | |
ff105f75 | 781 | xfs_dir2_data_make_free(args, bp, |
a2ceac1f | 782 | (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr), |
ff105f75 | 783 | dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan); |
2bd0ea18 NS |
784 | /* |
785 | * Fix up the block tail. | |
786 | */ | |
5e656dbb | 787 | be32_add_cpu(&btp->stale, 1); |
2bd0ea18 NS |
788 | xfs_dir2_block_log_tail(tp, bp); |
789 | /* | |
790 | * Remove the leaf entry by marking it stale. | |
791 | */ | |
5e656dbb | 792 | blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
2bd0ea18 NS |
793 | xfs_dir2_block_log_leaf(tp, bp, ent, ent); |
794 | /* | |
795 | * Fix up bestfree, log the header if necessary. | |
796 | */ | |
797 | if (needscan) | |
ff105f75 | 798 | xfs_dir2_data_freescan(args->geo, dp->d_ops, hdr, &needlog); |
2bd0ea18 | 799 | if (needlog) |
ff105f75 | 800 | xfs_dir2_data_log_header(args, bp); |
90ea28c3 | 801 | xfs_dir3_data_check(dp, bp); |
2bd0ea18 NS |
802 | /* |
803 | * See if the size as a shortform is good enough. | |
804 | */ | |
a2ceac1f DC |
805 | size = xfs_dir2_block_sfsize(dp, hdr, &sfh); |
806 | if (size > XFS_IFORK_DSIZE(dp)) | |
2bd0ea18 | 807 | return 0; |
a2ceac1f | 808 | |
2bd0ea18 NS |
809 | /* |
810 | * If it works, do the conversion. | |
811 | */ | |
812 | return xfs_dir2_block_to_sf(args, bp, size, &sfh); | |
813 | } | |
814 | ||
815 | /* | |
816 | * Replace an entry in a V2 block directory. | |
817 | * Change the inode number to the new value. | |
818 | */ | |
819 | int /* error */ | |
820 | xfs_dir2_block_replace( | |
821 | xfs_da_args_t *args) /* directory operation args */ | |
822 | { | |
a2ceac1f | 823 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 824 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 825 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 NS |
826 | xfs_dir2_block_tail_t *btp; /* block tail */ |
827 | xfs_dir2_data_entry_t *dep; /* block data entry */ | |
828 | xfs_inode_t *dp; /* incore inode */ | |
829 | int ent; /* leaf entry index */ | |
830 | int error; /* error return value */ | |
2bd0ea18 | 831 | |
56b2de80 DC |
832 | trace_xfs_dir2_block_replace(args); |
833 | ||
2bd0ea18 NS |
834 | /* |
835 | * Lookup the entry in the directory. Get buffer and entry index. | |
836 | * This will always succeed since the caller has already done a lookup. | |
837 | */ | |
0e266570 | 838 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) { |
2bd0ea18 NS |
839 | return error; |
840 | } | |
841 | dp = args->dp; | |
a2ceac1f | 842 | hdr = bp->b_addr; |
ff105f75 | 843 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
5e656dbb | 844 | blp = xfs_dir2_block_leaf_p(btp); |
2bd0ea18 NS |
845 | /* |
846 | * Point to the data entry we need to change. | |
847 | */ | |
ff105f75 DC |
848 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + |
849 | xfs_dir2_dataptr_to_off(args->geo, | |
850 | be32_to_cpu(blp[ent].address))); | |
5e656dbb | 851 | ASSERT(be64_to_cpu(dep->inumber) != args->inumber); |
2bd0ea18 NS |
852 | /* |
853 | * Change the inode number to the new value. | |
854 | */ | |
5e656dbb | 855 | dep->inumber = cpu_to_be64(args->inumber); |
ff105f75 DC |
856 | dp->d_ops->data_put_ftype(dep, args->filetype); |
857 | xfs_dir2_data_log_entry(args, bp, dep); | |
90ea28c3 | 858 | xfs_dir3_data_check(dp, bp); |
2bd0ea18 NS |
859 | return 0; |
860 | } | |
861 | ||
862 | /* | |
863 | * Qsort comparison routine for the block leaf entries. | |
864 | */ | |
865 | static int /* sort order */ | |
866 | xfs_dir2_block_sort( | |
867 | const void *a, /* first leaf entry */ | |
868 | const void *b) /* second leaf entry */ | |
869 | { | |
870 | const xfs_dir2_leaf_entry_t *la; /* first leaf entry */ | |
871 | const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */ | |
872 | ||
873 | la = a; | |
874 | lb = b; | |
5e656dbb BN |
875 | return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 : |
876 | (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0); | |
2bd0ea18 NS |
877 | } |
878 | ||
879 | /* | |
880 | * Convert a V2 leaf directory to a V2 block directory if possible. | |
881 | */ | |
882 | int /* error */ | |
883 | xfs_dir2_leaf_to_block( | |
884 | xfs_da_args_t *args, /* operation arguments */ | |
a2ceac1f DC |
885 | struct xfs_buf *lbp, /* leaf buffer */ |
886 | struct xfs_buf *dbp) /* data buffer */ | |
2bd0ea18 | 887 | { |
5e656dbb | 888 | __be16 *bestsp; /* leaf bests table */ |
a2ceac1f | 889 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 NS |
890 | xfs_dir2_block_tail_t *btp; /* block tail */ |
891 | xfs_inode_t *dp; /* incore directory inode */ | |
892 | xfs_dir2_data_unused_t *dup; /* unused data entry */ | |
893 | int error; /* error return value */ | |
894 | int from; /* leaf from index */ | |
895 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
896 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
897 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
898 | xfs_mount_t *mp; /* file system mount point */ | |
899 | int needlog; /* need to log data header */ | |
900 | int needscan; /* need to scan for bestfree */ | |
901 | xfs_dir2_sf_hdr_t sfh; /* shortform header */ | |
902 | int size; /* bytes used */ | |
5e656dbb | 903 | __be16 *tagp; /* end of entry (tag) */ |
2bd0ea18 NS |
904 | int to; /* block/leaf to index */ |
905 | xfs_trans_t *tp; /* transaction pointer */ | |
65b80c98 DC |
906 | struct xfs_dir2_leaf_entry *ents; |
907 | struct xfs_dir3_icleaf_hdr leafhdr; | |
2bd0ea18 | 908 | |
56b2de80 DC |
909 | trace_xfs_dir2_leaf_to_block(args); |
910 | ||
2bd0ea18 NS |
911 | dp = args->dp; |
912 | tp = args->trans; | |
913 | mp = dp->i_mount; | |
a2ceac1f | 914 | leaf = lbp->b_addr; |
ff105f75 DC |
915 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
916 | ents = dp->d_ops->leaf_ents_p(leaf); | |
917 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); | |
65b80c98 DC |
918 | |
919 | ASSERT(leafhdr.magic == XFS_DIR2_LEAF1_MAGIC || | |
920 | leafhdr.magic == XFS_DIR3_LEAF1_MAGIC); | |
2bd0ea18 NS |
921 | /* |
922 | * If there are data blocks other than the first one, take this | |
923 | * opportunity to remove trailing empty data blocks that may have | |
924 | * been left behind during no-space-reservation operations. | |
925 | * These will show up in the leaf bests table. | |
926 | */ | |
ff105f75 | 927 | while (dp->i_d.di_size > args->geo->blksize) { |
693fc8f6 DC |
928 | int hdrsz; |
929 | ||
ff105f75 | 930 | hdrsz = dp->d_ops->data_entry_offset; |
5e656dbb BN |
931 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
932 | if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) == | |
ff105f75 | 933 | args->geo->blksize - hdrsz) { |
0e266570 | 934 | if ((error = |
2bd0ea18 | 935 | xfs_dir2_leaf_trim_data(args, lbp, |
5e656dbb | 936 | (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1)))) |
a2ceac1f DC |
937 | return error; |
938 | } else | |
939 | return 0; | |
2bd0ea18 NS |
940 | } |
941 | /* | |
942 | * Read the data block if we don't already have it, give up if it fails. | |
943 | */ | |
a2ceac1f | 944 | if (!dbp) { |
ff105f75 | 945 | error = xfs_dir3_data_read(tp, dp, args->geo->datablk, -1, &dbp); |
a2ceac1f DC |
946 | if (error) |
947 | return error; | |
2bd0ea18 | 948 | } |
a2ceac1f | 949 | hdr = dbp->b_addr; |
90ea28c3 DC |
950 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
951 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)); | |
952 | ||
2bd0ea18 NS |
953 | /* |
954 | * Size of the "leaf" area in the block. | |
955 | */ | |
a2ceac1f | 956 | size = (uint)sizeof(xfs_dir2_block_tail_t) + |
65b80c98 | 957 | (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale); |
2bd0ea18 NS |
958 | /* |
959 | * Look at the last data entry. | |
960 | */ | |
ff105f75 | 961 | tagp = (__be16 *)((char *)hdr + args->geo->blksize) - 1; |
a2ceac1f | 962 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); |
2bd0ea18 NS |
963 | /* |
964 | * If it's not free or is too short we can't do it. | |
965 | */ | |
5e656dbb | 966 | if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG || |
a2ceac1f DC |
967 | be16_to_cpu(dup->length) < size) |
968 | return 0; | |
969 | ||
2bd0ea18 NS |
970 | /* |
971 | * Start converting it to block form. | |
972 | */ | |
8b4dc4a9 | 973 | xfs_dir3_block_init(mp, tp, dbp, dp); |
693fc8f6 | 974 | |
2bd0ea18 NS |
975 | needlog = 1; |
976 | needscan = 0; | |
977 | /* | |
978 | * Use up the space at the end of the block (blp/btp). | |
979 | */ | |
ff105f75 | 980 | xfs_dir2_data_use_free(args, dbp, dup, args->geo->blksize - size, size, |
2bd0ea18 NS |
981 | &needlog, &needscan); |
982 | /* | |
983 | * Initialize the block tail. | |
984 | */ | |
ff105f75 | 985 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
65b80c98 | 986 | btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale); |
46eca962 | 987 | btp->stale = 0; |
2bd0ea18 NS |
988 | xfs_dir2_block_log_tail(tp, dbp); |
989 | /* | |
990 | * Initialize the block leaf area. We compact out stale entries. | |
991 | */ | |
5e656dbb | 992 | lep = xfs_dir2_block_leaf_p(btp); |
65b80c98 DC |
993 | for (from = to = 0; from < leafhdr.count; from++) { |
994 | if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) | |
2bd0ea18 | 995 | continue; |
65b80c98 | 996 | lep[to++] = ents[from]; |
2bd0ea18 | 997 | } |
5e656dbb BN |
998 | ASSERT(to == be32_to_cpu(btp->count)); |
999 | xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1); | |
2bd0ea18 NS |
1000 | /* |
1001 | * Scan the bestfree if we need it and log the data block header. | |
1002 | */ | |
1003 | if (needscan) | |
ff105f75 | 1004 | xfs_dir2_data_freescan(args->geo, dp->d_ops, hdr, &needlog); |
2bd0ea18 | 1005 | if (needlog) |
ff105f75 | 1006 | xfs_dir2_data_log_header(args, dbp); |
2bd0ea18 NS |
1007 | /* |
1008 | * Pitch the old leaf block. | |
1009 | */ | |
ff105f75 | 1010 | error = xfs_da_shrink_inode(args, args->geo->leafblk, lbp); |
a2ceac1f DC |
1011 | if (error) |
1012 | return error; | |
1013 | ||
2bd0ea18 NS |
1014 | /* |
1015 | * Now see if the resulting block can be shrunken to shortform. | |
1016 | */ | |
a2ceac1f DC |
1017 | size = xfs_dir2_block_sfsize(dp, hdr, &sfh); |
1018 | if (size > XFS_IFORK_DSIZE(dp)) | |
1019 | return 0; | |
1020 | ||
2bd0ea18 | 1021 | return xfs_dir2_block_to_sf(args, dbp, size, &sfh); |
2bd0ea18 NS |
1022 | } |
1023 | ||
1024 | /* | |
1025 | * Convert the shortform directory to block form. | |
1026 | */ | |
1027 | int /* error */ | |
1028 | xfs_dir2_sf_to_block( | |
1029 | xfs_da_args_t *args) /* operation arguments */ | |
1030 | { | |
1031 | xfs_dir2_db_t blkno; /* dir-relative block # (0) */ | |
a2ceac1f | 1032 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 | 1033 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
a2ceac1f | 1034 | struct xfs_buf *bp; /* block buffer */ |
2bd0ea18 | 1035 | xfs_dir2_block_tail_t *btp; /* block tail pointer */ |
2bd0ea18 NS |
1036 | xfs_dir2_data_entry_t *dep; /* data entry pointer */ |
1037 | xfs_inode_t *dp; /* incore directory inode */ | |
1038 | int dummy; /* trash */ | |
1039 | xfs_dir2_data_unused_t *dup; /* unused entry pointer */ | |
1040 | int endoffset; /* end of data objects */ | |
1041 | int error; /* error return value */ | |
1042 | int i; /* index */ | |
1043 | xfs_mount_t *mp; /* filesystem mount point */ | |
1044 | int needlog; /* need to log block header */ | |
1045 | int needscan; /* need to scan block freespc */ | |
1046 | int newoffset; /* offset from current entry */ | |
1047 | int offset; /* target block offset */ | |
1048 | xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */ | |
a2ceac1f DC |
1049 | xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */ |
1050 | xfs_dir2_sf_hdr_t *sfp; /* shortform header */ | |
5e656dbb | 1051 | __be16 *tagp; /* end of data entry */ |
2bd0ea18 | 1052 | xfs_trans_t *tp; /* transaction pointer */ |
5e656dbb | 1053 | struct xfs_name name; |
3f17ed4b | 1054 | struct xfs_ifork *ifp; |
2bd0ea18 | 1055 | |
56b2de80 DC |
1056 | trace_xfs_dir2_sf_to_block(args); |
1057 | ||
2bd0ea18 NS |
1058 | dp = args->dp; |
1059 | tp = args->trans; | |
1060 | mp = dp->i_mount; | |
3f17ed4b DC |
1061 | ifp = XFS_IFORK_PTR(dp, XFS_DATA_FORK); |
1062 | ASSERT(ifp->if_flags & XFS_IFINLINE); | |
2bd0ea18 NS |
1063 | /* |
1064 | * Bomb out if the shortform directory is way too short. | |
1065 | */ | |
1066 | if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) { | |
2bd0ea18 | 1067 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
12b53197 | 1068 | return -EIO; |
2bd0ea18 | 1069 | } |
a2ceac1f | 1070 | |
3f17ed4b | 1071 | oldsfp = (xfs_dir2_sf_hdr_t *)ifp->if_u1.if_data; |
a2ceac1f | 1072 | |
3f17ed4b DC |
1073 | ASSERT(ifp->if_bytes == dp->i_d.di_size); |
1074 | ASSERT(ifp->if_u1.if_data != NULL); | |
a2ceac1f | 1075 | ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count)); |
3f17ed4b | 1076 | ASSERT(dp->i_d.di_nextents == 0); |
a2ceac1f | 1077 | |
2bd0ea18 | 1078 | /* |
a2ceac1f | 1079 | * Copy the directory into a temporary buffer. |
2bd0ea18 NS |
1080 | * Then pitch the incore inode data so we can make extents. |
1081 | */ | |
3f17ed4b DC |
1082 | sfp = kmem_alloc(ifp->if_bytes, KM_SLEEP); |
1083 | memcpy(sfp, oldsfp, ifp->if_bytes); | |
d663096d | 1084 | |
3f17ed4b DC |
1085 | xfs_idata_realloc(dp, -ifp->if_bytes, XFS_DATA_FORK); |
1086 | xfs_bmap_local_to_extents_empty(dp, XFS_DATA_FORK); | |
2bd0ea18 | 1087 | dp->i_d.di_size = 0; |
a2ceac1f | 1088 | |
2bd0ea18 NS |
1089 | /* |
1090 | * Add block 0 to the inode. | |
1091 | */ | |
1092 | error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno); | |
1093 | if (error) { | |
a2ceac1f | 1094 | kmem_free(sfp); |
2bd0ea18 NS |
1095 | return error; |
1096 | } | |
1097 | /* | |
693fc8f6 | 1098 | * Initialize the data block, then convert it to block format. |
2bd0ea18 | 1099 | */ |
693fc8f6 | 1100 | error = xfs_dir3_data_init(args, blkno, &bp); |
2bd0ea18 | 1101 | if (error) { |
a2ceac1f | 1102 | kmem_free(sfp); |
2bd0ea18 NS |
1103 | return error; |
1104 | } | |
8b4dc4a9 | 1105 | xfs_dir3_block_init(mp, tp, bp, dp); |
a2ceac1f | 1106 | hdr = bp->b_addr; |
693fc8f6 | 1107 | |
2bd0ea18 NS |
1108 | /* |
1109 | * Compute size of block "tail" area. | |
1110 | */ | |
1111 | i = (uint)sizeof(*btp) + | |
a2ceac1f | 1112 | (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t); |
2bd0ea18 NS |
1113 | /* |
1114 | * The whole thing is initialized to free by the init routine. | |
1115 | * Say we're using the leaf and tail area. | |
1116 | */ | |
ff105f75 | 1117 | dup = dp->d_ops->data_unused_p(hdr); |
2bd0ea18 | 1118 | needlog = needscan = 0; |
ff105f75 DC |
1119 | xfs_dir2_data_use_free(args, bp, dup, args->geo->blksize - i, |
1120 | i, &needlog, &needscan); | |
2bd0ea18 NS |
1121 | ASSERT(needscan == 0); |
1122 | /* | |
1123 | * Fill in the tail. | |
1124 | */ | |
ff105f75 | 1125 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
a2ceac1f | 1126 | btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */ |
46eca962 | 1127 | btp->stale = 0; |
5e656dbb | 1128 | blp = xfs_dir2_block_leaf_p(btp); |
a2ceac1f | 1129 | endoffset = (uint)((char *)blp - (char *)hdr); |
2bd0ea18 NS |
1130 | /* |
1131 | * Remove the freespace, we'll manage it. | |
1132 | */ | |
ff105f75 | 1133 | xfs_dir2_data_use_free(args, bp, dup, |
a2ceac1f | 1134 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), |
5e656dbb | 1135 | be16_to_cpu(dup->length), &needlog, &needscan); |
2bd0ea18 NS |
1136 | /* |
1137 | * Create entry for . | |
1138 | */ | |
ff105f75 | 1139 | dep = dp->d_ops->data_dot_entry_p(hdr); |
5e656dbb | 1140 | dep->inumber = cpu_to_be64(dp->i_ino); |
2bd0ea18 NS |
1141 | dep->namelen = 1; |
1142 | dep->name[0] = '.'; | |
ff105f75 DC |
1143 | dp->d_ops->data_put_ftype(dep, XFS_DIR3_FT_DIR); |
1144 | tagp = dp->d_ops->data_entry_tag_p(dep); | |
a2ceac1f | 1145 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
ff105f75 | 1146 | xfs_dir2_data_log_entry(args, bp, dep); |
5e656dbb | 1147 | blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot); |
ff105f75 | 1148 | blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr( |
a2ceac1f | 1149 | (char *)dep - (char *)hdr)); |
2bd0ea18 NS |
1150 | /* |
1151 | * Create entry for .. | |
1152 | */ | |
ff105f75 DC |
1153 | dep = dp->d_ops->data_dotdot_entry_p(hdr); |
1154 | dep->inumber = cpu_to_be64(dp->d_ops->sf_get_parent_ino(sfp)); | |
2bd0ea18 NS |
1155 | dep->namelen = 2; |
1156 | dep->name[0] = dep->name[1] = '.'; | |
ff105f75 DC |
1157 | dp->d_ops->data_put_ftype(dep, XFS_DIR3_FT_DIR); |
1158 | tagp = dp->d_ops->data_entry_tag_p(dep); | |
a2ceac1f | 1159 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
ff105f75 | 1160 | xfs_dir2_data_log_entry(args, bp, dep); |
5e656dbb | 1161 | blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot); |
ff105f75 | 1162 | blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr( |
a2ceac1f | 1163 | (char *)dep - (char *)hdr)); |
ff105f75 | 1164 | offset = dp->d_ops->data_first_offset; |
2bd0ea18 NS |
1165 | /* |
1166 | * Loop over existing entries, stuff them in. | |
1167 | */ | |
a2ceac1f DC |
1168 | i = 0; |
1169 | if (!sfp->count) | |
2bd0ea18 NS |
1170 | sfep = NULL; |
1171 | else | |
5e656dbb | 1172 | sfep = xfs_dir2_sf_firstentry(sfp); |
2bd0ea18 NS |
1173 | /* |
1174 | * Need to preserve the existing offset values in the sf directory. | |
1175 | * Insert holes (unused entries) where necessary. | |
1176 | */ | |
1177 | while (offset < endoffset) { | |
1178 | /* | |
1179 | * sfep is null when we reach the end of the list. | |
1180 | */ | |
1181 | if (sfep == NULL) | |
1182 | newoffset = endoffset; | |
1183 | else | |
5e656dbb | 1184 | newoffset = xfs_dir2_sf_get_offset(sfep); |
2bd0ea18 NS |
1185 | /* |
1186 | * There should be a hole here, make one. | |
1187 | */ | |
1188 | if (offset < newoffset) { | |
a2ceac1f | 1189 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); |
5e656dbb BN |
1190 | dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
1191 | dup->length = cpu_to_be16(newoffset - offset); | |
1192 | *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16( | |
a2ceac1f | 1193 | ((char *)dup - (char *)hdr)); |
ff105f75 DC |
1194 | xfs_dir2_data_log_unused(args, bp, dup); |
1195 | xfs_dir2_data_freeinsert(hdr, | |
1196 | dp->d_ops->data_bestfree_p(hdr), | |
1197 | dup, &dummy); | |
5e656dbb | 1198 | offset += be16_to_cpu(dup->length); |
2bd0ea18 NS |
1199 | continue; |
1200 | } | |
1201 | /* | |
1202 | * Copy a real entry. | |
1203 | */ | |
a2ceac1f | 1204 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset); |
ff105f75 | 1205 | dep->inumber = cpu_to_be64(dp->d_ops->sf_get_ino(sfp, sfep)); |
2bd0ea18 | 1206 | dep->namelen = sfep->namelen; |
ff105f75 | 1207 | dp->d_ops->data_put_ftype(dep, dp->d_ops->sf_get_ftype(sfep)); |
32181a02 | 1208 | memcpy(dep->name, sfep->name, dep->namelen); |
ff105f75 | 1209 | tagp = dp->d_ops->data_entry_tag_p(dep); |
a2ceac1f | 1210 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
ff105f75 | 1211 | xfs_dir2_data_log_entry(args, bp, dep); |
5e656dbb BN |
1212 | name.name = sfep->name; |
1213 | name.len = sfep->namelen; | |
1214 | blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops-> | |
1215 | hashname(&name)); | |
ff105f75 | 1216 | blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr( |
a2ceac1f DC |
1217 | (char *)dep - (char *)hdr)); |
1218 | offset = (int)((char *)(tagp + 1) - (char *)hdr); | |
1219 | if (++i == sfp->count) | |
2bd0ea18 NS |
1220 | sfep = NULL; |
1221 | else | |
ff105f75 | 1222 | sfep = dp->d_ops->sf_nextentry(sfp, sfep); |
2bd0ea18 | 1223 | } |
d663096d | 1224 | /* Done with the temporary buffer */ |
a2ceac1f | 1225 | kmem_free(sfp); |
2bd0ea18 NS |
1226 | /* |
1227 | * Sort the leaf entries by hash value. | |
1228 | */ | |
5e656dbb | 1229 | xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort); |
5000d01d | 1230 | /* |
2bd0ea18 NS |
1231 | * Log the leaf entry area and tail. |
1232 | * Already logged the header in data_init, ignore needlog. | |
1233 | */ | |
1234 | ASSERT(needscan == 0); | |
5e656dbb | 1235 | xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1); |
2bd0ea18 | 1236 | xfs_dir2_block_log_tail(tp, bp); |
90ea28c3 | 1237 | xfs_dir3_data_check(dp, bp); |
2bd0ea18 NS |
1238 | return 0; |
1239 | } |