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