File Index
Files and directories
Sign in
FILE

xfs_dir2.c

File preview with syntax highlighting.

linux-6.19.14/fs/xfs/libxfs/xfs_dir2.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
   4 * All Rights Reserved.
   5 */
   6#include "xfs.h"
   7#include "xfs_fs.h"
   8#include "xfs_shared.h"
   9#include "xfs_format.h"
  10#include "xfs_log_format.h"
  11#include "xfs_trans_resv.h"
  12#include "xfs_mount.h"
  13#include "xfs_inode.h"
  14#include "xfs_trans.h"
  15#include "xfs_bmap.h"
  16#include "xfs_dir2.h"
  17#include "xfs_dir2_priv.h"
  18#include "xfs_errortag.h"
  19#include "xfs_error.h"
  20#include "xfs_trace.h"
  21#include "xfs_health.h"
  22#include "xfs_bmap_btree.h"
  23#include "xfs_trans_space.h"
  24#include "xfs_parent.h"
  25#include "xfs_ag.h"
  26#include "xfs_ialloc.h"
  27
  28const struct xfs_name xfs_name_dotdot = {
  29	.name	= (const unsigned char *)"..",
  30	.len	= 2,
  31	.type	= XFS_DIR3_FT_DIR,
  32};
  33
  34const struct xfs_name xfs_name_dot = {
  35	.name	= (const unsigned char *)".",
  36	.len	= 1,
  37	.type	= XFS_DIR3_FT_DIR,
  38};
  39
  40/*
  41 * Convert inode mode to directory entry filetype
  42 */
  43unsigned char
  44xfs_mode_to_ftype(
  45	int		mode)
  46{
  47	switch (mode & S_IFMT) {
  48	case S_IFREG:
  49		return XFS_DIR3_FT_REG_FILE;
  50	case S_IFDIR:
  51		return XFS_DIR3_FT_DIR;
  52	case S_IFCHR:
  53		return XFS_DIR3_FT_CHRDEV;
  54	case S_IFBLK:
  55		return XFS_DIR3_FT_BLKDEV;
  56	case S_IFIFO:
  57		return XFS_DIR3_FT_FIFO;
  58	case S_IFSOCK:
  59		return XFS_DIR3_FT_SOCK;
  60	case S_IFLNK:
  61		return XFS_DIR3_FT_SYMLINK;
  62	default:
  63		return XFS_DIR3_FT_UNKNOWN;
  64	}
  65}
  66
  67/*
  68 * ASCII case-insensitive (ie. A-Z) support for directories that was
  69 * used in IRIX.
  70 */
  71xfs_dahash_t
  72xfs_ascii_ci_hashname(
  73	const struct xfs_name	*name)
  74{
  75	xfs_dahash_t		hash;
  76	int			i;
  77
  78	for (i = 0, hash = 0; i < name->len; i++)
  79		hash = xfs_ascii_ci_xfrm(name->name[i]) ^ rol32(hash, 7);
  80
  81	return hash;
  82}
  83
  84enum xfs_dacmp
  85xfs_ascii_ci_compname(
  86	struct xfs_da_args	*args,
  87	const unsigned char	*name,
  88	int			len)
  89{
  90	enum xfs_dacmp		result;
  91	int			i;
  92
  93	if (args->namelen != len)
  94		return XFS_CMP_DIFFERENT;
  95
  96	result = XFS_CMP_EXACT;
  97	for (i = 0; i < len; i++) {
  98		if (args->name[i] == name[i])
  99			continue;
 100		if (xfs_ascii_ci_xfrm(args->name[i]) !=
 101		    xfs_ascii_ci_xfrm(name[i]))
 102			return XFS_CMP_DIFFERENT;
 103		result = XFS_CMP_CASE;
 104	}
 105
 106	return result;
 107}
 108
 109int
 110xfs_da_mount(
 111	struct xfs_mount	*mp)
 112{
 113	struct xfs_da_geometry	*dageo;
 114
 115
 116	ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT);
 117	ASSERT(xfs_dir2_dirblock_bytes(&mp->m_sb) <= XFS_MAX_BLOCKSIZE);
 118
 119	mp->m_dir_geo = kzalloc(sizeof(struct xfs_da_geometry),
 120				GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 121	mp->m_attr_geo = kzalloc(sizeof(struct xfs_da_geometry),
 122				GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 123	if (!mp->m_dir_geo || !mp->m_attr_geo) {
 124		kfree(mp->m_dir_geo);
 125		kfree(mp->m_attr_geo);
 126		return -ENOMEM;
 127	}
 128
 129	/* set up directory geometry */
 130	dageo = mp->m_dir_geo;
 131	dageo->blklog = mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog;
 132	dageo->fsblog = mp->m_sb.sb_blocklog;
 133	dageo->blksize = xfs_dir2_dirblock_bytes(&mp->m_sb);
 134	dageo->fsbcount = 1 << mp->m_sb.sb_dirblklog;
 135	if (xfs_has_crc(mp)) {
 136		dageo->node_hdr_size = sizeof(struct xfs_da3_node_hdr);
 137		dageo->leaf_hdr_size = sizeof(struct xfs_dir3_leaf_hdr);
 138		dageo->free_hdr_size = sizeof(struct xfs_dir3_free_hdr);
 139		dageo->data_entry_offset =
 140				sizeof(struct xfs_dir3_data_hdr);
 141	} else {
 142		dageo->node_hdr_size = sizeof(struct xfs_da_node_hdr);
 143		dageo->leaf_hdr_size = sizeof(struct xfs_dir2_leaf_hdr);
 144		dageo->free_hdr_size = sizeof(struct xfs_dir2_free_hdr);
 145		dageo->data_entry_offset =
 146				sizeof(struct xfs_dir2_data_hdr);
 147	}
 148	dageo->leaf_max_ents = (dageo->blksize - dageo->leaf_hdr_size) /
 149			sizeof(struct xfs_dir2_leaf_entry);
 150	dageo->free_max_bests = (dageo->blksize - dageo->free_hdr_size) /
 151			sizeof(xfs_dir2_data_off_t);
 152
 153	dageo->data_first_offset = dageo->data_entry_offset +
 154			xfs_dir2_data_entsize(mp, 1) +
 155			xfs_dir2_data_entsize(mp, 2);
 156
 157	/*
 158	 * Now we've set up the block conversion variables, we can calculate the
 159	 * segment block constants using the geometry structure.
 160	 */
 161	dageo->datablk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_DATA_OFFSET);
 162	dageo->leafblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_LEAF_OFFSET);
 163	dageo->freeblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_FREE_OFFSET);
 164	dageo->node_ents = (dageo->blksize - dageo->node_hdr_size) /
 165				(uint)sizeof(xfs_da_node_entry_t);
 166	dageo->max_extents = (XFS_DIR2_MAX_SPACES * XFS_DIR2_SPACE_SIZE) >>
 167					mp->m_sb.sb_blocklog;
 168	dageo->magicpct = (dageo->blksize * 37) / 100;
 169
 170	/* set up attribute geometry - single fsb only */
 171	dageo = mp->m_attr_geo;
 172	dageo->blklog = mp->m_sb.sb_blocklog;
 173	dageo->fsblog = mp->m_sb.sb_blocklog;
 174	dageo->blksize = 1 << dageo->blklog;
 175	dageo->fsbcount = 1;
 176	dageo->node_hdr_size = mp->m_dir_geo->node_hdr_size;
 177	dageo->node_ents = (dageo->blksize - dageo->node_hdr_size) /
 178				(uint)sizeof(xfs_da_node_entry_t);
 179
 180	if (xfs_has_large_extent_counts(mp))
 181		dageo->max_extents = XFS_MAX_EXTCNT_ATTR_FORK_LARGE;
 182	else
 183		dageo->max_extents = XFS_MAX_EXTCNT_ATTR_FORK_SMALL;
 184
 185	dageo->magicpct = (dageo->blksize * 37) / 100;
 186	return 0;
 187}
 188
 189void
 190xfs_da_unmount(
 191	struct xfs_mount	*mp)
 192{
 193	kfree(mp->m_dir_geo);
 194	kfree(mp->m_attr_geo);
 195}
 196
 197/*
 198 * Return 1 if directory contains only "." and "..".
 199 */
 200static bool
 201xfs_dir_isempty(
 202	xfs_inode_t	*dp)
 203{
 204	xfs_dir2_sf_hdr_t	*sfp;
 205
 206	ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
 207	if (dp->i_disk_size == 0)	/* might happen during shutdown. */
 208		return true;
 209	if (dp->i_disk_size > xfs_inode_data_fork_size(dp))
 210		return false;
 211	sfp = dp->i_df.if_data;
 212	return !sfp->count;
 213}
 214
 215/*
 216 * Validate a given inode number.
 217 */
 218int
 219xfs_dir_ino_validate(
 220	xfs_mount_t	*mp,
 221	xfs_ino_t	ino)
 222{
 223	bool		ino_ok = xfs_verify_dir_ino(mp, ino);
 224
 225	if (XFS_IS_CORRUPT(mp, !ino_ok) ||
 226	    XFS_TEST_ERROR(mp, XFS_ERRTAG_DIR_INO_VALIDATE)) {
 227		xfs_warn(mp, "Invalid inode number 0x%Lx",
 228				(unsigned long long) ino);
 229		return -EFSCORRUPTED;
 230	}
 231	return 0;
 232}
 233
 234/*
 235 * Initialize a directory with its "." and ".." entries.
 236 */
 237int
 238xfs_dir_init(
 239	xfs_trans_t	*tp,
 240	xfs_inode_t	*dp,
 241	xfs_inode_t	*pdp)
 242{
 243	struct xfs_da_args *args;
 244	int		error;
 245
 246	ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
 247	error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino);
 248	if (error)
 249		return error;
 250
 251	args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
 252	if (!args)
 253		return -ENOMEM;
 254
 255	args->geo = dp->i_mount->m_dir_geo;
 256	args->dp = dp;
 257	args->trans = tp;
 258	args->owner = dp->i_ino;
 259	error = xfs_dir2_sf_create(args, pdp->i_ino);
 260	kfree(args);
 261	return error;
 262}
 263
 264enum xfs_dir2_fmt
 265xfs_dir2_format(
 266	struct xfs_da_args	*args,
 267	int			*error)
 268{
 269	struct xfs_inode	*dp = args->dp;
 270	struct xfs_mount	*mp = dp->i_mount;
 271	struct xfs_da_geometry	*geo = mp->m_dir_geo;
 272	xfs_fileoff_t		eof;
 273
 274	xfs_assert_ilocked(dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
 275
 276	*error = 0;
 277	if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL)
 278		return XFS_DIR2_FMT_SF;
 279
 280	*error = xfs_bmap_last_offset(dp, &eof, XFS_DATA_FORK);
 281	if (*error)
 282		return XFS_DIR2_FMT_ERROR;
 283
 284	if (eof == XFS_B_TO_FSB(mp, geo->blksize)) {
 285		if (XFS_IS_CORRUPT(mp, dp->i_disk_size != geo->blksize)) {
 286			xfs_da_mark_sick(args);
 287			*error = -EFSCORRUPTED;
 288			return XFS_DIR2_FMT_ERROR;
 289		}
 290		return XFS_DIR2_FMT_BLOCK;
 291	}
 292	if (eof == geo->leafblk + geo->fsbcount)
 293		return XFS_DIR2_FMT_LEAF;
 294	return XFS_DIR2_FMT_NODE;
 295}
 296
 297int
 298xfs_dir_createname_args(
 299	struct xfs_da_args	*args)
 300{
 301	int			error;
 302
 303	if (!args->inumber)
 304		args->op_flags |= XFS_DA_OP_JUSTCHECK;
 305
 306	switch (xfs_dir2_format(args, &error)) {
 307	case XFS_DIR2_FMT_SF:
 308		return xfs_dir2_sf_addname(args);
 309	case XFS_DIR2_FMT_BLOCK:
 310		return xfs_dir2_block_addname(args);
 311	case XFS_DIR2_FMT_LEAF:
 312		return xfs_dir2_leaf_addname(args);
 313	case XFS_DIR2_FMT_NODE:
 314		return xfs_dir2_node_addname(args);
 315	default:
 316		return error;
 317	}
 318}
 319
 320/*
 321 * Enter a name in a directory, or check for available space.
 322 * If inum is 0, only the available space test is performed.
 323 */
 324int
 325xfs_dir_createname(
 326	struct xfs_trans	*tp,
 327	struct xfs_inode	*dp,
 328	const struct xfs_name	*name,
 329	xfs_ino_t		inum,		/* new entry inode number */
 330	xfs_extlen_t		total)		/* bmap's total block count */
 331{
 332	struct xfs_da_args	*args;
 333	int			rval;
 334
 335	ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
 336
 337	if (inum) {
 338		rval = xfs_dir_ino_validate(tp->t_mountp, inum);
 339		if (rval)
 340			return rval;
 341		XFS_STATS_INC(dp->i_mount, xs_dir_create);
 342	}
 343
 344	args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
 345	if (!args)
 346		return -ENOMEM;
 347
 348	args->geo = dp->i_mount->m_dir_geo;
 349	args->name = name->name;
 350	args->namelen = name->len;
 351	args->filetype = name->type;
 352	args->hashval = xfs_dir2_hashname(dp->i_mount, name);
 353	args->inumber = inum;
 354	args->dp = dp;
 355	args->total = total;
 356	args->whichfork = XFS_DATA_FORK;
 357	args->trans = tp;
 358	args->op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
 359	args->owner = dp->i_ino;
 360
 361	rval = xfs_dir_createname_args(args);
 362	kfree(args);
 363	return rval;
 364}
 365
 366/*
 367 * If doing a CI lookup and case-insensitive match, dup actual name into
 368 * args.value. Return EEXIST for success (ie. name found) or an error.
 369 */
 370int
 371xfs_dir_cilookup_result(
 372	struct xfs_da_args *args,
 373	const unsigned char *name,
 374	int		len)
 375{
 376	if (args->cmpresult == XFS_CMP_DIFFERENT)
 377		return -ENOENT;
 378	if (args->cmpresult != XFS_CMP_CASE ||
 379					!(args->op_flags & XFS_DA_OP_CILOOKUP))
 380		return -EEXIST;
 381
 382	args->value = kmemdup(name, len,
 383			GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_RETRY_MAYFAIL);
 384	if (!args->value)
 385		return -ENOMEM;
 386
 387	args->valuelen = len;
 388	return -EEXIST;
 389}
 390
 391int
 392xfs_dir_lookup_args(
 393	struct xfs_da_args	*args)
 394{
 395	int			error;
 396
 397	switch (xfs_dir2_format(args, &error)) {
 398	case XFS_DIR2_FMT_SF:
 399		error = xfs_dir2_sf_lookup(args);
 400		break;
 401	case XFS_DIR2_FMT_BLOCK:
 402		error = xfs_dir2_block_lookup(args);
 403		break;
 404	case XFS_DIR2_FMT_LEAF:
 405		error = xfs_dir2_leaf_lookup(args);
 406		break;
 407	case XFS_DIR2_FMT_NODE:
 408		error = xfs_dir2_node_lookup(args);
 409		break;
 410	default:
 411		break;
 412	}
 413
 414	if (error != -EEXIST)
 415		return error;
 416	return 0;
 417}
 418
 419/*
 420 * Lookup a name in a directory, give back the inode number.
 421 * If ci_name is not NULL, returns the actual name in ci_name if it differs
 422 * to name, or ci_name->name is set to NULL for an exact match.
 423 */
 424
 425int
 426xfs_dir_lookup(
 427	struct xfs_trans	*tp,
 428	struct xfs_inode	*dp,
 429	const struct xfs_name	*name,
 430	xfs_ino_t		*inum,	  /* out: inode number */
 431	struct xfs_name		*ci_name) /* out: actual name if CI match */
 432{
 433	struct xfs_da_args	*args;
 434	int			rval;
 435	int			lock_mode;
 436
 437	ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
 438	XFS_STATS_INC(dp->i_mount, xs_dir_lookup);
 439
 440	args = kzalloc(sizeof(*args),
 441			GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
 442	args->geo = dp->i_mount->m_dir_geo;
 443	args->name = name->name;
 444	args->namelen = name->len;
 445	args->filetype = name->type;
 446	args->hashval = xfs_dir2_hashname(dp->i_mount, name);
 447	args->dp = dp;
 448	args->whichfork = XFS_DATA_FORK;
 449	args->trans = tp;
 450	args->op_flags = XFS_DA_OP_OKNOENT;
 451	args->owner = dp->i_ino;
 452	if (ci_name)
 453		args->op_flags |= XFS_DA_OP_CILOOKUP;
 454
 455	lock_mode = xfs_ilock_data_map_shared(dp);
 456	rval = xfs_dir_lookup_args(args);
 457	if (!rval) {
 458		*inum = args->inumber;
 459		if (ci_name) {
 460			ci_name->name = args->value;
 461			ci_name->len = args->valuelen;
 462		}
 463	}
 464	xfs_iunlock(dp, lock_mode);
 465	kfree(args);
 466	return rval;
 467}
 468
 469int
 470xfs_dir_removename_args(
 471	struct xfs_da_args	*args)
 472{
 473	int			error;
 474
 475	switch (xfs_dir2_format(args, &error)) {
 476	case XFS_DIR2_FMT_SF:
 477		return xfs_dir2_sf_removename(args);
 478	case XFS_DIR2_FMT_BLOCK:
 479		return xfs_dir2_block_removename(args);
 480	case XFS_DIR2_FMT_LEAF:
 481		return xfs_dir2_leaf_removename(args);
 482	case XFS_DIR2_FMT_NODE:
 483		return xfs_dir2_node_removename(args);
 484	default:
 485		return error;
 486	}
 487}
 488
 489/*
 490 * Remove an entry from a directory.
 491 */
 492int
 493xfs_dir_removename(
 494	struct xfs_trans	*tp,
 495	struct xfs_inode	*dp,
 496	const struct xfs_name	*name,
 497	xfs_ino_t		ino,
 498	xfs_extlen_t		total)		/* bmap's total block count */
 499{
 500	struct xfs_da_args	*args;
 501	int			rval;
 502
 503	ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
 504	XFS_STATS_INC(dp->i_mount, xs_dir_remove);
 505
 506	args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
 507	if (!args)
 508		return -ENOMEM;
 509
 510	args->geo = dp->i_mount->m_dir_geo;
 511	args->name = name->name;
 512	args->namelen = name->len;
 513	args->filetype = name->type;
 514	args->hashval = xfs_dir2_hashname(dp->i_mount, name);
 515	args->inumber = ino;
 516	args->dp = dp;
 517	args->total = total;
 518	args->whichfork = XFS_DATA_FORK;
 519	args->trans = tp;
 520	args->owner = dp->i_ino;
 521	rval = xfs_dir_removename_args(args);
 522	kfree(args);
 523	return rval;
 524}
 525
 526int
 527xfs_dir_replace_args(
 528	struct xfs_da_args	*args)
 529{
 530	int			error;
 531
 532	switch (xfs_dir2_format(args, &error)) {
 533	case XFS_DIR2_FMT_SF:
 534		return xfs_dir2_sf_replace(args);
 535	case XFS_DIR2_FMT_BLOCK:
 536		return xfs_dir2_block_replace(args);
 537	case XFS_DIR2_FMT_LEAF:
 538		return xfs_dir2_leaf_replace(args);
 539	case XFS_DIR2_FMT_NODE:
 540		return xfs_dir2_node_replace(args);
 541	default:
 542		return error;
 543	}
 544}
 545
 546/*
 547 * Replace the inode number of a directory entry.
 548 */
 549int
 550xfs_dir_replace(
 551	struct xfs_trans	*tp,
 552	struct xfs_inode	*dp,
 553	const struct xfs_name	*name,		/* name of entry to replace */
 554	xfs_ino_t		inum,		/* new inode number */
 555	xfs_extlen_t		total)		/* bmap's total block count */
 556{
 557	struct xfs_da_args	*args;
 558	int			rval;
 559
 560	ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
 561
 562	rval = xfs_dir_ino_validate(tp->t_mountp, inum);
 563	if (rval)
 564		return rval;
 565
 566	args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
 567	if (!args)
 568		return -ENOMEM;
 569
 570	args->geo = dp->i_mount->m_dir_geo;
 571	args->name = name->name;
 572	args->namelen = name->len;
 573	args->filetype = name->type;
 574	args->hashval = xfs_dir2_hashname(dp->i_mount, name);
 575	args->inumber = inum;
 576	args->dp = dp;
 577	args->total = total;
 578	args->whichfork = XFS_DATA_FORK;
 579	args->trans = tp;
 580	args->owner = dp->i_ino;
 581	rval = xfs_dir_replace_args(args);
 582	kfree(args);
 583	return rval;
 584}
 585
 586/*
 587 * See if this entry can be added to the directory without allocating space.
 588 */
 589int
 590xfs_dir_canenter(
 591	struct xfs_trans	*tp,
 592	struct xfs_inode	*dp,
 593	const struct xfs_name	*name)		/* name of entry to add */
 594{
 595	return xfs_dir_createname(tp, dp, name, 0, 0);
 596}
 597
 598/*
 599 * Utility routines.
 600 */
 601
 602/*
 603 * Add a block to the directory.
 604 *
 605 * This routine is for data and free blocks, not leaf/node blocks which are
 606 * handled by xfs_da_grow_inode.
 607 */
 608int
 609xfs_dir2_grow_inode(
 610	struct xfs_da_args	*args,
 611	int			space,	/* v2 dir's space XFS_DIR2_xxx_SPACE */
 612	xfs_dir2_db_t		*dbp)	/* out: block number added */
 613{
 614	struct xfs_inode	*dp = args->dp;
 615	struct xfs_mount	*mp = dp->i_mount;
 616	xfs_fileoff_t		bno;	/* directory offset of new block */
 617	int			count;	/* count of filesystem blocks */
 618	int			error;
 619
 620	trace_xfs_dir2_grow_inode(args, space);
 621
 622	/*
 623	 * Set lowest possible block in the space requested.
 624	 */
 625	bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
 626	count = args->geo->fsbcount;
 627
 628	error = xfs_da_grow_inode_int(args, &bno, count);
 629	if (error)
 630		return error;
 631
 632	*dbp = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)bno);
 633
 634	/*
 635	 * Update file's size if this is the data space and it grew.
 636	 */
 637	if (space == XFS_DIR2_DATA_SPACE) {
 638		xfs_fsize_t	size;		/* directory file (data) size */
 639
 640		size = XFS_FSB_TO_B(mp, bno + count);
 641		if (size > dp->i_disk_size) {
 642			dp->i_disk_size = size;
 643			xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
 644		}
 645	}
 646	return 0;
 647}
 648
 649/*
 650 * Remove the given block from the directory.
 651 * This routine is used for data and free blocks, leaf/node are done
 652 * by xfs_da_shrink_inode.
 653 */
 654int
 655xfs_dir2_shrink_inode(
 656	struct xfs_da_args	*args,
 657	xfs_dir2_db_t		db,
 658	struct xfs_buf		*bp)
 659{
 660	xfs_fileoff_t		bno;		/* directory file offset */
 661	xfs_dablk_t		da;		/* directory file offset */
 662	int			done;		/* bunmap is finished */
 663	struct xfs_inode	*dp;
 664	int			error;
 665	struct xfs_mount	*mp;
 666	struct xfs_trans	*tp;
 667
 668	trace_xfs_dir2_shrink_inode(args, db);
 669
 670	dp = args->dp;
 671	mp = dp->i_mount;
 672	tp = args->trans;
 673	da = xfs_dir2_db_to_da(args->geo, db);
 674
 675	/* Unmap the fsblock(s). */
 676	error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount, 0, 0, &done);
 677	if (error) {
 678		/*
 679		 * ENOSPC actually can happen if we're in a removename with no
 680		 * space reservation, and the resulting block removal would
 681		 * cause a bmap btree split or conversion from extents to btree.
 682		 * This can only happen for un-fragmented directory blocks,
 683		 * since you need to be punching out the middle of an extent.
 684		 * In this case we need to leave the block in the file, and not
 685		 * binval it.  So the block has to be in a consistent empty
 686		 * state and appropriately logged.  We don't free up the buffer,
 687		 * the caller can tell it hasn't happened since it got an error
 688		 * back.
 689		 */
 690		return error;
 691	}
 692	ASSERT(done);
 693	/*
 694	 * Invalidate the buffer from the transaction.
 695	 */
 696	xfs_trans_binval(tp, bp);
 697	/*
 698	 * If it's not a data block, we're done.
 699	 */
 700	if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET))
 701		return 0;
 702	/*
 703	 * If the block isn't the last one in the directory, we're done.
 704	 */
 705	if (dp->i_disk_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0))
 706		return 0;
 707	bno = da;
 708	if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
 709		/*
 710		 * This can't really happen unless there's kernel corruption.
 711		 */
 712		return error;
 713	}
 714	if (db == args->geo->datablk)
 715		ASSERT(bno == 0);
 716	else
 717		ASSERT(bno > 0);
 718	/*
 719	 * Set the size to the new last block.
 720	 */
 721	dp->i_disk_size = XFS_FSB_TO_B(mp, bno);
 722	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
 723	return 0;
 724}
 725
 726/* Returns true if the directory entry name is valid. */
 727bool
 728xfs_dir2_namecheck(
 729	const void	*name,
 730	size_t		length)
 731{
 732	/*
 733	 * MAXNAMELEN includes the trailing null, but (name/length) leave it
 734	 * out, so use >= for the length check.
 735	 */
 736	if (length >= MAXNAMELEN)
 737		return false;
 738
 739	/* There shouldn't be any slashes or nulls here */
 740	return !memchr(name, '/', length) && !memchr(name, 0, length);
 741}
 742
 743xfs_dahash_t
 744xfs_dir2_hashname(
 745	struct xfs_mount	*mp,
 746	const struct xfs_name	*name)
 747{
 748	if (unlikely(xfs_has_asciici(mp)))
 749		return xfs_ascii_ci_hashname(name);
 750	return xfs_da_hashname(name->name, name->len);
 751}
 752
 753enum xfs_dacmp
 754xfs_dir2_compname(
 755	struct xfs_da_args	*args,
 756	const unsigned char	*name,
 757	int			len)
 758{
 759	if (unlikely(xfs_has_asciici(args->dp->i_mount)))
 760		return xfs_ascii_ci_compname(args, name, len);
 761	return xfs_da_compname(args, name, len);
 762}
 763
 764#ifdef CONFIG_XFS_LIVE_HOOKS
 765/*
 766 * Use a static key here to reduce the overhead of directory live update hooks.
 767 * If the compiler supports jump labels, the static branch will be replaced by
 768 * a nop sled when there are no hook users.  Online fsck is currently the only
 769 * caller, so this is a reasonable tradeoff.
 770 *
 771 * Note: Patching the kernel code requires taking the cpu hotplug lock.  Other
 772 * parts of the kernel allocate memory with that lock held, which means that
 773 * XFS callers cannot hold any locks that might be used by memory reclaim or
 774 * writeback when calling the static_branch_{inc,dec} functions.
 775 */
 776DEFINE_STATIC_XFS_HOOK_SWITCH(xfs_dir_hooks_switch);
 777
 778void
 779xfs_dir_hook_disable(void)
 780{
 781	xfs_hooks_switch_off(&xfs_dir_hooks_switch);
 782}
 783
 784void
 785xfs_dir_hook_enable(void)
 786{
 787	xfs_hooks_switch_on(&xfs_dir_hooks_switch);
 788}
 789
 790/* Call hooks for a directory update relating to a child dirent update. */
 791inline void
 792xfs_dir_update_hook(
 793	struct xfs_inode		*dp,
 794	struct xfs_inode		*ip,
 795	int				delta,
 796	const struct xfs_name		*name)
 797{
 798	if (xfs_hooks_switched_on(&xfs_dir_hooks_switch)) {
 799		struct xfs_dir_update_params	p = {
 800			.dp		= dp,
 801			.ip		= ip,
 802			.delta		= delta,
 803			.name		= name,
 804		};
 805		struct xfs_mount	*mp = ip->i_mount;
 806
 807		xfs_hooks_call(&mp->m_dir_update_hooks, 0, &p);
 808	}
 809}
 810
 811/* Call the specified function during a directory update. */
 812int
 813xfs_dir_hook_add(
 814	struct xfs_mount	*mp,
 815	struct xfs_dir_hook	*hook)
 816{
 817	return xfs_hooks_add(&mp->m_dir_update_hooks, &hook->dirent_hook);
 818}
 819
 820/* Stop calling the specified function during a directory update. */
 821void
 822xfs_dir_hook_del(
 823	struct xfs_mount	*mp,
 824	struct xfs_dir_hook	*hook)
 825{
 826	xfs_hooks_del(&mp->m_dir_update_hooks, &hook->dirent_hook);
 827}
 828
 829/* Configure directory update hook functions. */
 830void
 831xfs_dir_hook_setup(
 832	struct xfs_dir_hook	*hook,
 833	notifier_fn_t		mod_fn)
 834{
 835	xfs_hook_setup(&hook->dirent_hook, mod_fn);
 836}
 837#endif /* CONFIG_XFS_LIVE_HOOKS */
 838
 839/*
 840 * Given a directory @dp, a newly allocated inode @ip, and a @name, link @ip
 841 * into @dp under the given @name.  If @ip is a directory, it will be
 842 * initialized.  Both inodes must have the ILOCK held and the transaction must
 843 * have sufficient blocks reserved.
 844 */
 845int
 846xfs_dir_create_child(
 847	struct xfs_trans	*tp,
 848	unsigned int		resblks,
 849	struct xfs_dir_update	*du)
 850{
 851	struct xfs_inode	*dp = du->dp;
 852	const struct xfs_name	*name = du->name;
 853	struct xfs_inode	*ip = du->ip;
 854	int			error;
 855
 856	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
 857	xfs_assert_ilocked(dp, XFS_ILOCK_EXCL);
 858
 859	error = xfs_dir_createname(tp, dp, name, ip->i_ino, resblks);
 860	if (error) {
 861		ASSERT(error != -ENOSPC);
 862		return error;
 863	}
 864
 865	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
 866	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
 867
 868	if (S_ISDIR(VFS_I(ip)->i_mode)) {
 869		error = xfs_dir_init(tp, ip, dp);
 870		if (error)
 871			return error;
 872
 873		xfs_bumplink(tp, dp);
 874	}
 875
 876	/*
 877	 * If we have parent pointers, we need to add the attribute containing
 878	 * the parent information now.
 879	 */
 880	if (du->ppargs) {
 881		error = xfs_parent_addname(tp, du->ppargs, dp, name, ip);
 882		if (error)
 883			return error;
 884	}
 885
 886	xfs_dir_update_hook(dp, ip, 1, name);
 887	return 0;
 888}
 889
 890/*
 891 * Given a directory @dp, an existing non-directory inode @ip, and a @name,
 892 * link @ip into @dp under the given @name.  Both inodes must have the ILOCK
 893 * held.
 894 */
 895int
 896xfs_dir_add_child(
 897	struct xfs_trans	*tp,
 898	unsigned int		resblks,
 899	struct xfs_dir_update	*du)
 900{
 901	struct xfs_inode	*dp = du->dp;
 902	const struct xfs_name	*name = du->name;
 903	struct xfs_inode	*ip = du->ip;
 904	struct xfs_mount	*mp = tp->t_mountp;
 905	int			error;
 906
 907	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
 908	xfs_assert_ilocked(dp, XFS_ILOCK_EXCL);
 909	ASSERT(!S_ISDIR(VFS_I(ip)->i_mode));
 910
 911	if (!resblks) {
 912		error = xfs_dir_canenter(tp, dp, name);
 913		if (error)
 914			return error;
 915	}
 916
 917	/*
 918	 * Handle initial link state of O_TMPFILE inode
 919	 */
 920	if (VFS_I(ip)->i_nlink == 0) {
 921		struct xfs_perag	*pag;
 922
 923		pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
 924		error = xfs_iunlink_remove(tp, pag, ip);
 925		xfs_perag_put(pag);
 926		if (error)
 927			return error;
 928	}
 929
 930	error = xfs_dir_createname(tp, dp, name, ip->i_ino, resblks);
 931	if (error)
 932		return error;
 933
 934	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
 935	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
 936
 937	xfs_bumplink(tp, ip);
 938
 939	/*
 940	 * If we have parent pointers, we now need to add the parent record to
 941	 * the attribute fork of the inode. If this is the initial parent
 942	 * attribute, we need to create it correctly, otherwise we can just add
 943	 * the parent to the inode.
 944	 */
 945	if (du->ppargs) {
 946		error = xfs_parent_addname(tp, du->ppargs, dp, name, ip);
 947		if (error)
 948			return error;
 949	}
 950
 951	xfs_dir_update_hook(dp, ip, 1, name);
 952	return 0;
 953}
 954
 955/*
 956 * Given a directory @dp, a child @ip, and a @name, remove the (@name, @ip)
 957 * entry from the directory.  Both inodes must have the ILOCK held.
 958 */
 959int
 960xfs_dir_remove_child(
 961	struct xfs_trans	*tp,
 962	unsigned int		resblks,
 963	struct xfs_dir_update	*du)
 964{
 965	struct xfs_inode	*dp = du->dp;
 966	const struct xfs_name	*name = du->name;
 967	struct xfs_inode	*ip = du->ip;
 968	int			error;
 969
 970	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
 971	xfs_assert_ilocked(dp, XFS_ILOCK_EXCL);
 972
 973	/*
 974	 * If we're removing a directory perform some additional validation.
 975	 */
 976	if (S_ISDIR(VFS_I(ip)->i_mode)) {
 977		ASSERT(VFS_I(ip)->i_nlink >= 2);
 978		if (VFS_I(ip)->i_nlink != 2)
 979			return -ENOTEMPTY;
 980		if (!xfs_dir_isempty(ip))
 981			return -ENOTEMPTY;
 982
 983		/* Drop the link from ip's "..".  */
 984		error = xfs_droplink(tp, dp);
 985		if (error)
 986			return error;
 987
 988		/* Drop the "." link from ip to self.  */
 989		error = xfs_droplink(tp, ip);
 990		if (error)
 991			return error;
 992
 993		/*
 994		 * Point the unlinked child directory's ".." entry to the root
 995		 * directory to eliminate back-references to inodes that may
 996		 * get freed before the child directory is closed.  If the fs
 997		 * gets shrunk, this can lead to dirent inode validation errors.
 998		 */
 999		if (dp->i_ino != tp->t_mountp->m_sb.sb_rootino) {
1000			error = xfs_dir_replace(tp, ip, &xfs_name_dotdot,
1001					tp->t_mountp->m_sb.sb_rootino, 0);
1002			if (error)
1003				return error;
1004		}
1005	} else {
1006		/*
1007		 * When removing a non-directory we need to log the parent
1008		 * inode here.  For a directory this is done implicitly
1009		 * by the xfs_droplink call for the ".." entry.
1010		 */
1011		xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1012	}
1013	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1014
1015	/* Drop the link from dp to ip. */
1016	error = xfs_droplink(tp, ip);
1017	if (error)
1018		return error;
1019
1020	error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks);
1021	if (error) {
1022		ASSERT(error != -ENOENT);
1023		return error;
1024	}
1025
1026	/* Remove parent pointer. */
1027	if (du->ppargs) {
1028		error = xfs_parent_removename(tp, du->ppargs, dp, name, ip);
1029		if (error)
1030			return error;
1031	}
1032
1033	xfs_dir_update_hook(dp, ip, -1, name);
1034	return 0;
1035}
1036
1037/*
1038 * Exchange the entry (@name1, @ip1) in directory @dp1 with the entry (@name2,
1039 * @ip2) in directory @dp2, and update '..' @ip1 and @ip2's entries as needed.
1040 * @ip1 and @ip2 need not be of the same type.
1041 *
1042 * All inodes must have the ILOCK held, and both entries must already exist.
1043 */
1044int
1045xfs_dir_exchange_children(
1046	struct xfs_trans	*tp,
1047	struct xfs_dir_update	*du1,
1048	struct xfs_dir_update	*du2,
1049	unsigned int		spaceres)
1050{
1051	struct xfs_inode	*dp1 = du1->dp;
1052	const struct xfs_name	*name1 = du1->name;
1053	struct xfs_inode	*ip1 = du1->ip;
1054	struct xfs_inode	*dp2 = du2->dp;
1055	const struct xfs_name	*name2 = du2->name;
1056	struct xfs_inode	*ip2 = du2->ip;
1057	int			ip1_flags = 0;
1058	int			ip2_flags = 0;
1059	int			dp2_flags = 0;
1060	int			error;
1061
1062	/* Swap inode number for dirent in first parent */
1063	error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres);
1064	if (error)
1065		return error;
1066
1067	/* Swap inode number for dirent in second parent */
1068	error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres);
1069	if (error)
1070		return error;
1071
1072	/*
1073	 * If we're renaming one or more directories across different parents,
1074	 * update the respective ".." entries (and link counts) to match the new
1075	 * parents.
1076	 */
1077	if (dp1 != dp2) {
1078		dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
1079
1080		if (S_ISDIR(VFS_I(ip2)->i_mode)) {
1081			error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
1082						dp1->i_ino, spaceres);
1083			if (error)
1084				return error;
1085
1086			/* transfer ip2 ".." reference to dp1 */
1087			if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
1088				error = xfs_droplink(tp, dp2);
1089				if (error)
1090					return error;
1091				xfs_bumplink(tp, dp1);
1092			}
1093
1094			/*
1095			 * Although ip1 isn't changed here, userspace needs
1096			 * to be warned about the change, so that applications
1097			 * relying on it (like backup ones), will properly
1098			 * notify the change
1099			 */
1100			ip1_flags |= XFS_ICHGTIME_CHG;
1101			ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
1102		}
1103
1104		if (S_ISDIR(VFS_I(ip1)->i_mode)) {
1105			error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
1106						dp2->i_ino, spaceres);
1107			if (error)
1108				return error;
1109
1110			/* transfer ip1 ".." reference to dp2 */
1111			if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
1112				error = xfs_droplink(tp, dp1);
1113				if (error)
1114					return error;
1115				xfs_bumplink(tp, dp2);
1116			}
1117
1118			/*
1119			 * Although ip2 isn't changed here, userspace needs
1120			 * to be warned about the change, so that applications
1121			 * relying on it (like backup ones), will properly
1122			 * notify the change
1123			 */
1124			ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
1125			ip2_flags |= XFS_ICHGTIME_CHG;
1126		}
1127	}
1128
1129	if (ip1_flags) {
1130		xfs_trans_ichgtime(tp, ip1, ip1_flags);
1131		xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
1132	}
1133	if (ip2_flags) {
1134		xfs_trans_ichgtime(tp, ip2, ip2_flags);
1135		xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
1136	}
1137	if (dp2_flags) {
1138		xfs_trans_ichgtime(tp, dp2, dp2_flags);
1139		xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
1140	}
1141	xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1142	xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
1143
1144	/* Schedule parent pointer replacements */
1145	if (du1->ppargs) {
1146		error = xfs_parent_replacename(tp, du1->ppargs, dp1, name1,
1147				dp2, name2, ip1);
1148		if (error)
1149			return error;
1150	}
1151
1152	if (du2->ppargs) {
1153		error = xfs_parent_replacename(tp, du2->ppargs, dp2, name2,
1154				dp1, name1, ip2);
1155		if (error)
1156			return error;
1157	}
1158
1159	/*
1160	 * Inform our hook clients that we've finished an exchange operation as
1161	 * follows: removed the source and target files from their directories;
1162	 * added the target to the source directory; and added the source to
1163	 * the target directory.  All inodes are locked, so it's ok to model a
1164	 * rename this way so long as we say we deleted entries before we add
1165	 * new ones.
1166	 */
1167	xfs_dir_update_hook(dp1, ip1, -1, name1);
1168	xfs_dir_update_hook(dp2, ip2, -1, name2);
1169	xfs_dir_update_hook(dp1, ip2, 1, name1);
1170	xfs_dir_update_hook(dp2, ip1, 1, name2);
1171	return 0;
1172}
1173
1174/*
1175 * Given an entry (@src_name, @src_ip) in directory @src_dp, make the entry
1176 * @target_name in directory @target_dp point to @src_ip and remove the
1177 * original entry, cleaning up everything left behind.
1178 *
1179 * Cleanup involves dropping a link count on @target_ip, and either removing
1180 * the (@src_name, @src_ip) entry from @src_dp or simply replacing the entry
1181 * with (@src_name, @wip) if a whiteout inode @wip is supplied.
1182 *
1183 * All inodes must have the ILOCK held.  We assume that if @src_ip is a
1184 * directory then its '..' doesn't already point to @target_dp, and that @wip
1185 * is a freshly allocated whiteout.
1186 */
1187int
1188xfs_dir_rename_children(
1189	struct xfs_trans	*tp,
1190	struct xfs_dir_update	*du_src,
1191	struct xfs_dir_update	*du_tgt,
1192	unsigned int		spaceres,
1193	struct xfs_dir_update	*du_wip)
1194{
1195	struct xfs_mount	*mp = tp->t_mountp;
1196	struct xfs_inode	*src_dp = du_src->dp;
1197	const struct xfs_name	*src_name = du_src->name;
1198	struct xfs_inode	*src_ip = du_src->ip;
1199	struct xfs_inode	*target_dp = du_tgt->dp;
1200	const struct xfs_name	*target_name = du_tgt->name;
1201	struct xfs_inode	*target_ip = du_tgt->ip;
1202	bool			new_parent = (src_dp != target_dp);
1203	bool			src_is_directory;
1204	int			error;
1205
1206	src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
1207
1208	/*
1209	 * Check for expected errors before we dirty the transaction
1210	 * so we can return an error without a transaction abort.
1211	 */
1212	if (target_ip == NULL) {
1213		/*
1214		 * If there's no space reservation, check the entry will
1215		 * fit before actually inserting it.
1216		 */
1217		if (!spaceres) {
1218			error = xfs_dir_canenter(tp, target_dp, target_name);
1219			if (error)
1220				return error;
1221		}
1222	} else {
1223		/*
1224		 * If target exists and it's a directory, check that whether
1225		 * it can be destroyed.
1226		 */
1227		if (S_ISDIR(VFS_I(target_ip)->i_mode) &&
1228		    (!xfs_dir_isempty(target_ip) ||
1229		     (VFS_I(target_ip)->i_nlink > 2)))
1230			return -EEXIST;
1231	}
1232
1233	/*
1234	 * Directory entry creation below may acquire the AGF. Remove
1235	 * the whiteout from the unlinked list first to preserve correct
1236	 * AGI/AGF locking order. This dirties the transaction so failures
1237	 * after this point will abort and log recovery will clean up the
1238	 * mess.
1239	 *
1240	 * For whiteouts, we need to bump the link count on the whiteout
1241	 * inode. After this point, we have a real link, clear the tmpfile
1242	 * state flag from the inode so it doesn't accidentally get misused
1243	 * in future.
1244	 */
1245	if (du_wip->ip) {
1246		struct xfs_perag	*pag;
1247
1248		ASSERT(VFS_I(du_wip->ip)->i_nlink == 0);
1249
1250		pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, du_wip->ip->i_ino));
1251		error = xfs_iunlink_remove(tp, pag, du_wip->ip);
1252		xfs_perag_put(pag);
1253		if (error)
1254			return error;
1255
1256		xfs_bumplink(tp, du_wip->ip);
1257	}
1258
1259	/*
1260	 * Set up the target.
1261	 */
1262	if (target_ip == NULL) {
1263		/*
1264		 * If target does not exist and the rename crosses
1265		 * directories, adjust the target directory link count
1266		 * to account for the ".." reference from the new entry.
1267		 */
1268		error = xfs_dir_createname(tp, target_dp, target_name,
1269					   src_ip->i_ino, spaceres);
1270		if (error)
1271			return error;
1272
1273		xfs_trans_ichgtime(tp, target_dp,
1274					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1275
1276		if (new_parent && src_is_directory) {
1277			xfs_bumplink(tp, target_dp);
1278		}
1279	} else { /* target_ip != NULL */
1280		/*
1281		 * Link the source inode under the target name.
1282		 * If the source inode is a directory and we are moving
1283		 * it across directories, its ".." entry will be
1284		 * inconsistent until we replace that down below.
1285		 *
1286		 * In case there is already an entry with the same
1287		 * name at the destination directory, remove it first.
1288		 */
1289		error = xfs_dir_replace(tp, target_dp, target_name,
1290					src_ip->i_ino, spaceres);
1291		if (error)
1292			return error;
1293
1294		xfs_trans_ichgtime(tp, target_dp,
1295					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1296
1297		/*
1298		 * Decrement the link count on the target since the target
1299		 * dir no longer points to it.
1300		 */
1301		error = xfs_droplink(tp, target_ip);
1302		if (error)
1303			return error;
1304
1305		if (src_is_directory) {
1306			/*
1307			 * Drop the link from the old "." entry.
1308			 */
1309			error = xfs_droplink(tp, target_ip);
1310			if (error)
1311				return error;
1312		}
1313	} /* target_ip != NULL */
1314
1315	/*
1316	 * Remove the source.
1317	 */
1318	if (new_parent && src_is_directory) {
1319		/*
1320		 * Rewrite the ".." entry to point to the new
1321		 * directory.
1322		 */
1323		error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
1324					target_dp->i_ino, spaceres);
1325		ASSERT(error != -EEXIST);
1326		if (error)
1327			return error;
1328	}
1329
1330	/*
1331	 * We always want to hit the ctime on the source inode.
1332	 *
1333	 * This isn't strictly required by the standards since the source
1334	 * inode isn't really being changed, but old unix file systems did
1335	 * it and some incremental backup programs won't work without it.
1336	 */
1337	xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
1338	xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
1339
1340	/*
1341	 * Adjust the link count on src_dp.  This is necessary when
1342	 * renaming a directory, either within one parent when
1343	 * the target existed, or across two parent directories.
1344	 */
1345	if (src_is_directory && (new_parent || target_ip != NULL)) {
1346
1347		/*
1348		 * Decrement link count on src_directory since the
1349		 * entry that's moved no longer points to it.
1350		 */
1351		error = xfs_droplink(tp, src_dp);
1352		if (error)
1353			return error;
1354	}
1355
1356	/*
1357	 * For whiteouts, we only need to update the source dirent with the
1358	 * inode number of the whiteout inode rather than removing it
1359	 * altogether.
1360	 */
1361	if (du_wip->ip)
1362		error = xfs_dir_replace(tp, src_dp, src_name, du_wip->ip->i_ino,
1363					spaceres);
1364	else
1365		error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
1366					   spaceres);
1367	if (error)
1368		return error;
1369
1370	xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1371	xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
1372	if (new_parent)
1373		xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
1374
1375	/* Schedule parent pointer updates. */
1376	if (du_wip->ppargs) {
1377		error = xfs_parent_addname(tp, du_wip->ppargs, src_dp,
1378				src_name, du_wip->ip);
1379		if (error)
1380			return error;
1381	}
1382
1383	if (du_src->ppargs) {
1384		error = xfs_parent_replacename(tp, du_src->ppargs, src_dp,
1385				src_name, target_dp, target_name, src_ip);
1386		if (error)
1387			return error;
1388	}
1389
1390	if (du_tgt->ppargs) {
1391		error = xfs_parent_removename(tp, du_tgt->ppargs, target_dp,
1392				target_name, target_ip);
1393		if (error)
1394			return error;
1395	}
1396
1397	/*
1398	 * Inform our hook clients that we've finished a rename operation as
1399	 * follows: removed the source and target files from their directories;
1400	 * that we've added the source to the target directory; and finally
1401	 * that we've added the whiteout, if there was one.  All inodes are
1402	 * locked, so it's ok to model a rename this way so long as we say we
1403	 * deleted entries before we add new ones.
1404	 */
1405	if (target_ip)
1406		xfs_dir_update_hook(target_dp, target_ip, -1, target_name);
1407	xfs_dir_update_hook(src_dp, src_ip, -1, src_name);
1408	xfs_dir_update_hook(target_dp, src_ip, 1, target_name);
1409	if (du_wip->ip)
1410		xfs_dir_update_hook(src_dp, du_wip->ip, 1, src_name);
1411	return 0;
1412}
~/ Read-only file preview.