blob: 25ce409487bed8c38c211395ceba4ec666d85b2d [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
22#include "xfs_trans.h"
23#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_inode_item.h"
Dave Chinner8d2a5e62012-03-07 04:50:19 +000031#include "xfs_dir2.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 prototypes.
39 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100040static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp,
41 int first, int last);
42static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp);
43static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 int *entno);
45static int xfs_dir2_block_sort(const void *a, const void *b);
46
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100047static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
48
49/*
50 * One-time startup routine called from xfs_init().
51 */
52void
53xfs_dir_startup(void)
54{
Dave Chinner4a24cb72010-01-20 10:48:05 +110055 xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1);
56 xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100057}
58
Dave Chinner20f7e9f2012-11-12 22:54:11 +110059static int
60xfs_dir2_block_read(
61 struct xfs_trans *tp,
62 struct xfs_inode *dp,
63 struct xfs_buf **bpp)
64{
65 struct xfs_mount *mp = dp->i_mount;
66
67 return xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, bpp,
68 XFS_DATA_FORK, NULL);
69}
70
71static void
72xfs_dir2_block_need_space(
73 struct xfs_dir2_data_hdr *hdr,
74 struct xfs_dir2_block_tail *btp,
75 struct xfs_dir2_leaf_entry *blp,
76 __be16 **tagpp,
77 struct xfs_dir2_data_unused **dupp,
78 struct xfs_dir2_data_unused **enddupp,
79 int *compact,
80 int len)
81{
82 struct xfs_dir2_data_free *bf;
83 __be16 *tagp = NULL;
84 struct xfs_dir2_data_unused *dup = NULL;
85 struct xfs_dir2_data_unused *enddup = NULL;
86
87 *compact = 0;
88 bf = hdr->bestfree;
89
90 /*
91 * If there are stale entries we'll use one for the leaf.
92 */
93 if (btp->stale) {
94 if (be16_to_cpu(bf[0].length) >= len) {
95 /*
96 * The biggest entry enough to avoid compaction.
97 */
98 dup = (xfs_dir2_data_unused_t *)
99 ((char *)hdr + be16_to_cpu(bf[0].offset));
100 goto out;
101 }
102
103 /*
104 * Will need to compact to make this work.
105 * Tag just before the first leaf entry.
106 */
107 *compact = 1;
108 tagp = (__be16 *)blp - 1;
109
110 /* Data object just before the first leaf entry. */
111 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
112
113 /*
114 * If it's not free then the data will go where the
115 * leaf data starts now, if it works at all.
116 */
117 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
118 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
119 (uint)sizeof(*blp) < len)
120 dup = NULL;
121 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
122 dup = NULL;
123 else
124 dup = (xfs_dir2_data_unused_t *)blp;
125 goto out;
126 }
127
128 /*
129 * no stale entries, so just use free space.
130 * Tag just before the first leaf entry.
131 */
132 tagp = (__be16 *)blp - 1;
133
134 /* Data object just before the first leaf entry. */
135 enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
136
137 /*
138 * If it's not free then can't do this add without cleaning up:
139 * the space before the first leaf entry needs to be free so it
140 * can be expanded to hold the pointer to the new entry.
141 */
142 if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
143 /*
144 * Check out the biggest freespace and see if it's the same one.
145 */
146 dup = (xfs_dir2_data_unused_t *)
147 ((char *)hdr + be16_to_cpu(bf[0].offset));
148 if (dup != enddup) {
149 /*
150 * Not the same free entry, just check its length.
151 */
152 if (be16_to_cpu(dup->length) < len)
153 dup = NULL;
154 goto out;
155 }
156
157 /*
158 * It is the biggest freespace, can it hold the leaf too?
159 */
160 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
161 /*
162 * Yes, use the second-largest entry instead if it works.
163 */
164 if (be16_to_cpu(bf[1].length) >= len)
165 dup = (xfs_dir2_data_unused_t *)
166 ((char *)hdr + be16_to_cpu(bf[1].offset));
167 else
168 dup = NULL;
169 }
170 }
171out:
172 *tagpp = tagp;
173 *dupp = dup;
174 *enddupp = enddup;
175}
176
177/*
178 * compact the leaf entries.
179 * Leave the highest-numbered stale entry stale.
180 * XXX should be the one closest to mid but mid is not yet computed.
181 */
182static void
183xfs_dir2_block_compact(
184 struct xfs_trans *tp,
185 struct xfs_buf *bp,
186 struct xfs_dir2_data_hdr *hdr,
187 struct xfs_dir2_block_tail *btp,
188 struct xfs_dir2_leaf_entry *blp,
189 int *needlog,
190 int *lfloghigh,
191 int *lfloglow)
192{
193 int fromidx; /* source leaf index */
194 int toidx; /* target leaf index */
195 int needscan = 0;
196 int highstale; /* high stale index */
197
198 fromidx = toidx = be32_to_cpu(btp->count) - 1;
199 highstale = *lfloghigh = -1;
200 for (; fromidx >= 0; fromidx--) {
201 if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
202 if (highstale == -1)
203 highstale = toidx;
204 else {
205 if (*lfloghigh == -1)
206 *lfloghigh = toidx;
207 continue;
208 }
209 }
210 if (fromidx < toidx)
211 blp[toidx] = blp[fromidx];
212 toidx--;
213 }
214 *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
215 *lfloghigh -= be32_to_cpu(btp->stale) - 1;
216 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
217 xfs_dir2_data_make_free(tp, bp,
218 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
219 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
220 needlog, &needscan);
221 blp += be32_to_cpu(btp->stale) - 1;
222 btp->stale = cpu_to_be32(1);
223 /*
224 * If we now need to rebuild the bestfree map, do so.
225 * This needs to happen before the next call to use_free.
226 */
227 if (needscan)
228 xfs_dir2_data_freescan(tp->t_mountp, hdr, needlog);
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/*
232 * Add an entry to a block directory.
233 */
234int /* error */
235xfs_dir2_block_addname(
236 xfs_da_args_t *args) /* directory op arguments */
237{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200238 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000240 struct xfs_buf *bp; /* buffer for block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 xfs_dir2_block_tail_t *btp; /* block tail */
242 int compact; /* need to compact leaf ents */
243 xfs_dir2_data_entry_t *dep; /* block data entry */
244 xfs_inode_t *dp; /* directory inode */
245 xfs_dir2_data_unused_t *dup; /* block unused entry */
246 int error; /* error return value */
247 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
248 xfs_dahash_t hash; /* hash value of found entry */
249 int high; /* high index for binary srch */
250 int highstale; /* high stale index */
251 int lfloghigh=0; /* last final leaf to log */
252 int lfloglow=0; /* first final leaf to log */
253 int len; /* length of the new entry */
254 int low; /* low index for binary srch */
255 int lowstale; /* low stale index */
256 int mid=0; /* midpoint for binary srch */
257 xfs_mount_t *mp; /* filesystem mount point */
258 int needlog; /* need to log header */
259 int needscan; /* need to rescan freespace */
Nathan Scott3d693c62006-03-17 17:28:27 +1100260 __be16 *tagp; /* pointer to tag value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 xfs_trans_t *tp; /* transaction structure */
262
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000263 trace_xfs_dir2_block_addname(args);
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 dp = args->dp;
266 tp = args->trans;
267 mp = dp->i_mount;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100268
269 /* Read the (one and only) directory block into bp. */
270 error = xfs_dir2_block_read(tp, dp, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100271 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return error;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100273
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000274 len = xfs_dir2_data_entsize(args->namelen);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /*
277 * Set up pointers to parts of the block.
278 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100279 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200280 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000281 blp = xfs_dir2_block_leaf_p(btp);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /*
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100284 * Find out if we can reuse stale entries or whether we need extra
285 * space for entry and new leaf.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100287 xfs_dir2_block_need_space(hdr, btp, blp, &tagp, &dup,
288 &enddup, &compact, len);
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /*
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100291 * Done everything we need for a space check now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100293 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000294 xfs_trans_brelse(tp, bp);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100295 if (!dup)
296 return XFS_ERROR(ENOSPC);
297 return 0;
298 }
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /*
301 * If we don't have space for the new entry & leaf ...
302 */
303 if (!dup) {
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100304 /* Don't have a space reservation: return no-space. */
305 if (args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return XFS_ERROR(ENOSPC);
307 /*
308 * Convert to the next larger format.
309 * Then add the new entry in that format.
310 */
311 error = xfs_dir2_block_to_leaf(args, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (error)
313 return error;
314 return xfs_dir2_leaf_addname(args);
315 }
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 needlog = needscan = 0;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /*
320 * If need to compact the leaf entries, do it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100322 if (compact)
323 xfs_dir2_block_compact(tp, bp, hdr, btp, blp, &needlog,
324 &lfloghigh, &lfloglow);
Nathan Scotte922fff2006-03-17 17:27:56 +1100325 else if (btp->stale) {
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100326 /*
327 * Set leaf logging boundaries to impossible state.
328 * For the no-stale case they're set explicitly.
329 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100330 lfloglow = be32_to_cpu(btp->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 lfloghigh = -1;
332 }
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /*
335 * Find the slot that's first lower than our hash value, -1 if none.
336 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100337 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100339 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 break;
341 if (hash < args->hashval)
342 low = mid + 1;
343 else
344 high = mid - 1;
345 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100346 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 mid--;
348 }
349 /*
350 * No stale entries, will use enddup space to hold new leaf.
351 */
352 if (!btp->stale) {
353 /*
354 * Mark the space needed for the new leaf entry, now in use.
355 */
356 xfs_dir2_data_use_free(tp, bp, enddup,
357 (xfs_dir2_data_aoff_t)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200358 ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 sizeof(*blp)),
360 (xfs_dir2_data_aoff_t)sizeof(*blp),
361 &needlog, &needscan);
362 /*
363 * Update the tail (entry count).
364 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800365 be32_add_cpu(&btp->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /*
367 * If we now need to rebuild the bestfree map, do so.
368 * This needs to happen before the next call to use_free.
369 */
370 if (needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200371 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 needscan = 0;
373 }
374 /*
375 * Adjust pointer to the first leaf entry, we're about to move
376 * the table up one to open up space for the new leaf entry.
377 * Then adjust our index to match.
378 */
379 blp--;
380 mid++;
381 if (mid)
382 memmove(blp, &blp[1], mid * sizeof(*blp));
383 lfloglow = 0;
384 lfloghigh = mid;
385 }
386 /*
387 * Use a stale leaf for our new entry.
388 */
389 else {
390 for (lowstale = mid;
391 lowstale >= 0 &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200392 blp[lowstale].address !=
393 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 lowstale--)
395 continue;
396 for (highstale = mid + 1;
Nathan Scotte922fff2006-03-17 17:27:56 +1100397 highstale < be32_to_cpu(btp->count) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200398 blp[highstale].address !=
399 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 (lowstale < 0 || mid - lowstale > highstale - mid);
401 highstale++)
402 continue;
403 /*
404 * Move entries toward the low-numbered stale entry.
405 */
406 if (lowstale >= 0 &&
Nathan Scotte922fff2006-03-17 17:27:56 +1100407 (highstale == be32_to_cpu(btp->count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 mid - lowstale <= highstale - mid)) {
409 if (mid - lowstale)
410 memmove(&blp[lowstale], &blp[lowstale + 1],
411 (mid - lowstale) * sizeof(*blp));
412 lfloglow = MIN(lowstale, lfloglow);
413 lfloghigh = MAX(mid, lfloghigh);
414 }
415 /*
416 * Move entries toward the high-numbered stale entry.
417 */
418 else {
Nathan Scotte922fff2006-03-17 17:27:56 +1100419 ASSERT(highstale < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 mid++;
421 if (highstale - mid)
422 memmove(&blp[mid + 1], &blp[mid],
423 (highstale - mid) * sizeof(*blp));
424 lfloglow = MIN(mid, lfloglow);
425 lfloghigh = MAX(highstale, lfloghigh);
426 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800427 be32_add_cpu(&btp->stale, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 /*
430 * Point to the new data entry.
431 */
432 dep = (xfs_dir2_data_entry_t *)dup;
433 /*
434 * Fill in the leaf entry.
435 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100436 blp[mid].hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000437 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200438 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
440 /*
441 * Mark space for the data entry used.
442 */
443 xfs_dir2_data_use_free(tp, bp, dup,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200444 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
446 /*
447 * Create the new data entry.
448 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000449 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 dep->namelen = args->namelen;
451 memcpy(dep->name, args->name, args->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000452 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200453 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /*
455 * Clean up the bestfree array and log the header, tail, and entry.
456 */
457 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200458 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (needlog)
460 xfs_dir2_data_log_header(tp, bp);
461 xfs_dir2_block_log_tail(tp, bp);
462 xfs_dir2_data_log_entry(tp, bp, dep);
463 xfs_dir2_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return 0;
465}
466
467/*
468 * Readdir for block directories.
469 */
470int /* error */
471xfs_dir2_block_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 xfs_inode_t *dp, /* incore inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000473 void *dirent,
474 xfs_off_t *offset,
475 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200477 xfs_dir2_data_hdr_t *hdr; /* block header */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000478 struct xfs_buf *bp; /* buffer for block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 xfs_dir2_block_tail_t *btp; /* block tail */
480 xfs_dir2_data_entry_t *dep; /* block data entry */
481 xfs_dir2_data_unused_t *dup; /* block unused entry */
482 char *endptr; /* end of the data entries */
483 int error; /* error return value */
484 xfs_mount_t *mp; /* filesystem mount point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 char *ptr; /* current data entry */
486 int wantoff; /* starting block offset */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000487 xfs_off_t cook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 mp = dp->i_mount;
490 /*
491 * If the block number in the offset is out of range, we're done.
492 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100493 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100495
496 error = xfs_dir2_block_read(NULL, dp, &bp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000497 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return error;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /*
501 * Extract the byte offset we start at from the seek pointer.
502 * We'll skip entries before this.
503 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000504 wantoff = xfs_dir2_dataptr_to_off(mp, *offset);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000505 hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 xfs_dir2_data_check(dp, bp);
507 /*
508 * Set up values for the loop.
509 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200510 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200511 ptr = (char *)(hdr + 1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000512 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /*
515 * Loop over the data portion of the block.
516 * Each object is a real entry (dep) or an unused one (dup).
517 */
518 while (ptr < endptr) {
519 dup = (xfs_dir2_data_unused_t *)ptr;
520 /*
521 * Unused, skip it.
522 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100523 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
524 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 continue;
526 }
527
528 dep = (xfs_dir2_data_entry_t *)ptr;
529
530 /*
531 * Bump pointer for the next iteration.
532 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000533 ptr += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /*
535 * The entry is before the desired starting point, skip it.
536 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200537 if ((char *)dep - (char *)hdr < wantoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000540 cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200541 (char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /*
544 * If it didn't fit, set the final offset to here & return.
545 */
Dave Chinner4a24cb72010-01-20 10:48:05 +1100546 if (filldir(dirent, (char *)dep->name, dep->namelen,
547 cook & 0x7fffffff, be64_to_cpu(dep->inumber),
548 DT_UNKNOWN)) {
Christoph Hellwig15440312009-01-08 14:00:00 -0500549 *offset = cook & 0x7fffffff;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000550 xfs_trans_brelse(NULL, bp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553 }
554
555 /*
556 * Reached the end of the block.
Nathan Scottc41564b2006-03-29 08:55:14 +1000557 * Set the offset to a non-existent block 1 and return.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
Christoph Hellwig15440312009-01-08 14:00:00 -0500559 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
560 0x7fffffff;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000561 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return 0;
563}
564
565/*
566 * Log leaf entries from the block.
567 */
568static void
569xfs_dir2_block_log_leaf(
570 xfs_trans_t *tp, /* transaction structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000571 struct xfs_buf *bp, /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int first, /* index of first logged leaf */
573 int last) /* index of last logged leaf */
574{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000575 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200576 xfs_dir2_leaf_entry_t *blp;
577 xfs_dir2_block_tail_t *btp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200579 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000580 blp = xfs_dir2_block_leaf_p(btp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000581 xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200582 (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585/*
586 * Log the block tail.
587 */
588static void
589xfs_dir2_block_log_tail(
590 xfs_trans_t *tp, /* transaction structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000591 struct xfs_buf *bp) /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000593 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200594 xfs_dir2_block_tail_t *btp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200596 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000597 xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200598 (uint)((char *)(btp + 1) - (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601/*
602 * Look up an entry in the block. This is the external routine,
603 * xfs_dir2_block_lookup_int does the real work.
604 */
605int /* error */
606xfs_dir2_block_lookup(
607 xfs_da_args_t *args) /* dir lookup arguments */
608{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200609 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000611 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 xfs_dir2_block_tail_t *btp; /* block tail */
613 xfs_dir2_data_entry_t *dep; /* block data entry */
614 xfs_inode_t *dp; /* incore inode */
615 int ent; /* entry index */
616 int error; /* error return value */
617 xfs_mount_t *mp; /* filesystem mount point */
618
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000619 trace_xfs_dir2_block_lookup(args);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /*
622 * Get the buffer, look up the entry.
623 * If not found (ENOENT) then return, have no buffer.
624 */
625 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
626 return error;
627 dp = args->dp;
628 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000629 hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 xfs_dir2_data_check(dp, bp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200631 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000632 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /*
634 * Get the offset from the leaf entry, to point to the data.
635 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200636 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
Barry Naujok384f3ce2008-05-21 16:58:22 +1000637 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +1000639 * Fill in inode number, CI name if appropriate, release the block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000641 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000642 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000643 xfs_trans_brelse(args->trans, bp);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000644 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
647/*
648 * Internal block lookup routine.
649 */
650static int /* error */
651xfs_dir2_block_lookup_int(
652 xfs_da_args_t *args, /* dir lookup arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000653 struct xfs_buf **bpp, /* returned block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int *entno) /* returned entry number */
655{
656 xfs_dir2_dataptr_t addr; /* data entry address */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200657 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000659 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 xfs_dir2_block_tail_t *btp; /* block tail */
661 xfs_dir2_data_entry_t *dep; /* block data entry */
662 xfs_inode_t *dp; /* incore inode */
663 int error; /* error return value */
664 xfs_dahash_t hash; /* found hash value */
665 int high; /* binary search high index */
666 int low; /* binary search low index */
667 int mid; /* binary search current idx */
668 xfs_mount_t *mp; /* filesystem mount point */
669 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000670 enum xfs_dacmp cmp; /* comparison result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 dp = args->dp;
673 tp = args->trans;
674 mp = dp->i_mount;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100675
676 error = xfs_dir2_block_read(tp, dp, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100677 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return error;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100679
Dave Chinner1d9025e2012-06-22 18:50:14 +1000680 hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 xfs_dir2_data_check(dp, bp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200682 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000683 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /*
685 * Loop doing a binary search for our hash value.
686 * Find our entry, ENOENT if it's not there.
687 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100688 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 ASSERT(low <= high);
690 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100691 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 break;
693 if (hash < args->hashval)
694 low = mid + 1;
695 else
696 high = mid - 1;
697 if (low > high) {
Barry Naujok6a178102008-05-21 16:42:05 +1000698 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000699 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return XFS_ERROR(ENOENT);
701 }
702 }
703 /*
704 * Back up to the first one with the right hash value.
705 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100706 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 mid--;
708 }
709 /*
710 * Now loop forward through all the entries with the
711 * right hash value looking for our name.
712 */
713 do {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100714 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 continue;
716 /*
717 * Get pointer to the entry from the leaf.
718 */
719 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200720 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000722 * Compare name and if it's an exact match, return the index
723 * and buffer. If it's the first case-insensitive match, store
724 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
Barry Naujok5163f952008-05-21 16:41:01 +1000726 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
727 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
728 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 *bpp = bp;
730 *entno = mid;
Barry Naujok5163f952008-05-21 16:41:01 +1000731 if (cmp == XFS_CMP_EXACT)
732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Barry Naujok5163f952008-05-21 16:41:01 +1000734 } while (++mid < be32_to_cpu(btp->count) &&
735 be32_to_cpu(blp[mid].hashval) == hash);
736
Barry Naujok6a178102008-05-21 16:42:05 +1000737 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000738 /*
739 * Here, we can only be doing a lookup (not a rename or replace).
740 * If a case-insensitive match was found earlier, return success.
741 */
742 if (args->cmpresult == XFS_CMP_CASE)
743 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /*
745 * No match, release the buffer and return ENOENT.
746 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000747 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return XFS_ERROR(ENOENT);
749}
750
751/*
752 * Remove an entry from a block format directory.
753 * If that makes the block small enough to fit in shortform, transform it.
754 */
755int /* error */
756xfs_dir2_block_removename(
757 xfs_da_args_t *args) /* directory operation args */
758{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200759 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000761 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 xfs_dir2_block_tail_t *btp; /* block tail */
763 xfs_dir2_data_entry_t *dep; /* block data entry */
764 xfs_inode_t *dp; /* incore inode */
765 int ent; /* block leaf entry index */
766 int error; /* error return value */
767 xfs_mount_t *mp; /* filesystem mount point */
768 int needlog; /* need to log block header */
769 int needscan; /* need to fixup bestfree */
770 xfs_dir2_sf_hdr_t sfh; /* shortform header */
771 int size; /* shortform size */
772 xfs_trans_t *tp; /* transaction pointer */
773
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000774 trace_xfs_dir2_block_removename(args);
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /*
777 * Look up the entry in the block. Gets the buffer and entry index.
778 * It will always be there, the vnodeops level does a lookup first.
779 */
780 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
781 return error;
782 }
783 dp = args->dp;
784 tp = args->trans;
785 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000786 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200787 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000788 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /*
790 * Point to the data entry using the leaf entry.
791 */
792 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200793 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /*
795 * Mark the data entry's space free.
796 */
797 needlog = needscan = 0;
798 xfs_dir2_data_make_free(tp, bp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200799 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000800 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /*
802 * Fix up the block tail.
803 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800804 be32_add_cpu(&btp->stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 xfs_dir2_block_log_tail(tp, bp);
806 /*
807 * Remove the leaf entry by marking it stale.
808 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100809 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
811 /*
812 * Fix up bestfree, log the header if necessary.
813 */
814 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200815 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (needlog)
817 xfs_dir2_data_log_header(tp, bp);
818 xfs_dir2_data_check(dp, bp);
819 /*
820 * See if the size as a shortform is good enough.
821 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200822 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000823 if (size > XFS_IFORK_DSIZE(dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /*
827 * If it works, do the conversion.
828 */
829 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
830}
831
832/*
833 * Replace an entry in a V2 block directory.
834 * Change the inode number to the new value.
835 */
836int /* error */
837xfs_dir2_block_replace(
838 xfs_da_args_t *args) /* directory operation args */
839{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200840 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000842 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 xfs_dir2_block_tail_t *btp; /* block tail */
844 xfs_dir2_data_entry_t *dep; /* block data entry */
845 xfs_inode_t *dp; /* incore inode */
846 int ent; /* leaf entry index */
847 int error; /* error return value */
848 xfs_mount_t *mp; /* filesystem mount point */
849
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000850 trace_xfs_dir2_block_replace(args);
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /*
853 * Lookup the entry in the directory. Get buffer and entry index.
854 * This will always succeed since the caller has already done a lookup.
855 */
856 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
857 return error;
858 }
859 dp = args->dp;
860 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000861 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200862 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000863 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /*
865 * Point to the data entry we need to change.
866 */
867 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200868 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000869 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /*
871 * Change the inode number to the new value.
872 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000873 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 xfs_dir2_data_log_entry(args->trans, bp, dep);
875 xfs_dir2_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return 0;
877}
878
879/*
880 * Qsort comparison routine for the block leaf entries.
881 */
882static int /* sort order */
883xfs_dir2_block_sort(
884 const void *a, /* first leaf entry */
885 const void *b) /* second leaf entry */
886{
887 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
888 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
889
890 la = a;
891 lb = b;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100892 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
893 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896/*
897 * Convert a V2 leaf directory to a V2 block directory if possible.
898 */
899int /* error */
900xfs_dir2_leaf_to_block(
901 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000902 struct xfs_buf *lbp, /* leaf buffer */
903 struct xfs_buf *dbp) /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Nathan Scott68b3a102006-03-17 17:27:19 +1100905 __be16 *bestsp; /* leaf bests table */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200906 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 xfs_dir2_block_tail_t *btp; /* block tail */
908 xfs_inode_t *dp; /* incore directory inode */
909 xfs_dir2_data_unused_t *dup; /* unused data entry */
910 int error; /* error return value */
911 int from; /* leaf from index */
912 xfs_dir2_leaf_t *leaf; /* leaf structure */
913 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
914 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
915 xfs_mount_t *mp; /* file system mount point */
916 int needlog; /* need to log data header */
917 int needscan; /* need to scan for bestfree */
918 xfs_dir2_sf_hdr_t sfh; /* shortform header */
919 int size; /* bytes used */
Nathan Scott3d693c62006-03-17 17:28:27 +1100920 __be16 *tagp; /* end of entry (tag) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int to; /* block/leaf to index */
922 xfs_trans_t *tp; /* transaction pointer */
923
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000924 trace_xfs_dir2_leaf_to_block(args);
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 dp = args->dp;
927 tp = args->trans;
928 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000929 leaf = lbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200930 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000931 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /*
933 * If there are data blocks other than the first one, take this
934 * opportunity to remove trailing empty data blocks that may have
935 * been left behind during no-space-reservation operations.
936 * These will show up in the leaf bests table.
937 */
938 while (dp->i_d.di_size > mp->m_dirblksize) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000939 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100940 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200941 mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if ((error =
943 xfs_dir2_leaf_trim_data(args, lbp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100944 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000945 return error;
946 } else
947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949 /*
950 * Read the data block if we don't already have it, give up if it fails.
951 */
Dave Chinner4bb20a82012-11-12 22:54:10 +1100952 if (!dbp) {
953 error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
954 XFS_DATA_FORK, NULL);
955 if (error)
956 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000958 hdr = dbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200959 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 /*
961 * Size of the "leaf" area in the block.
962 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200963 size = (uint)sizeof(xfs_dir2_block_tail_t) +
Nathan Scotta818e5d2006-03-17 17:28:07 +1100964 (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /*
966 * Look at the last data entry.
967 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200968 tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1;
969 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /*
971 * If it's not free or is too short we can't do it.
972 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100973 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
Dave Chinner1d9025e2012-06-22 18:50:14 +1000974 be16_to_cpu(dup->length) < size)
975 return 0;
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /*
978 * Start converting it to block form.
979 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200980 hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 needlog = 1;
982 needscan = 0;
983 /*
984 * Use up the space at the end of the block (blp/btp).
985 */
986 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
987 &needlog, &needscan);
988 /*
989 * Initialize the block tail.
990 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200991 btp = xfs_dir2_block_tail_p(mp, hdr);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100992 btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 btp->stale = 0;
994 xfs_dir2_block_log_tail(tp, dbp);
995 /*
996 * Initialize the block leaf area. We compact out stale entries.
997 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000998 lep = xfs_dir2_block_leaf_p(btp);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100999 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001000 if (leaf->ents[from].address ==
1001 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 continue;
1003 lep[to++] = leaf->ents[from];
1004 }
Nathan Scotte922fff2006-03-17 17:27:56 +11001005 ASSERT(to == be32_to_cpu(btp->count));
1006 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /*
1008 * Scan the bestfree if we need it and log the data block header.
1009 */
1010 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001011 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (needlog)
1013 xfs_dir2_data_log_header(tp, dbp);
1014 /*
1015 * Pitch the old leaf block.
1016 */
1017 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001018 if (error)
1019 return error;
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /*
1022 * Now see if the resulting block can be shrunken to shortform.
1023 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001024 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001025 if (size > XFS_IFORK_DSIZE(dp))
1026 return 0;
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031/*
1032 * Convert the shortform directory to block form.
1033 */
1034int /* error */
1035xfs_dir2_sf_to_block(
1036 xfs_da_args_t *args) /* operation arguments */
1037{
1038 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001039 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001041 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 xfs_dir2_block_tail_t *btp; /* block tail pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1044 xfs_inode_t *dp; /* incore directory inode */
1045 int dummy; /* trash */
1046 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1047 int endoffset; /* end of data objects */
1048 int error; /* error return value */
1049 int i; /* index */
1050 xfs_mount_t *mp; /* filesystem mount point */
1051 int needlog; /* need to log block header */
1052 int needscan; /* need to scan block freespc */
1053 int newoffset; /* offset from current entry */
1054 int offset; /* target block offset */
1055 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001056 xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
1057 xfs_dir2_sf_hdr_t *sfp; /* shortform header */
Nathan Scott3d693c62006-03-17 17:28:27 +11001058 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +10001060 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001062 trace_xfs_dir2_sf_to_block(args);
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 dp = args->dp;
1065 tp = args->trans;
1066 mp = dp->i_mount;
1067 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1068 /*
1069 * Bomb out if the shortform directory is way too short.
1070 */
1071 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1072 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1073 return XFS_ERROR(EIO);
1074 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001075
1076 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1079 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001080 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /*
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001083 * Copy the directory into a temporary buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 * Then pitch the incore inode data so we can make extents.
1085 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001086 sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1087 memcpy(sfp, oldsfp, dp->i_df.if_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001089 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 dp->i_d.di_size = 0;
1091 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
1094 * Add block 0 to the inode.
1095 */
1096 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1097 if (error) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001098 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return error;
1100 }
1101 /*
1102 * Initialize the data block.
1103 */
1104 error = xfs_dir2_data_init(args, blkno, &bp);
1105 if (error) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001106 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return error;
1108 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001109 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001110 hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /*
1112 * Compute size of block "tail" area.
1113 */
1114 i = (uint)sizeof(*btp) +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001115 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /*
1117 * The whole thing is initialized to free by the init routine.
1118 * Say we're using the leaf and tail area.
1119 */
Christoph Hellwiga64b0412011-07-08 14:35:32 +02001120 dup = (xfs_dir2_data_unused_t *)(hdr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 needlog = needscan = 0;
1122 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1123 &needscan);
1124 ASSERT(needscan == 0);
1125 /*
1126 * Fill in the tail.
1127 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001128 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001129 btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 btp->stale = 0;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001131 blp = xfs_dir2_block_leaf_p(btp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001132 endoffset = (uint)((char *)blp - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /*
1134 * Remove the freespace, we'll manage it.
1135 */
1136 xfs_dir2_data_use_free(tp, bp, dup,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001137 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
Nathan Scottad354eb2006-03-17 17:27:37 +11001138 be16_to_cpu(dup->length), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 /*
1140 * Create entry for .
1141 */
1142 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001143 ((char *)hdr + XFS_DIR2_DATA_DOT_OFFSET);
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001144 dep->inumber = cpu_to_be64(dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 dep->namelen = 1;
1146 dep->name[0] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001147 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001148 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001150 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001151 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001152 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /*
1154 * Create entry for ..
1155 */
1156 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001157 ((char *)hdr + XFS_DIR2_DATA_DOTDOT_OFFSET);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001158 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 dep->namelen = 2;
1160 dep->name[0] = dep->name[1] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001161 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001162 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001164 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001165 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001166 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 offset = XFS_DIR2_DATA_FIRST_OFFSET;
1168 /*
1169 * Loop over existing entries, stuff them in.
1170 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001171 i = 0;
1172 if (!sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 sfep = NULL;
1174 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001175 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /*
1177 * Need to preserve the existing offset values in the sf directory.
1178 * Insert holes (unused entries) where necessary.
1179 */
1180 while (offset < endoffset) {
1181 /*
1182 * sfep is null when we reach the end of the list.
1183 */
1184 if (sfep == NULL)
1185 newoffset = endoffset;
1186 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001187 newoffset = xfs_dir2_sf_get_offset(sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 /*
1189 * There should be a hole here, make one.
1190 */
1191 if (offset < newoffset) {
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001192 dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +11001193 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1194 dup->length = cpu_to_be16(newoffset - offset);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001195 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001196 ((char *)dup - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 xfs_dir2_data_log_unused(tp, bp, dup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001198 xfs_dir2_data_freeinsert(hdr, dup, &dummy);
Nathan Scottad354eb2006-03-17 17:27:37 +11001199 offset += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 continue;
1201 }
1202 /*
1203 * Copy a real entry.
1204 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001205 dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001206 dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 dep->namelen = sfep->namelen;
1208 memcpy(dep->name, sfep->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001209 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001210 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 xfs_dir2_data_log_entry(tp, bp, dep);
Barry Naujok5163f952008-05-21 16:41:01 +10001212 name.name = sfep->name;
1213 name.len = sfep->namelen;
1214 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1215 hashname(&name));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001216 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001217 (char *)dep - (char *)hdr));
1218 offset = (int)((char *)(tagp + 1) - (char *)hdr);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001219 if (++i == sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 sfep = NULL;
1221 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001222 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 /* Done with the temporary buffer */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001225 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /*
1227 * Sort the leaf entries by hash value.
1228 */
Nathan Scotte922fff2006-03-17 17:27:56 +11001229 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 /*
1231 * Log the leaf entry area and tail.
1232 * Already logged the header in data_init, ignore needlog.
1233 */
1234 ASSERT(needscan == 0);
Nathan Scotte922fff2006-03-17 17:27:56 +11001235 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 xfs_dir2_block_log_tail(tp, bp);
1237 xfs_dir2_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return 0;
1239}