blob: d5937dab8f5609f1bd015be78df0ca38bccc78c6 [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"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dinode.h"
32#include "xfs_inode.h"
33#include "xfs_bmap.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100034#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020035#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000037#include "xfs_trace.h"
Dave Chinner24df33b2013-04-12 07:30:21 +100038#include "xfs_buf_item.h"
39#include "xfs_cksum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * Local function declarations.
43 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100044static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
45 int *indexp, struct xfs_buf **dbpp);
Dave Chinner24df33b2013-04-12 07:30:21 +100046static void xfs_dir3_leaf_log_bests(struct xfs_trans *tp, struct xfs_buf *bp,
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100047 int first, int last);
Dave Chinner24df33b2013-04-12 07:30:21 +100048static void xfs_dir3_leaf_log_tail(struct xfs_trans *tp, struct xfs_buf *bp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100049
Dave Chinner24df33b2013-04-12 07:30:21 +100050/*
51 * Check the internal consistency of a leaf1 block.
52 * Pop an assert if something is wrong.
53 */
54#ifdef DEBUG
55#define xfs_dir3_leaf_check(mp, bp) \
56do { \
57 if (!xfs_dir3_leaf1_check((mp), (bp))) \
58 ASSERT(0); \
59} while (0);
60
61STATIC bool
62xfs_dir3_leaf1_check(
63 struct xfs_mount *mp,
64 struct xfs_buf *bp)
65{
66 struct xfs_dir2_leaf *leaf = bp->b_addr;
67 struct xfs_dir3_icleaf_hdr leafhdr;
68
69 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
70
71 if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
72 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
73 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
74 return false;
75 } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
76 return false;
77
78 return xfs_dir3_leaf_check_int(mp, &leafhdr, leaf);
79}
80#else
81#define xfs_dir3_leaf_check(mp, bp)
82#endif
83
84void
85xfs_dir3_leaf_hdr_from_disk(
86 struct xfs_dir3_icleaf_hdr *to,
87 struct xfs_dir2_leaf *from)
88{
89 if (from->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
90 from->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)) {
91 to->forw = be32_to_cpu(from->hdr.info.forw);
92 to->back = be32_to_cpu(from->hdr.info.back);
93 to->magic = be16_to_cpu(from->hdr.info.magic);
94 to->count = be16_to_cpu(from->hdr.count);
95 to->stale = be16_to_cpu(from->hdr.stale);
96 } else {
97 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)from;
98
99 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
100 to->back = be32_to_cpu(hdr3->info.hdr.back);
101 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
102 to->count = be16_to_cpu(hdr3->count);
103 to->stale = be16_to_cpu(hdr3->stale);
104 }
105
106 ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
107 to->magic == XFS_DIR3_LEAF1_MAGIC ||
108 to->magic == XFS_DIR2_LEAFN_MAGIC ||
109 to->magic == XFS_DIR3_LEAFN_MAGIC);
110}
111
112void
113xfs_dir3_leaf_hdr_to_disk(
114 struct xfs_dir2_leaf *to,
115 struct xfs_dir3_icleaf_hdr *from)
116{
117 ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
118 from->magic == XFS_DIR3_LEAF1_MAGIC ||
119 from->magic == XFS_DIR2_LEAFN_MAGIC ||
120 from->magic == XFS_DIR3_LEAFN_MAGIC);
121
122 if (from->magic == XFS_DIR2_LEAF1_MAGIC ||
123 from->magic == XFS_DIR2_LEAFN_MAGIC) {
124 to->hdr.info.forw = cpu_to_be32(from->forw);
125 to->hdr.info.back = cpu_to_be32(from->back);
126 to->hdr.info.magic = cpu_to_be16(from->magic);
127 to->hdr.count = cpu_to_be16(from->count);
128 to->hdr.stale = cpu_to_be16(from->stale);
129 } else {
130 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)to;
131
132 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
133 hdr3->info.hdr.back = cpu_to_be32(from->back);
134 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
135 hdr3->count = cpu_to_be16(from->count);
136 hdr3->stale = cpu_to_be16(from->stale);
137 }
138}
139
140bool
141xfs_dir3_leaf_check_int(
142 struct xfs_mount *mp,
143 struct xfs_dir3_icleaf_hdr *hdr,
144 struct xfs_dir2_leaf *leaf)
145{
146 struct xfs_dir2_leaf_entry *ents;
147 xfs_dir2_leaf_tail_t *ltp;
148 int stale;
149 int i;
150
151 ents = xfs_dir3_leaf_ents_p(leaf);
152 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
153
154 /*
155 * XXX (dgc): This value is not restrictive enough.
156 * Should factor in the size of the bests table as well.
157 * We can deduce a value for that from di_size.
158 */
159 if (hdr->count > xfs_dir3_max_leaf_ents(mp, leaf))
160 return false;
161
162 /* Leaves and bests don't overlap in leaf format. */
163 if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
164 hdr->magic == XFS_DIR3_LEAF1_MAGIC) &&
165 (char *)&ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
166 return false;
167
168 /* Check hash value order, count stale entries. */
169 for (i = stale = 0; i < hdr->count; i++) {
170 if (i + 1 < hdr->count) {
171 if (be32_to_cpu(ents[i].hashval) >
172 be32_to_cpu(ents[i + 1].hashval))
173 return false;
174 }
175 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
176 stale++;
177 }
178 if (hdr->stale != stale)
179 return false;
180 return true;
181}
182
Dave Chinner0f295a22013-09-03 10:06:58 +1000183/*
184 * We verify the magic numbers before decoding the leaf header so that on debug
185 * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
186 * to incorrect magic numbers.
187 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000188static bool
189xfs_dir3_leaf_verify(
Dave Chinnere6f76672012-11-12 22:54:15 +1100190 struct xfs_buf *bp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000191 __uint16_t magic)
Dave Chinnere6f76672012-11-12 22:54:15 +1100192{
193 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner24df33b2013-04-12 07:30:21 +1000194 struct xfs_dir2_leaf *leaf = bp->b_addr;
195 struct xfs_dir3_icleaf_hdr leafhdr;
Dave Chinnere6f76672012-11-12 22:54:15 +1100196
Dave Chinner24df33b2013-04-12 07:30:21 +1000197 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
198
Dave Chinner24df33b2013-04-12 07:30:21 +1000199 if (xfs_sb_version_hascrc(&mp->m_sb)) {
200 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
Dave Chinner0f295a22013-09-03 10:06:58 +1000201 __uint16_t magic3;
Dave Chinner24df33b2013-04-12 07:30:21 +1000202
Dave Chinner0f295a22013-09-03 10:06:58 +1000203 magic3 = (magic == XFS_DIR2_LEAF1_MAGIC) ? XFS_DIR3_LEAF1_MAGIC
204 : XFS_DIR3_LEAFN_MAGIC;
205
206 if (leaf3->info.hdr.magic != cpu_to_be16(magic3))
Dave Chinner24df33b2013-04-12 07:30:21 +1000207 return false;
Dave Chinner24df33b2013-04-12 07:30:21 +1000208 if (!uuid_equal(&leaf3->info.uuid, &mp->m_sb.sb_uuid))
209 return false;
210 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
211 return false;
212 } else {
Dave Chinner0f295a22013-09-03 10:06:58 +1000213 if (leaf->hdr.info.magic != cpu_to_be16(magic))
Dave Chinner24df33b2013-04-12 07:30:21 +1000214 return false;
215 }
Dave Chinner0f295a22013-09-03 10:06:58 +1000216
217 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000218 return xfs_dir3_leaf_check_int(mp, &leafhdr, leaf);
219}
220
221static void
222__read_verify(
223 struct xfs_buf *bp,
224 __uint16_t magic)
225{
226 struct xfs_mount *mp = bp->b_target->bt_mount;
227
228 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
229 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
230 XFS_DIR3_LEAF_CRC_OFF)) ||
231 !xfs_dir3_leaf_verify(bp, magic)) {
232 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
Dave Chinnere6f76672012-11-12 22:54:15 +1100233 xfs_buf_ioerror(bp, EFSCORRUPTED);
234 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100235}
Dave Chinnere6f76672012-11-12 22:54:15 +1100236
Dave Chinner612cfbf2012-11-14 17:52:32 +1100237static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000238__write_verify(
239 struct xfs_buf *bp,
240 __uint16_t magic)
Dave Chinner612cfbf2012-11-14 17:52:32 +1100241{
Dave Chinner24df33b2013-04-12 07:30:21 +1000242 struct xfs_mount *mp = bp->b_target->bt_mount;
243 struct xfs_buf_log_item *bip = bp->b_fspriv;
244 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
245
246 if (!xfs_dir3_leaf_verify(bp, magic)) {
247 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
248 xfs_buf_ioerror(bp, EFSCORRUPTED);
249 return;
250 }
251
252 if (!xfs_sb_version_hascrc(&mp->m_sb))
253 return;
254
255 if (bip)
256 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
257
258 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_LEAF_CRC_OFF);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100259}
260
261static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000262xfs_dir3_leaf1_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100263 struct xfs_buf *bp)
264{
Dave Chinner24df33b2013-04-12 07:30:21 +1000265 __read_verify(bp, XFS_DIR2_LEAF1_MAGIC);
Dave Chinner1813dd62012-11-14 17:54:40 +1100266}
267
Dave Chinner24df33b2013-04-12 07:30:21 +1000268static void
269xfs_dir3_leaf1_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100270 struct xfs_buf *bp)
271{
Dave Chinner24df33b2013-04-12 07:30:21 +1000272 __write_verify(bp, XFS_DIR2_LEAF1_MAGIC);
Dave Chinnere6f76672012-11-12 22:54:15 +1100273}
274
Dave Chinner24df33b2013-04-12 07:30:21 +1000275static void
276xfs_dir3_leafn_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100277 struct xfs_buf *bp)
Dave Chinnere6f76672012-11-12 22:54:15 +1100278{
Dave Chinner24df33b2013-04-12 07:30:21 +1000279 __read_verify(bp, XFS_DIR2_LEAFN_MAGIC);
Dave Chinnere6f76672012-11-12 22:54:15 +1100280}
281
Dave Chinner24df33b2013-04-12 07:30:21 +1000282static void
283xfs_dir3_leafn_write_verify(
284 struct xfs_buf *bp)
285{
286 __write_verify(bp, XFS_DIR2_LEAFN_MAGIC);
287}
288
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100289const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
Dave Chinner24df33b2013-04-12 07:30:21 +1000290 .verify_read = xfs_dir3_leaf1_read_verify,
291 .verify_write = xfs_dir3_leaf1_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100292};
293
Dave Chinner24df33b2013-04-12 07:30:21 +1000294const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
295 .verify_read = xfs_dir3_leafn_read_verify,
296 .verify_write = xfs_dir3_leafn_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100297};
Dave Chinnere6f76672012-11-12 22:54:15 +1100298
299static int
Dave Chinner24df33b2013-04-12 07:30:21 +1000300xfs_dir3_leaf_read(
Dave Chinnere6f76672012-11-12 22:54:15 +1100301 struct xfs_trans *tp,
302 struct xfs_inode *dp,
303 xfs_dablk_t fbno,
304 xfs_daddr_t mappedbno,
305 struct xfs_buf **bpp)
306{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100307 int err;
308
309 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000310 XFS_DATA_FORK, &xfs_dir3_leaf1_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100311 if (!err && tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100312 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100313 return err;
Dave Chinnere6f76672012-11-12 22:54:15 +1100314}
315
316int
Dave Chinner24df33b2013-04-12 07:30:21 +1000317xfs_dir3_leafn_read(
Dave Chinnere6f76672012-11-12 22:54:15 +1100318 struct xfs_trans *tp,
319 struct xfs_inode *dp,
320 xfs_dablk_t fbno,
321 xfs_daddr_t mappedbno,
322 struct xfs_buf **bpp)
323{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100324 int err;
325
326 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000327 XFS_DATA_FORK, &xfs_dir3_leafn_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100328 if (!err && tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100329 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100330 return err;
Dave Chinner24df33b2013-04-12 07:30:21 +1000331}
332
333/*
334 * Initialize a new leaf block, leaf1 or leafn magic accepted.
335 */
336static void
337xfs_dir3_leaf_init(
338 struct xfs_mount *mp,
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100339 struct xfs_trans *tp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000340 struct xfs_buf *bp,
341 xfs_ino_t owner,
342 __uint16_t type)
343{
344 struct xfs_dir2_leaf *leaf = bp->b_addr;
345
346 ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
347
348 if (xfs_sb_version_hascrc(&mp->m_sb)) {
349 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
350
351 memset(leaf3, 0, sizeof(*leaf3));
352
353 leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
354 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
355 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
356 leaf3->info.blkno = cpu_to_be64(bp->b_bn);
357 leaf3->info.owner = cpu_to_be64(owner);
358 uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_uuid);
359 } else {
360 memset(leaf, 0, sizeof(*leaf));
361 leaf->hdr.info.magic = cpu_to_be16(type);
362 }
363
364 /*
365 * If it's a leaf-format directory initialize the tail.
366 * Caller is responsible for initialising the bests table.
367 */
368 if (type == XFS_DIR2_LEAF1_MAGIC) {
369 struct xfs_dir2_leaf_tail *ltp;
370
371 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
372 ltp->bestcount = 0;
373 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100374 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100375 } else {
Dave Chinner24df33b2013-04-12 07:30:21 +1000376 bp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100377 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100378 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000379}
380
381int
382xfs_dir3_leaf_get_buf(
383 xfs_da_args_t *args,
384 xfs_dir2_db_t bno,
385 struct xfs_buf **bpp,
386 __uint16_t magic)
387{
388 struct xfs_inode *dp = args->dp;
389 struct xfs_trans *tp = args->trans;
390 struct xfs_mount *mp = dp->i_mount;
391 struct xfs_buf *bp;
392 int error;
393
394 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
395 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
396 bno < XFS_DIR2_FREE_FIRSTDB(mp));
397
398 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
399 XFS_DATA_FORK);
400 if (error)
401 return error;
402
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100403 xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
Dave Chinner24df33b2013-04-12 07:30:21 +1000404 xfs_dir3_leaf_log_header(tp, bp);
405 if (magic == XFS_DIR2_LEAF1_MAGIC)
406 xfs_dir3_leaf_log_tail(tp, bp);
407 *bpp = bp;
408 return 0;
Dave Chinnere6f76672012-11-12 22:54:15 +1100409}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411/*
412 * Convert a block form directory to a leaf form directory.
413 */
414int /* error */
415xfs_dir2_block_to_leaf(
416 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000417 struct xfs_buf *dbp) /* input block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Nathan Scott68b3a102006-03-17 17:27:19 +1100419 __be16 *bestsp; /* leaf's bestsp entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 xfs_dablk_t blkno; /* leaf block's bno */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200421 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
423 xfs_dir2_block_tail_t *btp; /* block's tail */
424 xfs_inode_t *dp; /* incore directory inode */
425 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000426 struct xfs_buf *lbp; /* leaf block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 xfs_dir2_db_t ldb; /* leaf block's bno */
428 xfs_dir2_leaf_t *leaf; /* leaf structure */
429 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
430 xfs_mount_t *mp; /* filesystem mount point */
431 int needlog; /* need to log block header */
432 int needscan; /* need to rescan bestfree */
433 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +1100434 struct xfs_dir2_data_free *bf;
Dave Chinner24df33b2013-04-12 07:30:21 +1000435 struct xfs_dir2_leaf_entry *ents;
436 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000438 trace_xfs_dir2_block_to_leaf(args);
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 dp = args->dp;
441 mp = dp->i_mount;
442 tp = args->trans;
443 /*
444 * Add the leaf block to the inode.
445 * This interface will only put blocks in the leaf/node range.
446 * Since that's empty now, we'll get the root (block 0 in range).
447 */
448 if ((error = xfs_da_grow_inode(args, &blkno))) {
449 return error;
450 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000451 ldb = xfs_dir2_da_to_db(mp, blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
453 /*
454 * Initialize the leaf block, get a buffer for it.
455 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000456 error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
457 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +1000459
Dave Chinner1d9025e2012-06-22 18:50:14 +1000460 leaf = lbp->b_addr;
461 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100462 xfs_dir3_data_check(dp, dbp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200463 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000464 blp = xfs_dir2_block_leaf_p(btp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100465 bf = xfs_dir3_data_bestfree_p(hdr);
Dave Chinner24df33b2013-04-12 07:30:21 +1000466 ents = xfs_dir3_leaf_ents_p(leaf);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /*
469 * Set the counts in the leaf header.
470 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000471 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
472 leafhdr.count = be32_to_cpu(btp->count);
473 leafhdr.stale = be32_to_cpu(btp->stale);
474 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
475 xfs_dir3_leaf_log_header(tp, lbp);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /*
478 * Could compact these but I think we always do the conversion
479 * after squeezing out stale entries.
480 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000481 memcpy(ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
482 xfs_dir3_leaf_log_ents(tp, lbp, 0, leafhdr.count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 needscan = 0;
484 needlog = 1;
485 /*
486 * Make the space formerly occupied by the leaf entries and block
487 * tail be free.
488 */
489 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200490 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
491 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 (char *)blp),
493 &needlog, &needscan);
494 /*
495 * Fix up the block header, make it a data block.
496 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100497 dbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100498 xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
Dave Chinner33363fe2013-04-03 16:11:22 +1100499 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
500 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
501 else
502 hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200505 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /*
507 * Set up leaf tail and bests table.
508 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000509 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100510 ltp->bestcount = cpu_to_be32(1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000511 bestsp = xfs_dir2_leaf_bests_p(ltp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100512 bestsp[0] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /*
514 * Log the data header and leaf bests table.
515 */
516 if (needlog)
517 xfs_dir2_data_log_header(tp, dbp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000518 xfs_dir3_leaf_check(mp, lbp);
Dave Chinner33363fe2013-04-03 16:11:22 +1100519 xfs_dir3_data_check(dp, dbp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000520 xfs_dir3_leaf_log_bests(tp, lbp, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return 0;
522}
523
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200524STATIC void
Dave Chinner24df33b2013-04-12 07:30:21 +1000525xfs_dir3_leaf_find_stale(
526 struct xfs_dir3_icleaf_hdr *leafhdr,
527 struct xfs_dir2_leaf_entry *ents,
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200528 int index,
529 int *lowstale,
530 int *highstale)
531{
532 /*
533 * Find the first stale entry before our index, if any.
534 */
535 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000536 if (ents[*lowstale].address ==
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200537 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
538 break;
539 }
540
541 /*
542 * Find the first stale entry at or after our index, if any.
543 * Stop if the result would require moving more entries than using
544 * lowstale.
545 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000546 for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
547 if (ents[*highstale].address ==
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200548 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
549 break;
550 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
551 break;
552 }
553}
554
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200555struct xfs_dir2_leaf_entry *
Dave Chinner24df33b2013-04-12 07:30:21 +1000556xfs_dir3_leaf_find_entry(
557 struct xfs_dir3_icleaf_hdr *leafhdr,
558 struct xfs_dir2_leaf_entry *ents,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200559 int index, /* leaf table position */
560 int compact, /* need to compact leaves */
561 int lowstale, /* index of prev stale leaf */
562 int highstale, /* index of next stale leaf */
563 int *lfloglow, /* low leaf logging index */
564 int *lfloghigh) /* high leaf logging index */
565{
Dave Chinner24df33b2013-04-12 07:30:21 +1000566 if (!leafhdr->stale) {
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200567 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
568
569 /*
570 * Now we need to make room to insert the leaf entry.
571 *
572 * If there are no stale entries, just insert a hole at index.
573 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000574 lep = &ents[index];
575 if (index < leafhdr->count)
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200576 memmove(lep + 1, lep,
Dave Chinner24df33b2013-04-12 07:30:21 +1000577 (leafhdr->count - index) * sizeof(*lep));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200578
579 /*
580 * Record low and high logging indices for the leaf.
581 */
582 *lfloglow = index;
Dave Chinner24df33b2013-04-12 07:30:21 +1000583 *lfloghigh = leafhdr->count++;
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200584 return lep;
585 }
586
587 /*
588 * There are stale entries.
589 *
590 * We will use one of them for the new entry. It's probably not at
591 * the right location, so we'll have to shift some up or down first.
592 *
593 * If we didn't compact before, we need to find the nearest stale
594 * entries before and after our insertion point.
595 */
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200596 if (compact == 0)
Dave Chinner24df33b2013-04-12 07:30:21 +1000597 xfs_dir3_leaf_find_stale(leafhdr, ents, index,
598 &lowstale, &highstale);
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200599
600 /*
601 * If the low one is better, use it.
602 */
603 if (lowstale >= 0 &&
Dave Chinner24df33b2013-04-12 07:30:21 +1000604 (highstale == leafhdr->count ||
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200605 index - lowstale - 1 < highstale - index)) {
606 ASSERT(index - lowstale - 1 >= 0);
Dave Chinner24df33b2013-04-12 07:30:21 +1000607 ASSERT(ents[lowstale].address ==
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200608 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200609
610 /*
611 * Copy entries up to cover the stale entry and make room
612 * for the new entry.
613 */
614 if (index - lowstale - 1 > 0) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000615 memmove(&ents[lowstale], &ents[lowstale + 1],
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200616 (index - lowstale - 1) *
Dave Chinner24df33b2013-04-12 07:30:21 +1000617 sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200618 }
619 *lfloglow = MIN(lowstale, *lfloglow);
620 *lfloghigh = MAX(index - 1, *lfloghigh);
Dave Chinner24df33b2013-04-12 07:30:21 +1000621 leafhdr->stale--;
622 return &ents[index - 1];
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200623 }
624
625 /*
626 * The high one is better, so use that one.
627 */
628 ASSERT(highstale - index >= 0);
Dave Chinner24df33b2013-04-12 07:30:21 +1000629 ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200630
631 /*
632 * Copy entries down to cover the stale entry and make room for the
633 * new entry.
634 */
635 if (highstale - index > 0) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000636 memmove(&ents[index + 1], &ents[index],
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200637 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
638 }
639 *lfloglow = MIN(index, *lfloglow);
640 *lfloghigh = MAX(highstale, *lfloghigh);
Dave Chinner24df33b2013-04-12 07:30:21 +1000641 leafhdr->stale--;
642 return &ents[index];
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645/*
646 * Add an entry to a leaf form directory.
647 */
648int /* error */
649xfs_dir2_leaf_addname(
650 xfs_da_args_t *args) /* operation arguments */
651{
Nathan Scott68b3a102006-03-17 17:27:19 +1100652 __be16 *bestsp; /* freespace table in leaf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int compact; /* need to compact leaves */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200654 xfs_dir2_data_hdr_t *hdr; /* data block header */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000655 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 xfs_dir2_data_entry_t *dep; /* data block entry */
657 xfs_inode_t *dp; /* incore directory inode */
658 xfs_dir2_data_unused_t *dup; /* data unused entry */
659 int error; /* error return value */
660 int grown; /* allocated new data block */
661 int highstale; /* index of next stale leaf */
662 int i; /* temporary, index */
663 int index; /* leaf table position */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000664 struct xfs_buf *lbp; /* leaf's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 xfs_dir2_leaf_t *leaf; /* leaf structure */
666 int length; /* length of new entry */
667 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
668 int lfloglow; /* low leaf logging index */
669 int lfloghigh; /* high leaf logging index */
670 int lowstale; /* index of prev stale leaf */
671 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
672 xfs_mount_t *mp; /* filesystem mount point */
673 int needbytes; /* leaf block bytes needed */
674 int needlog; /* need to log data header */
675 int needscan; /* need to rescan data free */
Nathan Scott3d693c62006-03-17 17:28:27 +1100676 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 xfs_trans_t *tp; /* transaction pointer */
678 xfs_dir2_db_t use_block; /* data block number */
Dave Chinner33363fe2013-04-03 16:11:22 +1100679 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +1000680 struct xfs_dir2_leaf_entry *ents;
681 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000683 trace_xfs_dir2_leaf_addname(args);
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 dp = args->dp;
686 tp = args->trans;
687 mp = dp->i_mount;
Dave Chinnere6f76672012-11-12 22:54:15 +1100688
Dave Chinner24df33b2013-04-12 07:30:21 +1000689 error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100690 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +1100692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /*
694 * Look up the entry by hash value and name.
695 * We know it's not there, our caller has already done a lookup.
696 * So the index is of the entry to insert in front of.
697 * But if there are dup hash values the index is of the first of those.
698 */
699 index = xfs_dir2_leaf_search_hash(args, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000700 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000701 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000702 ents = xfs_dir3_leaf_ents_p(leaf);
703 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000704 bestsp = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner0cb97762013-08-12 20:50:09 +1000705 length = xfs_dir3_data_entsize(mp, args->namelen);
Dave Chinner24df33b2013-04-12 07:30:21 +1000706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /*
708 * See if there are any entries with the same hash value
709 * and space in their block for the new entry.
710 * This is good because it puts multiple same-hash value entries
711 * in a data block, improving the lookup of those entries.
712 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000713 for (use_block = -1, lep = &ents[index];
714 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 index++, lep++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100716 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 continue;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000718 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100719 ASSERT(i < be32_to_cpu(ltp->bestcount));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200720 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
Nathan Scott68b3a102006-03-17 17:27:19 +1100721 if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 use_block = i;
723 break;
724 }
725 }
726 /*
727 * Didn't find a block yet, linear search all the data blocks.
728 */
729 if (use_block == -1) {
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100730 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /*
732 * Remember a block we see that's missing.
733 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200734 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
735 use_block == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 use_block = i;
Nathan Scott68b3a102006-03-17 17:27:19 +1100737 else if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 use_block = i;
739 break;
740 }
741 }
742 }
743 /*
744 * How many bytes do we need in the leaf block?
745 */
Christoph Hellwig22823962011-07-08 14:35:53 +0200746 needbytes = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000747 if (!leafhdr.stale)
Christoph Hellwig22823962011-07-08 14:35:53 +0200748 needbytes += sizeof(xfs_dir2_leaf_entry_t);
749 if (use_block == -1)
750 needbytes += sizeof(xfs_dir2_data_off_t);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /*
753 * Now kill use_block if it refers to a missing block, so we
754 * can use it as an indication of allocation needed.
755 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200756 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 use_block = -1;
758 /*
759 * If we don't have enough free bytes but we can make enough
760 * by compacting out stale entries, we'll do that.
761 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000762 if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
763 leafhdr.stale > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 compact = 1;
Dave Chinner24df33b2013-04-12 07:30:21 +1000765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /*
767 * Otherwise if we don't have enough free bytes we need to
768 * convert to node form.
769 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000770 else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /*
772 * Just checking or no space reservation, give up.
773 */
Barry Naujok6a178102008-05-21 16:42:05 +1000774 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
775 args->total == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000776 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return XFS_ERROR(ENOSPC);
778 }
779 /*
780 * Convert to node form.
781 */
782 error = xfs_dir2_leaf_to_node(args, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (error)
784 return error;
785 /*
786 * Then add the new entry.
787 */
788 return xfs_dir2_node_addname(args);
789 }
790 /*
791 * Otherwise it will fit without compaction.
792 */
793 else
794 compact = 0;
795 /*
796 * If just checking, then it will fit unless we needed to allocate
797 * a new data block.
798 */
Barry Naujok6a178102008-05-21 16:42:05 +1000799 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000800 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
802 }
803 /*
804 * If no allocations are allowed, return now before we've
805 * changed anything.
806 */
807 if (args->total == 0 && use_block == -1) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000808 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return XFS_ERROR(ENOSPC);
810 }
811 /*
812 * Need to compact the leaf entries, removing stale ones.
813 * Leave one stale entry behind - the one closest to our
814 * insertion index - and we'll shift that one to our insertion
815 * point later.
816 */
817 if (compact) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000818 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
819 &highstale, &lfloglow, &lfloghigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821 /*
822 * There are stale entries, so we'll need log-low and log-high
823 * impossibly bad values later.
824 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000825 else if (leafhdr.stale) {
826 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 lfloghigh = -1;
828 }
829 /*
830 * If there was no data block space found, we need to allocate
831 * a new one.
832 */
833 if (use_block == -1) {
834 /*
835 * Add the new data block.
836 */
837 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
838 &use_block))) {
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 * Initialize the block.
844 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100845 if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000846 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return error;
848 }
849 /*
850 * If we're adding a new data block on the end we need to
851 * extend the bests table. Copy it up one entry.
852 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100853 if (use_block >= be32_to_cpu(ltp->bestcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 bestsp--;
855 memmove(&bestsp[0], &bestsp[1],
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100856 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800857 be32_add_cpu(&ltp->bestcount, 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000858 xfs_dir3_leaf_log_tail(tp, lbp);
859 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861 /*
862 * If we're filling in a previously empty block just log it.
863 */
864 else
Dave Chinner24df33b2013-04-12 07:30:21 +1000865 xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000866 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100867 bf = xfs_dir3_data_bestfree_p(hdr);
868 bestsp[use_block] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 grown = 1;
Dave Chinnere4813572012-11-12 22:54:14 +1100870 } else {
871 /*
872 * Already had space in some data block.
873 * Just read that one in.
874 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100875 error = xfs_dir3_data_read(tp, dp,
Dave Chinnere4813572012-11-12 22:54:14 +1100876 xfs_dir2_db_to_da(mp, use_block),
877 -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100878 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000879 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return error;
881 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000882 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100883 bf = xfs_dir3_data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 grown = 0;
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /*
887 * Point to the biggest freespace in our data block.
888 */
889 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +1100890 ((char *)hdr + be16_to_cpu(bf[0].offset));
Nathan Scottad354eb2006-03-17 17:27:37 +1100891 ASSERT(be16_to_cpu(dup->length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 needscan = needlog = 0;
893 /*
894 * Mark the initial part of our freespace in use for the new entry.
895 */
896 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200897 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 &needlog, &needscan);
899 /*
900 * Initialize our new entry (at last).
901 */
902 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000903 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 dep->namelen = args->namelen;
905 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000906 xfs_dir3_dirent_put_ftype(mp, dep, args->filetype);
Dave Chinner0cb97762013-08-12 20:50:09 +1000907 tagp = xfs_dir3_data_entry_tag_p(mp, dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200908 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /*
910 * Need to scan fix up the bestfree table.
911 */
912 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200913 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /*
915 * Need to log the data block's header.
916 */
917 if (needlog)
918 xfs_dir2_data_log_header(tp, dbp);
919 xfs_dir2_data_log_entry(tp, dbp, dep);
920 /*
921 * If the bests table needs to be changed, do it.
922 * Log the change unless we've already done that.
923 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100924 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
925 bestsp[use_block] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (!grown)
Dave Chinner24df33b2013-04-12 07:30:21 +1000927 xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200929
Dave Chinner24df33b2013-04-12 07:30:21 +1000930 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200931 highstale, &lfloglow, &lfloghigh);
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /*
934 * Fill in the new leaf entry.
935 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100936 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000937 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
Nathan Scott3d693c62006-03-17 17:28:27 +1100938 be16_to_cpu(*tagp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /*
940 * Log the leaf fields and give up the buffers.
941 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000942 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
943 xfs_dir3_leaf_log_header(tp, lbp);
944 xfs_dir3_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
945 xfs_dir3_leaf_check(mp, lbp);
Dave Chinner33363fe2013-04-03 16:11:22 +1100946 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return 0;
948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950/*
951 * Compact out any stale entries in the leaf.
952 * Log the header and changed leaf entries, if any.
953 */
954void
Dave Chinner24df33b2013-04-12 07:30:21 +1000955xfs_dir3_leaf_compact(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 xfs_da_args_t *args, /* operation arguments */
Dave Chinner24df33b2013-04-12 07:30:21 +1000957 struct xfs_dir3_icleaf_hdr *leafhdr,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000958 struct xfs_buf *bp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 int from; /* source leaf index */
961 xfs_dir2_leaf_t *leaf; /* leaf structure */
962 int loglow; /* first leaf entry to log */
963 int to; /* target leaf index */
Dave Chinner24df33b2013-04-12 07:30:21 +1000964 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Dave Chinner1d9025e2012-06-22 18:50:14 +1000966 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +1000967 if (!leafhdr->stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /*
971 * Compress out the stale entries in place.
972 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000973 ents = xfs_dir3_leaf_ents_p(leaf);
974 for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
975 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 continue;
977 /*
978 * Only actually copy the entries that are different.
979 */
980 if (from > to) {
981 if (loglow == -1)
982 loglow = to;
Dave Chinner24df33b2013-04-12 07:30:21 +1000983 ents[to] = ents[from];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 to++;
986 }
987 /*
988 * Update and log the header, log the leaf entries.
989 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000990 ASSERT(leafhdr->stale == from - to);
991 leafhdr->count -= leafhdr->stale;
992 leafhdr->stale = 0;
993
994 xfs_dir3_leaf_hdr_to_disk(leaf, leafhdr);
995 xfs_dir3_leaf_log_header(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (loglow != -1)
Dave Chinner24df33b2013-04-12 07:30:21 +1000997 xfs_dir3_leaf_log_ents(args->trans, bp, loglow, to - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
1000/*
1001 * Compact the leaf entries, removing stale ones.
1002 * Leave one stale entry behind - the one closest to our
1003 * insertion index - and the caller will shift that one to our insertion
1004 * point later.
1005 * Return new insertion index, where the remaining stale entry is,
1006 * and leaf logging indices.
1007 */
1008void
Dave Chinner24df33b2013-04-12 07:30:21 +10001009xfs_dir3_leaf_compact_x1(
1010 struct xfs_dir3_icleaf_hdr *leafhdr,
1011 struct xfs_dir2_leaf_entry *ents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 int *indexp, /* insertion index */
1013 int *lowstalep, /* out: stale entry before us */
1014 int *highstalep, /* out: stale entry after us */
1015 int *lowlogp, /* out: low log index */
1016 int *highlogp) /* out: high log index */
1017{
1018 int from; /* source copy index */
1019 int highstale; /* stale entry at/after index */
1020 int index; /* insertion index */
1021 int keepstale; /* source index of kept stale */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 int lowstale; /* stale entry before index */
1023 int newindex=0; /* new insertion index */
1024 int to; /* destination copy index */
1025
Dave Chinner24df33b2013-04-12 07:30:21 +10001026 ASSERT(leafhdr->stale > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 index = *indexp;
Christoph Hellwiga230a1d2011-07-13 13:43:48 +02001028
Dave Chinner24df33b2013-04-12 07:30:21 +10001029 xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
Christoph Hellwiga230a1d2011-07-13 13:43:48 +02001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /*
1032 * Pick the better of lowstale and highstale.
1033 */
1034 if (lowstale >= 0 &&
Dave Chinner24df33b2013-04-12 07:30:21 +10001035 (highstale == leafhdr->count ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 index - lowstale <= highstale - index))
1037 keepstale = lowstale;
1038 else
1039 keepstale = highstale;
1040 /*
1041 * Copy the entries in place, removing all the stale entries
1042 * except keepstale.
1043 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001044 for (from = to = 0; from < leafhdr->count; from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /*
1046 * Notice the new value of index.
1047 */
1048 if (index == from)
1049 newindex = to;
1050 if (from != keepstale &&
Dave Chinner24df33b2013-04-12 07:30:21 +10001051 ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (from == to)
1053 *lowlogp = to;
1054 continue;
1055 }
1056 /*
1057 * Record the new keepstale value for the insertion.
1058 */
1059 if (from == keepstale)
1060 lowstale = highstale = to;
1061 /*
1062 * Copy only the entries that have moved.
1063 */
1064 if (from > to)
Dave Chinner24df33b2013-04-12 07:30:21 +10001065 ents[to] = ents[from];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 to++;
1067 }
1068 ASSERT(from > to);
1069 /*
1070 * If the insertion point was past the last entry,
1071 * set the new insertion point accordingly.
1072 */
1073 if (index == from)
1074 newindex = to;
1075 *indexp = newindex;
1076 /*
1077 * Adjust the leaf header values.
1078 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001079 leafhdr->count -= from - to;
1080 leafhdr->stale = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /*
1082 * Remember the low/high stale value only in the "right"
1083 * direction.
1084 */
1085 if (lowstale >= newindex)
1086 lowstale = -1;
1087 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001088 highstale = leafhdr->count;
1089 *highlogp = leafhdr->count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 *lowstalep = lowstale;
1091 *highstalep = highstale;
1092}
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094/*
1095 * Log the bests entries indicated from a leaf1 block.
1096 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001097static void
Dave Chinner24df33b2013-04-12 07:30:21 +10001098xfs_dir3_leaf_log_bests(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001100 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 int first, /* first entry to log */
1102 int last) /* last entry to log */
1103{
Nathan Scott68b3a102006-03-17 17:27:19 +11001104 __be16 *firstb; /* pointer to first entry */
1105 __be16 *lastb; /* pointer to last entry */
Dave Chinner24df33b2013-04-12 07:30:21 +10001106 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1108
Dave Chinner24df33b2013-04-12 07:30:21 +10001109 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1110 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1111
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001112 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1113 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1114 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001115 xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1117}
1118
1119/*
1120 * Log the leaf entries indicated from a leaf1 or leafn block.
1121 */
1122void
Dave Chinner24df33b2013-04-12 07:30:21 +10001123xfs_dir3_leaf_log_ents(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001125 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 int first, /* first entry to log */
1127 int last) /* last entry to log */
1128{
1129 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1130 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
Dave Chinner24df33b2013-04-12 07:30:21 +10001131 struct xfs_dir2_leaf *leaf = bp->b_addr;
1132 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001134 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001135 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1136 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1137 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1138
1139 ents = xfs_dir3_leaf_ents_p(leaf);
1140 firstlep = &ents[first];
1141 lastlep = &ents[last];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001142 xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1144}
1145
1146/*
1147 * Log the header of the leaf1 or leafn block.
1148 */
1149void
Dave Chinner24df33b2013-04-12 07:30:21 +10001150xfs_dir3_leaf_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001151 struct xfs_trans *tp,
1152 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Dave Chinner24df33b2013-04-12 07:30:21 +10001154 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001156 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001157 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1158 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1159 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1160
Dave Chinner1d9025e2012-06-22 18:50:14 +10001161 xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
Dave Chinner24df33b2013-04-12 07:30:21 +10001162 xfs_dir3_leaf_hdr_size(leaf) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165/*
1166 * Log the tail of the leaf1 block.
1167 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001168STATIC void
Dave Chinner24df33b2013-04-12 07:30:21 +10001169xfs_dir3_leaf_log_tail(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001170 struct xfs_trans *tp,
1171 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Dave Chinner24df33b2013-04-12 07:30:21 +10001173 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001175 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Dave Chinner24df33b2013-04-12 07:30:21 +10001177 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1178 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1179 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1180 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1181
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001182 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001183 xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 (uint)(mp->m_dirblksize - 1));
1185}
1186
1187/*
1188 * Look up the entry referred to by args in the leaf format directory.
1189 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1190 * is also used by the node-format code.
1191 */
1192int
1193xfs_dir2_leaf_lookup(
1194 xfs_da_args_t *args) /* operation arguments */
1195{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001196 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 xfs_dir2_data_entry_t *dep; /* data block entry */
1198 xfs_inode_t *dp; /* incore directory inode */
1199 int error; /* error return code */
1200 int index; /* found entry index */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001201 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 xfs_dir2_leaf_t *leaf; /* leaf structure */
1203 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1204 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10001205 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001207 trace_xfs_dir2_leaf_lookup(args);
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /*
1210 * Look up name in the leaf block, returning both buffers and index.
1211 */
1212 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1213 return error;
1214 }
1215 tp = args->trans;
1216 dp = args->dp;
Dave Chinner24df33b2013-04-12 07:30:21 +10001217 xfs_dir3_leaf_check(dp->i_mount, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001218 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001219 ents = xfs_dir3_leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 /*
1221 * Get to the leaf entry and contained data entry address.
1222 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001223 lep = &ents[index];
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /*
1226 * Point to the data entry.
1227 */
1228 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001229 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001230 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +10001232 * Return the found inode number & CI name if appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001234 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner1c55cec2013-08-12 20:50:10 +10001235 args->filetype = xfs_dir3_dirent_get_ftype(dp->i_mount, dep);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001236 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001237 xfs_trans_brelse(tp, dbp);
1238 xfs_trans_brelse(tp, lbp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001239 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242/*
1243 * Look up name/hash in the leaf block.
1244 * Fill in indexp with the found index, and dbpp with the data buffer.
1245 * If not found dbpp will be NULL, and ENOENT comes back.
1246 * lbpp will always be filled in with the leaf buffer unless there's an error.
1247 */
1248static int /* error */
1249xfs_dir2_leaf_lookup_int(
1250 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001251 struct xfs_buf **lbpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 int *indexp, /* out: index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001253 struct xfs_buf **dbpp) /* out: data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001255 xfs_dir2_db_t curdb = -1; /* current data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001256 struct xfs_buf *dbp = NULL; /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 xfs_dir2_data_entry_t *dep; /* data entry */
1258 xfs_inode_t *dp; /* incore directory inode */
1259 int error; /* error return code */
1260 int index; /* index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001261 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1263 xfs_dir2_leaf_t *leaf; /* leaf structure */
1264 xfs_mount_t *mp; /* filesystem mount point */
1265 xfs_dir2_db_t newdb; /* new data block number */
1266 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001267 xfs_dir2_db_t cidb = -1; /* case match data block no. */
Barry Naujok5163f952008-05-21 16:41:01 +10001268 enum xfs_dacmp cmp; /* name compare result */
Dave Chinner24df33b2013-04-12 07:30:21 +10001269 struct xfs_dir2_leaf_entry *ents;
1270 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 dp = args->dp;
1273 tp = args->trans;
1274 mp = dp->i_mount;
Dave Chinnere6f76672012-11-12 22:54:15 +11001275
Dave Chinner24df33b2013-04-12 07:30:21 +10001276 error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001277 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 *lbpp = lbp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001281 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001282 xfs_dir3_leaf_check(mp, lbp);
1283 ents = xfs_dir3_leaf_ents_p(leaf);
1284 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /*
1287 * Look for the first leaf entry with our hash value.
1288 */
1289 index = xfs_dir2_leaf_search_hash(args, lbp);
1290 /*
1291 * Loop over all the entries with the right hash value
1292 * looking to match the name.
1293 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001294 for (lep = &ents[index];
1295 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
1296 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /*
1298 * Skip over stale leaf entries.
1299 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001300 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 continue;
1302 /*
1303 * Get the new data block number.
1304 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001305 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /*
1307 * If it's not the same as the old data block number,
1308 * need to pitch the old one and read the new one.
1309 */
1310 if (newdb != curdb) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001311 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001312 xfs_trans_brelse(tp, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001313 error = xfs_dir3_data_read(tp, dp,
Dave Chinnere4813572012-11-12 22:54:14 +11001314 xfs_dir2_db_to_da(mp, newdb),
1315 -1, &dbp);
Barry Naujok5163f952008-05-21 16:41:01 +10001316 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001317 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return error;
1319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 curdb = newdb;
1321 }
1322 /*
1323 * Point to the data entry.
1324 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001325 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
Barry Naujok5163f952008-05-21 16:41:01 +10001326 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /*
Barry Naujok5163f952008-05-21 16:41:01 +10001328 * Compare name and if it's an exact match, return the index
1329 * and buffer. If it's the first case-insensitive match, store
1330 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 */
Barry Naujok5163f952008-05-21 16:41:01 +10001332 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1333 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1334 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 *indexp = index;
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001336 /* case exact match: return the current buffer. */
Barry Naujok5163f952008-05-21 16:41:01 +10001337 if (cmp == XFS_CMP_EXACT) {
Barry Naujok5163f952008-05-21 16:41:01 +10001338 *dbpp = dbp;
1339 return 0;
1340 }
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001341 cidb = curdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343 }
Barry Naujok6a178102008-05-21 16:42:05 +10001344 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +10001345 /*
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001346 * Here, we can only be doing a lookup (not a rename or remove).
1347 * If a case-insensitive match was found earlier, re-read the
1348 * appropriate data block if required and return it.
Barry Naujok5163f952008-05-21 16:41:01 +10001349 */
1350 if (args->cmpresult == XFS_CMP_CASE) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001351 ASSERT(cidb != -1);
1352 if (cidb != curdb) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001353 xfs_trans_brelse(tp, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001354 error = xfs_dir3_data_read(tp, dp,
Dave Chinnere4813572012-11-12 22:54:14 +11001355 xfs_dir2_db_to_da(mp, cidb),
1356 -1, &dbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001357 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001358 xfs_trans_brelse(tp, lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001359 return error;
1360 }
1361 }
1362 *dbpp = dbp;
Barry Naujok5163f952008-05-21 16:41:01 +10001363 return 0;
1364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 /*
1366 * No match found, return ENOENT.
1367 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001368 ASSERT(cidb == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001370 xfs_trans_brelse(tp, dbp);
1371 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return XFS_ERROR(ENOENT);
1373}
1374
1375/*
1376 * Remove an entry from a leaf format directory.
1377 */
1378int /* error */
1379xfs_dir2_leaf_removename(
1380 xfs_da_args_t *args) /* operation arguments */
1381{
Nathan Scott68b3a102006-03-17 17:27:19 +11001382 __be16 *bestsp; /* leaf block best freespace */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001383 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001385 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 xfs_dir2_data_entry_t *dep; /* data entry structure */
1387 xfs_inode_t *dp; /* incore directory inode */
1388 int error; /* error return code */
1389 xfs_dir2_db_t i; /* temporary data block # */
1390 int index; /* index into leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001391 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 xfs_dir2_leaf_t *leaf; /* leaf structure */
1393 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1394 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1395 xfs_mount_t *mp; /* filesystem mount point */
1396 int needlog; /* need to log data header */
1397 int needscan; /* need to rescan data frees */
1398 xfs_dir2_data_off_t oldbest; /* old value of best free */
1399 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001400 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001401 struct xfs_dir2_leaf_entry *ents;
1402 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001404 trace_xfs_dir2_leaf_removename(args);
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 /*
1407 * Lookup the leaf entry, get the leaf and data blocks read in.
1408 */
1409 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1410 return error;
1411 }
1412 dp = args->dp;
1413 tp = args->trans;
1414 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001415 leaf = lbp->b_addr;
1416 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001417 xfs_dir3_data_check(dp, dbp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001418 bf = xfs_dir3_data_bestfree_p(hdr);
1419 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1420 ents = xfs_dir3_leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 /*
1422 * Point to the leaf entry, use that to point to the data entry.
1423 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001424 lep = &ents[index];
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001425 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001427 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 needscan = needlog = 0;
Dave Chinner33363fe2013-04-03 16:11:22 +11001429 oldbest = be16_to_cpu(bf[0].length);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001430 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1431 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott68b3a102006-03-17 17:27:19 +11001432 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /*
1434 * Mark the former data entry unused.
1435 */
1436 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001437 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Dave Chinner0cb97762013-08-12 20:50:09 +10001438 xfs_dir3_data_entsize(mp, dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 /*
1440 * We just mark the leaf entry stale by putting a null in it.
1441 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001442 leafhdr.stale++;
1443 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
1444 xfs_dir3_leaf_log_header(tp, lbp);
1445
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001446 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinner24df33b2013-04-12 07:30:21 +10001447 xfs_dir3_leaf_log_ents(tp, lbp, index, index);
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 /*
1450 * Scan the freespace in the data block again if necessary,
1451 * log the data block header if necessary.
1452 */
1453 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001454 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (needlog)
1456 xfs_dir2_data_log_header(tp, dbp);
1457 /*
1458 * If the longest freespace in the data block has changed,
1459 * put the new value in the bests table and log that.
1460 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001461 if (be16_to_cpu(bf[0].length) != oldbest) {
1462 bestsp[db] = bf[0].length;
Dave Chinner24df33b2013-04-12 07:30:21 +10001463 xfs_dir3_leaf_log_bests(tp, lbp, db, db);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
Dave Chinner33363fe2013-04-03 16:11:22 +11001465 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 /*
1467 * If the data block is now empty then get rid of the data block.
1468 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001469 if (be16_to_cpu(bf[0].length) ==
1470 mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 ASSERT(db != mp->m_dirdatablk);
1472 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1473 /*
1474 * Nope, can't get rid of it because it caused
1475 * allocation of a bmap btree block to do so.
1476 * Just go on, returning success, leaving the
1477 * empty block in place.
1478 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001479 if (error == ENOSPC && args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 error = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +10001481 xfs_dir3_leaf_check(mp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return error;
1483 }
1484 dbp = NULL;
1485 /*
1486 * If this is the last data block then compact the
1487 * bests table by getting rid of entries.
1488 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001489 if (db == be32_to_cpu(ltp->bestcount) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 /*
1491 * Look for the last active entry (i).
1492 */
1493 for (i = db - 1; i > 0; i--) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001494 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 break;
1496 }
1497 /*
1498 * Copy the table down so inactive entries at the
1499 * end are removed.
1500 */
1501 memmove(&bestsp[db - i], bestsp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001502 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001503 be32_add_cpu(&ltp->bestcount, -(db - i));
Dave Chinner24df33b2013-04-12 07:30:21 +10001504 xfs_dir3_leaf_log_tail(tp, lbp);
1505 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 } else
Nathan Scott68b3a102006-03-17 17:27:19 +11001507 bestsp[db] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509 /*
1510 * If the data block was not the first one, drop it.
1511 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001512 else if (db != mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 dbp = NULL;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001514
Dave Chinner24df33b2013-04-12 07:30:21 +10001515 xfs_dir3_leaf_check(mp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 /*
1517 * See if we can convert to block form.
1518 */
1519 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1520}
1521
1522/*
1523 * Replace the inode number in a leaf format directory entry.
1524 */
1525int /* error */
1526xfs_dir2_leaf_replace(
1527 xfs_da_args_t *args) /* operation arguments */
1528{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001529 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 xfs_dir2_data_entry_t *dep; /* data block entry */
1531 xfs_inode_t *dp; /* incore directory inode */
1532 int error; /* error return code */
1533 int index; /* index of leaf entry */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001534 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 xfs_dir2_leaf_t *leaf; /* leaf structure */
1536 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1537 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10001538 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001540 trace_xfs_dir2_leaf_replace(args);
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /*
1543 * Look up the entry.
1544 */
1545 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1546 return error;
1547 }
1548 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001549 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001550 ents = xfs_dir3_leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /*
1552 * Point to the leaf entry, get data address from it.
1553 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001554 lep = &ents[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 /*
1556 * Point to the data entry.
1557 */
1558 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001559 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001560 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001561 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 /*
1563 * Put the new inode number in, log it.
1564 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001565 dep->inumber = cpu_to_be64(args->inumber);
Dave Chinner1c55cec2013-08-12 20:50:10 +10001566 xfs_dir3_dirent_put_ftype(dp->i_mount, dep, args->filetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 tp = args->trans;
1568 xfs_dir2_data_log_entry(tp, dbp, dep);
Dave Chinner24df33b2013-04-12 07:30:21 +10001569 xfs_dir3_leaf_check(dp->i_mount, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001570 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return 0;
1572}
1573
1574/*
1575 * Return index in the leaf block (lbp) which is either the first
1576 * one with this hash value, or if there are none, the insert point
1577 * for that hash value.
1578 */
1579int /* index value */
1580xfs_dir2_leaf_search_hash(
1581 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001582 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
1584 xfs_dahash_t hash=0; /* hash from this entry */
1585 xfs_dahash_t hashwant; /* hash value looking for */
1586 int high; /* high leaf index */
1587 int low; /* low leaf index */
1588 xfs_dir2_leaf_t *leaf; /* leaf structure */
1589 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1590 int mid=0; /* current leaf index */
Dave Chinner24df33b2013-04-12 07:30:21 +10001591 struct xfs_dir2_leaf_entry *ents;
1592 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Dave Chinner1d9025e2012-06-22 18:50:14 +10001594 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001595 ents = xfs_dir3_leaf_ents_p(leaf);
1596 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 /*
1599 * Note, the table cannot be empty, so we have to go through the loop.
1600 * Binary search the leaf entries looking for our hash value.
1601 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001602 for (lep = ents, low = 0, high = leafhdr.count - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 hashwant = args->hashval;
1604 low <= high; ) {
1605 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001606 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 break;
1608 if (hash < hashwant)
1609 low = mid + 1;
1610 else
1611 high = mid - 1;
1612 }
1613 /*
1614 * Found one, back up through all the equal hash values.
1615 */
1616 if (hash == hashwant) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001617 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 mid--;
1619 }
1620 }
1621 /*
1622 * Need to point to an entry higher than ours.
1623 */
1624 else if (hash < hashwant)
1625 mid++;
1626 return mid;
1627}
1628
1629/*
1630 * Trim off a trailing data block. We know it's empty since the leaf
1631 * freespace table says so.
1632 */
1633int /* error */
1634xfs_dir2_leaf_trim_data(
1635 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001636 struct xfs_buf *lbp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 xfs_dir2_db_t db) /* data block number */
1638{
Nathan Scott68b3a102006-03-17 17:27:19 +11001639 __be16 *bestsp; /* leaf bests table */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001640 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 xfs_inode_t *dp; /* incore directory inode */
1642 int error; /* error return value */
1643 xfs_dir2_leaf_t *leaf; /* leaf structure */
1644 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1645 xfs_mount_t *mp; /* filesystem mount point */
1646 xfs_trans_t *tp; /* transaction pointer */
1647
1648 dp = args->dp;
1649 mp = dp->i_mount;
1650 tp = args->trans;
1651 /*
1652 * Read the offending data block. We need its buffer.
1653 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001654 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001655 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Dave Chinner1d9025e2012-06-22 18:50:14 +10001658 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001659 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001660
1661#ifdef DEBUG
1662{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001663 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001664 struct xfs_dir2_data_free *bf = xfs_dir3_data_bestfree_p(hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001665
Dave Chinner33363fe2013-04-03 16:11:22 +11001666 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1667 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1668 ASSERT(be16_to_cpu(bf[0].length) ==
1669 mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001670 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001671}
1672#endif
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /*
1675 * Get rid of the data block.
1676 */
1677 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1678 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001679 xfs_trans_brelse(tp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return error;
1681 }
1682 /*
1683 * Eliminate the last bests entry from the table.
1684 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001685 bestsp = xfs_dir2_leaf_bests_p(ltp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001686 be32_add_cpu(&ltp->bestcount, -1);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001687 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
Dave Chinner24df33b2013-04-12 07:30:21 +10001688 xfs_dir3_leaf_log_tail(tp, lbp);
1689 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return 0;
1691}
1692
Christoph Hellwig22823962011-07-08 14:35:53 +02001693static inline size_t
Dave Chinner24df33b2013-04-12 07:30:21 +10001694xfs_dir3_leaf_size(
1695 struct xfs_dir3_icleaf_hdr *hdr,
Christoph Hellwig22823962011-07-08 14:35:53 +02001696 int counts)
1697{
Dave Chinner24df33b2013-04-12 07:30:21 +10001698 int entries;
1699 int hdrsize;
Christoph Hellwig22823962011-07-08 14:35:53 +02001700
Dave Chinner24df33b2013-04-12 07:30:21 +10001701 entries = hdr->count - hdr->stale;
1702 if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
1703 hdr->magic == XFS_DIR2_LEAFN_MAGIC)
1704 hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
1705 else
1706 hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
1707
1708 return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
1709 + counts * sizeof(xfs_dir2_data_off_t)
1710 + sizeof(xfs_dir2_leaf_tail_t);
Christoph Hellwig22823962011-07-08 14:35:53 +02001711}
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713/*
1714 * Convert node form directory to leaf form directory.
1715 * The root of the node form dir needs to already be a LEAFN block.
1716 * Just return if we can't do anything.
1717 */
1718int /* error */
1719xfs_dir2_node_to_leaf(
1720 xfs_da_state_t *state) /* directory operation state */
1721{
1722 xfs_da_args_t *args; /* operation arguments */
1723 xfs_inode_t *dp; /* incore directory inode */
1724 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001725 struct xfs_buf *fbp; /* buffer for freespace block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 xfs_fileoff_t fo; /* freespace file offset */
1727 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001728 struct xfs_buf *lbp; /* buffer for leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1730 xfs_dir2_leaf_t *leaf; /* leaf structure */
1731 xfs_mount_t *mp; /* filesystem mount point */
1732 int rval; /* successful free trim? */
1733 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10001734 struct xfs_dir3_icleaf_hdr leafhdr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001735 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 /*
1738 * There's more than a leaf level in the btree, so there must
1739 * be multiple leafn blocks. Give up.
1740 */
1741 if (state->path.active > 1)
1742 return 0;
1743 args = state->args;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001744
1745 trace_xfs_dir2_node_to_leaf(args);
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 mp = state->mp;
1748 dp = args->dp;
1749 tp = args->trans;
1750 /*
1751 * Get the last offset in the file.
1752 */
1753 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1754 return error;
1755 }
1756 fo -= mp->m_dirblkfsbs;
1757 /*
1758 * If there are freespace blocks other than the first one,
1759 * take this opportunity to remove trailing empty freespace blocks
1760 * that may have been left behind during no-space-reservation
1761 * operations.
1762 */
1763 while (fo > mp->m_dirfreeblk) {
1764 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1765 return error;
1766 }
1767 if (rval)
1768 fo -= mp->m_dirblkfsbs;
1769 else
1770 return 0;
1771 }
1772 /*
1773 * Now find the block just before the freespace block.
1774 */
1775 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1776 return error;
1777 }
1778 /*
1779 * If it's not the single leaf block, give up.
1780 */
1781 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1782 return 0;
1783 lbp = state->path.blk[0].bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001784 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001785 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1786
1787 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
1788 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /*
1791 * Read the freespace block.
1792 */
Dave Chinner20252072012-11-12 22:54:13 +11001793 error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001794 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001796 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001797 xfs_dir3_free_hdr_from_disk(&freehdr, free);
1798
1799 ASSERT(!freehdr.firstdb);
Christoph Hellwig22823962011-07-08 14:35:53 +02001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /*
1802 * Now see if the leafn and free data will fit in a leaf1.
1803 * If not, release the buffer and give up.
1804 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001805 if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > mp->m_dirblksize) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001806 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return 0;
1808 }
Christoph Hellwig22823962011-07-08 14:35:53 +02001809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 /*
1811 * If the leaf has any stale entries in it, compress them out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001813 if (leafhdr.stale)
1814 xfs_dir3_leaf_compact(args, &leafhdr, lbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001815
Dave Chinner24df33b2013-04-12 07:30:21 +10001816 lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +11001817 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
Dave Chinner24df33b2013-04-12 07:30:21 +10001818 leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
1819 ? XFS_DIR2_LEAF1_MAGIC
1820 : XFS_DIR3_LEAF1_MAGIC;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /*
1823 * Set up the leaf tail from the freespace block.
1824 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001825 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001826 ltp->bestcount = cpu_to_be32(freehdr.nvalid);
Dave Chinner24df33b2013-04-12 07:30:21 +10001827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /*
1829 * Set up the leaf bests table.
1830 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001831 memcpy(xfs_dir2_leaf_bests_p(ltp), xfs_dir3_free_bests_p(mp, free),
1832 freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
Dave Chinner24df33b2013-04-12 07:30:21 +10001833
1834 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
1835 xfs_dir3_leaf_log_header(tp, lbp);
1836 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1837 xfs_dir3_leaf_log_tail(tp, lbp);
1838 xfs_dir3_leaf_check(mp, lbp);
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /*
1841 * Get rid of the freespace block.
1842 */
1843 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1844 if (error) {
1845 /*
1846 * This can't fail here because it can only happen when
1847 * punching out the middle of an extent, and this is an
1848 * isolated block.
1849 */
1850 ASSERT(error != ENOSPC);
1851 return error;
1852 }
1853 fbp = NULL;
1854 /*
1855 * Now see if we can convert the single-leaf directory
1856 * down to a block form directory.
1857 * This routine always kills the dabuf for the leaf, so
1858 * eliminate it from the path.
1859 */
1860 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1861 state->path.blk[0].bp = NULL;
1862 return error;
1863}