Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[pandora-kernel.git] / fs / cifs / inode.c
index 24eb4d3..0d9d2e6 100644 (file)
@@ -30,7 +30,7 @@
 #include "cifs_fs_sb.h"
 
 
-static void cifs_set_ops(struct inode *inode)
+static void cifs_set_ops(struct inode *inode, const bool is_dfs_referral)
 {
        struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
 
@@ -57,8 +57,16 @@ static void cifs_set_ops(struct inode *inode)
                        inode->i_data.a_ops = &cifs_addr_ops;
                break;
        case S_IFDIR:
-               inode->i_op = &cifs_dir_inode_ops;
-               inode->i_fop = &cifs_dir_ops;
+#ifdef CONFIG_CIFS_DFS_UPCALL
+               if (is_dfs_referral) {
+                       inode->i_op = &cifs_dfs_referral_inode_operations;
+               } else {
+#else /* NO DFS support, treat as a directory */
+               {
+#endif
+                       inode->i_op = &cifs_dir_inode_ops;
+                       inode->i_fop = &cifs_dir_ops;
+               }
                break;
        case S_IFLNK:
                inode->i_op = &cifs_symlink_inode_ops;
@@ -153,6 +161,37 @@ static void cifs_unix_info_to_inode(struct inode *inode,
        spin_unlock(&inode->i_lock);
 }
 
+static const unsigned char *cifs_get_search_path(struct cifs_sb_info *cifs_sb,
+                                               const char *search_path)
+{
+       int tree_len;
+       int path_len;
+       int i;
+       char *tmp_path;
+       struct cifsTconInfo *pTcon = cifs_sb->tcon;
+
+       if (!(pTcon->Flags & SMB_SHARE_IS_IN_DFS))
+               return search_path;
+
+       /* use full path name for working with DFS */
+       tree_len = strnlen(pTcon->treeName, MAX_TREE_SIZE + 1);
+       path_len = strnlen(search_path, MAX_PATHCONF);
+
+       tmp_path = kmalloc(tree_len+path_len+1, GFP_KERNEL);
+       if (tmp_path == NULL)
+               return search_path;
+
+       strncpy(tmp_path, pTcon->treeName, tree_len);
+       if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
+               for (i = 0; i < tree_len; i++) {
+                       if (tmp_path[i] == '\\')
+                               tmp_path[i] = '/';
+               }
+       strncpy(tmp_path+tree_len, search_path, path_len);
+       tmp_path[tree_len+path_len] = 0;
+       return tmp_path;
+}
+
 int cifs_get_inode_info_unix(struct inode **pinode,
        const unsigned char *search_path, struct super_block *sb, int xid)
 {
@@ -161,41 +200,31 @@ int cifs_get_inode_info_unix(struct inode **pinode,
        struct cifsTconInfo *pTcon;
        struct inode *inode;
        struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
-       char *tmp_path;
+       const unsigned char *full_path;
+       bool is_dfs_referral = false;
 
        pTcon = cifs_sb->tcon;
        cFYI(1, ("Getting info on %s", search_path));
+
+       full_path = cifs_get_search_path(cifs_sb, search_path);
+
+try_again_CIFSSMBUnixQPathInfo:
        /* could have done a find first instead but this returns more info */
-       rc = CIFSSMBUnixQPathInfo(xid, pTcon, search_path, &findData,
+       rc = CIFSSMBUnixQPathInfo(xid, pTcon, full_path, &findData,
                                  cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
                                        CIFS_MOUNT_MAP_SPECIAL_CHR);
 /*     dump_mem("\nUnixQPathInfo return data", &findData,
                 sizeof(findData)); */
        if (rc) {
-               if (rc == -EREMOTE) {
-                       tmp_path =
-                           kmalloc(strnlen(pTcon->treeName,
-                                           MAX_TREE_SIZE + 1) +
-                                   strnlen(search_path, MAX_PATHCONF) + 1,
-                                   GFP_KERNEL);
-                       if (tmp_path == NULL)
-                               return -ENOMEM;
-
-                       /* have to skip first of the double backslash of
-                          UNC name */
-                       strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
-                       strncat(tmp_path, search_path, MAX_PATHCONF);
-                       rc = connect_to_dfs_path(xid, pTcon->ses,
-                                                /* treename + */ tmp_path,
-                                                cifs_sb->local_nls,
-                                                cifs_sb->mnt_cifs_flags &
-                                                   CIFS_MOUNT_MAP_SPECIAL_CHR);
-                       kfree(tmp_path);
-
-                       /* BB fix up inode etc. */
-               } else if (rc) {
-                       return rc;
+               if (rc == -EREMOTE && !is_dfs_referral) {
+                       is_dfs_referral = true;
+                       if (full_path != search_path) {
+                               kfree(full_path);
+                               full_path = search_path;
+                       }
+                       goto try_again_CIFSSMBUnixQPathInfo;
                }
+               goto cgiiu_exit;
        } else {
                struct cifsInodeInfo *cifsInfo;
                __u64 num_of_bytes = le64_to_cpu(findData.NumOfBytes);
@@ -204,8 +233,10 @@ int cifs_get_inode_info_unix(struct inode **pinode,
                /* get new inode */
                if (*pinode == NULL) {
                        *pinode = new_inode(sb);
-                       if (*pinode == NULL)
-                               return -ENOMEM;
+                       if (*pinode == NULL) {
+                               rc = -ENOMEM;
+                               goto cgiiu_exit;
+                       }
                        /* 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? */
@@ -237,8 +268,11 @@ int cifs_get_inode_info_unix(struct inode **pinode,
                        (unsigned long) inode->i_size,
                        (unsigned long long)inode->i_blocks));
 
-               cifs_set_ops(inode);
+               cifs_set_ops(inode, is_dfs_referral);
        }
+cgiiu_exit:
+       if (full_path != search_path)
+               kfree(full_path);
        return rc;
 }
 
@@ -247,7 +281,7 @@ static int decode_sfu_inode(struct inode *inode, __u64 size,
                            struct cifs_sb_info *cifs_sb, int xid)
 {
        int rc;
-       int oplock = FALSE;
+       int oplock = 0;
        __u16 netfid;
        struct cifsTconInfo *pTcon = cifs_sb->tcon;
        char buf[24];
@@ -347,15 +381,16 @@ static int get_sfu_mode(struct inode *inode,
 
 int cifs_get_inode_info(struct inode **pinode,
        const unsigned char *search_path, FILE_ALL_INFO *pfindData,
-       struct super_block *sb, int xid)
+       struct super_block *sb, int xid, const __u16 *pfid)
 {
        int rc = 0;
        struct cifsTconInfo *pTcon;
        struct inode *inode;
        struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
-       char *tmp_path;
+       const unsigned char *full_path = NULL;
        char *buf = NULL;
-       int adjustTZ = FALSE;
+       bool adjustTZ = false;
+       bool is_dfs_referral = false;
 
        pTcon = cifs_sb->tcon;
        cFYI(1, ("Getting info on %s", search_path));
@@ -373,8 +408,12 @@ int cifs_get_inode_info(struct inode **pinode,
                if (buf == NULL)
                        return -ENOMEM;
                pfindData = (FILE_ALL_INFO *)buf;
+
+               full_path = cifs_get_search_path(cifs_sb, search_path);
+
+try_again_CIFSSMBQPathInfo:
                /* could do find first instead but this returns more info */
-               rc = CIFSSMBQPathInfo(xid, pTcon, search_path, pfindData,
+               rc = CIFSSMBQPathInfo(xid, pTcon, full_path, pfindData,
                              0 /* not legacy */,
                              cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
                                CIFS_MOUNT_MAP_SPECIAL_CHR);
@@ -382,40 +421,24 @@ int cifs_get_inode_info(struct inode **pinode,
                when server claims no NT SMB support and the above call
                failed at least once - set flag in tcon or mount */
                if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
-                       rc = SMBQueryInformation(xid, pTcon, search_path,
+                       rc = SMBQueryInformation(xid, pTcon, full_path,
                                        pfindData, cifs_sb->local_nls,
                                        cifs_sb->mnt_cifs_flags &
                                          CIFS_MOUNT_MAP_SPECIAL_CHR);
-                       adjustTZ = TRUE;
+                       adjustTZ = true;
                }
        }
        /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
        if (rc) {
-               if (rc == -EREMOTE) {
-                       tmp_path =
-                           kmalloc(strnlen
-                                   (pTcon->treeName,
-                                    MAX_TREE_SIZE + 1) +
-                                   strnlen(search_path, MAX_PATHCONF) + 1,
-                                   GFP_KERNEL);
-                       if (tmp_path == NULL) {
-                               kfree(buf);
-                               return -ENOMEM;
+               if (rc == -EREMOTE && !is_dfs_referral) {
+                       is_dfs_referral = true;
+                       if (full_path != search_path) {
+                               kfree(full_path);
+                               full_path = search_path;
                        }
-
-                       strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
-                       strncat(tmp_path, search_path, MAX_PATHCONF);
-                       rc = connect_to_dfs_path(xid, pTcon->ses,
-                                                /* treename + */ tmp_path,
-                                                cifs_sb->local_nls,
-                                                cifs_sb->mnt_cifs_flags &
-                                                  CIFS_MOUNT_MAP_SPECIAL_CHR);
-                       kfree(tmp_path);
-                       /* BB fix up inode etc. */
-               } else if (rc) {
-                       kfree(buf);
-                       return rc;
+                       goto try_again_CIFSSMBQPathInfo;
                }
+               goto cgii_exit;
        } else {
                struct cifsInodeInfo *cifsInfo;
                __u32 attr = le32_to_cpu(pfindData->Attributes);
@@ -424,8 +447,8 @@ int cifs_get_inode_info(struct inode **pinode,
                if (*pinode == NULL) {
                        *pinode = new_inode(sb);
                        if (*pinode == NULL) {
-                               kfree(buf);
-                               return -ENOMEM;
+                               rc = -ENOMEM;
+                               goto cgii_exit;
                        }
                        /* Is an i_ino of zero legal? Can we use that to check
                           if the server supports returning inode numbers?  Are
@@ -559,7 +582,7 @@ int cifs_get_inode_info(struct inode **pinode,
                /* fill in 0777 bits from ACL */
                if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
                        cFYI(1, ("Getting mode bits from ACL"));
-                       acl_to_uid_mode(inode, search_path);
+                       acl_to_uid_mode(inode, search_path, pfid);
                }
 #endif
                if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
@@ -573,8 +596,11 @@ int cifs_get_inode_info(struct inode **pinode,
                        atomic_set(&cifsInfo->inUse, 1);
                }
 
-               cifs_set_ops(inode);
+               cifs_set_ops(inode, is_dfs_referral);
        }
+cgii_exit:
+       if (full_path != search_path)
+               kfree(full_path);
        kfree(buf);
        return rc;
 }
@@ -603,7 +629,8 @@ struct inode *cifs_iget(struct super_block *sb, unsigned long ino)
        if (cifs_sb->tcon->unix_ext)
                rc = cifs_get_inode_info_unix(&inode, "", inode->i_sb, xid);
        else
-               rc = cifs_get_inode_info(&inode, "", NULL, inode->i_sb, xid);
+               rc = cifs_get_inode_info(&inode, "", NULL, inode->i_sb, xid,
+                                        NULL);
        if (rc && cifs_sb->tcon->ipc) {
                cFYI(1, ("ipc connection - fake read inode"));
                inode->i_mode |= S_IFDIR;
@@ -676,7 +703,7 @@ psx_del_no_retry:
        } else if (rc == -ENOENT) {
                d_drop(direntry);
        } else if (rc == -ETXTBSY) {
-               int oplock = FALSE;
+               int oplock = 0;
                __u16 netfid;
 
                rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
@@ -709,7 +736,7 @@ psx_del_no_retry:
                                rc = -EOPNOTSUPP;
 
                        if (rc == -EOPNOTSUPP) {
-                               int oplock = FALSE;
+                               int oplock = 0;
                                __u16 netfid;
                        /*      rc = CIFSSMBSetAttrLegacy(xid, pTcon,
                                                          full_path,
@@ -747,7 +774,7 @@ psx_del_no_retry:
                                if (direntry->d_inode)
                                        drop_nlink(direntry->d_inode);
                        } else if (rc == -ETXTBSY) {
-                               int oplock = FALSE;
+                               int oplock = 0;
                                __u16 netfid;
 
                                rc = CIFSSMBOpen(xid, pTcon, full_path,
@@ -804,7 +831,7 @@ static void posix_fill_in_inode(struct inode *tmp_inode,
        local_size  = tmp_inode->i_size;
 
        cifs_unix_info_to_inode(tmp_inode, pData, 1);
-       cifs_set_ops(tmp_inode);
+       cifs_set_ops(tmp_inode, false);
 
        if (!S_ISREG(tmp_inode->i_mode))
                return;
@@ -936,7 +963,7 @@ mkdir_get_info:
                                                      inode->i_sb, xid);
                else
                        rc = cifs_get_inode_info(&newinode, full_path, NULL,
-                                                inode->i_sb, xid);
+                                                inode->i_sb, xid, NULL);
 
                if (pTcon->nocase)
                        direntry->d_op = &cifs_ci_dentry_ops;
@@ -1122,7 +1149,7 @@ int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
                cFYI(1, ("rename rc %d", rc));
 
        if ((rc == -EIO) || (rc == -EEXIST)) {
-               int oplock = FALSE;
+               int oplock = 0;
                __u16 netfid;
 
                /* BB FIXME Is Generic Read correct for rename? */
@@ -1159,7 +1186,7 @@ int cifs_revalidate(struct dentry *direntry)
        struct cifsInodeInfo *cifsInode;
        loff_t local_size;
        struct timespec local_mtime;
-       int invalidate_inode = FALSE;
+       bool invalidate_inode = false;
 
        if (direntry->d_inode == NULL)
                return -ENOENT;
@@ -1218,7 +1245,7 @@ int cifs_revalidate(struct dentry *direntry)
                }
        } else {
                rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
-                                        direntry->d_sb, xid);
+                                        direntry->d_sb, xid, NULL);
                if (rc) {
                        cFYI(1, ("error on getting revalidate info %d", rc));
 /*                     if (rc != -ENOENT)
@@ -1241,7 +1268,7 @@ int cifs_revalidate(struct dentry *direntry)
                           only ones who could have modified the file and the
                           server copy is staler than ours */
                } else {
-                       invalidate_inode = TRUE;
+                       invalidate_inode = true;
                }
        }
 
@@ -1375,8 +1402,8 @@ int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
        int rc = -EACCES;
        struct cifsFileInfo *open_file = NULL;
        FILE_BASIC_INFO time_buf;
-       int set_time = FALSE;
-       int set_dosattr = FALSE;
+       bool set_time = false;
+       bool set_dosattr = false;
        __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
        __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
        __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
@@ -1407,11 +1434,10 @@ int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
        }
        cifsInode = CIFS_I(direntry->d_inode);
 
-       /* BB check if we need to refresh inode from server now ? BB */
-
-       if (attrs->ia_valid & ATTR_SIZE) {
+       if ((attrs->ia_valid & ATTR_MTIME) || (attrs->ia_valid & ATTR_SIZE)) {
                /*
-                  Flush data before changing file size on server. If the
+                  Flush data before changing file size or changing the last
+                  write time of the file on the server. If the
                   flush returns error, store it to report later and continue.
                   BB: This should be smarter. Why bother flushing pages that
                   will be truncated anyway? Also, should we error out here if
@@ -1422,7 +1448,9 @@ int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
                        CIFS_I(direntry->d_inode)->write_behind_rc = rc;
                        rc = 0;
                }
+       }
 
+       if (attrs->ia_valid & ATTR_SIZE) {
                /* To avoid spurious oplock breaks from server, in the case of
                   inodes that we already have open, avoid doing path based
                   setting of file size if we can do it by handle.
@@ -1436,7 +1464,7 @@ int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
                        __u16 nfid = open_file->netfid;
                        __u32 npid = open_file->pid;
                        rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size,
-                                               nfid, npid, FALSE);
+                                               nfid, npid, false);
                        atomic_dec(&open_file->wrtPending);
                        cFYI(1, ("SetFSize for attrs rc = %d", rc));
                        if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
@@ -1456,14 +1484,14 @@ int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
                           it was found or because there was an error setting
                           it by handle */
                        rc = CIFSSMBSetEOF(xid, pTcon, full_path,
-                                          attrs->ia_size, FALSE,
+                                          attrs->ia_size, false,
                                           cifs_sb->local_nls,
                                           cifs_sb->mnt_cifs_flags &
                                                CIFS_MOUNT_MAP_SPECIAL_CHR);
                        cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
                        if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
                                __u16 netfid;
-                               int oplock = FALSE;
+                               int oplock = 0;
 
                                rc = SMBLegacyOpen(xid, pTcon, full_path,
                                        FILE_OPEN,
@@ -1488,7 +1516,7 @@ int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
 
                /* Server is ok setting allocation size implicitly - no need
                   to call:
-               CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE,
+               CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, true,
                         cifs_sb->local_nls);
                   */
 
@@ -1536,7 +1564,7 @@ int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
 #endif
                        /* not writeable */
                        if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
-                               set_dosattr = TRUE;
+                               set_dosattr = true;
                                time_buf.Attributes =
                                        cpu_to_le32(cifsInode->cifsAttrs |
                                                    ATTR_READONLY);
@@ -1546,28 +1574,24 @@ int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
                        not be able to write to it - so if any write
                        bit is enabled for user or group or other we
                        need to at least try to remove r/o dos attr */
-                       set_dosattr = TRUE;
+                       set_dosattr = true;
                        time_buf.Attributes = cpu_to_le32(cifsInode->cifsAttrs &
                                            (~ATTR_READONLY));
                        /* Windows ignores set to zero */
                        if (time_buf.Attributes == 0)
                                time_buf.Attributes |= cpu_to_le32(ATTR_NORMAL);
                }
-#ifdef CONFIG_CIFS_EXPERIMENTAL
-               if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
-                       mode_to_acl(direntry->d_inode, full_path, mode);
-#endif
        }
 
        if (attrs->ia_valid & ATTR_ATIME) {
-               set_time = TRUE;
+               set_time = true;
                time_buf.LastAccessTime =
                    cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
        } else
                time_buf.LastAccessTime = 0;
 
        if (attrs->ia_valid & ATTR_MTIME) {
-               set_time = TRUE;
+               set_time = true;
                time_buf.LastWriteTime =
                    cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
        } else
@@ -1578,7 +1602,7 @@ int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
           server times */
 
        if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
-               set_time = TRUE;
+               set_time = true;
                /* Although Samba throws this field away
                it may be useful to Windows - but we do
                not want to set ctime unless some other
@@ -1602,7 +1626,7 @@ int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
                        rc = -EOPNOTSUPP;
 
                if (rc == -EOPNOTSUPP) {
-                       int oplock = FALSE;
+                       int oplock = 0;
                        __u16 netfid;
 
                        cFYI(1, ("calling SetFileInfo since SetPathInfo for "