blob: 66e108f561a39aa9efcc1d4199bce74e0068855d [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_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dinode.h"
31#include "xfs_inode.h"
32#include "xfs_bmap.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020033#include "xfs_dir2_format.h"
34#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000036#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * Local function declarations.
40 */
41#ifdef DEBUG
42static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
43#else
44#define xfs_dir2_leaf_check(dp, bp)
45#endif
46static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **lbpp,
47 int *indexp, xfs_dabuf_t **dbpp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100048static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp,
49 int first, int last);
Eric Sandeen6add2c42005-06-21 15:39:44 +100050static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*
54 * Convert a block form directory to a leaf form directory.
55 */
56int /* error */
57xfs_dir2_block_to_leaf(
58 xfs_da_args_t *args, /* operation arguments */
59 xfs_dabuf_t *dbp) /* input block's buffer */
60{
Nathan Scott68b3a102006-03-17 17:27:19 +110061 __be16 *bestsp; /* leaf's bestsp entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 xfs_dablk_t blkno; /* leaf block's bno */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +020063 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
65 xfs_dir2_block_tail_t *btp; /* block's tail */
66 xfs_inode_t *dp; /* incore directory inode */
67 int error; /* error return code */
68 xfs_dabuf_t *lbp; /* leaf block's buffer */
69 xfs_dir2_db_t ldb; /* leaf block's bno */
70 xfs_dir2_leaf_t *leaf; /* leaf structure */
71 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
72 xfs_mount_t *mp; /* filesystem mount point */
73 int needlog; /* need to log block header */
74 int needscan; /* need to rescan bestfree */
75 xfs_trans_t *tp; /* transaction pointer */
76
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000077 trace_xfs_dir2_block_to_leaf(args);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 dp = args->dp;
80 mp = dp->i_mount;
81 tp = args->trans;
82 /*
83 * Add the leaf block to the inode.
84 * This interface will only put blocks in the leaf/node range.
85 * Since that's empty now, we'll get the root (block 0 in range).
86 */
87 if ((error = xfs_da_grow_inode(args, &blkno))) {
88 return error;
89 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100090 ldb = xfs_dir2_da_to_db(mp, blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
92 /*
93 * Initialize the leaf block, get a buffer for it.
94 */
95 if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
96 return error;
97 }
98 ASSERT(lbp != NULL);
99 leaf = lbp->data;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200100 hdr = dbp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 xfs_dir2_data_check(dp, dbp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200102 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000103 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /*
105 * Set the counts in the leaf header.
106 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100107 leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
108 leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /*
110 * Could compact these but I think we always do the conversion
111 * after squeezing out stale entries.
112 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100113 memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100114 xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 needscan = 0;
116 needlog = 1;
117 /*
118 * Make the space formerly occupied by the leaf entries and block
119 * tail be free.
120 */
121 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200122 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
123 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 (char *)blp),
125 &needlog, &needscan);
126 /*
127 * Fix up the block header, make it a data block.
128 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200129 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200131 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 /*
133 * Set up leaf tail and bests table.
134 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000135 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100136 ltp->bestcount = cpu_to_be32(1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000137 bestsp = xfs_dir2_leaf_bests_p(ltp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200138 bestsp[0] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /*
140 * Log the data header and leaf bests table.
141 */
142 if (needlog)
143 xfs_dir2_data_log_header(tp, dbp);
144 xfs_dir2_leaf_check(dp, lbp);
145 xfs_dir2_data_check(dp, dbp);
146 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
147 xfs_da_buf_done(lbp);
148 return 0;
149}
150
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200151STATIC void
152xfs_dir2_leaf_find_stale(
153 struct xfs_dir2_leaf *leaf,
154 int index,
155 int *lowstale,
156 int *highstale)
157{
158 /*
159 * Find the first stale entry before our index, if any.
160 */
161 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
162 if (leaf->ents[*lowstale].address ==
163 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
164 break;
165 }
166
167 /*
168 * Find the first stale entry at or after our index, if any.
169 * Stop if the result would require moving more entries than using
170 * lowstale.
171 */
172 for (*highstale = index;
173 *highstale < be16_to_cpu(leaf->hdr.count);
174 ++*highstale) {
175 if (leaf->ents[*highstale].address ==
176 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
177 break;
178 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
179 break;
180 }
181}
182
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200183struct xfs_dir2_leaf_entry *
184xfs_dir2_leaf_find_entry(
185 xfs_dir2_leaf_t *leaf, /* leaf structure */
186 int index, /* leaf table position */
187 int compact, /* need to compact leaves */
188 int lowstale, /* index of prev stale leaf */
189 int highstale, /* index of next stale leaf */
190 int *lfloglow, /* low leaf logging index */
191 int *lfloghigh) /* high leaf logging index */
192{
193 if (!leaf->hdr.stale) {
194 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
195
196 /*
197 * Now we need to make room to insert the leaf entry.
198 *
199 * If there are no stale entries, just insert a hole at index.
200 */
201 lep = &leaf->ents[index];
202 if (index < be16_to_cpu(leaf->hdr.count))
203 memmove(lep + 1, lep,
204 (be16_to_cpu(leaf->hdr.count) - index) *
205 sizeof(*lep));
206
207 /*
208 * Record low and high logging indices for the leaf.
209 */
210 *lfloglow = index;
211 *lfloghigh = be16_to_cpu(leaf->hdr.count);
212 be16_add_cpu(&leaf->hdr.count, 1);
213 return lep;
214 }
215
216 /*
217 * There are stale entries.
218 *
219 * We will use one of them for the new entry. It's probably not at
220 * the right location, so we'll have to shift some up or down first.
221 *
222 * If we didn't compact before, we need to find the nearest stale
223 * entries before and after our insertion point.
224 */
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200225 if (compact == 0)
226 xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200227
228 /*
229 * If the low one is better, use it.
230 */
231 if (lowstale >= 0 &&
232 (highstale == be16_to_cpu(leaf->hdr.count) ||
233 index - lowstale - 1 < highstale - index)) {
234 ASSERT(index - lowstale - 1 >= 0);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200235 ASSERT(leaf->ents[lowstale].address ==
236 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200237
238 /*
239 * Copy entries up to cover the stale entry and make room
240 * for the new entry.
241 */
242 if (index - lowstale - 1 > 0) {
243 memmove(&leaf->ents[lowstale],
244 &leaf->ents[lowstale + 1],
245 (index - lowstale - 1) *
246 sizeof(xfs_dir2_leaf_entry_t));
247 }
248 *lfloglow = MIN(lowstale, *lfloglow);
249 *lfloghigh = MAX(index - 1, *lfloghigh);
250 be16_add_cpu(&leaf->hdr.stale, -1);
251 return &leaf->ents[index - 1];
252 }
253
254 /*
255 * The high one is better, so use that one.
256 */
257 ASSERT(highstale - index >= 0);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200258 ASSERT(leaf->ents[highstale].address ==
259 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200260
261 /*
262 * Copy entries down to cover the stale entry and make room for the
263 * new entry.
264 */
265 if (highstale - index > 0) {
266 memmove(&leaf->ents[index + 1],
267 &leaf->ents[index],
268 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
269 }
270 *lfloglow = MIN(index, *lfloglow);
271 *lfloghigh = MAX(highstale, *lfloghigh);
272 be16_add_cpu(&leaf->hdr.stale, -1);
273 return &leaf->ents[index];
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/*
277 * Add an entry to a leaf form directory.
278 */
279int /* error */
280xfs_dir2_leaf_addname(
281 xfs_da_args_t *args) /* operation arguments */
282{
Nathan Scott68b3a102006-03-17 17:27:19 +1100283 __be16 *bestsp; /* freespace table in leaf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 int compact; /* need to compact leaves */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200285 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 xfs_dabuf_t *dbp; /* data block buffer */
287 xfs_dir2_data_entry_t *dep; /* data block entry */
288 xfs_inode_t *dp; /* incore directory inode */
289 xfs_dir2_data_unused_t *dup; /* data unused entry */
290 int error; /* error return value */
291 int grown; /* allocated new data block */
292 int highstale; /* index of next stale leaf */
293 int i; /* temporary, index */
294 int index; /* leaf table position */
295 xfs_dabuf_t *lbp; /* leaf's buffer */
296 xfs_dir2_leaf_t *leaf; /* leaf structure */
297 int length; /* length of new entry */
298 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
299 int lfloglow; /* low leaf logging index */
300 int lfloghigh; /* high leaf logging index */
301 int lowstale; /* index of prev stale leaf */
302 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
303 xfs_mount_t *mp; /* filesystem mount point */
304 int needbytes; /* leaf block bytes needed */
305 int needlog; /* need to log data header */
306 int needscan; /* need to rescan data free */
Nathan Scott3d693c62006-03-17 17:28:27 +1100307 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 xfs_trans_t *tp; /* transaction pointer */
309 xfs_dir2_db_t use_block; /* data block number */
310
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000311 trace_xfs_dir2_leaf_addname(args);
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 dp = args->dp;
314 tp = args->trans;
315 mp = dp->i_mount;
316 /*
317 * Read the leaf block.
318 */
319 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
320 XFS_DATA_FORK);
321 if (error) {
322 return error;
323 }
324 ASSERT(lbp != NULL);
325 /*
326 * Look up the entry by hash value and name.
327 * We know it's not there, our caller has already done a lookup.
328 * So the index is of the entry to insert in front of.
329 * But if there are dup hash values the index is of the first of those.
330 */
331 index = xfs_dir2_leaf_search_hash(args, lbp);
332 leaf = lbp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000333 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
334 bestsp = xfs_dir2_leaf_bests_p(ltp);
335 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /*
337 * See if there are any entries with the same hash value
338 * and space in their block for the new entry.
339 * This is good because it puts multiple same-hash value entries
340 * in a data block, improving the lookup of those entries.
341 */
342 for (use_block = -1, lep = &leaf->ents[index];
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100343 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 index++, lep++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100345 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 continue;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000347 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100348 ASSERT(i < be32_to_cpu(ltp->bestcount));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200349 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
Nathan Scott68b3a102006-03-17 17:27:19 +1100350 if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 use_block = i;
352 break;
353 }
354 }
355 /*
356 * Didn't find a block yet, linear search all the data blocks.
357 */
358 if (use_block == -1) {
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100359 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /*
361 * Remember a block we see that's missing.
362 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200363 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
364 use_block == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 use_block = i;
Nathan Scott68b3a102006-03-17 17:27:19 +1100366 else if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 use_block = i;
368 break;
369 }
370 }
371 }
372 /*
373 * How many bytes do we need in the leaf block?
374 */
Christoph Hellwig22823962011-07-08 14:35:53 +0200375 needbytes = 0;
376 if (!leaf->hdr.stale)
377 needbytes += sizeof(xfs_dir2_leaf_entry_t);
378 if (use_block == -1)
379 needbytes += sizeof(xfs_dir2_data_off_t);
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /*
382 * Now kill use_block if it refers to a missing block, so we
383 * can use it as an indication of allocation needed.
384 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200385 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 use_block = -1;
387 /*
388 * If we don't have enough free bytes but we can make enough
389 * by compacting out stale entries, we'll do that.
390 */
Barry Naujok6a178102008-05-21 16:42:05 +1000391 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
392 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 compact = 1;
394 }
395 /*
396 * Otherwise if we don't have enough free bytes we need to
397 * convert to node form.
398 */
Barry Naujok6a178102008-05-21 16:42:05 +1000399 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
400 leaf->hdr.count)] < needbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /*
402 * Just checking or no space reservation, give up.
403 */
Barry Naujok6a178102008-05-21 16:42:05 +1000404 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
405 args->total == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 xfs_da_brelse(tp, lbp);
407 return XFS_ERROR(ENOSPC);
408 }
409 /*
410 * Convert to node form.
411 */
412 error = xfs_dir2_leaf_to_node(args, lbp);
413 xfs_da_buf_done(lbp);
414 if (error)
415 return error;
416 /*
417 * Then add the new entry.
418 */
419 return xfs_dir2_node_addname(args);
420 }
421 /*
422 * Otherwise it will fit without compaction.
423 */
424 else
425 compact = 0;
426 /*
427 * If just checking, then it will fit unless we needed to allocate
428 * a new data block.
429 */
Barry Naujok6a178102008-05-21 16:42:05 +1000430 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 xfs_da_brelse(tp, lbp);
432 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
433 }
434 /*
435 * If no allocations are allowed, return now before we've
436 * changed anything.
437 */
438 if (args->total == 0 && use_block == -1) {
439 xfs_da_brelse(tp, lbp);
440 return XFS_ERROR(ENOSPC);
441 }
442 /*
443 * Need to compact the leaf entries, removing stale ones.
444 * Leave one stale entry behind - the one closest to our
445 * insertion index - and we'll shift that one to our insertion
446 * point later.
447 */
448 if (compact) {
449 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
450 &lfloglow, &lfloghigh);
451 }
452 /*
453 * There are stale entries, so we'll need log-low and log-high
454 * impossibly bad values later.
455 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100456 else if (be16_to_cpu(leaf->hdr.stale)) {
457 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 lfloghigh = -1;
459 }
460 /*
461 * If there was no data block space found, we need to allocate
462 * a new one.
463 */
464 if (use_block == -1) {
465 /*
466 * Add the new data block.
467 */
468 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
469 &use_block))) {
470 xfs_da_brelse(tp, lbp);
471 return error;
472 }
473 /*
474 * Initialize the block.
475 */
476 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
477 xfs_da_brelse(tp, lbp);
478 return error;
479 }
480 /*
481 * If we're adding a new data block on the end we need to
482 * extend the bests table. Copy it up one entry.
483 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100484 if (use_block >= be32_to_cpu(ltp->bestcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 bestsp--;
486 memmove(&bestsp[0], &bestsp[1],
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100487 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800488 be32_add_cpu(&ltp->bestcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100490 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 /*
493 * If we're filling in a previously empty block just log it.
494 */
495 else
496 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200497 hdr = dbp->data;
498 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 grown = 1;
500 }
501 /*
502 * Already had space in some data block.
503 * Just read that one in.
504 */
505 else {
506 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000507 xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 -1, &dbp, XFS_DATA_FORK))) {
509 xfs_da_brelse(tp, lbp);
510 return error;
511 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200512 hdr = dbp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 grown = 0;
514 }
515 xfs_dir2_data_check(dp, dbp);
516 /*
517 * Point to the biggest freespace in our data block.
518 */
519 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200520 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
Nathan Scottad354eb2006-03-17 17:27:37 +1100521 ASSERT(be16_to_cpu(dup->length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 needscan = needlog = 0;
523 /*
524 * Mark the initial part of our freespace in use for the new entry.
525 */
526 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200527 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 &needlog, &needscan);
529 /*
530 * Initialize our new entry (at last).
531 */
532 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000533 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 dep->namelen = args->namelen;
535 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000536 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200537 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /*
539 * Need to scan fix up the bestfree table.
540 */
541 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200542 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /*
544 * Need to log the data block's header.
545 */
546 if (needlog)
547 xfs_dir2_data_log_header(tp, dbp);
548 xfs_dir2_data_log_entry(tp, dbp, dep);
549 /*
550 * If the bests table needs to be changed, do it.
551 * Log the change unless we've already done that.
552 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200553 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
554 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (!grown)
556 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
557 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200558
559 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
560 highstale, &lfloglow, &lfloghigh);
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /*
563 * Fill in the new leaf entry.
564 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100565 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000566 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
Nathan Scott3d693c62006-03-17 17:28:27 +1100567 be16_to_cpu(*tagp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /*
569 * Log the leaf fields and give up the buffers.
570 */
571 xfs_dir2_leaf_log_header(tp, lbp);
572 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
573 xfs_dir2_leaf_check(dp, lbp);
574 xfs_da_buf_done(lbp);
575 xfs_dir2_data_check(dp, dbp);
576 xfs_da_buf_done(dbp);
577 return 0;
578}
579
580#ifdef DEBUG
581/*
582 * Check the internal consistency of a leaf1 block.
583 * Pop an assert if something is wrong.
584 */
Hannes Eder3180e662009-03-04 19:34:10 +0100585STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586xfs_dir2_leaf_check(
587 xfs_inode_t *dp, /* incore directory inode */
588 xfs_dabuf_t *bp) /* leaf's buffer */
589{
590 int i; /* leaf index */
591 xfs_dir2_leaf_t *leaf; /* leaf structure */
592 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
593 xfs_mount_t *mp; /* filesystem mount point */
594 int stale; /* count of stale leaves */
595
596 leaf = bp->data;
597 mp = dp->i_mount;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200598 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /*
600 * This value is not restrictive enough.
601 * Should factor in the size of the bests table as well.
602 * We can deduce a value for that from di_size.
603 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000604 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
605 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /*
607 * Leaves and bests don't overlap.
608 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100609 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000610 (char *)xfs_dir2_leaf_bests_p(ltp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /*
612 * Check hash value order, count stale entries.
613 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100614 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
615 if (i + 1 < be16_to_cpu(leaf->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100616 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
617 be32_to_cpu(leaf->ents[i + 1].hashval));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200618 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 stale++;
620 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100621 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623#endif /* DEBUG */
624
625/*
626 * Compact out any stale entries in the leaf.
627 * Log the header and changed leaf entries, if any.
628 */
629void
630xfs_dir2_leaf_compact(
631 xfs_da_args_t *args, /* operation arguments */
632 xfs_dabuf_t *bp) /* leaf buffer */
633{
634 int from; /* source leaf index */
635 xfs_dir2_leaf_t *leaf; /* leaf structure */
636 int loglow; /* first leaf entry to log */
637 int to; /* target leaf index */
638
639 leaf = bp->data;
640 if (!leaf->hdr.stale) {
641 return;
642 }
643 /*
644 * Compress out the stale entries in place.
645 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100646 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200647 if (leaf->ents[from].address ==
648 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 continue;
650 /*
651 * Only actually copy the entries that are different.
652 */
653 if (from > to) {
654 if (loglow == -1)
655 loglow = to;
656 leaf->ents[to] = leaf->ents[from];
657 }
658 to++;
659 }
660 /*
661 * Update and log the header, log the leaf entries.
662 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100663 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800664 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 leaf->hdr.stale = 0;
666 xfs_dir2_leaf_log_header(args->trans, bp);
667 if (loglow != -1)
668 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
669}
670
671/*
672 * Compact the leaf entries, removing stale ones.
673 * Leave one stale entry behind - the one closest to our
674 * insertion index - and the caller will shift that one to our insertion
675 * point later.
676 * Return new insertion index, where the remaining stale entry is,
677 * and leaf logging indices.
678 */
679void
680xfs_dir2_leaf_compact_x1(
681 xfs_dabuf_t *bp, /* leaf buffer */
682 int *indexp, /* insertion index */
683 int *lowstalep, /* out: stale entry before us */
684 int *highstalep, /* out: stale entry after us */
685 int *lowlogp, /* out: low log index */
686 int *highlogp) /* out: high log index */
687{
688 int from; /* source copy index */
689 int highstale; /* stale entry at/after index */
690 int index; /* insertion index */
691 int keepstale; /* source index of kept stale */
692 xfs_dir2_leaf_t *leaf; /* leaf structure */
693 int lowstale; /* stale entry before index */
694 int newindex=0; /* new insertion index */
695 int to; /* destination copy index */
696
697 leaf = bp->data;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100698 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 index = *indexp;
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200700
701 xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /*
704 * Pick the better of lowstale and highstale.
705 */
706 if (lowstale >= 0 &&
Nathan Scotta818e5d2006-03-17 17:28:07 +1100707 (highstale == be16_to_cpu(leaf->hdr.count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 index - lowstale <= highstale - index))
709 keepstale = lowstale;
710 else
711 keepstale = highstale;
712 /*
713 * Copy the entries in place, removing all the stale entries
714 * except keepstale.
715 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100716 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /*
718 * Notice the new value of index.
719 */
720 if (index == from)
721 newindex = to;
722 if (from != keepstale &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200723 leaf->ents[from].address ==
724 cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (from == to)
726 *lowlogp = to;
727 continue;
728 }
729 /*
730 * Record the new keepstale value for the insertion.
731 */
732 if (from == keepstale)
733 lowstale = highstale = to;
734 /*
735 * Copy only the entries that have moved.
736 */
737 if (from > to)
738 leaf->ents[to] = leaf->ents[from];
739 to++;
740 }
741 ASSERT(from > to);
742 /*
743 * If the insertion point was past the last entry,
744 * set the new insertion point accordingly.
745 */
746 if (index == from)
747 newindex = to;
748 *indexp = newindex;
749 /*
750 * Adjust the leaf header values.
751 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800752 be16_add_cpu(&leaf->hdr.count, -(from - to));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100753 leaf->hdr.stale = cpu_to_be16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /*
755 * Remember the low/high stale value only in the "right"
756 * direction.
757 */
758 if (lowstale >= newindex)
759 lowstale = -1;
760 else
Nathan Scotta818e5d2006-03-17 17:28:07 +1100761 highstale = be16_to_cpu(leaf->hdr.count);
762 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 *lowstalep = lowstale;
764 *highstalep = highstale;
765}
766
767/*
768 * Getdents (readdir) for leaf and node directories.
769 * This reads the data blocks only, so is the same for both forms.
770 */
771int /* error */
772xfs_dir2_leaf_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 xfs_inode_t *dp, /* incore directory inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000774 void *dirent,
775 size_t bufsize,
776 xfs_off_t *offset,
777 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 xfs_dabuf_t *bp; /* data block buffer */
780 int byteoff; /* offset in current block */
781 xfs_dir2_db_t curdb; /* db for current block */
782 xfs_dir2_off_t curoff; /* current overall offset */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200783 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 xfs_dir2_data_entry_t *dep; /* data entry */
785 xfs_dir2_data_unused_t *dup; /* unused entry */
Nathan Scottf6d75cb2006-03-14 13:32:24 +1100786 int error = 0; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 int i; /* temporary loop index */
788 int j; /* temporary loop index */
789 int length; /* temporary length value */
790 xfs_bmbt_irec_t *map; /* map vector for blocks */
791 xfs_extlen_t map_blocks; /* number of fsbs in map */
792 xfs_dablk_t map_off; /* last mapped file offset */
793 int map_size; /* total entries in *map */
794 int map_valid; /* valid entries in *map */
795 xfs_mount_t *mp; /* filesystem mount point */
796 xfs_dir2_off_t newoff; /* new curoff after new blk */
797 int nmap; /* mappings to ask xfs_bmapi */
Nathan Scottf6d75cb2006-03-14 13:32:24 +1100798 char *ptr = NULL; /* pointer to current data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 int ra_current; /* number of read-ahead blks */
800 int ra_index; /* *map index for read-ahead */
801 int ra_offset; /* map entry offset for ra */
802 int ra_want; /* readahead count wanted */
803
804 /*
805 * If the offset is at or past the largest allowed value,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000806 * give up right away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000808 if (*offset >= XFS_DIR2_MAX_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return 0;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 mp = dp->i_mount;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /*
814 * Set up to bmap a number of blocks based on the caller's
815 * buffer size, the directory block size, and the filesystem
816 * block size.
817 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000818 map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
820 map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
821 bp = NULL;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /*
824 * Inside the loop we keep the main offset value as a byte offset
825 * in the directory file.
826 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000827 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /*
830 * Force this conversion through db so we truncate the offset
831 * down to get the start of the data block.
832 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000833 map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /*
835 * Loop over directory entries until we reach the end offset.
836 * Get more blocks and readahead as necessary.
837 */
838 while (curoff < XFS_DIR2_LEAF_OFFSET) {
839 /*
840 * If we have no buffer, or we're off the end of the
841 * current buffer, need to get another one.
842 */
843 if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) {
844 /*
845 * If we have a buffer, we need to release it and
846 * take it out of the mapping.
847 */
848 if (bp) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000849 xfs_da_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 bp = NULL;
851 map_blocks -= mp->m_dirblkfsbs;
852 /*
853 * Loop to get rid of the extents for the
854 * directory block.
855 */
856 for (i = mp->m_dirblkfsbs; i > 0; ) {
857 j = MIN((int)map->br_blockcount, i);
858 map->br_blockcount -= j;
859 map->br_startblock += j;
860 map->br_startoff += j;
861 /*
862 * If mapping is done, pitch it from
863 * the table.
864 */
865 if (!map->br_blockcount && --map_valid)
866 memmove(&map[0], &map[1],
867 sizeof(map[0]) *
868 map_valid);
869 i -= j;
870 }
871 }
872 /*
873 * Recalculate the readahead blocks wanted.
874 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000875 ra_want = howmany(bufsize + mp->m_dirblksize,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 mp->m_sb.sb_blocksize) - 1;
Eric Sandeen8e69ce12009-09-25 19:42:26 +0000877 ASSERT(ra_want >= 0);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /*
880 * If we don't have as many as we want, and we haven't
881 * run out of data blocks, get some more mappings.
882 */
883 if (1 + ra_want > map_blocks &&
884 map_off <
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000885 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /*
887 * Get more bmaps, fill in after the ones
888 * we already have in the table.
889 */
890 nmap = map_size - map_valid;
Dave Chinner5c8ed202011-09-18 20:40:45 +0000891 error = xfs_bmapi_read(dp, map_off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000892 xfs_dir2_byte_to_da(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 XFS_DIR2_LEAF_OFFSET) - map_off,
Dave Chinner5c8ed202011-09-18 20:40:45 +0000894 &map[map_valid], &nmap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /*
896 * Don't know if we should ignore this or
897 * try to return an error.
898 * The trouble with returning errors
899 * is that readdir will just stop without
900 * actually passing the error through.
901 */
902 if (error)
903 break; /* XXX */
904 /*
905 * If we got all the mappings we asked for,
906 * set the final map offset based on the
907 * last bmap value received.
908 * Otherwise, we've reached the end.
909 */
910 if (nmap == map_size - map_valid)
911 map_off =
912 map[map_valid + nmap - 1].br_startoff +
913 map[map_valid + nmap - 1].br_blockcount;
914 else
915 map_off =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000916 xfs_dir2_byte_to_da(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 XFS_DIR2_LEAF_OFFSET);
918 /*
919 * Look for holes in the mapping, and
920 * eliminate them. Count up the valid blocks.
921 */
922 for (i = map_valid; i < map_valid + nmap; ) {
923 if (map[i].br_startblock ==
924 HOLESTARTBLOCK) {
925 nmap--;
926 length = map_valid + nmap - i;
927 if (length)
928 memmove(&map[i],
929 &map[i + 1],
930 sizeof(map[i]) *
931 length);
932 } else {
933 map_blocks +=
934 map[i].br_blockcount;
935 i++;
936 }
937 }
938 map_valid += nmap;
939 }
940 /*
941 * No valid mappings, so no more data blocks.
942 */
943 if (!map_valid) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000944 curoff = xfs_dir2_da_to_byte(mp, map_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 break;
946 }
947 /*
948 * Read the directory block starting at the first
949 * mapping.
950 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000951 curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000952 error = xfs_da_read_buf(NULL, dp, map->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 map->br_blockcount >= mp->m_dirblkfsbs ?
954 XFS_FSB_TO_DADDR(mp, map->br_startblock) :
955 -1,
956 &bp, XFS_DATA_FORK);
957 /*
958 * Should just skip over the data block instead
959 * of giving up.
960 */
961 if (error)
962 break; /* XXX */
963 /*
964 * Adjust the current amount of read-ahead: we just
965 * read a block that was previously ra.
966 */
967 if (ra_current)
968 ra_current -= mp->m_dirblkfsbs;
969 /*
970 * Do we need more readahead?
971 */
972 for (ra_index = ra_offset = i = 0;
973 ra_want > ra_current && i < map_blocks;
974 i += mp->m_dirblkfsbs) {
975 ASSERT(ra_index < map_valid);
976 /*
977 * Read-ahead a contiguous directory block.
978 */
979 if (i > ra_current &&
980 map[ra_index].br_blockcount >=
981 mp->m_dirblkfsbs) {
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000982 xfs_buf_readahead(mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 XFS_FSB_TO_DADDR(mp,
984 map[ra_index].br_startblock +
985 ra_offset),
986 (int)BTOBB(mp->m_dirblksize));
987 ra_current = i;
988 }
989 /*
990 * Read-ahead a non-contiguous directory block.
991 * This doesn't use our mapping, but this
992 * is a very rare case.
993 */
994 else if (i > ra_current) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000995 (void)xfs_da_reada_buf(NULL, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 map[ra_index].br_startoff +
997 ra_offset, XFS_DATA_FORK);
998 ra_current = i;
999 }
1000 /*
1001 * Advance offset through the mapping table.
1002 */
1003 for (j = 0; j < mp->m_dirblkfsbs; j++) {
1004 /*
1005 * The rest of this extent but not
1006 * more than a dir block.
1007 */
1008 length = MIN(mp->m_dirblkfsbs,
1009 (int)(map[ra_index].br_blockcount -
1010 ra_offset));
1011 j += length;
1012 ra_offset += length;
1013 /*
1014 * Advance to the next mapping if
1015 * this one is used up.
1016 */
1017 if (ra_offset ==
1018 map[ra_index].br_blockcount) {
1019 ra_offset = 0;
1020 ra_index++;
1021 }
1022 }
1023 }
1024 /*
1025 * Having done a read, we need to set a new offset.
1026 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001027 newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /*
1029 * Start of the current block.
1030 */
1031 if (curoff < newoff)
1032 curoff = newoff;
1033 /*
1034 * Make sure we're in the right block.
1035 */
1036 else if (curoff > newoff)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001037 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 curdb);
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +02001039 hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 xfs_dir2_data_check(dp, bp);
1041 /*
1042 * Find our position in the block.
1043 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +02001044 ptr = (char *)(hdr + 1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001045 byteoff = xfs_dir2_byte_to_off(mp, curoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 /*
1047 * Skip past the header.
1048 */
1049 if (byteoff == 0)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001050 curoff += (uint)sizeof(*hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /*
1052 * Skip past entries until we reach our offset.
1053 */
1054 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001055 while ((char *)ptr - (char *)hdr < byteoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 dup = (xfs_dir2_data_unused_t *)ptr;
1057
Nathan Scottad354eb2006-03-17 17:27:37 +11001058 if (be16_to_cpu(dup->freetag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 == XFS_DIR2_DATA_FREE_TAG) {
1060
Nathan Scottad354eb2006-03-17 17:27:37 +11001061 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 ptr += length;
1063 continue;
1064 }
1065 dep = (xfs_dir2_data_entry_t *)ptr;
1066 length =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001067 xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 ptr += length;
1069 }
1070 /*
1071 * Now set our real offset.
1072 */
1073 curoff =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001074 xfs_dir2_db_off_to_byte(mp,
1075 xfs_dir2_byte_to_db(mp, curoff),
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001076 (char *)ptr - (char *)hdr);
1077 if (ptr >= (char *)hdr + mp->m_dirblksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 continue;
1079 }
1080 }
1081 }
1082 /*
1083 * We have a pointer to an entry.
1084 * Is it a live one?
1085 */
1086 dup = (xfs_dir2_data_unused_t *)ptr;
1087 /*
1088 * No, it's unused, skip over it.
1089 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001090 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1091 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 ptr += length;
1093 curoff += length;
1094 continue;
1095 }
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 dep = (xfs_dir2_data_entry_t *)ptr;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001098 length = xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Dave Chinner4a24cb72010-01-20 10:48:05 +11001100 if (filldir(dirent, (char *)dep->name, dep->namelen,
Christoph Hellwig15440312009-01-08 14:00:00 -05001101 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
Christoph Hellwiga19d9f82009-03-29 09:51:08 +02001102 be64_to_cpu(dep->inumber), DT_UNKNOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 break;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 /*
1106 * Advance to next entry in the block.
1107 */
1108 ptr += length;
1109 curoff += length;
Eric Sandeen8e69ce12009-09-25 19:42:26 +00001110 /* bufsize may have just been a guess; don't go negative */
1111 bufsize = bufsize > length ? bufsize - length : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113
1114 /*
1115 * All done. Set output offset value to current offset.
1116 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001117 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
Christoph Hellwig15440312009-01-08 14:00:00 -05001118 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 else
Christoph Hellwig15440312009-01-08 14:00:00 -05001120 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001121 kmem_free(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (bp)
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001123 xfs_da_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return error;
1125}
1126
1127/*
1128 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1129 */
1130int
1131xfs_dir2_leaf_init(
1132 xfs_da_args_t *args, /* operation arguments */
1133 xfs_dir2_db_t bno, /* directory block number */
1134 xfs_dabuf_t **bpp, /* out: leaf buffer */
1135 int magic) /* magic number for block */
1136{
1137 xfs_dabuf_t *bp; /* leaf buffer */
1138 xfs_inode_t *dp; /* incore directory inode */
1139 int error; /* error return code */
1140 xfs_dir2_leaf_t *leaf; /* leaf structure */
1141 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1142 xfs_mount_t *mp; /* filesystem mount point */
1143 xfs_trans_t *tp; /* transaction pointer */
1144
1145 dp = args->dp;
1146 ASSERT(dp != NULL);
1147 tp = args->trans;
1148 mp = dp->i_mount;
1149 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1150 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1151 /*
1152 * Get the buffer for the block.
1153 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001154 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 XFS_DATA_FORK);
1156 if (error) {
1157 return error;
1158 }
1159 ASSERT(bp != NULL);
1160 leaf = bp->data;
1161 /*
1162 * Initialize the header.
1163 */
Nathan Scott89da0542006-03-17 17:28:40 +11001164 leaf->hdr.info.magic = cpu_to_be16(magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 leaf->hdr.info.forw = 0;
1166 leaf->hdr.info.back = 0;
1167 leaf->hdr.count = 0;
1168 leaf->hdr.stale = 0;
1169 xfs_dir2_leaf_log_header(tp, bp);
1170 /*
1171 * If it's a leaf-format directory initialize the tail.
1172 * In this case our caller has the real bests table to copy into
1173 * the block.
1174 */
1175 if (magic == XFS_DIR2_LEAF1_MAGIC) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001176 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 ltp->bestcount = 0;
1178 xfs_dir2_leaf_log_tail(tp, bp);
1179 }
1180 *bpp = bp;
1181 return 0;
1182}
1183
1184/*
1185 * Log the bests entries indicated from a leaf1 block.
1186 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001187static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188xfs_dir2_leaf_log_bests(
1189 xfs_trans_t *tp, /* transaction pointer */
1190 xfs_dabuf_t *bp, /* leaf buffer */
1191 int first, /* first entry to log */
1192 int last) /* last entry to log */
1193{
Nathan Scott68b3a102006-03-17 17:27:19 +11001194 __be16 *firstb; /* pointer to first entry */
1195 __be16 *lastb; /* pointer to last entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 xfs_dir2_leaf_t *leaf; /* leaf structure */
1197 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1198
1199 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001200 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001201 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1202 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1203 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1205 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1206}
1207
1208/*
1209 * Log the leaf entries indicated from a leaf1 or leafn block.
1210 */
1211void
1212xfs_dir2_leaf_log_ents(
1213 xfs_trans_t *tp, /* transaction pointer */
1214 xfs_dabuf_t *bp, /* leaf buffer */
1215 int first, /* first entry to log */
1216 int last) /* last entry to log */
1217{
1218 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1219 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1220 xfs_dir2_leaf_t *leaf; /* leaf structure */
1221
1222 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001223 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1224 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 firstlep = &leaf->ents[first];
1226 lastlep = &leaf->ents[last];
1227 xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1228 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1229}
1230
1231/*
1232 * Log the header of the leaf1 or leafn block.
1233 */
1234void
1235xfs_dir2_leaf_log_header(
1236 xfs_trans_t *tp, /* transaction pointer */
1237 xfs_dabuf_t *bp) /* leaf buffer */
1238{
1239 xfs_dir2_leaf_t *leaf; /* leaf structure */
1240
1241 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001242 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1243 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1245 (uint)(sizeof(leaf->hdr) - 1));
1246}
1247
1248/*
1249 * Log the tail of the leaf1 block.
1250 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001251STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252xfs_dir2_leaf_log_tail(
1253 xfs_trans_t *tp, /* transaction pointer */
1254 xfs_dabuf_t *bp) /* leaf buffer */
1255{
1256 xfs_dir2_leaf_t *leaf; /* leaf structure */
1257 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1258 xfs_mount_t *mp; /* filesystem mount point */
1259
1260 mp = tp->t_mountp;
1261 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001262 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001263 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1265 (uint)(mp->m_dirblksize - 1));
1266}
1267
1268/*
1269 * Look up the entry referred to by args in the leaf format directory.
1270 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1271 * is also used by the node-format code.
1272 */
1273int
1274xfs_dir2_leaf_lookup(
1275 xfs_da_args_t *args) /* operation arguments */
1276{
1277 xfs_dabuf_t *dbp; /* data block buffer */
1278 xfs_dir2_data_entry_t *dep; /* data block entry */
1279 xfs_inode_t *dp; /* incore directory inode */
1280 int error; /* error return code */
1281 int index; /* found entry index */
1282 xfs_dabuf_t *lbp; /* leaf buffer */
1283 xfs_dir2_leaf_t *leaf; /* leaf structure */
1284 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1285 xfs_trans_t *tp; /* transaction pointer */
1286
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001287 trace_xfs_dir2_leaf_lookup(args);
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 /*
1290 * Look up name in the leaf block, returning both buffers and index.
1291 */
1292 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1293 return error;
1294 }
1295 tp = args->trans;
1296 dp = args->dp;
1297 xfs_dir2_leaf_check(dp, lbp);
1298 leaf = lbp->data;
1299 /*
1300 * Get to the leaf entry and contained data entry address.
1301 */
1302 lep = &leaf->ents[index];
1303 /*
1304 * Point to the data entry.
1305 */
1306 dep = (xfs_dir2_data_entry_t *)
1307 ((char *)dbp->data +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001308 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +10001310 * Return the found inode number & CI name if appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001312 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001313 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 xfs_da_brelse(tp, dbp);
1315 xfs_da_brelse(tp, lbp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001316 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
1319/*
1320 * Look up name/hash in the leaf block.
1321 * Fill in indexp with the found index, and dbpp with the data buffer.
1322 * If not found dbpp will be NULL, and ENOENT comes back.
1323 * lbpp will always be filled in with the leaf buffer unless there's an error.
1324 */
1325static int /* error */
1326xfs_dir2_leaf_lookup_int(
1327 xfs_da_args_t *args, /* operation arguments */
1328 xfs_dabuf_t **lbpp, /* out: leaf buffer */
1329 int *indexp, /* out: index in leaf block */
1330 xfs_dabuf_t **dbpp) /* out: data buffer */
1331{
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001332 xfs_dir2_db_t curdb = -1; /* current data block number */
1333 xfs_dabuf_t *dbp = NULL; /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 xfs_dir2_data_entry_t *dep; /* data entry */
1335 xfs_inode_t *dp; /* incore directory inode */
1336 int error; /* error return code */
1337 int index; /* index in leaf block */
1338 xfs_dabuf_t *lbp; /* leaf buffer */
1339 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1340 xfs_dir2_leaf_t *leaf; /* leaf structure */
1341 xfs_mount_t *mp; /* filesystem mount point */
1342 xfs_dir2_db_t newdb; /* new data block number */
1343 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001344 xfs_dir2_db_t cidb = -1; /* case match data block no. */
Barry Naujok5163f952008-05-21 16:41:01 +10001345 enum xfs_dacmp cmp; /* name compare result */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 dp = args->dp;
1348 tp = args->trans;
1349 mp = dp->i_mount;
1350 /*
1351 * Read the leaf block into the buffer.
1352 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001353 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1354 XFS_DATA_FORK);
1355 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 *lbpp = lbp;
1358 leaf = lbp->data;
1359 xfs_dir2_leaf_check(dp, lbp);
1360 /*
1361 * Look for the first leaf entry with our hash value.
1362 */
1363 index = xfs_dir2_leaf_search_hash(args, lbp);
1364 /*
1365 * Loop over all the entries with the right hash value
1366 * looking to match the name.
1367 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001368 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
Barry Naujok5163f952008-05-21 16:41:01 +10001369 be32_to_cpu(lep->hashval) == args->hashval;
1370 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /*
1372 * Skip over stale leaf entries.
1373 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001374 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 continue;
1376 /*
1377 * Get the new data block number.
1378 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001379 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /*
1381 * If it's not the same as the old data block number,
1382 * need to pitch the old one and read the new one.
1383 */
1384 if (newdb != curdb) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001385 if (dbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 xfs_da_brelse(tp, dbp);
Barry Naujok5163f952008-05-21 16:41:01 +10001387 error = xfs_da_read_buf(tp, dp,
1388 xfs_dir2_db_to_da(mp, newdb),
1389 -1, &dbp, XFS_DATA_FORK);
1390 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 xfs_da_brelse(tp, lbp);
1392 return error;
1393 }
1394 xfs_dir2_data_check(dp, dbp);
1395 curdb = newdb;
1396 }
1397 /*
1398 * Point to the data entry.
1399 */
Barry Naujok5163f952008-05-21 16:41:01 +10001400 dep = (xfs_dir2_data_entry_t *)((char *)dbp->data +
1401 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /*
Barry Naujok5163f952008-05-21 16:41:01 +10001403 * Compare name and if it's an exact match, return the index
1404 * and buffer. If it's the first case-insensitive match, store
1405 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 */
Barry Naujok5163f952008-05-21 16:41:01 +10001407 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1408 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1409 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 *indexp = index;
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001411 /* case exact match: return the current buffer. */
Barry Naujok5163f952008-05-21 16:41:01 +10001412 if (cmp == XFS_CMP_EXACT) {
Barry Naujok5163f952008-05-21 16:41:01 +10001413 *dbpp = dbp;
1414 return 0;
1415 }
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001416 cidb = curdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418 }
Barry Naujok6a178102008-05-21 16:42:05 +10001419 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +10001420 /*
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001421 * Here, we can only be doing a lookup (not a rename or remove).
1422 * If a case-insensitive match was found earlier, re-read the
1423 * appropriate data block if required and return it.
Barry Naujok5163f952008-05-21 16:41:01 +10001424 */
1425 if (args->cmpresult == XFS_CMP_CASE) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001426 ASSERT(cidb != -1);
1427 if (cidb != curdb) {
Barry Naujok5163f952008-05-21 16:41:01 +10001428 xfs_da_brelse(tp, dbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001429 error = xfs_da_read_buf(tp, dp,
1430 xfs_dir2_db_to_da(mp, cidb),
1431 -1, &dbp, XFS_DATA_FORK);
1432 if (error) {
1433 xfs_da_brelse(tp, lbp);
1434 return error;
1435 }
1436 }
1437 *dbpp = dbp;
Barry Naujok5163f952008-05-21 16:41:01 +10001438 return 0;
1439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 /*
1441 * No match found, return ENOENT.
1442 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001443 ASSERT(cidb == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (dbp)
1445 xfs_da_brelse(tp, dbp);
1446 xfs_da_brelse(tp, lbp);
1447 return XFS_ERROR(ENOENT);
1448}
1449
1450/*
1451 * Remove an entry from a leaf format directory.
1452 */
1453int /* error */
1454xfs_dir2_leaf_removename(
1455 xfs_da_args_t *args) /* operation arguments */
1456{
Nathan Scott68b3a102006-03-17 17:27:19 +11001457 __be16 *bestsp; /* leaf block best freespace */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001458 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 xfs_dir2_db_t db; /* data block number */
1460 xfs_dabuf_t *dbp; /* data block buffer */
1461 xfs_dir2_data_entry_t *dep; /* data entry structure */
1462 xfs_inode_t *dp; /* incore directory inode */
1463 int error; /* error return code */
1464 xfs_dir2_db_t i; /* temporary data block # */
1465 int index; /* index into leaf entries */
1466 xfs_dabuf_t *lbp; /* leaf buffer */
1467 xfs_dir2_leaf_t *leaf; /* leaf structure */
1468 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1469 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1470 xfs_mount_t *mp; /* filesystem mount point */
1471 int needlog; /* need to log data header */
1472 int needscan; /* need to rescan data frees */
1473 xfs_dir2_data_off_t oldbest; /* old value of best free */
1474 xfs_trans_t *tp; /* transaction pointer */
1475
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001476 trace_xfs_dir2_leaf_removename(args);
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /*
1479 * Lookup the leaf entry, get the leaf and data blocks read in.
1480 */
1481 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1482 return error;
1483 }
1484 dp = args->dp;
1485 tp = args->trans;
1486 mp = dp->i_mount;
1487 leaf = lbp->data;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001488 hdr = dbp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 xfs_dir2_data_check(dp, dbp);
1490 /*
1491 * Point to the leaf entry, use that to point to the data entry.
1492 */
1493 lep = &leaf->ents[index];
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001494 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001496 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 needscan = needlog = 0;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001498 oldbest = be16_to_cpu(hdr->bestfree[0].length);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001499 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1500 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott68b3a102006-03-17 17:27:19 +11001501 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 /*
1503 * Mark the former data entry unused.
1504 */
1505 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001506 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001507 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /*
1509 * We just mark the leaf entry stale by putting a null in it.
1510 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001511 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001513 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1515 /*
1516 * Scan the freespace in the data block again if necessary,
1517 * log the data block header if necessary.
1518 */
1519 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001520 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (needlog)
1522 xfs_dir2_data_log_header(tp, dbp);
1523 /*
1524 * If the longest freespace in the data block has changed,
1525 * put the new value in the bests table and log that.
1526 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001527 if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1528 bestsp[db] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1530 }
1531 xfs_dir2_data_check(dp, dbp);
1532 /*
1533 * If the data block is now empty then get rid of the data block.
1534 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001535 if (be16_to_cpu(hdr->bestfree[0].length) ==
1536 mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 ASSERT(db != mp->m_dirdatablk);
1538 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1539 /*
1540 * Nope, can't get rid of it because it caused
1541 * allocation of a bmap btree block to do so.
1542 * Just go on, returning success, leaving the
1543 * empty block in place.
1544 */
1545 if (error == ENOSPC && args->total == 0) {
1546 xfs_da_buf_done(dbp);
1547 error = 0;
1548 }
1549 xfs_dir2_leaf_check(dp, lbp);
1550 xfs_da_buf_done(lbp);
1551 return error;
1552 }
1553 dbp = NULL;
1554 /*
1555 * If this is the last data block then compact the
1556 * bests table by getting rid of entries.
1557 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001558 if (db == be32_to_cpu(ltp->bestcount) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /*
1560 * Look for the last active entry (i).
1561 */
1562 for (i = db - 1; i > 0; i--) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001563 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 break;
1565 }
1566 /*
1567 * Copy the table down so inactive entries at the
1568 * end are removed.
1569 */
1570 memmove(&bestsp[db - i], bestsp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001571 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001572 be32_add_cpu(&ltp->bestcount, -(db - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001574 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 } else
Nathan Scott68b3a102006-03-17 17:27:19 +11001576 bestsp[db] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
1578 /*
1579 * If the data block was not the first one, drop it.
1580 */
1581 else if (db != mp->m_dirdatablk && dbp != NULL) {
1582 xfs_da_buf_done(dbp);
1583 dbp = NULL;
1584 }
1585 xfs_dir2_leaf_check(dp, lbp);
1586 /*
1587 * See if we can convert to block form.
1588 */
1589 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1590}
1591
1592/*
1593 * Replace the inode number in a leaf format directory entry.
1594 */
1595int /* error */
1596xfs_dir2_leaf_replace(
1597 xfs_da_args_t *args) /* operation arguments */
1598{
1599 xfs_dabuf_t *dbp; /* data block buffer */
1600 xfs_dir2_data_entry_t *dep; /* data block entry */
1601 xfs_inode_t *dp; /* incore directory inode */
1602 int error; /* error return code */
1603 int index; /* index of leaf entry */
1604 xfs_dabuf_t *lbp; /* leaf buffer */
1605 xfs_dir2_leaf_t *leaf; /* leaf structure */
1606 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1607 xfs_trans_t *tp; /* transaction pointer */
1608
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001609 trace_xfs_dir2_leaf_replace(args);
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 /*
1612 * Look up the entry.
1613 */
1614 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1615 return error;
1616 }
1617 dp = args->dp;
1618 leaf = lbp->data;
1619 /*
1620 * Point to the leaf entry, get data address from it.
1621 */
1622 lep = &leaf->ents[index];
1623 /*
1624 * Point to the data entry.
1625 */
1626 dep = (xfs_dir2_data_entry_t *)
1627 ((char *)dbp->data +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001628 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001629 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 /*
1631 * Put the new inode number in, log it.
1632 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001633 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 tp = args->trans;
1635 xfs_dir2_data_log_entry(tp, dbp, dep);
1636 xfs_da_buf_done(dbp);
1637 xfs_dir2_leaf_check(dp, lbp);
1638 xfs_da_brelse(tp, lbp);
1639 return 0;
1640}
1641
1642/*
1643 * Return index in the leaf block (lbp) which is either the first
1644 * one with this hash value, or if there are none, the insert point
1645 * for that hash value.
1646 */
1647int /* index value */
1648xfs_dir2_leaf_search_hash(
1649 xfs_da_args_t *args, /* operation arguments */
1650 xfs_dabuf_t *lbp) /* leaf buffer */
1651{
1652 xfs_dahash_t hash=0; /* hash from this entry */
1653 xfs_dahash_t hashwant; /* hash value looking for */
1654 int high; /* high leaf index */
1655 int low; /* low leaf index */
1656 xfs_dir2_leaf_t *leaf; /* leaf structure */
1657 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1658 int mid=0; /* current leaf index */
1659
1660 leaf = lbp->data;
1661#ifndef __KERNEL__
1662 if (!leaf->hdr.count)
1663 return 0;
1664#endif
1665 /*
1666 * Note, the table cannot be empty, so we have to go through the loop.
1667 * Binary search the leaf entries looking for our hash value.
1668 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001669 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 hashwant = args->hashval;
1671 low <= high; ) {
1672 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001673 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 break;
1675 if (hash < hashwant)
1676 low = mid + 1;
1677 else
1678 high = mid - 1;
1679 }
1680 /*
1681 * Found one, back up through all the equal hash values.
1682 */
1683 if (hash == hashwant) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001684 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 mid--;
1686 }
1687 }
1688 /*
1689 * Need to point to an entry higher than ours.
1690 */
1691 else if (hash < hashwant)
1692 mid++;
1693 return mid;
1694}
1695
1696/*
1697 * Trim off a trailing data block. We know it's empty since the leaf
1698 * freespace table says so.
1699 */
1700int /* error */
1701xfs_dir2_leaf_trim_data(
1702 xfs_da_args_t *args, /* operation arguments */
1703 xfs_dabuf_t *lbp, /* leaf buffer */
1704 xfs_dir2_db_t db) /* data block number */
1705{
Nathan Scott68b3a102006-03-17 17:27:19 +11001706 __be16 *bestsp; /* leaf bests table */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 xfs_dabuf_t *dbp; /* data block buffer */
1708 xfs_inode_t *dp; /* incore directory inode */
1709 int error; /* error return value */
1710 xfs_dir2_leaf_t *leaf; /* leaf structure */
1711 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1712 xfs_mount_t *mp; /* filesystem mount point */
1713 xfs_trans_t *tp; /* transaction pointer */
1714
1715 dp = args->dp;
1716 mp = dp->i_mount;
1717 tp = args->trans;
1718 /*
1719 * Read the offending data block. We need its buffer.
1720 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001721 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 XFS_DATA_FORK))) {
1723 return error;
1724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 leaf = lbp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001727 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001728
1729#ifdef DEBUG
1730{
1731 struct xfs_dir2_data_hdr *hdr = dbp->data;
1732
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001733 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001734 ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
1735 mp->m_dirblksize - (uint)sizeof(*hdr));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001736 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001737}
1738#endif
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /*
1741 * Get rid of the data block.
1742 */
1743 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1744 ASSERT(error != ENOSPC);
1745 xfs_da_brelse(tp, dbp);
1746 return error;
1747 }
1748 /*
1749 * Eliminate the last bests entry from the table.
1750 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001751 bestsp = xfs_dir2_leaf_bests_p(ltp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001752 be32_add_cpu(&ltp->bestcount, -1);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001753 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001755 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return 0;
1757}
1758
Christoph Hellwig22823962011-07-08 14:35:53 +02001759static inline size_t
1760xfs_dir2_leaf_size(
1761 struct xfs_dir2_leaf_hdr *hdr,
1762 int counts)
1763{
1764 int entries;
1765
1766 entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
1767 return sizeof(xfs_dir2_leaf_hdr_t) +
1768 entries * sizeof(xfs_dir2_leaf_entry_t) +
1769 counts * sizeof(xfs_dir2_data_off_t) +
1770 sizeof(xfs_dir2_leaf_tail_t);
1771}
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773/*
1774 * Convert node form directory to leaf form directory.
1775 * The root of the node form dir needs to already be a LEAFN block.
1776 * Just return if we can't do anything.
1777 */
1778int /* error */
1779xfs_dir2_node_to_leaf(
1780 xfs_da_state_t *state) /* directory operation state */
1781{
1782 xfs_da_args_t *args; /* operation arguments */
1783 xfs_inode_t *dp; /* incore directory inode */
1784 int error; /* error return code */
1785 xfs_dabuf_t *fbp; /* buffer for freespace block */
1786 xfs_fileoff_t fo; /* freespace file offset */
1787 xfs_dir2_free_t *free; /* freespace structure */
1788 xfs_dabuf_t *lbp; /* buffer for leaf block */
1789 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1790 xfs_dir2_leaf_t *leaf; /* leaf structure */
1791 xfs_mount_t *mp; /* filesystem mount point */
1792 int rval; /* successful free trim? */
1793 xfs_trans_t *tp; /* transaction pointer */
1794
1795 /*
1796 * There's more than a leaf level in the btree, so there must
1797 * be multiple leafn blocks. Give up.
1798 */
1799 if (state->path.active > 1)
1800 return 0;
1801 args = state->args;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001802
1803 trace_xfs_dir2_node_to_leaf(args);
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 mp = state->mp;
1806 dp = args->dp;
1807 tp = args->trans;
1808 /*
1809 * Get the last offset in the file.
1810 */
1811 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1812 return error;
1813 }
1814 fo -= mp->m_dirblkfsbs;
1815 /*
1816 * If there are freespace blocks other than the first one,
1817 * take this opportunity to remove trailing empty freespace blocks
1818 * that may have been left behind during no-space-reservation
1819 * operations.
1820 */
1821 while (fo > mp->m_dirfreeblk) {
1822 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1823 return error;
1824 }
1825 if (rval)
1826 fo -= mp->m_dirblkfsbs;
1827 else
1828 return 0;
1829 }
1830 /*
1831 * Now find the block just before the freespace block.
1832 */
1833 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1834 return error;
1835 }
1836 /*
1837 * If it's not the single leaf block, give up.
1838 */
1839 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1840 return 0;
1841 lbp = state->path.blk[0].bp;
1842 leaf = lbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001843 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 /*
1845 * Read the freespace block.
1846 */
1847 if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
1848 XFS_DATA_FORK))) {
1849 return error;
1850 }
1851 free = fbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001852 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 ASSERT(!free->hdr.firstdb);
Christoph Hellwig22823962011-07-08 14:35:53 +02001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 /*
1856 * Now see if the leafn and free data will fit in a leaf1.
1857 * If not, release the buffer and give up.
1858 */
Christoph Hellwig22823962011-07-08 14:35:53 +02001859 if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
1860 mp->m_dirblksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 xfs_da_brelse(tp, fbp);
1862 return 0;
1863 }
Christoph Hellwig22823962011-07-08 14:35:53 +02001864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 /*
1866 * If the leaf has any stale entries in it, compress them out.
1867 * The compact routine will log the header.
1868 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001869 if (be16_to_cpu(leaf->hdr.stale))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 xfs_dir2_leaf_compact(args, lbp);
1871 else
1872 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott89da0542006-03-17 17:28:40 +11001873 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 /*
1875 * Set up the leaf tail from the freespace block.
1876 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001877 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scott0ba962e2006-03-17 17:27:07 +11001878 ltp->bestcount = free->hdr.nvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 /*
1880 * Set up the leaf bests table.
1881 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001882 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
Christoph Hellwig22823962011-07-08 14:35:53 +02001883 be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001884 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 xfs_dir2_leaf_log_tail(tp, lbp);
1886 xfs_dir2_leaf_check(dp, lbp);
1887 /*
1888 * Get rid of the freespace block.
1889 */
1890 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1891 if (error) {
1892 /*
1893 * This can't fail here because it can only happen when
1894 * punching out the middle of an extent, and this is an
1895 * isolated block.
1896 */
1897 ASSERT(error != ENOSPC);
1898 return error;
1899 }
1900 fbp = NULL;
1901 /*
1902 * Now see if we can convert the single-leaf directory
1903 * down to a block form directory.
1904 * This routine always kills the dabuf for the leaf, so
1905 * eliminate it from the path.
1906 */
1907 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1908 state->path.blk[0].bp = NULL;
1909 return error;
1910}