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/quotaops.h>
31 #include <linux/smp_lock.h>
32 #include <linux/buffer_head.h>
34 static inline int udf_match(int len1, const char *name1, int len2, const char *name2)
38 return !memcmp(name1, name2, len1);
41 int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
42 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
43 uint8_t *impuse, uint8_t *fileident)
45 uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(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_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
59 offset = fibh->soffset + sizeof(struct fileIdentDesc);
63 if (adinicb || (offset + liu < 0))
64 memcpy((uint8_t *)sfi->impUse, impuse, liu);
66 memcpy(fibh->ebh->b_data + offset, impuse, liu);
69 memcpy((uint8_t *)sfi->impUse, impuse, -offset);
70 memcpy(fibh->ebh->b_data, impuse - offset, liu + offset);
78 if (adinicb || (offset + lfi < 0))
79 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
81 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
84 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, -offset);
85 memcpy(fibh->ebh->b_data, fileident - offset, lfi + offset);
91 if (adinicb || (offset + padlen < 0))
92 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
94 memset(fibh->ebh->b_data + offset, 0x00, padlen);
97 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
98 memset(fibh->ebh->b_data, 0x00, padlen + offset);
101 crc = udf_crc((uint8_t *)cfi + sizeof(tag), sizeof(struct fileIdentDesc) -
104 if (fibh->sbh == fibh->ebh)
105 crc = udf_crc((uint8_t *)sfi->impUse,
106 crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
107 else if (sizeof(struct fileIdentDesc) >= -fibh->soffset)
108 crc = udf_crc(fibh->ebh->b_data + sizeof(struct fileIdentDesc) + fibh->soffset,
109 crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
112 crc = udf_crc((uint8_t *)sfi->impUse,
113 -fibh->soffset - sizeof(struct fileIdentDesc), crc);
114 crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
117 cfi->descTag.descCRC = cpu_to_le16(crc);
118 cfi->descTag.descCRCLength = cpu_to_le16(crclen);
122 checksum += ((uint8_t *)&cfi->descTag)[i];
124 cfi->descTag.tagChecksum = checksum;
125 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset))
126 memcpy((uint8_t *)sfi, (uint8_t *)cfi, sizeof(struct fileIdentDesc));
129 memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset);
130 memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset,
131 sizeof(struct fileIdentDesc) + fibh->soffset);
135 mark_inode_dirty(inode);
138 if (fibh->sbh != fibh->ebh)
139 mark_buffer_dirty_inode(fibh->ebh, inode);
140 mark_buffer_dirty_inode(fibh->sbh, inode);
145 static struct fileIdentDesc *
146 udf_find_entry(struct inode *dir, struct dentry *dentry,
147 struct udf_fileident_bh *fibh,
148 struct fileIdentDesc *cfi)
150 struct fileIdentDesc *fi=NULL;
153 char fname[UDF_NAME_LEN];
158 kernel_lb_addr bloc, eloc;
159 uint32_t extoffset, elen;
161 struct buffer_head *bh = NULL;
163 size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
164 f_pos = (udf_ext0_offset(dir) >> 2);
166 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
167 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
168 fibh->sbh = fibh->ebh = NULL;
169 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
170 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == (EXT_RECORDED_ALLOCATED >> 30))
172 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
173 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
175 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
176 extoffset -= sizeof(short_ad);
177 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
178 extoffset -= sizeof(long_ad);
183 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))
185 udf_release_data(bh);
191 udf_release_data(bh);
195 while ( (f_pos < size) )
197 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &bloc, &extoffset, &eloc, &elen, &offset, &bh);
201 if (fibh->sbh != fibh->ebh)
202 udf_release_data(fibh->ebh);
203 udf_release_data(fibh->sbh);
204 udf_release_data(bh);
208 liu = le16_to_cpu(cfi->lengthOfImpUse);
209 lfi = cfi->lengthFileIdent;
211 if (fibh->sbh == fibh->ebh)
213 nameptr = fi->fileIdent + liu;
217 int poffset; /* Unpaded ending offset */
219 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
222 nameptr = (uint8_t *)(fibh->ebh->b_data + poffset - lfi);
226 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
227 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
231 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0 )
233 if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE) )
237 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0 )
239 if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE) )
246 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)))
248 if (udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name))
250 udf_release_data(bh);
255 if (fibh->sbh != fibh->ebh)
256 udf_release_data(fibh->ebh);
257 udf_release_data(fibh->sbh);
258 udf_release_data(bh);
266 * Look-up the inode for a given name.
269 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
270 * available for a directory. The filesystem is useless if this routine is
271 * not available for at least the filesystem's root directory.
273 * This routine is passed an incomplete dentry - it must be completed by
274 * calling d_add(dentry, inode). If the name does not exist, then the
275 * specified inode must be set to null. An error should only be returned
276 * when the lookup fails for a reason other than the name not existing.
277 * Note that the directory inode semaphore is held during the call.
279 * Refer to lookup_dentry() in fs/namei.c
280 * lookup_dentry() -> lookup() -> real_lookup() -> .
283 * dir Pointer to inode of parent directory.
284 * dentry Pointer to dentry to complete.
285 * nd Pointer to lookup nameidata
288 * <return> Zero on success.
291 * July 1, 1997 - Andrew E. Mileski
292 * Written, tested, and released.
295 static struct dentry *
296 udf_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
298 struct inode *inode = NULL;
299 struct fileIdentDesc cfi;
300 struct udf_fileident_bh fibh;
302 if (dentry->d_name.len > UDF_NAME_LEN-2)
303 return ERR_PTR(-ENAMETOOLONG);
307 /* temporary shorthand for specifying files by inode number */
308 if (!strncmp(dentry->d_name.name, ".B=", 3) )
310 kernel_lb_addr lb = { 0, simple_strtoul(dentry->d_name.name+3, NULL, 0) };
311 inode = udf_iget(dir->i_sb, lb);
315 return ERR_PTR(-EACCES);
319 #endif /* UDF_RECOVERY */
321 if (udf_find_entry(dir, dentry, &fibh, &cfi))
323 if (fibh.sbh != fibh.ebh)
324 udf_release_data(fibh.ebh);
325 udf_release_data(fibh.sbh);
327 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
331 return ERR_PTR(-EACCES);
335 d_add(dentry, inode);
339 static struct fileIdentDesc *
340 udf_add_entry(struct inode *dir, struct dentry *dentry,
341 struct udf_fileident_bh *fibh,
342 struct fileIdentDesc *cfi, int *err)
344 struct super_block *sb;
345 struct fileIdentDesc *fi=NULL;
346 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
351 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
356 kernel_lb_addr bloc, eloc;
357 uint32_t extoffset, elen;
359 struct buffer_head *bh = NULL;
365 if (!dentry->d_name.len)
371 if ( !(namelen = udf_put_filename(sb, dentry->d_name.name, name, dentry->d_name.len)))
373 *err = -ENAMETOOLONG;
380 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
382 f_pos = (udf_ext0_offset(dir) >> 2);
384 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
385 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
386 fibh->sbh = fibh->ebh = NULL;
387 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
388 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == (EXT_RECORDED_ALLOCATED >> 30))
390 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
391 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
393 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
394 extoffset -= sizeof(short_ad);
395 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
396 extoffset -= sizeof(long_ad);
401 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))
403 udf_release_data(bh);
408 block = UDF_I_LOCATION(dir).logicalBlockNum;
413 block = udf_get_lb_pblock(dir->i_sb, UDF_I_LOCATION(dir), 0);
414 fibh->sbh = fibh->ebh = NULL;
415 fibh->soffset = fibh->eoffset = sb->s_blocksize;
419 while ( (f_pos < size) )
421 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &bloc, &extoffset, &eloc, &elen, &offset, &bh);
425 if (fibh->sbh != fibh->ebh)
426 udf_release_data(fibh->ebh);
427 udf_release_data(fibh->sbh);
428 udf_release_data(bh);
433 liu = le16_to_cpu(cfi->lengthOfImpUse);
434 lfi = cfi->lengthFileIdent;
436 if (fibh->sbh == fibh->ebh)
437 nameptr = fi->fileIdent + liu;
440 int poffset; /* Unpaded ending offset */
442 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
445 nameptr = (char *)(fibh->ebh->b_data + poffset - lfi);
449 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
450 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
454 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0 )
456 if (((sizeof(struct fileIdentDesc) + liu + lfi + 3) & ~3) == nfidlen)
458 udf_release_data(bh);
459 cfi->descTag.tagSerialNum = cpu_to_le16(1);
460 cfi->fileVersionNum = cpu_to_le16(1);
461 cfi->fileCharacteristics = 0;
462 cfi->lengthFileIdent = namelen;
463 cfi->lengthOfImpUse = cpu_to_le16(0);
464 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name))
477 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)) &&
478 udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name))
480 if (fibh->sbh != fibh->ebh)
481 udf_release_data(fibh->ebh);
482 udf_release_data(fibh->sbh);
483 udf_release_data(bh);
492 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB &&
493 sb->s_blocksize - fibh->eoffset < nfidlen)
495 udf_release_data(bh);
497 fibh->soffset -= udf_ext0_offset(dir);
498 fibh->eoffset -= udf_ext0_offset(dir);
499 f_pos -= (udf_ext0_offset(dir) >> 2);
500 if (fibh->sbh != fibh->ebh)
501 udf_release_data(fibh->ebh);
502 udf_release_data(fibh->sbh);
503 if (!(fibh->sbh = fibh->ebh = udf_expand_dir_adinicb(dir, &block, err)))
505 bloc = UDF_I_LOCATION(dir);
506 eloc.logicalBlockNum = block;
507 eloc.partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
508 elen = dir->i_sb->s_blocksize;
509 extoffset = udf_file_entry_alloc_offset(dir);
510 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
511 extoffset += sizeof(short_ad);
512 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
513 extoffset += sizeof(long_ad);
516 if (sb->s_blocksize - fibh->eoffset >= nfidlen)
518 fibh->soffset = fibh->eoffset;
519 fibh->eoffset += nfidlen;
520 if (fibh->sbh != fibh->ebh)
522 udf_release_data(fibh->sbh);
523 fibh->sbh = fibh->ebh;
526 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
528 block = UDF_I_LOCATION(dir).logicalBlockNum;
529 fi = (struct fileIdentDesc *)(UDF_I_DATA(dir) + fibh->soffset - udf_ext0_offset(dir) + UDF_I_LENEATTR(dir));
533 block = eloc.logicalBlockNum + ((elen - 1) >>
534 dir->i_sb->s_blocksize_bits);
535 fi = (struct fileIdentDesc *)(fibh->sbh->b_data + fibh->soffset);
540 fibh->soffset = fibh->eoffset - sb->s_blocksize;
541 fibh->eoffset += nfidlen - sb->s_blocksize;
542 if (fibh->sbh != fibh->ebh)
544 udf_release_data(fibh->sbh);
545 fibh->sbh = fibh->ebh;
548 block = eloc.logicalBlockNum + ((elen - 1) >>
549 dir->i_sb->s_blocksize_bits);
551 if (!(fibh->ebh = udf_bread(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2), 1, err)))
553 udf_release_data(bh);
554 udf_release_data(fibh->sbh);
558 if (!(fibh->soffset))
560 if (udf_next_aext(dir, &bloc, &extoffset, &eloc, &elen, &bh, 1) ==
561 (EXT_RECORDED_ALLOCATED >> 30))
563 block = eloc.logicalBlockNum + ((elen - 1) >>
564 dir->i_sb->s_blocksize_bits);
569 udf_release_data(fibh->sbh);
570 fibh->sbh = fibh->ebh;
571 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
575 fi = (struct fileIdentDesc *)
576 (fibh->sbh->b_data + sb->s_blocksize + fibh->soffset);
580 memset(cfi, 0, sizeof(struct fileIdentDesc));
581 if (UDF_SB_UDFREV(sb) >= 0x0200)
582 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block, sizeof(tag));
584 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block, sizeof(tag));
585 cfi->fileVersionNum = cpu_to_le16(1);
586 cfi->lengthFileIdent = namelen;
587 cfi->lengthOfImpUse = cpu_to_le16(0);
588 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name))
590 udf_release_data(bh);
591 dir->i_size += nfidlen;
592 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
593 UDF_I_LENALLOC(dir) += nfidlen;
594 mark_inode_dirty(dir);
599 udf_release_data(bh);
600 if (fibh->sbh != fibh->ebh)
601 udf_release_data(fibh->ebh);
602 udf_release_data(fibh->sbh);
608 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
609 struct udf_fileident_bh *fibh, struct fileIdentDesc *cfi)
611 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
612 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
613 memset(&(cfi->icb), 0x00, sizeof(long_ad));
614 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
617 static int udf_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
619 struct udf_fileident_bh fibh;
621 struct fileIdentDesc cfi, *fi;
625 inode = udf_new_inode(dir, mode, &err);
632 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
633 inode->i_data.a_ops = &udf_adinicb_aops;
635 inode->i_data.a_ops = &udf_aops;
636 inode->i_op = &udf_file_inode_operations;
637 inode->i_fop = &udf_file_operations;
638 inode->i_mode = mode;
639 mark_inode_dirty(inode);
641 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
644 mark_inode_dirty(inode);
649 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
650 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
651 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
652 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
653 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
654 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
656 mark_inode_dirty(dir);
658 if (fibh.sbh != fibh.ebh)
659 udf_release_data(fibh.ebh);
660 udf_release_data(fibh.sbh);
662 d_instantiate(dentry, inode);
666 static int udf_mknod(struct inode * dir, struct dentry * dentry, int mode, dev_t rdev)
668 struct inode * inode;
669 struct udf_fileident_bh fibh;
670 struct fileIdentDesc cfi, *fi;
673 if (!old_valid_dev(rdev))
678 inode = udf_new_inode(dir, mode, &err);
682 inode->i_uid = current->fsuid;
683 init_special_inode(inode, mode, rdev);
684 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
687 mark_inode_dirty(inode);
692 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
693 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
694 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
695 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
696 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
697 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
699 mark_inode_dirty(dir);
701 mark_inode_dirty(inode);
703 if (fibh.sbh != fibh.ebh)
704 udf_release_data(fibh.ebh);
705 udf_release_data(fibh.sbh);
706 d_instantiate(dentry, inode);
713 static int udf_mkdir(struct inode * dir, struct dentry * dentry, int mode)
715 struct inode * inode;
716 struct udf_fileident_bh fibh;
717 struct fileIdentDesc cfi, *fi;
722 if (dir->i_nlink >= (256<<sizeof(dir->i_nlink))-1)
726 inode = udf_new_inode(dir, S_IFDIR, &err);
730 inode->i_op = &udf_dir_inode_operations;
731 inode->i_fop = &udf_dir_operations;
732 if (!(fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err)))
735 mark_inode_dirty(inode);
740 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
741 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(dir));
742 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
743 cpu_to_le32(UDF_I_UNIQUE(dir) & 0x00000000FFFFFFFFUL);
744 cfi.fileCharacteristics = FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
745 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
746 udf_release_data(fibh.sbh);
747 inode->i_mode = S_IFDIR | mode;
748 if (dir->i_mode & S_ISGID)
749 inode->i_mode |= S_ISGID;
750 mark_inode_dirty(inode);
752 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
755 mark_inode_dirty(inode);
759 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
760 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
761 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
762 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
763 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
764 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
766 mark_inode_dirty(dir);
767 d_instantiate(dentry, inode);
768 if (fibh.sbh != fibh.ebh)
769 udf_release_data(fibh.ebh);
770 udf_release_data(fibh.sbh);
777 static int empty_dir(struct inode *dir)
779 struct fileIdentDesc *fi, cfi;
780 struct udf_fileident_bh fibh;
782 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
784 kernel_lb_addr bloc, eloc;
785 uint32_t extoffset, elen;
787 struct buffer_head *bh = NULL;
789 f_pos = (udf_ext0_offset(dir) >> 2);
791 fibh.soffset = fibh.eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
793 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
794 fibh.sbh = fibh.ebh = NULL;
795 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
796 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == (EXT_RECORDED_ALLOCATED >> 30))
798 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
799 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
801 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
802 extoffset -= sizeof(short_ad);
803 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
804 extoffset -= sizeof(long_ad);
809 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block)))
811 udf_release_data(bh);
817 udf_release_data(bh);
822 while ( (f_pos < size) )
824 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &bloc, &extoffset, &eloc, &elen, &offset, &bh);
828 if (fibh.sbh != fibh.ebh)
829 udf_release_data(fibh.ebh);
830 udf_release_data(fibh.sbh);
831 udf_release_data(bh);
835 if (cfi.lengthFileIdent && (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0)
837 if (fibh.sbh != fibh.ebh)
838 udf_release_data(fibh.ebh);
839 udf_release_data(fibh.sbh);
840 udf_release_data(bh);
844 if (fibh.sbh != fibh.ebh)
845 udf_release_data(fibh.ebh);
846 udf_release_data(fibh.sbh);
847 udf_release_data(bh);
851 static int udf_rmdir(struct inode * dir, struct dentry * dentry)
854 struct inode * inode = dentry->d_inode;
855 struct udf_fileident_bh fibh;
856 struct fileIdentDesc *fi, cfi;
861 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
866 tloc = lelb_to_cpu(cfi.icb.extLocation);
867 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
870 if (!empty_dir(inode))
872 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
875 if (inode->i_nlink != 2)
876 udf_warning(inode->i_sb, "udf_rmdir",
877 "empty directory has nlink != 2 (%d)",
881 inode_dec_link_count(dir);
882 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
883 mark_inode_dirty(dir);
886 if (fibh.sbh != fibh.ebh)
887 udf_release_data(fibh.ebh);
888 udf_release_data(fibh.sbh);
894 static int udf_unlink(struct inode * dir, struct dentry * dentry)
897 struct inode * inode = dentry->d_inode;
898 struct udf_fileident_bh fibh;
899 struct fileIdentDesc *fi;
900 struct fileIdentDesc cfi;
905 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
910 tloc = lelb_to_cpu(cfi.icb.extLocation);
911 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
916 udf_debug("Deleting nonexistent file (%lu), %d\n",
917 inode->i_ino, inode->i_nlink);
920 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
923 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
924 mark_inode_dirty(dir);
925 inode_dec_link_count(inode);
926 inode->i_ctime = dir->i_ctime;
930 if (fibh.sbh != fibh.ebh)
931 udf_release_data(fibh.ebh);
932 udf_release_data(fibh.sbh);
938 static int udf_symlink(struct inode * dir, struct dentry * dentry, const char * symname)
940 struct inode * inode;
941 struct pathComponent *pc;
943 struct udf_fileident_bh fibh;
944 struct buffer_head *bh = NULL;
945 int eoffset, elen = 0;
946 struct fileIdentDesc *fi;
947 struct fileIdentDesc cfi;
951 char name[UDF_NAME_LEN];
955 if (!(inode = udf_new_inode(dir, S_IFLNK, &err)))
958 inode->i_mode = S_IFLNK | S_IRWXUGO;
959 inode->i_data.a_ops = &udf_symlink_aops;
960 inode->i_op = &page_symlink_inode_operations;
962 if (UDF_I_ALLOCTYPE(inode) != ICBTAG_FLAG_AD_IN_ICB)
964 struct buffer_head *bh = NULL;
965 kernel_lb_addr bloc, eloc;
966 uint32_t elen, extoffset;
968 block = udf_new_block(inode->i_sb, inode,
969 UDF_I_LOCATION(inode).partitionReferenceNum,
970 UDF_I_LOCATION(inode).logicalBlockNum, &err);
973 bloc = UDF_I_LOCATION(inode);
974 eloc.logicalBlockNum = block;
975 eloc.partitionReferenceNum = UDF_I_LOCATION(inode).partitionReferenceNum;
976 elen = inode->i_sb->s_blocksize;
977 UDF_I_LENEXTENTS(inode) = elen;
978 extoffset = udf_file_entry_alloc_offset(inode);
979 udf_add_aext(inode, &bloc, &extoffset, eloc, elen, &bh, 0);
980 udf_release_data(bh);
982 block = udf_get_pblock(inode->i_sb, block,
983 UDF_I_LOCATION(inode).partitionReferenceNum, 0);
984 bh = udf_tread(inode->i_sb, block);
986 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
987 set_buffer_uptodate(bh);
989 mark_buffer_dirty_inode(bh, inode);
990 ea = bh->b_data + udf_ext0_offset(inode);
993 ea = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
995 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
996 pc = (struct pathComponent *)ea;
1003 } while (*symname == '/');
1005 pc->componentType = 1;
1006 pc->lengthComponentIdent = 0;
1007 pc->componentFileVersionNum = 0;
1008 pc += sizeof(struct pathComponent);
1009 elen += sizeof(struct pathComponent);
1012 err = -ENAMETOOLONG;
1016 if (elen + sizeof(struct pathComponent) > eoffset)
1019 pc = (struct pathComponent *)(ea + elen);
1021 compstart = (char *)symname;
1026 } while (*symname && *symname != '/');
1028 pc->componentType = 5;
1029 pc->lengthComponentIdent = 0;
1030 pc->componentFileVersionNum = 0;
1031 if (compstart[0] == '.')
1033 if ((symname-compstart) == 1)
1034 pc->componentType = 4;
1035 else if ((symname-compstart) == 2 && compstart[1] == '.')
1036 pc->componentType = 3;
1039 if (pc->componentType == 5)
1041 if ( !(namelen = udf_put_filename(inode->i_sb, compstart, name, symname-compstart)))
1044 if (elen + sizeof(struct pathComponent) + namelen > eoffset)
1047 pc->lengthComponentIdent = namelen;
1049 memcpy(pc->componentIdent, name, namelen);
1052 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1059 } while (*symname == '/');
1063 udf_release_data(bh);
1064 inode->i_size = elen;
1065 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1066 UDF_I_LENALLOC(inode) = inode->i_size;
1067 mark_inode_dirty(inode);
1069 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1071 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1072 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1073 if (UDF_SB_LVIDBH(inode->i_sb))
1075 struct logicalVolHeaderDesc *lvhd;
1077 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1078 uniqueID = le64_to_cpu(lvhd->uniqueID);
1079 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1080 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1081 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1083 lvhd->uniqueID = cpu_to_le64(uniqueID);
1084 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1086 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1087 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
1089 mark_inode_dirty(dir);
1091 if (fibh.sbh != fibh.ebh)
1092 udf_release_data(fibh.ebh);
1093 udf_release_data(fibh.sbh);
1094 d_instantiate(dentry, inode);
1102 inode_dec_link_count(inode);
1107 static int udf_link(struct dentry * old_dentry, struct inode * dir,
1108 struct dentry *dentry)
1110 struct inode *inode = old_dentry->d_inode;
1111 struct udf_fileident_bh fibh;
1112 struct fileIdentDesc cfi, *fi;
1116 if (inode->i_nlink >= (256<<sizeof(inode->i_nlink))-1)
1122 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1127 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1128 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1129 if (UDF_SB_LVIDBH(inode->i_sb))
1131 struct logicalVolHeaderDesc *lvhd;
1133 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1134 uniqueID = le64_to_cpu(lvhd->uniqueID);
1135 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1136 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1137 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1139 lvhd->uniqueID = cpu_to_le64(uniqueID);
1140 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1142 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1143 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
1145 mark_inode_dirty(dir);
1147 if (fibh.sbh != fibh.ebh)
1148 udf_release_data(fibh.ebh);
1149 udf_release_data(fibh.sbh);
1151 inode->i_ctime = current_fs_time(inode->i_sb);
1152 mark_inode_dirty(inode);
1153 atomic_inc(&inode->i_count);
1154 d_instantiate(dentry, inode);
1159 /* Anybody can rename anything with this: the permission checks are left to the
1160 * higher-level routines.
1162 static int udf_rename (struct inode * old_dir, struct dentry * old_dentry,
1163 struct inode * new_dir, struct dentry * new_dentry)
1165 struct inode * old_inode = old_dentry->d_inode;
1166 struct inode * new_inode = new_dentry->d_inode;
1167 struct udf_fileident_bh ofibh, nfibh;
1168 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL, ocfi, ncfi;
1169 struct buffer_head *dir_bh = NULL;
1170 int retval = -ENOENT;
1171 kernel_lb_addr tloc;
1174 if ((ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi)))
1176 if (ofibh.sbh != ofibh.ebh)
1177 udf_release_data(ofibh.ebh);
1178 udf_release_data(ofibh.sbh);
1180 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1181 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
1182 != old_inode->i_ino)
1185 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1190 if (nfibh.sbh != nfibh.ebh)
1191 udf_release_data(nfibh.ebh);
1192 udf_release_data(nfibh.sbh);
1196 if (S_ISDIR(old_inode->i_mode))
1198 uint32_t offset = udf_ext0_offset(old_inode);
1202 retval = -ENOTEMPTY;
1203 if (!empty_dir(new_inode))
1207 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB)
1209 dir_fi = udf_get_fileident(UDF_I_DATA(old_inode) -
1210 (UDF_I_EFE(old_inode) ?
1211 sizeof(struct extendedFileEntry) :
1212 sizeof(struct fileEntry)),
1213 old_inode->i_sb->s_blocksize, &offset);
1217 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1220 dir_fi = udf_get_fileident(dir_bh->b_data, old_inode->i_sb->s_blocksize, &offset);
1224 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1225 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0)
1230 if (!new_inode && new_dir->i_nlink >= (256<<sizeof(new_dir->i_nlink))-1)
1235 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, &retval);
1241 * Like most other Unix systems, set the ctime for inodes on a
1244 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1245 mark_inode_dirty(old_inode);
1250 ncfi.fileVersionNum = ocfi.fileVersionNum;
1251 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1252 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1253 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1255 /* The old fid may have moved - find it again */
1256 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1257 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1261 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1262 inode_dec_link_count(new_inode);
1264 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1265 mark_inode_dirty(old_dir);
1269 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(new_dir));
1270 udf_update_tag((char *)dir_fi, (sizeof(struct fileIdentDesc) +
1271 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1272 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB)
1274 mark_inode_dirty(old_inode);
1277 mark_buffer_dirty_inode(dir_bh, old_inode);
1278 inode_dec_link_count(old_dir);
1281 inode_dec_link_count(new_inode);
1286 mark_inode_dirty(new_dir);
1292 if (ofibh.sbh != ofibh.ebh)
1293 udf_release_data(ofibh.ebh);
1294 udf_release_data(ofibh.sbh);
1300 udf_release_data(dir_bh);
1303 if (nfibh.sbh != nfibh.ebh)
1304 udf_release_data(nfibh.ebh);
1305 udf_release_data(nfibh.sbh);
1311 const struct inode_operations udf_dir_inode_operations = {
1312 .lookup = udf_lookup,
1313 .create = udf_create,
1315 .unlink = udf_unlink,
1316 .symlink = udf_symlink,
1320 .rename = udf_rename,