xref: /linux/fs/jfs/namei.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) International Business Machines Corp., 2000-2004
4  *   Portions Copyright (C) Christoph Hellwig, 2001-2002
5  */
6 
7 #include <linux/fs.h>
8 #include <linux/filelock.h>
9 #include <linux/namei.h>
10 #include <linux/ctype.h>
11 #include <linux/quotaops.h>
12 #include <linux/exportfs.h>
13 #include "jfs_incore.h"
14 #include "jfs_superblock.h"
15 #include "jfs_inode.h"
16 #include "jfs_dinode.h"
17 #include "jfs_dmap.h"
18 #include "jfs_unicode.h"
19 #include "jfs_metapage.h"
20 #include "jfs_xattr.h"
21 #include "jfs_acl.h"
22 #include "jfs_debug.h"
23 
24 /*
25  * forward references
26  */
27 const struct dentry_operations jfs_ci_dentry_operations;
28 
29 static s64 commitZeroLink(tid_t, struct inode *);
30 
31 /*
32  * NAME:	free_ea_wmap(inode)
33  *
34  * FUNCTION:	free uncommitted extended attributes from working map
35  *
36  */
37 static inline void free_ea_wmap(struct inode *inode)
38 {
39 	dxd_t *ea = &JFS_IP(inode)->ea;
40 
41 	if (ea->flag & DXD_EXTENT) {
42 		/* free EA pages from cache */
43 		invalidate_dxd_metapages(inode, *ea);
44 		dbFree(inode, addressDXD(ea), lengthDXD(ea));
45 	}
46 	ea->flag = 0;
47 }
48 
49 /*
50  * NAME:	jfs_create(dip, dentry, mode)
51  *
52  * FUNCTION:	create a regular file in the parent directory <dip>
53  *		with name = <from dentry> and mode = <mode>
54  *
55  * PARAMETER:	dip	- parent directory vnode
56  *		dentry	- dentry of new file
57  *		mode	- create mode (rwxrwxrwx).
58  *		nd- nd struct
59  *
60  * RETURN:	Errors from subroutines
61  *
62  */
63 static int jfs_create(struct mnt_idmap *idmap, struct inode *dip,
64 		      struct dentry *dentry, umode_t mode, bool excl)
65 {
66 	int rc = 0;
67 	tid_t tid;		/* transaction id */
68 	struct inode *ip = NULL;	/* child directory inode */
69 	ino_t ino;
70 	struct component_name dname;	/* child directory name */
71 	struct btstack btstack;
72 	struct inode *iplist[2];
73 	struct tblock *tblk;
74 
75 	jfs_info("jfs_create: dip:0x%p name:%pd", dip, dentry);
76 
77 	rc = dquot_initialize(dip);
78 	if (rc)
79 		goto out1;
80 
81 	/*
82 	 * search parent directory for entry/freespace
83 	 * (dtSearch() returns parent directory page pinned)
84 	 */
85 	if ((rc = get_UCSname(&dname, dentry)))
86 		goto out1;
87 
88 	/*
89 	 * Either iAlloc() or txBegin() may block.  Deadlock can occur if we
90 	 * block there while holding dtree page, so we allocate the inode &
91 	 * begin the transaction before we search the directory.
92 	 */
93 	ip = ialloc(dip, mode);
94 	if (IS_ERR(ip)) {
95 		rc = PTR_ERR(ip);
96 		goto out2;
97 	}
98 
99 	tid = txBegin(dip->i_sb, 0);
100 
101 	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
102 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
103 
104 	rc = jfs_init_acl(tid, ip, dip);
105 	if (rc)
106 		goto out3;
107 
108 	rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
109 	if (rc) {
110 		txAbort(tid, 0);
111 		goto out3;
112 	}
113 
114 	if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
115 		jfs_err("jfs_create: dtSearch returned %d", rc);
116 		txAbort(tid, 0);
117 		goto out3;
118 	}
119 
120 	tblk = tid_to_tblock(tid);
121 	tblk->xflag |= COMMIT_CREATE;
122 	tblk->ino = ip->i_ino;
123 	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
124 
125 	iplist[0] = dip;
126 	iplist[1] = ip;
127 
128 	/*
129 	 * initialize the child XAD tree root in-line in inode
130 	 */
131 	xtInitRoot(tid, ip);
132 
133 	/*
134 	 * create entry in parent directory for child directory
135 	 * (dtInsert() releases parent directory page)
136 	 */
137 	ino = ip->i_ino;
138 	if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
139 		if (rc == -EIO) {
140 			jfs_err("jfs_create: dtInsert returned -EIO");
141 			txAbort(tid, 1);	/* Marks Filesystem dirty */
142 		} else
143 			txAbort(tid, 0);	/* Filesystem full */
144 		goto out3;
145 	}
146 
147 	ip->i_op = &jfs_file_inode_operations;
148 	ip->i_fop = &jfs_file_operations;
149 	ip->i_mapping->a_ops = &jfs_aops;
150 
151 	mark_inode_dirty(ip);
152 
153 	inode_set_mtime_to_ts(dip, inode_set_ctime_current(dip));
154 
155 	mark_inode_dirty(dip);
156 
157 	rc = txCommit(tid, 2, &iplist[0], 0);
158 
159       out3:
160 	txEnd(tid);
161 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
162 	mutex_unlock(&JFS_IP(dip)->commit_mutex);
163 	if (rc) {
164 		free_ea_wmap(ip);
165 		clear_nlink(ip);
166 		discard_new_inode(ip);
167 	} else {
168 		d_instantiate_new(dentry, ip);
169 	}
170 
171       out2:
172 	free_UCSname(&dname);
173 
174       out1:
175 
176 	jfs_info("jfs_create: rc:%d", rc);
177 	return rc;
178 }
179 
180 
181 /*
182  * NAME:	jfs_mkdir(dip, dentry, mode)
183  *
184  * FUNCTION:	create a child directory in the parent directory <dip>
185  *		with name = <from dentry> and mode = <mode>
186  *
187  * PARAMETER:	dip	- parent directory vnode
188  *		dentry	- dentry of child directory
189  *		mode	- create mode (rwxrwxrwx).
190  *
191  * RETURN:	ERR_PTR() of errors from subroutines.
192  *
193  * note:
194  * EACCES: user needs search+write permission on the parent directory
195  */
196 static struct dentry *jfs_mkdir(struct mnt_idmap *idmap, struct inode *dip,
197 				struct dentry *dentry, umode_t mode)
198 {
199 	int rc = 0;
200 	tid_t tid;		/* transaction id */
201 	struct inode *ip = NULL;	/* child directory inode */
202 	ino_t ino;
203 	struct component_name dname;	/* child directory name */
204 	struct btstack btstack;
205 	struct inode *iplist[2];
206 	struct tblock *tblk;
207 
208 	jfs_info("jfs_mkdir: dip:0x%p name:%pd", dip, dentry);
209 
210 	rc = dquot_initialize(dip);
211 	if (rc)
212 		goto out1;
213 
214 	/*
215 	 * search parent directory for entry/freespace
216 	 * (dtSearch() returns parent directory page pinned)
217 	 */
218 	if ((rc = get_UCSname(&dname, dentry)))
219 		goto out1;
220 
221 	/*
222 	 * Either iAlloc() or txBegin() may block.  Deadlock can occur if we
223 	 * block there while holding dtree page, so we allocate the inode &
224 	 * begin the transaction before we search the directory.
225 	 */
226 	ip = ialloc(dip, S_IFDIR | mode);
227 	if (IS_ERR(ip)) {
228 		rc = PTR_ERR(ip);
229 		goto out2;
230 	}
231 
232 	tid = txBegin(dip->i_sb, 0);
233 
234 	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
235 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
236 
237 	rc = jfs_init_acl(tid, ip, dip);
238 	if (rc)
239 		goto out3;
240 
241 	rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
242 	if (rc) {
243 		txAbort(tid, 0);
244 		goto out3;
245 	}
246 
247 	if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
248 		jfs_err("jfs_mkdir: dtSearch returned %d", rc);
249 		txAbort(tid, 0);
250 		goto out3;
251 	}
252 
253 	tblk = tid_to_tblock(tid);
254 	tblk->xflag |= COMMIT_CREATE;
255 	tblk->ino = ip->i_ino;
256 	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
257 
258 	iplist[0] = dip;
259 	iplist[1] = ip;
260 
261 	/*
262 	 * initialize the child directory in-line in inode
263 	 */
264 	dtInitRoot(tid, ip, dip->i_ino);
265 
266 	/*
267 	 * create entry in parent directory for child directory
268 	 * (dtInsert() releases parent directory page)
269 	 */
270 	ino = ip->i_ino;
271 	if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
272 		if (rc == -EIO) {
273 			jfs_err("jfs_mkdir: dtInsert returned -EIO");
274 			txAbort(tid, 1);	/* Marks Filesystem dirty */
275 		} else
276 			txAbort(tid, 0);	/* Filesystem full */
277 		goto out3;
278 	}
279 
280 	set_nlink(ip, 2);	/* for '.' */
281 	ip->i_op = &jfs_dir_inode_operations;
282 	ip->i_fop = &jfs_dir_operations;
283 
284 	mark_inode_dirty(ip);
285 
286 	/* update parent directory inode */
287 	inc_nlink(dip);		/* for '..' from child directory */
288 	inode_set_mtime_to_ts(dip, inode_set_ctime_current(dip));
289 	mark_inode_dirty(dip);
290 
291 	rc = txCommit(tid, 2, &iplist[0], 0);
292 
293       out3:
294 	txEnd(tid);
295 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
296 	mutex_unlock(&JFS_IP(dip)->commit_mutex);
297 	if (rc) {
298 		free_ea_wmap(ip);
299 		clear_nlink(ip);
300 		discard_new_inode(ip);
301 	} else {
302 		d_instantiate_new(dentry, ip);
303 	}
304 
305       out2:
306 	free_UCSname(&dname);
307 
308 
309       out1:
310 
311 	jfs_info("jfs_mkdir: rc:%d", rc);
312 	return ERR_PTR(rc);
313 }
314 
315 /*
316  * NAME:	jfs_rmdir(dip, dentry)
317  *
318  * FUNCTION:	remove a link to child directory
319  *
320  * PARAMETER:	dip	- parent inode
321  *		dentry	- child directory dentry
322  *
323  * RETURN:	-EINVAL	- if name is . or ..
324  *		-EINVAL - if . or .. exist but are invalid.
325  *		errors from subroutines
326  *
327  * note:
328  * if other threads have the directory open when the last link
329  * is removed, the "." and ".." entries, if present, are removed before
330  * rmdir() returns and no new entries may be created in the directory,
331  * but the directory is not removed until the last reference to
332  * the directory is released (cf.unlink() of regular file).
333  */
334 static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
335 {
336 	int rc;
337 	tid_t tid;		/* transaction id */
338 	struct inode *ip = d_inode(dentry);
339 	ino_t ino;
340 	struct component_name dname;
341 	struct inode *iplist[2];
342 	struct tblock *tblk;
343 
344 	jfs_info("jfs_rmdir: dip:0x%p name:%pd", dip, dentry);
345 
346 	/* Init inode for quota operations. */
347 	rc = dquot_initialize(dip);
348 	if (rc)
349 		goto out;
350 	rc = dquot_initialize(ip);
351 	if (rc)
352 		goto out;
353 
354 	/* directory must be empty to be removed */
355 	if (!dtEmpty(ip)) {
356 		rc = -ENOTEMPTY;
357 		goto out;
358 	}
359 
360 	if ((rc = get_UCSname(&dname, dentry))) {
361 		goto out;
362 	}
363 
364 	tid = txBegin(dip->i_sb, 0);
365 
366 	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
367 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
368 
369 	iplist[0] = dip;
370 	iplist[1] = ip;
371 
372 	tblk = tid_to_tblock(tid);
373 	tblk->xflag |= COMMIT_DELETE;
374 	tblk->u.ip = ip;
375 
376 	/*
377 	 * delete the entry of target directory from parent directory
378 	 */
379 	ino = ip->i_ino;
380 	if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
381 		jfs_err("jfs_rmdir: dtDelete returned %d", rc);
382 		if (rc == -EIO)
383 			txAbort(tid, 1);
384 		txEnd(tid);
385 		mutex_unlock(&JFS_IP(ip)->commit_mutex);
386 		mutex_unlock(&JFS_IP(dip)->commit_mutex);
387 
388 		goto out2;
389 	}
390 
391 	/* update parent directory's link count corresponding
392 	 * to ".." entry of the target directory deleted
393 	 */
394 	inode_set_mtime_to_ts(dip, inode_set_ctime_current(dip));
395 	inode_dec_link_count(dip);
396 
397 	/*
398 	 * OS/2 could have created EA and/or ACL
399 	 */
400 	/* free EA from both persistent and working map */
401 	if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
402 		/* free EA pages */
403 		txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
404 	}
405 	JFS_IP(ip)->ea.flag = 0;
406 
407 	/* free ACL from both persistent and working map */
408 	if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
409 		/* free ACL pages */
410 		txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
411 	}
412 	JFS_IP(ip)->acl.flag = 0;
413 
414 	/* mark the target directory as deleted */
415 	clear_nlink(ip);
416 	mark_inode_dirty(ip);
417 
418 	rc = txCommit(tid, 2, &iplist[0], 0);
419 
420 	txEnd(tid);
421 
422 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
423 	mutex_unlock(&JFS_IP(dip)->commit_mutex);
424 
425 	/*
426 	 * Truncating the directory index table is not guaranteed.  It
427 	 * may need to be done iteratively
428 	 */
429 	if (test_cflag(COMMIT_Stale, dip)) {
430 		if (dip->i_size > 1)
431 			jfs_truncate_nolock(dip, 0);
432 
433 		clear_cflag(COMMIT_Stale, dip);
434 	}
435 
436       out2:
437 	free_UCSname(&dname);
438 
439       out:
440 	jfs_info("jfs_rmdir: rc:%d", rc);
441 	return rc;
442 }
443 
444 /*
445  * NAME:	jfs_unlink(dip, dentry)
446  *
447  * FUNCTION:	remove a link to object <vp> named by <name>
448  *		from parent directory <dvp>
449  *
450  * PARAMETER:	dip	- inode of parent directory
451  *		dentry	- dentry of object to be removed
452  *
453  * RETURN:	errors from subroutines
454  *
455  * note:
456  * temporary file: if one or more processes have the file open
457  * when the last link is removed, the link will be removed before
458  * unlink() returns, but the removal of the file contents will be
459  * postponed until all references to the files are closed.
460  *
461  * JFS does NOT support unlink() on directories.
462  *
463  */
464 static int jfs_unlink(struct inode *dip, struct dentry *dentry)
465 {
466 	int rc;
467 	tid_t tid;		/* transaction id */
468 	struct inode *ip = d_inode(dentry);
469 	ino_t ino;
470 	struct component_name dname;	/* object name */
471 	struct inode *iplist[2];
472 	struct tblock *tblk;
473 	s64 new_size = 0;
474 	int commit_flag;
475 
476 	jfs_info("jfs_unlink: dip:0x%p name:%pd", dip, dentry);
477 
478 	/* Init inode for quota operations. */
479 	rc = dquot_initialize(dip);
480 	if (rc)
481 		goto out;
482 	rc = dquot_initialize(ip);
483 	if (rc)
484 		goto out;
485 
486 	if ((rc = get_UCSname(&dname, dentry)))
487 		goto out;
488 
489 	IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
490 
491 	tid = txBegin(dip->i_sb, 0);
492 
493 	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
494 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
495 
496 	iplist[0] = dip;
497 	iplist[1] = ip;
498 
499 	/*
500 	 * delete the entry of target file from parent directory
501 	 */
502 	ino = ip->i_ino;
503 	if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
504 		jfs_err("jfs_unlink: dtDelete returned %d", rc);
505 		if (rc == -EIO)
506 			txAbort(tid, 1);	/* Marks FS Dirty */
507 		txEnd(tid);
508 		mutex_unlock(&JFS_IP(ip)->commit_mutex);
509 		mutex_unlock(&JFS_IP(dip)->commit_mutex);
510 		IWRITE_UNLOCK(ip);
511 		goto out1;
512 	}
513 
514 	ASSERT(ip->i_nlink);
515 
516 	inode_set_mtime_to_ts(dip,
517 			      inode_set_ctime_to_ts(dip, inode_set_ctime_current(ip)));
518 	mark_inode_dirty(dip);
519 
520 	/* update target's inode */
521 	inode_dec_link_count(ip);
522 
523 	/*
524 	 *	commit zero link count object
525 	 */
526 	if (ip->i_nlink == 0) {
527 		assert(!test_cflag(COMMIT_Nolink, ip));
528 		/* free block resources */
529 		if ((new_size = commitZeroLink(tid, ip)) < 0) {
530 			txAbort(tid, 1);	/* Marks FS Dirty */
531 			txEnd(tid);
532 			mutex_unlock(&JFS_IP(ip)->commit_mutex);
533 			mutex_unlock(&JFS_IP(dip)->commit_mutex);
534 			IWRITE_UNLOCK(ip);
535 			rc = new_size;
536 			goto out1;
537 		}
538 		tblk = tid_to_tblock(tid);
539 		tblk->xflag |= COMMIT_DELETE;
540 		tblk->u.ip = ip;
541 	}
542 
543 	/*
544 	 * Incomplete truncate of file data can
545 	 * result in timing problems unless we synchronously commit the
546 	 * transaction.
547 	 */
548 	if (new_size)
549 		commit_flag = COMMIT_SYNC;
550 	else
551 		commit_flag = 0;
552 
553 	/*
554 	 * If xtTruncate was incomplete, commit synchronously to avoid
555 	 * timing complications
556 	 */
557 	rc = txCommit(tid, 2, &iplist[0], commit_flag);
558 
559 	txEnd(tid);
560 
561 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
562 	mutex_unlock(&JFS_IP(dip)->commit_mutex);
563 
564 	while (new_size && (rc == 0)) {
565 		tid = txBegin(dip->i_sb, 0);
566 		mutex_lock(&JFS_IP(ip)->commit_mutex);
567 		new_size = xtTruncate_pmap(tid, ip, new_size);
568 		if (new_size < 0) {
569 			txAbort(tid, 1);	/* Marks FS Dirty */
570 			rc = new_size;
571 		} else
572 			rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
573 		txEnd(tid);
574 		mutex_unlock(&JFS_IP(ip)->commit_mutex);
575 	}
576 
577 	if (ip->i_nlink == 0)
578 		set_cflag(COMMIT_Nolink, ip);
579 
580 	IWRITE_UNLOCK(ip);
581 
582 	/*
583 	 * Truncating the directory index table is not guaranteed.  It
584 	 * may need to be done iteratively
585 	 */
586 	if (test_cflag(COMMIT_Stale, dip)) {
587 		if (dip->i_size > 1)
588 			jfs_truncate_nolock(dip, 0);
589 
590 		clear_cflag(COMMIT_Stale, dip);
591 	}
592 
593       out1:
594 	free_UCSname(&dname);
595       out:
596 	jfs_info("jfs_unlink: rc:%d", rc);
597 	return rc;
598 }
599 
600 /*
601  * NAME:	commitZeroLink()
602  *
603  * FUNCTION:	for non-directory, called by jfs_remove(),
604  *		truncate a regular file, directory or symbolic
605  *		link to zero length. return 0 if type is not
606  *		one of these.
607  *
608  *		if the file is currently associated with a VM segment
609  *		only permanent disk and inode map resources are freed,
610  *		and neither the inode nor indirect blocks are modified
611  *		so that the resources can be later freed in the work
612  *		map by ctrunc1.
613  *		if there is no VM segment on entry, the resources are
614  *		freed in both work and permanent map.
615  *		(? for temporary file - memory object is cached even
616  *		after no reference:
617  *		reference count > 0 -   )
618  *
619  * PARAMETERS:	cd	- pointer to commit data structure.
620  *			  current inode is the one to truncate.
621  *
622  * RETURN:	Errors from subroutines
623  */
624 static s64 commitZeroLink(tid_t tid, struct inode *ip)
625 {
626 	int filetype;
627 	struct tblock *tblk;
628 
629 	jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
630 
631 	filetype = ip->i_mode & S_IFMT;
632 	switch (filetype) {
633 	case S_IFREG:
634 		break;
635 	case S_IFLNK:
636 		/* fast symbolic link */
637 		if (ip->i_size < IDATASIZE) {
638 			ip->i_size = 0;
639 			return 0;
640 		}
641 		break;
642 	default:
643 		assert(filetype != S_IFDIR);
644 		return 0;
645 	}
646 
647 	set_cflag(COMMIT_Freewmap, ip);
648 
649 	/* mark transaction of block map update type */
650 	tblk = tid_to_tblock(tid);
651 	tblk->xflag |= COMMIT_PMAP;
652 
653 	/*
654 	 * free EA
655 	 */
656 	if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
657 		/* acquire maplock on EA to be freed from block map */
658 		txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
659 
660 	/*
661 	 * free ACL
662 	 */
663 	if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
664 		/* acquire maplock on EA to be freed from block map */
665 		txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
666 
667 	/*
668 	 * free xtree/data (truncate to zero length):
669 	 * free xtree/data pages from cache if COMMIT_PWMAP,
670 	 * free xtree/data blocks from persistent block map, and
671 	 * free xtree/data blocks from working block map if COMMIT_PWMAP;
672 	 */
673 	if (ip->i_size)
674 		return xtTruncate_pmap(tid, ip, 0);
675 
676 	return 0;
677 }
678 
679 
680 /*
681  * NAME:	jfs_free_zero_link()
682  *
683  * FUNCTION:	for non-directory, called by iClose(),
684  *		free resources of a file from cache and WORKING map
685  *		for a file previously committed with zero link count
686  *		while associated with a pager object,
687  *
688  * PARAMETER:	ip	- pointer to inode of file.
689  */
690 void jfs_free_zero_link(struct inode *ip)
691 {
692 	int type;
693 
694 	jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
695 
696 	/* return if not reg or symbolic link or if size is
697 	 * already ok.
698 	 */
699 	type = ip->i_mode & S_IFMT;
700 
701 	switch (type) {
702 	case S_IFREG:
703 		break;
704 	case S_IFLNK:
705 		/* if its contained in inode nothing to do */
706 		if (ip->i_size < IDATASIZE)
707 			return;
708 		break;
709 	default:
710 		return;
711 	}
712 
713 	/*
714 	 * free EA
715 	 */
716 	if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
717 		s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
718 		int xlen = lengthDXD(&JFS_IP(ip)->ea);
719 		struct maplock maplock;	/* maplock for COMMIT_WMAP */
720 		struct pxd_lock *pxdlock;	/* maplock for COMMIT_WMAP */
721 
722 		/* free EA pages from cache */
723 		invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
724 
725 		/* free EA extent from working block map */
726 		maplock.index = 1;
727 		pxdlock = (struct pxd_lock *) & maplock;
728 		pxdlock->flag = mlckFREEPXD;
729 		PXDaddress(&pxdlock->pxd, xaddr);
730 		PXDlength(&pxdlock->pxd, xlen);
731 		txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
732 	}
733 
734 	/*
735 	 * free ACL
736 	 */
737 	if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
738 		s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
739 		int xlen = lengthDXD(&JFS_IP(ip)->acl);
740 		struct maplock maplock;	/* maplock for COMMIT_WMAP */
741 		struct pxd_lock *pxdlock;	/* maplock for COMMIT_WMAP */
742 
743 		invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
744 
745 		/* free ACL extent from working block map */
746 		maplock.index = 1;
747 		pxdlock = (struct pxd_lock *) & maplock;
748 		pxdlock->flag = mlckFREEPXD;
749 		PXDaddress(&pxdlock->pxd, xaddr);
750 		PXDlength(&pxdlock->pxd, xlen);
751 		txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
752 	}
753 
754 	/*
755 	 * free xtree/data (truncate to zero length):
756 	 * free xtree/data pages from cache, and
757 	 * free xtree/data blocks from working block map;
758 	 */
759 	if (ip->i_size)
760 		xtTruncate(0, ip, 0, COMMIT_WMAP);
761 }
762 
763 /*
764  * NAME:	jfs_link(vp, dvp, name, crp)
765  *
766  * FUNCTION:	create a link to <vp> by the name = <name>
767  *		in the parent directory <dvp>
768  *
769  * PARAMETER:	vp	- target object
770  *		dvp	- parent directory of new link
771  *		name	- name of new link to target object
772  *		crp	- credential
773  *
774  * RETURN:	Errors from subroutines
775  *
776  * note:
777  * JFS does NOT support link() on directories (to prevent circular
778  * path in the directory hierarchy);
779  * EPERM: the target object is a directory, and either the caller
780  * does not have appropriate privileges or the implementation prohibits
781  * using link() on directories [XPG4.2].
782  *
783  * JFS does NOT support links between file systems:
784  * EXDEV: target object and new link are on different file systems and
785  * implementation does not support links between file systems [XPG4.2].
786  */
787 static int jfs_link(struct dentry *old_dentry,
788 	     struct inode *dir, struct dentry *dentry)
789 {
790 	int rc;
791 	tid_t tid;
792 	struct inode *ip = d_inode(old_dentry);
793 	ino_t ino;
794 	struct component_name dname;
795 	struct btstack btstack;
796 	struct inode *iplist[2];
797 
798 	jfs_info("jfs_link: %pd %pd", old_dentry, dentry);
799 
800 	rc = dquot_initialize(dir);
801 	if (rc)
802 		goto out;
803 
804 	if (isReadOnly(ip)) {
805 		jfs_error(ip->i_sb, "read-only filesystem\n");
806 		return -EROFS;
807 	}
808 
809 	tid = txBegin(ip->i_sb, 0);
810 
811 	mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
812 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
813 
814 	/*
815 	 * scan parent directory for entry/freespace
816 	 */
817 	if ((rc = get_UCSname(&dname, dentry)))
818 		goto out_tx;
819 
820 	if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
821 		goto free_dname;
822 
823 	/*
824 	 * create entry for new link in parent directory
825 	 */
826 	ino = ip->i_ino;
827 	if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
828 		goto free_dname;
829 
830 	/* update object inode */
831 	inc_nlink(ip);		/* for new link */
832 	inode_set_ctime_current(ip);
833 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
834 	mark_inode_dirty(dir);
835 	ihold(ip);
836 
837 	iplist[0] = ip;
838 	iplist[1] = dir;
839 	rc = txCommit(tid, 2, &iplist[0], 0);
840 
841 	if (rc) {
842 		drop_nlink(ip); /* never instantiated */
843 		iput(ip);
844 	} else
845 		d_instantiate(dentry, ip);
846 
847       free_dname:
848 	free_UCSname(&dname);
849 
850       out_tx:
851 	txEnd(tid);
852 
853 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
854 	mutex_unlock(&JFS_IP(dir)->commit_mutex);
855 
856       out:
857 	jfs_info("jfs_link: rc:%d", rc);
858 	return rc;
859 }
860 
861 /*
862  * NAME:	jfs_symlink(dip, dentry, name)
863  *
864  * FUNCTION:	creates a symbolic link to <symlink> by name <name>
865  *			in directory <dip>
866  *
867  * PARAMETER:	dip	- parent directory vnode
868  *		dentry	- dentry of symbolic link
869  *		name	- the path name of the existing object
870  *			  that will be the source of the link
871  *
872  * RETURN:	errors from subroutines
873  *
874  * note:
875  * ENAMETOOLONG: pathname resolution of a symbolic link produced
876  * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
877 */
878 
879 static int jfs_symlink(struct mnt_idmap *idmap, struct inode *dip,
880 		       struct dentry *dentry, const char *name)
881 {
882 	int rc;
883 	tid_t tid;
884 	ino_t ino = 0;
885 	struct component_name dname;
886 	u32 ssize;		/* source pathname size */
887 	struct btstack btstack;
888 	struct inode *ip;
889 	s64 xlen = 0;
890 	int bmask = 0, xsize;
891 	s64 xaddr;
892 	struct metapage *mp;
893 	struct super_block *sb;
894 	struct tblock *tblk;
895 
896 	struct inode *iplist[2];
897 
898 	jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
899 
900 	rc = dquot_initialize(dip);
901 	if (rc)
902 		goto out1;
903 
904 	ssize = strlen(name) + 1;
905 
906 	/*
907 	 * search parent directory for entry/freespace
908 	 * (dtSearch() returns parent directory page pinned)
909 	 */
910 
911 	if ((rc = get_UCSname(&dname, dentry)))
912 		goto out1;
913 
914 	/*
915 	 * allocate on-disk/in-memory inode for symbolic link:
916 	 * (iAlloc() returns new, locked inode)
917 	 */
918 	ip = ialloc(dip, S_IFLNK | 0777);
919 	if (IS_ERR(ip)) {
920 		rc = PTR_ERR(ip);
921 		goto out2;
922 	}
923 
924 	tid = txBegin(dip->i_sb, 0);
925 
926 	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
927 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
928 
929 	rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
930 	if (rc)
931 		goto out3;
932 
933 	tblk = tid_to_tblock(tid);
934 	tblk->xflag |= COMMIT_CREATE;
935 	tblk->ino = ip->i_ino;
936 	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
937 
938 	/* fix symlink access permission
939 	 * (dir_create() ANDs in the u.u_cmask,
940 	 * but symlinks really need to be 777 access)
941 	 */
942 	ip->i_mode |= 0777;
943 
944 	/*
945 	 * write symbolic link target path name
946 	 */
947 	xtInitRoot(tid, ip);
948 
949 	/*
950 	 * write source path name inline in on-disk inode (fast symbolic link)
951 	 */
952 
953 	if (ssize <= IDATASIZE) {
954 		ip->i_op = &jfs_fast_symlink_inode_operations;
955 
956 		ip->i_link = JFS_IP(ip)->i_inline_all;
957 		memcpy(ip->i_link, name, ssize);
958 		ip->i_size = ssize - 1;
959 
960 		/*
961 		 * if symlink is > 128 bytes, we don't have the space to
962 		 * store inline extended attributes
963 		 */
964 		if (ssize > sizeof (JFS_IP(ip)->i_inline))
965 			JFS_IP(ip)->mode2 &= ~INLINEEA;
966 
967 		jfs_info("jfs_symlink: fast symlink added  ssize:%u name:%s ",
968 			 ssize, name);
969 	}
970 	/*
971 	 * write source path name in a single extent
972 	 */
973 	else {
974 		jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
975 
976 		ip->i_op = &jfs_symlink_inode_operations;
977 		inode_nohighmem(ip);
978 		ip->i_mapping->a_ops = &jfs_aops;
979 
980 		/*
981 		 * even though the data of symlink object (source
982 		 * path name) is treated as non-journaled user data,
983 		 * it is read/written thru buffer cache for performance.
984 		 */
985 		sb = ip->i_sb;
986 		bmask = JFS_SBI(sb)->bsize - 1;
987 		xsize = (ssize + bmask) & ~bmask;
988 		xaddr = 0;
989 		xlen = xsize >> JFS_SBI(sb)->l2bsize;
990 		if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
991 			txAbort(tid, 0);
992 			goto out3;
993 		}
994 		ip->i_size = ssize - 1;
995 		while (ssize) {
996 			/* This is kind of silly since PATH_MAX == 4K */
997 			u32 copy_size = min_t(u32, ssize, PSIZE);
998 
999 			mp = get_metapage(ip, xaddr, PSIZE, 1);
1000 
1001 			if (mp == NULL) {
1002 				xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1003 				rc = -EIO;
1004 				txAbort(tid, 0);
1005 				goto out3;
1006 			}
1007 			memcpy(mp->data, name, copy_size);
1008 			flush_metapage(mp);
1009 			ssize -= copy_size;
1010 			name += copy_size;
1011 			xaddr += JFS_SBI(sb)->nbperpage;
1012 		}
1013 	}
1014 
1015 	/*
1016 	 * create entry for symbolic link in parent directory
1017 	 */
1018 	rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1019 	if (rc == 0) {
1020 		ino = ip->i_ino;
1021 		rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1022 	}
1023 	if (rc) {
1024 		if (xlen)
1025 			xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1026 		txAbort(tid, 0);
1027 		/* discard new inode */
1028 		goto out3;
1029 	}
1030 
1031 	mark_inode_dirty(ip);
1032 
1033 	inode_set_mtime_to_ts(dip, inode_set_ctime_current(dip));
1034 	mark_inode_dirty(dip);
1035 	/*
1036 	 * commit update of parent directory and link object
1037 	 */
1038 
1039 	iplist[0] = dip;
1040 	iplist[1] = ip;
1041 	rc = txCommit(tid, 2, &iplist[0], 0);
1042 
1043       out3:
1044 	txEnd(tid);
1045 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
1046 	mutex_unlock(&JFS_IP(dip)->commit_mutex);
1047 	if (rc) {
1048 		free_ea_wmap(ip);
1049 		clear_nlink(ip);
1050 		discard_new_inode(ip);
1051 	} else {
1052 		d_instantiate_new(dentry, ip);
1053 	}
1054 
1055       out2:
1056 	free_UCSname(&dname);
1057 
1058       out1:
1059 	jfs_info("jfs_symlink: rc:%d", rc);
1060 	return rc;
1061 }
1062 
1063 
1064 /*
1065  * NAME:	jfs_rename
1066  *
1067  * FUNCTION:	rename a file or directory
1068  */
1069 static int jfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
1070 		      struct dentry *old_dentry, struct inode *new_dir,
1071 		      struct dentry *new_dentry, unsigned int flags)
1072 {
1073 	struct btstack btstack;
1074 	ino_t ino;
1075 	struct component_name new_dname;
1076 	struct inode *new_ip;
1077 	struct component_name old_dname;
1078 	struct inode *old_ip;
1079 	int rc;
1080 	tid_t tid;
1081 	struct tlock *tlck;
1082 	struct dt_lock *dtlck;
1083 	struct lv *lv;
1084 	int ipcount;
1085 	struct inode *iplist[4];
1086 	struct tblock *tblk;
1087 	s64 new_size = 0;
1088 	int commit_flag;
1089 
1090 	if (flags & ~RENAME_NOREPLACE)
1091 		return -EINVAL;
1092 
1093 	jfs_info("jfs_rename: %pd %pd", old_dentry, new_dentry);
1094 
1095 	rc = dquot_initialize(old_dir);
1096 	if (rc)
1097 		goto out1;
1098 	rc = dquot_initialize(new_dir);
1099 	if (rc)
1100 		goto out1;
1101 
1102 	old_ip = d_inode(old_dentry);
1103 	new_ip = d_inode(new_dentry);
1104 
1105 	if ((rc = get_UCSname(&old_dname, old_dentry)))
1106 		goto out1;
1107 
1108 	if ((rc = get_UCSname(&new_dname, new_dentry)))
1109 		goto out2;
1110 
1111 	/*
1112 	 * Make sure source inode number is what we think it is
1113 	 */
1114 	rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1115 	if (rc || (ino != old_ip->i_ino)) {
1116 		rc = -ENOENT;
1117 		goto out3;
1118 	}
1119 
1120 	/*
1121 	 * Make sure dest inode number (if any) is what we think it is
1122 	 */
1123 	rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1124 	if (!rc) {
1125 		if ((!new_ip) || (ino != new_ip->i_ino)) {
1126 			rc = -ESTALE;
1127 			goto out3;
1128 		}
1129 	} else if (rc != -ENOENT)
1130 		goto out3;
1131 	else if (new_ip) {
1132 		/* no entry exists, but one was expected */
1133 		rc = -ESTALE;
1134 		goto out3;
1135 	}
1136 
1137 	if (S_ISDIR(old_ip->i_mode)) {
1138 		if (new_ip) {
1139 			if (!dtEmpty(new_ip)) {
1140 				rc = -ENOTEMPTY;
1141 				goto out3;
1142 			}
1143 		}
1144 	} else if (new_ip) {
1145 		IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL);
1146 		/* Init inode for quota operations. */
1147 		rc = dquot_initialize(new_ip);
1148 		if (rc)
1149 			goto out_unlock;
1150 	}
1151 
1152 	/*
1153 	 * The real work starts here
1154 	 */
1155 	tid = txBegin(new_dir->i_sb, 0);
1156 
1157 	/*
1158 	 * How do we know the locking is safe from deadlocks?
1159 	 * The vfs does the hard part for us.  Any time we are taking nested
1160 	 * commit_mutexes, the vfs already has i_mutex held on the parent.
1161 	 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1162 	 */
1163 	mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1164 	mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD);
1165 	if (old_dir != new_dir)
1166 		mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex,
1167 				  COMMIT_MUTEX_SECOND_PARENT);
1168 
1169 	if (new_ip) {
1170 		mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex,
1171 				  COMMIT_MUTEX_VICTIM);
1172 		/*
1173 		 * Change existing directory entry to new inode number
1174 		 */
1175 		ino = new_ip->i_ino;
1176 		rc = dtModify(tid, new_dir, &new_dname, &ino,
1177 			      old_ip->i_ino, JFS_RENAME);
1178 		if (rc)
1179 			goto out_tx;
1180 		drop_nlink(new_ip);
1181 		if (S_ISDIR(new_ip->i_mode)) {
1182 			drop_nlink(new_ip);
1183 			if (new_ip->i_nlink) {
1184 				mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1185 				if (old_dir != new_dir)
1186 					mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1187 				mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1188 				mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
1189 				if (!S_ISDIR(old_ip->i_mode) && new_ip)
1190 					IWRITE_UNLOCK(new_ip);
1191 				jfs_error(new_ip->i_sb,
1192 					  "new_ip->i_nlink != 0\n");
1193 				return -EIO;
1194 			}
1195 			tblk = tid_to_tblock(tid);
1196 			tblk->xflag |= COMMIT_DELETE;
1197 			tblk->u.ip = new_ip;
1198 		} else if (new_ip->i_nlink == 0) {
1199 			assert(!test_cflag(COMMIT_Nolink, new_ip));
1200 			/* free block resources */
1201 			if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1202 				txAbort(tid, 1);	/* Marks FS Dirty */
1203 				rc = new_size;
1204 				goto out_tx;
1205 			}
1206 			tblk = tid_to_tblock(tid);
1207 			tblk->xflag |= COMMIT_DELETE;
1208 			tblk->u.ip = new_ip;
1209 		} else {
1210 			inode_set_ctime_current(new_ip);
1211 			mark_inode_dirty(new_ip);
1212 		}
1213 	} else {
1214 		/*
1215 		 * Add new directory entry
1216 		 */
1217 		rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1218 			      JFS_CREATE);
1219 		if (rc) {
1220 			jfs_err("jfs_rename didn't expect dtSearch to fail w/rc = %d",
1221 				rc);
1222 			goto out_tx;
1223 		}
1224 
1225 		ino = old_ip->i_ino;
1226 		rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1227 		if (rc) {
1228 			if (rc == -EIO)
1229 				jfs_err("jfs_rename: dtInsert returned -EIO");
1230 			goto out_tx;
1231 		}
1232 		if (S_ISDIR(old_ip->i_mode))
1233 			inc_nlink(new_dir);
1234 	}
1235 	/*
1236 	 * Remove old directory entry
1237 	 */
1238 
1239 	ino = old_ip->i_ino;
1240 	rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1241 	if (rc) {
1242 		jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1243 			rc);
1244 		txAbort(tid, 1);	/* Marks Filesystem dirty */
1245 		goto out_tx;
1246 	}
1247 	if (S_ISDIR(old_ip->i_mode)) {
1248 		drop_nlink(old_dir);
1249 		if (old_dir != new_dir) {
1250 			/*
1251 			 * Change inode number of parent for moved directory
1252 			 */
1253 
1254 			JFS_IP(old_ip)->i_dtroot.header.idotdot =
1255 				cpu_to_le32(new_dir->i_ino);
1256 
1257 			/* Linelock header of dtree */
1258 			tlck = txLock(tid, old_ip,
1259 				    (struct metapage *) &JFS_IP(old_ip)->bxflag,
1260 				      tlckDTREE | tlckBTROOT | tlckRELINK);
1261 			dtlck = (struct dt_lock *) & tlck->lock;
1262 			ASSERT(dtlck->index == 0);
1263 			lv = & dtlck->lv[0];
1264 			lv->offset = 0;
1265 			lv->length = 1;
1266 			dtlck->index++;
1267 		}
1268 	}
1269 
1270 	/*
1271 	 * Update ctime on changed/moved inodes & mark dirty
1272 	 */
1273 	inode_set_ctime_current(old_ip);
1274 	mark_inode_dirty(old_ip);
1275 
1276 	inode_set_mtime_to_ts(new_dir, inode_set_ctime_current(new_dir));
1277 	mark_inode_dirty(new_dir);
1278 
1279 	/* Build list of inodes modified by this transaction */
1280 	ipcount = 0;
1281 	iplist[ipcount++] = old_ip;
1282 	if (new_ip)
1283 		iplist[ipcount++] = new_ip;
1284 	iplist[ipcount++] = old_dir;
1285 
1286 	if (old_dir != new_dir) {
1287 		iplist[ipcount++] = new_dir;
1288 		inode_set_mtime_to_ts(old_dir,
1289 				      inode_set_ctime_current(old_dir));
1290 		mark_inode_dirty(old_dir);
1291 	}
1292 
1293 	/*
1294 	 * Incomplete truncate of file data can
1295 	 * result in timing problems unless we synchronously commit the
1296 	 * transaction.
1297 	 */
1298 	if (new_size)
1299 		commit_flag = COMMIT_SYNC;
1300 	else
1301 		commit_flag = 0;
1302 
1303 	rc = txCommit(tid, ipcount, iplist, commit_flag);
1304 
1305       out_tx:
1306 	txEnd(tid);
1307 	if (new_ip)
1308 		mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1309 	if (old_dir != new_dir)
1310 		mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1311 	mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1312 	mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
1313 
1314 	while (new_size && (rc == 0)) {
1315 		tid = txBegin(new_ip->i_sb, 0);
1316 		mutex_lock(&JFS_IP(new_ip)->commit_mutex);
1317 		new_size = xtTruncate_pmap(tid, new_ip, new_size);
1318 		if (new_size < 0) {
1319 			txAbort(tid, 1);
1320 			rc = new_size;
1321 		} else
1322 			rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1323 		txEnd(tid);
1324 		mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1325 	}
1326 	if (new_ip && (new_ip->i_nlink == 0))
1327 		set_cflag(COMMIT_Nolink, new_ip);
1328 	/*
1329 	 * Truncating the directory index table is not guaranteed.  It
1330 	 * may need to be done iteratively
1331 	 */
1332 	if (test_cflag(COMMIT_Stale, old_dir)) {
1333 		if (old_dir->i_size > 1)
1334 			jfs_truncate_nolock(old_dir, 0);
1335 
1336 		clear_cflag(COMMIT_Stale, old_dir);
1337 	}
1338       out_unlock:
1339 	if (new_ip && !S_ISDIR(new_ip->i_mode))
1340 		IWRITE_UNLOCK(new_ip);
1341       out3:
1342 	free_UCSname(&new_dname);
1343       out2:
1344 	free_UCSname(&old_dname);
1345       out1:
1346 	jfs_info("jfs_rename: returning %d", rc);
1347 	return rc;
1348 }
1349 
1350 
1351 /*
1352  * NAME:	jfs_mknod
1353  *
1354  * FUNCTION:	Create a special file (device)
1355  */
1356 static int jfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
1357 		     struct dentry *dentry, umode_t mode, dev_t rdev)
1358 {
1359 	struct jfs_inode_info *jfs_ip;
1360 	struct btstack btstack;
1361 	struct component_name dname;
1362 	ino_t ino;
1363 	struct inode *ip;
1364 	struct inode *iplist[2];
1365 	int rc;
1366 	tid_t tid;
1367 	struct tblock *tblk;
1368 
1369 	jfs_info("jfs_mknod: %pd", dentry);
1370 
1371 	rc = dquot_initialize(dir);
1372 	if (rc)
1373 		goto out;
1374 
1375 	if ((rc = get_UCSname(&dname, dentry)))
1376 		goto out;
1377 
1378 	ip = ialloc(dir, mode);
1379 	if (IS_ERR(ip)) {
1380 		rc = PTR_ERR(ip);
1381 		goto out1;
1382 	}
1383 	jfs_ip = JFS_IP(ip);
1384 
1385 	tid = txBegin(dir->i_sb, 0);
1386 
1387 	mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1388 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
1389 
1390 	rc = jfs_init_acl(tid, ip, dir);
1391 	if (rc)
1392 		goto out3;
1393 
1394 	rc = jfs_init_security(tid, ip, dir, &dentry->d_name);
1395 	if (rc) {
1396 		txAbort(tid, 0);
1397 		goto out3;
1398 	}
1399 
1400 	if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1401 		txAbort(tid, 0);
1402 		goto out3;
1403 	}
1404 
1405 	tblk = tid_to_tblock(tid);
1406 	tblk->xflag |= COMMIT_CREATE;
1407 	tblk->ino = ip->i_ino;
1408 	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1409 
1410 	ino = ip->i_ino;
1411 	if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1412 		txAbort(tid, 0);
1413 		goto out3;
1414 	}
1415 
1416 	ip->i_op = &jfs_file_inode_operations;
1417 	jfs_ip->dev = new_encode_dev(rdev);
1418 	init_special_inode(ip, ip->i_mode, rdev);
1419 
1420 	mark_inode_dirty(ip);
1421 
1422 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
1423 
1424 	mark_inode_dirty(dir);
1425 
1426 	iplist[0] = dir;
1427 	iplist[1] = ip;
1428 	rc = txCommit(tid, 2, iplist, 0);
1429 
1430       out3:
1431 	txEnd(tid);
1432 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
1433 	mutex_unlock(&JFS_IP(dir)->commit_mutex);
1434 	if (rc) {
1435 		free_ea_wmap(ip);
1436 		clear_nlink(ip);
1437 		discard_new_inode(ip);
1438 	} else {
1439 		d_instantiate_new(dentry, ip);
1440 	}
1441 
1442       out1:
1443 	free_UCSname(&dname);
1444 
1445       out:
1446 	jfs_info("jfs_mknod: returning %d", rc);
1447 	return rc;
1448 }
1449 
1450 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
1451 {
1452 	struct btstack btstack;
1453 	ino_t inum;
1454 	struct inode *ip;
1455 	struct component_name key;
1456 	int rc;
1457 
1458 	jfs_info("jfs_lookup: name = %pd", dentry);
1459 
1460 	if ((rc = get_UCSname(&key, dentry)))
1461 		return ERR_PTR(rc);
1462 	rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1463 	free_UCSname(&key);
1464 	if (rc == -ENOENT) {
1465 		ip = NULL;
1466 	} else if (rc) {
1467 		jfs_err("jfs_lookup: dtSearch returned %d", rc);
1468 		ip = ERR_PTR(rc);
1469 	} else {
1470 		ip = jfs_iget(dip->i_sb, inum);
1471 		if (IS_ERR(ip))
1472 			jfs_err("jfs_lookup: iget failed on inum %d", (uint)inum);
1473 	}
1474 
1475 	return d_splice_alias(ip, dentry);
1476 }
1477 
1478 static struct inode *jfs_nfs_get_inode(struct super_block *sb,
1479 		u64 ino, u32 generation)
1480 {
1481 	struct inode *inode;
1482 
1483 	if (ino == 0)
1484 		return ERR_PTR(-ESTALE);
1485 	inode = jfs_iget(sb, ino);
1486 	if (IS_ERR(inode))
1487 		return ERR_CAST(inode);
1488 
1489 	if (generation && inode->i_generation != generation) {
1490 		iput(inode);
1491 		return ERR_PTR(-ESTALE);
1492 	}
1493 
1494 	return inode;
1495 }
1496 
1497 struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1498 		int fh_len, int fh_type)
1499 {
1500 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1501 				    jfs_nfs_get_inode);
1502 }
1503 
1504 struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1505 		int fh_len, int fh_type)
1506 {
1507 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1508 				    jfs_nfs_get_inode);
1509 }
1510 
1511 struct dentry *jfs_get_parent(struct dentry *dentry)
1512 {
1513 	unsigned long parent_ino;
1514 
1515 	parent_ino =
1516 		le32_to_cpu(JFS_IP(d_inode(dentry))->i_dtroot.header.idotdot);
1517 
1518 	return d_obtain_alias(jfs_iget(dentry->d_sb, parent_ino));
1519 }
1520 
1521 const struct inode_operations jfs_dir_inode_operations = {
1522 	.create		= jfs_create,
1523 	.lookup		= jfs_lookup,
1524 	.link		= jfs_link,
1525 	.unlink		= jfs_unlink,
1526 	.symlink	= jfs_symlink,
1527 	.mkdir		= jfs_mkdir,
1528 	.rmdir		= jfs_rmdir,
1529 	.mknod		= jfs_mknod,
1530 	.rename		= jfs_rename,
1531 	.listxattr	= jfs_listxattr,
1532 	.setattr	= jfs_setattr,
1533 	.fileattr_get	= jfs_fileattr_get,
1534 	.fileattr_set	= jfs_fileattr_set,
1535 #ifdef CONFIG_JFS_POSIX_ACL
1536 	.get_inode_acl	= jfs_get_acl,
1537 	.set_acl	= jfs_set_acl,
1538 #endif
1539 };
1540 
1541 WRAP_DIR_ITER(jfs_readdir) // FIXME!
1542 const struct file_operations jfs_dir_operations = {
1543 	.read		= generic_read_dir,
1544 	.iterate_shared	= shared_jfs_readdir,
1545 	.fsync		= jfs_fsync,
1546 	.unlocked_ioctl = jfs_ioctl,
1547 	.compat_ioctl	= compat_ptr_ioctl,
1548 	.llseek		= generic_file_llseek,
1549 	.setlease	= generic_setlease,
1550 };
1551 
1552 static int jfs_ci_hash(const struct dentry *dir, struct qstr *this)
1553 {
1554 	unsigned long hash;
1555 	int i;
1556 
1557 	hash = init_name_hash(dir);
1558 	for (i=0; i < this->len; i++)
1559 		hash = partial_name_hash(tolower(this->name[i]), hash);
1560 	this->hash = end_name_hash(hash);
1561 
1562 	return 0;
1563 }
1564 
1565 static int jfs_ci_compare(const struct dentry *dentry,
1566 		unsigned int len, const char *str, const struct qstr *name)
1567 {
1568 	int i, result = 1;
1569 
1570 	if (len != name->len)
1571 		goto out;
1572 	for (i=0; i < len; i++) {
1573 		if (tolower(str[i]) != tolower(name->name[i]))
1574 			goto out;
1575 	}
1576 	result = 0;
1577 out:
1578 	return result;
1579 }
1580 
1581 static int jfs_ci_revalidate(struct inode *dir, const struct qstr *name,
1582 			     struct dentry *dentry, unsigned int flags)
1583 {
1584 	/*
1585 	 * This is not negative dentry. Always valid.
1586 	 *
1587 	 * Note, rename() to existing directory entry will have ->d_inode,
1588 	 * and will use existing name which isn't specified name by user.
1589 	 *
1590 	 * We may be able to drop this positive dentry here. But dropping
1591 	 * positive dentry isn't good idea. So it's unsupported like
1592 	 * rename("filename", "FILENAME") for now.
1593 	 */
1594 	if (d_really_is_positive(dentry))
1595 		return 1;
1596 
1597 	/*
1598 	 * This may be nfsd (or something), anyway, we can't see the
1599 	 * intent of this. So, since this can be for creation, drop it.
1600 	 */
1601 	if (!flags)
1602 		return 0;
1603 
1604 	/*
1605 	 * Drop the negative dentry, in order to make sure to use the
1606 	 * case sensitive name which is specified by user if this is
1607 	 * for creation.
1608 	 */
1609 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
1610 		return 0;
1611 	return 1;
1612 }
1613 
1614 const struct dentry_operations jfs_ci_dentry_operations =
1615 {
1616 	.d_hash = jfs_ci_hash,
1617 	.d_compare = jfs_ci_compare,
1618 	.d_revalidate = jfs_ci_revalidate,
1619 };
1620