blob: e8a7aca5fe2393f51f48aedeb34178a6346c9f6c [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"
David Chinnerda353b02007-08-28 14:00:13 +100025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
27#include "xfs_dmapi.h"
28#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dir2_data.h"
37#include "xfs_dir2_leaf.h"
38#include "xfs_dir2_block.h"
39#include "xfs_dir2_trace.h"
40#include "xfs_error.h"
41
42/*
43 * Local function prototypes.
44 */
45static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, xfs_dabuf_t *bp, int first,
46 int last);
47static void xfs_dir2_block_log_tail(xfs_trans_t *tp, xfs_dabuf_t *bp);
48static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **bpp,
49 int *entno);
50static int xfs_dir2_block_sort(const void *a, const void *b);
51
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100052static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
53
54/*
55 * One-time startup routine called from xfs_init().
56 */
57void
58xfs_dir_startup(void)
59{
60 xfs_dir_hash_dot = xfs_da_hashname(".", 1);
61 xfs_dir_hash_dotdot = xfs_da_hashname("..", 2);
62}
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * Add an entry to a block directory.
66 */
67int /* error */
68xfs_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 */
Nathan Scott3d693c62006-03-17 17:28:27 +110094 __be16 *tagp; /* pointer to tag value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 xfs_trans_t *tp; /* transaction structure */
96
97 xfs_dir2_trace_args("block_addname", args);
98 dp = args->dp;
99 tp = args->trans;
100 mp = dp->i_mount;
101 /*
102 * Read the (one and only) directory block into dabuf bp.
103 */
104 if ((error =
105 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
106 return error;
107 }
108 ASSERT(bp != NULL);
109 block = bp->data;
110 /*
111 * Check the magic number, corrupted if wrong.
112 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100113 if (unlikely(be32_to_cpu(block->hdr.magic) != XFS_DIR2_BLOCK_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 XFS_CORRUPTION_ERROR("xfs_dir2_block_addname",
115 XFS_ERRLEVEL_LOW, mp, block);
116 xfs_da_brelse(tp, bp);
117 return XFS_ERROR(EFSCORRUPTED);
118 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000119 len = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /*
121 * Set up pointers to parts of the block.
122 */
123 bf = block->hdr.bestfree;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000124 btp = xfs_dir2_block_tail_p(mp, block);
125 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /*
127 * No stale entries? Need space for entry and new leaf.
128 */
129 if (!btp->stale) {
130 /*
131 * Tag just before the first leaf entry.
132 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100133 tagp = (__be16 *)blp - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /*
135 * Data object just before the first leaf entry.
136 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100137 enddup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /*
139 * If it's not free then can't do this add without cleaning up:
140 * the space before the first leaf entry needs to be free so it
141 * can be expanded to hold the pointer to the new entry.
142 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100143 if (be16_to_cpu(enddup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 dup = enddup = NULL;
145 /*
146 * Check out the biggest freespace and see if it's the same one.
147 */
148 else {
149 dup = (xfs_dir2_data_unused_t *)
Nathan Scott70e73f52006-03-17 17:26:52 +1100150 ((char *)block + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (dup == enddup) {
152 /*
153 * It is the biggest freespace, is it too small
154 * to hold the new leaf too?
155 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100156 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /*
158 * Yes, we use the second-largest
159 * entry instead if it works.
160 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100161 if (be16_to_cpu(bf[1].length) >= len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 dup = (xfs_dir2_data_unused_t *)
163 ((char *)block +
Nathan Scott70e73f52006-03-17 17:26:52 +1100164 be16_to_cpu(bf[1].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 else
166 dup = NULL;
167 }
168 } else {
169 /*
170 * Not the same free entry,
171 * just check its length.
172 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100173 if (be16_to_cpu(dup->length) < len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dup = NULL;
175 }
176 }
177 }
178 compact = 0;
179 }
180 /*
181 * If there are stale entries we'll use one for the leaf.
182 * Is the biggest entry enough to avoid compaction?
183 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100184 else if (be16_to_cpu(bf[0].length) >= len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 dup = (xfs_dir2_data_unused_t *)
Nathan Scott70e73f52006-03-17 17:26:52 +1100186 ((char *)block + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 compact = 0;
188 }
189 /*
190 * Will need to compact to make this work.
191 */
192 else {
193 /*
194 * Tag just before the first leaf entry.
195 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100196 tagp = (__be16 *)blp - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /*
198 * Data object just before the first leaf entry.
199 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100200 dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /*
202 * If it's not free then the data will go where the
203 * leaf data starts now, if it works at all.
204 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100205 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100206 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 (uint)sizeof(*blp) < len)
208 dup = NULL;
Nathan Scotte922fff2006-03-17 17:27:56 +1100209 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 dup = NULL;
211 else
212 dup = (xfs_dir2_data_unused_t *)blp;
213 compact = 1;
214 }
215 /*
216 * If this isn't a real add, we're done with the buffer.
217 */
218 if (args->justcheck)
219 xfs_da_brelse(tp, bp);
220 /*
221 * If we don't have space for the new entry & leaf ...
222 */
223 if (!dup) {
224 /*
225 * Not trying to actually do anything, or don't have
226 * a space reservation: return no-space.
227 */
228 if (args->justcheck || args->total == 0)
229 return XFS_ERROR(ENOSPC);
230 /*
231 * Convert to the next larger format.
232 * Then add the new entry in that format.
233 */
234 error = xfs_dir2_block_to_leaf(args, bp);
235 xfs_da_buf_done(bp);
236 if (error)
237 return error;
238 return xfs_dir2_leaf_addname(args);
239 }
240 /*
241 * Just checking, and it would work, so say so.
242 */
243 if (args->justcheck)
244 return 0;
245 needlog = needscan = 0;
246 /*
247 * If need to compact the leaf entries, do it now.
248 * Leave the highest-numbered stale entry stale.
249 * XXX should be the one closest to mid but mid is not yet computed.
250 */
251 if (compact) {
252 int fromidx; /* source leaf index */
253 int toidx; /* target leaf index */
254
Nathan Scotte922fff2006-03-17 17:27:56 +1100255 for (fromidx = toidx = be32_to_cpu(btp->count) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 highstale = lfloghigh = -1;
257 fromidx >= 0;
258 fromidx--) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100259 if (be32_to_cpu(blp[fromidx].address) == XFS_DIR2_NULL_DATAPTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (highstale == -1)
261 highstale = toidx;
262 else {
263 if (lfloghigh == -1)
264 lfloghigh = toidx;
265 continue;
266 }
267 }
268 if (fromidx < toidx)
269 blp[toidx] = blp[fromidx];
270 toidx--;
271 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100272 lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
273 lfloghigh -= be32_to_cpu(btp->stale) - 1;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800274 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 xfs_dir2_data_make_free(tp, bp,
276 (xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
Nathan Scotte922fff2006-03-17 17:27:56 +1100277 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 &needlog, &needscan);
Nathan Scotte922fff2006-03-17 17:27:56 +1100279 blp += be32_to_cpu(btp->stale) - 1;
280 btp->stale = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /*
282 * If we now need to rebuild the bestfree map, do so.
283 * This needs to happen before the next call to use_free.
284 */
285 if (needscan) {
Eric Sandeenef497f82007-05-08 13:48:49 +1000286 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 needscan = 0;
288 }
289 }
290 /*
291 * Set leaf logging boundaries to impossible state.
292 * For the no-stale case they're set explicitly.
293 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100294 else if (btp->stale) {
295 lfloglow = be32_to_cpu(btp->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 lfloghigh = -1;
297 }
298 /*
299 * Find the slot that's first lower than our hash value, -1 if none.
300 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100301 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100303 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 break;
305 if (hash < args->hashval)
306 low = mid + 1;
307 else
308 high = mid - 1;
309 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100310 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 mid--;
312 }
313 /*
314 * No stale entries, will use enddup space to hold new leaf.
315 */
316 if (!btp->stale) {
317 /*
318 * Mark the space needed for the new leaf entry, now in use.
319 */
320 xfs_dir2_data_use_free(tp, bp, enddup,
321 (xfs_dir2_data_aoff_t)
Nathan Scottad354eb2006-03-17 17:27:37 +1100322 ((char *)enddup - (char *)block + be16_to_cpu(enddup->length) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 sizeof(*blp)),
324 (xfs_dir2_data_aoff_t)sizeof(*blp),
325 &needlog, &needscan);
326 /*
327 * Update the tail (entry count).
328 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800329 be32_add_cpu(&btp->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /*
331 * If we now need to rebuild the bestfree map, do so.
332 * This needs to happen before the next call to use_free.
333 */
334 if (needscan) {
335 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block,
Eric Sandeenef497f82007-05-08 13:48:49 +1000336 &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 needscan = 0;
338 }
339 /*
340 * Adjust pointer to the first leaf entry, we're about to move
341 * the table up one to open up space for the new leaf entry.
342 * Then adjust our index to match.
343 */
344 blp--;
345 mid++;
346 if (mid)
347 memmove(blp, &blp[1], mid * sizeof(*blp));
348 lfloglow = 0;
349 lfloghigh = mid;
350 }
351 /*
352 * Use a stale leaf for our new entry.
353 */
354 else {
355 for (lowstale = mid;
356 lowstale >= 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100357 be32_to_cpu(blp[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 lowstale--)
359 continue;
360 for (highstale = mid + 1;
Nathan Scotte922fff2006-03-17 17:27:56 +1100361 highstale < be32_to_cpu(btp->count) &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100362 be32_to_cpu(blp[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 (lowstale < 0 || mid - lowstale > highstale - mid);
364 highstale++)
365 continue;
366 /*
367 * Move entries toward the low-numbered stale entry.
368 */
369 if (lowstale >= 0 &&
Nathan Scotte922fff2006-03-17 17:27:56 +1100370 (highstale == be32_to_cpu(btp->count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 mid - lowstale <= highstale - mid)) {
372 if (mid - lowstale)
373 memmove(&blp[lowstale], &blp[lowstale + 1],
374 (mid - lowstale) * sizeof(*blp));
375 lfloglow = MIN(lowstale, lfloglow);
376 lfloghigh = MAX(mid, lfloghigh);
377 }
378 /*
379 * Move entries toward the high-numbered stale entry.
380 */
381 else {
Nathan Scotte922fff2006-03-17 17:27:56 +1100382 ASSERT(highstale < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 mid++;
384 if (highstale - mid)
385 memmove(&blp[mid + 1], &blp[mid],
386 (highstale - mid) * sizeof(*blp));
387 lfloglow = MIN(mid, lfloglow);
388 lfloghigh = MAX(highstale, lfloghigh);
389 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800390 be32_add_cpu(&btp->stale, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392 /*
393 * Point to the new data entry.
394 */
395 dep = (xfs_dir2_data_entry_t *)dup;
396 /*
397 * Fill in the leaf entry.
398 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100399 blp[mid].hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000400 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100401 (char *)dep - (char *)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
403 /*
404 * Mark space for the data entry used.
405 */
406 xfs_dir2_data_use_free(tp, bp, dup,
407 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
408 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
409 /*
410 * Create the new data entry.
411 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000412 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 dep->namelen = args->namelen;
414 memcpy(dep->name, args->name, args->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000415 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +1100416 *tagp = cpu_to_be16((char *)dep - (char *)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /*
418 * Clean up the bestfree array and log the header, tail, and entry.
419 */
420 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000421 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (needlog)
423 xfs_dir2_data_log_header(tp, bp);
424 xfs_dir2_block_log_tail(tp, bp);
425 xfs_dir2_data_log_entry(tp, bp, dep);
426 xfs_dir2_data_check(dp, bp);
427 xfs_da_buf_done(bp);
428 return 0;
429}
430
431/*
432 * Readdir for block directories.
433 */
434int /* error */
435xfs_dir2_block_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 xfs_inode_t *dp, /* incore inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000437 void *dirent,
438 xfs_off_t *offset,
439 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 xfs_dir2_block_t *block; /* directory block structure */
442 xfs_dabuf_t *bp; /* buffer for block */
443 xfs_dir2_block_tail_t *btp; /* block tail */
444 xfs_dir2_data_entry_t *dep; /* block data entry */
445 xfs_dir2_data_unused_t *dup; /* block unused entry */
446 char *endptr; /* end of the data entries */
447 int error; /* error return value */
448 xfs_mount_t *mp; /* filesystem mount point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 char *ptr; /* current data entry */
450 int wantoff; /* starting block offset */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000451 xfs_ino_t ino;
452 xfs_off_t cook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 mp = dp->i_mount;
455 /*
456 * If the block number in the offset is out of range, we're done.
457 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000458 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 0;
460 }
461 /*
462 * Can't read the block, give up, else get dabuf in bp.
463 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000464 error = xfs_da_read_buf(NULL, dp, mp->m_dirdatablk, -1,
465 &bp, XFS_DATA_FORK);
466 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return error;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 ASSERT(bp != NULL);
470 /*
471 * Extract the byte offset we start at from the seek pointer.
472 * We'll skip entries before this.
473 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000474 wantoff = xfs_dir2_dataptr_to_off(mp, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 block = bp->data;
476 xfs_dir2_data_check(dp, bp);
477 /*
478 * Set up values for the loop.
479 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000480 btp = xfs_dir2_block_tail_p(mp, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 ptr = (char *)block->u;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000482 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /*
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 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100493 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
494 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 continue;
496 }
497
498 dep = (xfs_dir2_data_entry_t *)ptr;
499
500 /*
501 * Bump pointer for the next iteration.
502 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000503 ptr += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /*
505 * The entry is before the desired starting point, skip it.
506 */
507 if ((char *)dep - (char *)block < wantoff)
508 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000510 cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Lachlan McIlroy041388b2007-12-18 16:19:34 +1100511 (char *)dep - (char *)block);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000512 ino = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513#if XFS_BIG_INUMS
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000514 ino += mp->m_inoadd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 /*
518 * If it didn't fit, set the final offset to here & return.
519 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000520 if (filldir(dirent, dep->name, dep->namelen, cook,
521 ino, DT_UNKNOWN)) {
Lachlan McIlroy041388b2007-12-18 16:19:34 +1100522 *offset = cook;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000523 xfs_da_brelse(NULL, bp);
524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 }
527
528 /*
529 * Reached the end of the block.
Nathan Scottc41564b2006-03-29 08:55:14 +1000530 * Set the offset to a non-existent block 1 and return.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000532 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0);
533 xfs_da_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 0;
535}
536
537/*
538 * Log leaf entries from the block.
539 */
540static void
541xfs_dir2_block_log_leaf(
542 xfs_trans_t *tp, /* transaction structure */
543 xfs_dabuf_t *bp, /* block buffer */
544 int first, /* index of first logged leaf */
545 int last) /* index of last logged leaf */
546{
547 xfs_dir2_block_t *block; /* directory block structure */
548 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
549 xfs_dir2_block_tail_t *btp; /* block tail */
550 xfs_mount_t *mp; /* filesystem mount point */
551
552 mp = tp->t_mountp;
553 block = bp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000554 btp = xfs_dir2_block_tail_p(mp, block);
555 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)block),
557 (uint)((char *)&blp[last + 1] - (char *)block - 1));
558}
559
560/*
561 * Log the block tail.
562 */
563static void
564xfs_dir2_block_log_tail(
565 xfs_trans_t *tp, /* transaction structure */
566 xfs_dabuf_t *bp) /* block buffer */
567{
568 xfs_dir2_block_t *block; /* directory block structure */
569 xfs_dir2_block_tail_t *btp; /* block tail */
570 xfs_mount_t *mp; /* filesystem mount point */
571
572 mp = tp->t_mountp;
573 block = bp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000574 btp = xfs_dir2_block_tail_p(mp, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)block),
576 (uint)((char *)(btp + 1) - (char *)block - 1));
577}
578
579/*
580 * Look up an entry in the block. This is the external routine,
581 * xfs_dir2_block_lookup_int does the real work.
582 */
583int /* error */
584xfs_dir2_block_lookup(
585 xfs_da_args_t *args) /* dir lookup arguments */
586{
587 xfs_dir2_block_t *block; /* block structure */
588 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
589 xfs_dabuf_t *bp; /* block buffer */
590 xfs_dir2_block_tail_t *btp; /* block tail */
591 xfs_dir2_data_entry_t *dep; /* block data entry */
592 xfs_inode_t *dp; /* incore inode */
593 int ent; /* entry index */
594 int error; /* error return value */
595 xfs_mount_t *mp; /* filesystem mount point */
596
597 xfs_dir2_trace_args("block_lookup", args);
598 /*
599 * Get the buffer, look up the entry.
600 * If not found (ENOENT) then return, have no buffer.
601 */
602 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
603 return error;
604 dp = args->dp;
605 mp = dp->i_mount;
606 block = bp->data;
607 xfs_dir2_data_check(dp, bp);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000608 btp = xfs_dir2_block_tail_p(mp, block);
609 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /*
611 * Get the offset from the leaf entry, to point to the data.
612 */
613 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000614 ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /*
616 * Fill in inode number, release the block.
617 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000618 args->inumber = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 xfs_da_brelse(args->trans, bp);
620 return XFS_ERROR(EEXIST);
621}
622
623/*
624 * Internal block lookup routine.
625 */
626static int /* error */
627xfs_dir2_block_lookup_int(
628 xfs_da_args_t *args, /* dir lookup arguments */
629 xfs_dabuf_t **bpp, /* returned block buffer */
630 int *entno) /* returned entry number */
631{
632 xfs_dir2_dataptr_t addr; /* data entry address */
633 xfs_dir2_block_t *block; /* block structure */
634 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
635 xfs_dabuf_t *bp; /* block buffer */
636 xfs_dir2_block_tail_t *btp; /* block tail */
637 xfs_dir2_data_entry_t *dep; /* block data entry */
638 xfs_inode_t *dp; /* incore inode */
639 int error; /* error return value */
640 xfs_dahash_t hash; /* found hash value */
641 int high; /* binary search high index */
642 int low; /* binary search low index */
643 int mid; /* binary search current idx */
644 xfs_mount_t *mp; /* filesystem mount point */
645 xfs_trans_t *tp; /* transaction pointer */
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);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000660 btp = xfs_dir2_block_tail_p(mp, block);
661 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /*
663 * Loop doing a binary search for our hash value.
664 * Find our entry, ENOENT if it's not there.
665 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100666 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 ASSERT(low <= high);
668 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100669 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 break;
671 if (hash < args->hashval)
672 low = mid + 1;
673 else
674 high = mid - 1;
675 if (low > high) {
676 ASSERT(args->oknoent);
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 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100684 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 mid--;
686 }
687 /*
688 * Now loop forward through all the entries with the
689 * right hash value looking for our name.
690 */
691 do {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100692 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 continue;
694 /*
695 * Get pointer to the entry from the leaf.
696 */
697 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000698 ((char *)block + xfs_dir2_dataptr_to_off(mp, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /*
700 * Compare, if it's right give back buffer & entry number.
701 */
702 if (dep->namelen == args->namelen &&
703 dep->name[0] == args->name[0] &&
704 memcmp(dep->name, args->name, args->namelen) == 0) {
705 *bpp = bp;
706 *entno = mid;
707 return 0;
708 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100709 } while (++mid < be32_to_cpu(btp->count) && be32_to_cpu(blp[mid].hashval) == hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
711 * No match, release the buffer and return ENOENT.
712 */
713 ASSERT(args->oknoent);
714 xfs_da_brelse(tp, bp);
715 return XFS_ERROR(ENOENT);
716}
717
718/*
719 * Remove an entry from a block format directory.
720 * If that makes the block small enough to fit in shortform, transform it.
721 */
722int /* error */
723xfs_dir2_block_removename(
724 xfs_da_args_t *args) /* directory operation args */
725{
726 xfs_dir2_block_t *block; /* block structure */
727 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
728 xfs_dabuf_t *bp; /* block buffer */
729 xfs_dir2_block_tail_t *btp; /* block tail */
730 xfs_dir2_data_entry_t *dep; /* block data entry */
731 xfs_inode_t *dp; /* incore inode */
732 int ent; /* block leaf entry index */
733 int error; /* error return value */
734 xfs_mount_t *mp; /* filesystem mount point */
735 int needlog; /* need to log block header */
736 int needscan; /* need to fixup bestfree */
737 xfs_dir2_sf_hdr_t sfh; /* shortform header */
738 int size; /* shortform size */
739 xfs_trans_t *tp; /* transaction pointer */
740
741 xfs_dir2_trace_args("block_removename", args);
742 /*
743 * Look up the entry in the block. Gets the buffer and entry index.
744 * It will always be there, the vnodeops level does a lookup first.
745 */
746 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
747 return error;
748 }
749 dp = args->dp;
750 tp = args->trans;
751 mp = dp->i_mount;
752 block = bp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000753 btp = xfs_dir2_block_tail_p(mp, block);
754 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /*
756 * Point to the data entry using the leaf entry.
757 */
758 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000759 ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /*
761 * Mark the data entry's space free.
762 */
763 needlog = needscan = 0;
764 xfs_dir2_data_make_free(tp, bp,
765 (xfs_dir2_data_aoff_t)((char *)dep - (char *)block),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000766 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /*
768 * Fix up the block tail.
769 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800770 be32_add_cpu(&btp->stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 xfs_dir2_block_log_tail(tp, bp);
772 /*
773 * Remove the leaf entry by marking it stale.
774 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100775 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
777 /*
778 * Fix up bestfree, log the header if necessary.
779 */
780 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000781 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (needlog)
783 xfs_dir2_data_log_header(tp, bp);
784 xfs_dir2_data_check(dp, bp);
785 /*
786 * See if the size as a shortform is good enough.
787 */
788 if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
789 XFS_IFORK_DSIZE(dp)) {
790 xfs_da_buf_done(bp);
791 return 0;
792 }
793 /*
794 * If it works, do the conversion.
795 */
796 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
797}
798
799/*
800 * Replace an entry in a V2 block directory.
801 * Change the inode number to the new value.
802 */
803int /* error */
804xfs_dir2_block_replace(
805 xfs_da_args_t *args) /* directory operation args */
806{
807 xfs_dir2_block_t *block; /* block structure */
808 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
809 xfs_dabuf_t *bp; /* block buffer */
810 xfs_dir2_block_tail_t *btp; /* block tail */
811 xfs_dir2_data_entry_t *dep; /* block data entry */
812 xfs_inode_t *dp; /* incore inode */
813 int ent; /* leaf entry index */
814 int error; /* error return value */
815 xfs_mount_t *mp; /* filesystem mount point */
816
817 xfs_dir2_trace_args("block_replace", args);
818 /*
819 * Lookup the entry in the directory. Get buffer and entry index.
820 * This will always succeed since the caller has already done a lookup.
821 */
822 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
823 return error;
824 }
825 dp = args->dp;
826 mp = dp->i_mount;
827 block = bp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000828 btp = xfs_dir2_block_tail_p(mp, block);
829 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /*
831 * Point to the data entry we need to change.
832 */
833 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000834 ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000835 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /*
837 * Change the inode number to the new value.
838 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000839 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 xfs_dir2_data_log_entry(args->trans, bp, dep);
841 xfs_dir2_data_check(dp, bp);
842 xfs_da_buf_done(bp);
843 return 0;
844}
845
846/*
847 * Qsort comparison routine for the block leaf entries.
848 */
849static int /* sort order */
850xfs_dir2_block_sort(
851 const void *a, /* first leaf entry */
852 const void *b) /* second leaf entry */
853{
854 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
855 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
856
857 la = a;
858 lb = b;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100859 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
860 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
863/*
864 * Convert a V2 leaf directory to a V2 block directory if possible.
865 */
866int /* error */
867xfs_dir2_leaf_to_block(
868 xfs_da_args_t *args, /* operation arguments */
869 xfs_dabuf_t *lbp, /* leaf buffer */
870 xfs_dabuf_t *dbp) /* data buffer */
871{
Nathan Scott68b3a102006-03-17 17:27:19 +1100872 __be16 *bestsp; /* leaf bests table */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 xfs_dir2_block_t *block; /* block structure */
874 xfs_dir2_block_tail_t *btp; /* block tail */
875 xfs_inode_t *dp; /* incore directory inode */
876 xfs_dir2_data_unused_t *dup; /* unused data entry */
877 int error; /* error return value */
878 int from; /* leaf from index */
879 xfs_dir2_leaf_t *leaf; /* leaf structure */
880 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
881 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
882 xfs_mount_t *mp; /* file system mount point */
883 int needlog; /* need to log data header */
884 int needscan; /* need to scan for bestfree */
885 xfs_dir2_sf_hdr_t sfh; /* shortform header */
886 int size; /* bytes used */
Nathan Scott3d693c62006-03-17 17:28:27 +1100887 __be16 *tagp; /* end of entry (tag) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 int to; /* block/leaf to index */
889 xfs_trans_t *tp; /* transaction pointer */
890
891 xfs_dir2_trace_args_bb("leaf_to_block", args, lbp, dbp);
892 dp = args->dp;
893 tp = args->trans;
894 mp = dp->i_mount;
895 leaf = lbp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100896 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000897 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /*
899 * If there are data blocks other than the first one, take this
900 * opportunity to remove trailing empty data blocks that may have
901 * been left behind during no-space-reservation operations.
902 * These will show up in the leaf bests table.
903 */
904 while (dp->i_d.di_size > mp->m_dirblksize) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000905 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100906 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 mp->m_dirblksize - (uint)sizeof(block->hdr)) {
908 if ((error =
909 xfs_dir2_leaf_trim_data(args, lbp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100910 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 goto out;
912 } else {
913 error = 0;
914 goto out;
915 }
916 }
917 /*
918 * Read the data block if we don't already have it, give up if it fails.
919 */
920 if (dbp == NULL &&
921 (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
922 XFS_DATA_FORK))) {
923 goto out;
924 }
925 block = dbp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +1100926 ASSERT(be32_to_cpu(block->hdr.magic) == XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 /*
928 * Size of the "leaf" area in the block.
929 */
930 size = (uint)sizeof(block->tail) +
Nathan Scotta818e5d2006-03-17 17:28:07 +1100931 (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /*
933 * Look at the last data entry.
934 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100935 tagp = (__be16 *)((char *)block + mp->m_dirblksize) - 1;
936 dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /*
938 * If it's not free or is too short we can't do it.
939 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100940 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
941 be16_to_cpu(dup->length) < size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 error = 0;
943 goto out;
944 }
945 /*
946 * Start converting it to block form.
947 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100948 block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 needlog = 1;
950 needscan = 0;
951 /*
952 * Use up the space at the end of the block (blp/btp).
953 */
954 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
955 &needlog, &needscan);
956 /*
957 * Initialize the block tail.
958 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000959 btp = xfs_dir2_block_tail_p(mp, block);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100960 btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 btp->stale = 0;
962 xfs_dir2_block_log_tail(tp, dbp);
963 /*
964 * Initialize the block leaf area. We compact out stale entries.
965 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000966 lep = xfs_dir2_block_leaf_p(btp);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100967 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100968 if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 continue;
970 lep[to++] = leaf->ents[from];
971 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100972 ASSERT(to == be32_to_cpu(btp->count));
973 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /*
975 * Scan the bestfree if we need it and log the data block header.
976 */
977 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000978 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (needlog)
980 xfs_dir2_data_log_header(tp, dbp);
981 /*
982 * Pitch the old leaf block.
983 */
984 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
985 lbp = NULL;
986 if (error) {
987 goto out;
988 }
989 /*
990 * Now see if the resulting block can be shrunken to shortform.
991 */
992 if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
993 XFS_IFORK_DSIZE(dp)) {
994 error = 0;
995 goto out;
996 }
997 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
998out:
999 if (lbp)
1000 xfs_da_buf_done(lbp);
1001 if (dbp)
1002 xfs_da_buf_done(dbp);
1003 return error;
1004}
1005
1006/*
1007 * Convert the shortform directory to block form.
1008 */
1009int /* error */
1010xfs_dir2_sf_to_block(
1011 xfs_da_args_t *args) /* operation arguments */
1012{
1013 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
1014 xfs_dir2_block_t *block; /* block structure */
1015 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1016 xfs_dabuf_t *bp; /* block buffer */
1017 xfs_dir2_block_tail_t *btp; /* block tail pointer */
1018 char *buf; /* sf buffer */
1019 int buf_len;
1020 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1021 xfs_inode_t *dp; /* incore directory inode */
1022 int dummy; /* trash */
1023 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1024 int endoffset; /* end of data objects */
1025 int error; /* error return value */
1026 int i; /* index */
1027 xfs_mount_t *mp; /* filesystem mount point */
1028 int needlog; /* need to log block header */
1029 int needscan; /* need to scan block freespc */
1030 int newoffset; /* offset from current entry */
1031 int offset; /* target block offset */
1032 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
1033 xfs_dir2_sf_t *sfp; /* shortform structure */
Nathan Scott3d693c62006-03-17 17:28:27 +11001034 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 xfs_trans_t *tp; /* transaction pointer */
1036
1037 xfs_dir2_trace_args("sf_to_block", args);
1038 dp = args->dp;
1039 tp = args->trans;
1040 mp = dp->i_mount;
1041 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1042 /*
1043 * Bomb out if the shortform directory is way too short.
1044 */
1045 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1046 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1047 return XFS_ERROR(EIO);
1048 }
1049 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1050 ASSERT(dp->i_df.if_u1.if_data != NULL);
1051 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001052 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /*
1054 * Copy the directory into the stack buffer.
1055 * Then pitch the incore inode data so we can make extents.
1056 */
1057
1058 buf_len = dp->i_df.if_bytes;
1059 buf = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1060
1061 memcpy(buf, sfp, dp->i_df.if_bytes);
1062 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
1063 dp->i_d.di_size = 0;
1064 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1065 /*
1066 * Reset pointer - old sfp is gone.
1067 */
1068 sfp = (xfs_dir2_sf_t *)buf;
1069 /*
1070 * Add block 0 to the inode.
1071 */
1072 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1073 if (error) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001074 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return error;
1076 }
1077 /*
1078 * Initialize the data block.
1079 */
1080 error = xfs_dir2_data_init(args, blkno, &bp);
1081 if (error) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001082 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return error;
1084 }
1085 block = bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001086 block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 /*
1088 * Compute size of block "tail" area.
1089 */
1090 i = (uint)sizeof(*btp) +
Nathan Scott8f44e042006-03-17 17:28:47 +11001091 (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /*
1093 * The whole thing is initialized to free by the init routine.
1094 * Say we're using the leaf and tail area.
1095 */
1096 dup = (xfs_dir2_data_unused_t *)block->u;
1097 needlog = needscan = 0;
1098 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1099 &needscan);
1100 ASSERT(needscan == 0);
1101 /*
1102 * Fill in the tail.
1103 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001104 btp = xfs_dir2_block_tail_p(mp, block);
Nathan Scott8f44e042006-03-17 17:28:47 +11001105 btp->count = cpu_to_be32(sfp->hdr.count + 2); /* ., .. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 btp->stale = 0;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001107 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 endoffset = (uint)((char *)blp - (char *)block);
1109 /*
1110 * Remove the freespace, we'll manage it.
1111 */
1112 xfs_dir2_data_use_free(tp, bp, dup,
1113 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
Nathan Scottad354eb2006-03-17 17:27:37 +11001114 be16_to_cpu(dup->length), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 /*
1116 * Create entry for .
1117 */
1118 dep = (xfs_dir2_data_entry_t *)
1119 ((char *)block + XFS_DIR2_DATA_DOT_OFFSET);
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001120 dep->inumber = cpu_to_be64(dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 dep->namelen = 1;
1122 dep->name[0] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001123 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001124 *tagp = cpu_to_be16((char *)dep - (char *)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001126 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001127 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001128 (char *)dep - (char *)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /*
1130 * Create entry for ..
1131 */
1132 dep = (xfs_dir2_data_entry_t *)
1133 ((char *)block + XFS_DIR2_DATA_DOTDOT_OFFSET);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001134 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 dep->namelen = 2;
1136 dep->name[0] = dep->name[1] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001137 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001138 *tagp = cpu_to_be16((char *)dep - (char *)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001140 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001141 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001142 (char *)dep - (char *)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 offset = XFS_DIR2_DATA_FIRST_OFFSET;
1144 /*
1145 * Loop over existing entries, stuff them in.
1146 */
Nathan Scott8f44e042006-03-17 17:28:47 +11001147 if ((i = 0) == sfp->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 sfep = NULL;
1149 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001150 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /*
1152 * Need to preserve the existing offset values in the sf directory.
1153 * Insert holes (unused entries) where necessary.
1154 */
1155 while (offset < endoffset) {
1156 /*
1157 * sfep is null when we reach the end of the list.
1158 */
1159 if (sfep == NULL)
1160 newoffset = endoffset;
1161 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001162 newoffset = xfs_dir2_sf_get_offset(sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 /*
1164 * There should be a hole here, make one.
1165 */
1166 if (offset < newoffset) {
1167 dup = (xfs_dir2_data_unused_t *)
1168 ((char *)block + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +11001169 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1170 dup->length = cpu_to_be16(newoffset - offset);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001171 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 ((char *)dup - (char *)block));
1173 xfs_dir2_data_log_unused(tp, bp, dup);
1174 (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t *)block,
1175 dup, &dummy);
Nathan Scottad354eb2006-03-17 17:27:37 +11001176 offset += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 continue;
1178 }
1179 /*
1180 * Copy a real entry.
1181 */
1182 dep = (xfs_dir2_data_entry_t *)((char *)block + newoffset);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001183 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp,
1184 xfs_dir2_sf_inumberp(sfep)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 dep->namelen = sfep->namelen;
1186 memcpy(dep->name, sfep->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001187 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001188 *tagp = cpu_to_be16((char *)dep - (char *)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001190 blp[2 + i].hashval = cpu_to_be32(xfs_da_hashname(
1191 (char *)sfep->name, sfep->namelen));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001192 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 (char *)dep - (char *)block));
1194 offset = (int)((char *)(tagp + 1) - (char *)block);
Nathan Scott8f44e042006-03-17 17:28:47 +11001195 if (++i == sfp->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 sfep = NULL;
1197 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001198 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200 /* Done with the temporary buffer */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001201 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /*
1203 * Sort the leaf entries by hash value.
1204 */
Nathan Scotte922fff2006-03-17 17:27:56 +11001205 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /*
1207 * Log the leaf entry area and tail.
1208 * Already logged the header in data_init, ignore needlog.
1209 */
1210 ASSERT(needscan == 0);
Nathan Scotte922fff2006-03-17 17:27:56 +11001211 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 xfs_dir2_block_log_tail(tp, bp);
1213 xfs_dir2_data_check(dp, bp);
1214 xfs_da_buf_done(bp);
1215 return 0;
1216}