xref: /linux/fs/xfs/libxfs/xfs_dir2.c (revision 8934827db5403eae57d4537114a9ff88b0a8460f)
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_platform.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 
28 const struct xfs_name xfs_name_dotdot = {
29 	.name	= (const unsigned char *)"..",
30 	.len	= 2,
31 	.type	= XFS_DIR3_FT_DIR,
32 };
33 
34 const 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  */
43 unsigned char
xfs_mode_to_ftype(int mode)44 xfs_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  */
71 xfs_dahash_t
xfs_ascii_ci_hashname(const struct xfs_name * name)72 xfs_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 
84 enum xfs_dacmp
xfs_ascii_ci_compname(struct xfs_da_args * args,const unsigned char * name,int len)85 xfs_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 
109 int
xfs_da_mount(struct xfs_mount * mp)110 xfs_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_obj(struct xfs_da_geometry,
120 				    GFP_KERNEL | __GFP_RETRY_MAYFAIL);
121 	mp->m_attr_geo = kzalloc_obj(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 
189 void
xfs_da_unmount(struct xfs_mount * mp)190 xfs_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  */
200 static bool
xfs_dir_isempty(xfs_inode_t * dp)201 xfs_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  */
218 int
xfs_dir_ino_validate(xfs_mount_t * mp,xfs_ino_t ino)219 xfs_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  */
237 int
xfs_dir_init(xfs_trans_t * tp,xfs_inode_t * dp,xfs_inode_t * pdp)238 xfs_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_obj(*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 
264 enum xfs_dir2_fmt
xfs_dir2_format(struct xfs_da_args * args,int * error)265 xfs_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 
297 int
xfs_dir_createname_args(struct xfs_da_args * args)298 xfs_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  */
324 int
xfs_dir_createname(struct xfs_trans * tp,struct xfs_inode * dp,const struct xfs_name * name,xfs_ino_t inum,xfs_extlen_t total)325 xfs_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_obj(*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  */
370 int
xfs_dir_cilookup_result(struct xfs_da_args * args,const unsigned char * name,int len)371 xfs_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 
391 int
xfs_dir_lookup_args(struct xfs_da_args * args)392 xfs_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 
425 int
xfs_dir_lookup(struct xfs_trans * tp,struct xfs_inode * dp,const struct xfs_name * name,xfs_ino_t * inum,struct xfs_name * ci_name)426 xfs_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_obj(*args, GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
441 	args->geo = dp->i_mount->m_dir_geo;
442 	args->name = name->name;
443 	args->namelen = name->len;
444 	args->filetype = name->type;
445 	args->hashval = xfs_dir2_hashname(dp->i_mount, name);
446 	args->dp = dp;
447 	args->whichfork = XFS_DATA_FORK;
448 	args->trans = tp;
449 	args->op_flags = XFS_DA_OP_OKNOENT;
450 	args->owner = dp->i_ino;
451 	if (ci_name)
452 		args->op_flags |= XFS_DA_OP_CILOOKUP;
453 
454 	lock_mode = xfs_ilock_data_map_shared(dp);
455 	rval = xfs_dir_lookup_args(args);
456 	if (!rval) {
457 		*inum = args->inumber;
458 		if (ci_name) {
459 			ci_name->name = args->value;
460 			ci_name->len = args->valuelen;
461 		}
462 	}
463 	xfs_iunlock(dp, lock_mode);
464 	kfree(args);
465 	return rval;
466 }
467 
468 int
xfs_dir_removename_args(struct xfs_da_args * args)469 xfs_dir_removename_args(
470 	struct xfs_da_args	*args)
471 {
472 	int			error;
473 
474 	switch (xfs_dir2_format(args, &error)) {
475 	case XFS_DIR2_FMT_SF:
476 		return xfs_dir2_sf_removename(args);
477 	case XFS_DIR2_FMT_BLOCK:
478 		return xfs_dir2_block_removename(args);
479 	case XFS_DIR2_FMT_LEAF:
480 		return xfs_dir2_leaf_removename(args);
481 	case XFS_DIR2_FMT_NODE:
482 		return xfs_dir2_node_removename(args);
483 	default:
484 		return error;
485 	}
486 }
487 
488 /*
489  * Remove an entry from a directory.
490  */
491 int
xfs_dir_removename(struct xfs_trans * tp,struct xfs_inode * dp,const struct xfs_name * name,xfs_ino_t ino,xfs_extlen_t total)492 xfs_dir_removename(
493 	struct xfs_trans	*tp,
494 	struct xfs_inode	*dp,
495 	const struct xfs_name	*name,
496 	xfs_ino_t		ino,
497 	xfs_extlen_t		total)		/* bmap's total block count */
498 {
499 	struct xfs_da_args	*args;
500 	int			rval;
501 
502 	ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
503 	XFS_STATS_INC(dp->i_mount, xs_dir_remove);
504 
505 	args = kzalloc_obj(*args, GFP_KERNEL | __GFP_NOFAIL);
506 	if (!args)
507 		return -ENOMEM;
508 
509 	args->geo = dp->i_mount->m_dir_geo;
510 	args->name = name->name;
511 	args->namelen = name->len;
512 	args->filetype = name->type;
513 	args->hashval = xfs_dir2_hashname(dp->i_mount, name);
514 	args->inumber = ino;
515 	args->dp = dp;
516 	args->total = total;
517 	args->whichfork = XFS_DATA_FORK;
518 	args->trans = tp;
519 	args->owner = dp->i_ino;
520 	rval = xfs_dir_removename_args(args);
521 	kfree(args);
522 	return rval;
523 }
524 
525 int
xfs_dir_replace_args(struct xfs_da_args * args)526 xfs_dir_replace_args(
527 	struct xfs_da_args	*args)
528 {
529 	int			error;
530 
531 	switch (xfs_dir2_format(args, &error)) {
532 	case XFS_DIR2_FMT_SF:
533 		return xfs_dir2_sf_replace(args);
534 	case XFS_DIR2_FMT_BLOCK:
535 		return xfs_dir2_block_replace(args);
536 	case XFS_DIR2_FMT_LEAF:
537 		return xfs_dir2_leaf_replace(args);
538 	case XFS_DIR2_FMT_NODE:
539 		return xfs_dir2_node_replace(args);
540 	default:
541 		return error;
542 	}
543 }
544 
545 /*
546  * Replace the inode number of a directory entry.
547  */
548 int
xfs_dir_replace(struct xfs_trans * tp,struct xfs_inode * dp,const struct xfs_name * name,xfs_ino_t inum,xfs_extlen_t total)549 xfs_dir_replace(
550 	struct xfs_trans	*tp,
551 	struct xfs_inode	*dp,
552 	const struct xfs_name	*name,		/* name of entry to replace */
553 	xfs_ino_t		inum,		/* new inode number */
554 	xfs_extlen_t		total)		/* bmap's total block count */
555 {
556 	struct xfs_da_args	*args;
557 	int			rval;
558 
559 	ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
560 
561 	rval = xfs_dir_ino_validate(tp->t_mountp, inum);
562 	if (rval)
563 		return rval;
564 
565 	args = kzalloc_obj(*args, GFP_KERNEL | __GFP_NOFAIL);
566 	if (!args)
567 		return -ENOMEM;
568 
569 	args->geo = dp->i_mount->m_dir_geo;
570 	args->name = name->name;
571 	args->namelen = name->len;
572 	args->filetype = name->type;
573 	args->hashval = xfs_dir2_hashname(dp->i_mount, name);
574 	args->inumber = inum;
575 	args->dp = dp;
576 	args->total = total;
577 	args->whichfork = XFS_DATA_FORK;
578 	args->trans = tp;
579 	args->owner = dp->i_ino;
580 	rval = xfs_dir_replace_args(args);
581 	kfree(args);
582 	return rval;
583 }
584 
585 /*
586  * See if this entry can be added to the directory without allocating space.
587  */
588 int
xfs_dir_canenter(struct xfs_trans * tp,struct xfs_inode * dp,const struct xfs_name * name)589 xfs_dir_canenter(
590 	struct xfs_trans	*tp,
591 	struct xfs_inode	*dp,
592 	const struct xfs_name	*name)		/* name of entry to add */
593 {
594 	return xfs_dir_createname(tp, dp, name, 0, 0);
595 }
596 
597 /*
598  * Utility routines.
599  */
600 
601 /*
602  * Add a block to the directory.
603  *
604  * This routine is for data and free blocks, not leaf/node blocks which are
605  * handled by xfs_da_grow_inode.
606  */
607 int
xfs_dir2_grow_inode(struct xfs_da_args * args,int space,xfs_dir2_db_t * dbp)608 xfs_dir2_grow_inode(
609 	struct xfs_da_args	*args,
610 	int			space,	/* v2 dir's space XFS_DIR2_xxx_SPACE */
611 	xfs_dir2_db_t		*dbp)	/* out: block number added */
612 {
613 	struct xfs_inode	*dp = args->dp;
614 	struct xfs_mount	*mp = dp->i_mount;
615 	xfs_fileoff_t		bno;	/* directory offset of new block */
616 	int			count;	/* count of filesystem blocks */
617 	int			error;
618 
619 	trace_xfs_dir2_grow_inode(args, space);
620 
621 	/*
622 	 * Set lowest possible block in the space requested.
623 	 */
624 	bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
625 	count = args->geo->fsbcount;
626 
627 	error = xfs_da_grow_inode_int(args, &bno, count);
628 	if (error)
629 		return error;
630 
631 	*dbp = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)bno);
632 
633 	/*
634 	 * Update file's size if this is the data space and it grew.
635 	 */
636 	if (space == XFS_DIR2_DATA_SPACE) {
637 		xfs_fsize_t	size;		/* directory file (data) size */
638 
639 		size = XFS_FSB_TO_B(mp, bno + count);
640 		if (size > dp->i_disk_size) {
641 			dp->i_disk_size = size;
642 			xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
643 		}
644 	}
645 	return 0;
646 }
647 
648 /*
649  * Remove the given block from the directory.
650  * This routine is used for data and free blocks, leaf/node are done
651  * by xfs_da_shrink_inode.
652  */
653 int
xfs_dir2_shrink_inode(struct xfs_da_args * args,xfs_dir2_db_t db,struct xfs_buf * bp)654 xfs_dir2_shrink_inode(
655 	struct xfs_da_args	*args,
656 	xfs_dir2_db_t		db,
657 	struct xfs_buf		*bp)
658 {
659 	xfs_fileoff_t		bno;		/* directory file offset */
660 	xfs_dablk_t		da;		/* directory file offset */
661 	int			done;		/* bunmap is finished */
662 	struct xfs_inode	*dp;
663 	int			error;
664 	struct xfs_mount	*mp;
665 	struct xfs_trans	*tp;
666 
667 	trace_xfs_dir2_shrink_inode(args, db);
668 
669 	dp = args->dp;
670 	mp = dp->i_mount;
671 	tp = args->trans;
672 	da = xfs_dir2_db_to_da(args->geo, db);
673 
674 	/* Unmap the fsblock(s). */
675 	error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount, 0, 0, &done);
676 	if (error) {
677 		/*
678 		 * ENOSPC actually can happen if we're in a removename with no
679 		 * space reservation, and the resulting block removal would
680 		 * cause a bmap btree split or conversion from extents to btree.
681 		 * This can only happen for un-fragmented directory blocks,
682 		 * since you need to be punching out the middle of an extent.
683 		 * In this case we need to leave the block in the file, and not
684 		 * binval it.  So the block has to be in a consistent empty
685 		 * state and appropriately logged.  We don't free up the buffer,
686 		 * the caller can tell it hasn't happened since it got an error
687 		 * back.
688 		 */
689 		return error;
690 	}
691 	ASSERT(done);
692 	/*
693 	 * Invalidate the buffer from the transaction.
694 	 */
695 	xfs_trans_binval(tp, bp);
696 	/*
697 	 * If it's not a data block, we're done.
698 	 */
699 	if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET))
700 		return 0;
701 	/*
702 	 * If the block isn't the last one in the directory, we're done.
703 	 */
704 	if (dp->i_disk_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0))
705 		return 0;
706 	bno = da;
707 	if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
708 		/*
709 		 * This can't really happen unless there's kernel corruption.
710 		 */
711 		return error;
712 	}
713 	if (db == args->geo->datablk)
714 		ASSERT(bno == 0);
715 	else
716 		ASSERT(bno > 0);
717 	/*
718 	 * Set the size to the new last block.
719 	 */
720 	dp->i_disk_size = XFS_FSB_TO_B(mp, bno);
721 	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
722 	return 0;
723 }
724 
725 /* Returns true if the directory entry name is valid. */
726 bool
xfs_dir2_namecheck(const void * name,size_t length)727 xfs_dir2_namecheck(
728 	const void	*name,
729 	size_t		length)
730 {
731 	/*
732 	 * MAXNAMELEN includes the trailing null, but (name/length) leave it
733 	 * out, so use >= for the length check.
734 	 */
735 	if (length >= MAXNAMELEN)
736 		return false;
737 
738 	/* There shouldn't be any slashes or nulls here */
739 	return !memchr(name, '/', length) && !memchr(name, 0, length);
740 }
741 
742 xfs_dahash_t
xfs_dir2_hashname(struct xfs_mount * mp,const struct xfs_name * name)743 xfs_dir2_hashname(
744 	struct xfs_mount	*mp,
745 	const struct xfs_name	*name)
746 {
747 	if (unlikely(xfs_has_asciici(mp)))
748 		return xfs_ascii_ci_hashname(name);
749 	return xfs_da_hashname(name->name, name->len);
750 }
751 
752 enum xfs_dacmp
xfs_dir2_compname(struct xfs_da_args * args,const unsigned char * name,int len)753 xfs_dir2_compname(
754 	struct xfs_da_args	*args,
755 	const unsigned char	*name,
756 	int			len)
757 {
758 	if (unlikely(xfs_has_asciici(args->dp->i_mount)))
759 		return xfs_ascii_ci_compname(args, name, len);
760 	return xfs_da_compname(args, name, len);
761 }
762 
763 #ifdef CONFIG_XFS_LIVE_HOOKS
764 /*
765  * Use a static key here to reduce the overhead of directory live update hooks.
766  * If the compiler supports jump labels, the static branch will be replaced by
767  * a nop sled when there are no hook users.  Online fsck is currently the only
768  * caller, so this is a reasonable tradeoff.
769  *
770  * Note: Patching the kernel code requires taking the cpu hotplug lock.  Other
771  * parts of the kernel allocate memory with that lock held, which means that
772  * XFS callers cannot hold any locks that might be used by memory reclaim or
773  * writeback when calling the static_branch_{inc,dec} functions.
774  */
775 DEFINE_STATIC_XFS_HOOK_SWITCH(xfs_dir_hooks_switch);
776 
777 void
xfs_dir_hook_disable(void)778 xfs_dir_hook_disable(void)
779 {
780 	xfs_hooks_switch_off(&xfs_dir_hooks_switch);
781 }
782 
783 void
xfs_dir_hook_enable(void)784 xfs_dir_hook_enable(void)
785 {
786 	xfs_hooks_switch_on(&xfs_dir_hooks_switch);
787 }
788 
789 /* Call hooks for a directory update relating to a child dirent update. */
790 inline void
xfs_dir_update_hook(struct xfs_inode * dp,struct xfs_inode * ip,int delta,const struct xfs_name * name)791 xfs_dir_update_hook(
792 	struct xfs_inode		*dp,
793 	struct xfs_inode		*ip,
794 	int				delta,
795 	const struct xfs_name		*name)
796 {
797 	if (xfs_hooks_switched_on(&xfs_dir_hooks_switch)) {
798 		struct xfs_dir_update_params	p = {
799 			.dp		= dp,
800 			.ip		= ip,
801 			.delta		= delta,
802 			.name		= name,
803 		};
804 		struct xfs_mount	*mp = ip->i_mount;
805 
806 		xfs_hooks_call(&mp->m_dir_update_hooks, 0, &p);
807 	}
808 }
809 
810 /* Call the specified function during a directory update. */
811 int
xfs_dir_hook_add(struct xfs_mount * mp,struct xfs_dir_hook * hook)812 xfs_dir_hook_add(
813 	struct xfs_mount	*mp,
814 	struct xfs_dir_hook	*hook)
815 {
816 	return xfs_hooks_add(&mp->m_dir_update_hooks, &hook->dirent_hook);
817 }
818 
819 /* Stop calling the specified function during a directory update. */
820 void
xfs_dir_hook_del(struct xfs_mount * mp,struct xfs_dir_hook * hook)821 xfs_dir_hook_del(
822 	struct xfs_mount	*mp,
823 	struct xfs_dir_hook	*hook)
824 {
825 	xfs_hooks_del(&mp->m_dir_update_hooks, &hook->dirent_hook);
826 }
827 
828 /* Configure directory update hook functions. */
829 void
xfs_dir_hook_setup(struct xfs_dir_hook * hook,notifier_fn_t mod_fn)830 xfs_dir_hook_setup(
831 	struct xfs_dir_hook	*hook,
832 	notifier_fn_t		mod_fn)
833 {
834 	xfs_hook_setup(&hook->dirent_hook, mod_fn);
835 }
836 #endif /* CONFIG_XFS_LIVE_HOOKS */
837 
838 /*
839  * Given a directory @dp, a newly allocated inode @ip, and a @name, link @ip
840  * into @dp under the given @name.  If @ip is a directory, it will be
841  * initialized.  Both inodes must have the ILOCK held and the transaction must
842  * have sufficient blocks reserved.
843  */
844 int
xfs_dir_create_child(struct xfs_trans * tp,unsigned int resblks,struct xfs_dir_update * du)845 xfs_dir_create_child(
846 	struct xfs_trans	*tp,
847 	unsigned int		resblks,
848 	struct xfs_dir_update	*du)
849 {
850 	struct xfs_inode	*dp = du->dp;
851 	const struct xfs_name	*name = du->name;
852 	struct xfs_inode	*ip = du->ip;
853 	int			error;
854 
855 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
856 	xfs_assert_ilocked(dp, XFS_ILOCK_EXCL);
857 
858 	error = xfs_dir_createname(tp, dp, name, ip->i_ino, resblks);
859 	if (error) {
860 		ASSERT(error != -ENOSPC);
861 		return error;
862 	}
863 
864 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
865 	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
866 
867 	if (S_ISDIR(VFS_I(ip)->i_mode)) {
868 		error = xfs_dir_init(tp, ip, dp);
869 		if (error)
870 			return error;
871 
872 		xfs_bumplink(tp, dp);
873 	}
874 
875 	/*
876 	 * If we have parent pointers, we need to add the attribute containing
877 	 * the parent information now.
878 	 */
879 	if (du->ppargs) {
880 		error = xfs_parent_addname(tp, du->ppargs, dp, name, ip);
881 		if (error)
882 			return error;
883 	}
884 
885 	xfs_dir_update_hook(dp, ip, 1, name);
886 	return 0;
887 }
888 
889 /*
890  * Given a directory @dp, an existing non-directory inode @ip, and a @name,
891  * link @ip into @dp under the given @name.  Both inodes must have the ILOCK
892  * held.
893  */
894 int
xfs_dir_add_child(struct xfs_trans * tp,unsigned int resblks,struct xfs_dir_update * du)895 xfs_dir_add_child(
896 	struct xfs_trans	*tp,
897 	unsigned int		resblks,
898 	struct xfs_dir_update	*du)
899 {
900 	struct xfs_inode	*dp = du->dp;
901 	const struct xfs_name	*name = du->name;
902 	struct xfs_inode	*ip = du->ip;
903 	struct xfs_mount	*mp = tp->t_mountp;
904 	int			error;
905 
906 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
907 	xfs_assert_ilocked(dp, XFS_ILOCK_EXCL);
908 	ASSERT(!S_ISDIR(VFS_I(ip)->i_mode));
909 
910 	if (!resblks) {
911 		error = xfs_dir_canenter(tp, dp, name);
912 		if (error)
913 			return error;
914 	}
915 
916 	/*
917 	 * Handle initial link state of O_TMPFILE inode
918 	 */
919 	if (VFS_I(ip)->i_nlink == 0) {
920 		struct xfs_perag	*pag;
921 
922 		pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
923 		error = xfs_iunlink_remove(tp, pag, ip);
924 		xfs_perag_put(pag);
925 		if (error)
926 			return error;
927 	}
928 
929 	error = xfs_dir_createname(tp, dp, name, ip->i_ino, resblks);
930 	if (error)
931 		return error;
932 
933 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
934 	xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
935 
936 	xfs_bumplink(tp, ip);
937 
938 	/*
939 	 * If we have parent pointers, we now need to add the parent record to
940 	 * the attribute fork of the inode. If this is the initial parent
941 	 * attribute, we need to create it correctly, otherwise we can just add
942 	 * the parent to the inode.
943 	 */
944 	if (du->ppargs) {
945 		error = xfs_parent_addname(tp, du->ppargs, dp, name, ip);
946 		if (error)
947 			return error;
948 	}
949 
950 	xfs_dir_update_hook(dp, ip, 1, name);
951 	return 0;
952 }
953 
954 /*
955  * Given a directory @dp, a child @ip, and a @name, remove the (@name, @ip)
956  * entry from the directory.  Both inodes must have the ILOCK held.
957  */
958 int
xfs_dir_remove_child(struct xfs_trans * tp,unsigned int resblks,struct xfs_dir_update * du)959 xfs_dir_remove_child(
960 	struct xfs_trans	*tp,
961 	unsigned int		resblks,
962 	struct xfs_dir_update	*du)
963 {
964 	struct xfs_inode	*dp = du->dp;
965 	const struct xfs_name	*name = du->name;
966 	struct xfs_inode	*ip = du->ip;
967 	int			error;
968 
969 	xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
970 	xfs_assert_ilocked(dp, XFS_ILOCK_EXCL);
971 
972 	/*
973 	 * If we're removing a directory perform some additional validation.
974 	 */
975 	if (S_ISDIR(VFS_I(ip)->i_mode)) {
976 		ASSERT(VFS_I(ip)->i_nlink >= 2);
977 		if (VFS_I(ip)->i_nlink != 2)
978 			return -ENOTEMPTY;
979 		if (!xfs_dir_isempty(ip))
980 			return -ENOTEMPTY;
981 
982 		/* Drop the link from ip's "..".  */
983 		error = xfs_droplink(tp, dp);
984 		if (error)
985 			return error;
986 
987 		/* Drop the "." link from ip to self.  */
988 		error = xfs_droplink(tp, ip);
989 		if (error)
990 			return error;
991 
992 		/*
993 		 * Point the unlinked child directory's ".." entry to the root
994 		 * directory to eliminate back-references to inodes that may
995 		 * get freed before the child directory is closed.  If the fs
996 		 * gets shrunk, this can lead to dirent inode validation errors.
997 		 */
998 		if (dp->i_ino != tp->t_mountp->m_sb.sb_rootino) {
999 			error = xfs_dir_replace(tp, ip, &xfs_name_dotdot,
1000 					tp->t_mountp->m_sb.sb_rootino, 0);
1001 			if (error)
1002 				return error;
1003 		}
1004 	} else {
1005 		/*
1006 		 * When removing a non-directory we need to log the parent
1007 		 * inode here.  For a directory this is done implicitly
1008 		 * by the xfs_droplink call for the ".." entry.
1009 		 */
1010 		xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1011 	}
1012 	xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1013 
1014 	/* Drop the link from dp to ip. */
1015 	error = xfs_droplink(tp, ip);
1016 	if (error)
1017 		return error;
1018 
1019 	error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks);
1020 	if (error) {
1021 		ASSERT(error != -ENOENT);
1022 		return error;
1023 	}
1024 
1025 	/* Remove parent pointer. */
1026 	if (du->ppargs) {
1027 		error = xfs_parent_removename(tp, du->ppargs, dp, name, ip);
1028 		if (error)
1029 			return error;
1030 	}
1031 
1032 	xfs_dir_update_hook(dp, ip, -1, name);
1033 	return 0;
1034 }
1035 
1036 /*
1037  * Exchange the entry (@name1, @ip1) in directory @dp1 with the entry (@name2,
1038  * @ip2) in directory @dp2, and update '..' @ip1 and @ip2's entries as needed.
1039  * @ip1 and @ip2 need not be of the same type.
1040  *
1041  * All inodes must have the ILOCK held, and both entries must already exist.
1042  */
1043 int
xfs_dir_exchange_children(struct xfs_trans * tp,struct xfs_dir_update * du1,struct xfs_dir_update * du2,unsigned int spaceres)1044 xfs_dir_exchange_children(
1045 	struct xfs_trans	*tp,
1046 	struct xfs_dir_update	*du1,
1047 	struct xfs_dir_update	*du2,
1048 	unsigned int		spaceres)
1049 {
1050 	struct xfs_inode	*dp1 = du1->dp;
1051 	const struct xfs_name	*name1 = du1->name;
1052 	struct xfs_inode	*ip1 = du1->ip;
1053 	struct xfs_inode	*dp2 = du2->dp;
1054 	const struct xfs_name	*name2 = du2->name;
1055 	struct xfs_inode	*ip2 = du2->ip;
1056 	int			ip1_flags = 0;
1057 	int			ip2_flags = 0;
1058 	int			dp2_flags = 0;
1059 	int			error;
1060 
1061 	/* Swap inode number for dirent in first parent */
1062 	error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres);
1063 	if (error)
1064 		return error;
1065 
1066 	/* Swap inode number for dirent in second parent */
1067 	error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres);
1068 	if (error)
1069 		return error;
1070 
1071 	/*
1072 	 * If we're renaming one or more directories across different parents,
1073 	 * update the respective ".." entries (and link counts) to match the new
1074 	 * parents.
1075 	 */
1076 	if (dp1 != dp2) {
1077 		dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
1078 
1079 		if (S_ISDIR(VFS_I(ip2)->i_mode)) {
1080 			error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
1081 						dp1->i_ino, spaceres);
1082 			if (error)
1083 				return error;
1084 
1085 			/* transfer ip2 ".." reference to dp1 */
1086 			if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
1087 				error = xfs_droplink(tp, dp2);
1088 				if (error)
1089 					return error;
1090 				xfs_bumplink(tp, dp1);
1091 			}
1092 
1093 			/*
1094 			 * Although ip1 isn't changed here, userspace needs
1095 			 * to be warned about the change, so that applications
1096 			 * relying on it (like backup ones), will properly
1097 			 * notify the change
1098 			 */
1099 			ip1_flags |= XFS_ICHGTIME_CHG;
1100 			ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
1101 		}
1102 
1103 		if (S_ISDIR(VFS_I(ip1)->i_mode)) {
1104 			error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
1105 						dp2->i_ino, spaceres);
1106 			if (error)
1107 				return error;
1108 
1109 			/* transfer ip1 ".." reference to dp2 */
1110 			if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
1111 				error = xfs_droplink(tp, dp1);
1112 				if (error)
1113 					return error;
1114 				xfs_bumplink(tp, dp2);
1115 			}
1116 
1117 			/*
1118 			 * Although ip2 isn't changed here, userspace needs
1119 			 * to be warned about the change, so that applications
1120 			 * relying on it (like backup ones), will properly
1121 			 * notify the change
1122 			 */
1123 			ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
1124 			ip2_flags |= XFS_ICHGTIME_CHG;
1125 		}
1126 	}
1127 
1128 	if (ip1_flags) {
1129 		xfs_trans_ichgtime(tp, ip1, ip1_flags);
1130 		xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
1131 	}
1132 	if (ip2_flags) {
1133 		xfs_trans_ichgtime(tp, ip2, ip2_flags);
1134 		xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
1135 	}
1136 	if (dp2_flags) {
1137 		xfs_trans_ichgtime(tp, dp2, dp2_flags);
1138 		xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
1139 	}
1140 	xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1141 	xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
1142 
1143 	/* Schedule parent pointer replacements */
1144 	if (du1->ppargs) {
1145 		error = xfs_parent_replacename(tp, du1->ppargs, dp1, name1,
1146 				dp2, name2, ip1);
1147 		if (error)
1148 			return error;
1149 	}
1150 
1151 	if (du2->ppargs) {
1152 		error = xfs_parent_replacename(tp, du2->ppargs, dp2, name2,
1153 				dp1, name1, ip2);
1154 		if (error)
1155 			return error;
1156 	}
1157 
1158 	/*
1159 	 * Inform our hook clients that we've finished an exchange operation as
1160 	 * follows: removed the source and target files from their directories;
1161 	 * added the target to the source directory; and added the source to
1162 	 * the target directory.  All inodes are locked, so it's ok to model a
1163 	 * rename this way so long as we say we deleted entries before we add
1164 	 * new ones.
1165 	 */
1166 	xfs_dir_update_hook(dp1, ip1, -1, name1);
1167 	xfs_dir_update_hook(dp2, ip2, -1, name2);
1168 	xfs_dir_update_hook(dp1, ip2, 1, name1);
1169 	xfs_dir_update_hook(dp2, ip1, 1, name2);
1170 	return 0;
1171 }
1172 
1173 /*
1174  * Given an entry (@src_name, @src_ip) in directory @src_dp, make the entry
1175  * @target_name in directory @target_dp point to @src_ip and remove the
1176  * original entry, cleaning up everything left behind.
1177  *
1178  * Cleanup involves dropping a link count on @target_ip, and either removing
1179  * the (@src_name, @src_ip) entry from @src_dp or simply replacing the entry
1180  * with (@src_name, @wip) if a whiteout inode @wip is supplied.
1181  *
1182  * All inodes must have the ILOCK held.  We assume that if @src_ip is a
1183  * directory then its '..' doesn't already point to @target_dp, and that @wip
1184  * is a freshly allocated whiteout.
1185  */
1186 int
xfs_dir_rename_children(struct xfs_trans * tp,struct xfs_dir_update * du_src,struct xfs_dir_update * du_tgt,unsigned int spaceres,struct xfs_dir_update * du_wip)1187 xfs_dir_rename_children(
1188 	struct xfs_trans	*tp,
1189 	struct xfs_dir_update	*du_src,
1190 	struct xfs_dir_update	*du_tgt,
1191 	unsigned int		spaceres,
1192 	struct xfs_dir_update	*du_wip)
1193 {
1194 	struct xfs_mount	*mp = tp->t_mountp;
1195 	struct xfs_inode	*src_dp = du_src->dp;
1196 	const struct xfs_name	*src_name = du_src->name;
1197 	struct xfs_inode	*src_ip = du_src->ip;
1198 	struct xfs_inode	*target_dp = du_tgt->dp;
1199 	const struct xfs_name	*target_name = du_tgt->name;
1200 	struct xfs_inode	*target_ip = du_tgt->ip;
1201 	bool			new_parent = (src_dp != target_dp);
1202 	bool			src_is_directory;
1203 	int			error;
1204 
1205 	src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
1206 
1207 	/*
1208 	 * Check for expected errors before we dirty the transaction
1209 	 * so we can return an error without a transaction abort.
1210 	 */
1211 	if (target_ip == NULL) {
1212 		/*
1213 		 * If there's no space reservation, check the entry will
1214 		 * fit before actually inserting it.
1215 		 */
1216 		if (!spaceres) {
1217 			error = xfs_dir_canenter(tp, target_dp, target_name);
1218 			if (error)
1219 				return error;
1220 		}
1221 	} else {
1222 		/*
1223 		 * If target exists and it's a directory, check that whether
1224 		 * it can be destroyed.
1225 		 */
1226 		if (S_ISDIR(VFS_I(target_ip)->i_mode) &&
1227 		    (!xfs_dir_isempty(target_ip) ||
1228 		     (VFS_I(target_ip)->i_nlink > 2)))
1229 			return -EEXIST;
1230 	}
1231 
1232 	/*
1233 	 * Directory entry creation below may acquire the AGF. Remove
1234 	 * the whiteout from the unlinked list first to preserve correct
1235 	 * AGI/AGF locking order. This dirties the transaction so failures
1236 	 * after this point will abort and log recovery will clean up the
1237 	 * mess.
1238 	 *
1239 	 * For whiteouts, we need to bump the link count on the whiteout
1240 	 * inode. After this point, we have a real link, clear the tmpfile
1241 	 * state flag from the inode so it doesn't accidentally get misused
1242 	 * in future.
1243 	 */
1244 	if (du_wip->ip) {
1245 		struct xfs_perag	*pag;
1246 
1247 		ASSERT(VFS_I(du_wip->ip)->i_nlink == 0);
1248 
1249 		pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, du_wip->ip->i_ino));
1250 		error = xfs_iunlink_remove(tp, pag, du_wip->ip);
1251 		xfs_perag_put(pag);
1252 		if (error)
1253 			return error;
1254 
1255 		xfs_bumplink(tp, du_wip->ip);
1256 	}
1257 
1258 	/*
1259 	 * Set up the target.
1260 	 */
1261 	if (target_ip == NULL) {
1262 		/*
1263 		 * If target does not exist and the rename crosses
1264 		 * directories, adjust the target directory link count
1265 		 * to account for the ".." reference from the new entry.
1266 		 */
1267 		error = xfs_dir_createname(tp, target_dp, target_name,
1268 					   src_ip->i_ino, spaceres);
1269 		if (error)
1270 			return error;
1271 
1272 		xfs_trans_ichgtime(tp, target_dp,
1273 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1274 
1275 		if (new_parent && src_is_directory) {
1276 			xfs_bumplink(tp, target_dp);
1277 		}
1278 	} else { /* target_ip != NULL */
1279 		/*
1280 		 * Link the source inode under the target name.
1281 		 * If the source inode is a directory and we are moving
1282 		 * it across directories, its ".." entry will be
1283 		 * inconsistent until we replace that down below.
1284 		 *
1285 		 * In case there is already an entry with the same
1286 		 * name at the destination directory, remove it first.
1287 		 */
1288 		error = xfs_dir_replace(tp, target_dp, target_name,
1289 					src_ip->i_ino, spaceres);
1290 		if (error)
1291 			return error;
1292 
1293 		xfs_trans_ichgtime(tp, target_dp,
1294 					XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1295 
1296 		/*
1297 		 * Decrement the link count on the target since the target
1298 		 * dir no longer points to it.
1299 		 */
1300 		error = xfs_droplink(tp, target_ip);
1301 		if (error)
1302 			return error;
1303 
1304 		if (src_is_directory) {
1305 			/*
1306 			 * Drop the link from the old "." entry.
1307 			 */
1308 			error = xfs_droplink(tp, target_ip);
1309 			if (error)
1310 				return error;
1311 		}
1312 	} /* target_ip != NULL */
1313 
1314 	/*
1315 	 * Remove the source.
1316 	 */
1317 	if (new_parent && src_is_directory) {
1318 		/*
1319 		 * Rewrite the ".." entry to point to the new
1320 		 * directory.
1321 		 */
1322 		error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
1323 					target_dp->i_ino, spaceres);
1324 		ASSERT(error != -EEXIST);
1325 		if (error)
1326 			return error;
1327 	}
1328 
1329 	/*
1330 	 * We always want to hit the ctime on the source inode.
1331 	 *
1332 	 * This isn't strictly required by the standards since the source
1333 	 * inode isn't really being changed, but old unix file systems did
1334 	 * it and some incremental backup programs won't work without it.
1335 	 */
1336 	xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
1337 	xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
1338 
1339 	/*
1340 	 * Adjust the link count on src_dp.  This is necessary when
1341 	 * renaming a directory, either within one parent when
1342 	 * the target existed, or across two parent directories.
1343 	 */
1344 	if (src_is_directory && (new_parent || target_ip != NULL)) {
1345 
1346 		/*
1347 		 * Decrement link count on src_directory since the
1348 		 * entry that's moved no longer points to it.
1349 		 */
1350 		error = xfs_droplink(tp, src_dp);
1351 		if (error)
1352 			return error;
1353 	}
1354 
1355 	/*
1356 	 * For whiteouts, we only need to update the source dirent with the
1357 	 * inode number of the whiteout inode rather than removing it
1358 	 * altogether.
1359 	 */
1360 	if (du_wip->ip)
1361 		error = xfs_dir_replace(tp, src_dp, src_name, du_wip->ip->i_ino,
1362 					spaceres);
1363 	else
1364 		error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
1365 					   spaceres);
1366 	if (error)
1367 		return error;
1368 
1369 	xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1370 	xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
1371 	if (new_parent)
1372 		xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
1373 
1374 	/* Schedule parent pointer updates. */
1375 	if (du_wip->ppargs) {
1376 		error = xfs_parent_addname(tp, du_wip->ppargs, src_dp,
1377 				src_name, du_wip->ip);
1378 		if (error)
1379 			return error;
1380 	}
1381 
1382 	if (du_src->ppargs) {
1383 		error = xfs_parent_replacename(tp, du_src->ppargs, src_dp,
1384 				src_name, target_dp, target_name, src_ip);
1385 		if (error)
1386 			return error;
1387 	}
1388 
1389 	if (du_tgt->ppargs) {
1390 		error = xfs_parent_removename(tp, du_tgt->ppargs, target_dp,
1391 				target_name, target_ip);
1392 		if (error)
1393 			return error;
1394 	}
1395 
1396 	/*
1397 	 * Inform our hook clients that we've finished a rename operation as
1398 	 * follows: removed the source and target files from their directories;
1399 	 * that we've added the source to the target directory; and finally
1400 	 * that we've added the whiteout, if there was one.  All inodes are
1401 	 * locked, so it's ok to model a rename this way so long as we say we
1402 	 * deleted entries before we add new ones.
1403 	 */
1404 	if (target_ip)
1405 		xfs_dir_update_hook(target_dp, target_ip, -1, target_name);
1406 	xfs_dir_update_hook(src_dp, src_ip, -1, src_name);
1407 	xfs_dir_update_hook(target_dp, src_ip, 1, target_name);
1408 	if (du_wip->ip)
1409 		xfs_dir_update_hook(src_dp, du_wip->ip, 1, src_name);
1410 	return 0;
1411 }
1412