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