namei.c 43.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
 *
 * Trivial changes by Alan Cox to remove EHASHCOLLISION for compatibility
 *
 * Trivial Changes:
 * Rights granted to Hans Reiser to redistribute under other terms providing
 * he accepts all liability including but not limited to patent, fitness
 * for purpose, and direct or indirect claims arising from failure to perform.
 *
 * NO WARRANTY
 */

#include <linux/time.h>
#include <linux/bitops.h>
16
#include <linux/slab.h>
17
#include "reiserfs.h"
18
#include "acl.h"
19
#include "xattr.h"
Linus Torvalds's avatar
Linus Torvalds committed
20 21
#include <linux/quotaops.h>

22
#define INC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) { inc_nlink(i); if (i->i_nlink >= REISERFS_LINK_MAX) set_nlink(i, 1); }
23
#define DEC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) drop_nlink(i);
Linus Torvalds's avatar
Linus Torvalds committed
24

25 26 27 28
/*
 * directory item contains array of entry headers. This performs
 * binary search through that array
 */
29
static int bin_search_in_dir_item(struct reiserfs_dir_entry *de, loff_t off)
Linus Torvalds's avatar
Linus Torvalds committed
30
{
31 32 33 34 35
	struct item_head *ih = de->de_ih;
	struct reiserfs_de_head *deh = de->de_deh;
	int rbound, lbound, j;

	lbound = 0;
36
	rbound = ih_entry_count(ih) - 1;
37 38 39 40 41 42 43 44 45 46 47

	for (j = (rbound + lbound) / 2; lbound <= rbound;
	     j = (rbound + lbound) / 2) {
		if (off < deh_offset(deh + j)) {
			rbound = j - 1;
			continue;
		}
		if (off > deh_offset(deh + j)) {
			lbound = j + 1;
			continue;
		}
48
		/* this is not name found, but matched third key component */
49 50
		de->de_entry_num = j;
		return NAME_FOUND;
Linus Torvalds's avatar
Linus Torvalds committed
51 52
	}

53 54
	de->de_entry_num = lbound;
	return NAME_NOT_FOUND;
Linus Torvalds's avatar
Linus Torvalds committed
55 56
}

57 58 59
/*
 * comment?  maybe something like set de to point to what the path points to?
 */
60
static inline void set_de_item_location(struct reiserfs_dir_entry *de,
61
					struct treepath *path)
Linus Torvalds's avatar
Linus Torvalds committed
62
{
63
	de->de_bh = get_last_bh(path);
64
	de->de_ih = tp_item_head(path);
65 66 67
	de->de_deh = B_I_DEH(de->de_bh, de->de_ih);
	de->de_item_num = PATH_LAST_POSITION(path);
}
Linus Torvalds's avatar
Linus Torvalds committed
68

69 70 71
/*
 * de_bh, de_ih, de_deh (points to first element of array), de_item_num is set
 */
72
inline void set_de_name_and_namelen(struct reiserfs_dir_entry *de)
Linus Torvalds's avatar
Linus Torvalds committed
73
{
74
	struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
Linus Torvalds's avatar
Linus Torvalds committed
75

76
	BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
Linus Torvalds's avatar
Linus Torvalds committed
77

78 79
	de->de_entrylen = entry_length(de->de_bh, de->de_ih, de->de_entry_num);
	de->de_namelen = de->de_entrylen - (de_with_sd(deh) ? SD_SIZE : 0);
80
	de->de_name = ih_item_body(de->de_bh, de->de_ih) + deh_location(deh);
81 82
	if (de->de_name[de->de_namelen - 1] == 0)
		de->de_namelen = strlen(de->de_name);
Linus Torvalds's avatar
Linus Torvalds committed
83 84
}

85
/* what entry points to */
86
static inline void set_de_object_key(struct reiserfs_dir_entry *de)
Linus Torvalds's avatar
Linus Torvalds committed
87
{
88
	BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
89 90
	de->de_dir_id = deh_dir_id(&de->de_deh[de->de_entry_num]);
	de->de_objectid = deh_objectid(&de->de_deh[de->de_entry_num]);
Linus Torvalds's avatar
Linus Torvalds committed
91 92
}

93
static inline void store_de_entry_key(struct reiserfs_dir_entry *de)
Linus Torvalds's avatar
Linus Torvalds committed
94
{
95 96
	struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;

97
	BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
98 99 100 101 102 103 104

	/* store key of the found entry */
	de->de_entry_key.version = KEY_FORMAT_3_5;
	de->de_entry_key.on_disk_key.k_dir_id =
	    le32_to_cpu(de->de_ih->ih_key.k_dir_id);
	de->de_entry_key.on_disk_key.k_objectid =
	    le32_to_cpu(de->de_ih->ih_key.k_objectid);
105 106
	set_cpu_key_k_offset(&de->de_entry_key, deh_offset(deh));
	set_cpu_key_k_type(&de->de_entry_key, TYPE_DIRENTRY);
Linus Torvalds's avatar
Linus Torvalds committed
107 108
}

109 110 111 112 113 114 115 116 117 118
/*
 * We assign a key to each directory item, and place multiple entries in a
 * single directory item.  A directory item has a key equal to the key of
 * the first directory entry in it.

 * This function first calls search_by_key, then, if item whose first entry
 * matches is not found it looks for the entry inside directory item found
 * by search_by_key. Fills the path to the entry, and to the entry position
 * in the item
 */
Linus Torvalds's avatar
Linus Torvalds committed
119
/* The function is NOT SCHEDULE-SAFE! */
120
int search_by_entry_key(struct super_block *sb, const struct cpu_key *key,
121
			struct treepath *path, struct reiserfs_dir_entry *de)
Linus Torvalds's avatar
Linus Torvalds committed
122
{
123 124 125 126 127 128
	int retval;

	retval = search_item(sb, key, path);
	switch (retval) {
	case ITEM_NOT_FOUND:
		if (!PATH_LAST_POSITION(path)) {
129 130
			reiserfs_error(sb, "vs-7000", "search_by_key "
				       "returned item position == 0");
131 132 133 134 135 136 137 138 139 140 141 142 143
			pathrelse(path);
			return IO_ERROR;
		}
		PATH_LAST_POSITION(path)--;

	case ITEM_FOUND:
		break;

	case IO_ERROR:
		return retval;

	default:
		pathrelse(path);
144
		reiserfs_error(sb, "vs-7002", "no path to here");
145
		return IO_ERROR;
Linus Torvalds's avatar
Linus Torvalds committed
146 147
	}

148
	set_de_item_location(de, path);
Linus Torvalds's avatar
Linus Torvalds committed
149 150

#ifdef CONFIG_REISERFS_CHECK
151
	if (!is_direntry_le_ih(de->de_ih) ||
152
	    COMP_SHORT_KEYS(&de->de_ih->ih_key, key)) {
153
		print_block(de->de_bh, 0, -1, -1);
154 155 156
		reiserfs_panic(sb, "vs-7005", "found item %h is not directory "
			       "item or does not belong to the same directory "
			       "as key %K", de->de_ih, key);
157 158 159
	}
#endif				/* CONFIG_REISERFS_CHECK */

160 161 162 163
	/*
	 * binary search in directory item by third component of the
	 * key. sets de->de_entry_num of de
	 */
164 165 166
	retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
	path->pos_in_item = de->de_entry_num;
	if (retval != NAME_NOT_FOUND) {
167 168 169 170
		/*
		 * ugly, but rename needs de_bh, de_deh, de_name,
		 * de_namelen, de_objectid set
		 */
171 172 173 174
		set_de_name_and_namelen(de);
		set_de_object_key(de);
	}
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
175 176 177 178
}

/* Keyed 32-bit hash function using TEA in a Davis-Meyer function */

179 180 181 182 183 184
/*
 * The third component is hashed, and you can choose from more than
 * one hash function.  Per directory hashes are not yet implemented
 * but are thought about. This function should be moved to hashes.c
 * Jedi, please do so.  -Hans
 */
185 186
static __u32 get_third_component(struct super_block *s,
				 const char *name, int len)
Linus Torvalds's avatar
Linus Torvalds committed
187
{
188 189 190 191 192 193 194 195 196
	__u32 res;

	if (!len || (len == 1 && name[0] == '.'))
		return DOT_OFFSET;
	if (len == 2 && name[0] == '.' && name[1] == '.')
		return DOT_DOT_OFFSET;

	res = REISERFS_SB(s)->s_hash_function(name, len);

197
	/* take bits from 7-th to 30-th including both bounds */
198 199
	res = GET_HASH_VALUE(res);
	if (res == 0)
200 201 202 203
		/*
		 * needed to have no names before "." and ".." those have hash
		 * value == 0 and generation conters 1 and 2 accordingly
		 */
204 205
		res = 128;
	return res + MAX_GENERATION_NUMBER;
Linus Torvalds's avatar
Linus Torvalds committed
206 207
}

208 209
static int reiserfs_match(struct reiserfs_dir_entry *de,
			  const char *name, int namelen)
Linus Torvalds's avatar
Linus Torvalds committed
210
{
211
	int retval = NAME_NOT_FOUND;
Linus Torvalds's avatar
Linus Torvalds committed
212

213 214 215 216 217
	if ((namelen == de->de_namelen) &&
	    !memcmp(de->de_name, name, de->de_namelen))
		retval =
		    (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
		     NAME_FOUND_INVISIBLE);
Linus Torvalds's avatar
Linus Torvalds committed
218

219
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
220 221 222 223
}

/* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */

224
/* used when hash collisions exist */
Linus Torvalds's avatar
Linus Torvalds committed
225

226 227 228
static int linear_search_in_dir_item(struct cpu_key *key,
				     struct reiserfs_dir_entry *de,
				     const char *name, int namelen)
Linus Torvalds's avatar
Linus Torvalds committed
229
{
230 231 232
	struct reiserfs_de_head *deh = de->de_deh;
	int retval;
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
233

234
	i = de->de_entry_num;
Linus Torvalds's avatar
Linus Torvalds committed
235

236
	if (i == ih_entry_count(de->de_ih) ||
237 238 239 240
	    GET_HASH_VALUE(deh_offset(deh + i)) !=
	    GET_HASH_VALUE(cpu_key_k_offset(key))) {
		i--;
	}
Linus Torvalds's avatar
Linus Torvalds committed
241

242 243
	RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
	       "vs-7010: array of entry headers not found");
Linus Torvalds's avatar
Linus Torvalds committed
244

245
	deh += i;
Linus Torvalds's avatar
Linus Torvalds committed
246

247
	for (; i >= 0; i--, deh--) {
248
		/* hash value does not match, no need to check whole name */
249 250 251 252 253
		if (GET_HASH_VALUE(deh_offset(deh)) !=
		    GET_HASH_VALUE(cpu_key_k_offset(key))) {
			return NAME_NOT_FOUND;
		}

254
		/* mark that this generation number is used */
255 256
		if (de->de_gen_number_bit_string)
			set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
257
				de->de_gen_number_bit_string);
Linus Torvalds's avatar
Linus Torvalds committed
258

259
		/* calculate pointer to name and namelen */
260 261
		de->de_entry_num = i;
		set_de_name_and_namelen(de);
Linus Torvalds's avatar
Linus Torvalds committed
262

263 264 265 266
		/*
		 * de's de_name, de_namelen, de_recordlen are set.
		 * Fill the rest.
		 */
267 268
		if ((retval =
		     reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
Linus Torvalds's avatar
Linus Torvalds committed
269

270
			/* key of pointed object */
271
			set_de_object_key(de);
Linus Torvalds's avatar
Linus Torvalds committed
272

273
			store_de_entry_key(de);
Linus Torvalds's avatar
Linus Torvalds committed
274

275
			/* retval can be NAME_FOUND or NAME_FOUND_INVISIBLE */
276 277
			return retval;
		}
Linus Torvalds's avatar
Linus Torvalds committed
278 279
	}

280
	if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
281 282 283 284 285 286 287 288 289 290 291
		/*
		 * we have reached left most entry in the node. In common we
		 * have to go to the left neighbor, but if generation counter
		 * is 0 already, we know for sure, that there is no name with
		 * the same hash value
		 */
		/*
		 * FIXME: this work correctly only because hash value can not
		 *  be 0. Btw, in case of Yura's hash it is probably possible,
		 * so, this is a bug
		 */
292
		return NAME_NOT_FOUND;
Linus Torvalds's avatar
Linus Torvalds committed
293

294 295
	RFALSE(de->de_item_num,
	       "vs-7015: two diritems of the same directory in one node?");
Linus Torvalds's avatar
Linus Torvalds committed
296

297 298
	return GOTO_PREVIOUS_ITEM;
}
Linus Torvalds's avatar
Linus Torvalds committed
299

300 301 302 303
/*
 * may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
 * FIXME: should add something like IOERROR
 */
304
static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
305
			       struct treepath *path_to_entry,
306
			       struct reiserfs_dir_entry *de)
Linus Torvalds's avatar
Linus Torvalds committed
307
{
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
	struct cpu_key key_to_search;
	int retval;

	if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
		return NAME_NOT_FOUND;

	/* we will search for this key in the tree */
	make_cpu_key(&key_to_search, dir,
		     get_third_component(dir->i_sb, name, namelen),
		     TYPE_DIRENTRY, 3);

	while (1) {
		retval =
		    search_by_entry_key(dir->i_sb, &key_to_search,
					path_to_entry, de);
		if (retval == IO_ERROR) {
324
			reiserfs_error(dir->i_sb, "zam-7001", "io error");
325 326 327 328 329 330 331
			return IO_ERROR;
		}

		/* compare names for all entries having given hash value */
		retval =
		    linear_search_in_dir_item(&key_to_search, de, name,
					      namelen);
332 333 334 335
		/*
		 * there is no need to scan directory anymore.
		 * Given entry found or does not exist
		 */
336 337 338 339 340
		if (retval != GOTO_PREVIOUS_ITEM) {
			path_to_entry->pos_in_item = de->de_entry_num;
			return retval;
		}

341 342 343 344
		/*
		 * there is left neighboring item of this directory
		 * and given entry can be there
		 */
345 346 347 348 349
		set_cpu_key_k_offset(&key_to_search,
				     le_ih_k_offset(de->de_ih) - 1);
		pathrelse(path_to_entry);

	}			/* while (1) */
Linus Torvalds's avatar
Linus Torvalds committed
350 351
}

352
static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
353
				      unsigned int flags)
Linus Torvalds's avatar
Linus Torvalds committed
354
{
355 356 357 358 359 360 361 362
	int retval;
	struct inode *inode = NULL;
	struct reiserfs_dir_entry de;
	INITIALIZE_PATH(path_to_entry);

	if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len)
		return ERR_PTR(-ENAMETOOLONG);

363
	reiserfs_write_lock(dir->i_sb);
364

365 366 367 368 369 370
	de.de_gen_number_bit_string = NULL;
	retval =
	    reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
				&path_to_entry, &de);
	pathrelse(&path_to_entry);
	if (retval == NAME_FOUND) {
371
		inode = reiserfs_iget(dir->i_sb,
372
				      (struct cpu_key *)&de.de_dir_id);
373
		if (!inode || IS_ERR(inode)) {
374
			reiserfs_write_unlock(dir->i_sb);
375 376 377
			return ERR_PTR(-EACCES);
		}

378 379 380 381
		/*
		 * Propagate the private flag so we know we're
		 * in the priv tree
		 */
382 383
		if (IS_PRIVATE(dir))
			inode->i_flags |= S_PRIVATE;
384
	}
385
	reiserfs_write_unlock(dir->i_sb);
386 387
	if (retval == IO_ERROR) {
		return ERR_PTR(-EIO);
Linus Torvalds's avatar
Linus Torvalds committed
388 389
	}

390
	return d_splice_alias(inode, dentry);
391
}
Linus Torvalds's avatar
Linus Torvalds committed
392

393
/*
394 395 396
 * looks up the dentry of the parent directory for child.
 * taken from ext2_get_parent
 */
Linus Torvalds's avatar
Linus Torvalds committed
397 398
struct dentry *reiserfs_get_parent(struct dentry *child)
{
399 400 401 402
	int retval;
	struct inode *inode = NULL;
	struct reiserfs_dir_entry de;
	INITIALIZE_PATH(path_to_entry);
403
	struct inode *dir = d_inode(child);
404 405 406 407 408 409 410 411 412 413 414 415 416

	if (dir->i_nlink == 0) {
		return ERR_PTR(-ENOENT);
	}
	de.de_gen_number_bit_string = NULL;

	reiserfs_write_lock(dir->i_sb);
	retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
	pathrelse(&path_to_entry);
	if (retval != NAME_FOUND) {
		reiserfs_write_unlock(dir->i_sb);
		return ERR_PTR(-ENOENT);
	}
417
	inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&de.de_dir_id);
Linus Torvalds's avatar
Linus Torvalds committed
418 419
	reiserfs_write_unlock(dir->i_sb);

420
	return d_obtain_alias(inode);
421
}
Linus Torvalds's avatar
Linus Torvalds committed
422

423
/* add entry to the directory (entry can be hidden).
Linus Torvalds's avatar
Linus Torvalds committed
424 425 426 427 428

insert definition of when hidden directories are used here -Hans

 Does not mark dir   inode dirty, do it after successesfull call to it */

429 430 431
static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
			      struct inode *dir, const char *name, int namelen,
			      struct inode *inode, int visible)
Linus Torvalds's avatar
Linus Torvalds committed
432
{
433 434 435 436
	struct cpu_key entry_key;
	struct reiserfs_de_head *deh;
	INITIALIZE_PATH(path);
	struct reiserfs_dir_entry de;
437
	DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
438
	int gen_number;
439 440 441 442 443 444 445

	/*
	 * 48 bytes now and we avoid kmalloc if we
	 * create file with short name
	 */
	char small_buf[32 + DEH_SIZE];

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	char *buffer;
	int buflen, paste_size;
	int retval;

	BUG_ON(!th->t_trans_id);

	/* cannot allow items to be added into a busy deleted directory */
	if (!namelen)
		return -EINVAL;

	if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
		return -ENAMETOOLONG;

	/* each entry has unique key. compose it */
	make_cpu_key(&entry_key, dir,
		     get_third_component(dir->i_sb, name, namelen),
		     TYPE_DIRENTRY, 3);

	/* get memory for composing the entry */
	buflen = DEH_SIZE + ROUND_UP(namelen);
	if (buflen > sizeof(small_buf)) {
467
		buffer = kmalloc(buflen, GFP_NOFS);
468
		if (!buffer)
469 470 471 472 473 474 475 476
			return -ENOMEM;
	} else
		buffer = small_buf;

	paste_size =
	    (get_inode_sd_version(dir) ==
	     STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;

477 478 479 480
	/*
	 * fill buffer : directory entry head, name[, dir objectid | ,
	 * stat data | ,stat data, dir objectid ]
	 */
481 482 483 484 485
	deh = (struct reiserfs_de_head *)buffer;
	deh->deh_location = 0;	/* JDM Endian safe if 0 */
	put_deh_offset(deh, cpu_key_k_offset(&entry_key));
	deh->deh_state = 0;	/* JDM Endian safe if 0 */
	/* put key (ino analog) to de */
486 487 488 489 490

	/* safe: k_dir_id is le */
	deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id;
	/* safe: k_objectid is le */
	deh->deh_objectid = INODE_PKEY(inode)->k_objectid;
491 492 493 494 495 496

	/* copy name */
	memcpy((char *)(deh + 1), name, namelen);
	/* padd by 0s to the 4 byte boundary */
	padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);

497 498 499 500
	/*
	 * entry is ready to be pasted into tree, set 'visibility'
	 * and 'stat data in entry' attributes
	 */
501 502 503 504 505
	mark_de_without_sd(deh);
	visible ? mark_de_visible(deh) : mark_de_hidden(deh);

	/* find the proper place for the new entry */
	memset(bit_string, 0, sizeof(bit_string));
506
	de.de_gen_number_bit_string = bit_string;
507 508 509
	retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
	if (retval != NAME_NOT_FOUND) {
		if (buffer != small_buf)
510
			kfree(buffer);
511 512 513 514 515 516 517
		pathrelse(&path);

		if (retval == IO_ERROR) {
			return -EIO;
		}

		if (retval != NAME_FOUND) {
518 519 520
			reiserfs_error(dir->i_sb, "zam-7002",
				       "reiserfs_find_entry() returned "
				       "unexpected value (%d)", retval);
521 522 523 524
		}

		return -EEXIST;
	}
Linus Torvalds's avatar
Linus Torvalds committed
525

526
	gen_number =
527
	    find_first_zero_bit(bit_string,
528 529 530
				MAX_GENERATION_NUMBER + 1);
	if (gen_number > MAX_GENERATION_NUMBER) {
		/* there is no free generation number */
531 532 533
		reiserfs_warning(dir->i_sb, "reiserfs-7010",
				 "Congratulations! we have got hash function "
				 "screwed up");
534
		if (buffer != small_buf)
535
			kfree(buffer);
536 537 538 539 540 541 542 543 544 545
		pathrelse(&path);
		return -EBUSY;
	}
	/* adjust offset of directory enrty */
	put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
	set_cpu_key_k_offset(&entry_key, deh_offset(deh));

	/* update max-hash-collisions counter in reiserfs_sb_info */
	PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);

546 547
	/* we need to re-search for the insertion point */
	if (gen_number != 0) {
548 549
		if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
		    NAME_NOT_FOUND) {
550 551 552
			reiserfs_warning(dir->i_sb, "vs-7032",
					 "entry with this key (%K) already "
					 "exists", &entry_key);
553 554

			if (buffer != small_buf)
555
				kfree(buffer);
556 557 558
			pathrelse(&path);
			return -EBUSY;
		}
Linus Torvalds's avatar
Linus Torvalds committed
559 560
	}

561 562 563 564 565
	/* perform the insertion of the entry that we have prepared */
	retval =
	    reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
				     paste_size);
	if (buffer != small_buf)
566
		kfree(buffer);
567 568 569
	if (retval) {
		reiserfs_check_path(&path);
		return retval;
Linus Torvalds's avatar
Linus Torvalds committed
570 571
	}

572
	dir->i_size += paste_size;
573
	dir->i_mtime = dir->i_ctime = current_time(dir);
574
	if (!S_ISDIR(inode->i_mode) && visible)
575
		/* reiserfs_mkdir or reiserfs_rename will do that by itself */
576
		reiserfs_update_sd(th, dir);
Linus Torvalds's avatar
Linus Torvalds committed
577

578 579
	reiserfs_check_path(&path);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
580 581
}

582 583 584 585 586 587
/*
 * quota utility function, call if you've had to abort after calling
 * new_inode_init, and have not called reiserfs_new_inode yet.
 * This should only be called on inodes that do not have stat data
 * inserted into the tree yet.
 */
588 589
static int drop_new_inode(struct inode *inode)
{
590
	dquot_drop(inode);
591 592 593 594
	make_bad_inode(inode);
	inode->i_flags |= S_NOQUOTA;
	iput(inode);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
595 596
}

597 598 599 600 601 602
/*
 * utility function that does setup for reiserfs_new_inode.
 * dquot_initialize needs lots of credits so it's better to have it
 * outside of a transaction, so we had to pull some bits of
 * reiserfs_new_inode out into this func.
 */
Al Viro's avatar
Al Viro committed
603
static int new_inode_init(struct inode *inode, struct inode *dir, umode_t mode)
604
{
605 606 607 608
	/*
	 * Make inode invalid - just in case we are going to drop it before
	 * the initialization happens
	 */
609
	INODE_PKEY(inode)->k_objectid = 0;
610 611 612 613

	/*
	 * the quota init calls have to know who to charge the quota to, so
	 * we have to set uid and gid here
614 615
	 */
	inode_init_owner(inode, dir, mode);
616
	return dquot_initialize(inode);
Linus Torvalds's avatar
Linus Torvalds committed
617 618
}

Al Viro's avatar
Al Viro committed
619
static int reiserfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
620
			   bool excl)
Linus Torvalds's avatar
Linus Torvalds committed
621
{
622 623
	int retval;
	struct inode *inode;
624 625 626 627
	/*
	 * We need blocks for transaction + (user+group)*(quotas
	 * for new inode + update of quota for directory owner)
	 */
628 629 630 631 632
	int jbegin_count =
	    JOURNAL_PER_BALANCE_CNT * 2 +
	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
	struct reiserfs_transaction_handle th;
633
	struct reiserfs_security_handle security;
634

635 636 637
	retval = dquot_initialize(dir);
	if (retval)
		return retval;
638

639 640 641
	if (!(inode = new_inode(dir->i_sb))) {
		return -ENOMEM;
	}
642 643 644 645 646
	retval = new_inode_init(inode, dir, mode);
	if (retval) {
		drop_new_inode(inode);
		return retval;
	}
Linus Torvalds's avatar
Linus Torvalds committed
647

648
	jbegin_count += reiserfs_cache_default_acl(dir);
649
	retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
650 651 652 653 654
	if (retval < 0) {
		drop_new_inode(inode);
		return retval;
	}
	jbegin_count += retval;
655
	reiserfs_write_lock(dir->i_sb);
Linus Torvalds's avatar
Linus Torvalds committed
656

657 658 659 660 661 662 663 664
	retval = journal_begin(&th, dir->i_sb, jbegin_count);
	if (retval) {
		drop_new_inode(inode);
		goto out_failed;
	}

	retval =
	    reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
665
			       inode, &security);
666 667 668 669 670 671 672 673 674 675 676 677
	if (retval)
		goto out_failed;

	inode->i_op = &reiserfs_file_inode_operations;
	inode->i_fop = &reiserfs_file_operations;
	inode->i_mapping->a_ops = &reiserfs_address_space_operations;

	retval =
	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
			       dentry->d_name.len, inode, 1 /*visible */ );
	if (retval) {
		int err;
678
		drop_nlink(inode);
679
		reiserfs_update_sd(&th, inode);
680
		err = journal_end(&th);
681 682
		if (err)
			retval = err;
Al Viro's avatar
Al Viro committed
683
		unlock_new_inode(inode);
684 685 686 687 688
		iput(inode);
		goto out_failed;
	}
	reiserfs_update_inode_transaction(inode);
	reiserfs_update_inode_transaction(dir);
Linus Torvalds's avatar
Linus Torvalds committed
689

Al Viro's avatar
Al Viro committed
690
	unlock_new_inode(inode);
691
	d_instantiate(dentry, inode);
692
	retval = journal_end(&th);
Linus Torvalds's avatar
Linus Torvalds committed
693

694
out_failed:
695 696 697
	reiserfs_write_unlock(dir->i_sb);
	return retval;
}
Linus Torvalds's avatar
Linus Torvalds committed
698

Al Viro's avatar
Al Viro committed
699
static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
700 701 702 703 704
			  dev_t rdev)
{
	int retval;
	struct inode *inode;
	struct reiserfs_transaction_handle th;
705
	struct reiserfs_security_handle security;
706 707 708 709
	/*
	 * We need blocks for transaction + (user+group)*(quotas
	 * for new inode + update of quota for directory owner)
	 */
710 711 712 713 714
	int jbegin_count =
	    JOURNAL_PER_BALANCE_CNT * 3 +
	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));

715 716 717
	retval = dquot_initialize(dir);
	if (retval)
		return retval;
718

719 720 721
	if (!(inode = new_inode(dir->i_sb))) {
		return -ENOMEM;
	}
722 723 724 725 726
	retval = new_inode_init(inode, dir, mode);
	if (retval) {
		drop_new_inode(inode);
		return retval;
	}
Linus Torvalds's avatar
Linus Torvalds committed
727

728
	jbegin_count += reiserfs_cache_default_acl(dir);
729
	retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
730 731 732 733 734
	if (retval < 0) {
		drop_new_inode(inode);
		return retval;
	}
	jbegin_count += retval;
735
	reiserfs_write_lock(dir->i_sb);
Linus Torvalds's avatar
Linus Torvalds committed
736

737 738 739 740 741
	retval = journal_begin(&th, dir->i_sb, jbegin_count);
	if (retval) {
		drop_new_inode(inode);
		goto out_failed;
	}
Linus Torvalds's avatar
Linus Torvalds committed
742

743 744
	retval =
	    reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
745
			       inode, &security);
746 747 748
	if (retval) {
		goto out_failed;
	}
Linus Torvalds's avatar
Linus Torvalds committed
749

750 751 752
	inode->i_op = &reiserfs_special_inode_operations;
	init_special_inode(inode, inode->i_mode, rdev);

753
	/* FIXME: needed for block and char devices only */
754 755 756 757 758 759 760 761 762 763
	reiserfs_update_sd(&th, inode);

	reiserfs_update_inode_transaction(inode);
	reiserfs_update_inode_transaction(dir);

	retval =
	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
			       dentry->d_name.len, inode, 1 /*visible */ );
	if (retval) {
		int err;
764
		drop_nlink(inode);
765
		reiserfs_update_sd(&th, inode);
766
		err = journal_end(&th);
767 768
		if (err)
			retval = err;
Al Viro's avatar
Al Viro committed
769
		unlock_new_inode(inode);
770 771 772
		iput(inode);
		goto out_failed;
	}
Linus Torvalds's avatar
Linus Torvalds committed
773

Al Viro's avatar
Al Viro committed
774
	unlock_new_inode(inode);
775
	d_instantiate(dentry, inode);
776
	retval = journal_end(&th);
Linus Torvalds's avatar
Linus Torvalds committed
777

778
out_failed:
779 780
	reiserfs_write_unlock(dir->i_sb);
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
781 782
}

783
static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds's avatar
Linus Torvalds committed
784
{
785 786 787
	int retval;
	struct inode *inode;
	struct reiserfs_transaction_handle th;
788
	struct reiserfs_security_handle security;
789 790 791 792
	/*
	 * We need blocks for transaction + (user+group)*(quotas
	 * for new inode + update of quota for directory owner)
	 */
793 794 795 796
	int jbegin_count =
	    JOURNAL_PER_BALANCE_CNT * 3 +
	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
Linus Torvalds's avatar
Linus Torvalds committed
797

798 799 800
	retval = dquot_initialize(dir);
	if (retval)
		return retval;
801

Linus Torvalds's avatar
Linus Torvalds committed
802
#ifdef DISPLACE_NEW_PACKING_LOCALITIES
803 804 805 806
	/*
	 * set flag that new packing locality created and new blocks
	 * for the content of that directory are not displaced yet
	 */
807
	REISERFS_I(dir)->new_packing_locality = 1;
Linus Torvalds's avatar
Linus Torvalds committed
808
#endif
809 810 811 812
	mode = S_IFDIR | mode;
	if (!(inode = new_inode(dir->i_sb))) {
		return -ENOMEM;
	}
813 814 815 816 817
	retval = new_inode_init(inode, dir, mode);
	if (retval) {
		drop_new_inode(inode);
		return retval;
	}
818

819
	jbegin_count += reiserfs_cache_default_acl(dir);
820
	retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
821 822 823 824 825
	if (retval < 0) {
		drop_new_inode(inode);
		return retval;
	}
	jbegin_count += retval;
826
	reiserfs_write_lock(dir->i_sb);
827 828 829 830 831 832

	retval = journal_begin(&th, dir->i_sb, jbegin_count);
	if (retval) {
		drop_new_inode(inode);
		goto out_failed;
	}
Linus Torvalds's avatar
Linus Torvalds committed
833

834 835 836
	/*
	 * inc the link count now, so another writer doesn't overflow
	 * it while we sleep later on.
837 838 839 840 841 842
	 */
	INC_DIR_INODE_NLINK(dir)

	    retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
					old_format_only(dir->i_sb) ?
					EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
843
					dentry, inode, &security);
844
	if (retval) {
Al Viro's avatar
Al Viro committed
845
		DEC_DIR_INODE_NLINK(dir)
846 847 848 849 850 851 852 853 854
		goto out_failed;
	}

	reiserfs_update_inode_transaction(inode);
	reiserfs_update_inode_transaction(dir);

	inode->i_op = &reiserfs_dir_inode_operations;
	inode->i_fop = &reiserfs_dir_operations;

855
	/* note, _this_ add_entry will not update dir's stat data */
856 857 858 859 860
	retval =
	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
			       dentry->d_name.len, inode, 1 /*visible */ );
	if (retval) {
		int err;
861
		clear_nlink(inode);
862 863
		DEC_DIR_INODE_NLINK(dir);
		reiserfs_update_sd(&th, inode);
864
		err = journal_end(&th);
865 866
		if (err)
			retval = err;
Al Viro's avatar
Al Viro committed
867
		unlock_new_inode(inode);
868 869 870
		iput(inode);
		goto out_failed;
	}
871
	/* the above add_entry did not update dir's stat data */
872 873
	reiserfs_update_sd(&th, dir);

Al Viro's avatar
Al Viro committed
874
	unlock_new_inode(inode);
875
	d_instantiate(dentry, inode);
876
	retval = journal_end(&th);
877
out_failed:
878
	reiserfs_write_unlock(dir->i_sb);
879
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
880 881
}

882
static inline int reiserfs_empty_dir(struct inode *inode)
Linus Torvalds's avatar
Linus Torvalds committed
883
{
884 885 886 887 888
	/*
	 * we can cheat because an old format dir cannot have
	 * EMPTY_DIR_SIZE, and a new format dir cannot have
	 * EMPTY_DIR_SIZE_V1.  So, if the inode is either size,
	 * regardless of disk format version, the directory is empty.
889 890 891 892 893 894
	 */
	if (inode->i_size != EMPTY_DIR_SIZE &&
	    inode->i_size != EMPTY_DIR_SIZE_V1) {
		return 0;
	}
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
895 896
}

897
static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds's avatar
Linus Torvalds committed
898
{
899 900 901 902 903 904 905
	int retval, err;
	struct inode *inode;
	struct reiserfs_transaction_handle th;
	int jbegin_count;
	INITIALIZE_PATH(path);
	struct reiserfs_dir_entry de;

906 907 908 909 910 911
	/*
	 * we will be doing 2 balancings and update 2 stat data, we
	 * change quotas of the owner of the directory and of the owner
	 * of the parent directory.  The quota structure is possibly
	 * deleted only on last iput => outside of this transaction
	 */
912 913 914 915
	jbegin_count =
	    JOURNAL_PER_BALANCE_CNT * 2 + 2 +
	    4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);

916 917 918
	retval = dquot_initialize(dir);
	if (retval)
		return retval;
919

920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
	reiserfs_write_lock(dir->i_sb);
	retval = journal_begin(&th, dir->i_sb, jbegin_count);
	if (retval)
		goto out_rmdir;

	de.de_gen_number_bit_string = NULL;
	if ((retval =
	     reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
				 &path, &de)) == NAME_NOT_FOUND) {
		retval = -ENOENT;
		goto end_rmdir;
	} else if (retval == IO_ERROR) {
		retval = -EIO;
		goto end_rmdir;
	}

936
	inode = d_inode(dentry);
937 938 939 940 941

	reiserfs_update_inode_transaction(inode);
	reiserfs_update_inode_transaction(dir);

	if (de.de_objectid != inode->i_ino) {
942 943 944
		/*
		 * FIXME: compare key of an object and a key found in the entry
		 */
945 946 947 948 949 950 951 952 953
		retval = -EIO;
		goto end_rmdir;
	}
	if (!reiserfs_empty_dir(inode)) {
		retval = -ENOTEMPTY;
		goto end_rmdir;
	}

	/* cut entry from dir directory */
954 955
	retval = reiserfs_cut_from_item(&th, &path, &de.de_entry_key,
					dir, NULL,	/* page */
956 957 958 959 960
					0 /*new file size - not used here */ );
	if (retval < 0)
		goto end_rmdir;

	if (inode->i_nlink != 2 && inode->i_nlink != 1)
961 962 963
		reiserfs_error(inode->i_sb, "reiserfs-7040",
			       "empty directory has nlink != 2 (%d)",
			       inode->i_nlink);
964

965
	clear_nlink(inode);
966
	inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(dir);
967 968 969 970 971 972 973 974 975
	reiserfs_update_sd(&th, inode);

	DEC_DIR_INODE_NLINK(dir)
	    dir->i_size -= (DEH_SIZE + de.de_entrylen);
	reiserfs_update_sd(&th, dir);

	/* prevent empty directory from getting lost */
	add_save_link(&th, inode, 0 /* not truncate */ );

976
	retval = journal_end(&th);
977
	reiserfs_check_path(&path);
978
out_rmdir:
979 980 981
	reiserfs_write_unlock(dir->i_sb);
	return retval;

982
end_rmdir:
983 984 985 986 987
	/*
	 * we must release path, because we did not call
	 * reiserfs_cut_from_item, or reiserfs_cut_from_item does not
	 * release path if operation was not complete
	 */
988
	pathrelse(&path);
989
	err = journal_end(&th);
990 991
	reiserfs_write_unlock(dir->i_sb);
	return err ? err : retval;
Linus Torvalds's avatar
Linus Torvalds committed
992 993
}

994
static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds's avatar
Linus Torvalds committed
995
{
996 997 998 999 1000 1001 1002 1003
	int retval, err;
	struct inode *inode;
	struct reiserfs_dir_entry de;
	INITIALIZE_PATH(path);
	struct reiserfs_transaction_handle th;
	int jbegin_count;
	unsigned long savelink;

1004 1005 1006
	retval = dquot_initialize(dir);
	if (retval)
		return retval;
1007

1008
	inode = d_inode(dentry);
1009

1010 1011 1012 1013 1014 1015 1016
	/*
	 * in this transaction we can be doing at max two balancings and
	 * update two stat datas, we change quotas of the owner of the
	 * directory and of the owner of the parent directory. The quota
	 * structure is possibly deleted only on iput => outside of
	 * this transaction
	 */
1017 1018 1019 1020
	jbegin_count =
	    JOURNAL_PER_BALANCE_CNT * 2 + 2 +
	    4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);

1021
	reiserfs_write_lock(dir->i_sb);
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
	retval = journal_begin(&th, dir->i_sb, jbegin_count);
	if (retval)
		goto out_unlink;

	de.de_gen_number_bit_string = NULL;
	if ((retval =
	     reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
				 &path, &de)) == NAME_NOT_FOUND) {
		retval = -ENOENT;
		goto end_unlink;
	} else if (retval == IO_ERROR) {
		retval = -EIO;
		goto end_unlink;
	}

	reiserfs_update_inode_transaction(inode);
	reiserfs_update_inode_transaction(dir);

	if (de.de_objectid != inode->i_ino) {
1041 1042 1043
		/*
		 * FIXME: compare key of an object and a key found in the entry
		 */
1044 1045 1046 1047 1048
		retval = -EIO;
		goto end_unlink;
	}

	if (!inode->i_nlink) {
1049 1050 1051
		reiserfs_warning(inode->i_sb, "reiserfs-7042",
				 "deleting nonexistent file (%lu), %d",
				 inode->i_ino, inode->i_nlink);
1052
		set_nlink(inode, 1);
1053 1054
	}

1055
	drop_nlink(inode);
1056 1057 1058 1059 1060 1061 1062 1063

	/*
	 * we schedule before doing the add_save_link call, save the link
	 * count so we don't race
	 */
	savelink = inode->i_nlink;

	retval =
1064
	    reiserfs_cut_from_item(&th, &path, &de.de_entry_key, dir, NULL,
1065 1066
				   0);
	if (retval < 0) {
1067
		inc_nlink(inode);
1068 1069
		goto end_unlink;
	}
1070
	inode->i_ctime = current_time(inode);
1071 1072 1073
	reiserfs_update_sd(&th, inode);

	dir->i_size -= (de.de_entrylen + DEH_SIZE);
1074
	dir->i_ctime = dir->i_mtime = current_time(dir);
1075 1076 1077 1078 1079 1080
	reiserfs_update_sd(&th, dir);

	if (!savelink)
		/* prevent file from getting lost */
		add_save_link(&th, inode, 0 /* not truncate */ );

1081
	retval = journal_end(&th);
1082
	reiserfs_check_path(&path);
1083
	reiserfs_write_unlock(dir->i_sb);
1084 1085
	return retval;

1086
end_unlink:
1087
	pathrelse(&path);
1088
	err = journal_end(&th);
1089
	reiserfs_check_path(&path);
Linus Torvalds's avatar
Linus Torvalds committed
1090
	if (err)
1091
		retval = err;
1092
out_unlink:
1093
	reiserfs_write_unlock(dir->i_sb);
1094
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
1095 1096
}

1097 1098
static int reiserfs_symlink(struct inode *parent_dir,
			    struct dentry *dentry, const char *symname)
Linus Torvalds's avatar
Linus Torvalds committed
1099
{
1100 1101 1102 1103 1104
	int retval;
	struct inode *inode;
	char *name;
	int item_len;
	struct reiserfs_transaction_handle th;
1105
	struct reiserfs_security_handle security;
1106
	int mode = S_IFLNK | S_IRWXUGO;
1107 1108 1109 1110
	/*
	 * We need blocks for transaction + (user+group)*(quotas for
	 * new inode + update of quota for directory owner)
	 */
1111 1112 1113 1114 1115
	int jbegin_count =
	    JOURNAL_PER_BALANCE_CNT * 3 +
	    2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
		 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));

1116 1117 1118
	retval = dquot_initialize(parent_dir);
	if (retval)
		return retval;
1119

1120 1121 1122
	if (!(inode = new_inode(parent_dir->i_sb))) {
		return -ENOMEM;
	}
1123 1124 1125 1126 1127
	retval = new_inode_init(inode, parent_dir, mode);
	if (retval) {
		drop_new_inode(inode);
		return retval;
	}
1128

1129 1130
	retval = reiserfs_security_init(parent_dir, inode, &dentry->d_name,
					&security);
1131 1132 1133 1134 1135 1136
	if (retval < 0) {
		drop_new_inode(inode);
		return retval;
	}
	jbegin_count += retval;

1137 1138 1139 1140 1141 1142 1143 1144
	reiserfs_write_lock(parent_dir->i_sb);
	item_len = ROUND_UP(strlen(symname));
	if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
		retval = -ENAMETOOLONG;
		drop_new_inode(inode);
		goto out_failed;
	}

1145
	name = kmalloc(item_len, GFP_NOFS);
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
	if (!name) {
		drop_new_inode(inode);
		retval = -ENOMEM;
		goto out_failed;
	}
	memcpy(name, symname, strlen(symname));
	padd_item(name, item_len, strlen(symname));

	retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
	if (retval) {
		drop_new_inode(inode);
1157
		kfree(name);
1158 1159 1160 1161 1162
		goto out_failed;
	}

	retval =
	    reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
1163
			       dentry, inode, &security);
1164
	kfree(name);
1165 1166 1167
	if (retval) {		/* reiserfs_new_inode iputs for us */
		goto out_failed;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1168

1169 1170 1171 1172
	reiserfs_update_inode_transaction(inode);
	reiserfs_update_inode_transaction(parent_dir);

	inode->i_op = &reiserfs_symlink_inode_operations;
1173
	inode_nohighmem(inode);
1174 1175 1176 1177 1178 1179
	inode->i_mapping->a_ops = &reiserfs_address_space_operations;

	retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
				    dentry->d_name.len, inode, 1 /*visible */ );
	if (retval) {
		int err;
1180
		drop_nlink(inode);
1181
		reiserfs_update_sd(&th, inode);
1182
		err = journal_end(&th);
1183 1184
		if (err)
			retval = err;
Al Viro's avatar
Al Viro committed
1185
		unlock_new_inode(inode);
1186 1187 1188
		iput(inode);
		goto out_failed;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1189

Al Viro's avatar
Al Viro committed
1190
	unlock_new_inode(inode);
1191
	d_instantiate(dentry, inode);
1192
	retval = journal_end(&th);
1193
out_failed:
1194 1195
	reiserfs_write_unlock(parent_dir->i_sb);
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
1196 1197
}

1198 1199 1200 1201
static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
			 struct dentry *dentry)
{
	int retval;
1202
	struct inode *inode = d_inode(old_dentry);
1203
	struct reiserfs_transaction_handle th;
1204 1205 1206 1207
	/*
	 * We need blocks for transaction + update of quotas for
	 * the owners of the directory
	 */
1208 1209 1210 1211
	int jbegin_count =
	    JOURNAL_PER_BALANCE_CNT * 3 +
	    2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);

1212 1213 1214
	retval = dquot_initialize(dir);
	if (retval)
		return retval;
1215

1216 1217
	reiserfs_write_lock(dir->i_sb);
	if (inode->i_nlink >= REISERFS_LINK_MAX) {
1218
		/* FIXME: sd_nlink is 32 bit for new files */
1219 1220 1221 1222 1223
		reiserfs_write_unlock(dir->i_sb);
		return -EMLINK;
	}

	/* inc before scheduling so reiserfs_unlink knows we are here */
1224
	inc_nlink(inode);
1225 1226 1227

	retval = journal_begin(&th, dir->i_sb, jbegin_count);
	if (retval) {
1228
		drop_nlink(inode);
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
		reiserfs_write_unlock(dir->i_sb);
		return retval;
	}

	/* create new entry */
	retval =
	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
			       dentry->d_name.len, inode, 1 /*visible */ );

	reiserfs_update_inode_transaction(inode);
	reiserfs_update_inode_transaction(dir);

	if (retval) {
		int err;
1243
		drop_nlink(inode);
1244
		err = journal_end(&th);
1245 1246 1247 1248
		reiserfs_write_unlock(dir->i_sb);
		return err ? err : retval;
	}

1249
	inode->i_ctime = current_time(inode);
1250 1251
	reiserfs_update_sd(&th, inode);

Al Viro's avatar
Al Viro committed
1252
	ihold(inode);
1253
	d_instantiate(dentry, inode);
1254
	retval = journal_end(&th);
1255 1256 1257
	reiserfs_write_unlock(dir->i_sb);
	return retval;
}
Linus Torvalds's avatar
Linus Torvalds committed
1258

1259
/* de contains information pointing to an entry which */
1260 1261
static int de_still_valid(const char *name, int len,
			  struct reiserfs_dir_entry *de)
Linus Torvalds's avatar
Linus Torvalds committed
1262
{
1263 1264
	struct reiserfs_dir_entry tmp = *de;

1265
	/* recalculate pointer to name and name length */
1266
	set_de_name_and_namelen(&tmp);
1267
	/* FIXME: could check more */
1268 1269 1270
	if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
		return 0;
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
1271 1272
}

1273 1274 1275
static int entry_points_to_object(const char *name, int len,
				  struct reiserfs_dir_entry *de,
				  struct inode *inode)
Linus Torvalds's avatar
Linus Torvalds committed
1276
{
1277 1278 1279 1280 1281
	if (!de_still_valid(name, len, de))
		return 0;

	if (inode) {
		if (!de_visible(de->de_deh + de->de_entry_num))
1282 1283
			reiserfs_panic(inode->i_sb, "vs-7042",
				       "entry must be visible");
1284 1285
		return (de->de_objectid == inode->i_ino) ? 1 : 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1286

1287 1288
	/* this must be added hidden entry */
	if (de_visible(de->de_deh + de->de_entry_num))
1289
		reiserfs_panic(NULL, "vs-7043", "entry must be visible");
Linus Torvalds's avatar
Linus Torvalds committed
1290

1291
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
1292 1293 1294
}

/* sets key of objectid the entry has to point to */
1295 1296
static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
				 struct reiserfs_key *key)
Linus Torvalds's avatar
Linus Torvalds committed
1297
{
1298 1299 1300
	/* JDM These operations are endian safe - both are le */
	de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
	de->de_deh[