blob: 29f484c11b3ac11f5f9f89d7e54cf5e332aab2f6 [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"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
32#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dir2_sf.h"
34#include "xfs_dinode.h"
35#include "xfs_inode.h"
36#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_dir2_data.h"
38#include "xfs_dir2_leaf.h"
39#include "xfs_dir2_block.h"
40#include "xfs_dir2_node.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * Local function declarations.
46 */
47#ifdef DEBUG
48static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
49#else
50#define xfs_dir2_leaf_check(dp, bp)
51#endif
52static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **lbpp,
53 int *indexp, xfs_dabuf_t **dbpp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100054static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp,
55 int first, int last);
Eric Sandeen6add2c42005-06-21 15:39:44 +100056static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/*
60 * Convert a block form directory to a leaf form directory.
61 */
62int /* error */
63xfs_dir2_block_to_leaf(
64 xfs_da_args_t *args, /* operation arguments */
65 xfs_dabuf_t *dbp) /* input block's buffer */
66{
Nathan Scott68b3a102006-03-17 17:27:19 +110067 __be16 *bestsp; /* leaf's bestsp entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 xfs_dablk_t blkno; /* leaf block's bno */
69 xfs_dir2_block_t *block; /* block structure */
70 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
71 xfs_dir2_block_tail_t *btp; /* block's tail */
72 xfs_inode_t *dp; /* incore directory inode */
73 int error; /* error return code */
74 xfs_dabuf_t *lbp; /* leaf block's buffer */
75 xfs_dir2_db_t ldb; /* leaf block's bno */
76 xfs_dir2_leaf_t *leaf; /* leaf structure */
77 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
78 xfs_mount_t *mp; /* filesystem mount point */
79 int needlog; /* need to log block header */
80 int needscan; /* need to rescan bestfree */
81 xfs_trans_t *tp; /* transaction pointer */
82
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000083 trace_xfs_dir2_block_to_leaf(args);
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 dp = args->dp;
86 mp = dp->i_mount;
87 tp = args->trans;
88 /*
89 * Add the leaf block to the inode.
90 * This interface will only put blocks in the leaf/node range.
91 * Since that's empty now, we'll get the root (block 0 in range).
92 */
93 if ((error = xfs_da_grow_inode(args, &blkno))) {
94 return error;
95 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100096 ldb = xfs_dir2_da_to_db(mp, blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
98 /*
99 * Initialize the leaf block, get a buffer for it.
100 */
101 if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
102 return error;
103 }
104 ASSERT(lbp != NULL);
105 leaf = lbp->data;
106 block = dbp->data;
107 xfs_dir2_data_check(dp, dbp);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000108 btp = xfs_dir2_block_tail_p(mp, block);
109 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /*
111 * Set the counts in the leaf header.
112 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100113 leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
114 leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /*
116 * Could compact these but I think we always do the conversion
117 * after squeezing out stale entries.
118 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100119 memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100120 xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 needscan = 0;
122 needlog = 1;
123 /*
124 * Make the space formerly occupied by the leaf entries and block
125 * tail be free.
126 */
127 xfs_dir2_data_make_free(tp, dbp,
128 (xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
129 (xfs_dir2_data_aoff_t)((char *)block + mp->m_dirblksize -
130 (char *)blp),
131 &needlog, &needscan);
132 /*
133 * Fix up the block header, make it a data block.
134 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100135 block->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000137 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /*
139 * Set up leaf tail and bests table.
140 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000141 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100142 ltp->bestcount = cpu_to_be32(1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000143 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott70e73f52006-03-17 17:26:52 +1100144 bestsp[0] = block->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /*
146 * Log the data header and leaf bests table.
147 */
148 if (needlog)
149 xfs_dir2_data_log_header(tp, dbp);
150 xfs_dir2_leaf_check(dp, lbp);
151 xfs_dir2_data_check(dp, dbp);
152 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
153 xfs_da_buf_done(lbp);
154 return 0;
155}
156
157/*
158 * Add an entry to a leaf form directory.
159 */
160int /* error */
161xfs_dir2_leaf_addname(
162 xfs_da_args_t *args) /* operation arguments */
163{
Nathan Scott68b3a102006-03-17 17:27:19 +1100164 __be16 *bestsp; /* freespace table in leaf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 int compact; /* need to compact leaves */
166 xfs_dir2_data_t *data; /* data block structure */
167 xfs_dabuf_t *dbp; /* data block buffer */
168 xfs_dir2_data_entry_t *dep; /* data block entry */
169 xfs_inode_t *dp; /* incore directory inode */
170 xfs_dir2_data_unused_t *dup; /* data unused entry */
171 int error; /* error return value */
172 int grown; /* allocated new data block */
173 int highstale; /* index of next stale leaf */
174 int i; /* temporary, index */
175 int index; /* leaf table position */
176 xfs_dabuf_t *lbp; /* leaf's buffer */
177 xfs_dir2_leaf_t *leaf; /* leaf structure */
178 int length; /* length of new entry */
179 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
180 int lfloglow; /* low leaf logging index */
181 int lfloghigh; /* high leaf logging index */
182 int lowstale; /* index of prev stale leaf */
183 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
184 xfs_mount_t *mp; /* filesystem mount point */
185 int needbytes; /* leaf block bytes needed */
186 int needlog; /* need to log data header */
187 int needscan; /* need to rescan data free */
Nathan Scott3d693c62006-03-17 17:28:27 +1100188 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 xfs_trans_t *tp; /* transaction pointer */
190 xfs_dir2_db_t use_block; /* data block number */
191
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000192 trace_xfs_dir2_leaf_addname(args);
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 dp = args->dp;
195 tp = args->trans;
196 mp = dp->i_mount;
197 /*
198 * Read the leaf block.
199 */
200 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
201 XFS_DATA_FORK);
202 if (error) {
203 return error;
204 }
205 ASSERT(lbp != NULL);
206 /*
207 * Look up the entry by hash value and name.
208 * We know it's not there, our caller has already done a lookup.
209 * So the index is of the entry to insert in front of.
210 * But if there are dup hash values the index is of the first of those.
211 */
212 index = xfs_dir2_leaf_search_hash(args, lbp);
213 leaf = lbp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000214 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
215 bestsp = xfs_dir2_leaf_bests_p(ltp);
216 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /*
218 * See if there are any entries with the same hash value
219 * and space in their block for the new entry.
220 * This is good because it puts multiple same-hash value entries
221 * in a data block, improving the lookup of those entries.
222 */
223 for (use_block = -1, lep = &leaf->ents[index];
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100224 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 index++, lep++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100226 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 continue;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000228 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100229 ASSERT(i < be32_to_cpu(ltp->bestcount));
Nathan Scott68b3a102006-03-17 17:27:19 +1100230 ASSERT(be16_to_cpu(bestsp[i]) != NULLDATAOFF);
231 if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 use_block = i;
233 break;
234 }
235 }
236 /*
237 * Didn't find a block yet, linear search all the data blocks.
238 */
239 if (use_block == -1) {
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100240 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /*
242 * Remember a block we see that's missing.
243 */
Nathan Scott68b3a102006-03-17 17:27:19 +1100244 if (be16_to_cpu(bestsp[i]) == NULLDATAOFF && use_block == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 use_block = i;
Nathan Scott68b3a102006-03-17 17:27:19 +1100246 else if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 use_block = i;
248 break;
249 }
250 }
251 }
252 /*
253 * How many bytes do we need in the leaf block?
254 */
255 needbytes =
256 (leaf->hdr.stale ? 0 : (uint)sizeof(leaf->ents[0])) +
257 (use_block != -1 ? 0 : (uint)sizeof(leaf->bests[0]));
258 /*
259 * Now kill use_block if it refers to a missing block, so we
260 * can use it as an indication of allocation needed.
261 */
Nathan Scott68b3a102006-03-17 17:27:19 +1100262 if (use_block != -1 && be16_to_cpu(bestsp[use_block]) == NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 use_block = -1;
264 /*
265 * If we don't have enough free bytes but we can make enough
266 * by compacting out stale entries, we'll do that.
267 */
Barry Naujok6a178102008-05-21 16:42:05 +1000268 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
269 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 compact = 1;
271 }
272 /*
273 * Otherwise if we don't have enough free bytes we need to
274 * convert to node form.
275 */
Barry Naujok6a178102008-05-21 16:42:05 +1000276 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
277 leaf->hdr.count)] < needbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /*
279 * Just checking or no space reservation, give up.
280 */
Barry Naujok6a178102008-05-21 16:42:05 +1000281 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
282 args->total == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 xfs_da_brelse(tp, lbp);
284 return XFS_ERROR(ENOSPC);
285 }
286 /*
287 * Convert to node form.
288 */
289 error = xfs_dir2_leaf_to_node(args, lbp);
290 xfs_da_buf_done(lbp);
291 if (error)
292 return error;
293 /*
294 * Then add the new entry.
295 */
296 return xfs_dir2_node_addname(args);
297 }
298 /*
299 * Otherwise it will fit without compaction.
300 */
301 else
302 compact = 0;
303 /*
304 * If just checking, then it will fit unless we needed to allocate
305 * a new data block.
306 */
Barry Naujok6a178102008-05-21 16:42:05 +1000307 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 xfs_da_brelse(tp, lbp);
309 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
310 }
311 /*
312 * If no allocations are allowed, return now before we've
313 * changed anything.
314 */
315 if (args->total == 0 && use_block == -1) {
316 xfs_da_brelse(tp, lbp);
317 return XFS_ERROR(ENOSPC);
318 }
319 /*
320 * Need to compact the leaf entries, removing stale ones.
321 * Leave one stale entry behind - the one closest to our
322 * insertion index - and we'll shift that one to our insertion
323 * point later.
324 */
325 if (compact) {
326 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
327 &lfloglow, &lfloghigh);
328 }
329 /*
330 * There are stale entries, so we'll need log-low and log-high
331 * impossibly bad values later.
332 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100333 else if (be16_to_cpu(leaf->hdr.stale)) {
334 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 lfloghigh = -1;
336 }
337 /*
338 * If there was no data block space found, we need to allocate
339 * a new one.
340 */
341 if (use_block == -1) {
342 /*
343 * Add the new data block.
344 */
345 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
346 &use_block))) {
347 xfs_da_brelse(tp, lbp);
348 return error;
349 }
350 /*
351 * Initialize the block.
352 */
353 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
354 xfs_da_brelse(tp, lbp);
355 return error;
356 }
357 /*
358 * If we're adding a new data block on the end we need to
359 * extend the bests table. Copy it up one entry.
360 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100361 if (use_block >= be32_to_cpu(ltp->bestcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 bestsp--;
363 memmove(&bestsp[0], &bestsp[1],
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100364 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800365 be32_add_cpu(&ltp->bestcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100367 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 /*
370 * If we're filling in a previously empty block just log it.
371 */
372 else
373 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
374 data = dbp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +1100375 bestsp[use_block] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 grown = 1;
377 }
378 /*
379 * Already had space in some data block.
380 * Just read that one in.
381 */
382 else {
383 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000384 xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 -1, &dbp, XFS_DATA_FORK))) {
386 xfs_da_brelse(tp, lbp);
387 return error;
388 }
389 data = dbp->data;
390 grown = 0;
391 }
392 xfs_dir2_data_check(dp, dbp);
393 /*
394 * Point to the biggest freespace in our data block.
395 */
396 dup = (xfs_dir2_data_unused_t *)
Nathan Scott70e73f52006-03-17 17:26:52 +1100397 ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
Nathan Scottad354eb2006-03-17 17:27:37 +1100398 ASSERT(be16_to_cpu(dup->length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 needscan = needlog = 0;
400 /*
401 * Mark the initial part of our freespace in use for the new entry.
402 */
403 xfs_dir2_data_use_free(tp, dbp, dup,
404 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
405 &needlog, &needscan);
406 /*
407 * Initialize our new entry (at last).
408 */
409 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000410 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 dep->namelen = args->namelen;
412 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000413 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +1100414 *tagp = cpu_to_be16((char *)dep - (char *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /*
416 * Need to scan fix up the bestfree table.
417 */
418 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000419 xfs_dir2_data_freescan(mp, data, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /*
421 * Need to log the data block's header.
422 */
423 if (needlog)
424 xfs_dir2_data_log_header(tp, dbp);
425 xfs_dir2_data_log_entry(tp, dbp, dep);
426 /*
427 * If the bests table needs to be changed, do it.
428 * Log the change unless we've already done that.
429 */
Nathan Scott68b3a102006-03-17 17:27:19 +1100430 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
Nathan Scott70e73f52006-03-17 17:26:52 +1100431 bestsp[use_block] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (!grown)
433 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
434 }
435 /*
436 * Now we need to make room to insert the leaf entry.
437 * If there are no stale entries, we just insert a hole at index.
438 */
439 if (!leaf->hdr.stale) {
440 /*
441 * lep is still good as the index leaf entry.
442 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100443 if (index < be16_to_cpu(leaf->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 memmove(lep + 1, lep,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100445 (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /*
447 * Record low and high logging indices for the leaf.
448 */
449 lfloglow = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100450 lfloghigh = be16_to_cpu(leaf->hdr.count);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800451 be16_add_cpu(&leaf->hdr.count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 /*
454 * There are stale entries.
455 * We will use one of them for the new entry.
456 * It's probably not at the right location, so we'll have to
457 * shift some up or down first.
458 */
459 else {
460 /*
461 * If we didn't compact before, we need to find the nearest
462 * stale entries before and after our insertion point.
463 */
464 if (compact == 0) {
465 /*
466 * Find the first stale entry before the insertion
467 * point, if any.
468 */
469 for (lowstale = index - 1;
470 lowstale >= 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100471 be32_to_cpu(leaf->ents[lowstale].address) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 XFS_DIR2_NULL_DATAPTR;
473 lowstale--)
474 continue;
475 /*
476 * Find the next stale entry at or after the insertion
477 * point, if any. Stop if we go so far that the
478 * lowstale entry would be better.
479 */
480 for (highstale = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100481 highstale < be16_to_cpu(leaf->hdr.count) &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100482 be32_to_cpu(leaf->ents[highstale].address) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 XFS_DIR2_NULL_DATAPTR &&
484 (lowstale < 0 ||
485 index - lowstale - 1 >= highstale - index);
486 highstale++)
487 continue;
488 }
489 /*
490 * If the low one is better, use it.
491 */
492 if (lowstale >= 0 &&
Nathan Scotta818e5d2006-03-17 17:28:07 +1100493 (highstale == be16_to_cpu(leaf->hdr.count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 index - lowstale - 1 < highstale - index)) {
495 ASSERT(index - lowstale - 1 >= 0);
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100496 ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 XFS_DIR2_NULL_DATAPTR);
498 /*
499 * Copy entries up to cover the stale entry
500 * and make room for the new entry.
501 */
502 if (index - lowstale - 1 > 0)
503 memmove(&leaf->ents[lowstale],
504 &leaf->ents[lowstale + 1],
505 (index - lowstale - 1) * sizeof(*lep));
506 lep = &leaf->ents[index - 1];
507 lfloglow = MIN(lowstale, lfloglow);
508 lfloghigh = MAX(index - 1, lfloghigh);
509 }
510 /*
511 * The high one is better, so use that one.
512 */
513 else {
514 ASSERT(highstale - index >= 0);
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100515 ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 XFS_DIR2_NULL_DATAPTR);
517 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000518 * Copy entries down to cover the stale entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 * and make room for the new entry.
520 */
521 if (highstale - index > 0)
522 memmove(&leaf->ents[index + 1],
523 &leaf->ents[index],
524 (highstale - index) * sizeof(*lep));
525 lep = &leaf->ents[index];
526 lfloglow = MIN(index, lfloglow);
527 lfloghigh = MAX(highstale, lfloghigh);
528 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800529 be16_add_cpu(&leaf->hdr.stale, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531 /*
532 * Fill in the new leaf entry.
533 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100534 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000535 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
Nathan Scott3d693c62006-03-17 17:28:27 +1100536 be16_to_cpu(*tagp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
538 * Log the leaf fields and give up the buffers.
539 */
540 xfs_dir2_leaf_log_header(tp, lbp);
541 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
542 xfs_dir2_leaf_check(dp, lbp);
543 xfs_da_buf_done(lbp);
544 xfs_dir2_data_check(dp, dbp);
545 xfs_da_buf_done(dbp);
546 return 0;
547}
548
549#ifdef DEBUG
550/*
551 * Check the internal consistency of a leaf1 block.
552 * Pop an assert if something is wrong.
553 */
Hannes Eder3180e662009-03-04 19:34:10 +0100554STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555xfs_dir2_leaf_check(
556 xfs_inode_t *dp, /* incore directory inode */
557 xfs_dabuf_t *bp) /* leaf's buffer */
558{
559 int i; /* leaf index */
560 xfs_dir2_leaf_t *leaf; /* leaf structure */
561 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
562 xfs_mount_t *mp; /* filesystem mount point */
563 int stale; /* count of stale leaves */
564
565 leaf = bp->data;
566 mp = dp->i_mount;
Nathan Scott89da0542006-03-17 17:28:40 +1100567 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /*
569 * This value is not restrictive enough.
570 * Should factor in the size of the bests table as well.
571 * We can deduce a value for that from di_size.
572 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000573 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
574 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /*
576 * Leaves and bests don't overlap.
577 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100578 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000579 (char *)xfs_dir2_leaf_bests_p(ltp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /*
581 * Check hash value order, count stale entries.
582 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100583 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
584 if (i + 1 < be16_to_cpu(leaf->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100585 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
586 be32_to_cpu(leaf->ents[i + 1].hashval));
587 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 stale++;
589 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100590 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592#endif /* DEBUG */
593
594/*
595 * Compact out any stale entries in the leaf.
596 * Log the header and changed leaf entries, if any.
597 */
598void
599xfs_dir2_leaf_compact(
600 xfs_da_args_t *args, /* operation arguments */
601 xfs_dabuf_t *bp) /* leaf buffer */
602{
603 int from; /* source leaf index */
604 xfs_dir2_leaf_t *leaf; /* leaf structure */
605 int loglow; /* first leaf entry to log */
606 int to; /* target leaf index */
607
608 leaf = bp->data;
609 if (!leaf->hdr.stale) {
610 return;
611 }
612 /*
613 * Compress out the stale entries in place.
614 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100615 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100616 if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 continue;
618 /*
619 * Only actually copy the entries that are different.
620 */
621 if (from > to) {
622 if (loglow == -1)
623 loglow = to;
624 leaf->ents[to] = leaf->ents[from];
625 }
626 to++;
627 }
628 /*
629 * Update and log the header, log the leaf entries.
630 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100631 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800632 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 leaf->hdr.stale = 0;
634 xfs_dir2_leaf_log_header(args->trans, bp);
635 if (loglow != -1)
636 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
637}
638
639/*
640 * Compact the leaf entries, removing stale ones.
641 * Leave one stale entry behind - the one closest to our
642 * insertion index - and the caller will shift that one to our insertion
643 * point later.
644 * Return new insertion index, where the remaining stale entry is,
645 * and leaf logging indices.
646 */
647void
648xfs_dir2_leaf_compact_x1(
649 xfs_dabuf_t *bp, /* leaf buffer */
650 int *indexp, /* insertion index */
651 int *lowstalep, /* out: stale entry before us */
652 int *highstalep, /* out: stale entry after us */
653 int *lowlogp, /* out: low log index */
654 int *highlogp) /* out: high log index */
655{
656 int from; /* source copy index */
657 int highstale; /* stale entry at/after index */
658 int index; /* insertion index */
659 int keepstale; /* source index of kept stale */
660 xfs_dir2_leaf_t *leaf; /* leaf structure */
661 int lowstale; /* stale entry before index */
662 int newindex=0; /* new insertion index */
663 int to; /* destination copy index */
664
665 leaf = bp->data;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100666 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 index = *indexp;
668 /*
669 * Find the first stale entry before our index, if any.
670 */
671 for (lowstale = index - 1;
672 lowstale >= 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100673 be32_to_cpu(leaf->ents[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 lowstale--)
675 continue;
676 /*
677 * Find the first stale entry at or after our index, if any.
678 * Stop if the answer would be worse than lowstale.
679 */
680 for (highstale = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100681 highstale < be16_to_cpu(leaf->hdr.count) &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100682 be32_to_cpu(leaf->ents[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 (lowstale < 0 || index - lowstale > highstale - index);
684 highstale++)
685 continue;
686 /*
687 * Pick the better of lowstale and highstale.
688 */
689 if (lowstale >= 0 &&
Nathan Scotta818e5d2006-03-17 17:28:07 +1100690 (highstale == be16_to_cpu(leaf->hdr.count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 index - lowstale <= highstale - index))
692 keepstale = lowstale;
693 else
694 keepstale = highstale;
695 /*
696 * Copy the entries in place, removing all the stale entries
697 * except keepstale.
698 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100699 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /*
701 * Notice the new value of index.
702 */
703 if (index == from)
704 newindex = to;
705 if (from != keepstale &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100706 be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (from == to)
708 *lowlogp = to;
709 continue;
710 }
711 /*
712 * Record the new keepstale value for the insertion.
713 */
714 if (from == keepstale)
715 lowstale = highstale = to;
716 /*
717 * Copy only the entries that have moved.
718 */
719 if (from > to)
720 leaf->ents[to] = leaf->ents[from];
721 to++;
722 }
723 ASSERT(from > to);
724 /*
725 * If the insertion point was past the last entry,
726 * set the new insertion point accordingly.
727 */
728 if (index == from)
729 newindex = to;
730 *indexp = newindex;
731 /*
732 * Adjust the leaf header values.
733 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800734 be16_add_cpu(&leaf->hdr.count, -(from - to));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100735 leaf->hdr.stale = cpu_to_be16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /*
737 * Remember the low/high stale value only in the "right"
738 * direction.
739 */
740 if (lowstale >= newindex)
741 lowstale = -1;
742 else
Nathan Scotta818e5d2006-03-17 17:28:07 +1100743 highstale = be16_to_cpu(leaf->hdr.count);
744 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 *lowstalep = lowstale;
746 *highstalep = highstale;
747}
748
749/*
750 * Getdents (readdir) for leaf and node directories.
751 * This reads the data blocks only, so is the same for both forms.
752 */
753int /* error */
754xfs_dir2_leaf_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 xfs_inode_t *dp, /* incore directory inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000756 void *dirent,
757 size_t bufsize,
758 xfs_off_t *offset,
759 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 xfs_dabuf_t *bp; /* data block buffer */
762 int byteoff; /* offset in current block */
763 xfs_dir2_db_t curdb; /* db for current block */
764 xfs_dir2_off_t curoff; /* current overall offset */
765 xfs_dir2_data_t *data; /* data block structure */
766 xfs_dir2_data_entry_t *dep; /* data entry */
767 xfs_dir2_data_unused_t *dup; /* unused entry */
Nathan Scottf6d75cb2006-03-14 13:32:24 +1100768 int error = 0; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int i; /* temporary loop index */
770 int j; /* temporary loop index */
771 int length; /* temporary length value */
772 xfs_bmbt_irec_t *map; /* map vector for blocks */
773 xfs_extlen_t map_blocks; /* number of fsbs in map */
774 xfs_dablk_t map_off; /* last mapped file offset */
775 int map_size; /* total entries in *map */
776 int map_valid; /* valid entries in *map */
777 xfs_mount_t *mp; /* filesystem mount point */
778 xfs_dir2_off_t newoff; /* new curoff after new blk */
779 int nmap; /* mappings to ask xfs_bmapi */
Nathan Scottf6d75cb2006-03-14 13:32:24 +1100780 char *ptr = NULL; /* pointer to current data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 int ra_current; /* number of read-ahead blks */
782 int ra_index; /* *map index for read-ahead */
783 int ra_offset; /* map entry offset for ra */
784 int ra_want; /* readahead count wanted */
785
786 /*
787 * If the offset is at or past the largest allowed value,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000788 * give up right away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000790 if (*offset >= XFS_DIR2_MAX_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return 0;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 mp = dp->i_mount;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /*
796 * Set up to bmap a number of blocks based on the caller's
797 * buffer size, the directory block size, and the filesystem
798 * block size.
799 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000800 map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
802 map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
803 bp = NULL;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /*
806 * Inside the loop we keep the main offset value as a byte offset
807 * in the directory file.
808 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000809 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /*
812 * Force this conversion through db so we truncate the offset
813 * down to get the start of the data block.
814 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000815 map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /*
817 * Loop over directory entries until we reach the end offset.
818 * Get more blocks and readahead as necessary.
819 */
820 while (curoff < XFS_DIR2_LEAF_OFFSET) {
821 /*
822 * If we have no buffer, or we're off the end of the
823 * current buffer, need to get another one.
824 */
825 if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) {
826 /*
827 * If we have a buffer, we need to release it and
828 * take it out of the mapping.
829 */
830 if (bp) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000831 xfs_da_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 bp = NULL;
833 map_blocks -= mp->m_dirblkfsbs;
834 /*
835 * Loop to get rid of the extents for the
836 * directory block.
837 */
838 for (i = mp->m_dirblkfsbs; i > 0; ) {
839 j = MIN((int)map->br_blockcount, i);
840 map->br_blockcount -= j;
841 map->br_startblock += j;
842 map->br_startoff += j;
843 /*
844 * If mapping is done, pitch it from
845 * the table.
846 */
847 if (!map->br_blockcount && --map_valid)
848 memmove(&map[0], &map[1],
849 sizeof(map[0]) *
850 map_valid);
851 i -= j;
852 }
853 }
854 /*
855 * Recalculate the readahead blocks wanted.
856 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000857 ra_want = howmany(bufsize + mp->m_dirblksize,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 mp->m_sb.sb_blocksize) - 1;
Eric Sandeen8e69ce12009-09-25 19:42:26 +0000859 ASSERT(ra_want >= 0);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /*
862 * If we don't have as many as we want, and we haven't
863 * run out of data blocks, get some more mappings.
864 */
865 if (1 + ra_want > map_blocks &&
866 map_off <
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000867 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /*
869 * Get more bmaps, fill in after the ones
870 * we already have in the table.
871 */
872 nmap = map_size - map_valid;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000873 error = xfs_bmapi(NULL, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 map_off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000875 xfs_dir2_byte_to_da(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 XFS_DIR2_LEAF_OFFSET) - map_off,
877 XFS_BMAPI_METADATA, NULL, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000878 &map[map_valid], &nmap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /*
880 * Don't know if we should ignore this or
881 * try to return an error.
882 * The trouble with returning errors
883 * is that readdir will just stop without
884 * actually passing the error through.
885 */
886 if (error)
887 break; /* XXX */
888 /*
889 * If we got all the mappings we asked for,
890 * set the final map offset based on the
891 * last bmap value received.
892 * Otherwise, we've reached the end.
893 */
894 if (nmap == map_size - map_valid)
895 map_off =
896 map[map_valid + nmap - 1].br_startoff +
897 map[map_valid + nmap - 1].br_blockcount;
898 else
899 map_off =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000900 xfs_dir2_byte_to_da(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 XFS_DIR2_LEAF_OFFSET);
902 /*
903 * Look for holes in the mapping, and
904 * eliminate them. Count up the valid blocks.
905 */
906 for (i = map_valid; i < map_valid + nmap; ) {
907 if (map[i].br_startblock ==
908 HOLESTARTBLOCK) {
909 nmap--;
910 length = map_valid + nmap - i;
911 if (length)
912 memmove(&map[i],
913 &map[i + 1],
914 sizeof(map[i]) *
915 length);
916 } else {
917 map_blocks +=
918 map[i].br_blockcount;
919 i++;
920 }
921 }
922 map_valid += nmap;
923 }
924 /*
925 * No valid mappings, so no more data blocks.
926 */
927 if (!map_valid) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000928 curoff = xfs_dir2_da_to_byte(mp, map_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 break;
930 }
931 /*
932 * Read the directory block starting at the first
933 * mapping.
934 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000935 curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000936 error = xfs_da_read_buf(NULL, dp, map->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 map->br_blockcount >= mp->m_dirblkfsbs ?
938 XFS_FSB_TO_DADDR(mp, map->br_startblock) :
939 -1,
940 &bp, XFS_DATA_FORK);
941 /*
942 * Should just skip over the data block instead
943 * of giving up.
944 */
945 if (error)
946 break; /* XXX */
947 /*
948 * Adjust the current amount of read-ahead: we just
949 * read a block that was previously ra.
950 */
951 if (ra_current)
952 ra_current -= mp->m_dirblkfsbs;
953 /*
954 * Do we need more readahead?
955 */
956 for (ra_index = ra_offset = i = 0;
957 ra_want > ra_current && i < map_blocks;
958 i += mp->m_dirblkfsbs) {
959 ASSERT(ra_index < map_valid);
960 /*
961 * Read-ahead a contiguous directory block.
962 */
963 if (i > ra_current &&
964 map[ra_index].br_blockcount >=
965 mp->m_dirblkfsbs) {
966 xfs_baread(mp->m_ddev_targp,
967 XFS_FSB_TO_DADDR(mp,
968 map[ra_index].br_startblock +
969 ra_offset),
970 (int)BTOBB(mp->m_dirblksize));
971 ra_current = i;
972 }
973 /*
974 * Read-ahead a non-contiguous directory block.
975 * This doesn't use our mapping, but this
976 * is a very rare case.
977 */
978 else if (i > ra_current) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000979 (void)xfs_da_reada_buf(NULL, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 map[ra_index].br_startoff +
981 ra_offset, XFS_DATA_FORK);
982 ra_current = i;
983 }
984 /*
985 * Advance offset through the mapping table.
986 */
987 for (j = 0; j < mp->m_dirblkfsbs; j++) {
988 /*
989 * The rest of this extent but not
990 * more than a dir block.
991 */
992 length = MIN(mp->m_dirblkfsbs,
993 (int)(map[ra_index].br_blockcount -
994 ra_offset));
995 j += length;
996 ra_offset += length;
997 /*
998 * Advance to the next mapping if
999 * this one is used up.
1000 */
1001 if (ra_offset ==
1002 map[ra_index].br_blockcount) {
1003 ra_offset = 0;
1004 ra_index++;
1005 }
1006 }
1007 }
1008 /*
1009 * Having done a read, we need to set a new offset.
1010 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001011 newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /*
1013 * Start of the current block.
1014 */
1015 if (curoff < newoff)
1016 curoff = newoff;
1017 /*
1018 * Make sure we're in the right block.
1019 */
1020 else if (curoff > newoff)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001021 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 curdb);
1023 data = bp->data;
1024 xfs_dir2_data_check(dp, bp);
1025 /*
1026 * Find our position in the block.
1027 */
1028 ptr = (char *)&data->u;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001029 byteoff = xfs_dir2_byte_to_off(mp, curoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /*
1031 * Skip past the header.
1032 */
1033 if (byteoff == 0)
1034 curoff += (uint)sizeof(data->hdr);
1035 /*
1036 * Skip past entries until we reach our offset.
1037 */
1038 else {
1039 while ((char *)ptr - (char *)data < byteoff) {
1040 dup = (xfs_dir2_data_unused_t *)ptr;
1041
Nathan Scottad354eb2006-03-17 17:27:37 +11001042 if (be16_to_cpu(dup->freetag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 == XFS_DIR2_DATA_FREE_TAG) {
1044
Nathan Scottad354eb2006-03-17 17:27:37 +11001045 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 ptr += length;
1047 continue;
1048 }
1049 dep = (xfs_dir2_data_entry_t *)ptr;
1050 length =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001051 xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 ptr += length;
1053 }
1054 /*
1055 * Now set our real offset.
1056 */
1057 curoff =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001058 xfs_dir2_db_off_to_byte(mp,
1059 xfs_dir2_byte_to_db(mp, curoff),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 (char *)ptr - (char *)data);
1061 if (ptr >= (char *)data + mp->m_dirblksize) {
1062 continue;
1063 }
1064 }
1065 }
1066 /*
1067 * We have a pointer to an entry.
1068 * Is it a live one?
1069 */
1070 dup = (xfs_dir2_data_unused_t *)ptr;
1071 /*
1072 * No, it's unused, skip over it.
1073 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001074 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1075 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 ptr += length;
1077 curoff += length;
1078 continue;
1079 }
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 dep = (xfs_dir2_data_entry_t *)ptr;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001082 length = xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001084 if (filldir(dirent, dep->name, dep->namelen,
Christoph Hellwig15440312009-01-08 14:00:00 -05001085 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
Christoph Hellwiga19d9f82009-03-29 09:51:08 +02001086 be64_to_cpu(dep->inumber), DT_UNKNOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 break;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /*
1090 * Advance to next entry in the block.
1091 */
1092 ptr += length;
1093 curoff += length;
Eric Sandeen8e69ce12009-09-25 19:42:26 +00001094 /* bufsize may have just been a guess; don't go negative */
1095 bufsize = bufsize > length ? bufsize - length : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097
1098 /*
1099 * All done. Set output offset value to current offset.
1100 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001101 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
Christoph Hellwig15440312009-01-08 14:00:00 -05001102 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 else
Christoph Hellwig15440312009-01-08 14:00:00 -05001104 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001105 kmem_free(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (bp)
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001107 xfs_da_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return error;
1109}
1110
1111/*
1112 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1113 */
1114int
1115xfs_dir2_leaf_init(
1116 xfs_da_args_t *args, /* operation arguments */
1117 xfs_dir2_db_t bno, /* directory block number */
1118 xfs_dabuf_t **bpp, /* out: leaf buffer */
1119 int magic) /* magic number for block */
1120{
1121 xfs_dabuf_t *bp; /* leaf buffer */
1122 xfs_inode_t *dp; /* incore directory inode */
1123 int error; /* error return code */
1124 xfs_dir2_leaf_t *leaf; /* leaf structure */
1125 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1126 xfs_mount_t *mp; /* filesystem mount point */
1127 xfs_trans_t *tp; /* transaction pointer */
1128
1129 dp = args->dp;
1130 ASSERT(dp != NULL);
1131 tp = args->trans;
1132 mp = dp->i_mount;
1133 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1134 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1135 /*
1136 * Get the buffer for the block.
1137 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001138 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 XFS_DATA_FORK);
1140 if (error) {
1141 return error;
1142 }
1143 ASSERT(bp != NULL);
1144 leaf = bp->data;
1145 /*
1146 * Initialize the header.
1147 */
Nathan Scott89da0542006-03-17 17:28:40 +11001148 leaf->hdr.info.magic = cpu_to_be16(magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 leaf->hdr.info.forw = 0;
1150 leaf->hdr.info.back = 0;
1151 leaf->hdr.count = 0;
1152 leaf->hdr.stale = 0;
1153 xfs_dir2_leaf_log_header(tp, bp);
1154 /*
1155 * If it's a leaf-format directory initialize the tail.
1156 * In this case our caller has the real bests table to copy into
1157 * the block.
1158 */
1159 if (magic == XFS_DIR2_LEAF1_MAGIC) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001160 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 ltp->bestcount = 0;
1162 xfs_dir2_leaf_log_tail(tp, bp);
1163 }
1164 *bpp = bp;
1165 return 0;
1166}
1167
1168/*
1169 * Log the bests entries indicated from a leaf1 block.
1170 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001171static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172xfs_dir2_leaf_log_bests(
1173 xfs_trans_t *tp, /* transaction pointer */
1174 xfs_dabuf_t *bp, /* leaf buffer */
1175 int first, /* first entry to log */
1176 int last) /* last entry to log */
1177{
Nathan Scott68b3a102006-03-17 17:27:19 +11001178 __be16 *firstb; /* pointer to first entry */
1179 __be16 *lastb; /* pointer to last entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 xfs_dir2_leaf_t *leaf; /* leaf structure */
1181 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1182
1183 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001184 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001185 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1186 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1187 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1189 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1190}
1191
1192/*
1193 * Log the leaf entries indicated from a leaf1 or leafn block.
1194 */
1195void
1196xfs_dir2_leaf_log_ents(
1197 xfs_trans_t *tp, /* transaction pointer */
1198 xfs_dabuf_t *bp, /* leaf buffer */
1199 int first, /* first entry to log */
1200 int last) /* last entry to log */
1201{
1202 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1203 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1204 xfs_dir2_leaf_t *leaf; /* leaf structure */
1205
1206 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001207 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1208 be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 firstlep = &leaf->ents[first];
1210 lastlep = &leaf->ents[last];
1211 xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1212 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1213}
1214
1215/*
1216 * Log the header of the leaf1 or leafn block.
1217 */
1218void
1219xfs_dir2_leaf_log_header(
1220 xfs_trans_t *tp, /* transaction pointer */
1221 xfs_dabuf_t *bp) /* leaf buffer */
1222{
1223 xfs_dir2_leaf_t *leaf; /* leaf structure */
1224
1225 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001226 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1227 be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1229 (uint)(sizeof(leaf->hdr) - 1));
1230}
1231
1232/*
1233 * Log the tail of the leaf1 block.
1234 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001235STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236xfs_dir2_leaf_log_tail(
1237 xfs_trans_t *tp, /* transaction pointer */
1238 xfs_dabuf_t *bp) /* leaf buffer */
1239{
1240 xfs_dir2_leaf_t *leaf; /* leaf structure */
1241 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1242 xfs_mount_t *mp; /* filesystem mount point */
1243
1244 mp = tp->t_mountp;
1245 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001246 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001247 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1249 (uint)(mp->m_dirblksize - 1));
1250}
1251
1252/*
1253 * Look up the entry referred to by args in the leaf format directory.
1254 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1255 * is also used by the node-format code.
1256 */
1257int
1258xfs_dir2_leaf_lookup(
1259 xfs_da_args_t *args) /* operation arguments */
1260{
1261 xfs_dabuf_t *dbp; /* data block buffer */
1262 xfs_dir2_data_entry_t *dep; /* data block entry */
1263 xfs_inode_t *dp; /* incore directory inode */
1264 int error; /* error return code */
1265 int index; /* found entry index */
1266 xfs_dabuf_t *lbp; /* leaf buffer */
1267 xfs_dir2_leaf_t *leaf; /* leaf structure */
1268 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1269 xfs_trans_t *tp; /* transaction pointer */
1270
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001271 trace_xfs_dir2_leaf_lookup(args);
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /*
1274 * Look up name in the leaf block, returning both buffers and index.
1275 */
1276 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1277 return error;
1278 }
1279 tp = args->trans;
1280 dp = args->dp;
1281 xfs_dir2_leaf_check(dp, lbp);
1282 leaf = lbp->data;
1283 /*
1284 * Get to the leaf entry and contained data entry address.
1285 */
1286 lep = &leaf->ents[index];
1287 /*
1288 * Point to the data entry.
1289 */
1290 dep = (xfs_dir2_data_entry_t *)
1291 ((char *)dbp->data +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001292 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +10001294 * Return the found inode number & CI name if appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001296 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001297 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 xfs_da_brelse(tp, dbp);
1299 xfs_da_brelse(tp, lbp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001300 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
1303/*
1304 * Look up name/hash in the leaf block.
1305 * Fill in indexp with the found index, and dbpp with the data buffer.
1306 * If not found dbpp will be NULL, and ENOENT comes back.
1307 * lbpp will always be filled in with the leaf buffer unless there's an error.
1308 */
1309static int /* error */
1310xfs_dir2_leaf_lookup_int(
1311 xfs_da_args_t *args, /* operation arguments */
1312 xfs_dabuf_t **lbpp, /* out: leaf buffer */
1313 int *indexp, /* out: index in leaf block */
1314 xfs_dabuf_t **dbpp) /* out: data buffer */
1315{
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001316 xfs_dir2_db_t curdb = -1; /* current data block number */
1317 xfs_dabuf_t *dbp = NULL; /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 xfs_dir2_data_entry_t *dep; /* data entry */
1319 xfs_inode_t *dp; /* incore directory inode */
1320 int error; /* error return code */
1321 int index; /* index in leaf block */
1322 xfs_dabuf_t *lbp; /* leaf buffer */
1323 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1324 xfs_dir2_leaf_t *leaf; /* leaf structure */
1325 xfs_mount_t *mp; /* filesystem mount point */
1326 xfs_dir2_db_t newdb; /* new data block number */
1327 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001328 xfs_dir2_db_t cidb = -1; /* case match data block no. */
Barry Naujok5163f952008-05-21 16:41:01 +10001329 enum xfs_dacmp cmp; /* name compare result */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 dp = args->dp;
1332 tp = args->trans;
1333 mp = dp->i_mount;
1334 /*
1335 * Read the leaf block into the buffer.
1336 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001337 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1338 XFS_DATA_FORK);
1339 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 *lbpp = lbp;
1342 leaf = lbp->data;
1343 xfs_dir2_leaf_check(dp, lbp);
1344 /*
1345 * Look for the first leaf entry with our hash value.
1346 */
1347 index = xfs_dir2_leaf_search_hash(args, lbp);
1348 /*
1349 * Loop over all the entries with the right hash value
1350 * looking to match the name.
1351 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001352 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
Barry Naujok5163f952008-05-21 16:41:01 +10001353 be32_to_cpu(lep->hashval) == args->hashval;
1354 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 /*
1356 * Skip over stale leaf entries.
1357 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001358 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 continue;
1360 /*
1361 * Get the new data block number.
1362 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001363 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /*
1365 * If it's not the same as the old data block number,
1366 * need to pitch the old one and read the new one.
1367 */
1368 if (newdb != curdb) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001369 if (dbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 xfs_da_brelse(tp, dbp);
Barry Naujok5163f952008-05-21 16:41:01 +10001371 error = xfs_da_read_buf(tp, dp,
1372 xfs_dir2_db_to_da(mp, newdb),
1373 -1, &dbp, XFS_DATA_FORK);
1374 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 xfs_da_brelse(tp, lbp);
1376 return error;
1377 }
1378 xfs_dir2_data_check(dp, dbp);
1379 curdb = newdb;
1380 }
1381 /*
1382 * Point to the data entry.
1383 */
Barry Naujok5163f952008-05-21 16:41:01 +10001384 dep = (xfs_dir2_data_entry_t *)((char *)dbp->data +
1385 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /*
Barry Naujok5163f952008-05-21 16:41:01 +10001387 * Compare name and if it's an exact match, return the index
1388 * and buffer. If it's the first case-insensitive match, store
1389 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 */
Barry Naujok5163f952008-05-21 16:41:01 +10001391 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1392 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1393 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 *indexp = index;
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001395 /* case exact match: return the current buffer. */
Barry Naujok5163f952008-05-21 16:41:01 +10001396 if (cmp == XFS_CMP_EXACT) {
Barry Naujok5163f952008-05-21 16:41:01 +10001397 *dbpp = dbp;
1398 return 0;
1399 }
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001400 cidb = curdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402 }
Barry Naujok6a178102008-05-21 16:42:05 +10001403 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +10001404 /*
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001405 * Here, we can only be doing a lookup (not a rename or remove).
1406 * If a case-insensitive match was found earlier, re-read the
1407 * appropriate data block if required and return it.
Barry Naujok5163f952008-05-21 16:41:01 +10001408 */
1409 if (args->cmpresult == XFS_CMP_CASE) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001410 ASSERT(cidb != -1);
1411 if (cidb != curdb) {
Barry Naujok5163f952008-05-21 16:41:01 +10001412 xfs_da_brelse(tp, dbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001413 error = xfs_da_read_buf(tp, dp,
1414 xfs_dir2_db_to_da(mp, cidb),
1415 -1, &dbp, XFS_DATA_FORK);
1416 if (error) {
1417 xfs_da_brelse(tp, lbp);
1418 return error;
1419 }
1420 }
1421 *dbpp = dbp;
Barry Naujok5163f952008-05-21 16:41:01 +10001422 return 0;
1423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 /*
1425 * No match found, return ENOENT.
1426 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001427 ASSERT(cidb == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (dbp)
1429 xfs_da_brelse(tp, dbp);
1430 xfs_da_brelse(tp, lbp);
1431 return XFS_ERROR(ENOENT);
1432}
1433
1434/*
1435 * Remove an entry from a leaf format directory.
1436 */
1437int /* error */
1438xfs_dir2_leaf_removename(
1439 xfs_da_args_t *args) /* operation arguments */
1440{
Nathan Scott68b3a102006-03-17 17:27:19 +11001441 __be16 *bestsp; /* leaf block best freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 xfs_dir2_data_t *data; /* data block structure */
1443 xfs_dir2_db_t db; /* data block number */
1444 xfs_dabuf_t *dbp; /* data block buffer */
1445 xfs_dir2_data_entry_t *dep; /* data entry structure */
1446 xfs_inode_t *dp; /* incore directory inode */
1447 int error; /* error return code */
1448 xfs_dir2_db_t i; /* temporary data block # */
1449 int index; /* index into leaf entries */
1450 xfs_dabuf_t *lbp; /* leaf buffer */
1451 xfs_dir2_leaf_t *leaf; /* leaf structure */
1452 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1453 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1454 xfs_mount_t *mp; /* filesystem mount point */
1455 int needlog; /* need to log data header */
1456 int needscan; /* need to rescan data frees */
1457 xfs_dir2_data_off_t oldbest; /* old value of best free */
1458 xfs_trans_t *tp; /* transaction pointer */
1459
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001460 trace_xfs_dir2_leaf_removename(args);
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /*
1463 * Lookup the leaf entry, get the leaf and data blocks read in.
1464 */
1465 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1466 return error;
1467 }
1468 dp = args->dp;
1469 tp = args->trans;
1470 mp = dp->i_mount;
1471 leaf = lbp->data;
1472 data = dbp->data;
1473 xfs_dir2_data_check(dp, dbp);
1474 /*
1475 * Point to the leaf entry, use that to point to the data entry.
1476 */
1477 lep = &leaf->ents[index];
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001478 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001480 ((char *)data + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 needscan = needlog = 0;
Nathan Scott70e73f52006-03-17 17:26:52 +11001482 oldbest = be16_to_cpu(data->hdr.bestfree[0].length);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001483 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1484 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott68b3a102006-03-17 17:27:19 +11001485 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /*
1487 * Mark the former data entry unused.
1488 */
1489 xfs_dir2_data_make_free(tp, dbp,
1490 (xfs_dir2_data_aoff_t)((char *)dep - (char *)data),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001491 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 /*
1493 * We just mark the leaf entry stale by putting a null in it.
1494 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001495 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001497 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1499 /*
1500 * Scan the freespace in the data block again if necessary,
1501 * log the data block header if necessary.
1502 */
1503 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +10001504 xfs_dir2_data_freescan(mp, data, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (needlog)
1506 xfs_dir2_data_log_header(tp, dbp);
1507 /*
1508 * If the longest freespace in the data block has changed,
1509 * put the new value in the bests table and log that.
1510 */
Nathan Scott70e73f52006-03-17 17:26:52 +11001511 if (be16_to_cpu(data->hdr.bestfree[0].length) != oldbest) {
1512 bestsp[db] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1514 }
1515 xfs_dir2_data_check(dp, dbp);
1516 /*
1517 * If the data block is now empty then get rid of the data block.
1518 */
Nathan Scott70e73f52006-03-17 17:26:52 +11001519 if (be16_to_cpu(data->hdr.bestfree[0].length) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 mp->m_dirblksize - (uint)sizeof(data->hdr)) {
1521 ASSERT(db != mp->m_dirdatablk);
1522 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1523 /*
1524 * Nope, can't get rid of it because it caused
1525 * allocation of a bmap btree block to do so.
1526 * Just go on, returning success, leaving the
1527 * empty block in place.
1528 */
1529 if (error == ENOSPC && args->total == 0) {
1530 xfs_da_buf_done(dbp);
1531 error = 0;
1532 }
1533 xfs_dir2_leaf_check(dp, lbp);
1534 xfs_da_buf_done(lbp);
1535 return error;
1536 }
1537 dbp = NULL;
1538 /*
1539 * If this is the last data block then compact the
1540 * bests table by getting rid of entries.
1541 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001542 if (db == be32_to_cpu(ltp->bestcount) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 /*
1544 * Look for the last active entry (i).
1545 */
1546 for (i = db - 1; i > 0; i--) {
Nathan Scott68b3a102006-03-17 17:27:19 +11001547 if (be16_to_cpu(bestsp[i]) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 break;
1549 }
1550 /*
1551 * Copy the table down so inactive entries at the
1552 * end are removed.
1553 */
1554 memmove(&bestsp[db - i], bestsp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001555 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001556 be32_add_cpu(&ltp->bestcount, -(db - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001558 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 } else
Nathan Scott68b3a102006-03-17 17:27:19 +11001560 bestsp[db] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
1562 /*
1563 * If the data block was not the first one, drop it.
1564 */
1565 else if (db != mp->m_dirdatablk && dbp != NULL) {
1566 xfs_da_buf_done(dbp);
1567 dbp = NULL;
1568 }
1569 xfs_dir2_leaf_check(dp, lbp);
1570 /*
1571 * See if we can convert to block form.
1572 */
1573 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1574}
1575
1576/*
1577 * Replace the inode number in a leaf format directory entry.
1578 */
1579int /* error */
1580xfs_dir2_leaf_replace(
1581 xfs_da_args_t *args) /* operation arguments */
1582{
1583 xfs_dabuf_t *dbp; /* data block buffer */
1584 xfs_dir2_data_entry_t *dep; /* data block entry */
1585 xfs_inode_t *dp; /* incore directory inode */
1586 int error; /* error return code */
1587 int index; /* index of leaf entry */
1588 xfs_dabuf_t *lbp; /* leaf buffer */
1589 xfs_dir2_leaf_t *leaf; /* leaf structure */
1590 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1591 xfs_trans_t *tp; /* transaction pointer */
1592
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001593 trace_xfs_dir2_leaf_replace(args);
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 /*
1596 * Look up the entry.
1597 */
1598 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1599 return error;
1600 }
1601 dp = args->dp;
1602 leaf = lbp->data;
1603 /*
1604 * Point to the leaf entry, get data address from it.
1605 */
1606 lep = &leaf->ents[index];
1607 /*
1608 * Point to the data entry.
1609 */
1610 dep = (xfs_dir2_data_entry_t *)
1611 ((char *)dbp->data +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001612 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001613 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /*
1615 * Put the new inode number in, log it.
1616 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001617 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 tp = args->trans;
1619 xfs_dir2_data_log_entry(tp, dbp, dep);
1620 xfs_da_buf_done(dbp);
1621 xfs_dir2_leaf_check(dp, lbp);
1622 xfs_da_brelse(tp, lbp);
1623 return 0;
1624}
1625
1626/*
1627 * Return index in the leaf block (lbp) which is either the first
1628 * one with this hash value, or if there are none, the insert point
1629 * for that hash value.
1630 */
1631int /* index value */
1632xfs_dir2_leaf_search_hash(
1633 xfs_da_args_t *args, /* operation arguments */
1634 xfs_dabuf_t *lbp) /* leaf buffer */
1635{
1636 xfs_dahash_t hash=0; /* hash from this entry */
1637 xfs_dahash_t hashwant; /* hash value looking for */
1638 int high; /* high leaf index */
1639 int low; /* low leaf index */
1640 xfs_dir2_leaf_t *leaf; /* leaf structure */
1641 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1642 int mid=0; /* current leaf index */
1643
1644 leaf = lbp->data;
1645#ifndef __KERNEL__
1646 if (!leaf->hdr.count)
1647 return 0;
1648#endif
1649 /*
1650 * Note, the table cannot be empty, so we have to go through the loop.
1651 * Binary search the leaf entries looking for our hash value.
1652 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001653 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 hashwant = args->hashval;
1655 low <= high; ) {
1656 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001657 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 break;
1659 if (hash < hashwant)
1660 low = mid + 1;
1661 else
1662 high = mid - 1;
1663 }
1664 /*
1665 * Found one, back up through all the equal hash values.
1666 */
1667 if (hash == hashwant) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001668 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 mid--;
1670 }
1671 }
1672 /*
1673 * Need to point to an entry higher than ours.
1674 */
1675 else if (hash < hashwant)
1676 mid++;
1677 return mid;
1678}
1679
1680/*
1681 * Trim off a trailing data block. We know it's empty since the leaf
1682 * freespace table says so.
1683 */
1684int /* error */
1685xfs_dir2_leaf_trim_data(
1686 xfs_da_args_t *args, /* operation arguments */
1687 xfs_dabuf_t *lbp, /* leaf buffer */
1688 xfs_dir2_db_t db) /* data block number */
1689{
Nathan Scott68b3a102006-03-17 17:27:19 +11001690 __be16 *bestsp; /* leaf bests table */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691#ifdef DEBUG
1692 xfs_dir2_data_t *data; /* data block structure */
1693#endif
1694 xfs_dabuf_t *dbp; /* data block buffer */
1695 xfs_inode_t *dp; /* incore directory inode */
1696 int error; /* error return value */
1697 xfs_dir2_leaf_t *leaf; /* leaf structure */
1698 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1699 xfs_mount_t *mp; /* filesystem mount point */
1700 xfs_trans_t *tp; /* transaction pointer */
1701
1702 dp = args->dp;
1703 mp = dp->i_mount;
1704 tp = args->trans;
1705 /*
1706 * Read the offending data block. We need its buffer.
1707 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001708 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 XFS_DATA_FORK))) {
1710 return error;
1711 }
1712#ifdef DEBUG
1713 data = dbp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001714 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715#endif
1716 /* this seems to be an error
1717 * data is only valid if DEBUG is defined?
1718 * RMC 09/08/1999
1719 */
1720
1721 leaf = lbp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001722 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scott70e73f52006-03-17 17:26:52 +11001723 ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 mp->m_dirblksize - (uint)sizeof(data->hdr));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001725 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 /*
1727 * Get rid of the data block.
1728 */
1729 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1730 ASSERT(error != ENOSPC);
1731 xfs_da_brelse(tp, dbp);
1732 return error;
1733 }
1734 /*
1735 * Eliminate the last bests entry from the table.
1736 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001737 bestsp = xfs_dir2_leaf_bests_p(ltp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001738 be32_add_cpu(&ltp->bestcount, -1);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001739 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001741 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return 0;
1743}
1744
1745/*
1746 * Convert node form directory to leaf form directory.
1747 * The root of the node form dir needs to already be a LEAFN block.
1748 * Just return if we can't do anything.
1749 */
1750int /* error */
1751xfs_dir2_node_to_leaf(
1752 xfs_da_state_t *state) /* directory operation state */
1753{
1754 xfs_da_args_t *args; /* operation arguments */
1755 xfs_inode_t *dp; /* incore directory inode */
1756 int error; /* error return code */
1757 xfs_dabuf_t *fbp; /* buffer for freespace block */
1758 xfs_fileoff_t fo; /* freespace file offset */
1759 xfs_dir2_free_t *free; /* freespace structure */
1760 xfs_dabuf_t *lbp; /* buffer for leaf block */
1761 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1762 xfs_dir2_leaf_t *leaf; /* leaf structure */
1763 xfs_mount_t *mp; /* filesystem mount point */
1764 int rval; /* successful free trim? */
1765 xfs_trans_t *tp; /* transaction pointer */
1766
1767 /*
1768 * There's more than a leaf level in the btree, so there must
1769 * be multiple leafn blocks. Give up.
1770 */
1771 if (state->path.active > 1)
1772 return 0;
1773 args = state->args;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001774
1775 trace_xfs_dir2_node_to_leaf(args);
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 mp = state->mp;
1778 dp = args->dp;
1779 tp = args->trans;
1780 /*
1781 * Get the last offset in the file.
1782 */
1783 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1784 return error;
1785 }
1786 fo -= mp->m_dirblkfsbs;
1787 /*
1788 * If there are freespace blocks other than the first one,
1789 * take this opportunity to remove trailing empty freespace blocks
1790 * that may have been left behind during no-space-reservation
1791 * operations.
1792 */
1793 while (fo > mp->m_dirfreeblk) {
1794 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1795 return error;
1796 }
1797 if (rval)
1798 fo -= mp->m_dirblkfsbs;
1799 else
1800 return 0;
1801 }
1802 /*
1803 * Now find the block just before the freespace block.
1804 */
1805 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1806 return error;
1807 }
1808 /*
1809 * If it's not the single leaf block, give up.
1810 */
1811 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1812 return 0;
1813 lbp = state->path.blk[0].bp;
1814 leaf = lbp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001815 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 /*
1817 * Read the freespace block.
1818 */
1819 if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
1820 XFS_DATA_FORK))) {
1821 return error;
1822 }
1823 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001824 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 ASSERT(!free->hdr.firstdb);
1826 /*
1827 * Now see if the leafn and free data will fit in a leaf1.
1828 * If not, release the buffer and give up.
1829 */
1830 if ((uint)sizeof(leaf->hdr) +
Nathan Scotta818e5d2006-03-17 17:28:07 +11001831 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)) * (uint)sizeof(leaf->ents[0]) +
Nathan Scott0ba962e2006-03-17 17:27:07 +11001832 be32_to_cpu(free->hdr.nvalid) * (uint)sizeof(leaf->bests[0]) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 (uint)sizeof(leaf->tail) >
1834 mp->m_dirblksize) {
1835 xfs_da_brelse(tp, fbp);
1836 return 0;
1837 }
1838 /*
1839 * If the leaf has any stale entries in it, compress them out.
1840 * The compact routine will log the header.
1841 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001842 if (be16_to_cpu(leaf->hdr.stale))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 xfs_dir2_leaf_compact(args, lbp);
1844 else
1845 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott89da0542006-03-17 17:28:40 +11001846 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 /*
1848 * Set up the leaf tail from the freespace block.
1849 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001850 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scott0ba962e2006-03-17 17:27:07 +11001851 ltp->bestcount = free->hdr.nvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 /*
1853 * Set up the leaf bests table.
1854 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001855 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001856 be32_to_cpu(ltp->bestcount) * sizeof(leaf->bests[0]));
1857 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 xfs_dir2_leaf_log_tail(tp, lbp);
1859 xfs_dir2_leaf_check(dp, lbp);
1860 /*
1861 * Get rid of the freespace block.
1862 */
1863 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1864 if (error) {
1865 /*
1866 * This can't fail here because it can only happen when
1867 * punching out the middle of an extent, and this is an
1868 * isolated block.
1869 */
1870 ASSERT(error != ENOSPC);
1871 return error;
1872 }
1873 fbp = NULL;
1874 /*
1875 * Now see if we can convert the single-leaf directory
1876 * down to a block form directory.
1877 * This routine always kills the dabuf for the leaf, so
1878 * eliminate it from the path.
1879 */
1880 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1881 state->path.blk[0].bp = NULL;
1882 return error;
1883}