xref: /linux/fs/udf/namei.c (revision 827634added7f38b7d724cab1dccdb2b004c13c3)
1 /*
2  * namei.c
3  *
4  * PURPOSE
5  *      Inode name handling routines for the OSTA-UDF(tm) filesystem.
6  *
7  * COPYRIGHT
8  *      This file is distributed under the terms of the GNU General Public
9  *      License (GPL). Copies of the GPL can be obtained from:
10  *              ftp://prep.ai.mit.edu/pub/gnu/GPL
11  *      Each contributing author retains all rights to their own work.
12  *
13  *  (C) 1998-2004 Ben Fennema
14  *  (C) 1999-2000 Stelias Computing Inc
15  *
16  * HISTORY
17  *
18  *  12/12/98 blf  Created. Split out the lookup code from dir.c
19  *  04/19/99 blf  link, mknod, symlink support
20  */
21 
22 #include "udfdecl.h"
23 
24 #include "udf_i.h"
25 #include "udf_sb.h"
26 #include <linux/string.h>
27 #include <linux/errno.h>
28 #include <linux/mm.h>
29 #include <linux/slab.h>
30 #include <linux/sched.h>
31 #include <linux/crc-itu-t.h>
32 #include <linux/exportfs.h>
33 
34 static inline int udf_match(int len1, const unsigned char *name1, int len2,
35 			    const unsigned char *name2)
36 {
37 	if (len1 != len2)
38 		return 0;
39 
40 	return !memcmp(name1, name2, len1);
41 }
42 
43 int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
44 		 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
45 		 uint8_t *impuse, uint8_t *fileident)
46 {
47 	uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(struct tag);
48 	uint16_t crc;
49 	int offset;
50 	uint16_t liu = le16_to_cpu(cfi->lengthOfImpUse);
51 	uint8_t lfi = cfi->lengthFileIdent;
52 	int padlen = fibh->eoffset - fibh->soffset - liu - lfi -
53 		sizeof(struct fileIdentDesc);
54 	int adinicb = 0;
55 
56 	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
57 		adinicb = 1;
58 
59 	offset = fibh->soffset + sizeof(struct fileIdentDesc);
60 
61 	if (impuse) {
62 		if (adinicb || (offset + liu < 0)) {
63 			memcpy((uint8_t *)sfi->impUse, impuse, liu);
64 		} else if (offset >= 0) {
65 			memcpy(fibh->ebh->b_data + offset, impuse, liu);
66 		} else {
67 			memcpy((uint8_t *)sfi->impUse, impuse, -offset);
68 			memcpy(fibh->ebh->b_data, impuse - offset,
69 				liu + offset);
70 		}
71 	}
72 
73 	offset += liu;
74 
75 	if (fileident) {
76 		if (adinicb || (offset + lfi < 0)) {
77 			memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
78 		} else if (offset >= 0) {
79 			memcpy(fibh->ebh->b_data + offset, fileident, lfi);
80 		} else {
81 			memcpy((uint8_t *)sfi->fileIdent + liu, fileident,
82 				-offset);
83 			memcpy(fibh->ebh->b_data, fileident - offset,
84 				lfi + offset);
85 		}
86 	}
87 
88 	offset += lfi;
89 
90 	if (adinicb || (offset + padlen < 0)) {
91 		memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
92 	} else if (offset >= 0) {
93 		memset(fibh->ebh->b_data + offset, 0x00, padlen);
94 	} else {
95 		memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
96 		memset(fibh->ebh->b_data, 0x00, padlen + offset);
97 	}
98 
99 	crc = crc_itu_t(0, (uint8_t *)cfi + sizeof(struct tag),
100 		      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 +
108 					sizeof(struct fileIdentDesc) +
109 					fibh->soffset,
110 			      crclen + sizeof(struct tag) -
111 					sizeof(struct fileIdentDesc));
112 	} else {
113 		crc = crc_itu_t(crc, (uint8_t *)sfi->impUse,
114 			      -fibh->soffset - sizeof(struct fileIdentDesc));
115 		crc = crc_itu_t(crc, fibh->ebh->b_data, fibh->eoffset);
116 	}
117 
118 	cfi->descTag.descCRC = cpu_to_le16(crc);
119 	cfi->descTag.descCRCLength = cpu_to_le16(crclen);
120 	cfi->descTag.tagChecksum = udf_tag_checksum(&cfi->descTag);
121 
122 	if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset)) {
123 		memcpy((uint8_t *)sfi, (uint8_t *)cfi,
124 			sizeof(struct fileIdentDesc));
125 	} else {
126 		memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset);
127 		memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset,
128 		       sizeof(struct fileIdentDesc) + fibh->soffset);
129 	}
130 
131 	if (adinicb) {
132 		mark_inode_dirty(inode);
133 	} else {
134 		if (fibh->sbh != fibh->ebh)
135 			mark_buffer_dirty_inode(fibh->ebh, inode);
136 		mark_buffer_dirty_inode(fibh->sbh, inode);
137 	}
138 	return 0;
139 }
140 
141 static struct fileIdentDesc *udf_find_entry(struct inode *dir,
142 					    const struct qstr *child,
143 					    struct udf_fileident_bh *fibh,
144 					    struct fileIdentDesc *cfi)
145 {
146 	struct fileIdentDesc *fi = NULL;
147 	loff_t f_pos;
148 	int block, flen;
149 	unsigned char *fname = NULL;
150 	unsigned char *nameptr;
151 	uint8_t lfi;
152 	uint16_t liu;
153 	loff_t size;
154 	struct kernel_lb_addr eloc;
155 	uint32_t elen;
156 	sector_t offset;
157 	struct extent_position epos = {};
158 	struct udf_inode_info *dinfo = UDF_I(dir);
159 	int isdotdot = child->len == 2 &&
160 		child->name[0] == '.' && child->name[1] == '.';
161 	struct super_block *sb = dir->i_sb;
162 
163 	size = udf_ext0_offset(dir) + dir->i_size;
164 	f_pos = udf_ext0_offset(dir);
165 
166 	fibh->sbh = fibh->ebh = NULL;
167 	fibh->soffset = fibh->eoffset = f_pos & (sb->s_blocksize - 1);
168 	if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
169 		if (inode_bmap(dir, f_pos >> sb->s_blocksize_bits, &epos,
170 		    &eloc, &elen, &offset) != (EXT_RECORDED_ALLOCATED >> 30))
171 			goto out_err;
172 		block = udf_get_lb_pblock(sb, &eloc, offset);
173 		if ((++offset << sb->s_blocksize_bits) < elen) {
174 			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
175 				epos.offset -= sizeof(struct short_ad);
176 			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
177 				epos.offset -= sizeof(struct long_ad);
178 		} else
179 			offset = 0;
180 
181 		fibh->sbh = fibh->ebh = udf_tread(sb, block);
182 		if (!fibh->sbh)
183 			goto out_err;
184 	}
185 
186 	fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
187 	if (!fname)
188 		goto out_err;
189 
190 	while (f_pos < size) {
191 		fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
192 					&elen, &offset);
193 		if (!fi)
194 			goto out_err;
195 
196 		liu = le16_to_cpu(cfi->lengthOfImpUse);
197 		lfi = cfi->lengthFileIdent;
198 
199 		if (fibh->sbh == fibh->ebh) {
200 			nameptr = fi->fileIdent + liu;
201 		} else {
202 			int poffset;	/* Unpaded ending offset */
203 
204 			poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
205 					liu + lfi;
206 
207 			if (poffset >= lfi)
208 				nameptr = (uint8_t *)(fibh->ebh->b_data +
209 						      poffset - lfi);
210 			else {
211 				nameptr = fname;
212 				memcpy(nameptr, fi->fileIdent + liu,
213 					lfi - poffset);
214 				memcpy(nameptr + lfi - poffset,
215 					fibh->ebh->b_data, poffset);
216 			}
217 		}
218 
219 		if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
220 			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
221 				continue;
222 		}
223 
224 		if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
225 			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
226 				continue;
227 		}
228 
229 		if ((cfi->fileCharacteristics & FID_FILE_CHAR_PARENT) &&
230 		    isdotdot)
231 			goto out_ok;
232 
233 		if (!lfi)
234 			continue;
235 
236 		flen = udf_get_filename(sb, nameptr, lfi, fname, UDF_NAME_LEN);
237 		if (flen && udf_match(flen, fname, child->len, child->name))
238 			goto out_ok;
239 	}
240 
241 out_err:
242 	fi = NULL;
243 	if (fibh->sbh != fibh->ebh)
244 		brelse(fibh->ebh);
245 	brelse(fibh->sbh);
246 out_ok:
247 	brelse(epos.bh);
248 	kfree(fname);
249 
250 	return fi;
251 }
252 
253 static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
254 				 unsigned int flags)
255 {
256 	struct inode *inode = NULL;
257 	struct fileIdentDesc cfi;
258 	struct udf_fileident_bh fibh;
259 
260 	if (dentry->d_name.len > UDF_NAME_LEN - 2)
261 		return ERR_PTR(-ENAMETOOLONG);
262 
263 #ifdef UDF_RECOVERY
264 	/* temporary shorthand for specifying files by inode number */
265 	if (!strncmp(dentry->d_name.name, ".B=", 3)) {
266 		struct kernel_lb_addr lb = {
267 			.logicalBlockNum = 0,
268 			.partitionReferenceNum =
269 				simple_strtoul(dentry->d_name.name + 3,
270 						NULL, 0),
271 		};
272 		inode = udf_iget(dir->i_sb, lb);
273 		if (IS_ERR(inode))
274 			return inode;
275 	} else
276 #endif /* UDF_RECOVERY */
277 
278 	if (udf_find_entry(dir, &dentry->d_name, &fibh, &cfi)) {
279 		struct kernel_lb_addr loc;
280 
281 		if (fibh.sbh != fibh.ebh)
282 			brelse(fibh.ebh);
283 		brelse(fibh.sbh);
284 
285 		loc = lelb_to_cpu(cfi.icb.extLocation);
286 		inode = udf_iget(dir->i_sb, &loc);
287 		if (IS_ERR(inode))
288 			return ERR_CAST(inode);
289 	}
290 
291 	return d_splice_alias(inode, dentry);
292 }
293 
294 static struct fileIdentDesc *udf_add_entry(struct inode *dir,
295 					   struct dentry *dentry,
296 					   struct udf_fileident_bh *fibh,
297 					   struct fileIdentDesc *cfi, int *err)
298 {
299 	struct super_block *sb = dir->i_sb;
300 	struct fileIdentDesc *fi = NULL;
301 	unsigned char *name = NULL;
302 	int namelen;
303 	loff_t f_pos;
304 	loff_t size = udf_ext0_offset(dir) + dir->i_size;
305 	int nfidlen;
306 	uint8_t lfi;
307 	uint16_t liu;
308 	int block;
309 	struct kernel_lb_addr eloc;
310 	uint32_t elen = 0;
311 	sector_t offset;
312 	struct extent_position epos = {};
313 	struct udf_inode_info *dinfo;
314 
315 	fibh->sbh = fibh->ebh = NULL;
316 	name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
317 	if (!name) {
318 		*err = -ENOMEM;
319 		goto out_err;
320 	}
321 
322 	if (dentry) {
323 		if (!dentry->d_name.len) {
324 			*err = -EINVAL;
325 			goto out_err;
326 		}
327 		namelen = udf_put_filename(sb, dentry->d_name.name, name,
328 						 dentry->d_name.len);
329 		if (!namelen) {
330 			*err = -ENAMETOOLONG;
331 			goto out_err;
332 		}
333 	} else {
334 		namelen = 0;
335 	}
336 
337 	nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
338 
339 	f_pos = udf_ext0_offset(dir);
340 
341 	fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
342 	dinfo = UDF_I(dir);
343 	if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
344 		if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits, &epos,
345 		    &eloc, &elen, &offset) != (EXT_RECORDED_ALLOCATED >> 30)) {
346 			block = udf_get_lb_pblock(dir->i_sb,
347 					&dinfo->i_location, 0);
348 			fibh->soffset = fibh->eoffset = sb->s_blocksize;
349 			goto add;
350 		}
351 		block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
352 		if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
353 			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
354 				epos.offset -= sizeof(struct short_ad);
355 			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
356 				epos.offset -= sizeof(struct long_ad);
357 		} else
358 			offset = 0;
359 
360 		fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
361 		if (!fibh->sbh) {
362 			*err = -EIO;
363 			goto out_err;
364 		}
365 
366 		block = dinfo->i_location.logicalBlockNum;
367 	}
368 
369 	while (f_pos < size) {
370 		fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
371 					&elen, &offset);
372 
373 		if (!fi) {
374 			*err = -EIO;
375 			goto out_err;
376 		}
377 
378 		liu = le16_to_cpu(cfi->lengthOfImpUse);
379 		lfi = cfi->lengthFileIdent;
380 
381 		if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
382 			if (((sizeof(struct fileIdentDesc) +
383 					liu + lfi + 3) & ~3) == nfidlen) {
384 				cfi->descTag.tagSerialNum = cpu_to_le16(1);
385 				cfi->fileVersionNum = cpu_to_le16(1);
386 				cfi->fileCharacteristics = 0;
387 				cfi->lengthFileIdent = namelen;
388 				cfi->lengthOfImpUse = cpu_to_le16(0);
389 				if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
390 						  name))
391 					goto out_ok;
392 				else {
393 					*err = -EIO;
394 					goto out_err;
395 				}
396 			}
397 		}
398 	}
399 
400 add:
401 	f_pos += nfidlen;
402 
403 	if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
404 	    sb->s_blocksize - fibh->eoffset < nfidlen) {
405 		brelse(epos.bh);
406 		epos.bh = NULL;
407 		fibh->soffset -= udf_ext0_offset(dir);
408 		fibh->eoffset -= udf_ext0_offset(dir);
409 		f_pos -= udf_ext0_offset(dir);
410 		if (fibh->sbh != fibh->ebh)
411 			brelse(fibh->ebh);
412 		brelse(fibh->sbh);
413 		fibh->sbh = fibh->ebh =
414 				udf_expand_dir_adinicb(dir, &block, err);
415 		if (!fibh->sbh)
416 			goto out_err;
417 		epos.block = dinfo->i_location;
418 		epos.offset = udf_file_entry_alloc_offset(dir);
419 		/* Load extent udf_expand_dir_adinicb() has created */
420 		udf_current_aext(dir, &epos, &eloc, &elen, 1);
421 	}
422 
423 	/* Entry fits into current block? */
424 	if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
425 		fibh->soffset = fibh->eoffset;
426 		fibh->eoffset += nfidlen;
427 		if (fibh->sbh != fibh->ebh) {
428 			brelse(fibh->sbh);
429 			fibh->sbh = fibh->ebh;
430 		}
431 
432 		if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
433 			block = dinfo->i_location.logicalBlockNum;
434 			fi = (struct fileIdentDesc *)
435 					(dinfo->i_ext.i_data +
436 					 fibh->soffset -
437 					 udf_ext0_offset(dir) +
438 					 dinfo->i_lenEAttr);
439 		} else {
440 			block = eloc.logicalBlockNum +
441 					((elen - 1) >>
442 						dir->i_sb->s_blocksize_bits);
443 			fi = (struct fileIdentDesc *)
444 				(fibh->sbh->b_data + fibh->soffset);
445 		}
446 	} else {
447 		/* Round up last extent in the file */
448 		elen = (elen + sb->s_blocksize - 1) & ~(sb->s_blocksize - 1);
449 		if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
450 			epos.offset -= sizeof(struct short_ad);
451 		else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
452 			epos.offset -= sizeof(struct long_ad);
453 		udf_write_aext(dir, &epos, &eloc, elen, 1);
454 		dinfo->i_lenExtents = (dinfo->i_lenExtents + sb->s_blocksize
455 					- 1) & ~(sb->s_blocksize - 1);
456 
457 		fibh->soffset = fibh->eoffset - sb->s_blocksize;
458 		fibh->eoffset += nfidlen - sb->s_blocksize;
459 		if (fibh->sbh != fibh->ebh) {
460 			brelse(fibh->sbh);
461 			fibh->sbh = fibh->ebh;
462 		}
463 
464 		block = eloc.logicalBlockNum + ((elen - 1) >>
465 						dir->i_sb->s_blocksize_bits);
466 		fibh->ebh = udf_bread(dir,
467 				f_pos >> dir->i_sb->s_blocksize_bits, 1, err);
468 		if (!fibh->ebh)
469 			goto out_err;
470 		/* Extents could have been merged, invalidate our position */
471 		brelse(epos.bh);
472 		epos.bh = NULL;
473 		epos.block = dinfo->i_location;
474 		epos.offset = udf_file_entry_alloc_offset(dir);
475 
476 		if (!fibh->soffset) {
477 			/* Find the freshly allocated block */
478 			while (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
479 				(EXT_RECORDED_ALLOCATED >> 30))
480 				;
481 			block = eloc.logicalBlockNum + ((elen - 1) >>
482 					dir->i_sb->s_blocksize_bits);
483 			brelse(fibh->sbh);
484 			fibh->sbh = fibh->ebh;
485 			fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
486 		} else {
487 			fi = (struct fileIdentDesc *)
488 				(fibh->sbh->b_data + sb->s_blocksize +
489 					fibh->soffset);
490 		}
491 	}
492 
493 	memset(cfi, 0, sizeof(struct fileIdentDesc));
494 	if (UDF_SB(sb)->s_udfrev >= 0x0200)
495 		udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
496 			    sizeof(struct tag));
497 	else
498 		udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
499 			    sizeof(struct tag));
500 	cfi->fileVersionNum = cpu_to_le16(1);
501 	cfi->lengthFileIdent = namelen;
502 	cfi->lengthOfImpUse = cpu_to_le16(0);
503 	if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
504 		dir->i_size += nfidlen;
505 		if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
506 			dinfo->i_lenAlloc += nfidlen;
507 		else {
508 			/* Find the last extent and truncate it to proper size */
509 			while (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
510 				(EXT_RECORDED_ALLOCATED >> 30))
511 				;
512 			elen -= dinfo->i_lenExtents - dir->i_size;
513 			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
514 				epos.offset -= sizeof(struct short_ad);
515 			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
516 				epos.offset -= sizeof(struct long_ad);
517 			udf_write_aext(dir, &epos, &eloc, elen, 1);
518 			dinfo->i_lenExtents = dir->i_size;
519 		}
520 
521 		mark_inode_dirty(dir);
522 		goto out_ok;
523 	} else {
524 		*err = -EIO;
525 		goto out_err;
526 	}
527 
528 out_err:
529 	fi = NULL;
530 	if (fibh->sbh != fibh->ebh)
531 		brelse(fibh->ebh);
532 	brelse(fibh->sbh);
533 out_ok:
534 	brelse(epos.bh);
535 	kfree(name);
536 	return fi;
537 }
538 
539 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
540 			    struct udf_fileident_bh *fibh,
541 			    struct fileIdentDesc *cfi)
542 {
543 	cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
544 
545 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
546 		memset(&(cfi->icb), 0x00, sizeof(struct long_ad));
547 
548 	return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
549 }
550 
551 static int udf_add_nondir(struct dentry *dentry, struct inode *inode)
552 {
553 	struct udf_inode_info *iinfo = UDF_I(inode);
554 	struct inode *dir = d_inode(dentry->d_parent);
555 	struct udf_fileident_bh fibh;
556 	struct fileIdentDesc cfi, *fi;
557 	int err;
558 
559 	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
560 	if (unlikely(!fi)) {
561 		inode_dec_link_count(inode);
562 		unlock_new_inode(inode);
563 		iput(inode);
564 		return err;
565 	}
566 	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
567 	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
568 	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
569 		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
570 	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
571 	dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
572 	mark_inode_dirty(dir);
573 	if (fibh.sbh != fibh.ebh)
574 		brelse(fibh.ebh);
575 	brelse(fibh.sbh);
576 	unlock_new_inode(inode);
577 	d_instantiate(dentry, inode);
578 
579 	return 0;
580 }
581 
582 static int udf_create(struct inode *dir, struct dentry *dentry, umode_t mode,
583 		      bool excl)
584 {
585 	struct inode *inode = udf_new_inode(dir, mode);
586 
587 	if (IS_ERR(inode))
588 		return PTR_ERR(inode);
589 
590 	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
591 		inode->i_data.a_ops = &udf_adinicb_aops;
592 	else
593 		inode->i_data.a_ops = &udf_aops;
594 	inode->i_op = &udf_file_inode_operations;
595 	inode->i_fop = &udf_file_operations;
596 	mark_inode_dirty(inode);
597 
598 	return udf_add_nondir(dentry, inode);
599 }
600 
601 static int udf_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
602 {
603 	struct inode *inode = udf_new_inode(dir, mode);
604 
605 	if (IS_ERR(inode))
606 		return PTR_ERR(inode);
607 
608 	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
609 		inode->i_data.a_ops = &udf_adinicb_aops;
610 	else
611 		inode->i_data.a_ops = &udf_aops;
612 	inode->i_op = &udf_file_inode_operations;
613 	inode->i_fop = &udf_file_operations;
614 	mark_inode_dirty(inode);
615 	d_tmpfile(dentry, inode);
616 	unlock_new_inode(inode);
617 	return 0;
618 }
619 
620 static int udf_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
621 		     dev_t rdev)
622 {
623 	struct inode *inode;
624 
625 	if (!old_valid_dev(rdev))
626 		return -EINVAL;
627 
628 	inode = udf_new_inode(dir, mode);
629 	if (IS_ERR(inode))
630 		return PTR_ERR(inode);
631 
632 	init_special_inode(inode, mode, rdev);
633 	return udf_add_nondir(dentry, inode);
634 }
635 
636 static int udf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
637 {
638 	struct inode *inode;
639 	struct udf_fileident_bh fibh;
640 	struct fileIdentDesc cfi, *fi;
641 	int err;
642 	struct udf_inode_info *dinfo = UDF_I(dir);
643 	struct udf_inode_info *iinfo;
644 
645 	inode = udf_new_inode(dir, S_IFDIR | mode);
646 	if (IS_ERR(inode))
647 		return PTR_ERR(inode);
648 
649 	iinfo = UDF_I(inode);
650 	inode->i_op = &udf_dir_inode_operations;
651 	inode->i_fop = &udf_dir_operations;
652 	fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
653 	if (!fi) {
654 		inode_dec_link_count(inode);
655 		unlock_new_inode(inode);
656 		iput(inode);
657 		goto out;
658 	}
659 	set_nlink(inode, 2);
660 	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
661 	cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
662 	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
663 		cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
664 	cfi.fileCharacteristics =
665 			FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
666 	udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
667 	brelse(fibh.sbh);
668 	mark_inode_dirty(inode);
669 
670 	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
671 	if (!fi) {
672 		clear_nlink(inode);
673 		mark_inode_dirty(inode);
674 		unlock_new_inode(inode);
675 		iput(inode);
676 		goto out;
677 	}
678 	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
679 	cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
680 	*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
681 		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
682 	cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
683 	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
684 	inc_nlink(dir);
685 	dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
686 	mark_inode_dirty(dir);
687 	unlock_new_inode(inode);
688 	d_instantiate(dentry, inode);
689 	if (fibh.sbh != fibh.ebh)
690 		brelse(fibh.ebh);
691 	brelse(fibh.sbh);
692 	err = 0;
693 
694 out:
695 	return err;
696 }
697 
698 static int empty_dir(struct inode *dir)
699 {
700 	struct fileIdentDesc *fi, cfi;
701 	struct udf_fileident_bh fibh;
702 	loff_t f_pos;
703 	loff_t size = udf_ext0_offset(dir) + dir->i_size;
704 	int block;
705 	struct kernel_lb_addr eloc;
706 	uint32_t elen;
707 	sector_t offset;
708 	struct extent_position epos = {};
709 	struct udf_inode_info *dinfo = UDF_I(dir);
710 
711 	f_pos = udf_ext0_offset(dir);
712 	fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
713 
714 	if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
715 		fibh.sbh = fibh.ebh = NULL;
716 	else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
717 			      &epos, &eloc, &elen, &offset) ==
718 					(EXT_RECORDED_ALLOCATED >> 30)) {
719 		block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
720 		if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
721 			if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
722 				epos.offset -= sizeof(struct short_ad);
723 			else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
724 				epos.offset -= sizeof(struct long_ad);
725 		} else
726 			offset = 0;
727 
728 		fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
729 		if (!fibh.sbh) {
730 			brelse(epos.bh);
731 			return 0;
732 		}
733 	} else {
734 		brelse(epos.bh);
735 		return 0;
736 	}
737 
738 	while (f_pos < size) {
739 		fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
740 					&elen, &offset);
741 		if (!fi) {
742 			if (fibh.sbh != fibh.ebh)
743 				brelse(fibh.ebh);
744 			brelse(fibh.sbh);
745 			brelse(epos.bh);
746 			return 0;
747 		}
748 
749 		if (cfi.lengthFileIdent &&
750 		    (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
751 			if (fibh.sbh != fibh.ebh)
752 				brelse(fibh.ebh);
753 			brelse(fibh.sbh);
754 			brelse(epos.bh);
755 			return 0;
756 		}
757 	}
758 
759 	if (fibh.sbh != fibh.ebh)
760 		brelse(fibh.ebh);
761 	brelse(fibh.sbh);
762 	brelse(epos.bh);
763 
764 	return 1;
765 }
766 
767 static int udf_rmdir(struct inode *dir, struct dentry *dentry)
768 {
769 	int retval;
770 	struct inode *inode = d_inode(dentry);
771 	struct udf_fileident_bh fibh;
772 	struct fileIdentDesc *fi, cfi;
773 	struct kernel_lb_addr tloc;
774 
775 	retval = -ENOENT;
776 	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
777 	if (!fi)
778 		goto out;
779 
780 	retval = -EIO;
781 	tloc = lelb_to_cpu(cfi.icb.extLocation);
782 	if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
783 		goto end_rmdir;
784 	retval = -ENOTEMPTY;
785 	if (!empty_dir(inode))
786 		goto end_rmdir;
787 	retval = udf_delete_entry(dir, fi, &fibh, &cfi);
788 	if (retval)
789 		goto end_rmdir;
790 	if (inode->i_nlink != 2)
791 		udf_warn(inode->i_sb, "empty directory has nlink != 2 (%d)\n",
792 			 inode->i_nlink);
793 	clear_nlink(inode);
794 	inode->i_size = 0;
795 	inode_dec_link_count(dir);
796 	inode->i_ctime = dir->i_ctime = dir->i_mtime =
797 						current_fs_time(dir->i_sb);
798 	mark_inode_dirty(dir);
799 
800 end_rmdir:
801 	if (fibh.sbh != fibh.ebh)
802 		brelse(fibh.ebh);
803 	brelse(fibh.sbh);
804 
805 out:
806 	return retval;
807 }
808 
809 static int udf_unlink(struct inode *dir, struct dentry *dentry)
810 {
811 	int retval;
812 	struct inode *inode = d_inode(dentry);
813 	struct udf_fileident_bh fibh;
814 	struct fileIdentDesc *fi;
815 	struct fileIdentDesc cfi;
816 	struct kernel_lb_addr tloc;
817 
818 	retval = -ENOENT;
819 	fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
820 	if (!fi)
821 		goto out;
822 
823 	retval = -EIO;
824 	tloc = lelb_to_cpu(cfi.icb.extLocation);
825 	if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
826 		goto end_unlink;
827 
828 	if (!inode->i_nlink) {
829 		udf_debug("Deleting nonexistent file (%lu), %d\n",
830 			  inode->i_ino, inode->i_nlink);
831 		set_nlink(inode, 1);
832 	}
833 	retval = udf_delete_entry(dir, fi, &fibh, &cfi);
834 	if (retval)
835 		goto end_unlink;
836 	dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
837 	mark_inode_dirty(dir);
838 	inode_dec_link_count(inode);
839 	inode->i_ctime = dir->i_ctime;
840 	retval = 0;
841 
842 end_unlink:
843 	if (fibh.sbh != fibh.ebh)
844 		brelse(fibh.ebh);
845 	brelse(fibh.sbh);
846 
847 out:
848 	return retval;
849 }
850 
851 static int udf_symlink(struct inode *dir, struct dentry *dentry,
852 		       const char *symname)
853 {
854 	struct inode *inode = udf_new_inode(dir, S_IFLNK | S_IRWXUGO);
855 	struct pathComponent *pc;
856 	const char *compstart;
857 	struct extent_position epos = {};
858 	int eoffset, elen = 0;
859 	uint8_t *ea;
860 	int err;
861 	int block;
862 	unsigned char *name = NULL;
863 	int namelen;
864 	struct udf_inode_info *iinfo;
865 	struct super_block *sb = dir->i_sb;
866 
867 	if (IS_ERR(inode))
868 		return PTR_ERR(inode);
869 
870 	iinfo = UDF_I(inode);
871 	down_write(&iinfo->i_data_sem);
872 	name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
873 	if (!name) {
874 		err = -ENOMEM;
875 		goto out_no_entry;
876 	}
877 
878 	inode->i_data.a_ops = &udf_symlink_aops;
879 	inode->i_op = &udf_symlink_inode_operations;
880 
881 	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
882 		struct kernel_lb_addr eloc;
883 		uint32_t bsize;
884 
885 		block = udf_new_block(sb, inode,
886 				iinfo->i_location.partitionReferenceNum,
887 				iinfo->i_location.logicalBlockNum, &err);
888 		if (!block)
889 			goto out_no_entry;
890 		epos.block = iinfo->i_location;
891 		epos.offset = udf_file_entry_alloc_offset(inode);
892 		epos.bh = NULL;
893 		eloc.logicalBlockNum = block;
894 		eloc.partitionReferenceNum =
895 				iinfo->i_location.partitionReferenceNum;
896 		bsize = sb->s_blocksize;
897 		iinfo->i_lenExtents = bsize;
898 		udf_add_aext(inode, &epos, &eloc, bsize, 0);
899 		brelse(epos.bh);
900 
901 		block = udf_get_pblock(sb, block,
902 				iinfo->i_location.partitionReferenceNum,
903 				0);
904 		epos.bh = udf_tgetblk(sb, block);
905 		lock_buffer(epos.bh);
906 		memset(epos.bh->b_data, 0x00, bsize);
907 		set_buffer_uptodate(epos.bh);
908 		unlock_buffer(epos.bh);
909 		mark_buffer_dirty_inode(epos.bh, inode);
910 		ea = epos.bh->b_data + udf_ext0_offset(inode);
911 	} else
912 		ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
913 
914 	eoffset = sb->s_blocksize - udf_ext0_offset(inode);
915 	pc = (struct pathComponent *)ea;
916 
917 	if (*symname == '/') {
918 		do {
919 			symname++;
920 		} while (*symname == '/');
921 
922 		pc->componentType = 1;
923 		pc->lengthComponentIdent = 0;
924 		pc->componentFileVersionNum = 0;
925 		elen += sizeof(struct pathComponent);
926 	}
927 
928 	err = -ENAMETOOLONG;
929 
930 	while (*symname) {
931 		if (elen + sizeof(struct pathComponent) > eoffset)
932 			goto out_no_entry;
933 
934 		pc = (struct pathComponent *)(ea + elen);
935 
936 		compstart = symname;
937 
938 		do {
939 			symname++;
940 		} while (*symname && *symname != '/');
941 
942 		pc->componentType = 5;
943 		pc->lengthComponentIdent = 0;
944 		pc->componentFileVersionNum = 0;
945 		if (compstart[0] == '.') {
946 			if ((symname - compstart) == 1)
947 				pc->componentType = 4;
948 			else if ((symname - compstart) == 2 &&
949 					compstart[1] == '.')
950 				pc->componentType = 3;
951 		}
952 
953 		if (pc->componentType == 5) {
954 			namelen = udf_put_filename(sb, compstart, name,
955 						   symname - compstart);
956 			if (!namelen)
957 				goto out_no_entry;
958 
959 			if (elen + sizeof(struct pathComponent) + namelen >
960 					eoffset)
961 				goto out_no_entry;
962 			else
963 				pc->lengthComponentIdent = namelen;
964 
965 			memcpy(pc->componentIdent, name, namelen);
966 		}
967 
968 		elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
969 
970 		if (*symname) {
971 			do {
972 				symname++;
973 			} while (*symname == '/');
974 		}
975 	}
976 
977 	brelse(epos.bh);
978 	inode->i_size = elen;
979 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
980 		iinfo->i_lenAlloc = inode->i_size;
981 	else
982 		udf_truncate_tail_extent(inode);
983 	mark_inode_dirty(inode);
984 	up_write(&iinfo->i_data_sem);
985 
986 	err = udf_add_nondir(dentry, inode);
987 out:
988 	kfree(name);
989 	return err;
990 
991 out_no_entry:
992 	up_write(&iinfo->i_data_sem);
993 	inode_dec_link_count(inode);
994 	unlock_new_inode(inode);
995 	iput(inode);
996 	goto out;
997 }
998 
999 static int udf_link(struct dentry *old_dentry, struct inode *dir,
1000 		    struct dentry *dentry)
1001 {
1002 	struct inode *inode = d_inode(old_dentry);
1003 	struct udf_fileident_bh fibh;
1004 	struct fileIdentDesc cfi, *fi;
1005 	int err;
1006 
1007 	fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1008 	if (!fi) {
1009 		return err;
1010 	}
1011 	cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1012 	cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1013 	if (UDF_SB(inode->i_sb)->s_lvid_bh) {
1014 		*(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1015 			cpu_to_le32(lvid_get_unique_id(inode->i_sb));
1016 	}
1017 	udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1018 	if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1019 		mark_inode_dirty(dir);
1020 
1021 	if (fibh.sbh != fibh.ebh)
1022 		brelse(fibh.ebh);
1023 	brelse(fibh.sbh);
1024 	inc_nlink(inode);
1025 	inode->i_ctime = current_fs_time(inode->i_sb);
1026 	mark_inode_dirty(inode);
1027 	dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
1028 	mark_inode_dirty(dir);
1029 	ihold(inode);
1030 	d_instantiate(dentry, inode);
1031 
1032 	return 0;
1033 }
1034 
1035 /* Anybody can rename anything with this: the permission checks are left to the
1036  * higher-level routines.
1037  */
1038 static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1039 		      struct inode *new_dir, struct dentry *new_dentry)
1040 {
1041 	struct inode *old_inode = d_inode(old_dentry);
1042 	struct inode *new_inode = d_inode(new_dentry);
1043 	struct udf_fileident_bh ofibh, nfibh;
1044 	struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
1045 	struct fileIdentDesc ocfi, ncfi;
1046 	struct buffer_head *dir_bh = NULL;
1047 	int retval = -ENOENT;
1048 	struct kernel_lb_addr tloc;
1049 	struct udf_inode_info *old_iinfo = UDF_I(old_inode);
1050 
1051 	ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
1052 	if (ofi) {
1053 		if (ofibh.sbh != ofibh.ebh)
1054 			brelse(ofibh.ebh);
1055 		brelse(ofibh.sbh);
1056 	}
1057 	tloc = lelb_to_cpu(ocfi.icb.extLocation);
1058 	if (!ofi || udf_get_lb_pblock(old_dir->i_sb, &tloc, 0)
1059 	    != old_inode->i_ino)
1060 		goto end_rename;
1061 
1062 	nfi = udf_find_entry(new_dir, &new_dentry->d_name, &nfibh, &ncfi);
1063 	if (nfi) {
1064 		if (!new_inode) {
1065 			if (nfibh.sbh != nfibh.ebh)
1066 				brelse(nfibh.ebh);
1067 			brelse(nfibh.sbh);
1068 			nfi = NULL;
1069 		}
1070 	}
1071 	if (S_ISDIR(old_inode->i_mode)) {
1072 		int offset = udf_ext0_offset(old_inode);
1073 
1074 		if (new_inode) {
1075 			retval = -ENOTEMPTY;
1076 			if (!empty_dir(new_inode))
1077 				goto end_rename;
1078 		}
1079 		retval = -EIO;
1080 		if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1081 			dir_fi = udf_get_fileident(
1082 					old_iinfo->i_ext.i_data -
1083 					  (old_iinfo->i_efe ?
1084 					   sizeof(struct extendedFileEntry) :
1085 					   sizeof(struct fileEntry)),
1086 					old_inode->i_sb->s_blocksize, &offset);
1087 		} else {
1088 			dir_bh = udf_bread(old_inode, 0, 0, &retval);
1089 			if (!dir_bh)
1090 				goto end_rename;
1091 			dir_fi = udf_get_fileident(dir_bh->b_data,
1092 					old_inode->i_sb->s_blocksize, &offset);
1093 		}
1094 		if (!dir_fi)
1095 			goto end_rename;
1096 		tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1097 		if (udf_get_lb_pblock(old_inode->i_sb, &tloc, 0) !=
1098 				old_dir->i_ino)
1099 			goto end_rename;
1100 	}
1101 	if (!nfi) {
1102 		nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
1103 				    &retval);
1104 		if (!nfi)
1105 			goto end_rename;
1106 	}
1107 
1108 	/*
1109 	 * Like most other Unix systems, set the ctime for inodes on a
1110 	 * rename.
1111 	 */
1112 	old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1113 	mark_inode_dirty(old_inode);
1114 
1115 	/*
1116 	 * ok, that's it
1117 	 */
1118 	ncfi.fileVersionNum = ocfi.fileVersionNum;
1119 	ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1120 	memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(struct long_ad));
1121 	udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1122 
1123 	/* The old fid may have moved - find it again */
1124 	ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
1125 	udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1126 
1127 	if (new_inode) {
1128 		new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1129 		inode_dec_link_count(new_inode);
1130 	}
1131 	old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1132 	new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
1133 	mark_inode_dirty(old_dir);
1134 	mark_inode_dirty(new_dir);
1135 
1136 	if (dir_fi) {
1137 		dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
1138 		udf_update_tag((char *)dir_fi,
1139 				(sizeof(struct fileIdentDesc) +
1140 				le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1141 		if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1142 			mark_inode_dirty(old_inode);
1143 		else
1144 			mark_buffer_dirty_inode(dir_bh, old_inode);
1145 
1146 		inode_dec_link_count(old_dir);
1147 		if (new_inode)
1148 			inode_dec_link_count(new_inode);
1149 		else {
1150 			inc_nlink(new_dir);
1151 			mark_inode_dirty(new_dir);
1152 		}
1153 	}
1154 
1155 	if (ofi) {
1156 		if (ofibh.sbh != ofibh.ebh)
1157 			brelse(ofibh.ebh);
1158 		brelse(ofibh.sbh);
1159 	}
1160 
1161 	retval = 0;
1162 
1163 end_rename:
1164 	brelse(dir_bh);
1165 	if (nfi) {
1166 		if (nfibh.sbh != nfibh.ebh)
1167 			brelse(nfibh.ebh);
1168 		brelse(nfibh.sbh);
1169 	}
1170 
1171 	return retval;
1172 }
1173 
1174 static struct dentry *udf_get_parent(struct dentry *child)
1175 {
1176 	struct kernel_lb_addr tloc;
1177 	struct inode *inode = NULL;
1178 	struct qstr dotdot = QSTR_INIT("..", 2);
1179 	struct fileIdentDesc cfi;
1180 	struct udf_fileident_bh fibh;
1181 
1182 	if (!udf_find_entry(d_inode(child), &dotdot, &fibh, &cfi))
1183 		return ERR_PTR(-EACCES);
1184 
1185 	if (fibh.sbh != fibh.ebh)
1186 		brelse(fibh.ebh);
1187 	brelse(fibh.sbh);
1188 
1189 	tloc = lelb_to_cpu(cfi.icb.extLocation);
1190 	inode = udf_iget(d_inode(child)->i_sb, &tloc);
1191 	if (IS_ERR(inode))
1192 		return ERR_CAST(inode);
1193 
1194 	return d_obtain_alias(inode);
1195 }
1196 
1197 
1198 static struct dentry *udf_nfs_get_inode(struct super_block *sb, u32 block,
1199 					u16 partref, __u32 generation)
1200 {
1201 	struct inode *inode;
1202 	struct kernel_lb_addr loc;
1203 
1204 	if (block == 0)
1205 		return ERR_PTR(-ESTALE);
1206 
1207 	loc.logicalBlockNum = block;
1208 	loc.partitionReferenceNum = partref;
1209 	inode = udf_iget(sb, &loc);
1210 
1211 	if (IS_ERR(inode))
1212 		return ERR_CAST(inode);
1213 
1214 	if (generation && inode->i_generation != generation) {
1215 		iput(inode);
1216 		return ERR_PTR(-ESTALE);
1217 	}
1218 	return d_obtain_alias(inode);
1219 }
1220 
1221 static struct dentry *udf_fh_to_dentry(struct super_block *sb,
1222 				       struct fid *fid, int fh_len, int fh_type)
1223 {
1224 	if ((fh_len != 3 && fh_len != 5) ||
1225 	    (fh_type != FILEID_UDF_WITH_PARENT &&
1226 	     fh_type != FILEID_UDF_WITHOUT_PARENT))
1227 		return NULL;
1228 
1229 	return udf_nfs_get_inode(sb, fid->udf.block, fid->udf.partref,
1230 			fid->udf.generation);
1231 }
1232 
1233 static struct dentry *udf_fh_to_parent(struct super_block *sb,
1234 				       struct fid *fid, int fh_len, int fh_type)
1235 {
1236 	if (fh_len != 5 || fh_type != FILEID_UDF_WITH_PARENT)
1237 		return NULL;
1238 
1239 	return udf_nfs_get_inode(sb, fid->udf.parent_block,
1240 				 fid->udf.parent_partref,
1241 				 fid->udf.parent_generation);
1242 }
1243 static int udf_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
1244 			 struct inode *parent)
1245 {
1246 	int len = *lenp;
1247 	struct kernel_lb_addr location = UDF_I(inode)->i_location;
1248 	struct fid *fid = (struct fid *)fh;
1249 	int type = FILEID_UDF_WITHOUT_PARENT;
1250 
1251 	if (parent && (len < 5)) {
1252 		*lenp = 5;
1253 		return FILEID_INVALID;
1254 	} else if (len < 3) {
1255 		*lenp = 3;
1256 		return FILEID_INVALID;
1257 	}
1258 
1259 	*lenp = 3;
1260 	fid->udf.block = location.logicalBlockNum;
1261 	fid->udf.partref = location.partitionReferenceNum;
1262 	fid->udf.parent_partref = 0;
1263 	fid->udf.generation = inode->i_generation;
1264 
1265 	if (parent) {
1266 		location = UDF_I(parent)->i_location;
1267 		fid->udf.parent_block = location.logicalBlockNum;
1268 		fid->udf.parent_partref = location.partitionReferenceNum;
1269 		fid->udf.parent_generation = inode->i_generation;
1270 		*lenp = 5;
1271 		type = FILEID_UDF_WITH_PARENT;
1272 	}
1273 
1274 	return type;
1275 }
1276 
1277 const struct export_operations udf_export_ops = {
1278 	.encode_fh	= udf_encode_fh,
1279 	.fh_to_dentry   = udf_fh_to_dentry,
1280 	.fh_to_parent   = udf_fh_to_parent,
1281 	.get_parent     = udf_get_parent,
1282 };
1283 
1284 const struct inode_operations udf_dir_inode_operations = {
1285 	.lookup				= udf_lookup,
1286 	.create				= udf_create,
1287 	.link				= udf_link,
1288 	.unlink				= udf_unlink,
1289 	.symlink			= udf_symlink,
1290 	.mkdir				= udf_mkdir,
1291 	.rmdir				= udf_rmdir,
1292 	.mknod				= udf_mknod,
1293 	.rename				= udf_rename,
1294 	.tmpfile			= udf_tmpfile,
1295 };
1296 const struct inode_operations udf_symlink_inode_operations = {
1297 	.readlink	= generic_readlink,
1298 	.follow_link	= page_follow_link_light,
1299 	.put_link	= page_put_link,
1300 };
1301