blob: c9fdabe5e1adb1815a42a67d3a6d61210764d3df [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"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dir2_sf.h"
31#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_data.h"
35#include "xfs_dir2_leaf.h"
36#include "xfs_dir2_block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000038#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * Local function prototypes.
42 */
43static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, xfs_dabuf_t *bp, int first,
44 int last);
45static void xfs_dir2_block_log_tail(xfs_trans_t *tp, xfs_dabuf_t *bp);
46static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **bpp,
47 int *entno);
48static int xfs_dir2_block_sort(const void *a, const void *b);
49
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100050static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
51
52/*
53 * One-time startup routine called from xfs_init().
54 */
55void
56xfs_dir_startup(void)
57{
Dave Chinner4a24cb72010-01-20 10:48:05 +110058 xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1);
59 xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100060}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
63 * Add an entry to a block directory.
64 */
65int /* error */
66xfs_dir2_block_addname(
67 xfs_da_args_t *args) /* directory op arguments */
68{
69 xfs_dir2_data_free_t *bf; /* bestfree table in block */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +020070 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
72 xfs_dabuf_t *bp; /* buffer for block */
73 xfs_dir2_block_tail_t *btp; /* block tail */
74 int compact; /* need to compact leaf ents */
75 xfs_dir2_data_entry_t *dep; /* block data entry */
76 xfs_inode_t *dp; /* directory inode */
77 xfs_dir2_data_unused_t *dup; /* block unused entry */
78 int error; /* error return value */
79 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
80 xfs_dahash_t hash; /* hash value of found entry */
81 int high; /* high index for binary srch */
82 int highstale; /* high stale index */
83 int lfloghigh=0; /* last final leaf to log */
84 int lfloglow=0; /* first final leaf to log */
85 int len; /* length of the new entry */
86 int low; /* low index for binary srch */
87 int lowstale; /* low stale index */
88 int mid=0; /* midpoint for binary srch */
89 xfs_mount_t *mp; /* filesystem mount point */
90 int needlog; /* need to log header */
91 int needscan; /* need to rescan freespace */
Nathan Scott3d693c62006-03-17 17:28:27 +110092 __be16 *tagp; /* pointer to tag value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 xfs_trans_t *tp; /* transaction structure */
94
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000095 trace_xfs_dir2_block_addname(args);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 dp = args->dp;
98 tp = args->trans;
99 mp = dp->i_mount;
100 /*
101 * Read the (one and only) directory block into dabuf bp.
102 */
103 if ((error =
104 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
105 return error;
106 }
107 ASSERT(bp != NULL);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200108 hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /*
110 * Check the magic number, corrupted if wrong.
111 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200112 if (unlikely(be32_to_cpu(hdr->magic) != XFS_DIR2_BLOCK_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 XFS_CORRUPTION_ERROR("xfs_dir2_block_addname",
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200114 XFS_ERRLEVEL_LOW, mp, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 xfs_da_brelse(tp, bp);
116 return XFS_ERROR(EFSCORRUPTED);
117 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000118 len = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 /*
120 * Set up pointers to parts of the block.
121 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200122 bf = hdr->bestfree;
123 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000124 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /*
126 * No stale entries? Need space for entry and new leaf.
127 */
128 if (!btp->stale) {
129 /*
130 * Tag just before the first leaf entry.
131 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100132 tagp = (__be16 *)blp - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /*
134 * Data object just before the first leaf entry.
135 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200136 enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /*
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 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100142 if (be16_to_cpu(enddup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 dup = enddup = NULL;
144 /*
145 * Check out the biggest freespace and see if it's the same one.
146 */
147 else {
148 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200149 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (dup == enddup) {
151 /*
152 * It is the biggest freespace, is it too small
153 * to hold the new leaf too?
154 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100155 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /*
157 * Yes, we use the second-largest
158 * entry instead if it works.
159 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100160 if (be16_to_cpu(bf[1].length) >= len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200162 ((char *)hdr +
Nathan Scott70e73f52006-03-17 17:26:52 +1100163 be16_to_cpu(bf[1].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 else
165 dup = NULL;
166 }
167 } else {
168 /*
169 * Not the same free entry,
170 * just check its length.
171 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100172 if (be16_to_cpu(dup->length) < len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 dup = NULL;
174 }
175 }
176 }
177 compact = 0;
178 }
179 /*
180 * If there are stale entries we'll use one for the leaf.
181 * Is the biggest entry enough to avoid compaction?
182 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100183 else if (be16_to_cpu(bf[0].length) >= len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200185 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 compact = 0;
187 }
188 /*
189 * Will need to compact to make this work.
190 */
191 else {
192 /*
193 * Tag just before the first leaf entry.
194 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100195 tagp = (__be16 *)blp - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /*
197 * Data object just before the first leaf entry.
198 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200199 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /*
201 * If it's not free then the data will go where the
202 * leaf data starts now, if it works at all.
203 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100204 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100205 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 (uint)sizeof(*blp) < len)
207 dup = NULL;
Nathan Scotte922fff2006-03-17 17:27:56 +1100208 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 dup = NULL;
210 else
211 dup = (xfs_dir2_data_unused_t *)blp;
212 compact = 1;
213 }
214 /*
215 * If this isn't a real add, we're done with the buffer.
216 */
Barry Naujok6a178102008-05-21 16:42:05 +1000217 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 xfs_da_brelse(tp, bp);
219 /*
220 * If we don't have space for the new entry & leaf ...
221 */
222 if (!dup) {
223 /*
224 * Not trying to actually do anything, or don't have
225 * a space reservation: return no-space.
226 */
Barry Naujok6a178102008-05-21 16:42:05 +1000227 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return XFS_ERROR(ENOSPC);
229 /*
230 * Convert to the next larger format.
231 * Then add the new entry in that format.
232 */
233 error = xfs_dir2_block_to_leaf(args, bp);
234 xfs_da_buf_done(bp);
235 if (error)
236 return error;
237 return xfs_dir2_leaf_addname(args);
238 }
239 /*
240 * Just checking, and it would work, so say so.
241 */
Barry Naujok6a178102008-05-21 16:42:05 +1000242 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244 needlog = needscan = 0;
245 /*
246 * If need to compact the leaf entries, do it now.
247 * Leave the highest-numbered stale entry stale.
248 * XXX should be the one closest to mid but mid is not yet computed.
249 */
250 if (compact) {
251 int fromidx; /* source leaf index */
252 int toidx; /* target leaf index */
253
Nathan Scotte922fff2006-03-17 17:27:56 +1100254 for (fromidx = toidx = be32_to_cpu(btp->count) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 highstale = lfloghigh = -1;
256 fromidx >= 0;
257 fromidx--) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100258 if (be32_to_cpu(blp[fromidx].address) == XFS_DIR2_NULL_DATAPTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (highstale == -1)
260 highstale = toidx;
261 else {
262 if (lfloghigh == -1)
263 lfloghigh = toidx;
264 continue;
265 }
266 }
267 if (fromidx < toidx)
268 blp[toidx] = blp[fromidx];
269 toidx--;
270 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100271 lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
272 lfloghigh -= be32_to_cpu(btp->stale) - 1;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800273 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 xfs_dir2_data_make_free(tp, bp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200275 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
Nathan Scotte922fff2006-03-17 17:27:56 +1100276 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 &needlog, &needscan);
Nathan Scotte922fff2006-03-17 17:27:56 +1100278 blp += be32_to_cpu(btp->stale) - 1;
279 btp->stale = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /*
281 * If we now need to rebuild the bestfree map, do so.
282 * This needs to happen before the next call to use_free.
283 */
284 if (needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200285 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 needscan = 0;
287 }
288 }
289 /*
290 * Set leaf logging boundaries to impossible state.
291 * For the no-stale case they're set explicitly.
292 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100293 else if (btp->stale) {
294 lfloglow = be32_to_cpu(btp->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 lfloghigh = -1;
296 }
297 /*
298 * Find the slot that's first lower than our hash value, -1 if none.
299 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100300 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100302 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 break;
304 if (hash < args->hashval)
305 low = mid + 1;
306 else
307 high = mid - 1;
308 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100309 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 mid--;
311 }
312 /*
313 * No stale entries, will use enddup space to hold new leaf.
314 */
315 if (!btp->stale) {
316 /*
317 * Mark the space needed for the new leaf entry, now in use.
318 */
319 xfs_dir2_data_use_free(tp, bp, enddup,
320 (xfs_dir2_data_aoff_t)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200321 ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 sizeof(*blp)),
323 (xfs_dir2_data_aoff_t)sizeof(*blp),
324 &needlog, &needscan);
325 /*
326 * Update the tail (entry count).
327 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800328 be32_add_cpu(&btp->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /*
330 * If we now need to rebuild the bestfree map, do so.
331 * This needs to happen before the next call to use_free.
332 */
333 if (needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200334 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 needscan = 0;
336 }
337 /*
338 * Adjust pointer to the first leaf entry, we're about to move
339 * the table up one to open up space for the new leaf entry.
340 * Then adjust our index to match.
341 */
342 blp--;
343 mid++;
344 if (mid)
345 memmove(blp, &blp[1], mid * sizeof(*blp));
346 lfloglow = 0;
347 lfloghigh = mid;
348 }
349 /*
350 * Use a stale leaf for our new entry.
351 */
352 else {
353 for (lowstale = mid;
354 lowstale >= 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100355 be32_to_cpu(blp[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 lowstale--)
357 continue;
358 for (highstale = mid + 1;
Nathan Scotte922fff2006-03-17 17:27:56 +1100359 highstale < be32_to_cpu(btp->count) &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100360 be32_to_cpu(blp[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 (lowstale < 0 || mid - lowstale > highstale - mid);
362 highstale++)
363 continue;
364 /*
365 * Move entries toward the low-numbered stale entry.
366 */
367 if (lowstale >= 0 &&
Nathan Scotte922fff2006-03-17 17:27:56 +1100368 (highstale == be32_to_cpu(btp->count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 mid - lowstale <= highstale - mid)) {
370 if (mid - lowstale)
371 memmove(&blp[lowstale], &blp[lowstale + 1],
372 (mid - lowstale) * sizeof(*blp));
373 lfloglow = MIN(lowstale, lfloglow);
374 lfloghigh = MAX(mid, lfloghigh);
375 }
376 /*
377 * Move entries toward the high-numbered stale entry.
378 */
379 else {
Nathan Scotte922fff2006-03-17 17:27:56 +1100380 ASSERT(highstale < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 mid++;
382 if (highstale - mid)
383 memmove(&blp[mid + 1], &blp[mid],
384 (highstale - mid) * sizeof(*blp));
385 lfloglow = MIN(mid, lfloglow);
386 lfloghigh = MAX(highstale, lfloghigh);
387 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800388 be32_add_cpu(&btp->stale, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390 /*
391 * Point to the new data entry.
392 */
393 dep = (xfs_dir2_data_entry_t *)dup;
394 /*
395 * Fill in the leaf entry.
396 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100397 blp[mid].hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000398 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200399 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
401 /*
402 * Mark space for the data entry used.
403 */
404 xfs_dir2_data_use_free(tp, bp, dup,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200405 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
407 /*
408 * Create the new data entry.
409 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000410 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 dep->namelen = args->namelen;
412 memcpy(dep->name, args->name, args->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000413 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200414 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /*
416 * Clean up the bestfree array and log the header, tail, and entry.
417 */
418 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200419 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (needlog)
421 xfs_dir2_data_log_header(tp, bp);
422 xfs_dir2_block_log_tail(tp, bp);
423 xfs_dir2_data_log_entry(tp, bp, dep);
424 xfs_dir2_data_check(dp, bp);
425 xfs_da_buf_done(bp);
426 return 0;
427}
428
429/*
430 * Readdir for block directories.
431 */
432int /* error */
433xfs_dir2_block_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 xfs_inode_t *dp, /* incore inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000435 void *dirent,
436 xfs_off_t *offset,
437 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200439 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 xfs_dabuf_t *bp; /* buffer for block */
441 xfs_dir2_block_tail_t *btp; /* block tail */
442 xfs_dir2_data_entry_t *dep; /* block data entry */
443 xfs_dir2_data_unused_t *dup; /* block unused entry */
444 char *endptr; /* end of the data entries */
445 int error; /* error return value */
446 xfs_mount_t *mp; /* filesystem mount point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 char *ptr; /* current data entry */
448 int wantoff; /* starting block offset */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000449 xfs_off_t cook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 mp = dp->i_mount;
452 /*
453 * If the block number in the offset is out of range, we're done.
454 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000455 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 0;
457 }
458 /*
459 * Can't read the block, give up, else get dabuf in bp.
460 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000461 error = xfs_da_read_buf(NULL, dp, mp->m_dirdatablk, -1,
462 &bp, XFS_DATA_FORK);
463 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return error;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 ASSERT(bp != NULL);
467 /*
468 * Extract the byte offset we start at from the seek pointer.
469 * We'll skip entries before this.
470 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000471 wantoff = xfs_dir2_dataptr_to_off(mp, *offset);
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200472 hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 xfs_dir2_data_check(dp, bp);
474 /*
475 * Set up values for the loop.
476 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200477 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200478 ptr = (char *)(hdr + 1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000479 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /*
482 * Loop over the data portion of the block.
483 * Each object is a real entry (dep) or an unused one (dup).
484 */
485 while (ptr < endptr) {
486 dup = (xfs_dir2_data_unused_t *)ptr;
487 /*
488 * Unused, skip it.
489 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100490 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
491 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 continue;
493 }
494
495 dep = (xfs_dir2_data_entry_t *)ptr;
496
497 /*
498 * Bump pointer for the next iteration.
499 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000500 ptr += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /*
502 * The entry is before the desired starting point, skip it.
503 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200504 if ((char *)dep - (char *)hdr < wantoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000507 cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200508 (char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /*
511 * If it didn't fit, set the final offset to here & return.
512 */
Dave Chinner4a24cb72010-01-20 10:48:05 +1100513 if (filldir(dirent, (char *)dep->name, dep->namelen,
514 cook & 0x7fffffff, be64_to_cpu(dep->inumber),
515 DT_UNKNOWN)) {
Christoph Hellwig15440312009-01-08 14:00:00 -0500516 *offset = cook & 0x7fffffff;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000517 xfs_da_brelse(NULL, bp);
518 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 }
521
522 /*
523 * Reached the end of the block.
Nathan Scottc41564b2006-03-29 08:55:14 +1000524 * Set the offset to a non-existent block 1 and return.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
Christoph Hellwig15440312009-01-08 14:00:00 -0500526 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
527 0x7fffffff;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000528 xfs_da_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return 0;
530}
531
532/*
533 * Log leaf entries from the block.
534 */
535static void
536xfs_dir2_block_log_leaf(
537 xfs_trans_t *tp, /* transaction structure */
538 xfs_dabuf_t *bp, /* block buffer */
539 int first, /* index of first logged leaf */
540 int last) /* index of last logged leaf */
541{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200542 xfs_dir2_data_hdr_t *hdr = bp->data;
543 xfs_dir2_leaf_entry_t *blp;
544 xfs_dir2_block_tail_t *btp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200546 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000547 blp = xfs_dir2_block_leaf_p(btp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200548 xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
549 (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552/*
553 * Log the block tail.
554 */
555static void
556xfs_dir2_block_log_tail(
557 xfs_trans_t *tp, /* transaction structure */
558 xfs_dabuf_t *bp) /* block buffer */
559{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200560 xfs_dir2_data_hdr_t *hdr = bp->data;
561 xfs_dir2_block_tail_t *btp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200563 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
564 xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
565 (uint)((char *)(btp + 1) - (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568/*
569 * Look up an entry in the block. This is the external routine,
570 * xfs_dir2_block_lookup_int does the real work.
571 */
572int /* error */
573xfs_dir2_block_lookup(
574 xfs_da_args_t *args) /* dir lookup arguments */
575{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200576 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
578 xfs_dabuf_t *bp; /* block buffer */
579 xfs_dir2_block_tail_t *btp; /* block tail */
580 xfs_dir2_data_entry_t *dep; /* block data entry */
581 xfs_inode_t *dp; /* incore inode */
582 int ent; /* entry index */
583 int error; /* error return value */
584 xfs_mount_t *mp; /* filesystem mount point */
585
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000586 trace_xfs_dir2_block_lookup(args);
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /*
589 * Get the buffer, look up the entry.
590 * If not found (ENOENT) then return, have no buffer.
591 */
592 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
593 return error;
594 dp = args->dp;
595 mp = dp->i_mount;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200596 hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 xfs_dir2_data_check(dp, bp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200598 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000599 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /*
601 * Get the offset from the leaf entry, to point to the data.
602 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200603 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
Barry Naujok384f3ce2008-05-21 16:58:22 +1000604 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +1000606 * Fill in inode number, CI name if appropriate, release the block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000608 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000609 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 xfs_da_brelse(args->trans, bp);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000611 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
614/*
615 * Internal block lookup routine.
616 */
617static int /* error */
618xfs_dir2_block_lookup_int(
619 xfs_da_args_t *args, /* dir lookup arguments */
620 xfs_dabuf_t **bpp, /* returned block buffer */
621 int *entno) /* returned entry number */
622{
623 xfs_dir2_dataptr_t addr; /* data entry address */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200624 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
626 xfs_dabuf_t *bp; /* block buffer */
627 xfs_dir2_block_tail_t *btp; /* block tail */
628 xfs_dir2_data_entry_t *dep; /* block data entry */
629 xfs_inode_t *dp; /* incore inode */
630 int error; /* error return value */
631 xfs_dahash_t hash; /* found hash value */
632 int high; /* binary search high index */
633 int low; /* binary search low index */
634 int mid; /* binary search current idx */
635 xfs_mount_t *mp; /* filesystem mount point */
636 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000637 enum xfs_dacmp cmp; /* comparison result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 dp = args->dp;
640 tp = args->trans;
641 mp = dp->i_mount;
642 /*
643 * Read the buffer, return error if we can't get it.
644 */
645 if ((error =
646 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
647 return error;
648 }
649 ASSERT(bp != NULL);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200650 hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 xfs_dir2_data_check(dp, bp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200652 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000653 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /*
655 * Loop doing a binary search for our hash value.
656 * Find our entry, ENOENT if it's not there.
657 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100658 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 ASSERT(low <= high);
660 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100661 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
663 if (hash < args->hashval)
664 low = mid + 1;
665 else
666 high = mid - 1;
667 if (low > high) {
Barry Naujok6a178102008-05-21 16:42:05 +1000668 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 xfs_da_brelse(tp, bp);
670 return XFS_ERROR(ENOENT);
671 }
672 }
673 /*
674 * Back up to the first one with the right hash value.
675 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100676 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 mid--;
678 }
679 /*
680 * Now loop forward through all the entries with the
681 * right hash value looking for our name.
682 */
683 do {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100684 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 continue;
686 /*
687 * Get pointer to the entry from the leaf.
688 */
689 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200690 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000692 * Compare name and if it's an exact match, return the index
693 * and buffer. If it's the first case-insensitive match, store
694 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 */
Barry Naujok5163f952008-05-21 16:41:01 +1000696 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
697 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
698 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 *bpp = bp;
700 *entno = mid;
Barry Naujok5163f952008-05-21 16:41:01 +1000701 if (cmp == XFS_CMP_EXACT)
702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Barry Naujok5163f952008-05-21 16:41:01 +1000704 } while (++mid < be32_to_cpu(btp->count) &&
705 be32_to_cpu(blp[mid].hashval) == hash);
706
Barry Naujok6a178102008-05-21 16:42:05 +1000707 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000708 /*
709 * Here, we can only be doing a lookup (not a rename or replace).
710 * If a case-insensitive match was found earlier, return success.
711 */
712 if (args->cmpresult == XFS_CMP_CASE)
713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /*
715 * No match, release the buffer and return ENOENT.
716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 xfs_da_brelse(tp, bp);
718 return XFS_ERROR(ENOENT);
719}
720
721/*
722 * Remove an entry from a block format directory.
723 * If that makes the block small enough to fit in shortform, transform it.
724 */
725int /* error */
726xfs_dir2_block_removename(
727 xfs_da_args_t *args) /* directory operation args */
728{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200729 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
731 xfs_dabuf_t *bp; /* block buffer */
732 xfs_dir2_block_tail_t *btp; /* block tail */
733 xfs_dir2_data_entry_t *dep; /* block data entry */
734 xfs_inode_t *dp; /* incore inode */
735 int ent; /* block leaf entry index */
736 int error; /* error return value */
737 xfs_mount_t *mp; /* filesystem mount point */
738 int needlog; /* need to log block header */
739 int needscan; /* need to fixup bestfree */
740 xfs_dir2_sf_hdr_t sfh; /* shortform header */
741 int size; /* shortform size */
742 xfs_trans_t *tp; /* transaction pointer */
743
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000744 trace_xfs_dir2_block_removename(args);
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /*
747 * Look up the entry in the block. Gets the buffer and entry index.
748 * It will always be there, the vnodeops level does a lookup first.
749 */
750 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
751 return error;
752 }
753 dp = args->dp;
754 tp = args->trans;
755 mp = dp->i_mount;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200756 hdr = bp->data;
757 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000758 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /*
760 * Point to the data entry using the leaf entry.
761 */
762 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200763 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /*
765 * Mark the data entry's space free.
766 */
767 needlog = needscan = 0;
768 xfs_dir2_data_make_free(tp, bp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200769 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000770 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /*
772 * Fix up the block tail.
773 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800774 be32_add_cpu(&btp->stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 xfs_dir2_block_log_tail(tp, bp);
776 /*
777 * Remove the leaf entry by marking it stale.
778 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100779 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
781 /*
782 * Fix up bestfree, log the header if necessary.
783 */
784 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200785 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (needlog)
787 xfs_dir2_data_log_header(tp, bp);
788 xfs_dir2_data_check(dp, bp);
789 /*
790 * See if the size as a shortform is good enough.
791 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200792 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
793 if (size > XFS_IFORK_DSIZE(dp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 xfs_da_buf_done(bp);
795 return 0;
796 }
797 /*
798 * If it works, do the conversion.
799 */
800 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
801}
802
803/*
804 * Replace an entry in a V2 block directory.
805 * Change the inode number to the new value.
806 */
807int /* error */
808xfs_dir2_block_replace(
809 xfs_da_args_t *args) /* directory operation args */
810{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200811 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
813 xfs_dabuf_t *bp; /* block buffer */
814 xfs_dir2_block_tail_t *btp; /* block tail */
815 xfs_dir2_data_entry_t *dep; /* block data entry */
816 xfs_inode_t *dp; /* incore inode */
817 int ent; /* leaf entry index */
818 int error; /* error return value */
819 xfs_mount_t *mp; /* filesystem mount point */
820
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000821 trace_xfs_dir2_block_replace(args);
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /*
824 * Lookup the entry in the directory. Get buffer and entry index.
825 * This will always succeed since the caller has already done a lookup.
826 */
827 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
828 return error;
829 }
830 dp = args->dp;
831 mp = dp->i_mount;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200832 hdr = bp->data;
833 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000834 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /*
836 * Point to the data entry we need to change.
837 */
838 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200839 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000840 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /*
842 * Change the inode number to the new value.
843 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000844 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 xfs_dir2_data_log_entry(args->trans, bp, dep);
846 xfs_dir2_data_check(dp, bp);
847 xfs_da_buf_done(bp);
848 return 0;
849}
850
851/*
852 * Qsort comparison routine for the block leaf entries.
853 */
854static int /* sort order */
855xfs_dir2_block_sort(
856 const void *a, /* first leaf entry */
857 const void *b) /* second leaf entry */
858{
859 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
860 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
861
862 la = a;
863 lb = b;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100864 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
865 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868/*
869 * Convert a V2 leaf directory to a V2 block directory if possible.
870 */
871int /* error */
872xfs_dir2_leaf_to_block(
873 xfs_da_args_t *args, /* operation arguments */
874 xfs_dabuf_t *lbp, /* leaf buffer */
875 xfs_dabuf_t *dbp) /* data buffer */
876{
Nathan Scott68b3a102006-03-17 17:27:19 +1100877 __be16 *bestsp; /* leaf bests table */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200878 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 xfs_dir2_block_tail_t *btp; /* block tail */
880 xfs_inode_t *dp; /* incore directory inode */
881 xfs_dir2_data_unused_t *dup; /* unused data entry */
882 int error; /* error return value */
883 int from; /* leaf from index */
884 xfs_dir2_leaf_t *leaf; /* leaf structure */
885 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
886 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
887 xfs_mount_t *mp; /* file system mount point */
888 int needlog; /* need to log data header */
889 int needscan; /* need to scan for bestfree */
890 xfs_dir2_sf_hdr_t sfh; /* shortform header */
891 int size; /* bytes used */
Nathan Scott3d693c62006-03-17 17:28:27 +1100892 __be16 *tagp; /* end of entry (tag) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 int to; /* block/leaf to index */
894 xfs_trans_t *tp; /* transaction pointer */
895
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000896 trace_xfs_dir2_leaf_to_block(args);
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 dp = args->dp;
899 tp = args->trans;
900 mp = dp->i_mount;
901 leaf = lbp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100902 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000903 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /*
905 * If there are data blocks other than the first one, take this
906 * opportunity to remove trailing empty data blocks that may have
907 * been left behind during no-space-reservation operations.
908 * These will show up in the leaf bests table.
909 */
910 while (dp->i_d.di_size > mp->m_dirblksize) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000911 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100912 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200913 mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if ((error =
915 xfs_dir2_leaf_trim_data(args, lbp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100916 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 goto out;
918 } else {
919 error = 0;
920 goto out;
921 }
922 }
923 /*
924 * Read the data block if we don't already have it, give up if it fails.
925 */
926 if (dbp == NULL &&
927 (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
928 XFS_DATA_FORK))) {
929 goto out;
930 }
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200931 hdr = dbp->data;
932 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /*
934 * Size of the "leaf" area in the block.
935 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200936 size = (uint)sizeof(xfs_dir2_block_tail_t) +
Nathan Scotta818e5d2006-03-17 17:28:07 +1100937 (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /*
939 * Look at the last data entry.
940 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200941 tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1;
942 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /*
944 * If it's not free or is too short we can't do it.
945 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100946 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
947 be16_to_cpu(dup->length) < size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 error = 0;
949 goto out;
950 }
951 /*
952 * Start converting it to block form.
953 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200954 hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 needlog = 1;
956 needscan = 0;
957 /*
958 * Use up the space at the end of the block (blp/btp).
959 */
960 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
961 &needlog, &needscan);
962 /*
963 * Initialize the block tail.
964 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200965 btp = xfs_dir2_block_tail_p(mp, hdr);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100966 btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 btp->stale = 0;
968 xfs_dir2_block_log_tail(tp, dbp);
969 /*
970 * Initialize the block leaf area. We compact out stale entries.
971 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000972 lep = xfs_dir2_block_leaf_p(btp);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100973 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100974 if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 continue;
976 lep[to++] = leaf->ents[from];
977 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100978 ASSERT(to == be32_to_cpu(btp->count));
979 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /*
981 * Scan the bestfree if we need it and log the data block header.
982 */
983 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200984 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (needlog)
986 xfs_dir2_data_log_header(tp, dbp);
987 /*
988 * Pitch the old leaf block.
989 */
990 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
991 lbp = NULL;
992 if (error) {
993 goto out;
994 }
995 /*
996 * Now see if the resulting block can be shrunken to shortform.
997 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200998 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
999 if (size > XFS_IFORK_DSIZE(dp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 error = 0;
1001 goto out;
1002 }
1003 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
1004out:
1005 if (lbp)
1006 xfs_da_buf_done(lbp);
1007 if (dbp)
1008 xfs_da_buf_done(dbp);
1009 return error;
1010}
1011
1012/*
1013 * Convert the shortform directory to block form.
1014 */
1015int /* error */
1016xfs_dir2_sf_to_block(
1017 xfs_da_args_t *args) /* operation arguments */
1018{
1019 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001020 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1022 xfs_dabuf_t *bp; /* block buffer */
1023 xfs_dir2_block_tail_t *btp; /* block tail pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1025 xfs_inode_t *dp; /* incore directory inode */
1026 int dummy; /* trash */
1027 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1028 int endoffset; /* end of data objects */
1029 int error; /* error return value */
1030 int i; /* index */
1031 xfs_mount_t *mp; /* filesystem mount point */
1032 int needlog; /* need to log block header */
1033 int needscan; /* need to scan block freespc */
1034 int newoffset; /* offset from current entry */
1035 int offset; /* target block offset */
1036 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001037 xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
1038 xfs_dir2_sf_hdr_t *sfp; /* shortform header */
Nathan Scott3d693c62006-03-17 17:28:27 +11001039 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +10001041 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001043 trace_xfs_dir2_sf_to_block(args);
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 dp = args->dp;
1046 tp = args->trans;
1047 mp = dp->i_mount;
1048 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1049 /*
1050 * Bomb out if the shortform directory is way too short.
1051 */
1052 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1053 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1054 return XFS_ERROR(EIO);
1055 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001056
1057 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1060 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001061 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /*
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001064 * Copy the directory into a temporary buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 * Then pitch the incore inode data so we can make extents.
1066 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001067 sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1068 memcpy(sfp, oldsfp, dp->i_df.if_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001070 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 dp->i_d.di_size = 0;
1072 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /*
1075 * Add block 0 to the inode.
1076 */
1077 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1078 if (error) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001079 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return error;
1081 }
1082 /*
1083 * Initialize the data block.
1084 */
1085 error = xfs_dir2_data_init(args, blkno, &bp);
1086 if (error) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001087 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return error;
1089 }
Christoph Hellwiga64b0412011-07-08 14:35:32 +02001090 hdr = bp->data;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001091 hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /*
1093 * Compute size of block "tail" area.
1094 */
1095 i = (uint)sizeof(*btp) +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001096 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 /*
1098 * The whole thing is initialized to free by the init routine.
1099 * Say we're using the leaf and tail area.
1100 */
Christoph Hellwiga64b0412011-07-08 14:35:32 +02001101 dup = (xfs_dir2_data_unused_t *)(hdr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 needlog = needscan = 0;
1103 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1104 &needscan);
1105 ASSERT(needscan == 0);
1106 /*
1107 * Fill in the tail.
1108 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001109 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001110 btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 btp->stale = 0;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001112 blp = xfs_dir2_block_leaf_p(btp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001113 endoffset = (uint)((char *)blp - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 /*
1115 * Remove the freespace, we'll manage it.
1116 */
1117 xfs_dir2_data_use_free(tp, bp, dup,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001118 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
Nathan Scottad354eb2006-03-17 17:27:37 +11001119 be16_to_cpu(dup->length), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /*
1121 * Create entry for .
1122 */
1123 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001124 ((char *)hdr + XFS_DIR2_DATA_DOT_OFFSET);
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001125 dep->inumber = cpu_to_be64(dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 dep->namelen = 1;
1127 dep->name[0] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001128 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001129 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001131 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001132 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001133 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /*
1135 * Create entry for ..
1136 */
1137 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001138 ((char *)hdr + XFS_DIR2_DATA_DOTDOT_OFFSET);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001139 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 dep->namelen = 2;
1141 dep->name[0] = dep->name[1] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001142 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001143 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001145 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001146 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001147 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 offset = XFS_DIR2_DATA_FIRST_OFFSET;
1149 /*
1150 * Loop over existing entries, stuff them in.
1151 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001152 i = 0;
1153 if (!sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 sfep = NULL;
1155 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001156 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /*
1158 * Need to preserve the existing offset values in the sf directory.
1159 * Insert holes (unused entries) where necessary.
1160 */
1161 while (offset < endoffset) {
1162 /*
1163 * sfep is null when we reach the end of the list.
1164 */
1165 if (sfep == NULL)
1166 newoffset = endoffset;
1167 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001168 newoffset = xfs_dir2_sf_get_offset(sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /*
1170 * There should be a hole here, make one.
1171 */
1172 if (offset < newoffset) {
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001173 dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +11001174 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1175 dup->length = cpu_to_be16(newoffset - offset);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001176 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001177 ((char *)dup - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 xfs_dir2_data_log_unused(tp, bp, dup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001179 xfs_dir2_data_freeinsert(hdr, dup, &dummy);
Nathan Scottad354eb2006-03-17 17:27:37 +11001180 offset += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 continue;
1182 }
1183 /*
1184 * Copy a real entry.
1185 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001186 dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001187 dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 dep->namelen = sfep->namelen;
1189 memcpy(dep->name, sfep->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001190 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001191 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 xfs_dir2_data_log_entry(tp, bp, dep);
Barry Naujok5163f952008-05-21 16:41:01 +10001193 name.name = sfep->name;
1194 name.len = sfep->namelen;
1195 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1196 hashname(&name));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001197 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001198 (char *)dep - (char *)hdr));
1199 offset = (int)((char *)(tagp + 1) - (char *)hdr);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001200 if (++i == sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 sfep = NULL;
1202 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001203 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205 /* Done with the temporary buffer */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001206 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 /*
1208 * Sort the leaf entries by hash value.
1209 */
Nathan Scotte922fff2006-03-17 17:27:56 +11001210 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /*
1212 * Log the leaf entry area and tail.
1213 * Already logged the header in data_init, ignore needlog.
1214 */
1215 ASSERT(needscan == 0);
Nathan Scotte922fff2006-03-17 17:27:56 +11001216 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 xfs_dir2_block_log_tail(tp, bp);
1218 xfs_dir2_data_check(dp, bp);
1219 xfs_da_buf_done(bp);
1220 return 0;
1221}