blob: d629b888ca28f26c0cfc25252114ddcd3002b69c [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dir2_sf.h"
32#include "xfs_dinode.h"
33#include "xfs_inode.h"
34#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dir2_data.h"
36#include "xfs_dir2_leaf.h"
37#include "xfs_dir2_block.h"
38#include "xfs_dir2_node.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000040#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * Local function declarations.
44 */
45#ifdef DEBUG
46static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
47#else
48#define xfs_dir2_leaf_check(dp, bp)
49#endif
50static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **lbpp,
51 int *indexp, xfs_dabuf_t **dbpp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100052static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp,
53 int first, int last);
Eric Sandeen6add2c42005-06-21 15:39:44 +100054static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
58 * Convert a block form directory to a leaf form directory.
59 */
60int /* error */
61xfs_dir2_block_to_leaf(
62 xfs_da_args_t *args, /* operation arguments */
63 xfs_dabuf_t *dbp) /* input block's buffer */
64{
Nathan Scott68b3a102006-03-17 17:27:19 +110065 __be16 *bestsp; /* leaf's bestsp entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 xfs_dablk_t blkno; /* leaf block's bno */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +020067 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
69 xfs_dir2_block_tail_t *btp; /* block's tail */
70 xfs_inode_t *dp; /* incore directory inode */
71 int error; /* error return code */
72 xfs_dabuf_t *lbp; /* leaf block's buffer */
73 xfs_dir2_db_t ldb; /* leaf block's bno */
74 xfs_dir2_leaf_t *leaf; /* leaf structure */
75 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
76 xfs_mount_t *mp; /* filesystem mount point */
77 int needlog; /* need to log block header */
78 int needscan; /* need to rescan bestfree */
79 xfs_trans_t *tp; /* transaction pointer */
80
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000081 trace_xfs_dir2_block_to_leaf(args);
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 dp = args->dp;
84 mp = dp->i_mount;
85 tp = args->trans;
86 /*
87 * Add the leaf block to the inode.
88 * This interface will only put blocks in the leaf/node range.
89 * Since that's empty now, we'll get the root (block 0 in range).
90 */
91 if ((error = xfs_da_grow_inode(args, &blkno))) {
92 return error;
93 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100094 ldb = xfs_dir2_da_to_db(mp, blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
96 /*
97 * Initialize the leaf block, get a buffer for it.
98 */
99 if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
100 return error;
101 }
102 ASSERT(lbp != NULL);
103 leaf = lbp->data;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200104 hdr = dbp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 xfs_dir2_data_check(dp, dbp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200106 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000107 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /*
109 * Set the counts in the leaf header.
110 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100111 leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
112 leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /*
114 * Could compact these but I think we always do the conversion
115 * after squeezing out stale entries.
116 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100117 memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100118 xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 needscan = 0;
120 needlog = 1;
121 /*
122 * Make the space formerly occupied by the leaf entries and block
123 * tail be free.
124 */
125 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200126 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
127 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 (char *)blp),
129 &needlog, &needscan);
130 /*
131 * Fix up the block header, make it a data block.
132 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200133 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200135 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /*
137 * Set up leaf tail and bests table.
138 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000139 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100140 ltp->bestcount = cpu_to_be32(1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000141 bestsp = xfs_dir2_leaf_bests_p(ltp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200142 bestsp[0] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /*
144 * Log the data header and leaf bests table.
145 */
146 if (needlog)
147 xfs_dir2_data_log_header(tp, dbp);
148 xfs_dir2_leaf_check(dp, lbp);
149 xfs_dir2_data_check(dp, dbp);
150 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
151 xfs_da_buf_done(lbp);
152 return 0;
153}
154
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200155struct xfs_dir2_leaf_entry *
156xfs_dir2_leaf_find_entry(
157 xfs_dir2_leaf_t *leaf, /* leaf structure */
158 int index, /* leaf table position */
159 int compact, /* need to compact leaves */
160 int lowstale, /* index of prev stale leaf */
161 int highstale, /* index of next stale leaf */
162 int *lfloglow, /* low leaf logging index */
163 int *lfloghigh) /* high leaf logging index */
164{
165 if (!leaf->hdr.stale) {
166 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
167
168 /*
169 * Now we need to make room to insert the leaf entry.
170 *
171 * If there are no stale entries, just insert a hole at index.
172 */
173 lep = &leaf->ents[index];
174 if (index < be16_to_cpu(leaf->hdr.count))
175 memmove(lep + 1, lep,
176 (be16_to_cpu(leaf->hdr.count) - index) *
177 sizeof(*lep));
178
179 /*
180 * Record low and high logging indices for the leaf.
181 */
182 *lfloglow = index;
183 *lfloghigh = be16_to_cpu(leaf->hdr.count);
184 be16_add_cpu(&leaf->hdr.count, 1);
185 return lep;
186 }
187
188 /*
189 * There are stale entries.
190 *
191 * We will use one of them for the new entry. It's probably not at
192 * the right location, so we'll have to shift some up or down first.
193 *
194 * If we didn't compact before, we need to find the nearest stale
195 * entries before and after our insertion point.
196 */
197 if (compact == 0) {
198 /*
199 * Find the first stale entry before the insertion point,
200 * if any.
201 */
202 for (lowstale = index - 1;
203 lowstale >= 0 &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200204 leaf->ents[lowstale].address !=
205 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200206 lowstale--)
207 continue;
208
209 /*
210 * Find the next stale entry at or after the insertion point,
211 * if any. Stop if we go so far that the lowstale entry
212 * would be better.
213 */
214 for (highstale = index;
215 highstale < be16_to_cpu(leaf->hdr.count) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200216 leaf->ents[highstale].address !=
217 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200218 (lowstale < 0 ||
219 index - lowstale - 1 >= highstale - index);
220 highstale++)
221 continue;
222 }
223
224 /*
225 * If the low one is better, use it.
226 */
227 if (lowstale >= 0 &&
228 (highstale == be16_to_cpu(leaf->hdr.count) ||
229 index - lowstale - 1 < highstale - index)) {
230 ASSERT(index - lowstale - 1 >= 0);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200231 ASSERT(leaf->ents[lowstale].address ==
232 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200233
234 /*
235 * Copy entries up to cover the stale entry and make room
236 * for the new entry.
237 */
238 if (index - lowstale - 1 > 0) {
239 memmove(&leaf->ents[lowstale],
240 &leaf->ents[lowstale + 1],
241 (index - lowstale - 1) *
242 sizeof(xfs_dir2_leaf_entry_t));
243 }
244 *lfloglow = MIN(lowstale, *lfloglow);
245 *lfloghigh = MAX(index - 1, *lfloghigh);
246 be16_add_cpu(&leaf->hdr.stale, -1);
247 return &leaf->ents[index - 1];
248 }
249
250 /*
251 * The high one is better, so use that one.
252 */
253 ASSERT(highstale - index >= 0);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200254 ASSERT(leaf->ents[highstale].address ==
255 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200256
257 /*
258 * Copy entries down to cover the stale entry and make room for the
259 * new entry.
260 */
261 if (highstale - index > 0) {
262 memmove(&leaf->ents[index + 1],
263 &leaf->ents[index],
264 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
265 }
266 *lfloglow = MIN(index, *lfloglow);
267 *lfloghigh = MAX(highstale, *lfloghigh);
268 be16_add_cpu(&leaf->hdr.stale, -1);
269 return &leaf->ents[index];
270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/*
273 * Add an entry to a leaf form directory.
274 */
275int /* error */
276xfs_dir2_leaf_addname(
277 xfs_da_args_t *args) /* operation arguments */
278{
Nathan Scott68b3a102006-03-17 17:27:19 +1100279 __be16 *bestsp; /* freespace table in leaf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 int compact; /* need to compact leaves */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200281 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 xfs_dabuf_t *dbp; /* data block buffer */
283 xfs_dir2_data_entry_t *dep; /* data block entry */
284 xfs_inode_t *dp; /* incore directory inode */
285 xfs_dir2_data_unused_t *dup; /* data unused entry */
286 int error; /* error return value */
287 int grown; /* allocated new data block */
288 int highstale; /* index of next stale leaf */
289 int i; /* temporary, index */
290 int index; /* leaf table position */
291 xfs_dabuf_t *lbp; /* leaf's buffer */
292 xfs_dir2_leaf_t *leaf; /* leaf structure */
293 int length; /* length of new entry */
294 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
295 int lfloglow; /* low leaf logging index */
296 int lfloghigh; /* high leaf logging index */
297 int lowstale; /* index of prev stale leaf */
298 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
299 xfs_mount_t *mp; /* filesystem mount point */
300 int needbytes; /* leaf block bytes needed */
301 int needlog; /* need to log data header */
302 int needscan; /* need to rescan data free */
Nathan Scott3d693c62006-03-17 17:28:27 +1100303 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 xfs_trans_t *tp; /* transaction pointer */
305 xfs_dir2_db_t use_block; /* data block number */
306
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000307 trace_xfs_dir2_leaf_addname(args);
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 dp = args->dp;
310 tp = args->trans;
311 mp = dp->i_mount;
312 /*
313 * Read the leaf block.
314 */
315 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
316 XFS_DATA_FORK);
317 if (error) {
318 return error;
319 }
320 ASSERT(lbp != NULL);
321 /*
322 * Look up the entry by hash value and name.
323 * We know it's not there, our caller has already done a lookup.
324 * So the index is of the entry to insert in front of.
325 * But if there are dup hash values the index is of the first of those.
326 */
327 index = xfs_dir2_leaf_search_hash(args, lbp);
328 leaf = lbp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000329 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
330 bestsp = xfs_dir2_leaf_bests_p(ltp);
331 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /*
333 * See if there are any entries with the same hash value
334 * and space in their block for the new entry.
335 * This is good because it puts multiple same-hash value entries
336 * in a data block, improving the lookup of those entries.
337 */
338 for (use_block = -1, lep = &leaf->ents[index];
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100339 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 index++, lep++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100341 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 continue;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000343 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100344 ASSERT(i < be32_to_cpu(ltp->bestcount));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200345 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
Nathan Scott68b3a102006-03-17 17:27:19 +1100346 if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 use_block = i;
348 break;
349 }
350 }
351 /*
352 * Didn't find a block yet, linear search all the data blocks.
353 */
354 if (use_block == -1) {
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100355 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 /*
357 * Remember a block we see that's missing.
358 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200359 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
360 use_block == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 use_block = i;
Nathan Scott68b3a102006-03-17 17:27:19 +1100362 else if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 use_block = i;
364 break;
365 }
366 }
367 }
368 /*
369 * How many bytes do we need in the leaf block?
370 */
Christoph Hellwig22823962011-07-08 14:35:53 +0200371 needbytes = 0;
372 if (!leaf->hdr.stale)
373 needbytes += sizeof(xfs_dir2_leaf_entry_t);
374 if (use_block == -1)
375 needbytes += sizeof(xfs_dir2_data_off_t);
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /*
378 * Now kill use_block if it refers to a missing block, so we
379 * can use it as an indication of allocation needed.
380 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200381 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 use_block = -1;
383 /*
384 * If we don't have enough free bytes but we can make enough
385 * by compacting out stale entries, we'll do that.
386 */
Barry Naujok6a178102008-05-21 16:42:05 +1000387 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
388 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 compact = 1;
390 }
391 /*
392 * Otherwise if we don't have enough free bytes we need to
393 * convert to node form.
394 */
Barry Naujok6a178102008-05-21 16:42:05 +1000395 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
396 leaf->hdr.count)] < needbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
398 * Just checking or no space reservation, give up.
399 */
Barry Naujok6a178102008-05-21 16:42:05 +1000400 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
401 args->total == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 xfs_da_brelse(tp, lbp);
403 return XFS_ERROR(ENOSPC);
404 }
405 /*
406 * Convert to node form.
407 */
408 error = xfs_dir2_leaf_to_node(args, lbp);
409 xfs_da_buf_done(lbp);
410 if (error)
411 return error;
412 /*
413 * Then add the new entry.
414 */
415 return xfs_dir2_node_addname(args);
416 }
417 /*
418 * Otherwise it will fit without compaction.
419 */
420 else
421 compact = 0;
422 /*
423 * If just checking, then it will fit unless we needed to allocate
424 * a new data block.
425 */
Barry Naujok6a178102008-05-21 16:42:05 +1000426 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 xfs_da_brelse(tp, lbp);
428 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
429 }
430 /*
431 * If no allocations are allowed, return now before we've
432 * changed anything.
433 */
434 if (args->total == 0 && use_block == -1) {
435 xfs_da_brelse(tp, lbp);
436 return XFS_ERROR(ENOSPC);
437 }
438 /*
439 * Need to compact the leaf entries, removing stale ones.
440 * Leave one stale entry behind - the one closest to our
441 * insertion index - and we'll shift that one to our insertion
442 * point later.
443 */
444 if (compact) {
445 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
446 &lfloglow, &lfloghigh);
447 }
448 /*
449 * There are stale entries, so we'll need log-low and log-high
450 * impossibly bad values later.
451 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100452 else if (be16_to_cpu(leaf->hdr.stale)) {
453 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 lfloghigh = -1;
455 }
456 /*
457 * If there was no data block space found, we need to allocate
458 * a new one.
459 */
460 if (use_block == -1) {
461 /*
462 * Add the new data block.
463 */
464 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
465 &use_block))) {
466 xfs_da_brelse(tp, lbp);
467 return error;
468 }
469 /*
470 * Initialize the block.
471 */
472 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
473 xfs_da_brelse(tp, lbp);
474 return error;
475 }
476 /*
477 * If we're adding a new data block on the end we need to
478 * extend the bests table. Copy it up one entry.
479 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100480 if (use_block >= be32_to_cpu(ltp->bestcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 bestsp--;
482 memmove(&bestsp[0], &bestsp[1],
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100483 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800484 be32_add_cpu(&ltp->bestcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100486 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 /*
489 * If we're filling in a previously empty block just log it.
490 */
491 else
492 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200493 hdr = dbp->data;
494 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 grown = 1;
496 }
497 /*
498 * Already had space in some data block.
499 * Just read that one in.
500 */
501 else {
502 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000503 xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 -1, &dbp, XFS_DATA_FORK))) {
505 xfs_da_brelse(tp, lbp);
506 return error;
507 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200508 hdr = dbp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 grown = 0;
510 }
511 xfs_dir2_data_check(dp, dbp);
512 /*
513 * Point to the biggest freespace in our data block.
514 */
515 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200516 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
Nathan Scottad354eb2006-03-17 17:27:37 +1100517 ASSERT(be16_to_cpu(dup->length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 needscan = needlog = 0;
519 /*
520 * Mark the initial part of our freespace in use for the new entry.
521 */
522 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200523 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 &needlog, &needscan);
525 /*
526 * Initialize our new entry (at last).
527 */
528 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000529 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 dep->namelen = args->namelen;
531 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000532 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200533 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /*
535 * Need to scan fix up the bestfree table.
536 */
537 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200538 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /*
540 * Need to log the data block's header.
541 */
542 if (needlog)
543 xfs_dir2_data_log_header(tp, dbp);
544 xfs_dir2_data_log_entry(tp, dbp, dep);
545 /*
546 * If the bests table needs to be changed, do it.
547 * Log the change unless we've already done that.
548 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200549 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
550 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!grown)
552 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
553 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200554
555 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
556 highstale, &lfloglow, &lfloghigh);
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /*
559 * Fill in the new leaf entry.
560 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100561 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000562 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
Nathan Scott3d693c62006-03-17 17:28:27 +1100563 be16_to_cpu(*tagp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * Log the leaf fields and give up the buffers.
566 */
567 xfs_dir2_leaf_log_header(tp, lbp);
568 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
569 xfs_dir2_leaf_check(dp, lbp);
570 xfs_da_buf_done(lbp);
571 xfs_dir2_data_check(dp, dbp);
572 xfs_da_buf_done(dbp);
573 return 0;
574}
575
576#ifdef DEBUG
577/*
578 * Check the internal consistency of a leaf1 block.
579 * Pop an assert if something is wrong.
580 */
Hannes Eder3180e662009-03-04 19:34:10 +0100581STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582xfs_dir2_leaf_check(
583 xfs_inode_t *dp, /* incore directory inode */
584 xfs_dabuf_t *bp) /* leaf's buffer */
585{
586 int i; /* leaf index */
587 xfs_dir2_leaf_t *leaf; /* leaf structure */
588 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
589 xfs_mount_t *mp; /* filesystem mount point */
590 int stale; /* count of stale leaves */
591
592 leaf = bp->data;
593 mp = dp->i_mount;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200594 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /*
596 * This value is not restrictive enough.
597 * Should factor in the size of the bests table as well.
598 * We can deduce a value for that from di_size.
599 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000600 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
601 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /*
603 * Leaves and bests don't overlap.
604 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100605 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000606 (char *)xfs_dir2_leaf_bests_p(ltp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /*
608 * Check hash value order, count stale entries.
609 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100610 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
611 if (i + 1 < be16_to_cpu(leaf->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100612 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
613 be32_to_cpu(leaf->ents[i + 1].hashval));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200614 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 stale++;
616 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100617 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619#endif /* DEBUG */
620
621/*
622 * Compact out any stale entries in the leaf.
623 * Log the header and changed leaf entries, if any.
624 */
625void
626xfs_dir2_leaf_compact(
627 xfs_da_args_t *args, /* operation arguments */
628 xfs_dabuf_t *bp) /* leaf buffer */
629{
630 int from; /* source leaf index */
631 xfs_dir2_leaf_t *leaf; /* leaf structure */
632 int loglow; /* first leaf entry to log */
633 int to; /* target leaf index */
634
635 leaf = bp->data;
636 if (!leaf->hdr.stale) {
637 return;
638 }
639 /*
640 * Compress out the stale entries in place.
641 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100642 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200643 if (leaf->ents[from].address ==
644 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 continue;
646 /*
647 * Only actually copy the entries that are different.
648 */
649 if (from > to) {
650 if (loglow == -1)
651 loglow = to;
652 leaf->ents[to] = leaf->ents[from];
653 }
654 to++;
655 }
656 /*
657 * Update and log the header, log the leaf entries.
658 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100659 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800660 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 leaf->hdr.stale = 0;
662 xfs_dir2_leaf_log_header(args->trans, bp);
663 if (loglow != -1)
664 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
665}
666
667/*
668 * Compact the leaf entries, removing stale ones.
669 * Leave one stale entry behind - the one closest to our
670 * insertion index - and the caller will shift that one to our insertion
671 * point later.
672 * Return new insertion index, where the remaining stale entry is,
673 * and leaf logging indices.
674 */
675void
676xfs_dir2_leaf_compact_x1(
677 xfs_dabuf_t *bp, /* leaf buffer */
678 int *indexp, /* insertion index */
679 int *lowstalep, /* out: stale entry before us */
680 int *highstalep, /* out: stale entry after us */
681 int *lowlogp, /* out: low log index */
682 int *highlogp) /* out: high log index */
683{
684 int from; /* source copy index */
685 int highstale; /* stale entry at/after index */
686 int index; /* insertion index */
687 int keepstale; /* source index of kept stale */
688 xfs_dir2_leaf_t *leaf; /* leaf structure */
689 int lowstale; /* stale entry before index */
690 int newindex=0; /* new insertion index */
691 int to; /* destination copy index */
692
693 leaf = bp->data;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100694 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 index = *indexp;
696 /*
697 * Find the first stale entry before our index, if any.
698 */
699 for (lowstale = index - 1;
700 lowstale >= 0 &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200701 leaf->ents[lowstale].address !=
702 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 lowstale--)
704 continue;
705 /*
706 * Find the first stale entry at or after our index, if any.
707 * Stop if the answer would be worse than lowstale.
708 */
709 for (highstale = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100710 highstale < be16_to_cpu(leaf->hdr.count) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200711 leaf->ents[highstale].address !=
712 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 (lowstale < 0 || index - lowstale > highstale - index);
714 highstale++)
715 continue;
716 /*
717 * Pick the better of lowstale and highstale.
718 */
719 if (lowstale >= 0 &&
Nathan Scotta818e5d2006-03-17 17:28:07 +1100720 (highstale == be16_to_cpu(leaf->hdr.count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 index - lowstale <= highstale - index))
722 keepstale = lowstale;
723 else
724 keepstale = highstale;
725 /*
726 * Copy the entries in place, removing all the stale entries
727 * except keepstale.
728 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100729 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /*
731 * Notice the new value of index.
732 */
733 if (index == from)
734 newindex = to;
735 if (from != keepstale &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200736 leaf->ents[from].address ==
737 cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (from == to)
739 *lowlogp = to;
740 continue;
741 }
742 /*
743 * Record the new keepstale value for the insertion.
744 */
745 if (from == keepstale)
746 lowstale = highstale = to;
747 /*
748 * Copy only the entries that have moved.
749 */
750 if (from > to)
751 leaf->ents[to] = leaf->ents[from];
752 to++;
753 }
754 ASSERT(from > to);
755 /*
756 * If the insertion point was past the last entry,
757 * set the new insertion point accordingly.
758 */
759 if (index == from)
760 newindex = to;
761 *indexp = newindex;
762 /*
763 * Adjust the leaf header values.
764 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800765 be16_add_cpu(&leaf->hdr.count, -(from - to));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100766 leaf->hdr.stale = cpu_to_be16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /*
768 * Remember the low/high stale value only in the "right"
769 * direction.
770 */
771 if (lowstale >= newindex)
772 lowstale = -1;
773 else
Nathan Scotta818e5d2006-03-17 17:28:07 +1100774 highstale = be16_to_cpu(leaf->hdr.count);
775 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 *lowstalep = lowstale;
777 *highstalep = highstale;
778}
779
780/*
781 * Getdents (readdir) for leaf and node directories.
782 * This reads the data blocks only, so is the same for both forms.
783 */
784int /* error */
785xfs_dir2_leaf_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 xfs_inode_t *dp, /* incore directory inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000787 void *dirent,
788 size_t bufsize,
789 xfs_off_t *offset,
790 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 xfs_dabuf_t *bp; /* data block buffer */
793 int byteoff; /* offset in current block */
794 xfs_dir2_db_t curdb; /* db for current block */
795 xfs_dir2_off_t curoff; /* current overall offset */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200796 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 xfs_dir2_data_entry_t *dep; /* data entry */
798 xfs_dir2_data_unused_t *dup; /* unused entry */
Nathan Scottf6d75cb2006-03-14 13:32:24 +1100799 int error = 0; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 int i; /* temporary loop index */
801 int j; /* temporary loop index */
802 int length; /* temporary length value */
803 xfs_bmbt_irec_t *map; /* map vector for blocks */
804 xfs_extlen_t map_blocks; /* number of fsbs in map */
805 xfs_dablk_t map_off; /* last mapped file offset */
806 int map_size; /* total entries in *map */
807 int map_valid; /* valid entries in *map */
808 xfs_mount_t *mp; /* filesystem mount point */
809 xfs_dir2_off_t newoff; /* new curoff after new blk */
810 int nmap; /* mappings to ask xfs_bmapi */
Nathan Scottf6d75cb2006-03-14 13:32:24 +1100811 char *ptr = NULL; /* pointer to current data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 int ra_current; /* number of read-ahead blks */
813 int ra_index; /* *map index for read-ahead */
814 int ra_offset; /* map entry offset for ra */
815 int ra_want; /* readahead count wanted */
816
817 /*
818 * If the offset is at or past the largest allowed value,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000819 * give up right away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000821 if (*offset >= XFS_DIR2_MAX_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 mp = dp->i_mount;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /*
827 * Set up to bmap a number of blocks based on the caller's
828 * buffer size, the directory block size, and the filesystem
829 * block size.
830 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000831 map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
833 map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
834 bp = NULL;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /*
837 * Inside the loop we keep the main offset value as a byte offset
838 * in the directory file.
839 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000840 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /*
843 * Force this conversion through db so we truncate the offset
844 * down to get the start of the data block.
845 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000846 map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /*
848 * Loop over directory entries until we reach the end offset.
849 * Get more blocks and readahead as necessary.
850 */
851 while (curoff < XFS_DIR2_LEAF_OFFSET) {
852 /*
853 * If we have no buffer, or we're off the end of the
854 * current buffer, need to get another one.
855 */
856 if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) {
857 /*
858 * If we have a buffer, we need to release it and
859 * take it out of the mapping.
860 */
861 if (bp) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000862 xfs_da_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 bp = NULL;
864 map_blocks -= mp->m_dirblkfsbs;
865 /*
866 * Loop to get rid of the extents for the
867 * directory block.
868 */
869 for (i = mp->m_dirblkfsbs; i > 0; ) {
870 j = MIN((int)map->br_blockcount, i);
871 map->br_blockcount -= j;
872 map->br_startblock += j;
873 map->br_startoff += j;
874 /*
875 * If mapping is done, pitch it from
876 * the table.
877 */
878 if (!map->br_blockcount && --map_valid)
879 memmove(&map[0], &map[1],
880 sizeof(map[0]) *
881 map_valid);
882 i -= j;
883 }
884 }
885 /*
886 * Recalculate the readahead blocks wanted.
887 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000888 ra_want = howmany(bufsize + mp->m_dirblksize,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 mp->m_sb.sb_blocksize) - 1;
Eric Sandeen8e69ce12009-09-25 19:42:26 +0000890 ASSERT(ra_want >= 0);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /*
893 * If we don't have as many as we want, and we haven't
894 * run out of data blocks, get some more mappings.
895 */
896 if (1 + ra_want > map_blocks &&
897 map_off <
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000898 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /*
900 * Get more bmaps, fill in after the ones
901 * we already have in the table.
902 */
903 nmap = map_size - map_valid;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000904 error = xfs_bmapi(NULL, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 map_off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000906 xfs_dir2_byte_to_da(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 XFS_DIR2_LEAF_OFFSET) - map_off,
908 XFS_BMAPI_METADATA, NULL, 0,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000909 &map[map_valid], &nmap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /*
911 * Don't know if we should ignore this or
912 * try to return an error.
913 * The trouble with returning errors
914 * is that readdir will just stop without
915 * actually passing the error through.
916 */
917 if (error)
918 break; /* XXX */
919 /*
920 * If we got all the mappings we asked for,
921 * set the final map offset based on the
922 * last bmap value received.
923 * Otherwise, we've reached the end.
924 */
925 if (nmap == map_size - map_valid)
926 map_off =
927 map[map_valid + nmap - 1].br_startoff +
928 map[map_valid + nmap - 1].br_blockcount;
929 else
930 map_off =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000931 xfs_dir2_byte_to_da(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 XFS_DIR2_LEAF_OFFSET);
933 /*
934 * Look for holes in the mapping, and
935 * eliminate them. Count up the valid blocks.
936 */
937 for (i = map_valid; i < map_valid + nmap; ) {
938 if (map[i].br_startblock ==
939 HOLESTARTBLOCK) {
940 nmap--;
941 length = map_valid + nmap - i;
942 if (length)
943 memmove(&map[i],
944 &map[i + 1],
945 sizeof(map[i]) *
946 length);
947 } else {
948 map_blocks +=
949 map[i].br_blockcount;
950 i++;
951 }
952 }
953 map_valid += nmap;
954 }
955 /*
956 * No valid mappings, so no more data blocks.
957 */
958 if (!map_valid) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000959 curoff = xfs_dir2_da_to_byte(mp, map_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 break;
961 }
962 /*
963 * Read the directory block starting at the first
964 * mapping.
965 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000966 curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000967 error = xfs_da_read_buf(NULL, dp, map->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 map->br_blockcount >= mp->m_dirblkfsbs ?
969 XFS_FSB_TO_DADDR(mp, map->br_startblock) :
970 -1,
971 &bp, XFS_DATA_FORK);
972 /*
973 * Should just skip over the data block instead
974 * of giving up.
975 */
976 if (error)
977 break; /* XXX */
978 /*
979 * Adjust the current amount of read-ahead: we just
980 * read a block that was previously ra.
981 */
982 if (ra_current)
983 ra_current -= mp->m_dirblkfsbs;
984 /*
985 * Do we need more readahead?
986 */
987 for (ra_index = ra_offset = i = 0;
988 ra_want > ra_current && i < map_blocks;
989 i += mp->m_dirblkfsbs) {
990 ASSERT(ra_index < map_valid);
991 /*
992 * Read-ahead a contiguous directory block.
993 */
994 if (i > ra_current &&
995 map[ra_index].br_blockcount >=
996 mp->m_dirblkfsbs) {
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000997 xfs_buf_readahead(mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 XFS_FSB_TO_DADDR(mp,
999 map[ra_index].br_startblock +
1000 ra_offset),
1001 (int)BTOBB(mp->m_dirblksize));
1002 ra_current = i;
1003 }
1004 /*
1005 * Read-ahead a non-contiguous directory block.
1006 * This doesn't use our mapping, but this
1007 * is a very rare case.
1008 */
1009 else if (i > ra_current) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001010 (void)xfs_da_reada_buf(NULL, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 map[ra_index].br_startoff +
1012 ra_offset, XFS_DATA_FORK);
1013 ra_current = i;
1014 }
1015 /*
1016 * Advance offset through the mapping table.
1017 */
1018 for (j = 0; j < mp->m_dirblkfsbs; j++) {
1019 /*
1020 * The rest of this extent but not
1021 * more than a dir block.
1022 */
1023 length = MIN(mp->m_dirblkfsbs,
1024 (int)(map[ra_index].br_blockcount -
1025 ra_offset));
1026 j += length;
1027 ra_offset += length;
1028 /*
1029 * Advance to the next mapping if
1030 * this one is used up.
1031 */
1032 if (ra_offset ==
1033 map[ra_index].br_blockcount) {
1034 ra_offset = 0;
1035 ra_index++;
1036 }
1037 }
1038 }
1039 /*
1040 * Having done a read, we need to set a new offset.
1041 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001042 newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /*
1044 * Start of the current block.
1045 */
1046 if (curoff < newoff)
1047 curoff = newoff;
1048 /*
1049 * Make sure we're in the right block.
1050 */
1051 else if (curoff > newoff)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001052 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 curdb);
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +02001054 hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 xfs_dir2_data_check(dp, bp);
1056 /*
1057 * Find our position in the block.
1058 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +02001059 ptr = (char *)(hdr + 1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001060 byteoff = xfs_dir2_byte_to_off(mp, curoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 /*
1062 * Skip past the header.
1063 */
1064 if (byteoff == 0)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001065 curoff += (uint)sizeof(*hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /*
1067 * Skip past entries until we reach our offset.
1068 */
1069 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001070 while ((char *)ptr - (char *)hdr < byteoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 dup = (xfs_dir2_data_unused_t *)ptr;
1072
Nathan Scottad354eb2006-03-17 17:27:37 +11001073 if (be16_to_cpu(dup->freetag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 == XFS_DIR2_DATA_FREE_TAG) {
1075
Nathan Scottad354eb2006-03-17 17:27:37 +11001076 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 ptr += length;
1078 continue;
1079 }
1080 dep = (xfs_dir2_data_entry_t *)ptr;
1081 length =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001082 xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 ptr += length;
1084 }
1085 /*
1086 * Now set our real offset.
1087 */
1088 curoff =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001089 xfs_dir2_db_off_to_byte(mp,
1090 xfs_dir2_byte_to_db(mp, curoff),
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001091 (char *)ptr - (char *)hdr);
1092 if (ptr >= (char *)hdr + mp->m_dirblksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 continue;
1094 }
1095 }
1096 }
1097 /*
1098 * We have a pointer to an entry.
1099 * Is it a live one?
1100 */
1101 dup = (xfs_dir2_data_unused_t *)ptr;
1102 /*
1103 * No, it's unused, skip over it.
1104 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001105 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1106 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 ptr += length;
1108 curoff += length;
1109 continue;
1110 }
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 dep = (xfs_dir2_data_entry_t *)ptr;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001113 length = xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Dave Chinner4a24cb72010-01-20 10:48:05 +11001115 if (filldir(dirent, (char *)dep->name, dep->namelen,
Christoph Hellwig15440312009-01-08 14:00:00 -05001116 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
Christoph Hellwiga19d9f82009-03-29 09:51:08 +02001117 be64_to_cpu(dep->inumber), DT_UNKNOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 break;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /*
1121 * Advance to next entry in the block.
1122 */
1123 ptr += length;
1124 curoff += length;
Eric Sandeen8e69ce12009-09-25 19:42:26 +00001125 /* bufsize may have just been a guess; don't go negative */
1126 bufsize = bufsize > length ? bufsize - length : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
1129 /*
1130 * All done. Set output offset value to current offset.
1131 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001132 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
Christoph Hellwig15440312009-01-08 14:00:00 -05001133 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 else
Christoph Hellwig15440312009-01-08 14:00:00 -05001135 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001136 kmem_free(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (bp)
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001138 xfs_da_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return error;
1140}
1141
1142/*
1143 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1144 */
1145int
1146xfs_dir2_leaf_init(
1147 xfs_da_args_t *args, /* operation arguments */
1148 xfs_dir2_db_t bno, /* directory block number */
1149 xfs_dabuf_t **bpp, /* out: leaf buffer */
1150 int magic) /* magic number for block */
1151{
1152 xfs_dabuf_t *bp; /* leaf buffer */
1153 xfs_inode_t *dp; /* incore directory inode */
1154 int error; /* error return code */
1155 xfs_dir2_leaf_t *leaf; /* leaf structure */
1156 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1157 xfs_mount_t *mp; /* filesystem mount point */
1158 xfs_trans_t *tp; /* transaction pointer */
1159
1160 dp = args->dp;
1161 ASSERT(dp != NULL);
1162 tp = args->trans;
1163 mp = dp->i_mount;
1164 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1165 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1166 /*
1167 * Get the buffer for the block.
1168 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001169 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 XFS_DATA_FORK);
1171 if (error) {
1172 return error;
1173 }
1174 ASSERT(bp != NULL);
1175 leaf = bp->data;
1176 /*
1177 * Initialize the header.
1178 */
Nathan Scott89da0542006-03-17 17:28:40 +11001179 leaf->hdr.info.magic = cpu_to_be16(magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 leaf->hdr.info.forw = 0;
1181 leaf->hdr.info.back = 0;
1182 leaf->hdr.count = 0;
1183 leaf->hdr.stale = 0;
1184 xfs_dir2_leaf_log_header(tp, bp);
1185 /*
1186 * If it's a leaf-format directory initialize the tail.
1187 * In this case our caller has the real bests table to copy into
1188 * the block.
1189 */
1190 if (magic == XFS_DIR2_LEAF1_MAGIC) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001191 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 ltp->bestcount = 0;
1193 xfs_dir2_leaf_log_tail(tp, bp);
1194 }
1195 *bpp = bp;
1196 return 0;
1197}
1198
1199/*
1200 * Log the bests entries indicated from a leaf1 block.
1201 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001202static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203xfs_dir2_leaf_log_bests(
1204 xfs_trans_t *tp, /* transaction pointer */
1205 xfs_dabuf_t *bp, /* leaf buffer */
1206 int first, /* first entry to log */
1207 int last) /* last entry to log */
1208{
Nathan Scott68b3a102006-03-17 17:27:19 +11001209 __be16 *firstb; /* pointer to first entry */
1210 __be16 *lastb; /* pointer to last entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 xfs_dir2_leaf_t *leaf; /* leaf structure */
1212 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1213
1214 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001215 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001216 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1217 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1218 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1220 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1221}
1222
1223/*
1224 * Log the leaf entries indicated from a leaf1 or leafn block.
1225 */
1226void
1227xfs_dir2_leaf_log_ents(
1228 xfs_trans_t *tp, /* transaction pointer */
1229 xfs_dabuf_t *bp, /* leaf buffer */
1230 int first, /* first entry to log */
1231 int last) /* last entry to log */
1232{
1233 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1234 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1235 xfs_dir2_leaf_t *leaf; /* leaf structure */
1236
1237 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001238 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1239 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 firstlep = &leaf->ents[first];
1241 lastlep = &leaf->ents[last];
1242 xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1243 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1244}
1245
1246/*
1247 * Log the header of the leaf1 or leafn block.
1248 */
1249void
1250xfs_dir2_leaf_log_header(
1251 xfs_trans_t *tp, /* transaction pointer */
1252 xfs_dabuf_t *bp) /* leaf buffer */
1253{
1254 xfs_dir2_leaf_t *leaf; /* leaf structure */
1255
1256 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001257 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1258 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1260 (uint)(sizeof(leaf->hdr) - 1));
1261}
1262
1263/*
1264 * Log the tail of the leaf1 block.
1265 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001266STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267xfs_dir2_leaf_log_tail(
1268 xfs_trans_t *tp, /* transaction pointer */
1269 xfs_dabuf_t *bp) /* leaf buffer */
1270{
1271 xfs_dir2_leaf_t *leaf; /* leaf structure */
1272 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1273 xfs_mount_t *mp; /* filesystem mount point */
1274
1275 mp = tp->t_mountp;
1276 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001277 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001278 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1280 (uint)(mp->m_dirblksize - 1));
1281}
1282
1283/*
1284 * Look up the entry referred to by args in the leaf format directory.
1285 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1286 * is also used by the node-format code.
1287 */
1288int
1289xfs_dir2_leaf_lookup(
1290 xfs_da_args_t *args) /* operation arguments */
1291{
1292 xfs_dabuf_t *dbp; /* data block buffer */
1293 xfs_dir2_data_entry_t *dep; /* data block entry */
1294 xfs_inode_t *dp; /* incore directory inode */
1295 int error; /* error return code */
1296 int index; /* found entry index */
1297 xfs_dabuf_t *lbp; /* leaf buffer */
1298 xfs_dir2_leaf_t *leaf; /* leaf structure */
1299 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1300 xfs_trans_t *tp; /* transaction pointer */
1301
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001302 trace_xfs_dir2_leaf_lookup(args);
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
1305 * Look up name in the leaf block, returning both buffers and index.
1306 */
1307 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1308 return error;
1309 }
1310 tp = args->trans;
1311 dp = args->dp;
1312 xfs_dir2_leaf_check(dp, lbp);
1313 leaf = lbp->data;
1314 /*
1315 * Get to the leaf entry and contained data entry address.
1316 */
1317 lep = &leaf->ents[index];
1318 /*
1319 * Point to the data entry.
1320 */
1321 dep = (xfs_dir2_data_entry_t *)
1322 ((char *)dbp->data +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001323 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +10001325 * Return the found inode number & CI name if appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001327 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001328 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 xfs_da_brelse(tp, dbp);
1330 xfs_da_brelse(tp, lbp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001331 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
1334/*
1335 * Look up name/hash in the leaf block.
1336 * Fill in indexp with the found index, and dbpp with the data buffer.
1337 * If not found dbpp will be NULL, and ENOENT comes back.
1338 * lbpp will always be filled in with the leaf buffer unless there's an error.
1339 */
1340static int /* error */
1341xfs_dir2_leaf_lookup_int(
1342 xfs_da_args_t *args, /* operation arguments */
1343 xfs_dabuf_t **lbpp, /* out: leaf buffer */
1344 int *indexp, /* out: index in leaf block */
1345 xfs_dabuf_t **dbpp) /* out: data buffer */
1346{
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001347 xfs_dir2_db_t curdb = -1; /* current data block number */
1348 xfs_dabuf_t *dbp = NULL; /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 xfs_dir2_data_entry_t *dep; /* data entry */
1350 xfs_inode_t *dp; /* incore directory inode */
1351 int error; /* error return code */
1352 int index; /* index in leaf block */
1353 xfs_dabuf_t *lbp; /* leaf buffer */
1354 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1355 xfs_dir2_leaf_t *leaf; /* leaf structure */
1356 xfs_mount_t *mp; /* filesystem mount point */
1357 xfs_dir2_db_t newdb; /* new data block number */
1358 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001359 xfs_dir2_db_t cidb = -1; /* case match data block no. */
Barry Naujok5163f952008-05-21 16:41:01 +10001360 enum xfs_dacmp cmp; /* name compare result */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 dp = args->dp;
1363 tp = args->trans;
1364 mp = dp->i_mount;
1365 /*
1366 * Read the leaf block into the buffer.
1367 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001368 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1369 XFS_DATA_FORK);
1370 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 *lbpp = lbp;
1373 leaf = lbp->data;
1374 xfs_dir2_leaf_check(dp, lbp);
1375 /*
1376 * Look for the first leaf entry with our hash value.
1377 */
1378 index = xfs_dir2_leaf_search_hash(args, lbp);
1379 /*
1380 * Loop over all the entries with the right hash value
1381 * looking to match the name.
1382 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001383 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
Barry Naujok5163f952008-05-21 16:41:01 +10001384 be32_to_cpu(lep->hashval) == args->hashval;
1385 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /*
1387 * Skip over stale leaf entries.
1388 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001389 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 continue;
1391 /*
1392 * Get the new data block number.
1393 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001394 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 /*
1396 * If it's not the same as the old data block number,
1397 * need to pitch the old one and read the new one.
1398 */
1399 if (newdb != curdb) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001400 if (dbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 xfs_da_brelse(tp, dbp);
Barry Naujok5163f952008-05-21 16:41:01 +10001402 error = xfs_da_read_buf(tp, dp,
1403 xfs_dir2_db_to_da(mp, newdb),
1404 -1, &dbp, XFS_DATA_FORK);
1405 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 xfs_da_brelse(tp, lbp);
1407 return error;
1408 }
1409 xfs_dir2_data_check(dp, dbp);
1410 curdb = newdb;
1411 }
1412 /*
1413 * Point to the data entry.
1414 */
Barry Naujok5163f952008-05-21 16:41:01 +10001415 dep = (xfs_dir2_data_entry_t *)((char *)dbp->data +
1416 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /*
Barry Naujok5163f952008-05-21 16:41:01 +10001418 * Compare name and if it's an exact match, return the index
1419 * and buffer. If it's the first case-insensitive match, store
1420 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 */
Barry Naujok5163f952008-05-21 16:41:01 +10001422 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1423 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1424 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 *indexp = index;
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001426 /* case exact match: return the current buffer. */
Barry Naujok5163f952008-05-21 16:41:01 +10001427 if (cmp == XFS_CMP_EXACT) {
Barry Naujok5163f952008-05-21 16:41:01 +10001428 *dbpp = dbp;
1429 return 0;
1430 }
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001431 cidb = curdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433 }
Barry Naujok6a178102008-05-21 16:42:05 +10001434 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +10001435 /*
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001436 * Here, we can only be doing a lookup (not a rename or remove).
1437 * If a case-insensitive match was found earlier, re-read the
1438 * appropriate data block if required and return it.
Barry Naujok5163f952008-05-21 16:41:01 +10001439 */
1440 if (args->cmpresult == XFS_CMP_CASE) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001441 ASSERT(cidb != -1);
1442 if (cidb != curdb) {
Barry Naujok5163f952008-05-21 16:41:01 +10001443 xfs_da_brelse(tp, dbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001444 error = xfs_da_read_buf(tp, dp,
1445 xfs_dir2_db_to_da(mp, cidb),
1446 -1, &dbp, XFS_DATA_FORK);
1447 if (error) {
1448 xfs_da_brelse(tp, lbp);
1449 return error;
1450 }
1451 }
1452 *dbpp = dbp;
Barry Naujok5163f952008-05-21 16:41:01 +10001453 return 0;
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 /*
1456 * No match found, return ENOENT.
1457 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001458 ASSERT(cidb == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (dbp)
1460 xfs_da_brelse(tp, dbp);
1461 xfs_da_brelse(tp, lbp);
1462 return XFS_ERROR(ENOENT);
1463}
1464
1465/*
1466 * Remove an entry from a leaf format directory.
1467 */
1468int /* error */
1469xfs_dir2_leaf_removename(
1470 xfs_da_args_t *args) /* operation arguments */
1471{
Nathan Scott68b3a102006-03-17 17:27:19 +11001472 __be16 *bestsp; /* leaf block best freespace */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001473 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 xfs_dir2_db_t db; /* data block number */
1475 xfs_dabuf_t *dbp; /* data block buffer */
1476 xfs_dir2_data_entry_t *dep; /* data entry structure */
1477 xfs_inode_t *dp; /* incore directory inode */
1478 int error; /* error return code */
1479 xfs_dir2_db_t i; /* temporary data block # */
1480 int index; /* index into leaf entries */
1481 xfs_dabuf_t *lbp; /* leaf buffer */
1482 xfs_dir2_leaf_t *leaf; /* leaf structure */
1483 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1484 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1485 xfs_mount_t *mp; /* filesystem mount point */
1486 int needlog; /* need to log data header */
1487 int needscan; /* need to rescan data frees */
1488 xfs_dir2_data_off_t oldbest; /* old value of best free */
1489 xfs_trans_t *tp; /* transaction pointer */
1490
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001491 trace_xfs_dir2_leaf_removename(args);
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 /*
1494 * Lookup the leaf entry, get the leaf and data blocks read in.
1495 */
1496 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1497 return error;
1498 }
1499 dp = args->dp;
1500 tp = args->trans;
1501 mp = dp->i_mount;
1502 leaf = lbp->data;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001503 hdr = dbp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 xfs_dir2_data_check(dp, dbp);
1505 /*
1506 * Point to the leaf entry, use that to point to the data entry.
1507 */
1508 lep = &leaf->ents[index];
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001509 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001511 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 needscan = needlog = 0;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001513 oldbest = be16_to_cpu(hdr->bestfree[0].length);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001514 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1515 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott68b3a102006-03-17 17:27:19 +11001516 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 /*
1518 * Mark the former data entry unused.
1519 */
1520 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001521 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001522 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 /*
1524 * We just mark the leaf entry stale by putting a null in it.
1525 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001526 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001528 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1530 /*
1531 * Scan the freespace in the data block again if necessary,
1532 * log the data block header if necessary.
1533 */
1534 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001535 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (needlog)
1537 xfs_dir2_data_log_header(tp, dbp);
1538 /*
1539 * If the longest freespace in the data block has changed,
1540 * put the new value in the bests table and log that.
1541 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001542 if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1543 bestsp[db] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1545 }
1546 xfs_dir2_data_check(dp, dbp);
1547 /*
1548 * If the data block is now empty then get rid of the data block.
1549 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001550 if (be16_to_cpu(hdr->bestfree[0].length) ==
1551 mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 ASSERT(db != mp->m_dirdatablk);
1553 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1554 /*
1555 * Nope, can't get rid of it because it caused
1556 * allocation of a bmap btree block to do so.
1557 * Just go on, returning success, leaving the
1558 * empty block in place.
1559 */
1560 if (error == ENOSPC && args->total == 0) {
1561 xfs_da_buf_done(dbp);
1562 error = 0;
1563 }
1564 xfs_dir2_leaf_check(dp, lbp);
1565 xfs_da_buf_done(lbp);
1566 return error;
1567 }
1568 dbp = NULL;
1569 /*
1570 * If this is the last data block then compact the
1571 * bests table by getting rid of entries.
1572 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001573 if (db == be32_to_cpu(ltp->bestcount) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 /*
1575 * Look for the last active entry (i).
1576 */
1577 for (i = db - 1; i > 0; i--) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001578 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 break;
1580 }
1581 /*
1582 * Copy the table down so inactive entries at the
1583 * end are removed.
1584 */
1585 memmove(&bestsp[db - i], bestsp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001586 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001587 be32_add_cpu(&ltp->bestcount, -(db - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001589 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 } else
Nathan Scott68b3a102006-03-17 17:27:19 +11001591 bestsp[db] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593 /*
1594 * If the data block was not the first one, drop it.
1595 */
1596 else if (db != mp->m_dirdatablk && dbp != NULL) {
1597 xfs_da_buf_done(dbp);
1598 dbp = NULL;
1599 }
1600 xfs_dir2_leaf_check(dp, lbp);
1601 /*
1602 * See if we can convert to block form.
1603 */
1604 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1605}
1606
1607/*
1608 * Replace the inode number in a leaf format directory entry.
1609 */
1610int /* error */
1611xfs_dir2_leaf_replace(
1612 xfs_da_args_t *args) /* operation arguments */
1613{
1614 xfs_dabuf_t *dbp; /* data block buffer */
1615 xfs_dir2_data_entry_t *dep; /* data block entry */
1616 xfs_inode_t *dp; /* incore directory inode */
1617 int error; /* error return code */
1618 int index; /* index of leaf entry */
1619 xfs_dabuf_t *lbp; /* leaf buffer */
1620 xfs_dir2_leaf_t *leaf; /* leaf structure */
1621 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1622 xfs_trans_t *tp; /* transaction pointer */
1623
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001624 trace_xfs_dir2_leaf_replace(args);
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 /*
1627 * Look up the entry.
1628 */
1629 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1630 return error;
1631 }
1632 dp = args->dp;
1633 leaf = lbp->data;
1634 /*
1635 * Point to the leaf entry, get data address from it.
1636 */
1637 lep = &leaf->ents[index];
1638 /*
1639 * Point to the data entry.
1640 */
1641 dep = (xfs_dir2_data_entry_t *)
1642 ((char *)dbp->data +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001643 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001644 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 /*
1646 * Put the new inode number in, log it.
1647 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001648 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 tp = args->trans;
1650 xfs_dir2_data_log_entry(tp, dbp, dep);
1651 xfs_da_buf_done(dbp);
1652 xfs_dir2_leaf_check(dp, lbp);
1653 xfs_da_brelse(tp, lbp);
1654 return 0;
1655}
1656
1657/*
1658 * Return index in the leaf block (lbp) which is either the first
1659 * one with this hash value, or if there are none, the insert point
1660 * for that hash value.
1661 */
1662int /* index value */
1663xfs_dir2_leaf_search_hash(
1664 xfs_da_args_t *args, /* operation arguments */
1665 xfs_dabuf_t *lbp) /* leaf buffer */
1666{
1667 xfs_dahash_t hash=0; /* hash from this entry */
1668 xfs_dahash_t hashwant; /* hash value looking for */
1669 int high; /* high leaf index */
1670 int low; /* low leaf index */
1671 xfs_dir2_leaf_t *leaf; /* leaf structure */
1672 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1673 int mid=0; /* current leaf index */
1674
1675 leaf = lbp->data;
1676#ifndef __KERNEL__
1677 if (!leaf->hdr.count)
1678 return 0;
1679#endif
1680 /*
1681 * Note, the table cannot be empty, so we have to go through the loop.
1682 * Binary search the leaf entries looking for our hash value.
1683 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001684 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 hashwant = args->hashval;
1686 low <= high; ) {
1687 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001688 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 break;
1690 if (hash < hashwant)
1691 low = mid + 1;
1692 else
1693 high = mid - 1;
1694 }
1695 /*
1696 * Found one, back up through all the equal hash values.
1697 */
1698 if (hash == hashwant) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001699 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 mid--;
1701 }
1702 }
1703 /*
1704 * Need to point to an entry higher than ours.
1705 */
1706 else if (hash < hashwant)
1707 mid++;
1708 return mid;
1709}
1710
1711/*
1712 * Trim off a trailing data block. We know it's empty since the leaf
1713 * freespace table says so.
1714 */
1715int /* error */
1716xfs_dir2_leaf_trim_data(
1717 xfs_da_args_t *args, /* operation arguments */
1718 xfs_dabuf_t *lbp, /* leaf buffer */
1719 xfs_dir2_db_t db) /* data block number */
1720{
Nathan Scott68b3a102006-03-17 17:27:19 +11001721 __be16 *bestsp; /* leaf bests table */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 xfs_dabuf_t *dbp; /* data block buffer */
1723 xfs_inode_t *dp; /* incore directory inode */
1724 int error; /* error return value */
1725 xfs_dir2_leaf_t *leaf; /* leaf structure */
1726 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1727 xfs_mount_t *mp; /* filesystem mount point */
1728 xfs_trans_t *tp; /* transaction pointer */
1729
1730 dp = args->dp;
1731 mp = dp->i_mount;
1732 tp = args->trans;
1733 /*
1734 * Read the offending data block. We need its buffer.
1735 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001736 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 XFS_DATA_FORK))) {
1738 return error;
1739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 leaf = lbp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001742 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001743
1744#ifdef DEBUG
1745{
1746 struct xfs_dir2_data_hdr *hdr = dbp->data;
1747
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001748 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001749 ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
1750 mp->m_dirblksize - (uint)sizeof(*hdr));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001751 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001752}
1753#endif
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 /*
1756 * Get rid of the data block.
1757 */
1758 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1759 ASSERT(error != ENOSPC);
1760 xfs_da_brelse(tp, dbp);
1761 return error;
1762 }
1763 /*
1764 * Eliminate the last bests entry from the table.
1765 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001766 bestsp = xfs_dir2_leaf_bests_p(ltp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001767 be32_add_cpu(&ltp->bestcount, -1);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001768 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001770 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return 0;
1772}
1773
Christoph Hellwig22823962011-07-08 14:35:53 +02001774static inline size_t
1775xfs_dir2_leaf_size(
1776 struct xfs_dir2_leaf_hdr *hdr,
1777 int counts)
1778{
1779 int entries;
1780
1781 entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
1782 return sizeof(xfs_dir2_leaf_hdr_t) +
1783 entries * sizeof(xfs_dir2_leaf_entry_t) +
1784 counts * sizeof(xfs_dir2_data_off_t) +
1785 sizeof(xfs_dir2_leaf_tail_t);
1786}
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788/*
1789 * Convert node form directory to leaf form directory.
1790 * The root of the node form dir needs to already be a LEAFN block.
1791 * Just return if we can't do anything.
1792 */
1793int /* error */
1794xfs_dir2_node_to_leaf(
1795 xfs_da_state_t *state) /* directory operation state */
1796{
1797 xfs_da_args_t *args; /* operation arguments */
1798 xfs_inode_t *dp; /* incore directory inode */
1799 int error; /* error return code */
1800 xfs_dabuf_t *fbp; /* buffer for freespace block */
1801 xfs_fileoff_t fo; /* freespace file offset */
1802 xfs_dir2_free_t *free; /* freespace structure */
1803 xfs_dabuf_t *lbp; /* buffer for leaf block */
1804 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1805 xfs_dir2_leaf_t *leaf; /* leaf structure */
1806 xfs_mount_t *mp; /* filesystem mount point */
1807 int rval; /* successful free trim? */
1808 xfs_trans_t *tp; /* transaction pointer */
1809
1810 /*
1811 * There's more than a leaf level in the btree, so there must
1812 * be multiple leafn blocks. Give up.
1813 */
1814 if (state->path.active > 1)
1815 return 0;
1816 args = state->args;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001817
1818 trace_xfs_dir2_node_to_leaf(args);
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 mp = state->mp;
1821 dp = args->dp;
1822 tp = args->trans;
1823 /*
1824 * Get the last offset in the file.
1825 */
1826 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1827 return error;
1828 }
1829 fo -= mp->m_dirblkfsbs;
1830 /*
1831 * If there are freespace blocks other than the first one,
1832 * take this opportunity to remove trailing empty freespace blocks
1833 * that may have been left behind during no-space-reservation
1834 * operations.
1835 */
1836 while (fo > mp->m_dirfreeblk) {
1837 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1838 return error;
1839 }
1840 if (rval)
1841 fo -= mp->m_dirblkfsbs;
1842 else
1843 return 0;
1844 }
1845 /*
1846 * Now find the block just before the freespace block.
1847 */
1848 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1849 return error;
1850 }
1851 /*
1852 * If it's not the single leaf block, give up.
1853 */
1854 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1855 return 0;
1856 lbp = state->path.blk[0].bp;
1857 leaf = lbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001858 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 /*
1860 * Read the freespace block.
1861 */
1862 if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
1863 XFS_DATA_FORK))) {
1864 return error;
1865 }
1866 free = fbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001867 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 ASSERT(!free->hdr.firstdb);
Christoph Hellwig22823962011-07-08 14:35:53 +02001869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /*
1871 * Now see if the leafn and free data will fit in a leaf1.
1872 * If not, release the buffer and give up.
1873 */
Christoph Hellwig22823962011-07-08 14:35:53 +02001874 if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
1875 mp->m_dirblksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 xfs_da_brelse(tp, fbp);
1877 return 0;
1878 }
Christoph Hellwig22823962011-07-08 14:35:53 +02001879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 /*
1881 * If the leaf has any stale entries in it, compress them out.
1882 * The compact routine will log the header.
1883 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001884 if (be16_to_cpu(leaf->hdr.stale))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 xfs_dir2_leaf_compact(args, lbp);
1886 else
1887 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott89da0542006-03-17 17:28:40 +11001888 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 /*
1890 * Set up the leaf tail from the freespace block.
1891 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001892 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scott0ba962e2006-03-17 17:27:07 +11001893 ltp->bestcount = free->hdr.nvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 /*
1895 * Set up the leaf bests table.
1896 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001897 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
Christoph Hellwig22823962011-07-08 14:35:53 +02001898 be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001899 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 xfs_dir2_leaf_log_tail(tp, lbp);
1901 xfs_dir2_leaf_check(dp, lbp);
1902 /*
1903 * Get rid of the freespace block.
1904 */
1905 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1906 if (error) {
1907 /*
1908 * This can't fail here because it can only happen when
1909 * punching out the middle of an extent, and this is an
1910 * isolated block.
1911 */
1912 ASSERT(error != ENOSPC);
1913 return error;
1914 }
1915 fbp = NULL;
1916 /*
1917 * Now see if we can convert the single-leaf directory
1918 * down to a block form directory.
1919 * This routine always kills the dabuf for the leaf, so
1920 * eliminate it from the path.
1921 */
1922 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1923 state->path.blk[0].bp = NULL;
1924 return error;
1925}