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