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 = {};
162 size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
163 f_pos = (udf_ext0_offset(dir) >> 2);
165 fibh->soffset = fibh->eoffset =
166 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
167 if (UDF_I(dir)->i_alloc_type == 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 &epos, &eloc, &elen, &offset) ==
171 (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) {
174 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
175 epos.offset -= sizeof(short_ad);
176 else if (UDF_I(dir)->i_alloc_type ==
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 = {};
356 if (!dentry->d_name.len) {
360 namelen = udf_put_filename(sb, dentry->d_name.name, name,
363 *err = -ENAMETOOLONG;
370 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
372 f_pos = (udf_ext0_offset(dir) >> 2);
374 fibh->soffset = fibh->eoffset =
375 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
376 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
377 fibh->sbh = fibh->ebh = NULL;
378 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
379 &epos, &eloc, &elen, &offset) ==
380 (EXT_RECORDED_ALLOCATED >> 30)) {
381 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
382 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
383 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
384 epos.offset -= sizeof(short_ad);
385 else if (UDF_I(dir)->i_alloc_type ==
387 epos.offset -= sizeof(long_ad);
391 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
398 block = UDF_I(dir)->i_location.logicalBlockNum;
401 block = udf_get_lb_pblock(dir->i_sb, UDF_I(dir)->i_location, 0);
402 fibh->sbh = fibh->ebh = NULL;
403 fibh->soffset = fibh->eoffset = sb->s_blocksize;
407 while ((f_pos < size)) {
408 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
412 if (fibh->sbh != fibh->ebh)
420 liu = le16_to_cpu(cfi->lengthOfImpUse);
421 lfi = cfi->lengthFileIdent;
423 if (fibh->sbh == fibh->ebh)
424 nameptr = fi->fileIdent + liu;
426 int poffset; /* Unpaded ending offset */
428 poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
432 nameptr = (char *)(fibh->ebh->b_data +
436 memcpy(nameptr, fi->fileIdent + liu,
438 memcpy(nameptr + lfi - poffset,
439 fibh->ebh->b_data, poffset);
443 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
444 if (((sizeof(struct fileIdentDesc) +
445 liu + lfi + 3) & ~3) == nfidlen) {
447 cfi->descTag.tagSerialNum = cpu_to_le16(1);
448 cfi->fileVersionNum = cpu_to_le16(1);
449 cfi->fileCharacteristics = 0;
450 cfi->lengthFileIdent = namelen;
451 cfi->lengthOfImpUse = cpu_to_le16(0);
452 if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
465 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
466 if (flen && udf_match(flen, fname, dentry->d_name.len,
467 dentry->d_name.name)) {
468 if (fibh->sbh != fibh->ebh)
480 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
481 sb->s_blocksize - fibh->eoffset < nfidlen) {
484 fibh->soffset -= udf_ext0_offset(dir);
485 fibh->eoffset -= udf_ext0_offset(dir);
486 f_pos -= (udf_ext0_offset(dir) >> 2);
487 if (fibh->sbh != fibh->ebh)
490 fibh->sbh = fibh->ebh =
491 udf_expand_dir_adinicb(dir, &block, err);
494 epos.block = UDF_I(dir)->i_location;
495 eloc.logicalBlockNum = block;
496 eloc.partitionReferenceNum =
497 UDF_I(dir)->i_location.partitionReferenceNum;
498 elen = dir->i_sb->s_blocksize;
499 epos.offset = udf_file_entry_alloc_offset(dir);
500 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
501 epos.offset += sizeof(short_ad);
502 else if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
503 epos.offset += sizeof(long_ad);
506 if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
507 fibh->soffset = fibh->eoffset;
508 fibh->eoffset += nfidlen;
509 if (fibh->sbh != fibh->ebh) {
511 fibh->sbh = fibh->ebh;
514 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
515 block = UDF_I(dir)->i_location.logicalBlockNum;
516 fi = (struct fileIdentDesc *)
517 (UDF_I(dir)->i_ext.i_data +
519 udf_ext0_offset(dir) +
520 UDF_I(dir)->i_lenEAttr);
522 block = eloc.logicalBlockNum +
524 dir->i_sb->s_blocksize_bits);
525 fi = (struct fileIdentDesc *)
526 (fibh->sbh->b_data + fibh->soffset);
529 fibh->soffset = fibh->eoffset - sb->s_blocksize;
530 fibh->eoffset += nfidlen - sb->s_blocksize;
531 if (fibh->sbh != fibh->ebh) {
533 fibh->sbh = fibh->ebh;
536 block = eloc.logicalBlockNum + ((elen - 1) >>
537 dir->i_sb->s_blocksize_bits);
538 fibh->ebh = udf_bread(dir,
539 f_pos >> (dir->i_sb->s_blocksize_bits - 2),
547 if (!fibh->soffset) {
548 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
549 (EXT_RECORDED_ALLOCATED >> 30)) {
550 block = eloc.logicalBlockNum + ((elen - 1) >>
551 dir->i_sb->s_blocksize_bits);
556 fibh->sbh = fibh->ebh;
557 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
559 fi = (struct fileIdentDesc *)
560 (fibh->sbh->b_data + sb->s_blocksize +
565 memset(cfi, 0, sizeof(struct fileIdentDesc));
566 if (UDF_SB(sb)->s_udfrev >= 0x0200)
567 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
570 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
572 cfi->fileVersionNum = cpu_to_le16(1);
573 cfi->lengthFileIdent = namelen;
574 cfi->lengthOfImpUse = cpu_to_le16(0);
575 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
577 dir->i_size += nfidlen;
578 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
579 UDF_I(dir)->i_lenAlloc += nfidlen;
580 mark_inode_dirty(dir);
584 if (fibh->sbh != fibh->ebh)
592 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
593 struct udf_fileident_bh *fibh,
594 struct fileIdentDesc *cfi)
596 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
598 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
599 memset(&(cfi->icb), 0x00, sizeof(long_ad));
601 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
604 static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
605 struct nameidata *nd)
607 struct udf_fileident_bh fibh;
609 struct fileIdentDesc cfi, *fi;
613 inode = udf_new_inode(dir, mode, &err);
619 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
620 inode->i_data.a_ops = &udf_adinicb_aops;
622 inode->i_data.a_ops = &udf_aops;
623 inode->i_op = &udf_file_inode_operations;
624 inode->i_fop = &udf_file_operations;
625 inode->i_mode = mode;
626 mark_inode_dirty(inode);
628 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
631 mark_inode_dirty(inode);
636 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
637 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
638 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
639 cpu_to_le32(UDF_I(inode)->i_unique & 0x00000000FFFFFFFFUL);
640 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
641 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
642 mark_inode_dirty(dir);
643 if (fibh.sbh != fibh.ebh)
647 d_instantiate(dentry, inode);
652 static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
656 struct udf_fileident_bh fibh;
657 struct fileIdentDesc cfi, *fi;
660 if (!old_valid_dev(rdev))
665 inode = udf_new_inode(dir, mode, &err);
669 inode->i_uid = current->fsuid;
670 init_special_inode(inode, mode, rdev);
671 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
674 mark_inode_dirty(inode);
679 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
680 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
681 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
682 cpu_to_le32(UDF_I(inode)->i_unique & 0x00000000FFFFFFFFUL);
683 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
684 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
685 mark_inode_dirty(dir);
686 mark_inode_dirty(inode);
688 if (fibh.sbh != fibh.ebh)
691 d_instantiate(dentry, inode);
699 static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
702 struct udf_fileident_bh fibh;
703 struct fileIdentDesc cfi, *fi;
708 if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
712 inode = udf_new_inode(dir, S_IFDIR, &err);
716 inode->i_op = &udf_dir_inode_operations;
717 inode->i_fop = &udf_dir_operations;
718 fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
721 mark_inode_dirty(inode);
726 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
727 cfi.icb.extLocation = cpu_to_lelb(UDF_I(dir)->i_location);
728 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
729 cpu_to_le32(UDF_I(dir)->i_unique & 0x00000000FFFFFFFFUL);
730 cfi.fileCharacteristics =
731 FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
732 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
734 inode->i_mode = S_IFDIR | mode;
735 if (dir->i_mode & S_ISGID)
736 inode->i_mode |= S_ISGID;
737 mark_inode_dirty(inode);
739 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
742 mark_inode_dirty(inode);
746 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
747 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
748 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
749 cpu_to_le32(UDF_I(inode)->i_unique & 0x00000000FFFFFFFFUL);
750 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
751 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
753 mark_inode_dirty(dir);
754 d_instantiate(dentry, inode);
755 if (fibh.sbh != fibh.ebh)
765 static int empty_dir(struct inode *dir)
767 struct fileIdentDesc *fi, cfi;
768 struct udf_fileident_bh fibh;
770 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
775 struct extent_position epos = {};
777 f_pos = (udf_ext0_offset(dir) >> 2);
779 fibh.soffset = fibh.eoffset =
780 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
782 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
783 fibh.sbh = fibh.ebh = NULL;
784 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
785 &epos, &eloc, &elen, &offset) ==
786 (EXT_RECORDED_ALLOCATED >> 30)) {
787 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
788 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
789 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
790 epos.offset -= sizeof(short_ad);
791 else if (UDF_I(dir)->i_alloc_type ==
793 epos.offset -= sizeof(long_ad);
797 fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
807 while ((f_pos < size)) {
808 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
811 if (fibh.sbh != fibh.ebh)
818 if (cfi.lengthFileIdent &&
819 (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
820 if (fibh.sbh != fibh.ebh)
828 if (fibh.sbh != fibh.ebh)
836 static int udf_rmdir(struct inode *dir, struct dentry *dentry)
839 struct inode *inode = dentry->d_inode;
840 struct udf_fileident_bh fibh;
841 struct fileIdentDesc *fi, cfi;
846 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
851 tloc = lelb_to_cpu(cfi.icb.extLocation);
852 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
855 if (!empty_dir(inode))
857 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
860 if (inode->i_nlink != 2)
861 udf_warning(inode->i_sb, "udf_rmdir",
862 "empty directory has nlink != 2 (%d)",
866 inode_dec_link_count(dir);
867 inode->i_ctime = dir->i_ctime = dir->i_mtime =
868 current_fs_time(dir->i_sb);
869 mark_inode_dirty(dir);
872 if (fibh.sbh != fibh.ebh)
881 static int udf_unlink(struct inode *dir, struct dentry *dentry)
884 struct inode *inode = dentry->d_inode;
885 struct udf_fileident_bh fibh;
886 struct fileIdentDesc *fi;
887 struct fileIdentDesc cfi;
892 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
897 tloc = lelb_to_cpu(cfi.icb.extLocation);
898 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
901 if (!inode->i_nlink) {
902 udf_debug("Deleting nonexistent file (%lu), %d\n",
903 inode->i_ino, inode->i_nlink);
906 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
909 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
910 mark_inode_dirty(dir);
911 inode_dec_link_count(inode);
912 inode->i_ctime = dir->i_ctime;
916 if (fibh.sbh != fibh.ebh)
925 static int udf_symlink(struct inode *dir, struct dentry *dentry,
929 struct pathComponent *pc;
931 struct udf_fileident_bh fibh;
932 struct extent_position epos = {};
933 int eoffset, elen = 0;
934 struct fileIdentDesc *fi;
935 struct fileIdentDesc cfi;
939 char name[UDF_NAME_LEN];
941 struct buffer_head *bh;
944 inode = udf_new_inode(dir, S_IFLNK, &err);
948 inode->i_mode = S_IFLNK | S_IRWXUGO;
949 inode->i_data.a_ops = &udf_symlink_aops;
950 inode->i_op = &page_symlink_inode_operations;
952 if (UDF_I(inode)->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
956 block = udf_new_block(inode->i_sb, inode,
957 UDF_I(inode)->i_location.partitionReferenceNum,
958 UDF_I(inode)->i_location.logicalBlockNum, &err);
961 epos.block = UDF_I(inode)->i_location;
962 epos.offset = udf_file_entry_alloc_offset(inode);
964 eloc.logicalBlockNum = block;
965 eloc.partitionReferenceNum =
966 UDF_I(inode)->i_location.partitionReferenceNum;
967 elen = inode->i_sb->s_blocksize;
968 UDF_I(inode)->i_lenExtents = elen;
969 udf_add_aext(inode, &epos, eloc, elen, 0);
972 block = udf_get_pblock(inode->i_sb, block,
973 UDF_I(inode)->i_location.partitionReferenceNum,
975 epos.bh = udf_tread(inode->i_sb, block);
976 lock_buffer(epos.bh);
977 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
978 set_buffer_uptodate(epos.bh);
979 unlock_buffer(epos.bh);
980 mark_buffer_dirty_inode(epos.bh, inode);
981 ea = epos.bh->b_data + udf_ext0_offset(inode);
983 ea = UDF_I(inode)->i_ext.i_data + UDF_I(inode)->i_lenEAttr;
986 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
987 pc = (struct pathComponent *)ea;
989 if (*symname == '/') {
992 } while (*symname == '/');
994 pc->componentType = 1;
995 pc->lengthComponentIdent = 0;
996 pc->componentFileVersionNum = 0;
997 pc += sizeof(struct pathComponent);
998 elen += sizeof(struct pathComponent);
1001 err = -ENAMETOOLONG;
1004 if (elen + sizeof(struct pathComponent) > eoffset)
1007 pc = (struct pathComponent *)(ea + elen);
1009 compstart = (char *)symname;
1013 } while (*symname && *symname != '/');
1015 pc->componentType = 5;
1016 pc->lengthComponentIdent = 0;
1017 pc->componentFileVersionNum = 0;
1018 if (compstart[0] == '.') {
1019 if ((symname - compstart) == 1)
1020 pc->componentType = 4;
1021 else if ((symname - compstart) == 2 &&
1022 compstart[1] == '.')
1023 pc->componentType = 3;
1026 if (pc->componentType == 5) {
1027 namelen = udf_put_filename(inode->i_sb, compstart, name,
1028 symname - compstart);
1032 if (elen + sizeof(struct pathComponent) + namelen >
1036 pc->lengthComponentIdent = namelen;
1038 memcpy(pc->componentIdent, name, namelen);
1041 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1046 } while (*symname == '/');
1051 inode->i_size = elen;
1052 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1053 UDF_I(inode)->i_lenAlloc = inode->i_size;
1054 mark_inode_dirty(inode);
1056 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1059 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1060 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1061 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1063 struct logicalVolIntegrityDesc *lvid =
1064 (struct logicalVolIntegrityDesc *)bh->b_data;
1065 struct logicalVolHeaderDesc *lvhd;
1067 lvhd = (struct logicalVolHeaderDesc *)
1068 lvid->logicalVolContentsUse;
1069 uniqueID = le64_to_cpu(lvhd->uniqueID);
1070 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1071 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1072 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1074 lvhd->uniqueID = cpu_to_le64(uniqueID);
1075 mark_buffer_dirty(bh);
1077 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1078 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1079 mark_inode_dirty(dir);
1080 if (fibh.sbh != fibh.ebh)
1083 d_instantiate(dentry, inode);
1091 inode_dec_link_count(inode);
1096 static int udf_link(struct dentry *old_dentry, struct inode *dir,
1097 struct dentry *dentry)
1099 struct inode *inode = old_dentry->d_inode;
1100 struct udf_fileident_bh fibh;
1101 struct fileIdentDesc cfi, *fi;
1103 struct buffer_head *bh;
1106 if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
1111 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1116 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1117 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1118 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1120 struct logicalVolIntegrityDesc *lvid =
1121 (struct logicalVolIntegrityDesc *)bh->b_data;
1122 struct logicalVolHeaderDesc *lvhd;
1124 lvhd = (struct logicalVolHeaderDesc *)
1125 (lvid->logicalVolContentsUse);
1126 uniqueID = le64_to_cpu(lvhd->uniqueID);
1127 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1128 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1129 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1131 lvhd->uniqueID = cpu_to_le64(uniqueID);
1132 mark_buffer_dirty(bh);
1134 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1135 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1136 mark_inode_dirty(dir);
1138 if (fibh.sbh != fibh.ebh)
1142 inode->i_ctime = current_fs_time(inode->i_sb);
1143 mark_inode_dirty(inode);
1144 atomic_inc(&inode->i_count);
1145 d_instantiate(dentry, inode);
1151 /* Anybody can rename anything with this: the permission checks are left to the
1152 * higher-level routines.
1154 static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1155 struct inode *new_dir, struct dentry *new_dentry)
1157 struct inode *old_inode = old_dentry->d_inode;
1158 struct inode *new_inode = new_dentry->d_inode;
1159 struct udf_fileident_bh ofibh, nfibh;
1160 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
1161 struct fileIdentDesc ocfi, ncfi;
1162 struct buffer_head *dir_bh = NULL;
1163 int retval = -ENOENT;
1164 kernel_lb_addr tloc;
1167 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1169 if (ofibh.sbh != ofibh.ebh)
1173 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1174 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
1175 != old_inode->i_ino)
1178 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1181 if (nfibh.sbh != nfibh.ebh)
1187 if (S_ISDIR(old_inode->i_mode)) {
1188 uint32_t offset = udf_ext0_offset(old_inode);
1191 retval = -ENOTEMPTY;
1192 if (!empty_dir(new_inode))
1196 if (UDF_I(old_inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1197 dir_fi = udf_get_fileident(
1198 UDF_I(old_inode)->i_ext.i_data -
1199 (UDF_I(old_inode)->i_efe ?
1200 sizeof(struct extendedFileEntry) :
1201 sizeof(struct fileEntry)),
1202 old_inode->i_sb->s_blocksize, &offset);
1204 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1207 dir_fi = udf_get_fileident(dir_bh->b_data,
1208 old_inode->i_sb->s_blocksize, &offset);
1212 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1213 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) !=
1220 (256 << sizeof(new_dir->i_nlink)) - 1)
1224 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
1231 * Like most other Unix systems, set the ctime for inodes on a
1234 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1235 mark_inode_dirty(old_inode);
1240 ncfi.fileVersionNum = ocfi.fileVersionNum;
1241 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1242 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1243 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1245 /* The old fid may have moved - find it again */
1246 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1247 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1250 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1251 inode_dec_link_count(new_inode);
1253 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1254 mark_inode_dirty(old_dir);
1257 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
1258 udf_update_tag((char *)dir_fi,
1259 (sizeof(struct fileIdentDesc) +
1260 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1261 if (UDF_I(old_inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1262 mark_inode_dirty(old_inode);
1264 mark_buffer_dirty_inode(dir_bh, old_inode);
1266 inode_dec_link_count(old_dir);
1268 inode_dec_link_count(new_inode);
1271 mark_inode_dirty(new_dir);
1276 if (ofibh.sbh != ofibh.ebh)
1286 if (nfibh.sbh != nfibh.ebh)
1295 const struct inode_operations udf_dir_inode_operations = {
1296 .lookup = udf_lookup,
1297 .create = udf_create,
1299 .unlink = udf_unlink,
1300 .symlink = udf_symlink,
1304 .rename = udf_rename,