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,
40 return !memcmp(name1, name2, len1);
43 int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
44 struct fileIdentDesc *sfi, struct udf_fileident_bh *fibh,
45 uint8_t * impuse, uint8_t * fileident)
47 uint16_t crclen = fibh->eoffset - fibh->soffset - sizeof(tag);
52 uint16_t liu = le16_to_cpu(cfi->lengthOfImpUse);
53 uint8_t lfi = cfi->lengthFileIdent;
54 int padlen = fibh->eoffset - fibh->soffset - liu - lfi -
55 sizeof(struct fileIdentDesc);
58 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
61 offset = fibh->soffset + sizeof(struct fileIdentDesc);
64 if (adinicb || (offset + liu < 0))
65 memcpy((uint8_t *) sfi->impUse, impuse, liu);
67 memcpy(fibh->ebh->b_data + offset, impuse, liu);
69 memcpy((uint8_t *) sfi->impUse, impuse, -offset);
70 memcpy(fibh->ebh->b_data, impuse - offset,
78 if (adinicb || (offset + lfi < 0))
79 memcpy((uint8_t *) sfi->fileIdent + liu, fileident,
82 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
84 memcpy((uint8_t *) sfi->fileIdent + liu, fileident,
86 memcpy(fibh->ebh->b_data, fileident - offset,
93 if (adinicb || (offset + padlen < 0))
94 memset((uint8_t *) sfi->padding + liu + lfi, 0x00, padlen);
96 memset(fibh->ebh->b_data + offset, 0x00, padlen);
98 memset((uint8_t *) sfi->padding + liu + lfi, 0x00, -offset);
99 memset(fibh->ebh->b_data, 0x00, padlen + offset);
103 udf_crc((uint8_t *) cfi + sizeof(tag),
104 sizeof(struct fileIdentDesc) - sizeof(tag), 0);
106 if (fibh->sbh == fibh->ebh)
107 crc = udf_crc((uint8_t *) sfi->impUse,
108 crclen + sizeof(tag) -
109 sizeof(struct fileIdentDesc), crc);
110 else if (sizeof(struct fileIdentDesc) >= -fibh->soffset)
112 udf_crc(fibh->ebh->b_data + sizeof(struct fileIdentDesc) +
114 crclen + sizeof(tag) - sizeof(struct fileIdentDesc),
117 crc = udf_crc((uint8_t *) sfi->impUse,
118 -fibh->soffset - sizeof(struct fileIdentDesc),
120 crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
123 cfi->descTag.descCRC = cpu_to_le16(crc);
124 cfi->descTag.descCRCLength = cpu_to_le16(crclen);
126 for (i = 0; i < 16; i++)
128 checksum += ((uint8_t *) & cfi->descTag)[i];
130 cfi->descTag.tagChecksum = checksum;
131 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset))
132 memcpy((uint8_t *) sfi, (uint8_t *) cfi,
133 sizeof(struct fileIdentDesc));
135 memcpy((uint8_t *) sfi, (uint8_t *) cfi, -fibh->soffset);
136 memcpy(fibh->ebh->b_data, (uint8_t *) cfi - fibh->soffset,
137 sizeof(struct fileIdentDesc) + fibh->soffset);
141 mark_inode_dirty(inode);
143 if (fibh->sbh != fibh->ebh)
144 mark_buffer_dirty_inode(fibh->ebh, inode);
145 mark_buffer_dirty_inode(fibh->sbh, inode);
150 static struct fileIdentDesc *udf_find_entry(struct inode *dir,
151 struct dentry *dentry,
152 struct udf_fileident_bh *fibh,
153 struct fileIdentDesc *cfi)
155 struct fileIdentDesc *fi = NULL;
158 char fname[UDF_NAME_LEN];
166 struct extent_position epos = { NULL, 0, {0, 0} };
168 size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
169 f_pos = (udf_ext0_offset(dir) >> 2);
171 fibh->soffset = fibh->eoffset =
172 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
173 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
174 fibh->sbh = fibh->ebh = NULL;
175 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
177 &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
178 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
179 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
180 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
181 epos.offset -= sizeof(short_ad);
182 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
183 epos.offset -= sizeof(long_ad);
187 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block))) {
196 while ((f_pos < size)) {
197 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
201 if (fibh->sbh != fibh->ebh)
208 liu = le16_to_cpu(cfi->lengthOfImpUse);
209 lfi = cfi->lengthFileIdent;
211 if (fibh->sbh == fibh->ebh) {
212 nameptr = fi->fileIdent + liu;
214 int poffset; /* Unpaded ending offset */
217 fibh->soffset + sizeof(struct fileIdentDesc) + liu +
222 (uint8_t *) (fibh->ebh->b_data + poffset -
226 memcpy(nameptr, fi->fileIdent + liu,
228 memcpy(nameptr + lfi - poffset,
229 fibh->ebh->b_data, poffset);
233 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
234 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
238 if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
239 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
246 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi))) {
248 (flen, fname, dentry->d_name.len,
249 dentry->d_name.name)) {
255 if (fibh->sbh != fibh->ebh)
266 * Look-up the inode for a given name.
269 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
270 * available for a directory. The filesystem is useless if this routine is
271 * not available for at least the filesystem's root directory.
273 * This routine is passed an incomplete dentry - it must be completed by
274 * calling d_add(dentry, inode). If the name does not exist, then the
275 * specified inode must be set to null. An error should only be returned
276 * when the lookup fails for a reason other than the name not existing.
277 * Note that the directory inode semaphore is held during the call.
279 * Refer to lookup_dentry() in fs/namei.c
280 * lookup_dentry() -> lookup() -> real_lookup() -> .
283 * dir Pointer to inode of parent directory.
284 * dentry Pointer to dentry to complete.
285 * nd Pointer to lookup nameidata
288 * <return> Zero on success.
291 * July 1, 1997 - Andrew E. Mileski
292 * Written, tested, and released.
295 static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
296 struct nameidata *nd)
298 struct inode *inode = NULL;
299 struct fileIdentDesc cfi;
300 struct udf_fileident_bh fibh;
302 if (dentry->d_name.len > UDF_NAME_LEN - 2)
303 return ERR_PTR(-ENAMETOOLONG);
307 /* temporary shorthand for specifying files by inode number */
308 if (!strncmp(dentry->d_name.name, ".B=", 3)) {
310 { 0, simple_strtoul(dentry->d_name.name + 3, NULL, 0) };
311 inode = udf_iget(dir->i_sb, lb);
314 return ERR_PTR(-EACCES);
317 #endif /* UDF_RECOVERY */
319 if (udf_find_entry(dir, dentry, &fibh, &cfi)) {
320 if (fibh.sbh != fibh.ebh)
324 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
327 return ERR_PTR(-EACCES);
331 d_add(dentry, inode);
335 static struct fileIdentDesc *udf_add_entry(struct inode *dir,
336 struct dentry *dentry,
337 struct udf_fileident_bh *fibh,
338 struct fileIdentDesc *cfi, int *err)
340 struct super_block *sb;
341 struct fileIdentDesc *fi = NULL;
342 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
347 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
355 struct extent_position epos = { NULL, 0, {0, 0} };
360 if (!dentry->d_name.len) {
367 udf_put_filename(sb, dentry->d_name.name, name,
368 dentry->d_name.len))) {
369 *err = -ENAMETOOLONG;
375 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
377 f_pos = (udf_ext0_offset(dir) >> 2);
379 fibh->soffset = fibh->eoffset =
380 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
381 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
382 fibh->sbh = fibh->ebh = NULL;
383 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
385 &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
386 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
387 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
388 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
389 epos.offset -= sizeof(short_ad);
390 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
391 epos.offset -= sizeof(long_ad);
395 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block))) {
401 block = UDF_I_LOCATION(dir).logicalBlockNum;
404 block = udf_get_lb_pblock(dir->i_sb, UDF_I_LOCATION(dir), 0);
405 fibh->sbh = fibh->ebh = NULL;
406 fibh->soffset = fibh->eoffset = sb->s_blocksize;
410 while ((f_pos < size)) {
411 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
415 if (fibh->sbh != fibh->ebh)
423 liu = le16_to_cpu(cfi->lengthOfImpUse);
424 lfi = cfi->lengthFileIdent;
426 if (fibh->sbh == fibh->ebh)
427 nameptr = fi->fileIdent + liu;
429 int poffset; /* Unpaded ending offset */
432 fibh->soffset + sizeof(struct fileIdentDesc) + liu +
437 (char *)(fibh->ebh->b_data + poffset - lfi);
440 memcpy(nameptr, fi->fileIdent + liu,
442 memcpy(nameptr + lfi - poffset,
443 fibh->ebh->b_data, poffset);
447 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
448 if (((sizeof(struct fileIdentDesc) + liu + lfi +
449 3) & ~3) == nfidlen) {
451 cfi->descTag.tagSerialNum = cpu_to_le16(1);
452 cfi->fileVersionNum = cpu_to_le16(1);
453 cfi->fileCharacteristics = 0;
454 cfi->lengthFileIdent = namelen;
455 cfi->lengthOfImpUse = cpu_to_le16(0);
457 (dir, cfi, fi, fibh, NULL, name))
469 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)) &&
470 udf_match(flen, fname, dentry->d_name.len,
471 dentry->d_name.name)) {
472 if (fibh->sbh != fibh->ebh)
484 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB &&
485 sb->s_blocksize - fibh->eoffset < nfidlen) {
488 fibh->soffset -= udf_ext0_offset(dir);
489 fibh->eoffset -= udf_ext0_offset(dir);
490 f_pos -= (udf_ext0_offset(dir) >> 2);
491 if (fibh->sbh != fibh->ebh)
495 (fibh->sbh = fibh->ebh =
496 udf_expand_dir_adinicb(dir, &block, err)))
498 epos.block = UDF_I_LOCATION(dir);
499 eloc.logicalBlockNum = block;
500 eloc.partitionReferenceNum =
501 UDF_I_LOCATION(dir).partitionReferenceNum;
502 elen = dir->i_sb->s_blocksize;
503 epos.offset = udf_file_entry_alloc_offset(dir);
504 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
505 epos.offset += sizeof(short_ad);
506 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
507 epos.offset += sizeof(long_ad);
510 if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
511 fibh->soffset = fibh->eoffset;
512 fibh->eoffset += nfidlen;
513 if (fibh->sbh != fibh->ebh) {
515 fibh->sbh = fibh->ebh;
518 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
519 block = UDF_I_LOCATION(dir).logicalBlockNum;
520 fi = (struct fileIdentDesc *)(UDF_I_DATA(dir) +
522 udf_ext0_offset(dir) +
523 UDF_I_LENEATTR(dir));
525 block = eloc.logicalBlockNum + ((elen - 1) >>
528 fi = (struct fileIdentDesc *)(fibh->sbh->b_data +
532 fibh->soffset = fibh->eoffset - sb->s_blocksize;
533 fibh->eoffset += nfidlen - sb->s_blocksize;
534 if (fibh->sbh != fibh->ebh) {
536 fibh->sbh = fibh->ebh;
539 block = eloc.logicalBlockNum + ((elen - 1) >>
540 dir->i_sb->s_blocksize_bits);
544 udf_bread(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
551 if (!(fibh->soffset)) {
552 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
553 (EXT_RECORDED_ALLOCATED >> 30)) {
554 block = eloc.logicalBlockNum + ((elen - 1) >>
561 fibh->sbh = fibh->ebh;
562 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
564 fi = (struct fileIdentDesc *)
565 (fibh->sbh->b_data + sb->s_blocksize +
570 memset(cfi, 0, sizeof(struct fileIdentDesc));
571 if (UDF_SB_UDFREV(sb) >= 0x0200)
572 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
575 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
577 cfi->fileVersionNum = cpu_to_le16(1);
578 cfi->lengthFileIdent = namelen;
579 cfi->lengthOfImpUse = cpu_to_le16(0);
580 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
582 dir->i_size += nfidlen;
583 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
584 UDF_I_LENALLOC(dir) += nfidlen;
585 mark_inode_dirty(dir);
589 if (fibh->sbh != fibh->ebh)
597 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
598 struct udf_fileident_bh *fibh,
599 struct fileIdentDesc *cfi)
601 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
602 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
603 memset(&(cfi->icb), 0x00, sizeof(long_ad));
604 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
607 static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
608 struct nameidata *nd)
610 struct udf_fileident_bh fibh;
612 struct fileIdentDesc cfi, *fi;
616 inode = udf_new_inode(dir, mode, &err);
622 if (UDF_I_ALLOCTYPE(inode) == 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 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
633 mark_inode_dirty(inode);
638 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
639 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
640 *(__le32 *) ((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
641 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
642 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
643 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
644 mark_inode_dirty(dir);
646 if (fibh.sbh != fibh.ebh)
650 d_instantiate(dentry, inode);
654 static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
658 struct udf_fileident_bh fibh;
659 struct fileIdentDesc cfi, *fi;
662 if (!old_valid_dev(rdev))
667 inode = udf_new_inode(dir, mode, &err);
671 inode->i_uid = current->fsuid;
672 init_special_inode(inode, mode, rdev);
673 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
675 mark_inode_dirty(inode);
680 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
681 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
682 *(__le32 *) ((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
683 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
684 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
685 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
686 mark_inode_dirty(dir);
688 mark_inode_dirty(inode);
690 if (fibh.sbh != fibh.ebh)
693 d_instantiate(dentry, inode);
700 static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
703 struct udf_fileident_bh fibh;
704 struct fileIdentDesc cfi, *fi;
709 if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
713 inode = udf_new_inode(dir, S_IFDIR, &err);
717 inode->i_op = &udf_dir_inode_operations;
718 inode->i_fop = &udf_dir_operations;
719 if (!(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_LOCATION(dir));
728 *(__le32 *) ((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
729 cpu_to_le32(UDF_I_UNIQUE(dir) & 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 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
741 mark_inode_dirty(inode);
745 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
746 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
747 *(__le32 *) ((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
748 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
749 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
750 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
752 mark_inode_dirty(dir);
753 d_instantiate(dentry, inode);
754 if (fibh.sbh != fibh.ebh)
763 static int empty_dir(struct inode *dir)
765 struct fileIdentDesc *fi, cfi;
766 struct udf_fileident_bh fibh;
768 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
773 struct extent_position epos = { NULL, 0, {0, 0} };
775 f_pos = (udf_ext0_offset(dir) >> 2);
777 fibh.soffset = fibh.eoffset =
778 (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
780 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
781 fibh.sbh = fibh.ebh = NULL;
782 else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
784 &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
785 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
786 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
787 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
788 epos.offset -= sizeof(short_ad);
789 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
790 epos.offset -= sizeof(long_ad);
794 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block))) {
803 while ((f_pos < size)) {
804 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
808 if (fibh.sbh != fibh.ebh)
815 if (cfi.lengthFileIdent
816 && (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
817 if (fibh.sbh != fibh.ebh)
824 if (fibh.sbh != fibh.ebh)
831 static int udf_rmdir(struct inode *dir, struct dentry *dentry)
834 struct inode *inode = dentry->d_inode;
835 struct udf_fileident_bh fibh;
836 struct fileIdentDesc *fi, cfi;
841 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
846 tloc = lelb_to_cpu(cfi.icb.extLocation);
847 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
850 if (!empty_dir(inode))
852 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
855 if (inode->i_nlink != 2)
856 udf_warning(inode->i_sb, "udf_rmdir",
857 "empty directory has nlink != 2 (%d)",
861 inode_dec_link_count(dir);
862 inode->i_ctime = dir->i_ctime = dir->i_mtime =
863 current_fs_time(dir->i_sb);
864 mark_inode_dirty(dir);
867 if (fibh.sbh != fibh.ebh)
875 static int udf_unlink(struct inode *dir, struct dentry *dentry)
878 struct inode *inode = dentry->d_inode;
879 struct udf_fileident_bh fibh;
880 struct fileIdentDesc *fi;
881 struct fileIdentDesc cfi;
886 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
891 tloc = lelb_to_cpu(cfi.icb.extLocation);
892 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
895 if (!inode->i_nlink) {
896 udf_debug("Deleting nonexistent file (%lu), %d\n",
897 inode->i_ino, inode->i_nlink);
900 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
903 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
904 mark_inode_dirty(dir);
905 inode_dec_link_count(inode);
906 inode->i_ctime = dir->i_ctime;
910 if (fibh.sbh != fibh.ebh)
918 static int udf_symlink(struct inode *dir, struct dentry *dentry,
922 struct pathComponent *pc;
924 struct udf_fileident_bh fibh;
925 struct extent_position epos = { NULL, 0, {0, 0} };
926 int eoffset, elen = 0;
927 struct fileIdentDesc *fi;
928 struct fileIdentDesc cfi;
932 char name[UDF_NAME_LEN];
936 if (!(inode = udf_new_inode(dir, S_IFLNK, &err)))
939 inode->i_mode = S_IFLNK | S_IRWXUGO;
940 inode->i_data.a_ops = &udf_symlink_aops;
941 inode->i_op = &page_symlink_inode_operations;
943 if (UDF_I_ALLOCTYPE(inode) != ICBTAG_FLAG_AD_IN_ICB) {
947 block = udf_new_block(inode->i_sb, inode,
948 UDF_I_LOCATION(inode).
949 partitionReferenceNum,
950 UDF_I_LOCATION(inode).logicalBlockNum,
954 epos.block = UDF_I_LOCATION(inode);
955 epos.offset = udf_file_entry_alloc_offset(inode);
957 eloc.logicalBlockNum = block;
958 eloc.partitionReferenceNum =
959 UDF_I_LOCATION(inode).partitionReferenceNum;
960 elen = inode->i_sb->s_blocksize;
961 UDF_I_LENEXTENTS(inode) = elen;
962 udf_add_aext(inode, &epos, eloc, elen, 0);
965 block = udf_get_pblock(inode->i_sb, block,
966 UDF_I_LOCATION(inode).
967 partitionReferenceNum, 0);
968 epos.bh = udf_tread(inode->i_sb, block);
969 lock_buffer(epos.bh);
970 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
971 set_buffer_uptodate(epos.bh);
972 unlock_buffer(epos.bh);
973 mark_buffer_dirty_inode(epos.bh, inode);
974 ea = epos.bh->b_data + udf_ext0_offset(inode);
976 ea = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
978 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
979 pc = (struct pathComponent *)ea;
981 if (*symname == '/') {
984 } while (*symname == '/');
986 pc->componentType = 1;
987 pc->lengthComponentIdent = 0;
988 pc->componentFileVersionNum = 0;
989 pc += sizeof(struct pathComponent);
990 elen += sizeof(struct pathComponent);
996 if (elen + sizeof(struct pathComponent) > eoffset)
999 pc = (struct pathComponent *)(ea + elen);
1001 compstart = (char *)symname;
1005 } while (*symname && *symname != '/');
1007 pc->componentType = 5;
1008 pc->lengthComponentIdent = 0;
1009 pc->componentFileVersionNum = 0;
1010 if (compstart[0] == '.') {
1011 if ((symname - compstart) == 1)
1012 pc->componentType = 4;
1013 else if ((symname - compstart) == 2
1014 && compstart[1] == '.')
1015 pc->componentType = 3;
1018 if (pc->componentType == 5) {
1021 udf_put_filename(inode->i_sb, compstart, name,
1022 symname - compstart)))
1025 if (elen + sizeof(struct pathComponent) + namelen >
1029 pc->lengthComponentIdent = namelen;
1031 memcpy(pc->componentIdent, name, namelen);
1034 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1039 } while (*symname == '/');
1044 inode->i_size = elen;
1045 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1046 UDF_I_LENALLOC(inode) = inode->i_size;
1047 mark_inode_dirty(inode);
1049 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1051 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1052 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1053 if (UDF_SB_LVIDBH(inode->i_sb)) {
1054 struct logicalVolHeaderDesc *lvhd;
1057 (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->
1058 logicalVolContentsUse);
1059 uniqueID = le64_to_cpu(lvhd->uniqueID);
1060 *(__le32 *) ((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1061 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1062 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1064 lvhd->uniqueID = cpu_to_le64(uniqueID);
1065 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1067 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1068 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
1069 mark_inode_dirty(dir);
1071 if (fibh.sbh != fibh.ebh)
1074 d_instantiate(dentry, inode);
1082 inode_dec_link_count(inode);
1087 static int udf_link(struct dentry *old_dentry, struct inode *dir,
1088 struct dentry *dentry)
1090 struct inode *inode = old_dentry->d_inode;
1091 struct udf_fileident_bh fibh;
1092 struct fileIdentDesc cfi, *fi;
1096 if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
1101 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
1105 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1106 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1107 if (UDF_SB_LVIDBH(inode->i_sb)) {
1108 struct logicalVolHeaderDesc *lvhd;
1111 (struct logicalVolHeaderDesc *)(UDF_SB_LVID(inode->i_sb)->
1112 logicalVolContentsUse);
1113 uniqueID = le64_to_cpu(lvhd->uniqueID);
1114 *(__le32 *) ((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1115 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1116 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1118 lvhd->uniqueID = cpu_to_le64(uniqueID);
1119 mark_buffer_dirty(UDF_SB_LVIDBH(inode->i_sb));
1121 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1122 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
1123 mark_inode_dirty(dir);
1125 if (fibh.sbh != fibh.ebh)
1129 inode->i_ctime = current_fs_time(inode->i_sb);
1130 mark_inode_dirty(inode);
1131 atomic_inc(&inode->i_count);
1132 d_instantiate(dentry, inode);
1137 /* Anybody can rename anything with this: the permission checks are left to the
1138 * higher-level routines.
1140 static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1141 struct inode *new_dir, struct dentry *new_dentry)
1143 struct inode *old_inode = old_dentry->d_inode;
1144 struct inode *new_inode = new_dentry->d_inode;
1145 struct udf_fileident_bh ofibh, nfibh;
1146 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi =
1148 struct buffer_head *dir_bh = NULL;
1149 int retval = -ENOENT;
1150 kernel_lb_addr tloc;
1153 if ((ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi))) {
1154 if (ofibh.sbh != ofibh.ebh)
1158 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1159 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
1160 != old_inode->i_ino)
1163 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1166 if (nfibh.sbh != nfibh.ebh)
1172 if (S_ISDIR(old_inode->i_mode)) {
1173 uint32_t offset = udf_ext0_offset(old_inode);
1176 retval = -ENOTEMPTY;
1177 if (!empty_dir(new_inode))
1181 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB) {
1182 dir_fi = udf_get_fileident(UDF_I_DATA(old_inode) -
1183 (UDF_I_EFE(old_inode) ?
1185 extendedFileEntry) :
1186 sizeof(struct fileEntry)),
1187 old_inode->i_sb->s_blocksize,
1190 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1194 udf_get_fileident(dir_bh->b_data,
1195 old_inode->i_sb->s_blocksize,
1200 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1201 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0)
1207 && new_dir->i_nlink >=
1208 (256 << sizeof(new_dir->i_nlink)) - 1)
1213 udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, &retval);
1219 * Like most other Unix systems, set the ctime for inodes on a
1222 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1223 mark_inode_dirty(old_inode);
1228 ncfi.fileVersionNum = ocfi.fileVersionNum;
1229 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1230 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1231 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1233 /* The old fid may have moved - find it again */
1234 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1235 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1238 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1239 inode_dec_link_count(new_inode);
1241 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1242 mark_inode_dirty(old_dir);
1245 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(new_dir));
1246 udf_update_tag((char *)dir_fi, (sizeof(struct fileIdentDesc) +
1247 le16_to_cpu(dir_fi->
1250 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB) {
1251 mark_inode_dirty(old_inode);
1253 mark_buffer_dirty_inode(dir_bh, old_inode);
1254 inode_dec_link_count(old_dir);
1256 inode_dec_link_count(new_inode);
1259 mark_inode_dirty(new_dir);
1264 if (ofibh.sbh != ofibh.ebh)
1274 if (nfibh.sbh != nfibh.ebh)
1282 const struct inode_operations udf_dir_inode_operations = {
1283 .lookup = udf_lookup,
1284 .create = udf_create,
1286 .unlink = udf_unlink,
1287 .symlink = udf_symlink,
1291 .rename = udf_rename,