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>
34 #include <linux/crc-itu-t.h>
36 static inline int udf_match(int len1
, const char *name1
, int len2
,
42 return !memcmp(name1
, name2
, len1
);
45 int udf_write_fi(struct inode
*inode
, struct fileIdentDesc
*cfi
,
46 struct fileIdentDesc
*sfi
, struct udf_fileident_bh
*fibh
,
47 uint8_t *impuse
, uint8_t *fileident
)
49 uint16_t crclen
= fibh
->eoffset
- fibh
->soffset
- sizeof(tag
);
52 uint16_t liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
53 uint8_t lfi
= cfi
->lengthFileIdent
;
54 int padlen
= fibh
->eoffset
- fibh
->soffset
- liu
- lfi
-
55 sizeof(struct fileIdentDesc
);
58 if (UDF_I(inode
)->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
61 offset
= fibh
->soffset
+ sizeof(struct fileIdentDesc
);
64 if (adinicb
|| (offset
+ liu
< 0)) {
65 memcpy((uint8_t *)sfi
->impUse
, impuse
, liu
);
66 } else if (offset
>= 0) {
67 memcpy(fibh
->ebh
->b_data
+ offset
, impuse
, liu
);
69 memcpy((uint8_t *)sfi
->impUse
, impuse
, -offset
);
70 memcpy(fibh
->ebh
->b_data
, impuse
- offset
,
78 if (adinicb
|| (offset
+ lfi
< 0)) {
79 memcpy((uint8_t *)sfi
->fileIdent
+ liu
, fileident
, lfi
);
80 } else if (offset
>= 0) {
81 memcpy(fibh
->ebh
->b_data
+ offset
, fileident
, lfi
);
83 memcpy((uint8_t *)sfi
->fileIdent
+ liu
, fileident
,
85 memcpy(fibh
->ebh
->b_data
, fileident
- offset
,
92 if (adinicb
|| (offset
+ padlen
< 0)) {
93 memset((uint8_t *)sfi
->padding
+ liu
+ lfi
, 0x00, padlen
);
94 } else if (offset
>= 0) {
95 memset(fibh
->ebh
->b_data
+ offset
, 0x00, padlen
);
97 memset((uint8_t *)sfi
->padding
+ liu
+ lfi
, 0x00, -offset
);
98 memset(fibh
->ebh
->b_data
, 0x00, padlen
+ offset
);
101 crc
= crc_itu_t(0, (uint8_t *)cfi
+ sizeof(tag
),
102 sizeof(struct fileIdentDesc
) - sizeof(tag
));
104 if (fibh
->sbh
== fibh
->ebh
) {
105 crc
= crc_itu_t(crc
, (uint8_t *)sfi
->impUse
,
106 crclen
+ sizeof(tag
) -
107 sizeof(struct fileIdentDesc
));
108 } else if (sizeof(struct fileIdentDesc
) >= -fibh
->soffset
) {
109 crc
= crc_itu_t(crc
, fibh
->ebh
->b_data
+
110 sizeof(struct fileIdentDesc
) +
112 crclen
+ sizeof(tag
) -
113 sizeof(struct fileIdentDesc
));
115 crc
= crc_itu_t(crc
, (uint8_t *)sfi
->impUse
,
116 -fibh
->soffset
- sizeof(struct fileIdentDesc
));
117 crc
= crc_itu_t(crc
, fibh
->ebh
->b_data
, fibh
->eoffset
);
120 cfi
->descTag
.descCRC
= cpu_to_le16(crc
);
121 cfi
->descTag
.descCRCLength
= cpu_to_le16(crclen
);
122 cfi
->descTag
.tagChecksum
= udf_tag_checksum(&cfi
->descTag
);
124 if (adinicb
|| (sizeof(struct fileIdentDesc
) <= -fibh
->soffset
)) {
125 memcpy((uint8_t *)sfi
, (uint8_t *)cfi
,
126 sizeof(struct fileIdentDesc
));
128 memcpy((uint8_t *)sfi
, (uint8_t *)cfi
, -fibh
->soffset
);
129 memcpy(fibh
->ebh
->b_data
, (uint8_t *)cfi
- fibh
->soffset
,
130 sizeof(struct fileIdentDesc
) + fibh
->soffset
);
134 mark_inode_dirty(inode
);
136 if (fibh
->sbh
!= fibh
->ebh
)
137 mark_buffer_dirty_inode(fibh
->ebh
, inode
);
138 mark_buffer_dirty_inode(fibh
->sbh
, inode
);
143 static struct fileIdentDesc
*udf_find_entry(struct inode
*dir
,
144 struct dentry
*dentry
,
145 struct udf_fileident_bh
*fibh
,
146 struct fileIdentDesc
*cfi
)
148 struct fileIdentDesc
*fi
= NULL
;
159 struct extent_position epos
= {};
160 struct udf_inode_info
*dinfo
= UDF_I(dir
);
162 size
= udf_ext0_offset(dir
) + dir
->i_size
;
163 f_pos
= udf_ext0_offset(dir
);
165 fibh
->sbh
= fibh
->ebh
= NULL
;
166 fibh
->soffset
= fibh
->eoffset
= f_pos
& (dir
->i_sb
->s_blocksize
- 1);
167 if (dinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_IN_ICB
) {
168 if (inode_bmap(dir
, f_pos
>> dir
->i_sb
->s_blocksize_bits
, &epos
,
169 &eloc
, &elen
, &offset
) != (EXT_RECORDED_ALLOCATED
>> 30))
171 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, offset
);
172 if ((++offset
<< dir
->i_sb
->s_blocksize_bits
) < elen
) {
173 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
174 epos
.offset
-= sizeof(short_ad
);
175 else if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
176 epos
.offset
-= sizeof(long_ad
);
180 fibh
->sbh
= fibh
->ebh
= udf_tread(dir
->i_sb
, block
);
185 fname
= kmalloc(UDF_NAME_LEN
, GFP_NOFS
);
189 while (f_pos
< size
) {
190 fi
= udf_fileident_read(dir
, &f_pos
, fibh
, cfi
, &epos
, &eloc
,
195 liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
196 lfi
= cfi
->lengthFileIdent
;
198 if (fibh
->sbh
== fibh
->ebh
) {
199 nameptr
= fi
->fileIdent
+ liu
;
201 int poffset
; /* Unpaded ending offset */
203 poffset
= fibh
->soffset
+ sizeof(struct fileIdentDesc
) +
207 nameptr
= (uint8_t *)(fibh
->ebh
->b_data
+
211 memcpy(nameptr
, fi
->fileIdent
+ liu
,
213 memcpy(nameptr
+ lfi
- poffset
,
214 fibh
->ebh
->b_data
, poffset
);
218 if ((cfi
->fileCharacteristics
& FID_FILE_CHAR_DELETED
) != 0) {
219 if (!UDF_QUERY_FLAG(dir
->i_sb
, UDF_FLAG_UNDELETE
))
223 if ((cfi
->fileCharacteristics
& FID_FILE_CHAR_HIDDEN
) != 0) {
224 if (!UDF_QUERY_FLAG(dir
->i_sb
, UDF_FLAG_UNHIDE
))
231 flen
= udf_get_filename(dir
->i_sb
, nameptr
, fname
, lfi
);
232 if (flen
&& udf_match(flen
, fname
, dentry
->d_name
.len
,
233 dentry
->d_name
.name
))
239 if (fibh
->sbh
!= fibh
->ebh
)
249 static struct dentry
*udf_lookup(struct inode
*dir
, struct dentry
*dentry
,
250 struct nameidata
*nd
)
252 struct inode
*inode
= NULL
;
253 struct fileIdentDesc cfi
;
254 struct udf_fileident_bh fibh
;
256 if (dentry
->d_name
.len
> UDF_NAME_LEN
- 2)
257 return ERR_PTR(-ENAMETOOLONG
);
261 /* temporary shorthand for specifying files by inode number */
262 if (!strncmp(dentry
->d_name
.name
, ".B=", 3)) {
263 kernel_lb_addr lb
= {
264 .logicalBlockNum
= 0,
265 .partitionReferenceNum
=
266 simple_strtoul(dentry
->d_name
.name
+ 3,
269 inode
= udf_iget(dir
->i_sb
, lb
);
272 return ERR_PTR(-EACCES
);
275 #endif /* UDF_RECOVERY */
277 if (udf_find_entry(dir
, dentry
, &fibh
, &cfi
)) {
278 if (fibh
.sbh
!= fibh
.ebh
)
282 inode
= udf_iget(dir
->i_sb
, lelb_to_cpu(cfi
.icb
.extLocation
));
285 return ERR_PTR(-EACCES
);
289 d_add(dentry
, inode
);
294 static struct fileIdentDesc
*udf_add_entry(struct inode
*dir
,
295 struct dentry
*dentry
,
296 struct udf_fileident_bh
*fibh
,
297 struct fileIdentDesc
*cfi
, int *err
)
299 struct super_block
*sb
= dir
->i_sb
;
300 struct fileIdentDesc
*fi
= NULL
;
304 loff_t size
= udf_ext0_offset(dir
) + dir
->i_size
;
312 struct extent_position epos
= {};
313 struct udf_inode_info
*dinfo
;
315 fibh
->sbh
= fibh
->ebh
= NULL
;
316 name
= kmalloc(UDF_NAME_LEN
, GFP_NOFS
);
323 if (!dentry
->d_name
.len
) {
327 namelen
= udf_put_filename(sb
, dentry
->d_name
.name
, name
,
330 *err
= -ENAMETOOLONG
;
337 nfidlen
= (sizeof(struct fileIdentDesc
) + namelen
+ 3) & ~3;
339 f_pos
= udf_ext0_offset(dir
);
341 fibh
->soffset
= fibh
->eoffset
= f_pos
& (dir
->i_sb
->s_blocksize
- 1);
343 if (dinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_IN_ICB
) {
344 if (inode_bmap(dir
, f_pos
>> dir
->i_sb
->s_blocksize_bits
, &epos
,
345 &eloc
, &elen
, &offset
) != (EXT_RECORDED_ALLOCATED
>> 30)) {
346 block
= udf_get_lb_pblock(dir
->i_sb
,
347 dinfo
->i_location
, 0);
348 fibh
->soffset
= fibh
->eoffset
= sb
->s_blocksize
;
351 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, offset
);
352 if ((++offset
<< dir
->i_sb
->s_blocksize_bits
) < elen
) {
353 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
354 epos
.offset
-= sizeof(short_ad
);
355 else if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
356 epos
.offset
-= sizeof(long_ad
);
360 fibh
->sbh
= fibh
->ebh
= udf_tread(dir
->i_sb
, block
);
366 block
= dinfo
->i_location
.logicalBlockNum
;
369 while (f_pos
< size
) {
370 fi
= udf_fileident_read(dir
, &f_pos
, fibh
, cfi
, &epos
, &eloc
,
378 liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
379 lfi
= cfi
->lengthFileIdent
;
381 if ((cfi
->fileCharacteristics
& FID_FILE_CHAR_DELETED
) != 0) {
382 if (((sizeof(struct fileIdentDesc
) +
383 liu
+ lfi
+ 3) & ~3) == nfidlen
) {
384 cfi
->descTag
.tagSerialNum
= cpu_to_le16(1);
385 cfi
->fileVersionNum
= cpu_to_le16(1);
386 cfi
->fileCharacteristics
= 0;
387 cfi
->lengthFileIdent
= namelen
;
388 cfi
->lengthOfImpUse
= cpu_to_le16(0);
389 if (!udf_write_fi(dir
, cfi
, fi
, fibh
, NULL
,
401 if (dinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_IN_ICB
) {
402 elen
= (elen
+ sb
->s_blocksize
- 1) & ~(sb
->s_blocksize
- 1);
403 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
404 epos
.offset
-= sizeof(short_ad
);
405 else if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
406 epos
.offset
-= sizeof(long_ad
);
407 udf_write_aext(dir
, &epos
, eloc
, elen
, 1);
411 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
&&
412 sb
->s_blocksize
- fibh
->eoffset
< nfidlen
) {
415 fibh
->soffset
-= udf_ext0_offset(dir
);
416 fibh
->eoffset
-= udf_ext0_offset(dir
);
417 f_pos
-= udf_ext0_offset(dir
);
418 if (fibh
->sbh
!= fibh
->ebh
)
421 fibh
->sbh
= fibh
->ebh
=
422 udf_expand_dir_adinicb(dir
, &block
, err
);
425 epos
.block
= dinfo
->i_location
;
426 epos
.offset
= udf_file_entry_alloc_offset(dir
);
427 /* Load extent udf_expand_dir_adinicb() has created */
428 udf_current_aext(dir
, &epos
, &eloc
, &elen
, 1);
431 if (sb
->s_blocksize
- fibh
->eoffset
>= nfidlen
) {
432 fibh
->soffset
= fibh
->eoffset
;
433 fibh
->eoffset
+= nfidlen
;
434 if (fibh
->sbh
!= fibh
->ebh
) {
436 fibh
->sbh
= fibh
->ebh
;
439 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
440 block
= dinfo
->i_location
.logicalBlockNum
;
441 fi
= (struct fileIdentDesc
*)
442 (dinfo
->i_ext
.i_data
+
444 udf_ext0_offset(dir
) +
447 block
= eloc
.logicalBlockNum
+
449 dir
->i_sb
->s_blocksize_bits
);
450 fi
= (struct fileIdentDesc
*)
451 (fibh
->sbh
->b_data
+ fibh
->soffset
);
454 fibh
->soffset
= fibh
->eoffset
- sb
->s_blocksize
;
455 fibh
->eoffset
+= nfidlen
- sb
->s_blocksize
;
456 if (fibh
->sbh
!= fibh
->ebh
) {
458 fibh
->sbh
= fibh
->ebh
;
461 block
= eloc
.logicalBlockNum
+ ((elen
- 1) >>
462 dir
->i_sb
->s_blocksize_bits
);
463 fibh
->ebh
= udf_bread(dir
,
464 f_pos
>> dir
->i_sb
->s_blocksize_bits
, 1, err
);
468 if (!fibh
->soffset
) {
469 if (udf_next_aext(dir
, &epos
, &eloc
, &elen
, 1) ==
470 (EXT_RECORDED_ALLOCATED
>> 30)) {
471 block
= eloc
.logicalBlockNum
+ ((elen
- 1) >>
472 dir
->i_sb
->s_blocksize_bits
);
477 fibh
->sbh
= fibh
->ebh
;
478 fi
= (struct fileIdentDesc
*)(fibh
->sbh
->b_data
);
480 fi
= (struct fileIdentDesc
*)
481 (fibh
->sbh
->b_data
+ sb
->s_blocksize
+
486 memset(cfi
, 0, sizeof(struct fileIdentDesc
));
487 if (UDF_SB(sb
)->s_udfrev
>= 0x0200)
488 udf_new_tag((char *)cfi
, TAG_IDENT_FID
, 3, 1, block
,
491 udf_new_tag((char *)cfi
, TAG_IDENT_FID
, 2, 1, block
,
493 cfi
->fileVersionNum
= cpu_to_le16(1);
494 cfi
->lengthFileIdent
= namelen
;
495 cfi
->lengthOfImpUse
= cpu_to_le16(0);
496 if (!udf_write_fi(dir
, cfi
, fi
, fibh
, NULL
, name
)) {
497 dir
->i_size
+= nfidlen
;
498 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
499 dinfo
->i_lenAlloc
+= nfidlen
;
500 mark_inode_dirty(dir
);
509 if (fibh
->sbh
!= fibh
->ebh
)
518 static int udf_delete_entry(struct inode
*inode
, struct fileIdentDesc
*fi
,
519 struct udf_fileident_bh
*fibh
,
520 struct fileIdentDesc
*cfi
)
522 cfi
->fileCharacteristics
|= FID_FILE_CHAR_DELETED
;
524 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
))
525 memset(&(cfi
->icb
), 0x00, sizeof(long_ad
));
527 return udf_write_fi(inode
, cfi
, fi
, fibh
, NULL
, NULL
);
530 static int udf_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
531 struct nameidata
*nd
)
533 struct udf_fileident_bh fibh
;
535 struct fileIdentDesc cfi
, *fi
;
537 struct udf_inode_info
*iinfo
;
540 inode
= udf_new_inode(dir
, mode
, &err
);
546 iinfo
= UDF_I(inode
);
547 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
548 inode
->i_data
.a_ops
= &udf_adinicb_aops
;
550 inode
->i_data
.a_ops
= &udf_aops
;
551 inode
->i_op
= &udf_file_inode_operations
;
552 inode
->i_fop
= &udf_file_operations
;
553 inode
->i_mode
= mode
;
554 mark_inode_dirty(inode
);
556 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
559 mark_inode_dirty(inode
);
564 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
565 cfi
.icb
.extLocation
= cpu_to_lelb(iinfo
->i_location
);
566 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
567 cpu_to_le32(iinfo
->i_unique
& 0x00000000FFFFFFFFUL
);
568 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
569 if (UDF_I(dir
)->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
570 mark_inode_dirty(dir
);
571 if (fibh
.sbh
!= fibh
.ebh
)
575 d_instantiate(dentry
, inode
);
580 static int udf_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
,
584 struct udf_fileident_bh fibh
;
585 struct fileIdentDesc cfi
, *fi
;
587 struct udf_inode_info
*iinfo
;
589 if (!old_valid_dev(rdev
))
594 inode
= udf_new_inode(dir
, mode
, &err
);
598 iinfo
= UDF_I(inode
);
599 inode
->i_uid
= current
->fsuid
;
600 init_special_inode(inode
, mode
, rdev
);
601 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
604 mark_inode_dirty(inode
);
609 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
610 cfi
.icb
.extLocation
= cpu_to_lelb(iinfo
->i_location
);
611 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
612 cpu_to_le32(iinfo
->i_unique
& 0x00000000FFFFFFFFUL
);
613 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
614 if (UDF_I(dir
)->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
615 mark_inode_dirty(dir
);
616 mark_inode_dirty(inode
);
618 if (fibh
.sbh
!= fibh
.ebh
)
621 d_instantiate(dentry
, inode
);
629 static int udf_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
632 struct udf_fileident_bh fibh
;
633 struct fileIdentDesc cfi
, *fi
;
635 struct udf_inode_info
*dinfo
= UDF_I(dir
);
636 struct udf_inode_info
*iinfo
;
640 if (dir
->i_nlink
>= (256 << sizeof(dir
->i_nlink
)) - 1)
644 inode
= udf_new_inode(dir
, S_IFDIR
, &err
);
648 iinfo
= UDF_I(inode
);
649 inode
->i_op
= &udf_dir_inode_operations
;
650 inode
->i_fop
= &udf_dir_operations
;
651 fi
= udf_add_entry(inode
, NULL
, &fibh
, &cfi
, &err
);
654 mark_inode_dirty(inode
);
659 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
660 cfi
.icb
.extLocation
= cpu_to_lelb(dinfo
->i_location
);
661 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
662 cpu_to_le32(dinfo
->i_unique
& 0x00000000FFFFFFFFUL
);
663 cfi
.fileCharacteristics
=
664 FID_FILE_CHAR_DIRECTORY
| FID_FILE_CHAR_PARENT
;
665 udf_write_fi(inode
, &cfi
, fi
, &fibh
, NULL
, NULL
);
667 inode
->i_mode
= S_IFDIR
| mode
;
668 if (dir
->i_mode
& S_ISGID
)
669 inode
->i_mode
|= S_ISGID
;
670 mark_inode_dirty(inode
);
672 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
675 mark_inode_dirty(inode
);
679 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
680 cfi
.icb
.extLocation
= cpu_to_lelb(iinfo
->i_location
);
681 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
682 cpu_to_le32(iinfo
->i_unique
& 0x00000000FFFFFFFFUL
);
683 cfi
.fileCharacteristics
|= FID_FILE_CHAR_DIRECTORY
;
684 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
686 mark_inode_dirty(dir
);
687 d_instantiate(dentry
, inode
);
688 if (fibh
.sbh
!= fibh
.ebh
)
698 static int empty_dir(struct inode
*dir
)
700 struct fileIdentDesc
*fi
, cfi
;
701 struct udf_fileident_bh fibh
;
703 loff_t size
= udf_ext0_offset(dir
) + dir
->i_size
;
708 struct extent_position epos
= {};
709 struct udf_inode_info
*dinfo
= UDF_I(dir
);
711 f_pos
= udf_ext0_offset(dir
);
712 fibh
.soffset
= fibh
.eoffset
= f_pos
& (dir
->i_sb
->s_blocksize
- 1);
714 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
715 fibh
.sbh
= fibh
.ebh
= NULL
;
716 else if (inode_bmap(dir
, f_pos
>> dir
->i_sb
->s_blocksize_bits
,
717 &epos
, &eloc
, &elen
, &offset
) ==
718 (EXT_RECORDED_ALLOCATED
>> 30)) {
719 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, offset
);
720 if ((++offset
<< dir
->i_sb
->s_blocksize_bits
) < elen
) {
721 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
722 epos
.offset
-= sizeof(short_ad
);
723 else if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
724 epos
.offset
-= sizeof(long_ad
);
728 fibh
.sbh
= fibh
.ebh
= udf_tread(dir
->i_sb
, block
);
738 while (f_pos
< size
) {
739 fi
= udf_fileident_read(dir
, &f_pos
, &fibh
, &cfi
, &epos
, &eloc
,
742 if (fibh
.sbh
!= fibh
.ebh
)
749 if (cfi
.lengthFileIdent
&&
750 (cfi
.fileCharacteristics
& FID_FILE_CHAR_DELETED
) == 0) {
751 if (fibh
.sbh
!= fibh
.ebh
)
759 if (fibh
.sbh
!= fibh
.ebh
)
767 static int udf_rmdir(struct inode
*dir
, struct dentry
*dentry
)
770 struct inode
*inode
= dentry
->d_inode
;
771 struct udf_fileident_bh fibh
;
772 struct fileIdentDesc
*fi
, cfi
;
777 fi
= udf_find_entry(dir
, dentry
, &fibh
, &cfi
);
782 tloc
= lelb_to_cpu(cfi
.icb
.extLocation
);
783 if (udf_get_lb_pblock(dir
->i_sb
, tloc
, 0) != inode
->i_ino
)
786 if (!empty_dir(inode
))
788 retval
= udf_delete_entry(dir
, fi
, &fibh
, &cfi
);
791 if (inode
->i_nlink
!= 2)
792 udf_warning(inode
->i_sb
, "udf_rmdir",
793 "empty directory has nlink != 2 (%d)",
797 inode_dec_link_count(dir
);
798 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
=
799 current_fs_time(dir
->i_sb
);
800 mark_inode_dirty(dir
);
803 if (fibh
.sbh
!= fibh
.ebh
)
812 static int udf_unlink(struct inode
*dir
, struct dentry
*dentry
)
815 struct inode
*inode
= dentry
->d_inode
;
816 struct udf_fileident_bh fibh
;
817 struct fileIdentDesc
*fi
;
818 struct fileIdentDesc cfi
;
823 fi
= udf_find_entry(dir
, dentry
, &fibh
, &cfi
);
828 tloc
= lelb_to_cpu(cfi
.icb
.extLocation
);
829 if (udf_get_lb_pblock(dir
->i_sb
, tloc
, 0) != inode
->i_ino
)
832 if (!inode
->i_nlink
) {
833 udf_debug("Deleting nonexistent file (%lu), %d\n",
834 inode
->i_ino
, inode
->i_nlink
);
837 retval
= udf_delete_entry(dir
, fi
, &fibh
, &cfi
);
840 dir
->i_ctime
= dir
->i_mtime
= current_fs_time(dir
->i_sb
);
841 mark_inode_dirty(dir
);
842 inode_dec_link_count(inode
);
843 inode
->i_ctime
= dir
->i_ctime
;
847 if (fibh
.sbh
!= fibh
.ebh
)
856 static int udf_symlink(struct inode
*dir
, struct dentry
*dentry
,
860 struct pathComponent
*pc
;
862 struct udf_fileident_bh fibh
;
863 struct extent_position epos
= {};
864 int eoffset
, elen
= 0;
865 struct fileIdentDesc
*fi
;
866 struct fileIdentDesc cfi
;
872 struct buffer_head
*bh
;
873 struct udf_inode_info
*iinfo
;
876 inode
= udf_new_inode(dir
, S_IFLNK
, &err
);
880 name
= kmalloc(UDF_NAME_LEN
, GFP_NOFS
);
886 iinfo
= UDF_I(inode
);
887 inode
->i_mode
= S_IFLNK
| S_IRWXUGO
;
888 inode
->i_data
.a_ops
= &udf_symlink_aops
;
889 inode
->i_op
= &page_symlink_inode_operations
;
891 if (iinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_IN_ICB
) {
895 block
= udf_new_block(inode
->i_sb
, inode
,
896 iinfo
->i_location
.partitionReferenceNum
,
897 iinfo
->i_location
.logicalBlockNum
, &err
);
900 epos
.block
= iinfo
->i_location
;
901 epos
.offset
= udf_file_entry_alloc_offset(inode
);
903 eloc
.logicalBlockNum
= block
;
904 eloc
.partitionReferenceNum
=
905 iinfo
->i_location
.partitionReferenceNum
;
906 bsize
= inode
->i_sb
->s_blocksize
;
907 iinfo
->i_lenExtents
= bsize
;
908 udf_add_aext(inode
, &epos
, eloc
, bsize
, 0);
911 block
= udf_get_pblock(inode
->i_sb
, block
,
912 iinfo
->i_location
.partitionReferenceNum
,
914 epos
.bh
= udf_tread(inode
->i_sb
, block
);
915 lock_buffer(epos
.bh
);
916 memset(epos
.bh
->b_data
, 0x00, inode
->i_sb
->s_blocksize
);
917 set_buffer_uptodate(epos
.bh
);
918 unlock_buffer(epos
.bh
);
919 mark_buffer_dirty_inode(epos
.bh
, inode
);
920 ea
= epos
.bh
->b_data
+ udf_ext0_offset(inode
);
922 ea
= iinfo
->i_ext
.i_data
+ iinfo
->i_lenEAttr
;
924 eoffset
= inode
->i_sb
->s_blocksize
- udf_ext0_offset(inode
);
925 pc
= (struct pathComponent
*)ea
;
927 if (*symname
== '/') {
930 } while (*symname
== '/');
932 pc
->componentType
= 1;
933 pc
->lengthComponentIdent
= 0;
934 pc
->componentFileVersionNum
= 0;
935 pc
+= sizeof(struct pathComponent
);
936 elen
+= sizeof(struct pathComponent
);
942 if (elen
+ sizeof(struct pathComponent
) > eoffset
)
945 pc
= (struct pathComponent
*)(ea
+ elen
);
947 compstart
= (char *)symname
;
951 } while (*symname
&& *symname
!= '/');
953 pc
->componentType
= 5;
954 pc
->lengthComponentIdent
= 0;
955 pc
->componentFileVersionNum
= 0;
956 if (compstart
[0] == '.') {
957 if ((symname
- compstart
) == 1)
958 pc
->componentType
= 4;
959 else if ((symname
- compstart
) == 2 &&
961 pc
->componentType
= 3;
964 if (pc
->componentType
== 5) {
965 namelen
= udf_put_filename(inode
->i_sb
, compstart
, name
,
966 symname
- compstart
);
970 if (elen
+ sizeof(struct pathComponent
) + namelen
>
974 pc
->lengthComponentIdent
= namelen
;
976 memcpy(pc
->componentIdent
, name
, namelen
);
979 elen
+= sizeof(struct pathComponent
) + pc
->lengthComponentIdent
;
984 } while (*symname
== '/');
989 inode
->i_size
= elen
;
990 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
991 iinfo
->i_lenAlloc
= inode
->i_size
;
992 mark_inode_dirty(inode
);
994 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
997 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
998 cfi
.icb
.extLocation
= cpu_to_lelb(iinfo
->i_location
);
999 bh
= UDF_SB(inode
->i_sb
)->s_lvid_bh
;
1001 struct logicalVolIntegrityDesc
*lvid
=
1002 (struct logicalVolIntegrityDesc
*)bh
->b_data
;
1003 struct logicalVolHeaderDesc
*lvhd
;
1005 lvhd
= (struct logicalVolHeaderDesc
*)
1006 lvid
->logicalVolContentsUse
;
1007 uniqueID
= le64_to_cpu(lvhd
->uniqueID
);
1008 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
1009 cpu_to_le32(uniqueID
& 0x00000000FFFFFFFFUL
);
1010 if (!(++uniqueID
& 0x00000000FFFFFFFFUL
))
1012 lvhd
->uniqueID
= cpu_to_le64(uniqueID
);
1013 mark_buffer_dirty(bh
);
1015 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
1016 if (UDF_I(dir
)->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
1017 mark_inode_dirty(dir
);
1018 if (fibh
.sbh
!= fibh
.ebh
)
1021 d_instantiate(dentry
, inode
);
1030 inode_dec_link_count(inode
);
1035 static int udf_link(struct dentry
*old_dentry
, struct inode
*dir
,
1036 struct dentry
*dentry
)
1038 struct inode
*inode
= old_dentry
->d_inode
;
1039 struct udf_fileident_bh fibh
;
1040 struct fileIdentDesc cfi
, *fi
;
1042 struct buffer_head
*bh
;
1045 if (inode
->i_nlink
>= (256 << sizeof(inode
->i_nlink
)) - 1) {
1050 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(inode
)->i_location
);
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(dir
)->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
1075 mark_inode_dirty(dir
);
1077 if (fibh
.sbh
!= fibh
.ebh
)
1081 inode
->i_ctime
= current_fs_time(inode
->i_sb
);
1082 mark_inode_dirty(inode
);
1083 atomic_inc(&inode
->i_count
);
1084 d_instantiate(dentry
, inode
);
1090 /* Anybody can rename anything with this: the permission checks are left to the
1091 * higher-level routines.
1093 static int udf_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1094 struct inode
*new_dir
, struct dentry
*new_dentry
)
1096 struct inode
*old_inode
= old_dentry
->d_inode
;
1097 struct inode
*new_inode
= new_dentry
->d_inode
;
1098 struct udf_fileident_bh ofibh
, nfibh
;
1099 struct fileIdentDesc
*ofi
= NULL
, *nfi
= NULL
, *dir_fi
= NULL
;
1100 struct fileIdentDesc ocfi
, ncfi
;
1101 struct buffer_head
*dir_bh
= NULL
;
1102 int retval
= -ENOENT
;
1103 kernel_lb_addr tloc
;
1104 struct udf_inode_info
*old_iinfo
= UDF_I(old_inode
);
1107 ofi
= udf_find_entry(old_dir
, old_dentry
, &ofibh
, &ocfi
);
1109 if (ofibh
.sbh
!= ofibh
.ebh
)
1113 tloc
= lelb_to_cpu(ocfi
.icb
.extLocation
);
1114 if (!ofi
|| udf_get_lb_pblock(old_dir
->i_sb
, tloc
, 0)
1115 != old_inode
->i_ino
)
1118 nfi
= udf_find_entry(new_dir
, new_dentry
, &nfibh
, &ncfi
);
1121 if (nfibh
.sbh
!= nfibh
.ebh
)
1127 if (S_ISDIR(old_inode
->i_mode
)) {
1128 int offset
= udf_ext0_offset(old_inode
);
1131 retval
= -ENOTEMPTY
;
1132 if (!empty_dir(new_inode
))
1136 if (old_iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
1137 dir_fi
= udf_get_fileident(
1138 old_iinfo
->i_ext
.i_data
-
1140 sizeof(struct extendedFileEntry
) :
1141 sizeof(struct fileEntry
)),
1142 old_inode
->i_sb
->s_blocksize
, &offset
);
1144 dir_bh
= udf_bread(old_inode
, 0, 0, &retval
);
1147 dir_fi
= udf_get_fileident(dir_bh
->b_data
,
1148 old_inode
->i_sb
->s_blocksize
, &offset
);
1152 tloc
= lelb_to_cpu(dir_fi
->icb
.extLocation
);
1153 if (udf_get_lb_pblock(old_inode
->i_sb
, tloc
, 0) !=
1160 (256 << sizeof(new_dir
->i_nlink
)) - 1)
1164 nfi
= udf_add_entry(new_dir
, new_dentry
, &nfibh
, &ncfi
,
1171 * Like most other Unix systems, set the ctime for inodes on a
1174 old_inode
->i_ctime
= current_fs_time(old_inode
->i_sb
);
1175 mark_inode_dirty(old_inode
);
1180 ncfi
.fileVersionNum
= ocfi
.fileVersionNum
;
1181 ncfi
.fileCharacteristics
= ocfi
.fileCharacteristics
;
1182 memcpy(&(ncfi
.icb
), &(ocfi
.icb
), sizeof(long_ad
));
1183 udf_write_fi(new_dir
, &ncfi
, nfi
, &nfibh
, NULL
, NULL
);
1185 /* The old fid may have moved - find it again */
1186 ofi
= udf_find_entry(old_dir
, old_dentry
, &ofibh
, &ocfi
);
1187 udf_delete_entry(old_dir
, ofi
, &ofibh
, &ocfi
);
1190 new_inode
->i_ctime
= current_fs_time(new_inode
->i_sb
);
1191 inode_dec_link_count(new_inode
);
1193 old_dir
->i_ctime
= old_dir
->i_mtime
= current_fs_time(old_dir
->i_sb
);
1194 mark_inode_dirty(old_dir
);
1197 dir_fi
->icb
.extLocation
= cpu_to_lelb(UDF_I(new_dir
)->i_location
);
1198 udf_update_tag((char *)dir_fi
,
1199 (sizeof(struct fileIdentDesc
) +
1200 le16_to_cpu(dir_fi
->lengthOfImpUse
) + 3) & ~3);
1201 if (old_iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
1202 mark_inode_dirty(old_inode
);
1204 mark_buffer_dirty_inode(dir_bh
, old_inode
);
1206 inode_dec_link_count(old_dir
);
1208 inode_dec_link_count(new_inode
);
1211 mark_inode_dirty(new_dir
);
1216 if (ofibh
.sbh
!= ofibh
.ebh
)
1226 if (nfibh
.sbh
!= nfibh
.ebh
)
1235 const struct inode_operations udf_dir_inode_operations
= {
1236 .lookup
= udf_lookup
,
1237 .create
= udf_create
,
1239 .unlink
= udf_unlink
,
1240 .symlink
= udf_symlink
,
1244 .rename
= udf_rename
,