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

A
Al Viro 已提交
35 36
static inline int udf_match(int len1, const unsigned char *name1, int len2,
			    const unsigned 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
		 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
M
Marcin Slusarz 已提交
46
		 uint8_t *impuse, uint8_t *fileident)
L
Linus Torvalds 已提交
47
{
48
	uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(struct tag);
L
Linus Torvalds 已提交
49 50 51 52 53
	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 -
54
		sizeof(struct fileIdentDesc);
L
Linus Torvalds 已提交
55 56
	int adinicb = 0;

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

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

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

	offset += liu;

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

	offset += lfi;

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

100 101
	crc = crc_itu_t(0, (uint8_t *)cfi + sizeof(struct tag),
		      sizeof(struct fileIdentDesc) - sizeof(struct tag));
102 103

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

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

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

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

142
static struct fileIdentDesc *udf_find_entry(struct inode *dir,
A
Al Viro 已提交
143
					    const struct qstr *child,
144 145
					    struct udf_fileident_bh *fibh,
					    struct fileIdentDesc *cfi)
L
Linus Torvalds 已提交
146
{
147
	struct fileIdentDesc *fi = NULL;
L
Linus Torvalds 已提交
148 149
	loff_t f_pos;
	int block, flen;
A
Al Viro 已提交
150 151
	unsigned char *fname = NULL;
	unsigned char *nameptr;
L
Linus Torvalds 已提交
152 153
	uint8_t lfi;
	uint16_t liu;
154
	loff_t size;
155
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
156
	uint32_t elen;
157
	sector_t offset;
158
	struct extent_position epos = {};
159
	struct udf_inode_info *dinfo = UDF_I(dir);
160 161
	int isdotdot = child->len == 2 &&
		child->name[0] == '.' && child->name[1] == '.';
J
Jan Kara 已提交
162
	struct super_block *sb = dir->i_sb;
L
Linus Torvalds 已提交
163

164 165
	size = udf_ext0_offset(dir) + dir->i_size;
	f_pos = udf_ext0_offset(dir);
L
Linus Torvalds 已提交
166

167
	fibh->sbh = fibh->ebh = NULL;
J
Jan Kara 已提交
168
	fibh->soffset = fibh->eoffset = f_pos & (sb->s_blocksize - 1);
169
	if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
J
Jan Kara 已提交
170
		if (inode_bmap(dir, f_pos >> sb->s_blocksize_bits, &epos,
171 172
		    &eloc, &elen, &offset) != (EXT_RECORDED_ALLOCATED >> 30))
			goto out_err;
J
Jan Kara 已提交
173 174
		block = udf_get_lb_pblock(sb, &eloc, offset);
		if ((++offset << sb->s_blocksize_bits) < elen) {
175
			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
176
				epos.offset -= sizeof(struct short_ad);
177
			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
178
				epos.offset -= sizeof(struct long_ad);
M
Marcin Slusarz 已提交
179
		} else
L
Linus Torvalds 已提交
180 181
			offset = 0;

J
Jan Kara 已提交
182
		fibh->sbh = fibh->ebh = udf_tread(sb, block);
183 184
		if (!fibh->sbh)
			goto out_err;
L
Linus Torvalds 已提交
185 186
	}

187 188 189 190
	fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
	if (!fname)
		goto out_err;

191
	while (f_pos < size) {
192 193
		fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
					&elen, &offset);
194 195
		if (!fi)
			goto out_err;
L
Linus Torvalds 已提交
196 197 198 199

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

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

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

220
		if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
J
Jan Kara 已提交
221
			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
L
Linus Torvalds 已提交
222 223
				continue;
		}
224 225

		if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
J
Jan Kara 已提交
226
			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
L
Linus Torvalds 已提交
227 228 229
				continue;
		}

R
Rasmus Rohde 已提交
230
		if ((cfi->fileCharacteristics & FID_FILE_CHAR_PARENT) &&
231 232
		    isdotdot)
			goto out_ok;
R
Rasmus Rohde 已提交
233

L
Linus Torvalds 已提交
234 235 236
		if (!lfi)
			continue;

J
Jan Kara 已提交
237
		flen = udf_get_filename(sb, nameptr, lfi, fname, UDF_NAME_LEN);
238
		if (flen && udf_match(flen, fname, child->len, child->name))
239
			goto out_ok;
L
Linus Torvalds 已提交
240
	}
241

242 243
out_err:
	fi = NULL;
L
Linus Torvalds 已提交
244
	if (fibh->sbh != fibh->ebh)
J
Jan Kara 已提交
245 246
		brelse(fibh->ebh);
	brelse(fibh->sbh);
247
out_ok:
J
Jan Kara 已提交
248
	brelse(epos.bh);
249
	kfree(fname);
250

251
	return fi;
L
Linus Torvalds 已提交
252 253
}

254
static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
A
Al Viro 已提交
255
				 unsigned int flags)
L
Linus Torvalds 已提交
256 257
{
	struct inode *inode = NULL;
258
	struct fileIdentDesc cfi;
L
Linus Torvalds 已提交
259 260
	struct udf_fileident_bh fibh;

261
	if (dentry->d_name.len > UDF_NAME_LEN - 2)
L
Linus Torvalds 已提交
262 263 264 265
		return ERR_PTR(-ENAMETOOLONG);

#ifdef UDF_RECOVERY
	/* temporary shorthand for specifying files by inode number */
266
	if (!strncmp(dentry->d_name.name, ".B=", 3)) {
267
		struct kernel_lb_addr lb = {
268
			.logicalBlockNum = 0,
M
Marcin Slusarz 已提交
269 270 271
			.partitionReferenceNum =
				simple_strtoul(dentry->d_name.name + 3,
						NULL, 0),
272
		};
L
Linus Torvalds 已提交
273
		inode = udf_iget(dir->i_sb, lb);
274 275
		if (IS_ERR(inode))
			return inode;
M
Marcin Slusarz 已提交
276
	} else
277
#endif /* UDF_RECOVERY */
L
Linus Torvalds 已提交
278

279
	if (udf_find_entry(dir, &dentry->d_name, &fibh, &cfi)) {
280 281
		struct kernel_lb_addr loc;

L
Linus Torvalds 已提交
282
		if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
283 284
			brelse(fibh.ebh);
		brelse(fibh.sbh);
L
Linus Torvalds 已提交
285

286 287
		loc = lelb_to_cpu(cfi.icb.extLocation);
		inode = udf_iget(dir->i_sb, &loc);
288 289
		if (IS_ERR(inode))
			return ERR_CAST(inode);
L
Linus Torvalds 已提交
290
	}
291

R
Rasmus Rohde 已提交
292
	return d_splice_alias(inode, dentry);
L
Linus Torvalds 已提交
293 294
}

295 296 297 298
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 已提交
299
{
M
Marcin Slusarz 已提交
300
	struct super_block *sb = dir->i_sb;
301
	struct fileIdentDesc *fi = NULL;
A
Al Viro 已提交
302
	unsigned char *name = NULL;
L
Linus Torvalds 已提交
303 304
	int namelen;
	loff_t f_pos;
305
	loff_t size = udf_ext0_offset(dir) + dir->i_size;
L
Linus Torvalds 已提交
306 307 308 309
	int nfidlen;
	uint8_t lfi;
	uint16_t liu;
	int block;
310
	struct kernel_lb_addr eloc;
311
	uint32_t elen = 0;
312
	sector_t offset;
313
	struct extent_position epos = {};
314
	struct udf_inode_info *dinfo;
L
Linus Torvalds 已提交
315

316 317 318 319 320 321 322
	fibh->sbh = fibh->ebh = NULL;
	name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
	if (!name) {
		*err = -ENOMEM;
		goto out_err;
	}

323 324
	if (dentry) {
		if (!dentry->d_name.len) {
L
Linus Torvalds 已提交
325
			*err = -EINVAL;
326
			goto out_err;
L
Linus Torvalds 已提交
327
		}
M
Marcin Slusarz 已提交
328 329 330
		namelen = udf_put_filename(sb, dentry->d_name.name, name,
						 dentry->d_name.len);
		if (!namelen) {
L
Linus Torvalds 已提交
331
			*err = -ENAMETOOLONG;
332
			goto out_err;
L
Linus Torvalds 已提交
333
		}
334
	} else {
L
Linus Torvalds 已提交
335
		namelen = 0;
336
	}
L
Linus Torvalds 已提交
337 338 339

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

340
	f_pos = udf_ext0_offset(dir);
L
Linus Torvalds 已提交
341

342
	fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
343
	dinfo = UDF_I(dir);
344 345 346 347
	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,
348
					&dinfo->i_location, 0);
349 350 351
			fibh->soffset = fibh->eoffset = sb->s_blocksize;
			goto add;
		}
352
		block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
353
		if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
354
			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
355
				epos.offset -= sizeof(struct short_ad);
356
			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
357
				epos.offset -= sizeof(struct long_ad);
M
Marcin Slusarz 已提交
358
		} else
L
Linus Torvalds 已提交
359 360
			offset = 0;

M
Marcin Slusarz 已提交
361 362
		fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
		if (!fibh->sbh) {
L
Linus Torvalds 已提交
363
			*err = -EIO;
364
			goto out_err;
L
Linus Torvalds 已提交
365 366
		}

367
		block = dinfo->i_location.logicalBlockNum;
L
Linus Torvalds 已提交
368 369
	}

370
	while (f_pos < size) {
371 372
		fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
					&elen, &offset);
L
Linus Torvalds 已提交
373

374
		if (!fi) {
L
Linus Torvalds 已提交
375
			*err = -EIO;
376
			goto out_err;
L
Linus Torvalds 已提交
377 378 379 380 381
		}

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

382
		if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
M
Marcin Slusarz 已提交
383 384
			if (((sizeof(struct fileIdentDesc) +
					liu + lfi + 3) & ~3) == nfidlen) {
L
Linus Torvalds 已提交
385 386 387 388 389
				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 已提交
390 391
				if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
						  name))
392
					goto out_ok;
M
Marcin Slusarz 已提交
393
				else {
L
Linus Torvalds 已提交
394
					*err = -EIO;
395
					goto out_err;
L
Linus Torvalds 已提交
396 397 398 399 400
				}
			}
		}
	}

401
add:
L
Linus Torvalds 已提交
402 403
	f_pos += nfidlen;

404
	if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
405
	    sb->s_blocksize - fibh->eoffset < nfidlen) {
J
Jan Kara 已提交
406
		brelse(epos.bh);
J
Jan Kara 已提交
407
		epos.bh = NULL;
L
Linus Torvalds 已提交
408 409
		fibh->soffset -= udf_ext0_offset(dir);
		fibh->eoffset -= udf_ext0_offset(dir);
410
		f_pos -= udf_ext0_offset(dir);
L
Linus Torvalds 已提交
411
		if (fibh->sbh != fibh->ebh)
J
Jan Kara 已提交
412 413
			brelse(fibh->ebh);
		brelse(fibh->sbh);
M
Marcin Slusarz 已提交
414 415 416
		fibh->sbh = fibh->ebh =
				udf_expand_dir_adinicb(dir, &block, err);
		if (!fibh->sbh)
417
			goto out_err;
418
		epos.block = dinfo->i_location;
J
Jan Kara 已提交
419
		epos.offset = udf_file_entry_alloc_offset(dir);
J
Jan Kara 已提交
420 421
		/* Load extent udf_expand_dir_adinicb() has created */
		udf_current_aext(dir, &epos, &eloc, &elen, 1);
L
Linus Torvalds 已提交
422 423
	}

J
Jan Kara 已提交
424
	/* Entry fits into current block? */
425
	if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
L
Linus Torvalds 已提交
426 427
		fibh->soffset = fibh->eoffset;
		fibh->eoffset += nfidlen;
428
		if (fibh->sbh != fibh->ebh) {
J
Jan Kara 已提交
429
			brelse(fibh->sbh);
L
Linus Torvalds 已提交
430 431 432
			fibh->sbh = fibh->ebh;
		}

433 434
		if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
			block = dinfo->i_location.logicalBlockNum;
M
Marcin Slusarz 已提交
435
			fi = (struct fileIdentDesc *)
436
					(dinfo->i_ext.i_data +
437
					 fibh->soffset -
M
Marcin Slusarz 已提交
438
					 udf_ext0_offset(dir) +
439
					 dinfo->i_lenEAttr);
440
		} else {
M
Marcin Slusarz 已提交
441 442 443 444 445
			block = eloc.logicalBlockNum +
					((elen - 1) >>
						dir->i_sb->s_blocksize_bits);
			fi = (struct fileIdentDesc *)
				(fibh->sbh->b_data + fibh->soffset);
L
Linus Torvalds 已提交
446
		}
447
	} else {
J
Jan Kara 已提交
448 449 450 451 452 453 454 455 456 457
		/* 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 已提交
458 459
		fibh->soffset = fibh->eoffset - sb->s_blocksize;
		fibh->eoffset += nfidlen - sb->s_blocksize;
460
		if (fibh->sbh != fibh->ebh) {
J
Jan Kara 已提交
461
			brelse(fibh->sbh);
L
Linus Torvalds 已提交
462 463 464 465
			fibh->sbh = fibh->ebh;
		}

		block = eloc.logicalBlockNum + ((elen - 1) >>
466
						dir->i_sb->s_blocksize_bits);
M
Marcin Slusarz 已提交
467
		fibh->ebh = udf_bread(dir,
468
				f_pos >> dir->i_sb->s_blocksize_bits, 1, err);
469 470
		if (!fibh->ebh)
			goto out_err;
471 472 473 474 475
		/* 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 已提交
476

477
		if (!fibh->soffset) {
478 479 480 481 482
			/* Find the freshly allocated block */
			while (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
				(EXT_RECORDED_ALLOCATED >> 30))
				;
			block = eloc.logicalBlockNum + ((elen - 1) >>
483
					dir->i_sb->s_blocksize_bits);
J
Jan Kara 已提交
484
			brelse(fibh->sbh);
L
Linus Torvalds 已提交
485 486
			fibh->sbh = fibh->ebh;
			fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
487
		} else {
L
Linus Torvalds 已提交
488
			fi = (struct fileIdentDesc *)
M
Marcin Slusarz 已提交
489 490
				(fibh->sbh->b_data + sb->s_blocksize +
					fibh->soffset);
L
Linus Torvalds 已提交
491 492 493 494
		}
	}

	memset(cfi, 0, sizeof(struct fileIdentDesc));
M
Marcin Slusarz 已提交
495
	if (UDF_SB(sb)->s_udfrev >= 0x0200)
M
Marcin Slusarz 已提交
496
		udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
497
			    sizeof(struct tag));
L
Linus Torvalds 已提交
498
	else
M
Marcin Slusarz 已提交
499
		udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
500
			    sizeof(struct tag));
L
Linus Torvalds 已提交
501 502 503
	cfi->fileVersionNum = cpu_to_le16(1);
	cfi->lengthFileIdent = namelen;
	cfi->lengthOfImpUse = cpu_to_le16(0);
504
	if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
L
Linus Torvalds 已提交
505
		dir->i_size += nfidlen;
506 507
		if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
			dinfo->i_lenAlloc += nfidlen;
J
Jan Kara 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521
		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 已提交
522
		mark_inode_dirty(dir);
523
		goto out_ok;
524
	} else {
L
Linus Torvalds 已提交
525
		*err = -EIO;
526
		goto out_err;
L
Linus Torvalds 已提交
527
	}
528 529 530 531 532 533 534 535 536 537

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 已提交
538 539 540
}

static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
541 542
			    struct udf_fileident_bh *fibh,
			    struct fileIdentDesc *cfi)
L
Linus Torvalds 已提交
543 544
{
	cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
545

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

L
Linus Torvalds 已提交
549 550 551
	return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
}

552
static int udf_add_nondir(struct dentry *dentry, struct inode *inode)
L
Linus Torvalds 已提交
553
{
554 555
	struct udf_inode_info *iinfo = UDF_I(inode);
	struct inode *dir = dentry->d_parent->d_inode;
L
Linus Torvalds 已提交
556 557 558 559
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;

M
Marcin Slusarz 已提交
560
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
561
	if (unlikely(!fi)) {
562
		inode_dec_link_count(inode);
563
		unlock_new_inode(inode);
L
Linus Torvalds 已提交
564 565 566 567
		iput(inode);
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
568
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
569
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
570
		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
571
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
572
	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
573 574
		mark_inode_dirty(dir);
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
575 576
		brelse(fibh.ebh);
	brelse(fibh.sbh);
577
	unlock_new_inode(inode);
L
Linus Torvalds 已提交
578
	d_instantiate(dentry, inode);
579

L
Linus Torvalds 已提交
580 581 582
	return 0;
}

583 584 585
static int udf_create(struct inode *dir, struct dentry *dentry, umode_t mode,
		      bool excl)
{
586
	struct inode *inode = udf_new_inode(dir, mode);
587

588 589
	if (IS_ERR(inode))
		return PTR_ERR(inode);
590 591 592 593 594 595 596 597 598 599 600 601

	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 已提交
602 603
static int udf_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
{
604
	struct inode *inode = udf_new_inode(dir, mode);
A
Al Viro 已提交
605

606 607
	if (IS_ERR(inode))
		return PTR_ERR(inode);
A
Al Viro 已提交
608

609
	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
A
Al Viro 已提交
610 611 612 613 614 615 616
		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);
617
	unlock_new_inode(inode);
A
Al Viro 已提交
618 619 620
	return 0;
}

A
Al Viro 已提交
621
static int udf_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
622
		     dev_t rdev)
L
Linus Torvalds 已提交
623
{
624
	struct inode *inode;
L
Linus Torvalds 已提交
625 626 627 628

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

629 630 631
	inode = udf_new_inode(dir, mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);
632

633 634
	init_special_inode(inode, mode, rdev);
	return udf_add_nondir(dentry, inode);
L
Linus Torvalds 已提交
635 636
}

637
static int udf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
L
Linus Torvalds 已提交
638
{
639
	struct inode *inode;
L
Linus Torvalds 已提交
640 641 642
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;
643 644
	struct udf_inode_info *dinfo = UDF_I(dir);
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
645

646 647 648
	inode = udf_new_inode(dir, S_IFDIR | mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);
L
Linus Torvalds 已提交
649

650
	iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
651 652
	inode->i_op = &udf_dir_inode_operations;
	inode->i_fop = &udf_dir_operations;
M
Marcin Slusarz 已提交
653 654
	fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
	if (!fi) {
655
		inode_dec_link_count(inode);
656
		unlock_new_inode(inode);
L
Linus Torvalds 已提交
657 658 659
		iput(inode);
		goto out;
	}
M
Miklos Szeredi 已提交
660
	set_nlink(inode, 2);
L
Linus Torvalds 已提交
661
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
662
	cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
663
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
664
		cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
M
Marcin Slusarz 已提交
665 666
	cfi.fileCharacteristics =
			FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
L
Linus Torvalds 已提交
667
	udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
J
Jan Kara 已提交
668
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
669 670
	mark_inode_dirty(inode);

M
Marcin Slusarz 已提交
671 672
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi) {
673
		clear_nlink(inode);
L
Linus Torvalds 已提交
674
		mark_inode_dirty(inode);
675
		unlock_new_inode(inode);
L
Linus Torvalds 已提交
676 677 678 679
		iput(inode);
		goto out;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
680
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
681
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
682
		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
683 684
	cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
685
	inc_nlink(dir);
L
Linus Torvalds 已提交
686
	mark_inode_dirty(dir);
687
	unlock_new_inode(inode);
L
Linus Torvalds 已提交
688 689
	d_instantiate(dentry, inode);
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
690 691
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
692
	err = 0;
693 694

out:
L
Linus Torvalds 已提交
695 696 697 698 699 700 701 702
	return err;
}

static int empty_dir(struct inode *dir)
{
	struct fileIdentDesc *fi, cfi;
	struct udf_fileident_bh fibh;
	loff_t f_pos;
703
	loff_t size = udf_ext0_offset(dir) + dir->i_size;
L
Linus Torvalds 已提交
704
	int block;
705
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
706
	uint32_t elen;
707
	sector_t offset;
708
	struct extent_position epos = {};
709
	struct udf_inode_info *dinfo = UDF_I(dir);
L
Linus Torvalds 已提交
710

711 712
	f_pos = udf_ext0_offset(dir);
	fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
L
Linus Torvalds 已提交
713

714
	if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
715
		fibh.sbh = fibh.ebh = NULL;
716
	else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
M
Marcin Slusarz 已提交
717 718
			      &epos, &eloc, &elen, &offset) ==
					(EXT_RECORDED_ALLOCATED >> 30)) {
719
		block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
720
		if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
721
			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
722
				epos.offset -= sizeof(struct short_ad);
723
			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
724
				epos.offset -= sizeof(struct long_ad);
M
Marcin Slusarz 已提交
725
		} else
L
Linus Torvalds 已提交
726 727
			offset = 0;

M
Marcin Slusarz 已提交
728 729
		fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
		if (!fibh.sbh) {
J
Jan Kara 已提交
730
			brelse(epos.bh);
L
Linus Torvalds 已提交
731 732
			return 0;
		}
733
	} else {
J
Jan Kara 已提交
734
		brelse(epos.bh);
L
Linus Torvalds 已提交
735 736 737
		return 0;
	}

738
	while (f_pos < size) {
739 740 741
		fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
					&elen, &offset);
		if (!fi) {
L
Linus Torvalds 已提交
742
			if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
743 744 745
				brelse(fibh.ebh);
			brelse(fibh.sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
746 747 748
			return 0;
		}

749 750
		if (cfi.lengthFileIdent &&
		    (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
L
Linus Torvalds 已提交
751
			if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
752 753 754
				brelse(fibh.ebh);
			brelse(fibh.sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
755 756 757
			return 0;
		}
	}
758

L
Linus Torvalds 已提交
759
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
760 761 762
		brelse(fibh.ebh);
	brelse(fibh.sbh);
	brelse(epos.bh);
763

L
Linus Torvalds 已提交
764 765 766
	return 1;
}

767
static int udf_rmdir(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
768 769
{
	int retval;
770
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
771 772
	struct udf_fileident_bh fibh;
	struct fileIdentDesc *fi, cfi;
773
	struct kernel_lb_addr tloc;
L
Linus Torvalds 已提交
774 775

	retval = -ENOENT;
776
	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
L
Linus Torvalds 已提交
777 778 779 780 781
	if (!fi)
		goto out;

	retval = -EIO;
	tloc = lelb_to_cpu(cfi.icb.extLocation);
782
	if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
L
Linus Torvalds 已提交
783 784 785 786 787 788 789 790
		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 已提交
791 792
		udf_warn(inode->i_sb, "empty directory has nlink != 2 (%d)\n",
			 inode->i_nlink);
793
	clear_nlink(inode);
L
Linus Torvalds 已提交
794
	inode->i_size = 0;
795
	inode_dec_link_count(dir);
M
Marcin Slusarz 已提交
796 797
	inode->i_ctime = dir->i_ctime = dir->i_mtime =
						current_fs_time(dir->i_sb);
L
Linus Torvalds 已提交
798 799
	mark_inode_dirty(dir);

800
end_rmdir:
L
Linus Torvalds 已提交
801
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
802 803
		brelse(fibh.ebh);
	brelse(fibh.sbh);
804 805

out:
L
Linus Torvalds 已提交
806 807 808
	return retval;
}

809
static int udf_unlink(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
810 811
{
	int retval;
812
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
813 814 815
	struct udf_fileident_bh fibh;
	struct fileIdentDesc *fi;
	struct fileIdentDesc cfi;
816
	struct kernel_lb_addr tloc;
L
Linus Torvalds 已提交
817 818

	retval = -ENOENT;
819
	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
L
Linus Torvalds 已提交
820 821 822 823 824
	if (!fi)
		goto out;

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

828
	if (!inode->i_nlink) {
L
Linus Torvalds 已提交
829
		udf_debug("Deleting nonexistent file (%lu), %d\n",
830
			  inode->i_ino, inode->i_nlink);
M
Miklos Szeredi 已提交
831
		set_nlink(inode, 1);
L
Linus Torvalds 已提交
832 833 834 835 836 837
	}
	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);
838
	inode_dec_link_count(inode);
L
Linus Torvalds 已提交
839 840 841
	inode->i_ctime = dir->i_ctime;
	retval = 0;

842
end_unlink:
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 852
static int udf_symlink(struct inode *dir, struct dentry *dentry,
		       const char *symname)
L
Linus Torvalds 已提交
853
{
854
	struct inode *inode = udf_new_inode(dir, S_IFLNK | S_IRWXUGO);
L
Linus Torvalds 已提交
855
	struct pathComponent *pc;
A
Al Viro 已提交
856
	const char *compstart;
857
	struct extent_position epos = {};
L
Linus Torvalds 已提交
858
	int eoffset, elen = 0;
A
Al Viro 已提交
859
	uint8_t *ea;
L
Linus Torvalds 已提交
860 861
	int err;
	int block;
A
Al Viro 已提交
862
	unsigned char *name = NULL;
L
Linus Torvalds 已提交
863
	int namelen;
864
	struct udf_inode_info *iinfo;
865
	struct super_block *sb = dir->i_sb;
L
Linus Torvalds 已提交
866

867 868
	if (IS_ERR(inode))
		return PTR_ERR(inode);
L
Linus Torvalds 已提交
869

870 871
	iinfo = UDF_I(inode);
	down_write(&iinfo->i_data_sem);
872 873 874 875 876 877
	name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
	if (!name) {
		err = -ENOMEM;
		goto out_no_entry;
	}

L
Linus Torvalds 已提交
878
	inode->i_data.a_ops = &udf_symlink_aops;
879
	inode->i_op = &udf_symlink_inode_operations;
L
Linus Torvalds 已提交
880

881
	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
882
		struct kernel_lb_addr eloc;
883
		uint32_t bsize;
L
Linus Torvalds 已提交
884

885
		block = udf_new_block(sb, inode,
886 887
				iinfo->i_location.partitionReferenceNum,
				iinfo->i_location.logicalBlockNum, &err);
L
Linus Torvalds 已提交
888 889
		if (!block)
			goto out_no_entry;
890
		epos.block = iinfo->i_location;
J
Jan Kara 已提交
891 892
		epos.offset = udf_file_entry_alloc_offset(inode);
		epos.bh = NULL;
L
Linus Torvalds 已提交
893
		eloc.logicalBlockNum = block;
M
Marcin Slusarz 已提交
894
		eloc.partitionReferenceNum =
895
				iinfo->i_location.partitionReferenceNum;
896
		bsize = sb->s_blocksize;
897
		iinfo->i_lenExtents = bsize;
898
		udf_add_aext(inode, &epos, &eloc, bsize, 0);
J
Jan Kara 已提交
899
		brelse(epos.bh);
L
Linus Torvalds 已提交
900

901
		block = udf_get_pblock(sb, block,
902
				iinfo->i_location.partitionReferenceNum,
M
Marcin Slusarz 已提交
903
				0);
904
		epos.bh = udf_tgetblk(sb, block);
J
Jan Kara 已提交
905
		lock_buffer(epos.bh);
906
		memset(epos.bh->b_data, 0x00, bsize);
J
Jan Kara 已提交
907 908 909 910
		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);
911 912
	} else
		ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
L
Linus Torvalds 已提交
913

914
	eoffset = sb->s_blocksize - udf_ext0_offset(inode);
L
Linus Torvalds 已提交
915 916
	pc = (struct pathComponent *)ea;

917 918
	if (*symname == '/') {
		do {
L
Linus Torvalds 已提交
919 920 921 922 923 924 925 926 927 928 929
			symname++;
		} while (*symname == '/');

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

	err = -ENAMETOOLONG;

930
	while (*symname) {
L
Linus Torvalds 已提交
931 932 933 934 935
		if (elen + sizeof(struct pathComponent) > eoffset)
			goto out_no_entry;

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

A
Al Viro 已提交
936
		compstart = symname;
L
Linus Torvalds 已提交
937

938
		do {
L
Linus Torvalds 已提交
939 940 941 942 943 944
			symname++;
		} while (*symname && *symname != '/');

		pc->componentType = 5;
		pc->lengthComponentIdent = 0;
		pc->componentFileVersionNum = 0;
945 946
		if (compstart[0] == '.') {
			if ((symname - compstart) == 1)
L
Linus Torvalds 已提交
947
				pc->componentType = 4;
M
Marcin Slusarz 已提交
948 949
			else if ((symname - compstart) == 2 &&
					compstart[1] == '.')
L
Linus Torvalds 已提交
950 951 952
				pc->componentType = 3;
		}

953
		if (pc->componentType == 5) {
954
			namelen = udf_put_filename(sb, compstart, name,
955 956
						   symname - compstart);
			if (!namelen)
L
Linus Torvalds 已提交
957 958
				goto out_no_entry;

M
Marcin Slusarz 已提交
959 960
			if (elen + sizeof(struct pathComponent) + namelen >
					eoffset)
L
Linus Torvalds 已提交
961 962 963 964 965 966 967 968 969
				goto out_no_entry;
			else
				pc->lengthComponentIdent = namelen;

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

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

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

J
Jan Kara 已提交
977
	brelse(epos.bh);
L
Linus Torvalds 已提交
978
	inode->i_size = elen;
979 980
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
		iinfo->i_lenAlloc = inode->i_size;
J
Jan Kara 已提交
981 982
	else
		udf_truncate_tail_extent(inode);
L
Linus Torvalds 已提交
983
	mark_inode_dirty(inode);
984
	up_write(&iinfo->i_data_sem);
L
Linus Torvalds 已提交
985

986
	err = udf_add_nondir(dentry, inode);
987
out:
988
	kfree(name);
L
Linus Torvalds 已提交
989 990
	return err;

991
out_no_entry:
992
	up_write(&iinfo->i_data_sem);
993
	inode_dec_link_count(inode);
994
	unlock_new_inode(inode);
L
Linus Torvalds 已提交
995 996 997 998
	iput(inode);
	goto out;
}

999 1000
static int udf_link(struct dentry *old_dentry, struct inode *dir,
		    struct dentry *dentry)
L
Linus Torvalds 已提交
1001 1002 1003 1004 1005 1006
{
	struct inode *inode = old_dentry->d_inode;
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;

M
Marcin Slusarz 已提交
1007 1008
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi) {
L
Linus Torvalds 已提交
1009 1010 1011
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1012
	cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1013
	if (UDF_SB(inode->i_sb)->s_lvid_bh) {
1014
		*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1015
			cpu_to_le32(lvid_get_unique_id(inode->i_sb));
L
Linus Torvalds 已提交
1016 1017
	}
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1018
	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1019
		mark_inode_dirty(dir);
1020

L
Linus Torvalds 已提交
1021
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
1022 1023
		brelse(fibh.ebh);
	brelse(fibh.sbh);
1024
	inc_nlink(inode);
L
Linus Torvalds 已提交
1025 1026
	inode->i_ctime = current_fs_time(inode->i_sb);
	mark_inode_dirty(inode);
A
Al Viro 已提交
1027
	ihold(inode);
L
Linus Torvalds 已提交
1028
	d_instantiate(dentry, inode);
1029

L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035
	return 0;
}

/* Anybody can rename anything with this: the permission checks are left to the
 * higher-level routines.
 */
1036 1037
static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
		      struct inode *new_dir, struct dentry *new_dentry)
L
Linus Torvalds 已提交
1038
{
1039 1040
	struct inode *old_inode = old_dentry->d_inode;
	struct inode *new_inode = new_dentry->d_inode;
L
Linus Torvalds 已提交
1041
	struct udf_fileident_bh ofibh, nfibh;
M
Marcin Slusarz 已提交
1042 1043
	struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
	struct fileIdentDesc ocfi, ncfi;
L
Linus Torvalds 已提交
1044 1045
	struct buffer_head *dir_bh = NULL;
	int retval = -ENOENT;
1046
	struct kernel_lb_addr tloc;
1047
	struct udf_inode_info *old_iinfo = UDF_I(old_inode);
L
Linus Torvalds 已提交
1048

1049
	ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
M
Marcin Slusarz 已提交
1050
	if (ofi) {
L
Linus Torvalds 已提交
1051
		if (ofibh.sbh != ofibh.ebh)
J
Jan Kara 已提交
1052 1053
			brelse(ofibh.ebh);
		brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1054 1055
	}
	tloc = lelb_to_cpu(ocfi.icb.extLocation);
1056
	if (!ofi || udf_get_lb_pblock(old_dir->i_sb, &tloc, 0)
1057
	    != old_inode->i_ino)
L
Linus Torvalds 已提交
1058 1059
		goto end_rename;

1060
	nfi = udf_find_entry(new_dir, &new_dentry->d_name, &nfibh, &ncfi);
1061 1062
	if (nfi) {
		if (!new_inode) {
L
Linus Torvalds 已提交
1063
			if (nfibh.sbh != nfibh.ebh)
J
Jan Kara 已提交
1064 1065
				brelse(nfibh.ebh);
			brelse(nfibh.sbh);
L
Linus Torvalds 已提交
1066 1067 1068
			nfi = NULL;
		}
	}
1069
	if (S_ISDIR(old_inode->i_mode)) {
M
Marcin Slusarz 已提交
1070
		int offset = udf_ext0_offset(old_inode);
L
Linus Torvalds 已提交
1071

1072
		if (new_inode) {
L
Linus Torvalds 已提交
1073 1074 1075 1076 1077
			retval = -ENOTEMPTY;
			if (!empty_dir(new_inode))
				goto end_rename;
		}
		retval = -EIO;
1078
		if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
M
Marcin Slusarz 已提交
1079
			dir_fi = udf_get_fileident(
1080 1081
					old_iinfo->i_ext.i_data -
					  (old_iinfo->i_efe ?
M
Marcin Slusarz 已提交
1082 1083 1084
					   sizeof(struct extendedFileEntry) :
					   sizeof(struct fileEntry)),
					old_inode->i_sb->s_blocksize, &offset);
1085
		} else {
L
Linus Torvalds 已提交
1086 1087 1088
			dir_bh = udf_bread(old_inode, 0, 0, &retval);
			if (!dir_bh)
				goto end_rename;
M
Marcin Slusarz 已提交
1089 1090
			dir_fi = udf_get_fileident(dir_bh->b_data,
					old_inode->i_sb->s_blocksize, &offset);
L
Linus Torvalds 已提交
1091 1092 1093 1094
		}
		if (!dir_fi)
			goto end_rename;
		tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1095
		if (udf_get_lb_pblock(old_inode->i_sb, &tloc, 0) !=
M
Marcin Slusarz 已提交
1096
				old_dir->i_ino)
L
Linus Torvalds 已提交
1097 1098
			goto end_rename;
	}
1099
	if (!nfi) {
M
Marcin Slusarz 已提交
1100 1101
		nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
				    &retval);
L
Linus Torvalds 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
		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;
1118
	memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(struct long_ad));
L
Linus Torvalds 已提交
1119 1120 1121
	udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);

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

1125
	if (new_inode) {
L
Linus Torvalds 已提交
1126
		new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1127
		inode_dec_link_count(new_inode);
L
Linus Torvalds 已提交
1128 1129 1130 1131
	}
	old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
	mark_inode_dirty(old_dir);

1132
	if (dir_fi) {
1133
		dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
M
Marcin Slusarz 已提交
1134 1135 1136
		udf_update_tag((char *)dir_fi,
				(sizeof(struct fileIdentDesc) +
				le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1137
		if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1138
			mark_inode_dirty(old_inode);
M
Marcin Slusarz 已提交
1139
		else
L
Linus Torvalds 已提交
1140
			mark_buffer_dirty_inode(dir_bh, old_inode);
M
Marcin Slusarz 已提交
1141

1142
		inode_dec_link_count(old_dir);
M
Marcin Slusarz 已提交
1143
		if (new_inode)
1144
			inode_dec_link_count(new_inode);
M
Marcin Slusarz 已提交
1145
		else {
1146
			inc_nlink(new_dir);
L
Linus Torvalds 已提交
1147 1148 1149 1150
			mark_inode_dirty(new_dir);
		}
	}

1151
	if (ofi) {
L
Linus Torvalds 已提交
1152
		if (ofibh.sbh != ofibh.ebh)
J
Jan Kara 已提交
1153 1154
			brelse(ofibh.ebh);
		brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1155 1156 1157 1158
	}

	retval = 0;

1159
end_rename:
J
Jan Kara 已提交
1160
	brelse(dir_bh);
1161
	if (nfi) {
L
Linus Torvalds 已提交
1162
		if (nfibh.sbh != nfibh.ebh)
J
Jan Kara 已提交
1163 1164
			brelse(nfibh.ebh);
		brelse(nfibh.sbh);
L
Linus Torvalds 已提交
1165
	}
1166

L
Linus Torvalds 已提交
1167 1168 1169
	return retval;
}

R
Rasmus Rohde 已提交
1170 1171
static struct dentry *udf_get_parent(struct dentry *child)
{
1172
	struct kernel_lb_addr tloc;
R
Rasmus Rohde 已提交
1173
	struct inode *inode = NULL;
1174
	struct qstr dotdot = QSTR_INIT("..", 2);
R
Rasmus Rohde 已提交
1175 1176 1177 1178
	struct fileIdentDesc cfi;
	struct udf_fileident_bh fibh;

	if (!udf_find_entry(child->d_inode, &dotdot, &fibh, &cfi))
1179
		return ERR_PTR(-EACCES);
R
Rasmus Rohde 已提交
1180 1181 1182 1183 1184

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

1185 1186
	tloc = lelb_to_cpu(cfi.icb.extLocation);
	inode = udf_iget(child->d_inode->i_sb, &tloc);
1187 1188
	if (IS_ERR(inode))
		return ERR_CAST(inode);
R
Rasmus Rohde 已提交
1189

1190
	return d_obtain_alias(inode);
R
Rasmus Rohde 已提交
1191 1192 1193 1194 1195 1196 1197
}


static struct dentry *udf_nfs_get_inode(struct super_block *sb, u32 block,
					u16 partref, __u32 generation)
{
	struct inode *inode;
1198
	struct kernel_lb_addr loc;
R
Rasmus Rohde 已提交
1199 1200 1201 1202 1203 1204

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

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

1207 1208
	if (IS_ERR(inode))
		return ERR_CAST(inode);
R
Rasmus Rohde 已提交
1209 1210 1211 1212 1213

	if (generation && inode->i_generation != generation) {
		iput(inode);
		return ERR_PTR(-ESTALE);
	}
1214
	return d_obtain_alias(inode);
R
Rasmus Rohde 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
}

static struct dentry *udf_fh_to_dentry(struct super_block *sb,
				       struct fid *fid, int fh_len, int fh_type)
{
	if ((fh_len != 3 && fh_len != 5) ||
	    (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)
{
	if (fh_len != 5 || fh_type != FILEID_UDF_WITH_PARENT)
		return NULL;

	return udf_nfs_get_inode(sb, fid->udf.parent_block,
				 fid->udf.parent_partref,
				 fid->udf.parent_generation);
}
A
Al Viro 已提交
1239 1240
static int udf_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
			 struct inode *parent)
R
Rasmus Rohde 已提交
1241 1242
{
	int len = *lenp;
1243
	struct kernel_lb_addr location = UDF_I(inode)->i_location;
R
Rasmus Rohde 已提交
1244 1245 1246
	struct fid *fid = (struct fid *)fh;
	int type = FILEID_UDF_WITHOUT_PARENT;

A
Al Viro 已提交
1247
	if (parent && (len < 5)) {
1248
		*lenp = 5;
1249
		return FILEID_INVALID;
1250 1251
	} else if (len < 3) {
		*lenp = 3;
1252
		return FILEID_INVALID;
1253
	}
R
Rasmus Rohde 已提交
1254 1255 1256 1257

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

A
Al Viro 已提交
1261 1262
	if (parent) {
		location = UDF_I(parent)->i_location;
R
Rasmus Rohde 已提交
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
		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,
};

1280
const struct inode_operations udf_dir_inode_operations = {
1281 1282 1283 1284 1285 1286 1287 1288 1289
	.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 已提交
1290
	.tmpfile			= udf_tmpfile,
L
Linus Torvalds 已提交
1291
};
1292 1293 1294 1295 1296
const struct inode_operations udf_symlink_inode_operations = {
	.readlink	= generic_readlink,
	.follow_link	= page_follow_link_light,
	.put_link	= page_put_link,
};