namei.c 32.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * namei.c
 *
 * PURPOSE
 *      Inode name handling routines for the OSTA-UDF(tm) filesystem.
 *
 * COPYRIGHT
 *      This file is distributed under the terms of the GNU General Public
 *      License (GPL). Copies of the GPL can be obtained from:
 *              ftp://prep.ai.mit.edu/pub/gnu/GPL
 *      Each contributing author retains all rights to their own work.
 *
 *  (C) 1998-2004 Ben Fennema
 *  (C) 1999-2000 Stelias Computing Inc
 *
 * HISTORY
 *
 *  12/12/98 blf  Created. Split out the lookup code from dir.c
 *  04/19/99 blf  link, mknod, symlink support
 */

#include "udfdecl.h"

#include "udf_i.h"
#include "udf_sb.h"
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/quotaops.h>
#include <linux/smp_lock.h>
#include <linux/buffer_head.h>
A
Alexey Dobriyan 已提交
33
#include <linux/sched.h>
L
Linus Torvalds 已提交
34

35 36
static inline int udf_match(int len1, const char *name1, int len2,
			    const char *name2)
L
Linus Torvalds 已提交
37 38 39
{
	if (len1 != len2)
		return 0;
40

L
Linus Torvalds 已提交
41 42 43 44
	return !memcmp(name1, name2, len1);
}

int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
45 46
		 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
		 uint8_t * impuse, uint8_t * fileident)
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55
{
	uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(tag);
	uint16_t crc;
	uint8_t checksum = 0;
	int i;
	int offset;
	uint16_t liu = le16_to_cpu(cfi->lengthOfImpUse);
	uint8_t lfi = cfi->lengthFileIdent;
	int padlen = fibh->eoffset - fibh->soffset - liu - lfi -
56
		sizeof(struct fileIdentDesc);
L
Linus Torvalds 已提交
57 58 59 60 61 62 63
	int adinicb = 0;

	if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
		adinicb = 1;

	offset = fibh->soffset + sizeof(struct fileIdentDesc);

64
	if (impuse) {
65 66 67
		if (adinicb || (offset + liu < 0)) {
			memcpy((uint8_t *)sfi->impUse, impuse, liu);
		} else if (offset >= 0) {
L
Linus Torvalds 已提交
68
			memcpy(fibh->ebh->b_data + offset, impuse, liu);
69 70 71
		} else {
			memcpy((uint8_t *)sfi->impUse, impuse, -offset);
			memcpy(fibh->ebh->b_data, impuse - offset, liu + offset);
L
Linus Torvalds 已提交
72 73 74 75 76
		}
	}

	offset += liu;

77
	if (fileident) {
78 79 80
		if (adinicb || (offset + lfi < 0)) {
			memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
		} else if (offset >= 0) {
L
Linus Torvalds 已提交
81
			memcpy(fibh->ebh->b_data + offset, fileident, lfi);
82 83 84
		} else {
			memcpy((uint8_t *)sfi->fileIdent + liu, fileident, -offset);
			memcpy(fibh->ebh->b_data, fileident - offset, lfi + offset);
L
Linus Torvalds 已提交
85 86 87 88 89
		}
	}

	offset += lfi;

90 91 92
	if (adinicb || (offset + padlen < 0)) {
		memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
	} else if (offset >= 0) {
L
Linus Torvalds 已提交
93
		memset(fibh->ebh->b_data + offset, 0x00, padlen);
94 95
	} else {
		memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
L
Linus Torvalds 已提交
96 97 98
		memset(fibh->ebh->b_data, 0x00, padlen + offset);
	}

99 100 101 102 103 104 105 106 107 108 109 110
	crc = udf_crc((uint8_t *)cfi + sizeof(tag),
		      sizeof(struct fileIdentDesc) - sizeof(tag), 0);

	if (fibh->sbh == fibh->ebh) {
		crc = udf_crc((uint8_t *)sfi->impUse,
			      crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
	} else if (sizeof(struct fileIdentDesc) >= -fibh->soffset) {
		crc = udf_crc(fibh->ebh->b_data + sizeof(struct fileIdentDesc) + fibh->soffset,
			      crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
	} else {
		crc = udf_crc((uint8_t *)sfi->impUse,
			      -fibh->soffset - sizeof(struct fileIdentDesc), crc);
L
Linus Torvalds 已提交
111 112 113 114 115 116
		crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
	}

	cfi->descTag.descCRC = cpu_to_le16(crc);
	cfi->descTag.descCRCLength = cpu_to_le16(crclen);

117
	for (i = 0; i < 16; i++) {
L
Linus Torvalds 已提交
118
		if (i != 4)
119 120
			checksum += ((uint8_t *)&cfi->descTag)[i];
	}
L
Linus Torvalds 已提交
121 122

	cfi->descTag.tagChecksum = checksum;
123 124 125 126 127
	if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset)) {
		memcpy((uint8_t *)sfi, (uint8_t *)cfi, sizeof(struct fileIdentDesc));
	} else {
		memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset);
		memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset,
128
		       sizeof(struct fileIdentDesc) + fibh->soffset);
L
Linus Torvalds 已提交
129 130
	}

131
	if (adinicb) {
L
Linus Torvalds 已提交
132
		mark_inode_dirty(inode);
133
	} else {
L
Linus Torvalds 已提交
134 135 136 137 138 139 140
		if (fibh->sbh != fibh->ebh)
			mark_buffer_dirty_inode(fibh->ebh, inode);
		mark_buffer_dirty_inode(fibh->sbh, inode);
	}
	return 0;
}

141 142 143 144
static struct fileIdentDesc *udf_find_entry(struct inode *dir,
					    struct dentry *dentry,
					    struct udf_fileident_bh *fibh,
					    struct fileIdentDesc *cfi)
L
Linus Torvalds 已提交
145
{
146
	struct fileIdentDesc *fi = NULL;
L
Linus Torvalds 已提交
147 148 149 150 151 152
	loff_t f_pos;
	int block, flen;
	char fname[UDF_NAME_LEN];
	char *nameptr;
	uint8_t lfi;
	uint16_t liu;
153
	loff_t size;
J
Jan Kara 已提交
154 155
	kernel_lb_addr eloc;
	uint32_t elen;
156
	sector_t offset;
157
	struct extent_position epos = {};
L
Linus Torvalds 已提交
158

159
	size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
L
Linus Torvalds 已提交
160 161
	f_pos = (udf_ext0_offset(dir) >> 2);

162 163
	fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
	if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
L
Linus Torvalds 已提交
164
		fibh->sbh = fibh->ebh = NULL;
165 166
	} else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
			      &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
L
Linus Torvalds 已提交
167
		block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
168
		if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
L
Linus Torvalds 已提交
169
			if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
J
Jan Kara 已提交
170
				epos.offset -= sizeof(short_ad);
L
Linus Torvalds 已提交
171
			else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
J
Jan Kara 已提交
172
				epos.offset -= sizeof(long_ad);
173
		} else {
L
Linus Torvalds 已提交
174
			offset = 0;
175
		}
L
Linus Torvalds 已提交
176

177
		if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block))) {
J
Jan Kara 已提交
178
			brelse(epos.bh);
L
Linus Torvalds 已提交
179 180
			return NULL;
		}
181
	} else {
J
Jan Kara 已提交
182
		brelse(epos.bh);
L
Linus Torvalds 已提交
183 184 185
		return NULL;
	}

186 187 188 189
	while ((f_pos < size)) {
		fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
					&elen, &offset);
		if (!fi) {
L
Linus Torvalds 已提交
190
			if (fibh->sbh != fibh->ebh)
J
Jan Kara 已提交
191 192 193
				brelse(fibh->ebh);
			brelse(fibh->sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
194 195 196 197 198 199
			return NULL;
		}

		liu = le16_to_cpu(cfi->lengthOfImpUse);
		lfi = cfi->lengthFileIdent;

200
		if (fibh->sbh == fibh->ebh) {
L
Linus Torvalds 已提交
201
			nameptr = fi->fileIdent + liu;
202
		} else {
L
Linus Torvalds 已提交
203 204
			int poffset;	/* Unpaded ending offset */

205
			poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
L
Linus Torvalds 已提交
206

207 208 209
			if (poffset >= lfi) {
				nameptr = (uint8_t *)(fibh->ebh->b_data + poffset - lfi);
			} else {
L
Linus Torvalds 已提交
210
				nameptr = fname;
211 212
				memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
				memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
L
Linus Torvalds 已提交
213 214 215
			}
		}

216 217
		if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
			if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
L
Linus Torvalds 已提交
218 219
				continue;
		}
220 221 222

		if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
			if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
L
Linus Torvalds 已提交
223 224 225 226 227 228
				continue;
		}

		if (!lfi)
			continue;

229
		if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi))) {
230
			if (udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name)) {
J
Jan Kara 已提交
231
				brelse(epos.bh);
L
Linus Torvalds 已提交
232 233 234 235
				return fi;
			}
		}
	}
236

L
Linus Torvalds 已提交
237
	if (fibh->sbh != fibh->ebh)
J
Jan Kara 已提交
238 239 240
		brelse(fibh->ebh);
	brelse(fibh->sbh);
	brelse(epos.bh);
241

L
Linus Torvalds 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	return NULL;
}

/*
 * udf_lookup
 *
 * PURPOSE
 *	Look-up the inode for a given name.
 *
 * DESCRIPTION
 *	Required - lookup_dentry() will return -ENOTDIR if this routine is not
 *	available for a directory. The filesystem is useless if this routine is
 *	not available for at least the filesystem's root directory.
 *
 *	This routine is passed an incomplete dentry - it must be completed by
 *	calling d_add(dentry, inode). If the name does not exist, then the
 *	specified inode must be set to null. An error should only be returned
 *	when the lookup fails for a reason other than the name not existing.
 *	Note that the directory inode semaphore is held during the call.
 *
 *	Refer to lookup_dentry() in fs/namei.c
 *	lookup_dentry() -> lookup() -> real_lookup() -> .
 *
 * PRE-CONDITIONS
 *	dir			Pointer to inode of parent directory.
 *	dentry			Pointer to dentry to complete.
 *	nd			Pointer to lookup nameidata
 *
 * POST-CONDITIONS
 *	<return>		Zero on success.
 *
 * HISTORY
 *	July 1, 1997 - Andrew E. Mileski
 *	Written, tested, and released.
 */

278 279
static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
				 struct nameidata *nd)
L
Linus Torvalds 已提交
280 281
{
	struct inode *inode = NULL;
282
	struct fileIdentDesc cfi;
L
Linus Torvalds 已提交
283 284
	struct udf_fileident_bh fibh;

285
	if (dentry->d_name.len > UDF_NAME_LEN - 2)
L
Linus Torvalds 已提交
286 287 288 289 290
		return ERR_PTR(-ENAMETOOLONG);

	lock_kernel();
#ifdef UDF_RECOVERY
	/* temporary shorthand for specifying files by inode number */
291
	if (!strncmp(dentry->d_name.name, ".B=", 3)) {
292 293 294 295 296
		kernel_lb_addr lb = {
			.logicalBlockNum = 0,
			.partitionReferenceNum = simple_strtoul(dentry->d_name.name + 3,
								NULL, 0),
		};
L
Linus Torvalds 已提交
297
		inode = udf_iget(dir->i_sb, lb);
298
		if (!inode) {
L
Linus Torvalds 已提交
299 300 301
			unlock_kernel();
			return ERR_PTR(-EACCES);
		}
302 303 304
	}
	else
#endif /* UDF_RECOVERY */
L
Linus Torvalds 已提交
305

306
	if (udf_find_entry(dir, dentry, &fibh, &cfi)) {
L
Linus Torvalds 已提交
307
		if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
308 309
			brelse(fibh.ebh);
		brelse(fibh.sbh);
L
Linus Torvalds 已提交
310 311

		inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
312
		if (!inode) {
L
Linus Torvalds 已提交
313 314 315 316 317 318
			unlock_kernel();
			return ERR_PTR(-EACCES);
		}
	}
	unlock_kernel();
	d_add(dentry, inode);
319

L
Linus Torvalds 已提交
320 321 322
	return NULL;
}

323 324 325 326
static struct fileIdentDesc *udf_add_entry(struct inode *dir,
					   struct dentry *dentry,
					   struct udf_fileident_bh *fibh,
					   struct fileIdentDesc *cfi, int *err)
L
Linus Torvalds 已提交
327
{
M
Marcin Slusarz 已提交
328
	struct super_block *sb = dir->i_sb;
329
	struct fileIdentDesc *fi = NULL;
L
Linus Torvalds 已提交
330 331 332 333 334 335 336 337 338 339
	char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
	int namelen;
	loff_t f_pos;
	int flen;
	char *nameptr;
	loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
	int nfidlen;
	uint8_t lfi;
	uint16_t liu;
	int block;
J
Jan Kara 已提交
340 341
	kernel_lb_addr eloc;
	uint32_t elen;
342
	sector_t offset;
343
	struct extent_position epos = {};
L
Linus Torvalds 已提交
344

345 346
	if (dentry) {
		if (!dentry->d_name.len) {
L
Linus Torvalds 已提交
347 348 349
			*err = -EINVAL;
			return NULL;
		}
350 351
		if (!(namelen = udf_put_filename(sb, dentry->d_name.name, name,
						 dentry->d_name.len))) {
L
Linus Torvalds 已提交
352 353 354
			*err = -ENAMETOOLONG;
			return NULL;
		}
355
	} else {
L
Linus Torvalds 已提交
356
		namelen = 0;
357
	}
L
Linus Torvalds 已提交
358 359 360 361 362

	nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;

	f_pos = (udf_ext0_offset(dir) >> 2);

363 364
	fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
	if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
L
Linus Torvalds 已提交
365
		fibh->sbh = fibh->ebh = NULL;
366 367
	} else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
			      &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
L
Linus Torvalds 已提交
368
		block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
369
		if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
L
Linus Torvalds 已提交
370
			if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
J
Jan Kara 已提交
371
				epos.offset -= sizeof(short_ad);
L
Linus Torvalds 已提交
372
			else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
J
Jan Kara 已提交
373
				epos.offset -= sizeof(long_ad);
374
		} else {
L
Linus Torvalds 已提交
375
			offset = 0;
376
		}
L
Linus Torvalds 已提交
377

378
		if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block))) {
J
Jan Kara 已提交
379
			brelse(epos.bh);
L
Linus Torvalds 已提交
380 381 382 383 384 385
			*err = -EIO;
			return NULL;
		}

		block = UDF_I_LOCATION(dir).logicalBlockNum;

386
	} else {
L
Linus Torvalds 已提交
387 388 389 390 391 392
		block = udf_get_lb_pblock(dir->i_sb, UDF_I_LOCATION(dir), 0);
		fibh->sbh = fibh->ebh = NULL;
		fibh->soffset = fibh->eoffset = sb->s_blocksize;
		goto add;
	}

393 394 395
	while ((f_pos < size)) {
		fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
					&elen, &offset);
L
Linus Torvalds 已提交
396

397
		if (!fi) {
L
Linus Torvalds 已提交
398
			if (fibh->sbh != fibh->ebh)
J
Jan Kara 已提交
399 400 401
				brelse(fibh->ebh);
			brelse(fibh->sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
402 403 404 405 406 407 408
			*err = -EIO;
			return NULL;
		}

		liu = le16_to_cpu(cfi->lengthOfImpUse);
		lfi = cfi->lengthFileIdent;

409
		if (fibh->sbh == fibh->ebh) {
L
Linus Torvalds 已提交
410
			nameptr = fi->fileIdent + liu;
411
		} else {
L
Linus Torvalds 已提交
412 413
			int poffset;	/* Unpaded ending offset */

414
			poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
L
Linus Torvalds 已提交
415

416 417 418
			if (poffset >= lfi) {
				nameptr = (char *)(fibh->ebh->b_data + poffset - lfi);
			} else {
L
Linus Torvalds 已提交
419
				nameptr = fname;
420 421
				memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
				memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
L
Linus Torvalds 已提交
422 423 424
			}
		}

425
		if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
426
			if (((sizeof(struct fileIdentDesc) + liu + lfi + 3) & ~3) == nfidlen) {
J
Jan Kara 已提交
427
				brelse(epos.bh);
L
Linus Torvalds 已提交
428 429 430 431 432
				cfi->descTag.tagSerialNum = cpu_to_le16(1);
				cfi->fileVersionNum = cpu_to_le16(1);
				cfi->fileCharacteristics = 0;
				cfi->lengthFileIdent = namelen;
				cfi->lengthOfImpUse = cpu_to_le16(0);
433
				if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
L
Linus Torvalds 已提交
434
					return fi;
435
				} else {
L
Linus Torvalds 已提交
436 437 438 439 440 441 442 443 444 445
					*err = -EIO;
					return NULL;
				}
			}
		}

		if (!lfi || !dentry)
			continue;

		if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)) &&
446
		    udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name)) {
L
Linus Torvalds 已提交
447
			if (fibh->sbh != fibh->ebh)
J
Jan Kara 已提交
448 449 450
				brelse(fibh->ebh);
			brelse(fibh->sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
451 452 453 454 455
			*err = -EEXIST;
			return NULL;
		}
	}

456
add:
L
Linus Torvalds 已提交
457 458 459
	f_pos += nfidlen;

	if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB &&
460
	    sb->s_blocksize - fibh->eoffset < nfidlen) {
J
Jan Kara 已提交
461
		brelse(epos.bh);
J
Jan Kara 已提交
462
		epos.bh = NULL;
L
Linus Torvalds 已提交
463 464 465 466
		fibh->soffset -= udf_ext0_offset(dir);
		fibh->eoffset -= udf_ext0_offset(dir);
		f_pos -= (udf_ext0_offset(dir) >> 2);
		if (fibh->sbh != fibh->ebh)
J
Jan Kara 已提交
467 468
			brelse(fibh->ebh);
		brelse(fibh->sbh);
469
		if (!(fibh->sbh = fibh->ebh = udf_expand_dir_adinicb(dir, &block, err)))
L
Linus Torvalds 已提交
470
			return NULL;
J
Jan Kara 已提交
471
		epos.block = UDF_I_LOCATION(dir);
L
Linus Torvalds 已提交
472
		eloc.logicalBlockNum = block;
473
		eloc.partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
L
Linus Torvalds 已提交
474
		elen = dir->i_sb->s_blocksize;
J
Jan Kara 已提交
475
		epos.offset = udf_file_entry_alloc_offset(dir);
L
Linus Torvalds 已提交
476
		if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
J
Jan Kara 已提交
477
			epos.offset += sizeof(short_ad);
L
Linus Torvalds 已提交
478
		else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
J
Jan Kara 已提交
479
			epos.offset += sizeof(long_ad);
L
Linus Torvalds 已提交
480 481
	}

482
	if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
L
Linus Torvalds 已提交
483 484
		fibh->soffset = fibh->eoffset;
		fibh->eoffset += nfidlen;
485
		if (fibh->sbh != fibh->ebh) {
J
Jan Kara 已提交
486
			brelse(fibh->sbh);
L
Linus Torvalds 已提交
487 488 489
			fibh->sbh = fibh->ebh;
		}

490
		if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
L
Linus Torvalds 已提交
491
			block = UDF_I_LOCATION(dir).logicalBlockNum;
492
			fi = (struct fileIdentDesc *)(UDF_I_DATA(dir) + fibh->soffset -
493 494 495
						      udf_ext0_offset(dir) +
						      UDF_I_LENEATTR(dir));
		} else {
L
Linus Torvalds 已提交
496
			block = eloc.logicalBlockNum + ((elen - 1) >>
497 498
							dir->i_sb->s_blocksize_bits);
			fi = (struct fileIdentDesc *)(fibh->sbh->b_data + fibh->soffset);
L
Linus Torvalds 已提交
499
		}
500
	} else {
L
Linus Torvalds 已提交
501 502
		fibh->soffset = fibh->eoffset - sb->s_blocksize;
		fibh->eoffset += nfidlen - sb->s_blocksize;
503
		if (fibh->sbh != fibh->ebh) {
J
Jan Kara 已提交
504
			brelse(fibh->sbh);
L
Linus Torvalds 已提交
505 506 507 508
			fibh->sbh = fibh->ebh;
		}

		block = eloc.logicalBlockNum + ((elen - 1) >>
509
						dir->i_sb->s_blocksize_bits);
510 511
		fibh->ebh = udf_bread(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2), 1, err);
		if (!fibh->ebh) {
J
Jan Kara 已提交
512 513
			brelse(epos.bh);
			brelse(fibh->sbh);
L
Linus Torvalds 已提交
514 515 516
			return NULL;
		}

517
		if (!fibh->soffset) {
J
Jan Kara 已提交
518
			if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
519
			    (EXT_RECORDED_ALLOCATED >> 30)) {
L
Linus Torvalds 已提交
520
				block = eloc.logicalBlockNum + ((elen - 1) >>
521 522
					dir->i_sb->s_blocksize_bits);
			} else {
523
				block++;
524
			}
L
Linus Torvalds 已提交
525

J
Jan Kara 已提交
526
			brelse(fibh->sbh);
L
Linus Torvalds 已提交
527 528
			fibh->sbh = fibh->ebh;
			fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
529
		} else {
L
Linus Torvalds 已提交
530
			fi = (struct fileIdentDesc *)
531
				(fibh->sbh->b_data + sb->s_blocksize + fibh->soffset);
L
Linus Torvalds 已提交
532 533 534 535
		}
	}

	memset(cfi, 0, sizeof(struct fileIdentDesc));
M
Marcin Slusarz 已提交
536
	if (UDF_SB(sb)->s_udfrev >= 0x0200)
537
		udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block, sizeof(tag));
L
Linus Torvalds 已提交
538
	else
539
		udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block, sizeof(tag));
L
Linus Torvalds 已提交
540 541 542
	cfi->fileVersionNum = cpu_to_le16(1);
	cfi->lengthFileIdent = namelen;
	cfi->lengthOfImpUse = cpu_to_le16(0);
543
	if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
J
Jan Kara 已提交
544
		brelse(epos.bh);
L
Linus Torvalds 已提交
545 546 547 548 549
		dir->i_size += nfidlen;
		if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
			UDF_I_LENALLOC(dir) += nfidlen;
		mark_inode_dirty(dir);
		return fi;
550
	} else {
J
Jan Kara 已提交
551
		brelse(epos.bh);
L
Linus Torvalds 已提交
552
		if (fibh->sbh != fibh->ebh)
J
Jan Kara 已提交
553 554
			brelse(fibh->ebh);
		brelse(fibh->sbh);
L
Linus Torvalds 已提交
555 556 557 558 559 560
		*err = -EIO;
		return NULL;
	}
}

static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
561 562
			    struct udf_fileident_bh *fibh,
			    struct fileIdentDesc *cfi)
L
Linus Torvalds 已提交
563 564
{
	cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
565

L
Linus Torvalds 已提交
566 567
	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
		memset(&(cfi->icb), 0x00, sizeof(long_ad));
568

L
Linus Torvalds 已提交
569 570 571
	return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
}

572 573
static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
		      struct nameidata *nd)
L
Linus Torvalds 已提交
574 575 576 577 578 579 580 581
{
	struct udf_fileident_bh fibh;
	struct inode *inode;
	struct fileIdentDesc cfi, *fi;
	int err;

	lock_kernel();
	inode = udf_new_inode(dir, mode, &err);
582
	if (!inode) {
L
Linus Torvalds 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595
		unlock_kernel();
		return err;
	}

	if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
		inode->i_data.a_ops = &udf_adinicb_aops;
	else
		inode->i_data.a_ops = &udf_aops;
	inode->i_op = &udf_file_inode_operations;
	inode->i_fop = &udf_file_operations;
	inode->i_mode = mode;
	mark_inode_dirty(inode);

596 597
	if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
		inode->i_nlink--;
L
Linus Torvalds 已提交
598 599 600 601 602 603 604
		mark_inode_dirty(inode);
		iput(inode);
		unlock_kernel();
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
	cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
605 606
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
		cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
607
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
608
	if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
L
Linus Torvalds 已提交
609 610 611
		mark_inode_dirty(dir);
	}
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
612 613
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
614 615
	unlock_kernel();
	d_instantiate(dentry, inode);
616

L
Linus Torvalds 已提交
617 618 619
	return 0;
}

620 621
static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
		     dev_t rdev)
L
Linus Torvalds 已提交
622
{
623
	struct inode *inode;
L
Linus Torvalds 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;

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

	lock_kernel();
	err = -EIO;
	inode = udf_new_inode(dir, mode, &err);
	if (!inode)
		goto out;

	inode->i_uid = current->fsuid;
	init_special_inode(inode, mode, rdev);
639 640
	if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
		inode->i_nlink--;
L
Linus Torvalds 已提交
641 642 643 644 645 646 647
		mark_inode_dirty(inode);
		iput(inode);
		unlock_kernel();
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
	cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
648 649
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
		cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
650
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
651
	if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
L
Linus Torvalds 已提交
652 653 654 655 656
		mark_inode_dirty(dir);
	}
	mark_inode_dirty(inode);

	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
657 658
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
659 660
	d_instantiate(dentry, inode);
	err = 0;
661 662

out:
L
Linus Torvalds 已提交
663 664 665 666
	unlock_kernel();
	return err;
}

667
static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
L
Linus Torvalds 已提交
668
{
669
	struct inode *inode;
L
Linus Torvalds 已提交
670 671 672 673 674 675
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;

	lock_kernel();
	err = -EMLINK;
676
	if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
L
Linus Torvalds 已提交
677 678 679 680 681 682 683 684 685
		goto out;

	err = -EIO;
	inode = udf_new_inode(dir, S_IFDIR, &err);
	if (!inode)
		goto out;

	inode->i_op = &udf_dir_inode_operations;
	inode->i_fop = &udf_dir_operations;
686
	if (!(fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err))) {
L
Linus Torvalds 已提交
687 688 689 690 691 692 693 694
		inode->i_nlink--;
		mark_inode_dirty(inode);
		iput(inode);
		goto out;
	}
	inode->i_nlink = 2;
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
	cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(dir));
695 696 697
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
		cpu_to_le32(UDF_I_UNIQUE(dir) & 0x00000000FFFFFFFFUL);
	cfi.fileCharacteristics = FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
L
Linus Torvalds 已提交
698
	udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
J
Jan Kara 已提交
699
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
700 701 702 703 704
	inode->i_mode = S_IFDIR | mode;
	if (dir->i_mode & S_ISGID)
		inode->i_mode |= S_ISGID;
	mark_inode_dirty(inode);

705
	if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
L
Linus Torvalds 已提交
706 707 708 709 710 711 712
		inode->i_nlink = 0;
		mark_inode_dirty(inode);
		iput(inode);
		goto out;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
	cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
713 714
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
		cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
715 716
	cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
717
	inc_nlink(dir);
L
Linus Torvalds 已提交
718 719 720
	mark_inode_dirty(dir);
	d_instantiate(dentry, inode);
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
721 722
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
723
	err = 0;
724 725

out:
L
Linus Torvalds 已提交
726 727 728 729 730 731 732 733 734 735 736
	unlock_kernel();
	return err;
}

static int empty_dir(struct inode *dir)
{
	struct fileIdentDesc *fi, cfi;
	struct udf_fileident_bh fibh;
	loff_t f_pos;
	loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
	int block;
J
Jan Kara 已提交
737 738
	kernel_lb_addr eloc;
	uint32_t elen;
739
	sector_t offset;
740
	struct extent_position epos = {};
L
Linus Torvalds 已提交
741 742 743

	f_pos = (udf_ext0_offset(dir) >> 2);

744
	fibh.soffset = fibh.eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
L
Linus Torvalds 已提交
745

746
	if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
L
Linus Torvalds 已提交
747
		fibh.sbh = fibh.ebh = NULL;
748 749
	} else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
			      &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
L
Linus Torvalds 已提交
750
		block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
751
		if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
L
Linus Torvalds 已提交
752
			if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
J
Jan Kara 已提交
753
				epos.offset -= sizeof(short_ad);
L
Linus Torvalds 已提交
754
			else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
J
Jan Kara 已提交
755
				epos.offset -= sizeof(long_ad);
756
		} else {
L
Linus Torvalds 已提交
757
			offset = 0;
758
		}
L
Linus Torvalds 已提交
759

760
		if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block))) {
J
Jan Kara 已提交
761
			brelse(epos.bh);
L
Linus Torvalds 已提交
762 763
			return 0;
		}
764
	} else {
J
Jan Kara 已提交
765
		brelse(epos.bh);
L
Linus Torvalds 已提交
766 767 768
		return 0;
	}

769 770 771 772
	while ((f_pos < size)) {
		fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
					&elen, &offset);
		if (!fi) {
L
Linus Torvalds 已提交
773
			if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
774 775 776
				brelse(fibh.ebh);
			brelse(fibh.sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
777 778 779
			return 0;
		}

780 781
		if (cfi.lengthFileIdent &&
		    (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
L
Linus Torvalds 已提交
782
			if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
783 784 785
				brelse(fibh.ebh);
			brelse(fibh.sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
786 787 788
			return 0;
		}
	}
789

L
Linus Torvalds 已提交
790
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
791 792 793
		brelse(fibh.ebh);
	brelse(fibh.sbh);
	brelse(epos.bh);
794

L
Linus Torvalds 已提交
795 796 797
	return 1;
}

798
static int udf_rmdir(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
799 800
{
	int retval;
801
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
	struct udf_fileident_bh fibh;
	struct fileIdentDesc *fi, cfi;
	kernel_lb_addr tloc;

	retval = -ENOENT;
	lock_kernel();
	fi = udf_find_entry(dir, dentry, &fibh, &cfi);
	if (!fi)
		goto out;

	retval = -EIO;
	tloc = lelb_to_cpu(cfi.icb.extLocation);
	if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
		goto end_rmdir;
	retval = -ENOTEMPTY;
	if (!empty_dir(inode))
		goto end_rmdir;
	retval = udf_delete_entry(dir, fi, &fibh, &cfi);
	if (retval)
		goto end_rmdir;
	if (inode->i_nlink != 2)
		udf_warning(inode->i_sb, "udf_rmdir",
824 825
			    "empty directory has nlink != 2 (%d)",
			    inode->i_nlink);
826
	clear_nlink(inode);
L
Linus Torvalds 已提交
827
	inode->i_size = 0;
828
	inode_dec_link_count(dir);
829
	inode->i_ctime = dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
L
Linus Torvalds 已提交
830 831
	mark_inode_dirty(dir);

832
end_rmdir:
L
Linus Torvalds 已提交
833
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
834 835
		brelse(fibh.ebh);
	brelse(fibh.sbh);
836 837

out:
L
Linus Torvalds 已提交
838 839 840 841
	unlock_kernel();
	return retval;
}

842
static int udf_unlink(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
843 844
{
	int retval;
845
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
	struct udf_fileident_bh fibh;
	struct fileIdentDesc *fi;
	struct fileIdentDesc cfi;
	kernel_lb_addr tloc;

	retval = -ENOENT;
	lock_kernel();
	fi = udf_find_entry(dir, dentry, &fibh, &cfi);
	if (!fi)
		goto out;

	retval = -EIO;
	tloc = lelb_to_cpu(cfi.icb.extLocation);
	if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
		goto end_unlink;

862
	if (!inode->i_nlink) {
L
Linus Torvalds 已提交
863
		udf_debug("Deleting nonexistent file (%lu), %d\n",
864
			  inode->i_ino, inode->i_nlink);
L
Linus Torvalds 已提交
865 866 867 868 869 870 871
		inode->i_nlink = 1;
	}
	retval = udf_delete_entry(dir, fi, &fibh, &cfi);
	if (retval)
		goto end_unlink;
	dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
	mark_inode_dirty(dir);
872
	inode_dec_link_count(inode);
L
Linus Torvalds 已提交
873 874 875
	inode->i_ctime = dir->i_ctime;
	retval = 0;

876
end_unlink:
L
Linus Torvalds 已提交
877
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
878 879
		brelse(fibh.ebh);
	brelse(fibh.sbh);
880 881

out:
L
Linus Torvalds 已提交
882 883 884 885
	unlock_kernel();
	return retval;
}

886 887
static int udf_symlink(struct inode *dir, struct dentry *dentry,
		       const char *symname)
L
Linus Torvalds 已提交
888
{
889
	struct inode *inode;
L
Linus Torvalds 已提交
890 891 892
	struct pathComponent *pc;
	char *compstart;
	struct udf_fileident_bh fibh;
893
	struct extent_position epos = {};
L
Linus Torvalds 已提交
894 895 896 897 898 899 900 901
	int eoffset, elen = 0;
	struct fileIdentDesc *fi;
	struct fileIdentDesc cfi;
	char *ea;
	int err;
	int block;
	char name[UDF_NAME_LEN];
	int namelen;
M
Marcin Slusarz 已提交
902
	struct buffer_head *bh;
L
Linus Torvalds 已提交
903 904 905 906 907 908 909 910 911

	lock_kernel();
	if (!(inode = udf_new_inode(dir, S_IFLNK, &err)))
		goto out;

	inode->i_mode = S_IFLNK | S_IRWXUGO;
	inode->i_data.a_ops = &udf_symlink_aops;
	inode->i_op = &page_symlink_inode_operations;

912
	if (UDF_I_ALLOCTYPE(inode) != ICBTAG_FLAG_AD_IN_ICB) {
J
Jan Kara 已提交
913 914
		kernel_lb_addr eloc;
		uint32_t elen;
L
Linus Torvalds 已提交
915 916

		block = udf_new_block(inode->i_sb, inode,
917 918
				      UDF_I_LOCATION(inode).partitionReferenceNum,
				      UDF_I_LOCATION(inode).logicalBlockNum, &err);
L
Linus Torvalds 已提交
919 920
		if (!block)
			goto out_no_entry;
J
Jan Kara 已提交
921 922 923
		epos.block = UDF_I_LOCATION(inode);
		epos.offset = udf_file_entry_alloc_offset(inode);
		epos.bh = NULL;
L
Linus Torvalds 已提交
924
		eloc.logicalBlockNum = block;
925
		eloc.partitionReferenceNum = UDF_I_LOCATION(inode).partitionReferenceNum;
L
Linus Torvalds 已提交
926 927
		elen = inode->i_sb->s_blocksize;
		UDF_I_LENEXTENTS(inode) = elen;
J
Jan Kara 已提交
928
		udf_add_aext(inode, &epos, eloc, elen, 0);
J
Jan Kara 已提交
929
		brelse(epos.bh);
L
Linus Torvalds 已提交
930 931

		block = udf_get_pblock(inode->i_sb, block,
932
				       UDF_I_LOCATION(inode).partitionReferenceNum, 0);
J
Jan Kara 已提交
933 934 935 936 937 938 939
		epos.bh = udf_tread(inode->i_sb, block);
		lock_buffer(epos.bh);
		memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
		set_buffer_uptodate(epos.bh);
		unlock_buffer(epos.bh);
		mark_buffer_dirty_inode(epos.bh, inode);
		ea = epos.bh->b_data + udf_ext0_offset(inode);
940
	} else {
L
Linus Torvalds 已提交
941
		ea = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
942
	}
L
Linus Torvalds 已提交
943 944 945 946

	eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
	pc = (struct pathComponent *)ea;

947 948
	if (*symname == '/') {
		do {
L
Linus Torvalds 已提交
949 950 951 952 953 954 955 956 957 958 959 960
			symname++;
		} while (*symname == '/');

		pc->componentType = 1;
		pc->lengthComponentIdent = 0;
		pc->componentFileVersionNum = 0;
		pc += sizeof(struct pathComponent);
		elen += sizeof(struct pathComponent);
	}

	err = -ENAMETOOLONG;

961
	while (*symname) {
L
Linus Torvalds 已提交
962 963 964 965 966 967 968
		if (elen + sizeof(struct pathComponent) > eoffset)
			goto out_no_entry;

		pc = (struct pathComponent *)(ea + elen);

		compstart = (char *)symname;

969
		do {
L
Linus Torvalds 已提交
970 971 972 973 974 975
			symname++;
		} while (*symname && *symname != '/');

		pc->componentType = 5;
		pc->lengthComponentIdent = 0;
		pc->componentFileVersionNum = 0;
976 977
		if (compstart[0] == '.') {
			if ((symname - compstart) == 1)
L
Linus Torvalds 已提交
978
				pc->componentType = 4;
979
			else if ((symname - compstart) == 2 && compstart[1] == '.')
L
Linus Torvalds 已提交
980 981 982
				pc->componentType = 3;
		}

983
		if (pc->componentType == 5) {
984 985 986
			namelen = udf_put_filename(inode->i_sb, compstart, name,
						   symname - compstart);
			if (!namelen)
L
Linus Torvalds 已提交
987 988
				goto out_no_entry;

989
			if (elen + sizeof(struct pathComponent) + namelen > eoffset)
L
Linus Torvalds 已提交
990 991 992 993 994 995 996 997 998
				goto out_no_entry;
			else
				pc->lengthComponentIdent = namelen;

			memcpy(pc->componentIdent, name, namelen);
		}

		elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;

999 1000
		if (*symname) {
			do {
L
Linus Torvalds 已提交
1001 1002 1003 1004 1005
				symname++;
			} while (*symname == '/');
		}
	}

J
Jan Kara 已提交
1006
	brelse(epos.bh);
L
Linus Torvalds 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015
	inode->i_size = elen;
	if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
		UDF_I_LENALLOC(inode) = inode->i_size;
	mark_inode_dirty(inode);

	if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
		goto out_no_entry;
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
	cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
M
Marcin Slusarz 已提交
1016 1017 1018
	bh = UDF_SB(inode->i_sb)->s_lvid_bh;
	if (bh) {
		struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
L
Linus Torvalds 已提交
1019 1020
		struct logicalVolHeaderDesc *lvhd;
		uint64_t uniqueID;
M
Marcin Slusarz 已提交
1021
		lvhd = (struct logicalVolHeaderDesc *)(lvid->logicalVolContentsUse);
L
Linus Torvalds 已提交
1022
		uniqueID = le64_to_cpu(lvhd->uniqueID);
1023 1024
		*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
			cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
1025 1026 1027
		if (!(++uniqueID & 0x00000000FFFFFFFFUL))
			uniqueID += 16;
		lvhd->uniqueID = cpu_to_le64(uniqueID);
M
Marcin Slusarz 已提交
1028
		mark_buffer_dirty(bh);
L
Linus Torvalds 已提交
1029 1030
	}
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1031
	if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
L
Linus Torvalds 已提交
1032 1033 1034
		mark_inode_dirty(dir);
	}
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
1035 1036
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
1037 1038 1039
	d_instantiate(dentry, inode);
	err = 0;

1040
out:
L
Linus Torvalds 已提交
1041 1042 1043
	unlock_kernel();
	return err;

1044
out_no_entry:
1045
	inode_dec_link_count(inode);
L
Linus Torvalds 已提交
1046 1047 1048 1049
	iput(inode);
	goto out;
}

1050 1051
static int udf_link(struct dentry *old_dentry, struct inode *dir,
		    struct dentry *dentry)
L
Linus Torvalds 已提交
1052 1053 1054 1055 1056
{
	struct inode *inode = old_dentry->d_inode;
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;
M
Marcin Slusarz 已提交
1057
	struct buffer_head *bh;
L
Linus Torvalds 已提交
1058 1059

	lock_kernel();
1060
	if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
L
Linus Torvalds 已提交
1061 1062 1063 1064
		unlock_kernel();
		return -EMLINK;
	}

1065
	if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
L
Linus Torvalds 已提交
1066 1067 1068 1069 1070
		unlock_kernel();
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
	cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
M
Marcin Slusarz 已提交
1071 1072 1073
	bh = UDF_SB(inode->i_sb)->s_lvid_bh;
	if (bh) {
		struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
L
Linus Torvalds 已提交
1074 1075
		struct logicalVolHeaderDesc *lvhd;
		uint64_t uniqueID;
M
Marcin Slusarz 已提交
1076
		lvhd = (struct logicalVolHeaderDesc *)(lvid->logicalVolContentsUse);
L
Linus Torvalds 已提交
1077
		uniqueID = le64_to_cpu(lvhd->uniqueID);
1078 1079
		*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
			cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
1080 1081 1082
		if (!(++uniqueID & 0x00000000FFFFFFFFUL))
			uniqueID += 16;
		lvhd->uniqueID = cpu_to_le64(uniqueID);
M
Marcin Slusarz 已提交
1083
		mark_buffer_dirty(bh);
L
Linus Torvalds 已提交
1084 1085
	}
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1086
	if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
L
Linus Torvalds 已提交
1087 1088
		mark_inode_dirty(dir);
	}
1089

L
Linus Torvalds 已提交
1090
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
1091 1092
		brelse(fibh.ebh);
	brelse(fibh.sbh);
1093
	inc_nlink(inode);
L
Linus Torvalds 已提交
1094 1095 1096 1097 1098
	inode->i_ctime = current_fs_time(inode->i_sb);
	mark_inode_dirty(inode);
	atomic_inc(&inode->i_count);
	d_instantiate(dentry, inode);
	unlock_kernel();
1099

L
Linus Torvalds 已提交
1100 1101 1102 1103 1104 1105
	return 0;
}

/* Anybody can rename anything with this: the permission checks are left to the
 * higher-level routines.
 */
1106 1107
static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
		      struct inode *new_dir, struct dentry *new_dentry)
L
Linus Torvalds 已提交
1108
{
1109 1110
	struct inode *old_inode = old_dentry->d_inode;
	struct inode *new_inode = new_dentry->d_inode;
L
Linus Torvalds 已提交
1111
	struct udf_fileident_bh ofibh, nfibh;
1112
	struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL, ocfi, ncfi;
L
Linus Torvalds 已提交
1113 1114 1115 1116 1117
	struct buffer_head *dir_bh = NULL;
	int retval = -ENOENT;
	kernel_lb_addr tloc;

	lock_kernel();
1118
	if ((ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi))) {
L
Linus Torvalds 已提交
1119
		if (ofibh.sbh != ofibh.ebh)
J
Jan Kara 已提交
1120 1121
			brelse(ofibh.ebh);
		brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1122 1123 1124
	}
	tloc = lelb_to_cpu(ocfi.icb.extLocation);
	if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
1125
	    != old_inode->i_ino)
L
Linus Torvalds 已提交
1126 1127 1128
		goto end_rename;

	nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1129 1130
	if (nfi) {
		if (!new_inode) {
L
Linus Torvalds 已提交
1131
			if (nfibh.sbh != nfibh.ebh)
J
Jan Kara 已提交
1132 1133
				brelse(nfibh.ebh);
			brelse(nfibh.sbh);
L
Linus Torvalds 已提交
1134 1135 1136
			nfi = NULL;
		}
	}
1137
	if (S_ISDIR(old_inode->i_mode)) {
L
Linus Torvalds 已提交
1138 1139
		uint32_t offset = udf_ext0_offset(old_inode);

1140
		if (new_inode) {
L
Linus Torvalds 已提交
1141 1142 1143 1144 1145
			retval = -ENOTEMPTY;
			if (!empty_dir(new_inode))
				goto end_rename;
		}
		retval = -EIO;
1146
		if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB) {
L
Linus Torvalds 已提交
1147
			dir_fi = udf_get_fileident(UDF_I_DATA(old_inode) -
1148
						   (UDF_I_EFE(old_inode) ?
1149
						    sizeof(struct extendedFileEntry) :
1150
						    sizeof(struct fileEntry)),
1151
						   old_inode->i_sb->s_blocksize, &offset);
1152
		} else {
L
Linus Torvalds 已提交
1153 1154 1155
			dir_bh = udf_bread(old_inode, 0, 0, &retval);
			if (!dir_bh)
				goto end_rename;
1156
			dir_fi = udf_get_fileident(dir_bh->b_data, old_inode->i_sb->s_blocksize, &offset);
L
Linus Torvalds 已提交
1157 1158 1159 1160
		}
		if (!dir_fi)
			goto end_rename;
		tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1161
		if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) != old_dir->i_ino)
L
Linus Torvalds 已提交
1162 1163 1164
			goto end_rename;

		retval = -EMLINK;
1165
		if (!new_inode && new_dir->i_nlink >= (256 << sizeof(new_dir->i_nlink)) - 1)
L
Linus Torvalds 已提交
1166 1167
			goto end_rename;
	}
1168
	if (!nfi) {
1169
		nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, &retval);
L
Linus Torvalds 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
		if (!nfi)
			goto end_rename;
	}

	/*
	 * Like most other Unix systems, set the ctime for inodes on a
	 * rename.
	 */
	old_inode->i_ctime = current_fs_time(old_inode->i_sb);
	mark_inode_dirty(old_inode);

	/*
	 * ok, that's it
	 */
	ncfi.fileVersionNum = ocfi.fileVersionNum;
	ncfi.fileCharacteristics = ocfi.fileCharacteristics;
	memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
	udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);

	/* The old fid may have moved - find it again */
	ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
	udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);

1193
	if (new_inode) {
L
Linus Torvalds 已提交
1194
		new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1195
		inode_dec_link_count(new_inode);
L
Linus Torvalds 已提交
1196 1197 1198 1199
	}
	old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
	mark_inode_dirty(old_dir);

1200
	if (dir_fi) {
L
Linus Torvalds 已提交
1201 1202
		dir_fi->icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(new_dir));
		udf_update_tag((char *)dir_fi, (sizeof(struct fileIdentDesc) +
1203
						le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1204
		if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB) {
L
Linus Torvalds 已提交
1205
			mark_inode_dirty(old_inode);
1206
		} else {
L
Linus Torvalds 已提交
1207
			mark_buffer_dirty_inode(dir_bh, old_inode);
1208
		}
1209
		inode_dec_link_count(old_dir);
1210
		if (new_inode) {
1211
			inode_dec_link_count(new_inode);
1212
		} else {
1213
			inc_nlink(new_dir);
L
Linus Torvalds 已提交
1214 1215 1216 1217
			mark_inode_dirty(new_dir);
		}
	}

1218
	if (ofi) {
L
Linus Torvalds 已提交
1219
		if (ofibh.sbh != ofibh.ebh)
J
Jan Kara 已提交
1220 1221
			brelse(ofibh.ebh);
		brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1222 1223 1224 1225
	}

	retval = 0;

1226
end_rename:
J
Jan Kara 已提交
1227
	brelse(dir_bh);
1228
	if (nfi) {
L
Linus Torvalds 已提交
1229
		if (nfibh.sbh != nfibh.ebh)
J
Jan Kara 已提交
1230 1231
			brelse(nfibh.ebh);
		brelse(nfibh.sbh);
L
Linus Torvalds 已提交
1232 1233
	}
	unlock_kernel();
1234

L
Linus Torvalds 已提交
1235 1236 1237
	return retval;
}

1238
const struct inode_operations udf_dir_inode_operations = {
1239 1240 1241 1242 1243 1244 1245 1246 1247
	.lookup				= udf_lookup,
	.create				= udf_create,
	.link				= udf_link,
	.unlink				= udf_unlink,
	.symlink			= udf_symlink,
	.mkdir				= udf_mkdir,
	.rmdir				= udf_rmdir,
	.mknod				= udf_mknod,
	.rename				= udf_rename,
L
Linus Torvalds 已提交
1248
};