Commit 4468eb3fd102cad559e51594a01cbc65b994d264

Authored by Jeff Layton
Committed by Steve French
1 parent aaa9bbe039

on non-posix shares, clear write bits in mode when ATTR_READONLY is set

When mounting a share with posix extensions disabled,
cifs_get_inode_info turns off all the write bits in the mode for regular
files if ATTR_READONLY is set. Directories and other inode types,
however, can also have ATTR_READONLY set, but the mode gives no
indication of this.

This patch makes this apply to other inode types besides regular files.
It also cleans up how modes are set in cifs_get_inode_info for both the
"normal" and "dynperm" cases.

Signed-off-by: Jeff Layton <jlayton@redhat.com>
Signed-off-by: Steve French <sfrench@us.ibm.com>

Showing 2 changed files with 75 additions and 72 deletions Inline Diff

1 /* 1 /*
2 * fs/cifs/inode.c 2 * fs/cifs/inode.c
3 * 3 *
4 * Copyright (C) International Business Machines Corp., 2002,2007 4 * Copyright (C) International Business Machines Corp., 2002,2007
5 * Author(s): Steve French (sfrench@us.ibm.com) 5 * Author(s): Steve French (sfrench@us.ibm.com)
6 * 6 *
7 * This library is free software; you can redistribute it and/or modify 7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published 8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or 9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 * 11 *
12 * This library is distributed in the hope that it will be useful, 12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details. 15 * the GNU Lesser General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU Lesser General Public License 17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software 18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 20 */
21 #include <linux/fs.h> 21 #include <linux/fs.h>
22 #include <linux/stat.h> 22 #include <linux/stat.h>
23 #include <linux/pagemap.h> 23 #include <linux/pagemap.h>
24 #include <asm/div64.h> 24 #include <asm/div64.h>
25 #include "cifsfs.h" 25 #include "cifsfs.h"
26 #include "cifspdu.h" 26 #include "cifspdu.h"
27 #include "cifsglob.h" 27 #include "cifsglob.h"
28 #include "cifsproto.h" 28 #include "cifsproto.h"
29 #include "cifs_debug.h" 29 #include "cifs_debug.h"
30 #include "cifs_fs_sb.h" 30 #include "cifs_fs_sb.h"
31 31
32 32
33 static void cifs_set_ops(struct inode *inode, const bool is_dfs_referral) 33 static void cifs_set_ops(struct inode *inode, const bool is_dfs_referral)
34 { 34 {
35 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 35 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
36 36
37 switch (inode->i_mode & S_IFMT) { 37 switch (inode->i_mode & S_IFMT) {
38 case S_IFREG: 38 case S_IFREG:
39 inode->i_op = &cifs_file_inode_ops; 39 inode->i_op = &cifs_file_inode_ops;
40 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { 40 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
41 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) 41 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
42 inode->i_fop = &cifs_file_direct_nobrl_ops; 42 inode->i_fop = &cifs_file_direct_nobrl_ops;
43 else 43 else
44 inode->i_fop = &cifs_file_direct_ops; 44 inode->i_fop = &cifs_file_direct_ops;
45 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) 45 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
46 inode->i_fop = &cifs_file_nobrl_ops; 46 inode->i_fop = &cifs_file_nobrl_ops;
47 else { /* not direct, send byte range locks */ 47 else { /* not direct, send byte range locks */
48 inode->i_fop = &cifs_file_ops; 48 inode->i_fop = &cifs_file_ops;
49 } 49 }
50 50
51 51
52 /* check if server can support readpages */ 52 /* check if server can support readpages */
53 if (cifs_sb->tcon->ses->server->maxBuf < 53 if (cifs_sb->tcon->ses->server->maxBuf <
54 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE) 54 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
55 inode->i_data.a_ops = &cifs_addr_ops_smallbuf; 55 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
56 else 56 else
57 inode->i_data.a_ops = &cifs_addr_ops; 57 inode->i_data.a_ops = &cifs_addr_ops;
58 break; 58 break;
59 case S_IFDIR: 59 case S_IFDIR:
60 #ifdef CONFIG_CIFS_DFS_UPCALL 60 #ifdef CONFIG_CIFS_DFS_UPCALL
61 if (is_dfs_referral) { 61 if (is_dfs_referral) {
62 inode->i_op = &cifs_dfs_referral_inode_operations; 62 inode->i_op = &cifs_dfs_referral_inode_operations;
63 } else { 63 } else {
64 #else /* NO DFS support, treat as a directory */ 64 #else /* NO DFS support, treat as a directory */
65 { 65 {
66 #endif 66 #endif
67 inode->i_op = &cifs_dir_inode_ops; 67 inode->i_op = &cifs_dir_inode_ops;
68 inode->i_fop = &cifs_dir_ops; 68 inode->i_fop = &cifs_dir_ops;
69 } 69 }
70 break; 70 break;
71 case S_IFLNK: 71 case S_IFLNK:
72 inode->i_op = &cifs_symlink_inode_ops; 72 inode->i_op = &cifs_symlink_inode_ops;
73 break; 73 break;
74 default: 74 default:
75 init_special_inode(inode, inode->i_mode, inode->i_rdev); 75 init_special_inode(inode, inode->i_mode, inode->i_rdev);
76 break; 76 break;
77 } 77 }
78 } 78 }
79 79
80 static void cifs_unix_info_to_inode(struct inode *inode, 80 static void cifs_unix_info_to_inode(struct inode *inode,
81 FILE_UNIX_BASIC_INFO *info, int force_uid_gid) 81 FILE_UNIX_BASIC_INFO *info, int force_uid_gid)
82 { 82 {
83 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 83 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
84 struct cifsInodeInfo *cifsInfo = CIFS_I(inode); 84 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
85 __u64 num_of_bytes = le64_to_cpu(info->NumOfBytes); 85 __u64 num_of_bytes = le64_to_cpu(info->NumOfBytes);
86 __u64 end_of_file = le64_to_cpu(info->EndOfFile); 86 __u64 end_of_file = le64_to_cpu(info->EndOfFile);
87 87
88 inode->i_atime = cifs_NTtimeToUnix(le64_to_cpu(info->LastAccessTime)); 88 inode->i_atime = cifs_NTtimeToUnix(le64_to_cpu(info->LastAccessTime));
89 inode->i_mtime = 89 inode->i_mtime =
90 cifs_NTtimeToUnix(le64_to_cpu(info->LastModificationTime)); 90 cifs_NTtimeToUnix(le64_to_cpu(info->LastModificationTime));
91 inode->i_ctime = cifs_NTtimeToUnix(le64_to_cpu(info->LastStatusChange)); 91 inode->i_ctime = cifs_NTtimeToUnix(le64_to_cpu(info->LastStatusChange));
92 inode->i_mode = le64_to_cpu(info->Permissions); 92 inode->i_mode = le64_to_cpu(info->Permissions);
93 93
94 /* 94 /*
95 * Since we set the inode type below we need to mask off 95 * Since we set the inode type below we need to mask off
96 * to avoid strange results if bits set above. 96 * to avoid strange results if bits set above.
97 */ 97 */
98 inode->i_mode &= ~S_IFMT; 98 inode->i_mode &= ~S_IFMT;
99 switch (le32_to_cpu(info->Type)) { 99 switch (le32_to_cpu(info->Type)) {
100 case UNIX_FILE: 100 case UNIX_FILE:
101 inode->i_mode |= S_IFREG; 101 inode->i_mode |= S_IFREG;
102 break; 102 break;
103 case UNIX_SYMLINK: 103 case UNIX_SYMLINK:
104 inode->i_mode |= S_IFLNK; 104 inode->i_mode |= S_IFLNK;
105 break; 105 break;
106 case UNIX_DIR: 106 case UNIX_DIR:
107 inode->i_mode |= S_IFDIR; 107 inode->i_mode |= S_IFDIR;
108 break; 108 break;
109 case UNIX_CHARDEV: 109 case UNIX_CHARDEV:
110 inode->i_mode |= S_IFCHR; 110 inode->i_mode |= S_IFCHR;
111 inode->i_rdev = MKDEV(le64_to_cpu(info->DevMajor), 111 inode->i_rdev = MKDEV(le64_to_cpu(info->DevMajor),
112 le64_to_cpu(info->DevMinor) & MINORMASK); 112 le64_to_cpu(info->DevMinor) & MINORMASK);
113 break; 113 break;
114 case UNIX_BLOCKDEV: 114 case UNIX_BLOCKDEV:
115 inode->i_mode |= S_IFBLK; 115 inode->i_mode |= S_IFBLK;
116 inode->i_rdev = MKDEV(le64_to_cpu(info->DevMajor), 116 inode->i_rdev = MKDEV(le64_to_cpu(info->DevMajor),
117 le64_to_cpu(info->DevMinor) & MINORMASK); 117 le64_to_cpu(info->DevMinor) & MINORMASK);
118 break; 118 break;
119 case UNIX_FIFO: 119 case UNIX_FIFO:
120 inode->i_mode |= S_IFIFO; 120 inode->i_mode |= S_IFIFO;
121 break; 121 break;
122 case UNIX_SOCKET: 122 case UNIX_SOCKET:
123 inode->i_mode |= S_IFSOCK; 123 inode->i_mode |= S_IFSOCK;
124 break; 124 break;
125 default: 125 default:
126 /* safest to call it a file if we do not know */ 126 /* safest to call it a file if we do not know */
127 inode->i_mode |= S_IFREG; 127 inode->i_mode |= S_IFREG;
128 cFYI(1, ("unknown type %d", le32_to_cpu(info->Type))); 128 cFYI(1, ("unknown type %d", le32_to_cpu(info->Type)));
129 break; 129 break;
130 } 130 }
131 131
132 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) && 132 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) &&
133 !force_uid_gid) 133 !force_uid_gid)
134 inode->i_uid = cifs_sb->mnt_uid; 134 inode->i_uid = cifs_sb->mnt_uid;
135 else 135 else
136 inode->i_uid = le64_to_cpu(info->Uid); 136 inode->i_uid = le64_to_cpu(info->Uid);
137 137
138 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) && 138 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) &&
139 !force_uid_gid) 139 !force_uid_gid)
140 inode->i_gid = cifs_sb->mnt_gid; 140 inode->i_gid = cifs_sb->mnt_gid;
141 else 141 else
142 inode->i_gid = le64_to_cpu(info->Gid); 142 inode->i_gid = le64_to_cpu(info->Gid);
143 143
144 inode->i_nlink = le64_to_cpu(info->Nlinks); 144 inode->i_nlink = le64_to_cpu(info->Nlinks);
145 145
146 spin_lock(&inode->i_lock); 146 spin_lock(&inode->i_lock);
147 if (is_size_safe_to_change(cifsInfo, end_of_file)) { 147 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
148 /* 148 /*
149 * We can not safely change the file size here if the client 149 * We can not safely change the file size here if the client
150 * is writing to it due to potential races. 150 * is writing to it due to potential races.
151 */ 151 */
152 i_size_write(inode, end_of_file); 152 i_size_write(inode, end_of_file);
153 153
154 /* 154 /*
155 * i_blocks is not related to (i_size / i_blksize), 155 * i_blocks is not related to (i_size / i_blksize),
156 * but instead 512 byte (2**9) size is required for 156 * but instead 512 byte (2**9) size is required for
157 * calculating num blocks. 157 * calculating num blocks.
158 */ 158 */
159 inode->i_blocks = (512 - 1 + num_of_bytes) >> 9; 159 inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
160 } 160 }
161 spin_unlock(&inode->i_lock); 161 spin_unlock(&inode->i_lock);
162 } 162 }
163 163
164 164
165 /* 165 /*
166 * Needed to setup inode data for the directory which is the 166 * Needed to setup inode data for the directory which is the
167 * junction to the new submount (ie to setup the fake directory 167 * junction to the new submount (ie to setup the fake directory
168 * which represents a DFS referral) 168 * which represents a DFS referral)
169 */ 169 */
170 static void fill_fake_finddataunix(FILE_UNIX_BASIC_INFO *pfnd_dat, 170 static void fill_fake_finddataunix(FILE_UNIX_BASIC_INFO *pfnd_dat,
171 struct super_block *sb) 171 struct super_block *sb)
172 { 172 {
173 struct inode *pinode = NULL; 173 struct inode *pinode = NULL;
174 174
175 memset(pfnd_dat, 0, sizeof(FILE_UNIX_BASIC_INFO)); 175 memset(pfnd_dat, 0, sizeof(FILE_UNIX_BASIC_INFO));
176 176
177 /* __le64 pfnd_dat->EndOfFile = cpu_to_le64(0); 177 /* __le64 pfnd_dat->EndOfFile = cpu_to_le64(0);
178 __le64 pfnd_dat->NumOfBytes = cpu_to_le64(0); 178 __le64 pfnd_dat->NumOfBytes = cpu_to_le64(0);
179 __u64 UniqueId = 0; */ 179 __u64 UniqueId = 0; */
180 pfnd_dat->LastStatusChange = 180 pfnd_dat->LastStatusChange =
181 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); 181 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
182 pfnd_dat->LastAccessTime = 182 pfnd_dat->LastAccessTime =
183 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); 183 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
184 pfnd_dat->LastModificationTime = 184 pfnd_dat->LastModificationTime =
185 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); 185 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
186 pfnd_dat->Type = cpu_to_le32(UNIX_DIR); 186 pfnd_dat->Type = cpu_to_le32(UNIX_DIR);
187 pfnd_dat->Permissions = cpu_to_le64(S_IXUGO | S_IRWXU); 187 pfnd_dat->Permissions = cpu_to_le64(S_IXUGO | S_IRWXU);
188 pfnd_dat->Nlinks = cpu_to_le64(2); 188 pfnd_dat->Nlinks = cpu_to_le64(2);
189 if (sb->s_root) 189 if (sb->s_root)
190 pinode = sb->s_root->d_inode; 190 pinode = sb->s_root->d_inode;
191 if (pinode == NULL) 191 if (pinode == NULL)
192 return; 192 return;
193 193
194 /* fill in default values for the remaining based on root 194 /* fill in default values for the remaining based on root
195 inode since we can not query the server for this inode info */ 195 inode since we can not query the server for this inode info */
196 pfnd_dat->DevMajor = cpu_to_le64(MAJOR(pinode->i_rdev)); 196 pfnd_dat->DevMajor = cpu_to_le64(MAJOR(pinode->i_rdev));
197 pfnd_dat->DevMinor = cpu_to_le64(MINOR(pinode->i_rdev)); 197 pfnd_dat->DevMinor = cpu_to_le64(MINOR(pinode->i_rdev));
198 pfnd_dat->Uid = cpu_to_le64(pinode->i_uid); 198 pfnd_dat->Uid = cpu_to_le64(pinode->i_uid);
199 pfnd_dat->Gid = cpu_to_le64(pinode->i_gid); 199 pfnd_dat->Gid = cpu_to_le64(pinode->i_gid);
200 } 200 }
201 201
202 int cifs_get_inode_info_unix(struct inode **pinode, 202 int cifs_get_inode_info_unix(struct inode **pinode,
203 const unsigned char *full_path, struct super_block *sb, int xid) 203 const unsigned char *full_path, struct super_block *sb, int xid)
204 { 204 {
205 int rc = 0; 205 int rc = 0;
206 FILE_UNIX_BASIC_INFO find_data; 206 FILE_UNIX_BASIC_INFO find_data;
207 struct cifsTconInfo *pTcon; 207 struct cifsTconInfo *pTcon;
208 struct inode *inode; 208 struct inode *inode;
209 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 209 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
210 bool is_dfs_referral = false; 210 bool is_dfs_referral = false;
211 struct cifsInodeInfo *cifsInfo; 211 struct cifsInodeInfo *cifsInfo;
212 __u64 num_of_bytes; 212 __u64 num_of_bytes;
213 __u64 end_of_file; 213 __u64 end_of_file;
214 214
215 pTcon = cifs_sb->tcon; 215 pTcon = cifs_sb->tcon;
216 cFYI(1, ("Getting info on %s", full_path)); 216 cFYI(1, ("Getting info on %s", full_path));
217 217
218 /* could have done a find first instead but this returns more info */ 218 /* could have done a find first instead but this returns more info */
219 rc = CIFSSMBUnixQPathInfo(xid, pTcon, full_path, &find_data, 219 rc = CIFSSMBUnixQPathInfo(xid, pTcon, full_path, &find_data,
220 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & 220 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
221 CIFS_MOUNT_MAP_SPECIAL_CHR); 221 CIFS_MOUNT_MAP_SPECIAL_CHR);
222 if (rc) { 222 if (rc) {
223 if (rc == -EREMOTE && !is_dfs_referral) { 223 if (rc == -EREMOTE && !is_dfs_referral) {
224 is_dfs_referral = true; 224 is_dfs_referral = true;
225 cFYI(DBG2, ("DFS ref")); 225 cFYI(DBG2, ("DFS ref"));
226 /* for DFS, server does not give us real inode data */ 226 /* for DFS, server does not give us real inode data */
227 fill_fake_finddataunix(&find_data, sb); 227 fill_fake_finddataunix(&find_data, sb);
228 rc = 0; 228 rc = 0;
229 } 229 }
230 } 230 }
231 num_of_bytes = le64_to_cpu(find_data.NumOfBytes); 231 num_of_bytes = le64_to_cpu(find_data.NumOfBytes);
232 end_of_file = le64_to_cpu(find_data.EndOfFile); 232 end_of_file = le64_to_cpu(find_data.EndOfFile);
233 233
234 /* get new inode */ 234 /* get new inode */
235 if (*pinode == NULL) { 235 if (*pinode == NULL) {
236 *pinode = new_inode(sb); 236 *pinode = new_inode(sb);
237 if (*pinode == NULL) { 237 if (*pinode == NULL) {
238 rc = -ENOMEM; 238 rc = -ENOMEM;
239 goto cgiiu_exit; 239 goto cgiiu_exit;
240 } 240 }
241 /* Is an i_ino of zero legal? */ 241 /* Is an i_ino of zero legal? */
242 /* note ino incremented to unique num in new_inode */ 242 /* note ino incremented to unique num in new_inode */
243 /* Are there sanity checks we can use to ensure that 243 /* Are there sanity checks we can use to ensure that
244 the server is really filling in that field? */ 244 the server is really filling in that field? */
245 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) 245 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
246 (*pinode)->i_ino = (unsigned long)find_data.UniqueId; 246 (*pinode)->i_ino = (unsigned long)find_data.UniqueId;
247 247
248 if (sb->s_flags & MS_NOATIME) 248 if (sb->s_flags & MS_NOATIME)
249 (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME; 249 (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
250 250
251 insert_inode_hash(*pinode); 251 insert_inode_hash(*pinode);
252 } 252 }
253 253
254 inode = *pinode; 254 inode = *pinode;
255 cifsInfo = CIFS_I(inode); 255 cifsInfo = CIFS_I(inode);
256 256
257 cFYI(1, ("Old time %ld", cifsInfo->time)); 257 cFYI(1, ("Old time %ld", cifsInfo->time));
258 cifsInfo->time = jiffies; 258 cifsInfo->time = jiffies;
259 cFYI(1, ("New time %ld", cifsInfo->time)); 259 cFYI(1, ("New time %ld", cifsInfo->time));
260 /* this is ok to set on every inode revalidate */ 260 /* this is ok to set on every inode revalidate */
261 atomic_set(&cifsInfo->inUse, 1); 261 atomic_set(&cifsInfo->inUse, 1);
262 262
263 cifs_unix_info_to_inode(inode, &find_data, 0); 263 cifs_unix_info_to_inode(inode, &find_data, 0);
264 264
265 if (num_of_bytes < end_of_file) 265 if (num_of_bytes < end_of_file)
266 cFYI(1, ("allocation size less than end of file")); 266 cFYI(1, ("allocation size less than end of file"));
267 cFYI(1, ("Size %ld and blocks %llu", 267 cFYI(1, ("Size %ld and blocks %llu",
268 (unsigned long) inode->i_size, 268 (unsigned long) inode->i_size,
269 (unsigned long long)inode->i_blocks)); 269 (unsigned long long)inode->i_blocks));
270 270
271 cifs_set_ops(inode, is_dfs_referral); 271 cifs_set_ops(inode, is_dfs_referral);
272 cgiiu_exit: 272 cgiiu_exit:
273 return rc; 273 return rc;
274 } 274 }
275 275
276 static int decode_sfu_inode(struct inode *inode, __u64 size, 276 static int decode_sfu_inode(struct inode *inode, __u64 size,
277 const unsigned char *path, 277 const unsigned char *path,
278 struct cifs_sb_info *cifs_sb, int xid) 278 struct cifs_sb_info *cifs_sb, int xid)
279 { 279 {
280 int rc; 280 int rc;
281 int oplock = 0; 281 int oplock = 0;
282 __u16 netfid; 282 __u16 netfid;
283 struct cifsTconInfo *pTcon = cifs_sb->tcon; 283 struct cifsTconInfo *pTcon = cifs_sb->tcon;
284 char buf[24]; 284 char buf[24];
285 unsigned int bytes_read; 285 unsigned int bytes_read;
286 char *pbuf; 286 char *pbuf;
287 287
288 pbuf = buf; 288 pbuf = buf;
289 289
290 if (size == 0) { 290 if (size == 0) {
291 inode->i_mode |= S_IFIFO; 291 inode->i_mode |= S_IFIFO;
292 return 0; 292 return 0;
293 } else if (size < 8) { 293 } else if (size < 8) {
294 return -EINVAL; /* EOPNOTSUPP? */ 294 return -EINVAL; /* EOPNOTSUPP? */
295 } 295 }
296 296
297 rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ, 297 rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
298 CREATE_NOT_DIR, &netfid, &oplock, NULL, 298 CREATE_NOT_DIR, &netfid, &oplock, NULL,
299 cifs_sb->local_nls, 299 cifs_sb->local_nls,
300 cifs_sb->mnt_cifs_flags & 300 cifs_sb->mnt_cifs_flags &
301 CIFS_MOUNT_MAP_SPECIAL_CHR); 301 CIFS_MOUNT_MAP_SPECIAL_CHR);
302 if (rc == 0) { 302 if (rc == 0) {
303 int buf_type = CIFS_NO_BUFFER; 303 int buf_type = CIFS_NO_BUFFER;
304 /* Read header */ 304 /* Read header */
305 rc = CIFSSMBRead(xid, pTcon, 305 rc = CIFSSMBRead(xid, pTcon,
306 netfid, 306 netfid,
307 24 /* length */, 0 /* offset */, 307 24 /* length */, 0 /* offset */,
308 &bytes_read, &pbuf, &buf_type); 308 &bytes_read, &pbuf, &buf_type);
309 if ((rc == 0) && (bytes_read >= 8)) { 309 if ((rc == 0) && (bytes_read >= 8)) {
310 if (memcmp("IntxBLK", pbuf, 8) == 0) { 310 if (memcmp("IntxBLK", pbuf, 8) == 0) {
311 cFYI(1, ("Block device")); 311 cFYI(1, ("Block device"));
312 inode->i_mode |= S_IFBLK; 312 inode->i_mode |= S_IFBLK;
313 if (bytes_read == 24) { 313 if (bytes_read == 24) {
314 /* we have enough to decode dev num */ 314 /* we have enough to decode dev num */
315 __u64 mjr; /* major */ 315 __u64 mjr; /* major */
316 __u64 mnr; /* minor */ 316 __u64 mnr; /* minor */
317 mjr = le64_to_cpu(*(__le64 *)(pbuf+8)); 317 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
318 mnr = le64_to_cpu(*(__le64 *)(pbuf+16)); 318 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
319 inode->i_rdev = MKDEV(mjr, mnr); 319 inode->i_rdev = MKDEV(mjr, mnr);
320 } 320 }
321 } else if (memcmp("IntxCHR", pbuf, 8) == 0) { 321 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
322 cFYI(1, ("Char device")); 322 cFYI(1, ("Char device"));
323 inode->i_mode |= S_IFCHR; 323 inode->i_mode |= S_IFCHR;
324 if (bytes_read == 24) { 324 if (bytes_read == 24) {
325 /* we have enough to decode dev num */ 325 /* we have enough to decode dev num */
326 __u64 mjr; /* major */ 326 __u64 mjr; /* major */
327 __u64 mnr; /* minor */ 327 __u64 mnr; /* minor */
328 mjr = le64_to_cpu(*(__le64 *)(pbuf+8)); 328 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
329 mnr = le64_to_cpu(*(__le64 *)(pbuf+16)); 329 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
330 inode->i_rdev = MKDEV(mjr, mnr); 330 inode->i_rdev = MKDEV(mjr, mnr);
331 } 331 }
332 } else if (memcmp("IntxLNK", pbuf, 7) == 0) { 332 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
333 cFYI(1, ("Symlink")); 333 cFYI(1, ("Symlink"));
334 inode->i_mode |= S_IFLNK; 334 inode->i_mode |= S_IFLNK;
335 } else { 335 } else {
336 inode->i_mode |= S_IFREG; /* file? */ 336 inode->i_mode |= S_IFREG; /* file? */
337 rc = -EOPNOTSUPP; 337 rc = -EOPNOTSUPP;
338 } 338 }
339 } else { 339 } else {
340 inode->i_mode |= S_IFREG; /* then it is a file */ 340 inode->i_mode |= S_IFREG; /* then it is a file */
341 rc = -EOPNOTSUPP; /* or some unknown SFU type */ 341 rc = -EOPNOTSUPP; /* or some unknown SFU type */
342 } 342 }
343 CIFSSMBClose(xid, pTcon, netfid); 343 CIFSSMBClose(xid, pTcon, netfid);
344 } 344 }
345 return rc; 345 return rc;
346 } 346 }
347 347
348 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */ 348 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
349 349
350 static int get_sfu_mode(struct inode *inode, 350 static int get_sfu_mode(struct inode *inode,
351 const unsigned char *path, 351 const unsigned char *path,
352 struct cifs_sb_info *cifs_sb, int xid) 352 struct cifs_sb_info *cifs_sb, int xid)
353 { 353 {
354 #ifdef CONFIG_CIFS_XATTR 354 #ifdef CONFIG_CIFS_XATTR
355 ssize_t rc; 355 ssize_t rc;
356 char ea_value[4]; 356 char ea_value[4];
357 __u32 mode; 357 __u32 mode;
358 358
359 rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS", 359 rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
360 ea_value, 4 /* size of buf */, cifs_sb->local_nls, 360 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
361 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); 361 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
362 if (rc < 0) 362 if (rc < 0)
363 return (int)rc; 363 return (int)rc;
364 else if (rc > 3) { 364 else if (rc > 3) {
365 mode = le32_to_cpu(*((__le32 *)ea_value)); 365 mode = le32_to_cpu(*((__le32 *)ea_value));
366 inode->i_mode &= ~SFBITS_MASK; 366 inode->i_mode &= ~SFBITS_MASK;
367 cFYI(1, ("special bits 0%o org mode 0%o", mode, inode->i_mode)); 367 cFYI(1, ("special bits 0%o org mode 0%o", mode, inode->i_mode));
368 inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode; 368 inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode;
369 cFYI(1, ("special mode bits 0%o", mode)); 369 cFYI(1, ("special mode bits 0%o", mode));
370 return 0; 370 return 0;
371 } else { 371 } else {
372 return 0; 372 return 0;
373 } 373 }
374 #else 374 #else
375 return -EOPNOTSUPP; 375 return -EOPNOTSUPP;
376 #endif 376 #endif
377 } 377 }
378 378
379 /* 379 /*
380 * Needed to setup inode data for the directory which is the 380 * Needed to setup inode data for the directory which is the
381 * junction to the new submount (ie to setup the fake directory 381 * junction to the new submount (ie to setup the fake directory
382 * which represents a DFS referral) 382 * which represents a DFS referral)
383 */ 383 */
384 static void fill_fake_finddata(FILE_ALL_INFO *pfnd_dat, 384 static void fill_fake_finddata(FILE_ALL_INFO *pfnd_dat,
385 struct super_block *sb) 385 struct super_block *sb)
386 { 386 {
387 memset(pfnd_dat, 0, sizeof(FILE_ALL_INFO)); 387 memset(pfnd_dat, 0, sizeof(FILE_ALL_INFO));
388 388
389 /* __le64 pfnd_dat->AllocationSize = cpu_to_le64(0); 389 /* __le64 pfnd_dat->AllocationSize = cpu_to_le64(0);
390 __le64 pfnd_dat->EndOfFile = cpu_to_le64(0); 390 __le64 pfnd_dat->EndOfFile = cpu_to_le64(0);
391 __u8 pfnd_dat->DeletePending = 0; 391 __u8 pfnd_dat->DeletePending = 0;
392 __u8 pfnd_data->Directory = 0; 392 __u8 pfnd_data->Directory = 0;
393 __le32 pfnd_dat->EASize = 0; 393 __le32 pfnd_dat->EASize = 0;
394 __u64 pfnd_dat->IndexNumber = 0; 394 __u64 pfnd_dat->IndexNumber = 0;
395 __u64 pfnd_dat->IndexNumber1 = 0; */ 395 __u64 pfnd_dat->IndexNumber1 = 0; */
396 pfnd_dat->CreationTime = 396 pfnd_dat->CreationTime =
397 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); 397 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
398 pfnd_dat->LastAccessTime = 398 pfnd_dat->LastAccessTime =
399 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); 399 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
400 pfnd_dat->LastWriteTime = 400 pfnd_dat->LastWriteTime =
401 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); 401 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
402 pfnd_dat->ChangeTime = 402 pfnd_dat->ChangeTime =
403 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); 403 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
404 pfnd_dat->Attributes = cpu_to_le32(ATTR_DIRECTORY); 404 pfnd_dat->Attributes = cpu_to_le32(ATTR_DIRECTORY);
405 pfnd_dat->NumberOfLinks = cpu_to_le32(2); 405 pfnd_dat->NumberOfLinks = cpu_to_le32(2);
406 } 406 }
407 407
408 int cifs_get_inode_info(struct inode **pinode, 408 int cifs_get_inode_info(struct inode **pinode,
409 const unsigned char *full_path, FILE_ALL_INFO *pfindData, 409 const unsigned char *full_path, FILE_ALL_INFO *pfindData,
410 struct super_block *sb, int xid, const __u16 *pfid) 410 struct super_block *sb, int xid, const __u16 *pfid)
411 { 411 {
412 int rc = 0; 412 int rc = 0;
413 __u32 attr; 413 __u32 attr;
414 struct cifsInodeInfo *cifsInfo; 414 struct cifsInodeInfo *cifsInfo;
415 struct cifsTconInfo *pTcon; 415 struct cifsTconInfo *pTcon;
416 struct inode *inode; 416 struct inode *inode;
417 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 417 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
418 char *buf = NULL; 418 char *buf = NULL;
419 bool adjustTZ = false; 419 bool adjustTZ = false;
420 bool is_dfs_referral = false; 420 bool is_dfs_referral = false;
421 umode_t default_mode;
421 422
422 pTcon = cifs_sb->tcon; 423 pTcon = cifs_sb->tcon;
423 cFYI(1, ("Getting info on %s", full_path)); 424 cFYI(1, ("Getting info on %s", full_path));
424 425
425 if ((pfindData == NULL) && (*pinode != NULL)) { 426 if ((pfindData == NULL) && (*pinode != NULL)) {
426 if (CIFS_I(*pinode)->clientCanCacheRead) { 427 if (CIFS_I(*pinode)->clientCanCacheRead) {
427 cFYI(1, ("No need to revalidate cached inode sizes")); 428 cFYI(1, ("No need to revalidate cached inode sizes"));
428 return rc; 429 return rc;
429 } 430 }
430 } 431 }
431 432
432 /* if file info not passed in then get it from server */ 433 /* if file info not passed in then get it from server */
433 if (pfindData == NULL) { 434 if (pfindData == NULL) {
434 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); 435 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
435 if (buf == NULL) 436 if (buf == NULL)
436 return -ENOMEM; 437 return -ENOMEM;
437 pfindData = (FILE_ALL_INFO *)buf; 438 pfindData = (FILE_ALL_INFO *)buf;
438 439
439 /* could do find first instead but this returns more info */ 440 /* could do find first instead but this returns more info */
440 rc = CIFSSMBQPathInfo(xid, pTcon, full_path, pfindData, 441 rc = CIFSSMBQPathInfo(xid, pTcon, full_path, pfindData,
441 0 /* not legacy */, 442 0 /* not legacy */,
442 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & 443 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
443 CIFS_MOUNT_MAP_SPECIAL_CHR); 444 CIFS_MOUNT_MAP_SPECIAL_CHR);
444 /* BB optimize code so we do not make the above call 445 /* BB optimize code so we do not make the above call
445 when server claims no NT SMB support and the above call 446 when server claims no NT SMB support and the above call
446 failed at least once - set flag in tcon or mount */ 447 failed at least once - set flag in tcon or mount */
447 if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) { 448 if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
448 rc = SMBQueryInformation(xid, pTcon, full_path, 449 rc = SMBQueryInformation(xid, pTcon, full_path,
449 pfindData, cifs_sb->local_nls, 450 pfindData, cifs_sb->local_nls,
450 cifs_sb->mnt_cifs_flags & 451 cifs_sb->mnt_cifs_flags &
451 CIFS_MOUNT_MAP_SPECIAL_CHR); 452 CIFS_MOUNT_MAP_SPECIAL_CHR);
452 adjustTZ = true; 453 adjustTZ = true;
453 } 454 }
454 } 455 }
455 /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */ 456 /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
456 if (rc == -EREMOTE) { 457 if (rc == -EREMOTE) {
457 is_dfs_referral = true; 458 is_dfs_referral = true;
458 fill_fake_finddata(pfindData, sb); 459 fill_fake_finddata(pfindData, sb);
459 rc = 0; 460 rc = 0;
460 } else if (rc) 461 } else if (rc)
461 goto cgii_exit; 462 goto cgii_exit;
462 463
463 attr = le32_to_cpu(pfindData->Attributes); 464 attr = le32_to_cpu(pfindData->Attributes);
464 465
465 /* get new inode */ 466 /* get new inode */
466 if (*pinode == NULL) { 467 if (*pinode == NULL) {
467 *pinode = new_inode(sb); 468 *pinode = new_inode(sb);
468 if (*pinode == NULL) { 469 if (*pinode == NULL) {
469 rc = -ENOMEM; 470 rc = -ENOMEM;
470 goto cgii_exit; 471 goto cgii_exit;
471 } 472 }
472 /* Is an i_ino of zero legal? Can we use that to check 473 /* Is an i_ino of zero legal? Can we use that to check
473 if the server supports returning inode numbers? Are 474 if the server supports returning inode numbers? Are
474 there other sanity checks we can use to ensure that 475 there other sanity checks we can use to ensure that
475 the server is really filling in that field? */ 476 the server is really filling in that field? */
476 477
477 /* We can not use the IndexNumber field by default from 478 /* We can not use the IndexNumber field by default from
478 Windows or Samba (in ALL_INFO buf) but we can request 479 Windows or Samba (in ALL_INFO buf) but we can request
479 it explicitly. It may not be unique presumably if 480 it explicitly. It may not be unique presumably if
480 the server has multiple devices mounted under one share */ 481 the server has multiple devices mounted under one share */
481 482
482 /* There may be higher info levels that work but are 483 /* There may be higher info levels that work but are
483 there Windows server or network appliances for which 484 there Windows server or network appliances for which
484 IndexNumber field is not guaranteed unique? */ 485 IndexNumber field is not guaranteed unique? */
485 486
486 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { 487 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
487 int rc1 = 0; 488 int rc1 = 0;
488 __u64 inode_num; 489 __u64 inode_num;
489 490
490 rc1 = CIFSGetSrvInodeNumber(xid, pTcon, 491 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
491 full_path, &inode_num, 492 full_path, &inode_num,
492 cifs_sb->local_nls, 493 cifs_sb->local_nls,
493 cifs_sb->mnt_cifs_flags & 494 cifs_sb->mnt_cifs_flags &
494 CIFS_MOUNT_MAP_SPECIAL_CHR); 495 CIFS_MOUNT_MAP_SPECIAL_CHR);
495 if (rc1) { 496 if (rc1) {
496 cFYI(1, ("GetSrvInodeNum rc %d", rc1)); 497 cFYI(1, ("GetSrvInodeNum rc %d", rc1));
497 /* BB EOPNOSUPP disable SERVER_INUM? */ 498 /* BB EOPNOSUPP disable SERVER_INUM? */
498 } else /* do we need cast or hash to ino? */ 499 } else /* do we need cast or hash to ino? */
499 (*pinode)->i_ino = inode_num; 500 (*pinode)->i_ino = inode_num;
500 } /* else ino incremented to unique num in new_inode*/ 501 } /* else ino incremented to unique num in new_inode*/
501 if (sb->s_flags & MS_NOATIME) 502 if (sb->s_flags & MS_NOATIME)
502 (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME; 503 (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
503 insert_inode_hash(*pinode); 504 insert_inode_hash(*pinode);
504 } 505 }
505 inode = *pinode; 506 inode = *pinode;
506 cifsInfo = CIFS_I(inode); 507 cifsInfo = CIFS_I(inode);
507 cifsInfo->cifsAttrs = attr; 508 cifsInfo->cifsAttrs = attr;
508 cFYI(1, ("Old time %ld", cifsInfo->time)); 509 cFYI(1, ("Old time %ld", cifsInfo->time));
509 cifsInfo->time = jiffies; 510 cifsInfo->time = jiffies;
510 cFYI(1, ("New time %ld", cifsInfo->time)); 511 cFYI(1, ("New time %ld", cifsInfo->time));
511 512
512 /* blksize needs to be multiple of two. So safer to default to 513 /* blksize needs to be multiple of two. So safer to default to
513 blksize and blkbits set in superblock so 2**blkbits and blksize 514 blksize and blkbits set in superblock so 2**blkbits and blksize
514 will match rather than setting to: 515 will match rather than setting to:
515 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/ 516 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
516 517
517 /* Linux can not store file creation time so ignore it */ 518 /* Linux can not store file creation time so ignore it */
518 if (pfindData->LastAccessTime) 519 if (pfindData->LastAccessTime)
519 inode->i_atime = cifs_NTtimeToUnix 520 inode->i_atime = cifs_NTtimeToUnix
520 (le64_to_cpu(pfindData->LastAccessTime)); 521 (le64_to_cpu(pfindData->LastAccessTime));
521 else /* do not need to use current_fs_time - time not stored */ 522 else /* do not need to use current_fs_time - time not stored */
522 inode->i_atime = CURRENT_TIME; 523 inode->i_atime = CURRENT_TIME;
523 inode->i_mtime = 524 inode->i_mtime =
524 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime)); 525 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
525 inode->i_ctime = 526 inode->i_ctime =
526 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime)); 527 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
527 cFYI(DBG2, ("Attributes came in as 0x%x", attr)); 528 cFYI(DBG2, ("Attributes came in as 0x%x", attr));
528 if (adjustTZ && (pTcon->ses) && (pTcon->ses->server)) { 529 if (adjustTZ && (pTcon->ses) && (pTcon->ses->server)) {
529 inode->i_ctime.tv_sec += pTcon->ses->server->timeAdj; 530 inode->i_ctime.tv_sec += pTcon->ses->server->timeAdj;
530 inode->i_mtime.tv_sec += pTcon->ses->server->timeAdj; 531 inode->i_mtime.tv_sec += pTcon->ses->server->timeAdj;
531 } 532 }
532 533
533 /* set default mode. will override for dirs below */ 534 /* get default inode mode */
534 if (atomic_read(&cifsInfo->inUse) == 0) 535 if (attr & ATTR_DIRECTORY)
535 /* new inode, can safely set these fields */ 536 default_mode = cifs_sb->mnt_dir_mode;
536 inode->i_mode = cifs_sb->mnt_file_mode; 537 else
537 else /* since we set the inode type below we need to mask off 538 default_mode = cifs_sb->mnt_file_mode;
538 to avoid strange results if type changes and both
539 get orred in */
540 inode->i_mode &= ~S_IFMT;
541 /* if (attr & ATTR_REPARSE) */
542 /* We no longer handle these as symlinks because we could not
543 follow them due to the absolute path with drive letter */
544 if (attr & ATTR_DIRECTORY) {
545 /* override default perms since we do not do byte range locking
546 on dirs */
547 inode->i_mode = cifs_sb->mnt_dir_mode;
548 inode->i_mode |= S_IFDIR;
549 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
550 (cifsInfo->cifsAttrs & ATTR_SYSTEM) &&
551 /* No need to le64 convert size of zero */
552 (pfindData->EndOfFile == 0)) {
553 inode->i_mode = cifs_sb->mnt_file_mode;
554 inode->i_mode |= S_IFIFO;
555 /* BB Finish for SFU style symlinks and devices */
556 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
557 (cifsInfo->cifsAttrs & ATTR_SYSTEM)) {
558 if (decode_sfu_inode(inode, le64_to_cpu(pfindData->EndOfFile),
559 full_path, cifs_sb, xid))
560 cFYI(1, ("Unrecognized sfu inode type"));
561 539
562 cFYI(1, ("sfu mode 0%o", inode->i_mode)); 540 /* set permission bits */
541 if (atomic_read(&cifsInfo->inUse) == 0 ||
542 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
543 inode->i_mode = default_mode;
544 else {
545 /* just reenable write bits if !ATTR_READONLY */
546 if ((inode->i_mode & S_IWUGO) == 0 &&
547 (attr & ATTR_READONLY) == 0)
548 inode->i_mode |= (S_IWUGO & default_mode);
549 inode->i_mode &= ~S_IFMT;
550 }
551 /* clear write bits if ATTR_READONLY is set */
552 if (attr & ATTR_READONLY)
553 inode->i_mode &= ~S_IWUGO;
554
555 /* set inode type */
556 if ((attr & ATTR_SYSTEM) &&
557 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
558 /* no need to fix endianness on 0 */
559 if (pfindData->EndOfFile == 0)
560 inode->i_mode |= S_IFIFO;
561 else if (decode_sfu_inode(inode,
562 le64_to_cpu(pfindData->EndOfFile),
563 full_path, cifs_sb, xid))
564 cFYI(1, ("unknown SFU file type\n"));
563 } else { 565 } else {
564 inode->i_mode |= S_IFREG; 566 if (attr & ATTR_DIRECTORY)
565 /* treat dos attribute of read-only as read-only mode eg 555 */ 567 inode->i_mode |= S_IFDIR;
566 if (cifsInfo->cifsAttrs & ATTR_READONLY) 568 else
567 inode->i_mode &= ~(S_IWUGO); 569 inode->i_mode |= S_IFREG;
568 else if ((inode->i_mode & S_IWUGO) == 0)
569 /* the ATTR_READONLY flag may have been */
570 /* changed on server -- set any w bits */
571 /* allowed by mnt_file_mode */
572 inode->i_mode |= (S_IWUGO & cifs_sb->mnt_file_mode);
573 /* BB add code to validate if device or weird share or device type? */
574 } 570 }
575 571
576 spin_lock(&inode->i_lock); 572 spin_lock(&inode->i_lock);
577 if (is_size_safe_to_change(cifsInfo, 573 if (is_size_safe_to_change(cifsInfo,
578 le64_to_cpu(pfindData->EndOfFile))) { 574 le64_to_cpu(pfindData->EndOfFile))) {
579 /* can not safely shrink the file size here if the 575 /* can not safely shrink the file size here if the
580 client is writing to it due to potential races */ 576 client is writing to it due to potential races */
581 i_size_write(inode, le64_to_cpu(pfindData->EndOfFile)); 577 i_size_write(inode, le64_to_cpu(pfindData->EndOfFile));
582 578
583 /* 512 bytes (2**9) is the fake blocksize that must be 579 /* 512 bytes (2**9) is the fake blocksize that must be
584 used for this calculation */ 580 used for this calculation */
585 inode->i_blocks = (512 - 1 + le64_to_cpu( 581 inode->i_blocks = (512 - 1 + le64_to_cpu(
586 pfindData->AllocationSize)) >> 9; 582 pfindData->AllocationSize)) >> 9;
587 } 583 }
588 spin_unlock(&inode->i_lock); 584 spin_unlock(&inode->i_lock);
589 585
590 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks); 586 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
591 587
592 /* BB fill in uid and gid here? with help from winbind? 588 /* BB fill in uid and gid here? with help from winbind?
593 or retrieve from NTFS stream extended attribute */ 589 or retrieve from NTFS stream extended attribute */
594 #ifdef CONFIG_CIFS_EXPERIMENTAL 590 #ifdef CONFIG_CIFS_EXPERIMENTAL
595 /* fill in 0777 bits from ACL */ 591 /* fill in 0777 bits from ACL */
596 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) { 592 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
597 cFYI(1, ("Getting mode bits from ACL")); 593 cFYI(1, ("Getting mode bits from ACL"));
598 acl_to_uid_mode(inode, full_path, pfid); 594 acl_to_uid_mode(inode, full_path, pfid);
599 } 595 }
600 #endif 596 #endif
601 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) { 597 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
602 /* fill in remaining high mode bits e.g. SUID, VTX */ 598 /* fill in remaining high mode bits e.g. SUID, VTX */
603 get_sfu_mode(inode, full_path, cifs_sb, xid); 599 get_sfu_mode(inode, full_path, cifs_sb, xid);
604 } else if (atomic_read(&cifsInfo->inUse) == 0) { 600 } else if (atomic_read(&cifsInfo->inUse) == 0) {
605 inode->i_uid = cifs_sb->mnt_uid; 601 inode->i_uid = cifs_sb->mnt_uid;
606 inode->i_gid = cifs_sb->mnt_gid; 602 inode->i_gid = cifs_sb->mnt_gid;
607 /* set so we do not keep refreshing these fields with 603 /* set so we do not keep refreshing these fields with
608 bad data after user has changed them in memory */ 604 bad data after user has changed them in memory */
609 atomic_set(&cifsInfo->inUse, 1); 605 atomic_set(&cifsInfo->inUse, 1);
610 } 606 }
611 607
612 cifs_set_ops(inode, is_dfs_referral); 608 cifs_set_ops(inode, is_dfs_referral);
613 609
614 610
615 611
616 612
617 cgii_exit: 613 cgii_exit:
618 kfree(buf); 614 kfree(buf);
619 return rc; 615 return rc;
620 } 616 }
621 617
622 static const struct inode_operations cifs_ipc_inode_ops = { 618 static const struct inode_operations cifs_ipc_inode_ops = {
623 .lookup = cifs_lookup, 619 .lookup = cifs_lookup,
624 }; 620 };
625 621
626 /* gets root inode */ 622 /* gets root inode */
627 struct inode *cifs_iget(struct super_block *sb, unsigned long ino) 623 struct inode *cifs_iget(struct super_block *sb, unsigned long ino)
628 { 624 {
629 int xid; 625 int xid;
630 struct cifs_sb_info *cifs_sb; 626 struct cifs_sb_info *cifs_sb;
631 struct inode *inode; 627 struct inode *inode;
632 long rc; 628 long rc;
633 629
634 inode = iget_locked(sb, ino); 630 inode = iget_locked(sb, ino);
635 if (!inode) 631 if (!inode)
636 return ERR_PTR(-ENOMEM); 632 return ERR_PTR(-ENOMEM);
637 if (!(inode->i_state & I_NEW)) 633 if (!(inode->i_state & I_NEW))
638 return inode; 634 return inode;
639 635
640 cifs_sb = CIFS_SB(inode->i_sb); 636 cifs_sb = CIFS_SB(inode->i_sb);
641 xid = GetXid(); 637 xid = GetXid();
642 638
643 if (cifs_sb->tcon->unix_ext) 639 if (cifs_sb->tcon->unix_ext)
644 rc = cifs_get_inode_info_unix(&inode, "", inode->i_sb, xid); 640 rc = cifs_get_inode_info_unix(&inode, "", inode->i_sb, xid);
645 else 641 else
646 rc = cifs_get_inode_info(&inode, "", NULL, inode->i_sb, xid, 642 rc = cifs_get_inode_info(&inode, "", NULL, inode->i_sb, xid,
647 NULL); 643 NULL);
648 if (rc && cifs_sb->tcon->ipc) { 644 if (rc && cifs_sb->tcon->ipc) {
649 cFYI(1, ("ipc connection - fake read inode")); 645 cFYI(1, ("ipc connection - fake read inode"));
650 inode->i_mode |= S_IFDIR; 646 inode->i_mode |= S_IFDIR;
651 inode->i_nlink = 2; 647 inode->i_nlink = 2;
652 inode->i_op = &cifs_ipc_inode_ops; 648 inode->i_op = &cifs_ipc_inode_ops;
653 inode->i_fop = &simple_dir_operations; 649 inode->i_fop = &simple_dir_operations;
654 inode->i_uid = cifs_sb->mnt_uid; 650 inode->i_uid = cifs_sb->mnt_uid;
655 inode->i_gid = cifs_sb->mnt_gid; 651 inode->i_gid = cifs_sb->mnt_gid;
656 _FreeXid(xid); 652 _FreeXid(xid);
657 iget_failed(inode); 653 iget_failed(inode);
658 return ERR_PTR(rc); 654 return ERR_PTR(rc);
659 } 655 }
660 656
661 unlock_new_inode(inode); 657 unlock_new_inode(inode);
662 658
663 /* can not call macro FreeXid here since in a void func 659 /* can not call macro FreeXid here since in a void func
664 * TODO: This is no longer true 660 * TODO: This is no longer true
665 */ 661 */
666 _FreeXid(xid); 662 _FreeXid(xid);
667 return inode; 663 return inode;
668 } 664 }
669 665
670 int cifs_unlink(struct inode *inode, struct dentry *direntry) 666 int cifs_unlink(struct inode *inode, struct dentry *direntry)
671 { 667 {
672 int rc = 0; 668 int rc = 0;
673 int xid; 669 int xid;
674 struct cifs_sb_info *cifs_sb; 670 struct cifs_sb_info *cifs_sb;
675 struct cifsTconInfo *pTcon; 671 struct cifsTconInfo *pTcon;
676 char *full_path = NULL; 672 char *full_path = NULL;
677 struct cifsInodeInfo *cifsInode; 673 struct cifsInodeInfo *cifsInode;
678 FILE_BASIC_INFO *pinfo_buf; 674 FILE_BASIC_INFO *pinfo_buf;
679 675
680 cFYI(1, ("cifs_unlink, inode = 0x%p", inode)); 676 cFYI(1, ("cifs_unlink, inode = 0x%p", inode));
681 677
682 xid = GetXid(); 678 xid = GetXid();
683 679
684 if (inode) 680 if (inode)
685 cifs_sb = CIFS_SB(inode->i_sb); 681 cifs_sb = CIFS_SB(inode->i_sb);
686 else 682 else
687 cifs_sb = CIFS_SB(direntry->d_sb); 683 cifs_sb = CIFS_SB(direntry->d_sb);
688 pTcon = cifs_sb->tcon; 684 pTcon = cifs_sb->tcon;
689 685
690 /* Unlink can be called from rename so we can not grab the sem here 686 /* Unlink can be called from rename so we can not grab the sem here
691 since we deadlock otherwise */ 687 since we deadlock otherwise */
692 /* mutex_lock(&direntry->d_sb->s_vfs_rename_mutex);*/ 688 /* mutex_lock(&direntry->d_sb->s_vfs_rename_mutex);*/
693 full_path = build_path_from_dentry(direntry); 689 full_path = build_path_from_dentry(direntry);
694 /* mutex_unlock(&direntry->d_sb->s_vfs_rename_mutex);*/ 690 /* mutex_unlock(&direntry->d_sb->s_vfs_rename_mutex);*/
695 if (full_path == NULL) { 691 if (full_path == NULL) {
696 FreeXid(xid); 692 FreeXid(xid);
697 return -ENOMEM; 693 return -ENOMEM;
698 } 694 }
699 695
700 if ((pTcon->ses->capabilities & CAP_UNIX) && 696 if ((pTcon->ses->capabilities & CAP_UNIX) &&
701 (CIFS_UNIX_POSIX_PATH_OPS_CAP & 697 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
702 le64_to_cpu(pTcon->fsUnixInfo.Capability))) { 698 le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
703 rc = CIFSPOSIXDelFile(xid, pTcon, full_path, 699 rc = CIFSPOSIXDelFile(xid, pTcon, full_path,
704 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls, 700 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
705 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); 701 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
706 cFYI(1, ("posix del rc %d", rc)); 702 cFYI(1, ("posix del rc %d", rc));
707 if ((rc == 0) || (rc == -ENOENT)) 703 if ((rc == 0) || (rc == -ENOENT))
708 goto psx_del_no_retry; 704 goto psx_del_no_retry;
709 } 705 }
710 706
711 rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls, 707 rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
712 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); 708 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
713 psx_del_no_retry: 709 psx_del_no_retry:
714 if (!rc) { 710 if (!rc) {
715 if (direntry->d_inode) 711 if (direntry->d_inode)
716 drop_nlink(direntry->d_inode); 712 drop_nlink(direntry->d_inode);
717 } else if (rc == -ENOENT) { 713 } else if (rc == -ENOENT) {
718 d_drop(direntry); 714 d_drop(direntry);
719 } else if (rc == -ETXTBSY) { 715 } else if (rc == -ETXTBSY) {
720 int oplock = 0; 716 int oplock = 0;
721 __u16 netfid; 717 __u16 netfid;
722 718
723 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE, 719 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
724 CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE, 720 CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
725 &netfid, &oplock, NULL, cifs_sb->local_nls, 721 &netfid, &oplock, NULL, cifs_sb->local_nls,
726 cifs_sb->mnt_cifs_flags & 722 cifs_sb->mnt_cifs_flags &
727 CIFS_MOUNT_MAP_SPECIAL_CHR); 723 CIFS_MOUNT_MAP_SPECIAL_CHR);
728 if (rc == 0) { 724 if (rc == 0) {
729 CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL, 725 CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
730 cifs_sb->local_nls, 726 cifs_sb->local_nls,
731 cifs_sb->mnt_cifs_flags & 727 cifs_sb->mnt_cifs_flags &
732 CIFS_MOUNT_MAP_SPECIAL_CHR); 728 CIFS_MOUNT_MAP_SPECIAL_CHR);
733 CIFSSMBClose(xid, pTcon, netfid); 729 CIFSSMBClose(xid, pTcon, netfid);
734 if (direntry->d_inode) 730 if (direntry->d_inode)
735 drop_nlink(direntry->d_inode); 731 drop_nlink(direntry->d_inode);
736 } 732 }
737 } else if (rc == -EACCES) { 733 } else if (rc == -EACCES) {
738 /* try only if r/o attribute set in local lookup data? */ 734 /* try only if r/o attribute set in local lookup data? */
739 pinfo_buf = kzalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL); 735 pinfo_buf = kzalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
740 if (pinfo_buf) { 736 if (pinfo_buf) {
741 /* ATTRS set to normal clears r/o bit */ 737 /* ATTRS set to normal clears r/o bit */
742 pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL); 738 pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
743 if (!(pTcon->ses->flags & CIFS_SES_NT4)) 739 if (!(pTcon->ses->flags & CIFS_SES_NT4))
744 rc = CIFSSMBSetTimes(xid, pTcon, full_path, 740 rc = CIFSSMBSetTimes(xid, pTcon, full_path,
745 pinfo_buf, 741 pinfo_buf,
746 cifs_sb->local_nls, 742 cifs_sb->local_nls,
747 cifs_sb->mnt_cifs_flags & 743 cifs_sb->mnt_cifs_flags &
748 CIFS_MOUNT_MAP_SPECIAL_CHR); 744 CIFS_MOUNT_MAP_SPECIAL_CHR);
749 else 745 else
750 rc = -EOPNOTSUPP; 746 rc = -EOPNOTSUPP;
751 747
752 if (rc == -EOPNOTSUPP) { 748 if (rc == -EOPNOTSUPP) {
753 int oplock = 0; 749 int oplock = 0;
754 __u16 netfid; 750 __u16 netfid;
755 /* rc = CIFSSMBSetAttrLegacy(xid, pTcon, 751 /* rc = CIFSSMBSetAttrLegacy(xid, pTcon,
756 full_path, 752 full_path,
757 (__u16)ATTR_NORMAL, 753 (__u16)ATTR_NORMAL,
758 cifs_sb->local_nls); 754 cifs_sb->local_nls);
759 For some strange reason it seems that NT4 eats the 755 For some strange reason it seems that NT4 eats the
760 old setattr call without actually setting the 756 old setattr call without actually setting the
761 attributes so on to the third attempted workaround 757 attributes so on to the third attempted workaround
762 */ 758 */
763 759
764 /* BB could scan to see if we already have it open 760 /* BB could scan to see if we already have it open
765 and pass in pid of opener to function */ 761 and pass in pid of opener to function */
766 rc = CIFSSMBOpen(xid, pTcon, full_path, 762 rc = CIFSSMBOpen(xid, pTcon, full_path,
767 FILE_OPEN, SYNCHRONIZE | 763 FILE_OPEN, SYNCHRONIZE |
768 FILE_WRITE_ATTRIBUTES, 0, 764 FILE_WRITE_ATTRIBUTES, 0,
769 &netfid, &oplock, NULL, 765 &netfid, &oplock, NULL,
770 cifs_sb->local_nls, 766 cifs_sb->local_nls,
771 cifs_sb->mnt_cifs_flags & 767 cifs_sb->mnt_cifs_flags &
772 CIFS_MOUNT_MAP_SPECIAL_CHR); 768 CIFS_MOUNT_MAP_SPECIAL_CHR);
773 if (rc == 0) { 769 if (rc == 0) {
774 rc = CIFSSMBSetFileTimes(xid, pTcon, 770 rc = CIFSSMBSetFileTimes(xid, pTcon,
775 pinfo_buf, 771 pinfo_buf,
776 netfid); 772 netfid);
777 CIFSSMBClose(xid, pTcon, netfid); 773 CIFSSMBClose(xid, pTcon, netfid);
778 } 774 }
779 } 775 }
780 kfree(pinfo_buf); 776 kfree(pinfo_buf);
781 } 777 }
782 if (rc == 0) { 778 if (rc == 0) {
783 rc = CIFSSMBDelFile(xid, pTcon, full_path, 779 rc = CIFSSMBDelFile(xid, pTcon, full_path,
784 cifs_sb->local_nls, 780 cifs_sb->local_nls,
785 cifs_sb->mnt_cifs_flags & 781 cifs_sb->mnt_cifs_flags &
786 CIFS_MOUNT_MAP_SPECIAL_CHR); 782 CIFS_MOUNT_MAP_SPECIAL_CHR);
787 if (!rc) { 783 if (!rc) {
788 if (direntry->d_inode) 784 if (direntry->d_inode)
789 drop_nlink(direntry->d_inode); 785 drop_nlink(direntry->d_inode);
790 } else if (rc == -ETXTBSY) { 786 } else if (rc == -ETXTBSY) {
791 int oplock = 0; 787 int oplock = 0;
792 __u16 netfid; 788 __u16 netfid;
793 789
794 rc = CIFSSMBOpen(xid, pTcon, full_path, 790 rc = CIFSSMBOpen(xid, pTcon, full_path,
795 FILE_OPEN, DELETE, 791 FILE_OPEN, DELETE,
796 CREATE_NOT_DIR | 792 CREATE_NOT_DIR |
797 CREATE_DELETE_ON_CLOSE, 793 CREATE_DELETE_ON_CLOSE,
798 &netfid, &oplock, NULL, 794 &netfid, &oplock, NULL,
799 cifs_sb->local_nls, 795 cifs_sb->local_nls,
800 cifs_sb->mnt_cifs_flags & 796 cifs_sb->mnt_cifs_flags &
801 CIFS_MOUNT_MAP_SPECIAL_CHR); 797 CIFS_MOUNT_MAP_SPECIAL_CHR);
802 if (rc == 0) { 798 if (rc == 0) {
803 CIFSSMBRenameOpenFile(xid, pTcon, 799 CIFSSMBRenameOpenFile(xid, pTcon,
804 netfid, NULL, 800 netfid, NULL,
805 cifs_sb->local_nls, 801 cifs_sb->local_nls,
806 cifs_sb->mnt_cifs_flags & 802 cifs_sb->mnt_cifs_flags &
807 CIFS_MOUNT_MAP_SPECIAL_CHR); 803 CIFS_MOUNT_MAP_SPECIAL_CHR);
808 CIFSSMBClose(xid, pTcon, netfid); 804 CIFSSMBClose(xid, pTcon, netfid);
809 if (direntry->d_inode) 805 if (direntry->d_inode)
810 drop_nlink(direntry->d_inode); 806 drop_nlink(direntry->d_inode);
811 } 807 }
812 /* BB if rc = -ETXTBUSY goto the rename logic BB */ 808 /* BB if rc = -ETXTBUSY goto the rename logic BB */
813 } 809 }
814 } 810 }
815 } 811 }
816 if (direntry->d_inode) { 812 if (direntry->d_inode) {
817 cifsInode = CIFS_I(direntry->d_inode); 813 cifsInode = CIFS_I(direntry->d_inode);
818 cifsInode->time = 0; /* will force revalidate to get info 814 cifsInode->time = 0; /* will force revalidate to get info
819 when needed */ 815 when needed */
820 direntry->d_inode->i_ctime = current_fs_time(inode->i_sb); 816 direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
821 } 817 }
822 if (inode) { 818 if (inode) {
823 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb); 819 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
824 cifsInode = CIFS_I(inode); 820 cifsInode = CIFS_I(inode);
825 cifsInode->time = 0; /* force revalidate of dir as well */ 821 cifsInode->time = 0; /* force revalidate of dir as well */
826 } 822 }
827 823
828 kfree(full_path); 824 kfree(full_path);
829 FreeXid(xid); 825 FreeXid(xid);
830 return rc; 826 return rc;
831 } 827 }
832 828
833 static void posix_fill_in_inode(struct inode *tmp_inode, 829 static void posix_fill_in_inode(struct inode *tmp_inode,
834 FILE_UNIX_BASIC_INFO *pData, int isNewInode) 830 FILE_UNIX_BASIC_INFO *pData, int isNewInode)
835 { 831 {
836 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode); 832 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
837 loff_t local_size; 833 loff_t local_size;
838 struct timespec local_mtime; 834 struct timespec local_mtime;
839 835
840 cifsInfo->time = jiffies; 836 cifsInfo->time = jiffies;
841 atomic_inc(&cifsInfo->inUse); 837 atomic_inc(&cifsInfo->inUse);
842 838
843 /* save mtime and size */ 839 /* save mtime and size */
844 local_mtime = tmp_inode->i_mtime; 840 local_mtime = tmp_inode->i_mtime;
845 local_size = tmp_inode->i_size; 841 local_size = tmp_inode->i_size;
846 842
847 cifs_unix_info_to_inode(tmp_inode, pData, 1); 843 cifs_unix_info_to_inode(tmp_inode, pData, 1);
848 cifs_set_ops(tmp_inode, false); 844 cifs_set_ops(tmp_inode, false);
849 845
850 if (!S_ISREG(tmp_inode->i_mode)) 846 if (!S_ISREG(tmp_inode->i_mode))
851 return; 847 return;
852 848
853 /* 849 /*
854 * No sense invalidating pages for new inode 850 * No sense invalidating pages for new inode
855 * since we we have not started caching 851 * since we we have not started caching
856 * readahead file data yet. 852 * readahead file data yet.
857 */ 853 */
858 if (isNewInode) 854 if (isNewInode)
859 return; 855 return;
860 856
861 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) && 857 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
862 (local_size == tmp_inode->i_size)) { 858 (local_size == tmp_inode->i_size)) {
863 cFYI(1, ("inode exists but unchanged")); 859 cFYI(1, ("inode exists but unchanged"));
864 } else { 860 } else {
865 /* file may have changed on server */ 861 /* file may have changed on server */
866 cFYI(1, ("invalidate inode, readdir detected change")); 862 cFYI(1, ("invalidate inode, readdir detected change"));
867 invalidate_remote_inode(tmp_inode); 863 invalidate_remote_inode(tmp_inode);
868 } 864 }
869 } 865 }
870 866
871 int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode) 867 int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
872 { 868 {
873 int rc = 0; 869 int rc = 0;
874 int xid; 870 int xid;
875 struct cifs_sb_info *cifs_sb; 871 struct cifs_sb_info *cifs_sb;
876 struct cifsTconInfo *pTcon; 872 struct cifsTconInfo *pTcon;
877 char *full_path = NULL; 873 char *full_path = NULL;
878 struct inode *newinode = NULL; 874 struct inode *newinode = NULL;
879 875
880 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode)); 876 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
881 877
882 xid = GetXid(); 878 xid = GetXid();
883 879
884 cifs_sb = CIFS_SB(inode->i_sb); 880 cifs_sb = CIFS_SB(inode->i_sb);
885 pTcon = cifs_sb->tcon; 881 pTcon = cifs_sb->tcon;
886 882
887 full_path = build_path_from_dentry(direntry); 883 full_path = build_path_from_dentry(direntry);
888 if (full_path == NULL) { 884 if (full_path == NULL) {
889 FreeXid(xid); 885 FreeXid(xid);
890 return -ENOMEM; 886 return -ENOMEM;
891 } 887 }
892 888
893 if ((pTcon->ses->capabilities & CAP_UNIX) && 889 if ((pTcon->ses->capabilities & CAP_UNIX) &&
894 (CIFS_UNIX_POSIX_PATH_OPS_CAP & 890 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
895 le64_to_cpu(pTcon->fsUnixInfo.Capability))) { 891 le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
896 u32 oplock = 0; 892 u32 oplock = 0;
897 FILE_UNIX_BASIC_INFO *pInfo = 893 FILE_UNIX_BASIC_INFO *pInfo =
898 kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); 894 kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
899 if (pInfo == NULL) { 895 if (pInfo == NULL) {
900 rc = -ENOMEM; 896 rc = -ENOMEM;
901 goto mkdir_out; 897 goto mkdir_out;
902 } 898 }
903 899
904 mode &= ~current->fs->umask; 900 mode &= ~current->fs->umask;
905 rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT, 901 rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT,
906 mode, NULL /* netfid */, pInfo, &oplock, 902 mode, NULL /* netfid */, pInfo, &oplock,
907 full_path, cifs_sb->local_nls, 903 full_path, cifs_sb->local_nls,
908 cifs_sb->mnt_cifs_flags & 904 cifs_sb->mnt_cifs_flags &
909 CIFS_MOUNT_MAP_SPECIAL_CHR); 905 CIFS_MOUNT_MAP_SPECIAL_CHR);
910 if (rc == -EOPNOTSUPP) { 906 if (rc == -EOPNOTSUPP) {
911 kfree(pInfo); 907 kfree(pInfo);
912 goto mkdir_retry_old; 908 goto mkdir_retry_old;
913 } else if (rc) { 909 } else if (rc) {
914 cFYI(1, ("posix mkdir returned 0x%x", rc)); 910 cFYI(1, ("posix mkdir returned 0x%x", rc));
915 d_drop(direntry); 911 d_drop(direntry);
916 } else { 912 } else {
917 if (pInfo->Type == cpu_to_le32(-1)) { 913 if (pInfo->Type == cpu_to_le32(-1)) {
918 /* no return info, go query for it */ 914 /* no return info, go query for it */
919 kfree(pInfo); 915 kfree(pInfo);
920 goto mkdir_get_info; 916 goto mkdir_get_info;
921 } 917 }
922 /*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need 918 /*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need
923 to set uid/gid */ 919 to set uid/gid */
924 inc_nlink(inode); 920 inc_nlink(inode);
925 if (pTcon->nocase) 921 if (pTcon->nocase)
926 direntry->d_op = &cifs_ci_dentry_ops; 922 direntry->d_op = &cifs_ci_dentry_ops;
927 else 923 else
928 direntry->d_op = &cifs_dentry_ops; 924 direntry->d_op = &cifs_dentry_ops;
929 925
930 newinode = new_inode(inode->i_sb); 926 newinode = new_inode(inode->i_sb);
931 if (newinode == NULL) { 927 if (newinode == NULL) {
932 kfree(pInfo); 928 kfree(pInfo);
933 goto mkdir_get_info; 929 goto mkdir_get_info;
934 } 930 }
935 /* Is an i_ino of zero legal? */ 931 /* Is an i_ino of zero legal? */
936 /* Are there sanity checks we can use to ensure that 932 /* Are there sanity checks we can use to ensure that
937 the server is really filling in that field? */ 933 the server is really filling in that field? */
938 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { 934 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
939 newinode->i_ino = 935 newinode->i_ino =
940 (unsigned long)pInfo->UniqueId; 936 (unsigned long)pInfo->UniqueId;
941 } /* note ino incremented to unique num in new_inode */ 937 } /* note ino incremented to unique num in new_inode */
942 if (inode->i_sb->s_flags & MS_NOATIME) 938 if (inode->i_sb->s_flags & MS_NOATIME)
943 newinode->i_flags |= S_NOATIME | S_NOCMTIME; 939 newinode->i_flags |= S_NOATIME | S_NOCMTIME;
944 newinode->i_nlink = 2; 940 newinode->i_nlink = 2;
945 941
946 insert_inode_hash(newinode); 942 insert_inode_hash(newinode);
947 d_instantiate(direntry, newinode); 943 d_instantiate(direntry, newinode);
948 944
949 /* we already checked in POSIXCreate whether 945 /* we already checked in POSIXCreate whether
950 frame was long enough */ 946 frame was long enough */
951 posix_fill_in_inode(direntry->d_inode, 947 posix_fill_in_inode(direntry->d_inode,
952 pInfo, 1 /* NewInode */); 948 pInfo, 1 /* NewInode */);
953 #ifdef CONFIG_CIFS_DEBUG2 949 #ifdef CONFIG_CIFS_DEBUG2
954 cFYI(1, ("instantiated dentry %p %s to inode %p", 950 cFYI(1, ("instantiated dentry %p %s to inode %p",
955 direntry, direntry->d_name.name, newinode)); 951 direntry, direntry->d_name.name, newinode));
956 952
957 if (newinode->i_nlink != 2) 953 if (newinode->i_nlink != 2)
958 cFYI(1, ("unexpected number of links %d", 954 cFYI(1, ("unexpected number of links %d",
959 newinode->i_nlink)); 955 newinode->i_nlink));
960 #endif 956 #endif
961 } 957 }
962 kfree(pInfo); 958 kfree(pInfo);
963 goto mkdir_out; 959 goto mkdir_out;
964 } 960 }
965 mkdir_retry_old: 961 mkdir_retry_old:
966 /* BB add setting the equivalent of mode via CreateX w/ACLs */ 962 /* BB add setting the equivalent of mode via CreateX w/ACLs */
967 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls, 963 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
968 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); 964 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
969 if (rc) { 965 if (rc) {
970 cFYI(1, ("cifs_mkdir returned 0x%x", rc)); 966 cFYI(1, ("cifs_mkdir returned 0x%x", rc));
971 d_drop(direntry); 967 d_drop(direntry);
972 } else { 968 } else {
973 mkdir_get_info: 969 mkdir_get_info:
974 inc_nlink(inode); 970 inc_nlink(inode);
975 if (pTcon->unix_ext) 971 if (pTcon->unix_ext)
976 rc = cifs_get_inode_info_unix(&newinode, full_path, 972 rc = cifs_get_inode_info_unix(&newinode, full_path,
977 inode->i_sb, xid); 973 inode->i_sb, xid);
978 else 974 else
979 rc = cifs_get_inode_info(&newinode, full_path, NULL, 975 rc = cifs_get_inode_info(&newinode, full_path, NULL,
980 inode->i_sb, xid, NULL); 976 inode->i_sb, xid, NULL);
981 977
982 if (pTcon->nocase) 978 if (pTcon->nocase)
983 direntry->d_op = &cifs_ci_dentry_ops; 979 direntry->d_op = &cifs_ci_dentry_ops;
984 else 980 else
985 direntry->d_op = &cifs_dentry_ops; 981 direntry->d_op = &cifs_dentry_ops;
986 d_instantiate(direntry, newinode); 982 d_instantiate(direntry, newinode);
987 /* setting nlink not necessary except in cases where we 983 /* setting nlink not necessary except in cases where we
988 * failed to get it from the server or was set bogus */ 984 * failed to get it from the server or was set bogus */
989 if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2)) 985 if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2))
990 direntry->d_inode->i_nlink = 2; 986 direntry->d_inode->i_nlink = 2;
991 mode &= ~current->fs->umask; 987 mode &= ~current->fs->umask;
992 if (pTcon->unix_ext) { 988 if (pTcon->unix_ext) {
993 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { 989 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
994 CIFSSMBUnixSetPerms(xid, pTcon, full_path, 990 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
995 mode, 991 mode,
996 (__u64)current->fsuid, 992 (__u64)current->fsuid,
997 (__u64)current->fsgid, 993 (__u64)current->fsgid,
998 0 /* dev_t */, 994 0 /* dev_t */,
999 cifs_sb->local_nls, 995 cifs_sb->local_nls,
1000 cifs_sb->mnt_cifs_flags & 996 cifs_sb->mnt_cifs_flags &
1001 CIFS_MOUNT_MAP_SPECIAL_CHR); 997 CIFS_MOUNT_MAP_SPECIAL_CHR);
1002 } else { 998 } else {
1003 CIFSSMBUnixSetPerms(xid, pTcon, full_path, 999 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
1004 mode, (__u64)-1, 1000 mode, (__u64)-1,
1005 (__u64)-1, 0 /* dev_t */, 1001 (__u64)-1, 0 /* dev_t */,
1006 cifs_sb->local_nls, 1002 cifs_sb->local_nls,
1007 cifs_sb->mnt_cifs_flags & 1003 cifs_sb->mnt_cifs_flags &
1008 CIFS_MOUNT_MAP_SPECIAL_CHR); 1004 CIFS_MOUNT_MAP_SPECIAL_CHR);
1009 } 1005 }
1010 } else { 1006 } else {
1011 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) && 1007 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1012 (mode & S_IWUGO) == 0) { 1008 (mode & S_IWUGO) == 0) {
1013 FILE_BASIC_INFO pInfo; 1009 FILE_BASIC_INFO pInfo;
1014 memset(&pInfo, 0, sizeof(pInfo)); 1010 memset(&pInfo, 0, sizeof(pInfo));
1015 pInfo.Attributes = cpu_to_le32(ATTR_READONLY); 1011 pInfo.Attributes = cpu_to_le32(ATTR_READONLY);
1016 CIFSSMBSetTimes(xid, pTcon, full_path, 1012 CIFSSMBSetTimes(xid, pTcon, full_path,
1017 &pInfo, cifs_sb->local_nls, 1013 &pInfo, cifs_sb->local_nls,
1018 cifs_sb->mnt_cifs_flags & 1014 cifs_sb->mnt_cifs_flags &
1019 CIFS_MOUNT_MAP_SPECIAL_CHR); 1015 CIFS_MOUNT_MAP_SPECIAL_CHR);
1020 } 1016 }
1021 if (direntry->d_inode) { 1017 if (direntry->d_inode) {
1022 direntry->d_inode->i_mode = mode; 1018 direntry->d_inode->i_mode = mode;
1023 direntry->d_inode->i_mode |= S_IFDIR; 1019 direntry->d_inode->i_mode |= S_IFDIR;
1024 if (cifs_sb->mnt_cifs_flags & 1020 if (cifs_sb->mnt_cifs_flags &
1025 CIFS_MOUNT_SET_UID) { 1021 CIFS_MOUNT_SET_UID) {
1026 direntry->d_inode->i_uid = 1022 direntry->d_inode->i_uid =
1027 current->fsuid; 1023 current->fsuid;
1028 direntry->d_inode->i_gid = 1024 direntry->d_inode->i_gid =
1029 current->fsgid; 1025 current->fsgid;
1030 } 1026 }
1031 } 1027 }
1032 } 1028 }
1033 } 1029 }
1034 mkdir_out: 1030 mkdir_out:
1035 kfree(full_path); 1031 kfree(full_path);
1036 FreeXid(xid); 1032 FreeXid(xid);
1037 return rc; 1033 return rc;
1038 } 1034 }
1039 1035
1040 int cifs_rmdir(struct inode *inode, struct dentry *direntry) 1036 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1041 { 1037 {
1042 int rc = 0; 1038 int rc = 0;
1043 int xid; 1039 int xid;
1044 struct cifs_sb_info *cifs_sb; 1040 struct cifs_sb_info *cifs_sb;
1045 struct cifsTconInfo *pTcon; 1041 struct cifsTconInfo *pTcon;
1046 char *full_path = NULL; 1042 char *full_path = NULL;
1047 struct cifsInodeInfo *cifsInode; 1043 struct cifsInodeInfo *cifsInode;
1048 1044
1049 cFYI(1, ("cifs_rmdir, inode = 0x%p", inode)); 1045 cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
1050 1046
1051 xid = GetXid(); 1047 xid = GetXid();
1052 1048
1053 cifs_sb = CIFS_SB(inode->i_sb); 1049 cifs_sb = CIFS_SB(inode->i_sb);
1054 pTcon = cifs_sb->tcon; 1050 pTcon = cifs_sb->tcon;
1055 1051
1056 full_path = build_path_from_dentry(direntry); 1052 full_path = build_path_from_dentry(direntry);
1057 if (full_path == NULL) { 1053 if (full_path == NULL) {
1058 FreeXid(xid); 1054 FreeXid(xid);
1059 return -ENOMEM; 1055 return -ENOMEM;
1060 } 1056 }
1061 1057
1062 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls, 1058 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
1063 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); 1059 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1064 1060
1065 if (!rc) { 1061 if (!rc) {
1066 drop_nlink(inode); 1062 drop_nlink(inode);
1067 spin_lock(&direntry->d_inode->i_lock); 1063 spin_lock(&direntry->d_inode->i_lock);
1068 i_size_write(direntry->d_inode, 0); 1064 i_size_write(direntry->d_inode, 0);
1069 clear_nlink(direntry->d_inode); 1065 clear_nlink(direntry->d_inode);
1070 spin_unlock(&direntry->d_inode->i_lock); 1066 spin_unlock(&direntry->d_inode->i_lock);
1071 } 1067 }
1072 1068
1073 cifsInode = CIFS_I(direntry->d_inode); 1069 cifsInode = CIFS_I(direntry->d_inode);
1074 cifsInode->time = 0; /* force revalidate to go get info when 1070 cifsInode->time = 0; /* force revalidate to go get info when
1075 needed */ 1071 needed */
1076 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime = 1072 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
1077 current_fs_time(inode->i_sb); 1073 current_fs_time(inode->i_sb);
1078 1074
1079 kfree(full_path); 1075 kfree(full_path);
1080 FreeXid(xid); 1076 FreeXid(xid);
1081 return rc; 1077 return rc;
1082 } 1078 }
1083 1079
1084 int cifs_rename(struct inode *source_inode, struct dentry *source_direntry, 1080 int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
1085 struct inode *target_inode, struct dentry *target_direntry) 1081 struct inode *target_inode, struct dentry *target_direntry)
1086 { 1082 {
1087 char *fromName; 1083 char *fromName;
1088 char *toName; 1084 char *toName;
1089 struct cifs_sb_info *cifs_sb_source; 1085 struct cifs_sb_info *cifs_sb_source;
1090 struct cifs_sb_info *cifs_sb_target; 1086 struct cifs_sb_info *cifs_sb_target;
1091 struct cifsTconInfo *pTcon; 1087 struct cifsTconInfo *pTcon;
1092 int xid; 1088 int xid;
1093 int rc = 0; 1089 int rc = 0;
1094 1090
1095 xid = GetXid(); 1091 xid = GetXid();
1096 1092
1097 cifs_sb_target = CIFS_SB(target_inode->i_sb); 1093 cifs_sb_target = CIFS_SB(target_inode->i_sb);
1098 cifs_sb_source = CIFS_SB(source_inode->i_sb); 1094 cifs_sb_source = CIFS_SB(source_inode->i_sb);
1099 pTcon = cifs_sb_source->tcon; 1095 pTcon = cifs_sb_source->tcon;
1100 1096
1101 if (pTcon != cifs_sb_target->tcon) { 1097 if (pTcon != cifs_sb_target->tcon) {
1102 FreeXid(xid); 1098 FreeXid(xid);
1103 return -EXDEV; /* BB actually could be allowed if same server, 1099 return -EXDEV; /* BB actually could be allowed if same server,
1104 but different share. 1100 but different share.
1105 Might eventually add support for this */ 1101 Might eventually add support for this */
1106 } 1102 }
1107 1103
1108 /* we already have the rename sem so we do not need to grab it again 1104 /* we already have the rename sem so we do not need to grab it again
1109 here to protect the path integrity */ 1105 here to protect the path integrity */
1110 fromName = build_path_from_dentry(source_direntry); 1106 fromName = build_path_from_dentry(source_direntry);
1111 toName = build_path_from_dentry(target_direntry); 1107 toName = build_path_from_dentry(target_direntry);
1112 if ((fromName == NULL) || (toName == NULL)) { 1108 if ((fromName == NULL) || (toName == NULL)) {
1113 rc = -ENOMEM; 1109 rc = -ENOMEM;
1114 goto cifs_rename_exit; 1110 goto cifs_rename_exit;
1115 } 1111 }
1116 1112
1117 rc = CIFSSMBRename(xid, pTcon, fromName, toName, 1113 rc = CIFSSMBRename(xid, pTcon, fromName, toName,
1118 cifs_sb_source->local_nls, 1114 cifs_sb_source->local_nls,
1119 cifs_sb_source->mnt_cifs_flags & 1115 cifs_sb_source->mnt_cifs_flags &
1120 CIFS_MOUNT_MAP_SPECIAL_CHR); 1116 CIFS_MOUNT_MAP_SPECIAL_CHR);
1121 if (rc == -EEXIST) { 1117 if (rc == -EEXIST) {
1122 /* check if they are the same file because rename of hardlinked 1118 /* check if they are the same file because rename of hardlinked
1123 files is a noop */ 1119 files is a noop */
1124 FILE_UNIX_BASIC_INFO *info_buf_source; 1120 FILE_UNIX_BASIC_INFO *info_buf_source;
1125 FILE_UNIX_BASIC_INFO *info_buf_target; 1121 FILE_UNIX_BASIC_INFO *info_buf_target;
1126 1122
1127 info_buf_source = 1123 info_buf_source =
1128 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); 1124 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1129 if (info_buf_source != NULL) { 1125 if (info_buf_source != NULL) {
1130 info_buf_target = info_buf_source + 1; 1126 info_buf_target = info_buf_source + 1;
1131 if (pTcon->unix_ext) 1127 if (pTcon->unix_ext)
1132 rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName, 1128 rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
1133 info_buf_source, 1129 info_buf_source,
1134 cifs_sb_source->local_nls, 1130 cifs_sb_source->local_nls,
1135 cifs_sb_source->mnt_cifs_flags & 1131 cifs_sb_source->mnt_cifs_flags &
1136 CIFS_MOUNT_MAP_SPECIAL_CHR); 1132 CIFS_MOUNT_MAP_SPECIAL_CHR);
1137 /* else rc is still EEXIST so will fall through to 1133 /* else rc is still EEXIST so will fall through to
1138 unlink the target and retry rename */ 1134 unlink the target and retry rename */
1139 if (rc == 0) { 1135 if (rc == 0) {
1140 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName, 1136 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
1141 info_buf_target, 1137 info_buf_target,
1142 cifs_sb_target->local_nls, 1138 cifs_sb_target->local_nls,
1143 /* remap based on source sb */ 1139 /* remap based on source sb */
1144 cifs_sb_source->mnt_cifs_flags & 1140 cifs_sb_source->mnt_cifs_flags &
1145 CIFS_MOUNT_MAP_SPECIAL_CHR); 1141 CIFS_MOUNT_MAP_SPECIAL_CHR);
1146 } 1142 }
1147 if ((rc == 0) && 1143 if ((rc == 0) &&
1148 (info_buf_source->UniqueId == 1144 (info_buf_source->UniqueId ==
1149 info_buf_target->UniqueId)) { 1145 info_buf_target->UniqueId)) {
1150 /* do not rename since the files are hardlinked which 1146 /* do not rename since the files are hardlinked which
1151 is a noop */ 1147 is a noop */
1152 } else { 1148 } else {
1153 /* we either can not tell the files are hardlinked 1149 /* we either can not tell the files are hardlinked
1154 (as with Windows servers) or files are not 1150 (as with Windows servers) or files are not
1155 hardlinked so delete the target manually before 1151 hardlinked so delete the target manually before
1156 renaming to follow POSIX rather than Windows 1152 renaming to follow POSIX rather than Windows
1157 semantics */ 1153 semantics */
1158 cifs_unlink(target_inode, target_direntry); 1154 cifs_unlink(target_inode, target_direntry);
1159 rc = CIFSSMBRename(xid, pTcon, fromName, 1155 rc = CIFSSMBRename(xid, pTcon, fromName,
1160 toName, 1156 toName,
1161 cifs_sb_source->local_nls, 1157 cifs_sb_source->local_nls,
1162 cifs_sb_source->mnt_cifs_flags 1158 cifs_sb_source->mnt_cifs_flags
1163 & CIFS_MOUNT_MAP_SPECIAL_CHR); 1159 & CIFS_MOUNT_MAP_SPECIAL_CHR);
1164 } 1160 }
1165 kfree(info_buf_source); 1161 kfree(info_buf_source);
1166 } /* if we can not get memory just leave rc as EEXIST */ 1162 } /* if we can not get memory just leave rc as EEXIST */
1167 } 1163 }
1168 1164
1169 if (rc) 1165 if (rc)
1170 cFYI(1, ("rename rc %d", rc)); 1166 cFYI(1, ("rename rc %d", rc));
1171 1167
1172 if ((rc == -EIO) || (rc == -EEXIST)) { 1168 if ((rc == -EIO) || (rc == -EEXIST)) {
1173 int oplock = 0; 1169 int oplock = 0;
1174 __u16 netfid; 1170 __u16 netfid;
1175 1171
1176 /* BB FIXME Is Generic Read correct for rename? */ 1172 /* BB FIXME Is Generic Read correct for rename? */
1177 /* if renaming directory - we should not say CREATE_NOT_DIR, 1173 /* if renaming directory - we should not say CREATE_NOT_DIR,
1178 need to test renaming open directory, also GENERIC_READ 1174 need to test renaming open directory, also GENERIC_READ
1179 might not right be right access to request */ 1175 might not right be right access to request */
1180 rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ, 1176 rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
1181 CREATE_NOT_DIR, &netfid, &oplock, NULL, 1177 CREATE_NOT_DIR, &netfid, &oplock, NULL,
1182 cifs_sb_source->local_nls, 1178 cifs_sb_source->local_nls,
1183 cifs_sb_source->mnt_cifs_flags & 1179 cifs_sb_source->mnt_cifs_flags &
1184 CIFS_MOUNT_MAP_SPECIAL_CHR); 1180 CIFS_MOUNT_MAP_SPECIAL_CHR);
1185 if (rc == 0) { 1181 if (rc == 0) {
1186 rc = CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName, 1182 rc = CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
1187 cifs_sb_source->local_nls, 1183 cifs_sb_source->local_nls,
1188 cifs_sb_source->mnt_cifs_flags & 1184 cifs_sb_source->mnt_cifs_flags &
1189 CIFS_MOUNT_MAP_SPECIAL_CHR); 1185 CIFS_MOUNT_MAP_SPECIAL_CHR);
1190 CIFSSMBClose(xid, pTcon, netfid); 1186 CIFSSMBClose(xid, pTcon, netfid);
1191 } 1187 }
1192 } 1188 }
1193 1189
1194 cifs_rename_exit: 1190 cifs_rename_exit:
1195 kfree(fromName); 1191 kfree(fromName);
1196 kfree(toName); 1192 kfree(toName);
1197 FreeXid(xid); 1193 FreeXid(xid);
1198 return rc; 1194 return rc;
1199 } 1195 }
1200 1196
1201 int cifs_revalidate(struct dentry *direntry) 1197 int cifs_revalidate(struct dentry *direntry)
1202 { 1198 {
1203 int xid; 1199 int xid;
1204 int rc = 0, wbrc = 0; 1200 int rc = 0, wbrc = 0;
1205 char *full_path; 1201 char *full_path;
1206 struct cifs_sb_info *cifs_sb; 1202 struct cifs_sb_info *cifs_sb;
1207 struct cifsInodeInfo *cifsInode; 1203 struct cifsInodeInfo *cifsInode;
1208 loff_t local_size; 1204 loff_t local_size;
1209 struct timespec local_mtime; 1205 struct timespec local_mtime;
1210 bool invalidate_inode = false; 1206 bool invalidate_inode = false;
1211 1207
1212 if (direntry->d_inode == NULL) 1208 if (direntry->d_inode == NULL)
1213 return -ENOENT; 1209 return -ENOENT;
1214 1210
1215 cifsInode = CIFS_I(direntry->d_inode); 1211 cifsInode = CIFS_I(direntry->d_inode);
1216 1212
1217 if (cifsInode == NULL) 1213 if (cifsInode == NULL)
1218 return -ENOENT; 1214 return -ENOENT;
1219 1215
1220 /* no sense revalidating inode info on file that no one can write */ 1216 /* no sense revalidating inode info on file that no one can write */
1221 if (CIFS_I(direntry->d_inode)->clientCanCacheRead) 1217 if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
1222 return rc; 1218 return rc;
1223 1219
1224 xid = GetXid(); 1220 xid = GetXid();
1225 1221
1226 cifs_sb = CIFS_SB(direntry->d_sb); 1222 cifs_sb = CIFS_SB(direntry->d_sb);
1227 1223
1228 /* can not safely grab the rename sem here if rename calls revalidate 1224 /* can not safely grab the rename sem here if rename calls revalidate
1229 since that would deadlock */ 1225 since that would deadlock */
1230 full_path = build_path_from_dentry(direntry); 1226 full_path = build_path_from_dentry(direntry);
1231 if (full_path == NULL) { 1227 if (full_path == NULL) {
1232 FreeXid(xid); 1228 FreeXid(xid);
1233 return -ENOMEM; 1229 return -ENOMEM;
1234 } 1230 }
1235 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld " 1231 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
1236 "jiffies %ld", full_path, direntry->d_inode, 1232 "jiffies %ld", full_path, direntry->d_inode,
1237 direntry->d_inode->i_count.counter, direntry, 1233 direntry->d_inode->i_count.counter, direntry,
1238 direntry->d_time, jiffies)); 1234 direntry->d_time, jiffies));
1239 1235
1240 if (cifsInode->time == 0) { 1236 if (cifsInode->time == 0) {
1241 /* was set to zero previously to force revalidate */ 1237 /* was set to zero previously to force revalidate */
1242 } else if (time_before(jiffies, cifsInode->time + HZ) && 1238 } else if (time_before(jiffies, cifsInode->time + HZ) &&
1243 lookupCacheEnabled) { 1239 lookupCacheEnabled) {
1244 if ((S_ISREG(direntry->d_inode->i_mode) == 0) || 1240 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
1245 (direntry->d_inode->i_nlink == 1)) { 1241 (direntry->d_inode->i_nlink == 1)) {
1246 kfree(full_path); 1242 kfree(full_path);
1247 FreeXid(xid); 1243 FreeXid(xid);
1248 return rc; 1244 return rc;
1249 } else { 1245 } else {
1250 cFYI(1, ("Have to revalidate file due to hardlinks")); 1246 cFYI(1, ("Have to revalidate file due to hardlinks"));
1251 } 1247 }
1252 } 1248 }
1253 1249
1254 /* save mtime and size */ 1250 /* save mtime and size */
1255 local_mtime = direntry->d_inode->i_mtime; 1251 local_mtime = direntry->d_inode->i_mtime;
1256 local_size = direntry->d_inode->i_size; 1252 local_size = direntry->d_inode->i_size;
1257 1253
1258 if (cifs_sb->tcon->unix_ext) { 1254 if (cifs_sb->tcon->unix_ext) {
1259 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path, 1255 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
1260 direntry->d_sb, xid); 1256 direntry->d_sb, xid);
1261 if (rc) { 1257 if (rc) {
1262 cFYI(1, ("error on getting revalidate info %d", rc)); 1258 cFYI(1, ("error on getting revalidate info %d", rc));
1263 /* if (rc != -ENOENT) 1259 /* if (rc != -ENOENT)
1264 rc = 0; */ /* BB should we cache info on 1260 rc = 0; */ /* BB should we cache info on
1265 certain errors? */ 1261 certain errors? */
1266 } 1262 }
1267 } else { 1263 } else {
1268 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL, 1264 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
1269 direntry->d_sb, xid, NULL); 1265 direntry->d_sb, xid, NULL);
1270 if (rc) { 1266 if (rc) {
1271 cFYI(1, ("error on getting revalidate info %d", rc)); 1267 cFYI(1, ("error on getting revalidate info %d", rc));
1272 /* if (rc != -ENOENT) 1268 /* if (rc != -ENOENT)
1273 rc = 0; */ /* BB should we cache info on 1269 rc = 0; */ /* BB should we cache info on
1274 certain errors? */ 1270 certain errors? */
1275 } 1271 }
1276 } 1272 }
1277 /* should we remap certain errors, access denied?, to zero */ 1273 /* should we remap certain errors, access denied?, to zero */
1278 1274
1279 /* if not oplocked, we invalidate inode pages if mtime or file size 1275 /* if not oplocked, we invalidate inode pages if mtime or file size
1280 had changed on server */ 1276 had changed on server */
1281 1277
1282 if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) && 1278 if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) &&
1283 (local_size == direntry->d_inode->i_size)) { 1279 (local_size == direntry->d_inode->i_size)) {
1284 cFYI(1, ("cifs_revalidate - inode unchanged")); 1280 cFYI(1, ("cifs_revalidate - inode unchanged"));
1285 } else { 1281 } else {
1286 /* file may have changed on server */ 1282 /* file may have changed on server */
1287 if (cifsInode->clientCanCacheRead) { 1283 if (cifsInode->clientCanCacheRead) {
1288 /* no need to invalidate inode pages since we were the 1284 /* no need to invalidate inode pages since we were the
1289 only ones who could have modified the file and the 1285 only ones who could have modified the file and the
1290 server copy is staler than ours */ 1286 server copy is staler than ours */
1291 } else { 1287 } else {
1292 invalidate_inode = true; 1288 invalidate_inode = true;
1293 } 1289 }
1294 } 1290 }
1295 1291
1296 /* can not grab this sem since kernel filesys locking documentation 1292 /* can not grab this sem since kernel filesys locking documentation
1297 indicates i_mutex may be taken by the kernel on lookup and rename 1293 indicates i_mutex may be taken by the kernel on lookup and rename
1298 which could deadlock if we grab the i_mutex here as well */ 1294 which could deadlock if we grab the i_mutex here as well */
1299 /* mutex_lock(&direntry->d_inode->i_mutex);*/ 1295 /* mutex_lock(&direntry->d_inode->i_mutex);*/
1300 /* need to write out dirty pages here */ 1296 /* need to write out dirty pages here */
1301 if (direntry->d_inode->i_mapping) { 1297 if (direntry->d_inode->i_mapping) {
1302 /* do we need to lock inode until after invalidate completes 1298 /* do we need to lock inode until after invalidate completes
1303 below? */ 1299 below? */
1304 wbrc = filemap_fdatawrite(direntry->d_inode->i_mapping); 1300 wbrc = filemap_fdatawrite(direntry->d_inode->i_mapping);
1305 if (wbrc) 1301 if (wbrc)
1306 CIFS_I(direntry->d_inode)->write_behind_rc = wbrc; 1302 CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
1307 } 1303 }
1308 if (invalidate_inode) { 1304 if (invalidate_inode) {
1309 /* shrink_dcache not necessary now that cifs dentry ops 1305 /* shrink_dcache not necessary now that cifs dentry ops
1310 are exported for negative dentries */ 1306 are exported for negative dentries */
1311 /* if (S_ISDIR(direntry->d_inode->i_mode)) 1307 /* if (S_ISDIR(direntry->d_inode->i_mode))
1312 shrink_dcache_parent(direntry); */ 1308 shrink_dcache_parent(direntry); */
1313 if (S_ISREG(direntry->d_inode->i_mode)) { 1309 if (S_ISREG(direntry->d_inode->i_mode)) {
1314 if (direntry->d_inode->i_mapping) 1310 if (direntry->d_inode->i_mapping)
1315 wbrc = filemap_fdatawait(direntry->d_inode->i_mapping); 1311 wbrc = filemap_fdatawait(direntry->d_inode->i_mapping);
1316 if (wbrc) 1312 if (wbrc)
1317 CIFS_I(direntry->d_inode)->write_behind_rc = wbrc; 1313 CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
1318 /* may eventually have to do this for open files too */ 1314 /* may eventually have to do this for open files too */
1319 if (list_empty(&(cifsInode->openFileList))) { 1315 if (list_empty(&(cifsInode->openFileList))) {
1320 /* changed on server - flush read ahead pages */ 1316 /* changed on server - flush read ahead pages */
1321 cFYI(1, ("Invalidating read ahead data on " 1317 cFYI(1, ("Invalidating read ahead data on "
1322 "closed file")); 1318 "closed file"));
1323 invalidate_remote_inode(direntry->d_inode); 1319 invalidate_remote_inode(direntry->d_inode);
1324 } 1320 }
1325 } 1321 }
1326 } 1322 }
1327 /* mutex_unlock(&direntry->d_inode->i_mutex); */ 1323 /* mutex_unlock(&direntry->d_inode->i_mutex); */
1328 1324
1329 kfree(full_path); 1325 kfree(full_path);
1330 FreeXid(xid); 1326 FreeXid(xid);
1331 return rc; 1327 return rc;
1332 } 1328 }
1333 1329
1334 int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry, 1330 int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1335 struct kstat *stat) 1331 struct kstat *stat)
1336 { 1332 {
1337 int err = cifs_revalidate(dentry); 1333 int err = cifs_revalidate(dentry);
1338 if (!err) { 1334 if (!err) {
1339 generic_fillattr(dentry->d_inode, stat); 1335 generic_fillattr(dentry->d_inode, stat);
1340 stat->blksize = CIFS_MAX_MSGSIZE; 1336 stat->blksize = CIFS_MAX_MSGSIZE;
1341 } 1337 }
1342 return err; 1338 return err;
1343 } 1339 }
1344 1340
1345 static int cifs_truncate_page(struct address_space *mapping, loff_t from) 1341 static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1346 { 1342 {
1347 pgoff_t index = from >> PAGE_CACHE_SHIFT; 1343 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1348 unsigned offset = from & (PAGE_CACHE_SIZE - 1); 1344 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1349 struct page *page; 1345 struct page *page;
1350 int rc = 0; 1346 int rc = 0;
1351 1347
1352 page = grab_cache_page(mapping, index); 1348 page = grab_cache_page(mapping, index);
1353 if (!page) 1349 if (!page)
1354 return -ENOMEM; 1350 return -ENOMEM;
1355 1351
1356 zero_user_segment(page, offset, PAGE_CACHE_SIZE); 1352 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
1357 unlock_page(page); 1353 unlock_page(page);
1358 page_cache_release(page); 1354 page_cache_release(page);
1359 return rc; 1355 return rc;
1360 } 1356 }
1361 1357
1362 static int cifs_vmtruncate(struct inode *inode, loff_t offset) 1358 static int cifs_vmtruncate(struct inode *inode, loff_t offset)
1363 { 1359 {
1364 struct address_space *mapping = inode->i_mapping; 1360 struct address_space *mapping = inode->i_mapping;
1365 unsigned long limit; 1361 unsigned long limit;
1366 1362
1367 spin_lock(&inode->i_lock); 1363 spin_lock(&inode->i_lock);
1368 if (inode->i_size < offset) 1364 if (inode->i_size < offset)
1369 goto do_expand; 1365 goto do_expand;
1370 /* 1366 /*
1371 * truncation of in-use swapfiles is disallowed - it would cause 1367 * truncation of in-use swapfiles is disallowed - it would cause
1372 * subsequent swapout to scribble on the now-freed blocks. 1368 * subsequent swapout to scribble on the now-freed blocks.
1373 */ 1369 */
1374 if (IS_SWAPFILE(inode)) { 1370 if (IS_SWAPFILE(inode)) {
1375 spin_unlock(&inode->i_lock); 1371 spin_unlock(&inode->i_lock);
1376 goto out_busy; 1372 goto out_busy;
1377 } 1373 }
1378 i_size_write(inode, offset); 1374 i_size_write(inode, offset);
1379 spin_unlock(&inode->i_lock); 1375 spin_unlock(&inode->i_lock);
1380 /* 1376 /*
1381 * unmap_mapping_range is called twice, first simply for efficiency 1377 * unmap_mapping_range is called twice, first simply for efficiency
1382 * so that truncate_inode_pages does fewer single-page unmaps. However 1378 * so that truncate_inode_pages does fewer single-page unmaps. However
1383 * after this first call, and before truncate_inode_pages finishes, 1379 * after this first call, and before truncate_inode_pages finishes,
1384 * it is possible for private pages to be COWed, which remain after 1380 * it is possible for private pages to be COWed, which remain after
1385 * truncate_inode_pages finishes, hence the second unmap_mapping_range 1381 * truncate_inode_pages finishes, hence the second unmap_mapping_range
1386 * call must be made for correctness. 1382 * call must be made for correctness.
1387 */ 1383 */
1388 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); 1384 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1389 truncate_inode_pages(mapping, offset); 1385 truncate_inode_pages(mapping, offset);
1390 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); 1386 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1391 goto out_truncate; 1387 goto out_truncate;
1392 1388
1393 do_expand: 1389 do_expand:
1394 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; 1390 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1395 if (limit != RLIM_INFINITY && offset > limit) { 1391 if (limit != RLIM_INFINITY && offset > limit) {
1396 spin_unlock(&inode->i_lock); 1392 spin_unlock(&inode->i_lock);
1397 goto out_sig; 1393 goto out_sig;
1398 } 1394 }
1399 if (offset > inode->i_sb->s_maxbytes) { 1395 if (offset > inode->i_sb->s_maxbytes) {
1400 spin_unlock(&inode->i_lock); 1396 spin_unlock(&inode->i_lock);
1401 goto out_big; 1397 goto out_big;
1402 } 1398 }
1403 i_size_write(inode, offset); 1399 i_size_write(inode, offset);
1404 spin_unlock(&inode->i_lock); 1400 spin_unlock(&inode->i_lock);
1405 out_truncate: 1401 out_truncate:
1406 if (inode->i_op && inode->i_op->truncate) 1402 if (inode->i_op && inode->i_op->truncate)
1407 inode->i_op->truncate(inode); 1403 inode->i_op->truncate(inode);
1408 return 0; 1404 return 0;
1409 out_sig: 1405 out_sig:
1410 send_sig(SIGXFSZ, current, 0); 1406 send_sig(SIGXFSZ, current, 0);
1411 out_big: 1407 out_big:
1412 return -EFBIG; 1408 return -EFBIG;
1413 out_busy: 1409 out_busy:
1414 return -ETXTBSY; 1410 return -ETXTBSY;
1415 } 1411 }
1416 1412
1417 int cifs_setattr(struct dentry *direntry, struct iattr *attrs) 1413 int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
1418 { 1414 {
1419 int xid; 1415 int xid;
1420 struct cifs_sb_info *cifs_sb; 1416 struct cifs_sb_info *cifs_sb;
1421 struct cifsTconInfo *pTcon; 1417 struct cifsTconInfo *pTcon;
1422 char *full_path = NULL; 1418 char *full_path = NULL;
1423 int rc = -EACCES; 1419 int rc = -EACCES;
1424 struct cifsFileInfo *open_file = NULL; 1420 struct cifsFileInfo *open_file = NULL;
1425 FILE_BASIC_INFO time_buf; 1421 FILE_BASIC_INFO time_buf;
1426 bool set_time = false; 1422 bool set_time = false;
1427 bool set_dosattr = false; 1423 bool set_dosattr = false;
1428 __u64 mode = 0xFFFFFFFFFFFFFFFFULL; 1424 __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
1429 __u64 uid = 0xFFFFFFFFFFFFFFFFULL; 1425 __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
1430 __u64 gid = 0xFFFFFFFFFFFFFFFFULL; 1426 __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
1431 struct cifsInodeInfo *cifsInode; 1427 struct cifsInodeInfo *cifsInode;
1432 struct inode *inode = direntry->d_inode; 1428 struct inode *inode = direntry->d_inode;
1433 1429
1434 xid = GetXid(); 1430 xid = GetXid();
1435 1431
1436 cFYI(1, ("setattr on file %s attrs->iavalid 0x%x", 1432 cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
1437 direntry->d_name.name, attrs->ia_valid)); 1433 direntry->d_name.name, attrs->ia_valid));
1438 1434
1439 cifs_sb = CIFS_SB(inode->i_sb); 1435 cifs_sb = CIFS_SB(inode->i_sb);
1440 pTcon = cifs_sb->tcon; 1436 pTcon = cifs_sb->tcon;
1441 1437
1442 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) { 1438 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
1443 /* check if we have permission to change attrs */ 1439 /* check if we have permission to change attrs */
1444 rc = inode_change_ok(inode, attrs); 1440 rc = inode_change_ok(inode, attrs);
1445 if (rc < 0) { 1441 if (rc < 0) {
1446 FreeXid(xid); 1442 FreeXid(xid);
1447 return rc; 1443 return rc;
1448 } else 1444 } else
1449 rc = 0; 1445 rc = 0;
1450 } 1446 }
1451 1447
1452 full_path = build_path_from_dentry(direntry); 1448 full_path = build_path_from_dentry(direntry);
1453 if (full_path == NULL) { 1449 if (full_path == NULL) {
1454 FreeXid(xid); 1450 FreeXid(xid);
1455 return -ENOMEM; 1451 return -ENOMEM;
1456 } 1452 }
1457 cifsInode = CIFS_I(inode); 1453 cifsInode = CIFS_I(inode);
1458 1454
1459 if ((attrs->ia_valid & ATTR_MTIME) || (attrs->ia_valid & ATTR_SIZE)) { 1455 if ((attrs->ia_valid & ATTR_MTIME) || (attrs->ia_valid & ATTR_SIZE)) {
1460 /* 1456 /*
1461 Flush data before changing file size or changing the last 1457 Flush data before changing file size or changing the last
1462 write time of the file on the server. If the 1458 write time of the file on the server. If the
1463 flush returns error, store it to report later and continue. 1459 flush returns error, store it to report later and continue.
1464 BB: This should be smarter. Why bother flushing pages that 1460 BB: This should be smarter. Why bother flushing pages that
1465 will be truncated anyway? Also, should we error out here if 1461 will be truncated anyway? Also, should we error out here if
1466 the flush returns error? 1462 the flush returns error?
1467 */ 1463 */
1468 rc = filemap_write_and_wait(inode->i_mapping); 1464 rc = filemap_write_and_wait(inode->i_mapping);
1469 if (rc != 0) { 1465 if (rc != 0) {
1470 cifsInode->write_behind_rc = rc; 1466 cifsInode->write_behind_rc = rc;
1471 rc = 0; 1467 rc = 0;
1472 } 1468 }
1473 } 1469 }
1474 1470
1475 if (attrs->ia_valid & ATTR_SIZE) { 1471 if (attrs->ia_valid & ATTR_SIZE) {
1476 /* To avoid spurious oplock breaks from server, in the case of 1472 /* To avoid spurious oplock breaks from server, in the case of
1477 inodes that we already have open, avoid doing path based 1473 inodes that we already have open, avoid doing path based
1478 setting of file size if we can do it by handle. 1474 setting of file size if we can do it by handle.
1479 This keeps our caching token (oplock) and avoids timeouts 1475 This keeps our caching token (oplock) and avoids timeouts
1480 when the local oplock break takes longer to flush 1476 when the local oplock break takes longer to flush
1481 writebehind data than the SMB timeout for the SetPathInfo 1477 writebehind data than the SMB timeout for the SetPathInfo
1482 request would allow */ 1478 request would allow */
1483 1479
1484 open_file = find_writable_file(cifsInode); 1480 open_file = find_writable_file(cifsInode);
1485 if (open_file) { 1481 if (open_file) {
1486 __u16 nfid = open_file->netfid; 1482 __u16 nfid = open_file->netfid;
1487 __u32 npid = open_file->pid; 1483 __u32 npid = open_file->pid;
1488 rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size, 1484 rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size,
1489 nfid, npid, false); 1485 nfid, npid, false);
1490 atomic_dec(&open_file->wrtPending); 1486 atomic_dec(&open_file->wrtPending);
1491 cFYI(1, ("SetFSize for attrs rc = %d", rc)); 1487 cFYI(1, ("SetFSize for attrs rc = %d", rc));
1492 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { 1488 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1493 unsigned int bytes_written; 1489 unsigned int bytes_written;
1494 rc = CIFSSMBWrite(xid, pTcon, 1490 rc = CIFSSMBWrite(xid, pTcon,
1495 nfid, 0, attrs->ia_size, 1491 nfid, 0, attrs->ia_size,
1496 &bytes_written, NULL, NULL, 1492 &bytes_written, NULL, NULL,
1497 1 /* 45 seconds */); 1493 1 /* 45 seconds */);
1498 cFYI(1, ("Wrt seteof rc %d", rc)); 1494 cFYI(1, ("Wrt seteof rc %d", rc));
1499 } 1495 }
1500 } else 1496 } else
1501 rc = -EINVAL; 1497 rc = -EINVAL;
1502 1498
1503 if (rc != 0) { 1499 if (rc != 0) {
1504 /* Set file size by pathname rather than by handle 1500 /* Set file size by pathname rather than by handle
1505 either because no valid, writeable file handle for 1501 either because no valid, writeable file handle for
1506 it was found or because there was an error setting 1502 it was found or because there was an error setting
1507 it by handle */ 1503 it by handle */
1508 rc = CIFSSMBSetEOF(xid, pTcon, full_path, 1504 rc = CIFSSMBSetEOF(xid, pTcon, full_path,
1509 attrs->ia_size, false, 1505 attrs->ia_size, false,
1510 cifs_sb->local_nls, 1506 cifs_sb->local_nls,
1511 cifs_sb->mnt_cifs_flags & 1507 cifs_sb->mnt_cifs_flags &
1512 CIFS_MOUNT_MAP_SPECIAL_CHR); 1508 CIFS_MOUNT_MAP_SPECIAL_CHR);
1513 cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc)); 1509 cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
1514 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { 1510 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1515 __u16 netfid; 1511 __u16 netfid;
1516 int oplock = 0; 1512 int oplock = 0;
1517 1513
1518 rc = SMBLegacyOpen(xid, pTcon, full_path, 1514 rc = SMBLegacyOpen(xid, pTcon, full_path,
1519 FILE_OPEN, GENERIC_WRITE, 1515 FILE_OPEN, GENERIC_WRITE,
1520 CREATE_NOT_DIR, &netfid, &oplock, 1516 CREATE_NOT_DIR, &netfid, &oplock,
1521 NULL, cifs_sb->local_nls, 1517 NULL, cifs_sb->local_nls,
1522 cifs_sb->mnt_cifs_flags & 1518 cifs_sb->mnt_cifs_flags &
1523 CIFS_MOUNT_MAP_SPECIAL_CHR); 1519 CIFS_MOUNT_MAP_SPECIAL_CHR);
1524 if (rc == 0) { 1520 if (rc == 0) {
1525 unsigned int bytes_written; 1521 unsigned int bytes_written;
1526 rc = CIFSSMBWrite(xid, pTcon, 1522 rc = CIFSSMBWrite(xid, pTcon,
1527 netfid, 0, 1523 netfid, 0,
1528 attrs->ia_size, 1524 attrs->ia_size,
1529 &bytes_written, NULL, 1525 &bytes_written, NULL,
1530 NULL, 1 /* 45 sec */); 1526 NULL, 1 /* 45 sec */);
1531 cFYI(1, ("wrt seteof rc %d", rc)); 1527 cFYI(1, ("wrt seteof rc %d", rc));
1532 CIFSSMBClose(xid, pTcon, netfid); 1528 CIFSSMBClose(xid, pTcon, netfid);
1533 } 1529 }
1534 1530
1535 } 1531 }
1536 } 1532 }
1537 1533
1538 /* Server is ok setting allocation size implicitly - no need 1534 /* Server is ok setting allocation size implicitly - no need
1539 to call: 1535 to call:
1540 CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, true, 1536 CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, true,
1541 cifs_sb->local_nls); 1537 cifs_sb->local_nls);
1542 */ 1538 */
1543 1539
1544 if (rc == 0) { 1540 if (rc == 0) {
1545 rc = cifs_vmtruncate(inode, attrs->ia_size); 1541 rc = cifs_vmtruncate(inode, attrs->ia_size);
1546 cifs_truncate_page(inode->i_mapping, inode->i_size); 1542 cifs_truncate_page(inode->i_mapping, inode->i_size);
1547 } else 1543 } else
1548 goto cifs_setattr_exit; 1544 goto cifs_setattr_exit;
1549 } 1545 }
1550 if (attrs->ia_valid & ATTR_UID) { 1546 if (attrs->ia_valid & ATTR_UID) {
1551 cFYI(1, ("UID changed to %d", attrs->ia_uid)); 1547 cFYI(1, ("UID changed to %d", attrs->ia_uid));
1552 uid = attrs->ia_uid; 1548 uid = attrs->ia_uid;
1553 } 1549 }
1554 if (attrs->ia_valid & ATTR_GID) { 1550 if (attrs->ia_valid & ATTR_GID) {
1555 cFYI(1, ("GID changed to %d", attrs->ia_gid)); 1551 cFYI(1, ("GID changed to %d", attrs->ia_gid));
1556 gid = attrs->ia_gid; 1552 gid = attrs->ia_gid;
1557 } 1553 }
1558 1554
1559 time_buf.Attributes = 0; 1555 time_buf.Attributes = 0;
1560 1556
1561 /* skip mode change if it's just for clearing setuid/setgid */ 1557 /* skip mode change if it's just for clearing setuid/setgid */
1562 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) 1558 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1563 attrs->ia_valid &= ~ATTR_MODE; 1559 attrs->ia_valid &= ~ATTR_MODE;
1564 1560
1565 if (attrs->ia_valid & ATTR_MODE) { 1561 if (attrs->ia_valid & ATTR_MODE) {
1566 cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode)); 1562 cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode));
1567 mode = attrs->ia_mode; 1563 mode = attrs->ia_mode;
1568 } 1564 }
1569 1565
1570 if ((pTcon->unix_ext) 1566 if ((pTcon->unix_ext)
1571 && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID))) 1567 && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID)))
1572 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid, 1568 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid,
1573 0 /* dev_t */, cifs_sb->local_nls, 1569 0 /* dev_t */, cifs_sb->local_nls,
1574 cifs_sb->mnt_cifs_flags & 1570 cifs_sb->mnt_cifs_flags &
1575 CIFS_MOUNT_MAP_SPECIAL_CHR); 1571 CIFS_MOUNT_MAP_SPECIAL_CHR);
1576 else if (attrs->ia_valid & ATTR_MODE) { 1572 else if (attrs->ia_valid & ATTR_MODE) {
1577 rc = 0; 1573 rc = 0;
1578 #ifdef CONFIG_CIFS_EXPERIMENTAL 1574 #ifdef CONFIG_CIFS_EXPERIMENTAL
1579 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) 1575 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
1580 rc = mode_to_acl(inode, full_path, mode); 1576 rc = mode_to_acl(inode, full_path, mode);
1581 else if ((mode & S_IWUGO) == 0) { 1577 else if ((mode & S_IWUGO) == 0) {
1582 #else 1578 #else
1583 if ((mode & S_IWUGO) == 0) { 1579 if ((mode & S_IWUGO) == 0) {
1584 #endif 1580 #endif
1585 /* not writeable */ 1581 /* not writeable */
1586 if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0) { 1582 if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
1587 set_dosattr = true; 1583 set_dosattr = true;
1588 time_buf.Attributes = 1584 time_buf.Attributes =
1589 cpu_to_le32(cifsInode->cifsAttrs | 1585 cpu_to_le32(cifsInode->cifsAttrs |
1590 ATTR_READONLY); 1586 ATTR_READONLY);
1591 } 1587 }
1592 } else if (cifsInode->cifsAttrs & ATTR_READONLY) { 1588 } else if (cifsInode->cifsAttrs & ATTR_READONLY) {
1593 /* If file is readonly on server, we would 1589 /* If file is readonly on server, we would
1594 not be able to write to it - so if any write 1590 not be able to write to it - so if any write
1595 bit is enabled for user or group or other we 1591 bit is enabled for user or group or other we
1596 need to at least try to remove r/o dos attr */ 1592 need to at least try to remove r/o dos attr */
1597 set_dosattr = true; 1593 set_dosattr = true;
1598 time_buf.Attributes = cpu_to_le32(cifsInode->cifsAttrs & 1594 time_buf.Attributes = cpu_to_le32(cifsInode->cifsAttrs &
1599 (~ATTR_READONLY)); 1595 (~ATTR_READONLY));
1600 /* Windows ignores set to zero */ 1596 /* Windows ignores set to zero */
1601 if (time_buf.Attributes == 0) 1597 if (time_buf.Attributes == 0)
1602 time_buf.Attributes |= cpu_to_le32(ATTR_NORMAL); 1598 time_buf.Attributes |= cpu_to_le32(ATTR_NORMAL);
1603 } 1599 }
1604 } 1600 }
1605 1601
1606 if (attrs->ia_valid & ATTR_ATIME) { 1602 if (attrs->ia_valid & ATTR_ATIME) {
1607 set_time = true; 1603 set_time = true;
1608 time_buf.LastAccessTime = 1604 time_buf.LastAccessTime =
1609 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime)); 1605 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1610 } else 1606 } else
1611 time_buf.LastAccessTime = 0; 1607 time_buf.LastAccessTime = 0;
1612 1608
1613 if (attrs->ia_valid & ATTR_MTIME) { 1609 if (attrs->ia_valid & ATTR_MTIME) {
1614 set_time = true; 1610 set_time = true;
1615 time_buf.LastWriteTime = 1611 time_buf.LastWriteTime =
1616 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime)); 1612 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1617 } else 1613 } else
1618 time_buf.LastWriteTime = 0; 1614 time_buf.LastWriteTime = 0;
1619 /* Do not set ctime explicitly unless other time 1615 /* Do not set ctime explicitly unless other time
1620 stamps are changed explicitly (i.e. by utime() 1616 stamps are changed explicitly (i.e. by utime()
1621 since we would then have a mix of client and 1617 since we would then have a mix of client and
1622 server times */ 1618 server times */
1623 1619
1624 if (set_time && (attrs->ia_valid & ATTR_CTIME)) { 1620 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1625 set_time = true; 1621 set_time = true;
1626 /* Although Samba throws this field away 1622 /* Although Samba throws this field away
1627 it may be useful to Windows - but we do 1623 it may be useful to Windows - but we do
1628 not want to set ctime unless some other 1624 not want to set ctime unless some other
1629 timestamp is changing */ 1625 timestamp is changing */
1630 cFYI(1, ("CIFS - CTIME changed")); 1626 cFYI(1, ("CIFS - CTIME changed"));
1631 time_buf.ChangeTime = 1627 time_buf.ChangeTime =
1632 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime)); 1628 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1633 } else 1629 } else
1634 time_buf.ChangeTime = 0; 1630 time_buf.ChangeTime = 0;
1635 1631
1636 if (set_time || set_dosattr) { 1632 if (set_time || set_dosattr) {
1637 time_buf.CreationTime = 0; /* do not change */ 1633 time_buf.CreationTime = 0; /* do not change */
1638 /* In the future we should experiment - try setting timestamps 1634 /* In the future we should experiment - try setting timestamps
1639 via Handle (SetFileInfo) instead of by path */ 1635 via Handle (SetFileInfo) instead of by path */
1640 if (!(pTcon->ses->flags & CIFS_SES_NT4)) 1636 if (!(pTcon->ses->flags & CIFS_SES_NT4))
1641 rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf, 1637 rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf,
1642 cifs_sb->local_nls, 1638 cifs_sb->local_nls,
1643 cifs_sb->mnt_cifs_flags & 1639 cifs_sb->mnt_cifs_flags &
1644 CIFS_MOUNT_MAP_SPECIAL_CHR); 1640 CIFS_MOUNT_MAP_SPECIAL_CHR);
1645 else 1641 else
1646 rc = -EOPNOTSUPP; 1642 rc = -EOPNOTSUPP;
1647 1643
1648 if (rc == -EOPNOTSUPP) { 1644 if (rc == -EOPNOTSUPP) {
1649 int oplock = 0; 1645 int oplock = 0;
1650 __u16 netfid; 1646 __u16 netfid;
1651 1647
1652 cFYI(1, ("calling SetFileInfo since SetPathInfo for " 1648 cFYI(1, ("calling SetFileInfo since SetPathInfo for "
1653 "times not supported by this server")); 1649 "times not supported by this server"));
1654 /* BB we could scan to see if we already have it open 1650 /* BB we could scan to see if we already have it open
1655 and pass in pid of opener to function */ 1651 and pass in pid of opener to function */
1656 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, 1652 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
1657 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES, 1653 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1658 CREATE_NOT_DIR, &netfid, &oplock, 1654 CREATE_NOT_DIR, &netfid, &oplock,
1659 NULL, cifs_sb->local_nls, 1655 NULL, cifs_sb->local_nls,
1660 cifs_sb->mnt_cifs_flags & 1656 cifs_sb->mnt_cifs_flags &
1661 CIFS_MOUNT_MAP_SPECIAL_CHR); 1657 CIFS_MOUNT_MAP_SPECIAL_CHR);
1662 if (rc == 0) { 1658 if (rc == 0) {
1663 rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf, 1659 rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
1664 netfid); 1660 netfid);
1665 CIFSSMBClose(xid, pTcon, netfid); 1661 CIFSSMBClose(xid, pTcon, netfid);
1666 } else { 1662 } else {
1667 /* BB For even older servers we could convert time_buf 1663 /* BB For even older servers we could convert time_buf
1668 into old DOS style which uses two second 1664 into old DOS style which uses two second
1669 granularity */ 1665 granularity */
1670 1666
1671 /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path, 1667 /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
1672 &time_buf, cifs_sb->local_nls); */ 1668 &time_buf, cifs_sb->local_nls); */
1673 } 1669 }
1674 } 1670 }
1675 /* Even if error on time set, no sense failing the call if 1671 /* Even if error on time set, no sense failing the call if
1676 the server would set the time to a reasonable value anyway, 1672 the server would set the time to a reasonable value anyway,
1677 and this check ensures that we are not being called from 1673 and this check ensures that we are not being called from
1678 sys_utimes in which case we ought to fail the call back to 1674 sys_utimes in which case we ought to fail the call back to
1 /* 1 /*
2 * fs/cifs/readdir.c 2 * fs/cifs/readdir.c
3 * 3 *
4 * Directory search handling 4 * Directory search handling
5 * 5 *
6 * Copyright (C) International Business Machines Corp., 2004, 2008 6 * Copyright (C) International Business Machines Corp., 2004, 2008
7 * Author(s): Steve French (sfrench@us.ibm.com) 7 * Author(s): Steve French (sfrench@us.ibm.com)
8 * 8 *
9 * This library is free software; you can redistribute it and/or modify 9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published 10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or 11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version. 12 * (at your option) any later version.
13 * 13 *
14 * This library is distributed in the hope that it will be useful, 14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details. 17 * the GNU Lesser General Public License for more details.
18 * 18 *
19 * You should have received a copy of the GNU Lesser General Public License 19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software 20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */ 22 */
23 #include <linux/fs.h> 23 #include <linux/fs.h>
24 #include <linux/pagemap.h> 24 #include <linux/pagemap.h>
25 #include <linux/stat.h> 25 #include <linux/stat.h>
26 #include "cifspdu.h" 26 #include "cifspdu.h"
27 #include "cifsglob.h" 27 #include "cifsglob.h"
28 #include "cifsproto.h" 28 #include "cifsproto.h"
29 #include "cifs_unicode.h" 29 #include "cifs_unicode.h"
30 #include "cifs_debug.h" 30 #include "cifs_debug.h"
31 #include "cifs_fs_sb.h" 31 #include "cifs_fs_sb.h"
32 #include "cifsfs.h" 32 #include "cifsfs.h"
33 33
34 #ifdef CONFIG_CIFS_DEBUG2 34 #ifdef CONFIG_CIFS_DEBUG2
35 static void dump_cifs_file_struct(struct file *file, char *label) 35 static void dump_cifs_file_struct(struct file *file, char *label)
36 { 36 {
37 struct cifsFileInfo *cf; 37 struct cifsFileInfo *cf;
38 38
39 if (file) { 39 if (file) {
40 cf = file->private_data; 40 cf = file->private_data;
41 if (cf == NULL) { 41 if (cf == NULL) {
42 cFYI(1, ("empty cifs private file data")); 42 cFYI(1, ("empty cifs private file data"));
43 return; 43 return;
44 } 44 }
45 if (cf->invalidHandle) 45 if (cf->invalidHandle)
46 cFYI(1, ("invalid handle")); 46 cFYI(1, ("invalid handle"));
47 if (cf->srch_inf.endOfSearch) 47 if (cf->srch_inf.endOfSearch)
48 cFYI(1, ("end of search")); 48 cFYI(1, ("end of search"));
49 if (cf->srch_inf.emptyDir) 49 if (cf->srch_inf.emptyDir)
50 cFYI(1, ("empty dir")); 50 cFYI(1, ("empty dir"));
51 } 51 }
52 } 52 }
53 #else 53 #else
54 static inline void dump_cifs_file_struct(struct file *file, char *label) 54 static inline void dump_cifs_file_struct(struct file *file, char *label)
55 { 55 {
56 } 56 }
57 #endif /* DEBUG2 */ 57 #endif /* DEBUG2 */
58 58
59 /* Returns one if new inode created (which therefore needs to be hashed) */ 59 /* Returns one if new inode created (which therefore needs to be hashed) */
60 /* Might check in the future if inode number changed so we can rehash inode */ 60 /* Might check in the future if inode number changed so we can rehash inode */
61 static int construct_dentry(struct qstr *qstring, struct file *file, 61 static int construct_dentry(struct qstr *qstring, struct file *file,
62 struct inode **ptmp_inode, struct dentry **pnew_dentry) 62 struct inode **ptmp_inode, struct dentry **pnew_dentry)
63 { 63 {
64 struct dentry *tmp_dentry; 64 struct dentry *tmp_dentry;
65 struct cifs_sb_info *cifs_sb; 65 struct cifs_sb_info *cifs_sb;
66 struct cifsTconInfo *pTcon; 66 struct cifsTconInfo *pTcon;
67 int rc = 0; 67 int rc = 0;
68 68
69 cFYI(1, ("For %s", qstring->name)); 69 cFYI(1, ("For %s", qstring->name));
70 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); 70 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
71 pTcon = cifs_sb->tcon; 71 pTcon = cifs_sb->tcon;
72 72
73 qstring->hash = full_name_hash(qstring->name, qstring->len); 73 qstring->hash = full_name_hash(qstring->name, qstring->len);
74 tmp_dentry = d_lookup(file->f_path.dentry, qstring); 74 tmp_dentry = d_lookup(file->f_path.dentry, qstring);
75 if (tmp_dentry) { 75 if (tmp_dentry) {
76 cFYI(0, ("existing dentry with inode 0x%p", 76 cFYI(0, ("existing dentry with inode 0x%p",
77 tmp_dentry->d_inode)); 77 tmp_dentry->d_inode));
78 *ptmp_inode = tmp_dentry->d_inode; 78 *ptmp_inode = tmp_dentry->d_inode;
79 /* BB overwrite old name? i.e. tmp_dentry->d_name and tmp_dentry->d_name.len??*/ 79 /* BB overwrite old name? i.e. tmp_dentry->d_name and tmp_dentry->d_name.len??*/
80 if (*ptmp_inode == NULL) { 80 if (*ptmp_inode == NULL) {
81 *ptmp_inode = new_inode(file->f_path.dentry->d_sb); 81 *ptmp_inode = new_inode(file->f_path.dentry->d_sb);
82 if (*ptmp_inode == NULL) 82 if (*ptmp_inode == NULL)
83 return rc; 83 return rc;
84 rc = 1; 84 rc = 1;
85 } 85 }
86 if (file->f_path.dentry->d_sb->s_flags & MS_NOATIME) 86 if (file->f_path.dentry->d_sb->s_flags & MS_NOATIME)
87 (*ptmp_inode)->i_flags |= S_NOATIME | S_NOCMTIME; 87 (*ptmp_inode)->i_flags |= S_NOATIME | S_NOCMTIME;
88 } else { 88 } else {
89 tmp_dentry = d_alloc(file->f_path.dentry, qstring); 89 tmp_dentry = d_alloc(file->f_path.dentry, qstring);
90 if (tmp_dentry == NULL) { 90 if (tmp_dentry == NULL) {
91 cERROR(1, ("Failed allocating dentry")); 91 cERROR(1, ("Failed allocating dentry"));
92 *ptmp_inode = NULL; 92 *ptmp_inode = NULL;
93 return rc; 93 return rc;
94 } 94 }
95 95
96 *ptmp_inode = new_inode(file->f_path.dentry->d_sb); 96 *ptmp_inode = new_inode(file->f_path.dentry->d_sb);
97 if (pTcon->nocase) 97 if (pTcon->nocase)
98 tmp_dentry->d_op = &cifs_ci_dentry_ops; 98 tmp_dentry->d_op = &cifs_ci_dentry_ops;
99 else 99 else
100 tmp_dentry->d_op = &cifs_dentry_ops; 100 tmp_dentry->d_op = &cifs_dentry_ops;
101 if (*ptmp_inode == NULL) 101 if (*ptmp_inode == NULL)
102 return rc; 102 return rc;
103 if (file->f_path.dentry->d_sb->s_flags & MS_NOATIME) 103 if (file->f_path.dentry->d_sb->s_flags & MS_NOATIME)
104 (*ptmp_inode)->i_flags |= S_NOATIME | S_NOCMTIME; 104 (*ptmp_inode)->i_flags |= S_NOATIME | S_NOCMTIME;
105 rc = 2; 105 rc = 2;
106 } 106 }
107 107
108 tmp_dentry->d_time = jiffies; 108 tmp_dentry->d_time = jiffies;
109 *pnew_dentry = tmp_dentry; 109 *pnew_dentry = tmp_dentry;
110 return rc; 110 return rc;
111 } 111 }
112 112
113 static void AdjustForTZ(struct cifsTconInfo *tcon, struct inode *inode) 113 static void AdjustForTZ(struct cifsTconInfo *tcon, struct inode *inode)
114 { 114 {
115 if ((tcon) && (tcon->ses) && (tcon->ses->server)) { 115 if ((tcon) && (tcon->ses) && (tcon->ses->server)) {
116 inode->i_ctime.tv_sec += tcon->ses->server->timeAdj; 116 inode->i_ctime.tv_sec += tcon->ses->server->timeAdj;
117 inode->i_mtime.tv_sec += tcon->ses->server->timeAdj; 117 inode->i_mtime.tv_sec += tcon->ses->server->timeAdj;
118 inode->i_atime.tv_sec += tcon->ses->server->timeAdj; 118 inode->i_atime.tv_sec += tcon->ses->server->timeAdj;
119 } 119 }
120 return; 120 return;
121 } 121 }
122 122
123 123
124 static void fill_in_inode(struct inode *tmp_inode, int new_buf_type, 124 static void fill_in_inode(struct inode *tmp_inode, int new_buf_type,
125 char *buf, unsigned int *pobject_type, int isNewInode) 125 char *buf, unsigned int *pobject_type, int isNewInode)
126 { 126 {
127 loff_t local_size; 127 loff_t local_size;
128 struct timespec local_mtime; 128 struct timespec local_mtime;
129 129
130 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode); 130 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
131 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb); 131 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
132 __u32 attr; 132 __u32 attr;
133 __u64 allocation_size; 133 __u64 allocation_size;
134 __u64 end_of_file; 134 __u64 end_of_file;
135 umode_t default_mode;
135 136
136 /* save mtime and size */ 137 /* save mtime and size */
137 local_mtime = tmp_inode->i_mtime; 138 local_mtime = tmp_inode->i_mtime;
138 local_size = tmp_inode->i_size; 139 local_size = tmp_inode->i_size;
139 140
140 if (new_buf_type) { 141 if (new_buf_type) {
141 FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf; 142 FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf;
142 143
143 attr = le32_to_cpu(pfindData->ExtFileAttributes); 144 attr = le32_to_cpu(pfindData->ExtFileAttributes);
144 allocation_size = le64_to_cpu(pfindData->AllocationSize); 145 allocation_size = le64_to_cpu(pfindData->AllocationSize);
145 end_of_file = le64_to_cpu(pfindData->EndOfFile); 146 end_of_file = le64_to_cpu(pfindData->EndOfFile);
146 tmp_inode->i_atime = 147 tmp_inode->i_atime =
147 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime)); 148 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
148 tmp_inode->i_mtime = 149 tmp_inode->i_mtime =
149 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime)); 150 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
150 tmp_inode->i_ctime = 151 tmp_inode->i_ctime =
151 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime)); 152 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
152 } else { /* legacy, OS2 and DOS style */ 153 } else { /* legacy, OS2 and DOS style */
153 /* struct timespec ts;*/ 154 /* struct timespec ts;*/
154 FIND_FILE_STANDARD_INFO *pfindData = 155 FIND_FILE_STANDARD_INFO *pfindData =
155 (FIND_FILE_STANDARD_INFO *)buf; 156 (FIND_FILE_STANDARD_INFO *)buf;
156 157
157 tmp_inode->i_mtime = cnvrtDosUnixTm( 158 tmp_inode->i_mtime = cnvrtDosUnixTm(
158 le16_to_cpu(pfindData->LastWriteDate), 159 le16_to_cpu(pfindData->LastWriteDate),
159 le16_to_cpu(pfindData->LastWriteTime)); 160 le16_to_cpu(pfindData->LastWriteTime));
160 tmp_inode->i_atime = cnvrtDosUnixTm( 161 tmp_inode->i_atime = cnvrtDosUnixTm(
161 le16_to_cpu(pfindData->LastAccessDate), 162 le16_to_cpu(pfindData->LastAccessDate),
162 le16_to_cpu(pfindData->LastAccessTime)); 163 le16_to_cpu(pfindData->LastAccessTime));
163 tmp_inode->i_ctime = cnvrtDosUnixTm( 164 tmp_inode->i_ctime = cnvrtDosUnixTm(
164 le16_to_cpu(pfindData->LastWriteDate), 165 le16_to_cpu(pfindData->LastWriteDate),
165 le16_to_cpu(pfindData->LastWriteTime)); 166 le16_to_cpu(pfindData->LastWriteTime));
166 AdjustForTZ(cifs_sb->tcon, tmp_inode); 167 AdjustForTZ(cifs_sb->tcon, tmp_inode);
167 attr = le16_to_cpu(pfindData->Attributes); 168 attr = le16_to_cpu(pfindData->Attributes);
168 allocation_size = le32_to_cpu(pfindData->AllocationSize); 169 allocation_size = le32_to_cpu(pfindData->AllocationSize);
169 end_of_file = le32_to_cpu(pfindData->DataSize); 170 end_of_file = le32_to_cpu(pfindData->DataSize);
170 } 171 }
171 172
172 /* Linux can not store file creation time unfortunately so ignore it */ 173 /* Linux can not store file creation time unfortunately so ignore it */
173 174
174 cifsInfo->cifsAttrs = attr; 175 cifsInfo->cifsAttrs = attr;
175 #ifdef CONFIG_CIFS_EXPERIMENTAL 176 #ifdef CONFIG_CIFS_EXPERIMENTAL
176 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) { 177 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
177 /* get more accurate mode via ACL - so force inode refresh */ 178 /* get more accurate mode via ACL - so force inode refresh */
178 cifsInfo->time = 0; 179 cifsInfo->time = 0;
179 } else 180 } else
180 #endif /* CONFIG_CIFS_EXPERIMENTAL */ 181 #endif /* CONFIG_CIFS_EXPERIMENTAL */
181 cifsInfo->time = jiffies; 182 cifsInfo->time = jiffies;
182 183
183 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */ 184 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
184 /* 2767 perms - indicate mandatory locking */ 185 /* 2767 perms - indicate mandatory locking */
185 /* BB fill in uid and gid here? with help from winbind? 186 /* BB fill in uid and gid here? with help from winbind?
186 or retrieve from NTFS stream extended attribute */ 187 or retrieve from NTFS stream extended attribute */
187 if (atomic_read(&cifsInfo->inUse) == 0) { 188 if (atomic_read(&cifsInfo->inUse) == 0) {
188 tmp_inode->i_uid = cifs_sb->mnt_uid; 189 tmp_inode->i_uid = cifs_sb->mnt_uid;
189 tmp_inode->i_gid = cifs_sb->mnt_gid; 190 tmp_inode->i_gid = cifs_sb->mnt_gid;
190 /* set default mode. will override for dirs below */ 191 }
191 tmp_inode->i_mode = cifs_sb->mnt_file_mode; 192
192 } else { 193 if (attr & ATTR_DIRECTORY)
193 /* mask off the type bits since it gets set 194 default_mode = cifs_sb->mnt_dir_mode;
194 below and we do not want to get two type 195 else
195 bits set */ 196 default_mode = cifs_sb->mnt_file_mode;
197
198 /* set initial permissions */
199 if ((atomic_read(&cifsInfo->inUse) == 0) ||
200 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
201 tmp_inode->i_mode = default_mode;
202 else {
203 /* just reenable write bits if !ATTR_READONLY */
204 if ((tmp_inode->i_mode & S_IWUGO) == 0 &&
205 (attr & ATTR_READONLY) == 0)
206 tmp_inode->i_mode |= (S_IWUGO & default_mode);
207
196 tmp_inode->i_mode &= ~S_IFMT; 208 tmp_inode->i_mode &= ~S_IFMT;
197 } 209 }
198 210
199 if (attr & ATTR_DIRECTORY) { 211 /* clear write bits if ATTR_READONLY is set */
200 *pobject_type = DT_DIR; 212 if (attr & ATTR_READONLY)
201 /* override default perms since we do not lock dirs */ 213 tmp_inode->i_mode &= ~S_IWUGO;
202 if (atomic_read(&cifsInfo->inUse) == 0) 214
203 tmp_inode->i_mode = cifs_sb->mnt_dir_mode; 215 /* set inode type */
204 tmp_inode->i_mode |= S_IFDIR; 216 if ((attr & ATTR_SYSTEM) &&
205 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) && 217 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
206 (attr & ATTR_SYSTEM)) {
207 if (end_of_file == 0) { 218 if (end_of_file == 0) {
208 *pobject_type = DT_FIFO;
209 tmp_inode->i_mode |= S_IFIFO; 219 tmp_inode->i_mode |= S_IFIFO;
220 *pobject_type = DT_FIFO;
210 } else { 221 } else {
211 /* rather than get the type here, we mark the 222 /*
212 inode as needing revalidate and get the real type 223 * trying to get the type can be slow, so just call
213 (blk vs chr vs. symlink) later ie in lookup */ 224 * this a regular file for now, and mark for reval
214 *pobject_type = DT_REG; 225 */
215 tmp_inode->i_mode |= S_IFREG; 226 tmp_inode->i_mode |= S_IFREG;
227 *pobject_type = DT_REG;
216 cifsInfo->time = 0; 228 cifsInfo->time = 0;
217 } 229 }
218 /* we no longer mark these because we could not follow them */
219 /* } else if (attr & ATTR_REPARSE) {
220 *pobject_type = DT_LNK;
221 tmp_inode->i_mode |= S_IFLNK; */
222 } else { 230 } else {
223 *pobject_type = DT_REG; 231 if (attr & ATTR_DIRECTORY) {
224 tmp_inode->i_mode |= S_IFREG; 232 tmp_inode->i_mode |= S_IFDIR;
225 if (attr & ATTR_READONLY) 233 *pobject_type = DT_DIR;
226 tmp_inode->i_mode &= ~(S_IWUGO); 234 } else {
227 else if ((tmp_inode->i_mode & S_IWUGO) == 0) 235 tmp_inode->i_mode |= S_IFREG;
228 /* the ATTR_READONLY flag may have been changed on */ 236 *pobject_type = DT_REG;
229 /* server -- set any w bits allowed by mnt_file_mode */ 237 }
230 tmp_inode->i_mode |= (S_IWUGO & cifs_sb->mnt_file_mode); 238 }
231 } /* could add code here - to validate if device or weird share type? */
232 239
233 /* can not fill in nlink here as in qpathinfo version and Unx search */ 240 /* can not fill in nlink here as in qpathinfo version and Unx search */
234 if (atomic_read(&cifsInfo->inUse) == 0) 241 if (atomic_read(&cifsInfo->inUse) == 0)
235 atomic_set(&cifsInfo->inUse, 1); 242 atomic_set(&cifsInfo->inUse, 1);
236 243
237 spin_lock(&tmp_inode->i_lock); 244 spin_lock(&tmp_inode->i_lock);
238 if (is_size_safe_to_change(cifsInfo, end_of_file)) { 245 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
239 /* can not safely change the file size here if the 246 /* can not safely change the file size here if the
240 client is writing to it due to potential races */ 247 client is writing to it due to potential races */
241 i_size_write(tmp_inode, end_of_file); 248 i_size_write(tmp_inode, end_of_file);
242 249
243 /* 512 bytes (2**9) is the fake blocksize that must be used */ 250 /* 512 bytes (2**9) is the fake blocksize that must be used */
244 /* for this calculation, even though the reported blocksize is larger */ 251 /* for this calculation, even though the reported blocksize is larger */
245 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9; 252 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
246 } 253 }
247 spin_unlock(&tmp_inode->i_lock); 254 spin_unlock(&tmp_inode->i_lock);
248 255
249 if (allocation_size < end_of_file) 256 if (allocation_size < end_of_file)
250 cFYI(1, ("May be sparse file, allocation less than file size")); 257 cFYI(1, ("May be sparse file, allocation less than file size"));
251 cFYI(1, ("File Size %ld and blocks %llu", 258 cFYI(1, ("File Size %ld and blocks %llu",
252 (unsigned long)tmp_inode->i_size, 259 (unsigned long)tmp_inode->i_size,
253 (unsigned long long)tmp_inode->i_blocks)); 260 (unsigned long long)tmp_inode->i_blocks));
254 if (S_ISREG(tmp_inode->i_mode)) { 261 if (S_ISREG(tmp_inode->i_mode)) {
255 cFYI(1, ("File inode")); 262 cFYI(1, ("File inode"));
256 tmp_inode->i_op = &cifs_file_inode_ops; 263 tmp_inode->i_op = &cifs_file_inode_ops;
257 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { 264 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
258 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) 265 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
259 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops; 266 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
260 else 267 else
261 tmp_inode->i_fop = &cifs_file_direct_ops; 268 tmp_inode->i_fop = &cifs_file_direct_ops;
262 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) 269 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
263 tmp_inode->i_fop = &cifs_file_nobrl_ops; 270 tmp_inode->i_fop = &cifs_file_nobrl_ops;
264 else 271 else
265 tmp_inode->i_fop = &cifs_file_ops; 272 tmp_inode->i_fop = &cifs_file_ops;
266 273
267 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) && 274 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
268 (cifs_sb->tcon->ses->server->maxBuf < 275 (cifs_sb->tcon->ses->server->maxBuf <
269 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)) 276 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
270 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf; 277 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
271 else 278 else
272 tmp_inode->i_data.a_ops = &cifs_addr_ops; 279 tmp_inode->i_data.a_ops = &cifs_addr_ops;
273 280
274 if (isNewInode) 281 if (isNewInode)
275 return; /* No sense invalidating pages for new inode 282 return; /* No sense invalidating pages for new inode
276 since have not started caching readahead file 283 since have not started caching readahead file
277 data yet */ 284 data yet */
278 285
279 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) && 286 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
280 (local_size == tmp_inode->i_size)) { 287 (local_size == tmp_inode->i_size)) {
281 cFYI(1, ("inode exists but unchanged")); 288 cFYI(1, ("inode exists but unchanged"));
282 } else { 289 } else {
283 /* file may have changed on server */ 290 /* file may have changed on server */
284 cFYI(1, ("invalidate inode, readdir detected change")); 291 cFYI(1, ("invalidate inode, readdir detected change"));
285 invalidate_remote_inode(tmp_inode); 292 invalidate_remote_inode(tmp_inode);
286 } 293 }
287 } else if (S_ISDIR(tmp_inode->i_mode)) { 294 } else if (S_ISDIR(tmp_inode->i_mode)) {
288 cFYI(1, ("Directory inode")); 295 cFYI(1, ("Directory inode"));
289 tmp_inode->i_op = &cifs_dir_inode_ops; 296 tmp_inode->i_op = &cifs_dir_inode_ops;
290 tmp_inode->i_fop = &cifs_dir_ops; 297 tmp_inode->i_fop = &cifs_dir_ops;
291 } else if (S_ISLNK(tmp_inode->i_mode)) { 298 } else if (S_ISLNK(tmp_inode->i_mode)) {
292 cFYI(1, ("Symbolic Link inode")); 299 cFYI(1, ("Symbolic Link inode"));
293 tmp_inode->i_op = &cifs_symlink_inode_ops; 300 tmp_inode->i_op = &cifs_symlink_inode_ops;
294 } else { 301 } else {
295 cFYI(1, ("Init special inode")); 302 cFYI(1, ("Init special inode"));
296 init_special_inode(tmp_inode, tmp_inode->i_mode, 303 init_special_inode(tmp_inode, tmp_inode->i_mode,
297 tmp_inode->i_rdev); 304 tmp_inode->i_rdev);
298 } 305 }
299 } 306 }
300 307
301 static void unix_fill_in_inode(struct inode *tmp_inode, 308 static void unix_fill_in_inode(struct inode *tmp_inode,
302 FILE_UNIX_INFO *pfindData, unsigned int *pobject_type, int isNewInode) 309 FILE_UNIX_INFO *pfindData, unsigned int *pobject_type, int isNewInode)
303 { 310 {
304 loff_t local_size; 311 loff_t local_size;
305 struct timespec local_mtime; 312 struct timespec local_mtime;
306 313
307 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode); 314 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
308 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb); 315 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
309 316
310 __u32 type = le32_to_cpu(pfindData->Type); 317 __u32 type = le32_to_cpu(pfindData->Type);
311 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes); 318 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
312 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile); 319 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
313 cifsInfo->time = jiffies; 320 cifsInfo->time = jiffies;
314 atomic_inc(&cifsInfo->inUse); 321 atomic_inc(&cifsInfo->inUse);
315 322
316 /* save mtime and size */ 323 /* save mtime and size */
317 local_mtime = tmp_inode->i_mtime; 324 local_mtime = tmp_inode->i_mtime;
318 local_size = tmp_inode->i_size; 325 local_size = tmp_inode->i_size;
319 326
320 tmp_inode->i_atime = 327 tmp_inode->i_atime =
321 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime)); 328 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
322 tmp_inode->i_mtime = 329 tmp_inode->i_mtime =
323 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime)); 330 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
324 tmp_inode->i_ctime = 331 tmp_inode->i_ctime =
325 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange)); 332 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
326 333
327 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions); 334 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
328 /* since we set the inode type below we need to mask off type 335 /* since we set the inode type below we need to mask off type
329 to avoid strange results if bits above were corrupt */ 336 to avoid strange results if bits above were corrupt */
330 tmp_inode->i_mode &= ~S_IFMT; 337 tmp_inode->i_mode &= ~S_IFMT;
331 if (type == UNIX_FILE) { 338 if (type == UNIX_FILE) {
332 *pobject_type = DT_REG; 339 *pobject_type = DT_REG;
333 tmp_inode->i_mode |= S_IFREG; 340 tmp_inode->i_mode |= S_IFREG;
334 } else if (type == UNIX_SYMLINK) { 341 } else if (type == UNIX_SYMLINK) {
335 *pobject_type = DT_LNK; 342 *pobject_type = DT_LNK;
336 tmp_inode->i_mode |= S_IFLNK; 343 tmp_inode->i_mode |= S_IFLNK;
337 } else if (type == UNIX_DIR) { 344 } else if (type == UNIX_DIR) {
338 *pobject_type = DT_DIR; 345 *pobject_type = DT_DIR;
339 tmp_inode->i_mode |= S_IFDIR; 346 tmp_inode->i_mode |= S_IFDIR;
340 } else if (type == UNIX_CHARDEV) { 347 } else if (type == UNIX_CHARDEV) {
341 *pobject_type = DT_CHR; 348 *pobject_type = DT_CHR;
342 tmp_inode->i_mode |= S_IFCHR; 349 tmp_inode->i_mode |= S_IFCHR;
343 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor), 350 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
344 le64_to_cpu(pfindData->DevMinor) & MINORMASK); 351 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
345 } else if (type == UNIX_BLOCKDEV) { 352 } else if (type == UNIX_BLOCKDEV) {
346 *pobject_type = DT_BLK; 353 *pobject_type = DT_BLK;
347 tmp_inode->i_mode |= S_IFBLK; 354 tmp_inode->i_mode |= S_IFBLK;
348 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor), 355 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
349 le64_to_cpu(pfindData->DevMinor) & MINORMASK); 356 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
350 } else if (type == UNIX_FIFO) { 357 } else if (type == UNIX_FIFO) {
351 *pobject_type = DT_FIFO; 358 *pobject_type = DT_FIFO;
352 tmp_inode->i_mode |= S_IFIFO; 359 tmp_inode->i_mode |= S_IFIFO;
353 } else if (type == UNIX_SOCKET) { 360 } else if (type == UNIX_SOCKET) {
354 *pobject_type = DT_SOCK; 361 *pobject_type = DT_SOCK;
355 tmp_inode->i_mode |= S_IFSOCK; 362 tmp_inode->i_mode |= S_IFSOCK;
356 } else { 363 } else {
357 /* safest to just call it a file */ 364 /* safest to just call it a file */
358 *pobject_type = DT_REG; 365 *pobject_type = DT_REG;
359 tmp_inode->i_mode |= S_IFREG; 366 tmp_inode->i_mode |= S_IFREG;
360 cFYI(1, ("unknown inode type %d", type)); 367 cFYI(1, ("unknown inode type %d", type));
361 } 368 }
362 369
363 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) 370 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
364 tmp_inode->i_uid = cifs_sb->mnt_uid; 371 tmp_inode->i_uid = cifs_sb->mnt_uid;
365 else 372 else
366 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid); 373 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
367 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) 374 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
368 tmp_inode->i_gid = cifs_sb->mnt_gid; 375 tmp_inode->i_gid = cifs_sb->mnt_gid;
369 else 376 else
370 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid); 377 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
371 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks); 378 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
372 379
373 spin_lock(&tmp_inode->i_lock); 380 spin_lock(&tmp_inode->i_lock);
374 if (is_size_safe_to_change(cifsInfo, end_of_file)) { 381 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
375 /* can not safely change the file size here if the 382 /* can not safely change the file size here if the
376 client is writing to it due to potential races */ 383 client is writing to it due to potential races */
377 i_size_write(tmp_inode, end_of_file); 384 i_size_write(tmp_inode, end_of_file);
378 385
379 /* 512 bytes (2**9) is the fake blocksize that must be used */ 386 /* 512 bytes (2**9) is the fake blocksize that must be used */
380 /* for this calculation, not the real blocksize */ 387 /* for this calculation, not the real blocksize */
381 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9; 388 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
382 } 389 }
383 spin_unlock(&tmp_inode->i_lock); 390 spin_unlock(&tmp_inode->i_lock);
384 391
385 if (S_ISREG(tmp_inode->i_mode)) { 392 if (S_ISREG(tmp_inode->i_mode)) {
386 cFYI(1, ("File inode")); 393 cFYI(1, ("File inode"));
387 tmp_inode->i_op = &cifs_file_inode_ops; 394 tmp_inode->i_op = &cifs_file_inode_ops;
388 395
389 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { 396 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
390 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) 397 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
391 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops; 398 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
392 else 399 else
393 tmp_inode->i_fop = &cifs_file_direct_ops; 400 tmp_inode->i_fop = &cifs_file_direct_ops;
394 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) 401 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
395 tmp_inode->i_fop = &cifs_file_nobrl_ops; 402 tmp_inode->i_fop = &cifs_file_nobrl_ops;
396 else 403 else
397 tmp_inode->i_fop = &cifs_file_ops; 404 tmp_inode->i_fop = &cifs_file_ops;
398 405
399 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) && 406 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
400 (cifs_sb->tcon->ses->server->maxBuf < 407 (cifs_sb->tcon->ses->server->maxBuf <
401 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)) 408 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
402 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf; 409 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
403 else 410 else
404 tmp_inode->i_data.a_ops = &cifs_addr_ops; 411 tmp_inode->i_data.a_ops = &cifs_addr_ops;
405 412
406 if (isNewInode) 413 if (isNewInode)
407 return; /* No sense invalidating pages for new inode 414 return; /* No sense invalidating pages for new inode
408 since we have not started caching readahead 415 since we have not started caching readahead
409 file data for it yet */ 416 file data for it yet */
410 417
411 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) && 418 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
412 (local_size == tmp_inode->i_size)) { 419 (local_size == tmp_inode->i_size)) {
413 cFYI(1, ("inode exists but unchanged")); 420 cFYI(1, ("inode exists but unchanged"));
414 } else { 421 } else {
415 /* file may have changed on server */ 422 /* file may have changed on server */
416 cFYI(1, ("invalidate inode, readdir detected change")); 423 cFYI(1, ("invalidate inode, readdir detected change"));
417 invalidate_remote_inode(tmp_inode); 424 invalidate_remote_inode(tmp_inode);
418 } 425 }
419 } else if (S_ISDIR(tmp_inode->i_mode)) { 426 } else if (S_ISDIR(tmp_inode->i_mode)) {
420 cFYI(1, ("Directory inode")); 427 cFYI(1, ("Directory inode"));
421 tmp_inode->i_op = &cifs_dir_inode_ops; 428 tmp_inode->i_op = &cifs_dir_inode_ops;
422 tmp_inode->i_fop = &cifs_dir_ops; 429 tmp_inode->i_fop = &cifs_dir_ops;
423 } else if (S_ISLNK(tmp_inode->i_mode)) { 430 } else if (S_ISLNK(tmp_inode->i_mode)) {
424 cFYI(1, ("Symbolic Link inode")); 431 cFYI(1, ("Symbolic Link inode"));
425 tmp_inode->i_op = &cifs_symlink_inode_ops; 432 tmp_inode->i_op = &cifs_symlink_inode_ops;
426 /* tmp_inode->i_fop = *//* do not need to set to anything */ 433 /* tmp_inode->i_fop = *//* do not need to set to anything */
427 } else { 434 } else {
428 cFYI(1, ("Special inode")); 435 cFYI(1, ("Special inode"));
429 init_special_inode(tmp_inode, tmp_inode->i_mode, 436 init_special_inode(tmp_inode, tmp_inode->i_mode,
430 tmp_inode->i_rdev); 437 tmp_inode->i_rdev);
431 } 438 }
432 } 439 }
433 440
434 static int initiate_cifs_search(const int xid, struct file *file) 441 static int initiate_cifs_search(const int xid, struct file *file)
435 { 442 {
436 int rc = 0; 443 int rc = 0;
437 char *full_path; 444 char *full_path;
438 struct cifsFileInfo *cifsFile; 445 struct cifsFileInfo *cifsFile;
439 struct cifs_sb_info *cifs_sb; 446 struct cifs_sb_info *cifs_sb;
440 struct cifsTconInfo *pTcon; 447 struct cifsTconInfo *pTcon;
441 448
442 if (file->private_data == NULL) { 449 if (file->private_data == NULL) {
443 file->private_data = 450 file->private_data =
444 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); 451 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
445 } 452 }
446 453
447 if (file->private_data == NULL) 454 if (file->private_data == NULL)
448 return -ENOMEM; 455 return -ENOMEM;
449 cifsFile = file->private_data; 456 cifsFile = file->private_data;
450 cifsFile->invalidHandle = true; 457 cifsFile->invalidHandle = true;
451 cifsFile->srch_inf.endOfSearch = false; 458 cifsFile->srch_inf.endOfSearch = false;
452 459
453 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); 460 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
454 if (cifs_sb == NULL) 461 if (cifs_sb == NULL)
455 return -EINVAL; 462 return -EINVAL;
456 463
457 pTcon = cifs_sb->tcon; 464 pTcon = cifs_sb->tcon;
458 if (pTcon == NULL) 465 if (pTcon == NULL)
459 return -EINVAL; 466 return -EINVAL;
460 467
461 full_path = build_path_from_dentry(file->f_path.dentry); 468 full_path = build_path_from_dentry(file->f_path.dentry);
462 469
463 if (full_path == NULL) 470 if (full_path == NULL)
464 return -ENOMEM; 471 return -ENOMEM;
465 472
466 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos)); 473 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
467 474
468 ffirst_retry: 475 ffirst_retry:
469 /* test for Unix extensions */ 476 /* test for Unix extensions */
470 /* but now check for them on the share/mount not on the SMB session */ 477 /* but now check for them on the share/mount not on the SMB session */
471 /* if (pTcon->ses->capabilities & CAP_UNIX) { */ 478 /* if (pTcon->ses->capabilities & CAP_UNIX) { */
472 if (pTcon->unix_ext) 479 if (pTcon->unix_ext)
473 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX; 480 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
474 else if ((pTcon->ses->capabilities & 481 else if ((pTcon->ses->capabilities &
475 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) { 482 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
476 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD; 483 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
477 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { 484 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
478 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO; 485 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
479 } else /* not srvinos - BB fixme add check for backlevel? */ { 486 } else /* not srvinos - BB fixme add check for backlevel? */ {
480 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO; 487 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
481 } 488 }
482 489
483 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls, 490 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
484 &cifsFile->netfid, &cifsFile->srch_inf, 491 &cifsFile->netfid, &cifsFile->srch_inf,
485 cifs_sb->mnt_cifs_flags & 492 cifs_sb->mnt_cifs_flags &
486 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb)); 493 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
487 if (rc == 0) 494 if (rc == 0)
488 cifsFile->invalidHandle = false; 495 cifsFile->invalidHandle = false;
489 if ((rc == -EOPNOTSUPP) && 496 if ((rc == -EOPNOTSUPP) &&
490 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) { 497 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
491 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM; 498 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
492 goto ffirst_retry; 499 goto ffirst_retry;
493 } 500 }
494 kfree(full_path); 501 kfree(full_path);
495 return rc; 502 return rc;
496 } 503 }
497 504
498 /* return length of unicode string in bytes */ 505 /* return length of unicode string in bytes */
499 static int cifs_unicode_bytelen(char *str) 506 static int cifs_unicode_bytelen(char *str)
500 { 507 {
501 int len; 508 int len;
502 __le16 *ustr = (__le16 *)str; 509 __le16 *ustr = (__le16 *)str;
503 510
504 for (len = 0; len <= PATH_MAX; len++) { 511 for (len = 0; len <= PATH_MAX; len++) {
505 if (ustr[len] == 0) 512 if (ustr[len] == 0)
506 return len << 1; 513 return len << 1;
507 } 514 }
508 cFYI(1, ("Unicode string longer than PATH_MAX found")); 515 cFYI(1, ("Unicode string longer than PATH_MAX found"));
509 return len << 1; 516 return len << 1;
510 } 517 }
511 518
512 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level) 519 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
513 { 520 {
514 char *new_entry; 521 char *new_entry;
515 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry; 522 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
516 523
517 if (level == SMB_FIND_FILE_INFO_STANDARD) { 524 if (level == SMB_FIND_FILE_INFO_STANDARD) {
518 FIND_FILE_STANDARD_INFO *pfData; 525 FIND_FILE_STANDARD_INFO *pfData;
519 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo; 526 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
520 527
521 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) + 528 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
522 pfData->FileNameLength; 529 pfData->FileNameLength;
523 } else 530 } else
524 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset); 531 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
525 cFYI(1, ("new entry %p old entry %p", new_entry, old_entry)); 532 cFYI(1, ("new entry %p old entry %p", new_entry, old_entry));
526 /* validate that new_entry is not past end of SMB */ 533 /* validate that new_entry is not past end of SMB */
527 if (new_entry >= end_of_smb) { 534 if (new_entry >= end_of_smb) {
528 cERROR(1, 535 cERROR(1,
529 ("search entry %p began after end of SMB %p old entry %p", 536 ("search entry %p began after end of SMB %p old entry %p",
530 new_entry, end_of_smb, old_entry)); 537 new_entry, end_of_smb, old_entry));
531 return NULL; 538 return NULL;
532 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) && 539 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
533 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb)) 540 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
534 || ((level != SMB_FIND_FILE_INFO_STANDARD) && 541 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
535 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) { 542 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
536 cERROR(1, ("search entry %p extends after end of SMB %p", 543 cERROR(1, ("search entry %p extends after end of SMB %p",
537 new_entry, end_of_smb)); 544 new_entry, end_of_smb));
538 return NULL; 545 return NULL;
539 } else 546 } else
540 return new_entry; 547 return new_entry;
541 548
542 } 549 }
543 550
544 #define UNICODE_DOT cpu_to_le16(0x2e) 551 #define UNICODE_DOT cpu_to_le16(0x2e)
545 552
546 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */ 553 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
547 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile) 554 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
548 { 555 {
549 int rc = 0; 556 int rc = 0;
550 char *filename = NULL; 557 char *filename = NULL;
551 int len = 0; 558 int len = 0;
552 559
553 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) { 560 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
554 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry; 561 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
555 filename = &pFindData->FileName[0]; 562 filename = &pFindData->FileName[0];
556 if (cfile->srch_inf.unicode) { 563 if (cfile->srch_inf.unicode) {
557 len = cifs_unicode_bytelen(filename); 564 len = cifs_unicode_bytelen(filename);
558 } else { 565 } else {
559 /* BB should we make this strnlen of PATH_MAX? */ 566 /* BB should we make this strnlen of PATH_MAX? */
560 len = strnlen(filename, 5); 567 len = strnlen(filename, 5);
561 } 568 }
562 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) { 569 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
563 FILE_DIRECTORY_INFO *pFindData = 570 FILE_DIRECTORY_INFO *pFindData =
564 (FILE_DIRECTORY_INFO *)current_entry; 571 (FILE_DIRECTORY_INFO *)current_entry;
565 filename = &pFindData->FileName[0]; 572 filename = &pFindData->FileName[0];
566 len = le32_to_cpu(pFindData->FileNameLength); 573 len = le32_to_cpu(pFindData->FileNameLength);
567 } else if (cfile->srch_inf.info_level == 574 } else if (cfile->srch_inf.info_level ==
568 SMB_FIND_FILE_FULL_DIRECTORY_INFO) { 575 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
569 FILE_FULL_DIRECTORY_INFO *pFindData = 576 FILE_FULL_DIRECTORY_INFO *pFindData =
570 (FILE_FULL_DIRECTORY_INFO *)current_entry; 577 (FILE_FULL_DIRECTORY_INFO *)current_entry;
571 filename = &pFindData->FileName[0]; 578 filename = &pFindData->FileName[0];
572 len = le32_to_cpu(pFindData->FileNameLength); 579 len = le32_to_cpu(pFindData->FileNameLength);
573 } else if (cfile->srch_inf.info_level == 580 } else if (cfile->srch_inf.info_level ==
574 SMB_FIND_FILE_ID_FULL_DIR_INFO) { 581 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
575 SEARCH_ID_FULL_DIR_INFO *pFindData = 582 SEARCH_ID_FULL_DIR_INFO *pFindData =
576 (SEARCH_ID_FULL_DIR_INFO *)current_entry; 583 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
577 filename = &pFindData->FileName[0]; 584 filename = &pFindData->FileName[0];
578 len = le32_to_cpu(pFindData->FileNameLength); 585 len = le32_to_cpu(pFindData->FileNameLength);
579 } else if (cfile->srch_inf.info_level == 586 } else if (cfile->srch_inf.info_level ==
580 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) { 587 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
581 FILE_BOTH_DIRECTORY_INFO *pFindData = 588 FILE_BOTH_DIRECTORY_INFO *pFindData =
582 (FILE_BOTH_DIRECTORY_INFO *)current_entry; 589 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
583 filename = &pFindData->FileName[0]; 590 filename = &pFindData->FileName[0];
584 len = le32_to_cpu(pFindData->FileNameLength); 591 len = le32_to_cpu(pFindData->FileNameLength);
585 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) { 592 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
586 FIND_FILE_STANDARD_INFO *pFindData = 593 FIND_FILE_STANDARD_INFO *pFindData =
587 (FIND_FILE_STANDARD_INFO *)current_entry; 594 (FIND_FILE_STANDARD_INFO *)current_entry;
588 filename = &pFindData->FileName[0]; 595 filename = &pFindData->FileName[0];
589 len = pFindData->FileNameLength; 596 len = pFindData->FileNameLength;
590 } else { 597 } else {
591 cFYI(1, ("Unknown findfirst level %d", 598 cFYI(1, ("Unknown findfirst level %d",
592 cfile->srch_inf.info_level)); 599 cfile->srch_inf.info_level));
593 } 600 }
594 601
595 if (filename) { 602 if (filename) {
596 if (cfile->srch_inf.unicode) { 603 if (cfile->srch_inf.unicode) {
597 __le16 *ufilename = (__le16 *)filename; 604 __le16 *ufilename = (__le16 *)filename;
598 if (len == 2) { 605 if (len == 2) {
599 /* check for . */ 606 /* check for . */
600 if (ufilename[0] == UNICODE_DOT) 607 if (ufilename[0] == UNICODE_DOT)
601 rc = 1; 608 rc = 1;
602 } else if (len == 4) { 609 } else if (len == 4) {
603 /* check for .. */ 610 /* check for .. */
604 if ((ufilename[0] == UNICODE_DOT) 611 if ((ufilename[0] == UNICODE_DOT)
605 && (ufilename[1] == UNICODE_DOT)) 612 && (ufilename[1] == UNICODE_DOT))
606 rc = 2; 613 rc = 2;
607 } 614 }
608 } else /* ASCII */ { 615 } else /* ASCII */ {
609 if (len == 1) { 616 if (len == 1) {
610 if (filename[0] == '.') 617 if (filename[0] == '.')
611 rc = 1; 618 rc = 1;
612 } else if (len == 2) { 619 } else if (len == 2) {
613 if ((filename[0] == '.') && (filename[1] == '.')) 620 if ((filename[0] == '.') && (filename[1] == '.'))
614 rc = 2; 621 rc = 2;
615 } 622 }
616 } 623 }
617 } 624 }
618 625
619 return rc; 626 return rc;
620 } 627 }
621 628
622 /* Check if directory that we are searching has changed so we can decide 629 /* Check if directory that we are searching has changed so we can decide
623 whether we can use the cached search results from the previous search */ 630 whether we can use the cached search results from the previous search */
624 static int is_dir_changed(struct file *file) 631 static int is_dir_changed(struct file *file)
625 { 632 {
626 struct inode *inode = file->f_path.dentry->d_inode; 633 struct inode *inode = file->f_path.dentry->d_inode;
627 struct cifsInodeInfo *cifsInfo = CIFS_I(inode); 634 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
628 635
629 if (cifsInfo->time == 0) 636 if (cifsInfo->time == 0)
630 return 1; /* directory was changed, perhaps due to unlink */ 637 return 1; /* directory was changed, perhaps due to unlink */
631 else 638 else
632 return 0; 639 return 0;
633 640
634 } 641 }
635 642
636 /* find the corresponding entry in the search */ 643 /* find the corresponding entry in the search */
637 /* Note that the SMB server returns search entries for . and .. which 644 /* Note that the SMB server returns search entries for . and .. which
638 complicates logic here if we choose to parse for them and we do not 645 complicates logic here if we choose to parse for them and we do not
639 assume that they are located in the findfirst return buffer.*/ 646 assume that they are located in the findfirst return buffer.*/
640 /* We start counting in the buffer with entry 2 and increment for every 647 /* We start counting in the buffer with entry 2 and increment for every
641 entry (do not increment for . or .. entry) */ 648 entry (do not increment for . or .. entry) */
642 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon, 649 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
643 struct file *file, char **ppCurrentEntry, int *num_to_ret) 650 struct file *file, char **ppCurrentEntry, int *num_to_ret)
644 { 651 {
645 int rc = 0; 652 int rc = 0;
646 int pos_in_buf = 0; 653 int pos_in_buf = 0;
647 loff_t first_entry_in_buffer; 654 loff_t first_entry_in_buffer;
648 loff_t index_to_find = file->f_pos; 655 loff_t index_to_find = file->f_pos;
649 struct cifsFileInfo *cifsFile = file->private_data; 656 struct cifsFileInfo *cifsFile = file->private_data;
650 /* check if index in the buffer */ 657 /* check if index in the buffer */
651 658
652 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) || 659 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
653 (num_to_ret == NULL)) 660 (num_to_ret == NULL))
654 return -ENOENT; 661 return -ENOENT;
655 662
656 *ppCurrentEntry = NULL; 663 *ppCurrentEntry = NULL;
657 first_entry_in_buffer = 664 first_entry_in_buffer =
658 cifsFile->srch_inf.index_of_last_entry - 665 cifsFile->srch_inf.index_of_last_entry -
659 cifsFile->srch_inf.entries_in_buffer; 666 cifsFile->srch_inf.entries_in_buffer;
660 667
661 /* if first entry in buf is zero then is first buffer 668 /* if first entry in buf is zero then is first buffer
662 in search response data which means it is likely . and .. 669 in search response data which means it is likely . and ..
663 will be in this buffer, although some servers do not return 670 will be in this buffer, although some servers do not return
664 . and .. for the root of a drive and for those we need 671 . and .. for the root of a drive and for those we need
665 to start two entries earlier */ 672 to start two entries earlier */
666 673
667 dump_cifs_file_struct(file, "In fce "); 674 dump_cifs_file_struct(file, "In fce ");
668 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) && 675 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
669 is_dir_changed(file)) || 676 is_dir_changed(file)) ||
670 (index_to_find < first_entry_in_buffer)) { 677 (index_to_find < first_entry_in_buffer)) {
671 /* close and restart search */ 678 /* close and restart search */
672 cFYI(1, ("search backing up - close and restart search")); 679 cFYI(1, ("search backing up - close and restart search"));
673 if (!cifsFile->srch_inf.endOfSearch && 680 if (!cifsFile->srch_inf.endOfSearch &&
674 !cifsFile->invalidHandle) { 681 !cifsFile->invalidHandle) {
675 cifsFile->invalidHandle = true; 682 cifsFile->invalidHandle = true;
676 CIFSFindClose(xid, pTcon, cifsFile->netfid); 683 CIFSFindClose(xid, pTcon, cifsFile->netfid);
677 } 684 }
678 if (cifsFile->srch_inf.ntwrk_buf_start) { 685 if (cifsFile->srch_inf.ntwrk_buf_start) {
679 cFYI(1, ("freeing SMB ff cache buf on search rewind")); 686 cFYI(1, ("freeing SMB ff cache buf on search rewind"));
680 if (cifsFile->srch_inf.smallBuf) 687 if (cifsFile->srch_inf.smallBuf)
681 cifs_small_buf_release(cifsFile->srch_inf. 688 cifs_small_buf_release(cifsFile->srch_inf.
682 ntwrk_buf_start); 689 ntwrk_buf_start);
683 else 690 else
684 cifs_buf_release(cifsFile->srch_inf. 691 cifs_buf_release(cifsFile->srch_inf.
685 ntwrk_buf_start); 692 ntwrk_buf_start);
686 } 693 }
687 rc = initiate_cifs_search(xid, file); 694 rc = initiate_cifs_search(xid, file);
688 if (rc) { 695 if (rc) {
689 cFYI(1, ("error %d reinitiating a search on rewind", 696 cFYI(1, ("error %d reinitiating a search on rewind",
690 rc)); 697 rc));
691 return rc; 698 return rc;
692 } 699 }
693 } 700 }
694 701
695 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) && 702 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
696 (rc == 0) && !cifsFile->srch_inf.endOfSearch) { 703 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
697 cFYI(1, ("calling findnext2")); 704 cFYI(1, ("calling findnext2"));
698 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid, 705 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
699 &cifsFile->srch_inf); 706 &cifsFile->srch_inf);
700 if (rc) 707 if (rc)
701 return -ENOENT; 708 return -ENOENT;
702 } 709 }
703 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) { 710 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
704 /* we found the buffer that contains the entry */ 711 /* we found the buffer that contains the entry */
705 /* scan and find it */ 712 /* scan and find it */
706 int i; 713 int i;
707 char *current_entry; 714 char *current_entry;
708 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + 715 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
709 smbCalcSize((struct smb_hdr *) 716 smbCalcSize((struct smb_hdr *)
710 cifsFile->srch_inf.ntwrk_buf_start); 717 cifsFile->srch_inf.ntwrk_buf_start);
711 718
712 current_entry = cifsFile->srch_inf.srch_entries_start; 719 current_entry = cifsFile->srch_inf.srch_entries_start;
713 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry 720 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
714 - cifsFile->srch_inf.entries_in_buffer; 721 - cifsFile->srch_inf.entries_in_buffer;
715 pos_in_buf = index_to_find - first_entry_in_buffer; 722 pos_in_buf = index_to_find - first_entry_in_buffer;
716 cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf)); 723 cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf));
717 724
718 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) { 725 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
719 /* go entry by entry figuring out which is first */ 726 /* go entry by entry figuring out which is first */
720 current_entry = nxt_dir_entry(current_entry, end_of_smb, 727 current_entry = nxt_dir_entry(current_entry, end_of_smb,
721 cifsFile->srch_inf.info_level); 728 cifsFile->srch_inf.info_level);
722 } 729 }
723 if ((current_entry == NULL) && (i < pos_in_buf)) { 730 if ((current_entry == NULL) && (i < pos_in_buf)) {
724 /* BB fixme - check if we should flag this error */ 731 /* BB fixme - check if we should flag this error */
725 cERROR(1, ("reached end of buf searching for pos in buf" 732 cERROR(1, ("reached end of buf searching for pos in buf"
726 " %d index to find %lld rc %d", 733 " %d index to find %lld rc %d",
727 pos_in_buf, index_to_find, rc)); 734 pos_in_buf, index_to_find, rc));
728 } 735 }
729 rc = 0; 736 rc = 0;
730 *ppCurrentEntry = current_entry; 737 *ppCurrentEntry = current_entry;
731 } else { 738 } else {
732 cFYI(1, ("index not in buffer - could not findnext into it")); 739 cFYI(1, ("index not in buffer - could not findnext into it"));
733 return 0; 740 return 0;
734 } 741 }
735 742
736 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) { 743 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
737 cFYI(1, ("can not return entries pos_in_buf beyond last")); 744 cFYI(1, ("can not return entries pos_in_buf beyond last"));
738 *num_to_ret = 0; 745 *num_to_ret = 0;
739 } else 746 } else
740 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf; 747 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
741 748
742 return rc; 749 return rc;
743 } 750 }
744 751
745 /* inode num, inode type and filename returned */ 752 /* inode num, inode type and filename returned */
746 static int cifs_get_name_from_search_buf(struct qstr *pqst, 753 static int cifs_get_name_from_search_buf(struct qstr *pqst,
747 char *current_entry, __u16 level, unsigned int unicode, 754 char *current_entry, __u16 level, unsigned int unicode,
748 struct cifs_sb_info *cifs_sb, int max_len, ino_t *pinum) 755 struct cifs_sb_info *cifs_sb, int max_len, ino_t *pinum)
749 { 756 {
750 int rc = 0; 757 int rc = 0;
751 unsigned int len = 0; 758 unsigned int len = 0;
752 char *filename; 759 char *filename;
753 struct nls_table *nlt = cifs_sb->local_nls; 760 struct nls_table *nlt = cifs_sb->local_nls;
754 761
755 *pinum = 0; 762 *pinum = 0;
756 763
757 if (level == SMB_FIND_FILE_UNIX) { 764 if (level == SMB_FIND_FILE_UNIX) {
758 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry; 765 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
759 766
760 filename = &pFindData->FileName[0]; 767 filename = &pFindData->FileName[0];
761 if (unicode) { 768 if (unicode) {
762 len = cifs_unicode_bytelen(filename); 769 len = cifs_unicode_bytelen(filename);
763 } else { 770 } else {
764 /* BB should we make this strnlen of PATH_MAX? */ 771 /* BB should we make this strnlen of PATH_MAX? */
765 len = strnlen(filename, PATH_MAX); 772 len = strnlen(filename, PATH_MAX);
766 } 773 }
767 774
768 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB */ 775 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB */
769 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) 776 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
770 *pinum = pFindData->UniqueId; 777 *pinum = pFindData->UniqueId;
771 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) { 778 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
772 FILE_DIRECTORY_INFO *pFindData = 779 FILE_DIRECTORY_INFO *pFindData =
773 (FILE_DIRECTORY_INFO *)current_entry; 780 (FILE_DIRECTORY_INFO *)current_entry;
774 filename = &pFindData->FileName[0]; 781 filename = &pFindData->FileName[0];
775 len = le32_to_cpu(pFindData->FileNameLength); 782 len = le32_to_cpu(pFindData->FileNameLength);
776 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) { 783 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
777 FILE_FULL_DIRECTORY_INFO *pFindData = 784 FILE_FULL_DIRECTORY_INFO *pFindData =
778 (FILE_FULL_DIRECTORY_INFO *)current_entry; 785 (FILE_FULL_DIRECTORY_INFO *)current_entry;
779 filename = &pFindData->FileName[0]; 786 filename = &pFindData->FileName[0];
780 len = le32_to_cpu(pFindData->FileNameLength); 787 len = le32_to_cpu(pFindData->FileNameLength);
781 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) { 788 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
782 SEARCH_ID_FULL_DIR_INFO *pFindData = 789 SEARCH_ID_FULL_DIR_INFO *pFindData =
783 (SEARCH_ID_FULL_DIR_INFO *)current_entry; 790 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
784 filename = &pFindData->FileName[0]; 791 filename = &pFindData->FileName[0];
785 len = le32_to_cpu(pFindData->FileNameLength); 792 len = le32_to_cpu(pFindData->FileNameLength);
786 *pinum = pFindData->UniqueId; 793 *pinum = pFindData->UniqueId;
787 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) { 794 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
788 FILE_BOTH_DIRECTORY_INFO *pFindData = 795 FILE_BOTH_DIRECTORY_INFO *pFindData =
789 (FILE_BOTH_DIRECTORY_INFO *)current_entry; 796 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
790 filename = &pFindData->FileName[0]; 797 filename = &pFindData->FileName[0];
791 len = le32_to_cpu(pFindData->FileNameLength); 798 len = le32_to_cpu(pFindData->FileNameLength);
792 } else if (level == SMB_FIND_FILE_INFO_STANDARD) { 799 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
793 FIND_FILE_STANDARD_INFO *pFindData = 800 FIND_FILE_STANDARD_INFO *pFindData =
794 (FIND_FILE_STANDARD_INFO *)current_entry; 801 (FIND_FILE_STANDARD_INFO *)current_entry;
795 filename = &pFindData->FileName[0]; 802 filename = &pFindData->FileName[0];
796 /* one byte length, no name conversion */ 803 /* one byte length, no name conversion */
797 len = (unsigned int)pFindData->FileNameLength; 804 len = (unsigned int)pFindData->FileNameLength;
798 } else { 805 } else {
799 cFYI(1, ("Unknown findfirst level %d", level)); 806 cFYI(1, ("Unknown findfirst level %d", level));
800 return -EINVAL; 807 return -EINVAL;
801 } 808 }
802 809
803 if (len > max_len) { 810 if (len > max_len) {
804 cERROR(1, ("bad search response length %d past smb end", len)); 811 cERROR(1, ("bad search response length %d past smb end", len));
805 return -EINVAL; 812 return -EINVAL;
806 } 813 }
807 814
808 if (unicode) { 815 if (unicode) {
809 /* BB fixme - test with long names */ 816 /* BB fixme - test with long names */
810 /* Note converted filename can be longer than in unicode */ 817 /* Note converted filename can be longer than in unicode */
811 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR) 818 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
812 pqst->len = cifs_convertUCSpath((char *)pqst->name, 819 pqst->len = cifs_convertUCSpath((char *)pqst->name,
813 (__le16 *)filename, len/2, nlt); 820 (__le16 *)filename, len/2, nlt);
814 else 821 else
815 pqst->len = cifs_strfromUCS_le((char *)pqst->name, 822 pqst->len = cifs_strfromUCS_le((char *)pqst->name,
816 (__le16 *)filename, len/2, nlt); 823 (__le16 *)filename, len/2, nlt);
817 } else { 824 } else {
818 pqst->name = filename; 825 pqst->name = filename;
819 pqst->len = len; 826 pqst->len = len;
820 } 827 }
821 pqst->hash = full_name_hash(pqst->name, pqst->len); 828 pqst->hash = full_name_hash(pqst->name, pqst->len);
822 /* cFYI(1, ("filldir on %s",pqst->name)); */ 829 /* cFYI(1, ("filldir on %s",pqst->name)); */
823 return rc; 830 return rc;
824 } 831 }
825 832
826 static int cifs_filldir(char *pfindEntry, struct file *file, 833 static int cifs_filldir(char *pfindEntry, struct file *file,
827 filldir_t filldir, void *direntry, char *scratch_buf, int max_len) 834 filldir_t filldir, void *direntry, char *scratch_buf, int max_len)
828 { 835 {
829 int rc = 0; 836 int rc = 0;
830 struct qstr qstring; 837 struct qstr qstring;
831 struct cifsFileInfo *pCifsF; 838 struct cifsFileInfo *pCifsF;
832 unsigned int obj_type; 839 unsigned int obj_type;
833 ino_t inum; 840 ino_t inum;
834 struct cifs_sb_info *cifs_sb; 841 struct cifs_sb_info *cifs_sb;
835 struct inode *tmp_inode; 842 struct inode *tmp_inode;
836 struct dentry *tmp_dentry; 843 struct dentry *tmp_dentry;
837 844
838 /* get filename and len into qstring */ 845 /* get filename and len into qstring */
839 /* get dentry */ 846 /* get dentry */
840 /* decide whether to create and populate ionde */ 847 /* decide whether to create and populate ionde */
841 if ((direntry == NULL) || (file == NULL)) 848 if ((direntry == NULL) || (file == NULL))
842 return -EINVAL; 849 return -EINVAL;
843 850
844 pCifsF = file->private_data; 851 pCifsF = file->private_data;
845 852
846 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL)) 853 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
847 return -ENOENT; 854 return -ENOENT;
848 855
849 rc = cifs_entry_is_dot(pfindEntry, pCifsF); 856 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
850 /* skip . and .. since we added them first */ 857 /* skip . and .. since we added them first */
851 if (rc != 0) 858 if (rc != 0)
852 return 0; 859 return 0;
853 860
854 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); 861 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
855 862
856 qstring.name = scratch_buf; 863 qstring.name = scratch_buf;
857 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry, 864 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
858 pCifsF->srch_inf.info_level, 865 pCifsF->srch_inf.info_level,
859 pCifsF->srch_inf.unicode, cifs_sb, 866 pCifsF->srch_inf.unicode, cifs_sb,
860 max_len, 867 max_len,
861 &inum /* returned */); 868 &inum /* returned */);
862 869
863 if (rc) 870 if (rc)
864 return rc; 871 return rc;
865 872
866 rc = construct_dentry(&qstring, file, &tmp_inode, &tmp_dentry); 873 rc = construct_dentry(&qstring, file, &tmp_inode, &tmp_dentry);
867 if ((tmp_inode == NULL) || (tmp_dentry == NULL)) 874 if ((tmp_inode == NULL) || (tmp_dentry == NULL))
868 return -ENOMEM; 875 return -ENOMEM;
869 876
870 if (rc) { 877 if (rc) {
871 /* inode created, we need to hash it with right inode number */ 878 /* inode created, we need to hash it with right inode number */
872 if (inum != 0) { 879 if (inum != 0) {
873 /* BB fixme - hash the 2 32 quantities bits together if 880 /* BB fixme - hash the 2 32 quantities bits together if
874 * necessary BB */ 881 * necessary BB */
875 tmp_inode->i_ino = inum; 882 tmp_inode->i_ino = inum;
876 } 883 }
877 insert_inode_hash(tmp_inode); 884 insert_inode_hash(tmp_inode);
878 } 885 }
879 886
880 /* we pass in rc below, indicating whether it is a new inode, 887 /* we pass in rc below, indicating whether it is a new inode,
881 so we can figure out whether to invalidate the inode cached 888 so we can figure out whether to invalidate the inode cached
882 data if the file has changed */ 889 data if the file has changed */
883 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX) 890 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
884 unix_fill_in_inode(tmp_inode, 891 unix_fill_in_inode(tmp_inode,
885 (FILE_UNIX_INFO *)pfindEntry, 892 (FILE_UNIX_INFO *)pfindEntry,
886 &obj_type, rc); 893 &obj_type, rc);
887 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) 894 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
888 fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */, 895 fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */,
889 pfindEntry, &obj_type, rc); 896 pfindEntry, &obj_type, rc);
890 else 897 else
891 fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc); 898 fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc);
892 899
893 if (rc) /* new inode - needs to be tied to dentry */ { 900 if (rc) /* new inode - needs to be tied to dentry */ {
894 d_instantiate(tmp_dentry, tmp_inode); 901 d_instantiate(tmp_dentry, tmp_inode);
895 if (rc == 2) 902 if (rc == 2)
896 d_rehash(tmp_dentry); 903 d_rehash(tmp_dentry);
897 } 904 }
898 905
899 906
900 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos, 907 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
901 tmp_inode->i_ino, obj_type); 908 tmp_inode->i_ino, obj_type);
902 if (rc) { 909 if (rc) {
903 cFYI(1, ("filldir rc = %d", rc)); 910 cFYI(1, ("filldir rc = %d", rc));
904 /* we can not return filldir errors to the caller 911 /* we can not return filldir errors to the caller
905 since they are "normal" when the stat blocksize 912 since they are "normal" when the stat blocksize
906 is too small - we return remapped error instead */ 913 is too small - we return remapped error instead */
907 rc = -EOVERFLOW; 914 rc = -EOVERFLOW;
908 } 915 }
909 916
910 dput(tmp_dentry); 917 dput(tmp_dentry);
911 return rc; 918 return rc;
912 } 919 }
913 920
914 static int cifs_save_resume_key(const char *current_entry, 921 static int cifs_save_resume_key(const char *current_entry,
915 struct cifsFileInfo *cifsFile) 922 struct cifsFileInfo *cifsFile)
916 { 923 {
917 int rc = 0; 924 int rc = 0;
918 unsigned int len = 0; 925 unsigned int len = 0;
919 __u16 level; 926 __u16 level;
920 char *filename; 927 char *filename;
921 928
922 if ((cifsFile == NULL) || (current_entry == NULL)) 929 if ((cifsFile == NULL) || (current_entry == NULL))
923 return -EINVAL; 930 return -EINVAL;
924 931
925 level = cifsFile->srch_inf.info_level; 932 level = cifsFile->srch_inf.info_level;
926 933
927 if (level == SMB_FIND_FILE_UNIX) { 934 if (level == SMB_FIND_FILE_UNIX) {
928 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry; 935 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
929 936
930 filename = &pFindData->FileName[0]; 937 filename = &pFindData->FileName[0];
931 if (cifsFile->srch_inf.unicode) { 938 if (cifsFile->srch_inf.unicode) {
932 len = cifs_unicode_bytelen(filename); 939 len = cifs_unicode_bytelen(filename);
933 } else { 940 } else {
934 /* BB should we make this strnlen of PATH_MAX? */ 941 /* BB should we make this strnlen of PATH_MAX? */
935 len = strnlen(filename, PATH_MAX); 942 len = strnlen(filename, PATH_MAX);
936 } 943 }
937 cifsFile->srch_inf.resume_key = pFindData->ResumeKey; 944 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
938 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) { 945 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
939 FILE_DIRECTORY_INFO *pFindData = 946 FILE_DIRECTORY_INFO *pFindData =
940 (FILE_DIRECTORY_INFO *)current_entry; 947 (FILE_DIRECTORY_INFO *)current_entry;
941 filename = &pFindData->FileName[0]; 948 filename = &pFindData->FileName[0];
942 len = le32_to_cpu(pFindData->FileNameLength); 949 len = le32_to_cpu(pFindData->FileNameLength);
943 cifsFile->srch_inf.resume_key = pFindData->FileIndex; 950 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
944 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) { 951 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
945 FILE_FULL_DIRECTORY_INFO *pFindData = 952 FILE_FULL_DIRECTORY_INFO *pFindData =
946 (FILE_FULL_DIRECTORY_INFO *)current_entry; 953 (FILE_FULL_DIRECTORY_INFO *)current_entry;
947 filename = &pFindData->FileName[0]; 954 filename = &pFindData->FileName[0];
948 len = le32_to_cpu(pFindData->FileNameLength); 955 len = le32_to_cpu(pFindData->FileNameLength);
949 cifsFile->srch_inf.resume_key = pFindData->FileIndex; 956 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
950 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) { 957 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
951 SEARCH_ID_FULL_DIR_INFO *pFindData = 958 SEARCH_ID_FULL_DIR_INFO *pFindData =
952 (SEARCH_ID_FULL_DIR_INFO *)current_entry; 959 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
953 filename = &pFindData->FileName[0]; 960 filename = &pFindData->FileName[0];
954 len = le32_to_cpu(pFindData->FileNameLength); 961 len = le32_to_cpu(pFindData->FileNameLength);
955 cifsFile->srch_inf.resume_key = pFindData->FileIndex; 962 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
956 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) { 963 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
957 FILE_BOTH_DIRECTORY_INFO *pFindData = 964 FILE_BOTH_DIRECTORY_INFO *pFindData =
958 (FILE_BOTH_DIRECTORY_INFO *)current_entry; 965 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
959 filename = &pFindData->FileName[0]; 966 filename = &pFindData->FileName[0];
960 len = le32_to_cpu(pFindData->FileNameLength); 967 len = le32_to_cpu(pFindData->FileNameLength);
961 cifsFile->srch_inf.resume_key = pFindData->FileIndex; 968 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
962 } else if (level == SMB_FIND_FILE_INFO_STANDARD) { 969 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
963 FIND_FILE_STANDARD_INFO *pFindData = 970 FIND_FILE_STANDARD_INFO *pFindData =
964 (FIND_FILE_STANDARD_INFO *)current_entry; 971 (FIND_FILE_STANDARD_INFO *)current_entry;
965 filename = &pFindData->FileName[0]; 972 filename = &pFindData->FileName[0];
966 /* one byte length, no name conversion */ 973 /* one byte length, no name conversion */
967 len = (unsigned int)pFindData->FileNameLength; 974 len = (unsigned int)pFindData->FileNameLength;
968 cifsFile->srch_inf.resume_key = pFindData->ResumeKey; 975 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
969 } else { 976 } else {
970 cFYI(1, ("Unknown findfirst level %d", level)); 977 cFYI(1, ("Unknown findfirst level %d", level));
971 return -EINVAL; 978 return -EINVAL;
972 } 979 }
973 cifsFile->srch_inf.resume_name_len = len; 980 cifsFile->srch_inf.resume_name_len = len;
974 cifsFile->srch_inf.presume_name = filename; 981 cifsFile->srch_inf.presume_name = filename;
975 return rc; 982 return rc;
976 } 983 }
977 984
978 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir) 985 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
979 { 986 {
980 int rc = 0; 987 int rc = 0;
981 int xid, i; 988 int xid, i;
982 struct cifs_sb_info *cifs_sb; 989 struct cifs_sb_info *cifs_sb;
983 struct cifsTconInfo *pTcon; 990 struct cifsTconInfo *pTcon;
984 struct cifsFileInfo *cifsFile = NULL; 991 struct cifsFileInfo *cifsFile = NULL;
985 char *current_entry; 992 char *current_entry;
986 int num_to_fill = 0; 993 int num_to_fill = 0;
987 char *tmp_buf = NULL; 994 char *tmp_buf = NULL;
988 char *end_of_smb; 995 char *end_of_smb;
989 int max_len; 996 int max_len;
990 997
991 xid = GetXid(); 998 xid = GetXid();
992 999
993 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); 1000 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
994 pTcon = cifs_sb->tcon; 1001 pTcon = cifs_sb->tcon;
995 if (pTcon == NULL) 1002 if (pTcon == NULL)
996 return -EINVAL; 1003 return -EINVAL;
997 1004
998 switch ((int) file->f_pos) { 1005 switch ((int) file->f_pos) {
999 case 0: 1006 case 0:
1000 if (filldir(direntry, ".", 1, file->f_pos, 1007 if (filldir(direntry, ".", 1, file->f_pos,
1001 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) { 1008 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
1002 cERROR(1, ("Filldir for current dir failed")); 1009 cERROR(1, ("Filldir for current dir failed"));
1003 rc = -ENOMEM; 1010 rc = -ENOMEM;
1004 break; 1011 break;
1005 } 1012 }
1006 file->f_pos++; 1013 file->f_pos++;
1007 case 1: 1014 case 1:
1008 if (filldir(direntry, "..", 2, file->f_pos, 1015 if (filldir(direntry, "..", 2, file->f_pos,
1009 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) { 1016 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
1010 cERROR(1, ("Filldir for parent dir failed")); 1017 cERROR(1, ("Filldir for parent dir failed"));
1011 rc = -ENOMEM; 1018 rc = -ENOMEM;
1012 break; 1019 break;
1013 } 1020 }
1014 file->f_pos++; 1021 file->f_pos++;
1015 default: 1022 default:
1016 /* 1) If search is active, 1023 /* 1) If search is active,
1017 is in current search buffer? 1024 is in current search buffer?
1018 if it before then restart search 1025 if it before then restart search
1019 if after then keep searching till find it */ 1026 if after then keep searching till find it */
1020 1027
1021 if (file->private_data == NULL) { 1028 if (file->private_data == NULL) {
1022 rc = initiate_cifs_search(xid, file); 1029 rc = initiate_cifs_search(xid, file);
1023 cFYI(1, ("initiate cifs search rc %d", rc)); 1030 cFYI(1, ("initiate cifs search rc %d", rc));
1024 if (rc) { 1031 if (rc) {
1025 FreeXid(xid); 1032 FreeXid(xid);
1026 return rc; 1033 return rc;
1027 } 1034 }
1028 } 1035 }
1029 if (file->private_data == NULL) { 1036 if (file->private_data == NULL) {
1030 rc = -EINVAL; 1037 rc = -EINVAL;
1031 FreeXid(xid); 1038 FreeXid(xid);
1032 return rc; 1039 return rc;
1033 } 1040 }
1034 cifsFile = file->private_data; 1041 cifsFile = file->private_data;
1035 if (cifsFile->srch_inf.endOfSearch) { 1042 if (cifsFile->srch_inf.endOfSearch) {
1036 if (cifsFile->srch_inf.emptyDir) { 1043 if (cifsFile->srch_inf.emptyDir) {
1037 cFYI(1, ("End of search, empty dir")); 1044 cFYI(1, ("End of search, empty dir"));
1038 rc = 0; 1045 rc = 0;
1039 break; 1046 break;
1040 } 1047 }
1041 } /* else { 1048 } /* else {
1042 cifsFile->invalidHandle = true; 1049 cifsFile->invalidHandle = true;
1043 CIFSFindClose(xid, pTcon, cifsFile->netfid); 1050 CIFSFindClose(xid, pTcon, cifsFile->netfid);
1044 } */ 1051 } */
1045 1052
1046 rc = find_cifs_entry(xid, pTcon, file, 1053 rc = find_cifs_entry(xid, pTcon, file,
1047 &current_entry, &num_to_fill); 1054 &current_entry, &num_to_fill);
1048 if (rc) { 1055 if (rc) {
1049 cFYI(1, ("fce error %d", rc)); 1056 cFYI(1, ("fce error %d", rc));
1050 goto rddir2_exit; 1057 goto rddir2_exit;
1051 } else if (current_entry != NULL) { 1058 } else if (current_entry != NULL) {
1052 cFYI(1, ("entry %lld found", file->f_pos)); 1059 cFYI(1, ("entry %lld found", file->f_pos));
1053 } else { 1060 } else {
1054 cFYI(1, ("could not find entry")); 1061 cFYI(1, ("could not find entry"));
1055 goto rddir2_exit; 1062 goto rddir2_exit;
1056 } 1063 }
1057 cFYI(1, ("loop through %d times filling dir for net buf %p", 1064 cFYI(1, ("loop through %d times filling dir for net buf %p",
1058 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start)); 1065 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
1059 max_len = smbCalcSize((struct smb_hdr *) 1066 max_len = smbCalcSize((struct smb_hdr *)
1060 cifsFile->srch_inf.ntwrk_buf_start); 1067 cifsFile->srch_inf.ntwrk_buf_start);
1061 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len; 1068 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1062 1069
1063 /* To be safe - for UCS to UTF-8 with strings loaded 1070 /* To be safe - for UCS to UTF-8 with strings loaded
1064 with the rare long characters alloc more to account for 1071 with the rare long characters alloc more to account for
1065 such multibyte target UTF-8 characters. cifs_unicode.c, 1072 such multibyte target UTF-8 characters. cifs_unicode.c,
1066 which actually does the conversion, has the same limit */ 1073 which actually does the conversion, has the same limit */
1067 tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL); 1074 tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
1068 for (i = 0; (i < num_to_fill) && (rc == 0); i++) { 1075 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
1069 if (current_entry == NULL) { 1076 if (current_entry == NULL) {
1070 /* evaluate whether this case is an error */ 1077 /* evaluate whether this case is an error */
1071 cERROR(1, ("past SMB end, num to fill %d i %d", 1078 cERROR(1, ("past SMB end, num to fill %d i %d",
1072 num_to_fill, i)); 1079 num_to_fill, i));
1073 break; 1080 break;
1074 } 1081 }
1075 /* if buggy server returns . and .. late do 1082 /* if buggy server returns . and .. late do
1076 we want to check for that here? */ 1083 we want to check for that here? */
1077 rc = cifs_filldir(current_entry, file, 1084 rc = cifs_filldir(current_entry, file,
1078 filldir, direntry, tmp_buf, max_len); 1085 filldir, direntry, tmp_buf, max_len);
1079 if (rc == -EOVERFLOW) { 1086 if (rc == -EOVERFLOW) {
1080 rc = 0; 1087 rc = 0;
1081 break; 1088 break;
1082 } 1089 }
1083 1090
1084 file->f_pos++; 1091 file->f_pos++;
1085 if (file->f_pos == 1092 if (file->f_pos ==
1086 cifsFile->srch_inf.index_of_last_entry) { 1093 cifsFile->srch_inf.index_of_last_entry) {
1087 cFYI(1, ("last entry in buf at pos %lld %s", 1094 cFYI(1, ("last entry in buf at pos %lld %s",
1088 file->f_pos, tmp_buf)); 1095 file->f_pos, tmp_buf));
1089 cifs_save_resume_key(current_entry, cifsFile); 1096 cifs_save_resume_key(current_entry, cifsFile);
1090 break; 1097 break;
1091 } else 1098 } else
1092 current_entry = 1099 current_entry =
1093 nxt_dir_entry(current_entry, end_of_smb, 1100 nxt_dir_entry(current_entry, end_of_smb,
1094 cifsFile->srch_inf.info_level); 1101 cifsFile->srch_inf.info_level);
1095 } 1102 }
1096 kfree(tmp_buf); 1103 kfree(tmp_buf);
1097 break; 1104 break;