blob: 487036d3751bd782163d0112f57d2dc789cdf8b1 [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 Hellwig4fb44c82011-07-08 14:34:59 +0200151struct xfs_dir2_leaf_entry *
152xfs_dir2_leaf_find_entry(
153 xfs_dir2_leaf_t *leaf, /* leaf structure */
154 int index, /* leaf table position */
155 int compact, /* need to compact leaves */
156 int lowstale, /* index of prev stale leaf */
157 int highstale, /* index of next stale leaf */
158 int *lfloglow, /* low leaf logging index */
159 int *lfloghigh) /* high leaf logging index */
160{
161 if (!leaf->hdr.stale) {
162 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
163
164 /*
165 * Now we need to make room to insert the leaf entry.
166 *
167 * If there are no stale entries, just insert a hole at index.
168 */
169 lep = &leaf->ents[index];
170 if (index < be16_to_cpu(leaf->hdr.count))
171 memmove(lep + 1, lep,
172 (be16_to_cpu(leaf->hdr.count) - index) *
173 sizeof(*lep));
174
175 /*
176 * Record low and high logging indices for the leaf.
177 */
178 *lfloglow = index;
179 *lfloghigh = be16_to_cpu(leaf->hdr.count);
180 be16_add_cpu(&leaf->hdr.count, 1);
181 return lep;
182 }
183
184 /*
185 * There are stale entries.
186 *
187 * We will use one of them for the new entry. It's probably not at
188 * the right location, so we'll have to shift some up or down first.
189 *
190 * If we didn't compact before, we need to find the nearest stale
191 * entries before and after our insertion point.
192 */
193 if (compact == 0) {
194 /*
195 * Find the first stale entry before the insertion point,
196 * if any.
197 */
198 for (lowstale = index - 1;
199 lowstale >= 0 &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200200 leaf->ents[lowstale].address !=
201 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200202 lowstale--)
203 continue;
204
205 /*
206 * Find the next stale entry at or after the insertion point,
207 * if any. Stop if we go so far that the lowstale entry
208 * would be better.
209 */
210 for (highstale = index;
211 highstale < be16_to_cpu(leaf->hdr.count) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200212 leaf->ents[highstale].address !=
213 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200214 (lowstale < 0 ||
215 index - lowstale - 1 >= highstale - index);
216 highstale++)
217 continue;
218 }
219
220 /*
221 * If the low one is better, use it.
222 */
223 if (lowstale >= 0 &&
224 (highstale == be16_to_cpu(leaf->hdr.count) ||
225 index - lowstale - 1 < highstale - index)) {
226 ASSERT(index - lowstale - 1 >= 0);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200227 ASSERT(leaf->ents[lowstale].address ==
228 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200229
230 /*
231 * Copy entries up to cover the stale entry and make room
232 * for the new entry.
233 */
234 if (index - lowstale - 1 > 0) {
235 memmove(&leaf->ents[lowstale],
236 &leaf->ents[lowstale + 1],
237 (index - lowstale - 1) *
238 sizeof(xfs_dir2_leaf_entry_t));
239 }
240 *lfloglow = MIN(lowstale, *lfloglow);
241 *lfloghigh = MAX(index - 1, *lfloghigh);
242 be16_add_cpu(&leaf->hdr.stale, -1);
243 return &leaf->ents[index - 1];
244 }
245
246 /*
247 * The high one is better, so use that one.
248 */
249 ASSERT(highstale - index >= 0);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200250 ASSERT(leaf->ents[highstale].address ==
251 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200252
253 /*
254 * Copy entries down to cover the stale entry and make room for the
255 * new entry.
256 */
257 if (highstale - index > 0) {
258 memmove(&leaf->ents[index + 1],
259 &leaf->ents[index],
260 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
261 }
262 *lfloglow = MIN(index, *lfloglow);
263 *lfloghigh = MAX(highstale, *lfloghigh);
264 be16_add_cpu(&leaf->hdr.stale, -1);
265 return &leaf->ents[index];
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/*
269 * Add an entry to a leaf form directory.
270 */
271int /* error */
272xfs_dir2_leaf_addname(
273 xfs_da_args_t *args) /* operation arguments */
274{
Nathan Scott68b3a102006-03-17 17:27:19 +1100275 __be16 *bestsp; /* freespace table in leaf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 int compact; /* need to compact leaves */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200277 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 xfs_dabuf_t *dbp; /* data block buffer */
279 xfs_dir2_data_entry_t *dep; /* data block entry */
280 xfs_inode_t *dp; /* incore directory inode */
281 xfs_dir2_data_unused_t *dup; /* data unused entry */
282 int error; /* error return value */
283 int grown; /* allocated new data block */
284 int highstale; /* index of next stale leaf */
285 int i; /* temporary, index */
286 int index; /* leaf table position */
287 xfs_dabuf_t *lbp; /* leaf's buffer */
288 xfs_dir2_leaf_t *leaf; /* leaf structure */
289 int length; /* length of new entry */
290 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
291 int lfloglow; /* low leaf logging index */
292 int lfloghigh; /* high leaf logging index */
293 int lowstale; /* index of prev stale leaf */
294 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
295 xfs_mount_t *mp; /* filesystem mount point */
296 int needbytes; /* leaf block bytes needed */
297 int needlog; /* need to log data header */
298 int needscan; /* need to rescan data free */
Nathan Scott3d693c62006-03-17 17:28:27 +1100299 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 xfs_trans_t *tp; /* transaction pointer */
301 xfs_dir2_db_t use_block; /* data block number */
302
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000303 trace_xfs_dir2_leaf_addname(args);
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 dp = args->dp;
306 tp = args->trans;
307 mp = dp->i_mount;
308 /*
309 * Read the leaf block.
310 */
311 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
312 XFS_DATA_FORK);
313 if (error) {
314 return error;
315 }
316 ASSERT(lbp != NULL);
317 /*
318 * Look up the entry by hash value and name.
319 * We know it's not there, our caller has already done a lookup.
320 * So the index is of the entry to insert in front of.
321 * But if there are dup hash values the index is of the first of those.
322 */
323 index = xfs_dir2_leaf_search_hash(args, lbp);
324 leaf = lbp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000325 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
326 bestsp = xfs_dir2_leaf_bests_p(ltp);
327 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /*
329 * See if there are any entries with the same hash value
330 * and space in their block for the new entry.
331 * This is good because it puts multiple same-hash value entries
332 * in a data block, improving the lookup of those entries.
333 */
334 for (use_block = -1, lep = &leaf->ents[index];
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100335 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 index++, lep++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100337 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 continue;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000339 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100340 ASSERT(i < be32_to_cpu(ltp->bestcount));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200341 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
Nathan Scott68b3a102006-03-17 17:27:19 +1100342 if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 use_block = i;
344 break;
345 }
346 }
347 /*
348 * Didn't find a block yet, linear search all the data blocks.
349 */
350 if (use_block == -1) {
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100351 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /*
353 * Remember a block we see that's missing.
354 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200355 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
356 use_block == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 use_block = i;
Nathan Scott68b3a102006-03-17 17:27:19 +1100358 else if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 use_block = i;
360 break;
361 }
362 }
363 }
364 /*
365 * How many bytes do we need in the leaf block?
366 */
Christoph Hellwig22823962011-07-08 14:35:53 +0200367 needbytes = 0;
368 if (!leaf->hdr.stale)
369 needbytes += sizeof(xfs_dir2_leaf_entry_t);
370 if (use_block == -1)
371 needbytes += sizeof(xfs_dir2_data_off_t);
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /*
374 * Now kill use_block if it refers to a missing block, so we
375 * can use it as an indication of allocation needed.
376 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200377 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 use_block = -1;
379 /*
380 * If we don't have enough free bytes but we can make enough
381 * by compacting out stale entries, we'll do that.
382 */
Barry Naujok6a178102008-05-21 16:42:05 +1000383 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
384 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 compact = 1;
386 }
387 /*
388 * Otherwise if we don't have enough free bytes we need to
389 * convert to node form.
390 */
Barry Naujok6a178102008-05-21 16:42:05 +1000391 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
392 leaf->hdr.count)] < needbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /*
394 * Just checking or no space reservation, give up.
395 */
Barry Naujok6a178102008-05-21 16:42:05 +1000396 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
397 args->total == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 xfs_da_brelse(tp, lbp);
399 return XFS_ERROR(ENOSPC);
400 }
401 /*
402 * Convert to node form.
403 */
404 error = xfs_dir2_leaf_to_node(args, lbp);
405 xfs_da_buf_done(lbp);
406 if (error)
407 return error;
408 /*
409 * Then add the new entry.
410 */
411 return xfs_dir2_node_addname(args);
412 }
413 /*
414 * Otherwise it will fit without compaction.
415 */
416 else
417 compact = 0;
418 /*
419 * If just checking, then it will fit unless we needed to allocate
420 * a new data block.
421 */
Barry Naujok6a178102008-05-21 16:42:05 +1000422 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 xfs_da_brelse(tp, lbp);
424 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
425 }
426 /*
427 * If no allocations are allowed, return now before we've
428 * changed anything.
429 */
430 if (args->total == 0 && use_block == -1) {
431 xfs_da_brelse(tp, lbp);
432 return XFS_ERROR(ENOSPC);
433 }
434 /*
435 * Need to compact the leaf entries, removing stale ones.
436 * Leave one stale entry behind - the one closest to our
437 * insertion index - and we'll shift that one to our insertion
438 * point later.
439 */
440 if (compact) {
441 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
442 &lfloglow, &lfloghigh);
443 }
444 /*
445 * There are stale entries, so we'll need log-low and log-high
446 * impossibly bad values later.
447 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100448 else if (be16_to_cpu(leaf->hdr.stale)) {
449 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 lfloghigh = -1;
451 }
452 /*
453 * If there was no data block space found, we need to allocate
454 * a new one.
455 */
456 if (use_block == -1) {
457 /*
458 * Add the new data block.
459 */
460 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
461 &use_block))) {
462 xfs_da_brelse(tp, lbp);
463 return error;
464 }
465 /*
466 * Initialize the block.
467 */
468 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
469 xfs_da_brelse(tp, lbp);
470 return error;
471 }
472 /*
473 * If we're adding a new data block on the end we need to
474 * extend the bests table. Copy it up one entry.
475 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100476 if (use_block >= be32_to_cpu(ltp->bestcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 bestsp--;
478 memmove(&bestsp[0], &bestsp[1],
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100479 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800480 be32_add_cpu(&ltp->bestcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100482 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 /*
485 * If we're filling in a previously empty block just log it.
486 */
487 else
488 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200489 hdr = dbp->data;
490 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 grown = 1;
492 }
493 /*
494 * Already had space in some data block.
495 * Just read that one in.
496 */
497 else {
498 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000499 xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 -1, &dbp, XFS_DATA_FORK))) {
501 xfs_da_brelse(tp, lbp);
502 return error;
503 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200504 hdr = dbp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 grown = 0;
506 }
507 xfs_dir2_data_check(dp, dbp);
508 /*
509 * Point to the biggest freespace in our data block.
510 */
511 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200512 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
Nathan Scottad354eb2006-03-17 17:27:37 +1100513 ASSERT(be16_to_cpu(dup->length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 needscan = needlog = 0;
515 /*
516 * Mark the initial part of our freespace in use for the new entry.
517 */
518 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200519 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 &needlog, &needscan);
521 /*
522 * Initialize our new entry (at last).
523 */
524 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000525 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 dep->namelen = args->namelen;
527 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000528 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200529 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /*
531 * Need to scan fix up the bestfree table.
532 */
533 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200534 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /*
536 * Need to log the data block's header.
537 */
538 if (needlog)
539 xfs_dir2_data_log_header(tp, dbp);
540 xfs_dir2_data_log_entry(tp, dbp, dep);
541 /*
542 * If the bests table needs to be changed, do it.
543 * Log the change unless we've already done that.
544 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200545 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
546 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (!grown)
548 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
549 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200550
551 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
552 highstale, &lfloglow, &lfloghigh);
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /*
555 * Fill in the new leaf entry.
556 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100557 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000558 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
Nathan Scott3d693c62006-03-17 17:28:27 +1100559 be16_to_cpu(*tagp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /*
561 * Log the leaf fields and give up the buffers.
562 */
563 xfs_dir2_leaf_log_header(tp, lbp);
564 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
565 xfs_dir2_leaf_check(dp, lbp);
566 xfs_da_buf_done(lbp);
567 xfs_dir2_data_check(dp, dbp);
568 xfs_da_buf_done(dbp);
569 return 0;
570}
571
572#ifdef DEBUG
573/*
574 * Check the internal consistency of a leaf1 block.
575 * Pop an assert if something is wrong.
576 */
Hannes Eder3180e662009-03-04 19:34:10 +0100577STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578xfs_dir2_leaf_check(
579 xfs_inode_t *dp, /* incore directory inode */
580 xfs_dabuf_t *bp) /* leaf's buffer */
581{
582 int i; /* leaf index */
583 xfs_dir2_leaf_t *leaf; /* leaf structure */
584 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
585 xfs_mount_t *mp; /* filesystem mount point */
586 int stale; /* count of stale leaves */
587
588 leaf = bp->data;
589 mp = dp->i_mount;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200590 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /*
592 * This value is not restrictive enough.
593 * Should factor in the size of the bests table as well.
594 * We can deduce a value for that from di_size.
595 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000596 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
597 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
599 * Leaves and bests don't overlap.
600 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100601 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000602 (char *)xfs_dir2_leaf_bests_p(ltp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /*
604 * Check hash value order, count stale entries.
605 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100606 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
607 if (i + 1 < be16_to_cpu(leaf->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100608 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
609 be32_to_cpu(leaf->ents[i + 1].hashval));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200610 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 stale++;
612 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100613 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615#endif /* DEBUG */
616
617/*
618 * Compact out any stale entries in the leaf.
619 * Log the header and changed leaf entries, if any.
620 */
621void
622xfs_dir2_leaf_compact(
623 xfs_da_args_t *args, /* operation arguments */
624 xfs_dabuf_t *bp) /* leaf buffer */
625{
626 int from; /* source leaf index */
627 xfs_dir2_leaf_t *leaf; /* leaf structure */
628 int loglow; /* first leaf entry to log */
629 int to; /* target leaf index */
630
631 leaf = bp->data;
632 if (!leaf->hdr.stale) {
633 return;
634 }
635 /*
636 * Compress out the stale entries in place.
637 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100638 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200639 if (leaf->ents[from].address ==
640 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 continue;
642 /*
643 * Only actually copy the entries that are different.
644 */
645 if (from > to) {
646 if (loglow == -1)
647 loglow = to;
648 leaf->ents[to] = leaf->ents[from];
649 }
650 to++;
651 }
652 /*
653 * Update and log the header, log the leaf entries.
654 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100655 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800656 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 leaf->hdr.stale = 0;
658 xfs_dir2_leaf_log_header(args->trans, bp);
659 if (loglow != -1)
660 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
661}
662
663/*
664 * Compact the leaf entries, removing stale ones.
665 * Leave one stale entry behind - the one closest to our
666 * insertion index - and the caller will shift that one to our insertion
667 * point later.
668 * Return new insertion index, where the remaining stale entry is,
669 * and leaf logging indices.
670 */
671void
672xfs_dir2_leaf_compact_x1(
673 xfs_dabuf_t *bp, /* leaf buffer */
674 int *indexp, /* insertion index */
675 int *lowstalep, /* out: stale entry before us */
676 int *highstalep, /* out: stale entry after us */
677 int *lowlogp, /* out: low log index */
678 int *highlogp) /* out: high log index */
679{
680 int from; /* source copy index */
681 int highstale; /* stale entry at/after index */
682 int index; /* insertion index */
683 int keepstale; /* source index of kept stale */
684 xfs_dir2_leaf_t *leaf; /* leaf structure */
685 int lowstale; /* stale entry before index */
686 int newindex=0; /* new insertion index */
687 int to; /* destination copy index */
688
689 leaf = bp->data;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100690 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 index = *indexp;
692 /*
693 * Find the first stale entry before our index, if any.
694 */
695 for (lowstale = index - 1;
696 lowstale >= 0 &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200697 leaf->ents[lowstale].address !=
698 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 lowstale--)
700 continue;
701 /*
702 * Find the first stale entry at or after our index, if any.
703 * Stop if the answer would be worse than lowstale.
704 */
705 for (highstale = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100706 highstale < be16_to_cpu(leaf->hdr.count) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200707 leaf->ents[highstale].address !=
708 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 (lowstale < 0 || index - lowstale > highstale - index);
710 highstale++)
711 continue;
712 /*
713 * Pick the better of lowstale and highstale.
714 */
715 if (lowstale >= 0 &&
Nathan Scotta818e5d2006-03-17 17:28:07 +1100716 (highstale == be16_to_cpu(leaf->hdr.count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 index - lowstale <= highstale - index))
718 keepstale = lowstale;
719 else
720 keepstale = highstale;
721 /*
722 * Copy the entries in place, removing all the stale entries
723 * except keepstale.
724 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100725 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /*
727 * Notice the new value of index.
728 */
729 if (index == from)
730 newindex = to;
731 if (from != keepstale &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200732 leaf->ents[from].address ==
733 cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (from == to)
735 *lowlogp = to;
736 continue;
737 }
738 /*
739 * Record the new keepstale value for the insertion.
740 */
741 if (from == keepstale)
742 lowstale = highstale = to;
743 /*
744 * Copy only the entries that have moved.
745 */
746 if (from > to)
747 leaf->ents[to] = leaf->ents[from];
748 to++;
749 }
750 ASSERT(from > to);
751 /*
752 * If the insertion point was past the last entry,
753 * set the new insertion point accordingly.
754 */
755 if (index == from)
756 newindex = to;
757 *indexp = newindex;
758 /*
759 * Adjust the leaf header values.
760 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800761 be16_add_cpu(&leaf->hdr.count, -(from - to));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100762 leaf->hdr.stale = cpu_to_be16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /*
764 * Remember the low/high stale value only in the "right"
765 * direction.
766 */
767 if (lowstale >= newindex)
768 lowstale = -1;
769 else
Nathan Scotta818e5d2006-03-17 17:28:07 +1100770 highstale = be16_to_cpu(leaf->hdr.count);
771 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 *lowstalep = lowstale;
773 *highstalep = highstale;
774}
775
776/*
777 * Getdents (readdir) for leaf and node directories.
778 * This reads the data blocks only, so is the same for both forms.
779 */
780int /* error */
781xfs_dir2_leaf_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 xfs_inode_t *dp, /* incore directory inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000783 void *dirent,
784 size_t bufsize,
785 xfs_off_t *offset,
786 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 xfs_dabuf_t *bp; /* data block buffer */
789 int byteoff; /* offset in current block */
790 xfs_dir2_db_t curdb; /* db for current block */
791 xfs_dir2_off_t curoff; /* current overall offset */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200792 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 xfs_dir2_data_entry_t *dep; /* data entry */
794 xfs_dir2_data_unused_t *dup; /* unused entry */
Nathan Scottf6d75cb2006-03-14 13:32:24 +1100795 int error = 0; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 int i; /* temporary loop index */
797 int j; /* temporary loop index */
798 int length; /* temporary length value */
799 xfs_bmbt_irec_t *map; /* map vector for blocks */
800 xfs_extlen_t map_blocks; /* number of fsbs in map */
801 xfs_dablk_t map_off; /* last mapped file offset */
802 int map_size; /* total entries in *map */
803 int map_valid; /* valid entries in *map */
804 xfs_mount_t *mp; /* filesystem mount point */
805 xfs_dir2_off_t newoff; /* new curoff after new blk */
806 int nmap; /* mappings to ask xfs_bmapi */
Nathan Scottf6d75cb2006-03-14 13:32:24 +1100807 char *ptr = NULL; /* pointer to current data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int ra_current; /* number of read-ahead blks */
809 int ra_index; /* *map index for read-ahead */
810 int ra_offset; /* map entry offset for ra */
811 int ra_want; /* readahead count wanted */
812
813 /*
814 * If the offset is at or past the largest allowed value,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000815 * give up right away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000817 if (*offset >= XFS_DIR2_MAX_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return 0;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 mp = dp->i_mount;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /*
823 * Set up to bmap a number of blocks based on the caller's
824 * buffer size, the directory block size, and the filesystem
825 * block size.
826 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000827 map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
829 map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
830 bp = NULL;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /*
833 * Inside the loop we keep the main offset value as a byte offset
834 * in the directory file.
835 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000836 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /*
839 * Force this conversion through db so we truncate the offset
840 * down to get the start of the data block.
841 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000842 map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /*
844 * Loop over directory entries until we reach the end offset.
845 * Get more blocks and readahead as necessary.
846 */
847 while (curoff < XFS_DIR2_LEAF_OFFSET) {
848 /*
849 * If we have no buffer, or we're off the end of the
850 * current buffer, need to get another one.
851 */
852 if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) {
853 /*
854 * If we have a buffer, we need to release it and
855 * take it out of the mapping.
856 */
857 if (bp) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000858 xfs_da_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 bp = NULL;
860 map_blocks -= mp->m_dirblkfsbs;
861 /*
862 * Loop to get rid of the extents for the
863 * directory block.
864 */
865 for (i = mp->m_dirblkfsbs; i > 0; ) {
866 j = MIN((int)map->br_blockcount, i);
867 map->br_blockcount -= j;
868 map->br_startblock += j;
869 map->br_startoff += j;
870 /*
871 * If mapping is done, pitch it from
872 * the table.
873 */
874 if (!map->br_blockcount && --map_valid)
875 memmove(&map[0], &map[1],
876 sizeof(map[0]) *
877 map_valid);
878 i -= j;
879 }
880 }
881 /*
882 * Recalculate the readahead blocks wanted.
883 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000884 ra_want = howmany(bufsize + mp->m_dirblksize,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 mp->m_sb.sb_blocksize) - 1;
Eric Sandeen8e69ce12009-09-25 19:42:26 +0000886 ASSERT(ra_want >= 0);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /*
889 * If we don't have as many as we want, and we haven't
890 * run out of data blocks, get some more mappings.
891 */
892 if (1 + ra_want > map_blocks &&
893 map_off <
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000894 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /*
896 * Get more bmaps, fill in after the ones
897 * we already have in the table.
898 */
899 nmap = map_size - map_valid;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000900 error = xfs_bmapi(NULL, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 map_off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000902 xfs_dir2_byte_to_da(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 XFS_DIR2_LEAF_OFFSET) - map_off,
904 XFS_BMAPI_METADATA, NULL, 0,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000905 &map[map_valid], &nmap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /*
907 * Don't know if we should ignore this or
908 * try to return an error.
909 * The trouble with returning errors
910 * is that readdir will just stop without
911 * actually passing the error through.
912 */
913 if (error)
914 break; /* XXX */
915 /*
916 * If we got all the mappings we asked for,
917 * set the final map offset based on the
918 * last bmap value received.
919 * Otherwise, we've reached the end.
920 */
921 if (nmap == map_size - map_valid)
922 map_off =
923 map[map_valid + nmap - 1].br_startoff +
924 map[map_valid + nmap - 1].br_blockcount;
925 else
926 map_off =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000927 xfs_dir2_byte_to_da(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 XFS_DIR2_LEAF_OFFSET);
929 /*
930 * Look for holes in the mapping, and
931 * eliminate them. Count up the valid blocks.
932 */
933 for (i = map_valid; i < map_valid + nmap; ) {
934 if (map[i].br_startblock ==
935 HOLESTARTBLOCK) {
936 nmap--;
937 length = map_valid + nmap - i;
938 if (length)
939 memmove(&map[i],
940 &map[i + 1],
941 sizeof(map[i]) *
942 length);
943 } else {
944 map_blocks +=
945 map[i].br_blockcount;
946 i++;
947 }
948 }
949 map_valid += nmap;
950 }
951 /*
952 * No valid mappings, so no more data blocks.
953 */
954 if (!map_valid) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000955 curoff = xfs_dir2_da_to_byte(mp, map_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 break;
957 }
958 /*
959 * Read the directory block starting at the first
960 * mapping.
961 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000962 curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000963 error = xfs_da_read_buf(NULL, dp, map->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 map->br_blockcount >= mp->m_dirblkfsbs ?
965 XFS_FSB_TO_DADDR(mp, map->br_startblock) :
966 -1,
967 &bp, XFS_DATA_FORK);
968 /*
969 * Should just skip over the data block instead
970 * of giving up.
971 */
972 if (error)
973 break; /* XXX */
974 /*
975 * Adjust the current amount of read-ahead: we just
976 * read a block that was previously ra.
977 */
978 if (ra_current)
979 ra_current -= mp->m_dirblkfsbs;
980 /*
981 * Do we need more readahead?
982 */
983 for (ra_index = ra_offset = i = 0;
984 ra_want > ra_current && i < map_blocks;
985 i += mp->m_dirblkfsbs) {
986 ASSERT(ra_index < map_valid);
987 /*
988 * Read-ahead a contiguous directory block.
989 */
990 if (i > ra_current &&
991 map[ra_index].br_blockcount >=
992 mp->m_dirblkfsbs) {
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000993 xfs_buf_readahead(mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 XFS_FSB_TO_DADDR(mp,
995 map[ra_index].br_startblock +
996 ra_offset),
997 (int)BTOBB(mp->m_dirblksize));
998 ra_current = i;
999 }
1000 /*
1001 * Read-ahead a non-contiguous directory block.
1002 * This doesn't use our mapping, but this
1003 * is a very rare case.
1004 */
1005 else if (i > ra_current) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001006 (void)xfs_da_reada_buf(NULL, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 map[ra_index].br_startoff +
1008 ra_offset, XFS_DATA_FORK);
1009 ra_current = i;
1010 }
1011 /*
1012 * Advance offset through the mapping table.
1013 */
1014 for (j = 0; j < mp->m_dirblkfsbs; j++) {
1015 /*
1016 * The rest of this extent but not
1017 * more than a dir block.
1018 */
1019 length = MIN(mp->m_dirblkfsbs,
1020 (int)(map[ra_index].br_blockcount -
1021 ra_offset));
1022 j += length;
1023 ra_offset += length;
1024 /*
1025 * Advance to the next mapping if
1026 * this one is used up.
1027 */
1028 if (ra_offset ==
1029 map[ra_index].br_blockcount) {
1030 ra_offset = 0;
1031 ra_index++;
1032 }
1033 }
1034 }
1035 /*
1036 * Having done a read, we need to set a new offset.
1037 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001038 newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /*
1040 * Start of the current block.
1041 */
1042 if (curoff < newoff)
1043 curoff = newoff;
1044 /*
1045 * Make sure we're in the right block.
1046 */
1047 else if (curoff > newoff)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001048 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 curdb);
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +02001050 hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 xfs_dir2_data_check(dp, bp);
1052 /*
1053 * Find our position in the block.
1054 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +02001055 ptr = (char *)(hdr + 1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001056 byteoff = xfs_dir2_byte_to_off(mp, curoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /*
1058 * Skip past the header.
1059 */
1060 if (byteoff == 0)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001061 curoff += (uint)sizeof(*hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /*
1063 * Skip past entries until we reach our offset.
1064 */
1065 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001066 while ((char *)ptr - (char *)hdr < byteoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 dup = (xfs_dir2_data_unused_t *)ptr;
1068
Nathan Scottad354eb2006-03-17 17:27:37 +11001069 if (be16_to_cpu(dup->freetag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 == XFS_DIR2_DATA_FREE_TAG) {
1071
Nathan Scottad354eb2006-03-17 17:27:37 +11001072 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ptr += length;
1074 continue;
1075 }
1076 dep = (xfs_dir2_data_entry_t *)ptr;
1077 length =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001078 xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ptr += length;
1080 }
1081 /*
1082 * Now set our real offset.
1083 */
1084 curoff =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001085 xfs_dir2_db_off_to_byte(mp,
1086 xfs_dir2_byte_to_db(mp, curoff),
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001087 (char *)ptr - (char *)hdr);
1088 if (ptr >= (char *)hdr + mp->m_dirblksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 continue;
1090 }
1091 }
1092 }
1093 /*
1094 * We have a pointer to an entry.
1095 * Is it a live one?
1096 */
1097 dup = (xfs_dir2_data_unused_t *)ptr;
1098 /*
1099 * No, it's unused, skip over it.
1100 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001101 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1102 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ptr += length;
1104 curoff += length;
1105 continue;
1106 }
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 dep = (xfs_dir2_data_entry_t *)ptr;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001109 length = xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Dave Chinner4a24cb72010-01-20 10:48:05 +11001111 if (filldir(dirent, (char *)dep->name, dep->namelen,
Christoph Hellwig15440312009-01-08 14:00:00 -05001112 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
Christoph Hellwiga19d9f82009-03-29 09:51:08 +02001113 be64_to_cpu(dep->inumber), DT_UNKNOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 break;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /*
1117 * Advance to next entry in the block.
1118 */
1119 ptr += length;
1120 curoff += length;
Eric Sandeen8e69ce12009-09-25 19:42:26 +00001121 /* bufsize may have just been a guess; don't go negative */
1122 bufsize = bufsize > length ? bufsize - length : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124
1125 /*
1126 * All done. Set output offset value to current offset.
1127 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001128 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
Christoph Hellwig15440312009-01-08 14:00:00 -05001129 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 else
Christoph Hellwig15440312009-01-08 14:00:00 -05001131 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001132 kmem_free(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (bp)
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001134 xfs_da_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return error;
1136}
1137
1138/*
1139 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1140 */
1141int
1142xfs_dir2_leaf_init(
1143 xfs_da_args_t *args, /* operation arguments */
1144 xfs_dir2_db_t bno, /* directory block number */
1145 xfs_dabuf_t **bpp, /* out: leaf buffer */
1146 int magic) /* magic number for block */
1147{
1148 xfs_dabuf_t *bp; /* leaf buffer */
1149 xfs_inode_t *dp; /* incore directory inode */
1150 int error; /* error return code */
1151 xfs_dir2_leaf_t *leaf; /* leaf structure */
1152 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1153 xfs_mount_t *mp; /* filesystem mount point */
1154 xfs_trans_t *tp; /* transaction pointer */
1155
1156 dp = args->dp;
1157 ASSERT(dp != NULL);
1158 tp = args->trans;
1159 mp = dp->i_mount;
1160 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1161 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1162 /*
1163 * Get the buffer for the block.
1164 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001165 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 XFS_DATA_FORK);
1167 if (error) {
1168 return error;
1169 }
1170 ASSERT(bp != NULL);
1171 leaf = bp->data;
1172 /*
1173 * Initialize the header.
1174 */
Nathan Scott89da0542006-03-17 17:28:40 +11001175 leaf->hdr.info.magic = cpu_to_be16(magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 leaf->hdr.info.forw = 0;
1177 leaf->hdr.info.back = 0;
1178 leaf->hdr.count = 0;
1179 leaf->hdr.stale = 0;
1180 xfs_dir2_leaf_log_header(tp, bp);
1181 /*
1182 * If it's a leaf-format directory initialize the tail.
1183 * In this case our caller has the real bests table to copy into
1184 * the block.
1185 */
1186 if (magic == XFS_DIR2_LEAF1_MAGIC) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001187 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 ltp->bestcount = 0;
1189 xfs_dir2_leaf_log_tail(tp, bp);
1190 }
1191 *bpp = bp;
1192 return 0;
1193}
1194
1195/*
1196 * Log the bests entries indicated from a leaf1 block.
1197 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001198static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199xfs_dir2_leaf_log_bests(
1200 xfs_trans_t *tp, /* transaction pointer */
1201 xfs_dabuf_t *bp, /* leaf buffer */
1202 int first, /* first entry to log */
1203 int last) /* last entry to log */
1204{
Nathan Scott68b3a102006-03-17 17:27:19 +11001205 __be16 *firstb; /* pointer to first entry */
1206 __be16 *lastb; /* pointer to last entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 xfs_dir2_leaf_t *leaf; /* leaf structure */
1208 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1209
1210 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001211 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001212 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1213 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1214 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1216 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1217}
1218
1219/*
1220 * Log the leaf entries indicated from a leaf1 or leafn block.
1221 */
1222void
1223xfs_dir2_leaf_log_ents(
1224 xfs_trans_t *tp, /* transaction pointer */
1225 xfs_dabuf_t *bp, /* leaf buffer */
1226 int first, /* first entry to log */
1227 int last) /* last entry to log */
1228{
1229 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1230 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1231 xfs_dir2_leaf_t *leaf; /* leaf structure */
1232
1233 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001234 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1235 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 firstlep = &leaf->ents[first];
1237 lastlep = &leaf->ents[last];
1238 xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1239 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1240}
1241
1242/*
1243 * Log the header of the leaf1 or leafn block.
1244 */
1245void
1246xfs_dir2_leaf_log_header(
1247 xfs_trans_t *tp, /* transaction pointer */
1248 xfs_dabuf_t *bp) /* leaf buffer */
1249{
1250 xfs_dir2_leaf_t *leaf; /* leaf structure */
1251
1252 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001253 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1254 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1256 (uint)(sizeof(leaf->hdr) - 1));
1257}
1258
1259/*
1260 * Log the tail of the leaf1 block.
1261 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001262STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263xfs_dir2_leaf_log_tail(
1264 xfs_trans_t *tp, /* transaction pointer */
1265 xfs_dabuf_t *bp) /* leaf buffer */
1266{
1267 xfs_dir2_leaf_t *leaf; /* leaf structure */
1268 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1269 xfs_mount_t *mp; /* filesystem mount point */
1270
1271 mp = tp->t_mountp;
1272 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001273 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001274 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1276 (uint)(mp->m_dirblksize - 1));
1277}
1278
1279/*
1280 * Look up the entry referred to by args in the leaf format directory.
1281 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1282 * is also used by the node-format code.
1283 */
1284int
1285xfs_dir2_leaf_lookup(
1286 xfs_da_args_t *args) /* operation arguments */
1287{
1288 xfs_dabuf_t *dbp; /* data block buffer */
1289 xfs_dir2_data_entry_t *dep; /* data block entry */
1290 xfs_inode_t *dp; /* incore directory inode */
1291 int error; /* error return code */
1292 int index; /* found entry index */
1293 xfs_dabuf_t *lbp; /* leaf buffer */
1294 xfs_dir2_leaf_t *leaf; /* leaf structure */
1295 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1296 xfs_trans_t *tp; /* transaction pointer */
1297
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001298 trace_xfs_dir2_leaf_lookup(args);
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 /*
1301 * Look up name in the leaf block, returning both buffers and index.
1302 */
1303 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1304 return error;
1305 }
1306 tp = args->trans;
1307 dp = args->dp;
1308 xfs_dir2_leaf_check(dp, lbp);
1309 leaf = lbp->data;
1310 /*
1311 * Get to the leaf entry and contained data entry address.
1312 */
1313 lep = &leaf->ents[index];
1314 /*
1315 * Point to the data entry.
1316 */
1317 dep = (xfs_dir2_data_entry_t *)
1318 ((char *)dbp->data +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001319 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +10001321 * Return the found inode number & CI name if appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001323 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001324 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 xfs_da_brelse(tp, dbp);
1326 xfs_da_brelse(tp, lbp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001327 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
1330/*
1331 * Look up name/hash in the leaf block.
1332 * Fill in indexp with the found index, and dbpp with the data buffer.
1333 * If not found dbpp will be NULL, and ENOENT comes back.
1334 * lbpp will always be filled in with the leaf buffer unless there's an error.
1335 */
1336static int /* error */
1337xfs_dir2_leaf_lookup_int(
1338 xfs_da_args_t *args, /* operation arguments */
1339 xfs_dabuf_t **lbpp, /* out: leaf buffer */
1340 int *indexp, /* out: index in leaf block */
1341 xfs_dabuf_t **dbpp) /* out: data buffer */
1342{
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001343 xfs_dir2_db_t curdb = -1; /* current data block number */
1344 xfs_dabuf_t *dbp = NULL; /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 xfs_dir2_data_entry_t *dep; /* data entry */
1346 xfs_inode_t *dp; /* incore directory inode */
1347 int error; /* error return code */
1348 int index; /* index in leaf block */
1349 xfs_dabuf_t *lbp; /* leaf buffer */
1350 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1351 xfs_dir2_leaf_t *leaf; /* leaf structure */
1352 xfs_mount_t *mp; /* filesystem mount point */
1353 xfs_dir2_db_t newdb; /* new data block number */
1354 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001355 xfs_dir2_db_t cidb = -1; /* case match data block no. */
Barry Naujok5163f952008-05-21 16:41:01 +10001356 enum xfs_dacmp cmp; /* name compare result */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 dp = args->dp;
1359 tp = args->trans;
1360 mp = dp->i_mount;
1361 /*
1362 * Read the leaf block into the buffer.
1363 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001364 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1365 XFS_DATA_FORK);
1366 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 *lbpp = lbp;
1369 leaf = lbp->data;
1370 xfs_dir2_leaf_check(dp, lbp);
1371 /*
1372 * Look for the first leaf entry with our hash value.
1373 */
1374 index = xfs_dir2_leaf_search_hash(args, lbp);
1375 /*
1376 * Loop over all the entries with the right hash value
1377 * looking to match the name.
1378 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001379 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
Barry Naujok5163f952008-05-21 16:41:01 +10001380 be32_to_cpu(lep->hashval) == args->hashval;
1381 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /*
1383 * Skip over stale leaf entries.
1384 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001385 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 continue;
1387 /*
1388 * Get the new data block number.
1389 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001390 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /*
1392 * If it's not the same as the old data block number,
1393 * need to pitch the old one and read the new one.
1394 */
1395 if (newdb != curdb) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001396 if (dbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 xfs_da_brelse(tp, dbp);
Barry Naujok5163f952008-05-21 16:41:01 +10001398 error = xfs_da_read_buf(tp, dp,
1399 xfs_dir2_db_to_da(mp, newdb),
1400 -1, &dbp, XFS_DATA_FORK);
1401 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 xfs_da_brelse(tp, lbp);
1403 return error;
1404 }
1405 xfs_dir2_data_check(dp, dbp);
1406 curdb = newdb;
1407 }
1408 /*
1409 * Point to the data entry.
1410 */
Barry Naujok5163f952008-05-21 16:41:01 +10001411 dep = (xfs_dir2_data_entry_t *)((char *)dbp->data +
1412 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 /*
Barry Naujok5163f952008-05-21 16:41:01 +10001414 * Compare name and if it's an exact match, return the index
1415 * and buffer. If it's the first case-insensitive match, store
1416 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 */
Barry Naujok5163f952008-05-21 16:41:01 +10001418 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1419 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1420 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 *indexp = index;
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001422 /* case exact match: return the current buffer. */
Barry Naujok5163f952008-05-21 16:41:01 +10001423 if (cmp == XFS_CMP_EXACT) {
Barry Naujok5163f952008-05-21 16:41:01 +10001424 *dbpp = dbp;
1425 return 0;
1426 }
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001427 cidb = curdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429 }
Barry Naujok6a178102008-05-21 16:42:05 +10001430 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +10001431 /*
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001432 * Here, we can only be doing a lookup (not a rename or remove).
1433 * If a case-insensitive match was found earlier, re-read the
1434 * appropriate data block if required and return it.
Barry Naujok5163f952008-05-21 16:41:01 +10001435 */
1436 if (args->cmpresult == XFS_CMP_CASE) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001437 ASSERT(cidb != -1);
1438 if (cidb != curdb) {
Barry Naujok5163f952008-05-21 16:41:01 +10001439 xfs_da_brelse(tp, dbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001440 error = xfs_da_read_buf(tp, dp,
1441 xfs_dir2_db_to_da(mp, cidb),
1442 -1, &dbp, XFS_DATA_FORK);
1443 if (error) {
1444 xfs_da_brelse(tp, lbp);
1445 return error;
1446 }
1447 }
1448 *dbpp = dbp;
Barry Naujok5163f952008-05-21 16:41:01 +10001449 return 0;
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 /*
1452 * No match found, return ENOENT.
1453 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001454 ASSERT(cidb == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (dbp)
1456 xfs_da_brelse(tp, dbp);
1457 xfs_da_brelse(tp, lbp);
1458 return XFS_ERROR(ENOENT);
1459}
1460
1461/*
1462 * Remove an entry from a leaf format directory.
1463 */
1464int /* error */
1465xfs_dir2_leaf_removename(
1466 xfs_da_args_t *args) /* operation arguments */
1467{
Nathan Scott68b3a102006-03-17 17:27:19 +11001468 __be16 *bestsp; /* leaf block best freespace */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001469 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 xfs_dir2_db_t db; /* data block number */
1471 xfs_dabuf_t *dbp; /* data block buffer */
1472 xfs_dir2_data_entry_t *dep; /* data entry structure */
1473 xfs_inode_t *dp; /* incore directory inode */
1474 int error; /* error return code */
1475 xfs_dir2_db_t i; /* temporary data block # */
1476 int index; /* index into leaf entries */
1477 xfs_dabuf_t *lbp; /* leaf buffer */
1478 xfs_dir2_leaf_t *leaf; /* leaf structure */
1479 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1480 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1481 xfs_mount_t *mp; /* filesystem mount point */
1482 int needlog; /* need to log data header */
1483 int needscan; /* need to rescan data frees */
1484 xfs_dir2_data_off_t oldbest; /* old value of best free */
1485 xfs_trans_t *tp; /* transaction pointer */
1486
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001487 trace_xfs_dir2_leaf_removename(args);
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 /*
1490 * Lookup the leaf entry, get the leaf and data blocks read in.
1491 */
1492 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1493 return error;
1494 }
1495 dp = args->dp;
1496 tp = args->trans;
1497 mp = dp->i_mount;
1498 leaf = lbp->data;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001499 hdr = dbp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 xfs_dir2_data_check(dp, dbp);
1501 /*
1502 * Point to the leaf entry, use that to point to the data entry.
1503 */
1504 lep = &leaf->ents[index];
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001505 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001507 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 needscan = needlog = 0;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001509 oldbest = be16_to_cpu(hdr->bestfree[0].length);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001510 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1511 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott68b3a102006-03-17 17:27:19 +11001512 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 /*
1514 * Mark the former data entry unused.
1515 */
1516 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001517 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001518 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 /*
1520 * We just mark the leaf entry stale by putting a null in it.
1521 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001522 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001524 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1526 /*
1527 * Scan the freespace in the data block again if necessary,
1528 * log the data block header if necessary.
1529 */
1530 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001531 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (needlog)
1533 xfs_dir2_data_log_header(tp, dbp);
1534 /*
1535 * If the longest freespace in the data block has changed,
1536 * put the new value in the bests table and log that.
1537 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001538 if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1539 bestsp[db] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1541 }
1542 xfs_dir2_data_check(dp, dbp);
1543 /*
1544 * If the data block is now empty then get rid of the data block.
1545 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001546 if (be16_to_cpu(hdr->bestfree[0].length) ==
1547 mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 ASSERT(db != mp->m_dirdatablk);
1549 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1550 /*
1551 * Nope, can't get rid of it because it caused
1552 * allocation of a bmap btree block to do so.
1553 * Just go on, returning success, leaving the
1554 * empty block in place.
1555 */
1556 if (error == ENOSPC && args->total == 0) {
1557 xfs_da_buf_done(dbp);
1558 error = 0;
1559 }
1560 xfs_dir2_leaf_check(dp, lbp);
1561 xfs_da_buf_done(lbp);
1562 return error;
1563 }
1564 dbp = NULL;
1565 /*
1566 * If this is the last data block then compact the
1567 * bests table by getting rid of entries.
1568 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001569 if (db == be32_to_cpu(ltp->bestcount) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /*
1571 * Look for the last active entry (i).
1572 */
1573 for (i = db - 1; i > 0; i--) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001574 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 break;
1576 }
1577 /*
1578 * Copy the table down so inactive entries at the
1579 * end are removed.
1580 */
1581 memmove(&bestsp[db - i], bestsp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001582 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001583 be32_add_cpu(&ltp->bestcount, -(db - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001585 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 } else
Nathan Scott68b3a102006-03-17 17:27:19 +11001587 bestsp[db] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589 /*
1590 * If the data block was not the first one, drop it.
1591 */
1592 else if (db != mp->m_dirdatablk && dbp != NULL) {
1593 xfs_da_buf_done(dbp);
1594 dbp = NULL;
1595 }
1596 xfs_dir2_leaf_check(dp, lbp);
1597 /*
1598 * See if we can convert to block form.
1599 */
1600 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1601}
1602
1603/*
1604 * Replace the inode number in a leaf format directory entry.
1605 */
1606int /* error */
1607xfs_dir2_leaf_replace(
1608 xfs_da_args_t *args) /* operation arguments */
1609{
1610 xfs_dabuf_t *dbp; /* data block buffer */
1611 xfs_dir2_data_entry_t *dep; /* data block entry */
1612 xfs_inode_t *dp; /* incore directory inode */
1613 int error; /* error return code */
1614 int index; /* index of leaf entry */
1615 xfs_dabuf_t *lbp; /* leaf buffer */
1616 xfs_dir2_leaf_t *leaf; /* leaf structure */
1617 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1618 xfs_trans_t *tp; /* transaction pointer */
1619
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001620 trace_xfs_dir2_leaf_replace(args);
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 /*
1623 * Look up the entry.
1624 */
1625 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1626 return error;
1627 }
1628 dp = args->dp;
1629 leaf = lbp->data;
1630 /*
1631 * Point to the leaf entry, get data address from it.
1632 */
1633 lep = &leaf->ents[index];
1634 /*
1635 * Point to the data entry.
1636 */
1637 dep = (xfs_dir2_data_entry_t *)
1638 ((char *)dbp->data +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001639 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001640 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 /*
1642 * Put the new inode number in, log it.
1643 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001644 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 tp = args->trans;
1646 xfs_dir2_data_log_entry(tp, dbp, dep);
1647 xfs_da_buf_done(dbp);
1648 xfs_dir2_leaf_check(dp, lbp);
1649 xfs_da_brelse(tp, lbp);
1650 return 0;
1651}
1652
1653/*
1654 * Return index in the leaf block (lbp) which is either the first
1655 * one with this hash value, or if there are none, the insert point
1656 * for that hash value.
1657 */
1658int /* index value */
1659xfs_dir2_leaf_search_hash(
1660 xfs_da_args_t *args, /* operation arguments */
1661 xfs_dabuf_t *lbp) /* leaf buffer */
1662{
1663 xfs_dahash_t hash=0; /* hash from this entry */
1664 xfs_dahash_t hashwant; /* hash value looking for */
1665 int high; /* high leaf index */
1666 int low; /* low leaf index */
1667 xfs_dir2_leaf_t *leaf; /* leaf structure */
1668 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1669 int mid=0; /* current leaf index */
1670
1671 leaf = lbp->data;
1672#ifndef __KERNEL__
1673 if (!leaf->hdr.count)
1674 return 0;
1675#endif
1676 /*
1677 * Note, the table cannot be empty, so we have to go through the loop.
1678 * Binary search the leaf entries looking for our hash value.
1679 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001680 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 hashwant = args->hashval;
1682 low <= high; ) {
1683 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001684 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 break;
1686 if (hash < hashwant)
1687 low = mid + 1;
1688 else
1689 high = mid - 1;
1690 }
1691 /*
1692 * Found one, back up through all the equal hash values.
1693 */
1694 if (hash == hashwant) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001695 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 mid--;
1697 }
1698 }
1699 /*
1700 * Need to point to an entry higher than ours.
1701 */
1702 else if (hash < hashwant)
1703 mid++;
1704 return mid;
1705}
1706
1707/*
1708 * Trim off a trailing data block. We know it's empty since the leaf
1709 * freespace table says so.
1710 */
1711int /* error */
1712xfs_dir2_leaf_trim_data(
1713 xfs_da_args_t *args, /* operation arguments */
1714 xfs_dabuf_t *lbp, /* leaf buffer */
1715 xfs_dir2_db_t db) /* data block number */
1716{
Nathan Scott68b3a102006-03-17 17:27:19 +11001717 __be16 *bestsp; /* leaf bests table */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 xfs_dabuf_t *dbp; /* data block buffer */
1719 xfs_inode_t *dp; /* incore directory inode */
1720 int error; /* error return value */
1721 xfs_dir2_leaf_t *leaf; /* leaf structure */
1722 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1723 xfs_mount_t *mp; /* filesystem mount point */
1724 xfs_trans_t *tp; /* transaction pointer */
1725
1726 dp = args->dp;
1727 mp = dp->i_mount;
1728 tp = args->trans;
1729 /*
1730 * Read the offending data block. We need its buffer.
1731 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001732 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 XFS_DATA_FORK))) {
1734 return error;
1735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 leaf = lbp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001738 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001739
1740#ifdef DEBUG
1741{
1742 struct xfs_dir2_data_hdr *hdr = dbp->data;
1743
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001744 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001745 ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
1746 mp->m_dirblksize - (uint)sizeof(*hdr));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001747 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001748}
1749#endif
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /*
1752 * Get rid of the data block.
1753 */
1754 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1755 ASSERT(error != ENOSPC);
1756 xfs_da_brelse(tp, dbp);
1757 return error;
1758 }
1759 /*
1760 * Eliminate the last bests entry from the table.
1761 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001762 bestsp = xfs_dir2_leaf_bests_p(ltp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001763 be32_add_cpu(&ltp->bestcount, -1);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001764 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001766 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return 0;
1768}
1769
Christoph Hellwig22823962011-07-08 14:35:53 +02001770static inline size_t
1771xfs_dir2_leaf_size(
1772 struct xfs_dir2_leaf_hdr *hdr,
1773 int counts)
1774{
1775 int entries;
1776
1777 entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
1778 return sizeof(xfs_dir2_leaf_hdr_t) +
1779 entries * sizeof(xfs_dir2_leaf_entry_t) +
1780 counts * sizeof(xfs_dir2_data_off_t) +
1781 sizeof(xfs_dir2_leaf_tail_t);
1782}
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784/*
1785 * Convert node form directory to leaf form directory.
1786 * The root of the node form dir needs to already be a LEAFN block.
1787 * Just return if we can't do anything.
1788 */
1789int /* error */
1790xfs_dir2_node_to_leaf(
1791 xfs_da_state_t *state) /* directory operation state */
1792{
1793 xfs_da_args_t *args; /* operation arguments */
1794 xfs_inode_t *dp; /* incore directory inode */
1795 int error; /* error return code */
1796 xfs_dabuf_t *fbp; /* buffer for freespace block */
1797 xfs_fileoff_t fo; /* freespace file offset */
1798 xfs_dir2_free_t *free; /* freespace structure */
1799 xfs_dabuf_t *lbp; /* buffer for leaf block */
1800 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1801 xfs_dir2_leaf_t *leaf; /* leaf structure */
1802 xfs_mount_t *mp; /* filesystem mount point */
1803 int rval; /* successful free trim? */
1804 xfs_trans_t *tp; /* transaction pointer */
1805
1806 /*
1807 * There's more than a leaf level in the btree, so there must
1808 * be multiple leafn blocks. Give up.
1809 */
1810 if (state->path.active > 1)
1811 return 0;
1812 args = state->args;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001813
1814 trace_xfs_dir2_node_to_leaf(args);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 mp = state->mp;
1817 dp = args->dp;
1818 tp = args->trans;
1819 /*
1820 * Get the last offset in the file.
1821 */
1822 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1823 return error;
1824 }
1825 fo -= mp->m_dirblkfsbs;
1826 /*
1827 * If there are freespace blocks other than the first one,
1828 * take this opportunity to remove trailing empty freespace blocks
1829 * that may have been left behind during no-space-reservation
1830 * operations.
1831 */
1832 while (fo > mp->m_dirfreeblk) {
1833 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1834 return error;
1835 }
1836 if (rval)
1837 fo -= mp->m_dirblkfsbs;
1838 else
1839 return 0;
1840 }
1841 /*
1842 * Now find the block just before the freespace block.
1843 */
1844 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1845 return error;
1846 }
1847 /*
1848 * If it's not the single leaf block, give up.
1849 */
1850 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1851 return 0;
1852 lbp = state->path.blk[0].bp;
1853 leaf = lbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001854 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 /*
1856 * Read the freespace block.
1857 */
1858 if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
1859 XFS_DATA_FORK))) {
1860 return error;
1861 }
1862 free = fbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001863 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 ASSERT(!free->hdr.firstdb);
Christoph Hellwig22823962011-07-08 14:35:53 +02001865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 /*
1867 * Now see if the leafn and free data will fit in a leaf1.
1868 * If not, release the buffer and give up.
1869 */
Christoph Hellwig22823962011-07-08 14:35:53 +02001870 if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
1871 mp->m_dirblksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 xfs_da_brelse(tp, fbp);
1873 return 0;
1874 }
Christoph Hellwig22823962011-07-08 14:35:53 +02001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /*
1877 * If the leaf has any stale entries in it, compress them out.
1878 * The compact routine will log the header.
1879 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001880 if (be16_to_cpu(leaf->hdr.stale))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 xfs_dir2_leaf_compact(args, lbp);
1882 else
1883 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott89da0542006-03-17 17:28:40 +11001884 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 /*
1886 * Set up the leaf tail from the freespace block.
1887 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001888 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scott0ba962e2006-03-17 17:27:07 +11001889 ltp->bestcount = free->hdr.nvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /*
1891 * Set up the leaf bests table.
1892 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001893 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
Christoph Hellwig22823962011-07-08 14:35:53 +02001894 be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001895 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 xfs_dir2_leaf_log_tail(tp, lbp);
1897 xfs_dir2_leaf_check(dp, lbp);
1898 /*
1899 * Get rid of the freespace block.
1900 */
1901 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1902 if (error) {
1903 /*
1904 * This can't fail here because it can only happen when
1905 * punching out the middle of an extent, and this is an
1906 * isolated block.
1907 */
1908 ASSERT(error != ENOSPC);
1909 return error;
1910 }
1911 fbp = NULL;
1912 /*
1913 * Now see if we can convert the single-leaf directory
1914 * down to a block form directory.
1915 * This routine always kills the dabuf for the leaf, so
1916 * eliminate it from the path.
1917 */
1918 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1919 state->path.blk[0].bp = NULL;
1920 return error;
1921}