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/sched.h>
31 #include <linux/crc-itu-t.h>
32 #include <linux/exportfs.h>
33 #include <linux/iversion.h>
35 static inline int udf_match(int len1, const unsigned char *name1, int len2,
36 const unsigned char *name2)
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(struct 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(sfi->impUse + liu, fileident, lfi);
79 } else if (offset >= 0) {
80 memcpy(fibh->ebh->b_data + offset, fileident, lfi);
82 memcpy(sfi->impUse + liu, fileident, -offset);
83 memcpy(fibh->ebh->b_data, fileident - offset,
90 if (adinicb || (offset + padlen < 0)) {
91 memset(sfi->impUse + liu + lfi, 0x00, padlen);
92 } else if (offset >= 0) {
93 memset(fibh->ebh->b_data + offset, 0x00, padlen);
95 memset(sfi->impUse + liu + lfi, 0x00, -offset);
96 memset(fibh->ebh->b_data, 0x00, padlen + offset);
99 crc = crc_itu_t(0, (uint8_t *)cfi + sizeof(struct tag),
100 sizeof(struct fileIdentDesc) - sizeof(struct tag));
102 if (fibh->sbh == fibh->ebh) {
103 crc = crc_itu_t(crc, (uint8_t *)sfi->impUse,
104 crclen + sizeof(struct tag) -
105 sizeof(struct fileIdentDesc));
106 } else if (sizeof(struct fileIdentDesc) >= -fibh->soffset) {
107 crc = crc_itu_t(crc, fibh->ebh->b_data +
108 sizeof(struct fileIdentDesc) +
110 crclen + sizeof(struct tag) -
111 sizeof(struct fileIdentDesc));
113 crc = crc_itu_t(crc, (uint8_t *)sfi->impUse,
114 -fibh->soffset - sizeof(struct fileIdentDesc));
115 crc = crc_itu_t(crc, fibh->ebh->b_data, fibh->eoffset);
118 cfi->descTag.descCRC = cpu_to_le16(crc);
119 cfi->descTag.descCRCLength = cpu_to_le16(crclen);
120 cfi->descTag.tagChecksum = udf_tag_checksum(&cfi->descTag);
122 if (adinicb || (sizeof(struct fileIdentDesc) <= -fibh->soffset)) {
123 memcpy((uint8_t *)sfi, (uint8_t *)cfi,
124 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);
138 inode_inc_iversion(inode);
144 * udf_find_entry - find entry in given directory.
146 * @dir: directory inode to search in
147 * @child: qstr of the name
148 * @fibh: buffer head / inode with file identifier descriptor we found
149 * @cfi: found file identifier descriptor with given name
151 * This function searches in the directory @dir for a file name @child. When
152 * found, @fibh points to the buffer head(s) (bh is NULL for in ICB
153 * directories) containing the file identifier descriptor (FID). In that case
154 * the function returns pointer to the FID in the buffer or inode - but note
155 * that FID may be split among two buffers (blocks) so accessing it via that
156 * pointer isn't easily possible. This pointer can be used only as an iterator
157 * for other directory manipulation functions. For inspection of the FID @cfi
158 * can be used - the found FID is copied there.
160 * Returns pointer to FID, NULL when nothing found, or error code.
162 static struct fileIdentDesc *udf_find_entry(struct inode *dir,
163 const struct qstr *child,
164 struct udf_fileident_bh *fibh,
165 struct fileIdentDesc *cfi)
167 struct fileIdentDesc *fi = NULL;
171 unsigned char *fname = NULL, *copy_name = NULL;
172 unsigned char *nameptr;
176 struct kernel_lb_addr eloc;
179 struct extent_position epos = {};
180 struct udf_inode_info *dinfo = UDF_I(dir);
181 int isdotdot = child->len == 2 &&
182 child->name[0] == '.' && child->name[1] == '.';
183 struct super_block *sb = dir->i_sb;
185 size = udf_ext0_offset(dir) + dir->i_size;
186 f_pos = udf_ext0_offset(dir);
188 fibh->sbh = fibh->ebh = NULL;
189 fibh->soffset = fibh->eoffset = f_pos & (sb->s_blocksize - 1);
190 if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
191 if (inode_bmap(dir, f_pos >> sb->s_blocksize_bits, &epos,
192 &eloc, &elen, &offset) != (EXT_RECORDED_ALLOCATED >> 30)) {
197 block = udf_get_lb_pblock(sb, &eloc, offset);
198 if ((++offset << sb->s_blocksize_bits) < elen) {
199 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
200 epos.offset -= sizeof(struct short_ad);
201 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
202 epos.offset -= sizeof(struct long_ad);
206 fibh->sbh = fibh->ebh = udf_tread(sb, block);
213 fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
215 fi = ERR_PTR(-ENOMEM);
219 while (f_pos < size) {
220 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
227 liu = le16_to_cpu(cfi->lengthOfImpUse);
228 lfi = cfi->lengthFileIdent;
230 if (fibh->sbh == fibh->ebh) {
231 nameptr = udf_get_fi_ident(fi);
233 int poffset; /* Unpaded ending offset */
235 poffset = fibh->soffset + sizeof(struct fileIdentDesc) +
239 nameptr = (uint8_t *)(fibh->ebh->b_data +
243 copy_name = kmalloc(UDF_NAME_LEN_CS0,
246 fi = ERR_PTR(-ENOMEM);
251 memcpy(nameptr, udf_get_fi_ident(fi),
253 memcpy(nameptr + lfi - poffset,
254 fibh->ebh->b_data, poffset);
258 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
259 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
263 if ((cfi->fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
264 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
268 if ((cfi->fileCharacteristics & FID_FILE_CHAR_PARENT) &&
275 flen = udf_get_filename(sb, nameptr, lfi, fname, UDF_NAME_LEN);
281 if (udf_match(flen, fname, child->len, child->name))
287 if (fibh->sbh != fibh->ebh)
298 static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry,
301 struct inode *inode = NULL;
302 struct fileIdentDesc cfi;
303 struct udf_fileident_bh fibh;
304 struct fileIdentDesc *fi;
306 if (dentry->d_name.len > UDF_NAME_LEN)
307 return ERR_PTR(-ENAMETOOLONG);
309 fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
314 struct kernel_lb_addr loc;
316 if (fibh.sbh != fibh.ebh)
320 loc = lelb_to_cpu(cfi.icb.extLocation);
321 inode = udf_iget(dir->i_sb, &loc);
323 return ERR_CAST(inode);
326 return d_splice_alias(inode, dentry);
329 static struct fileIdentDesc *udf_add_entry(struct inode *dir,
330 struct dentry *dentry,
331 struct udf_fileident_bh *fibh,
332 struct fileIdentDesc *cfi, int *err)
334 struct super_block *sb = dir->i_sb;
335 struct fileIdentDesc *fi = NULL;
336 unsigned char *name = NULL;
339 loff_t size = udf_ext0_offset(dir) + dir->i_size;
342 struct kernel_lb_addr eloc;
345 struct extent_position epos = {};
346 struct udf_inode_info *dinfo;
348 fibh->sbh = fibh->ebh = NULL;
349 name = kmalloc(UDF_NAME_LEN_CS0, GFP_NOFS);
356 if (!dentry->d_name.len) {
360 namelen = udf_put_filename(sb, dentry->d_name.name,
362 name, UDF_NAME_LEN_CS0);
364 *err = -ENAMETOOLONG;
371 nfidlen = ALIGN(sizeof(struct fileIdentDesc) + namelen, UDF_NAME_PAD);
373 f_pos = udf_ext0_offset(dir);
375 fibh->soffset = fibh->eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
377 if (dinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
378 if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits, &epos,
379 &eloc, &elen, &offset) != (EXT_RECORDED_ALLOCATED >> 30)) {
380 block = udf_get_lb_pblock(dir->i_sb,
381 &dinfo->i_location, 0);
382 fibh->soffset = fibh->eoffset = sb->s_blocksize;
385 block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
386 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
387 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
388 epos.offset -= sizeof(struct short_ad);
389 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
390 epos.offset -= sizeof(struct long_ad);
394 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
400 block = dinfo->i_location.logicalBlockNum;
403 while (f_pos < size) {
404 fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &epos, &eloc,
412 if ((cfi->fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
413 if (udf_dir_entry_len(cfi) == nfidlen) {
414 cfi->descTag.tagSerialNum = cpu_to_le16(1);
415 cfi->fileVersionNum = cpu_to_le16(1);
416 cfi->fileCharacteristics = 0;
417 cfi->lengthFileIdent = namelen;
418 cfi->lengthOfImpUse = cpu_to_le16(0);
419 if (!udf_write_fi(dir, cfi, fi, fibh, NULL,
433 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
434 sb->s_blocksize - fibh->eoffset < nfidlen) {
437 fibh->soffset -= udf_ext0_offset(dir);
438 fibh->eoffset -= udf_ext0_offset(dir);
439 f_pos -= udf_ext0_offset(dir);
440 if (fibh->sbh != fibh->ebh)
443 fibh->sbh = fibh->ebh =
444 udf_expand_dir_adinicb(dir, &block, err);
447 epos.block = dinfo->i_location;
448 epos.offset = udf_file_entry_alloc_offset(dir);
449 /* Load extent udf_expand_dir_adinicb() has created */
450 udf_current_aext(dir, &epos, &eloc, &elen, 1);
453 /* Entry fits into current block? */
454 if (sb->s_blocksize - fibh->eoffset >= nfidlen) {
455 fibh->soffset = fibh->eoffset;
456 fibh->eoffset += nfidlen;
457 if (fibh->sbh != fibh->ebh) {
459 fibh->sbh = fibh->ebh;
462 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
463 block = dinfo->i_location.logicalBlockNum;
464 fi = (struct fileIdentDesc *)
465 (dinfo->i_data + fibh->soffset -
466 udf_ext0_offset(dir) +
469 block = eloc.logicalBlockNum +
471 dir->i_sb->s_blocksize_bits);
472 fi = (struct fileIdentDesc *)
473 (fibh->sbh->b_data + fibh->soffset);
476 /* Round up last extent in the file */
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(struct short_ad);
480 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
481 epos.offset -= sizeof(struct long_ad);
482 udf_write_aext(dir, &epos, &eloc, elen, 1);
483 dinfo->i_lenExtents = (dinfo->i_lenExtents + sb->s_blocksize
484 - 1) & ~(sb->s_blocksize - 1);
486 fibh->soffset = fibh->eoffset - sb->s_blocksize;
487 fibh->eoffset += nfidlen - sb->s_blocksize;
488 if (fibh->sbh != fibh->ebh) {
490 fibh->sbh = fibh->ebh;
493 block = eloc.logicalBlockNum + ((elen - 1) >>
494 dir->i_sb->s_blocksize_bits);
495 fibh->ebh = udf_bread(dir,
496 f_pos >> dir->i_sb->s_blocksize_bits, 1, err);
499 /* Extents could have been merged, invalidate our position */
502 epos.block = dinfo->i_location;
503 epos.offset = udf_file_entry_alloc_offset(dir);
505 if (!fibh->soffset) {
506 /* Find the freshly allocated block */
507 while (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
508 (EXT_RECORDED_ALLOCATED >> 30))
510 block = eloc.logicalBlockNum + ((elen - 1) >>
511 dir->i_sb->s_blocksize_bits);
513 fibh->sbh = fibh->ebh;
514 fi = (struct fileIdentDesc *)(fibh->sbh->b_data);
516 fi = (struct fileIdentDesc *)
517 (fibh->sbh->b_data + sb->s_blocksize +
522 memset(cfi, 0, sizeof(struct fileIdentDesc));
523 if (UDF_SB(sb)->s_udfrev >= 0x0200)
524 udf_new_tag((char *)cfi, TAG_IDENT_FID, 3, 1, block,
527 udf_new_tag((char *)cfi, TAG_IDENT_FID, 2, 1, block,
529 cfi->fileVersionNum = cpu_to_le16(1);
530 cfi->lengthFileIdent = namelen;
531 cfi->lengthOfImpUse = cpu_to_le16(0);
532 if (!udf_write_fi(dir, cfi, fi, fibh, NULL, name)) {
533 dir->i_size += nfidlen;
534 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
535 dinfo->i_lenAlloc += nfidlen;
537 /* Find the last extent and truncate it to proper size */
538 while (udf_next_aext(dir, &epos, &eloc, &elen, 1) ==
539 (EXT_RECORDED_ALLOCATED >> 30))
541 elen -= dinfo->i_lenExtents - dir->i_size;
542 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
543 epos.offset -= sizeof(struct short_ad);
544 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
545 epos.offset -= sizeof(struct long_ad);
546 udf_write_aext(dir, &epos, &eloc, elen, 1);
547 dinfo->i_lenExtents = dir->i_size;
550 mark_inode_dirty(dir);
559 if (fibh->sbh != fibh->ebh)
568 static int udf_delete_entry(struct inode *inode, struct fileIdentDesc *fi,
569 struct udf_fileident_bh *fibh,
570 struct fileIdentDesc *cfi)
572 cfi->fileCharacteristics |= FID_FILE_CHAR_DELETED;
574 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
575 memset(&(cfi->icb), 0x00, sizeof(struct long_ad));
577 return udf_write_fi(inode, cfi, fi, fibh, NULL, NULL);
580 static int udf_add_nondir(struct dentry *dentry, struct inode *inode)
582 struct udf_inode_info *iinfo = UDF_I(inode);
583 struct inode *dir = d_inode(dentry->d_parent);
584 struct udf_fileident_bh fibh;
585 struct fileIdentDesc cfi, *fi;
588 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
590 inode_dec_link_count(inode);
591 discard_new_inode(inode);
594 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
595 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
596 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
597 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
598 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
599 dir->i_ctime = dir->i_mtime = current_time(dir);
600 mark_inode_dirty(dir);
601 if (fibh.sbh != fibh.ebh)
604 d_instantiate_new(dentry, inode);
609 static int udf_create(struct user_namespace *mnt_userns, struct inode *dir,
610 struct dentry *dentry, umode_t mode, bool excl)
612 struct inode *inode = udf_new_inode(dir, mode);
615 return PTR_ERR(inode);
617 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
618 inode->i_data.a_ops = &udf_adinicb_aops;
620 inode->i_data.a_ops = &udf_aops;
621 inode->i_op = &udf_file_inode_operations;
622 inode->i_fop = &udf_file_operations;
623 mark_inode_dirty(inode);
625 return udf_add_nondir(dentry, inode);
628 static int udf_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
629 struct file *file, umode_t mode)
631 struct inode *inode = udf_new_inode(dir, mode);
634 return PTR_ERR(inode);
636 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
637 inode->i_data.a_ops = &udf_adinicb_aops;
639 inode->i_data.a_ops = &udf_aops;
640 inode->i_op = &udf_file_inode_operations;
641 inode->i_fop = &udf_file_operations;
642 mark_inode_dirty(inode);
643 d_tmpfile(file, inode);
644 unlock_new_inode(inode);
645 return finish_open_simple(file, 0);
648 static int udf_mknod(struct user_namespace *mnt_userns, struct inode *dir,
649 struct dentry *dentry, umode_t mode, dev_t rdev)
653 if (!old_valid_dev(rdev))
656 inode = udf_new_inode(dir, mode);
658 return PTR_ERR(inode);
660 init_special_inode(inode, mode, rdev);
661 return udf_add_nondir(dentry, inode);
664 static int udf_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
665 struct dentry *dentry, umode_t mode)
668 struct udf_fileident_bh fibh;
669 struct fileIdentDesc cfi, *fi;
671 struct udf_inode_info *dinfo = UDF_I(dir);
672 struct udf_inode_info *iinfo;
674 inode = udf_new_inode(dir, S_IFDIR | mode);
676 return PTR_ERR(inode);
678 iinfo = UDF_I(inode);
679 inode->i_op = &udf_dir_inode_operations;
680 inode->i_fop = &udf_dir_operations;
681 fi = udf_add_entry(inode, NULL, &fibh, &cfi, &err);
683 inode_dec_link_count(inode);
684 discard_new_inode(inode);
688 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
689 cfi.icb.extLocation = cpu_to_lelb(dinfo->i_location);
690 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
691 cpu_to_le32(dinfo->i_unique & 0x00000000FFFFFFFFUL);
692 cfi.fileCharacteristics =
693 FID_FILE_CHAR_DIRECTORY | FID_FILE_CHAR_PARENT;
694 udf_write_fi(inode, &cfi, fi, &fibh, NULL, NULL);
696 mark_inode_dirty(inode);
698 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
701 mark_inode_dirty(inode);
702 discard_new_inode(inode);
705 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
706 cfi.icb.extLocation = cpu_to_lelb(iinfo->i_location);
707 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
708 cpu_to_le32(iinfo->i_unique & 0x00000000FFFFFFFFUL);
709 cfi.fileCharacteristics |= FID_FILE_CHAR_DIRECTORY;
710 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
712 dir->i_ctime = dir->i_mtime = current_time(dir);
713 mark_inode_dirty(dir);
714 d_instantiate_new(dentry, inode);
715 if (fibh.sbh != fibh.ebh)
724 static int empty_dir(struct inode *dir)
726 struct fileIdentDesc *fi, cfi;
727 struct udf_fileident_bh fibh;
729 loff_t size = udf_ext0_offset(dir) + dir->i_size;
731 struct kernel_lb_addr eloc;
734 struct extent_position epos = {};
735 struct udf_inode_info *dinfo = UDF_I(dir);
737 f_pos = udf_ext0_offset(dir);
738 fibh.soffset = fibh.eoffset = f_pos & (dir->i_sb->s_blocksize - 1);
740 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
741 fibh.sbh = fibh.ebh = NULL;
742 else if (inode_bmap(dir, f_pos >> dir->i_sb->s_blocksize_bits,
743 &epos, &eloc, &elen, &offset) ==
744 (EXT_RECORDED_ALLOCATED >> 30)) {
745 block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
746 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
747 if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
748 epos.offset -= sizeof(struct short_ad);
749 else if (dinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
750 epos.offset -= sizeof(struct long_ad);
754 fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block);
764 while (f_pos < size) {
765 fi = udf_fileident_read(dir, &f_pos, &fibh, &cfi, &epos, &eloc,
768 if (fibh.sbh != fibh.ebh)
775 if (cfi.lengthFileIdent &&
776 (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) == 0) {
777 if (fibh.sbh != fibh.ebh)
785 if (fibh.sbh != fibh.ebh)
793 static int udf_rmdir(struct inode *dir, struct dentry *dentry)
796 struct inode *inode = d_inode(dentry);
797 struct udf_fileident_bh fibh;
798 struct fileIdentDesc *fi, cfi;
799 struct kernel_lb_addr tloc;
802 fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
803 if (IS_ERR_OR_NULL(fi)) {
805 retval = PTR_ERR(fi);
810 tloc = lelb_to_cpu(cfi.icb.extLocation);
811 if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
814 if (!empty_dir(inode))
816 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
819 if (inode->i_nlink != 2)
820 udf_warn(inode->i_sb, "empty directory has nlink != 2 (%u)\n",
824 inode_dec_link_count(dir);
825 inode->i_ctime = dir->i_ctime = dir->i_mtime =
827 mark_inode_dirty(dir);
830 if (fibh.sbh != fibh.ebh)
838 static int udf_unlink(struct inode *dir, struct dentry *dentry)
841 struct inode *inode = d_inode(dentry);
842 struct udf_fileident_bh fibh;
843 struct fileIdentDesc *fi;
844 struct fileIdentDesc cfi;
845 struct kernel_lb_addr tloc;
848 fi = udf_find_entry(dir, &dentry->d_name, &fibh, &cfi);
850 if (IS_ERR_OR_NULL(fi)) {
852 retval = PTR_ERR(fi);
857 tloc = lelb_to_cpu(cfi.icb.extLocation);
858 if (udf_get_lb_pblock(dir->i_sb, &tloc, 0) != inode->i_ino)
861 if (!inode->i_nlink) {
862 udf_debug("Deleting nonexistent file (%lu), %u\n",
863 inode->i_ino, inode->i_nlink);
866 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
869 dir->i_ctime = dir->i_mtime = current_time(dir);
870 mark_inode_dirty(dir);
871 inode_dec_link_count(inode);
872 inode->i_ctime = dir->i_ctime;
876 if (fibh.sbh != fibh.ebh)
884 static int udf_symlink(struct user_namespace *mnt_userns, struct inode *dir,
885 struct dentry *dentry, const char *symname)
887 struct inode *inode = udf_new_inode(dir, S_IFLNK | 0777);
888 struct pathComponent *pc;
889 const char *compstart;
890 struct extent_position epos = {};
891 int eoffset, elen = 0;
895 unsigned char *name = NULL;
897 struct udf_inode_info *iinfo;
898 struct super_block *sb = dir->i_sb;
901 return PTR_ERR(inode);
903 iinfo = UDF_I(inode);
904 down_write(&iinfo->i_data_sem);
905 name = kmalloc(UDF_NAME_LEN_CS0, GFP_NOFS);
911 inode->i_data.a_ops = &udf_symlink_aops;
912 inode->i_op = &udf_symlink_inode_operations;
913 inode_nohighmem(inode);
915 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
916 struct kernel_lb_addr eloc;
919 block = udf_new_block(sb, inode,
920 iinfo->i_location.partitionReferenceNum,
921 iinfo->i_location.logicalBlockNum, &err);
924 epos.block = iinfo->i_location;
925 epos.offset = udf_file_entry_alloc_offset(inode);
927 eloc.logicalBlockNum = block;
928 eloc.partitionReferenceNum =
929 iinfo->i_location.partitionReferenceNum;
930 bsize = sb->s_blocksize;
931 iinfo->i_lenExtents = bsize;
932 udf_add_aext(inode, &epos, &eloc, bsize, 0);
935 block = udf_get_pblock(sb, block,
936 iinfo->i_location.partitionReferenceNum,
938 epos.bh = udf_tgetblk(sb, block);
939 if (unlikely(!epos.bh)) {
943 lock_buffer(epos.bh);
944 memset(epos.bh->b_data, 0x00, bsize);
945 set_buffer_uptodate(epos.bh);
946 unlock_buffer(epos.bh);
947 mark_buffer_dirty_inode(epos.bh, inode);
948 ea = epos.bh->b_data + udf_ext0_offset(inode);
950 ea = iinfo->i_data + iinfo->i_lenEAttr;
952 eoffset = sb->s_blocksize - udf_ext0_offset(inode);
953 pc = (struct pathComponent *)ea;
955 if (*symname == '/') {
958 } while (*symname == '/');
960 pc->componentType = 1;
961 pc->lengthComponentIdent = 0;
962 pc->componentFileVersionNum = 0;
963 elen += sizeof(struct pathComponent);
969 if (elen + sizeof(struct pathComponent) > eoffset)
972 pc = (struct pathComponent *)(ea + elen);
978 } while (*symname && *symname != '/');
980 pc->componentType = 5;
981 pc->lengthComponentIdent = 0;
982 pc->componentFileVersionNum = 0;
983 if (compstart[0] == '.') {
984 if ((symname - compstart) == 1)
985 pc->componentType = 4;
986 else if ((symname - compstart) == 2 &&
988 pc->componentType = 3;
991 if (pc->componentType == 5) {
992 namelen = udf_put_filename(sb, compstart,
994 name, UDF_NAME_LEN_CS0);
998 if (elen + sizeof(struct pathComponent) + namelen >
1002 pc->lengthComponentIdent = namelen;
1004 memcpy(pc->componentIdent, name, namelen);
1007 elen += sizeof(struct pathComponent) + pc->lengthComponentIdent;
1012 } while (*symname == '/');
1017 inode->i_size = elen;
1018 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1019 iinfo->i_lenAlloc = inode->i_size;
1021 udf_truncate_tail_extent(inode);
1022 mark_inode_dirty(inode);
1023 up_write(&iinfo->i_data_sem);
1025 err = udf_add_nondir(dentry, inode);
1031 up_write(&iinfo->i_data_sem);
1032 inode_dec_link_count(inode);
1033 discard_new_inode(inode);
1037 static int udf_link(struct dentry *old_dentry, struct inode *dir,
1038 struct dentry *dentry)
1040 struct inode *inode = d_inode(old_dentry);
1041 struct udf_fileident_bh fibh;
1042 struct fileIdentDesc cfi, *fi;
1045 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1049 cfi.icb.extLength = cpu_to_le32(inode->i_sb->s_blocksize);
1050 cfi.icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1051 if (UDF_SB(inode->i_sb)->s_lvid_bh) {
1052 *(__le32 *)((struct allocDescImpUse *)cfi.icb.impUse)->impUse =
1053 cpu_to_le32(lvid_get_unique_id(inode->i_sb));
1055 udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
1056 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1057 mark_inode_dirty(dir);
1059 if (fibh.sbh != fibh.ebh)
1063 inode->i_ctime = current_time(inode);
1064 mark_inode_dirty(inode);
1065 dir->i_ctime = dir->i_mtime = current_time(dir);
1066 mark_inode_dirty(dir);
1068 d_instantiate(dentry, inode);
1073 /* Anybody can rename anything with this: the permission checks are left to the
1074 * higher-level routines.
1076 static int udf_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
1077 struct dentry *old_dentry, struct inode *new_dir,
1078 struct dentry *new_dentry, unsigned int flags)
1080 struct inode *old_inode = d_inode(old_dentry);
1081 struct inode *new_inode = d_inode(new_dentry);
1082 struct udf_fileident_bh ofibh, nfibh;
1083 struct fileIdentDesc *ofi = NULL, *nfi = NULL, *dir_fi = NULL;
1084 struct fileIdentDesc ocfi, ncfi;
1085 struct buffer_head *dir_bh = NULL;
1086 int retval = -ENOENT;
1087 struct kernel_lb_addr tloc;
1088 struct udf_inode_info *old_iinfo = UDF_I(old_inode);
1090 if (flags & ~RENAME_NOREPLACE)
1093 ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
1094 if (!ofi || IS_ERR(ofi)) {
1096 retval = PTR_ERR(ofi);
1100 if (ofibh.sbh != ofibh.ebh)
1104 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1105 if (udf_get_lb_pblock(old_dir->i_sb, &tloc, 0) != old_inode->i_ino)
1108 nfi = udf_find_entry(new_dir, &new_dentry->d_name, &nfibh, &ncfi);
1110 retval = PTR_ERR(nfi);
1113 if (nfi && !new_inode) {
1114 if (nfibh.sbh != nfibh.ebh)
1119 if (S_ISDIR(old_inode->i_mode)) {
1120 int offset = udf_ext0_offset(old_inode);
1123 retval = -ENOTEMPTY;
1124 if (!empty_dir(new_inode))
1128 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1129 dir_fi = udf_get_fileident(
1132 sizeof(struct extendedFileEntry) :
1133 sizeof(struct fileEntry)),
1134 old_inode->i_sb->s_blocksize, &offset);
1136 dir_bh = udf_bread(old_inode, 0, 0, &retval);
1139 dir_fi = udf_get_fileident(dir_bh->b_data,
1140 old_inode->i_sb->s_blocksize, &offset);
1144 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1145 if (udf_get_lb_pblock(old_inode->i_sb, &tloc, 0) !=
1150 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
1157 * Like most other Unix systems, set the ctime for inodes on a
1160 old_inode->i_ctime = current_time(old_inode);
1161 mark_inode_dirty(old_inode);
1166 ncfi.fileVersionNum = ocfi.fileVersionNum;
1167 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1168 memcpy(&(ncfi.icb), &(ocfi.icb), sizeof(ocfi.icb));
1169 udf_write_fi(new_dir, &ncfi, nfi, &nfibh, NULL, NULL);
1171 /* The old fid may have moved - find it again */
1172 ofi = udf_find_entry(old_dir, &old_dentry->d_name, &ofibh, &ocfi);
1173 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1176 new_inode->i_ctime = current_time(new_inode);
1177 inode_dec_link_count(new_inode);
1179 old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
1180 new_dir->i_ctime = new_dir->i_mtime = current_time(new_dir);
1181 mark_inode_dirty(old_dir);
1182 mark_inode_dirty(new_dir);
1185 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->i_location);
1186 udf_update_tag((char *)dir_fi, udf_dir_entry_len(dir_fi));
1187 if (old_iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1188 mark_inode_dirty(old_inode);
1190 mark_buffer_dirty_inode(dir_bh, old_inode);
1192 inode_dec_link_count(old_dir);
1194 inode_dec_link_count(new_inode);
1197 mark_inode_dirty(new_dir);
1202 if (ofibh.sbh != ofibh.ebh)
1212 if (nfibh.sbh != nfibh.ebh)
1220 static struct dentry *udf_get_parent(struct dentry *child)
1222 struct kernel_lb_addr tloc;
1223 struct inode *inode = NULL;
1224 struct fileIdentDesc cfi;
1225 struct udf_fileident_bh fibh;
1227 if (!udf_find_entry(d_inode(child), &dotdot_name, &fibh, &cfi))
1228 return ERR_PTR(-EACCES);
1230 if (fibh.sbh != fibh.ebh)
1234 tloc = lelb_to_cpu(cfi.icb.extLocation);
1235 inode = udf_iget(child->d_sb, &tloc);
1237 return ERR_CAST(inode);
1239 return d_obtain_alias(inode);
1243 static struct dentry *udf_nfs_get_inode(struct super_block *sb, u32 block,
1244 u16 partref, __u32 generation)
1246 struct inode *inode;
1247 struct kernel_lb_addr loc;
1250 return ERR_PTR(-ESTALE);
1252 loc.logicalBlockNum = block;
1253 loc.partitionReferenceNum = partref;
1254 inode = udf_iget(sb, &loc);
1257 return ERR_CAST(inode);
1259 if (generation && inode->i_generation != generation) {
1261 return ERR_PTR(-ESTALE);
1263 return d_obtain_alias(inode);
1266 static struct dentry *udf_fh_to_dentry(struct super_block *sb,
1267 struct fid *fid, int fh_len, int fh_type)
1270 (fh_type != FILEID_UDF_WITH_PARENT &&
1271 fh_type != FILEID_UDF_WITHOUT_PARENT))
1274 return udf_nfs_get_inode(sb, fid->udf.block, fid->udf.partref,
1275 fid->udf.generation);
1278 static struct dentry *udf_fh_to_parent(struct super_block *sb,
1279 struct fid *fid, int fh_len, int fh_type)
1281 if (fh_len < 5 || fh_type != FILEID_UDF_WITH_PARENT)
1284 return udf_nfs_get_inode(sb, fid->udf.parent_block,
1285 fid->udf.parent_partref,
1286 fid->udf.parent_generation);
1288 static int udf_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
1289 struct inode *parent)
1292 struct kernel_lb_addr location = UDF_I(inode)->i_location;
1293 struct fid *fid = (struct fid *)fh;
1294 int type = FILEID_UDF_WITHOUT_PARENT;
1296 if (parent && (len < 5)) {
1298 return FILEID_INVALID;
1299 } else if (len < 3) {
1301 return FILEID_INVALID;
1305 fid->udf.block = location.logicalBlockNum;
1306 fid->udf.partref = location.partitionReferenceNum;
1307 fid->udf.parent_partref = 0;
1308 fid->udf.generation = inode->i_generation;
1311 location = UDF_I(parent)->i_location;
1312 fid->udf.parent_block = location.logicalBlockNum;
1313 fid->udf.parent_partref = location.partitionReferenceNum;
1314 fid->udf.parent_generation = inode->i_generation;
1316 type = FILEID_UDF_WITH_PARENT;
1322 const struct export_operations udf_export_ops = {
1323 .encode_fh = udf_encode_fh,
1324 .fh_to_dentry = udf_fh_to_dentry,
1325 .fh_to_parent = udf_fh_to_parent,
1326 .get_parent = udf_get_parent,
1329 const struct inode_operations udf_dir_inode_operations = {
1330 .lookup = udf_lookup,
1331 .create = udf_create,
1333 .unlink = udf_unlink,
1334 .symlink = udf_symlink,
1338 .rename = udf_rename,
1339 .tmpfile = udf_tmpfile,