namei.c 33.3 KB
Newer Older
Dave Kleikamp's avatar
Dave Kleikamp committed
1
/*
Dave Kleikamp's avatar
Dave Kleikamp committed
2 3
 *   Copyright (C) International Business Machines Corp., 2000-2003
 *   Portions Copyright (C) Christoph Hellwig, 2001-2002
Dave Kleikamp's avatar
Dave Kleikamp committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 *   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
 *   the Free Software Foundation; either version 2 of the License, or 
 *   (at your option) any later version.
 * 
 *   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
 *   along with this program;  if not, write to the Free Software 
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/fs.h>
#include "jfs_incore.h"
Dave Kleikamp's avatar
Dave Kleikamp committed
22
#include "jfs_superblock.h"
Dave Kleikamp's avatar
Dave Kleikamp committed
23 24 25 26 27
#include "jfs_inode.h"
#include "jfs_dinode.h"
#include "jfs_dmap.h"
#include "jfs_unicode.h"
#include "jfs_metapage.h"
Dave Kleikamp's avatar
Dave Kleikamp committed
28
#include "jfs_xattr.h"
Dave Kleikamp's avatar
Dave Kleikamp committed
29
#include "jfs_acl.h"
Dave Kleikamp's avatar
Dave Kleikamp committed
30 31 32 33 34 35 36 37 38
#include "jfs_debug.h"

extern struct inode_operations jfs_file_inode_operations;
extern struct inode_operations jfs_symlink_inode_operations;
extern struct file_operations jfs_file_operations;
extern struct address_space_operations jfs_aops;

extern int jfs_fsync(struct file *, struct dentry *, int);
extern void jfs_truncate_nolock(struct inode *, loff_t);
Dave Kleikamp's avatar
Dave Kleikamp committed
39
extern int jfs_init_acl(struct inode *, struct inode *);
Dave Kleikamp's avatar
Dave Kleikamp committed
40 41 42 43 44 45 46

/*
 * forward references
 */
struct inode_operations jfs_dir_inode_operations;
struct file_operations jfs_dir_operations;

47
static s64 commitZeroLink(tid_t, struct inode *);
Dave Kleikamp's avatar
Dave Kleikamp committed
48 49 50 51 52 53 54 55 56 57

/*
 * NAME:	jfs_create(dip, dentry, mode)
 *
 * FUNCTION:	create a regular file in the parent directory <dip>
 *		with name = <from dentry> and mode = <mode>
 *
 * PARAMETER:	dip 	- parent directory vnode
 *		dentry	- dentry of new file
 *		mode	- create mode (rwxrwxrwx).
58
 *		nd- nd struct
Dave Kleikamp's avatar
Dave Kleikamp committed
59 60 61 62
 *
 * RETURN:	Errors from subroutines
 *
 */
63
static int jfs_create(struct inode *dip, struct dentry *dentry, int mode,
64
		struct nameidata *nd)
Dave Kleikamp's avatar
Dave Kleikamp committed
65 66 67 68 69
{
	int rc = 0;
	tid_t tid;		/* transaction id */
	struct inode *ip = NULL;	/* child directory inode */
	ino_t ino;
70 71
	struct component_name dname;	/* child directory name */
	struct btstack btstack;
Dave Kleikamp's avatar
Dave Kleikamp committed
72
	struct inode *iplist[2];
73
	struct tblock *tblk;
Dave Kleikamp's avatar
Dave Kleikamp committed
74

75
	jfs_info("jfs_create: dip:0x%p name:%s", dip, dentry->d_name.name);
Dave Kleikamp's avatar
Dave Kleikamp committed
76 77 78 79 80

	/*
	 * search parent directory for entry/freespace
	 * (dtSearch() returns parent directory page pinned)
	 */
81
	if ((rc = get_UCSname(&dname, dentry)))
Dave Kleikamp's avatar
Dave Kleikamp committed
82 83 84 85 86 87 88 89 90
		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);
	if (ip == NULL) {
91
		rc = -ENOSPC;
Dave Kleikamp's avatar
Dave Kleikamp committed
92 93 94 95 96
		goto out2;
	}

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

Dave Kleikamp's avatar
Dave Kleikamp committed
97 98 99
	down(&JFS_IP(dip)->commit_sem);
	down(&JFS_IP(ip)->commit_sem);

Dave Kleikamp's avatar
Dave Kleikamp committed
100
	if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
101
		jfs_err("jfs_create: dtSearch returned %d", rc);
Dave Kleikamp's avatar
Dave Kleikamp committed
102
		goto out3;
Dave Kleikamp's avatar
Dave Kleikamp committed
103 104 105 106
	}

	tblk = tid_to_tblock(tid);
	tblk->xflag |= COMMIT_CREATE;
107 108
	tblk->ino = ip->i_ino;
	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
Dave Kleikamp's avatar
Dave Kleikamp committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

	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))) {
124
		jfs_err("jfs_create: dtInsert returned %d", rc);
125
		if (rc == -EIO)
Dave Kleikamp's avatar
Dave Kleikamp committed
126 127 128
			txAbort(tid, 1);	/* Marks Filesystem dirty */
		else
			txAbort(tid, 0);	/* Filesystem full */
Dave Kleikamp's avatar
Dave Kleikamp committed
129
		goto out3;
Dave Kleikamp's avatar
Dave Kleikamp committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143
	}

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

	insert_inode_hash(ip);
	mark_inode_dirty(ip);

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

	mark_inode_dirty(dip);

	rc = txCommit(tid, 2, &iplist[0], 0);
Dave Kleikamp's avatar
Dave Kleikamp committed
144 145

      out3:
Dave Kleikamp's avatar
Dave Kleikamp committed
146
	txEnd(tid);
Dave Kleikamp's avatar
Dave Kleikamp committed
147 148 149 150 151
	up(&JFS_IP(dip)->commit_sem);
	up(&JFS_IP(ip)->commit_sem);
	if (rc) {
		ip->i_nlink = 0;
		iput(ip);
152 153
	} else
		d_instantiate(dentry, ip);
Dave Kleikamp's avatar
Dave Kleikamp committed
154 155 156 157

      out2:
	free_UCSname(&dname);

Dave Kleikamp's avatar
Dave Kleikamp committed
158 159 160 161 162
#ifdef CONFIG_JFS_POSIX_ACL
	if (rc == 0)
		jfs_init_acl(ip, dip);
#endif

Dave Kleikamp's avatar
Dave Kleikamp committed
163 164
      out1:

165 166
	jfs_info("jfs_create: rc:%d", rc);
	return rc;
Dave Kleikamp's avatar
Dave Kleikamp committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
}


/*
 * NAME:	jfs_mkdir(dip, dentry, mode)
 *
 * FUNCTION:	create a child directory in the parent directory <dip>
 *		with name = <from dentry> and mode = <mode>
 *
 * PARAMETER:	dip 	- parent directory vnode
 *		dentry	- dentry of child directory
 *		mode	- create mode (rwxrwxrwx).
 *
 * RETURN:	Errors from subroutines
 *
 * note:
 * EACCESS: user needs search+write permission on the parent directory
 */
185
static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
Dave Kleikamp's avatar
Dave Kleikamp committed
186 187 188 189 190
{
	int rc = 0;
	tid_t tid;		/* transaction id */
	struct inode *ip = NULL;	/* child directory inode */
	ino_t ino;
191 192
	struct component_name dname;	/* child directory name */
	struct btstack btstack;
Dave Kleikamp's avatar
Dave Kleikamp committed
193
	struct inode *iplist[2];
194
	struct tblock *tblk;
Dave Kleikamp's avatar
Dave Kleikamp committed
195

196
	jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
Dave Kleikamp's avatar
Dave Kleikamp committed
197 198 199

	/* link count overflow on parent directory ? */
	if (dip->i_nlink == JFS_LINK_MAX) {
200
		rc = -EMLINK;
Dave Kleikamp's avatar
Dave Kleikamp committed
201 202 203 204 205 206 207
		goto out1;
	}

	/*
	 * search parent directory for entry/freespace
	 * (dtSearch() returns parent directory page pinned)
	 */
208
	if ((rc = get_UCSname(&dname, dentry)))
Dave Kleikamp's avatar
Dave Kleikamp committed
209 210 211 212 213 214 215 216 217
		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);
	if (ip == NULL) {
218
		rc = -ENOSPC;
Dave Kleikamp's avatar
Dave Kleikamp committed
219 220 221 222 223
		goto out2;
	}

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

Dave Kleikamp's avatar
Dave Kleikamp committed
224 225 226
	down(&JFS_IP(dip)->commit_sem);
	down(&JFS_IP(ip)->commit_sem);

Dave Kleikamp's avatar
Dave Kleikamp committed
227
	if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
228
		jfs_err("jfs_mkdir: dtSearch returned %d", rc);
Dave Kleikamp's avatar
Dave Kleikamp committed
229
		goto out3;
Dave Kleikamp's avatar
Dave Kleikamp committed
230 231 232 233
	}

	tblk = tid_to_tblock(tid);
	tblk->xflag |= COMMIT_CREATE;
234 235
	tblk->ino = ip->i_ino;
	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
Dave Kleikamp's avatar
Dave Kleikamp committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

	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))) {
251
		jfs_err("jfs_mkdir: dtInsert returned %d", rc);
Dave Kleikamp's avatar
Dave Kleikamp committed
252

253
		if (rc == -EIO)
Dave Kleikamp's avatar
Dave Kleikamp committed
254 255 256
			txAbort(tid, 1);	/* Marks Filesystem dirty */
		else
			txAbort(tid, 0);	/* Filesystem full */
Dave Kleikamp's avatar
Dave Kleikamp committed
257
		goto out3;
Dave Kleikamp's avatar
Dave Kleikamp committed
258 259 260 261 262 263
	}

	ip->i_nlink = 2;	/* for '.' */
	ip->i_op = &jfs_dir_inode_operations;
	ip->i_fop = &jfs_dir_operations;
	ip->i_mapping->a_ops = &jfs_aops;
264
	mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
Dave Kleikamp's avatar
Dave Kleikamp committed
265 266 267 268 269 270 271 272 273 274

	insert_inode_hash(ip);
	mark_inode_dirty(ip);

	/* update parent directory inode */
	dip->i_nlink++;		/* for '..' from child directory */
	dip->i_ctime = dip->i_mtime = CURRENT_TIME;
	mark_inode_dirty(dip);

	rc = txCommit(tid, 2, &iplist[0], 0);
Dave Kleikamp's avatar
Dave Kleikamp committed
275 276

      out3:
Dave Kleikamp's avatar
Dave Kleikamp committed
277
	txEnd(tid);
Dave Kleikamp's avatar
Dave Kleikamp committed
278 279 280 281 282
	up(&JFS_IP(dip)->commit_sem);
	up(&JFS_IP(ip)->commit_sem);
	if (rc) {
		ip->i_nlink = 0;
		iput(ip);
283 284
	} else
		d_instantiate(dentry, ip);
Dave Kleikamp's avatar
Dave Kleikamp committed
285 286 287 288

      out2:
	free_UCSname(&dname);

Dave Kleikamp's avatar
Dave Kleikamp committed
289 290 291 292 293
#ifdef CONFIG_JFS_POSIX_ACL
	if (rc == 0)
		jfs_init_acl(ip, dip);
#endif

Dave Kleikamp's avatar
Dave Kleikamp committed
294 295
      out1:

296 297
	jfs_info("jfs_mkdir: rc:%d", rc);
	return rc;
Dave Kleikamp's avatar
Dave Kleikamp committed
298 299 300 301 302 303 304 305 306 307
}

/*
 * NAME:	jfs_rmdir(dip, dentry)
 *
 * FUNCTION:	remove a link to child directory
 *
 * PARAMETER:	dip 	- parent inode
 *		dentry	- child directory dentry
 *
308 309
 * RETURN:	-EINVAL	- if name is . or ..
 *		-EINVAL  - if . or .. exist but are invalid.
Dave Kleikamp's avatar
Dave Kleikamp committed
310 311 312 313 314 315 316 317 318
 *		errors from subroutines
 *
 * note:
 * 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 
 * the directory is released (cf.unlink() of regular file).
 */
319
static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
Dave Kleikamp's avatar
Dave Kleikamp committed
320 321 322 323 324
{
	int rc;
	tid_t tid;		/* transaction id */
	struct inode *ip = dentry->d_inode;
	ino_t ino;
325
	struct component_name dname;
Dave Kleikamp's avatar
Dave Kleikamp committed
326
	struct inode *iplist[2];
327
	struct tblock *tblk;
Dave Kleikamp's avatar
Dave Kleikamp committed
328

329
	jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
Dave Kleikamp's avatar
Dave Kleikamp committed
330 331 332

	/* directory must be empty to be removed */
	if (!dtEmpty(ip)) {
333
		rc = -ENOTEMPTY;
Dave Kleikamp's avatar
Dave Kleikamp committed
334 335 336
		goto out;
	}

337
	if ((rc = get_UCSname(&dname, dentry))) {
Dave Kleikamp's avatar
Dave Kleikamp committed
338 339 340 341 342
		goto out;
	}

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

Dave Kleikamp's avatar
Dave Kleikamp committed
343 344 345
	down(&JFS_IP(dip)->commit_sem);
	down(&JFS_IP(ip)->commit_sem);

Dave Kleikamp's avatar
Dave Kleikamp committed
346 347 348 349 350
	iplist[0] = dip;
	iplist[1] = ip;

	tblk = tid_to_tblock(tid);
	tblk->xflag |= COMMIT_DELETE;
351
	tblk->u.ip = ip;
Dave Kleikamp's avatar
Dave Kleikamp committed
352 353 354 355 356 357

	/*
	 * delete the entry of target directory from parent directory
	 */
	ino = ip->i_ino;
	if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
358
		jfs_err("jfs_rmdir: dtDelete returned %d", rc);
359
		if (rc == -EIO)
Dave Kleikamp's avatar
Dave Kleikamp committed
360 361
			txAbort(tid, 1);
		txEnd(tid);
Dave Kleikamp's avatar
Dave Kleikamp committed
362 363
		up(&JFS_IP(dip)->commit_sem);
		up(&JFS_IP(ip)->commit_sem);
Dave Kleikamp's avatar
Dave Kleikamp committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

		goto out2;
	}

	/* update parent directory's link count corresponding
	 * to ".." entry of the target directory deleted
	 */
	dip->i_nlink--;
	dip->i_ctime = dip->i_mtime = CURRENT_TIME;
	mark_inode_dirty(dip);

	/*
	 * 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 */
	ip->i_nlink = 0;
	mark_inode_dirty(ip);

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

	txEnd(tid);

Dave Kleikamp's avatar
Dave Kleikamp committed
400 401
	up(&JFS_IP(dip)->commit_sem);
	up(&JFS_IP(ip)->commit_sem);
Dave Kleikamp's avatar
Dave Kleikamp committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

	/*
	 * 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:
418
	jfs_info("jfs_rmdir: rc:%d", rc);
419
	return rc;
Dave Kleikamp's avatar
Dave Kleikamp committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
}

/*
 * NAME:	jfs_unlink(dip, dentry)
 *
 * FUNCTION:	remove a link to object <vp> named by <name> 
 *		from parent directory <dvp>
 *
 * PARAMETER:	dip 	- inode of parent directory
 *		dentry 	- dentry of object to be removed
 *
 * 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.
 *
 */
442
static int jfs_unlink(struct inode *dip, struct dentry *dentry)
Dave Kleikamp's avatar
Dave Kleikamp committed
443 444 445 446 447
{
	int rc;
	tid_t tid;		/* transaction id */
	struct inode *ip = dentry->d_inode;
	ino_t ino;
448
	struct component_name dname;	/* object name */
Dave Kleikamp's avatar
Dave Kleikamp committed
449
	struct inode *iplist[2];
450
	struct tblock *tblk;
Dave Kleikamp's avatar
Dave Kleikamp committed
451 452 453
	s64 new_size = 0;
	int commit_flag;

454
	jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
Dave Kleikamp's avatar
Dave Kleikamp committed
455

456
	if ((rc = get_UCSname(&dname, dentry)))
Dave Kleikamp's avatar
Dave Kleikamp committed
457 458
		goto out;

Dave Kleikamp's avatar
Dave Kleikamp committed
459
	IWRITE_LOCK(ip);
Dave Kleikamp's avatar
Dave Kleikamp committed
460 461 462

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

Dave Kleikamp's avatar
Dave Kleikamp committed
463 464 465
	down(&JFS_IP(dip)->commit_sem);
	down(&JFS_IP(ip)->commit_sem);

Dave Kleikamp's avatar
Dave Kleikamp committed
466 467 468 469 470 471 472 473
	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))) {
474
		jfs_err("jfs_unlink: dtDelete returned %d", rc);
475
		if (rc == -EIO)
Dave Kleikamp's avatar
Dave Kleikamp committed
476 477
			txAbort(tid, 1);	/* Marks FS Dirty */
		txEnd(tid);
Dave Kleikamp's avatar
Dave Kleikamp committed
478 479
		up(&JFS_IP(dip)->commit_sem);
		up(&JFS_IP(ip)->commit_sem);
Dave Kleikamp's avatar
Dave Kleikamp committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
		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 */
	ip->i_nlink--;
	mark_inode_dirty(ip);

	/*
	 *      commit zero link count object
	 */
	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);
Dave Kleikamp's avatar
Dave Kleikamp committed
502 503
			up(&JFS_IP(dip)->commit_sem);
			up(&JFS_IP(ip)->commit_sem);
Dave Kleikamp's avatar
Dave Kleikamp committed
504
			IWRITE_UNLOCK(ip);
505
			rc = new_size;
Dave Kleikamp's avatar
Dave Kleikamp committed
506 507 508 509
			goto out1;
		}
		tblk = tid_to_tblock(tid);
		tblk->xflag |= COMMIT_DELETE;
510
		tblk->u.ip = ip;
Dave Kleikamp's avatar
Dave Kleikamp committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
	}

	/*
	 * 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);

Dave Kleikamp's avatar
Dave Kleikamp committed
531 532 533 534
	up(&JFS_IP(dip)->commit_sem);
	up(&JFS_IP(ip)->commit_sem);


Dave Kleikamp's avatar
Dave Kleikamp committed
535 536
	while (new_size && (rc == 0)) {
		tid = txBegin(dip->i_sb, 0);
Dave Kleikamp's avatar
Dave Kleikamp committed
537
		down(&JFS_IP(ip)->commit_sem);
Dave Kleikamp's avatar
Dave Kleikamp committed
538 539 540
		new_size = xtTruncate_pmap(tid, ip, new_size);
		if (new_size < 0) {
			txAbort(tid, 1);	/* Marks FS Dirty */
541
			rc = new_size;
Dave Kleikamp's avatar
Dave Kleikamp committed
542 543 544
		} else
			rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
		txEnd(tid);
Dave Kleikamp's avatar
Dave Kleikamp committed
545
		up(&JFS_IP(ip)->commit_sem);
Dave Kleikamp's avatar
Dave Kleikamp committed
546 547
	}

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

Dave Kleikamp's avatar
Dave Kleikamp committed
551
	IWRITE_UNLOCK(ip);
Dave Kleikamp's avatar
Dave Kleikamp committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

	/*
	 * 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:
567 568
	jfs_info("jfs_unlink: rc:%d", rc);
	return rc;
Dave Kleikamp's avatar
Dave Kleikamp committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
}

/*
 * NAME:	commitZeroLink()
 *
 * FUNCTION:    for non-directory, called by jfs_remove(),
 *		truncate a regular file, directory or symbolic
 *		link to zero length. return 0 if type is not 
 *		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.
 *		(? for temporary file - memory object is cached even 
 *		after no reference:
 *		reference count > 0 -   )
 *
 * PARAMETERS:	cd	- pointer to commit data structure.
 *			  current inode is the one to truncate.
 *
593
 * RETURN:	Errors from subroutines
Dave Kleikamp's avatar
Dave Kleikamp committed
594
 */
595
static s64 commitZeroLink(tid_t tid, struct inode *ip)
Dave Kleikamp's avatar
Dave Kleikamp committed
596
{
Dave Kleikamp's avatar
Dave Kleikamp committed
597
	int filetype;
598
	struct tblock *tblk;
Dave Kleikamp's avatar
Dave Kleikamp committed
599

600
	jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
Dave Kleikamp's avatar
Dave Kleikamp committed
601 602 603 604 605 606 607

	filetype = ip->i_mode & S_IFMT;
	switch (filetype) {
	case S_IFREG:
		break;
	case S_IFLNK:
		/* fast symbolic link */
608
		if (ip->i_size < IDATASIZE) {
Dave Kleikamp's avatar
Dave Kleikamp committed
609 610 611 612 613 614 615 616 617 618 619 620 621
			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);
Dave Kleikamp's avatar
Dave Kleikamp committed
622
	tblk->xflag |= COMMIT_PMAP;
Dave Kleikamp's avatar
Dave Kleikamp committed
623 624 625 626

	/*
	 * free EA
	 */
Dave Kleikamp's avatar
Dave Kleikamp committed
627
	if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
Dave Kleikamp's avatar
Dave Kleikamp committed
628 629 630 631 632 633
		/* acquire maplock on EA to be freed from block map */
		txEA(tid, ip, &JFS_IP(ip)->ea, NULL);

	/*
	 * free ACL
	 */
Dave Kleikamp's avatar
Dave Kleikamp committed
634
	if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
Dave Kleikamp's avatar
Dave Kleikamp committed
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
		/* 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):
	 * free xtree/data pages from cache if COMMIT_PWMAP, 
	 * 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;
}


/*
 * NAME:	freeZeroLink()
 *
 * FUNCTION:    for non-directory, called by iClose(),
 *		free resources of a file from cache and WORKING map 
 *		for a file previously committed with zero link count
 *		while associated with a pager object,
 *
 * PARAMETER:	ip	- pointer to inode of file.
 *
 * RETURN:	0 -ok
 */
int freeZeroLink(struct inode *ip)
{
	int rc = 0;
	int type;

668
	jfs_info("freeZeroLink: ip = 0x%p", ip);
Dave Kleikamp's avatar
Dave Kleikamp committed
669 670 671 672 673 674 675 676 677 678 679

	/* 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 */
680
		if (ip->i_size < IDATASIZE)
Dave Kleikamp's avatar
Dave Kleikamp committed
681 682 683 684 685 686 687 688 689 690
			return 0;
		break;
	default:
		return 0;
	}

	/*
	 * free EA
	 */
	if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
691 692
		s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
		int xlen = lengthDXD(&JFS_IP(ip)->ea);
693 694
		struct maplock maplock;	/* maplock for COMMIT_WMAP */
		struct pxd_lock *pxdlock;	/* maplock for COMMIT_WMAP */
Dave Kleikamp's avatar
Dave Kleikamp committed
695 696

		/* free EA pages from cache */
697
		invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
Dave Kleikamp's avatar
Dave Kleikamp committed
698 699 700

		/* free EA extent from working block map */
		maplock.index = 1;
701
		pxdlock = (struct pxd_lock *) & maplock;
Dave Kleikamp's avatar
Dave Kleikamp committed
702 703 704 705 706 707 708 709 710 711
		pxdlock->flag = mlckFREEPXD;
		PXDaddress(&pxdlock->pxd, xaddr);
		PXDlength(&pxdlock->pxd, xlen);
		txFreeMap(ip, pxdlock, 0, COMMIT_WMAP);
	}

	/*
	 * free ACL
	 */
	if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
712 713
		s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
		int xlen = lengthDXD(&JFS_IP(ip)->acl);
714 715
		struct maplock maplock;	/* maplock for COMMIT_WMAP */
		struct pxd_lock *pxdlock;	/* maplock for COMMIT_WMAP */
Dave Kleikamp's avatar
Dave Kleikamp committed
716

717
		invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
Dave Kleikamp's avatar
Dave Kleikamp committed
718 719 720

		/* free ACL extent from working block map */
		maplock.index = 1;
721
		pxdlock = (struct pxd_lock *) & maplock;
Dave Kleikamp's avatar
Dave Kleikamp committed
722 723 724 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
		pxdlock->flag = mlckFREEPXD;
		PXDaddress(&pxdlock->pxd, xaddr);
		PXDlength(&pxdlock->pxd, xlen);
		txFreeMap(ip, pxdlock, 0, 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)
		rc = xtTruncate(0, ip, 0, COMMIT_WMAP);

	return rc;
}

/*
 * NAME:	jfs_link(vp, dvp, name, crp)
 *
 * FUNCTION:	create a link to <vp> by the name = <name>
 *		in the parent directory <dvp>
 *
 * PARAMETER:	vp 	- target object
 *		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].
 */
763
static int jfs_link(struct dentry *old_dentry,
Dave Kleikamp's avatar
Dave Kleikamp committed
764 765 766 767 768 769
	     struct inode *dir, struct dentry *dentry)
{
	int rc;
	tid_t tid;
	struct inode *ip = old_dentry->d_inode;
	ino_t ino;
770 771
	struct component_name dname;
	struct btstack btstack;
Dave Kleikamp's avatar
Dave Kleikamp committed
772 773
	struct inode *iplist[2];

774 775
	jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
		 dentry->d_name.name);
Dave Kleikamp's avatar
Dave Kleikamp committed
776

777 778 779 780 781 782
	if (ip->i_nlink == JFS_LINK_MAX)
		return -EMLINK;

	if (ip->i_nlink == 0)
		return -ENOENT;

Dave Kleikamp's avatar
Dave Kleikamp committed
783 784
	tid = txBegin(ip->i_sb, 0);

Dave Kleikamp's avatar
Dave Kleikamp committed
785 786 787
	down(&JFS_IP(dir)->commit_sem);
	down(&JFS_IP(ip)->commit_sem);

Dave Kleikamp's avatar
Dave Kleikamp committed
788 789 790
	/*
	 * scan parent directory for entry/freespace
	 */
791
	if ((rc = get_UCSname(&dname, dentry)))
Dave Kleikamp's avatar
Dave Kleikamp committed
792 793 794
		goto out;

	if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
795
		goto free_dname;
Dave Kleikamp's avatar
Dave Kleikamp committed
796 797 798 799 800 801

	/*
	 * create entry for new link in parent directory
	 */
	ino = ip->i_ino;
	if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
802
		goto free_dname;
Dave Kleikamp's avatar
Dave Kleikamp committed
803 804 805 806 807 808 809 810 811 812 813

	/* update object inode */
	ip->i_nlink++;		/* for new link */
	ip->i_ctime = CURRENT_TIME;
	mark_inode_dirty(dir);
	atomic_inc(&ip->i_count);

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

814 815 816
	if (!rc)
		d_instantiate(dentry, ip);

817 818 819
      free_dname:
	free_UCSname(&dname);

Dave Kleikamp's avatar
Dave Kleikamp committed
820 821 822
      out:
	txEnd(tid);

Dave Kleikamp's avatar
Dave Kleikamp committed
823 824 825
	up(&JFS_IP(dir)->commit_sem);
	up(&JFS_IP(ip)->commit_sem);

826
	jfs_info("jfs_link: rc:%d", rc);
827
	return rc;
Dave Kleikamp's avatar
Dave Kleikamp committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
}

/*
 * NAME:	jfs_symlink(dip, dentry, name)
 *
 * FUNCTION:	creates a symbolic link to <symlink> by name <name>
 *		        in directory <dip>
 *
 * 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
 *
 * RETURN:	errors from subroutines
 *
 * note:
 * ENAMETOOLONG: pathname resolution of a symbolic link produced
 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
*/

848 849
static int jfs_symlink(struct inode *dip, struct dentry *dentry,
		const char *name)
Dave Kleikamp's avatar
Dave Kleikamp committed
850 851 852 853
{
	int rc;
	tid_t tid;
	ino_t ino = 0;
854
	struct component_name dname;
Dave Kleikamp's avatar
Dave Kleikamp committed
855
	int ssize;		/* source pathname size */
856
	struct btstack btstack;
Dave Kleikamp's avatar
Dave Kleikamp committed
857 858 859 860
	struct inode *ip = dentry->d_inode;
	unchar *i_fastsymlink;
	s64 xlen = 0;
	int bmask = 0, xsize;
861
	s64 extent = 0, xaddr;
862
	struct metapage *mp;
Dave Kleikamp's avatar
Dave Kleikamp committed
863
	struct super_block *sb;
864
	struct tblock *tblk;
Dave Kleikamp's avatar
Dave Kleikamp committed
865 866 867

	struct inode *iplist[2];

868
	jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
Dave Kleikamp's avatar
Dave Kleikamp committed
869 870 871 872 873 874 875 876

	ssize = strlen(name) + 1;

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

877
	if ((rc = get_UCSname(&dname, dentry)))
Dave Kleikamp's avatar
Dave Kleikamp committed
878 879 880 881 882 883 884 885
		goto out1;

	/*
	 * allocate on-disk/in-memory inode for symbolic link:
	 * (iAlloc() returns new, locked inode)
	 */
	ip = ialloc(dip, S_IFLNK | 0777);
	if (ip == NULL) {
886
		rc = -ENOSPC;
Dave Kleikamp's avatar
Dave Kleikamp committed
887 888 889
		goto out2;
	}

Dave Kleikamp's avatar
Dave Kleikamp committed
890 891 892 893 894
	tid = txBegin(dip->i_sb, 0);

	down(&JFS_IP(dip)->commit_sem);
	down(&JFS_IP(ip)->commit_sem);

Dave Kleikamp's avatar
Dave Kleikamp committed
895 896
	tblk = tid_to_tblock(tid);
	tblk->xflag |= COMMIT_CREATE;
897 898
	tblk->ino = ip->i_ino;
	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
Dave Kleikamp's avatar
Dave Kleikamp committed
899 900 901 902 903 904 905 906

	/* fix symlink access permission
	 * (dir_create() ANDs in the u.u_cmask, 
	 * but symlinks really need to be 777 access)
	 */
	ip->i_mode |= 0777;

	/*
907
	 * write symbolic link target path name
Dave Kleikamp's avatar
Dave Kleikamp committed
908 909 910 911 912 913 914 915 916 917 918 919 920
	 */
	xtInitRoot(tid, ip);

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

	if (ssize <= IDATASIZE) {
		ip->i_op = &jfs_symlink_inode_operations;

		i_fastsymlink = JFS_IP(ip)->i_inline;
		memcpy(i_fastsymlink, name, ssize);
		ip->i_size = ssize - 1;
Dave Kleikamp's avatar
Dave Kleikamp committed
921 922 923 924 925 926 927 928

		/*
		 * 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;

929 930
		jfs_info("jfs_symlink: fast symlink added  ssize:%d name:%s ",
			 ssize, name);
Dave Kleikamp's avatar
Dave Kleikamp committed
931 932 933 934 935
	}
	/*
	 * write source path name in a single extent
	 */
	else {
936
		jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
Dave Kleikamp's avatar
Dave Kleikamp committed
937 938 939 940 941 942 943 944 945 946 947 948 949 950

		ip->i_op = &page_symlink_inode_operations;
		ip->i_mapping->a_ops = &jfs_aops;

		/*
		 * even though the data of symlink object (source 
		 * 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;
951 952
		if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
			txAbort(tid, 0);
953
			rc = -ENOSPC;
Dave Kleikamp's avatar
Dave Kleikamp committed
954
			goto out3;
Dave Kleikamp's avatar
Dave Kleikamp committed
955
		}
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
		extent = xaddr;
		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) {
				dbFree(ip, extent, xlen);
				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;
		}
		ip->i_blocks = LBLK2PBLK(sb, xlen);
	}

	/*
	 * 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)
			dbFree(ip, extent, xlen);
		txAbort(tid, 0);
		/* discard new inode */
		goto out3;
Dave Kleikamp's avatar
Dave Kleikamp committed
993 994 995 996 997 998 999 1000 1001 1002
	}

	insert_inode_hash(ip);
	mark_inode_dirty(ip);

	/*
	 * commit update of parent directory and link object
	 */

	iplist[0] = dip;
1003 1004
	iplist[1] = ip;
	rc = txCommit(tid, 2, &iplist[0], 0);
Dave Kleikamp's avatar
Dave Kleikamp committed
1005

Dave Kleikamp's avatar
Dave Kleikamp committed
1006 1007 1008 1009 1010 1011 1012
      out3:
	txEnd(tid);
	up(&JFS_IP(dip)->commit_sem);
	up(&JFS_IP(ip)->commit_sem);
	if (rc) {
		ip->i_nlink = 0;
		iput(ip);
1013 1014
	} else
		d_instantiate(dentry, ip);
Dave Kleikamp's avatar
Dave Kleikamp committed
1015

Dave Kleikamp's avatar
Dave Kleikamp committed
1016
      out2:
Dave Kleikamp's avatar
Dave Kleikamp committed
1017 1018
	free_UCSname(&dname);

Dave Kleikamp's avatar
Dave Kleikamp committed
1019 1020 1021 1022 1023
#ifdef CONFIG_JFS_POSIX_ACL
	if (rc == 0)
		jfs_init_acl(ip, dip);
#endif

Dave Kleikamp's avatar
Dave Kleikamp committed
1024
      out1:
1025 1026
	jfs_info("jfs_symlink: rc:%d", rc);
	return rc;
Dave Kleikamp's avatar
Dave Kleikamp committed
1027 1028 1029 1030 1031 1032 1033 1034
}


/*
 * NAME:        jfs_rename
 *
 * FUNCTION:    rename a file or directory
 */
1035
static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dave Kleikamp's avatar
Dave Kleikamp committed
1036 1037
	       struct inode *new_dir, struct dentry *new_dentry)
{
1038
	struct btstack btstack;
Dave Kleikamp's avatar
Dave Kleikamp committed
1039
	ino_t ino;
1040
	struct component_name new_dname;
Dave Kleikamp's avatar
Dave Kleikamp committed
1041
	struct inode *new_ip;
1042
	struct component_name old_dname;
Dave Kleikamp's avatar
Dave Kleikamp committed
1043 1044 1045
	struct inode *old_ip;
	int rc;
	tid_t tid;
1046 1047 1048
	struct tlock *tlck;
	struct dt_lock *dtlck;
	struct lv *lv;
Dave Kleikamp's avatar
Dave Kleikamp committed
1049 1050
	int ipcount;
	struct inode *iplist[4];
1051
	struct tblock *tblk;
Dave Kleikamp's avatar
Dave Kleikamp committed
1052 1053 1054 1055
	s64 new_size = 0;
	int commit_flag;


1056 1057
	jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
		 new_dentry->d_name.name);
Dave Kleikamp's avatar
Dave Kleikamp committed
1058 1059 1060 1061

	old_ip = old_dentry->d_inode;
	new_ip = new_dentry->d_inode;

1062
	if ((rc = get_UCSname(&old_dname, old_dentry)))
Dave Kleikamp's avatar
Dave Kleikamp committed
1063 1064
		goto out1;

1065
	if ((rc = get_UCSname(&new_dname, new_dentry)))
Dave Kleikamp's avatar
Dave Kleikamp committed
1066 1067 1068 1069 1070 1071 1072
		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)) {
1073
		rc = -ENOENT;
Dave Kleikamp's avatar
Dave Kleikamp committed
1074 1075 1076 1077 1078 1079 1080 1081 1082
		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);
	if (rc == 0) {
		if ((new_ip == 0) || (ino != new_ip->i_ino)) {
1083
			rc = -ESTALE;
Dave Kleikamp's avatar
Dave Kleikamp committed
1084 1085
			goto out3;
		}
1086
	} else if (rc != -ENOENT)
Dave Kleikamp's avatar
Dave Kleikamp committed
1087 1088 1089
		goto out3;
	else if (new_ip) {
		/* no entry exists, but one was expected */
1090
		rc = -ESTALE;
Dave Kleikamp's avatar
Dave Kleikamp committed
1091 1092 1093 1094 1095 1096
		goto out3;
	}

	if (S_ISDIR(old_ip->i_mode)) {
		if (new_ip) {
			if (!dtEmpty(new_ip)) {
1097
				rc = -ENOTEMPTY;
Dave Kleikamp's avatar
Dave Kleikamp committed
1098 1099 1100 1101
				goto out3;
			}
		} else if ((new_dir != old_dir) &&
			   (new_dir->i_nlink == JFS_LINK_MAX)) {
1102
			rc = -EMLINK;
Dave Kleikamp's avatar
Dave Kleikamp committed
1103 1104
			goto out3;
		}
Dave Kleikamp's avatar
Dave Kleikamp committed
1105 1106
	} else if (new_ip)
		IWRITE_LOCK(new_ip);
Dave Kleikamp's avatar
Dave Kleikamp committed
1107 1108 1109 1110 1111 1112

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

Dave Kleikamp's avatar
Dave Kleikamp committed
1113 1114 1115 1116 1117
	down(&JFS_IP(new_dir)->commit_sem);
	down(&JFS_IP(old_ip)->commit_sem);
	if (old_dir != new_dir)
		down(&JFS_IP(old_dir)->commit_sem);

Dave Kleikamp's avatar
Dave Kleikamp committed
1118
	if (new_ip) {
Dave Kleikamp's avatar
Dave Kleikamp committed
1119
		down(&JFS_IP(new_ip)->commit_sem);
Dave Kleikamp's avatar
Dave Kleikamp committed
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
		/*
		 * 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)
			goto out4;
		new_ip->i_nlink--;
		if (S_ISDIR(new_ip->i_mode)) {
			new_ip->i_nlink--;
Dave Kleikamp's avatar
Dave Kleikamp committed
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
			if (new_ip->i_nlink) {
				up(&JFS_IP(new_dir)->commit_sem);
				up(&JFS_IP(old_ip)->commit_sem);
				if (old_dir != new_dir)
					up(&JFS_IP(old_dir)->commit_sem);
				if (!S_ISDIR(old_ip->i_mode) && new_ip)
					IWRITE_UNLOCK(new_ip);
				jfs_error(new_ip->i_sb,
					  "jfs_rename: new_ip->i_nlink != 0");
				return -EIO;
			}
Dave Kleikamp's avatar
Dave Kleikamp committed
1142 1143
			tblk = tid_to_tblock(tid);
			tblk->xflag |= COMMIT_DELETE;
1144
			tblk->u.ip = new_ip;
Dave Kleikamp's avatar
Dave Kleikamp committed
1145 1146 1147 1148 1149
		} 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 */
1150
				rc = new_size;		
Dave Kleikamp's avatar
Dave Kleikamp committed
1151 1152 1153 1154
				goto out4;
			}
			tblk = tid_to_tblock(tid);
			tblk->xflag |= COMMIT_DELETE;
1155
			tblk->u.ip = new_ip;
Dave Kleikamp's avatar
Dave Kleikamp committed
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
		} 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) {
1167 1168
			jfs_err("jfs_rename didn't expect dtSearch to fail "
				"w/rc = %d", rc);
Dave Kleikamp's avatar
Dave Kleikamp committed
1169 1170 1171 1172 1173 1174
			goto out4;
		}

		ino = old_ip->i_ino;
		rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
		if (rc) {
1175 1176
			jfs_err("jfs_rename: dtInsert failed w/rc = %d",
				rc);
Dave Kleikamp's avatar
Dave Kleikamp committed
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
			goto out4;
		}
		if (S_ISDIR(old_ip->i_mode))
			new_dir->i_nlink++;
	}
	/*
	 * Remove old directory entry
	 */

	ino = old_ip->i_ino;
	rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
	if (rc) {
1189 1190
		jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
			rc);
Dave Kleikamp's avatar
Dave Kleikamp committed
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
		txAbort(tid, 1);	/* Marks Filesystem dirty */
		goto out4;
	}
	if (S_ISDIR(old_ip->i_mode)) {
		old_dir->i_nlink--;
		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,
1206
				    (struct metapage *) &JFS_IP(old_ip)->bxflag,
1207
				      tlckDTREE | tlckBTROOT | tlckRELINK);
1208
			dtlck = (struct dt_lock *) & tlck->lock;
Dave Kleikamp's avatar
Dave Kleikamp committed
1209
			ASSERT(dtlck->index == 0);
1210
			lv = & dtlck->lv[0];
Dave Kleikamp's avatar
Dave Kleikamp committed
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
			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);

1223
	new_dir->i_ctime = new_dir->i_mtime = CURRENT_TIME;
Dave Kleikamp's avatar
Dave Kleikamp committed
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
	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;
1235
		old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
Dave Kleikamp's avatar
Dave Kleikamp committed
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
		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);

      out4:
	txEnd(tid);

Dave Kleikamp's avatar
Dave Kleikamp committed
1254 1255 1256 1257 1258 1259 1260
	up(&JFS_IP(new_dir)->commit_sem);
	up(&JFS_IP(old_ip)->commit_sem);
	if (old_dir != new_dir)
		up(&JFS_IP(old_dir)->commit_sem);
	if (new_ip)
		up(&JFS_IP(new_ip)->commit_sem);

Dave Kleikamp's avatar
Dave Kleikamp committed
1261 1262
	while (new_size && (rc == 0)) {
		tid = txBegin(new_ip->i_sb, 0);
Dave Kleikamp's avatar
Dave Kleikamp committed
1263
		down(&JFS_IP(new_ip)->commit_sem);
Dave Kleikamp's avatar
Dave Kleikamp committed
1264 1265 1266
		new_size = xtTruncate_pmap(tid, new_ip, new_size);
		if (new_size < 0) {
			txAbort(tid, 1);
1267
			rc = new_size;		
Dave Kleikamp's avatar
Dave Kleikamp committed
1268 1269 1270
		} else
			rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
		txEnd(tid);
Dave Kleikamp's avatar
Dave Kleikamp committed
1271
		up(&JFS_IP(new_ip)->commit_sem);
Dave Kleikamp's avatar
Dave Kleikamp committed
1272
	}
1273 1274
	if (new_ip && (new_ip->i_nlink == 0))
		set_cflag(COMMIT_Nolink, new_ip);
Dave Kleikamp's avatar
Dave Kleikamp committed
1275 1276 1277 1278 1279
      out3:
	free_UCSname(&new_dname);
      out2:
	free_UCSname(&old_dname);
      out1:
Dave Kleikamp's avatar
Dave Kleikamp committed
1280
	if (new_ip && !S_ISDIR(new_ip->i_mode))
Dave Kleikamp's avatar
Dave Kleikamp committed
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
		IWRITE_UNLOCK(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);
	}

1293
	jfs_info("jfs_rename: returning %d", rc);
1294
	return rc;
Dave Kleikamp's avatar
Dave Kleikamp committed
1295 1296 1297 1298 1299 1300 1301 1302
}


/*
 * NAME:        jfs_mknod
 *
 * FUNCTION:    Create a special file (device)
 */
1303 1304
static int jfs_mknod(struct inode *dir, struct dentry *dentry,
		int mode, dev_t rdev)
Dave Kleikamp's avatar
Dave Kleikamp committed
1305
{
1306
	struct jfs_inode_info *jfs_ip;
1307 1308
	struct btstack btstack;
	struct component_name dname;
Dave Kleikamp's avatar
Dave Kleikamp committed
1309 1310 1311 1312 1313
	ino_t ino;
	struct inode *ip;
	struct inode *iplist[2];
	int rc;
	tid_t tid;
1314
	struct tblock *tblk;
Dave Kleikamp's avatar
Dave Kleikamp committed
1315

1316
	if (!new_valid_dev(rdev))
1317 1318
		return -EINVAL;

1319
	jfs_info("jfs_mknod: %s", dentry->d_name.name);
Dave Kleikamp's avatar
Dave Kleikamp committed
1320

1321
	if ((rc = get_UCSname(&dname, dentry)))
Dave Kleikamp's avatar
Dave Kleikamp committed
1322 1323 1324 1325
		goto out;

	ip = ialloc(dir, mode);
	if (ip == NULL) {
1326
		rc = -ENOSPC;
Dave Kleikamp's avatar
Dave Kleikamp committed
1327 1328
		goto out1;
	}
1329
	jfs_ip = JFS_IP(ip);
Dave Kleikamp's avatar
Dave Kleikamp committed
1330 1331 1332

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

Dave Kleikamp's avatar
Dave Kleikamp committed
1333 1334 1335 1336 1337
	down(&JFS_IP(dir)->commit_sem);
	down(&JFS_IP(ip)->commit_sem);

	if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
		goto out3;
Dave Kleikamp's avatar
Dave Kleikamp committed
1338 1339 1340

	tblk = tid_to_tblock(tid);
	tblk->xflag |= COMMIT_CREATE;
1341 1342
	tblk->ino = ip->i_ino;
	tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
Dave Kleikamp's avatar
Dave Kleikamp committed
1343 1344

	ino = ip->i_ino;
Dave Kleikamp's avatar
Dave Kleikamp committed
1345 1346
	if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
		goto out3;
Dave Kleikamp's avatar
Dave Kleikamp committed
1347

1348
	ip->i_op = &jfs_file_inode_operations;
1349
	jfs_ip->dev = new_encode_dev(rdev);
1350
	init_special_inode(ip, ip->i_mode, rdev);
Dave Kleikamp's avatar
Dave Kleikamp committed
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361

	insert_inode_hash(ip);
	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);
Dave Kleikamp's avatar
Dave Kleikamp committed
1362 1363

      out3:
Dave Kleikamp's avatar
Dave Kleikamp committed
1364
	txEnd(tid);
Dave Kleikamp's avatar
Dave Kleikamp committed
1365 1366 1367 1368 1369
	up(&JFS_IP(ip)->commit_sem);
	up(&JFS_IP(dir)->commit_sem);
	if (rc) {
		ip->i_nlink = 0;
		iput(ip);
1370 1371
	} else
		d_instantiate(dentry, ip);
Dave Kleikamp's avatar
Dave Kleikamp committed
1372 1373 1374 1375

      out1:
	free_UCSname(&dname);

Dave Kleikamp's avatar
Dave Kleikamp committed
1376 1377 1378 1379 1380
#ifdef CONFIG_JFS_POSIX_ACL
	if (rc == 0)
		jfs_init_acl(ip, dir);
#endif

Dave Kleikamp's avatar
Dave Kleikamp committed
1381
      out:
1382
	jfs_info("jfs_mknod: returning %d", rc);
1383
	return rc;
Dave Kleikamp's avatar
Dave Kleikamp committed
1384 1385
}

1386
static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
Dave Kleikamp's avatar
Dave Kleikamp committed
1387
{
1388
	struct btstack btstack;
Dave Kleikamp's avatar
Dave Kleikamp committed
1389 1390
	ino_t inum;
	struct inode *ip;
1391
	struct component_name key;
Dave Kleikamp's avatar
Dave Kleikamp committed
1392 1393 1394 1395
	const char *name = dentry->d_name.name;
	int len = dentry->d_name.len;
	int rc;

1396
	jfs_info("jfs_lookup: name = %s", name);
Dave Kleikamp's avatar
Dave Kleikamp committed
1397 1398 1399 1400 1401 1402 1403


	if ((name[0] == '.') && (len == 1))
		inum = dip->i_ino;
	else if (strcmp(name, "..") == 0)
		inum = PARENT(dip);
	else {
1404
		if ((rc = get_UCSname(&key, dentry)))
1405
			return ERR_PTR(rc);
Dave Kleikamp's avatar
Dave Kleikamp committed
1406 1407
		rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
		free_UCSname(&key);
1408
		if (rc == -ENOENT) {
Dave Kleikamp's avatar
Dave Kleikamp committed
1409 1410 1411
			d_add(dentry, NULL);
			return ERR_PTR(0);
		} else if (rc) {
1412
			jfs_err("jfs_lookup: dtSearch returned %d", rc);
1413
			return ERR_PTR(rc);
Dave Kleikamp's avatar
Dave Kleikamp committed
1414 1415 1416
		}
	}

1417 1418
	ip = iget(dip->i_sb, inum);
	if (ip == NULL || is_bad_inode(ip)) {
1419
		jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1420 1421
		if (ip)
			iput(ip);
Dave Kleikamp's avatar
Dave Kleikamp committed
1422 1423 1424
		return ERR_PTR(-EACCES);
	}

Dave Kleikamp's avatar
Dave Kleikamp committed
1425 1426 1427 1428 1429 1430
	return d_splice_alias(ip, dentry);
}

struct dentry *jfs_get_parent(struct dentry *dentry)
{
	struct super_block *sb = dentry->d_inode->i_sb;
1431
	struct dentry *parent = ERR_PTR(-ENOENT);
Dave Kleikamp's avatar
Dave Kleikamp committed
1432
	struct inode *inode;
1433
	unsigned long parent_ino;
Dave Kleikamp's avatar
Dave Kleikamp committed
1434

1435 1436 1437
	parent_ino =
		le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
	inode = iget(sb, parent_ino);
Dave Kleikamp's avatar
Dave Kleikamp committed
1438
	if (inode) {
1439
		if (is_bad_inode(inode)) {
Dave Kleikamp's avatar
Dave Kleikamp committed
1440
			iput(inode);
1441 1442
			parent = ERR_PTR(-EACCES);
		} else {
1443 1444 1445 1446 1447
			parent = d_alloc_anon(inode);
			if (!parent) {
				parent = ERR_PTR(-ENOMEM);
				iput(inode);
			}
Dave Kleikamp's avatar
Dave Kleikamp committed
1448 1449
		}
	}
Dave Kleikamp's avatar
Dave Kleikamp committed
1450

Dave Kleikamp's avatar
Dave Kleikamp committed
1451
	return parent;
Dave Kleikamp's avatar
Dave Kleikamp committed
1452 1453 1454
}

struct inode_operations jfs_dir_inode_operations = {
1455 1456 1457 1458 1459 1460 1461 1462 1463
	.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,
Dave Kleikamp's avatar
Dave Kleikamp committed
1464 1465 1466 1467
	.setxattr	= jfs_setxattr,
	.getxattr	= jfs_getxattr,
	.listxattr	= jfs_listxattr,
	.removexattr	= jfs_removexattr,
Dave Kleikamp's avatar
Dave Kleikamp committed
1468 1469 1470 1471
#ifdef CONFIG_JFS_POSIX_ACL
	.setattr	= jfs_setattr,
	.permission	= jfs_permission,
#endif
Dave Kleikamp's avatar
Dave Kleikamp committed
1472 1473 1474
};

struct file_operations jfs_dir_operations = {
1475 1476 1477
	.read		= generic_read_dir,
	.readdir	= jfs_readdir,
	.fsync		= jfs_fsync,
Dave Kleikamp's avatar
Dave Kleikamp committed
1478
};