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, offset;
160 struct buffer_head *bh = NULL;
162 size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
163 f_pos = (udf_ext0_offset(dir) >> 2);
165 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
166 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
167 fibh->sbh = fibh->ebh = NULL;
168 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
169 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == (EXT_RECORDED_ALLOCATED >> 30))
171 offset >>= dir->i_sb->s_blocksize_bits;
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, *fi;
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 ((fi = 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, offset;
358 struct buffer_head *bh = NULL;
364 if (!dentry->d_name.len)
370 if ( !(namelen = udf_put_filename(sb, dentry->d_name.name, name, dentry->d_name.len)))
372 *err = -ENAMETOOLONG;
379 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
381 f_pos = (udf_ext0_offset(dir) >> 2);
383 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
384 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
385 fibh->sbh = fibh->ebh = NULL;
386 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
387 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == (EXT_RECORDED_ALLOCATED >> 30))
389 offset >>= dir->i_sb->s_blocksize_bits;
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, offset;
786 struct buffer_head *bh = NULL;
788 f_pos = (udf_ext0_offset(dir) >> 2);
790 fibh.soffset = fibh.eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
792 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
793 fibh.sbh = fibh.ebh = NULL;
794 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
795 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == (EXT_RECORDED_ALLOCATED >> 30))
797 offset >>= dir->i_sb->s_blocksize_bits;
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 mark_inode_dirty(inode);
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)
888 udf_release_data(fibh.ebh);
889 udf_release_data(fibh.sbh);
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);
927 mark_inode_dirty(inode);
928 inode->i_ctime = dir->i_ctime;
932 if (fibh.sbh != fibh.ebh)
933 udf_release_data(fibh.ebh);
934 udf_release_data(fibh.sbh);
940 static int udf_symlink(struct inode * dir, struct dentry * dentry, const char * symname)
942 struct inode * inode;
943 struct pathComponent *pc;
945 struct udf_fileident_bh fibh;
946 struct buffer_head *bh = NULL;
947 int eoffset, elen = 0;
948 struct fileIdentDesc *fi;
949 struct fileIdentDesc cfi;
953 char name[UDF_NAME_LEN];
957 if (!(inode = udf_new_inode(dir, S_IFLNK, &err)))
960 inode->i_mode = S_IFLNK | S_IRWXUGO;
961 inode->i_data.a_ops = &udf_symlink_aops;
962 inode->i_op = &page_symlink_inode_operations;
964 if (UDF_I_ALLOCTYPE(inode) != ICBTAG_FLAG_AD_IN_ICB)
966 struct buffer_head *bh = NULL;
967 kernel_lb_addr bloc, eloc;
968 uint32_t elen, extoffset;
970 block = udf_new_block(inode->i_sb, inode,
971 UDF_I_LOCATION(inode).partitionReferenceNum,
972 UDF_I_LOCATION(inode).logicalBlockNum, &err);
975 bloc = UDF_I_LOCATION(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 extoffset = udf_file_entry_alloc_offset(inode);
981 udf_add_aext(inode, &bloc, &extoffset, eloc, elen, &bh, 0);
982 udf_release_data(bh);
984 block = udf_get_pblock(inode->i_sb, block,
985 UDF_I_LOCATION(inode).partitionReferenceNum, 0);
986 bh = udf_tread(inode->i_sb, block);
988 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
989 set_buffer_uptodate(bh);
991 mark_buffer_dirty_inode(bh, inode);
992 ea = bh->b_data + udf_ext0_offset(inode);
995 ea = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
997 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
998 pc = (struct pathComponent *)ea;
1000 if (*symname == '/')
1005 } while (*symname == '/');
1007 pc->componentType = 1;
1008 pc->lengthComponentIdent = 0;
1009 pc->componentFileVersionNum = 0;
1010 pc += sizeof(struct pathComponent);
1011 elen += sizeof(struct pathComponent);
1014 err = -ENAMETOOLONG;
1018 if (elen + sizeof(struct pathComponent) > eoffset)
1021 pc = (struct pathComponent *)(ea + elen);
1023 compstart = (char *)symname;
1028 } while (*symname && *symname != '/');
1030 pc->componentType = 5;
1031 pc->lengthComponentIdent = 0;
1032 pc->componentFileVersionNum = 0;
1033 if (compstart[0] == '.')
1035 if ((symname-compstart) == 1)
1036 pc->componentType = 4;
1037 else if ((symname-compstart) == 2 && compstart[1] == '.')
1038 pc->componentType = 3;
1041 if (pc->componentType == 5)
1043 if ( !(namelen = udf_put_filename(inode->i_sb, compstart, name, symname-compstart)))
1046 if (elen + sizeof(struct pathComponent) + namelen > eoffset)
1049 pc->lengthComponentIdent = namelen;
1051 memcpy(pc->componentIdent, name, namelen);
1054 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1061 } while (*symname == '/');
1065 udf_release_data(bh);
1066 inode->i_size = elen;
1067 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1068 UDF_I_LENALLOC(inode) = inode->i_size;
1069 mark_inode_dirty(inode);
1071 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1073 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1074 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1075 if (UDF_SB_LVIDBH(inode->i_sb))
1077 struct logicalVolHeaderDesc *lvhd;
1079 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1080 uniqueID = le64_to_cpu(lvhd->uniqueID);
1081 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1082 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1083 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1085 lvhd->uniqueID = cpu_to_le64(uniqueID);
1086 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1088 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1089 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
1091 mark_inode_dirty(dir);
1093 if (fibh.sbh != fibh.ebh)
1094 udf_release_data(fibh.ebh);
1095 udf_release_data(fibh.sbh);
1096 d_instantiate(dentry, inode);
1105 mark_inode_dirty(inode);
1110 static int udf_link(struct dentry * old_dentry, struct inode * dir,
1111 struct dentry *dentry)
1113 struct inode *inode = old_dentry->d_inode;
1114 struct udf_fileident_bh fibh;
1115 struct fileIdentDesc cfi, *fi;
1119 if (inode->i_nlink >= (256<<sizeof(inode->i_nlink))-1)
1125 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1130 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1131 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1132 if (UDF_SB_LVIDBH(inode->i_sb))
1134 struct logicalVolHeaderDesc *lvhd;
1136 lvhd = (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->logicalVolContentsUse);
1137 uniqueID = le64_to_cpu(lvhd->uniqueID);
1138 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1139 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1140 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1142 lvhd->uniqueID = cpu_to_le64(uniqueID);
1143 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1145 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1146 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
1148 mark_inode_dirty(dir);
1150 if (fibh.sbh != fibh.ebh)
1151 udf_release_data(fibh.ebh);
1152 udf_release_data(fibh.sbh);
1154 inode->i_ctime = current_fs_time(inode->i_sb);
1155 mark_inode_dirty(inode);
1156 atomic_inc(&inode->i_count);
1157 d_instantiate(dentry, inode);
1162 /* Anybody can rename anything with this: the permission checks are left to the
1163 * higher-level routines.
1165 static int udf_rename (struct inode * old_dir, struct dentry * old_dentry,
1166 struct inode * new_dir, struct dentry * new_dentry)
1168 struct inode * old_inode = old_dentry->d_inode;
1169 struct inode * new_inode = new_dentry->d_inode;
1170 struct udf_fileident_bh ofibh, nfibh;
1171 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL, ocfi, ncfi;
1172 struct buffer_head *dir_bh = NULL;
1173 int retval = -ENOENT;
1174 kernel_lb_addr tloc;
1177 if ((ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi)))
1179 if (ofibh.sbh != ofibh.ebh)
1180 udf_release_data(ofibh.ebh);
1181 udf_release_data(ofibh.sbh);
1183 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1184 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
1185 != old_inode->i_ino)
1188 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1193 if (nfibh.sbh != nfibh.ebh)
1194 udf_release_data(nfibh.ebh);
1195 udf_release_data(nfibh.sbh);
1199 if (S_ISDIR(old_inode->i_mode))
1201 uint32_t offset = udf_ext0_offset(old_inode);
1205 retval = -ENOTEMPTY;
1206 if (!empty_dir(new_inode))
1210 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB)
1212 dir_fi = udf_get_fileident(UDF_I_DATA(old_inode) -
1213 (UDF_I_EFE(old_inode) ?
1214 sizeof(struct extendedFileEntry) :
1215 sizeof(struct fileEntry)),
1216 old_inode->i_sb->s_blocksize, &offset);
1220 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1223 dir_fi = udf_get_fileident(dir_bh->b_data, old_inode->i_sb->s_blocksize, &offset);
1227 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1228 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0)
1233 if (!new_inode && new_dir->i_nlink >= (256<<sizeof(new_dir->i_nlink))-1)
1238 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, &retval);
1244 * Like most other Unix systems, set the ctime for inodes on a
1247 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1248 mark_inode_dirty(old_inode);
1253 ncfi.fileVersionNum = ocfi.fileVersionNum;
1254 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1255 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1256 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1258 /* The old fid may have moved - find it again */
1259 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1260 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1264 new_inode->i_nlink--;
1265 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1266 mark_inode_dirty(new_inode);
1268 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1269 mark_inode_dirty(old_dir);
1273 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(new_dir));
1274 udf_update_tag((char *)dir_fi, (sizeof(struct fileIdentDesc) +
1275 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1276 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB)
1278 mark_inode_dirty(old_inode);
1281 mark_buffer_dirty_inode(dir_bh, old_inode);
1282 old_dir->i_nlink --;
1283 mark_inode_dirty(old_dir);
1286 new_inode->i_nlink --;
1287 mark_inode_dirty(new_inode);
1291 new_dir->i_nlink ++;
1292 mark_inode_dirty(new_dir);
1298 if (ofibh.sbh != ofibh.ebh)
1299 udf_release_data(ofibh.ebh);
1300 udf_release_data(ofibh.sbh);
1306 udf_release_data(dir_bh);
1309 if (nfibh.sbh != nfibh.ebh)
1310 udf_release_data(nfibh.ebh);
1311 udf_release_data(nfibh.sbh);
1317 struct inode_operations udf_dir_inode_operations = {
1318 .lookup = udf_lookup,
1319 .create = udf_create,
1321 .unlink = udf_unlink,
1322 .symlink = udf_symlink,
1326 .rename = udf_rename,