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

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

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

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

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

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

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

	offset += liu;

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

	offset += lfi;

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

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

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

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

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

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

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
/**
 * udf_find_entry - find entry in given directory.
 *
 * @dir:	directory inode to search in
 * @child:	qstr of the name
 * @fibh:	buffer head / inode with file identifier descriptor we found
 * @cfi:	found file identifier descriptor with given name
 *
 * This function searches in the directory @dir for a file name @child. When
 * found, @fibh points to the buffer head(s) (bh is NULL for in ICB
 * directories) containing the file identifier descriptor (FID). In that case
 * the function returns pointer to the FID in the buffer or inode - but note
 * that FID may be split among two buffers (blocks) so accessing it via that
 * pointer isn't easily possible. This pointer can be used only as an iterator
 * for other directory manipulation functions. For inspection of the FID @cfi
 * can be used - the found FID is copied there.
 *
 * Returns pointer to FID, NULL when nothing found, or error code.
 */
160
static struct fileIdentDesc *udf_find_entry(struct inode *dir,
A
Al Viro 已提交
161
					    const struct qstr *child,
162 163
					    struct udf_fileident_bh *fibh,
					    struct fileIdentDesc *cfi)
L
Linus Torvalds 已提交
164
{
165
	struct fileIdentDesc *fi = NULL;
L
Linus Torvalds 已提交
166
	loff_t f_pos;
167 168
	udf_pblk_t block;
	int flen;
169
	unsigned char *fname = NULL, *copy_name = NULL;
A
Al Viro 已提交
170
	unsigned char *nameptr;
L
Linus Torvalds 已提交
171 172
	uint8_t lfi;
	uint16_t liu;
173
	loff_t size;
174
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
175
	uint32_t elen;
176
	sector_t offset;
177
	struct extent_position epos = {};
178
	struct udf_inode_info *dinfo = UDF_I(dir);
179 180
	int isdotdot = child->len == 2 &&
		child->name[0] == '.' && child->name[1] == '.';
J
Jan Kara 已提交
181
	struct super_block *sb = dir->i_sb;
L
Linus Torvalds 已提交
182

183 184
	size = udf_ext0_offset(dir) + dir->i_size;
	f_pos = udf_ext0_offset(dir);
L
Linus Torvalds 已提交
185

186
	fibh->sbh = fibh->ebh = NULL;
J
Jan Kara 已提交
187
	fibh->soffset = fibh->eoffset = f_pos & (sb->s_blocksize - 1);
188
	if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
J
Jan Kara 已提交
189
		if (inode_bmap(dir, f_pos >> sb->s_blocksize_bits, &epos,
190 191
		    &eloc, &elen, &offset) != (EXT_RECORDED_ALLOCATED >> 30)) {
			fi = ERR_PTR(-EIO);
192
			goto out_err;
193 194
		}

J
Jan Kara 已提交
195 196
		block = udf_get_lb_pblock(sb, &eloc, offset);
		if ((++offset << sb->s_blocksize_bits) < elen) {
197
			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
198
				epos.offset -= sizeof(struct short_ad);
199
			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
200
				epos.offset -= sizeof(struct long_ad);
M
Marcin Slusarz 已提交
201
		} else
L
Linus Torvalds 已提交
202 203
			offset = 0;

J
Jan Kara 已提交
204
		fibh->sbh = fibh->ebh = udf_tread(sb, block);
205 206
		if (!fibh->sbh) {
			fi = ERR_PTR(-EIO);
207
			goto out_err;
208
		}
L
Linus Torvalds 已提交
209 210
	}

211
	fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
212 213
	if (!fname) {
		fi = ERR_PTR(-ENOMEM);
214
		goto out_err;
215
	}
216

217
	while (f_pos < size) {
218 219
		fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
					&elen, &offset);
220 221
		if (!fi) {
			fi = ERR_PTR(-EIO);
222
			goto out_err;
223
		}
L
Linus Torvalds 已提交
224 225 226 227

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

228
		if (fibh->sbh == fibh->ebh) {
L
Linus Torvalds 已提交
229
			nameptr = fi->fileIdent + liu;
230
		} else {
L
Linus Torvalds 已提交
231 232
			int poffset;	/* Unpaded ending offset */

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

M
Marcin Slusarz 已提交
236 237 238 239
			if (poffset >= lfi)
				nameptr = (uint8_t *)(fibh->ebh->b_data +
						      poffset - lfi);
			else {
240 241 242 243 244 245 246 247 248
				if (!copy_name) {
					copy_name = kmalloc(UDF_NAME_LEN,
							    GFP_NOFS);
					if (!copy_name) {
						fi = ERR_PTR(-ENOMEM);
						goto out_err;
					}
				}
				nameptr = copy_name;
M
Marcin Slusarz 已提交
249 250 251 252
				memcpy(nameptr, fi->fileIdent + liu,
					lfi - poffset);
				memcpy(nameptr + lfi - poffset,
					fibh->ebh->b_data, poffset);
L
Linus Torvalds 已提交
253 254 255
			}
		}

256
		if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
J
Jan Kara 已提交
257
			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
L
Linus Torvalds 已提交
258 259
				continue;
		}
260 261

		if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
J
Jan Kara 已提交
262
			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
L
Linus Torvalds 已提交
263 264 265
				continue;
		}

R
Rasmus Rohde 已提交
266
		if ((cfi->fileCharacteristics & FID_FILE_CHAR_PARENT) &&
267 268
		    isdotdot)
			goto out_ok;
R
Rasmus Rohde 已提交
269

L
Linus Torvalds 已提交
270 271 272
		if (!lfi)
			continue;

J
Jan Kara 已提交
273
		flen = udf_get_filename(sb, nameptr, lfi, fname, UDF_NAME_LEN);
274 275 276 277 278 279
		if (flen < 0) {
			fi = ERR_PTR(flen);
			goto out_err;
		}

		if (udf_match(flen, fname, child->len, child->name))
280
			goto out_ok;
L
Linus Torvalds 已提交
281
	}
282

283
	fi = NULL;
284
out_err:
L
Linus Torvalds 已提交
285
	if (fibh->sbh != fibh->ebh)
J
Jan Kara 已提交
286 287
		brelse(fibh->ebh);
	brelse(fibh->sbh);
288
out_ok:
J
Jan Kara 已提交
289
	brelse(epos.bh);
290
	kfree(fname);
291
	kfree(copy_name);
292

293
	return fi;
L
Linus Torvalds 已提交
294 295
}

296
static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
A
Al Viro 已提交
297
				 unsigned int flags)
L
Linus Torvalds 已提交
298 299
{
	struct inode *inode = NULL;
300
	struct fileIdentDesc cfi;
L
Linus Torvalds 已提交
301
	struct udf_fileident_bh fibh;
302
	struct fileIdentDesc *fi;
L
Linus Torvalds 已提交
303

304
	if (dentry->d_name.len > UDF_NAME_LEN)
L
Linus Torvalds 已提交
305 306 307 308
		return ERR_PTR(-ENAMETOOLONG);

#ifdef UDF_RECOVERY
	/* temporary shorthand for specifying files by inode number */
309
	if (!strncmp(dentry->d_name.name, ".B=", 3)) {
310
		struct kernel_lb_addr lb = {
311
			.logicalBlockNum = 0,
M
Marcin Slusarz 已提交
312 313 314
			.partitionReferenceNum =
				simple_strtoul(dentry->d_name.name + 3,
						NULL, 0),
315
		};
L
Linus Torvalds 已提交
316
		inode = udf_iget(dir->i_sb, lb);
317 318
		if (IS_ERR(inode))
			return inode;
M
Marcin Slusarz 已提交
319
	} else
320
#endif /* UDF_RECOVERY */
L
Linus Torvalds 已提交
321

322 323 324 325 326
	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
	if (IS_ERR(fi))
		return ERR_CAST(fi);

	if (fi) {
327 328
		struct kernel_lb_addr loc;

L
Linus Torvalds 已提交
329
		if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
330 331
			brelse(fibh.ebh);
		brelse(fibh.sbh);
L
Linus Torvalds 已提交
332

333 334
		loc = lelb_to_cpu(cfi.icb.extLocation);
		inode = udf_iget(dir->i_sb, &loc);
335 336
		if (IS_ERR(inode))
			return ERR_CAST(inode);
L
Linus Torvalds 已提交
337
	}
338

R
Rasmus Rohde 已提交
339
	return d_splice_alias(inode, dentry);
L
Linus Torvalds 已提交
340 341
}

342 343 344 345
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 已提交
346
{
M
Marcin Slusarz 已提交
347
	struct super_block *sb = dir->i_sb;
348
	struct fileIdentDesc *fi = NULL;
A
Al Viro 已提交
349
	unsigned char *name = NULL;
L
Linus Torvalds 已提交
350 351
	int namelen;
	loff_t f_pos;
352
	loff_t size = udf_ext0_offset(dir) + dir->i_size;
L
Linus Torvalds 已提交
353 354 355
	int nfidlen;
	uint8_t lfi;
	uint16_t liu;
356
	udf_pblk_t block;
357
	struct kernel_lb_addr eloc;
358
	uint32_t elen = 0;
359
	sector_t offset;
360
	struct extent_position epos = {};
361
	struct udf_inode_info *dinfo;
L
Linus Torvalds 已提交
362

363
	fibh->sbh = fibh->ebh = NULL;
364
	name = kmalloc(UDF_NAME_LEN_CS0, GFP_NOFS);
365 366 367 368 369
	if (!name) {
		*err = -ENOMEM;
		goto out_err;
	}

370 371
	if (dentry) {
		if (!dentry->d_name.len) {
L
Linus Torvalds 已提交
372
			*err = -EINVAL;
373
			goto out_err;
L
Linus Torvalds 已提交
374
		}
375 376
		namelen = udf_put_filename(sb, dentry->d_name.name,
					   dentry->d_name.len,
377
					   name, UDF_NAME_LEN_CS0);
M
Marcin Slusarz 已提交
378
		if (!namelen) {
L
Linus Torvalds 已提交
379
			*err = -ENAMETOOLONG;
380
			goto out_err;
L
Linus Torvalds 已提交
381
		}
382
	} else {
L
Linus Torvalds 已提交
383
		namelen = 0;
384
	}
L
Linus Torvalds 已提交
385 386 387

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

388
	f_pos = udf_ext0_offset(dir);
L
Linus Torvalds 已提交
389

390
	fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
391
	dinfo = UDF_I(dir);
392 393 394 395
	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,
396
					&dinfo->i_location, 0);
397 398 399
			fibh->soffset = fibh->eoffset = sb->s_blocksize;
			goto add;
		}
400
		block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
401
		if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
402
			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
403
				epos.offset -= sizeof(struct short_ad);
404
			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
405
				epos.offset -= sizeof(struct long_ad);
M
Marcin Slusarz 已提交
406
		} else
L
Linus Torvalds 已提交
407 408
			offset = 0;

M
Marcin Slusarz 已提交
409 410
		fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
		if (!fibh->sbh) {
L
Linus Torvalds 已提交
411
			*err = -EIO;
412
			goto out_err;
L
Linus Torvalds 已提交
413 414
		}

415
		block = dinfo->i_location.logicalBlockNum;
L
Linus Torvalds 已提交
416 417
	}

418
	while (f_pos < size) {
419 420
		fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
					&elen, &offset);
L
Linus Torvalds 已提交
421

422
		if (!fi) {
L
Linus Torvalds 已提交
423
			*err = -EIO;
424
			goto out_err;
L
Linus Torvalds 已提交
425 426 427 428 429
		}

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

430
		if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
M
Marcin Slusarz 已提交
431 432
			if (((sizeof(struct fileIdentDesc) +
					liu + lfi + 3) & ~3) == nfidlen) {
L
Linus Torvalds 已提交
433 434 435 436 437
				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 已提交
438 439
				if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
						  name))
440
					goto out_ok;
M
Marcin Slusarz 已提交
441
				else {
L
Linus Torvalds 已提交
442
					*err = -EIO;
443
					goto out_err;
L
Linus Torvalds 已提交
444 445 446 447 448
				}
			}
		}
	}

449
add:
L
Linus Torvalds 已提交
450 451
	f_pos += nfidlen;

452
	if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
453
	    sb->s_blocksize - fibh->eoffset < nfidlen) {
J
Jan Kara 已提交
454
		brelse(epos.bh);
J
Jan Kara 已提交
455
		epos.bh = NULL;
L
Linus Torvalds 已提交
456 457
		fibh->soffset -= udf_ext0_offset(dir);
		fibh->eoffset -= udf_ext0_offset(dir);
458
		f_pos -= udf_ext0_offset(dir);
L
Linus Torvalds 已提交
459
		if (fibh->sbh != fibh->ebh)
J
Jan Kara 已提交
460 461
			brelse(fibh->ebh);
		brelse(fibh->sbh);
M
Marcin Slusarz 已提交
462 463 464
		fibh->sbh = fibh->ebh =
				udf_expand_dir_adinicb(dir, &block, err);
		if (!fibh->sbh)
465
			goto out_err;
466
		epos.block = dinfo->i_location;
J
Jan Kara 已提交
467
		epos.offset = udf_file_entry_alloc_offset(dir);
J
Jan Kara 已提交
468 469
		/* Load extent udf_expand_dir_adinicb() has created */
		udf_current_aext(dir, &epos, &eloc, &elen, 1);
L
Linus Torvalds 已提交
470 471
	}

J
Jan Kara 已提交
472
	/* Entry fits into current block? */
473
	if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
L
Linus Torvalds 已提交
474 475
		fibh->soffset = fibh->eoffset;
		fibh->eoffset += nfidlen;
476
		if (fibh->sbh != fibh->ebh) {
J
Jan Kara 已提交
477
			brelse(fibh->sbh);
L
Linus Torvalds 已提交
478 479 480
			fibh->sbh = fibh->ebh;
		}

481 482
		if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
			block = dinfo->i_location.logicalBlockNum;
M
Marcin Slusarz 已提交
483
			fi = (struct fileIdentDesc *)
484
					(dinfo->i_ext.i_data +
485
					 fibh->soffset -
M
Marcin Slusarz 已提交
486
					 udf_ext0_offset(dir) +
487
					 dinfo->i_lenEAttr);
488
		} else {
M
Marcin Slusarz 已提交
489 490 491 492 493
			block = eloc.logicalBlockNum +
					((elen - 1) >>
						dir->i_sb->s_blocksize_bits);
			fi = (struct fileIdentDesc *)
				(fibh->sbh->b_data + fibh->soffset);
L
Linus Torvalds 已提交
494
		}
495
	} else {
J
Jan Kara 已提交
496 497 498 499 500 501 502 503 504 505
		/* 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 已提交
506 507
		fibh->soffset = fibh->eoffset - sb->s_blocksize;
		fibh->eoffset += nfidlen - sb->s_blocksize;
508
		if (fibh->sbh != fibh->ebh) {
J
Jan Kara 已提交
509
			brelse(fibh->sbh);
L
Linus Torvalds 已提交
510 511 512 513
			fibh->sbh = fibh->ebh;
		}

		block = eloc.logicalBlockNum + ((elen - 1) >>
514
						dir->i_sb->s_blocksize_bits);
M
Marcin Slusarz 已提交
515
		fibh->ebh = udf_bread(dir,
516
				f_pos >> dir->i_sb->s_blocksize_bits, 1, err);
517 518
		if (!fibh->ebh)
			goto out_err;
519 520 521 522 523
		/* Extents could have been merged, invalidate our position */
		brelse(epos.bh);
		epos.bh = NULL;
		epos.block = dinfo->i_location;
		epos.offset = udf_file_entry_alloc_offset(dir);
L
Linus Torvalds 已提交
524

525
		if (!fibh->soffset) {
526 527 528 529 530
			/* Find the freshly allocated block */
			while (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
				(EXT_RECORDED_ALLOCATED >> 30))
				;
			block = eloc.logicalBlockNum + ((elen - 1) >>
531
					dir->i_sb->s_blocksize_bits);
J
Jan Kara 已提交
532
			brelse(fibh->sbh);
L
Linus Torvalds 已提交
533 534
			fibh->sbh = fibh->ebh;
			fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
535
		} else {
L
Linus Torvalds 已提交
536
			fi = (struct fileIdentDesc *)
M
Marcin Slusarz 已提交
537 538
				(fibh->sbh->b_data + sb->s_blocksize +
					fibh->soffset);
L
Linus Torvalds 已提交
539 540 541 542
		}
	}

	memset(cfi, 0, sizeof(struct fileIdentDesc));
M
Marcin Slusarz 已提交
543
	if (UDF_SB(sb)->s_udfrev >= 0x0200)
M
Marcin Slusarz 已提交
544
		udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
545
			    sizeof(struct tag));
L
Linus Torvalds 已提交
546
	else
M
Marcin Slusarz 已提交
547
		udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
548
			    sizeof(struct tag));
L
Linus Torvalds 已提交
549 550 551
	cfi->fileVersionNum = cpu_to_le16(1);
	cfi->lengthFileIdent = namelen;
	cfi->lengthOfImpUse = cpu_to_le16(0);
552
	if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
L
Linus Torvalds 已提交
553
		dir->i_size += nfidlen;
554 555
		if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
			dinfo->i_lenAlloc += nfidlen;
J
Jan Kara 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569
		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 已提交
570
		mark_inode_dirty(dir);
571
		goto out_ok;
572
	} else {
L
Linus Torvalds 已提交
573
		*err = -EIO;
574
		goto out_err;
L
Linus Torvalds 已提交
575
	}
576 577 578 579 580 581 582 583 584 585

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 已提交
586 587 588
}

static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
589 590
			    struct udf_fileident_bh *fibh,
			    struct fileIdentDesc *cfi)
L
Linus Torvalds 已提交
591 592
{
	cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
593

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

L
Linus Torvalds 已提交
597 598 599
	return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
}

600
static int udf_add_nondir(struct dentry *dentry, struct inode *inode)
L
Linus Torvalds 已提交
601
{
602
	struct udf_inode_info *iinfo = UDF_I(inode);
603
	struct inode *dir = d_inode(dentry->d_parent);
L
Linus Torvalds 已提交
604 605 606 607
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;

M
Marcin Slusarz 已提交
608
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
609
	if (unlikely(!fi)) {
610
		inode_dec_link_count(inode);
611
		unlock_new_inode(inode);
L
Linus Torvalds 已提交
612 613 614 615
		iput(inode);
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
616
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
617
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
618
		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
619
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
620
	dir->i_ctime = dir->i_mtime = current_time(dir);
621
	mark_inode_dirty(dir);
L
Linus Torvalds 已提交
622
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
623 624
		brelse(fibh.ebh);
	brelse(fibh.sbh);
625
	unlock_new_inode(inode);
L
Linus Torvalds 已提交
626
	d_instantiate(dentry, inode);
627

L
Linus Torvalds 已提交
628 629 630
	return 0;
}

631 632 633
static int udf_create(struct inode *dir, struct dentry *dentry, umode_t mode,
		      bool excl)
{
634
	struct inode *inode = udf_new_inode(dir, mode);
635

636 637
	if (IS_ERR(inode))
		return PTR_ERR(inode);
638 639 640 641 642 643 644 645 646 647 648 649

	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
		inode->i_data.a_ops = &udf_adinicb_aops;
	else
		inode->i_data.a_ops = &udf_aops;
	inode->i_op = &udf_file_inode_operations;
	inode->i_fop = &udf_file_operations;
	mark_inode_dirty(inode);

	return udf_add_nondir(dentry, inode);
}

A
Al Viro 已提交
650 651
static int udf_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
{
652
	struct inode *inode = udf_new_inode(dir, mode);
A
Al Viro 已提交
653

654 655
	if (IS_ERR(inode))
		return PTR_ERR(inode);
A
Al Viro 已提交
656

657
	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
A
Al Viro 已提交
658 659 660 661 662 663 664
		inode->i_data.a_ops = &udf_adinicb_aops;
	else
		inode->i_data.a_ops = &udf_aops;
	inode->i_op = &udf_file_inode_operations;
	inode->i_fop = &udf_file_operations;
	mark_inode_dirty(inode);
	d_tmpfile(dentry, inode);
665
	unlock_new_inode(inode);
A
Al Viro 已提交
666 667 668
	return 0;
}

A
Al Viro 已提交
669
static int udf_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
670
		     dev_t rdev)
L
Linus Torvalds 已提交
671
{
672
	struct inode *inode;
L
Linus Torvalds 已提交
673 674 675 676

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

677 678 679
	inode = udf_new_inode(dir, mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);
680

681 682
	init_special_inode(inode, mode, rdev);
	return udf_add_nondir(dentry, inode);
L
Linus Torvalds 已提交
683 684
}

685
static int udf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
L
Linus Torvalds 已提交
686
{
687
	struct inode *inode;
L
Linus Torvalds 已提交
688 689 690
	struct udf_fileident_bh fibh;
	struct fileIdentDesc cfi, *fi;
	int err;
691 692
	struct udf_inode_info *dinfo = UDF_I(dir);
	struct udf_inode_info *iinfo;
L
Linus Torvalds 已提交
693

694 695 696
	inode = udf_new_inode(dir, S_IFDIR | mode);
	if (IS_ERR(inode))
		return PTR_ERR(inode);
L
Linus Torvalds 已提交
697

698
	iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
699 700
	inode->i_op = &udf_dir_inode_operations;
	inode->i_fop = &udf_dir_operations;
M
Marcin Slusarz 已提交
701 702
	fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
	if (!fi) {
703
		inode_dec_link_count(inode);
704
		unlock_new_inode(inode);
L
Linus Torvalds 已提交
705 706 707
		iput(inode);
		goto out;
	}
M
Miklos Szeredi 已提交
708
	set_nlink(inode, 2);
L
Linus Torvalds 已提交
709
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
710
	cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
711
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
712
		cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
M
Marcin Slusarz 已提交
713 714
	cfi.fileCharacteristics =
			FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
L
Linus Torvalds 已提交
715
	udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
J
Jan Kara 已提交
716
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
717 718
	mark_inode_dirty(inode);

M
Marcin Slusarz 已提交
719 720
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi) {
721
		clear_nlink(inode);
L
Linus Torvalds 已提交
722
		mark_inode_dirty(inode);
723
		unlock_new_inode(inode);
L
Linus Torvalds 已提交
724 725 726 727
		iput(inode);
		goto out;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
728
	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
729
	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
730
		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
L
Linus Torvalds 已提交
731 732
	cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
733
	inc_nlink(dir);
734
	dir->i_ctime = dir->i_mtime = current_time(dir);
L
Linus Torvalds 已提交
735
	mark_inode_dirty(dir);
736
	unlock_new_inode(inode);
L
Linus Torvalds 已提交
737 738
	d_instantiate(dentry, inode);
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
739 740
		brelse(fibh.ebh);
	brelse(fibh.sbh);
L
Linus Torvalds 已提交
741
	err = 0;
742 743

out:
L
Linus Torvalds 已提交
744 745 746 747 748 749 750 751
	return err;
}

static int empty_dir(struct inode *dir)
{
	struct fileIdentDesc *fi, cfi;
	struct udf_fileident_bh fibh;
	loff_t f_pos;
752
	loff_t size = udf_ext0_offset(dir) + dir->i_size;
753
	udf_pblk_t block;
754
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
755
	uint32_t elen;
756
	sector_t offset;
757
	struct extent_position epos = {};
758
	struct udf_inode_info *dinfo = UDF_I(dir);
L
Linus Torvalds 已提交
759

760 761
	f_pos = udf_ext0_offset(dir);
	fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
L
Linus Torvalds 已提交
762

763
	if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
764
		fibh.sbh = fibh.ebh = NULL;
765
	else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
M
Marcin Slusarz 已提交
766 767
			      &epos, &eloc, &elen, &offset) ==
					(EXT_RECORDED_ALLOCATED >> 30)) {
768
		block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
769
		if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
770
			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
771
				epos.offset -= sizeof(struct short_ad);
772
			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
773
				epos.offset -= sizeof(struct long_ad);
M
Marcin Slusarz 已提交
774
		} else
L
Linus Torvalds 已提交
775 776
			offset = 0;

M
Marcin Slusarz 已提交
777 778
		fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
		if (!fibh.sbh) {
J
Jan Kara 已提交
779
			brelse(epos.bh);
L
Linus Torvalds 已提交
780 781
			return 0;
		}
782
	} else {
J
Jan Kara 已提交
783
		brelse(epos.bh);
L
Linus Torvalds 已提交
784 785 786
		return 0;
	}

787
	while (f_pos < size) {
788 789 790
		fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
					&elen, &offset);
		if (!fi) {
L
Linus Torvalds 已提交
791
			if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
792 793 794
				brelse(fibh.ebh);
			brelse(fibh.sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
795 796 797
			return 0;
		}

798 799
		if (cfi.lengthFileIdent &&
		    (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
L
Linus Torvalds 已提交
800
			if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
801 802 803
				brelse(fibh.ebh);
			brelse(fibh.sbh);
			brelse(epos.bh);
L
Linus Torvalds 已提交
804 805 806
			return 0;
		}
	}
807

L
Linus Torvalds 已提交
808
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
809 810 811
		brelse(fibh.ebh);
	brelse(fibh.sbh);
	brelse(epos.bh);
812

L
Linus Torvalds 已提交
813 814 815
	return 1;
}

816
static int udf_rmdir(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
817 818
{
	int retval;
819
	struct inode *inode = d_inode(dentry);
L
Linus Torvalds 已提交
820 821
	struct udf_fileident_bh fibh;
	struct fileIdentDesc *fi, cfi;
822
	struct kernel_lb_addr tloc;
L
Linus Torvalds 已提交
823 824

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

	retval = -EIO;
	tloc = lelb_to_cpu(cfi.icb.extLocation);
834
	if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
L
Linus Torvalds 已提交
835 836 837 838 839 840 841 842
		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)
843
		udf_warn(inode->i_sb, "empty directory has nlink != 2 (%u)\n",
J
Joe Perches 已提交
844
			 inode->i_nlink);
845
	clear_nlink(inode);
L
Linus Torvalds 已提交
846
	inode->i_size = 0;
847
	inode_dec_link_count(dir);
M
Marcin Slusarz 已提交
848
	inode->i_ctime = dir->i_ctime = dir->i_mtime =
849
						current_time(inode);
L
Linus Torvalds 已提交
850 851
	mark_inode_dirty(dir);

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

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

861
static int udf_unlink(struct inode *dir, struct dentry *dentry)
L
Linus Torvalds 已提交
862 863
{
	int retval;
864
	struct inode *inode = d_inode(dentry);
L
Linus Torvalds 已提交
865 866 867
	struct udf_fileident_bh fibh;
	struct fileIdentDesc *fi;
	struct fileIdentDesc cfi;
868
	struct kernel_lb_addr tloc;
L
Linus Torvalds 已提交
869 870

	retval = -ENOENT;
871
	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
872 873 874 875

	if (IS_ERR_OR_NULL(fi)) {
		if (fi)
			retval = PTR_ERR(fi);
L
Linus Torvalds 已提交
876
		goto out;
877
	}
L
Linus Torvalds 已提交
878 879 880

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

884
	if (!inode->i_nlink) {
885
		udf_debug("Deleting nonexistent file (%lu), %u\n",
886
			  inode->i_ino, inode->i_nlink);
M
Miklos Szeredi 已提交
887
		set_nlink(inode, 1);
L
Linus Torvalds 已提交
888 889 890 891
	}
	retval = udf_delete_entry(dir, fi, &fibh, &cfi);
	if (retval)
		goto end_unlink;
892
	dir->i_ctime = dir->i_mtime = current_time(dir);
L
Linus Torvalds 已提交
893
	mark_inode_dirty(dir);
894
	inode_dec_link_count(inode);
L
Linus Torvalds 已提交
895 896 897
	inode->i_ctime = dir->i_ctime;
	retval = 0;

898
end_unlink:
L
Linus Torvalds 已提交
899
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
900 901
		brelse(fibh.ebh);
	brelse(fibh.sbh);
902 903

out:
L
Linus Torvalds 已提交
904 905 906
	return retval;
}

907 908
static int udf_symlink(struct inode *dir, struct dentry *dentry,
		       const char *symname)
L
Linus Torvalds 已提交
909
{
910
	struct inode *inode = udf_new_inode(dir, S_IFLNK | 0777);
L
Linus Torvalds 已提交
911
	struct pathComponent *pc;
A
Al Viro 已提交
912
	const char *compstart;
913
	struct extent_position epos = {};
L
Linus Torvalds 已提交
914
	int eoffset, elen = 0;
A
Al Viro 已提交
915
	uint8_t *ea;
L
Linus Torvalds 已提交
916
	int err;
917
	udf_pblk_t block;
A
Al Viro 已提交
918
	unsigned char *name = NULL;
L
Linus Torvalds 已提交
919
	int namelen;
920
	struct udf_inode_info *iinfo;
921
	struct super_block *sb = dir->i_sb;
L
Linus Torvalds 已提交
922

923 924
	if (IS_ERR(inode))
		return PTR_ERR(inode);
L
Linus Torvalds 已提交
925

926 927
	iinfo = UDF_I(inode);
	down_write(&iinfo->i_data_sem);
928
	name = kmalloc(UDF_NAME_LEN_CS0, GFP_NOFS);
929 930 931 932 933
	if (!name) {
		err = -ENOMEM;
		goto out_no_entry;
	}

L
Linus Torvalds 已提交
934
	inode->i_data.a_ops = &udf_symlink_aops;
935
	inode->i_op = &udf_symlink_inode_operations;
936
	inode_nohighmem(inode);
L
Linus Torvalds 已提交
937

938
	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
939
		struct kernel_lb_addr eloc;
940
		uint32_t bsize;
L
Linus Torvalds 已提交
941

942
		block = udf_new_block(sb, inode,
943 944
				iinfo->i_location.partitionReferenceNum,
				iinfo->i_location.logicalBlockNum, &err);
L
Linus Torvalds 已提交
945 946
		if (!block)
			goto out_no_entry;
947
		epos.block = iinfo->i_location;
J
Jan Kara 已提交
948 949
		epos.offset = udf_file_entry_alloc_offset(inode);
		epos.bh = NULL;
L
Linus Torvalds 已提交
950
		eloc.logicalBlockNum = block;
M
Marcin Slusarz 已提交
951
		eloc.partitionReferenceNum =
952
				iinfo->i_location.partitionReferenceNum;
953
		bsize = sb->s_blocksize;
954
		iinfo->i_lenExtents = bsize;
955
		udf_add_aext(inode, &epos, &eloc, bsize, 0);
J
Jan Kara 已提交
956
		brelse(epos.bh);
L
Linus Torvalds 已提交
957

958
		block = udf_get_pblock(sb, block,
959
				iinfo->i_location.partitionReferenceNum,
M
Marcin Slusarz 已提交
960
				0);
961
		epos.bh = udf_tgetblk(sb, block);
J
Jan Kara 已提交
962
		lock_buffer(epos.bh);
963
		memset(epos.bh->b_data, 0x00, bsize);
J
Jan Kara 已提交
964 965 966 967
		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);
968 969
	} else
		ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
L
Linus Torvalds 已提交
970

971
	eoffset = sb->s_blocksize - udf_ext0_offset(inode);
L
Linus Torvalds 已提交
972 973
	pc = (struct pathComponent *)ea;

974 975
	if (*symname == '/') {
		do {
L
Linus Torvalds 已提交
976 977 978 979 980 981 982 983 984 985 986
			symname++;
		} while (*symname == '/');

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

	err = -ENAMETOOLONG;

987
	while (*symname) {
L
Linus Torvalds 已提交
988 989 990 991 992
		if (elen + sizeof(struct pathComponent) > eoffset)
			goto out_no_entry;

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

A
Al Viro 已提交
993
		compstart = symname;
L
Linus Torvalds 已提交
994

995
		do {
L
Linus Torvalds 已提交
996 997 998 999 1000 1001
			symname++;
		} while (*symname && *symname != '/');

		pc->componentType = 5;
		pc->lengthComponentIdent = 0;
		pc->componentFileVersionNum = 0;
1002 1003
		if (compstart[0] == '.') {
			if ((symname - compstart) == 1)
L
Linus Torvalds 已提交
1004
				pc->componentType = 4;
M
Marcin Slusarz 已提交
1005 1006
			else if ((symname - compstart) == 2 &&
					compstart[1] == '.')
L
Linus Torvalds 已提交
1007 1008 1009
				pc->componentType = 3;
		}

1010
		if (pc->componentType == 5) {
1011 1012
			namelen = udf_put_filename(sb, compstart,
						   symname - compstart,
1013
						   name, UDF_NAME_LEN_CS0);
1014
			if (!namelen)
L
Linus Torvalds 已提交
1015 1016
				goto out_no_entry;

M
Marcin Slusarz 已提交
1017 1018
			if (elen + sizeof(struct pathComponent) + namelen >
					eoffset)
L
Linus Torvalds 已提交
1019 1020 1021 1022 1023 1024 1025 1026 1027
				goto out_no_entry;
			else
				pc->lengthComponentIdent = namelen;

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

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

1028 1029
		if (*symname) {
			do {
L
Linus Torvalds 已提交
1030 1031 1032 1033 1034
				symname++;
			} while (*symname == '/');
		}
	}

J
Jan Kara 已提交
1035
	brelse(epos.bh);
L
Linus Torvalds 已提交
1036
	inode->i_size = elen;
1037 1038
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
		iinfo->i_lenAlloc = inode->i_size;
J
Jan Kara 已提交
1039 1040
	else
		udf_truncate_tail_extent(inode);
L
Linus Torvalds 已提交
1041
	mark_inode_dirty(inode);
1042
	up_write(&iinfo->i_data_sem);
L
Linus Torvalds 已提交
1043

1044
	err = udf_add_nondir(dentry, inode);
1045
out:
1046
	kfree(name);
L
Linus Torvalds 已提交
1047 1048
	return err;

1049
out_no_entry:
1050
	up_write(&iinfo->i_data_sem);
1051
	inode_dec_link_count(inode);
1052
	unlock_new_inode(inode);
L
Linus Torvalds 已提交
1053 1054 1055 1056
	iput(inode);
	goto out;
}

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

M
Marcin Slusarz 已提交
1065 1066
	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
	if (!fi) {
L
Linus Torvalds 已提交
1067 1068 1069
		return err;
	}
	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1070
	cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1071
	if (UDF_SB(inode->i_sb)->s_lvid_bh) {
1072
		*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1073
			cpu_to_le32(lvid_get_unique_id(inode->i_sb));
L
Linus Torvalds 已提交
1074 1075
	}
	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1076
	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1077
		mark_inode_dirty(dir);
1078

L
Linus Torvalds 已提交
1079
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
1080 1081
		brelse(fibh.ebh);
	brelse(fibh.sbh);
1082
	inc_nlink(inode);
1083
	inode->i_ctime = current_time(inode);
L
Linus Torvalds 已提交
1084
	mark_inode_dirty(inode);
1085
	dir->i_ctime = dir->i_mtime = current_time(dir);
1086
	mark_inode_dirty(dir);
A
Al Viro 已提交
1087
	ihold(inode);
L
Linus Torvalds 已提交
1088
	d_instantiate(dentry, inode);
1089

L
Linus Torvalds 已提交
1090 1091 1092 1093 1094 1095
	return 0;
}

/* Anybody can rename anything with this: the permission checks are left to the
 * higher-level routines.
 */
1096
static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1097 1098
		      struct inode *new_dir, struct dentry *new_dentry,
		      unsigned int flags)
L
Linus Torvalds 已提交
1099
{
1100 1101
	struct inode *old_inode = d_inode(old_dentry);
	struct inode *new_inode = d_inode(new_dentry);
L
Linus Torvalds 已提交
1102
	struct udf_fileident_bh ofibh, nfibh;
M
Marcin Slusarz 已提交
1103 1104
	struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
	struct fileIdentDesc ocfi, ncfi;
L
Linus Torvalds 已提交
1105 1106
	struct buffer_head *dir_bh = NULL;
	int retval = -ENOENT;
1107
	struct kernel_lb_addr tloc;
1108
	struct udf_inode_info *old_iinfo = UDF_I(old_inode);
L
Linus Torvalds 已提交
1109

1110 1111 1112
	if (flags & ~RENAME_NOREPLACE)
		return -EINVAL;

1113
	ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
1114 1115 1116
	if (IS_ERR(ofi)) {
		retval = PTR_ERR(ofi);
		goto end_rename;
L
Linus Torvalds 已提交
1117
	}
1118 1119 1120 1121 1122

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

	brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1123
	tloc = lelb_to_cpu(ocfi.icb.extLocation);
1124
	if (!ofi || udf_get_lb_pblock(old_dir->i_sb, &tloc, 0)
1125
	    != old_inode->i_ino)
L
Linus Torvalds 已提交
1126 1127
		goto end_rename;

1128
	nfi = udf_find_entry(new_dir, &new_dentry->d_name, &nfibh, &ncfi);
1129 1130 1131 1132 1133 1134 1135 1136 1137
	if (IS_ERR(nfi)) {
		retval = PTR_ERR(nfi);
		goto end_rename;
	}
	if (nfi && !new_inode) {
		if (nfibh.sbh != nfibh.ebh)
			brelse(nfibh.ebh);
		brelse(nfibh.sbh);
		nfi = NULL;
L
Linus Torvalds 已提交
1138
	}
1139
	if (S_ISDIR(old_inode->i_mode)) {
M
Marcin Slusarz 已提交
1140
		int offset = udf_ext0_offset(old_inode);
L
Linus Torvalds 已提交
1141

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

	/*
	 * Like most other Unix systems, set the ctime for inodes on a
	 * rename.
	 */
1180
	old_inode->i_ctime = current_time(old_inode);
L
Linus Torvalds 已提交
1181 1182 1183 1184 1185 1186 1187
	mark_inode_dirty(old_inode);

	/*
	 * ok, that's it
	 */
	ncfi.fileVersionNum = ocfi.fileVersionNum;
	ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1188
	memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(ocfi.icb));
L
Linus Torvalds 已提交
1189 1190 1191
	udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);

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

1195
	if (new_inode) {
1196
		new_inode->i_ctime = current_time(new_inode);
1197
		inode_dec_link_count(new_inode);
L
Linus Torvalds 已提交
1198
	}
1199 1200
	old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
	new_dir->i_ctime = new_dir->i_mtime = current_time(new_dir);
L
Linus Torvalds 已提交
1201
	mark_inode_dirty(old_dir);
1202
	mark_inode_dirty(new_dir);
L
Linus Torvalds 已提交
1203

1204
	if (dir_fi) {
1205
		dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
M
Marcin Slusarz 已提交
1206 1207 1208
		udf_update_tag((char *)dir_fi,
				(sizeof(struct fileIdentDesc) +
				le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1209
		if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
L
Linus Torvalds 已提交
1210
			mark_inode_dirty(old_inode);
M
Marcin Slusarz 已提交
1211
		else
L
Linus Torvalds 已提交
1212
			mark_buffer_dirty_inode(dir_bh, old_inode);
M
Marcin Slusarz 已提交
1213

1214
		inode_dec_link_count(old_dir);
M
Marcin Slusarz 已提交
1215
		if (new_inode)
1216
			inode_dec_link_count(new_inode);
M
Marcin Slusarz 已提交
1217
		else {
1218
			inc_nlink(new_dir);
L
Linus Torvalds 已提交
1219 1220 1221 1222
			mark_inode_dirty(new_dir);
		}
	}

1223
	if (ofi) {
L
Linus Torvalds 已提交
1224
		if (ofibh.sbh != ofibh.ebh)
J
Jan Kara 已提交
1225 1226
			brelse(ofibh.ebh);
		brelse(ofibh.sbh);
L
Linus Torvalds 已提交
1227 1228 1229 1230
	}

	retval = 0;

1231
end_rename:
J
Jan Kara 已提交
1232
	brelse(dir_bh);
1233
	if (nfi) {
L
Linus Torvalds 已提交
1234
		if (nfibh.sbh != nfibh.ebh)
J
Jan Kara 已提交
1235 1236
			brelse(nfibh.ebh);
		brelse(nfibh.sbh);
L
Linus Torvalds 已提交
1237
	}
1238

L
Linus Torvalds 已提交
1239 1240 1241
	return retval;
}

R
Rasmus Rohde 已提交
1242 1243
static struct dentry *udf_get_parent(struct dentry *child)
{
1244
	struct kernel_lb_addr tloc;
R
Rasmus Rohde 已提交
1245
	struct inode *inode = NULL;
1246
	struct qstr dotdot = QSTR_INIT("..", 2);
R
Rasmus Rohde 已提交
1247 1248 1249
	struct fileIdentDesc cfi;
	struct udf_fileident_bh fibh;

1250
	if (!udf_find_entry(d_inode(child), &dotdot, &fibh, &cfi))
1251
		return ERR_PTR(-EACCES);
R
Rasmus Rohde 已提交
1252 1253 1254 1255 1256

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

1257
	tloc = lelb_to_cpu(cfi.icb.extLocation);
1258
	inode = udf_iget(child->d_sb, &tloc);
1259 1260
	if (IS_ERR(inode))
		return ERR_CAST(inode);
R
Rasmus Rohde 已提交
1261

1262
	return d_obtain_alias(inode);
R
Rasmus Rohde 已提交
1263 1264 1265 1266 1267 1268 1269
}


static struct dentry *udf_nfs_get_inode(struct super_block *sb, u32 block,
					u16 partref, __u32 generation)
{
	struct inode *inode;
1270
	struct kernel_lb_addr loc;
R
Rasmus Rohde 已提交
1271 1272 1273 1274 1275 1276

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

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

1279 1280
	if (IS_ERR(inode))
		return ERR_CAST(inode);
R
Rasmus Rohde 已提交
1281 1282 1283 1284 1285

	if (generation && inode->i_generation != generation) {
		iput(inode);
		return ERR_PTR(-ESTALE);
	}
1286
	return d_obtain_alias(inode);
R
Rasmus Rohde 已提交
1287 1288 1289 1290 1291
}

static struct dentry *udf_fh_to_dentry(struct super_block *sb,
				       struct fid *fid, int fh_len, int fh_type)
{
N
NeilBrown 已提交
1292
	if (fh_len < 3 ||
R
Rasmus Rohde 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
	    (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)
{
N
NeilBrown 已提交
1304
	if (fh_len < 5 || fh_type != FILEID_UDF_WITH_PARENT)
R
Rasmus Rohde 已提交
1305 1306 1307 1308 1309 1310
		return NULL;

	return udf_nfs_get_inode(sb, fid->udf.parent_block,
				 fid->udf.parent_partref,
				 fid->udf.parent_generation);
}
A
Al Viro 已提交
1311 1312
static int udf_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
			 struct inode *parent)
R
Rasmus Rohde 已提交
1313 1314
{
	int len = *lenp;
1315
	struct kernel_lb_addr location = UDF_I(inode)->i_location;
R
Rasmus Rohde 已提交
1316 1317 1318
	struct fid *fid = (struct fid *)fh;
	int type = FILEID_UDF_WITHOUT_PARENT;

A
Al Viro 已提交
1319
	if (parent && (len < 5)) {
1320
		*lenp = 5;
1321
		return FILEID_INVALID;
1322 1323
	} else if (len < 3) {
		*lenp = 3;
1324
		return FILEID_INVALID;
1325
	}
R
Rasmus Rohde 已提交
1326 1327 1328 1329

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

A
Al Viro 已提交
1333 1334
	if (parent) {
		location = UDF_I(parent)->i_location;
R
Rasmus Rohde 已提交
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
		fid->udf.parent_block = location.logicalBlockNum;
		fid->udf.parent_partref = location.partitionReferenceNum;
		fid->udf.parent_generation = inode->i_generation;
		*lenp = 5;
		type = FILEID_UDF_WITH_PARENT;
	}

	return type;
}

const struct export_operations udf_export_ops = {
	.encode_fh	= udf_encode_fh,
	.fh_to_dentry   = udf_fh_to_dentry,
	.fh_to_parent   = udf_fh_to_parent,
	.get_parent     = udf_get_parent,
};

1352
const struct inode_operations udf_dir_inode_operations = {
1353 1354 1355 1356 1357 1358 1359 1360 1361
	.lookup				= udf_lookup,
	.create				= udf_create,
	.link				= udf_link,
	.unlink				= udf_unlink,
	.symlink			= udf_symlink,
	.mkdir				= udf_mkdir,
	.rmdir				= udf_rmdir,
	.mknod				= udf_mknod,
	.rename				= udf_rename,
A
Al Viro 已提交
1362
	.tmpfile			= udf_tmpfile,
L
Linus Torvalds 已提交
1363
};