blob: 7536faaa61e7852175f7b7c20f1e4b0c566c24db [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 Chinner82025d72012-11-12 22:54:12 +110059static void
60xfs_dir2_block_verify(
61 struct xfs_buf *bp)
62{
63 struct xfs_mount *mp = bp->b_target->bt_mount;
64 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
65 int block_ok = 0;
66
67 block_ok = hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
68 block_ok = block_ok && __xfs_dir2_data_check(NULL, bp) == 0;
69
70 if (!block_ok) {
71 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
72 xfs_buf_ioerror(bp, EFSCORRUPTED);
73 }
Dave Chinner612cfbf2012-11-14 17:52:32 +110074}
Dave Chinner82025d72012-11-12 22:54:12 +110075
Dave Chinner612cfbf2012-11-14 17:52:32 +110076static void
Dave Chinner1813dd62012-11-14 17:54:40 +110077xfs_dir2_block_read_verify(
78 struct xfs_buf *bp)
79{
80 xfs_dir2_block_verify(bp);
81}
82
83static void
Dave Chinner612cfbf2012-11-14 17:52:32 +110084xfs_dir2_block_write_verify(
85 struct xfs_buf *bp)
86{
87 xfs_dir2_block_verify(bp);
88}
89
Dave Chinner1813dd62012-11-14 17:54:40 +110090const struct xfs_buf_ops xfs_dir2_block_buf_ops = {
91 .verify_read = xfs_dir2_block_read_verify,
92 .verify_write = xfs_dir2_block_write_verify,
93};
Dave Chinner82025d72012-11-12 22:54:12 +110094
Dave Chinner20f7e9f2012-11-12 22:54:11 +110095static int
96xfs_dir2_block_read(
97 struct xfs_trans *tp,
98 struct xfs_inode *dp,
99 struct xfs_buf **bpp)
100{
101 struct xfs_mount *mp = dp->i_mount;
102
103 return xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100104 XFS_DATA_FORK, &xfs_dir2_block_buf_ops);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100105}
106
107static void
108xfs_dir2_block_need_space(
109 struct xfs_dir2_data_hdr *hdr,
110 struct xfs_dir2_block_tail *btp,
111 struct xfs_dir2_leaf_entry *blp,
112 __be16 **tagpp,
113 struct xfs_dir2_data_unused **dupp,
114 struct xfs_dir2_data_unused **enddupp,
115 int *compact,
116 int len)
117{
118 struct xfs_dir2_data_free *bf;
119 __be16 *tagp = NULL;
120 struct xfs_dir2_data_unused *dup = NULL;
121 struct xfs_dir2_data_unused *enddup = NULL;
122
123 *compact = 0;
124 bf = hdr->bestfree;
125
126 /*
127 * If there are stale entries we'll use one for the leaf.
128 */
129 if (btp->stale) {
130 if (be16_to_cpu(bf[0].length) >= len) {
131 /*
132 * The biggest entry enough to avoid compaction.
133 */
134 dup = (xfs_dir2_data_unused_t *)
135 ((char *)hdr + be16_to_cpu(bf[0].offset));
136 goto out;
137 }
138
139 /*
140 * Will need to compact to make this work.
141 * Tag just before the first leaf entry.
142 */
143 *compact = 1;
144 tagp = (__be16 *)blp - 1;
145
146 /* Data object just before the first leaf entry. */
147 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
148
149 /*
150 * If it's not free then the data will go where the
151 * leaf data starts now, if it works at all.
152 */
153 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
154 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
155 (uint)sizeof(*blp) < len)
156 dup = NULL;
157 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
158 dup = NULL;
159 else
160 dup = (xfs_dir2_data_unused_t *)blp;
161 goto out;
162 }
163
164 /*
165 * no stale entries, so just use free space.
166 * Tag just before the first leaf entry.
167 */
168 tagp = (__be16 *)blp - 1;
169
170 /* Data object just before the first leaf entry. */
171 enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
172
173 /*
174 * If it's not free then can't do this add without cleaning up:
175 * the space before the first leaf entry needs to be free so it
176 * can be expanded to hold the pointer to the new entry.
177 */
178 if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
179 /*
180 * Check out the biggest freespace and see if it's the same one.
181 */
182 dup = (xfs_dir2_data_unused_t *)
183 ((char *)hdr + be16_to_cpu(bf[0].offset));
184 if (dup != enddup) {
185 /*
186 * Not the same free entry, just check its length.
187 */
188 if (be16_to_cpu(dup->length) < len)
189 dup = NULL;
190 goto out;
191 }
192
193 /*
194 * It is the biggest freespace, can it hold the leaf too?
195 */
196 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
197 /*
198 * Yes, use the second-largest entry instead if it works.
199 */
200 if (be16_to_cpu(bf[1].length) >= len)
201 dup = (xfs_dir2_data_unused_t *)
202 ((char *)hdr + be16_to_cpu(bf[1].offset));
203 else
204 dup = NULL;
205 }
206 }
207out:
208 *tagpp = tagp;
209 *dupp = dup;
210 *enddupp = enddup;
211}
212
213/*
214 * compact the leaf entries.
215 * Leave the highest-numbered stale entry stale.
216 * XXX should be the one closest to mid but mid is not yet computed.
217 */
218static void
219xfs_dir2_block_compact(
220 struct xfs_trans *tp,
221 struct xfs_buf *bp,
222 struct xfs_dir2_data_hdr *hdr,
223 struct xfs_dir2_block_tail *btp,
224 struct xfs_dir2_leaf_entry *blp,
225 int *needlog,
226 int *lfloghigh,
227 int *lfloglow)
228{
229 int fromidx; /* source leaf index */
230 int toidx; /* target leaf index */
231 int needscan = 0;
232 int highstale; /* high stale index */
233
234 fromidx = toidx = be32_to_cpu(btp->count) - 1;
235 highstale = *lfloghigh = -1;
236 for (; fromidx >= 0; fromidx--) {
237 if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
238 if (highstale == -1)
239 highstale = toidx;
240 else {
241 if (*lfloghigh == -1)
242 *lfloghigh = toidx;
243 continue;
244 }
245 }
246 if (fromidx < toidx)
247 blp[toidx] = blp[fromidx];
248 toidx--;
249 }
250 *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
251 *lfloghigh -= be32_to_cpu(btp->stale) - 1;
252 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
253 xfs_dir2_data_make_free(tp, bp,
254 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
255 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
256 needlog, &needscan);
257 blp += be32_to_cpu(btp->stale) - 1;
258 btp->stale = cpu_to_be32(1);
259 /*
260 * If we now need to rebuild the bestfree map, do so.
261 * This needs to happen before the next call to use_free.
262 */
263 if (needscan)
264 xfs_dir2_data_freescan(tp->t_mountp, hdr, needlog);
265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/*
268 * Add an entry to a block directory.
269 */
270int /* error */
271xfs_dir2_block_addname(
272 xfs_da_args_t *args) /* directory op arguments */
273{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200274 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000276 struct xfs_buf *bp; /* buffer for block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 xfs_dir2_block_tail_t *btp; /* block tail */
278 int compact; /* need to compact leaf ents */
279 xfs_dir2_data_entry_t *dep; /* block data entry */
280 xfs_inode_t *dp; /* directory inode */
281 xfs_dir2_data_unused_t *dup; /* block unused entry */
282 int error; /* error return value */
283 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
284 xfs_dahash_t hash; /* hash value of found entry */
285 int high; /* high index for binary srch */
286 int highstale; /* high stale index */
287 int lfloghigh=0; /* last final leaf to log */
288 int lfloglow=0; /* first final leaf to log */
289 int len; /* length of the new entry */
290 int low; /* low index for binary srch */
291 int lowstale; /* low stale index */
292 int mid=0; /* midpoint for binary srch */
293 xfs_mount_t *mp; /* filesystem mount point */
294 int needlog; /* need to log header */
295 int needscan; /* need to rescan freespace */
Nathan Scott3d693c62006-03-17 17:28:27 +1100296 __be16 *tagp; /* pointer to tag value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 xfs_trans_t *tp; /* transaction structure */
298
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000299 trace_xfs_dir2_block_addname(args);
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 dp = args->dp;
302 tp = args->trans;
303 mp = dp->i_mount;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100304
305 /* Read the (one and only) directory block into bp. */
306 error = xfs_dir2_block_read(tp, dp, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100307 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return error;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100309
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000310 len = xfs_dir2_data_entsize(args->namelen);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /*
313 * Set up pointers to parts of the block.
314 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100315 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200316 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000317 blp = xfs_dir2_block_leaf_p(btp);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /*
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100320 * Find out if we can reuse stale entries or whether we need extra
321 * space for entry and new leaf.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100323 xfs_dir2_block_need_space(hdr, btp, blp, &tagp, &dup,
324 &enddup, &compact, len);
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /*
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100327 * Done everything we need for a space check now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100329 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000330 xfs_trans_brelse(tp, bp);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100331 if (!dup)
332 return XFS_ERROR(ENOSPC);
333 return 0;
334 }
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /*
337 * If we don't have space for the new entry & leaf ...
338 */
339 if (!dup) {
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100340 /* Don't have a space reservation: return no-space. */
341 if (args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return XFS_ERROR(ENOSPC);
343 /*
344 * Convert to the next larger format.
345 * Then add the new entry in that format.
346 */
347 error = xfs_dir2_block_to_leaf(args, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (error)
349 return error;
350 return xfs_dir2_leaf_addname(args);
351 }
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 needlog = needscan = 0;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /*
356 * If need to compact the leaf entries, do it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100358 if (compact)
359 xfs_dir2_block_compact(tp, bp, hdr, btp, blp, &needlog,
360 &lfloghigh, &lfloglow);
Nathan Scotte922fff2006-03-17 17:27:56 +1100361 else if (btp->stale) {
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100362 /*
363 * Set leaf logging boundaries to impossible state.
364 * For the no-stale case they're set explicitly.
365 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100366 lfloglow = be32_to_cpu(btp->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 lfloghigh = -1;
368 }
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /*
371 * Find the slot that's first lower than our hash value, -1 if none.
372 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100373 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100375 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 break;
377 if (hash < args->hashval)
378 low = mid + 1;
379 else
380 high = mid - 1;
381 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100382 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 mid--;
384 }
385 /*
386 * No stale entries, will use enddup space to hold new leaf.
387 */
388 if (!btp->stale) {
389 /*
390 * Mark the space needed for the new leaf entry, now in use.
391 */
392 xfs_dir2_data_use_free(tp, bp, enddup,
393 (xfs_dir2_data_aoff_t)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200394 ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 sizeof(*blp)),
396 (xfs_dir2_data_aoff_t)sizeof(*blp),
397 &needlog, &needscan);
398 /*
399 * Update the tail (entry count).
400 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800401 be32_add_cpu(&btp->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /*
403 * If we now need to rebuild the bestfree map, do so.
404 * This needs to happen before the next call to use_free.
405 */
406 if (needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200407 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 needscan = 0;
409 }
410 /*
411 * Adjust pointer to the first leaf entry, we're about to move
412 * the table up one to open up space for the new leaf entry.
413 * Then adjust our index to match.
414 */
415 blp--;
416 mid++;
417 if (mid)
418 memmove(blp, &blp[1], mid * sizeof(*blp));
419 lfloglow = 0;
420 lfloghigh = mid;
421 }
422 /*
423 * Use a stale leaf for our new entry.
424 */
425 else {
426 for (lowstale = mid;
427 lowstale >= 0 &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200428 blp[lowstale].address !=
429 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 lowstale--)
431 continue;
432 for (highstale = mid + 1;
Nathan Scotte922fff2006-03-17 17:27:56 +1100433 highstale < be32_to_cpu(btp->count) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200434 blp[highstale].address !=
435 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 (lowstale < 0 || mid - lowstale > highstale - mid);
437 highstale++)
438 continue;
439 /*
440 * Move entries toward the low-numbered stale entry.
441 */
442 if (lowstale >= 0 &&
Nathan Scotte922fff2006-03-17 17:27:56 +1100443 (highstale == be32_to_cpu(btp->count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 mid - lowstale <= highstale - mid)) {
445 if (mid - lowstale)
446 memmove(&blp[lowstale], &blp[lowstale + 1],
447 (mid - lowstale) * sizeof(*blp));
448 lfloglow = MIN(lowstale, lfloglow);
449 lfloghigh = MAX(mid, lfloghigh);
450 }
451 /*
452 * Move entries toward the high-numbered stale entry.
453 */
454 else {
Nathan Scotte922fff2006-03-17 17:27:56 +1100455 ASSERT(highstale < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 mid++;
457 if (highstale - mid)
458 memmove(&blp[mid + 1], &blp[mid],
459 (highstale - mid) * sizeof(*blp));
460 lfloglow = MIN(mid, lfloglow);
461 lfloghigh = MAX(highstale, lfloghigh);
462 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800463 be32_add_cpu(&btp->stale, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465 /*
466 * Point to the new data entry.
467 */
468 dep = (xfs_dir2_data_entry_t *)dup;
469 /*
470 * Fill in the leaf entry.
471 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100472 blp[mid].hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000473 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200474 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
476 /*
477 * Mark space for the data entry used.
478 */
479 xfs_dir2_data_use_free(tp, bp, dup,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200480 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
482 /*
483 * Create the new data entry.
484 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000485 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 dep->namelen = args->namelen;
487 memcpy(dep->name, args->name, args->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000488 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200489 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /*
491 * Clean up the bestfree array and log the header, tail, and entry.
492 */
493 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200494 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (needlog)
496 xfs_dir2_data_log_header(tp, bp);
497 xfs_dir2_block_log_tail(tp, bp);
498 xfs_dir2_data_log_entry(tp, bp, dep);
499 xfs_dir2_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
501}
502
503/*
504 * Readdir for block directories.
505 */
506int /* error */
507xfs_dir2_block_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 xfs_inode_t *dp, /* incore inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000509 void *dirent,
510 xfs_off_t *offset,
511 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200513 xfs_dir2_data_hdr_t *hdr; /* block header */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000514 struct xfs_buf *bp; /* buffer for block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 xfs_dir2_block_tail_t *btp; /* block tail */
516 xfs_dir2_data_entry_t *dep; /* block data entry */
517 xfs_dir2_data_unused_t *dup; /* block unused entry */
518 char *endptr; /* end of the data entries */
519 int error; /* error return value */
520 xfs_mount_t *mp; /* filesystem mount point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 char *ptr; /* current data entry */
522 int wantoff; /* starting block offset */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000523 xfs_off_t cook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 mp = dp->i_mount;
526 /*
527 * If the block number in the offset is out of range, we're done.
528 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100529 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return 0;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100531
532 error = xfs_dir2_block_read(NULL, dp, &bp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000533 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return error;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /*
537 * Extract the byte offset we start at from the seek pointer.
538 * We'll skip entries before this.
539 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000540 wantoff = xfs_dir2_dataptr_to_off(mp, *offset);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000541 hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 xfs_dir2_data_check(dp, bp);
543 /*
544 * Set up values for the loop.
545 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200546 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200547 ptr = (char *)(hdr + 1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000548 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /*
551 * Loop over the data portion of the block.
552 * Each object is a real entry (dep) or an unused one (dup).
553 */
554 while (ptr < endptr) {
555 dup = (xfs_dir2_data_unused_t *)ptr;
556 /*
557 * Unused, skip it.
558 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100559 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
560 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 continue;
562 }
563
564 dep = (xfs_dir2_data_entry_t *)ptr;
565
566 /*
567 * Bump pointer for the next iteration.
568 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000569 ptr += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /*
571 * The entry is before the desired starting point, skip it.
572 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200573 if ((char *)dep - (char *)hdr < wantoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000576 cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200577 (char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /*
580 * If it didn't fit, set the final offset to here & return.
581 */
Dave Chinner4a24cb72010-01-20 10:48:05 +1100582 if (filldir(dirent, (char *)dep->name, dep->namelen,
583 cook & 0x7fffffff, be64_to_cpu(dep->inumber),
584 DT_UNKNOWN)) {
Christoph Hellwig15440312009-01-08 14:00:00 -0500585 *offset = cook & 0x7fffffff;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000586 xfs_trans_brelse(NULL, bp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589 }
590
591 /*
592 * Reached the end of the block.
Nathan Scottc41564b2006-03-29 08:55:14 +1000593 * Set the offset to a non-existent block 1 and return.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 */
Christoph Hellwig15440312009-01-08 14:00:00 -0500595 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
596 0x7fffffff;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000597 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 0;
599}
600
601/*
602 * Log leaf entries from the block.
603 */
604static void
605xfs_dir2_block_log_leaf(
606 xfs_trans_t *tp, /* transaction structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000607 struct xfs_buf *bp, /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int first, /* index of first logged leaf */
609 int last) /* index of last logged leaf */
610{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000611 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200612 xfs_dir2_leaf_entry_t *blp;
613 xfs_dir2_block_tail_t *btp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200615 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000616 blp = xfs_dir2_block_leaf_p(btp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000617 xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200618 (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621/*
622 * Log the block tail.
623 */
624static void
625xfs_dir2_block_log_tail(
626 xfs_trans_t *tp, /* transaction structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000627 struct xfs_buf *bp) /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000629 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200630 xfs_dir2_block_tail_t *btp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200632 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000633 xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200634 (uint)((char *)(btp + 1) - (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637/*
638 * Look up an entry in the block. This is the external routine,
639 * xfs_dir2_block_lookup_int does the real work.
640 */
641int /* error */
642xfs_dir2_block_lookup(
643 xfs_da_args_t *args) /* dir lookup arguments */
644{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200645 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000647 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 xfs_dir2_block_tail_t *btp; /* block tail */
649 xfs_dir2_data_entry_t *dep; /* block data entry */
650 xfs_inode_t *dp; /* incore inode */
651 int ent; /* entry index */
652 int error; /* error return value */
653 xfs_mount_t *mp; /* filesystem mount point */
654
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000655 trace_xfs_dir2_block_lookup(args);
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
658 * Get the buffer, look up the entry.
659 * If not found (ENOENT) then return, have no buffer.
660 */
661 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
662 return error;
663 dp = args->dp;
664 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000665 hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 xfs_dir2_data_check(dp, bp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200667 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000668 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /*
670 * Get the offset from the leaf entry, to point to the data.
671 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200672 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
Barry Naujok384f3ce2008-05-21 16:58:22 +1000673 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +1000675 * Fill in inode number, CI name if appropriate, release the block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000677 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000678 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000679 xfs_trans_brelse(args->trans, bp);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000680 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683/*
684 * Internal block lookup routine.
685 */
686static int /* error */
687xfs_dir2_block_lookup_int(
688 xfs_da_args_t *args, /* dir lookup arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000689 struct xfs_buf **bpp, /* returned block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int *entno) /* returned entry number */
691{
692 xfs_dir2_dataptr_t addr; /* data entry address */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200693 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000695 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 xfs_dir2_block_tail_t *btp; /* block tail */
697 xfs_dir2_data_entry_t *dep; /* block data entry */
698 xfs_inode_t *dp; /* incore inode */
699 int error; /* error return value */
700 xfs_dahash_t hash; /* found hash value */
701 int high; /* binary search high index */
702 int low; /* binary search low index */
703 int mid; /* binary search current idx */
704 xfs_mount_t *mp; /* filesystem mount point */
705 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000706 enum xfs_dacmp cmp; /* comparison result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 dp = args->dp;
709 tp = args->trans;
710 mp = dp->i_mount;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100711
712 error = xfs_dir2_block_read(tp, dp, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100713 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return error;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100715
Dave Chinner1d9025e2012-06-22 18:50:14 +1000716 hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 xfs_dir2_data_check(dp, bp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200718 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000719 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /*
721 * Loop doing a binary search for our hash value.
722 * Find our entry, ENOENT if it's not there.
723 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100724 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 ASSERT(low <= high);
726 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100727 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 break;
729 if (hash < args->hashval)
730 low = mid + 1;
731 else
732 high = mid - 1;
733 if (low > high) {
Barry Naujok6a178102008-05-21 16:42:05 +1000734 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000735 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return XFS_ERROR(ENOENT);
737 }
738 }
739 /*
740 * Back up to the first one with the right hash value.
741 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100742 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 mid--;
744 }
745 /*
746 * Now loop forward through all the entries with the
747 * right hash value looking for our name.
748 */
749 do {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100750 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 continue;
752 /*
753 * Get pointer to the entry from the leaf.
754 */
755 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200756 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000758 * Compare name and if it's an exact match, return the index
759 * and buffer. If it's the first case-insensitive match, store
760 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 */
Barry Naujok5163f952008-05-21 16:41:01 +1000762 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
763 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
764 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 *bpp = bp;
766 *entno = mid;
Barry Naujok5163f952008-05-21 16:41:01 +1000767 if (cmp == XFS_CMP_EXACT)
768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Barry Naujok5163f952008-05-21 16:41:01 +1000770 } while (++mid < be32_to_cpu(btp->count) &&
771 be32_to_cpu(blp[mid].hashval) == hash);
772
Barry Naujok6a178102008-05-21 16:42:05 +1000773 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000774 /*
775 * Here, we can only be doing a lookup (not a rename or replace).
776 * If a case-insensitive match was found earlier, return success.
777 */
778 if (args->cmpresult == XFS_CMP_CASE)
779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /*
781 * No match, release the buffer and return ENOENT.
782 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000783 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return XFS_ERROR(ENOENT);
785}
786
787/*
788 * Remove an entry from a block format directory.
789 * If that makes the block small enough to fit in shortform, transform it.
790 */
791int /* error */
792xfs_dir2_block_removename(
793 xfs_da_args_t *args) /* directory operation args */
794{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200795 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000797 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 xfs_dir2_block_tail_t *btp; /* block tail */
799 xfs_dir2_data_entry_t *dep; /* block data entry */
800 xfs_inode_t *dp; /* incore inode */
801 int ent; /* block leaf entry index */
802 int error; /* error return value */
803 xfs_mount_t *mp; /* filesystem mount point */
804 int needlog; /* need to log block header */
805 int needscan; /* need to fixup bestfree */
806 xfs_dir2_sf_hdr_t sfh; /* shortform header */
807 int size; /* shortform size */
808 xfs_trans_t *tp; /* transaction pointer */
809
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000810 trace_xfs_dir2_block_removename(args);
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /*
813 * Look up the entry in the block. Gets the buffer and entry index.
814 * It will always be there, the vnodeops level does a lookup first.
815 */
816 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
817 return error;
818 }
819 dp = args->dp;
820 tp = args->trans;
821 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000822 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200823 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000824 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /*
826 * Point to the data entry using the leaf entry.
827 */
828 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200829 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /*
831 * Mark the data entry's space free.
832 */
833 needlog = needscan = 0;
834 xfs_dir2_data_make_free(tp, bp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200835 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000836 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /*
838 * Fix up the block tail.
839 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800840 be32_add_cpu(&btp->stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 xfs_dir2_block_log_tail(tp, bp);
842 /*
843 * Remove the leaf entry by marking it stale.
844 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100845 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
847 /*
848 * Fix up bestfree, log the header if necessary.
849 */
850 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200851 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (needlog)
853 xfs_dir2_data_log_header(tp, bp);
854 xfs_dir2_data_check(dp, bp);
855 /*
856 * See if the size as a shortform is good enough.
857 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200858 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000859 if (size > XFS_IFORK_DSIZE(dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /*
863 * If it works, do the conversion.
864 */
865 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
866}
867
868/*
869 * Replace an entry in a V2 block directory.
870 * Change the inode number to the new value.
871 */
872int /* error */
873xfs_dir2_block_replace(
874 xfs_da_args_t *args) /* directory operation args */
875{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200876 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000878 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 xfs_dir2_block_tail_t *btp; /* block tail */
880 xfs_dir2_data_entry_t *dep; /* block data entry */
881 xfs_inode_t *dp; /* incore inode */
882 int ent; /* leaf entry index */
883 int error; /* error return value */
884 xfs_mount_t *mp; /* filesystem mount point */
885
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000886 trace_xfs_dir2_block_replace(args);
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /*
889 * Lookup the entry in the directory. Get buffer and entry index.
890 * This will always succeed since the caller has already done a lookup.
891 */
892 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
893 return error;
894 }
895 dp = args->dp;
896 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000897 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200898 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000899 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /*
901 * Point to the data entry we need to change.
902 */
903 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200904 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000905 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /*
907 * Change the inode number to the new value.
908 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000909 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 xfs_dir2_data_log_entry(args->trans, bp, dep);
911 xfs_dir2_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 0;
913}
914
915/*
916 * Qsort comparison routine for the block leaf entries.
917 */
918static int /* sort order */
919xfs_dir2_block_sort(
920 const void *a, /* first leaf entry */
921 const void *b) /* second leaf entry */
922{
923 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
924 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
925
926 la = a;
927 lb = b;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100928 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
929 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
932/*
933 * Convert a V2 leaf directory to a V2 block directory if possible.
934 */
935int /* error */
936xfs_dir2_leaf_to_block(
937 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000938 struct xfs_buf *lbp, /* leaf buffer */
939 struct xfs_buf *dbp) /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Nathan Scott68b3a102006-03-17 17:27:19 +1100941 __be16 *bestsp; /* leaf bests table */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200942 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 xfs_dir2_block_tail_t *btp; /* block tail */
944 xfs_inode_t *dp; /* incore directory inode */
945 xfs_dir2_data_unused_t *dup; /* unused data entry */
946 int error; /* error return value */
947 int from; /* leaf from index */
948 xfs_dir2_leaf_t *leaf; /* leaf structure */
949 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
950 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
951 xfs_mount_t *mp; /* file system mount point */
952 int needlog; /* need to log data header */
953 int needscan; /* need to scan for bestfree */
954 xfs_dir2_sf_hdr_t sfh; /* shortform header */
955 int size; /* bytes used */
Nathan Scott3d693c62006-03-17 17:28:27 +1100956 __be16 *tagp; /* end of entry (tag) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 int to; /* block/leaf to index */
958 xfs_trans_t *tp; /* transaction pointer */
959
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000960 trace_xfs_dir2_leaf_to_block(args);
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 dp = args->dp;
963 tp = args->trans;
964 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000965 leaf = lbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200966 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000967 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /*
969 * If there are data blocks other than the first one, take this
970 * opportunity to remove trailing empty data blocks that may have
971 * been left behind during no-space-reservation operations.
972 * These will show up in the leaf bests table.
973 */
974 while (dp->i_d.di_size > mp->m_dirblksize) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000975 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100976 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200977 mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if ((error =
979 xfs_dir2_leaf_trim_data(args, lbp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100980 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000981 return error;
982 } else
983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 /*
986 * Read the data block if we don't already have it, give up if it fails.
987 */
Dave Chinner4bb20a82012-11-12 22:54:10 +1100988 if (!dbp) {
Dave Chinnere4813572012-11-12 22:54:14 +1100989 error = xfs_dir2_data_read(tp, dp, mp->m_dirdatablk, -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100990 if (error)
991 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000993 hdr = dbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200994 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /*
996 * Size of the "leaf" area in the block.
997 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200998 size = (uint)sizeof(xfs_dir2_block_tail_t) +
Nathan Scotta818e5d2006-03-17 17:28:07 +1100999 (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /*
1001 * Look at the last data entry.
1002 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001003 tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1;
1004 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /*
1006 * If it's not free or is too short we can't do it.
1007 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001008 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
Dave Chinner1d9025e2012-06-22 18:50:14 +10001009 be16_to_cpu(dup->length) < size)
1010 return 0;
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /*
1013 * Start converting it to block form.
1014 */
Dave Chinner1813dd62012-11-14 17:54:40 +11001015 dbp->b_ops = &xfs_dir2_block_buf_ops;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001016 hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 needlog = 1;
1018 needscan = 0;
1019 /*
1020 * Use up the space at the end of the block (blp/btp).
1021 */
1022 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
1023 &needlog, &needscan);
1024 /*
1025 * Initialize the block tail.
1026 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001027 btp = xfs_dir2_block_tail_p(mp, hdr);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001028 btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 btp->stale = 0;
1030 xfs_dir2_block_log_tail(tp, dbp);
1031 /*
1032 * Initialize the block leaf area. We compact out stale entries.
1033 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001034 lep = xfs_dir2_block_leaf_p(btp);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001035 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001036 if (leaf->ents[from].address ==
1037 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 continue;
1039 lep[to++] = leaf->ents[from];
1040 }
Nathan Scotte922fff2006-03-17 17:27:56 +11001041 ASSERT(to == be32_to_cpu(btp->count));
1042 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /*
1044 * Scan the bestfree if we need it and log the data block header.
1045 */
1046 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001047 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (needlog)
1049 xfs_dir2_data_log_header(tp, dbp);
1050 /*
1051 * Pitch the old leaf block.
1052 */
1053 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001054 if (error)
1055 return error;
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /*
1058 * Now see if the resulting block can be shrunken to shortform.
1059 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001060 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001061 if (size > XFS_IFORK_DSIZE(dp))
1062 return 0;
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067/*
1068 * Convert the shortform directory to block form.
1069 */
1070int /* error */
1071xfs_dir2_sf_to_block(
1072 xfs_da_args_t *args) /* operation arguments */
1073{
1074 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001075 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001077 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 xfs_dir2_block_tail_t *btp; /* block tail pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1080 xfs_inode_t *dp; /* incore directory inode */
1081 int dummy; /* trash */
1082 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1083 int endoffset; /* end of data objects */
1084 int error; /* error return value */
1085 int i; /* index */
1086 xfs_mount_t *mp; /* filesystem mount point */
1087 int needlog; /* need to log block header */
1088 int needscan; /* need to scan block freespc */
1089 int newoffset; /* offset from current entry */
1090 int offset; /* target block offset */
1091 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001092 xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
1093 xfs_dir2_sf_hdr_t *sfp; /* shortform header */
Nathan Scott3d693c62006-03-17 17:28:27 +11001094 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +10001096 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001098 trace_xfs_dir2_sf_to_block(args);
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 dp = args->dp;
1101 tp = args->trans;
1102 mp = dp->i_mount;
1103 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1104 /*
1105 * Bomb out if the shortform directory is way too short.
1106 */
1107 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1108 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1109 return XFS_ERROR(EIO);
1110 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001111
1112 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1115 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001116 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /*
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001119 * Copy the directory into a temporary buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 * Then pitch the incore inode data so we can make extents.
1121 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001122 sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1123 memcpy(sfp, oldsfp, dp->i_df.if_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001125 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 dp->i_d.di_size = 0;
1127 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /*
1130 * Add block 0 to the inode.
1131 */
1132 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1133 if (error) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001134 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return error;
1136 }
1137 /*
1138 * Initialize the data block.
1139 */
1140 error = xfs_dir2_data_init(args, blkno, &bp);
1141 if (error) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001142 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return error;
1144 }
Dave Chinner1813dd62012-11-14 17:54:40 +11001145 bp->b_ops = &xfs_dir2_block_buf_ops;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001146 hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001147 hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /*
1149 * Compute size of block "tail" area.
1150 */
1151 i = (uint)sizeof(*btp) +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001152 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /*
1154 * The whole thing is initialized to free by the init routine.
1155 * Say we're using the leaf and tail area.
1156 */
Christoph Hellwiga64b0412011-07-08 14:35:32 +02001157 dup = (xfs_dir2_data_unused_t *)(hdr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 needlog = needscan = 0;
1159 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1160 &needscan);
1161 ASSERT(needscan == 0);
1162 /*
1163 * Fill in the tail.
1164 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001165 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001166 btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 btp->stale = 0;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001168 blp = xfs_dir2_block_leaf_p(btp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001169 endoffset = (uint)((char *)blp - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /*
1171 * Remove the freespace, we'll manage it.
1172 */
1173 xfs_dir2_data_use_free(tp, bp, dup,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001174 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
Nathan Scottad354eb2006-03-17 17:27:37 +11001175 be16_to_cpu(dup->length), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /*
1177 * Create entry for .
1178 */
1179 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001180 ((char *)hdr + XFS_DIR2_DATA_DOT_OFFSET);
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001181 dep->inumber = cpu_to_be64(dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 dep->namelen = 1;
1183 dep->name[0] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001184 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001185 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001187 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001188 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001189 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 /*
1191 * Create entry for ..
1192 */
1193 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001194 ((char *)hdr + XFS_DIR2_DATA_DOTDOT_OFFSET);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001195 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 dep->namelen = 2;
1197 dep->name[0] = dep->name[1] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001198 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001199 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001201 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001202 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001203 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 offset = XFS_DIR2_DATA_FIRST_OFFSET;
1205 /*
1206 * Loop over existing entries, stuff them in.
1207 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001208 i = 0;
1209 if (!sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 sfep = NULL;
1211 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001212 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 /*
1214 * Need to preserve the existing offset values in the sf directory.
1215 * Insert holes (unused entries) where necessary.
1216 */
1217 while (offset < endoffset) {
1218 /*
1219 * sfep is null when we reach the end of the list.
1220 */
1221 if (sfep == NULL)
1222 newoffset = endoffset;
1223 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001224 newoffset = xfs_dir2_sf_get_offset(sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /*
1226 * There should be a hole here, make one.
1227 */
1228 if (offset < newoffset) {
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001229 dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +11001230 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1231 dup->length = cpu_to_be16(newoffset - offset);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001232 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001233 ((char *)dup - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 xfs_dir2_data_log_unused(tp, bp, dup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001235 xfs_dir2_data_freeinsert(hdr, dup, &dummy);
Nathan Scottad354eb2006-03-17 17:27:37 +11001236 offset += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 continue;
1238 }
1239 /*
1240 * Copy a real entry.
1241 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001242 dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001243 dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 dep->namelen = sfep->namelen;
1245 memcpy(dep->name, sfep->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001246 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001247 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 xfs_dir2_data_log_entry(tp, bp, dep);
Barry Naujok5163f952008-05-21 16:41:01 +10001249 name.name = sfep->name;
1250 name.len = sfep->namelen;
1251 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1252 hashname(&name));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001253 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001254 (char *)dep - (char *)hdr));
1255 offset = (int)((char *)(tagp + 1) - (char *)hdr);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001256 if (++i == sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 sfep = NULL;
1258 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001259 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261 /* Done with the temporary buffer */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001262 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /*
1264 * Sort the leaf entries by hash value.
1265 */
Nathan Scotte922fff2006-03-17 17:27:56 +11001266 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 /*
1268 * Log the leaf entry area and tail.
1269 * Already logged the header in data_init, ignore needlog.
1270 */
1271 ASSERT(needscan == 0);
Nathan Scotte922fff2006-03-17 17:27:56 +11001272 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 xfs_dir2_block_log_tail(tp, bp);
1274 xfs_dir2_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return 0;
1276}