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