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);
53 uint16_t liu = le16_to_cpu(cfi->lengthOfImpUse);
54 uint8_t lfi = cfi->lengthFileIdent;
55 int padlen = fibh->eoffset - fibh->soffset - liu - lfi -
56 sizeof(struct fileIdentDesc);
59 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
62 offset = fibh->soffset + sizeof(struct fileIdentDesc);
65 if (adinicb || (offset + liu < 0)) {
66 memcpy((uint8_t *)sfi->impUse, impuse, liu);
67 } else if (offset >= 0) {
68 memcpy(fibh->ebh->b_data + offset, impuse, liu);
70 memcpy((uint8_t *)sfi->impUse, impuse, -offset);
71 memcpy(fibh->ebh->b_data, impuse - offset, liu + offset);
78 if (adinicb || (offset + lfi < 0)) {
79 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, lfi);
80 } else if (offset >= 0) {
81 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
83 memcpy((uint8_t *)sfi->fileIdent + liu, fileident, -offset);
84 memcpy(fibh->ebh->b_data, fileident - offset, lfi + offset);
90 if (adinicb || (offset + padlen < 0)) {
91 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, padlen);
92 } else if (offset >= 0) {
93 memset(fibh->ebh->b_data + offset, 0x00, padlen);
95 memset((uint8_t *)sfi->padding + liu + lfi, 0x00, -offset);
96 memset(fibh->ebh->b_data, 0x00, padlen + offset);
99 crc = udf_crc((uint8_t *)cfi + sizeof(tag),
100 sizeof(struct fileIdentDesc) - sizeof(tag), 0);
102 if (fibh->sbh == fibh->ebh) {
103 crc = udf_crc((uint8_t *)sfi->impUse,
104 crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
105 } else if (sizeof(struct fileIdentDesc) >= -fibh->soffset) {
106 crc = udf_crc(fibh->ebh->b_data + sizeof(struct fileIdentDesc) + fibh->soffset,
107 crclen + sizeof(tag) - sizeof(struct fileIdentDesc), crc);
109 crc = udf_crc((uint8_t *)sfi->impUse,
110 -fibh->soffset - sizeof(struct fileIdentDesc), crc);
111 crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);
114 cfi->descTag.descCRC = cpu_to_le16(crc);
115 cfi->descTag.descCRCLength = cpu_to_le16(crclen);
117 for (i = 0; i < 16; i++) {
119 checksum += ((uint8_t *)&cfi->descTag)[i];
122 cfi->descTag.tagChecksum = checksum;
123 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset)) {
124 memcpy((uint8_t *)sfi, (uint8_t *)cfi, sizeof(struct fileIdentDesc));
126 memcpy((uint8_t *)sfi, (uint8_t *)cfi, -fibh->soffset);
127 memcpy(fibh->ebh->b_data, (uint8_t *)cfi - fibh->soffset,
128 sizeof(struct fileIdentDesc) + fibh->soffset);
132 mark_inode_dirty(inode);
134 if (fibh->sbh != fibh->ebh)
135 mark_buffer_dirty_inode(fibh->ebh, inode);
136 mark_buffer_dirty_inode(fibh->sbh, inode);
141 static struct fileIdentDesc *udf_find_entry(struct inode *dir,
142 struct dentry *dentry,
143 struct udf_fileident_bh *fibh,
144 struct fileIdentDesc *cfi)
146 struct fileIdentDesc *fi = NULL;
149 char fname[UDF_NAME_LEN];
157 struct extent_position epos = {};
159 size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
160 f_pos = (udf_ext0_offset(dir) >> 2);
162 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
163 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
164 fibh->sbh = fibh->ebh = NULL;
165 } else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
166 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
167 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
168 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
169 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
170 epos.offset -= sizeof(short_ad);
171 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
172 epos.offset -= sizeof(long_ad);
177 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block))) {
186 while ((f_pos < size)) {
187 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
190 if (fibh->sbh != fibh->ebh)
197 liu = le16_to_cpu(cfi->lengthOfImpUse);
198 lfi = cfi->lengthFileIdent;
200 if (fibh->sbh == fibh->ebh) {
201 nameptr = fi->fileIdent + liu;
203 int poffset; /* Unpaded ending offset */
205 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
207 if (poffset >= lfi) {
208 nameptr = (uint8_t *)(fibh->ebh->b_data + poffset - lfi);
211 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
212 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
216 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
217 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
221 if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
222 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
229 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi))) {
230 if (udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name)) {
237 if (fibh->sbh != fibh->ebh)
249 * Look-up the inode for a given name.
252 * Required - lookup_dentry() will return -ENOTDIR if this routine is not
253 * available for a directory. The filesystem is useless if this routine is
254 * not available for at least the filesystem's root directory.
256 * This routine is passed an incomplete dentry - it must be completed by
257 * calling d_add(dentry, inode). If the name does not exist, then the
258 * specified inode must be set to null. An error should only be returned
259 * when the lookup fails for a reason other than the name not existing.
260 * Note that the directory inode semaphore is held during the call.
262 * Refer to lookup_dentry() in fs/namei.c
263 * lookup_dentry() -> lookup() -> real_lookup() -> .
266 * dir Pointer to inode of parent directory.
267 * dentry Pointer to dentry to complete.
268 * nd Pointer to lookup nameidata
271 * <return> Zero on success.
274 * July 1, 1997 - Andrew E. Mileski
275 * Written, tested, and released.
278 static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
279 struct nameidata *nd)
281 struct inode *inode = NULL;
282 struct fileIdentDesc cfi;
283 struct udf_fileident_bh fibh;
285 if (dentry->d_name.len > UDF_NAME_LEN - 2)
286 return ERR_PTR(-ENAMETOOLONG);
290 /* temporary shorthand for specifying files by inode number */
291 if (!strncmp(dentry->d_name.name, ".B=", 3)) {
292 kernel_lb_addr lb = {
293 .logicalBlockNum = 0,
294 .partitionReferenceNum = simple_strtoul(dentry->d_name.name + 3,
297 inode = udf_iget(dir->i_sb, lb);
300 return ERR_PTR(-EACCES);
304 #endif /* UDF_RECOVERY */
306 if (udf_find_entry(dir, dentry, &fibh, &cfi)) {
307 if (fibh.sbh != fibh.ebh)
311 inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));
314 return ERR_PTR(-EACCES);
318 d_add(dentry, inode);
323 static struct fileIdentDesc *udf_add_entry(struct inode *dir,
324 struct dentry *dentry,
325 struct udf_fileident_bh *fibh,
326 struct fileIdentDesc *cfi, int *err)
328 struct super_block *sb = dir->i_sb;
329 struct fileIdentDesc *fi = NULL;
330 char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];
335 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
343 struct extent_position epos = {};
346 if (!dentry->d_name.len) {
350 if (!(namelen = udf_put_filename(sb, dentry->d_name.name, name,
351 dentry->d_name.len))) {
352 *err = -ENAMETOOLONG;
359 nfidlen = (sizeof(struct fileIdentDesc) + namelen + 3) & ~3;
361 f_pos = (udf_ext0_offset(dir) >> 2);
363 fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
364 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
365 fibh->sbh = fibh->ebh = NULL;
366 } else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
367 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
368 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
369 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
370 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
371 epos.offset -= sizeof(short_ad);
372 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
373 epos.offset -= sizeof(long_ad);
378 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block))) {
384 block = UDF_I_LOCATION(dir).logicalBlockNum;
387 block = udf_get_lb_pblock(dir->i_sb, UDF_I_LOCATION(dir), 0);
388 fibh->sbh = fibh->ebh = NULL;
389 fibh->soffset = fibh->eoffset = sb->s_blocksize;
393 while ((f_pos < size)) {
394 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
398 if (fibh->sbh != fibh->ebh)
406 liu = le16_to_cpu(cfi->lengthOfImpUse);
407 lfi = cfi->lengthFileIdent;
409 if (fibh->sbh == fibh->ebh) {
410 nameptr = fi->fileIdent + liu;
412 int poffset; /* Unpaded ending offset */
414 poffset = fibh->soffset + sizeof(struct fileIdentDesc) + liu + lfi;
416 if (poffset >= lfi) {
417 nameptr = (char *)(fibh->ebh->b_data + poffset - lfi);
420 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
421 memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);
425 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
426 if (((sizeof(struct fileIdentDesc) + liu + lfi + 3) & ~3) == nfidlen) {
428 cfi->descTag.tagSerialNum = cpu_to_le16(1);
429 cfi->fileVersionNum = cpu_to_le16(1);
430 cfi->fileCharacteristics = 0;
431 cfi->lengthFileIdent = namelen;
432 cfi->lengthOfImpUse = cpu_to_le16(0);
433 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
445 if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)) &&
446 udf_match(flen, fname, dentry->d_name.len, dentry->d_name.name)) {
447 if (fibh->sbh != fibh->ebh)
459 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB &&
460 sb->s_blocksize - fibh->eoffset < nfidlen) {
463 fibh->soffset -= udf_ext0_offset(dir);
464 fibh->eoffset -= udf_ext0_offset(dir);
465 f_pos -= (udf_ext0_offset(dir) >> 2);
466 if (fibh->sbh != fibh->ebh)
469 if (!(fibh->sbh = fibh->ebh = udf_expand_dir_adinicb(dir, &block, err)))
471 epos.block = UDF_I_LOCATION(dir);
472 eloc.logicalBlockNum = block;
473 eloc.partitionReferenceNum = UDF_I_LOCATION(dir).partitionReferenceNum;
474 elen = dir->i_sb->s_blocksize;
475 epos.offset = udf_file_entry_alloc_offset(dir);
476 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
477 epos.offset += sizeof(short_ad);
478 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
479 epos.offset += sizeof(long_ad);
482 if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
483 fibh->soffset = fibh->eoffset;
484 fibh->eoffset += nfidlen;
485 if (fibh->sbh != fibh->ebh) {
487 fibh->sbh = fibh->ebh;
490 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
491 block = UDF_I_LOCATION(dir).logicalBlockNum;
492 fi = (struct fileIdentDesc *)(UDF_I_DATA(dir) + fibh->soffset -
493 udf_ext0_offset(dir) +
494 UDF_I_LENEATTR(dir));
496 block = eloc.logicalBlockNum + ((elen - 1) >>
497 dir->i_sb->s_blocksize_bits);
498 fi = (struct fileIdentDesc *)(fibh->sbh->b_data + fibh->soffset);
501 fibh->soffset = fibh->eoffset - sb->s_blocksize;
502 fibh->eoffset += nfidlen - sb->s_blocksize;
503 if (fibh->sbh != fibh->ebh) {
505 fibh->sbh = fibh->ebh;
508 block = eloc.logicalBlockNum + ((elen - 1) >>
509 dir->i_sb->s_blocksize_bits);
510 fibh->ebh = udf_bread(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2), 1, err);
517 if (!fibh->soffset) {
518 if (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
519 (EXT_RECORDED_ALLOCATED >> 30)) {
520 block = eloc.logicalBlockNum + ((elen - 1) >>
521 dir->i_sb->s_blocksize_bits);
527 fibh->sbh = fibh->ebh;
528 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
530 fi = (struct fileIdentDesc *)
531 (fibh->sbh->b_data + sb->s_blocksize + fibh->soffset);
535 memset(cfi, 0, sizeof(struct fileIdentDesc));
536 if (UDF_SB(sb)->s_udfrev >= 0x0200)
537 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block, sizeof(tag));
539 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block, sizeof(tag));
540 cfi->fileVersionNum = cpu_to_le16(1);
541 cfi->lengthFileIdent = namelen;
542 cfi->lengthOfImpUse = cpu_to_le16(0);
543 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
545 dir->i_size += nfidlen;
546 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
547 UDF_I_LENALLOC(dir) += nfidlen;
548 mark_inode_dirty(dir);
552 if (fibh->sbh != fibh->ebh)
560 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
561 struct udf_fileident_bh *fibh,
562 struct fileIdentDesc *cfi)
564 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
566 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
567 memset(&(cfi->icb), 0x00, sizeof(long_ad));
569 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
572 static int udf_create(struct inode *dir, struct dentry *dentry, int mode,
573 struct nameidata *nd)
575 struct udf_fileident_bh fibh;
577 struct fileIdentDesc cfi, *fi;
581 inode = udf_new_inode(dir, mode, &err);
587 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
588 inode->i_data.a_ops = &udf_adinicb_aops;
590 inode->i_data.a_ops = &udf_aops;
591 inode->i_op = &udf_file_inode_operations;
592 inode->i_fop = &udf_file_operations;
593 inode->i_mode = mode;
594 mark_inode_dirty(inode);
596 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
598 mark_inode_dirty(inode);
603 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
604 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
605 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
606 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
607 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
608 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
609 mark_inode_dirty(dir);
611 if (fibh.sbh != fibh.ebh)
615 d_instantiate(dentry, inode);
620 static int udf_mknod(struct inode *dir, struct dentry *dentry, int mode,
624 struct udf_fileident_bh fibh;
625 struct fileIdentDesc cfi, *fi;
628 if (!old_valid_dev(rdev))
633 inode = udf_new_inode(dir, mode, &err);
637 inode->i_uid = current->fsuid;
638 init_special_inode(inode, mode, rdev);
639 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
641 mark_inode_dirty(inode);
646 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
647 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
648 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
649 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
650 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
651 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
652 mark_inode_dirty(dir);
654 mark_inode_dirty(inode);
656 if (fibh.sbh != fibh.ebh)
659 d_instantiate(dentry, inode);
667 static int udf_mkdir(struct inode *dir, struct dentry *dentry, int mode)
670 struct udf_fileident_bh fibh;
671 struct fileIdentDesc cfi, *fi;
676 if (dir->i_nlink >= (256 << sizeof(dir->i_nlink)) - 1)
680 inode = udf_new_inode(dir, S_IFDIR, &err);
684 inode->i_op = &udf_dir_inode_operations;
685 inode->i_fop = &udf_dir_operations;
686 if (!(fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err))) {
688 mark_inode_dirty(inode);
693 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
694 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(dir));
695 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
696 cpu_to_le32(UDF_I_UNIQUE(dir) & 0x00000000FFFFFFFFUL);
697 cfi.fileCharacteristics = FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
698 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
700 inode->i_mode = S_IFDIR | mode;
701 if (dir->i_mode & S_ISGID)
702 inode->i_mode |= S_ISGID;
703 mark_inode_dirty(inode);
705 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
707 mark_inode_dirty(inode);
711 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
712 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
713 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
714 cpu_to_le32(UDF_I_UNIQUE(inode) & 0x00000000FFFFFFFFUL);
715 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
716 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
718 mark_inode_dirty(dir);
719 d_instantiate(dentry, inode);
720 if (fibh.sbh != fibh.ebh)
730 static int empty_dir(struct inode *dir)
732 struct fileIdentDesc *fi, cfi;
733 struct udf_fileident_bh fibh;
735 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
740 struct extent_position epos = {};
742 f_pos = (udf_ext0_offset(dir) >> 2);
744 fibh.soffset = fibh.eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
746 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
747 fibh.sbh = fibh.ebh = NULL;
748 } else if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),
749 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
750 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
751 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
752 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
753 epos.offset -= sizeof(short_ad);
754 else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
755 epos.offset -= sizeof(long_ad);
760 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block))) {
769 while ((f_pos < size)) {
770 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
773 if (fibh.sbh != fibh.ebh)
780 if (cfi.lengthFileIdent &&
781 (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
782 if (fibh.sbh != fibh.ebh)
790 if (fibh.sbh != fibh.ebh)
798 static int udf_rmdir(struct inode *dir, struct dentry *dentry)
801 struct inode *inode = dentry->d_inode;
802 struct udf_fileident_bh fibh;
803 struct fileIdentDesc *fi, cfi;
808 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
813 tloc = lelb_to_cpu(cfi.icb.extLocation);
814 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
817 if (!empty_dir(inode))
819 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
822 if (inode->i_nlink != 2)
823 udf_warning(inode->i_sb, "udf_rmdir",
824 "empty directory has nlink != 2 (%d)",
828 inode_dec_link_count(dir);
829 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
830 mark_inode_dirty(dir);
833 if (fibh.sbh != fibh.ebh)
842 static int udf_unlink(struct inode *dir, struct dentry *dentry)
845 struct inode *inode = dentry->d_inode;
846 struct udf_fileident_bh fibh;
847 struct fileIdentDesc *fi;
848 struct fileIdentDesc cfi;
853 fi = udf_find_entry(dir, dentry, &fibh, &cfi);
858 tloc = lelb_to_cpu(cfi.icb.extLocation);
859 if (udf_get_lb_pblock(dir->i_sb, tloc, 0) != inode->i_ino)
862 if (!inode->i_nlink) {
863 udf_debug("Deleting nonexistent file (%lu), %d\n",
864 inode->i_ino, inode->i_nlink);
867 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
870 dir->i_ctime = dir->i_mtime = current_fs_time(dir->i_sb);
871 mark_inode_dirty(dir);
872 inode_dec_link_count(inode);
873 inode->i_ctime = dir->i_ctime;
877 if (fibh.sbh != fibh.ebh)
886 static int udf_symlink(struct inode *dir, struct dentry *dentry,
890 struct pathComponent *pc;
892 struct udf_fileident_bh fibh;
893 struct extent_position epos = {};
894 int eoffset, elen = 0;
895 struct fileIdentDesc *fi;
896 struct fileIdentDesc cfi;
900 char name[UDF_NAME_LEN];
902 struct buffer_head *bh;
905 if (!(inode = udf_new_inode(dir, S_IFLNK, &err)))
908 inode->i_mode = S_IFLNK | S_IRWXUGO;
909 inode->i_data.a_ops = &udf_symlink_aops;
910 inode->i_op = &page_symlink_inode_operations;
912 if (UDF_I_ALLOCTYPE(inode) != ICBTAG_FLAG_AD_IN_ICB) {
916 block = udf_new_block(inode->i_sb, inode,
917 UDF_I_LOCATION(inode).partitionReferenceNum,
918 UDF_I_LOCATION(inode).logicalBlockNum, &err);
921 epos.block = UDF_I_LOCATION(inode);
922 epos.offset = udf_file_entry_alloc_offset(inode);
924 eloc.logicalBlockNum = block;
925 eloc.partitionReferenceNum = UDF_I_LOCATION(inode).partitionReferenceNum;
926 elen = inode->i_sb->s_blocksize;
927 UDF_I_LENEXTENTS(inode) = elen;
928 udf_add_aext(inode, &epos, eloc, elen, 0);
931 block = udf_get_pblock(inode->i_sb, block,
932 UDF_I_LOCATION(inode).partitionReferenceNum, 0);
933 epos.bh = udf_tread(inode->i_sb, block);
934 lock_buffer(epos.bh);
935 memset(epos.bh->b_data, 0x00, inode->i_sb->s_blocksize);
936 set_buffer_uptodate(epos.bh);
937 unlock_buffer(epos.bh);
938 mark_buffer_dirty_inode(epos.bh, inode);
939 ea = epos.bh->b_data + udf_ext0_offset(inode);
941 ea = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
944 eoffset = inode->i_sb->s_blocksize - udf_ext0_offset(inode);
945 pc = (struct pathComponent *)ea;
947 if (*symname == '/') {
950 } while (*symname == '/');
952 pc->componentType = 1;
953 pc->lengthComponentIdent = 0;
954 pc->componentFileVersionNum = 0;
955 pc += sizeof(struct pathComponent);
956 elen += sizeof(struct pathComponent);
962 if (elen + sizeof(struct pathComponent) > eoffset)
965 pc = (struct pathComponent *)(ea + elen);
967 compstart = (char *)symname;
971 } while (*symname && *symname != '/');
973 pc->componentType = 5;
974 pc->lengthComponentIdent = 0;
975 pc->componentFileVersionNum = 0;
976 if (compstart[0] == '.') {
977 if ((symname - compstart) == 1)
978 pc->componentType = 4;
979 else if ((symname - compstart) == 2 && compstart[1] == '.')
980 pc->componentType = 3;
983 if (pc->componentType == 5) {
984 namelen = udf_put_filename(inode->i_sb, compstart, name,
985 symname - compstart);
989 if (elen + sizeof(struct pathComponent) + namelen > eoffset)
992 pc->lengthComponentIdent = namelen;
994 memcpy(pc->componentIdent, name, namelen);
997 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1002 } while (*symname == '/');
1007 inode->i_size = elen;
1008 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1009 UDF_I_LENALLOC(inode) = inode->i_size;
1010 mark_inode_dirty(inode);
1012 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err)))
1014 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1015 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1016 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1018 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1019 struct logicalVolHeaderDesc *lvhd;
1021 lvhd = (struct logicalVolHeaderDesc *)(lvid->logicalVolContentsUse);
1022 uniqueID = le64_to_cpu(lvhd->uniqueID);
1023 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1024 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1025 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1027 lvhd->uniqueID = cpu_to_le64(uniqueID);
1028 mark_buffer_dirty(bh);
1030 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1031 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
1032 mark_inode_dirty(dir);
1034 if (fibh.sbh != fibh.ebh)
1037 d_instantiate(dentry, inode);
1045 inode_dec_link_count(inode);
1050 static int udf_link(struct dentry *old_dentry, struct inode *dir,
1051 struct dentry *dentry)
1053 struct inode *inode = old_dentry->d_inode;
1054 struct udf_fileident_bh fibh;
1055 struct fileIdentDesc cfi, *fi;
1057 struct buffer_head *bh;
1060 if (inode->i_nlink >= (256 << sizeof(inode->i_nlink)) - 1) {
1065 if (!(fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err))) {
1069 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1070 cfi.icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(inode));
1071 bh = UDF_SB(inode->i_sb)->s_lvid_bh;
1073 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1074 struct logicalVolHeaderDesc *lvhd;
1076 lvhd = (struct logicalVolHeaderDesc *)(lvid->logicalVolContentsUse);
1077 uniqueID = le64_to_cpu(lvhd->uniqueID);
1078 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1079 cpu_to_le32(uniqueID & 0x00000000FFFFFFFFUL);
1080 if (!(++uniqueID & 0x00000000FFFFFFFFUL))
1082 lvhd->uniqueID = cpu_to_le64(uniqueID);
1083 mark_buffer_dirty(bh);
1085 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1086 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
1087 mark_inode_dirty(dir);
1090 if (fibh.sbh != fibh.ebh)
1094 inode->i_ctime = current_fs_time(inode->i_sb);
1095 mark_inode_dirty(inode);
1096 atomic_inc(&inode->i_count);
1097 d_instantiate(dentry, inode);
1103 /* Anybody can rename anything with this: the permission checks are left to the
1104 * higher-level routines.
1106 static int udf_rename(struct inode *old_dir, struct dentry *old_dentry,
1107 struct inode *new_dir, struct dentry *new_dentry)
1109 struct inode *old_inode = old_dentry->d_inode;
1110 struct inode *new_inode = new_dentry->d_inode;
1111 struct udf_fileident_bh ofibh, nfibh;
1112 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL, ocfi, ncfi;
1113 struct buffer_head *dir_bh = NULL;
1114 int retval = -ENOENT;
1115 kernel_lb_addr tloc;
1118 if ((ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi))) {
1119 if (ofibh.sbh != ofibh.ebh)
1123 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1124 if (!ofi || udf_get_lb_pblock(old_dir->i_sb, tloc, 0)
1125 != old_inode->i_ino)
1128 nfi = udf_find_entry(new_dir, new_dentry, &nfibh, &ncfi);
1131 if (nfibh.sbh != nfibh.ebh)
1137 if (S_ISDIR(old_inode->i_mode)) {
1138 uint32_t offset = udf_ext0_offset(old_inode);
1141 retval = -ENOTEMPTY;
1142 if (!empty_dir(new_inode))
1146 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB) {
1147 dir_fi = udf_get_fileident(UDF_I_DATA(old_inode) -
1148 (UDF_I_EFE(old_inode) ?
1149 sizeof(struct extendedFileEntry) :
1150 sizeof(struct fileEntry)),
1151 old_inode->i_sb->s_blocksize, &offset);
1153 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1156 dir_fi = udf_get_fileident(dir_bh->b_data, old_inode->i_sb->s_blocksize, &offset);
1160 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1161 if (udf_get_lb_pblock(old_inode->i_sb, tloc, 0) != old_dir->i_ino)
1165 if (!new_inode && new_dir->i_nlink >= (256 << sizeof(new_dir->i_nlink)) - 1)
1169 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi, &retval);
1175 * Like most other Unix systems, set the ctime for inodes on a
1178 old_inode->i_ctime = current_fs_time(old_inode->i_sb);
1179 mark_inode_dirty(old_inode);
1184 ncfi.fileVersionNum = ocfi.fileVersionNum;
1185 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1186 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(long_ad));
1187 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1189 /* The old fid may have moved - find it again */
1190 ofi = udf_find_entry(old_dir, old_dentry, &ofibh, &ocfi);
1191 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1194 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
1195 inode_dec_link_count(new_inode);
1197 old_dir->i_ctime = old_dir->i_mtime = current_fs_time(old_dir->i_sb);
1198 mark_inode_dirty(old_dir);
1201 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I_LOCATION(new_dir));
1202 udf_update_tag((char *)dir_fi, (sizeof(struct fileIdentDesc) +
1203 le16_to_cpu(dir_fi->lengthOfImpUse) + 3) & ~3);
1204 if (UDF_I_ALLOCTYPE(old_inode) == ICBTAG_FLAG_AD_IN_ICB) {
1205 mark_inode_dirty(old_inode);
1207 mark_buffer_dirty_inode(dir_bh, old_inode);
1209 inode_dec_link_count(old_dir);
1211 inode_dec_link_count(new_inode);
1214 mark_inode_dirty(new_dir);
1219 if (ofibh.sbh != ofibh.ebh)
1229 if (nfibh.sbh != nfibh.ebh)
1238 const struct inode_operations udf_dir_inode_operations = {
1239 .lookup = udf_lookup,
1240 .create = udf_create,
1242 .unlink = udf_unlink,
1243 .symlink = udf_symlink,
1247 .rename = udf_rename,