namei.c 33.8 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
enum { UDF_MAX_LINKS = 0xffff };

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

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

int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
47
		 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
M
Marcin Slusarz 已提交
48
		 uint8_t *impuse, uint8_t *fileident)
L
Linus Torvalds 已提交
49
{
50
	uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(struct tag);
L
Linus Torvalds 已提交
51 52 53 54 55
	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 -
56
		sizeof(struct fileIdentDesc);
L
Linus Torvalds 已提交
57 58
	int adinicb = 0;

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

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

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

	offset += liu;

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

	offset += lfi;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

255 256
static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
				 struct nameidata *nd)
L
Linus Torvalds 已提交
257 258
{
	struct inode *inode = NULL;
259
	struct fileIdentDesc cfi;
L
Linus Torvalds 已提交
260 261
	struct udf_fileident_bh fibh;

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

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

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

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

288 289
		loc = lelb_to_cpu(cfi.icb.extLocation);
		inode = udf_iget(dir->i_sb, &loc);
290
		if (!inode) {
L
Linus Torvalds 已提交
291 292 293
			return ERR_PTR(-EACCES);
		}
	}
294

R
Rasmus Rohde 已提交
295
	return d_splice_alias(inode, dentry);
L
Linus Torvalds 已提交
296 297
}

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

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

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

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

343
	f_pos = udf_ext0_offset(dir);
L
Linus Torvalds 已提交
344

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

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

370
		block = dinfo->i_location.logicalBlockNum;
L
Linus Torvalds 已提交
371 372
	}

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

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

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

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

404
add:
L
Linus Torvalds 已提交
405 406
	f_pos += nfidlen;

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

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

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

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

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

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

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 已提交
541 542 543
}

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

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

L
Linus Torvalds 已提交
552 553 554
	return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
}

555 556
static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
		      struct nameidata *nd)
L
Linus Torvalds 已提交
557 558 559 560 561
{
	struct udf_fileident_bh fibh;
	struct inode *inode;
	struct fileIdentDesc cfi, *fi;
	int err;
562
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
563 564

	inode = udf_new_inode(dir, mode, &err);
565
	if (!inode) {
L
Linus Torvalds 已提交
566 567 568
		return err;
	}

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

L
Linus Torvalds 已提交
596 597 598
	return 0;
}

599 600
static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
		     dev_t rdev)
L
Linus Torvalds 已提交
601
{
602
	struct inode *inode;
L
Linus Torvalds 已提交
603 604 605
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;
606
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
607 608 609 610 611 612 613 614 615

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

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

616
	iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
617
	init_special_inode(inode, mode, rdev);
M
Marcin Slusarz 已提交
618 619
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi) {
620
		inode_dec_link_count(inode);
L
Linus Torvalds 已提交
621 622 623 624
		iput(inode);
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
625
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
626
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
627
		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
628
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
629
	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
630 631 632 633
		mark_inode_dirty(dir);
	mark_inode_dirty(inode);

	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
634 635
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
636 637
	d_instantiate(dentry, inode);
	err = 0;
638 639

out:
L
Linus Torvalds 已提交
640 641 642
	return err;
}

643
static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
L
Linus Torvalds 已提交
644
{
645
	struct inode *inode;
L
Linus Torvalds 已提交
646 647 648
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;
649 650
	struct udf_inode_info *dinfo = UDF_I(dir);
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
651 652

	err = -EMLINK;
A
Al Viro 已提交
653
	if (dir->i_nlink >= UDF_MAX_LINKS)
L
Linus Torvalds 已提交
654 655 656
		goto out;

	err = -EIO;
657
	inode = udf_new_inode(dir, S_IFDIR | mode, &err);
L
Linus Torvalds 已提交
658 659 660
	if (!inode)
		goto out;

661
	iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
662 663
	inode->i_op = &udf_dir_inode_operations;
	inode->i_fop = &udf_dir_operations;
M
Marcin Slusarz 已提交
664 665
	fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
	if (!fi) {
666
		inode_dec_link_count(inode);
L
Linus Torvalds 已提交
667 668 669 670 671
		iput(inode);
		goto out;
	}
	inode->i_nlink = 2;
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
672
	cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
673
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
674
		cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
M
Marcin Slusarz 已提交
675 676
	cfi.fileCharacteristics =
			FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
L
Linus Torvalds 已提交
677
	udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
J
Jan Kara 已提交
678
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
679 680
	mark_inode_dirty(inode);

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

out:
L
Linus Torvalds 已提交
703 704 705 706 707 708 709 710
	return err;
}

static int empty_dir(struct inode *dir)
{
	struct fileIdentDesc *fi, cfi;
	struct udf_fileident_bh fibh;
	loff_t f_pos;
711
	loff_t size = udf_ext0_offset(dir) + dir->i_size;
L
Linus Torvalds 已提交
712
	int block;
713
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
714
	uint32_t elen;
715
	sector_t offset;
716
	struct extent_position epos = {};
717
	struct udf_inode_info *dinfo = UDF_I(dir);
L
Linus Torvalds 已提交
718

719 720
	f_pos = udf_ext0_offset(dir);
	fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
L
Linus Torvalds 已提交
721

722
	if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
723
		fibh.sbh = fibh.ebh = NULL;
724
	else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
M
Marcin Slusarz 已提交
725 726
			      &epos, &eloc, &elen, &offset) ==
					(EXT_RECORDED_ALLOCATED >> 30)) {
727
		block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
728
		if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
729
			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
730
				epos.offset -= sizeof(struct short_ad);
731
			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
732
				epos.offset -= sizeof(struct long_ad);
M
Marcin Slusarz 已提交
733
		} else
L
Linus Torvalds 已提交
734 735
			offset = 0;

M
Marcin Slusarz 已提交
736 737
		fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
		if (!fibh.sbh) {
J
Jan Kara 已提交
738
			brelse(epos.bh);
L
Linus Torvalds 已提交
739 740
			return 0;
		}
741
	} else {
J
Jan Kara 已提交
742
		brelse(epos.bh);
L
Linus Torvalds 已提交
743 744 745
		return 0;
	}

746
	while (f_pos < size) {
747 748 749
		fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
					&elen, &offset);
		if (!fi) {
L
Linus Torvalds 已提交
750
			if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
751 752 753
				brelse(fibh.ebh);
			brelse(fibh.sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
754 755 756
			return 0;
		}

757 758
		if (cfi.lengthFileIdent &&
		    (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
L
Linus Torvalds 已提交
759
			if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
760 761 762
				brelse(fibh.ebh);
			brelse(fibh.sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
763 764 765
			return 0;
		}
	}
766

L
Linus Torvalds 已提交
767
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
768 769 770
		brelse(fibh.ebh);
	brelse(fibh.sbh);
	brelse(epos.bh);
771

L
Linus Torvalds 已提交
772 773 774
	return 1;
}

775
static int udf_rmdir(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
776 777
{
	int retval;
778
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
779 780
	struct udf_fileident_bh fibh;
	struct fileIdentDesc *fi, cfi;
781
	struct kernel_lb_addr tloc;
L
Linus Torvalds 已提交
782 783

	retval = -ENOENT;
784
	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
L
Linus Torvalds 已提交
785 786 787 788 789
	if (!fi)
		goto out;

	retval = -EIO;
	tloc = lelb_to_cpu(cfi.icb.extLocation);
790
	if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
L
Linus Torvalds 已提交
791 792 793 794 795 796 797 798 799
		goto end_rmdir;
	retval = -ENOTEMPTY;
	if (!empty_dir(inode))
		goto end_rmdir;
	retval = udf_delete_entry(dir, fi, &fibh, &cfi);
	if (retval)
		goto end_rmdir;
	if (inode->i_nlink != 2)
		udf_warning(inode->i_sb, "udf_rmdir",
800 801
			    "empty directory has nlink != 2 (%d)",
			    inode->i_nlink);
802
	clear_nlink(inode);
L
Linus Torvalds 已提交
803
	inode->i_size = 0;
804
	inode_dec_link_count(dir);
M
Marcin Slusarz 已提交
805 806
	inode->i_ctime = dir->i_ctime = dir->i_mtime =
						current_fs_time(dir->i_sb);
L
Linus Torvalds 已提交
807 808
	mark_inode_dirty(dir);

809
end_rmdir:
L
Linus Torvalds 已提交
810
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
811 812
		brelse(fibh.ebh);
	brelse(fibh.sbh);
813 814

out:
L
Linus Torvalds 已提交
815 816 817
	return retval;
}

818
static int udf_unlink(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
819 820
{
	int retval;
821
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
822 823 824
	struct udf_fileident_bh fibh;
	struct fileIdentDesc *fi;
	struct fileIdentDesc cfi;
825
	struct kernel_lb_addr tloc;
L
Linus Torvalds 已提交
826 827

	retval = -ENOENT;
828
	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
L
Linus Torvalds 已提交
829 830 831 832 833
	if (!fi)
		goto out;

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

837
	if (!inode->i_nlink) {
L
Linus Torvalds 已提交
838
		udf_debug("Deleting nonexistent file (%lu), %d\n",
839
			  inode->i_ino, inode->i_nlink);
L
Linus Torvalds 已提交
840 841 842 843 844 845 846
		inode->i_nlink = 1;
	}
	retval = udf_delete_entry(dir, fi, &fibh, &cfi);
	if (retval)
		goto end_unlink;
	dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
	mark_inode_dirty(dir);
847
	inode_dec_link_count(inode);
L
Linus Torvalds 已提交
848 849 850
	inode->i_ctime = dir->i_ctime;
	retval = 0;

851
end_unlink:
L
Linus Torvalds 已提交
852
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
853 854
		brelse(fibh.ebh);
	brelse(fibh.sbh);
855 856

out:
L
Linus Torvalds 已提交
857 858 859
	return retval;
}

860 861
static int udf_symlink(struct inode *dir, struct dentry *dentry,
		       const char *symname)
L
Linus Torvalds 已提交
862
{
863
	struct inode *inode;
L
Linus Torvalds 已提交
864
	struct pathComponent *pc;
A
Al Viro 已提交
865
	const char *compstart;
L
Linus Torvalds 已提交
866
	struct udf_fileident_bh fibh;
867
	struct extent_position epos = {};
L
Linus Torvalds 已提交
868 869 870
	int eoffset, elen = 0;
	struct fileIdentDesc *fi;
	struct fileIdentDesc cfi;
A
Al Viro 已提交
871
	uint8_t *ea;
L
Linus Torvalds 已提交
872 873
	int err;
	int block;
A
Al Viro 已提交
874
	unsigned char *name = NULL;
L
Linus Torvalds 已提交
875
	int namelen;
876
	struct udf_inode_info *iinfo;
877
	struct super_block *sb = dir->i_sb;
L
Linus Torvalds 已提交
878

879
	inode = udf_new_inode(dir, S_IFLNK | S_IRWXUGO, &err);
M
Marcin Slusarz 已提交
880
	if (!inode)
L
Linus Torvalds 已提交
881 882
		goto out;

883 884
	iinfo = UDF_I(inode);
	down_write(&iinfo->i_data_sem);
885 886 887 888 889 890
	name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
	if (!name) {
		err = -ENOMEM;
		goto out_no_entry;
	}

L
Linus Torvalds 已提交
891
	inode->i_data.a_ops = &udf_symlink_aops;
892
	inode->i_op = &udf_symlink_inode_operations;
L
Linus Torvalds 已提交
893

894
	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
895
		struct kernel_lb_addr eloc;
896
		uint32_t bsize;
L
Linus Torvalds 已提交
897

898
		block = udf_new_block(sb, inode,
899 900
				iinfo->i_location.partitionReferenceNum,
				iinfo->i_location.logicalBlockNum, &err);
L
Linus Torvalds 已提交
901 902
		if (!block)
			goto out_no_entry;
903
		epos.block = iinfo->i_location;
J
Jan Kara 已提交
904 905
		epos.offset = udf_file_entry_alloc_offset(inode);
		epos.bh = NULL;
L
Linus Torvalds 已提交
906
		eloc.logicalBlockNum = block;
M
Marcin Slusarz 已提交
907
		eloc.partitionReferenceNum =
908
				iinfo->i_location.partitionReferenceNum;
909
		bsize = sb->s_blocksize;
910
		iinfo->i_lenExtents = bsize;
911
		udf_add_aext(inode, &epos, &eloc, bsize, 0);
J
Jan Kara 已提交
912
		brelse(epos.bh);
L
Linus Torvalds 已提交
913

914
		block = udf_get_pblock(sb, block,
915
				iinfo->i_location.partitionReferenceNum,
M
Marcin Slusarz 已提交
916
				0);
917
		epos.bh = udf_tgetblk(sb, block);
J
Jan Kara 已提交
918
		lock_buffer(epos.bh);
919
		memset(epos.bh->b_data, 0x00, bsize);
J
Jan Kara 已提交
920 921 922 923
		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);
924 925
	} else
		ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
L
Linus Torvalds 已提交
926

927
	eoffset = sb->s_blocksize - udf_ext0_offset(inode);
L
Linus Torvalds 已提交
928 929
	pc = (struct pathComponent *)ea;

930 931
	if (*symname == '/') {
		do {
L
Linus Torvalds 已提交
932 933 934 935 936 937 938 939 940 941 942
			symname++;
		} while (*symname == '/');

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

	err = -ENAMETOOLONG;

943
	while (*symname) {
L
Linus Torvalds 已提交
944 945 946 947 948
		if (elen + sizeof(struct pathComponent) > eoffset)
			goto out_no_entry;

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

A
Al Viro 已提交
949
		compstart = symname;
L
Linus Torvalds 已提交
950

951
		do {
L
Linus Torvalds 已提交
952 953 954 955 956 957
			symname++;
		} while (*symname && *symname != '/');

		pc->componentType = 5;
		pc->lengthComponentIdent = 0;
		pc->componentFileVersionNum = 0;
958 959
		if (compstart[0] == '.') {
			if ((symname - compstart) == 1)
L
Linus Torvalds 已提交
960
				pc->componentType = 4;
M
Marcin Slusarz 已提交
961 962
			else if ((symname - compstart) == 2 &&
					compstart[1] == '.')
L
Linus Torvalds 已提交
963 964 965
				pc->componentType = 3;
		}

966
		if (pc->componentType == 5) {
967
			namelen = udf_put_filename(sb, compstart, name,
968 969
						   symname - compstart);
			if (!namelen)
L
Linus Torvalds 已提交
970 971
				goto out_no_entry;

M
Marcin Slusarz 已提交
972 973
			if (elen + sizeof(struct pathComponent) + namelen >
					eoffset)
L
Linus Torvalds 已提交
974 975 976 977 978 979 980 981 982
				goto out_no_entry;
			else
				pc->lengthComponentIdent = namelen;

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

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

983 984
		if (*symname) {
			do {
L
Linus Torvalds 已提交
985 986 987 988 989
				symname++;
			} while (*symname == '/');
		}
	}

J
Jan Kara 已提交
990
	brelse(epos.bh);
L
Linus Torvalds 已提交
991
	inode->i_size = elen;
992 993
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
		iinfo->i_lenAlloc = inode->i_size;
J
Jan Kara 已提交
994 995
	else
		udf_truncate_tail_extent(inode);
L
Linus Torvalds 已提交
996 997
	mark_inode_dirty(inode);

M
Marcin Slusarz 已提交
998 999
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi)
L
Linus Torvalds 已提交
1000
		goto out_no_entry;
1001
	cfi.icb.extLength = cpu_to_le32(sb->s_blocksize);
1002
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
1003
	if (UDF_SB(inode->i_sb)->s_lvid_bh) {
1004
		*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1005
			cpu_to_le32(lvid_get_unique_id(sb));
L
Linus Torvalds 已提交
1006 1007
	}
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1008
	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1009
		mark_inode_dirty(dir);
1010
	up_write(&iinfo->i_data_sem);
L
Linus Torvalds 已提交
1011
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
1012 1013
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
1014 1015 1016
	d_instantiate(dentry, inode);
	err = 0;

1017
out:
1018
	kfree(name);
L
Linus Torvalds 已提交
1019 1020
	return err;

1021
out_no_entry:
1022
	up_write(&iinfo->i_data_sem);
1023
	inode_dec_link_count(inode);
L
Linus Torvalds 已提交
1024 1025 1026 1027
	iput(inode);
	goto out;
}

1028 1029
static int udf_link(struct dentry *old_dentry, struct inode *dir,
		    struct dentry *dentry)
L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035
{
	struct inode *inode = old_dentry->d_inode;
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;

A
Al Viro 已提交
1036
	if (inode->i_nlink >= UDF_MAX_LINKS)
L
Linus Torvalds 已提交
1037 1038
		return -EMLINK;

M
Marcin Slusarz 已提交
1039 1040
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi) {
L
Linus Torvalds 已提交
1041 1042 1043
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1044
	cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1045
	if (UDF_SB(inode->i_sb)->s_lvid_bh) {
1046
		*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1047
			cpu_to_le32(lvid_get_unique_id(inode->i_sb));
L
Linus Torvalds 已提交
1048 1049
	}
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1050
	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1051
		mark_inode_dirty(dir);
1052

L
Linus Torvalds 已提交
1053
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
1054 1055
		brelse(fibh.ebh);
	brelse(fibh.sbh);
1056
	inc_nlink(inode);
L
Linus Torvalds 已提交
1057 1058
	inode->i_ctime = current_fs_time(inode->i_sb);
	mark_inode_dirty(inode);
A
Al Viro 已提交
1059
	ihold(inode);
L
Linus Torvalds 已提交
1060
	d_instantiate(dentry, inode);
1061

L
Linus Torvalds 已提交
1062 1063 1064 1065 1066 1067
	return 0;
}

/* Anybody can rename anything with this: the permission checks are left to the
 * higher-level routines.
 */
1068 1069
static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
		      struct inode *new_dir, struct dentry *new_dentry)
L
Linus Torvalds 已提交
1070
{
1071 1072
	struct inode *old_inode = old_dentry->d_inode;
	struct inode *new_inode = new_dentry->d_inode;
L
Linus Torvalds 已提交
1073
	struct udf_fileident_bh ofibh, nfibh;
M
Marcin Slusarz 已提交
1074 1075
	struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
	struct fileIdentDesc ocfi, ncfi;
L
Linus Torvalds 已提交
1076 1077
	struct buffer_head *dir_bh = NULL;
	int retval = -ENOENT;
1078
	struct kernel_lb_addr tloc;
1079
	struct udf_inode_info *old_iinfo = UDF_I(old_inode);
L
Linus Torvalds 已提交
1080

1081
	ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
M
Marcin Slusarz 已提交
1082
	if (ofi) {
L
Linus Torvalds 已提交
1083
		if (ofibh.sbh != ofibh.ebh)
J
Jan Kara 已提交
1084 1085
			brelse(ofibh.ebh);
		brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1086 1087
	}
	tloc = lelb_to_cpu(ocfi.icb.extLocation);
1088
	if (!ofi || udf_get_lb_pblock(old_dir->i_sb, &tloc, 0)
1089
	    != old_inode->i_ino)
L
Linus Torvalds 已提交
1090 1091
		goto end_rename;

1092
	nfi = udf_find_entry(new_dir, &new_dentry->d_name, &nfibh, &ncfi);
1093 1094
	if (nfi) {
		if (!new_inode) {
L
Linus Torvalds 已提交
1095
			if (nfibh.sbh != nfibh.ebh)
J
Jan Kara 已提交
1096 1097
				brelse(nfibh.ebh);
			brelse(nfibh.sbh);
L
Linus Torvalds 已提交
1098 1099 1100
			nfi = NULL;
		}
	}
1101
	if (S_ISDIR(old_inode->i_mode)) {
M
Marcin Slusarz 已提交
1102
		int offset = udf_ext0_offset(old_inode);
L
Linus Torvalds 已提交
1103

1104
		if (new_inode) {
L
Linus Torvalds 已提交
1105 1106 1107 1108 1109
			retval = -ENOTEMPTY;
			if (!empty_dir(new_inode))
				goto end_rename;
		}
		retval = -EIO;
1110
		if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
M
Marcin Slusarz 已提交
1111
			dir_fi = udf_get_fileident(
1112 1113
					old_iinfo->i_ext.i_data -
					  (old_iinfo->i_efe ?
M
Marcin Slusarz 已提交
1114 1115 1116
					   sizeof(struct extendedFileEntry) :
					   sizeof(struct fileEntry)),
					old_inode->i_sb->s_blocksize, &offset);
1117
		} else {
L
Linus Torvalds 已提交
1118 1119 1120
			dir_bh = udf_bread(old_inode, 0, 0, &retval);
			if (!dir_bh)
				goto end_rename;
M
Marcin Slusarz 已提交
1121 1122
			dir_fi = udf_get_fileident(dir_bh->b_data,
					old_inode->i_sb->s_blocksize, &offset);
L
Linus Torvalds 已提交
1123 1124 1125 1126
		}
		if (!dir_fi)
			goto end_rename;
		tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1127
		if (udf_get_lb_pblock(old_inode->i_sb, &tloc, 0) !=
M
Marcin Slusarz 已提交
1128
				old_dir->i_ino)
L
Linus Torvalds 已提交
1129 1130 1131
			goto end_rename;

		retval = -EMLINK;
A
Al Viro 已提交
1132
		if (!new_inode && new_dir->i_nlink >= UDF_MAX_LINKS)
L
Linus Torvalds 已提交
1133 1134
			goto end_rename;
	}
1135
	if (!nfi) {
M
Marcin Slusarz 已提交
1136 1137
		nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
				    &retval);
L
Linus Torvalds 已提交
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
		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;
1154
	memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(struct long_ad));
L
Linus Torvalds 已提交
1155 1156 1157
	udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);

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

1161
	if (new_inode) {
L
Linus Torvalds 已提交
1162
		new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1163
		inode_dec_link_count(new_inode);
L
Linus Torvalds 已提交
1164 1165 1166 1167
	}
	old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
	mark_inode_dirty(old_dir);

1168
	if (dir_fi) {
1169
		dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
M
Marcin Slusarz 已提交
1170 1171 1172
		udf_update_tag((char *)dir_fi,
				(sizeof(struct fileIdentDesc) +
				le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1173
		if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1174
			mark_inode_dirty(old_inode);
M
Marcin Slusarz 已提交
1175
		else
L
Linus Torvalds 已提交
1176
			mark_buffer_dirty_inode(dir_bh, old_inode);
M
Marcin Slusarz 已提交
1177

1178
		inode_dec_link_count(old_dir);
M
Marcin Slusarz 已提交
1179
		if (new_inode)
1180
			inode_dec_link_count(new_inode);
M
Marcin Slusarz 已提交
1181
		else {
1182
			inc_nlink(new_dir);
L
Linus Torvalds 已提交
1183 1184 1185 1186
			mark_inode_dirty(new_dir);
		}
	}

1187
	if (ofi) {
L
Linus Torvalds 已提交
1188
		if (ofibh.sbh != ofibh.ebh)
J
Jan Kara 已提交
1189 1190
			brelse(ofibh.ebh);
		brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1191 1192 1193 1194
	}

	retval = 0;

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

L
Linus Torvalds 已提交
1203 1204 1205
	return retval;
}

R
Rasmus Rohde 已提交
1206 1207
static struct dentry *udf_get_parent(struct dentry *child)
{
1208
	struct kernel_lb_addr tloc;
R
Rasmus Rohde 已提交
1209
	struct inode *inode = NULL;
1210
	struct qstr dotdot = {.name = "..", .len = 2};
R
Rasmus Rohde 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
	struct fileIdentDesc cfi;
	struct udf_fileident_bh fibh;

	if (!udf_find_entry(child->d_inode, &dotdot, &fibh, &cfi))
		goto out_unlock;

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

1221 1222
	tloc = lelb_to_cpu(cfi.icb.extLocation);
	inode = udf_iget(child->d_inode->i_sb, &tloc);
R
Rasmus Rohde 已提交
1223 1224 1225
	if (!inode)
		goto out_unlock;

1226
	return d_obtain_alias(inode);
R
Rasmus Rohde 已提交
1227 1228 1229 1230 1231 1232 1233 1234 1235
out_unlock:
	return ERR_PTR(-EACCES);
}


static struct dentry *udf_nfs_get_inode(struct super_block *sb, u32 block,
					u16 partref, __u32 generation)
{
	struct inode *inode;
1236
	struct kernel_lb_addr loc;
R
Rasmus Rohde 已提交
1237 1238 1239 1240 1241 1242

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

	loc.logicalBlockNum = block;
	loc.partitionReferenceNum = partref;
1243
	inode = udf_iget(sb, &loc);
R
Rasmus Rohde 已提交
1244 1245 1246 1247 1248 1249 1250 1251

	if (inode == NULL)
		return ERR_PTR(-ENOMEM);

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

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);
}
static int udf_encode_fh(struct dentry *de, __u32 *fh, int *lenp,
			 int connectable)
{
	int len = *lenp;
	struct inode *inode =  de->d_inode;
1282
	struct kernel_lb_addr location = UDF_I(inode)->i_location;
R
Rasmus Rohde 已提交
1283 1284 1285
	struct fid *fid = (struct fid *)fh;
	int type = FILEID_UDF_WITHOUT_PARENT;

1286 1287
	if (connectable && (len < 5)) {
		*lenp = 5;
R
Rasmus Rohde 已提交
1288
		return 255;
1289 1290 1291 1292
	} else if (len < 3) {
		*lenp = 3;
		return 255;
	}
R
Rasmus Rohde 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320

	*lenp = 3;
	fid->udf.block = location.logicalBlockNum;
	fid->udf.partref = location.partitionReferenceNum;
	fid->udf.generation = inode->i_generation;

	if (connectable && !S_ISDIR(inode->i_mode)) {
		spin_lock(&de->d_lock);
		inode = de->d_parent->d_inode;
		location = UDF_I(inode)->i_location;
		fid->udf.parent_block = location.logicalBlockNum;
		fid->udf.parent_partref = location.partitionReferenceNum;
		fid->udf.parent_generation = inode->i_generation;
		spin_unlock(&de->d_lock);
		*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,
};

1321
const struct inode_operations udf_dir_inode_operations = {
1322 1323 1324 1325 1326 1327 1328 1329 1330
	.lookup				= udf_lookup,
	.create				= udf_create,
	.link				= udf_link,
	.unlink				= udf_unlink,
	.symlink			= udf_symlink,
	.mkdir				= udf_mkdir,
	.rmdir				= udf_rmdir,
	.mknod				= udf_mknod,
	.rename				= udf_rename,
L
Linus Torvalds 已提交
1331
};
1332 1333 1334 1335 1336
const struct inode_operations udf_symlink_inode_operations = {
	.readlink	= generic_readlink,
	.follow_link	= page_follow_link_light,
	.put_link	= page_put_link,
};