blob: 6c1359dc98988d0015a483ee3b25b9de94cc3369 [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;
496 }
497 /*
498 * Already had space in some data block.
499 * Just read that one in.
500 */
501 else {
Dave Chinner4bb20a82012-11-12 22:54:10 +1100502 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
503 -1, &dbp, XFS_DATA_FORK, NULL);
504 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 }
511 xfs_dir2_data_check(dp, dbp);
512 /*
513 * Point to the biggest freespace in our data block.
514 */
515 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200516 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
Nathan Scottad354eb2006-03-17 17:27:37 +1100517 ASSERT(be16_to_cpu(dup->length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 needscan = needlog = 0;
519 /*
520 * Mark the initial part of our freespace in use for the new entry.
521 */
522 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200523 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 &needlog, &needscan);
525 /*
526 * Initialize our new entry (at last).
527 */
528 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000529 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 dep->namelen = args->namelen;
531 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000532 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200533 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /*
535 * Need to scan fix up the bestfree table.
536 */
537 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200538 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /*
540 * Need to log the data block's header.
541 */
542 if (needlog)
543 xfs_dir2_data_log_header(tp, dbp);
544 xfs_dir2_data_log_entry(tp, dbp, dep);
545 /*
546 * If the bests table needs to be changed, do it.
547 * Log the change unless we've already done that.
548 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200549 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
550 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!grown)
552 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
553 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200554
555 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
556 highstale, &lfloglow, &lfloghigh);
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /*
559 * Fill in the new leaf entry.
560 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100561 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000562 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
Nathan Scott3d693c62006-03-17 17:28:27 +1100563 be16_to_cpu(*tagp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * Log the leaf fields and give up the buffers.
566 */
567 xfs_dir2_leaf_log_header(tp, lbp);
568 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
569 xfs_dir2_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 xfs_dir2_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return 0;
572}
573
574#ifdef DEBUG
575/*
576 * Check the internal consistency of a leaf1 block.
577 * Pop an assert if something is wrong.
578 */
Hannes Eder3180e662009-03-04 19:34:10 +0100579STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580xfs_dir2_leaf_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000581 struct xfs_inode *dp, /* incore directory inode */
582 struct xfs_buf *bp) /* leaf's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 int i; /* leaf index */
585 xfs_dir2_leaf_t *leaf; /* leaf structure */
586 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
587 xfs_mount_t *mp; /* filesystem mount point */
588 int stale; /* count of stale leaves */
589
Dave Chinner1d9025e2012-06-22 18:50:14 +1000590 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 mp = dp->i_mount;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200592 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /*
594 * This value is not restrictive enough.
595 * Should factor in the size of the bests table as well.
596 * We can deduce a value for that from di_size.
597 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000598 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
599 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /*
601 * Leaves and bests don't overlap.
602 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100603 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000604 (char *)xfs_dir2_leaf_bests_p(ltp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /*
606 * Check hash value order, count stale entries.
607 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100608 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
609 if (i + 1 < be16_to_cpu(leaf->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100610 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
611 be32_to_cpu(leaf->ents[i + 1].hashval));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200612 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 stale++;
614 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100615 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617#endif /* DEBUG */
618
619/*
620 * Compact out any stale entries in the leaf.
621 * Log the header and changed leaf entries, if any.
622 */
623void
624xfs_dir2_leaf_compact(
625 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000626 struct xfs_buf *bp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 int from; /* source leaf index */
629 xfs_dir2_leaf_t *leaf; /* leaf structure */
630 int loglow; /* first leaf entry to log */
631 int to; /* target leaf index */
632
Dave Chinner1d9025e2012-06-22 18:50:14 +1000633 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (!leaf->hdr.stale) {
635 return;
636 }
637 /*
638 * Compress out the stale entries in place.
639 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100640 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200641 if (leaf->ents[from].address ==
642 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 continue;
644 /*
645 * Only actually copy the entries that are different.
646 */
647 if (from > to) {
648 if (loglow == -1)
649 loglow = to;
650 leaf->ents[to] = leaf->ents[from];
651 }
652 to++;
653 }
654 /*
655 * Update and log the header, log the leaf entries.
656 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100657 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800658 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 leaf->hdr.stale = 0;
660 xfs_dir2_leaf_log_header(args->trans, bp);
661 if (loglow != -1)
662 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
663}
664
665/*
666 * Compact the leaf entries, removing stale ones.
667 * Leave one stale entry behind - the one closest to our
668 * insertion index - and the caller will shift that one to our insertion
669 * point later.
670 * Return new insertion index, where the remaining stale entry is,
671 * and leaf logging indices.
672 */
673void
674xfs_dir2_leaf_compact_x1(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000675 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int *indexp, /* insertion index */
677 int *lowstalep, /* out: stale entry before us */
678 int *highstalep, /* out: stale entry after us */
679 int *lowlogp, /* out: low log index */
680 int *highlogp) /* out: high log index */
681{
682 int from; /* source copy index */
683 int highstale; /* stale entry at/after index */
684 int index; /* insertion index */
685 int keepstale; /* source index of kept stale */
686 xfs_dir2_leaf_t *leaf; /* leaf structure */
687 int lowstale; /* stale entry before index */
688 int newindex=0; /* new insertion index */
689 int to; /* destination copy index */
690
Dave Chinner1d9025e2012-06-22 18:50:14 +1000691 leaf = bp->b_addr;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100692 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 index = *indexp;
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200694
695 xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /*
698 * Pick the better of lowstale and highstale.
699 */
700 if (lowstale >= 0 &&
Nathan Scotta818e5d2006-03-17 17:28:07 +1100701 (highstale == be16_to_cpu(leaf->hdr.count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 index - lowstale <= highstale - index))
703 keepstale = lowstale;
704 else
705 keepstale = highstale;
706 /*
707 * Copy the entries in place, removing all the stale entries
708 * except keepstale.
709 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100710 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /*
712 * Notice the new value of index.
713 */
714 if (index == from)
715 newindex = to;
716 if (from != keepstale &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200717 leaf->ents[from].address ==
718 cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (from == to)
720 *lowlogp = to;
721 continue;
722 }
723 /*
724 * Record the new keepstale value for the insertion.
725 */
726 if (from == keepstale)
727 lowstale = highstale = to;
728 /*
729 * Copy only the entries that have moved.
730 */
731 if (from > to)
732 leaf->ents[to] = leaf->ents[from];
733 to++;
734 }
735 ASSERT(from > to);
736 /*
737 * If the insertion point was past the last entry,
738 * set the new insertion point accordingly.
739 */
740 if (index == from)
741 newindex = to;
742 *indexp = newindex;
743 /*
744 * Adjust the leaf header values.
745 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800746 be16_add_cpu(&leaf->hdr.count, -(from - to));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100747 leaf->hdr.stale = cpu_to_be16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
749 * Remember the low/high stale value only in the "right"
750 * direction.
751 */
752 if (lowstale >= newindex)
753 lowstale = -1;
754 else
Nathan Scotta818e5d2006-03-17 17:28:07 +1100755 highstale = be16_to_cpu(leaf->hdr.count);
756 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 *lowstalep = lowstale;
758 *highstalep = highstale;
759}
760
Dave Chinner9b73bd72012-06-22 18:50:15 +1000761struct xfs_dir2_leaf_map_info {
762 xfs_extlen_t map_blocks; /* number of fsbs in map */
763 xfs_dablk_t map_off; /* last mapped file offset */
764 int map_size; /* total entries in *map */
765 int map_valid; /* valid entries in *map */
766 int nmap; /* mappings to ask xfs_bmapi */
767 xfs_dir2_db_t curdb; /* db for current block */
768 int ra_current; /* number of read-ahead blks */
769 int ra_index; /* *map index for read-ahead */
770 int ra_offset; /* map entry offset for ra */
771 int ra_want; /* readahead count wanted */
772 struct xfs_bmbt_irec map[]; /* map vector for blocks */
773};
774
775STATIC int
776xfs_dir2_leaf_readbuf(
777 struct xfs_inode *dp,
778 size_t bufsize,
779 struct xfs_dir2_leaf_map_info *mip,
780 xfs_dir2_off_t *curoff,
781 struct xfs_buf **bpp)
782{
783 struct xfs_mount *mp = dp->i_mount;
784 struct xfs_buf *bp = *bpp;
785 struct xfs_bmbt_irec *map = mip->map;
786 int error = 0;
787 int length;
788 int i;
789 int j;
790
791 /*
792 * If we have a buffer, we need to release it and
793 * take it out of the mapping.
794 */
795
796 if (bp) {
797 xfs_trans_brelse(NULL, bp);
798 bp = NULL;
799 mip->map_blocks -= mp->m_dirblkfsbs;
800 /*
801 * Loop to get rid of the extents for the
802 * directory block.
803 */
804 for (i = mp->m_dirblkfsbs; i > 0; ) {
805 j = min_t(int, map->br_blockcount, i);
806 map->br_blockcount -= j;
807 map->br_startblock += j;
808 map->br_startoff += j;
809 /*
810 * If mapping is done, pitch it from
811 * the table.
812 */
813 if (!map->br_blockcount && --mip->map_valid)
814 memmove(&map[0], &map[1],
815 sizeof(map[0]) * mip->map_valid);
816 i -= j;
817 }
818 }
819
820 /*
821 * Recalculate the readahead blocks wanted.
822 */
823 mip->ra_want = howmany(bufsize + mp->m_dirblksize,
824 mp->m_sb.sb_blocksize) - 1;
825 ASSERT(mip->ra_want >= 0);
826
827 /*
828 * If we don't have as many as we want, and we haven't
829 * run out of data blocks, get some more mappings.
830 */
831 if (1 + mip->ra_want > mip->map_blocks &&
832 mip->map_off < xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
833 /*
834 * Get more bmaps, fill in after the ones
835 * we already have in the table.
836 */
837 mip->nmap = mip->map_size - mip->map_valid;
838 error = xfs_bmapi_read(dp, mip->map_off,
839 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET) -
840 mip->map_off,
841 &map[mip->map_valid], &mip->nmap, 0);
842
843 /*
844 * Don't know if we should ignore this or try to return an
845 * error. The trouble with returning errors is that readdir
846 * will just stop without actually passing the error through.
847 */
848 if (error)
849 goto out; /* XXX */
850
851 /*
852 * If we got all the mappings we asked for, set the final map
853 * offset based on the last bmap value received. Otherwise,
854 * we've reached the end.
855 */
856 if (mip->nmap == mip->map_size - mip->map_valid) {
857 i = mip->map_valid + mip->nmap - 1;
858 mip->map_off = map[i].br_startoff + map[i].br_blockcount;
859 } else
860 mip->map_off = xfs_dir2_byte_to_da(mp,
861 XFS_DIR2_LEAF_OFFSET);
862
863 /*
864 * Look for holes in the mapping, and eliminate them. Count up
865 * the valid blocks.
866 */
867 for (i = mip->map_valid; i < mip->map_valid + mip->nmap; ) {
868 if (map[i].br_startblock == HOLESTARTBLOCK) {
869 mip->nmap--;
870 length = mip->map_valid + mip->nmap - i;
871 if (length)
872 memmove(&map[i], &map[i + 1],
873 sizeof(map[i]) * length);
874 } else {
875 mip->map_blocks += map[i].br_blockcount;
876 i++;
877 }
878 }
879 mip->map_valid += mip->nmap;
880 }
881
882 /*
883 * No valid mappings, so no more data blocks.
884 */
885 if (!mip->map_valid) {
886 *curoff = xfs_dir2_da_to_byte(mp, mip->map_off);
887 goto out;
888 }
889
890 /*
891 * Read the directory block starting at the first mapping.
892 */
893 mip->curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
894 error = xfs_da_read_buf(NULL, dp, map->br_startoff,
895 map->br_blockcount >= mp->m_dirblkfsbs ?
896 XFS_FSB_TO_DADDR(mp, map->br_startblock) : -1,
Dave Chinner4bb20a82012-11-12 22:54:10 +1100897 &bp, XFS_DATA_FORK, NULL);
Dave Chinner9b73bd72012-06-22 18:50:15 +1000898
899 /*
900 * Should just skip over the data block instead of giving up.
901 */
902 if (error)
903 goto out; /* XXX */
904
905 /*
906 * Adjust the current amount of read-ahead: we just read a block that
907 * was previously ra.
908 */
909 if (mip->ra_current)
910 mip->ra_current -= mp->m_dirblkfsbs;
911
912 /*
913 * Do we need more readahead?
914 */
915 for (mip->ra_index = mip->ra_offset = i = 0;
916 mip->ra_want > mip->ra_current && i < mip->map_blocks;
917 i += mp->m_dirblkfsbs) {
918 ASSERT(mip->ra_index < mip->map_valid);
919 /*
920 * Read-ahead a contiguous directory block.
921 */
922 if (i > mip->ra_current &&
923 map[mip->ra_index].br_blockcount >= mp->m_dirblkfsbs) {
924 xfs_buf_readahead(mp->m_ddev_targp,
925 XFS_FSB_TO_DADDR(mp,
926 map[mip->ra_index].br_startblock +
927 mip->ra_offset),
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100928 (int)BTOBB(mp->m_dirblksize), NULL);
Dave Chinner9b73bd72012-06-22 18:50:15 +1000929 mip->ra_current = i;
930 }
931
932 /*
933 * Read-ahead a non-contiguous directory block. This doesn't
934 * use our mapping, but this is a very rare case.
935 */
936 else if (i > mip->ra_current) {
937 xfs_da_reada_buf(NULL, dp,
938 map[mip->ra_index].br_startoff +
939 mip->ra_offset,
Dave Chinner4bb20a82012-11-12 22:54:10 +1100940 XFS_DATA_FORK, NULL);
Dave Chinner9b73bd72012-06-22 18:50:15 +1000941 mip->ra_current = i;
942 }
943
944 /*
945 * Advance offset through the mapping table.
946 */
947 for (j = 0; j < mp->m_dirblkfsbs; j++) {
948 /*
949 * The rest of this extent but not more than a dir
950 * block.
951 */
952 length = min_t(int, mp->m_dirblkfsbs,
953 map[mip->ra_index].br_blockcount -
954 mip->ra_offset);
955 j += length;
956 mip->ra_offset += length;
957
958 /*
959 * Advance to the next mapping if this one is used up.
960 */
961 if (mip->ra_offset == map[mip->ra_index].br_blockcount) {
962 mip->ra_offset = 0;
963 mip->ra_index++;
964 }
965 }
966 }
967
968out:
969 *bpp = bp;
970 return error;
971}
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973/*
974 * Getdents (readdir) for leaf and node directories.
975 * This reads the data blocks only, so is the same for both forms.
976 */
977int /* error */
978xfs_dir2_leaf_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 xfs_inode_t *dp, /* incore directory inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000980 void *dirent,
981 size_t bufsize,
982 xfs_off_t *offset,
983 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Dave Chinner9b73bd72012-06-22 18:50:15 +1000985 struct xfs_buf *bp = NULL; /* data block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200986 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 xfs_dir2_data_entry_t *dep; /* data entry */
988 xfs_dir2_data_unused_t *dup; /* unused entry */
Nathan Scottf6d75cb2006-03-14 13:32:24 +1100989 int error = 0; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 int length; /* temporary length value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 xfs_mount_t *mp; /* filesystem mount point */
Dave Chinner9b73bd72012-06-22 18:50:15 +1000992 int byteoff; /* offset in current block */
993 xfs_dir2_off_t curoff; /* current overall offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 xfs_dir2_off_t newoff; /* new curoff after new blk */
Nathan Scottf6d75cb2006-03-14 13:32:24 +1100995 char *ptr = NULL; /* pointer to current data */
Dave Chinner9b73bd72012-06-22 18:50:15 +1000996 struct xfs_dir2_leaf_map_info *map_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 /*
999 * If the offset is at or past the largest allowed value,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001000 * give up right away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001002 if (*offset >= XFS_DIR2_MAX_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return 0;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 mp = dp->i_mount;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /*
1008 * Set up to bmap a number of blocks based on the caller's
1009 * buffer size, the directory block size, and the filesystem
1010 * block size.
1011 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001012 length = howmany(bufsize + mp->m_dirblksize,
1013 mp->m_sb.sb_blocksize);
1014 map_info = kmem_zalloc(offsetof(struct xfs_dir2_leaf_map_info, map) +
1015 (length * sizeof(struct xfs_bmbt_irec)),
1016 KM_SLEEP);
1017 map_info->map_size = length;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /*
1020 * Inside the loop we keep the main offset value as a byte offset
1021 * in the directory file.
1022 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001023 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /*
1026 * Force this conversion through db so we truncate the offset
1027 * down to get the start of the data block.
1028 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001029 map_info->map_off = xfs_dir2_db_to_da(mp,
1030 xfs_dir2_byte_to_db(mp, curoff));
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /*
1033 * Loop over directory entries until we reach the end offset.
1034 * Get more blocks and readahead as necessary.
1035 */
1036 while (curoff < XFS_DIR2_LEAF_OFFSET) {
1037 /*
1038 * If we have no buffer, or we're off the end of the
1039 * current buffer, need to get another one.
1040 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001041 if (!bp || ptr >= (char *)bp->b_addr + mp->m_dirblksize) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001042
Dave Chinner9b73bd72012-06-22 18:50:15 +10001043 error = xfs_dir2_leaf_readbuf(dp, bufsize, map_info,
1044 &curoff, &bp);
1045 if (error || !map_info->map_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 break;
Dave Chinner9b73bd72012-06-22 18:50:15 +10001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /*
1049 * Having done a read, we need to set a new offset.
1050 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001051 newoff = xfs_dir2_db_off_to_byte(mp, map_info->curdb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 /*
1053 * Start of the current block.
1054 */
1055 if (curoff < newoff)
1056 curoff = newoff;
1057 /*
1058 * Make sure we're in the right block.
1059 */
1060 else if (curoff > newoff)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001061 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
Dave Chinner9b73bd72012-06-22 18:50:15 +10001062 map_info->curdb);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001063 hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 xfs_dir2_data_check(dp, bp);
1065 /*
1066 * Find our position in the block.
1067 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +02001068 ptr = (char *)(hdr + 1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001069 byteoff = xfs_dir2_byte_to_off(mp, curoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /*
1071 * Skip past the header.
1072 */
1073 if (byteoff == 0)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001074 curoff += (uint)sizeof(*hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 /*
1076 * Skip past entries until we reach our offset.
1077 */
1078 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001079 while ((char *)ptr - (char *)hdr < byteoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 dup = (xfs_dir2_data_unused_t *)ptr;
1081
Nathan Scottad354eb2006-03-17 17:27:37 +11001082 if (be16_to_cpu(dup->freetag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 == XFS_DIR2_DATA_FREE_TAG) {
1084
Nathan Scottad354eb2006-03-17 17:27:37 +11001085 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 ptr += length;
1087 continue;
1088 }
1089 dep = (xfs_dir2_data_entry_t *)ptr;
1090 length =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001091 xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 ptr += length;
1093 }
1094 /*
1095 * Now set our real offset.
1096 */
1097 curoff =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001098 xfs_dir2_db_off_to_byte(mp,
1099 xfs_dir2_byte_to_db(mp, curoff),
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001100 (char *)ptr - (char *)hdr);
1101 if (ptr >= (char *)hdr + mp->m_dirblksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 continue;
1103 }
1104 }
1105 }
1106 /*
1107 * We have a pointer to an entry.
1108 * Is it a live one?
1109 */
1110 dup = (xfs_dir2_data_unused_t *)ptr;
1111 /*
1112 * No, it's unused, skip over it.
1113 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001114 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1115 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 ptr += length;
1117 curoff += length;
1118 continue;
1119 }
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 dep = (xfs_dir2_data_entry_t *)ptr;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001122 length = xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Dave Chinner4a24cb72010-01-20 10:48:05 +11001124 if (filldir(dirent, (char *)dep->name, dep->namelen,
Christoph Hellwig15440312009-01-08 14:00:00 -05001125 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
Christoph Hellwiga19d9f82009-03-29 09:51:08 +02001126 be64_to_cpu(dep->inumber), DT_UNKNOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 break;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /*
1130 * Advance to next entry in the block.
1131 */
1132 ptr += length;
1133 curoff += length;
Eric Sandeen8e69ce12009-09-25 19:42:26 +00001134 /* bufsize may have just been a guess; don't go negative */
1135 bufsize = bufsize > length ? bufsize - length : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137
1138 /*
1139 * All done. Set output offset value to current offset.
1140 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001141 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
Christoph Hellwig15440312009-01-08 14:00:00 -05001142 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 else
Christoph Hellwig15440312009-01-08 14:00:00 -05001144 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
Dave Chinner9b73bd72012-06-22 18:50:15 +10001145 kmem_free(map_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (bp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001147 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return error;
1149}
1150
1151/*
1152 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1153 */
1154int
1155xfs_dir2_leaf_init(
1156 xfs_da_args_t *args, /* operation arguments */
1157 xfs_dir2_db_t bno, /* directory block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001158 struct xfs_buf **bpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int magic) /* magic number for block */
1160{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001161 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 xfs_inode_t *dp; /* incore directory inode */
1163 int error; /* error return code */
1164 xfs_dir2_leaf_t *leaf; /* leaf structure */
1165 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1166 xfs_mount_t *mp; /* filesystem mount point */
1167 xfs_trans_t *tp; /* transaction pointer */
1168
1169 dp = args->dp;
1170 ASSERT(dp != NULL);
1171 tp = args->trans;
1172 mp = dp->i_mount;
1173 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1174 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1175 /*
1176 * Get the buffer for the block.
1177 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001178 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 XFS_DATA_FORK);
1180 if (error) {
1181 return error;
1182 }
1183 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001184 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /*
1186 * Initialize the header.
1187 */
Nathan Scott89da0542006-03-17 17:28:40 +11001188 leaf->hdr.info.magic = cpu_to_be16(magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 leaf->hdr.info.forw = 0;
1190 leaf->hdr.info.back = 0;
1191 leaf->hdr.count = 0;
1192 leaf->hdr.stale = 0;
1193 xfs_dir2_leaf_log_header(tp, bp);
1194 /*
1195 * If it's a leaf-format directory initialize the tail.
1196 * In this case our caller has the real bests table to copy into
1197 * the block.
1198 */
1199 if (magic == XFS_DIR2_LEAF1_MAGIC) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001200 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 ltp->bestcount = 0;
1202 xfs_dir2_leaf_log_tail(tp, bp);
1203 }
1204 *bpp = bp;
1205 return 0;
1206}
1207
1208/*
1209 * Log the bests entries indicated from a leaf1 block.
1210 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001211static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212xfs_dir2_leaf_log_bests(
1213 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001214 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int first, /* first entry to log */
1216 int last) /* last entry to log */
1217{
Nathan Scott68b3a102006-03-17 17:27:19 +11001218 __be16 *firstb; /* pointer to first entry */
1219 __be16 *lastb; /* pointer to last entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 xfs_dir2_leaf_t *leaf; /* leaf structure */
1221 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1222
Dave Chinner1d9025e2012-06-22 18:50:14 +10001223 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001224 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001225 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1226 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1227 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001228 xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1230}
1231
1232/*
1233 * Log the leaf entries indicated from a leaf1 or leafn block.
1234 */
1235void
1236xfs_dir2_leaf_log_ents(
1237 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001238 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int first, /* first entry to log */
1240 int last) /* last entry to log */
1241{
1242 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1243 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1244 xfs_dir2_leaf_t *leaf; /* leaf structure */
1245
Dave Chinner1d9025e2012-06-22 18:50:14 +10001246 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001247 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1248 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 firstlep = &leaf->ents[first];
1250 lastlep = &leaf->ents[last];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001251 xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1253}
1254
1255/*
1256 * Log the header of the leaf1 or leafn block.
1257 */
1258void
1259xfs_dir2_leaf_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001260 struct xfs_trans *tp,
1261 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 xfs_dir2_leaf_t *leaf; /* leaf structure */
1264
Dave Chinner1d9025e2012-06-22 18:50:14 +10001265 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001266 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1267 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001268 xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 (uint)(sizeof(leaf->hdr) - 1));
1270}
1271
1272/*
1273 * Log the tail of the leaf1 block.
1274 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001275STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276xfs_dir2_leaf_log_tail(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001277 struct xfs_trans *tp,
1278 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 xfs_dir2_leaf_t *leaf; /* leaf structure */
1281 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1282 xfs_mount_t *mp; /* filesystem mount point */
1283
1284 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001285 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001286 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001287 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001288 xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 (uint)(mp->m_dirblksize - 1));
1290}
1291
1292/*
1293 * Look up the entry referred to by args in the leaf format directory.
1294 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1295 * is also used by the node-format code.
1296 */
1297int
1298xfs_dir2_leaf_lookup(
1299 xfs_da_args_t *args) /* operation arguments */
1300{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001301 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 xfs_dir2_data_entry_t *dep; /* data block entry */
1303 xfs_inode_t *dp; /* incore directory inode */
1304 int error; /* error return code */
1305 int index; /* found entry index */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001306 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 xfs_dir2_leaf_t *leaf; /* leaf structure */
1308 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1309 xfs_trans_t *tp; /* transaction pointer */
1310
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001311 trace_xfs_dir2_leaf_lookup(args);
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /*
1314 * Look up name in the leaf block, returning both buffers and index.
1315 */
1316 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1317 return error;
1318 }
1319 tp = args->trans;
1320 dp = args->dp;
1321 xfs_dir2_leaf_check(dp, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001322 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /*
1324 * Get to the leaf entry and contained data entry address.
1325 */
1326 lep = &leaf->ents[index];
1327 /*
1328 * Point to the data entry.
1329 */
1330 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001331 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001332 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +10001334 * Return the found inode number & CI name if appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001336 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001337 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001338 xfs_trans_brelse(tp, dbp);
1339 xfs_trans_brelse(tp, lbp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001340 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
1343/*
1344 * Look up name/hash in the leaf block.
1345 * Fill in indexp with the found index, and dbpp with the data buffer.
1346 * If not found dbpp will be NULL, and ENOENT comes back.
1347 * lbpp will always be filled in with the leaf buffer unless there's an error.
1348 */
1349static int /* error */
1350xfs_dir2_leaf_lookup_int(
1351 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001352 struct xfs_buf **lbpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 int *indexp, /* out: index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001354 struct xfs_buf **dbpp) /* out: data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001356 xfs_dir2_db_t curdb = -1; /* current data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001357 struct xfs_buf *dbp = NULL; /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 xfs_dir2_data_entry_t *dep; /* data entry */
1359 xfs_inode_t *dp; /* incore directory inode */
1360 int error; /* error return code */
1361 int index; /* index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001362 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1364 xfs_dir2_leaf_t *leaf; /* leaf structure */
1365 xfs_mount_t *mp; /* filesystem mount point */
1366 xfs_dir2_db_t newdb; /* new data block number */
1367 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001368 xfs_dir2_db_t cidb = -1; /* case match data block no. */
Barry Naujok5163f952008-05-21 16:41:01 +10001369 enum xfs_dacmp cmp; /* name compare result */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 dp = args->dp;
1372 tp = args->trans;
1373 mp = dp->i_mount;
1374 /*
1375 * Read the leaf block into the buffer.
1376 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001377 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
Dave Chinner4bb20a82012-11-12 22:54:10 +11001378 XFS_DATA_FORK, NULL);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001379 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 *lbpp = lbp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001382 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 xfs_dir2_leaf_check(dp, lbp);
1384 /*
1385 * Look for the first leaf entry with our hash value.
1386 */
1387 index = xfs_dir2_leaf_search_hash(args, lbp);
1388 /*
1389 * Loop over all the entries with the right hash value
1390 * looking to match the name.
1391 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001392 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
Barry Naujok5163f952008-05-21 16:41:01 +10001393 be32_to_cpu(lep->hashval) == args->hashval;
1394 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 /*
1396 * Skip over stale leaf entries.
1397 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001398 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 continue;
1400 /*
1401 * Get the new data block number.
1402 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001403 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /*
1405 * If it's not the same as the old data block number,
1406 * need to pitch the old one and read the new one.
1407 */
1408 if (newdb != curdb) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001409 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001410 xfs_trans_brelse(tp, dbp);
Barry Naujok5163f952008-05-21 16:41:01 +10001411 error = xfs_da_read_buf(tp, dp,
1412 xfs_dir2_db_to_da(mp, newdb),
Dave Chinner4bb20a82012-11-12 22:54:10 +11001413 -1, &dbp, XFS_DATA_FORK, NULL);
Barry Naujok5163f952008-05-21 16:41:01 +10001414 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001415 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return error;
1417 }
1418 xfs_dir2_data_check(dp, dbp);
1419 curdb = newdb;
1420 }
1421 /*
1422 * Point to the data entry.
1423 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001424 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
Barry Naujok5163f952008-05-21 16:41:01 +10001425 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 /*
Barry Naujok5163f952008-05-21 16:41:01 +10001427 * Compare name and if it's an exact match, return the index
1428 * and buffer. If it's the first case-insensitive match, store
1429 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 */
Barry Naujok5163f952008-05-21 16:41:01 +10001431 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1432 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1433 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 *indexp = index;
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001435 /* case exact match: return the current buffer. */
Barry Naujok5163f952008-05-21 16:41:01 +10001436 if (cmp == XFS_CMP_EXACT) {
Barry Naujok5163f952008-05-21 16:41:01 +10001437 *dbpp = dbp;
1438 return 0;
1439 }
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001440 cidb = curdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442 }
Barry Naujok6a178102008-05-21 16:42:05 +10001443 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +10001444 /*
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001445 * Here, we can only be doing a lookup (not a rename or remove).
1446 * If a case-insensitive match was found earlier, re-read the
1447 * appropriate data block if required and return it.
Barry Naujok5163f952008-05-21 16:41:01 +10001448 */
1449 if (args->cmpresult == XFS_CMP_CASE) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001450 ASSERT(cidb != -1);
1451 if (cidb != curdb) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001452 xfs_trans_brelse(tp, dbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001453 error = xfs_da_read_buf(tp, dp,
1454 xfs_dir2_db_to_da(mp, cidb),
Dave Chinner4bb20a82012-11-12 22:54:10 +11001455 -1, &dbp, XFS_DATA_FORK, NULL);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001456 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001457 xfs_trans_brelse(tp, lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001458 return error;
1459 }
1460 }
1461 *dbpp = dbp;
Barry Naujok5163f952008-05-21 16:41:01 +10001462 return 0;
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 /*
1465 * No match found, return ENOENT.
1466 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001467 ASSERT(cidb == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001469 xfs_trans_brelse(tp, dbp);
1470 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return XFS_ERROR(ENOENT);
1472}
1473
1474/*
1475 * Remove an entry from a leaf format directory.
1476 */
1477int /* error */
1478xfs_dir2_leaf_removename(
1479 xfs_da_args_t *args) /* operation arguments */
1480{
Nathan Scott68b3a102006-03-17 17:27:19 +11001481 __be16 *bestsp; /* leaf block best freespace */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001482 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001484 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 xfs_dir2_data_entry_t *dep; /* data entry structure */
1486 xfs_inode_t *dp; /* incore directory inode */
1487 int error; /* error return code */
1488 xfs_dir2_db_t i; /* temporary data block # */
1489 int index; /* index into leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001490 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 xfs_dir2_leaf_t *leaf; /* leaf structure */
1492 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1493 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1494 xfs_mount_t *mp; /* filesystem mount point */
1495 int needlog; /* need to log data header */
1496 int needscan; /* need to rescan data frees */
1497 xfs_dir2_data_off_t oldbest; /* old value of best free */
1498 xfs_trans_t *tp; /* transaction pointer */
1499
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001500 trace_xfs_dir2_leaf_removename(args);
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 /*
1503 * Lookup the leaf entry, get the leaf and data blocks read in.
1504 */
1505 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1506 return error;
1507 }
1508 dp = args->dp;
1509 tp = args->trans;
1510 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001511 leaf = lbp->b_addr;
1512 hdr = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 xfs_dir2_data_check(dp, dbp);
1514 /*
1515 * Point to the leaf entry, use that to point to the data entry.
1516 */
1517 lep = &leaf->ents[index];
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001518 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001520 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 needscan = needlog = 0;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001522 oldbest = be16_to_cpu(hdr->bestfree[0].length);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001523 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1524 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott68b3a102006-03-17 17:27:19 +11001525 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 /*
1527 * Mark the former data entry unused.
1528 */
1529 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001530 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001531 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 /*
1533 * We just mark the leaf entry stale by putting a null in it.
1534 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001535 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001537 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1539 /*
1540 * Scan the freespace in the data block again if necessary,
1541 * log the data block header if necessary.
1542 */
1543 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001544 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (needlog)
1546 xfs_dir2_data_log_header(tp, dbp);
1547 /*
1548 * If the longest freespace in the data block has changed,
1549 * put the new value in the bests table and log that.
1550 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001551 if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1552 bestsp[db] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1554 }
1555 xfs_dir2_data_check(dp, dbp);
1556 /*
1557 * If the data block is now empty then get rid of the data block.
1558 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001559 if (be16_to_cpu(hdr->bestfree[0].length) ==
1560 mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 ASSERT(db != mp->m_dirdatablk);
1562 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1563 /*
1564 * Nope, can't get rid of it because it caused
1565 * allocation of a bmap btree block to do so.
1566 * Just go on, returning success, leaving the
1567 * empty block in place.
1568 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001569 if (error == ENOSPC && args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 xfs_dir2_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return error;
1573 }
1574 dbp = NULL;
1575 /*
1576 * If this is the last data block then compact the
1577 * bests table by getting rid of entries.
1578 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001579 if (db == be32_to_cpu(ltp->bestcount) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 /*
1581 * Look for the last active entry (i).
1582 */
1583 for (i = db - 1; i > 0; i--) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001584 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 break;
1586 }
1587 /*
1588 * Copy the table down so inactive entries at the
1589 * end are removed.
1590 */
1591 memmove(&bestsp[db - i], bestsp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001592 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001593 be32_add_cpu(&ltp->bestcount, -(db - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001595 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 } else
Nathan Scott68b3a102006-03-17 17:27:19 +11001597 bestsp[db] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599 /*
1600 * If the data block was not the first one, drop it.
1601 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001602 else if (db != mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 dbp = NULL;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 xfs_dir2_leaf_check(dp, lbp);
1606 /*
1607 * See if we can convert to block form.
1608 */
1609 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1610}
1611
1612/*
1613 * Replace the inode number in a leaf format directory entry.
1614 */
1615int /* error */
1616xfs_dir2_leaf_replace(
1617 xfs_da_args_t *args) /* operation arguments */
1618{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001619 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 xfs_dir2_data_entry_t *dep; /* data block entry */
1621 xfs_inode_t *dp; /* incore directory inode */
1622 int error; /* error return code */
1623 int index; /* index of leaf entry */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001624 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 xfs_dir2_leaf_t *leaf; /* leaf structure */
1626 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1627 xfs_trans_t *tp; /* transaction pointer */
1628
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001629 trace_xfs_dir2_leaf_replace(args);
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /*
1632 * Look up the entry.
1633 */
1634 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1635 return error;
1636 }
1637 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001638 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /*
1640 * Point to the leaf entry, get data address from it.
1641 */
1642 lep = &leaf->ents[index];
1643 /*
1644 * Point to the data entry.
1645 */
1646 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001647 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001648 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001649 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 /*
1651 * Put the new inode number in, log it.
1652 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001653 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 tp = args->trans;
1655 xfs_dir2_data_log_entry(tp, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 xfs_dir2_leaf_check(dp, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001657 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return 0;
1659}
1660
1661/*
1662 * Return index in the leaf block (lbp) which is either the first
1663 * one with this hash value, or if there are none, the insert point
1664 * for that hash value.
1665 */
1666int /* index value */
1667xfs_dir2_leaf_search_hash(
1668 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001669 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 xfs_dahash_t hash=0; /* hash from this entry */
1672 xfs_dahash_t hashwant; /* hash value looking for */
1673 int high; /* high leaf index */
1674 int low; /* low leaf index */
1675 xfs_dir2_leaf_t *leaf; /* leaf structure */
1676 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1677 int mid=0; /* current leaf index */
1678
Dave Chinner1d9025e2012-06-22 18:50:14 +10001679 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680#ifndef __KERNEL__
1681 if (!leaf->hdr.count)
1682 return 0;
1683#endif
1684 /*
1685 * Note, the table cannot be empty, so we have to go through the loop.
1686 * Binary search the leaf entries looking for our hash value.
1687 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001688 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 hashwant = args->hashval;
1690 low <= high; ) {
1691 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001692 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 break;
1694 if (hash < hashwant)
1695 low = mid + 1;
1696 else
1697 high = mid - 1;
1698 }
1699 /*
1700 * Found one, back up through all the equal hash values.
1701 */
1702 if (hash == hashwant) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001703 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 mid--;
1705 }
1706 }
1707 /*
1708 * Need to point to an entry higher than ours.
1709 */
1710 else if (hash < hashwant)
1711 mid++;
1712 return mid;
1713}
1714
1715/*
1716 * Trim off a trailing data block. We know it's empty since the leaf
1717 * freespace table says so.
1718 */
1719int /* error */
1720xfs_dir2_leaf_trim_data(
1721 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001722 struct xfs_buf *lbp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 xfs_dir2_db_t db) /* data block number */
1724{
Nathan Scott68b3a102006-03-17 17:27:19 +11001725 __be16 *bestsp; /* leaf bests table */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001726 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 xfs_inode_t *dp; /* incore directory inode */
1728 int error; /* error return value */
1729 xfs_dir2_leaf_t *leaf; /* leaf structure */
1730 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1731 xfs_mount_t *mp; /* filesystem mount point */
1732 xfs_trans_t *tp; /* transaction pointer */
1733
1734 dp = args->dp;
1735 mp = dp->i_mount;
1736 tp = args->trans;
1737 /*
1738 * Read the offending data block. We need its buffer.
1739 */
Dave Chinner4bb20a82012-11-12 22:54:10 +11001740 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
1741 XFS_DATA_FORK, NULL);
1742 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Dave Chinner1d9025e2012-06-22 18:50:14 +10001745 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001746 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001747
1748#ifdef DEBUG
1749{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001750 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001751
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001752 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001753 ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
1754 mp->m_dirblksize - (uint)sizeof(*hdr));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001755 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001756}
1757#endif
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 /*
1760 * Get rid of the data block.
1761 */
1762 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1763 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001764 xfs_trans_brelse(tp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return error;
1766 }
1767 /*
1768 * Eliminate the last bests entry from the table.
1769 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001770 bestsp = xfs_dir2_leaf_bests_p(ltp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001771 be32_add_cpu(&ltp->bestcount, -1);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001772 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001774 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return 0;
1776}
1777
Christoph Hellwig22823962011-07-08 14:35:53 +02001778static inline size_t
1779xfs_dir2_leaf_size(
1780 struct xfs_dir2_leaf_hdr *hdr,
1781 int counts)
1782{
1783 int entries;
1784
1785 entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
1786 return sizeof(xfs_dir2_leaf_hdr_t) +
1787 entries * sizeof(xfs_dir2_leaf_entry_t) +
1788 counts * sizeof(xfs_dir2_data_off_t) +
1789 sizeof(xfs_dir2_leaf_tail_t);
1790}
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792/*
1793 * Convert node form directory to leaf form directory.
1794 * The root of the node form dir needs to already be a LEAFN block.
1795 * Just return if we can't do anything.
1796 */
1797int /* error */
1798xfs_dir2_node_to_leaf(
1799 xfs_da_state_t *state) /* directory operation state */
1800{
1801 xfs_da_args_t *args; /* operation arguments */
1802 xfs_inode_t *dp; /* incore directory inode */
1803 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001804 struct xfs_buf *fbp; /* buffer for freespace block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 xfs_fileoff_t fo; /* freespace file offset */
1806 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001807 struct xfs_buf *lbp; /* buffer for leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1809 xfs_dir2_leaf_t *leaf; /* leaf structure */
1810 xfs_mount_t *mp; /* filesystem mount point */
1811 int rval; /* successful free trim? */
1812 xfs_trans_t *tp; /* transaction pointer */
1813
1814 /*
1815 * There's more than a leaf level in the btree, so there must
1816 * be multiple leafn blocks. Give up.
1817 */
1818 if (state->path.active > 1)
1819 return 0;
1820 args = state->args;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001821
1822 trace_xfs_dir2_node_to_leaf(args);
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 mp = state->mp;
1825 dp = args->dp;
1826 tp = args->trans;
1827 /*
1828 * Get the last offset in the file.
1829 */
1830 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1831 return error;
1832 }
1833 fo -= mp->m_dirblkfsbs;
1834 /*
1835 * If there are freespace blocks other than the first one,
1836 * take this opportunity to remove trailing empty freespace blocks
1837 * that may have been left behind during no-space-reservation
1838 * operations.
1839 */
1840 while (fo > mp->m_dirfreeblk) {
1841 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1842 return error;
1843 }
1844 if (rval)
1845 fo -= mp->m_dirblkfsbs;
1846 else
1847 return 0;
1848 }
1849 /*
1850 * Now find the block just before the freespace block.
1851 */
1852 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1853 return error;
1854 }
1855 /*
1856 * If it's not the single leaf block, give up.
1857 */
1858 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1859 return 0;
1860 lbp = state->path.blk[0].bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001861 leaf = lbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001862 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 /*
1864 * Read the freespace block.
1865 */
Dave Chinner20252072012-11-12 22:54:13 +11001866 error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001867 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001869 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001870 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 ASSERT(!free->hdr.firstdb);
Christoph Hellwig22823962011-07-08 14:35:53 +02001872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 /*
1874 * Now see if the leafn and free data will fit in a leaf1.
1875 * If not, release the buffer and give up.
1876 */
Christoph Hellwig22823962011-07-08 14:35:53 +02001877 if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
1878 mp->m_dirblksize) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001879 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return 0;
1881 }
Christoph Hellwig22823962011-07-08 14:35:53 +02001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 /*
1884 * If the leaf has any stale entries in it, compress them out.
1885 * The compact routine will log the header.
1886 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001887 if (be16_to_cpu(leaf->hdr.stale))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 xfs_dir2_leaf_compact(args, lbp);
1889 else
1890 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott89da0542006-03-17 17:28:40 +11001891 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 /*
1893 * Set up the leaf tail from the freespace block.
1894 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001895 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scott0ba962e2006-03-17 17:27:07 +11001896 ltp->bestcount = free->hdr.nvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /*
1898 * Set up the leaf bests table.
1899 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001900 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
Christoph Hellwig22823962011-07-08 14:35:53 +02001901 be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001902 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 xfs_dir2_leaf_log_tail(tp, lbp);
1904 xfs_dir2_leaf_check(dp, lbp);
1905 /*
1906 * Get rid of the freespace block.
1907 */
1908 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1909 if (error) {
1910 /*
1911 * This can't fail here because it can only happen when
1912 * punching out the middle of an extent, and this is an
1913 * isolated block.
1914 */
1915 ASSERT(error != ENOSPC);
1916 return error;
1917 }
1918 fbp = NULL;
1919 /*
1920 * Now see if we can convert the single-leaf directory
1921 * down to a block form directory.
1922 * This routine always kills the dabuf for the leaf, so
1923 * eliminate it from the path.
1924 */
1925 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1926 state->path.blk[0].bp = NULL;
1927 return error;
1928}