26 #include <linux/string.h>
27 #include <linux/errno.h>
29 #include <linux/slab.h>
31 #include <linux/sched.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);
63 if (adinicb || (offset + liu < 0)) {
65 }
else if (offset >= 0) {
66 memcpy(fibh->
ebh->b_data + offset, impuse, liu);
69 memcpy(fibh->
ebh->b_data, impuse - offset,
77 if (adinicb || (offset + lfi < 0)) {
79 }
else if (offset >= 0) {
80 memcpy(fibh->
ebh->b_data + offset, fileident, lfi);
84 memcpy(fibh->
ebh->b_data, fileident - offset,
91 if (adinicb || (offset + padlen < 0)) {
93 }
else if (offset >= 0) {
94 memset(fibh->
ebh->b_data + offset, 0x00, padlen);
97 memset(fibh->
ebh->b_data, 0x00, padlen + offset);
103 if (fibh->
sbh == fibh->
ebh) {
105 crclen +
sizeof(
struct tag) -
111 crclen +
sizeof(
struct tag) -
133 mark_inode_dirty(inode);
135 if (fibh->
sbh != fibh->
ebh)
151 unsigned char *nameptr;
160 int isdotdot = child->len == 2 &&
161 child->
name[0] ==
'.' && child->
name[1] ==
'.';
163 size = udf_ext0_offset(dir) + dir->
i_size;
164 f_pos = udf_ext0_offset(dir);
172 block = udf_get_lb_pblock(dir->
i_sb, &eloc, offset);
173 if ((++offset << dir->
i_sb->s_blocksize_bits) < elen) {
190 while (f_pos < size) {
199 if (fibh->
sbh == fibh->
ebh) {
214 memcpy(nameptr + lfi - poffset,
215 fibh->
ebh->b_data, poffset);
237 if (flen && udf_match(flen, fname, child->len, child->
name))
243 if (fibh->
sbh != fibh->
ebh)
268 .partitionReferenceNum =
279 if (udf_find_entry(dir, &dentry->
d_name, &fibh, &cfi)) {
286 loc = lelb_to_cpu(cfi.
icb.extLocation);
296 static struct fileIdentDesc *udf_add_entry(
struct inode *dir,
297 struct dentry *dentry,
306 loff_t size = udf_ext0_offset(dir) + dir->
i_size;
325 if (!dentry->
d_name.len) {
341 f_pos = udf_ext0_offset(dir);
348 block = udf_get_lb_pblock(dir->
i_sb,
349 &dinfo->i_location, 0);
353 block = udf_get_lb_pblock(dir->
i_sb, &eloc, offset);
354 if ((++offset << dir->
i_sb->s_blocksize_bits) < elen) {
368 block = dinfo->i_location.logicalBlockNum;
371 while (f_pos < size) {
385 liu + lfi + 3) & ~3) == nfidlen) {
409 fibh->
soffset -= udf_ext0_offset(dir);
410 fibh->
eoffset -= udf_ext0_offset(dir);
411 f_pos -= udf_ext0_offset(dir);
412 if (fibh->
sbh != fibh->
ebh)
419 epos.
block = dinfo->i_location;
420 epos.
offset = udf_file_entry_alloc_offset(dir);
429 if (fibh->
sbh != fibh->
ebh) {
435 block = dinfo->i_location.logicalBlockNum;
437 (dinfo->i_ext.i_data +
439 udf_ext0_offset(dir) +
442 block = eloc.logicalBlockNum +
444 dir->
i_sb->s_blocksize_bits);
456 dinfo->i_lenExtents = (dinfo->i_lenExtents + sb->
s_blocksize
461 if (fibh->
sbh != fibh->
ebh) {
466 block = eloc.logicalBlockNum + ((elen - 1) >>
467 dir->
i_sb->s_blocksize_bits);
469 f_pos >> dir->
i_sb->s_blocksize_bits, 1, err);
475 epos.
block = dinfo->i_location;
476 epos.
offset = udf_file_entry_alloc_offset(dir);
483 block = eloc.logicalBlockNum + ((elen - 1) >>
484 dir->
i_sb->s_blocksize_bits);
496 if (UDF_SB(sb)->s_udfrev >= 0x0200)
508 dinfo->i_lenAlloc += nfidlen;
514 elen -= dinfo->i_lenExtents - dir->
i_size;
520 dinfo->i_lenExtents = dir->
i_size;
523 mark_inode_dirty(dir);
532 if (fibh->
sbh != fibh->
ebh)
541 static int udf_delete_entry(
struct inode *inode,
struct fileIdentDesc *fi,
553 static int udf_create(
struct inode *dir,
struct dentry *dentry,
umode_t mode,
567 iinfo = UDF_I(inode);
574 mark_inode_dirty(inode);
576 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
578 inode_dec_link_count(inode);
588 mark_inode_dirty(dir);
597 static int udf_mknod(
struct inode *dir,
struct dentry *dentry,
umode_t mode,
606 if (!old_valid_dev(rdev))
614 iinfo = UDF_I(inode);
616 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
618 inode_dec_link_count(inode);
628 mark_inode_dirty(dir);
629 mark_inode_dirty(inode);
641 static int udf_mkdir(
struct inode *dir,
struct dentry *dentry,
umode_t mode)
655 iinfo = UDF_I(inode);
658 fi = udf_add_entry(inode,
NULL, &fibh, &cfi, &err);
660 inode_dec_link_count(inode);
673 mark_inode_dirty(inode);
675 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
678 mark_inode_dirty(inode);
689 mark_inode_dirty(dir);
700 static int empty_dir(
struct inode *dir)
705 loff_t size = udf_ext0_offset(dir) + dir->
i_size;
713 f_pos = udf_ext0_offset(dir);
719 &epos, &eloc, &elen, &offset) ==
721 block = udf_get_lb_pblock(dir->
i_sb, &eloc, offset);
722 if ((++offset << dir->
i_sb->s_blocksize_bits) < elen) {
740 while (f_pos < size) {
769 static int udf_rmdir(
struct inode *dir,
struct dentry *dentry)
772 struct inode *inode = dentry->
d_inode;
778 fi = udf_find_entry(dir, &dentry->
d_name, &fibh, &cfi);
783 tloc = lelb_to_cpu(cfi.
icb.extLocation);
784 if (udf_get_lb_pblock(dir->
i_sb, &tloc, 0) != inode->
i_ino)
787 if (!empty_dir(inode))
789 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
793 udf_warn(inode->
i_sb,
"empty directory has nlink != 2 (%d)\n",
797 inode_dec_link_count(dir);
800 mark_inode_dirty(dir);
811 static int udf_unlink(
struct inode *dir,
struct dentry *dentry)
814 struct inode *inode = dentry->
d_inode;
821 fi = udf_find_entry(dir, &dentry->
d_name, &fibh, &cfi);
826 tloc = lelb_to_cpu(cfi.
icb.extLocation);
827 if (udf_get_lb_pblock(dir->
i_sb, &tloc, 0) != inode->
i_ino)
831 udf_debug(
"Deleting nonexistent file (%lu), %d\n",
835 retval = udf_delete_entry(dir, fi, &fibh, &cfi);
839 mark_inode_dirty(dir);
840 inode_dec_link_count(inode);
853 static int udf_symlink(
struct inode *dir,
struct dentry *dentry,
858 const char *compstart;
861 int eoffset, elen = 0;
867 unsigned char *name =
NULL;
876 iinfo = UDF_I(inode);
897 epos.
offset = udf_file_entry_alloc_offset(inode);
899 eloc.logicalBlockNum =
block;
900 eloc.partitionReferenceNum =
911 lock_buffer(epos.
bh);
912 memset(epos.
bh->b_data, 0x00, bsize);
913 set_buffer_uptodate(epos.
bh);
916 ea = epos.
bh->b_data + udf_ext0_offset(inode);
920 eoffset = sb->
s_blocksize - udf_ext0_offset(inode);
923 if (*symname ==
'/') {
926 }
while (*symname ==
'/');
946 }
while (*symname && *symname !=
'/');
951 if (compstart[0] ==
'.') {
952 if ((symname - compstart) == 1)
954 else if ((symname - compstart) == 2 &&
961 symname - compstart);
979 }
while (*symname ==
'/');
989 mark_inode_dirty(inode);
991 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
996 if (UDF_SB(inode->
i_sb)->s_lvid_bh) {
1002 mark_inode_dirty(dir);
1004 if (fibh.
sbh != fibh.
ebh)
1016 inode_dec_link_count(inode);
1021 static int udf_link(
struct dentry *old_dentry,
struct inode *dir,
1022 struct dentry *dentry)
1024 struct inode *inode = old_dentry->
d_inode;
1029 fi = udf_add_entry(dir, dentry, &fibh, &cfi, &err);
1034 cfi.
icb.extLocation = cpu_to_lelb(UDF_I(inode)->i_location);
1035 if (UDF_SB(inode->
i_sb)->s_lvid_bh) {
1041 mark_inode_dirty(dir);
1043 if (fibh.
sbh != fibh.
ebh)
1048 mark_inode_dirty(inode);
1058 static int udf_rename(
struct inode *old_dir,
struct dentry *old_dentry,
1059 struct inode *new_dir,
struct dentry *new_dentry)
1061 struct inode *old_inode = old_dentry->
d_inode;
1066 struct buffer_head *dir_bh =
NULL;
1071 ofi = udf_find_entry(old_dir, &old_dentry->
d_name, &ofibh, &ocfi);
1073 if (ofibh.sbh != ofibh.ebh)
1077 tloc = lelb_to_cpu(ocfi.icb.extLocation);
1078 if (!ofi || udf_get_lb_pblock(old_dir->
i_sb, &tloc, 0)
1079 != old_inode->
i_ino)
1082 nfi = udf_find_entry(new_dir, &new_dentry->
d_name, &nfibh, &ncfi);
1085 if (nfibh.sbh != nfibh.ebh)
1092 int offset = udf_ext0_offset(old_inode);
1096 if (!empty_dir(new_inode))
1106 old_inode->
i_sb->s_blocksize, &offset);
1108 dir_bh =
udf_bread(old_inode, 0, 0, &retval);
1112 old_inode->
i_sb->s_blocksize, &offset);
1116 tloc = lelb_to_cpu(dir_fi->icb.extLocation);
1117 if (udf_get_lb_pblock(old_inode->
i_sb, &tloc, 0) !=
1122 nfi = udf_add_entry(new_dir, new_dentry, &nfibh, &ncfi,
1133 mark_inode_dirty(old_inode);
1138 ncfi.fileVersionNum = ocfi.fileVersionNum;
1139 ncfi.fileCharacteristics = ocfi.fileCharacteristics;
1144 ofi = udf_find_entry(old_dir, &old_dentry->
d_name, &ofibh, &ocfi);
1145 udf_delete_entry(old_dir, ofi, &ofibh, &ocfi);
1149 inode_dec_link_count(new_inode);
1152 mark_inode_dirty(old_dir);
1155 dir_fi->icb.extLocation = cpu_to_lelb(UDF_I(new_dir)->
i_location);
1160 mark_inode_dirty(old_inode);
1164 inode_dec_link_count(old_dir);
1166 inode_dec_link_count(new_inode);
1169 mark_inode_dirty(new_dir);
1174 if (ofibh.sbh != ofibh.ebh)
1184 if (nfibh.sbh != nfibh.ebh)
1192 static struct dentry *udf_get_parent(
struct dentry *child)
1195 struct inode *inode =
NULL;
1200 if (!udf_find_entry(child->
d_inode, &dotdot, &fibh, &cfi))
1203 if (fibh.
sbh != fibh.
ebh)
1207 tloc = lelb_to_cpu(cfi.
icb.extLocation);
1218 static struct dentry *udf_nfs_get_inode(
struct super_block *sb,
u32 block,
1221 struct inode *
inode;
1228 loc.partitionReferenceNum = partref;
1241 static struct dentry *udf_fh_to_dentry(
struct super_block *sb,
1242 struct fid *
fid,
int fh_len,
int fh_type)
1244 if ((fh_len != 3 && fh_len != 5) ||
1249 return udf_nfs_get_inode(sb, fid->
udf.block, fid->
udf.partref,
1250 fid->
udf.generation);
1253 static struct dentry *udf_fh_to_parent(
struct super_block *sb,
1254 struct fid *fid,
int fh_len,
int fh_type)
1259 return udf_nfs_get_inode(sb, fid->
udf.parent_block,
1260 fid->
udf.parent_partref,
1261 fid->
udf.parent_generation);
1263 static int udf_encode_fh(
struct inode *inode,
__u32 *fh,
int *lenp,
1264 struct inode *parent)
1268 struct fid *fid = (
struct fid *)fh;
1271 if (parent && (len < 5)) {
1274 }
else if (len < 3) {
1282 fid->
udf.parent_partref = 0;
1286 location = UDF_I(parent)->i_location;
1298 .encode_fh = udf_encode_fh,
1299 .fh_to_dentry = udf_fh_to_dentry,
1300 .fh_to_parent = udf_fh_to_parent,
1301 .get_parent = udf_get_parent,
1305 .lookup = udf_lookup,
1306 .create = udf_create,
1308 .unlink = udf_unlink,
1309 .symlink = udf_symlink,
1313 .rename = udf_rename,