blob: fb0aad4440c1a5cf8044110c95cc78cdb9d709ca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
Dave Chinner24df33b2013-04-12 07:30:21 +10003 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110027#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_inode.h"
30#include "xfs_bmap.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100031#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020032#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"
Dave Chinner239880e2013-10-23 10:50:10 +110035#include "xfs_trans.h"
Dave Chinner24df33b2013-04-12 07:30:21 +100036#include "xfs_buf_item.h"
37#include "xfs_cksum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * Local function declarations.
41 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100042static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
43 int *indexp, struct xfs_buf **dbpp);
Dave Chinnerbc851782014-06-06 15:20:54 +100044static void xfs_dir3_leaf_log_bests(struct xfs_da_args *args,
45 struct xfs_buf *bp, int first, int last);
46static void xfs_dir3_leaf_log_tail(struct xfs_da_args *args,
47 struct xfs_buf *bp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100048
Dave Chinner24df33b2013-04-12 07:30:21 +100049/*
50 * Check the internal consistency of a leaf1 block.
51 * Pop an assert if something is wrong.
52 */
53#ifdef DEBUG
Dave Chinner41419562013-10-29 22:11:50 +110054#define xfs_dir3_leaf_check(dp, bp) \
Dave Chinner24df33b2013-04-12 07:30:21 +100055do { \
Dave Chinner41419562013-10-29 22:11:50 +110056 if (!xfs_dir3_leaf1_check((dp), (bp))) \
Dave Chinner24df33b2013-04-12 07:30:21 +100057 ASSERT(0); \
58} while (0);
59
60STATIC bool
61xfs_dir3_leaf1_check(
Dave Chinner41419562013-10-29 22:11:50 +110062 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100063 struct xfs_buf *bp)
64{
65 struct xfs_dir2_leaf *leaf = bp->b_addr;
66 struct xfs_dir3_icleaf_hdr leafhdr;
67
Dave Chinner01ba43b2013-10-29 22:11:52 +110068 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100069
70 if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
71 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
72 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
73 return false;
74 } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
75 return false;
76
Dave Chinner41419562013-10-29 22:11:50 +110077 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100078}
79#else
Dave Chinner41419562013-10-29 22:11:50 +110080#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100081#endif
82
Dave Chinner24df33b2013-04-12 07:30:21 +100083bool
84xfs_dir3_leaf_check_int(
85 struct xfs_mount *mp,
Dave Chinner41419562013-10-29 22:11:50 +110086 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100087 struct xfs_dir3_icleaf_hdr *hdr,
88 struct xfs_dir2_leaf *leaf)
89{
90 struct xfs_dir2_leaf_entry *ents;
91 xfs_dir2_leaf_tail_t *ltp;
92 int stale;
93 int i;
Dave Chinner41419562013-10-29 22:11:50 +110094 const struct xfs_dir_ops *ops;
Dave Chinner01ba43b2013-10-29 22:11:52 +110095 struct xfs_dir3_icleaf_hdr leafhdr;
Dave Chinner8f661932014-06-06 15:15:59 +100096 struct xfs_da_geometry *geo = mp->m_dir_geo;
Dave Chinner24df33b2013-04-12 07:30:21 +100097
Dave Chinner41419562013-10-29 22:11:50 +110098 /*
99 * we can be passed a null dp here from a verifier, so we need to go the
100 * hard way to get them.
101 */
102 ops = xfs_dir_get_ops(mp, dp);
103
Dave Chinner01ba43b2013-10-29 22:11:52 +1100104 if (!hdr) {
105 ops->leaf_hdr_from_disk(&leafhdr, leaf);
106 hdr = &leafhdr;
107 }
108
Dave Chinner41419562013-10-29 22:11:50 +1100109 ents = ops->leaf_ents_p(leaf);
Dave Chinner8f661932014-06-06 15:15:59 +1000110 ltp = xfs_dir2_leaf_tail_p(geo, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000111
112 /*
113 * XXX (dgc): This value is not restrictive enough.
114 * Should factor in the size of the bests table as well.
115 * We can deduce a value for that from di_size.
116 */
Dave Chinner8f661932014-06-06 15:15:59 +1000117 if (hdr->count > ops->leaf_max_ents(geo))
Dave Chinner24df33b2013-04-12 07:30:21 +1000118 return false;
119
120 /* Leaves and bests don't overlap in leaf format. */
121 if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
122 hdr->magic == XFS_DIR3_LEAF1_MAGIC) &&
123 (char *)&ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
124 return false;
125
126 /* Check hash value order, count stale entries. */
127 for (i = stale = 0; i < hdr->count; i++) {
128 if (i + 1 < hdr->count) {
129 if (be32_to_cpu(ents[i].hashval) >
130 be32_to_cpu(ents[i + 1].hashval))
131 return false;
132 }
133 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
134 stale++;
135 }
136 if (hdr->stale != stale)
137 return false;
138 return true;
139}
140
Dave Chinner0f295a22013-09-03 10:06:58 +1000141/*
142 * We verify the magic numbers before decoding the leaf header so that on debug
143 * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
144 * to incorrect magic numbers.
145 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000146static bool
147xfs_dir3_leaf_verify(
Dave Chinnere6f76672012-11-12 22:54:15 +1100148 struct xfs_buf *bp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000149 __uint16_t magic)
Dave Chinnere6f76672012-11-12 22:54:15 +1100150{
151 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner24df33b2013-04-12 07:30:21 +1000152 struct xfs_dir2_leaf *leaf = bp->b_addr;
Dave Chinnere6f76672012-11-12 22:54:15 +1100153
Dave Chinner24df33b2013-04-12 07:30:21 +1000154 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
155
Dave Chinner24df33b2013-04-12 07:30:21 +1000156 if (xfs_sb_version_hascrc(&mp->m_sb)) {
157 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
Dave Chinner0f295a22013-09-03 10:06:58 +1000158 __uint16_t magic3;
Dave Chinner24df33b2013-04-12 07:30:21 +1000159
Dave Chinner0f295a22013-09-03 10:06:58 +1000160 magic3 = (magic == XFS_DIR2_LEAF1_MAGIC) ? XFS_DIR3_LEAF1_MAGIC
161 : XFS_DIR3_LEAFN_MAGIC;
162
163 if (leaf3->info.hdr.magic != cpu_to_be16(magic3))
Dave Chinner24df33b2013-04-12 07:30:21 +1000164 return false;
Dave Chinner24df33b2013-04-12 07:30:21 +1000165 if (!uuid_equal(&leaf3->info.uuid, &mp->m_sb.sb_uuid))
166 return false;
167 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
168 return false;
169 } else {
Dave Chinner0f295a22013-09-03 10:06:58 +1000170 if (leaf->hdr.info.magic != cpu_to_be16(magic))
Dave Chinner24df33b2013-04-12 07:30:21 +1000171 return false;
172 }
Dave Chinner0f295a22013-09-03 10:06:58 +1000173
Dave Chinner01ba43b2013-10-29 22:11:52 +1100174 return xfs_dir3_leaf_check_int(mp, NULL, NULL, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000175}
176
177static void
178__read_verify(
179 struct xfs_buf *bp,
180 __uint16_t magic)
181{
182 struct xfs_mount *mp = bp->b_target->bt_mount;
183
Eric Sandeence5028c2014-02-27 15:23:10 +1100184 if (xfs_sb_version_hascrc(&mp->m_sb) &&
185 !xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF))
186 xfs_buf_ioerror(bp, EFSBADCRC);
187 else if (!xfs_dir3_leaf_verify(bp, magic))
Dave Chinnere6f76672012-11-12 22:54:15 +1100188 xfs_buf_ioerror(bp, EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100189
190 if (bp->b_error)
191 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100192}
Dave Chinnere6f76672012-11-12 22:54:15 +1100193
Dave Chinner612cfbf2012-11-14 17:52:32 +1100194static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000195__write_verify(
196 struct xfs_buf *bp,
197 __uint16_t magic)
Dave Chinner612cfbf2012-11-14 17:52:32 +1100198{
Dave Chinner24df33b2013-04-12 07:30:21 +1000199 struct xfs_mount *mp = bp->b_target->bt_mount;
200 struct xfs_buf_log_item *bip = bp->b_fspriv;
201 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
202
203 if (!xfs_dir3_leaf_verify(bp, magic)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000204 xfs_buf_ioerror(bp, EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100205 xfs_verifier_error(bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000206 return;
207 }
208
209 if (!xfs_sb_version_hascrc(&mp->m_sb))
210 return;
211
212 if (bip)
213 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
214
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100215 xfs_buf_update_cksum(bp, XFS_DIR3_LEAF_CRC_OFF);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100216}
217
218static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000219xfs_dir3_leaf1_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100220 struct xfs_buf *bp)
221{
Dave Chinner24df33b2013-04-12 07:30:21 +1000222 __read_verify(bp, XFS_DIR2_LEAF1_MAGIC);
Dave Chinner1813dd62012-11-14 17:54:40 +1100223}
224
Dave Chinner24df33b2013-04-12 07:30:21 +1000225static void
226xfs_dir3_leaf1_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100227 struct xfs_buf *bp)
228{
Dave Chinner24df33b2013-04-12 07:30:21 +1000229 __write_verify(bp, XFS_DIR2_LEAF1_MAGIC);
Dave Chinnere6f76672012-11-12 22:54:15 +1100230}
231
Dave Chinner24df33b2013-04-12 07:30:21 +1000232static void
233xfs_dir3_leafn_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100234 struct xfs_buf *bp)
Dave Chinnere6f76672012-11-12 22:54:15 +1100235{
Dave Chinner24df33b2013-04-12 07:30:21 +1000236 __read_verify(bp, XFS_DIR2_LEAFN_MAGIC);
Dave Chinnere6f76672012-11-12 22:54:15 +1100237}
238
Dave Chinner24df33b2013-04-12 07:30:21 +1000239static void
240xfs_dir3_leafn_write_verify(
241 struct xfs_buf *bp)
242{
243 __write_verify(bp, XFS_DIR2_LEAFN_MAGIC);
244}
245
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100246const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
Dave Chinner24df33b2013-04-12 07:30:21 +1000247 .verify_read = xfs_dir3_leaf1_read_verify,
248 .verify_write = xfs_dir3_leaf1_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100249};
250
Dave Chinner24df33b2013-04-12 07:30:21 +1000251const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
252 .verify_read = xfs_dir3_leafn_read_verify,
253 .verify_write = xfs_dir3_leafn_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100254};
Dave Chinnere6f76672012-11-12 22:54:15 +1100255
256static int
Dave Chinner24df33b2013-04-12 07:30:21 +1000257xfs_dir3_leaf_read(
Dave Chinnere6f76672012-11-12 22:54:15 +1100258 struct xfs_trans *tp,
259 struct xfs_inode *dp,
260 xfs_dablk_t fbno,
261 xfs_daddr_t mappedbno,
262 struct xfs_buf **bpp)
263{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100264 int err;
265
266 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000267 XFS_DATA_FORK, &xfs_dir3_leaf1_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100268 if (!err && tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100269 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100270 return err;
Dave Chinnere6f76672012-11-12 22:54:15 +1100271}
272
273int
Dave Chinner24df33b2013-04-12 07:30:21 +1000274xfs_dir3_leafn_read(
Dave Chinnere6f76672012-11-12 22:54:15 +1100275 struct xfs_trans *tp,
276 struct xfs_inode *dp,
277 xfs_dablk_t fbno,
278 xfs_daddr_t mappedbno,
279 struct xfs_buf **bpp)
280{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100281 int err;
282
283 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000284 XFS_DATA_FORK, &xfs_dir3_leafn_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100285 if (!err && tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100286 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100287 return err;
Dave Chinner24df33b2013-04-12 07:30:21 +1000288}
289
290/*
291 * Initialize a new leaf block, leaf1 or leafn magic accepted.
292 */
293static void
294xfs_dir3_leaf_init(
295 struct xfs_mount *mp,
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100296 struct xfs_trans *tp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000297 struct xfs_buf *bp,
298 xfs_ino_t owner,
299 __uint16_t type)
300{
301 struct xfs_dir2_leaf *leaf = bp->b_addr;
302
303 ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
304
305 if (xfs_sb_version_hascrc(&mp->m_sb)) {
306 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
307
308 memset(leaf3, 0, sizeof(*leaf3));
309
310 leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
311 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
312 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
313 leaf3->info.blkno = cpu_to_be64(bp->b_bn);
314 leaf3->info.owner = cpu_to_be64(owner);
315 uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_uuid);
316 } else {
317 memset(leaf, 0, sizeof(*leaf));
318 leaf->hdr.info.magic = cpu_to_be16(type);
319 }
320
321 /*
322 * If it's a leaf-format directory initialize the tail.
323 * Caller is responsible for initialising the bests table.
324 */
325 if (type == XFS_DIR2_LEAF1_MAGIC) {
326 struct xfs_dir2_leaf_tail *ltp;
327
Dave Chinner8f661932014-06-06 15:15:59 +1000328 ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000329 ltp->bestcount = 0;
330 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100331 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100332 } else {
Dave Chinner24df33b2013-04-12 07:30:21 +1000333 bp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100334 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100335 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000336}
337
338int
339xfs_dir3_leaf_get_buf(
340 xfs_da_args_t *args,
341 xfs_dir2_db_t bno,
342 struct xfs_buf **bpp,
343 __uint16_t magic)
344{
345 struct xfs_inode *dp = args->dp;
346 struct xfs_trans *tp = args->trans;
347 struct xfs_mount *mp = dp->i_mount;
348 struct xfs_buf *bp;
349 int error;
350
351 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner30028032014-06-06 15:08:18 +1000352 ASSERT(bno >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET) &&
353 bno < xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Dave Chinner24df33b2013-04-12 07:30:21 +1000354
Dave Chinner2998ab12014-06-06 15:07:53 +1000355 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno),
356 -1, &bp, XFS_DATA_FORK);
Dave Chinner24df33b2013-04-12 07:30:21 +1000357 if (error)
358 return error;
359
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100360 xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
Dave Chinnerbc851782014-06-06 15:20:54 +1000361 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000362 if (magic == XFS_DIR2_LEAF1_MAGIC)
Dave Chinnerbc851782014-06-06 15:20:54 +1000363 xfs_dir3_leaf_log_tail(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000364 *bpp = bp;
365 return 0;
Dave Chinnere6f76672012-11-12 22:54:15 +1100366}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368/*
369 * Convert a block form directory to a leaf form directory.
370 */
371int /* error */
372xfs_dir2_block_to_leaf(
373 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000374 struct xfs_buf *dbp) /* input block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Nathan Scott68b3a102006-03-17 17:27:19 +1100376 __be16 *bestsp; /* leaf's bestsp entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 xfs_dablk_t blkno; /* leaf block's bno */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200378 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
380 xfs_dir2_block_tail_t *btp; /* block's tail */
381 xfs_inode_t *dp; /* incore directory inode */
382 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000383 struct xfs_buf *lbp; /* leaf block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 xfs_dir2_db_t ldb; /* leaf block's bno */
385 xfs_dir2_leaf_t *leaf; /* leaf structure */
386 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
387 xfs_mount_t *mp; /* filesystem mount point */
388 int needlog; /* need to log block header */
389 int needscan; /* need to rescan bestfree */
390 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +1100391 struct xfs_dir2_data_free *bf;
Dave Chinner24df33b2013-04-12 07:30:21 +1000392 struct xfs_dir2_leaf_entry *ents;
393 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000395 trace_xfs_dir2_block_to_leaf(args);
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 dp = args->dp;
398 mp = dp->i_mount;
399 tp = args->trans;
400 /*
401 * Add the leaf block to the inode.
402 * This interface will only put blocks in the leaf/node range.
403 * Since that's empty now, we'll get the root (block 0 in range).
404 */
405 if ((error = xfs_da_grow_inode(args, &blkno))) {
406 return error;
407 }
Dave Chinner2998ab12014-06-06 15:07:53 +1000408 ldb = xfs_dir2_da_to_db(args->geo, blkno);
Dave Chinner30028032014-06-06 15:08:18 +1000409 ASSERT(ldb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /*
411 * Initialize the leaf block, get a buffer for it.
412 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000413 error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
414 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +1000416
Dave Chinner1d9025e2012-06-22 18:50:14 +1000417 leaf = lbp->b_addr;
418 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100419 xfs_dir3_data_check(dp, dbp);
Dave Chinner8f661932014-06-06 15:15:59 +1000420 btp = xfs_dir2_block_tail_p(args->geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000421 blp = xfs_dir2_block_leaf_p(btp);
Dave Chinner2ca98772013-10-29 22:11:49 +1100422 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner41419562013-10-29 22:11:50 +1100423 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /*
426 * Set the counts in the leaf header.
427 */
Dave Chinner01ba43b2013-10-29 22:11:52 +1100428 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000429 leafhdr.count = be32_to_cpu(btp->count);
430 leafhdr.stale = be32_to_cpu(btp->stale);
Dave Chinner01ba43b2013-10-29 22:11:52 +1100431 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000432 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /*
435 * Could compact these but I think we always do the conversion
436 * after squeezing out stale entries.
437 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000438 memcpy(ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000439 xfs_dir3_leaf_log_ents(args, lbp, 0, leafhdr.count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 needscan = 0;
441 needlog = 1;
442 /*
443 * Make the space formerly occupied by the leaf entries and block
444 * tail be free.
445 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000446 xfs_dir2_data_make_free(args, dbp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200447 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
Dave Chinner8f661932014-06-06 15:15:59 +1000448 (xfs_dir2_data_aoff_t)((char *)hdr + args->geo->blksize -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 (char *)blp),
450 &needlog, &needscan);
451 /*
452 * Fix up the block header, make it a data block.
453 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100454 dbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100455 xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
Dave Chinner33363fe2013-04-03 16:11:22 +1100456 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
457 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
458 else
459 hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +1100462 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /*
464 * Set up leaf tail and bests table.
465 */
Dave Chinner8f661932014-06-06 15:15:59 +1000466 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100467 ltp->bestcount = cpu_to_be32(1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000468 bestsp = xfs_dir2_leaf_bests_p(ltp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100469 bestsp[0] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /*
471 * Log the data header and leaf bests table.
472 */
473 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +1000474 xfs_dir2_data_log_header(args, dbp);
Dave Chinner41419562013-10-29 22:11:50 +1100475 xfs_dir3_leaf_check(dp, lbp);
Dave Chinner33363fe2013-04-03 16:11:22 +1100476 xfs_dir3_data_check(dp, dbp);
Dave Chinnerbc851782014-06-06 15:20:54 +1000477 xfs_dir3_leaf_log_bests(args, lbp, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return 0;
479}
480
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200481STATIC void
Dave Chinner24df33b2013-04-12 07:30:21 +1000482xfs_dir3_leaf_find_stale(
483 struct xfs_dir3_icleaf_hdr *leafhdr,
484 struct xfs_dir2_leaf_entry *ents,
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200485 int index,
486 int *lowstale,
487 int *highstale)
488{
489 /*
490 * Find the first stale entry before our index, if any.
491 */
492 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000493 if (ents[*lowstale].address ==
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200494 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
495 break;
496 }
497
498 /*
499 * Find the first stale entry at or after our index, if any.
500 * Stop if the result would require moving more entries than using
501 * lowstale.
502 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000503 for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
504 if (ents[*highstale].address ==
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200505 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
506 break;
507 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
508 break;
509 }
510}
511
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200512struct xfs_dir2_leaf_entry *
Dave Chinner24df33b2013-04-12 07:30:21 +1000513xfs_dir3_leaf_find_entry(
514 struct xfs_dir3_icleaf_hdr *leafhdr,
515 struct xfs_dir2_leaf_entry *ents,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200516 int index, /* leaf table position */
517 int compact, /* need to compact leaves */
518 int lowstale, /* index of prev stale leaf */
519 int highstale, /* index of next stale leaf */
520 int *lfloglow, /* low leaf logging index */
521 int *lfloghigh) /* high leaf logging index */
522{
Dave Chinner24df33b2013-04-12 07:30:21 +1000523 if (!leafhdr->stale) {
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200524 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
525
526 /*
527 * Now we need to make room to insert the leaf entry.
528 *
529 * If there are no stale entries, just insert a hole at index.
530 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000531 lep = &ents[index];
532 if (index < leafhdr->count)
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200533 memmove(lep + 1, lep,
Dave Chinner24df33b2013-04-12 07:30:21 +1000534 (leafhdr->count - index) * sizeof(*lep));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200535
536 /*
537 * Record low and high logging indices for the leaf.
538 */
539 *lfloglow = index;
Dave Chinner24df33b2013-04-12 07:30:21 +1000540 *lfloghigh = leafhdr->count++;
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200541 return lep;
542 }
543
544 /*
545 * There are stale entries.
546 *
547 * We will use one of them for the new entry. It's probably not at
548 * the right location, so we'll have to shift some up or down first.
549 *
550 * If we didn't compact before, we need to find the nearest stale
551 * entries before and after our insertion point.
552 */
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200553 if (compact == 0)
Dave Chinner24df33b2013-04-12 07:30:21 +1000554 xfs_dir3_leaf_find_stale(leafhdr, ents, index,
555 &lowstale, &highstale);
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200556
557 /*
558 * If the low one is better, use it.
559 */
560 if (lowstale >= 0 &&
Dave Chinner24df33b2013-04-12 07:30:21 +1000561 (highstale == leafhdr->count ||
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200562 index - lowstale - 1 < highstale - index)) {
563 ASSERT(index - lowstale - 1 >= 0);
Dave Chinner24df33b2013-04-12 07:30:21 +1000564 ASSERT(ents[lowstale].address ==
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200565 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200566
567 /*
568 * Copy entries up to cover the stale entry and make room
569 * for the new entry.
570 */
571 if (index - lowstale - 1 > 0) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000572 memmove(&ents[lowstale], &ents[lowstale + 1],
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200573 (index - lowstale - 1) *
Dave Chinner24df33b2013-04-12 07:30:21 +1000574 sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200575 }
576 *lfloglow = MIN(lowstale, *lfloglow);
577 *lfloghigh = MAX(index - 1, *lfloghigh);
Dave Chinner24df33b2013-04-12 07:30:21 +1000578 leafhdr->stale--;
579 return &ents[index - 1];
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200580 }
581
582 /*
583 * The high one is better, so use that one.
584 */
585 ASSERT(highstale - index >= 0);
Dave Chinner24df33b2013-04-12 07:30:21 +1000586 ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200587
588 /*
589 * Copy entries down to cover the stale entry and make room for the
590 * new entry.
591 */
592 if (highstale - index > 0) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000593 memmove(&ents[index + 1], &ents[index],
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200594 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
595 }
596 *lfloglow = MIN(index, *lfloglow);
597 *lfloghigh = MAX(highstale, *lfloghigh);
Dave Chinner24df33b2013-04-12 07:30:21 +1000598 leafhdr->stale--;
599 return &ents[index];
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602/*
603 * Add an entry to a leaf form directory.
604 */
605int /* error */
606xfs_dir2_leaf_addname(
607 xfs_da_args_t *args) /* operation arguments */
608{
Nathan Scott68b3a102006-03-17 17:27:19 +1100609 __be16 *bestsp; /* freespace table in leaf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 int compact; /* need to compact leaves */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200611 xfs_dir2_data_hdr_t *hdr; /* data block header */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000612 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 xfs_dir2_data_entry_t *dep; /* data block entry */
614 xfs_inode_t *dp; /* incore directory inode */
615 xfs_dir2_data_unused_t *dup; /* data unused entry */
616 int error; /* error return value */
617 int grown; /* allocated new data block */
618 int highstale; /* index of next stale leaf */
619 int i; /* temporary, index */
620 int index; /* leaf table position */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000621 struct xfs_buf *lbp; /* leaf's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 xfs_dir2_leaf_t *leaf; /* leaf structure */
623 int length; /* length of new entry */
624 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
625 int lfloglow; /* low leaf logging index */
626 int lfloghigh; /* high leaf logging index */
627 int lowstale; /* index of prev stale leaf */
628 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
629 xfs_mount_t *mp; /* filesystem mount point */
630 int needbytes; /* leaf block bytes needed */
631 int needlog; /* need to log data header */
632 int needscan; /* need to rescan data free */
Nathan Scott3d693c62006-03-17 17:28:27 +1100633 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 xfs_trans_t *tp; /* transaction pointer */
635 xfs_dir2_db_t use_block; /* data block number */
Dave Chinner33363fe2013-04-03 16:11:22 +1100636 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +1000637 struct xfs_dir2_leaf_entry *ents;
638 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000640 trace_xfs_dir2_leaf_addname(args);
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 dp = args->dp;
643 tp = args->trans;
644 mp = dp->i_mount;
Dave Chinnere6f76672012-11-12 22:54:15 +1100645
Dave Chinner7dda6e82014-06-06 15:11:18 +1000646 error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, -1, &lbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100647 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +1100649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /*
651 * Look up the entry by hash value and name.
652 * We know it's not there, our caller has already done a lookup.
653 * So the index is of the entry to insert in front of.
654 * But if there are dup hash values the index is of the first of those.
655 */
656 index = xfs_dir2_leaf_search_hash(args, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000657 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000658 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100659 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner01ba43b2013-10-29 22:11:52 +1100660 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000661 bestsp = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100662 length = dp->d_ops->data_entsize(args->namelen);
Dave Chinner24df33b2013-04-12 07:30:21 +1000663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /*
665 * See if there are any entries with the same hash value
666 * and space in their block for the new entry.
667 * This is good because it puts multiple same-hash value entries
668 * in a data block, improving the lookup of those entries.
669 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000670 for (use_block = -1, lep = &ents[index];
671 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 index++, lep++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100673 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 continue;
Dave Chinner30028032014-06-06 15:08:18 +1000675 i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100676 ASSERT(i < be32_to_cpu(ltp->bestcount));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200677 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
Nathan Scott68b3a102006-03-17 17:27:19 +1100678 if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 use_block = i;
680 break;
681 }
682 }
683 /*
684 * Didn't find a block yet, linear search all the data blocks.
685 */
686 if (use_block == -1) {
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100687 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /*
689 * Remember a block we see that's missing.
690 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200691 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
692 use_block == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 use_block = i;
Nathan Scott68b3a102006-03-17 17:27:19 +1100694 else if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 use_block = i;
696 break;
697 }
698 }
699 }
700 /*
701 * How many bytes do we need in the leaf block?
702 */
Christoph Hellwig22823962011-07-08 14:35:53 +0200703 needbytes = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000704 if (!leafhdr.stale)
Christoph Hellwig22823962011-07-08 14:35:53 +0200705 needbytes += sizeof(xfs_dir2_leaf_entry_t);
706 if (use_block == -1)
707 needbytes += sizeof(xfs_dir2_data_off_t);
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /*
710 * Now kill use_block if it refers to a missing block, so we
711 * can use it as an indication of allocation needed.
712 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200713 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 use_block = -1;
715 /*
716 * If we don't have enough free bytes but we can make enough
717 * by compacting out stale entries, we'll do that.
718 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000719 if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
720 leafhdr.stale > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 compact = 1;
Dave Chinner24df33b2013-04-12 07:30:21 +1000722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /*
724 * Otherwise if we don't have enough free bytes we need to
725 * convert to node form.
726 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000727 else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /*
729 * Just checking or no space reservation, give up.
730 */
Barry Naujok6a178102008-05-21 16:42:05 +1000731 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
732 args->total == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000733 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return XFS_ERROR(ENOSPC);
735 }
736 /*
737 * Convert to node form.
738 */
739 error = xfs_dir2_leaf_to_node(args, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (error)
741 return error;
742 /*
743 * Then add the new entry.
744 */
745 return xfs_dir2_node_addname(args);
746 }
747 /*
748 * Otherwise it will fit without compaction.
749 */
750 else
751 compact = 0;
752 /*
753 * If just checking, then it will fit unless we needed to allocate
754 * a new data block.
755 */
Barry Naujok6a178102008-05-21 16:42:05 +1000756 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000757 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
759 }
760 /*
761 * If no allocations are allowed, return now before we've
762 * changed anything.
763 */
764 if (args->total == 0 && use_block == -1) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000765 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return XFS_ERROR(ENOSPC);
767 }
768 /*
769 * Need to compact the leaf entries, removing stale ones.
770 * Leave one stale entry behind - the one closest to our
771 * insertion index - and we'll shift that one to our insertion
772 * point later.
773 */
774 if (compact) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000775 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
776 &highstale, &lfloglow, &lfloghigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778 /*
779 * There are stale entries, so we'll need log-low and log-high
780 * impossibly bad values later.
781 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000782 else if (leafhdr.stale) {
783 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 lfloghigh = -1;
785 }
786 /*
787 * If there was no data block space found, we need to allocate
788 * a new one.
789 */
790 if (use_block == -1) {
791 /*
792 * Add the new data block.
793 */
794 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
795 &use_block))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000796 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return error;
798 }
799 /*
800 * Initialize the block.
801 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100802 if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000803 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return error;
805 }
806 /*
807 * If we're adding a new data block on the end we need to
808 * extend the bests table. Copy it up one entry.
809 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100810 if (use_block >= be32_to_cpu(ltp->bestcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 bestsp--;
812 memmove(&bestsp[0], &bestsp[1],
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100813 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800814 be32_add_cpu(&ltp->bestcount, 1);
Dave Chinnerbc851782014-06-06 15:20:54 +1000815 xfs_dir3_leaf_log_tail(args, lbp);
816 xfs_dir3_leaf_log_bests(args, lbp, 0,
817 be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819 /*
820 * If we're filling in a previously empty block just log it.
821 */
822 else
Dave Chinnerbc851782014-06-06 15:20:54 +1000823 xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000824 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +1100825 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +1100826 bestsp[use_block] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 grown = 1;
Dave Chinnere4813572012-11-12 22:54:14 +1100828 } else {
829 /*
830 * Already had space in some data block.
831 * Just read that one in.
832 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100833 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab12014-06-06 15:07:53 +1000834 xfs_dir2_db_to_da(args->geo, use_block),
835 -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100836 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000837 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return error;
839 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000840 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +1100841 bf = dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 grown = 0;
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /*
845 * Point to the biggest freespace in our data block.
846 */
847 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +1100848 ((char *)hdr + be16_to_cpu(bf[0].offset));
Nathan Scottad354eb2006-03-17 17:27:37 +1100849 ASSERT(be16_to_cpu(dup->length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 needscan = needlog = 0;
851 /*
852 * Mark the initial part of our freespace in use for the new entry.
853 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000854 xfs_dir2_data_use_free(args, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200855 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 &needlog, &needscan);
857 /*
858 * Initialize our new entry (at last).
859 */
860 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000861 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 dep->namelen = args->namelen;
863 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100864 dp->d_ops->data_put_ftype(dep, args->filetype);
865 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200866 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /*
868 * Need to scan fix up the bestfree table.
869 */
870 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +1100871 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /*
873 * Need to log the data block's header.
874 */
875 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +1000876 xfs_dir2_data_log_header(args, dbp);
877 xfs_dir2_data_log_entry(args, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /*
879 * If the bests table needs to be changed, do it.
880 * Log the change unless we've already done that.
881 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100882 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
883 bestsp[use_block] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (!grown)
Dave Chinnerbc851782014-06-06 15:20:54 +1000885 xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200887
Dave Chinner24df33b2013-04-12 07:30:21 +1000888 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200889 highstale, &lfloglow, &lfloghigh);
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /*
892 * Fill in the new leaf entry.
893 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100894 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000895 lep->address = cpu_to_be32(
896 xfs_dir2_db_off_to_dataptr(args->geo, use_block,
Nathan Scott3d693c62006-03-17 17:28:27 +1100897 be16_to_cpu(*tagp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /*
899 * Log the leaf fields and give up the buffers.
900 */
Dave Chinner01ba43b2013-10-29 22:11:52 +1100901 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000902 xfs_dir3_leaf_log_header(args, lbp);
903 xfs_dir3_leaf_log_ents(args, lbp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100904 xfs_dir3_leaf_check(dp, lbp);
Dave Chinner33363fe2013-04-03 16:11:22 +1100905 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return 0;
907}
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909/*
910 * Compact out any stale entries in the leaf.
911 * Log the header and changed leaf entries, if any.
912 */
913void
Dave Chinner24df33b2013-04-12 07:30:21 +1000914xfs_dir3_leaf_compact(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 xfs_da_args_t *args, /* operation arguments */
Dave Chinner24df33b2013-04-12 07:30:21 +1000916 struct xfs_dir3_icleaf_hdr *leafhdr,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000917 struct xfs_buf *bp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 int from; /* source leaf index */
920 xfs_dir2_leaf_t *leaf; /* leaf structure */
921 int loglow; /* first leaf entry to log */
922 int to; /* target leaf index */
Dave Chinner24df33b2013-04-12 07:30:21 +1000923 struct xfs_dir2_leaf_entry *ents;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100924 struct xfs_inode *dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Dave Chinner1d9025e2012-06-22 18:50:14 +1000926 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +1000927 if (!leafhdr->stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /*
931 * Compress out the stale entries in place.
932 */
Dave Chinner01ba43b2013-10-29 22:11:52 +1100933 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000934 for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
935 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 continue;
937 /*
938 * Only actually copy the entries that are different.
939 */
940 if (from > to) {
941 if (loglow == -1)
942 loglow = to;
Dave Chinner24df33b2013-04-12 07:30:21 +1000943 ents[to] = ents[from];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945 to++;
946 }
947 /*
948 * Update and log the header, log the leaf entries.
949 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000950 ASSERT(leafhdr->stale == from - to);
951 leafhdr->count -= leafhdr->stale;
952 leafhdr->stale = 0;
953
Dave Chinner01ba43b2013-10-29 22:11:52 +1100954 dp->d_ops->leaf_hdr_to_disk(leaf, leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000955 xfs_dir3_leaf_log_header(args, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (loglow != -1)
Dave Chinnerbc851782014-06-06 15:20:54 +1000957 xfs_dir3_leaf_log_ents(args, bp, loglow, to - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960/*
961 * Compact the leaf entries, removing stale ones.
962 * Leave one stale entry behind - the one closest to our
963 * insertion index - and the caller will shift that one to our insertion
964 * point later.
965 * Return new insertion index, where the remaining stale entry is,
966 * and leaf logging indices.
967 */
968void
Dave Chinner24df33b2013-04-12 07:30:21 +1000969xfs_dir3_leaf_compact_x1(
970 struct xfs_dir3_icleaf_hdr *leafhdr,
971 struct xfs_dir2_leaf_entry *ents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 int *indexp, /* insertion index */
973 int *lowstalep, /* out: stale entry before us */
974 int *highstalep, /* out: stale entry after us */
975 int *lowlogp, /* out: low log index */
976 int *highlogp) /* out: high log index */
977{
978 int from; /* source copy index */
979 int highstale; /* stale entry at/after index */
980 int index; /* insertion index */
981 int keepstale; /* source index of kept stale */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int lowstale; /* stale entry before index */
983 int newindex=0; /* new insertion index */
984 int to; /* destination copy index */
985
Dave Chinner24df33b2013-04-12 07:30:21 +1000986 ASSERT(leafhdr->stale > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 index = *indexp;
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200988
Dave Chinner24df33b2013-04-12 07:30:21 +1000989 xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /*
992 * Pick the better of lowstale and highstale.
993 */
994 if (lowstale >= 0 &&
Dave Chinner24df33b2013-04-12 07:30:21 +1000995 (highstale == leafhdr->count ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 index - lowstale <= highstale - index))
997 keepstale = lowstale;
998 else
999 keepstale = highstale;
1000 /*
1001 * Copy the entries in place, removing all the stale entries
1002 * except keepstale.
1003 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001004 for (from = to = 0; from < leafhdr->count; from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /*
1006 * Notice the new value of index.
1007 */
1008 if (index == from)
1009 newindex = to;
1010 if (from != keepstale &&
Dave Chinner24df33b2013-04-12 07:30:21 +10001011 ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (from == to)
1013 *lowlogp = to;
1014 continue;
1015 }
1016 /*
1017 * Record the new keepstale value for the insertion.
1018 */
1019 if (from == keepstale)
1020 lowstale = highstale = to;
1021 /*
1022 * Copy only the entries that have moved.
1023 */
1024 if (from > to)
Dave Chinner24df33b2013-04-12 07:30:21 +10001025 ents[to] = ents[from];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 to++;
1027 }
1028 ASSERT(from > to);
1029 /*
1030 * If the insertion point was past the last entry,
1031 * set the new insertion point accordingly.
1032 */
1033 if (index == from)
1034 newindex = to;
1035 *indexp = newindex;
1036 /*
1037 * Adjust the leaf header values.
1038 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001039 leafhdr->count -= from - to;
1040 leafhdr->stale = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /*
1042 * Remember the low/high stale value only in the "right"
1043 * direction.
1044 */
1045 if (lowstale >= newindex)
1046 lowstale = -1;
1047 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001048 highstale = leafhdr->count;
1049 *highlogp = leafhdr->count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 *lowstalep = lowstale;
1051 *highstalep = highstale;
1052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/*
1055 * Log the bests entries indicated from a leaf1 block.
1056 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001057static void
Dave Chinner24df33b2013-04-12 07:30:21 +10001058xfs_dir3_leaf_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +10001059 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +10001060 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 int first, /* first entry to log */
1062 int last) /* last entry to log */
1063{
Nathan Scott68b3a102006-03-17 17:27:19 +11001064 __be16 *firstb; /* pointer to first entry */
1065 __be16 *lastb; /* pointer to last entry */
Dave Chinner24df33b2013-04-12 07:30:21 +10001066 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1068
Dave Chinner24df33b2013-04-12 07:30:21 +10001069 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1070 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1071
Dave Chinnerbc851782014-06-06 15:20:54 +10001072 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001073 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1074 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
Dave Chinnerbc851782014-06-06 15:20:54 +10001075 xfs_trans_log_buf(args->trans, bp,
1076 (uint)((char *)firstb - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1078}
1079
1080/*
1081 * Log the leaf entries indicated from a leaf1 or leafn block.
1082 */
1083void
Dave Chinner24df33b2013-04-12 07:30:21 +10001084xfs_dir3_leaf_log_ents(
Dave Chinnerbc851782014-06-06 15:20:54 +10001085 struct xfs_da_args *args,
Dave Chinner41419562013-10-29 22:11:50 +11001086 struct xfs_buf *bp,
1087 int first,
1088 int last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1091 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
Dave Chinner24df33b2013-04-12 07:30:21 +10001092 struct xfs_dir2_leaf *leaf = bp->b_addr;
1093 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001095 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001096 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1097 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1098 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1099
Dave Chinnerbc851782014-06-06 15:20:54 +10001100 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001101 firstlep = &ents[first];
1102 lastlep = &ents[last];
Dave Chinnerbc851782014-06-06 15:20:54 +10001103 xfs_trans_log_buf(args->trans, bp,
1104 (uint)((char *)firstlep - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1106}
1107
1108/*
1109 * Log the header of the leaf1 or leafn block.
1110 */
1111void
Dave Chinner24df33b2013-04-12 07:30:21 +10001112xfs_dir3_leaf_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +10001113 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +10001114 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Dave Chinner24df33b2013-04-12 07:30:21 +10001116 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001118 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001119 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1120 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1121 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1122
Dave Chinnerbc851782014-06-06 15:20:54 +10001123 xfs_trans_log_buf(args->trans, bp,
1124 (uint)((char *)&leaf->hdr - (char *)leaf),
1125 args->dp->d_ops->leaf_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128/*
1129 * Log the tail of the leaf1 block.
1130 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001131STATIC void
Dave Chinner24df33b2013-04-12 07:30:21 +10001132xfs_dir3_leaf_log_tail(
Dave Chinnerbc851782014-06-06 15:20:54 +10001133 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +10001134 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Dave Chinner24df33b2013-04-12 07:30:21 +10001136 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Dave Chinner24df33b2013-04-12 07:30:21 +10001139 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1140 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1141 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1142 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1143
Dave Chinnerbc851782014-06-06 15:20:54 +10001144 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1145 xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf),
1146 (uint)(args->geo->blksize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
1149/*
1150 * Look up the entry referred to by args in the leaf format directory.
1151 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1152 * is also used by the node-format code.
1153 */
1154int
1155xfs_dir2_leaf_lookup(
1156 xfs_da_args_t *args) /* operation arguments */
1157{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001158 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 xfs_dir2_data_entry_t *dep; /* data block entry */
1160 xfs_inode_t *dp; /* incore directory inode */
1161 int error; /* error return code */
1162 int index; /* found entry index */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001163 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 xfs_dir2_leaf_t *leaf; /* leaf structure */
1165 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1166 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10001167 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001169 trace_xfs_dir2_leaf_lookup(args);
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /*
1172 * Look up name in the leaf block, returning both buffers and index.
1173 */
1174 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1175 return error;
1176 }
1177 tp = args->trans;
1178 dp = args->dp;
Dave Chinner41419562013-10-29 22:11:50 +11001179 xfs_dir3_leaf_check(dp, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001180 leaf = lbp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11001181 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 /*
1183 * Get to the leaf entry and contained data entry address.
1184 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001185 lep = &ents[index];
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /*
1188 * Point to the data entry.
1189 */
1190 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001191 ((char *)dbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +10001192 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +10001194 * Return the found inode number & CI name if appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001196 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001197 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001198 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001199 xfs_trans_brelse(tp, dbp);
1200 xfs_trans_brelse(tp, lbp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001201 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204/*
1205 * Look up name/hash in the leaf block.
1206 * Fill in indexp with the found index, and dbpp with the data buffer.
1207 * If not found dbpp will be NULL, and ENOENT comes back.
1208 * lbpp will always be filled in with the leaf buffer unless there's an error.
1209 */
1210static int /* error */
1211xfs_dir2_leaf_lookup_int(
1212 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001213 struct xfs_buf **lbpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int *indexp, /* out: index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001215 struct xfs_buf **dbpp) /* out: data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001217 xfs_dir2_db_t curdb = -1; /* current data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001218 struct xfs_buf *dbp = NULL; /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 xfs_dir2_data_entry_t *dep; /* data entry */
1220 xfs_inode_t *dp; /* incore directory inode */
1221 int error; /* error return code */
1222 int index; /* index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001223 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1225 xfs_dir2_leaf_t *leaf; /* leaf structure */
1226 xfs_mount_t *mp; /* filesystem mount point */
1227 xfs_dir2_db_t newdb; /* new data block number */
1228 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001229 xfs_dir2_db_t cidb = -1; /* case match data block no. */
Barry Naujok5163f952008-05-21 16:41:01 +10001230 enum xfs_dacmp cmp; /* name compare result */
Dave Chinner24df33b2013-04-12 07:30:21 +10001231 struct xfs_dir2_leaf_entry *ents;
1232 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 dp = args->dp;
1235 tp = args->trans;
1236 mp = dp->i_mount;
Dave Chinnere6f76672012-11-12 22:54:15 +11001237
Dave Chinner7dda6e82014-06-06 15:11:18 +10001238 error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, -1, &lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001239 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 *lbpp = lbp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001243 leaf = lbp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11001244 xfs_dir3_leaf_check(dp, lbp);
1245 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001246 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /*
1249 * Look for the first leaf entry with our hash value.
1250 */
1251 index = xfs_dir2_leaf_search_hash(args, lbp);
1252 /*
1253 * Loop over all the entries with the right hash value
1254 * looking to match the name.
1255 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001256 for (lep = &ents[index];
1257 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
1258 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /*
1260 * Skip over stale leaf entries.
1261 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001262 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 continue;
1264 /*
1265 * Get the new data block number.
1266 */
Dave Chinner30028032014-06-06 15:08:18 +10001267 newdb = xfs_dir2_dataptr_to_db(args->geo,
1268 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 /*
1270 * If it's not the same as the old data block number,
1271 * need to pitch the old one and read the new one.
1272 */
1273 if (newdb != curdb) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001274 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001275 xfs_trans_brelse(tp, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001276 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab12014-06-06 15:07:53 +10001277 xfs_dir2_db_to_da(args->geo, newdb),
1278 -1, &dbp);
Barry Naujok5163f952008-05-21 16:41:01 +10001279 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001280 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return error;
1282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 curdb = newdb;
1284 }
1285 /*
1286 * Point to the data entry.
1287 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001288 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +10001289 xfs_dir2_dataptr_to_off(args->geo,
1290 be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /*
Barry Naujok5163f952008-05-21 16:41:01 +10001292 * Compare name and if it's an exact match, return the index
1293 * and buffer. If it's the first case-insensitive match, store
1294 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 */
Barry Naujok5163f952008-05-21 16:41:01 +10001296 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1297 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1298 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 *indexp = index;
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001300 /* case exact match: return the current buffer. */
Barry Naujok5163f952008-05-21 16:41:01 +10001301 if (cmp == XFS_CMP_EXACT) {
Barry Naujok5163f952008-05-21 16:41:01 +10001302 *dbpp = dbp;
1303 return 0;
1304 }
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001305 cidb = curdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307 }
Barry Naujok6a178102008-05-21 16:42:05 +10001308 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +10001309 /*
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001310 * Here, we can only be doing a lookup (not a rename or remove).
1311 * If a case-insensitive match was found earlier, re-read the
1312 * appropriate data block if required and return it.
Barry Naujok5163f952008-05-21 16:41:01 +10001313 */
1314 if (args->cmpresult == XFS_CMP_CASE) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001315 ASSERT(cidb != -1);
1316 if (cidb != curdb) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001317 xfs_trans_brelse(tp, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001318 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab12014-06-06 15:07:53 +10001319 xfs_dir2_db_to_da(args->geo, cidb),
1320 -1, &dbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001321 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001322 xfs_trans_brelse(tp, lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001323 return error;
1324 }
1325 }
1326 *dbpp = dbp;
Barry Naujok5163f952008-05-21 16:41:01 +10001327 return 0;
1328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 /*
1330 * No match found, return ENOENT.
1331 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001332 ASSERT(cidb == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001334 xfs_trans_brelse(tp, dbp);
1335 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return XFS_ERROR(ENOENT);
1337}
1338
1339/*
1340 * Remove an entry from a leaf format directory.
1341 */
1342int /* error */
1343xfs_dir2_leaf_removename(
1344 xfs_da_args_t *args) /* operation arguments */
1345{
Nathan Scott68b3a102006-03-17 17:27:19 +11001346 __be16 *bestsp; /* leaf block best freespace */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001347 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001349 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 xfs_dir2_data_entry_t *dep; /* data entry structure */
1351 xfs_inode_t *dp; /* incore directory inode */
1352 int error; /* error return code */
1353 xfs_dir2_db_t i; /* temporary data block # */
1354 int index; /* index into leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001355 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 xfs_dir2_leaf_t *leaf; /* leaf structure */
1357 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1358 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1359 xfs_mount_t *mp; /* filesystem mount point */
1360 int needlog; /* need to log data header */
1361 int needscan; /* need to rescan data frees */
1362 xfs_dir2_data_off_t oldbest; /* old value of best free */
1363 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001364 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001365 struct xfs_dir2_leaf_entry *ents;
1366 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001368 trace_xfs_dir2_leaf_removename(args);
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /*
1371 * Lookup the leaf entry, get the leaf and data blocks read in.
1372 */
1373 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1374 return error;
1375 }
1376 dp = args->dp;
1377 tp = args->trans;
1378 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001379 leaf = lbp->b_addr;
1380 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001381 xfs_dir3_data_check(dp, dbp);
Dave Chinner2ca98772013-10-29 22:11:49 +11001382 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001383 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001384 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /*
1386 * Point to the leaf entry, use that to point to the data entry.
1387 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001388 lep = &ents[index];
Dave Chinner30028032014-06-06 15:08:18 +10001389 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
1390 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
1391 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 needscan = needlog = 0;
Dave Chinner33363fe2013-04-03 16:11:22 +11001393 oldbest = be16_to_cpu(bf[0].length);
Dave Chinner8f661932014-06-06 15:15:59 +10001394 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001395 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott68b3a102006-03-17 17:27:19 +11001396 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 /*
1398 * Mark the former data entry unused.
1399 */
Dave Chinnerbc851782014-06-06 15:20:54 +10001400 xfs_dir2_data_make_free(args, dbp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001401 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Dave Chinner9d23fc82013-10-29 22:11:48 +11001402 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /*
1404 * We just mark the leaf entry stale by putting a null in it.
1405 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001406 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001407 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001408 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001409
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001410 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinnerbc851782014-06-06 15:20:54 +10001411 xfs_dir3_leaf_log_ents(args, lbp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 /*
1414 * Scan the freespace in the data block again if necessary,
1415 * log the data block header if necessary.
1416 */
1417 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001418 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001420 xfs_dir2_data_log_header(args, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 /*
1422 * If the longest freespace in the data block has changed,
1423 * put the new value in the bests table and log that.
1424 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001425 if (be16_to_cpu(bf[0].length) != oldbest) {
1426 bestsp[db] = bf[0].length;
Dave Chinnerbc851782014-06-06 15:20:54 +10001427 xfs_dir3_leaf_log_bests(args, lbp, db, db);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
Dave Chinner33363fe2013-04-03 16:11:22 +11001429 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /*
1431 * If the data block is now empty then get rid of the data block.
1432 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001433 if (be16_to_cpu(bf[0].length) ==
Dave Chinner8f661932014-06-06 15:15:59 +10001434 args->geo->blksize - dp->d_ops->data_entry_offset) {
Dave Chinner7dda6e82014-06-06 15:11:18 +10001435 ASSERT(db != args->geo->datablk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1437 /*
1438 * Nope, can't get rid of it because it caused
1439 * allocation of a bmap btree block to do so.
1440 * Just go on, returning success, leaving the
1441 * empty block in place.
1442 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001443 if (error == ENOSPC && args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 error = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001445 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return error;
1447 }
1448 dbp = NULL;
1449 /*
1450 * If this is the last data block then compact the
1451 * bests table by getting rid of entries.
1452 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001453 if (db == be32_to_cpu(ltp->bestcount) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 /*
1455 * Look for the last active entry (i).
1456 */
1457 for (i = db - 1; i > 0; i--) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001458 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 break;
1460 }
1461 /*
1462 * Copy the table down so inactive entries at the
1463 * end are removed.
1464 */
1465 memmove(&bestsp[db - i], bestsp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001466 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001467 be32_add_cpu(&ltp->bestcount, -(db - i));
Dave Chinnerbc851782014-06-06 15:20:54 +10001468 xfs_dir3_leaf_log_tail(args, lbp);
1469 xfs_dir3_leaf_log_bests(args, lbp, 0,
1470 be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 } else
Nathan Scott68b3a102006-03-17 17:27:19 +11001472 bestsp[db] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474 /*
1475 * If the data block was not the first one, drop it.
1476 */
Dave Chinner7dda6e82014-06-06 15:11:18 +10001477 else if (db != args->geo->datablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 dbp = NULL;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001479
Dave Chinner41419562013-10-29 22:11:50 +11001480 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /*
1482 * See if we can convert to block form.
1483 */
1484 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1485}
1486
1487/*
1488 * Replace the inode number in a leaf format directory entry.
1489 */
1490int /* error */
1491xfs_dir2_leaf_replace(
1492 xfs_da_args_t *args) /* operation arguments */
1493{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001494 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 xfs_dir2_data_entry_t *dep; /* data block entry */
1496 xfs_inode_t *dp; /* incore directory inode */
1497 int error; /* error return code */
1498 int index; /* index of leaf entry */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001499 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 xfs_dir2_leaf_t *leaf; /* leaf structure */
1501 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1502 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10001503 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001505 trace_xfs_dir2_leaf_replace(args);
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /*
1508 * Look up the entry.
1509 */
1510 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1511 return error;
1512 }
1513 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001514 leaf = lbp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11001515 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 /*
1517 * Point to the leaf entry, get data address from it.
1518 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001519 lep = &ents[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /*
1521 * Point to the data entry.
1522 */
1523 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001524 ((char *)dbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +10001525 xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001526 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 /*
1528 * Put the new inode number in, log it.
1529 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001530 dep->inumber = cpu_to_be64(args->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001531 dp->d_ops->data_put_ftype(dep, args->filetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 tp = args->trans;
Dave Chinnerbc851782014-06-06 15:20:54 +10001533 xfs_dir2_data_log_entry(args, dbp, dep);
Dave Chinner41419562013-10-29 22:11:50 +11001534 xfs_dir3_leaf_check(dp, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001535 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return 0;
1537}
1538
1539/*
1540 * Return index in the leaf block (lbp) which is either the first
1541 * one with this hash value, or if there are none, the insert point
1542 * for that hash value.
1543 */
1544int /* index value */
1545xfs_dir2_leaf_search_hash(
1546 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001547 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 xfs_dahash_t hash=0; /* hash from this entry */
1550 xfs_dahash_t hashwant; /* hash value looking for */
1551 int high; /* high leaf index */
1552 int low; /* low leaf index */
1553 xfs_dir2_leaf_t *leaf; /* leaf structure */
1554 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1555 int mid=0; /* current leaf index */
Dave Chinner24df33b2013-04-12 07:30:21 +10001556 struct xfs_dir2_leaf_entry *ents;
1557 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Dave Chinner1d9025e2012-06-22 18:50:14 +10001559 leaf = lbp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11001560 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001561 args->dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /*
1564 * Note, the table cannot be empty, so we have to go through the loop.
1565 * Binary search the leaf entries looking for our hash value.
1566 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001567 for (lep = ents, low = 0, high = leafhdr.count - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 hashwant = args->hashval;
1569 low <= high; ) {
1570 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001571 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 break;
1573 if (hash < hashwant)
1574 low = mid + 1;
1575 else
1576 high = mid - 1;
1577 }
1578 /*
1579 * Found one, back up through all the equal hash values.
1580 */
1581 if (hash == hashwant) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001582 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 mid--;
1584 }
1585 }
1586 /*
1587 * Need to point to an entry higher than ours.
1588 */
1589 else if (hash < hashwant)
1590 mid++;
1591 return mid;
1592}
1593
1594/*
1595 * Trim off a trailing data block. We know it's empty since the leaf
1596 * freespace table says so.
1597 */
1598int /* error */
1599xfs_dir2_leaf_trim_data(
1600 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001601 struct xfs_buf *lbp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 xfs_dir2_db_t db) /* data block number */
1603{
Nathan Scott68b3a102006-03-17 17:27:19 +11001604 __be16 *bestsp; /* leaf bests table */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001605 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 xfs_inode_t *dp; /* incore directory inode */
1607 int error; /* error return value */
1608 xfs_dir2_leaf_t *leaf; /* leaf structure */
1609 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1610 xfs_mount_t *mp; /* filesystem mount point */
1611 xfs_trans_t *tp; /* transaction pointer */
1612
1613 dp = args->dp;
1614 mp = dp->i_mount;
1615 tp = args->trans;
1616 /*
1617 * Read the offending data block. We need its buffer.
1618 */
Dave Chinner2998ab12014-06-06 15:07:53 +10001619 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(args->geo, db),
1620 -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001621 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Dave Chinner1d9025e2012-06-22 18:50:14 +10001624 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +10001625 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001626
1627#ifdef DEBUG
1628{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001629 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001630 struct xfs_dir2_data_free *bf = dp->d_ops->data_bestfree_p(hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001631
Dave Chinner33363fe2013-04-03 16:11:22 +11001632 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1633 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1634 ASSERT(be16_to_cpu(bf[0].length) ==
Dave Chinner8f661932014-06-06 15:15:59 +10001635 args->geo->blksize - dp->d_ops->data_entry_offset);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001636 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001637}
1638#endif
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 /*
1641 * Get rid of the data block.
1642 */
1643 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1644 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001645 xfs_trans_brelse(tp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return error;
1647 }
1648 /*
1649 * Eliminate the last bests entry from the table.
1650 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001651 bestsp = xfs_dir2_leaf_bests_p(ltp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001652 be32_add_cpu(&ltp->bestcount, -1);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001653 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
Dave Chinnerbc851782014-06-06 15:20:54 +10001654 xfs_dir3_leaf_log_tail(args, lbp);
1655 xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return 0;
1657}
1658
Christoph Hellwig22823962011-07-08 14:35:53 +02001659static inline size_t
Dave Chinner24df33b2013-04-12 07:30:21 +10001660xfs_dir3_leaf_size(
1661 struct xfs_dir3_icleaf_hdr *hdr,
Christoph Hellwig22823962011-07-08 14:35:53 +02001662 int counts)
1663{
Dave Chinner24df33b2013-04-12 07:30:21 +10001664 int entries;
1665 int hdrsize;
Christoph Hellwig22823962011-07-08 14:35:53 +02001666
Dave Chinner24df33b2013-04-12 07:30:21 +10001667 entries = hdr->count - hdr->stale;
1668 if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
1669 hdr->magic == XFS_DIR2_LEAFN_MAGIC)
1670 hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
1671 else
1672 hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
1673
1674 return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
1675 + counts * sizeof(xfs_dir2_data_off_t)
1676 + sizeof(xfs_dir2_leaf_tail_t);
Christoph Hellwig22823962011-07-08 14:35:53 +02001677}
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679/*
1680 * Convert node form directory to leaf form directory.
1681 * The root of the node form dir needs to already be a LEAFN block.
1682 * Just return if we can't do anything.
1683 */
1684int /* error */
1685xfs_dir2_node_to_leaf(
1686 xfs_da_state_t *state) /* directory operation state */
1687{
1688 xfs_da_args_t *args; /* operation arguments */
1689 xfs_inode_t *dp; /* incore directory inode */
1690 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001691 struct xfs_buf *fbp; /* buffer for freespace block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 xfs_fileoff_t fo; /* freespace file offset */
1693 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001694 struct xfs_buf *lbp; /* buffer for leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1696 xfs_dir2_leaf_t *leaf; /* leaf structure */
1697 xfs_mount_t *mp; /* filesystem mount point */
1698 int rval; /* successful free trim? */
1699 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10001700 struct xfs_dir3_icleaf_hdr leafhdr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001701 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 /*
1704 * There's more than a leaf level in the btree, so there must
1705 * be multiple leafn blocks. Give up.
1706 */
1707 if (state->path.active > 1)
1708 return 0;
1709 args = state->args;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001710
1711 trace_xfs_dir2_node_to_leaf(args);
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 mp = state->mp;
1714 dp = args->dp;
1715 tp = args->trans;
1716 /*
1717 * Get the last offset in the file.
1718 */
Eric Sandeen7fb2cd42014-04-14 18:58:05 +10001719 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return error;
1721 }
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001722 fo -= args->geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /*
1724 * If there are freespace blocks other than the first one,
1725 * take this opportunity to remove trailing empty freespace blocks
1726 * that may have been left behind during no-space-reservation
1727 * operations.
1728 */
Dave Chinner7dda6e82014-06-06 15:11:18 +10001729 while (fo > args->geo->freeblk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1731 return error;
1732 }
1733 if (rval)
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001734 fo -= args->geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 else
1736 return 0;
1737 }
1738 /*
1739 * Now find the block just before the freespace block.
1740 */
1741 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1742 return error;
1743 }
1744 /*
1745 * If it's not the single leaf block, give up.
1746 */
Dave Chinner8f661932014-06-06 15:15:59 +10001747 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return 0;
1749 lbp = state->path.blk[0].bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001750 leaf = lbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001751 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001752
1753 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
1754 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /*
1757 * Read the freespace block.
1758 */
Dave Chinner7dda6e82014-06-06 15:11:18 +10001759 error = xfs_dir2_free_read(tp, dp, args->geo->freeblk, &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001760 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001762 free = fbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001763 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001764
1765 ASSERT(!freehdr.firstdb);
Christoph Hellwig22823962011-07-08 14:35:53 +02001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 /*
1768 * Now see if the leafn and free data will fit in a leaf1.
1769 * If not, release the buffer and give up.
1770 */
Dave Chinner8f661932014-06-06 15:15:59 +10001771 if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001772 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return 0;
1774 }
Christoph Hellwig22823962011-07-08 14:35:53 +02001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 /*
1777 * If the leaf has any stale entries in it, compress them out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001779 if (leafhdr.stale)
1780 xfs_dir3_leaf_compact(args, &leafhdr, lbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001781
Dave Chinner24df33b2013-04-12 07:30:21 +10001782 lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +11001783 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
Dave Chinner24df33b2013-04-12 07:30:21 +10001784 leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
1785 ? XFS_DIR2_LEAF1_MAGIC
1786 : XFS_DIR3_LEAF1_MAGIC;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 /*
1789 * Set up the leaf tail from the freespace block.
1790 */
Dave Chinner8f661932014-06-06 15:15:59 +10001791 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001792 ltp->bestcount = cpu_to_be32(freehdr.nvalid);
Dave Chinner24df33b2013-04-12 07:30:21 +10001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 /*
1795 * Set up the leaf bests table.
1796 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001797 memcpy(xfs_dir2_leaf_bests_p(ltp), dp->d_ops->free_bests_p(free),
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001798 freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
Dave Chinner24df33b2013-04-12 07:30:21 +10001799
Dave Chinner01ba43b2013-10-29 22:11:52 +11001800 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001801 xfs_dir3_leaf_log_header(args, lbp);
1802 xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1803 xfs_dir3_leaf_log_tail(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +11001804 xfs_dir3_leaf_check(dp, lbp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 /*
1807 * Get rid of the freespace block.
1808 */
Dave Chinner8c44a282014-06-06 15:04:41 +10001809 error = xfs_dir2_shrink_inode(args,
Dave Chinner30028032014-06-06 15:08:18 +10001810 xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET),
1811 fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (error) {
1813 /*
1814 * This can't fail here because it can only happen when
1815 * punching out the middle of an extent, and this is an
1816 * isolated block.
1817 */
1818 ASSERT(error != ENOSPC);
1819 return error;
1820 }
1821 fbp = NULL;
1822 /*
1823 * Now see if we can convert the single-leaf directory
1824 * down to a block form directory.
1825 * This routine always kills the dabuf for the leaf, so
1826 * eliminate it from the path.
1827 */
1828 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1829 state->path.blk[0].bp = NULL;
1830 return error;
1831}