[CIFS] undo changes in cifs_rename_pending_delete if it errors out
[safe/jmp/linux-2.6] / fs / cifs / inode.c
index 8dbc7c9..232ab16 100644 (file)
@@ -506,6 +506,7 @@ int cifs_get_inode_info(struct inode **pinode,
        inode = *pinode;
        cifsInfo = CIFS_I(inode);
        cifsInfo->cifsAttrs = attr;
+       cifsInfo->delete_pending = pfindData->DeletePending ? true : false;
        cFYI(1, ("Old time %ld", cifsInfo->time));
        cifsInfo->time = jiffies;
        cFYI(1, ("New time %ld", cifsInfo->time));
@@ -729,7 +730,10 @@ cifs_set_file_info(struct inode *inode, struct iattr *attrs, int xid,
                                     &info_buf, cifs_sb->local_nls,
                                     cifs_sb->mnt_cifs_flags &
                                        CIFS_MOUNT_MAP_SPECIAL_CHR);
-               if (rc != -EOPNOTSUPP && rc != -EINVAL)
+               if (rc == 0) {
+                       cifsInode->cifsAttrs = dosattr;
+                       goto out;
+               } else if (rc != -EOPNOTSUPP && rc != -EINVAL)
                        goto out;
        }
 
@@ -752,6 +756,9 @@ cifs_set_file_info(struct inode *inode, struct iattr *attrs, int xid,
 
 set_via_filehandle:
        rc = CIFSSMBSetFileInfo(xid, pTcon, &info_buf, netfid, netpid);
+       if (!rc)
+               cifsInode->cifsAttrs = dosattr;
+
        if (open_file == NULL)
                CIFSSMBClose(xid, pTcon, netfid);
        else
@@ -760,18 +767,126 @@ out:
        return rc;
 }
 
+/*
+ * open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
+ * and rename it to a random name that hopefully won't conflict with
+ * anything else.
+ */
+static int
+cifs_rename_pending_delete(char *full_path, struct dentry *dentry, int xid)
+{
+       int oplock = 0;
+       int rc;
+       __u16 netfid;
+       struct inode *inode = dentry->d_inode;
+       struct cifsInodeInfo *cifsInode = CIFS_I(inode);
+       struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
+       struct cifsTconInfo *tcon = cifs_sb->tcon;
+       __u32 dosattr, origattr;
+       FILE_BASIC_INFO *info_buf = NULL;
+
+       rc = CIFSSMBOpen(xid, tcon, full_path, FILE_OPEN,
+                        DELETE|FILE_WRITE_ATTRIBUTES, CREATE_NOT_DIR,
+                        &netfid, &oplock, NULL, cifs_sb->local_nls,
+                        cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
+       if (rc != 0)
+               goto out;
+
+       origattr = cifsInode->cifsAttrs;
+       if (origattr == 0)
+               origattr |= ATTR_NORMAL;
+
+       dosattr = origattr & ~ATTR_READONLY;
+       if (dosattr == 0)
+               dosattr |= ATTR_NORMAL;
+       dosattr |= ATTR_HIDDEN;
+
+       /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
+       if (dosattr != origattr) {
+               info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
+               if (info_buf == NULL) {
+                       rc = -ENOMEM;
+                       goto out_close;
+               }
+               info_buf->Attributes = cpu_to_le32(dosattr);
+               rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
+                                       current->tgid);
+               /* although we would like to mark the file hidden
+                  if that fails we will still try to rename it */
+               if (rc != 0) {
+                       cifsInode->cifsAttrs = dosattr;
+               else
+                       dosattr = origattr; /* since not able to change them */
+       }
+
+       /* rename the file */
+       rc = CIFSSMBRenameOpenFile(xid, tcon, netfid, NULL, cifs_sb->local_nls,
+                                  cifs_sb->mnt_cifs_flags &
+                                           CIFS_MOUNT_MAP_SPECIAL_CHR);
+       if (rc != 0) {
+               rc = -ETXTBSY;
+               goto undo_setattr;
+       }
+
+       /* try to set DELETE_ON_CLOSE */
+       if (!cifsInode->delete_pending) {
+               rc = CIFSSMBSetFileDisposition(xid, tcon, true, netfid,
+                                              current->tgid);
+               /*
+                * some samba versions return -ENOENT when we try to set the
+                * file disposition here. Likely a samba bug, but work around
+                * it for now. This means that some cifsXXX files may hang
+                * around after they shouldn't.
+                *
+                * BB: remove this hack after more servers have the fix
+                */
+               if (rc == -ENOENT)
+                       rc = 0;
+               else if (rc != 0) {
+                       rc = -ETXTBSY;
+                       goto undo_rename;
+               }
+               cifsInode->delete_pending = true;
+       }
+
+out_close:
+       CIFSSMBClose(xid, tcon, netfid);
+out:
+       kfree(info_buf);
+       return rc;
+
+       /*
+        * reset everything back to the original state. Don't bother
+        * dealing with errors here since we can't do anything about
+        * them anyway.
+        */
+undo_rename:
+       CIFSSMBRenameOpenFile(xid, tcon, netfid, dentry->d_name.name,
+                               cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
+                                           CIFS_MOUNT_MAP_SPECIAL_CHR);
+undo_setattr:
+       if (dosattr != origattr) {
+               info_buf->Attributes = cpu_to_le32(origattr);
+               if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
+                                       current->tgid))
+                       cifsInode->cifsAttrs = origattr;
+       }
+
+       goto out_close;
+}
+
 int cifs_unlink(struct inode *dir, struct dentry *dentry)
 {
        int rc = 0;
        int xid;
        char *full_path = NULL;
        struct inode *inode = dentry->d_inode;
-       struct cifsInodeInfo *cifsInode;
+       struct cifsInodeInfo *cifsInode = CIFS_I(inode);
        struct super_block *sb = dir->i_sb;
        struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
        struct cifsTconInfo *tcon = cifs_sb->tcon;
-       struct iattr *attrs;
-       __u32 dosattr;
+       struct iattr *attrs = NULL;
+       __u32 dosattr = 0, origattr = 0;
 
        cFYI(1, ("cifs_unlink, dir=0x%p, dentry=0x%p", dir, dentry));
 
@@ -796,8 +911,10 @@ int cifs_unlink(struct inode *dir, struct dentry *dentry)
                        goto psx_del_no_retry;
        }
 
+retry_std_delete:
        rc = CIFSSMBDelFile(xid, tcon, full_path, cifs_sb->local_nls,
                        cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
+
 psx_del_no_retry:
        if (!rc) {
                if (inode)
@@ -805,25 +922,10 @@ psx_del_no_retry:
        } else if (rc == -ENOENT) {
                d_drop(dentry);
        } else if (rc == -ETXTBSY) {
-               int oplock = 0;
-               __u16 netfid;
-
-               rc = CIFSSMBOpen(xid, tcon, full_path, FILE_OPEN, DELETE,
-                                CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
-                                &netfid, &oplock, NULL, cifs_sb->local_nls,
-                                cifs_sb->mnt_cifs_flags &
-                                       CIFS_MOUNT_MAP_SPECIAL_CHR);
-               if (rc == 0) {
-                       CIFSSMBRenameOpenFile(xid, tcon, netfid, NULL,
-                                             cifs_sb->local_nls,
-                                             cifs_sb->mnt_cifs_flags &
-                                               CIFS_MOUNT_MAP_SPECIAL_CHR);
-                       CIFSSMBClose(xid, tcon, netfid);
-                       if (inode)
-                               drop_nlink(inode);
-               }
-       } else if (rc == -EACCES) {
-               /* try only if r/o attribute set in local lookup data? */
+               rc = cifs_rename_pending_delete(full_path, dentry, xid);
+               if (rc == 0)
+                       drop_nlink(inode);
+       } else if (rc == -EACCES && dosattr == 0) {
                attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
                if (attrs == NULL) {
                        rc = -ENOMEM;
@@ -831,47 +933,25 @@ psx_del_no_retry:
                }
 
                /* try to reset dos attributes */
-               cifsInode = CIFS_I(inode);
-               dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
+               origattr = cifsInode->cifsAttrs;
+               if (origattr == 0)
+                       origattr |= ATTR_NORMAL;
+               dosattr = origattr & ~ATTR_READONLY;
                if (dosattr == 0)
                        dosattr |= ATTR_NORMAL;
                dosattr |= ATTR_HIDDEN;
 
                rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
-               kfree(attrs);
                if (rc != 0)
                        goto out_reval;
-               rc = CIFSSMBDelFile(xid, tcon, full_path, cifs_sb->local_nls,
-                                   cifs_sb->mnt_cifs_flags &
-                                       CIFS_MOUNT_MAP_SPECIAL_CHR);
-               if (rc == 0) {
-                       if (inode)
-                               drop_nlink(inode);
-               } else if (rc == -ETXTBSY) {
-                       int oplock = 0;
-                       __u16 netfid;
 
-                       rc = CIFSSMBOpen(xid, tcon, full_path,
-                                        FILE_OPEN, DELETE,
-                                        CREATE_NOT_DIR |
-                                        CREATE_DELETE_ON_CLOSE,
-                                        &netfid, &oplock, NULL,
-                                        cifs_sb->local_nls,
-                                        cifs_sb->mnt_cifs_flags &
-                                           CIFS_MOUNT_MAP_SPECIAL_CHR);
-                       if (rc == 0) {
-                               CIFSSMBRenameOpenFile(xid, tcon,
-                                       netfid, NULL,
-                                       cifs_sb->local_nls,
-                                       cifs_sb->mnt_cifs_flags &
-                                           CIFS_MOUNT_MAP_SPECIAL_CHR);
-                               CIFSSMBClose(xid, tcon, netfid);
-                               if (inode)
-                                       drop_nlink(inode);
-                       }
-               /* BB if rc = -ETXTBUSY goto the rename logic BB */
-               }
+               goto retry_std_delete;
        }
+
+       /* undo the setattr if we errored out and it's needed */
+       if (rc != 0 && dosattr != 0)
+               cifs_set_file_info(inode, attrs, xid, full_path, origattr);
+
 out_reval:
        if (inode) {
                cifsInode = CIFS_I(inode);
@@ -881,9 +961,10 @@ out_reval:
        }
        dir->i_ctime = dir->i_mtime = current_fs_time(sb);
        cifsInode = CIFS_I(dir);
-       cifsInode->time = 0;    /* force revalidate of dir as well */
+       CIFS_I(dir)->time = 0;  /* force revalidate of dir as well */
 
        kfree(full_path);
+       kfree(attrs);
        FreeXid(xid);
        return rc;
 }
@@ -928,7 +1009,7 @@ static void posix_fill_in_inode(struct inode *tmp_inode,
 
 int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
 {
-       int rc = 0;
+       int rc = 0, tmprc;
        int xid;
        struct cifs_sb_info *cifs_sb;
        struct cifsTconInfo *pTcon;
@@ -990,6 +1071,7 @@ int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
                                kfree(pInfo);
                                goto mkdir_get_info;
                        }
+
                        /* Is an i_ino of zero legal? */
                        /* Are there sanity checks we can use to ensure that
                           the server is really filling in that field? */
@@ -1078,12 +1160,20 @@ mkdir_get_info:
                        if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
                            (mode & S_IWUGO) == 0) {
                                FILE_BASIC_INFO pInfo;
+                               struct cifsInodeInfo *cifsInode;
+                               u32 dosattrs;
+
                                memset(&pInfo, 0, sizeof(pInfo));
-                               pInfo.Attributes = cpu_to_le32(ATTR_READONLY);
-                               CIFSSMBSetPathInfo(xid, pTcon, full_path,
-                                               &pInfo, cifs_sb->local_nls,
+                               cifsInode = CIFS_I(newinode);
+                               dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
+                               pInfo.Attributes = cpu_to_le32(dosattrs);
+                               tmprc = CIFSSMBSetPathInfo(xid, pTcon,
+                                               full_path, &pInfo,
+                                               cifs_sb->local_nls,
                                                cifs_sb->mnt_cifs_flags &
                                                CIFS_MOUNT_MAP_SPECIAL_CHR);
+                               if (tmprc == 0)
+                                       cifsInode->cifsAttrs = dosattrs;
                        }
                        if (direntry->d_inode) {
                                if (cifs_sb->mnt_cifs_flags &
@@ -1155,117 +1245,141 @@ int cifs_rmdir(struct inode *inode, struct dentry *direntry)
        return rc;
 }
 
+static int
+cifs_do_rename(int xid, struct dentry *from_dentry, const char *fromPath,
+               struct dentry *to_dentry, const char *toPath)
+{
+       struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
+       struct cifsTconInfo *pTcon = cifs_sb->tcon;
+       __u16 srcfid;
+       int oplock, rc;
+
+       /* try path-based rename first */
+       rc = CIFSSMBRename(xid, pTcon, fromPath, toPath, cifs_sb->local_nls,
+                          cifs_sb->mnt_cifs_flags &
+                               CIFS_MOUNT_MAP_SPECIAL_CHR);
+
+       /*
+        * don't bother with rename by filehandle unless file is busy and
+        * source Note that cross directory moves do not work with
+        * rename by filehandle to various Windows servers.
+        */
+       if (rc == 0 || rc != -ETXTBSY)
+               return rc;
+
+       /* open the file to be renamed -- we need DELETE perms */
+       rc = CIFSSMBOpen(xid, pTcon, fromPath, FILE_OPEN, DELETE,
+                        CREATE_NOT_DIR, &srcfid, &oplock, NULL,
+                        cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
+                               CIFS_MOUNT_MAP_SPECIAL_CHR);
+
+       if (rc == 0) {
+               rc = CIFSSMBRenameOpenFile(xid, pTcon, srcfid,
+                               (const char *) to_dentry->d_name.name,
+                               cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
+                                       CIFS_MOUNT_MAP_SPECIAL_CHR);
+
+               CIFSSMBClose(xid, pTcon, srcfid);
+       }
+
+       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;
+       char *fromName = NULL;
+       char *toName = NULL;
        struct cifs_sb_info *cifs_sb_source;
        struct cifs_sb_info *cifs_sb_target;
        struct cifsTconInfo *pTcon;
+       FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
+       FILE_UNIX_BASIC_INFO *info_buf_target;
        int xid;
-       int rc = 0;
-
-       xid = GetXid();
+       int rc;
 
        cifs_sb_target = CIFS_SB(target_inode->i_sb);
        cifs_sb_source = CIFS_SB(source_inode->i_sb);
        pTcon = cifs_sb_source->tcon;
 
+       xid = GetXid();
+
+       /*
+        * BB: this might be allowed if same server, but different share.
+        * Consider adding support for this
+        */
        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 */
+               rc = -EXDEV;
+               goto cifs_rename_exit;
        }
 
-       /* we already  have the rename sem so we do not need to grab it again
-          here to protect the path integrity */
+       /*
+        * 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);
+       if (fromName == NULL) {
+               rc = -ENOMEM;
+               goto cifs_rename_exit;
+       }
+
        toName = build_path_from_dentry(target_direntry);
-       if ((fromName == NULL) || (toName == NULL)) {
+       if (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);
+       rc = cifs_do_rename(xid, source_direntry, fromName,
+                           target_direntry, toName);
+
        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) {
+               if (pTcon->unix_ext) {
+                       /*
+                        * Are src and dst hardlinks of same inode? We can
+                        * only tell with unix extensions enabled
+                        */
+                       info_buf_source =
+                               kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
+                                               GFP_KERNEL);
+                       if (info_buf_source == NULL)
+                               goto unlink_target;
+
                        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 &
+                       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,
+                       if (rc != 0)
+                               goto unlink_target;
+
+                       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 = 0;
-               __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,
-                                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);
-               }
+
+                       if (rc == 0 && (info_buf_source->UniqueId ==
+                                       info_buf_target->UniqueId))
+                               /* same file, POSIX says that this is a noop */
+                               goto cifs_rename_exit;
+               } /* else ... BB we could add the same check for Windows by
+                    checking the UniqueId via FILE_INTERNAL_INFO */
+unlink_target:
+               /*
+                * we either can not tell the files are hardlinked (as with
+                * Windows servers) or files are not hardlinked. Delete the
+                * target manually before renaming to follow POSIX rather than
+                * Windows semantics
+                */
+               cifs_unlink(target_inode, target_direntry);
+               rc = cifs_do_rename(xid, source_direntry, fromName,
+                                   target_direntry, toName);
        }
 
 cifs_rename_exit:
+       kfree(info_buf_source);
        kfree(fromName);
        kfree(toName);
        FreeXid(xid);