1 // SPDX-License-Identifier: GPL-2.0-only
6 * Inode name handling routines for the OSTA-UDF(tm) filesystem.
9 * (C) 1998-2004 Ben Fennema
10 * (C) 1999-2000 Stelias Computing Inc
14 * 12/12/98 blf Created. Split out the lookup code from dir.c
15 * 04/19/99 blf link, mknod, symlink support
22 #include <linux/string.h>
23 #include <linux/errno.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>
31 static inline int udf_match(int len1, const unsigned char *name1, int len2,
32 const unsigned char *name2)
37 return !memcmp(name1, name2, len1);
41 * udf_fiiter_find_entry - find entry in given directory.
43 * @dir: directory inode to search in
44 * @child: qstr of the name
45 * @iter: iter to use for searching
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.
50 * Returns 0 on success, < 0 on error (including -ENOENT).
52 static int udf_fiiter_find_entry(struct inode *dir, const struct qstr *child,
53 struct udf_fileident_iter *iter)
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] == '.';
62 fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
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))
74 if (iter->fi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) {
75 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
79 if ((iter->fi.fileCharacteristics & FID_FILE_CHAR_PARENT) &&
83 if (!iter->fi.lengthFileIdent)
86 flen = udf_get_filename(sb, iter->name,
87 iter->fi.lengthFileIdent, fname, UDF_NAME_LEN);
93 if (udf_match(flen, fname, child->len, child->name))
100 udf_fiiter_release(iter);
107 static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
110 struct inode *inode = NULL;
111 struct udf_fileident_iter iter;
114 if (dentry->d_name.len > UDF_NAME_LEN)
115 return ERR_PTR(-ENAMETOOLONG);
117 err = udf_fiiter_find_entry(dir, &dentry->d_name, &iter);
118 if (err < 0 && err != -ENOENT)
122 struct kernel_lb_addr loc;
124 loc = lelb_to_cpu(iter.fi.icb.extLocation);
125 udf_fiiter_release(&iter);
127 inode = udf_iget(dir->i_sb, &loc);
129 return ERR_CAST(inode);
132 return d_splice_alias(inode, dentry);
135 static int udf_expand_dir_adinicb(struct inode *inode, udf_pblk_t *block)
138 struct buffer_head *dbh = NULL;
139 struct kernel_lb_addr eloc;
140 struct extent_position epos;
142 struct udf_inode_info *iinfo = UDF_I(inode);
143 struct udf_fileident_iter iter;
147 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
148 alloctype = ICBTAG_FLAG_AD_SHORT;
150 alloctype = ICBTAG_FLAG_AD_LONG;
152 if (!inode->i_size) {
153 iinfo->i_alloc_type = alloctype;
154 mark_inode_dirty(inode);
158 /* alloc block, and copy data to it */
159 *block = udf_new_block(inode->i_sb, inode,
160 iinfo->i_location.partitionReferenceNum,
161 iinfo->i_location.logicalBlockNum, &ret);
164 newblock = udf_get_pblock(inode->i_sb, *block,
165 iinfo->i_location.partitionReferenceNum,
167 if (newblock == 0xffffffff)
168 return -EFSCORRUPTED;
169 dbh = sb_getblk(inode->i_sb, newblock);
173 memcpy(dbh->b_data, iinfo->i_data, inode->i_size);
174 memset(dbh->b_data + inode->i_size, 0,
175 inode->i_sb->s_blocksize - inode->i_size);
176 set_buffer_uptodate(dbh);
179 /* Drop inline data, add block instead */
180 iinfo->i_alloc_type = alloctype;
181 memset(iinfo->i_data + iinfo->i_lenEAttr, 0, iinfo->i_lenAlloc);
182 iinfo->i_lenAlloc = 0;
183 eloc.logicalBlockNum = *block;
184 eloc.partitionReferenceNum =
185 iinfo->i_location.partitionReferenceNum;
186 iinfo->i_lenExtents = inode->i_size;
188 epos.block = iinfo->i_location;
189 epos.offset = udf_file_entry_alloc_offset(inode);
190 ret = udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
194 udf_free_blocks(inode->i_sb, inode, &eloc, 0, 1);
197 mark_inode_dirty(inode);
199 /* Now fixup tags in moved directory entries */
200 for (ret = udf_fiiter_init(&iter, inode, 0);
201 !ret && iter.pos < inode->i_size;
202 ret = udf_fiiter_advance(&iter)) {
203 iter.fi.descTag.tagLocation = cpu_to_le32(*block);
204 if (iter.fi.lengthOfImpUse != cpu_to_le16(0))
205 impuse = dbh->b_data + iter.pos +
206 sizeof(struct fileIdentDesc);
209 udf_fiiter_write_fi(&iter, impuse);
213 * We don't expect the iteration to fail as the directory has been
214 * already verified to be correct
217 udf_fiiter_release(&iter);
222 static int udf_fiiter_add_entry(struct inode *dir, struct dentry *dentry,
223 struct udf_fileident_iter *iter)
225 struct udf_inode_info *dinfo = UDF_I(dir);
226 int nfidlen, namelen = 0;
228 int off, blksize = 1 << dir->i_blkbits;
230 char name[UDF_NAME_LEN_CS0];
233 if (!dentry->d_name.len)
235 namelen = udf_put_filename(dir->i_sb, dentry->d_name.name,
237 name, UDF_NAME_LEN_CS0);
239 return -ENAMETOOLONG;
241 nfidlen = ALIGN(sizeof(struct fileIdentDesc) + namelen, UDF_NAME_PAD);
243 for (ret = udf_fiiter_init(iter, dir, 0);
244 !ret && iter->pos < dir->i_size;
245 ret = udf_fiiter_advance(iter)) {
246 if (iter->fi.fileCharacteristics & FID_FILE_CHAR_DELETED) {
247 if (udf_dir_entry_len(&iter->fi) == nfidlen) {
248 iter->fi.descTag.tagSerialNum = cpu_to_le16(1);
249 iter->fi.fileVersionNum = cpu_to_le16(1);
250 iter->fi.fileCharacteristics = 0;
251 iter->fi.lengthFileIdent = namelen;
252 iter->fi.lengthOfImpUse = cpu_to_le16(0);
253 memcpy(iter->namebuf, name, namelen);
254 iter->name = iter->namebuf;
260 udf_fiiter_release(iter);
263 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
264 blksize - udf_ext0_offset(dir) - iter->pos < nfidlen) {
265 udf_fiiter_release(iter);
266 ret = udf_expand_dir_adinicb(dir, &block);
269 ret = udf_fiiter_init(iter, dir, dir->i_size);
274 /* Get blocknumber to use for entry tag */
275 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
276 block = dinfo->i_location.logicalBlockNum;
278 block = iter->eloc.logicalBlockNum +
279 ((iter->elen - 1) >> dir->i_blkbits);
281 off = iter->pos & (blksize - 1);
284 /* Entry fits into current block? */
285 if (blksize - udf_ext0_offset(dir) - off >= nfidlen)
288 ret = udf_fiiter_append_blk(iter);
290 udf_fiiter_release(iter);
294 /* Entry will be completely in the new block? Update tag location... */
295 if (!(iter->pos & (blksize - 1)))
296 block = iter->eloc.logicalBlockNum +
297 ((iter->elen - 1) >> dir->i_blkbits);
299 memset(&iter->fi, 0, sizeof(struct fileIdentDesc));
300 if (UDF_SB(dir->i_sb)->s_udfrev >= 0x0200)
301 udf_new_tag((char *)(&iter->fi), TAG_IDENT_FID, 3, 1, block,
304 udf_new_tag((char *)(&iter->fi), TAG_IDENT_FID, 2, 1, block,
306 iter->fi.fileVersionNum = cpu_to_le16(1);
307 iter->fi.lengthFileIdent = namelen;
308 iter->fi.lengthOfImpUse = cpu_to_le16(0);
309 memcpy(iter->namebuf, name, namelen);
310 iter->name = iter->namebuf;
312 dir->i_size += nfidlen;
313 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
314 dinfo->i_lenAlloc += nfidlen;
316 /* Truncate last extent to proper size */
317 udf_fiiter_update_elen(iter, iter->elen -
318 (dinfo->i_lenExtents - dir->i_size));
320 mark_inode_dirty(dir);
325 static void udf_fiiter_delete_entry(struct udf_fileident_iter *iter)
327 iter->fi.fileCharacteristics |= FID_FILE_CHAR_DELETED;
329 if (UDF_QUERY_FLAG(iter->dir->i_sb, UDF_FLAG_STRICT))
330 memset(&iter->fi.icb, 0x00, sizeof(struct long_ad));
332 udf_fiiter_write_fi(iter, NULL);
335 static void udf_add_fid_counter(struct super_block *sb, bool dir, int val)
337 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sb);
341 mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
343 le32_add_cpu(&lvidiu->numDirs, val);
345 le32_add_cpu(&lvidiu->numFiles, val);
346 udf_updated_lvid(sb);
347 mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
350 static int udf_add_nondir(struct dentry *dentry, struct inode *inode)
352 struct udf_inode_info *iinfo = UDF_I(inode);
353 struct inode *dir = d_inode(dentry->d_parent);
354 struct udf_fileident_iter iter;
357 err = udf_fiiter_add_entry(dir, dentry, &iter);
359 inode_dec_link_count(inode);
360 discard_new_inode(inode);
363 iter.fi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
364 iter.fi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
365 *(__le32 *)((struct allocDescImpUse *)iter.fi.icb.impUse)->impUse =
366 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
367 udf_fiiter_write_fi(&iter, NULL);
368 dir->i_ctime = dir->i_mtime = current_time(dir);
369 mark_inode_dirty(dir);
370 udf_fiiter_release(&iter);
371 udf_add_fid_counter(dir->i_sb, false, 1);
372 d_instantiate_new(dentry, inode);
377 static int udf_create(struct mnt_idmap *idmap, struct inode *dir,
378 struct dentry *dentry, umode_t mode, bool excl)
380 struct inode *inode = udf_new_inode(dir, mode);
383 return PTR_ERR(inode);
385 inode->i_data.a_ops = &udf_aops;
386 inode->i_op = &udf_file_inode_operations;
387 inode->i_fop = &udf_file_operations;
388 mark_inode_dirty(inode);
390 return udf_add_nondir(dentry, inode);
393 static int udf_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
394 struct file *file, umode_t mode)
396 struct inode *inode = udf_new_inode(dir, mode);
399 return PTR_ERR(inode);
401 inode->i_data.a_ops = &udf_aops;
402 inode->i_op = &udf_file_inode_operations;
403 inode->i_fop = &udf_file_operations;
404 mark_inode_dirty(inode);
405 d_tmpfile(file, inode);
406 unlock_new_inode(inode);
407 return finish_open_simple(file, 0);
410 static int udf_mknod(struct mnt_idmap *idmap, struct inode *dir,
411 struct dentry *dentry, umode_t mode, dev_t rdev)
415 if (!old_valid_dev(rdev))
418 inode = udf_new_inode(dir, mode);
420 return PTR_ERR(inode);
422 init_special_inode(inode, mode, rdev);
423 return udf_add_nondir(dentry, inode);
426 static int udf_mkdir(struct mnt_idmap *idmap, struct inode *dir,
427 struct dentry *dentry, umode_t mode)
430 struct udf_fileident_iter iter;
432 struct udf_inode_info *dinfo = UDF_I(dir);
433 struct udf_inode_info *iinfo;
435 inode = udf_new_inode(dir, S_IFDIR | mode);
437 return PTR_ERR(inode);
439 iinfo = UDF_I(inode);
440 inode->i_op = &udf_dir_inode_operations;
441 inode->i_fop = &udf_dir_operations;
442 err = udf_fiiter_add_entry(inode, NULL, &iter);
445 discard_new_inode(inode);
449 iter.fi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
450 iter.fi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
451 *(__le32 *)((struct allocDescImpUse *)iter.fi.icb.impUse)->impUse =
452 cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
453 iter.fi.fileCharacteristics =
454 FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
455 udf_fiiter_write_fi(&iter, NULL);
456 udf_fiiter_release(&iter);
457 mark_inode_dirty(inode);
459 err = udf_fiiter_add_entry(dir, dentry, &iter);
462 discard_new_inode(inode);
465 iter.fi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
466 iter.fi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
467 *(__le32 *)((struct allocDescImpUse *)iter.fi.icb.impUse)->impUse =
468 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
469 iter.fi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
470 udf_fiiter_write_fi(&iter, NULL);
471 udf_fiiter_release(&iter);
472 udf_add_fid_counter(dir->i_sb, true, 1);
474 dir->i_ctime = dir->i_mtime = current_time(dir);
475 mark_inode_dirty(dir);
476 d_instantiate_new(dentry, inode);
481 static int empty_dir(struct inode *dir)
483 struct udf_fileident_iter iter;
486 for (ret = udf_fiiter_init(&iter, dir, 0);
487 !ret && iter.pos < dir->i_size;
488 ret = udf_fiiter_advance(&iter)) {
489 if (iter.fi.lengthFileIdent &&
490 !(iter.fi.fileCharacteristics & FID_FILE_CHAR_DELETED)) {
491 udf_fiiter_release(&iter);
495 udf_fiiter_release(&iter);
500 static int udf_rmdir(struct inode *dir, struct dentry *dentry)
503 struct inode *inode = d_inode(dentry);
504 struct udf_fileident_iter iter;
505 struct kernel_lb_addr tloc;
507 ret = udf_fiiter_find_entry(dir, &dentry->d_name, &iter);
512 tloc = lelb_to_cpu(iter.fi.icb.extLocation);
513 if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
516 if (!empty_dir(inode))
518 udf_fiiter_delete_entry(&iter);
519 if (inode->i_nlink != 2)
520 udf_warn(inode->i_sb, "empty directory has nlink != 2 (%u)\n",
524 inode_dec_link_count(dir);
525 udf_add_fid_counter(dir->i_sb, true, -1);
526 inode->i_ctime = dir->i_ctime = dir->i_mtime =
528 mark_inode_dirty(dir);
531 udf_fiiter_release(&iter);
536 static int udf_unlink(struct inode *dir, struct dentry *dentry)
539 struct inode *inode = d_inode(dentry);
540 struct udf_fileident_iter iter;
541 struct kernel_lb_addr tloc;
543 ret = udf_fiiter_find_entry(dir, &dentry->d_name, &iter);
548 tloc = lelb_to_cpu(iter.fi.icb.extLocation);
549 if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
552 if (!inode->i_nlink) {
553 udf_debug("Deleting nonexistent file (%lu), %u\n",
554 inode->i_ino, inode->i_nlink);
557 udf_fiiter_delete_entry(&iter);
558 dir->i_ctime = dir->i_mtime = current_time(dir);
559 mark_inode_dirty(dir);
560 inode_dec_link_count(inode);
561 udf_add_fid_counter(dir->i_sb, false, -1);
562 inode->i_ctime = dir->i_ctime;
565 udf_fiiter_release(&iter);
570 static int udf_symlink(struct mnt_idmap *idmap, struct inode *dir,
571 struct dentry *dentry, const char *symname)
573 struct inode *inode = udf_new_inode(dir, S_IFLNK | 0777);
574 struct pathComponent *pc;
575 const char *compstart;
576 struct extent_position epos = {};
577 int eoffset, elen = 0;
581 unsigned char *name = NULL;
583 struct udf_inode_info *iinfo;
584 struct super_block *sb = dir->i_sb;
587 return PTR_ERR(inode);
589 iinfo = UDF_I(inode);
590 down_write(&iinfo->i_data_sem);
591 name = kmalloc(UDF_NAME_LEN_CS0, GFP_NOFS);
597 inode->i_data.a_ops = &udf_symlink_aops;
598 inode->i_op = &udf_symlink_inode_operations;
599 inode_nohighmem(inode);
601 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
602 struct kernel_lb_addr eloc;
605 block = udf_new_block(sb, inode,
606 iinfo->i_location.partitionReferenceNum,
607 iinfo->i_location.logicalBlockNum, &err);
610 epos.block = iinfo->i_location;
611 epos.offset = udf_file_entry_alloc_offset(inode);
613 eloc.logicalBlockNum = block;
614 eloc.partitionReferenceNum =
615 iinfo->i_location.partitionReferenceNum;
616 bsize = sb->s_blocksize;
617 iinfo->i_lenExtents = bsize;
618 err = udf_add_aext(inode, &epos, &eloc, bsize, 0);
621 udf_free_blocks(sb, inode, &eloc, 0, 1);
625 block = udf_get_pblock(sb, block,
626 iinfo->i_location.partitionReferenceNum,
628 epos.bh = sb_getblk(sb, block);
629 if (unlikely(!epos.bh)) {
631 udf_free_blocks(sb, inode, &eloc, 0, 1);
634 lock_buffer(epos.bh);
635 memset(epos.bh->b_data, 0x00, bsize);
636 set_buffer_uptodate(epos.bh);
637 unlock_buffer(epos.bh);
638 mark_buffer_dirty_inode(epos.bh, inode);
639 ea = epos.bh->b_data + udf_ext0_offset(inode);
641 ea = iinfo->i_data + iinfo->i_lenEAttr;
643 eoffset = sb->s_blocksize - udf_ext0_offset(inode);
644 pc = (struct pathComponent *)ea;
646 if (*symname == '/') {
649 } while (*symname == '/');
651 pc->componentType = 1;
652 pc->lengthComponentIdent = 0;
653 pc->componentFileVersionNum = 0;
654 elen += sizeof(struct pathComponent);
660 if (elen + sizeof(struct pathComponent) > eoffset)
663 pc = (struct pathComponent *)(ea + elen);
669 } while (*symname && *symname != '/');
671 pc->componentType = 5;
672 pc->lengthComponentIdent = 0;
673 pc->componentFileVersionNum = 0;
674 if (compstart[0] == '.') {
675 if ((symname - compstart) == 1)
676 pc->componentType = 4;
677 else if ((symname - compstart) == 2 &&
679 pc->componentType = 3;
682 if (pc->componentType == 5) {
683 namelen = udf_put_filename(sb, compstart,
685 name, UDF_NAME_LEN_CS0);
689 if (elen + sizeof(struct pathComponent) + namelen >
693 pc->lengthComponentIdent = namelen;
695 memcpy(pc->componentIdent, name, namelen);
698 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
703 } while (*symname == '/');
708 inode->i_size = elen;
709 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
710 iinfo->i_lenAlloc = inode->i_size;
712 udf_truncate_tail_extent(inode);
713 mark_inode_dirty(inode);
714 up_write(&iinfo->i_data_sem);
716 err = udf_add_nondir(dentry, inode);
722 up_write(&iinfo->i_data_sem);
723 inode_dec_link_count(inode);
724 discard_new_inode(inode);
728 static int udf_link(struct dentry *old_dentry, struct inode *dir,
729 struct dentry *dentry)
731 struct inode *inode = d_inode(old_dentry);
732 struct udf_fileident_iter iter;
735 err = udf_fiiter_add_entry(dir, dentry, &iter);
738 iter.fi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
739 iter.fi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
740 if (UDF_SB(inode->i_sb)->s_lvid_bh) {
741 *(__le32 *)((struct allocDescImpUse *)iter.fi.icb.impUse)->impUse =
742 cpu_to_le32(lvid_get_unique_id(inode->i_sb));
744 udf_fiiter_write_fi(&iter, NULL);
745 udf_fiiter_release(&iter);
748 udf_add_fid_counter(dir->i_sb, false, 1);
749 inode->i_ctime = current_time(inode);
750 mark_inode_dirty(inode);
751 dir->i_ctime = dir->i_mtime = current_time(dir);
752 mark_inode_dirty(dir);
754 d_instantiate(dentry, inode);
759 /* Anybody can rename anything with this: the permission checks are left to the
760 * higher-level routines.
762 static int udf_rename(struct mnt_idmap *idmap, struct inode *old_dir,
763 struct dentry *old_dentry, struct inode *new_dir,
764 struct dentry *new_dentry, unsigned int flags)
766 struct inode *old_inode = d_inode(old_dentry);
767 struct inode *new_inode = d_inode(new_dentry);
768 struct udf_fileident_iter oiter, niter, diriter;
769 bool has_diriter = false;
771 struct kernel_lb_addr tloc;
773 if (flags & ~RENAME_NOREPLACE)
776 retval = udf_fiiter_find_entry(old_dir, &old_dentry->d_name, &oiter);
780 tloc = lelb_to_cpu(oiter.fi.icb.extLocation);
781 if (udf_get_lb_pblock(old_dir->i_sb, &tloc, 0) != old_inode->i_ino) {
786 if (S_ISDIR(old_inode->i_mode)) {
789 if (!empty_dir(new_inode))
792 retval = udf_fiiter_find_entry(old_inode, &dotdot_name,
794 if (retval == -ENOENT) {
795 udf_err(old_inode->i_sb,
796 "directory (ino %lu) has no '..' entry\n",
798 retval = -EFSCORRUPTED;
803 tloc = lelb_to_cpu(diriter.fi.icb.extLocation);
804 if (udf_get_lb_pblock(old_inode->i_sb, &tloc, 0) !=
806 retval = -EFSCORRUPTED;
807 udf_err(old_inode->i_sb,
808 "directory (ino %lu) has parent entry pointing to another inode (%lu != %u)\n",
809 old_inode->i_ino, old_dir->i_ino,
810 udf_get_lb_pblock(old_inode->i_sb, &tloc, 0));
815 retval = udf_fiiter_find_entry(new_dir, &new_dentry->d_name, &niter);
816 if (retval && retval != -ENOENT)
818 /* Entry found but not passed by VFS? */
819 if (!retval && !new_inode) {
820 retval = -EFSCORRUPTED;
821 udf_fiiter_release(&niter);
824 /* Entry not found? Need to add one... */
826 udf_fiiter_release(&niter);
827 retval = udf_fiiter_add_entry(new_dir, new_dentry, &niter);
833 * Like most other Unix systems, set the ctime for inodes on a
836 old_inode->i_ctime = current_time(old_inode);
837 mark_inode_dirty(old_inode);
842 niter.fi.fileVersionNum = oiter.fi.fileVersionNum;
843 niter.fi.fileCharacteristics = oiter.fi.fileCharacteristics;
844 memcpy(&(niter.fi.icb), &(oiter.fi.icb), sizeof(oiter.fi.icb));
845 udf_fiiter_write_fi(&niter, NULL);
846 udf_fiiter_release(&niter);
849 * The old entry may have moved due to new entry allocation. Find it
852 udf_fiiter_release(&oiter);
853 retval = udf_fiiter_find_entry(old_dir, &old_dentry->d_name, &oiter);
855 udf_err(old_dir->i_sb,
856 "failed to find renamed entry again in directory (ino %lu)\n",
859 udf_fiiter_delete_entry(&oiter);
860 udf_fiiter_release(&oiter);
864 new_inode->i_ctime = current_time(new_inode);
865 inode_dec_link_count(new_inode);
866 udf_add_fid_counter(old_dir->i_sb, S_ISDIR(new_inode->i_mode),
869 old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
870 new_dir->i_ctime = new_dir->i_mtime = current_time(new_dir);
871 mark_inode_dirty(old_dir);
872 mark_inode_dirty(new_dir);
875 diriter.fi.icb.extLocation =
876 cpu_to_lelb(UDF_I(new_dir)->i_location);
877 udf_update_tag((char *)&diriter.fi,
878 udf_dir_entry_len(&diriter.fi));
879 udf_fiiter_write_fi(&diriter, NULL);
880 udf_fiiter_release(&diriter);
882 inode_dec_link_count(old_dir);
884 inode_dec_link_count(new_inode);
887 mark_inode_dirty(new_dir);
893 udf_fiiter_release(&diriter);
894 udf_fiiter_release(&oiter);
899 static struct dentry *udf_get_parent(struct dentry *child)
901 struct kernel_lb_addr tloc;
902 struct inode *inode = NULL;
903 struct udf_fileident_iter iter;
906 err = udf_fiiter_find_entry(d_inode(child), &dotdot_name, &iter);
910 tloc = lelb_to_cpu(iter.fi.icb.extLocation);
911 udf_fiiter_release(&iter);
912 inode = udf_iget(child->d_sb, &tloc);
914 return ERR_CAST(inode);
916 return d_obtain_alias(inode);
920 static struct dentry *udf_nfs_get_inode(struct super_block *sb, u32 block,
921 u16 partref, __u32 generation)
924 struct kernel_lb_addr loc;
927 return ERR_PTR(-ESTALE);
929 loc.logicalBlockNum = block;
930 loc.partitionReferenceNum = partref;
931 inode = udf_iget(sb, &loc);
934 return ERR_CAST(inode);
936 if (generation && inode->i_generation != generation) {
938 return ERR_PTR(-ESTALE);
940 return d_obtain_alias(inode);
943 static struct dentry *udf_fh_to_dentry(struct super_block *sb,
944 struct fid *fid, int fh_len, int fh_type)
947 (fh_type != FILEID_UDF_WITH_PARENT &&
948 fh_type != FILEID_UDF_WITHOUT_PARENT))
951 return udf_nfs_get_inode(sb, fid->udf.block, fid->udf.partref,
952 fid->udf.generation);
955 static struct dentry *udf_fh_to_parent(struct super_block *sb,
956 struct fid *fid, int fh_len, int fh_type)
958 if (fh_len < 5 || fh_type != FILEID_UDF_WITH_PARENT)
961 return udf_nfs_get_inode(sb, fid->udf.parent_block,
962 fid->udf.parent_partref,
963 fid->udf.parent_generation);
965 static int udf_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
966 struct inode *parent)
969 struct kernel_lb_addr location = UDF_I(inode)->i_location;
970 struct fid *fid = (struct fid *)fh;
971 int type = FILEID_UDF_WITHOUT_PARENT;
973 if (parent && (len < 5)) {
975 return FILEID_INVALID;
976 } else if (len < 3) {
978 return FILEID_INVALID;
982 fid->udf.block = location.logicalBlockNum;
983 fid->udf.partref = location.partitionReferenceNum;
984 fid->udf.parent_partref = 0;
985 fid->udf.generation = inode->i_generation;
988 location = UDF_I(parent)->i_location;
989 fid->udf.parent_block = location.logicalBlockNum;
990 fid->udf.parent_partref = location.partitionReferenceNum;
991 fid->udf.parent_generation = inode->i_generation;
993 type = FILEID_UDF_WITH_PARENT;
999 const struct export_operations udf_export_ops = {
1000 .encode_fh = udf_encode_fh,
1001 .fh_to_dentry = udf_fh_to_dentry,
1002 .fh_to_parent = udf_fh_to_parent,
1003 .get_parent = udf_get_parent,
1006 const struct inode_operations udf_dir_inode_operations = {
1007 .lookup = udf_lookup,
1008 .create = udf_create,
1010 .unlink = udf_unlink,
1011 .symlink = udf_symlink,
1015 .rename = udf_rename,
1016 .tmpfile = udf_tmpfile,