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