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>
33 #include <linux/sched.h>
35 static inline int udf_match(int len1, const char *name1, int len2, const char *name2)
39 return !memcmp(name1, name2, len1);
42 int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
43 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
44 uint8_t *impuse, uint8_t *fileident)
46 uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(tag);
51 uint16_t liu = le16_to_cpu(cfi->lengthOfImpUse);
52 uint8_t lfi = cfi->lengthFileIdent;
53 int padlen = fibh->eoffset - fibh->soffset - liu - lfi -
54 sizeof(struct fileIdentDesc);
57 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
60 offset = fibh->soffset + sizeof(struct fileIdentDesc);
64 if (adinicb || (offset + liu < 0))
65 memcpy((uint8_t *)sfi->impUse, impuse, liu);
67 memcpy(fibh->ebh->b_data + offset, impuse, liu);
70 memcpy((uint8_t *)sfi->impUse, impuse, -offset);
71 memcpy(fibh->ebh->b_data, impuse - offset, liu + offset);
79 if (adinicb || (offset + lfi < 0))
80 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
82 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
85 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, -offset);
86 memcpy(fibh->ebh->b_data, fileident - offset, lfi + offset);
92 if (adinicb || (offset + padlen < 0))
93 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
95 memset(fibh->ebh->b_data + offset, 0x00, padlen);
98 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
99 memset(fibh->ebh->b_data, 0x00, padlen + offset);
102 crc = udf_crc((uint8_t *)cfi + sizeof(tag), sizeof(struct fileIdentDesc) -
105 if (fibh->sbh == fibh->ebh)
106 crc = udf_crc((uint8_t *)sfi->impUse,
107 crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
108 else if (sizeof(struct fileIdentDesc) >= -fibh->soffset)
109 crc = udf_crc(fibh->ebh->b_data + sizeof(struct fileIdentDesc) + fibh->soffset,
110 crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
113 crc = udf_crc((uint8_t *)sfi->impUse,
114 -fibh->soffset - sizeof(struct fileIdentDesc), crc);
115 crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
118 cfi->descTag.descCRC = cpu_to_le16(crc);
119 cfi->descTag.descCRCLength = cpu_to_le16(crclen);
123 checksum += ((uint8_t *)&cfi->descTag)[i];
125 cfi->descTag.tagChecksum = checksum;
126 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset))
127 memcpy((uint8_t *)sfi, (uint8_t *)cfi, sizeof(struct fileIdentDesc));
130 memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset);
131 memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset,
132 sizeof(struct fileIdentDesc) + fibh->soffset);
136 mark_inode_dirty(inode);
139 if (fibh->sbh != fibh->ebh)
140 mark_buffer_dirty_inode(fibh->ebh, inode);
141 mark_buffer_dirty_inode(fibh->sbh, inode);
146 static struct fileIdentDesc *
147 udf_find_entry(struct inode *dir, struct dentry *dentry,
148 struct udf_fileident_bh *fibh,
149 struct fileIdentDesc *cfi)
151 struct fileIdentDesc *fi=NULL;
154 char fname[UDF_NAME_LEN];
162 struct extent_position epos = { NULL, 0, { 0, 0}};
164 size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
165 f_pos = (udf_ext0_offset(dir) >> 2);
167 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
168 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
169 fibh->sbh = fibh->ebh = NULL;
170 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
171 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30))
173 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
174 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
176 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
177 epos.offset -= sizeof(short_ad);
178 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
179 epos.offset -= sizeof(long_ad);
184 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))
196 while ( (f_pos < size) )
198 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc, &elen, &offset);
202 if (fibh->sbh != fibh->ebh)
209 liu = le16_to_cpu(cfi->lengthOfImpUse);
210 lfi = cfi->lengthFileIdent;
212 if (fibh->sbh == fibh->ebh)
214 nameptr = fi->fileIdent + liu;
218 int poffset; /* Unpaded ending offset */
220 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
223 nameptr = (uint8_t *)(fibh->ebh->b_data + poffset - lfi);
227 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
228 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
232 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0 )
234 if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE) )
238 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0 )
240 if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE) )
247 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)))
249 if (udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name))
256 if (fibh->sbh != fibh->ebh)
267 * Look-up the inode for a given name.
270 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
271 * available for a directory. The filesystem is useless if this routine is
272 * not available for at least the filesystem's root directory.
274 * This routine is passed an incomplete dentry - it must be completed by
275 * calling d_add(dentry, inode). If the name does not exist, then the
276 * specified inode must be set to null. An error should only be returned
277 * when the lookup fails for a reason other than the name not existing.
278 * Note that the directory inode semaphore is held during the call.
280 * Refer to lookup_dentry() in fs/namei.c
281 * lookup_dentry() -> lookup() -> real_lookup() -> .
284 * dir Pointer to inode of parent directory.
285 * dentry Pointer to dentry to complete.
286 * nd Pointer to lookup nameidata
289 * <return> Zero on success.
292 * July 1, 1997 - Andrew E. Mileski
293 * Written, tested, and released.
296 static struct dentry *
297 udf_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
299 struct inode *inode = NULL;
300 struct fileIdentDesc cfi;
301 struct udf_fileident_bh fibh;
303 if (dentry->d_name.len > UDF_NAME_LEN-2)
304 return ERR_PTR(-ENAMETOOLONG);
308 /* temporary shorthand for specifying files by inode number */
309 if (!strncmp(dentry->d_name.name, ".B=", 3) )
311 kernel_lb_addr lb = { 0, simple_strtoul(dentry->d_name.name+3, NULL, 0) };
312 inode = udf_iget(dir->i_sb, lb);
316 return ERR_PTR(-EACCES);
320 #endif /* UDF_RECOVERY */
322 if (udf_find_entry(dir, dentry, &fibh, &cfi))
324 if (fibh.sbh != fibh.ebh)
328 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
332 return ERR_PTR(-EACCES);
336 d_add(dentry, inode);
340 static struct fileIdentDesc *
341 udf_add_entry(struct inode *dir, struct dentry *dentry,
342 struct udf_fileident_bh *fibh,
343 struct fileIdentDesc *cfi, int *err)
345 struct super_block *sb;
346 struct fileIdentDesc *fi=NULL;
347 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
352 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
360 struct extent_position epos = { NULL, 0, { 0, 0 }};
366 if (!dentry->d_name.len)
372 if ( !(namelen = udf_put_filename(sb, dentry->d_name.name, name, dentry->d_name.len)))
374 *err = -ENAMETOOLONG;
381 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
383 f_pos = (udf_ext0_offset(dir) >> 2);
385 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
386 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
387 fibh->sbh = fibh->ebh = NULL;
388 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
389 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30))
391 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
392 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
394 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
395 epos.offset -= sizeof(short_ad);
396 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
397 epos.offset -= sizeof(long_ad);
402 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))
409 block = UDF_I_LOCATION(dir).logicalBlockNum;
414 block = udf_get_lb_pblock(dir->i_sb, UDF_I_LOCATION(dir), 0);
415 fibh->sbh = fibh->ebh = NULL;
416 fibh->soffset = fibh->eoffset = sb->s_blocksize;
420 while ( (f_pos < size) )
422 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc, &elen, &offset);
426 if (fibh->sbh != fibh->ebh)
434 liu = le16_to_cpu(cfi->lengthOfImpUse);
435 lfi = cfi->lengthFileIdent;
437 if (fibh->sbh == fibh->ebh)
438 nameptr = fi->fileIdent + liu;
441 int poffset; /* Unpaded ending offset */
443 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
446 nameptr = (char *)(fibh->ebh->b_data + poffset - lfi);
450 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
451 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
455 if ( (cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0 )
457 if (((sizeof(struct fileIdentDesc) + liu + lfi + 3) & ~3) == nfidlen)
460 cfi->descTag.tagSerialNum = cpu_to_le16(1);
461 cfi->fileVersionNum = cpu_to_le16(1);
462 cfi->fileCharacteristics = 0;
463 cfi->lengthFileIdent = namelen;
464 cfi->lengthOfImpUse = cpu_to_le16(0);
465 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name))
478 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)) &&
479 udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name))
481 if (fibh->sbh != fibh->ebh)
493 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB &&
494 sb->s_blocksize - fibh->eoffset < nfidlen)
498 fibh->soffset -= udf_ext0_offset(dir);
499 fibh->eoffset -= udf_ext0_offset(dir);
500 f_pos -= (udf_ext0_offset(dir) >> 2);
501 if (fibh->sbh != fibh->ebh)
504 if (!(fibh->sbh = fibh->ebh = udf_expand_dir_adinicb(dir, &block, err)))
506 epos.block = UDF_I_LOCATION(dir);
507 eloc.logicalBlockNum = block;
508 eloc.partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
509 elen = dir->i_sb->s_blocksize;
510 epos.offset = udf_file_entry_alloc_offset(dir);
511 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
512 epos.offset += sizeof(short_ad);
513 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
514 epos.offset += sizeof(long_ad);
517 if (sb->s_blocksize - fibh->eoffset >= nfidlen)
519 fibh->soffset = fibh->eoffset;
520 fibh->eoffset += nfidlen;
521 if (fibh->sbh != fibh->ebh)
524 fibh->sbh = fibh->ebh;
527 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
529 block = UDF_I_LOCATION(dir).logicalBlockNum;
530 fi = (struct fileIdentDesc *)(UDF_I_DATA(dir) + fibh->soffset - udf_ext0_offset(dir) + UDF_I_LENEATTR(dir));
534 block = eloc.logicalBlockNum + ((elen - 1) >>
535 dir->i_sb->s_blocksize_bits);
536 fi = (struct fileIdentDesc *)(fibh->sbh->b_data + fibh->soffset);
541 fibh->soffset = fibh->eoffset - sb->s_blocksize;
542 fibh->eoffset += nfidlen - sb->s_blocksize;
543 if (fibh->sbh != fibh->ebh)
546 fibh->sbh = fibh->ebh;
549 block = eloc.logicalBlockNum + ((elen - 1) >>
550 dir->i_sb->s_blocksize_bits);
552 if (!(fibh->ebh = udf_bread(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2), 1, err)))
559 if (!(fibh->soffset))
561 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
562 (EXT_RECORDED_ALLOCATED >> 30))
564 block = eloc.logicalBlockNum + ((elen - 1) >>
565 dir->i_sb->s_blocksize_bits);
571 fibh->sbh = fibh->ebh;
572 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
576 fi = (struct fileIdentDesc *)
577 (fibh->sbh->b_data + sb->s_blocksize + fibh->soffset);
581 memset(cfi, 0, sizeof(struct fileIdentDesc));
582 if (UDF_SB_UDFREV(sb) >= 0x0200)
583 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block, sizeof(tag));
585 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block, sizeof(tag));
586 cfi->fileVersionNum = cpu_to_le16(1);
587 cfi->lengthFileIdent = namelen;
588 cfi->lengthOfImpUse = cpu_to_le16(0);
589 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name))
592 dir->i_size += nfidlen;
593 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
594 UDF_I_LENALLOC(dir) += nfidlen;
595 mark_inode_dirty(dir);
601 if (fibh->sbh != fibh->ebh)
609 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
610 struct udf_fileident_bh *fibh, struct fileIdentDesc *cfi)
612 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
613 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
614 memset(&(cfi->icb), 0x00, sizeof(long_ad));
615 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
618 static int udf_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
620 struct udf_fileident_bh fibh;
622 struct fileIdentDesc cfi, *fi;
626 inode = udf_new_inode(dir, mode, &err);
633 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
634 inode->i_data.a_ops = &udf_adinicb_aops;
636 inode->i_data.a_ops = &udf_aops;
637 inode->i_op = &udf_file_inode_operations;
638 inode->i_fop = &udf_file_operations;
639 inode->i_mode = mode;
640 mark_inode_dirty(inode);
642 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
645 mark_inode_dirty(inode);
650 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
651 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
652 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
653 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
654 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
655 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
657 mark_inode_dirty(dir);
659 if (fibh.sbh != fibh.ebh)
663 d_instantiate(dentry, inode);
667 static int udf_mknod(struct inode * dir, struct dentry * dentry, int mode, dev_t rdev)
669 struct inode * inode;
670 struct udf_fileident_bh fibh;
671 struct fileIdentDesc cfi, *fi;
674 if (!old_valid_dev(rdev))
679 inode = udf_new_inode(dir, mode, &err);
683 inode->i_uid = current->fsuid;
684 init_special_inode(inode, mode, rdev);
685 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
688 mark_inode_dirty(inode);
693 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
694 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
695 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
696 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
697 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
698 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
700 mark_inode_dirty(dir);
702 mark_inode_dirty(inode);
704 if (fibh.sbh != fibh.ebh)
707 d_instantiate(dentry, inode);
714 static int udf_mkdir(struct inode * dir, struct dentry * dentry, int mode)
716 struct inode * inode;
717 struct udf_fileident_bh fibh;
718 struct fileIdentDesc cfi, *fi;
723 if (dir->i_nlink >= (256<<sizeof(dir->i_nlink))-1)
727 inode = udf_new_inode(dir, S_IFDIR, &err);
731 inode->i_op = &udf_dir_inode_operations;
732 inode->i_fop = &udf_dir_operations;
733 if (!(fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err)))
736 mark_inode_dirty(inode);
741 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
742 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(dir));
743 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
744 cpu_to_le32(UDF_I_UNIQUE(dir) & 0x00000000FFFFFFFFUL);
745 cfi.fileCharacteristics = FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
746 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
748 inode->i_mode = S_IFDIR | mode;
749 if (dir->i_mode & S_ISGID)
750 inode->i_mode |= S_ISGID;
751 mark_inode_dirty(inode);
753 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
756 mark_inode_dirty(inode);
760 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
761 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
762 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
763 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
764 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
765 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
767 mark_inode_dirty(dir);
768 d_instantiate(dentry, inode);
769 if (fibh.sbh != fibh.ebh)
778 static int empty_dir(struct inode *dir)
780 struct fileIdentDesc *fi, cfi;
781 struct udf_fileident_bh fibh;
783 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
788 struct extent_position epos = { NULL, 0, { 0, 0}};
790 f_pos = (udf_ext0_offset(dir) >> 2);
792 fibh.soffset = fibh.eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
794 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
795 fibh.sbh = fibh.ebh = NULL;
796 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
797 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30))
799 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
800 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
802 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
803 epos.offset -= sizeof(short_ad);
804 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
805 epos.offset -= sizeof(long_ad);
810 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block)))
823 while ( (f_pos < size) )
825 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc, &elen, &offset);
829 if (fibh.sbh != fibh.ebh)
836 if (cfi.lengthFileIdent && (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0)
838 if (fibh.sbh != fibh.ebh)
845 if (fibh.sbh != fibh.ebh)
852 static int udf_rmdir(struct inode * dir, struct dentry * dentry)
855 struct inode * inode = dentry->d_inode;
856 struct udf_fileident_bh fibh;
857 struct fileIdentDesc *fi, cfi;
862 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
867 tloc = lelb_to_cpu(cfi.icb.extLocation);
868 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
871 if (!empty_dir(inode))
873 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
876 if (inode->i_nlink != 2)
877 udf_warning(inode->i_sb, "udf_rmdir",
878 "empty directory has nlink != 2 (%d)",
882 inode_dec_link_count(dir);
883 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
884 mark_inode_dirty(dir);
887 if (fibh.sbh != fibh.ebh)
895 static int udf_unlink(struct inode * dir, struct dentry * dentry)
898 struct inode * inode = dentry->d_inode;
899 struct udf_fileident_bh fibh;
900 struct fileIdentDesc *fi;
901 struct fileIdentDesc cfi;
906 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
911 tloc = lelb_to_cpu(cfi.icb.extLocation);
912 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
917 udf_debug("Deleting nonexistent file (%lu), %d\n",
918 inode->i_ino, inode->i_nlink);
921 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
924 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
925 mark_inode_dirty(dir);
926 inode_dec_link_count(inode);
927 inode->i_ctime = dir->i_ctime;
931 if (fibh.sbh != fibh.ebh)
939 static int udf_symlink(struct inode * dir, struct dentry * dentry, const char * symname)
941 struct inode * inode;
942 struct pathComponent *pc;
944 struct udf_fileident_bh fibh;
945 struct extent_position epos = { NULL, 0, {0, 0}};
946 int eoffset, elen = 0;
947 struct fileIdentDesc *fi;
948 struct fileIdentDesc cfi;
952 char name[UDF_NAME_LEN];
956 if (!(inode = udf_new_inode(dir, S_IFLNK, &err)))
959 inode->i_mode = S_IFLNK | S_IRWXUGO;
960 inode->i_data.a_ops = &udf_symlink_aops;
961 inode->i_op = &page_symlink_inode_operations;
963 if (UDF_I_ALLOCTYPE(inode) != ICBTAG_FLAG_AD_IN_ICB)
968 block = udf_new_block(inode->i_sb, inode,
969 UDF_I_LOCATION(inode).partitionReferenceNum,
970 UDF_I_LOCATION(inode).logicalBlockNum, &err);
973 epos.block = UDF_I_LOCATION(inode);
974 epos.offset = udf_file_entry_alloc_offset(inode);
976 eloc.logicalBlockNum = block;
977 eloc.partitionReferenceNum = UDF_I_LOCATION(inode).partitionReferenceNum;
978 elen = inode->i_sb->s_blocksize;
979 UDF_I_LENEXTENTS(inode) = elen;
980 udf_add_aext(inode, &epos, eloc, elen, 0);
983 block = udf_get_pblock(inode->i_sb, block,
984 UDF_I_LOCATION(inode).partitionReferenceNum, 0);
985 epos.bh = udf_tread(inode->i_sb, block);
986 lock_buffer(epos.bh);
987 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
988 set_buffer_uptodate(epos.bh);
989 unlock_buffer(epos.bh);
990 mark_buffer_dirty_inode(epos.bh, inode);
991 ea = epos.bh->b_data + udf_ext0_offset(inode);
994 ea = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
996 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
997 pc = (struct pathComponent *)ea;
1004 } while (*symname == '/');
1006 pc->componentType = 1;
1007 pc->lengthComponentIdent = 0;
1008 pc->componentFileVersionNum = 0;
1009 pc += sizeof(struct pathComponent);
1010 elen += sizeof(struct pathComponent);
1013 err = -ENAMETOOLONG;
1017 if (elen + sizeof(struct pathComponent) > eoffset)
1020 pc = (struct pathComponent *)(ea + elen);
1022 compstart = (char *)symname;
1027 } while (*symname && *symname != '/');
1029 pc->componentType = 5;
1030 pc->lengthComponentIdent = 0;
1031 pc->componentFileVersionNum = 0;
1032 if (compstart[0] == '.')
1034 if ((symname-compstart) == 1)
1035 pc->componentType = 4;
1036 else if ((symname-compstart) == 2 && compstart[1] == '.')
1037 pc->componentType = 3;
1040 if (pc->componentType == 5)
1042 if ( !(namelen = udf_put_filename(inode->i_sb, compstart, name, symname-compstart)))
1045 if (elen + sizeof(struct pathComponent) + namelen > eoffset)
1048 pc->lengthComponentIdent = namelen;
1050 memcpy(pc->componentIdent, name, namelen);
1053 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1060 } while (*symname == '/');
1065 inode->i_size = elen;
1066 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1067 UDF_I_LENALLOC(inode) = inode->i_size;
1068 mark_inode_dirty(inode);
1070 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1072 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1073 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1074 if (UDF_SB_LVIDBH(inode->i_sb))
1076 struct logicalVolHeaderDesc *lvhd;
1078 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1079 uniqueID = le64_to_cpu(lvhd->uniqueID);
1080 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1081 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1082 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1084 lvhd->uniqueID = cpu_to_le64(uniqueID);
1085 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1087 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1088 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
1090 mark_inode_dirty(dir);
1092 if (fibh.sbh != fibh.ebh)
1095 d_instantiate(dentry, inode);
1103 inode_dec_link_count(inode);
1108 static int udf_link(struct dentry * old_dentry, struct inode * dir,
1109 struct dentry *dentry)
1111 struct inode *inode = old_dentry->d_inode;
1112 struct udf_fileident_bh fibh;
1113 struct fileIdentDesc cfi, *fi;
1117 if (inode->i_nlink >= (256<<sizeof(inode->i_nlink))-1)
1123 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1128 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1129 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1130 if (UDF_SB_LVIDBH(inode->i_sb))
1132 struct logicalVolHeaderDesc *lvhd;
1134 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1135 uniqueID = le64_to_cpu(lvhd->uniqueID);
1136 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1137 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1138 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1140 lvhd->uniqueID = cpu_to_le64(uniqueID);
1141 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1143 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1144 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
1146 mark_inode_dirty(dir);
1148 if (fibh.sbh != fibh.ebh)
1152 inode->i_ctime = current_fs_time(inode->i_sb);
1153 mark_inode_dirty(inode);
1154 atomic_inc(&inode->i_count);
1155 d_instantiate(dentry, inode);
1160 /* Anybody can rename anything with this: the permission checks are left to the
1161 * higher-level routines.
1163 static int udf_rename (struct inode * old_dir, struct dentry * old_dentry,
1164 struct inode * new_dir, struct dentry * new_dentry)
1166 struct inode * old_inode = old_dentry->d_inode;
1167 struct inode * new_inode = new_dentry->d_inode;
1168 struct udf_fileident_bh ofibh, nfibh;
1169 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL, ocfi, ncfi;
1170 struct buffer_head *dir_bh = NULL;
1171 int retval = -ENOENT;
1172 kernel_lb_addr tloc;
1175 if ((ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi)))
1177 if (ofibh.sbh != ofibh.ebh)
1181 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1182 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
1183 != old_inode->i_ino)
1186 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1191 if (nfibh.sbh != nfibh.ebh)
1197 if (S_ISDIR(old_inode->i_mode))
1199 uint32_t offset = udf_ext0_offset(old_inode);
1203 retval = -ENOTEMPTY;
1204 if (!empty_dir(new_inode))
1208 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB)
1210 dir_fi = udf_get_fileident(UDF_I_DATA(old_inode) -
1211 (UDF_I_EFE(old_inode) ?
1212 sizeof(struct extendedFileEntry) :
1213 sizeof(struct fileEntry)),
1214 old_inode->i_sb->s_blocksize, &offset);
1218 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1221 dir_fi = udf_get_fileident(dir_bh->b_data, old_inode->i_sb->s_blocksize, &offset);
1225 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1226 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0)
1231 if (!new_inode && new_dir->i_nlink >= (256<<sizeof(new_dir->i_nlink))-1)
1236 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, &retval);
1242 * Like most other Unix systems, set the ctime for inodes on a
1245 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1246 mark_inode_dirty(old_inode);
1251 ncfi.fileVersionNum = ocfi.fileVersionNum;
1252 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1253 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1254 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1256 /* The old fid may have moved - find it again */
1257 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1258 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1262 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1263 inode_dec_link_count(new_inode);
1265 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1266 mark_inode_dirty(old_dir);
1270 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(new_dir));
1271 udf_update_tag((char *)dir_fi, (sizeof(struct fileIdentDesc) +
1272 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1273 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB)
1275 mark_inode_dirty(old_inode);
1278 mark_buffer_dirty_inode(dir_bh, old_inode);
1279 inode_dec_link_count(old_dir);
1282 inode_dec_link_count(new_inode);
1287 mark_inode_dirty(new_dir);
1293 if (ofibh.sbh != ofibh.ebh)
1304 if (nfibh.sbh != nfibh.ebh)
1312 const struct inode_operations udf_dir_inode_operations = {
1313 .lookup = udf_lookup,
1314 .create = udf_create,
1316 .unlink = udf_unlink,
1317 .symlink = udf_symlink,
1321 .rename = udf_rename,