5 * Inode name handling routines for the OSTA-UDF(tm) filesystem.
8 * E-mail regarding any portion of the Linux UDF file system should be
9 * directed to the development team mailing list (run by majordomo):
10 * linux_udf@hootie.lvld.hp.com
13 * This file is distributed under the terms of the GNU General Public
14 * License (GPL). Copies of the GPL can be obtained from:
15 * ftp://prep.ai.mit.edu/pub/gnu/GPL
16 * Each contributing author retains all rights to their own work.
18 * (C) 1998-1999 Ben Fennema
19 * (C) 1999 Stelias Computing Inc
23 * 12/12/98 blf Created. Split out the lookup code from dir.c
24 * 04/19/99 blf link, mknod, symlink support
28 #if defined(__linux__) && defined(__KERNEL__)
29 #include <linux/version.h>
32 #include <linux/string.h>
33 #include <linux/errno.h>
35 #include <linux/malloc.h>
36 #include <linux/udf_fs.h>
41 static inline int udf_match(int len
, const char * const name
, struct qstr
*qs
)
45 return !memcmp(name
, qs
->name
, len
);
48 int udf_write_fi(struct FileIdentDesc
*cfi
, struct FileIdentDesc
*sfi
,
49 struct udf_fileident_bh
*fibh
,
50 Uint8
*impuse
, Uint8
*fileident
)
52 Uint16 crclen
= fibh
->eoffset
- fibh
->soffset
- sizeof(tag
);
57 Uint16 liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
58 Uint8 lfi
= cfi
->lengthFileIdent
;
59 int padlen
= fibh
->eoffset
- fibh
->soffset
- liu
- lfi
-
60 sizeof(struct FileIdentDesc
);
63 offset
= fibh
->soffset
+ sizeof(struct FileIdentDesc
);
68 memcpy((Uint8
*)sfi
->impUse
, impuse
, liu
);
70 memcpy(fibh
->ebh
->b_data
+ offset
, impuse
, liu
);
73 memcpy((Uint8
*)sfi
->impUse
, impuse
, -offset
);
74 memcpy(fibh
->ebh
->b_data
, impuse
- offset
, liu
+ offset
);
83 memcpy((Uint8
*)sfi
->fileIdent
+ liu
, fileident
, lfi
);
85 memcpy(fibh
->ebh
->b_data
+ offset
, fileident
, lfi
);
88 memcpy((Uint8
*)sfi
->fileIdent
+ liu
, fileident
, -offset
);
89 memcpy(fibh
->ebh
->b_data
, fileident
- offset
, lfi
+ offset
);
95 if (offset
+ padlen
< 0)
96 memset((Uint8
*)sfi
->padding
+ liu
+ lfi
, 0x00, padlen
);
98 memset(fibh
->ebh
->b_data
+ offset
, 0x00, padlen
);
101 memset((Uint8
*)sfi
->padding
+ liu
+ lfi
, 0x00, -offset
);
102 memset(fibh
->ebh
->b_data
, 0x00, padlen
+ offset
);
105 crc
= udf_crc((Uint8
*)cfi
+ sizeof(tag
), sizeof(struct FileIdentDesc
) -
108 if (fibh
->sbh
== fibh
->ebh
)
109 crc
= udf_crc((Uint8
*)sfi
->impUse
,
110 crclen
+ sizeof(tag
) - sizeof(struct FileIdentDesc
), crc
);
111 else if (sizeof(struct FileIdentDesc
) >= -fibh
->soffset
)
112 crc
= udf_crc(fibh
->ebh
->b_data
+ sizeof(struct FileIdentDesc
) + fibh
->soffset
,
113 crclen
+ sizeof(tag
) - sizeof(struct FileIdentDesc
), crc
);
116 crc
= udf_crc((Uint8
*)sfi
->impUse
,
117 -fibh
->soffset
- sizeof(struct FileIdentDesc
), crc
);
118 crc
= udf_crc(fibh
->ebh
->b_data
, fibh
->eoffset
, crc
);
121 cfi
->descTag
.descCRC
= cpu_to_le32(crc
);
122 cfi
->descTag
.descCRCLength
= cpu_to_le16(crclen
);
126 checksum
+= ((Uint8
*)&cfi
->descTag
)[i
];
128 cfi
->descTag
.tagChecksum
= checksum
;
129 if (sizeof(struct FileIdentDesc
) <= -fibh
->soffset
)
130 memcpy((Uint8
*)sfi
, (Uint8
*)cfi
, sizeof(struct FileIdentDesc
));
133 memcpy((Uint8
*)sfi
, (Uint8
*)cfi
, -fibh
->soffset
);
134 memcpy(fibh
->ebh
->b_data
, (Uint8
*)cfi
- fibh
->soffset
,
135 sizeof(struct FileIdentDesc
) + fibh
->soffset
);
138 if (fibh
->sbh
!= fibh
->ebh
)
139 mark_buffer_dirty(fibh
->ebh
, 1);
140 mark_buffer_dirty(fibh
->sbh
, 1);
144 static struct FileIdentDesc
*
145 udf_find_entry(struct inode
*dir
, struct dentry
*dentry
,
146 struct udf_fileident_bh
*fibh
,
147 struct FileIdentDesc
*cfi
)
149 struct FileIdentDesc
*fi
=NULL
;
156 int size
= (UDF_I_EXT0OFFS(dir
) + dir
->i_size
) >> 2;
158 Uint32 extoffset
, elen
, offset
;
159 struct buffer_head
*bh
= NULL
;
164 f_pos
= (UDF_I_EXT0OFFS(dir
) >> 2);
166 fibh
->soffset
= fibh
->eoffset
= (f_pos
& ((dir
->i_sb
->s_blocksize
- 1) >> 2)) << 2;
167 if (inode_bmap(dir
, f_pos
>> (dir
->i_sb
->s_blocksize_bits
- 2),
168 &bloc
, &extoffset
, &eloc
, &elen
, &offset
, &bh
) == EXTENT_RECORDED_ALLOCATED
)
170 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, offset
);
171 if ((++offset
<< dir
->i_sb
->s_blocksize_bits
) < elen
)
173 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_SHORT
)
174 extoffset
-= sizeof(short_ad
);
175 else if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_LONG
)
176 extoffset
-= sizeof(long_ad
);
183 udf_release_data(bh
);
187 if (!(fibh
->sbh
= fibh
->ebh
= udf_tread(dir
->i_sb
, block
, dir
->i_sb
->s_blocksize
)))
189 udf_debug("udf_tread failed: block=%d\n", block
);
190 udf_release_data(bh
);
194 while ( (f_pos
< size
) )
196 fi
= udf_fileident_read(dir
, &f_pos
, fibh
, cfi
, &bloc
, &extoffset
, &offset
, &bh
);
200 if (fibh
->sbh
!= fibh
->ebh
)
201 udf_release_data(fibh
->ebh
);
202 udf_release_data(fibh
->sbh
);
203 udf_release_data(bh
);
207 liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
208 lfi
= cfi
->lengthFileIdent
;
210 if (fibh
->sbh
== fibh
->ebh
)
212 nameptr
= fi
->fileIdent
+ liu
;
216 int poffset
; /* Unpaded ending offset */
218 poffset
= fibh
->soffset
+ sizeof(struct FileIdentDesc
) + liu
+ lfi
;
221 nameptr
= (Uint8
*)(fibh
->ebh
->b_data
+ poffset
- lfi
);
225 memcpy(nameptr
, fi
->fileIdent
+ liu
, lfi
- poffset
);
226 memcpy(nameptr
+ lfi
- poffset
, fibh
->ebh
->b_data
, poffset
);
230 if ( (cfi
->fileCharacteristics
& FILE_DELETED
) != 0 )
232 if ( !IS_UNDELETE(dir
->i_sb
) )
236 if ( (cfi
->fileCharacteristics
& FILE_HIDDEN
) != 0 )
238 if ( !IS_UNHIDE(dir
->i_sb
) )
245 if ((flen
= udf_get_filename(nameptr
, fname
, lfi
)))
247 if (udf_match(flen
, fname
, &(dentry
->d_name
)))
249 udf_release_data(bh
);
254 if (fibh
->sbh
!= fibh
->ebh
)
255 udf_release_data(fibh
->ebh
);
256 udf_release_data(fibh
->sbh
);
257 udf_release_data(bh
);
265 * Look-up the inode for a given name.
268 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
269 * available for a directory. The filesystem is useless if this routine is
270 * not available for at least the filesystem's root directory.
272 * This routine is passed an incomplete dentry - it must be completed by
273 * calling d_add(dentry, inode). If the name does not exist, then the
274 * specified inode must be set to null. An error should only be returned
275 * when the lookup fails for a reason other than the name not existing.
276 * Note that the directory inode semaphore is held during the call.
278 * Refer to lookup_dentry() in fs/namei.c
279 * lookup_dentry() -> lookup() -> real_lookup() -> .
282 * dir Pointer to inode of parent directory.
283 * dentry Pointer to dentry to complete.
286 * <return> Zero on success.
289 * July 1, 1997 - Andrew E. Mileski
290 * Written, tested, and released.
294 udf_lookup(struct inode
*dir
, struct dentry
*dentry
)
296 struct inode
*inode
= NULL
;
297 struct FileIdentDesc cfi
, *fi
;
298 struct udf_fileident_bh fibh
;
301 /* temporary shorthand for specifying files by inode number */
302 if (!strncmp(dentry
->d_name
.name
, ".B=", 3) )
304 lb_addr lb
= { 0, simple_strtoul(dentry
->d_name
.name
+3, NULL
, 0) };
305 inode
= udf_iget(dir
->i_sb
, lb
);
307 return ERR_PTR(-EACCES
);
310 #endif /* UDF_RECOVERY */
312 if ((fi
= udf_find_entry(dir
, dentry
, &fibh
, &cfi
)))
314 if (fibh
.sbh
!= fibh
.ebh
)
315 udf_release_data(fibh
.ebh
);
316 udf_release_data(fibh
.sbh
);
318 inode
= udf_iget(dir
->i_sb
, lelb_to_cpu(cfi
.icb
.extLocation
));
320 return ERR_PTR(-EACCES
);
322 d_add(dentry
, inode
);
326 static struct FileIdentDesc
*
327 udf_add_entry(struct inode
*dir
, struct dentry
*dentry
,
328 struct udf_fileident_bh
*fibh
,
329 struct FileIdentDesc
*cfi
, int *err
)
331 struct super_block
*sb
;
332 struct FileIdentDesc
*fi
=NULL
;
333 struct ustr unifilename
;
334 char name
[UDF_NAME_LEN
], fname
[UDF_NAME_LEN
];
339 int size
= (UDF_I_EXT0OFFS(dir
) + dir
->i_size
) >> 2;
345 Uint32 extoffset
, elen
, offset
;
346 struct buffer_head
*bh
= NULL
;
349 if (!dir
|| !dir
->i_nlink
)
353 if (!dentry
->d_name
.len
)
356 if (dir
->i_size
== 0)
362 if ( !(udf_char_to_ustr(&unifilename
, dentry
->d_name
.name
, dentry
->d_name
.len
)) )
364 *err
= -ENAMETOOLONG
;
368 if ( !(namelen
= udf_UTF8toCS0(name
, &unifilename
, UDF_NAME_LEN
)) )
371 nfidlen
= (sizeof(struct FileIdentDesc
) + 0 + namelen
+ 3) & ~3;
373 f_pos
= (UDF_I_EXT0OFFS(dir
) >> 2);
375 fibh
->soffset
= fibh
->eoffset
= (f_pos
& ((dir
->i_sb
->s_blocksize
- 1) >> 2)) << 2;
376 if (inode_bmap(dir
, f_pos
>> (dir
->i_sb
->s_blocksize_bits
- 2),
377 &bloc
, &extoffset
, &eloc
, &elen
, &offset
, &bh
) == EXTENT_RECORDED_ALLOCATED
)
379 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, offset
);
380 if ((++offset
<< dir
->i_sb
->s_blocksize_bits
) < elen
)
382 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_SHORT
)
383 extoffset
-= sizeof(short_ad
);
384 else if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_LONG
)
385 extoffset
-= sizeof(long_ad
);
392 udf_release_data(bh
);
396 if (!(fibh
->sbh
= fibh
->ebh
= udf_tread(dir
->i_sb
, block
, dir
->i_sb
->s_blocksize
)))
399 block
= UDF_I_LOCATION(dir
).logicalBlockNum
;
401 while ( (f_pos
< size
) )
403 fi
= udf_fileident_read(dir
, &f_pos
, fibh
, cfi
, &bloc
, &extoffset
, &offset
, &bh
);
407 if (fibh
->sbh
!= fibh
->ebh
)
408 udf_release_data(fibh
->ebh
);
409 udf_release_data(fibh
->sbh
);
410 udf_release_data(bh
);
414 liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
415 lfi
= cfi
->lengthFileIdent
;
417 if (fibh
->sbh
== fibh
->ebh
)
418 nameptr
= fi
->fileIdent
+ liu
;
421 int poffset
; /* Unpaded ending offset */
423 poffset
= fibh
->soffset
+ sizeof(struct FileIdentDesc
) + liu
+ lfi
;
426 nameptr
= (char *)(fibh
->ebh
->b_data
+ poffset
- lfi
);
430 memcpy(nameptr
, fi
->fileIdent
+ liu
, lfi
- poffset
);
431 memcpy(nameptr
+ lfi
- poffset
, fibh
->ebh
->b_data
, poffset
);
435 if ( (cfi
->fileCharacteristics
& FILE_DELETED
) != 0 )
437 if (((sizeof(struct FileIdentDesc
) + liu
+ lfi
+ 3) & ~3) == nfidlen
)
439 udf_release_data(bh
);
440 cfi
->descTag
.tagSerialNum
= cpu_to_le16(1);
441 cfi
->fileVersionNum
= cpu_to_le16(1);
442 cfi
->fileCharacteristics
= 0;
443 cfi
->lengthFileIdent
= namelen
;
444 cfi
->lengthOfImpUse
= cpu_to_le16(0);
445 if (!udf_write_fi(cfi
, fi
, fibh
, NULL
, name
))
455 if ((flen
= udf_get_filename(nameptr
, fname
, lfi
)))
457 if (udf_match(flen
, fname
, &(dentry
->d_name
)))
459 if (fibh
->sbh
!= fibh
->ebh
)
460 udf_release_data(fibh
->ebh
);
461 udf_release_data(fibh
->sbh
);
462 udf_release_data(bh
);
471 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_IN_ICB
&&
472 sb
->s_blocksize
- fibh
->eoffset
< nfidlen
)
474 udf_release_data(bh
);
476 fibh
->soffset
-= UDF_I_EXT0OFFS(dir
);
477 fibh
->eoffset
-= UDF_I_EXT0OFFS(dir
);
478 f_pos
-= (UDF_I_EXT0OFFS(dir
) >> 2);
479 if (fibh
->sbh
!= fibh
->ebh
)
480 udf_release_data(fibh
->ebh
);
481 udf_release_data(fibh
->sbh
);
482 if (!(fibh
->sbh
= fibh
->ebh
= udf_expand_adinicb(dir
, &block
, 1, err
)))
484 bloc
= UDF_I_LOCATION(dir
);
485 extoffset
= udf_file_entry_alloc_offset(dir
);
489 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_SHORT
)
490 extoffset
-= sizeof(short_ad
);
491 else if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_LONG
)
492 extoffset
-= sizeof(long_ad
);
495 dir
->i_size
+= nfidlen
;
496 if (sb
->s_blocksize
- fibh
->eoffset
>= nfidlen
)
498 fibh
->soffset
= fibh
->eoffset
;
499 fibh
->eoffset
+= nfidlen
;
500 if (fibh
->sbh
!= fibh
->ebh
)
502 udf_release_data(fibh
->sbh
);
503 fibh
->sbh
= fibh
->ebh
;
506 if (UDF_I_ALLOCTYPE(dir
) != ICB_FLAG_AD_IN_ICB
)
508 Uint32 lextoffset
= extoffset
;
509 if (udf_next_aext(dir
, &bloc
, &extoffset
, &eloc
, &elen
, &bh
, 1) !=
510 EXTENT_RECORDED_ALLOCATED
)
512 udf_release_data(bh
);
513 udf_release_data(fibh
->sbh
);
518 if (elen
& (sb
->s_blocksize
- 1))
520 block
= eloc
.logicalBlockNum
+ ((elen
- 1) >>
521 dir
->i_sb
->s_blocksize_bits
);
522 elen
= (EXTENT_RECORDED_ALLOCATED
<< 30) | elen
;
523 udf_write_aext(dir
, bloc
, &lextoffset
, eloc
, elen
, &bh
, 1);
527 block
= UDF_I_LOCATION(dir
).logicalBlockNum
;
529 fi
= (struct FileIdentDesc
*)(fibh
->sbh
->b_data
+ fibh
->soffset
);
533 Uint32 lextoffset
= extoffset
;
535 fibh
->soffset
= fibh
->eoffset
- sb
->s_blocksize
;
536 fibh
->eoffset
+= nfidlen
- sb
->s_blocksize
;
537 if (fibh
->sbh
!= fibh
->ebh
)
539 udf_release_data(fibh
->sbh
);
540 fibh
->sbh
= fibh
->ebh
;
543 if (udf_next_aext(dir
, &bloc
, &extoffset
, &eloc
, &elen
, &bh
, 1) !=
544 EXTENT_RECORDED_ALLOCATED
)
546 udf_release_data(bh
);
547 udf_release_data(fibh
->sbh
);
552 elen
= ((elen
+ sb
->s_blocksize
- 1) & ~(sb
->s_blocksize
- 1));
553 block
= eloc
.logicalBlockNum
+ ((elen
- 1) >>
554 dir
->i_sb
->s_blocksize_bits
);
555 elen
= (EXTENT_RECORDED_ALLOCATED
<< 30) | elen
;
556 udf_write_aext(dir
, bloc
, &lextoffset
, eloc
, elen
, &bh
, 0);
560 if (!(fibh
->ebh
= udf_bread(dir
, f_pos
>> (dir
->i_sb
->s_blocksize_bits
- 2), 1, err
)))
562 udf_release_data(bh
);
563 udf_release_data(fibh
->sbh
);
566 if (!(fibh
->soffset
))
568 if (udf_next_aext(dir
, &bloc
, &lextoffset
, &eloc
, &elen
, &bh
, 1) ==
569 EXTENT_RECORDED_ALLOCATED
)
571 block
= eloc
.logicalBlockNum
+ ((elen
- 1) >>
572 dir
->i_sb
->s_blocksize_bits
);
578 fi
= (struct FileIdentDesc
*)(fibh
->sbh
->b_data
+ sb
->s_blocksize
+ fibh
->soffset
);
581 memset(cfi
, 0, sizeof(struct FileIdentDesc
));
582 udf_new_tag((char *)cfi
, TID_FILE_IDENT_DESC
, 2, 1, block
, sizeof(tag
));
583 cfi
->fileVersionNum
= cpu_to_le16(1);
584 cfi
->lengthFileIdent
= namelen
;
585 cfi
->lengthOfImpUse
= cpu_to_le16(0);
586 if (!udf_write_fi(cfi
, fi
, fibh
, NULL
, name
))
588 udf_release_data(bh
);
589 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_IN_ICB
)
590 UDF_I_LENALLOC(dir
) += nfidlen
;
591 dir
->i_version
= ++event
;
592 mark_inode_dirty(dir
);
597 udf_release_data(bh
);
598 dir
->i_size
-= nfidlen
;
599 if (fibh
->sbh
!= fibh
->ebh
)
600 udf_release_data(fibh
->ebh
);
601 udf_release_data(fibh
->sbh
);
606 static int udf_delete_entry(struct FileIdentDesc
*fi
,
607 struct udf_fileident_bh
*fibh
,
608 struct FileIdentDesc
*cfi
)
610 cfi
->fileCharacteristics
|= FILE_DELETED
;
611 return udf_write_fi(cfi
, fi
, fibh
, NULL
, NULL
);
614 int udf_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
616 struct udf_fileident_bh fibh
;
618 struct FileIdentDesc cfi
, *fi
;
621 inode
= udf_new_inode(dir
, mode
, &err
);
625 inode
->i_op
= &udf_file_inode_operations
;
626 inode
->i_mode
= mode
;
627 mark_inode_dirty(inode
);
629 if (!(fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
)))
631 udf_debug("udf_add_entry failure!\n");
633 mark_inode_dirty(inode
);
637 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
638 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
639 *(Uint32
*)((struct ADImpUse
*)cfi
.icb
.impUse
)->impUse
=
640 cpu_to_le32(UDF_I_UNIQUE(inode
) & 0x00000000FFFFFFFFUL
);
641 udf_write_fi(&cfi
, fi
, &fibh
, NULL
, NULL
);
642 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_IN_ICB
)
644 mark_inode_dirty(dir
);
645 dir
->i_version
= ++event
;
647 if (fibh
.sbh
!= fibh
.ebh
)
648 udf_release_data(fibh
.ebh
);
649 udf_release_data(fibh
.sbh
);
650 d_instantiate(dentry
, inode
);
654 int udf_mknod(struct inode
* dir
, struct dentry
* dentry
, int mode
, int rdev
)
656 struct inode
* inode
;
657 struct udf_fileident_bh fibh
;
659 struct FileIdentDesc cfi
, *fi
;
662 inode
= udf_new_inode(dir
, mode
, &err
);
666 inode
->i_uid
= current
->fsuid
;
667 inode
->i_mode
= mode
;
669 if (!(fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
)))
671 udf_debug("udf_add_entry failure!\n");
673 mark_inode_dirty(inode
);
677 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
678 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
679 *(Uint32
*)((struct ADImpUse
*)cfi
.icb
.impUse
)->impUse
=
680 cpu_to_le32(UDF_I_UNIQUE(inode
) & 0x00000000FFFFFFFFUL
);
681 udf_write_fi(&cfi
, fi
, &fibh
, NULL
, NULL
);
682 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_IN_ICB
)
684 mark_inode_dirty(dir
);
685 dir
->i_version
= ++event
;
687 if (S_ISREG(inode
->i_mode
))
689 inode
->i_op
= &udf_file_inode_operations
;
691 else if (S_ISCHR(inode
->i_mode
))
693 inode
->i_op
= &chrdev_inode_operations
;
695 else if (S_ISBLK(inode
->i_mode
))
697 inode
->i_op
= &blkdev_inode_operations
;
699 else if (S_ISFIFO(inode
->i_mode
))
703 if (S_ISBLK(mode
) || S_ISCHR(mode
))
704 inode
->i_rdev
= to_kdev_t(rdev
);
705 mark_inode_dirty(inode
);
707 if (fibh
.sbh
!= fibh
.ebh
)
708 udf_release_data(fibh
.ebh
);
709 udf_release_data(fibh
.sbh
);
710 d_instantiate(dentry
, inode
);
716 int udf_mkdir(struct inode
* dir
, struct dentry
* dentry
, int mode
)
718 struct inode
* inode
;
719 struct udf_fileident_bh fibh
;
721 struct FileEntry
*fe
;
722 struct FileIdentDesc cfi
, *fi
;
726 if (dir
->i_nlink
>= (256<<sizeof(dir
->i_nlink
))-1)
730 inode
= udf_new_inode(dir
, S_IFDIR
, &err
);
734 inode
->i_op
= &udf_dir_inode_operations
;
735 inode
->i_size
= (sizeof(struct FileIdentDesc
) + 3) & ~3;
736 if (UDF_I_ALLOCTYPE(inode
) == ICB_FLAG_AD_IN_ICB
)
738 UDF_I_EXT0LEN(inode
) = inode
->i_size
;
739 UDF_I_EXT0LOC(inode
) = UDF_I_LOCATION(inode
);
740 UDF_I_LENALLOC(inode
) = inode
->i_size
;
741 loc
= UDF_I_LOCATION(inode
).logicalBlockNum
;
742 fibh
.sbh
= udf_tread(inode
->i_sb
, inode
->i_ino
, inode
->i_sb
->s_blocksize
);
746 fibh
.sbh
= udf_bread (inode
, 0, 1, &err
);
747 loc
= UDF_I_EXT0LOC(inode
).logicalBlockNum
;
753 mark_inode_dirty(inode
);
758 fe
= (struct FileEntry
*)fibh
.sbh
->b_data
;
759 fi
= (struct FileIdentDesc
*)&(fe
->extendedAttr
[UDF_I_LENEATTR(inode
)]);
760 udf_new_tag((char *)&cfi
, TID_FILE_IDENT_DESC
, 2, 1, loc
,
761 sizeof(struct FileIdentDesc
));
762 cfi
.fileVersionNum
= cpu_to_le16(1);
763 cfi
.fileCharacteristics
= FILE_DIRECTORY
| FILE_PARENT
;
764 cfi
.lengthFileIdent
= 0;
765 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
766 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(dir
));
767 *(Uint32
*)((struct ADImpUse
*)cfi
.icb
.impUse
)->impUse
=
768 cpu_to_le32(UDF_I_UNIQUE(dir
) & 0x00000000FFFFFFFFUL
);
769 cfi
.lengthOfImpUse
= cpu_to_le16(0);
771 fibh
.soffset
= sizeof(struct FileEntry
);
772 fibh
.eoffset
= sizeof(struct FileEntry
) + inode
->i_size
;
773 udf_write_fi(&cfi
, fi
, &fibh
, NULL
, NULL
);
774 udf_release_data(fibh
.sbh
);
775 inode
->i_mode
= S_IFDIR
| (mode
& (S_IRWXUGO
|S_ISVTX
) & ~current
->fs
->umask
);
776 if (dir
->i_mode
& S_ISGID
)
777 inode
->i_mode
|= S_ISGID
;
778 mark_inode_dirty(inode
);
780 if (!(fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
)))
782 udf_debug("udf_add_entry failure!\n");
784 mark_inode_dirty(inode
);
788 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
789 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
790 *(Uint32
*)((struct ADImpUse
*)cfi
.icb
.impUse
)->impUse
=
791 cpu_to_le32(UDF_I_UNIQUE(inode
) & 0x00000000FFFFFFFFUL
);
792 cfi
.fileCharacteristics
|= FILE_DIRECTORY
;
793 udf_write_fi(&cfi
, fi
, &fibh
, NULL
, NULL
);
794 dir
->i_version
= ++event
;
796 mark_inode_dirty(dir
);
797 d_instantiate(dentry
, inode
);
798 if (fibh
.sbh
!= fibh
.ebh
)
799 udf_release_data(fibh
.ebh
);
800 udf_release_data(fibh
.sbh
);
806 static int empty_dir(struct inode
*dir
)
808 struct FileIdentDesc
*fi
, cfi
;
809 struct udf_fileident_bh fibh
;
811 int size
= (UDF_I_EXT0OFFS(dir
) + dir
->i_size
) >> 2;
814 Uint32 extoffset
, elen
, offset
;
815 struct buffer_head
*bh
= NULL
;
817 f_pos
= (UDF_I_EXT0OFFS(dir
) >> 2);
819 fibh
.soffset
= fibh
.eoffset
= (f_pos
& ((dir
->i_sb
->s_blocksize
- 1) >> 2)) << 2;
820 if (inode_bmap(dir
, f_pos
>> (dir
->i_sb
->s_blocksize_bits
- 2),
821 &bloc
, &extoffset
, &eloc
, &elen
, &offset
, &bh
) == EXTENT_RECORDED_ALLOCATED
)
823 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, offset
);
824 if ((++offset
<< dir
->i_sb
->s_blocksize_bits
) < elen
)
826 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_SHORT
)
827 extoffset
-= sizeof(short_ad
);
828 else if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_LONG
)
829 extoffset
-= sizeof(long_ad
);
836 udf_release_data(bh
);
840 if (!(fibh
.sbh
= fibh
.ebh
= udf_tread(dir
->i_sb
, block
, dir
->i_sb
->s_blocksize
)))
843 while ( (f_pos
< size
) )
845 fi
= udf_fileident_read(dir
, &f_pos
, &fibh
, &cfi
, &bloc
, &extoffset
, &offset
, &bh
);
849 if (fibh
.sbh
!= fibh
.ebh
)
850 udf_release_data(fibh
.ebh
);
851 udf_release_data(fibh
.sbh
);
852 udf_release_data(bh
);
856 if (cfi
.lengthFileIdent
&& (cfi
.fileCharacteristics
& FILE_DELETED
) == 0)
858 udf_release_data(bh
);
862 if (fibh
.sbh
!= fibh
.ebh
)
863 udf_release_data(fibh
.ebh
);
864 udf_release_data(fibh
.sbh
);
865 udf_release_data(bh
);
869 int udf_rmdir(struct inode
* dir
, struct dentry
* dentry
)
872 struct inode
* inode
;
873 struct udf_fileident_bh fibh
;
874 struct FileIdentDesc
*fi
, cfi
;
877 fi
= udf_find_entry(dir
, dentry
, &fibh
, &cfi
);
881 inode
= dentry
->d_inode
;
884 if (udf_get_lb_pblock(dir
->i_sb
, lelb_to_cpu(cfi
.icb
.extLocation
), 0) != inode
->i_ino
)
887 if (!empty_dir(inode
))
889 retval
= udf_delete_entry(fi
, &fibh
, &cfi
);
890 dir
->i_version
= ++event
;
893 if (inode
->i_nlink
!= 2)
894 udf_warning(inode
->i_sb
, "udf_rmdir",
895 "empty directory has nlink != 2 (%d)",
897 inode
->i_version
= ++event
;
900 mark_inode_dirty(inode
);
902 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
903 UDF_I_UCTIME(inode
) = UDF_I_UCTIME(dir
) = UDF_I_UMTIME(dir
) = CURRENT_UTIME
;
904 mark_inode_dirty(dir
);
908 if (fibh
.sbh
!= fibh
.ebh
)
909 udf_release_data(fibh
.ebh
);
910 udf_release_data(fibh
.sbh
);
915 int udf_unlink(struct inode
* dir
, struct dentry
* dentry
)
918 struct inode
* inode
;
919 struct udf_fileident_bh fibh
;
920 struct FileIdentDesc
*fi
;
921 struct FileIdentDesc cfi
;
924 fi
= udf_find_entry(dir
, dentry
, &fibh
, &cfi
);
928 inode
= dentry
->d_inode
;
932 if (udf_get_lb_pblock(dir
->i_sb
, lelb_to_cpu(cfi
.icb
.extLocation
), 0) !=
940 udf_debug("Deleting nonexistent file (%lu), %d\n",
941 inode
->i_ino
, inode
->i_nlink
);
944 retval
= udf_delete_entry(fi
, &fibh
, &cfi
);
947 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
948 UDF_I_UCTIME(dir
) = UDF_I_UMTIME(dir
) = CURRENT_UTIME
;
949 mark_inode_dirty(dir
);
951 mark_inode_dirty(inode
);
952 inode
->i_ctime
= dir
->i_ctime
;
954 d_delete(dentry
); /* This also frees the inode */
957 if (fibh
.sbh
!= fibh
.ebh
)
958 udf_release_data(fibh
.ebh
);
959 udf_release_data(fibh
.sbh
);
964 int udf_symlink(struct inode
* dir
, struct dentry
* dentry
, const char * symname
)
966 struct inode
* inode
;
967 struct PathComponent
*pc
;
968 struct udf_fileident_bh fibh
;
969 struct buffer_head
*bh
= NULL
;
970 int eoffset
, elen
= 0;
971 struct FileIdentDesc
*fi
;
972 struct FileIdentDesc cfi
;
976 if (!(inode
= udf_new_inode(dir
, S_IFLNK
, &err
)))
979 inode
->i_mode
= S_IFLNK
| S_IRWXUGO
;
980 inode
->i_op
= &udf_symlink_inode_operations
;
982 bh
= udf_tread(inode
->i_sb
, inode
->i_ino
, inode
->i_sb
->s_blocksize
);
983 ea
= bh
->b_data
+ udf_file_entry_alloc_offset(inode
);
985 eoffset
= inode
->i_sb
->s_blocksize
- (ea
- bh
->b_data
);
986 pc
= (struct PathComponent
*)ea
;
993 } while (*symname
== '/');
995 pc
->componentType
= 1;
996 pc
->lengthComponentIdent
= 0;
997 pc
->componentFileVersionNum
= 0;
998 pc
+= sizeof(struct PathComponent
);
999 elen
+= sizeof(struct PathComponent
);
1002 while (*symname
&& eoffset
> elen
+ sizeof(struct PathComponent
))
1005 pc
= (struct PathComponent
*)(ea
+ elen
);
1007 compstart
= (char *)symname
;
1012 } while (*symname
&& *symname
!= '/');
1014 pc
->componentType
= 5;
1015 pc
->lengthComponentIdent
= 0;
1016 pc
->componentFileVersionNum
= 0;
1017 if (pc
->componentIdent
[0] == '.')
1019 if (pc
->lengthComponentIdent
== 1)
1020 pc
->componentType
= 4;
1021 else if (pc
->lengthComponentIdent
== 2 && pc
->componentIdent
[1] == '.')
1022 pc
->componentType
= 3;
1025 if (pc
->componentType
== 5)
1027 if (elen
+ sizeof(struct PathComponent
) + symname
- compstart
> eoffset
)
1028 pc
->lengthComponentIdent
= eoffset
- elen
- sizeof(struct PathComponent
);
1030 pc
->lengthComponentIdent
= symname
- compstart
;
1032 memcpy(pc
->componentIdent
, compstart
, pc
->lengthComponentIdent
);
1035 elen
+= sizeof(struct PathComponent
) + pc
->lengthComponentIdent
;
1042 } while (*symname
== '/');
1046 udf_release_data(bh
);
1047 UDF_I_LENALLOC(inode
) = inode
->i_size
= elen
;
1048 mark_inode_dirty(inode
);
1050 if (!(fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
)))
1052 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
1053 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
1054 if (UDF_SB_LVIDBH(inode
->i_sb
))
1056 struct LogicalVolHeaderDesc
*lvhd
;
1058 lvhd
= (struct LogicalVolHeaderDesc
*)(UDF_SB_LVID(inode
->i_sb
)->logicalVolContentsUse
);
1059 uniqueID
= le64_to_cpu(lvhd
->uniqueID
);
1060 *(Uint32
*)((struct ADImpUse
*)cfi
.icb
.impUse
)->impUse
=
1061 le32_to_cpu(uniqueID
& 0x00000000FFFFFFFFUL
);
1062 if (!(++uniqueID
& 0x00000000FFFFFFFFUL
))
1064 lvhd
->uniqueID
= cpu_to_le64(uniqueID
);
1065 mark_buffer_dirty(UDF_SB_LVIDBH(inode
->i_sb
), 1);
1067 udf_write_fi(&cfi
, fi
, &fibh
, NULL
, NULL
);
1068 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_IN_ICB
)
1070 mark_inode_dirty(dir
);
1071 dir
->i_version
= ++event
;
1073 if (fibh
.sbh
!= fibh
.ebh
)
1074 udf_release_data(fibh
.ebh
);
1075 udf_release_data(fibh
.sbh
);
1076 d_instantiate(dentry
, inode
);
1083 int udf_link(struct dentry
* old_dentry
, struct inode
* dir
,
1084 struct dentry
*dentry
)
1086 struct inode
*inode
= old_dentry
->d_inode
;
1087 struct udf_fileident_bh fibh
;
1089 struct FileIdentDesc cfi
, *fi
;
1091 if (S_ISDIR(inode
->i_mode
))
1094 if (inode
->i_nlink
>= (256<<sizeof(inode
->i_nlink
))-1)
1097 if (!(fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
)))
1099 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
1100 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
1101 if (UDF_SB_LVIDBH(inode
->i_sb
))
1103 struct LogicalVolHeaderDesc
*lvhd
;
1105 lvhd
= (struct LogicalVolHeaderDesc
*)(UDF_SB_LVID(inode
->i_sb
)->logicalVolContentsUse
);
1106 uniqueID
= le64_to_cpu(lvhd
->uniqueID
);
1107 *(Uint32
*)((struct ADImpUse
*)cfi
.icb
.impUse
)->impUse
=
1108 cpu_to_le32(uniqueID
& 0x00000000FFFFFFFFUL
);
1109 if (!(++uniqueID
& 0x00000000FFFFFFFFUL
))
1111 lvhd
->uniqueID
= cpu_to_le64(uniqueID
);
1112 mark_buffer_dirty(UDF_SB_LVIDBH(inode
->i_sb
), 1);
1114 udf_write_fi(&cfi
, fi
, &fibh
, NULL
, NULL
);
1115 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_IN_ICB
)
1117 mark_inode_dirty(dir
);
1118 dir
->i_version
= ++event
;
1120 if (fibh
.sbh
!= fibh
.ebh
)
1121 udf_release_data(fibh
.ebh
);
1122 udf_release_data(fibh
.sbh
);
1124 inode
->i_ctime
= CURRENT_TIME
;
1125 UDF_I_UCTIME(inode
) = CURRENT_UTIME
;
1126 mark_inode_dirty(inode
);
1128 d_instantiate(dentry
, inode
);
1132 /* Anybody can rename anything with this: the permission checks are left to the
1133 * higher-level routines.
1135 int udf_rename (struct inode
* old_dir
, struct dentry
* old_dentry
,
1136 struct inode
* new_dir
, struct dentry
* new_dentry
)
1138 struct inode
* old_inode
, * new_inode
;
1139 struct udf_fileident_bh ofibh
, nfibh
;
1140 struct FileIdentDesc
*ofi
= NULL
, *nfi
= NULL
, *dir_fi
= NULL
, ocfi
, ncfi
;
1141 struct buffer_head
*dir_bh
= NULL
;
1142 int retval
= -ENOENT
;
1144 old_inode
= old_dentry
->d_inode
;
1145 ofi
= udf_find_entry(old_dir
, old_dentry
, &ofibh
, &ocfi
);
1146 if (!ofi
|| udf_get_lb_pblock(old_dir
->i_sb
, lelb_to_cpu(ocfi
.icb
.extLocation
), 0) !=
1152 new_inode
= new_dentry
->d_inode
;
1153 nfi
= udf_find_entry(new_dir
, new_dentry
, &nfibh
, &ncfi
);
1158 if (nfibh
.sbh
!= nfibh
.ebh
)
1159 udf_release_data(nfibh
.ebh
);
1160 udf_release_data(nfibh
.sbh
);
1166 DQUOT_INIT(new_inode);
1170 if (S_ISDIR(old_inode
->i_mode
))
1172 Uint32 offset
= UDF_I_EXT0OFFS(old_inode
);
1176 retval
= -ENOTEMPTY
;
1177 if (!empty_dir(new_inode
))
1181 dir_bh
= udf_bread(old_inode
, 0, 0, &retval
);
1184 dir_fi
= udf_get_fileident(dir_bh
->b_data
, old_inode
->i_sb
->s_blocksize
, &offset
);
1187 if (udf_get_lb_pblock(old_inode
->i_sb
, cpu_to_lelb(dir_fi
->icb
.extLocation
), 0) !=
1193 if (!new_inode
&& new_dir
->i_nlink
>= (256<<sizeof(new_dir
->i_nlink
))-1)
1198 nfi
= udf_add_entry(new_dir
, new_dentry
, &nfibh
, &ncfi
, &retval
);
1202 new_dir
->i_version
= ++event
;
1207 ncfi
.fileVersionNum
= ocfi
.fileVersionNum
;
1208 ncfi
.fileCharacteristics
= ocfi
.fileCharacteristics
;
1209 memcpy(&(ncfi
.icb
), &(ocfi
.icb
), sizeof(long_ad
));
1210 udf_write_fi(&ncfi
, nfi
, &nfibh
, NULL
, NULL
);
1212 udf_delete_entry(ofi
, &ofibh
, &ocfi
);
1214 old_dir
->i_version
= ++event
;
1217 new_inode
->i_nlink
--;
1218 new_inode
->i_ctime
= CURRENT_TIME
;
1219 UDF_I_UCTIME(new_inode
) = CURRENT_UTIME
;
1220 mark_inode_dirty(new_inode
);
1222 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME
;
1223 UDF_I_UCTIME(old_dir
) = UDF_I_UMTIME(old_dir
) = CURRENT_UTIME
;
1224 mark_inode_dirty(old_dir
);
1228 dir_fi
->icb
.extLocation
= lelb_to_cpu(UDF_I_LOCATION(new_dir
));
1229 udf_update_tag((char *)dir_fi
, sizeof(struct FileIdentDesc
) +
1230 cpu_to_le16(dir_fi
->lengthOfImpUse
));
1231 if (UDF_I_ALLOCTYPE(old_inode
) == ICB_FLAG_AD_IN_ICB
)
1233 mark_inode_dirty(old_inode
);
1234 old_inode
->i_version
= ++event
;
1237 mark_buffer_dirty(dir_bh
, 1);
1238 old_dir
->i_nlink
--;
1239 mark_inode_dirty(old_dir
);
1242 new_inode
->i_nlink
--;
1243 mark_inode_dirty(new_inode
);
1247 new_dir
->i_nlink
++;
1248 mark_inode_dirty(new_dir
);
1255 udf_release_data(dir_bh
);
1258 if (ofibh
.sbh
!= ofibh
.ebh
)
1259 udf_release_data(ofibh
.ebh
);
1260 udf_release_data(ofibh
.sbh
);
1264 if (nfibh
.sbh
!= nfibh
.ebh
)
1265 udf_release_data(nfibh
.ebh
);
1266 udf_release_data(nfibh
.sbh
);