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 struct FileIdentDesc
*efi
;
53 Uint16 crclen
= fibh
->eoffset
- fibh
->soffset
- sizeof(tag
);
58 int padlen
= fibh
->eoffset
- fibh
->soffset
- cfi
->lengthOfImpUse
- cfi
->lengthFileIdent
-
59 sizeof(struct FileIdentDesc
);
61 crc
= udf_crc((Uint8
*)cfi
+ sizeof(tag
), sizeof(struct FileIdentDesc
) -
63 efi
= (struct FileIdentDesc
*)(fibh
->ebh
->b_data
+ fibh
->soffset
);
64 if (fibh
->sbh
== fibh
->ebh
||
66 (sizeof(struct FileIdentDesc
) + (impuse
? cfi
->lengthOfImpUse
: 0))
69 memcpy((Uint8
*)sfi
, (Uint8
*)cfi
, sizeof(struct FileIdentDesc
));
72 memcpy(sfi
->impUse
, impuse
, cfi
->lengthOfImpUse
);
75 memcpy(sfi
->fileIdent
+ cfi
->lengthOfImpUse
, fileident
,
76 cfi
->lengthFileIdent
);
79 memset(sfi
->fileIdent
+ cfi
->lengthOfImpUse
+ cfi
->lengthFileIdent
, 0,
82 if (fibh
->sbh
== fibh
->ebh
)
83 crc
= udf_crc((Uint8
*)sfi
+ sizeof(tag
), crclen
, 0);
86 crc
= udf_crc((Uint8
*)sfi
+ sizeof(tag
), crclen
- fibh
->eoffset
, 0);
87 crc
= udf_crc(fibh
->ebh
->b_data
, fibh
->eoffset
, crc
);
90 sfi
->descTag
.descCRC
= cpu_to_le32(crc
);
91 sfi
->descTag
.descCRCLength
= cpu_to_le16(crclen
);
95 checksum
+= ((Uint8
*)&sfi
->descTag
)[i
];
97 sfi
->descTag
.tagChecksum
= checksum
;
99 mark_buffer_dirty(fibh
->sbh
, 1);
103 offset
= -fibh
->soffset
;
104 len
= sizeof(struct FileIdentDesc
);
107 memcpy((Uint8
*)sfi
, (Uint8
*)cfi
, len
);
110 memcpy((Uint8
*)sfi
, (Uint8
*)cfi
, offset
);
111 memcpy(fibh
->ebh
->b_data
, (Uint8
*)cfi
+ offset
, len
- offset
);
115 len
= cfi
->lengthOfImpUse
;
120 memcpy(efi
->impUse
, impuse
, len
);
121 else if (sizeof(struct FileIdentDesc
) + len
<= -fibh
->soffset
)
122 memcpy(sfi
->impUse
, impuse
, len
);
125 memcpy(sfi
->impUse
, impuse
, offset
);
126 memcpy(efi
->impUse
+ offset
, impuse
+ offset
, len
- offset
);
131 len
= cfi
->lengthFileIdent
;
136 memcpy(efi
->fileIdent
+ cfi
->lengthOfImpUse
, fileident
, len
);
139 memcpy(sfi
->fileIdent
+ cfi
->lengthOfImpUse
, fileident
, offset
);
140 memcpy(efi
->fileIdent
+ cfi
->lengthOfImpUse
+ offset
,
141 fileident
+ offset
, len
- offset
);
146 memset(efi
->fileIdent
+ cfi
->lengthOfImpUse
+ cfi
->lengthFileIdent
, 0x00,
149 if (sizeof(tag
) < -fibh
->soffset
)
151 crc
= udf_crc((Uint8
*)sfi
+ sizeof(tag
), crclen
- fibh
->eoffset
, 0);
152 crc
= udf_crc(fibh
->ebh
->b_data
, fibh
->eoffset
, crc
);
155 crc
= udf_crc((Uint8
*)fibh
->ebh
->b_data
+ fibh
->eoffset
- crclen
, crclen
, 0);
157 if (&(efi
->descTag
.descCRC
) < (Uint16
*)fibh
->ebh
->b_data
)
159 sfi
->descTag
.descCRC
= cpu_to_le16(crc
);
160 sfi
->descTag
.descCRCLength
= cpu_to_le16(crclen
);
164 efi
->descTag
.descCRC
= cpu_to_le16(crc
);
165 efi
->descTag
.descCRCLength
= cpu_to_le16(crclen
);
172 if (&(((Uint8
*)&efi
->descTag
)[i
]) < (Uint8
*)fibh
->ebh
->b_data
)
173 checksum
+= ((Uint8
*)&sfi
->descTag
)[i
];
175 checksum
+= ((Uint8
*)&efi
->descTag
)[i
];
179 if (&(cfi
->descTag
.tagChecksum
) < (Uint8
*)fibh
->ebh
->b_data
)
180 sfi
->descTag
.tagChecksum
= checksum
;
182 efi
->descTag
.tagChecksum
= checksum
;
184 mark_buffer_dirty(fibh
->sbh
, 1);
185 mark_buffer_dirty(fibh
->ebh
, 1);
190 static struct FileIdentDesc
*
191 udf_find_entry(struct inode
*dir
, struct dentry
*dentry
,
192 struct udf_fileident_bh
*fibh
,
193 struct FileIdentDesc
*cfi
)
195 struct FileIdentDesc
*fi
=NULL
;
202 int size
= (UDF_I_EXT0OFFS(dir
) + dir
->i_size
) >> 2;
204 Uint32 extoffset
, elen
, offset
;
205 struct buffer_head
*bh
= NULL
;
210 f_pos
= (UDF_I_EXT0OFFS(dir
) >> 2);
212 fibh
->soffset
= fibh
->eoffset
= (f_pos
& ((dir
->i_sb
->s_blocksize
- 1) >> 2)) << 2;
213 if (inode_bmap(dir
, f_pos
>> (dir
->i_sb
->s_blocksize_bits
- 2),
214 &bloc
, &extoffset
, &eloc
, &elen
, &offset
, &bh
) == EXTENT_RECORDED_ALLOCATED
)
216 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, offset
);
217 if (++offset
< (elen
>> dir
->i_sb
->s_blocksize_bits
))
219 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_SHORT
)
220 extoffset
-= sizeof(short_ad
);
221 else if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_LONG
)
222 extoffset
-= sizeof(long_ad
);
229 udf_release_data(bh
);
233 if (!(fibh
->sbh
= fibh
->ebh
= udf_tread(dir
->i_sb
, block
, dir
->i_sb
->s_blocksize
)))
235 udf_debug("udf_tread failed: block=%d\n", block
);
236 udf_release_data(bh
);
240 while ( (f_pos
< size
) )
242 fi
= udf_fileident_read(dir
, &f_pos
, fibh
, cfi
, &bloc
, &extoffset
, &offset
, &bh
);
243 liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
244 lfi
= cfi
->lengthFileIdent
;
248 if (fibh
->sbh
!= fibh
->ebh
)
249 udf_release_data(fibh
->ebh
);
250 udf_release_data(fibh
->sbh
);
251 udf_release_data(bh
);
255 if (fibh
->sbh
== fibh
->ebh
)
257 nameptr
= fi
->fileIdent
+ liu
;
261 int poffset
; /* Unpaded ending offset */
263 poffset
= fibh
->soffset
+ sizeof(struct FileIdentDesc
) + liu
+ lfi
;
266 nameptr
= (Uint8
*)(fibh
->ebh
->b_data
+ poffset
- lfi
);
270 memcpy(nameptr
, fi
->fileIdent
+ liu
, lfi
- poffset
);
271 memcpy(nameptr
+ lfi
- poffset
, fibh
->ebh
->b_data
, poffset
);
275 if ( (cfi
->fileCharacteristics
& FILE_DELETED
) != 0 )
277 if ( !IS_UNDELETE(dir
->i_sb
) )
281 if ( (cfi
->fileCharacteristics
& FILE_HIDDEN
) != 0 )
283 if ( !IS_UNHIDE(dir
->i_sb
) )
290 if ((flen
= udf_get_filename(nameptr
, fname
, lfi
)))
292 if (udf_match(flen
, fname
, &(dentry
->d_name
)))
294 udf_release_data(bh
);
299 if (fibh
->sbh
!= fibh
->ebh
)
300 udf_release_data(fibh
->ebh
);
301 udf_release_data(fibh
->sbh
);
302 udf_release_data(bh
);
310 * Look-up the inode for a given name.
313 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
314 * available for a directory. The filesystem is useless if this routine is
315 * not available for at least the filesystem's root directory.
317 * This routine is passed an incomplete dentry - it must be completed by
318 * calling d_add(dentry, inode). If the name does not exist, then the
319 * specified inode must be set to null. An error should only be returned
320 * when the lookup fails for a reason other than the name not existing.
321 * Note that the directory inode semaphore is held during the call.
323 * Refer to lookup_dentry() in fs/namei.c
324 * lookup_dentry() -> lookup() -> real_lookup() -> .
327 * dir Pointer to inode of parent directory.
328 * dentry Pointer to dentry to complete.
331 * <return> Zero on success.
334 * July 1, 1997 - Andrew E. Mileski
335 * Written, tested, and released.
339 udf_lookup(struct inode
*dir
, struct dentry
*dentry
)
341 struct inode
*inode
= NULL
;
342 struct FileIdentDesc cfi
, *fi
;
343 struct udf_fileident_bh fibh
;
346 /* temporary shorthand for specifying files by inode number */
347 if (!strncmp(dentry
->d_name
.name
, ".B=", 3) )
349 lb_addr lb
= { 0, simple_strtoul(dentry
->d_name
.name
+3, NULL
, 0) };
350 inode
= udf_iget(dir
->i_sb
, lb
);
352 return ERR_PTR(-EACCES
);
355 #endif /* UDF_RECOVERY */
357 if ((fi
= udf_find_entry(dir
, dentry
, &fibh
, &cfi
)))
359 if (fibh
.sbh
!= fibh
.ebh
)
360 udf_release_data(fibh
.ebh
);
361 udf_release_data(fibh
.sbh
);
363 inode
= udf_iget(dir
->i_sb
, lelb_to_cpu(cfi
.icb
.extLocation
));
365 return ERR_PTR(-EACCES
);
367 d_add(dentry
, inode
);
371 static struct FileIdentDesc
*
372 udf_add_entry(struct inode
*dir
, struct dentry
*dentry
,
373 struct udf_fileident_bh
*fibh
,
374 struct FileIdentDesc
*cfi
, int *err
)
376 struct super_block
*sb
;
377 struct FileIdentDesc
*fi
=NULL
;
378 struct ustr unifilename
;
379 char name
[UDF_NAME_LEN
], fname
[UDF_NAME_LEN
];
384 int size
= (UDF_I_EXT0OFFS(dir
) + dir
->i_size
) >> 2;
390 Uint32 extoffset
, elen
, offset
;
391 struct buffer_head
*bh
= NULL
;
394 if (!dir
|| !dir
->i_nlink
)
398 if (!dentry
->d_name
.len
)
401 if (dir
->i_size
== 0)
407 if ( !(udf_char_to_ustr(&unifilename
, dentry
->d_name
.name
, dentry
->d_name
.len
)) )
409 *err
= -ENAMETOOLONG
;
413 if ( !(namelen
= udf_UTF8toCS0(name
, &unifilename
, UDF_NAME_LEN
)) )
416 nfidlen
= (sizeof(struct FileIdentDesc
) + 0 + namelen
+ 3) & ~3;
418 f_pos
= (UDF_I_EXT0OFFS(dir
) >> 2);
420 fibh
->soffset
= fibh
->eoffset
= (f_pos
& ((dir
->i_sb
->s_blocksize
- 1) >> 2)) << 2;
421 if (inode_bmap(dir
, f_pos
>> (dir
->i_sb
->s_blocksize_bits
- 2),
422 &bloc
, &extoffset
, &eloc
, &elen
, &offset
, &bh
) == EXTENT_RECORDED_ALLOCATED
)
424 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, offset
);
425 if (++offset
< (elen
>> dir
->i_sb
->s_blocksize_bits
))
427 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_SHORT
)
428 extoffset
-= sizeof(short_ad
);
429 else if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_LONG
)
430 extoffset
-= sizeof(long_ad
);
437 udf_release_data(bh
);
441 if (!(fibh
->sbh
= fibh
->ebh
= udf_tread(dir
->i_sb
, block
, dir
->i_sb
->s_blocksize
)))
444 block
= UDF_I_LOCATION(dir
).logicalBlockNum
;
446 while ( (f_pos
< size
) )
448 fi
= udf_fileident_read(dir
, &f_pos
, fibh
, cfi
, &bloc
, &extoffset
, &offset
, &bh
);
449 liu
= le16_to_cpu(cfi
->lengthOfImpUse
);
450 lfi
= cfi
->lengthFileIdent
;
454 if (fibh
->sbh
!= fibh
->ebh
)
455 udf_release_data(fibh
->ebh
);
456 udf_release_data(fibh
->sbh
);
457 udf_release_data(bh
);
461 if (fibh
->sbh
== fibh
->ebh
)
462 nameptr
= fi
->fileIdent
+ liu
;
465 int poffset
; /* Unpaded ending offset */
467 poffset
= fibh
->soffset
+ sizeof(struct FileIdentDesc
) + liu
+ lfi
;
470 nameptr
= (char *)(fibh
->ebh
->b_data
+ poffset
- lfi
);
474 memcpy(nameptr
, fi
->fileIdent
+ liu
, lfi
- poffset
);
475 memcpy(nameptr
+ lfi
- poffset
, fibh
->ebh
->b_data
, poffset
);
479 if ( (cfi
->fileCharacteristics
& FILE_DELETED
) != 0 )
481 if (((sizeof(struct FileIdentDesc
) + liu
+ lfi
+ 3) & ~3) == nfidlen
)
483 udf_release_data(bh
);
484 cfi
->descTag
.tagSerialNum
= cpu_to_le16(1);
485 cfi
->fileVersionNum
= cpu_to_le16(1);
486 cfi
->fileCharacteristics
= 0;
487 cfi
->lengthFileIdent
= namelen
;
488 cfi
->lengthOfImpUse
= cpu_to_le16(0);
489 if (!udf_write_fi(cfi
, fi
, fibh
, NULL
, name
))
499 if ((flen
= udf_get_filename(nameptr
, fname
, lfi
)))
501 if (udf_match(flen
, fname
, &(dentry
->d_name
)))
503 if (fibh
->sbh
!= fibh
->ebh
)
504 udf_release_data(fibh
->ebh
);
505 udf_release_data(fibh
->sbh
);
506 udf_release_data(bh
);
515 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_IN_ICB
&&
516 sb
->s_blocksize
- fibh
->eoffset
< nfidlen
)
518 udf_release_data(bh
);
520 fibh
->soffset
-= UDF_I_EXT0OFFS(dir
);
521 fibh
->eoffset
-= UDF_I_EXT0OFFS(dir
);
522 f_pos
-= (UDF_I_EXT0OFFS(dir
) >> 2);
523 udf_release_data(fibh
->sbh
);
524 if (!(fibh
->sbh
= fibh
->ebh
= udf_expand_adinicb(dir
, &block
, 1, err
)))
526 bloc
= UDF_I_LOCATION(dir
);
527 extoffset
= udf_file_entry_alloc_offset(dir
);
531 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_SHORT
)
532 extoffset
-= sizeof(short_ad
);
533 else if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_LONG
)
534 extoffset
-= sizeof(long_ad
);
537 dir
->i_size
+= nfidlen
;
538 if (sb
->s_blocksize
- fibh
->eoffset
>= nfidlen
)
540 fibh
->soffset
= fibh
->eoffset
;
541 fibh
->eoffset
+= nfidlen
;
542 if (fibh
->sbh
!= fibh
->ebh
)
544 udf_release_data(fibh
->sbh
);
545 fibh
->sbh
= fibh
->ebh
;
548 if (UDF_I_ALLOCTYPE(dir
) != ICB_FLAG_AD_IN_ICB
)
550 Uint32 lextoffset
= extoffset
;
551 if (udf_next_aext(dir
, &bloc
, &extoffset
, &eloc
, &elen
, &bh
, 1) !=
552 EXTENT_RECORDED_ALLOCATED
)
554 udf_release_data(bh
);
555 udf_release_data(fibh
->sbh
);
561 elen
= (EXTENT_RECORDED_ALLOCATED
<< 30) | elen
;
562 udf_write_aext(dir
, bloc
, &lextoffset
, eloc
, elen
, &bh
, 1);
563 block
= eloc
.logicalBlockNum
+ (elen
>> dir
->i_sb
->s_blocksize_bits
);
567 block
= UDF_I_LOCATION(dir
).logicalBlockNum
;
569 fi
= (struct FileIdentDesc
*)(fibh
->sbh
->b_data
+ fibh
->soffset
);
573 Uint32 lextoffset
= extoffset
;
575 fibh
->soffset
= fibh
->eoffset
- sb
->s_blocksize
;
576 fibh
->eoffset
+= nfidlen
- sb
->s_blocksize
;
577 if (fibh
->sbh
!= fibh
->ebh
)
579 udf_release_data(fibh
->sbh
);
580 fibh
->sbh
= fibh
->ebh
;
583 if (udf_next_aext(dir
, &bloc
, &extoffset
, &eloc
, &elen
, &bh
, 1) !=
584 EXTENT_RECORDED_ALLOCATED
)
586 udf_release_data(bh
);
587 udf_release_data(fibh
->sbh
);
591 block
= eloc
.logicalBlockNum
+ (elen
>> dir
->i_sb
->s_blocksize_bits
);
594 if (!(fibh
->ebh
= udf_getblk(dir
, f_pos
>> (dir
->i_sb
->s_blocksize_bits
- 2), 1, err
)))
596 udf_release_data(bh
);
597 udf_release_data(fibh
->sbh
);
600 if (!(fibh
->soffset
))
602 if (udf_next_aext(dir
, &bloc
, &lextoffset
, &eloc
, &elen
, &bh
, 1) ==
603 EXTENT_RECORDED_ALLOCATED
)
605 block
= eloc
.logicalBlockNum
+ (elen
>> dir
->i_sb
->s_blocksize_bits
);
611 fi
= (struct FileIdentDesc
*)(fibh
->sbh
->b_data
+ sb
->s_blocksize
+ fibh
->soffset
);
614 memset(cfi
, 0, sizeof(struct FileIdentDesc
));
615 udf_new_tag((char *)cfi
, TID_FILE_IDENT_DESC
, 2, 1, block
, sizeof(tag
));
616 cfi
->fileVersionNum
= cpu_to_le16(1);
617 cfi
->lengthFileIdent
= namelen
;
618 cfi
->lengthOfImpUse
= cpu_to_le16(0);
619 if (!udf_write_fi(cfi
, fi
, fibh
, NULL
, name
))
621 udf_release_data(bh
);
622 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_IN_ICB
)
623 UDF_I_LENALLOC(dir
) += nfidlen
;
624 dir
->i_version
= ++event
;
625 mark_inode_dirty(dir
);
630 udf_release_data(bh
);
631 dir
->i_size
-= nfidlen
;
632 if (fibh
->sbh
!= fibh
->ebh
)
633 udf_release_data(fibh
->ebh
);
634 udf_release_data(fibh
->sbh
);
639 static int udf_delete_entry(struct FileIdentDesc
*fi
,
640 struct udf_fileident_bh
*fibh
,
641 struct FileIdentDesc
*cfi
)
643 cfi
->fileCharacteristics
|= FILE_DELETED
;
644 return udf_write_fi(cfi
, fi
, fibh
, NULL
, NULL
);
647 int udf_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
649 struct udf_fileident_bh fibh
;
651 struct FileIdentDesc cfi
, *fi
;
654 inode
= udf_new_inode(dir
, mode
, &err
);
658 inode
->i_op
= &udf_file_inode_operations
;
659 inode
->i_mode
= mode
;
660 mark_inode_dirty(inode
);
662 if (!(fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
)))
664 udf_debug("udf_add_entry failure!\n");
666 mark_inode_dirty(inode
);
670 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
671 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
672 *(Uint32
*)((struct ADImpUse
*)cfi
.icb
.impUse
)->impUse
=
673 cpu_to_le32(UDF_I_UNIQUE(inode
) & 0x00000000FFFFFFFFUL
);
674 udf_write_fi(&cfi
, fi
, &fibh
, NULL
, NULL
);
675 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_IN_ICB
)
677 mark_inode_dirty(dir
);
678 dir
->i_version
= ++event
;
680 if (fibh
.sbh
!= fibh
.ebh
)
681 udf_release_data(fibh
.ebh
);
682 udf_release_data(fibh
.sbh
);
683 d_instantiate(dentry
, inode
);
687 int udf_mknod(struct inode
* dir
, struct dentry
* dentry
, int mode
, int rdev
)
689 struct inode
* inode
;
690 struct udf_fileident_bh fibh
;
692 struct FileIdentDesc cfi
, *fi
;
695 inode
= udf_new_inode(dir
, mode
, &err
);
699 inode
->i_uid
= current
->fsuid
;
700 inode
->i_mode
= mode
;
702 if (!(fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
)))
704 udf_debug("udf_add_entry failure!\n");
706 mark_inode_dirty(inode
);
710 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
711 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
712 *(Uint32
*)((struct ADImpUse
*)cfi
.icb
.impUse
)->impUse
=
713 cpu_to_le32(UDF_I_UNIQUE(inode
) & 0x00000000FFFFFFFFUL
);
714 udf_write_fi(&cfi
, fi
, &fibh
, NULL
, NULL
);
715 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_IN_ICB
)
717 mark_inode_dirty(dir
);
718 dir
->i_version
= ++event
;
720 if (S_ISREG(inode
->i_mode
))
722 inode
->i_op
= &udf_file_inode_operations
;
724 else if (S_ISCHR(inode
->i_mode
))
726 inode
->i_op
= &chrdev_inode_operations
;
728 else if (S_ISBLK(inode
->i_mode
))
730 inode
->i_op
= &blkdev_inode_operations
;
732 else if (S_ISFIFO(inode
->i_mode
))
736 if (S_ISBLK(mode
) || S_ISCHR(mode
))
737 inode
->i_rdev
= to_kdev_t(rdev
);
738 mark_inode_dirty(inode
);
740 if (fibh
.sbh
!= fibh
.ebh
)
741 udf_release_data(fibh
.ebh
);
742 udf_release_data(fibh
.sbh
);
743 d_instantiate(dentry
, inode
);
749 int udf_mkdir(struct inode
* dir
, struct dentry
* dentry
, int mode
)
751 struct inode
* inode
;
752 struct udf_fileident_bh fibh
;
754 struct FileEntry
*fe
;
755 struct FileIdentDesc cfi
, *fi
;
759 if (dir
->i_nlink
>= (256<<sizeof(dir
->i_nlink
))-1)
763 inode
= udf_new_inode(dir
, S_IFDIR
, &err
);
767 inode
->i_op
= &udf_dir_inode_operations
;
768 inode
->i_size
= (sizeof(struct FileIdentDesc
) + 3) & ~3;
769 if (UDF_I_ALLOCTYPE(inode
) == ICB_FLAG_AD_IN_ICB
)
771 UDF_I_EXT0LEN(inode
) = inode
->i_size
;
772 UDF_I_EXT0LOC(inode
) = UDF_I_LOCATION(inode
);
773 UDF_I_LENALLOC(inode
) = inode
->i_size
;
774 loc
= UDF_I_LOCATION(inode
).logicalBlockNum
;
775 fibh
.sbh
= udf_tread(inode
->i_sb
, inode
->i_ino
, inode
->i_sb
->s_blocksize
);
779 fibh
.sbh
= udf_bread (inode
, 0, 1, &err
);
780 loc
= UDF_I_EXT0LOC(inode
).logicalBlockNum
;
786 mark_inode_dirty(inode
);
791 fe
= (struct FileEntry
*)fibh
.sbh
->b_data
;
792 fi
= (struct FileIdentDesc
*)&(fe
->extendedAttr
[UDF_I_LENEATTR(inode
)]);
793 udf_new_tag((char *)&cfi
, TID_FILE_IDENT_DESC
, 2, 1, loc
,
794 sizeof(struct FileIdentDesc
));
795 cfi
.fileVersionNum
= cpu_to_le16(1);
796 cfi
.fileCharacteristics
= FILE_DIRECTORY
| FILE_PARENT
;
797 cfi
.lengthFileIdent
= 0;
798 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
799 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(dir
));
800 *(Uint32
*)((struct ADImpUse
*)cfi
.icb
.impUse
)->impUse
=
801 cpu_to_le32(UDF_I_UNIQUE(dir
) & 0x00000000FFFFFFFFUL
);
802 cfi
.lengthOfImpUse
= cpu_to_le16(0);
804 fibh
.soffset
= sizeof(struct FileEntry
);
805 fibh
.eoffset
= sizeof(struct FileEntry
) + inode
->i_size
;
806 udf_write_fi(&cfi
, fi
, &fibh
, NULL
, NULL
);
807 udf_release_data(fibh
.sbh
);
808 inode
->i_mode
= S_IFDIR
| (mode
& (S_IRWXUGO
|S_ISVTX
) & ~current
->fs
->umask
);
809 if (dir
->i_mode
& S_ISGID
)
810 inode
->i_mode
|= S_ISGID
;
811 mark_inode_dirty(inode
);
813 if (!(fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
)))
815 udf_debug("udf_add_entry failure!\n");
817 mark_inode_dirty(inode
);
821 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
822 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
823 *(Uint32
*)((struct ADImpUse
*)cfi
.icb
.impUse
)->impUse
=
824 cpu_to_le32(UDF_I_UNIQUE(inode
) & 0x00000000FFFFFFFFUL
);
825 cfi
.fileCharacteristics
|= FILE_DIRECTORY
;
826 udf_write_fi(&cfi
, fi
, &fibh
, NULL
, NULL
);
827 dir
->i_version
= ++event
;
829 mark_inode_dirty(dir
);
830 d_instantiate(dentry
, inode
);
831 if (fibh
.sbh
!= fibh
.ebh
)
832 udf_release_data(fibh
.ebh
);
833 udf_release_data(fibh
.sbh
);
839 static int empty_dir(struct inode
*dir
)
841 struct FileIdentDesc
*fi
, cfi
;
842 struct udf_fileident_bh fibh
;
844 int size
= (UDF_I_EXT0OFFS(dir
) + dir
->i_size
) >> 2;
847 Uint32 extoffset
, elen
, offset
;
848 struct buffer_head
*bh
= NULL
;
850 f_pos
= (UDF_I_EXT0OFFS(dir
) >> 2);
852 fibh
.soffset
= fibh
.eoffset
= (f_pos
& ((dir
->i_sb
->s_blocksize
- 1) >> 2)) << 2;
853 if (inode_bmap(dir
, f_pos
>> (dir
->i_sb
->s_blocksize_bits
- 2),
854 &bloc
, &extoffset
, &eloc
, &elen
, &offset
, &bh
) == EXTENT_RECORDED_ALLOCATED
)
856 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, offset
);
857 if (++offset
< (elen
>> dir
->i_sb
->s_blocksize_bits
))
859 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_SHORT
)
860 extoffset
-= sizeof(short_ad
);
861 else if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_LONG
)
862 extoffset
-= sizeof(long_ad
);
869 udf_release_data(bh
);
873 if (!(fibh
.sbh
= fibh
.ebh
= udf_tread(dir
->i_sb
, block
, dir
->i_sb
->s_blocksize
)))
876 while ( (f_pos
< size
) )
878 fi
= udf_fileident_read(dir
, &f_pos
, &fibh
, &cfi
, &bloc
, &extoffset
, &offset
, &bh
);
882 if (fibh
.sbh
!= fibh
.ebh
)
883 udf_release_data(fibh
.ebh
);
884 udf_release_data(fibh
.sbh
);
885 udf_release_data(bh
);
889 if (cfi
.lengthFileIdent
&& (cfi
.fileCharacteristics
& FILE_DELETED
) == 0)
891 udf_release_data(bh
);
895 if (fibh
.sbh
!= fibh
.ebh
)
896 udf_release_data(fibh
.ebh
);
897 udf_release_data(fibh
.sbh
);
898 udf_release_data(bh
);
902 int udf_rmdir(struct inode
* dir
, struct dentry
* dentry
)
905 struct inode
* inode
;
906 struct udf_fileident_bh fibh
;
907 struct FileIdentDesc
*fi
, cfi
;
910 fi
= udf_find_entry(dir
, dentry
, &fibh
, &cfi
);
914 inode
= dentry
->d_inode
;
917 if (udf_get_lb_pblock(dir
->i_sb
, lelb_to_cpu(cfi
.icb
.extLocation
), 0) != inode
->i_ino
)
920 if (!empty_dir(inode
))
922 retval
= udf_delete_entry(fi
, &fibh
, &cfi
);
923 dir
->i_version
= ++event
;
926 if (inode
->i_nlink
!= 2)
927 udf_warning(inode
->i_sb
, "udf_rmdir",
928 "empty directory has nlink != 2 (%d)",
930 inode
->i_version
= ++event
;
933 mark_inode_dirty(inode
);
935 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
936 UDF_I_UCTIME(inode
) = UDF_I_UCTIME(dir
) = UDF_I_UMTIME(dir
) = CURRENT_UTIME
;
937 mark_inode_dirty(dir
);
941 if (fibh
.sbh
!= fibh
.ebh
)
942 udf_release_data(fibh
.ebh
);
943 udf_release_data(fibh
.sbh
);
948 int udf_unlink(struct inode
* dir
, struct dentry
* dentry
)
951 struct inode
* inode
;
952 struct udf_fileident_bh fibh
;
953 struct FileIdentDesc
*fi
;
954 struct FileIdentDesc cfi
;
957 fi
= udf_find_entry(dir
, dentry
, &fibh
, &cfi
);
961 inode
= dentry
->d_inode
;
965 if (udf_get_lb_pblock(dir
->i_sb
, lelb_to_cpu(cfi
.icb
.extLocation
), 0) !=
973 udf_debug("Deleting nonexistent file (%lu), %d\n",
974 inode
->i_ino
, inode
->i_nlink
);
977 retval
= udf_delete_entry(fi
, &fibh
, &cfi
);
980 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
981 UDF_I_UCTIME(dir
) = UDF_I_UMTIME(dir
) = CURRENT_UTIME
;
982 mark_inode_dirty(dir
);
984 mark_inode_dirty(inode
);
985 inode
->i_ctime
= dir
->i_ctime
;
987 d_delete(dentry
); /* This also frees the inode */
990 if (fibh
.sbh
!= fibh
.ebh
)
991 udf_release_data(fibh
.ebh
);
992 udf_release_data(fibh
.sbh
);
997 int udf_symlink(struct inode
* dir
, struct dentry
* dentry
, const char * symname
)
999 struct inode
* inode
;
1000 struct PathComponent
*pc
;
1001 struct udf_fileident_bh fibh
;
1002 struct buffer_head
*bh
= NULL
;
1003 int eoffset
, elen
= 0;
1004 struct FileIdentDesc
*fi
;
1005 struct FileIdentDesc cfi
;
1009 if (!(inode
= udf_new_inode(dir
, S_IFLNK
, &err
)))
1012 inode
->i_mode
= S_IFLNK
| S_IRWXUGO
;
1013 inode
->i_op
= &udf_symlink_inode_operations
;
1015 bh
= udf_tread(inode
->i_sb
, inode
->i_ino
, inode
->i_sb
->s_blocksize
);
1016 ea
= bh
->b_data
+ udf_file_entry_alloc_offset(inode
);
1018 eoffset
= inode
->i_sb
->s_blocksize
- (ea
- bh
->b_data
);
1019 pc
= (struct PathComponent
*)ea
;
1021 if (*symname
== '/')
1026 } while (*symname
== '/');
1028 pc
->componentType
= 1;
1029 pc
->lengthComponentIdent
= 0;
1030 pc
->componentFileVersionNum
= 0;
1031 pc
+= sizeof(struct PathComponent
);
1032 elen
+= sizeof(struct PathComponent
);
1035 while (*symname
&& eoffset
> elen
+ sizeof(struct PathComponent
))
1038 pc
= (struct PathComponent
*)(ea
+ elen
);
1040 compstart
= (char *)symname
;
1045 } while (*symname
&& *symname
!= '/');
1047 pc
->componentType
= 5;
1048 pc
->lengthComponentIdent
= 0;
1049 pc
->componentFileVersionNum
= 0;
1050 if (pc
->componentIdent
[0] == '.')
1052 if (pc
->lengthComponentIdent
== 1)
1053 pc
->componentType
= 4;
1054 else if (pc
->lengthComponentIdent
== 2 && pc
->componentIdent
[1] == '.')
1055 pc
->componentType
= 3;
1058 if (pc
->componentType
== 5)
1060 if (elen
+ sizeof(struct PathComponent
) + symname
- compstart
> eoffset
)
1061 pc
->lengthComponentIdent
= eoffset
- elen
- sizeof(struct PathComponent
);
1063 pc
->lengthComponentIdent
= symname
- compstart
;
1065 memcpy(pc
->componentIdent
, compstart
, pc
->lengthComponentIdent
);
1068 elen
+= sizeof(struct PathComponent
) + pc
->lengthComponentIdent
;
1075 } while (*symname
== '/');
1079 udf_release_data(bh
);
1080 UDF_I_LENALLOC(inode
) = inode
->i_size
= elen
;
1081 mark_inode_dirty(inode
);
1083 if (!(fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
)))
1085 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
1086 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
1087 if (UDF_SB_LVIDBH(inode
->i_sb
))
1089 struct LogicalVolHeaderDesc
*lvhd
;
1091 lvhd
= (struct LogicalVolHeaderDesc
*)(UDF_SB_LVID(inode
->i_sb
)->logicalVolContentsUse
);
1092 uniqueID
= le64_to_cpu(lvhd
->uniqueID
);
1093 *(Uint32
*)((struct ADImpUse
*)cfi
.icb
.impUse
)->impUse
=
1094 le32_to_cpu(uniqueID
& 0x00000000FFFFFFFFUL
);
1095 if (!(++uniqueID
& 0x00000000FFFFFFFFUL
))
1097 lvhd
->uniqueID
= cpu_to_le64(uniqueID
);
1098 mark_buffer_dirty(UDF_SB_LVIDBH(inode
->i_sb
), 1);
1100 udf_write_fi(&cfi
, fi
, &fibh
, NULL
, NULL
);
1101 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_IN_ICB
)
1103 mark_inode_dirty(dir
);
1104 dir
->i_version
= ++event
;
1106 if (fibh
.sbh
!= fibh
.ebh
)
1107 udf_release_data(fibh
.ebh
);
1108 udf_release_data(fibh
.sbh
);
1109 d_instantiate(dentry
, inode
);
1116 int udf_link(struct dentry
* old_dentry
, struct inode
* dir
,
1117 struct dentry
*dentry
)
1119 struct inode
*inode
= old_dentry
->d_inode
;
1120 struct udf_fileident_bh fibh
;
1122 struct FileIdentDesc cfi
, *fi
;
1124 if (S_ISDIR(inode
->i_mode
))
1127 if (inode
->i_nlink
>= (256<<sizeof(inode
->i_nlink
))-1)
1130 if (!(fi
= udf_add_entry(dir
, dentry
, &fibh
, &cfi
, &err
)))
1132 cfi
.icb
.extLength
= cpu_to_le32(inode
->i_sb
->s_blocksize
);
1133 cfi
.icb
.extLocation
= cpu_to_lelb(UDF_I_LOCATION(inode
));
1134 if (UDF_SB_LVIDBH(inode
->i_sb
))
1136 struct LogicalVolHeaderDesc
*lvhd
;
1138 lvhd
= (struct LogicalVolHeaderDesc
*)(UDF_SB_LVID(inode
->i_sb
)->logicalVolContentsUse
);
1139 uniqueID
= le64_to_cpu(lvhd
->uniqueID
);
1140 *(Uint32
*)((struct ADImpUse
*)cfi
.icb
.impUse
)->impUse
=
1141 cpu_to_le32(uniqueID
& 0x00000000FFFFFFFFUL
);
1142 if (!(++uniqueID
& 0x00000000FFFFFFFFUL
))
1144 lvhd
->uniqueID
= cpu_to_le64(uniqueID
);
1145 mark_buffer_dirty(UDF_SB_LVIDBH(inode
->i_sb
), 1);
1147 udf_write_fi(&cfi
, fi
, &fibh
, NULL
, NULL
);
1148 if (UDF_I_ALLOCTYPE(dir
) == ICB_FLAG_AD_IN_ICB
)
1150 mark_inode_dirty(dir
);
1151 dir
->i_version
= ++event
;
1153 if (fibh
.sbh
!= fibh
.ebh
)
1154 udf_release_data(fibh
.ebh
);
1155 udf_release_data(fibh
.sbh
);
1157 inode
->i_ctime
= CURRENT_TIME
;
1158 UDF_I_UCTIME(inode
) = CURRENT_UTIME
;
1159 mark_inode_dirty(inode
);
1161 d_instantiate(dentry
, inode
);
1165 /* Anybody can rename anything with this: the permission checks are left to the
1166 * higher-level routines.
1168 int udf_rename (struct inode
* old_dir
, struct dentry
* old_dentry
,
1169 struct inode
* new_dir
, struct dentry
* new_dentry
)
1171 struct inode
* old_inode
, * new_inode
;
1172 struct udf_fileident_bh ofibh
, nfibh
;
1173 struct FileIdentDesc
*ofi
= NULL
, *nfi
= NULL
, *dir_fi
= NULL
, ocfi
, ncfi
;
1174 struct buffer_head
*dir_bh
= NULL
;
1175 int retval
= -ENOENT
;
1177 old_inode
= old_dentry
->d_inode
;
1178 ofi
= udf_find_entry(old_dir
, old_dentry
, &ofibh
, &ocfi
);
1179 if (!ofi
|| udf_get_lb_pblock(old_dir
->i_sb
, lelb_to_cpu(ocfi
.icb
.extLocation
), 0) !=
1185 new_inode
= new_dentry
->d_inode
;
1186 nfi
= udf_find_entry(new_dir
, new_dentry
, &nfibh
, &ncfi
);
1191 if (nfibh
.sbh
!= nfibh
.ebh
)
1192 udf_release_data(nfibh
.ebh
);
1193 udf_release_data(nfibh
.sbh
);
1199 DQUOT_INIT(new_inode);
1203 if (S_ISDIR(old_inode
->i_mode
))
1205 Uint32 offset
= UDF_I_EXT0OFFS(old_inode
);
1209 retval
= -ENOTEMPTY
;
1210 if (!empty_dir(new_inode
))
1214 dir_bh
= udf_bread(old_inode
, 0, 0, &retval
);
1217 dir_fi
= udf_get_fileident(dir_bh
->b_data
, old_inode
->i_sb
->s_blocksize
, &offset
);
1220 if (udf_get_lb_pblock(old_inode
->i_sb
, cpu_to_lelb(dir_fi
->icb
.extLocation
), 0) !=
1226 if (!new_inode
&& new_dir
->i_nlink
>= (256<<sizeof(new_dir
->i_nlink
))-1)
1231 nfi
= udf_add_entry(new_dir
, new_dentry
, &nfibh
, &ncfi
, &retval
);
1235 new_dir
->i_version
= ++event
;
1240 ncfi
.fileVersionNum
= ocfi
.fileVersionNum
;
1241 ncfi
.fileCharacteristics
= ocfi
.fileCharacteristics
;
1242 memcpy(&(ncfi
.icb
), &(ocfi
.icb
), sizeof(long_ad
));
1243 udf_write_fi(&ncfi
, nfi
, &nfibh
, NULL
, NULL
);
1245 udf_delete_entry(ofi
, &ofibh
, &ocfi
);
1247 old_dir
->i_version
= ++event
;
1250 new_inode
->i_nlink
--;
1251 new_inode
->i_ctime
= CURRENT_TIME
;
1252 UDF_I_UCTIME(new_inode
) = CURRENT_UTIME
;
1253 mark_inode_dirty(new_inode
);
1255 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME
;
1256 UDF_I_UCTIME(old_dir
) = UDF_I_UMTIME(old_dir
) = CURRENT_UTIME
;
1257 mark_inode_dirty(old_dir
);
1261 dir_fi
->icb
.extLocation
= lelb_to_cpu(UDF_I_LOCATION(new_dir
));
1262 udf_update_tag((char *)dir_fi
, sizeof(struct FileIdentDesc
) +
1263 cpu_to_le16(dir_fi
->lengthOfImpUse
));
1264 if (UDF_I_ALLOCTYPE(old_inode
) == ICB_FLAG_AD_IN_ICB
)
1266 mark_inode_dirty(old_inode
);
1267 old_inode
->i_version
= ++event
;
1270 mark_buffer_dirty(dir_bh
, 1);
1271 old_dir
->i_nlink
--;
1272 mark_inode_dirty(old_dir
);
1275 new_inode
->i_nlink
--;
1276 mark_inode_dirty(new_inode
);
1280 new_dir
->i_nlink
++;
1281 mark_inode_dirty(new_dir
);
1288 udf_release_data(dir_bh
);
1291 if (ofibh
.sbh
!= ofibh
.ebh
)
1292 udf_release_data(ofibh
.ebh
);
1293 udf_release_data(ofibh
.sbh
);
1297 if (nfibh
.sbh
!= nfibh
.ebh
)
1298 udf_release_data(nfibh
.ebh
);
1299 udf_release_data(nfibh
.sbh
);