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@hpesjro.fc.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-2004 Ben Fennema
19 * (C) 1999-2000 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
31 #include <linux/string.h>
32 #include <linux/errno.h>
34 #include <linux/slab.h>
35 #include <linux/quotaops.h>
36 #include <linux/smp_lock.h>
37 #include <linux/buffer_head.h>
39 static inline int udf_match(int len1, const char *name1, int len2, const char *name2)
43 return !memcmp(name1, name2, len1);
46 int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
47 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
48 uint8_t *impuse, uint8_t *fileident)
50 uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(tag);
55 uint16_t liu = le16_to_cpu(cfi->lengthOfImpUse);
56 uint8_t lfi = cfi->lengthFileIdent;
57 int padlen = fibh->eoffset - fibh->soffset - liu - lfi -
58 sizeof(struct fileIdentDesc);
61 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
64 offset = fibh->soffset + sizeof(struct fileIdentDesc);
68 if (adinicb || (offset + liu < 0))
69 memcpy((uint8_t *)sfi->impUse, impuse, liu);
71 memcpy(fibh->ebh->b_data + offset, impuse, liu);
74 memcpy((uint8_t *)sfi->impUse, impuse, -offset);
75 memcpy(fibh->ebh->b_data, impuse - offset, liu + offset);
83 if (adinicb || (offset + lfi < 0))
84 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
86 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
89 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, -offset);
90 memcpy(fibh->ebh->b_data, fileident - offset, lfi + offset);
96 if (adinicb || (offset + padlen < 0))
97 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
99 memset(fibh->ebh->b_data + offset, 0x00, padlen);
102 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
103 memset(fibh->ebh->b_data, 0x00, padlen + offset);
106 crc = udf_crc((uint8_t *)cfi + sizeof(tag), sizeof(struct fileIdentDesc) -
109 if (fibh->sbh == fibh->ebh)
110 crc = udf_crc((uint8_t *)sfi->impUse,
111 crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
112 else if (sizeof(struct fileIdentDesc) >= -fibh->soffset)
113 crc = udf_crc(fibh->ebh->b_data + sizeof(struct fileIdentDesc) + fibh->soffset,
114 crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
117 crc = udf_crc((uint8_t *)sfi->impUse,
118 -fibh->soffset - sizeof(struct fileIdentDesc), crc);
119 crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
122 cfi->descTag.descCRC = cpu_to_le16(crc);
123 cfi->descTag.descCRCLength = cpu_to_le16(crclen);
127 checksum += ((uint8_t *)&cfi->descTag)[i];
129 cfi->descTag.tagChecksum = checksum;
130 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset))
131 memcpy((uint8_t *)sfi, (uint8_t *)cfi, sizeof(struct fileIdentDesc));
134 memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset);
135 memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset,
136 sizeof(struct fileIdentDesc) + fibh->soffset);
140 mark_inode_dirty(inode);
143 if (fibh->sbh != fibh->ebh)
144 mark_buffer_dirty_inode(fibh->ebh, inode);
145 mark_buffer_dirty_inode(fibh->sbh, inode);
150 static struct fileIdentDesc *
151 udf_find_entry(struct inode *dir, struct dentry *dentry,
152 struct udf_fileident_bh *fibh,
153 struct fileIdentDesc *cfi)
155 struct fileIdentDesc *fi=NULL;
158 char fname[UDF_NAME_LEN];
162 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
163 kernel_lb_addr bloc, eloc;
164 uint32_t extoffset, elen, offset;
165 struct buffer_head *bh = NULL;
170 f_pos = (udf_ext0_offset(dir) >> 2);
172 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
173 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
174 fibh->sbh = fibh->ebh = NULL;
175 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
176 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == (EXT_RECORDED_ALLOCATED >> 30))
178 offset >>= dir->i_sb->s_blocksize_bits;
179 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
180 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
182 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
183 extoffset -= sizeof(short_ad);
184 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
185 extoffset -= sizeof(long_ad);
190 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))
192 udf_release_data(bh);
198 udf_release_data(bh);
202 while ( (f_pos < size) )
204 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &bloc, &extoffset, &eloc, &elen, &offset, &bh);
208 if (fibh->sbh != fibh->ebh)
209 udf_release_data(fibh->ebh);
210 udf_release_data(fibh->sbh);
211 udf_release_data(bh);
215 liu = le16_to_cpu(cfi->lengthOfImpUse);
216 lfi = cfi->lengthFileIdent;
218 if (fibh->sbh == fibh->ebh)
220 nameptr = fi->fileIdent + liu;
224 int poffset; /* Unpaded ending offset */
226 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
229 nameptr = (uint8_t *)(fibh->ebh->b_data + poffset - lfi);
233 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
234 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
238 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0 )
240 if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE) )
244 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0 )
246 if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE) )
253 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)))
255 if (udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name))
257 udf_release_data(bh);
262 if (fibh->sbh != fibh->ebh)
263 udf_release_data(fibh->ebh);
264 udf_release_data(fibh->sbh);
265 udf_release_data(bh);
273 * Look-up the inode for a given name.
276 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
277 * available for a directory. The filesystem is useless if this routine is
278 * not available for at least the filesystem's root directory.
280 * This routine is passed an incomplete dentry - it must be completed by
281 * calling d_add(dentry, inode). If the name does not exist, then the
282 * specified inode must be set to null. An error should only be returned
283 * when the lookup fails for a reason other than the name not existing.
284 * Note that the directory inode semaphore is held during the call.
286 * Refer to lookup_dentry() in fs/namei.c
287 * lookup_dentry() -> lookup() -> real_lookup() -> .
290 * dir Pointer to inode of parent directory.
291 * dentry Pointer to dentry to complete.
292 * nd Pointer to lookup nameidata
295 * <return> Zero on success.
298 * July 1, 1997 - Andrew E. Mileski
299 * Written, tested, and released.
302 static struct dentry *
303 udf_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
305 struct inode *inode = NULL;
306 struct fileIdentDesc cfi, *fi;
307 struct udf_fileident_bh fibh;
309 if (dentry->d_name.len > UDF_NAME_LEN-2)
310 return ERR_PTR(-ENAMETOOLONG);
314 /* temporary shorthand for specifying files by inode number */
315 if (!strncmp(dentry->d_name.name, ".B=", 3) )
317 kernel_lb_addr lb = { 0, simple_strtoul(dentry->d_name.name+3, NULL, 0) };
318 inode = udf_iget(dir->i_sb, lb);
322 return ERR_PTR(-EACCES);
326 #endif /* UDF_RECOVERY */
328 if ((fi = udf_find_entry(dir, dentry, &fibh, &cfi)))
330 if (fibh.sbh != fibh.ebh)
331 udf_release_data(fibh.ebh);
332 udf_release_data(fibh.sbh);
334 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
338 return ERR_PTR(-EACCES);
342 d_add(dentry, inode);
346 static struct fileIdentDesc *
347 udf_add_entry(struct inode *dir, struct dentry *dentry,
348 struct udf_fileident_bh *fibh,
349 struct fileIdentDesc *cfi, int *err)
351 struct super_block *sb;
352 struct fileIdentDesc *fi=NULL;
353 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
358 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
363 kernel_lb_addr bloc, eloc;
364 uint32_t extoffset, elen, offset;
365 struct buffer_head *bh = NULL;
371 if (!dentry->d_name.len)
377 if ( !(namelen = udf_put_filename(sb, dentry->d_name.name, name, dentry->d_name.len)))
379 *err = -ENAMETOOLONG;
386 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
388 f_pos = (udf_ext0_offset(dir) >> 2);
390 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
391 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
392 fibh->sbh = fibh->ebh = NULL;
393 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
394 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == (EXT_RECORDED_ALLOCATED >> 30))
396 offset >>= dir->i_sb->s_blocksize_bits;
397 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
398 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
400 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
401 extoffset -= sizeof(short_ad);
402 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
403 extoffset -= sizeof(long_ad);
408 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))
410 udf_release_data(bh);
415 block = UDF_I_LOCATION(dir).logicalBlockNum;
420 block = udf_get_lb_pblock(dir->i_sb, UDF_I_LOCATION(dir), 0);
421 fibh->sbh = fibh->ebh = NULL;
422 fibh->soffset = fibh->eoffset = sb->s_blocksize;
426 while ( (f_pos < size) )
428 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &bloc, &extoffset, &eloc, &elen, &offset, &bh);
432 if (fibh->sbh != fibh->ebh)
433 udf_release_data(fibh->ebh);
434 udf_release_data(fibh->sbh);
435 udf_release_data(bh);
440 liu = le16_to_cpu(cfi->lengthOfImpUse);
441 lfi = cfi->lengthFileIdent;
443 if (fibh->sbh == fibh->ebh)
444 nameptr = fi->fileIdent + liu;
447 int poffset; /* Unpaded ending offset */
449 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
452 nameptr = (char *)(fibh->ebh->b_data + poffset - lfi);
456 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
457 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
461 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0 )
463 if (((sizeof(struct fileIdentDesc) + liu + lfi + 3) & ~3) == nfidlen)
465 udf_release_data(bh);
466 cfi->descTag.tagSerialNum = cpu_to_le16(1);
467 cfi->fileVersionNum = cpu_to_le16(1);
468 cfi->fileCharacteristics = 0;
469 cfi->lengthFileIdent = namelen;
470 cfi->lengthOfImpUse = cpu_to_le16(0);
471 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name))
484 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)) &&
485 udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name))
487 if (fibh->sbh != fibh->ebh)
488 udf_release_data(fibh->ebh);
489 udf_release_data(fibh->sbh);
490 udf_release_data(bh);
499 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB &&
500 sb->s_blocksize - fibh->eoffset < nfidlen)
502 udf_release_data(bh);
504 fibh->soffset -= udf_ext0_offset(dir);
505 fibh->eoffset -= udf_ext0_offset(dir);
506 f_pos -= (udf_ext0_offset(dir) >> 2);
507 if (fibh->sbh != fibh->ebh)
508 udf_release_data(fibh->ebh);
509 udf_release_data(fibh->sbh);
510 if (!(fibh->sbh = fibh->ebh = udf_expand_dir_adinicb(dir, &block, err)))
512 bloc = UDF_I_LOCATION(dir);
513 eloc.logicalBlockNum = block;
514 eloc.partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
515 elen = dir->i_sb->s_blocksize;
516 extoffset = udf_file_entry_alloc_offset(dir);
517 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
518 extoffset += sizeof(short_ad);
519 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
520 extoffset += sizeof(long_ad);
523 if (sb->s_blocksize - fibh->eoffset >= nfidlen)
525 fibh->soffset = fibh->eoffset;
526 fibh->eoffset += nfidlen;
527 if (fibh->sbh != fibh->ebh)
529 udf_release_data(fibh->sbh);
530 fibh->sbh = fibh->ebh;
533 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
535 block = UDF_I_LOCATION(dir).logicalBlockNum;
536 fi = (struct fileIdentDesc *)(UDF_I_DATA(dir) + fibh->soffset - udf_ext0_offset(dir) + UDF_I_LENEATTR(dir));
540 block = eloc.logicalBlockNum + ((elen - 1) >>
541 dir->i_sb->s_blocksize_bits);
542 fi = (struct fileIdentDesc *)(fibh->sbh->b_data + fibh->soffset);
547 fibh->soffset = fibh->eoffset - sb->s_blocksize;
548 fibh->eoffset += nfidlen - sb->s_blocksize;
549 if (fibh->sbh != fibh->ebh)
551 udf_release_data(fibh->sbh);
552 fibh->sbh = fibh->ebh;
555 block = eloc.logicalBlockNum + ((elen - 1) >>
556 dir->i_sb->s_blocksize_bits);
558 if (!(fibh->ebh = udf_bread(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2), 1, err)))
560 udf_release_data(bh);
561 udf_release_data(fibh->sbh);
565 if (!(fibh->soffset))
567 if (udf_next_aext(dir, &bloc, &extoffset, &eloc, &elen, &bh, 1) ==
568 (EXT_RECORDED_ALLOCATED >> 30))
570 block = eloc.logicalBlockNum + ((elen - 1) >>
571 dir->i_sb->s_blocksize_bits);
576 udf_release_data(fibh->sbh);
577 fibh->sbh = fibh->ebh;
578 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
582 fi = (struct fileIdentDesc *)
583 (fibh->sbh->b_data + sb->s_blocksize + fibh->soffset);
587 memset(cfi, 0, sizeof(struct fileIdentDesc));
588 if (UDF_SB_UDFREV(sb) >= 0x0200)
589 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block, sizeof(tag));
591 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block, sizeof(tag));
592 cfi->fileVersionNum = cpu_to_le16(1);
593 cfi->lengthFileIdent = namelen;
594 cfi->lengthOfImpUse = cpu_to_le16(0);
595 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name))
597 udf_release_data(bh);
598 dir->i_size += nfidlen;
599 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
600 UDF_I_LENALLOC(dir) += nfidlen;
601 mark_inode_dirty(dir);
606 udf_release_data(bh);
607 if (fibh->sbh != fibh->ebh)
608 udf_release_data(fibh->ebh);
609 udf_release_data(fibh->sbh);
615 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
616 struct udf_fileident_bh *fibh, struct fileIdentDesc *cfi)
618 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
619 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
620 memset(&(cfi->icb), 0x00, sizeof(long_ad));
621 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
624 static int udf_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
626 struct udf_fileident_bh fibh;
628 struct fileIdentDesc cfi, *fi;
632 inode = udf_new_inode(dir, mode, &err);
639 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
640 inode->i_data.a_ops = &udf_adinicb_aops;
642 inode->i_data.a_ops = &udf_aops;
643 inode->i_op = &udf_file_inode_operations;
644 inode->i_fop = &udf_file_operations;
645 inode->i_mode = mode;
646 mark_inode_dirty(inode);
648 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
651 mark_inode_dirty(inode);
656 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
657 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
658 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
659 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
660 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
661 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
663 mark_inode_dirty(dir);
665 if (fibh.sbh != fibh.ebh)
666 udf_release_data(fibh.ebh);
667 udf_release_data(fibh.sbh);
669 d_instantiate(dentry, inode);
673 static int udf_mknod(struct inode * dir, struct dentry * dentry, int mode, dev_t rdev)
675 struct inode * inode;
676 struct udf_fileident_bh fibh;
677 struct fileIdentDesc cfi, *fi;
680 if (!old_valid_dev(rdev))
685 inode = udf_new_inode(dir, mode, &err);
689 inode->i_uid = current->fsuid;
690 init_special_inode(inode, mode, rdev);
691 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
694 mark_inode_dirty(inode);
699 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
700 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
701 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
702 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
703 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
704 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
706 mark_inode_dirty(dir);
708 mark_inode_dirty(inode);
710 if (fibh.sbh != fibh.ebh)
711 udf_release_data(fibh.ebh);
712 udf_release_data(fibh.sbh);
713 d_instantiate(dentry, inode);
720 static int udf_mkdir(struct inode * dir, struct dentry * dentry, int mode)
722 struct inode * inode;
723 struct udf_fileident_bh fibh;
724 struct fileIdentDesc cfi, *fi;
729 if (dir->i_nlink >= (256<<sizeof(dir->i_nlink))-1)
733 inode = udf_new_inode(dir, S_IFDIR, &err);
737 inode->i_op = &udf_dir_inode_operations;
738 inode->i_fop = &udf_dir_operations;
739 if (!(fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err)))
742 mark_inode_dirty(inode);
747 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
748 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(dir));
749 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
750 cpu_to_le32(UDF_I_UNIQUE(dir) & 0x00000000FFFFFFFFUL);
751 cfi.fileCharacteristics = FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
752 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
753 udf_release_data(fibh.sbh);
754 inode->i_mode = S_IFDIR | mode;
755 if (dir->i_mode & S_ISGID)
756 inode->i_mode |= S_ISGID;
757 mark_inode_dirty(inode);
759 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
762 mark_inode_dirty(inode);
766 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
767 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
768 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
769 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
770 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
771 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
773 mark_inode_dirty(dir);
774 d_instantiate(dentry, inode);
775 if (fibh.sbh != fibh.ebh)
776 udf_release_data(fibh.ebh);
777 udf_release_data(fibh.sbh);
784 static int empty_dir(struct inode *dir)
786 struct fileIdentDesc *fi, cfi;
787 struct udf_fileident_bh fibh;
789 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
791 kernel_lb_addr bloc, eloc;
792 uint32_t extoffset, elen, offset;
793 struct buffer_head *bh = NULL;
795 f_pos = (udf_ext0_offset(dir) >> 2);
797 fibh.soffset = fibh.eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
799 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
800 fibh.sbh = fibh.ebh = NULL;
801 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
802 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == (EXT_RECORDED_ALLOCATED >> 30))
804 offset >>= dir->i_sb->s_blocksize_bits;
805 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
806 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
808 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
809 extoffset -= sizeof(short_ad);
810 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
811 extoffset -= sizeof(long_ad);
816 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block)))
818 udf_release_data(bh);
824 udf_release_data(bh);
829 while ( (f_pos < size) )
831 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &bloc, &extoffset, &eloc, &elen, &offset, &bh);
835 if (fibh.sbh != fibh.ebh)
836 udf_release_data(fibh.ebh);
837 udf_release_data(fibh.sbh);
838 udf_release_data(bh);
842 if (cfi.lengthFileIdent && (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0)
844 if (fibh.sbh != fibh.ebh)
845 udf_release_data(fibh.ebh);
846 udf_release_data(fibh.sbh);
847 udf_release_data(bh);
851 if (fibh.sbh != fibh.ebh)
852 udf_release_data(fibh.ebh);
853 udf_release_data(fibh.sbh);
854 udf_release_data(bh);
858 static int udf_rmdir(struct inode * dir, struct dentry * dentry)
861 struct inode * inode = dentry->d_inode;
862 struct udf_fileident_bh fibh;
863 struct fileIdentDesc *fi, cfi;
868 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
873 tloc = lelb_to_cpu(cfi.icb.extLocation);
874 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
877 if (!empty_dir(inode))
879 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
882 if (inode->i_nlink != 2)
883 udf_warning(inode->i_sb, "udf_rmdir",
884 "empty directory has nlink != 2 (%d)",
888 mark_inode_dirty(inode);
890 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
891 mark_inode_dirty(dir);
894 if (fibh.sbh != fibh.ebh)
895 udf_release_data(fibh.ebh);
896 udf_release_data(fibh.sbh);
902 static int udf_unlink(struct inode * dir, struct dentry * dentry)
905 struct inode * inode = dentry->d_inode;
906 struct udf_fileident_bh fibh;
907 struct fileIdentDesc *fi;
908 struct fileIdentDesc cfi;
913 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
918 tloc = lelb_to_cpu(cfi.icb.extLocation);
919 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
924 udf_debug("Deleting nonexistent file (%lu), %d\n",
925 inode->i_ino, inode->i_nlink);
928 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
931 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
932 mark_inode_dirty(dir);
934 mark_inode_dirty(inode);
935 inode->i_ctime = dir->i_ctime;
939 if (fibh.sbh != fibh.ebh)
940 udf_release_data(fibh.ebh);
941 udf_release_data(fibh.sbh);
947 static int udf_symlink(struct inode * dir, struct dentry * dentry, const char * symname)
949 struct inode * inode;
950 struct pathComponent *pc;
952 struct udf_fileident_bh fibh;
953 struct buffer_head *bh = NULL;
954 int eoffset, elen = 0;
955 struct fileIdentDesc *fi;
956 struct fileIdentDesc cfi;
960 char name[UDF_NAME_LEN];
964 if (!(inode = udf_new_inode(dir, S_IFLNK, &err)))
967 inode->i_mode = S_IFLNK | S_IRWXUGO;
968 inode->i_data.a_ops = &udf_symlink_aops;
969 inode->i_op = &page_symlink_inode_operations;
971 if (UDF_I_ALLOCTYPE(inode) != ICBTAG_FLAG_AD_IN_ICB)
973 struct buffer_head *bh = NULL;
974 kernel_lb_addr bloc, eloc;
975 uint32_t elen, extoffset;
977 block = udf_new_block(inode->i_sb, inode,
978 UDF_I_LOCATION(inode).partitionReferenceNum,
979 UDF_I_LOCATION(inode).logicalBlockNum, &err);
982 bloc = UDF_I_LOCATION(inode);
983 eloc.logicalBlockNum = block;
984 eloc.partitionReferenceNum = UDF_I_LOCATION(inode).partitionReferenceNum;
985 elen = inode->i_sb->s_blocksize;
986 UDF_I_LENEXTENTS(inode) = elen;
987 extoffset = udf_file_entry_alloc_offset(inode);
988 udf_add_aext(inode, &bloc, &extoffset, eloc, elen, &bh, 0);
989 udf_release_data(bh);
991 block = udf_get_pblock(inode->i_sb, block,
992 UDF_I_LOCATION(inode).partitionReferenceNum, 0);
993 bh = udf_tread(inode->i_sb, block);
995 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
996 set_buffer_uptodate(bh);
998 mark_buffer_dirty_inode(bh, inode);
999 ea = bh->b_data + udf_ext0_offset(inode);
1002 ea = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
1004 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
1005 pc = (struct pathComponent *)ea;
1007 if (*symname == '/')
1012 } while (*symname == '/');
1014 pc->componentType = 1;
1015 pc->lengthComponentIdent = 0;
1016 pc->componentFileVersionNum = 0;
1017 pc += sizeof(struct pathComponent);
1018 elen += sizeof(struct pathComponent);
1021 err = -ENAMETOOLONG;
1025 if (elen + sizeof(struct pathComponent) > eoffset)
1028 pc = (struct pathComponent *)(ea + elen);
1030 compstart = (char *)symname;
1035 } while (*symname && *symname != '/');
1037 pc->componentType = 5;
1038 pc->lengthComponentIdent = 0;
1039 pc->componentFileVersionNum = 0;
1040 if (compstart[0] == '.')
1042 if ((symname-compstart) == 1)
1043 pc->componentType = 4;
1044 else if ((symname-compstart) == 2 && compstart[1] == '.')
1045 pc->componentType = 3;
1048 if (pc->componentType == 5)
1050 if ( !(namelen = udf_put_filename(inode->i_sb, compstart, name, symname-compstart)))
1053 if (elen + sizeof(struct pathComponent) + namelen > eoffset)
1056 pc->lengthComponentIdent = namelen;
1058 memcpy(pc->componentIdent, name, namelen);
1061 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1068 } while (*symname == '/');
1072 udf_release_data(bh);
1073 inode->i_size = elen;
1074 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1075 UDF_I_LENALLOC(inode) = inode->i_size;
1076 mark_inode_dirty(inode);
1078 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1080 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1081 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1082 if (UDF_SB_LVIDBH(inode->i_sb))
1084 struct logicalVolHeaderDesc *lvhd;
1086 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1087 uniqueID = le64_to_cpu(lvhd->uniqueID);
1088 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1089 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1090 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1092 lvhd->uniqueID = cpu_to_le64(uniqueID);
1093 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1095 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1096 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
1098 mark_inode_dirty(dir);
1100 if (fibh.sbh != fibh.ebh)
1101 udf_release_data(fibh.ebh);
1102 udf_release_data(fibh.sbh);
1103 d_instantiate(dentry, inode);
1112 mark_inode_dirty(inode);
1117 static int udf_link(struct dentry * old_dentry, struct inode * dir,
1118 struct dentry *dentry)
1120 struct inode *inode = old_dentry->d_inode;
1121 struct udf_fileident_bh fibh;
1122 struct fileIdentDesc cfi, *fi;
1126 if (inode->i_nlink >= (256<<sizeof(inode->i_nlink))-1)
1132 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1137 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1138 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1139 if (UDF_SB_LVIDBH(inode->i_sb))
1141 struct logicalVolHeaderDesc *lvhd;
1143 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1144 uniqueID = le64_to_cpu(lvhd->uniqueID);
1145 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1146 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1147 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1149 lvhd->uniqueID = cpu_to_le64(uniqueID);
1150 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1152 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1153 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
1155 mark_inode_dirty(dir);
1157 if (fibh.sbh != fibh.ebh)
1158 udf_release_data(fibh.ebh);
1159 udf_release_data(fibh.sbh);
1161 inode->i_ctime = current_fs_time(inode->i_sb);
1162 mark_inode_dirty(inode);
1163 atomic_inc(&inode->i_count);
1164 d_instantiate(dentry, inode);
1169 /* Anybody can rename anything with this: the permission checks are left to the
1170 * higher-level routines.
1172 static int udf_rename (struct inode * old_dir, struct dentry * old_dentry,
1173 struct inode * new_dir, struct dentry * new_dentry)
1175 struct inode * old_inode = old_dentry->d_inode;
1176 struct inode * new_inode = new_dentry->d_inode;
1177 struct udf_fileident_bh ofibh, nfibh;
1178 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL, ocfi, ncfi;
1179 struct buffer_head *dir_bh = NULL;
1180 int retval = -ENOENT;
1181 kernel_lb_addr tloc;
1184 if ((ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi)))
1186 if (ofibh.sbh != ofibh.ebh)
1187 udf_release_data(ofibh.ebh);
1188 udf_release_data(ofibh.sbh);
1190 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1191 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
1192 != old_inode->i_ino)
1195 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1200 if (nfibh.sbh != nfibh.ebh)
1201 udf_release_data(nfibh.ebh);
1202 udf_release_data(nfibh.sbh);
1206 if (S_ISDIR(old_inode->i_mode))
1208 uint32_t offset = udf_ext0_offset(old_inode);
1212 retval = -ENOTEMPTY;
1213 if (!empty_dir(new_inode))
1217 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB)
1219 dir_fi = udf_get_fileident(UDF_I_DATA(old_inode) -
1220 (UDF_I_EFE(old_inode) ?
1221 sizeof(struct extendedFileEntry) :
1222 sizeof(struct fileEntry)),
1223 old_inode->i_sb->s_blocksize, &offset);
1227 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1230 dir_fi = udf_get_fileident(dir_bh->b_data, old_inode->i_sb->s_blocksize, &offset);
1234 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1235 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0)
1240 if (!new_inode && new_dir->i_nlink >= (256<<sizeof(new_dir->i_nlink))-1)
1245 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, &retval);
1251 * Like most other Unix systems, set the ctime for inodes on a
1254 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1255 mark_inode_dirty(old_inode);
1260 ncfi.fileVersionNum = ocfi.fileVersionNum;
1261 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1262 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1263 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1265 /* The old fid may have moved - find it again */
1266 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1267 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1271 new_inode->i_nlink--;
1272 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1273 mark_inode_dirty(new_inode);
1275 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1276 mark_inode_dirty(old_dir);
1280 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(new_dir));
1281 udf_update_tag((char *)dir_fi, (sizeof(struct fileIdentDesc) +
1282 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1283 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB)
1285 mark_inode_dirty(old_inode);
1288 mark_buffer_dirty_inode(dir_bh, old_inode);
1289 old_dir->i_nlink --;
1290 mark_inode_dirty(old_dir);
1293 new_inode->i_nlink --;
1294 mark_inode_dirty(new_inode);
1298 new_dir->i_nlink ++;
1299 mark_inode_dirty(new_dir);
1305 if (ofibh.sbh != ofibh.ebh)
1306 udf_release_data(ofibh.ebh);
1307 udf_release_data(ofibh.sbh);
1313 udf_release_data(dir_bh);
1316 if (nfibh.sbh != nfibh.ebh)
1317 udf_release_data(nfibh.ebh);
1318 udf_release_data(nfibh.sbh);
1324 struct inode_operations udf_dir_inode_operations = {
1325 .lookup = udf_lookup,
1326 .create = udf_create,
1328 .unlink = udf_unlink,
1329 .symlink = udf_symlink,
1333 .rename = udf_rename,