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/smp_lock.h>
31 #include <linux/buffer_head.h>
32 #include <linux/sched.h>
33 #include <linux/crc-itu-t.h>
34 #include <linux/exportfs.h>
36 static inline int udf_match(int len1
, const unsigned char *name1
, int len2
,
37 const unsigned char *name2
)
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(struct 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(struct tag
),
102 sizeof(struct fileIdentDesc
) - sizeof(struct tag
));
104 if (fibh
->sbh
== fibh
->ebh
) {
105 crc
= crc_itu_t(crc
, (uint8_t *)sfi
->impUse
,
106 crclen
+ sizeof(struct 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(struct 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 const struct qstr
*child
,
145 struct udf_fileident_bh
*fibh
,
146 struct fileIdentDesc
*cfi
)
148 struct fileIdentDesc
*fi
= NULL
;
151 unsigned char *fname
= NULL
;
152 unsigned char *nameptr
;
156 struct kernel_lb_addr eloc
;
159 struct extent_position epos
= {};
160 struct udf_inode_info
*dinfo
= UDF_I(dir
);
161 int isdotdot
= child
->len
== 2 &&
162 child
->name
[0] == '.' && child
->name
[1] == '.';
164 size
= udf_ext0_offset(dir
) + dir
->i_size
;
165 f_pos
= udf_ext0_offset(dir
);
167 fibh
->sbh
= fibh
->ebh
= NULL
;
168 fibh
->soffset
= fibh
->eoffset
= f_pos
& (dir
->i_sb
->s_blocksize
- 1);
169 if (dinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_IN_ICB
) {
170 if (inode_bmap(dir
, f_pos
>> dir
->i_sb
->s_blocksize_bits
, &epos
,
171 &eloc
, &elen
, &offset
) != (EXT_RECORDED_ALLOCATED
>> 30))
173 block
= udf_get_lb_pblock(dir
->i_sb
, &eloc
, offset
);
174 if ((++offset
<< dir
->i_sb
->s_blocksize_bits
) < elen
) {
175 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
176 epos
.offset
-= sizeof(struct short_ad
);
177 else if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
178 epos
.offset
-= sizeof(struct long_ad
);
182 fibh
->sbh
= fibh
->ebh
= udf_tread(dir
->i_sb
, block
);
187 fname
= kmalloc(UDF_NAME_LEN
, GFP_NOFS
);
191 while (f_pos
< size
) {
192 fi
= udf_fileident_read(dir
, &f_pos
, fibh
, cfi
, &epos
, &eloc
,
197 liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
198 lfi
= cfi
->lengthFileIdent
;
200 if (fibh
->sbh
== fibh
->ebh
) {
201 nameptr
= fi
->fileIdent
+ liu
;
203 int poffset
; /* Unpaded ending offset */
205 poffset
= fibh
->soffset
+ sizeof(struct fileIdentDesc
) +
209 nameptr
= (uint8_t *)(fibh
->ebh
->b_data
+
213 memcpy(nameptr
, fi
->fileIdent
+ liu
,
215 memcpy(nameptr
+ lfi
- poffset
,
216 fibh
->ebh
->b_data
, poffset
);
220 if ((cfi
->fileCharacteristics
& FID_FILE_CHAR_DELETED
) != 0) {
221 if (!UDF_QUERY_FLAG(dir
->i_sb
, UDF_FLAG_UNDELETE
))
225 if ((cfi
->fileCharacteristics
& FID_FILE_CHAR_HIDDEN
) != 0) {
226 if (!UDF_QUERY_FLAG(dir
->i_sb
, UDF_FLAG_UNHIDE
))
230 if ((cfi
->fileCharacteristics
& FID_FILE_CHAR_PARENT
) &&
239 flen
= udf_get_filename(dir
->i_sb
, nameptr
, fname
, lfi
);
240 if (flen
&& udf_match(flen
, fname
, child
->len
, child
->name
))
246 if (fibh
->sbh
!= fibh
->ebh
)
256 static struct dentry
*udf_lookup(struct inode
*dir
, struct dentry
*dentry
,
257 struct nameidata
*nd
)
259 struct inode
*inode
= NULL
;
260 struct fileIdentDesc cfi
;
261 struct udf_fileident_bh fibh
;
263 if (dentry
->d_name
.len
> UDF_NAME_LEN
- 2)
264 return ERR_PTR(-ENAMETOOLONG
);
268 /* temporary shorthand for specifying files by inode number */
269 if (!strncmp(dentry
->d_name
.name
, ".B=", 3)) {
270 struct kernel_lb_addr lb
= {
271 .logicalBlockNum
= 0,
272 .partitionReferenceNum
=
273 simple_strtoul(dentry
->d_name
.name
+ 3,
276 inode
= udf_iget(dir
->i_sb
, lb
);
279 return ERR_PTR(-EACCES
);
282 #endif /* UDF_RECOVERY */
284 if (udf_find_entry(dir
, &dentry
->d_name
, &fibh
, &cfi
)) {
285 struct kernel_lb_addr loc
;
287 if (fibh
.sbh
!= fibh
.ebh
)
291 loc
= lelb_to_cpu(cfi
.icb
.extLocation
);
292 inode
= udf_iget(dir
->i_sb
, &loc
);
295 return ERR_PTR(-EACCES
);
300 return d_splice_alias(inode
, dentry
);
303 static struct fileIdentDesc
*udf_add_entry(struct inode
*dir
,
304 struct dentry
*dentry
,
305 struct udf_fileident_bh
*fibh
,
306 struct fileIdentDesc
*cfi
, int *err
)
308 struct super_block
*sb
= dir
->i_sb
;
309 struct fileIdentDesc
*fi
= NULL
;
310 unsigned char *name
= NULL
;
313 loff_t size
= udf_ext0_offset(dir
) + dir
->i_size
;
318 struct kernel_lb_addr eloc
;
321 struct extent_position epos
= {};
322 struct udf_inode_info
*dinfo
;
324 fibh
->sbh
= fibh
->ebh
= NULL
;
325 name
= kmalloc(UDF_NAME_LEN
, GFP_NOFS
);
332 if (!dentry
->d_name
.len
) {
336 namelen
= udf_put_filename(sb
, dentry
->d_name
.name
, name
,
339 *err
= -ENAMETOOLONG
;
346 nfidlen
= (sizeof(struct fileIdentDesc
) + namelen
+ 3) & ~3;
348 f_pos
= udf_ext0_offset(dir
);
350 fibh
->soffset
= fibh
->eoffset
= f_pos
& (dir
->i_sb
->s_blocksize
- 1);
352 if (dinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_IN_ICB
) {
353 if (inode_bmap(dir
, f_pos
>> dir
->i_sb
->s_blocksize_bits
, &epos
,
354 &eloc
, &elen
, &offset
) != (EXT_RECORDED_ALLOCATED
>> 30)) {
355 block
= udf_get_lb_pblock(dir
->i_sb
,
356 &dinfo
->i_location
, 0);
357 fibh
->soffset
= fibh
->eoffset
= sb
->s_blocksize
;
360 block
= udf_get_lb_pblock(dir
->i_sb
, &eloc
, offset
);
361 if ((++offset
<< dir
->i_sb
->s_blocksize_bits
) < elen
) {
362 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
363 epos
.offset
-= sizeof(struct short_ad
);
364 else if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
365 epos
.offset
-= sizeof(struct long_ad
);
369 fibh
->sbh
= fibh
->ebh
= udf_tread(dir
->i_sb
, block
);
375 block
= dinfo
->i_location
.logicalBlockNum
;
378 while (f_pos
< size
) {
379 fi
= udf_fileident_read(dir
, &f_pos
, fibh
, cfi
, &epos
, &eloc
,
387 liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
388 lfi
= cfi
->lengthFileIdent
;
390 if ((cfi
->fileCharacteristics
& FID_FILE_CHAR_DELETED
) != 0) {
391 if (((sizeof(struct fileIdentDesc
) +
392 liu
+ lfi
+ 3) & ~3) == nfidlen
) {
393 cfi
->descTag
.tagSerialNum
= cpu_to_le16(1);
394 cfi
->fileVersionNum
= cpu_to_le16(1);
395 cfi
->fileCharacteristics
= 0;
396 cfi
->lengthFileIdent
= namelen
;
397 cfi
->lengthOfImpUse
= cpu_to_le16(0);
398 if (!udf_write_fi(dir
, cfi
, fi
, fibh
, NULL
,
412 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
&&
413 sb
->s_blocksize
- fibh
->eoffset
< nfidlen
) {
416 fibh
->soffset
-= udf_ext0_offset(dir
);
417 fibh
->eoffset
-= udf_ext0_offset(dir
);
418 f_pos
-= udf_ext0_offset(dir
);
419 if (fibh
->sbh
!= fibh
->ebh
)
422 fibh
->sbh
= fibh
->ebh
=
423 udf_expand_dir_adinicb(dir
, &block
, err
);
426 epos
.block
= dinfo
->i_location
;
427 epos
.offset
= udf_file_entry_alloc_offset(dir
);
428 /* Load extent udf_expand_dir_adinicb() has created */
429 udf_current_aext(dir
, &epos
, &eloc
, &elen
, 1);
432 /* Entry fits into current block? */
433 if (sb
->s_blocksize
- fibh
->eoffset
>= nfidlen
) {
434 fibh
->soffset
= fibh
->eoffset
;
435 fibh
->eoffset
+= nfidlen
;
436 if (fibh
->sbh
!= fibh
->ebh
) {
438 fibh
->sbh
= fibh
->ebh
;
441 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
442 block
= dinfo
->i_location
.logicalBlockNum
;
443 fi
= (struct fileIdentDesc
*)
444 (dinfo
->i_ext
.i_data
+
446 udf_ext0_offset(dir
) +
449 block
= eloc
.logicalBlockNum
+
451 dir
->i_sb
->s_blocksize_bits
);
452 fi
= (struct fileIdentDesc
*)
453 (fibh
->sbh
->b_data
+ fibh
->soffset
);
456 /* Round up last extent in the file */
457 elen
= (elen
+ sb
->s_blocksize
- 1) & ~(sb
->s_blocksize
- 1);
458 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
459 epos
.offset
-= sizeof(struct short_ad
);
460 else if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
461 epos
.offset
-= sizeof(struct long_ad
);
462 udf_write_aext(dir
, &epos
, &eloc
, elen
, 1);
463 dinfo
->i_lenExtents
= (dinfo
->i_lenExtents
+ sb
->s_blocksize
464 - 1) & ~(sb
->s_blocksize
- 1);
466 fibh
->soffset
= fibh
->eoffset
- sb
->s_blocksize
;
467 fibh
->eoffset
+= nfidlen
- sb
->s_blocksize
;
468 if (fibh
->sbh
!= fibh
->ebh
) {
470 fibh
->sbh
= fibh
->ebh
;
473 block
= eloc
.logicalBlockNum
+ ((elen
- 1) >>
474 dir
->i_sb
->s_blocksize_bits
);
475 fibh
->ebh
= udf_bread(dir
,
476 f_pos
>> dir
->i_sb
->s_blocksize_bits
, 1, err
);
480 if (!fibh
->soffset
) {
481 if (udf_next_aext(dir
, &epos
, &eloc
, &elen
, 1) ==
482 (EXT_RECORDED_ALLOCATED
>> 30)) {
483 block
= eloc
.logicalBlockNum
+ ((elen
- 1) >>
484 dir
->i_sb
->s_blocksize_bits
);
489 fibh
->sbh
= fibh
->ebh
;
490 fi
= (struct fileIdentDesc
*)(fibh
->sbh
->b_data
);
492 fi
= (struct fileIdentDesc
*)
493 (fibh
->sbh
->b_data
+ sb
->s_blocksize
+
498 memset(cfi
, 0, sizeof(struct fileIdentDesc
));
499 if (UDF_SB(sb
)->s_udfrev
>= 0x0200)
500 udf_new_tag((char *)cfi
, TAG_IDENT_FID
, 3, 1, block
,
503 udf_new_tag((char *)cfi
, TAG_IDENT_FID
, 2, 1, block
,
505 cfi
->fileVersionNum
= cpu_to_le16(1);
506 cfi
->lengthFileIdent
= namelen
;
507 cfi
->lengthOfImpUse
= cpu_to_le16(0);
508 if (!udf_write_fi(dir
, cfi
, fi
, fibh
, NULL
, name
)) {
509 dir
->i_size
+= nfidlen
;
510 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
511 dinfo
->i_lenAlloc
+= nfidlen
;
513 /* Find the last extent and truncate it to proper size */
514 while (udf_next_aext(dir
, &epos
, &eloc
, &elen
, 1) ==
515 (EXT_RECORDED_ALLOCATED
>> 30))
517 elen
-= dinfo
->i_lenExtents
- dir
->i_size
;
518 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
519 epos
.offset
-= sizeof(struct short_ad
);
520 else if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
521 epos
.offset
-= sizeof(struct long_ad
);
522 udf_write_aext(dir
, &epos
, &eloc
, elen
, 1);
523 dinfo
->i_lenExtents
= dir
->i_size
;
526 mark_inode_dirty(dir
);
535 if (fibh
->sbh
!= fibh
->ebh
)
544 static int udf_delete_entry(struct inode
*inode
, struct fileIdentDesc
*fi
,
545 struct udf_fileident_bh
*fibh
,
546 struct fileIdentDesc
*cfi
)
548 cfi
->fileCharacteristics
|= FID_FILE_CHAR_DELETED
;
550 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
))
551 memset(&(cfi
->icb
), 0x00, sizeof(struct long_ad
));
553 return udf_write_fi(inode
, cfi
, fi
, fibh
, NULL
, NULL
);
556 static int udf_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
557 struct nameidata
*nd
)
559 struct udf_fileident_bh fibh
;
561 struct fileIdentDesc cfi
, *fi
;
563 struct udf_inode_info
*iinfo
;
566 inode
= udf_new_inode(dir
, mode
, &err
);
572 iinfo
= UDF_I(inode
);
573 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
574 inode
->i_data
.a_ops
= &udf_adinicb_aops
;
576 inode
->i_data
.a_ops
= &udf_aops
;
577 inode
->i_op
= &udf_file_inode_operations
;
578 inode
->i_fop
= &udf_file_operations
;
579 mark_inode_dirty(inode
);
581 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
584 mark_inode_dirty(inode
);
589 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
590 cfi
.icb
.extLocation
= cpu_to_lelb(iinfo
->i_location
);
591 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
592 cpu_to_le32(iinfo
->i_unique
& 0x00000000FFFFFFFFUL
);
593 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
594 if (UDF_I(dir
)->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
595 mark_inode_dirty(dir
);
596 if (fibh
.sbh
!= fibh
.ebh
)
600 d_instantiate(dentry
, inode
);
605 static int udf_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
,
609 struct udf_fileident_bh fibh
;
610 struct fileIdentDesc cfi
, *fi
;
612 struct udf_inode_info
*iinfo
;
614 if (!old_valid_dev(rdev
))
619 inode
= udf_new_inode(dir
, mode
, &err
);
623 iinfo
= UDF_I(inode
);
624 init_special_inode(inode
, mode
, rdev
);
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(iinfo
->i_location
);
635 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
636 cpu_to_le32(iinfo
->i_unique
& 0x00000000FFFFFFFFUL
);
637 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
638 if (UDF_I(dir
)->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
639 mark_inode_dirty(dir
);
640 mark_inode_dirty(inode
);
642 if (fibh
.sbh
!= fibh
.ebh
)
645 d_instantiate(dentry
, inode
);
653 static int udf_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
656 struct udf_fileident_bh fibh
;
657 struct fileIdentDesc cfi
, *fi
;
659 struct udf_inode_info
*dinfo
= UDF_I(dir
);
660 struct udf_inode_info
*iinfo
;
664 if (dir
->i_nlink
>= (256 << sizeof(dir
->i_nlink
)) - 1)
668 inode
= udf_new_inode(dir
, S_IFDIR
| mode
, &err
);
672 iinfo
= UDF_I(inode
);
673 inode
->i_op
= &udf_dir_inode_operations
;
674 inode
->i_fop
= &udf_dir_operations
;
675 fi
= udf_add_entry(inode
, NULL
, &fibh
, &cfi
, &err
);
678 mark_inode_dirty(inode
);
683 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
684 cfi
.icb
.extLocation
= cpu_to_lelb(dinfo
->i_location
);
685 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
686 cpu_to_le32(dinfo
->i_unique
& 0x00000000FFFFFFFFUL
);
687 cfi
.fileCharacteristics
=
688 FID_FILE_CHAR_DIRECTORY
| FID_FILE_CHAR_PARENT
;
689 udf_write_fi(inode
, &cfi
, fi
, &fibh
, NULL
, NULL
);
691 mark_inode_dirty(inode
);
693 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
696 mark_inode_dirty(inode
);
700 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
701 cfi
.icb
.extLocation
= cpu_to_lelb(iinfo
->i_location
);
702 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
703 cpu_to_le32(iinfo
->i_unique
& 0x00000000FFFFFFFFUL
);
704 cfi
.fileCharacteristics
|= FID_FILE_CHAR_DIRECTORY
;
705 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
707 mark_inode_dirty(dir
);
708 d_instantiate(dentry
, inode
);
709 if (fibh
.sbh
!= fibh
.ebh
)
719 static int empty_dir(struct inode
*dir
)
721 struct fileIdentDesc
*fi
, cfi
;
722 struct udf_fileident_bh fibh
;
724 loff_t size
= udf_ext0_offset(dir
) + dir
->i_size
;
726 struct kernel_lb_addr eloc
;
729 struct extent_position epos
= {};
730 struct udf_inode_info
*dinfo
= UDF_I(dir
);
732 f_pos
= udf_ext0_offset(dir
);
733 fibh
.soffset
= fibh
.eoffset
= f_pos
& (dir
->i_sb
->s_blocksize
- 1);
735 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
736 fibh
.sbh
= fibh
.ebh
= NULL
;
737 else if (inode_bmap(dir
, f_pos
>> dir
->i_sb
->s_blocksize_bits
,
738 &epos
, &eloc
, &elen
, &offset
) ==
739 (EXT_RECORDED_ALLOCATED
>> 30)) {
740 block
= udf_get_lb_pblock(dir
->i_sb
, &eloc
, offset
);
741 if ((++offset
<< dir
->i_sb
->s_blocksize_bits
) < elen
) {
742 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
743 epos
.offset
-= sizeof(struct short_ad
);
744 else if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
745 epos
.offset
-= sizeof(struct long_ad
);
749 fibh
.sbh
= fibh
.ebh
= udf_tread(dir
->i_sb
, block
);
759 while (f_pos
< size
) {
760 fi
= udf_fileident_read(dir
, &f_pos
, &fibh
, &cfi
, &epos
, &eloc
,
763 if (fibh
.sbh
!= fibh
.ebh
)
770 if (cfi
.lengthFileIdent
&&
771 (cfi
.fileCharacteristics
& FID_FILE_CHAR_DELETED
) == 0) {
772 if (fibh
.sbh
!= fibh
.ebh
)
780 if (fibh
.sbh
!= fibh
.ebh
)
788 static int udf_rmdir(struct inode
*dir
, struct dentry
*dentry
)
791 struct inode
*inode
= dentry
->d_inode
;
792 struct udf_fileident_bh fibh
;
793 struct fileIdentDesc
*fi
, cfi
;
794 struct kernel_lb_addr tloc
;
798 fi
= udf_find_entry(dir
, &dentry
->d_name
, &fibh
, &cfi
);
803 tloc
= lelb_to_cpu(cfi
.icb
.extLocation
);
804 if (udf_get_lb_pblock(dir
->i_sb
, &tloc
, 0) != inode
->i_ino
)
807 if (!empty_dir(inode
))
809 retval
= udf_delete_entry(dir
, fi
, &fibh
, &cfi
);
812 if (inode
->i_nlink
!= 2)
813 udf_warning(inode
->i_sb
, "udf_rmdir",
814 "empty directory has nlink != 2 (%d)",
818 inode_dec_link_count(dir
);
819 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
=
820 current_fs_time(dir
->i_sb
);
821 mark_inode_dirty(dir
);
824 if (fibh
.sbh
!= fibh
.ebh
)
833 static int udf_unlink(struct inode
*dir
, struct dentry
*dentry
)
836 struct inode
*inode
= dentry
->d_inode
;
837 struct udf_fileident_bh fibh
;
838 struct fileIdentDesc
*fi
;
839 struct fileIdentDesc cfi
;
840 struct kernel_lb_addr tloc
;
844 fi
= udf_find_entry(dir
, &dentry
->d_name
, &fibh
, &cfi
);
849 tloc
= lelb_to_cpu(cfi
.icb
.extLocation
);
850 if (udf_get_lb_pblock(dir
->i_sb
, &tloc
, 0) != inode
->i_ino
)
853 if (!inode
->i_nlink
) {
854 udf_debug("Deleting nonexistent file (%lu), %d\n",
855 inode
->i_ino
, inode
->i_nlink
);
858 retval
= udf_delete_entry(dir
, fi
, &fibh
, &cfi
);
861 dir
->i_ctime
= dir
->i_mtime
= current_fs_time(dir
->i_sb
);
862 mark_inode_dirty(dir
);
863 inode_dec_link_count(inode
);
864 inode
->i_ctime
= dir
->i_ctime
;
868 if (fibh
.sbh
!= fibh
.ebh
)
877 static int udf_symlink(struct inode
*dir
, struct dentry
*dentry
,
881 struct pathComponent
*pc
;
882 const char *compstart
;
883 struct udf_fileident_bh fibh
;
884 struct extent_position epos
= {};
885 int eoffset
, elen
= 0;
886 struct fileIdentDesc
*fi
;
887 struct fileIdentDesc cfi
;
891 unsigned char *name
= NULL
;
893 struct buffer_head
*bh
;
894 struct udf_inode_info
*iinfo
;
897 inode
= udf_new_inode(dir
, S_IFLNK
| S_IRWXUGO
, &err
);
901 name
= kmalloc(UDF_NAME_LEN
, GFP_NOFS
);
907 iinfo
= UDF_I(inode
);
908 inode
->i_data
.a_ops
= &udf_symlink_aops
;
909 inode
->i_op
= &udf_symlink_inode_operations
;
911 if (iinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_IN_ICB
) {
912 struct kernel_lb_addr eloc
;
915 block
= udf_new_block(inode
->i_sb
, inode
,
916 iinfo
->i_location
.partitionReferenceNum
,
917 iinfo
->i_location
.logicalBlockNum
, &err
);
920 epos
.block
= iinfo
->i_location
;
921 epos
.offset
= udf_file_entry_alloc_offset(inode
);
923 eloc
.logicalBlockNum
= block
;
924 eloc
.partitionReferenceNum
=
925 iinfo
->i_location
.partitionReferenceNum
;
926 bsize
= inode
->i_sb
->s_blocksize
;
927 iinfo
->i_lenExtents
= bsize
;
928 udf_add_aext(inode
, &epos
, &eloc
, bsize
, 0);
931 block
= udf_get_pblock(inode
->i_sb
, block
,
932 iinfo
->i_location
.partitionReferenceNum
,
934 epos
.bh
= udf_tgetblk(inode
->i_sb
, block
);
935 lock_buffer(epos
.bh
);
936 memset(epos
.bh
->b_data
, 0x00, inode
->i_sb
->s_blocksize
);
937 set_buffer_uptodate(epos
.bh
);
938 unlock_buffer(epos
.bh
);
939 mark_buffer_dirty_inode(epos
.bh
, inode
);
940 ea
= epos
.bh
->b_data
+ udf_ext0_offset(inode
);
942 ea
= iinfo
->i_ext
.i_data
+ iinfo
->i_lenEAttr
;
944 eoffset
= inode
->i_sb
->s_blocksize
- udf_ext0_offset(inode
);
945 pc
= (struct pathComponent
*)ea
;
947 if (*symname
== '/') {
950 } while (*symname
== '/');
952 pc
->componentType
= 1;
953 pc
->lengthComponentIdent
= 0;
954 pc
->componentFileVersionNum
= 0;
955 elen
+= sizeof(struct pathComponent
);
961 if (elen
+ sizeof(struct pathComponent
) > eoffset
)
964 pc
= (struct pathComponent
*)(ea
+ elen
);
970 } while (*symname
&& *symname
!= '/');
972 pc
->componentType
= 5;
973 pc
->lengthComponentIdent
= 0;
974 pc
->componentFileVersionNum
= 0;
975 if (compstart
[0] == '.') {
976 if ((symname
- compstart
) == 1)
977 pc
->componentType
= 4;
978 else if ((symname
- compstart
) == 2 &&
980 pc
->componentType
= 3;
983 if (pc
->componentType
== 5) {
984 namelen
= udf_put_filename(inode
->i_sb
, compstart
, name
,
985 symname
- compstart
);
989 if (elen
+ sizeof(struct pathComponent
) + namelen
>
993 pc
->lengthComponentIdent
= namelen
;
995 memcpy(pc
->componentIdent
, name
, namelen
);
998 elen
+= sizeof(struct pathComponent
) + pc
->lengthComponentIdent
;
1003 } while (*symname
== '/');
1008 inode
->i_size
= elen
;
1009 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
1010 iinfo
->i_lenAlloc
= inode
->i_size
;
1012 udf_truncate_tail_extent(inode
);
1013 mark_inode_dirty(inode
);
1015 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
1018 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
1019 cfi
.icb
.extLocation
= cpu_to_lelb(iinfo
->i_location
);
1020 bh
= UDF_SB(inode
->i_sb
)->s_lvid_bh
;
1022 struct logicalVolIntegrityDesc
*lvid
=
1023 (struct logicalVolIntegrityDesc
*)bh
->b_data
;
1024 struct logicalVolHeaderDesc
*lvhd
;
1026 lvhd
= (struct logicalVolHeaderDesc
*)
1027 lvid
->logicalVolContentsUse
;
1028 uniqueID
= le64_to_cpu(lvhd
->uniqueID
);
1029 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
1030 cpu_to_le32(uniqueID
& 0x00000000FFFFFFFFUL
);
1031 if (!(++uniqueID
& 0x00000000FFFFFFFFUL
))
1033 lvhd
->uniqueID
= cpu_to_le64(uniqueID
);
1034 mark_buffer_dirty(bh
);
1036 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
1037 if (UDF_I(dir
)->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
1038 mark_inode_dirty(dir
);
1039 if (fibh
.sbh
!= fibh
.ebh
)
1042 d_instantiate(dentry
, inode
);
1051 inode_dec_link_count(inode
);
1056 static int udf_link(struct dentry
*old_dentry
, struct inode
*dir
,
1057 struct dentry
*dentry
)
1059 struct inode
*inode
= old_dentry
->d_inode
;
1060 struct udf_fileident_bh fibh
;
1061 struct fileIdentDesc cfi
, *fi
;
1063 struct buffer_head
*bh
;
1066 if (inode
->i_nlink
>= (256 << sizeof(inode
->i_nlink
)) - 1) {
1071 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
1076 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
1077 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I(inode
)->i_location
);
1078 bh
= UDF_SB(inode
->i_sb
)->s_lvid_bh
;
1080 struct logicalVolIntegrityDesc
*lvid
=
1081 (struct logicalVolIntegrityDesc
*)bh
->b_data
;
1082 struct logicalVolHeaderDesc
*lvhd
;
1084 lvhd
= (struct logicalVolHeaderDesc
*)
1085 (lvid
->logicalVolContentsUse
);
1086 uniqueID
= le64_to_cpu(lvhd
->uniqueID
);
1087 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
1088 cpu_to_le32(uniqueID
& 0x00000000FFFFFFFFUL
);
1089 if (!(++uniqueID
& 0x00000000FFFFFFFFUL
))
1091 lvhd
->uniqueID
= cpu_to_le64(uniqueID
);
1092 mark_buffer_dirty(bh
);
1094 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
1095 if (UDF_I(dir
)->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
1096 mark_inode_dirty(dir
);
1098 if (fibh
.sbh
!= fibh
.ebh
)
1102 inode
->i_ctime
= current_fs_time(inode
->i_sb
);
1103 mark_inode_dirty(inode
);
1104 atomic_inc(&inode
->i_count
);
1105 d_instantiate(dentry
, inode
);
1111 /* Anybody can rename anything with this: the permission checks are left to the
1112 * higher-level routines.
1114 static int udf_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1115 struct inode
*new_dir
, struct dentry
*new_dentry
)
1117 struct inode
*old_inode
= old_dentry
->d_inode
;
1118 struct inode
*new_inode
= new_dentry
->d_inode
;
1119 struct udf_fileident_bh ofibh
, nfibh
;
1120 struct fileIdentDesc
*ofi
= NULL
, *nfi
= NULL
, *dir_fi
= NULL
;
1121 struct fileIdentDesc ocfi
, ncfi
;
1122 struct buffer_head
*dir_bh
= NULL
;
1123 int retval
= -ENOENT
;
1124 struct kernel_lb_addr tloc
;
1125 struct udf_inode_info
*old_iinfo
= UDF_I(old_inode
);
1128 ofi
= udf_find_entry(old_dir
, &old_dentry
->d_name
, &ofibh
, &ocfi
);
1130 if (ofibh
.sbh
!= ofibh
.ebh
)
1134 tloc
= lelb_to_cpu(ocfi
.icb
.extLocation
);
1135 if (!ofi
|| udf_get_lb_pblock(old_dir
->i_sb
, &tloc
, 0)
1136 != old_inode
->i_ino
)
1139 nfi
= udf_find_entry(new_dir
, &new_dentry
->d_name
, &nfibh
, &ncfi
);
1142 if (nfibh
.sbh
!= nfibh
.ebh
)
1148 if (S_ISDIR(old_inode
->i_mode
)) {
1149 int offset
= udf_ext0_offset(old_inode
);
1152 retval
= -ENOTEMPTY
;
1153 if (!empty_dir(new_inode
))
1157 if (old_iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
1158 dir_fi
= udf_get_fileident(
1159 old_iinfo
->i_ext
.i_data
-
1161 sizeof(struct extendedFileEntry
) :
1162 sizeof(struct fileEntry
)),
1163 old_inode
->i_sb
->s_blocksize
, &offset
);
1165 dir_bh
= udf_bread(old_inode
, 0, 0, &retval
);
1168 dir_fi
= udf_get_fileident(dir_bh
->b_data
,
1169 old_inode
->i_sb
->s_blocksize
, &offset
);
1173 tloc
= lelb_to_cpu(dir_fi
->icb
.extLocation
);
1174 if (udf_get_lb_pblock(old_inode
->i_sb
, &tloc
, 0) !=
1181 (256 << sizeof(new_dir
->i_nlink
)) - 1)
1185 nfi
= udf_add_entry(new_dir
, new_dentry
, &nfibh
, &ncfi
,
1192 * Like most other Unix systems, set the ctime for inodes on a
1195 old_inode
->i_ctime
= current_fs_time(old_inode
->i_sb
);
1196 mark_inode_dirty(old_inode
);
1201 ncfi
.fileVersionNum
= ocfi
.fileVersionNum
;
1202 ncfi
.fileCharacteristics
= ocfi
.fileCharacteristics
;
1203 memcpy(&(ncfi
.icb
), &(ocfi
.icb
), sizeof(struct long_ad
));
1204 udf_write_fi(new_dir
, &ncfi
, nfi
, &nfibh
, NULL
, NULL
);
1206 /* The old fid may have moved - find it again */
1207 ofi
= udf_find_entry(old_dir
, &old_dentry
->d_name
, &ofibh
, &ocfi
);
1208 udf_delete_entry(old_dir
, ofi
, &ofibh
, &ocfi
);
1211 new_inode
->i_ctime
= current_fs_time(new_inode
->i_sb
);
1212 inode_dec_link_count(new_inode
);
1214 old_dir
->i_ctime
= old_dir
->i_mtime
= current_fs_time(old_dir
->i_sb
);
1215 mark_inode_dirty(old_dir
);
1218 dir_fi
->icb
.extLocation
= cpu_to_lelb(UDF_I(new_dir
)->i_location
);
1219 udf_update_tag((char *)dir_fi
,
1220 (sizeof(struct fileIdentDesc
) +
1221 le16_to_cpu(dir_fi
->lengthOfImpUse
) + 3) & ~3);
1222 if (old_iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
1223 mark_inode_dirty(old_inode
);
1225 mark_buffer_dirty_inode(dir_bh
, old_inode
);
1227 inode_dec_link_count(old_dir
);
1229 inode_dec_link_count(new_inode
);
1232 mark_inode_dirty(new_dir
);
1237 if (ofibh
.sbh
!= ofibh
.ebh
)
1247 if (nfibh
.sbh
!= nfibh
.ebh
)
1256 static struct dentry
*udf_get_parent(struct dentry
*child
)
1258 struct kernel_lb_addr tloc
;
1259 struct inode
*inode
= NULL
;
1260 struct qstr dotdot
= {.name
= "..", .len
= 2};
1261 struct fileIdentDesc cfi
;
1262 struct udf_fileident_bh fibh
;
1265 if (!udf_find_entry(child
->d_inode
, &dotdot
, &fibh
, &cfi
))
1268 if (fibh
.sbh
!= fibh
.ebh
)
1272 tloc
= lelb_to_cpu(cfi
.icb
.extLocation
);
1273 inode
= udf_iget(child
->d_inode
->i_sb
, &tloc
);
1278 return d_obtain_alias(inode
);
1281 return ERR_PTR(-EACCES
);
1285 static struct dentry
*udf_nfs_get_inode(struct super_block
*sb
, u32 block
,
1286 u16 partref
, __u32 generation
)
1288 struct inode
*inode
;
1289 struct kernel_lb_addr loc
;
1292 return ERR_PTR(-ESTALE
);
1294 loc
.logicalBlockNum
= block
;
1295 loc
.partitionReferenceNum
= partref
;
1296 inode
= udf_iget(sb
, &loc
);
1299 return ERR_PTR(-ENOMEM
);
1301 if (generation
&& inode
->i_generation
!= generation
) {
1303 return ERR_PTR(-ESTALE
);
1305 return d_obtain_alias(inode
);
1308 static struct dentry
*udf_fh_to_dentry(struct super_block
*sb
,
1309 struct fid
*fid
, int fh_len
, int fh_type
)
1311 if ((fh_len
!= 3 && fh_len
!= 5) ||
1312 (fh_type
!= FILEID_UDF_WITH_PARENT
&&
1313 fh_type
!= FILEID_UDF_WITHOUT_PARENT
))
1316 return udf_nfs_get_inode(sb
, fid
->udf
.block
, fid
->udf
.partref
,
1317 fid
->udf
.generation
);
1320 static struct dentry
*udf_fh_to_parent(struct super_block
*sb
,
1321 struct fid
*fid
, int fh_len
, int fh_type
)
1323 if (fh_len
!= 5 || fh_type
!= FILEID_UDF_WITH_PARENT
)
1326 return udf_nfs_get_inode(sb
, fid
->udf
.parent_block
,
1327 fid
->udf
.parent_partref
,
1328 fid
->udf
.parent_generation
);
1330 static int udf_encode_fh(struct dentry
*de
, __u32
*fh
, int *lenp
,
1334 struct inode
*inode
= de
->d_inode
;
1335 struct kernel_lb_addr location
= UDF_I(inode
)->i_location
;
1336 struct fid
*fid
= (struct fid
*)fh
;
1337 int type
= FILEID_UDF_WITHOUT_PARENT
;
1339 if (len
< 3 || (connectable
&& len
< 5))
1343 fid
->udf
.block
= location
.logicalBlockNum
;
1344 fid
->udf
.partref
= location
.partitionReferenceNum
;
1345 fid
->udf
.generation
= inode
->i_generation
;
1347 if (connectable
&& !S_ISDIR(inode
->i_mode
)) {
1348 spin_lock(&de
->d_lock
);
1349 inode
= de
->d_parent
->d_inode
;
1350 location
= UDF_I(inode
)->i_location
;
1351 fid
->udf
.parent_block
= location
.logicalBlockNum
;
1352 fid
->udf
.parent_partref
= location
.partitionReferenceNum
;
1353 fid
->udf
.parent_generation
= inode
->i_generation
;
1354 spin_unlock(&de
->d_lock
);
1356 type
= FILEID_UDF_WITH_PARENT
;
1362 const struct export_operations udf_export_ops
= {
1363 .encode_fh
= udf_encode_fh
,
1364 .fh_to_dentry
= udf_fh_to_dentry
,
1365 .fh_to_parent
= udf_fh_to_parent
,
1366 .get_parent
= udf_get_parent
,
1369 const struct inode_operations udf_dir_inode_operations
= {
1370 .lookup
= udf_lookup
,
1371 .create
= udf_create
,
1373 .unlink
= udf_unlink
,
1374 .symlink
= udf_symlink
,
1378 .rename
= udf_rename
,
1380 const struct inode_operations udf_symlink_inode_operations
= {
1381 .readlink
= generic_readlink
,
1382 .follow_link
= page_follow_link_light
,
1383 .put_link
= page_put_link
,