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