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