blob: f6b919af7b828c468c0d7bdebac6d87fffde64b9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_dir2.h"
26#include "xfs_dmapi.h"
27#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dir2_data.h"
36#include "xfs_dir2_leaf.h"
37#include "xfs_dir2_block.h"
38#include "xfs_dir2_trace.h"
39#include "xfs_error.h"
40
41/*
42 * Local function prototypes.
43 */
44static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, xfs_dabuf_t *bp, int first,
45 int last);
46static void xfs_dir2_block_log_tail(xfs_trans_t *tp, xfs_dabuf_t *bp);
47static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **bpp,
48 int *entno);
49static int xfs_dir2_block_sort(const void *a, const void *b);
50
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100051static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
52
53/*
54 * One-time startup routine called from xfs_init().
55 */
56void
57xfs_dir_startup(void)
58{
59 xfs_dir_hash_dot = xfs_da_hashname(".", 1);
60 xfs_dir_hash_dotdot = xfs_da_hashname("..", 2);
61}
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Add an entry to a block directory.
65 */
66int /* error */
67xfs_dir2_block_addname(
68 xfs_da_args_t *args) /* directory op arguments */
69{
70 xfs_dir2_data_free_t *bf; /* bestfree table in block */
71 xfs_dir2_block_t *block; /* directory block structure */
72 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
73 xfs_dabuf_t *bp; /* buffer for block */
74 xfs_dir2_block_tail_t *btp; /* block tail */
75 int compact; /* need to compact leaf ents */
76 xfs_dir2_data_entry_t *dep; /* block data entry */
77 xfs_inode_t *dp; /* directory inode */
78 xfs_dir2_data_unused_t *dup; /* block unused entry */
79 int error; /* error return value */
80 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
81 xfs_dahash_t hash; /* hash value of found entry */
82 int high; /* high index for binary srch */
83 int highstale; /* high stale index */
84 int lfloghigh=0; /* last final leaf to log */
85 int lfloglow=0; /* first final leaf to log */
86 int len; /* length of the new entry */
87 int low; /* low index for binary srch */
88 int lowstale; /* low stale index */
89 int mid=0; /* midpoint for binary srch */
90 xfs_mount_t *mp; /* filesystem mount point */
91 int needlog; /* need to log header */
92 int needscan; /* need to rescan freespace */
Nathan Scott3d693c62006-03-17 17:28:27 +110093 __be16 *tagp; /* pointer to tag value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 xfs_trans_t *tp; /* transaction structure */
95
96 xfs_dir2_trace_args("block_addname", args);
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);
108 block = bp->data;
109 /*
110 * Check the magic number, corrupted if wrong.
111 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100112 if (unlikely(be32_to_cpu(block->hdr.magic) != XFS_DIR2_BLOCK_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 XFS_CORRUPTION_ERROR("xfs_dir2_block_addname",
114 XFS_ERRLEVEL_LOW, mp, block);
115 xfs_da_brelse(tp, bp);
116 return XFS_ERROR(EFSCORRUPTED);
117 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000118 len = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 /*
120 * Set up pointers to parts of the block.
121 */
122 bf = block->hdr.bestfree;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000123 btp = xfs_dir2_block_tail_p(mp, block);
124 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /*
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 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100132 tagp = (__be16 *)blp - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /*
134 * Data object just before the first leaf entry.
135 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100136 enddup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /*
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 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100142 if (be16_to_cpu(enddup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 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 *)
Nathan Scott70e73f52006-03-17 17:26:52 +1100149 ((char *)block + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (dup == enddup) {
151 /*
152 * It is the biggest freespace, is it too small
153 * to hold the new leaf too?
154 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100155 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /*
157 * Yes, we use the second-largest
158 * entry instead if it works.
159 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100160 if (be16_to_cpu(bf[1].length) >= len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 dup = (xfs_dir2_data_unused_t *)
162 ((char *)block +
Nathan Scott70e73f52006-03-17 17:26:52 +1100163 be16_to_cpu(bf[1].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 else
165 dup = NULL;
166 }
167 } else {
168 /*
169 * Not the same free entry,
170 * just check its length.
171 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100172 if (be16_to_cpu(dup->length) < len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 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 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100183 else if (be16_to_cpu(bf[0].length) >= len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 dup = (xfs_dir2_data_unused_t *)
Nathan Scott70e73f52006-03-17 17:26:52 +1100185 ((char *)block + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 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 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100195 tagp = (__be16 *)blp - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /*
197 * Data object just before the first leaf entry.
198 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100199 dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /*
201 * If it's not free then the data will go where the
202 * leaf data starts now, if it works at all.
203 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100204 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100205 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 (uint)sizeof(*blp) < len)
207 dup = NULL;
Nathan Scotte922fff2006-03-17 17:27:56 +1100208 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 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 */
217 if (args->justcheck)
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 */
227 if (args->justcheck || args->total == 0)
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 */
242 if (args->justcheck)
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
Nathan Scotte922fff2006-03-17 17:27:56 +1100254 for (fromidx = toidx = be32_to_cpu(btp->count) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 highstale = lfloghigh = -1;
256 fromidx >= 0;
257 fromidx--) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100258 if (be32_to_cpu(blp[fromidx].address) == XFS_DIR2_NULL_DATAPTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (highstale == -1)
260 highstale = toidx;
261 else {
262 if (lfloghigh == -1)
263 lfloghigh = toidx;
264 continue;
265 }
266 }
267 if (fromidx < toidx)
268 blp[toidx] = blp[fromidx];
269 toidx--;
270 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100271 lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
272 lfloghigh -= be32_to_cpu(btp->stale) - 1;
273 be32_add(&btp->count, -(be32_to_cpu(btp->stale) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 xfs_dir2_data_make_free(tp, bp,
275 (xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
Nathan Scotte922fff2006-03-17 17:27:56 +1100276 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 &needlog, &needscan);
Nathan Scotte922fff2006-03-17 17:27:56 +1100278 blp += be32_to_cpu(btp->stale) - 1;
279 btp->stale = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /*
281 * If we now need to rebuild the bestfree map, do so.
282 * This needs to happen before the next call to use_free.
283 */
284 if (needscan) {
Eric Sandeenef497f82007-05-08 13:48:49 +1000285 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 needscan = 0;
287 }
288 }
289 /*
290 * Set leaf logging boundaries to impossible state.
291 * For the no-stale case they're set explicitly.
292 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100293 else if (btp->stale) {
294 lfloglow = be32_to_cpu(btp->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 lfloghigh = -1;
296 }
297 /*
298 * Find the slot that's first lower than our hash value, -1 if none.
299 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100300 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100302 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 break;
304 if (hash < args->hashval)
305 low = mid + 1;
306 else
307 high = mid - 1;
308 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100309 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 mid--;
311 }
312 /*
313 * No stale entries, will use enddup space to hold new leaf.
314 */
315 if (!btp->stale) {
316 /*
317 * Mark the space needed for the new leaf entry, now in use.
318 */
319 xfs_dir2_data_use_free(tp, bp, enddup,
320 (xfs_dir2_data_aoff_t)
Nathan Scottad354eb2006-03-17 17:27:37 +1100321 ((char *)enddup - (char *)block + be16_to_cpu(enddup->length) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 sizeof(*blp)),
323 (xfs_dir2_data_aoff_t)sizeof(*blp),
324 &needlog, &needscan);
325 /*
326 * Update the tail (entry count).
327 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100328 be32_add(&btp->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /*
330 * If we now need to rebuild the bestfree map, do so.
331 * This needs to happen before the next call to use_free.
332 */
333 if (needscan) {
334 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block,
Eric Sandeenef497f82007-05-08 13:48:49 +1000335 &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 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 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100356 be32_to_cpu(blp[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 lowstale--)
358 continue;
359 for (highstale = mid + 1;
Nathan Scotte922fff2006-03-17 17:27:56 +1100360 highstale < be32_to_cpu(btp->count) &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100361 be32_to_cpu(blp[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 (lowstale < 0 || mid - lowstale > highstale - mid);
363 highstale++)
364 continue;
365 /*
366 * Move entries toward the low-numbered stale entry.
367 */
368 if (lowstale >= 0 &&
Nathan Scotte922fff2006-03-17 17:27:56 +1100369 (highstale == be32_to_cpu(btp->count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 mid - lowstale <= highstale - mid)) {
371 if (mid - lowstale)
372 memmove(&blp[lowstale], &blp[lowstale + 1],
373 (mid - lowstale) * sizeof(*blp));
374 lfloglow = MIN(lowstale, lfloglow);
375 lfloghigh = MAX(mid, lfloghigh);
376 }
377 /*
378 * Move entries toward the high-numbered stale entry.
379 */
380 else {
Nathan Scotte922fff2006-03-17 17:27:56 +1100381 ASSERT(highstale < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 mid++;
383 if (highstale - mid)
384 memmove(&blp[mid + 1], &blp[mid],
385 (highstale - mid) * sizeof(*blp));
386 lfloglow = MIN(mid, lfloglow);
387 lfloghigh = MAX(highstale, lfloghigh);
388 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100389 be32_add(&btp->stale, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 /*
392 * Point to the new data entry.
393 */
394 dep = (xfs_dir2_data_entry_t *)dup;
395 /*
396 * Fill in the leaf entry.
397 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100398 blp[mid].hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000399 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100400 (char *)dep - (char *)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
402 /*
403 * Mark space for the data entry used.
404 */
405 xfs_dir2_data_use_free(tp, bp, dup,
406 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
407 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
408 /*
409 * Create the new data entry.
410 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000411 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 dep->namelen = args->namelen;
413 memcpy(dep->name, args->name, args->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000414 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +1100415 *tagp = cpu_to_be16((char *)dep - (char *)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /*
417 * Clean up the bestfree array and log the header, tail, and entry.
418 */
419 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000420 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (needlog)
422 xfs_dir2_data_log_header(tp, bp);
423 xfs_dir2_block_log_tail(tp, bp);
424 xfs_dir2_data_log_entry(tp, bp, dep);
425 xfs_dir2_data_check(dp, bp);
426 xfs_da_buf_done(bp);
427 return 0;
428}
429
430/*
431 * Readdir for block directories.
432 */
433int /* error */
434xfs_dir2_block_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 xfs_inode_t *dp, /* incore inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000436 void *dirent,
437 xfs_off_t *offset,
438 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 xfs_dir2_block_t *block; /* directory block structure */
441 xfs_dabuf_t *bp; /* buffer for block */
442 xfs_dir2_block_tail_t *btp; /* block tail */
443 xfs_dir2_data_entry_t *dep; /* block data entry */
444 xfs_dir2_data_unused_t *dup; /* block unused entry */
445 char *endptr; /* end of the data entries */
446 int error; /* error return value */
447 xfs_mount_t *mp; /* filesystem mount point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 char *ptr; /* current data entry */
449 int wantoff; /* starting block offset */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000450 xfs_ino_t ino;
451 xfs_off_t cook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 mp = dp->i_mount;
454 /*
455 * If the block number in the offset is out of range, we're done.
456 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000457 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 0;
459 }
460 /*
461 * Can't read the block, give up, else get dabuf in bp.
462 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000463 error = xfs_da_read_buf(NULL, dp, mp->m_dirdatablk, -1,
464 &bp, XFS_DATA_FORK);
465 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return error;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 ASSERT(bp != NULL);
469 /*
470 * Extract the byte offset we start at from the seek pointer.
471 * We'll skip entries before this.
472 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000473 wantoff = xfs_dir2_dataptr_to_off(mp, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 block = bp->data;
475 xfs_dir2_data_check(dp, bp);
476 /*
477 * Set up values for the loop.
478 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000479 btp = xfs_dir2_block_tail_p(mp, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 ptr = (char *)block->u;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000481 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /*
484 * Loop over the data portion of the block.
485 * Each object is a real entry (dep) or an unused one (dup).
486 */
487 while (ptr < endptr) {
488 dup = (xfs_dir2_data_unused_t *)ptr;
489 /*
490 * Unused, skip it.
491 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100492 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
493 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 continue;
495 }
496
497 dep = (xfs_dir2_data_entry_t *)ptr;
498
499 /*
500 * Bump pointer for the next iteration.
501 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000502 ptr += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /*
504 * The entry is before the desired starting point, skip it.
505 */
506 if ((char *)dep - (char *)block < wantoff)
507 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000509 cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 ptr - (char *)block);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000511 ino = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512#if XFS_BIG_INUMS
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000513 ino += mp->m_inoadd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /*
517 * If it didn't fit, set the final offset to here & return.
518 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000519 if (filldir(dirent, dep->name, dep->namelen, cook,
520 ino, DT_UNKNOWN)) {
521 *offset = xfs_dir2_db_off_to_dataptr(mp,
522 mp->m_dirdatablk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 (char *)dep - (char *)block);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000524 xfs_da_brelse(NULL, bp);
525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527 }
528
529 /*
530 * Reached the end of the block.
Nathan Scottc41564b2006-03-29 08:55:14 +1000531 * Set the offset to a non-existent block 1 and return.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000533 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0);
534 xfs_da_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return 0;
536}
537
538/*
539 * Log leaf entries from the block.
540 */
541static void
542xfs_dir2_block_log_leaf(
543 xfs_trans_t *tp, /* transaction structure */
544 xfs_dabuf_t *bp, /* block buffer */
545 int first, /* index of first logged leaf */
546 int last) /* index of last logged leaf */
547{
548 xfs_dir2_block_t *block; /* directory block structure */
549 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
550 xfs_dir2_block_tail_t *btp; /* block tail */
551 xfs_mount_t *mp; /* filesystem mount point */
552
553 mp = tp->t_mountp;
554 block = bp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000555 btp = xfs_dir2_block_tail_p(mp, block);
556 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)block),
558 (uint)((char *)&blp[last + 1] - (char *)block - 1));
559}
560
561/*
562 * Log the block tail.
563 */
564static void
565xfs_dir2_block_log_tail(
566 xfs_trans_t *tp, /* transaction structure */
567 xfs_dabuf_t *bp) /* block buffer */
568{
569 xfs_dir2_block_t *block; /* directory block structure */
570 xfs_dir2_block_tail_t *btp; /* block tail */
571 xfs_mount_t *mp; /* filesystem mount point */
572
573 mp = tp->t_mountp;
574 block = bp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000575 btp = xfs_dir2_block_tail_p(mp, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)block),
577 (uint)((char *)(btp + 1) - (char *)block - 1));
578}
579
580/*
581 * Look up an entry in the block. This is the external routine,
582 * xfs_dir2_block_lookup_int does the real work.
583 */
584int /* error */
585xfs_dir2_block_lookup(
586 xfs_da_args_t *args) /* dir lookup arguments */
587{
588 xfs_dir2_block_t *block; /* block structure */
589 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
590 xfs_dabuf_t *bp; /* block buffer */
591 xfs_dir2_block_tail_t *btp; /* block tail */
592 xfs_dir2_data_entry_t *dep; /* block data entry */
593 xfs_inode_t *dp; /* incore inode */
594 int ent; /* entry index */
595 int error; /* error return value */
596 xfs_mount_t *mp; /* filesystem mount point */
597
598 xfs_dir2_trace_args("block_lookup", args);
599 /*
600 * Get the buffer, look up the entry.
601 * If not found (ENOENT) then return, have no buffer.
602 */
603 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
604 return error;
605 dp = args->dp;
606 mp = dp->i_mount;
607 block = bp->data;
608 xfs_dir2_data_check(dp, bp);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000609 btp = xfs_dir2_block_tail_p(mp, block);
610 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /*
612 * Get the offset from the leaf entry, to point to the data.
613 */
614 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000615 ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /*
617 * Fill in inode number, release the block.
618 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000619 args->inumber = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 xfs_da_brelse(args->trans, bp);
621 return XFS_ERROR(EEXIST);
622}
623
624/*
625 * Internal block lookup routine.
626 */
627static int /* error */
628xfs_dir2_block_lookup_int(
629 xfs_da_args_t *args, /* dir lookup arguments */
630 xfs_dabuf_t **bpp, /* returned block buffer */
631 int *entno) /* returned entry number */
632{
633 xfs_dir2_dataptr_t addr; /* data entry address */
634 xfs_dir2_block_t *block; /* block structure */
635 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
636 xfs_dabuf_t *bp; /* block buffer */
637 xfs_dir2_block_tail_t *btp; /* block tail */
638 xfs_dir2_data_entry_t *dep; /* block data entry */
639 xfs_inode_t *dp; /* incore inode */
640 int error; /* error return value */
641 xfs_dahash_t hash; /* found hash value */
642 int high; /* binary search high index */
643 int low; /* binary search low index */
644 int mid; /* binary search current idx */
645 xfs_mount_t *mp; /* filesystem mount point */
646 xfs_trans_t *tp; /* transaction pointer */
647
648 dp = args->dp;
649 tp = args->trans;
650 mp = dp->i_mount;
651 /*
652 * Read the buffer, return error if we can't get it.
653 */
654 if ((error =
655 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
656 return error;
657 }
658 ASSERT(bp != NULL);
659 block = bp->data;
660 xfs_dir2_data_check(dp, bp);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000661 btp = xfs_dir2_block_tail_p(mp, block);
662 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
664 * Loop doing a binary search for our hash value.
665 * Find our entry, ENOENT if it's not there.
666 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100667 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 ASSERT(low <= high);
669 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100670 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 break;
672 if (hash < args->hashval)
673 low = mid + 1;
674 else
675 high = mid - 1;
676 if (low > high) {
677 ASSERT(args->oknoent);
678 xfs_da_brelse(tp, bp);
679 return XFS_ERROR(ENOENT);
680 }
681 }
682 /*
683 * Back up to the first one with the right hash value.
684 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100685 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 mid--;
687 }
688 /*
689 * Now loop forward through all the entries with the
690 * right hash value looking for our name.
691 */
692 do {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100693 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 continue;
695 /*
696 * Get pointer to the entry from the leaf.
697 */
698 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000699 ((char *)block + xfs_dir2_dataptr_to_off(mp, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /*
701 * Compare, if it's right give back buffer & entry number.
702 */
703 if (dep->namelen == args->namelen &&
704 dep->name[0] == args->name[0] &&
705 memcmp(dep->name, args->name, args->namelen) == 0) {
706 *bpp = bp;
707 *entno = mid;
708 return 0;
709 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100710 } while (++mid < be32_to_cpu(btp->count) && be32_to_cpu(blp[mid].hashval) == hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /*
712 * No match, release the buffer and return ENOENT.
713 */
714 ASSERT(args->oknoent);
715 xfs_da_brelse(tp, bp);
716 return XFS_ERROR(ENOENT);
717}
718
719/*
720 * Remove an entry from a block format directory.
721 * If that makes the block small enough to fit in shortform, transform it.
722 */
723int /* error */
724xfs_dir2_block_removename(
725 xfs_da_args_t *args) /* directory operation args */
726{
727 xfs_dir2_block_t *block; /* block structure */
728 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
729 xfs_dabuf_t *bp; /* block buffer */
730 xfs_dir2_block_tail_t *btp; /* block tail */
731 xfs_dir2_data_entry_t *dep; /* block data entry */
732 xfs_inode_t *dp; /* incore inode */
733 int ent; /* block leaf entry index */
734 int error; /* error return value */
735 xfs_mount_t *mp; /* filesystem mount point */
736 int needlog; /* need to log block header */
737 int needscan; /* need to fixup bestfree */
738 xfs_dir2_sf_hdr_t sfh; /* shortform header */
739 int size; /* shortform size */
740 xfs_trans_t *tp; /* transaction pointer */
741
742 xfs_dir2_trace_args("block_removename", args);
743 /*
744 * Look up the entry in the block. Gets the buffer and entry index.
745 * It will always be there, the vnodeops level does a lookup first.
746 */
747 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
748 return error;
749 }
750 dp = args->dp;
751 tp = args->trans;
752 mp = dp->i_mount;
753 block = bp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000754 btp = xfs_dir2_block_tail_p(mp, block);
755 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /*
757 * Point to the data entry using the leaf entry.
758 */
759 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000760 ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /*
762 * Mark the data entry's space free.
763 */
764 needlog = needscan = 0;
765 xfs_dir2_data_make_free(tp, bp,
766 (xfs_dir2_data_aoff_t)((char *)dep - (char *)block),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000767 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /*
769 * Fix up the block tail.
770 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100771 be32_add(&btp->stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 xfs_dir2_block_log_tail(tp, bp);
773 /*
774 * Remove the leaf entry by marking it stale.
775 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100776 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
778 /*
779 * Fix up bestfree, log the header if necessary.
780 */
781 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000782 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (needlog)
784 xfs_dir2_data_log_header(tp, bp);
785 xfs_dir2_data_check(dp, bp);
786 /*
787 * See if the size as a shortform is good enough.
788 */
789 if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
790 XFS_IFORK_DSIZE(dp)) {
791 xfs_da_buf_done(bp);
792 return 0;
793 }
794 /*
795 * If it works, do the conversion.
796 */
797 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
798}
799
800/*
801 * Replace an entry in a V2 block directory.
802 * Change the inode number to the new value.
803 */
804int /* error */
805xfs_dir2_block_replace(
806 xfs_da_args_t *args) /* directory operation args */
807{
808 xfs_dir2_block_t *block; /* block structure */
809 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
810 xfs_dabuf_t *bp; /* block buffer */
811 xfs_dir2_block_tail_t *btp; /* block tail */
812 xfs_dir2_data_entry_t *dep; /* block data entry */
813 xfs_inode_t *dp; /* incore inode */
814 int ent; /* leaf entry index */
815 int error; /* error return value */
816 xfs_mount_t *mp; /* filesystem mount point */
817
818 xfs_dir2_trace_args("block_replace", args);
819 /*
820 * Lookup the entry in the directory. Get buffer and entry index.
821 * This will always succeed since the caller has already done a lookup.
822 */
823 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
824 return error;
825 }
826 dp = args->dp;
827 mp = dp->i_mount;
828 block = bp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000829 btp = xfs_dir2_block_tail_p(mp, block);
830 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /*
832 * Point to the data entry we need to change.
833 */
834 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000835 ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000836 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /*
838 * Change the inode number to the new value.
839 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000840 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 xfs_dir2_data_log_entry(args->trans, bp, dep);
842 xfs_dir2_data_check(dp, bp);
843 xfs_da_buf_done(bp);
844 return 0;
845}
846
847/*
848 * Qsort comparison routine for the block leaf entries.
849 */
850static int /* sort order */
851xfs_dir2_block_sort(
852 const void *a, /* first leaf entry */
853 const void *b) /* second leaf entry */
854{
855 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
856 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
857
858 la = a;
859 lb = b;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100860 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
861 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
864/*
865 * Convert a V2 leaf directory to a V2 block directory if possible.
866 */
867int /* error */
868xfs_dir2_leaf_to_block(
869 xfs_da_args_t *args, /* operation arguments */
870 xfs_dabuf_t *lbp, /* leaf buffer */
871 xfs_dabuf_t *dbp) /* data buffer */
872{
Nathan Scott68b3a102006-03-17 17:27:19 +1100873 __be16 *bestsp; /* leaf bests table */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 xfs_dir2_block_t *block; /* block structure */
875 xfs_dir2_block_tail_t *btp; /* block tail */
876 xfs_inode_t *dp; /* incore directory inode */
877 xfs_dir2_data_unused_t *dup; /* unused data entry */
878 int error; /* error return value */
879 int from; /* leaf from index */
880 xfs_dir2_leaf_t *leaf; /* leaf structure */
881 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
882 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
883 xfs_mount_t *mp; /* file system mount point */
884 int needlog; /* need to log data header */
885 int needscan; /* need to scan for bestfree */
886 xfs_dir2_sf_hdr_t sfh; /* shortform header */
887 int size; /* bytes used */
Nathan Scott3d693c62006-03-17 17:28:27 +1100888 __be16 *tagp; /* end of entry (tag) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 int to; /* block/leaf to index */
890 xfs_trans_t *tp; /* transaction pointer */
891
892 xfs_dir2_trace_args_bb("leaf_to_block", args, lbp, dbp);
893 dp = args->dp;
894 tp = args->trans;
895 mp = dp->i_mount;
896 leaf = lbp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100897 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000898 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /*
900 * If there are data blocks other than the first one, take this
901 * opportunity to remove trailing empty data blocks that may have
902 * been left behind during no-space-reservation operations.
903 * These will show up in the leaf bests table.
904 */
905 while (dp->i_d.di_size > mp->m_dirblksize) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000906 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100907 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 mp->m_dirblksize - (uint)sizeof(block->hdr)) {
909 if ((error =
910 xfs_dir2_leaf_trim_data(args, lbp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100911 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 goto out;
913 } else {
914 error = 0;
915 goto out;
916 }
917 }
918 /*
919 * Read the data block if we don't already have it, give up if it fails.
920 */
921 if (dbp == NULL &&
922 (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
923 XFS_DATA_FORK))) {
924 goto out;
925 }
926 block = dbp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +1100927 ASSERT(be32_to_cpu(block->hdr.magic) == XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /*
929 * Size of the "leaf" area in the block.
930 */
931 size = (uint)sizeof(block->tail) +
Nathan Scotta818e5d2006-03-17 17:28:07 +1100932 (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /*
934 * Look at the last data entry.
935 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100936 tagp = (__be16 *)((char *)block + mp->m_dirblksize) - 1;
937 dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /*
939 * If it's not free or is too short we can't do it.
940 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100941 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
942 be16_to_cpu(dup->length) < size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 error = 0;
944 goto out;
945 }
946 /*
947 * Start converting it to block form.
948 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100949 block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 needlog = 1;
951 needscan = 0;
952 /*
953 * Use up the space at the end of the block (blp/btp).
954 */
955 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
956 &needlog, &needscan);
957 /*
958 * Initialize the block tail.
959 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000960 btp = xfs_dir2_block_tail_p(mp, block);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100961 btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 btp->stale = 0;
963 xfs_dir2_block_log_tail(tp, dbp);
964 /*
965 * Initialize the block leaf area. We compact out stale entries.
966 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000967 lep = xfs_dir2_block_leaf_p(btp);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100968 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100969 if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 continue;
971 lep[to++] = leaf->ents[from];
972 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100973 ASSERT(to == be32_to_cpu(btp->count));
974 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /*
976 * Scan the bestfree if we need it and log the data block header.
977 */
978 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000979 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (needlog)
981 xfs_dir2_data_log_header(tp, dbp);
982 /*
983 * Pitch the old leaf block.
984 */
985 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
986 lbp = NULL;
987 if (error) {
988 goto out;
989 }
990 /*
991 * Now see if the resulting block can be shrunken to shortform.
992 */
993 if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
994 XFS_IFORK_DSIZE(dp)) {
995 error = 0;
996 goto out;
997 }
998 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
999out:
1000 if (lbp)
1001 xfs_da_buf_done(lbp);
1002 if (dbp)
1003 xfs_da_buf_done(dbp);
1004 return error;
1005}
1006
1007/*
1008 * Convert the shortform directory to block form.
1009 */
1010int /* error */
1011xfs_dir2_sf_to_block(
1012 xfs_da_args_t *args) /* operation arguments */
1013{
1014 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
1015 xfs_dir2_block_t *block; /* block structure */
1016 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1017 xfs_dabuf_t *bp; /* block buffer */
1018 xfs_dir2_block_tail_t *btp; /* block tail pointer */
1019 char *buf; /* sf buffer */
1020 int buf_len;
1021 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1022 xfs_inode_t *dp; /* incore directory inode */
1023 int dummy; /* trash */
1024 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1025 int endoffset; /* end of data objects */
1026 int error; /* error return value */
1027 int i; /* index */
1028 xfs_mount_t *mp; /* filesystem mount point */
1029 int needlog; /* need to log block header */
1030 int needscan; /* need to scan block freespc */
1031 int newoffset; /* offset from current entry */
1032 int offset; /* target block offset */
1033 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
1034 xfs_dir2_sf_t *sfp; /* shortform structure */
Nathan Scott3d693c62006-03-17 17:28:27 +11001035 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 xfs_trans_t *tp; /* transaction pointer */
1037
1038 xfs_dir2_trace_args("sf_to_block", args);
1039 dp = args->dp;
1040 tp = args->trans;
1041 mp = dp->i_mount;
1042 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1043 /*
1044 * Bomb out if the shortform directory is way too short.
1045 */
1046 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1047 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1048 return XFS_ERROR(EIO);
1049 }
1050 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1051 ASSERT(dp->i_df.if_u1.if_data != NULL);
1052 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001053 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /*
1055 * Copy the directory into the stack buffer.
1056 * Then pitch the incore inode data so we can make extents.
1057 */
1058
1059 buf_len = dp->i_df.if_bytes;
1060 buf = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1061
1062 memcpy(buf, sfp, dp->i_df.if_bytes);
1063 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
1064 dp->i_d.di_size = 0;
1065 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1066 /*
1067 * Reset pointer - old sfp is gone.
1068 */
1069 sfp = (xfs_dir2_sf_t *)buf;
1070 /*
1071 * Add block 0 to the inode.
1072 */
1073 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1074 if (error) {
1075 kmem_free(buf, buf_len);
1076 return error;
1077 }
1078 /*
1079 * Initialize the data block.
1080 */
1081 error = xfs_dir2_data_init(args, blkno, &bp);
1082 if (error) {
1083 kmem_free(buf, buf_len);
1084 return error;
1085 }
1086 block = bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001087 block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 /*
1089 * Compute size of block "tail" area.
1090 */
1091 i = (uint)sizeof(*btp) +
Nathan Scott8f44e042006-03-17 17:28:47 +11001092 (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
1094 * The whole thing is initialized to free by the init routine.
1095 * Say we're using the leaf and tail area.
1096 */
1097 dup = (xfs_dir2_data_unused_t *)block->u;
1098 needlog = needscan = 0;
1099 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1100 &needscan);
1101 ASSERT(needscan == 0);
1102 /*
1103 * Fill in the tail.
1104 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001105 btp = xfs_dir2_block_tail_p(mp, block);
Nathan Scott8f44e042006-03-17 17:28:47 +11001106 btp->count = cpu_to_be32(sfp->hdr.count + 2); /* ., .. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 btp->stale = 0;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001108 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 endoffset = (uint)((char *)blp - (char *)block);
1110 /*
1111 * Remove the freespace, we'll manage it.
1112 */
1113 xfs_dir2_data_use_free(tp, bp, dup,
1114 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
Nathan Scottad354eb2006-03-17 17:27:37 +11001115 be16_to_cpu(dup->length), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /*
1117 * Create entry for .
1118 */
1119 dep = (xfs_dir2_data_entry_t *)
1120 ((char *)block + XFS_DIR2_DATA_DOT_OFFSET);
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001121 dep->inumber = cpu_to_be64(dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 dep->namelen = 1;
1123 dep->name[0] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001124 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001125 *tagp = cpu_to_be16((char *)dep - (char *)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001127 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001128 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001129 (char *)dep - (char *)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /*
1131 * Create entry for ..
1132 */
1133 dep = (xfs_dir2_data_entry_t *)
1134 ((char *)block + XFS_DIR2_DATA_DOTDOT_OFFSET);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001135 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 dep->namelen = 2;
1137 dep->name[0] = dep->name[1] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001138 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001139 *tagp = cpu_to_be16((char *)dep - (char *)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001141 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001142 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001143 (char *)dep - (char *)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 offset = XFS_DIR2_DATA_FIRST_OFFSET;
1145 /*
1146 * Loop over existing entries, stuff them in.
1147 */
Nathan Scott8f44e042006-03-17 17:28:47 +11001148 if ((i = 0) == sfp->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 sfep = NULL;
1150 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001151 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /*
1153 * Need to preserve the existing offset values in the sf directory.
1154 * Insert holes (unused entries) where necessary.
1155 */
1156 while (offset < endoffset) {
1157 /*
1158 * sfep is null when we reach the end of the list.
1159 */
1160 if (sfep == NULL)
1161 newoffset = endoffset;
1162 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001163 newoffset = xfs_dir2_sf_get_offset(sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /*
1165 * There should be a hole here, make one.
1166 */
1167 if (offset < newoffset) {
1168 dup = (xfs_dir2_data_unused_t *)
1169 ((char *)block + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +11001170 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1171 dup->length = cpu_to_be16(newoffset - offset);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001172 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 ((char *)dup - (char *)block));
1174 xfs_dir2_data_log_unused(tp, bp, dup);
1175 (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t *)block,
1176 dup, &dummy);
Nathan Scottad354eb2006-03-17 17:27:37 +11001177 offset += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 continue;
1179 }
1180 /*
1181 * Copy a real entry.
1182 */
1183 dep = (xfs_dir2_data_entry_t *)((char *)block + newoffset);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001184 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp,
1185 xfs_dir2_sf_inumberp(sfep)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 dep->namelen = sfep->namelen;
1187 memcpy(dep->name, sfep->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001188 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001189 *tagp = cpu_to_be16((char *)dep - (char *)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001191 blp[2 + i].hashval = cpu_to_be32(xfs_da_hashname(
1192 (char *)sfep->name, sfep->namelen));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001193 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 (char *)dep - (char *)block));
1195 offset = (int)((char *)(tagp + 1) - (char *)block);
Nathan Scott8f44e042006-03-17 17:28:47 +11001196 if (++i == sfp->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 sfep = NULL;
1198 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001199 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201 /* Done with the temporary buffer */
1202 kmem_free(buf, buf_len);
1203 /*
1204 * Sort the leaf entries by hash value.
1205 */
Nathan Scotte922fff2006-03-17 17:27:56 +11001206 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 /*
1208 * Log the leaf entry area and tail.
1209 * Already logged the header in data_init, ignore needlog.
1210 */
1211 ASSERT(needscan == 0);
Nathan Scotte922fff2006-03-17 17:27:56 +11001212 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 xfs_dir2_block_log_tail(tp, bp);
1214 xfs_dir2_data_check(dp, bp);
1215 xfs_da_buf_done(bp);
1216 return 0;
1217}