blob: 2da86a394bcf05c4091db70b3651a4c52158bc81 [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 Chinnerf5f3d9b2013-04-03 16:11:20 +11003 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110025#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110028#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_inode_item.h"
Dave Chinnerf3508bc2013-07-10 07:04:00 +100030#include "xfs_bmap.h"
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110031#include "xfs_buf_item.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100032#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020033#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000035#include "xfs_trace.h"
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110036#include "xfs_cksum.h"
Brian Fostera45086e2015-10-12 15:59:25 +110037#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * Local function prototypes.
41 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100042static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp,
43 int first, int last);
44static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp);
45static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 int *entno);
47static int xfs_dir2_block_sort(const void *a, const void *b);
48
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100049static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
50
51/*
52 * One-time startup routine called from xfs_init().
53 */
54void
55xfs_dir_startup(void)
56{
Dave Chinner4a24cb72010-01-20 10:48:05 +110057 xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1);
58 xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100059}
60
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080061static xfs_failaddr_t
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110062xfs_dir3_block_verify(
Dave Chinner82025d72012-11-12 22:54:12 +110063 struct xfs_buf *bp)
64{
65 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110066 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Dave Chinner82025d72012-11-12 22:54:12 +110067
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110068 if (xfs_sb_version_hascrc(&mp->m_sb)) {
69 if (hdr3->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080070 return __this_address;
Eric Sandeence748ea2015-07-29 11:53:31 +100071 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080072 return __this_address;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110073 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080074 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +110075 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080076 return __this_address;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110077 } else {
78 if (hdr3->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080079 return __this_address;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110080 }
Darrick J. Wong9101d372018-01-08 10:51:01 -080081 return __xfs_dir3_data_check(NULL, bp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110082}
Dave Chinner82025d72012-11-12 22:54:12 +110083
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110084static void
85xfs_dir3_block_read_verify(
86 struct xfs_buf *bp)
87{
88 struct xfs_mount *mp = bp->b_target->bt_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -080089 xfs_failaddr_t fa;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110090
Eric Sandeence5028c2014-02-27 15:23:10 +110091 if (xfs_sb_version_hascrc(&mp->m_sb) &&
92 !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -080093 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
94 else {
95 fa = xfs_dir3_block_verify(bp);
96 if (fa)
97 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
98 }
Dave Chinner612cfbf2012-11-14 17:52:32 +110099}
Dave Chinner82025d72012-11-12 22:54:12 +1100100
Dave Chinner612cfbf2012-11-14 17:52:32 +1100101static void
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100102xfs_dir3_block_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100103 struct xfs_buf *bp)
104{
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100105 struct xfs_mount *mp = bp->b_target->bt_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800106 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100107 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800108 xfs_failaddr_t fa;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100109
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800110 fa = xfs_dir3_block_verify(bp);
111 if (fa) {
112 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100113 return;
114 }
115
116 if (!xfs_sb_version_hascrc(&mp->m_sb))
117 return;
118
119 if (bip)
120 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
121
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100122 xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100123}
124
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100125const struct xfs_buf_ops xfs_dir3_block_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100126 .name = "xfs_dir3_block",
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100127 .verify_read = xfs_dir3_block_read_verify,
128 .verify_write = xfs_dir3_block_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800129 .verify_struct = xfs_dir3_block_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100130};
Dave Chinner82025d72012-11-12 22:54:12 +1100131
Dave Chinner4a8af272013-08-12 20:49:36 +1000132int
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100133xfs_dir3_block_read(
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100134 struct xfs_trans *tp,
135 struct xfs_inode *dp,
136 struct xfs_buf **bpp)
137{
138 struct xfs_mount *mp = dp->i_mount;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100139 int err;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100140
Dave Chinner7dda6e82014-06-06 15:11:18 +1000141 err = xfs_da_read_buf(tp, dp, mp->m_dir_geo->datablk, -1, bpp,
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100142 XFS_DATA_FORK, &xfs_dir3_block_buf_ops);
Darrick J. Wongcd87d862017-07-07 18:55:17 -0700143 if (!err && tp && *bpp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100144 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_BLOCK_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100145 return err;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100146}
147
148static void
149xfs_dir3_block_init(
150 struct xfs_mount *mp,
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100151 struct xfs_trans *tp,
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100152 struct xfs_buf *bp,
153 struct xfs_inode *dp)
154{
155 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
156
157 bp->b_ops = &xfs_dir3_block_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100158 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_BLOCK_BUF);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100159
160 if (xfs_sb_version_hascrc(&mp->m_sb)) {
161 memset(hdr3, 0, sizeof(*hdr3));
162 hdr3->magic = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
163 hdr3->blkno = cpu_to_be64(bp->b_bn);
164 hdr3->owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000165 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100166 return;
167
168 }
169 hdr3->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100170}
171
172static void
173xfs_dir2_block_need_space(
Dave Chinner2ca98772013-10-29 22:11:49 +1100174 struct xfs_inode *dp,
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100175 struct xfs_dir2_data_hdr *hdr,
176 struct xfs_dir2_block_tail *btp,
177 struct xfs_dir2_leaf_entry *blp,
178 __be16 **tagpp,
179 struct xfs_dir2_data_unused **dupp,
180 struct xfs_dir2_data_unused **enddupp,
181 int *compact,
182 int len)
183{
184 struct xfs_dir2_data_free *bf;
185 __be16 *tagp = NULL;
186 struct xfs_dir2_data_unused *dup = NULL;
187 struct xfs_dir2_data_unused *enddup = NULL;
188
189 *compact = 0;
Dave Chinner2ca98772013-10-29 22:11:49 +1100190 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100191
192 /*
193 * If there are stale entries we'll use one for the leaf.
194 */
195 if (btp->stale) {
196 if (be16_to_cpu(bf[0].length) >= len) {
197 /*
198 * The biggest entry enough to avoid compaction.
199 */
200 dup = (xfs_dir2_data_unused_t *)
201 ((char *)hdr + be16_to_cpu(bf[0].offset));
202 goto out;
203 }
204
205 /*
206 * Will need to compact to make this work.
207 * Tag just before the first leaf entry.
208 */
209 *compact = 1;
210 tagp = (__be16 *)blp - 1;
211
212 /* Data object just before the first leaf entry. */
213 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
214
215 /*
216 * If it's not free then the data will go where the
217 * leaf data starts now, if it works at all.
218 */
219 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
220 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
221 (uint)sizeof(*blp) < len)
222 dup = NULL;
223 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
224 dup = NULL;
225 else
226 dup = (xfs_dir2_data_unused_t *)blp;
227 goto out;
228 }
229
230 /*
231 * no stale entries, so just use free space.
232 * Tag just before the first leaf entry.
233 */
234 tagp = (__be16 *)blp - 1;
235
236 /* Data object just before the first leaf entry. */
237 enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
238
239 /*
240 * If it's not free then can't do this add without cleaning up:
241 * the space before the first leaf entry needs to be free so it
242 * can be expanded to hold the pointer to the new entry.
243 */
244 if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
245 /*
246 * Check out the biggest freespace and see if it's the same one.
247 */
248 dup = (xfs_dir2_data_unused_t *)
249 ((char *)hdr + be16_to_cpu(bf[0].offset));
250 if (dup != enddup) {
251 /*
252 * Not the same free entry, just check its length.
253 */
254 if (be16_to_cpu(dup->length) < len)
255 dup = NULL;
256 goto out;
257 }
258
259 /*
260 * It is the biggest freespace, can it hold the leaf too?
261 */
262 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
263 /*
264 * Yes, use the second-largest entry instead if it works.
265 */
266 if (be16_to_cpu(bf[1].length) >= len)
267 dup = (xfs_dir2_data_unused_t *)
268 ((char *)hdr + be16_to_cpu(bf[1].offset));
269 else
270 dup = NULL;
271 }
272 }
273out:
274 *tagpp = tagp;
275 *dupp = dup;
276 *enddupp = enddup;
277}
278
279/*
280 * compact the leaf entries.
281 * Leave the highest-numbered stale entry stale.
282 * XXX should be the one closest to mid but mid is not yet computed.
283 */
284static void
285xfs_dir2_block_compact(
Dave Chinnerbc851782014-06-06 15:20:54 +1000286 struct xfs_da_args *args,
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100287 struct xfs_buf *bp,
288 struct xfs_dir2_data_hdr *hdr,
289 struct xfs_dir2_block_tail *btp,
290 struct xfs_dir2_leaf_entry *blp,
291 int *needlog,
292 int *lfloghigh,
293 int *lfloglow)
294{
295 int fromidx; /* source leaf index */
296 int toidx; /* target leaf index */
297 int needscan = 0;
298 int highstale; /* high stale index */
299
300 fromidx = toidx = be32_to_cpu(btp->count) - 1;
301 highstale = *lfloghigh = -1;
302 for (; fromidx >= 0; fromidx--) {
303 if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
304 if (highstale == -1)
305 highstale = toidx;
306 else {
307 if (*lfloghigh == -1)
308 *lfloghigh = toidx;
309 continue;
310 }
311 }
312 if (fromidx < toidx)
313 blp[toidx] = blp[fromidx];
314 toidx--;
315 }
316 *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
317 *lfloghigh -= be32_to_cpu(btp->stale) - 1;
318 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
Dave Chinnerbc851782014-06-06 15:20:54 +1000319 xfs_dir2_data_make_free(args, bp,
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100320 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
321 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
322 needlog, &needscan);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100323 btp->stale = cpu_to_be32(1);
324 /*
325 * If we now need to rebuild the bestfree map, do so.
326 * This needs to happen before the next call to use_free.
327 */
328 if (needscan)
Dave Chinnerbc851782014-06-06 15:20:54 +1000329 xfs_dir2_data_freescan(args->dp, hdr, needlog);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/*
333 * Add an entry to a block directory.
334 */
335int /* error */
336xfs_dir2_block_addname(
337 xfs_da_args_t *args) /* directory op arguments */
338{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200339 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000341 struct xfs_buf *bp; /* buffer for block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 xfs_dir2_block_tail_t *btp; /* block tail */
343 int compact; /* need to compact leaf ents */
344 xfs_dir2_data_entry_t *dep; /* block data entry */
345 xfs_inode_t *dp; /* directory inode */
346 xfs_dir2_data_unused_t *dup; /* block unused entry */
347 int error; /* error return value */
348 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
349 xfs_dahash_t hash; /* hash value of found entry */
350 int high; /* high index for binary srch */
351 int highstale; /* high stale index */
352 int lfloghigh=0; /* last final leaf to log */
353 int lfloglow=0; /* first final leaf to log */
354 int len; /* length of the new entry */
355 int low; /* low index for binary srch */
356 int lowstale; /* low stale index */
357 int mid=0; /* midpoint for binary srch */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 int needlog; /* need to log header */
359 int needscan; /* need to rescan freespace */
Nathan Scott3d693c62006-03-17 17:28:27 +1100360 __be16 *tagp; /* pointer to tag value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 xfs_trans_t *tp; /* transaction structure */
362
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000363 trace_xfs_dir2_block_addname(args);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 dp = args->dp;
366 tp = args->trans;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100367
368 /* Read the (one and only) directory block into bp. */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100369 error = xfs_dir3_block_read(tp, dp, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100370 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return error;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100372
Dave Chinner9d23fc82013-10-29 22:11:48 +1100373 len = dp->d_ops->data_entsize(args->namelen);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /*
376 * Set up pointers to parts of the block.
377 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100378 hdr = bp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000379 btp = xfs_dir2_block_tail_p(args->geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000380 blp = xfs_dir2_block_leaf_p(btp);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /*
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100383 * Find out if we can reuse stale entries or whether we need extra
384 * space for entry and new leaf.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 */
Dave Chinner2ca98772013-10-29 22:11:49 +1100386 xfs_dir2_block_need_space(dp, hdr, btp, blp, &tagp, &dup,
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100387 &enddup, &compact, len);
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /*
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100390 * Done everything we need for a space check now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100392 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000393 xfs_trans_brelse(tp, bp);
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100394 if (!dup)
Dave Chinner24513372014-06-25 14:58:08 +1000395 return -ENOSPC;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100396 return 0;
397 }
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /*
400 * If we don't have space for the new entry & leaf ...
401 */
402 if (!dup) {
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100403 /* Don't have a space reservation: return no-space. */
404 if (args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +1000405 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /*
407 * Convert to the next larger format.
408 * Then add the new entry in that format.
409 */
410 error = xfs_dir2_block_to_leaf(args, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (error)
412 return error;
413 return xfs_dir2_leaf_addname(args);
414 }
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 needlog = needscan = 0;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /*
419 * If need to compact the leaf entries, do it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Eric Sandeen37f13562013-01-10 10:41:48 -0600421 if (compact) {
Dave Chinnerbc851782014-06-06 15:20:54 +1000422 xfs_dir2_block_compact(args, bp, hdr, btp, blp, &needlog,
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100423 &lfloghigh, &lfloglow);
Eric Sandeen37f13562013-01-10 10:41:48 -0600424 /* recalculate blp post-compaction */
425 blp = xfs_dir2_block_leaf_p(btp);
426 } else if (btp->stale) {
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100427 /*
428 * Set leaf logging boundaries to impossible state.
429 * For the no-stale case they're set explicitly.
430 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100431 lfloglow = be32_to_cpu(btp->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 lfloghigh = -1;
433 }
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /*
436 * Find the slot that's first lower than our hash value, -1 if none.
437 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100438 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100440 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 break;
442 if (hash < args->hashval)
443 low = mid + 1;
444 else
445 high = mid - 1;
446 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100447 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 mid--;
449 }
450 /*
451 * No stale entries, will use enddup space to hold new leaf.
452 */
453 if (!btp->stale) {
454 /*
455 * Mark the space needed for the new leaf entry, now in use.
456 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000457 xfs_dir2_data_use_free(args, bp, enddup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 (xfs_dir2_data_aoff_t)
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200459 ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 sizeof(*blp)),
461 (xfs_dir2_data_aoff_t)sizeof(*blp),
462 &needlog, &needscan);
463 /*
464 * Update the tail (entry count).
465 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800466 be32_add_cpu(&btp->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /*
468 * If we now need to rebuild the bestfree map, do so.
469 * This needs to happen before the next call to use_free.
470 */
471 if (needscan) {
Dave Chinner9d23fc82013-10-29 22:11:48 +1100472 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 needscan = 0;
474 }
475 /*
476 * Adjust pointer to the first leaf entry, we're about to move
477 * the table up one to open up space for the new leaf entry.
478 * Then adjust our index to match.
479 */
480 blp--;
481 mid++;
482 if (mid)
483 memmove(blp, &blp[1], mid * sizeof(*blp));
484 lfloglow = 0;
485 lfloghigh = mid;
486 }
487 /*
488 * Use a stale leaf for our new entry.
489 */
490 else {
491 for (lowstale = mid;
492 lowstale >= 0 &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200493 blp[lowstale].address !=
494 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 lowstale--)
496 continue;
497 for (highstale = mid + 1;
Nathan Scotte922fff2006-03-17 17:27:56 +1100498 highstale < be32_to_cpu(btp->count) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200499 blp[highstale].address !=
500 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 (lowstale < 0 || mid - lowstale > highstale - mid);
502 highstale++)
503 continue;
504 /*
505 * Move entries toward the low-numbered stale entry.
506 */
507 if (lowstale >= 0 &&
Nathan Scotte922fff2006-03-17 17:27:56 +1100508 (highstale == be32_to_cpu(btp->count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 mid - lowstale <= highstale - mid)) {
510 if (mid - lowstale)
511 memmove(&blp[lowstale], &blp[lowstale + 1],
512 (mid - lowstale) * sizeof(*blp));
513 lfloglow = MIN(lowstale, lfloglow);
514 lfloghigh = MAX(mid, lfloghigh);
515 }
516 /*
517 * Move entries toward the high-numbered stale entry.
518 */
519 else {
Nathan Scotte922fff2006-03-17 17:27:56 +1100520 ASSERT(highstale < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 mid++;
522 if (highstale - mid)
523 memmove(&blp[mid + 1], &blp[mid],
524 (highstale - mid) * sizeof(*blp));
525 lfloglow = MIN(mid, lfloglow);
526 lfloghigh = MAX(highstale, lfloghigh);
527 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800528 be32_add_cpu(&btp->stale, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530 /*
531 * Point to the new data entry.
532 */
533 dep = (xfs_dir2_data_entry_t *)dup;
534 /*
535 * Fill in the leaf entry.
536 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100537 blp[mid].hashval = cpu_to_be32(args->hashval);
Eric Sandeen25994052014-04-14 19:02:30 +1000538 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200539 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
541 /*
542 * Mark space for the data entry used.
543 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000544 xfs_dir2_data_use_free(args, bp, dup,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200545 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
547 /*
548 * Create the new data entry.
549 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000550 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 dep->namelen = args->namelen;
552 memcpy(dep->name, args->name, args->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100553 dp->d_ops->data_put_ftype(dep, args->filetype);
554 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200555 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /*
557 * Clean up the bestfree array and log the header, tail, and entry.
558 */
559 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +1100560 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +1000562 xfs_dir2_data_log_header(args, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 xfs_dir2_block_log_tail(tp, bp);
Dave Chinnerbc851782014-06-06 15:20:54 +1000564 xfs_dir2_data_log_entry(args, bp, dep);
Dave Chinner33363fe2013-04-03 16:11:22 +1100565 xfs_dir3_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return 0;
567}
568
569/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 * Log leaf entries from the block.
571 */
572static void
573xfs_dir2_block_log_leaf(
574 xfs_trans_t *tp, /* transaction structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000575 struct xfs_buf *bp, /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int first, /* index of first logged leaf */
577 int last) /* index of last logged leaf */
578{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000579 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200580 xfs_dir2_leaf_entry_t *blp;
581 xfs_dir2_block_tail_t *btp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Dave Chinner8f661932014-06-06 15:15:59 +1000583 btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000584 blp = xfs_dir2_block_leaf_p(btp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000585 xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200586 (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589/*
590 * Log the block tail.
591 */
592static void
593xfs_dir2_block_log_tail(
594 xfs_trans_t *tp, /* transaction structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000595 struct xfs_buf *bp) /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000597 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200598 xfs_dir2_block_tail_t *btp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Dave Chinner8f661932014-06-06 15:15:59 +1000600 btp = xfs_dir2_block_tail_p(tp->t_mountp->m_dir_geo, hdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000601 xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200602 (uint)((char *)(btp + 1) - (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605/*
606 * Look up an entry in the block. This is the external routine,
607 * xfs_dir2_block_lookup_int does the real work.
608 */
609int /* error */
610xfs_dir2_block_lookup(
611 xfs_da_args_t *args) /* dir lookup arguments */
612{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200613 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000615 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 xfs_dir2_block_tail_t *btp; /* block tail */
617 xfs_dir2_data_entry_t *dep; /* block data entry */
618 xfs_inode_t *dp; /* incore inode */
619 int ent; /* entry index */
620 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000622 trace_xfs_dir2_block_lookup(args);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /*
625 * Get the buffer, look up the entry.
626 * If not found (ENOENT) then return, have no buffer.
627 */
628 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
629 return error;
630 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000631 hdr = bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100632 xfs_dir3_data_check(dp, bp);
Dave Chinner8f661932014-06-06 15:15:59 +1000633 btp = xfs_dir2_block_tail_p(args->geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000634 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /*
636 * Get the offset from the leaf entry, to point to the data.
637 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200638 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +1000639 xfs_dir2_dataptr_to_off(args->geo,
640 be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +1000642 * Fill in inode number, CI name if appropriate, release the block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000644 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100645 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok384f3ce2008-05-21 16:58:22 +1000646 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000647 xfs_trans_brelse(args->trans, bp);
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000648 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651/*
652 * Internal block lookup routine.
653 */
654static int /* error */
655xfs_dir2_block_lookup_int(
656 xfs_da_args_t *args, /* dir lookup arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000657 struct xfs_buf **bpp, /* returned block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 int *entno) /* returned entry number */
659{
660 xfs_dir2_dataptr_t addr; /* data entry address */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200661 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000663 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 xfs_dir2_block_tail_t *btp; /* block tail */
665 xfs_dir2_data_entry_t *dep; /* block data entry */
666 xfs_inode_t *dp; /* incore inode */
667 int error; /* error return value */
668 xfs_dahash_t hash; /* found hash value */
669 int high; /* binary search high index */
670 int low; /* binary search low index */
671 int mid; /* binary search current idx */
672 xfs_mount_t *mp; /* filesystem mount point */
673 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000674 enum xfs_dacmp cmp; /* comparison result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 dp = args->dp;
677 tp = args->trans;
678 mp = dp->i_mount;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100679
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100680 error = xfs_dir3_block_read(tp, dp, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100681 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return error;
Dave Chinner20f7e9f2012-11-12 22:54:11 +1100683
Dave Chinner1d9025e2012-06-22 18:50:14 +1000684 hdr = bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100685 xfs_dir3_data_check(dp, bp);
Dave Chinner8f661932014-06-06 15:15:59 +1000686 btp = xfs_dir2_block_tail_p(args->geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000687 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /*
689 * Loop doing a binary search for our hash value.
690 * Find our entry, ENOENT if it's not there.
691 */
Nathan Scotte922fff2006-03-17 17:27:56 +1100692 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 ASSERT(low <= high);
694 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100695 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 break;
697 if (hash < args->hashval)
698 low = mid + 1;
699 else
700 high = mid - 1;
701 if (low > high) {
Barry Naujok6a178102008-05-21 16:42:05 +1000702 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000703 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +1000704 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 }
707 /*
708 * Back up to the first one with the right hash value.
709 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100710 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 mid--;
712 }
713 /*
714 * Now loop forward through all the entries with the
715 * right hash value looking for our name.
716 */
717 do {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100718 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 continue;
720 /*
721 * Get pointer to the entry from the leaf.
722 */
723 dep = (xfs_dir2_data_entry_t *)
Dave Chinner30028032014-06-06 15:08:18 +1000724 ((char *)hdr + xfs_dir2_dataptr_to_off(args->geo, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000726 * Compare name and if it's an exact match, return the index
727 * and buffer. If it's the first case-insensitive match, store
728 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
Barry Naujok5163f952008-05-21 16:41:01 +1000730 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
731 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
732 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 *bpp = bp;
734 *entno = mid;
Barry Naujok5163f952008-05-21 16:41:01 +1000735 if (cmp == XFS_CMP_EXACT)
736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
Barry Naujok5163f952008-05-21 16:41:01 +1000738 } while (++mid < be32_to_cpu(btp->count) &&
739 be32_to_cpu(blp[mid].hashval) == hash);
740
Barry Naujok6a178102008-05-21 16:42:05 +1000741 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +1000742 /*
743 * Here, we can only be doing a lookup (not a rename or replace).
744 * If a case-insensitive match was found earlier, return success.
745 */
746 if (args->cmpresult == XFS_CMP_CASE)
747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
749 * No match, release the buffer and return ENOENT.
750 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000751 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +1000752 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755/*
756 * Remove an entry from a block format directory.
757 * If that makes the block small enough to fit in shortform, transform it.
758 */
759int /* error */
760xfs_dir2_block_removename(
761 xfs_da_args_t *args) /* directory operation args */
762{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200763 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000765 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 xfs_dir2_block_tail_t *btp; /* block tail */
767 xfs_dir2_data_entry_t *dep; /* block data entry */
768 xfs_inode_t *dp; /* incore inode */
769 int ent; /* block leaf entry index */
770 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 int needlog; /* need to log block header */
772 int needscan; /* need to fixup bestfree */
773 xfs_dir2_sf_hdr_t sfh; /* shortform header */
774 int size; /* shortform size */
775 xfs_trans_t *tp; /* transaction pointer */
776
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000777 trace_xfs_dir2_block_removename(args);
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /*
780 * Look up the entry in the block. Gets the buffer and entry index.
781 * It will always be there, the vnodeops level does a lookup first.
782 */
783 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
784 return error;
785 }
786 dp = args->dp;
787 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000788 hdr = bp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000789 btp = xfs_dir2_block_tail_p(args->geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000790 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /*
792 * Point to the data entry using the leaf entry.
793 */
Dave Chinner30028032014-06-06 15:08:18 +1000794 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
795 xfs_dir2_dataptr_to_off(args->geo,
796 be32_to_cpu(blp[ent].address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /*
798 * Mark the data entry's space free.
799 */
800 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +1000801 xfs_dir2_data_make_free(args, bp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200802 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Dave Chinner9d23fc82013-10-29 22:11:48 +1100803 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /*
805 * Fix up the block tail.
806 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800807 be32_add_cpu(&btp->stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 xfs_dir2_block_log_tail(tp, bp);
809 /*
810 * Remove the leaf entry by marking it stale.
811 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100812 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
814 /*
815 * Fix up bestfree, log the header if necessary.
816 */
817 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +1100818 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +1000820 xfs_dir2_data_log_header(args, bp);
Dave Chinner33363fe2013-04-03 16:11:22 +1100821 xfs_dir3_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /*
823 * See if the size as a shortform is good enough.
824 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200825 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000826 if (size > XFS_IFORK_DSIZE(dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /*
830 * If it works, do the conversion.
831 */
832 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
833}
834
835/*
836 * Replace an entry in a V2 block directory.
837 * Change the inode number to the new value.
838 */
839int /* error */
840xfs_dir2_block_replace(
841 xfs_da_args_t *args) /* directory operation args */
842{
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200843 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000845 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 xfs_dir2_block_tail_t *btp; /* block tail */
847 xfs_dir2_data_entry_t *dep; /* block data entry */
848 xfs_inode_t *dp; /* incore inode */
849 int ent; /* leaf entry index */
850 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000852 trace_xfs_dir2_block_replace(args);
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /*
855 * Lookup the entry in the directory. Get buffer and entry index.
856 * This will always succeed since the caller has already done a lookup.
857 */
858 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
859 return error;
860 }
861 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000862 hdr = bp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000863 btp = xfs_dir2_block_tail_p(args->geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000864 blp = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 /*
866 * Point to the data entry we need to change.
867 */
Dave Chinner30028032014-06-06 15:08:18 +1000868 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
869 xfs_dir2_dataptr_to_off(args->geo,
870 be32_to_cpu(blp[ent].address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000871 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /*
873 * Change the inode number to the new value.
874 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000875 dep->inumber = cpu_to_be64(args->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100876 dp->d_ops->data_put_ftype(dep, args->filetype);
Dave Chinnerbc851782014-06-06 15:20:54 +1000877 xfs_dir2_data_log_entry(args, bp, dep);
Dave Chinner33363fe2013-04-03 16:11:22 +1100878 xfs_dir3_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return 0;
880}
881
882/*
883 * Qsort comparison routine for the block leaf entries.
884 */
885static int /* sort order */
886xfs_dir2_block_sort(
887 const void *a, /* first leaf entry */
888 const void *b) /* second leaf entry */
889{
890 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
891 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
892
893 la = a;
894 lb = b;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100895 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
896 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
899/*
900 * Convert a V2 leaf directory to a V2 block directory if possible.
901 */
902int /* error */
903xfs_dir2_leaf_to_block(
904 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000905 struct xfs_buf *lbp, /* leaf buffer */
906 struct xfs_buf *dbp) /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Nathan Scott68b3a102006-03-17 17:27:19 +1100908 __be16 *bestsp; /* leaf bests table */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200909 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 xfs_dir2_block_tail_t *btp; /* block tail */
911 xfs_inode_t *dp; /* incore directory inode */
912 xfs_dir2_data_unused_t *dup; /* unused data entry */
913 int error; /* error return value */
914 int from; /* leaf from index */
915 xfs_dir2_leaf_t *leaf; /* leaf structure */
916 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
917 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
918 xfs_mount_t *mp; /* file system mount point */
919 int needlog; /* need to log data header */
920 int needscan; /* need to scan for bestfree */
921 xfs_dir2_sf_hdr_t sfh; /* shortform header */
922 int size; /* bytes used */
Nathan Scott3d693c62006-03-17 17:28:27 +1100923 __be16 *tagp; /* end of entry (tag) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 int to; /* block/leaf to index */
925 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000926 struct xfs_dir2_leaf_entry *ents;
927 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000929 trace_xfs_dir2_leaf_to_block(args);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 dp = args->dp;
932 tp = args->trans;
933 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000934 leaf = lbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100935 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100936 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner8f661932014-06-06 15:15:59 +1000937 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000938
939 ASSERT(leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
940 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /*
942 * If there are data blocks other than the first one, take this
943 * opportunity to remove trailing empty data blocks that may have
944 * been left behind during no-space-reservation operations.
945 * These will show up in the leaf bests table.
946 */
Dave Chinner8f661932014-06-06 15:15:59 +1000947 while (dp->i_d.di_size > args->geo->blksize) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100948 int hdrsz;
949
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100950 hdrsz = dp->d_ops->data_entry_offset;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000951 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100952 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
Dave Chinner8f661932014-06-06 15:15:59 +1000953 args->geo->blksize - hdrsz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if ((error =
955 xfs_dir2_leaf_trim_data(args, lbp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100956 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000957 return error;
958 } else
959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 /*
962 * Read the data block if we don't already have it, give up if it fails.
963 */
Dave Chinner4bb20a82012-11-12 22:54:10 +1100964 if (!dbp) {
Dave Chinner7dda6e82014-06-06 15:11:18 +1000965 error = xfs_dir3_data_read(tp, dp, args->geo->datablk, -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100966 if (error)
967 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000969 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100970 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
971 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /*
974 * Size of the "leaf" area in the block.
975 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200976 size = (uint)sizeof(xfs_dir2_block_tail_t) +
Dave Chinner24df33b2013-04-12 07:30:21 +1000977 (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /*
979 * Look at the last data entry.
980 */
Dave Chinner8f661932014-06-06 15:15:59 +1000981 tagp = (__be16 *)((char *)hdr + args->geo->blksize) - 1;
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200982 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /*
984 * If it's not free or is too short we can't do it.
985 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100986 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
Dave Chinner1d9025e2012-06-22 18:50:14 +1000987 be16_to_cpu(dup->length) < size)
988 return 0;
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /*
991 * Start converting it to block form.
992 */
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100993 xfs_dir3_block_init(mp, tp, dbp, dp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 needlog = 1;
996 needscan = 0;
997 /*
998 * Use up the space at the end of the block (blp/btp).
999 */
Dave Chinnerbc851782014-06-06 15:20:54 +10001000 xfs_dir2_data_use_free(args, dbp, dup, args->geo->blksize - size, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 &needlog, &needscan);
1002 /*
1003 * Initialize the block tail.
1004 */
Dave Chinner8f661932014-06-06 15:15:59 +10001005 btp = xfs_dir2_block_tail_p(args->geo, hdr);
Dave Chinner24df33b2013-04-12 07:30:21 +10001006 btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 btp->stale = 0;
1008 xfs_dir2_block_log_tail(tp, dbp);
1009 /*
1010 * Initialize the block leaf area. We compact out stale entries.
1011 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001012 lep = xfs_dir2_block_leaf_p(btp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001013 for (from = to = 0; from < leafhdr.count; from++) {
1014 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 continue;
Dave Chinner24df33b2013-04-12 07:30:21 +10001016 lep[to++] = ents[from];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
Nathan Scotte922fff2006-03-17 17:27:56 +11001018 ASSERT(to == be32_to_cpu(btp->count));
1019 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /*
1021 * Scan the bestfree if we need it and log the data block header.
1022 */
1023 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001024 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001026 xfs_dir2_data_log_header(args, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /*
1028 * Pitch the old leaf block.
1029 */
Dave Chinner7dda6e82014-06-06 15:11:18 +10001030 error = xfs_da_shrink_inode(args, args->geo->leafblk, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001031 if (error)
1032 return error;
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /*
1035 * Now see if the resulting block can be shrunken to shortform.
1036 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001037 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001038 if (size > XFS_IFORK_DSIZE(dp))
1039 return 0;
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044/*
1045 * Convert the shortform directory to block form.
1046 */
1047int /* error */
1048xfs_dir2_sf_to_block(
1049 xfs_da_args_t *args) /* operation arguments */
1050{
1051 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001052 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001054 struct xfs_buf *bp; /* block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 xfs_dir2_block_tail_t *btp; /* block tail pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1057 xfs_inode_t *dp; /* incore directory inode */
1058 int dummy; /* trash */
1059 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1060 int endoffset; /* end of data objects */
1061 int error; /* error return value */
1062 int i; /* index */
1063 xfs_mount_t *mp; /* filesystem mount point */
1064 int needlog; /* need to log block header */
1065 int needscan; /* need to scan block freespc */
1066 int newoffset; /* offset from current entry */
1067 int offset; /* target block offset */
1068 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001069 xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
1070 xfs_dir2_sf_hdr_t *sfp; /* shortform header */
Nathan Scott3d693c62006-03-17 17:28:27 +11001071 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +10001073 struct xfs_name name;
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001074 struct xfs_ifork *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001076 trace_xfs_dir2_sf_to_block(args);
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 dp = args->dp;
1079 tp = args->trans;
1080 mp = dp->i_mount;
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001081 ifp = XFS_IFORK_PTR(dp, XFS_DATA_FORK);
1082 ASSERT(ifp->if_flags & XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /*
1084 * Bomb out if the shortform directory is way too short.
1085 */
1086 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1087 ASSERT(XFS_FORCED_SHUTDOWN(mp));
Dave Chinner24513372014-06-25 14:58:08 +10001088 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001090
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001091 oldsfp = (xfs_dir2_sf_hdr_t *)ifp->if_u1.if_data;
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001092
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001093 ASSERT(ifp->if_bytes == dp->i_d.di_size);
1094 ASSERT(ifp->if_u1.if_data != NULL);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001095 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001096 ASSERT(dp->i_d.di_nextents == 0);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /*
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001099 * Copy the directory into a temporary buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 * Then pitch the incore inode data so we can make extents.
1101 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001102 sfp = kmem_alloc(ifp->if_bytes, KM_SLEEP);
1103 memcpy(sfp, oldsfp, ifp->if_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001105 xfs_idata_realloc(dp, -ifp->if_bytes, XFS_DATA_FORK);
1106 xfs_bmap_local_to_extents_empty(dp, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 dp->i_d.di_size = 0;
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /*
1110 * Add block 0 to the inode.
1111 */
1112 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1113 if (error) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001114 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return error;
1116 }
1117 /*
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001118 * Initialize the data block, then convert it to block format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001120 error = xfs_dir3_data_init(args, blkno, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (error) {
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001122 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return error;
1124 }
Dave Chinnerd75afeb2013-04-03 16:11:29 +11001125 xfs_dir3_block_init(mp, tp, bp, dp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001126 hdr = bp->b_addr;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 /*
1129 * Compute size of block "tail" area.
1130 */
1131 i = (uint)sizeof(*btp) +
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001132 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /*
1134 * The whole thing is initialized to free by the init routine.
1135 * Say we're using the leaf and tail area.
1136 */
Dave Chinner2ca98772013-10-29 22:11:49 +11001137 dup = dp->d_ops->data_unused_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001139 xfs_dir2_data_use_free(args, bp, dup, args->geo->blksize - i,
Dave Chinner8f661932014-06-06 15:15:59 +10001140 i, &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 ASSERT(needscan == 0);
1142 /*
1143 * Fill in the tail.
1144 */
Dave Chinner8f661932014-06-06 15:15:59 +10001145 btp = xfs_dir2_block_tail_p(args->geo, hdr);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001146 btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 btp->stale = 0;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001148 blp = xfs_dir2_block_leaf_p(btp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001149 endoffset = (uint)((char *)blp - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /*
1151 * Remove the freespace, we'll manage it.
1152 */
Dave Chinnerbc851782014-06-06 15:20:54 +10001153 xfs_dir2_data_use_free(args, bp, dup,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001154 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
Nathan Scottad354eb2006-03-17 17:27:37 +11001155 be16_to_cpu(dup->length), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /*
1157 * Create entry for .
1158 */
Dave Chinner9d23fc82013-10-29 22:11:48 +11001159 dep = dp->d_ops->data_dot_entry_p(hdr);
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001160 dep->inumber = cpu_to_be64(dp->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 dep->namelen = 1;
1162 dep->name[0] = '.';
Dave Chinner9d23fc82013-10-29 22:11:48 +11001163 dp->d_ops->data_put_ftype(dep, XFS_DIR3_FT_DIR);
1164 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001165 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001166 xfs_dir2_data_log_entry(args, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001167 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
Eric Sandeen25994052014-04-14 19:02:30 +10001168 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001169 (char *)dep - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /*
1171 * Create entry for ..
1172 */
Dave Chinner9d23fc82013-10-29 22:11:48 +11001173 dep = dp->d_ops->data_dotdot_entry_p(hdr);
Dave Chinner47401752013-10-29 22:11:47 +11001174 dep->inumber = cpu_to_be64(dp->d_ops->sf_get_parent_ino(sfp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 dep->namelen = 2;
1176 dep->name[0] = dep->name[1] = '.';
Dave Chinner9d23fc82013-10-29 22:11:48 +11001177 dp->d_ops->data_put_ftype(dep, XFS_DIR3_FT_DIR);
1178 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001179 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001180 xfs_dir2_data_log_entry(args, bp, dep);
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001181 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
Eric Sandeen25994052014-04-14 19:02:30 +10001182 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001183 (char *)dep - (char *)hdr));
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001184 offset = dp->d_ops->data_first_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /*
1186 * Loop over existing entries, stuff them in.
1187 */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001188 i = 0;
1189 if (!sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 sfep = NULL;
1191 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001192 sfep = xfs_dir2_sf_firstentry(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /*
1194 * Need to preserve the existing offset values in the sf directory.
1195 * Insert holes (unused entries) where necessary.
1196 */
1197 while (offset < endoffset) {
1198 /*
1199 * sfep is null when we reach the end of the list.
1200 */
1201 if (sfep == NULL)
1202 newoffset = endoffset;
1203 else
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001204 newoffset = xfs_dir2_sf_get_offset(sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 /*
1206 * There should be a hole here, make one.
1207 */
1208 if (offset < newoffset) {
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001209 dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +11001210 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1211 dup->length = cpu_to_be16(newoffset - offset);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001212 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001213 ((char *)dup - (char *)hdr));
Dave Chinnerbc851782014-06-06 15:20:54 +10001214 xfs_dir2_data_log_unused(args, bp, dup);
Dave Chinner2ca98772013-10-29 22:11:49 +11001215 xfs_dir2_data_freeinsert(hdr,
1216 dp->d_ops->data_bestfree_p(hdr),
1217 dup, &dummy);
Nathan Scottad354eb2006-03-17 17:27:37 +11001218 offset += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 continue;
1220 }
1221 /*
1222 * Copy a real entry.
1223 */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001224 dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
Dave Chinner47401752013-10-29 22:11:47 +11001225 dep->inumber = cpu_to_be64(dp->d_ops->sf_get_ino(sfp, sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 dep->namelen = sfep->namelen;
Dave Chinner9d23fc82013-10-29 22:11:48 +11001227 dp->d_ops->data_put_ftype(dep, dp->d_ops->sf_get_ftype(sfep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 memcpy(dep->name, sfep->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001229 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001230 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001231 xfs_dir2_data_log_entry(args, bp, dep);
Barry Naujok5163f952008-05-21 16:41:01 +10001232 name.name = sfep->name;
1233 name.len = sfep->namelen;
1234 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1235 hashname(&name));
Eric Sandeen25994052014-04-14 19:02:30 +10001236 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +02001237 (char *)dep - (char *)hdr));
1238 offset = (int)((char *)(tagp + 1) - (char *)hdr);
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001239 if (++i == sfp->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 sfep = NULL;
1241 else
Dave Chinner32c54832013-10-29 22:11:46 +11001242 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244 /* Done with the temporary buffer */
Christoph Hellwigac8ba502011-07-08 14:35:13 +02001245 kmem_free(sfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /*
1247 * Sort the leaf entries by hash value.
1248 */
Nathan Scotte922fff2006-03-17 17:27:56 +11001249 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /*
1251 * Log the leaf entry area and tail.
1252 * Already logged the header in data_init, ignore needlog.
1253 */
1254 ASSERT(needscan == 0);
Nathan Scotte922fff2006-03-17 17:27:56 +11001255 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 xfs_dir2_block_log_tail(tp, bp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001257 xfs_dir3_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return 0;
1259}