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