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