blob: 0fdf765c917fe11b936552d4ba84e5eea54d27c9 [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"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_dinode.h"
30#include "xfs_inode.h"
31#include "xfs_bmap.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020032#include "xfs_dir2_format.h"
33#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000035#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
38 * Local function declarations.
39 */
40#ifdef DEBUG
Dave Chinner1d9025e2012-06-22 18:50:14 +100041static void xfs_dir2_leaf_check(struct xfs_inode *dp, struct xfs_buf *bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#else
43#define xfs_dir2_leaf_check(dp, bp)
44#endif
Dave Chinner1d9025e2012-06-22 18:50:14 +100045static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
46 int *indexp, struct xfs_buf **dbpp);
47static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_buf *bp,
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100048 int first, int last);
Dave Chinner1d9025e2012-06-22 18:50:14 +100049static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_buf *bp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
53 * Convert a block form directory to a leaf form directory.
54 */
55int /* error */
56xfs_dir2_block_to_leaf(
57 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +100058 struct xfs_buf *dbp) /* input block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Nathan Scott68b3a102006-03-17 17:27:19 +110060 __be16 *bestsp; /* leaf's bestsp entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 xfs_dablk_t blkno; /* leaf block's bno */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +020062 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
64 xfs_dir2_block_tail_t *btp; /* block's tail */
65 xfs_inode_t *dp; /* incore directory inode */
66 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +100067 struct xfs_buf *lbp; /* leaf block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 xfs_dir2_db_t ldb; /* leaf block's bno */
69 xfs_dir2_leaf_t *leaf; /* leaf structure */
70 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
71 xfs_mount_t *mp; /* filesystem mount point */
72 int needlog; /* need to log block header */
73 int needscan; /* need to rescan bestfree */
74 xfs_trans_t *tp; /* transaction pointer */
75
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000076 trace_xfs_dir2_block_to_leaf(args);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 dp = args->dp;
79 mp = dp->i_mount;
80 tp = args->trans;
81 /*
82 * Add the leaf block to the inode.
83 * This interface will only put blocks in the leaf/node range.
84 * Since that's empty now, we'll get the root (block 0 in range).
85 */
86 if ((error = xfs_da_grow_inode(args, &blkno))) {
87 return error;
88 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100089 ldb = xfs_dir2_da_to_db(mp, blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
91 /*
92 * Initialize the leaf block, get a buffer for it.
93 */
94 if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
95 return error;
96 }
97 ASSERT(lbp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +100098 leaf = lbp->b_addr;
99 hdr = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 xfs_dir2_data_check(dp, dbp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200101 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000102 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /*
104 * Set the counts in the leaf header.
105 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100106 leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
107 leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /*
109 * Could compact these but I think we always do the conversion
110 * after squeezing out stale entries.
111 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100112 memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100113 xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 needscan = 0;
115 needlog = 1;
116 /*
117 * Make the space formerly occupied by the leaf entries and block
118 * tail be free.
119 */
120 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200121 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
122 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 (char *)blp),
124 &needlog, &needscan);
125 /*
126 * Fix up the block header, make it a data block.
127 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200128 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200130 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 /*
132 * Set up leaf tail and bests table.
133 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000134 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100135 ltp->bestcount = cpu_to_be32(1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000136 bestsp = xfs_dir2_leaf_bests_p(ltp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200137 bestsp[0] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /*
139 * Log the data header and leaf bests table.
140 */
141 if (needlog)
142 xfs_dir2_data_log_header(tp, dbp);
143 xfs_dir2_leaf_check(dp, lbp);
144 xfs_dir2_data_check(dp, dbp);
145 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return 0;
147}
148
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200149STATIC void
150xfs_dir2_leaf_find_stale(
151 struct xfs_dir2_leaf *leaf,
152 int index,
153 int *lowstale,
154 int *highstale)
155{
156 /*
157 * Find the first stale entry before our index, if any.
158 */
159 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
160 if (leaf->ents[*lowstale].address ==
161 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
162 break;
163 }
164
165 /*
166 * Find the first stale entry at or after our index, if any.
167 * Stop if the result would require moving more entries than using
168 * lowstale.
169 */
170 for (*highstale = index;
171 *highstale < be16_to_cpu(leaf->hdr.count);
172 ++*highstale) {
173 if (leaf->ents[*highstale].address ==
174 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
175 break;
176 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
177 break;
178 }
179}
180
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200181struct xfs_dir2_leaf_entry *
182xfs_dir2_leaf_find_entry(
183 xfs_dir2_leaf_t *leaf, /* leaf structure */
184 int index, /* leaf table position */
185 int compact, /* need to compact leaves */
186 int lowstale, /* index of prev stale leaf */
187 int highstale, /* index of next stale leaf */
188 int *lfloglow, /* low leaf logging index */
189 int *lfloghigh) /* high leaf logging index */
190{
191 if (!leaf->hdr.stale) {
192 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
193
194 /*
195 * Now we need to make room to insert the leaf entry.
196 *
197 * If there are no stale entries, just insert a hole at index.
198 */
199 lep = &leaf->ents[index];
200 if (index < be16_to_cpu(leaf->hdr.count))
201 memmove(lep + 1, lep,
202 (be16_to_cpu(leaf->hdr.count) - index) *
203 sizeof(*lep));
204
205 /*
206 * Record low and high logging indices for the leaf.
207 */
208 *lfloglow = index;
209 *lfloghigh = be16_to_cpu(leaf->hdr.count);
210 be16_add_cpu(&leaf->hdr.count, 1);
211 return lep;
212 }
213
214 /*
215 * There are stale entries.
216 *
217 * We will use one of them for the new entry. It's probably not at
218 * the right location, so we'll have to shift some up or down first.
219 *
220 * If we didn't compact before, we need to find the nearest stale
221 * entries before and after our insertion point.
222 */
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200223 if (compact == 0)
224 xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200225
226 /*
227 * If the low one is better, use it.
228 */
229 if (lowstale >= 0 &&
230 (highstale == be16_to_cpu(leaf->hdr.count) ||
231 index - lowstale - 1 < highstale - index)) {
232 ASSERT(index - lowstale - 1 >= 0);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200233 ASSERT(leaf->ents[lowstale].address ==
234 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200235
236 /*
237 * Copy entries up to cover the stale entry and make room
238 * for the new entry.
239 */
240 if (index - lowstale - 1 > 0) {
241 memmove(&leaf->ents[lowstale],
242 &leaf->ents[lowstale + 1],
243 (index - lowstale - 1) *
244 sizeof(xfs_dir2_leaf_entry_t));
245 }
246 *lfloglow = MIN(lowstale, *lfloglow);
247 *lfloghigh = MAX(index - 1, *lfloghigh);
248 be16_add_cpu(&leaf->hdr.stale, -1);
249 return &leaf->ents[index - 1];
250 }
251
252 /*
253 * The high one is better, so use that one.
254 */
255 ASSERT(highstale - index >= 0);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200256 ASSERT(leaf->ents[highstale].address ==
257 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200258
259 /*
260 * Copy entries down to cover the stale entry and make room for the
261 * new entry.
262 */
263 if (highstale - index > 0) {
264 memmove(&leaf->ents[index + 1],
265 &leaf->ents[index],
266 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
267 }
268 *lfloglow = MIN(index, *lfloglow);
269 *lfloghigh = MAX(highstale, *lfloghigh);
270 be16_add_cpu(&leaf->hdr.stale, -1);
271 return &leaf->ents[index];
272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/*
275 * Add an entry to a leaf form directory.
276 */
277int /* error */
278xfs_dir2_leaf_addname(
279 xfs_da_args_t *args) /* operation arguments */
280{
Nathan Scott68b3a102006-03-17 17:27:19 +1100281 __be16 *bestsp; /* freespace table in leaf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int compact; /* need to compact leaves */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200283 xfs_dir2_data_hdr_t *hdr; /* data block header */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000284 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 xfs_dir2_data_entry_t *dep; /* data block entry */
286 xfs_inode_t *dp; /* incore directory inode */
287 xfs_dir2_data_unused_t *dup; /* data unused entry */
288 int error; /* error return value */
289 int grown; /* allocated new data block */
290 int highstale; /* index of next stale leaf */
291 int i; /* temporary, index */
292 int index; /* leaf table position */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000293 struct xfs_buf *lbp; /* leaf's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 xfs_dir2_leaf_t *leaf; /* leaf structure */
295 int length; /* length of new entry */
296 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
297 int lfloglow; /* low leaf logging index */
298 int lfloghigh; /* high leaf logging index */
299 int lowstale; /* index of prev stale leaf */
300 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
301 xfs_mount_t *mp; /* filesystem mount point */
302 int needbytes; /* leaf block bytes needed */
303 int needlog; /* need to log data header */
304 int needscan; /* need to rescan data free */
Nathan Scott3d693c62006-03-17 17:28:27 +1100305 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 xfs_trans_t *tp; /* transaction pointer */
307 xfs_dir2_db_t use_block; /* data block number */
308
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000309 trace_xfs_dir2_leaf_addname(args);
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 dp = args->dp;
312 tp = args->trans;
313 mp = dp->i_mount;
314 /*
315 * Read the leaf block.
316 */
317 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
Dave Chinner4bb20a82012-11-12 22:54:10 +1100318 XFS_DATA_FORK, NULL);
319 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 ASSERT(lbp != NULL);
322 /*
323 * Look up the entry by hash value and name.
324 * We know it's not there, our caller has already done a lookup.
325 * So the index is of the entry to insert in front of.
326 * But if there are dup hash values the index is of the first of those.
327 */
328 index = xfs_dir2_leaf_search_hash(args, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000329 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000330 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
331 bestsp = xfs_dir2_leaf_bests_p(ltp);
332 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /*
334 * See if there are any entries with the same hash value
335 * and space in their block for the new entry.
336 * This is good because it puts multiple same-hash value entries
337 * in a data block, improving the lookup of those entries.
338 */
339 for (use_block = -1, lep = &leaf->ents[index];
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100340 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 index++, lep++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100342 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 continue;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000344 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100345 ASSERT(i < be32_to_cpu(ltp->bestcount));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200346 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
Nathan Scott68b3a102006-03-17 17:27:19 +1100347 if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 use_block = i;
349 break;
350 }
351 }
352 /*
353 * Didn't find a block yet, linear search all the data blocks.
354 */
355 if (use_block == -1) {
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100356 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /*
358 * Remember a block we see that's missing.
359 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200360 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
361 use_block == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 use_block = i;
Nathan Scott68b3a102006-03-17 17:27:19 +1100363 else if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 use_block = i;
365 break;
366 }
367 }
368 }
369 /*
370 * How many bytes do we need in the leaf block?
371 */
Christoph Hellwig22823962011-07-08 14:35:53 +0200372 needbytes = 0;
373 if (!leaf->hdr.stale)
374 needbytes += sizeof(xfs_dir2_leaf_entry_t);
375 if (use_block == -1)
376 needbytes += sizeof(xfs_dir2_data_off_t);
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /*
379 * Now kill use_block if it refers to a missing block, so we
380 * can use it as an indication of allocation needed.
381 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200382 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 use_block = -1;
384 /*
385 * If we don't have enough free bytes but we can make enough
386 * by compacting out stale entries, we'll do that.
387 */
Barry Naujok6a178102008-05-21 16:42:05 +1000388 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
389 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 compact = 1;
391 }
392 /*
393 * Otherwise if we don't have enough free bytes we need to
394 * convert to node form.
395 */
Barry Naujok6a178102008-05-21 16:42:05 +1000396 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
397 leaf->hdr.count)] < needbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /*
399 * Just checking or no space reservation, give up.
400 */
Barry Naujok6a178102008-05-21 16:42:05 +1000401 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
402 args->total == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000403 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return XFS_ERROR(ENOSPC);
405 }
406 /*
407 * Convert to node form.
408 */
409 error = xfs_dir2_leaf_to_node(args, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 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) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000427 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 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) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000435 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 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))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000466 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return error;
468 }
469 /*
470 * Initialize the block.
471 */
472 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000473 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 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);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000493 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200494 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 grown = 1;
Dave Chinnere4813572012-11-12 22:54:14 +1100496 } else {
497 /*
498 * Already had space in some data block.
499 * Just read that one in.
500 */
501 error = xfs_dir2_data_read(tp, dp,
502 xfs_dir2_db_to_da(mp, use_block),
503 -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100504 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000505 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return error;
507 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000508 hdr = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 grown = 0;
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /*
512 * Point to the biggest freespace in our data block.
513 */
514 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200515 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
Nathan Scottad354eb2006-03-17 17:27:37 +1100516 ASSERT(be16_to_cpu(dup->length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 needscan = needlog = 0;
518 /*
519 * Mark the initial part of our freespace in use for the new entry.
520 */
521 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200522 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 &needlog, &needscan);
524 /*
525 * Initialize our new entry (at last).
526 */
527 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000528 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 dep->namelen = args->namelen;
530 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000531 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200532 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /*
534 * Need to scan fix up the bestfree table.
535 */
536 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200537 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /*
539 * Need to log the data block's header.
540 */
541 if (needlog)
542 xfs_dir2_data_log_header(tp, dbp);
543 xfs_dir2_data_log_entry(tp, dbp, dep);
544 /*
545 * If the bests table needs to be changed, do it.
546 * Log the change unless we've already done that.
547 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200548 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
549 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (!grown)
551 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
552 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200553
554 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
555 highstale, &lfloglow, &lfloghigh);
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /*
558 * Fill in the new leaf entry.
559 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100560 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000561 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
Nathan Scott3d693c62006-03-17 17:28:27 +1100562 be16_to_cpu(*tagp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /*
564 * Log the leaf fields and give up the buffers.
565 */
566 xfs_dir2_leaf_log_header(tp, lbp);
567 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
568 xfs_dir2_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 xfs_dir2_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return 0;
571}
572
573#ifdef DEBUG
574/*
575 * Check the internal consistency of a leaf1 block.
576 * Pop an assert if something is wrong.
577 */
Hannes Eder3180e662009-03-04 19:34:10 +0100578STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579xfs_dir2_leaf_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000580 struct xfs_inode *dp, /* incore directory inode */
581 struct xfs_buf *bp) /* leaf's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 int i; /* leaf index */
584 xfs_dir2_leaf_t *leaf; /* leaf structure */
585 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
586 xfs_mount_t *mp; /* filesystem mount point */
587 int stale; /* count of stale leaves */
588
Dave Chinner1d9025e2012-06-22 18:50:14 +1000589 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 mp = dp->i_mount;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200591 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /*
593 * This value is not restrictive enough.
594 * Should factor in the size of the bests table as well.
595 * We can deduce a value for that from di_size.
596 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000597 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
598 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /*
600 * Leaves and bests don't overlap.
601 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100602 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000603 (char *)xfs_dir2_leaf_bests_p(ltp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /*
605 * Check hash value order, count stale entries.
606 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100607 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
608 if (i + 1 < be16_to_cpu(leaf->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100609 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
610 be32_to_cpu(leaf->ents[i + 1].hashval));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200611 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 stale++;
613 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100614 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616#endif /* DEBUG */
617
618/*
619 * Compact out any stale entries in the leaf.
620 * Log the header and changed leaf entries, if any.
621 */
622void
623xfs_dir2_leaf_compact(
624 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000625 struct xfs_buf *bp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 int from; /* source leaf index */
628 xfs_dir2_leaf_t *leaf; /* leaf structure */
629 int loglow; /* first leaf entry to log */
630 int to; /* target leaf index */
631
Dave Chinner1d9025e2012-06-22 18:50:14 +1000632 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (!leaf->hdr.stale) {
634 return;
635 }
636 /*
637 * Compress out the stale entries in place.
638 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100639 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200640 if (leaf->ents[from].address ==
641 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 continue;
643 /*
644 * Only actually copy the entries that are different.
645 */
646 if (from > to) {
647 if (loglow == -1)
648 loglow = to;
649 leaf->ents[to] = leaf->ents[from];
650 }
651 to++;
652 }
653 /*
654 * Update and log the header, log the leaf entries.
655 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100656 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800657 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 leaf->hdr.stale = 0;
659 xfs_dir2_leaf_log_header(args->trans, bp);
660 if (loglow != -1)
661 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
662}
663
664/*
665 * Compact the leaf entries, removing stale ones.
666 * Leave one stale entry behind - the one closest to our
667 * insertion index - and the caller will shift that one to our insertion
668 * point later.
669 * Return new insertion index, where the remaining stale entry is,
670 * and leaf logging indices.
671 */
672void
673xfs_dir2_leaf_compact_x1(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000674 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int *indexp, /* insertion index */
676 int *lowstalep, /* out: stale entry before us */
677 int *highstalep, /* out: stale entry after us */
678 int *lowlogp, /* out: low log index */
679 int *highlogp) /* out: high log index */
680{
681 int from; /* source copy index */
682 int highstale; /* stale entry at/after index */
683 int index; /* insertion index */
684 int keepstale; /* source index of kept stale */
685 xfs_dir2_leaf_t *leaf; /* leaf structure */
686 int lowstale; /* stale entry before index */
687 int newindex=0; /* new insertion index */
688 int to; /* destination copy index */
689
Dave Chinner1d9025e2012-06-22 18:50:14 +1000690 leaf = bp->b_addr;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100691 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 index = *indexp;
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200693
694 xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /*
697 * Pick the better of lowstale and highstale.
698 */
699 if (lowstale >= 0 &&
Nathan Scotta818e5d2006-03-17 17:28:07 +1100700 (highstale == be16_to_cpu(leaf->hdr.count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 index - lowstale <= highstale - index))
702 keepstale = lowstale;
703 else
704 keepstale = highstale;
705 /*
706 * Copy the entries in place, removing all the stale entries
707 * except keepstale.
708 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100709 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
711 * Notice the new value of index.
712 */
713 if (index == from)
714 newindex = to;
715 if (from != keepstale &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200716 leaf->ents[from].address ==
717 cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (from == to)
719 *lowlogp = to;
720 continue;
721 }
722 /*
723 * Record the new keepstale value for the insertion.
724 */
725 if (from == keepstale)
726 lowstale = highstale = to;
727 /*
728 * Copy only the entries that have moved.
729 */
730 if (from > to)
731 leaf->ents[to] = leaf->ents[from];
732 to++;
733 }
734 ASSERT(from > to);
735 /*
736 * If the insertion point was past the last entry,
737 * set the new insertion point accordingly.
738 */
739 if (index == from)
740 newindex = to;
741 *indexp = newindex;
742 /*
743 * Adjust the leaf header values.
744 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800745 be16_add_cpu(&leaf->hdr.count, -(from - to));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100746 leaf->hdr.stale = cpu_to_be16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /*
748 * Remember the low/high stale value only in the "right"
749 * direction.
750 */
751 if (lowstale >= newindex)
752 lowstale = -1;
753 else
Nathan Scotta818e5d2006-03-17 17:28:07 +1100754 highstale = be16_to_cpu(leaf->hdr.count);
755 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 *lowstalep = lowstale;
757 *highstalep = highstale;
758}
759
Dave Chinner9b73bd72012-06-22 18:50:15 +1000760struct xfs_dir2_leaf_map_info {
761 xfs_extlen_t map_blocks; /* number of fsbs in map */
762 xfs_dablk_t map_off; /* last mapped file offset */
763 int map_size; /* total entries in *map */
764 int map_valid; /* valid entries in *map */
765 int nmap; /* mappings to ask xfs_bmapi */
766 xfs_dir2_db_t curdb; /* db for current block */
767 int ra_current; /* number of read-ahead blks */
768 int ra_index; /* *map index for read-ahead */
769 int ra_offset; /* map entry offset for ra */
770 int ra_want; /* readahead count wanted */
771 struct xfs_bmbt_irec map[]; /* map vector for blocks */
772};
773
774STATIC int
775xfs_dir2_leaf_readbuf(
776 struct xfs_inode *dp,
777 size_t bufsize,
778 struct xfs_dir2_leaf_map_info *mip,
779 xfs_dir2_off_t *curoff,
780 struct xfs_buf **bpp)
781{
782 struct xfs_mount *mp = dp->i_mount;
783 struct xfs_buf *bp = *bpp;
784 struct xfs_bmbt_irec *map = mip->map;
785 int error = 0;
786 int length;
787 int i;
788 int j;
789
790 /*
791 * If we have a buffer, we need to release it and
792 * take it out of the mapping.
793 */
794
795 if (bp) {
796 xfs_trans_brelse(NULL, bp);
797 bp = NULL;
798 mip->map_blocks -= mp->m_dirblkfsbs;
799 /*
800 * Loop to get rid of the extents for the
801 * directory block.
802 */
803 for (i = mp->m_dirblkfsbs; i > 0; ) {
804 j = min_t(int, map->br_blockcount, i);
805 map->br_blockcount -= j;
806 map->br_startblock += j;
807 map->br_startoff += j;
808 /*
809 * If mapping is done, pitch it from
810 * the table.
811 */
812 if (!map->br_blockcount && --mip->map_valid)
813 memmove(&map[0], &map[1],
814 sizeof(map[0]) * mip->map_valid);
815 i -= j;
816 }
817 }
818
819 /*
820 * Recalculate the readahead blocks wanted.
821 */
822 mip->ra_want = howmany(bufsize + mp->m_dirblksize,
823 mp->m_sb.sb_blocksize) - 1;
824 ASSERT(mip->ra_want >= 0);
825
826 /*
827 * If we don't have as many as we want, and we haven't
828 * run out of data blocks, get some more mappings.
829 */
830 if (1 + mip->ra_want > mip->map_blocks &&
831 mip->map_off < xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
832 /*
833 * Get more bmaps, fill in after the ones
834 * we already have in the table.
835 */
836 mip->nmap = mip->map_size - mip->map_valid;
837 error = xfs_bmapi_read(dp, mip->map_off,
838 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET) -
839 mip->map_off,
840 &map[mip->map_valid], &mip->nmap, 0);
841
842 /*
843 * Don't know if we should ignore this or try to return an
844 * error. The trouble with returning errors is that readdir
845 * will just stop without actually passing the error through.
846 */
847 if (error)
848 goto out; /* XXX */
849
850 /*
851 * If we got all the mappings we asked for, set the final map
852 * offset based on the last bmap value received. Otherwise,
853 * we've reached the end.
854 */
855 if (mip->nmap == mip->map_size - mip->map_valid) {
856 i = mip->map_valid + mip->nmap - 1;
857 mip->map_off = map[i].br_startoff + map[i].br_blockcount;
858 } else
859 mip->map_off = xfs_dir2_byte_to_da(mp,
860 XFS_DIR2_LEAF_OFFSET);
861
862 /*
863 * Look for holes in the mapping, and eliminate them. Count up
864 * the valid blocks.
865 */
866 for (i = mip->map_valid; i < mip->map_valid + mip->nmap; ) {
867 if (map[i].br_startblock == HOLESTARTBLOCK) {
868 mip->nmap--;
869 length = mip->map_valid + mip->nmap - i;
870 if (length)
871 memmove(&map[i], &map[i + 1],
872 sizeof(map[i]) * length);
873 } else {
874 mip->map_blocks += map[i].br_blockcount;
875 i++;
876 }
877 }
878 mip->map_valid += mip->nmap;
879 }
880
881 /*
882 * No valid mappings, so no more data blocks.
883 */
884 if (!mip->map_valid) {
885 *curoff = xfs_dir2_da_to_byte(mp, mip->map_off);
886 goto out;
887 }
888
889 /*
890 * Read the directory block starting at the first mapping.
891 */
892 mip->curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
Dave Chinnere4813572012-11-12 22:54:14 +1100893 error = xfs_dir2_data_read(NULL, dp, map->br_startoff,
Dave Chinner9b73bd72012-06-22 18:50:15 +1000894 map->br_blockcount >= mp->m_dirblkfsbs ?
Dave Chinnere4813572012-11-12 22:54:14 +1100895 XFS_FSB_TO_DADDR(mp, map->br_startblock) : -1, &bp);
Dave Chinner9b73bd72012-06-22 18:50:15 +1000896
897 /*
898 * Should just skip over the data block instead of giving up.
899 */
900 if (error)
901 goto out; /* XXX */
902
903 /*
904 * Adjust the current amount of read-ahead: we just read a block that
905 * was previously ra.
906 */
907 if (mip->ra_current)
908 mip->ra_current -= mp->m_dirblkfsbs;
909
910 /*
911 * Do we need more readahead?
912 */
913 for (mip->ra_index = mip->ra_offset = i = 0;
914 mip->ra_want > mip->ra_current && i < mip->map_blocks;
915 i += mp->m_dirblkfsbs) {
916 ASSERT(mip->ra_index < mip->map_valid);
917 /*
918 * Read-ahead a contiguous directory block.
919 */
920 if (i > mip->ra_current &&
921 map[mip->ra_index].br_blockcount >= mp->m_dirblkfsbs) {
922 xfs_buf_readahead(mp->m_ddev_targp,
923 XFS_FSB_TO_DADDR(mp,
924 map[mip->ra_index].br_startblock +
925 mip->ra_offset),
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100926 (int)BTOBB(mp->m_dirblksize), NULL);
Dave Chinner9b73bd72012-06-22 18:50:15 +1000927 mip->ra_current = i;
928 }
929
930 /*
931 * Read-ahead a non-contiguous directory block. This doesn't
932 * use our mapping, but this is a very rare case.
933 */
934 else if (i > mip->ra_current) {
935 xfs_da_reada_buf(NULL, dp,
936 map[mip->ra_index].br_startoff +
937 mip->ra_offset,
Dave Chinner4bb20a82012-11-12 22:54:10 +1100938 XFS_DATA_FORK, NULL);
Dave Chinner9b73bd72012-06-22 18:50:15 +1000939 mip->ra_current = i;
940 }
941
942 /*
943 * Advance offset through the mapping table.
944 */
945 for (j = 0; j < mp->m_dirblkfsbs; j++) {
946 /*
947 * The rest of this extent but not more than a dir
948 * block.
949 */
950 length = min_t(int, mp->m_dirblkfsbs,
951 map[mip->ra_index].br_blockcount -
952 mip->ra_offset);
953 j += length;
954 mip->ra_offset += length;
955
956 /*
957 * Advance to the next mapping if this one is used up.
958 */
959 if (mip->ra_offset == map[mip->ra_index].br_blockcount) {
960 mip->ra_offset = 0;
961 mip->ra_index++;
962 }
963 }
964 }
965
966out:
967 *bpp = bp;
968 return error;
969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/*
972 * Getdents (readdir) for leaf and node directories.
973 * This reads the data blocks only, so is the same for both forms.
974 */
975int /* error */
976xfs_dir2_leaf_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 xfs_inode_t *dp, /* incore directory inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000978 void *dirent,
979 size_t bufsize,
980 xfs_off_t *offset,
981 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Dave Chinner9b73bd72012-06-22 18:50:15 +1000983 struct xfs_buf *bp = NULL; /* data block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200984 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 xfs_dir2_data_entry_t *dep; /* data entry */
986 xfs_dir2_data_unused_t *dup; /* unused entry */
Nathan Scottf6d75cb2006-03-14 13:32:24 +1100987 int error = 0; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 int length; /* temporary length value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 xfs_mount_t *mp; /* filesystem mount point */
Dave Chinner9b73bd72012-06-22 18:50:15 +1000990 int byteoff; /* offset in current block */
991 xfs_dir2_off_t curoff; /* current overall offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 xfs_dir2_off_t newoff; /* new curoff after new blk */
Nathan Scottf6d75cb2006-03-14 13:32:24 +1100993 char *ptr = NULL; /* pointer to current data */
Dave Chinner9b73bd72012-06-22 18:50:15 +1000994 struct xfs_dir2_leaf_map_info *map_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 /*
997 * If the offset is at or past the largest allowed value,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000998 * give up right away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001000 if (*offset >= XFS_DIR2_MAX_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return 0;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 mp = dp->i_mount;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /*
1006 * Set up to bmap a number of blocks based on the caller's
1007 * buffer size, the directory block size, and the filesystem
1008 * block size.
1009 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001010 length = howmany(bufsize + mp->m_dirblksize,
1011 mp->m_sb.sb_blocksize);
1012 map_info = kmem_zalloc(offsetof(struct xfs_dir2_leaf_map_info, map) +
1013 (length * sizeof(struct xfs_bmbt_irec)),
1014 KM_SLEEP);
1015 map_info->map_size = length;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 /*
1018 * Inside the loop we keep the main offset value as a byte offset
1019 * in the directory file.
1020 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001021 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /*
1024 * Force this conversion through db so we truncate the offset
1025 * down to get the start of the data block.
1026 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001027 map_info->map_off = xfs_dir2_db_to_da(mp,
1028 xfs_dir2_byte_to_db(mp, curoff));
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /*
1031 * Loop over directory entries until we reach the end offset.
1032 * Get more blocks and readahead as necessary.
1033 */
1034 while (curoff < XFS_DIR2_LEAF_OFFSET) {
1035 /*
1036 * If we have no buffer, or we're off the end of the
1037 * current buffer, need to get another one.
1038 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001039 if (!bp || ptr >= (char *)bp->b_addr + mp->m_dirblksize) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001040
Dave Chinner9b73bd72012-06-22 18:50:15 +10001041 error = xfs_dir2_leaf_readbuf(dp, bufsize, map_info,
1042 &curoff, &bp);
1043 if (error || !map_info->map_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 break;
Dave Chinner9b73bd72012-06-22 18:50:15 +10001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 /*
1047 * Having done a read, we need to set a new offset.
1048 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001049 newoff = xfs_dir2_db_off_to_byte(mp, map_info->curdb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /*
1051 * Start of the current block.
1052 */
1053 if (curoff < newoff)
1054 curoff = newoff;
1055 /*
1056 * Make sure we're in the right block.
1057 */
1058 else if (curoff > newoff)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001059 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
Dave Chinner9b73bd72012-06-22 18:50:15 +10001060 map_info->curdb);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001061 hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 xfs_dir2_data_check(dp, bp);
1063 /*
1064 * Find our position in the block.
1065 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +02001066 ptr = (char *)(hdr + 1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001067 byteoff = xfs_dir2_byte_to_off(mp, curoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /*
1069 * Skip past the header.
1070 */
1071 if (byteoff == 0)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001072 curoff += (uint)sizeof(*hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /*
1074 * Skip past entries until we reach our offset.
1075 */
1076 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001077 while ((char *)ptr - (char *)hdr < byteoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 dup = (xfs_dir2_data_unused_t *)ptr;
1079
Nathan Scottad354eb2006-03-17 17:27:37 +11001080 if (be16_to_cpu(dup->freetag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 == XFS_DIR2_DATA_FREE_TAG) {
1082
Nathan Scottad354eb2006-03-17 17:27:37 +11001083 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 ptr += length;
1085 continue;
1086 }
1087 dep = (xfs_dir2_data_entry_t *)ptr;
1088 length =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001089 xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 ptr += length;
1091 }
1092 /*
1093 * Now set our real offset.
1094 */
1095 curoff =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001096 xfs_dir2_db_off_to_byte(mp,
1097 xfs_dir2_byte_to_db(mp, curoff),
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001098 (char *)ptr - (char *)hdr);
1099 if (ptr >= (char *)hdr + mp->m_dirblksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 continue;
1101 }
1102 }
1103 }
1104 /*
1105 * We have a pointer to an entry.
1106 * Is it a live one?
1107 */
1108 dup = (xfs_dir2_data_unused_t *)ptr;
1109 /*
1110 * No, it's unused, skip over it.
1111 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001112 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1113 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 ptr += length;
1115 curoff += length;
1116 continue;
1117 }
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 dep = (xfs_dir2_data_entry_t *)ptr;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001120 length = xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Dave Chinner4a24cb72010-01-20 10:48:05 +11001122 if (filldir(dirent, (char *)dep->name, dep->namelen,
Christoph Hellwig15440312009-01-08 14:00:00 -05001123 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
Christoph Hellwiga19d9f82009-03-29 09:51:08 +02001124 be64_to_cpu(dep->inumber), DT_UNKNOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 break;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 /*
1128 * Advance to next entry in the block.
1129 */
1130 ptr += length;
1131 curoff += length;
Eric Sandeen8e69ce12009-09-25 19:42:26 +00001132 /* bufsize may have just been a guess; don't go negative */
1133 bufsize = bufsize > length ? bufsize - length : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
1136 /*
1137 * All done. Set output offset value to current offset.
1138 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001139 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
Christoph Hellwig15440312009-01-08 14:00:00 -05001140 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 else
Christoph Hellwig15440312009-01-08 14:00:00 -05001142 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
Dave Chinner9b73bd72012-06-22 18:50:15 +10001143 kmem_free(map_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (bp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001145 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return error;
1147}
1148
1149/*
1150 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1151 */
1152int
1153xfs_dir2_leaf_init(
1154 xfs_da_args_t *args, /* operation arguments */
1155 xfs_dir2_db_t bno, /* directory block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001156 struct xfs_buf **bpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int magic) /* magic number for block */
1158{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001159 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 xfs_inode_t *dp; /* incore directory inode */
1161 int error; /* error return code */
1162 xfs_dir2_leaf_t *leaf; /* leaf structure */
1163 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1164 xfs_mount_t *mp; /* filesystem mount point */
1165 xfs_trans_t *tp; /* transaction pointer */
1166
1167 dp = args->dp;
1168 ASSERT(dp != NULL);
1169 tp = args->trans;
1170 mp = dp->i_mount;
1171 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1172 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1173 /*
1174 * Get the buffer for the block.
1175 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001176 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 XFS_DATA_FORK);
1178 if (error) {
1179 return error;
1180 }
1181 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001182 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /*
1184 * Initialize the header.
1185 */
Nathan Scott89da0542006-03-17 17:28:40 +11001186 leaf->hdr.info.magic = cpu_to_be16(magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 leaf->hdr.info.forw = 0;
1188 leaf->hdr.info.back = 0;
1189 leaf->hdr.count = 0;
1190 leaf->hdr.stale = 0;
1191 xfs_dir2_leaf_log_header(tp, bp);
1192 /*
1193 * If it's a leaf-format directory initialize the tail.
1194 * In this case our caller has the real bests table to copy into
1195 * the block.
1196 */
1197 if (magic == XFS_DIR2_LEAF1_MAGIC) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001198 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 ltp->bestcount = 0;
1200 xfs_dir2_leaf_log_tail(tp, bp);
1201 }
1202 *bpp = bp;
1203 return 0;
1204}
1205
1206/*
1207 * Log the bests entries indicated from a leaf1 block.
1208 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001209static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210xfs_dir2_leaf_log_bests(
1211 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001212 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 int first, /* first entry to log */
1214 int last) /* last entry to log */
1215{
Nathan Scott68b3a102006-03-17 17:27:19 +11001216 __be16 *firstb; /* pointer to first entry */
1217 __be16 *lastb; /* pointer to last entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 xfs_dir2_leaf_t *leaf; /* leaf structure */
1219 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1220
Dave Chinner1d9025e2012-06-22 18:50:14 +10001221 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001222 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001223 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1224 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1225 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001226 xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1228}
1229
1230/*
1231 * Log the leaf entries indicated from a leaf1 or leafn block.
1232 */
1233void
1234xfs_dir2_leaf_log_ents(
1235 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001236 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int first, /* first entry to log */
1238 int last) /* last entry to log */
1239{
1240 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1241 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1242 xfs_dir2_leaf_t *leaf; /* leaf structure */
1243
Dave Chinner1d9025e2012-06-22 18:50:14 +10001244 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001245 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1246 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 firstlep = &leaf->ents[first];
1248 lastlep = &leaf->ents[last];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001249 xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1251}
1252
1253/*
1254 * Log the header of the leaf1 or leafn block.
1255 */
1256void
1257xfs_dir2_leaf_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001258 struct xfs_trans *tp,
1259 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 xfs_dir2_leaf_t *leaf; /* leaf structure */
1262
Dave Chinner1d9025e2012-06-22 18:50:14 +10001263 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001264 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1265 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001266 xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 (uint)(sizeof(leaf->hdr) - 1));
1268}
1269
1270/*
1271 * Log the tail of the leaf1 block.
1272 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001273STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274xfs_dir2_leaf_log_tail(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001275 struct xfs_trans *tp,
1276 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 xfs_dir2_leaf_t *leaf; /* leaf structure */
1279 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1280 xfs_mount_t *mp; /* filesystem mount point */
1281
1282 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001283 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001284 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001285 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001286 xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 (uint)(mp->m_dirblksize - 1));
1288}
1289
1290/*
1291 * Look up the entry referred to by args in the leaf format directory.
1292 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1293 * is also used by the node-format code.
1294 */
1295int
1296xfs_dir2_leaf_lookup(
1297 xfs_da_args_t *args) /* operation arguments */
1298{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001299 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 xfs_dir2_data_entry_t *dep; /* data block entry */
1301 xfs_inode_t *dp; /* incore directory inode */
1302 int error; /* error return code */
1303 int index; /* found entry index */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001304 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 xfs_dir2_leaf_t *leaf; /* leaf structure */
1306 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1307 xfs_trans_t *tp; /* transaction pointer */
1308
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001309 trace_xfs_dir2_leaf_lookup(args);
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /*
1312 * Look up name in the leaf block, returning both buffers and index.
1313 */
1314 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1315 return error;
1316 }
1317 tp = args->trans;
1318 dp = args->dp;
1319 xfs_dir2_leaf_check(dp, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001320 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /*
1322 * Get to the leaf entry and contained data entry address.
1323 */
1324 lep = &leaf->ents[index];
1325 /*
1326 * Point to the data entry.
1327 */
1328 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001329 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001330 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +10001332 * Return the found inode number & CI name if appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001334 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001335 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001336 xfs_trans_brelse(tp, dbp);
1337 xfs_trans_brelse(tp, lbp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001338 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
1341/*
1342 * Look up name/hash in the leaf block.
1343 * Fill in indexp with the found index, and dbpp with the data buffer.
1344 * If not found dbpp will be NULL, and ENOENT comes back.
1345 * lbpp will always be filled in with the leaf buffer unless there's an error.
1346 */
1347static int /* error */
1348xfs_dir2_leaf_lookup_int(
1349 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001350 struct xfs_buf **lbpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 int *indexp, /* out: index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001352 struct xfs_buf **dbpp) /* out: data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001354 xfs_dir2_db_t curdb = -1; /* current data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001355 struct xfs_buf *dbp = NULL; /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 xfs_dir2_data_entry_t *dep; /* data entry */
1357 xfs_inode_t *dp; /* incore directory inode */
1358 int error; /* error return code */
1359 int index; /* index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001360 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1362 xfs_dir2_leaf_t *leaf; /* leaf structure */
1363 xfs_mount_t *mp; /* filesystem mount point */
1364 xfs_dir2_db_t newdb; /* new data block number */
1365 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001366 xfs_dir2_db_t cidb = -1; /* case match data block no. */
Barry Naujok5163f952008-05-21 16:41:01 +10001367 enum xfs_dacmp cmp; /* name compare result */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 dp = args->dp;
1370 tp = args->trans;
1371 mp = dp->i_mount;
1372 /*
1373 * Read the leaf block into the buffer.
1374 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001375 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
Dave Chinner4bb20a82012-11-12 22:54:10 +11001376 XFS_DATA_FORK, NULL);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001377 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 *lbpp = lbp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001380 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 xfs_dir2_leaf_check(dp, lbp);
1382 /*
1383 * Look for the first leaf entry with our hash value.
1384 */
1385 index = xfs_dir2_leaf_search_hash(args, lbp);
1386 /*
1387 * Loop over all the entries with the right hash value
1388 * looking to match the name.
1389 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001390 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
Barry Naujok5163f952008-05-21 16:41:01 +10001391 be32_to_cpu(lep->hashval) == args->hashval;
1392 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 /*
1394 * Skip over stale leaf entries.
1395 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001396 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 continue;
1398 /*
1399 * Get the new data block number.
1400 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001401 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /*
1403 * If it's not the same as the old data block number,
1404 * need to pitch the old one and read the new one.
1405 */
1406 if (newdb != curdb) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001407 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001408 xfs_trans_brelse(tp, dbp);
Dave Chinnere4813572012-11-12 22:54:14 +11001409 error = xfs_dir2_data_read(tp, dp,
1410 xfs_dir2_db_to_da(mp, newdb),
1411 -1, &dbp);
Barry Naujok5163f952008-05-21 16:41:01 +10001412 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001413 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return error;
1415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 curdb = newdb;
1417 }
1418 /*
1419 * Point to the data entry.
1420 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001421 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
Barry Naujok5163f952008-05-21 16:41:01 +10001422 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 /*
Barry Naujok5163f952008-05-21 16:41:01 +10001424 * Compare name and if it's an exact match, return the index
1425 * and buffer. If it's the first case-insensitive match, store
1426 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 */
Barry Naujok5163f952008-05-21 16:41:01 +10001428 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1429 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1430 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 *indexp = index;
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001432 /* case exact match: return the current buffer. */
Barry Naujok5163f952008-05-21 16:41:01 +10001433 if (cmp == XFS_CMP_EXACT) {
Barry Naujok5163f952008-05-21 16:41:01 +10001434 *dbpp = dbp;
1435 return 0;
1436 }
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001437 cidb = curdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439 }
Barry Naujok6a178102008-05-21 16:42:05 +10001440 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +10001441 /*
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001442 * Here, we can only be doing a lookup (not a rename or remove).
1443 * If a case-insensitive match was found earlier, re-read the
1444 * appropriate data block if required and return it.
Barry Naujok5163f952008-05-21 16:41:01 +10001445 */
1446 if (args->cmpresult == XFS_CMP_CASE) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001447 ASSERT(cidb != -1);
1448 if (cidb != curdb) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001449 xfs_trans_brelse(tp, dbp);
Dave Chinnere4813572012-11-12 22:54:14 +11001450 error = xfs_dir2_data_read(tp, dp,
1451 xfs_dir2_db_to_da(mp, cidb),
1452 -1, &dbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001453 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001454 xfs_trans_brelse(tp, lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001455 return error;
1456 }
1457 }
1458 *dbpp = dbp;
Barry Naujok5163f952008-05-21 16:41:01 +10001459 return 0;
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 /*
1462 * No match found, return ENOENT.
1463 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001464 ASSERT(cidb == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001466 xfs_trans_brelse(tp, dbp);
1467 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return XFS_ERROR(ENOENT);
1469}
1470
1471/*
1472 * Remove an entry from a leaf format directory.
1473 */
1474int /* error */
1475xfs_dir2_leaf_removename(
1476 xfs_da_args_t *args) /* operation arguments */
1477{
Nathan Scott68b3a102006-03-17 17:27:19 +11001478 __be16 *bestsp; /* leaf block best freespace */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001479 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001481 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 xfs_dir2_data_entry_t *dep; /* data entry structure */
1483 xfs_inode_t *dp; /* incore directory inode */
1484 int error; /* error return code */
1485 xfs_dir2_db_t i; /* temporary data block # */
1486 int index; /* index into leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001487 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 xfs_dir2_leaf_t *leaf; /* leaf structure */
1489 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1490 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1491 xfs_mount_t *mp; /* filesystem mount point */
1492 int needlog; /* need to log data header */
1493 int needscan; /* need to rescan data frees */
1494 xfs_dir2_data_off_t oldbest; /* old value of best free */
1495 xfs_trans_t *tp; /* transaction pointer */
1496
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001497 trace_xfs_dir2_leaf_removename(args);
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 /*
1500 * Lookup the leaf entry, get the leaf and data blocks read in.
1501 */
1502 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1503 return error;
1504 }
1505 dp = args->dp;
1506 tp = args->trans;
1507 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001508 leaf = lbp->b_addr;
1509 hdr = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 xfs_dir2_data_check(dp, dbp);
1511 /*
1512 * Point to the leaf entry, use that to point to the data entry.
1513 */
1514 lep = &leaf->ents[index];
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001515 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001517 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 needscan = needlog = 0;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001519 oldbest = be16_to_cpu(hdr->bestfree[0].length);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001520 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1521 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott68b3a102006-03-17 17:27:19 +11001522 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 /*
1524 * Mark the former data entry unused.
1525 */
1526 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001527 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001528 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 /*
1530 * We just mark the leaf entry stale by putting a null in it.
1531 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001532 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001534 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1536 /*
1537 * Scan the freespace in the data block again if necessary,
1538 * log the data block header if necessary.
1539 */
1540 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001541 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (needlog)
1543 xfs_dir2_data_log_header(tp, dbp);
1544 /*
1545 * If the longest freespace in the data block has changed,
1546 * put the new value in the bests table and log that.
1547 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001548 if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1549 bestsp[db] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1551 }
1552 xfs_dir2_data_check(dp, dbp);
1553 /*
1554 * If the data block is now empty then get rid of the data block.
1555 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001556 if (be16_to_cpu(hdr->bestfree[0].length) ==
1557 mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 ASSERT(db != mp->m_dirdatablk);
1559 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1560 /*
1561 * Nope, can't get rid of it because it caused
1562 * allocation of a bmap btree block to do so.
1563 * Just go on, returning success, leaving the
1564 * empty block in place.
1565 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001566 if (error == ENOSPC && args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 xfs_dir2_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return error;
1570 }
1571 dbp = NULL;
1572 /*
1573 * If this is the last data block then compact the
1574 * bests table by getting rid of entries.
1575 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001576 if (db == be32_to_cpu(ltp->bestcount) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /*
1578 * Look for the last active entry (i).
1579 */
1580 for (i = db - 1; i > 0; i--) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001581 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 break;
1583 }
1584 /*
1585 * Copy the table down so inactive entries at the
1586 * end are removed.
1587 */
1588 memmove(&bestsp[db - i], bestsp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001589 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001590 be32_add_cpu(&ltp->bestcount, -(db - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001592 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 } else
Nathan Scott68b3a102006-03-17 17:27:19 +11001594 bestsp[db] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
1596 /*
1597 * If the data block was not the first one, drop it.
1598 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001599 else if (db != mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 dbp = NULL;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 xfs_dir2_leaf_check(dp, lbp);
1603 /*
1604 * See if we can convert to block form.
1605 */
1606 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1607}
1608
1609/*
1610 * Replace the inode number in a leaf format directory entry.
1611 */
1612int /* error */
1613xfs_dir2_leaf_replace(
1614 xfs_da_args_t *args) /* operation arguments */
1615{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001616 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 xfs_dir2_data_entry_t *dep; /* data block entry */
1618 xfs_inode_t *dp; /* incore directory inode */
1619 int error; /* error return code */
1620 int index; /* index of leaf entry */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001621 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 xfs_dir2_leaf_t *leaf; /* leaf structure */
1623 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1624 xfs_trans_t *tp; /* transaction pointer */
1625
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001626 trace_xfs_dir2_leaf_replace(args);
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /*
1629 * Look up the entry.
1630 */
1631 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1632 return error;
1633 }
1634 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001635 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /*
1637 * Point to the leaf entry, get data address from it.
1638 */
1639 lep = &leaf->ents[index];
1640 /*
1641 * Point to the data entry.
1642 */
1643 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001644 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001645 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001646 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /*
1648 * Put the new inode number in, log it.
1649 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001650 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 tp = args->trans;
1652 xfs_dir2_data_log_entry(tp, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 xfs_dir2_leaf_check(dp, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001654 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return 0;
1656}
1657
1658/*
1659 * Return index in the leaf block (lbp) which is either the first
1660 * one with this hash value, or if there are none, the insert point
1661 * for that hash value.
1662 */
1663int /* index value */
1664xfs_dir2_leaf_search_hash(
1665 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001666 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 xfs_dahash_t hash=0; /* hash from this entry */
1669 xfs_dahash_t hashwant; /* hash value looking for */
1670 int high; /* high leaf index */
1671 int low; /* low leaf index */
1672 xfs_dir2_leaf_t *leaf; /* leaf structure */
1673 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1674 int mid=0; /* current leaf index */
1675
Dave Chinner1d9025e2012-06-22 18:50:14 +10001676 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677#ifndef __KERNEL__
1678 if (!leaf->hdr.count)
1679 return 0;
1680#endif
1681 /*
1682 * Note, the table cannot be empty, so we have to go through the loop.
1683 * Binary search the leaf entries looking for our hash value.
1684 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001685 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 hashwant = args->hashval;
1687 low <= high; ) {
1688 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001689 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 break;
1691 if (hash < hashwant)
1692 low = mid + 1;
1693 else
1694 high = mid - 1;
1695 }
1696 /*
1697 * Found one, back up through all the equal hash values.
1698 */
1699 if (hash == hashwant) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001700 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 mid--;
1702 }
1703 }
1704 /*
1705 * Need to point to an entry higher than ours.
1706 */
1707 else if (hash < hashwant)
1708 mid++;
1709 return mid;
1710}
1711
1712/*
1713 * Trim off a trailing data block. We know it's empty since the leaf
1714 * freespace table says so.
1715 */
1716int /* error */
1717xfs_dir2_leaf_trim_data(
1718 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001719 struct xfs_buf *lbp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 xfs_dir2_db_t db) /* data block number */
1721{
Nathan Scott68b3a102006-03-17 17:27:19 +11001722 __be16 *bestsp; /* leaf bests table */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001723 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 xfs_inode_t *dp; /* incore directory inode */
1725 int error; /* error return value */
1726 xfs_dir2_leaf_t *leaf; /* leaf structure */
1727 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1728 xfs_mount_t *mp; /* filesystem mount point */
1729 xfs_trans_t *tp; /* transaction pointer */
1730
1731 dp = args->dp;
1732 mp = dp->i_mount;
1733 tp = args->trans;
1734 /*
1735 * Read the offending data block. We need its buffer.
1736 */
Dave Chinnere4813572012-11-12 22:54:14 +11001737 error = xfs_dir2_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001738 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Dave Chinner1d9025e2012-06-22 18:50:14 +10001741 leaf = lbp->b_addr;
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{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001746 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001747
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);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001760 xfs_trans_brelse(tp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 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 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001800 struct xfs_buf *fbp; /* buffer for freespace block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 xfs_fileoff_t fo; /* freespace file offset */
1802 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001803 struct xfs_buf *lbp; /* buffer for leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 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;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001857 leaf = lbp->b_addr;
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 */
Dave Chinner20252072012-11-12 22:54:13 +11001862 error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001863 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001865 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001866 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 ASSERT(!free->hdr.firstdb);
Christoph Hellwig22823962011-07-08 14:35:53 +02001868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /*
1870 * Now see if the leafn and free data will fit in a leaf1.
1871 * If not, release the buffer and give up.
1872 */
Christoph Hellwig22823962011-07-08 14:35:53 +02001873 if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
1874 mp->m_dirblksize) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001875 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return 0;
1877 }
Christoph Hellwig22823962011-07-08 14:35:53 +02001878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 /*
1880 * If the leaf has any stale entries in it, compress them out.
1881 * The compact routine will log the header.
1882 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001883 if (be16_to_cpu(leaf->hdr.stale))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 xfs_dir2_leaf_compact(args, lbp);
1885 else
1886 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott89da0542006-03-17 17:28:40 +11001887 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /*
1889 * Set up the leaf tail from the freespace block.
1890 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001891 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scott0ba962e2006-03-17 17:27:07 +11001892 ltp->bestcount = free->hdr.nvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 /*
1894 * Set up the leaf bests table.
1895 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001896 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
Christoph Hellwig22823962011-07-08 14:35:53 +02001897 be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001898 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 xfs_dir2_leaf_log_tail(tp, lbp);
1900 xfs_dir2_leaf_check(dp, lbp);
1901 /*
1902 * Get rid of the freespace block.
1903 */
1904 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1905 if (error) {
1906 /*
1907 * This can't fail here because it can only happen when
1908 * punching out the middle of an extent, and this is an
1909 * isolated block.
1910 */
1911 ASSERT(error != ENOSPC);
1912 return error;
1913 }
1914 fbp = NULL;
1915 /*
1916 * Now see if we can convert the single-leaf directory
1917 * down to a block form directory.
1918 * This routine always kills the dabuf for the leaf, so
1919 * eliminate it from the path.
1920 */
1921 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1922 state->path.blk[0].bp = NULL;
1923 return error;
1924}