blob: bb22aac9fc01afa7384ac4972ac868d5d51f2d09 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dinode.h"
31#include "xfs_inode.h"
32#include "xfs_bmap.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020033#include "xfs_dir2_format.h"
34#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000036#include "xfs_trace.h"
Dave Chinner24df33b2013-04-12 07:30:21 +100037#include "xfs_buf_item.h"
38#include "xfs_cksum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * Local function declarations.
42 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100043static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
44 int *indexp, struct xfs_buf **dbpp);
Dave Chinner24df33b2013-04-12 07:30:21 +100045static void xfs_dir3_leaf_log_bests(struct xfs_trans *tp, struct xfs_buf *bp,
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100046 int first, int last);
Dave Chinner24df33b2013-04-12 07:30:21 +100047static void xfs_dir3_leaf_log_tail(struct xfs_trans *tp, 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
54#define xfs_dir3_leaf_check(mp, bp) \
55do { \
56 if (!xfs_dir3_leaf1_check((mp), (bp))) \
57 ASSERT(0); \
58} while (0);
59
60STATIC bool
61xfs_dir3_leaf1_check(
62 struct xfs_mount *mp,
63 struct xfs_buf *bp)
64{
65 struct xfs_dir2_leaf *leaf = bp->b_addr;
66 struct xfs_dir3_icleaf_hdr leafhdr;
67
68 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
69
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
77 return xfs_dir3_leaf_check_int(mp, &leafhdr, leaf);
78}
79#else
80#define xfs_dir3_leaf_check(mp, bp)
81#endif
82
83void
84xfs_dir3_leaf_hdr_from_disk(
85 struct xfs_dir3_icleaf_hdr *to,
86 struct xfs_dir2_leaf *from)
87{
88 if (from->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
89 from->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)) {
90 to->forw = be32_to_cpu(from->hdr.info.forw);
91 to->back = be32_to_cpu(from->hdr.info.back);
92 to->magic = be16_to_cpu(from->hdr.info.magic);
93 to->count = be16_to_cpu(from->hdr.count);
94 to->stale = be16_to_cpu(from->hdr.stale);
95 } else {
96 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)from;
97
98 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
99 to->back = be32_to_cpu(hdr3->info.hdr.back);
100 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
101 to->count = be16_to_cpu(hdr3->count);
102 to->stale = be16_to_cpu(hdr3->stale);
103 }
104
105 ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
106 to->magic == XFS_DIR3_LEAF1_MAGIC ||
107 to->magic == XFS_DIR2_LEAFN_MAGIC ||
108 to->magic == XFS_DIR3_LEAFN_MAGIC);
109}
110
111void
112xfs_dir3_leaf_hdr_to_disk(
113 struct xfs_dir2_leaf *to,
114 struct xfs_dir3_icleaf_hdr *from)
115{
116 ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
117 from->magic == XFS_DIR3_LEAF1_MAGIC ||
118 from->magic == XFS_DIR2_LEAFN_MAGIC ||
119 from->magic == XFS_DIR3_LEAFN_MAGIC);
120
121 if (from->magic == XFS_DIR2_LEAF1_MAGIC ||
122 from->magic == XFS_DIR2_LEAFN_MAGIC) {
123 to->hdr.info.forw = cpu_to_be32(from->forw);
124 to->hdr.info.back = cpu_to_be32(from->back);
125 to->hdr.info.magic = cpu_to_be16(from->magic);
126 to->hdr.count = cpu_to_be16(from->count);
127 to->hdr.stale = cpu_to_be16(from->stale);
128 } else {
129 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)to;
130
131 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
132 hdr3->info.hdr.back = cpu_to_be32(from->back);
133 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
134 hdr3->count = cpu_to_be16(from->count);
135 hdr3->stale = cpu_to_be16(from->stale);
136 }
137}
138
139bool
140xfs_dir3_leaf_check_int(
141 struct xfs_mount *mp,
142 struct xfs_dir3_icleaf_hdr *hdr,
143 struct xfs_dir2_leaf *leaf)
144{
145 struct xfs_dir2_leaf_entry *ents;
146 xfs_dir2_leaf_tail_t *ltp;
147 int stale;
148 int i;
149
150 ents = xfs_dir3_leaf_ents_p(leaf);
151 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
152
153 /*
154 * XXX (dgc): This value is not restrictive enough.
155 * Should factor in the size of the bests table as well.
156 * We can deduce a value for that from di_size.
157 */
158 if (hdr->count > xfs_dir3_max_leaf_ents(mp, leaf))
159 return false;
160
161 /* Leaves and bests don't overlap in leaf format. */
162 if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
163 hdr->magic == XFS_DIR3_LEAF1_MAGIC) &&
164 (char *)&ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
165 return false;
166
167 /* Check hash value order, count stale entries. */
168 for (i = stale = 0; i < hdr->count; i++) {
169 if (i + 1 < hdr->count) {
170 if (be32_to_cpu(ents[i].hashval) >
171 be32_to_cpu(ents[i + 1].hashval))
172 return false;
173 }
174 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
175 stale++;
176 }
177 if (hdr->stale != stale)
178 return false;
179 return true;
180}
181
182static bool
183xfs_dir3_leaf_verify(
Dave Chinnere6f76672012-11-12 22:54:15 +1100184 struct xfs_buf *bp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000185 __uint16_t magic)
Dave Chinnere6f76672012-11-12 22:54:15 +1100186{
187 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner24df33b2013-04-12 07:30:21 +1000188 struct xfs_dir2_leaf *leaf = bp->b_addr;
189 struct xfs_dir3_icleaf_hdr leafhdr;
Dave Chinnere6f76672012-11-12 22:54:15 +1100190
Dave Chinner24df33b2013-04-12 07:30:21 +1000191 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
192
193 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
194 if (xfs_sb_version_hascrc(&mp->m_sb)) {
195 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
196
197 if ((magic == XFS_DIR2_LEAF1_MAGIC &&
198 leafhdr.magic != XFS_DIR3_LEAF1_MAGIC) ||
199 (magic == XFS_DIR2_LEAFN_MAGIC &&
200 leafhdr.magic != XFS_DIR3_LEAFN_MAGIC))
201 return false;
202
203 if (!uuid_equal(&leaf3->info.uuid, &mp->m_sb.sb_uuid))
204 return false;
205 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
206 return false;
207 } else {
208 if (leafhdr.magic != magic)
209 return false;
210 }
211 return xfs_dir3_leaf_check_int(mp, &leafhdr, leaf);
212}
213
214static void
215__read_verify(
216 struct xfs_buf *bp,
217 __uint16_t magic)
218{
219 struct xfs_mount *mp = bp->b_target->bt_mount;
220
221 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
222 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
223 XFS_DIR3_LEAF_CRC_OFF)) ||
224 !xfs_dir3_leaf_verify(bp, magic)) {
225 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
Dave Chinnere6f76672012-11-12 22:54:15 +1100226 xfs_buf_ioerror(bp, EFSCORRUPTED);
227 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100228}
Dave Chinnere6f76672012-11-12 22:54:15 +1100229
Dave Chinner612cfbf2012-11-14 17:52:32 +1100230static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000231__write_verify(
232 struct xfs_buf *bp,
233 __uint16_t magic)
Dave Chinner612cfbf2012-11-14 17:52:32 +1100234{
Dave Chinner24df33b2013-04-12 07:30:21 +1000235 struct xfs_mount *mp = bp->b_target->bt_mount;
236 struct xfs_buf_log_item *bip = bp->b_fspriv;
237 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
238
239 if (!xfs_dir3_leaf_verify(bp, magic)) {
240 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
241 xfs_buf_ioerror(bp, EFSCORRUPTED);
242 return;
243 }
244
245 if (!xfs_sb_version_hascrc(&mp->m_sb))
246 return;
247
248 if (bip)
249 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
250
251 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_LEAF_CRC_OFF);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100252}
253
254static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000255xfs_dir3_leaf1_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100256 struct xfs_buf *bp)
257{
Dave Chinner24df33b2013-04-12 07:30:21 +1000258 __read_verify(bp, XFS_DIR2_LEAF1_MAGIC);
Dave Chinner1813dd62012-11-14 17:54:40 +1100259}
260
Dave Chinner24df33b2013-04-12 07:30:21 +1000261static void
262xfs_dir3_leaf1_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100263 struct xfs_buf *bp)
264{
Dave Chinner24df33b2013-04-12 07:30:21 +1000265 __write_verify(bp, XFS_DIR2_LEAF1_MAGIC);
Dave Chinnere6f76672012-11-12 22:54:15 +1100266}
267
Dave Chinner24df33b2013-04-12 07:30:21 +1000268static void
269xfs_dir3_leafn_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100270 struct xfs_buf *bp)
Dave Chinnere6f76672012-11-12 22:54:15 +1100271{
Dave Chinner24df33b2013-04-12 07:30:21 +1000272 __read_verify(bp, XFS_DIR2_LEAFN_MAGIC);
Dave Chinnere6f76672012-11-12 22:54:15 +1100273}
274
Dave Chinner24df33b2013-04-12 07:30:21 +1000275static void
276xfs_dir3_leafn_write_verify(
277 struct xfs_buf *bp)
278{
279 __write_verify(bp, XFS_DIR2_LEAFN_MAGIC);
280}
281
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100282const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
Dave Chinner24df33b2013-04-12 07:30:21 +1000283 .verify_read = xfs_dir3_leaf1_read_verify,
284 .verify_write = xfs_dir3_leaf1_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100285};
286
Dave Chinner24df33b2013-04-12 07:30:21 +1000287const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
288 .verify_read = xfs_dir3_leafn_read_verify,
289 .verify_write = xfs_dir3_leafn_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100290};
Dave Chinnere6f76672012-11-12 22:54:15 +1100291
292static int
Dave Chinner24df33b2013-04-12 07:30:21 +1000293xfs_dir3_leaf_read(
Dave Chinnere6f76672012-11-12 22:54:15 +1100294 struct xfs_trans *tp,
295 struct xfs_inode *dp,
296 xfs_dablk_t fbno,
297 xfs_daddr_t mappedbno,
298 struct xfs_buf **bpp)
299{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100300 int err;
301
302 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000303 XFS_DATA_FORK, &xfs_dir3_leaf1_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100304 if (!err && tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100305 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100306 return err;
Dave Chinnere6f76672012-11-12 22:54:15 +1100307}
308
309int
Dave Chinner24df33b2013-04-12 07:30:21 +1000310xfs_dir3_leafn_read(
Dave Chinnere6f76672012-11-12 22:54:15 +1100311 struct xfs_trans *tp,
312 struct xfs_inode *dp,
313 xfs_dablk_t fbno,
314 xfs_daddr_t mappedbno,
315 struct xfs_buf **bpp)
316{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100317 int err;
318
319 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000320 XFS_DATA_FORK, &xfs_dir3_leafn_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100321 if (!err && tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100322 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100323 return err;
Dave Chinner24df33b2013-04-12 07:30:21 +1000324}
325
326/*
327 * Initialize a new leaf block, leaf1 or leafn magic accepted.
328 */
329static void
330xfs_dir3_leaf_init(
331 struct xfs_mount *mp,
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100332 struct xfs_trans *tp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000333 struct xfs_buf *bp,
334 xfs_ino_t owner,
335 __uint16_t type)
336{
337 struct xfs_dir2_leaf *leaf = bp->b_addr;
338
339 ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
340
341 if (xfs_sb_version_hascrc(&mp->m_sb)) {
342 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
343
344 memset(leaf3, 0, sizeof(*leaf3));
345
346 leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
347 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
348 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
349 leaf3->info.blkno = cpu_to_be64(bp->b_bn);
350 leaf3->info.owner = cpu_to_be64(owner);
351 uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_uuid);
352 } else {
353 memset(leaf, 0, sizeof(*leaf));
354 leaf->hdr.info.magic = cpu_to_be16(type);
355 }
356
357 /*
358 * If it's a leaf-format directory initialize the tail.
359 * Caller is responsible for initialising the bests table.
360 */
361 if (type == XFS_DIR2_LEAF1_MAGIC) {
362 struct xfs_dir2_leaf_tail *ltp;
363
364 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
365 ltp->bestcount = 0;
366 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100367 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100368 } else {
Dave Chinner24df33b2013-04-12 07:30:21 +1000369 bp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100370 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100371 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000372}
373
374int
375xfs_dir3_leaf_get_buf(
376 xfs_da_args_t *args,
377 xfs_dir2_db_t bno,
378 struct xfs_buf **bpp,
379 __uint16_t magic)
380{
381 struct xfs_inode *dp = args->dp;
382 struct xfs_trans *tp = args->trans;
383 struct xfs_mount *mp = dp->i_mount;
384 struct xfs_buf *bp;
385 int error;
386
387 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
388 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
389 bno < XFS_DIR2_FREE_FIRSTDB(mp));
390
391 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
392 XFS_DATA_FORK);
393 if (error)
394 return error;
395
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100396 xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
Dave Chinner24df33b2013-04-12 07:30:21 +1000397 xfs_dir3_leaf_log_header(tp, bp);
398 if (magic == XFS_DIR2_LEAF1_MAGIC)
399 xfs_dir3_leaf_log_tail(tp, bp);
400 *bpp = bp;
401 return 0;
Dave Chinnere6f76672012-11-12 22:54:15 +1100402}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404/*
405 * Convert a block form directory to a leaf form directory.
406 */
407int /* error */
408xfs_dir2_block_to_leaf(
409 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000410 struct xfs_buf *dbp) /* input block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Nathan Scott68b3a102006-03-17 17:27:19 +1100412 __be16 *bestsp; /* leaf's bestsp entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 xfs_dablk_t blkno; /* leaf block's bno */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200414 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
416 xfs_dir2_block_tail_t *btp; /* block's tail */
417 xfs_inode_t *dp; /* incore directory inode */
418 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000419 struct xfs_buf *lbp; /* leaf block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 xfs_dir2_db_t ldb; /* leaf block's bno */
421 xfs_dir2_leaf_t *leaf; /* leaf structure */
422 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
423 xfs_mount_t *mp; /* filesystem mount point */
424 int needlog; /* need to log block header */
425 int needscan; /* need to rescan bestfree */
426 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +1100427 struct xfs_dir2_data_free *bf;
Dave Chinner24df33b2013-04-12 07:30:21 +1000428 struct xfs_dir2_leaf_entry *ents;
429 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000431 trace_xfs_dir2_block_to_leaf(args);
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 dp = args->dp;
434 mp = dp->i_mount;
435 tp = args->trans;
436 /*
437 * Add the leaf block to the inode.
438 * This interface will only put blocks in the leaf/node range.
439 * Since that's empty now, we'll get the root (block 0 in range).
440 */
441 if ((error = xfs_da_grow_inode(args, &blkno))) {
442 return error;
443 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000444 ldb = xfs_dir2_da_to_db(mp, blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
446 /*
447 * Initialize the leaf block, get a buffer for it.
448 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000449 error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
450 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +1000452
Dave Chinner1d9025e2012-06-22 18:50:14 +1000453 leaf = lbp->b_addr;
454 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100455 xfs_dir3_data_check(dp, dbp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200456 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000457 blp = xfs_dir2_block_leaf_p(btp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100458 bf = xfs_dir3_data_bestfree_p(hdr);
Dave Chinner24df33b2013-04-12 07:30:21 +1000459 ents = xfs_dir3_leaf_ents_p(leaf);
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /*
462 * Set the counts in the leaf header.
463 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000464 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
465 leafhdr.count = be32_to_cpu(btp->count);
466 leafhdr.stale = be32_to_cpu(btp->stale);
467 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
468 xfs_dir3_leaf_log_header(tp, lbp);
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /*
471 * Could compact these but I think we always do the conversion
472 * after squeezing out stale entries.
473 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000474 memcpy(ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
475 xfs_dir3_leaf_log_ents(tp, lbp, 0, leafhdr.count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 needscan = 0;
477 needlog = 1;
478 /*
479 * Make the space formerly occupied by the leaf entries and block
480 * tail be free.
481 */
482 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200483 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
484 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 (char *)blp),
486 &needlog, &needscan);
487 /*
488 * Fix up the block header, make it a data block.
489 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100490 dbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100491 xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
Dave Chinner33363fe2013-04-03 16:11:22 +1100492 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
493 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
494 else
495 hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200498 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /*
500 * Set up leaf tail and bests table.
501 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000502 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100503 ltp->bestcount = cpu_to_be32(1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000504 bestsp = xfs_dir2_leaf_bests_p(ltp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100505 bestsp[0] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /*
507 * Log the data header and leaf bests table.
508 */
509 if (needlog)
510 xfs_dir2_data_log_header(tp, dbp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000511 xfs_dir3_leaf_check(mp, lbp);
Dave Chinner33363fe2013-04-03 16:11:22 +1100512 xfs_dir3_data_check(dp, dbp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000513 xfs_dir3_leaf_log_bests(tp, lbp, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return 0;
515}
516
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200517STATIC void
Dave Chinner24df33b2013-04-12 07:30:21 +1000518xfs_dir3_leaf_find_stale(
519 struct xfs_dir3_icleaf_hdr *leafhdr,
520 struct xfs_dir2_leaf_entry *ents,
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200521 int index,
522 int *lowstale,
523 int *highstale)
524{
525 /*
526 * Find the first stale entry before our index, if any.
527 */
528 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000529 if (ents[*lowstale].address ==
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200530 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
531 break;
532 }
533
534 /*
535 * Find the first stale entry at or after our index, if any.
536 * Stop if the result would require moving more entries than using
537 * lowstale.
538 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000539 for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
540 if (ents[*highstale].address ==
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200541 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
542 break;
543 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
544 break;
545 }
546}
547
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200548struct xfs_dir2_leaf_entry *
Dave Chinner24df33b2013-04-12 07:30:21 +1000549xfs_dir3_leaf_find_entry(
550 struct xfs_dir3_icleaf_hdr *leafhdr,
551 struct xfs_dir2_leaf_entry *ents,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200552 int index, /* leaf table position */
553 int compact, /* need to compact leaves */
554 int lowstale, /* index of prev stale leaf */
555 int highstale, /* index of next stale leaf */
556 int *lfloglow, /* low leaf logging index */
557 int *lfloghigh) /* high leaf logging index */
558{
Dave Chinner24df33b2013-04-12 07:30:21 +1000559 if (!leafhdr->stale) {
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200560 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
561
562 /*
563 * Now we need to make room to insert the leaf entry.
564 *
565 * If there are no stale entries, just insert a hole at index.
566 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000567 lep = &ents[index];
568 if (index < leafhdr->count)
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200569 memmove(lep + 1, lep,
Dave Chinner24df33b2013-04-12 07:30:21 +1000570 (leafhdr->count - index) * sizeof(*lep));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200571
572 /*
573 * Record low and high logging indices for the leaf.
574 */
575 *lfloglow = index;
Dave Chinner24df33b2013-04-12 07:30:21 +1000576 *lfloghigh = leafhdr->count++;
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200577 return lep;
578 }
579
580 /*
581 * There are stale entries.
582 *
583 * We will use one of them for the new entry. It's probably not at
584 * the right location, so we'll have to shift some up or down first.
585 *
586 * If we didn't compact before, we need to find the nearest stale
587 * entries before and after our insertion point.
588 */
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200589 if (compact == 0)
Dave Chinner24df33b2013-04-12 07:30:21 +1000590 xfs_dir3_leaf_find_stale(leafhdr, ents, index,
591 &lowstale, &highstale);
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200592
593 /*
594 * If the low one is better, use it.
595 */
596 if (lowstale >= 0 &&
Dave Chinner24df33b2013-04-12 07:30:21 +1000597 (highstale == leafhdr->count ||
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200598 index - lowstale - 1 < highstale - index)) {
599 ASSERT(index - lowstale - 1 >= 0);
Dave Chinner24df33b2013-04-12 07:30:21 +1000600 ASSERT(ents[lowstale].address ==
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200601 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200602
603 /*
604 * Copy entries up to cover the stale entry and make room
605 * for the new entry.
606 */
607 if (index - lowstale - 1 > 0) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000608 memmove(&ents[lowstale], &ents[lowstale + 1],
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200609 (index - lowstale - 1) *
Dave Chinner24df33b2013-04-12 07:30:21 +1000610 sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200611 }
612 *lfloglow = MIN(lowstale, *lfloglow);
613 *lfloghigh = MAX(index - 1, *lfloghigh);
Dave Chinner24df33b2013-04-12 07:30:21 +1000614 leafhdr->stale--;
615 return &ents[index - 1];
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200616 }
617
618 /*
619 * The high one is better, so use that one.
620 */
621 ASSERT(highstale - index >= 0);
Dave Chinner24df33b2013-04-12 07:30:21 +1000622 ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200623
624 /*
625 * Copy entries down to cover the stale entry and make room for the
626 * new entry.
627 */
628 if (highstale - index > 0) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000629 memmove(&ents[index + 1], &ents[index],
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200630 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
631 }
632 *lfloglow = MIN(index, *lfloglow);
633 *lfloghigh = MAX(highstale, *lfloghigh);
Dave Chinner24df33b2013-04-12 07:30:21 +1000634 leafhdr->stale--;
635 return &ents[index];
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/*
639 * Add an entry to a leaf form directory.
640 */
641int /* error */
642xfs_dir2_leaf_addname(
643 xfs_da_args_t *args) /* operation arguments */
644{
Nathan Scott68b3a102006-03-17 17:27:19 +1100645 __be16 *bestsp; /* freespace table in leaf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int compact; /* need to compact leaves */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200647 xfs_dir2_data_hdr_t *hdr; /* data block header */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000648 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 xfs_dir2_data_entry_t *dep; /* data block entry */
650 xfs_inode_t *dp; /* incore directory inode */
651 xfs_dir2_data_unused_t *dup; /* data unused entry */
652 int error; /* error return value */
653 int grown; /* allocated new data block */
654 int highstale; /* index of next stale leaf */
655 int i; /* temporary, index */
656 int index; /* leaf table position */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000657 struct xfs_buf *lbp; /* leaf's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 xfs_dir2_leaf_t *leaf; /* leaf structure */
659 int length; /* length of new entry */
660 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
661 int lfloglow; /* low leaf logging index */
662 int lfloghigh; /* high leaf logging index */
663 int lowstale; /* index of prev stale leaf */
664 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
665 xfs_mount_t *mp; /* filesystem mount point */
666 int needbytes; /* leaf block bytes needed */
667 int needlog; /* need to log data header */
668 int needscan; /* need to rescan data free */
Nathan Scott3d693c62006-03-17 17:28:27 +1100669 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 xfs_trans_t *tp; /* transaction pointer */
671 xfs_dir2_db_t use_block; /* data block number */
Dave Chinner33363fe2013-04-03 16:11:22 +1100672 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +1000673 struct xfs_dir2_leaf_entry *ents;
674 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000676 trace_xfs_dir2_leaf_addname(args);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 dp = args->dp;
679 tp = args->trans;
680 mp = dp->i_mount;
Dave Chinnere6f76672012-11-12 22:54:15 +1100681
Dave Chinner24df33b2013-04-12 07:30:21 +1000682 error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100683 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +1100685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /*
687 * Look up the entry by hash value and name.
688 * We know it's not there, our caller has already done a lookup.
689 * So the index is of the entry to insert in front of.
690 * But if there are dup hash values the index is of the first of those.
691 */
692 index = xfs_dir2_leaf_search_hash(args, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000693 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000694 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000695 ents = xfs_dir3_leaf_ents_p(leaf);
696 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000697 bestsp = xfs_dir2_leaf_bests_p(ltp);
698 length = xfs_dir2_data_entsize(args->namelen);
Dave Chinner24df33b2013-04-12 07:30:21 +1000699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /*
701 * See if there are any entries with the same hash value
702 * and space in their block for the new entry.
703 * This is good because it puts multiple same-hash value entries
704 * in a data block, improving the lookup of those entries.
705 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000706 for (use_block = -1, lep = &ents[index];
707 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 index++, lep++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100709 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 continue;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000711 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100712 ASSERT(i < be32_to_cpu(ltp->bestcount));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200713 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
Nathan Scott68b3a102006-03-17 17:27:19 +1100714 if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 use_block = i;
716 break;
717 }
718 }
719 /*
720 * Didn't find a block yet, linear search all the data blocks.
721 */
722 if (use_block == -1) {
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100723 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /*
725 * Remember a block we see that's missing.
726 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200727 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
728 use_block == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 use_block = i;
Nathan Scott68b3a102006-03-17 17:27:19 +1100730 else if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 use_block = i;
732 break;
733 }
734 }
735 }
736 /*
737 * How many bytes do we need in the leaf block?
738 */
Christoph Hellwig22823962011-07-08 14:35:53 +0200739 needbytes = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000740 if (!leafhdr.stale)
Christoph Hellwig22823962011-07-08 14:35:53 +0200741 needbytes += sizeof(xfs_dir2_leaf_entry_t);
742 if (use_block == -1)
743 needbytes += sizeof(xfs_dir2_data_off_t);
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /*
746 * Now kill use_block if it refers to a missing block, so we
747 * can use it as an indication of allocation needed.
748 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200749 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 use_block = -1;
751 /*
752 * If we don't have enough free bytes but we can make enough
753 * by compacting out stale entries, we'll do that.
754 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000755 if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
756 leafhdr.stale > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 compact = 1;
Dave Chinner24df33b2013-04-12 07:30:21 +1000758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /*
760 * Otherwise if we don't have enough free bytes we need to
761 * convert to node form.
762 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000763 else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /*
765 * Just checking or no space reservation, give up.
766 */
Barry Naujok6a178102008-05-21 16:42:05 +1000767 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
768 args->total == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000769 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return XFS_ERROR(ENOSPC);
771 }
772 /*
773 * Convert to node form.
774 */
775 error = xfs_dir2_leaf_to_node(args, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (error)
777 return error;
778 /*
779 * Then add the new entry.
780 */
781 return xfs_dir2_node_addname(args);
782 }
783 /*
784 * Otherwise it will fit without compaction.
785 */
786 else
787 compact = 0;
788 /*
789 * If just checking, then it will fit unless we needed to allocate
790 * a new data block.
791 */
Barry Naujok6a178102008-05-21 16:42:05 +1000792 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000793 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
795 }
796 /*
797 * If no allocations are allowed, return now before we've
798 * changed anything.
799 */
800 if (args->total == 0 && use_block == -1) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000801 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return XFS_ERROR(ENOSPC);
803 }
804 /*
805 * Need to compact the leaf entries, removing stale ones.
806 * Leave one stale entry behind - the one closest to our
807 * insertion index - and we'll shift that one to our insertion
808 * point later.
809 */
810 if (compact) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000811 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
812 &highstale, &lfloglow, &lfloghigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814 /*
815 * There are stale entries, so we'll need log-low and log-high
816 * impossibly bad values later.
817 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000818 else if (leafhdr.stale) {
819 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 lfloghigh = -1;
821 }
822 /*
823 * If there was no data block space found, we need to allocate
824 * a new one.
825 */
826 if (use_block == -1) {
827 /*
828 * Add the new data block.
829 */
830 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
831 &use_block))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000832 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return error;
834 }
835 /*
836 * Initialize the block.
837 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100838 if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000839 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return error;
841 }
842 /*
843 * If we're adding a new data block on the end we need to
844 * extend the bests table. Copy it up one entry.
845 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100846 if (use_block >= be32_to_cpu(ltp->bestcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 bestsp--;
848 memmove(&bestsp[0], &bestsp[1],
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100849 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800850 be32_add_cpu(&ltp->bestcount, 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000851 xfs_dir3_leaf_log_tail(tp, lbp);
852 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854 /*
855 * If we're filling in a previously empty block just log it.
856 */
857 else
Dave Chinner24df33b2013-04-12 07:30:21 +1000858 xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000859 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100860 bf = xfs_dir3_data_bestfree_p(hdr);
861 bestsp[use_block] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 grown = 1;
Dave Chinnere4813572012-11-12 22:54:14 +1100863 } else {
864 /*
865 * Already had space in some data block.
866 * Just read that one in.
867 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100868 error = xfs_dir3_data_read(tp, dp,
Dave Chinnere4813572012-11-12 22:54:14 +1100869 xfs_dir2_db_to_da(mp, use_block),
870 -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100871 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000872 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return error;
874 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000875 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100876 bf = xfs_dir3_data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 grown = 0;
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /*
880 * Point to the biggest freespace in our data block.
881 */
882 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +1100883 ((char *)hdr + be16_to_cpu(bf[0].offset));
Nathan Scottad354eb2006-03-17 17:27:37 +1100884 ASSERT(be16_to_cpu(dup->length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 needscan = needlog = 0;
886 /*
887 * Mark the initial part of our freespace in use for the new entry.
888 */
889 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200890 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 &needlog, &needscan);
892 /*
893 * Initialize our new entry (at last).
894 */
895 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000896 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 dep->namelen = args->namelen;
898 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000899 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200900 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /*
902 * Need to scan fix up the bestfree table.
903 */
904 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200905 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /*
907 * Need to log the data block's header.
908 */
909 if (needlog)
910 xfs_dir2_data_log_header(tp, dbp);
911 xfs_dir2_data_log_entry(tp, dbp, dep);
912 /*
913 * If the bests table needs to be changed, do it.
914 * Log the change unless we've already done that.
915 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100916 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
917 bestsp[use_block] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (!grown)
Dave Chinner24df33b2013-04-12 07:30:21 +1000919 xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200921
Dave Chinner24df33b2013-04-12 07:30:21 +1000922 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200923 highstale, &lfloglow, &lfloghigh);
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /*
926 * Fill in the new leaf entry.
927 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100928 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000929 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
Nathan Scott3d693c62006-03-17 17:28:27 +1100930 be16_to_cpu(*tagp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /*
932 * Log the leaf fields and give up the buffers.
933 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000934 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
935 xfs_dir3_leaf_log_header(tp, lbp);
936 xfs_dir3_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
937 xfs_dir3_leaf_check(mp, lbp);
Dave Chinner33363fe2013-04-03 16:11:22 +1100938 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return 0;
940}
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942/*
943 * Compact out any stale entries in the leaf.
944 * Log the header and changed leaf entries, if any.
945 */
946void
Dave Chinner24df33b2013-04-12 07:30:21 +1000947xfs_dir3_leaf_compact(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 xfs_da_args_t *args, /* operation arguments */
Dave Chinner24df33b2013-04-12 07:30:21 +1000949 struct xfs_dir3_icleaf_hdr *leafhdr,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000950 struct xfs_buf *bp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 int from; /* source leaf index */
953 xfs_dir2_leaf_t *leaf; /* leaf structure */
954 int loglow; /* first leaf entry to log */
955 int to; /* target leaf index */
Dave Chinner24df33b2013-04-12 07:30:21 +1000956 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Dave Chinner1d9025e2012-06-22 18:50:14 +1000958 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +1000959 if (!leafhdr->stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /*
963 * Compress out the stale entries in place.
964 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000965 ents = xfs_dir3_leaf_ents_p(leaf);
966 for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
967 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 continue;
969 /*
970 * Only actually copy the entries that are different.
971 */
972 if (from > to) {
973 if (loglow == -1)
974 loglow = to;
Dave Chinner24df33b2013-04-12 07:30:21 +1000975 ents[to] = ents[from];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977 to++;
978 }
979 /*
980 * Update and log the header, log the leaf entries.
981 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000982 ASSERT(leafhdr->stale == from - to);
983 leafhdr->count -= leafhdr->stale;
984 leafhdr->stale = 0;
985
986 xfs_dir3_leaf_hdr_to_disk(leaf, leafhdr);
987 xfs_dir3_leaf_log_header(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (loglow != -1)
Dave Chinner24df33b2013-04-12 07:30:21 +1000989 xfs_dir3_leaf_log_ents(args->trans, bp, loglow, to - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992/*
993 * Compact the leaf entries, removing stale ones.
994 * Leave one stale entry behind - the one closest to our
995 * insertion index - and the caller will shift that one to our insertion
996 * point later.
997 * Return new insertion index, where the remaining stale entry is,
998 * and leaf logging indices.
999 */
1000void
Dave Chinner24df33b2013-04-12 07:30:21 +10001001xfs_dir3_leaf_compact_x1(
1002 struct xfs_dir3_icleaf_hdr *leafhdr,
1003 struct xfs_dir2_leaf_entry *ents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 int *indexp, /* insertion index */
1005 int *lowstalep, /* out: stale entry before us */
1006 int *highstalep, /* out: stale entry after us */
1007 int *lowlogp, /* out: low log index */
1008 int *highlogp) /* out: high log index */
1009{
1010 int from; /* source copy index */
1011 int highstale; /* stale entry at/after index */
1012 int index; /* insertion index */
1013 int keepstale; /* source index of kept stale */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int lowstale; /* stale entry before index */
1015 int newindex=0; /* new insertion index */
1016 int to; /* destination copy index */
1017
Dave Chinner24df33b2013-04-12 07:30:21 +10001018 ASSERT(leafhdr->stale > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 index = *indexp;
Christoph Hellwiga230a1d2011-07-13 13:43:48 +02001020
Dave Chinner24df33b2013-04-12 07:30:21 +10001021 xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
Christoph Hellwiga230a1d2011-07-13 13:43:48 +02001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /*
1024 * Pick the better of lowstale and highstale.
1025 */
1026 if (lowstale >= 0 &&
Dave Chinner24df33b2013-04-12 07:30:21 +10001027 (highstale == leafhdr->count ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 index - lowstale <= highstale - index))
1029 keepstale = lowstale;
1030 else
1031 keepstale = highstale;
1032 /*
1033 * Copy the entries in place, removing all the stale entries
1034 * except keepstale.
1035 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001036 for (from = to = 0; from < leafhdr->count; from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /*
1038 * Notice the new value of index.
1039 */
1040 if (index == from)
1041 newindex = to;
1042 if (from != keepstale &&
Dave Chinner24df33b2013-04-12 07:30:21 +10001043 ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (from == to)
1045 *lowlogp = to;
1046 continue;
1047 }
1048 /*
1049 * Record the new keepstale value for the insertion.
1050 */
1051 if (from == keepstale)
1052 lowstale = highstale = to;
1053 /*
1054 * Copy only the entries that have moved.
1055 */
1056 if (from > to)
Dave Chinner24df33b2013-04-12 07:30:21 +10001057 ents[to] = ents[from];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 to++;
1059 }
1060 ASSERT(from > to);
1061 /*
1062 * If the insertion point was past the last entry,
1063 * set the new insertion point accordingly.
1064 */
1065 if (index == from)
1066 newindex = to;
1067 *indexp = newindex;
1068 /*
1069 * Adjust the leaf header values.
1070 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001071 leafhdr->count -= from - to;
1072 leafhdr->stale = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /*
1074 * Remember the low/high stale value only in the "right"
1075 * direction.
1076 */
1077 if (lowstale >= newindex)
1078 lowstale = -1;
1079 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001080 highstale = leafhdr->count;
1081 *highlogp = leafhdr->count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 *lowstalep = lowstale;
1083 *highstalep = highstale;
1084}
1085
Dave Chinner9b73bd72012-06-22 18:50:15 +10001086struct xfs_dir2_leaf_map_info {
1087 xfs_extlen_t map_blocks; /* number of fsbs in map */
1088 xfs_dablk_t map_off; /* last mapped file offset */
1089 int map_size; /* total entries in *map */
1090 int map_valid; /* valid entries in *map */
1091 int nmap; /* mappings to ask xfs_bmapi */
1092 xfs_dir2_db_t curdb; /* db for current block */
1093 int ra_current; /* number of read-ahead blks */
1094 int ra_index; /* *map index for read-ahead */
1095 int ra_offset; /* map entry offset for ra */
1096 int ra_want; /* readahead count wanted */
1097 struct xfs_bmbt_irec map[]; /* map vector for blocks */
1098};
1099
1100STATIC int
1101xfs_dir2_leaf_readbuf(
1102 struct xfs_inode *dp,
1103 size_t bufsize,
1104 struct xfs_dir2_leaf_map_info *mip,
1105 xfs_dir2_off_t *curoff,
1106 struct xfs_buf **bpp)
1107{
1108 struct xfs_mount *mp = dp->i_mount;
1109 struct xfs_buf *bp = *bpp;
1110 struct xfs_bmbt_irec *map = mip->map;
Dave Chinner34eefc02013-06-27 16:04:47 +10001111 struct blk_plug plug;
Dave Chinner9b73bd72012-06-22 18:50:15 +10001112 int error = 0;
1113 int length;
1114 int i;
1115 int j;
1116
1117 /*
1118 * If we have a buffer, we need to release it and
1119 * take it out of the mapping.
1120 */
1121
1122 if (bp) {
1123 xfs_trans_brelse(NULL, bp);
1124 bp = NULL;
1125 mip->map_blocks -= mp->m_dirblkfsbs;
1126 /*
1127 * Loop to get rid of the extents for the
1128 * directory block.
1129 */
1130 for (i = mp->m_dirblkfsbs; i > 0; ) {
1131 j = min_t(int, map->br_blockcount, i);
1132 map->br_blockcount -= j;
1133 map->br_startblock += j;
1134 map->br_startoff += j;
1135 /*
1136 * If mapping is done, pitch it from
1137 * the table.
1138 */
1139 if (!map->br_blockcount && --mip->map_valid)
1140 memmove(&map[0], &map[1],
1141 sizeof(map[0]) * mip->map_valid);
1142 i -= j;
1143 }
1144 }
1145
1146 /*
1147 * Recalculate the readahead blocks wanted.
1148 */
1149 mip->ra_want = howmany(bufsize + mp->m_dirblksize,
1150 mp->m_sb.sb_blocksize) - 1;
1151 ASSERT(mip->ra_want >= 0);
1152
1153 /*
1154 * If we don't have as many as we want, and we haven't
1155 * run out of data blocks, get some more mappings.
1156 */
1157 if (1 + mip->ra_want > mip->map_blocks &&
1158 mip->map_off < xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
1159 /*
1160 * Get more bmaps, fill in after the ones
1161 * we already have in the table.
1162 */
1163 mip->nmap = mip->map_size - mip->map_valid;
1164 error = xfs_bmapi_read(dp, mip->map_off,
1165 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET) -
1166 mip->map_off,
1167 &map[mip->map_valid], &mip->nmap, 0);
1168
1169 /*
1170 * Don't know if we should ignore this or try to return an
1171 * error. The trouble with returning errors is that readdir
1172 * will just stop without actually passing the error through.
1173 */
1174 if (error)
1175 goto out; /* XXX */
1176
1177 /*
1178 * If we got all the mappings we asked for, set the final map
1179 * offset based on the last bmap value received. Otherwise,
1180 * we've reached the end.
1181 */
1182 if (mip->nmap == mip->map_size - mip->map_valid) {
1183 i = mip->map_valid + mip->nmap - 1;
1184 mip->map_off = map[i].br_startoff + map[i].br_blockcount;
1185 } else
1186 mip->map_off = xfs_dir2_byte_to_da(mp,
1187 XFS_DIR2_LEAF_OFFSET);
1188
1189 /*
1190 * Look for holes in the mapping, and eliminate them. Count up
1191 * the valid blocks.
1192 */
1193 for (i = mip->map_valid; i < mip->map_valid + mip->nmap; ) {
1194 if (map[i].br_startblock == HOLESTARTBLOCK) {
1195 mip->nmap--;
1196 length = mip->map_valid + mip->nmap - i;
1197 if (length)
1198 memmove(&map[i], &map[i + 1],
1199 sizeof(map[i]) * length);
1200 } else {
1201 mip->map_blocks += map[i].br_blockcount;
1202 i++;
1203 }
1204 }
1205 mip->map_valid += mip->nmap;
1206 }
1207
1208 /*
1209 * No valid mappings, so no more data blocks.
1210 */
1211 if (!mip->map_valid) {
1212 *curoff = xfs_dir2_da_to_byte(mp, mip->map_off);
1213 goto out;
1214 }
1215
1216 /*
1217 * Read the directory block starting at the first mapping.
1218 */
1219 mip->curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
Dave Chinner33363fe2013-04-03 16:11:22 +11001220 error = xfs_dir3_data_read(NULL, dp, map->br_startoff,
Dave Chinner9b73bd72012-06-22 18:50:15 +10001221 map->br_blockcount >= mp->m_dirblkfsbs ?
Dave Chinnere4813572012-11-12 22:54:14 +11001222 XFS_FSB_TO_DADDR(mp, map->br_startblock) : -1, &bp);
Dave Chinner9b73bd72012-06-22 18:50:15 +10001223
1224 /*
1225 * Should just skip over the data block instead of giving up.
1226 */
1227 if (error)
1228 goto out; /* XXX */
1229
1230 /*
1231 * Adjust the current amount of read-ahead: we just read a block that
1232 * was previously ra.
1233 */
1234 if (mip->ra_current)
1235 mip->ra_current -= mp->m_dirblkfsbs;
1236
1237 /*
1238 * Do we need more readahead?
1239 */
Dave Chinner34eefc02013-06-27 16:04:47 +10001240 blk_start_plug(&plug);
Dave Chinner9b73bd72012-06-22 18:50:15 +10001241 for (mip->ra_index = mip->ra_offset = i = 0;
1242 mip->ra_want > mip->ra_current && i < mip->map_blocks;
1243 i += mp->m_dirblkfsbs) {
1244 ASSERT(mip->ra_index < mip->map_valid);
1245 /*
1246 * Read-ahead a contiguous directory block.
1247 */
1248 if (i > mip->ra_current &&
1249 map[mip->ra_index].br_blockcount >= mp->m_dirblkfsbs) {
Dave Chinner33363fe2013-04-03 16:11:22 +11001250 xfs_dir3_data_readahead(NULL, dp,
Dave Chinnerda6958c2012-11-12 22:54:18 +11001251 map[mip->ra_index].br_startoff + mip->ra_offset,
Dave Chinner9b73bd72012-06-22 18:50:15 +10001252 XFS_FSB_TO_DADDR(mp,
1253 map[mip->ra_index].br_startblock +
Dave Chinnerda6958c2012-11-12 22:54:18 +11001254 mip->ra_offset));
Dave Chinner9b73bd72012-06-22 18:50:15 +10001255 mip->ra_current = i;
1256 }
1257
1258 /*
1259 * Read-ahead a non-contiguous directory block. This doesn't
1260 * use our mapping, but this is a very rare case.
1261 */
1262 else if (i > mip->ra_current) {
Dave Chinner33363fe2013-04-03 16:11:22 +11001263 xfs_dir3_data_readahead(NULL, dp,
Dave Chinner9b73bd72012-06-22 18:50:15 +10001264 map[mip->ra_index].br_startoff +
Dave Chinnerda6958c2012-11-12 22:54:18 +11001265 mip->ra_offset, -1);
Dave Chinner9b73bd72012-06-22 18:50:15 +10001266 mip->ra_current = i;
1267 }
1268
1269 /*
1270 * Advance offset through the mapping table.
1271 */
1272 for (j = 0; j < mp->m_dirblkfsbs; j++) {
1273 /*
1274 * The rest of this extent but not more than a dir
1275 * block.
1276 */
1277 length = min_t(int, mp->m_dirblkfsbs,
1278 map[mip->ra_index].br_blockcount -
1279 mip->ra_offset);
1280 j += length;
1281 mip->ra_offset += length;
1282
1283 /*
1284 * Advance to the next mapping if this one is used up.
1285 */
1286 if (mip->ra_offset == map[mip->ra_index].br_blockcount) {
1287 mip->ra_offset = 0;
1288 mip->ra_index++;
1289 }
1290 }
1291 }
Dave Chinner34eefc02013-06-27 16:04:47 +10001292 blk_finish_plug(&plug);
Dave Chinner9b73bd72012-06-22 18:50:15 +10001293
1294out:
1295 *bpp = bp;
1296 return error;
1297}
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299/*
1300 * Getdents (readdir) for leaf and node directories.
1301 * This reads the data blocks only, so is the same for both forms.
1302 */
1303int /* error */
1304xfs_dir2_leaf_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 xfs_inode_t *dp, /* incore directory inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001306 void *dirent,
1307 size_t bufsize,
1308 xfs_off_t *offset,
1309 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Dave Chinner9b73bd72012-06-22 18:50:15 +10001311 struct xfs_buf *bp = NULL; /* data block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001312 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 xfs_dir2_data_entry_t *dep; /* data entry */
1314 xfs_dir2_data_unused_t *dup; /* unused entry */
Nathan Scottf6d75cb2006-03-14 13:32:24 +11001315 int error = 0; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 int length; /* temporary length value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 xfs_mount_t *mp; /* filesystem mount point */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001318 int byteoff; /* offset in current block */
1319 xfs_dir2_off_t curoff; /* current overall offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 xfs_dir2_off_t newoff; /* new curoff after new blk */
Nathan Scottf6d75cb2006-03-14 13:32:24 +11001321 char *ptr = NULL; /* pointer to current data */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001322 struct xfs_dir2_leaf_map_info *map_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 /*
1325 * If the offset is at or past the largest allowed value,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001326 * give up right away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001328 if (*offset >= XFS_DIR2_MAX_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return 0;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 mp = dp->i_mount;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /*
1334 * Set up to bmap a number of blocks based on the caller's
1335 * buffer size, the directory block size, and the filesystem
1336 * block size.
1337 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001338 length = howmany(bufsize + mp->m_dirblksize,
1339 mp->m_sb.sb_blocksize);
1340 map_info = kmem_zalloc(offsetof(struct xfs_dir2_leaf_map_info, map) +
1341 (length * sizeof(struct xfs_bmbt_irec)),
Dave Chinnerac148762013-05-20 09:51:12 +10001342 KM_SLEEP | KM_NOFS);
Dave Chinner9b73bd72012-06-22 18:50:15 +10001343 map_info->map_size = length;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /*
1346 * Inside the loop we keep the main offset value as a byte offset
1347 * in the directory file.
1348 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001349 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /*
1352 * Force this conversion through db so we truncate the offset
1353 * down to get the start of the data block.
1354 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001355 map_info->map_off = xfs_dir2_db_to_da(mp,
1356 xfs_dir2_byte_to_db(mp, curoff));
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /*
1359 * Loop over directory entries until we reach the end offset.
1360 * Get more blocks and readahead as necessary.
1361 */
1362 while (curoff < XFS_DIR2_LEAF_OFFSET) {
1363 /*
1364 * If we have no buffer, or we're off the end of the
1365 * current buffer, need to get another one.
1366 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001367 if (!bp || ptr >= (char *)bp->b_addr + mp->m_dirblksize) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001368
Dave Chinner9b73bd72012-06-22 18:50:15 +10001369 error = xfs_dir2_leaf_readbuf(dp, bufsize, map_info,
1370 &curoff, &bp);
1371 if (error || !map_info->map_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 break;
Dave Chinner9b73bd72012-06-22 18:50:15 +10001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /*
1375 * Having done a read, we need to set a new offset.
1376 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001377 newoff = xfs_dir2_db_off_to_byte(mp, map_info->curdb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /*
1379 * Start of the current block.
1380 */
1381 if (curoff < newoff)
1382 curoff = newoff;
1383 /*
1384 * Make sure we're in the right block.
1385 */
1386 else if (curoff > newoff)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001387 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
Dave Chinner9b73bd72012-06-22 18:50:15 +10001388 map_info->curdb);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001389 hdr = bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001390 xfs_dir3_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /*
1392 * Find our position in the block.
1393 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001394 ptr = (char *)xfs_dir3_data_entry_p(hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001395 byteoff = xfs_dir2_byte_to_off(mp, curoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 /*
1397 * Skip past the header.
1398 */
1399 if (byteoff == 0)
Dave Chinner33363fe2013-04-03 16:11:22 +11001400 curoff += xfs_dir3_data_entry_offset(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /*
1402 * Skip past entries until we reach our offset.
1403 */
1404 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001405 while ((char *)ptr - (char *)hdr < byteoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 dup = (xfs_dir2_data_unused_t *)ptr;
1407
Nathan Scottad354eb2006-03-17 17:27:37 +11001408 if (be16_to_cpu(dup->freetag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 == XFS_DIR2_DATA_FREE_TAG) {
1410
Nathan Scottad354eb2006-03-17 17:27:37 +11001411 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 ptr += length;
1413 continue;
1414 }
1415 dep = (xfs_dir2_data_entry_t *)ptr;
1416 length =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001417 xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 ptr += length;
1419 }
1420 /*
1421 * Now set our real offset.
1422 */
1423 curoff =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001424 xfs_dir2_db_off_to_byte(mp,
1425 xfs_dir2_byte_to_db(mp, curoff),
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001426 (char *)ptr - (char *)hdr);
1427 if (ptr >= (char *)hdr + mp->m_dirblksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 continue;
1429 }
1430 }
1431 }
1432 /*
1433 * We have a pointer to an entry.
1434 * Is it a live one?
1435 */
1436 dup = (xfs_dir2_data_unused_t *)ptr;
1437 /*
1438 * No, it's unused, skip over it.
1439 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001440 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1441 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 ptr += length;
1443 curoff += length;
1444 continue;
1445 }
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 dep = (xfs_dir2_data_entry_t *)ptr;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001448 length = xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Dave Chinner4a24cb72010-01-20 10:48:05 +11001450 if (filldir(dirent, (char *)dep->name, dep->namelen,
Christoph Hellwig15440312009-01-08 14:00:00 -05001451 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
Christoph Hellwiga19d9f82009-03-29 09:51:08 +02001452 be64_to_cpu(dep->inumber), DT_UNKNOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 break;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 /*
1456 * Advance to next entry in the block.
1457 */
1458 ptr += length;
1459 curoff += length;
Eric Sandeen8e69ce12009-09-25 19:42:26 +00001460 /* bufsize may have just been a guess; don't go negative */
1461 bufsize = bufsize > length ? bufsize - length : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463
1464 /*
1465 * All done. Set output offset value to current offset.
1466 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001467 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
Christoph Hellwig15440312009-01-08 14:00:00 -05001468 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 else
Christoph Hellwig15440312009-01-08 14:00:00 -05001470 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
Dave Chinner9b73bd72012-06-22 18:50:15 +10001471 kmem_free(map_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (bp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001473 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return error;
1475}
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478/*
1479 * Log the bests entries indicated from a leaf1 block.
1480 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001481static void
Dave Chinner24df33b2013-04-12 07:30:21 +10001482xfs_dir3_leaf_log_bests(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001484 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 int first, /* first entry to log */
1486 int last) /* last entry to log */
1487{
Nathan Scott68b3a102006-03-17 17:27:19 +11001488 __be16 *firstb; /* pointer to first entry */
1489 __be16 *lastb; /* pointer to last entry */
Dave Chinner24df33b2013-04-12 07:30:21 +10001490 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1492
Dave Chinner24df33b2013-04-12 07:30:21 +10001493 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1494 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1495
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001496 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1497 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1498 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001499 xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1501}
1502
1503/*
1504 * Log the leaf entries indicated from a leaf1 or leafn block.
1505 */
1506void
Dave Chinner24df33b2013-04-12 07:30:21 +10001507xfs_dir3_leaf_log_ents(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001509 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 int first, /* first entry to log */
1511 int last) /* last entry to log */
1512{
1513 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1514 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
Dave Chinner24df33b2013-04-12 07:30:21 +10001515 struct xfs_dir2_leaf *leaf = bp->b_addr;
1516 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001518 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001519 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1520 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1521 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1522
1523 ents = xfs_dir3_leaf_ents_p(leaf);
1524 firstlep = &ents[first];
1525 lastlep = &ents[last];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001526 xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1528}
1529
1530/*
1531 * Log the header of the leaf1 or leafn block.
1532 */
1533void
Dave Chinner24df33b2013-04-12 07:30:21 +10001534xfs_dir3_leaf_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001535 struct xfs_trans *tp,
1536 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Dave Chinner24df33b2013-04-12 07:30:21 +10001538 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001540 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001541 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1542 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1543 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1544
Dave Chinner1d9025e2012-06-22 18:50:14 +10001545 xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
Dave Chinner24df33b2013-04-12 07:30:21 +10001546 xfs_dir3_leaf_hdr_size(leaf) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
1549/*
1550 * Log the tail of the leaf1 block.
1551 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001552STATIC void
Dave Chinner24df33b2013-04-12 07:30:21 +10001553xfs_dir3_leaf_log_tail(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001554 struct xfs_trans *tp,
1555 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Dave Chinner24df33b2013-04-12 07:30:21 +10001557 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001559 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Dave Chinner24df33b2013-04-12 07:30:21 +10001561 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1562 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1563 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1564 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1565
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001566 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001567 xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 (uint)(mp->m_dirblksize - 1));
1569}
1570
1571/*
1572 * Look up the entry referred to by args in the leaf format directory.
1573 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1574 * is also used by the node-format code.
1575 */
1576int
1577xfs_dir2_leaf_lookup(
1578 xfs_da_args_t *args) /* operation arguments */
1579{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001580 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 xfs_dir2_data_entry_t *dep; /* data block entry */
1582 xfs_inode_t *dp; /* incore directory inode */
1583 int error; /* error return code */
1584 int index; /* found entry index */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001585 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 xfs_dir2_leaf_t *leaf; /* leaf structure */
1587 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1588 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10001589 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001591 trace_xfs_dir2_leaf_lookup(args);
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 /*
1594 * Look up name in the leaf block, returning both buffers and index.
1595 */
1596 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1597 return error;
1598 }
1599 tp = args->trans;
1600 dp = args->dp;
Dave Chinner24df33b2013-04-12 07:30:21 +10001601 xfs_dir3_leaf_check(dp->i_mount, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001602 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001603 ents = xfs_dir3_leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 /*
1605 * Get to the leaf entry and contained data entry address.
1606 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001607 lep = &ents[index];
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /*
1610 * Point to the data entry.
1611 */
1612 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001613 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001614 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +10001616 * Return the found inode number & CI name if appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001618 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001619 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001620 xfs_trans_brelse(tp, dbp);
1621 xfs_trans_brelse(tp, lbp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001622 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
1625/*
1626 * Look up name/hash in the leaf block.
1627 * Fill in indexp with the found index, and dbpp with the data buffer.
1628 * If not found dbpp will be NULL, and ENOENT comes back.
1629 * lbpp will always be filled in with the leaf buffer unless there's an error.
1630 */
1631static int /* error */
1632xfs_dir2_leaf_lookup_int(
1633 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001634 struct xfs_buf **lbpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 int *indexp, /* out: index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001636 struct xfs_buf **dbpp) /* out: data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001638 xfs_dir2_db_t curdb = -1; /* current data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001639 struct xfs_buf *dbp = NULL; /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 xfs_dir2_data_entry_t *dep; /* data entry */
1641 xfs_inode_t *dp; /* incore directory inode */
1642 int error; /* error return code */
1643 int index; /* index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001644 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1646 xfs_dir2_leaf_t *leaf; /* leaf structure */
1647 xfs_mount_t *mp; /* filesystem mount point */
1648 xfs_dir2_db_t newdb; /* new data block number */
1649 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001650 xfs_dir2_db_t cidb = -1; /* case match data block no. */
Barry Naujok5163f952008-05-21 16:41:01 +10001651 enum xfs_dacmp cmp; /* name compare result */
Dave Chinner24df33b2013-04-12 07:30:21 +10001652 struct xfs_dir2_leaf_entry *ents;
1653 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 dp = args->dp;
1656 tp = args->trans;
1657 mp = dp->i_mount;
Dave Chinnere6f76672012-11-12 22:54:15 +11001658
Dave Chinner24df33b2013-04-12 07:30:21 +10001659 error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001660 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 *lbpp = lbp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001664 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001665 xfs_dir3_leaf_check(mp, lbp);
1666 ents = xfs_dir3_leaf_ents_p(leaf);
1667 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 /*
1670 * Look for the first leaf entry with our hash value.
1671 */
1672 index = xfs_dir2_leaf_search_hash(args, lbp);
1673 /*
1674 * Loop over all the entries with the right hash value
1675 * looking to match the name.
1676 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001677 for (lep = &ents[index];
1678 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
1679 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 /*
1681 * Skip over stale leaf entries.
1682 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001683 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 continue;
1685 /*
1686 * Get the new data block number.
1687 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001688 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /*
1690 * If it's not the same as the old data block number,
1691 * need to pitch the old one and read the new one.
1692 */
1693 if (newdb != curdb) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001694 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001695 xfs_trans_brelse(tp, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001696 error = xfs_dir3_data_read(tp, dp,
Dave Chinnere4813572012-11-12 22:54:14 +11001697 xfs_dir2_db_to_da(mp, newdb),
1698 -1, &dbp);
Barry Naujok5163f952008-05-21 16:41:01 +10001699 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001700 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return error;
1702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 curdb = newdb;
1704 }
1705 /*
1706 * Point to the data entry.
1707 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001708 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
Barry Naujok5163f952008-05-21 16:41:01 +10001709 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 /*
Barry Naujok5163f952008-05-21 16:41:01 +10001711 * Compare name and if it's an exact match, return the index
1712 * and buffer. If it's the first case-insensitive match, store
1713 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 */
Barry Naujok5163f952008-05-21 16:41:01 +10001715 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1716 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1717 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 *indexp = index;
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001719 /* case exact match: return the current buffer. */
Barry Naujok5163f952008-05-21 16:41:01 +10001720 if (cmp == XFS_CMP_EXACT) {
Barry Naujok5163f952008-05-21 16:41:01 +10001721 *dbpp = dbp;
1722 return 0;
1723 }
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001724 cidb = curdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726 }
Barry Naujok6a178102008-05-21 16:42:05 +10001727 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +10001728 /*
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001729 * Here, we can only be doing a lookup (not a rename or remove).
1730 * If a case-insensitive match was found earlier, re-read the
1731 * appropriate data block if required and return it.
Barry Naujok5163f952008-05-21 16:41:01 +10001732 */
1733 if (args->cmpresult == XFS_CMP_CASE) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001734 ASSERT(cidb != -1);
1735 if (cidb != curdb) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001736 xfs_trans_brelse(tp, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001737 error = xfs_dir3_data_read(tp, dp,
Dave Chinnere4813572012-11-12 22:54:14 +11001738 xfs_dir2_db_to_da(mp, cidb),
1739 -1, &dbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001740 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001741 xfs_trans_brelse(tp, lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001742 return error;
1743 }
1744 }
1745 *dbpp = dbp;
Barry Naujok5163f952008-05-21 16:41:01 +10001746 return 0;
1747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 /*
1749 * No match found, return ENOENT.
1750 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001751 ASSERT(cidb == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001753 xfs_trans_brelse(tp, dbp);
1754 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return XFS_ERROR(ENOENT);
1756}
1757
1758/*
1759 * Remove an entry from a leaf format directory.
1760 */
1761int /* error */
1762xfs_dir2_leaf_removename(
1763 xfs_da_args_t *args) /* operation arguments */
1764{
Nathan Scott68b3a102006-03-17 17:27:19 +11001765 __be16 *bestsp; /* leaf block best freespace */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001766 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001768 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 xfs_dir2_data_entry_t *dep; /* data entry structure */
1770 xfs_inode_t *dp; /* incore directory inode */
1771 int error; /* error return code */
1772 xfs_dir2_db_t i; /* temporary data block # */
1773 int index; /* index into leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001774 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 xfs_dir2_leaf_t *leaf; /* leaf structure */
1776 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1777 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1778 xfs_mount_t *mp; /* filesystem mount point */
1779 int needlog; /* need to log data header */
1780 int needscan; /* need to rescan data frees */
1781 xfs_dir2_data_off_t oldbest; /* old value of best free */
1782 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001783 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001784 struct xfs_dir2_leaf_entry *ents;
1785 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001787 trace_xfs_dir2_leaf_removename(args);
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 /*
1790 * Lookup the leaf entry, get the leaf and data blocks read in.
1791 */
1792 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1793 return error;
1794 }
1795 dp = args->dp;
1796 tp = args->trans;
1797 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001798 leaf = lbp->b_addr;
1799 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001800 xfs_dir3_data_check(dp, dbp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001801 bf = xfs_dir3_data_bestfree_p(hdr);
1802 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1803 ents = xfs_dir3_leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 /*
1805 * Point to the leaf entry, use that to point to the data entry.
1806 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001807 lep = &ents[index];
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001808 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001810 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 needscan = needlog = 0;
Dave Chinner33363fe2013-04-03 16:11:22 +11001812 oldbest = be16_to_cpu(bf[0].length);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001813 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1814 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott68b3a102006-03-17 17:27:19 +11001815 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 /*
1817 * Mark the former data entry unused.
1818 */
1819 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001820 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001821 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /*
1823 * We just mark the leaf entry stale by putting a null in it.
1824 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001825 leafhdr.stale++;
1826 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
1827 xfs_dir3_leaf_log_header(tp, lbp);
1828
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001829 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinner24df33b2013-04-12 07:30:21 +10001830 xfs_dir3_leaf_log_ents(tp, lbp, index, index);
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /*
1833 * Scan the freespace in the data block again if necessary,
1834 * log the data block header if necessary.
1835 */
1836 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001837 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (needlog)
1839 xfs_dir2_data_log_header(tp, dbp);
1840 /*
1841 * If the longest freespace in the data block has changed,
1842 * put the new value in the bests table and log that.
1843 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001844 if (be16_to_cpu(bf[0].length) != oldbest) {
1845 bestsp[db] = bf[0].length;
Dave Chinner24df33b2013-04-12 07:30:21 +10001846 xfs_dir3_leaf_log_bests(tp, lbp, db, db);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
Dave Chinner33363fe2013-04-03 16:11:22 +11001848 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 /*
1850 * If the data block is now empty then get rid of the data block.
1851 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001852 if (be16_to_cpu(bf[0].length) ==
1853 mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 ASSERT(db != mp->m_dirdatablk);
1855 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1856 /*
1857 * Nope, can't get rid of it because it caused
1858 * allocation of a bmap btree block to do so.
1859 * Just go on, returning success, leaving the
1860 * empty block in place.
1861 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001862 if (error == ENOSPC && args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 error = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +10001864 xfs_dir3_leaf_check(mp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return error;
1866 }
1867 dbp = NULL;
1868 /*
1869 * If this is the last data block then compact the
1870 * bests table by getting rid of entries.
1871 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001872 if (db == be32_to_cpu(ltp->bestcount) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 /*
1874 * Look for the last active entry (i).
1875 */
1876 for (i = db - 1; i > 0; i--) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001877 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 break;
1879 }
1880 /*
1881 * Copy the table down so inactive entries at the
1882 * end are removed.
1883 */
1884 memmove(&bestsp[db - i], bestsp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001885 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001886 be32_add_cpu(&ltp->bestcount, -(db - i));
Dave Chinner24df33b2013-04-12 07:30:21 +10001887 xfs_dir3_leaf_log_tail(tp, lbp);
1888 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 } else
Nathan Scott68b3a102006-03-17 17:27:19 +11001890 bestsp[db] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
1892 /*
1893 * If the data block was not the first one, drop it.
1894 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001895 else if (db != mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 dbp = NULL;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001897
Dave Chinner24df33b2013-04-12 07:30:21 +10001898 xfs_dir3_leaf_check(mp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /*
1900 * See if we can convert to block form.
1901 */
1902 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1903}
1904
1905/*
1906 * Replace the inode number in a leaf format directory entry.
1907 */
1908int /* error */
1909xfs_dir2_leaf_replace(
1910 xfs_da_args_t *args) /* operation arguments */
1911{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001912 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 xfs_dir2_data_entry_t *dep; /* data block entry */
1914 xfs_inode_t *dp; /* incore directory inode */
1915 int error; /* error return code */
1916 int index; /* index of leaf entry */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001917 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 xfs_dir2_leaf_t *leaf; /* leaf structure */
1919 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1920 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10001921 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001923 trace_xfs_dir2_leaf_replace(args);
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 /*
1926 * Look up the entry.
1927 */
1928 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1929 return error;
1930 }
1931 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001932 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001933 ents = xfs_dir3_leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 /*
1935 * Point to the leaf entry, get data address from it.
1936 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001937 lep = &ents[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 /*
1939 * Point to the data entry.
1940 */
1941 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001942 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001943 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001944 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 /*
1946 * Put the new inode number in, log it.
1947 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001948 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 tp = args->trans;
1950 xfs_dir2_data_log_entry(tp, dbp, dep);
Dave Chinner24df33b2013-04-12 07:30:21 +10001951 xfs_dir3_leaf_check(dp->i_mount, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001952 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return 0;
1954}
1955
1956/*
1957 * Return index in the leaf block (lbp) which is either the first
1958 * one with this hash value, or if there are none, the insert point
1959 * for that hash value.
1960 */
1961int /* index value */
1962xfs_dir2_leaf_search_hash(
1963 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001964 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 xfs_dahash_t hash=0; /* hash from this entry */
1967 xfs_dahash_t hashwant; /* hash value looking for */
1968 int high; /* high leaf index */
1969 int low; /* low leaf index */
1970 xfs_dir2_leaf_t *leaf; /* leaf structure */
1971 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1972 int mid=0; /* current leaf index */
Dave Chinner24df33b2013-04-12 07:30:21 +10001973 struct xfs_dir2_leaf_entry *ents;
1974 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Dave Chinner1d9025e2012-06-22 18:50:14 +10001976 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001977 ents = xfs_dir3_leaf_ents_p(leaf);
1978 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980#ifndef __KERNEL__
Dave Chinner24df33b2013-04-12 07:30:21 +10001981 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 return 0;
1983#endif
1984 /*
1985 * Note, the table cannot be empty, so we have to go through the loop.
1986 * Binary search the leaf entries looking for our hash value.
1987 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001988 for (lep = ents, low = 0, high = leafhdr.count - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 hashwant = args->hashval;
1990 low <= high; ) {
1991 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001992 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 break;
1994 if (hash < hashwant)
1995 low = mid + 1;
1996 else
1997 high = mid - 1;
1998 }
1999 /*
2000 * Found one, back up through all the equal hash values.
2001 */
2002 if (hash == hashwant) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +11002003 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 mid--;
2005 }
2006 }
2007 /*
2008 * Need to point to an entry higher than ours.
2009 */
2010 else if (hash < hashwant)
2011 mid++;
2012 return mid;
2013}
2014
2015/*
2016 * Trim off a trailing data block. We know it's empty since the leaf
2017 * freespace table says so.
2018 */
2019int /* error */
2020xfs_dir2_leaf_trim_data(
2021 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002022 struct xfs_buf *lbp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 xfs_dir2_db_t db) /* data block number */
2024{
Nathan Scott68b3a102006-03-17 17:27:19 +11002025 __be16 *bestsp; /* leaf bests table */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002026 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 xfs_inode_t *dp; /* incore directory inode */
2028 int error; /* error return value */
2029 xfs_dir2_leaf_t *leaf; /* leaf structure */
2030 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
2031 xfs_mount_t *mp; /* filesystem mount point */
2032 xfs_trans_t *tp; /* transaction pointer */
2033
2034 dp = args->dp;
2035 mp = dp->i_mount;
2036 tp = args->trans;
2037 /*
2038 * Read the offending data block. We need its buffer.
2039 */
Dave Chinner33363fe2013-04-03 16:11:22 +11002040 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002041 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Dave Chinner1d9025e2012-06-22 18:50:14 +10002044 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002045 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002046
2047#ifdef DEBUG
2048{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002049 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002050 struct xfs_dir2_data_free *bf = xfs_dir3_data_bestfree_p(hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002051
Dave Chinner33363fe2013-04-03 16:11:22 +11002052 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2053 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
2054 ASSERT(be16_to_cpu(bf[0].length) ==
2055 mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11002056 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002057}
2058#endif
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 /*
2061 * Get rid of the data block.
2062 */
2063 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
2064 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002065 xfs_trans_brelse(tp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return error;
2067 }
2068 /*
2069 * Eliminate the last bests entry from the table.
2070 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002071 bestsp = xfs_dir2_leaf_bests_p(ltp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002072 be32_add_cpu(&ltp->bestcount, -1);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11002073 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
Dave Chinner24df33b2013-04-12 07:30:21 +10002074 xfs_dir3_leaf_log_tail(tp, lbp);
2075 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return 0;
2077}
2078
Christoph Hellwig22823962011-07-08 14:35:53 +02002079static inline size_t
Dave Chinner24df33b2013-04-12 07:30:21 +10002080xfs_dir3_leaf_size(
2081 struct xfs_dir3_icleaf_hdr *hdr,
Christoph Hellwig22823962011-07-08 14:35:53 +02002082 int counts)
2083{
Dave Chinner24df33b2013-04-12 07:30:21 +10002084 int entries;
2085 int hdrsize;
Christoph Hellwig22823962011-07-08 14:35:53 +02002086
Dave Chinner24df33b2013-04-12 07:30:21 +10002087 entries = hdr->count - hdr->stale;
2088 if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
2089 hdr->magic == XFS_DIR2_LEAFN_MAGIC)
2090 hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
2091 else
2092 hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
2093
2094 return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
2095 + counts * sizeof(xfs_dir2_data_off_t)
2096 + sizeof(xfs_dir2_leaf_tail_t);
Christoph Hellwig22823962011-07-08 14:35:53 +02002097}
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099/*
2100 * Convert node form directory to leaf form directory.
2101 * The root of the node form dir needs to already be a LEAFN block.
2102 * Just return if we can't do anything.
2103 */
2104int /* error */
2105xfs_dir2_node_to_leaf(
2106 xfs_da_state_t *state) /* directory operation state */
2107{
2108 xfs_da_args_t *args; /* operation arguments */
2109 xfs_inode_t *dp; /* incore directory inode */
2110 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002111 struct xfs_buf *fbp; /* buffer for freespace block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 xfs_fileoff_t fo; /* freespace file offset */
2113 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002114 struct xfs_buf *lbp; /* buffer for leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
2116 xfs_dir2_leaf_t *leaf; /* leaf structure */
2117 xfs_mount_t *mp; /* filesystem mount point */
2118 int rval; /* successful free trim? */
2119 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10002120 struct xfs_dir3_icleaf_hdr leafhdr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002121 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 /*
2124 * There's more than a leaf level in the btree, so there must
2125 * be multiple leafn blocks. Give up.
2126 */
2127 if (state->path.active > 1)
2128 return 0;
2129 args = state->args;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002130
2131 trace_xfs_dir2_node_to_leaf(args);
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 mp = state->mp;
2134 dp = args->dp;
2135 tp = args->trans;
2136 /*
2137 * Get the last offset in the file.
2138 */
2139 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
2140 return error;
2141 }
2142 fo -= mp->m_dirblkfsbs;
2143 /*
2144 * If there are freespace blocks other than the first one,
2145 * take this opportunity to remove trailing empty freespace blocks
2146 * that may have been left behind during no-space-reservation
2147 * operations.
2148 */
2149 while (fo > mp->m_dirfreeblk) {
2150 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
2151 return error;
2152 }
2153 if (rval)
2154 fo -= mp->m_dirblkfsbs;
2155 else
2156 return 0;
2157 }
2158 /*
2159 * Now find the block just before the freespace block.
2160 */
2161 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
2162 return error;
2163 }
2164 /*
2165 * If it's not the single leaf block, give up.
2166 */
2167 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
2168 return 0;
2169 lbp = state->path.blk[0].bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002170 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10002171 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
2172
2173 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
2174 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 /*
2177 * Read the freespace block.
2178 */
Dave Chinner20252072012-11-12 22:54:13 +11002179 error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002180 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002182 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002183 xfs_dir3_free_hdr_from_disk(&freehdr, free);
2184
2185 ASSERT(!freehdr.firstdb);
Christoph Hellwig22823962011-07-08 14:35:53 +02002186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 /*
2188 * Now see if the leafn and free data will fit in a leaf1.
2189 * If not, release the buffer and give up.
2190 */
Dave Chinner24df33b2013-04-12 07:30:21 +10002191 if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > mp->m_dirblksize) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002192 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return 0;
2194 }
Christoph Hellwig22823962011-07-08 14:35:53 +02002195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 /*
2197 * If the leaf has any stale entries in it, compress them out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 */
Dave Chinner24df33b2013-04-12 07:30:21 +10002199 if (leafhdr.stale)
2200 xfs_dir3_leaf_compact(args, &leafhdr, lbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002201
Dave Chinner24df33b2013-04-12 07:30:21 +10002202 lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +11002203 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
Dave Chinner24df33b2013-04-12 07:30:21 +10002204 leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
2205 ? XFS_DIR2_LEAF1_MAGIC
2206 : XFS_DIR3_LEAF1_MAGIC;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 /*
2209 * Set up the leaf tail from the freespace block.
2210 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002211 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002212 ltp->bestcount = cpu_to_be32(freehdr.nvalid);
Dave Chinner24df33b2013-04-12 07:30:21 +10002213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 /*
2215 * Set up the leaf bests table.
2216 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002217 memcpy(xfs_dir2_leaf_bests_p(ltp), xfs_dir3_free_bests_p(mp, free),
2218 freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
Dave Chinner24df33b2013-04-12 07:30:21 +10002219
2220 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
2221 xfs_dir3_leaf_log_header(tp, lbp);
2222 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
2223 xfs_dir3_leaf_log_tail(tp, lbp);
2224 xfs_dir3_leaf_check(mp, lbp);
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /*
2227 * Get rid of the freespace block.
2228 */
2229 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
2230 if (error) {
2231 /*
2232 * This can't fail here because it can only happen when
2233 * punching out the middle of an extent, and this is an
2234 * isolated block.
2235 */
2236 ASSERT(error != ENOSPC);
2237 return error;
2238 }
2239 fbp = NULL;
2240 /*
2241 * Now see if we can convert the single-leaf directory
2242 * down to a block form directory.
2243 * This routine always kills the dabuf for the leaf, so
2244 * eliminate it from the path.
2245 */
2246 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
2247 state->path.blk[0].bp = NULL;
2248 return error;
2249}