int cifs_rename(struct inode *source_inode, struct dentry *source_direntry, struct inode *target_inode, struct dentry *target_direntry) { char *fromName; char *toName; struct cifs_sb_info *cifs_sb_source; struct cifs_sb_info *cifs_sb_target; struct cifsTconInfo *pTcon; int xid; int rc = 0; xid = GetXid(); cifs_sb_target = CIFS_SB(target_inode->i_sb); cifs_sb_source = CIFS_SB(source_inode->i_sb); pTcon = cifs_sb_source->tcon; if (pTcon != cifs_sb_target->tcon) { FreeXid(xid); return -EXDEV; /* BB actually could be allowed if same server, but different share. Might eventually add support for this */ } /* we already have the rename sem so we do not need to grab it again here to protect the path integrity */ fromName = build_path_from_dentry(source_direntry); toName = build_path_from_dentry(target_direntry); if((fromName == NULL) || (toName == NULL)) { rc = -ENOMEM; goto cifs_rename_exit; } rc = CIFSSMBRename(xid, pTcon, fromName, toName, cifs_sb_source->local_nls); if(rc == -EEXIST) { /* check if they are the same file because rename of hardlinked files is a noop */ FILE_UNIX_BASIC_INFO * info_buf_source; FILE_UNIX_BASIC_INFO * info_buf_target; info_buf_source = kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),GFP_KERNEL); if(info_buf_source != NULL) { info_buf_target = info_buf_source+1; rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName, info_buf_source, cifs_sb_source->local_nls); if(rc == 0) { rc = CIFSSMBUnixQPathInfo(xid,pTcon,toName, info_buf_target, cifs_sb_target->local_nls); } if((rc == 0) && (info_buf_source->UniqueId == info_buf_target->UniqueId)) { /* do not rename since the files are hardlinked which is a noop */ } else { /* we either can not tell the files are hardlinked (as with Windows servers) or files are not hardlinked so delete the target manually before renaming to follow POSIX rather than Windows semantics */ cifs_unlink(target_inode, target_direntry); rc = CIFSSMBRename(xid, pTcon, fromName, toName, cifs_sb_source->local_nls); } kfree(info_buf_source); } /* if we can not get memory just leave rc as EEXIST */ } if((rc == -EIO)||(rc == -EEXIST)) { int oplock = FALSE; __u16 netfid; rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ, CREATE_NOT_DIR, &netfid, &oplock, NULL, cifs_sb_source->local_nls); if(rc==0) { CIFSSMBRenameOpenFile(xid,pTcon,netfid, toName, cifs_sb_source->local_nls); CIFSSMBClose(xid, pTcon, netfid); } } cifs_rename_exit: if (fromName) kfree(fromName); if (toName) kfree(toName); FreeXid(xid); return rc; }
int cifs_rename(struct inode *source_inode, struct dentry *source_direntry, struct inode *target_inode, struct dentry *target_direntry) { char *fromName; char *toName; struct cifs_sb_info *cifs_sb_source; struct cifs_sb_info *cifs_sb_target; struct cifsTconInfo *pTcon; int xid; int rc = 0; xid = GetXid(); cifs_sb_target = CIFS_SB(target_inode->i_sb); cifs_sb_source = CIFS_SB(source_inode->i_sb); pTcon = cifs_sb_source->tcon; if (pTcon != cifs_sb_target->tcon) { FreeXid(xid); return -EXDEV; /* BB actually could be allowed if same server, but different share. Might eventually add support for this */ } /* we already have the rename sem so we do not need to grab it again here to protect the path integrity */ fromName = build_path_from_dentry(source_direntry); toName = build_path_from_dentry(target_direntry); if ((fromName == NULL) || (toName == NULL)) { rc = -ENOMEM; goto cifs_rename_exit; } rc = CIFSSMBRename(xid, pTcon, fromName, toName, cifs_sb_source->local_nls, cifs_sb_source->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); if (rc == -EEXIST) { /* check if they are the same file because rename of hardlinked files is a noop */ FILE_UNIX_BASIC_INFO *info_buf_source; FILE_UNIX_BASIC_INFO *info_buf_target; info_buf_source = kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); if (info_buf_source != NULL) { info_buf_target = info_buf_source + 1; if (pTcon->unix_ext) rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName, info_buf_source, cifs_sb_source->local_nls, cifs_sb_source->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); /* else rc is still EEXIST so will fall through to unlink the target and retry rename */ if (rc == 0) { rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName, info_buf_target, cifs_sb_target->local_nls, /* remap based on source sb */ cifs_sb_source->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); } if ((rc == 0) && (info_buf_source->UniqueId == info_buf_target->UniqueId)) { /* do not rename since the files are hardlinked which is a noop */ } else { /* we either can not tell the files are hardlinked (as with Windows servers) or files are not hardlinked so delete the target manually before renaming to follow POSIX rather than Windows semantics */ cifs_unlink(target_inode, target_direntry); rc = CIFSSMBRename(xid, pTcon, fromName, toName, cifs_sb_source->local_nls, cifs_sb_source->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); } kfree(info_buf_source); } /* if we can not get memory just leave rc as EEXIST */ } if (rc) cFYI(1, ("rename rc %d", rc)); if ((rc == -EIO) || (rc == -EEXIST)) { int oplock = FALSE; __u16 netfid; /* BB FIXME Is Generic Read correct for rename? */ /* if renaming directory - we should not say CREATE_NOT_DIR, need to test renaming open directory, also GENERIC_READ might not right be right access to request */ rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ, FILE_SHARE_ALL, CREATE_NOT_DIR, &netfid, &oplock, NULL, cifs_sb_source->local_nls, cifs_sb_source->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); if (rc == 0) { rc = CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName, cifs_sb_source->local_nls, cifs_sb_source->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); CIFSSMBClose(xid, pTcon, netfid); } } cifs_rename_exit: kfree(fromName); kfree(toName); FreeXid(xid); return rc; }