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_KERNEL
);
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
);
130 return d_splice_alias(inode
, dentry
);
133 static int udf_expand_dir_adinicb(struct inode
*inode
, udf_pblk_t
*block
)
136 struct buffer_head
*dbh
= NULL
;
137 struct kernel_lb_addr eloc
;
138 struct extent_position epos
;
140 struct udf_inode_info
*iinfo
= UDF_I(inode
);
141 struct udf_fileident_iter iter
;
145 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_USE_SHORT_AD
))
146 alloctype
= ICBTAG_FLAG_AD_SHORT
;
148 alloctype
= ICBTAG_FLAG_AD_LONG
;
150 if (!inode
->i_size
) {
151 iinfo
->i_alloc_type
= alloctype
;
152 mark_inode_dirty(inode
);
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
);
162 newblock
= udf_get_pblock(inode
->i_sb
, *block
,
163 iinfo
->i_location
.partitionReferenceNum
,
165 if (newblock
== 0xffffffff)
166 return -EFSCORRUPTED
;
167 dbh
= sb_getblk(inode
->i_sb
, newblock
);
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
);
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
;
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);
192 udf_free_blocks(inode
->i_sb
, inode
, &eloc
, 0, 1);
195 mark_inode_dirty(inode
);
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
);
207 udf_fiiter_write_fi(&iter
, impuse
);
211 * We don't expect the iteration to fail as the directory has been
212 * already verified to be correct
215 udf_fiiter_release(&iter
);
220 static int udf_fiiter_add_entry(struct inode
*dir
, struct dentry
*dentry
,
221 struct udf_fileident_iter
*iter
)
223 struct udf_inode_info
*dinfo
= UDF_I(dir
);
224 int nfidlen
, namelen
= 0;
226 int off
, blksize
= 1 << dir
->i_blkbits
;
228 char name
[UDF_NAME_LEN_CS0
];
231 namelen
= udf_put_filename(dir
->i_sb
, dentry
->d_name
.name
,
233 name
, UDF_NAME_LEN_CS0
);
235 return -ENAMETOOLONG
;
237 nfidlen
= ALIGN(sizeof(struct fileIdentDesc
) + namelen
, UDF_NAME_PAD
);
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
;
256 udf_fiiter_release(iter
);
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
);
265 ret
= udf_fiiter_init(iter
, dir
, dir
->i_size
);
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
;
274 block
= iter
->eloc
.logicalBlockNum
+
275 ((iter
->elen
- 1) >> dir
->i_blkbits
);
277 off
= iter
->pos
& (blksize
- 1);
280 /* Entry fits into current block? */
281 if (blksize
- udf_ext0_offset(dir
) - off
>= nfidlen
)
284 ret
= udf_fiiter_append_blk(iter
);
286 udf_fiiter_release(iter
);
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
);
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
,
300 udf_new_tag((char *)(&iter
->fi
), TAG_IDENT_FID
, 2, 1, block
,
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
;
308 dir
->i_size
+= nfidlen
;
309 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
310 dinfo
->i_lenAlloc
+= nfidlen
;
312 /* Truncate last extent to proper size */
313 udf_fiiter_update_elen(iter
, iter
->elen
-
314 (dinfo
->i_lenExtents
- dir
->i_size
));
316 mark_inode_dirty(dir
);
321 static void udf_fiiter_delete_entry(struct udf_fileident_iter
*iter
)
323 iter
->fi
.fileCharacteristics
|= FID_FILE_CHAR_DELETED
;
325 if (UDF_QUERY_FLAG(iter
->dir
->i_sb
, UDF_FLAG_STRICT
))
326 memset(&iter
->fi
.icb
, 0x00, sizeof(struct long_ad
));
328 udf_fiiter_write_fi(iter
, NULL
);
331 static void udf_add_fid_counter(struct super_block
*sb
, bool dir
, int val
)
333 struct logicalVolIntegrityDescImpUse
*lvidiu
= udf_sb_lvidiu(sb
);
337 mutex_lock(&UDF_SB(sb
)->s_alloc_mutex
);
339 le32_add_cpu(&lvidiu
->numDirs
, val
);
341 le32_add_cpu(&lvidiu
->numFiles
, val
);
342 udf_updated_lvid(sb
);
343 mutex_unlock(&UDF_SB(sb
)->s_alloc_mutex
);
346 static int udf_add_nondir(struct dentry
*dentry
, struct inode
*inode
)
348 struct udf_inode_info
*iinfo
= UDF_I(inode
);
349 struct inode
*dir
= d_inode(dentry
->d_parent
);
350 struct udf_fileident_iter iter
;
353 err
= udf_fiiter_add_entry(dir
, dentry
, &iter
);
355 inode_dec_link_count(inode
);
356 discard_new_inode(inode
);
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
);
373 static int udf_create(struct mnt_idmap
*idmap
, struct inode
*dir
,
374 struct dentry
*dentry
, umode_t mode
, bool excl
)
376 struct inode
*inode
= udf_new_inode(dir
, mode
);
379 return PTR_ERR(inode
);
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
);
386 return udf_add_nondir(dentry
, inode
);
389 static int udf_tmpfile(struct mnt_idmap
*idmap
, struct inode
*dir
,
390 struct file
*file
, umode_t mode
)
392 struct inode
*inode
= udf_new_inode(dir
, mode
);
395 return PTR_ERR(inode
);
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);
406 static int udf_mknod(struct mnt_idmap
*idmap
, struct inode
*dir
,
407 struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
411 if (!old_valid_dev(rdev
))
414 inode
= udf_new_inode(dir
, mode
);
416 return PTR_ERR(inode
);
418 init_special_inode(inode
, mode
, rdev
);
419 return udf_add_nondir(dentry
, inode
);
422 static int udf_mkdir(struct mnt_idmap
*idmap
, struct inode
*dir
,
423 struct dentry
*dentry
, umode_t mode
)
426 struct udf_fileident_iter iter
;
428 struct udf_inode_info
*dinfo
= UDF_I(dir
);
429 struct udf_inode_info
*iinfo
;
431 inode
= udf_new_inode(dir
, S_IFDIR
| mode
);
433 return PTR_ERR(inode
);
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
);
441 discard_new_inode(inode
);
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
);
455 err
= udf_fiiter_add_entry(dir
, dentry
, &iter
);
458 discard_new_inode(inode
);
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);
470 inode_set_mtime_to_ts(dir
, inode_set_ctime_current(dir
));
471 mark_inode_dirty(dir
);
472 d_instantiate_new(dentry
, inode
);
477 static int empty_dir(struct inode
*dir
)
479 struct udf_fileident_iter iter
;
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
);
491 udf_fiiter_release(&iter
);
496 static int udf_rmdir(struct inode
*dir
, struct dentry
*dentry
)
499 struct inode
*inode
= d_inode(dentry
);
500 struct udf_fileident_iter iter
;
501 struct kernel_lb_addr tloc
;
503 ret
= udf_fiiter_find_entry(dir
, &dentry
->d_name
, &iter
);
508 tloc
= lelb_to_cpu(iter
.fi
.icb
.extLocation
);
509 if (udf_get_lb_pblock(dir
->i_sb
, &tloc
, 0) != inode
->i_ino
)
512 if (!empty_dir(inode
))
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",
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
);
527 udf_fiiter_release(&iter
);
532 static int udf_unlink(struct inode
*dir
, struct dentry
*dentry
)
535 struct inode
*inode
= d_inode(dentry
);
536 struct udf_fileident_iter iter
;
537 struct kernel_lb_addr tloc
;
539 ret
= udf_fiiter_find_entry(dir
, &dentry
->d_name
, &iter
);
544 tloc
= lelb_to_cpu(iter
.fi
.icb
.extLocation
);
545 if (udf_get_lb_pblock(dir
->i_sb
, &tloc
, 0) != inode
->i_ino
)
548 if (!inode
->i_nlink
) {
549 udf_debug("Deleting nonexistent file (%lu), %u\n",
550 inode
->i_ino
, inode
->i_nlink
);
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
));
561 udf_fiiter_release(&iter
);
566 static int udf_symlink(struct mnt_idmap
*idmap
, struct inode
*dir
,
567 struct dentry
*dentry
, const char *symname
)
570 struct pathComponent
*pc
;
571 const char *compstart
;
572 struct extent_position epos
= {};
573 int eoffset
, elen
= 0;
577 unsigned char *name
= NULL
;
579 struct udf_inode_info
*iinfo
;
580 struct super_block
*sb
= dir
->i_sb
;
582 name
= kmalloc(UDF_NAME_LEN_CS0
, GFP_KERNEL
);
588 inode
= udf_new_inode(dir
, S_IFLNK
| 0777);
590 err
= PTR_ERR(inode
);
594 iinfo
= UDF_I(inode
);
595 down_write(&iinfo
->i_data_sem
);
596 inode
->i_data
.a_ops
= &udf_symlink_aops
;
597 inode
->i_op
= &udf_symlink_inode_operations
;
598 inode_nohighmem(inode
);
600 if (iinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_IN_ICB
) {
601 struct kernel_lb_addr eloc
;
604 block
= udf_new_block(sb
, inode
,
605 iinfo
->i_location
.partitionReferenceNum
,
606 iinfo
->i_location
.logicalBlockNum
, &err
);
609 epos
.block
= iinfo
->i_location
;
610 epos
.offset
= udf_file_entry_alloc_offset(inode
);
612 eloc
.logicalBlockNum
= block
;
613 eloc
.partitionReferenceNum
=
614 iinfo
->i_location
.partitionReferenceNum
;
615 bsize
= sb
->s_blocksize
;
616 iinfo
->i_lenExtents
= bsize
;
617 err
= udf_add_aext(inode
, &epos
, &eloc
, bsize
, 0);
620 udf_free_blocks(sb
, inode
, &eloc
, 0, 1);
624 block
= udf_get_pblock(sb
, block
,
625 iinfo
->i_location
.partitionReferenceNum
,
627 epos
.bh
= sb_getblk(sb
, block
);
628 if (unlikely(!epos
.bh
)) {
630 udf_free_blocks(sb
, inode
, &eloc
, 0, 1);
633 lock_buffer(epos
.bh
);
634 memset(epos
.bh
->b_data
, 0x00, bsize
);
635 set_buffer_uptodate(epos
.bh
);
636 unlock_buffer(epos
.bh
);
637 mark_buffer_dirty_inode(epos
.bh
, inode
);
638 ea
= epos
.bh
->b_data
+ udf_ext0_offset(inode
);
640 ea
= iinfo
->i_data
+ iinfo
->i_lenEAttr
;
642 eoffset
= sb
->s_blocksize
- udf_ext0_offset(inode
);
643 pc
= (struct pathComponent
*)ea
;
645 if (*symname
== '/') {
648 } while (*symname
== '/');
650 pc
->componentType
= 1;
651 pc
->lengthComponentIdent
= 0;
652 pc
->componentFileVersionNum
= 0;
653 elen
+= sizeof(struct pathComponent
);
659 if (elen
+ sizeof(struct pathComponent
) > eoffset
)
662 pc
= (struct pathComponent
*)(ea
+ elen
);
668 } while (*symname
&& *symname
!= '/');
670 pc
->componentType
= 5;
671 pc
->lengthComponentIdent
= 0;
672 pc
->componentFileVersionNum
= 0;
673 if (compstart
[0] == '.') {
674 if ((symname
- compstart
) == 1)
675 pc
->componentType
= 4;
676 else if ((symname
- compstart
) == 2 &&
678 pc
->componentType
= 3;
681 if (pc
->componentType
== 5) {
682 namelen
= udf_put_filename(sb
, compstart
,
684 name
, UDF_NAME_LEN_CS0
);
688 if (elen
+ sizeof(struct pathComponent
) + namelen
>
692 pc
->lengthComponentIdent
= namelen
;
694 memcpy(pc
->componentIdent
, name
, namelen
);
697 elen
+= sizeof(struct pathComponent
) + pc
->lengthComponentIdent
;
702 } while (*symname
== '/');
707 inode
->i_size
= elen
;
708 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
709 iinfo
->i_lenAlloc
= inode
->i_size
;
711 udf_truncate_tail_extent(inode
);
712 mark_inode_dirty(inode
);
713 up_write(&iinfo
->i_data_sem
);
715 err
= udf_add_nondir(dentry
, inode
);
721 up_write(&iinfo
->i_data_sem
);
722 inode_dec_link_count(inode
);
723 discard_new_inode(inode
);
727 static int udf_link(struct dentry
*old_dentry
, struct inode
*dir
,
728 struct dentry
*dentry
)
730 struct inode
*inode
= d_inode(old_dentry
);
731 struct udf_fileident_iter iter
;
734 err
= udf_fiiter_add_entry(dir
, dentry
, &iter
);
737 iter
.fi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
738 iter
.fi
.icb
.extLocation
= cpu_to_lelb(UDF_I(inode
)->i_location
);
739 if (UDF_SB(inode
->i_sb
)->s_lvid_bh
) {
740 *(__le32
*)((struct allocDescImpUse
*)iter
.fi
.icb
.impUse
)->impUse
=
741 cpu_to_le32(lvid_get_unique_id(inode
->i_sb
));
743 udf_fiiter_write_fi(&iter
, NULL
);
744 udf_fiiter_release(&iter
);
747 udf_add_fid_counter(dir
->i_sb
, false, 1);
748 inode_set_ctime_current(inode
);
749 mark_inode_dirty(inode
);
750 inode_set_mtime_to_ts(dir
, inode_set_ctime_current(dir
));
751 mark_inode_dirty(dir
);
753 d_instantiate(dentry
, inode
);
758 /* Anybody can rename anything with this: the permission checks are left to the
759 * higher-level routines.
761 static int udf_rename(struct mnt_idmap
*idmap
, struct inode
*old_dir
,
762 struct dentry
*old_dentry
, struct inode
*new_dir
,
763 struct dentry
*new_dentry
, unsigned int flags
)
765 struct inode
*old_inode
= d_inode(old_dentry
);
766 struct inode
*new_inode
= d_inode(new_dentry
);
767 struct udf_fileident_iter oiter
, niter
, diriter
;
768 bool has_diriter
= false, is_dir
= false;
770 struct kernel_lb_addr tloc
;
772 if (flags
& ~RENAME_NOREPLACE
)
775 retval
= udf_fiiter_find_entry(old_dir
, &old_dentry
->d_name
, &oiter
);
779 tloc
= lelb_to_cpu(oiter
.fi
.icb
.extLocation
);
780 if (udf_get_lb_pblock(old_dir
->i_sb
, &tloc
, 0) != old_inode
->i_ino
) {
785 if (S_ISDIR(old_inode
->i_mode
)) {
788 if (!empty_dir(new_inode
))
793 if (is_dir
&& old_dir
!= new_dir
) {
794 retval
= udf_fiiter_find_entry(old_inode
, &dotdot_name
,
796 if (retval
== -ENOENT
) {
797 udf_err(old_inode
->i_sb
,
798 "directory (ino %lu) has no '..' entry\n",
800 retval
= -EFSCORRUPTED
;
805 tloc
= lelb_to_cpu(diriter
.fi
.icb
.extLocation
);
806 if (udf_get_lb_pblock(old_inode
->i_sb
, &tloc
, 0) !=
808 retval
= -EFSCORRUPTED
;
809 udf_err(old_inode
->i_sb
,
810 "directory (ino %lu) has parent entry pointing to another inode (%lu != %u)\n",
811 old_inode
->i_ino
, old_dir
->i_ino
,
812 udf_get_lb_pblock(old_inode
->i_sb
, &tloc
, 0));
817 retval
= udf_fiiter_find_entry(new_dir
, &new_dentry
->d_name
, &niter
);
818 if (retval
&& retval
!= -ENOENT
)
820 /* Entry found but not passed by VFS? */
821 if (!retval
&& !new_inode
) {
822 retval
= -EFSCORRUPTED
;
823 udf_fiiter_release(&niter
);
826 /* Entry not found? Need to add one... */
828 udf_fiiter_release(&niter
);
829 retval
= udf_fiiter_add_entry(new_dir
, new_dentry
, &niter
);
835 * Like most other Unix systems, set the ctime for inodes on a
838 inode_set_ctime_current(old_inode
);
839 mark_inode_dirty(old_inode
);
844 niter
.fi
.fileVersionNum
= oiter
.fi
.fileVersionNum
;
845 niter
.fi
.fileCharacteristics
= oiter
.fi
.fileCharacteristics
;
846 memcpy(&(niter
.fi
.icb
), &(oiter
.fi
.icb
), sizeof(oiter
.fi
.icb
));
847 udf_fiiter_write_fi(&niter
, NULL
);
848 udf_fiiter_release(&niter
);
851 * The old entry may have moved due to new entry allocation. Find it
854 udf_fiiter_release(&oiter
);
855 retval
= udf_fiiter_find_entry(old_dir
, &old_dentry
->d_name
, &oiter
);
857 udf_err(old_dir
->i_sb
,
858 "failed to find renamed entry again in directory (ino %lu)\n",
861 udf_fiiter_delete_entry(&oiter
);
862 udf_fiiter_release(&oiter
);
866 inode_set_ctime_current(new_inode
);
867 inode_dec_link_count(new_inode
);
868 udf_add_fid_counter(old_dir
->i_sb
, S_ISDIR(new_inode
->i_mode
),
871 inode_set_mtime_to_ts(old_dir
, inode_set_ctime_current(old_dir
));
872 inode_set_mtime_to_ts(new_dir
, inode_set_ctime_current(new_dir
));
873 mark_inode_dirty(old_dir
);
874 mark_inode_dirty(new_dir
);
877 diriter
.fi
.icb
.extLocation
=
878 cpu_to_lelb(UDF_I(new_dir
)->i_location
);
879 udf_fiiter_write_fi(&diriter
, NULL
);
880 udf_fiiter_release(&diriter
);
884 inode_dec_link_count(old_dir
);
886 inode_dec_link_count(new_inode
);
889 mark_inode_dirty(new_dir
);
895 udf_fiiter_release(&diriter
);
896 udf_fiiter_release(&oiter
);
901 static struct dentry
*udf_get_parent(struct dentry
*child
)
903 struct kernel_lb_addr tloc
;
904 struct udf_fileident_iter iter
;
907 err
= udf_fiiter_find_entry(d_inode(child
), &dotdot_name
, &iter
);
911 tloc
= lelb_to_cpu(iter
.fi
.icb
.extLocation
);
912 udf_fiiter_release(&iter
);
913 return d_obtain_alias(udf_iget(child
->d_sb
, &tloc
));
917 static struct dentry
*udf_nfs_get_inode(struct super_block
*sb
, u32 block
,
918 u16 partref
, __u32 generation
)
921 struct kernel_lb_addr loc
;
924 return ERR_PTR(-ESTALE
);
926 loc
.logicalBlockNum
= block
;
927 loc
.partitionReferenceNum
= partref
;
928 inode
= udf_iget(sb
, &loc
);
931 return ERR_CAST(inode
);
933 if (generation
&& inode
->i_generation
!= generation
) {
935 return ERR_PTR(-ESTALE
);
937 return d_obtain_alias(inode
);
940 static struct dentry
*udf_fh_to_dentry(struct super_block
*sb
,
941 struct fid
*fid
, int fh_len
, int fh_type
)
944 (fh_type
!= FILEID_UDF_WITH_PARENT
&&
945 fh_type
!= FILEID_UDF_WITHOUT_PARENT
))
948 return udf_nfs_get_inode(sb
, fid
->udf
.block
, fid
->udf
.partref
,
949 fid
->udf
.generation
);
952 static struct dentry
*udf_fh_to_parent(struct super_block
*sb
,
953 struct fid
*fid
, int fh_len
, int fh_type
)
955 if (fh_len
< 5 || fh_type
!= FILEID_UDF_WITH_PARENT
)
958 return udf_nfs_get_inode(sb
, fid
->udf
.parent_block
,
959 fid
->udf
.parent_partref
,
960 fid
->udf
.parent_generation
);
962 static int udf_encode_fh(struct inode
*inode
, __u32
*fh
, int *lenp
,
963 struct inode
*parent
)
966 struct kernel_lb_addr location
= UDF_I(inode
)->i_location
;
967 struct fid
*fid
= (struct fid
*)fh
;
968 int type
= FILEID_UDF_WITHOUT_PARENT
;
970 if (parent
&& (len
< 5)) {
972 return FILEID_INVALID
;
973 } else if (len
< 3) {
975 return FILEID_INVALID
;
979 fid
->udf
.block
= location
.logicalBlockNum
;
980 fid
->udf
.partref
= location
.partitionReferenceNum
;
981 fid
->udf
.parent_partref
= 0;
982 fid
->udf
.generation
= inode
->i_generation
;
985 location
= UDF_I(parent
)->i_location
;
986 fid
->udf
.parent_block
= location
.logicalBlockNum
;
987 fid
->udf
.parent_partref
= location
.partitionReferenceNum
;
988 fid
->udf
.parent_generation
= inode
->i_generation
;
990 type
= FILEID_UDF_WITH_PARENT
;
996 const struct export_operations udf_export_ops
= {
997 .encode_fh
= udf_encode_fh
,
998 .fh_to_dentry
= udf_fh_to_dentry
,
999 .fh_to_parent
= udf_fh_to_parent
,
1000 .get_parent
= udf_get_parent
,
1003 const struct inode_operations udf_dir_inode_operations
= {
1004 .lookup
= udf_lookup
,
1005 .create
= udf_create
,
1007 .unlink
= udf_unlink
,
1008 .symlink
= udf_symlink
,
1012 .rename
= udf_rename
,
1013 .tmpfile
= udf_tmpfile
,