blob: b0f26780449d5ebabca4d4b3b448a40728615e56 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
22#include "xfs_trans.h"
23#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dinode.h"
29#include "xfs_inode.h"
30#include "xfs_bmap.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020031#include "xfs_dir2_format.h"
32#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000034#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/*
37 * Function declarations.
38 */
39static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp);
40static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index);
41#ifdef DEBUG
42static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
43#else
44#define xfs_dir2_leafn_check(dp, bp)
45#endif
46static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s,
47 int start_s, xfs_dabuf_t *bp_d, int start_d,
48 int count);
49static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
50 xfs_da_state_blk_t *blk1,
51 xfs_da_state_blk_t *blk2);
52static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp,
53 int index, xfs_da_state_blk_t *dblk,
54 int *rval);
55static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
56 xfs_da_state_blk_t *fblk);
57
58/*
59 * Log entries from a freespace block.
60 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +000061STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -070062xfs_dir2_free_log_bests(
63 xfs_trans_t *tp, /* transaction pointer */
64 xfs_dabuf_t *bp, /* freespace buffer */
65 int first, /* first entry to log */
66 int last) /* last entry to log */
67{
68 xfs_dir2_free_t *free; /* freespace structure */
69
70 free = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +020071 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 xfs_da_log_buf(tp, bp,
73 (uint)((char *)&free->bests[first] - (char *)free),
74 (uint)((char *)&free->bests[last] - (char *)free +
75 sizeof(free->bests[0]) - 1));
76}
77
78/*
79 * Log header from a freespace block.
80 */
81static void
82xfs_dir2_free_log_header(
83 xfs_trans_t *tp, /* transaction pointer */
84 xfs_dabuf_t *bp) /* freespace buffer */
85{
86 xfs_dir2_free_t *free; /* freespace structure */
87
88 free = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +020089 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
91 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
92}
93
94/*
95 * Convert a leaf-format directory to a node-format directory.
96 * We need to change the magic number of the leaf block, and copy
97 * the freespace table out of the leaf block into its own block.
98 */
99int /* error */
100xfs_dir2_leaf_to_node(
101 xfs_da_args_t *args, /* operation arguments */
102 xfs_dabuf_t *lbp) /* leaf buffer */
103{
104 xfs_inode_t *dp; /* incore directory inode */
105 int error; /* error return value */
106 xfs_dabuf_t *fbp; /* freespace buffer */
107 xfs_dir2_db_t fdb; /* freespace block number */
108 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100109 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 int i; /* leaf freespace index */
111 xfs_dir2_leaf_t *leaf; /* leaf structure */
112 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
113 xfs_mount_t *mp; /* filesystem mount point */
114 int n; /* count of live freespc ents */
115 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100116 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 xfs_trans_t *tp; /* transaction pointer */
118
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000119 trace_xfs_dir2_leaf_to_node(args);
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 dp = args->dp;
122 mp = dp->i_mount;
123 tp = args->trans;
124 /*
125 * Add a freespace block to the directory.
126 */
127 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
128 return error;
129 }
130 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
131 /*
132 * Get the buffer for the new freespace block.
133 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000134 if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 XFS_DATA_FORK))) {
136 return error;
137 }
138 ASSERT(fbp != NULL);
139 free = fbp->data;
140 leaf = lbp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000141 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /*
143 * Initialize the freespace block header.
144 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100145 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 free->hdr.firstdb = 0;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100147 ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100148 free->hdr.nvalid = ltp->bestcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /*
150 * Copy freespace entries from the leaf block to the new block.
151 * Count active entries.
152 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000153 for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100154 i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100155 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100157 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
Nathan Scott0ba962e2006-03-17 17:27:07 +1100159 free->hdr.nused = cpu_to_be32(n);
Nathan Scott89da0542006-03-17 17:28:40 +1100160 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /*
162 * Log everything.
163 */
164 xfs_dir2_leaf_log_header(tp, lbp);
165 xfs_dir2_free_log_header(tp, fbp);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100166 xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 xfs_da_buf_done(fbp);
168 xfs_dir2_leafn_check(dp, lbp);
169 return 0;
170}
171
172/*
173 * Add a leaf entry to a leaf block in a node-form directory.
174 * The other work necessary is done from the caller.
175 */
176static int /* error */
177xfs_dir2_leafn_add(
178 xfs_dabuf_t *bp, /* leaf buffer */
179 xfs_da_args_t *args, /* operation arguments */
180 int index) /* insertion pt for new entry */
181{
182 int compact; /* compacting stale leaves */
183 xfs_inode_t *dp; /* incore directory inode */
184 int highstale; /* next stale entry */
185 xfs_dir2_leaf_t *leaf; /* leaf structure */
186 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
187 int lfloghigh; /* high leaf entry logging */
188 int lfloglow; /* low leaf entry logging */
189 int lowstale; /* previous stale entry */
190 xfs_mount_t *mp; /* filesystem mount point */
191 xfs_trans_t *tp; /* transaction pointer */
192
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000193 trace_xfs_dir2_leafn_add(args, index);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 dp = args->dp;
196 mp = dp->i_mount;
197 tp = args->trans;
198 leaf = bp->data;
199
200 /*
201 * Quick check just to make sure we are not going to index
202 * into other peoples memory
203 */
204 if (index < 0)
205 return XFS_ERROR(EFSCORRUPTED);
206
207 /*
208 * If there are already the maximum number of leaf entries in
209 * the block, if there are no stale entries it won't fit.
210 * Caller will do a split. If there are stale entries we'll do
211 * a compact.
212 */
213
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000214 if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!leaf->hdr.stale)
216 return XFS_ERROR(ENOSPC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100217 compact = be16_to_cpu(leaf->hdr.stale) > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 } else
219 compact = 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100220 ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100221 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100222 be32_to_cpu(leaf->ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Barry Naujok6a178102008-05-21 16:42:05 +1000224 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return 0;
226
227 /*
228 * Compact out all but one stale leaf entry. Leaves behind
229 * the entry closest to index.
230 */
231 if (compact) {
232 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
233 &lfloglow, &lfloghigh);
234 }
235 /*
236 * Set impossible logging indices for this case.
237 */
238 else if (leaf->hdr.stale) {
Nathan Scotta818e5d2006-03-17 17:28:07 +1100239 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 lfloghigh = -1;
241 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /*
244 * Insert the new entry, log everything.
245 */
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200246 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
247 highstale, &lfloglow, &lfloghigh);
248
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100249 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000250 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100251 args->blkno, args->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 xfs_dir2_leaf_log_header(tp, bp);
253 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
254 xfs_dir2_leafn_check(dp, bp);
255 return 0;
256}
257
258#ifdef DEBUG
259/*
260 * Check internal consistency of a leafn block.
261 */
262void
263xfs_dir2_leafn_check(
264 xfs_inode_t *dp, /* incore directory inode */
265 xfs_dabuf_t *bp) /* leaf buffer */
266{
267 int i; /* leaf index */
268 xfs_dir2_leaf_t *leaf; /* leaf structure */
269 xfs_mount_t *mp; /* filesystem mount point */
270 int stale; /* count of stale leaves */
271
272 leaf = bp->data;
273 mp = dp->i_mount;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200274 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000275 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100276 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
277 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100278 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
279 be32_to_cpu(leaf->ents[i + 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200281 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 stale++;
283 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100284 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286#endif /* DEBUG */
287
288/*
289 * Return the last hash value in the leaf.
290 * Stale entries are ok.
291 */
292xfs_dahash_t /* hash value */
293xfs_dir2_leafn_lasthash(
294 xfs_dabuf_t *bp, /* leaf buffer */
295 int *count) /* count of entries in leaf */
296{
297 xfs_dir2_leaf_t *leaf; /* leaf structure */
298
299 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200300 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (count)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100302 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (!leaf->hdr.count)
304 return 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100305 return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000309 * Look up a leaf entry for space to add a name in a node-format leaf block.
310 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000312STATIC int
313xfs_dir2_leafn_lookup_for_addname(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 xfs_dabuf_t *bp, /* leaf buffer */
315 xfs_da_args_t *args, /* operation arguments */
316 int *indexp, /* out: leaf entry index */
317 xfs_da_state_t *state) /* state to fill in */
318{
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000319 xfs_dabuf_t *curbp = NULL; /* current data/free buffer */
320 xfs_dir2_db_t curdb = -1; /* current data block number */
321 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 xfs_inode_t *dp; /* incore directory inode */
323 int error; /* error return value */
324 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000325 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int index; /* leaf entry index */
327 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000328 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
330 xfs_mount_t *mp; /* filesystem mount point */
331 xfs_dir2_db_t newdb; /* new data block number */
332 xfs_dir2_db_t newfdb; /* new free block number */
333 xfs_trans_t *tp; /* transaction pointer */
334
335 dp = args->dp;
336 tp = args->trans;
337 mp = dp->i_mount;
338 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200339 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#ifdef __KERNEL__
Nathan Scotta818e5d2006-03-17 17:28:07 +1100341 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342#endif
343 xfs_dir2_leafn_check(dp, bp);
344 /*
345 * Look up the hash value in the leaf entries.
346 */
347 index = xfs_dir2_leaf_search_hash(args, bp);
348 /*
349 * Do we have a buffer coming in?
350 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000351 if (state->extravalid) {
352 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000354 curfdb = state->extrablk.blkno;
355 free = curbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200356 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000358 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /*
360 * Loop over leaf entries with the right hash value.
361 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000362 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
363 be32_to_cpu(lep->hashval) == args->hashval;
364 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /*
366 * Skip stale leaf entries.
367 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100368 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 continue;
370 /*
371 * Pull the data block number from the entry.
372 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000373 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /*
375 * For addname, we're looking for a place to put the new entry.
376 * We want to use a data block with an entry of equal
377 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000378 *
379 * If this block isn't the data block we already have
380 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000382 if (newdb != curdb) {
383 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000385 * Convert the data block to the free block
386 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000388 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000390 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000392 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000394 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
396 if (curbp)
397 xfs_da_brelse(tp, curbp);
398 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000399 * Read the free block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000401 error = xfs_da_read_buf(tp, dp,
402 xfs_dir2_db_to_da(mp, newfdb),
403 -1, &curbp, XFS_DATA_FORK);
404 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return error;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000406 free = curbp->data;
407 ASSERT(be32_to_cpu(free->hdr.magic) ==
408 XFS_DIR2_FREE_MAGIC);
409 ASSERT((be32_to_cpu(free->hdr.firstdb) %
Christoph Hellwiga00b7742011-07-13 13:43:48 +0200410 xfs_dir2_free_max_bests(mp)) == 0);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000411 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
412 ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) +
413 be32_to_cpu(free->hdr.nvalid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000416 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000418 fi = xfs_dir2_db_to_fdindex(mp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000420 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200422 if (unlikely(free->bests[fi] ==
423 cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000424 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
425 XFS_ERRLEVEL_LOW, mp);
426 if (curfdb != newfdb)
427 xfs_da_brelse(tp, curbp);
428 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000430 curfdb = newfdb;
431 if (be16_to_cpu(free->bests[fi]) >= length)
432 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000435 /* Didn't find any space */
436 fi = -1;
437out:
Barry Naujok6a178102008-05-21 16:42:05 +1000438 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000439 if (curbp) {
440 /* Giving back a free block. */
441 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000443 state->extrablk.index = fi;
444 state->extrablk.blkno = curfdb;
445 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
446 } else {
447 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000450 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 */
452 *indexp = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return XFS_ERROR(ENOENT);
454}
455
456/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000457 * Look up a leaf entry in a node-format leaf block.
458 * The extrablk in state a data block.
459 */
460STATIC int
461xfs_dir2_leafn_lookup_for_entry(
462 xfs_dabuf_t *bp, /* leaf buffer */
463 xfs_da_args_t *args, /* operation arguments */
464 int *indexp, /* out: leaf entry index */
465 xfs_da_state_t *state) /* state to fill in */
466{
467 xfs_dabuf_t *curbp = NULL; /* current data/free buffer */
468 xfs_dir2_db_t curdb = -1; /* current data block number */
469 xfs_dir2_data_entry_t *dep; /* data block entry */
470 xfs_inode_t *dp; /* incore directory inode */
471 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000472 int index; /* leaf entry index */
473 xfs_dir2_leaf_t *leaf; /* leaf structure */
474 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
475 xfs_mount_t *mp; /* filesystem mount point */
476 xfs_dir2_db_t newdb; /* new data block number */
477 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000478 enum xfs_dacmp cmp; /* comparison result */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000479
480 dp = args->dp;
481 tp = args->trans;
482 mp = dp->i_mount;
483 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200484 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000485#ifdef __KERNEL__
486 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
487#endif
488 xfs_dir2_leafn_check(dp, bp);
489 /*
490 * Look up the hash value in the leaf entries.
491 */
492 index = xfs_dir2_leaf_search_hash(args, bp);
493 /*
494 * Do we have a buffer coming in?
495 */
496 if (state->extravalid) {
497 curbp = state->extrablk.bp;
498 curdb = state->extrablk.blkno;
499 }
500 /*
501 * Loop over leaf entries with the right hash value.
502 */
503 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
504 be32_to_cpu(lep->hashval) == args->hashval;
505 lep++, index++) {
506 /*
507 * Skip stale leaf entries.
508 */
509 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
510 continue;
511 /*
512 * Pull the data block number from the entry.
513 */
514 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
515 /*
516 * Not adding a new entry, so we really want to find
517 * the name given to us.
518 *
519 * If it's a different data block, go get it.
520 */
521 if (newdb != curdb) {
522 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000523 * If we had a block before that we aren't saving
524 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000525 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000526 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
527 curdb != state->extrablk.blkno))
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000528 xfs_da_brelse(tp, curbp);
529 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000530 * If needing the block that is saved with a CI match,
531 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000532 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000533 if (args->cmpresult != XFS_CMP_DIFFERENT &&
534 newdb == state->extrablk.blkno) {
535 ASSERT(state->extravalid);
536 curbp = state->extrablk.bp;
537 } else {
538 error = xfs_da_read_buf(tp, dp,
539 xfs_dir2_db_to_da(mp, newdb),
540 -1, &curbp, XFS_DATA_FORK);
541 if (error)
542 return error;
543 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000544 xfs_dir2_data_check(dp, curbp);
545 curdb = newdb;
546 }
547 /*
548 * Point to the data entry.
549 */
550 dep = (xfs_dir2_data_entry_t *)((char *)curbp->data +
551 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
552 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000553 * Compare the entry and if it's an exact match, return
554 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000555 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000556 */
Barry Naujok5163f952008-05-21 16:41:01 +1000557 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
558 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000559 /* If there is a CI match block, drop it */
560 if (args->cmpresult != XFS_CMP_DIFFERENT &&
561 curdb != state->extrablk.blkno)
562 xfs_da_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000563 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000564 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000565 *indexp = index;
566 state->extravalid = 1;
567 state->extrablk.bp = curbp;
568 state->extrablk.blkno = curdb;
569 state->extrablk.index = (int)((char *)dep -
570 (char *)curbp->data);
571 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Barry Naujok5163f952008-05-21 16:41:01 +1000572 if (cmp == XFS_CMP_EXACT)
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000573 return XFS_ERROR(EEXIST);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000574 }
575 }
Barry Naujok6a178102008-05-21 16:42:05 +1000576 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
577 (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000578 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000579 if (args->cmpresult == XFS_CMP_DIFFERENT) {
580 /* Giving back last used data block. */
581 state->extravalid = 1;
582 state->extrablk.bp = curbp;
583 state->extrablk.index = -1;
584 state->extrablk.blkno = curdb;
585 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
586 } else {
587 /* If the curbp is not the CI match block, drop it */
588 if (state->extrablk.bp != curbp)
589 xfs_da_brelse(tp, curbp);
590 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000591 } else {
592 state->extravalid = 0;
593 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000594 *indexp = index;
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000595 return XFS_ERROR(ENOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000596}
597
598/*
599 * Look up a leaf entry in a node-format leaf block.
600 * If this is an addname then the extrablk in state is a freespace block,
601 * otherwise it's a data block.
602 */
603int
604xfs_dir2_leafn_lookup_int(
605 xfs_dabuf_t *bp, /* leaf buffer */
606 xfs_da_args_t *args, /* operation arguments */
607 int *indexp, /* out: leaf entry index */
608 xfs_da_state_t *state) /* state to fill in */
609{
Barry Naujok6a178102008-05-21 16:42:05 +1000610 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000611 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
612 state);
613 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
614}
615
616/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 * Move count leaf entries from source to destination leaf.
618 * Log entries and headers. Stale entries are preserved.
619 */
620static void
621xfs_dir2_leafn_moveents(
622 xfs_da_args_t *args, /* operation arguments */
623 xfs_dabuf_t *bp_s, /* source leaf buffer */
624 int start_s, /* source leaf index */
625 xfs_dabuf_t *bp_d, /* destination leaf buffer */
626 int start_d, /* destination leaf index */
627 int count) /* count of leaves to copy */
628{
629 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
630 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
631 int stale; /* count stale leaves copied */
632 xfs_trans_t *tp; /* transaction pointer */
633
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000634 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /*
637 * Silently return if nothing to do.
638 */
639 if (count == 0) {
640 return;
641 }
642 tp = args->trans;
643 leaf_s = bp_s->data;
644 leaf_d = bp_d->data;
645 /*
646 * If the destination index is not the end of the current
647 * destination leaf entries, open up a hole in the destination
648 * to hold the new entries.
649 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100650 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
Nathan Scotta818e5d2006-03-17 17:28:07 +1100652 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 sizeof(xfs_dir2_leaf_entry_t));
654 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100655 count + be16_to_cpu(leaf_d->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 /*
658 * If the source has stale leaves, count the ones in the copy range
659 * so we can update the header correctly.
660 */
661 if (leaf_s->hdr.stale) {
662 int i; /* temp leaf index */
663
664 for (i = start_s, stale = 0; i < start_s + count; i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200665 if (leaf_s->ents[i].address ==
666 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 stale++;
668 }
669 } else
670 stale = 0;
671 /*
672 * Copy the leaf entries from source to destination.
673 */
674 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
675 count * sizeof(xfs_dir2_leaf_entry_t));
676 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
677 /*
678 * If there are source entries after the ones we copied,
679 * delete the ones we copied by sliding the next ones down.
680 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100681 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
683 count * sizeof(xfs_dir2_leaf_entry_t));
684 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
685 }
686 /*
687 * Update the headers and log them.
688 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800689 be16_add_cpu(&leaf_s->hdr.count, -(count));
690 be16_add_cpu(&leaf_s->hdr.stale, -(stale));
691 be16_add_cpu(&leaf_d->hdr.count, count);
692 be16_add_cpu(&leaf_d->hdr.stale, stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 xfs_dir2_leaf_log_header(tp, bp_s);
694 xfs_dir2_leaf_log_header(tp, bp_d);
695 xfs_dir2_leafn_check(args->dp, bp_s);
696 xfs_dir2_leafn_check(args->dp, bp_d);
697}
698
699/*
700 * Determine the sort order of two leaf blocks.
701 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
702 */
703int /* sort order */
704xfs_dir2_leafn_order(
705 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
706 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
707{
708 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
709 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
710
711 leaf1 = leaf1_bp->data;
712 leaf2 = leaf2_bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200713 ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
714 ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100715 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
716 be16_to_cpu(leaf2->hdr.count) > 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100717 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
718 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
719 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return 1;
721 return 0;
722}
723
724/*
725 * Rebalance leaf entries between two leaf blocks.
726 * This is actually only called when the second block is new,
727 * though the code deals with the general case.
728 * A new entry will be inserted in one of the blocks, and that
729 * entry is taken into account when balancing.
730 */
731static void
732xfs_dir2_leafn_rebalance(
733 xfs_da_state_t *state, /* btree cursor */
734 xfs_da_state_blk_t *blk1, /* first btree block */
735 xfs_da_state_blk_t *blk2) /* second btree block */
736{
737 xfs_da_args_t *args; /* operation arguments */
738 int count; /* count (& direction) leaves */
739 int isleft; /* new goes in left leaf */
740 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
741 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
742 int mid; /* midpoint leaf index */
743#ifdef DEBUG
744 int oldstale; /* old count of stale leaves */
745#endif
746 int oldsum; /* old total leaf count */
747 int swap; /* swapped leaf blocks */
748
749 args = state->args;
750 /*
751 * If the block order is wrong, swap the arguments.
752 */
753 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
754 xfs_da_state_blk_t *tmp; /* temp for block swap */
755
756 tmp = blk1;
757 blk1 = blk2;
758 blk2 = tmp;
759 }
760 leaf1 = blk1->bp->data;
761 leaf2 = blk2->bp->data;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100762 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763#ifdef DEBUG
Nathan Scotta818e5d2006-03-17 17:28:07 +1100764 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765#endif
766 mid = oldsum >> 1;
767 /*
768 * If the old leaf count was odd then the new one will be even,
769 * so we need to divide the new count evenly.
770 */
771 if (oldsum & 1) {
772 xfs_dahash_t midhash; /* middle entry hash value */
773
Nathan Scotta818e5d2006-03-17 17:28:07 +1100774 if (mid >= be16_to_cpu(leaf1->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100775 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 else
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100777 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 isleft = args->hashval <= midhash;
779 }
780 /*
781 * If the old count is even then the new count is odd, so there's
782 * no preferred side for the new entry.
783 * Pick the left one.
784 */
785 else
786 isleft = 1;
787 /*
788 * Calculate moved entry count. Positive means left-to-right,
789 * negative means right-to-left. Then move the entries.
790 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100791 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (count > 0)
793 xfs_dir2_leafn_moveents(args, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100794 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 else if (count < 0)
796 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100797 be16_to_cpu(leaf1->hdr.count), count);
798 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
799 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /*
801 * Mark whether we're inserting into the old or new leaf.
802 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100803 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 state->inleaf = swap;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100805 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 state->inleaf = !swap;
807 else
808 state->inleaf =
Nathan Scotta818e5d2006-03-17 17:28:07 +1100809 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 /*
811 * Adjust the expected index for insertion.
812 */
813 if (!state->inleaf)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100814 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000815
816 /*
817 * Finally sanity check just to make sure we are not returning a
818 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 */
820 if(blk2->index < 0) {
821 state->inleaf = 1;
822 blk2->index = 0;
Dave Chinner0b932cc2011-03-07 10:08:35 +1100823 xfs_alert(args->dp->i_mount,
824 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d\n",
825 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827}
828
829/*
830 * Remove an entry from a node directory.
831 * This removes the leaf entry and the data entry,
832 * and updates the free block if necessary.
833 */
834static int /* error */
835xfs_dir2_leafn_remove(
836 xfs_da_args_t *args, /* operation arguments */
837 xfs_dabuf_t *bp, /* leaf buffer */
838 int index, /* leaf entry index */
839 xfs_da_state_blk_t *dblk, /* data block */
840 int *rval) /* resulting block needs join */
841{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200842 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 xfs_dir2_db_t db; /* data block number */
844 xfs_dabuf_t *dbp; /* data block buffer */
845 xfs_dir2_data_entry_t *dep; /* data block entry */
846 xfs_inode_t *dp; /* incore directory inode */
847 xfs_dir2_leaf_t *leaf; /* leaf structure */
848 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
849 int longest; /* longest data free entry */
850 int off; /* data block entry offset */
851 xfs_mount_t *mp; /* filesystem mount point */
852 int needlog; /* need to log data header */
853 int needscan; /* need to rescan data frees */
854 xfs_trans_t *tp; /* transaction pointer */
855
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000856 trace_xfs_dir2_leafn_remove(args, index);
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 dp = args->dp;
859 tp = args->trans;
860 mp = dp->i_mount;
861 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200862 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /*
864 * Point to the entry we're removing.
865 */
866 lep = &leaf->ents[index];
867 /*
868 * Extract the data block and offset from the entry.
869 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000870 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000872 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 ASSERT(dblk->index == off);
874 /*
875 * Kill the leaf entry by marking it stale.
876 * Log the leaf block changes.
877 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800878 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 xfs_dir2_leaf_log_header(tp, bp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100880 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 xfs_dir2_leaf_log_ents(tp, bp, index, index);
882 /*
883 * Make the data entry free. Keep track of the longest freespace
884 * in the data block in case it changes.
885 */
886 dbp = dblk->bp;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200887 hdr = dbp->data;
888 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
889 longest = be16_to_cpu(hdr->bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 needlog = needscan = 0;
891 xfs_dir2_data_make_free(tp, dbp, off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000892 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /*
894 * Rescan the data block freespaces for bestfree.
895 * Log the data block header if needed.
896 */
897 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200898 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (needlog)
900 xfs_dir2_data_log_header(tp, dbp);
901 xfs_dir2_data_check(dp, dbp);
902 /*
903 * If the longest data block freespace changes, need to update
904 * the corresponding freeblock entry.
905 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200906 if (longest < be16_to_cpu(hdr->bestfree[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 int error; /* error return value */
908 xfs_dabuf_t *fbp; /* freeblock buffer */
909 xfs_dir2_db_t fdb; /* freeblock block number */
910 int findex; /* index in freeblock entries */
911 xfs_dir2_free_t *free; /* freeblock structure */
912 int logfree; /* need to log free entry */
913
914 /*
915 * Convert the data block number to a free block,
916 * read in the free block.
917 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000918 fdb = xfs_dir2_db_to_fdb(mp, db);
919 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 -1, &fbp, XFS_DATA_FORK))) {
921 return error;
922 }
923 free = fbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200924 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Nathan Scott0ba962e2006-03-17 17:27:07 +1100925 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
Christoph Hellwiga00b7742011-07-13 13:43:48 +0200926 xfs_dir2_free_max_bests(mp) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
928 /*
929 * Calculate which entry we need to fix.
930 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000931 findex = xfs_dir2_db_to_fdindex(mp, db);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200932 longest = be16_to_cpu(hdr->bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /*
934 * If the data block is now empty we can get rid of it
935 * (usually).
936 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200937 if (longest == mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /*
939 * Try to punch out the data block.
940 */
941 error = xfs_dir2_shrink_inode(args, db, dbp);
942 if (error == 0) {
943 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200944 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 /*
947 * We can get ENOSPC if there's no space reservation.
948 * In this case just drop the buffer and some one else
949 * will eventually get rid of the empty block.
950 */
951 else if (error == ENOSPC && args->total == 0)
952 xfs_da_buf_done(dbp);
953 else
954 return error;
955 }
956 /*
957 * If we got rid of the data block, we can eliminate that entry
958 * in the free block.
959 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200960 if (hdr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /*
962 * One less used entry in the free table.
963 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800964 be32_add_cpu(&free->hdr.nused, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 xfs_dir2_free_log_header(tp, fbp);
966 /*
967 * If this was the last entry in the table, we can
968 * trim the table size back. There might be other
969 * entries at the end referring to non-existent
970 * data blocks, get those too.
971 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100972 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 int i; /* free entry index */
974
975 for (i = findex - 1;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200976 i >= 0 &&
977 free->bests[i] == cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 i--)
979 continue;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100980 free->hdr.nvalid = cpu_to_be32(i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 logfree = 0;
982 }
983 /*
984 * Not the last entry, just punch it out.
985 */
986 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +1100987 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 logfree = 1;
989 }
990 /*
991 * If there are no useful entries left in the block,
992 * get rid of the block if we can.
993 */
994 if (!free->hdr.nused) {
995 error = xfs_dir2_shrink_inode(args, fdb, fbp);
996 if (error == 0) {
997 fbp = NULL;
998 logfree = 0;
999 } else if (error != ENOSPC || args->total != 0)
1000 return error;
1001 /*
1002 * It's possible to get ENOSPC if there is no
1003 * space reservation. In this case some one
1004 * else will eventually get rid of this block.
1005 */
1006 }
1007 }
1008 /*
1009 * Data block is not empty, just set the free entry to
1010 * the new value.
1011 */
1012 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001013 free->bests[findex] = cpu_to_be16(longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 logfree = 1;
1015 }
1016 /*
1017 * Log the free entry that changed, unless we got rid of it.
1018 */
1019 if (logfree)
1020 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1021 /*
1022 * Drop the buffer if we still have it.
1023 */
1024 if (fbp)
1025 xfs_da_buf_done(fbp);
1026 }
1027 xfs_dir2_leafn_check(dp, bp);
1028 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001029 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 * to justify trying to join it with a neighbor.
1031 */
1032 *rval =
1033 ((uint)sizeof(leaf->hdr) +
1034 (uint)sizeof(leaf->ents[0]) *
Nathan Scotta818e5d2006-03-17 17:28:07 +11001035 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 mp->m_dir_magicpct;
1037 return 0;
1038}
1039
1040/*
1041 * Split the leaf entries in the old block into old and new blocks.
1042 */
1043int /* error */
1044xfs_dir2_leafn_split(
1045 xfs_da_state_t *state, /* btree cursor */
1046 xfs_da_state_blk_t *oldblk, /* original block */
1047 xfs_da_state_blk_t *newblk) /* newly created block */
1048{
1049 xfs_da_args_t *args; /* operation arguments */
1050 xfs_dablk_t blkno; /* new leaf block number */
1051 int error; /* error return value */
1052 xfs_mount_t *mp; /* filesystem mount point */
1053
1054 /*
1055 * Allocate space for a new leaf node.
1056 */
1057 args = state->args;
1058 mp = args->dp->i_mount;
1059 ASSERT(args != NULL);
1060 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1061 error = xfs_da_grow_inode(args, &blkno);
1062 if (error) {
1063 return error;
1064 }
1065 /*
1066 * Initialize the new leaf block.
1067 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001068 error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1070 if (error) {
1071 return error;
1072 }
1073 newblk->blkno = blkno;
1074 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1075 /*
1076 * Rebalance the entries across the two leaves, link the new
1077 * block into the leaves.
1078 */
1079 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1080 error = xfs_da_blk_link(state, oldblk, newblk);
1081 if (error) {
1082 return error;
1083 }
1084 /*
1085 * Insert the new entry in the correct block.
1086 */
1087 if (state->inleaf)
1088 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1089 else
1090 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1091 /*
1092 * Update last hashval in each block since we added the name.
1093 */
1094 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1095 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1096 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1097 xfs_dir2_leafn_check(args->dp, newblk->bp);
1098 return error;
1099}
1100
1101/*
1102 * Check a leaf block and its neighbors to see if the block should be
1103 * collapsed into one or the other neighbor. Always keep the block
1104 * with the smaller block number.
1105 * If the current block is over 50% full, don't try to join it, return 0.
1106 * If the block is empty, fill in the state structure and return 2.
1107 * If it can be collapsed, fill in the state structure and return 1.
1108 * If nothing can be done, return 0.
1109 */
1110int /* error */
1111xfs_dir2_leafn_toosmall(
1112 xfs_da_state_t *state, /* btree cursor */
1113 int *action) /* resulting action to take */
1114{
1115 xfs_da_state_blk_t *blk; /* leaf block */
1116 xfs_dablk_t blkno; /* leaf block number */
1117 xfs_dabuf_t *bp; /* leaf buffer */
1118 int bytes; /* bytes in use */
1119 int count; /* leaf live entry count */
1120 int error; /* error return value */
1121 int forward; /* sibling block direction */
1122 int i; /* sibling counter */
1123 xfs_da_blkinfo_t *info; /* leaf block header */
1124 xfs_dir2_leaf_t *leaf; /* leaf structure */
1125 int rval; /* result from path_shift */
1126
1127 /*
1128 * Check for the degenerate case of the block being over 50% full.
1129 * If so, it's not worth even looking to see if we might be able
1130 * to coalesce with a sibling.
1131 */
1132 blk = &state->path.blk[state->path.active - 1];
1133 info = blk->bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001134 ASSERT(info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001136 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1138 if (bytes > (state->blocksize >> 1)) {
1139 /*
1140 * Blk over 50%, don't try to join.
1141 */
1142 *action = 0;
1143 return 0;
1144 }
1145 /*
1146 * Check for the degenerate case of the block being empty.
1147 * If the block is empty, we'll simply delete it, no need to
1148 * coalesce it with a sibling block. We choose (arbitrarily)
1149 * to merge with the forward block unless it is NULL.
1150 */
1151 if (count == 0) {
1152 /*
1153 * Make altpath point to the block we want to keep and
1154 * path point to the block we want to drop (this one).
1155 */
Nathan Scott89da0542006-03-17 17:28:40 +11001156 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 memcpy(&state->altpath, &state->path, sizeof(state->path));
1158 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1159 &rval);
1160 if (error)
1161 return error;
1162 *action = rval ? 2 : 0;
1163 return 0;
1164 }
1165 /*
1166 * Examine each sibling block to see if we can coalesce with
1167 * at least 25% free space to spare. We need to figure out
1168 * whether to merge with the forward or the backward block.
1169 * We prefer coalescing with the lower numbered sibling so as
1170 * to shrink a directory over time.
1171 */
Nathan Scott89da0542006-03-17 17:28:40 +11001172 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Nathan Scott89da0542006-03-17 17:28:40 +11001174 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (blkno == 0)
1176 continue;
1177 /*
1178 * Read the sibling leaf block.
1179 */
1180 if ((error =
1181 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1182 -1, &bp, XFS_DATA_FORK))) {
1183 return error;
1184 }
1185 ASSERT(bp != NULL);
1186 /*
1187 * Count bytes in the two blocks combined.
1188 */
1189 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001190 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 bytes = state->blocksize - (state->blocksize >> 2);
1192 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001193 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Nathan Scotta818e5d2006-03-17 17:28:07 +11001194 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 bytes -= count * (uint)sizeof(leaf->ents[0]);
1196 /*
1197 * Fits with at least 25% to spare.
1198 */
1199 if (bytes >= 0)
1200 break;
1201 xfs_da_brelse(state->args->trans, bp);
1202 }
1203 /*
1204 * Didn't like either block, give up.
1205 */
1206 if (i >= 2) {
1207 *action = 0;
1208 return 0;
1209 }
1210 /*
1211 * Done with the sibling leaf block here, drop the dabuf
1212 * so path_shift can get it.
1213 */
1214 xfs_da_buf_done(bp);
1215 /*
1216 * Make altpath point to the block we want to keep (the lower
1217 * numbered block) and path point to the block we want to drop.
1218 */
1219 memcpy(&state->altpath, &state->path, sizeof(state->path));
1220 if (blkno < blk->blkno)
1221 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1222 &rval);
1223 else
1224 error = xfs_da_path_shift(state, &state->path, forward, 0,
1225 &rval);
1226 if (error) {
1227 return error;
1228 }
1229 *action = rval ? 0 : 1;
1230 return 0;
1231}
1232
1233/*
1234 * Move all the leaf entries from drop_blk to save_blk.
1235 * This is done as part of a join operation.
1236 */
1237void
1238xfs_dir2_leafn_unbalance(
1239 xfs_da_state_t *state, /* cursor */
1240 xfs_da_state_blk_t *drop_blk, /* dead block */
1241 xfs_da_state_blk_t *save_blk) /* surviving block */
1242{
1243 xfs_da_args_t *args; /* operation arguments */
1244 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1245 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1246
1247 args = state->args;
1248 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1249 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1250 drop_leaf = drop_blk->bp->data;
1251 save_leaf = save_blk->bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001252 ASSERT(drop_leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1253 ASSERT(save_leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /*
1255 * If there are any stale leaf entries, take this opportunity
1256 * to purge them.
1257 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001258 if (drop_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 xfs_dir2_leaf_compact(args, drop_blk->bp);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001260 if (save_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 xfs_dir2_leaf_compact(args, save_blk->bp);
1262 /*
1263 * Move the entries from drop to the appropriate end of save.
1264 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001265 drop_blk->hashval = be32_to_cpu(drop_leaf->ents[be16_to_cpu(drop_leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1267 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001268 be16_to_cpu(drop_leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 else
1270 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001271 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001272 save_blk->hashval = be32_to_cpu(save_leaf->ents[be16_to_cpu(save_leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1274}
1275
1276/*
1277 * Top-level node form directory addname routine.
1278 */
1279int /* error */
1280xfs_dir2_node_addname(
1281 xfs_da_args_t *args) /* operation arguments */
1282{
1283 xfs_da_state_blk_t *blk; /* leaf block for insert */
1284 int error; /* error return value */
1285 int rval; /* sub-return value */
1286 xfs_da_state_t *state; /* btree cursor */
1287
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001288 trace_xfs_dir2_node_addname(args);
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 /*
1291 * Allocate and initialize the state (btree cursor).
1292 */
1293 state = xfs_da_state_alloc();
1294 state->args = args;
1295 state->mp = args->dp->i_mount;
1296 state->blocksize = state->mp->m_dirblksize;
1297 state->node_ents = state->mp->m_dir_node_ents;
1298 /*
1299 * Look up the name. We're not supposed to find it, but
1300 * this gives us the insertion point.
1301 */
1302 error = xfs_da_node_lookup_int(state, &rval);
1303 if (error)
1304 rval = error;
1305 if (rval != ENOENT) {
1306 goto done;
1307 }
1308 /*
1309 * Add the data entry to a data block.
1310 * Extravalid is set to a freeblock found by lookup.
1311 */
1312 rval = xfs_dir2_node_addname_int(args,
1313 state->extravalid ? &state->extrablk : NULL);
1314 if (rval) {
1315 goto done;
1316 }
1317 blk = &state->path.blk[state->path.active - 1];
1318 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1319 /*
1320 * Add the new leaf entry.
1321 */
1322 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1323 if (rval == 0) {
1324 /*
1325 * It worked, fix the hash values up the btree.
1326 */
Barry Naujok6a178102008-05-21 16:42:05 +10001327 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 xfs_da_fixhashpath(state, &state->path);
1329 } else {
1330 /*
1331 * It didn't work, we need to split the leaf block.
1332 */
1333 if (args->total == 0) {
1334 ASSERT(rval == ENOSPC);
1335 goto done;
1336 }
1337 /*
1338 * Split the leaf block and insert the new entry.
1339 */
1340 rval = xfs_da_split(state);
1341 }
1342done:
1343 xfs_da_state_free(state);
1344 return rval;
1345}
1346
1347/*
1348 * Add the data entry for a node-format directory name addition.
1349 * The leaf entry is added in xfs_dir2_leafn_add.
1350 * We may enter with a freespace block that the lookup found.
1351 */
1352static int /* error */
1353xfs_dir2_node_addname_int(
1354 xfs_da_args_t *args, /* operation arguments */
1355 xfs_da_state_blk_t *fblk) /* optional freespace block */
1356{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001357 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 xfs_dir2_db_t dbno; /* data block number */
1359 xfs_dabuf_t *dbp; /* data block buffer */
1360 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1361 xfs_inode_t *dp; /* incore directory inode */
1362 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1363 int error; /* error return value */
1364 xfs_dir2_db_t fbno; /* freespace block number */
1365 xfs_dabuf_t *fbp; /* freespace buffer */
1366 int findex; /* freespace entry index */
1367 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1368 xfs_dir2_db_t ifbno; /* initial freespace block no */
1369 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1370 int length; /* length of the new entry */
1371 int logfree; /* need to log free entry */
1372 xfs_mount_t *mp; /* filesystem mount point */
1373 int needlog; /* need to log data header */
1374 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001375 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 xfs_trans_t *tp; /* transaction pointer */
1377
1378 dp = args->dp;
1379 mp = dp->i_mount;
1380 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001381 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /*
1383 * If we came in with a freespace block that means that lookup
1384 * found an entry with our hash value. This is the freespace
1385 * block for that data entry.
1386 */
1387 if (fblk) {
1388 fbp = fblk->bp;
1389 /*
1390 * Remember initial freespace block number.
1391 */
1392 ifbno = fblk->blkno;
1393 free = fbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001394 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 findex = fblk->index;
1396 /*
1397 * This means the free entry showed that the data block had
1398 * space for our entry, so we remembered it.
1399 * Use that data block.
1400 */
1401 if (findex >= 0) {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001402 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1403 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1404 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1405 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407 /*
1408 * The data block looked at didn't have enough room.
1409 * We'll start at the beginning of the freespace entries.
1410 */
1411 else {
1412 dbno = -1;
1413 findex = 0;
1414 }
1415 }
1416 /*
1417 * Didn't come in with a freespace block, so don't have a data block.
1418 */
1419 else {
1420 ifbno = dbno = -1;
1421 fbp = NULL;
1422 findex = 0;
1423 }
1424 /*
1425 * If we don't have a data block yet, we're going to scan the
1426 * freespace blocks looking for one. Figure out what the
1427 * highest freespace block number is.
1428 */
1429 if (dbno == -1) {
1430 xfs_fileoff_t fo; /* freespace block number */
1431
1432 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1433 return error;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001434 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 fbno = ifbno;
1436 }
1437 /*
1438 * While we haven't identified a data block, search the freeblock
1439 * data for a good data block. If we find a null freeblock entry,
1440 * indicating a hole in the data blocks, remember that.
1441 */
1442 while (dbno == -1) {
1443 /*
1444 * If we don't have a freeblock in hand, get the next one.
1445 */
1446 if (fbp == NULL) {
1447 /*
1448 * Happens the first time through unless lookup gave
1449 * us a freespace block to start with.
1450 */
1451 if (++fbno == 0)
1452 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1453 /*
1454 * If it's ifbno we already looked at it.
1455 */
1456 if (fbno == ifbno)
1457 fbno++;
1458 /*
1459 * If it's off the end we're done.
1460 */
1461 if (fbno >= lastfbno)
1462 break;
1463 /*
1464 * Read the block. There can be holes in the
1465 * freespace blocks, so this might not succeed.
1466 * This should be really rare, so there's no reason
1467 * to avoid it.
1468 */
1469 if ((error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001470 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 XFS_DATA_FORK))) {
1472 return error;
1473 }
1474 if (unlikely(fbp == NULL)) {
1475 continue;
1476 }
1477 free = fbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001478 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 findex = 0;
1480 }
1481 /*
1482 * Look at the current free entry. Is it good enough?
1483 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001484 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1485 be16_to_cpu(free->bests[findex]) >= length)
1486 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 else {
1488 /*
1489 * Are we done with the freeblock?
1490 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001491 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 /*
1493 * Drop the block.
1494 */
1495 xfs_da_brelse(tp, fbp);
1496 fbp = NULL;
1497 if (fblk && fblk->bp)
1498 fblk->bp = NULL;
1499 }
1500 }
1501 }
1502 /*
1503 * If we don't have a data block, we need to allocate one and make
1504 * the freespace entries refer to it.
1505 */
1506 if (unlikely(dbno == -1)) {
1507 /*
1508 * Not allowed to allocate, return failure.
1509 */
Barry Naujok6a178102008-05-21 16:42:05 +10001510 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
1511 args->total == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 /*
1513 * Drop the freespace buffer unless it came from our
1514 * caller.
1515 */
1516 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1517 xfs_da_buf_done(fbp);
1518 return XFS_ERROR(ENOSPC);
1519 }
1520 /*
1521 * Allocate and initialize the new data block.
1522 */
1523 if (unlikely((error = xfs_dir2_grow_inode(args,
1524 XFS_DIR2_DATA_SPACE,
1525 &dbno)) ||
1526 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1527 /*
1528 * Drop the freespace buffer unless it came from our
1529 * caller.
1530 */
1531 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1532 xfs_da_buf_done(fbp);
1533 return error;
1534 }
1535 /*
1536 * If (somehow) we have a freespace block, get rid of it.
1537 */
1538 if (fbp)
1539 xfs_da_brelse(tp, fbp);
1540 if (fblk && fblk->bp)
1541 fblk->bp = NULL;
1542
1543 /*
1544 * Get the freespace block corresponding to the data block
1545 * that was just allocated.
1546 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001547 fbno = xfs_dir2_db_to_fdb(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (unlikely(error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001549 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 XFS_DATA_FORK))) {
1551 xfs_da_buf_done(dbp);
1552 return error;
1553 }
1554 /*
1555 * If there wasn't a freespace block, the read will
1556 * return a NULL fbp. Allocate and initialize a new one.
1557 */
1558 if( fbp == NULL ) {
1559 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1560 &fbno))) {
1561 return error;
1562 }
1563
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001564 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001565 xfs_alert(mp,
Jean Delvaredf2e3012011-07-16 18:10:35 +02001566 "%s: dir ino %llu needed freesp block %lld for\n"
Dave Chinner0b932cc2011-03-07 10:08:35 +11001567 " data block %lld, got %lld ifbno %llu lastfbno %d",
1568 __func__, (unsigned long long)dp->i_ino,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001569 (long long)xfs_dir2_db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 (long long)dbno, (long long)fbno,
1571 (unsigned long long)ifbno, lastfbno);
1572 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001573 xfs_alert(mp,
1574 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 fblk,
1576 (unsigned long long)fblk->blkno,
1577 fblk->index,
1578 fblk->magic);
1579 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001580 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1583 XFS_ERRLEVEL_LOW, mp);
1584 return XFS_ERROR(EFSCORRUPTED);
1585 }
1586
1587 /*
1588 * Get a buffer for the new block.
1589 */
1590 if ((error = xfs_da_get_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001591 xfs_dir2_db_to_da(mp, fbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 -1, &fbp, XFS_DATA_FORK))) {
1593 return error;
1594 }
1595 ASSERT(fbp != NULL);
1596
1597 /*
1598 * Initialize the new block to be empty, and remember
1599 * its first slot as our empty slot.
1600 */
1601 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001602 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1603 free->hdr.firstdb = cpu_to_be32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
Christoph Hellwiga00b7742011-07-13 13:43:48 +02001605 xfs_dir2_free_max_bests(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 free->hdr.nvalid = 0;
1607 free->hdr.nused = 0;
1608 } else {
1609 free = fbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001610 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612
1613 /*
1614 * Set the freespace block index from the data block number.
1615 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001616 findex = xfs_dir2_db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 /*
1618 * If it's after the end of the current entries in the
1619 * freespace block, extend that table.
1620 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001621 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
Christoph Hellwiga00b7742011-07-13 13:43:48 +02001622 ASSERT(findex < xfs_dir2_free_max_bests(mp));
Nathan Scott0ba962e2006-03-17 17:27:07 +11001623 free->hdr.nvalid = cpu_to_be32(findex + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /*
1625 * Tag new entry so nused will go up.
1626 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001627 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629 /*
1630 * If this entry was for an empty data block
1631 * (this should always be true) then update the header.
1632 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001633 if (free->bests[findex] == cpu_to_be16(NULLDATAOFF)) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001634 be32_add_cpu(&free->hdr.nused, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 xfs_dir2_free_log_header(tp, fbp);
1636 }
1637 /*
1638 * Update the real value in the table.
1639 * We haven't allocated the data entry yet so this will
1640 * change again.
1641 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001642 hdr = dbp->data;
1643 free->bests[findex] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 logfree = 1;
1645 }
1646 /*
1647 * We had a data block so we don't have to make a new one.
1648 */
1649 else {
1650 /*
1651 * If just checking, we succeeded.
1652 */
Barry Naujok6a178102008-05-21 16:42:05 +10001653 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1655 xfs_da_buf_done(fbp);
1656 return 0;
1657 }
1658 /*
1659 * Read the data block in.
1660 */
1661 if (unlikely(
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001662 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 -1, &dbp, XFS_DATA_FORK))) {
1664 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1665 xfs_da_buf_done(fbp);
1666 return error;
1667 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001668 hdr = dbp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 logfree = 0;
1670 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001671 ASSERT(be16_to_cpu(hdr->bestfree[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 /*
1673 * Point to the existing unused space.
1674 */
1675 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001676 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 needscan = needlog = 0;
1678 /*
1679 * Mark the first part of the unused space, inuse for us.
1680 */
1681 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001682 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 &needlog, &needscan);
1684 /*
1685 * Fill in the new entry and log it.
1686 */
1687 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001688 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 dep->namelen = args->namelen;
1690 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001691 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001692 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 xfs_dir2_data_log_entry(tp, dbp, dep);
1694 /*
1695 * Rescan the block for bestfree if needed.
1696 */
1697 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001698 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /*
1700 * Log the data block header if needed.
1701 */
1702 if (needlog)
1703 xfs_dir2_data_log_header(tp, dbp);
1704 /*
1705 * If the freespace entry is now wrong, update it.
1706 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001707 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(hdr->bestfree[0].length)) {
1708 free->bests[findex] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 logfree = 1;
1710 }
1711 /*
1712 * Log the freespace entry if needed.
1713 */
1714 if (logfree)
1715 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1716 /*
1717 * If the caller didn't hand us the freespace block, drop it.
1718 */
1719 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1720 xfs_da_buf_done(fbp);
1721 /*
1722 * Return the data block and offset in args, then drop the data block.
1723 */
1724 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001725 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 xfs_da_buf_done(dbp);
1727 return 0;
1728}
1729
1730/*
1731 * Lookup an entry in a node-format directory.
1732 * All the real work happens in xfs_da_node_lookup_int.
1733 * The only real output is the inode number of the entry.
1734 */
1735int /* error */
1736xfs_dir2_node_lookup(
1737 xfs_da_args_t *args) /* operation arguments */
1738{
1739 int error; /* error return value */
1740 int i; /* btree level */
1741 int rval; /* operation return value */
1742 xfs_da_state_t *state; /* btree cursor */
1743
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001744 trace_xfs_dir2_node_lookup(args);
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 /*
1747 * Allocate and initialize the btree cursor.
1748 */
1749 state = xfs_da_state_alloc();
1750 state->args = args;
1751 state->mp = args->dp->i_mount;
1752 state->blocksize = state->mp->m_dirblksize;
1753 state->node_ents = state->mp->m_dir_node_ents;
1754 /*
1755 * Fill in the path to the entry in the cursor.
1756 */
1757 error = xfs_da_node_lookup_int(state, &rval);
1758 if (error)
1759 rval = error;
Barry Naujok384f3ce2008-05-21 16:58:22 +10001760 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
1761 /* If a CI match, dup the actual name and return EEXIST */
1762 xfs_dir2_data_entry_t *dep;
1763
1764 dep = (xfs_dir2_data_entry_t *)((char *)state->extrablk.bp->
1765 data + state->extrablk.index);
1766 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 /*
1769 * Release the btree blocks and leaf block.
1770 */
1771 for (i = 0; i < state->path.active; i++) {
1772 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1773 state->path.blk[i].bp = NULL;
1774 }
1775 /*
1776 * Release the data block if we have it.
1777 */
1778 if (state->extravalid && state->extrablk.bp) {
1779 xfs_da_brelse(args->trans, state->extrablk.bp);
1780 state->extrablk.bp = NULL;
1781 }
1782 xfs_da_state_free(state);
1783 return rval;
1784}
1785
1786/*
1787 * Remove an entry from a node-format directory.
1788 */
1789int /* error */
1790xfs_dir2_node_removename(
1791 xfs_da_args_t *args) /* operation arguments */
1792{
1793 xfs_da_state_blk_t *blk; /* leaf block */
1794 int error; /* error return value */
1795 int rval; /* operation return value */
1796 xfs_da_state_t *state; /* btree cursor */
1797
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001798 trace_xfs_dir2_node_removename(args);
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 /*
1801 * Allocate and initialize the btree cursor.
1802 */
1803 state = xfs_da_state_alloc();
1804 state->args = args;
1805 state->mp = args->dp->i_mount;
1806 state->blocksize = state->mp->m_dirblksize;
1807 state->node_ents = state->mp->m_dir_node_ents;
1808 /*
1809 * Look up the entry we're deleting, set up the cursor.
1810 */
1811 error = xfs_da_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001812 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /*
1815 * Didn't find it, upper layer screwed up.
1816 */
1817 if (rval != EEXIST) {
1818 xfs_da_state_free(state);
1819 return rval;
1820 }
1821 blk = &state->path.blk[state->path.active - 1];
1822 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1823 ASSERT(state->extravalid);
1824 /*
1825 * Remove the leaf and data entries.
1826 * Extrablk refers to the data block.
1827 */
1828 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1829 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001830 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /*
1833 * Fix the hash values up the btree.
1834 */
1835 xfs_da_fixhashpath(state, &state->path);
1836 /*
1837 * If we need to join leaf blocks, do it.
1838 */
1839 if (rval && state->path.active > 1)
1840 error = xfs_da_join(state);
1841 /*
1842 * If no errors so far, try conversion to leaf format.
1843 */
1844 if (!error)
1845 error = xfs_dir2_node_to_leaf(state);
1846 xfs_da_state_free(state);
1847 return error;
1848}
1849
1850/*
1851 * Replace an entry's inode number in a node-format directory.
1852 */
1853int /* error */
1854xfs_dir2_node_replace(
1855 xfs_da_args_t *args) /* operation arguments */
1856{
1857 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001858 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 xfs_dir2_data_entry_t *dep; /* data entry changed */
1860 int error; /* error return value */
1861 int i; /* btree level */
1862 xfs_ino_t inum; /* new inode number */
1863 xfs_dir2_leaf_t *leaf; /* leaf structure */
1864 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1865 int rval; /* internal return value */
1866 xfs_da_state_t *state; /* btree cursor */
1867
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001868 trace_xfs_dir2_node_replace(args);
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /*
1871 * Allocate and initialize the btree cursor.
1872 */
1873 state = xfs_da_state_alloc();
1874 state->args = args;
1875 state->mp = args->dp->i_mount;
1876 state->blocksize = state->mp->m_dirblksize;
1877 state->node_ents = state->mp->m_dir_node_ents;
1878 inum = args->inumber;
1879 /*
1880 * Lookup the entry to change in the btree.
1881 */
1882 error = xfs_da_node_lookup_int(state, &rval);
1883 if (error) {
1884 rval = error;
1885 }
1886 /*
1887 * It should be found, since the vnodeops layer has looked it up
1888 * and locked it. But paranoia is good.
1889 */
1890 if (rval == EEXIST) {
1891 /*
1892 * Find the leaf entry.
1893 */
1894 blk = &state->path.blk[state->path.active - 1];
1895 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1896 leaf = blk->bp->data;
1897 lep = &leaf->ents[blk->index];
1898 ASSERT(state->extravalid);
1899 /*
1900 * Point to the data entry.
1901 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001902 hdr = state->extrablk.bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001903 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001905 ((char *)hdr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001906 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001907 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 /*
1909 * Fill in the new inode number and log the entry.
1910 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001911 dep->inumber = cpu_to_be64(inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1913 rval = 0;
1914 }
1915 /*
1916 * Didn't find it, and we're holding a data block. Drop it.
1917 */
1918 else if (state->extravalid) {
1919 xfs_da_brelse(args->trans, state->extrablk.bp);
1920 state->extrablk.bp = NULL;
1921 }
1922 /*
1923 * Release all the buffers in the cursor.
1924 */
1925 for (i = 0; i < state->path.active; i++) {
1926 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1927 state->path.blk[i].bp = NULL;
1928 }
1929 xfs_da_state_free(state);
1930 return rval;
1931}
1932
1933/*
1934 * Trim off a trailing empty freespace block.
1935 * Return (in rvalp) 1 if we did it, 0 if not.
1936 */
1937int /* error */
1938xfs_dir2_node_trim_free(
1939 xfs_da_args_t *args, /* operation arguments */
1940 xfs_fileoff_t fo, /* free block number */
1941 int *rvalp) /* out: did something */
1942{
1943 xfs_dabuf_t *bp; /* freespace buffer */
1944 xfs_inode_t *dp; /* incore directory inode */
1945 int error; /* error return code */
1946 xfs_dir2_free_t *free; /* freespace structure */
1947 xfs_mount_t *mp; /* filesystem mount point */
1948 xfs_trans_t *tp; /* transaction pointer */
1949
1950 dp = args->dp;
1951 mp = dp->i_mount;
1952 tp = args->trans;
1953 /*
1954 * Read the freespace block.
1955 */
1956 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
1957 XFS_DATA_FORK))) {
1958 return error;
1959 }
1960
1961 /*
1962 * There can be holes in freespace. If fo is a hole, there's
1963 * nothing to do.
1964 */
1965 if (bp == NULL) {
1966 return 0;
1967 }
1968 free = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001969 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 /*
1971 * If there are used entries, there's nothing to do.
1972 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001973 if (be32_to_cpu(free->hdr.nused) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 xfs_da_brelse(tp, bp);
1975 *rvalp = 0;
1976 return 0;
1977 }
1978 /*
1979 * Blow the block away.
1980 */
1981 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001982 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 bp))) {
1984 /*
1985 * Can't fail with ENOSPC since that only happens with no
1986 * space reservation, when breaking up an extent into two
1987 * pieces. This is the last block of an extent.
1988 */
1989 ASSERT(error != ENOSPC);
1990 xfs_da_brelse(tp, bp);
1991 return error;
1992 }
1993 /*
1994 * Return that we succeeded.
1995 */
1996 *rvalp = 1;
1997 return 0;
1998}