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