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