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