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