namei.c 35.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * 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/smp_lock.h>
#include <linux/buffer_head.h>
A
Alexey Dobriyan 已提交
32
#include <linux/sched.h>
33
#include <linux/crc-itu-t.h>
R
Rasmus Rohde 已提交
34
#include <linux/exportfs.h>
L
Linus Torvalds 已提交
35

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

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

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

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

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

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

	offset += liu;

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

	offset += lfi;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

R
Rasmus Rohde 已提交
230 231 232 233 234 235
		if ((cfi->fileCharacteristics & FID_FILE_CHAR_PARENT) &&
		    isdotdot) {
			brelse(epos.bh);
			return fi;
		}

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

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

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

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

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

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

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

284
	if (udf_find_entry(dir, &dentry->d_name, &fibh, &cfi)) {
285 286
		struct kernel_lb_addr loc;

L
Linus Torvalds 已提交
287
		if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
288 289
			brelse(fibh.ebh);
		brelse(fibh.sbh);
L
Linus Torvalds 已提交
290

291 292
		loc = lelb_to_cpu(cfi.icb.extLocation);
		inode = udf_iget(dir->i_sb, &loc);
293
		if (!inode) {
L
Linus Torvalds 已提交
294 295 296 297 298
			unlock_kernel();
			return ERR_PTR(-EACCES);
		}
	}
	unlock_kernel();
299

R
Rasmus Rohde 已提交
300
	return d_splice_alias(inode, dentry);
L
Linus Torvalds 已提交
301 302
}

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

324 325 326 327 328 329 330
	fibh->sbh = fibh->ebh = NULL;
	name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
	if (!name) {
		*err = -ENOMEM;
		goto out_err;
	}

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

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

348
	f_pos = udf_ext0_offset(dir);
L
Linus Torvalds 已提交
349

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

M
Marcin Slusarz 已提交
369 370
		fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
		if (!fibh->sbh) {
L
Linus Torvalds 已提交
371
			*err = -EIO;
372
			goto out_err;
L
Linus Torvalds 已提交
373 374
		}

375
		block = dinfo->i_location.logicalBlockNum;
L
Linus Torvalds 已提交
376 377
	}

378
	while (f_pos < size) {
379 380
		fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
					&elen, &offset);
L
Linus Torvalds 已提交
381

382
		if (!fi) {
L
Linus Torvalds 已提交
383
			*err = -EIO;
384
			goto out_err;
L
Linus Torvalds 已提交
385 386 387 388 389
		}

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

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

409
add:
L
Linus Torvalds 已提交
410 411
	f_pos += nfidlen;

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

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

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

		block = eloc.logicalBlockNum + ((elen - 1) >>
474
						dir->i_sb->s_blocksize_bits);
M
Marcin Slusarz 已提交
475
		fibh->ebh = udf_bread(dir,
476
				f_pos >> dir->i_sb->s_blocksize_bits, 1, err);
477 478
		if (!fibh->ebh)
			goto out_err;
L
Linus Torvalds 已提交
479

480
		if (!fibh->soffset) {
J
Jan Kara 已提交
481
			if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
482
			    (EXT_RECORDED_ALLOCATED >> 30)) {
L
Linus Torvalds 已提交
483
				block = eloc.logicalBlockNum + ((elen - 1) >>
484
					dir->i_sb->s_blocksize_bits);
M
Marcin Slusarz 已提交
485
			} else
486
				block++;
L
Linus Torvalds 已提交
487

J
Jan Kara 已提交
488
			brelse(fibh->sbh);
L
Linus Torvalds 已提交
489 490
			fibh->sbh = fibh->ebh;
			fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
491
		} else {
L
Linus Torvalds 已提交
492
			fi = (struct fileIdentDesc *)
M
Marcin Slusarz 已提交
493 494
				(fibh->sbh->b_data + sb->s_blocksize +
					fibh->soffset);
L
Linus Torvalds 已提交
495 496 497 498
		}
	}

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

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

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

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

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

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

	lock_kernel();
	inode = udf_new_inode(dir, mode, &err);
567
	if (!inode) {
L
Linus Torvalds 已提交
568 569 570 571
		unlock_kernel();
		return err;
	}

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

L
Linus Torvalds 已提交
602 603 604
	return 0;
}

605 606
static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
		     dev_t rdev)
L
Linus Torvalds 已提交
607
{
608
	struct inode *inode;
L
Linus Torvalds 已提交
609 610 611
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;
612
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
613 614 615 616 617 618 619 620 621 622

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

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

623
	iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
624
	init_special_inode(inode, mode, rdev);
M
Marcin Slusarz 已提交
625 626
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi) {
627
		inode->i_nlink--;
L
Linus Torvalds 已提交
628 629 630 631 632 633
		mark_inode_dirty(inode);
		iput(inode);
		unlock_kernel();
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
634
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
635
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
636
		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
637
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
638
	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
639 640 641 642
		mark_inode_dirty(dir);
	mark_inode_dirty(inode);

	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
643 644
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
645 646
	d_instantiate(dentry, inode);
	err = 0;
647 648

out:
L
Linus Torvalds 已提交
649 650 651 652
	unlock_kernel();
	return err;
}

653
static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
L
Linus Torvalds 已提交
654
{
655
	struct inode *inode;
L
Linus Torvalds 已提交
656 657 658
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;
659 660
	struct udf_inode_info *dinfo = UDF_I(dir);
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
661 662 663

	lock_kernel();
	err = -EMLINK;
664
	if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
L
Linus Torvalds 已提交
665 666 667
		goto out;

	err = -EIO;
668
	inode = udf_new_inode(dir, S_IFDIR | mode, &err);
L
Linus Torvalds 已提交
669 670 671
	if (!inode)
		goto out;

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

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

out:
L
Linus Torvalds 已提交
715 716 717 718 719 720 721 722 723
	unlock_kernel();
	return err;
}

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

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

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

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

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

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

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

L
Linus Torvalds 已提交
785 786 787
	return 1;
}

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

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

	retval = -EIO;
	tloc = lelb_to_cpu(cfi.icb.extLocation);
804
	if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
L
Linus Torvalds 已提交
805 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)
		udf_warning(inode->i_sb, "udf_rmdir",
814 815
			    "empty directory has nlink != 2 (%d)",
			    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 832
	unlock_kernel();
	return retval;
}

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

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

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

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

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

out:
L
Linus Torvalds 已提交
873 874 875 876
	unlock_kernel();
	return retval;
}

877 878
static int udf_symlink(struct inode *dir, struct dentry *dentry,
		       const char *symname)
L
Linus Torvalds 已提交
879
{
880
	struct inode *inode;
L
Linus Torvalds 已提交
881
	struct pathComponent *pc;
A
Al Viro 已提交
882
	const char *compstart;
L
Linus Torvalds 已提交
883
	struct udf_fileident_bh fibh;
884
	struct extent_position epos = {};
L
Linus Torvalds 已提交
885 886 887
	int eoffset, elen = 0;
	struct fileIdentDesc *fi;
	struct fileIdentDesc cfi;
A
Al Viro 已提交
888
	uint8_t *ea;
L
Linus Torvalds 已提交
889 890
	int err;
	int block;
A
Al Viro 已提交
891
	unsigned char *name = NULL;
L
Linus Torvalds 已提交
892
	int namelen;
M
Marcin Slusarz 已提交
893
	struct buffer_head *bh;
894
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
895 896

	lock_kernel();
897
	inode = udf_new_inode(dir, S_IFLNK | S_IRWXUGO, &err);
M
Marcin Slusarz 已提交
898
	if (!inode)
L
Linus Torvalds 已提交
899 900
		goto out;

901 902 903 904 905 906
	name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
	if (!name) {
		err = -ENOMEM;
		goto out_no_entry;
	}

907
	iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
908
	inode->i_data.a_ops = &udf_symlink_aops;
909
	inode->i_op = &udf_symlink_inode_operations;
L
Linus Torvalds 已提交
910

911
	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
912
		struct kernel_lb_addr eloc;
913
		uint32_t bsize;
L
Linus Torvalds 已提交
914 915

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

		block = udf_get_pblock(inode->i_sb, block,
932
				iinfo->i_location.partitionReferenceNum,
M
Marcin Slusarz 已提交
933
				0);
J
Jan Kara 已提交
934
		epos.bh = udf_tgetblk(inode->i_sb, block);
J
Jan Kara 已提交
935 936 937 938 939 940
		lock_buffer(epos.bh);
		memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
		set_buffer_uptodate(epos.bh);
		unlock_buffer(epos.bh);
		mark_buffer_dirty_inode(epos.bh, inode);
		ea = epos.bh->b_data + udf_ext0_offset(inode);
941 942
	} else
		ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
L
Linus Torvalds 已提交
943 944 945 946

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

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

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

	err = -ENAMETOOLONG;

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

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

A
Al Viro 已提交
966
		compstart = symname;
L
Linus Torvalds 已提交
967

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

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

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

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

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

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

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

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

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

1045
out:
1046
	kfree(name);
L
Linus Torvalds 已提交
1047 1048 1049
	unlock_kernel();
	return err;

1050
out_no_entry:
1051
	inode_dec_link_count(inode);
L
Linus Torvalds 已提交
1052 1053 1054 1055
	iput(inode);
	goto out;
}

1056 1057
static int udf_link(struct dentry *old_dentry, struct inode *dir,
		    struct dentry *dentry)
L
Linus Torvalds 已提交
1058 1059 1060 1061 1062
{
	struct inode *inode = old_dentry->d_inode;
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;
M
Marcin Slusarz 已提交
1063
	struct buffer_head *bh;
L
Linus Torvalds 已提交
1064 1065

	lock_kernel();
1066
	if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
L
Linus Torvalds 已提交
1067 1068 1069 1070
		unlock_kernel();
		return -EMLINK;
	}

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

L
Linus Torvalds 已提交
1098
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
1099 1100
		brelse(fibh.ebh);
	brelse(fibh.sbh);
1101
	inc_nlink(inode);
L
Linus Torvalds 已提交
1102 1103 1104 1105 1106
	inode->i_ctime = current_fs_time(inode->i_sb);
	mark_inode_dirty(inode);
	atomic_inc(&inode->i_count);
	d_instantiate(dentry, inode);
	unlock_kernel();
1107

L
Linus Torvalds 已提交
1108 1109 1110 1111 1112 1113
	return 0;
}

/* Anybody can rename anything with this: the permission checks are left to the
 * higher-level routines.
 */
1114 1115
static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
		      struct inode *new_dir, struct dentry *new_dentry)
L
Linus Torvalds 已提交
1116
{
1117 1118
	struct inode *old_inode = old_dentry->d_inode;
	struct inode *new_inode = new_dentry->d_inode;
L
Linus Torvalds 已提交
1119
	struct udf_fileident_bh ofibh, nfibh;
M
Marcin Slusarz 已提交
1120 1121
	struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
	struct fileIdentDesc ocfi, ncfi;
L
Linus Torvalds 已提交
1122 1123
	struct buffer_head *dir_bh = NULL;
	int retval = -ENOENT;
1124
	struct kernel_lb_addr tloc;
1125
	struct udf_inode_info *old_iinfo = UDF_I(old_inode);
L
Linus Torvalds 已提交
1126 1127

	lock_kernel();
1128
	ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
M
Marcin Slusarz 已提交
1129
	if (ofi) {
L
Linus Torvalds 已提交
1130
		if (ofibh.sbh != ofibh.ebh)
J
Jan Kara 已提交
1131 1132
			brelse(ofibh.ebh);
		brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1133 1134
	}
	tloc = lelb_to_cpu(ocfi.icb.extLocation);
1135
	if (!ofi || udf_get_lb_pblock(old_dir->i_sb, &tloc, 0)
1136
	    != old_inode->i_ino)
L
Linus Torvalds 已提交
1137 1138
		goto end_rename;

1139
	nfi = udf_find_entry(new_dir, &new_dentry->d_name, &nfibh, &ncfi);
1140 1141
	if (nfi) {
		if (!new_inode) {
L
Linus Torvalds 已提交
1142
			if (nfibh.sbh != nfibh.ebh)
J
Jan Kara 已提交
1143 1144
				brelse(nfibh.ebh);
			brelse(nfibh.sbh);
L
Linus Torvalds 已提交
1145 1146 1147
			nfi = NULL;
		}
	}
1148
	if (S_ISDIR(old_inode->i_mode)) {
M
Marcin Slusarz 已提交
1149
		int offset = udf_ext0_offset(old_inode);
L
Linus Torvalds 已提交
1150

1151
		if (new_inode) {
L
Linus Torvalds 已提交
1152 1153 1154 1155 1156
			retval = -ENOTEMPTY;
			if (!empty_dir(new_inode))
				goto end_rename;
		}
		retval = -EIO;
1157
		if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
M
Marcin Slusarz 已提交
1158
			dir_fi = udf_get_fileident(
1159 1160
					old_iinfo->i_ext.i_data -
					  (old_iinfo->i_efe ?
M
Marcin Slusarz 已提交
1161 1162 1163
					   sizeof(struct extendedFileEntry) :
					   sizeof(struct fileEntry)),
					old_inode->i_sb->s_blocksize, &offset);
1164
		} else {
L
Linus Torvalds 已提交
1165 1166 1167
			dir_bh = udf_bread(old_inode, 0, 0, &retval);
			if (!dir_bh)
				goto end_rename;
M
Marcin Slusarz 已提交
1168 1169
			dir_fi = udf_get_fileident(dir_bh->b_data,
					old_inode->i_sb->s_blocksize, &offset);
L
Linus Torvalds 已提交
1170 1171 1172 1173
		}
		if (!dir_fi)
			goto end_rename;
		tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1174
		if (udf_get_lb_pblock(old_inode->i_sb, &tloc, 0) !=
M
Marcin Slusarz 已提交
1175
				old_dir->i_ino)
L
Linus Torvalds 已提交
1176 1177 1178
			goto end_rename;

		retval = -EMLINK;
M
Marcin Slusarz 已提交
1179 1180 1181
		if (!new_inode &&
			new_dir->i_nlink >=
				(256 << sizeof(new_dir->i_nlink)) - 1)
L
Linus Torvalds 已提交
1182 1183
			goto end_rename;
	}
1184
	if (!nfi) {
M
Marcin Slusarz 已提交
1185 1186
		nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
				    &retval);
L
Linus Torvalds 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
		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;
1203
	memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(struct long_ad));
L
Linus Torvalds 已提交
1204 1205 1206
	udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);

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

1210
	if (new_inode) {
L
Linus Torvalds 已提交
1211
		new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1212
		inode_dec_link_count(new_inode);
L
Linus Torvalds 已提交
1213 1214 1215 1216
	}
	old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
	mark_inode_dirty(old_dir);

1217
	if (dir_fi) {
1218
		dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
M
Marcin Slusarz 已提交
1219 1220 1221
		udf_update_tag((char *)dir_fi,
				(sizeof(struct fileIdentDesc) +
				le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1222
		if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1223
			mark_inode_dirty(old_inode);
M
Marcin Slusarz 已提交
1224
		else
L
Linus Torvalds 已提交
1225
			mark_buffer_dirty_inode(dir_bh, old_inode);
M
Marcin Slusarz 已提交
1226

1227
		inode_dec_link_count(old_dir);
M
Marcin Slusarz 已提交
1228
		if (new_inode)
1229
			inode_dec_link_count(new_inode);
M
Marcin Slusarz 已提交
1230
		else {
1231
			inc_nlink(new_dir);
L
Linus Torvalds 已提交
1232 1233 1234 1235
			mark_inode_dirty(new_dir);
		}
	}

1236
	if (ofi) {
L
Linus Torvalds 已提交
1237
		if (ofibh.sbh != ofibh.ebh)
J
Jan Kara 已提交
1238 1239
			brelse(ofibh.ebh);
		brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1240 1241 1242 1243
	}

	retval = 0;

1244
end_rename:
J
Jan Kara 已提交
1245
	brelse(dir_bh);
1246
	if (nfi) {
L
Linus Torvalds 已提交
1247
		if (nfibh.sbh != nfibh.ebh)
J
Jan Kara 已提交
1248 1249
			brelse(nfibh.ebh);
		brelse(nfibh.sbh);
L
Linus Torvalds 已提交
1250 1251
	}
	unlock_kernel();
1252

L
Linus Torvalds 已提交
1253 1254 1255
	return retval;
}

R
Rasmus Rohde 已提交
1256 1257
static struct dentry *udf_get_parent(struct dentry *child)
{
1258
	struct kernel_lb_addr tloc;
R
Rasmus Rohde 已提交
1259
	struct inode *inode = NULL;
1260
	struct qstr dotdot = {.name = "..", .len = 2};
R
Rasmus Rohde 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
	struct fileIdentDesc cfi;
	struct udf_fileident_bh fibh;

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

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

1272 1273
	tloc = lelb_to_cpu(cfi.icb.extLocation);
	inode = udf_iget(child->d_inode->i_sb, &tloc);
R
Rasmus Rohde 已提交
1274 1275 1276 1277
	if (!inode)
		goto out_unlock;
	unlock_kernel();

1278
	return d_obtain_alias(inode);
R
Rasmus Rohde 已提交
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
out_unlock:
	unlock_kernel();
	return ERR_PTR(-EACCES);
}


static struct dentry *udf_nfs_get_inode(struct super_block *sb, u32 block,
					u16 partref, __u32 generation)
{
	struct inode *inode;
1289
	struct kernel_lb_addr loc;
R
Rasmus Rohde 已提交
1290 1291 1292 1293 1294 1295

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

	loc.logicalBlockNum = block;
	loc.partitionReferenceNum = partref;
1296
	inode = udf_iget(sb, &loc);
R
Rasmus Rohde 已提交
1297 1298 1299 1300 1301 1302 1303 1304

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

	if (generation && inode->i_generation != generation) {
		iput(inode);
		return ERR_PTR(-ESTALE);
	}
1305
	return d_obtain_alias(inode);
R
Rasmus Rohde 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
}

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;
1335
	struct kernel_lb_addr location = UDF_I(inode)->i_location;
R
Rasmus Rohde 已提交
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	struct fid *fid = (struct fid *)fh;
	int type = FILEID_UDF_WITHOUT_PARENT;

	if (len < 3 || (connectable && len < 5))
		return 255;

	*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,
};

1369
const struct inode_operations udf_dir_inode_operations = {
1370 1371 1372 1373 1374 1375 1376 1377 1378
	.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 已提交
1379
};
1380 1381 1382 1383 1384
const struct inode_operations udf_symlink_inode_operations = {
	.readlink	= generic_readlink,
	.follow_link	= page_follow_link_light,
	.put_link	= page_put_link,
};