blob: 8d4b54c1bedbd42261aecc440121392bf907244d [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"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dir2_sf.h"
31#include "xfs_dinode.h"
32#include "xfs_inode.h"
33#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_data.h"
35#include "xfs_dir2_leaf.h"
36#include "xfs_dir2_block.h"
37#include "xfs_dir2_node.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000039#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * Function declarations.
43 */
44static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp);
45static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index);
46#ifdef DEBUG
47static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
48#else
49#define xfs_dir2_leafn_check(dp, bp)
50#endif
51static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s,
52 int start_s, xfs_dabuf_t *bp_d, int start_d,
53 int count);
54static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
55 xfs_da_state_blk_t *blk1,
56 xfs_da_state_blk_t *blk2);
57static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp,
58 int index, xfs_da_state_blk_t *dblk,
59 int *rval);
60static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
61 xfs_da_state_blk_t *fblk);
62
63/*
64 * Log entries from a freespace block.
65 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +000066STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -070067xfs_dir2_free_log_bests(
68 xfs_trans_t *tp, /* transaction pointer */
69 xfs_dabuf_t *bp, /* freespace buffer */
70 int first, /* first entry to log */
71 int last) /* last entry to log */
72{
73 xfs_dir2_free_t *free; /* freespace structure */
74
75 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +110076 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 xfs_da_log_buf(tp, bp,
78 (uint)((char *)&free->bests[first] - (char *)free),
79 (uint)((char *)&free->bests[last] - (char *)free +
80 sizeof(free->bests[0]) - 1));
81}
82
83/*
84 * Log header from a freespace block.
85 */
86static void
87xfs_dir2_free_log_header(
88 xfs_trans_t *tp, /* transaction pointer */
89 xfs_dabuf_t *bp) /* freespace buffer */
90{
91 xfs_dir2_free_t *free; /* freespace structure */
92
93 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +110094 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
96 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
97}
98
99/*
100 * Convert a leaf-format directory to a node-format directory.
101 * We need to change the magic number of the leaf block, and copy
102 * the freespace table out of the leaf block into its own block.
103 */
104int /* error */
105xfs_dir2_leaf_to_node(
106 xfs_da_args_t *args, /* operation arguments */
107 xfs_dabuf_t *lbp) /* leaf buffer */
108{
109 xfs_inode_t *dp; /* incore directory inode */
110 int error; /* error return value */
111 xfs_dabuf_t *fbp; /* freespace buffer */
112 xfs_dir2_db_t fdb; /* freespace block number */
113 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100114 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int i; /* leaf freespace index */
116 xfs_dir2_leaf_t *leaf; /* leaf structure */
117 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
118 xfs_mount_t *mp; /* filesystem mount point */
119 int n; /* count of live freespc ents */
120 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100121 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 xfs_trans_t *tp; /* transaction pointer */
123
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000124 trace_xfs_dir2_leaf_to_node(args);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 dp = args->dp;
127 mp = dp->i_mount;
128 tp = args->trans;
129 /*
130 * Add a freespace block to the directory.
131 */
132 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
133 return error;
134 }
135 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
136 /*
137 * Get the buffer for the new freespace block.
138 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000139 if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 XFS_DATA_FORK))) {
141 return error;
142 }
143 ASSERT(fbp != NULL);
144 free = fbp->data;
145 leaf = lbp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000146 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /*
148 * Initialize the freespace block header.
149 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100150 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 free->hdr.firstdb = 0;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100152 ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100153 free->hdr.nvalid = ltp->bestcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /*
155 * Copy freespace entries from the leaf block to the new block.
156 * Count active entries.
157 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000158 for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100159 i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100160 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100162 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Nathan Scott0ba962e2006-03-17 17:27:07 +1100164 free->hdr.nused = cpu_to_be32(n);
Nathan Scott89da0542006-03-17 17:28:40 +1100165 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /*
167 * Log everything.
168 */
169 xfs_dir2_leaf_log_header(tp, lbp);
170 xfs_dir2_free_log_header(tp, fbp);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100171 xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 xfs_da_buf_done(fbp);
173 xfs_dir2_leafn_check(dp, lbp);
174 return 0;
175}
176
177/*
178 * Add a leaf entry to a leaf block in a node-form directory.
179 * The other work necessary is done from the caller.
180 */
181static int /* error */
182xfs_dir2_leafn_add(
183 xfs_dabuf_t *bp, /* leaf buffer */
184 xfs_da_args_t *args, /* operation arguments */
185 int index) /* insertion pt for new entry */
186{
187 int compact; /* compacting stale leaves */
188 xfs_inode_t *dp; /* incore directory inode */
189 int highstale; /* next stale entry */
190 xfs_dir2_leaf_t *leaf; /* leaf structure */
191 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
192 int lfloghigh; /* high leaf entry logging */
193 int lfloglow; /* low leaf entry logging */
194 int lowstale; /* previous stale entry */
195 xfs_mount_t *mp; /* filesystem mount point */
196 xfs_trans_t *tp; /* transaction pointer */
197
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000198 trace_xfs_dir2_leafn_add(args, index);
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 dp = args->dp;
201 mp = dp->i_mount;
202 tp = args->trans;
203 leaf = bp->data;
204
205 /*
206 * Quick check just to make sure we are not going to index
207 * into other peoples memory
208 */
209 if (index < 0)
210 return XFS_ERROR(EFSCORRUPTED);
211
212 /*
213 * If there are already the maximum number of leaf entries in
214 * the block, if there are no stale entries it won't fit.
215 * Caller will do a split. If there are stale entries we'll do
216 * a compact.
217 */
218
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000219 if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!leaf->hdr.stale)
221 return XFS_ERROR(ENOSPC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100222 compact = be16_to_cpu(leaf->hdr.stale) > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 } else
224 compact = 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100225 ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100226 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100227 be32_to_cpu(leaf->ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Barry Naujok6a178102008-05-21 16:42:05 +1000229 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0;
231
232 /*
233 * Compact out all but one stale leaf entry. Leaves behind
234 * the entry closest to index.
235 */
236 if (compact) {
237 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
238 &lfloglow, &lfloghigh);
239 }
240 /*
241 * Set impossible logging indices for this case.
242 */
243 else if (leaf->hdr.stale) {
Nathan Scotta818e5d2006-03-17 17:28:07 +1100244 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 lfloghigh = -1;
246 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /*
249 * Insert the new entry, log everything.
250 */
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200251 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
252 highstale, &lfloglow, &lfloghigh);
253
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100254 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000255 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100256 args->blkno, args->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 xfs_dir2_leaf_log_header(tp, bp);
258 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
259 xfs_dir2_leafn_check(dp, bp);
260 return 0;
261}
262
263#ifdef DEBUG
264/*
265 * Check internal consistency of a leafn block.
266 */
267void
268xfs_dir2_leafn_check(
269 xfs_inode_t *dp, /* incore directory inode */
270 xfs_dabuf_t *bp) /* leaf buffer */
271{
272 int i; /* leaf index */
273 xfs_dir2_leaf_t *leaf; /* leaf structure */
274 xfs_mount_t *mp; /* filesystem mount point */
275 int stale; /* count of stale leaves */
276
277 leaf = bp->data;
278 mp = dp->i_mount;
Nathan Scott89da0542006-03-17 17:28:40 +1100279 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000280 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100281 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
282 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100283 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
284 be32_to_cpu(leaf->ents[i + 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100286 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 stale++;
288 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100289 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291#endif /* DEBUG */
292
293/*
294 * Return the last hash value in the leaf.
295 * Stale entries are ok.
296 */
297xfs_dahash_t /* hash value */
298xfs_dir2_leafn_lasthash(
299 xfs_dabuf_t *bp, /* leaf buffer */
300 int *count) /* count of entries in leaf */
301{
302 xfs_dir2_leaf_t *leaf; /* leaf structure */
303
304 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100305 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (count)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100307 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (!leaf->hdr.count)
309 return 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100310 return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000314 * Look up a leaf entry for space to add a name in a node-format leaf block.
315 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000317STATIC int
318xfs_dir2_leafn_lookup_for_addname(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 xfs_dabuf_t *bp, /* leaf buffer */
320 xfs_da_args_t *args, /* operation arguments */
321 int *indexp, /* out: leaf entry index */
322 xfs_da_state_t *state) /* state to fill in */
323{
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000324 xfs_dabuf_t *curbp = NULL; /* current data/free buffer */
325 xfs_dir2_db_t curdb = -1; /* current data block number */
326 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 xfs_inode_t *dp; /* incore directory inode */
328 int error; /* error return value */
329 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000330 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 int index; /* leaf entry index */
332 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000333 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
335 xfs_mount_t *mp; /* filesystem mount point */
336 xfs_dir2_db_t newdb; /* new data block number */
337 xfs_dir2_db_t newfdb; /* new free block number */
338 xfs_trans_t *tp; /* transaction pointer */
339
340 dp = args->dp;
341 tp = args->trans;
342 mp = dp->i_mount;
343 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100344 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345#ifdef __KERNEL__
Nathan Scotta818e5d2006-03-17 17:28:07 +1100346 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347#endif
348 xfs_dir2_leafn_check(dp, bp);
349 /*
350 * Look up the hash value in the leaf entries.
351 */
352 index = xfs_dir2_leaf_search_hash(args, bp);
353 /*
354 * Do we have a buffer coming in?
355 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000356 if (state->extravalid) {
357 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000359 curfdb = state->extrablk.blkno;
360 free = curbp->data;
361 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000363 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
365 * Loop over leaf entries with the right hash value.
366 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000367 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
368 be32_to_cpu(lep->hashval) == args->hashval;
369 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /*
371 * Skip stale leaf entries.
372 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100373 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 continue;
375 /*
376 * Pull the data block number from the entry.
377 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000378 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /*
380 * For addname, we're looking for a place to put the new entry.
381 * We want to use a data block with an entry of equal
382 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000383 *
384 * If this block isn't the data block we already have
385 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000387 if (newdb != curdb) {
388 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000390 * Convert the data block to the free block
391 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000393 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000395 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000397 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000399 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 */
401 if (curbp)
402 xfs_da_brelse(tp, curbp);
403 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000404 * Read the free block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000406 error = xfs_da_read_buf(tp, dp,
407 xfs_dir2_db_to_da(mp, newfdb),
408 -1, &curbp, XFS_DATA_FORK);
409 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return error;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000411 free = curbp->data;
412 ASSERT(be32_to_cpu(free->hdr.magic) ==
413 XFS_DIR2_FREE_MAGIC);
414 ASSERT((be32_to_cpu(free->hdr.firstdb) %
415 XFS_DIR2_MAX_FREE_BESTS(mp)) == 0);
416 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
417 ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) +
418 be32_to_cpu(free->hdr.nvalid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000421 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000423 fi = xfs_dir2_db_to_fdindex(mp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000425 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000427 if (unlikely(be16_to_cpu(free->bests[fi]) == NULLDATAOFF)) {
428 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
429 XFS_ERRLEVEL_LOW, mp);
430 if (curfdb != newfdb)
431 xfs_da_brelse(tp, curbp);
432 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000434 curfdb = newfdb;
435 if (be16_to_cpu(free->bests[fi]) >= length)
436 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000439 /* Didn't find any space */
440 fi = -1;
441out:
Barry Naujok6a178102008-05-21 16:42:05 +1000442 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000443 if (curbp) {
444 /* Giving back a free block. */
445 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000447 state->extrablk.index = fi;
448 state->extrablk.blkno = curfdb;
449 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
450 } else {
451 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000454 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
456 *indexp = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return XFS_ERROR(ENOENT);
458}
459
460/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000461 * Look up a leaf entry in a node-format leaf block.
462 * The extrablk in state a data block.
463 */
464STATIC int
465xfs_dir2_leafn_lookup_for_entry(
466 xfs_dabuf_t *bp, /* leaf buffer */
467 xfs_da_args_t *args, /* operation arguments */
468 int *indexp, /* out: leaf entry index */
469 xfs_da_state_t *state) /* state to fill in */
470{
471 xfs_dabuf_t *curbp = NULL; /* current data/free buffer */
472 xfs_dir2_db_t curdb = -1; /* current data block number */
473 xfs_dir2_data_entry_t *dep; /* data block entry */
474 xfs_inode_t *dp; /* incore directory inode */
475 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000476 int index; /* leaf entry index */
477 xfs_dir2_leaf_t *leaf; /* leaf structure */
478 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
479 xfs_mount_t *mp; /* filesystem mount point */
480 xfs_dir2_db_t newdb; /* new data block number */
481 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000482 enum xfs_dacmp cmp; /* comparison result */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000483
484 dp = args->dp;
485 tp = args->trans;
486 mp = dp->i_mount;
487 leaf = bp->data;
488 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
489#ifdef __KERNEL__
490 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
491#endif
492 xfs_dir2_leafn_check(dp, bp);
493 /*
494 * Look up the hash value in the leaf entries.
495 */
496 index = xfs_dir2_leaf_search_hash(args, bp);
497 /*
498 * Do we have a buffer coming in?
499 */
500 if (state->extravalid) {
501 curbp = state->extrablk.bp;
502 curdb = state->extrablk.blkno;
503 }
504 /*
505 * Loop over leaf entries with the right hash value.
506 */
507 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
508 be32_to_cpu(lep->hashval) == args->hashval;
509 lep++, index++) {
510 /*
511 * Skip stale leaf entries.
512 */
513 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
514 continue;
515 /*
516 * Pull the data block number from the entry.
517 */
518 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
519 /*
520 * Not adding a new entry, so we really want to find
521 * the name given to us.
522 *
523 * If it's a different data block, go get it.
524 */
525 if (newdb != curdb) {
526 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000527 * If we had a block before that we aren't saving
528 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000529 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000530 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
531 curdb != state->extrablk.blkno))
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000532 xfs_da_brelse(tp, curbp);
533 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000534 * If needing the block that is saved with a CI match,
535 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000536 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000537 if (args->cmpresult != XFS_CMP_DIFFERENT &&
538 newdb == state->extrablk.blkno) {
539 ASSERT(state->extravalid);
540 curbp = state->extrablk.bp;
541 } else {
542 error = xfs_da_read_buf(tp, dp,
543 xfs_dir2_db_to_da(mp, newdb),
544 -1, &curbp, XFS_DATA_FORK);
545 if (error)
546 return error;
547 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000548 xfs_dir2_data_check(dp, curbp);
549 curdb = newdb;
550 }
551 /*
552 * Point to the data entry.
553 */
554 dep = (xfs_dir2_data_entry_t *)((char *)curbp->data +
555 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
556 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000557 * Compare the entry and if it's an exact match, return
558 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000559 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000560 */
Barry Naujok5163f952008-05-21 16:41:01 +1000561 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
562 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000563 /* If there is a CI match block, drop it */
564 if (args->cmpresult != XFS_CMP_DIFFERENT &&
565 curdb != state->extrablk.blkno)
566 xfs_da_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000567 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000568 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000569 *indexp = index;
570 state->extravalid = 1;
571 state->extrablk.bp = curbp;
572 state->extrablk.blkno = curdb;
573 state->extrablk.index = (int)((char *)dep -
574 (char *)curbp->data);
575 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Barry Naujok5163f952008-05-21 16:41:01 +1000576 if (cmp == XFS_CMP_EXACT)
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000577 return XFS_ERROR(EEXIST);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000578 }
579 }
Barry Naujok6a178102008-05-21 16:42:05 +1000580 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
581 (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000582 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000583 if (args->cmpresult == XFS_CMP_DIFFERENT) {
584 /* Giving back last used data block. */
585 state->extravalid = 1;
586 state->extrablk.bp = curbp;
587 state->extrablk.index = -1;
588 state->extrablk.blkno = curdb;
589 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
590 } else {
591 /* If the curbp is not the CI match block, drop it */
592 if (state->extrablk.bp != curbp)
593 xfs_da_brelse(tp, curbp);
594 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000595 } else {
596 state->extravalid = 0;
597 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000598 *indexp = index;
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000599 return XFS_ERROR(ENOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000600}
601
602/*
603 * Look up a leaf entry in a node-format leaf block.
604 * If this is an addname then the extrablk in state is a freespace block,
605 * otherwise it's a data block.
606 */
607int
608xfs_dir2_leafn_lookup_int(
609 xfs_dabuf_t *bp, /* leaf buffer */
610 xfs_da_args_t *args, /* operation arguments */
611 int *indexp, /* out: leaf entry index */
612 xfs_da_state_t *state) /* state to fill in */
613{
Barry Naujok6a178102008-05-21 16:42:05 +1000614 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000615 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
616 state);
617 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
618}
619
620/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 * Move count leaf entries from source to destination leaf.
622 * Log entries and headers. Stale entries are preserved.
623 */
624static void
625xfs_dir2_leafn_moveents(
626 xfs_da_args_t *args, /* operation arguments */
627 xfs_dabuf_t *bp_s, /* source leaf buffer */
628 int start_s, /* source leaf index */
629 xfs_dabuf_t *bp_d, /* destination leaf buffer */
630 int start_d, /* destination leaf index */
631 int count) /* count of leaves to copy */
632{
633 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
634 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
635 int stale; /* count stale leaves copied */
636 xfs_trans_t *tp; /* transaction pointer */
637
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000638 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /*
641 * Silently return if nothing to do.
642 */
643 if (count == 0) {
644 return;
645 }
646 tp = args->trans;
647 leaf_s = bp_s->data;
648 leaf_d = bp_d->data;
649 /*
650 * If the destination index is not the end of the current
651 * destination leaf entries, open up a hole in the destination
652 * to hold the new entries.
653 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100654 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
Nathan Scotta818e5d2006-03-17 17:28:07 +1100656 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 sizeof(xfs_dir2_leaf_entry_t));
658 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100659 count + be16_to_cpu(leaf_d->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661 /*
662 * If the source has stale leaves, count the ones in the copy range
663 * so we can update the header correctly.
664 */
665 if (leaf_s->hdr.stale) {
666 int i; /* temp leaf index */
667
668 for (i = start_s, stale = 0; i < start_s + count; i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100669 if (be32_to_cpu(leaf_s->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 stale++;
671 }
672 } else
673 stale = 0;
674 /*
675 * Copy the leaf entries from source to destination.
676 */
677 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
678 count * sizeof(xfs_dir2_leaf_entry_t));
679 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
680 /*
681 * If there are source entries after the ones we copied,
682 * delete the ones we copied by sliding the next ones down.
683 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100684 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
686 count * sizeof(xfs_dir2_leaf_entry_t));
687 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
688 }
689 /*
690 * Update the headers and log them.
691 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800692 be16_add_cpu(&leaf_s->hdr.count, -(count));
693 be16_add_cpu(&leaf_s->hdr.stale, -(stale));
694 be16_add_cpu(&leaf_d->hdr.count, count);
695 be16_add_cpu(&leaf_d->hdr.stale, stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 xfs_dir2_leaf_log_header(tp, bp_s);
697 xfs_dir2_leaf_log_header(tp, bp_d);
698 xfs_dir2_leafn_check(args->dp, bp_s);
699 xfs_dir2_leafn_check(args->dp, bp_d);
700}
701
702/*
703 * Determine the sort order of two leaf blocks.
704 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
705 */
706int /* sort order */
707xfs_dir2_leafn_order(
708 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
709 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
710{
711 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
712 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
713
714 leaf1 = leaf1_bp->data;
715 leaf2 = leaf2_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100716 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
717 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100718 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
719 be16_to_cpu(leaf2->hdr.count) > 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100720 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
721 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
722 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return 1;
724 return 0;
725}
726
727/*
728 * Rebalance leaf entries between two leaf blocks.
729 * This is actually only called when the second block is new,
730 * though the code deals with the general case.
731 * A new entry will be inserted in one of the blocks, and that
732 * entry is taken into account when balancing.
733 */
734static void
735xfs_dir2_leafn_rebalance(
736 xfs_da_state_t *state, /* btree cursor */
737 xfs_da_state_blk_t *blk1, /* first btree block */
738 xfs_da_state_blk_t *blk2) /* second btree block */
739{
740 xfs_da_args_t *args; /* operation arguments */
741 int count; /* count (& direction) leaves */
742 int isleft; /* new goes in left leaf */
743 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
744 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
745 int mid; /* midpoint leaf index */
746#ifdef DEBUG
747 int oldstale; /* old count of stale leaves */
748#endif
749 int oldsum; /* old total leaf count */
750 int swap; /* swapped leaf blocks */
751
752 args = state->args;
753 /*
754 * If the block order is wrong, swap the arguments.
755 */
756 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
757 xfs_da_state_blk_t *tmp; /* temp for block swap */
758
759 tmp = blk1;
760 blk1 = blk2;
761 blk2 = tmp;
762 }
763 leaf1 = blk1->bp->data;
764 leaf2 = blk2->bp->data;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100765 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766#ifdef DEBUG
Nathan Scotta818e5d2006-03-17 17:28:07 +1100767 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768#endif
769 mid = oldsum >> 1;
770 /*
771 * If the old leaf count was odd then the new one will be even,
772 * so we need to divide the new count evenly.
773 */
774 if (oldsum & 1) {
775 xfs_dahash_t midhash; /* middle entry hash value */
776
Nathan Scotta818e5d2006-03-17 17:28:07 +1100777 if (mid >= be16_to_cpu(leaf1->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100778 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 else
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100780 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 isleft = args->hashval <= midhash;
782 }
783 /*
784 * If the old count is even then the new count is odd, so there's
785 * no preferred side for the new entry.
786 * Pick the left one.
787 */
788 else
789 isleft = 1;
790 /*
791 * Calculate moved entry count. Positive means left-to-right,
792 * negative means right-to-left. Then move the entries.
793 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100794 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (count > 0)
796 xfs_dir2_leafn_moveents(args, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100797 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 else if (count < 0)
799 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100800 be16_to_cpu(leaf1->hdr.count), count);
801 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
802 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /*
804 * Mark whether we're inserting into the old or new leaf.
805 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100806 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 state->inleaf = swap;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100808 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 state->inleaf = !swap;
810 else
811 state->inleaf =
Nathan Scotta818e5d2006-03-17 17:28:07 +1100812 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /*
814 * Adjust the expected index for insertion.
815 */
816 if (!state->inleaf)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100817 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000818
819 /*
820 * Finally sanity check just to make sure we are not returning a
821 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
823 if(blk2->index < 0) {
824 state->inleaf = 1;
825 blk2->index = 0;
Dave Chinner0b932cc2011-03-07 10:08:35 +1100826 xfs_alert(args->dp->i_mount,
827 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d\n",
828 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830}
831
832/*
833 * Remove an entry from a node directory.
834 * This removes the leaf entry and the data entry,
835 * and updates the free block if necessary.
836 */
837static int /* error */
838xfs_dir2_leafn_remove(
839 xfs_da_args_t *args, /* operation arguments */
840 xfs_dabuf_t *bp, /* leaf buffer */
841 int index, /* leaf entry index */
842 xfs_da_state_blk_t *dblk, /* data block */
843 int *rval) /* resulting block needs join */
844{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200845 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 xfs_dir2_db_t db; /* data block number */
847 xfs_dabuf_t *dbp; /* data block buffer */
848 xfs_dir2_data_entry_t *dep; /* data block entry */
849 xfs_inode_t *dp; /* incore directory inode */
850 xfs_dir2_leaf_t *leaf; /* leaf structure */
851 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
852 int longest; /* longest data free entry */
853 int off; /* data block entry offset */
854 xfs_mount_t *mp; /* filesystem mount point */
855 int needlog; /* need to log data header */
856 int needscan; /* need to rescan data frees */
857 xfs_trans_t *tp; /* transaction pointer */
858
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000859 trace_xfs_dir2_leafn_remove(args, index);
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 dp = args->dp;
862 tp = args->trans;
863 mp = dp->i_mount;
864 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100865 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 /*
867 * Point to the entry we're removing.
868 */
869 lep = &leaf->ents[index];
870 /*
871 * Extract the data block and offset from the entry.
872 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000873 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000875 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 ASSERT(dblk->index == off);
877 /*
878 * Kill the leaf entry by marking it stale.
879 * Log the leaf block changes.
880 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800881 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 xfs_dir2_leaf_log_header(tp, bp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100883 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 xfs_dir2_leaf_log_ents(tp, bp, index, index);
885 /*
886 * Make the data entry free. Keep track of the longest freespace
887 * in the data block in case it changes.
888 */
889 dbp = dblk->bp;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200890 hdr = dbp->data;
891 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
892 longest = be16_to_cpu(hdr->bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 needlog = needscan = 0;
894 xfs_dir2_data_make_free(tp, dbp, off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000895 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /*
897 * Rescan the data block freespaces for bestfree.
898 * Log the data block header if needed.
899 */
900 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200901 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (needlog)
903 xfs_dir2_data_log_header(tp, dbp);
904 xfs_dir2_data_check(dp, dbp);
905 /*
906 * If the longest data block freespace changes, need to update
907 * the corresponding freeblock entry.
908 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200909 if (longest < be16_to_cpu(hdr->bestfree[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int error; /* error return value */
911 xfs_dabuf_t *fbp; /* freeblock buffer */
912 xfs_dir2_db_t fdb; /* freeblock block number */
913 int findex; /* index in freeblock entries */
914 xfs_dir2_free_t *free; /* freeblock structure */
915 int logfree; /* need to log free entry */
916
917 /*
918 * Convert the data block number to a free block,
919 * read in the free block.
920 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000921 fdb = xfs_dir2_db_to_fdb(mp, db);
922 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 -1, &fbp, XFS_DATA_FORK))) {
924 return error;
925 }
926 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100927 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
928 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 XFS_DIR2_MAX_FREE_BESTS(mp) *
930 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
931 /*
932 * Calculate which entry we need to fix.
933 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000934 findex = xfs_dir2_db_to_fdindex(mp, db);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200935 longest = be16_to_cpu(hdr->bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 /*
937 * If the data block is now empty we can get rid of it
938 * (usually).
939 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200940 if (longest == mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /*
942 * Try to punch out the data block.
943 */
944 error = xfs_dir2_shrink_inode(args, db, dbp);
945 if (error == 0) {
946 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200947 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949 /*
950 * We can get ENOSPC if there's no space reservation.
951 * In this case just drop the buffer and some one else
952 * will eventually get rid of the empty block.
953 */
954 else if (error == ENOSPC && args->total == 0)
955 xfs_da_buf_done(dbp);
956 else
957 return error;
958 }
959 /*
960 * If we got rid of the data block, we can eliminate that entry
961 * in the free block.
962 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200963 if (hdr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 /*
965 * One less used entry in the free table.
966 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800967 be32_add_cpu(&free->hdr.nused, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 xfs_dir2_free_log_header(tp, fbp);
969 /*
970 * If this was the last entry in the table, we can
971 * trim the table size back. There might be other
972 * entries at the end referring to non-existent
973 * data blocks, get those too.
974 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100975 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 int i; /* free entry index */
977
978 for (i = findex - 1;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100979 i >= 0 && be16_to_cpu(free->bests[i]) == NULLDATAOFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 i--)
981 continue;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100982 free->hdr.nvalid = cpu_to_be32(i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 logfree = 0;
984 }
985 /*
986 * Not the last entry, just punch it out.
987 */
988 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +1100989 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 logfree = 1;
991 }
992 /*
993 * If there are no useful entries left in the block,
994 * get rid of the block if we can.
995 */
996 if (!free->hdr.nused) {
997 error = xfs_dir2_shrink_inode(args, fdb, fbp);
998 if (error == 0) {
999 fbp = NULL;
1000 logfree = 0;
1001 } else if (error != ENOSPC || args->total != 0)
1002 return error;
1003 /*
1004 * It's possible to get ENOSPC if there is no
1005 * space reservation. In this case some one
1006 * else will eventually get rid of this block.
1007 */
1008 }
1009 }
1010 /*
1011 * Data block is not empty, just set the free entry to
1012 * the new value.
1013 */
1014 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001015 free->bests[findex] = cpu_to_be16(longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 logfree = 1;
1017 }
1018 /*
1019 * Log the free entry that changed, unless we got rid of it.
1020 */
1021 if (logfree)
1022 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1023 /*
1024 * Drop the buffer if we still have it.
1025 */
1026 if (fbp)
1027 xfs_da_buf_done(fbp);
1028 }
1029 xfs_dir2_leafn_check(dp, bp);
1030 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001031 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 * to justify trying to join it with a neighbor.
1033 */
1034 *rval =
1035 ((uint)sizeof(leaf->hdr) +
1036 (uint)sizeof(leaf->ents[0]) *
Nathan Scotta818e5d2006-03-17 17:28:07 +11001037 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 mp->m_dir_magicpct;
1039 return 0;
1040}
1041
1042/*
1043 * Split the leaf entries in the old block into old and new blocks.
1044 */
1045int /* error */
1046xfs_dir2_leafn_split(
1047 xfs_da_state_t *state, /* btree cursor */
1048 xfs_da_state_blk_t *oldblk, /* original block */
1049 xfs_da_state_blk_t *newblk) /* newly created block */
1050{
1051 xfs_da_args_t *args; /* operation arguments */
1052 xfs_dablk_t blkno; /* new leaf block number */
1053 int error; /* error return value */
1054 xfs_mount_t *mp; /* filesystem mount point */
1055
1056 /*
1057 * Allocate space for a new leaf node.
1058 */
1059 args = state->args;
1060 mp = args->dp->i_mount;
1061 ASSERT(args != NULL);
1062 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1063 error = xfs_da_grow_inode(args, &blkno);
1064 if (error) {
1065 return error;
1066 }
1067 /*
1068 * Initialize the new leaf block.
1069 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001070 error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1072 if (error) {
1073 return error;
1074 }
1075 newblk->blkno = blkno;
1076 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1077 /*
1078 * Rebalance the entries across the two leaves, link the new
1079 * block into the leaves.
1080 */
1081 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1082 error = xfs_da_blk_link(state, oldblk, newblk);
1083 if (error) {
1084 return error;
1085 }
1086 /*
1087 * Insert the new entry in the correct block.
1088 */
1089 if (state->inleaf)
1090 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1091 else
1092 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1093 /*
1094 * Update last hashval in each block since we added the name.
1095 */
1096 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1097 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1098 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1099 xfs_dir2_leafn_check(args->dp, newblk->bp);
1100 return error;
1101}
1102
1103/*
1104 * Check a leaf block and its neighbors to see if the block should be
1105 * collapsed into one or the other neighbor. Always keep the block
1106 * with the smaller block number.
1107 * If the current block is over 50% full, don't try to join it, return 0.
1108 * If the block is empty, fill in the state structure and return 2.
1109 * If it can be collapsed, fill in the state structure and return 1.
1110 * If nothing can be done, return 0.
1111 */
1112int /* error */
1113xfs_dir2_leafn_toosmall(
1114 xfs_da_state_t *state, /* btree cursor */
1115 int *action) /* resulting action to take */
1116{
1117 xfs_da_state_blk_t *blk; /* leaf block */
1118 xfs_dablk_t blkno; /* leaf block number */
1119 xfs_dabuf_t *bp; /* leaf buffer */
1120 int bytes; /* bytes in use */
1121 int count; /* leaf live entry count */
1122 int error; /* error return value */
1123 int forward; /* sibling block direction */
1124 int i; /* sibling counter */
1125 xfs_da_blkinfo_t *info; /* leaf block header */
1126 xfs_dir2_leaf_t *leaf; /* leaf structure */
1127 int rval; /* result from path_shift */
1128
1129 /*
1130 * Check for the degenerate case of the block being over 50% full.
1131 * If so, it's not worth even looking to see if we might be able
1132 * to coalesce with a sibling.
1133 */
1134 blk = &state->path.blk[state->path.active - 1];
1135 info = blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001136 ASSERT(be16_to_cpu(info->magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001138 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1140 if (bytes > (state->blocksize >> 1)) {
1141 /*
1142 * Blk over 50%, don't try to join.
1143 */
1144 *action = 0;
1145 return 0;
1146 }
1147 /*
1148 * Check for the degenerate case of the block being empty.
1149 * If the block is empty, we'll simply delete it, no need to
1150 * coalesce it with a sibling block. We choose (arbitrarily)
1151 * to merge with the forward block unless it is NULL.
1152 */
1153 if (count == 0) {
1154 /*
1155 * Make altpath point to the block we want to keep and
1156 * path point to the block we want to drop (this one).
1157 */
Nathan Scott89da0542006-03-17 17:28:40 +11001158 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 memcpy(&state->altpath, &state->path, sizeof(state->path));
1160 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1161 &rval);
1162 if (error)
1163 return error;
1164 *action = rval ? 2 : 0;
1165 return 0;
1166 }
1167 /*
1168 * Examine each sibling block to see if we can coalesce with
1169 * at least 25% free space to spare. We need to figure out
1170 * whether to merge with the forward or the backward block.
1171 * We prefer coalescing with the lower numbered sibling so as
1172 * to shrink a directory over time.
1173 */
Nathan Scott89da0542006-03-17 17:28:40 +11001174 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Nathan Scott89da0542006-03-17 17:28:40 +11001176 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (blkno == 0)
1178 continue;
1179 /*
1180 * Read the sibling leaf block.
1181 */
1182 if ((error =
1183 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1184 -1, &bp, XFS_DATA_FORK))) {
1185 return error;
1186 }
1187 ASSERT(bp != NULL);
1188 /*
1189 * Count bytes in the two blocks combined.
1190 */
1191 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001192 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 bytes = state->blocksize - (state->blocksize >> 2);
1194 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001195 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001196 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 bytes -= count * (uint)sizeof(leaf->ents[0]);
1198 /*
1199 * Fits with at least 25% to spare.
1200 */
1201 if (bytes >= 0)
1202 break;
1203 xfs_da_brelse(state->args->trans, bp);
1204 }
1205 /*
1206 * Didn't like either block, give up.
1207 */
1208 if (i >= 2) {
1209 *action = 0;
1210 return 0;
1211 }
1212 /*
1213 * Done with the sibling leaf block here, drop the dabuf
1214 * so path_shift can get it.
1215 */
1216 xfs_da_buf_done(bp);
1217 /*
1218 * Make altpath point to the block we want to keep (the lower
1219 * numbered block) and path point to the block we want to drop.
1220 */
1221 memcpy(&state->altpath, &state->path, sizeof(state->path));
1222 if (blkno < blk->blkno)
1223 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1224 &rval);
1225 else
1226 error = xfs_da_path_shift(state, &state->path, forward, 0,
1227 &rval);
1228 if (error) {
1229 return error;
1230 }
1231 *action = rval ? 0 : 1;
1232 return 0;
1233}
1234
1235/*
1236 * Move all the leaf entries from drop_blk to save_blk.
1237 * This is done as part of a join operation.
1238 */
1239void
1240xfs_dir2_leafn_unbalance(
1241 xfs_da_state_t *state, /* cursor */
1242 xfs_da_state_blk_t *drop_blk, /* dead block */
1243 xfs_da_state_blk_t *save_blk) /* surviving block */
1244{
1245 xfs_da_args_t *args; /* operation arguments */
1246 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1247 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1248
1249 args = state->args;
1250 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1251 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1252 drop_leaf = drop_blk->bp->data;
1253 save_leaf = save_blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001254 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1255 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /*
1257 * If there are any stale leaf entries, take this opportunity
1258 * to purge them.
1259 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001260 if (drop_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 xfs_dir2_leaf_compact(args, drop_blk->bp);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001262 if (save_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 xfs_dir2_leaf_compact(args, save_blk->bp);
1264 /*
1265 * Move the entries from drop to the appropriate end of save.
1266 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001267 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 -07001268 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1269 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001270 be16_to_cpu(drop_leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 else
1272 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001273 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001274 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 -07001275 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1276}
1277
1278/*
1279 * Top-level node form directory addname routine.
1280 */
1281int /* error */
1282xfs_dir2_node_addname(
1283 xfs_da_args_t *args) /* operation arguments */
1284{
1285 xfs_da_state_blk_t *blk; /* leaf block for insert */
1286 int error; /* error return value */
1287 int rval; /* sub-return value */
1288 xfs_da_state_t *state; /* btree cursor */
1289
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001290 trace_xfs_dir2_node_addname(args);
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 /*
1293 * Allocate and initialize the state (btree cursor).
1294 */
1295 state = xfs_da_state_alloc();
1296 state->args = args;
1297 state->mp = args->dp->i_mount;
1298 state->blocksize = state->mp->m_dirblksize;
1299 state->node_ents = state->mp->m_dir_node_ents;
1300 /*
1301 * Look up the name. We're not supposed to find it, but
1302 * this gives us the insertion point.
1303 */
1304 error = xfs_da_node_lookup_int(state, &rval);
1305 if (error)
1306 rval = error;
1307 if (rval != ENOENT) {
1308 goto done;
1309 }
1310 /*
1311 * Add the data entry to a data block.
1312 * Extravalid is set to a freeblock found by lookup.
1313 */
1314 rval = xfs_dir2_node_addname_int(args,
1315 state->extravalid ? &state->extrablk : NULL);
1316 if (rval) {
1317 goto done;
1318 }
1319 blk = &state->path.blk[state->path.active - 1];
1320 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1321 /*
1322 * Add the new leaf entry.
1323 */
1324 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1325 if (rval == 0) {
1326 /*
1327 * It worked, fix the hash values up the btree.
1328 */
Barry Naujok6a178102008-05-21 16:42:05 +10001329 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 xfs_da_fixhashpath(state, &state->path);
1331 } else {
1332 /*
1333 * It didn't work, we need to split the leaf block.
1334 */
1335 if (args->total == 0) {
1336 ASSERT(rval == ENOSPC);
1337 goto done;
1338 }
1339 /*
1340 * Split the leaf block and insert the new entry.
1341 */
1342 rval = xfs_da_split(state);
1343 }
1344done:
1345 xfs_da_state_free(state);
1346 return rval;
1347}
1348
1349/*
1350 * Add the data entry for a node-format directory name addition.
1351 * The leaf entry is added in xfs_dir2_leafn_add.
1352 * We may enter with a freespace block that the lookup found.
1353 */
1354static int /* error */
1355xfs_dir2_node_addname_int(
1356 xfs_da_args_t *args, /* operation arguments */
1357 xfs_da_state_blk_t *fblk) /* optional freespace block */
1358{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001359 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 xfs_dir2_db_t dbno; /* data block number */
1361 xfs_dabuf_t *dbp; /* data block buffer */
1362 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1363 xfs_inode_t *dp; /* incore directory inode */
1364 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1365 int error; /* error return value */
1366 xfs_dir2_db_t fbno; /* freespace block number */
1367 xfs_dabuf_t *fbp; /* freespace buffer */
1368 int findex; /* freespace entry index */
1369 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1370 xfs_dir2_db_t ifbno; /* initial freespace block no */
1371 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1372 int length; /* length of the new entry */
1373 int logfree; /* need to log free entry */
1374 xfs_mount_t *mp; /* filesystem mount point */
1375 int needlog; /* need to log data header */
1376 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001377 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 xfs_trans_t *tp; /* transaction pointer */
1379
1380 dp = args->dp;
1381 mp = dp->i_mount;
1382 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001383 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /*
1385 * If we came in with a freespace block that means that lookup
1386 * found an entry with our hash value. This is the freespace
1387 * block for that data entry.
1388 */
1389 if (fblk) {
1390 fbp = fblk->bp;
1391 /*
1392 * Remember initial freespace block number.
1393 */
1394 ifbno = fblk->blkno;
1395 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001396 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 findex = fblk->index;
1398 /*
1399 * This means the free entry showed that the data block had
1400 * space for our entry, so we remembered it.
1401 * Use that data block.
1402 */
1403 if (findex >= 0) {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001404 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1405 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1406 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1407 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409 /*
1410 * The data block looked at didn't have enough room.
1411 * We'll start at the beginning of the freespace entries.
1412 */
1413 else {
1414 dbno = -1;
1415 findex = 0;
1416 }
1417 }
1418 /*
1419 * Didn't come in with a freespace block, so don't have a data block.
1420 */
1421 else {
1422 ifbno = dbno = -1;
1423 fbp = NULL;
1424 findex = 0;
1425 }
1426 /*
1427 * If we don't have a data block yet, we're going to scan the
1428 * freespace blocks looking for one. Figure out what the
1429 * highest freespace block number is.
1430 */
1431 if (dbno == -1) {
1432 xfs_fileoff_t fo; /* freespace block number */
1433
1434 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1435 return error;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001436 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 fbno = ifbno;
1438 }
1439 /*
1440 * While we haven't identified a data block, search the freeblock
1441 * data for a good data block. If we find a null freeblock entry,
1442 * indicating a hole in the data blocks, remember that.
1443 */
1444 while (dbno == -1) {
1445 /*
1446 * If we don't have a freeblock in hand, get the next one.
1447 */
1448 if (fbp == NULL) {
1449 /*
1450 * Happens the first time through unless lookup gave
1451 * us a freespace block to start with.
1452 */
1453 if (++fbno == 0)
1454 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1455 /*
1456 * If it's ifbno we already looked at it.
1457 */
1458 if (fbno == ifbno)
1459 fbno++;
1460 /*
1461 * If it's off the end we're done.
1462 */
1463 if (fbno >= lastfbno)
1464 break;
1465 /*
1466 * Read the block. There can be holes in the
1467 * freespace blocks, so this might not succeed.
1468 * This should be really rare, so there's no reason
1469 * to avoid it.
1470 */
1471 if ((error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001472 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 XFS_DATA_FORK))) {
1474 return error;
1475 }
1476 if (unlikely(fbp == NULL)) {
1477 continue;
1478 }
1479 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001480 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 findex = 0;
1482 }
1483 /*
1484 * Look at the current free entry. Is it good enough?
1485 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001486 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1487 be16_to_cpu(free->bests[findex]) >= length)
1488 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 else {
1490 /*
1491 * Are we done with the freeblock?
1492 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001493 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 /*
1495 * Drop the block.
1496 */
1497 xfs_da_brelse(tp, fbp);
1498 fbp = NULL;
1499 if (fblk && fblk->bp)
1500 fblk->bp = NULL;
1501 }
1502 }
1503 }
1504 /*
1505 * If we don't have a data block, we need to allocate one and make
1506 * the freespace entries refer to it.
1507 */
1508 if (unlikely(dbno == -1)) {
1509 /*
1510 * Not allowed to allocate, return failure.
1511 */
Barry Naujok6a178102008-05-21 16:42:05 +10001512 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
1513 args->total == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 /*
1515 * Drop the freespace buffer unless it came from our
1516 * caller.
1517 */
1518 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1519 xfs_da_buf_done(fbp);
1520 return XFS_ERROR(ENOSPC);
1521 }
1522 /*
1523 * Allocate and initialize the new data block.
1524 */
1525 if (unlikely((error = xfs_dir2_grow_inode(args,
1526 XFS_DIR2_DATA_SPACE,
1527 &dbno)) ||
1528 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1529 /*
1530 * Drop the freespace buffer unless it came from our
1531 * caller.
1532 */
1533 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1534 xfs_da_buf_done(fbp);
1535 return error;
1536 }
1537 /*
1538 * If (somehow) we have a freespace block, get rid of it.
1539 */
1540 if (fbp)
1541 xfs_da_brelse(tp, fbp);
1542 if (fblk && fblk->bp)
1543 fblk->bp = NULL;
1544
1545 /*
1546 * Get the freespace block corresponding to the data block
1547 * that was just allocated.
1548 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001549 fbno = xfs_dir2_db_to_fdb(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (unlikely(error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001551 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 XFS_DATA_FORK))) {
1553 xfs_da_buf_done(dbp);
1554 return error;
1555 }
1556 /*
1557 * If there wasn't a freespace block, the read will
1558 * return a NULL fbp. Allocate and initialize a new one.
1559 */
1560 if( fbp == NULL ) {
1561 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1562 &fbno))) {
1563 return error;
1564 }
1565
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001566 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001567 xfs_alert(mp,
1568 "%s: dir ino " "%llu needed freesp block %lld for\n"
1569 " data block %lld, got %lld ifbno %llu lastfbno %d",
1570 __func__, (unsigned long long)dp->i_ino,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001571 (long long)xfs_dir2_db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 (long long)dbno, (long long)fbno,
1573 (unsigned long long)ifbno, lastfbno);
1574 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001575 xfs_alert(mp,
1576 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 fblk,
1578 (unsigned long long)fblk->blkno,
1579 fblk->index,
1580 fblk->magic);
1581 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001582 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
1584 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1585 XFS_ERRLEVEL_LOW, mp);
1586 return XFS_ERROR(EFSCORRUPTED);
1587 }
1588
1589 /*
1590 * Get a buffer for the new block.
1591 */
1592 if ((error = xfs_da_get_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001593 xfs_dir2_db_to_da(mp, fbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 -1, &fbp, XFS_DATA_FORK))) {
1595 return error;
1596 }
1597 ASSERT(fbp != NULL);
1598
1599 /*
1600 * Initialize the new block to be empty, and remember
1601 * its first slot as our empty slot.
1602 */
1603 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001604 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1605 free->hdr.firstdb = cpu_to_be32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1607 XFS_DIR2_MAX_FREE_BESTS(mp));
1608 free->hdr.nvalid = 0;
1609 free->hdr.nused = 0;
1610 } else {
1611 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001612 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614
1615 /*
1616 * Set the freespace block index from the data block number.
1617 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001618 findex = xfs_dir2_db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 /*
1620 * If it's after the end of the current entries in the
1621 * freespace block, extend that table.
1622 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001623 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
Nathan Scott0ba962e2006-03-17 17:27:07 +11001625 free->hdr.nvalid = cpu_to_be32(findex + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 /*
1627 * Tag new entry so nused will go up.
1628 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001629 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631 /*
1632 * If this entry was for an empty data block
1633 * (this should always be true) then update the header.
1634 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001635 if (be16_to_cpu(free->bests[findex]) == NULLDATAOFF) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001636 be32_add_cpu(&free->hdr.nused, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 xfs_dir2_free_log_header(tp, fbp);
1638 }
1639 /*
1640 * Update the real value in the table.
1641 * We haven't allocated the data entry yet so this will
1642 * change again.
1643 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001644 hdr = dbp->data;
1645 free->bests[findex] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 logfree = 1;
1647 }
1648 /*
1649 * We had a data block so we don't have to make a new one.
1650 */
1651 else {
1652 /*
1653 * If just checking, we succeeded.
1654 */
Barry Naujok6a178102008-05-21 16:42:05 +10001655 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1657 xfs_da_buf_done(fbp);
1658 return 0;
1659 }
1660 /*
1661 * Read the data block in.
1662 */
1663 if (unlikely(
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001664 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 -1, &dbp, XFS_DATA_FORK))) {
1666 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1667 xfs_da_buf_done(fbp);
1668 return error;
1669 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001670 hdr = dbp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 logfree = 0;
1672 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001673 ASSERT(be16_to_cpu(hdr->bestfree[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /*
1675 * Point to the existing unused space.
1676 */
1677 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001678 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 needscan = needlog = 0;
1680 /*
1681 * Mark the first part of the unused space, inuse for us.
1682 */
1683 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001684 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 &needlog, &needscan);
1686 /*
1687 * Fill in the new entry and log it.
1688 */
1689 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001690 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 dep->namelen = args->namelen;
1692 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001693 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001694 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 xfs_dir2_data_log_entry(tp, dbp, dep);
1696 /*
1697 * Rescan the block for bestfree if needed.
1698 */
1699 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001700 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 /*
1702 * Log the data block header if needed.
1703 */
1704 if (needlog)
1705 xfs_dir2_data_log_header(tp, dbp);
1706 /*
1707 * If the freespace entry is now wrong, update it.
1708 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001709 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(hdr->bestfree[0].length)) {
1710 free->bests[findex] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 logfree = 1;
1712 }
1713 /*
1714 * Log the freespace entry if needed.
1715 */
1716 if (logfree)
1717 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1718 /*
1719 * If the caller didn't hand us the freespace block, drop it.
1720 */
1721 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1722 xfs_da_buf_done(fbp);
1723 /*
1724 * Return the data block and offset in args, then drop the data block.
1725 */
1726 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001727 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 xfs_da_buf_done(dbp);
1729 return 0;
1730}
1731
1732/*
1733 * Lookup an entry in a node-format directory.
1734 * All the real work happens in xfs_da_node_lookup_int.
1735 * The only real output is the inode number of the entry.
1736 */
1737int /* error */
1738xfs_dir2_node_lookup(
1739 xfs_da_args_t *args) /* operation arguments */
1740{
1741 int error; /* error return value */
1742 int i; /* btree level */
1743 int rval; /* operation return value */
1744 xfs_da_state_t *state; /* btree cursor */
1745
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001746 trace_xfs_dir2_node_lookup(args);
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 /*
1749 * Allocate and initialize the btree cursor.
1750 */
1751 state = xfs_da_state_alloc();
1752 state->args = args;
1753 state->mp = args->dp->i_mount;
1754 state->blocksize = state->mp->m_dirblksize;
1755 state->node_ents = state->mp->m_dir_node_ents;
1756 /*
1757 * Fill in the path to the entry in the cursor.
1758 */
1759 error = xfs_da_node_lookup_int(state, &rval);
1760 if (error)
1761 rval = error;
Barry Naujok384f3ce2008-05-21 16:58:22 +10001762 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
1763 /* If a CI match, dup the actual name and return EEXIST */
1764 xfs_dir2_data_entry_t *dep;
1765
1766 dep = (xfs_dir2_data_entry_t *)((char *)state->extrablk.bp->
1767 data + state->extrablk.index);
1768 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /*
1771 * Release the btree blocks and leaf block.
1772 */
1773 for (i = 0; i < state->path.active; i++) {
1774 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1775 state->path.blk[i].bp = NULL;
1776 }
1777 /*
1778 * Release the data block if we have it.
1779 */
1780 if (state->extravalid && state->extrablk.bp) {
1781 xfs_da_brelse(args->trans, state->extrablk.bp);
1782 state->extrablk.bp = NULL;
1783 }
1784 xfs_da_state_free(state);
1785 return rval;
1786}
1787
1788/*
1789 * Remove an entry from a node-format directory.
1790 */
1791int /* error */
1792xfs_dir2_node_removename(
1793 xfs_da_args_t *args) /* operation arguments */
1794{
1795 xfs_da_state_blk_t *blk; /* leaf block */
1796 int error; /* error return value */
1797 int rval; /* operation return value */
1798 xfs_da_state_t *state; /* btree cursor */
1799
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001800 trace_xfs_dir2_node_removename(args);
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 /*
1803 * Allocate and initialize the btree cursor.
1804 */
1805 state = xfs_da_state_alloc();
1806 state->args = args;
1807 state->mp = args->dp->i_mount;
1808 state->blocksize = state->mp->m_dirblksize;
1809 state->node_ents = state->mp->m_dir_node_ents;
1810 /*
1811 * Look up the entry we're deleting, set up the cursor.
1812 */
1813 error = xfs_da_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001814 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 /*
1817 * Didn't find it, upper layer screwed up.
1818 */
1819 if (rval != EEXIST) {
1820 xfs_da_state_free(state);
1821 return rval;
1822 }
1823 blk = &state->path.blk[state->path.active - 1];
1824 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1825 ASSERT(state->extravalid);
1826 /*
1827 * Remove the leaf and data entries.
1828 * Extrablk refers to the data block.
1829 */
1830 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1831 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001832 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 /*
1835 * Fix the hash values up the btree.
1836 */
1837 xfs_da_fixhashpath(state, &state->path);
1838 /*
1839 * If we need to join leaf blocks, do it.
1840 */
1841 if (rval && state->path.active > 1)
1842 error = xfs_da_join(state);
1843 /*
1844 * If no errors so far, try conversion to leaf format.
1845 */
1846 if (!error)
1847 error = xfs_dir2_node_to_leaf(state);
1848 xfs_da_state_free(state);
1849 return error;
1850}
1851
1852/*
1853 * Replace an entry's inode number in a node-format directory.
1854 */
1855int /* error */
1856xfs_dir2_node_replace(
1857 xfs_da_args_t *args) /* operation arguments */
1858{
1859 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001860 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 xfs_dir2_data_entry_t *dep; /* data entry changed */
1862 int error; /* error return value */
1863 int i; /* btree level */
1864 xfs_ino_t inum; /* new inode number */
1865 xfs_dir2_leaf_t *leaf; /* leaf structure */
1866 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1867 int rval; /* internal return value */
1868 xfs_da_state_t *state; /* btree cursor */
1869
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001870 trace_xfs_dir2_node_replace(args);
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /*
1873 * Allocate and initialize the btree cursor.
1874 */
1875 state = xfs_da_state_alloc();
1876 state->args = args;
1877 state->mp = args->dp->i_mount;
1878 state->blocksize = state->mp->m_dirblksize;
1879 state->node_ents = state->mp->m_dir_node_ents;
1880 inum = args->inumber;
1881 /*
1882 * Lookup the entry to change in the btree.
1883 */
1884 error = xfs_da_node_lookup_int(state, &rval);
1885 if (error) {
1886 rval = error;
1887 }
1888 /*
1889 * It should be found, since the vnodeops layer has looked it up
1890 * and locked it. But paranoia is good.
1891 */
1892 if (rval == EEXIST) {
1893 /*
1894 * Find the leaf entry.
1895 */
1896 blk = &state->path.blk[state->path.active - 1];
1897 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1898 leaf = blk->bp->data;
1899 lep = &leaf->ents[blk->index];
1900 ASSERT(state->extravalid);
1901 /*
1902 * Point to the data entry.
1903 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001904 hdr = state->extrablk.bp->data;
1905 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001907 ((char *)hdr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001908 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001909 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /*
1911 * Fill in the new inode number and log the entry.
1912 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001913 dep->inumber = cpu_to_be64(inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1915 rval = 0;
1916 }
1917 /*
1918 * Didn't find it, and we're holding a data block. Drop it.
1919 */
1920 else if (state->extravalid) {
1921 xfs_da_brelse(args->trans, state->extrablk.bp);
1922 state->extrablk.bp = NULL;
1923 }
1924 /*
1925 * Release all the buffers in the cursor.
1926 */
1927 for (i = 0; i < state->path.active; i++) {
1928 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1929 state->path.blk[i].bp = NULL;
1930 }
1931 xfs_da_state_free(state);
1932 return rval;
1933}
1934
1935/*
1936 * Trim off a trailing empty freespace block.
1937 * Return (in rvalp) 1 if we did it, 0 if not.
1938 */
1939int /* error */
1940xfs_dir2_node_trim_free(
1941 xfs_da_args_t *args, /* operation arguments */
1942 xfs_fileoff_t fo, /* free block number */
1943 int *rvalp) /* out: did something */
1944{
1945 xfs_dabuf_t *bp; /* freespace buffer */
1946 xfs_inode_t *dp; /* incore directory inode */
1947 int error; /* error return code */
1948 xfs_dir2_free_t *free; /* freespace structure */
1949 xfs_mount_t *mp; /* filesystem mount point */
1950 xfs_trans_t *tp; /* transaction pointer */
1951
1952 dp = args->dp;
1953 mp = dp->i_mount;
1954 tp = args->trans;
1955 /*
1956 * Read the freespace block.
1957 */
1958 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
1959 XFS_DATA_FORK))) {
1960 return error;
1961 }
1962
1963 /*
1964 * There can be holes in freespace. If fo is a hole, there's
1965 * nothing to do.
1966 */
1967 if (bp == NULL) {
1968 return 0;
1969 }
1970 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001971 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /*
1973 * If there are used entries, there's nothing to do.
1974 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001975 if (be32_to_cpu(free->hdr.nused) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 xfs_da_brelse(tp, bp);
1977 *rvalp = 0;
1978 return 0;
1979 }
1980 /*
1981 * Blow the block away.
1982 */
1983 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001984 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 bp))) {
1986 /*
1987 * Can't fail with ENOSPC since that only happens with no
1988 * space reservation, when breaking up an extent into two
1989 * pieces. This is the last block of an extent.
1990 */
1991 ASSERT(error != ENOSPC);
1992 xfs_da_brelse(tp, bp);
1993 return error;
1994 }
1995 /*
1996 * Return that we succeeded.
1997 */
1998 *rvalp = 1;
1999 return 0;
2000}