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;
399 block = udf_get_lb_pblock(dir->i_sb, dinfo->i_location, 0);
400 fibh->sbh = fibh->ebh = NULL;
401 fibh->soffset = fibh->eoffset = sb->s_blocksize;
405 while (f_pos < size) {
406 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
410 if (fibh->sbh != fibh->ebh)
418 liu = le16_to_cpu(cfi->lengthOfImpUse);
419 lfi = cfi->lengthFileIdent;
421 if (fibh->sbh == fibh->ebh)
422 nameptr = fi->fileIdent + liu;
424 int poffset; /* Unpaded ending offset */
426 poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
430 nameptr = (char *)(fibh->ebh->b_data +
434 memcpy(nameptr, fi->fileIdent + liu,
436 memcpy(nameptr + lfi - poffset,
437 fibh->ebh->b_data, poffset);
441 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
442 if (((sizeof(struct fileIdentDesc) +
443 liu + lfi + 3) & ~3) == nfidlen) {
445 cfi->descTag.tagSerialNum = cpu_to_le16(1);
446 cfi->fileVersionNum = cpu_to_le16(1);
447 cfi->fileCharacteristics = 0;
448 cfi->lengthFileIdent = namelen;
449 cfi->lengthOfImpUse = cpu_to_le16(0);
450 if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
463 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
464 if (flen && udf_match(flen, fname, dentry->d_name.len,
465 dentry->d_name.name)) {
466 if (fibh->sbh != fibh->ebh)
476 if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
477 elen = (elen + sb->s_blocksize - 1) & ~(sb->s_blocksize - 1);
478 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
479 epos.offset -= sizeof(short_ad);
480 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
481 epos.offset -= sizeof(long_ad);
482 udf_write_aext(dir, &epos, eloc, elen, 1);
486 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
487 sb->s_blocksize - fibh->eoffset < nfidlen) {
490 fibh->soffset -= udf_ext0_offset(dir);
491 fibh->eoffset -= udf_ext0_offset(dir);
492 f_pos -= udf_ext0_offset(dir);
493 if (fibh->sbh != fibh->ebh)
496 fibh->sbh = fibh->ebh =
497 udf_expand_dir_adinicb(dir, &block, err);
500 epos.block = dinfo->i_location;
501 epos.offset = udf_file_entry_alloc_offset(dir);
502 /* Load extent udf_expand_dir_adinicb() has created */
503 udf_current_aext(dir, &epos, &eloc, &elen, 1);
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 (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
515 block = dinfo->i_location.logicalBlockNum;
516 fi = (struct fileIdentDesc *)
517 (dinfo->i_ext.i_data +
519 udf_ext0_offset(dir) +
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, 1, err);
546 if (!fibh->soffset) {
547 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
548 (EXT_RECORDED_ALLOCATED >> 30)) {
549 block = eloc.logicalBlockNum + ((elen - 1) >>
550 dir->i_sb->s_blocksize_bits);
555 fibh->sbh = fibh->ebh;
556 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
558 fi = (struct fileIdentDesc *)
559 (fibh->sbh->b_data + sb->s_blocksize +
564 memset(cfi, 0, sizeof(struct fileIdentDesc));
565 if (UDF_SB(sb)->s_udfrev >= 0x0200)
566 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
569 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
571 cfi->fileVersionNum = cpu_to_le16(1);
572 cfi->lengthFileIdent = namelen;
573 cfi->lengthOfImpUse = cpu_to_le16(0);
574 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
576 dir->i_size += nfidlen;
577 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
578 dinfo->i_lenAlloc += nfidlen;
579 mark_inode_dirty(dir);
583 if (fibh->sbh != fibh->ebh)
591 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
592 struct udf_fileident_bh *fibh,
593 struct fileIdentDesc *cfi)
595 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
597 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
598 memset(&(cfi->icb), 0x00, sizeof(long_ad));
600 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
603 static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
604 struct nameidata *nd)
606 struct udf_fileident_bh fibh;
608 struct fileIdentDesc cfi, *fi;
610 struct udf_inode_info *iinfo;
613 inode = udf_new_inode(dir, mode, &err);
619 iinfo = UDF_I(inode);
620 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
621 inode->i_data.a_ops = &udf_adinicb_aops;
623 inode->i_data.a_ops = &udf_aops;
624 inode->i_op = &udf_file_inode_operations;
625 inode->i_fop = &udf_file_operations;
626 inode->i_mode = mode;
627 mark_inode_dirty(inode);
629 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
632 mark_inode_dirty(inode);
637 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
638 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
639 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
640 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
641 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
642 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
643 mark_inode_dirty(dir);
644 if (fibh.sbh != fibh.ebh)
648 d_instantiate(dentry, inode);
653 static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
657 struct udf_fileident_bh fibh;
658 struct fileIdentDesc cfi, *fi;
660 struct udf_inode_info *iinfo;
662 if (!old_valid_dev(rdev))
667 inode = udf_new_inode(dir, mode, &err);
671 iinfo = UDF_I(inode);
672 inode->i_uid = current->fsuid;
673 init_special_inode(inode, mode, rdev);
674 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
677 mark_inode_dirty(inode);
682 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
683 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
684 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
685 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
686 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
687 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
688 mark_inode_dirty(dir);
689 mark_inode_dirty(inode);
691 if (fibh.sbh != fibh.ebh)
694 d_instantiate(dentry, inode);
702 static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
705 struct udf_fileident_bh fibh;
706 struct fileIdentDesc cfi, *fi;
708 struct udf_inode_info *dinfo = UDF_I(dir);
709 struct udf_inode_info *iinfo;
713 if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
717 inode = udf_new_inode(dir, S_IFDIR, &err);
721 iinfo = UDF_I(inode);
722 inode->i_op = &udf_dir_inode_operations;
723 inode->i_fop = &udf_dir_operations;
724 fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
727 mark_inode_dirty(inode);
732 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
733 cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
734 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
735 cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
736 cfi.fileCharacteristics =
737 FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
738 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
740 inode->i_mode = S_IFDIR | mode;
741 if (dir->i_mode & S_ISGID)
742 inode->i_mode |= S_ISGID;
743 mark_inode_dirty(inode);
745 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
748 mark_inode_dirty(inode);
752 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
753 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
754 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
755 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
756 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
757 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
759 mark_inode_dirty(dir);
760 d_instantiate(dentry, inode);
761 if (fibh.sbh != fibh.ebh)
771 static int empty_dir(struct inode *dir)
773 struct fileIdentDesc *fi, cfi;
774 struct udf_fileident_bh fibh;
776 loff_t size = udf_ext0_offset(dir) + dir->i_size;
781 struct extent_position epos = {};
782 struct udf_inode_info *dinfo = UDF_I(dir);
784 f_pos = udf_ext0_offset(dir);
785 fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
787 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
788 fibh.sbh = fibh.ebh = NULL;
789 else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
790 &epos, &eloc, &elen, &offset) ==
791 (EXT_RECORDED_ALLOCATED >> 30)) {
792 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
793 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
794 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
795 epos.offset -= sizeof(short_ad);
796 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
797 epos.offset -= sizeof(long_ad);
801 fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
811 while (f_pos < size) {
812 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
815 if (fibh.sbh != fibh.ebh)
822 if (cfi.lengthFileIdent &&
823 (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
824 if (fibh.sbh != fibh.ebh)
832 if (fibh.sbh != fibh.ebh)
840 static int udf_rmdir(struct inode *dir, struct dentry *dentry)
843 struct inode *inode = dentry->d_inode;
844 struct udf_fileident_bh fibh;
845 struct fileIdentDesc *fi, cfi;
850 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
855 tloc = lelb_to_cpu(cfi.icb.extLocation);
856 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
859 if (!empty_dir(inode))
861 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
864 if (inode->i_nlink != 2)
865 udf_warning(inode->i_sb, "udf_rmdir",
866 "empty directory has nlink != 2 (%d)",
870 inode_dec_link_count(dir);
871 inode->i_ctime = dir->i_ctime = dir->i_mtime =
872 current_fs_time(dir->i_sb);
873 mark_inode_dirty(dir);
876 if (fibh.sbh != fibh.ebh)
885 static int udf_unlink(struct inode *dir, struct dentry *dentry)
888 struct inode *inode = dentry->d_inode;
889 struct udf_fileident_bh fibh;
890 struct fileIdentDesc *fi;
891 struct fileIdentDesc cfi;
896 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
901 tloc = lelb_to_cpu(cfi.icb.extLocation);
902 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
905 if (!inode->i_nlink) {
906 udf_debug("Deleting nonexistent file (%lu), %d\n",
907 inode->i_ino, inode->i_nlink);
910 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
913 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
914 mark_inode_dirty(dir);
915 inode_dec_link_count(inode);
916 inode->i_ctime = dir->i_ctime;
920 if (fibh.sbh != fibh.ebh)
929 static int udf_symlink(struct inode *dir, struct dentry *dentry,
933 struct pathComponent *pc;
935 struct udf_fileident_bh fibh;
936 struct extent_position epos = {};
937 int eoffset, elen = 0;
938 struct fileIdentDesc *fi;
939 struct fileIdentDesc cfi;
943 char name[UDF_NAME_LEN];
945 struct buffer_head *bh;
946 struct udf_inode_info *iinfo;
949 inode = udf_new_inode(dir, S_IFLNK, &err);
953 iinfo = UDF_I(inode);
954 inode->i_mode = S_IFLNK | S_IRWXUGO;
955 inode->i_data.a_ops = &udf_symlink_aops;
956 inode->i_op = &page_symlink_inode_operations;
958 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
962 block = udf_new_block(inode->i_sb, inode,
963 iinfo->i_location.partitionReferenceNum,
964 iinfo->i_location.logicalBlockNum, &err);
967 epos.block = iinfo->i_location;
968 epos.offset = udf_file_entry_alloc_offset(inode);
970 eloc.logicalBlockNum = block;
971 eloc.partitionReferenceNum =
972 iinfo->i_location.partitionReferenceNum;
973 elen = inode->i_sb->s_blocksize;
974 iinfo->i_lenExtents = elen;
975 udf_add_aext(inode, &epos, eloc, elen, 0);
978 block = udf_get_pblock(inode->i_sb, block,
979 iinfo->i_location.partitionReferenceNum,
981 epos.bh = udf_tread(inode->i_sb, block);
982 lock_buffer(epos.bh);
983 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
984 set_buffer_uptodate(epos.bh);
985 unlock_buffer(epos.bh);
986 mark_buffer_dirty_inode(epos.bh, inode);
987 ea = epos.bh->b_data + udf_ext0_offset(inode);
989 ea = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
991 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
992 pc = (struct pathComponent *)ea;
994 if (*symname == '/') {
997 } while (*symname == '/');
999 pc->componentType = 1;
1000 pc->lengthComponentIdent = 0;
1001 pc->componentFileVersionNum = 0;
1002 pc += sizeof(struct pathComponent);
1003 elen += sizeof(struct pathComponent);
1006 err = -ENAMETOOLONG;
1009 if (elen + sizeof(struct pathComponent) > eoffset)
1012 pc = (struct pathComponent *)(ea + elen);
1014 compstart = (char *)symname;
1018 } while (*symname && *symname != '/');
1020 pc->componentType = 5;
1021 pc->lengthComponentIdent = 0;
1022 pc->componentFileVersionNum = 0;
1023 if (compstart[0] == '.') {
1024 if ((symname - compstart) == 1)
1025 pc->componentType = 4;
1026 else if ((symname - compstart) == 2 &&
1027 compstart[1] == '.')
1028 pc->componentType = 3;
1031 if (pc->componentType == 5) {
1032 namelen = udf_put_filename(inode->i_sb, compstart, name,
1033 symname - compstart);
1037 if (elen + sizeof(struct pathComponent) + namelen >
1041 pc->lengthComponentIdent = namelen;
1043 memcpy(pc->componentIdent, name, namelen);
1046 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1051 } while (*symname == '/');
1056 inode->i_size = elen;
1057 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1058 iinfo->i_lenAlloc = inode->i_size;
1059 mark_inode_dirty(inode);
1061 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1064 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1065 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
1066 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1068 struct logicalVolIntegrityDesc *lvid =
1069 (struct logicalVolIntegrityDesc *)bh->b_data;
1070 struct logicalVolHeaderDesc *lvhd;
1072 lvhd = (struct logicalVolHeaderDesc *)
1073 lvid->logicalVolContentsUse;
1074 uniqueID = le64_to_cpu(lvhd->uniqueID);
1075 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1076 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1077 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1079 lvhd->uniqueID = cpu_to_le64(uniqueID);
1080 mark_buffer_dirty(bh);
1082 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1083 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1084 mark_inode_dirty(dir);
1085 if (fibh.sbh != fibh.ebh)
1088 d_instantiate(dentry, inode);
1096 inode_dec_link_count(inode);
1101 static int udf_link(struct dentry *old_dentry, struct inode *dir,
1102 struct dentry *dentry)
1104 struct inode *inode = old_dentry->d_inode;
1105 struct udf_fileident_bh fibh;
1106 struct fileIdentDesc cfi, *fi;
1108 struct buffer_head *bh;
1111 if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
1116 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1121 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1122 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1123 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1125 struct logicalVolIntegrityDesc *lvid =
1126 (struct logicalVolIntegrityDesc *)bh->b_data;
1127 struct logicalVolHeaderDesc *lvhd;
1129 lvhd = (struct logicalVolHeaderDesc *)
1130 (lvid->logicalVolContentsUse);
1131 uniqueID = le64_to_cpu(lvhd->uniqueID);
1132 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1133 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1134 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1136 lvhd->uniqueID = cpu_to_le64(uniqueID);
1137 mark_buffer_dirty(bh);
1139 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1140 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1141 mark_inode_dirty(dir);
1143 if (fibh.sbh != fibh.ebh)
1147 inode->i_ctime = current_fs_time(inode->i_sb);
1148 mark_inode_dirty(inode);
1149 atomic_inc(&inode->i_count);
1150 d_instantiate(dentry, inode);
1156 /* Anybody can rename anything with this: the permission checks are left to the
1157 * higher-level routines.
1159 static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1160 struct inode *new_dir, struct dentry *new_dentry)
1162 struct inode *old_inode = old_dentry->d_inode;
1163 struct inode *new_inode = new_dentry->d_inode;
1164 struct udf_fileident_bh ofibh, nfibh;
1165 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
1166 struct fileIdentDesc ocfi, ncfi;
1167 struct buffer_head *dir_bh = NULL;
1168 int retval = -ENOENT;
1169 kernel_lb_addr tloc;
1170 struct udf_inode_info *old_iinfo = UDF_I(old_inode);
1173 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1175 if (ofibh.sbh != ofibh.ebh)
1179 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1180 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
1181 != old_inode->i_ino)
1184 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1187 if (nfibh.sbh != nfibh.ebh)
1193 if (S_ISDIR(old_inode->i_mode)) {
1194 int offset = udf_ext0_offset(old_inode);
1197 retval = -ENOTEMPTY;
1198 if (!empty_dir(new_inode))
1202 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1203 dir_fi = udf_get_fileident(
1204 old_iinfo->i_ext.i_data -
1206 sizeof(struct extendedFileEntry) :
1207 sizeof(struct fileEntry)),
1208 old_inode->i_sb->s_blocksize, &offset);
1210 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1213 dir_fi = udf_get_fileident(dir_bh->b_data,
1214 old_inode->i_sb->s_blocksize, &offset);
1218 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1219 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) !=
1226 (256 << sizeof(new_dir->i_nlink)) - 1)
1230 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
1237 * Like most other Unix systems, set the ctime for inodes on a
1240 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1241 mark_inode_dirty(old_inode);
1246 ncfi.fileVersionNum = ocfi.fileVersionNum;
1247 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1248 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1249 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1251 /* The old fid may have moved - find it again */
1252 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1253 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1256 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1257 inode_dec_link_count(new_inode);
1259 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1260 mark_inode_dirty(old_dir);
1263 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
1264 udf_update_tag((char *)dir_fi,
1265 (sizeof(struct fileIdentDesc) +
1266 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1267 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1268 mark_inode_dirty(old_inode);
1270 mark_buffer_dirty_inode(dir_bh, old_inode);
1272 inode_dec_link_count(old_dir);
1274 inode_dec_link_count(new_inode);
1277 mark_inode_dirty(new_dir);
1282 if (ofibh.sbh != ofibh.ebh)
1292 if (nfibh.sbh != nfibh.ebh)
1301 const struct inode_operations udf_dir_inode_operations = {
1302 .lookup = udf_lookup,
1303 .create = udf_create,
1305 .unlink = udf_unlink,
1306 .symlink = udf_symlink,
1310 .rename = udf_rename,