blob: 09aea0247d9655c1cd7e9a7c77d1547968222813 [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.
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11003 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100025#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_inode_item.h"
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110032#include "xfs_buf_item.h"
Dave Chinner8d2a5e62012-03-07 04:50:19 +000033#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020034#include "xfs_dir2_format.h"
35#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000037#include "xfs_trace.h"
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110038#include "xfs_cksum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * Local function prototypes.
42 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100043static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp,
44 int first, int last);
45static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp);
46static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int *entno);
48static int xfs_dir2_block_sort(const void *a, const void *b);
49
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100050static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
51
52/*
53 * One-time startup routine called from xfs_init().
54 */
55void
56xfs_dir_startup(void)
57{
Dave Chinner4a24cb72010-01-20 10:48:05 +110058 xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1);
59 xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100060}
61
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110062static bool
63xfs_dir3_block_verify(
Dave Chinner82025d72012-11-12 22:54:12 +110064 struct xfs_buf *bp)
65{
66 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110067 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Dave Chinner82025d72012-11-12 22:54:12 +110068
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110069 if (xfs_sb_version_hascrc(&mp->m_sb)) {
70 if (hdr3->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC))
71 return false;
72 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
73 return false;
74 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
75 return false;
76 } else {
77 if (hdr3->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
78 return false;
79 }
Dave Chinner33363fe2013-04-03 16:11:22 +110080 if (__xfs_dir3_data_check(NULL, bp))
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110081 return false;
82 return true;
83}
Dave Chinner82025d72012-11-12 22:54:12 +110084
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110085static void
86xfs_dir3_block_read_verify(
87 struct xfs_buf *bp)
88{
89 struct xfs_mount *mp = bp->b_target->bt_mount;
90
91 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
92 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
93 XFS_DIR3_DATA_CRC_OFF)) ||
94 !xfs_dir3_block_verify(bp)) {
95 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
Dave Chinner82025d72012-11-12 22:54:12 +110096 xfs_buf_ioerror(bp, EFSCORRUPTED);
97 }
Dave Chinner612cfbf2012-11-14 17:52:32 +110098}
Dave Chinner82025d72012-11-12 22:54:12 +110099
Dave Chinner612cfbf2012-11-14 17:52:32 +1100100static void
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100101xfs_dir3_block_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100102 struct xfs_buf *bp)
103{
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100104 struct xfs_mount *mp = bp->b_target->bt_mount;
105 struct xfs_buf_log_item *bip = bp->b_fspriv;
106 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
107
108 if (!xfs_dir3_block_verify(bp)) {
109 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
110 xfs_buf_ioerror(bp, EFSCORRUPTED);
111 return;
112 }
113
114 if (!xfs_sb_version_hascrc(&mp->m_sb))
115 return;
116
117 if (bip)
118 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
119
120 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100121}
122
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100123const struct xfs_buf_ops xfs_dir3_block_buf_ops = {
124 .verify_read = xfs_dir3_block_read_verify,
125 .verify_write = xfs_dir3_block_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100126};
Dave Chinner82025d72012-11-12 22:54:12 +1100127
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100128static int
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100129xfs_dir3_block_read(
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100130 struct xfs_trans *tp,
131 struct xfs_inode *dp,
132 struct xfs_buf **bpp)
133{
134 struct xfs_mount *mp = dp->i_mount;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100135 int err;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100136
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100137 err = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, bpp,
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100138 XFS_DATA_FORK, &xfs_dir3_block_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100139 if (!err && tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100140 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_BLOCK_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100141 return err;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100142}
143
144static void
145xfs_dir3_block_init(
146 struct xfs_mount *mp,
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100147 struct xfs_trans *tp,
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100148 struct xfs_buf *bp,
149 struct xfs_inode *dp)
150{
151 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
152
153 bp->b_ops = &xfs_dir3_block_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100154 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_BLOCK_BUF);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100155
156 if (xfs_sb_version_hascrc(&mp->m_sb)) {
157 memset(hdr3, 0, sizeof(*hdr3));
158 hdr3->magic = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
159 hdr3->blkno = cpu_to_be64(bp->b_bn);
160 hdr3->owner = cpu_to_be64(dp->i_ino);
161 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
162 return;
163
164 }
165 hdr3->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100166}
167
168static void
169xfs_dir2_block_need_space(
170 struct xfs_dir2_data_hdr *hdr,
171 struct xfs_dir2_block_tail *btp,
172 struct xfs_dir2_leaf_entry *blp,
173 __be16 **tagpp,
174 struct xfs_dir2_data_unused **dupp,
175 struct xfs_dir2_data_unused **enddupp,
176 int *compact,
177 int len)
178{
179 struct xfs_dir2_data_free *bf;
180 __be16 *tagp = NULL;
181 struct xfs_dir2_data_unused *dup = NULL;
182 struct xfs_dir2_data_unused *enddup = NULL;
183
184 *compact = 0;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100185 bf = xfs_dir3_data_bestfree_p(hdr);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100186
187 /*
188 * If there are stale entries we'll use one for the leaf.
189 */
190 if (btp->stale) {
191 if (be16_to_cpu(bf[0].length) >= len) {
192 /*
193 * The biggest entry enough to avoid compaction.
194 */
195 dup = (xfs_dir2_data_unused_t *)
196 ((char *)hdr + be16_to_cpu(bf[0].offset));
197 goto out;
198 }
199
200 /*
201 * Will need to compact to make this work.
202 * Tag just before the first leaf entry.
203 */
204 *compact = 1;
205 tagp = (__be16 *)blp - 1;
206
207 /* Data object just before the first leaf entry. */
208 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
209
210 /*
211 * If it's not free then the data will go where the
212 * leaf data starts now, if it works at all.
213 */
214 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
215 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
216 (uint)sizeof(*blp) < len)
217 dup = NULL;
218 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
219 dup = NULL;
220 else
221 dup = (xfs_dir2_data_unused_t *)blp;
222 goto out;
223 }
224
225 /*
226 * no stale entries, so just use free space.
227 * Tag just before the first leaf entry.
228 */
229 tagp = (__be16 *)blp - 1;
230
231 /* Data object just before the first leaf entry. */
232 enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
233
234 /*
235 * If it's not free then can't do this add without cleaning up:
236 * the space before the first leaf entry needs to be free so it
237 * can be expanded to hold the pointer to the new entry.
238 */
239 if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
240 /*
241 * Check out the biggest freespace and see if it's the same one.
242 */
243 dup = (xfs_dir2_data_unused_t *)
244 ((char *)hdr + be16_to_cpu(bf[0].offset));
245 if (dup != enddup) {
246 /*
247 * Not the same free entry, just check its length.
248 */
249 if (be16_to_cpu(dup->length) < len)
250 dup = NULL;
251 goto out;
252 }
253
254 /*
255 * It is the biggest freespace, can it hold the leaf too?
256 */
257 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
258 /*
259 * Yes, use the second-largest entry instead if it works.
260 */
261 if (be16_to_cpu(bf[1].length) >= len)
262 dup = (xfs_dir2_data_unused_t *)
263 ((char *)hdr + be16_to_cpu(bf[1].offset));
264 else
265 dup = NULL;
266 }
267 }
268out:
269 *tagpp = tagp;
270 *dupp = dup;
271 *enddupp = enddup;
272}
273
274/*
275 * compact the leaf entries.
276 * Leave the highest-numbered stale entry stale.
277 * XXX should be the one closest to mid but mid is not yet computed.
278 */
279static void
280xfs_dir2_block_compact(
281 struct xfs_trans *tp,
282 struct xfs_buf *bp,
283 struct xfs_dir2_data_hdr *hdr,
284 struct xfs_dir2_block_tail *btp,
285 struct xfs_dir2_leaf_entry *blp,
286 int *needlog,
287 int *lfloghigh,
288 int *lfloglow)
289{
290 int fromidx; /* source leaf index */
291 int toidx; /* target leaf index */
292 int needscan = 0;
293 int highstale; /* high stale index */
294
295 fromidx = toidx = be32_to_cpu(btp->count) - 1;
296 highstale = *lfloghigh = -1;
297 for (; fromidx >= 0; fromidx--) {
298 if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
299 if (highstale == -1)
300 highstale = toidx;
301 else {
302 if (*lfloghigh == -1)
303 *lfloghigh = toidx;
304 continue;
305 }
306 }
307 if (fromidx < toidx)
308 blp[toidx] = blp[fromidx];
309 toidx--;
310 }
311 *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
312 *lfloghigh -= be32_to_cpu(btp->stale) - 1;
313 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
314 xfs_dir2_data_make_free(tp, bp,
315 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
316 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
317 needlog, &needscan);
318 blp += be32_to_cpu(btp->stale) - 1;
319 btp->stale = cpu_to_be32(1);
320 /*
321 * If we now need to rebuild the bestfree map, do so.
322 * This needs to happen before the next call to use_free.
323 */
324 if (needscan)
325 xfs_dir2_data_freescan(tp->t_mountp, hdr, needlog);
326}
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/*
329 * Add an entry to a block directory.
330 */
331int /* error */
332xfs_dir2_block_addname(
333 xfs_da_args_t *args) /* directory op arguments */
334{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200335 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000337 struct xfs_buf *bp; /* buffer for block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 xfs_dir2_block_tail_t *btp; /* block tail */
339 int compact; /* need to compact leaf ents */
340 xfs_dir2_data_entry_t *dep; /* block data entry */
341 xfs_inode_t *dp; /* directory inode */
342 xfs_dir2_data_unused_t *dup; /* block unused entry */
343 int error; /* error return value */
344 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
345 xfs_dahash_t hash; /* hash value of found entry */
346 int high; /* high index for binary srch */
347 int highstale; /* high stale index */
348 int lfloghigh=0; /* last final leaf to log */
349 int lfloglow=0; /* first final leaf to log */
350 int len; /* length of the new entry */
351 int low; /* low index for binary srch */
352 int lowstale; /* low stale index */
353 int mid=0; /* midpoint for binary srch */
354 xfs_mount_t *mp; /* filesystem mount point */
355 int needlog; /* need to log header */
356 int needscan; /* need to rescan freespace */
Nathan Scott3d693c62006-03-17 17:28:27 +1100357 __be16 *tagp; /* pointer to tag value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 xfs_trans_t *tp; /* transaction structure */
359
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000360 trace_xfs_dir2_block_addname(args);
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 dp = args->dp;
363 tp = args->trans;
364 mp = dp->i_mount;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100365
366 /* Read the (one and only) directory block into bp. */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100367 error = xfs_dir3_block_read(tp, dp, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100368 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return error;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100370
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000371 len = xfs_dir2_data_entsize(args->namelen);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /*
374 * Set up pointers to parts of the block.
375 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100376 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200377 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000378 blp = xfs_dir2_block_leaf_p(btp);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /*
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100381 * Find out if we can reuse stale entries or whether we need extra
382 * space for entry and new leaf.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100384 xfs_dir2_block_need_space(hdr, btp, blp, &tagp, &dup,
385 &enddup, &compact, len);
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /*
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100388 * Done everything we need for a space check now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100390 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000391 xfs_trans_brelse(tp, bp);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100392 if (!dup)
393 return XFS_ERROR(ENOSPC);
394 return 0;
395 }
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
398 * If we don't have space for the new entry & leaf ...
399 */
400 if (!dup) {
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100401 /* Don't have a space reservation: return no-space. */
402 if (args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return XFS_ERROR(ENOSPC);
404 /*
405 * Convert to the next larger format.
406 * Then add the new entry in that format.
407 */
408 error = xfs_dir2_block_to_leaf(args, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (error)
410 return error;
411 return xfs_dir2_leaf_addname(args);
412 }
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 needlog = needscan = 0;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /*
417 * If need to compact the leaf entries, do it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
Eric Sandeen37f13562013-01-10 10:41:48 -0600419 if (compact) {
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100420 xfs_dir2_block_compact(tp, bp, hdr, btp, blp, &needlog,
421 &lfloghigh, &lfloglow);
Eric Sandeen37f13562013-01-10 10:41:48 -0600422 /* recalculate blp post-compaction */
423 blp = xfs_dir2_block_leaf_p(btp);
424 } else if (btp->stale) {
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100425 /*
426 * Set leaf logging boundaries to impossible state.
427 * For the no-stale case they're set explicitly.
428 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100429 lfloglow = be32_to_cpu(btp->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 lfloghigh = -1;
431 }
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /*
434 * Find the slot that's first lower than our hash value, -1 if none.
435 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100436 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100438 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 break;
440 if (hash < args->hashval)
441 low = mid + 1;
442 else
443 high = mid - 1;
444 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100445 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 mid--;
447 }
448 /*
449 * No stale entries, will use enddup space to hold new leaf.
450 */
451 if (!btp->stale) {
452 /*
453 * Mark the space needed for the new leaf entry, now in use.
454 */
455 xfs_dir2_data_use_free(tp, bp, enddup,
456 (xfs_dir2_data_aoff_t)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200457 ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 sizeof(*blp)),
459 (xfs_dir2_data_aoff_t)sizeof(*blp),
460 &needlog, &needscan);
461 /*
462 * Update the tail (entry count).
463 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800464 be32_add_cpu(&btp->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /*
466 * If we now need to rebuild the bestfree map, do so.
467 * This needs to happen before the next call to use_free.
468 */
469 if (needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200470 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 needscan = 0;
472 }
473 /*
474 * Adjust pointer to the first leaf entry, we're about to move
475 * the table up one to open up space for the new leaf entry.
476 * Then adjust our index to match.
477 */
478 blp--;
479 mid++;
480 if (mid)
481 memmove(blp, &blp[1], mid * sizeof(*blp));
482 lfloglow = 0;
483 lfloghigh = mid;
484 }
485 /*
486 * Use a stale leaf for our new entry.
487 */
488 else {
489 for (lowstale = mid;
490 lowstale >= 0 &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200491 blp[lowstale].address !=
492 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 lowstale--)
494 continue;
495 for (highstale = mid + 1;
Nathan Scotte922fff2006-03-17 17:27:56 +1100496 highstale < be32_to_cpu(btp->count) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200497 blp[highstale].address !=
498 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 (lowstale < 0 || mid - lowstale > highstale - mid);
500 highstale++)
501 continue;
502 /*
503 * Move entries toward the low-numbered stale entry.
504 */
505 if (lowstale >= 0 &&
Nathan Scotte922fff2006-03-17 17:27:56 +1100506 (highstale == be32_to_cpu(btp->count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 mid - lowstale <= highstale - mid)) {
508 if (mid - lowstale)
509 memmove(&blp[lowstale], &blp[lowstale + 1],
510 (mid - lowstale) * sizeof(*blp));
511 lfloglow = MIN(lowstale, lfloglow);
512 lfloghigh = MAX(mid, lfloghigh);
513 }
514 /*
515 * Move entries toward the high-numbered stale entry.
516 */
517 else {
Nathan Scotte922fff2006-03-17 17:27:56 +1100518 ASSERT(highstale < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 mid++;
520 if (highstale - mid)
521 memmove(&blp[mid + 1], &blp[mid],
522 (highstale - mid) * sizeof(*blp));
523 lfloglow = MIN(mid, lfloglow);
524 lfloghigh = MAX(highstale, lfloghigh);
525 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800526 be32_add_cpu(&btp->stale, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528 /*
529 * Point to the new data entry.
530 */
531 dep = (xfs_dir2_data_entry_t *)dup;
532 /*
533 * Fill in the leaf entry.
534 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100535 blp[mid].hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000536 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200537 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
539 /*
540 * Mark space for the data entry used.
541 */
542 xfs_dir2_data_use_free(tp, bp, dup,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200543 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
545 /*
546 * Create the new data entry.
547 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000548 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 dep->namelen = args->namelen;
550 memcpy(dep->name, args->name, args->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000551 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200552 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /*
554 * Clean up the bestfree array and log the header, tail, and entry.
555 */
556 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200557 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (needlog)
559 xfs_dir2_data_log_header(tp, bp);
560 xfs_dir2_block_log_tail(tp, bp);
561 xfs_dir2_data_log_entry(tp, bp, dep);
Dave Chinner33363fe2013-04-03 16:11:22 +1100562 xfs_dir3_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return 0;
564}
565
566/*
567 * Readdir for block directories.
568 */
569int /* error */
570xfs_dir2_block_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 xfs_inode_t *dp, /* incore inode */
Al Virob8227552013-05-22 17:07:56 -0400572 struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200574 xfs_dir2_data_hdr_t *hdr; /* block header */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000575 struct xfs_buf *bp; /* buffer for block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 xfs_dir2_block_tail_t *btp; /* block tail */
577 xfs_dir2_data_entry_t *dep; /* block data entry */
578 xfs_dir2_data_unused_t *dup; /* block unused entry */
579 char *endptr; /* end of the data entries */
580 int error; /* error return value */
581 xfs_mount_t *mp; /* filesystem mount point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 char *ptr; /* current data entry */
583 int wantoff; /* starting block offset */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000584 xfs_off_t cook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 mp = dp->i_mount;
587 /*
588 * If the block number in the offset is out of range, we're done.
589 */
Al Virob8227552013-05-22 17:07:56 -0400590 if (xfs_dir2_dataptr_to_db(mp, ctx->pos) > mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return 0;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100592
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100593 error = xfs_dir3_block_read(NULL, dp, &bp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000594 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return error;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /*
598 * Extract the byte offset we start at from the seek pointer.
599 * We'll skip entries before this.
600 */
Al Virob8227552013-05-22 17:07:56 -0400601 wantoff = xfs_dir2_dataptr_to_off(mp, ctx->pos);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000602 hdr = bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100603 xfs_dir3_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /*
605 * Set up values for the loop.
606 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200607 btp = xfs_dir2_block_tail_p(mp, hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100608 ptr = (char *)xfs_dir3_data_entry_p(hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000609 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /*
612 * Loop over the data portion of the block.
613 * Each object is a real entry (dep) or an unused one (dup).
614 */
615 while (ptr < endptr) {
616 dup = (xfs_dir2_data_unused_t *)ptr;
617 /*
618 * Unused, skip it.
619 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100620 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
621 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 continue;
623 }
624
625 dep = (xfs_dir2_data_entry_t *)ptr;
626
627 /*
628 * Bump pointer for the next iteration.
629 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000630 ptr += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /*
632 * The entry is before the desired starting point, skip it.
633 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200634 if ((char *)dep - (char *)hdr < wantoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000637 cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200638 (char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Al Virob8227552013-05-22 17:07:56 -0400640 ctx->pos = cook & 0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /*
642 * If it didn't fit, set the final offset to here & return.
643 */
Al Virob8227552013-05-22 17:07:56 -0400644 if (!dir_emit(ctx, (char *)dep->name, dep->namelen,
645 be64_to_cpu(dep->inumber), DT_UNKNOWN)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000646 xfs_trans_brelse(NULL, bp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649 }
650
651 /*
652 * Reached the end of the block.
Nathan Scottc41564b2006-03-29 08:55:14 +1000653 * Set the offset to a non-existent block 1 and return.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 */
Al Virob8227552013-05-22 17:07:56 -0400655 ctx->pos = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
Christoph Hellwig15440312009-01-08 14:00:00 -0500656 0x7fffffff;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000657 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return 0;
659}
660
661/*
662 * Log leaf entries from the block.
663 */
664static void
665xfs_dir2_block_log_leaf(
666 xfs_trans_t *tp, /* transaction structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000667 struct xfs_buf *bp, /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int first, /* index of first logged leaf */
669 int last) /* index of last logged leaf */
670{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000671 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200672 xfs_dir2_leaf_entry_t *blp;
673 xfs_dir2_block_tail_t *btp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200675 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000676 blp = xfs_dir2_block_leaf_p(btp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000677 xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200678 (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681/*
682 * Log the block tail.
683 */
684static void
685xfs_dir2_block_log_tail(
686 xfs_trans_t *tp, /* transaction structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000687 struct xfs_buf *bp) /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000689 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200690 xfs_dir2_block_tail_t *btp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200692 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000693 xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200694 (uint)((char *)(btp + 1) - (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697/*
698 * Look up an entry in the block. This is the external routine,
699 * xfs_dir2_block_lookup_int does the real work.
700 */
701int /* error */
702xfs_dir2_block_lookup(
703 xfs_da_args_t *args) /* dir lookup arguments */
704{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200705 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000707 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 xfs_dir2_block_tail_t *btp; /* block tail */
709 xfs_dir2_data_entry_t *dep; /* block data entry */
710 xfs_inode_t *dp; /* incore inode */
711 int ent; /* entry index */
712 int error; /* error return value */
713 xfs_mount_t *mp; /* filesystem mount point */
714
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000715 trace_xfs_dir2_block_lookup(args);
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /*
718 * Get the buffer, look up the entry.
719 * If not found (ENOENT) then return, have no buffer.
720 */
721 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
722 return error;
723 dp = args->dp;
724 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000725 hdr = bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100726 xfs_dir3_data_check(dp, bp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200727 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000728 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /*
730 * Get the offset from the leaf entry, to point to the data.
731 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200732 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
Barry Naujok384f3ce2008-05-21 16:58:22 +1000733 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +1000735 * Fill in inode number, CI name if appropriate, release the block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000737 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000738 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000739 xfs_trans_brelse(args->trans, bp);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000740 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743/*
744 * Internal block lookup routine.
745 */
746static int /* error */
747xfs_dir2_block_lookup_int(
748 xfs_da_args_t *args, /* dir lookup arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000749 struct xfs_buf **bpp, /* returned block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 int *entno) /* returned entry number */
751{
752 xfs_dir2_dataptr_t addr; /* data entry address */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200753 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000755 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 xfs_dir2_block_tail_t *btp; /* block tail */
757 xfs_dir2_data_entry_t *dep; /* block data entry */
758 xfs_inode_t *dp; /* incore inode */
759 int error; /* error return value */
760 xfs_dahash_t hash; /* found hash value */
761 int high; /* binary search high index */
762 int low; /* binary search low index */
763 int mid; /* binary search current idx */
764 xfs_mount_t *mp; /* filesystem mount point */
765 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000766 enum xfs_dacmp cmp; /* comparison result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 dp = args->dp;
769 tp = args->trans;
770 mp = dp->i_mount;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100771
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100772 error = xfs_dir3_block_read(tp, dp, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100773 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return error;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100775
Dave Chinner1d9025e2012-06-22 18:50:14 +1000776 hdr = bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100777 xfs_dir3_data_check(dp, bp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200778 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000779 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /*
781 * Loop doing a binary search for our hash value.
782 * Find our entry, ENOENT if it's not there.
783 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100784 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 ASSERT(low <= high);
786 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100787 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 break;
789 if (hash < args->hashval)
790 low = mid + 1;
791 else
792 high = mid - 1;
793 if (low > high) {
Barry Naujok6a178102008-05-21 16:42:05 +1000794 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000795 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return XFS_ERROR(ENOENT);
797 }
798 }
799 /*
800 * Back up to the first one with the right hash value.
801 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100802 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 mid--;
804 }
805 /*
806 * Now loop forward through all the entries with the
807 * right hash value looking for our name.
808 */
809 do {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100810 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 continue;
812 /*
813 * Get pointer to the entry from the leaf.
814 */
815 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200816 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000818 * Compare name and if it's an exact match, return the index
819 * and buffer. If it's the first case-insensitive match, store
820 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 */
Barry Naujok5163f952008-05-21 16:41:01 +1000822 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
823 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
824 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 *bpp = bp;
826 *entno = mid;
Barry Naujok5163f952008-05-21 16:41:01 +1000827 if (cmp == XFS_CMP_EXACT)
828 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Barry Naujok5163f952008-05-21 16:41:01 +1000830 } while (++mid < be32_to_cpu(btp->count) &&
831 be32_to_cpu(blp[mid].hashval) == hash);
832
Barry Naujok6a178102008-05-21 16:42:05 +1000833 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000834 /*
835 * Here, we can only be doing a lookup (not a rename or replace).
836 * If a case-insensitive match was found earlier, return success.
837 */
838 if (args->cmpresult == XFS_CMP_CASE)
839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /*
841 * No match, release the buffer and return ENOENT.
842 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000843 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return XFS_ERROR(ENOENT);
845}
846
847/*
848 * Remove an entry from a block format directory.
849 * If that makes the block small enough to fit in shortform, transform it.
850 */
851int /* error */
852xfs_dir2_block_removename(
853 xfs_da_args_t *args) /* directory operation args */
854{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200855 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000857 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 xfs_dir2_block_tail_t *btp; /* block tail */
859 xfs_dir2_data_entry_t *dep; /* block data entry */
860 xfs_inode_t *dp; /* incore inode */
861 int ent; /* block leaf entry index */
862 int error; /* error return value */
863 xfs_mount_t *mp; /* filesystem mount point */
864 int needlog; /* need to log block header */
865 int needscan; /* need to fixup bestfree */
866 xfs_dir2_sf_hdr_t sfh; /* shortform header */
867 int size; /* shortform size */
868 xfs_trans_t *tp; /* transaction pointer */
869
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000870 trace_xfs_dir2_block_removename(args);
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /*
873 * Look up the entry in the block. Gets the buffer and entry index.
874 * It will always be there, the vnodeops level does a lookup first.
875 */
876 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
877 return error;
878 }
879 dp = args->dp;
880 tp = args->trans;
881 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000882 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200883 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000884 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 /*
886 * Point to the data entry using the leaf entry.
887 */
888 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200889 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /*
891 * Mark the data entry's space free.
892 */
893 needlog = needscan = 0;
894 xfs_dir2_data_make_free(tp, bp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200895 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000896 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /*
898 * Fix up the block tail.
899 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800900 be32_add_cpu(&btp->stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 xfs_dir2_block_log_tail(tp, bp);
902 /*
903 * Remove the leaf entry by marking it stale.
904 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100905 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
907 /*
908 * Fix up bestfree, log the header if necessary.
909 */
910 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200911 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (needlog)
913 xfs_dir2_data_log_header(tp, bp);
Dave Chinner33363fe2013-04-03 16:11:22 +1100914 xfs_dir3_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /*
916 * See if the size as a shortform is good enough.
917 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200918 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000919 if (size > XFS_IFORK_DSIZE(dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 /*
923 * If it works, do the conversion.
924 */
925 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
926}
927
928/*
929 * Replace an entry in a V2 block directory.
930 * Change the inode number to the new value.
931 */
932int /* error */
933xfs_dir2_block_replace(
934 xfs_da_args_t *args) /* directory operation args */
935{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200936 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000938 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 xfs_dir2_block_tail_t *btp; /* block tail */
940 xfs_dir2_data_entry_t *dep; /* block data entry */
941 xfs_inode_t *dp; /* incore inode */
942 int ent; /* leaf entry index */
943 int error; /* error return value */
944 xfs_mount_t *mp; /* filesystem mount point */
945
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000946 trace_xfs_dir2_block_replace(args);
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 /*
949 * Lookup the entry in the directory. Get buffer and entry index.
950 * This will always succeed since the caller has already done a lookup.
951 */
952 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
953 return error;
954 }
955 dp = args->dp;
956 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000957 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200958 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000959 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 /*
961 * Point to the data entry we need to change.
962 */
963 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200964 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000965 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /*
967 * Change the inode number to the new value.
968 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000969 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 xfs_dir2_data_log_entry(args->trans, bp, dep);
Dave Chinner33363fe2013-04-03 16:11:22 +1100971 xfs_dir3_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return 0;
973}
974
975/*
976 * Qsort comparison routine for the block leaf entries.
977 */
978static int /* sort order */
979xfs_dir2_block_sort(
980 const void *a, /* first leaf entry */
981 const void *b) /* second leaf entry */
982{
983 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
984 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
985
986 la = a;
987 lb = b;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100988 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
989 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992/*
993 * Convert a V2 leaf directory to a V2 block directory if possible.
994 */
995int /* error */
996xfs_dir2_leaf_to_block(
997 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000998 struct xfs_buf *lbp, /* leaf buffer */
999 struct xfs_buf *dbp) /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Nathan Scott68b3a102006-03-17 17:27:19 +11001001 __be16 *bestsp; /* leaf bests table */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001002 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 xfs_dir2_block_tail_t *btp; /* block tail */
1004 xfs_inode_t *dp; /* incore directory inode */
1005 xfs_dir2_data_unused_t *dup; /* unused data entry */
1006 int error; /* error return value */
1007 int from; /* leaf from index */
1008 xfs_dir2_leaf_t *leaf; /* leaf structure */
1009 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1010 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1011 xfs_mount_t *mp; /* file system mount point */
1012 int needlog; /* need to log data header */
1013 int needscan; /* need to scan for bestfree */
1014 xfs_dir2_sf_hdr_t sfh; /* shortform header */
1015 int size; /* bytes used */
Nathan Scott3d693c62006-03-17 17:28:27 +11001016 __be16 *tagp; /* end of entry (tag) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 int to; /* block/leaf to index */
1018 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10001019 struct xfs_dir2_leaf_entry *ents;
1020 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001022 trace_xfs_dir2_leaf_to_block(args);
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 dp = args->dp;
1025 tp = args->trans;
1026 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001027 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001028 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1029 ents = xfs_dir3_leaf_ents_p(leaf);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001030 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001031
1032 ASSERT(leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
1033 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /*
1035 * If there are data blocks other than the first one, take this
1036 * opportunity to remove trailing empty data blocks that may have
1037 * been left behind during no-space-reservation operations.
1038 * These will show up in the leaf bests table.
1039 */
1040 while (dp->i_d.di_size > mp->m_dirblksize) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001041 int hdrsz;
1042
1043 hdrsz = xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&mp->m_sb));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001044 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001045 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001046 mp->m_dirblksize - hdrsz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if ((error =
1048 xfs_dir2_leaf_trim_data(args, lbp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001049 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
Dave Chinner1d9025e2012-06-22 18:50:14 +10001050 return error;
1051 } else
1052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054 /*
1055 * Read the data block if we don't already have it, give up if it fails.
1056 */
Dave Chinner4bb20a82012-11-12 22:54:10 +11001057 if (!dbp) {
Dave Chinner33363fe2013-04-03 16:11:22 +11001058 error = xfs_dir3_data_read(tp, dp, mp->m_dirdatablk, -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001059 if (error)
1060 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001062 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001063 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1064 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /*
1067 * Size of the "leaf" area in the block.
1068 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001069 size = (uint)sizeof(xfs_dir2_block_tail_t) +
Dave Chinner24df33b2013-04-12 07:30:21 +10001070 (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /*
1072 * Look at the last data entry.
1073 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001074 tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1;
1075 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 /*
1077 * If it's not free or is too short we can't do it.
1078 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001079 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
Dave Chinner1d9025e2012-06-22 18:50:14 +10001080 be16_to_cpu(dup->length) < size)
1081 return 0;
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /*
1084 * Start converting it to block form.
1085 */
Dave Chinnerd75afeb2013-04-03 16:11:29 +11001086 xfs_dir3_block_init(mp, tp, dbp, dp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 needlog = 1;
1089 needscan = 0;
1090 /*
1091 * Use up the space at the end of the block (blp/btp).
1092 */
1093 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
1094 &needlog, &needscan);
1095 /*
1096 * Initialize the block tail.
1097 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001098 btp = xfs_dir2_block_tail_p(mp, hdr);
Dave Chinner24df33b2013-04-12 07:30:21 +10001099 btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 btp->stale = 0;
1101 xfs_dir2_block_log_tail(tp, dbp);
1102 /*
1103 * Initialize the block leaf area. We compact out stale entries.
1104 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001105 lep = xfs_dir2_block_leaf_p(btp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001106 for (from = to = 0; from < leafhdr.count; from++) {
1107 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 continue;
Dave Chinner24df33b2013-04-12 07:30:21 +10001109 lep[to++] = ents[from];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
Nathan Scotte922fff2006-03-17 17:27:56 +11001111 ASSERT(to == be32_to_cpu(btp->count));
1112 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 /*
1114 * Scan the bestfree if we need it and log the data block header.
1115 */
1116 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001117 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (needlog)
1119 xfs_dir2_data_log_header(tp, dbp);
1120 /*
1121 * Pitch the old leaf block.
1122 */
1123 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001124 if (error)
1125 return error;
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 /*
1128 * Now see if the resulting block can be shrunken to shortform.
1129 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001130 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001131 if (size > XFS_IFORK_DSIZE(dp))
1132 return 0;
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137/*
1138 * Convert the shortform directory to block form.
1139 */
1140int /* error */
1141xfs_dir2_sf_to_block(
1142 xfs_da_args_t *args) /* operation arguments */
1143{
1144 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001145 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001147 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 xfs_dir2_block_tail_t *btp; /* block tail pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1150 xfs_inode_t *dp; /* incore directory inode */
1151 int dummy; /* trash */
1152 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1153 int endoffset; /* end of data objects */
1154 int error; /* error return value */
1155 int i; /* index */
1156 xfs_mount_t *mp; /* filesystem mount point */
1157 int needlog; /* need to log block header */
1158 int needscan; /* need to scan block freespc */
1159 int newoffset; /* offset from current entry */
1160 int offset; /* target block offset */
1161 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001162 xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
1163 xfs_dir2_sf_hdr_t *sfp; /* shortform header */
Nathan Scott3d693c62006-03-17 17:28:27 +11001164 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +10001166 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001168 trace_xfs_dir2_sf_to_block(args);
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 dp = args->dp;
1171 tp = args->trans;
1172 mp = dp->i_mount;
1173 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1174 /*
1175 * Bomb out if the shortform directory is way too short.
1176 */
1177 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1178 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1179 return XFS_ERROR(EIO);
1180 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001181
1182 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1185 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001186 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 /*
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001189 * Copy the directory into a temporary buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 * Then pitch the incore inode data so we can make extents.
1191 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001192 sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1193 memcpy(sfp, oldsfp, dp->i_df.if_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001195 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 dp->i_d.di_size = 0;
1197 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /*
1200 * Add block 0 to the inode.
1201 */
1202 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1203 if (error) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001204 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return error;
1206 }
1207 /*
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001208 * Initialize the data block, then convert it to block format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001210 error = xfs_dir3_data_init(args, blkno, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (error) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001212 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return error;
1214 }
Dave Chinnerd75afeb2013-04-03 16:11:29 +11001215 xfs_dir3_block_init(mp, tp, bp, dp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001216 hdr = bp->b_addr;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 /*
1219 * Compute size of block "tail" area.
1220 */
1221 i = (uint)sizeof(*btp) +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001222 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 /*
1224 * The whole thing is initialized to free by the init routine.
1225 * Say we're using the leaf and tail area.
1226 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001227 dup = xfs_dir3_data_unused_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 needlog = needscan = 0;
1229 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1230 &needscan);
1231 ASSERT(needscan == 0);
1232 /*
1233 * Fill in the tail.
1234 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001235 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001236 btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 btp->stale = 0;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001238 blp = xfs_dir2_block_leaf_p(btp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001239 endoffset = (uint)((char *)blp - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /*
1241 * Remove the freespace, we'll manage it.
1242 */
1243 xfs_dir2_data_use_free(tp, bp, dup,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001244 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
Nathan Scottad354eb2006-03-17 17:27:37 +11001245 be16_to_cpu(dup->length), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /*
1247 * Create entry for .
1248 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001249 dep = xfs_dir3_data_dot_entry_p(hdr);
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001250 dep->inumber = cpu_to_be64(dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 dep->namelen = 1;
1252 dep->name[0] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001253 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001254 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001256 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001257 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001258 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /*
1260 * Create entry for ..
1261 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001262 dep = xfs_dir3_data_dotdot_entry_p(hdr);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001263 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 dep->namelen = 2;
1265 dep->name[0] = dep->name[1] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001266 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001267 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001269 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001270 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001271 (char *)dep - (char *)hdr));
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001272 offset = xfs_dir3_data_first_offset(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /*
1274 * Loop over existing entries, stuff them in.
1275 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001276 i = 0;
1277 if (!sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 sfep = NULL;
1279 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001280 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 /*
1282 * Need to preserve the existing offset values in the sf directory.
1283 * Insert holes (unused entries) where necessary.
1284 */
1285 while (offset < endoffset) {
1286 /*
1287 * sfep is null when we reach the end of the list.
1288 */
1289 if (sfep == NULL)
1290 newoffset = endoffset;
1291 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001292 newoffset = xfs_dir2_sf_get_offset(sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 /*
1294 * There should be a hole here, make one.
1295 */
1296 if (offset < newoffset) {
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001297 dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +11001298 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1299 dup->length = cpu_to_be16(newoffset - offset);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001300 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001301 ((char *)dup - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 xfs_dir2_data_log_unused(tp, bp, dup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001303 xfs_dir2_data_freeinsert(hdr, dup, &dummy);
Nathan Scottad354eb2006-03-17 17:27:37 +11001304 offset += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 continue;
1306 }
1307 /*
1308 * Copy a real entry.
1309 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001310 dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001311 dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 dep->namelen = sfep->namelen;
1313 memcpy(dep->name, sfep->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001314 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001315 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 xfs_dir2_data_log_entry(tp, bp, dep);
Barry Naujok5163f952008-05-21 16:41:01 +10001317 name.name = sfep->name;
1318 name.len = sfep->namelen;
1319 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1320 hashname(&name));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001321 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001322 (char *)dep - (char *)hdr));
1323 offset = (int)((char *)(tagp + 1) - (char *)hdr);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001324 if (++i == sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 sfep = NULL;
1326 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001327 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329 /* Done with the temporary buffer */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001330 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /*
1332 * Sort the leaf entries by hash value.
1333 */
Nathan Scotte922fff2006-03-17 17:27:56 +11001334 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /*
1336 * Log the leaf entry area and tail.
1337 * Already logged the header in data_init, ignore needlog.
1338 */
1339 ASSERT(needscan == 0);
Nathan Scotte922fff2006-03-17 17:27:56 +11001340 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 xfs_dir2_block_log_tail(tp, bp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001342 xfs_dir3_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return 0;
1344}