blob: 3002ab7d54c31f4742145afb46a7d5f2a773343f [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
Dave Chinnere6f76672012-11-12 22:54:15 +110051static void
52xfs_dir2_leaf_verify(
53 struct xfs_buf *bp,
54 __be16 magic)
55{
56 struct xfs_mount *mp = bp->b_target->bt_mount;
57 struct xfs_dir2_leaf_hdr *hdr = bp->b_addr;
58 int block_ok = 0;
59
60 block_ok = hdr->info.magic == magic;
61 if (!block_ok) {
62 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
63 xfs_buf_ioerror(bp, EFSCORRUPTED);
64 }
Dave Chinner612cfbf2012-11-14 17:52:32 +110065}
Dave Chinnere6f76672012-11-12 22:54:15 +110066
Dave Chinner612cfbf2012-11-14 17:52:32 +110067static void
68xfs_dir2_leaf1_write_verify(
69 struct xfs_buf *bp)
70{
71 xfs_dir2_leaf_verify(bp, cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
72}
73
74static void
75xfs_dir2_leaf1_read_verify(
76 struct xfs_buf *bp)
77{
78 xfs_dir2_leaf_verify(bp, cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
79 bp->b_pre_io = xfs_dir2_leaf1_write_verify;
Dave Chinnere6f76672012-11-12 22:54:15 +110080 bp->b_iodone = NULL;
81 xfs_buf_ioend(bp, 0);
82}
83
Dave Chinnerb0f539d2012-11-14 17:53:49 +110084void
Dave Chinner612cfbf2012-11-14 17:52:32 +110085xfs_dir2_leafn_write_verify(
86 struct xfs_buf *bp)
Dave Chinnere6f76672012-11-12 22:54:15 +110087{
Dave Chinner612cfbf2012-11-14 17:52:32 +110088 xfs_dir2_leaf_verify(bp, cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Dave Chinnere6f76672012-11-12 22:54:15 +110089}
90
Dave Chinnerd9392a42012-11-12 22:54:17 +110091void
Dave Chinner612cfbf2012-11-14 17:52:32 +110092xfs_dir2_leafn_read_verify(
93 struct xfs_buf *bp)
Dave Chinnere6f76672012-11-12 22:54:15 +110094{
95 xfs_dir2_leaf_verify(bp, cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Dave Chinner612cfbf2012-11-14 17:52:32 +110096 bp->b_pre_io = xfs_dir2_leafn_write_verify;
97 bp->b_iodone = NULL;
98 xfs_buf_ioend(bp, 0);
Dave Chinnere6f76672012-11-12 22:54:15 +110099}
100
101static int
102xfs_dir2_leaf_read(
103 struct xfs_trans *tp,
104 struct xfs_inode *dp,
105 xfs_dablk_t fbno,
106 xfs_daddr_t mappedbno,
107 struct xfs_buf **bpp)
108{
109 return xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinner612cfbf2012-11-14 17:52:32 +1100110 XFS_DATA_FORK, xfs_dir2_leaf1_read_verify);
Dave Chinnere6f76672012-11-12 22:54:15 +1100111}
112
113int
114xfs_dir2_leafn_read(
115 struct xfs_trans *tp,
116 struct xfs_inode *dp,
117 xfs_dablk_t fbno,
118 xfs_daddr_t mappedbno,
119 struct xfs_buf **bpp)
120{
121 return xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinner612cfbf2012-11-14 17:52:32 +1100122 XFS_DATA_FORK, xfs_dir2_leafn_read_verify);
Dave Chinnere6f76672012-11-12 22:54:15 +1100123}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/*
126 * Convert a block form directory to a leaf form directory.
127 */
128int /* error */
129xfs_dir2_block_to_leaf(
130 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000131 struct xfs_buf *dbp) /* input block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Nathan Scott68b3a102006-03-17 17:27:19 +1100133 __be16 *bestsp; /* leaf's bestsp entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 xfs_dablk_t blkno; /* leaf block's bno */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200135 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
137 xfs_dir2_block_tail_t *btp; /* block's tail */
138 xfs_inode_t *dp; /* incore directory inode */
139 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000140 struct xfs_buf *lbp; /* leaf block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 xfs_dir2_db_t ldb; /* leaf block's bno */
142 xfs_dir2_leaf_t *leaf; /* leaf structure */
143 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
144 xfs_mount_t *mp; /* filesystem mount point */
145 int needlog; /* need to log block header */
146 int needscan; /* need to rescan bestfree */
147 xfs_trans_t *tp; /* transaction pointer */
148
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000149 trace_xfs_dir2_block_to_leaf(args);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 dp = args->dp;
152 mp = dp->i_mount;
153 tp = args->trans;
154 /*
155 * Add the leaf block to the inode.
156 * This interface will only put blocks in the leaf/node range.
157 * Since that's empty now, we'll get the root (block 0 in range).
158 */
159 if ((error = xfs_da_grow_inode(args, &blkno))) {
160 return error;
161 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000162 ldb = xfs_dir2_da_to_db(mp, blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
164 /*
165 * Initialize the leaf block, get a buffer for it.
166 */
167 if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
168 return error;
169 }
170 ASSERT(lbp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000171 leaf = lbp->b_addr;
172 hdr = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 xfs_dir2_data_check(dp, dbp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200174 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000175 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /*
177 * Set the counts in the leaf header.
178 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100179 leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
180 leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /*
182 * Could compact these but I think we always do the conversion
183 * after squeezing out stale entries.
184 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100185 memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100186 xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 needscan = 0;
188 needlog = 1;
189 /*
190 * Make the space formerly occupied by the leaf entries and block
191 * tail be free.
192 */
193 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200194 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
195 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 (char *)blp),
197 &needlog, &needscan);
198 /*
199 * Fix up the block header, make it a data block.
200 */
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100201 dbp->b_pre_io = xfs_dir2_data_write_verify;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200202 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200204 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /*
206 * Set up leaf tail and bests table.
207 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000208 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100209 ltp->bestcount = cpu_to_be32(1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000210 bestsp = xfs_dir2_leaf_bests_p(ltp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200211 bestsp[0] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /*
213 * Log the data header and leaf bests table.
214 */
215 if (needlog)
216 xfs_dir2_data_log_header(tp, dbp);
217 xfs_dir2_leaf_check(dp, lbp);
218 xfs_dir2_data_check(dp, dbp);
219 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return 0;
221}
222
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200223STATIC void
224xfs_dir2_leaf_find_stale(
225 struct xfs_dir2_leaf *leaf,
226 int index,
227 int *lowstale,
228 int *highstale)
229{
230 /*
231 * Find the first stale entry before our index, if any.
232 */
233 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
234 if (leaf->ents[*lowstale].address ==
235 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
236 break;
237 }
238
239 /*
240 * Find the first stale entry at or after our index, if any.
241 * Stop if the result would require moving more entries than using
242 * lowstale.
243 */
244 for (*highstale = index;
245 *highstale < be16_to_cpu(leaf->hdr.count);
246 ++*highstale) {
247 if (leaf->ents[*highstale].address ==
248 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
249 break;
250 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
251 break;
252 }
253}
254
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200255struct xfs_dir2_leaf_entry *
256xfs_dir2_leaf_find_entry(
257 xfs_dir2_leaf_t *leaf, /* leaf structure */
258 int index, /* leaf table position */
259 int compact, /* need to compact leaves */
260 int lowstale, /* index of prev stale leaf */
261 int highstale, /* index of next stale leaf */
262 int *lfloglow, /* low leaf logging index */
263 int *lfloghigh) /* high leaf logging index */
264{
265 if (!leaf->hdr.stale) {
266 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
267
268 /*
269 * Now we need to make room to insert the leaf entry.
270 *
271 * If there are no stale entries, just insert a hole at index.
272 */
273 lep = &leaf->ents[index];
274 if (index < be16_to_cpu(leaf->hdr.count))
275 memmove(lep + 1, lep,
276 (be16_to_cpu(leaf->hdr.count) - index) *
277 sizeof(*lep));
278
279 /*
280 * Record low and high logging indices for the leaf.
281 */
282 *lfloglow = index;
283 *lfloghigh = be16_to_cpu(leaf->hdr.count);
284 be16_add_cpu(&leaf->hdr.count, 1);
285 return lep;
286 }
287
288 /*
289 * There are stale entries.
290 *
291 * We will use one of them for the new entry. It's probably not at
292 * the right location, so we'll have to shift some up or down first.
293 *
294 * If we didn't compact before, we need to find the nearest stale
295 * entries before and after our insertion point.
296 */
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200297 if (compact == 0)
298 xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200299
300 /*
301 * If the low one is better, use it.
302 */
303 if (lowstale >= 0 &&
304 (highstale == be16_to_cpu(leaf->hdr.count) ||
305 index - lowstale - 1 < highstale - index)) {
306 ASSERT(index - lowstale - 1 >= 0);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200307 ASSERT(leaf->ents[lowstale].address ==
308 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200309
310 /*
311 * Copy entries up to cover the stale entry and make room
312 * for the new entry.
313 */
314 if (index - lowstale - 1 > 0) {
315 memmove(&leaf->ents[lowstale],
316 &leaf->ents[lowstale + 1],
317 (index - lowstale - 1) *
318 sizeof(xfs_dir2_leaf_entry_t));
319 }
320 *lfloglow = MIN(lowstale, *lfloglow);
321 *lfloghigh = MAX(index - 1, *lfloghigh);
322 be16_add_cpu(&leaf->hdr.stale, -1);
323 return &leaf->ents[index - 1];
324 }
325
326 /*
327 * The high one is better, so use that one.
328 */
329 ASSERT(highstale - index >= 0);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200330 ASSERT(leaf->ents[highstale].address ==
331 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200332
333 /*
334 * Copy entries down to cover the stale entry and make room for the
335 * new entry.
336 */
337 if (highstale - index > 0) {
338 memmove(&leaf->ents[index + 1],
339 &leaf->ents[index],
340 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
341 }
342 *lfloglow = MIN(index, *lfloglow);
343 *lfloghigh = MAX(highstale, *lfloghigh);
344 be16_add_cpu(&leaf->hdr.stale, -1);
345 return &leaf->ents[index];
346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/*
349 * Add an entry to a leaf form directory.
350 */
351int /* error */
352xfs_dir2_leaf_addname(
353 xfs_da_args_t *args) /* operation arguments */
354{
Nathan Scott68b3a102006-03-17 17:27:19 +1100355 __be16 *bestsp; /* freespace table in leaf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 int compact; /* need to compact leaves */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200357 xfs_dir2_data_hdr_t *hdr; /* data block header */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000358 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 xfs_dir2_data_entry_t *dep; /* data block entry */
360 xfs_inode_t *dp; /* incore directory inode */
361 xfs_dir2_data_unused_t *dup; /* data unused entry */
362 int error; /* error return value */
363 int grown; /* allocated new data block */
364 int highstale; /* index of next stale leaf */
365 int i; /* temporary, index */
366 int index; /* leaf table position */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000367 struct xfs_buf *lbp; /* leaf's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 xfs_dir2_leaf_t *leaf; /* leaf structure */
369 int length; /* length of new entry */
370 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
371 int lfloglow; /* low leaf logging index */
372 int lfloghigh; /* high leaf logging index */
373 int lowstale; /* index of prev stale leaf */
374 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
375 xfs_mount_t *mp; /* filesystem mount point */
376 int needbytes; /* leaf block bytes needed */
377 int needlog; /* need to log data header */
378 int needscan; /* need to rescan data free */
Nathan Scott3d693c62006-03-17 17:28:27 +1100379 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 xfs_trans_t *tp; /* transaction pointer */
381 xfs_dir2_db_t use_block; /* data block number */
382
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000383 trace_xfs_dir2_leaf_addname(args);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 dp = args->dp;
386 tp = args->trans;
387 mp = dp->i_mount;
Dave Chinnere6f76672012-11-12 22:54:15 +1100388
389 error = xfs_dir2_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100390 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +1100392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /*
394 * Look up the entry by hash value and name.
395 * We know it's not there, our caller has already done a lookup.
396 * So the index is of the entry to insert in front of.
397 * But if there are dup hash values the index is of the first of those.
398 */
399 index = xfs_dir2_leaf_search_hash(args, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000400 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000401 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
402 bestsp = xfs_dir2_leaf_bests_p(ltp);
403 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /*
405 * See if there are any entries with the same hash value
406 * and space in their block for the new entry.
407 * This is good because it puts multiple same-hash value entries
408 * in a data block, improving the lookup of those entries.
409 */
410 for (use_block = -1, lep = &leaf->ents[index];
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100411 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 index++, lep++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100413 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 continue;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000415 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100416 ASSERT(i < be32_to_cpu(ltp->bestcount));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200417 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
Nathan Scott68b3a102006-03-17 17:27:19 +1100418 if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 use_block = i;
420 break;
421 }
422 }
423 /*
424 * Didn't find a block yet, linear search all the data blocks.
425 */
426 if (use_block == -1) {
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100427 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /*
429 * Remember a block we see that's missing.
430 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200431 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
432 use_block == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 use_block = i;
Nathan Scott68b3a102006-03-17 17:27:19 +1100434 else if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 use_block = i;
436 break;
437 }
438 }
439 }
440 /*
441 * How many bytes do we need in the leaf block?
442 */
Christoph Hellwig22823962011-07-08 14:35:53 +0200443 needbytes = 0;
444 if (!leaf->hdr.stale)
445 needbytes += sizeof(xfs_dir2_leaf_entry_t);
446 if (use_block == -1)
447 needbytes += sizeof(xfs_dir2_data_off_t);
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /*
450 * Now kill use_block if it refers to a missing block, so we
451 * can use it as an indication of allocation needed.
452 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200453 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 use_block = -1;
455 /*
456 * If we don't have enough free bytes but we can make enough
457 * by compacting out stale entries, we'll do that.
458 */
Barry Naujok6a178102008-05-21 16:42:05 +1000459 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
460 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 compact = 1;
462 }
463 /*
464 * Otherwise if we don't have enough free bytes we need to
465 * convert to node form.
466 */
Barry Naujok6a178102008-05-21 16:42:05 +1000467 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
468 leaf->hdr.count)] < needbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /*
470 * Just checking or no space reservation, give up.
471 */
Barry Naujok6a178102008-05-21 16:42:05 +1000472 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
473 args->total == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000474 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return XFS_ERROR(ENOSPC);
476 }
477 /*
478 * Convert to node form.
479 */
480 error = xfs_dir2_leaf_to_node(args, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (error)
482 return error;
483 /*
484 * Then add the new entry.
485 */
486 return xfs_dir2_node_addname(args);
487 }
488 /*
489 * Otherwise it will fit without compaction.
490 */
491 else
492 compact = 0;
493 /*
494 * If just checking, then it will fit unless we needed to allocate
495 * a new data block.
496 */
Barry Naujok6a178102008-05-21 16:42:05 +1000497 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000498 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
500 }
501 /*
502 * If no allocations are allowed, return now before we've
503 * changed anything.
504 */
505 if (args->total == 0 && use_block == -1) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000506 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return XFS_ERROR(ENOSPC);
508 }
509 /*
510 * Need to compact the leaf entries, removing stale ones.
511 * Leave one stale entry behind - the one closest to our
512 * insertion index - and we'll shift that one to our insertion
513 * point later.
514 */
515 if (compact) {
516 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
517 &lfloglow, &lfloghigh);
518 }
519 /*
520 * There are stale entries, so we'll need log-low and log-high
521 * impossibly bad values later.
522 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100523 else if (be16_to_cpu(leaf->hdr.stale)) {
524 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 lfloghigh = -1;
526 }
527 /*
528 * If there was no data block space found, we need to allocate
529 * a new one.
530 */
531 if (use_block == -1) {
532 /*
533 * Add the new data block.
534 */
535 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
536 &use_block))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000537 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return error;
539 }
540 /*
541 * Initialize the block.
542 */
543 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000544 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return error;
546 }
547 /*
548 * If we're adding a new data block on the end we need to
549 * extend the bests table. Copy it up one entry.
550 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100551 if (use_block >= be32_to_cpu(ltp->bestcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 bestsp--;
553 memmove(&bestsp[0], &bestsp[1],
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100554 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800555 be32_add_cpu(&ltp->bestcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100557 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559 /*
560 * If we're filling in a previously empty block just log it.
561 */
562 else
563 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000564 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200565 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 grown = 1;
Dave Chinnere4813572012-11-12 22:54:14 +1100567 } else {
568 /*
569 * Already had space in some data block.
570 * Just read that one in.
571 */
572 error = xfs_dir2_data_read(tp, dp,
573 xfs_dir2_db_to_da(mp, use_block),
574 -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100575 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000576 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return error;
578 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000579 hdr = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 grown = 0;
581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /*
583 * Point to the biggest freespace in our data block.
584 */
585 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200586 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
Nathan Scottad354eb2006-03-17 17:27:37 +1100587 ASSERT(be16_to_cpu(dup->length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 needscan = needlog = 0;
589 /*
590 * Mark the initial part of our freespace in use for the new entry.
591 */
592 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200593 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 &needlog, &needscan);
595 /*
596 * Initialize our new entry (at last).
597 */
598 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000599 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 dep->namelen = args->namelen;
601 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000602 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200603 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /*
605 * Need to scan fix up the bestfree table.
606 */
607 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200608 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /*
610 * Need to log the data block's header.
611 */
612 if (needlog)
613 xfs_dir2_data_log_header(tp, dbp);
614 xfs_dir2_data_log_entry(tp, dbp, dep);
615 /*
616 * If the bests table needs to be changed, do it.
617 * Log the change unless we've already done that.
618 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200619 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
620 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (!grown)
622 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
623 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200624
625 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
626 highstale, &lfloglow, &lfloghigh);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /*
629 * Fill in the new leaf entry.
630 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100631 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000632 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
Nathan Scott3d693c62006-03-17 17:28:27 +1100633 be16_to_cpu(*tagp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /*
635 * Log the leaf fields and give up the buffers.
636 */
637 xfs_dir2_leaf_log_header(tp, lbp);
638 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
639 xfs_dir2_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 xfs_dir2_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 0;
642}
643
644#ifdef DEBUG
645/*
646 * Check the internal consistency of a leaf1 block.
647 * Pop an assert if something is wrong.
648 */
Hannes Eder3180e662009-03-04 19:34:10 +0100649STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650xfs_dir2_leaf_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000651 struct xfs_inode *dp, /* incore directory inode */
652 struct xfs_buf *bp) /* leaf's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 int i; /* leaf index */
655 xfs_dir2_leaf_t *leaf; /* leaf structure */
656 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
657 xfs_mount_t *mp; /* filesystem mount point */
658 int stale; /* count of stale leaves */
659
Dave Chinner1d9025e2012-06-22 18:50:14 +1000660 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 mp = dp->i_mount;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200662 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
664 * This value is not restrictive enough.
665 * Should factor in the size of the bests table as well.
666 * We can deduce a value for that from di_size.
667 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000668 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
669 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /*
671 * Leaves and bests don't overlap.
672 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100673 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000674 (char *)xfs_dir2_leaf_bests_p(ltp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /*
676 * Check hash value order, count stale entries.
677 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100678 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
679 if (i + 1 < be16_to_cpu(leaf->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100680 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
681 be32_to_cpu(leaf->ents[i + 1].hashval));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200682 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 stale++;
684 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100685 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687#endif /* DEBUG */
688
689/*
690 * Compact out any stale entries in the leaf.
691 * Log the header and changed leaf entries, if any.
692 */
693void
694xfs_dir2_leaf_compact(
695 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000696 struct xfs_buf *bp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 int from; /* source leaf index */
699 xfs_dir2_leaf_t *leaf; /* leaf structure */
700 int loglow; /* first leaf entry to log */
701 int to; /* target leaf index */
702
Dave Chinner1d9025e2012-06-22 18:50:14 +1000703 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (!leaf->hdr.stale) {
705 return;
706 }
707 /*
708 * Compress out the stale entries in place.
709 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100710 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200711 if (leaf->ents[from].address ==
712 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 continue;
714 /*
715 * Only actually copy the entries that are different.
716 */
717 if (from > to) {
718 if (loglow == -1)
719 loglow = to;
720 leaf->ents[to] = leaf->ents[from];
721 }
722 to++;
723 }
724 /*
725 * Update and log the header, log the leaf entries.
726 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100727 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800728 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 leaf->hdr.stale = 0;
730 xfs_dir2_leaf_log_header(args->trans, bp);
731 if (loglow != -1)
732 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
733}
734
735/*
736 * Compact the leaf entries, removing stale ones.
737 * Leave one stale entry behind - the one closest to our
738 * insertion index - and the caller will shift that one to our insertion
739 * point later.
740 * Return new insertion index, where the remaining stale entry is,
741 * and leaf logging indices.
742 */
743void
744xfs_dir2_leaf_compact_x1(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000745 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int *indexp, /* insertion index */
747 int *lowstalep, /* out: stale entry before us */
748 int *highstalep, /* out: stale entry after us */
749 int *lowlogp, /* out: low log index */
750 int *highlogp) /* out: high log index */
751{
752 int from; /* source copy index */
753 int highstale; /* stale entry at/after index */
754 int index; /* insertion index */
755 int keepstale; /* source index of kept stale */
756 xfs_dir2_leaf_t *leaf; /* leaf structure */
757 int lowstale; /* stale entry before index */
758 int newindex=0; /* new insertion index */
759 int to; /* destination copy index */
760
Dave Chinner1d9025e2012-06-22 18:50:14 +1000761 leaf = bp->b_addr;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100762 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 index = *indexp;
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200764
765 xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /*
768 * Pick the better of lowstale and highstale.
769 */
770 if (lowstale >= 0 &&
Nathan Scotta818e5d2006-03-17 17:28:07 +1100771 (highstale == be16_to_cpu(leaf->hdr.count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 index - lowstale <= highstale - index))
773 keepstale = lowstale;
774 else
775 keepstale = highstale;
776 /*
777 * Copy the entries in place, removing all the stale entries
778 * except keepstale.
779 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100780 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /*
782 * Notice the new value of index.
783 */
784 if (index == from)
785 newindex = to;
786 if (from != keepstale &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200787 leaf->ents[from].address ==
788 cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (from == to)
790 *lowlogp = to;
791 continue;
792 }
793 /*
794 * Record the new keepstale value for the insertion.
795 */
796 if (from == keepstale)
797 lowstale = highstale = to;
798 /*
799 * Copy only the entries that have moved.
800 */
801 if (from > to)
802 leaf->ents[to] = leaf->ents[from];
803 to++;
804 }
805 ASSERT(from > to);
806 /*
807 * If the insertion point was past the last entry,
808 * set the new insertion point accordingly.
809 */
810 if (index == from)
811 newindex = to;
812 *indexp = newindex;
813 /*
814 * Adjust the leaf header values.
815 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800816 be16_add_cpu(&leaf->hdr.count, -(from - to));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100817 leaf->hdr.stale = cpu_to_be16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /*
819 * Remember the low/high stale value only in the "right"
820 * direction.
821 */
822 if (lowstale >= newindex)
823 lowstale = -1;
824 else
Nathan Scotta818e5d2006-03-17 17:28:07 +1100825 highstale = be16_to_cpu(leaf->hdr.count);
826 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 *lowstalep = lowstale;
828 *highstalep = highstale;
829}
830
Dave Chinner9b73bd72012-06-22 18:50:15 +1000831struct xfs_dir2_leaf_map_info {
832 xfs_extlen_t map_blocks; /* number of fsbs in map */
833 xfs_dablk_t map_off; /* last mapped file offset */
834 int map_size; /* total entries in *map */
835 int map_valid; /* valid entries in *map */
836 int nmap; /* mappings to ask xfs_bmapi */
837 xfs_dir2_db_t curdb; /* db for current block */
838 int ra_current; /* number of read-ahead blks */
839 int ra_index; /* *map index for read-ahead */
840 int ra_offset; /* map entry offset for ra */
841 int ra_want; /* readahead count wanted */
842 struct xfs_bmbt_irec map[]; /* map vector for blocks */
843};
844
845STATIC int
846xfs_dir2_leaf_readbuf(
847 struct xfs_inode *dp,
848 size_t bufsize,
849 struct xfs_dir2_leaf_map_info *mip,
850 xfs_dir2_off_t *curoff,
851 struct xfs_buf **bpp)
852{
853 struct xfs_mount *mp = dp->i_mount;
854 struct xfs_buf *bp = *bpp;
855 struct xfs_bmbt_irec *map = mip->map;
856 int error = 0;
857 int length;
858 int i;
859 int j;
860
861 /*
862 * If we have a buffer, we need to release it and
863 * take it out of the mapping.
864 */
865
866 if (bp) {
867 xfs_trans_brelse(NULL, bp);
868 bp = NULL;
869 mip->map_blocks -= mp->m_dirblkfsbs;
870 /*
871 * Loop to get rid of the extents for the
872 * directory block.
873 */
874 for (i = mp->m_dirblkfsbs; i > 0; ) {
875 j = min_t(int, map->br_blockcount, i);
876 map->br_blockcount -= j;
877 map->br_startblock += j;
878 map->br_startoff += j;
879 /*
880 * If mapping is done, pitch it from
881 * the table.
882 */
883 if (!map->br_blockcount && --mip->map_valid)
884 memmove(&map[0], &map[1],
885 sizeof(map[0]) * mip->map_valid);
886 i -= j;
887 }
888 }
889
890 /*
891 * Recalculate the readahead blocks wanted.
892 */
893 mip->ra_want = howmany(bufsize + mp->m_dirblksize,
894 mp->m_sb.sb_blocksize) - 1;
895 ASSERT(mip->ra_want >= 0);
896
897 /*
898 * If we don't have as many as we want, and we haven't
899 * run out of data blocks, get some more mappings.
900 */
901 if (1 + mip->ra_want > mip->map_blocks &&
902 mip->map_off < xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
903 /*
904 * Get more bmaps, fill in after the ones
905 * we already have in the table.
906 */
907 mip->nmap = mip->map_size - mip->map_valid;
908 error = xfs_bmapi_read(dp, mip->map_off,
909 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET) -
910 mip->map_off,
911 &map[mip->map_valid], &mip->nmap, 0);
912
913 /*
914 * Don't know if we should ignore this or try to return an
915 * error. The trouble with returning errors is that readdir
916 * will just stop without actually passing the error through.
917 */
918 if (error)
919 goto out; /* XXX */
920
921 /*
922 * If we got all the mappings we asked for, set the final map
923 * offset based on the last bmap value received. Otherwise,
924 * we've reached the end.
925 */
926 if (mip->nmap == mip->map_size - mip->map_valid) {
927 i = mip->map_valid + mip->nmap - 1;
928 mip->map_off = map[i].br_startoff + map[i].br_blockcount;
929 } else
930 mip->map_off = xfs_dir2_byte_to_da(mp,
931 XFS_DIR2_LEAF_OFFSET);
932
933 /*
934 * Look for holes in the mapping, and eliminate them. Count up
935 * the valid blocks.
936 */
937 for (i = mip->map_valid; i < mip->map_valid + mip->nmap; ) {
938 if (map[i].br_startblock == HOLESTARTBLOCK) {
939 mip->nmap--;
940 length = mip->map_valid + mip->nmap - i;
941 if (length)
942 memmove(&map[i], &map[i + 1],
943 sizeof(map[i]) * length);
944 } else {
945 mip->map_blocks += map[i].br_blockcount;
946 i++;
947 }
948 }
949 mip->map_valid += mip->nmap;
950 }
951
952 /*
953 * No valid mappings, so no more data blocks.
954 */
955 if (!mip->map_valid) {
956 *curoff = xfs_dir2_da_to_byte(mp, mip->map_off);
957 goto out;
958 }
959
960 /*
961 * Read the directory block starting at the first mapping.
962 */
963 mip->curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
Dave Chinnere4813572012-11-12 22:54:14 +1100964 error = xfs_dir2_data_read(NULL, dp, map->br_startoff,
Dave Chinner9b73bd72012-06-22 18:50:15 +1000965 map->br_blockcount >= mp->m_dirblkfsbs ?
Dave Chinnere4813572012-11-12 22:54:14 +1100966 XFS_FSB_TO_DADDR(mp, map->br_startblock) : -1, &bp);
Dave Chinner9b73bd72012-06-22 18:50:15 +1000967
968 /*
969 * Should just skip over the data block instead of giving up.
970 */
971 if (error)
972 goto out; /* XXX */
973
974 /*
975 * Adjust the current amount of read-ahead: we just read a block that
976 * was previously ra.
977 */
978 if (mip->ra_current)
979 mip->ra_current -= mp->m_dirblkfsbs;
980
981 /*
982 * Do we need more readahead?
983 */
984 for (mip->ra_index = mip->ra_offset = i = 0;
985 mip->ra_want > mip->ra_current && i < mip->map_blocks;
986 i += mp->m_dirblkfsbs) {
987 ASSERT(mip->ra_index < mip->map_valid);
988 /*
989 * Read-ahead a contiguous directory block.
990 */
991 if (i > mip->ra_current &&
992 map[mip->ra_index].br_blockcount >= mp->m_dirblkfsbs) {
Dave Chinnerda6958c2012-11-12 22:54:18 +1100993 xfs_dir2_data_readahead(NULL, dp,
994 map[mip->ra_index].br_startoff + mip->ra_offset,
Dave Chinner9b73bd72012-06-22 18:50:15 +1000995 XFS_FSB_TO_DADDR(mp,
996 map[mip->ra_index].br_startblock +
Dave Chinnerda6958c2012-11-12 22:54:18 +1100997 mip->ra_offset));
Dave Chinner9b73bd72012-06-22 18:50:15 +1000998 mip->ra_current = i;
999 }
1000
1001 /*
1002 * Read-ahead a non-contiguous directory block. This doesn't
1003 * use our mapping, but this is a very rare case.
1004 */
1005 else if (i > mip->ra_current) {
Dave Chinnerda6958c2012-11-12 22:54:18 +11001006 xfs_dir2_data_readahead(NULL, dp,
Dave Chinner9b73bd72012-06-22 18:50:15 +10001007 map[mip->ra_index].br_startoff +
Dave Chinnerda6958c2012-11-12 22:54:18 +11001008 mip->ra_offset, -1);
Dave Chinner9b73bd72012-06-22 18:50:15 +10001009 mip->ra_current = i;
1010 }
1011
1012 /*
1013 * Advance offset through the mapping table.
1014 */
1015 for (j = 0; j < mp->m_dirblkfsbs; j++) {
1016 /*
1017 * The rest of this extent but not more than a dir
1018 * block.
1019 */
1020 length = min_t(int, mp->m_dirblkfsbs,
1021 map[mip->ra_index].br_blockcount -
1022 mip->ra_offset);
1023 j += length;
1024 mip->ra_offset += length;
1025
1026 /*
1027 * Advance to the next mapping if this one is used up.
1028 */
1029 if (mip->ra_offset == map[mip->ra_index].br_blockcount) {
1030 mip->ra_offset = 0;
1031 mip->ra_index++;
1032 }
1033 }
1034 }
1035
1036out:
1037 *bpp = bp;
1038 return error;
1039}
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041/*
1042 * Getdents (readdir) for leaf and node directories.
1043 * This reads the data blocks only, so is the same for both forms.
1044 */
1045int /* error */
1046xfs_dir2_leaf_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 xfs_inode_t *dp, /* incore directory inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001048 void *dirent,
1049 size_t bufsize,
1050 xfs_off_t *offset,
1051 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Dave Chinner9b73bd72012-06-22 18:50:15 +10001053 struct xfs_buf *bp = NULL; /* data block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001054 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 xfs_dir2_data_entry_t *dep; /* data entry */
1056 xfs_dir2_data_unused_t *dup; /* unused entry */
Nathan Scottf6d75cb2006-03-14 13:32:24 +11001057 int error = 0; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 int length; /* temporary length value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 xfs_mount_t *mp; /* filesystem mount point */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001060 int byteoff; /* offset in current block */
1061 xfs_dir2_off_t curoff; /* current overall offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 xfs_dir2_off_t newoff; /* new curoff after new blk */
Nathan Scottf6d75cb2006-03-14 13:32:24 +11001063 char *ptr = NULL; /* pointer to current data */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001064 struct xfs_dir2_leaf_map_info *map_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 /*
1067 * If the offset is at or past the largest allowed value,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001068 * give up right away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001070 if (*offset >= XFS_DIR2_MAX_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return 0;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 mp = dp->i_mount;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 /*
1076 * Set up to bmap a number of blocks based on the caller's
1077 * buffer size, the directory block size, and the filesystem
1078 * block size.
1079 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001080 length = howmany(bufsize + mp->m_dirblksize,
1081 mp->m_sb.sb_blocksize);
1082 map_info = kmem_zalloc(offsetof(struct xfs_dir2_leaf_map_info, map) +
1083 (length * sizeof(struct xfs_bmbt_irec)),
1084 KM_SLEEP);
1085 map_info->map_size = length;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 /*
1088 * Inside the loop we keep the main offset value as a byte offset
1089 * in the directory file.
1090 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001091 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
1094 * Force this conversion through db so we truncate the offset
1095 * down to get the start of the data block.
1096 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001097 map_info->map_off = xfs_dir2_db_to_da(mp,
1098 xfs_dir2_byte_to_db(mp, curoff));
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /*
1101 * Loop over directory entries until we reach the end offset.
1102 * Get more blocks and readahead as necessary.
1103 */
1104 while (curoff < XFS_DIR2_LEAF_OFFSET) {
1105 /*
1106 * If we have no buffer, or we're off the end of the
1107 * current buffer, need to get another one.
1108 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001109 if (!bp || ptr >= (char *)bp->b_addr + mp->m_dirblksize) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001110
Dave Chinner9b73bd72012-06-22 18:50:15 +10001111 error = xfs_dir2_leaf_readbuf(dp, bufsize, map_info,
1112 &curoff, &bp);
1113 if (error || !map_info->map_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 break;
Dave Chinner9b73bd72012-06-22 18:50:15 +10001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /*
1117 * Having done a read, we need to set a new offset.
1118 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001119 newoff = xfs_dir2_db_off_to_byte(mp, map_info->curdb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /*
1121 * Start of the current block.
1122 */
1123 if (curoff < newoff)
1124 curoff = newoff;
1125 /*
1126 * Make sure we're in the right block.
1127 */
1128 else if (curoff > newoff)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001129 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
Dave Chinner9b73bd72012-06-22 18:50:15 +10001130 map_info->curdb);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001131 hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 xfs_dir2_data_check(dp, bp);
1133 /*
1134 * Find our position in the block.
1135 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +02001136 ptr = (char *)(hdr + 1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001137 byteoff = xfs_dir2_byte_to_off(mp, curoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /*
1139 * Skip past the header.
1140 */
1141 if (byteoff == 0)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001142 curoff += (uint)sizeof(*hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 /*
1144 * Skip past entries until we reach our offset.
1145 */
1146 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001147 while ((char *)ptr - (char *)hdr < byteoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 dup = (xfs_dir2_data_unused_t *)ptr;
1149
Nathan Scottad354eb2006-03-17 17:27:37 +11001150 if (be16_to_cpu(dup->freetag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 == XFS_DIR2_DATA_FREE_TAG) {
1152
Nathan Scottad354eb2006-03-17 17:27:37 +11001153 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 ptr += length;
1155 continue;
1156 }
1157 dep = (xfs_dir2_data_entry_t *)ptr;
1158 length =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001159 xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 ptr += length;
1161 }
1162 /*
1163 * Now set our real offset.
1164 */
1165 curoff =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001166 xfs_dir2_db_off_to_byte(mp,
1167 xfs_dir2_byte_to_db(mp, curoff),
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001168 (char *)ptr - (char *)hdr);
1169 if (ptr >= (char *)hdr + mp->m_dirblksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 continue;
1171 }
1172 }
1173 }
1174 /*
1175 * We have a pointer to an entry.
1176 * Is it a live one?
1177 */
1178 dup = (xfs_dir2_data_unused_t *)ptr;
1179 /*
1180 * No, it's unused, skip over it.
1181 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001182 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1183 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 ptr += length;
1185 curoff += length;
1186 continue;
1187 }
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 dep = (xfs_dir2_data_entry_t *)ptr;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001190 length = xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Dave Chinner4a24cb72010-01-20 10:48:05 +11001192 if (filldir(dirent, (char *)dep->name, dep->namelen,
Christoph Hellwig15440312009-01-08 14:00:00 -05001193 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
Christoph Hellwiga19d9f82009-03-29 09:51:08 +02001194 be64_to_cpu(dep->inumber), DT_UNKNOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 break;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 /*
1198 * Advance to next entry in the block.
1199 */
1200 ptr += length;
1201 curoff += length;
Eric Sandeen8e69ce12009-09-25 19:42:26 +00001202 /* bufsize may have just been a guess; don't go negative */
1203 bufsize = bufsize > length ? bufsize - length : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205
1206 /*
1207 * All done. Set output offset value to current offset.
1208 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001209 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
Christoph Hellwig15440312009-01-08 14:00:00 -05001210 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 else
Christoph Hellwig15440312009-01-08 14:00:00 -05001212 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
Dave Chinner9b73bd72012-06-22 18:50:15 +10001213 kmem_free(map_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (bp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001215 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return error;
1217}
1218
1219/*
1220 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1221 */
1222int
1223xfs_dir2_leaf_init(
1224 xfs_da_args_t *args, /* operation arguments */
1225 xfs_dir2_db_t bno, /* directory block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001226 struct xfs_buf **bpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 int magic) /* magic number for block */
1228{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001229 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 xfs_inode_t *dp; /* incore directory inode */
1231 int error; /* error return code */
1232 xfs_dir2_leaf_t *leaf; /* leaf structure */
1233 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1234 xfs_mount_t *mp; /* filesystem mount point */
1235 xfs_trans_t *tp; /* transaction pointer */
1236
1237 dp = args->dp;
1238 ASSERT(dp != NULL);
1239 tp = args->trans;
1240 mp = dp->i_mount;
1241 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1242 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1243 /*
1244 * Get the buffer for the block.
1245 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001246 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001247 XFS_DATA_FORK);
1248 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /*
1252 * Initialize the header.
1253 */
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001254 leaf = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001255 leaf->hdr.info.magic = cpu_to_be16(magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 leaf->hdr.info.forw = 0;
1257 leaf->hdr.info.back = 0;
1258 leaf->hdr.count = 0;
1259 leaf->hdr.stale = 0;
1260 xfs_dir2_leaf_log_header(tp, bp);
1261 /*
1262 * If it's a leaf-format directory initialize the tail.
1263 * In this case our caller has the real bests table to copy into
1264 * the block.
1265 */
1266 if (magic == XFS_DIR2_LEAF1_MAGIC) {
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001267 bp->b_pre_io = xfs_dir2_leaf1_write_verify;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001268 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 ltp->bestcount = 0;
1270 xfs_dir2_leaf_log_tail(tp, bp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001271 } else
1272 bp->b_pre_io = xfs_dir2_leafn_write_verify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 *bpp = bp;
1274 return 0;
1275}
1276
1277/*
1278 * Log the bests entries indicated from a leaf1 block.
1279 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001280static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281xfs_dir2_leaf_log_bests(
1282 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001283 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int first, /* first entry to log */
1285 int last) /* last entry to log */
1286{
Nathan Scott68b3a102006-03-17 17:27:19 +11001287 __be16 *firstb; /* pointer to first entry */
1288 __be16 *lastb; /* pointer to last entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 xfs_dir2_leaf_t *leaf; /* leaf structure */
1290 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1291
Dave Chinner1d9025e2012-06-22 18:50:14 +10001292 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001293 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001294 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1295 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1296 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001297 xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1299}
1300
1301/*
1302 * Log the leaf entries indicated from a leaf1 or leafn block.
1303 */
1304void
1305xfs_dir2_leaf_log_ents(
1306 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001307 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int first, /* first entry to log */
1309 int last) /* last entry to log */
1310{
1311 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1312 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1313 xfs_dir2_leaf_t *leaf; /* leaf structure */
1314
Dave Chinner1d9025e2012-06-22 18:50:14 +10001315 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001316 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1317 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 firstlep = &leaf->ents[first];
1319 lastlep = &leaf->ents[last];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001320 xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1322}
1323
1324/*
1325 * Log the header of the leaf1 or leafn block.
1326 */
1327void
1328xfs_dir2_leaf_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001329 struct xfs_trans *tp,
1330 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 xfs_dir2_leaf_t *leaf; /* leaf structure */
1333
Dave Chinner1d9025e2012-06-22 18:50:14 +10001334 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001335 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1336 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001337 xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 (uint)(sizeof(leaf->hdr) - 1));
1339}
1340
1341/*
1342 * Log the tail of the leaf1 block.
1343 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001344STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345xfs_dir2_leaf_log_tail(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001346 struct xfs_trans *tp,
1347 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 xfs_dir2_leaf_t *leaf; /* leaf structure */
1350 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1351 xfs_mount_t *mp; /* filesystem mount point */
1352
1353 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001354 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001355 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001356 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001357 xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 (uint)(mp->m_dirblksize - 1));
1359}
1360
1361/*
1362 * Look up the entry referred to by args in the leaf format directory.
1363 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1364 * is also used by the node-format code.
1365 */
1366int
1367xfs_dir2_leaf_lookup(
1368 xfs_da_args_t *args) /* operation arguments */
1369{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001370 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 xfs_dir2_data_entry_t *dep; /* data block entry */
1372 xfs_inode_t *dp; /* incore directory inode */
1373 int error; /* error return code */
1374 int index; /* found entry index */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001375 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 xfs_dir2_leaf_t *leaf; /* leaf structure */
1377 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1378 xfs_trans_t *tp; /* transaction pointer */
1379
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001380 trace_xfs_dir2_leaf_lookup(args);
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /*
1383 * Look up name in the leaf block, returning both buffers and index.
1384 */
1385 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1386 return error;
1387 }
1388 tp = args->trans;
1389 dp = args->dp;
1390 xfs_dir2_leaf_check(dp, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001391 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /*
1393 * Get to the leaf entry and contained data entry address.
1394 */
1395 lep = &leaf->ents[index];
1396 /*
1397 * Point to the data entry.
1398 */
1399 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001400 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001401 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +10001403 * Return the found inode number & CI name if appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001405 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001406 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001407 xfs_trans_brelse(tp, dbp);
1408 xfs_trans_brelse(tp, lbp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001409 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
1412/*
1413 * Look up name/hash in the leaf block.
1414 * Fill in indexp with the found index, and dbpp with the data buffer.
1415 * If not found dbpp will be NULL, and ENOENT comes back.
1416 * lbpp will always be filled in with the leaf buffer unless there's an error.
1417 */
1418static int /* error */
1419xfs_dir2_leaf_lookup_int(
1420 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001421 struct xfs_buf **lbpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 int *indexp, /* out: index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001423 struct xfs_buf **dbpp) /* out: data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001425 xfs_dir2_db_t curdb = -1; /* current data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001426 struct xfs_buf *dbp = NULL; /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 xfs_dir2_data_entry_t *dep; /* data entry */
1428 xfs_inode_t *dp; /* incore directory inode */
1429 int error; /* error return code */
1430 int index; /* index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001431 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1433 xfs_dir2_leaf_t *leaf; /* leaf structure */
1434 xfs_mount_t *mp; /* filesystem mount point */
1435 xfs_dir2_db_t newdb; /* new data block number */
1436 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001437 xfs_dir2_db_t cidb = -1; /* case match data block no. */
Barry Naujok5163f952008-05-21 16:41:01 +10001438 enum xfs_dacmp cmp; /* name compare result */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 dp = args->dp;
1441 tp = args->trans;
1442 mp = dp->i_mount;
Dave Chinnere6f76672012-11-12 22:54:15 +11001443
1444 error = xfs_dir2_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001445 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 *lbpp = lbp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001449 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 xfs_dir2_leaf_check(dp, lbp);
1451 /*
1452 * Look for the first leaf entry with our hash value.
1453 */
1454 index = xfs_dir2_leaf_search_hash(args, lbp);
1455 /*
1456 * Loop over all the entries with the right hash value
1457 * looking to match the name.
1458 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001459 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
Barry Naujok5163f952008-05-21 16:41:01 +10001460 be32_to_cpu(lep->hashval) == args->hashval;
1461 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /*
1463 * Skip over stale leaf entries.
1464 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001465 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 continue;
1467 /*
1468 * Get the new data block number.
1469 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001470 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /*
1472 * If it's not the same as the old data block number,
1473 * need to pitch the old one and read the new one.
1474 */
1475 if (newdb != curdb) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001476 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001477 xfs_trans_brelse(tp, dbp);
Dave Chinnere4813572012-11-12 22:54:14 +11001478 error = xfs_dir2_data_read(tp, dp,
1479 xfs_dir2_db_to_da(mp, newdb),
1480 -1, &dbp);
Barry Naujok5163f952008-05-21 16:41:01 +10001481 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001482 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return error;
1484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 curdb = newdb;
1486 }
1487 /*
1488 * Point to the data entry.
1489 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001490 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
Barry Naujok5163f952008-05-21 16:41:01 +10001491 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 /*
Barry Naujok5163f952008-05-21 16:41:01 +10001493 * Compare name and if it's an exact match, return the index
1494 * and buffer. If it's the first case-insensitive match, store
1495 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 */
Barry Naujok5163f952008-05-21 16:41:01 +10001497 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1498 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1499 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 *indexp = index;
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001501 /* case exact match: return the current buffer. */
Barry Naujok5163f952008-05-21 16:41:01 +10001502 if (cmp == XFS_CMP_EXACT) {
Barry Naujok5163f952008-05-21 16:41:01 +10001503 *dbpp = dbp;
1504 return 0;
1505 }
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001506 cidb = curdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
1508 }
Barry Naujok6a178102008-05-21 16:42:05 +10001509 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +10001510 /*
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001511 * Here, we can only be doing a lookup (not a rename or remove).
1512 * If a case-insensitive match was found earlier, re-read the
1513 * appropriate data block if required and return it.
Barry Naujok5163f952008-05-21 16:41:01 +10001514 */
1515 if (args->cmpresult == XFS_CMP_CASE) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001516 ASSERT(cidb != -1);
1517 if (cidb != curdb) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001518 xfs_trans_brelse(tp, dbp);
Dave Chinnere4813572012-11-12 22:54:14 +11001519 error = xfs_dir2_data_read(tp, dp,
1520 xfs_dir2_db_to_da(mp, cidb),
1521 -1, &dbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001522 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001523 xfs_trans_brelse(tp, lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001524 return error;
1525 }
1526 }
1527 *dbpp = dbp;
Barry Naujok5163f952008-05-21 16:41:01 +10001528 return 0;
1529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /*
1531 * No match found, return ENOENT.
1532 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001533 ASSERT(cidb == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001535 xfs_trans_brelse(tp, dbp);
1536 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return XFS_ERROR(ENOENT);
1538}
1539
1540/*
1541 * Remove an entry from a leaf format directory.
1542 */
1543int /* error */
1544xfs_dir2_leaf_removename(
1545 xfs_da_args_t *args) /* operation arguments */
1546{
Nathan Scott68b3a102006-03-17 17:27:19 +11001547 __be16 *bestsp; /* leaf block best freespace */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001548 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001550 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 xfs_dir2_data_entry_t *dep; /* data entry structure */
1552 xfs_inode_t *dp; /* incore directory inode */
1553 int error; /* error return code */
1554 xfs_dir2_db_t i; /* temporary data block # */
1555 int index; /* index into leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001556 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 xfs_dir2_leaf_t *leaf; /* leaf structure */
1558 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1559 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1560 xfs_mount_t *mp; /* filesystem mount point */
1561 int needlog; /* need to log data header */
1562 int needscan; /* need to rescan data frees */
1563 xfs_dir2_data_off_t oldbest; /* old value of best free */
1564 xfs_trans_t *tp; /* transaction pointer */
1565
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001566 trace_xfs_dir2_leaf_removename(args);
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 /*
1569 * Lookup the leaf entry, get the leaf and data blocks read in.
1570 */
1571 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1572 return error;
1573 }
1574 dp = args->dp;
1575 tp = args->trans;
1576 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001577 leaf = lbp->b_addr;
1578 hdr = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 xfs_dir2_data_check(dp, dbp);
1580 /*
1581 * Point to the leaf entry, use that to point to the data entry.
1582 */
1583 lep = &leaf->ents[index];
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001584 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001586 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 needscan = needlog = 0;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001588 oldbest = be16_to_cpu(hdr->bestfree[0].length);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001589 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1590 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott68b3a102006-03-17 17:27:19 +11001591 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /*
1593 * Mark the former data entry unused.
1594 */
1595 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001596 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001597 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 /*
1599 * We just mark the leaf entry stale by putting a null in it.
1600 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001601 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001603 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1605 /*
1606 * Scan the freespace in the data block again if necessary,
1607 * log the data block header if necessary.
1608 */
1609 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001610 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (needlog)
1612 xfs_dir2_data_log_header(tp, dbp);
1613 /*
1614 * If the longest freespace in the data block has changed,
1615 * put the new value in the bests table and log that.
1616 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001617 if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1618 bestsp[db] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1620 }
1621 xfs_dir2_data_check(dp, dbp);
1622 /*
1623 * If the data block is now empty then get rid of the data block.
1624 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001625 if (be16_to_cpu(hdr->bestfree[0].length) ==
1626 mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 ASSERT(db != mp->m_dirdatablk);
1628 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1629 /*
1630 * Nope, can't get rid of it because it caused
1631 * allocation of a bmap btree block to do so.
1632 * Just go on, returning success, leaving the
1633 * empty block in place.
1634 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001635 if (error == ENOSPC && args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 xfs_dir2_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return error;
1639 }
1640 dbp = NULL;
1641 /*
1642 * If this is the last data block then compact the
1643 * bests table by getting rid of entries.
1644 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001645 if (db == be32_to_cpu(ltp->bestcount) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 /*
1647 * Look for the last active entry (i).
1648 */
1649 for (i = db - 1; i > 0; i--) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001650 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 break;
1652 }
1653 /*
1654 * Copy the table down so inactive entries at the
1655 * end are removed.
1656 */
1657 memmove(&bestsp[db - i], bestsp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001658 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001659 be32_add_cpu(&ltp->bestcount, -(db - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001661 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 } else
Nathan Scott68b3a102006-03-17 17:27:19 +11001663 bestsp[db] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665 /*
1666 * If the data block was not the first one, drop it.
1667 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001668 else if (db != mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 dbp = NULL;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 xfs_dir2_leaf_check(dp, lbp);
1672 /*
1673 * See if we can convert to block form.
1674 */
1675 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1676}
1677
1678/*
1679 * Replace the inode number in a leaf format directory entry.
1680 */
1681int /* error */
1682xfs_dir2_leaf_replace(
1683 xfs_da_args_t *args) /* operation arguments */
1684{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001685 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 xfs_dir2_data_entry_t *dep; /* data block entry */
1687 xfs_inode_t *dp; /* incore directory inode */
1688 int error; /* error return code */
1689 int index; /* index of leaf entry */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001690 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 xfs_dir2_leaf_t *leaf; /* leaf structure */
1692 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1693 xfs_trans_t *tp; /* transaction pointer */
1694
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001695 trace_xfs_dir2_leaf_replace(args);
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /*
1698 * Look up the entry.
1699 */
1700 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1701 return error;
1702 }
1703 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001704 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 /*
1706 * Point to the leaf entry, get data address from it.
1707 */
1708 lep = &leaf->ents[index];
1709 /*
1710 * Point to the data entry.
1711 */
1712 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001713 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001714 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001715 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /*
1717 * Put the new inode number in, log it.
1718 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001719 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 tp = args->trans;
1721 xfs_dir2_data_log_entry(tp, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 xfs_dir2_leaf_check(dp, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001723 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return 0;
1725}
1726
1727/*
1728 * Return index in the leaf block (lbp) which is either the first
1729 * one with this hash value, or if there are none, the insert point
1730 * for that hash value.
1731 */
1732int /* index value */
1733xfs_dir2_leaf_search_hash(
1734 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001735 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 xfs_dahash_t hash=0; /* hash from this entry */
1738 xfs_dahash_t hashwant; /* hash value looking for */
1739 int high; /* high leaf index */
1740 int low; /* low leaf index */
1741 xfs_dir2_leaf_t *leaf; /* leaf structure */
1742 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1743 int mid=0; /* current leaf index */
1744
Dave Chinner1d9025e2012-06-22 18:50:14 +10001745 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746#ifndef __KERNEL__
1747 if (!leaf->hdr.count)
1748 return 0;
1749#endif
1750 /*
1751 * Note, the table cannot be empty, so we have to go through the loop.
1752 * Binary search the leaf entries looking for our hash value.
1753 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001754 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 hashwant = args->hashval;
1756 low <= high; ) {
1757 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001758 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 break;
1760 if (hash < hashwant)
1761 low = mid + 1;
1762 else
1763 high = mid - 1;
1764 }
1765 /*
1766 * Found one, back up through all the equal hash values.
1767 */
1768 if (hash == hashwant) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001769 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 mid--;
1771 }
1772 }
1773 /*
1774 * Need to point to an entry higher than ours.
1775 */
1776 else if (hash < hashwant)
1777 mid++;
1778 return mid;
1779}
1780
1781/*
1782 * Trim off a trailing data block. We know it's empty since the leaf
1783 * freespace table says so.
1784 */
1785int /* error */
1786xfs_dir2_leaf_trim_data(
1787 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001788 struct xfs_buf *lbp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 xfs_dir2_db_t db) /* data block number */
1790{
Nathan Scott68b3a102006-03-17 17:27:19 +11001791 __be16 *bestsp; /* leaf bests table */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001792 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 xfs_inode_t *dp; /* incore directory inode */
1794 int error; /* error return value */
1795 xfs_dir2_leaf_t *leaf; /* leaf structure */
1796 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1797 xfs_mount_t *mp; /* filesystem mount point */
1798 xfs_trans_t *tp; /* transaction pointer */
1799
1800 dp = args->dp;
1801 mp = dp->i_mount;
1802 tp = args->trans;
1803 /*
1804 * Read the offending data block. We need its buffer.
1805 */
Dave Chinnere4813572012-11-12 22:54:14 +11001806 error = xfs_dir2_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001807 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Dave Chinner1d9025e2012-06-22 18:50:14 +10001810 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001811 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001812
1813#ifdef DEBUG
1814{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001815 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001816
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001817 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001818 ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
1819 mp->m_dirblksize - (uint)sizeof(*hdr));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001820 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001821}
1822#endif
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /*
1825 * Get rid of the data block.
1826 */
1827 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1828 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001829 xfs_trans_brelse(tp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return error;
1831 }
1832 /*
1833 * Eliminate the last bests entry from the table.
1834 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001835 bestsp = xfs_dir2_leaf_bests_p(ltp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001836 be32_add_cpu(&ltp->bestcount, -1);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001837 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001839 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return 0;
1841}
1842
Christoph Hellwig22823962011-07-08 14:35:53 +02001843static inline size_t
1844xfs_dir2_leaf_size(
1845 struct xfs_dir2_leaf_hdr *hdr,
1846 int counts)
1847{
1848 int entries;
1849
1850 entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
1851 return sizeof(xfs_dir2_leaf_hdr_t) +
1852 entries * sizeof(xfs_dir2_leaf_entry_t) +
1853 counts * sizeof(xfs_dir2_data_off_t) +
1854 sizeof(xfs_dir2_leaf_tail_t);
1855}
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857/*
1858 * Convert node form directory to leaf form directory.
1859 * The root of the node form dir needs to already be a LEAFN block.
1860 * Just return if we can't do anything.
1861 */
1862int /* error */
1863xfs_dir2_node_to_leaf(
1864 xfs_da_state_t *state) /* directory operation state */
1865{
1866 xfs_da_args_t *args; /* operation arguments */
1867 xfs_inode_t *dp; /* incore directory inode */
1868 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001869 struct xfs_buf *fbp; /* buffer for freespace block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 xfs_fileoff_t fo; /* freespace file offset */
1871 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001872 struct xfs_buf *lbp; /* buffer for leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1874 xfs_dir2_leaf_t *leaf; /* leaf structure */
1875 xfs_mount_t *mp; /* filesystem mount point */
1876 int rval; /* successful free trim? */
1877 xfs_trans_t *tp; /* transaction pointer */
1878
1879 /*
1880 * There's more than a leaf level in the btree, so there must
1881 * be multiple leafn blocks. Give up.
1882 */
1883 if (state->path.active > 1)
1884 return 0;
1885 args = state->args;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001886
1887 trace_xfs_dir2_node_to_leaf(args);
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 mp = state->mp;
1890 dp = args->dp;
1891 tp = args->trans;
1892 /*
1893 * Get the last offset in the file.
1894 */
1895 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1896 return error;
1897 }
1898 fo -= mp->m_dirblkfsbs;
1899 /*
1900 * If there are freespace blocks other than the first one,
1901 * take this opportunity to remove trailing empty freespace blocks
1902 * that may have been left behind during no-space-reservation
1903 * operations.
1904 */
1905 while (fo > mp->m_dirfreeblk) {
1906 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1907 return error;
1908 }
1909 if (rval)
1910 fo -= mp->m_dirblkfsbs;
1911 else
1912 return 0;
1913 }
1914 /*
1915 * Now find the block just before the freespace block.
1916 */
1917 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1918 return error;
1919 }
1920 /*
1921 * If it's not the single leaf block, give up.
1922 */
1923 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1924 return 0;
1925 lbp = state->path.blk[0].bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001926 leaf = lbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001927 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 /*
1929 * Read the freespace block.
1930 */
Dave Chinner20252072012-11-12 22:54:13 +11001931 error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001932 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001934 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001935 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 ASSERT(!free->hdr.firstdb);
Christoph Hellwig22823962011-07-08 14:35:53 +02001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 /*
1939 * Now see if the leafn and free data will fit in a leaf1.
1940 * If not, release the buffer and give up.
1941 */
Christoph Hellwig22823962011-07-08 14:35:53 +02001942 if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
1943 mp->m_dirblksize) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001944 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return 0;
1946 }
Christoph Hellwig22823962011-07-08 14:35:53 +02001947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 /*
1949 * If the leaf has any stale entries in it, compress them out.
1950 * The compact routine will log the header.
1951 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001952 if (be16_to_cpu(leaf->hdr.stale))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 xfs_dir2_leaf_compact(args, lbp);
1954 else
1955 xfs_dir2_leaf_log_header(tp, lbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001956
1957 lbp->b_pre_io = xfs_dir2_leaf1_write_verify;
Nathan Scott89da0542006-03-17 17:28:40 +11001958 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /*
1961 * Set up the leaf tail from the freespace block.
1962 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001963 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scott0ba962e2006-03-17 17:27:07 +11001964 ltp->bestcount = free->hdr.nvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 /*
1966 * Set up the leaf bests table.
1967 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001968 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
Christoph Hellwig22823962011-07-08 14:35:53 +02001969 be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001970 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 xfs_dir2_leaf_log_tail(tp, lbp);
1972 xfs_dir2_leaf_check(dp, lbp);
1973 /*
1974 * Get rid of the freespace block.
1975 */
1976 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1977 if (error) {
1978 /*
1979 * This can't fail here because it can only happen when
1980 * punching out the middle of an extent, and this is an
1981 * isolated block.
1982 */
1983 ASSERT(error != ENOSPC);
1984 return error;
1985 }
1986 fbp = NULL;
1987 /*
1988 * Now see if we can convert the single-leaf directory
1989 * down to a block form directory.
1990 * This routine always kills the dabuf for the leaf, so
1991 * eliminate it from the path.
1992 */
1993 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1994 state->path.blk[0].bp = NULL;
1995 return error;
1996}