xref: /openbmc/linux/fs/jfs/namei.c (revision 79bf7c73)
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/namei.h>
22 #include <linux/ctype.h>
23 #include <linux/quotaops.h>
24 #include <linux/exportfs.h>
25 #include "jfs_incore.h"
26 #include "jfs_superblock.h"
27 #include "jfs_inode.h"
28 #include "jfs_dinode.h"
29 #include "jfs_dmap.h"
30 #include "jfs_unicode.h"
31 #include "jfs_metapage.h"
32 #include "jfs_xattr.h"
33 #include "jfs_acl.h"
34 #include "jfs_debug.h"
35 
36 /*
37  * forward references
38  */
39 const struct dentry_operations jfs_ci_dentry_operations;
40 
41 static s64 commitZeroLink(tid_t, struct inode *);
42 
43 /*
44  * NAME:	free_ea_wmap(inode)
45  *
46  * FUNCTION:	free uncommitted extended attributes from working map
47  *
48  */
49 static inline void free_ea_wmap(struct inode *inode)
50 {
51 	dxd_t *ea = &JFS_IP(inode)->ea;
52 
53 	if (ea->flag & DXD_EXTENT) {
54 		/* free EA pages from cache */
55 		invalidate_dxd_metapages(inode, *ea);
56 		dbFree(inode, addressDXD(ea), lengthDXD(ea));
57 	}
58 	ea->flag = 0;
59 }
60 
61 /*
62  * NAME:	jfs_create(dip, dentry, mode)
63  *
64  * FUNCTION:	create a regular file in the parent directory <dip>
65  *		with name = <from dentry> and mode = <mode>
66  *
67  * PARAMETER:	dip	- parent directory vnode
68  *		dentry	- dentry of new file
69  *		mode	- create mode (rwxrwxrwx).
70  *		nd- nd struct
71  *
72  * RETURN:	Errors from subroutines
73  *
74  */
75 static int jfs_create(struct inode *dip, struct dentry *dentry, int mode,
76 		struct nameidata *nd)
77 {
78 	int rc = 0;
79 	tid_t tid;		/* transaction id */
80 	struct inode *ip = NULL;	/* child directory inode */
81 	ino_t ino;
82 	struct component_name dname;	/* child directory name */
83 	struct btstack btstack;
84 	struct inode *iplist[2];
85 	struct tblock *tblk;
86 
87 	jfs_info("jfs_create: dip:0x%p name:%s", dip, dentry->d_name.name);
88 
89 	dquot_initialize(dip);
90 
91 	/*
92 	 * search parent directory for entry/freespace
93 	 * (dtSearch() returns parent directory page pinned)
94 	 */
95 	if ((rc = get_UCSname(&dname, dentry)))
96 		goto out1;
97 
98 	/*
99 	 * Either iAlloc() or txBegin() may block.  Deadlock can occur if we
100 	 * block there while holding dtree page, so we allocate the inode &
101 	 * begin the transaction before we search the directory.
102 	 */
103 	ip = ialloc(dip, mode);
104 	if (IS_ERR(ip)) {
105 		rc = PTR_ERR(ip);
106 		goto out2;
107 	}
108 
109 	tid = txBegin(dip->i_sb, 0);
110 
111 	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
112 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
113 
114 	rc = jfs_init_acl(tid, ip, dip);
115 	if (rc)
116 		goto out3;
117 
118 	rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
119 	if (rc) {
120 		txAbort(tid, 0);
121 		goto out3;
122 	}
123 
124 	if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
125 		jfs_err("jfs_create: dtSearch returned %d", rc);
126 		txAbort(tid, 0);
127 		goto out3;
128 	}
129 
130 	tblk = tid_to_tblock(tid);
131 	tblk->xflag |= COMMIT_CREATE;
132 	tblk->ino = ip->i_ino;
133 	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
134 
135 	iplist[0] = dip;
136 	iplist[1] = ip;
137 
138 	/*
139 	 * initialize the child XAD tree root in-line in inode
140 	 */
141 	xtInitRoot(tid, ip);
142 
143 	/*
144 	 * create entry in parent directory for child directory
145 	 * (dtInsert() releases parent directory page)
146 	 */
147 	ino = ip->i_ino;
148 	if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
149 		if (rc == -EIO) {
150 			jfs_err("jfs_create: dtInsert returned -EIO");
151 			txAbort(tid, 1);	/* Marks Filesystem dirty */
152 		} else
153 			txAbort(tid, 0);	/* Filesystem full */
154 		goto out3;
155 	}
156 
157 	ip->i_op = &jfs_file_inode_operations;
158 	ip->i_fop = &jfs_file_operations;
159 	ip->i_mapping->a_ops = &jfs_aops;
160 
161 	mark_inode_dirty(ip);
162 
163 	dip->i_ctime = dip->i_mtime = CURRENT_TIME;
164 
165 	mark_inode_dirty(dip);
166 
167 	rc = txCommit(tid, 2, &iplist[0], 0);
168 
169       out3:
170 	txEnd(tid);
171 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
172 	mutex_unlock(&JFS_IP(dip)->commit_mutex);
173 	if (rc) {
174 		free_ea_wmap(ip);
175 		ip->i_nlink = 0;
176 		unlock_new_inode(ip);
177 		iput(ip);
178 	} else {
179 		d_instantiate(dentry, ip);
180 		unlock_new_inode(ip);
181 	}
182 
183       out2:
184 	free_UCSname(&dname);
185 
186       out1:
187 
188 	jfs_info("jfs_create: rc:%d", rc);
189 	return rc;
190 }
191 
192 
193 /*
194  * NAME:	jfs_mkdir(dip, dentry, mode)
195  *
196  * FUNCTION:	create a child directory in the parent directory <dip>
197  *		with name = <from dentry> and mode = <mode>
198  *
199  * PARAMETER:	dip	- parent directory vnode
200  *		dentry	- dentry of child directory
201  *		mode	- create mode (rwxrwxrwx).
202  *
203  * RETURN:	Errors from subroutines
204  *
205  * note:
206  * EACCESS: user needs search+write permission on the parent directory
207  */
208 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
209 {
210 	int rc = 0;
211 	tid_t tid;		/* transaction id */
212 	struct inode *ip = NULL;	/* child directory inode */
213 	ino_t ino;
214 	struct component_name dname;	/* child directory name */
215 	struct btstack btstack;
216 	struct inode *iplist[2];
217 	struct tblock *tblk;
218 
219 	jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
220 
221 	dquot_initialize(dip);
222 
223 	/* link count overflow on parent directory ? */
224 	if (dip->i_nlink == JFS_LINK_MAX) {
225 		rc = -EMLINK;
226 		goto out1;
227 	}
228 
229 	/*
230 	 * search parent directory for entry/freespace
231 	 * (dtSearch() returns parent directory page pinned)
232 	 */
233 	if ((rc = get_UCSname(&dname, dentry)))
234 		goto out1;
235 
236 	/*
237 	 * Either iAlloc() or txBegin() may block.  Deadlock can occur if we
238 	 * block there while holding dtree page, so we allocate the inode &
239 	 * begin the transaction before we search the directory.
240 	 */
241 	ip = ialloc(dip, S_IFDIR | mode);
242 	if (IS_ERR(ip)) {
243 		rc = PTR_ERR(ip);
244 		goto out2;
245 	}
246 
247 	tid = txBegin(dip->i_sb, 0);
248 
249 	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
250 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
251 
252 	rc = jfs_init_acl(tid, ip, dip);
253 	if (rc)
254 		goto out3;
255 
256 	rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
257 	if (rc) {
258 		txAbort(tid, 0);
259 		goto out3;
260 	}
261 
262 	if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
263 		jfs_err("jfs_mkdir: dtSearch returned %d", rc);
264 		txAbort(tid, 0);
265 		goto out3;
266 	}
267 
268 	tblk = tid_to_tblock(tid);
269 	tblk->xflag |= COMMIT_CREATE;
270 	tblk->ino = ip->i_ino;
271 	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
272 
273 	iplist[0] = dip;
274 	iplist[1] = ip;
275 
276 	/*
277 	 * initialize the child directory in-line in inode
278 	 */
279 	dtInitRoot(tid, ip, dip->i_ino);
280 
281 	/*
282 	 * create entry in parent directory for child directory
283 	 * (dtInsert() releases parent directory page)
284 	 */
285 	ino = ip->i_ino;
286 	if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
287 		if (rc == -EIO) {
288 			jfs_err("jfs_mkdir: dtInsert returned -EIO");
289 			txAbort(tid, 1);	/* Marks Filesystem dirty */
290 		} else
291 			txAbort(tid, 0);	/* Filesystem full */
292 		goto out3;
293 	}
294 
295 	ip->i_nlink = 2;	/* for '.' */
296 	ip->i_op = &jfs_dir_inode_operations;
297 	ip->i_fop = &jfs_dir_operations;
298 
299 	mark_inode_dirty(ip);
300 
301 	/* update parent directory inode */
302 	inc_nlink(dip);		/* for '..' from child directory */
303 	dip->i_ctime = dip->i_mtime = CURRENT_TIME;
304 	mark_inode_dirty(dip);
305 
306 	rc = txCommit(tid, 2, &iplist[0], 0);
307 
308       out3:
309 	txEnd(tid);
310 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
311 	mutex_unlock(&JFS_IP(dip)->commit_mutex);
312 	if (rc) {
313 		free_ea_wmap(ip);
314 		ip->i_nlink = 0;
315 		unlock_new_inode(ip);
316 		iput(ip);
317 	} else {
318 		d_instantiate(dentry, ip);
319 		unlock_new_inode(ip);
320 	}
321 
322       out2:
323 	free_UCSname(&dname);
324 
325 
326       out1:
327 
328 	jfs_info("jfs_mkdir: rc:%d", rc);
329 	return rc;
330 }
331 
332 /*
333  * NAME:	jfs_rmdir(dip, dentry)
334  *
335  * FUNCTION:	remove a link to child directory
336  *
337  * PARAMETER:	dip	- parent inode
338  *		dentry	- child directory dentry
339  *
340  * RETURN:	-EINVAL	- if name is . or ..
341  *		-EINVAL - if . or .. exist but are invalid.
342  *		errors from subroutines
343  *
344  * note:
345  * if other threads have the directory open when the last link
346  * is removed, the "." and ".." entries, if present, are removed before
347  * rmdir() returns and no new entries may be created in the directory,
348  * but the directory is not removed until the last reference to
349  * the directory is released (cf.unlink() of regular file).
350  */
351 static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
352 {
353 	int rc;
354 	tid_t tid;		/* transaction id */
355 	struct inode *ip = dentry->d_inode;
356 	ino_t ino;
357 	struct component_name dname;
358 	struct inode *iplist[2];
359 	struct tblock *tblk;
360 
361 	jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
362 
363 	dentry_unhash(dentry);
364 
365 	/* Init inode for quota operations. */
366 	dquot_initialize(dip);
367 	dquot_initialize(ip);
368 
369 	/* directory must be empty to be removed */
370 	if (!dtEmpty(ip)) {
371 		rc = -ENOTEMPTY;
372 		goto out;
373 	}
374 
375 	if ((rc = get_UCSname(&dname, dentry))) {
376 		goto out;
377 	}
378 
379 	tid = txBegin(dip->i_sb, 0);
380 
381 	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
382 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
383 
384 	iplist[0] = dip;
385 	iplist[1] = ip;
386 
387 	tblk = tid_to_tblock(tid);
388 	tblk->xflag |= COMMIT_DELETE;
389 	tblk->u.ip = ip;
390 
391 	/*
392 	 * delete the entry of target directory from parent directory
393 	 */
394 	ino = ip->i_ino;
395 	if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
396 		jfs_err("jfs_rmdir: dtDelete returned %d", rc);
397 		if (rc == -EIO)
398 			txAbort(tid, 1);
399 		txEnd(tid);
400 		mutex_unlock(&JFS_IP(ip)->commit_mutex);
401 		mutex_unlock(&JFS_IP(dip)->commit_mutex);
402 
403 		goto out2;
404 	}
405 
406 	/* update parent directory's link count corresponding
407 	 * to ".." entry of the target directory deleted
408 	 */
409 	dip->i_ctime = dip->i_mtime = CURRENT_TIME;
410 	inode_dec_link_count(dip);
411 
412 	/*
413 	 * OS/2 could have created EA and/or ACL
414 	 */
415 	/* free EA from both persistent and working map */
416 	if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
417 		/* free EA pages */
418 		txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
419 	}
420 	JFS_IP(ip)->ea.flag = 0;
421 
422 	/* free ACL from both persistent and working map */
423 	if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
424 		/* free ACL pages */
425 		txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
426 	}
427 	JFS_IP(ip)->acl.flag = 0;
428 
429 	/* mark the target directory as deleted */
430 	clear_nlink(ip);
431 	mark_inode_dirty(ip);
432 
433 	rc = txCommit(tid, 2, &iplist[0], 0);
434 
435 	txEnd(tid);
436 
437 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
438 	mutex_unlock(&JFS_IP(dip)->commit_mutex);
439 
440 	/*
441 	 * Truncating the directory index table is not guaranteed.  It
442 	 * may need to be done iteratively
443 	 */
444 	if (test_cflag(COMMIT_Stale, dip)) {
445 		if (dip->i_size > 1)
446 			jfs_truncate_nolock(dip, 0);
447 
448 		clear_cflag(COMMIT_Stale, dip);
449 	}
450 
451       out2:
452 	free_UCSname(&dname);
453 
454       out:
455 	jfs_info("jfs_rmdir: rc:%d", rc);
456 	return rc;
457 }
458 
459 /*
460  * NAME:	jfs_unlink(dip, dentry)
461  *
462  * FUNCTION:	remove a link to object <vp> named by <name>
463  *		from parent directory <dvp>
464  *
465  * PARAMETER:	dip	- inode of parent directory
466  *		dentry	- dentry of object to be removed
467  *
468  * RETURN:	errors from subroutines
469  *
470  * note:
471  * temporary file: if one or more processes have the file open
472  * when the last link is removed, the link will be removed before
473  * unlink() returns, but the removal of the file contents will be
474  * postponed until all references to the files are closed.
475  *
476  * JFS does NOT support unlink() on directories.
477  *
478  */
479 static int jfs_unlink(struct inode *dip, struct dentry *dentry)
480 {
481 	int rc;
482 	tid_t tid;		/* transaction id */
483 	struct inode *ip = dentry->d_inode;
484 	ino_t ino;
485 	struct component_name dname;	/* object name */
486 	struct inode *iplist[2];
487 	struct tblock *tblk;
488 	s64 new_size = 0;
489 	int commit_flag;
490 
491 	jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
492 
493 	/* Init inode for quota operations. */
494 	dquot_initialize(dip);
495 	dquot_initialize(ip);
496 
497 	if ((rc = get_UCSname(&dname, dentry)))
498 		goto out;
499 
500 	IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
501 
502 	tid = txBegin(dip->i_sb, 0);
503 
504 	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
505 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
506 
507 	iplist[0] = dip;
508 	iplist[1] = ip;
509 
510 	/*
511 	 * delete the entry of target file from parent directory
512 	 */
513 	ino = ip->i_ino;
514 	if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
515 		jfs_err("jfs_unlink: dtDelete returned %d", rc);
516 		if (rc == -EIO)
517 			txAbort(tid, 1);	/* Marks FS Dirty */
518 		txEnd(tid);
519 		mutex_unlock(&JFS_IP(ip)->commit_mutex);
520 		mutex_unlock(&JFS_IP(dip)->commit_mutex);
521 		IWRITE_UNLOCK(ip);
522 		goto out1;
523 	}
524 
525 	ASSERT(ip->i_nlink);
526 
527 	ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
528 	mark_inode_dirty(dip);
529 
530 	/* update target's inode */
531 	inode_dec_link_count(ip);
532 
533 	/*
534 	 *	commit zero link count object
535 	 */
536 	if (ip->i_nlink == 0) {
537 		assert(!test_cflag(COMMIT_Nolink, ip));
538 		/* free block resources */
539 		if ((new_size = commitZeroLink(tid, ip)) < 0) {
540 			txAbort(tid, 1);	/* Marks FS Dirty */
541 			txEnd(tid);
542 			mutex_unlock(&JFS_IP(ip)->commit_mutex);
543 			mutex_unlock(&JFS_IP(dip)->commit_mutex);
544 			IWRITE_UNLOCK(ip);
545 			rc = new_size;
546 			goto out1;
547 		}
548 		tblk = tid_to_tblock(tid);
549 		tblk->xflag |= COMMIT_DELETE;
550 		tblk->u.ip = ip;
551 	}
552 
553 	/*
554 	 * Incomplete truncate of file data can
555 	 * result in timing problems unless we synchronously commit the
556 	 * transaction.
557 	 */
558 	if (new_size)
559 		commit_flag = COMMIT_SYNC;
560 	else
561 		commit_flag = 0;
562 
563 	/*
564 	 * If xtTruncate was incomplete, commit synchronously to avoid
565 	 * timing complications
566 	 */
567 	rc = txCommit(tid, 2, &iplist[0], commit_flag);
568 
569 	txEnd(tid);
570 
571 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
572 	mutex_unlock(&JFS_IP(dip)->commit_mutex);
573 
574 	while (new_size && (rc == 0)) {
575 		tid = txBegin(dip->i_sb, 0);
576 		mutex_lock(&JFS_IP(ip)->commit_mutex);
577 		new_size = xtTruncate_pmap(tid, ip, new_size);
578 		if (new_size < 0) {
579 			txAbort(tid, 1);	/* Marks FS Dirty */
580 			rc = new_size;
581 		} else
582 			rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
583 		txEnd(tid);
584 		mutex_unlock(&JFS_IP(ip)->commit_mutex);
585 	}
586 
587 	if (ip->i_nlink == 0)
588 		set_cflag(COMMIT_Nolink, ip);
589 
590 	IWRITE_UNLOCK(ip);
591 
592 	/*
593 	 * Truncating the directory index table is not guaranteed.  It
594 	 * may need to be done iteratively
595 	 */
596 	if (test_cflag(COMMIT_Stale, dip)) {
597 		if (dip->i_size > 1)
598 			jfs_truncate_nolock(dip, 0);
599 
600 		clear_cflag(COMMIT_Stale, dip);
601 	}
602 
603       out1:
604 	free_UCSname(&dname);
605       out:
606 	jfs_info("jfs_unlink: rc:%d", rc);
607 	return rc;
608 }
609 
610 /*
611  * NAME:	commitZeroLink()
612  *
613  * FUNCTION:	for non-directory, called by jfs_remove(),
614  *		truncate a regular file, directory or symbolic
615  *		link to zero length. return 0 if type is not
616  *		one of these.
617  *
618  *		if the file is currently associated with a VM segment
619  *		only permanent disk and inode map resources are freed,
620  *		and neither the inode nor indirect blocks are modified
621  *		so that the resources can be later freed in the work
622  *		map by ctrunc1.
623  *		if there is no VM segment on entry, the resources are
624  *		freed in both work and permanent map.
625  *		(? for temporary file - memory object is cached even
626  *		after no reference:
627  *		reference count > 0 -   )
628  *
629  * PARAMETERS:	cd	- pointer to commit data structure.
630  *			  current inode is the one to truncate.
631  *
632  * RETURN:	Errors from subroutines
633  */
634 static s64 commitZeroLink(tid_t tid, struct inode *ip)
635 {
636 	int filetype;
637 	struct tblock *tblk;
638 
639 	jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
640 
641 	filetype = ip->i_mode & S_IFMT;
642 	switch (filetype) {
643 	case S_IFREG:
644 		break;
645 	case S_IFLNK:
646 		/* fast symbolic link */
647 		if (ip->i_size < IDATASIZE) {
648 			ip->i_size = 0;
649 			return 0;
650 		}
651 		break;
652 	default:
653 		assert(filetype != S_IFDIR);
654 		return 0;
655 	}
656 
657 	set_cflag(COMMIT_Freewmap, ip);
658 
659 	/* mark transaction of block map update type */
660 	tblk = tid_to_tblock(tid);
661 	tblk->xflag |= COMMIT_PMAP;
662 
663 	/*
664 	 * free EA
665 	 */
666 	if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
667 		/* acquire maplock on EA to be freed from block map */
668 		txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
669 
670 	/*
671 	 * free ACL
672 	 */
673 	if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
674 		/* acquire maplock on EA to be freed from block map */
675 		txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
676 
677 	/*
678 	 * free xtree/data (truncate to zero length):
679 	 * free xtree/data pages from cache if COMMIT_PWMAP,
680 	 * free xtree/data blocks from persistent block map, and
681 	 * free xtree/data blocks from working block map if COMMIT_PWMAP;
682 	 */
683 	if (ip->i_size)
684 		return xtTruncate_pmap(tid, ip, 0);
685 
686 	return 0;
687 }
688 
689 
690 /*
691  * NAME:	jfs_free_zero_link()
692  *
693  * FUNCTION:	for non-directory, called by iClose(),
694  *		free resources of a file from cache and WORKING map
695  *		for a file previously committed with zero link count
696  *		while associated with a pager object,
697  *
698  * PARAMETER:	ip	- pointer to inode of file.
699  */
700 void jfs_free_zero_link(struct inode *ip)
701 {
702 	int type;
703 
704 	jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
705 
706 	/* return if not reg or symbolic link or if size is
707 	 * already ok.
708 	 */
709 	type = ip->i_mode & S_IFMT;
710 
711 	switch (type) {
712 	case S_IFREG:
713 		break;
714 	case S_IFLNK:
715 		/* if its contained in inode nothing to do */
716 		if (ip->i_size < IDATASIZE)
717 			return;
718 		break;
719 	default:
720 		return;
721 	}
722 
723 	/*
724 	 * free EA
725 	 */
726 	if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
727 		s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
728 		int xlen = lengthDXD(&JFS_IP(ip)->ea);
729 		struct maplock maplock;	/* maplock for COMMIT_WMAP */
730 		struct pxd_lock *pxdlock;	/* maplock for COMMIT_WMAP */
731 
732 		/* free EA pages from cache */
733 		invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
734 
735 		/* free EA extent from working block map */
736 		maplock.index = 1;
737 		pxdlock = (struct pxd_lock *) & maplock;
738 		pxdlock->flag = mlckFREEPXD;
739 		PXDaddress(&pxdlock->pxd, xaddr);
740 		PXDlength(&pxdlock->pxd, xlen);
741 		txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
742 	}
743 
744 	/*
745 	 * free ACL
746 	 */
747 	if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
748 		s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
749 		int xlen = lengthDXD(&JFS_IP(ip)->acl);
750 		struct maplock maplock;	/* maplock for COMMIT_WMAP */
751 		struct pxd_lock *pxdlock;	/* maplock for COMMIT_WMAP */
752 
753 		invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
754 
755 		/* free ACL extent from working block map */
756 		maplock.index = 1;
757 		pxdlock = (struct pxd_lock *) & maplock;
758 		pxdlock->flag = mlckFREEPXD;
759 		PXDaddress(&pxdlock->pxd, xaddr);
760 		PXDlength(&pxdlock->pxd, xlen);
761 		txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
762 	}
763 
764 	/*
765 	 * free xtree/data (truncate to zero length):
766 	 * free xtree/data pages from cache, and
767 	 * free xtree/data blocks from working block map;
768 	 */
769 	if (ip->i_size)
770 		xtTruncate(0, ip, 0, COMMIT_WMAP);
771 }
772 
773 /*
774  * NAME:	jfs_link(vp, dvp, name, crp)
775  *
776  * FUNCTION:	create a link to <vp> by the name = <name>
777  *		in the parent directory <dvp>
778  *
779  * PARAMETER:	vp	- target object
780  *		dvp	- parent directory of new link
781  *		name	- name of new link to target object
782  *		crp	- credential
783  *
784  * RETURN:	Errors from subroutines
785  *
786  * note:
787  * JFS does NOT support link() on directories (to prevent circular
788  * path in the directory hierarchy);
789  * EPERM: the target object is a directory, and either the caller
790  * does not have appropriate privileges or the implementation prohibits
791  * using link() on directories [XPG4.2].
792  *
793  * JFS does NOT support links between file systems:
794  * EXDEV: target object and new link are on different file systems and
795  * implementation does not support links between file systems [XPG4.2].
796  */
797 static int jfs_link(struct dentry *old_dentry,
798 	     struct inode *dir, struct dentry *dentry)
799 {
800 	int rc;
801 	tid_t tid;
802 	struct inode *ip = old_dentry->d_inode;
803 	ino_t ino;
804 	struct component_name dname;
805 	struct btstack btstack;
806 	struct inode *iplist[2];
807 
808 	jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
809 		 dentry->d_name.name);
810 
811 	if (ip->i_nlink == JFS_LINK_MAX)
812 		return -EMLINK;
813 
814 	dquot_initialize(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 	ihold(ip);
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 	dquot_initialize(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, &dentry->d_name);
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_fast_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 = &jfs_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 	dquot_initialize(old_dir);
1101 	dquot_initialize(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 		dquot_initialize(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 	dquot_initialize(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, &dentry->d_name);
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 ((name[0] == '.') && (len == 1))
1468 		inum = dip->i_ino;
1469 	else if (strcmp(name, "..") == 0)
1470 		inum = PARENT(dip);
1471 	else {
1472 		if ((rc = get_UCSname(&key, dentry)))
1473 			return ERR_PTR(rc);
1474 		rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1475 		free_UCSname(&key);
1476 		if (rc == -ENOENT) {
1477 			d_add(dentry, NULL);
1478 			return NULL;
1479 		} else if (rc) {
1480 			jfs_err("jfs_lookup: dtSearch returned %d", rc);
1481 			return ERR_PTR(rc);
1482 		}
1483 	}
1484 
1485 	ip = jfs_iget(dip->i_sb, inum);
1486 	if (IS_ERR(ip)) {
1487 		jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1488 		return ERR_CAST(ip);
1489 	}
1490 
1491 	return d_splice_alias(ip, dentry);
1492 }
1493 
1494 static struct inode *jfs_nfs_get_inode(struct super_block *sb,
1495 		u64 ino, u32 generation)
1496 {
1497 	struct inode *inode;
1498 
1499 	if (ino == 0)
1500 		return ERR_PTR(-ESTALE);
1501 	inode = jfs_iget(sb, ino);
1502 	if (IS_ERR(inode))
1503 		return ERR_CAST(inode);
1504 
1505 	if (generation && inode->i_generation != generation) {
1506 		iput(inode);
1507 		return ERR_PTR(-ESTALE);
1508 	}
1509 
1510 	return inode;
1511 }
1512 
1513 struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1514 		int fh_len, int fh_type)
1515 {
1516 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1517 				    jfs_nfs_get_inode);
1518 }
1519 
1520 struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1521 		int fh_len, int fh_type)
1522 {
1523 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1524 				    jfs_nfs_get_inode);
1525 }
1526 
1527 struct dentry *jfs_get_parent(struct dentry *dentry)
1528 {
1529 	unsigned long parent_ino;
1530 
1531 	parent_ino =
1532 		le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1533 
1534 	return d_obtain_alias(jfs_iget(dentry->d_inode->i_sb, parent_ino));
1535 }
1536 
1537 const struct inode_operations jfs_dir_inode_operations = {
1538 	.create		= jfs_create,
1539 	.lookup		= jfs_lookup,
1540 	.link		= jfs_link,
1541 	.unlink		= jfs_unlink,
1542 	.symlink	= jfs_symlink,
1543 	.mkdir		= jfs_mkdir,
1544 	.rmdir		= jfs_rmdir,
1545 	.mknod		= jfs_mknod,
1546 	.rename		= jfs_rename,
1547 	.setxattr	= jfs_setxattr,
1548 	.getxattr	= jfs_getxattr,
1549 	.listxattr	= jfs_listxattr,
1550 	.removexattr	= jfs_removexattr,
1551 	.setattr	= jfs_setattr,
1552 #ifdef CONFIG_JFS_POSIX_ACL
1553 	.check_acl	= jfs_check_acl,
1554 #endif
1555 };
1556 
1557 const struct file_operations jfs_dir_operations = {
1558 	.read		= generic_read_dir,
1559 	.readdir	= jfs_readdir,
1560 	.fsync		= jfs_fsync,
1561 	.unlocked_ioctl = jfs_ioctl,
1562 #ifdef CONFIG_COMPAT
1563 	.compat_ioctl	= jfs_compat_ioctl,
1564 #endif
1565 	.llseek		= generic_file_llseek,
1566 };
1567 
1568 static int jfs_ci_hash(const struct dentry *dir, const struct inode *inode,
1569 		struct qstr *this)
1570 {
1571 	unsigned long hash;
1572 	int i;
1573 
1574 	hash = init_name_hash();
1575 	for (i=0; i < this->len; i++)
1576 		hash = partial_name_hash(tolower(this->name[i]), hash);
1577 	this->hash = end_name_hash(hash);
1578 
1579 	return 0;
1580 }
1581 
1582 static int jfs_ci_compare(const struct dentry *parent,
1583 		const struct inode *pinode,
1584 		const struct dentry *dentry, const struct inode *inode,
1585 		unsigned int len, const char *str, const struct qstr *name)
1586 {
1587 	int i, result = 1;
1588 
1589 	if (len != name->len)
1590 		goto out;
1591 	for (i=0; i < len; i++) {
1592 		if (tolower(str[i]) != tolower(name->name[i]))
1593 			goto out;
1594 	}
1595 	result = 0;
1596 out:
1597 	return result;
1598 }
1599 
1600 static int jfs_ci_revalidate(struct dentry *dentry, struct nameidata *nd)
1601 {
1602 	if (nd && nd->flags & LOOKUP_RCU)
1603 		return -ECHILD;
1604 	/*
1605 	 * This is not negative dentry. Always valid.
1606 	 *
1607 	 * Note, rename() to existing directory entry will have ->d_inode,
1608 	 * and will use existing name which isn't specified name by user.
1609 	 *
1610 	 * We may be able to drop this positive dentry here. But dropping
1611 	 * positive dentry isn't good idea. So it's unsupported like
1612 	 * rename("filename", "FILENAME") for now.
1613 	 */
1614 	if (dentry->d_inode)
1615 		return 1;
1616 
1617 	/*
1618 	 * This may be nfsd (or something), anyway, we can't see the
1619 	 * intent of this. So, since this can be for creation, drop it.
1620 	 */
1621 	if (!nd)
1622 		return 0;
1623 
1624 	/*
1625 	 * Drop the negative dentry, in order to make sure to use the
1626 	 * case sensitive name which is specified by user if this is
1627 	 * for creation.
1628 	 */
1629 	if (!(nd->flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))) {
1630 		if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
1631 			return 0;
1632 	}
1633 	return 1;
1634 }
1635 
1636 const struct dentry_operations jfs_ci_dentry_operations =
1637 {
1638 	.d_hash = jfs_ci_hash,
1639 	.d_compare = jfs_ci_compare,
1640 	.d_revalidate = jfs_ci_revalidate,
1641 };
1642