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

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 232 233 234 235 236
		if ((cfi->fileCharacteristics & FID_FILE_CHAR_PARENT) &&
		    isdotdot) {
			brelse(epos.bh);
			return fi;
		}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

566
	dquot_initialize(dir);
567

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

575 576
	iinfo = UDF_I(inode);
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
577 578 579 580 581 582 583 584
		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;
	inode->i_mode = mode;
	mark_inode_dirty(inode);

M
Marcin Slusarz 已提交
585 586
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi) {
587
		inode->i_nlink--;
L
Linus Torvalds 已提交
588 589 590 591 592 593
		mark_inode_dirty(inode);
		iput(inode);
		unlock_kernel();
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
594
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
595
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
596
		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
597
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
598
	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
599 600
		mark_inode_dirty(dir);
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
601 602
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
603 604
	unlock_kernel();
	d_instantiate(dentry, inode);
605

L
Linus Torvalds 已提交
606 607 608
	return 0;
}

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

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

621
	dquot_initialize(dir);
622

L
Linus Torvalds 已提交
623 624 625 626 627 628
	lock_kernel();
	err = -EIO;
	inode = udf_new_inode(dir, mode, &err);
	if (!inode)
		goto out;

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

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

out:
L
Linus Torvalds 已提交
656 657 658 659
	unlock_kernel();
	return err;
}

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

669
	dquot_initialize(dir);
670

L
Linus Torvalds 已提交
671 672
	lock_kernel();
	err = -EMLINK;
673
	if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
L
Linus Torvalds 已提交
674 675 676 677 678 679 680
		goto out;

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

681
	iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
682 683
	inode->i_op = &udf_dir_inode_operations;
	inode->i_fop = &udf_dir_operations;
M
Marcin Slusarz 已提交
684 685
	fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
	if (!fi) {
L
Linus Torvalds 已提交
686 687 688 689 690 691 692
		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);
693
	cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
694
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
695
		cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
M
Marcin Slusarz 已提交
696 697
	cfi.fileCharacteristics =
			FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
L
Linus Torvalds 已提交
698
	udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
J
Jan Kara 已提交
699
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
700 701 702 703 704
	inode->i_mode = S_IFDIR | mode;
	if (dir->i_mode & S_ISGID)
		inode->i_mode |= S_ISGID;
	mark_inode_dirty(inode);

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

out:
L
Linus Torvalds 已提交
727 728 729 730 731 732 733 734 735
	unlock_kernel();
	return err;
}

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

744 745
	f_pos = udf_ext0_offset(dir);
	fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
L
Linus Torvalds 已提交
746

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

M
Marcin Slusarz 已提交
761 762
		fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
		if (!fibh.sbh) {
J
Jan Kara 已提交
763
			brelse(epos.bh);
L
Linus Torvalds 已提交
764 765
			return 0;
		}
766
	} else {
J
Jan Kara 已提交
767
		brelse(epos.bh);
L
Linus Torvalds 已提交
768 769 770
		return 0;
	}

771
	while (f_pos < size) {
772 773 774
		fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
					&elen, &offset);
		if (!fi) {
L
Linus Torvalds 已提交
775
			if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
776 777 778
				brelse(fibh.ebh);
			brelse(fibh.sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
779 780 781
			return 0;
		}

782 783
		if (cfi.lengthFileIdent &&
		    (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
L
Linus Torvalds 已提交
784
			if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
785 786 787
				brelse(fibh.ebh);
			brelse(fibh.sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
788 789 790
			return 0;
		}
	}
791

L
Linus Torvalds 已提交
792
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
793 794 795
		brelse(fibh.ebh);
	brelse(fibh.sbh);
	brelse(epos.bh);
796

L
Linus Torvalds 已提交
797 798 799
	return 1;
}

800
static int udf_rmdir(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
801 802
{
	int retval;
803
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
804 805
	struct udf_fileident_bh fibh;
	struct fileIdentDesc *fi, cfi;
806
	struct kernel_lb_addr tloc;
L
Linus Torvalds 已提交
807

808
	dquot_initialize(dir);
809

L
Linus Torvalds 已提交
810 811
	retval = -ENOENT;
	lock_kernel();
812
	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
L
Linus Torvalds 已提交
813 814 815 816 817
	if (!fi)
		goto out;

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

837
end_rmdir:
L
Linus Torvalds 已提交
838
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
839 840
		brelse(fibh.ebh);
	brelse(fibh.sbh);
841 842

out:
L
Linus Torvalds 已提交
843 844 845 846
	unlock_kernel();
	return retval;
}

847
static int udf_unlink(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
848 849
{
	int retval;
850
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
851 852 853
	struct udf_fileident_bh fibh;
	struct fileIdentDesc *fi;
	struct fileIdentDesc cfi;
854
	struct kernel_lb_addr tloc;
L
Linus Torvalds 已提交
855

856
	dquot_initialize(dir);
857

L
Linus Torvalds 已提交
858 859
	retval = -ENOENT;
	lock_kernel();
860
	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
L
Linus Torvalds 已提交
861 862 863 864 865
	if (!fi)
		goto out;

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

869
	if (!inode->i_nlink) {
L
Linus Torvalds 已提交
870
		udf_debug("Deleting nonexistent file (%lu), %d\n",
871
			  inode->i_ino, inode->i_nlink);
L
Linus Torvalds 已提交
872 873 874 875 876 877 878
		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);
879
	inode_dec_link_count(inode);
L
Linus Torvalds 已提交
880 881 882
	inode->i_ctime = dir->i_ctime;
	retval = 0;

883
end_unlink:
L
Linus Torvalds 已提交
884
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
885 886
		brelse(fibh.ebh);
	brelse(fibh.sbh);
887 888

out:
L
Linus Torvalds 已提交
889 890 891 892
	unlock_kernel();
	return retval;
}

893 894
static int udf_symlink(struct inode *dir, struct dentry *dentry,
		       const char *symname)
L
Linus Torvalds 已提交
895
{
896
	struct inode *inode;
L
Linus Torvalds 已提交
897
	struct pathComponent *pc;
A
Al Viro 已提交
898
	const char *compstart;
L
Linus Torvalds 已提交
899
	struct udf_fileident_bh fibh;
900
	struct extent_position epos = {};
L
Linus Torvalds 已提交
901 902 903
	int eoffset, elen = 0;
	struct fileIdentDesc *fi;
	struct fileIdentDesc cfi;
A
Al Viro 已提交
904
	uint8_t *ea;
L
Linus Torvalds 已提交
905 906
	int err;
	int block;
A
Al Viro 已提交
907
	unsigned char *name = NULL;
L
Linus Torvalds 已提交
908
	int namelen;
M
Marcin Slusarz 已提交
909
	struct buffer_head *bh;
910
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
911

912
	dquot_initialize(dir);
913

L
Linus Torvalds 已提交
914
	lock_kernel();
M
Marcin Slusarz 已提交
915 916
	inode = udf_new_inode(dir, S_IFLNK, &err);
	if (!inode)
L
Linus Torvalds 已提交
917 918
		goto out;

919 920 921 922 923 924
	name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
	if (!name) {
		err = -ENOMEM;
		goto out_no_entry;
	}

925
	iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
926 927 928 929
	inode->i_mode = S_IFLNK | S_IRWXUGO;
	inode->i_data.a_ops = &udf_symlink_aops;
	inode->i_op = &page_symlink_inode_operations;

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

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

		block = udf_get_pblock(inode->i_sb, block,
951
				iinfo->i_location.partitionReferenceNum,
M
Marcin Slusarz 已提交
952
				0);
J
Jan Kara 已提交
953
		epos.bh = udf_tgetblk(inode->i_sb, block);
J
Jan Kara 已提交
954 955 956 957 958 959
		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);
960 961
	} else
		ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
L
Linus Torvalds 已提交
962 963 964 965

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

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

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

	err = -ENAMETOOLONG;

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

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

A
Al Viro 已提交
985
		compstart = symname;
L
Linus Torvalds 已提交
986

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

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

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

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

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

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

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

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

M
Marcin Slusarz 已提交
1034 1035
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi)
L
Linus Torvalds 已提交
1036 1037
		goto out_no_entry;
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1038
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
M
Marcin Slusarz 已提交
1039 1040
	bh = UDF_SB(inode->i_sb)->s_lvid_bh;
	if (bh) {
M
Marcin Slusarz 已提交
1041 1042
		struct logicalVolIntegrityDesc *lvid =
				(struct logicalVolIntegrityDesc *)bh->b_data;
L
Linus Torvalds 已提交
1043 1044
		struct logicalVolHeaderDesc *lvhd;
		uint64_t uniqueID;
M
Marcin Slusarz 已提交
1045 1046
		lvhd = (struct logicalVolHeaderDesc *)
				lvid->logicalVolContentsUse;
L
Linus Torvalds 已提交
1047
		uniqueID = le64_to_cpu(lvhd->uniqueID);
1048 1049
		*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
			cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
1050 1051 1052
		if (!(++uniqueID & 0x00000000FFFFFFFFUL))
			uniqueID += 16;
		lvhd->uniqueID = cpu_to_le64(uniqueID);
M
Marcin Slusarz 已提交
1053
		mark_buffer_dirty(bh);
L
Linus Torvalds 已提交
1054 1055
	}
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1056
	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1057 1058
		mark_inode_dirty(dir);
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
1059 1060
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
1061 1062 1063
	d_instantiate(dentry, inode);
	err = 0;

1064
out:
1065
	kfree(name);
L
Linus Torvalds 已提交
1066 1067 1068
	unlock_kernel();
	return err;

1069
out_no_entry:
1070
	inode_dec_link_count(inode);
L
Linus Torvalds 已提交
1071 1072 1073 1074
	iput(inode);
	goto out;
}

1075 1076
static int udf_link(struct dentry *old_dentry, struct inode *dir,
		    struct dentry *dentry)
L
Linus Torvalds 已提交
1077 1078 1079 1080 1081
{
	struct inode *inode = old_dentry->d_inode;
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;
M
Marcin Slusarz 已提交
1082
	struct buffer_head *bh;
L
Linus Torvalds 已提交
1083

1084
	dquot_initialize(dir);
1085

L
Linus Torvalds 已提交
1086
	lock_kernel();
1087
	if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
L
Linus Torvalds 已提交
1088 1089 1090 1091
		unlock_kernel();
		return -EMLINK;
	}

M
Marcin Slusarz 已提交
1092 1093
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi) {
L
Linus Torvalds 已提交
1094 1095 1096 1097
		unlock_kernel();
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1098
	cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
M
Marcin Slusarz 已提交
1099 1100
	bh = UDF_SB(inode->i_sb)->s_lvid_bh;
	if (bh) {
M
Marcin Slusarz 已提交
1101 1102
		struct logicalVolIntegrityDesc *lvid =
				(struct logicalVolIntegrityDesc *)bh->b_data;
L
Linus Torvalds 已提交
1103 1104
		struct logicalVolHeaderDesc *lvhd;
		uint64_t uniqueID;
M
Marcin Slusarz 已提交
1105 1106
		lvhd = (struct logicalVolHeaderDesc *)
				(lvid->logicalVolContentsUse);
L
Linus Torvalds 已提交
1107
		uniqueID = le64_to_cpu(lvhd->uniqueID);
1108 1109
		*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
			cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
1110 1111 1112
		if (!(++uniqueID & 0x00000000FFFFFFFFUL))
			uniqueID += 16;
		lvhd->uniqueID = cpu_to_le64(uniqueID);
M
Marcin Slusarz 已提交
1113
		mark_buffer_dirty(bh);
L
Linus Torvalds 已提交
1114 1115
	}
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1116
	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1117
		mark_inode_dirty(dir);
1118

L
Linus Torvalds 已提交
1119
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
1120 1121
		brelse(fibh.ebh);
	brelse(fibh.sbh);
1122
	inc_nlink(inode);
L
Linus Torvalds 已提交
1123 1124 1125 1126 1127
	inode->i_ctime = current_fs_time(inode->i_sb);
	mark_inode_dirty(inode);
	atomic_inc(&inode->i_count);
	d_instantiate(dentry, inode);
	unlock_kernel();
1128

L
Linus Torvalds 已提交
1129 1130 1131 1132 1133 1134
	return 0;
}

/* Anybody can rename anything with this: the permission checks are left to the
 * higher-level routines.
 */
1135 1136
static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
		      struct inode *new_dir, struct dentry *new_dentry)
L
Linus Torvalds 已提交
1137
{
1138 1139
	struct inode *old_inode = old_dentry->d_inode;
	struct inode *new_inode = new_dentry->d_inode;
L
Linus Torvalds 已提交
1140
	struct udf_fileident_bh ofibh, nfibh;
M
Marcin Slusarz 已提交
1141 1142
	struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
	struct fileIdentDesc ocfi, ncfi;
L
Linus Torvalds 已提交
1143 1144
	struct buffer_head *dir_bh = NULL;
	int retval = -ENOENT;
1145
	struct kernel_lb_addr tloc;
1146
	struct udf_inode_info *old_iinfo = UDF_I(old_inode);
L
Linus Torvalds 已提交
1147

1148 1149
	dquot_initialize(old_dir);
	dquot_initialize(new_dir);
1150

L
Linus Torvalds 已提交
1151
	lock_kernel();
1152
	ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
M
Marcin Slusarz 已提交
1153
	if (ofi) {
L
Linus Torvalds 已提交
1154
		if (ofibh.sbh != ofibh.ebh)
J
Jan Kara 已提交
1155 1156
			brelse(ofibh.ebh);
		brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1157 1158
	}
	tloc = lelb_to_cpu(ocfi.icb.extLocation);
1159
	if (!ofi || udf_get_lb_pblock(old_dir->i_sb, &tloc, 0)
1160
	    != old_inode->i_ino)
L
Linus Torvalds 已提交
1161 1162
		goto end_rename;

1163
	nfi = udf_find_entry(new_dir, &new_dentry->d_name, &nfibh, &ncfi);
1164 1165
	if (nfi) {
		if (!new_inode) {
L
Linus Torvalds 已提交
1166
			if (nfibh.sbh != nfibh.ebh)
J
Jan Kara 已提交
1167 1168
				brelse(nfibh.ebh);
			brelse(nfibh.sbh);
L
Linus Torvalds 已提交
1169 1170 1171
			nfi = NULL;
		}
	}
1172
	if (S_ISDIR(old_inode->i_mode)) {
M
Marcin Slusarz 已提交
1173
		int offset = udf_ext0_offset(old_inode);
L
Linus Torvalds 已提交
1174

1175
		if (new_inode) {
L
Linus Torvalds 已提交
1176 1177 1178 1179 1180
			retval = -ENOTEMPTY;
			if (!empty_dir(new_inode))
				goto end_rename;
		}
		retval = -EIO;
1181
		if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
M
Marcin Slusarz 已提交
1182
			dir_fi = udf_get_fileident(
1183 1184
					old_iinfo->i_ext.i_data -
					  (old_iinfo->i_efe ?
M
Marcin Slusarz 已提交
1185 1186 1187
					   sizeof(struct extendedFileEntry) :
					   sizeof(struct fileEntry)),
					old_inode->i_sb->s_blocksize, &offset);
1188
		} else {
L
Linus Torvalds 已提交
1189 1190 1191
			dir_bh = udf_bread(old_inode, 0, 0, &retval);
			if (!dir_bh)
				goto end_rename;
M
Marcin Slusarz 已提交
1192 1193
			dir_fi = udf_get_fileident(dir_bh->b_data,
					old_inode->i_sb->s_blocksize, &offset);
L
Linus Torvalds 已提交
1194 1195 1196 1197
		}
		if (!dir_fi)
			goto end_rename;
		tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1198
		if (udf_get_lb_pblock(old_inode->i_sb, &tloc, 0) !=
M
Marcin Slusarz 已提交
1199
				old_dir->i_ino)
L
Linus Torvalds 已提交
1200 1201 1202
			goto end_rename;

		retval = -EMLINK;
M
Marcin Slusarz 已提交
1203 1204 1205
		if (!new_inode &&
			new_dir->i_nlink >=
				(256 << sizeof(new_dir->i_nlink)) - 1)
L
Linus Torvalds 已提交
1206 1207
			goto end_rename;
	}
1208
	if (!nfi) {
M
Marcin Slusarz 已提交
1209 1210
		nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
				    &retval);
L
Linus Torvalds 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
		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;
1227
	memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(struct long_ad));
L
Linus Torvalds 已提交
1228 1229 1230
	udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);

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

1234
	if (new_inode) {
L
Linus Torvalds 已提交
1235
		new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1236
		inode_dec_link_count(new_inode);
L
Linus Torvalds 已提交
1237 1238 1239 1240
	}
	old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
	mark_inode_dirty(old_dir);

1241
	if (dir_fi) {
1242
		dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
M
Marcin Slusarz 已提交
1243 1244 1245
		udf_update_tag((char *)dir_fi,
				(sizeof(struct fileIdentDesc) +
				le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1246
		if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1247
			mark_inode_dirty(old_inode);
M
Marcin Slusarz 已提交
1248
		else
L
Linus Torvalds 已提交
1249
			mark_buffer_dirty_inode(dir_bh, old_inode);
M
Marcin Slusarz 已提交
1250

1251
		inode_dec_link_count(old_dir);
M
Marcin Slusarz 已提交
1252
		if (new_inode)
1253
			inode_dec_link_count(new_inode);
M
Marcin Slusarz 已提交
1254
		else {
1255
			inc_nlink(new_dir);
L
Linus Torvalds 已提交
1256 1257 1258 1259
			mark_inode_dirty(new_dir);
		}
	}

1260
	if (ofi) {
L
Linus Torvalds 已提交
1261
		if (ofibh.sbh != ofibh.ebh)
J
Jan Kara 已提交
1262 1263
			brelse(ofibh.ebh);
		brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1264 1265 1266 1267
	}

	retval = 0;

1268
end_rename:
J
Jan Kara 已提交
1269
	brelse(dir_bh);
1270
	if (nfi) {
L
Linus Torvalds 已提交
1271
		if (nfibh.sbh != nfibh.ebh)
J
Jan Kara 已提交
1272 1273
			brelse(nfibh.ebh);
		brelse(nfibh.sbh);
L
Linus Torvalds 已提交
1274 1275
	}
	unlock_kernel();
1276

L
Linus Torvalds 已提交
1277 1278 1279
	return retval;
}

R
Rasmus Rohde 已提交
1280 1281
static struct dentry *udf_get_parent(struct dentry *child)
{
1282
	struct kernel_lb_addr tloc;
R
Rasmus Rohde 已提交
1283
	struct inode *inode = NULL;
1284
	struct qstr dotdot = {.name = "..", .len = 2};
R
Rasmus Rohde 已提交
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
	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);

1296 1297
	tloc = lelb_to_cpu(cfi.icb.extLocation);
	inode = udf_iget(child->d_inode->i_sb, &tloc);
R
Rasmus Rohde 已提交
1298 1299 1300 1301
	if (!inode)
		goto out_unlock;
	unlock_kernel();

1302
	return d_obtain_alias(inode);
R
Rasmus Rohde 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
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;
1313
	struct kernel_lb_addr loc;
R
Rasmus Rohde 已提交
1314 1315 1316 1317 1318 1319

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

	loc.logicalBlockNum = block;
	loc.partitionReferenceNum = partref;
1320
	inode = udf_iget(sb, &loc);
R
Rasmus Rohde 已提交
1321 1322 1323 1324 1325 1326 1327 1328

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

	if (generation && inode->i_generation != generation) {
		iput(inode);
		return ERR_PTR(-ESTALE);
	}
1329
	return d_obtain_alias(inode);
R
Rasmus Rohde 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
}

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;
1359
	struct kernel_lb_addr location = UDF_I(inode)->i_location;
R
Rasmus Rohde 已提交
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
	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,
};

1393
const struct inode_operations udf_dir_inode_operations = {
1394 1395 1396 1397 1398 1399 1400 1401 1402
	.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 已提交
1403
};