blob: 5b3bcab2a656ab998e2e920829c9a081826be2c6 [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
84static void
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 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200201 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200203 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /*
205 * Set up leaf tail and bests table.
206 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000207 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100208 ltp->bestcount = cpu_to_be32(1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000209 bestsp = xfs_dir2_leaf_bests_p(ltp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200210 bestsp[0] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /*
212 * Log the data header and leaf bests table.
213 */
214 if (needlog)
215 xfs_dir2_data_log_header(tp, dbp);
216 xfs_dir2_leaf_check(dp, lbp);
217 xfs_dir2_data_check(dp, dbp);
218 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return 0;
220}
221
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200222STATIC void
223xfs_dir2_leaf_find_stale(
224 struct xfs_dir2_leaf *leaf,
225 int index,
226 int *lowstale,
227 int *highstale)
228{
229 /*
230 * Find the first stale entry before our index, if any.
231 */
232 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
233 if (leaf->ents[*lowstale].address ==
234 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
235 break;
236 }
237
238 /*
239 * Find the first stale entry at or after our index, if any.
240 * Stop if the result would require moving more entries than using
241 * lowstale.
242 */
243 for (*highstale = index;
244 *highstale < be16_to_cpu(leaf->hdr.count);
245 ++*highstale) {
246 if (leaf->ents[*highstale].address ==
247 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
248 break;
249 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
250 break;
251 }
252}
253
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200254struct xfs_dir2_leaf_entry *
255xfs_dir2_leaf_find_entry(
256 xfs_dir2_leaf_t *leaf, /* leaf structure */
257 int index, /* leaf table position */
258 int compact, /* need to compact leaves */
259 int lowstale, /* index of prev stale leaf */
260 int highstale, /* index of next stale leaf */
261 int *lfloglow, /* low leaf logging index */
262 int *lfloghigh) /* high leaf logging index */
263{
264 if (!leaf->hdr.stale) {
265 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
266
267 /*
268 * Now we need to make room to insert the leaf entry.
269 *
270 * If there are no stale entries, just insert a hole at index.
271 */
272 lep = &leaf->ents[index];
273 if (index < be16_to_cpu(leaf->hdr.count))
274 memmove(lep + 1, lep,
275 (be16_to_cpu(leaf->hdr.count) - index) *
276 sizeof(*lep));
277
278 /*
279 * Record low and high logging indices for the leaf.
280 */
281 *lfloglow = index;
282 *lfloghigh = be16_to_cpu(leaf->hdr.count);
283 be16_add_cpu(&leaf->hdr.count, 1);
284 return lep;
285 }
286
287 /*
288 * There are stale entries.
289 *
290 * We will use one of them for the new entry. It's probably not at
291 * the right location, so we'll have to shift some up or down first.
292 *
293 * If we didn't compact before, we need to find the nearest stale
294 * entries before and after our insertion point.
295 */
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200296 if (compact == 0)
297 xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200298
299 /*
300 * If the low one is better, use it.
301 */
302 if (lowstale >= 0 &&
303 (highstale == be16_to_cpu(leaf->hdr.count) ||
304 index - lowstale - 1 < highstale - index)) {
305 ASSERT(index - lowstale - 1 >= 0);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200306 ASSERT(leaf->ents[lowstale].address ==
307 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200308
309 /*
310 * Copy entries up to cover the stale entry and make room
311 * for the new entry.
312 */
313 if (index - lowstale - 1 > 0) {
314 memmove(&leaf->ents[lowstale],
315 &leaf->ents[lowstale + 1],
316 (index - lowstale - 1) *
317 sizeof(xfs_dir2_leaf_entry_t));
318 }
319 *lfloglow = MIN(lowstale, *lfloglow);
320 *lfloghigh = MAX(index - 1, *lfloghigh);
321 be16_add_cpu(&leaf->hdr.stale, -1);
322 return &leaf->ents[index - 1];
323 }
324
325 /*
326 * The high one is better, so use that one.
327 */
328 ASSERT(highstale - index >= 0);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200329 ASSERT(leaf->ents[highstale].address ==
330 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200331
332 /*
333 * Copy entries down to cover the stale entry and make room for the
334 * new entry.
335 */
336 if (highstale - index > 0) {
337 memmove(&leaf->ents[index + 1],
338 &leaf->ents[index],
339 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
340 }
341 *lfloglow = MIN(index, *lfloglow);
342 *lfloghigh = MAX(highstale, *lfloghigh);
343 be16_add_cpu(&leaf->hdr.stale, -1);
344 return &leaf->ents[index];
345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347/*
348 * Add an entry to a leaf form directory.
349 */
350int /* error */
351xfs_dir2_leaf_addname(
352 xfs_da_args_t *args) /* operation arguments */
353{
Nathan Scott68b3a102006-03-17 17:27:19 +1100354 __be16 *bestsp; /* freespace table in leaf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int compact; /* need to compact leaves */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200356 xfs_dir2_data_hdr_t *hdr; /* data block header */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000357 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 xfs_dir2_data_entry_t *dep; /* data block entry */
359 xfs_inode_t *dp; /* incore directory inode */
360 xfs_dir2_data_unused_t *dup; /* data unused entry */
361 int error; /* error return value */
362 int grown; /* allocated new data block */
363 int highstale; /* index of next stale leaf */
364 int i; /* temporary, index */
365 int index; /* leaf table position */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000366 struct xfs_buf *lbp; /* leaf's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 xfs_dir2_leaf_t *leaf; /* leaf structure */
368 int length; /* length of new entry */
369 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
370 int lfloglow; /* low leaf logging index */
371 int lfloghigh; /* high leaf logging index */
372 int lowstale; /* index of prev stale leaf */
373 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
374 xfs_mount_t *mp; /* filesystem mount point */
375 int needbytes; /* leaf block bytes needed */
376 int needlog; /* need to log data header */
377 int needscan; /* need to rescan data free */
Nathan Scott3d693c62006-03-17 17:28:27 +1100378 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 xfs_trans_t *tp; /* transaction pointer */
380 xfs_dir2_db_t use_block; /* data block number */
381
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000382 trace_xfs_dir2_leaf_addname(args);
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 dp = args->dp;
385 tp = args->trans;
386 mp = dp->i_mount;
Dave Chinnere6f76672012-11-12 22:54:15 +1100387
388 error = xfs_dir2_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100389 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +1100391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /*
393 * Look up the entry by hash value and name.
394 * We know it's not there, our caller has already done a lookup.
395 * So the index is of the entry to insert in front of.
396 * But if there are dup hash values the index is of the first of those.
397 */
398 index = xfs_dir2_leaf_search_hash(args, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000399 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000400 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
401 bestsp = xfs_dir2_leaf_bests_p(ltp);
402 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /*
404 * See if there are any entries with the same hash value
405 * and space in their block for the new entry.
406 * This is good because it puts multiple same-hash value entries
407 * in a data block, improving the lookup of those entries.
408 */
409 for (use_block = -1, lep = &leaf->ents[index];
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100410 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 index++, lep++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100412 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 continue;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000414 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100415 ASSERT(i < be32_to_cpu(ltp->bestcount));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200416 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
Nathan Scott68b3a102006-03-17 17:27:19 +1100417 if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 use_block = i;
419 break;
420 }
421 }
422 /*
423 * Didn't find a block yet, linear search all the data blocks.
424 */
425 if (use_block == -1) {
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100426 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /*
428 * Remember a block we see that's missing.
429 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200430 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
431 use_block == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 use_block = i;
Nathan Scott68b3a102006-03-17 17:27:19 +1100433 else if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 use_block = i;
435 break;
436 }
437 }
438 }
439 /*
440 * How many bytes do we need in the leaf block?
441 */
Christoph Hellwig22823962011-07-08 14:35:53 +0200442 needbytes = 0;
443 if (!leaf->hdr.stale)
444 needbytes += sizeof(xfs_dir2_leaf_entry_t);
445 if (use_block == -1)
446 needbytes += sizeof(xfs_dir2_data_off_t);
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /*
449 * Now kill use_block if it refers to a missing block, so we
450 * can use it as an indication of allocation needed.
451 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200452 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 use_block = -1;
454 /*
455 * If we don't have enough free bytes but we can make enough
456 * by compacting out stale entries, we'll do that.
457 */
Barry Naujok6a178102008-05-21 16:42:05 +1000458 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
459 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 compact = 1;
461 }
462 /*
463 * Otherwise if we don't have enough free bytes we need to
464 * convert to node form.
465 */
Barry Naujok6a178102008-05-21 16:42:05 +1000466 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
467 leaf->hdr.count)] < needbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /*
469 * Just checking or no space reservation, give up.
470 */
Barry Naujok6a178102008-05-21 16:42:05 +1000471 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
472 args->total == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000473 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return XFS_ERROR(ENOSPC);
475 }
476 /*
477 * Convert to node form.
478 */
479 error = xfs_dir2_leaf_to_node(args, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (error)
481 return error;
482 /*
483 * Then add the new entry.
484 */
485 return xfs_dir2_node_addname(args);
486 }
487 /*
488 * Otherwise it will fit without compaction.
489 */
490 else
491 compact = 0;
492 /*
493 * If just checking, then it will fit unless we needed to allocate
494 * a new data block.
495 */
Barry Naujok6a178102008-05-21 16:42:05 +1000496 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000497 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
499 }
500 /*
501 * If no allocations are allowed, return now before we've
502 * changed anything.
503 */
504 if (args->total == 0 && use_block == -1) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000505 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return XFS_ERROR(ENOSPC);
507 }
508 /*
509 * Need to compact the leaf entries, removing stale ones.
510 * Leave one stale entry behind - the one closest to our
511 * insertion index - and we'll shift that one to our insertion
512 * point later.
513 */
514 if (compact) {
515 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
516 &lfloglow, &lfloghigh);
517 }
518 /*
519 * There are stale entries, so we'll need log-low and log-high
520 * impossibly bad values later.
521 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100522 else if (be16_to_cpu(leaf->hdr.stale)) {
523 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 lfloghigh = -1;
525 }
526 /*
527 * If there was no data block space found, we need to allocate
528 * a new one.
529 */
530 if (use_block == -1) {
531 /*
532 * Add the new data block.
533 */
534 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
535 &use_block))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000536 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return error;
538 }
539 /*
540 * Initialize the block.
541 */
542 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000543 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return error;
545 }
546 /*
547 * If we're adding a new data block on the end we need to
548 * extend the bests table. Copy it up one entry.
549 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100550 if (use_block >= be32_to_cpu(ltp->bestcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 bestsp--;
552 memmove(&bestsp[0], &bestsp[1],
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100553 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800554 be32_add_cpu(&ltp->bestcount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100556 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 /*
559 * If we're filling in a previously empty block just log it.
560 */
561 else
562 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000563 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200564 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 grown = 1;
Dave Chinnere4813572012-11-12 22:54:14 +1100566 } else {
567 /*
568 * Already had space in some data block.
569 * Just read that one in.
570 */
571 error = xfs_dir2_data_read(tp, dp,
572 xfs_dir2_db_to_da(mp, use_block),
573 -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100574 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000575 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return error;
577 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000578 hdr = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 grown = 0;
580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /*
582 * Point to the biggest freespace in our data block.
583 */
584 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200585 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
Nathan Scottad354eb2006-03-17 17:27:37 +1100586 ASSERT(be16_to_cpu(dup->length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 needscan = needlog = 0;
588 /*
589 * Mark the initial part of our freespace in use for the new entry.
590 */
591 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200592 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 &needlog, &needscan);
594 /*
595 * Initialize our new entry (at last).
596 */
597 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000598 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 dep->namelen = args->namelen;
600 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000601 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200602 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /*
604 * Need to scan fix up the bestfree table.
605 */
606 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200607 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
609 * Need to log the data block's header.
610 */
611 if (needlog)
612 xfs_dir2_data_log_header(tp, dbp);
613 xfs_dir2_data_log_entry(tp, dbp, dep);
614 /*
615 * If the bests table needs to be changed, do it.
616 * Log the change unless we've already done that.
617 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200618 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
619 bestsp[use_block] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (!grown)
621 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
622 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200623
624 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
625 highstale, &lfloglow, &lfloghigh);
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /*
628 * Fill in the new leaf entry.
629 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100630 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000631 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
Nathan Scott3d693c62006-03-17 17:28:27 +1100632 be16_to_cpu(*tagp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /*
634 * Log the leaf fields and give up the buffers.
635 */
636 xfs_dir2_leaf_log_header(tp, lbp);
637 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
638 xfs_dir2_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 xfs_dir2_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return 0;
641}
642
643#ifdef DEBUG
644/*
645 * Check the internal consistency of a leaf1 block.
646 * Pop an assert if something is wrong.
647 */
Hannes Eder3180e662009-03-04 19:34:10 +0100648STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649xfs_dir2_leaf_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000650 struct xfs_inode *dp, /* incore directory inode */
651 struct xfs_buf *bp) /* leaf's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 int i; /* leaf index */
654 xfs_dir2_leaf_t *leaf; /* leaf structure */
655 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
656 xfs_mount_t *mp; /* filesystem mount point */
657 int stale; /* count of stale leaves */
658
Dave Chinner1d9025e2012-06-22 18:50:14 +1000659 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 mp = dp->i_mount;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200661 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /*
663 * This value is not restrictive enough.
664 * Should factor in the size of the bests table as well.
665 * We can deduce a value for that from di_size.
666 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000667 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
668 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /*
670 * Leaves and bests don't overlap.
671 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100672 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000673 (char *)xfs_dir2_leaf_bests_p(ltp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /*
675 * Check hash value order, count stale entries.
676 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100677 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
678 if (i + 1 < be16_to_cpu(leaf->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100679 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
680 be32_to_cpu(leaf->ents[i + 1].hashval));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200681 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 stale++;
683 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100684 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686#endif /* DEBUG */
687
688/*
689 * Compact out any stale entries in the leaf.
690 * Log the header and changed leaf entries, if any.
691 */
692void
693xfs_dir2_leaf_compact(
694 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000695 struct xfs_buf *bp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 int from; /* source leaf index */
698 xfs_dir2_leaf_t *leaf; /* leaf structure */
699 int loglow; /* first leaf entry to log */
700 int to; /* target leaf index */
701
Dave Chinner1d9025e2012-06-22 18:50:14 +1000702 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (!leaf->hdr.stale) {
704 return;
705 }
706 /*
707 * Compress out the stale entries in place.
708 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100709 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200710 if (leaf->ents[from].address ==
711 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 continue;
713 /*
714 * Only actually copy the entries that are different.
715 */
716 if (from > to) {
717 if (loglow == -1)
718 loglow = to;
719 leaf->ents[to] = leaf->ents[from];
720 }
721 to++;
722 }
723 /*
724 * Update and log the header, log the leaf entries.
725 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100726 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800727 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 leaf->hdr.stale = 0;
729 xfs_dir2_leaf_log_header(args->trans, bp);
730 if (loglow != -1)
731 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
732}
733
734/*
735 * Compact the leaf entries, removing stale ones.
736 * Leave one stale entry behind - the one closest to our
737 * insertion index - and the caller will shift that one to our insertion
738 * point later.
739 * Return new insertion index, where the remaining stale entry is,
740 * and leaf logging indices.
741 */
742void
743xfs_dir2_leaf_compact_x1(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000744 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 int *indexp, /* insertion index */
746 int *lowstalep, /* out: stale entry before us */
747 int *highstalep, /* out: stale entry after us */
748 int *lowlogp, /* out: low log index */
749 int *highlogp) /* out: high log index */
750{
751 int from; /* source copy index */
752 int highstale; /* stale entry at/after index */
753 int index; /* insertion index */
754 int keepstale; /* source index of kept stale */
755 xfs_dir2_leaf_t *leaf; /* leaf structure */
756 int lowstale; /* stale entry before index */
757 int newindex=0; /* new insertion index */
758 int to; /* destination copy index */
759
Dave Chinner1d9025e2012-06-22 18:50:14 +1000760 leaf = bp->b_addr;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100761 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 index = *indexp;
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200763
764 xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /*
767 * Pick the better of lowstale and highstale.
768 */
769 if (lowstale >= 0 &&
Nathan Scotta818e5d2006-03-17 17:28:07 +1100770 (highstale == be16_to_cpu(leaf->hdr.count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 index - lowstale <= highstale - index))
772 keepstale = lowstale;
773 else
774 keepstale = highstale;
775 /*
776 * Copy the entries in place, removing all the stale entries
777 * except keepstale.
778 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100779 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /*
781 * Notice the new value of index.
782 */
783 if (index == from)
784 newindex = to;
785 if (from != keepstale &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200786 leaf->ents[from].address ==
787 cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (from == to)
789 *lowlogp = to;
790 continue;
791 }
792 /*
793 * Record the new keepstale value for the insertion.
794 */
795 if (from == keepstale)
796 lowstale = highstale = to;
797 /*
798 * Copy only the entries that have moved.
799 */
800 if (from > to)
801 leaf->ents[to] = leaf->ents[from];
802 to++;
803 }
804 ASSERT(from > to);
805 /*
806 * If the insertion point was past the last entry,
807 * set the new insertion point accordingly.
808 */
809 if (index == from)
810 newindex = to;
811 *indexp = newindex;
812 /*
813 * Adjust the leaf header values.
814 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800815 be16_add_cpu(&leaf->hdr.count, -(from - to));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100816 leaf->hdr.stale = cpu_to_be16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /*
818 * Remember the low/high stale value only in the "right"
819 * direction.
820 */
821 if (lowstale >= newindex)
822 lowstale = -1;
823 else
Nathan Scotta818e5d2006-03-17 17:28:07 +1100824 highstale = be16_to_cpu(leaf->hdr.count);
825 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 *lowstalep = lowstale;
827 *highstalep = highstale;
828}
829
Dave Chinner9b73bd72012-06-22 18:50:15 +1000830struct xfs_dir2_leaf_map_info {
831 xfs_extlen_t map_blocks; /* number of fsbs in map */
832 xfs_dablk_t map_off; /* last mapped file offset */
833 int map_size; /* total entries in *map */
834 int map_valid; /* valid entries in *map */
835 int nmap; /* mappings to ask xfs_bmapi */
836 xfs_dir2_db_t curdb; /* db for current block */
837 int ra_current; /* number of read-ahead blks */
838 int ra_index; /* *map index for read-ahead */
839 int ra_offset; /* map entry offset for ra */
840 int ra_want; /* readahead count wanted */
841 struct xfs_bmbt_irec map[]; /* map vector for blocks */
842};
843
844STATIC int
845xfs_dir2_leaf_readbuf(
846 struct xfs_inode *dp,
847 size_t bufsize,
848 struct xfs_dir2_leaf_map_info *mip,
849 xfs_dir2_off_t *curoff,
850 struct xfs_buf **bpp)
851{
852 struct xfs_mount *mp = dp->i_mount;
853 struct xfs_buf *bp = *bpp;
854 struct xfs_bmbt_irec *map = mip->map;
855 int error = 0;
856 int length;
857 int i;
858 int j;
859
860 /*
861 * If we have a buffer, we need to release it and
862 * take it out of the mapping.
863 */
864
865 if (bp) {
866 xfs_trans_brelse(NULL, bp);
867 bp = NULL;
868 mip->map_blocks -= mp->m_dirblkfsbs;
869 /*
870 * Loop to get rid of the extents for the
871 * directory block.
872 */
873 for (i = mp->m_dirblkfsbs; i > 0; ) {
874 j = min_t(int, map->br_blockcount, i);
875 map->br_blockcount -= j;
876 map->br_startblock += j;
877 map->br_startoff += j;
878 /*
879 * If mapping is done, pitch it from
880 * the table.
881 */
882 if (!map->br_blockcount && --mip->map_valid)
883 memmove(&map[0], &map[1],
884 sizeof(map[0]) * mip->map_valid);
885 i -= j;
886 }
887 }
888
889 /*
890 * Recalculate the readahead blocks wanted.
891 */
892 mip->ra_want = howmany(bufsize + mp->m_dirblksize,
893 mp->m_sb.sb_blocksize) - 1;
894 ASSERT(mip->ra_want >= 0);
895
896 /*
897 * If we don't have as many as we want, and we haven't
898 * run out of data blocks, get some more mappings.
899 */
900 if (1 + mip->ra_want > mip->map_blocks &&
901 mip->map_off < xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
902 /*
903 * Get more bmaps, fill in after the ones
904 * we already have in the table.
905 */
906 mip->nmap = mip->map_size - mip->map_valid;
907 error = xfs_bmapi_read(dp, mip->map_off,
908 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET) -
909 mip->map_off,
910 &map[mip->map_valid], &mip->nmap, 0);
911
912 /*
913 * Don't know if we should ignore this or try to return an
914 * error. The trouble with returning errors is that readdir
915 * will just stop without actually passing the error through.
916 */
917 if (error)
918 goto out; /* XXX */
919
920 /*
921 * If we got all the mappings we asked for, set the final map
922 * offset based on the last bmap value received. Otherwise,
923 * we've reached the end.
924 */
925 if (mip->nmap == mip->map_size - mip->map_valid) {
926 i = mip->map_valid + mip->nmap - 1;
927 mip->map_off = map[i].br_startoff + map[i].br_blockcount;
928 } else
929 mip->map_off = xfs_dir2_byte_to_da(mp,
930 XFS_DIR2_LEAF_OFFSET);
931
932 /*
933 * Look for holes in the mapping, and eliminate them. Count up
934 * the valid blocks.
935 */
936 for (i = mip->map_valid; i < mip->map_valid + mip->nmap; ) {
937 if (map[i].br_startblock == HOLESTARTBLOCK) {
938 mip->nmap--;
939 length = mip->map_valid + mip->nmap - i;
940 if (length)
941 memmove(&map[i], &map[i + 1],
942 sizeof(map[i]) * length);
943 } else {
944 mip->map_blocks += map[i].br_blockcount;
945 i++;
946 }
947 }
948 mip->map_valid += mip->nmap;
949 }
950
951 /*
952 * No valid mappings, so no more data blocks.
953 */
954 if (!mip->map_valid) {
955 *curoff = xfs_dir2_da_to_byte(mp, mip->map_off);
956 goto out;
957 }
958
959 /*
960 * Read the directory block starting at the first mapping.
961 */
962 mip->curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
Dave Chinnere4813572012-11-12 22:54:14 +1100963 error = xfs_dir2_data_read(NULL, dp, map->br_startoff,
Dave Chinner9b73bd72012-06-22 18:50:15 +1000964 map->br_blockcount >= mp->m_dirblkfsbs ?
Dave Chinnere4813572012-11-12 22:54:14 +1100965 XFS_FSB_TO_DADDR(mp, map->br_startblock) : -1, &bp);
Dave Chinner9b73bd72012-06-22 18:50:15 +1000966
967 /*
968 * Should just skip over the data block instead of giving up.
969 */
970 if (error)
971 goto out; /* XXX */
972
973 /*
974 * Adjust the current amount of read-ahead: we just read a block that
975 * was previously ra.
976 */
977 if (mip->ra_current)
978 mip->ra_current -= mp->m_dirblkfsbs;
979
980 /*
981 * Do we need more readahead?
982 */
983 for (mip->ra_index = mip->ra_offset = i = 0;
984 mip->ra_want > mip->ra_current && i < mip->map_blocks;
985 i += mp->m_dirblkfsbs) {
986 ASSERT(mip->ra_index < mip->map_valid);
987 /*
988 * Read-ahead a contiguous directory block.
989 */
990 if (i > mip->ra_current &&
991 map[mip->ra_index].br_blockcount >= mp->m_dirblkfsbs) {
Dave Chinnerda6958c2012-11-12 22:54:18 +1100992 xfs_dir2_data_readahead(NULL, dp,
993 map[mip->ra_index].br_startoff + mip->ra_offset,
Dave Chinner9b73bd72012-06-22 18:50:15 +1000994 XFS_FSB_TO_DADDR(mp,
995 map[mip->ra_index].br_startblock +
Dave Chinnerda6958c2012-11-12 22:54:18 +1100996 mip->ra_offset));
Dave Chinner9b73bd72012-06-22 18:50:15 +1000997 mip->ra_current = i;
998 }
999
1000 /*
1001 * Read-ahead a non-contiguous directory block. This doesn't
1002 * use our mapping, but this is a very rare case.
1003 */
1004 else if (i > mip->ra_current) {
Dave Chinnerda6958c2012-11-12 22:54:18 +11001005 xfs_dir2_data_readahead(NULL, dp,
Dave Chinner9b73bd72012-06-22 18:50:15 +10001006 map[mip->ra_index].br_startoff +
Dave Chinnerda6958c2012-11-12 22:54:18 +11001007 mip->ra_offset, -1);
Dave Chinner9b73bd72012-06-22 18:50:15 +10001008 mip->ra_current = i;
1009 }
1010
1011 /*
1012 * Advance offset through the mapping table.
1013 */
1014 for (j = 0; j < mp->m_dirblkfsbs; j++) {
1015 /*
1016 * The rest of this extent but not more than a dir
1017 * block.
1018 */
1019 length = min_t(int, mp->m_dirblkfsbs,
1020 map[mip->ra_index].br_blockcount -
1021 mip->ra_offset);
1022 j += length;
1023 mip->ra_offset += length;
1024
1025 /*
1026 * Advance to the next mapping if this one is used up.
1027 */
1028 if (mip->ra_offset == map[mip->ra_index].br_blockcount) {
1029 mip->ra_offset = 0;
1030 mip->ra_index++;
1031 }
1032 }
1033 }
1034
1035out:
1036 *bpp = bp;
1037 return error;
1038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/*
1041 * Getdents (readdir) for leaf and node directories.
1042 * This reads the data blocks only, so is the same for both forms.
1043 */
1044int /* error */
1045xfs_dir2_leaf_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 xfs_inode_t *dp, /* incore directory inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001047 void *dirent,
1048 size_t bufsize,
1049 xfs_off_t *offset,
1050 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Dave Chinner9b73bd72012-06-22 18:50:15 +10001052 struct xfs_buf *bp = NULL; /* data block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001053 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 xfs_dir2_data_entry_t *dep; /* data entry */
1055 xfs_dir2_data_unused_t *dup; /* unused entry */
Nathan Scottf6d75cb2006-03-14 13:32:24 +11001056 int error = 0; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 int length; /* temporary length value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 xfs_mount_t *mp; /* filesystem mount point */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001059 int byteoff; /* offset in current block */
1060 xfs_dir2_off_t curoff; /* current overall offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 xfs_dir2_off_t newoff; /* new curoff after new blk */
Nathan Scottf6d75cb2006-03-14 13:32:24 +11001062 char *ptr = NULL; /* pointer to current data */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001063 struct xfs_dir2_leaf_map_info *map_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 /*
1066 * If the offset is at or past the largest allowed value,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001067 * give up right away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001069 if (*offset >= XFS_DIR2_MAX_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return 0;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 mp = dp->i_mount;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /*
1075 * Set up to bmap a number of blocks based on the caller's
1076 * buffer size, the directory block size, and the filesystem
1077 * block size.
1078 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001079 length = howmany(bufsize + mp->m_dirblksize,
1080 mp->m_sb.sb_blocksize);
1081 map_info = kmem_zalloc(offsetof(struct xfs_dir2_leaf_map_info, map) +
1082 (length * sizeof(struct xfs_bmbt_irec)),
1083 KM_SLEEP);
1084 map_info->map_size = length;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /*
1087 * Inside the loop we keep the main offset value as a byte offset
1088 * in the directory file.
1089 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001090 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /*
1093 * Force this conversion through db so we truncate the offset
1094 * down to get the start of the data block.
1095 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001096 map_info->map_off = xfs_dir2_db_to_da(mp,
1097 xfs_dir2_byte_to_db(mp, curoff));
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /*
1100 * Loop over directory entries until we reach the end offset.
1101 * Get more blocks and readahead as necessary.
1102 */
1103 while (curoff < XFS_DIR2_LEAF_OFFSET) {
1104 /*
1105 * If we have no buffer, or we're off the end of the
1106 * current buffer, need to get another one.
1107 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001108 if (!bp || ptr >= (char *)bp->b_addr + mp->m_dirblksize) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001109
Dave Chinner9b73bd72012-06-22 18:50:15 +10001110 error = xfs_dir2_leaf_readbuf(dp, bufsize, map_info,
1111 &curoff, &bp);
1112 if (error || !map_info->map_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 break;
Dave Chinner9b73bd72012-06-22 18:50:15 +10001114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 /*
1116 * Having done a read, we need to set a new offset.
1117 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001118 newoff = xfs_dir2_db_off_to_byte(mp, map_info->curdb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /*
1120 * Start of the current block.
1121 */
1122 if (curoff < newoff)
1123 curoff = newoff;
1124 /*
1125 * Make sure we're in the right block.
1126 */
1127 else if (curoff > newoff)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001128 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
Dave Chinner9b73bd72012-06-22 18:50:15 +10001129 map_info->curdb);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001130 hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 xfs_dir2_data_check(dp, bp);
1132 /*
1133 * Find our position in the block.
1134 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +02001135 ptr = (char *)(hdr + 1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001136 byteoff = xfs_dir2_byte_to_off(mp, curoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /*
1138 * Skip past the header.
1139 */
1140 if (byteoff == 0)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001141 curoff += (uint)sizeof(*hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 /*
1143 * Skip past entries until we reach our offset.
1144 */
1145 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001146 while ((char *)ptr - (char *)hdr < byteoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 dup = (xfs_dir2_data_unused_t *)ptr;
1148
Nathan Scottad354eb2006-03-17 17:27:37 +11001149 if (be16_to_cpu(dup->freetag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 == XFS_DIR2_DATA_FREE_TAG) {
1151
Nathan Scottad354eb2006-03-17 17:27:37 +11001152 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 ptr += length;
1154 continue;
1155 }
1156 dep = (xfs_dir2_data_entry_t *)ptr;
1157 length =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001158 xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 ptr += length;
1160 }
1161 /*
1162 * Now set our real offset.
1163 */
1164 curoff =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001165 xfs_dir2_db_off_to_byte(mp,
1166 xfs_dir2_byte_to_db(mp, curoff),
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001167 (char *)ptr - (char *)hdr);
1168 if (ptr >= (char *)hdr + mp->m_dirblksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 continue;
1170 }
1171 }
1172 }
1173 /*
1174 * We have a pointer to an entry.
1175 * Is it a live one?
1176 */
1177 dup = (xfs_dir2_data_unused_t *)ptr;
1178 /*
1179 * No, it's unused, skip over it.
1180 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001181 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1182 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 ptr += length;
1184 curoff += length;
1185 continue;
1186 }
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 dep = (xfs_dir2_data_entry_t *)ptr;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001189 length = xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Dave Chinner4a24cb72010-01-20 10:48:05 +11001191 if (filldir(dirent, (char *)dep->name, dep->namelen,
Christoph Hellwig15440312009-01-08 14:00:00 -05001192 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
Christoph Hellwiga19d9f82009-03-29 09:51:08 +02001193 be64_to_cpu(dep->inumber), DT_UNKNOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 break;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /*
1197 * Advance to next entry in the block.
1198 */
1199 ptr += length;
1200 curoff += length;
Eric Sandeen8e69ce12009-09-25 19:42:26 +00001201 /* bufsize may have just been a guess; don't go negative */
1202 bufsize = bufsize > length ? bufsize - length : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204
1205 /*
1206 * All done. Set output offset value to current offset.
1207 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001208 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
Christoph Hellwig15440312009-01-08 14:00:00 -05001209 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 else
Christoph Hellwig15440312009-01-08 14:00:00 -05001211 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
Dave Chinner9b73bd72012-06-22 18:50:15 +10001212 kmem_free(map_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (bp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001214 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return error;
1216}
1217
1218/*
1219 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1220 */
1221int
1222xfs_dir2_leaf_init(
1223 xfs_da_args_t *args, /* operation arguments */
1224 xfs_dir2_db_t bno, /* directory block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001225 struct xfs_buf **bpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 int magic) /* magic number for block */
1227{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001228 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 xfs_inode_t *dp; /* incore directory inode */
1230 int error; /* error return code */
1231 xfs_dir2_leaf_t *leaf; /* leaf structure */
1232 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1233 xfs_mount_t *mp; /* filesystem mount point */
1234 xfs_trans_t *tp; /* transaction pointer */
1235
1236 dp = args->dp;
1237 ASSERT(dp != NULL);
1238 tp = args->trans;
1239 mp = dp->i_mount;
1240 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1241 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1242 /*
1243 * Get the buffer for the block.
1244 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001245 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 XFS_DATA_FORK);
1247 if (error) {
1248 return error;
1249 }
1250 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001251 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 /*
1253 * Initialize the header.
1254 */
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) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001267 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 ltp->bestcount = 0;
1269 xfs_dir2_leaf_log_tail(tp, bp);
1270 }
1271 *bpp = bp;
1272 return 0;
1273}
1274
1275/*
1276 * Log the bests entries indicated from a leaf1 block.
1277 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001278static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279xfs_dir2_leaf_log_bests(
1280 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001281 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 int first, /* first entry to log */
1283 int last) /* last entry to log */
1284{
Nathan Scott68b3a102006-03-17 17:27:19 +11001285 __be16 *firstb; /* pointer to first entry */
1286 __be16 *lastb; /* pointer to last entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 xfs_dir2_leaf_t *leaf; /* leaf structure */
1288 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1289
Dave Chinner1d9025e2012-06-22 18:50:14 +10001290 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001291 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001292 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1293 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1294 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001295 xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1297}
1298
1299/*
1300 * Log the leaf entries indicated from a leaf1 or leafn block.
1301 */
1302void
1303xfs_dir2_leaf_log_ents(
1304 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001305 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int first, /* first entry to log */
1307 int last) /* last entry to log */
1308{
1309 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1310 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1311 xfs_dir2_leaf_t *leaf; /* leaf structure */
1312
Dave Chinner1d9025e2012-06-22 18:50:14 +10001313 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001314 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1315 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 firstlep = &leaf->ents[first];
1317 lastlep = &leaf->ents[last];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001318 xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1320}
1321
1322/*
1323 * Log the header of the leaf1 or leafn block.
1324 */
1325void
1326xfs_dir2_leaf_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001327 struct xfs_trans *tp,
1328 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 xfs_dir2_leaf_t *leaf; /* leaf structure */
1331
Dave Chinner1d9025e2012-06-22 18:50:14 +10001332 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001333 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1334 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001335 xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 (uint)(sizeof(leaf->hdr) - 1));
1337}
1338
1339/*
1340 * Log the tail of the leaf1 block.
1341 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001342STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343xfs_dir2_leaf_log_tail(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001344 struct xfs_trans *tp,
1345 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 xfs_dir2_leaf_t *leaf; /* leaf structure */
1348 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1349 xfs_mount_t *mp; /* filesystem mount point */
1350
1351 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001352 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001353 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001354 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001355 xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 (uint)(mp->m_dirblksize - 1));
1357}
1358
1359/*
1360 * Look up the entry referred to by args in the leaf format directory.
1361 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1362 * is also used by the node-format code.
1363 */
1364int
1365xfs_dir2_leaf_lookup(
1366 xfs_da_args_t *args) /* operation arguments */
1367{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001368 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 xfs_dir2_data_entry_t *dep; /* data block entry */
1370 xfs_inode_t *dp; /* incore directory inode */
1371 int error; /* error return code */
1372 int index; /* found entry index */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001373 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 xfs_dir2_leaf_t *leaf; /* leaf structure */
1375 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1376 xfs_trans_t *tp; /* transaction pointer */
1377
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001378 trace_xfs_dir2_leaf_lookup(args);
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /*
1381 * Look up name in the leaf block, returning both buffers and index.
1382 */
1383 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1384 return error;
1385 }
1386 tp = args->trans;
1387 dp = args->dp;
1388 xfs_dir2_leaf_check(dp, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001389 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /*
1391 * Get to the leaf entry and contained data entry address.
1392 */
1393 lep = &leaf->ents[index];
1394 /*
1395 * Point to the data entry.
1396 */
1397 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001398 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001399 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +10001401 * Return the found inode number & CI name if appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001403 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001404 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001405 xfs_trans_brelse(tp, dbp);
1406 xfs_trans_brelse(tp, lbp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001407 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
1410/*
1411 * Look up name/hash in the leaf block.
1412 * Fill in indexp with the found index, and dbpp with the data buffer.
1413 * If not found dbpp will be NULL, and ENOENT comes back.
1414 * lbpp will always be filled in with the leaf buffer unless there's an error.
1415 */
1416static int /* error */
1417xfs_dir2_leaf_lookup_int(
1418 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001419 struct xfs_buf **lbpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 int *indexp, /* out: index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001421 struct xfs_buf **dbpp) /* out: data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001423 xfs_dir2_db_t curdb = -1; /* current data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001424 struct xfs_buf *dbp = NULL; /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 xfs_dir2_data_entry_t *dep; /* data entry */
1426 xfs_inode_t *dp; /* incore directory inode */
1427 int error; /* error return code */
1428 int index; /* index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001429 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1431 xfs_dir2_leaf_t *leaf; /* leaf structure */
1432 xfs_mount_t *mp; /* filesystem mount point */
1433 xfs_dir2_db_t newdb; /* new data block number */
1434 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001435 xfs_dir2_db_t cidb = -1; /* case match data block no. */
Barry Naujok5163f952008-05-21 16:41:01 +10001436 enum xfs_dacmp cmp; /* name compare result */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 dp = args->dp;
1439 tp = args->trans;
1440 mp = dp->i_mount;
Dave Chinnere6f76672012-11-12 22:54:15 +11001441
1442 error = xfs_dir2_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001443 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 *lbpp = lbp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001447 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 xfs_dir2_leaf_check(dp, lbp);
1449 /*
1450 * Look for the first leaf entry with our hash value.
1451 */
1452 index = xfs_dir2_leaf_search_hash(args, lbp);
1453 /*
1454 * Loop over all the entries with the right hash value
1455 * looking to match the name.
1456 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001457 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
Barry Naujok5163f952008-05-21 16:41:01 +10001458 be32_to_cpu(lep->hashval) == args->hashval;
1459 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /*
1461 * Skip over stale leaf entries.
1462 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001463 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 continue;
1465 /*
1466 * Get the new data block number.
1467 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001468 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /*
1470 * If it's not the same as the old data block number,
1471 * need to pitch the old one and read the new one.
1472 */
1473 if (newdb != curdb) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001474 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001475 xfs_trans_brelse(tp, dbp);
Dave Chinnere4813572012-11-12 22:54:14 +11001476 error = xfs_dir2_data_read(tp, dp,
1477 xfs_dir2_db_to_da(mp, newdb),
1478 -1, &dbp);
Barry Naujok5163f952008-05-21 16:41:01 +10001479 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001480 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return error;
1482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 curdb = newdb;
1484 }
1485 /*
1486 * Point to the data entry.
1487 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001488 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
Barry Naujok5163f952008-05-21 16:41:01 +10001489 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 /*
Barry Naujok5163f952008-05-21 16:41:01 +10001491 * Compare name and if it's an exact match, return the index
1492 * and buffer. If it's the first case-insensitive match, store
1493 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 */
Barry Naujok5163f952008-05-21 16:41:01 +10001495 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1496 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1497 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 *indexp = index;
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001499 /* case exact match: return the current buffer. */
Barry Naujok5163f952008-05-21 16:41:01 +10001500 if (cmp == XFS_CMP_EXACT) {
Barry Naujok5163f952008-05-21 16:41:01 +10001501 *dbpp = dbp;
1502 return 0;
1503 }
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001504 cidb = curdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506 }
Barry Naujok6a178102008-05-21 16:42:05 +10001507 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +10001508 /*
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001509 * Here, we can only be doing a lookup (not a rename or remove).
1510 * If a case-insensitive match was found earlier, re-read the
1511 * appropriate data block if required and return it.
Barry Naujok5163f952008-05-21 16:41:01 +10001512 */
1513 if (args->cmpresult == XFS_CMP_CASE) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001514 ASSERT(cidb != -1);
1515 if (cidb != curdb) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001516 xfs_trans_brelse(tp, dbp);
Dave Chinnere4813572012-11-12 22:54:14 +11001517 error = xfs_dir2_data_read(tp, dp,
1518 xfs_dir2_db_to_da(mp, cidb),
1519 -1, &dbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001520 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001521 xfs_trans_brelse(tp, lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001522 return error;
1523 }
1524 }
1525 *dbpp = dbp;
Barry Naujok5163f952008-05-21 16:41:01 +10001526 return 0;
1527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 /*
1529 * No match found, return ENOENT.
1530 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001531 ASSERT(cidb == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001533 xfs_trans_brelse(tp, dbp);
1534 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return XFS_ERROR(ENOENT);
1536}
1537
1538/*
1539 * Remove an entry from a leaf format directory.
1540 */
1541int /* error */
1542xfs_dir2_leaf_removename(
1543 xfs_da_args_t *args) /* operation arguments */
1544{
Nathan Scott68b3a102006-03-17 17:27:19 +11001545 __be16 *bestsp; /* leaf block best freespace */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001546 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001548 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 xfs_dir2_data_entry_t *dep; /* data entry structure */
1550 xfs_inode_t *dp; /* incore directory inode */
1551 int error; /* error return code */
1552 xfs_dir2_db_t i; /* temporary data block # */
1553 int index; /* index into leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001554 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 xfs_dir2_leaf_t *leaf; /* leaf structure */
1556 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1557 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1558 xfs_mount_t *mp; /* filesystem mount point */
1559 int needlog; /* need to log data header */
1560 int needscan; /* need to rescan data frees */
1561 xfs_dir2_data_off_t oldbest; /* old value of best free */
1562 xfs_trans_t *tp; /* transaction pointer */
1563
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001564 trace_xfs_dir2_leaf_removename(args);
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 /*
1567 * Lookup the leaf entry, get the leaf and data blocks read in.
1568 */
1569 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1570 return error;
1571 }
1572 dp = args->dp;
1573 tp = args->trans;
1574 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001575 leaf = lbp->b_addr;
1576 hdr = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 xfs_dir2_data_check(dp, dbp);
1578 /*
1579 * Point to the leaf entry, use that to point to the data entry.
1580 */
1581 lep = &leaf->ents[index];
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001582 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001584 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 needscan = needlog = 0;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001586 oldbest = be16_to_cpu(hdr->bestfree[0].length);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001587 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1588 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott68b3a102006-03-17 17:27:19 +11001589 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 /*
1591 * Mark the former data entry unused.
1592 */
1593 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001594 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001595 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 /*
1597 * We just mark the leaf entry stale by putting a null in it.
1598 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001599 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001601 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1603 /*
1604 * Scan the freespace in the data block again if necessary,
1605 * log the data block header if necessary.
1606 */
1607 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001608 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (needlog)
1610 xfs_dir2_data_log_header(tp, dbp);
1611 /*
1612 * If the longest freespace in the data block has changed,
1613 * put the new value in the bests table and log that.
1614 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001615 if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1616 bestsp[db] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1618 }
1619 xfs_dir2_data_check(dp, dbp);
1620 /*
1621 * If the data block is now empty then get rid of the data block.
1622 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001623 if (be16_to_cpu(hdr->bestfree[0].length) ==
1624 mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 ASSERT(db != mp->m_dirdatablk);
1626 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1627 /*
1628 * Nope, can't get rid of it because it caused
1629 * allocation of a bmap btree block to do so.
1630 * Just go on, returning success, leaving the
1631 * empty block in place.
1632 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001633 if (error == ENOSPC && args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 xfs_dir2_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return error;
1637 }
1638 dbp = NULL;
1639 /*
1640 * If this is the last data block then compact the
1641 * bests table by getting rid of entries.
1642 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001643 if (db == be32_to_cpu(ltp->bestcount) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /*
1645 * Look for the last active entry (i).
1646 */
1647 for (i = db - 1; i > 0; i--) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001648 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 break;
1650 }
1651 /*
1652 * Copy the table down so inactive entries at the
1653 * end are removed.
1654 */
1655 memmove(&bestsp[db - i], bestsp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001656 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001657 be32_add_cpu(&ltp->bestcount, -(db - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001659 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 } else
Nathan Scott68b3a102006-03-17 17:27:19 +11001661 bestsp[db] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
1663 /*
1664 * If the data block was not the first one, drop it.
1665 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001666 else if (db != mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 dbp = NULL;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 xfs_dir2_leaf_check(dp, lbp);
1670 /*
1671 * See if we can convert to block form.
1672 */
1673 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1674}
1675
1676/*
1677 * Replace the inode number in a leaf format directory entry.
1678 */
1679int /* error */
1680xfs_dir2_leaf_replace(
1681 xfs_da_args_t *args) /* operation arguments */
1682{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001683 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 xfs_dir2_data_entry_t *dep; /* data block entry */
1685 xfs_inode_t *dp; /* incore directory inode */
1686 int error; /* error return code */
1687 int index; /* index of leaf entry */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001688 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 xfs_dir2_leaf_t *leaf; /* leaf structure */
1690 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1691 xfs_trans_t *tp; /* transaction pointer */
1692
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001693 trace_xfs_dir2_leaf_replace(args);
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /*
1696 * Look up the entry.
1697 */
1698 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1699 return error;
1700 }
1701 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001702 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 /*
1704 * Point to the leaf entry, get data address from it.
1705 */
1706 lep = &leaf->ents[index];
1707 /*
1708 * Point to the data entry.
1709 */
1710 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001711 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001712 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001713 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /*
1715 * Put the new inode number in, log it.
1716 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001717 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 tp = args->trans;
1719 xfs_dir2_data_log_entry(tp, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 xfs_dir2_leaf_check(dp, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001721 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return 0;
1723}
1724
1725/*
1726 * Return index in the leaf block (lbp) which is either the first
1727 * one with this hash value, or if there are none, the insert point
1728 * for that hash value.
1729 */
1730int /* index value */
1731xfs_dir2_leaf_search_hash(
1732 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001733 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 xfs_dahash_t hash=0; /* hash from this entry */
1736 xfs_dahash_t hashwant; /* hash value looking for */
1737 int high; /* high leaf index */
1738 int low; /* low leaf index */
1739 xfs_dir2_leaf_t *leaf; /* leaf structure */
1740 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1741 int mid=0; /* current leaf index */
1742
Dave Chinner1d9025e2012-06-22 18:50:14 +10001743 leaf = lbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744#ifndef __KERNEL__
1745 if (!leaf->hdr.count)
1746 return 0;
1747#endif
1748 /*
1749 * Note, the table cannot be empty, so we have to go through the loop.
1750 * Binary search the leaf entries looking for our hash value.
1751 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001752 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 hashwant = args->hashval;
1754 low <= high; ) {
1755 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001756 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 break;
1758 if (hash < hashwant)
1759 low = mid + 1;
1760 else
1761 high = mid - 1;
1762 }
1763 /*
1764 * Found one, back up through all the equal hash values.
1765 */
1766 if (hash == hashwant) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001767 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 mid--;
1769 }
1770 }
1771 /*
1772 * Need to point to an entry higher than ours.
1773 */
1774 else if (hash < hashwant)
1775 mid++;
1776 return mid;
1777}
1778
1779/*
1780 * Trim off a trailing data block. We know it's empty since the leaf
1781 * freespace table says so.
1782 */
1783int /* error */
1784xfs_dir2_leaf_trim_data(
1785 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001786 struct xfs_buf *lbp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 xfs_dir2_db_t db) /* data block number */
1788{
Nathan Scott68b3a102006-03-17 17:27:19 +11001789 __be16 *bestsp; /* leaf bests table */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001790 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 xfs_inode_t *dp; /* incore directory inode */
1792 int error; /* error return value */
1793 xfs_dir2_leaf_t *leaf; /* leaf structure */
1794 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1795 xfs_mount_t *mp; /* filesystem mount point */
1796 xfs_trans_t *tp; /* transaction pointer */
1797
1798 dp = args->dp;
1799 mp = dp->i_mount;
1800 tp = args->trans;
1801 /*
1802 * Read the offending data block. We need its buffer.
1803 */
Dave Chinnere4813572012-11-12 22:54:14 +11001804 error = xfs_dir2_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001805 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Dave Chinner1d9025e2012-06-22 18:50:14 +10001808 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001809 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001810
1811#ifdef DEBUG
1812{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001813 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001814
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001815 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001816 ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
1817 mp->m_dirblksize - (uint)sizeof(*hdr));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001818 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001819}
1820#endif
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /*
1823 * Get rid of the data block.
1824 */
1825 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1826 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001827 xfs_trans_brelse(tp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return error;
1829 }
1830 /*
1831 * Eliminate the last bests entry from the table.
1832 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001833 bestsp = xfs_dir2_leaf_bests_p(ltp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001834 be32_add_cpu(&ltp->bestcount, -1);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001835 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 xfs_dir2_leaf_log_tail(tp, lbp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001837 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return 0;
1839}
1840
Christoph Hellwig22823962011-07-08 14:35:53 +02001841static inline size_t
1842xfs_dir2_leaf_size(
1843 struct xfs_dir2_leaf_hdr *hdr,
1844 int counts)
1845{
1846 int entries;
1847
1848 entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
1849 return sizeof(xfs_dir2_leaf_hdr_t) +
1850 entries * sizeof(xfs_dir2_leaf_entry_t) +
1851 counts * sizeof(xfs_dir2_data_off_t) +
1852 sizeof(xfs_dir2_leaf_tail_t);
1853}
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855/*
1856 * Convert node form directory to leaf form directory.
1857 * The root of the node form dir needs to already be a LEAFN block.
1858 * Just return if we can't do anything.
1859 */
1860int /* error */
1861xfs_dir2_node_to_leaf(
1862 xfs_da_state_t *state) /* directory operation state */
1863{
1864 xfs_da_args_t *args; /* operation arguments */
1865 xfs_inode_t *dp; /* incore directory inode */
1866 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001867 struct xfs_buf *fbp; /* buffer for freespace block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 xfs_fileoff_t fo; /* freespace file offset */
1869 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001870 struct xfs_buf *lbp; /* buffer for leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1872 xfs_dir2_leaf_t *leaf; /* leaf structure */
1873 xfs_mount_t *mp; /* filesystem mount point */
1874 int rval; /* successful free trim? */
1875 xfs_trans_t *tp; /* transaction pointer */
1876
1877 /*
1878 * There's more than a leaf level in the btree, so there must
1879 * be multiple leafn blocks. Give up.
1880 */
1881 if (state->path.active > 1)
1882 return 0;
1883 args = state->args;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001884
1885 trace_xfs_dir2_node_to_leaf(args);
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 mp = state->mp;
1888 dp = args->dp;
1889 tp = args->trans;
1890 /*
1891 * Get the last offset in the file.
1892 */
1893 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1894 return error;
1895 }
1896 fo -= mp->m_dirblkfsbs;
1897 /*
1898 * If there are freespace blocks other than the first one,
1899 * take this opportunity to remove trailing empty freespace blocks
1900 * that may have been left behind during no-space-reservation
1901 * operations.
1902 */
1903 while (fo > mp->m_dirfreeblk) {
1904 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1905 return error;
1906 }
1907 if (rval)
1908 fo -= mp->m_dirblkfsbs;
1909 else
1910 return 0;
1911 }
1912 /*
1913 * Now find the block just before the freespace block.
1914 */
1915 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1916 return error;
1917 }
1918 /*
1919 * If it's not the single leaf block, give up.
1920 */
1921 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1922 return 0;
1923 lbp = state->path.blk[0].bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001924 leaf = lbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001925 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 /*
1927 * Read the freespace block.
1928 */
Dave Chinner20252072012-11-12 22:54:13 +11001929 error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001930 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001932 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001933 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 ASSERT(!free->hdr.firstdb);
Christoph Hellwig22823962011-07-08 14:35:53 +02001935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 /*
1937 * Now see if the leafn and free data will fit in a leaf1.
1938 * If not, release the buffer and give up.
1939 */
Christoph Hellwig22823962011-07-08 14:35:53 +02001940 if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
1941 mp->m_dirblksize) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001942 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return 0;
1944 }
Christoph Hellwig22823962011-07-08 14:35:53 +02001945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 /*
1947 * If the leaf has any stale entries in it, compress them out.
1948 * The compact routine will log the header.
1949 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001950 if (be16_to_cpu(leaf->hdr.stale))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 xfs_dir2_leaf_compact(args, lbp);
1952 else
1953 xfs_dir2_leaf_log_header(tp, lbp);
Nathan Scott89da0542006-03-17 17:28:40 +11001954 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 /*
1956 * Set up the leaf tail from the freespace block.
1957 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001958 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scott0ba962e2006-03-17 17:27:07 +11001959 ltp->bestcount = free->hdr.nvalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /*
1961 * Set up the leaf bests table.
1962 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001963 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
Christoph Hellwig22823962011-07-08 14:35:53 +02001964 be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001965 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 xfs_dir2_leaf_log_tail(tp, lbp);
1967 xfs_dir2_leaf_check(dp, lbp);
1968 /*
1969 * Get rid of the freespace block.
1970 */
1971 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1972 if (error) {
1973 /*
1974 * This can't fail here because it can only happen when
1975 * punching out the middle of an extent, and this is an
1976 * isolated block.
1977 */
1978 ASSERT(error != ENOSPC);
1979 return error;
1980 }
1981 fbp = NULL;
1982 /*
1983 * Now see if we can convert the single-leaf directory
1984 * down to a block form directory.
1985 * This routine always kills the dabuf for the leaf, so
1986 * eliminate it from the path.
1987 */
1988 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1989 state->path.blk[0].bp = NULL;
1990 return error;
1991}