]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/xfs/libxfs/xfs_dir2_sf.c
xfs: convert xfs_sb_version_has checks to use mount features
[mirror_ubuntu-jammy-kernel.git] / fs / xfs / libxfs / xfs_dir2_sf.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
7b718769
NS
3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
1da177e4 5 */
1da177e4 6#include "xfs.h"
a844f451 7#include "xfs_fs.h"
5467b34b 8#include "xfs_shared.h"
a4fbe6ab 9#include "xfs_format.h"
239880ef
DC
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
1da177e4 12#include "xfs_mount.h"
1da177e4 13#include "xfs_inode.h"
239880ef 14#include "xfs_trans.h"
2b9ab5ab 15#include "xfs_dir2.h"
57926640 16#include "xfs_dir2_priv.h"
0b1b213f 17#include "xfs_trace.h"
1da177e4
LT
18
19/*
20 * Prototypes for internal functions.
21 */
22static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
23 xfs_dir2_sf_entry_t *sfep,
24 xfs_dir2_data_aoff_t offset,
25 int new_isize);
26static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
27 int new_isize);
28static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
29 xfs_dir2_sf_entry_t **sfepp,
30 xfs_dir2_data_aoff_t *offsetp);
31#ifdef DEBUG
32static void xfs_dir2_sf_check(xfs_da_args_t *args);
33#else
34#define xfs_dir2_sf_check(args)
35#endif /* DEBUG */
d5cf09ba 36
1da177e4
LT
37static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
38static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
1da177e4 39
af952aeb 40int
50f6bb6b
CH
41xfs_dir2_sf_entsize(
42 struct xfs_mount *mp,
43 struct xfs_dir2_sf_hdr *hdr,
44 int len)
45{
46 int count = len;
47
48 count += sizeof(struct xfs_dir2_sf_entry); /* namelen + offset */
49 count += hdr->i8count ? XFS_INO64_SIZE : XFS_INO32_SIZE; /* ino # */
50
ebd9027d 51 if (xfs_has_ftype(mp))
50f6bb6b
CH
52 count += sizeof(uint8_t);
53 return count;
54}
55
56struct xfs_dir2_sf_entry *
57xfs_dir2_sf_nextentry(
58 struct xfs_mount *mp,
59 struct xfs_dir2_sf_hdr *hdr,
60 struct xfs_dir2_sf_entry *sfep)
61{
62 return (void *)sfep + xfs_dir2_sf_entsize(mp, hdr, sfep->namelen);
63}
64
93b1e96a
CH
65/*
66 * In short-form directory entries the inode numbers are stored at variable
67 * offset behind the entry name. If the entry stores a filetype value, then it
68 * sits between the name and the inode number. The actual inode numbers can
69 * come in two formats as well, either 4 bytes or 8 bytes wide.
70 */
71xfs_ino_t
72xfs_dir2_sf_get_ino(
73 struct xfs_mount *mp,
74 struct xfs_dir2_sf_hdr *hdr,
75 struct xfs_dir2_sf_entry *sfep)
76{
77 uint8_t *from = sfep->name + sfep->namelen;
78
ebd9027d 79 if (xfs_has_ftype(mp))
93b1e96a
CH
80 from++;
81
82 if (!hdr->i8count)
83 return get_unaligned_be32(from);
84 return get_unaligned_be64(from) & XFS_MAXINUMBER;
85}
86
af952aeb 87void
93b1e96a
CH
88xfs_dir2_sf_put_ino(
89 struct xfs_mount *mp,
90 struct xfs_dir2_sf_hdr *hdr,
91 struct xfs_dir2_sf_entry *sfep,
92 xfs_ino_t ino)
93{
94 uint8_t *to = sfep->name + sfep->namelen;
95
96 ASSERT(ino <= XFS_MAXINUMBER);
97
ebd9027d 98 if (xfs_has_ftype(mp))
93b1e96a
CH
99 to++;
100
101 if (hdr->i8count)
102 put_unaligned_be64(ino, to);
103 else
104 put_unaligned_be32(ino, to);
105}
106
107xfs_ino_t
108xfs_dir2_sf_get_parent_ino(
109 struct xfs_dir2_sf_hdr *hdr)
110{
111 if (!hdr->i8count)
112 return get_unaligned_be32(hdr->parent);
113 return get_unaligned_be64(hdr->parent) & XFS_MAXINUMBER;
114}
115
116void
117xfs_dir2_sf_put_parent_ino(
118 struct xfs_dir2_sf_hdr *hdr,
119 xfs_ino_t ino)
120{
121 ASSERT(ino <= XFS_MAXINUMBER);
122
123 if (hdr->i8count)
124 put_unaligned_be64(ino, hdr->parent);
125 else
126 put_unaligned_be32(ino, hdr->parent);
127}
128
4501ed2a
CH
129/*
130 * The file type field is stored at the end of the name for filetype enabled
131 * shortform directories, or not at all otherwise.
132 */
133uint8_t
134xfs_dir2_sf_get_ftype(
135 struct xfs_mount *mp,
136 struct xfs_dir2_sf_entry *sfep)
137{
ebd9027d 138 if (xfs_has_ftype(mp)) {
4501ed2a
CH
139 uint8_t ftype = sfep->name[sfep->namelen];
140
141 if (ftype < XFS_DIR3_FT_MAX)
142 return ftype;
143 }
144
145 return XFS_DIR3_FT_UNKNOWN;
146}
147
af952aeb 148void
4501ed2a
CH
149xfs_dir2_sf_put_ftype(
150 struct xfs_mount *mp,
151 struct xfs_dir2_sf_entry *sfep,
152 uint8_t ftype)
153{
154 ASSERT(ftype < XFS_DIR3_FT_MAX);
155
ebd9027d 156 if (xfs_has_ftype(mp))
4501ed2a
CH
157 sfep->name[sfep->namelen] = ftype;
158}
159
1da177e4
LT
160/*
161 * Given a block directory (dp/block), calculate its size as a shortform (sf)
162 * directory and a header for the sf directory, if it will fit it the
163 * space currently present in the inode. If it won't fit, the output
164 * size is too big (but not accurate).
165 */
166int /* size for sf form */
167xfs_dir2_block_sfsize(
168 xfs_inode_t *dp, /* incore inode pointer */
4f6ae1a4 169 xfs_dir2_data_hdr_t *hdr, /* block directory data */
1da177e4
LT
170 xfs_dir2_sf_hdr_t *sfhp) /* output: header for sf form */
171{
172 xfs_dir2_dataptr_t addr; /* data entry address */
173 xfs_dir2_leaf_entry_t *blp; /* leaf area of the block */
174 xfs_dir2_block_tail_t *btp; /* tail area of the block */
175 int count; /* shortform entry count */
176 xfs_dir2_data_entry_t *dep; /* data entry in the block */
177 int i; /* block entry index */
178 int i8count; /* count of big-inode entries */
179 int isdot; /* entry is "." */
180 int isdotdot; /* entry is ".." */
181 xfs_mount_t *mp; /* mount structure pointer */
182 int namelen; /* total name bytes */
5bde1ba9 183 xfs_ino_t parent = 0; /* parent inode number */
1da177e4 184 int size=0; /* total computed size */
0cb97766 185 int has_ftype;
8f66193c 186 struct xfs_da_geometry *geo;
1da177e4
LT
187
188 mp = dp->i_mount;
8f66193c 189 geo = mp->m_dir_geo;
1da177e4 190
0cb97766
DC
191 /*
192 * if there is a filetype field, add the extra byte to the namelen
193 * for each entry that we see.
194 */
38c26bfd 195 has_ftype = xfs_has_ftype(mp) ? 1 : 0;
0cb97766 196
1da177e4 197 count = i8count = namelen = 0;
8f66193c 198 btp = xfs_dir2_block_tail_p(geo, hdr);
bbaaf538 199 blp = xfs_dir2_block_leaf_p(btp);
1da177e4
LT
200
201 /*
202 * Iterate over the block's data entries by using the leaf pointers.
203 */
e922fffa 204 for (i = 0; i < be32_to_cpu(btp->count); i++) {
3c1f9c15 205 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
1da177e4
LT
206 continue;
207 /*
208 * Calculate the pointer to the entry at hand.
209 */
30028030 210 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
8f66193c 211 xfs_dir2_dataptr_to_off(geo, addr));
1da177e4
LT
212 /*
213 * Detect . and .., so we can special-case them.
214 * . is not included in sf directories.
215 * .. is included by just the parent inode number.
216 */
217 isdot = dep->namelen == 1 && dep->name[0] == '.';
218 isdotdot =
219 dep->namelen == 2 &&
220 dep->name[0] == '.' && dep->name[1] == '.';
d5cf09ba 221
1da177e4 222 if (!isdot)
ff9901c1 223 i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
d5cf09ba 224
0cb97766 225 /* take into account the file type field */
1da177e4
LT
226 if (!isdot && !isdotdot) {
227 count++;
0cb97766 228 namelen += dep->namelen + has_ftype;
1da177e4 229 } else if (isdotdot)
ff9901c1 230 parent = be64_to_cpu(dep->inumber);
1da177e4
LT
231 /*
232 * Calculate the new size, see if we should give up yet.
233 */
8353a649
CH
234 size = xfs_dir2_sf_hdr_size(i8count) + /* header */
235 count * 3 * sizeof(u8) + /* namelen + offset */
236 namelen + /* name */
237 (i8count ? /* inumber */
266b6969
CH
238 count * XFS_INO64_SIZE :
239 count * XFS_INO32_SIZE);
1da177e4
LT
240 if (size > XFS_IFORK_DSIZE(dp))
241 return size; /* size value is a failure */
242 }
243 /*
244 * Create the output header, if it worked.
245 */
246 sfhp->count = count;
247 sfhp->i8count = i8count;
84915e1b 248 xfs_dir2_sf_put_parent_ino(sfhp, parent);
1da177e4
LT
249 return size;
250}
251
252/*
253 * Convert a block format directory to shortform.
254 * Caller has already checked that it will fit, and built us a header.
255 */
256int /* error */
257xfs_dir2_block_to_sf(
8073af51 258 struct xfs_da_args *args, /* operation arguments */
1d9025e5 259 struct xfs_buf *bp,
1da177e4 260 int size, /* shortform directory size */
8073af51 261 struct xfs_dir2_sf_hdr *sfhp) /* shortform directory hdr */
1da177e4 262{
8073af51
CH
263 struct xfs_inode *dp = args->dp;
264 struct xfs_mount *mp = dp->i_mount;
1da177e4
LT
265 int error; /* error return value */
266 int logflags; /* inode logging flags */
8073af51
CH
267 struct xfs_dir2_sf_entry *sfep; /* shortform entry */
268 struct xfs_dir2_sf_hdr *sfp; /* shortform directory header */
d73e1cee 269 unsigned int offset = args->geo->data_entry_offset;
8073af51 270 unsigned int end;
1da177e4 271
0b1b213f
CH
272 trace_xfs_dir2_block_to_sf(args);
273
1da177e4 274 /*
8073af51
CH
275 * Allocate a temporary destination buffer the size of the inode to
276 * format the data into. Once we have formatted the data, we can free
277 * the block and copy the formatted data into the inode literal area.
1da177e4 278 */
8073af51 279 sfp = kmem_alloc(mp->m_sb.sb_inodesize, 0);
bbaaf538 280 memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
b3f03bac 281
1da177e4 282 /*
8073af51
CH
283 * Loop over the active and unused entries. Stop when we reach the
284 * leaf/tail portion of the block.
1da177e4 285 */
5c072127 286 end = xfs_dir3_data_end_offset(args->geo, bp->b_addr);
bbaaf538 287 sfep = xfs_dir2_sf_firstentry(sfp);
8073af51
CH
288 while (offset < end) {
289 struct xfs_dir2_data_unused *dup = bp->b_addr + offset;
290 struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
291
1da177e4
LT
292 /*
293 * If it's unused, just skip over it.
294 */
ad354eb3 295 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
8073af51 296 offset += be16_to_cpu(dup->length);
1da177e4
LT
297 continue;
298 }
8073af51 299
1da177e4
LT
300 /*
301 * Skip .
302 */
303 if (dep->namelen == 1 && dep->name[0] == '.')
ff9901c1 304 ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
1da177e4
LT
305 /*
306 * Skip .., but make sure the inode number is right.
307 */
308 else if (dep->namelen == 2 &&
309 dep->name[0] == '.' && dep->name[1] == '.')
ff9901c1 310 ASSERT(be64_to_cpu(dep->inumber) ==
84915e1b 311 xfs_dir2_sf_get_parent_ino(sfp));
1da177e4
LT
312 /*
313 * Normal entry, copy it into shortform.
314 */
315 else {
316 sfep->namelen = dep->namelen;
8073af51 317 xfs_dir2_sf_put_offset(sfep, offset);
1da177e4 318 memcpy(sfep->name, dep->name, dep->namelen);
93b1e96a 319 xfs_dir2_sf_put_ino(mp, sfp, sfep,
4740175e 320 be64_to_cpu(dep->inumber));
4501ed2a 321 xfs_dir2_sf_put_ftype(mp, sfep,
59b8b465 322 xfs_dir2_data_get_ftype(mp, dep));
8bc38787 323
50f6bb6b 324 sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
1da177e4 325 }
fdbb8c5b 326 offset += xfs_dir2_data_entsize(mp, dep->namelen);
1da177e4
LT
327 }
328 ASSERT((char *)sfep - (char *)sfp == size);
b3f03bac
DC
329
330 /* now we are done with the block, we can shrink the inode */
331 logflags = XFS_ILOG_CORE;
7dda6e86 332 error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
b3f03bac 333 if (error) {
2451337d 334 ASSERT(error != -ENOSPC);
b3f03bac
DC
335 goto out;
336 }
337
338 /*
339 * The buffer is now unconditionally gone, whether
340 * xfs_dir2_shrink_inode worked or not.
341 *
342 * Convert the inode to local format and copy the data in.
343 */
b3f03bac 344 ASSERT(dp->i_df.if_bytes == 0);
8073af51 345 xfs_init_local_fork(dp, XFS_DATA_FORK, sfp, size);
f7e67b20 346 dp->i_df.if_format = XFS_DINODE_FMT_LOCAL;
13d2c10b 347 dp->i_disk_size = size;
b3f03bac
DC
348
349 logflags |= XFS_ILOG_DDATA;
1da177e4
LT
350 xfs_dir2_sf_check(args);
351out:
352 xfs_trans_log_inode(args->trans, dp, logflags);
8073af51 353 kmem_free(sfp);
1da177e4
LT
354 return error;
355}
356
357/*
358 * Add a name to a shortform directory.
359 * There are two algorithms, "easy" and "hard" which we decide on
360 * before changing anything.
361 * Convert to block form if necessary, if the new entry won't fit.
362 */
363int /* error */
364xfs_dir2_sf_addname(
365 xfs_da_args_t *args) /* operation arguments */
366{
1da177e4
LT
367 xfs_inode_t *dp; /* incore directory inode */
368 int error; /* error return value */
369 int incr_isize; /* total change in size */
13d2c10b 370 int new_isize; /* size after adding name */
1da177e4 371 int objchange; /* changing to 8-byte inodes */
5bde1ba9 372 xfs_dir2_data_aoff_t offset = 0; /* offset for new entry */
1da177e4 373 int pick; /* which algorithm to use */
ac8ba50f 374 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
5bde1ba9 375 xfs_dir2_sf_entry_t *sfep = NULL; /* shortform entry */
1da177e4 376
0b1b213f
CH
377 trace_xfs_dir2_sf_addname(args);
378
2451337d 379 ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
1da177e4 380 dp = args->dp;
0779f4a6 381 ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
13d2c10b
CH
382 ASSERT(dp->i_disk_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
383 ASSERT(dp->i_df.if_bytes == dp->i_disk_size);
1da177e4 384 ASSERT(dp->i_df.if_u1.if_data != NULL);
ac8ba50f 385 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
13d2c10b 386 ASSERT(dp->i_disk_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
1da177e4
LT
387 /*
388 * Compute entry (and change in) size.
389 */
50f6bb6b 390 incr_isize = xfs_dir2_sf_entsize(dp->i_mount, sfp, args->namelen);
1da177e4 391 objchange = 0;
d5cf09ba 392
1da177e4
LT
393 /*
394 * Do we have to change to 8 byte inodes?
395 */
ac8ba50f 396 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
1da177e4 397 /*
5e06d148 398 * Yes, adjust the inode size. old count + (parent + new)
1da177e4 399 */
266b6969 400 incr_isize += (sfp->count + 2) * XFS_INO64_DIFF;
1da177e4
LT
401 objchange = 1;
402 }
d5cf09ba 403
13d2c10b 404 new_isize = (int)dp->i_disk_size + incr_isize;
1da177e4
LT
405 /*
406 * Won't fit as shortform any more (due to size),
407 * or the pick routine says it won't (due to offset values).
408 */
409 if (new_isize > XFS_IFORK_DSIZE(dp) ||
410 (pick =
411 xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
412 /*
413 * Just checking or no space reservation, it doesn't fit.
414 */
6a178100 415 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
2451337d 416 return -ENOSPC;
1da177e4
LT
417 /*
418 * Convert to block form then add the name.
419 */
420 error = xfs_dir2_sf_to_block(args);
421 if (error)
422 return error;
423 return xfs_dir2_block_addname(args);
424 }
425 /*
426 * Just checking, it fits.
427 */
6a178100 428 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
1da177e4
LT
429 return 0;
430 /*
431 * Do it the easy way - just add it at the end.
432 */
433 if (pick == 1)
434 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
435 /*
436 * Do it the hard way - look for a place to insert the new entry.
437 * Convert to 8 byte inode numbers first if necessary.
438 */
439 else {
440 ASSERT(pick == 2);
1da177e4
LT
441 if (objchange)
442 xfs_dir2_sf_toino8(args);
1da177e4
LT
443 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
444 }
445 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
446 return 0;
447}
448
449/*
450 * Add the new entry the "easy" way.
451 * This is copying the old directory and adding the new entry at the end.
452 * Since it's sorted by "offset" we need room after the last offset
453 * that's already there, and then room to convert to a block directory.
454 * This is already checked by the pick routine.
455 */
456static void
457xfs_dir2_sf_addname_easy(
458 xfs_da_args_t *args, /* operation arguments */
459 xfs_dir2_sf_entry_t *sfep, /* pointer to new entry */
460 xfs_dir2_data_aoff_t offset, /* offset to use for new ent */
461 int new_isize) /* new directory size */
462{
50f6bb6b
CH
463 struct xfs_inode *dp = args->dp;
464 struct xfs_mount *mp = dp->i_mount;
1da177e4 465 int byteoff; /* byte offset in sf dir */
ac8ba50f 466 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
1da177e4 467
ac8ba50f 468 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1da177e4
LT
469 byteoff = (int)((char *)sfep - (char *)sfp);
470 /*
471 * Grow the in-inode space.
472 */
50f6bb6b 473 xfs_idata_realloc(dp, xfs_dir2_sf_entsize(mp, sfp, args->namelen),
0cb97766 474 XFS_DATA_FORK);
1da177e4
LT
475 /*
476 * Need to set up again due to realloc of the inode data.
477 */
ac8ba50f 478 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1da177e4
LT
479 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
480 /*
481 * Fill in the new entry.
482 */
483 sfep->namelen = args->namelen;
bbaaf538 484 xfs_dir2_sf_put_offset(sfep, offset);
1da177e4 485 memcpy(sfep->name, args->name, sfep->namelen);
93b1e96a 486 xfs_dir2_sf_put_ino(mp, sfp, sfep, args->inumber);
4501ed2a 487 xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
1c55cece 488
1da177e4
LT
489 /*
490 * Update the header and inode.
491 */
ac8ba50f 492 sfp->count++;
1da177e4 493 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
ac8ba50f 494 sfp->i8count++;
13d2c10b 495 dp->i_disk_size = new_isize;
1da177e4
LT
496 xfs_dir2_sf_check(args);
497}
498
499/*
500 * Add the new entry the "hard" way.
501 * The caller has already converted to 8 byte inode numbers if necessary,
502 * in which case we need to leave the i8count at 1.
503 * Find a hole that the new entry will fit into, and copy
504 * the first part of the entries, the new entry, and the last part of
505 * the entries.
506 */
507/* ARGSUSED */
508static void
509xfs_dir2_sf_addname_hard(
510 xfs_da_args_t *args, /* operation arguments */
511 int objchange, /* changing inode number size */
512 int new_isize) /* new directory size */
513{
50f6bb6b
CH
514 struct xfs_inode *dp = args->dp;
515 struct xfs_mount *mp = dp->i_mount;
1da177e4
LT
516 int add_datasize; /* data size need for new ent */
517 char *buf; /* buffer for old */
1da177e4
LT
518 int eof; /* reached end of old dir */
519 int nbytes; /* temp for byte copies */
520 xfs_dir2_data_aoff_t new_offset; /* next offset value */
521 xfs_dir2_data_aoff_t offset; /* current offset value */
13d2c10b 522 int old_isize; /* previous size */
1da177e4 523 xfs_dir2_sf_entry_t *oldsfep; /* entry in original dir */
ac8ba50f 524 xfs_dir2_sf_hdr_t *oldsfp; /* original shortform dir */
1da177e4 525 xfs_dir2_sf_entry_t *sfep; /* entry in new dir */
ac8ba50f 526 xfs_dir2_sf_hdr_t *sfp; /* new shortform dir */
1da177e4
LT
527
528 /*
529 * Copy the old directory to the stack buffer.
530 */
ac8ba50f 531 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
13d2c10b 532 old_isize = (int)dp->i_disk_size;
707e0dda 533 buf = kmem_alloc(old_isize, 0);
ac8ba50f 534 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1da177e4
LT
535 memcpy(oldsfp, sfp, old_isize);
536 /*
537 * Loop over the old directory finding the place we're going
538 * to insert the new entry.
539 * If it's going to end up at the end then oldsfep will point there.
540 */
d73e1cee 541 for (offset = args->geo->data_first_offset,
bbaaf538 542 oldsfep = xfs_dir2_sf_firstentry(oldsfp),
fdbb8c5b 543 add_datasize = xfs_dir2_data_entsize(mp, args->namelen),
1da177e4
LT
544 eof = (char *)oldsfep == &buf[old_isize];
545 !eof;
fdbb8c5b 546 offset = new_offset + xfs_dir2_data_entsize(mp, oldsfep->namelen),
50f6bb6b 547 oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep),
1da177e4 548 eof = (char *)oldsfep == &buf[old_isize]) {
bbaaf538 549 new_offset = xfs_dir2_sf_get_offset(oldsfep);
1da177e4
LT
550 if (offset + add_datasize <= new_offset)
551 break;
552 }
553 /*
554 * Get rid of the old directory, then allocate space for
555 * the new one. We do this so xfs_idata_realloc won't copy
556 * the data.
557 */
558 xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
559 xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
560 /*
561 * Reset the pointer since the buffer was reallocated.
562 */
ac8ba50f 563 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1da177e4
LT
564 /*
565 * Copy the first part of the directory, including the header.
566 */
567 nbytes = (int)((char *)oldsfep - (char *)oldsfp);
568 memcpy(sfp, oldsfp, nbytes);
569 sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
570 /*
571 * Fill in the new entry, and update the header counts.
572 */
573 sfep->namelen = args->namelen;
bbaaf538 574 xfs_dir2_sf_put_offset(sfep, offset);
1da177e4 575 memcpy(sfep->name, args->name, sfep->namelen);
93b1e96a 576 xfs_dir2_sf_put_ino(mp, sfp, sfep, args->inumber);
4501ed2a 577 xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
ac8ba50f 578 sfp->count++;
1da177e4 579 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
ac8ba50f 580 sfp->i8count++;
1da177e4
LT
581 /*
582 * If there's more left to copy, do that.
583 */
584 if (!eof) {
50f6bb6b 585 sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
1da177e4
LT
586 memcpy(sfep, oldsfep, old_isize - nbytes);
587 }
f0e2d93c 588 kmem_free(buf);
13d2c10b 589 dp->i_disk_size = new_isize;
1da177e4
LT
590 xfs_dir2_sf_check(args);
591}
592
593/*
594 * Decide if the new entry will fit at all.
595 * If it will fit, pick between adding the new entry to the end (easy)
596 * or somewhere else (hard).
597 * Return 0 (won't fit), 1 (easy), 2 (hard).
598 */
599/*ARGSUSED*/
600static int /* pick result */
601xfs_dir2_sf_addname_pick(
602 xfs_da_args_t *args, /* operation arguments */
603 int objchange, /* inode # size changes */
604 xfs_dir2_sf_entry_t **sfepp, /* out(1): new entry ptr */
605 xfs_dir2_data_aoff_t *offsetp) /* out(1): new offset */
606{
50f6bb6b
CH
607 struct xfs_inode *dp = args->dp;
608 struct xfs_mount *mp = dp->i_mount;
1da177e4
LT
609 int holefit; /* found hole it will fit in */
610 int i; /* entry number */
1da177e4
LT
611 xfs_dir2_data_aoff_t offset; /* data block offset */
612 xfs_dir2_sf_entry_t *sfep; /* shortform entry */
ac8ba50f 613 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
1da177e4
LT
614 int size; /* entry's data size */
615 int used; /* data bytes used */
616
ac8ba50f 617 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
fdbb8c5b 618 size = xfs_dir2_data_entsize(mp, args->namelen);
d73e1cee 619 offset = args->geo->data_first_offset;
bbaaf538 620 sfep = xfs_dir2_sf_firstentry(sfp);
1da177e4
LT
621 holefit = 0;
622 /*
623 * Loop over sf entries.
624 * Keep track of data offset and whether we've seen a place
625 * to insert the new entry.
626 */
ac8ba50f 627 for (i = 0; i < sfp->count; i++) {
1da177e4 628 if (!holefit)
bbaaf538
CH
629 holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
630 offset = xfs_dir2_sf_get_offset(sfep) +
fdbb8c5b 631 xfs_dir2_data_entsize(mp, sfep->namelen);
50f6bb6b 632 sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
1da177e4
LT
633 }
634 /*
635 * Calculate data bytes used excluding the new entry, if this
636 * was a data block (block form directory).
637 */
638 used = offset +
ac8ba50f 639 (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
1da177e4
LT
640 (uint)sizeof(xfs_dir2_block_tail_t);
641 /*
642 * If it won't fit in a block form then we can't insert it,
643 * we'll go back, convert to block, then try the insert and convert
644 * to leaf.
645 */
8f66193c 646 if (used + (holefit ? 0 : size) > args->geo->blksize)
1da177e4
LT
647 return 0;
648 /*
649 * If changing the inode number size, do it the hard way.
650 */
d5cf09ba 651 if (objchange)
1da177e4 652 return 2;
1da177e4
LT
653 /*
654 * If it won't fit at the end then do it the hard way (use the hole).
655 */
8f66193c 656 if (used + size > args->geo->blksize)
1da177e4
LT
657 return 2;
658 /*
659 * Do it the easy way.
660 */
661 *sfepp = sfep;
662 *offsetp = offset;
663 return 1;
664}
665
666#ifdef DEBUG
667/*
668 * Check consistency of shortform directory, assert if bad.
669 */
670static void
671xfs_dir2_sf_check(
672 xfs_da_args_t *args) /* operation arguments */
673{
4501ed2a
CH
674 struct xfs_inode *dp = args->dp;
675 struct xfs_mount *mp = dp->i_mount;
1da177e4
LT
676 int i; /* entry number */
677 int i8count; /* number of big inode#s */
678 xfs_ino_t ino; /* entry inode number */
679 int offset; /* data offset */
680 xfs_dir2_sf_entry_t *sfep; /* shortform dir entry */
ac8ba50f 681 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
1da177e4 682
ac8ba50f 683 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
d73e1cee 684 offset = args->geo->data_first_offset;
84915e1b 685 ino = xfs_dir2_sf_get_parent_ino(sfp);
1da177e4
LT
686 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
687
bbaaf538 688 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
ac8ba50f 689 i < sfp->count;
4501ed2a 690 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
bbaaf538 691 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
4501ed2a 692 ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
1da177e4
LT
693 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
694 offset =
bbaaf538 695 xfs_dir2_sf_get_offset(sfep) +
fdbb8c5b 696 xfs_dir2_data_entsize(mp, sfep->namelen);
4501ed2a 697 ASSERT(xfs_dir2_sf_get_ftype(mp, sfep) < XFS_DIR3_FT_MAX);
1da177e4 698 }
ac8ba50f 699 ASSERT(i8count == sfp->i8count);
13d2c10b 700 ASSERT((char *)sfep - (char *)sfp == dp->i_disk_size);
1da177e4 701 ASSERT(offset +
ac8ba50f 702 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
8f66193c 703 (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
1da177e4
LT
704}
705#endif /* DEBUG */
706
630a04e7 707/* Verify the consistency of an inline directory. */
dc042c2d 708xfs_failaddr_t
630a04e7 709xfs_dir2_sf_verify(
78420281 710 struct xfs_inode *ip)
630a04e7 711{
78420281 712 struct xfs_mount *mp = ip->i_mount;
f7e67b20 713 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
78420281 714 struct xfs_dir2_sf_hdr *sfp;
630a04e7
DW
715 struct xfs_dir2_sf_entry *sfep;
716 struct xfs_dir2_sf_entry *next_sfep;
717 char *endp;
630a04e7
DW
718 xfs_ino_t ino;
719 int i;
720 int i8count;
721 int offset;
3f8a4f1d 722 int64_t size;
78420281 723 int error;
c8ce540d 724 uint8_t filetype;
630a04e7 725
f7e67b20 726 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
630a04e7 727
78420281
DW
728 sfp = (struct xfs_dir2_sf_hdr *)ifp->if_u1.if_data;
729 size = ifp->if_bytes;
730
630a04e7
DW
731 /*
732 * Give up if the directory is way too short.
733 */
78420281
DW
734 if (size <= offsetof(struct xfs_dir2_sf_hdr, parent) ||
735 size < xfs_dir2_sf_hdr_size(sfp->i8count))
dc042c2d 736 return __this_address;
630a04e7
DW
737
738 endp = (char *)sfp + size;
739
740 /* Check .. entry */
84915e1b 741 ino = xfs_dir2_sf_get_parent_ino(sfp);
630a04e7 742 i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
78420281
DW
743 error = xfs_dir_ino_validate(mp, ino);
744 if (error)
dc042c2d 745 return __this_address;
d73e1cee 746 offset = mp->m_dir_geo->data_first_offset;
630a04e7
DW
747
748 /* Check all reported entries */
749 sfep = xfs_dir2_sf_firstentry(sfp);
750 for (i = 0; i < sfp->count; i++) {
751 /*
752 * struct xfs_dir2_sf_entry has a variable length.
753 * Check the fixed-offset parts of the structure are
754 * within the data buffer.
755 */
78420281 756 if (((char *)sfep + sizeof(*sfep)) >= endp)
dc042c2d 757 return __this_address;
630a04e7
DW
758
759 /* Don't allow names with known bad length. */
78420281 760 if (sfep->namelen == 0)
dc042c2d 761 return __this_address;
630a04e7
DW
762
763 /*
764 * Check that the variable-length part of the structure is
765 * within the data buffer. The next entry starts after the
766 * name component, so nextentry is an acceptable test.
767 */
50f6bb6b 768 next_sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
78420281 769 if (endp < (char *)next_sfep)
dc042c2d 770 return __this_address;
630a04e7
DW
771
772 /* Check that the offsets always increase. */
78420281 773 if (xfs_dir2_sf_get_offset(sfep) < offset)
dc042c2d 774 return __this_address;
630a04e7
DW
775
776 /* Check the inode number. */
93b1e96a 777 ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
630a04e7 778 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
78420281
DW
779 error = xfs_dir_ino_validate(mp, ino);
780 if (error)
dc042c2d 781 return __this_address;
630a04e7
DW
782
783 /* Check the file type. */
4501ed2a 784 filetype = xfs_dir2_sf_get_ftype(mp, sfep);
78420281 785 if (filetype >= XFS_DIR3_FT_MAX)
dc042c2d 786 return __this_address;
630a04e7
DW
787
788 offset = xfs_dir2_sf_get_offset(sfep) +
fdbb8c5b 789 xfs_dir2_data_entsize(mp, sfep->namelen);
630a04e7
DW
790
791 sfep = next_sfep;
792 }
78420281 793 if (i8count != sfp->i8count)
dc042c2d 794 return __this_address;
78420281 795 if ((void *)sfep != (void *)endp)
dc042c2d 796 return __this_address;
630a04e7
DW
797
798 /* Make sure this whole thing ought to be in local format. */
78420281
DW
799 if (offset + (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
800 (uint)sizeof(xfs_dir2_block_tail_t) > mp->m_dir_geo->blksize)
dc042c2d 801 return __this_address;
630a04e7 802
dc042c2d 803 return NULL;
630a04e7
DW
804}
805
1da177e4
LT
806/*
807 * Create a new (shortform) directory.
808 */
809int /* error, always 0 */
810xfs_dir2_sf_create(
811 xfs_da_args_t *args, /* operation arguments */
812 xfs_ino_t pino) /* parent inode number */
813{
814 xfs_inode_t *dp; /* incore directory inode */
815 int i8count; /* parent inode is an 8-byte number */
ac8ba50f 816 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
1da177e4
LT
817 int size; /* directory size */
818
0b1b213f
CH
819 trace_xfs_dir2_sf_create(args);
820
1da177e4
LT
821 dp = args->dp;
822
823 ASSERT(dp != NULL);
13d2c10b 824 ASSERT(dp->i_disk_size == 0);
1da177e4
LT
825 /*
826 * If it's currently a zero-length extent file,
827 * convert it to local format.
828 */
f7e67b20 829 if (dp->i_df.if_format == XFS_DINODE_FMT_EXTENTS) {
f7e67b20 830 dp->i_df.if_format = XFS_DINODE_FMT_LOCAL;
1da177e4 831 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
1da177e4 832 }
0779f4a6 833 ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
1da177e4
LT
834 ASSERT(dp->i_df.if_bytes == 0);
835 i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
bbaaf538 836 size = xfs_dir2_sf_hdr_size(i8count);
1da177e4
LT
837 /*
838 * Make a buffer for the data.
839 */
840 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
841 /*
842 * Fill in the header,
843 */
ac8ba50f
CH
844 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
845 sfp->i8count = i8count;
1da177e4
LT
846 /*
847 * Now can put in the inode number, since i8count is set.
848 */
84915e1b 849 xfs_dir2_sf_put_parent_ino(sfp, pino);
ac8ba50f 850 sfp->count = 0;
13d2c10b 851 dp->i_disk_size = size;
1da177e4
LT
852 xfs_dir2_sf_check(args);
853 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
854 return 0;
855}
856
1da177e4
LT
857/*
858 * Lookup an entry in a shortform directory.
859 * Returns EEXIST if found, ENOENT if not found.
860 */
861int /* error */
862xfs_dir2_sf_lookup(
863 xfs_da_args_t *args) /* operation arguments */
864{
50f6bb6b
CH
865 struct xfs_inode *dp = args->dp;
866 struct xfs_mount *mp = dp->i_mount;
1da177e4 867 int i; /* entry index */
384f3ced 868 int error;
1da177e4 869 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
ac8ba50f 870 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
5163f95a 871 enum xfs_dacmp cmp; /* comparison result */
384f3ced 872 xfs_dir2_sf_entry_t *ci_sfep; /* case-insens. entry */
1da177e4 873
0b1b213f
CH
874 trace_xfs_dir2_sf_lookup(args);
875
1da177e4 876 xfs_dir2_sf_check(args);
1da177e4 877
0779f4a6 878 ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
13d2c10b
CH
879 ASSERT(dp->i_disk_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
880 ASSERT(dp->i_df.if_bytes == dp->i_disk_size);
1da177e4 881 ASSERT(dp->i_df.if_u1.if_data != NULL);
ac8ba50f 882 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
13d2c10b 883 ASSERT(dp->i_disk_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
1da177e4
LT
884 /*
885 * Special case for .
886 */
887 if (args->namelen == 1 && args->name[0] == '.') {
888 args->inumber = dp->i_ino;
5163f95a 889 args->cmpresult = XFS_CMP_EXACT;
1c55cece 890 args->filetype = XFS_DIR3_FT_DIR;
2451337d 891 return -EEXIST;
1da177e4
LT
892 }
893 /*
894 * Special case for ..
895 */
896 if (args->namelen == 2 &&
897 args->name[0] == '.' && args->name[1] == '.') {
84915e1b 898 args->inumber = xfs_dir2_sf_get_parent_ino(sfp);
5163f95a 899 args->cmpresult = XFS_CMP_EXACT;
1c55cece 900 args->filetype = XFS_DIR3_FT_DIR;
2451337d 901 return -EEXIST;
1da177e4
LT
902 }
903 /*
904 * Loop over all the entries trying to match ours.
905 */
384f3ced 906 ci_sfep = NULL;
ac8ba50f 907 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
50f6bb6b 908 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
5163f95a
BN
909 /*
910 * Compare name and if it's an exact match, return the inode
911 * number. If it's the first case-insensitive match, store the
912 * inode number and continue looking for an exact match.
913 */
d8d11fc7 914 cmp = xfs_dir2_compname(args, sfep->name, sfep->namelen);
5163f95a
BN
915 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
916 args->cmpresult = cmp;
93b1e96a 917 args->inumber = xfs_dir2_sf_get_ino(mp, sfp, sfep);
4501ed2a 918 args->filetype = xfs_dir2_sf_get_ftype(mp, sfep);
5163f95a 919 if (cmp == XFS_CMP_EXACT)
2451337d 920 return -EEXIST;
384f3ced 921 ci_sfep = sfep;
1da177e4
LT
922 }
923 }
6a178100 924 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
5163f95a
BN
925 /*
926 * Here, we can only be doing a lookup (not a rename or replace).
2451337d 927 * If a case-insensitive match was not found, return -ENOENT.
5163f95a 928 */
384f3ced 929 if (!ci_sfep)
2451337d 930 return -ENOENT;
384f3ced
BN
931 /* otherwise process the CI match as required by the caller */
932 error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
b474c7ae 933 return error;
1da177e4
LT
934}
935
936/*
937 * Remove an entry from a shortform directory.
938 */
939int /* error */
940xfs_dir2_sf_removename(
941 xfs_da_args_t *args)
942{
50f6bb6b
CH
943 struct xfs_inode *dp = args->dp;
944 struct xfs_mount *mp = dp->i_mount;
1da177e4 945 int byteoff; /* offset of removed entry */
1da177e4
LT
946 int entsize; /* this entry's size */
947 int i; /* shortform entry index */
948 int newsize; /* new inode size */
949 int oldsize; /* old inode size */
950 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
ac8ba50f 951 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
1da177e4 952
0b1b213f
CH
953 trace_xfs_dir2_sf_removename(args);
954
0779f4a6 955 ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
13d2c10b 956 oldsize = (int)dp->i_disk_size;
e91ec882 957 ASSERT(oldsize >= offsetof(struct xfs_dir2_sf_hdr, parent));
1da177e4
LT
958 ASSERT(dp->i_df.if_bytes == oldsize);
959 ASSERT(dp->i_df.if_u1.if_data != NULL);
ac8ba50f
CH
960 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
961 ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
1da177e4
LT
962 /*
963 * Loop over the old directory entries.
964 * Find the one we're deleting.
965 */
ac8ba50f 966 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
50f6bb6b 967 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
5163f95a
BN
968 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
969 XFS_CMP_EXACT) {
93b1e96a 970 ASSERT(xfs_dir2_sf_get_ino(mp, sfp, sfep) ==
8bc38787 971 args->inumber);
1da177e4
LT
972 break;
973 }
974 }
975 /*
976 * Didn't find it.
977 */
ac8ba50f 978 if (i == sfp->count)
2451337d 979 return -ENOENT;
1da177e4
LT
980 /*
981 * Calculate sizes.
982 */
983 byteoff = (int)((char *)sfep - (char *)sfp);
50f6bb6b 984 entsize = xfs_dir2_sf_entsize(mp, sfp, args->namelen);
1da177e4
LT
985 newsize = oldsize - entsize;
986 /*
987 * Copy the part if any after the removed entry, sliding it down.
988 */
989 if (byteoff + entsize < oldsize)
990 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
991 oldsize - (byteoff + entsize));
992 /*
993 * Fix up the header and file size.
994 */
ac8ba50f 995 sfp->count--;
13d2c10b 996 dp->i_disk_size = newsize;
1da177e4
LT
997 /*
998 * Reallocate, making it smaller.
999 */
1000 xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
ac8ba50f 1001 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1da177e4
LT
1002 /*
1003 * Are we changing inode number size?
1004 */
1005 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
ac8ba50f 1006 if (sfp->i8count == 1)
1da177e4
LT
1007 xfs_dir2_sf_toino4(args);
1008 else
ac8ba50f 1009 sfp->i8count--;
1da177e4 1010 }
1da177e4
LT
1011 xfs_dir2_sf_check(args);
1012 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1013 return 0;
1014}
1015
93597ae8 1016/*
1017 * Check whether the sf dir replace operation need more blocks.
1018 */
6da1b4b1 1019static bool
93597ae8 1020xfs_dir2_sf_replace_needblock(
1021 struct xfs_inode *dp,
1022 xfs_ino_t inum)
1023{
1024 int newsize;
1025 struct xfs_dir2_sf_hdr *sfp;
1026
f7e67b20 1027 if (dp->i_df.if_format != XFS_DINODE_FMT_LOCAL)
93597ae8 1028 return false;
1029
1030 sfp = (struct xfs_dir2_sf_hdr *)dp->i_df.if_u1.if_data;
1031 newsize = dp->i_df.if_bytes + (sfp->count + 1) * XFS_INO64_DIFF;
1032
1033 return inum > XFS_DIR2_MAX_SHORT_INUM &&
1034 sfp->i8count == 0 && newsize > XFS_IFORK_DSIZE(dp);
1035}
1036
1da177e4
LT
1037/*
1038 * Replace the inode number of an entry in a shortform directory.
1039 */
1040int /* error */
1041xfs_dir2_sf_replace(
1042 xfs_da_args_t *args) /* operation arguments */
1043{
50f6bb6b
CH
1044 struct xfs_inode *dp = args->dp;
1045 struct xfs_mount *mp = dp->i_mount;
1da177e4 1046 int i; /* entry index */
1da177e4 1047 xfs_ino_t ino=0; /* entry old inode number */
1da177e4 1048 int i8elevated; /* sf_toino8 set i8count=1 */
1da177e4 1049 xfs_dir2_sf_entry_t *sfep; /* shortform directory entry */
ac8ba50f 1050 xfs_dir2_sf_hdr_t *sfp; /* shortform structure */
1da177e4 1051
0b1b213f
CH
1052 trace_xfs_dir2_sf_replace(args);
1053
0779f4a6 1054 ASSERT(dp->i_df.if_format == XFS_DINODE_FMT_LOCAL);
13d2c10b
CH
1055 ASSERT(dp->i_disk_size >= offsetof(struct xfs_dir2_sf_hdr, parent));
1056 ASSERT(dp->i_df.if_bytes == dp->i_disk_size);
1da177e4 1057 ASSERT(dp->i_df.if_u1.if_data != NULL);
ac8ba50f 1058 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
13d2c10b 1059 ASSERT(dp->i_disk_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
d5cf09ba 1060
1da177e4
LT
1061 /*
1062 * New inode number is large, and need to convert to 8-byte inodes.
1063 */
ac8ba50f 1064 if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
1da177e4 1065 int error; /* error return value */
1da177e4 1066
1da177e4
LT
1067 /*
1068 * Won't fit as shortform, convert to block then do replace.
1069 */
93597ae8 1070 if (xfs_dir2_sf_replace_needblock(dp, args->inumber)) {
1da177e4 1071 error = xfs_dir2_sf_to_block(args);
93597ae8 1072 if (error)
1da177e4 1073 return error;
1da177e4
LT
1074 return xfs_dir2_block_replace(args);
1075 }
1076 /*
1077 * Still fits, convert to 8-byte now.
1078 */
1079 xfs_dir2_sf_toino8(args);
1080 i8elevated = 1;
ac8ba50f 1081 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1da177e4
LT
1082 } else
1083 i8elevated = 0;
d5cf09ba 1084
1da177e4
LT
1085 ASSERT(args->namelen != 1 || args->name[0] != '.');
1086 /*
1087 * Replace ..'s entry.
1088 */
1089 if (args->namelen == 2 &&
1090 args->name[0] == '.' && args->name[1] == '.') {
84915e1b 1091 ino = xfs_dir2_sf_get_parent_ino(sfp);
1da177e4 1092 ASSERT(args->inumber != ino);
84915e1b 1093 xfs_dir2_sf_put_parent_ino(sfp, args->inumber);
1da177e4
LT
1094 }
1095 /*
1096 * Normal entry, look for the name.
1097 */
1098 else {
0cb97766 1099 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
50f6bb6b 1100 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep)) {
5163f95a
BN
1101 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
1102 XFS_CMP_EXACT) {
93b1e96a 1103 ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
1da177e4 1104 ASSERT(args->inumber != ino);
93b1e96a
CH
1105 xfs_dir2_sf_put_ino(mp, sfp, sfep,
1106 args->inumber);
4501ed2a 1107 xfs_dir2_sf_put_ftype(mp, sfep, args->filetype);
1da177e4
LT
1108 break;
1109 }
1110 }
1111 /*
1112 * Didn't find it.
1113 */
ac8ba50f 1114 if (i == sfp->count) {
6a178100 1115 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1da177e4
LT
1116 if (i8elevated)
1117 xfs_dir2_sf_toino4(args);
2451337d 1118 return -ENOENT;
1da177e4
LT
1119 }
1120 }
1da177e4
LT
1121 /*
1122 * See if the old number was large, the new number is small.
1123 */
1124 if (ino > XFS_DIR2_MAX_SHORT_INUM &&
1125 args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
1126 /*
1127 * And the old count was one, so need to convert to small.
1128 */
ac8ba50f 1129 if (sfp->i8count == 1)
1da177e4
LT
1130 xfs_dir2_sf_toino4(args);
1131 else
ac8ba50f 1132 sfp->i8count--;
1da177e4
LT
1133 }
1134 /*
1135 * See if the old number was small, the new number is large.
1136 */
1137 if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
1138 args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
1139 /*
1140 * add to the i8count unless we just converted to 8-byte
1141 * inodes (which does an implied i8count = 1)
1142 */
ac8ba50f 1143 ASSERT(sfp->i8count != 0);
1da177e4 1144 if (!i8elevated)
ac8ba50f 1145 sfp->i8count++;
1da177e4 1146 }
1da177e4
LT
1147 xfs_dir2_sf_check(args);
1148 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
1149 return 0;
1150}
1151
1da177e4
LT
1152/*
1153 * Convert from 8-byte inode numbers to 4-byte inode numbers.
1154 * The last 8-byte inode number is gone, but the count is still 1.
1155 */
1156static void
1157xfs_dir2_sf_toino4(
1158 xfs_da_args_t *args) /* operation arguments */
1159{
50f6bb6b
CH
1160 struct xfs_inode *dp = args->dp;
1161 struct xfs_mount *mp = dp->i_mount;
1da177e4 1162 char *buf; /* old dir's buffer */
1da177e4 1163 int i; /* entry index */
1da177e4
LT
1164 int newsize; /* new inode size */
1165 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
ac8ba50f 1166 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1da177e4
LT
1167 int oldsize; /* old inode size */
1168 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
ac8ba50f 1169 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1da177e4 1170
0b1b213f
CH
1171 trace_xfs_dir2_sf_toino4(args);
1172
1da177e4
LT
1173 /*
1174 * Copy the old directory to the buffer.
1175 * Then nuke it from the inode, and add the new buffer to the inode.
1176 * Don't want xfs_idata_realloc copying the data here.
1177 */
1178 oldsize = dp->i_df.if_bytes;
707e0dda 1179 buf = kmem_alloc(oldsize, 0);
ac8ba50f
CH
1180 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1181 ASSERT(oldsfp->i8count == 1);
1da177e4
LT
1182 memcpy(buf, oldsfp, oldsize);
1183 /*
1184 * Compute the new inode size.
1185 */
266b6969 1186 newsize = oldsize - (oldsfp->count + 1) * XFS_INO64_DIFF;
1da177e4
LT
1187 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1188 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1189 /*
1190 * Reset our pointers, the data has moved.
1191 */
ac8ba50f
CH
1192 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1193 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1da177e4
LT
1194 /*
1195 * Fill in the new header.
1196 */
ac8ba50f
CH
1197 sfp->count = oldsfp->count;
1198 sfp->i8count = 0;
84915e1b 1199 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
1da177e4
LT
1200 /*
1201 * Copy the entries field by field.
1202 */
bbaaf538
CH
1203 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1204 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
ac8ba50f 1205 i < sfp->count;
50f6bb6b
CH
1206 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep),
1207 oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep)) {
1da177e4 1208 sfep->namelen = oldsfep->namelen;
8353a649 1209 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1da177e4 1210 memcpy(sfep->name, oldsfep->name, sfep->namelen);
93b1e96a
CH
1211 xfs_dir2_sf_put_ino(mp, sfp, sfep,
1212 xfs_dir2_sf_get_ino(mp, oldsfp, oldsfep));
4501ed2a
CH
1213 xfs_dir2_sf_put_ftype(mp, sfep,
1214 xfs_dir2_sf_get_ftype(mp, oldsfep));
1da177e4
LT
1215 }
1216 /*
1217 * Clean up the inode.
1218 */
f0e2d93c 1219 kmem_free(buf);
13d2c10b 1220 dp->i_disk_size = newsize;
1da177e4
LT
1221 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1222}
1223
1224/*
5e06d148
ES
1225 * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1226 * The new entry w/ an 8-byte inode number is not there yet; we leave with
1227 * i8count set to 1, but no corresponding 8-byte entry.
1da177e4
LT
1228 */
1229static void
1230xfs_dir2_sf_toino8(
1231 xfs_da_args_t *args) /* operation arguments */
1232{
50f6bb6b
CH
1233 struct xfs_inode *dp = args->dp;
1234 struct xfs_mount *mp = dp->i_mount;
1da177e4 1235 char *buf; /* old dir's buffer */
1da177e4 1236 int i; /* entry index */
1da177e4
LT
1237 int newsize; /* new inode size */
1238 xfs_dir2_sf_entry_t *oldsfep; /* old sf entry */
ac8ba50f 1239 xfs_dir2_sf_hdr_t *oldsfp; /* old sf directory */
1da177e4
LT
1240 int oldsize; /* old inode size */
1241 xfs_dir2_sf_entry_t *sfep; /* new sf entry */
ac8ba50f 1242 xfs_dir2_sf_hdr_t *sfp; /* new sf directory */
1da177e4 1243
0b1b213f
CH
1244 trace_xfs_dir2_sf_toino8(args);
1245
1da177e4
LT
1246 /*
1247 * Copy the old directory to the buffer.
1248 * Then nuke it from the inode, and add the new buffer to the inode.
1249 * Don't want xfs_idata_realloc copying the data here.
1250 */
1251 oldsize = dp->i_df.if_bytes;
707e0dda 1252 buf = kmem_alloc(oldsize, 0);
ac8ba50f
CH
1253 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1254 ASSERT(oldsfp->i8count == 0);
1da177e4
LT
1255 memcpy(buf, oldsfp, oldsize);
1256 /*
5e06d148 1257 * Compute the new inode size (nb: entry count + 1 for parent)
1da177e4 1258 */
266b6969 1259 newsize = oldsize + (oldsfp->count + 1) * XFS_INO64_DIFF;
1da177e4
LT
1260 xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1261 xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1262 /*
1263 * Reset our pointers, the data has moved.
1264 */
ac8ba50f
CH
1265 oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1266 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1da177e4
LT
1267 /*
1268 * Fill in the new header.
1269 */
ac8ba50f
CH
1270 sfp->count = oldsfp->count;
1271 sfp->i8count = 1;
84915e1b 1272 xfs_dir2_sf_put_parent_ino(sfp, xfs_dir2_sf_get_parent_ino(oldsfp));
1da177e4
LT
1273 /*
1274 * Copy the entries field by field.
1275 */
bbaaf538
CH
1276 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1277 oldsfep = xfs_dir2_sf_firstentry(oldsfp);
ac8ba50f 1278 i < sfp->count;
50f6bb6b
CH
1279 i++, sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep),
1280 oldsfep = xfs_dir2_sf_nextentry(mp, oldsfp, oldsfep)) {
1da177e4 1281 sfep->namelen = oldsfep->namelen;
8353a649 1282 memcpy(sfep->offset, oldsfep->offset, sizeof(sfep->offset));
1da177e4 1283 memcpy(sfep->name, oldsfep->name, sfep->namelen);
93b1e96a
CH
1284 xfs_dir2_sf_put_ino(mp, sfp, sfep,
1285 xfs_dir2_sf_get_ino(mp, oldsfp, oldsfep));
4501ed2a
CH
1286 xfs_dir2_sf_put_ftype(mp, sfep,
1287 xfs_dir2_sf_get_ftype(mp, oldsfep));
1da177e4
LT
1288 }
1289 /*
1290 * Clean up the inode.
1291 */
f0e2d93c 1292 kmem_free(buf);
13d2c10b 1293 dp->i_disk_size = newsize;
1da177e4
LT
1294 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1295}