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