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