blob: 69accf6cbc4619b1f299960711236bf5f5ae42f0 [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,
318 XFS_DATA_FORK);
319 if (error) {
320 return error;
321 }
322 ASSERT(lbp != NULL);
323 /*
324 * Look up the entry by hash value and name.
325 * We know it's not there, our caller has already done a lookup.
326 * So the index is of the entry to insert in front of.
327 * But if there are dup hash values the index is of the first of those.
328 */
329 index = xfs_dir2_leaf_search_hash(args, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000330 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000331 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
332 bestsp = xfs_dir2_leaf_bests_p(ltp);
333 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /*
335 * See if there are any entries with the same hash value
336 * and space in their block for the new entry.
337 * This is good because it puts multiple same-hash value entries
338 * in a data block, improving the lookup of those entries.
339 */
340 for (use_block = -1, lep = &leaf->ents[index];
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100341 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 index++, lep++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100343 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 continue;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000345 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100346 ASSERT(i < be32_to_cpu(ltp->bestcount));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200347 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
Nathan Scott68b3a102006-03-17 17:27:19 +1100348 if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 use_block = i;
350 break;
351 }
352 }
353 /*
354 * Didn't find a block yet, linear search all the data blocks.
355 */
356 if (use_block == -1) {
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100357 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /*
359 * Remember a block we see that's missing.
360 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200361 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
362 use_block == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 use_block = i;
Nathan Scott68b3a102006-03-17 17:27:19 +1100364 else if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 use_block = i;
366 break;
367 }
368 }
369 }
370 /*
371 * How many bytes do we need in the leaf block?
372 */
Christoph Hellwig22823962011-07-08 14:35:53 +0200373 needbytes = 0;
374 if (!leaf->hdr.stale)
375 needbytes += sizeof(xfs_dir2_leaf_entry_t);
376 if (use_block == -1)
377 needbytes += sizeof(xfs_dir2_data_off_t);
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /*
380 * Now kill use_block if it refers to a missing block, so we
381 * can use it as an indication of allocation needed.
382 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200383 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 use_block = -1;
385 /*
386 * If we don't have enough free bytes but we can make enough
387 * by compacting out stale entries, we'll do that.
388 */
Barry Naujok6a178102008-05-21 16:42:05 +1000389 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
390 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 compact = 1;
392 }
393 /*
394 * Otherwise if we don't have enough free bytes we need to
395 * convert to node form.
396 */
Barry Naujok6a178102008-05-21 16:42:05 +1000397 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
398 leaf->hdr.count)] < needbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /*
400 * Just checking or no space reservation, give up.
401 */
Barry Naujok6a178102008-05-21 16:42:05 +1000402 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
403 args->total == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000404 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return XFS_ERROR(ENOSPC);
406 }
407 /*
408 * Convert to node form.
409 */
410 error = xfs_dir2_leaf_to_node(args, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (error)
412 return error;
413 /*
414 * Then add the new entry.
415 */
416 return xfs_dir2_node_addname(args);
417 }
418 /*
419 * Otherwise it will fit without compaction.
420 */
421 else
422 compact = 0;
423 /*
424 * If just checking, then it will fit unless we needed to allocate
425 * a new data block.
426 */
Barry Naujok6a178102008-05-21 16:42:05 +1000427 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000428 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
430 }
431 /*
432 * If no allocations are allowed, return now before we've
433 * changed anything.
434 */
435 if (args->total == 0 && use_block == -1) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000436 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return XFS_ERROR(ENOSPC);
438 }
439 /*
440 * Need to compact the leaf entries, removing stale ones.
441 * Leave one stale entry behind - the one closest to our
442 * insertion index - and we'll shift that one to our insertion
443 * point later.
444 */
445 if (compact) {
446 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
447 &lfloglow, &lfloghigh);
448 }
449 /*
450 * There are stale entries, so we'll need log-low and log-high
451 * impossibly bad values later.
452 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100453 else if (be16_to_cpu(leaf->hdr.stale)) {
454 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 lfloghigh = -1;
456 }
457 /*
458 * If there was no data block space found, we need to allocate
459 * a new one.
460 */
461 if (use_block == -1) {
462 /*
463 * Add the new data block.
464 */
465 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
466 &use_block))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000467 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return error;
469 }
470 /*
471 * Initialize the block.
472 */
473 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000474 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return error;
476 }
477 /*
478 * If we're adding a new data block on the end we need to
479 * extend the bests table. Copy it up one entry.
480 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100481 if (use_block >= be32_to_cpu(ltp->bestcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 bestsp--;
483 memmove(&bestsp[0], &bestsp[1],
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100484 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800485 be32_add_cpu(&ltp->bestcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100487 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 /*
490 * If we're filling in a previously empty block just log it.
491 */
492 else
493 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000494 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200495 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 grown = 1;
497 }
498 /*
499 * Already had space in some data block.
500 * Just read that one in.
501 */
502 else {
503 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000504 xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 -1, &dbp, XFS_DATA_FORK))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000506 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return error;
508 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000509 hdr = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 grown = 0;
511 }
512 xfs_dir2_data_check(dp, dbp);
513 /*
514 * Point to the biggest freespace in our data block.
515 */
516 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200517 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
Nathan Scottad354eb2006-03-17 17:27:37 +1100518 ASSERT(be16_to_cpu(dup->length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 needscan = needlog = 0;
520 /*
521 * Mark the initial part of our freespace in use for the new entry.
522 */
523 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200524 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 &needlog, &needscan);
526 /*
527 * Initialize our new entry (at last).
528 */
529 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000530 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 dep->namelen = args->namelen;
532 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000533 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200534 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /*
536 * Need to scan fix up the bestfree table.
537 */
538 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200539 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /*
541 * Need to log the data block's header.
542 */
543 if (needlog)
544 xfs_dir2_data_log_header(tp, dbp);
545 xfs_dir2_data_log_entry(tp, dbp, dep);
546 /*
547 * If the bests table needs to be changed, do it.
548 * Log the change unless we've already done that.
549 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200550 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
551 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (!grown)
553 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
554 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200555
556 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
557 highstale, &lfloglow, &lfloghigh);
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /*
560 * Fill in the new leaf entry.
561 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100562 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000563 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
Nathan Scott3d693c62006-03-17 17:28:27 +1100564 be16_to_cpu(*tagp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /*
566 * Log the leaf fields and give up the buffers.
567 */
568 xfs_dir2_leaf_log_header(tp, lbp);
569 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
570 xfs_dir2_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 xfs_dir2_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return 0;
573}
574
575#ifdef DEBUG
576/*
577 * Check the internal consistency of a leaf1 block.
578 * Pop an assert if something is wrong.
579 */
Hannes Eder3180e662009-03-04 19:34:10 +0100580STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581xfs_dir2_leaf_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000582 struct xfs_inode *dp, /* incore directory inode */
583 struct xfs_buf *bp) /* leaf's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 int i; /* leaf index */
586 xfs_dir2_leaf_t *leaf; /* leaf structure */
587 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
588 xfs_mount_t *mp; /* filesystem mount point */
589 int stale; /* count of stale leaves */
590
Dave Chinner1d9025e2012-06-22 18:50:14 +1000591 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 mp = dp->i_mount;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200593 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /*
595 * This value is not restrictive enough.
596 * Should factor in the size of the bests table as well.
597 * We can deduce a value for that from di_size.
598 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000599 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
600 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /*
602 * Leaves and bests don't overlap.
603 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100604 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000605 (char *)xfs_dir2_leaf_bests_p(ltp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /*
607 * Check hash value order, count stale entries.
608 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100609 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
610 if (i + 1 < be16_to_cpu(leaf->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100611 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
612 be32_to_cpu(leaf->ents[i + 1].hashval));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200613 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 stale++;
615 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100616 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618#endif /* DEBUG */
619
620/*
621 * Compact out any stale entries in the leaf.
622 * Log the header and changed leaf entries, if any.
623 */
624void
625xfs_dir2_leaf_compact(
626 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000627 struct xfs_buf *bp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 int from; /* source leaf index */
630 xfs_dir2_leaf_t *leaf; /* leaf structure */
631 int loglow; /* first leaf entry to log */
632 int to; /* target leaf index */
633
Dave Chinner1d9025e2012-06-22 18:50:14 +1000634 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (!leaf->hdr.stale) {
636 return;
637 }
638 /*
639 * Compress out the stale entries in place.
640 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100641 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200642 if (leaf->ents[from].address ==
643 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 continue;
645 /*
646 * Only actually copy the entries that are different.
647 */
648 if (from > to) {
649 if (loglow == -1)
650 loglow = to;
651 leaf->ents[to] = leaf->ents[from];
652 }
653 to++;
654 }
655 /*
656 * Update and log the header, log the leaf entries.
657 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100658 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800659 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 leaf->hdr.stale = 0;
661 xfs_dir2_leaf_log_header(args->trans, bp);
662 if (loglow != -1)
663 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
664}
665
666/*
667 * Compact the leaf entries, removing stale ones.
668 * Leave one stale entry behind - the one closest to our
669 * insertion index - and the caller will shift that one to our insertion
670 * point later.
671 * Return new insertion index, where the remaining stale entry is,
672 * and leaf logging indices.
673 */
674void
675xfs_dir2_leaf_compact_x1(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000676 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int *indexp, /* insertion index */
678 int *lowstalep, /* out: stale entry before us */
679 int *highstalep, /* out: stale entry after us */
680 int *lowlogp, /* out: low log index */
681 int *highlogp) /* out: high log index */
682{
683 int from; /* source copy index */
684 int highstale; /* stale entry at/after index */
685 int index; /* insertion index */
686 int keepstale; /* source index of kept stale */
687 xfs_dir2_leaf_t *leaf; /* leaf structure */
688 int lowstale; /* stale entry before index */
689 int newindex=0; /* new insertion index */
690 int to; /* destination copy index */
691
Dave Chinner1d9025e2012-06-22 18:50:14 +1000692 leaf = bp->b_addr;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100693 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 index = *indexp;
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200695
696 xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /*
699 * Pick the better of lowstale and highstale.
700 */
701 if (lowstale >= 0 &&
Nathan Scotta818e5d2006-03-17 17:28:07 +1100702 (highstale == be16_to_cpu(leaf->hdr.count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 index - lowstale <= highstale - index))
704 keepstale = lowstale;
705 else
706 keepstale = highstale;
707 /*
708 * Copy the entries in place, removing all the stale entries
709 * except keepstale.
710 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100711 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /*
713 * Notice the new value of index.
714 */
715 if (index == from)
716 newindex = to;
717 if (from != keepstale &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200718 leaf->ents[from].address ==
719 cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (from == to)
721 *lowlogp = to;
722 continue;
723 }
724 /*
725 * Record the new keepstale value for the insertion.
726 */
727 if (from == keepstale)
728 lowstale = highstale = to;
729 /*
730 * Copy only the entries that have moved.
731 */
732 if (from > to)
733 leaf->ents[to] = leaf->ents[from];
734 to++;
735 }
736 ASSERT(from > to);
737 /*
738 * If the insertion point was past the last entry,
739 * set the new insertion point accordingly.
740 */
741 if (index == from)
742 newindex = to;
743 *indexp = newindex;
744 /*
745 * Adjust the leaf header values.
746 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800747 be16_add_cpu(&leaf->hdr.count, -(from - to));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100748 leaf->hdr.stale = cpu_to_be16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /*
750 * Remember the low/high stale value only in the "right"
751 * direction.
752 */
753 if (lowstale >= newindex)
754 lowstale = -1;
755 else
Nathan Scotta818e5d2006-03-17 17:28:07 +1100756 highstale = be16_to_cpu(leaf->hdr.count);
757 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *lowstalep = lowstale;
759 *highstalep = highstale;
760}
761
762/*
763 * Getdents (readdir) for leaf and node directories.
764 * This reads the data blocks only, so is the same for both forms.
765 */
766int /* error */
767xfs_dir2_leaf_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 xfs_inode_t *dp, /* incore directory inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000769 void *dirent,
770 size_t bufsize,
771 xfs_off_t *offset,
772 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000774 struct xfs_buf *bp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 int byteoff; /* offset in current block */
776 xfs_dir2_db_t curdb; /* db for current block */
777 xfs_dir2_off_t curoff; /* current overall offset */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200778 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 xfs_dir2_data_entry_t *dep; /* data entry */
780 xfs_dir2_data_unused_t *dup; /* unused entry */
Nathan Scottf6d75cb2006-03-14 13:32:24 +1100781 int error = 0; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 int i; /* temporary loop index */
783 int j; /* temporary loop index */
784 int length; /* temporary length value */
785 xfs_bmbt_irec_t *map; /* map vector for blocks */
786 xfs_extlen_t map_blocks; /* number of fsbs in map */
787 xfs_dablk_t map_off; /* last mapped file offset */
788 int map_size; /* total entries in *map */
789 int map_valid; /* valid entries in *map */
790 xfs_mount_t *mp; /* filesystem mount point */
791 xfs_dir2_off_t newoff; /* new curoff after new blk */
792 int nmap; /* mappings to ask xfs_bmapi */
Nathan Scottf6d75cb2006-03-14 13:32:24 +1100793 char *ptr = NULL; /* pointer to current data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int ra_current; /* number of read-ahead blks */
795 int ra_index; /* *map index for read-ahead */
796 int ra_offset; /* map entry offset for ra */
797 int ra_want; /* readahead count wanted */
798
799 /*
800 * If the offset is at or past the largest allowed value,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000801 * give up right away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000803 if (*offset >= XFS_DIR2_MAX_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return 0;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 mp = dp->i_mount;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /*
809 * Set up to bmap a number of blocks based on the caller's
810 * buffer size, the directory block size, and the filesystem
811 * block size.
812 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000813 map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
815 map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
816 bp = NULL;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /*
819 * Inside the loop we keep the main offset value as a byte offset
820 * in the directory file.
821 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000822 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /*
825 * Force this conversion through db so we truncate the offset
826 * down to get the start of the data block.
827 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000828 map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /*
830 * Loop over directory entries until we reach the end offset.
831 * Get more blocks and readahead as necessary.
832 */
833 while (curoff < XFS_DIR2_LEAF_OFFSET) {
834 /*
835 * If we have no buffer, or we're off the end of the
836 * current buffer, need to get another one.
837 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000838 if (!bp || ptr >= (char *)bp->b_addr + mp->m_dirblksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /*
840 * If we have a buffer, we need to release it and
841 * take it out of the mapping.
842 */
843 if (bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000844 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 bp = NULL;
846 map_blocks -= mp->m_dirblkfsbs;
847 /*
848 * Loop to get rid of the extents for the
849 * directory block.
850 */
851 for (i = mp->m_dirblkfsbs; i > 0; ) {
852 j = MIN((int)map->br_blockcount, i);
853 map->br_blockcount -= j;
854 map->br_startblock += j;
855 map->br_startoff += j;
856 /*
857 * If mapping is done, pitch it from
858 * the table.
859 */
860 if (!map->br_blockcount && --map_valid)
861 memmove(&map[0], &map[1],
862 sizeof(map[0]) *
863 map_valid);
864 i -= j;
865 }
866 }
867 /*
868 * Recalculate the readahead blocks wanted.
869 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000870 ra_want = howmany(bufsize + mp->m_dirblksize,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 mp->m_sb.sb_blocksize) - 1;
Eric Sandeen8e69ce12009-09-25 19:42:26 +0000872 ASSERT(ra_want >= 0);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /*
875 * If we don't have as many as we want, and we haven't
876 * run out of data blocks, get some more mappings.
877 */
878 if (1 + ra_want > map_blocks &&
879 map_off <
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000880 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /*
882 * Get more bmaps, fill in after the ones
883 * we already have in the table.
884 */
885 nmap = map_size - map_valid;
Dave Chinner5c8ed202011-09-18 20:40:45 +0000886 error = xfs_bmapi_read(dp, map_off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000887 xfs_dir2_byte_to_da(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 XFS_DIR2_LEAF_OFFSET) - map_off,
Dave Chinner5c8ed202011-09-18 20:40:45 +0000889 &map[map_valid], &nmap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /*
891 * Don't know if we should ignore this or
892 * try to return an error.
893 * The trouble with returning errors
894 * is that readdir will just stop without
895 * actually passing the error through.
896 */
897 if (error)
898 break; /* XXX */
899 /*
900 * If we got all the mappings we asked for,
901 * set the final map offset based on the
902 * last bmap value received.
903 * Otherwise, we've reached the end.
904 */
905 if (nmap == map_size - map_valid)
906 map_off =
907 map[map_valid + nmap - 1].br_startoff +
908 map[map_valid + nmap - 1].br_blockcount;
909 else
910 map_off =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000911 xfs_dir2_byte_to_da(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 XFS_DIR2_LEAF_OFFSET);
913 /*
914 * Look for holes in the mapping, and
915 * eliminate them. Count up the valid blocks.
916 */
917 for (i = map_valid; i < map_valid + nmap; ) {
918 if (map[i].br_startblock ==
919 HOLESTARTBLOCK) {
920 nmap--;
921 length = map_valid + nmap - i;
922 if (length)
923 memmove(&map[i],
924 &map[i + 1],
925 sizeof(map[i]) *
926 length);
927 } else {
928 map_blocks +=
929 map[i].br_blockcount;
930 i++;
931 }
932 }
933 map_valid += nmap;
934 }
935 /*
936 * No valid mappings, so no more data blocks.
937 */
938 if (!map_valid) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000939 curoff = xfs_dir2_da_to_byte(mp, map_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 break;
941 }
942 /*
943 * Read the directory block starting at the first
944 * mapping.
945 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000946 curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000947 error = xfs_da_read_buf(NULL, dp, map->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 map->br_blockcount >= mp->m_dirblkfsbs ?
949 XFS_FSB_TO_DADDR(mp, map->br_startblock) :
950 -1,
951 &bp, XFS_DATA_FORK);
952 /*
953 * Should just skip over the data block instead
954 * of giving up.
955 */
956 if (error)
957 break; /* XXX */
958 /*
959 * Adjust the current amount of read-ahead: we just
960 * read a block that was previously ra.
961 */
962 if (ra_current)
963 ra_current -= mp->m_dirblkfsbs;
964 /*
965 * Do we need more readahead?
966 */
967 for (ra_index = ra_offset = i = 0;
968 ra_want > ra_current && i < map_blocks;
969 i += mp->m_dirblkfsbs) {
970 ASSERT(ra_index < map_valid);
971 /*
972 * Read-ahead a contiguous directory block.
973 */
974 if (i > ra_current &&
975 map[ra_index].br_blockcount >=
976 mp->m_dirblkfsbs) {
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +0000977 xfs_buf_readahead(mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 XFS_FSB_TO_DADDR(mp,
979 map[ra_index].br_startblock +
980 ra_offset),
981 (int)BTOBB(mp->m_dirblksize));
982 ra_current = i;
983 }
984 /*
985 * Read-ahead a non-contiguous directory block.
986 * This doesn't use our mapping, but this
987 * is a very rare case.
988 */
989 else if (i > ra_current) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000990 (void)xfs_da_reada_buf(NULL, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 map[ra_index].br_startoff +
992 ra_offset, XFS_DATA_FORK);
993 ra_current = i;
994 }
995 /*
996 * Advance offset through the mapping table.
997 */
998 for (j = 0; j < mp->m_dirblkfsbs; j++) {
999 /*
1000 * The rest of this extent but not
1001 * more than a dir block.
1002 */
1003 length = MIN(mp->m_dirblkfsbs,
1004 (int)(map[ra_index].br_blockcount -
1005 ra_offset));
1006 j += length;
1007 ra_offset += length;
1008 /*
1009 * Advance to the next mapping if
1010 * this one is used up.
1011 */
1012 if (ra_offset ==
1013 map[ra_index].br_blockcount) {
1014 ra_offset = 0;
1015 ra_index++;
1016 }
1017 }
1018 }
1019 /*
1020 * Having done a read, we need to set a new offset.
1021 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001022 newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /*
1024 * Start of the current block.
1025 */
1026 if (curoff < newoff)
1027 curoff = newoff;
1028 /*
1029 * Make sure we're in the right block.
1030 */
1031 else if (curoff > newoff)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001032 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 curdb);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001034 hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 xfs_dir2_data_check(dp, bp);
1036 /*
1037 * Find our position in the block.
1038 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +02001039 ptr = (char *)(hdr + 1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001040 byteoff = xfs_dir2_byte_to_off(mp, curoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /*
1042 * Skip past the header.
1043 */
1044 if (byteoff == 0)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001045 curoff += (uint)sizeof(*hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 /*
1047 * Skip past entries until we reach our offset.
1048 */
1049 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001050 while ((char *)ptr - (char *)hdr < byteoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 dup = (xfs_dir2_data_unused_t *)ptr;
1052
Nathan Scottad354eb2006-03-17 17:27:37 +11001053 if (be16_to_cpu(dup->freetag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 == XFS_DIR2_DATA_FREE_TAG) {
1055
Nathan Scottad354eb2006-03-17 17:27:37 +11001056 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 ptr += length;
1058 continue;
1059 }
1060 dep = (xfs_dir2_data_entry_t *)ptr;
1061 length =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001062 xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 ptr += length;
1064 }
1065 /*
1066 * Now set our real offset.
1067 */
1068 curoff =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001069 xfs_dir2_db_off_to_byte(mp,
1070 xfs_dir2_byte_to_db(mp, curoff),
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001071 (char *)ptr - (char *)hdr);
1072 if (ptr >= (char *)hdr + mp->m_dirblksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 continue;
1074 }
1075 }
1076 }
1077 /*
1078 * We have a pointer to an entry.
1079 * Is it a live one?
1080 */
1081 dup = (xfs_dir2_data_unused_t *)ptr;
1082 /*
1083 * No, it's unused, skip over it.
1084 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001085 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1086 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ptr += length;
1088 curoff += length;
1089 continue;
1090 }
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 dep = (xfs_dir2_data_entry_t *)ptr;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001093 length = xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Dave Chinner4a24cb72010-01-20 10:48:05 +11001095 if (filldir(dirent, (char *)dep->name, dep->namelen,
Christoph Hellwig15440312009-01-08 14:00:00 -05001096 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
Christoph Hellwiga19d9f82009-03-29 09:51:08 +02001097 be64_to_cpu(dep->inumber), DT_UNKNOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 break;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /*
1101 * Advance to next entry in the block.
1102 */
1103 ptr += length;
1104 curoff += length;
Eric Sandeen8e69ce12009-09-25 19:42:26 +00001105 /* bufsize may have just been a guess; don't go negative */
1106 bufsize = bufsize > length ? bufsize - length : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108
1109 /*
1110 * All done. Set output offset value to current offset.
1111 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001112 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
Christoph Hellwig15440312009-01-08 14:00:00 -05001113 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 else
Christoph Hellwig15440312009-01-08 14:00:00 -05001115 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001116 kmem_free(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (bp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001118 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return error;
1120}
1121
1122/*
1123 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1124 */
1125int
1126xfs_dir2_leaf_init(
1127 xfs_da_args_t *args, /* operation arguments */
1128 xfs_dir2_db_t bno, /* directory block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001129 struct xfs_buf **bpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 int magic) /* magic number for block */
1131{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001132 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 xfs_inode_t *dp; /* incore directory inode */
1134 int error; /* error return code */
1135 xfs_dir2_leaf_t *leaf; /* leaf structure */
1136 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1137 xfs_mount_t *mp; /* filesystem mount point */
1138 xfs_trans_t *tp; /* transaction pointer */
1139
1140 dp = args->dp;
1141 ASSERT(dp != NULL);
1142 tp = args->trans;
1143 mp = dp->i_mount;
1144 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1145 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1146 /*
1147 * Get the buffer for the block.
1148 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001149 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 XFS_DATA_FORK);
1151 if (error) {
1152 return error;
1153 }
1154 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001155 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /*
1157 * Initialize the header.
1158 */
Nathan Scott89da0542006-03-17 17:28:40 +11001159 leaf->hdr.info.magic = cpu_to_be16(magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 leaf->hdr.info.forw = 0;
1161 leaf->hdr.info.back = 0;
1162 leaf->hdr.count = 0;
1163 leaf->hdr.stale = 0;
1164 xfs_dir2_leaf_log_header(tp, bp);
1165 /*
1166 * If it's a leaf-format directory initialize the tail.
1167 * In this case our caller has the real bests table to copy into
1168 * the block.
1169 */
1170 if (magic == XFS_DIR2_LEAF1_MAGIC) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001171 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 ltp->bestcount = 0;
1173 xfs_dir2_leaf_log_tail(tp, bp);
1174 }
1175 *bpp = bp;
1176 return 0;
1177}
1178
1179/*
1180 * Log the bests entries indicated from a leaf1 block.
1181 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001182static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183xfs_dir2_leaf_log_bests(
1184 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001185 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 int first, /* first entry to log */
1187 int last) /* last entry to log */
1188{
Nathan Scott68b3a102006-03-17 17:27:19 +11001189 __be16 *firstb; /* pointer to first entry */
1190 __be16 *lastb; /* pointer to last entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 xfs_dir2_leaf_t *leaf; /* leaf structure */
1192 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1193
Dave Chinner1d9025e2012-06-22 18:50:14 +10001194 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001195 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001196 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1197 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1198 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001199 xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1201}
1202
1203/*
1204 * Log the leaf entries indicated from a leaf1 or leafn block.
1205 */
1206void
1207xfs_dir2_leaf_log_ents(
1208 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001209 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 int first, /* first entry to log */
1211 int last) /* last entry to log */
1212{
1213 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1214 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1215 xfs_dir2_leaf_t *leaf; /* leaf structure */
1216
Dave Chinner1d9025e2012-06-22 18:50:14 +10001217 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001218 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1219 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 firstlep = &leaf->ents[first];
1221 lastlep = &leaf->ents[last];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001222 xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1224}
1225
1226/*
1227 * Log the header of the leaf1 or leafn block.
1228 */
1229void
1230xfs_dir2_leaf_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001231 struct xfs_trans *tp,
1232 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 xfs_dir2_leaf_t *leaf; /* leaf structure */
1235
Dave Chinner1d9025e2012-06-22 18:50:14 +10001236 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001237 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1238 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001239 xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 (uint)(sizeof(leaf->hdr) - 1));
1241}
1242
1243/*
1244 * Log the tail of the leaf1 block.
1245 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001246STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247xfs_dir2_leaf_log_tail(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001248 struct xfs_trans *tp,
1249 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 xfs_dir2_leaf_t *leaf; /* leaf structure */
1252 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1253 xfs_mount_t *mp; /* filesystem mount point */
1254
1255 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001256 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001257 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001258 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001259 xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 (uint)(mp->m_dirblksize - 1));
1261}
1262
1263/*
1264 * Look up the entry referred to by args in the leaf format directory.
1265 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1266 * is also used by the node-format code.
1267 */
1268int
1269xfs_dir2_leaf_lookup(
1270 xfs_da_args_t *args) /* operation arguments */
1271{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001272 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 xfs_dir2_data_entry_t *dep; /* data block entry */
1274 xfs_inode_t *dp; /* incore directory inode */
1275 int error; /* error return code */
1276 int index; /* found entry index */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001277 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 xfs_dir2_leaf_t *leaf; /* leaf structure */
1279 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1280 xfs_trans_t *tp; /* transaction pointer */
1281
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001282 trace_xfs_dir2_leaf_lookup(args);
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /*
1285 * Look up name in the leaf block, returning both buffers and index.
1286 */
1287 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1288 return error;
1289 }
1290 tp = args->trans;
1291 dp = args->dp;
1292 xfs_dir2_leaf_check(dp, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001293 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /*
1295 * Get to the leaf entry and contained data entry address.
1296 */
1297 lep = &leaf->ents[index];
1298 /*
1299 * Point to the data entry.
1300 */
1301 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001302 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001303 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +10001305 * Return the found inode number & CI name if appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001307 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001308 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001309 xfs_trans_brelse(tp, dbp);
1310 xfs_trans_brelse(tp, lbp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001311 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
1314/*
1315 * Look up name/hash in the leaf block.
1316 * Fill in indexp with the found index, and dbpp with the data buffer.
1317 * If not found dbpp will be NULL, and ENOENT comes back.
1318 * lbpp will always be filled in with the leaf buffer unless there's an error.
1319 */
1320static int /* error */
1321xfs_dir2_leaf_lookup_int(
1322 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001323 struct xfs_buf **lbpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 int *indexp, /* out: index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001325 struct xfs_buf **dbpp) /* out: data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001327 xfs_dir2_db_t curdb = -1; /* current data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001328 struct xfs_buf *dbp = NULL; /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 xfs_dir2_data_entry_t *dep; /* data entry */
1330 xfs_inode_t *dp; /* incore directory inode */
1331 int error; /* error return code */
1332 int index; /* index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001333 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1335 xfs_dir2_leaf_t *leaf; /* leaf structure */
1336 xfs_mount_t *mp; /* filesystem mount point */
1337 xfs_dir2_db_t newdb; /* new data block number */
1338 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001339 xfs_dir2_db_t cidb = -1; /* case match data block no. */
Barry Naujok5163f952008-05-21 16:41:01 +10001340 enum xfs_dacmp cmp; /* name compare result */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 dp = args->dp;
1343 tp = args->trans;
1344 mp = dp->i_mount;
1345 /*
1346 * Read the leaf block into the buffer.
1347 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001348 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1349 XFS_DATA_FORK);
1350 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 *lbpp = lbp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001353 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 xfs_dir2_leaf_check(dp, lbp);
1355 /*
1356 * Look for the first leaf entry with our hash value.
1357 */
1358 index = xfs_dir2_leaf_search_hash(args, lbp);
1359 /*
1360 * Loop over all the entries with the right hash value
1361 * looking to match the name.
1362 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001363 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
Barry Naujok5163f952008-05-21 16:41:01 +10001364 be32_to_cpu(lep->hashval) == args->hashval;
1365 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 /*
1367 * Skip over stale leaf entries.
1368 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001369 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 continue;
1371 /*
1372 * Get the new data block number.
1373 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001374 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 /*
1376 * If it's not the same as the old data block number,
1377 * need to pitch the old one and read the new one.
1378 */
1379 if (newdb != curdb) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001380 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001381 xfs_trans_brelse(tp, dbp);
Barry Naujok5163f952008-05-21 16:41:01 +10001382 error = xfs_da_read_buf(tp, dp,
1383 xfs_dir2_db_to_da(mp, newdb),
1384 -1, &dbp, XFS_DATA_FORK);
1385 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001386 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return error;
1388 }
1389 xfs_dir2_data_check(dp, dbp);
1390 curdb = newdb;
1391 }
1392 /*
1393 * Point to the data entry.
1394 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001395 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
Barry Naujok5163f952008-05-21 16:41:01 +10001396 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 /*
Barry Naujok5163f952008-05-21 16:41:01 +10001398 * Compare name and if it's an exact match, return the index
1399 * and buffer. If it's the first case-insensitive match, store
1400 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 */
Barry Naujok5163f952008-05-21 16:41:01 +10001402 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1403 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1404 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 *indexp = index;
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001406 /* case exact match: return the current buffer. */
Barry Naujok5163f952008-05-21 16:41:01 +10001407 if (cmp == XFS_CMP_EXACT) {
Barry Naujok5163f952008-05-21 16:41:01 +10001408 *dbpp = dbp;
1409 return 0;
1410 }
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001411 cidb = curdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
1413 }
Barry Naujok6a178102008-05-21 16:42:05 +10001414 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +10001415 /*
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001416 * Here, we can only be doing a lookup (not a rename or remove).
1417 * If a case-insensitive match was found earlier, re-read the
1418 * appropriate data block if required and return it.
Barry Naujok5163f952008-05-21 16:41:01 +10001419 */
1420 if (args->cmpresult == XFS_CMP_CASE) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001421 ASSERT(cidb != -1);
1422 if (cidb != curdb) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001423 xfs_trans_brelse(tp, dbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001424 error = xfs_da_read_buf(tp, dp,
1425 xfs_dir2_db_to_da(mp, cidb),
1426 -1, &dbp, XFS_DATA_FORK);
1427 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001428 xfs_trans_brelse(tp, lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001429 return error;
1430 }
1431 }
1432 *dbpp = dbp;
Barry Naujok5163f952008-05-21 16:41:01 +10001433 return 0;
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /*
1436 * No match found, return ENOENT.
1437 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001438 ASSERT(cidb == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001440 xfs_trans_brelse(tp, dbp);
1441 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return XFS_ERROR(ENOENT);
1443}
1444
1445/*
1446 * Remove an entry from a leaf format directory.
1447 */
1448int /* error */
1449xfs_dir2_leaf_removename(
1450 xfs_da_args_t *args) /* operation arguments */
1451{
Nathan Scott68b3a102006-03-17 17:27:19 +11001452 __be16 *bestsp; /* leaf block best freespace */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001453 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001455 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 xfs_dir2_data_entry_t *dep; /* data entry structure */
1457 xfs_inode_t *dp; /* incore directory inode */
1458 int error; /* error return code */
1459 xfs_dir2_db_t i; /* temporary data block # */
1460 int index; /* index into leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001461 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 xfs_dir2_leaf_t *leaf; /* leaf structure */
1463 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1464 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1465 xfs_mount_t *mp; /* filesystem mount point */
1466 int needlog; /* need to log data header */
1467 int needscan; /* need to rescan data frees */
1468 xfs_dir2_data_off_t oldbest; /* old value of best free */
1469 xfs_trans_t *tp; /* transaction pointer */
1470
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001471 trace_xfs_dir2_leaf_removename(args);
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 /*
1474 * Lookup the leaf entry, get the leaf and data blocks read in.
1475 */
1476 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1477 return error;
1478 }
1479 dp = args->dp;
1480 tp = args->trans;
1481 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001482 leaf = lbp->b_addr;
1483 hdr = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 xfs_dir2_data_check(dp, dbp);
1485 /*
1486 * Point to the leaf entry, use that to point to the data entry.
1487 */
1488 lep = &leaf->ents[index];
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001489 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001491 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 needscan = needlog = 0;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001493 oldbest = be16_to_cpu(hdr->bestfree[0].length);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001494 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1495 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott68b3a102006-03-17 17:27:19 +11001496 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 /*
1498 * Mark the former data entry unused.
1499 */
1500 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001501 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001502 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 /*
1504 * We just mark the leaf entry stale by putting a null in it.
1505 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001506 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001508 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1510 /*
1511 * Scan the freespace in the data block again if necessary,
1512 * log the data block header if necessary.
1513 */
1514 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001515 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (needlog)
1517 xfs_dir2_data_log_header(tp, dbp);
1518 /*
1519 * If the longest freespace in the data block has changed,
1520 * put the new value in the bests table and log that.
1521 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001522 if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1523 bestsp[db] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1525 }
1526 xfs_dir2_data_check(dp, dbp);
1527 /*
1528 * If the data block is now empty then get rid of the data block.
1529 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001530 if (be16_to_cpu(hdr->bestfree[0].length) ==
1531 mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 ASSERT(db != mp->m_dirdatablk);
1533 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1534 /*
1535 * Nope, can't get rid of it because it caused
1536 * allocation of a bmap btree block to do so.
1537 * Just go on, returning success, leaving the
1538 * empty block in place.
1539 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001540 if (error == ENOSPC && args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 xfs_dir2_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return error;
1544 }
1545 dbp = NULL;
1546 /*
1547 * If this is the last data block then compact the
1548 * bests table by getting rid of entries.
1549 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001550 if (db == be32_to_cpu(ltp->bestcount) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /*
1552 * Look for the last active entry (i).
1553 */
1554 for (i = db - 1; i > 0; i--) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001555 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 break;
1557 }
1558 /*
1559 * Copy the table down so inactive entries at the
1560 * end are removed.
1561 */
1562 memmove(&bestsp[db - i], bestsp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001563 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001564 be32_add_cpu(&ltp->bestcount, -(db - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001566 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 } else
Nathan Scott68b3a102006-03-17 17:27:19 +11001568 bestsp[db] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570 /*
1571 * If the data block was not the first one, drop it.
1572 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001573 else if (db != mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 dbp = NULL;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 xfs_dir2_leaf_check(dp, lbp);
1577 /*
1578 * See if we can convert to block form.
1579 */
1580 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1581}
1582
1583/*
1584 * Replace the inode number in a leaf format directory entry.
1585 */
1586int /* error */
1587xfs_dir2_leaf_replace(
1588 xfs_da_args_t *args) /* operation arguments */
1589{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001590 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 xfs_dir2_data_entry_t *dep; /* data block entry */
1592 xfs_inode_t *dp; /* incore directory inode */
1593 int error; /* error return code */
1594 int index; /* index of leaf entry */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001595 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 xfs_dir2_leaf_t *leaf; /* leaf structure */
1597 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1598 xfs_trans_t *tp; /* transaction pointer */
1599
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001600 trace_xfs_dir2_leaf_replace(args);
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 /*
1603 * Look up the entry.
1604 */
1605 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1606 return error;
1607 }
1608 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001609 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /*
1611 * Point to the leaf entry, get data address from it.
1612 */
1613 lep = &leaf->ents[index];
1614 /*
1615 * Point to the data entry.
1616 */
1617 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001618 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001619 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001620 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 /*
1622 * Put the new inode number in, log it.
1623 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001624 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 tp = args->trans;
1626 xfs_dir2_data_log_entry(tp, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 xfs_dir2_leaf_check(dp, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001628 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return 0;
1630}
1631
1632/*
1633 * Return index in the leaf block (lbp) which is either the first
1634 * one with this hash value, or if there are none, the insert point
1635 * for that hash value.
1636 */
1637int /* index value */
1638xfs_dir2_leaf_search_hash(
1639 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001640 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 xfs_dahash_t hash=0; /* hash from this entry */
1643 xfs_dahash_t hashwant; /* hash value looking for */
1644 int high; /* high leaf index */
1645 int low; /* low leaf index */
1646 xfs_dir2_leaf_t *leaf; /* leaf structure */
1647 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1648 int mid=0; /* current leaf index */
1649
Dave Chinner1d9025e2012-06-22 18:50:14 +10001650 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651#ifndef __KERNEL__
1652 if (!leaf->hdr.count)
1653 return 0;
1654#endif
1655 /*
1656 * Note, the table cannot be empty, so we have to go through the loop.
1657 * Binary search the leaf entries looking for our hash value.
1658 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001659 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 hashwant = args->hashval;
1661 low <= high; ) {
1662 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001663 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 break;
1665 if (hash < hashwant)
1666 low = mid + 1;
1667 else
1668 high = mid - 1;
1669 }
1670 /*
1671 * Found one, back up through all the equal hash values.
1672 */
1673 if (hash == hashwant) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001674 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 mid--;
1676 }
1677 }
1678 /*
1679 * Need to point to an entry higher than ours.
1680 */
1681 else if (hash < hashwant)
1682 mid++;
1683 return mid;
1684}
1685
1686/*
1687 * Trim off a trailing data block. We know it's empty since the leaf
1688 * freespace table says so.
1689 */
1690int /* error */
1691xfs_dir2_leaf_trim_data(
1692 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001693 struct xfs_buf *lbp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 xfs_dir2_db_t db) /* data block number */
1695{
Nathan Scott68b3a102006-03-17 17:27:19 +11001696 __be16 *bestsp; /* leaf bests table */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001697 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 xfs_inode_t *dp; /* incore directory inode */
1699 int error; /* error return value */
1700 xfs_dir2_leaf_t *leaf; /* leaf structure */
1701 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1702 xfs_mount_t *mp; /* filesystem mount point */
1703 xfs_trans_t *tp; /* transaction pointer */
1704
1705 dp = args->dp;
1706 mp = dp->i_mount;
1707 tp = args->trans;
1708 /*
1709 * Read the offending data block. We need its buffer.
1710 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001711 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 XFS_DATA_FORK))) {
1713 return error;
1714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Dave Chinner1d9025e2012-06-22 18:50:14 +10001716 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001717 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001718
1719#ifdef DEBUG
1720{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001721 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001722
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001723 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001724 ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
1725 mp->m_dirblksize - (uint)sizeof(*hdr));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001726 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001727}
1728#endif
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /*
1731 * Get rid of the data block.
1732 */
1733 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1734 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001735 xfs_trans_brelse(tp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return error;
1737 }
1738 /*
1739 * Eliminate the last bests entry from the table.
1740 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001741 bestsp = xfs_dir2_leaf_bests_p(ltp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001742 be32_add_cpu(&ltp->bestcount, -1);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001743 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001745 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return 0;
1747}
1748
Christoph Hellwig22823962011-07-08 14:35:53 +02001749static inline size_t
1750xfs_dir2_leaf_size(
1751 struct xfs_dir2_leaf_hdr *hdr,
1752 int counts)
1753{
1754 int entries;
1755
1756 entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
1757 return sizeof(xfs_dir2_leaf_hdr_t) +
1758 entries * sizeof(xfs_dir2_leaf_entry_t) +
1759 counts * sizeof(xfs_dir2_data_off_t) +
1760 sizeof(xfs_dir2_leaf_tail_t);
1761}
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763/*
1764 * Convert node form directory to leaf form directory.
1765 * The root of the node form dir needs to already be a LEAFN block.
1766 * Just return if we can't do anything.
1767 */
1768int /* error */
1769xfs_dir2_node_to_leaf(
1770 xfs_da_state_t *state) /* directory operation state */
1771{
1772 xfs_da_args_t *args; /* operation arguments */
1773 xfs_inode_t *dp; /* incore directory inode */
1774 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001775 struct xfs_buf *fbp; /* buffer for freespace block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 xfs_fileoff_t fo; /* freespace file offset */
1777 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001778 struct xfs_buf *lbp; /* buffer for leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1780 xfs_dir2_leaf_t *leaf; /* leaf structure */
1781 xfs_mount_t *mp; /* filesystem mount point */
1782 int rval; /* successful free trim? */
1783 xfs_trans_t *tp; /* transaction pointer */
1784
1785 /*
1786 * There's more than a leaf level in the btree, so there must
1787 * be multiple leafn blocks. Give up.
1788 */
1789 if (state->path.active > 1)
1790 return 0;
1791 args = state->args;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001792
1793 trace_xfs_dir2_node_to_leaf(args);
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 mp = state->mp;
1796 dp = args->dp;
1797 tp = args->trans;
1798 /*
1799 * Get the last offset in the file.
1800 */
1801 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1802 return error;
1803 }
1804 fo -= mp->m_dirblkfsbs;
1805 /*
1806 * If there are freespace blocks other than the first one,
1807 * take this opportunity to remove trailing empty freespace blocks
1808 * that may have been left behind during no-space-reservation
1809 * operations.
1810 */
1811 while (fo > mp->m_dirfreeblk) {
1812 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1813 return error;
1814 }
1815 if (rval)
1816 fo -= mp->m_dirblkfsbs;
1817 else
1818 return 0;
1819 }
1820 /*
1821 * Now find the block just before the freespace block.
1822 */
1823 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1824 return error;
1825 }
1826 /*
1827 * If it's not the single leaf block, give up.
1828 */
1829 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1830 return 0;
1831 lbp = state->path.blk[0].bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001832 leaf = lbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001833 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 /*
1835 * Read the freespace block.
1836 */
1837 if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
1838 XFS_DATA_FORK))) {
1839 return error;
1840 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001841 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001842 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 ASSERT(!free->hdr.firstdb);
Christoph Hellwig22823962011-07-08 14:35:53 +02001844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 /*
1846 * Now see if the leafn and free data will fit in a leaf1.
1847 * If not, release the buffer and give up.
1848 */
Christoph Hellwig22823962011-07-08 14:35:53 +02001849 if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
1850 mp->m_dirblksize) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001851 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return 0;
1853 }
Christoph Hellwig22823962011-07-08 14:35:53 +02001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 /*
1856 * If the leaf has any stale entries in it, compress them out.
1857 * The compact routine will log the header.
1858 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001859 if (be16_to_cpu(leaf->hdr.stale))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 xfs_dir2_leaf_compact(args, lbp);
1861 else
1862 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott89da0542006-03-17 17:28:40 +11001863 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /*
1865 * Set up the leaf tail from the freespace block.
1866 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001867 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scott0ba962e2006-03-17 17:27:07 +11001868 ltp->bestcount = free->hdr.nvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /*
1870 * Set up the leaf bests table.
1871 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001872 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
Christoph Hellwig22823962011-07-08 14:35:53 +02001873 be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001874 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 xfs_dir2_leaf_log_tail(tp, lbp);
1876 xfs_dir2_leaf_check(dp, lbp);
1877 /*
1878 * Get rid of the freespace block.
1879 */
1880 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1881 if (error) {
1882 /*
1883 * This can't fail here because it can only happen when
1884 * punching out the middle of an extent, and this is an
1885 * isolated block.
1886 */
1887 ASSERT(error != ENOSPC);
1888 return error;
1889 }
1890 fbp = NULL;
1891 /*
1892 * Now see if we can convert the single-leaf directory
1893 * down to a block form directory.
1894 * This routine always kills the dabuf for the leaf, so
1895 * eliminate it from the path.
1896 */
1897 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1898 state->path.blk[0].bp = NULL;
1899 return error;
1900}