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  */
free_ea_wmap(struct inode * inode)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  */
jfs_create(struct inode * dip,struct dentry * dentry,umode_t mode,bool excl)75 static int jfs_create(struct inode *dip, struct dentry *dentry, umode_t mode,
76 		bool excl)
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:%pd", dip, dentry);
88 
89 	rc = dquot_initialize(dip);
90 	if (rc)
91 		goto out1;
92 
93 	/*
94 	 * search parent directory for entry/freespace
95 	 * (dtSearch() returns parent directory page pinned)
96 	 */
97 	if ((rc = get_UCSname(&dname, dentry)))
98 		goto out1;
99 
100 	/*
101 	 * Either iAlloc() or txBegin() may block.  Deadlock can occur if we
102 	 * block there while holding dtree page, so we allocate the inode &
103 	 * begin the transaction before we search the directory.
104 	 */
105 	ip = ialloc(dip, mode);
106 	if (IS_ERR(ip)) {
107 		rc = PTR_ERR(ip);
108 		goto out2;
109 	}
110 
111 	tid = txBegin(dip->i_sb, 0);
112 
113 	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
114 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
115 
116 	rc = jfs_init_acl(tid, ip, dip);
117 	if (rc)
118 		goto out3;
119 
120 	rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
121 	if (rc) {
122 		txAbort(tid, 0);
123 		goto out3;
124 	}
125 
126 	if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
127 		jfs_err("jfs_create: dtSearch returned %d", rc);
128 		txAbort(tid, 0);
129 		goto out3;
130 	}
131 
132 	tblk = tid_to_tblock(tid);
133 	tblk->xflag |= COMMIT_CREATE;
134 	tblk->ino = ip->i_ino;
135 	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
136 
137 	iplist[0] = dip;
138 	iplist[1] = ip;
139 
140 	/*
141 	 * initialize the child XAD tree root in-line in inode
142 	 */
143 	xtInitRoot(tid, ip);
144 
145 	/*
146 	 * create entry in parent directory for child directory
147 	 * (dtInsert() releases parent directory page)
148 	 */
149 	ino = ip->i_ino;
150 	if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
151 		if (rc == -EIO) {
152 			jfs_err("jfs_create: dtInsert returned -EIO");
153 			txAbort(tid, 1);	/* Marks Filesystem dirty */
154 		} else
155 			txAbort(tid, 0);	/* Filesystem full */
156 		goto out3;
157 	}
158 
159 	ip->i_op = &jfs_file_inode_operations;
160 	ip->i_fop = &jfs_file_operations;
161 	ip->i_mapping->a_ops = &jfs_aops;
162 
163 	mark_inode_dirty(ip);
164 
165 	dip->i_ctime = dip->i_mtime = current_time(dip);
166 
167 	mark_inode_dirty(dip);
168 
169 	rc = txCommit(tid, 2, &iplist[0], 0);
170 
171       out3:
172 	txEnd(tid);
173 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
174 	mutex_unlock(&JFS_IP(dip)->commit_mutex);
175 	if (rc) {
176 		free_ea_wmap(ip);
177 		clear_nlink(ip);
178 		discard_new_inode(ip);
179 	} else {
180 		d_instantiate_new(dentry, 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  */
jfs_mkdir(struct inode * dip,struct dentry * dentry,umode_t mode)208 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, umode_t 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:%pd", dip, dentry);
220 
221 	rc = dquot_initialize(dip);
222 	if (rc)
223 		goto out1;
224 
225 	/*
226 	 * search parent directory for entry/freespace
227 	 * (dtSearch() returns parent directory page pinned)
228 	 */
229 	if ((rc = get_UCSname(&dname, dentry)))
230 		goto out1;
231 
232 	/*
233 	 * Either iAlloc() or txBegin() may block.  Deadlock can occur if we
234 	 * block there while holding dtree page, so we allocate the inode &
235 	 * begin the transaction before we search the directory.
236 	 */
237 	ip = ialloc(dip, S_IFDIR | mode);
238 	if (IS_ERR(ip)) {
239 		rc = PTR_ERR(ip);
240 		goto out2;
241 	}
242 
243 	tid = txBegin(dip->i_sb, 0);
244 
245 	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
246 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
247 
248 	rc = jfs_init_acl(tid, ip, dip);
249 	if (rc)
250 		goto out3;
251 
252 	rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
253 	if (rc) {
254 		txAbort(tid, 0);
255 		goto out3;
256 	}
257 
258 	if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
259 		jfs_err("jfs_mkdir: dtSearch returned %d", rc);
260 		txAbort(tid, 0);
261 		goto out3;
262 	}
263 
264 	tblk = tid_to_tblock(tid);
265 	tblk->xflag |= COMMIT_CREATE;
266 	tblk->ino = ip->i_ino;
267 	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
268 
269 	iplist[0] = dip;
270 	iplist[1] = ip;
271 
272 	/*
273 	 * initialize the child directory in-line in inode
274 	 */
275 	dtInitRoot(tid, ip, dip->i_ino);
276 
277 	/*
278 	 * create entry in parent directory for child directory
279 	 * (dtInsert() releases parent directory page)
280 	 */
281 	ino = ip->i_ino;
282 	if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
283 		if (rc == -EIO) {
284 			jfs_err("jfs_mkdir: dtInsert returned -EIO");
285 			txAbort(tid, 1);	/* Marks Filesystem dirty */
286 		} else
287 			txAbort(tid, 0);	/* Filesystem full */
288 		goto out3;
289 	}
290 
291 	set_nlink(ip, 2);	/* for '.' */
292 	ip->i_op = &jfs_dir_inode_operations;
293 	ip->i_fop = &jfs_dir_operations;
294 
295 	mark_inode_dirty(ip);
296 
297 	/* update parent directory inode */
298 	inc_nlink(dip);		/* for '..' from child directory */
299 	dip->i_ctime = dip->i_mtime = current_time(dip);
300 	mark_inode_dirty(dip);
301 
302 	rc = txCommit(tid, 2, &iplist[0], 0);
303 
304       out3:
305 	txEnd(tid);
306 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
307 	mutex_unlock(&JFS_IP(dip)->commit_mutex);
308 	if (rc) {
309 		free_ea_wmap(ip);
310 		clear_nlink(ip);
311 		discard_new_inode(ip);
312 	} else {
313 		d_instantiate_new(dentry, ip);
314 	}
315 
316       out2:
317 	free_UCSname(&dname);
318 
319 
320       out1:
321 
322 	jfs_info("jfs_mkdir: rc:%d", rc);
323 	return rc;
324 }
325 
326 /*
327  * NAME:	jfs_rmdir(dip, dentry)
328  *
329  * FUNCTION:	remove a link to child directory
330  *
331  * PARAMETER:	dip	- parent inode
332  *		dentry	- child directory dentry
333  *
334  * RETURN:	-EINVAL	- if name is . or ..
335  *		-EINVAL - if . or .. exist but are invalid.
336  *		errors from subroutines
337  *
338  * note:
339  * if other threads have the directory open when the last link
340  * is removed, the "." and ".." entries, if present, are removed before
341  * rmdir() returns and no new entries may be created in the directory,
342  * but the directory is not removed until the last reference to
343  * the directory is released (cf.unlink() of regular file).
344  */
jfs_rmdir(struct inode * dip,struct dentry * dentry)345 static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
346 {
347 	int rc;
348 	tid_t tid;		/* transaction id */
349 	struct inode *ip = d_inode(dentry);
350 	ino_t ino;
351 	struct component_name dname;
352 	struct inode *iplist[2];
353 	struct tblock *tblk;
354 
355 	jfs_info("jfs_rmdir: dip:0x%p name:%pd", dip, dentry);
356 
357 	/* Init inode for quota operations. */
358 	rc = dquot_initialize(dip);
359 	if (rc)
360 		goto out;
361 	rc = dquot_initialize(ip);
362 	if (rc)
363 		goto out;
364 
365 	/* directory must be empty to be removed */
366 	if (!dtEmpty(ip)) {
367 		rc = -ENOTEMPTY;
368 		goto out;
369 	}
370 
371 	if ((rc = get_UCSname(&dname, dentry))) {
372 		goto out;
373 	}
374 
375 	tid = txBegin(dip->i_sb, 0);
376 
377 	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
378 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
379 
380 	iplist[0] = dip;
381 	iplist[1] = ip;
382 
383 	tblk = tid_to_tblock(tid);
384 	tblk->xflag |= COMMIT_DELETE;
385 	tblk->u.ip = ip;
386 
387 	/*
388 	 * delete the entry of target directory from parent directory
389 	 */
390 	ino = ip->i_ino;
391 	if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
392 		jfs_err("jfs_rmdir: dtDelete returned %d", rc);
393 		if (rc == -EIO)
394 			txAbort(tid, 1);
395 		txEnd(tid);
396 		mutex_unlock(&JFS_IP(ip)->commit_mutex);
397 		mutex_unlock(&JFS_IP(dip)->commit_mutex);
398 
399 		goto out2;
400 	}
401 
402 	/* update parent directory's link count corresponding
403 	 * to ".." entry of the target directory deleted
404 	 */
405 	dip->i_ctime = dip->i_mtime = current_time(dip);
406 	inode_dec_link_count(dip);
407 
408 	/*
409 	 * OS/2 could have created EA and/or ACL
410 	 */
411 	/* free EA from both persistent and working map */
412 	if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
413 		/* free EA pages */
414 		txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
415 	}
416 	JFS_IP(ip)->ea.flag = 0;
417 
418 	/* free ACL from both persistent and working map */
419 	if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
420 		/* free ACL pages */
421 		txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
422 	}
423 	JFS_IP(ip)->acl.flag = 0;
424 
425 	/* mark the target directory as deleted */
426 	clear_nlink(ip);
427 	mark_inode_dirty(ip);
428 
429 	rc = txCommit(tid, 2, &iplist[0], 0);
430 
431 	txEnd(tid);
432 
433 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
434 	mutex_unlock(&JFS_IP(dip)->commit_mutex);
435 
436 	/*
437 	 * Truncating the directory index table is not guaranteed.  It
438 	 * may need to be done iteratively
439 	 */
440 	if (test_cflag(COMMIT_Stale, dip)) {
441 		if (dip->i_size > 1)
442 			jfs_truncate_nolock(dip, 0);
443 
444 		clear_cflag(COMMIT_Stale, dip);
445 	}
446 
447       out2:
448 	free_UCSname(&dname);
449 
450       out:
451 	jfs_info("jfs_rmdir: rc:%d", rc);
452 	return rc;
453 }
454 
455 /*
456  * NAME:	jfs_unlink(dip, dentry)
457  *
458  * FUNCTION:	remove a link to object <vp> named by <name>
459  *		from parent directory <dvp>
460  *
461  * PARAMETER:	dip	- inode of parent directory
462  *		dentry	- dentry of object to be removed
463  *
464  * RETURN:	errors from subroutines
465  *
466  * note:
467  * temporary file: if one or more processes have the file open
468  * when the last link is removed, the link will be removed before
469  * unlink() returns, but the removal of the file contents will be
470  * postponed until all references to the files are closed.
471  *
472  * JFS does NOT support unlink() on directories.
473  *
474  */
jfs_unlink(struct inode * dip,struct dentry * dentry)475 static int jfs_unlink(struct inode *dip, struct dentry *dentry)
476 {
477 	int rc;
478 	tid_t tid;		/* transaction id */
479 	struct inode *ip = d_inode(dentry);
480 	ino_t ino;
481 	struct component_name dname;	/* object name */
482 	struct inode *iplist[2];
483 	struct tblock *tblk;
484 	s64 new_size = 0;
485 	int commit_flag;
486 
487 	jfs_info("jfs_unlink: dip:0x%p name:%pd", dip, dentry);
488 
489 	/* Init inode for quota operations. */
490 	rc = dquot_initialize(dip);
491 	if (rc)
492 		goto out;
493 	rc = dquot_initialize(ip);
494 	if (rc)
495 		goto out;
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(ip);
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  */
commitZeroLink(tid_t tid,struct inode * ip)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  */
jfs_free_zero_link(struct inode * ip)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  */
jfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)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 = d_inode(old_dentry);
803 	ino_t ino;
804 	struct component_name dname;
805 	struct btstack btstack;
806 	struct inode *iplist[2];
807 
808 	jfs_info("jfs_link: %pd %pd", old_dentry, dentry);
809 
810 	rc = dquot_initialize(dir);
811 	if (rc)
812 		goto out;
813 
814 	if (isReadOnly(ip)) {
815 		jfs_error(ip->i_sb, "read-only filesystem\n");
816 		return -EROFS;
817 	}
818 
819 	tid = txBegin(ip->i_sb, 0);
820 
821 	mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
822 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
823 
824 	/*
825 	 * scan parent directory for entry/freespace
826 	 */
827 	if ((rc = get_UCSname(&dname, dentry)))
828 		goto out_tx;
829 
830 	if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
831 		goto free_dname;
832 
833 	/*
834 	 * create entry for new link in parent directory
835 	 */
836 	ino = ip->i_ino;
837 	if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
838 		goto free_dname;
839 
840 	/* update object inode */
841 	inc_nlink(ip);		/* for new link */
842 	ip->i_ctime = current_time(ip);
843 	dir->i_ctime = dir->i_mtime = current_time(dir);
844 	mark_inode_dirty(dir);
845 	ihold(ip);
846 
847 	iplist[0] = ip;
848 	iplist[1] = dir;
849 	rc = txCommit(tid, 2, &iplist[0], 0);
850 
851 	if (rc) {
852 		drop_nlink(ip); /* never instantiated */
853 		iput(ip);
854 	} else
855 		d_instantiate(dentry, ip);
856 
857       free_dname:
858 	free_UCSname(&dname);
859 
860       out_tx:
861 	txEnd(tid);
862 
863 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
864 	mutex_unlock(&JFS_IP(dir)->commit_mutex);
865 
866       out:
867 	jfs_info("jfs_link: rc:%d", rc);
868 	return rc;
869 }
870 
871 /*
872  * NAME:	jfs_symlink(dip, dentry, name)
873  *
874  * FUNCTION:	creates a symbolic link to <symlink> by name <name>
875  *			in directory <dip>
876  *
877  * PARAMETER:	dip	- parent directory vnode
878  *		dentry	- dentry of symbolic link
879  *		name	- the path name of the existing object
880  *			  that will be the source of the link
881  *
882  * RETURN:	errors from subroutines
883  *
884  * note:
885  * ENAMETOOLONG: pathname resolution of a symbolic link produced
886  * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
887 */
888 
jfs_symlink(struct inode * dip,struct dentry * dentry,const char * name)889 static int jfs_symlink(struct inode *dip, struct dentry *dentry,
890 		const char *name)
891 {
892 	int rc;
893 	tid_t tid;
894 	ino_t ino = 0;
895 	struct component_name dname;
896 	int ssize;		/* source pathname size */
897 	struct btstack btstack;
898 	struct inode *ip = d_inode(dentry);
899 	s64 xlen = 0;
900 	int bmask = 0, xsize;
901 	s64 xaddr;
902 	struct metapage *mp;
903 	struct super_block *sb;
904 	struct tblock *tblk;
905 
906 	struct inode *iplist[2];
907 
908 	jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
909 
910 	rc = dquot_initialize(dip);
911 	if (rc)
912 		goto out1;
913 
914 	ssize = strlen(name) + 1;
915 
916 	/*
917 	 * search parent directory for entry/freespace
918 	 * (dtSearch() returns parent directory page pinned)
919 	 */
920 
921 	if ((rc = get_UCSname(&dname, dentry)))
922 		goto out1;
923 
924 	/*
925 	 * allocate on-disk/in-memory inode for symbolic link:
926 	 * (iAlloc() returns new, locked inode)
927 	 */
928 	ip = ialloc(dip, S_IFLNK | 0777);
929 	if (IS_ERR(ip)) {
930 		rc = PTR_ERR(ip);
931 		goto out2;
932 	}
933 
934 	tid = txBegin(dip->i_sb, 0);
935 
936 	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
937 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
938 
939 	rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
940 	if (rc)
941 		goto out3;
942 
943 	tblk = tid_to_tblock(tid);
944 	tblk->xflag |= COMMIT_CREATE;
945 	tblk->ino = ip->i_ino;
946 	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
947 
948 	/* fix symlink access permission
949 	 * (dir_create() ANDs in the u.u_cmask,
950 	 * but symlinks really need to be 777 access)
951 	 */
952 	ip->i_mode |= 0777;
953 
954 	/*
955 	 * write symbolic link target path name
956 	 */
957 	xtInitRoot(tid, ip);
958 
959 	/*
960 	 * write source path name inline in on-disk inode (fast symbolic link)
961 	 */
962 
963 	if (ssize <= IDATASIZE) {
964 		ip->i_op = &jfs_fast_symlink_inode_operations;
965 
966 		ip->i_link = JFS_IP(ip)->i_inline;
967 		memcpy(ip->i_link, name, ssize);
968 		ip->i_size = ssize - 1;
969 
970 		/*
971 		 * if symlink is > 128 bytes, we don't have the space to
972 		 * store inline extended attributes
973 		 */
974 		if (ssize > sizeof (JFS_IP(ip)->i_inline))
975 			JFS_IP(ip)->mode2 &= ~INLINEEA;
976 
977 		jfs_info("jfs_symlink: fast symlink added  ssize:%d name:%s ",
978 			 ssize, name);
979 	}
980 	/*
981 	 * write source path name in a single extent
982 	 */
983 	else {
984 		jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
985 
986 		ip->i_op = &jfs_symlink_inode_operations;
987 		inode_nohighmem(ip);
988 		ip->i_mapping->a_ops = &jfs_aops;
989 
990 		/*
991 		 * even though the data of symlink object (source
992 		 * path name) is treated as non-journaled user data,
993 		 * it is read/written thru buffer cache for performance.
994 		 */
995 		sb = ip->i_sb;
996 		bmask = JFS_SBI(sb)->bsize - 1;
997 		xsize = (ssize + bmask) & ~bmask;
998 		xaddr = 0;
999 		xlen = xsize >> JFS_SBI(sb)->l2bsize;
1000 		if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
1001 			txAbort(tid, 0);
1002 			goto out3;
1003 		}
1004 		ip->i_size = ssize - 1;
1005 		while (ssize) {
1006 			/* This is kind of silly since PATH_MAX == 4K */
1007 			int copy_size = min(ssize, PSIZE);
1008 
1009 			mp = get_metapage(ip, xaddr, PSIZE, 1);
1010 
1011 			if (mp == NULL) {
1012 				xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1013 				rc = -EIO;
1014 				txAbort(tid, 0);
1015 				goto out3;
1016 			}
1017 			memcpy(mp->data, name, copy_size);
1018 			flush_metapage(mp);
1019 			ssize -= copy_size;
1020 			name += copy_size;
1021 			xaddr += JFS_SBI(sb)->nbperpage;
1022 		}
1023 	}
1024 
1025 	/*
1026 	 * create entry for symbolic link in parent directory
1027 	 */
1028 	rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1029 	if (rc == 0) {
1030 		ino = ip->i_ino;
1031 		rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1032 	}
1033 	if (rc) {
1034 		if (xlen)
1035 			xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1036 		txAbort(tid, 0);
1037 		/* discard new inode */
1038 		goto out3;
1039 	}
1040 
1041 	mark_inode_dirty(ip);
1042 
1043 	dip->i_ctime = dip->i_mtime = current_time(dip);
1044 	mark_inode_dirty(dip);
1045 	/*
1046 	 * commit update of parent directory and link object
1047 	 */
1048 
1049 	iplist[0] = dip;
1050 	iplist[1] = ip;
1051 	rc = txCommit(tid, 2, &iplist[0], 0);
1052 
1053       out3:
1054 	txEnd(tid);
1055 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
1056 	mutex_unlock(&JFS_IP(dip)->commit_mutex);
1057 	if (rc) {
1058 		free_ea_wmap(ip);
1059 		clear_nlink(ip);
1060 		discard_new_inode(ip);
1061 	} else {
1062 		d_instantiate_new(dentry, ip);
1063 	}
1064 
1065       out2:
1066 	free_UCSname(&dname);
1067 
1068       out1:
1069 	jfs_info("jfs_symlink: rc:%d", rc);
1070 	return rc;
1071 }
1072 
1073 
1074 /*
1075  * NAME:	jfs_rename
1076  *
1077  * FUNCTION:	rename a file or directory
1078  */
jfs_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)1079 static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1080 		      struct inode *new_dir, struct dentry *new_dentry,
1081 		      unsigned int flags)
1082 {
1083 	struct btstack btstack;
1084 	ino_t ino;
1085 	struct component_name new_dname;
1086 	struct inode *new_ip;
1087 	struct component_name old_dname;
1088 	struct inode *old_ip;
1089 	int rc;
1090 	tid_t tid;
1091 	struct tlock *tlck;
1092 	struct dt_lock *dtlck;
1093 	struct lv *lv;
1094 	int ipcount;
1095 	struct inode *iplist[4];
1096 	struct tblock *tblk;
1097 	s64 new_size = 0;
1098 	int commit_flag;
1099 
1100 	if (flags & ~RENAME_NOREPLACE)
1101 		return -EINVAL;
1102 
1103 	jfs_info("jfs_rename: %pd %pd", old_dentry, new_dentry);
1104 
1105 	rc = dquot_initialize(old_dir);
1106 	if (rc)
1107 		goto out1;
1108 	rc = dquot_initialize(new_dir);
1109 	if (rc)
1110 		goto out1;
1111 
1112 	old_ip = d_inode(old_dentry);
1113 	new_ip = d_inode(new_dentry);
1114 
1115 	if ((rc = get_UCSname(&old_dname, old_dentry)))
1116 		goto out1;
1117 
1118 	if ((rc = get_UCSname(&new_dname, new_dentry)))
1119 		goto out2;
1120 
1121 	/*
1122 	 * Make sure source inode number is what we think it is
1123 	 */
1124 	rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1125 	if (rc || (ino != old_ip->i_ino)) {
1126 		rc = -ENOENT;
1127 		goto out3;
1128 	}
1129 
1130 	/*
1131 	 * Make sure dest inode number (if any) is what we think it is
1132 	 */
1133 	rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1134 	if (!rc) {
1135 		if ((!new_ip) || (ino != new_ip->i_ino)) {
1136 			rc = -ESTALE;
1137 			goto out3;
1138 		}
1139 	} else if (rc != -ENOENT)
1140 		goto out3;
1141 	else if (new_ip) {
1142 		/* no entry exists, but one was expected */
1143 		rc = -ESTALE;
1144 		goto out3;
1145 	}
1146 
1147 	if (S_ISDIR(old_ip->i_mode)) {
1148 		if (new_ip) {
1149 			if (!dtEmpty(new_ip)) {
1150 				rc = -ENOTEMPTY;
1151 				goto out3;
1152 			}
1153 		}
1154 	} else if (new_ip) {
1155 		IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL);
1156 		/* Init inode for quota operations. */
1157 		rc = dquot_initialize(new_ip);
1158 		if (rc)
1159 			goto out_unlock;
1160 	}
1161 
1162 	/*
1163 	 * The real work starts here
1164 	 */
1165 	tid = txBegin(new_dir->i_sb, 0);
1166 
1167 	/*
1168 	 * How do we know the locking is safe from deadlocks?
1169 	 * The vfs does the hard part for us.  Any time we are taking nested
1170 	 * commit_mutexes, the vfs already has i_mutex held on the parent.
1171 	 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1172 	 */
1173 	mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1174 	mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD);
1175 	if (old_dir != new_dir)
1176 		mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex,
1177 				  COMMIT_MUTEX_SECOND_PARENT);
1178 
1179 	if (new_ip) {
1180 		mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex,
1181 				  COMMIT_MUTEX_VICTIM);
1182 		/*
1183 		 * Change existing directory entry to new inode number
1184 		 */
1185 		ino = new_ip->i_ino;
1186 		rc = dtModify(tid, new_dir, &new_dname, &ino,
1187 			      old_ip->i_ino, JFS_RENAME);
1188 		if (rc)
1189 			goto out_tx;
1190 		drop_nlink(new_ip);
1191 		if (S_ISDIR(new_ip->i_mode)) {
1192 			drop_nlink(new_ip);
1193 			if (new_ip->i_nlink) {
1194 				mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1195 				if (old_dir != new_dir)
1196 					mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1197 				mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1198 				mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
1199 				if (!S_ISDIR(old_ip->i_mode) && new_ip)
1200 					IWRITE_UNLOCK(new_ip);
1201 				jfs_error(new_ip->i_sb,
1202 					  "new_ip->i_nlink != 0\n");
1203 				return -EIO;
1204 			}
1205 			tblk = tid_to_tblock(tid);
1206 			tblk->xflag |= COMMIT_DELETE;
1207 			tblk->u.ip = new_ip;
1208 		} else if (new_ip->i_nlink == 0) {
1209 			assert(!test_cflag(COMMIT_Nolink, new_ip));
1210 			/* free block resources */
1211 			if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1212 				txAbort(tid, 1);	/* Marks FS Dirty */
1213 				rc = new_size;
1214 				goto out_tx;
1215 			}
1216 			tblk = tid_to_tblock(tid);
1217 			tblk->xflag |= COMMIT_DELETE;
1218 			tblk->u.ip = new_ip;
1219 		} else {
1220 			new_ip->i_ctime = current_time(new_ip);
1221 			mark_inode_dirty(new_ip);
1222 		}
1223 	} else {
1224 		/*
1225 		 * Add new directory entry
1226 		 */
1227 		rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1228 			      JFS_CREATE);
1229 		if (rc) {
1230 			jfs_err("jfs_rename didn't expect dtSearch to fail w/rc = %d",
1231 				rc);
1232 			goto out_tx;
1233 		}
1234 
1235 		ino = old_ip->i_ino;
1236 		rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1237 		if (rc) {
1238 			if (rc == -EIO)
1239 				jfs_err("jfs_rename: dtInsert returned -EIO");
1240 			goto out_tx;
1241 		}
1242 		if (S_ISDIR(old_ip->i_mode))
1243 			inc_nlink(new_dir);
1244 	}
1245 	/*
1246 	 * Remove old directory entry
1247 	 */
1248 
1249 	ino = old_ip->i_ino;
1250 	rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1251 	if (rc) {
1252 		jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1253 			rc);
1254 		txAbort(tid, 1);	/* Marks Filesystem dirty */
1255 		goto out_tx;
1256 	}
1257 	if (S_ISDIR(old_ip->i_mode)) {
1258 		drop_nlink(old_dir);
1259 		if (old_dir != new_dir) {
1260 			/*
1261 			 * Change inode number of parent for moved directory
1262 			 */
1263 
1264 			JFS_IP(old_ip)->i_dtroot.header.idotdot =
1265 				cpu_to_le32(new_dir->i_ino);
1266 
1267 			/* Linelock header of dtree */
1268 			tlck = txLock(tid, old_ip,
1269 				    (struct metapage *) &JFS_IP(old_ip)->bxflag,
1270 				      tlckDTREE | tlckBTROOT | tlckRELINK);
1271 			dtlck = (struct dt_lock *) & tlck->lock;
1272 			ASSERT(dtlck->index == 0);
1273 			lv = & dtlck->lv[0];
1274 			lv->offset = 0;
1275 			lv->length = 1;
1276 			dtlck->index++;
1277 		}
1278 	}
1279 
1280 	/*
1281 	 * Update ctime on changed/moved inodes & mark dirty
1282 	 */
1283 	old_ip->i_ctime = current_time(old_ip);
1284 	mark_inode_dirty(old_ip);
1285 
1286 	new_dir->i_ctime = new_dir->i_mtime = current_time(new_dir);
1287 	mark_inode_dirty(new_dir);
1288 
1289 	/* Build list of inodes modified by this transaction */
1290 	ipcount = 0;
1291 	iplist[ipcount++] = old_ip;
1292 	if (new_ip)
1293 		iplist[ipcount++] = new_ip;
1294 	iplist[ipcount++] = old_dir;
1295 
1296 	if (old_dir != new_dir) {
1297 		iplist[ipcount++] = new_dir;
1298 		old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
1299 		mark_inode_dirty(old_dir);
1300 	}
1301 
1302 	/*
1303 	 * Incomplete truncate of file data can
1304 	 * result in timing problems unless we synchronously commit the
1305 	 * transaction.
1306 	 */
1307 	if (new_size)
1308 		commit_flag = COMMIT_SYNC;
1309 	else
1310 		commit_flag = 0;
1311 
1312 	rc = txCommit(tid, ipcount, iplist, commit_flag);
1313 
1314       out_tx:
1315 	txEnd(tid);
1316 	if (new_ip)
1317 		mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1318 	if (old_dir != new_dir)
1319 		mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1320 	mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1321 	mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
1322 
1323 	while (new_size && (rc == 0)) {
1324 		tid = txBegin(new_ip->i_sb, 0);
1325 		mutex_lock(&JFS_IP(new_ip)->commit_mutex);
1326 		new_size = xtTruncate_pmap(tid, new_ip, new_size);
1327 		if (new_size < 0) {
1328 			txAbort(tid, 1);
1329 			rc = new_size;
1330 		} else
1331 			rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1332 		txEnd(tid);
1333 		mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1334 	}
1335 	if (new_ip && (new_ip->i_nlink == 0))
1336 		set_cflag(COMMIT_Nolink, 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       out_unlock:
1348 	if (new_ip && !S_ISDIR(new_ip->i_mode))
1349 		IWRITE_UNLOCK(new_ip);
1350       out3:
1351 	free_UCSname(&new_dname);
1352       out2:
1353 	free_UCSname(&old_dname);
1354       out1:
1355 	jfs_info("jfs_rename: returning %d", rc);
1356 	return rc;
1357 }
1358 
1359 
1360 /*
1361  * NAME:	jfs_mknod
1362  *
1363  * FUNCTION:	Create a special file (device)
1364  */
jfs_mknod(struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)1365 static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1366 		umode_t mode, dev_t rdev)
1367 {
1368 	struct jfs_inode_info *jfs_ip;
1369 	struct btstack btstack;
1370 	struct component_name dname;
1371 	ino_t ino;
1372 	struct inode *ip;
1373 	struct inode *iplist[2];
1374 	int rc;
1375 	tid_t tid;
1376 	struct tblock *tblk;
1377 
1378 	jfs_info("jfs_mknod: %pd", dentry);
1379 
1380 	rc = dquot_initialize(dir);
1381 	if (rc)
1382 		goto out;
1383 
1384 	if ((rc = get_UCSname(&dname, dentry)))
1385 		goto out;
1386 
1387 	ip = ialloc(dir, mode);
1388 	if (IS_ERR(ip)) {
1389 		rc = PTR_ERR(ip);
1390 		goto out1;
1391 	}
1392 	jfs_ip = JFS_IP(ip);
1393 
1394 	tid = txBegin(dir->i_sb, 0);
1395 
1396 	mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1397 	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
1398 
1399 	rc = jfs_init_acl(tid, ip, dir);
1400 	if (rc)
1401 		goto out3;
1402 
1403 	rc = jfs_init_security(tid, ip, dir, &dentry->d_name);
1404 	if (rc) {
1405 		txAbort(tid, 0);
1406 		goto out3;
1407 	}
1408 
1409 	if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1410 		txAbort(tid, 0);
1411 		goto out3;
1412 	}
1413 
1414 	tblk = tid_to_tblock(tid);
1415 	tblk->xflag |= COMMIT_CREATE;
1416 	tblk->ino = ip->i_ino;
1417 	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1418 
1419 	ino = ip->i_ino;
1420 	if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1421 		txAbort(tid, 0);
1422 		goto out3;
1423 	}
1424 
1425 	ip->i_op = &jfs_file_inode_operations;
1426 	jfs_ip->dev = new_encode_dev(rdev);
1427 	init_special_inode(ip, ip->i_mode, rdev);
1428 
1429 	mark_inode_dirty(ip);
1430 
1431 	dir->i_ctime = dir->i_mtime = current_time(dir);
1432 
1433 	mark_inode_dirty(dir);
1434 
1435 	iplist[0] = dir;
1436 	iplist[1] = ip;
1437 	rc = txCommit(tid, 2, iplist, 0);
1438 
1439       out3:
1440 	txEnd(tid);
1441 	mutex_unlock(&JFS_IP(ip)->commit_mutex);
1442 	mutex_unlock(&JFS_IP(dir)->commit_mutex);
1443 	if (rc) {
1444 		free_ea_wmap(ip);
1445 		clear_nlink(ip);
1446 		discard_new_inode(ip);
1447 	} else {
1448 		d_instantiate_new(dentry, ip);
1449 	}
1450 
1451       out1:
1452 	free_UCSname(&dname);
1453 
1454       out:
1455 	jfs_info("jfs_mknod: returning %d", rc);
1456 	return rc;
1457 }
1458 
jfs_lookup(struct inode * dip,struct dentry * dentry,unsigned int flags)1459 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
1460 {
1461 	struct btstack btstack;
1462 	ino_t inum;
1463 	struct inode *ip;
1464 	struct component_name key;
1465 	int rc;
1466 
1467 	jfs_info("jfs_lookup: name = %pd", dentry);
1468 
1469 	if ((rc = get_UCSname(&key, dentry)))
1470 		return ERR_PTR(rc);
1471 	rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1472 	free_UCSname(&key);
1473 	if (rc == -ENOENT) {
1474 		ip = NULL;
1475 	} else if (rc) {
1476 		jfs_err("jfs_lookup: dtSearch returned %d", rc);
1477 		ip = ERR_PTR(rc);
1478 	} else {
1479 		ip = jfs_iget(dip->i_sb, inum);
1480 		if (IS_ERR(ip))
1481 			jfs_err("jfs_lookup: iget failed on inum %d", (uint)inum);
1482 	}
1483 
1484 	return d_splice_alias(ip, dentry);
1485 }
1486 
jfs_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)1487 static struct inode *jfs_nfs_get_inode(struct super_block *sb,
1488 		u64 ino, u32 generation)
1489 {
1490 	struct inode *inode;
1491 
1492 	if (ino == 0)
1493 		return ERR_PTR(-ESTALE);
1494 	inode = jfs_iget(sb, ino);
1495 	if (IS_ERR(inode))
1496 		return ERR_CAST(inode);
1497 
1498 	if (generation && inode->i_generation != generation) {
1499 		iput(inode);
1500 		return ERR_PTR(-ESTALE);
1501 	}
1502 
1503 	return inode;
1504 }
1505 
jfs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1506 struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1507 		int fh_len, int fh_type)
1508 {
1509 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1510 				    jfs_nfs_get_inode);
1511 }
1512 
jfs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1513 struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1514 		int fh_len, int fh_type)
1515 {
1516 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1517 				    jfs_nfs_get_inode);
1518 }
1519 
jfs_get_parent(struct dentry * dentry)1520 struct dentry *jfs_get_parent(struct dentry *dentry)
1521 {
1522 	unsigned long parent_ino;
1523 
1524 	parent_ino =
1525 		le32_to_cpu(JFS_IP(d_inode(dentry))->i_dtroot.header.idotdot);
1526 
1527 	return d_obtain_alias(jfs_iget(dentry->d_sb, parent_ino));
1528 }
1529 
1530 const struct inode_operations jfs_dir_inode_operations = {
1531 	.create		= jfs_create,
1532 	.lookup		= jfs_lookup,
1533 	.link		= jfs_link,
1534 	.unlink		= jfs_unlink,
1535 	.symlink	= jfs_symlink,
1536 	.mkdir		= jfs_mkdir,
1537 	.rmdir		= jfs_rmdir,
1538 	.mknod		= jfs_mknod,
1539 	.rename		= jfs_rename,
1540 	.listxattr	= jfs_listxattr,
1541 	.setattr	= jfs_setattr,
1542 #ifdef CONFIG_JFS_POSIX_ACL
1543 	.get_acl	= jfs_get_acl,
1544 	.set_acl	= jfs_set_acl,
1545 #endif
1546 };
1547 
1548 const struct file_operations jfs_dir_operations = {
1549 	.read		= generic_read_dir,
1550 	.iterate	= jfs_readdir,
1551 	.fsync		= jfs_fsync,
1552 	.unlocked_ioctl = jfs_ioctl,
1553 #ifdef CONFIG_COMPAT
1554 	.compat_ioctl	= jfs_compat_ioctl,
1555 #endif
1556 	.llseek		= generic_file_llseek,
1557 };
1558 
jfs_ci_hash(const struct dentry * dir,struct qstr * this)1559 static int jfs_ci_hash(const struct dentry *dir, struct qstr *this)
1560 {
1561 	unsigned long hash;
1562 	int i;
1563 
1564 	hash = init_name_hash(dir);
1565 	for (i=0; i < this->len; i++)
1566 		hash = partial_name_hash(tolower(this->name[i]), hash);
1567 	this->hash = end_name_hash(hash);
1568 
1569 	return 0;
1570 }
1571 
jfs_ci_compare(const struct dentry * dentry,unsigned int len,const char * str,const struct qstr * name)1572 static int jfs_ci_compare(const struct dentry *dentry,
1573 		unsigned int len, const char *str, const struct qstr *name)
1574 {
1575 	int i, result = 1;
1576 
1577 	if (len != name->len)
1578 		goto out;
1579 	for (i=0; i < len; i++) {
1580 		if (tolower(str[i]) != tolower(name->name[i]))
1581 			goto out;
1582 	}
1583 	result = 0;
1584 out:
1585 	return result;
1586 }
1587 
jfs_ci_revalidate(struct dentry * dentry,unsigned int flags)1588 static int jfs_ci_revalidate(struct dentry *dentry, unsigned int flags)
1589 {
1590 	/*
1591 	 * This is not negative dentry. Always valid.
1592 	 *
1593 	 * Note, rename() to existing directory entry will have ->d_inode,
1594 	 * and will use existing name which isn't specified name by user.
1595 	 *
1596 	 * We may be able to drop this positive dentry here. But dropping
1597 	 * positive dentry isn't good idea. So it's unsupported like
1598 	 * rename("filename", "FILENAME") for now.
1599 	 */
1600 	if (d_really_is_positive(dentry))
1601 		return 1;
1602 
1603 	/*
1604 	 * This may be nfsd (or something), anyway, we can't see the
1605 	 * intent of this. So, since this can be for creation, drop it.
1606 	 */
1607 	if (!flags)
1608 		return 0;
1609 
1610 	/*
1611 	 * Drop the negative dentry, in order to make sure to use the
1612 	 * case sensitive name which is specified by user if this is
1613 	 * for creation.
1614 	 */
1615 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
1616 		return 0;
1617 	return 1;
1618 }
1619 
1620 const struct dentry_operations jfs_ci_dentry_operations =
1621 {
1622 	.d_hash = jfs_ci_hash,
1623 	.d_compare = jfs_ci_compare,
1624 	.d_revalidate = jfs_ci_revalidate,
1625 };
1626