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/sched.h>
31 #include <linux/crc-itu-t.h>
32 #include <linux/exportfs.h>
34 static inline int udf_match(int len1
, const unsigned char *name1
, int len2
,
35 const unsigned char *name2
)
40 return !memcmp(name1
, name2
, len1
);
43 int udf_write_fi(struct inode
*inode
, struct fileIdentDesc
*cfi
,
44 struct fileIdentDesc
*sfi
, struct udf_fileident_bh
*fibh
,
45 uint8_t *impuse
, uint8_t *fileident
)
47 uint16_t crclen
= fibh
->eoffset
- fibh
->soffset
- sizeof(struct tag
);
50 uint16_t liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
51 uint8_t lfi
= cfi
->lengthFileIdent
;
52 int padlen
= fibh
->eoffset
- fibh
->soffset
- liu
- lfi
-
53 sizeof(struct fileIdentDesc
);
56 if (UDF_I(inode
)->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
59 offset
= fibh
->soffset
+ sizeof(struct fileIdentDesc
);
62 if (adinicb
|| (offset
+ liu
< 0)) {
63 memcpy((uint8_t *)sfi
->impUse
, impuse
, liu
);
64 } else if (offset
>= 0) {
65 memcpy(fibh
->ebh
->b_data
+ offset
, impuse
, liu
);
67 memcpy((uint8_t *)sfi
->impUse
, impuse
, -offset
);
68 memcpy(fibh
->ebh
->b_data
, impuse
- offset
,
76 if (adinicb
|| (offset
+ lfi
< 0)) {
77 memcpy((uint8_t *)sfi
->fileIdent
+ liu
, fileident
, lfi
);
78 } else if (offset
>= 0) {
79 memcpy(fibh
->ebh
->b_data
+ offset
, fileident
, lfi
);
81 memcpy((uint8_t *)sfi
->fileIdent
+ liu
, fileident
,
83 memcpy(fibh
->ebh
->b_data
, fileident
- offset
,
90 if (adinicb
|| (offset
+ padlen
< 0)) {
91 memset((uint8_t *)sfi
->padding
+ liu
+ lfi
, 0x00, padlen
);
92 } else if (offset
>= 0) {
93 memset(fibh
->ebh
->b_data
+ offset
, 0x00, padlen
);
95 memset((uint8_t *)sfi
->padding
+ liu
+ lfi
, 0x00, -offset
);
96 memset(fibh
->ebh
->b_data
, 0x00, padlen
+ offset
);
99 crc
= crc_itu_t(0, (uint8_t *)cfi
+ sizeof(struct tag
),
100 sizeof(struct fileIdentDesc
) - sizeof(struct tag
));
102 if (fibh
->sbh
== fibh
->ebh
) {
103 crc
= crc_itu_t(crc
, (uint8_t *)sfi
->impUse
,
104 crclen
+ sizeof(struct tag
) -
105 sizeof(struct fileIdentDesc
));
106 } else if (sizeof(struct fileIdentDesc
) >= -fibh
->soffset
) {
107 crc
= crc_itu_t(crc
, fibh
->ebh
->b_data
+
108 sizeof(struct fileIdentDesc
) +
110 crclen
+ sizeof(struct tag
) -
111 sizeof(struct fileIdentDesc
));
113 crc
= crc_itu_t(crc
, (uint8_t *)sfi
->impUse
,
114 -fibh
->soffset
- sizeof(struct fileIdentDesc
));
115 crc
= crc_itu_t(crc
, fibh
->ebh
->b_data
, fibh
->eoffset
);
118 cfi
->descTag
.descCRC
= cpu_to_le16(crc
);
119 cfi
->descTag
.descCRCLength
= cpu_to_le16(crclen
);
120 cfi
->descTag
.tagChecksum
= udf_tag_checksum(&cfi
->descTag
);
122 if (adinicb
|| (sizeof(struct fileIdentDesc
) <= -fibh
->soffset
)) {
123 memcpy((uint8_t *)sfi
, (uint8_t *)cfi
,
124 sizeof(struct fileIdentDesc
));
126 memcpy((uint8_t *)sfi
, (uint8_t *)cfi
, -fibh
->soffset
);
127 memcpy(fibh
->ebh
->b_data
, (uint8_t *)cfi
- fibh
->soffset
,
128 sizeof(struct fileIdentDesc
) + fibh
->soffset
);
132 mark_inode_dirty(inode
);
134 if (fibh
->sbh
!= fibh
->ebh
)
135 mark_buffer_dirty_inode(fibh
->ebh
, inode
);
136 mark_buffer_dirty_inode(fibh
->sbh
, inode
);
141 static struct fileIdentDesc
*udf_find_entry(struct inode
*dir
,
142 const struct qstr
*child
,
143 struct udf_fileident_bh
*fibh
,
144 struct fileIdentDesc
*cfi
)
146 struct fileIdentDesc
*fi
= NULL
;
149 unsigned char *fname
= NULL
;
150 unsigned char *nameptr
;
154 struct kernel_lb_addr eloc
;
157 struct extent_position epos
= {};
158 struct udf_inode_info
*dinfo
= UDF_I(dir
);
159 int isdotdot
= child
->len
== 2 &&
160 child
->name
[0] == '.' && child
->name
[1] == '.';
161 struct super_block
*sb
= dir
->i_sb
;
163 size
= udf_ext0_offset(dir
) + dir
->i_size
;
164 f_pos
= udf_ext0_offset(dir
);
166 fibh
->sbh
= fibh
->ebh
= NULL
;
167 fibh
->soffset
= fibh
->eoffset
= f_pos
& (sb
->s_blocksize
- 1);
168 if (dinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_IN_ICB
) {
169 if (inode_bmap(dir
, f_pos
>> sb
->s_blocksize_bits
, &epos
,
170 &eloc
, &elen
, &offset
) != (EXT_RECORDED_ALLOCATED
>> 30))
172 block
= udf_get_lb_pblock(sb
, &eloc
, offset
);
173 if ((++offset
<< sb
->s_blocksize_bits
) < elen
) {
174 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
175 epos
.offset
-= sizeof(struct short_ad
);
176 else if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
177 epos
.offset
-= sizeof(struct long_ad
);
181 fibh
->sbh
= fibh
->ebh
= udf_tread(sb
, block
);
186 fname
= kmalloc(UDF_NAME_LEN
, GFP_NOFS
);
190 while (f_pos
< size
) {
191 fi
= udf_fileident_read(dir
, &f_pos
, fibh
, cfi
, &epos
, &eloc
,
196 liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
197 lfi
= cfi
->lengthFileIdent
;
199 if (fibh
->sbh
== fibh
->ebh
) {
200 nameptr
= fi
->fileIdent
+ liu
;
202 int poffset
; /* Unpaded ending offset */
204 poffset
= fibh
->soffset
+ sizeof(struct fileIdentDesc
) +
208 nameptr
= (uint8_t *)(fibh
->ebh
->b_data
+
212 memcpy(nameptr
, fi
->fileIdent
+ liu
,
214 memcpy(nameptr
+ lfi
- poffset
,
215 fibh
->ebh
->b_data
, poffset
);
219 if ((cfi
->fileCharacteristics
& FID_FILE_CHAR_DELETED
) != 0) {
220 if (!UDF_QUERY_FLAG(sb
, UDF_FLAG_UNDELETE
))
224 if ((cfi
->fileCharacteristics
& FID_FILE_CHAR_HIDDEN
) != 0) {
225 if (!UDF_QUERY_FLAG(sb
, UDF_FLAG_UNHIDE
))
229 if ((cfi
->fileCharacteristics
& FID_FILE_CHAR_PARENT
) &&
236 flen
= udf_get_filename(sb
, nameptr
, lfi
, fname
, UDF_NAME_LEN
);
237 if (flen
&& udf_match(flen
, fname
, child
->len
, child
->name
))
243 if (fibh
->sbh
!= fibh
->ebh
)
253 static struct dentry
*udf_lookup(struct inode
*dir
, struct dentry
*dentry
,
256 struct inode
*inode
= NULL
;
257 struct fileIdentDesc cfi
;
258 struct udf_fileident_bh fibh
;
260 if (dentry
->d_name
.len
> UDF_NAME_LEN
- 2)
261 return ERR_PTR(-ENAMETOOLONG
);
264 /* temporary shorthand for specifying files by inode number */
265 if (!strncmp(dentry
->d_name
.name
, ".B=", 3)) {
266 struct kernel_lb_addr lb
= {
267 .logicalBlockNum
= 0,
268 .partitionReferenceNum
=
269 simple_strtoul(dentry
->d_name
.name
+ 3,
272 inode
= udf_iget(dir
->i_sb
, lb
);
276 #endif /* UDF_RECOVERY */
278 if (udf_find_entry(dir
, &dentry
->d_name
, &fibh
, &cfi
)) {
279 struct kernel_lb_addr loc
;
281 if (fibh
.sbh
!= fibh
.ebh
)
285 loc
= lelb_to_cpu(cfi
.icb
.extLocation
);
286 inode
= udf_iget(dir
->i_sb
, &loc
);
288 return ERR_CAST(inode
);
291 return d_splice_alias(inode
, dentry
);
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
;
301 unsigned char *name
= NULL
;
304 loff_t size
= udf_ext0_offset(dir
) + dir
->i_size
;
309 struct kernel_lb_addr eloc
;
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(struct short_ad
);
355 else if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
356 epos
.offset
-= sizeof(struct 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
,
403 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
&&
404 sb
->s_blocksize
- fibh
->eoffset
< nfidlen
) {
407 fibh
->soffset
-= udf_ext0_offset(dir
);
408 fibh
->eoffset
-= udf_ext0_offset(dir
);
409 f_pos
-= udf_ext0_offset(dir
);
410 if (fibh
->sbh
!= fibh
->ebh
)
413 fibh
->sbh
= fibh
->ebh
=
414 udf_expand_dir_adinicb(dir
, &block
, err
);
417 epos
.block
= dinfo
->i_location
;
418 epos
.offset
= udf_file_entry_alloc_offset(dir
);
419 /* Load extent udf_expand_dir_adinicb() has created */
420 udf_current_aext(dir
, &epos
, &eloc
, &elen
, 1);
423 /* Entry fits into current block? */
424 if (sb
->s_blocksize
- fibh
->eoffset
>= nfidlen
) {
425 fibh
->soffset
= fibh
->eoffset
;
426 fibh
->eoffset
+= nfidlen
;
427 if (fibh
->sbh
!= fibh
->ebh
) {
429 fibh
->sbh
= fibh
->ebh
;
432 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
433 block
= dinfo
->i_location
.logicalBlockNum
;
434 fi
= (struct fileIdentDesc
*)
435 (dinfo
->i_ext
.i_data
+
437 udf_ext0_offset(dir
) +
440 block
= eloc
.logicalBlockNum
+
442 dir
->i_sb
->s_blocksize_bits
);
443 fi
= (struct fileIdentDesc
*)
444 (fibh
->sbh
->b_data
+ fibh
->soffset
);
447 /* Round up last extent in the file */
448 elen
= (elen
+ sb
->s_blocksize
- 1) & ~(sb
->s_blocksize
- 1);
449 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
450 epos
.offset
-= sizeof(struct short_ad
);
451 else if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
452 epos
.offset
-= sizeof(struct long_ad
);
453 udf_write_aext(dir
, &epos
, &eloc
, elen
, 1);
454 dinfo
->i_lenExtents
= (dinfo
->i_lenExtents
+ sb
->s_blocksize
455 - 1) & ~(sb
->s_blocksize
- 1);
457 fibh
->soffset
= fibh
->eoffset
- sb
->s_blocksize
;
458 fibh
->eoffset
+= nfidlen
- sb
->s_blocksize
;
459 if (fibh
->sbh
!= fibh
->ebh
) {
461 fibh
->sbh
= fibh
->ebh
;
464 block
= eloc
.logicalBlockNum
+ ((elen
- 1) >>
465 dir
->i_sb
->s_blocksize_bits
);
466 fibh
->ebh
= udf_bread(dir
,
467 f_pos
>> dir
->i_sb
->s_blocksize_bits
, 1, err
);
470 /* Extents could have been merged, invalidate our position */
473 epos
.block
= dinfo
->i_location
;
474 epos
.offset
= udf_file_entry_alloc_offset(dir
);
476 if (!fibh
->soffset
) {
477 /* Find the freshly allocated block */
478 while (udf_next_aext(dir
, &epos
, &eloc
, &elen
, 1) ==
479 (EXT_RECORDED_ALLOCATED
>> 30))
481 block
= eloc
.logicalBlockNum
+ ((elen
- 1) >>
482 dir
->i_sb
->s_blocksize_bits
);
484 fibh
->sbh
= fibh
->ebh
;
485 fi
= (struct fileIdentDesc
*)(fibh
->sbh
->b_data
);
487 fi
= (struct fileIdentDesc
*)
488 (fibh
->sbh
->b_data
+ sb
->s_blocksize
+
493 memset(cfi
, 0, sizeof(struct fileIdentDesc
));
494 if (UDF_SB(sb
)->s_udfrev
>= 0x0200)
495 udf_new_tag((char *)cfi
, TAG_IDENT_FID
, 3, 1, block
,
498 udf_new_tag((char *)cfi
, TAG_IDENT_FID
, 2, 1, block
,
500 cfi
->fileVersionNum
= cpu_to_le16(1);
501 cfi
->lengthFileIdent
= namelen
;
502 cfi
->lengthOfImpUse
= cpu_to_le16(0);
503 if (!udf_write_fi(dir
, cfi
, fi
, fibh
, NULL
, name
)) {
504 dir
->i_size
+= nfidlen
;
505 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
506 dinfo
->i_lenAlloc
+= nfidlen
;
508 /* Find the last extent and truncate it to proper size */
509 while (udf_next_aext(dir
, &epos
, &eloc
, &elen
, 1) ==
510 (EXT_RECORDED_ALLOCATED
>> 30))
512 elen
-= dinfo
->i_lenExtents
- dir
->i_size
;
513 if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
514 epos
.offset
-= sizeof(struct short_ad
);
515 else if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
516 epos
.offset
-= sizeof(struct long_ad
);
517 udf_write_aext(dir
, &epos
, &eloc
, elen
, 1);
518 dinfo
->i_lenExtents
= dir
->i_size
;
521 mark_inode_dirty(dir
);
530 if (fibh
->sbh
!= fibh
->ebh
)
539 static int udf_delete_entry(struct inode
*inode
, struct fileIdentDesc
*fi
,
540 struct udf_fileident_bh
*fibh
,
541 struct fileIdentDesc
*cfi
)
543 cfi
->fileCharacteristics
|= FID_FILE_CHAR_DELETED
;
545 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
))
546 memset(&(cfi
->icb
), 0x00, sizeof(struct long_ad
));
548 return udf_write_fi(inode
, cfi
, fi
, fibh
, NULL
, NULL
);
551 static int udf_add_nondir(struct dentry
*dentry
, struct inode
*inode
)
553 struct udf_inode_info
*iinfo
= UDF_I(inode
);
554 struct inode
*dir
= d_inode(dentry
->d_parent
);
555 struct udf_fileident_bh fibh
;
556 struct fileIdentDesc cfi
, *fi
;
559 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
561 inode_dec_link_count(inode
);
562 unlock_new_inode(inode
);
566 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
567 cfi
.icb
.extLocation
= cpu_to_lelb(iinfo
->i_location
);
568 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
569 cpu_to_le32(iinfo
->i_unique
& 0x00000000FFFFFFFFUL
);
570 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
571 dir
->i_ctime
= dir
->i_mtime
= current_fs_time(dir
->i_sb
);
572 mark_inode_dirty(dir
);
573 if (fibh
.sbh
!= fibh
.ebh
)
576 unlock_new_inode(inode
);
577 d_instantiate(dentry
, inode
);
582 static int udf_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
585 struct inode
*inode
= udf_new_inode(dir
, mode
);
588 return PTR_ERR(inode
);
590 if (UDF_I(inode
)->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
591 inode
->i_data
.a_ops
= &udf_adinicb_aops
;
593 inode
->i_data
.a_ops
= &udf_aops
;
594 inode
->i_op
= &udf_file_inode_operations
;
595 inode
->i_fop
= &udf_file_operations
;
596 mark_inode_dirty(inode
);
598 return udf_add_nondir(dentry
, inode
);
601 static int udf_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
603 struct inode
*inode
= udf_new_inode(dir
, mode
);
606 return PTR_ERR(inode
);
608 if (UDF_I(inode
)->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
609 inode
->i_data
.a_ops
= &udf_adinicb_aops
;
611 inode
->i_data
.a_ops
= &udf_aops
;
612 inode
->i_op
= &udf_file_inode_operations
;
613 inode
->i_fop
= &udf_file_operations
;
614 mark_inode_dirty(inode
);
615 d_tmpfile(dentry
, inode
);
616 unlock_new_inode(inode
);
620 static int udf_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
625 if (!old_valid_dev(rdev
))
628 inode
= udf_new_inode(dir
, mode
);
630 return PTR_ERR(inode
);
632 init_special_inode(inode
, mode
, rdev
);
633 return udf_add_nondir(dentry
, inode
);
636 static int udf_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
639 struct udf_fileident_bh fibh
;
640 struct fileIdentDesc cfi
, *fi
;
642 struct udf_inode_info
*dinfo
= UDF_I(dir
);
643 struct udf_inode_info
*iinfo
;
645 inode
= udf_new_inode(dir
, S_IFDIR
| mode
);
647 return PTR_ERR(inode
);
649 iinfo
= UDF_I(inode
);
650 inode
->i_op
= &udf_dir_inode_operations
;
651 inode
->i_fop
= &udf_dir_operations
;
652 fi
= udf_add_entry(inode
, NULL
, &fibh
, &cfi
, &err
);
654 inode_dec_link_count(inode
);
655 unlock_new_inode(inode
);
660 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
661 cfi
.icb
.extLocation
= cpu_to_lelb(dinfo
->i_location
);
662 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
663 cpu_to_le32(dinfo
->i_unique
& 0x00000000FFFFFFFFUL
);
664 cfi
.fileCharacteristics
=
665 FID_FILE_CHAR_DIRECTORY
| FID_FILE_CHAR_PARENT
;
666 udf_write_fi(inode
, &cfi
, fi
, &fibh
, NULL
, NULL
);
668 mark_inode_dirty(inode
);
670 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
673 mark_inode_dirty(inode
);
674 unlock_new_inode(inode
);
678 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
679 cfi
.icb
.extLocation
= cpu_to_lelb(iinfo
->i_location
);
680 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
681 cpu_to_le32(iinfo
->i_unique
& 0x00000000FFFFFFFFUL
);
682 cfi
.fileCharacteristics
|= FID_FILE_CHAR_DIRECTORY
;
683 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
685 dir
->i_ctime
= dir
->i_mtime
= current_fs_time(dir
->i_sb
);
686 mark_inode_dirty(dir
);
687 unlock_new_inode(inode
);
688 d_instantiate(dentry
, inode
);
689 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
;
705 struct kernel_lb_addr eloc
;
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(struct short_ad
);
723 else if (dinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
724 epos
.offset
-= sizeof(struct 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
= d_inode(dentry
);
771 struct udf_fileident_bh fibh
;
772 struct fileIdentDesc
*fi
, cfi
;
773 struct kernel_lb_addr tloc
;
776 fi
= udf_find_entry(dir
, &dentry
->d_name
, &fibh
, &cfi
);
781 tloc
= lelb_to_cpu(cfi
.icb
.extLocation
);
782 if (udf_get_lb_pblock(dir
->i_sb
, &tloc
, 0) != inode
->i_ino
)
785 if (!empty_dir(inode
))
787 retval
= udf_delete_entry(dir
, fi
, &fibh
, &cfi
);
790 if (inode
->i_nlink
!= 2)
791 udf_warn(inode
->i_sb
, "empty directory has nlink != 2 (%d)\n",
795 inode_dec_link_count(dir
);
796 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
=
797 current_fs_time(dir
->i_sb
);
798 mark_inode_dirty(dir
);
801 if (fibh
.sbh
!= fibh
.ebh
)
809 static int udf_unlink(struct inode
*dir
, struct dentry
*dentry
)
812 struct inode
*inode
= d_inode(dentry
);
813 struct udf_fileident_bh fibh
;
814 struct fileIdentDesc
*fi
;
815 struct fileIdentDesc cfi
;
816 struct kernel_lb_addr tloc
;
819 fi
= udf_find_entry(dir
, &dentry
->d_name
, &fibh
, &cfi
);
824 tloc
= lelb_to_cpu(cfi
.icb
.extLocation
);
825 if (udf_get_lb_pblock(dir
->i_sb
, &tloc
, 0) != inode
->i_ino
)
828 if (!inode
->i_nlink
) {
829 udf_debug("Deleting nonexistent file (%lu), %d\n",
830 inode
->i_ino
, inode
->i_nlink
);
833 retval
= udf_delete_entry(dir
, fi
, &fibh
, &cfi
);
836 dir
->i_ctime
= dir
->i_mtime
= current_fs_time(dir
->i_sb
);
837 mark_inode_dirty(dir
);
838 inode_dec_link_count(inode
);
839 inode
->i_ctime
= dir
->i_ctime
;
843 if (fibh
.sbh
!= fibh
.ebh
)
851 static int udf_symlink(struct inode
*dir
, struct dentry
*dentry
,
854 struct inode
*inode
= udf_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
855 struct pathComponent
*pc
;
856 const char *compstart
;
857 struct extent_position epos
= {};
858 int eoffset
, elen
= 0;
862 unsigned char *name
= NULL
;
864 struct udf_inode_info
*iinfo
;
865 struct super_block
*sb
= dir
->i_sb
;
868 return PTR_ERR(inode
);
870 iinfo
= UDF_I(inode
);
871 down_write(&iinfo
->i_data_sem
);
872 name
= kmalloc(UDF_NAME_LEN
, GFP_NOFS
);
878 inode
->i_data
.a_ops
= &udf_symlink_aops
;
879 inode
->i_op
= &udf_symlink_inode_operations
;
881 if (iinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_IN_ICB
) {
882 struct kernel_lb_addr eloc
;
885 block
= udf_new_block(sb
, inode
,
886 iinfo
->i_location
.partitionReferenceNum
,
887 iinfo
->i_location
.logicalBlockNum
, &err
);
890 epos
.block
= iinfo
->i_location
;
891 epos
.offset
= udf_file_entry_alloc_offset(inode
);
893 eloc
.logicalBlockNum
= block
;
894 eloc
.partitionReferenceNum
=
895 iinfo
->i_location
.partitionReferenceNum
;
896 bsize
= sb
->s_blocksize
;
897 iinfo
->i_lenExtents
= bsize
;
898 udf_add_aext(inode
, &epos
, &eloc
, bsize
, 0);
901 block
= udf_get_pblock(sb
, block
,
902 iinfo
->i_location
.partitionReferenceNum
,
904 epos
.bh
= udf_tgetblk(sb
, block
);
905 lock_buffer(epos
.bh
);
906 memset(epos
.bh
->b_data
, 0x00, bsize
);
907 set_buffer_uptodate(epos
.bh
);
908 unlock_buffer(epos
.bh
);
909 mark_buffer_dirty_inode(epos
.bh
, inode
);
910 ea
= epos
.bh
->b_data
+ udf_ext0_offset(inode
);
912 ea
= iinfo
->i_ext
.i_data
+ iinfo
->i_lenEAttr
;
914 eoffset
= sb
->s_blocksize
- udf_ext0_offset(inode
);
915 pc
= (struct pathComponent
*)ea
;
917 if (*symname
== '/') {
920 } while (*symname
== '/');
922 pc
->componentType
= 1;
923 pc
->lengthComponentIdent
= 0;
924 pc
->componentFileVersionNum
= 0;
925 elen
+= sizeof(struct pathComponent
);
931 if (elen
+ sizeof(struct pathComponent
) > eoffset
)
934 pc
= (struct pathComponent
*)(ea
+ elen
);
940 } while (*symname
&& *symname
!= '/');
942 pc
->componentType
= 5;
943 pc
->lengthComponentIdent
= 0;
944 pc
->componentFileVersionNum
= 0;
945 if (compstart
[0] == '.') {
946 if ((symname
- compstart
) == 1)
947 pc
->componentType
= 4;
948 else if ((symname
- compstart
) == 2 &&
950 pc
->componentType
= 3;
953 if (pc
->componentType
== 5) {
954 namelen
= udf_put_filename(sb
, compstart
, name
,
955 symname
- compstart
);
959 if (elen
+ sizeof(struct pathComponent
) + namelen
>
963 pc
->lengthComponentIdent
= namelen
;
965 memcpy(pc
->componentIdent
, name
, namelen
);
968 elen
+= sizeof(struct pathComponent
) + pc
->lengthComponentIdent
;
973 } while (*symname
== '/');
978 inode
->i_size
= elen
;
979 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
980 iinfo
->i_lenAlloc
= inode
->i_size
;
982 udf_truncate_tail_extent(inode
);
983 mark_inode_dirty(inode
);
984 up_write(&iinfo
->i_data_sem
);
986 err
= udf_add_nondir(dentry
, inode
);
992 up_write(&iinfo
->i_data_sem
);
993 inode_dec_link_count(inode
);
994 unlock_new_inode(inode
);
999 static int udf_link(struct dentry
*old_dentry
, struct inode
*dir
,
1000 struct dentry
*dentry
)
1002 struct inode
*inode
= d_inode(old_dentry
);
1003 struct udf_fileident_bh fibh
;
1004 struct fileIdentDesc cfi
, *fi
;
1007 fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
);
1011 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
1012 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I(inode
)->i_location
);
1013 if (UDF_SB(inode
->i_sb
)->s_lvid_bh
) {
1014 *(__le32
*)((struct allocDescImpUse
*)cfi
.icb
.impUse
)->impUse
=
1015 cpu_to_le32(lvid_get_unique_id(inode
->i_sb
));
1017 udf_write_fi(dir
, &cfi
, fi
, &fibh
, NULL
, NULL
);
1018 if (UDF_I(dir
)->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
1019 mark_inode_dirty(dir
);
1021 if (fibh
.sbh
!= fibh
.ebh
)
1025 inode
->i_ctime
= current_fs_time(inode
->i_sb
);
1026 mark_inode_dirty(inode
);
1027 dir
->i_ctime
= dir
->i_mtime
= current_fs_time(dir
->i_sb
);
1028 mark_inode_dirty(dir
);
1030 d_instantiate(dentry
, inode
);
1035 /* Anybody can rename anything with this: the permission checks are left to the
1036 * higher-level routines.
1038 static int udf_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1039 struct inode
*new_dir
, struct dentry
*new_dentry
)
1041 struct inode
*old_inode
= d_inode(old_dentry
);
1042 struct inode
*new_inode
= d_inode(new_dentry
);
1043 struct udf_fileident_bh ofibh
, nfibh
;
1044 struct fileIdentDesc
*ofi
= NULL
, *nfi
= NULL
, *dir_fi
= NULL
;
1045 struct fileIdentDesc ocfi
, ncfi
;
1046 struct buffer_head
*dir_bh
= NULL
;
1047 int retval
= -ENOENT
;
1048 struct kernel_lb_addr tloc
;
1049 struct udf_inode_info
*old_iinfo
= UDF_I(old_inode
);
1051 ofi
= udf_find_entry(old_dir
, &old_dentry
->d_name
, &ofibh
, &ocfi
);
1053 if (ofibh
.sbh
!= ofibh
.ebh
)
1057 tloc
= lelb_to_cpu(ocfi
.icb
.extLocation
);
1058 if (!ofi
|| udf_get_lb_pblock(old_dir
->i_sb
, &tloc
, 0)
1059 != old_inode
->i_ino
)
1062 nfi
= udf_find_entry(new_dir
, &new_dentry
->d_name
, &nfibh
, &ncfi
);
1065 if (nfibh
.sbh
!= nfibh
.ebh
)
1071 if (S_ISDIR(old_inode
->i_mode
)) {
1072 int offset
= udf_ext0_offset(old_inode
);
1075 retval
= -ENOTEMPTY
;
1076 if (!empty_dir(new_inode
))
1080 if (old_iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
1081 dir_fi
= udf_get_fileident(
1082 old_iinfo
->i_ext
.i_data
-
1084 sizeof(struct extendedFileEntry
) :
1085 sizeof(struct fileEntry
)),
1086 old_inode
->i_sb
->s_blocksize
, &offset
);
1088 dir_bh
= udf_bread(old_inode
, 0, 0, &retval
);
1091 dir_fi
= udf_get_fileident(dir_bh
->b_data
,
1092 old_inode
->i_sb
->s_blocksize
, &offset
);
1096 tloc
= lelb_to_cpu(dir_fi
->icb
.extLocation
);
1097 if (udf_get_lb_pblock(old_inode
->i_sb
, &tloc
, 0) !=
1102 nfi
= udf_add_entry(new_dir
, new_dentry
, &nfibh
, &ncfi
,
1109 * Like most other Unix systems, set the ctime for inodes on a
1112 old_inode
->i_ctime
= current_fs_time(old_inode
->i_sb
);
1113 mark_inode_dirty(old_inode
);
1118 ncfi
.fileVersionNum
= ocfi
.fileVersionNum
;
1119 ncfi
.fileCharacteristics
= ocfi
.fileCharacteristics
;
1120 memcpy(&(ncfi
.icb
), &(ocfi
.icb
), sizeof(struct long_ad
));
1121 udf_write_fi(new_dir
, &ncfi
, nfi
, &nfibh
, NULL
, NULL
);
1123 /* The old fid may have moved - find it again */
1124 ofi
= udf_find_entry(old_dir
, &old_dentry
->d_name
, &ofibh
, &ocfi
);
1125 udf_delete_entry(old_dir
, ofi
, &ofibh
, &ocfi
);
1128 new_inode
->i_ctime
= current_fs_time(new_inode
->i_sb
);
1129 inode_dec_link_count(new_inode
);
1131 old_dir
->i_ctime
= old_dir
->i_mtime
= current_fs_time(old_dir
->i_sb
);
1132 new_dir
->i_ctime
= new_dir
->i_mtime
= current_fs_time(new_dir
->i_sb
);
1133 mark_inode_dirty(old_dir
);
1134 mark_inode_dirty(new_dir
);
1137 dir_fi
->icb
.extLocation
= cpu_to_lelb(UDF_I(new_dir
)->i_location
);
1138 udf_update_tag((char *)dir_fi
,
1139 (sizeof(struct fileIdentDesc
) +
1140 le16_to_cpu(dir_fi
->lengthOfImpUse
) + 3) & ~3);
1141 if (old_iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
1142 mark_inode_dirty(old_inode
);
1144 mark_buffer_dirty_inode(dir_bh
, old_inode
);
1146 inode_dec_link_count(old_dir
);
1148 inode_dec_link_count(new_inode
);
1151 mark_inode_dirty(new_dir
);
1156 if (ofibh
.sbh
!= ofibh
.ebh
)
1166 if (nfibh
.sbh
!= nfibh
.ebh
)
1174 static struct dentry
*udf_get_parent(struct dentry
*child
)
1176 struct kernel_lb_addr tloc
;
1177 struct inode
*inode
= NULL
;
1178 struct qstr dotdot
= QSTR_INIT("..", 2);
1179 struct fileIdentDesc cfi
;
1180 struct udf_fileident_bh fibh
;
1182 if (!udf_find_entry(d_inode(child
), &dotdot
, &fibh
, &cfi
))
1183 return ERR_PTR(-EACCES
);
1185 if (fibh
.sbh
!= fibh
.ebh
)
1189 tloc
= lelb_to_cpu(cfi
.icb
.extLocation
);
1190 inode
= udf_iget(d_inode(child
)->i_sb
, &tloc
);
1192 return ERR_CAST(inode
);
1194 return d_obtain_alias(inode
);
1198 static struct dentry
*udf_nfs_get_inode(struct super_block
*sb
, u32 block
,
1199 u16 partref
, __u32 generation
)
1201 struct inode
*inode
;
1202 struct kernel_lb_addr loc
;
1205 return ERR_PTR(-ESTALE
);
1207 loc
.logicalBlockNum
= block
;
1208 loc
.partitionReferenceNum
= partref
;
1209 inode
= udf_iget(sb
, &loc
);
1212 return ERR_CAST(inode
);
1214 if (generation
&& inode
->i_generation
!= generation
) {
1216 return ERR_PTR(-ESTALE
);
1218 return d_obtain_alias(inode
);
1221 static struct dentry
*udf_fh_to_dentry(struct super_block
*sb
,
1222 struct fid
*fid
, int fh_len
, int fh_type
)
1224 if ((fh_len
!= 3 && fh_len
!= 5) ||
1225 (fh_type
!= FILEID_UDF_WITH_PARENT
&&
1226 fh_type
!= FILEID_UDF_WITHOUT_PARENT
))
1229 return udf_nfs_get_inode(sb
, fid
->udf
.block
, fid
->udf
.partref
,
1230 fid
->udf
.generation
);
1233 static struct dentry
*udf_fh_to_parent(struct super_block
*sb
,
1234 struct fid
*fid
, int fh_len
, int fh_type
)
1236 if (fh_len
!= 5 || fh_type
!= FILEID_UDF_WITH_PARENT
)
1239 return udf_nfs_get_inode(sb
, fid
->udf
.parent_block
,
1240 fid
->udf
.parent_partref
,
1241 fid
->udf
.parent_generation
);
1243 static int udf_encode_fh(struct inode
*inode
, __u32
*fh
, int *lenp
,
1244 struct inode
*parent
)
1247 struct kernel_lb_addr location
= UDF_I(inode
)->i_location
;
1248 struct fid
*fid
= (struct fid
*)fh
;
1249 int type
= FILEID_UDF_WITHOUT_PARENT
;
1251 if (parent
&& (len
< 5)) {
1253 return FILEID_INVALID
;
1254 } else if (len
< 3) {
1256 return FILEID_INVALID
;
1260 fid
->udf
.block
= location
.logicalBlockNum
;
1261 fid
->udf
.partref
= location
.partitionReferenceNum
;
1262 fid
->udf
.parent_partref
= 0;
1263 fid
->udf
.generation
= inode
->i_generation
;
1266 location
= UDF_I(parent
)->i_location
;
1267 fid
->udf
.parent_block
= location
.logicalBlockNum
;
1268 fid
->udf
.parent_partref
= location
.partitionReferenceNum
;
1269 fid
->udf
.parent_generation
= inode
->i_generation
;
1271 type
= FILEID_UDF_WITH_PARENT
;
1277 const struct export_operations udf_export_ops
= {
1278 .encode_fh
= udf_encode_fh
,
1279 .fh_to_dentry
= udf_fh_to_dentry
,
1280 .fh_to_parent
= udf_fh_to_parent
,
1281 .get_parent
= udf_get_parent
,
1284 const struct inode_operations udf_dir_inode_operations
= {
1285 .lookup
= udf_lookup
,
1286 .create
= udf_create
,
1288 .unlink
= udf_unlink
,
1289 .symlink
= udf_symlink
,
1293 .rename
= udf_rename
,
1294 .tmpfile
= udf_tmpfile
,
1296 const struct inode_operations udf_symlink_inode_operations
= {
1297 .readlink
= generic_readlink
,
1298 .follow_link
= page_follow_link_light
,
1299 .put_link
= page_put_link
,