namei.c 34.3 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
/*
 * 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>
A
Alexey Dobriyan 已提交
30
#include <linux/sched.h>
31
#include <linux/crc-itu-t.h>
R
Rasmus Rohde 已提交
32
#include <linux/exportfs.h>
L
Linus Torvalds 已提交
33

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

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

int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
44
		 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
M
Marcin Slusarz 已提交
45
		 uint8_t *impuse, uint8_t *fileident)
L
Linus Torvalds 已提交
46
{
47
	uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(struct tag);
L
Linus Torvalds 已提交
48 49 50 51 52
	uint16_t crc;
	int offset;
	uint16_t liu = le16_to_cpu(cfi->lengthOfImpUse);
	uint8_t lfi = cfi->lengthFileIdent;
	int padlen = fibh->eoffset - fibh->soffset - liu - lfi -
53
		sizeof(struct fileIdentDesc);
L
Linus Torvalds 已提交
54 55
	int adinicb = 0;

56
	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
57 58 59 60
		adinicb = 1;

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

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

	offset += liu;

75
	if (fileident) {
76 77 78
		if (adinicb || (offset + lfi < 0)) {
			memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
		} else if (offset >= 0) {
L
Linus Torvalds 已提交
79
			memcpy(fibh->ebh->b_data + offset, fileident, lfi);
80
		} else {
M
Marcin Slusarz 已提交
81 82 83 84
			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
	crc = crc_itu_t(0, (uint8_t *)cfi + sizeof(struct tag),
		      sizeof(struct fileIdentDesc) - sizeof(struct tag));
101 102

	if (fibh->sbh == fibh->ebh) {
103
		crc = crc_itu_t(crc, (uint8_t *)sfi->impUse,
104
			      crclen + sizeof(struct tag) -
105
			      sizeof(struct fileIdentDesc));
106
	} else if (sizeof(struct fileIdentDesc) >= -fibh->soffset) {
107
		crc = crc_itu_t(crc, fibh->ebh->b_data +
M
Marcin Slusarz 已提交
108 109
					sizeof(struct fileIdentDesc) +
					fibh->soffset,
110
			      crclen + sizeof(struct tag) -
111
					sizeof(struct fileIdentDesc));
112
	} else {
113 114 115
		crc = crc_itu_t(crc, (uint8_t *)sfi->impUse,
			      -fibh->soffset - sizeof(struct fileIdentDesc));
		crc = crc_itu_t(crc, fibh->ebh->b_data, fibh->eoffset);
L
Linus Torvalds 已提交
116 117 118 119
	}

	cfi->descTag.descCRC = cpu_to_le16(crc);
	cfi->descTag.descCRCLength = cpu_to_le16(crclen);
120
	cfi->descTag.tagChecksum = udf_tag_checksum(&cfi->descTag);
L
Linus Torvalds 已提交
121

122
	if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset)) {
M
Marcin Slusarz 已提交
123 124
		memcpy((uint8_t *)sfi, (uint8_t *)cfi,
			sizeof(struct fileIdentDesc));
125 126 127
	} 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
/**
 * udf_find_entry - find entry in given directory.
 *
 * @dir:	directory inode to search in
 * @child:	qstr of the name
 * @fibh:	buffer head / inode with file identifier descriptor we found
 * @cfi:	found file identifier descriptor with given name
 *
 * This function searches in the directory @dir for a file name @child. When
 * found, @fibh points to the buffer head(s) (bh is NULL for in ICB
 * directories) containing the file identifier descriptor (FID). In that case
 * the function returns pointer to the FID in the buffer or inode - but note
 * that FID may be split among two buffers (blocks) so accessing it via that
 * pointer isn't easily possible. This pointer can be used only as an iterator
 * for other directory manipulation functions. For inspection of the FID @cfi
 * can be used - the found FID is copied there.
 *
 * Returns pointer to FID, NULL when nothing found, or error code.
 */
160
static struct fileIdentDesc *udf_find_entry(struct inode *dir,
A
Al Viro 已提交
161
					    const struct qstr *child,
162 163
					    struct udf_fileident_bh *fibh,
					    struct fileIdentDesc *cfi)
L
Linus Torvalds 已提交
164
{
165
	struct fileIdentDesc *fi = NULL;
L
Linus Torvalds 已提交
166 167
	loff_t f_pos;
	int block, flen;
A
Al Viro 已提交
168 169
	unsigned char *fname = NULL;
	unsigned char *nameptr;
L
Linus Torvalds 已提交
170 171
	uint8_t lfi;
	uint16_t liu;
172
	loff_t size;
173
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
174
	uint32_t elen;
175
	sector_t offset;
176
	struct extent_position epos = {};
177
	struct udf_inode_info *dinfo = UDF_I(dir);
178 179
	int isdotdot = child->len == 2 &&
		child->name[0] == '.' && child->name[1] == '.';
J
Jan Kara 已提交
180
	struct super_block *sb = dir->i_sb;
L
Linus Torvalds 已提交
181

182 183
	size = udf_ext0_offset(dir) + dir->i_size;
	f_pos = udf_ext0_offset(dir);
L
Linus Torvalds 已提交
184

185
	fibh->sbh = fibh->ebh = NULL;
J
Jan Kara 已提交
186
	fibh->soffset = fibh->eoffset = f_pos & (sb->s_blocksize - 1);
187
	if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
J
Jan Kara 已提交
188
		if (inode_bmap(dir, f_pos >> sb->s_blocksize_bits, &epos,
189 190
		    &eloc, &elen, &offset) != (EXT_RECORDED_ALLOCATED >> 30)) {
			fi = ERR_PTR(-EIO);
191
			goto out_err;
192 193
		}

J
Jan Kara 已提交
194 195
		block = udf_get_lb_pblock(sb, &eloc, offset);
		if ((++offset << sb->s_blocksize_bits) < elen) {
196
			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
197
				epos.offset -= sizeof(struct short_ad);
198
			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
199
				epos.offset -= sizeof(struct long_ad);
M
Marcin Slusarz 已提交
200
		} else
L
Linus Torvalds 已提交
201 202
			offset = 0;

J
Jan Kara 已提交
203
		fibh->sbh = fibh->ebh = udf_tread(sb, block);
204 205
		if (!fibh->sbh) {
			fi = ERR_PTR(-EIO);
206
			goto out_err;
207
		}
L
Linus Torvalds 已提交
208 209
	}

210
	fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
211 212
	if (!fname) {
		fi = ERR_PTR(-ENOMEM);
213
		goto out_err;
214
	}
215

216
	while (f_pos < size) {
217 218
		fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
					&elen, &offset);
219 220
		if (!fi) {
			fi = ERR_PTR(-EIO);
221
			goto out_err;
222
		}
L
Linus Torvalds 已提交
223 224 225 226

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

227
		if (fibh->sbh == fibh->ebh) {
L
Linus Torvalds 已提交
228
			nameptr = fi->fileIdent + liu;
229
		} else {
L
Linus Torvalds 已提交
230 231
			int poffset;	/* Unpaded ending offset */

M
Marcin Slusarz 已提交
232 233
			poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
					liu + lfi;
L
Linus Torvalds 已提交
234

M
Marcin Slusarz 已提交
235 236 237 238
			if (poffset >= lfi)
				nameptr = (uint8_t *)(fibh->ebh->b_data +
						      poffset - lfi);
			else {
L
Linus Torvalds 已提交
239
				nameptr = fname;
M
Marcin Slusarz 已提交
240 241 242 243
				memcpy(nameptr, fi->fileIdent + liu,
					lfi - poffset);
				memcpy(nameptr + lfi - poffset,
					fibh->ebh->b_data, poffset);
L
Linus Torvalds 已提交
244 245 246
			}
		}

247
		if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
J
Jan Kara 已提交
248
			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
L
Linus Torvalds 已提交
249 250
				continue;
		}
251 252

		if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
J
Jan Kara 已提交
253
			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
L
Linus Torvalds 已提交
254 255 256
				continue;
		}

R
Rasmus Rohde 已提交
257
		if ((cfi->fileCharacteristics & FID_FILE_CHAR_PARENT) &&
258 259
		    isdotdot)
			goto out_ok;
R
Rasmus Rohde 已提交
260

L
Linus Torvalds 已提交
261 262 263
		if (!lfi)
			continue;

J
Jan Kara 已提交
264
		flen = udf_get_filename(sb, nameptr, lfi, fname, UDF_NAME_LEN);
265 266 267 268 269 270
		if (flen < 0) {
			fi = ERR_PTR(flen);
			goto out_err;
		}

		if (udf_match(flen, fname, child->len, child->name))
271
			goto out_ok;
L
Linus Torvalds 已提交
272
	}
273

274
	fi = NULL;
275
out_err:
L
Linus Torvalds 已提交
276
	if (fibh->sbh != fibh->ebh)
J
Jan Kara 已提交
277 278
		brelse(fibh->ebh);
	brelse(fibh->sbh);
279
out_ok:
J
Jan Kara 已提交
280
	brelse(epos.bh);
281
	kfree(fname);
282

283
	return fi;
L
Linus Torvalds 已提交
284 285
}

286
static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
A
Al Viro 已提交
287
				 unsigned int flags)
L
Linus Torvalds 已提交
288 289
{
	struct inode *inode = NULL;
290
	struct fileIdentDesc cfi;
L
Linus Torvalds 已提交
291
	struct udf_fileident_bh fibh;
292
	struct fileIdentDesc *fi;
L
Linus Torvalds 已提交
293

294
	if (dentry->d_name.len > UDF_NAME_LEN - 2)
L
Linus Torvalds 已提交
295 296 297 298
		return ERR_PTR(-ENAMETOOLONG);

#ifdef UDF_RECOVERY
	/* temporary shorthand for specifying files by inode number */
299
	if (!strncmp(dentry->d_name.name, ".B=", 3)) {
300
		struct kernel_lb_addr lb = {
301
			.logicalBlockNum = 0,
M
Marcin Slusarz 已提交
302 303 304
			.partitionReferenceNum =
				simple_strtoul(dentry->d_name.name + 3,
						NULL, 0),
305
		};
L
Linus Torvalds 已提交
306
		inode = udf_iget(dir->i_sb, lb);
307 308
		if (IS_ERR(inode))
			return inode;
M
Marcin Slusarz 已提交
309
	} else
310
#endif /* UDF_RECOVERY */
L
Linus Torvalds 已提交
311

312 313 314 315 316
	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
	if (IS_ERR(fi))
		return ERR_CAST(fi);

	if (fi) {
317 318
		struct kernel_lb_addr loc;

L
Linus Torvalds 已提交
319
		if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
320 321
			brelse(fibh.ebh);
		brelse(fibh.sbh);
L
Linus Torvalds 已提交
322

323 324
		loc = lelb_to_cpu(cfi.icb.extLocation);
		inode = udf_iget(dir->i_sb, &loc);
325 326
		if (IS_ERR(inode))
			return ERR_CAST(inode);
L
Linus Torvalds 已提交
327
	}
328

R
Rasmus Rohde 已提交
329
	return d_splice_alias(inode, dentry);
L
Linus Torvalds 已提交
330 331
}

332 333 334 335
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 已提交
336
{
M
Marcin Slusarz 已提交
337
	struct super_block *sb = dir->i_sb;
338
	struct fileIdentDesc *fi = NULL;
A
Al Viro 已提交
339
	unsigned char *name = NULL;
L
Linus Torvalds 已提交
340 341
	int namelen;
	loff_t f_pos;
342
	loff_t size = udf_ext0_offset(dir) + dir->i_size;
L
Linus Torvalds 已提交
343 344 345 346
	int nfidlen;
	uint8_t lfi;
	uint16_t liu;
	int block;
347
	struct kernel_lb_addr eloc;
348
	uint32_t elen = 0;
349
	sector_t offset;
350
	struct extent_position epos = {};
351
	struct udf_inode_info *dinfo;
L
Linus Torvalds 已提交
352

353 354 355 356 357 358 359
	fibh->sbh = fibh->ebh = NULL;
	name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
	if (!name) {
		*err = -ENOMEM;
		goto out_err;
	}

360 361
	if (dentry) {
		if (!dentry->d_name.len) {
L
Linus Torvalds 已提交
362
			*err = -EINVAL;
363
			goto out_err;
L
Linus Torvalds 已提交
364
		}
365 366 367
		namelen = udf_put_filename(sb, dentry->d_name.name,
					   dentry->d_name.len,
					   name, UDF_NAME_LEN);
M
Marcin Slusarz 已提交
368
		if (!namelen) {
L
Linus Torvalds 已提交
369
			*err = -ENAMETOOLONG;
370
			goto out_err;
L
Linus Torvalds 已提交
371
		}
372
	} else {
L
Linus Torvalds 已提交
373
		namelen = 0;
374
	}
L
Linus Torvalds 已提交
375 376 377

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

378
	f_pos = udf_ext0_offset(dir);
L
Linus Torvalds 已提交
379

380
	fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
381
	dinfo = UDF_I(dir);
382 383 384 385
	if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
		if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits, &epos,
		    &eloc, &elen, &offset) != (EXT_RECORDED_ALLOCATED >> 30)) {
			block = udf_get_lb_pblock(dir->i_sb,
386
					&dinfo->i_location, 0);
387 388 389
			fibh->soffset = fibh->eoffset = sb->s_blocksize;
			goto add;
		}
390
		block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
391
		if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
392
			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
393
				epos.offset -= sizeof(struct short_ad);
394
			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
395
				epos.offset -= sizeof(struct long_ad);
M
Marcin Slusarz 已提交
396
		} else
L
Linus Torvalds 已提交
397 398
			offset = 0;

M
Marcin Slusarz 已提交
399 400
		fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
		if (!fibh->sbh) {
L
Linus Torvalds 已提交
401
			*err = -EIO;
402
			goto out_err;
L
Linus Torvalds 已提交
403 404
		}

405
		block = dinfo->i_location.logicalBlockNum;
L
Linus Torvalds 已提交
406 407
	}

408
	while (f_pos < size) {
409 410
		fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
					&elen, &offset);
L
Linus Torvalds 已提交
411

412
		if (!fi) {
L
Linus Torvalds 已提交
413
			*err = -EIO;
414
			goto out_err;
L
Linus Torvalds 已提交
415 416 417 418 419
		}

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

420
		if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
M
Marcin Slusarz 已提交
421 422
			if (((sizeof(struct fileIdentDesc) +
					liu + lfi + 3) & ~3) == nfidlen) {
L
Linus Torvalds 已提交
423 424 425 426 427
				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);
M
Marcin Slusarz 已提交
428 429
				if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
						  name))
430
					goto out_ok;
M
Marcin Slusarz 已提交
431
				else {
L
Linus Torvalds 已提交
432
					*err = -EIO;
433
					goto out_err;
L
Linus Torvalds 已提交
434 435 436 437 438
				}
			}
		}
	}

439
add:
L
Linus Torvalds 已提交
440 441
	f_pos += nfidlen;

442
	if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
443
	    sb->s_blocksize - fibh->eoffset < nfidlen) {
J
Jan Kara 已提交
444
		brelse(epos.bh);
J
Jan Kara 已提交
445
		epos.bh = NULL;
L
Linus Torvalds 已提交
446 447
		fibh->soffset -= udf_ext0_offset(dir);
		fibh->eoffset -= udf_ext0_offset(dir);
448
		f_pos -= udf_ext0_offset(dir);
L
Linus Torvalds 已提交
449
		if (fibh->sbh != fibh->ebh)
J
Jan Kara 已提交
450 451
			brelse(fibh->ebh);
		brelse(fibh->sbh);
M
Marcin Slusarz 已提交
452 453 454
		fibh->sbh = fibh->ebh =
				udf_expand_dir_adinicb(dir, &block, err);
		if (!fibh->sbh)
455
			goto out_err;
456
		epos.block = dinfo->i_location;
J
Jan Kara 已提交
457
		epos.offset = udf_file_entry_alloc_offset(dir);
J
Jan Kara 已提交
458 459
		/* Load extent udf_expand_dir_adinicb() has created */
		udf_current_aext(dir, &epos, &eloc, &elen, 1);
L
Linus Torvalds 已提交
460 461
	}

J
Jan Kara 已提交
462
	/* Entry fits into current block? */
463
	if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
L
Linus Torvalds 已提交
464 465
		fibh->soffset = fibh->eoffset;
		fibh->eoffset += nfidlen;
466
		if (fibh->sbh != fibh->ebh) {
J
Jan Kara 已提交
467
			brelse(fibh->sbh);
L
Linus Torvalds 已提交
468 469 470
			fibh->sbh = fibh->ebh;
		}

471 472
		if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
			block = dinfo->i_location.logicalBlockNum;
M
Marcin Slusarz 已提交
473
			fi = (struct fileIdentDesc *)
474
					(dinfo->i_ext.i_data +
475
					 fibh->soffset -
M
Marcin Slusarz 已提交
476
					 udf_ext0_offset(dir) +
477
					 dinfo->i_lenEAttr);
478
		} else {
M
Marcin Slusarz 已提交
479 480 481 482 483
			block = eloc.logicalBlockNum +
					((elen - 1) >>
						dir->i_sb->s_blocksize_bits);
			fi = (struct fileIdentDesc *)
				(fibh->sbh->b_data + fibh->soffset);
L
Linus Torvalds 已提交
484
		}
485
	} else {
J
Jan Kara 已提交
486 487 488 489 490 491 492 493 494 495
		/* Round up last extent in the file */
		elen = (elen + sb->s_blocksize - 1) & ~(sb->s_blocksize - 1);
		if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
			epos.offset -= sizeof(struct short_ad);
		else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
			epos.offset -= sizeof(struct long_ad);
		udf_write_aext(dir, &epos, &eloc, elen, 1);
		dinfo->i_lenExtents = (dinfo->i_lenExtents + sb->s_blocksize
					- 1) & ~(sb->s_blocksize - 1);

L
Linus Torvalds 已提交
496 497
		fibh->soffset = fibh->eoffset - sb->s_blocksize;
		fibh->eoffset += nfidlen - sb->s_blocksize;
498
		if (fibh->sbh != fibh->ebh) {
J
Jan Kara 已提交
499
			brelse(fibh->sbh);
L
Linus Torvalds 已提交
500 501 502 503
			fibh->sbh = fibh->ebh;
		}

		block = eloc.logicalBlockNum + ((elen - 1) >>
504
						dir->i_sb->s_blocksize_bits);
M
Marcin Slusarz 已提交
505
		fibh->ebh = udf_bread(dir,
506
				f_pos >> dir->i_sb->s_blocksize_bits, 1, err);
507 508
		if (!fibh->ebh)
			goto out_err;
509 510 511 512 513
		/* Extents could have been merged, invalidate our position */
		brelse(epos.bh);
		epos.bh = NULL;
		epos.block = dinfo->i_location;
		epos.offset = udf_file_entry_alloc_offset(dir);
L
Linus Torvalds 已提交
514

515
		if (!fibh->soffset) {
516 517 518 519 520
			/* Find the freshly allocated block */
			while (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
				(EXT_RECORDED_ALLOCATED >> 30))
				;
			block = eloc.logicalBlockNum + ((elen - 1) >>
521
					dir->i_sb->s_blocksize_bits);
J
Jan Kara 已提交
522
			brelse(fibh->sbh);
L
Linus Torvalds 已提交
523 524
			fibh->sbh = fibh->ebh;
			fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
525
		} else {
L
Linus Torvalds 已提交
526
			fi = (struct fileIdentDesc *)
M
Marcin Slusarz 已提交
527 528
				(fibh->sbh->b_data + sb->s_blocksize +
					fibh->soffset);
L
Linus Torvalds 已提交
529 530 531 532
		}
	}

	memset(cfi, 0, sizeof(struct fileIdentDesc));
M
Marcin Slusarz 已提交
533
	if (UDF_SB(sb)->s_udfrev >= 0x0200)
M
Marcin Slusarz 已提交
534
		udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
535
			    sizeof(struct tag));
L
Linus Torvalds 已提交
536
	else
M
Marcin Slusarz 已提交
537
		udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
538
			    sizeof(struct tag));
L
Linus Torvalds 已提交
539 540 541
	cfi->fileVersionNum = cpu_to_le16(1);
	cfi->lengthFileIdent = namelen;
	cfi->lengthOfImpUse = cpu_to_le16(0);
542
	if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
L
Linus Torvalds 已提交
543
		dir->i_size += nfidlen;
544 545
		if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
			dinfo->i_lenAlloc += nfidlen;
J
Jan Kara 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559
		else {
			/* Find the last extent and truncate it to proper size */
			while (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
				(EXT_RECORDED_ALLOCATED >> 30))
				;
			elen -= dinfo->i_lenExtents - dir->i_size;
			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
				epos.offset -= sizeof(struct short_ad);
			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
				epos.offset -= sizeof(struct long_ad);
			udf_write_aext(dir, &epos, &eloc, elen, 1);
			dinfo->i_lenExtents = dir->i_size;
		}

L
Linus Torvalds 已提交
560
		mark_inode_dirty(dir);
561
		goto out_ok;
562
	} else {
L
Linus Torvalds 已提交
563
		*err = -EIO;
564
		goto out_err;
L
Linus Torvalds 已提交
565
	}
566 567 568 569 570 571 572 573 574 575

out_err:
	fi = NULL;
	if (fibh->sbh != fibh->ebh)
		brelse(fibh->ebh);
	brelse(fibh->sbh);
out_ok:
	brelse(epos.bh);
	kfree(name);
	return fi;
L
Linus Torvalds 已提交
576 577 578
}

static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
579 580
			    struct udf_fileident_bh *fibh,
			    struct fileIdentDesc *cfi)
L
Linus Torvalds 已提交
581 582
{
	cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
583

L
Linus Torvalds 已提交
584
	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
585
		memset(&(cfi->icb), 0x00, sizeof(struct long_ad));
586

L
Linus Torvalds 已提交
587 588 589
	return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
}

590
static int udf_add_nondir(struct dentry *dentry, struct inode *inode)
L
Linus Torvalds 已提交
591
{
592
	struct udf_inode_info *iinfo = UDF_I(inode);
593
	struct inode *dir = d_inode(dentry->d_parent);
L
Linus Torvalds 已提交
594 595 596 597
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;

M
Marcin Slusarz 已提交
598
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
599
	if (unlikely(!fi)) {
600
		inode_dec_link_count(inode);
601
		unlock_new_inode(inode);
L
Linus Torvalds 已提交
602 603 604 605
		iput(inode);
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
606
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
607
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
608
		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
609
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
610 611
	dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
	mark_inode_dirty(dir);
L
Linus Torvalds 已提交
612
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
613 614
		brelse(fibh.ebh);
	brelse(fibh.sbh);
615
	unlock_new_inode(inode);
L
Linus Torvalds 已提交
616
	d_instantiate(dentry, inode);
617

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

621 622 623
static int udf_create(struct inode *dir, struct dentry *dentry, umode_t mode,
		      bool excl)
{
624
	struct inode *inode = udf_new_inode(dir, mode);
625

626 627
	if (IS_ERR(inode))
		return PTR_ERR(inode);
628 629 630 631 632 633 634 635 636 637 638 639

	if (UDF_I(inode)->i_alloc_type == 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;
	mark_inode_dirty(inode);

	return udf_add_nondir(dentry, inode);
}

A
Al Viro 已提交
640 641
static int udf_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
{
642
	struct inode *inode = udf_new_inode(dir, mode);
A
Al Viro 已提交
643

644 645
	if (IS_ERR(inode))
		return PTR_ERR(inode);
A
Al Viro 已提交
646

647
	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
A
Al Viro 已提交
648 649 650 651 652 653 654
		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;
	mark_inode_dirty(inode);
	d_tmpfile(dentry, inode);
655
	unlock_new_inode(inode);
A
Al Viro 已提交
656 657 658
	return 0;
}

A
Al Viro 已提交
659
static int udf_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
660
		     dev_t rdev)
L
Linus Torvalds 已提交
661
{
662
	struct inode *inode;
L
Linus Torvalds 已提交
663 664 665 666

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

667 668 669
	inode = udf_new_inode(dir, mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);
670

671 672
	init_special_inode(inode, mode, rdev);
	return udf_add_nondir(dentry, inode);
L
Linus Torvalds 已提交
673 674
}

675
static int udf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
L
Linus Torvalds 已提交
676
{
677
	struct inode *inode;
L
Linus Torvalds 已提交
678 679 680
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;
681 682
	struct udf_inode_info *dinfo = UDF_I(dir);
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
683

684 685 686
	inode = udf_new_inode(dir, S_IFDIR | mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);
L
Linus Torvalds 已提交
687

688
	iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
689 690
	inode->i_op = &udf_dir_inode_operations;
	inode->i_fop = &udf_dir_operations;
M
Marcin Slusarz 已提交
691 692
	fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
	if (!fi) {
693
		inode_dec_link_count(inode);
694
		unlock_new_inode(inode);
L
Linus Torvalds 已提交
695 696 697
		iput(inode);
		goto out;
	}
M
Miklos Szeredi 已提交
698
	set_nlink(inode, 2);
L
Linus Torvalds 已提交
699
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
700
	cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
701
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
702
		cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
M
Marcin Slusarz 已提交
703 704
	cfi.fileCharacteristics =
			FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
L
Linus Torvalds 已提交
705
	udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
J
Jan Kara 已提交
706
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
707 708
	mark_inode_dirty(inode);

M
Marcin Slusarz 已提交
709 710
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi) {
711
		clear_nlink(inode);
L
Linus Torvalds 已提交
712
		mark_inode_dirty(inode);
713
		unlock_new_inode(inode);
L
Linus Torvalds 已提交
714 715 716 717
		iput(inode);
		goto out;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
718
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
719
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
720
		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
721 722
	cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
723
	inc_nlink(dir);
724
	dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
L
Linus Torvalds 已提交
725
	mark_inode_dirty(dir);
726
	unlock_new_inode(inode);
L
Linus Torvalds 已提交
727 728
	d_instantiate(dentry, inode);
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
729 730
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
731
	err = 0;
732 733

out:
L
Linus Torvalds 已提交
734 735 736 737 738 739 740 741
	return err;
}

static int empty_dir(struct inode *dir)
{
	struct fileIdentDesc *fi, cfi;
	struct udf_fileident_bh fibh;
	loff_t f_pos;
742
	loff_t size = udf_ext0_offset(dir) + dir->i_size;
L
Linus Torvalds 已提交
743
	int block;
744
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
745
	uint32_t elen;
746
	sector_t offset;
747
	struct extent_position epos = {};
748
	struct udf_inode_info *dinfo = UDF_I(dir);
L
Linus Torvalds 已提交
749

750 751
	f_pos = udf_ext0_offset(dir);
	fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
L
Linus Torvalds 已提交
752

753
	if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
754
		fibh.sbh = fibh.ebh = NULL;
755
	else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
M
Marcin Slusarz 已提交
756 757
			      &epos, &eloc, &elen, &offset) ==
					(EXT_RECORDED_ALLOCATED >> 30)) {
758
		block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
759
		if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
760
			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
761
				epos.offset -= sizeof(struct short_ad);
762
			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
763
				epos.offset -= sizeof(struct long_ad);
M
Marcin Slusarz 已提交
764
		} else
L
Linus Torvalds 已提交
765 766
			offset = 0;

M
Marcin Slusarz 已提交
767 768
		fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
		if (!fibh.sbh) {
J
Jan Kara 已提交
769
			brelse(epos.bh);
L
Linus Torvalds 已提交
770 771
			return 0;
		}
772
	} else {
J
Jan Kara 已提交
773
		brelse(epos.bh);
L
Linus Torvalds 已提交
774 775 776
		return 0;
	}

777
	while (f_pos < size) {
778 779 780
		fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
					&elen, &offset);
		if (!fi) {
L
Linus Torvalds 已提交
781
			if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
782 783 784
				brelse(fibh.ebh);
			brelse(fibh.sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
785 786 787
			return 0;
		}

788 789
		if (cfi.lengthFileIdent &&
		    (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
L
Linus Torvalds 已提交
790
			if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
791 792 793
				brelse(fibh.ebh);
			brelse(fibh.sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
794 795 796
			return 0;
		}
	}
797

L
Linus Torvalds 已提交
798
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
799 800 801
		brelse(fibh.ebh);
	brelse(fibh.sbh);
	brelse(epos.bh);
802

L
Linus Torvalds 已提交
803 804 805
	return 1;
}

806
static int udf_rmdir(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
807 808
{
	int retval;
809
	struct inode *inode = d_inode(dentry);
L
Linus Torvalds 已提交
810 811
	struct udf_fileident_bh fibh;
	struct fileIdentDesc *fi, cfi;
812
	struct kernel_lb_addr tloc;
L
Linus Torvalds 已提交
813 814

	retval = -ENOENT;
815
	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
816 817 818
	if (IS_ERR_OR_NULL(fi)) {
		if (fi)
			retval = PTR_ERR(fi);
L
Linus Torvalds 已提交
819
		goto out;
820
	}
L
Linus Torvalds 已提交
821 822 823

	retval = -EIO;
	tloc = lelb_to_cpu(cfi.icb.extLocation);
824
	if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
L
Linus Torvalds 已提交
825 826 827 828 829 830 831 832
		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)
J
Joe Perches 已提交
833 834
		udf_warn(inode->i_sb, "empty directory has nlink != 2 (%d)\n",
			 inode->i_nlink);
835
	clear_nlink(inode);
L
Linus Torvalds 已提交
836
	inode->i_size = 0;
837
	inode_dec_link_count(dir);
M
Marcin Slusarz 已提交
838 839
	inode->i_ctime = dir->i_ctime = dir->i_mtime =
						current_fs_time(dir->i_sb);
L
Linus Torvalds 已提交
840 841
	mark_inode_dirty(dir);

842
end_rmdir:
L
Linus Torvalds 已提交
843
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
844 845
		brelse(fibh.ebh);
	brelse(fibh.sbh);
846 847

out:
L
Linus Torvalds 已提交
848 849 850
	return retval;
}

851
static int udf_unlink(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
852 853
{
	int retval;
854
	struct inode *inode = d_inode(dentry);
L
Linus Torvalds 已提交
855 856 857
	struct udf_fileident_bh fibh;
	struct fileIdentDesc *fi;
	struct fileIdentDesc cfi;
858
	struct kernel_lb_addr tloc;
L
Linus Torvalds 已提交
859 860

	retval = -ENOENT;
861
	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
862 863 864 865

	if (IS_ERR_OR_NULL(fi)) {
		if (fi)
			retval = PTR_ERR(fi);
L
Linus Torvalds 已提交
866
		goto out;
867
	}
L
Linus Torvalds 已提交
868 869 870

	retval = -EIO;
	tloc = lelb_to_cpu(cfi.icb.extLocation);
871
	if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
L
Linus Torvalds 已提交
872 873
		goto end_unlink;

874
	if (!inode->i_nlink) {
L
Linus Torvalds 已提交
875
		udf_debug("Deleting nonexistent file (%lu), %d\n",
876
			  inode->i_ino, inode->i_nlink);
M
Miklos Szeredi 已提交
877
		set_nlink(inode, 1);
L
Linus Torvalds 已提交
878 879 880 881 882 883
	}
	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);
884
	inode_dec_link_count(inode);
L
Linus Torvalds 已提交
885 886 887
	inode->i_ctime = dir->i_ctime;
	retval = 0;

888
end_unlink:
L
Linus Torvalds 已提交
889
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
890 891
		brelse(fibh.ebh);
	brelse(fibh.sbh);
892 893

out:
L
Linus Torvalds 已提交
894 895 896
	return retval;
}

897 898
static int udf_symlink(struct inode *dir, struct dentry *dentry,
		       const char *symname)
L
Linus Torvalds 已提交
899
{
900
	struct inode *inode = udf_new_inode(dir, S_IFLNK | S_IRWXUGO);
L
Linus Torvalds 已提交
901
	struct pathComponent *pc;
A
Al Viro 已提交
902
	const char *compstart;
903
	struct extent_position epos = {};
L
Linus Torvalds 已提交
904
	int eoffset, elen = 0;
A
Al Viro 已提交
905
	uint8_t *ea;
L
Linus Torvalds 已提交
906 907
	int err;
	int block;
A
Al Viro 已提交
908
	unsigned char *name = NULL;
L
Linus Torvalds 已提交
909
	int namelen;
910
	struct udf_inode_info *iinfo;
911
	struct super_block *sb = dir->i_sb;
L
Linus Torvalds 已提交
912

913 914
	if (IS_ERR(inode))
		return PTR_ERR(inode);
L
Linus Torvalds 已提交
915

916 917
	iinfo = UDF_I(inode);
	down_write(&iinfo->i_data_sem);
918 919 920 921 922 923
	name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
	if (!name) {
		err = -ENOMEM;
		goto out_no_entry;
	}

L
Linus Torvalds 已提交
924
	inode->i_data.a_ops = &udf_symlink_aops;
925
	inode->i_op = &page_symlink_inode_operations;
926
	inode_nohighmem(inode);
L
Linus Torvalds 已提交
927

928
	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
929
		struct kernel_lb_addr eloc;
930
		uint32_t bsize;
L
Linus Torvalds 已提交
931

932
		block = udf_new_block(sb, inode,
933 934
				iinfo->i_location.partitionReferenceNum,
				iinfo->i_location.logicalBlockNum, &err);
L
Linus Torvalds 已提交
935 936
		if (!block)
			goto out_no_entry;
937
		epos.block = iinfo->i_location;
J
Jan Kara 已提交
938 939
		epos.offset = udf_file_entry_alloc_offset(inode);
		epos.bh = NULL;
L
Linus Torvalds 已提交
940
		eloc.logicalBlockNum = block;
M
Marcin Slusarz 已提交
941
		eloc.partitionReferenceNum =
942
				iinfo->i_location.partitionReferenceNum;
943
		bsize = sb->s_blocksize;
944
		iinfo->i_lenExtents = bsize;
945
		udf_add_aext(inode, &epos, &eloc, bsize, 0);
J
Jan Kara 已提交
946
		brelse(epos.bh);
L
Linus Torvalds 已提交
947

948
		block = udf_get_pblock(sb, block,
949
				iinfo->i_location.partitionReferenceNum,
M
Marcin Slusarz 已提交
950
				0);
951
		epos.bh = udf_tgetblk(sb, block);
J
Jan Kara 已提交
952
		lock_buffer(epos.bh);
953
		memset(epos.bh->b_data, 0x00, bsize);
J
Jan Kara 已提交
954 955 956 957
		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);
958 959
	} else
		ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
L
Linus Torvalds 已提交
960

961
	eoffset = sb->s_blocksize - udf_ext0_offset(inode);
L
Linus Torvalds 已提交
962 963
	pc = (struct pathComponent *)ea;

964 965
	if (*symname == '/') {
		do {
L
Linus Torvalds 已提交
966 967 968 969 970 971 972 973 974 975 976
			symname++;
		} while (*symname == '/');

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

	err = -ENAMETOOLONG;

977
	while (*symname) {
L
Linus Torvalds 已提交
978 979 980 981 982
		if (elen + sizeof(struct pathComponent) > eoffset)
			goto out_no_entry;

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

A
Al Viro 已提交
983
		compstart = symname;
L
Linus Torvalds 已提交
984

985
		do {
L
Linus Torvalds 已提交
986 987 988 989 990 991
			symname++;
		} while (*symname && *symname != '/');

		pc->componentType = 5;
		pc->lengthComponentIdent = 0;
		pc->componentFileVersionNum = 0;
992 993
		if (compstart[0] == '.') {
			if ((symname - compstart) == 1)
L
Linus Torvalds 已提交
994
				pc->componentType = 4;
M
Marcin Slusarz 已提交
995 996
			else if ((symname - compstart) == 2 &&
					compstart[1] == '.')
L
Linus Torvalds 已提交
997 998 999
				pc->componentType = 3;
		}

1000
		if (pc->componentType == 5) {
1001 1002 1003
			namelen = udf_put_filename(sb, compstart,
						   symname - compstart,
						   name, UDF_NAME_LEN);
1004
			if (!namelen)
L
Linus Torvalds 已提交
1005 1006
				goto out_no_entry;

M
Marcin Slusarz 已提交
1007 1008
			if (elen + sizeof(struct pathComponent) + namelen >
					eoffset)
L
Linus Torvalds 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017
				goto out_no_entry;
			else
				pc->lengthComponentIdent = namelen;

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

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

1018 1019
		if (*symname) {
			do {
L
Linus Torvalds 已提交
1020 1021 1022 1023 1024
				symname++;
			} while (*symname == '/');
		}
	}

J
Jan Kara 已提交
1025
	brelse(epos.bh);
L
Linus Torvalds 已提交
1026
	inode->i_size = elen;
1027 1028
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
		iinfo->i_lenAlloc = inode->i_size;
J
Jan Kara 已提交
1029 1030
	else
		udf_truncate_tail_extent(inode);
L
Linus Torvalds 已提交
1031
	mark_inode_dirty(inode);
1032
	up_write(&iinfo->i_data_sem);
L
Linus Torvalds 已提交
1033

1034
	err = udf_add_nondir(dentry, inode);
1035
out:
1036
	kfree(name);
L
Linus Torvalds 已提交
1037 1038
	return err;

1039
out_no_entry:
1040
	up_write(&iinfo->i_data_sem);
1041
	inode_dec_link_count(inode);
1042
	unlock_new_inode(inode);
L
Linus Torvalds 已提交
1043 1044 1045 1046
	iput(inode);
	goto out;
}

1047 1048
static int udf_link(struct dentry *old_dentry, struct inode *dir,
		    struct dentry *dentry)
L
Linus Torvalds 已提交
1049
{
1050
	struct inode *inode = d_inode(old_dentry);
L
Linus Torvalds 已提交
1051 1052 1053 1054
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;

M
Marcin Slusarz 已提交
1055 1056
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi) {
L
Linus Torvalds 已提交
1057 1058 1059
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1060
	cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1061
	if (UDF_SB(inode->i_sb)->s_lvid_bh) {
1062
		*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1063
			cpu_to_le32(lvid_get_unique_id(inode->i_sb));
L
Linus Torvalds 已提交
1064 1065
	}
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1066
	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1067
		mark_inode_dirty(dir);
1068

L
Linus Torvalds 已提交
1069
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
1070 1071
		brelse(fibh.ebh);
	brelse(fibh.sbh);
1072
	inc_nlink(inode);
L
Linus Torvalds 已提交
1073 1074
	inode->i_ctime = current_fs_time(inode->i_sb);
	mark_inode_dirty(inode);
1075 1076
	dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
	mark_inode_dirty(dir);
A
Al Viro 已提交
1077
	ihold(inode);
L
Linus Torvalds 已提交
1078
	d_instantiate(dentry, inode);
1079

L
Linus Torvalds 已提交
1080 1081 1082 1083 1084 1085
	return 0;
}

/* Anybody can rename anything with this: the permission checks are left to the
 * higher-level routines.
 */
1086 1087
static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
		      struct inode *new_dir, struct dentry *new_dentry)
L
Linus Torvalds 已提交
1088
{
1089 1090
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
L
Linus Torvalds 已提交
1091
	struct udf_fileident_bh ofibh, nfibh;
M
Marcin Slusarz 已提交
1092 1093
	struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
	struct fileIdentDesc ocfi, ncfi;
L
Linus Torvalds 已提交
1094 1095
	struct buffer_head *dir_bh = NULL;
	int retval = -ENOENT;
1096
	struct kernel_lb_addr tloc;
1097
	struct udf_inode_info *old_iinfo = UDF_I(old_inode);
L
Linus Torvalds 已提交
1098

1099
	ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
1100 1101 1102
	if (IS_ERR(ofi)) {
		retval = PTR_ERR(ofi);
		goto end_rename;
L
Linus Torvalds 已提交
1103
	}
1104 1105 1106 1107 1108

	if (ofibh.sbh != ofibh.ebh)
		brelse(ofibh.ebh);

	brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1109
	tloc = lelb_to_cpu(ocfi.icb.extLocation);
1110
	if (!ofi || udf_get_lb_pblock(old_dir->i_sb, &tloc, 0)
1111
	    != old_inode->i_ino)
L
Linus Torvalds 已提交
1112 1113
		goto end_rename;

1114
	nfi = udf_find_entry(new_dir, &new_dentry->d_name, &nfibh, &ncfi);
1115 1116 1117 1118 1119 1120 1121 1122 1123
	if (IS_ERR(nfi)) {
		retval = PTR_ERR(nfi);
		goto end_rename;
	}
	if (nfi && !new_inode) {
		if (nfibh.sbh != nfibh.ebh)
			brelse(nfibh.ebh);
		brelse(nfibh.sbh);
		nfi = NULL;
L
Linus Torvalds 已提交
1124
	}
1125
	if (S_ISDIR(old_inode->i_mode)) {
M
Marcin Slusarz 已提交
1126
		int offset = udf_ext0_offset(old_inode);
L
Linus Torvalds 已提交
1127

1128
		if (new_inode) {
L
Linus Torvalds 已提交
1129 1130 1131 1132 1133
			retval = -ENOTEMPTY;
			if (!empty_dir(new_inode))
				goto end_rename;
		}
		retval = -EIO;
1134
		if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
M
Marcin Slusarz 已提交
1135
			dir_fi = udf_get_fileident(
1136 1137
					old_iinfo->i_ext.i_data -
					  (old_iinfo->i_efe ?
M
Marcin Slusarz 已提交
1138 1139 1140
					   sizeof(struct extendedFileEntry) :
					   sizeof(struct fileEntry)),
					old_inode->i_sb->s_blocksize, &offset);
1141
		} else {
L
Linus Torvalds 已提交
1142 1143 1144
			dir_bh = udf_bread(old_inode, 0, 0, &retval);
			if (!dir_bh)
				goto end_rename;
M
Marcin Slusarz 已提交
1145 1146
			dir_fi = udf_get_fileident(dir_bh->b_data,
					old_inode->i_sb->s_blocksize, &offset);
L
Linus Torvalds 已提交
1147 1148 1149 1150
		}
		if (!dir_fi)
			goto end_rename;
		tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1151
		if (udf_get_lb_pblock(old_inode->i_sb, &tloc, 0) !=
M
Marcin Slusarz 已提交
1152
				old_dir->i_ino)
L
Linus Torvalds 已提交
1153 1154
			goto end_rename;
	}
1155
	if (!nfi) {
M
Marcin Slusarz 已提交
1156 1157
		nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
				    &retval);
L
Linus Torvalds 已提交
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
		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;
1174
	memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(struct long_ad));
L
Linus Torvalds 已提交
1175 1176 1177
	udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);

	/* The old fid may have moved - find it again */
1178
	ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
L
Linus Torvalds 已提交
1179 1180
	udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);

1181
	if (new_inode) {
L
Linus Torvalds 已提交
1182
		new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1183
		inode_dec_link_count(new_inode);
L
Linus Torvalds 已提交
1184 1185
	}
	old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1186
	new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
L
Linus Torvalds 已提交
1187
	mark_inode_dirty(old_dir);
1188
	mark_inode_dirty(new_dir);
L
Linus Torvalds 已提交
1189

1190
	if (dir_fi) {
1191
		dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
M
Marcin Slusarz 已提交
1192 1193 1194
		udf_update_tag((char *)dir_fi,
				(sizeof(struct fileIdentDesc) +
				le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1195
		if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1196
			mark_inode_dirty(old_inode);
M
Marcin Slusarz 已提交
1197
		else
L
Linus Torvalds 已提交
1198
			mark_buffer_dirty_inode(dir_bh, old_inode);
M
Marcin Slusarz 已提交
1199

1200
		inode_dec_link_count(old_dir);
M
Marcin Slusarz 已提交
1201
		if (new_inode)
1202
			inode_dec_link_count(new_inode);
M
Marcin Slusarz 已提交
1203
		else {
1204
			inc_nlink(new_dir);
L
Linus Torvalds 已提交
1205 1206 1207 1208
			mark_inode_dirty(new_dir);
		}
	}

1209
	if (ofi) {
L
Linus Torvalds 已提交
1210
		if (ofibh.sbh != ofibh.ebh)
J
Jan Kara 已提交
1211 1212
			brelse(ofibh.ebh);
		brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1213 1214 1215 1216
	}

	retval = 0;

1217
end_rename:
J
Jan Kara 已提交
1218
	brelse(dir_bh);
1219
	if (nfi) {
L
Linus Torvalds 已提交
1220
		if (nfibh.sbh != nfibh.ebh)
J
Jan Kara 已提交
1221 1222
			brelse(nfibh.ebh);
		brelse(nfibh.sbh);
L
Linus Torvalds 已提交
1223
	}
1224

L
Linus Torvalds 已提交
1225 1226 1227
	return retval;
}

R
Rasmus Rohde 已提交
1228 1229
static struct dentry *udf_get_parent(struct dentry *child)
{
1230
	struct kernel_lb_addr tloc;
R
Rasmus Rohde 已提交
1231
	struct inode *inode = NULL;
1232
	struct qstr dotdot = QSTR_INIT("..", 2);
R
Rasmus Rohde 已提交
1233 1234 1235
	struct fileIdentDesc cfi;
	struct udf_fileident_bh fibh;

1236
	if (!udf_find_entry(d_inode(child), &dotdot, &fibh, &cfi))
1237
		return ERR_PTR(-EACCES);
R
Rasmus Rohde 已提交
1238 1239 1240 1241 1242

	if (fibh.sbh != fibh.ebh)
		brelse(fibh.ebh);
	brelse(fibh.sbh);

1243
	tloc = lelb_to_cpu(cfi.icb.extLocation);
1244
	inode = udf_iget(d_inode(child)->i_sb, &tloc);
1245 1246
	if (IS_ERR(inode))
		return ERR_CAST(inode);
R
Rasmus Rohde 已提交
1247

1248
	return d_obtain_alias(inode);
R
Rasmus Rohde 已提交
1249 1250 1251 1252 1253 1254 1255
}


static struct dentry *udf_nfs_get_inode(struct super_block *sb, u32 block,
					u16 partref, __u32 generation)
{
	struct inode *inode;
1256
	struct kernel_lb_addr loc;
R
Rasmus Rohde 已提交
1257 1258 1259 1260 1261 1262

	if (block == 0)
		return ERR_PTR(-ESTALE);

	loc.logicalBlockNum = block;
	loc.partitionReferenceNum = partref;
1263
	inode = udf_iget(sb, &loc);
R
Rasmus Rohde 已提交
1264

1265 1266
	if (IS_ERR(inode))
		return ERR_CAST(inode);
R
Rasmus Rohde 已提交
1267 1268 1269 1270 1271

	if (generation && inode->i_generation != generation) {
		iput(inode);
		return ERR_PTR(-ESTALE);
	}
1272
	return d_obtain_alias(inode);
R
Rasmus Rohde 已提交
1273 1274 1275 1276 1277
}

static struct dentry *udf_fh_to_dentry(struct super_block *sb,
				       struct fid *fid, int fh_len, int fh_type)
{
N
NeilBrown 已提交
1278
	if (fh_len < 3 ||
R
Rasmus Rohde 已提交
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
	    (fh_type != FILEID_UDF_WITH_PARENT &&
	     fh_type != FILEID_UDF_WITHOUT_PARENT))
		return NULL;

	return udf_nfs_get_inode(sb, fid->udf.block, fid->udf.partref,
			fid->udf.generation);
}

static struct dentry *udf_fh_to_parent(struct super_block *sb,
				       struct fid *fid, int fh_len, int fh_type)
{
N
NeilBrown 已提交
1290
	if (fh_len < 5 || fh_type != FILEID_UDF_WITH_PARENT)
R
Rasmus Rohde 已提交
1291 1292 1293 1294 1295 1296
		return NULL;

	return udf_nfs_get_inode(sb, fid->udf.parent_block,
				 fid->udf.parent_partref,
				 fid->udf.parent_generation);
}
A
Al Viro 已提交
1297 1298
static int udf_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
			 struct inode *parent)
R
Rasmus Rohde 已提交
1299 1300
{
	int len = *lenp;
1301
	struct kernel_lb_addr location = UDF_I(inode)->i_location;
R
Rasmus Rohde 已提交
1302 1303 1304
	struct fid *fid = (struct fid *)fh;
	int type = FILEID_UDF_WITHOUT_PARENT;

A
Al Viro 已提交
1305
	if (parent && (len < 5)) {
1306
		*lenp = 5;
1307
		return FILEID_INVALID;
1308 1309
	} else if (len < 3) {
		*lenp = 3;
1310
		return FILEID_INVALID;
1311
	}
R
Rasmus Rohde 已提交
1312 1313 1314 1315

	*lenp = 3;
	fid->udf.block = location.logicalBlockNum;
	fid->udf.partref = location.partitionReferenceNum;
M
Mathias Krause 已提交
1316
	fid->udf.parent_partref = 0;
R
Rasmus Rohde 已提交
1317 1318
	fid->udf.generation = inode->i_generation;

A
Al Viro 已提交
1319 1320
	if (parent) {
		location = UDF_I(parent)->i_location;
R
Rasmus Rohde 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
		fid->udf.parent_block = location.logicalBlockNum;
		fid->udf.parent_partref = location.partitionReferenceNum;
		fid->udf.parent_generation = inode->i_generation;
		*lenp = 5;
		type = FILEID_UDF_WITH_PARENT;
	}

	return type;
}

const struct export_operations udf_export_ops = {
	.encode_fh	= udf_encode_fh,
	.fh_to_dentry   = udf_fh_to_dentry,
	.fh_to_parent   = udf_fh_to_parent,
	.get_parent     = udf_get_parent,
};

1338
const struct inode_operations udf_dir_inode_operations = {
1339 1340 1341 1342 1343 1344 1345 1346 1347
	.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,
A
Al Viro 已提交
1348
	.tmpfile			= udf_tmpfile,
L
Linus Torvalds 已提交
1349
};