blob: da90a91f4420918421eb24c9a02db9e766768cdf [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 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100039static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
40 int index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#ifdef DEBUG
Dave Chinner1d9025e2012-06-22 18:50:14 +100042static void xfs_dir2_leafn_check(struct xfs_inode *dp, struct xfs_buf *bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#else
44#define xfs_dir2_leafn_check(dp, bp)
45#endif
Dave Chinner1d9025e2012-06-22 18:50:14 +100046static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, struct xfs_buf *bp_s,
47 int start_s, struct xfs_buf *bp_d,
48 int start_d, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
50 xfs_da_state_blk_t *blk1,
51 xfs_da_state_blk_t *blk2);
Dave Chinner1d9025e2012-06-22 18:50:14 +100052static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 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
Dave Chinner20252072012-11-12 22:54:13 +110058static void
59xfs_dir2_free_verify(
60 struct xfs_buf *bp)
61{
62 struct xfs_mount *mp = bp->b_target->bt_mount;
63 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
64 int block_ok = 0;
65
66 block_ok = hdr->magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC);
67 if (!block_ok) {
68 XFS_CORRUPTION_ERROR("xfs_dir2_free_verify magic",
69 XFS_ERRLEVEL_LOW, mp, hdr);
70 xfs_buf_ioerror(bp, EFSCORRUPTED);
71 }
Dave Chinner612cfbf2012-11-14 17:52:32 +110072}
Dave Chinner20252072012-11-12 22:54:13 +110073
Dave Chinner612cfbf2012-11-14 17:52:32 +110074static void
75xfs_dir2_free_write_verify(
76 struct xfs_buf *bp)
77{
78 xfs_dir2_free_verify(bp);
79}
80
81void
82xfs_dir2_free_read_verify(
83 struct xfs_buf *bp)
84{
85 xfs_dir2_free_verify(bp);
86 bp->b_pre_io = xfs_dir2_free_write_verify;
Dave Chinner20252072012-11-12 22:54:13 +110087 bp->b_iodone = NULL;
88 xfs_buf_ioend(bp, 0);
89}
90
Dave Chinner612cfbf2012-11-14 17:52:32 +110091
Dave Chinner20252072012-11-12 22:54:13 +110092static int
93__xfs_dir2_free_read(
94 struct xfs_trans *tp,
95 struct xfs_inode *dp,
96 xfs_dablk_t fbno,
97 xfs_daddr_t mappedbno,
98 struct xfs_buf **bpp)
99{
100 return xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinner612cfbf2012-11-14 17:52:32 +1100101 XFS_DATA_FORK, xfs_dir2_free_read_verify);
Dave Chinner20252072012-11-12 22:54:13 +1100102}
103
104int
105xfs_dir2_free_read(
106 struct xfs_trans *tp,
107 struct xfs_inode *dp,
108 xfs_dablk_t fbno,
109 struct xfs_buf **bpp)
110{
111 return __xfs_dir2_free_read(tp, dp, fbno, -1, bpp);
112}
113
114static int
115xfs_dir2_free_try_read(
116 struct xfs_trans *tp,
117 struct xfs_inode *dp,
118 xfs_dablk_t fbno,
119 struct xfs_buf **bpp)
120{
121 return __xfs_dir2_free_read(tp, dp, fbno, -2, bpp);
122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/*
125 * Log entries from a freespace block.
126 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000127STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128xfs_dir2_free_log_bests(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000129 struct xfs_trans *tp,
130 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int first, /* first entry to log */
132 int last) /* last entry to log */
133{
134 xfs_dir2_free_t *free; /* freespace structure */
135
Dave Chinner1d9025e2012-06-22 18:50:14 +1000136 free = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200137 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000138 xfs_trans_log_buf(tp, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 (uint)((char *)&free->bests[first] - (char *)free),
140 (uint)((char *)&free->bests[last] - (char *)free +
141 sizeof(free->bests[0]) - 1));
142}
143
144/*
145 * Log header from a freespace block.
146 */
147static void
148xfs_dir2_free_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000149 struct xfs_trans *tp,
150 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 xfs_dir2_free_t *free; /* freespace structure */
153
Dave Chinner1d9025e2012-06-22 18:50:14 +1000154 free = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200155 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000156 xfs_trans_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
158}
159
160/*
161 * Convert a leaf-format directory to a node-format directory.
162 * We need to change the magic number of the leaf block, and copy
163 * the freespace table out of the leaf block into its own block.
164 */
165int /* error */
166xfs_dir2_leaf_to_node(
167 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000168 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 xfs_inode_t *dp; /* incore directory inode */
171 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000172 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 xfs_dir2_db_t fdb; /* freespace block number */
174 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100175 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 int i; /* leaf freespace index */
177 xfs_dir2_leaf_t *leaf; /* leaf structure */
178 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
179 xfs_mount_t *mp; /* filesystem mount point */
180 int n; /* count of live freespc ents */
181 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100182 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 xfs_trans_t *tp; /* transaction pointer */
184
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000185 trace_xfs_dir2_leaf_to_node(args);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 dp = args->dp;
188 mp = dp->i_mount;
189 tp = args->trans;
190 /*
191 * Add a freespace block to the directory.
192 */
193 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
194 return error;
195 }
196 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
197 /*
198 * Get the buffer for the new freespace block.
199 */
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100200 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp,
201 XFS_DATA_FORK);
202 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100204 fbp->b_pre_io = xfs_dir2_free_write_verify;
205
Dave Chinner1d9025e2012-06-22 18:50:14 +1000206 free = fbp->b_addr;
207 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000208 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /*
210 * Initialize the freespace block header.
211 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100212 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 free->hdr.firstdb = 0;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100214 ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100215 free->hdr.nvalid = ltp->bestcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /*
217 * Copy freespace entries from the leaf block to the new block.
218 * Count active entries.
219 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000220 for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100221 i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100222 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100224 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Nathan Scott0ba962e2006-03-17 17:27:07 +1100226 free->hdr.nused = cpu_to_be32(n);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100227
228 lbp->b_pre_io = xfs_dir2_leafn_write_verify;
Nathan Scott89da0542006-03-17 17:28:40 +1100229 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /*
232 * Log everything.
233 */
234 xfs_dir2_leaf_log_header(tp, lbp);
235 xfs_dir2_free_log_header(tp, fbp);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100236 xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 xfs_dir2_leafn_check(dp, lbp);
238 return 0;
239}
240
241/*
242 * Add a leaf entry to a leaf block in a node-form directory.
243 * The other work necessary is done from the caller.
244 */
245static int /* error */
246xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000247 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 xfs_da_args_t *args, /* operation arguments */
249 int index) /* insertion pt for new entry */
250{
251 int compact; /* compacting stale leaves */
252 xfs_inode_t *dp; /* incore directory inode */
253 int highstale; /* next stale entry */
254 xfs_dir2_leaf_t *leaf; /* leaf structure */
255 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
256 int lfloghigh; /* high leaf entry logging */
257 int lfloglow; /* low leaf entry logging */
258 int lowstale; /* previous stale entry */
259 xfs_mount_t *mp; /* filesystem mount point */
260 xfs_trans_t *tp; /* transaction pointer */
261
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000262 trace_xfs_dir2_leafn_add(args, index);
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 dp = args->dp;
265 mp = dp->i_mount;
266 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000267 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /*
270 * Quick check just to make sure we are not going to index
271 * into other peoples memory
272 */
273 if (index < 0)
274 return XFS_ERROR(EFSCORRUPTED);
275
276 /*
277 * If there are already the maximum number of leaf entries in
278 * the block, if there are no stale entries it won't fit.
279 * Caller will do a split. If there are stale entries we'll do
280 * a compact.
281 */
282
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000283 if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (!leaf->hdr.stale)
285 return XFS_ERROR(ENOSPC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100286 compact = be16_to_cpu(leaf->hdr.stale) > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 } else
288 compact = 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100289 ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100290 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100291 be32_to_cpu(leaf->ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Barry Naujok6a178102008-05-21 16:42:05 +1000293 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0;
295
296 /*
297 * Compact out all but one stale leaf entry. Leaves behind
298 * the entry closest to index.
299 */
300 if (compact) {
301 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
302 &lfloglow, &lfloghigh);
303 }
304 /*
305 * Set impossible logging indices for this case.
306 */
307 else if (leaf->hdr.stale) {
Nathan Scotta818e5d2006-03-17 17:28:07 +1100308 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 lfloghigh = -1;
310 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /*
313 * Insert the new entry, log everything.
314 */
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200315 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
316 highstale, &lfloglow, &lfloghigh);
317
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100318 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000319 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100320 args->blkno, args->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 xfs_dir2_leaf_log_header(tp, bp);
322 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
323 xfs_dir2_leafn_check(dp, bp);
324 return 0;
325}
326
327#ifdef DEBUG
328/*
329 * Check internal consistency of a leafn block.
330 */
331void
332xfs_dir2_leafn_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000333 struct xfs_inode *dp,
334 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 int i; /* leaf index */
337 xfs_dir2_leaf_t *leaf; /* leaf structure */
338 xfs_mount_t *mp; /* filesystem mount point */
339 int stale; /* count of stale leaves */
340
Dave Chinner1d9025e2012-06-22 18:50:14 +1000341 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 mp = dp->i_mount;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200343 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000344 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100345 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
346 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100347 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
348 be32_to_cpu(leaf->ents[i + 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200350 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 stale++;
352 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100353 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355#endif /* DEBUG */
356
357/*
358 * Return the last hash value in the leaf.
359 * Stale entries are ok.
360 */
361xfs_dahash_t /* hash value */
362xfs_dir2_leafn_lasthash(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000363 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 int *count) /* count of entries in leaf */
365{
366 xfs_dir2_leaf_t *leaf; /* leaf structure */
367
Dave Chinner1d9025e2012-06-22 18:50:14 +1000368 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200369 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (count)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100371 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (!leaf->hdr.count)
373 return 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100374 return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000378 * Look up a leaf entry for space to add a name in a node-format leaf block.
379 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000381STATIC int
382xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000383 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 xfs_da_args_t *args, /* operation arguments */
385 int *indexp, /* out: leaf entry index */
386 xfs_da_state_t *state) /* state to fill in */
387{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000388 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000389 xfs_dir2_db_t curdb = -1; /* current data block number */
390 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 xfs_inode_t *dp; /* incore directory inode */
392 int error; /* error return value */
393 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000394 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 int index; /* leaf entry index */
396 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000397 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
399 xfs_mount_t *mp; /* filesystem mount point */
400 xfs_dir2_db_t newdb; /* new data block number */
401 xfs_dir2_db_t newfdb; /* new free block number */
402 xfs_trans_t *tp; /* transaction pointer */
403
404 dp = args->dp;
405 tp = args->trans;
406 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000407 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200408 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409#ifdef __KERNEL__
Nathan Scotta818e5d2006-03-17 17:28:07 +1100410 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411#endif
412 xfs_dir2_leafn_check(dp, bp);
413 /*
414 * Look up the hash value in the leaf entries.
415 */
416 index = xfs_dir2_leaf_search_hash(args, bp);
417 /*
418 * Do we have a buffer coming in?
419 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000420 if (state->extravalid) {
421 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000423 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000424 free = curbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200425 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000427 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /*
429 * Loop over leaf entries with the right hash value.
430 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000431 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
432 be32_to_cpu(lep->hashval) == args->hashval;
433 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /*
435 * Skip stale leaf entries.
436 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100437 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 continue;
439 /*
440 * Pull the data block number from the entry.
441 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000442 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /*
444 * For addname, we're looking for a place to put the new entry.
445 * We want to use a data block with an entry of equal
446 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000447 *
448 * If this block isn't the data block we already have
449 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000451 if (newdb != curdb) {
452 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000454 * Convert the data block to the free block
455 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000457 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000459 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000461 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000463 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 */
465 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000466 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100467
468 error = xfs_dir2_free_read(tp, dp,
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000469 xfs_dir2_db_to_da(mp, newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100470 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000471 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000473 free = curbp->b_addr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000474 ASSERT(be32_to_cpu(free->hdr.magic) ==
475 XFS_DIR2_FREE_MAGIC);
476 ASSERT((be32_to_cpu(free->hdr.firstdb) %
Christoph Hellwiga00b7742011-07-13 13:43:48 +0200477 xfs_dir2_free_max_bests(mp)) == 0);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000478 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
479 ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) +
480 be32_to_cpu(free->hdr.nvalid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000483 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000485 fi = xfs_dir2_db_to_fdindex(mp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000487 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200489 if (unlikely(free->bests[fi] ==
490 cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000491 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
492 XFS_ERRLEVEL_LOW, mp);
493 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000494 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000495 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000497 curfdb = newfdb;
498 if (be16_to_cpu(free->bests[fi]) >= length)
499 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000502 /* Didn't find any space */
503 fi = -1;
504out:
Barry Naujok6a178102008-05-21 16:42:05 +1000505 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000506 if (curbp) {
507 /* Giving back a free block. */
508 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000510 state->extrablk.index = fi;
511 state->extrablk.blkno = curfdb;
512 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
513 } else {
514 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000517 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 */
519 *indexp = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return XFS_ERROR(ENOENT);
521}
522
523/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000524 * Look up a leaf entry in a node-format leaf block.
525 * The extrablk in state a data block.
526 */
527STATIC int
528xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000529 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000530 xfs_da_args_t *args, /* operation arguments */
531 int *indexp, /* out: leaf entry index */
532 xfs_da_state_t *state) /* state to fill in */
533{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000534 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000535 xfs_dir2_db_t curdb = -1; /* current data block number */
536 xfs_dir2_data_entry_t *dep; /* data block entry */
537 xfs_inode_t *dp; /* incore directory inode */
538 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000539 int index; /* leaf entry index */
540 xfs_dir2_leaf_t *leaf; /* leaf structure */
541 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
542 xfs_mount_t *mp; /* filesystem mount point */
543 xfs_dir2_db_t newdb; /* new data block number */
544 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000545 enum xfs_dacmp cmp; /* comparison result */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000546
547 dp = args->dp;
548 tp = args->trans;
549 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000550 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200551 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000552#ifdef __KERNEL__
553 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
554#endif
555 xfs_dir2_leafn_check(dp, bp);
556 /*
557 * Look up the hash value in the leaf entries.
558 */
559 index = xfs_dir2_leaf_search_hash(args, bp);
560 /*
561 * Do we have a buffer coming in?
562 */
563 if (state->extravalid) {
564 curbp = state->extrablk.bp;
565 curdb = state->extrablk.blkno;
566 }
567 /*
568 * Loop over leaf entries with the right hash value.
569 */
570 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
571 be32_to_cpu(lep->hashval) == args->hashval;
572 lep++, index++) {
573 /*
574 * Skip stale leaf entries.
575 */
576 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
577 continue;
578 /*
579 * Pull the data block number from the entry.
580 */
581 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
582 /*
583 * Not adding a new entry, so we really want to find
584 * the name given to us.
585 *
586 * If it's a different data block, go get it.
587 */
588 if (newdb != curdb) {
589 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000590 * If we had a block before that we aren't saving
591 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000592 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000593 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
594 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000595 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000596 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000597 * If needing the block that is saved with a CI match,
598 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000599 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000600 if (args->cmpresult != XFS_CMP_DIFFERENT &&
601 newdb == state->extrablk.blkno) {
602 ASSERT(state->extravalid);
603 curbp = state->extrablk.bp;
604 } else {
Dave Chinnere4813572012-11-12 22:54:14 +1100605 error = xfs_dir2_data_read(tp, dp,
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000606 xfs_dir2_db_to_da(mp, newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100607 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000608 if (error)
609 return error;
610 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000611 xfs_dir2_data_check(dp, curbp);
612 curdb = newdb;
613 }
614 /*
615 * Point to the data entry.
616 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000617 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000618 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
619 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000620 * Compare the entry and if it's an exact match, return
621 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000622 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000623 */
Barry Naujok5163f952008-05-21 16:41:01 +1000624 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
625 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000626 /* If there is a CI match block, drop it */
627 if (args->cmpresult != XFS_CMP_DIFFERENT &&
628 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000629 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000630 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000631 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000632 *indexp = index;
633 state->extravalid = 1;
634 state->extrablk.bp = curbp;
635 state->extrablk.blkno = curdb;
636 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000637 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000638 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100639 curbp->b_pre_io = xfs_dir2_data_write_verify;
Barry Naujok5163f952008-05-21 16:41:01 +1000640 if (cmp == XFS_CMP_EXACT)
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000641 return XFS_ERROR(EEXIST);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000642 }
643 }
Barry Naujok6a178102008-05-21 16:42:05 +1000644 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
645 (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000646 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000647 if (args->cmpresult == XFS_CMP_DIFFERENT) {
648 /* Giving back last used data block. */
649 state->extravalid = 1;
650 state->extrablk.bp = curbp;
651 state->extrablk.index = -1;
652 state->extrablk.blkno = curdb;
653 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100654 curbp->b_pre_io = xfs_dir2_data_write_verify;
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000655 } else {
656 /* If the curbp is not the CI match block, drop it */
657 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000658 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000659 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000660 } else {
661 state->extravalid = 0;
662 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000663 *indexp = index;
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000664 return XFS_ERROR(ENOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000665}
666
667/*
668 * Look up a leaf entry in a node-format leaf block.
669 * If this is an addname then the extrablk in state is a freespace block,
670 * otherwise it's a data block.
671 */
672int
673xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000674 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000675 xfs_da_args_t *args, /* operation arguments */
676 int *indexp, /* out: leaf entry index */
677 xfs_da_state_t *state) /* state to fill in */
678{
Barry Naujok6a178102008-05-21 16:42:05 +1000679 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000680 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
681 state);
682 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
683}
684
685/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 * Move count leaf entries from source to destination leaf.
687 * Log entries and headers. Stale entries are preserved.
688 */
689static void
690xfs_dir2_leafn_moveents(
691 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000692 struct xfs_buf *bp_s, /* source leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int start_s, /* source leaf index */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000694 struct xfs_buf *bp_d, /* destination leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 int start_d, /* destination leaf index */
696 int count) /* count of leaves to copy */
697{
698 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
699 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
700 int stale; /* count stale leaves copied */
701 xfs_trans_t *tp; /* transaction pointer */
702
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000703 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /*
706 * Silently return if nothing to do.
707 */
708 if (count == 0) {
709 return;
710 }
711 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000712 leaf_s = bp_s->b_addr;
713 leaf_d = bp_d->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /*
715 * If the destination index is not the end of the current
716 * destination leaf entries, open up a hole in the destination
717 * to hold the new entries.
718 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100719 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
Nathan Scotta818e5d2006-03-17 17:28:07 +1100721 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 sizeof(xfs_dir2_leaf_entry_t));
723 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100724 count + be16_to_cpu(leaf_d->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 /*
727 * If the source has stale leaves, count the ones in the copy range
728 * so we can update the header correctly.
729 */
730 if (leaf_s->hdr.stale) {
731 int i; /* temp leaf index */
732
733 for (i = start_s, stale = 0; i < start_s + count; i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200734 if (leaf_s->ents[i].address ==
735 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 stale++;
737 }
738 } else
739 stale = 0;
740 /*
741 * Copy the leaf entries from source to destination.
742 */
743 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
744 count * sizeof(xfs_dir2_leaf_entry_t));
745 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
746 /*
747 * If there are source entries after the ones we copied,
748 * delete the ones we copied by sliding the next ones down.
749 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100750 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
752 count * sizeof(xfs_dir2_leaf_entry_t));
753 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
754 }
755 /*
756 * Update the headers and log them.
757 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800758 be16_add_cpu(&leaf_s->hdr.count, -(count));
759 be16_add_cpu(&leaf_s->hdr.stale, -(stale));
760 be16_add_cpu(&leaf_d->hdr.count, count);
761 be16_add_cpu(&leaf_d->hdr.stale, stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 xfs_dir2_leaf_log_header(tp, bp_s);
763 xfs_dir2_leaf_log_header(tp, bp_d);
764 xfs_dir2_leafn_check(args->dp, bp_s);
765 xfs_dir2_leafn_check(args->dp, bp_d);
766}
767
768/*
769 * Determine the sort order of two leaf blocks.
770 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
771 */
772int /* sort order */
773xfs_dir2_leafn_order(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000774 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
775 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
778 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
779
Dave Chinner1d9025e2012-06-22 18:50:14 +1000780 leaf1 = leaf1_bp->b_addr;
781 leaf2 = leaf2_bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200782 ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
783 ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100784 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
785 be16_to_cpu(leaf2->hdr.count) > 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100786 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
787 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
788 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return 1;
790 return 0;
791}
792
793/*
794 * Rebalance leaf entries between two leaf blocks.
795 * This is actually only called when the second block is new,
796 * though the code deals with the general case.
797 * A new entry will be inserted in one of the blocks, and that
798 * entry is taken into account when balancing.
799 */
800static void
801xfs_dir2_leafn_rebalance(
802 xfs_da_state_t *state, /* btree cursor */
803 xfs_da_state_blk_t *blk1, /* first btree block */
804 xfs_da_state_blk_t *blk2) /* second btree block */
805{
806 xfs_da_args_t *args; /* operation arguments */
807 int count; /* count (& direction) leaves */
808 int isleft; /* new goes in left leaf */
809 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
810 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
811 int mid; /* midpoint leaf index */
812#ifdef DEBUG
813 int oldstale; /* old count of stale leaves */
814#endif
815 int oldsum; /* old total leaf count */
816 int swap; /* swapped leaf blocks */
817
818 args = state->args;
819 /*
820 * If the block order is wrong, swap the arguments.
821 */
822 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
823 xfs_da_state_blk_t *tmp; /* temp for block swap */
824
825 tmp = blk1;
826 blk1 = blk2;
827 blk2 = tmp;
828 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000829 leaf1 = blk1->bp->b_addr;
830 leaf2 = blk2->bp->b_addr;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100831 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832#ifdef DEBUG
Nathan Scotta818e5d2006-03-17 17:28:07 +1100833 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834#endif
835 mid = oldsum >> 1;
836 /*
837 * If the old leaf count was odd then the new one will be even,
838 * so we need to divide the new count evenly.
839 */
840 if (oldsum & 1) {
841 xfs_dahash_t midhash; /* middle entry hash value */
842
Nathan Scotta818e5d2006-03-17 17:28:07 +1100843 if (mid >= be16_to_cpu(leaf1->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100844 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 else
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100846 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 isleft = args->hashval <= midhash;
848 }
849 /*
850 * If the old count is even then the new count is odd, so there's
851 * no preferred side for the new entry.
852 * Pick the left one.
853 */
854 else
855 isleft = 1;
856 /*
857 * Calculate moved entry count. Positive means left-to-right,
858 * negative means right-to-left. Then move the entries.
859 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100860 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (count > 0)
862 xfs_dir2_leafn_moveents(args, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100863 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 else if (count < 0)
865 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100866 be16_to_cpu(leaf1->hdr.count), count);
867 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
868 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 /*
870 * Mark whether we're inserting into the old or new leaf.
871 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100872 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 state->inleaf = swap;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100874 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 state->inleaf = !swap;
876 else
877 state->inleaf =
Nathan Scotta818e5d2006-03-17 17:28:07 +1100878 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /*
880 * Adjust the expected index for insertion.
881 */
882 if (!state->inleaf)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100883 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000884
885 /*
886 * Finally sanity check just to make sure we are not returning a
887 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
889 if(blk2->index < 0) {
890 state->inleaf = 1;
891 blk2->index = 0;
Dave Chinner0b932cc2011-03-07 10:08:35 +1100892 xfs_alert(args->dp->i_mount,
893 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d\n",
894 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896}
897
Dave Chinner20252072012-11-12 22:54:13 +1100898static int
899xfs_dir2_data_block_free(
900 xfs_da_args_t *args,
901 struct xfs_dir2_data_hdr *hdr,
902 struct xfs_dir2_free *free,
903 xfs_dir2_db_t fdb,
904 int findex,
905 struct xfs_buf *fbp,
906 int longest)
907{
908 struct xfs_trans *tp = args->trans;
909 int logfree = 0;
910
911 if (!hdr) {
912 /* One less used entry in the free table. */
913 be32_add_cpu(&free->hdr.nused, -1);
914 xfs_dir2_free_log_header(tp, fbp);
915
916 /*
917 * If this was the last entry in the table, we can trim the
918 * table size back. There might be other entries at the end
919 * referring to non-existent data blocks, get those too.
920 */
921 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
922 int i; /* free entry index */
923
924 for (i = findex - 1; i >= 0; i--) {
925 if (free->bests[i] != cpu_to_be16(NULLDATAOFF))
926 break;
927 }
928 free->hdr.nvalid = cpu_to_be32(i + 1);
929 logfree = 0;
930 } else {
931 /* Not the last entry, just punch it out. */
932 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
933 logfree = 1;
934 }
935 /*
936 * If there are no useful entries left in the block,
937 * get rid of the block if we can.
938 */
939 if (!free->hdr.nused) {
940 int error;
941
942 error = xfs_dir2_shrink_inode(args, fdb, fbp);
943 if (error == 0) {
944 fbp = NULL;
945 logfree = 0;
946 } else if (error != ENOSPC || args->total != 0)
947 return error;
948 /*
949 * It's possible to get ENOSPC if there is no
950 * space reservation. In this case some one
951 * else will eventually get rid of this block.
952 */
953 }
954 } else {
955 /*
956 * Data block is not empty, just set the free entry to the new
957 * value.
958 */
959 free->bests[findex] = cpu_to_be16(longest);
960 logfree = 1;
961 }
962
963 /* Log the free entry that changed, unless we got rid of it. */
964 if (logfree)
965 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
966 return 0;
967}
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969/*
970 * Remove an entry from a node directory.
971 * This removes the leaf entry and the data entry,
972 * and updates the free block if necessary.
973 */
974static int /* error */
975xfs_dir2_leafn_remove(
976 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000977 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 int index, /* leaf entry index */
979 xfs_da_state_blk_t *dblk, /* data block */
980 int *rval) /* resulting block needs join */
981{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200982 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000984 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 xfs_dir2_data_entry_t *dep; /* data block entry */
986 xfs_inode_t *dp; /* incore directory inode */
987 xfs_dir2_leaf_t *leaf; /* leaf structure */
988 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
989 int longest; /* longest data free entry */
990 int off; /* data block entry offset */
991 xfs_mount_t *mp; /* filesystem mount point */
992 int needlog; /* need to log data header */
993 int needscan; /* need to rescan data frees */
994 xfs_trans_t *tp; /* transaction pointer */
995
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000996 trace_xfs_dir2_leafn_remove(args, index);
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 dp = args->dp;
999 tp = args->trans;
1000 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001001 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001002 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /*
1004 * Point to the entry we're removing.
1005 */
1006 lep = &leaf->ents[index];
1007 /*
1008 * Extract the data block and offset from the entry.
1009 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001010 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001012 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 ASSERT(dblk->index == off);
1014 /*
1015 * Kill the leaf entry by marking it stale.
1016 * Log the leaf block changes.
1017 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001018 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 xfs_dir2_leaf_log_header(tp, bp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001020 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 xfs_dir2_leaf_log_ents(tp, bp, index, index);
1022 /*
1023 * Make the data entry free. Keep track of the longest freespace
1024 * in the data block in case it changes.
1025 */
1026 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001027 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001028 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
1029 longest = be16_to_cpu(hdr->bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 needlog = needscan = 0;
1031 xfs_dir2_data_make_free(tp, dbp, off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001032 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /*
1034 * Rescan the data block freespaces for bestfree.
1035 * Log the data block header if needed.
1036 */
1037 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001038 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (needlog)
1040 xfs_dir2_data_log_header(tp, dbp);
1041 xfs_dir2_data_check(dp, dbp);
1042 /*
1043 * If the longest data block freespace changes, need to update
1044 * the corresponding freeblock entry.
1045 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001046 if (longest < be16_to_cpu(hdr->bestfree[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001048 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 xfs_dir2_db_t fdb; /* freeblock block number */
1050 int findex; /* index in freeblock entries */
1051 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 /*
1054 * Convert the data block number to a free block,
1055 * read in the free block.
1056 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001057 fdb = xfs_dir2_db_to_fdb(mp, db);
Dave Chinner20252072012-11-12 22:54:13 +11001058 error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(mp, fdb),
1059 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001060 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001062 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001063 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Nathan Scott0ba962e2006-03-17 17:27:07 +11001064 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
Christoph Hellwiga00b7742011-07-13 13:43:48 +02001065 xfs_dir2_free_max_bests(mp) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
1067 /*
1068 * Calculate which entry we need to fix.
1069 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001070 findex = xfs_dir2_db_to_fdindex(mp, db);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001071 longest = be16_to_cpu(hdr->bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /*
1073 * If the data block is now empty we can get rid of it
1074 * (usually).
1075 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001076 if (longest == mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /*
1078 * Try to punch out the data block.
1079 */
1080 error = xfs_dir2_shrink_inode(args, db, dbp);
1081 if (error == 0) {
1082 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001083 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085 /*
1086 * We can get ENOSPC if there's no space reservation.
1087 * In this case just drop the buffer and some one else
1088 * will eventually get rid of the empty block.
1089 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001090 else if (!(error == ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return error;
1092 }
1093 /*
1094 * If we got rid of the data block, we can eliminate that entry
1095 * in the free block.
1096 */
Dave Chinner20252072012-11-12 22:54:13 +11001097 error = xfs_dir2_data_block_free(args, hdr, free,
1098 fdb, findex, fbp, longest);
1099 if (error)
1100 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
Dave Chinner20252072012-11-12 22:54:13 +11001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 xfs_dir2_leafn_check(dp, bp);
1104 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001105 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 * to justify trying to join it with a neighbor.
1107 */
1108 *rval =
1109 ((uint)sizeof(leaf->hdr) +
1110 (uint)sizeof(leaf->ents[0]) *
Nathan Scotta818e5d2006-03-17 17:28:07 +11001111 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 mp->m_dir_magicpct;
1113 return 0;
1114}
1115
1116/*
1117 * Split the leaf entries in the old block into old and new blocks.
1118 */
1119int /* error */
1120xfs_dir2_leafn_split(
1121 xfs_da_state_t *state, /* btree cursor */
1122 xfs_da_state_blk_t *oldblk, /* original block */
1123 xfs_da_state_blk_t *newblk) /* newly created block */
1124{
1125 xfs_da_args_t *args; /* operation arguments */
1126 xfs_dablk_t blkno; /* new leaf block number */
1127 int error; /* error return value */
1128 xfs_mount_t *mp; /* filesystem mount point */
1129
1130 /*
1131 * Allocate space for a new leaf node.
1132 */
1133 args = state->args;
1134 mp = args->dp->i_mount;
1135 ASSERT(args != NULL);
1136 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1137 error = xfs_da_grow_inode(args, &blkno);
1138 if (error) {
1139 return error;
1140 }
1141 /*
1142 * Initialize the new leaf block.
1143 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001144 error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1146 if (error) {
1147 return error;
1148 }
1149 newblk->blkno = blkno;
1150 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1151 /*
1152 * Rebalance the entries across the two leaves, link the new
1153 * block into the leaves.
1154 */
1155 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1156 error = xfs_da_blk_link(state, oldblk, newblk);
1157 if (error) {
1158 return error;
1159 }
1160 /*
1161 * Insert the new entry in the correct block.
1162 */
1163 if (state->inleaf)
1164 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1165 else
1166 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1167 /*
1168 * Update last hashval in each block since we added the name.
1169 */
1170 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1171 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1172 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1173 xfs_dir2_leafn_check(args->dp, newblk->bp);
1174 return error;
1175}
1176
1177/*
1178 * Check a leaf block and its neighbors to see if the block should be
1179 * collapsed into one or the other neighbor. Always keep the block
1180 * with the smaller block number.
1181 * If the current block is over 50% full, don't try to join it, return 0.
1182 * If the block is empty, fill in the state structure and return 2.
1183 * If it can be collapsed, fill in the state structure and return 1.
1184 * If nothing can be done, return 0.
1185 */
1186int /* error */
1187xfs_dir2_leafn_toosmall(
1188 xfs_da_state_t *state, /* btree cursor */
1189 int *action) /* resulting action to take */
1190{
1191 xfs_da_state_blk_t *blk; /* leaf block */
1192 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001193 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 int bytes; /* bytes in use */
1195 int count; /* leaf live entry count */
1196 int error; /* error return value */
1197 int forward; /* sibling block direction */
1198 int i; /* sibling counter */
1199 xfs_da_blkinfo_t *info; /* leaf block header */
1200 xfs_dir2_leaf_t *leaf; /* leaf structure */
1201 int rval; /* result from path_shift */
1202
1203 /*
1204 * Check for the degenerate case of the block being over 50% full.
1205 * If so, it's not worth even looking to see if we might be able
1206 * to coalesce with a sibling.
1207 */
1208 blk = &state->path.blk[state->path.active - 1];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001209 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001210 ASSERT(info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001212 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1214 if (bytes > (state->blocksize >> 1)) {
1215 /*
1216 * Blk over 50%, don't try to join.
1217 */
1218 *action = 0;
1219 return 0;
1220 }
1221 /*
1222 * Check for the degenerate case of the block being empty.
1223 * If the block is empty, we'll simply delete it, no need to
1224 * coalesce it with a sibling block. We choose (arbitrarily)
1225 * to merge with the forward block unless it is NULL.
1226 */
1227 if (count == 0) {
1228 /*
1229 * Make altpath point to the block we want to keep and
1230 * path point to the block we want to drop (this one).
1231 */
Nathan Scott89da0542006-03-17 17:28:40 +11001232 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 memcpy(&state->altpath, &state->path, sizeof(state->path));
1234 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1235 &rval);
1236 if (error)
1237 return error;
1238 *action = rval ? 2 : 0;
1239 return 0;
1240 }
1241 /*
1242 * Examine each sibling block to see if we can coalesce with
1243 * at least 25% free space to spare. We need to figure out
1244 * whether to merge with the forward or the backward block.
1245 * We prefer coalescing with the lower numbered sibling so as
1246 * to shrink a directory over time.
1247 */
Nathan Scott89da0542006-03-17 17:28:40 +11001248 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Nathan Scott89da0542006-03-17 17:28:40 +11001250 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (blkno == 0)
1252 continue;
1253 /*
1254 * Read the sibling leaf block.
1255 */
Dave Chinnere6f76672012-11-12 22:54:15 +11001256 error = xfs_dir2_leafn_read(state->args->trans, state->args->dp,
1257 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001258 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /*
1262 * Count bytes in the two blocks combined.
1263 */
1264 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001265 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 bytes = state->blocksize - (state->blocksize >> 2);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001267 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001268 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Nathan Scotta818e5d2006-03-17 17:28:07 +11001269 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 bytes -= count * (uint)sizeof(leaf->ents[0]);
1271 /*
1272 * Fits with at least 25% to spare.
1273 */
1274 if (bytes >= 0)
1275 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001276 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 /*
1279 * Didn't like either block, give up.
1280 */
1281 if (i >= 2) {
1282 *action = 0;
1283 return 0;
1284 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /*
1287 * Make altpath point to the block we want to keep (the lower
1288 * numbered block) and path point to the block we want to drop.
1289 */
1290 memcpy(&state->altpath, &state->path, sizeof(state->path));
1291 if (blkno < blk->blkno)
1292 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1293 &rval);
1294 else
1295 error = xfs_da_path_shift(state, &state->path, forward, 0,
1296 &rval);
1297 if (error) {
1298 return error;
1299 }
1300 *action = rval ? 0 : 1;
1301 return 0;
1302}
1303
1304/*
1305 * Move all the leaf entries from drop_blk to save_blk.
1306 * This is done as part of a join operation.
1307 */
1308void
1309xfs_dir2_leafn_unbalance(
1310 xfs_da_state_t *state, /* cursor */
1311 xfs_da_state_blk_t *drop_blk, /* dead block */
1312 xfs_da_state_blk_t *save_blk) /* surviving block */
1313{
1314 xfs_da_args_t *args; /* operation arguments */
1315 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1316 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1317
1318 args = state->args;
1319 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1320 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001321 drop_leaf = drop_blk->bp->b_addr;
1322 save_leaf = save_blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001323 ASSERT(drop_leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1324 ASSERT(save_leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 /*
1326 * If there are any stale leaf entries, take this opportunity
1327 * to purge them.
1328 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001329 if (drop_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 xfs_dir2_leaf_compact(args, drop_blk->bp);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001331 if (save_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 xfs_dir2_leaf_compact(args, save_blk->bp);
1333 /*
1334 * Move the entries from drop to the appropriate end of save.
1335 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001336 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 -07001337 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1338 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001339 be16_to_cpu(drop_leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 else
1341 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001342 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001343 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 -07001344 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1345}
1346
1347/*
1348 * Top-level node form directory addname routine.
1349 */
1350int /* error */
1351xfs_dir2_node_addname(
1352 xfs_da_args_t *args) /* operation arguments */
1353{
1354 xfs_da_state_blk_t *blk; /* leaf block for insert */
1355 int error; /* error return value */
1356 int rval; /* sub-return value */
1357 xfs_da_state_t *state; /* btree cursor */
1358
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001359 trace_xfs_dir2_node_addname(args);
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 /*
1362 * Allocate and initialize the state (btree cursor).
1363 */
1364 state = xfs_da_state_alloc();
1365 state->args = args;
1366 state->mp = args->dp->i_mount;
1367 state->blocksize = state->mp->m_dirblksize;
1368 state->node_ents = state->mp->m_dir_node_ents;
1369 /*
1370 * Look up the name. We're not supposed to find it, but
1371 * this gives us the insertion point.
1372 */
1373 error = xfs_da_node_lookup_int(state, &rval);
1374 if (error)
1375 rval = error;
1376 if (rval != ENOENT) {
1377 goto done;
1378 }
1379 /*
1380 * Add the data entry to a data block.
1381 * Extravalid is set to a freeblock found by lookup.
1382 */
1383 rval = xfs_dir2_node_addname_int(args,
1384 state->extravalid ? &state->extrablk : NULL);
1385 if (rval) {
1386 goto done;
1387 }
1388 blk = &state->path.blk[state->path.active - 1];
1389 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1390 /*
1391 * Add the new leaf entry.
1392 */
1393 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1394 if (rval == 0) {
1395 /*
1396 * It worked, fix the hash values up the btree.
1397 */
Barry Naujok6a178102008-05-21 16:42:05 +10001398 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 xfs_da_fixhashpath(state, &state->path);
1400 } else {
1401 /*
1402 * It didn't work, we need to split the leaf block.
1403 */
1404 if (args->total == 0) {
1405 ASSERT(rval == ENOSPC);
1406 goto done;
1407 }
1408 /*
1409 * Split the leaf block and insert the new entry.
1410 */
1411 rval = xfs_da_split(state);
1412 }
1413done:
1414 xfs_da_state_free(state);
1415 return rval;
1416}
1417
1418/*
1419 * Add the data entry for a node-format directory name addition.
1420 * The leaf entry is added in xfs_dir2_leafn_add.
1421 * We may enter with a freespace block that the lookup found.
1422 */
1423static int /* error */
1424xfs_dir2_node_addname_int(
1425 xfs_da_args_t *args, /* operation arguments */
1426 xfs_da_state_blk_t *fblk) /* optional freespace block */
1427{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001428 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001430 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1432 xfs_inode_t *dp; /* incore directory inode */
1433 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1434 int error; /* error return value */
1435 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001436 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int findex; /* freespace entry index */
1438 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1439 xfs_dir2_db_t ifbno; /* initial freespace block no */
1440 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1441 int length; /* length of the new entry */
1442 int logfree; /* need to log free entry */
1443 xfs_mount_t *mp; /* filesystem mount point */
1444 int needlog; /* need to log data header */
1445 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001446 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 xfs_trans_t *tp; /* transaction pointer */
1448
1449 dp = args->dp;
1450 mp = dp->i_mount;
1451 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001452 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 /*
1454 * If we came in with a freespace block that means that lookup
1455 * found an entry with our hash value. This is the freespace
1456 * block for that data entry.
1457 */
1458 if (fblk) {
1459 fbp = fblk->bp;
1460 /*
1461 * Remember initial freespace block number.
1462 */
1463 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001464 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001465 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 findex = fblk->index;
1467 /*
1468 * This means the free entry showed that the data block had
1469 * space for our entry, so we remembered it.
1470 * Use that data block.
1471 */
1472 if (findex >= 0) {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001473 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1474 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1475 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1476 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478 /*
1479 * The data block looked at didn't have enough room.
1480 * We'll start at the beginning of the freespace entries.
1481 */
1482 else {
1483 dbno = -1;
1484 findex = 0;
1485 }
1486 }
1487 /*
1488 * Didn't come in with a freespace block, so don't have a data block.
1489 */
1490 else {
1491 ifbno = dbno = -1;
1492 fbp = NULL;
1493 findex = 0;
1494 }
1495 /*
1496 * If we don't have a data block yet, we're going to scan the
1497 * freespace blocks looking for one. Figure out what the
1498 * highest freespace block number is.
1499 */
1500 if (dbno == -1) {
1501 xfs_fileoff_t fo; /* freespace block number */
1502
1503 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1504 return error;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001505 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 fbno = ifbno;
1507 }
1508 /*
1509 * While we haven't identified a data block, search the freeblock
1510 * data for a good data block. If we find a null freeblock entry,
1511 * indicating a hole in the data blocks, remember that.
1512 */
1513 while (dbno == -1) {
1514 /*
1515 * If we don't have a freeblock in hand, get the next one.
1516 */
1517 if (fbp == NULL) {
1518 /*
1519 * Happens the first time through unless lookup gave
1520 * us a freespace block to start with.
1521 */
1522 if (++fbno == 0)
1523 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1524 /*
1525 * If it's ifbno we already looked at it.
1526 */
1527 if (fbno == ifbno)
1528 fbno++;
1529 /*
1530 * If it's off the end we're done.
1531 */
1532 if (fbno >= lastfbno)
1533 break;
1534 /*
1535 * Read the block. There can be holes in the
1536 * freespace blocks, so this might not succeed.
1537 * This should be really rare, so there's no reason
1538 * to avoid it.
1539 */
Dave Chinner20252072012-11-12 22:54:13 +11001540 error = xfs_dir2_free_try_read(tp, dp,
1541 xfs_dir2_db_to_da(mp, fbno),
1542 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001543 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001545 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001547 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001548 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 findex = 0;
1550 }
1551 /*
1552 * Look at the current free entry. Is it good enough?
1553 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001554 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1555 be16_to_cpu(free->bests[findex]) >= length)
1556 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 else {
1558 /*
1559 * Are we done with the freeblock?
1560 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001561 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 /*
1563 * Drop the block.
1564 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001565 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 fbp = NULL;
1567 if (fblk && fblk->bp)
1568 fblk->bp = NULL;
1569 }
1570 }
1571 }
1572 /*
1573 * If we don't have a data block, we need to allocate one and make
1574 * the freespace entries refer to it.
1575 */
1576 if (unlikely(dbno == -1)) {
1577 /*
1578 * Not allowed to allocate, return failure.
1579 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001580 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return XFS_ERROR(ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 /*
1584 * Allocate and initialize the new data block.
1585 */
1586 if (unlikely((error = xfs_dir2_grow_inode(args,
1587 XFS_DIR2_DATA_SPACE,
1588 &dbno)) ||
Dave Chinner1d9025e2012-06-22 18:50:14 +10001589 (error = xfs_dir2_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /*
1593 * If (somehow) we have a freespace block, get rid of it.
1594 */
1595 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001596 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (fblk && fblk->bp)
1598 fblk->bp = NULL;
1599
1600 /*
1601 * Get the freespace block corresponding to the data block
1602 * that was just allocated.
1603 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001604 fbno = xfs_dir2_db_to_fdb(mp, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001605 error = xfs_dir2_free_try_read(tp, dp,
1606 xfs_dir2_db_to_da(mp, fbno),
1607 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001608 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 /*
1612 * If there wasn't a freespace block, the read will
1613 * return a NULL fbp. Allocate and initialize a new one.
1614 */
1615 if( fbp == NULL ) {
1616 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1617 &fbno))) {
1618 return error;
1619 }
1620
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001621 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001622 xfs_alert(mp,
Jean Delvaredf2e3012011-07-16 18:10:35 +02001623 "%s: dir ino %llu needed freesp block %lld for\n"
Dave Chinner0b932cc2011-03-07 10:08:35 +11001624 " data block %lld, got %lld ifbno %llu lastfbno %d",
1625 __func__, (unsigned long long)dp->i_ino,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001626 (long long)xfs_dir2_db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 (long long)dbno, (long long)fbno,
1628 (unsigned long long)ifbno, lastfbno);
1629 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001630 xfs_alert(mp,
1631 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 fblk,
1633 (unsigned long long)fblk->blkno,
1634 fblk->index,
1635 fblk->magic);
1636 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001637 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1640 XFS_ERRLEVEL_LOW, mp);
1641 return XFS_ERROR(EFSCORRUPTED);
1642 }
1643
1644 /*
1645 * Get a buffer for the new block.
1646 */
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001647 error = xfs_da_get_buf(tp, dp,
1648 xfs_dir2_db_to_da(mp, fbno),
1649 -1, &fbp, XFS_DATA_FORK);
1650 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001652 fbp->b_pre_io = xfs_dir2_free_write_verify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 /*
1655 * Initialize the new block to be empty, and remember
1656 * its first slot as our empty slot.
1657 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001658 free = fbp->b_addr;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001659 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1660 free->hdr.firstdb = cpu_to_be32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
Christoph Hellwiga00b7742011-07-13 13:43:48 +02001662 xfs_dir2_free_max_bests(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 free->hdr.nvalid = 0;
1664 free->hdr.nused = 0;
1665 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001666 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001667 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
1669
1670 /*
1671 * Set the freespace block index from the data block number.
1672 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001673 findex = xfs_dir2_db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /*
1675 * If it's after the end of the current entries in the
1676 * freespace block, extend that table.
1677 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001678 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
Christoph Hellwiga00b7742011-07-13 13:43:48 +02001679 ASSERT(findex < xfs_dir2_free_max_bests(mp));
Nathan Scott0ba962e2006-03-17 17:27:07 +11001680 free->hdr.nvalid = cpu_to_be32(findex + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 /*
1682 * Tag new entry so nused will go up.
1683 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001684 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686 /*
1687 * If this entry was for an empty data block
1688 * (this should always be true) then update the header.
1689 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001690 if (free->bests[findex] == cpu_to_be16(NULLDATAOFF)) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001691 be32_add_cpu(&free->hdr.nused, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 xfs_dir2_free_log_header(tp, fbp);
1693 }
1694 /*
1695 * Update the real value in the table.
1696 * We haven't allocated the data entry yet so this will
1697 * change again.
1698 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001699 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001700 free->bests[findex] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 logfree = 1;
1702 }
1703 /*
1704 * We had a data block so we don't have to make a new one.
1705 */
1706 else {
1707 /*
1708 * If just checking, we succeeded.
1709 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001710 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 /*
1714 * Read the data block in.
1715 */
Dave Chinnere4813572012-11-12 22:54:14 +11001716 error = xfs_dir2_data_read(tp, dp, xfs_dir2_db_to_da(mp, dbno),
1717 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001718 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001720 hdr = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 logfree = 0;
1722 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001723 ASSERT(be16_to_cpu(hdr->bestfree[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 /*
1725 * Point to the existing unused space.
1726 */
1727 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001728 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 needscan = needlog = 0;
1730 /*
1731 * Mark the first part of the unused space, inuse for us.
1732 */
1733 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001734 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 &needlog, &needscan);
1736 /*
1737 * Fill in the new entry and log it.
1738 */
1739 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001740 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 dep->namelen = args->namelen;
1742 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001743 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001744 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 xfs_dir2_data_log_entry(tp, dbp, dep);
1746 /*
1747 * Rescan the block for bestfree if needed.
1748 */
1749 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001750 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /*
1752 * Log the data block header if needed.
1753 */
1754 if (needlog)
1755 xfs_dir2_data_log_header(tp, dbp);
1756 /*
1757 * If the freespace entry is now wrong, update it.
1758 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001759 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(hdr->bestfree[0].length)) {
1760 free->bests[findex] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 logfree = 1;
1762 }
1763 /*
1764 * Log the freespace entry if needed.
1765 */
1766 if (logfree)
1767 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1768 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 * Return the data block and offset in args, then drop the data block.
1770 */
1771 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001772 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return 0;
1774}
1775
1776/*
1777 * Lookup an entry in a node-format directory.
1778 * All the real work happens in xfs_da_node_lookup_int.
1779 * The only real output is the inode number of the entry.
1780 */
1781int /* error */
1782xfs_dir2_node_lookup(
1783 xfs_da_args_t *args) /* operation arguments */
1784{
1785 int error; /* error return value */
1786 int i; /* btree level */
1787 int rval; /* operation return value */
1788 xfs_da_state_t *state; /* btree cursor */
1789
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001790 trace_xfs_dir2_node_lookup(args);
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /*
1793 * Allocate and initialize the btree cursor.
1794 */
1795 state = xfs_da_state_alloc();
1796 state->args = args;
1797 state->mp = args->dp->i_mount;
1798 state->blocksize = state->mp->m_dirblksize;
1799 state->node_ents = state->mp->m_dir_node_ents;
1800 /*
1801 * Fill in the path to the entry in the cursor.
1802 */
1803 error = xfs_da_node_lookup_int(state, &rval);
1804 if (error)
1805 rval = error;
Barry Naujok384f3ce2008-05-21 16:58:22 +10001806 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
1807 /* If a CI match, dup the actual name and return EEXIST */
1808 xfs_dir2_data_entry_t *dep;
1809
Dave Chinner1d9025e2012-06-22 18:50:14 +10001810 dep = (xfs_dir2_data_entry_t *)
1811 ((char *)state->extrablk.bp->b_addr +
1812 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001813 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /*
1816 * Release the btree blocks and leaf block.
1817 */
1818 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001819 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 state->path.blk[i].bp = NULL;
1821 }
1822 /*
1823 * Release the data block if we have it.
1824 */
1825 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001826 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 state->extrablk.bp = NULL;
1828 }
1829 xfs_da_state_free(state);
1830 return rval;
1831}
1832
1833/*
1834 * Remove an entry from a node-format directory.
1835 */
1836int /* error */
1837xfs_dir2_node_removename(
1838 xfs_da_args_t *args) /* operation arguments */
1839{
1840 xfs_da_state_blk_t *blk; /* leaf block */
1841 int error; /* error return value */
1842 int rval; /* operation return value */
1843 xfs_da_state_t *state; /* btree cursor */
1844
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001845 trace_xfs_dir2_node_removename(args);
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 /*
1848 * Allocate and initialize the btree cursor.
1849 */
1850 state = xfs_da_state_alloc();
1851 state->args = args;
1852 state->mp = args->dp->i_mount;
1853 state->blocksize = state->mp->m_dirblksize;
1854 state->node_ents = state->mp->m_dir_node_ents;
1855 /*
1856 * Look up the entry we're deleting, set up the cursor.
1857 */
1858 error = xfs_da_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001859 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 /*
1862 * Didn't find it, upper layer screwed up.
1863 */
1864 if (rval != EEXIST) {
1865 xfs_da_state_free(state);
1866 return rval;
1867 }
1868 blk = &state->path.blk[state->path.active - 1];
1869 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1870 ASSERT(state->extravalid);
1871 /*
1872 * Remove the leaf and data entries.
1873 * Extrablk refers to the data block.
1874 */
1875 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1876 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001877 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 /*
1880 * Fix the hash values up the btree.
1881 */
1882 xfs_da_fixhashpath(state, &state->path);
1883 /*
1884 * If we need to join leaf blocks, do it.
1885 */
1886 if (rval && state->path.active > 1)
1887 error = xfs_da_join(state);
1888 /*
1889 * If no errors so far, try conversion to leaf format.
1890 */
1891 if (!error)
1892 error = xfs_dir2_node_to_leaf(state);
1893 xfs_da_state_free(state);
1894 return error;
1895}
1896
1897/*
1898 * Replace an entry's inode number in a node-format directory.
1899 */
1900int /* error */
1901xfs_dir2_node_replace(
1902 xfs_da_args_t *args) /* operation arguments */
1903{
1904 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001905 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 xfs_dir2_data_entry_t *dep; /* data entry changed */
1907 int error; /* error return value */
1908 int i; /* btree level */
1909 xfs_ino_t inum; /* new inode number */
1910 xfs_dir2_leaf_t *leaf; /* leaf structure */
1911 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1912 int rval; /* internal return value */
1913 xfs_da_state_t *state; /* btree cursor */
1914
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001915 trace_xfs_dir2_node_replace(args);
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 /*
1918 * Allocate and initialize the btree cursor.
1919 */
1920 state = xfs_da_state_alloc();
1921 state->args = args;
1922 state->mp = args->dp->i_mount;
1923 state->blocksize = state->mp->m_dirblksize;
1924 state->node_ents = state->mp->m_dir_node_ents;
1925 inum = args->inumber;
1926 /*
1927 * Lookup the entry to change in the btree.
1928 */
1929 error = xfs_da_node_lookup_int(state, &rval);
1930 if (error) {
1931 rval = error;
1932 }
1933 /*
1934 * It should be found, since the vnodeops layer has looked it up
1935 * and locked it. But paranoia is good.
1936 */
1937 if (rval == EEXIST) {
1938 /*
1939 * Find the leaf entry.
1940 */
1941 blk = &state->path.blk[state->path.active - 1];
1942 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001943 leaf = blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 lep = &leaf->ents[blk->index];
1945 ASSERT(state->extravalid);
1946 /*
1947 * Point to the data entry.
1948 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001949 hdr = state->extrablk.bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001950 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001952 ((char *)hdr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001953 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001954 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 /*
1956 * Fill in the new inode number and log the entry.
1957 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001958 dep->inumber = cpu_to_be64(inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1960 rval = 0;
1961 }
1962 /*
1963 * Didn't find it, and we're holding a data block. Drop it.
1964 */
1965 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001966 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 state->extrablk.bp = NULL;
1968 }
1969 /*
1970 * Release all the buffers in the cursor.
1971 */
1972 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001973 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 state->path.blk[i].bp = NULL;
1975 }
1976 xfs_da_state_free(state);
1977 return rval;
1978}
1979
1980/*
1981 * Trim off a trailing empty freespace block.
1982 * Return (in rvalp) 1 if we did it, 0 if not.
1983 */
1984int /* error */
1985xfs_dir2_node_trim_free(
1986 xfs_da_args_t *args, /* operation arguments */
1987 xfs_fileoff_t fo, /* free block number */
1988 int *rvalp) /* out: did something */
1989{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001990 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 xfs_inode_t *dp; /* incore directory inode */
1992 int error; /* error return code */
1993 xfs_dir2_free_t *free; /* freespace structure */
1994 xfs_mount_t *mp; /* filesystem mount point */
1995 xfs_trans_t *tp; /* transaction pointer */
1996
1997 dp = args->dp;
1998 mp = dp->i_mount;
1999 tp = args->trans;
2000 /*
2001 * Read the freespace block.
2002 */
Dave Chinner20252072012-11-12 22:54:13 +11002003 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002004 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 /*
2007 * There can be holes in freespace. If fo is a hole, there's
2008 * nothing to do.
2009 */
Dave Chinner20252072012-11-12 22:54:13 +11002010 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002012 free = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002013 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 /*
2015 * If there are used entries, there's nothing to do.
2016 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11002017 if (be32_to_cpu(free->hdr.nused) > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002018 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 *rvalp = 0;
2020 return 0;
2021 }
2022 /*
2023 * Blow the block away.
2024 */
2025 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002026 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 bp))) {
2028 /*
2029 * Can't fail with ENOSPC since that only happens with no
2030 * space reservation, when breaking up an extent into two
2031 * pieces. This is the last block of an extent.
2032 */
2033 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002034 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return error;
2036 }
2037 /*
2038 * Return that we succeeded.
2039 */
2040 *rvalp = 1;
2041 return 0;
2042}