5 * Inode name handling routines for the OSTA-UDF(tm) filesystem.
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.
13 * (C) 1998-2004 Ben Fennema
14 * (C) 1999-2000 Stelias Computing Inc
18 * 12/12/98 blf Created. Split out the lookup code from dir.c
19 * 04/19/99 blf link, mknod, symlink support
26 #include <linux/string.h>
27 #include <linux/errno.h>
29 #include <linux/slab.h>
30 #include <linux/quotaops.h>
31 #include <linux/smp_lock.h>
32 #include <linux/buffer_head.h>
33 #include <linux/sched.h>
35 static inline int udf_match(int len1
, const char *name1
, int len2
,
41 return !memcmp(name1
, name2
, len1
);
44 int udf_write_fi(struct inode
*inode
, struct fileIdentDesc
*cfi
,
45 struct fileIdentDesc
*sfi
, struct udf_fileident_bh
*fibh
,
46 uint8_t *impuse
, uint8_t *fileident
)
48 uint16_t crclen
= fibh
->eoffset
- fibh
->soffset
- sizeof(tag
);
51 uint16_t liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
52 uint8_t lfi
= cfi
->lengthFileIdent
;
53 int padlen
= fibh
->eoffset
- fibh
->soffset
- liu
- lfi
-
54 sizeof(struct fileIdentDesc
);
57 if (UDF_I_ALLOCTYPE(inode
) == ICBTAG_FLAG_AD_IN_ICB
)
60 offset
= fibh
->soffset
+ sizeof(struct fileIdentDesc
);
63 if (adinicb
|| (offset
+ liu
< 0)) {
64 memcpy((uint8_t *)sfi
->impUse
, impuse
, liu
);
65 } else if (offset
>= 0) {
66 memcpy(fibh
->ebh
->b_data
+ offset
, impuse
, liu
);
68 memcpy((uint8_t *)sfi
->impUse
, impuse
, -offset
);
69 memcpy(fibh
->ebh
->b_data
, impuse
- offset
,
77 if (adinicb
|| (offset
+ lfi
< 0)) {
78 memcpy((uint8_t *)sfi
->fileIdent
+ liu
, fileident
, lfi
);
79 } else if (offset
>= 0) {
80 memcpy(fibh
->ebh
->b_data
+ offset
, fileident
, lfi
);
82 memcpy((uint8_t *)sfi
->fileIdent
+ liu
, fileident
,
84 memcpy(fibh
->ebh
->b_data
, fileident
- offset
,
91 if (adinicb
|| (offset
+ padlen
< 0)) {
92 memset((uint8_t *)sfi
->padding
+ liu
+ lfi
, 0x00, padlen
);
93 } else if (offset
>= 0) {
94 memset(fibh
->ebh
->b_data
+ offset
, 0x00, padlen
);
96 memset((uint8_t *)sfi
->padding
+ liu
+ lfi
, 0x00, -offset
);
97 memset(fibh
->ebh
->b_data
, 0x00, padlen
+ offset
);
100 crc
= udf_crc((uint8_t *)cfi
+ sizeof(tag
),
101 sizeof(struct fileIdentDesc
) - sizeof(tag
), 0);
103 if (fibh
->sbh
== fibh
->ebh
) {
104 crc
= udf_crc((uint8_t *)sfi
->impUse
,
105 crclen
+ sizeof(tag
) -
106 sizeof(struct fileIdentDesc
), crc
);
107 } else if (sizeof(struct fileIdentDesc
) >= -fibh
->soffset
) {
108 crc
= udf_crc(fibh
->ebh
->b_data
+
109 sizeof(struct fileIdentDesc
) +
111 crclen
+ sizeof(tag
) -
112 sizeof(struct fileIdentDesc
),
115 crc
= udf_crc((uint8_t *)sfi
->impUse
,
116 -fibh
->soffset
- sizeof(struct fileIdentDesc
),
118 crc
= udf_crc(fibh
->ebh
->b_data
, fibh
->eoffset
, crc
);
121 cfi
->descTag
.descCRC
= cpu_to_le16(crc
);
122 cfi
->descTag
.descCRCLength
= cpu_to_le16(crclen
);
123 cfi
->descTag
.tagChecksum
= udf_tag_checksum(&cfi
->descTag
);
125 if (adinicb
|| (sizeof(struct fileIdentDesc
) <= -fibh
->soffset
)) {
126 memcpy((uint8_t *)sfi
, (uint8_t *)cfi
,
127 sizeof(struct fileIdentDesc
));
129 memcpy((uint8_t *)sfi
, (uint8_t *)cfi
, -fibh
->soffset
);
130 memcpy(fibh
->ebh
->b_data
, (uint8_t *)cfi
- fibh
->soffset
,
131 sizeof(struct fileIdentDesc
) + fibh
->soffset
);
135 mark_inode_dirty(inode
);
137 if (fibh
->sbh
!= fibh
->ebh
)
138 mark_buffer_dirty_inode(fibh
->ebh
, inode
);
139 mark_buffer_dirty_inode(fibh
->sbh
, inode
);
144 static struct fileIdentDesc
*udf_find_entry(struct inode
*dir
,
145 struct dentry
*dentry
,
146 struct udf_fileident_bh
*fibh
,
147 struct fileIdentDesc
*cfi
)
149 struct fileIdentDesc
*fi
= NULL
;
152 char fname
[UDF_NAME_LEN
];
160 struct extent_position epos
= {};
162 size
= (udf_ext0_offset(dir
) + dir
->i_size
) >> 2;
163 f_pos
= (udf_ext0_offset(dir
) >> 2);
165 fibh
->soffset
= fibh
->eoffset
=
166 (f_pos
& ((dir
->i_sb
->s_blocksize
- 1) >> 2)) << 2;
167 if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_IN_ICB
)
168 fibh
->sbh
= fibh
->ebh
= NULL
;
169 else if (inode_bmap(dir
, f_pos
>> (dir
->i_sb
->s_blocksize_bits
- 2),
170 &epos
, &eloc
, &elen
, &offset
) ==
171 (EXT_RECORDED_ALLOCATED
>> 30)) {
172 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, offset
);
173 if ((++offset
<< dir
->i_sb
->s_blocksize_bits
) < elen
) {
174 if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_SHORT
)
175 epos
.offset
-= sizeof(short_ad
);
176 else if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_LONG
)
177 epos
.offset
-= sizeof(long_ad
);
181 fibh
->sbh
= fibh
->ebh
= udf_tread(dir
->i_sb
, block
);
191 while ((f_pos
< size
)) {
192 fi
= udf_fileident_read(dir
, &f_pos
, fibh
, cfi
, &epos
, &eloc
,
195 if (fibh
->sbh
!= fibh
->ebh
)
202 liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
203 lfi
= cfi
->lengthFileIdent
;
205 if (fibh
->sbh
== fibh
->ebh
) {
206 nameptr
= fi
->fileIdent
+ liu
;
208 int poffset
; /* Unpaded ending offset */
210 poffset
= fibh
->soffset
+ sizeof(struct fileIdentDesc
) +
214 nameptr
= (uint8_t *)(fibh
->ebh
->b_data
+
218 memcpy(nameptr
, fi
->fileIdent
+ liu
,
220 memcpy(nameptr
+ lfi
- poffset
,
221 fibh
->ebh
->b_data
, poffset
);
225 if ((cfi
->fileCharacteristics
& FID_FILE_CHAR_DELETED
) != 0) {
226 if (!UDF_QUERY_FLAG(dir
->i_sb
, UDF_FLAG_UNDELETE
))
230 if ((cfi
->fileCharacteristics
& FID_FILE_CHAR_HIDDEN
) != 0) {
231 if (!UDF_QUERY_FLAG(dir
->i_sb
, UDF_FLAG_UNHIDE
))
238 flen
= udf_get_filename(dir
->i_sb
, nameptr
, fname
, lfi
);
239 if (flen
&& udf_match(flen
, fname
, dentry
->d_name
.len
,
240 dentry
->d_name
.name
)) {
246 if (fibh
->sbh
!= fibh
->ebh
)
258 * Look-up the inode for a given name.
261 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
262 * available for a directory. The filesystem is useless if this routine is
263 * not available for at least the filesystem's root directory.
265 * This routine is passed an incomplete dentry - it must be completed by
266 * calling d_add(dentry, inode). If the name does not exist, then the
267 * specified inode must be set to null. An error should only be returned
268 * when the lookup fails for a reason other than the name not existing.
269 * Note that the directory inode semaphore is held during the call.
271 * Refer to lookup_dentry() in fs/namei.c
272 * lookup_dentry() -> lookup() -> real_lookup() -> .
275 * dir Pointer to inode of parent directory.
276 * dentry Pointer to dentry to complete.
277 * nd Pointer to lookup nameidata
280 * <return> Zero on success.
283 * July 1, 1997 - Andrew E. Mileski
284 * Written, tested, and released.
287 static struct dentry
*udf_lookup(struct inode
*dir
, struct dentry
*dentry
,
288 struct nameidata
*nd
)
290 struct inode
*inode
= NULL
;
291 struct fileIdentDesc cfi
;
292 struct udf_fileident_bh fibh
;
294 if (dentry
->d_name
.len
> UDF_NAME_LEN
- 2)
295 return ERR_PTR(-ENAMETOOLONG
);
299 /* temporary shorthand for specifying files by inode number */
300 if (!strncmp(dentry
->d_name
.name
, ".B=", 3)) {
301 kernel_lb_addr lb
= {
302 .logicalBlockNum
= 0,
303 .partitionReferenceNum
=
304 simple_strtoul(dentry
->d_name
.name
+ 3,
307 inode
= udf_iget(dir
->i_sb
, lb
);
310 return ERR_PTR(-EACCES
);
313 #endif /* UDF_RECOVERY */
315 if (udf_find_entry(dir
, dentry
, &fibh
, &cfi
)) {
316 if (fibh
.sbh
!= fibh
.ebh
)
320 inode
= udf_iget(dir
->i_sb
, lelb_to_cpu(cfi
.icb
.extLocation
));
323 return ERR_PTR(-EACCES
);
327 d_add(dentry
, inode
);
332 static struct fileIdentDesc
*udf_add_entry(struct inode
*dir
,
333 struct dentry
*dentry
,
334 struct udf_fileident_bh
*fibh
,
335 struct fileIdentDesc
*cfi
, int *err
)
337 struct super_block
*sb
= dir
->i_sb
;
338 struct fileIdentDesc
*fi
= NULL
;
339 char name
[UDF_NAME_LEN
], fname
[UDF_NAME_LEN
];
344 loff_t size
= (udf_ext0_offset(dir
) + dir
->i_size
) >> 2;
352 struct extent_position epos
= {};
355 if (!dentry
->d_name
.len
) {
359 namelen
= udf_put_filename(sb
, dentry
->d_name
.name
, name
,
362 *err
= -ENAMETOOLONG
;
369 nfidlen
= (sizeof(struct fileIdentDesc
) + namelen
+ 3) & ~3;
371 f_pos
= (udf_ext0_offset(dir
) >> 2);
373 fibh
->soffset
= fibh
->eoffset
=
374 (f_pos
& ((dir
->i_sb
->s_blocksize
- 1) >> 2)) << 2;
375 if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_IN_ICB
)
376 fibh
->sbh
= fibh
->ebh
= NULL
;
377 else if (inode_bmap(dir
, f_pos
>> (dir
->i_sb
->s_blocksize_bits
- 2),
378 &epos
, &eloc
, &elen
, &offset
) ==
379 (EXT_RECORDED_ALLOCATED
>> 30)) {
380 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, offset
);
381 if ((++offset
<< dir
->i_sb
->s_blocksize_bits
) < elen
) {
382 if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_SHORT
)
383 epos
.offset
-= sizeof(short_ad
);
384 else if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_LONG
)
385 epos
.offset
-= sizeof(long_ad
);
389 fibh
->sbh
= fibh
->ebh
= udf_tread(dir
->i_sb
, block
);
396 block
= UDF_I_LOCATION(dir
).logicalBlockNum
;
399 block
= udf_get_lb_pblock(dir
->i_sb
, UDF_I_LOCATION(dir
), 0);
400 fibh
->sbh
= fibh
->ebh
= NULL
;
401 fibh
->soffset
= fibh
->eoffset
= sb
->s_blocksize
;
405 while ((f_pos
< size
)) {
406 fi
= udf_fileident_read(dir
, &f_pos
, fibh
, cfi
, &epos
, &eloc
,
410 if (fibh
->sbh
!= fibh
->ebh
)
418 liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
419 lfi
= cfi
->lengthFileIdent
;
421 if (fibh
->sbh
== fibh
->ebh
)
422 nameptr
= fi
->fileIdent
+ liu
;
424 int poffset
; /* Unpaded ending offset */
426 poffset
= fibh
->soffset
+ sizeof(struct fileIdentDesc
) +
430 nameptr
= (char *)(fibh
->ebh
->b_data
+
434 memcpy(nameptr
, fi
->fileIdent
+ liu
,
436 memcpy(nameptr
+ lfi
- poffset
,
437 fibh
->ebh
->b_data
, poffset
);
441 if ((cfi
->fileCharacteristics
& FID_FILE_CHAR_DELETED
) != 0) {
442 if (((sizeof(struct fileIdentDesc
) +
443 liu
+ lfi
+ 3) & ~3) == nfidlen
) {
445 cfi
->descTag
.tagSerialNum
= cpu_to_le16(1);
446 cfi
->fileVersionNum
= cpu_to_le16(1);
447 cfi
->fileCharacteristics
= 0;
448 cfi
->lengthFileIdent
= namelen
;
449 cfi
->lengthOfImpUse
= cpu_to_le16(0);
450 if (!udf_write_fi(dir
, cfi
, fi
, fibh
, NULL
,
463 flen
= udf_get_filename(dir
->i_sb
, nameptr
, fname
, lfi
);
464 if (flen
&& udf_match(flen
, fname
, dentry
->d_name
.len
,
465 dentry
->d_name
.name
)) {
466 if (fibh
->sbh
!= fibh
->ebh
)
478 if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_IN_ICB
&&
479 sb
->s_blocksize
- fibh
->eoffset
< nfidlen
) {
482 fibh
->soffset
-= udf_ext0_offset(dir
);
483 fibh
->eoffset
-= udf_ext0_offset(dir
);
484 f_pos
-= (udf_ext0_offset(dir
) >> 2);
485 if (fibh
->sbh
!= fibh
->ebh
)
488 fibh
->sbh
= fibh
->ebh
=
489 udf_expand_dir_adinicb(dir
, &block
, err
);
492 epos
.block
= UDF_I_LOCATION(dir
);
493 eloc
.logicalBlockNum
= block
;
494 eloc
.partitionReferenceNum
=
495 UDF_I_LOCATION(dir
).partitionReferenceNum
;
496 elen
= dir
->i_sb
->s_blocksize
;
497 epos
.offset
= udf_file_entry_alloc_offset(dir
);
498 if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_SHORT
)
499 epos
.offset
+= sizeof(short_ad
);
500 else if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_LONG
)
501 epos
.offset
+= sizeof(long_ad
);
504 if (sb
->s_blocksize
- fibh
->eoffset
>= nfidlen
) {
505 fibh
->soffset
= fibh
->eoffset
;
506 fibh
->eoffset
+= nfidlen
;
507 if (fibh
->sbh
!= fibh
->ebh
) {
509 fibh
->sbh
= fibh
->ebh
;
512 if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_IN_ICB
) {
513 block
= UDF_I_LOCATION(dir
).logicalBlockNum
;
514 fi
= (struct fileIdentDesc
*)
515 (UDF_I_DATA(dir
) + fibh
->soffset
-
516 udf_ext0_offset(dir
) +
517 UDF_I_LENEATTR(dir
));
519 block
= eloc
.logicalBlockNum
+
521 dir
->i_sb
->s_blocksize_bits
);
522 fi
= (struct fileIdentDesc
*)
523 (fibh
->sbh
->b_data
+ fibh
->soffset
);
526 fibh
->soffset
= fibh
->eoffset
- sb
->s_blocksize
;
527 fibh
->eoffset
+= nfidlen
- sb
->s_blocksize
;
528 if (fibh
->sbh
!= fibh
->ebh
) {
530 fibh
->sbh
= fibh
->ebh
;
533 block
= eloc
.logicalBlockNum
+ ((elen
- 1) >>
534 dir
->i_sb
->s_blocksize_bits
);
535 fibh
->ebh
= udf_bread(dir
,
536 f_pos
>> (dir
->i_sb
->s_blocksize_bits
- 2),
544 if (!fibh
->soffset
) {
545 if (udf_next_aext(dir
, &epos
, &eloc
, &elen
, 1) ==
546 (EXT_RECORDED_ALLOCATED
>> 30)) {
547 block
= eloc
.logicalBlockNum
+ ((elen
- 1) >>
548 dir
->i_sb
->s_blocksize_bits
);
553 fibh
->sbh
= fibh
->ebh
;
554 fi
= (struct fileIdentDesc
*)(fibh
->sbh
->b_data
);
556 fi
= (struct fileIdentDesc
*)
557 (fibh
->sbh
->b_data
+ sb
->s_blocksize
+
562 memset(cfi
, 0, sizeof(struct fileIdentDesc
));
563 if (UDF_SB(sb
)->s_udfrev
>= 0x0200)
564 udf_new_tag((char *)cfi
, TAG_IDENT_FID
, 3, 1, block
,
567 udf_new_tag((char *)cfi
, TAG_IDENT_FID
, 2, 1, block
,
569 cfi
->fileVersionNum
= cpu_to_le16(1);
570 cfi
->lengthFileIdent
= namelen
;
571 cfi
->lengthOfImpUse
= cpu_to_le16(0);
572 if (!udf_write_fi(dir
, cfi
, fi
, fibh
, NULL
, name
)) {
574 dir
->i_size
+= nfidlen
;
575 if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_IN_ICB
)
576 UDF_I_LENALLOC(dir
) += nfidlen
;
577 mark_inode_dirty(dir
);
581 if (fibh
->sbh
!= fibh
->ebh
)
589 static int udf_delete_entry(struct inode
*inode
, struct fileIdentDesc
*fi
,
590 struct udf_fileident_bh
*fibh
,
591 struct fileIdentDesc
*cfi
)
593 cfi
->fileCharacteristics
|= FID_FILE_CHAR_DELETED
;
595 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
))
596 memset(&(cfi
->icb
), 0x00, sizeof(long_ad
));
598 return udf_write_fi(inode
, cfi
, fi
, fibh
, NULL
, NULL
);
601 static int udf_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
602 struct nameidata
*nd
)
604 struct udf_fileident_bh fibh
;
606 struct fileIdentDesc cfi
, *fi
;
610 inode
= udf_new_inode(dir
, mode
, &err
);
616 if (UDF_I_ALLOCTYPE(inode
) == ICBTAG_FLAG_AD_IN_ICB
)
617 inode
->i_data
.a_ops
= &udf_adinicb_aops
;
619 inode
->i_data
.a_ops
= &udf_aops
;
620 inode
->i_op
= &udf_file_inode_operations
;
621 inode
->i_fop
= &udf_file_operations
;
622 inode
->i_mode
= mode
;
623 mark_inode_dirty(inode
);
625 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
628 mark_inode_dirty(inode
);
633 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
634 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
635 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
636 cpu_to_le32(UDF_I_UNIQUE(inode
) & 0x00000000FFFFFFFFUL
);
637 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
638 if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_IN_ICB
)
639 mark_inode_dirty(dir
);
640 if (fibh
.sbh
!= fibh
.ebh
)
644 d_instantiate(dentry
, inode
);
649 static int udf_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
,
653 struct udf_fileident_bh fibh
;
654 struct fileIdentDesc cfi
, *fi
;
657 if (!old_valid_dev(rdev
))
662 inode
= udf_new_inode(dir
, mode
, &err
);
666 inode
->i_uid
= current
->fsuid
;
667 init_special_inode(inode
, mode
, rdev
);
668 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
671 mark_inode_dirty(inode
);
676 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
677 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
678 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
679 cpu_to_le32(UDF_I_UNIQUE(inode
) & 0x00000000FFFFFFFFUL
);
680 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
681 if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_IN_ICB
)
682 mark_inode_dirty(dir
);
683 mark_inode_dirty(inode
);
685 if (fibh
.sbh
!= fibh
.ebh
)
688 d_instantiate(dentry
, inode
);
696 static int udf_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
699 struct udf_fileident_bh fibh
;
700 struct fileIdentDesc cfi
, *fi
;
705 if (dir
->i_nlink
>= (256 << sizeof(dir
->i_nlink
)) - 1)
709 inode
= udf_new_inode(dir
, S_IFDIR
, &err
);
713 inode
->i_op
= &udf_dir_inode_operations
;
714 inode
->i_fop
= &udf_dir_operations
;
715 fi
= udf_add_entry(inode
, NULL
, &fibh
, &cfi
, &err
);
718 mark_inode_dirty(inode
);
723 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
724 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(dir
));
725 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
726 cpu_to_le32(UDF_I_UNIQUE(dir
) & 0x00000000FFFFFFFFUL
);
727 cfi
.fileCharacteristics
=
728 FID_FILE_CHAR_DIRECTORY
| FID_FILE_CHAR_PARENT
;
729 udf_write_fi(inode
, &cfi
, fi
, &fibh
, NULL
, NULL
);
731 inode
->i_mode
= S_IFDIR
| mode
;
732 if (dir
->i_mode
& S_ISGID
)
733 inode
->i_mode
|= S_ISGID
;
734 mark_inode_dirty(inode
);
736 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
739 mark_inode_dirty(inode
);
743 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
744 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
745 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
746 cpu_to_le32(UDF_I_UNIQUE(inode
) & 0x00000000FFFFFFFFUL
);
747 cfi
.fileCharacteristics
|= FID_FILE_CHAR_DIRECTORY
;
748 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
750 mark_inode_dirty(dir
);
751 d_instantiate(dentry
, inode
);
752 if (fibh
.sbh
!= fibh
.ebh
)
762 static int empty_dir(struct inode
*dir
)
764 struct fileIdentDesc
*fi
, cfi
;
765 struct udf_fileident_bh fibh
;
767 loff_t size
= (udf_ext0_offset(dir
) + dir
->i_size
) >> 2;
772 struct extent_position epos
= {};
774 f_pos
= (udf_ext0_offset(dir
) >> 2);
776 fibh
.soffset
= fibh
.eoffset
=
777 (f_pos
& ((dir
->i_sb
->s_blocksize
- 1) >> 2)) << 2;
779 if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_IN_ICB
)
780 fibh
.sbh
= fibh
.ebh
= NULL
;
781 else if (inode_bmap(dir
, f_pos
>> (dir
->i_sb
->s_blocksize_bits
- 2),
782 &epos
, &eloc
, &elen
, &offset
) ==
783 (EXT_RECORDED_ALLOCATED
>> 30)) {
784 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, offset
);
785 if ((++offset
<< dir
->i_sb
->s_blocksize_bits
) < elen
) {
786 if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_SHORT
)
787 epos
.offset
-= sizeof(short_ad
);
788 else if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_LONG
)
789 epos
.offset
-= sizeof(long_ad
);
793 fibh
.sbh
= fibh
.ebh
= udf_tread(dir
->i_sb
, block
);
803 while ((f_pos
< size
)) {
804 fi
= udf_fileident_read(dir
, &f_pos
, &fibh
, &cfi
, &epos
, &eloc
,
807 if (fibh
.sbh
!= fibh
.ebh
)
814 if (cfi
.lengthFileIdent
&&
815 (cfi
.fileCharacteristics
& FID_FILE_CHAR_DELETED
) == 0) {
816 if (fibh
.sbh
!= fibh
.ebh
)
824 if (fibh
.sbh
!= fibh
.ebh
)
832 static int udf_rmdir(struct inode
*dir
, struct dentry
*dentry
)
835 struct inode
*inode
= dentry
->d_inode
;
836 struct udf_fileident_bh fibh
;
837 struct fileIdentDesc
*fi
, cfi
;
842 fi
= udf_find_entry(dir
, dentry
, &fibh
, &cfi
);
847 tloc
= lelb_to_cpu(cfi
.icb
.extLocation
);
848 if (udf_get_lb_pblock(dir
->i_sb
, tloc
, 0) != inode
->i_ino
)
851 if (!empty_dir(inode
))
853 retval
= udf_delete_entry(dir
, fi
, &fibh
, &cfi
);
856 if (inode
->i_nlink
!= 2)
857 udf_warning(inode
->i_sb
, "udf_rmdir",
858 "empty directory has nlink != 2 (%d)",
862 inode_dec_link_count(dir
);
863 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
=
864 current_fs_time(dir
->i_sb
);
865 mark_inode_dirty(dir
);
868 if (fibh
.sbh
!= fibh
.ebh
)
877 static int udf_unlink(struct inode
*dir
, struct dentry
*dentry
)
880 struct inode
*inode
= dentry
->d_inode
;
881 struct udf_fileident_bh fibh
;
882 struct fileIdentDesc
*fi
;
883 struct fileIdentDesc cfi
;
888 fi
= udf_find_entry(dir
, dentry
, &fibh
, &cfi
);
893 tloc
= lelb_to_cpu(cfi
.icb
.extLocation
);
894 if (udf_get_lb_pblock(dir
->i_sb
, tloc
, 0) != inode
->i_ino
)
897 if (!inode
->i_nlink
) {
898 udf_debug("Deleting nonexistent file (%lu), %d\n",
899 inode
->i_ino
, inode
->i_nlink
);
902 retval
= udf_delete_entry(dir
, fi
, &fibh
, &cfi
);
905 dir
->i_ctime
= dir
->i_mtime
= current_fs_time(dir
->i_sb
);
906 mark_inode_dirty(dir
);
907 inode_dec_link_count(inode
);
908 inode
->i_ctime
= dir
->i_ctime
;
912 if (fibh
.sbh
!= fibh
.ebh
)
921 static int udf_symlink(struct inode
*dir
, struct dentry
*dentry
,
925 struct pathComponent
*pc
;
927 struct udf_fileident_bh fibh
;
928 struct extent_position epos
= {};
929 int eoffset
, elen
= 0;
930 struct fileIdentDesc
*fi
;
931 struct fileIdentDesc cfi
;
935 char name
[UDF_NAME_LEN
];
937 struct buffer_head
*bh
;
940 inode
= udf_new_inode(dir
, S_IFLNK
, &err
);
944 inode
->i_mode
= S_IFLNK
| S_IRWXUGO
;
945 inode
->i_data
.a_ops
= &udf_symlink_aops
;
946 inode
->i_op
= &page_symlink_inode_operations
;
948 if (UDF_I_ALLOCTYPE(inode
) != ICBTAG_FLAG_AD_IN_ICB
) {
952 block
= udf_new_block(inode
->i_sb
, inode
,
953 UDF_I_LOCATION(inode
).partitionReferenceNum
,
954 UDF_I_LOCATION(inode
).logicalBlockNum
, &err
);
957 epos
.block
= UDF_I_LOCATION(inode
);
958 epos
.offset
= udf_file_entry_alloc_offset(inode
);
960 eloc
.logicalBlockNum
= block
;
961 eloc
.partitionReferenceNum
=
962 UDF_I_LOCATION(inode
).partitionReferenceNum
;
963 elen
= inode
->i_sb
->s_blocksize
;
964 UDF_I_LENEXTENTS(inode
) = elen
;
965 udf_add_aext(inode
, &epos
, eloc
, elen
, 0);
968 block
= udf_get_pblock(inode
->i_sb
, block
,
969 UDF_I_LOCATION(inode
).partitionReferenceNum
,
971 epos
.bh
= udf_tread(inode
->i_sb
, block
);
972 lock_buffer(epos
.bh
);
973 memset(epos
.bh
->b_data
, 0x00, inode
->i_sb
->s_blocksize
);
974 set_buffer_uptodate(epos
.bh
);
975 unlock_buffer(epos
.bh
);
976 mark_buffer_dirty_inode(epos
.bh
, inode
);
977 ea
= epos
.bh
->b_data
+ udf_ext0_offset(inode
);
979 ea
= UDF_I_DATA(inode
) + UDF_I_LENEATTR(inode
);
982 eoffset
= inode
->i_sb
->s_blocksize
- udf_ext0_offset(inode
);
983 pc
= (struct pathComponent
*)ea
;
985 if (*symname
== '/') {
988 } while (*symname
== '/');
990 pc
->componentType
= 1;
991 pc
->lengthComponentIdent
= 0;
992 pc
->componentFileVersionNum
= 0;
993 pc
+= sizeof(struct pathComponent
);
994 elen
+= sizeof(struct pathComponent
);
1000 if (elen
+ sizeof(struct pathComponent
) > eoffset
)
1003 pc
= (struct pathComponent
*)(ea
+ elen
);
1005 compstart
= (char *)symname
;
1009 } while (*symname
&& *symname
!= '/');
1011 pc
->componentType
= 5;
1012 pc
->lengthComponentIdent
= 0;
1013 pc
->componentFileVersionNum
= 0;
1014 if (compstart
[0] == '.') {
1015 if ((symname
- compstart
) == 1)
1016 pc
->componentType
= 4;
1017 else if ((symname
- compstart
) == 2 &&
1018 compstart
[1] == '.')
1019 pc
->componentType
= 3;
1022 if (pc
->componentType
== 5) {
1023 namelen
= udf_put_filename(inode
->i_sb
, compstart
, name
,
1024 symname
- compstart
);
1028 if (elen
+ sizeof(struct pathComponent
) + namelen
>
1032 pc
->lengthComponentIdent
= namelen
;
1034 memcpy(pc
->componentIdent
, name
, namelen
);
1037 elen
+= sizeof(struct pathComponent
) + pc
->lengthComponentIdent
;
1042 } while (*symname
== '/');
1047 inode
->i_size
= elen
;
1048 if (UDF_I_ALLOCTYPE(inode
) == ICBTAG_FLAG_AD_IN_ICB
)
1049 UDF_I_LENALLOC(inode
) = inode
->i_size
;
1050 mark_inode_dirty(inode
);
1052 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
1055 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
1056 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
1057 bh
= UDF_SB(inode
->i_sb
)->s_lvid_bh
;
1059 struct logicalVolIntegrityDesc
*lvid
=
1060 (struct logicalVolIntegrityDesc
*)bh
->b_data
;
1061 struct logicalVolHeaderDesc
*lvhd
;
1063 lvhd
= (struct logicalVolHeaderDesc
*)
1064 lvid
->logicalVolContentsUse
;
1065 uniqueID
= le64_to_cpu(lvhd
->uniqueID
);
1066 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
1067 cpu_to_le32(uniqueID
& 0x00000000FFFFFFFFUL
);
1068 if (!(++uniqueID
& 0x00000000FFFFFFFFUL
))
1070 lvhd
->uniqueID
= cpu_to_le64(uniqueID
);
1071 mark_buffer_dirty(bh
);
1073 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
1074 if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_IN_ICB
)
1075 mark_inode_dirty(dir
);
1076 if (fibh
.sbh
!= fibh
.ebh
)
1079 d_instantiate(dentry
, inode
);
1087 inode_dec_link_count(inode
);
1092 static int udf_link(struct dentry
*old_dentry
, struct inode
*dir
,
1093 struct dentry
*dentry
)
1095 struct inode
*inode
= old_dentry
->d_inode
;
1096 struct udf_fileident_bh fibh
;
1097 struct fileIdentDesc cfi
, *fi
;
1099 struct buffer_head
*bh
;
1102 if (inode
->i_nlink
>= (256 << sizeof(inode
->i_nlink
)) - 1) {
1107 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
1112 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
1113 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
1114 bh
= UDF_SB(inode
->i_sb
)->s_lvid_bh
;
1116 struct logicalVolIntegrityDesc
*lvid
=
1117 (struct logicalVolIntegrityDesc
*)bh
->b_data
;
1118 struct logicalVolHeaderDesc
*lvhd
;
1120 lvhd
= (struct logicalVolHeaderDesc
*)
1121 (lvid
->logicalVolContentsUse
);
1122 uniqueID
= le64_to_cpu(lvhd
->uniqueID
);
1123 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
1124 cpu_to_le32(uniqueID
& 0x00000000FFFFFFFFUL
);
1125 if (!(++uniqueID
& 0x00000000FFFFFFFFUL
))
1127 lvhd
->uniqueID
= cpu_to_le64(uniqueID
);
1128 mark_buffer_dirty(bh
);
1130 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
1131 if (UDF_I_ALLOCTYPE(dir
) == ICBTAG_FLAG_AD_IN_ICB
)
1132 mark_inode_dirty(dir
);
1134 if (fibh
.sbh
!= fibh
.ebh
)
1138 inode
->i_ctime
= current_fs_time(inode
->i_sb
);
1139 mark_inode_dirty(inode
);
1140 atomic_inc(&inode
->i_count
);
1141 d_instantiate(dentry
, inode
);
1147 /* Anybody can rename anything with this: the permission checks are left to the
1148 * higher-level routines.
1150 static int udf_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1151 struct inode
*new_dir
, struct dentry
*new_dentry
)
1153 struct inode
*old_inode
= old_dentry
->d_inode
;
1154 struct inode
*new_inode
= new_dentry
->d_inode
;
1155 struct udf_fileident_bh ofibh
, nfibh
;
1156 struct fileIdentDesc
*ofi
= NULL
, *nfi
= NULL
, *dir_fi
= NULL
;
1157 struct fileIdentDesc ocfi
, ncfi
;
1158 struct buffer_head
*dir_bh
= NULL
;
1159 int retval
= -ENOENT
;
1160 kernel_lb_addr tloc
;
1163 ofi
= udf_find_entry(old_dir
, old_dentry
, &ofibh
, &ocfi
);
1165 if (ofibh
.sbh
!= ofibh
.ebh
)
1169 tloc
= lelb_to_cpu(ocfi
.icb
.extLocation
);
1170 if (!ofi
|| udf_get_lb_pblock(old_dir
->i_sb
, tloc
, 0)
1171 != old_inode
->i_ino
)
1174 nfi
= udf_find_entry(new_dir
, new_dentry
, &nfibh
, &ncfi
);
1177 if (nfibh
.sbh
!= nfibh
.ebh
)
1183 if (S_ISDIR(old_inode
->i_mode
)) {
1184 uint32_t offset
= udf_ext0_offset(old_inode
);
1187 retval
= -ENOTEMPTY
;
1188 if (!empty_dir(new_inode
))
1192 if (UDF_I_ALLOCTYPE(old_inode
) == ICBTAG_FLAG_AD_IN_ICB
) {
1193 dir_fi
= udf_get_fileident(
1194 UDF_I_DATA(old_inode
) -
1195 (UDF_I_EFE(old_inode
) ?
1196 sizeof(struct extendedFileEntry
) :
1197 sizeof(struct fileEntry
)),
1198 old_inode
->i_sb
->s_blocksize
, &offset
);
1200 dir_bh
= udf_bread(old_inode
, 0, 0, &retval
);
1203 dir_fi
= udf_get_fileident(dir_bh
->b_data
,
1204 old_inode
->i_sb
->s_blocksize
, &offset
);
1208 tloc
= lelb_to_cpu(dir_fi
->icb
.extLocation
);
1209 if (udf_get_lb_pblock(old_inode
->i_sb
, tloc
, 0) !=
1216 (256 << sizeof(new_dir
->i_nlink
)) - 1)
1220 nfi
= udf_add_entry(new_dir
, new_dentry
, &nfibh
, &ncfi
,
1227 * Like most other Unix systems, set the ctime for inodes on a
1230 old_inode
->i_ctime
= current_fs_time(old_inode
->i_sb
);
1231 mark_inode_dirty(old_inode
);
1236 ncfi
.fileVersionNum
= ocfi
.fileVersionNum
;
1237 ncfi
.fileCharacteristics
= ocfi
.fileCharacteristics
;
1238 memcpy(&(ncfi
.icb
), &(ocfi
.icb
), sizeof(long_ad
));
1239 udf_write_fi(new_dir
, &ncfi
, nfi
, &nfibh
, NULL
, NULL
);
1241 /* The old fid may have moved - find it again */
1242 ofi
= udf_find_entry(old_dir
, old_dentry
, &ofibh
, &ocfi
);
1243 udf_delete_entry(old_dir
, ofi
, &ofibh
, &ocfi
);
1246 new_inode
->i_ctime
= current_fs_time(new_inode
->i_sb
);
1247 inode_dec_link_count(new_inode
);
1249 old_dir
->i_ctime
= old_dir
->i_mtime
= current_fs_time(old_dir
->i_sb
);
1250 mark_inode_dirty(old_dir
);
1253 dir_fi
->icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(new_dir
));
1254 udf_update_tag((char *)dir_fi
,
1255 (sizeof(struct fileIdentDesc
) +
1256 le16_to_cpu(dir_fi
->lengthOfImpUse
) + 3) & ~3);
1257 if (UDF_I_ALLOCTYPE(old_inode
) == ICBTAG_FLAG_AD_IN_ICB
)
1258 mark_inode_dirty(old_inode
);
1260 mark_buffer_dirty_inode(dir_bh
, old_inode
);
1262 inode_dec_link_count(old_dir
);
1264 inode_dec_link_count(new_inode
);
1267 mark_inode_dirty(new_dir
);
1272 if (ofibh
.sbh
!= ofibh
.ebh
)
1282 if (nfibh
.sbh
!= nfibh
.ebh
)
1291 const struct inode_operations udf_dir_inode_operations
= {
1292 .lookup
= udf_lookup
,
1293 .create
= udf_create
,
1295 .unlink
= udf_unlink
,
1296 .symlink
= udf_symlink
,
1300 .rename
= udf_rename
,