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