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,
41 return !memcmp(name1, name2, len1);
44 int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
45 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
46 uint8_t *impuse, uint8_t *fileident)
48 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(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
60 offset = fibh->soffset + sizeof(struct fileIdentDesc);
63 if (adinicb || (offset + liu < 0)) {
64 memcpy((uint8_t *)sfi->impUse, impuse, liu);
65 } else if (offset >= 0) {
66 memcpy(fibh->ebh->b_data + offset, impuse, liu);
68 memcpy((uint8_t *)sfi->impUse, impuse, -offset);
69 memcpy(fibh->ebh->b_data, impuse - offset,
77 if (adinicb || (offset + lfi < 0)) {
78 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
79 } else if (offset >= 0) {
80 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
82 memcpy((uint8_t *)sfi->fileIdent + liu, fileident,
84 memcpy(fibh->ebh->b_data, fileident - offset,
91 if (adinicb || (offset + padlen < 0)) {
92 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
93 } else if (offset >= 0) {
94 memset(fibh->ebh->b_data + offset, 0x00, padlen);
96 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
97 memset(fibh->ebh->b_data, 0x00, padlen + offset);
100 crc = udf_crc((uint8_t *)cfi + sizeof(tag),
101 sizeof(struct fileIdentDesc) - sizeof(tag), 0);
103 if (fibh->sbh == fibh->ebh) {
104 crc = udf_crc((uint8_t *)sfi->impUse,
105 crclen + sizeof(tag) -
106 sizeof(struct fileIdentDesc), crc);
107 } else if (sizeof(struct fileIdentDesc) >= -fibh->soffset) {
108 crc = udf_crc(fibh->ebh->b_data +
109 sizeof(struct fileIdentDesc) +
111 crclen + sizeof(tag) -
112 sizeof(struct fileIdentDesc),
115 crc = udf_crc((uint8_t *)sfi->impUse,
116 -fibh->soffset - sizeof(struct fileIdentDesc),
118 crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
121 cfi->descTag.descCRC = cpu_to_le16(crc);
122 cfi->descTag.descCRCLength = cpu_to_le16(crclen);
123 cfi->descTag.tagChecksum = udf_tag_checksum(&cfi->descTag);
125 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset)) {
126 memcpy((uint8_t *)sfi, (uint8_t *)cfi,
127 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);
137 if (fibh->sbh != fibh->ebh)
138 mark_buffer_dirty_inode(fibh->ebh, inode);
139 mark_buffer_dirty_inode(fibh->sbh, inode);
144 static struct fileIdentDesc *udf_find_entry(struct inode *dir,
145 struct dentry *dentry,
146 struct udf_fileident_bh *fibh,
147 struct fileIdentDesc *cfi)
149 struct fileIdentDesc *fi = NULL;
152 char fname[UDF_NAME_LEN];
160 struct extent_position epos = {};
161 struct udf_inode_info *dinfo = UDF_I(dir);
163 size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
164 f_pos = (udf_ext0_offset(dir) >> 2);
166 fibh->soffset = fibh->eoffset =
167 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
168 if (dinfo->i_alloc_type == 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) ==
172 (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) {
175 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
176 epos.offset -= sizeof(short_ad);
177 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
178 epos.offset -= sizeof(long_ad);
182 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
192 while ((f_pos < size)) {
193 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
196 if (fibh->sbh != fibh->ebh)
203 liu = le16_to_cpu(cfi->lengthOfImpUse);
204 lfi = cfi->lengthFileIdent;
206 if (fibh->sbh == fibh->ebh) {
207 nameptr = fi->fileIdent + liu;
209 int poffset; /* Unpaded ending offset */
211 poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
215 nameptr = (uint8_t *)(fibh->ebh->b_data +
219 memcpy(nameptr, fi->fileIdent + liu,
221 memcpy(nameptr + lfi - poffset,
222 fibh->ebh->b_data, poffset);
226 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
227 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
231 if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
232 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
239 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
240 if (flen && udf_match(flen, fname, dentry->d_name.len,
241 dentry->d_name.name)) {
247 if (fibh->sbh != fibh->ebh)
259 * Look-up the inode for a given name.
262 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
263 * available for a directory. The filesystem is useless if this routine is
264 * not available for at least the filesystem's root directory.
266 * This routine is passed an incomplete dentry - it must be completed by
267 * calling d_add(dentry, inode). If the name does not exist, then the
268 * specified inode must be set to null. An error should only be returned
269 * when the lookup fails for a reason other than the name not existing.
270 * Note that the directory inode semaphore is held during the call.
272 * Refer to lookup_dentry() in fs/namei.c
273 * lookup_dentry() -> lookup() -> real_lookup() -> .
276 * dir Pointer to inode of parent directory.
277 * dentry Pointer to dentry to complete.
278 * nd Pointer to lookup nameidata
281 * <return> Zero on success.
284 * July 1, 1997 - Andrew E. Mileski
285 * Written, tested, and released.
288 static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
289 struct nameidata *nd)
291 struct inode *inode = NULL;
292 struct fileIdentDesc cfi;
293 struct udf_fileident_bh fibh;
295 if (dentry->d_name.len > UDF_NAME_LEN - 2)
296 return ERR_PTR(-ENAMETOOLONG);
300 /* temporary shorthand for specifying files by inode number */
301 if (!strncmp(dentry->d_name.name, ".B=", 3)) {
302 kernel_lb_addr lb = {
303 .logicalBlockNum = 0,
304 .partitionReferenceNum =
305 simple_strtoul(dentry->d_name.name + 3,
308 inode = udf_iget(dir->i_sb, lb);
311 return ERR_PTR(-EACCES);
314 #endif /* UDF_RECOVERY */
316 if (udf_find_entry(dir, dentry, &fibh, &cfi)) {
317 if (fibh.sbh != fibh.ebh)
321 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
324 return ERR_PTR(-EACCES);
328 d_add(dentry, inode);
333 static struct fileIdentDesc *udf_add_entry(struct inode *dir,
334 struct dentry *dentry,
335 struct udf_fileident_bh *fibh,
336 struct fileIdentDesc *cfi, int *err)
338 struct super_block *sb = dir->i_sb;
339 struct fileIdentDesc *fi = NULL;
340 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
345 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
353 struct extent_position epos = {};
354 struct udf_inode_info *dinfo;
357 if (!dentry->d_name.len) {
361 namelen = udf_put_filename(sb, dentry->d_name.name, name,
364 *err = -ENAMETOOLONG;
371 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
373 f_pos = (udf_ext0_offset(dir) >> 2);
375 fibh->soffset = fibh->eoffset =
376 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
378 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
379 fibh->sbh = fibh->ebh = NULL;
380 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
381 &epos, &eloc, &elen, &offset) ==
382 (EXT_RECORDED_ALLOCATED >> 30)) {
383 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
384 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
385 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
386 epos.offset -= sizeof(short_ad);
387 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
388 epos.offset -= sizeof(long_ad);
392 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
399 block = dinfo->i_location.logicalBlockNum;
402 block = udf_get_lb_pblock(dir->i_sb, dinfo->i_location, 0);
403 fibh->sbh = fibh->ebh = NULL;
404 fibh->soffset = fibh->eoffset = sb->s_blocksize;
408 while ((f_pos < size)) {
409 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
413 if (fibh->sbh != fibh->ebh)
421 liu = le16_to_cpu(cfi->lengthOfImpUse);
422 lfi = cfi->lengthFileIdent;
424 if (fibh->sbh == fibh->ebh)
425 nameptr = fi->fileIdent + liu;
427 int poffset; /* Unpaded ending offset */
429 poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
433 nameptr = (char *)(fibh->ebh->b_data +
437 memcpy(nameptr, fi->fileIdent + liu,
439 memcpy(nameptr + lfi - poffset,
440 fibh->ebh->b_data, poffset);
444 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
445 if (((sizeof(struct fileIdentDesc) +
446 liu + lfi + 3) & ~3) == nfidlen) {
448 cfi->descTag.tagSerialNum = cpu_to_le16(1);
449 cfi->fileVersionNum = cpu_to_le16(1);
450 cfi->fileCharacteristics = 0;
451 cfi->lengthFileIdent = namelen;
452 cfi->lengthOfImpUse = cpu_to_le16(0);
453 if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
466 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
467 if (flen && udf_match(flen, fname, dentry->d_name.len,
468 dentry->d_name.name)) {
469 if (fibh->sbh != fibh->ebh)
481 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
482 sb->s_blocksize - fibh->eoffset < nfidlen) {
485 fibh->soffset -= udf_ext0_offset(dir);
486 fibh->eoffset -= udf_ext0_offset(dir);
487 f_pos -= (udf_ext0_offset(dir) >> 2);
488 if (fibh->sbh != fibh->ebh)
491 fibh->sbh = fibh->ebh =
492 udf_expand_dir_adinicb(dir, &block, err);
495 epos.block = dinfo->i_location;
496 eloc.logicalBlockNum = block;
497 eloc.partitionReferenceNum =
498 dinfo->i_location.partitionReferenceNum;
499 elen = dir->i_sb->s_blocksize;
500 epos.offset = udf_file_entry_alloc_offset(dir);
501 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
502 epos.offset += sizeof(short_ad);
503 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
504 epos.offset += sizeof(long_ad);
507 if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
508 fibh->soffset = fibh->eoffset;
509 fibh->eoffset += nfidlen;
510 if (fibh->sbh != fibh->ebh) {
512 fibh->sbh = fibh->ebh;
515 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
516 block = dinfo->i_location.logicalBlockNum;
517 fi = (struct fileIdentDesc *)
518 (dinfo->i_ext.i_data +
520 udf_ext0_offset(dir) +
523 block = eloc.logicalBlockNum +
525 dir->i_sb->s_blocksize_bits);
526 fi = (struct fileIdentDesc *)
527 (fibh->sbh->b_data + fibh->soffset);
530 fibh->soffset = fibh->eoffset - sb->s_blocksize;
531 fibh->eoffset += nfidlen - sb->s_blocksize;
532 if (fibh->sbh != fibh->ebh) {
534 fibh->sbh = fibh->ebh;
537 block = eloc.logicalBlockNum + ((elen - 1) >>
538 dir->i_sb->s_blocksize_bits);
539 fibh->ebh = udf_bread(dir,
540 f_pos >> (dir->i_sb->s_blocksize_bits - 2),
548 if (!fibh->soffset) {
549 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
550 (EXT_RECORDED_ALLOCATED >> 30)) {
551 block = eloc.logicalBlockNum + ((elen - 1) >>
552 dir->i_sb->s_blocksize_bits);
557 fibh->sbh = fibh->ebh;
558 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
560 fi = (struct fileIdentDesc *)
561 (fibh->sbh->b_data + sb->s_blocksize +
566 memset(cfi, 0, sizeof(struct fileIdentDesc));
567 if (UDF_SB(sb)->s_udfrev >= 0x0200)
568 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
571 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
573 cfi->fileVersionNum = cpu_to_le16(1);
574 cfi->lengthFileIdent = namelen;
575 cfi->lengthOfImpUse = cpu_to_le16(0);
576 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
578 dir->i_size += nfidlen;
579 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
580 dinfo->i_lenAlloc += nfidlen;
581 mark_inode_dirty(dir);
585 if (fibh->sbh != fibh->ebh)
593 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
594 struct udf_fileident_bh *fibh,
595 struct fileIdentDesc *cfi)
597 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
599 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
600 memset(&(cfi->icb), 0x00, sizeof(long_ad));
602 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
605 static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
606 struct nameidata *nd)
608 struct udf_fileident_bh fibh;
610 struct fileIdentDesc cfi, *fi;
612 struct udf_inode_info *iinfo;
615 inode = udf_new_inode(dir, mode, &err);
621 iinfo = UDF_I(inode);
622 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
623 inode->i_data.a_ops = &udf_adinicb_aops;
625 inode->i_data.a_ops = &udf_aops;
626 inode->i_op = &udf_file_inode_operations;
627 inode->i_fop = &udf_file_operations;
628 inode->i_mode = mode;
629 mark_inode_dirty(inode);
631 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
634 mark_inode_dirty(inode);
639 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
640 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
641 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
642 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
643 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
644 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
645 mark_inode_dirty(dir);
646 if (fibh.sbh != fibh.ebh)
650 d_instantiate(dentry, inode);
655 static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
659 struct udf_fileident_bh fibh;
660 struct fileIdentDesc cfi, *fi;
662 struct udf_inode_info *iinfo;
664 if (!old_valid_dev(rdev))
669 inode = udf_new_inode(dir, mode, &err);
673 iinfo = UDF_I(inode);
674 inode->i_uid = current->fsuid;
675 init_special_inode(inode, mode, rdev);
676 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
679 mark_inode_dirty(inode);
684 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
685 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
686 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
687 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
688 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
689 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
690 mark_inode_dirty(dir);
691 mark_inode_dirty(inode);
693 if (fibh.sbh != fibh.ebh)
696 d_instantiate(dentry, inode);
704 static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
707 struct udf_fileident_bh fibh;
708 struct fileIdentDesc cfi, *fi;
710 struct udf_inode_info *dinfo = UDF_I(dir);
711 struct udf_inode_info *iinfo;
715 if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
719 inode = udf_new_inode(dir, S_IFDIR, &err);
723 iinfo = UDF_I(inode);
724 inode->i_op = &udf_dir_inode_operations;
725 inode->i_fop = &udf_dir_operations;
726 fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
729 mark_inode_dirty(inode);
734 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
735 cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
736 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
737 cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
738 cfi.fileCharacteristics =
739 FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
740 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
742 inode->i_mode = S_IFDIR | mode;
743 if (dir->i_mode & S_ISGID)
744 inode->i_mode |= S_ISGID;
745 mark_inode_dirty(inode);
747 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
750 mark_inode_dirty(inode);
754 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
755 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
756 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
757 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
758 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
759 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
761 mark_inode_dirty(dir);
762 d_instantiate(dentry, inode);
763 if (fibh.sbh != fibh.ebh)
773 static int empty_dir(struct inode *dir)
775 struct fileIdentDesc *fi, cfi;
776 struct udf_fileident_bh fibh;
778 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
783 struct extent_position epos = {};
784 struct udf_inode_info *dinfo = UDF_I(dir);
786 f_pos = (udf_ext0_offset(dir) >> 2);
788 fibh.soffset = fibh.eoffset =
789 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
791 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
792 fibh.sbh = fibh.ebh = NULL;
793 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
794 &epos, &eloc, &elen, &offset) ==
795 (EXT_RECORDED_ALLOCATED >> 30)) {
796 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
797 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
798 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
799 epos.offset -= sizeof(short_ad);
800 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
801 epos.offset -= sizeof(long_ad);
805 fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
815 while ((f_pos < size)) {
816 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
819 if (fibh.sbh != fibh.ebh)
826 if (cfi.lengthFileIdent &&
827 (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
828 if (fibh.sbh != fibh.ebh)
836 if (fibh.sbh != fibh.ebh)
844 static int udf_rmdir(struct inode *dir, struct dentry *dentry)
847 struct inode *inode = dentry->d_inode;
848 struct udf_fileident_bh fibh;
849 struct fileIdentDesc *fi, cfi;
854 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
859 tloc = lelb_to_cpu(cfi.icb.extLocation);
860 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
863 if (!empty_dir(inode))
865 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
868 if (inode->i_nlink != 2)
869 udf_warning(inode->i_sb, "udf_rmdir",
870 "empty directory has nlink != 2 (%d)",
874 inode_dec_link_count(dir);
875 inode->i_ctime = dir->i_ctime = dir->i_mtime =
876 current_fs_time(dir->i_sb);
877 mark_inode_dirty(dir);
880 if (fibh.sbh != fibh.ebh)
889 static int udf_unlink(struct inode *dir, struct dentry *dentry)
892 struct inode *inode = dentry->d_inode;
893 struct udf_fileident_bh fibh;
894 struct fileIdentDesc *fi;
895 struct fileIdentDesc cfi;
900 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
905 tloc = lelb_to_cpu(cfi.icb.extLocation);
906 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
909 if (!inode->i_nlink) {
910 udf_debug("Deleting nonexistent file (%lu), %d\n",
911 inode->i_ino, inode->i_nlink);
914 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
917 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
918 mark_inode_dirty(dir);
919 inode_dec_link_count(inode);
920 inode->i_ctime = dir->i_ctime;
924 if (fibh.sbh != fibh.ebh)
933 static int udf_symlink(struct inode *dir, struct dentry *dentry,
937 struct pathComponent *pc;
939 struct udf_fileident_bh fibh;
940 struct extent_position epos = {};
941 int eoffset, elen = 0;
942 struct fileIdentDesc *fi;
943 struct fileIdentDesc cfi;
947 char name[UDF_NAME_LEN];
949 struct buffer_head *bh;
950 struct udf_inode_info *iinfo;
953 inode = udf_new_inode(dir, S_IFLNK, &err);
957 iinfo = UDF_I(inode);
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 (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
966 block = udf_new_block(inode->i_sb, inode,
967 iinfo->i_location.partitionReferenceNum,
968 iinfo->i_location.logicalBlockNum, &err);
971 epos.block = iinfo->i_location;
972 epos.offset = udf_file_entry_alloc_offset(inode);
974 eloc.logicalBlockNum = block;
975 eloc.partitionReferenceNum =
976 iinfo->i_location.partitionReferenceNum;
977 elen = inode->i_sb->s_blocksize;
978 iinfo->i_lenExtents = elen;
979 udf_add_aext(inode, &epos, eloc, elen, 0);
982 block = udf_get_pblock(inode->i_sb, block,
983 iinfo->i_location.partitionReferenceNum,
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);
993 ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
995 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
996 pc = (struct pathComponent *)ea;
998 if (*symname == '/') {
1001 } while (*symname == '/');
1003 pc->componentType = 1;
1004 pc->lengthComponentIdent = 0;
1005 pc->componentFileVersionNum = 0;
1006 pc += sizeof(struct pathComponent);
1007 elen += sizeof(struct pathComponent);
1010 err = -ENAMETOOLONG;
1013 if (elen + sizeof(struct pathComponent) > eoffset)
1016 pc = (struct pathComponent *)(ea + elen);
1018 compstart = (char *)symname;
1022 } while (*symname && *symname != '/');
1024 pc->componentType = 5;
1025 pc->lengthComponentIdent = 0;
1026 pc->componentFileVersionNum = 0;
1027 if (compstart[0] == '.') {
1028 if ((symname - compstart) == 1)
1029 pc->componentType = 4;
1030 else if ((symname - compstart) == 2 &&
1031 compstart[1] == '.')
1032 pc->componentType = 3;
1035 if (pc->componentType == 5) {
1036 namelen = udf_put_filename(inode->i_sb, compstart, name,
1037 symname - compstart);
1041 if (elen + sizeof(struct pathComponent) + namelen >
1045 pc->lengthComponentIdent = namelen;
1047 memcpy(pc->componentIdent, name, namelen);
1050 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1055 } while (*symname == '/');
1060 inode->i_size = elen;
1061 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1062 iinfo->i_lenAlloc = inode->i_size;
1063 mark_inode_dirty(inode);
1065 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1068 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1069 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
1070 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1072 struct logicalVolIntegrityDesc *lvid =
1073 (struct logicalVolIntegrityDesc *)bh->b_data;
1074 struct logicalVolHeaderDesc *lvhd;
1076 lvhd = (struct logicalVolHeaderDesc *)
1077 lvid->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(bh);
1086 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1087 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1088 mark_inode_dirty(dir);
1089 if (fibh.sbh != fibh.ebh)
1092 d_instantiate(dentry, inode);
1100 inode_dec_link_count(inode);
1105 static int udf_link(struct dentry *old_dentry, struct inode *dir,
1106 struct dentry *dentry)
1108 struct inode *inode = old_dentry->d_inode;
1109 struct udf_fileident_bh fibh;
1110 struct fileIdentDesc cfi, *fi;
1112 struct buffer_head *bh;
1115 if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
1120 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1125 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1126 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1127 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1129 struct logicalVolIntegrityDesc *lvid =
1130 (struct logicalVolIntegrityDesc *)bh->b_data;
1131 struct logicalVolHeaderDesc *lvhd;
1133 lvhd = (struct logicalVolHeaderDesc *)
1134 (lvid->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(bh);
1143 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1144 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1145 mark_inode_dirty(dir);
1147 if (fibh.sbh != fibh.ebh)
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);
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;
1170 struct fileIdentDesc ocfi, ncfi;
1171 struct buffer_head *dir_bh = NULL;
1172 int retval = -ENOENT;
1173 kernel_lb_addr tloc;
1174 struct udf_inode_info *old_iinfo = UDF_I(old_inode);
1177 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1179 if (ofibh.sbh != ofibh.ebh)
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);
1191 if (nfibh.sbh != nfibh.ebh)
1197 if (S_ISDIR(old_inode->i_mode)) {
1198 int offset = udf_ext0_offset(old_inode);
1201 retval = -ENOTEMPTY;
1202 if (!empty_dir(new_inode))
1206 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1207 dir_fi = udf_get_fileident(
1208 old_iinfo->i_ext.i_data -
1210 sizeof(struct extendedFileEntry) :
1211 sizeof(struct fileEntry)),
1212 old_inode->i_sb->s_blocksize, &offset);
1214 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1217 dir_fi = udf_get_fileident(dir_bh->b_data,
1218 old_inode->i_sb->s_blocksize, &offset);
1222 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1223 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) !=
1230 (256 << sizeof(new_dir->i_nlink)) - 1)
1234 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
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);
1260 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1261 inode_dec_link_count(new_inode);
1263 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1264 mark_inode_dirty(old_dir);
1267 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
1268 udf_update_tag((char *)dir_fi,
1269 (sizeof(struct fileIdentDesc) +
1270 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1271 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1272 mark_inode_dirty(old_inode);
1274 mark_buffer_dirty_inode(dir_bh, old_inode);
1276 inode_dec_link_count(old_dir);
1278 inode_dec_link_count(new_inode);
1281 mark_inode_dirty(new_dir);
1286 if (ofibh.sbh != ofibh.ebh)
1296 if (nfibh.sbh != nfibh.ebh)
1305 const struct inode_operations udf_dir_inode_operations = {
1306 .lookup = udf_lookup,
1307 .create = udf_create,
1309 .unlink = udf_unlink,
1310 .symlink = udf_symlink,
1314 .rename = udf_rename,