blob: 779a267b0a84c55a83764d25d469493b24ca0a7f [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"
27#include "xfs_dmapi.h"
28#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dir2_data.h"
37#include "xfs_dir2_leaf.h"
38#include "xfs_dir2_block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000040#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * Local function prototypes.
44 */
45static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, xfs_dabuf_t *bp, int first,
46 int last);
47static void xfs_dir2_block_log_tail(xfs_trans_t *tp, xfs_dabuf_t *bp);
48static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **bpp,
49 int *entno);
50static int xfs_dir2_block_sort(const void *a, const void *b);
51
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100052static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
53
54/*
55 * One-time startup routine called from xfs_init().
56 */
57void
58xfs_dir_startup(void)
59{
Dave Chinner4a24cb72010-01-20 10:48:05 +110060 xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1);
61 xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100062}
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * Add an entry to a block directory.
66 */
67int /* error */
68xfs_dir2_block_addname(
69 xfs_da_args_t *args) /* directory op arguments */
70{
71 xfs_dir2_data_free_t *bf; /* bestfree table in block */
72 xfs_dir2_block_t *block; /* directory block structure */
73 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
74 xfs_dabuf_t *bp; /* buffer for block */
75 xfs_dir2_block_tail_t *btp; /* block tail */
76 int compact; /* need to compact leaf ents */
77 xfs_dir2_data_entry_t *dep; /* block data entry */
78 xfs_inode_t *dp; /* directory inode */
79 xfs_dir2_data_unused_t *dup; /* block unused entry */
80 int error; /* error return value */
81 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
82 xfs_dahash_t hash; /* hash value of found entry */
83 int high; /* high index for binary srch */
84 int highstale; /* high stale index */
85 int lfloghigh=0; /* last final leaf to log */
86 int lfloglow=0; /* first final leaf to log */
87 int len; /* length of the new entry */
88 int low; /* low index for binary srch */
89 int lowstale; /* low stale index */
90 int mid=0; /* midpoint for binary srch */
91 xfs_mount_t *mp; /* filesystem mount point */
92 int needlog; /* need to log header */
93 int needscan; /* need to rescan freespace */
Nathan Scott3d693c62006-03-17 17:28:27 +110094 __be16 *tagp; /* pointer to tag value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 xfs_trans_t *tp; /* transaction structure */
96
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000097 trace_xfs_dir2_block_addname(args);
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 dp = args->dp;
100 tp = args->trans;
101 mp = dp->i_mount;
102 /*
103 * Read the (one and only) directory block into dabuf bp.
104 */
105 if ((error =
106 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
107 return error;
108 }
109 ASSERT(bp != NULL);
110 block = bp->data;
111 /*
112 * Check the magic number, corrupted if wrong.
113 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100114 if (unlikely(be32_to_cpu(block->hdr.magic) != XFS_DIR2_BLOCK_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 XFS_CORRUPTION_ERROR("xfs_dir2_block_addname",
116 XFS_ERRLEVEL_LOW, mp, block);
117 xfs_da_brelse(tp, bp);
118 return XFS_ERROR(EFSCORRUPTED);
119 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000120 len = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 /*
122 * Set up pointers to parts of the block.
123 */
124 bf = block->hdr.bestfree;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000125 btp = xfs_dir2_block_tail_p(mp, block);
126 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /*
128 * No stale entries? Need space for entry and new leaf.
129 */
130 if (!btp->stale) {
131 /*
132 * Tag just before the first leaf entry.
133 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100134 tagp = (__be16 *)blp - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /*
136 * Data object just before the first leaf entry.
137 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100138 enddup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /*
140 * If it's not free then can't do this add without cleaning up:
141 * the space before the first leaf entry needs to be free so it
142 * can be expanded to hold the pointer to the new entry.
143 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100144 if (be16_to_cpu(enddup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 dup = enddup = NULL;
146 /*
147 * Check out the biggest freespace and see if it's the same one.
148 */
149 else {
150 dup = (xfs_dir2_data_unused_t *)
Nathan Scott70e73f52006-03-17 17:26:52 +1100151 ((char *)block + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (dup == enddup) {
153 /*
154 * It is the biggest freespace, is it too small
155 * to hold the new leaf too?
156 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100157 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /*
159 * Yes, we use the second-largest
160 * entry instead if it works.
161 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100162 if (be16_to_cpu(bf[1].length) >= len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 dup = (xfs_dir2_data_unused_t *)
164 ((char *)block +
Nathan Scott70e73f52006-03-17 17:26:52 +1100165 be16_to_cpu(bf[1].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 else
167 dup = NULL;
168 }
169 } else {
170 /*
171 * Not the same free entry,
172 * just check its length.
173 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100174 if (be16_to_cpu(dup->length) < len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dup = NULL;
176 }
177 }
178 }
179 compact = 0;
180 }
181 /*
182 * If there are stale entries we'll use one for the leaf.
183 * Is the biggest entry enough to avoid compaction?
184 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100185 else if (be16_to_cpu(bf[0].length) >= len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 dup = (xfs_dir2_data_unused_t *)
Nathan Scott70e73f52006-03-17 17:26:52 +1100187 ((char *)block + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 compact = 0;
189 }
190 /*
191 * Will need to compact to make this work.
192 */
193 else {
194 /*
195 * Tag just before the first leaf entry.
196 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100197 tagp = (__be16 *)blp - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /*
199 * Data object just before the first leaf entry.
200 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100201 dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /*
203 * If it's not free then the data will go where the
204 * leaf data starts now, if it works at all.
205 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100206 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100207 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 (uint)sizeof(*blp) < len)
209 dup = NULL;
Nathan Scotte922fff2006-03-17 17:27:56 +1100210 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 dup = NULL;
212 else
213 dup = (xfs_dir2_data_unused_t *)blp;
214 compact = 1;
215 }
216 /*
217 * If this isn't a real add, we're done with the buffer.
218 */
Barry Naujok6a178102008-05-21 16:42:05 +1000219 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 xfs_da_brelse(tp, bp);
221 /*
222 * If we don't have space for the new entry & leaf ...
223 */
224 if (!dup) {
225 /*
226 * Not trying to actually do anything, or don't have
227 * a space reservation: return no-space.
228 */
Barry Naujok6a178102008-05-21 16:42:05 +1000229 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return XFS_ERROR(ENOSPC);
231 /*
232 * Convert to the next larger format.
233 * Then add the new entry in that format.
234 */
235 error = xfs_dir2_block_to_leaf(args, bp);
236 xfs_da_buf_done(bp);
237 if (error)
238 return error;
239 return xfs_dir2_leaf_addname(args);
240 }
241 /*
242 * Just checking, and it would work, so say so.
243 */
Barry Naujok6a178102008-05-21 16:42:05 +1000244 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 0;
246 needlog = needscan = 0;
247 /*
248 * If need to compact the leaf entries, do it now.
249 * Leave the highest-numbered stale entry stale.
250 * XXX should be the one closest to mid but mid is not yet computed.
251 */
252 if (compact) {
253 int fromidx; /* source leaf index */
254 int toidx; /* target leaf index */
255
Nathan Scotte922fff2006-03-17 17:27:56 +1100256 for (fromidx = toidx = be32_to_cpu(btp->count) - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 highstale = lfloghigh = -1;
258 fromidx >= 0;
259 fromidx--) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100260 if (be32_to_cpu(blp[fromidx].address) == XFS_DIR2_NULL_DATAPTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (highstale == -1)
262 highstale = toidx;
263 else {
264 if (lfloghigh == -1)
265 lfloghigh = toidx;
266 continue;
267 }
268 }
269 if (fromidx < toidx)
270 blp[toidx] = blp[fromidx];
271 toidx--;
272 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100273 lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
274 lfloghigh -= be32_to_cpu(btp->stale) - 1;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800275 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 xfs_dir2_data_make_free(tp, bp,
277 (xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
Nathan Scotte922fff2006-03-17 17:27:56 +1100278 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 &needlog, &needscan);
Nathan Scotte922fff2006-03-17 17:27:56 +1100280 blp += be32_to_cpu(btp->stale) - 1;
281 btp->stale = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /*
283 * If we now need to rebuild the bestfree map, do so.
284 * This needs to happen before the next call to use_free.
285 */
286 if (needscan) {
Eric Sandeenef497f82007-05-08 13:48:49 +1000287 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 needscan = 0;
289 }
290 }
291 /*
292 * Set leaf logging boundaries to impossible state.
293 * For the no-stale case they're set explicitly.
294 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100295 else if (btp->stale) {
296 lfloglow = be32_to_cpu(btp->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 lfloghigh = -1;
298 }
299 /*
300 * Find the slot that's first lower than our hash value, -1 if none.
301 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100302 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100304 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 break;
306 if (hash < args->hashval)
307 low = mid + 1;
308 else
309 high = mid - 1;
310 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100311 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 mid--;
313 }
314 /*
315 * No stale entries, will use enddup space to hold new leaf.
316 */
317 if (!btp->stale) {
318 /*
319 * Mark the space needed for the new leaf entry, now in use.
320 */
321 xfs_dir2_data_use_free(tp, bp, enddup,
322 (xfs_dir2_data_aoff_t)
Nathan Scottad354eb2006-03-17 17:27:37 +1100323 ((char *)enddup - (char *)block + be16_to_cpu(enddup->length) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 sizeof(*blp)),
325 (xfs_dir2_data_aoff_t)sizeof(*blp),
326 &needlog, &needscan);
327 /*
328 * Update the tail (entry count).
329 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800330 be32_add_cpu(&btp->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /*
332 * If we now need to rebuild the bestfree map, do so.
333 * This needs to happen before the next call to use_free.
334 */
335 if (needscan) {
336 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block,
Eric Sandeenef497f82007-05-08 13:48:49 +1000337 &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 needscan = 0;
339 }
340 /*
341 * Adjust pointer to the first leaf entry, we're about to move
342 * the table up one to open up space for the new leaf entry.
343 * Then adjust our index to match.
344 */
345 blp--;
346 mid++;
347 if (mid)
348 memmove(blp, &blp[1], mid * sizeof(*blp));
349 lfloglow = 0;
350 lfloghigh = mid;
351 }
352 /*
353 * Use a stale leaf for our new entry.
354 */
355 else {
356 for (lowstale = mid;
357 lowstale >= 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100358 be32_to_cpu(blp[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 lowstale--)
360 continue;
361 for (highstale = mid + 1;
Nathan Scotte922fff2006-03-17 17:27:56 +1100362 highstale < be32_to_cpu(btp->count) &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100363 be32_to_cpu(blp[highstale].address) != 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,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100402 (char *)dep - (char *)block));
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,
408 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
409 (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);
Nathan Scott3d693c62006-03-17 17:28:27 +1100417 *tagp = cpu_to_be16((char *)dep - (char *)block);
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)
Eric Sandeenef497f82007-05-08 13:48:49 +1000422 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &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{
442 xfs_dir2_block_t *block; /* directory block structure */
443 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 block = bp->data;
476 xfs_dir2_data_check(dp, bp);
477 /*
478 * Set up values for the loop.
479 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000480 btp = xfs_dir2_block_tail_p(mp, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 ptr = (char *)block->u;
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 */
507 if ((char *)dep - (char *)block < wantoff)
508 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,
Lachlan McIlroy041388b2007-12-18 16:19:34 +1100511 (char *)dep - (char *)block);
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{
545 xfs_dir2_block_t *block; /* directory block structure */
546 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
547 xfs_dir2_block_tail_t *btp; /* block tail */
548 xfs_mount_t *mp; /* filesystem mount point */
549
550 mp = tp->t_mountp;
551 block = bp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000552 btp = xfs_dir2_block_tail_p(mp, block);
553 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)block),
555 (uint)((char *)&blp[last + 1] - (char *)block - 1));
556}
557
558/*
559 * Log the block tail.
560 */
561static void
562xfs_dir2_block_log_tail(
563 xfs_trans_t *tp, /* transaction structure */
564 xfs_dabuf_t *bp) /* block buffer */
565{
566 xfs_dir2_block_t *block; /* directory block structure */
567 xfs_dir2_block_tail_t *btp; /* block tail */
568 xfs_mount_t *mp; /* filesystem mount point */
569
570 mp = tp->t_mountp;
571 block = bp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000572 btp = xfs_dir2_block_tail_p(mp, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)block),
574 (uint)((char *)(btp + 1) - (char *)block - 1));
575}
576
577/*
578 * Look up an entry in the block. This is the external routine,
579 * xfs_dir2_block_lookup_int does the real work.
580 */
581int /* error */
582xfs_dir2_block_lookup(
583 xfs_da_args_t *args) /* dir lookup arguments */
584{
585 xfs_dir2_block_t *block; /* block structure */
586 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
587 xfs_dabuf_t *bp; /* block buffer */
588 xfs_dir2_block_tail_t *btp; /* block tail */
589 xfs_dir2_data_entry_t *dep; /* block data entry */
590 xfs_inode_t *dp; /* incore inode */
591 int ent; /* entry index */
592 int error; /* error return value */
593 xfs_mount_t *mp; /* filesystem mount point */
594
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000595 trace_xfs_dir2_block_lookup(args);
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /*
598 * Get the buffer, look up the entry.
599 * If not found (ENOENT) then return, have no buffer.
600 */
601 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
602 return error;
603 dp = args->dp;
604 mp = dp->i_mount;
605 block = bp->data;
606 xfs_dir2_data_check(dp, bp);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000607 btp = xfs_dir2_block_tail_p(mp, block);
608 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /*
610 * Get the offset from the leaf entry, to point to the data.
611 */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000612 dep = (xfs_dir2_data_entry_t *)((char *)block +
613 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +1000615 * Fill in inode number, CI name if appropriate, release the block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000617 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000618 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 xfs_da_brelse(args->trans, bp);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000620 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623/*
624 * Internal block lookup routine.
625 */
626static int /* error */
627xfs_dir2_block_lookup_int(
628 xfs_da_args_t *args, /* dir lookup arguments */
629 xfs_dabuf_t **bpp, /* returned block buffer */
630 int *entno) /* returned entry number */
631{
632 xfs_dir2_dataptr_t addr; /* data entry address */
633 xfs_dir2_block_t *block; /* block structure */
634 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
635 xfs_dabuf_t *bp; /* block buffer */
636 xfs_dir2_block_tail_t *btp; /* block tail */
637 xfs_dir2_data_entry_t *dep; /* block data entry */
638 xfs_inode_t *dp; /* incore inode */
639 int error; /* error return value */
640 xfs_dahash_t hash; /* found hash value */
641 int high; /* binary search high index */
642 int low; /* binary search low index */
643 int mid; /* binary search current idx */
644 xfs_mount_t *mp; /* filesystem mount point */
645 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000646 enum xfs_dacmp cmp; /* comparison result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 dp = args->dp;
649 tp = args->trans;
650 mp = dp->i_mount;
651 /*
652 * Read the buffer, return error if we can't get it.
653 */
654 if ((error =
655 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
656 return error;
657 }
658 ASSERT(bp != NULL);
659 block = bp->data;
660 xfs_dir2_data_check(dp, bp);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000661 btp = xfs_dir2_block_tail_p(mp, block);
662 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
664 * Loop doing a binary search for our hash value.
665 * Find our entry, ENOENT if it's not there.
666 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100667 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 ASSERT(low <= high);
669 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100670 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 break;
672 if (hash < args->hashval)
673 low = mid + 1;
674 else
675 high = mid - 1;
676 if (low > high) {
Barry Naujok6a178102008-05-21 16:42:05 +1000677 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 xfs_da_brelse(tp, bp);
679 return XFS_ERROR(ENOENT);
680 }
681 }
682 /*
683 * Back up to the first one with the right hash value.
684 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100685 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 mid--;
687 }
688 /*
689 * Now loop forward through all the entries with the
690 * right hash value looking for our name.
691 */
692 do {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100693 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 continue;
695 /*
696 * Get pointer to the entry from the leaf.
697 */
698 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000699 ((char *)block + xfs_dir2_dataptr_to_off(mp, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000701 * Compare name and if it's an exact match, return the index
702 * and buffer. If it's the first case-insensitive match, store
703 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
Barry Naujok5163f952008-05-21 16:41:01 +1000705 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
706 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
707 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 *bpp = bp;
709 *entno = mid;
Barry Naujok5163f952008-05-21 16:41:01 +1000710 if (cmp == XFS_CMP_EXACT)
711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
Barry Naujok5163f952008-05-21 16:41:01 +1000713 } while (++mid < be32_to_cpu(btp->count) &&
714 be32_to_cpu(blp[mid].hashval) == hash);
715
Barry Naujok6a178102008-05-21 16:42:05 +1000716 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000717 /*
718 * Here, we can only be doing a lookup (not a rename or replace).
719 * If a case-insensitive match was found earlier, return success.
720 */
721 if (args->cmpresult == XFS_CMP_CASE)
722 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /*
724 * No match, release the buffer and return ENOENT.
725 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 xfs_da_brelse(tp, bp);
727 return XFS_ERROR(ENOENT);
728}
729
730/*
731 * Remove an entry from a block format directory.
732 * If that makes the block small enough to fit in shortform, transform it.
733 */
734int /* error */
735xfs_dir2_block_removename(
736 xfs_da_args_t *args) /* directory operation args */
737{
738 xfs_dir2_block_t *block; /* block structure */
739 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
740 xfs_dabuf_t *bp; /* block buffer */
741 xfs_dir2_block_tail_t *btp; /* block tail */
742 xfs_dir2_data_entry_t *dep; /* block data entry */
743 xfs_inode_t *dp; /* incore inode */
744 int ent; /* block leaf entry index */
745 int error; /* error return value */
746 xfs_mount_t *mp; /* filesystem mount point */
747 int needlog; /* need to log block header */
748 int needscan; /* need to fixup bestfree */
749 xfs_dir2_sf_hdr_t sfh; /* shortform header */
750 int size; /* shortform size */
751 xfs_trans_t *tp; /* transaction pointer */
752
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000753 trace_xfs_dir2_block_removename(args);
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /*
756 * Look up the entry in the block. Gets the buffer and entry index.
757 * It will always be there, the vnodeops level does a lookup first.
758 */
759 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
760 return error;
761 }
762 dp = args->dp;
763 tp = args->trans;
764 mp = dp->i_mount;
765 block = bp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000766 btp = xfs_dir2_block_tail_p(mp, block);
767 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /*
769 * Point to the data entry using the leaf entry.
770 */
771 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000772 ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /*
774 * Mark the data entry's space free.
775 */
776 needlog = needscan = 0;
777 xfs_dir2_data_make_free(tp, bp,
778 (xfs_dir2_data_aoff_t)((char *)dep - (char *)block),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000779 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /*
781 * Fix up the block tail.
782 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800783 be32_add_cpu(&btp->stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 xfs_dir2_block_log_tail(tp, bp);
785 /*
786 * Remove the leaf entry by marking it stale.
787 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100788 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
790 /*
791 * Fix up bestfree, log the header if necessary.
792 */
793 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000794 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (needlog)
796 xfs_dir2_data_log_header(tp, bp);
797 xfs_dir2_data_check(dp, bp);
798 /*
799 * See if the size as a shortform is good enough.
800 */
801 if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
802 XFS_IFORK_DSIZE(dp)) {
803 xfs_da_buf_done(bp);
804 return 0;
805 }
806 /*
807 * If it works, do the conversion.
808 */
809 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
810}
811
812/*
813 * Replace an entry in a V2 block directory.
814 * Change the inode number to the new value.
815 */
816int /* error */
817xfs_dir2_block_replace(
818 xfs_da_args_t *args) /* directory operation args */
819{
820 xfs_dir2_block_t *block; /* block structure */
821 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
822 xfs_dabuf_t *bp; /* block buffer */
823 xfs_dir2_block_tail_t *btp; /* block tail */
824 xfs_dir2_data_entry_t *dep; /* block data entry */
825 xfs_inode_t *dp; /* incore inode */
826 int ent; /* leaf entry index */
827 int error; /* error return value */
828 xfs_mount_t *mp; /* filesystem mount point */
829
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000830 trace_xfs_dir2_block_replace(args);
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /*
833 * Lookup the entry in the directory. Get buffer and entry index.
834 * This will always succeed since the caller has already done a lookup.
835 */
836 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
837 return error;
838 }
839 dp = args->dp;
840 mp = dp->i_mount;
841 block = bp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000842 btp = xfs_dir2_block_tail_p(mp, block);
843 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /*
845 * Point to the data entry we need to change.
846 */
847 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000848 ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000849 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /*
851 * Change the inode number to the new value.
852 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000853 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 xfs_dir2_data_log_entry(args->trans, bp, dep);
855 xfs_dir2_data_check(dp, bp);
856 xfs_da_buf_done(bp);
857 return 0;
858}
859
860/*
861 * Qsort comparison routine for the block leaf entries.
862 */
863static int /* sort order */
864xfs_dir2_block_sort(
865 const void *a, /* first leaf entry */
866 const void *b) /* second leaf entry */
867{
868 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
869 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
870
871 la = a;
872 lb = b;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100873 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
874 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
877/*
878 * Convert a V2 leaf directory to a V2 block directory if possible.
879 */
880int /* error */
881xfs_dir2_leaf_to_block(
882 xfs_da_args_t *args, /* operation arguments */
883 xfs_dabuf_t *lbp, /* leaf buffer */
884 xfs_dabuf_t *dbp) /* data buffer */
885{
Nathan Scott68b3a102006-03-17 17:27:19 +1100886 __be16 *bestsp; /* leaf bests table */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 xfs_dir2_block_t *block; /* block structure */
888 xfs_dir2_block_tail_t *btp; /* block tail */
889 xfs_inode_t *dp; /* incore directory inode */
890 xfs_dir2_data_unused_t *dup; /* unused data entry */
891 int error; /* error return value */
892 int from; /* leaf from index */
893 xfs_dir2_leaf_t *leaf; /* leaf structure */
894 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
895 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
896 xfs_mount_t *mp; /* file system mount point */
897 int needlog; /* need to log data header */
898 int needscan; /* need to scan for bestfree */
899 xfs_dir2_sf_hdr_t sfh; /* shortform header */
900 int size; /* bytes used */
Nathan Scott3d693c62006-03-17 17:28:27 +1100901 __be16 *tagp; /* end of entry (tag) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 int to; /* block/leaf to index */
903 xfs_trans_t *tp; /* transaction pointer */
904
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000905 trace_xfs_dir2_leaf_to_block(args);
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 dp = args->dp;
908 tp = args->trans;
909 mp = dp->i_mount;
910 leaf = lbp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100911 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000912 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /*
914 * If there are data blocks other than the first one, take this
915 * opportunity to remove trailing empty data blocks that may have
916 * been left behind during no-space-reservation operations.
917 * These will show up in the leaf bests table.
918 */
919 while (dp->i_d.di_size > mp->m_dirblksize) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000920 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100921 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 mp->m_dirblksize - (uint)sizeof(block->hdr)) {
923 if ((error =
924 xfs_dir2_leaf_trim_data(args, lbp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100925 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 goto out;
927 } else {
928 error = 0;
929 goto out;
930 }
931 }
932 /*
933 * Read the data block if we don't already have it, give up if it fails.
934 */
935 if (dbp == NULL &&
936 (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
937 XFS_DATA_FORK))) {
938 goto out;
939 }
940 block = dbp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +1100941 ASSERT(be32_to_cpu(block->hdr.magic) == XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /*
943 * Size of the "leaf" area in the block.
944 */
945 size = (uint)sizeof(block->tail) +
Nathan Scotta818e5d2006-03-17 17:28:07 +1100946 (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /*
948 * Look at the last data entry.
949 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100950 tagp = (__be16 *)((char *)block + mp->m_dirblksize) - 1;
951 dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /*
953 * If it's not free or is too short we can't do it.
954 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100955 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
956 be16_to_cpu(dup->length) < size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 error = 0;
958 goto out;
959 }
960 /*
961 * Start converting it to block form.
962 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100963 block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 needlog = 1;
965 needscan = 0;
966 /*
967 * Use up the space at the end of the block (blp/btp).
968 */
969 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
970 &needlog, &needscan);
971 /*
972 * Initialize the block tail.
973 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000974 btp = xfs_dir2_block_tail_p(mp, block);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100975 btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 btp->stale = 0;
977 xfs_dir2_block_log_tail(tp, dbp);
978 /*
979 * Initialize the block leaf area. We compact out stale entries.
980 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000981 lep = xfs_dir2_block_leaf_p(btp);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100982 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100983 if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 continue;
985 lep[to++] = leaf->ents[from];
986 }
Nathan Scotte922fff2006-03-17 17:27:56 +1100987 ASSERT(to == be32_to_cpu(btp->count));
988 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /*
990 * Scan the bestfree if we need it and log the data block header.
991 */
992 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000993 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (needlog)
995 xfs_dir2_data_log_header(tp, dbp);
996 /*
997 * Pitch the old leaf block.
998 */
999 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
1000 lbp = NULL;
1001 if (error) {
1002 goto out;
1003 }
1004 /*
1005 * Now see if the resulting block can be shrunken to shortform.
1006 */
1007 if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
1008 XFS_IFORK_DSIZE(dp)) {
1009 error = 0;
1010 goto out;
1011 }
1012 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
1013out:
1014 if (lbp)
1015 xfs_da_buf_done(lbp);
1016 if (dbp)
1017 xfs_da_buf_done(dbp);
1018 return error;
1019}
1020
1021/*
1022 * Convert the shortform directory to block form.
1023 */
1024int /* error */
1025xfs_dir2_sf_to_block(
1026 xfs_da_args_t *args) /* operation arguments */
1027{
1028 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
1029 xfs_dir2_block_t *block; /* block structure */
1030 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1031 xfs_dabuf_t *bp; /* block buffer */
1032 xfs_dir2_block_tail_t *btp; /* block tail pointer */
1033 char *buf; /* sf buffer */
1034 int buf_len;
1035 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1036 xfs_inode_t *dp; /* incore directory inode */
1037 int dummy; /* trash */
1038 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1039 int endoffset; /* end of data objects */
1040 int error; /* error return value */
1041 int i; /* index */
1042 xfs_mount_t *mp; /* filesystem mount point */
1043 int needlog; /* need to log block header */
1044 int needscan; /* need to scan block freespc */
1045 int newoffset; /* offset from current entry */
1046 int offset; /* target block offset */
1047 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
1048 xfs_dir2_sf_t *sfp; /* shortform structure */
Nathan Scott3d693c62006-03-17 17:28:27 +11001049 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +10001051 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001053 trace_xfs_dir2_sf_to_block(args);
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 dp = args->dp;
1056 tp = args->trans;
1057 mp = dp->i_mount;
1058 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1059 /*
1060 * Bomb out if the shortform directory is way too short.
1061 */
1062 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1063 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1064 return XFS_ERROR(EIO);
1065 }
1066 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1067 ASSERT(dp->i_df.if_u1.if_data != NULL);
1068 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001069 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /*
1071 * Copy the directory into the stack buffer.
1072 * Then pitch the incore inode data so we can make extents.
1073 */
1074
1075 buf_len = dp->i_df.if_bytes;
1076 buf = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1077
1078 memcpy(buf, sfp, dp->i_df.if_bytes);
1079 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
1080 dp->i_d.di_size = 0;
1081 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1082 /*
1083 * Reset pointer - old sfp is gone.
1084 */
1085 sfp = (xfs_dir2_sf_t *)buf;
1086 /*
1087 * Add block 0 to the inode.
1088 */
1089 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1090 if (error) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001091 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return error;
1093 }
1094 /*
1095 * Initialize the data block.
1096 */
1097 error = xfs_dir2_data_init(args, blkno, &bp);
1098 if (error) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001099 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return error;
1101 }
1102 block = bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001103 block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /*
1105 * Compute size of block "tail" area.
1106 */
1107 i = (uint)sizeof(*btp) +
Nathan Scott8f44e042006-03-17 17:28:47 +11001108 (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /*
1110 * The whole thing is initialized to free by the init routine.
1111 * Say we're using the leaf and tail area.
1112 */
1113 dup = (xfs_dir2_data_unused_t *)block->u;
1114 needlog = needscan = 0;
1115 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1116 &needscan);
1117 ASSERT(needscan == 0);
1118 /*
1119 * Fill in the tail.
1120 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001121 btp = xfs_dir2_block_tail_p(mp, block);
Nathan Scott8f44e042006-03-17 17:28:47 +11001122 btp->count = cpu_to_be32(sfp->hdr.count + 2); /* ., .. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 btp->stale = 0;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001124 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 endoffset = (uint)((char *)blp - (char *)block);
1126 /*
1127 * Remove the freespace, we'll manage it.
1128 */
1129 xfs_dir2_data_use_free(tp, bp, dup,
1130 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
Nathan Scottad354eb2006-03-17 17:27:37 +11001131 be16_to_cpu(dup->length), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 /*
1133 * Create entry for .
1134 */
1135 dep = (xfs_dir2_data_entry_t *)
1136 ((char *)block + XFS_DIR2_DATA_DOT_OFFSET);
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001137 dep->inumber = cpu_to_be64(dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 dep->namelen = 1;
1139 dep->name[0] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001140 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001141 *tagp = cpu_to_be16((char *)dep - (char *)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001143 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001144 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001145 (char *)dep - (char *)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 /*
1147 * Create entry for ..
1148 */
1149 dep = (xfs_dir2_data_entry_t *)
1150 ((char *)block + XFS_DIR2_DATA_DOTDOT_OFFSET);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001151 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 dep->namelen = 2;
1153 dep->name[0] = dep->name[1] = '.';
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001154 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001155 *tagp = cpu_to_be16((char *)dep - (char *)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 xfs_dir2_data_log_entry(tp, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001157 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001158 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001159 (char *)dep - (char *)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 offset = XFS_DIR2_DATA_FIRST_OFFSET;
1161 /*
1162 * Loop over existing entries, stuff them in.
1163 */
Nathan Scott8f44e042006-03-17 17:28:47 +11001164 if ((i = 0) == sfp->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 sfep = NULL;
1166 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001167 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /*
1169 * Need to preserve the existing offset values in the sf directory.
1170 * Insert holes (unused entries) where necessary.
1171 */
1172 while (offset < endoffset) {
1173 /*
1174 * sfep is null when we reach the end of the list.
1175 */
1176 if (sfep == NULL)
1177 newoffset = endoffset;
1178 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001179 newoffset = xfs_dir2_sf_get_offset(sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /*
1181 * There should be a hole here, make one.
1182 */
1183 if (offset < newoffset) {
1184 dup = (xfs_dir2_data_unused_t *)
1185 ((char *)block + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +11001186 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1187 dup->length = cpu_to_be16(newoffset - offset);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001188 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 ((char *)dup - (char *)block));
1190 xfs_dir2_data_log_unused(tp, bp, dup);
1191 (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t *)block,
1192 dup, &dummy);
Nathan Scottad354eb2006-03-17 17:27:37 +11001193 offset += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 continue;
1195 }
1196 /*
1197 * Copy a real entry.
1198 */
1199 dep = (xfs_dir2_data_entry_t *)((char *)block + newoffset);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001200 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp,
1201 xfs_dir2_sf_inumberp(sfep)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 dep->namelen = sfep->namelen;
1203 memcpy(dep->name, sfep->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001204 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001205 *tagp = cpu_to_be16((char *)dep - (char *)block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 xfs_dir2_data_log_entry(tp, bp, dep);
Barry Naujok5163f952008-05-21 16:41:01 +10001207 name.name = sfep->name;
1208 name.len = sfep->namelen;
1209 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1210 hashname(&name));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001211 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 (char *)dep - (char *)block));
1213 offset = (int)((char *)(tagp + 1) - (char *)block);
Nathan Scott8f44e042006-03-17 17:28:47 +11001214 if (++i == sfp->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 sfep = NULL;
1216 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001217 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219 /* Done with the temporary buffer */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001220 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /*
1222 * Sort the leaf entries by hash value.
1223 */
Nathan Scotte922fff2006-03-17 17:27:56 +11001224 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /*
1226 * Log the leaf entry area and tail.
1227 * Already logged the header in data_init, ignore needlog.
1228 */
1229 ASSERT(needscan == 0);
Nathan Scotte922fff2006-03-17 17:27:56 +11001230 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 xfs_dir2_block_log_tail(tp, bp);
1232 xfs_dir2_data_check(dp, bp);
1233 xfs_da_buf_done(bp);
1234 return 0;
1235}