namei.c 33.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] == '.';
L
Linus Torvalds 已提交
162

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

166
	fibh->sbh = fibh->ebh = NULL;
167
	fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
168 169 170 171
	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))
			goto out_err;
172
		block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
173
		if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
174
			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
175
				epos.offset -= sizeof(struct short_ad);
176
			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
177
				epos.offset -= sizeof(struct long_ad);
M
Marcin Slusarz 已提交
178
		} else
L
Linus Torvalds 已提交
179 180
			offset = 0;

M
Marcin Slusarz 已提交
181
		fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
182 183
		if (!fibh->sbh)
			goto out_err;
L
Linus Torvalds 已提交
184 185
	}

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

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

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

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

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

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

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

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

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

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

M
Marcin Slusarz 已提交
236
		flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
237
		if (flen && udf_match(flen, fname, child->len, child->name))
238
			goto out_ok;
L
Linus Torvalds 已提交
239
	}
240

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

A
Al Viro 已提交
551
static int udf_create(struct inode *dir, struct dentry *dentry, umode_t mode,
A
Al Viro 已提交
552
		      bool excl)
L
Linus Torvalds 已提交
553 554 555 556 557
{
	struct udf_fileident_bh fibh;
	struct inode *inode;
	struct fileIdentDesc cfi, *fi;
	int err;
558
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
559 560

	inode = udf_new_inode(dir, mode, &err);
561
	if (!inode) {
L
Linus Torvalds 已提交
562 563 564
		return err;
	}

565 566
	iinfo = UDF_I(inode);
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
567 568 569 570 571 572 573
		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);

M
Marcin Slusarz 已提交
574 575
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi) {
576
		inode_dec_link_count(inode);
L
Linus Torvalds 已提交
577 578 579 580
		iput(inode);
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
581
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
582
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
583
		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
584
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
585
	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
586 587
		mark_inode_dirty(dir);
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
588 589
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
590
	d_instantiate(dentry, inode);
591

L
Linus Torvalds 已提交
592 593 594
	return 0;
}

A
Al Viro 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
static int udf_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
{
	struct inode *inode;
	struct udf_inode_info *iinfo;
	int err;

	inode = udf_new_inode(dir, mode, &err);
	if (!inode)
		return err;

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

	d_tmpfile(dentry, inode);
	return 0;
}

A
Al Viro 已提交
618
static int udf_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
619
		     dev_t rdev)
L
Linus Torvalds 已提交
620
{
621
	struct inode *inode;
L
Linus Torvalds 已提交
622 623 624
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;
625
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
626 627 628 629 630 631 632 633 634

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

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

635
	iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
636
	init_special_inode(inode, mode, rdev);
M
Marcin Slusarz 已提交
637 638
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi) {
639
		inode_dec_link_count(inode);
L
Linus Torvalds 已提交
640 641 642 643
		iput(inode);
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
644
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
645
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
646
		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
647
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
648
	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
649 650 651 652
		mark_inode_dirty(dir);
	mark_inode_dirty(inode);

	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
653 654
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
655 656
	d_instantiate(dentry, inode);
	err = 0;
657 658

out:
L
Linus Torvalds 已提交
659 660 661
	return err;
}

662
static int udf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
L
Linus Torvalds 已提交
663
{
664
	struct inode *inode;
L
Linus Torvalds 已提交
665 666 667
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;
668 669
	struct udf_inode_info *dinfo = UDF_I(dir);
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
670 671

	err = -EIO;
672
	inode = udf_new_inode(dir, S_IFDIR | mode, &err);
L
Linus Torvalds 已提交
673 674 675
	if (!inode)
		goto out;

676
	iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
677 678
	inode->i_op = &udf_dir_inode_operations;
	inode->i_fop = &udf_dir_operations;
M
Marcin Slusarz 已提交
679 680
	fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
	if (!fi) {
681
		inode_dec_link_count(inode);
L
Linus Torvalds 已提交
682 683 684
		iput(inode);
		goto out;
	}
M
Miklos Szeredi 已提交
685
	set_nlink(inode, 2);
L
Linus Torvalds 已提交
686
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
687
	cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
688
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
689
		cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
M
Marcin Slusarz 已提交
690 691
	cfi.fileCharacteristics =
			FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
L
Linus Torvalds 已提交
692
	udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
J
Jan Kara 已提交
693
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
694 695
	mark_inode_dirty(inode);

M
Marcin Slusarz 已提交
696 697
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi) {
698
		clear_nlink(inode);
L
Linus Torvalds 已提交
699 700 701 702 703
		mark_inode_dirty(inode);
		iput(inode);
		goto out;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
704
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
705
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
706
		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
707 708
	cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
709
	inc_nlink(dir);
L
Linus Torvalds 已提交
710 711 712
	mark_inode_dirty(dir);
	d_instantiate(dentry, inode);
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
713 714
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
715
	err = 0;
716 717

out:
L
Linus Torvalds 已提交
718 719 720 721 722 723 724 725
	return err;
}

static int empty_dir(struct inode *dir)
{
	struct fileIdentDesc *fi, cfi;
	struct udf_fileident_bh fibh;
	loff_t f_pos;
726
	loff_t size = udf_ext0_offset(dir) + dir->i_size;
L
Linus Torvalds 已提交
727
	int block;
728
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
729
	uint32_t elen;
730
	sector_t offset;
731
	struct extent_position epos = {};
732
	struct udf_inode_info *dinfo = UDF_I(dir);
L
Linus Torvalds 已提交
733

734 735
	f_pos = udf_ext0_offset(dir);
	fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
L
Linus Torvalds 已提交
736

737
	if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
738
		fibh.sbh = fibh.ebh = NULL;
739
	else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
M
Marcin Slusarz 已提交
740 741
			      &epos, &eloc, &elen, &offset) ==
					(EXT_RECORDED_ALLOCATED >> 30)) {
742
		block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
743
		if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
744
			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
745
				epos.offset -= sizeof(struct short_ad);
746
			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
747
				epos.offset -= sizeof(struct long_ad);
M
Marcin Slusarz 已提交
748
		} else
L
Linus Torvalds 已提交
749 750
			offset = 0;

M
Marcin Slusarz 已提交
751 752
		fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
		if (!fibh.sbh) {
J
Jan Kara 已提交
753
			brelse(epos.bh);
L
Linus Torvalds 已提交
754 755
			return 0;
		}
756
	} else {
J
Jan Kara 已提交
757
		brelse(epos.bh);
L
Linus Torvalds 已提交
758 759 760
		return 0;
	}

761
	while (f_pos < size) {
762 763 764
		fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
					&elen, &offset);
		if (!fi) {
L
Linus Torvalds 已提交
765
			if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
766 767 768
				brelse(fibh.ebh);
			brelse(fibh.sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
769 770 771
			return 0;
		}

772 773
		if (cfi.lengthFileIdent &&
		    (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
L
Linus Torvalds 已提交
774
			if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
775 776 777
				brelse(fibh.ebh);
			brelse(fibh.sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
778 779 780
			return 0;
		}
	}
781

L
Linus Torvalds 已提交
782
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
783 784 785
		brelse(fibh.ebh);
	brelse(fibh.sbh);
	brelse(epos.bh);
786

L
Linus Torvalds 已提交
787 788 789
	return 1;
}

790
static int udf_rmdir(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
791 792
{
	int retval;
793
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
794 795
	struct udf_fileident_bh fibh;
	struct fileIdentDesc *fi, cfi;
796
	struct kernel_lb_addr tloc;
L
Linus Torvalds 已提交
797 798

	retval = -ENOENT;
799
	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
L
Linus Torvalds 已提交
800 801 802 803 804
	if (!fi)
		goto out;

	retval = -EIO;
	tloc = lelb_to_cpu(cfi.icb.extLocation);
805
	if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
L
Linus Torvalds 已提交
806 807 808 809 810 811 812 813
		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 已提交
814 815
		udf_warn(inode->i_sb, "empty directory has nlink != 2 (%d)\n",
			 inode->i_nlink);
816
	clear_nlink(inode);
L
Linus Torvalds 已提交
817
	inode->i_size = 0;
818
	inode_dec_link_count(dir);
M
Marcin Slusarz 已提交
819 820
	inode->i_ctime = dir->i_ctime = dir->i_mtime =
						current_fs_time(dir->i_sb);
L
Linus Torvalds 已提交
821 822
	mark_inode_dirty(dir);

823
end_rmdir:
L
Linus Torvalds 已提交
824
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
825 826
		brelse(fibh.ebh);
	brelse(fibh.sbh);
827 828

out:
L
Linus Torvalds 已提交
829 830 831
	return retval;
}

832
static int udf_unlink(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
833 834
{
	int retval;
835
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
836 837 838
	struct udf_fileident_bh fibh;
	struct fileIdentDesc *fi;
	struct fileIdentDesc cfi;
839
	struct kernel_lb_addr tloc;
L
Linus Torvalds 已提交
840 841

	retval = -ENOENT;
842
	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
L
Linus Torvalds 已提交
843 844 845 846 847
	if (!fi)
		goto out;

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

851
	if (!inode->i_nlink) {
L
Linus Torvalds 已提交
852
		udf_debug("Deleting nonexistent file (%lu), %d\n",
853
			  inode->i_ino, inode->i_nlink);
M
Miklos Szeredi 已提交
854
		set_nlink(inode, 1);
L
Linus Torvalds 已提交
855 856 857 858 859 860
	}
	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);
861
	inode_dec_link_count(inode);
L
Linus Torvalds 已提交
862 863 864
	inode->i_ctime = dir->i_ctime;
	retval = 0;

865
end_unlink:
L
Linus Torvalds 已提交
866
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
867 868
		brelse(fibh.ebh);
	brelse(fibh.sbh);
869 870

out:
L
Linus Torvalds 已提交
871 872 873
	return retval;
}

874 875
static int udf_symlink(struct inode *dir, struct dentry *dentry,
		       const char *symname)
L
Linus Torvalds 已提交
876
{
877
	struct inode *inode;
L
Linus Torvalds 已提交
878
	struct pathComponent *pc;
A
Al Viro 已提交
879
	const char *compstart;
L
Linus Torvalds 已提交
880
	struct udf_fileident_bh fibh;
881
	struct extent_position epos = {};
L
Linus Torvalds 已提交
882 883 884
	int eoffset, elen = 0;
	struct fileIdentDesc *fi;
	struct fileIdentDesc cfi;
A
Al Viro 已提交
885
	uint8_t *ea;
L
Linus Torvalds 已提交
886 887
	int err;
	int block;
A
Al Viro 已提交
888
	unsigned char *name = NULL;
L
Linus Torvalds 已提交
889
	int namelen;
890
	struct udf_inode_info *iinfo;
891
	struct super_block *sb = dir->i_sb;
L
Linus Torvalds 已提交
892

893
	inode = udf_new_inode(dir, S_IFLNK | S_IRWXUGO, &err);
M
Marcin Slusarz 已提交
894
	if (!inode)
L
Linus Torvalds 已提交
895 896
		goto out;

897 898
	iinfo = UDF_I(inode);
	down_write(&iinfo->i_data_sem);
899 900 901 902 903 904
	name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
	if (!name) {
		err = -ENOMEM;
		goto out_no_entry;
	}

L
Linus Torvalds 已提交
905
	inode->i_data.a_ops = &udf_symlink_aops;
906
	inode->i_op = &udf_symlink_inode_operations;
L
Linus Torvalds 已提交
907

908
	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
909
		struct kernel_lb_addr eloc;
910
		uint32_t bsize;
L
Linus Torvalds 已提交
911

912
		block = udf_new_block(sb, inode,
913 914
				iinfo->i_location.partitionReferenceNum,
				iinfo->i_location.logicalBlockNum, &err);
L
Linus Torvalds 已提交
915 916
		if (!block)
			goto out_no_entry;
917
		epos.block = iinfo->i_location;
J
Jan Kara 已提交
918 919
		epos.offset = udf_file_entry_alloc_offset(inode);
		epos.bh = NULL;
L
Linus Torvalds 已提交
920
		eloc.logicalBlockNum = block;
M
Marcin Slusarz 已提交
921
		eloc.partitionReferenceNum =
922
				iinfo->i_location.partitionReferenceNum;
923
		bsize = sb->s_blocksize;
924
		iinfo->i_lenExtents = bsize;
925
		udf_add_aext(inode, &epos, &eloc, bsize, 0);
J
Jan Kara 已提交
926
		brelse(epos.bh);
L
Linus Torvalds 已提交
927

928
		block = udf_get_pblock(sb, block,
929
				iinfo->i_location.partitionReferenceNum,
M
Marcin Slusarz 已提交
930
				0);
931
		epos.bh = udf_tgetblk(sb, block);
J
Jan Kara 已提交
932
		lock_buffer(epos.bh);
933
		memset(epos.bh->b_data, 0x00, bsize);
J
Jan Kara 已提交
934 935 936 937
		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);
938 939
	} else
		ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
L
Linus Torvalds 已提交
940

941
	eoffset = sb->s_blocksize - udf_ext0_offset(inode);
L
Linus Torvalds 已提交
942 943
	pc = (struct pathComponent *)ea;

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

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

	err = -ENAMETOOLONG;

957
	while (*symname) {
L
Linus Torvalds 已提交
958 959 960 961 962
		if (elen + sizeof(struct pathComponent) > eoffset)
			goto out_no_entry;

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

A
Al Viro 已提交
963
		compstart = symname;
L
Linus Torvalds 已提交
964

965
		do {
L
Linus Torvalds 已提交
966 967 968 969 970 971
			symname++;
		} while (*symname && *symname != '/');

		pc->componentType = 5;
		pc->lengthComponentIdent = 0;
		pc->componentFileVersionNum = 0;
972 973
		if (compstart[0] == '.') {
			if ((symname - compstart) == 1)
L
Linus Torvalds 已提交
974
				pc->componentType = 4;
M
Marcin Slusarz 已提交
975 976
			else if ((symname - compstart) == 2 &&
					compstart[1] == '.')
L
Linus Torvalds 已提交
977 978 979
				pc->componentType = 3;
		}

980
		if (pc->componentType == 5) {
981
			namelen = udf_put_filename(sb, compstart, name,
982 983
						   symname - compstart);
			if (!namelen)
L
Linus Torvalds 已提交
984 985
				goto out_no_entry;

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

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

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

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

J
Jan Kara 已提交
1004
	brelse(epos.bh);
L
Linus Torvalds 已提交
1005
	inode->i_size = elen;
1006 1007
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
		iinfo->i_lenAlloc = inode->i_size;
J
Jan Kara 已提交
1008 1009
	else
		udf_truncate_tail_extent(inode);
L
Linus Torvalds 已提交
1010
	mark_inode_dirty(inode);
1011
	up_write(&iinfo->i_data_sem);
L
Linus Torvalds 已提交
1012

M
Marcin Slusarz 已提交
1013 1014
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi)
1015
		goto out_fail;
1016
	cfi.icb.extLength = cpu_to_le32(sb->s_blocksize);
1017
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
1018
	if (UDF_SB(inode->i_sb)->s_lvid_bh) {
1019
		*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1020
			cpu_to_le32(lvid_get_unique_id(sb));
L
Linus Torvalds 已提交
1021 1022
	}
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1023
	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1024 1025
		mark_inode_dirty(dir);
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
1026 1027
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
1028 1029 1030
	d_instantiate(dentry, inode);
	err = 0;

1031
out:
1032
	kfree(name);
L
Linus Torvalds 已提交
1033 1034
	return err;

1035
out_no_entry:
1036
	up_write(&iinfo->i_data_sem);
1037
out_fail:
1038
	inode_dec_link_count(inode);
L
Linus Torvalds 已提交
1039 1040 1041 1042
	iput(inode);
	goto out;
}

1043 1044
static int udf_link(struct dentry *old_dentry, struct inode *dir,
		    struct dentry *dentry)
L
Linus Torvalds 已提交
1045 1046 1047 1048 1049 1050
{
	struct inode *inode = old_dentry->d_inode;
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;

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

L
Linus Torvalds 已提交
1065
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
1066 1067
		brelse(fibh.ebh);
	brelse(fibh.sbh);
1068
	inc_nlink(inode);
L
Linus Torvalds 已提交
1069 1070
	inode->i_ctime = current_fs_time(inode->i_sb);
	mark_inode_dirty(inode);
A
Al Viro 已提交
1071
	ihold(inode);
L
Linus Torvalds 已提交
1072
	d_instantiate(dentry, inode);
1073

L
Linus Torvalds 已提交
1074 1075 1076 1077 1078 1079
	return 0;
}

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

1093
	ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
M
Marcin Slusarz 已提交
1094
	if (ofi) {
L
Linus Torvalds 已提交
1095
		if (ofibh.sbh != ofibh.ebh)
J
Jan Kara 已提交
1096 1097
			brelse(ofibh.ebh);
		brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1098 1099
	}
	tloc = lelb_to_cpu(ocfi.icb.extLocation);
1100
	if (!ofi || udf_get_lb_pblock(old_dir->i_sb, &tloc, 0)
1101
	    != old_inode->i_ino)
L
Linus Torvalds 已提交
1102 1103
		goto end_rename;

1104
	nfi = udf_find_entry(new_dir, &new_dentry->d_name, &nfibh, &ncfi);
1105 1106
	if (nfi) {
		if (!new_inode) {
L
Linus Torvalds 已提交
1107
			if (nfibh.sbh != nfibh.ebh)
J
Jan Kara 已提交
1108 1109
				brelse(nfibh.ebh);
			brelse(nfibh.sbh);
L
Linus Torvalds 已提交
1110 1111 1112
			nfi = NULL;
		}
	}
1113
	if (S_ISDIR(old_inode->i_mode)) {
M
Marcin Slusarz 已提交
1114
		int offset = udf_ext0_offset(old_inode);
L
Linus Torvalds 已提交
1115

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

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

1169
	if (new_inode) {
L
Linus Torvalds 已提交
1170
		new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1171
		inode_dec_link_count(new_inode);
L
Linus Torvalds 已提交
1172 1173 1174 1175
	}
	old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
	mark_inode_dirty(old_dir);

1176
	if (dir_fi) {
1177
		dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
M
Marcin Slusarz 已提交
1178 1179 1180
		udf_update_tag((char *)dir_fi,
				(sizeof(struct fileIdentDesc) +
				le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1181
		if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1182
			mark_inode_dirty(old_inode);
M
Marcin Slusarz 已提交
1183
		else
L
Linus Torvalds 已提交
1184
			mark_buffer_dirty_inode(dir_bh, old_inode);
M
Marcin Slusarz 已提交
1185

1186
		inode_dec_link_count(old_dir);
M
Marcin Slusarz 已提交
1187
		if (new_inode)
1188
			inode_dec_link_count(new_inode);
M
Marcin Slusarz 已提交
1189
		else {
1190
			inc_nlink(new_dir);
L
Linus Torvalds 已提交
1191 1192 1193 1194
			mark_inode_dirty(new_dir);
		}
	}

1195
	if (ofi) {
L
Linus Torvalds 已提交
1196
		if (ofibh.sbh != ofibh.ebh)
J
Jan Kara 已提交
1197 1198
			brelse(ofibh.ebh);
		brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1199 1200 1201 1202
	}

	retval = 0;

1203
end_rename:
J
Jan Kara 已提交
1204
	brelse(dir_bh);
1205
	if (nfi) {
L
Linus Torvalds 已提交
1206
		if (nfibh.sbh != nfibh.ebh)
J
Jan Kara 已提交
1207 1208
			brelse(nfibh.ebh);
		brelse(nfibh.sbh);
L
Linus Torvalds 已提交
1209
	}
1210

L
Linus Torvalds 已提交
1211 1212 1213
	return retval;
}

R
Rasmus Rohde 已提交
1214 1215
static struct dentry *udf_get_parent(struct dentry *child)
{
1216
	struct kernel_lb_addr tloc;
R
Rasmus Rohde 已提交
1217
	struct inode *inode = NULL;
1218
	struct qstr dotdot = QSTR_INIT("..", 2);
R
Rasmus Rohde 已提交
1219 1220 1221 1222
	struct fileIdentDesc cfi;
	struct udf_fileident_bh fibh;

	if (!udf_find_entry(child->d_inode, &dotdot, &fibh, &cfi))
1223
		return ERR_PTR(-EACCES);
R
Rasmus Rohde 已提交
1224 1225 1226 1227 1228

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

1229 1230
	tloc = lelb_to_cpu(cfi.icb.extLocation);
	inode = udf_iget(child->d_inode->i_sb, &tloc);
1231 1232
	if (IS_ERR(inode))
		return ERR_CAST(inode);
R
Rasmus Rohde 已提交
1233

1234
	return d_obtain_alias(inode);
R
Rasmus Rohde 已提交
1235 1236 1237 1238 1239 1240 1241
}


static struct dentry *udf_nfs_get_inode(struct super_block *sb, u32 block,
					u16 partref, __u32 generation)
{
	struct inode *inode;
1242
	struct kernel_lb_addr loc;
R
Rasmus Rohde 已提交
1243 1244 1245 1246 1247 1248

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

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

1251 1252
	if (IS_ERR(inode))
		return ERR_CAST(inode);
R
Rasmus Rohde 已提交
1253 1254 1255 1256 1257

	if (generation && inode->i_generation != generation) {
		iput(inode);
		return ERR_PTR(-ESTALE);
	}
1258
	return d_obtain_alias(inode);
R
Rasmus Rohde 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
}

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 已提交
1283 1284
static int udf_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
			 struct inode *parent)
R
Rasmus Rohde 已提交
1285 1286
{
	int len = *lenp;
1287
	struct kernel_lb_addr location = UDF_I(inode)->i_location;
R
Rasmus Rohde 已提交
1288 1289 1290
	struct fid *fid = (struct fid *)fh;
	int type = FILEID_UDF_WITHOUT_PARENT;

A
Al Viro 已提交
1291
	if (parent && (len < 5)) {
1292
		*lenp = 5;
1293
		return FILEID_INVALID;
1294 1295
	} else if (len < 3) {
		*lenp = 3;
1296
		return FILEID_INVALID;
1297
	}
R
Rasmus Rohde 已提交
1298 1299 1300 1301

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

A
Al Viro 已提交
1305 1306
	if (parent) {
		location = UDF_I(parent)->i_location;
R
Rasmus Rohde 已提交
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
		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,
};

1324
const struct inode_operations udf_dir_inode_operations = {
1325 1326 1327 1328 1329 1330 1331 1332 1333
	.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 已提交
1334
	.tmpfile			= udf_tmpfile,
L
Linus Torvalds 已提交
1335
};
1336 1337 1338 1339 1340
const struct inode_operations udf_symlink_inode_operations = {
	.readlink	= generic_readlink,
	.follow_link	= page_follow_link_light,
	.put_link	= page_put_link,
};