blob: 70fb0cb6bd6983ac3c8c5b387d78c9288d21a00b [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 Hellwig69ef9212011-07-08 14:36:05 +0200112 if (unlikely(hdr->magic != cpu_to_be32(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--) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200258 if (blp[fromidx].address ==
259 cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (highstale == -1)
261 highstale = toidx;
262 else {
263 if (lfloghigh == -1)
264 lfloghigh = toidx;
265 continue;
266 }
267 }
268 if (fromidx < toidx)
269 blp[toidx] = blp[fromidx];
270 toidx--;
271 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100272 lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
273 lfloghigh -= be32_to_cpu(btp->stale) - 1;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800274 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 xfs_dir2_data_make_free(tp, bp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200276 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
Nathan Scotte922fff2006-03-17 17:27:56 +1100277 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 &needlog, &needscan);
Nathan Scotte922fff2006-03-17 17:27:56 +1100279 blp += be32_to_cpu(btp->stale) - 1;
280 btp->stale = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /*
282 * If we now need to rebuild the bestfree map, do so.
283 * This needs to happen before the next call to use_free.
284 */
285 if (needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200286 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 needscan = 0;
288 }
289 }
290 /*
291 * Set leaf logging boundaries to impossible state.
292 * For the no-stale case they're set explicitly.
293 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100294 else if (btp->stale) {
295 lfloglow = be32_to_cpu(btp->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 lfloghigh = -1;
297 }
298 /*
299 * Find the slot that's first lower than our hash value, -1 if none.
300 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100301 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100303 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 break;
305 if (hash < args->hashval)
306 low = mid + 1;
307 else
308 high = mid - 1;
309 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100310 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 mid--;
312 }
313 /*
314 * No stale entries, will use enddup space to hold new leaf.
315 */
316 if (!btp->stale) {
317 /*
318 * Mark the space needed for the new leaf entry, now in use.
319 */
320 xfs_dir2_data_use_free(tp, bp, enddup,
321 (xfs_dir2_data_aoff_t)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200322 ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 sizeof(*blp)),
324 (xfs_dir2_data_aoff_t)sizeof(*blp),
325 &needlog, &needscan);
326 /*
327 * Update the tail (entry count).
328 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800329 be32_add_cpu(&btp->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /*
331 * If we now need to rebuild the bestfree map, do so.
332 * This needs to happen before the next call to use_free.
333 */
334 if (needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200335 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 needscan = 0;
337 }
338 /*
339 * Adjust pointer to the first leaf entry, we're about to move
340 * the table up one to open up space for the new leaf entry.
341 * Then adjust our index to match.
342 */
343 blp--;
344 mid++;
345 if (mid)
346 memmove(blp, &blp[1], mid * sizeof(*blp));
347 lfloglow = 0;
348 lfloghigh = mid;
349 }
350 /*
351 * Use a stale leaf for our new entry.
352 */
353 else {
354 for (lowstale = mid;
355 lowstale >= 0 &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200356 blp[lowstale].address !=
357 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 lowstale--)
359 continue;
360 for (highstale = mid + 1;
Nathan Scotte922fff2006-03-17 17:27:56 +1100361 highstale < be32_to_cpu(btp->count) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200362 blp[highstale].address !=
363 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 (lowstale < 0 || mid - lowstale > highstale - mid);
365 highstale++)
366 continue;
367 /*
368 * Move entries toward the low-numbered stale entry.
369 */
370 if (lowstale >= 0 &&
Nathan Scotte922fff2006-03-17 17:27:56 +1100371 (highstale == be32_to_cpu(btp->count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 mid - lowstale <= highstale - mid)) {
373 if (mid - lowstale)
374 memmove(&blp[lowstale], &blp[lowstale + 1],
375 (mid - lowstale) * sizeof(*blp));
376 lfloglow = MIN(lowstale, lfloglow);
377 lfloghigh = MAX(mid, lfloghigh);
378 }
379 /*
380 * Move entries toward the high-numbered stale entry.
381 */
382 else {
Nathan Scotte922fff2006-03-17 17:27:56 +1100383 ASSERT(highstale < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 mid++;
385 if (highstale - mid)
386 memmove(&blp[mid + 1], &blp[mid],
387 (highstale - mid) * sizeof(*blp));
388 lfloglow = MIN(mid, lfloglow);
389 lfloghigh = MAX(highstale, lfloghigh);
390 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800391 be32_add_cpu(&btp->stale, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393 /*
394 * Point to the new data entry.
395 */
396 dep = (xfs_dir2_data_entry_t *)dup;
397 /*
398 * Fill in the leaf entry.
399 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100400 blp[mid].hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000401 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200402 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
404 /*
405 * Mark space for the data entry used.
406 */
407 xfs_dir2_data_use_free(tp, bp, dup,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200408 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
410 /*
411 * Create the new data entry.
412 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000413 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 dep->namelen = args->namelen;
415 memcpy(dep->name, args->name, args->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000416 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200417 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /*
419 * Clean up the bestfree array and log the header, tail, and entry.
420 */
421 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200422 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (needlog)
424 xfs_dir2_data_log_header(tp, bp);
425 xfs_dir2_block_log_tail(tp, bp);
426 xfs_dir2_data_log_entry(tp, bp, dep);
427 xfs_dir2_data_check(dp, bp);
428 xfs_da_buf_done(bp);
429 return 0;
430}
431
432/*
433 * Readdir for block directories.
434 */
435int /* error */
436xfs_dir2_block_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 xfs_inode_t *dp, /* incore inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000438 void *dirent,
439 xfs_off_t *offset,
440 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200442 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 xfs_dabuf_t *bp; /* buffer for block */
444 xfs_dir2_block_tail_t *btp; /* block tail */
445 xfs_dir2_data_entry_t *dep; /* block data entry */
446 xfs_dir2_data_unused_t *dup; /* block unused entry */
447 char *endptr; /* end of the data entries */
448 int error; /* error return value */
449 xfs_mount_t *mp; /* filesystem mount point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 char *ptr; /* current data entry */
451 int wantoff; /* starting block offset */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000452 xfs_off_t cook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 mp = dp->i_mount;
455 /*
456 * If the block number in the offset is out of range, we're done.
457 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000458 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 0;
460 }
461 /*
462 * Can't read the block, give up, else get dabuf in bp.
463 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000464 error = xfs_da_read_buf(NULL, dp, mp->m_dirdatablk, -1,
465 &bp, XFS_DATA_FORK);
466 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return error;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 ASSERT(bp != NULL);
470 /*
471 * Extract the byte offset we start at from the seek pointer.
472 * We'll skip entries before this.
473 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000474 wantoff = xfs_dir2_dataptr_to_off(mp, *offset);
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200475 hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 xfs_dir2_data_check(dp, bp);
477 /*
478 * Set up values for the loop.
479 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200480 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwiga64b0412011-07-08 14:35:32 +0200481 ptr = (char *)(hdr + 1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000482 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /*
485 * Loop over the data portion of the block.
486 * Each object is a real entry (dep) or an unused one (dup).
487 */
488 while (ptr < endptr) {
489 dup = (xfs_dir2_data_unused_t *)ptr;
490 /*
491 * Unused, skip it.
492 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100493 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
494 ptr += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 continue;
496 }
497
498 dep = (xfs_dir2_data_entry_t *)ptr;
499
500 /*
501 * Bump pointer for the next iteration.
502 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000503 ptr += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /*
505 * The entry is before the desired starting point, skip it.
506 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200507 if ((char *)dep - (char *)hdr < wantoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000510 cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200511 (char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /*
514 * If it didn't fit, set the final offset to here & return.
515 */
Dave Chinner4a24cb72010-01-20 10:48:05 +1100516 if (filldir(dirent, (char *)dep->name, dep->namelen,
517 cook & 0x7fffffff, be64_to_cpu(dep->inumber),
518 DT_UNKNOWN)) {
Christoph Hellwig15440312009-01-08 14:00:00 -0500519 *offset = cook & 0x7fffffff;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000520 xfs_da_brelse(NULL, bp);
521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523 }
524
525 /*
526 * Reached the end of the block.
Nathan Scottc41564b2006-03-29 08:55:14 +1000527 * Set the offset to a non-existent block 1 and return.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
Christoph Hellwig15440312009-01-08 14:00:00 -0500529 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
530 0x7fffffff;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +1000531 xfs_da_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return 0;
533}
534
535/*
536 * Log leaf entries from the block.
537 */
538static void
539xfs_dir2_block_log_leaf(
540 xfs_trans_t *tp, /* transaction structure */
541 xfs_dabuf_t *bp, /* block buffer */
542 int first, /* index of first logged leaf */
543 int last) /* index of last logged leaf */
544{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200545 xfs_dir2_data_hdr_t *hdr = bp->data;
546 xfs_dir2_leaf_entry_t *blp;
547 xfs_dir2_block_tail_t *btp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200549 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000550 blp = xfs_dir2_block_leaf_p(btp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200551 xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
552 (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/*
556 * Log the block tail.
557 */
558static void
559xfs_dir2_block_log_tail(
560 xfs_trans_t *tp, /* transaction structure */
561 xfs_dabuf_t *bp) /* block buffer */
562{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200563 xfs_dir2_data_hdr_t *hdr = bp->data;
564 xfs_dir2_block_tail_t *btp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200566 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
567 xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
568 (uint)((char *)(btp + 1) - (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571/*
572 * Look up an entry in the block. This is the external routine,
573 * xfs_dir2_block_lookup_int does the real work.
574 */
575int /* error */
576xfs_dir2_block_lookup(
577 xfs_da_args_t *args) /* dir lookup arguments */
578{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200579 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
581 xfs_dabuf_t *bp; /* block buffer */
582 xfs_dir2_block_tail_t *btp; /* block tail */
583 xfs_dir2_data_entry_t *dep; /* block data entry */
584 xfs_inode_t *dp; /* incore inode */
585 int ent; /* entry index */
586 int error; /* error return value */
587 xfs_mount_t *mp; /* filesystem mount point */
588
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000589 trace_xfs_dir2_block_lookup(args);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /*
592 * Get the buffer, look up the entry.
593 * If not found (ENOENT) then return, have no buffer.
594 */
595 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
596 return error;
597 dp = args->dp;
598 mp = dp->i_mount;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200599 hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 xfs_dir2_data_check(dp, bp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200601 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000602 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /*
604 * Get the offset from the leaf entry, to point to the data.
605 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200606 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
Barry Naujok384f3ce2008-05-21 16:58:22 +1000607 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +1000609 * Fill in inode number, CI name if appropriate, release the block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000611 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000612 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 xfs_da_brelse(args->trans, bp);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000614 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617/*
618 * Internal block lookup routine.
619 */
620static int /* error */
621xfs_dir2_block_lookup_int(
622 xfs_da_args_t *args, /* dir lookup arguments */
623 xfs_dabuf_t **bpp, /* returned block buffer */
624 int *entno) /* returned entry number */
625{
626 xfs_dir2_dataptr_t addr; /* data entry address */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200627 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
629 xfs_dabuf_t *bp; /* block buffer */
630 xfs_dir2_block_tail_t *btp; /* block tail */
631 xfs_dir2_data_entry_t *dep; /* block data entry */
632 xfs_inode_t *dp; /* incore inode */
633 int error; /* error return value */
634 xfs_dahash_t hash; /* found hash value */
635 int high; /* binary search high index */
636 int low; /* binary search low index */
637 int mid; /* binary search current idx */
638 xfs_mount_t *mp; /* filesystem mount point */
639 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000640 enum xfs_dacmp cmp; /* comparison result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 dp = args->dp;
643 tp = args->trans;
644 mp = dp->i_mount;
645 /*
646 * Read the buffer, return error if we can't get it.
647 */
648 if ((error =
649 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
650 return error;
651 }
652 ASSERT(bp != NULL);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200653 hdr = bp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 xfs_dir2_data_check(dp, bp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200655 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000656 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
658 * Loop doing a binary search for our hash value.
659 * Find our entry, ENOENT if it's not there.
660 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100661 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 ASSERT(low <= high);
663 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100664 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 break;
666 if (hash < args->hashval)
667 low = mid + 1;
668 else
669 high = mid - 1;
670 if (low > high) {
Barry Naujok6a178102008-05-21 16:42:05 +1000671 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 xfs_da_brelse(tp, bp);
673 return XFS_ERROR(ENOENT);
674 }
675 }
676 /*
677 * Back up to the first one with the right hash value.
678 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100679 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 mid--;
681 }
682 /*
683 * Now loop forward through all the entries with the
684 * right hash value looking for our name.
685 */
686 do {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100687 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 continue;
689 /*
690 * Get pointer to the entry from the leaf.
691 */
692 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200693 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000695 * Compare name and if it's an exact match, return the index
696 * and buffer. If it's the first case-insensitive match, store
697 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 */
Barry Naujok5163f952008-05-21 16:41:01 +1000699 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
700 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
701 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *bpp = bp;
703 *entno = mid;
Barry Naujok5163f952008-05-21 16:41:01 +1000704 if (cmp == XFS_CMP_EXACT)
705 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Barry Naujok5163f952008-05-21 16:41:01 +1000707 } while (++mid < be32_to_cpu(btp->count) &&
708 be32_to_cpu(blp[mid].hashval) == hash);
709
Barry Naujok6a178102008-05-21 16:42:05 +1000710 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000711 /*
712 * Here, we can only be doing a lookup (not a rename or replace).
713 * If a case-insensitive match was found earlier, return success.
714 */
715 if (args->cmpresult == XFS_CMP_CASE)
716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /*
718 * No match, release the buffer and return ENOENT.
719 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 xfs_da_brelse(tp, bp);
721 return XFS_ERROR(ENOENT);
722}
723
724/*
725 * Remove an entry from a block format directory.
726 * If that makes the block small enough to fit in shortform, transform it.
727 */
728int /* error */
729xfs_dir2_block_removename(
730 xfs_da_args_t *args) /* directory operation args */
731{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200732 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
734 xfs_dabuf_t *bp; /* block buffer */
735 xfs_dir2_block_tail_t *btp; /* block tail */
736 xfs_dir2_data_entry_t *dep; /* block data entry */
737 xfs_inode_t *dp; /* incore inode */
738 int ent; /* block leaf entry index */
739 int error; /* error return value */
740 xfs_mount_t *mp; /* filesystem mount point */
741 int needlog; /* need to log block header */
742 int needscan; /* need to fixup bestfree */
743 xfs_dir2_sf_hdr_t sfh; /* shortform header */
744 int size; /* shortform size */
745 xfs_trans_t *tp; /* transaction pointer */
746
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000747 trace_xfs_dir2_block_removename(args);
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /*
750 * Look up the entry in the block. Gets the buffer and entry index.
751 * It will always be there, the vnodeops level does a lookup first.
752 */
753 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
754 return error;
755 }
756 dp = args->dp;
757 tp = args->trans;
758 mp = dp->i_mount;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200759 hdr = bp->data;
760 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000761 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /*
763 * Point to the data entry using the leaf entry.
764 */
765 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200766 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /*
768 * Mark the data entry's space free.
769 */
770 needlog = needscan = 0;
771 xfs_dir2_data_make_free(tp, bp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200772 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000773 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /*
775 * Fix up the block tail.
776 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800777 be32_add_cpu(&btp->stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 xfs_dir2_block_log_tail(tp, bp);
779 /*
780 * Remove the leaf entry by marking it stale.
781 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100782 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
784 /*
785 * Fix up bestfree, log the header if necessary.
786 */
787 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200788 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (needlog)
790 xfs_dir2_data_log_header(tp, bp);
791 xfs_dir2_data_check(dp, bp);
792 /*
793 * See if the size as a shortform is good enough.
794 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200795 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
796 if (size > XFS_IFORK_DSIZE(dp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 xfs_da_buf_done(bp);
798 return 0;
799 }
800 /*
801 * If it works, do the conversion.
802 */
803 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
804}
805
806/*
807 * Replace an entry in a V2 block directory.
808 * Change the inode number to the new value.
809 */
810int /* error */
811xfs_dir2_block_replace(
812 xfs_da_args_t *args) /* directory operation args */
813{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200814 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
816 xfs_dabuf_t *bp; /* block buffer */
817 xfs_dir2_block_tail_t *btp; /* block tail */
818 xfs_dir2_data_entry_t *dep; /* block data entry */
819 xfs_inode_t *dp; /* incore inode */
820 int ent; /* leaf entry index */
821 int error; /* error return value */
822 xfs_mount_t *mp; /* filesystem mount point */
823
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000824 trace_xfs_dir2_block_replace(args);
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /*
827 * Lookup the entry in the directory. Get buffer and entry index.
828 * This will always succeed since the caller has already done a lookup.
829 */
830 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
831 return error;
832 }
833 dp = args->dp;
834 mp = dp->i_mount;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200835 hdr = bp->data;
836 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000837 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /*
839 * Point to the data entry we need to change.
840 */
841 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200842 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000843 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /*
845 * Change the inode number to the new value.
846 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000847 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 xfs_dir2_data_log_entry(args->trans, bp, dep);
849 xfs_dir2_data_check(dp, bp);
850 xfs_da_buf_done(bp);
851 return 0;
852}
853
854/*
855 * Qsort comparison routine for the block leaf entries.
856 */
857static int /* sort order */
858xfs_dir2_block_sort(
859 const void *a, /* first leaf entry */
860 const void *b) /* second leaf entry */
861{
862 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
863 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
864
865 la = a;
866 lb = b;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100867 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
868 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871/*
872 * Convert a V2 leaf directory to a V2 block directory if possible.
873 */
874int /* error */
875xfs_dir2_leaf_to_block(
876 xfs_da_args_t *args, /* operation arguments */
877 xfs_dabuf_t *lbp, /* leaf buffer */
878 xfs_dabuf_t *dbp) /* data buffer */
879{
Nathan Scott68b3a102006-03-17 17:27:19 +1100880 __be16 *bestsp; /* leaf bests table */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200881 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 xfs_dir2_block_tail_t *btp; /* block tail */
883 xfs_inode_t *dp; /* incore directory inode */
884 xfs_dir2_data_unused_t *dup; /* unused data entry */
885 int error; /* error return value */
886 int from; /* leaf from index */
887 xfs_dir2_leaf_t *leaf; /* leaf structure */
888 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
889 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
890 xfs_mount_t *mp; /* file system mount point */
891 int needlog; /* need to log data header */
892 int needscan; /* need to scan for bestfree */
893 xfs_dir2_sf_hdr_t sfh; /* shortform header */
894 int size; /* bytes used */
Nathan Scott3d693c62006-03-17 17:28:27 +1100895 __be16 *tagp; /* end of entry (tag) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 int to; /* block/leaf to index */
897 xfs_trans_t *tp; /* transaction pointer */
898
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000899 trace_xfs_dir2_leaf_to_block(args);
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 dp = args->dp;
902 tp = args->trans;
903 mp = dp->i_mount;
904 leaf = lbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200905 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000906 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 /*
908 * If there are data blocks other than the first one, take this
909 * opportunity to remove trailing empty data blocks that may have
910 * been left behind during no-space-reservation operations.
911 * These will show up in the leaf bests table.
912 */
913 while (dp->i_d.di_size > mp->m_dirblksize) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000914 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100915 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200916 mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if ((error =
918 xfs_dir2_leaf_trim_data(args, lbp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100919 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 goto out;
921 } else {
922 error = 0;
923 goto out;
924 }
925 }
926 /*
927 * Read the data block if we don't already have it, give up if it fails.
928 */
929 if (dbp == NULL &&
930 (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
931 XFS_DATA_FORK))) {
932 goto out;
933 }
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200934 hdr = dbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200935 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 /*
937 * Size of the "leaf" area in the block.
938 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200939 size = (uint)sizeof(xfs_dir2_block_tail_t) +
Nathan Scotta818e5d2006-03-17 17:28:07 +1100940 (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /*
942 * Look at the last data entry.
943 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200944 tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1;
945 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /*
947 * If it's not free or is too short we can't do it.
948 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100949 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
950 be16_to_cpu(dup->length) < size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 error = 0;
952 goto out;
953 }
954 /*
955 * Start converting it to block form.
956 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200957 hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 needlog = 1;
959 needscan = 0;
960 /*
961 * Use up the space at the end of the block (blp/btp).
962 */
963 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
964 &needlog, &needscan);
965 /*
966 * Initialize the block tail.
967 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200968 btp = xfs_dir2_block_tail_p(mp, hdr);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100969 btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 btp->stale = 0;
971 xfs_dir2_block_log_tail(tp, dbp);
972 /*
973 * Initialize the block leaf area. We compact out stale entries.
974 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000975 lep = xfs_dir2_block_leaf_p(btp);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100976 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200977 if (leaf->ents[from].address ==
978 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 continue;
980 lep[to++] = leaf->ents[from];
981 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100982 ASSERT(to == be32_to_cpu(btp->count));
983 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /*
985 * Scan the bestfree if we need it and log the data block header.
986 */
987 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200988 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (needlog)
990 xfs_dir2_data_log_header(tp, dbp);
991 /*
992 * Pitch the old leaf block.
993 */
994 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
995 lbp = NULL;
996 if (error) {
997 goto out;
998 }
999 /*
1000 * Now see if the resulting block can be shrunken to shortform.
1001 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001002 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
1003 if (size > XFS_IFORK_DSIZE(dp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 error = 0;
1005 goto out;
1006 }
1007 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
1008out:
1009 if (lbp)
1010 xfs_da_buf_done(lbp);
1011 if (dbp)
1012 xfs_da_buf_done(dbp);
1013 return error;
1014}
1015
1016/*
1017 * Convert the shortform directory to block form.
1018 */
1019int /* error */
1020xfs_dir2_sf_to_block(
1021 xfs_da_args_t *args) /* operation arguments */
1022{
1023 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001024 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1026 xfs_dabuf_t *bp; /* block buffer */
1027 xfs_dir2_block_tail_t *btp; /* block tail pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1029 xfs_inode_t *dp; /* incore directory inode */
1030 int dummy; /* trash */
1031 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1032 int endoffset; /* end of data objects */
1033 int error; /* error return value */
1034 int i; /* index */
1035 xfs_mount_t *mp; /* filesystem mount point */
1036 int needlog; /* need to log block header */
1037 int needscan; /* need to scan block freespc */
1038 int newoffset; /* offset from current entry */
1039 int offset; /* target block offset */
1040 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001041 xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
1042 xfs_dir2_sf_hdr_t *sfp; /* shortform header */
Nathan Scott3d693c62006-03-17 17:28:27 +11001043 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +10001045 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001047 trace_xfs_dir2_sf_to_block(args);
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 dp = args->dp;
1050 tp = args->trans;
1051 mp = dp->i_mount;
1052 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1053 /*
1054 * Bomb out if the shortform directory is way too short.
1055 */
1056 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1057 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1058 return XFS_ERROR(EIO);
1059 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001060
1061 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1064 ASSERT(dp->i_df.if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001065 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /*
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001068 * Copy the directory into a temporary buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 * Then pitch the incore inode data so we can make extents.
1070 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001071 sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1072 memcpy(sfp, oldsfp, dp->i_df.if_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001074 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 dp->i_d.di_size = 0;
1076 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 /*
1079 * Add block 0 to the inode.
1080 */
1081 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1082 if (error) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001083 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return error;
1085 }
1086 /*
1087 * Initialize the data block.
1088 */
1089 error = xfs_dir2_data_init(args, blkno, &bp);
1090 if (error) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001091 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return error;
1093 }
Christoph Hellwiga64b0412011-07-08 14:35:32 +02001094 hdr = bp->data;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001095 hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 /*
1097 * Compute size of block "tail" area.
1098 */
1099 i = (uint)sizeof(*btp) +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001100 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /*
1102 * The whole thing is initialized to free by the init routine.
1103 * Say we're using the leaf and tail area.
1104 */
Christoph Hellwiga64b0412011-07-08 14:35:32 +02001105 dup = (xfs_dir2_data_unused_t *)(hdr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 needlog = needscan = 0;
1107 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1108 &needscan);
1109 ASSERT(needscan == 0);
1110 /*
1111 * Fill in the tail.
1112 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001113 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001114 btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 btp->stale = 0;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001116 blp = xfs_dir2_block_leaf_p(btp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001117 endoffset = (uint)((char *)blp - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /*
1119 * Remove the freespace, we'll manage it.
1120 */
1121 xfs_dir2_data_use_free(tp, bp, dup,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001122 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
Nathan Scottad354eb2006-03-17 17:27:37 +11001123 be16_to_cpu(dup->length), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /*
1125 * Create entry for .
1126 */
1127 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001128 ((char *)hdr + XFS_DIR2_DATA_DOT_OFFSET);
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001129 dep->inumber = cpu_to_be64(dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 dep->namelen = 1;
1131 dep->name[0] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001132 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001133 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001135 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001136 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001137 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /*
1139 * Create entry for ..
1140 */
1141 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001142 ((char *)hdr + XFS_DIR2_DATA_DOTDOT_OFFSET);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001143 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 dep->namelen = 2;
1145 dep->name[0] = dep->name[1] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001146 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001147 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001149 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001150 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001151 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 offset = XFS_DIR2_DATA_FIRST_OFFSET;
1153 /*
1154 * Loop over existing entries, stuff them in.
1155 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001156 i = 0;
1157 if (!sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 sfep = NULL;
1159 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001160 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /*
1162 * Need to preserve the existing offset values in the sf directory.
1163 * Insert holes (unused entries) where necessary.
1164 */
1165 while (offset < endoffset) {
1166 /*
1167 * sfep is null when we reach the end of the list.
1168 */
1169 if (sfep == NULL)
1170 newoffset = endoffset;
1171 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001172 newoffset = xfs_dir2_sf_get_offset(sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /*
1174 * There should be a hole here, make one.
1175 */
1176 if (offset < newoffset) {
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001177 dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +11001178 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1179 dup->length = cpu_to_be16(newoffset - offset);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001180 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001181 ((char *)dup - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 xfs_dir2_data_log_unused(tp, bp, dup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001183 xfs_dir2_data_freeinsert(hdr, dup, &dummy);
Nathan Scottad354eb2006-03-17 17:27:37 +11001184 offset += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 continue;
1186 }
1187 /*
1188 * Copy a real entry.
1189 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001190 dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
Christoph Hellwig8bc38782011-07-08 14:35:03 +02001191 dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 dep->namelen = sfep->namelen;
1193 memcpy(dep->name, sfep->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001194 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001195 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 xfs_dir2_data_log_entry(tp, bp, dep);
Barry Naujok5163f952008-05-21 16:41:01 +10001197 name.name = sfep->name;
1198 name.len = sfep->namelen;
1199 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1200 hashname(&name));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001201 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001202 (char *)dep - (char *)hdr));
1203 offset = (int)((char *)(tagp + 1) - (char *)hdr);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001204 if (++i == sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 sfep = NULL;
1206 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001207 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209 /* Done with the temporary buffer */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001210 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /*
1212 * Sort the leaf entries by hash value.
1213 */
Nathan Scotte922fff2006-03-17 17:27:56 +11001214 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /*
1216 * Log the leaf entry area and tail.
1217 * Already logged the header in data_init, ignore needlog.
1218 */
1219 ASSERT(needscan == 0);
Nathan Scotte922fff2006-03-17 17:27:56 +11001220 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 xfs_dir2_block_log_tail(tp, bp);
1222 xfs_dir2_data_check(dp, bp);
1223 xfs_da_buf_done(bp);
1224 return 0;
1225}