xref: /linux/fs/udf/namei.c (revision 119ff04864a24470b1e531bb53e5c141aa8fefb0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * namei.c
4  *
5  * PURPOSE
6  *      Inode name handling routines for the OSTA-UDF(tm) filesystem.
7  *
8  * COPYRIGHT
9  *  (C) 1998-2004 Ben Fennema
10  *  (C) 1999-2000 Stelias Computing Inc
11  *
12  * HISTORY
13  *
14  *  12/12/98 blf  Created. Split out the lookup code from dir.c
15  *  04/19/99 blf  link, mknod, symlink support
16  */
17 
18 #include "udfdecl.h"
19 
20 #include "udf_i.h"
21 #include "udf_sb.h"
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/mm.h>
25 #include <linux/slab.h>
26 #include <linux/sched.h>
27 #include <linux/crc-itu-t.h>
28 #include <linux/exportfs.h>
29 #include <linux/iversion.h>
30 
31 static inline int udf_match(int len1, const unsigned char *name1, int len2,
32 			    const unsigned char *name2)
33 {
34 	if (len1 != len2)
35 		return 0;
36 
37 	return !memcmp(name1, name2, len1);
38 }
39 
40 /**
41  * udf_fiiter_find_entry - find entry in given directory.
42  *
43  * @dir:	directory inode to search in
44  * @child:	qstr of the name
45  * @iter:	iter to use for searching
46  *
47  * This function searches in the directory @dir for a file name @child. When
48  * found, @iter points to the position in the directory with given entry.
49  *
50  * Returns 0 on success, < 0 on error (including -ENOENT).
51  */
52 static int udf_fiiter_find_entry(struct inode *dir, const struct qstr *child,
53 				 struct udf_fileident_iter *iter)
54 {
55 	int flen;
56 	unsigned char *fname = NULL;
57 	struct super_block *sb = dir->i_sb;
58 	int isdotdot = child->len == 2 &&
59 		child->name[0] == '.' && child->name[1] == '.';
60 	int ret;
61 
62 	fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
63 	if (!fname)
64 		return -ENOMEM;
65 
66 	for (ret = udf_fiiter_init(iter, dir, 0);
67 	     !ret && iter->pos < dir->i_size;
68 	     ret = udf_fiiter_advance(iter)) {
69 		if (iter->fi.fileCharacteristics & FID_FILE_CHAR_DELETED) {
70 			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
71 				continue;
72 		}
73 
74 		if (iter->fi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) {
75 			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
76 				continue;
77 		}
78 
79 		if ((iter->fi.fileCharacteristics & FID_FILE_CHAR_PARENT) &&
80 		    isdotdot)
81 			goto out_ok;
82 
83 		if (!iter->fi.lengthFileIdent)
84 			continue;
85 
86 		flen = udf_get_filename(sb, iter->name,
87 				iter->fi.lengthFileIdent, fname, UDF_NAME_LEN);
88 		if (flen < 0) {
89 			ret = flen;
90 			goto out_err;
91 		}
92 
93 		if (udf_match(flen, fname, child->len, child->name))
94 			goto out_ok;
95 	}
96 	if (!ret)
97 		ret = -ENOENT;
98 
99 out_err:
100 	udf_fiiter_release(iter);
101 out_ok:
102 	kfree(fname);
103 
104 	return ret;
105 }
106 
107 static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
108 				 unsigned int flags)
109 {
110 	struct inode *inode = NULL;
111 	struct udf_fileident_iter iter;
112 	int err;
113 
114 	if (dentry->d_name.len > UDF_NAME_LEN)
115 		return ERR_PTR(-ENAMETOOLONG);
116 
117 	err = udf_fiiter_find_entry(dir, &dentry->d_name, &iter);
118 	if (err < 0 && err != -ENOENT)
119 		return ERR_PTR(err);
120 
121 	if (err == 0) {
122 		struct kernel_lb_addr loc;
123 
124 		loc = lelb_to_cpu(iter.fi.icb.extLocation);
125 		udf_fiiter_release(&iter);
126 
127 		inode = udf_iget(dir->i_sb, &loc);
128 	}
129 
130 	return d_splice_alias(inode, dentry);
131 }
132 
133 static int udf_expand_dir_adinicb(struct inode *inode, udf_pblk_t *block)
134 {
135 	udf_pblk_t newblock;
136 	struct buffer_head *dbh = NULL;
137 	struct kernel_lb_addr eloc;
138 	struct extent_position epos;
139 	uint8_t alloctype;
140 	struct udf_inode_info *iinfo = UDF_I(inode);
141 	struct udf_fileident_iter iter;
142 	uint8_t *impuse;
143 	int ret;
144 
145 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
146 		alloctype = ICBTAG_FLAG_AD_SHORT;
147 	else
148 		alloctype = ICBTAG_FLAG_AD_LONG;
149 
150 	if (!inode->i_size) {
151 		iinfo->i_alloc_type = alloctype;
152 		mark_inode_dirty(inode);
153 		return 0;
154 	}
155 
156 	/* alloc block, and copy data to it */
157 	*block = udf_new_block(inode->i_sb, inode,
158 			       iinfo->i_location.partitionReferenceNum,
159 			       iinfo->i_location.logicalBlockNum, &ret);
160 	if (!(*block))
161 		return ret;
162 	newblock = udf_get_pblock(inode->i_sb, *block,
163 				  iinfo->i_location.partitionReferenceNum,
164 				0);
165 	if (newblock == 0xffffffff)
166 		return -EFSCORRUPTED;
167 	dbh = sb_getblk(inode->i_sb, newblock);
168 	if (!dbh)
169 		return -ENOMEM;
170 	lock_buffer(dbh);
171 	memcpy(dbh->b_data, iinfo->i_data, inode->i_size);
172 	memset(dbh->b_data + inode->i_size, 0,
173 	       inode->i_sb->s_blocksize - inode->i_size);
174 	set_buffer_uptodate(dbh);
175 	unlock_buffer(dbh);
176 
177 	/* Drop inline data, add block instead */
178 	iinfo->i_alloc_type = alloctype;
179 	memset(iinfo->i_data + iinfo->i_lenEAttr, 0, iinfo->i_lenAlloc);
180 	iinfo->i_lenAlloc = 0;
181 	eloc.logicalBlockNum = *block;
182 	eloc.partitionReferenceNum =
183 				iinfo->i_location.partitionReferenceNum;
184 	iinfo->i_lenExtents = inode->i_size;
185 	epos.bh = NULL;
186 	epos.block = iinfo->i_location;
187 	epos.offset = udf_file_entry_alloc_offset(inode);
188 	ret = udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
189 	brelse(epos.bh);
190 	if (ret < 0) {
191 		brelse(dbh);
192 		udf_free_blocks(inode->i_sb, inode, &eloc, 0, 1);
193 		return ret;
194 	}
195 	mark_inode_dirty(inode);
196 
197 	/* Now fixup tags in moved directory entries */
198 	for (ret = udf_fiiter_init(&iter, inode, 0);
199 	     !ret && iter.pos < inode->i_size;
200 	     ret = udf_fiiter_advance(&iter)) {
201 		iter.fi.descTag.tagLocation = cpu_to_le32(*block);
202 		if (iter.fi.lengthOfImpUse != cpu_to_le16(0))
203 			impuse = dbh->b_data + iter.pos +
204 						sizeof(struct fileIdentDesc);
205 		else
206 			impuse = NULL;
207 		udf_fiiter_write_fi(&iter, impuse);
208 	}
209 	brelse(dbh);
210 	/*
211 	 * We don't expect the iteration to fail as the directory has been
212 	 * already verified to be correct
213 	 */
214 	WARN_ON_ONCE(ret);
215 	udf_fiiter_release(&iter);
216 
217 	return 0;
218 }
219 
220 static int udf_fiiter_add_entry(struct inode *dir, struct dentry *dentry,
221 				struct udf_fileident_iter *iter)
222 {
223 	struct udf_inode_info *dinfo = UDF_I(dir);
224 	int nfidlen, namelen = 0;
225 	int ret;
226 	int off, blksize = 1 << dir->i_blkbits;
227 	udf_pblk_t block;
228 	char name[UDF_NAME_LEN_CS0];
229 
230 	if (dentry) {
231 		namelen = udf_put_filename(dir->i_sb, dentry->d_name.name,
232 					   dentry->d_name.len,
233 					   name, UDF_NAME_LEN_CS0);
234 		if (!namelen)
235 			return -ENAMETOOLONG;
236 	}
237 	nfidlen = ALIGN(sizeof(struct fileIdentDesc) + namelen, UDF_NAME_PAD);
238 
239 	for (ret = udf_fiiter_init(iter, dir, 0);
240 	     !ret && iter->pos < dir->i_size;
241 	     ret = udf_fiiter_advance(iter)) {
242 		if (iter->fi.fileCharacteristics & FID_FILE_CHAR_DELETED) {
243 			if (udf_dir_entry_len(&iter->fi) == nfidlen) {
244 				iter->fi.descTag.tagSerialNum = cpu_to_le16(1);
245 				iter->fi.fileVersionNum = cpu_to_le16(1);
246 				iter->fi.fileCharacteristics = 0;
247 				iter->fi.lengthFileIdent = namelen;
248 				iter->fi.lengthOfImpUse = cpu_to_le16(0);
249 				memcpy(iter->namebuf, name, namelen);
250 				iter->name = iter->namebuf;
251 				return 0;
252 			}
253 		}
254 	}
255 	if (ret) {
256 		udf_fiiter_release(iter);
257 		return ret;
258 	}
259 	if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
260 	    blksize - udf_ext0_offset(dir) - iter->pos < nfidlen) {
261 		udf_fiiter_release(iter);
262 		ret = udf_expand_dir_adinicb(dir, &block);
263 		if (ret)
264 			return ret;
265 		ret = udf_fiiter_init(iter, dir, dir->i_size);
266 		if (ret < 0)
267 			return ret;
268 	}
269 
270 	/* Get blocknumber to use for entry tag */
271 	if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
272 		block = dinfo->i_location.logicalBlockNum;
273 	} else {
274 		block = iter->eloc.logicalBlockNum +
275 				((iter->elen - 1) >> dir->i_blkbits);
276 	}
277 	off = iter->pos & (blksize - 1);
278 	if (!off)
279 		off = blksize;
280 	/* Entry fits into current block? */
281 	if (blksize - udf_ext0_offset(dir) - off >= nfidlen)
282 		goto store_fi;
283 
284 	ret = udf_fiiter_append_blk(iter);
285 	if (ret) {
286 		udf_fiiter_release(iter);
287 		return ret;
288 	}
289 
290 	/* Entry will be completely in the new block? Update tag location... */
291 	if (!(iter->pos & (blksize - 1)))
292 		block = iter->eloc.logicalBlockNum +
293 				((iter->elen - 1) >> dir->i_blkbits);
294 store_fi:
295 	memset(&iter->fi, 0, sizeof(struct fileIdentDesc));
296 	if (UDF_SB(dir->i_sb)->s_udfrev >= 0x0200)
297 		udf_new_tag((char *)(&iter->fi), TAG_IDENT_FID, 3, 1, block,
298 			    sizeof(struct tag));
299 	else
300 		udf_new_tag((char *)(&iter->fi), TAG_IDENT_FID, 2, 1, block,
301 			    sizeof(struct tag));
302 	iter->fi.fileVersionNum = cpu_to_le16(1);
303 	iter->fi.lengthFileIdent = namelen;
304 	iter->fi.lengthOfImpUse = cpu_to_le16(0);
305 	memcpy(iter->namebuf, name, namelen);
306 	iter->name = iter->namebuf;
307 
308 	dir->i_size += nfidlen;
309 	if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
310 		dinfo->i_lenAlloc += nfidlen;
311 	} else {
312 		/* Truncate last extent to proper size */
313 		udf_fiiter_update_elen(iter, iter->elen -
314 					(dinfo->i_lenExtents - dir->i_size));
315 	}
316 	mark_inode_dirty(dir);
317 
318 	return 0;
319 }
320 
321 static void udf_fiiter_delete_entry(struct udf_fileident_iter *iter)
322 {
323 	iter->fi.fileCharacteristics |= FID_FILE_CHAR_DELETED;
324 
325 	if (UDF_QUERY_FLAG(iter->dir->i_sb, UDF_FLAG_STRICT))
326 		memset(&iter->fi.icb, 0x00, sizeof(struct long_ad));
327 
328 	udf_fiiter_write_fi(iter, NULL);
329 }
330 
331 static void udf_add_fid_counter(struct super_block *sb, bool dir, int val)
332 {
333 	struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sb);
334 
335 	if (!lvidiu)
336 		return;
337 	mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
338 	if (dir)
339 		le32_add_cpu(&lvidiu->numDirs, val);
340 	else
341 		le32_add_cpu(&lvidiu->numFiles, val);
342 	udf_updated_lvid(sb);
343 	mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
344 }
345 
346 static int udf_add_nondir(struct dentry *dentry, struct inode *inode)
347 {
348 	struct udf_inode_info *iinfo = UDF_I(inode);
349 	struct inode *dir = d_inode(dentry->d_parent);
350 	struct udf_fileident_iter iter;
351 	int err;
352 
353 	err = udf_fiiter_add_entry(dir, dentry, &iter);
354 	if (err) {
355 		inode_dec_link_count(inode);
356 		discard_new_inode(inode);
357 		return err;
358 	}
359 	iter.fi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
360 	iter.fi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
361 	*(__le32 *)((struct allocDescImpUse *)iter.fi.icb.impUse)->impUse =
362 		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
363 	udf_fiiter_write_fi(&iter, NULL);
364 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
365 	mark_inode_dirty(dir);
366 	udf_fiiter_release(&iter);
367 	udf_add_fid_counter(dir->i_sb, false, 1);
368 	d_instantiate_new(dentry, inode);
369 
370 	return 0;
371 }
372 
373 static int udf_create(struct mnt_idmap *idmap, struct inode *dir,
374 		      struct dentry *dentry, umode_t mode, bool excl)
375 {
376 	struct inode *inode = udf_new_inode(dir, mode);
377 
378 	if (IS_ERR(inode))
379 		return PTR_ERR(inode);
380 
381 	inode->i_data.a_ops = &udf_aops;
382 	inode->i_op = &udf_file_inode_operations;
383 	inode->i_fop = &udf_file_operations;
384 	mark_inode_dirty(inode);
385 
386 	return udf_add_nondir(dentry, inode);
387 }
388 
389 static int udf_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
390 		       struct file *file, umode_t mode)
391 {
392 	struct inode *inode = udf_new_inode(dir, mode);
393 
394 	if (IS_ERR(inode))
395 		return PTR_ERR(inode);
396 
397 	inode->i_data.a_ops = &udf_aops;
398 	inode->i_op = &udf_file_inode_operations;
399 	inode->i_fop = &udf_file_operations;
400 	mark_inode_dirty(inode);
401 	d_tmpfile(file, inode);
402 	unlock_new_inode(inode);
403 	return finish_open_simple(file, 0);
404 }
405 
406 static int udf_mknod(struct mnt_idmap *idmap, struct inode *dir,
407 		     struct dentry *dentry, umode_t mode, dev_t rdev)
408 {
409 	struct inode *inode;
410 
411 	if (!old_valid_dev(rdev))
412 		return -EINVAL;
413 
414 	inode = udf_new_inode(dir, mode);
415 	if (IS_ERR(inode))
416 		return PTR_ERR(inode);
417 
418 	init_special_inode(inode, mode, rdev);
419 	return udf_add_nondir(dentry, inode);
420 }
421 
422 static int udf_mkdir(struct mnt_idmap *idmap, struct inode *dir,
423 		     struct dentry *dentry, umode_t mode)
424 {
425 	struct inode *inode;
426 	struct udf_fileident_iter iter;
427 	int err;
428 	struct udf_inode_info *dinfo = UDF_I(dir);
429 	struct udf_inode_info *iinfo;
430 
431 	inode = udf_new_inode(dir, S_IFDIR | mode);
432 	if (IS_ERR(inode))
433 		return PTR_ERR(inode);
434 
435 	iinfo = UDF_I(inode);
436 	inode->i_op = &udf_dir_inode_operations;
437 	inode->i_fop = &udf_dir_operations;
438 	err = udf_fiiter_add_entry(inode, NULL, &iter);
439 	if (err) {
440 		clear_nlink(inode);
441 		discard_new_inode(inode);
442 		return err;
443 	}
444 	set_nlink(inode, 2);
445 	iter.fi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
446 	iter.fi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
447 	*(__le32 *)((struct allocDescImpUse *)iter.fi.icb.impUse)->impUse =
448 		cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
449 	iter.fi.fileCharacteristics =
450 			FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
451 	udf_fiiter_write_fi(&iter, NULL);
452 	udf_fiiter_release(&iter);
453 	mark_inode_dirty(inode);
454 
455 	err = udf_fiiter_add_entry(dir, dentry, &iter);
456 	if (err) {
457 		clear_nlink(inode);
458 		discard_new_inode(inode);
459 		return err;
460 	}
461 	iter.fi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
462 	iter.fi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
463 	*(__le32 *)((struct allocDescImpUse *)iter.fi.icb.impUse)->impUse =
464 		cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
465 	iter.fi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
466 	udf_fiiter_write_fi(&iter, NULL);
467 	udf_fiiter_release(&iter);
468 	udf_add_fid_counter(dir->i_sb, true, 1);
469 	inc_nlink(dir);
470 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
471 	mark_inode_dirty(dir);
472 	d_instantiate_new(dentry, inode);
473 
474 	return 0;
475 }
476 
477 static int empty_dir(struct inode *dir)
478 {
479 	struct udf_fileident_iter iter;
480 	int ret;
481 
482 	for (ret = udf_fiiter_init(&iter, dir, 0);
483 	     !ret && iter.pos < dir->i_size;
484 	     ret = udf_fiiter_advance(&iter)) {
485 		if (iter.fi.lengthFileIdent &&
486 		    !(iter.fi.fileCharacteristics & FID_FILE_CHAR_DELETED)) {
487 			udf_fiiter_release(&iter);
488 			return 0;
489 		}
490 	}
491 	udf_fiiter_release(&iter);
492 
493 	return 1;
494 }
495 
496 static int udf_rmdir(struct inode *dir, struct dentry *dentry)
497 {
498 	int ret;
499 	struct inode *inode = d_inode(dentry);
500 	struct udf_fileident_iter iter;
501 	struct kernel_lb_addr tloc;
502 
503 	ret = udf_fiiter_find_entry(dir, &dentry->d_name, &iter);
504 	if (ret)
505 		goto out;
506 
507 	ret = -EFSCORRUPTED;
508 	tloc = lelb_to_cpu(iter.fi.icb.extLocation);
509 	if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
510 		goto end_rmdir;
511 	ret = -ENOTEMPTY;
512 	if (!empty_dir(inode))
513 		goto end_rmdir;
514 	udf_fiiter_delete_entry(&iter);
515 	if (inode->i_nlink != 2)
516 		udf_warn(inode->i_sb, "empty directory has nlink != 2 (%u)\n",
517 			 inode->i_nlink);
518 	clear_nlink(inode);
519 	inode->i_size = 0;
520 	inode_dec_link_count(dir);
521 	udf_add_fid_counter(dir->i_sb, true, -1);
522 	inode_set_mtime_to_ts(dir,
523 			      inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
524 	mark_inode_dirty(dir);
525 	ret = 0;
526 end_rmdir:
527 	udf_fiiter_release(&iter);
528 out:
529 	return ret;
530 }
531 
532 static int udf_unlink(struct inode *dir, struct dentry *dentry)
533 {
534 	int ret;
535 	struct inode *inode = d_inode(dentry);
536 	struct udf_fileident_iter iter;
537 	struct kernel_lb_addr tloc;
538 
539 	ret = udf_fiiter_find_entry(dir, &dentry->d_name, &iter);
540 	if (ret)
541 		goto out;
542 
543 	ret = -EFSCORRUPTED;
544 	tloc = lelb_to_cpu(iter.fi.icb.extLocation);
545 	if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
546 		goto end_unlink;
547 
548 	if (!inode->i_nlink) {
549 		udf_debug("Deleting nonexistent file (%lu), %u\n",
550 			  inode->i_ino, inode->i_nlink);
551 		set_nlink(inode, 1);
552 	}
553 	udf_fiiter_delete_entry(&iter);
554 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
555 	mark_inode_dirty(dir);
556 	inode_dec_link_count(inode);
557 	udf_add_fid_counter(dir->i_sb, false, -1);
558 	inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
559 	ret = 0;
560 end_unlink:
561 	udf_fiiter_release(&iter);
562 out:
563 	return ret;
564 }
565 
566 static int udf_symlink(struct mnt_idmap *idmap, struct inode *dir,
567 		       struct dentry *dentry, const char *symname)
568 {
569 	struct inode *inode = udf_new_inode(dir, S_IFLNK | 0777);
570 	struct pathComponent *pc;
571 	const char *compstart;
572 	struct extent_position epos = {};
573 	int eoffset, elen = 0;
574 	uint8_t *ea;
575 	int err;
576 	udf_pblk_t block;
577 	unsigned char *name = NULL;
578 	int namelen;
579 	struct udf_inode_info *iinfo;
580 	struct super_block *sb = dir->i_sb;
581 
582 	if (IS_ERR(inode))
583 		return PTR_ERR(inode);
584 
585 	iinfo = UDF_I(inode);
586 	down_write(&iinfo->i_data_sem);
587 	name = kmalloc(UDF_NAME_LEN_CS0, GFP_NOFS);
588 	if (!name) {
589 		err = -ENOMEM;
590 		goto out_no_entry;
591 	}
592 
593 	inode->i_data.a_ops = &udf_symlink_aops;
594 	inode->i_op = &udf_symlink_inode_operations;
595 	inode_nohighmem(inode);
596 
597 	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
598 		struct kernel_lb_addr eloc;
599 		uint32_t bsize;
600 
601 		block = udf_new_block(sb, inode,
602 				iinfo->i_location.partitionReferenceNum,
603 				iinfo->i_location.logicalBlockNum, &err);
604 		if (!block)
605 			goto out_no_entry;
606 		epos.block = iinfo->i_location;
607 		epos.offset = udf_file_entry_alloc_offset(inode);
608 		epos.bh = NULL;
609 		eloc.logicalBlockNum = block;
610 		eloc.partitionReferenceNum =
611 				iinfo->i_location.partitionReferenceNum;
612 		bsize = sb->s_blocksize;
613 		iinfo->i_lenExtents = bsize;
614 		err = udf_add_aext(inode, &epos, &eloc, bsize, 0);
615 		brelse(epos.bh);
616 		if (err < 0) {
617 			udf_free_blocks(sb, inode, &eloc, 0, 1);
618 			goto out_no_entry;
619 		}
620 
621 		block = udf_get_pblock(sb, block,
622 				iinfo->i_location.partitionReferenceNum,
623 				0);
624 		epos.bh = sb_getblk(sb, block);
625 		if (unlikely(!epos.bh)) {
626 			err = -ENOMEM;
627 			udf_free_blocks(sb, inode, &eloc, 0, 1);
628 			goto out_no_entry;
629 		}
630 		lock_buffer(epos.bh);
631 		memset(epos.bh->b_data, 0x00, bsize);
632 		set_buffer_uptodate(epos.bh);
633 		unlock_buffer(epos.bh);
634 		mark_buffer_dirty_inode(epos.bh, inode);
635 		ea = epos.bh->b_data + udf_ext0_offset(inode);
636 	} else
637 		ea = iinfo->i_data + iinfo->i_lenEAttr;
638 
639 	eoffset = sb->s_blocksize - udf_ext0_offset(inode);
640 	pc = (struct pathComponent *)ea;
641 
642 	if (*symname == '/') {
643 		do {
644 			symname++;
645 		} while (*symname == '/');
646 
647 		pc->componentType = 1;
648 		pc->lengthComponentIdent = 0;
649 		pc->componentFileVersionNum = 0;
650 		elen += sizeof(struct pathComponent);
651 	}
652 
653 	err = -ENAMETOOLONG;
654 
655 	while (*symname) {
656 		if (elen + sizeof(struct pathComponent) > eoffset)
657 			goto out_no_entry;
658 
659 		pc = (struct pathComponent *)(ea + elen);
660 
661 		compstart = symname;
662 
663 		do {
664 			symname++;
665 		} while (*symname && *symname != '/');
666 
667 		pc->componentType = 5;
668 		pc->lengthComponentIdent = 0;
669 		pc->componentFileVersionNum = 0;
670 		if (compstart[0] == '.') {
671 			if ((symname - compstart) == 1)
672 				pc->componentType = 4;
673 			else if ((symname - compstart) == 2 &&
674 					compstart[1] == '.')
675 				pc->componentType = 3;
676 		}
677 
678 		if (pc->componentType == 5) {
679 			namelen = udf_put_filename(sb, compstart,
680 						   symname - compstart,
681 						   name, UDF_NAME_LEN_CS0);
682 			if (!namelen)
683 				goto out_no_entry;
684 
685 			if (elen + sizeof(struct pathComponent) + namelen >
686 					eoffset)
687 				goto out_no_entry;
688 			else
689 				pc->lengthComponentIdent = namelen;
690 
691 			memcpy(pc->componentIdent, name, namelen);
692 		}
693 
694 		elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
695 
696 		if (*symname) {
697 			do {
698 				symname++;
699 			} while (*symname == '/');
700 		}
701 	}
702 
703 	brelse(epos.bh);
704 	inode->i_size = elen;
705 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
706 		iinfo->i_lenAlloc = inode->i_size;
707 	else
708 		udf_truncate_tail_extent(inode);
709 	mark_inode_dirty(inode);
710 	up_write(&iinfo->i_data_sem);
711 
712 	err = udf_add_nondir(dentry, inode);
713 out:
714 	kfree(name);
715 	return err;
716 
717 out_no_entry:
718 	up_write(&iinfo->i_data_sem);
719 	inode_dec_link_count(inode);
720 	discard_new_inode(inode);
721 	goto out;
722 }
723 
724 static int udf_link(struct dentry *old_dentry, struct inode *dir,
725 		    struct dentry *dentry)
726 {
727 	struct inode *inode = d_inode(old_dentry);
728 	struct udf_fileident_iter iter;
729 	int err;
730 
731 	err = udf_fiiter_add_entry(dir, dentry, &iter);
732 	if (err)
733 		return err;
734 	iter.fi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
735 	iter.fi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
736 	if (UDF_SB(inode->i_sb)->s_lvid_bh) {
737 		*(__le32 *)((struct allocDescImpUse *)iter.fi.icb.impUse)->impUse =
738 			cpu_to_le32(lvid_get_unique_id(inode->i_sb));
739 	}
740 	udf_fiiter_write_fi(&iter, NULL);
741 	udf_fiiter_release(&iter);
742 
743 	inc_nlink(inode);
744 	udf_add_fid_counter(dir->i_sb, false, 1);
745 	inode_set_ctime_current(inode);
746 	mark_inode_dirty(inode);
747 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
748 	mark_inode_dirty(dir);
749 	ihold(inode);
750 	d_instantiate(dentry, inode);
751 
752 	return 0;
753 }
754 
755 /* Anybody can rename anything with this: the permission checks are left to the
756  * higher-level routines.
757  */
758 static int udf_rename(struct mnt_idmap *idmap, struct inode *old_dir,
759 		      struct dentry *old_dentry, struct inode *new_dir,
760 		      struct dentry *new_dentry, unsigned int flags)
761 {
762 	struct inode *old_inode = d_inode(old_dentry);
763 	struct inode *new_inode = d_inode(new_dentry);
764 	struct udf_fileident_iter oiter, niter, diriter;
765 	bool has_diriter = false, is_dir = false;
766 	int retval;
767 	struct kernel_lb_addr tloc;
768 
769 	if (flags & ~RENAME_NOREPLACE)
770 		return -EINVAL;
771 
772 	retval = udf_fiiter_find_entry(old_dir, &old_dentry->d_name, &oiter);
773 	if (retval)
774 		return retval;
775 
776 	tloc = lelb_to_cpu(oiter.fi.icb.extLocation);
777 	if (udf_get_lb_pblock(old_dir->i_sb, &tloc, 0) != old_inode->i_ino) {
778 		retval = -ENOENT;
779 		goto out_oiter;
780 	}
781 
782 	if (S_ISDIR(old_inode->i_mode)) {
783 		if (new_inode) {
784 			retval = -ENOTEMPTY;
785 			if (!empty_dir(new_inode))
786 				goto out_oiter;
787 		}
788 		is_dir = true;
789 	}
790 	if (is_dir && old_dir != new_dir) {
791 		retval = udf_fiiter_find_entry(old_inode, &dotdot_name,
792 					       &diriter);
793 		if (retval == -ENOENT) {
794 			udf_err(old_inode->i_sb,
795 				"directory (ino %lu) has no '..' entry\n",
796 				old_inode->i_ino);
797 			retval = -EFSCORRUPTED;
798 		}
799 		if (retval)
800 			goto out_oiter;
801 		has_diriter = true;
802 		tloc = lelb_to_cpu(diriter.fi.icb.extLocation);
803 		if (udf_get_lb_pblock(old_inode->i_sb, &tloc, 0) !=
804 				old_dir->i_ino) {
805 			retval = -EFSCORRUPTED;
806 			udf_err(old_inode->i_sb,
807 				"directory (ino %lu) has parent entry pointing to another inode (%lu != %u)\n",
808 				old_inode->i_ino, old_dir->i_ino,
809 				udf_get_lb_pblock(old_inode->i_sb, &tloc, 0));
810 			goto out_oiter;
811 		}
812 	}
813 
814 	retval = udf_fiiter_find_entry(new_dir, &new_dentry->d_name, &niter);
815 	if (retval && retval != -ENOENT)
816 		goto out_oiter;
817 	/* Entry found but not passed by VFS? */
818 	if (!retval && !new_inode) {
819 		retval = -EFSCORRUPTED;
820 		udf_fiiter_release(&niter);
821 		goto out_oiter;
822 	}
823 	/* Entry not found? Need to add one... */
824 	if (retval) {
825 		udf_fiiter_release(&niter);
826 		retval = udf_fiiter_add_entry(new_dir, new_dentry, &niter);
827 		if (retval)
828 			goto out_oiter;
829 	}
830 
831 	/*
832 	 * Like most other Unix systems, set the ctime for inodes on a
833 	 * rename.
834 	 */
835 	inode_set_ctime_current(old_inode);
836 	mark_inode_dirty(old_inode);
837 
838 	/*
839 	 * ok, that's it
840 	 */
841 	niter.fi.fileVersionNum = oiter.fi.fileVersionNum;
842 	niter.fi.fileCharacteristics = oiter.fi.fileCharacteristics;
843 	memcpy(&(niter.fi.icb), &(oiter.fi.icb), sizeof(oiter.fi.icb));
844 	udf_fiiter_write_fi(&niter, NULL);
845 	udf_fiiter_release(&niter);
846 
847 	/*
848 	 * The old entry may have moved due to new entry allocation. Find it
849 	 * again.
850 	 */
851 	udf_fiiter_release(&oiter);
852 	retval = udf_fiiter_find_entry(old_dir, &old_dentry->d_name, &oiter);
853 	if (retval) {
854 		udf_err(old_dir->i_sb,
855 			"failed to find renamed entry again in directory (ino %lu)\n",
856 			old_dir->i_ino);
857 	} else {
858 		udf_fiiter_delete_entry(&oiter);
859 		udf_fiiter_release(&oiter);
860 	}
861 
862 	if (new_inode) {
863 		inode_set_ctime_current(new_inode);
864 		inode_dec_link_count(new_inode);
865 		udf_add_fid_counter(old_dir->i_sb, S_ISDIR(new_inode->i_mode),
866 				    -1);
867 	}
868 	inode_set_mtime_to_ts(old_dir, inode_set_ctime_current(old_dir));
869 	inode_set_mtime_to_ts(new_dir, inode_set_ctime_current(new_dir));
870 	mark_inode_dirty(old_dir);
871 	mark_inode_dirty(new_dir);
872 
873 	if (has_diriter) {
874 		diriter.fi.icb.extLocation =
875 					cpu_to_lelb(UDF_I(new_dir)->i_location);
876 		udf_update_tag((char *)&diriter.fi,
877 			       udf_dir_entry_len(&diriter.fi));
878 		udf_fiiter_write_fi(&diriter, NULL);
879 		udf_fiiter_release(&diriter);
880 	}
881 
882 	if (is_dir) {
883 		inode_dec_link_count(old_dir);
884 		if (new_inode)
885 			inode_dec_link_count(new_inode);
886 		else {
887 			inc_nlink(new_dir);
888 			mark_inode_dirty(new_dir);
889 		}
890 	}
891 	return 0;
892 out_oiter:
893 	if (has_diriter)
894 		udf_fiiter_release(&diriter);
895 	udf_fiiter_release(&oiter);
896 
897 	return retval;
898 }
899 
900 static struct dentry *udf_get_parent(struct dentry *child)
901 {
902 	struct kernel_lb_addr tloc;
903 	struct udf_fileident_iter iter;
904 	int err;
905 
906 	err = udf_fiiter_find_entry(d_inode(child), &dotdot_name, &iter);
907 	if (err)
908 		return ERR_PTR(err);
909 
910 	tloc = lelb_to_cpu(iter.fi.icb.extLocation);
911 	udf_fiiter_release(&iter);
912 	return d_obtain_alias(udf_iget(child->d_sb, &tloc));
913 }
914 
915 
916 static struct dentry *udf_nfs_get_inode(struct super_block *sb, u32 block,
917 					u16 partref, __u32 generation)
918 {
919 	struct inode *inode;
920 	struct kernel_lb_addr loc;
921 
922 	if (block == 0)
923 		return ERR_PTR(-ESTALE);
924 
925 	loc.logicalBlockNum = block;
926 	loc.partitionReferenceNum = partref;
927 	inode = udf_iget(sb, &loc);
928 
929 	if (IS_ERR(inode))
930 		return ERR_CAST(inode);
931 
932 	if (generation && inode->i_generation != generation) {
933 		iput(inode);
934 		return ERR_PTR(-ESTALE);
935 	}
936 	return d_obtain_alias(inode);
937 }
938 
939 static struct dentry *udf_fh_to_dentry(struct super_block *sb,
940 				       struct fid *fid, int fh_len, int fh_type)
941 {
942 	if (fh_len < 3 ||
943 	    (fh_type != FILEID_UDF_WITH_PARENT &&
944 	     fh_type != FILEID_UDF_WITHOUT_PARENT))
945 		return NULL;
946 
947 	return udf_nfs_get_inode(sb, fid->udf.block, fid->udf.partref,
948 			fid->udf.generation);
949 }
950 
951 static struct dentry *udf_fh_to_parent(struct super_block *sb,
952 				       struct fid *fid, int fh_len, int fh_type)
953 {
954 	if (fh_len < 5 || fh_type != FILEID_UDF_WITH_PARENT)
955 		return NULL;
956 
957 	return udf_nfs_get_inode(sb, fid->udf.parent_block,
958 				 fid->udf.parent_partref,
959 				 fid->udf.parent_generation);
960 }
961 static int udf_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
962 			 struct inode *parent)
963 {
964 	int len = *lenp;
965 	struct kernel_lb_addr location = UDF_I(inode)->i_location;
966 	struct fid *fid = (struct fid *)fh;
967 	int type = FILEID_UDF_WITHOUT_PARENT;
968 
969 	if (parent && (len < 5)) {
970 		*lenp = 5;
971 		return FILEID_INVALID;
972 	} else if (len < 3) {
973 		*lenp = 3;
974 		return FILEID_INVALID;
975 	}
976 
977 	*lenp = 3;
978 	fid->udf.block = location.logicalBlockNum;
979 	fid->udf.partref = location.partitionReferenceNum;
980 	fid->udf.parent_partref = 0;
981 	fid->udf.generation = inode->i_generation;
982 
983 	if (parent) {
984 		location = UDF_I(parent)->i_location;
985 		fid->udf.parent_block = location.logicalBlockNum;
986 		fid->udf.parent_partref = location.partitionReferenceNum;
987 		fid->udf.parent_generation = inode->i_generation;
988 		*lenp = 5;
989 		type = FILEID_UDF_WITH_PARENT;
990 	}
991 
992 	return type;
993 }
994 
995 const struct export_operations udf_export_ops = {
996 	.encode_fh	= udf_encode_fh,
997 	.fh_to_dentry   = udf_fh_to_dentry,
998 	.fh_to_parent   = udf_fh_to_parent,
999 	.get_parent     = udf_get_parent,
1000 };
1001 
1002 const struct inode_operations udf_dir_inode_operations = {
1003 	.lookup				= udf_lookup,
1004 	.create				= udf_create,
1005 	.link				= udf_link,
1006 	.unlink				= udf_unlink,
1007 	.symlink			= udf_symlink,
1008 	.mkdir				= udf_mkdir,
1009 	.rmdir				= udf_rmdir,
1010 	.mknod				= udf_mknod,
1011 	.rename				= udf_rename,
1012 	.tmpfile			= udf_tmpfile,
1013 };
1014