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;
164 f_pos = udf_ext0_offset(dir);
166 fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
167 if (dinfo->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,
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 (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
175 epos.offset -= sizeof(short_ad);
176 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
177 epos.offset -= sizeof(long_ad);
181 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
191 while (f_pos < size) {
192 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
195 if (fibh->sbh != fibh->ebh)
202 liu = le16_to_cpu(cfi->lengthOfImpUse);
203 lfi = cfi->lengthFileIdent;
205 if (fibh->sbh == fibh->ebh) {
206 nameptr = fi->fileIdent + liu;
208 int poffset; /* Unpaded ending offset */
210 poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
214 nameptr = (uint8_t *)(fibh->ebh->b_data +
218 memcpy(nameptr, fi->fileIdent + liu,
220 memcpy(nameptr + lfi - poffset,
221 fibh->ebh->b_data, poffset);
225 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
226 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
230 if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
231 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
238 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
239 if (flen && udf_match(flen, fname, dentry->d_name.len,
240 dentry->d_name.name)) {
246 if (fibh->sbh != fibh->ebh)
258 * Look-up the inode for a given name.
261 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
262 * available for a directory. The filesystem is useless if this routine is
263 * not available for at least the filesystem's root directory.
265 * This routine is passed an incomplete dentry - it must be completed by
266 * calling d_add(dentry, inode). If the name does not exist, then the
267 * specified inode must be set to null. An error should only be returned
268 * when the lookup fails for a reason other than the name not existing.
269 * Note that the directory inode semaphore is held during the call.
271 * Refer to lookup_dentry() in fs/namei.c
272 * lookup_dentry() -> lookup() -> real_lookup() -> .
275 * dir Pointer to inode of parent directory.
276 * dentry Pointer to dentry to complete.
277 * nd Pointer to lookup nameidata
280 * <return> Zero on success.
283 * July 1, 1997 - Andrew E. Mileski
284 * Written, tested, and released.
287 static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
288 struct nameidata *nd)
290 struct inode *inode = NULL;
291 struct fileIdentDesc cfi;
292 struct udf_fileident_bh fibh;
294 if (dentry->d_name.len > UDF_NAME_LEN - 2)
295 return ERR_PTR(-ENAMETOOLONG);
299 /* temporary shorthand for specifying files by inode number */
300 if (!strncmp(dentry->d_name.name, ".B=", 3)) {
301 kernel_lb_addr lb = {
302 .logicalBlockNum = 0,
303 .partitionReferenceNum =
304 simple_strtoul(dentry->d_name.name + 3,
307 inode = udf_iget(dir->i_sb, lb);
310 return ERR_PTR(-EACCES);
313 #endif /* UDF_RECOVERY */
315 if (udf_find_entry(dir, dentry, &fibh, &cfi)) {
316 if (fibh.sbh != fibh.ebh)
320 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
323 return ERR_PTR(-EACCES);
327 d_add(dentry, inode);
332 static struct fileIdentDesc *udf_add_entry(struct inode *dir,
333 struct dentry *dentry,
334 struct udf_fileident_bh *fibh,
335 struct fileIdentDesc *cfi, int *err)
337 struct super_block *sb = dir->i_sb;
338 struct fileIdentDesc *fi = NULL;
339 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
344 loff_t size = udf_ext0_offset(dir) + dir->i_size;
352 struct extent_position epos = {};
353 struct udf_inode_info *dinfo;
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);
374 fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
376 if (dinfo->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,
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 (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
384 epos.offset -= sizeof(short_ad);
385 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
386 epos.offset -= sizeof(long_ad);
390 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
397 block = dinfo->i_location.logicalBlockNum;
400 block = udf_get_lb_pblock(dir->i_sb, dinfo->i_location, 0);
401 fibh->sbh = fibh->ebh = NULL;
402 fibh->soffset = fibh->eoffset = sb->s_blocksize;
406 while (f_pos < size) {
407 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
411 if (fibh->sbh != fibh->ebh)
419 liu = le16_to_cpu(cfi->lengthOfImpUse);
420 lfi = cfi->lengthFileIdent;
422 if (fibh->sbh == fibh->ebh)
423 nameptr = fi->fileIdent + liu;
425 int poffset; /* Unpaded ending offset */
427 poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
431 nameptr = (char *)(fibh->ebh->b_data +
435 memcpy(nameptr, fi->fileIdent + liu,
437 memcpy(nameptr + lfi - poffset,
438 fibh->ebh->b_data, poffset);
442 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
443 if (((sizeof(struct fileIdentDesc) +
444 liu + lfi + 3) & ~3) == nfidlen) {
446 cfi->descTag.tagSerialNum = cpu_to_le16(1);
447 cfi->fileVersionNum = cpu_to_le16(1);
448 cfi->fileCharacteristics = 0;
449 cfi->lengthFileIdent = namelen;
450 cfi->lengthOfImpUse = cpu_to_le16(0);
451 if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
464 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
465 if (flen && udf_match(flen, fname, dentry->d_name.len,
466 dentry->d_name.name)) {
467 if (fibh->sbh != fibh->ebh)
479 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
480 sb->s_blocksize - fibh->eoffset < nfidlen) {
483 fibh->soffset -= udf_ext0_offset(dir);
484 fibh->eoffset -= udf_ext0_offset(dir);
485 f_pos -= udf_ext0_offset(dir);
486 if (fibh->sbh != fibh->ebh)
489 fibh->sbh = fibh->ebh =
490 udf_expand_dir_adinicb(dir, &block, err);
493 epos.block = dinfo->i_location;
494 eloc.logicalBlockNum = block;
495 eloc.partitionReferenceNum =
496 dinfo->i_location.partitionReferenceNum;
497 elen = dir->i_sb->s_blocksize;
498 epos.offset = udf_file_entry_alloc_offset(dir);
499 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
500 epos.offset += sizeof(short_ad);
501 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
502 epos.offset += sizeof(long_ad);
505 if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
506 fibh->soffset = fibh->eoffset;
507 fibh->eoffset += nfidlen;
508 if (fibh->sbh != fibh->ebh) {
510 fibh->sbh = fibh->ebh;
513 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
514 block = dinfo->i_location.logicalBlockNum;
515 fi = (struct fileIdentDesc *)
516 (dinfo->i_ext.i_data +
518 udf_ext0_offset(dir) +
521 block = eloc.logicalBlockNum +
523 dir->i_sb->s_blocksize_bits);
524 fi = (struct fileIdentDesc *)
525 (fibh->sbh->b_data + fibh->soffset);
528 fibh->soffset = fibh->eoffset - sb->s_blocksize;
529 fibh->eoffset += nfidlen - sb->s_blocksize;
530 if (fibh->sbh != fibh->ebh) {
532 fibh->sbh = fibh->ebh;
535 block = eloc.logicalBlockNum + ((elen - 1) >>
536 dir->i_sb->s_blocksize_bits);
537 fibh->ebh = udf_bread(dir,
538 f_pos >> dir->i_sb->s_blocksize_bits, 1, err);
545 if (!fibh->soffset) {
546 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
547 (EXT_RECORDED_ALLOCATED >> 30)) {
548 block = eloc.logicalBlockNum + ((elen - 1) >>
549 dir->i_sb->s_blocksize_bits);
554 fibh->sbh = fibh->ebh;
555 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
557 fi = (struct fileIdentDesc *)
558 (fibh->sbh->b_data + sb->s_blocksize +
563 memset(cfi, 0, sizeof(struct fileIdentDesc));
564 if (UDF_SB(sb)->s_udfrev >= 0x0200)
565 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
568 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
570 cfi->fileVersionNum = cpu_to_le16(1);
571 cfi->lengthFileIdent = namelen;
572 cfi->lengthOfImpUse = cpu_to_le16(0);
573 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
575 dir->i_size += nfidlen;
576 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
577 dinfo->i_lenAlloc += nfidlen;
578 mark_inode_dirty(dir);
582 if (fibh->sbh != fibh->ebh)
590 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
591 struct udf_fileident_bh *fibh,
592 struct fileIdentDesc *cfi)
594 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
596 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
597 memset(&(cfi->icb), 0x00, sizeof(long_ad));
599 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
602 static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
603 struct nameidata *nd)
605 struct udf_fileident_bh fibh;
607 struct fileIdentDesc cfi, *fi;
609 struct udf_inode_info *iinfo;
612 inode = udf_new_inode(dir, mode, &err);
618 iinfo = UDF_I(inode);
619 if (iinfo->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(iinfo->i_location);
638 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
639 cpu_to_le32(iinfo->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;
659 struct udf_inode_info *iinfo;
661 if (!old_valid_dev(rdev))
666 inode = udf_new_inode(dir, mode, &err);
670 iinfo = UDF_I(inode);
671 inode->i_uid = current->fsuid;
672 init_special_inode(inode, mode, rdev);
673 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
676 mark_inode_dirty(inode);
681 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
682 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
683 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
684 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
685 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
686 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
687 mark_inode_dirty(dir);
688 mark_inode_dirty(inode);
690 if (fibh.sbh != fibh.ebh)
693 d_instantiate(dentry, inode);
701 static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
704 struct udf_fileident_bh fibh;
705 struct fileIdentDesc cfi, *fi;
707 struct udf_inode_info *dinfo = UDF_I(dir);
708 struct udf_inode_info *iinfo;
712 if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
716 inode = udf_new_inode(dir, S_IFDIR, &err);
720 iinfo = UDF_I(inode);
721 inode->i_op = &udf_dir_inode_operations;
722 inode->i_fop = &udf_dir_operations;
723 fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
726 mark_inode_dirty(inode);
731 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
732 cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
733 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
734 cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
735 cfi.fileCharacteristics =
736 FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
737 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
739 inode->i_mode = S_IFDIR | mode;
740 if (dir->i_mode & S_ISGID)
741 inode->i_mode |= S_ISGID;
742 mark_inode_dirty(inode);
744 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
747 mark_inode_dirty(inode);
751 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
752 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
753 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
754 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
755 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
756 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
758 mark_inode_dirty(dir);
759 d_instantiate(dentry, inode);
760 if (fibh.sbh != fibh.ebh)
770 static int empty_dir(struct inode *dir)
772 struct fileIdentDesc *fi, cfi;
773 struct udf_fileident_bh fibh;
775 loff_t size = udf_ext0_offset(dir) + dir->i_size;
780 struct extent_position epos = {};
781 struct udf_inode_info *dinfo = UDF_I(dir);
783 f_pos = udf_ext0_offset(dir);
784 fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
786 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
787 fibh.sbh = fibh.ebh = NULL;
788 else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
789 &epos, &eloc, &elen, &offset) ==
790 (EXT_RECORDED_ALLOCATED >> 30)) {
791 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
792 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
793 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
794 epos.offset -= sizeof(short_ad);
795 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
796 epos.offset -= sizeof(long_ad);
800 fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
810 while (f_pos < size) {
811 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
814 if (fibh.sbh != fibh.ebh)
821 if (cfi.lengthFileIdent &&
822 (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
823 if (fibh.sbh != fibh.ebh)
831 if (fibh.sbh != fibh.ebh)
839 static int udf_rmdir(struct inode *dir, struct dentry *dentry)
842 struct inode *inode = dentry->d_inode;
843 struct udf_fileident_bh fibh;
844 struct fileIdentDesc *fi, cfi;
849 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
854 tloc = lelb_to_cpu(cfi.icb.extLocation);
855 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
858 if (!empty_dir(inode))
860 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
863 if (inode->i_nlink != 2)
864 udf_warning(inode->i_sb, "udf_rmdir",
865 "empty directory has nlink != 2 (%d)",
869 inode_dec_link_count(dir);
870 inode->i_ctime = dir->i_ctime = dir->i_mtime =
871 current_fs_time(dir->i_sb);
872 mark_inode_dirty(dir);
875 if (fibh.sbh != fibh.ebh)
884 static int udf_unlink(struct inode *dir, struct dentry *dentry)
887 struct inode *inode = dentry->d_inode;
888 struct udf_fileident_bh fibh;
889 struct fileIdentDesc *fi;
890 struct fileIdentDesc cfi;
895 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
900 tloc = lelb_to_cpu(cfi.icb.extLocation);
901 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
904 if (!inode->i_nlink) {
905 udf_debug("Deleting nonexistent file (%lu), %d\n",
906 inode->i_ino, inode->i_nlink);
909 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
912 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
913 mark_inode_dirty(dir);
914 inode_dec_link_count(inode);
915 inode->i_ctime = dir->i_ctime;
919 if (fibh.sbh != fibh.ebh)
928 static int udf_symlink(struct inode *dir, struct dentry *dentry,
932 struct pathComponent *pc;
934 struct udf_fileident_bh fibh;
935 struct extent_position epos = {};
936 int eoffset, elen = 0;
937 struct fileIdentDesc *fi;
938 struct fileIdentDesc cfi;
942 char name[UDF_NAME_LEN];
944 struct buffer_head *bh;
945 struct udf_inode_info *iinfo;
948 inode = udf_new_inode(dir, S_IFLNK, &err);
952 iinfo = UDF_I(inode);
953 inode->i_mode = S_IFLNK | S_IRWXUGO;
954 inode->i_data.a_ops = &udf_symlink_aops;
955 inode->i_op = &page_symlink_inode_operations;
957 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
961 block = udf_new_block(inode->i_sb, inode,
962 iinfo->i_location.partitionReferenceNum,
963 iinfo->i_location.logicalBlockNum, &err);
966 epos.block = iinfo->i_location;
967 epos.offset = udf_file_entry_alloc_offset(inode);
969 eloc.logicalBlockNum = block;
970 eloc.partitionReferenceNum =
971 iinfo->i_location.partitionReferenceNum;
972 elen = inode->i_sb->s_blocksize;
973 iinfo->i_lenExtents = elen;
974 udf_add_aext(inode, &epos, eloc, elen, 0);
977 block = udf_get_pblock(inode->i_sb, block,
978 iinfo->i_location.partitionReferenceNum,
980 epos.bh = udf_tread(inode->i_sb, block);
981 lock_buffer(epos.bh);
982 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
983 set_buffer_uptodate(epos.bh);
984 unlock_buffer(epos.bh);
985 mark_buffer_dirty_inode(epos.bh, inode);
986 ea = epos.bh->b_data + udf_ext0_offset(inode);
988 ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
990 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
991 pc = (struct pathComponent *)ea;
993 if (*symname == '/') {
996 } while (*symname == '/');
998 pc->componentType = 1;
999 pc->lengthComponentIdent = 0;
1000 pc->componentFileVersionNum = 0;
1001 pc += sizeof(struct pathComponent);
1002 elen += sizeof(struct pathComponent);
1005 err = -ENAMETOOLONG;
1008 if (elen + sizeof(struct pathComponent) > eoffset)
1011 pc = (struct pathComponent *)(ea + elen);
1013 compstart = (char *)symname;
1017 } while (*symname && *symname != '/');
1019 pc->componentType = 5;
1020 pc->lengthComponentIdent = 0;
1021 pc->componentFileVersionNum = 0;
1022 if (compstart[0] == '.') {
1023 if ((symname - compstart) == 1)
1024 pc->componentType = 4;
1025 else if ((symname - compstart) == 2 &&
1026 compstart[1] == '.')
1027 pc->componentType = 3;
1030 if (pc->componentType == 5) {
1031 namelen = udf_put_filename(inode->i_sb, compstart, name,
1032 symname - compstart);
1036 if (elen + sizeof(struct pathComponent) + namelen >
1040 pc->lengthComponentIdent = namelen;
1042 memcpy(pc->componentIdent, name, namelen);
1045 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1050 } while (*symname == '/');
1055 inode->i_size = elen;
1056 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1057 iinfo->i_lenAlloc = inode->i_size;
1058 mark_inode_dirty(inode);
1060 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1063 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1064 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
1065 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1067 struct logicalVolIntegrityDesc *lvid =
1068 (struct logicalVolIntegrityDesc *)bh->b_data;
1069 struct logicalVolHeaderDesc *lvhd;
1071 lvhd = (struct logicalVolHeaderDesc *)
1072 lvid->logicalVolContentsUse;
1073 uniqueID = le64_to_cpu(lvhd->uniqueID);
1074 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1075 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1076 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1078 lvhd->uniqueID = cpu_to_le64(uniqueID);
1079 mark_buffer_dirty(bh);
1081 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1082 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1083 mark_inode_dirty(dir);
1084 if (fibh.sbh != fibh.ebh)
1087 d_instantiate(dentry, inode);
1095 inode_dec_link_count(inode);
1100 static int udf_link(struct dentry *old_dentry, struct inode *dir,
1101 struct dentry *dentry)
1103 struct inode *inode = old_dentry->d_inode;
1104 struct udf_fileident_bh fibh;
1105 struct fileIdentDesc cfi, *fi;
1107 struct buffer_head *bh;
1110 if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
1115 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1120 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1121 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1122 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1124 struct logicalVolIntegrityDesc *lvid =
1125 (struct logicalVolIntegrityDesc *)bh->b_data;
1126 struct logicalVolHeaderDesc *lvhd;
1128 lvhd = (struct logicalVolHeaderDesc *)
1129 (lvid->logicalVolContentsUse);
1130 uniqueID = le64_to_cpu(lvhd->uniqueID);
1131 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1132 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1133 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1135 lvhd->uniqueID = cpu_to_le64(uniqueID);
1136 mark_buffer_dirty(bh);
1138 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1139 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1140 mark_inode_dirty(dir);
1142 if (fibh.sbh != fibh.ebh)
1146 inode->i_ctime = current_fs_time(inode->i_sb);
1147 mark_inode_dirty(inode);
1148 atomic_inc(&inode->i_count);
1149 d_instantiate(dentry, inode);
1155 /* Anybody can rename anything with this: the permission checks are left to the
1156 * higher-level routines.
1158 static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1159 struct inode *new_dir, struct dentry *new_dentry)
1161 struct inode *old_inode = old_dentry->d_inode;
1162 struct inode *new_inode = new_dentry->d_inode;
1163 struct udf_fileident_bh ofibh, nfibh;
1164 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
1165 struct fileIdentDesc ocfi, ncfi;
1166 struct buffer_head *dir_bh = NULL;
1167 int retval = -ENOENT;
1168 kernel_lb_addr tloc;
1169 struct udf_inode_info *old_iinfo = UDF_I(old_inode);
1172 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1174 if (ofibh.sbh != ofibh.ebh)
1178 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1179 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
1180 != old_inode->i_ino)
1183 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1186 if (nfibh.sbh != nfibh.ebh)
1192 if (S_ISDIR(old_inode->i_mode)) {
1193 int offset = udf_ext0_offset(old_inode);
1196 retval = -ENOTEMPTY;
1197 if (!empty_dir(new_inode))
1201 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1202 dir_fi = udf_get_fileident(
1203 old_iinfo->i_ext.i_data -
1205 sizeof(struct extendedFileEntry) :
1206 sizeof(struct fileEntry)),
1207 old_inode->i_sb->s_blocksize, &offset);
1209 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1212 dir_fi = udf_get_fileident(dir_bh->b_data,
1213 old_inode->i_sb->s_blocksize, &offset);
1217 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1218 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) !=
1225 (256 << sizeof(new_dir->i_nlink)) - 1)
1229 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
1236 * Like most other Unix systems, set the ctime for inodes on a
1239 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1240 mark_inode_dirty(old_inode);
1245 ncfi.fileVersionNum = ocfi.fileVersionNum;
1246 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1247 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1248 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1250 /* The old fid may have moved - find it again */
1251 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1252 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1255 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1256 inode_dec_link_count(new_inode);
1258 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1259 mark_inode_dirty(old_dir);
1262 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
1263 udf_update_tag((char *)dir_fi,
1264 (sizeof(struct fileIdentDesc) +
1265 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1266 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1267 mark_inode_dirty(old_inode);
1269 mark_buffer_dirty_inode(dir_bh, old_inode);
1271 inode_dec_link_count(old_dir);
1273 inode_dec_link_count(new_inode);
1276 mark_inode_dirty(new_dir);
1281 if (ofibh.sbh != ofibh.ebh)
1291 if (nfibh.sbh != nfibh.ebh)
1300 const struct inode_operations udf_dir_inode_operations = {
1301 .lookup = udf_lookup,
1302 .create = udf_create,
1304 .unlink = udf_unlink,
1305 .symlink = udf_symlink,
1309 .rename = udf_rename,