namei.c 37.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 *   Copyright (C) International Business Machines Corp., 2000-2004
 *   Portions Copyright (C) Christoph Hellwig, 2001-2002
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
D
Dave Kleikamp 已提交
7
 *   the Free Software Foundation; either version 2 of the License, or
L
Linus Torvalds 已提交
8
 *   (at your option) any later version.
D
Dave Kleikamp 已提交
9
 *
L
Linus Torvalds 已提交
10 11 12 13 14 15
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
D
Dave Kleikamp 已提交
16
 *   along with this program;  if not, write to the Free Software
L
Linus Torvalds 已提交
17 18 19 20
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/fs.h>
21
#include <linux/namei.h>
L
Linus Torvalds 已提交
22 23
#include <linux/ctype.h>
#include <linux/quotaops.h>
C
Christoph Hellwig 已提交
24
#include <linux/exportfs.h>
L
Linus Torvalds 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include "jfs_incore.h"
#include "jfs_superblock.h"
#include "jfs_inode.h"
#include "jfs_dinode.h"
#include "jfs_dmap.h"
#include "jfs_unicode.h"
#include "jfs_metapage.h"
#include "jfs_xattr.h"
#include "jfs_acl.h"
#include "jfs_debug.h"

/*
 * forward references
 */
A
Al Viro 已提交
39
const struct dentry_operations jfs_ci_dentry_operations;
L
Linus Torvalds 已提交
40 41 42

static s64 commitZeroLink(tid_t, struct inode *);

43 44 45
/*
 * NAME:	free_ea_wmap(inode)
 *
D
Dave Kleikamp 已提交
46
 * FUNCTION:	free uncommitted extended attributes from working map
47 48 49 50 51 52 53 54 55 56 57 58 59 60
 *
 */
static inline void free_ea_wmap(struct inode *inode)
{
	dxd_t *ea = &JFS_IP(inode)->ea;

	if (ea->flag & DXD_EXTENT) {
		/* free EA pages from cache */
		invalidate_dxd_metapages(inode, *ea);
		dbFree(inode, addressDXD(ea), lengthDXD(ea));
	}
	ea->flag = 0;
}

L
Linus Torvalds 已提交
61 62 63 64 65 66
/*
 * NAME:	jfs_create(dip, dentry, mode)
 *
 * FUNCTION:	create a regular file in the parent directory <dip>
 *		with name = <from dentry> and mode = <mode>
 *
D
Dave Kleikamp 已提交
67
 * PARAMETER:	dip	- parent directory vnode
L
Linus Torvalds 已提交
68 69 70 71 72 73 74
 *		dentry	- dentry of new file
 *		mode	- create mode (rwxrwxrwx).
 *		nd- nd struct
 *
 * RETURN:	Errors from subroutines
 *
 */
A
Al Viro 已提交
75
static int jfs_create(struct inode *dip, struct dentry *dentry, umode_t mode,
A
Al Viro 已提交
76
		bool excl)
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85 86
{
	int rc = 0;
	tid_t tid;		/* transaction id */
	struct inode *ip = NULL;	/* child directory inode */
	ino_t ino;
	struct component_name dname;	/* child directory name */
	struct btstack btstack;
	struct inode *iplist[2];
	struct tblock *tblk;

A
Al Viro 已提交
87
	jfs_info("jfs_create: dip:0x%p name:%pd", dip, dentry);
L
Linus Torvalds 已提交
88

89 90 91
	rc = dquot_initialize(dip);
	if (rc)
		goto out1;
92

L
Linus Torvalds 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105
	/*
	 * search parent directory for entry/freespace
	 * (dtSearch() returns parent directory page pinned)
	 */
	if ((rc = get_UCSname(&dname, dentry)))
		goto out1;

	/*
	 * Either iAlloc() or txBegin() may block.  Deadlock can occur if we
	 * block there while holding dtree page, so we allocate the inode &
	 * begin the transaction before we search the directory.
	 */
	ip = ialloc(dip, mode);
106 107
	if (IS_ERR(ip)) {
		rc = PTR_ERR(ip);
L
Linus Torvalds 已提交
108 109 110 111 112
		goto out2;
	}

	tid = txBegin(dip->i_sb, 0);

D
Dave Kleikamp 已提交
113 114
	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
L
Linus Torvalds 已提交
115

116 117 118 119
	rc = jfs_init_acl(tid, ip, dip);
	if (rc)
		goto out3;

120
	rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
D
Dave Kleikamp 已提交
121 122 123 124 125
	if (rc) {
		txAbort(tid, 0);
		goto out3;
	}

L
Linus Torvalds 已提交
126 127
	if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
		jfs_err("jfs_create: dtSearch returned %d", rc);
128
		txAbort(tid, 0);
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
		goto out3;
	}

	tblk = tid_to_tblock(tid);
	tblk->xflag |= COMMIT_CREATE;
	tblk->ino = ip->i_ino;
	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;

	iplist[0] = dip;
	iplist[1] = ip;

	/*
	 * initialize the child XAD tree root in-line in inode
	 */
	xtInitRoot(tid, ip);

	/*
	 * create entry in parent directory for child directory
	 * (dtInsert() releases parent directory page)
	 */
	ino = ip->i_ino;
	if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
		if (rc == -EIO) {
			jfs_err("jfs_create: dtInsert returned -EIO");
			txAbort(tid, 1);	/* Marks Filesystem dirty */
		} else
			txAbort(tid, 0);	/* Filesystem full */
		goto out3;
	}

	ip->i_op = &jfs_file_inode_operations;
	ip->i_fop = &jfs_file_operations;
	ip->i_mapping->a_ops = &jfs_aops;

	mark_inode_dirty(ip);

	dip->i_ctime = dip->i_mtime = CURRENT_TIME;

	mark_inode_dirty(dip);

	rc = txCommit(tid, 2, &iplist[0], 0);

      out3:
	txEnd(tid);
173
	mutex_unlock(&JFS_IP(ip)->commit_mutex);
E
Evgeniy Dushistov 已提交
174
	mutex_unlock(&JFS_IP(dip)->commit_mutex);
L
Linus Torvalds 已提交
175
	if (rc) {
176
		free_ea_wmap(ip);
177
		clear_nlink(ip);
D
Dave Kleikamp 已提交
178
		unlock_new_inode(ip);
L
Linus Torvalds 已提交
179
		iput(ip);
D
Dave Kleikamp 已提交
180 181
	} else {
		unlock_new_inode(ip);
182
		d_instantiate(dentry, ip);
D
Dave Kleikamp 已提交
183
	}
L
Linus Torvalds 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

      out2:
	free_UCSname(&dname);

      out1:

	jfs_info("jfs_create: rc:%d", rc);
	return rc;
}


/*
 * NAME:	jfs_mkdir(dip, dentry, mode)
 *
 * FUNCTION:	create a child directory in the parent directory <dip>
 *		with name = <from dentry> and mode = <mode>
 *
D
Dave Kleikamp 已提交
201
 * PARAMETER:	dip	- parent directory vnode
L
Linus Torvalds 已提交
202 203 204 205 206 207 208 209
 *		dentry	- dentry of child directory
 *		mode	- create mode (rwxrwxrwx).
 *
 * RETURN:	Errors from subroutines
 *
 * note:
 * EACCESS: user needs search+write permission on the parent directory
 */
210
static int jfs_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode)
L
Linus Torvalds 已提交
211 212 213 214 215 216 217 218 219 220
{
	int rc = 0;
	tid_t tid;		/* transaction id */
	struct inode *ip = NULL;	/* child directory inode */
	ino_t ino;
	struct component_name dname;	/* child directory name */
	struct btstack btstack;
	struct inode *iplist[2];
	struct tblock *tblk;

A
Al Viro 已提交
221
	jfs_info("jfs_mkdir: dip:0x%p name:%pd", dip, dentry);
L
Linus Torvalds 已提交
222

223 224 225
	rc = dquot_initialize(dip);
	if (rc)
		goto out1;
226

L
Linus Torvalds 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239
	/*
	 * search parent directory for entry/freespace
	 * (dtSearch() returns parent directory page pinned)
	 */
	if ((rc = get_UCSname(&dname, dentry)))
		goto out1;

	/*
	 * Either iAlloc() or txBegin() may block.  Deadlock can occur if we
	 * block there while holding dtree page, so we allocate the inode &
	 * begin the transaction before we search the directory.
	 */
	ip = ialloc(dip, S_IFDIR | mode);
240 241
	if (IS_ERR(ip)) {
		rc = PTR_ERR(ip);
L
Linus Torvalds 已提交
242 243 244 245 246
		goto out2;
	}

	tid = txBegin(dip->i_sb, 0);

D
Dave Kleikamp 已提交
247 248
	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
L
Linus Torvalds 已提交
249

250 251 252 253
	rc = jfs_init_acl(tid, ip, dip);
	if (rc)
		goto out3;

254
	rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
D
Dave Kleikamp 已提交
255 256 257 258 259
	if (rc) {
		txAbort(tid, 0);
		goto out3;
	}

L
Linus Torvalds 已提交
260 261
	if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
		jfs_err("jfs_mkdir: dtSearch returned %d", rc);
262
		txAbort(tid, 0);
L
Linus Torvalds 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
		goto out3;
	}

	tblk = tid_to_tblock(tid);
	tblk->xflag |= COMMIT_CREATE;
	tblk->ino = ip->i_ino;
	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;

	iplist[0] = dip;
	iplist[1] = ip;

	/*
	 * initialize the child directory in-line in inode
	 */
	dtInitRoot(tid, ip, dip->i_ino);

	/*
	 * create entry in parent directory for child directory
	 * (dtInsert() releases parent directory page)
	 */
	ino = ip->i_ino;
	if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
		if (rc == -EIO) {
			jfs_err("jfs_mkdir: dtInsert returned -EIO");
			txAbort(tid, 1);	/* Marks Filesystem dirty */
		} else
			txAbort(tid, 0);	/* Filesystem full */
		goto out3;
	}

M
Miklos Szeredi 已提交
293
	set_nlink(ip, 2);	/* for '.' */
L
Linus Torvalds 已提交
294 295 296 297 298 299
	ip->i_op = &jfs_dir_inode_operations;
	ip->i_fop = &jfs_dir_operations;

	mark_inode_dirty(ip);

	/* update parent directory inode */
300
	inc_nlink(dip);		/* for '..' from child directory */
L
Linus Torvalds 已提交
301 302 303 304 305 306 307
	dip->i_ctime = dip->i_mtime = CURRENT_TIME;
	mark_inode_dirty(dip);

	rc = txCommit(tid, 2, &iplist[0], 0);

      out3:
	txEnd(tid);
308
	mutex_unlock(&JFS_IP(ip)->commit_mutex);
E
Evgeniy Dushistov 已提交
309
	mutex_unlock(&JFS_IP(dip)->commit_mutex);
L
Linus Torvalds 已提交
310
	if (rc) {
311
		free_ea_wmap(ip);
312
		clear_nlink(ip);
D
Dave Kleikamp 已提交
313
		unlock_new_inode(ip);
L
Linus Torvalds 已提交
314
		iput(ip);
D
Dave Kleikamp 已提交
315 316
	} else {
		unlock_new_inode(ip);
317
		d_instantiate(dentry, ip);
D
Dave Kleikamp 已提交
318
	}
L
Linus Torvalds 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

      out2:
	free_UCSname(&dname);


      out1:

	jfs_info("jfs_mkdir: rc:%d", rc);
	return rc;
}

/*
 * NAME:	jfs_rmdir(dip, dentry)
 *
 * FUNCTION:	remove a link to child directory
 *
D
Dave Kleikamp 已提交
335
 * PARAMETER:	dip	- parent inode
L
Linus Torvalds 已提交
336 337 338
 *		dentry	- child directory dentry
 *
 * RETURN:	-EINVAL	- if name is . or ..
339
 *		-EINVAL - if . or .. exist but are invalid.
L
Linus Torvalds 已提交
340 341 342
 *		errors from subroutines
 *
 * note:
D
Dave Kleikamp 已提交
343 344 345 346
 * if other threads have the directory open when the last link
 * is removed, the "." and ".." entries, if present, are removed before
 * rmdir() returns and no new entries may be created in the directory,
 * but the directory is not removed until the last reference to
L
Linus Torvalds 已提交
347 348 349 350 351 352
 * the directory is released (cf.unlink() of regular file).
 */
static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
{
	int rc;
	tid_t tid;		/* transaction id */
353
	struct inode *ip = d_inode(dentry);
L
Linus Torvalds 已提交
354 355 356 357 358
	ino_t ino;
	struct component_name dname;
	struct inode *iplist[2];
	struct tblock *tblk;

A
Al Viro 已提交
359
	jfs_info("jfs_rmdir: dip:0x%p name:%pd", dip, dentry);
L
Linus Torvalds 已提交
360 361

	/* Init inode for quota operations. */
362 363 364 365 366 367
	rc = dquot_initialize(dip);
	if (rc)
		goto out;
	rc = dquot_initialize(ip);
	if (rc)
		goto out;
L
Linus Torvalds 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380

	/* directory must be empty to be removed */
	if (!dtEmpty(ip)) {
		rc = -ENOTEMPTY;
		goto out;
	}

	if ((rc = get_UCSname(&dname, dentry))) {
		goto out;
	}

	tid = txBegin(dip->i_sb, 0);

D
Dave Kleikamp 已提交
381 382
	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
L
Linus Torvalds 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

	iplist[0] = dip;
	iplist[1] = ip;

	tblk = tid_to_tblock(tid);
	tblk->xflag |= COMMIT_DELETE;
	tblk->u.ip = ip;

	/*
	 * delete the entry of target directory from parent directory
	 */
	ino = ip->i_ino;
	if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
		jfs_err("jfs_rmdir: dtDelete returned %d", rc);
		if (rc == -EIO)
			txAbort(tid, 1);
		txEnd(tid);
400
		mutex_unlock(&JFS_IP(ip)->commit_mutex);
E
Evgeniy Dushistov 已提交
401
		mutex_unlock(&JFS_IP(dip)->commit_mutex);
L
Linus Torvalds 已提交
402 403 404 405 406 407 408 409

		goto out2;
	}

	/* update parent directory's link count corresponding
	 * to ".." entry of the target directory deleted
	 */
	dip->i_ctime = dip->i_mtime = CURRENT_TIME;
410
	inode_dec_link_count(dip);
L
Linus Torvalds 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429

	/*
	 * OS/2 could have created EA and/or ACL
	 */
	/* free EA from both persistent and working map */
	if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
		/* free EA pages */
		txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
	}
	JFS_IP(ip)->ea.flag = 0;

	/* free ACL from both persistent and working map */
	if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
		/* free ACL pages */
		txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
	}
	JFS_IP(ip)->acl.flag = 0;

	/* mark the target directory as deleted */
430
	clear_nlink(ip);
L
Linus Torvalds 已提交
431 432 433 434 435 436
	mark_inode_dirty(ip);

	rc = txCommit(tid, 2, &iplist[0], 0);

	txEnd(tid);

437
	mutex_unlock(&JFS_IP(ip)->commit_mutex);
E
Evgeniy Dushistov 已提交
438
	mutex_unlock(&JFS_IP(dip)->commit_mutex);
L
Linus Torvalds 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

	/*
	 * Truncating the directory index table is not guaranteed.  It
	 * may need to be done iteratively
	 */
	if (test_cflag(COMMIT_Stale, dip)) {
		if (dip->i_size > 1)
			jfs_truncate_nolock(dip, 0);

		clear_cflag(COMMIT_Stale, dip);
	}

      out2:
	free_UCSname(&dname);

      out:
	jfs_info("jfs_rmdir: rc:%d", rc);
	return rc;
}

/*
 * NAME:	jfs_unlink(dip, dentry)
 *
D
Dave Kleikamp 已提交
462
 * FUNCTION:	remove a link to object <vp> named by <name>
L
Linus Torvalds 已提交
463 464
 *		from parent directory <dvp>
 *
D
Dave Kleikamp 已提交
465 466
 * PARAMETER:	dip	- inode of parent directory
 *		dentry	- dentry of object to be removed
L
Linus Torvalds 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
 *
 * RETURN:	errors from subroutines
 *
 * note:
 * temporary file: if one or more processes have the file open
 * when the last link is removed, the link will be removed before
 * unlink() returns, but the removal of the file contents will be
 * postponed until all references to the files are closed.
 *
 * JFS does NOT support unlink() on directories.
 *
 */
static int jfs_unlink(struct inode *dip, struct dentry *dentry)
{
	int rc;
	tid_t tid;		/* transaction id */
483
	struct inode *ip = d_inode(dentry);
L
Linus Torvalds 已提交
484 485 486 487 488 489 490
	ino_t ino;
	struct component_name dname;	/* object name */
	struct inode *iplist[2];
	struct tblock *tblk;
	s64 new_size = 0;
	int commit_flag;

A
Al Viro 已提交
491
	jfs_info("jfs_unlink: dip:0x%p name:%pd", dip, dentry);
L
Linus Torvalds 已提交
492 493

	/* Init inode for quota operations. */
494 495 496 497 498 499
	rc = dquot_initialize(dip);
	if (rc)
		goto out;
	rc = dquot_initialize(ip);
	if (rc)
		goto out;
L
Linus Torvalds 已提交
500 501 502 503

	if ((rc = get_UCSname(&dname, dentry)))
		goto out;

D
Dave Kleikamp 已提交
504
	IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
L
Linus Torvalds 已提交
505 506 507

	tid = txBegin(dip->i_sb, 0);

D
Dave Kleikamp 已提交
508 509
	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
L
Linus Torvalds 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522

	iplist[0] = dip;
	iplist[1] = ip;

	/*
	 * delete the entry of target file from parent directory
	 */
	ino = ip->i_ino;
	if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
		jfs_err("jfs_unlink: dtDelete returned %d", rc);
		if (rc == -EIO)
			txAbort(tid, 1);	/* Marks FS Dirty */
		txEnd(tid);
523
		mutex_unlock(&JFS_IP(ip)->commit_mutex);
E
Evgeniy Dushistov 已提交
524
		mutex_unlock(&JFS_IP(dip)->commit_mutex);
L
Linus Torvalds 已提交
525 526 527 528 529 530 531 532 533 534
		IWRITE_UNLOCK(ip);
		goto out1;
	}

	ASSERT(ip->i_nlink);

	ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
	mark_inode_dirty(dip);

	/* update target's inode */
535
	inode_dec_link_count(ip);
L
Linus Torvalds 已提交
536 537

	/*
538
	 *	commit zero link count object
L
Linus Torvalds 已提交
539 540 541 542 543 544 545
	 */
	if (ip->i_nlink == 0) {
		assert(!test_cflag(COMMIT_Nolink, ip));
		/* free block resources */
		if ((new_size = commitZeroLink(tid, ip)) < 0) {
			txAbort(tid, 1);	/* Marks FS Dirty */
			txEnd(tid);
546
			mutex_unlock(&JFS_IP(ip)->commit_mutex);
E
Evgeniy Dushistov 已提交
547
			mutex_unlock(&JFS_IP(dip)->commit_mutex);
L
Linus Torvalds 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
			IWRITE_UNLOCK(ip);
			rc = new_size;
			goto out1;
		}
		tblk = tid_to_tblock(tid);
		tblk->xflag |= COMMIT_DELETE;
		tblk->u.ip = ip;
	}

	/*
	 * Incomplete truncate of file data can
	 * result in timing problems unless we synchronously commit the
	 * transaction.
	 */
	if (new_size)
		commit_flag = COMMIT_SYNC;
	else
		commit_flag = 0;

	/*
	 * If xtTruncate was incomplete, commit synchronously to avoid
	 * timing complications
	 */
	rc = txCommit(tid, 2, &iplist[0], commit_flag);

	txEnd(tid);

575
	mutex_unlock(&JFS_IP(ip)->commit_mutex);
E
Evgeniy Dushistov 已提交
576
	mutex_unlock(&JFS_IP(dip)->commit_mutex);
L
Linus Torvalds 已提交
577 578 579

	while (new_size && (rc == 0)) {
		tid = txBegin(dip->i_sb, 0);
580
		mutex_lock(&JFS_IP(ip)->commit_mutex);
L
Linus Torvalds 已提交
581 582 583 584 585 586 587
		new_size = xtTruncate_pmap(tid, ip, new_size);
		if (new_size < 0) {
			txAbort(tid, 1);	/* Marks FS Dirty */
			rc = new_size;
		} else
			rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
		txEnd(tid);
588
		mutex_unlock(&JFS_IP(ip)->commit_mutex);
L
Linus Torvalds 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
	}

	if (ip->i_nlink == 0)
		set_cflag(COMMIT_Nolink, ip);

	IWRITE_UNLOCK(ip);

	/*
	 * Truncating the directory index table is not guaranteed.  It
	 * may need to be done iteratively
	 */
	if (test_cflag(COMMIT_Stale, dip)) {
		if (dip->i_size > 1)
			jfs_truncate_nolock(dip, 0);

		clear_cflag(COMMIT_Stale, dip);
	}

      out1:
	free_UCSname(&dname);
      out:
	jfs_info("jfs_unlink: rc:%d", rc);
	return rc;
}

/*
 * NAME:	commitZeroLink()
 *
617
 * FUNCTION:	for non-directory, called by jfs_remove(),
L
Linus Torvalds 已提交
618
 *		truncate a regular file, directory or symbolic
D
Dave Kleikamp 已提交
619
 *		link to zero length. return 0 if type is not
L
Linus Torvalds 已提交
620 621 622 623 624 625 626 627 628
 *		one of these.
 *
 *		if the file is currently associated with a VM segment
 *		only permanent disk and inode map resources are freed,
 *		and neither the inode nor indirect blocks are modified
 *		so that the resources can be later freed in the work
 *		map by ctrunc1.
 *		if there is no VM segment on entry, the resources are
 *		freed in both work and permanent map.
D
Dave Kleikamp 已提交
629
 *		(? for temporary file - memory object is cached even
L
Linus Torvalds 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
 *		after no reference:
 *		reference count > 0 -   )
 *
 * PARAMETERS:	cd	- pointer to commit data structure.
 *			  current inode is the one to truncate.
 *
 * RETURN:	Errors from subroutines
 */
static s64 commitZeroLink(tid_t tid, struct inode *ip)
{
	int filetype;
	struct tblock *tblk;

	jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);

	filetype = ip->i_mode & S_IFMT;
	switch (filetype) {
	case S_IFREG:
		break;
	case S_IFLNK:
		/* fast symbolic link */
		if (ip->i_size < IDATASIZE) {
			ip->i_size = 0;
			return 0;
		}
		break;
	default:
		assert(filetype != S_IFDIR);
		return 0;
	}

	set_cflag(COMMIT_Freewmap, ip);

	/* mark transaction of block map update type */
	tblk = tid_to_tblock(tid);
	tblk->xflag |= COMMIT_PMAP;

	/*
	 * free EA
	 */
	if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
		/* acquire maplock on EA to be freed from block map */
		txEA(tid, ip, &JFS_IP(ip)->ea, NULL);

	/*
	 * free ACL
	 */
	if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
		/* acquire maplock on EA to be freed from block map */
		txEA(tid, ip, &JFS_IP(ip)->acl, NULL);

	/*
	 * free xtree/data (truncate to zero length):
D
Dave Kleikamp 已提交
683
	 * free xtree/data pages from cache if COMMIT_PWMAP,
L
Linus Torvalds 已提交
684 685 686 687 688 689 690 691 692 693 694
	 * free xtree/data blocks from persistent block map, and
	 * free xtree/data blocks from working block map if COMMIT_PWMAP;
	 */
	if (ip->i_size)
		return xtTruncate_pmap(tid, ip, 0);

	return 0;
}


/*
695
 * NAME:	jfs_free_zero_link()
L
Linus Torvalds 已提交
696
 *
697
 * FUNCTION:	for non-directory, called by iClose(),
D
Dave Kleikamp 已提交
698
 *		free resources of a file from cache and WORKING map
L
Linus Torvalds 已提交
699 700 701 702 703
 *		for a file previously committed with zero link count
 *		while associated with a pager object,
 *
 * PARAMETER:	ip	- pointer to inode of file.
 */
704
void jfs_free_zero_link(struct inode *ip)
L
Linus Torvalds 已提交
705 706 707
{
	int type;

708
	jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
L
Linus Torvalds 已提交
709 710 711 712 713 714 715 716 717 718 719 720

	/* return if not reg or symbolic link or if size is
	 * already ok.
	 */
	type = ip->i_mode & S_IFMT;

	switch (type) {
	case S_IFREG:
		break;
	case S_IFLNK:
		/* if its contained in inode nothing to do */
		if (ip->i_size < IDATASIZE)
721
			return;
L
Linus Torvalds 已提交
722 723
		break;
	default:
724
		return;
L
Linus Torvalds 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
	}

	/*
	 * free EA
	 */
	if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
		s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
		int xlen = lengthDXD(&JFS_IP(ip)->ea);
		struct maplock maplock;	/* maplock for COMMIT_WMAP */
		struct pxd_lock *pxdlock;	/* maplock for COMMIT_WMAP */

		/* free EA pages from cache */
		invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);

		/* free EA extent from working block map */
		maplock.index = 1;
		pxdlock = (struct pxd_lock *) & maplock;
		pxdlock->flag = mlckFREEPXD;
		PXDaddress(&pxdlock->pxd, xaddr);
		PXDlength(&pxdlock->pxd, xlen);
		txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
	}

	/*
	 * free ACL
	 */
	if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
		s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
		int xlen = lengthDXD(&JFS_IP(ip)->acl);
		struct maplock maplock;	/* maplock for COMMIT_WMAP */
		struct pxd_lock *pxdlock;	/* maplock for COMMIT_WMAP */

		invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);

		/* free ACL extent from working block map */
		maplock.index = 1;
		pxdlock = (struct pxd_lock *) & maplock;
		pxdlock->flag = mlckFREEPXD;
		PXDaddress(&pxdlock->pxd, xaddr);
		PXDlength(&pxdlock->pxd, xlen);
		txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
	}

	/*
	 * free xtree/data (truncate to zero length):
	 * free xtree/data pages from cache, and
	 * free xtree/data blocks from working block map;
	 */
	if (ip->i_size)
774
		xtTruncate(0, ip, 0, COMMIT_WMAP);
L
Linus Torvalds 已提交
775 776 777 778 779 780 781 782
}

/*
 * NAME:	jfs_link(vp, dvp, name, crp)
 *
 * FUNCTION:	create a link to <vp> by the name = <name>
 *		in the parent directory <dvp>
 *
D
Dave Kleikamp 已提交
783
 * PARAMETER:	vp	- target object
L
Linus Torvalds 已提交
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
 *		dvp	- parent directory of new link
 *		name	- name of new link to target object
 *		crp	- credential
 *
 * RETURN:	Errors from subroutines
 *
 * note:
 * JFS does NOT support link() on directories (to prevent circular
 * path in the directory hierarchy);
 * EPERM: the target object is a directory, and either the caller
 * does not have appropriate privileges or the implementation prohibits
 * using link() on directories [XPG4.2].
 *
 * JFS does NOT support links between file systems:
 * EXDEV: target object and new link are on different file systems and
 * implementation does not support links between file systems [XPG4.2].
 */
static int jfs_link(struct dentry *old_dentry,
	     struct inode *dir, struct dentry *dentry)
{
	int rc;
	tid_t tid;
806
	struct inode *ip = d_inode(old_dentry);
L
Linus Torvalds 已提交
807 808 809 810 811
	ino_t ino;
	struct component_name dname;
	struct btstack btstack;
	struct inode *iplist[2];

A
Al Viro 已提交
812
	jfs_info("jfs_link: %pd %pd", old_dentry, dentry);
L
Linus Torvalds 已提交
813

814 815 816
	rc = dquot_initialize(dir);
	if (rc)
		goto out;
817

L
Linus Torvalds 已提交
818 819
	tid = txBegin(ip->i_sb, 0);

D
Dave Kleikamp 已提交
820 821
	mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
L
Linus Torvalds 已提交
822 823 824 825 826

	/*
	 * scan parent directory for entry/freespace
	 */
	if ((rc = get_UCSname(&dname, dentry)))
827
		goto out_tx;
L
Linus Torvalds 已提交
828 829 830 831 832 833 834 835 836 837 838 839

	if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
		goto free_dname;

	/*
	 * create entry for new link in parent directory
	 */
	ino = ip->i_ino;
	if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
		goto free_dname;

	/* update object inode */
840
	inc_nlink(ip);		/* for new link */
L
Linus Torvalds 已提交
841
	ip->i_ctime = CURRENT_TIME;
842
	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
L
Linus Torvalds 已提交
843
	mark_inode_dirty(dir);
A
Al Viro 已提交
844
	ihold(ip);
L
Linus Torvalds 已提交
845 846 847 848 849 850

	iplist[0] = ip;
	iplist[1] = dir;
	rc = txCommit(tid, 2, &iplist[0], 0);

	if (rc) {
851
		drop_nlink(ip); /* never instantiated */
L
Linus Torvalds 已提交
852 853 854 855 856 857 858
		iput(ip);
	} else
		d_instantiate(dentry, ip);

      free_dname:
	free_UCSname(&dname);

859
      out_tx:
L
Linus Torvalds 已提交
860 861
	txEnd(tid);

862
	mutex_unlock(&JFS_IP(ip)->commit_mutex);
E
Evgeniy Dushistov 已提交
863
	mutex_unlock(&JFS_IP(dir)->commit_mutex);
L
Linus Torvalds 已提交
864

865
      out:
L
Linus Torvalds 已提交
866 867 868 869 870 871 872 873
	jfs_info("jfs_link: rc:%d", rc);
	return rc;
}

/*
 * NAME:	jfs_symlink(dip, dentry, name)
 *
 * FUNCTION:	creates a symbolic link to <symlink> by name <name>
874
 *			in directory <dip>
L
Linus Torvalds 已提交
875
 *
876 877 878 879
 * PARAMETER:	dip	- parent directory vnode
 *		dentry	- dentry of symbolic link
 *		name	- the path name of the existing object
 *			  that will be the source of the link
L
Linus Torvalds 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
 *
 * RETURN:	errors from subroutines
 *
 * note:
 * ENAMETOOLONG: pathname resolution of a symbolic link produced
 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
*/

static int jfs_symlink(struct inode *dip, struct dentry *dentry,
		const char *name)
{
	int rc;
	tid_t tid;
	ino_t ino = 0;
	struct component_name dname;
	int ssize;		/* source pathname size */
	struct btstack btstack;
897
	struct inode *ip = d_inode(dentry);
L
Linus Torvalds 已提交
898 899
	s64 xlen = 0;
	int bmask = 0, xsize;
900
	s64 xaddr;
L
Linus Torvalds 已提交
901 902 903 904 905 906 907 908
	struct metapage *mp;
	struct super_block *sb;
	struct tblock *tblk;

	struct inode *iplist[2];

	jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);

909 910 911
	rc = dquot_initialize(dip);
	if (rc)
		goto out1;
912

L
Linus Torvalds 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
	ssize = strlen(name) + 1;

	/*
	 * search parent directory for entry/freespace
	 * (dtSearch() returns parent directory page pinned)
	 */

	if ((rc = get_UCSname(&dname, dentry)))
		goto out1;

	/*
	 * allocate on-disk/in-memory inode for symbolic link:
	 * (iAlloc() returns new, locked inode)
	 */
	ip = ialloc(dip, S_IFLNK | 0777);
928 929
	if (IS_ERR(ip)) {
		rc = PTR_ERR(ip);
L
Linus Torvalds 已提交
930 931 932 933 934
		goto out2;
	}

	tid = txBegin(dip->i_sb, 0);

D
Dave Kleikamp 已提交
935 936
	mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
L
Linus Torvalds 已提交
937

938
	rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
D
Dave Kleikamp 已提交
939 940 941
	if (rc)
		goto out3;

L
Linus Torvalds 已提交
942 943 944 945 946 947
	tblk = tid_to_tblock(tid);
	tblk->xflag |= COMMIT_CREATE;
	tblk->ino = ip->i_ino;
	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;

	/* fix symlink access permission
D
Dave Kleikamp 已提交
948
	 * (dir_create() ANDs in the u.u_cmask,
L
Linus Torvalds 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962
	 * but symlinks really need to be 777 access)
	 */
	ip->i_mode |= 0777;

	/*
	 * write symbolic link target path name
	 */
	xtInitRoot(tid, ip);

	/*
	 * write source path name inline in on-disk inode (fast symbolic link)
	 */

	if (ssize <= IDATASIZE) {
963
		ip->i_op = &jfs_fast_symlink_inode_operations;
L
Linus Torvalds 已提交
964

A
Al Viro 已提交
965 966
		ip->i_link = JFS_IP(ip)->i_inline;
		memcpy(ip->i_link, name, ssize);
L
Linus Torvalds 已提交
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
		ip->i_size = ssize - 1;

		/*
		 * if symlink is > 128 bytes, we don't have the space to
		 * store inline extended attributes
		 */
		if (ssize > sizeof (JFS_IP(ip)->i_inline))
			JFS_IP(ip)->mode2 &= ~INLINEEA;

		jfs_info("jfs_symlink: fast symlink added  ssize:%d name:%s ",
			 ssize, name);
	}
	/*
	 * write source path name in a single extent
	 */
	else {
		jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);

985
		ip->i_op = &jfs_symlink_inode_operations;
L
Linus Torvalds 已提交
986 987 988
		ip->i_mapping->a_ops = &jfs_aops;

		/*
D
Dave Kleikamp 已提交
989
		 * even though the data of symlink object (source
L
Linus Torvalds 已提交
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
		 * path name) is treated as non-journaled user data,
		 * it is read/written thru buffer cache for performance.
		 */
		sb = ip->i_sb;
		bmask = JFS_SBI(sb)->bsize - 1;
		xsize = (ssize + bmask) & ~bmask;
		xaddr = 0;
		xlen = xsize >> JFS_SBI(sb)->l2bsize;
		if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
			txAbort(tid, 0);
			goto out3;
		}
		ip->i_size = ssize - 1;
		while (ssize) {
			/* This is kind of silly since PATH_MAX == 4K */
			int copy_size = min(ssize, PSIZE);

			mp = get_metapage(ip, xaddr, PSIZE, 1);

			if (mp == NULL) {
				xtTruncate(tid, ip, 0, COMMIT_PWMAP);
				rc = -EIO;
				txAbort(tid, 0);
				goto out3;
			}
			memcpy(mp->data, name, copy_size);
			flush_metapage(mp);
			ssize -= copy_size;
			name += copy_size;
			xaddr += JFS_SBI(sb)->nbperpage;
		}
	}

	/*
	 * create entry for symbolic link in parent directory
	 */
	rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
	if (rc == 0) {
		ino = ip->i_ino;
		rc = dtInsert(tid, dip, &dname, &ino, &btstack);
	}
	if (rc) {
		if (xlen)
			xtTruncate(tid, ip, 0, COMMIT_PWMAP);
		txAbort(tid, 0);
		/* discard new inode */
		goto out3;
	}

	mark_inode_dirty(ip);

1041 1042
	dip->i_ctime = dip->i_mtime = CURRENT_TIME;
	mark_inode_dirty(dip);
L
Linus Torvalds 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
	/*
	 * commit update of parent directory and link object
	 */

	iplist[0] = dip;
	iplist[1] = ip;
	rc = txCommit(tid, 2, &iplist[0], 0);

      out3:
	txEnd(tid);
1053
	mutex_unlock(&JFS_IP(ip)->commit_mutex);
E
Evgeniy Dushistov 已提交
1054
	mutex_unlock(&JFS_IP(dip)->commit_mutex);
L
Linus Torvalds 已提交
1055
	if (rc) {
1056
		free_ea_wmap(ip);
1057
		clear_nlink(ip);
D
Dave Kleikamp 已提交
1058
		unlock_new_inode(ip);
L
Linus Torvalds 已提交
1059
		iput(ip);
D
Dave Kleikamp 已提交
1060 1061
	} else {
		unlock_new_inode(ip);
1062
		d_instantiate(dentry, ip);
D
Dave Kleikamp 已提交
1063
	}
L
Linus Torvalds 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074

      out2:
	free_UCSname(&dname);

      out1:
	jfs_info("jfs_symlink: rc:%d", rc);
	return rc;
}


/*
1075
 * NAME:	jfs_rename
L
Linus Torvalds 已提交
1076
 *
1077
 * FUNCTION:	rename a file or directory
L
Linus Torvalds 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
 */
static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
	       struct inode *new_dir, struct dentry *new_dentry)
{
	struct btstack btstack;
	ino_t ino;
	struct component_name new_dname;
	struct inode *new_ip;
	struct component_name old_dname;
	struct inode *old_ip;
	int rc;
	tid_t tid;
	struct tlock *tlck;
	struct dt_lock *dtlck;
	struct lv *lv;
	int ipcount;
	struct inode *iplist[4];
	struct tblock *tblk;
	s64 new_size = 0;
	int commit_flag;


A
Al Viro 已提交
1100
	jfs_info("jfs_rename: %pd %pd", old_dentry, new_dentry);
L
Linus Torvalds 已提交
1101

1102 1103 1104 1105 1106 1107
	rc = dquot_initialize(old_dir);
	if (rc)
		goto out1;
	rc = dquot_initialize(new_dir);
	if (rc)
		goto out1;
1108

1109 1110
	old_ip = d_inode(old_dentry);
	new_ip = d_inode(new_dentry);
L
Linus Torvalds 已提交
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130

	if ((rc = get_UCSname(&old_dname, old_dentry)))
		goto out1;

	if ((rc = get_UCSname(&new_dname, new_dentry)))
		goto out2;

	/*
	 * Make sure source inode number is what we think it is
	 */
	rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
	if (rc || (ino != old_ip->i_ino)) {
		rc = -ENOENT;
		goto out3;
	}

	/*
	 * Make sure dest inode number (if any) is what we think it is
	 */
	rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1131
	if (!rc) {
1132
		if ((!new_ip) || (ino != new_ip->i_ino)) {
L
Linus Torvalds 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
			rc = -ESTALE;
			goto out3;
		}
	} else if (rc != -ENOENT)
		goto out3;
	else if (new_ip) {
		/* no entry exists, but one was expected */
		rc = -ESTALE;
		goto out3;
	}

	if (S_ISDIR(old_ip->i_mode)) {
		if (new_ip) {
			if (!dtEmpty(new_ip)) {
				rc = -ENOTEMPTY;
				goto out3;
			}
		}
	} else if (new_ip) {
D
Dave Kleikamp 已提交
1152
		IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL);
L
Linus Torvalds 已提交
1153
		/* Init inode for quota operations. */
1154 1155 1156
		rc = dquot_initialize(new_ip);
		if (rc)
			goto out_unlock;
L
Linus Torvalds 已提交
1157 1158 1159 1160 1161 1162 1163
	}

	/*
	 * The real work starts here
	 */
	tid = txBegin(new_dir->i_sb, 0);

D
Dave Kleikamp 已提交
1164 1165 1166 1167 1168 1169 1170 1171
	/*
	 * How do we know the locking is safe from deadlocks?
	 * The vfs does the hard part for us.  Any time we are taking nested
	 * commit_mutexes, the vfs already has i_mutex held on the parent.
	 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
	 */
	mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT);
	mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD);
L
Linus Torvalds 已提交
1172
	if (old_dir != new_dir)
D
Dave Kleikamp 已提交
1173 1174
		mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex,
				  COMMIT_MUTEX_SECOND_PARENT);
L
Linus Torvalds 已提交
1175 1176

	if (new_ip) {
D
Dave Kleikamp 已提交
1177 1178
		mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex,
				  COMMIT_MUTEX_VICTIM);
L
Linus Torvalds 已提交
1179 1180 1181 1182 1183 1184 1185
		/*
		 * Change existing directory entry to new inode number
		 */
		ino = new_ip->i_ino;
		rc = dtModify(tid, new_dir, &new_dname, &ino,
			      old_ip->i_ino, JFS_RENAME);
		if (rc)
1186
			goto out_tx;
1187
		drop_nlink(new_ip);
L
Linus Torvalds 已提交
1188
		if (S_ISDIR(new_ip->i_mode)) {
1189
			drop_nlink(new_ip);
L
Linus Torvalds 已提交
1190
			if (new_ip->i_nlink) {
E
Evgeniy Dushistov 已提交
1191
				mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
L
Linus Torvalds 已提交
1192
				if (old_dir != new_dir)
1193
					mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
E
Evgeniy Dushistov 已提交
1194 1195
				mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
				mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
L
Linus Torvalds 已提交
1196 1197 1198
				if (!S_ISDIR(old_ip->i_mode) && new_ip)
					IWRITE_UNLOCK(new_ip);
				jfs_error(new_ip->i_sb,
J
Joe Perches 已提交
1199
					  "new_ip->i_nlink != 0\n");
L
Linus Torvalds 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
				return -EIO;
			}
			tblk = tid_to_tblock(tid);
			tblk->xflag |= COMMIT_DELETE;
			tblk->u.ip = new_ip;
		} else if (new_ip->i_nlink == 0) {
			assert(!test_cflag(COMMIT_Nolink, new_ip));
			/* free block resources */
			if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
				txAbort(tid, 1);	/* Marks FS Dirty */
D
Dave Kleikamp 已提交
1210
				rc = new_size;
1211
				goto out_tx;
L
Linus Torvalds 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
			}
			tblk = tid_to_tblock(tid);
			tblk->xflag |= COMMIT_DELETE;
			tblk->u.ip = new_ip;
		} else {
			new_ip->i_ctime = CURRENT_TIME;
			mark_inode_dirty(new_ip);
		}
	} else {
		/*
		 * Add new directory entry
		 */
		rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
			      JFS_CREATE);
		if (rc) {
			jfs_err("jfs_rename didn't expect dtSearch to fail "
				"w/rc = %d", rc);
1229
			goto out_tx;
L
Linus Torvalds 已提交
1230 1231 1232 1233 1234 1235 1236
		}

		ino = old_ip->i_ino;
		rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
		if (rc) {
			if (rc == -EIO)
				jfs_err("jfs_rename: dtInsert returned -EIO");
1237
			goto out_tx;
L
Linus Torvalds 已提交
1238 1239
		}
		if (S_ISDIR(old_ip->i_mode))
1240
			inc_nlink(new_dir);
L
Linus Torvalds 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
	}
	/*
	 * Remove old directory entry
	 */

	ino = old_ip->i_ino;
	rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
	if (rc) {
		jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
			rc);
		txAbort(tid, 1);	/* Marks Filesystem dirty */
1252
		goto out_tx;
L
Linus Torvalds 已提交
1253 1254
	}
	if (S_ISDIR(old_ip->i_mode)) {
1255
		drop_nlink(old_dir);
L
Linus Torvalds 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
		if (old_dir != new_dir) {
			/*
			 * Change inode number of parent for moved directory
			 */

			JFS_IP(old_ip)->i_dtroot.header.idotdot =
				cpu_to_le32(new_dir->i_ino);

			/* Linelock header of dtree */
			tlck = txLock(tid, old_ip,
				    (struct metapage *) &JFS_IP(old_ip)->bxflag,
				      tlckDTREE | tlckBTROOT | tlckRELINK);
			dtlck = (struct dt_lock *) & tlck->lock;
			ASSERT(dtlck->index == 0);
			lv = & dtlck->lv[0];
			lv->offset = 0;
			lv->length = 1;
			dtlck->index++;
		}
	}

	/*
	 * Update ctime on changed/moved inodes & mark dirty
	 */
	old_ip->i_ctime = CURRENT_TIME;
	mark_inode_dirty(old_ip);

	new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
	mark_inode_dirty(new_dir);

	/* Build list of inodes modified by this transaction */
	ipcount = 0;
	iplist[ipcount++] = old_ip;
	if (new_ip)
		iplist[ipcount++] = new_ip;
	iplist[ipcount++] = old_dir;

	if (old_dir != new_dir) {
		iplist[ipcount++] = new_dir;
		old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
		mark_inode_dirty(old_dir);
	}

	/*
	 * Incomplete truncate of file data can
	 * result in timing problems unless we synchronously commit the
	 * transaction.
	 */
	if (new_size)
		commit_flag = COMMIT_SYNC;
	else
		commit_flag = 0;

	rc = txCommit(tid, ipcount, iplist, commit_flag);

1311
      out_tx:
L
Linus Torvalds 已提交
1312 1313
	txEnd(tid);
	if (new_ip)
1314
		mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
E
Evgeniy Dushistov 已提交
1315 1316 1317 1318
	if (old_dir != new_dir)
		mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
	mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
	mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
L
Linus Torvalds 已提交
1319 1320 1321

	while (new_size && (rc == 0)) {
		tid = txBegin(new_ip->i_sb, 0);
1322
		mutex_lock(&JFS_IP(new_ip)->commit_mutex);
L
Linus Torvalds 已提交
1323 1324 1325
		new_size = xtTruncate_pmap(tid, new_ip, new_size);
		if (new_size < 0) {
			txAbort(tid, 1);
D
Dave Kleikamp 已提交
1326
			rc = new_size;
L
Linus Torvalds 已提交
1327 1328 1329
		} else
			rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
		txEnd(tid);
1330
		mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
L
Linus Torvalds 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
	}
	if (new_ip && (new_ip->i_nlink == 0))
		set_cflag(COMMIT_Nolink, new_ip);
	/*
	 * Truncating the directory index table is not guaranteed.  It
	 * may need to be done iteratively
	 */
	if (test_cflag(COMMIT_Stale, old_dir)) {
		if (old_dir->i_size > 1)
			jfs_truncate_nolock(old_dir, 0);

		clear_cflag(COMMIT_Stale, old_dir);
	}
1344
      out_unlock:
1345 1346 1347 1348 1349 1350 1351
	if (new_ip && !S_ISDIR(new_ip->i_mode))
		IWRITE_UNLOCK(new_ip);
      out3:
	free_UCSname(&new_dname);
      out2:
	free_UCSname(&old_dname);
      out1:
L
Linus Torvalds 已提交
1352 1353 1354 1355 1356 1357
	jfs_info("jfs_rename: returning %d", rc);
	return rc;
}


/*
1358
 * NAME:	jfs_mknod
L
Linus Torvalds 已提交
1359
 *
1360
 * FUNCTION:	Create a special file (device)
L
Linus Torvalds 已提交
1361 1362
 */
static int jfs_mknod(struct inode *dir, struct dentry *dentry,
A
Al Viro 已提交
1363
		umode_t mode, dev_t rdev)
L
Linus Torvalds 已提交
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
{
	struct jfs_inode_info *jfs_ip;
	struct btstack btstack;
	struct component_name dname;
	ino_t ino;
	struct inode *ip;
	struct inode *iplist[2];
	int rc;
	tid_t tid;
	struct tblock *tblk;

	if (!new_valid_dev(rdev))
		return -EINVAL;

A
Al Viro 已提交
1378
	jfs_info("jfs_mknod: %pd", dentry);
L
Linus Torvalds 已提交
1379

1380 1381 1382
	rc = dquot_initialize(dir);
	if (rc)
		goto out;
1383

L
Linus Torvalds 已提交
1384 1385 1386 1387
	if ((rc = get_UCSname(&dname, dentry)))
		goto out;

	ip = ialloc(dir, mode);
1388 1389
	if (IS_ERR(ip)) {
		rc = PTR_ERR(ip);
L
Linus Torvalds 已提交
1390 1391 1392 1393 1394 1395
		goto out1;
	}
	jfs_ip = JFS_IP(ip);

	tid = txBegin(dir->i_sb, 0);

D
Dave Kleikamp 已提交
1396 1397
	mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
	mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
L
Linus Torvalds 已提交
1398

1399 1400
	rc = jfs_init_acl(tid, ip, dir);
	if (rc)
L
Linus Torvalds 已提交
1401 1402
		goto out3;

1403
	rc = jfs_init_security(tid, ip, dir, &dentry->d_name);
D
Dave Kleikamp 已提交
1404 1405 1406 1407 1408
	if (rc) {
		txAbort(tid, 0);
		goto out3;
	}

1409 1410 1411 1412 1413
	if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
		txAbort(tid, 0);
		goto out3;
	}

L
Linus Torvalds 已提交
1414 1415 1416 1417 1418 1419
	tblk = tid_to_tblock(tid);
	tblk->xflag |= COMMIT_CREATE;
	tblk->ino = ip->i_ino;
	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;

	ino = ip->i_ino;
1420 1421
	if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
		txAbort(tid, 0);
L
Linus Torvalds 已提交
1422
		goto out3;
1423
	}
L
Linus Torvalds 已提交
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440

	ip->i_op = &jfs_file_inode_operations;
	jfs_ip->dev = new_encode_dev(rdev);
	init_special_inode(ip, ip->i_mode, rdev);

	mark_inode_dirty(ip);

	dir->i_ctime = dir->i_mtime = CURRENT_TIME;

	mark_inode_dirty(dir);

	iplist[0] = dir;
	iplist[1] = ip;
	rc = txCommit(tid, 2, iplist, 0);

      out3:
	txEnd(tid);
1441 1442
	mutex_unlock(&JFS_IP(ip)->commit_mutex);
	mutex_unlock(&JFS_IP(dir)->commit_mutex);
L
Linus Torvalds 已提交
1443
	if (rc) {
1444
		free_ea_wmap(ip);
1445
		clear_nlink(ip);
D
Dave Kleikamp 已提交
1446
		unlock_new_inode(ip);
L
Linus Torvalds 已提交
1447
		iput(ip);
D
Dave Kleikamp 已提交
1448 1449
	} else {
		unlock_new_inode(ip);
1450
		d_instantiate(dentry, ip);
D
Dave Kleikamp 已提交
1451
	}
L
Linus Torvalds 已提交
1452 1453 1454 1455 1456 1457 1458 1459 1460

      out1:
	free_UCSname(&dname);

      out:
	jfs_info("jfs_mknod: returning %d", rc);
	return rc;
}

A
Al Viro 已提交
1461
static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
L
Linus Torvalds 已提交
1462 1463 1464 1465 1466 1467 1468
{
	struct btstack btstack;
	ino_t inum;
	struct inode *ip;
	struct component_name key;
	int rc;

A
Al Viro 已提交
1469
	jfs_info("jfs_lookup: name = %pd", dentry);
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483

	if ((rc = get_UCSname(&key, dentry)))
		return ERR_PTR(rc);
	rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
	free_UCSname(&key);
	if (rc == -ENOENT) {
		ip = NULL;
	} else if (rc) {
		jfs_err("jfs_lookup: dtSearch returned %d", rc);
		ip = ERR_PTR(rc);
	} else {
		ip = jfs_iget(dip->i_sb, inum);
		if (IS_ERR(ip))
			jfs_err("jfs_lookup: iget failed on inum %d", (uint)inum);
L
Linus Torvalds 已提交
1484 1485
	}

1486
	return d_splice_alias(ip, dentry);
L
Linus Torvalds 已提交
1487 1488
}

C
Christoph Hellwig 已提交
1489 1490
static struct inode *jfs_nfs_get_inode(struct super_block *sb,
		u64 ino, u32 generation)
1491 1492 1493 1494 1495
{
	struct inode *inode;

	if (ino == 0)
		return ERR_PTR(-ESTALE);
1496 1497 1498
	inode = jfs_iget(sb, ino);
	if (IS_ERR(inode))
		return ERR_CAST(inode);
1499

1500
	if (generation && inode->i_generation != generation) {
C
Christoph Hellwig 已提交
1501 1502
		iput(inode);
		return ERR_PTR(-ESTALE);
1503 1504
	}

C
Christoph Hellwig 已提交
1505 1506
	return inode;
}
1507

C
Christoph Hellwig 已提交
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
		int fh_len, int fh_type)
{
	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
				    jfs_nfs_get_inode);
}

struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid,
		int fh_len, int fh_type)
{
	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
				    jfs_nfs_get_inode);
1520 1521
}

L
Linus Torvalds 已提交
1522 1523 1524 1525 1526
struct dentry *jfs_get_parent(struct dentry *dentry)
{
	unsigned long parent_ino;

	parent_ino =
1527
		le32_to_cpu(JFS_IP(d_inode(dentry))->i_dtroot.header.idotdot);
L
Linus Torvalds 已提交
1528

1529
	return d_obtain_alias(jfs_iget(d_inode(dentry)->i_sb, parent_ino));
L
Linus Torvalds 已提交
1530 1531
}

1532
const struct inode_operations jfs_dir_inode_operations = {
L
Linus Torvalds 已提交
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
	.create		= jfs_create,
	.lookup		= jfs_lookup,
	.link		= jfs_link,
	.unlink		= jfs_unlink,
	.symlink	= jfs_symlink,
	.mkdir		= jfs_mkdir,
	.rmdir		= jfs_rmdir,
	.mknod		= jfs_mknod,
	.rename		= jfs_rename,
	.setxattr	= jfs_setxattr,
	.getxattr	= jfs_getxattr,
	.listxattr	= jfs_listxattr,
	.removexattr	= jfs_removexattr,
	.setattr	= jfs_setattr,
1547
#ifdef CONFIG_JFS_POSIX_ACL
1548
	.get_acl	= jfs_get_acl,
1549
	.set_acl	= jfs_set_acl,
L
Linus Torvalds 已提交
1550 1551 1552
#endif
};

1553
const struct file_operations jfs_dir_operations = {
L
Linus Torvalds 已提交
1554
	.read		= generic_read_dir,
A
Al Viro 已提交
1555
	.iterate	= jfs_readdir,
L
Linus Torvalds 已提交
1556
	.fsync		= jfs_fsync,
1557
	.unlocked_ioctl = jfs_ioctl,
1558 1559 1560
#ifdef CONFIG_COMPAT
	.compat_ioctl	= jfs_compat_ioctl,
#endif
1561
	.llseek		= generic_file_llseek,
L
Linus Torvalds 已提交
1562 1563
};

1564
static int jfs_ci_hash(const struct dentry *dir, struct qstr *this)
L
Linus Torvalds 已提交
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
{
	unsigned long hash;
	int i;

	hash = init_name_hash();
	for (i=0; i < this->len; i++)
		hash = partial_name_hash(tolower(this->name[i]), hash);
	this->hash = end_name_hash(hash);

	return 0;
}

1577
static int jfs_ci_compare(const struct dentry *parent, const struct dentry *dentry,
N
Nick Piggin 已提交
1578
		unsigned int len, const char *str, const struct qstr *name)
L
Linus Torvalds 已提交
1579 1580 1581
{
	int i, result = 1;

N
Nick Piggin 已提交
1582
	if (len != name->len)
L
Linus Torvalds 已提交
1583
		goto out;
N
Nick Piggin 已提交
1584 1585
	for (i=0; i < len; i++) {
		if (tolower(str[i]) != tolower(name->name[i]))
L
Linus Torvalds 已提交
1586 1587 1588
			goto out;
	}
	result = 0;
1589 1590 1591
out:
	return result;
}
L
Linus Torvalds 已提交
1592

1593
static int jfs_ci_revalidate(struct dentry *dentry, unsigned int flags)
1594
{
L
Linus Torvalds 已提交
1595
	/*
1596 1597 1598 1599 1600 1601 1602 1603
	 * This is not negative dentry. Always valid.
	 *
	 * Note, rename() to existing directory entry will have ->d_inode,
	 * and will use existing name which isn't specified name by user.
	 *
	 * We may be able to drop this positive dentry here. But dropping
	 * positive dentry isn't good idea. So it's unsupported like
	 * rename("filename", "FILENAME") for now.
L
Linus Torvalds 已提交
1604
	 */
1605
	if (d_really_is_positive(dentry))
1606 1607 1608 1609 1610 1611
		return 1;

	/*
	 * This may be nfsd (or something), anyway, we can't see the
	 * intent of this. So, since this can be for creation, drop it.
	 */
1612
	if (!flags)
1613 1614 1615 1616 1617 1618 1619
		return 0;

	/*
	 * Drop the negative dentry, in order to make sure to use the
	 * case sensitive name which is specified by user if this is
	 * for creation.
	 */
1620
	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
1621
		return 0;
1622
	return 1;
L
Linus Torvalds 已提交
1623 1624
}

A
Al Viro 已提交
1625
const struct dentry_operations jfs_ci_dentry_operations =
L
Linus Torvalds 已提交
1626 1627 1628
{
	.d_hash = jfs_ci_hash,
	.d_compare = jfs_ci_compare,
1629
	.d_revalidate = jfs_ci_revalidate,
L
Linus Torvalds 已提交
1630
};