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