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