Commit a6ce4932fbdbcd8f8e8c6df76812014351c32892

Authored by Steve French
1 parent d9fb5c091b

[CIFS] Add support for posix open during lookup

This patch by utilizing lookup intents, and thus removing a network
roundtrip in the open path, improves performance dramatically on
open (30% or more) to Samba and other servers which support the
cifs posix extensions

Signed-off-by: Shirish Pargaonkar <shirishp@us.ibm.com>
Signed-off-by: Steve French <sfrench@us.ibm.com>

Showing 3 changed files with 118 additions and 80 deletions Inline Diff

1 /* 1 /*
2 * fs/cifs/cifsglob.h 2 * fs/cifs/cifsglob.h
3 * 3 *
4 * Copyright (C) International Business Machines Corp., 2002,2008 4 * Copyright (C) International Business Machines Corp., 2002,2008
5 * Author(s): Steve French (sfrench@us.ibm.com) 5 * Author(s): Steve French (sfrench@us.ibm.com)
6 * Jeremy Allison (jra@samba.org) 6 * Jeremy Allison (jra@samba.org)
7 * 7 *
8 * This library is free software; you can redistribute it and/or modify 8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published 9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; either version 2.1 of the License, or 10 * by the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 * 12 *
13 * This library is distributed in the hope that it will be useful, 13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
16 * the GNU Lesser General Public License for more details. 16 * the GNU Lesser General Public License for more details.
17 * 17 *
18 */ 18 */
19 #include <linux/in.h> 19 #include <linux/in.h>
20 #include <linux/in6.h> 20 #include <linux/in6.h>
21 #include "cifs_fs_sb.h" 21 #include "cifs_fs_sb.h"
22 #include "cifsacl.h" 22 #include "cifsacl.h"
23 /* 23 /*
24 * The sizes of various internal tables and strings 24 * The sizes of various internal tables and strings
25 */ 25 */
26 #define MAX_UID_INFO 16 26 #define MAX_UID_INFO 16
27 #define MAX_SES_INFO 2 27 #define MAX_SES_INFO 2
28 #define MAX_TCON_INFO 4 28 #define MAX_TCON_INFO 4
29 29
30 #define MAX_TREE_SIZE (2 + MAX_SERVER_SIZE + 1 + MAX_SHARE_SIZE + 1) 30 #define MAX_TREE_SIZE (2 + MAX_SERVER_SIZE + 1 + MAX_SHARE_SIZE + 1)
31 #define MAX_SERVER_SIZE 15 31 #define MAX_SERVER_SIZE 15
32 #define MAX_SHARE_SIZE 64 /* used to be 20, this should still be enough */ 32 #define MAX_SHARE_SIZE 64 /* used to be 20, this should still be enough */
33 #define MAX_USERNAME_SIZE 32 /* 32 is to allow for 15 char names + null 33 #define MAX_USERNAME_SIZE 32 /* 32 is to allow for 15 char names + null
34 termination then *2 for unicode versions */ 34 termination then *2 for unicode versions */
35 #define MAX_PASSWORD_SIZE 16 35 #define MAX_PASSWORD_SIZE 16
36 36
37 #define CIFS_MIN_RCV_POOL 4 37 #define CIFS_MIN_RCV_POOL 4
38 38
39 /* 39 /*
40 * MAX_REQ is the maximum number of requests that WE will send 40 * MAX_REQ is the maximum number of requests that WE will send
41 * on one socket concurently. It also matches the most common 41 * on one socket concurently. It also matches the most common
42 * value of max multiplex returned by servers. We may 42 * value of max multiplex returned by servers. We may
43 * eventually want to use the negotiated value (in case 43 * eventually want to use the negotiated value (in case
44 * future servers can handle more) when we are more confident that 44 * future servers can handle more) when we are more confident that
45 * we will not have problems oveloading the socket with pending 45 * we will not have problems oveloading the socket with pending
46 * write data. 46 * write data.
47 */ 47 */
48 #define CIFS_MAX_REQ 50 48 #define CIFS_MAX_REQ 50
49 49
50 #define RFC1001_NAME_LEN 15 50 #define RFC1001_NAME_LEN 15
51 #define RFC1001_NAME_LEN_WITH_NULL (RFC1001_NAME_LEN + 1) 51 #define RFC1001_NAME_LEN_WITH_NULL (RFC1001_NAME_LEN + 1)
52 52
53 /* currently length of NIP6_FMT */ 53 /* currently length of NIP6_FMT */
54 #define SERVER_NAME_LENGTH 40 54 #define SERVER_NAME_LENGTH 40
55 #define SERVER_NAME_LEN_WITH_NULL (SERVER_NAME_LENGTH + 1) 55 #define SERVER_NAME_LEN_WITH_NULL (SERVER_NAME_LENGTH + 1)
56 56
57 /* used to define string lengths for reversing unicode strings */ 57 /* used to define string lengths for reversing unicode strings */
58 /* (256+1)*2 = 514 */ 58 /* (256+1)*2 = 514 */
59 /* (max path length + 1 for null) * 2 for unicode */ 59 /* (max path length + 1 for null) * 2 for unicode */
60 #define MAX_NAME 514 60 #define MAX_NAME 514
61 61
62 #include "cifspdu.h" 62 #include "cifspdu.h"
63 63
64 #ifndef XATTR_DOS_ATTRIB 64 #ifndef XATTR_DOS_ATTRIB
65 #define XATTR_DOS_ATTRIB "user.DOSATTRIB" 65 #define XATTR_DOS_ATTRIB "user.DOSATTRIB"
66 #endif 66 #endif
67 67
68 /* 68 /*
69 * CIFS vfs client Status information (based on what we know.) 69 * CIFS vfs client Status information (based on what we know.)
70 */ 70 */
71 71
72 /* associated with each tcp and smb session */ 72 /* associated with each tcp and smb session */
73 enum statusEnum { 73 enum statusEnum {
74 CifsNew = 0, 74 CifsNew = 0,
75 CifsGood, 75 CifsGood,
76 CifsExiting, 76 CifsExiting,
77 CifsNeedReconnect 77 CifsNeedReconnect
78 }; 78 };
79 79
80 enum securityEnum { 80 enum securityEnum {
81 PLAINTXT = 0, /* Legacy with Plaintext passwords */ 81 PLAINTXT = 0, /* Legacy with Plaintext passwords */
82 LANMAN, /* Legacy LANMAN auth */ 82 LANMAN, /* Legacy LANMAN auth */
83 NTLM, /* Legacy NTLM012 auth with NTLM hash */ 83 NTLM, /* Legacy NTLM012 auth with NTLM hash */
84 NTLMv2, /* Legacy NTLM auth with NTLMv2 hash */ 84 NTLMv2, /* Legacy NTLM auth with NTLMv2 hash */
85 RawNTLMSSP, /* NTLMSSP without SPNEGO */ 85 RawNTLMSSP, /* NTLMSSP without SPNEGO */
86 NTLMSSP, /* NTLMSSP via SPNEGO */ 86 NTLMSSP, /* NTLMSSP via SPNEGO */
87 Kerberos, /* Kerberos via SPNEGO */ 87 Kerberos, /* Kerberos via SPNEGO */
88 MSKerberos, /* MS Kerberos via SPNEGO */ 88 MSKerberos, /* MS Kerberos via SPNEGO */
89 }; 89 };
90 90
91 enum protocolEnum { 91 enum protocolEnum {
92 TCP = 0, 92 TCP = 0,
93 SCTP 93 SCTP
94 /* Netbios frames protocol not supported at this time */ 94 /* Netbios frames protocol not supported at this time */
95 }; 95 };
96 96
97 struct mac_key { 97 struct mac_key {
98 unsigned int len; 98 unsigned int len;
99 union { 99 union {
100 char ntlm[CIFS_SESS_KEY_SIZE + 16]; 100 char ntlm[CIFS_SESS_KEY_SIZE + 16];
101 char krb5[CIFS_SESS_KEY_SIZE + 16]; /* BB: length correct? */ 101 char krb5[CIFS_SESS_KEY_SIZE + 16]; /* BB: length correct? */
102 struct { 102 struct {
103 char key[16]; 103 char key[16];
104 struct ntlmv2_resp resp; 104 struct ntlmv2_resp resp;
105 } ntlmv2; 105 } ntlmv2;
106 } data; 106 } data;
107 }; 107 };
108 108
109 struct cifs_cred { 109 struct cifs_cred {
110 int uid; 110 int uid;
111 int gid; 111 int gid;
112 int mode; 112 int mode;
113 int cecount; 113 int cecount;
114 struct cifs_sid osid; 114 struct cifs_sid osid;
115 struct cifs_sid gsid; 115 struct cifs_sid gsid;
116 struct cifs_ntace *ntaces; 116 struct cifs_ntace *ntaces;
117 struct cifs_ace *aces; 117 struct cifs_ace *aces;
118 }; 118 };
119 119
120 /* 120 /*
121 ***************************************************************** 121 *****************************************************************
122 * Except the CIFS PDUs themselves all the 122 * Except the CIFS PDUs themselves all the
123 * globally interesting structs should go here 123 * globally interesting structs should go here
124 ***************************************************************** 124 *****************************************************************
125 */ 125 */
126 126
127 struct TCP_Server_Info { 127 struct TCP_Server_Info {
128 struct list_head tcp_ses_list; 128 struct list_head tcp_ses_list;
129 struct list_head smb_ses_list; 129 struct list_head smb_ses_list;
130 int srv_count; /* reference counter */ 130 int srv_count; /* reference counter */
131 /* 15 character server name + 0x20 16th byte indicating type = srv */ 131 /* 15 character server name + 0x20 16th byte indicating type = srv */
132 char server_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL]; 132 char server_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL];
133 char *hostname; /* hostname portion of UNC string */ 133 char *hostname; /* hostname portion of UNC string */
134 struct socket *ssocket; 134 struct socket *ssocket;
135 union { 135 union {
136 struct sockaddr_in sockAddr; 136 struct sockaddr_in sockAddr;
137 struct sockaddr_in6 sockAddr6; 137 struct sockaddr_in6 sockAddr6;
138 } addr; 138 } addr;
139 wait_queue_head_t response_q; 139 wait_queue_head_t response_q;
140 wait_queue_head_t request_q; /* if more than maxmpx to srvr must block*/ 140 wait_queue_head_t request_q; /* if more than maxmpx to srvr must block*/
141 struct list_head pending_mid_q; 141 struct list_head pending_mid_q;
142 void *Server_NlsInfo; /* BB - placeholder for future NLS info */ 142 void *Server_NlsInfo; /* BB - placeholder for future NLS info */
143 unsigned short server_codepage; /* codepage for the server */ 143 unsigned short server_codepage; /* codepage for the server */
144 unsigned long ip_address; /* IP addr for the server if known */ 144 unsigned long ip_address; /* IP addr for the server if known */
145 enum protocolEnum protocolType; 145 enum protocolEnum protocolType;
146 char versionMajor; 146 char versionMajor;
147 char versionMinor; 147 char versionMinor;
148 bool svlocal:1; /* local server or remote */ 148 bool svlocal:1; /* local server or remote */
149 bool noblocksnd; /* use blocking sendmsg */ 149 bool noblocksnd; /* use blocking sendmsg */
150 bool noautotune; /* do not autotune send buf sizes */ 150 bool noautotune; /* do not autotune send buf sizes */
151 atomic_t inFlight; /* number of requests on the wire to server */ 151 atomic_t inFlight; /* number of requests on the wire to server */
152 #ifdef CONFIG_CIFS_STATS2 152 #ifdef CONFIG_CIFS_STATS2
153 atomic_t inSend; /* requests trying to send */ 153 atomic_t inSend; /* requests trying to send */
154 atomic_t num_waiters; /* blocked waiting to get in sendrecv */ 154 atomic_t num_waiters; /* blocked waiting to get in sendrecv */
155 #endif 155 #endif
156 enum statusEnum tcpStatus; /* what we think the status is */ 156 enum statusEnum tcpStatus; /* what we think the status is */
157 struct mutex srv_mutex; 157 struct mutex srv_mutex;
158 struct task_struct *tsk; 158 struct task_struct *tsk;
159 char server_GUID[16]; 159 char server_GUID[16];
160 char secMode; 160 char secMode;
161 enum securityEnum secType; 161 enum securityEnum secType;
162 unsigned int maxReq; /* Clients should submit no more */ 162 unsigned int maxReq; /* Clients should submit no more */
163 /* than maxReq distinct unanswered SMBs to the server when using */ 163 /* than maxReq distinct unanswered SMBs to the server when using */
164 /* multiplexed reads or writes */ 164 /* multiplexed reads or writes */
165 unsigned int maxBuf; /* maxBuf specifies the maximum */ 165 unsigned int maxBuf; /* maxBuf specifies the maximum */
166 /* message size the server can send or receive for non-raw SMBs */ 166 /* message size the server can send or receive for non-raw SMBs */
167 unsigned int max_rw; /* maxRw specifies the maximum */ 167 unsigned int max_rw; /* maxRw specifies the maximum */
168 /* message size the server can send or receive for */ 168 /* message size the server can send or receive for */
169 /* SMB_COM_WRITE_RAW or SMB_COM_READ_RAW. */ 169 /* SMB_COM_WRITE_RAW or SMB_COM_READ_RAW. */
170 unsigned int max_vcs; /* maximum number of smb sessions, at least 170 unsigned int max_vcs; /* maximum number of smb sessions, at least
171 those that can be specified uniquely with 171 those that can be specified uniquely with
172 vcnumbers */ 172 vcnumbers */
173 char sessid[4]; /* unique token id for this session */ 173 char sessid[4]; /* unique token id for this session */
174 /* (returned on Negotiate */ 174 /* (returned on Negotiate */
175 int capabilities; /* allow selective disabling of caps by smb sess */ 175 int capabilities; /* allow selective disabling of caps by smb sess */
176 int timeAdj; /* Adjust for difference in server time zone in sec */ 176 int timeAdj; /* Adjust for difference in server time zone in sec */
177 __u16 CurrentMid; /* multiplex id - rotating counter */ 177 __u16 CurrentMid; /* multiplex id - rotating counter */
178 char cryptKey[CIFS_CRYPTO_KEY_SIZE]; 178 char cryptKey[CIFS_CRYPTO_KEY_SIZE];
179 /* 16th byte of RFC1001 workstation name is always null */ 179 /* 16th byte of RFC1001 workstation name is always null */
180 char workstation_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL]; 180 char workstation_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL];
181 __u32 sequence_number; /* needed for CIFS PDU signature */ 181 __u32 sequence_number; /* needed for CIFS PDU signature */
182 struct mac_key mac_signing_key; 182 struct mac_key mac_signing_key;
183 char ntlmv2_hash[16]; 183 char ntlmv2_hash[16];
184 unsigned long lstrp; /* when we got last response from this server */ 184 unsigned long lstrp; /* when we got last response from this server */
185 }; 185 };
186 186
187 /* 187 /*
188 * The following is our shortcut to user information. We surface the uid, 188 * The following is our shortcut to user information. We surface the uid,
189 * and name. We always get the password on the fly in case it 189 * and name. We always get the password on the fly in case it
190 * has changed. We also hang a list of sessions owned by this user off here. 190 * has changed. We also hang a list of sessions owned by this user off here.
191 */ 191 */
192 struct cifsUidInfo { 192 struct cifsUidInfo {
193 struct list_head userList; 193 struct list_head userList;
194 struct list_head sessionList; /* SMB sessions for this user */ 194 struct list_head sessionList; /* SMB sessions for this user */
195 uid_t linux_uid; 195 uid_t linux_uid;
196 char user[MAX_USERNAME_SIZE + 1]; /* ascii name of user */ 196 char user[MAX_USERNAME_SIZE + 1]; /* ascii name of user */
197 /* BB may need ptr or callback for PAM or WinBind info */ 197 /* BB may need ptr or callback for PAM or WinBind info */
198 }; 198 };
199 199
200 /* 200 /*
201 * Session structure. One of these for each uid session with a particular host 201 * Session structure. One of these for each uid session with a particular host
202 */ 202 */
203 struct cifsSesInfo { 203 struct cifsSesInfo {
204 struct list_head smb_ses_list; 204 struct list_head smb_ses_list;
205 struct list_head tcon_list; 205 struct list_head tcon_list;
206 struct semaphore sesSem; 206 struct semaphore sesSem;
207 #if 0 207 #if 0
208 struct cifsUidInfo *uidInfo; /* pointer to user info */ 208 struct cifsUidInfo *uidInfo; /* pointer to user info */
209 #endif 209 #endif
210 struct TCP_Server_Info *server; /* pointer to server info */ 210 struct TCP_Server_Info *server; /* pointer to server info */
211 int ses_count; /* reference counter */ 211 int ses_count; /* reference counter */
212 enum statusEnum status; 212 enum statusEnum status;
213 unsigned overrideSecFlg; /* if non-zero override global sec flags */ 213 unsigned overrideSecFlg; /* if non-zero override global sec flags */
214 __u16 ipc_tid; /* special tid for connection to IPC share */ 214 __u16 ipc_tid; /* special tid for connection to IPC share */
215 __u16 flags; 215 __u16 flags;
216 __u16 vcnum; 216 __u16 vcnum;
217 char *serverOS; /* name of operating system underlying server */ 217 char *serverOS; /* name of operating system underlying server */
218 char *serverNOS; /* name of network operating system of server */ 218 char *serverNOS; /* name of network operating system of server */
219 char *serverDomain; /* security realm of server */ 219 char *serverDomain; /* security realm of server */
220 int Suid; /* remote smb uid */ 220 int Suid; /* remote smb uid */
221 uid_t linux_uid; /* local Linux uid */ 221 uid_t linux_uid; /* local Linux uid */
222 int capabilities; 222 int capabilities;
223 char serverName[SERVER_NAME_LEN_WITH_NULL * 2]; /* BB make bigger for 223 char serverName[SERVER_NAME_LEN_WITH_NULL * 2]; /* BB make bigger for
224 TCP names - will ipv6 and sctp addresses fit? */ 224 TCP names - will ipv6 and sctp addresses fit? */
225 char userName[MAX_USERNAME_SIZE + 1]; 225 char userName[MAX_USERNAME_SIZE + 1];
226 char *domainName; 226 char *domainName;
227 char *password; 227 char *password;
228 bool need_reconnect:1; /* connection reset, uid now invalid */ 228 bool need_reconnect:1; /* connection reset, uid now invalid */
229 }; 229 };
230 /* no more than one of the following three session flags may be set */ 230 /* no more than one of the following three session flags may be set */
231 #define CIFS_SES_NT4 1 231 #define CIFS_SES_NT4 1
232 #define CIFS_SES_OS2 2 232 #define CIFS_SES_OS2 2
233 #define CIFS_SES_W9X 4 233 #define CIFS_SES_W9X 4
234 /* following flag is set for old servers such as OS2 (and Win95?) 234 /* following flag is set for old servers such as OS2 (and Win95?)
235 which do not negotiate NTLM or POSIX dialects, but instead 235 which do not negotiate NTLM or POSIX dialects, but instead
236 negotiate one of the older LANMAN dialects */ 236 negotiate one of the older LANMAN dialects */
237 #define CIFS_SES_LANMAN 8 237 #define CIFS_SES_LANMAN 8
238 /* 238 /*
239 * there is one of these for each connection to a resource on a particular 239 * there is one of these for each connection to a resource on a particular
240 * session 240 * session
241 */ 241 */
242 struct cifsTconInfo { 242 struct cifsTconInfo {
243 struct list_head tcon_list; 243 struct list_head tcon_list;
244 int tc_count; 244 int tc_count;
245 struct list_head openFileList; 245 struct list_head openFileList;
246 struct cifsSesInfo *ses; /* pointer to session associated with */ 246 struct cifsSesInfo *ses; /* pointer to session associated with */
247 char treeName[MAX_TREE_SIZE + 1]; /* UNC name of resource in ASCII */ 247 char treeName[MAX_TREE_SIZE + 1]; /* UNC name of resource in ASCII */
248 char *nativeFileSystem; 248 char *nativeFileSystem;
249 char *password; /* for share-level security */ 249 char *password; /* for share-level security */
250 __u16 tid; /* The 2 byte tree id */ 250 __u16 tid; /* The 2 byte tree id */
251 __u16 Flags; /* optional support bits */ 251 __u16 Flags; /* optional support bits */
252 enum statusEnum tidStatus; 252 enum statusEnum tidStatus;
253 #ifdef CONFIG_CIFS_STATS 253 #ifdef CONFIG_CIFS_STATS
254 atomic_t num_smbs_sent; 254 atomic_t num_smbs_sent;
255 atomic_t num_writes; 255 atomic_t num_writes;
256 atomic_t num_reads; 256 atomic_t num_reads;
257 atomic_t num_flushes; 257 atomic_t num_flushes;
258 atomic_t num_oplock_brks; 258 atomic_t num_oplock_brks;
259 atomic_t num_opens; 259 atomic_t num_opens;
260 atomic_t num_closes; 260 atomic_t num_closes;
261 atomic_t num_deletes; 261 atomic_t num_deletes;
262 atomic_t num_mkdirs; 262 atomic_t num_mkdirs;
263 atomic_t num_rmdirs; 263 atomic_t num_rmdirs;
264 atomic_t num_renames; 264 atomic_t num_renames;
265 atomic_t num_t2renames; 265 atomic_t num_t2renames;
266 atomic_t num_ffirst; 266 atomic_t num_ffirst;
267 atomic_t num_fnext; 267 atomic_t num_fnext;
268 atomic_t num_fclose; 268 atomic_t num_fclose;
269 atomic_t num_hardlinks; 269 atomic_t num_hardlinks;
270 atomic_t num_symlinks; 270 atomic_t num_symlinks;
271 atomic_t num_locks; 271 atomic_t num_locks;
272 atomic_t num_acl_get; 272 atomic_t num_acl_get;
273 atomic_t num_acl_set; 273 atomic_t num_acl_set;
274 #ifdef CONFIG_CIFS_STATS2 274 #ifdef CONFIG_CIFS_STATS2
275 unsigned long long time_writes; 275 unsigned long long time_writes;
276 unsigned long long time_reads; 276 unsigned long long time_reads;
277 unsigned long long time_opens; 277 unsigned long long time_opens;
278 unsigned long long time_deletes; 278 unsigned long long time_deletes;
279 unsigned long long time_closes; 279 unsigned long long time_closes;
280 unsigned long long time_mkdirs; 280 unsigned long long time_mkdirs;
281 unsigned long long time_rmdirs; 281 unsigned long long time_rmdirs;
282 unsigned long long time_renames; 282 unsigned long long time_renames;
283 unsigned long long time_t2renames; 283 unsigned long long time_t2renames;
284 unsigned long long time_ffirst; 284 unsigned long long time_ffirst;
285 unsigned long long time_fnext; 285 unsigned long long time_fnext;
286 unsigned long long time_fclose; 286 unsigned long long time_fclose;
287 #endif /* CONFIG_CIFS_STATS2 */ 287 #endif /* CONFIG_CIFS_STATS2 */
288 __u64 bytes_read; 288 __u64 bytes_read;
289 __u64 bytes_written; 289 __u64 bytes_written;
290 spinlock_t stat_lock; 290 spinlock_t stat_lock;
291 #endif /* CONFIG_CIFS_STATS */ 291 #endif /* CONFIG_CIFS_STATS */
292 FILE_SYSTEM_DEVICE_INFO fsDevInfo; 292 FILE_SYSTEM_DEVICE_INFO fsDevInfo;
293 FILE_SYSTEM_ATTRIBUTE_INFO fsAttrInfo; /* ok if fs name truncated */ 293 FILE_SYSTEM_ATTRIBUTE_INFO fsAttrInfo; /* ok if fs name truncated */
294 FILE_SYSTEM_UNIX_INFO fsUnixInfo; 294 FILE_SYSTEM_UNIX_INFO fsUnixInfo;
295 bool ipc:1; /* set if connection to IPC$ eg for RPC/PIPES */ 295 bool ipc:1; /* set if connection to IPC$ eg for RPC/PIPES */
296 bool retry:1; 296 bool retry:1;
297 bool nocase:1; 297 bool nocase:1;
298 bool seal:1; /* transport encryption for this mounted share */ 298 bool seal:1; /* transport encryption for this mounted share */
299 bool unix_ext:1; /* if false disable Linux extensions to CIFS protocol 299 bool unix_ext:1; /* if false disable Linux extensions to CIFS protocol
300 for this mount even if server would support */ 300 for this mount even if server would support */
301 bool local_lease:1; /* check leases (only) on local system not remote */ 301 bool local_lease:1; /* check leases (only) on local system not remote */
302 bool broken_posix_open; /* e.g. Samba server versions < 3.3.2, 3.2.9 */ 302 bool broken_posix_open; /* e.g. Samba server versions < 3.3.2, 3.2.9 */
303 bool need_reconnect:1; /* connection reset, tid now invalid */ 303 bool need_reconnect:1; /* connection reset, tid now invalid */
304 /* BB add field for back pointer to sb struct(s)? */ 304 /* BB add field for back pointer to sb struct(s)? */
305 }; 305 };
306 306
307 /* 307 /*
308 * This info hangs off the cifsFileInfo structure, pointed to by llist. 308 * This info hangs off the cifsFileInfo structure, pointed to by llist.
309 * This is used to track byte stream locks on the file 309 * This is used to track byte stream locks on the file
310 */ 310 */
311 struct cifsLockInfo { 311 struct cifsLockInfo {
312 struct list_head llist; /* pointer to next cifsLockInfo */ 312 struct list_head llist; /* pointer to next cifsLockInfo */
313 __u64 offset; 313 __u64 offset;
314 __u64 length; 314 __u64 length;
315 __u8 type; 315 __u8 type;
316 }; 316 };
317 317
318 /* 318 /*
319 * One of these for each open instance of a file 319 * One of these for each open instance of a file
320 */ 320 */
321 struct cifs_search_info { 321 struct cifs_search_info {
322 loff_t index_of_last_entry; 322 loff_t index_of_last_entry;
323 __u16 entries_in_buffer; 323 __u16 entries_in_buffer;
324 __u16 info_level; 324 __u16 info_level;
325 __u32 resume_key; 325 __u32 resume_key;
326 char *ntwrk_buf_start; 326 char *ntwrk_buf_start;
327 char *srch_entries_start; 327 char *srch_entries_start;
328 char *last_entry; 328 char *last_entry;
329 char *presume_name; 329 char *presume_name;
330 unsigned int resume_name_len; 330 unsigned int resume_name_len;
331 bool endOfSearch:1; 331 bool endOfSearch:1;
332 bool emptyDir:1; 332 bool emptyDir:1;
333 bool unicode:1; 333 bool unicode:1;
334 bool smallBuf:1; /* so we know which buf_release function to call */ 334 bool smallBuf:1; /* so we know which buf_release function to call */
335 }; 335 };
336 336
337 struct cifsFileInfo { 337 struct cifsFileInfo {
338 struct list_head tlist; /* pointer to next fid owned by tcon */ 338 struct list_head tlist; /* pointer to next fid owned by tcon */
339 struct list_head flist; /* next fid (file instance) for this inode */ 339 struct list_head flist; /* next fid (file instance) for this inode */
340 unsigned int uid; /* allows finding which FileInfo structure */ 340 unsigned int uid; /* allows finding which FileInfo structure */
341 __u32 pid; /* process id who opened file */ 341 __u32 pid; /* process id who opened file */
342 __u16 netfid; /* file id from remote */ 342 __u16 netfid; /* file id from remote */
343 /* BB add lock scope info here if needed */ ; 343 /* BB add lock scope info here if needed */ ;
344 /* lock scope id (0 if none) */ 344 /* lock scope id (0 if none) */
345 struct file *pfile; /* needed for writepage */ 345 struct file *pfile; /* needed for writepage */
346 struct inode *pInode; /* needed for oplock break */ 346 struct inode *pInode; /* needed for oplock break */
347 struct mutex lock_mutex; 347 struct mutex lock_mutex;
348 struct list_head llist; /* list of byte range locks we have. */ 348 struct list_head llist; /* list of byte range locks we have. */
349 bool closePend:1; /* file is marked to close */ 349 bool closePend:1; /* file is marked to close */
350 bool invalidHandle:1; /* file closed via session abend */ 350 bool invalidHandle:1; /* file closed via session abend */
351 bool messageMode:1; /* for pipes: message vs byte mode */ 351 bool messageMode:1; /* for pipes: message vs byte mode */
352 atomic_t wrtPending; /* handle in use - defer close */ 352 atomic_t wrtPending; /* handle in use - defer close */
353 struct semaphore fh_sem; /* prevents reopen race after dead ses*/ 353 struct mutex fh_mutex; /* prevents reopen race after dead ses*/
354 struct cifs_search_info srch_inf; 354 struct cifs_search_info srch_inf;
355 }; 355 };
356 356
357 /* 357 /*
358 * One of these for each file inode 358 * One of these for each file inode
359 */ 359 */
360 360
361 struct cifsInodeInfo { 361 struct cifsInodeInfo {
362 struct list_head lockList; 362 struct list_head lockList;
363 /* BB add in lists for dirty pages i.e. write caching info for oplock */ 363 /* BB add in lists for dirty pages i.e. write caching info for oplock */
364 struct list_head openFileList; 364 struct list_head openFileList;
365 int write_behind_rc; 365 int write_behind_rc;
366 __u32 cifsAttrs; /* e.g. DOS archive bit, sparse, compressed, system */ 366 __u32 cifsAttrs; /* e.g. DOS archive bit, sparse, compressed, system */
367 atomic_t inUse; /* num concurrent users (local openers cifs) of file*/ 367 atomic_t inUse; /* num concurrent users (local openers cifs) of file*/
368 unsigned long time; /* jiffies of last update/check of inode */ 368 unsigned long time; /* jiffies of last update/check of inode */
369 bool clientCanCacheRead:1; /* read oplock */ 369 bool clientCanCacheRead:1; /* read oplock */
370 bool clientCanCacheAll:1; /* read and writebehind oplock */ 370 bool clientCanCacheAll:1; /* read and writebehind oplock */
371 bool oplockPending:1; 371 bool oplockPending:1;
372 bool delete_pending:1; /* DELETE_ON_CLOSE is set */ 372 bool delete_pending:1; /* DELETE_ON_CLOSE is set */
373 u64 server_eof; /* current file size on server */ 373 u64 server_eof; /* current file size on server */
374 struct inode vfs_inode; 374 struct inode vfs_inode;
375 }; 375 };
376 376
377 static inline struct cifsInodeInfo * 377 static inline struct cifsInodeInfo *
378 CIFS_I(struct inode *inode) 378 CIFS_I(struct inode *inode)
379 { 379 {
380 return container_of(inode, struct cifsInodeInfo, vfs_inode); 380 return container_of(inode, struct cifsInodeInfo, vfs_inode);
381 } 381 }
382 382
383 static inline struct cifs_sb_info * 383 static inline struct cifs_sb_info *
384 CIFS_SB(struct super_block *sb) 384 CIFS_SB(struct super_block *sb)
385 { 385 {
386 return sb->s_fs_info; 386 return sb->s_fs_info;
387 } 387 }
388 388
389 static inline char CIFS_DIR_SEP(const struct cifs_sb_info *cifs_sb) 389 static inline char CIFS_DIR_SEP(const struct cifs_sb_info *cifs_sb)
390 { 390 {
391 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) 391 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
392 return '/'; 392 return '/';
393 else 393 else
394 return '\\'; 394 return '\\';
395 } 395 }
396 396
397 #ifdef CONFIG_CIFS_STATS 397 #ifdef CONFIG_CIFS_STATS
398 #define cifs_stats_inc atomic_inc 398 #define cifs_stats_inc atomic_inc
399 399
400 static inline void cifs_stats_bytes_written(struct cifsTconInfo *tcon, 400 static inline void cifs_stats_bytes_written(struct cifsTconInfo *tcon,
401 unsigned int bytes) 401 unsigned int bytes)
402 { 402 {
403 if (bytes) { 403 if (bytes) {
404 spin_lock(&tcon->stat_lock); 404 spin_lock(&tcon->stat_lock);
405 tcon->bytes_written += bytes; 405 tcon->bytes_written += bytes;
406 spin_unlock(&tcon->stat_lock); 406 spin_unlock(&tcon->stat_lock);
407 } 407 }
408 } 408 }
409 409
410 static inline void cifs_stats_bytes_read(struct cifsTconInfo *tcon, 410 static inline void cifs_stats_bytes_read(struct cifsTconInfo *tcon,
411 unsigned int bytes) 411 unsigned int bytes)
412 { 412 {
413 spin_lock(&tcon->stat_lock); 413 spin_lock(&tcon->stat_lock);
414 tcon->bytes_read += bytes; 414 tcon->bytes_read += bytes;
415 spin_unlock(&tcon->stat_lock); 415 spin_unlock(&tcon->stat_lock);
416 } 416 }
417 #else 417 #else
418 418
419 #define cifs_stats_inc(field) do {} while (0) 419 #define cifs_stats_inc(field) do {} while (0)
420 #define cifs_stats_bytes_written(tcon, bytes) do {} while (0) 420 #define cifs_stats_bytes_written(tcon, bytes) do {} while (0)
421 #define cifs_stats_bytes_read(tcon, bytes) do {} while (0) 421 #define cifs_stats_bytes_read(tcon, bytes) do {} while (0)
422 422
423 #endif 423 #endif
424 424
425 /* one of these for every pending CIFS request to the server */ 425 /* one of these for every pending CIFS request to the server */
426 struct mid_q_entry { 426 struct mid_q_entry {
427 struct list_head qhead; /* mids waiting on reply from this server */ 427 struct list_head qhead; /* mids waiting on reply from this server */
428 __u16 mid; /* multiplex id */ 428 __u16 mid; /* multiplex id */
429 __u16 pid; /* process id */ 429 __u16 pid; /* process id */
430 __u32 sequence_number; /* for CIFS signing */ 430 __u32 sequence_number; /* for CIFS signing */
431 unsigned long when_alloc; /* when mid was created */ 431 unsigned long when_alloc; /* when mid was created */
432 #ifdef CONFIG_CIFS_STATS2 432 #ifdef CONFIG_CIFS_STATS2
433 unsigned long when_sent; /* time when smb send finished */ 433 unsigned long when_sent; /* time when smb send finished */
434 unsigned long when_received; /* when demux complete (taken off wire) */ 434 unsigned long when_received; /* when demux complete (taken off wire) */
435 #endif 435 #endif
436 struct task_struct *tsk; /* task waiting for response */ 436 struct task_struct *tsk; /* task waiting for response */
437 struct smb_hdr *resp_buf; /* response buffer */ 437 struct smb_hdr *resp_buf; /* response buffer */
438 int midState; /* wish this were enum but can not pass to wait_event */ 438 int midState; /* wish this were enum but can not pass to wait_event */
439 __u8 command; /* smb command code */ 439 __u8 command; /* smb command code */
440 bool largeBuf:1; /* if valid response, is pointer to large buf */ 440 bool largeBuf:1; /* if valid response, is pointer to large buf */
441 bool multiRsp:1; /* multiple trans2 responses for one request */ 441 bool multiRsp:1; /* multiple trans2 responses for one request */
442 bool multiEnd:1; /* both received */ 442 bool multiEnd:1; /* both received */
443 }; 443 };
444 444
445 struct oplock_q_entry { 445 struct oplock_q_entry {
446 struct list_head qhead; 446 struct list_head qhead;
447 struct inode *pinode; 447 struct inode *pinode;
448 struct cifsTconInfo *tcon; 448 struct cifsTconInfo *tcon;
449 __u16 netfid; 449 __u16 netfid;
450 }; 450 };
451 451
452 /* for pending dnotify requests */ 452 /* for pending dnotify requests */
453 struct dir_notify_req { 453 struct dir_notify_req {
454 struct list_head lhead; 454 struct list_head lhead;
455 __le16 Pid; 455 __le16 Pid;
456 __le16 PidHigh; 456 __le16 PidHigh;
457 __u16 Mid; 457 __u16 Mid;
458 __u16 Tid; 458 __u16 Tid;
459 __u16 Uid; 459 __u16 Uid;
460 __u16 netfid; 460 __u16 netfid;
461 __u32 filter; /* CompletionFilter (for multishot) */ 461 __u32 filter; /* CompletionFilter (for multishot) */
462 int multishot; 462 int multishot;
463 struct file *pfile; 463 struct file *pfile;
464 }; 464 };
465 465
466 struct dfs_info3_param { 466 struct dfs_info3_param {
467 int flags; /* DFSREF_REFERRAL_SERVER, DFSREF_STORAGE_SERVER*/ 467 int flags; /* DFSREF_REFERRAL_SERVER, DFSREF_STORAGE_SERVER*/
468 int path_consumed; 468 int path_consumed;
469 int server_type; 469 int server_type;
470 int ref_flag; 470 int ref_flag;
471 char *path_name; 471 char *path_name;
472 char *node_name; 472 char *node_name;
473 }; 473 };
474 474
475 static inline void free_dfs_info_param(struct dfs_info3_param *param) 475 static inline void free_dfs_info_param(struct dfs_info3_param *param)
476 { 476 {
477 if (param) { 477 if (param) {
478 kfree(param->path_name); 478 kfree(param->path_name);
479 kfree(param->node_name); 479 kfree(param->node_name);
480 kfree(param); 480 kfree(param);
481 } 481 }
482 } 482 }
483 483
484 static inline void free_dfs_info_array(struct dfs_info3_param *param, 484 static inline void free_dfs_info_array(struct dfs_info3_param *param,
485 int number_of_items) 485 int number_of_items)
486 { 486 {
487 int i; 487 int i;
488 if ((number_of_items == 0) || (param == NULL)) 488 if ((number_of_items == 0) || (param == NULL))
489 return; 489 return;
490 for (i = 0; i < number_of_items; i++) { 490 for (i = 0; i < number_of_items; i++) {
491 kfree(param[i].path_name); 491 kfree(param[i].path_name);
492 kfree(param[i].node_name); 492 kfree(param[i].node_name);
493 } 493 }
494 kfree(param); 494 kfree(param);
495 } 495 }
496 496
497 #define MID_FREE 0 497 #define MID_FREE 0
498 #define MID_REQUEST_ALLOCATED 1 498 #define MID_REQUEST_ALLOCATED 1
499 #define MID_REQUEST_SUBMITTED 2 499 #define MID_REQUEST_SUBMITTED 2
500 #define MID_RESPONSE_RECEIVED 4 500 #define MID_RESPONSE_RECEIVED 4
501 #define MID_RETRY_NEEDED 8 /* session closed while this request out */ 501 #define MID_RETRY_NEEDED 8 /* session closed while this request out */
502 #define MID_NO_RESP_NEEDED 0x10 502 #define MID_NO_RESP_NEEDED 0x10
503 503
504 /* Types of response buffer returned from SendReceive2 */ 504 /* Types of response buffer returned from SendReceive2 */
505 #define CIFS_NO_BUFFER 0 /* Response buffer not returned */ 505 #define CIFS_NO_BUFFER 0 /* Response buffer not returned */
506 #define CIFS_SMALL_BUFFER 1 506 #define CIFS_SMALL_BUFFER 1
507 #define CIFS_LARGE_BUFFER 2 507 #define CIFS_LARGE_BUFFER 2
508 #define CIFS_IOVEC 4 /* array of response buffers */ 508 #define CIFS_IOVEC 4 /* array of response buffers */
509 509
510 /* Type of Request to SendReceive2 */ 510 /* Type of Request to SendReceive2 */
511 #define CIFS_STD_OP 0 /* normal request timeout */ 511 #define CIFS_STD_OP 0 /* normal request timeout */
512 #define CIFS_LONG_OP 1 /* long op (up to 45 sec, oplock time) */ 512 #define CIFS_LONG_OP 1 /* long op (up to 45 sec, oplock time) */
513 #define CIFS_VLONG_OP 2 /* sloow op - can take up to 180 seconds */ 513 #define CIFS_VLONG_OP 2 /* sloow op - can take up to 180 seconds */
514 #define CIFS_BLOCKING_OP 4 /* operation can block */ 514 #define CIFS_BLOCKING_OP 4 /* operation can block */
515 #define CIFS_ASYNC_OP 8 /* do not wait for response */ 515 #define CIFS_ASYNC_OP 8 /* do not wait for response */
516 #define CIFS_TIMEOUT_MASK 0x00F /* only one of 5 above set in req */ 516 #define CIFS_TIMEOUT_MASK 0x00F /* only one of 5 above set in req */
517 #define CIFS_LOG_ERROR 0x010 /* log NT STATUS if non-zero */ 517 #define CIFS_LOG_ERROR 0x010 /* log NT STATUS if non-zero */
518 #define CIFS_LARGE_BUF_OP 0x020 /* large request buffer */ 518 #define CIFS_LARGE_BUF_OP 0x020 /* large request buffer */
519 #define CIFS_NO_RESP 0x040 /* no response buffer required */ 519 #define CIFS_NO_RESP 0x040 /* no response buffer required */
520 520
521 /* Security Flags: indicate type of session setup needed */ 521 /* Security Flags: indicate type of session setup needed */
522 #define CIFSSEC_MAY_SIGN 0x00001 522 #define CIFSSEC_MAY_SIGN 0x00001
523 #define CIFSSEC_MAY_NTLM 0x00002 523 #define CIFSSEC_MAY_NTLM 0x00002
524 #define CIFSSEC_MAY_NTLMV2 0x00004 524 #define CIFSSEC_MAY_NTLMV2 0x00004
525 #define CIFSSEC_MAY_KRB5 0x00008 525 #define CIFSSEC_MAY_KRB5 0x00008
526 #ifdef CONFIG_CIFS_WEAK_PW_HASH 526 #ifdef CONFIG_CIFS_WEAK_PW_HASH
527 #define CIFSSEC_MAY_LANMAN 0x00010 527 #define CIFSSEC_MAY_LANMAN 0x00010
528 #define CIFSSEC_MAY_PLNTXT 0x00020 528 #define CIFSSEC_MAY_PLNTXT 0x00020
529 #else 529 #else
530 #define CIFSSEC_MAY_LANMAN 0 530 #define CIFSSEC_MAY_LANMAN 0
531 #define CIFSSEC_MAY_PLNTXT 0 531 #define CIFSSEC_MAY_PLNTXT 0
532 #endif /* weak passwords */ 532 #endif /* weak passwords */
533 #define CIFSSEC_MAY_SEAL 0x00040 /* not supported yet */ 533 #define CIFSSEC_MAY_SEAL 0x00040 /* not supported yet */
534 534
535 #define CIFSSEC_MUST_SIGN 0x01001 535 #define CIFSSEC_MUST_SIGN 0x01001
536 /* note that only one of the following can be set so the 536 /* note that only one of the following can be set so the
537 result of setting MUST flags more than once will be to 537 result of setting MUST flags more than once will be to
538 require use of the stronger protocol */ 538 require use of the stronger protocol */
539 #define CIFSSEC_MUST_NTLM 0x02002 539 #define CIFSSEC_MUST_NTLM 0x02002
540 #define CIFSSEC_MUST_NTLMV2 0x04004 540 #define CIFSSEC_MUST_NTLMV2 0x04004
541 #define CIFSSEC_MUST_KRB5 0x08008 541 #define CIFSSEC_MUST_KRB5 0x08008
542 #ifdef CONFIG_CIFS_WEAK_PW_HASH 542 #ifdef CONFIG_CIFS_WEAK_PW_HASH
543 #define CIFSSEC_MUST_LANMAN 0x10010 543 #define CIFSSEC_MUST_LANMAN 0x10010
544 #define CIFSSEC_MUST_PLNTXT 0x20020 544 #define CIFSSEC_MUST_PLNTXT 0x20020
545 #ifdef CONFIG_CIFS_UPCALL 545 #ifdef CONFIG_CIFS_UPCALL
546 #define CIFSSEC_MASK 0x3F03F /* allows weak security but also krb5 */ 546 #define CIFSSEC_MASK 0x3F03F /* allows weak security but also krb5 */
547 #else 547 #else
548 #define CIFSSEC_MASK 0x37037 /* current flags supported if weak */ 548 #define CIFSSEC_MASK 0x37037 /* current flags supported if weak */
549 #endif /* UPCALL */ 549 #endif /* UPCALL */
550 #else /* do not allow weak pw hash */ 550 #else /* do not allow weak pw hash */
551 #ifdef CONFIG_CIFS_UPCALL 551 #ifdef CONFIG_CIFS_UPCALL
552 #define CIFSSEC_MASK 0x0F00F /* flags supported if no weak allowed */ 552 #define CIFSSEC_MASK 0x0F00F /* flags supported if no weak allowed */
553 #else 553 #else
554 #define CIFSSEC_MASK 0x07007 /* flags supported if no weak allowed */ 554 #define CIFSSEC_MASK 0x07007 /* flags supported if no weak allowed */
555 #endif /* UPCALL */ 555 #endif /* UPCALL */
556 #endif /* WEAK_PW_HASH */ 556 #endif /* WEAK_PW_HASH */
557 #define CIFSSEC_MUST_SEAL 0x40040 /* not supported yet */ 557 #define CIFSSEC_MUST_SEAL 0x40040 /* not supported yet */
558 558
559 #define CIFSSEC_DEF (CIFSSEC_MAY_SIGN | CIFSSEC_MAY_NTLM | CIFSSEC_MAY_NTLMV2) 559 #define CIFSSEC_DEF (CIFSSEC_MAY_SIGN | CIFSSEC_MAY_NTLM | CIFSSEC_MAY_NTLMV2)
560 #define CIFSSEC_MAX (CIFSSEC_MUST_SIGN | CIFSSEC_MUST_NTLMV2) 560 #define CIFSSEC_MAX (CIFSSEC_MUST_SIGN | CIFSSEC_MUST_NTLMV2)
561 #define CIFSSEC_AUTH_MASK (CIFSSEC_MAY_NTLM | CIFSSEC_MAY_NTLMV2 | CIFSSEC_MAY_LANMAN | CIFSSEC_MAY_PLNTXT | CIFSSEC_MAY_KRB5) 561 #define CIFSSEC_AUTH_MASK (CIFSSEC_MAY_NTLM | CIFSSEC_MAY_NTLMV2 | CIFSSEC_MAY_LANMAN | CIFSSEC_MAY_PLNTXT | CIFSSEC_MAY_KRB5)
562 /* 562 /*
563 ***************************************************************** 563 *****************************************************************
564 * All constants go here 564 * All constants go here
565 ***************************************************************** 565 *****************************************************************
566 */ 566 */
567 567
568 #define UID_HASH (16) 568 #define UID_HASH (16)
569 569
570 /* 570 /*
571 * Note that ONE module should define _DECLARE_GLOBALS_HERE to cause the 571 * Note that ONE module should define _DECLARE_GLOBALS_HERE to cause the
572 * following to be declared. 572 * following to be declared.
573 */ 573 */
574 574
575 /**************************************************************************** 575 /****************************************************************************
576 * Locking notes. All updates to global variables and lists should be 576 * Locking notes. All updates to global variables and lists should be
577 * protected by spinlocks or semaphores. 577 * protected by spinlocks or semaphores.
578 * 578 *
579 * Spinlocks 579 * Spinlocks
580 * --------- 580 * ---------
581 * GlobalMid_Lock protects: 581 * GlobalMid_Lock protects:
582 * list operations on pending_mid_q and oplockQ 582 * list operations on pending_mid_q and oplockQ
583 * updates to XID counters, multiplex id and SMB sequence numbers 583 * updates to XID counters, multiplex id and SMB sequence numbers
584 * GlobalSMBSesLock protects: 584 * GlobalSMBSesLock protects:
585 * list operations on tcp and SMB session lists and tCon lists 585 * list operations on tcp and SMB session lists and tCon lists
586 * f_owner.lock protects certain per file struct operations 586 * f_owner.lock protects certain per file struct operations
587 * mapping->page_lock protects certain per page operations 587 * mapping->page_lock protects certain per page operations
588 * 588 *
589 * Semaphores 589 * Semaphores
590 * ---------- 590 * ----------
591 * sesSem operations on smb session 591 * sesSem operations on smb session
592 * tconSem operations on tree connection 592 * tconSem operations on tree connection
593 * fh_sem file handle reconnection operations 593 * fh_sem file handle reconnection operations
594 * 594 *
595 ****************************************************************************/ 595 ****************************************************************************/
596 596
597 #ifdef DECLARE_GLOBALS_HERE 597 #ifdef DECLARE_GLOBALS_HERE
598 #define GLOBAL_EXTERN 598 #define GLOBAL_EXTERN
599 #else 599 #else
600 #define GLOBAL_EXTERN extern 600 #define GLOBAL_EXTERN extern
601 #endif 601 #endif
602 602
603 /* 603 /*
604 * the list of TCP_Server_Info structures, ie each of the sockets 604 * the list of TCP_Server_Info structures, ie each of the sockets
605 * connecting our client to a distinct server (ip address), is 605 * connecting our client to a distinct server (ip address), is
606 * chained together by cifs_tcp_ses_list. The list of all our SMB 606 * chained together by cifs_tcp_ses_list. The list of all our SMB
607 * sessions (and from that the tree connections) can be found 607 * sessions (and from that the tree connections) can be found
608 * by iterating over cifs_tcp_ses_list 608 * by iterating over cifs_tcp_ses_list
609 */ 609 */
610 GLOBAL_EXTERN struct list_head cifs_tcp_ses_list; 610 GLOBAL_EXTERN struct list_head cifs_tcp_ses_list;
611 611
612 /* 612 /*
613 * This lock protects the cifs_tcp_ses_list, the list of smb sessions per 613 * This lock protects the cifs_tcp_ses_list, the list of smb sessions per
614 * tcp session, and the list of tcon's per smb session. It also protects 614 * tcp session, and the list of tcon's per smb session. It also protects
615 * the reference counters for the server, smb session, and tcon. Finally, 615 * the reference counters for the server, smb session, and tcon. Finally,
616 * changes to the tcon->tidStatus should be done while holding this lock. 616 * changes to the tcon->tidStatus should be done while holding this lock.
617 */ 617 */
618 GLOBAL_EXTERN rwlock_t cifs_tcp_ses_lock; 618 GLOBAL_EXTERN rwlock_t cifs_tcp_ses_lock;
619 619
620 /* 620 /*
621 * This lock protects the cifs_file->llist and cifs_file->flist 621 * This lock protects the cifs_file->llist and cifs_file->flist
622 * list operations, and updates to some flags (cifs_file->invalidHandle) 622 * list operations, and updates to some flags (cifs_file->invalidHandle)
623 * It will be moved to either use the tcon->stat_lock or equivalent later. 623 * It will be moved to either use the tcon->stat_lock or equivalent later.
624 * If cifs_tcp_ses_lock and the lock below are both needed to be held, then 624 * If cifs_tcp_ses_lock and the lock below are both needed to be held, then
625 * the cifs_tcp_ses_lock must be grabbed first and released last. 625 * the cifs_tcp_ses_lock must be grabbed first and released last.
626 */ 626 */
627 GLOBAL_EXTERN rwlock_t GlobalSMBSeslock; 627 GLOBAL_EXTERN rwlock_t GlobalSMBSeslock;
628 628
629 GLOBAL_EXTERN struct list_head GlobalOplock_Q; 629 GLOBAL_EXTERN struct list_head GlobalOplock_Q;
630 630
631 /* Outstanding dir notify requests */ 631 /* Outstanding dir notify requests */
632 GLOBAL_EXTERN struct list_head GlobalDnotifyReqList; 632 GLOBAL_EXTERN struct list_head GlobalDnotifyReqList;
633 /* DirNotify response queue */ 633 /* DirNotify response queue */
634 GLOBAL_EXTERN struct list_head GlobalDnotifyRsp_Q; 634 GLOBAL_EXTERN struct list_head GlobalDnotifyRsp_Q;
635 635
636 /* 636 /*
637 * Global transaction id (XID) information 637 * Global transaction id (XID) information
638 */ 638 */
639 GLOBAL_EXTERN unsigned int GlobalCurrentXid; /* protected by GlobalMid_Sem */ 639 GLOBAL_EXTERN unsigned int GlobalCurrentXid; /* protected by GlobalMid_Sem */
640 GLOBAL_EXTERN unsigned int GlobalTotalActiveXid; /* prot by GlobalMid_Sem */ 640 GLOBAL_EXTERN unsigned int GlobalTotalActiveXid; /* prot by GlobalMid_Sem */
641 GLOBAL_EXTERN unsigned int GlobalMaxActiveXid; /* prot by GlobalMid_Sem */ 641 GLOBAL_EXTERN unsigned int GlobalMaxActiveXid; /* prot by GlobalMid_Sem */
642 GLOBAL_EXTERN spinlock_t GlobalMid_Lock; /* protects above & list operations */ 642 GLOBAL_EXTERN spinlock_t GlobalMid_Lock; /* protects above & list operations */
643 /* on midQ entries */ 643 /* on midQ entries */
644 GLOBAL_EXTERN char Local_System_Name[15]; 644 GLOBAL_EXTERN char Local_System_Name[15];
645 645
646 /* 646 /*
647 * Global counters, updated atomically 647 * Global counters, updated atomically
648 */ 648 */
649 GLOBAL_EXTERN atomic_t sesInfoAllocCount; 649 GLOBAL_EXTERN atomic_t sesInfoAllocCount;
650 GLOBAL_EXTERN atomic_t tconInfoAllocCount; 650 GLOBAL_EXTERN atomic_t tconInfoAllocCount;
651 GLOBAL_EXTERN atomic_t tcpSesAllocCount; 651 GLOBAL_EXTERN atomic_t tcpSesAllocCount;
652 GLOBAL_EXTERN atomic_t tcpSesReconnectCount; 652 GLOBAL_EXTERN atomic_t tcpSesReconnectCount;
653 GLOBAL_EXTERN atomic_t tconInfoReconnectCount; 653 GLOBAL_EXTERN atomic_t tconInfoReconnectCount;
654 654
655 /* Various Debug counters */ 655 /* Various Debug counters */
656 GLOBAL_EXTERN atomic_t bufAllocCount; /* current number allocated */ 656 GLOBAL_EXTERN atomic_t bufAllocCount; /* current number allocated */
657 #ifdef CONFIG_CIFS_STATS2 657 #ifdef CONFIG_CIFS_STATS2
658 GLOBAL_EXTERN atomic_t totBufAllocCount; /* total allocated over all time */ 658 GLOBAL_EXTERN atomic_t totBufAllocCount; /* total allocated over all time */
659 GLOBAL_EXTERN atomic_t totSmBufAllocCount; 659 GLOBAL_EXTERN atomic_t totSmBufAllocCount;
660 #endif 660 #endif
661 GLOBAL_EXTERN atomic_t smBufAllocCount; 661 GLOBAL_EXTERN atomic_t smBufAllocCount;
662 GLOBAL_EXTERN atomic_t midCount; 662 GLOBAL_EXTERN atomic_t midCount;
663 663
664 /* Misc globals */ 664 /* Misc globals */
665 GLOBAL_EXTERN unsigned int multiuser_mount; /* if enabled allows new sessions 665 GLOBAL_EXTERN unsigned int multiuser_mount; /* if enabled allows new sessions
666 to be established on existing mount if we 666 to be established on existing mount if we
667 have the uid/password or Kerberos credential 667 have the uid/password or Kerberos credential
668 or equivalent for current user */ 668 or equivalent for current user */
669 GLOBAL_EXTERN unsigned int oplockEnabled; 669 GLOBAL_EXTERN unsigned int oplockEnabled;
670 GLOBAL_EXTERN unsigned int experimEnabled; 670 GLOBAL_EXTERN unsigned int experimEnabled;
671 GLOBAL_EXTERN unsigned int lookupCacheEnabled; 671 GLOBAL_EXTERN unsigned int lookupCacheEnabled;
672 GLOBAL_EXTERN unsigned int extended_security; /* if on, session setup sent 672 GLOBAL_EXTERN unsigned int extended_security; /* if on, session setup sent
673 with more secure ntlmssp2 challenge/resp */ 673 with more secure ntlmssp2 challenge/resp */
674 GLOBAL_EXTERN unsigned int sign_CIFS_PDUs; /* enable smb packet signing */ 674 GLOBAL_EXTERN unsigned int sign_CIFS_PDUs; /* enable smb packet signing */
675 GLOBAL_EXTERN unsigned int linuxExtEnabled;/*enable Linux/Unix CIFS extensions*/ 675 GLOBAL_EXTERN unsigned int linuxExtEnabled;/*enable Linux/Unix CIFS extensions*/
676 GLOBAL_EXTERN unsigned int CIFSMaxBufSize; /* max size not including hdr */ 676 GLOBAL_EXTERN unsigned int CIFSMaxBufSize; /* max size not including hdr */
677 GLOBAL_EXTERN unsigned int cifs_min_rcv; /* min size of big ntwrk buf pool */ 677 GLOBAL_EXTERN unsigned int cifs_min_rcv; /* min size of big ntwrk buf pool */
678 GLOBAL_EXTERN unsigned int cifs_min_small; /* min size of small buf pool */ 678 GLOBAL_EXTERN unsigned int cifs_min_small; /* min size of small buf pool */
679 GLOBAL_EXTERN unsigned int cifs_max_pending; /* MAX requests at once to server*/ 679 GLOBAL_EXTERN unsigned int cifs_max_pending; /* MAX requests at once to server*/
680 680
681 681
1 /* 1 /*
2 * fs/cifs/dir.c 2 * fs/cifs/dir.c
3 * 3 *
4 * vfs operations that deal with dentries 4 * vfs operations that deal with dentries
5 * 5 *
6 * Copyright (C) International Business Machines Corp., 2002,2009 6 * Copyright (C) International Business Machines Corp., 2002,2009
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/stat.h> 24 #include <linux/stat.h>
25 #include <linux/slab.h> 25 #include <linux/slab.h>
26 #include <linux/namei.h> 26 #include <linux/namei.h>
27 #include "cifsfs.h" 27 #include "cifsfs.h"
28 #include "cifspdu.h" 28 #include "cifspdu.h"
29 #include "cifsglob.h" 29 #include "cifsglob.h"
30 #include "cifsproto.h" 30 #include "cifsproto.h"
31 #include "cifs_debug.h" 31 #include "cifs_debug.h"
32 #include "cifs_fs_sb.h" 32 #include "cifs_fs_sb.h"
33 33
34 static void 34 static void
35 renew_parental_timestamps(struct dentry *direntry) 35 renew_parental_timestamps(struct dentry *direntry)
36 { 36 {
37 /* BB check if there is a way to get the kernel to do this or if we 37 /* BB check if there is a way to get the kernel to do this or if we
38 really need this */ 38 really need this */
39 do { 39 do {
40 direntry->d_time = jiffies; 40 direntry->d_time = jiffies;
41 direntry = direntry->d_parent; 41 direntry = direntry->d_parent;
42 } while (!IS_ROOT(direntry)); 42 } while (!IS_ROOT(direntry));
43 } 43 }
44 44
45 /* Note: caller must free return buffer */ 45 /* Note: caller must free return buffer */
46 char * 46 char *
47 build_path_from_dentry(struct dentry *direntry) 47 build_path_from_dentry(struct dentry *direntry)
48 { 48 {
49 struct dentry *temp; 49 struct dentry *temp;
50 int namelen; 50 int namelen;
51 int pplen; 51 int pplen;
52 int dfsplen; 52 int dfsplen;
53 char *full_path; 53 char *full_path;
54 char dirsep; 54 char dirsep;
55 struct cifs_sb_info *cifs_sb; 55 struct cifs_sb_info *cifs_sb;
56 56
57 if (direntry == NULL) 57 if (direntry == NULL)
58 return NULL; /* not much we can do if dentry is freed and 58 return NULL; /* not much we can do if dentry is freed and
59 we need to reopen the file after it was closed implicitly 59 we need to reopen the file after it was closed implicitly
60 when the server crashed */ 60 when the server crashed */
61 61
62 cifs_sb = CIFS_SB(direntry->d_sb); 62 cifs_sb = CIFS_SB(direntry->d_sb);
63 dirsep = CIFS_DIR_SEP(cifs_sb); 63 dirsep = CIFS_DIR_SEP(cifs_sb);
64 pplen = cifs_sb->prepathlen; 64 pplen = cifs_sb->prepathlen;
65 if (cifs_sb->tcon && (cifs_sb->tcon->Flags & SMB_SHARE_IS_IN_DFS)) 65 if (cifs_sb->tcon && (cifs_sb->tcon->Flags & SMB_SHARE_IS_IN_DFS))
66 dfsplen = strnlen(cifs_sb->tcon->treeName, MAX_TREE_SIZE + 1); 66 dfsplen = strnlen(cifs_sb->tcon->treeName, MAX_TREE_SIZE + 1);
67 else 67 else
68 dfsplen = 0; 68 dfsplen = 0;
69 cifs_bp_rename_retry: 69 cifs_bp_rename_retry:
70 namelen = pplen + dfsplen; 70 namelen = pplen + dfsplen;
71 for (temp = direntry; !IS_ROOT(temp);) { 71 for (temp = direntry; !IS_ROOT(temp);) {
72 namelen += (1 + temp->d_name.len); 72 namelen += (1 + temp->d_name.len);
73 temp = temp->d_parent; 73 temp = temp->d_parent;
74 if (temp == NULL) { 74 if (temp == NULL) {
75 cERROR(1, ("corrupt dentry")); 75 cERROR(1, ("corrupt dentry"));
76 return NULL; 76 return NULL;
77 } 77 }
78 } 78 }
79 79
80 full_path = kmalloc(namelen+1, GFP_KERNEL); 80 full_path = kmalloc(namelen+1, GFP_KERNEL);
81 if (full_path == NULL) 81 if (full_path == NULL)
82 return full_path; 82 return full_path;
83 full_path[namelen] = 0; /* trailing null */ 83 full_path[namelen] = 0; /* trailing null */
84 for (temp = direntry; !IS_ROOT(temp);) { 84 for (temp = direntry; !IS_ROOT(temp);) {
85 namelen -= 1 + temp->d_name.len; 85 namelen -= 1 + temp->d_name.len;
86 if (namelen < 0) { 86 if (namelen < 0) {
87 break; 87 break;
88 } else { 88 } else {
89 full_path[namelen] = dirsep; 89 full_path[namelen] = dirsep;
90 strncpy(full_path + namelen + 1, temp->d_name.name, 90 strncpy(full_path + namelen + 1, temp->d_name.name,
91 temp->d_name.len); 91 temp->d_name.len);
92 cFYI(0, ("name: %s", full_path + namelen)); 92 cFYI(0, ("name: %s", full_path + namelen));
93 } 93 }
94 temp = temp->d_parent; 94 temp = temp->d_parent;
95 if (temp == NULL) { 95 if (temp == NULL) {
96 cERROR(1, ("corrupt dentry")); 96 cERROR(1, ("corrupt dentry"));
97 kfree(full_path); 97 kfree(full_path);
98 return NULL; 98 return NULL;
99 } 99 }
100 } 100 }
101 if (namelen != pplen + dfsplen) { 101 if (namelen != pplen + dfsplen) {
102 cERROR(1, 102 cERROR(1,
103 ("did not end path lookup where expected namelen is %d", 103 ("did not end path lookup where expected namelen is %d",
104 namelen)); 104 namelen));
105 /* presumably this is only possible if racing with a rename 105 /* presumably this is only possible if racing with a rename
106 of one of the parent directories (we can not lock the dentries 106 of one of the parent directories (we can not lock the dentries
107 above us to prevent this, but retrying should be harmless) */ 107 above us to prevent this, but retrying should be harmless) */
108 kfree(full_path); 108 kfree(full_path);
109 goto cifs_bp_rename_retry; 109 goto cifs_bp_rename_retry;
110 } 110 }
111 /* DIR_SEP already set for byte 0 / vs \ but not for 111 /* DIR_SEP already set for byte 0 / vs \ but not for
112 subsequent slashes in prepath which currently must 112 subsequent slashes in prepath which currently must
113 be entered the right way - not sure if there is an alternative 113 be entered the right way - not sure if there is an alternative
114 since the '\' is a valid posix character so we can not switch 114 since the '\' is a valid posix character so we can not switch
115 those safely to '/' if any are found in the middle of the prepath */ 115 those safely to '/' if any are found in the middle of the prepath */
116 /* BB test paths to Windows with '/' in the midst of prepath */ 116 /* BB test paths to Windows with '/' in the midst of prepath */
117 117
118 if (dfsplen) { 118 if (dfsplen) {
119 strncpy(full_path, cifs_sb->tcon->treeName, dfsplen); 119 strncpy(full_path, cifs_sb->tcon->treeName, dfsplen);
120 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) { 120 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
121 int i; 121 int i;
122 for (i = 0; i < dfsplen; i++) { 122 for (i = 0; i < dfsplen; i++) {
123 if (full_path[i] == '\\') 123 if (full_path[i] == '\\')
124 full_path[i] = '/'; 124 full_path[i] = '/';
125 } 125 }
126 } 126 }
127 } 127 }
128 strncpy(full_path + dfsplen, CIFS_SB(direntry->d_sb)->prepath, pplen); 128 strncpy(full_path + dfsplen, CIFS_SB(direntry->d_sb)->prepath, pplen);
129 return full_path; 129 return full_path;
130 } 130 }
131 131
132 static void
133 cifs_fill_fileinfo(struct inode *newinode, __u16 fileHandle,
134 struct cifsTconInfo *tcon, bool write_only)
135 {
136 int oplock = 0;
137 struct cifsFileInfo *pCifsFile;
138 struct cifsInodeInfo *pCifsInode;
139
140 pCifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
141
142 if (pCifsFile == NULL)
143 return;
144
145 if (oplockEnabled)
146 oplock = REQ_OPLOCK;
147
148 pCifsFile->netfid = fileHandle;
149 pCifsFile->pid = current->tgid;
150 pCifsFile->pInode = newinode;
151 pCifsFile->invalidHandle = false;
152 pCifsFile->closePend = false;
153 mutex_init(&pCifsFile->fh_mutex);
154 mutex_init(&pCifsFile->lock_mutex);
155 INIT_LIST_HEAD(&pCifsFile->llist);
156 atomic_set(&pCifsFile->wrtPending, 0);
157
158 /* set the following in open now
159 pCifsFile->pfile = file; */
160 write_lock(&GlobalSMBSeslock);
161 list_add(&pCifsFile->tlist, &tcon->openFileList);
162 pCifsInode = CIFS_I(newinode);
163 if (pCifsInode) {
164 /* if readable file instance put first in list*/
165 if (write_only) {
166 list_add_tail(&pCifsFile->flist,
167 &pCifsInode->openFileList);
168 } else {
169 list_add(&pCifsFile->flist,
170 &pCifsInode->openFileList);
171 }
172 if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
173 pCifsInode->clientCanCacheAll = true;
174 pCifsInode->clientCanCacheRead = true;
175 cFYI(1, ("Exclusive Oplock inode %p",
176 newinode));
177 } else if ((oplock & 0xF) == OPLOCK_READ)
178 pCifsInode->clientCanCacheRead = true;
179 }
180 write_unlock(&GlobalSMBSeslock);
181 }
182
132 int cifs_posix_open(char *full_path, struct inode **pinode, 183 int cifs_posix_open(char *full_path, struct inode **pinode,
133 struct super_block *sb, int mode, int oflags, 184 struct super_block *sb, int mode, int oflags,
134 int *poplock, __u16 *pnetfid, int xid) 185 int *poplock, __u16 *pnetfid, int xid)
135 { 186 {
136 int rc; 187 int rc;
137 __u32 oplock; 188 __u32 oplock;
189 bool write_only = false;
138 FILE_UNIX_BASIC_INFO *presp_data; 190 FILE_UNIX_BASIC_INFO *presp_data;
139 __u32 posix_flags = 0; 191 __u32 posix_flags = 0;
140 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 192 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
141 193
142 cFYI(1, ("posix open %s", full_path)); 194 cFYI(1, ("posix open %s", full_path));
143 195
144 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); 196 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
145 if (presp_data == NULL) 197 if (presp_data == NULL)
146 return -ENOMEM; 198 return -ENOMEM;
147 199
148 /* So far cifs posix extensions can only map the following flags. 200 /* So far cifs posix extensions can only map the following flags.
149 There are other valid fmode oflags such as FMODE_LSEEK, FMODE_PREAD, but 201 There are other valid fmode oflags such as FMODE_LSEEK, FMODE_PREAD, but
150 so far we do not seem to need them, and we can treat them as local only */ 202 so far we do not seem to need them, and we can treat them as local only */
151 if ((oflags & (FMODE_READ | FMODE_WRITE)) == 203 if ((oflags & (FMODE_READ | FMODE_WRITE)) ==
152 (FMODE_READ | FMODE_WRITE)) 204 (FMODE_READ | FMODE_WRITE))
153 posix_flags = SMB_O_RDWR; 205 posix_flags = SMB_O_RDWR;
154 else if (oflags & FMODE_READ) 206 else if (oflags & FMODE_READ)
155 posix_flags = SMB_O_RDONLY; 207 posix_flags = SMB_O_RDONLY;
156 else if (oflags & FMODE_WRITE) 208 else if (oflags & FMODE_WRITE)
157 posix_flags = SMB_O_WRONLY; 209 posix_flags = SMB_O_WRONLY;
158 if (oflags & O_CREAT) 210 if (oflags & O_CREAT)
159 posix_flags |= SMB_O_CREAT; 211 posix_flags |= SMB_O_CREAT;
160 if (oflags & O_EXCL) 212 if (oflags & O_EXCL)
161 posix_flags |= SMB_O_EXCL; 213 posix_flags |= SMB_O_EXCL;
162 if (oflags & O_TRUNC) 214 if (oflags & O_TRUNC)
163 posix_flags |= SMB_O_TRUNC; 215 posix_flags |= SMB_O_TRUNC;
164 if (oflags & O_APPEND) 216 if (oflags & O_APPEND)
165 posix_flags |= SMB_O_APPEND; 217 posix_flags |= SMB_O_APPEND;
166 if (oflags & O_SYNC) 218 if (oflags & O_SYNC)
167 posix_flags |= SMB_O_SYNC; 219 posix_flags |= SMB_O_SYNC;
168 if (oflags & O_DIRECTORY) 220 if (oflags & O_DIRECTORY)
169 posix_flags |= SMB_O_DIRECTORY; 221 posix_flags |= SMB_O_DIRECTORY;
170 if (oflags & O_NOFOLLOW) 222 if (oflags & O_NOFOLLOW)
171 posix_flags |= SMB_O_NOFOLLOW; 223 posix_flags |= SMB_O_NOFOLLOW;
172 if (oflags & O_DIRECT) 224 if (oflags & O_DIRECT)
173 posix_flags |= SMB_O_DIRECT; 225 posix_flags |= SMB_O_DIRECT;
174 226
227 if (!(oflags & FMODE_READ))
228 write_only = true;
175 229
176 rc = CIFSPOSIXCreate(xid, cifs_sb->tcon, posix_flags, mode, 230 rc = CIFSPOSIXCreate(xid, cifs_sb->tcon, posix_flags, mode,
177 pnetfid, presp_data, &oplock, full_path, 231 pnetfid, presp_data, &oplock, full_path,
178 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & 232 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
179 CIFS_MOUNT_MAP_SPECIAL_CHR); 233 CIFS_MOUNT_MAP_SPECIAL_CHR);
180 if (rc) 234 if (rc)
181 goto posix_open_ret; 235 goto posix_open_ret;
182 236
183 if (presp_data->Type == cpu_to_le32(-1)) 237 if (presp_data->Type == cpu_to_le32(-1))
184 goto posix_open_ret; /* open ok, caller does qpathinfo */ 238 goto posix_open_ret; /* open ok, caller does qpathinfo */
185 239
186 /* get new inode and set it up */ 240 /* get new inode and set it up */
187 if (!pinode) 241 if (!pinode)
188 goto posix_open_ret; /* caller does not need info */ 242 goto posix_open_ret; /* caller does not need info */
189 243
190 if (*pinode == NULL) { 244 if (*pinode == NULL) {
191 __u64 unique_id = le64_to_cpu(presp_data->UniqueId); 245 __u64 unique_id = le64_to_cpu(presp_data->UniqueId);
192 *pinode = cifs_new_inode(sb, &unique_id); 246 *pinode = cifs_new_inode(sb, &unique_id);
193 } 247 }
194 /* else an inode was passed in. Update its info, don't create one */ 248 /* else an inode was passed in. Update its info, don't create one */
195 249
196 /* We do not need to close the file if new_inode fails since 250 /* We do not need to close the file if new_inode fails since
197 the caller will retry qpathinfo as long as inode is null */ 251 the caller will retry qpathinfo as long as inode is null */
198 if (*pinode == NULL) 252 if (*pinode == NULL)
199 goto posix_open_ret; 253 goto posix_open_ret;
200 254
201 posix_fill_in_inode(*pinode, presp_data, 1); 255 posix_fill_in_inode(*pinode, presp_data, 1);
202 256
257 cifs_fill_fileinfo(*pinode, *pnetfid, cifs_sb->tcon, write_only);
258
203 posix_open_ret: 259 posix_open_ret:
204 kfree(presp_data); 260 kfree(presp_data);
205 return rc; 261 return rc;
206 } 262 }
207 263
208 static void setup_cifs_dentry(struct cifsTconInfo *tcon, 264 static void setup_cifs_dentry(struct cifsTconInfo *tcon,
209 struct dentry *direntry, 265 struct dentry *direntry,
210 struct inode *newinode) 266 struct inode *newinode)
211 { 267 {
212 if (tcon->nocase) 268 if (tcon->nocase)
213 direntry->d_op = &cifs_ci_dentry_ops; 269 direntry->d_op = &cifs_ci_dentry_ops;
214 else 270 else
215 direntry->d_op = &cifs_dentry_ops; 271 direntry->d_op = &cifs_dentry_ops;
216 d_instantiate(direntry, newinode); 272 d_instantiate(direntry, newinode);
217 } 273 }
218 274
219 /* Inode operations in similar order to how they appear in Linux file fs.h */ 275 /* Inode operations in similar order to how they appear in Linux file fs.h */
220 276
221 int 277 int
222 cifs_create(struct inode *inode, struct dentry *direntry, int mode, 278 cifs_create(struct inode *inode, struct dentry *direntry, int mode,
223 struct nameidata *nd) 279 struct nameidata *nd)
224 { 280 {
225 int rc = -ENOENT; 281 int rc = -ENOENT;
226 int xid; 282 int xid;
227 int create_options = CREATE_NOT_DIR; 283 int create_options = CREATE_NOT_DIR;
228 int oplock = 0; 284 int oplock = 0;
229 int oflags; 285 int oflags;
230 /* 286 /*
231 * BB below access is probably too much for mknod to request 287 * BB below access is probably too much for mknod to request
232 * but we have to do query and setpathinfo so requesting 288 * but we have to do query and setpathinfo so requesting
233 * less could fail (unless we want to request getatr and setatr 289 * less could fail (unless we want to request getatr and setatr
234 * permissions (only). At least for POSIX we do not have to 290 * permissions (only). At least for POSIX we do not have to
235 * request so much. 291 * request so much.
236 */ 292 */
237 int desiredAccess = GENERIC_READ | GENERIC_WRITE; 293 int desiredAccess = GENERIC_READ | GENERIC_WRITE;
238 __u16 fileHandle; 294 __u16 fileHandle;
239 struct cifs_sb_info *cifs_sb; 295 struct cifs_sb_info *cifs_sb;
240 struct cifsTconInfo *tcon; 296 struct cifsTconInfo *tcon;
241 char *full_path = NULL; 297 char *full_path = NULL;
242 FILE_ALL_INFO *buf = NULL; 298 FILE_ALL_INFO *buf = NULL;
243 struct inode *newinode = NULL; 299 struct inode *newinode = NULL;
244 struct cifsInodeInfo *pCifsInode;
245 int disposition = FILE_OVERWRITE_IF; 300 int disposition = FILE_OVERWRITE_IF;
246 bool write_only = false; 301 bool write_only = false;
247 302
248 xid = GetXid(); 303 xid = GetXid();
249 304
250 cifs_sb = CIFS_SB(inode->i_sb); 305 cifs_sb = CIFS_SB(inode->i_sb);
251 tcon = cifs_sb->tcon; 306 tcon = cifs_sb->tcon;
252 307
253 full_path = build_path_from_dentry(direntry); 308 full_path = build_path_from_dentry(direntry);
254 if (full_path == NULL) { 309 if (full_path == NULL) {
255 FreeXid(xid); 310 FreeXid(xid);
256 return -ENOMEM; 311 return -ENOMEM;
257 } 312 }
258 313
259 mode &= ~current_umask(); 314 mode &= ~current_umask();
260 if (oplockEnabled) 315 if (oplockEnabled)
261 oplock = REQ_OPLOCK; 316 oplock = REQ_OPLOCK;
262 317
263 if (nd && (nd->flags & LOOKUP_OPEN)) 318 if (nd && (nd->flags & LOOKUP_OPEN))
264 oflags = nd->intent.open.flags; 319 oflags = nd->intent.open.flags;
265 else 320 else
266 oflags = FMODE_READ; 321 oflags = FMODE_READ;
267 322
268 if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) && 323 if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) &&
269 (CIFS_UNIX_POSIX_PATH_OPS_CAP & 324 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
270 le64_to_cpu(tcon->fsUnixInfo.Capability))) { 325 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
271 rc = cifs_posix_open(full_path, &newinode, inode->i_sb, 326 rc = cifs_posix_open(full_path, &newinode, inode->i_sb,
272 mode, oflags, &oplock, &fileHandle, xid); 327 mode, oflags, &oplock, &fileHandle, xid);
273 /* EIO could indicate that (posix open) operation is not 328 /* EIO could indicate that (posix open) operation is not
274 supported, despite what server claimed in capability 329 supported, despite what server claimed in capability
275 negotation. EREMOTE indicates DFS junction, which is not 330 negotation. EREMOTE indicates DFS junction, which is not
276 handled in posix open */ 331 handled in posix open */
277 332
278 if ((rc == 0) && (newinode == NULL)) 333 if ((rc == 0) && (newinode == NULL))
279 goto cifs_create_get_file_info; /* query inode info */ 334 goto cifs_create_get_file_info; /* query inode info */
280 else if (rc == 0) /* success, no need to query */ 335 else if (rc == 0) /* success, no need to query */
281 goto cifs_create_set_dentry; 336 goto cifs_create_set_dentry;
282 else if ((rc != -EIO) && (rc != -EREMOTE) && 337 else if ((rc != -EIO) && (rc != -EREMOTE) &&
283 (rc != -EOPNOTSUPP)) /* path not found or net err */ 338 (rc != -EOPNOTSUPP)) /* path not found or net err */
284 goto cifs_create_out; 339 goto cifs_create_out;
285 /* else fallthrough to retry, using older open call, this is 340 /* else fallthrough to retry, using older open call, this is
286 case where server does not support this SMB level, and 341 case where server does not support this SMB level, and
287 falsely claims capability (also get here for DFS case 342 falsely claims capability (also get here for DFS case
288 which should be rare for path not covered on files) */ 343 which should be rare for path not covered on files) */
289 } 344 }
290 345
291 if (nd && (nd->flags & LOOKUP_OPEN)) { 346 if (nd && (nd->flags & LOOKUP_OPEN)) {
292 /* if the file is going to stay open, then we 347 /* if the file is going to stay open, then we
293 need to set the desired access properly */ 348 need to set the desired access properly */
294 desiredAccess = 0; 349 desiredAccess = 0;
295 if (oflags & FMODE_READ) 350 if (oflags & FMODE_READ)
296 desiredAccess |= GENERIC_READ; /* is this too little? */ 351 desiredAccess |= GENERIC_READ; /* is this too little? */
297 if (oflags & FMODE_WRITE) { 352 if (oflags & FMODE_WRITE) {
298 desiredAccess |= GENERIC_WRITE; 353 desiredAccess |= GENERIC_WRITE;
299 if (!(oflags & FMODE_READ)) 354 if (!(oflags & FMODE_READ))
300 write_only = true; 355 write_only = true;
301 } 356 }
302 357
303 if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) 358 if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
304 disposition = FILE_CREATE; 359 disposition = FILE_CREATE;
305 else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) 360 else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
306 disposition = FILE_OVERWRITE_IF; 361 disposition = FILE_OVERWRITE_IF;
307 else if ((oflags & O_CREAT) == O_CREAT) 362 else if ((oflags & O_CREAT) == O_CREAT)
308 disposition = FILE_OPEN_IF; 363 disposition = FILE_OPEN_IF;
309 else 364 else
310 cFYI(1, ("Create flag not set in create function")); 365 cFYI(1, ("Create flag not set in create function"));
311 } 366 }
312 367
313 /* BB add processing to set equivalent of mode - e.g. via CreateX with 368 /* BB add processing to set equivalent of mode - e.g. via CreateX with
314 ACLs */ 369 ACLs */
315 370
316 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); 371 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
317 if (buf == NULL) { 372 if (buf == NULL) {
318 kfree(full_path); 373 kfree(full_path);
319 FreeXid(xid); 374 FreeXid(xid);
320 return -ENOMEM; 375 return -ENOMEM;
321 } 376 }
322 377
323 /* 378 /*
324 * if we're not using unix extensions, see if we need to set 379 * if we're not using unix extensions, see if we need to set
325 * ATTR_READONLY on the create call 380 * ATTR_READONLY on the create call
326 */ 381 */
327 if (!tcon->unix_ext && (mode & S_IWUGO) == 0) 382 if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
328 create_options |= CREATE_OPTION_READONLY; 383 create_options |= CREATE_OPTION_READONLY;
329 384
330 if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS) 385 if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS)
331 rc = CIFSSMBOpen(xid, tcon, full_path, disposition, 386 rc = CIFSSMBOpen(xid, tcon, full_path, disposition,
332 desiredAccess, create_options, 387 desiredAccess, create_options,
333 &fileHandle, &oplock, buf, cifs_sb->local_nls, 388 &fileHandle, &oplock, buf, cifs_sb->local_nls,
334 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); 389 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
335 else 390 else
336 rc = -EIO; /* no NT SMB support fall into legacy open below */ 391 rc = -EIO; /* no NT SMB support fall into legacy open below */
337 392
338 if (rc == -EIO) { 393 if (rc == -EIO) {
339 /* old server, retry the open legacy style */ 394 /* old server, retry the open legacy style */
340 rc = SMBLegacyOpen(xid, tcon, full_path, disposition, 395 rc = SMBLegacyOpen(xid, tcon, full_path, disposition,
341 desiredAccess, create_options, 396 desiredAccess, create_options,
342 &fileHandle, &oplock, buf, cifs_sb->local_nls, 397 &fileHandle, &oplock, buf, cifs_sb->local_nls,
343 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); 398 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
344 } 399 }
345 if (rc) { 400 if (rc) {
346 cFYI(1, ("cifs_create returned 0x%x", rc)); 401 cFYI(1, ("cifs_create returned 0x%x", rc));
347 goto cifs_create_out; 402 goto cifs_create_out;
348 } 403 }
349 404
350 /* If Open reported that we actually created a file 405 /* If Open reported that we actually created a file
351 then we now have to set the mode if possible */ 406 then we now have to set the mode if possible */
352 if ((tcon->unix_ext) && (oplock & CIFS_CREATE_ACTION)) { 407 if ((tcon->unix_ext) && (oplock & CIFS_CREATE_ACTION)) {
353 struct cifs_unix_set_info_args args = { 408 struct cifs_unix_set_info_args args = {
354 .mode = mode, 409 .mode = mode,
355 .ctime = NO_CHANGE_64, 410 .ctime = NO_CHANGE_64,
356 .atime = NO_CHANGE_64, 411 .atime = NO_CHANGE_64,
357 .mtime = NO_CHANGE_64, 412 .mtime = NO_CHANGE_64,
358 .device = 0, 413 .device = 0,
359 }; 414 };
360 415
361 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { 416 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
362 args.uid = (__u64) current_fsuid(); 417 args.uid = (__u64) current_fsuid();
363 if (inode->i_mode & S_ISGID) 418 if (inode->i_mode & S_ISGID)
364 args.gid = (__u64) inode->i_gid; 419 args.gid = (__u64) inode->i_gid;
365 else 420 else
366 args.gid = (__u64) current_fsgid(); 421 args.gid = (__u64) current_fsgid();
367 } else { 422 } else {
368 args.uid = NO_CHANGE_64; 423 args.uid = NO_CHANGE_64;
369 args.gid = NO_CHANGE_64; 424 args.gid = NO_CHANGE_64;
370 } 425 }
371 CIFSSMBUnixSetInfo(xid, tcon, full_path, &args, 426 CIFSSMBUnixSetInfo(xid, tcon, full_path, &args,
372 cifs_sb->local_nls, 427 cifs_sb->local_nls,
373 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); 428 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
374 } else { 429 } else {
375 /* BB implement mode setting via Windows security 430 /* BB implement mode setting via Windows security
376 descriptors e.g. */ 431 descriptors e.g. */
377 /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/ 432 /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
378 433
379 /* Could set r/o dos attribute if mode & 0222 == 0 */ 434 /* Could set r/o dos attribute if mode & 0222 == 0 */
380 } 435 }
381 436
382 cifs_create_get_file_info: 437 cifs_create_get_file_info:
383 /* server might mask mode so we have to query for it */ 438 /* server might mask mode so we have to query for it */
384 if (tcon->unix_ext) 439 if (tcon->unix_ext)
385 rc = cifs_get_inode_info_unix(&newinode, full_path, 440 rc = cifs_get_inode_info_unix(&newinode, full_path,
386 inode->i_sb, xid); 441 inode->i_sb, xid);
387 else { 442 else {
388 rc = cifs_get_inode_info(&newinode, full_path, buf, 443 rc = cifs_get_inode_info(&newinode, full_path, buf,
389 inode->i_sb, xid, &fileHandle); 444 inode->i_sb, xid, &fileHandle);
390 if (newinode) { 445 if (newinode) {
391 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) 446 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
392 newinode->i_mode = mode; 447 newinode->i_mode = mode;
393 if ((oplock & CIFS_CREATE_ACTION) && 448 if ((oplock & CIFS_CREATE_ACTION) &&
394 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) { 449 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) {
395 newinode->i_uid = current_fsuid(); 450 newinode->i_uid = current_fsuid();
396 if (inode->i_mode & S_ISGID) 451 if (inode->i_mode & S_ISGID)
397 newinode->i_gid = inode->i_gid; 452 newinode->i_gid = inode->i_gid;
398 else 453 else
399 newinode->i_gid = current_fsgid(); 454 newinode->i_gid = current_fsgid();
400 } 455 }
401 } 456 }
402 } 457 }
403 458
404 cifs_create_set_dentry: 459 cifs_create_set_dentry:
405 if (rc == 0) 460 if (rc == 0)
406 setup_cifs_dentry(tcon, direntry, newinode); 461 setup_cifs_dentry(tcon, direntry, newinode);
407 else 462 else
408 cFYI(1, ("Create worked, get_inode_info failed rc = %d", rc)); 463 cFYI(1, ("Create worked, get_inode_info failed rc = %d", rc));
409 464
410 /* nfsd case - nfs srv does not set nd */ 465 /* nfsd case - nfs srv does not set nd */
411 if ((nd == NULL) || (!(nd->flags & LOOKUP_OPEN))) { 466 if ((nd == NULL) || (!(nd->flags & LOOKUP_OPEN))) {
412 /* mknod case - do not leave file open */ 467 /* mknod case - do not leave file open */
413 CIFSSMBClose(xid, tcon, fileHandle); 468 CIFSSMBClose(xid, tcon, fileHandle);
414 } else if (newinode) { 469 } else if (newinode) {
415 struct cifsFileInfo *pCifsFile = 470 cifs_fill_fileinfo(newinode, fileHandle,
416 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); 471 cifs_sb->tcon, write_only);
417
418 if (pCifsFile == NULL)
419 goto cifs_create_out;
420 pCifsFile->netfid = fileHandle;
421 pCifsFile->pid = current->tgid;
422 pCifsFile->pInode = newinode;
423 pCifsFile->invalidHandle = false;
424 pCifsFile->closePend = false;
425 init_MUTEX(&pCifsFile->fh_sem);
426 mutex_init(&pCifsFile->lock_mutex);
427 INIT_LIST_HEAD(&pCifsFile->llist);
428 atomic_set(&pCifsFile->wrtPending, 0);
429
430 /* set the following in open now
431 pCifsFile->pfile = file; */
432 write_lock(&GlobalSMBSeslock);
433 list_add(&pCifsFile->tlist, &tcon->openFileList);
434 pCifsInode = CIFS_I(newinode);
435 if (pCifsInode) {
436 /* if readable file instance put first in list*/
437 if (write_only) {
438 list_add_tail(&pCifsFile->flist,
439 &pCifsInode->openFileList);
440 } else {
441 list_add(&pCifsFile->flist,
442 &pCifsInode->openFileList);
443 }
444 if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
445 pCifsInode->clientCanCacheAll = true;
446 pCifsInode->clientCanCacheRead = true;
447 cFYI(1, ("Exclusive Oplock inode %p",
448 newinode));
449 } else if ((oplock & 0xF) == OPLOCK_READ)
450 pCifsInode->clientCanCacheRead = true;
451 }
452 write_unlock(&GlobalSMBSeslock);
453 } 472 }
454 cifs_create_out: 473 cifs_create_out:
455 kfree(buf); 474 kfree(buf);
456 kfree(full_path); 475 kfree(full_path);
457 FreeXid(xid); 476 FreeXid(xid);
458 return rc; 477 return rc;
459 } 478 }
460 479
461 int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode, 480 int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode,
462 dev_t device_number) 481 dev_t device_number)
463 { 482 {
464 int rc = -EPERM; 483 int rc = -EPERM;
465 int xid; 484 int xid;
466 struct cifs_sb_info *cifs_sb; 485 struct cifs_sb_info *cifs_sb;
467 struct cifsTconInfo *pTcon; 486 struct cifsTconInfo *pTcon;
468 char *full_path = NULL; 487 char *full_path = NULL;
469 struct inode *newinode = NULL; 488 struct inode *newinode = NULL;
470 489
471 if (!old_valid_dev(device_number)) 490 if (!old_valid_dev(device_number))
472 return -EINVAL; 491 return -EINVAL;
473 492
474 xid = GetXid(); 493 xid = GetXid();
475 494
476 cifs_sb = CIFS_SB(inode->i_sb); 495 cifs_sb = CIFS_SB(inode->i_sb);
477 pTcon = cifs_sb->tcon; 496 pTcon = cifs_sb->tcon;
478 497
479 full_path = build_path_from_dentry(direntry); 498 full_path = build_path_from_dentry(direntry);
480 if (full_path == NULL) 499 if (full_path == NULL)
481 rc = -ENOMEM; 500 rc = -ENOMEM;
482 else if (pTcon->unix_ext) { 501 else if (pTcon->unix_ext) {
483 struct cifs_unix_set_info_args args = { 502 struct cifs_unix_set_info_args args = {
484 .mode = mode & ~current_umask(), 503 .mode = mode & ~current_umask(),
485 .ctime = NO_CHANGE_64, 504 .ctime = NO_CHANGE_64,
486 .atime = NO_CHANGE_64, 505 .atime = NO_CHANGE_64,
487 .mtime = NO_CHANGE_64, 506 .mtime = NO_CHANGE_64,
488 .device = device_number, 507 .device = device_number,
489 }; 508 };
490 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { 509 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
491 args.uid = (__u64) current_fsuid(); 510 args.uid = (__u64) current_fsuid();
492 args.gid = (__u64) current_fsgid(); 511 args.gid = (__u64) current_fsgid();
493 } else { 512 } else {
494 args.uid = NO_CHANGE_64; 513 args.uid = NO_CHANGE_64;
495 args.gid = NO_CHANGE_64; 514 args.gid = NO_CHANGE_64;
496 } 515 }
497 rc = CIFSSMBUnixSetInfo(xid, pTcon, full_path, 516 rc = CIFSSMBUnixSetInfo(xid, pTcon, full_path,
498 &args, cifs_sb->local_nls, 517 &args, cifs_sb->local_nls,
499 cifs_sb->mnt_cifs_flags & 518 cifs_sb->mnt_cifs_flags &
500 CIFS_MOUNT_MAP_SPECIAL_CHR); 519 CIFS_MOUNT_MAP_SPECIAL_CHR);
501 520
502 if (!rc) { 521 if (!rc) {
503 rc = cifs_get_inode_info_unix(&newinode, full_path, 522 rc = cifs_get_inode_info_unix(&newinode, full_path,
504 inode->i_sb, xid); 523 inode->i_sb, xid);
505 if (pTcon->nocase) 524 if (pTcon->nocase)
506 direntry->d_op = &cifs_ci_dentry_ops; 525 direntry->d_op = &cifs_ci_dentry_ops;
507 else 526 else
508 direntry->d_op = &cifs_dentry_ops; 527 direntry->d_op = &cifs_dentry_ops;
509 if (rc == 0) 528 if (rc == 0)
510 d_instantiate(direntry, newinode); 529 d_instantiate(direntry, newinode);
511 } 530 }
512 } else { 531 } else {
513 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) { 532 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
514 int oplock = 0; 533 int oplock = 0;
515 u16 fileHandle; 534 u16 fileHandle;
516 FILE_ALL_INFO *buf; 535 FILE_ALL_INFO *buf;
517 536
518 cFYI(1, ("sfu compat create special file")); 537 cFYI(1, ("sfu compat create special file"));
519 538
520 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); 539 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
521 if (buf == NULL) { 540 if (buf == NULL) {
522 kfree(full_path); 541 kfree(full_path);
523 FreeXid(xid); 542 FreeXid(xid);
524 return -ENOMEM; 543 return -ENOMEM;
525 } 544 }
526 545
527 rc = CIFSSMBOpen(xid, pTcon, full_path, 546 rc = CIFSSMBOpen(xid, pTcon, full_path,
528 FILE_CREATE, /* fail if exists */ 547 FILE_CREATE, /* fail if exists */
529 GENERIC_WRITE /* BB would 548 GENERIC_WRITE /* BB would
530 WRITE_OWNER | WRITE_DAC be better? */, 549 WRITE_OWNER | WRITE_DAC be better? */,
531 /* Create a file and set the 550 /* Create a file and set the
532 file attribute to SYSTEM */ 551 file attribute to SYSTEM */
533 CREATE_NOT_DIR | CREATE_OPTION_SPECIAL, 552 CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
534 &fileHandle, &oplock, buf, 553 &fileHandle, &oplock, buf,
535 cifs_sb->local_nls, 554 cifs_sb->local_nls,
536 cifs_sb->mnt_cifs_flags & 555 cifs_sb->mnt_cifs_flags &
537 CIFS_MOUNT_MAP_SPECIAL_CHR); 556 CIFS_MOUNT_MAP_SPECIAL_CHR);
538 557
539 /* BB FIXME - add handling for backlevel servers 558 /* BB FIXME - add handling for backlevel servers
540 which need legacy open and check for all 559 which need legacy open and check for all
541 calls to SMBOpen for fallback to SMBLeagcyOpen */ 560 calls to SMBOpen for fallback to SMBLeagcyOpen */
542 if (!rc) { 561 if (!rc) {
543 /* BB Do not bother to decode buf since no 562 /* BB Do not bother to decode buf since no
544 local inode yet to put timestamps in, 563 local inode yet to put timestamps in,
545 but we can reuse it safely */ 564 but we can reuse it safely */
546 unsigned int bytes_written; 565 unsigned int bytes_written;
547 struct win_dev *pdev; 566 struct win_dev *pdev;
548 pdev = (struct win_dev *)buf; 567 pdev = (struct win_dev *)buf;
549 if (S_ISCHR(mode)) { 568 if (S_ISCHR(mode)) {
550 memcpy(pdev->type, "IntxCHR", 8); 569 memcpy(pdev->type, "IntxCHR", 8);
551 pdev->major = 570 pdev->major =
552 cpu_to_le64(MAJOR(device_number)); 571 cpu_to_le64(MAJOR(device_number));
553 pdev->minor = 572 pdev->minor =
554 cpu_to_le64(MINOR(device_number)); 573 cpu_to_le64(MINOR(device_number));
555 rc = CIFSSMBWrite(xid, pTcon, 574 rc = CIFSSMBWrite(xid, pTcon,
556 fileHandle, 575 fileHandle,
557 sizeof(struct win_dev), 576 sizeof(struct win_dev),
558 0, &bytes_written, (char *)pdev, 577 0, &bytes_written, (char *)pdev,
559 NULL, 0); 578 NULL, 0);
560 } else if (S_ISBLK(mode)) { 579 } else if (S_ISBLK(mode)) {
561 memcpy(pdev->type, "IntxBLK", 8); 580 memcpy(pdev->type, "IntxBLK", 8);
562 pdev->major = 581 pdev->major =
563 cpu_to_le64(MAJOR(device_number)); 582 cpu_to_le64(MAJOR(device_number));
564 pdev->minor = 583 pdev->minor =
565 cpu_to_le64(MINOR(device_number)); 584 cpu_to_le64(MINOR(device_number));
566 rc = CIFSSMBWrite(xid, pTcon, 585 rc = CIFSSMBWrite(xid, pTcon,
567 fileHandle, 586 fileHandle,
568 sizeof(struct win_dev), 587 sizeof(struct win_dev),
569 0, &bytes_written, (char *)pdev, 588 0, &bytes_written, (char *)pdev,
570 NULL, 0); 589 NULL, 0);
571 } /* else if(S_ISFIFO */ 590 } /* else if(S_ISFIFO */
572 CIFSSMBClose(xid, pTcon, fileHandle); 591 CIFSSMBClose(xid, pTcon, fileHandle);
573 d_drop(direntry); 592 d_drop(direntry);
574 } 593 }
575 kfree(buf); 594 kfree(buf);
576 /* add code here to set EAs */ 595 /* add code here to set EAs */
577 } 596 }
578 } 597 }
579 598
580 kfree(full_path); 599 kfree(full_path);
581 FreeXid(xid); 600 FreeXid(xid);
582 return rc; 601 return rc;
583 } 602 }
584 603
585
586 struct dentry * 604 struct dentry *
587 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, 605 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
588 struct nameidata *nd) 606 struct nameidata *nd)
589 { 607 {
590 int xid; 608 int xid;
591 int rc = 0; /* to get around spurious gcc warning, set to zero here */ 609 int rc = 0; /* to get around spurious gcc warning, set to zero here */
610 int oplock = 0;
611 int mode;
612 __u16 fileHandle = 0;
613 bool posix_open = false;
592 struct cifs_sb_info *cifs_sb; 614 struct cifs_sb_info *cifs_sb;
593 struct cifsTconInfo *pTcon; 615 struct cifsTconInfo *pTcon;
594 struct inode *newInode = NULL; 616 struct inode *newInode = NULL;
595 char *full_path = NULL; 617 char *full_path = NULL;
618 struct file *filp;
596 619
597 xid = GetXid(); 620 xid = GetXid();
598 621
599 cFYI(1, ("parent inode = 0x%p name is: %s and dentry = 0x%p", 622 cFYI(1, ("parent inode = 0x%p name is: %s and dentry = 0x%p",
600 parent_dir_inode, direntry->d_name.name, direntry)); 623 parent_dir_inode, direntry->d_name.name, direntry));
601 624
602 /* check whether path exists */ 625 /* check whether path exists */
603 626
604 cifs_sb = CIFS_SB(parent_dir_inode->i_sb); 627 cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
605 pTcon = cifs_sb->tcon; 628 pTcon = cifs_sb->tcon;
606 629
607 /* 630 /*
608 * Don't allow the separator character in a path component. 631 * Don't allow the separator character in a path component.
609 * The VFS will not allow "/", but "\" is allowed by posix. 632 * The VFS will not allow "/", but "\" is allowed by posix.
610 */ 633 */
611 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) { 634 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
612 int i; 635 int i;
613 for (i = 0; i < direntry->d_name.len; i++) 636 for (i = 0; i < direntry->d_name.len; i++)
614 if (direntry->d_name.name[i] == '\\') { 637 if (direntry->d_name.name[i] == '\\') {
615 cFYI(1, ("Invalid file name")); 638 cFYI(1, ("Invalid file name"));
616 FreeXid(xid); 639 FreeXid(xid);
617 return ERR_PTR(-EINVAL); 640 return ERR_PTR(-EINVAL);
618 } 641 }
619 } 642 }
620 643
621 /* can not grab the rename sem here since it would 644 /* can not grab the rename sem here since it would
622 deadlock in the cases (beginning of sys_rename itself) 645 deadlock in the cases (beginning of sys_rename itself)
623 in which we already have the sb rename sem */ 646 in which we already have the sb rename sem */
624 full_path = build_path_from_dentry(direntry); 647 full_path = build_path_from_dentry(direntry);
625 if (full_path == NULL) { 648 if (full_path == NULL) {
626 FreeXid(xid); 649 FreeXid(xid);
627 return ERR_PTR(-ENOMEM); 650 return ERR_PTR(-ENOMEM);
628 } 651 }
629 652
630 if (direntry->d_inode != NULL) { 653 if (direntry->d_inode != NULL) {
631 cFYI(1, ("non-NULL inode in lookup")); 654 cFYI(1, ("non-NULL inode in lookup"));
632 } else { 655 } else {
633 cFYI(1, ("NULL inode in lookup")); 656 cFYI(1, ("NULL inode in lookup"));
634 } 657 }
635 cFYI(1, ("Full path: %s inode = 0x%p", full_path, direntry->d_inode)); 658 cFYI(1, ("Full path: %s inode = 0x%p", full_path, direntry->d_inode));
636 659
637 if (pTcon->unix_ext) 660 if (pTcon->unix_ext) {
638 rc = cifs_get_inode_info_unix(&newInode, full_path, 661 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY)) &&
639 parent_dir_inode->i_sb, xid); 662 (nd->flags & LOOKUP_OPEN)) {
640 else 663 if (!((nd->intent.open.flags & O_CREAT) &&
664 (nd->intent.open.flags & O_EXCL))) {
665 mode = nd->intent.open.create_mode &
666 ~current->fs->umask;
667 rc = cifs_posix_open(full_path, &newInode,
668 parent_dir_inode->i_sb, mode,
669 nd->intent.open.flags, &oplock,
670 &fileHandle, xid);
671 if ((rc != -EINVAL) && (rc != -EOPNOTSUPP))
672 posix_open = true;
673 }
674 }
675 if (!posix_open)
676 rc = cifs_get_inode_info_unix(&newInode, full_path,
677 parent_dir_inode->i_sb, xid);
678 } else
641 rc = cifs_get_inode_info(&newInode, full_path, NULL, 679 rc = cifs_get_inode_info(&newInode, full_path, NULL,
642 parent_dir_inode->i_sb, xid, NULL); 680 parent_dir_inode->i_sb, xid, NULL);
643 681
644 if ((rc == 0) && (newInode != NULL)) { 682 if ((rc == 0) && (newInode != NULL)) {
645 if (pTcon->nocase) 683 if (pTcon->nocase)
646 direntry->d_op = &cifs_ci_dentry_ops; 684 direntry->d_op = &cifs_ci_dentry_ops;
647 else 685 else
648 direntry->d_op = &cifs_dentry_ops; 686 direntry->d_op = &cifs_dentry_ops;
649 d_add(direntry, newInode); 687 d_add(direntry, newInode);
650 688 if (posix_open)
689 filp = lookup_instantiate_filp(nd, direntry, NULL);
651 /* since paths are not looked up by component - the parent 690 /* since paths are not looked up by component - the parent
652 directories are presumed to be good here */ 691 directories are presumed to be good here */
653 renew_parental_timestamps(direntry); 692 renew_parental_timestamps(direntry);
654 693
655 } else if (rc == -ENOENT) { 694 } else if (rc == -ENOENT) {
656 rc = 0; 695 rc = 0;
657 direntry->d_time = jiffies; 696 direntry->d_time = jiffies;
658 if (pTcon->nocase) 697 if (pTcon->nocase)
659 direntry->d_op = &cifs_ci_dentry_ops; 698 direntry->d_op = &cifs_ci_dentry_ops;
660 else 699 else
661 direntry->d_op = &cifs_dentry_ops; 700 direntry->d_op = &cifs_dentry_ops;
662 d_add(direntry, NULL); 701 d_add(direntry, NULL);
663 /* if it was once a directory (but how can we tell?) we could do 702 /* if it was once a directory (but how can we tell?) we could do
664 shrink_dcache_parent(direntry); */ 703 shrink_dcache_parent(direntry); */
665 } else if (rc != -EACCES) { 704 } else if (rc != -EACCES) {
666 cERROR(1, ("Unexpected lookup error %d", rc)); 705 cERROR(1, ("Unexpected lookup error %d", rc));
667 /* We special case check for Access Denied - since that 706 /* We special case check for Access Denied - since that
668 is a common return code */ 707 is a common return code */
669 } 708 }
670 709
671 kfree(full_path); 710 kfree(full_path);
672 FreeXid(xid); 711 FreeXid(xid);
673 return ERR_PTR(rc); 712 return ERR_PTR(rc);
674 } 713 }
675 714
676 static int 715 static int
677 cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd) 716 cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
678 { 717 {
679 int isValid = 1; 718 int isValid = 1;
680 719
681 if (direntry->d_inode) { 720 if (direntry->d_inode) {
682 if (cifs_revalidate(direntry)) 721 if (cifs_revalidate(direntry))
683 return 0; 722 return 0;
684 } else { 723 } else {
685 cFYI(1, ("neg dentry 0x%p name = %s", 724 cFYI(1, ("neg dentry 0x%p name = %s",
686 direntry, direntry->d_name.name)); 725 direntry, direntry->d_name.name));
687 if (time_after(jiffies, direntry->d_time + HZ) || 726 if (time_after(jiffies, direntry->d_time + HZ) ||
688 !lookupCacheEnabled) { 727 !lookupCacheEnabled) {
689 d_drop(direntry); 728 d_drop(direntry);
690 isValid = 0; 729 isValid = 0;
691 } 730 }
692 } 731 }
693 732
694 return isValid; 733 return isValid;
695 } 734 }
696 735
697 /* static int cifs_d_delete(struct dentry *direntry) 736 /* static int cifs_d_delete(struct dentry *direntry)
698 { 737 {
699 int rc = 0; 738 int rc = 0;
700 739
701 cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name)); 740 cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
702 741
703 return rc; 742 return rc;
704 } */ 743 } */
705 744
706 const struct dentry_operations cifs_dentry_ops = { 745 const struct dentry_operations cifs_dentry_ops = {
707 .d_revalidate = cifs_d_revalidate, 746 .d_revalidate = cifs_d_revalidate,
708 /* d_delete: cifs_d_delete, */ /* not needed except for debugging */ 747 /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
709 }; 748 };
710 749
711 static int cifs_ci_hash(struct dentry *dentry, struct qstr *q) 750 static int cifs_ci_hash(struct dentry *dentry, struct qstr *q)
1 /* 1 /*
2 * fs/cifs/file.c 2 * fs/cifs/file.c
3 * 3 *
4 * vfs operations that deal with files 4 * vfs operations that deal with files
5 * 5 *
6 * Copyright (C) International Business Machines Corp., 2002,2007 6 * Copyright (C) International Business Machines Corp., 2002,2007
7 * Author(s): Steve French (sfrench@us.ibm.com) 7 * Author(s): Steve French (sfrench@us.ibm.com)
8 * Jeremy Allison (jra@samba.org) 8 * Jeremy Allison (jra@samba.org)
9 * 9 *
10 * This library is free software; you can redistribute it and/or modify 10 * This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published 11 * it under the terms of the GNU Lesser General Public License as published
12 * by the Free Software Foundation; either version 2.1 of the License, or 12 * by the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version. 13 * (at your option) any later version.
14 * 14 *
15 * This library is distributed in the hope that it will be useful, 15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU Lesser General Public License for more details. 18 * the GNU Lesser General Public License for more details.
19 * 19 *
20 * You should have received a copy of the GNU Lesser General Public License 20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software 21 * along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */ 23 */
24 #include <linux/fs.h> 24 #include <linux/fs.h>
25 #include <linux/backing-dev.h> 25 #include <linux/backing-dev.h>
26 #include <linux/stat.h> 26 #include <linux/stat.h>
27 #include <linux/fcntl.h> 27 #include <linux/fcntl.h>
28 #include <linux/pagemap.h> 28 #include <linux/pagemap.h>
29 #include <linux/pagevec.h> 29 #include <linux/pagevec.h>
30 #include <linux/writeback.h> 30 #include <linux/writeback.h>
31 #include <linux/task_io_accounting_ops.h> 31 #include <linux/task_io_accounting_ops.h>
32 #include <linux/delay.h> 32 #include <linux/delay.h>
33 #include <asm/div64.h> 33 #include <asm/div64.h>
34 #include "cifsfs.h" 34 #include "cifsfs.h"
35 #include "cifspdu.h" 35 #include "cifspdu.h"
36 #include "cifsglob.h" 36 #include "cifsglob.h"
37 #include "cifsproto.h" 37 #include "cifsproto.h"
38 #include "cifs_unicode.h" 38 #include "cifs_unicode.h"
39 #include "cifs_debug.h" 39 #include "cifs_debug.h"
40 #include "cifs_fs_sb.h" 40 #include "cifs_fs_sb.h"
41 41
42 static inline struct cifsFileInfo *cifs_init_private( 42 static inline struct cifsFileInfo *cifs_init_private(
43 struct cifsFileInfo *private_data, struct inode *inode, 43 struct cifsFileInfo *private_data, struct inode *inode,
44 struct file *file, __u16 netfid) 44 struct file *file, __u16 netfid)
45 { 45 {
46 memset(private_data, 0, sizeof(struct cifsFileInfo)); 46 memset(private_data, 0, sizeof(struct cifsFileInfo));
47 private_data->netfid = netfid; 47 private_data->netfid = netfid;
48 private_data->pid = current->tgid; 48 private_data->pid = current->tgid;
49 init_MUTEX(&private_data->fh_sem); 49 mutex_init(&private_data->fh_mutex);
50 mutex_init(&private_data->lock_mutex); 50 mutex_init(&private_data->lock_mutex);
51 INIT_LIST_HEAD(&private_data->llist); 51 INIT_LIST_HEAD(&private_data->llist);
52 private_data->pfile = file; /* needed for writepage */ 52 private_data->pfile = file; /* needed for writepage */
53 private_data->pInode = inode; 53 private_data->pInode = inode;
54 private_data->invalidHandle = false; 54 private_data->invalidHandle = false;
55 private_data->closePend = false; 55 private_data->closePend = false;
56 /* we have to track num writers to the inode, since writepages 56 /* we have to track num writers to the inode, since writepages
57 does not tell us which handle the write is for so there can 57 does not tell us which handle the write is for so there can
58 be a close (overlapping with write) of the filehandle that 58 be a close (overlapping with write) of the filehandle that
59 cifs_writepages chose to use */ 59 cifs_writepages chose to use */
60 atomic_set(&private_data->wrtPending, 0); 60 atomic_set(&private_data->wrtPending, 0);
61 61
62 return private_data; 62 return private_data;
63 } 63 }
64 64
65 static inline int cifs_convert_flags(unsigned int flags) 65 static inline int cifs_convert_flags(unsigned int flags)
66 { 66 {
67 if ((flags & O_ACCMODE) == O_RDONLY) 67 if ((flags & O_ACCMODE) == O_RDONLY)
68 return GENERIC_READ; 68 return GENERIC_READ;
69 else if ((flags & O_ACCMODE) == O_WRONLY) 69 else if ((flags & O_ACCMODE) == O_WRONLY)
70 return GENERIC_WRITE; 70 return GENERIC_WRITE;
71 else if ((flags & O_ACCMODE) == O_RDWR) { 71 else if ((flags & O_ACCMODE) == O_RDWR) {
72 /* GENERIC_ALL is too much permission to request 72 /* GENERIC_ALL is too much permission to request
73 can cause unnecessary access denied on create */ 73 can cause unnecessary access denied on create */
74 /* return GENERIC_ALL; */ 74 /* return GENERIC_ALL; */
75 return (GENERIC_READ | GENERIC_WRITE); 75 return (GENERIC_READ | GENERIC_WRITE);
76 } 76 }
77 77
78 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | 78 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
79 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA | 79 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
80 FILE_READ_DATA); 80 FILE_READ_DATA);
81 } 81 }
82 82
83 static inline fmode_t cifs_posix_convert_flags(unsigned int flags) 83 static inline fmode_t cifs_posix_convert_flags(unsigned int flags)
84 { 84 {
85 fmode_t posix_flags = 0; 85 fmode_t posix_flags = 0;
86 86
87 if ((flags & O_ACCMODE) == O_RDONLY) 87 if ((flags & O_ACCMODE) == O_RDONLY)
88 posix_flags = FMODE_READ; 88 posix_flags = FMODE_READ;
89 else if ((flags & O_ACCMODE) == O_WRONLY) 89 else if ((flags & O_ACCMODE) == O_WRONLY)
90 posix_flags = FMODE_WRITE; 90 posix_flags = FMODE_WRITE;
91 else if ((flags & O_ACCMODE) == O_RDWR) { 91 else if ((flags & O_ACCMODE) == O_RDWR) {
92 /* GENERIC_ALL is too much permission to request 92 /* GENERIC_ALL is too much permission to request
93 can cause unnecessary access denied on create */ 93 can cause unnecessary access denied on create */
94 /* return GENERIC_ALL; */ 94 /* return GENERIC_ALL; */
95 posix_flags = FMODE_READ | FMODE_WRITE; 95 posix_flags = FMODE_READ | FMODE_WRITE;
96 } 96 }
97 /* can not map O_CREAT or O_EXCL or O_TRUNC flags when 97 /* can not map O_CREAT or O_EXCL or O_TRUNC flags when
98 reopening a file. They had their effect on the original open */ 98 reopening a file. They had their effect on the original open */
99 if (flags & O_APPEND) 99 if (flags & O_APPEND)
100 posix_flags |= (fmode_t)O_APPEND; 100 posix_flags |= (fmode_t)O_APPEND;
101 if (flags & O_SYNC) 101 if (flags & O_SYNC)
102 posix_flags |= (fmode_t)O_SYNC; 102 posix_flags |= (fmode_t)O_SYNC;
103 if (flags & O_DIRECTORY) 103 if (flags & O_DIRECTORY)
104 posix_flags |= (fmode_t)O_DIRECTORY; 104 posix_flags |= (fmode_t)O_DIRECTORY;
105 if (flags & O_NOFOLLOW) 105 if (flags & O_NOFOLLOW)
106 posix_flags |= (fmode_t)O_NOFOLLOW; 106 posix_flags |= (fmode_t)O_NOFOLLOW;
107 if (flags & O_DIRECT) 107 if (flags & O_DIRECT)
108 posix_flags |= (fmode_t)O_DIRECT; 108 posix_flags |= (fmode_t)O_DIRECT;
109 109
110 return posix_flags; 110 return posix_flags;
111 } 111 }
112 112
113 static inline int cifs_get_disposition(unsigned int flags) 113 static inline int cifs_get_disposition(unsigned int flags)
114 { 114 {
115 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) 115 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
116 return FILE_CREATE; 116 return FILE_CREATE;
117 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) 117 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
118 return FILE_OVERWRITE_IF; 118 return FILE_OVERWRITE_IF;
119 else if ((flags & O_CREAT) == O_CREAT) 119 else if ((flags & O_CREAT) == O_CREAT)
120 return FILE_OPEN_IF; 120 return FILE_OPEN_IF;
121 else if ((flags & O_TRUNC) == O_TRUNC) 121 else if ((flags & O_TRUNC) == O_TRUNC)
122 return FILE_OVERWRITE; 122 return FILE_OVERWRITE;
123 else 123 else
124 return FILE_OPEN; 124 return FILE_OPEN;
125 } 125 }
126 126
127 /* all arguments to this function must be checked for validity in caller */ 127 /* all arguments to this function must be checked for validity in caller */
128 static inline int cifs_posix_open_inode_helper(struct inode *inode, 128 static inline int cifs_posix_open_inode_helper(struct inode *inode,
129 struct file *file, struct cifsInodeInfo *pCifsInode, 129 struct file *file, struct cifsInodeInfo *pCifsInode,
130 struct cifsFileInfo *pCifsFile, int oplock, u16 netfid) 130 struct cifsFileInfo *pCifsFile, int oplock, u16 netfid)
131 { 131 {
132 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 132 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
133 /* struct timespec temp; */ /* BB REMOVEME BB */ 133 /* struct timespec temp; */ /* BB REMOVEME BB */
134 134
135 file->private_data = kmalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); 135 file->private_data = kmalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
136 if (file->private_data == NULL) 136 if (file->private_data == NULL)
137 return -ENOMEM; 137 return -ENOMEM;
138 pCifsFile = cifs_init_private(file->private_data, inode, file, netfid); 138 pCifsFile = cifs_init_private(file->private_data, inode, file, netfid);
139 write_lock(&GlobalSMBSeslock); 139 write_lock(&GlobalSMBSeslock);
140 list_add(&pCifsFile->tlist, &cifs_sb->tcon->openFileList); 140 list_add(&pCifsFile->tlist, &cifs_sb->tcon->openFileList);
141 141
142 pCifsInode = CIFS_I(file->f_path.dentry->d_inode); 142 pCifsInode = CIFS_I(file->f_path.dentry->d_inode);
143 if (pCifsInode == NULL) { 143 if (pCifsInode == NULL) {
144 write_unlock(&GlobalSMBSeslock); 144 write_unlock(&GlobalSMBSeslock);
145 return -EINVAL; 145 return -EINVAL;
146 } 146 }
147 147
148 /* want handles we can use to read with first 148 /* want handles we can use to read with first
149 in the list so we do not have to walk the 149 in the list so we do not have to walk the
150 list to search for one in write_begin */ 150 list to search for one in write_begin */
151 if ((file->f_flags & O_ACCMODE) == O_WRONLY) { 151 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
152 list_add_tail(&pCifsFile->flist, 152 list_add_tail(&pCifsFile->flist,
153 &pCifsInode->openFileList); 153 &pCifsInode->openFileList);
154 } else { 154 } else {
155 list_add(&pCifsFile->flist, 155 list_add(&pCifsFile->flist,
156 &pCifsInode->openFileList); 156 &pCifsInode->openFileList);
157 } 157 }
158 158
159 if (pCifsInode->clientCanCacheRead) { 159 if (pCifsInode->clientCanCacheRead) {
160 /* we have the inode open somewhere else 160 /* we have the inode open somewhere else
161 no need to discard cache data */ 161 no need to discard cache data */
162 goto psx_client_can_cache; 162 goto psx_client_can_cache;
163 } 163 }
164 164
165 /* BB FIXME need to fix this check to move it earlier into posix_open 165 /* BB FIXME need to fix this check to move it earlier into posix_open
166 BB fIX following section BB FIXME */ 166 BB fIX following section BB FIXME */
167 167
168 /* if not oplocked, invalidate inode pages if mtime or file 168 /* if not oplocked, invalidate inode pages if mtime or file
169 size changed */ 169 size changed */
170 /* temp = cifs_NTtimeToUnix(le64_to_cpu(buf->LastWriteTime)); 170 /* temp = cifs_NTtimeToUnix(le64_to_cpu(buf->LastWriteTime));
171 if (timespec_equal(&file->f_path.dentry->d_inode->i_mtime, &temp) && 171 if (timespec_equal(&file->f_path.dentry->d_inode->i_mtime, &temp) &&
172 (file->f_path.dentry->d_inode->i_size == 172 (file->f_path.dentry->d_inode->i_size ==
173 (loff_t)le64_to_cpu(buf->EndOfFile))) { 173 (loff_t)le64_to_cpu(buf->EndOfFile))) {
174 cFYI(1, ("inode unchanged on server")); 174 cFYI(1, ("inode unchanged on server"));
175 } else { 175 } else {
176 if (file->f_path.dentry->d_inode->i_mapping) { 176 if (file->f_path.dentry->d_inode->i_mapping) {
177 rc = filemap_write_and_wait(file->f_path.dentry->d_inode->i_mapping); 177 rc = filemap_write_and_wait(file->f_path.dentry->d_inode->i_mapping);
178 if (rc != 0) 178 if (rc != 0)
179 CIFS_I(file->f_path.dentry->d_inode)->write_behind_rc = rc; 179 CIFS_I(file->f_path.dentry->d_inode)->write_behind_rc = rc;
180 } 180 }
181 cFYI(1, ("invalidating remote inode since open detected it " 181 cFYI(1, ("invalidating remote inode since open detected it "
182 "changed")); 182 "changed"));
183 invalidate_remote_inode(file->f_path.dentry->d_inode); 183 invalidate_remote_inode(file->f_path.dentry->d_inode);
184 } */ 184 } */
185 185
186 psx_client_can_cache: 186 psx_client_can_cache:
187 if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) { 187 if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
188 pCifsInode->clientCanCacheAll = true; 188 pCifsInode->clientCanCacheAll = true;
189 pCifsInode->clientCanCacheRead = true; 189 pCifsInode->clientCanCacheRead = true;
190 cFYI(1, ("Exclusive Oplock granted on inode %p", 190 cFYI(1, ("Exclusive Oplock granted on inode %p",
191 file->f_path.dentry->d_inode)); 191 file->f_path.dentry->d_inode));
192 } else if ((oplock & 0xF) == OPLOCK_READ) 192 } else if ((oplock & 0xF) == OPLOCK_READ)
193 pCifsInode->clientCanCacheRead = true; 193 pCifsInode->clientCanCacheRead = true;
194 194
195 /* will have to change the unlock if we reenable the 195 /* will have to change the unlock if we reenable the
196 filemap_fdatawrite (which does not seem necessary */ 196 filemap_fdatawrite (which does not seem necessary */
197 write_unlock(&GlobalSMBSeslock); 197 write_unlock(&GlobalSMBSeslock);
198 return 0; 198 return 0;
199 } 199 }
200 200
201 /* all arguments to this function must be checked for validity in caller */ 201 /* all arguments to this function must be checked for validity in caller */
202 static inline int cifs_open_inode_helper(struct inode *inode, struct file *file, 202 static inline int cifs_open_inode_helper(struct inode *inode, struct file *file,
203 struct cifsInodeInfo *pCifsInode, struct cifsFileInfo *pCifsFile, 203 struct cifsInodeInfo *pCifsInode, struct cifsFileInfo *pCifsFile,
204 struct cifsTconInfo *pTcon, int *oplock, FILE_ALL_INFO *buf, 204 struct cifsTconInfo *pTcon, int *oplock, FILE_ALL_INFO *buf,
205 char *full_path, int xid) 205 char *full_path, int xid)
206 { 206 {
207 struct timespec temp; 207 struct timespec temp;
208 int rc; 208 int rc;
209 209
210 /* want handles we can use to read with first 210 /* want handles we can use to read with first
211 in the list so we do not have to walk the 211 in the list so we do not have to walk the
212 list to search for one in write_begin */ 212 list to search for one in write_begin */
213 if ((file->f_flags & O_ACCMODE) == O_WRONLY) { 213 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
214 list_add_tail(&pCifsFile->flist, 214 list_add_tail(&pCifsFile->flist,
215 &pCifsInode->openFileList); 215 &pCifsInode->openFileList);
216 } else { 216 } else {
217 list_add(&pCifsFile->flist, 217 list_add(&pCifsFile->flist,
218 &pCifsInode->openFileList); 218 &pCifsInode->openFileList);
219 } 219 }
220 write_unlock(&GlobalSMBSeslock); 220 write_unlock(&GlobalSMBSeslock);
221 if (pCifsInode->clientCanCacheRead) { 221 if (pCifsInode->clientCanCacheRead) {
222 /* we have the inode open somewhere else 222 /* we have the inode open somewhere else
223 no need to discard cache data */ 223 no need to discard cache data */
224 goto client_can_cache; 224 goto client_can_cache;
225 } 225 }
226 226
227 /* BB need same check in cifs_create too? */ 227 /* BB need same check in cifs_create too? */
228 /* if not oplocked, invalidate inode pages if mtime or file 228 /* if not oplocked, invalidate inode pages if mtime or file
229 size changed */ 229 size changed */
230 temp = cifs_NTtimeToUnix(le64_to_cpu(buf->LastWriteTime)); 230 temp = cifs_NTtimeToUnix(le64_to_cpu(buf->LastWriteTime));
231 if (timespec_equal(&file->f_path.dentry->d_inode->i_mtime, &temp) && 231 if (timespec_equal(&file->f_path.dentry->d_inode->i_mtime, &temp) &&
232 (file->f_path.dentry->d_inode->i_size == 232 (file->f_path.dentry->d_inode->i_size ==
233 (loff_t)le64_to_cpu(buf->EndOfFile))) { 233 (loff_t)le64_to_cpu(buf->EndOfFile))) {
234 cFYI(1, ("inode unchanged on server")); 234 cFYI(1, ("inode unchanged on server"));
235 } else { 235 } else {
236 if (file->f_path.dentry->d_inode->i_mapping) { 236 if (file->f_path.dentry->d_inode->i_mapping) {
237 /* BB no need to lock inode until after invalidate 237 /* BB no need to lock inode until after invalidate
238 since namei code should already have it locked? */ 238 since namei code should already have it locked? */
239 rc = filemap_write_and_wait(file->f_path.dentry->d_inode->i_mapping); 239 rc = filemap_write_and_wait(file->f_path.dentry->d_inode->i_mapping);
240 if (rc != 0) 240 if (rc != 0)
241 CIFS_I(file->f_path.dentry->d_inode)->write_behind_rc = rc; 241 CIFS_I(file->f_path.dentry->d_inode)->write_behind_rc = rc;
242 } 242 }
243 cFYI(1, ("invalidating remote inode since open detected it " 243 cFYI(1, ("invalidating remote inode since open detected it "
244 "changed")); 244 "changed"));
245 invalidate_remote_inode(file->f_path.dentry->d_inode); 245 invalidate_remote_inode(file->f_path.dentry->d_inode);
246 } 246 }
247 247
248 client_can_cache: 248 client_can_cache:
249 if (pTcon->unix_ext) 249 if (pTcon->unix_ext)
250 rc = cifs_get_inode_info_unix(&file->f_path.dentry->d_inode, 250 rc = cifs_get_inode_info_unix(&file->f_path.dentry->d_inode,
251 full_path, inode->i_sb, xid); 251 full_path, inode->i_sb, xid);
252 else 252 else
253 rc = cifs_get_inode_info(&file->f_path.dentry->d_inode, 253 rc = cifs_get_inode_info(&file->f_path.dentry->d_inode,
254 full_path, buf, inode->i_sb, xid, NULL); 254 full_path, buf, inode->i_sb, xid, NULL);
255 255
256 if ((*oplock & 0xF) == OPLOCK_EXCLUSIVE) { 256 if ((*oplock & 0xF) == OPLOCK_EXCLUSIVE) {
257 pCifsInode->clientCanCacheAll = true; 257 pCifsInode->clientCanCacheAll = true;
258 pCifsInode->clientCanCacheRead = true; 258 pCifsInode->clientCanCacheRead = true;
259 cFYI(1, ("Exclusive Oplock granted on inode %p", 259 cFYI(1, ("Exclusive Oplock granted on inode %p",
260 file->f_path.dentry->d_inode)); 260 file->f_path.dentry->d_inode));
261 } else if ((*oplock & 0xF) == OPLOCK_READ) 261 } else if ((*oplock & 0xF) == OPLOCK_READ)
262 pCifsInode->clientCanCacheRead = true; 262 pCifsInode->clientCanCacheRead = true;
263 263
264 return rc; 264 return rc;
265 } 265 }
266 266
267 int cifs_open(struct inode *inode, struct file *file) 267 int cifs_open(struct inode *inode, struct file *file)
268 { 268 {
269 int rc = -EACCES; 269 int rc = -EACCES;
270 int xid, oplock; 270 int xid, oplock;
271 struct cifs_sb_info *cifs_sb; 271 struct cifs_sb_info *cifs_sb;
272 struct cifsTconInfo *tcon; 272 struct cifsTconInfo *tcon;
273 struct cifsFileInfo *pCifsFile; 273 struct cifsFileInfo *pCifsFile;
274 struct cifsInodeInfo *pCifsInode; 274 struct cifsInodeInfo *pCifsInode;
275 struct list_head *tmp; 275 struct list_head *tmp;
276 char *full_path = NULL; 276 char *full_path = NULL;
277 int desiredAccess; 277 int desiredAccess;
278 int disposition; 278 int disposition;
279 __u16 netfid; 279 __u16 netfid;
280 FILE_ALL_INFO *buf = NULL; 280 FILE_ALL_INFO *buf = NULL;
281 281
282 xid = GetXid(); 282 xid = GetXid();
283 283
284 cifs_sb = CIFS_SB(inode->i_sb); 284 cifs_sb = CIFS_SB(inode->i_sb);
285 tcon = cifs_sb->tcon; 285 tcon = cifs_sb->tcon;
286 286
287 if (file->f_flags & O_CREAT) { 287 /* search inode for this file and fill in file->private_data */
288 /* search inode for this file and fill in file->private_data */ 288 pCifsInode = CIFS_I(file->f_path.dentry->d_inode);
289 pCifsInode = CIFS_I(file->f_path.dentry->d_inode); 289 read_lock(&GlobalSMBSeslock);
290 read_lock(&GlobalSMBSeslock); 290 list_for_each(tmp, &pCifsInode->openFileList) {
291 list_for_each(tmp, &pCifsInode->openFileList) { 291 pCifsFile = list_entry(tmp, struct cifsFileInfo,
292 pCifsFile = list_entry(tmp, struct cifsFileInfo, 292 flist);
293 flist); 293 if ((pCifsFile->pfile == NULL) &&
294 if ((pCifsFile->pfile == NULL) && 294 (pCifsFile->pid == current->tgid)) {
295 (pCifsFile->pid == current->tgid)) { 295 /* mode set in cifs_create */
296 /* mode set in cifs_create */
297 296
298 /* needed for writepage */ 297 /* needed for writepage */
299 pCifsFile->pfile = file; 298 pCifsFile->pfile = file;
300 299
301 file->private_data = pCifsFile; 300 file->private_data = pCifsFile;
302 break; 301 break;
303 }
304 } 302 }
305 read_unlock(&GlobalSMBSeslock);
306 if (file->private_data != NULL) {
307 rc = 0;
308 FreeXid(xid);
309 return rc;
310 } else {
311 if (file->f_flags & O_EXCL)
312 cERROR(1, ("could not find file instance for "
313 "new file %p", file));
314 }
315 } 303 }
304 read_unlock(&GlobalSMBSeslock);
316 305
306 if (file->private_data != NULL) {
307 rc = 0;
308 FreeXid(xid);
309 return rc;
310 } else {
311 if ((file->f_flags & O_CREAT) && (file->f_flags & O_EXCL))
312 cERROR(1, ("could not find file instance for "
313 "new file %p", file));
314 }
315
317 full_path = build_path_from_dentry(file->f_path.dentry); 316 full_path = build_path_from_dentry(file->f_path.dentry);
318 if (full_path == NULL) { 317 if (full_path == NULL) {
319 FreeXid(xid); 318 FreeXid(xid);
320 return -ENOMEM; 319 return -ENOMEM;
321 } 320 }
322 321
323 cFYI(1, ("inode = 0x%p file flags are 0x%x for %s", 322 cFYI(1, ("inode = 0x%p file flags are 0x%x for %s",
324 inode, file->f_flags, full_path)); 323 inode, file->f_flags, full_path));
325 324
326 if (oplockEnabled) 325 if (oplockEnabled)
327 oplock = REQ_OPLOCK; 326 oplock = REQ_OPLOCK;
328 else 327 else
329 oplock = 0; 328 oplock = 0;
330 329
331 if (!tcon->broken_posix_open && tcon->unix_ext && 330 if (!tcon->broken_posix_open && tcon->unix_ext &&
332 (tcon->ses->capabilities & CAP_UNIX) && 331 (tcon->ses->capabilities & CAP_UNIX) &&
333 (CIFS_UNIX_POSIX_PATH_OPS_CAP & 332 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
334 le64_to_cpu(tcon->fsUnixInfo.Capability))) { 333 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
335 int oflags = (int) cifs_posix_convert_flags(file->f_flags); 334 int oflags = (int) cifs_posix_convert_flags(file->f_flags);
336 /* can not refresh inode info since size could be stale */ 335 /* can not refresh inode info since size could be stale */
337 rc = cifs_posix_open(full_path, &inode, inode->i_sb, 336 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
338 cifs_sb->mnt_file_mode /* ignored */, 337 cifs_sb->mnt_file_mode /* ignored */,
339 oflags, &oplock, &netfid, xid); 338 oflags, &oplock, &netfid, xid);
340 if (rc == 0) { 339 if (rc == 0) {
341 cFYI(1, ("posix open succeeded")); 340 cFYI(1, ("posix open succeeded"));
342 /* no need for special case handling of setting mode 341 /* no need for special case handling of setting mode
343 on read only files needed here */ 342 on read only files needed here */
344 343
345 cifs_posix_open_inode_helper(inode, file, pCifsInode, 344 cifs_posix_open_inode_helper(inode, file, pCifsInode,
346 pCifsFile, oplock, netfid); 345 pCifsFile, oplock, netfid);
347 goto out; 346 goto out;
348 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { 347 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
349 if (tcon->ses->serverNOS) 348 if (tcon->ses->serverNOS)
350 cERROR(1, ("server %s of type %s returned" 349 cERROR(1, ("server %s of type %s returned"
351 " unexpected error on SMB posix open" 350 " unexpected error on SMB posix open"
352 ", disabling posix open support." 351 ", disabling posix open support."
353 " Check if server update available.", 352 " Check if server update available.",
354 tcon->ses->serverName, 353 tcon->ses->serverName,
355 tcon->ses->serverNOS)); 354 tcon->ses->serverNOS));
356 tcon->broken_posix_open = true; 355 tcon->broken_posix_open = true;
357 } else if ((rc != -EIO) && (rc != -EREMOTE) && 356 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
358 (rc != -EOPNOTSUPP)) /* path not found or net err */ 357 (rc != -EOPNOTSUPP)) /* path not found or net err */
359 goto out; 358 goto out;
360 /* else fallthrough to retry open the old way on network i/o 359 /* else fallthrough to retry open the old way on network i/o
361 or DFS errors */ 360 or DFS errors */
362 } 361 }
363 362
364 desiredAccess = cifs_convert_flags(file->f_flags); 363 desiredAccess = cifs_convert_flags(file->f_flags);
365 364
366 /********************************************************************* 365 /*********************************************************************
367 * open flag mapping table: 366 * open flag mapping table:
368 * 367 *
369 * POSIX Flag CIFS Disposition 368 * POSIX Flag CIFS Disposition
370 * ---------- ---------------- 369 * ---------- ----------------
371 * O_CREAT FILE_OPEN_IF 370 * O_CREAT FILE_OPEN_IF
372 * O_CREAT | O_EXCL FILE_CREATE 371 * O_CREAT | O_EXCL FILE_CREATE
373 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF 372 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
374 * O_TRUNC FILE_OVERWRITE 373 * O_TRUNC FILE_OVERWRITE
375 * none of the above FILE_OPEN 374 * none of the above FILE_OPEN
376 * 375 *
377 * Note that there is not a direct match between disposition 376 * Note that there is not a direct match between disposition
378 * FILE_SUPERSEDE (ie create whether or not file exists although 377 * FILE_SUPERSEDE (ie create whether or not file exists although
379 * O_CREAT | O_TRUNC is similar but truncates the existing 378 * O_CREAT | O_TRUNC is similar but truncates the existing
380 * file rather than creating a new file as FILE_SUPERSEDE does 379 * file rather than creating a new file as FILE_SUPERSEDE does
381 * (which uses the attributes / metadata passed in on open call) 380 * (which uses the attributes / metadata passed in on open call)
382 *? 381 *?
383 *? O_SYNC is a reasonable match to CIFS writethrough flag 382 *? O_SYNC is a reasonable match to CIFS writethrough flag
384 *? and the read write flags match reasonably. O_LARGEFILE 383 *? and the read write flags match reasonably. O_LARGEFILE
385 *? is irrelevant because largefile support is always used 384 *? is irrelevant because largefile support is always used
386 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY, 385 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
387 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation 386 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
388 *********************************************************************/ 387 *********************************************************************/
389 388
390 disposition = cifs_get_disposition(file->f_flags); 389 disposition = cifs_get_disposition(file->f_flags);
391 390
392 /* BB pass O_SYNC flag through on file attributes .. BB */ 391 /* BB pass O_SYNC flag through on file attributes .. BB */
393 392
394 /* Also refresh inode by passing in file_info buf returned by SMBOpen 393 /* Also refresh inode by passing in file_info buf returned by SMBOpen
395 and calling get_inode_info with returned buf (at least helps 394 and calling get_inode_info with returned buf (at least helps
396 non-Unix server case) */ 395 non-Unix server case) */
397 396
398 /* BB we can not do this if this is the second open of a file 397 /* BB we can not do this if this is the second open of a file
399 and the first handle has writebehind data, we might be 398 and the first handle has writebehind data, we might be
400 able to simply do a filemap_fdatawrite/filemap_fdatawait first */ 399 able to simply do a filemap_fdatawrite/filemap_fdatawait first */
401 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); 400 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
402 if (!buf) { 401 if (!buf) {
403 rc = -ENOMEM; 402 rc = -ENOMEM;
404 goto out; 403 goto out;
405 } 404 }
406 405
407 if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS) 406 if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS)
408 rc = CIFSSMBOpen(xid, tcon, full_path, disposition, 407 rc = CIFSSMBOpen(xid, tcon, full_path, disposition,
409 desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf, 408 desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf,
410 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags 409 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags
411 & CIFS_MOUNT_MAP_SPECIAL_CHR); 410 & CIFS_MOUNT_MAP_SPECIAL_CHR);
412 else 411 else
413 rc = -EIO; /* no NT SMB support fall into legacy open below */ 412 rc = -EIO; /* no NT SMB support fall into legacy open below */
414 413
415 if (rc == -EIO) { 414 if (rc == -EIO) {
416 /* Old server, try legacy style OpenX */ 415 /* Old server, try legacy style OpenX */
417 rc = SMBLegacyOpen(xid, tcon, full_path, disposition, 416 rc = SMBLegacyOpen(xid, tcon, full_path, disposition,
418 desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf, 417 desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf,
419 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags 418 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags
420 & CIFS_MOUNT_MAP_SPECIAL_CHR); 419 & CIFS_MOUNT_MAP_SPECIAL_CHR);
421 } 420 }
422 if (rc) { 421 if (rc) {
423 cFYI(1, ("cifs_open returned 0x%x", rc)); 422 cFYI(1, ("cifs_open returned 0x%x", rc));
424 goto out; 423 goto out;
425 } 424 }
426 file->private_data = 425 file->private_data =
427 kmalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); 426 kmalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
428 if (file->private_data == NULL) { 427 if (file->private_data == NULL) {
429 rc = -ENOMEM; 428 rc = -ENOMEM;
430 goto out; 429 goto out;
431 } 430 }
432 pCifsFile = cifs_init_private(file->private_data, inode, file, netfid); 431 pCifsFile = cifs_init_private(file->private_data, inode, file, netfid);
433 write_lock(&GlobalSMBSeslock); 432 write_lock(&GlobalSMBSeslock);
434 list_add(&pCifsFile->tlist, &tcon->openFileList); 433 list_add(&pCifsFile->tlist, &tcon->openFileList);
435 434
436 pCifsInode = CIFS_I(file->f_path.dentry->d_inode); 435 pCifsInode = CIFS_I(file->f_path.dentry->d_inode);
437 if (pCifsInode) { 436 if (pCifsInode) {
438 rc = cifs_open_inode_helper(inode, file, pCifsInode, 437 rc = cifs_open_inode_helper(inode, file, pCifsInode,
439 pCifsFile, tcon, 438 pCifsFile, tcon,
440 &oplock, buf, full_path, xid); 439 &oplock, buf, full_path, xid);
441 } else { 440 } else {
442 write_unlock(&GlobalSMBSeslock); 441 write_unlock(&GlobalSMBSeslock);
443 } 442 }
444 443
445 if (oplock & CIFS_CREATE_ACTION) { 444 if (oplock & CIFS_CREATE_ACTION) {
446 /* time to set mode which we can not set earlier due to 445 /* time to set mode which we can not set earlier due to
447 problems creating new read-only files */ 446 problems creating new read-only files */
448 if (tcon->unix_ext) { 447 if (tcon->unix_ext) {
449 struct cifs_unix_set_info_args args = { 448 struct cifs_unix_set_info_args args = {
450 .mode = inode->i_mode, 449 .mode = inode->i_mode,
451 .uid = NO_CHANGE_64, 450 .uid = NO_CHANGE_64,
452 .gid = NO_CHANGE_64, 451 .gid = NO_CHANGE_64,
453 .ctime = NO_CHANGE_64, 452 .ctime = NO_CHANGE_64,
454 .atime = NO_CHANGE_64, 453 .atime = NO_CHANGE_64,
455 .mtime = NO_CHANGE_64, 454 .mtime = NO_CHANGE_64,
456 .device = 0, 455 .device = 0,
457 }; 456 };
458 CIFSSMBUnixSetInfo(xid, tcon, full_path, &args, 457 CIFSSMBUnixSetInfo(xid, tcon, full_path, &args,
459 cifs_sb->local_nls, 458 cifs_sb->local_nls,
460 cifs_sb->mnt_cifs_flags & 459 cifs_sb->mnt_cifs_flags &
461 CIFS_MOUNT_MAP_SPECIAL_CHR); 460 CIFS_MOUNT_MAP_SPECIAL_CHR);
462 } 461 }
463 } 462 }
464 463
465 out: 464 out:
466 kfree(buf); 465 kfree(buf);
467 kfree(full_path); 466 kfree(full_path);
468 FreeXid(xid); 467 FreeXid(xid);
469 return rc; 468 return rc;
470 } 469 }
471 470
472 /* Try to reacquire byte range locks that were released when session */ 471 /* Try to reacquire byte range locks that were released when session */
473 /* to server was lost */ 472 /* to server was lost */
474 static int cifs_relock_file(struct cifsFileInfo *cifsFile) 473 static int cifs_relock_file(struct cifsFileInfo *cifsFile)
475 { 474 {
476 int rc = 0; 475 int rc = 0;
477 476
478 /* BB list all locks open on this file and relock */ 477 /* BB list all locks open on this file and relock */
479 478
480 return rc; 479 return rc;
481 } 480 }
482 481
483 static int cifs_reopen_file(struct file *file, bool can_flush) 482 static int cifs_reopen_file(struct file *file, bool can_flush)
484 { 483 {
485 int rc = -EACCES; 484 int rc = -EACCES;
486 int xid, oplock; 485 int xid, oplock;
487 struct cifs_sb_info *cifs_sb; 486 struct cifs_sb_info *cifs_sb;
488 struct cifsTconInfo *tcon; 487 struct cifsTconInfo *tcon;
489 struct cifsFileInfo *pCifsFile; 488 struct cifsFileInfo *pCifsFile;
490 struct cifsInodeInfo *pCifsInode; 489 struct cifsInodeInfo *pCifsInode;
491 struct inode *inode; 490 struct inode *inode;
492 char *full_path = NULL; 491 char *full_path = NULL;
493 int desiredAccess; 492 int desiredAccess;
494 int disposition = FILE_OPEN; 493 int disposition = FILE_OPEN;
495 __u16 netfid; 494 __u16 netfid;
496 495
497 if (file->private_data) 496 if (file->private_data)
498 pCifsFile = (struct cifsFileInfo *)file->private_data; 497 pCifsFile = (struct cifsFileInfo *)file->private_data;
499 else 498 else
500 return -EBADF; 499 return -EBADF;
501 500
502 xid = GetXid(); 501 xid = GetXid();
503 down(&pCifsFile->fh_sem); 502 mutex_unlock(&pCifsFile->fh_mutex);
504 if (!pCifsFile->invalidHandle) { 503 if (!pCifsFile->invalidHandle) {
505 up(&pCifsFile->fh_sem); 504 mutex_lock(&pCifsFile->fh_mutex);
506 FreeXid(xid); 505 FreeXid(xid);
507 return 0; 506 return 0;
508 } 507 }
509 508
510 if (file->f_path.dentry == NULL) { 509 if (file->f_path.dentry == NULL) {
511 cERROR(1, ("no valid name if dentry freed")); 510 cERROR(1, ("no valid name if dentry freed"));
512 dump_stack(); 511 dump_stack();
513 rc = -EBADF; 512 rc = -EBADF;
514 goto reopen_error_exit; 513 goto reopen_error_exit;
515 } 514 }
516 515
517 inode = file->f_path.dentry->d_inode; 516 inode = file->f_path.dentry->d_inode;
518 if (inode == NULL) { 517 if (inode == NULL) {
519 cERROR(1, ("inode not valid")); 518 cERROR(1, ("inode not valid"));
520 dump_stack(); 519 dump_stack();
521 rc = -EBADF; 520 rc = -EBADF;
522 goto reopen_error_exit; 521 goto reopen_error_exit;
523 } 522 }
524 523
525 cifs_sb = CIFS_SB(inode->i_sb); 524 cifs_sb = CIFS_SB(inode->i_sb);
526 tcon = cifs_sb->tcon; 525 tcon = cifs_sb->tcon;
527 526
528 /* can not grab rename sem here because various ops, including 527 /* can not grab rename sem here because various ops, including
529 those that already have the rename sem can end up causing writepage 528 those that already have the rename sem can end up causing writepage
530 to get called and if the server was down that means we end up here, 529 to get called and if the server was down that means we end up here,
531 and we can never tell if the caller already has the rename_sem */ 530 and we can never tell if the caller already has the rename_sem */
532 full_path = build_path_from_dentry(file->f_path.dentry); 531 full_path = build_path_from_dentry(file->f_path.dentry);
533 if (full_path == NULL) { 532 if (full_path == NULL) {
534 rc = -ENOMEM; 533 rc = -ENOMEM;
535 reopen_error_exit: 534 reopen_error_exit:
536 up(&pCifsFile->fh_sem); 535 mutex_lock(&pCifsFile->fh_mutex);
537 FreeXid(xid); 536 FreeXid(xid);
538 return rc; 537 return rc;
539 } 538 }
540 539
541 cFYI(1, ("inode = 0x%p file flags 0x%x for %s", 540 cFYI(1, ("inode = 0x%p file flags 0x%x for %s",
542 inode, file->f_flags, full_path)); 541 inode, file->f_flags, full_path));
543 542
544 if (oplockEnabled) 543 if (oplockEnabled)
545 oplock = REQ_OPLOCK; 544 oplock = REQ_OPLOCK;
546 else 545 else
547 oplock = 0; 546 oplock = 0;
548 547
549 if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) && 548 if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) &&
550 (CIFS_UNIX_POSIX_PATH_OPS_CAP & 549 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
551 le64_to_cpu(tcon->fsUnixInfo.Capability))) { 550 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
552 int oflags = (int) cifs_posix_convert_flags(file->f_flags); 551 int oflags = (int) cifs_posix_convert_flags(file->f_flags);
553 /* can not refresh inode info since size could be stale */ 552 /* can not refresh inode info since size could be stale */
554 rc = cifs_posix_open(full_path, NULL, inode->i_sb, 553 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
555 cifs_sb->mnt_file_mode /* ignored */, 554 cifs_sb->mnt_file_mode /* ignored */,
556 oflags, &oplock, &netfid, xid); 555 oflags, &oplock, &netfid, xid);
557 if (rc == 0) { 556 if (rc == 0) {
558 cFYI(1, ("posix reopen succeeded")); 557 cFYI(1, ("posix reopen succeeded"));
559 goto reopen_success; 558 goto reopen_success;
560 } 559 }
561 /* fallthrough to retry open the old way on errors, especially 560 /* fallthrough to retry open the old way on errors, especially
562 in the reconnect path it is important to retry hard */ 561 in the reconnect path it is important to retry hard */
563 } 562 }
564 563
565 desiredAccess = cifs_convert_flags(file->f_flags); 564 desiredAccess = cifs_convert_flags(file->f_flags);
566 565
567 /* Can not refresh inode by passing in file_info buf to be returned 566 /* Can not refresh inode by passing in file_info buf to be returned
568 by SMBOpen and then calling get_inode_info with returned buf 567 by SMBOpen and then calling get_inode_info with returned buf
569 since file might have write behind data that needs to be flushed 568 since file might have write behind data that needs to be flushed
570 and server version of file size can be stale. If we knew for sure 569 and server version of file size can be stale. If we knew for sure
571 that inode was not dirty locally we could do this */ 570 that inode was not dirty locally we could do this */
572 571
573 rc = CIFSSMBOpen(xid, tcon, full_path, disposition, desiredAccess, 572 rc = CIFSSMBOpen(xid, tcon, full_path, disposition, desiredAccess,
574 CREATE_NOT_DIR, &netfid, &oplock, NULL, 573 CREATE_NOT_DIR, &netfid, &oplock, NULL,
575 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & 574 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
576 CIFS_MOUNT_MAP_SPECIAL_CHR); 575 CIFS_MOUNT_MAP_SPECIAL_CHR);
577 if (rc) { 576 if (rc) {
578 up(&pCifsFile->fh_sem); 577 mutex_lock(&pCifsFile->fh_mutex);
579 cFYI(1, ("cifs_open returned 0x%x", rc)); 578 cFYI(1, ("cifs_open returned 0x%x", rc));
580 cFYI(1, ("oplock: %d", oplock)); 579 cFYI(1, ("oplock: %d", oplock));
581 } else { 580 } else {
582 reopen_success: 581 reopen_success:
583 pCifsFile->netfid = netfid; 582 pCifsFile->netfid = netfid;
584 pCifsFile->invalidHandle = false; 583 pCifsFile->invalidHandle = false;
585 up(&pCifsFile->fh_sem); 584 mutex_lock(&pCifsFile->fh_mutex);
586 pCifsInode = CIFS_I(inode); 585 pCifsInode = CIFS_I(inode);
587 if (pCifsInode) { 586 if (pCifsInode) {
588 if (can_flush) { 587 if (can_flush) {
589 rc = filemap_write_and_wait(inode->i_mapping); 588 rc = filemap_write_and_wait(inode->i_mapping);
590 if (rc != 0) 589 if (rc != 0)
591 CIFS_I(inode)->write_behind_rc = rc; 590 CIFS_I(inode)->write_behind_rc = rc;
592 /* temporarily disable caching while we 591 /* temporarily disable caching while we
593 go to server to get inode info */ 592 go to server to get inode info */
594 pCifsInode->clientCanCacheAll = false; 593 pCifsInode->clientCanCacheAll = false;
595 pCifsInode->clientCanCacheRead = false; 594 pCifsInode->clientCanCacheRead = false;
596 if (tcon->unix_ext) 595 if (tcon->unix_ext)
597 rc = cifs_get_inode_info_unix(&inode, 596 rc = cifs_get_inode_info_unix(&inode,
598 full_path, inode->i_sb, xid); 597 full_path, inode->i_sb, xid);
599 else 598 else
600 rc = cifs_get_inode_info(&inode, 599 rc = cifs_get_inode_info(&inode,
601 full_path, NULL, inode->i_sb, 600 full_path, NULL, inode->i_sb,
602 xid, NULL); 601 xid, NULL);
603 } /* else we are writing out data to server already 602 } /* else we are writing out data to server already
604 and could deadlock if we tried to flush data, and 603 and could deadlock if we tried to flush data, and
605 since we do not know if we have data that would 604 since we do not know if we have data that would
606 invalidate the current end of file on the server 605 invalidate the current end of file on the server
607 we can not go to the server to get the new inod 606 we can not go to the server to get the new inod
608 info */ 607 info */
609 if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) { 608 if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
610 pCifsInode->clientCanCacheAll = true; 609 pCifsInode->clientCanCacheAll = true;
611 pCifsInode->clientCanCacheRead = true; 610 pCifsInode->clientCanCacheRead = true;
612 cFYI(1, ("Exclusive Oplock granted on inode %p", 611 cFYI(1, ("Exclusive Oplock granted on inode %p",
613 file->f_path.dentry->d_inode)); 612 file->f_path.dentry->d_inode));
614 } else if ((oplock & 0xF) == OPLOCK_READ) { 613 } else if ((oplock & 0xF) == OPLOCK_READ) {
615 pCifsInode->clientCanCacheRead = true; 614 pCifsInode->clientCanCacheRead = true;
616 pCifsInode->clientCanCacheAll = false; 615 pCifsInode->clientCanCacheAll = false;
617 } else { 616 } else {
618 pCifsInode->clientCanCacheRead = false; 617 pCifsInode->clientCanCacheRead = false;
619 pCifsInode->clientCanCacheAll = false; 618 pCifsInode->clientCanCacheAll = false;
620 } 619 }
621 cifs_relock_file(pCifsFile); 620 cifs_relock_file(pCifsFile);
622 } 621 }
623 } 622 }
624 kfree(full_path); 623 kfree(full_path);
625 FreeXid(xid); 624 FreeXid(xid);
626 return rc; 625 return rc;
627 } 626 }
628 627
629 int cifs_close(struct inode *inode, struct file *file) 628 int cifs_close(struct inode *inode, struct file *file)
630 { 629 {
631 int rc = 0; 630 int rc = 0;
632 int xid, timeout; 631 int xid, timeout;
633 struct cifs_sb_info *cifs_sb; 632 struct cifs_sb_info *cifs_sb;
634 struct cifsTconInfo *pTcon; 633 struct cifsTconInfo *pTcon;
635 struct cifsFileInfo *pSMBFile = 634 struct cifsFileInfo *pSMBFile =
636 (struct cifsFileInfo *)file->private_data; 635 (struct cifsFileInfo *)file->private_data;
637 636
638 xid = GetXid(); 637 xid = GetXid();
639 638
640 cifs_sb = CIFS_SB(inode->i_sb); 639 cifs_sb = CIFS_SB(inode->i_sb);
641 pTcon = cifs_sb->tcon; 640 pTcon = cifs_sb->tcon;
642 if (pSMBFile) { 641 if (pSMBFile) {
643 struct cifsLockInfo *li, *tmp; 642 struct cifsLockInfo *li, *tmp;
644 write_lock(&GlobalSMBSeslock); 643 write_lock(&GlobalSMBSeslock);
645 pSMBFile->closePend = true; 644 pSMBFile->closePend = true;
646 if (pTcon) { 645 if (pTcon) {
647 /* no sense reconnecting to close a file that is 646 /* no sense reconnecting to close a file that is
648 already closed */ 647 already closed */
649 if (!pTcon->need_reconnect) { 648 if (!pTcon->need_reconnect) {
650 write_unlock(&GlobalSMBSeslock); 649 write_unlock(&GlobalSMBSeslock);
651 timeout = 2; 650 timeout = 2;
652 while ((atomic_read(&pSMBFile->wrtPending) != 0) 651 while ((atomic_read(&pSMBFile->wrtPending) != 0)
653 && (timeout <= 2048)) { 652 && (timeout <= 2048)) {
654 /* Give write a better chance to get to 653 /* Give write a better chance to get to
655 server ahead of the close. We do not 654 server ahead of the close. We do not
656 want to add a wait_q here as it would 655 want to add a wait_q here as it would
657 increase the memory utilization as 656 increase the memory utilization as
658 the struct would be in each open file, 657 the struct would be in each open file,
659 but this should give enough time to 658 but this should give enough time to
660 clear the socket */ 659 clear the socket */
661 cFYI(DBG2, 660 cFYI(DBG2,
662 ("close delay, write pending")); 661 ("close delay, write pending"));
663 msleep(timeout); 662 msleep(timeout);
664 timeout *= 4; 663 timeout *= 4;
665 } 664 }
666 if (atomic_read(&pSMBFile->wrtPending)) 665 if (atomic_read(&pSMBFile->wrtPending))
667 cERROR(1, ("close with pending write")); 666 cERROR(1, ("close with pending write"));
668 if (!pTcon->need_reconnect && 667 if (!pTcon->need_reconnect &&
669 !pSMBFile->invalidHandle) 668 !pSMBFile->invalidHandle)
670 rc = CIFSSMBClose(xid, pTcon, 669 rc = CIFSSMBClose(xid, pTcon,
671 pSMBFile->netfid); 670 pSMBFile->netfid);
672 } else 671 } else
673 write_unlock(&GlobalSMBSeslock); 672 write_unlock(&GlobalSMBSeslock);
674 } else 673 } else
675 write_unlock(&GlobalSMBSeslock); 674 write_unlock(&GlobalSMBSeslock);
676 675
677 /* Delete any outstanding lock records. 676 /* Delete any outstanding lock records.
678 We'll lose them when the file is closed anyway. */ 677 We'll lose them when the file is closed anyway. */
679 mutex_lock(&pSMBFile->lock_mutex); 678 mutex_lock(&pSMBFile->lock_mutex);
680 list_for_each_entry_safe(li, tmp, &pSMBFile->llist, llist) { 679 list_for_each_entry_safe(li, tmp, &pSMBFile->llist, llist) {
681 list_del(&li->llist); 680 list_del(&li->llist);
682 kfree(li); 681 kfree(li);
683 } 682 }
684 mutex_unlock(&pSMBFile->lock_mutex); 683 mutex_unlock(&pSMBFile->lock_mutex);
685 684
686 write_lock(&GlobalSMBSeslock); 685 write_lock(&GlobalSMBSeslock);
687 list_del(&pSMBFile->flist); 686 list_del(&pSMBFile->flist);
688 list_del(&pSMBFile->tlist); 687 list_del(&pSMBFile->tlist);
689 write_unlock(&GlobalSMBSeslock); 688 write_unlock(&GlobalSMBSeslock);
690 timeout = 10; 689 timeout = 10;
691 /* We waited above to give the SMBWrite a chance to issue 690 /* We waited above to give the SMBWrite a chance to issue
692 on the wire (so we do not get SMBWrite returning EBADF 691 on the wire (so we do not get SMBWrite returning EBADF
693 if writepages is racing with close. Note that writepages 692 if writepages is racing with close. Note that writepages
694 does not specify a file handle, so it is possible for a file 693 does not specify a file handle, so it is possible for a file
695 to be opened twice, and the application close the "wrong" 694 to be opened twice, and the application close the "wrong"
696 file handle - in these cases we delay long enough to allow 695 file handle - in these cases we delay long enough to allow
697 the SMBWrite to get on the wire before the SMB Close. 696 the SMBWrite to get on the wire before the SMB Close.
698 We allow total wait here over 45 seconds, more than 697 We allow total wait here over 45 seconds, more than
699 oplock break time, and more than enough to allow any write 698 oplock break time, and more than enough to allow any write
700 to complete on the server, or to time out on the client */ 699 to complete on the server, or to time out on the client */
701 while ((atomic_read(&pSMBFile->wrtPending) != 0) 700 while ((atomic_read(&pSMBFile->wrtPending) != 0)
702 && (timeout <= 50000)) { 701 && (timeout <= 50000)) {
703 cERROR(1, ("writes pending, delay free of handle")); 702 cERROR(1, ("writes pending, delay free of handle"));
704 msleep(timeout); 703 msleep(timeout);
705 timeout *= 8; 704 timeout *= 8;
706 } 705 }
707 kfree(file->private_data); 706 kfree(file->private_data);
708 file->private_data = NULL; 707 file->private_data = NULL;
709 } else 708 } else
710 rc = -EBADF; 709 rc = -EBADF;
711 710
712 read_lock(&GlobalSMBSeslock); 711 read_lock(&GlobalSMBSeslock);
713 if (list_empty(&(CIFS_I(inode)->openFileList))) { 712 if (list_empty(&(CIFS_I(inode)->openFileList))) {
714 cFYI(1, ("closing last open instance for inode %p", inode)); 713 cFYI(1, ("closing last open instance for inode %p", inode));
715 /* if the file is not open we do not know if we can cache info 714 /* if the file is not open we do not know if we can cache info
716 on this inode, much less write behind and read ahead */ 715 on this inode, much less write behind and read ahead */
717 CIFS_I(inode)->clientCanCacheRead = false; 716 CIFS_I(inode)->clientCanCacheRead = false;
718 CIFS_I(inode)->clientCanCacheAll = false; 717 CIFS_I(inode)->clientCanCacheAll = false;
719 } 718 }
720 read_unlock(&GlobalSMBSeslock); 719 read_unlock(&GlobalSMBSeslock);
721 if ((rc == 0) && CIFS_I(inode)->write_behind_rc) 720 if ((rc == 0) && CIFS_I(inode)->write_behind_rc)
722 rc = CIFS_I(inode)->write_behind_rc; 721 rc = CIFS_I(inode)->write_behind_rc;
723 FreeXid(xid); 722 FreeXid(xid);
724 return rc; 723 return rc;
725 } 724 }
726 725
727 int cifs_closedir(struct inode *inode, struct file *file) 726 int cifs_closedir(struct inode *inode, struct file *file)
728 { 727 {
729 int rc = 0; 728 int rc = 0;
730 int xid; 729 int xid;
731 struct cifsFileInfo *pCFileStruct = 730 struct cifsFileInfo *pCFileStruct =
732 (struct cifsFileInfo *)file->private_data; 731 (struct cifsFileInfo *)file->private_data;
733 char *ptmp; 732 char *ptmp;
734 733
735 cFYI(1, ("Closedir inode = 0x%p", inode)); 734 cFYI(1, ("Closedir inode = 0x%p", inode));
736 735
737 xid = GetXid(); 736 xid = GetXid();
738 737
739 if (pCFileStruct) { 738 if (pCFileStruct) {
740 struct cifsTconInfo *pTcon; 739 struct cifsTconInfo *pTcon;
741 struct cifs_sb_info *cifs_sb = 740 struct cifs_sb_info *cifs_sb =
742 CIFS_SB(file->f_path.dentry->d_sb); 741 CIFS_SB(file->f_path.dentry->d_sb);
743 742
744 pTcon = cifs_sb->tcon; 743 pTcon = cifs_sb->tcon;
745 744
746 cFYI(1, ("Freeing private data in close dir")); 745 cFYI(1, ("Freeing private data in close dir"));
747 write_lock(&GlobalSMBSeslock); 746 write_lock(&GlobalSMBSeslock);
748 if (!pCFileStruct->srch_inf.endOfSearch && 747 if (!pCFileStruct->srch_inf.endOfSearch &&
749 !pCFileStruct->invalidHandle) { 748 !pCFileStruct->invalidHandle) {
750 pCFileStruct->invalidHandle = true; 749 pCFileStruct->invalidHandle = true;
751 write_unlock(&GlobalSMBSeslock); 750 write_unlock(&GlobalSMBSeslock);
752 rc = CIFSFindClose(xid, pTcon, pCFileStruct->netfid); 751 rc = CIFSFindClose(xid, pTcon, pCFileStruct->netfid);
753 cFYI(1, ("Closing uncompleted readdir with rc %d", 752 cFYI(1, ("Closing uncompleted readdir with rc %d",
754 rc)); 753 rc));
755 /* not much we can do if it fails anyway, ignore rc */ 754 /* not much we can do if it fails anyway, ignore rc */
756 rc = 0; 755 rc = 0;
757 } else 756 } else
758 write_unlock(&GlobalSMBSeslock); 757 write_unlock(&GlobalSMBSeslock);
759 ptmp = pCFileStruct->srch_inf.ntwrk_buf_start; 758 ptmp = pCFileStruct->srch_inf.ntwrk_buf_start;
760 if (ptmp) { 759 if (ptmp) {
761 cFYI(1, ("closedir free smb buf in srch struct")); 760 cFYI(1, ("closedir free smb buf in srch struct"));
762 pCFileStruct->srch_inf.ntwrk_buf_start = NULL; 761 pCFileStruct->srch_inf.ntwrk_buf_start = NULL;
763 if (pCFileStruct->srch_inf.smallBuf) 762 if (pCFileStruct->srch_inf.smallBuf)
764 cifs_small_buf_release(ptmp); 763 cifs_small_buf_release(ptmp);
765 else 764 else
766 cifs_buf_release(ptmp); 765 cifs_buf_release(ptmp);
767 } 766 }
768 kfree(file->private_data); 767 kfree(file->private_data);
769 file->private_data = NULL; 768 file->private_data = NULL;
770 } 769 }
771 /* BB can we lock the filestruct while this is going on? */ 770 /* BB can we lock the filestruct while this is going on? */
772 FreeXid(xid); 771 FreeXid(xid);
773 return rc; 772 return rc;
774 } 773 }
775 774
776 static int store_file_lock(struct cifsFileInfo *fid, __u64 len, 775 static int store_file_lock(struct cifsFileInfo *fid, __u64 len,
777 __u64 offset, __u8 lockType) 776 __u64 offset, __u8 lockType)
778 { 777 {
779 struct cifsLockInfo *li = 778 struct cifsLockInfo *li =
780 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL); 779 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
781 if (li == NULL) 780 if (li == NULL)
782 return -ENOMEM; 781 return -ENOMEM;
783 li->offset = offset; 782 li->offset = offset;
784 li->length = len; 783 li->length = len;
785 li->type = lockType; 784 li->type = lockType;
786 mutex_lock(&fid->lock_mutex); 785 mutex_lock(&fid->lock_mutex);
787 list_add(&li->llist, &fid->llist); 786 list_add(&li->llist, &fid->llist);
788 mutex_unlock(&fid->lock_mutex); 787 mutex_unlock(&fid->lock_mutex);
789 return 0; 788 return 0;
790 } 789 }
791 790
792 int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock) 791 int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock)
793 { 792 {
794 int rc, xid; 793 int rc, xid;
795 __u32 numLock = 0; 794 __u32 numLock = 0;
796 __u32 numUnlock = 0; 795 __u32 numUnlock = 0;
797 __u64 length; 796 __u64 length;
798 bool wait_flag = false; 797 bool wait_flag = false;
799 struct cifs_sb_info *cifs_sb; 798 struct cifs_sb_info *cifs_sb;
800 struct cifsTconInfo *tcon; 799 struct cifsTconInfo *tcon;
801 __u16 netfid; 800 __u16 netfid;
802 __u8 lockType = LOCKING_ANDX_LARGE_FILES; 801 __u8 lockType = LOCKING_ANDX_LARGE_FILES;
803 bool posix_locking = 0; 802 bool posix_locking = 0;
804 803
805 length = 1 + pfLock->fl_end - pfLock->fl_start; 804 length = 1 + pfLock->fl_end - pfLock->fl_start;
806 rc = -EACCES; 805 rc = -EACCES;
807 xid = GetXid(); 806 xid = GetXid();
808 807
809 cFYI(1, ("Lock parm: 0x%x flockflags: " 808 cFYI(1, ("Lock parm: 0x%x flockflags: "
810 "0x%x flocktype: 0x%x start: %lld end: %lld", 809 "0x%x flocktype: 0x%x start: %lld end: %lld",
811 cmd, pfLock->fl_flags, pfLock->fl_type, pfLock->fl_start, 810 cmd, pfLock->fl_flags, pfLock->fl_type, pfLock->fl_start,
812 pfLock->fl_end)); 811 pfLock->fl_end));
813 812
814 if (pfLock->fl_flags & FL_POSIX) 813 if (pfLock->fl_flags & FL_POSIX)
815 cFYI(1, ("Posix")); 814 cFYI(1, ("Posix"));
816 if (pfLock->fl_flags & FL_FLOCK) 815 if (pfLock->fl_flags & FL_FLOCK)
817 cFYI(1, ("Flock")); 816 cFYI(1, ("Flock"));
818 if (pfLock->fl_flags & FL_SLEEP) { 817 if (pfLock->fl_flags & FL_SLEEP) {
819 cFYI(1, ("Blocking lock")); 818 cFYI(1, ("Blocking lock"));
820 wait_flag = true; 819 wait_flag = true;
821 } 820 }
822 if (pfLock->fl_flags & FL_ACCESS) 821 if (pfLock->fl_flags & FL_ACCESS)
823 cFYI(1, ("Process suspended by mandatory locking - " 822 cFYI(1, ("Process suspended by mandatory locking - "
824 "not implemented yet")); 823 "not implemented yet"));
825 if (pfLock->fl_flags & FL_LEASE) 824 if (pfLock->fl_flags & FL_LEASE)
826 cFYI(1, ("Lease on file - not implemented yet")); 825 cFYI(1, ("Lease on file - not implemented yet"));
827 if (pfLock->fl_flags & 826 if (pfLock->fl_flags &
828 (~(FL_POSIX | FL_FLOCK | FL_SLEEP | FL_ACCESS | FL_LEASE))) 827 (~(FL_POSIX | FL_FLOCK | FL_SLEEP | FL_ACCESS | FL_LEASE)))
829 cFYI(1, ("Unknown lock flags 0x%x", pfLock->fl_flags)); 828 cFYI(1, ("Unknown lock flags 0x%x", pfLock->fl_flags));
830 829
831 if (pfLock->fl_type == F_WRLCK) { 830 if (pfLock->fl_type == F_WRLCK) {
832 cFYI(1, ("F_WRLCK ")); 831 cFYI(1, ("F_WRLCK "));
833 numLock = 1; 832 numLock = 1;
834 } else if (pfLock->fl_type == F_UNLCK) { 833 } else if (pfLock->fl_type == F_UNLCK) {
835 cFYI(1, ("F_UNLCK")); 834 cFYI(1, ("F_UNLCK"));
836 numUnlock = 1; 835 numUnlock = 1;
837 /* Check if unlock includes more than 836 /* Check if unlock includes more than
838 one lock range */ 837 one lock range */
839 } else if (pfLock->fl_type == F_RDLCK) { 838 } else if (pfLock->fl_type == F_RDLCK) {
840 cFYI(1, ("F_RDLCK")); 839 cFYI(1, ("F_RDLCK"));
841 lockType |= LOCKING_ANDX_SHARED_LOCK; 840 lockType |= LOCKING_ANDX_SHARED_LOCK;
842 numLock = 1; 841 numLock = 1;
843 } else if (pfLock->fl_type == F_EXLCK) { 842 } else if (pfLock->fl_type == F_EXLCK) {
844 cFYI(1, ("F_EXLCK")); 843 cFYI(1, ("F_EXLCK"));
845 numLock = 1; 844 numLock = 1;
846 } else if (pfLock->fl_type == F_SHLCK) { 845 } else if (pfLock->fl_type == F_SHLCK) {
847 cFYI(1, ("F_SHLCK")); 846 cFYI(1, ("F_SHLCK"));
848 lockType |= LOCKING_ANDX_SHARED_LOCK; 847 lockType |= LOCKING_ANDX_SHARED_LOCK;
849 numLock = 1; 848 numLock = 1;
850 } else 849 } else
851 cFYI(1, ("Unknown type of lock")); 850 cFYI(1, ("Unknown type of lock"));
852 851
853 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); 852 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
854 tcon = cifs_sb->tcon; 853 tcon = cifs_sb->tcon;
855 854
856 if (file->private_data == NULL) { 855 if (file->private_data == NULL) {
857 FreeXid(xid); 856 FreeXid(xid);
858 return -EBADF; 857 return -EBADF;
859 } 858 }
860 netfid = ((struct cifsFileInfo *)file->private_data)->netfid; 859 netfid = ((struct cifsFileInfo *)file->private_data)->netfid;
861 860
862 if ((tcon->ses->capabilities & CAP_UNIX) && 861 if ((tcon->ses->capabilities & CAP_UNIX) &&
863 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 862 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
864 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 863 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
865 posix_locking = 1; 864 posix_locking = 1;
866 /* BB add code here to normalize offset and length to 865 /* BB add code here to normalize offset and length to
867 account for negative length which we can not accept over the 866 account for negative length which we can not accept over the
868 wire */ 867 wire */
869 if (IS_GETLK(cmd)) { 868 if (IS_GETLK(cmd)) {
870 if (posix_locking) { 869 if (posix_locking) {
871 int posix_lock_type; 870 int posix_lock_type;
872 if (lockType & LOCKING_ANDX_SHARED_LOCK) 871 if (lockType & LOCKING_ANDX_SHARED_LOCK)
873 posix_lock_type = CIFS_RDLCK; 872 posix_lock_type = CIFS_RDLCK;
874 else 873 else
875 posix_lock_type = CIFS_WRLCK; 874 posix_lock_type = CIFS_WRLCK;
876 rc = CIFSSMBPosixLock(xid, tcon, netfid, 1 /* get */, 875 rc = CIFSSMBPosixLock(xid, tcon, netfid, 1 /* get */,
877 length, pfLock, 876 length, pfLock,
878 posix_lock_type, wait_flag); 877 posix_lock_type, wait_flag);
879 FreeXid(xid); 878 FreeXid(xid);
880 return rc; 879 return rc;
881 } 880 }
882 881
883 /* BB we could chain these into one lock request BB */ 882 /* BB we could chain these into one lock request BB */
884 rc = CIFSSMBLock(xid, tcon, netfid, length, pfLock->fl_start, 883 rc = CIFSSMBLock(xid, tcon, netfid, length, pfLock->fl_start,
885 0, 1, lockType, 0 /* wait flag */ ); 884 0, 1, lockType, 0 /* wait flag */ );
886 if (rc == 0) { 885 if (rc == 0) {
887 rc = CIFSSMBLock(xid, tcon, netfid, length, 886 rc = CIFSSMBLock(xid, tcon, netfid, length,
888 pfLock->fl_start, 1 /* numUnlock */ , 887 pfLock->fl_start, 1 /* numUnlock */ ,
889 0 /* numLock */ , lockType, 888 0 /* numLock */ , lockType,
890 0 /* wait flag */ ); 889 0 /* wait flag */ );
891 pfLock->fl_type = F_UNLCK; 890 pfLock->fl_type = F_UNLCK;
892 if (rc != 0) 891 if (rc != 0)
893 cERROR(1, ("Error unlocking previously locked " 892 cERROR(1, ("Error unlocking previously locked "
894 "range %d during test of lock", rc)); 893 "range %d during test of lock", rc));
895 rc = 0; 894 rc = 0;
896 895
897 } else { 896 } else {
898 /* if rc == ERR_SHARING_VIOLATION ? */ 897 /* if rc == ERR_SHARING_VIOLATION ? */
899 rc = 0; /* do not change lock type to unlock 898 rc = 0; /* do not change lock type to unlock
900 since range in use */ 899 since range in use */
901 } 900 }
902 901
903 FreeXid(xid); 902 FreeXid(xid);
904 return rc; 903 return rc;
905 } 904 }
906 905
907 if (!numLock && !numUnlock) { 906 if (!numLock && !numUnlock) {
908 /* if no lock or unlock then nothing 907 /* if no lock or unlock then nothing
909 to do since we do not know what it is */ 908 to do since we do not know what it is */
910 FreeXid(xid); 909 FreeXid(xid);
911 return -EOPNOTSUPP; 910 return -EOPNOTSUPP;
912 } 911 }
913 912
914 if (posix_locking) { 913 if (posix_locking) {
915 int posix_lock_type; 914 int posix_lock_type;
916 if (lockType & LOCKING_ANDX_SHARED_LOCK) 915 if (lockType & LOCKING_ANDX_SHARED_LOCK)
917 posix_lock_type = CIFS_RDLCK; 916 posix_lock_type = CIFS_RDLCK;
918 else 917 else
919 posix_lock_type = CIFS_WRLCK; 918 posix_lock_type = CIFS_WRLCK;
920 919
921 if (numUnlock == 1) 920 if (numUnlock == 1)
922 posix_lock_type = CIFS_UNLCK; 921 posix_lock_type = CIFS_UNLCK;
923 922
924 rc = CIFSSMBPosixLock(xid, tcon, netfid, 0 /* set */, 923 rc = CIFSSMBPosixLock(xid, tcon, netfid, 0 /* set */,
925 length, pfLock, 924 length, pfLock,
926 posix_lock_type, wait_flag); 925 posix_lock_type, wait_flag);
927 } else { 926 } else {
928 struct cifsFileInfo *fid = 927 struct cifsFileInfo *fid =
929 (struct cifsFileInfo *)file->private_data; 928 (struct cifsFileInfo *)file->private_data;
930 929
931 if (numLock) { 930 if (numLock) {
932 rc = CIFSSMBLock(xid, tcon, netfid, length, 931 rc = CIFSSMBLock(xid, tcon, netfid, length,
933 pfLock->fl_start, 932 pfLock->fl_start,
934 0, numLock, lockType, wait_flag); 933 0, numLock, lockType, wait_flag);
935 934
936 if (rc == 0) { 935 if (rc == 0) {
937 /* For Windows locks we must store them. */ 936 /* For Windows locks we must store them. */
938 rc = store_file_lock(fid, length, 937 rc = store_file_lock(fid, length,
939 pfLock->fl_start, lockType); 938 pfLock->fl_start, lockType);
940 } 939 }
941 } else if (numUnlock) { 940 } else if (numUnlock) {
942 /* For each stored lock that this unlock overlaps 941 /* For each stored lock that this unlock overlaps
943 completely, unlock it. */ 942 completely, unlock it. */
944 int stored_rc = 0; 943 int stored_rc = 0;
945 struct cifsLockInfo *li, *tmp; 944 struct cifsLockInfo *li, *tmp;
946 945
947 rc = 0; 946 rc = 0;
948 mutex_lock(&fid->lock_mutex); 947 mutex_lock(&fid->lock_mutex);
949 list_for_each_entry_safe(li, tmp, &fid->llist, llist) { 948 list_for_each_entry_safe(li, tmp, &fid->llist, llist) {
950 if (pfLock->fl_start <= li->offset && 949 if (pfLock->fl_start <= li->offset &&
951 (pfLock->fl_start + length) >= 950 (pfLock->fl_start + length) >=
952 (li->offset + li->length)) { 951 (li->offset + li->length)) {
953 stored_rc = CIFSSMBLock(xid, tcon, 952 stored_rc = CIFSSMBLock(xid, tcon,
954 netfid, 953 netfid,
955 li->length, li->offset, 954 li->length, li->offset,
956 1, 0, li->type, false); 955 1, 0, li->type, false);
957 if (stored_rc) 956 if (stored_rc)
958 rc = stored_rc; 957 rc = stored_rc;
959 958
960 list_del(&li->llist); 959 list_del(&li->llist);
961 kfree(li); 960 kfree(li);
962 } 961 }
963 } 962 }
964 mutex_unlock(&fid->lock_mutex); 963 mutex_unlock(&fid->lock_mutex);
965 } 964 }
966 } 965 }
967 966
968 if (pfLock->fl_flags & FL_POSIX) 967 if (pfLock->fl_flags & FL_POSIX)
969 posix_lock_file_wait(file, pfLock); 968 posix_lock_file_wait(file, pfLock);
970 FreeXid(xid); 969 FreeXid(xid);
971 return rc; 970 return rc;
972 } 971 }
973 972
974 /* 973 /*
975 * Set the timeout on write requests past EOF. For some servers (Windows) 974 * Set the timeout on write requests past EOF. For some servers (Windows)
976 * these calls can be very long. 975 * these calls can be very long.
977 * 976 *
978 * If we're writing >10M past the EOF we give a 180s timeout. Anything less 977 * If we're writing >10M past the EOF we give a 180s timeout. Anything less
979 * than that gets a 45s timeout. Writes not past EOF get 15s timeouts. 978 * than that gets a 45s timeout. Writes not past EOF get 15s timeouts.
980 * The 10M cutoff is totally arbitrary. A better scheme for this would be 979 * The 10M cutoff is totally arbitrary. A better scheme for this would be
981 * welcome if someone wants to suggest one. 980 * welcome if someone wants to suggest one.
982 * 981 *
983 * We may be able to do a better job with this if there were some way to 982 * We may be able to do a better job with this if there were some way to
984 * declare that a file should be sparse. 983 * declare that a file should be sparse.
985 */ 984 */
986 static int 985 static int
987 cifs_write_timeout(struct cifsInodeInfo *cifsi, loff_t offset) 986 cifs_write_timeout(struct cifsInodeInfo *cifsi, loff_t offset)
988 { 987 {
989 if (offset <= cifsi->server_eof) 988 if (offset <= cifsi->server_eof)
990 return CIFS_STD_OP; 989 return CIFS_STD_OP;
991 else if (offset > (cifsi->server_eof + (10 * 1024 * 1024))) 990 else if (offset > (cifsi->server_eof + (10 * 1024 * 1024)))
992 return CIFS_VLONG_OP; 991 return CIFS_VLONG_OP;
993 else 992 else
994 return CIFS_LONG_OP; 993 return CIFS_LONG_OP;
995 } 994 }
996 995
997 /* update the file size (if needed) after a write */ 996 /* update the file size (if needed) after a write */
998 static void 997 static void
999 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset, 998 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
1000 unsigned int bytes_written) 999 unsigned int bytes_written)
1001 { 1000 {
1002 loff_t end_of_write = offset + bytes_written; 1001 loff_t end_of_write = offset + bytes_written;
1003 1002
1004 if (end_of_write > cifsi->server_eof) 1003 if (end_of_write > cifsi->server_eof)
1005 cifsi->server_eof = end_of_write; 1004 cifsi->server_eof = end_of_write;
1006 } 1005 }
1007 1006
1008 ssize_t cifs_user_write(struct file *file, const char __user *write_data, 1007 ssize_t cifs_user_write(struct file *file, const char __user *write_data,
1009 size_t write_size, loff_t *poffset) 1008 size_t write_size, loff_t *poffset)
1010 { 1009 {
1011 int rc = 0; 1010 int rc = 0;
1012 unsigned int bytes_written = 0; 1011 unsigned int bytes_written = 0;
1013 unsigned int total_written; 1012 unsigned int total_written;
1014 struct cifs_sb_info *cifs_sb; 1013 struct cifs_sb_info *cifs_sb;
1015 struct cifsTconInfo *pTcon; 1014 struct cifsTconInfo *pTcon;
1016 int xid, long_op; 1015 int xid, long_op;
1017 struct cifsFileInfo *open_file; 1016 struct cifsFileInfo *open_file;
1018 struct cifsInodeInfo *cifsi = CIFS_I(file->f_path.dentry->d_inode); 1017 struct cifsInodeInfo *cifsi = CIFS_I(file->f_path.dentry->d_inode);
1019 1018
1020 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); 1019 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1021 1020
1022 pTcon = cifs_sb->tcon; 1021 pTcon = cifs_sb->tcon;
1023 1022
1024 /* cFYI(1, 1023 /* cFYI(1,
1025 (" write %d bytes to offset %lld of %s", write_size, 1024 (" write %d bytes to offset %lld of %s", write_size,
1026 *poffset, file->f_path.dentry->d_name.name)); */ 1025 *poffset, file->f_path.dentry->d_name.name)); */
1027 1026
1028 if (file->private_data == NULL) 1027 if (file->private_data == NULL)
1029 return -EBADF; 1028 return -EBADF;
1030 open_file = (struct cifsFileInfo *) file->private_data; 1029 open_file = (struct cifsFileInfo *) file->private_data;
1031 1030
1032 rc = generic_write_checks(file, poffset, &write_size, 0); 1031 rc = generic_write_checks(file, poffset, &write_size, 0);
1033 if (rc) 1032 if (rc)
1034 return rc; 1033 return rc;
1035 1034
1036 xid = GetXid(); 1035 xid = GetXid();
1037 1036
1038 long_op = cifs_write_timeout(cifsi, *poffset); 1037 long_op = cifs_write_timeout(cifsi, *poffset);
1039 for (total_written = 0; write_size > total_written; 1038 for (total_written = 0; write_size > total_written;
1040 total_written += bytes_written) { 1039 total_written += bytes_written) {
1041 rc = -EAGAIN; 1040 rc = -EAGAIN;
1042 while (rc == -EAGAIN) { 1041 while (rc == -EAGAIN) {
1043 if (file->private_data == NULL) { 1042 if (file->private_data == NULL) {
1044 /* file has been closed on us */ 1043 /* file has been closed on us */
1045 FreeXid(xid); 1044 FreeXid(xid);
1046 /* if we have gotten here we have written some data 1045 /* if we have gotten here we have written some data
1047 and blocked, and the file has been freed on us while 1046 and blocked, and the file has been freed on us while
1048 we blocked so return what we managed to write */ 1047 we blocked so return what we managed to write */
1049 return total_written; 1048 return total_written;
1050 } 1049 }
1051 if (open_file->closePend) { 1050 if (open_file->closePend) {
1052 FreeXid(xid); 1051 FreeXid(xid);
1053 if (total_written) 1052 if (total_written)
1054 return total_written; 1053 return total_written;
1055 else 1054 else
1056 return -EBADF; 1055 return -EBADF;
1057 } 1056 }
1058 if (open_file->invalidHandle) { 1057 if (open_file->invalidHandle) {
1059 /* we could deadlock if we called 1058 /* we could deadlock if we called
1060 filemap_fdatawait from here so tell 1059 filemap_fdatawait from here so tell
1061 reopen_file not to flush data to server 1060 reopen_file not to flush data to server
1062 now */ 1061 now */
1063 rc = cifs_reopen_file(file, false); 1062 rc = cifs_reopen_file(file, false);
1064 if (rc != 0) 1063 if (rc != 0)
1065 break; 1064 break;
1066 } 1065 }
1067 1066
1068 rc = CIFSSMBWrite(xid, pTcon, 1067 rc = CIFSSMBWrite(xid, pTcon,
1069 open_file->netfid, 1068 open_file->netfid,
1070 min_t(const int, cifs_sb->wsize, 1069 min_t(const int, cifs_sb->wsize,
1071 write_size - total_written), 1070 write_size - total_written),
1072 *poffset, &bytes_written, 1071 *poffset, &bytes_written,
1073 NULL, write_data + total_written, long_op); 1072 NULL, write_data + total_written, long_op);
1074 } 1073 }
1075 if (rc || (bytes_written == 0)) { 1074 if (rc || (bytes_written == 0)) {
1076 if (total_written) 1075 if (total_written)
1077 break; 1076 break;
1078 else { 1077 else {
1079 FreeXid(xid); 1078 FreeXid(xid);
1080 return rc; 1079 return rc;
1081 } 1080 }
1082 } else { 1081 } else {
1083 cifs_update_eof(cifsi, *poffset, bytes_written); 1082 cifs_update_eof(cifsi, *poffset, bytes_written);
1084 *poffset += bytes_written; 1083 *poffset += bytes_written;
1085 } 1084 }
1086 long_op = CIFS_STD_OP; /* subsequent writes fast - 1085 long_op = CIFS_STD_OP; /* subsequent writes fast -
1087 15 seconds is plenty */ 1086 15 seconds is plenty */
1088 } 1087 }
1089 1088
1090 cifs_stats_bytes_written(pTcon, total_written); 1089 cifs_stats_bytes_written(pTcon, total_written);
1091 1090
1092 /* since the write may have blocked check these pointers again */ 1091 /* since the write may have blocked check these pointers again */
1093 if ((file->f_path.dentry) && (file->f_path.dentry->d_inode)) { 1092 if ((file->f_path.dentry) && (file->f_path.dentry->d_inode)) {
1094 struct inode *inode = file->f_path.dentry->d_inode; 1093 struct inode *inode = file->f_path.dentry->d_inode;
1095 /* Do not update local mtime - server will set its actual value on write 1094 /* Do not update local mtime - server will set its actual value on write
1096 * inode->i_ctime = inode->i_mtime = 1095 * inode->i_ctime = inode->i_mtime =
1097 * current_fs_time(inode->i_sb);*/ 1096 * current_fs_time(inode->i_sb);*/
1098 if (total_written > 0) { 1097 if (total_written > 0) {
1099 spin_lock(&inode->i_lock); 1098 spin_lock(&inode->i_lock);
1100 if (*poffset > file->f_path.dentry->d_inode->i_size) 1099 if (*poffset > file->f_path.dentry->d_inode->i_size)
1101 i_size_write(file->f_path.dentry->d_inode, 1100 i_size_write(file->f_path.dentry->d_inode,
1102 *poffset); 1101 *poffset);
1103 spin_unlock(&inode->i_lock); 1102 spin_unlock(&inode->i_lock);
1104 } 1103 }
1105 mark_inode_dirty_sync(file->f_path.dentry->d_inode); 1104 mark_inode_dirty_sync(file->f_path.dentry->d_inode);
1106 } 1105 }
1107 FreeXid(xid); 1106 FreeXid(xid);
1108 return total_written; 1107 return total_written;
1109 } 1108 }
1110 1109
1111 static ssize_t cifs_write(struct file *file, const char *write_data, 1110 static ssize_t cifs_write(struct file *file, const char *write_data,
1112 size_t write_size, loff_t *poffset) 1111 size_t write_size, loff_t *poffset)
1113 { 1112 {
1114 int rc = 0; 1113 int rc = 0;
1115 unsigned int bytes_written = 0; 1114 unsigned int bytes_written = 0;
1116 unsigned int total_written; 1115 unsigned int total_written;
1117 struct cifs_sb_info *cifs_sb; 1116 struct cifs_sb_info *cifs_sb;
1118 struct cifsTconInfo *pTcon; 1117 struct cifsTconInfo *pTcon;
1119 int xid, long_op; 1118 int xid, long_op;
1120 struct cifsFileInfo *open_file; 1119 struct cifsFileInfo *open_file;
1121 struct cifsInodeInfo *cifsi = CIFS_I(file->f_path.dentry->d_inode); 1120 struct cifsInodeInfo *cifsi = CIFS_I(file->f_path.dentry->d_inode);
1122 1121
1123 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); 1122 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1124 1123
1125 pTcon = cifs_sb->tcon; 1124 pTcon = cifs_sb->tcon;
1126 1125
1127 cFYI(1, ("write %zd bytes to offset %lld of %s", write_size, 1126 cFYI(1, ("write %zd bytes to offset %lld of %s", write_size,
1128 *poffset, file->f_path.dentry->d_name.name)); 1127 *poffset, file->f_path.dentry->d_name.name));
1129 1128
1130 if (file->private_data == NULL) 1129 if (file->private_data == NULL)
1131 return -EBADF; 1130 return -EBADF;
1132 open_file = (struct cifsFileInfo *)file->private_data; 1131 open_file = (struct cifsFileInfo *)file->private_data;
1133 1132
1134 xid = GetXid(); 1133 xid = GetXid();
1135 1134
1136 long_op = cifs_write_timeout(cifsi, *poffset); 1135 long_op = cifs_write_timeout(cifsi, *poffset);
1137 for (total_written = 0; write_size > total_written; 1136 for (total_written = 0; write_size > total_written;
1138 total_written += bytes_written) { 1137 total_written += bytes_written) {
1139 rc = -EAGAIN; 1138 rc = -EAGAIN;
1140 while (rc == -EAGAIN) { 1139 while (rc == -EAGAIN) {
1141 if (file->private_data == NULL) { 1140 if (file->private_data == NULL) {
1142 /* file has been closed on us */ 1141 /* file has been closed on us */
1143 FreeXid(xid); 1142 FreeXid(xid);
1144 /* if we have gotten here we have written some data 1143 /* if we have gotten here we have written some data
1145 and blocked, and the file has been freed on us 1144 and blocked, and the file has been freed on us
1146 while we blocked so return what we managed to 1145 while we blocked so return what we managed to
1147 write */ 1146 write */
1148 return total_written; 1147 return total_written;
1149 } 1148 }
1150 if (open_file->closePend) { 1149 if (open_file->closePend) {
1151 FreeXid(xid); 1150 FreeXid(xid);
1152 if (total_written) 1151 if (total_written)
1153 return total_written; 1152 return total_written;
1154 else 1153 else
1155 return -EBADF; 1154 return -EBADF;
1156 } 1155 }
1157 if (open_file->invalidHandle) { 1156 if (open_file->invalidHandle) {
1158 /* we could deadlock if we called 1157 /* we could deadlock if we called
1159 filemap_fdatawait from here so tell 1158 filemap_fdatawait from here so tell
1160 reopen_file not to flush data to 1159 reopen_file not to flush data to
1161 server now */ 1160 server now */
1162 rc = cifs_reopen_file(file, false); 1161 rc = cifs_reopen_file(file, false);
1163 if (rc != 0) 1162 if (rc != 0)
1164 break; 1163 break;
1165 } 1164 }
1166 if (experimEnabled || (pTcon->ses->server && 1165 if (experimEnabled || (pTcon->ses->server &&
1167 ((pTcon->ses->server->secMode & 1166 ((pTcon->ses->server->secMode &
1168 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) 1167 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
1169 == 0))) { 1168 == 0))) {
1170 struct kvec iov[2]; 1169 struct kvec iov[2];
1171 unsigned int len; 1170 unsigned int len;
1172 1171
1173 len = min((size_t)cifs_sb->wsize, 1172 len = min((size_t)cifs_sb->wsize,
1174 write_size - total_written); 1173 write_size - total_written);
1175 /* iov[0] is reserved for smb header */ 1174 /* iov[0] is reserved for smb header */
1176 iov[1].iov_base = (char *)write_data + 1175 iov[1].iov_base = (char *)write_data +
1177 total_written; 1176 total_written;
1178 iov[1].iov_len = len; 1177 iov[1].iov_len = len;
1179 rc = CIFSSMBWrite2(xid, pTcon, 1178 rc = CIFSSMBWrite2(xid, pTcon,
1180 open_file->netfid, len, 1179 open_file->netfid, len,
1181 *poffset, &bytes_written, 1180 *poffset, &bytes_written,
1182 iov, 1, long_op); 1181 iov, 1, long_op);
1183 } else 1182 } else
1184 rc = CIFSSMBWrite(xid, pTcon, 1183 rc = CIFSSMBWrite(xid, pTcon,
1185 open_file->netfid, 1184 open_file->netfid,
1186 min_t(const int, cifs_sb->wsize, 1185 min_t(const int, cifs_sb->wsize,
1187 write_size - total_written), 1186 write_size - total_written),
1188 *poffset, &bytes_written, 1187 *poffset, &bytes_written,
1189 write_data + total_written, 1188 write_data + total_written,
1190 NULL, long_op); 1189 NULL, long_op);
1191 } 1190 }
1192 if (rc || (bytes_written == 0)) { 1191 if (rc || (bytes_written == 0)) {
1193 if (total_written) 1192 if (total_written)
1194 break; 1193 break;
1195 else { 1194 else {
1196 FreeXid(xid); 1195 FreeXid(xid);
1197 return rc; 1196 return rc;
1198 } 1197 }
1199 } else { 1198 } else {
1200 cifs_update_eof(cifsi, *poffset, bytes_written); 1199 cifs_update_eof(cifsi, *poffset, bytes_written);
1201 *poffset += bytes_written; 1200 *poffset += bytes_written;
1202 } 1201 }
1203 long_op = CIFS_STD_OP; /* subsequent writes fast - 1202 long_op = CIFS_STD_OP; /* subsequent writes fast -
1204 15 seconds is plenty */ 1203 15 seconds is plenty */
1205 } 1204 }
1206 1205
1207 cifs_stats_bytes_written(pTcon, total_written); 1206 cifs_stats_bytes_written(pTcon, total_written);
1208 1207
1209 /* since the write may have blocked check these pointers again */ 1208 /* since the write may have blocked check these pointers again */
1210 if ((file->f_path.dentry) && (file->f_path.dentry->d_inode)) { 1209 if ((file->f_path.dentry) && (file->f_path.dentry->d_inode)) {
1211 /*BB We could make this contingent on superblock ATIME flag too */ 1210 /*BB We could make this contingent on superblock ATIME flag too */
1212 /* file->f_path.dentry->d_inode->i_ctime = 1211 /* file->f_path.dentry->d_inode->i_ctime =
1213 file->f_path.dentry->d_inode->i_mtime = CURRENT_TIME;*/ 1212 file->f_path.dentry->d_inode->i_mtime = CURRENT_TIME;*/
1214 if (total_written > 0) { 1213 if (total_written > 0) {
1215 spin_lock(&file->f_path.dentry->d_inode->i_lock); 1214 spin_lock(&file->f_path.dentry->d_inode->i_lock);
1216 if (*poffset > file->f_path.dentry->d_inode->i_size) 1215 if (*poffset > file->f_path.dentry->d_inode->i_size)
1217 i_size_write(file->f_path.dentry->d_inode, 1216 i_size_write(file->f_path.dentry->d_inode,
1218 *poffset); 1217 *poffset);
1219 spin_unlock(&file->f_path.dentry->d_inode->i_lock); 1218 spin_unlock(&file->f_path.dentry->d_inode->i_lock);
1220 } 1219 }
1221 mark_inode_dirty_sync(file->f_path.dentry->d_inode); 1220 mark_inode_dirty_sync(file->f_path.dentry->d_inode);
1222 } 1221 }
1223 FreeXid(xid); 1222 FreeXid(xid);
1224 return total_written; 1223 return total_written;
1225 } 1224 }
1226 1225
1227 #ifdef CONFIG_CIFS_EXPERIMENTAL 1226 #ifdef CONFIG_CIFS_EXPERIMENTAL
1228 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode) 1227 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode)
1229 { 1228 {
1230 struct cifsFileInfo *open_file = NULL; 1229 struct cifsFileInfo *open_file = NULL;
1231 1230
1232 read_lock(&GlobalSMBSeslock); 1231 read_lock(&GlobalSMBSeslock);
1233 /* we could simply get the first_list_entry since write-only entries 1232 /* we could simply get the first_list_entry since write-only entries
1234 are always at the end of the list but since the first entry might 1233 are always at the end of the list but since the first entry might
1235 have a close pending, we go through the whole list */ 1234 have a close pending, we go through the whole list */
1236 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 1235 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1237 if (open_file->closePend) 1236 if (open_file->closePend)
1238 continue; 1237 continue;
1239 if (open_file->pfile && ((open_file->pfile->f_flags & O_RDWR) || 1238 if (open_file->pfile && ((open_file->pfile->f_flags & O_RDWR) ||
1240 (open_file->pfile->f_flags & O_RDONLY))) { 1239 (open_file->pfile->f_flags & O_RDONLY))) {
1241 if (!open_file->invalidHandle) { 1240 if (!open_file->invalidHandle) {
1242 /* found a good file */ 1241 /* found a good file */
1243 /* lock it so it will not be closed on us */ 1242 /* lock it so it will not be closed on us */
1244 atomic_inc(&open_file->wrtPending); 1243 atomic_inc(&open_file->wrtPending);
1245 read_unlock(&GlobalSMBSeslock); 1244 read_unlock(&GlobalSMBSeslock);
1246 return open_file; 1245 return open_file;
1247 } /* else might as well continue, and look for 1246 } /* else might as well continue, and look for
1248 another, or simply have the caller reopen it 1247 another, or simply have the caller reopen it
1249 again rather than trying to fix this handle */ 1248 again rather than trying to fix this handle */
1250 } else /* write only file */ 1249 } else /* write only file */
1251 break; /* write only files are last so must be done */ 1250 break; /* write only files are last so must be done */
1252 } 1251 }
1253 read_unlock(&GlobalSMBSeslock); 1252 read_unlock(&GlobalSMBSeslock);
1254 return NULL; 1253 return NULL;
1255 } 1254 }
1256 #endif 1255 #endif
1257 1256
1258 struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode) 1257 struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode)
1259 { 1258 {
1260 struct cifsFileInfo *open_file; 1259 struct cifsFileInfo *open_file;
1261 bool any_available = false; 1260 bool any_available = false;
1262 int rc; 1261 int rc;
1263 1262
1264 /* Having a null inode here (because mapping->host was set to zero by 1263 /* Having a null inode here (because mapping->host was set to zero by
1265 the VFS or MM) should not happen but we had reports of on oops (due to 1264 the VFS or MM) should not happen but we had reports of on oops (due to
1266 it being zero) during stress testcases so we need to check for it */ 1265 it being zero) during stress testcases so we need to check for it */
1267 1266
1268 if (cifs_inode == NULL) { 1267 if (cifs_inode == NULL) {
1269 cERROR(1, ("Null inode passed to cifs_writeable_file")); 1268 cERROR(1, ("Null inode passed to cifs_writeable_file"));
1270 dump_stack(); 1269 dump_stack();
1271 return NULL; 1270 return NULL;
1272 } 1271 }
1273 1272
1274 read_lock(&GlobalSMBSeslock); 1273 read_lock(&GlobalSMBSeslock);
1275 refind_writable: 1274 refind_writable:
1276 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 1275 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1277 if (open_file->closePend || 1276 if (open_file->closePend ||
1278 (!any_available && open_file->pid != current->tgid)) 1277 (!any_available && open_file->pid != current->tgid))
1279 continue; 1278 continue;
1280 1279
1281 if (open_file->pfile && 1280 if (open_file->pfile &&
1282 ((open_file->pfile->f_flags & O_RDWR) || 1281 ((open_file->pfile->f_flags & O_RDWR) ||
1283 (open_file->pfile->f_flags & O_WRONLY))) { 1282 (open_file->pfile->f_flags & O_WRONLY))) {
1284 atomic_inc(&open_file->wrtPending); 1283 atomic_inc(&open_file->wrtPending);
1285 1284
1286 if (!open_file->invalidHandle) { 1285 if (!open_file->invalidHandle) {
1287 /* found a good writable file */ 1286 /* found a good writable file */
1288 read_unlock(&GlobalSMBSeslock); 1287 read_unlock(&GlobalSMBSeslock);
1289 return open_file; 1288 return open_file;
1290 } 1289 }
1291 1290
1292 read_unlock(&GlobalSMBSeslock); 1291 read_unlock(&GlobalSMBSeslock);
1293 /* Had to unlock since following call can block */ 1292 /* Had to unlock since following call can block */
1294 rc = cifs_reopen_file(open_file->pfile, false); 1293 rc = cifs_reopen_file(open_file->pfile, false);
1295 if (!rc) { 1294 if (!rc) {
1296 if (!open_file->closePend) 1295 if (!open_file->closePend)
1297 return open_file; 1296 return open_file;
1298 else { /* start over in case this was deleted */ 1297 else { /* start over in case this was deleted */
1299 /* since the list could be modified */ 1298 /* since the list could be modified */
1300 read_lock(&GlobalSMBSeslock); 1299 read_lock(&GlobalSMBSeslock);
1301 atomic_dec(&open_file->wrtPending); 1300 atomic_dec(&open_file->wrtPending);
1302 goto refind_writable; 1301 goto refind_writable;
1303 } 1302 }
1304 } 1303 }
1305 1304
1306 /* if it fails, try another handle if possible - 1305 /* if it fails, try another handle if possible -
1307 (we can not do this if closePending since 1306 (we can not do this if closePending since
1308 loop could be modified - in which case we 1307 loop could be modified - in which case we
1309 have to start at the beginning of the list 1308 have to start at the beginning of the list
1310 again. Note that it would be bad 1309 again. Note that it would be bad
1311 to hold up writepages here (rather than 1310 to hold up writepages here (rather than
1312 in caller) with continuous retries */ 1311 in caller) with continuous retries */
1313 cFYI(1, ("wp failed on reopen file")); 1312 cFYI(1, ("wp failed on reopen file"));
1314 read_lock(&GlobalSMBSeslock); 1313 read_lock(&GlobalSMBSeslock);
1315 /* can not use this handle, no write 1314 /* can not use this handle, no write
1316 pending on this one after all */ 1315 pending on this one after all */
1317 atomic_dec(&open_file->wrtPending); 1316 atomic_dec(&open_file->wrtPending);
1318 1317
1319 if (open_file->closePend) /* list could have changed */ 1318 if (open_file->closePend) /* list could have changed */
1320 goto refind_writable; 1319 goto refind_writable;
1321 /* else we simply continue to the next entry. Thus 1320 /* else we simply continue to the next entry. Thus
1322 we do not loop on reopen errors. If we 1321 we do not loop on reopen errors. If we
1323 can not reopen the file, for example if we 1322 can not reopen the file, for example if we
1324 reconnected to a server with another client 1323 reconnected to a server with another client
1325 racing to delete or lock the file we would not 1324 racing to delete or lock the file we would not
1326 make progress if we restarted before the beginning 1325 make progress if we restarted before the beginning
1327 of the loop here. */ 1326 of the loop here. */
1328 } 1327 }
1329 } 1328 }
1330 /* couldn't find useable FH with same pid, try any available */ 1329 /* couldn't find useable FH with same pid, try any available */
1331 if (!any_available) { 1330 if (!any_available) {
1332 any_available = true; 1331 any_available = true;
1333 goto refind_writable; 1332 goto refind_writable;
1334 } 1333 }
1335 read_unlock(&GlobalSMBSeslock); 1334 read_unlock(&GlobalSMBSeslock);
1336 return NULL; 1335 return NULL;
1337 } 1336 }
1338 1337
1339 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to) 1338 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
1340 { 1339 {
1341 struct address_space *mapping = page->mapping; 1340 struct address_space *mapping = page->mapping;
1342 loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT; 1341 loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
1343 char *write_data; 1342 char *write_data;
1344 int rc = -EFAULT; 1343 int rc = -EFAULT;
1345 int bytes_written = 0; 1344 int bytes_written = 0;
1346 struct cifs_sb_info *cifs_sb; 1345 struct cifs_sb_info *cifs_sb;
1347 struct cifsTconInfo *pTcon; 1346 struct cifsTconInfo *pTcon;
1348 struct inode *inode; 1347 struct inode *inode;
1349 struct cifsFileInfo *open_file; 1348 struct cifsFileInfo *open_file;
1350 1349
1351 if (!mapping || !mapping->host) 1350 if (!mapping || !mapping->host)
1352 return -EFAULT; 1351 return -EFAULT;
1353 1352
1354 inode = page->mapping->host; 1353 inode = page->mapping->host;
1355 cifs_sb = CIFS_SB(inode->i_sb); 1354 cifs_sb = CIFS_SB(inode->i_sb);
1356 pTcon = cifs_sb->tcon; 1355 pTcon = cifs_sb->tcon;
1357 1356
1358 offset += (loff_t)from; 1357 offset += (loff_t)from;
1359 write_data = kmap(page); 1358 write_data = kmap(page);
1360 write_data += from; 1359 write_data += from;
1361 1360
1362 if ((to > PAGE_CACHE_SIZE) || (from > to)) { 1361 if ((to > PAGE_CACHE_SIZE) || (from > to)) {
1363 kunmap(page); 1362 kunmap(page);
1364 return -EIO; 1363 return -EIO;
1365 } 1364 }
1366 1365
1367 /* racing with truncate? */ 1366 /* racing with truncate? */
1368 if (offset > mapping->host->i_size) { 1367 if (offset > mapping->host->i_size) {
1369 kunmap(page); 1368 kunmap(page);
1370 return 0; /* don't care */ 1369 return 0; /* don't care */
1371 } 1370 }
1372 1371
1373 /* check to make sure that we are not extending the file */ 1372 /* check to make sure that we are not extending the file */
1374 if (mapping->host->i_size - offset < (loff_t)to) 1373 if (mapping->host->i_size - offset < (loff_t)to)
1375 to = (unsigned)(mapping->host->i_size - offset); 1374 to = (unsigned)(mapping->host->i_size - offset);
1376 1375
1377 open_file = find_writable_file(CIFS_I(mapping->host)); 1376 open_file = find_writable_file(CIFS_I(mapping->host));
1378 if (open_file) { 1377 if (open_file) {
1379 bytes_written = cifs_write(open_file->pfile, write_data, 1378 bytes_written = cifs_write(open_file->pfile, write_data,
1380 to-from, &offset); 1379 to-from, &offset);
1381 atomic_dec(&open_file->wrtPending); 1380 atomic_dec(&open_file->wrtPending);
1382 /* Does mm or vfs already set times? */ 1381 /* Does mm or vfs already set times? */
1383 inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb); 1382 inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb);
1384 if ((bytes_written > 0) && (offset)) 1383 if ((bytes_written > 0) && (offset))
1385 rc = 0; 1384 rc = 0;
1386 else if (bytes_written < 0) 1385 else if (bytes_written < 0)
1387 rc = bytes_written; 1386 rc = bytes_written;
1388 } else { 1387 } else {
1389 cFYI(1, ("No writeable filehandles for inode")); 1388 cFYI(1, ("No writeable filehandles for inode"));
1390 rc = -EIO; 1389 rc = -EIO;
1391 } 1390 }
1392 1391
1393 kunmap(page); 1392 kunmap(page);
1394 return rc; 1393 return rc;
1395 } 1394 }
1396 1395
1397 static int cifs_writepages(struct address_space *mapping, 1396 static int cifs_writepages(struct address_space *mapping,
1398 struct writeback_control *wbc) 1397 struct writeback_control *wbc)
1399 { 1398 {
1400 struct backing_dev_info *bdi = mapping->backing_dev_info; 1399 struct backing_dev_info *bdi = mapping->backing_dev_info;
1401 unsigned int bytes_to_write; 1400 unsigned int bytes_to_write;
1402 unsigned int bytes_written; 1401 unsigned int bytes_written;
1403 struct cifs_sb_info *cifs_sb; 1402 struct cifs_sb_info *cifs_sb;
1404 int done = 0; 1403 int done = 0;
1405 pgoff_t end; 1404 pgoff_t end;
1406 pgoff_t index; 1405 pgoff_t index;
1407 int range_whole = 0; 1406 int range_whole = 0;
1408 struct kvec *iov; 1407 struct kvec *iov;
1409 int len; 1408 int len;
1410 int n_iov = 0; 1409 int n_iov = 0;
1411 pgoff_t next; 1410 pgoff_t next;
1412 int nr_pages; 1411 int nr_pages;
1413 __u64 offset = 0; 1412 __u64 offset = 0;
1414 struct cifsFileInfo *open_file; 1413 struct cifsFileInfo *open_file;
1415 struct cifsInodeInfo *cifsi = CIFS_I(mapping->host); 1414 struct cifsInodeInfo *cifsi = CIFS_I(mapping->host);
1416 struct page *page; 1415 struct page *page;
1417 struct pagevec pvec; 1416 struct pagevec pvec;
1418 int rc = 0; 1417 int rc = 0;
1419 int scanned = 0; 1418 int scanned = 0;
1420 int xid, long_op; 1419 int xid, long_op;
1421 1420
1422 cifs_sb = CIFS_SB(mapping->host->i_sb); 1421 cifs_sb = CIFS_SB(mapping->host->i_sb);
1423 1422
1424 /* 1423 /*
1425 * If wsize is smaller that the page cache size, default to writing 1424 * If wsize is smaller that the page cache size, default to writing
1426 * one page at a time via cifs_writepage 1425 * one page at a time via cifs_writepage
1427 */ 1426 */
1428 if (cifs_sb->wsize < PAGE_CACHE_SIZE) 1427 if (cifs_sb->wsize < PAGE_CACHE_SIZE)
1429 return generic_writepages(mapping, wbc); 1428 return generic_writepages(mapping, wbc);
1430 1429
1431 if ((cifs_sb->tcon->ses) && (cifs_sb->tcon->ses->server)) 1430 if ((cifs_sb->tcon->ses) && (cifs_sb->tcon->ses->server))
1432 if (cifs_sb->tcon->ses->server->secMode & 1431 if (cifs_sb->tcon->ses->server->secMode &
1433 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) 1432 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
1434 if (!experimEnabled) 1433 if (!experimEnabled)
1435 return generic_writepages(mapping, wbc); 1434 return generic_writepages(mapping, wbc);
1436 1435
1437 iov = kmalloc(32 * sizeof(struct kvec), GFP_KERNEL); 1436 iov = kmalloc(32 * sizeof(struct kvec), GFP_KERNEL);
1438 if (iov == NULL) 1437 if (iov == NULL)
1439 return generic_writepages(mapping, wbc); 1438 return generic_writepages(mapping, wbc);
1440 1439
1441 1440
1442 /* 1441 /*
1443 * BB: Is this meaningful for a non-block-device file system? 1442 * BB: Is this meaningful for a non-block-device file system?
1444 * If it is, we should test it again after we do I/O 1443 * If it is, we should test it again after we do I/O
1445 */ 1444 */
1446 if (wbc->nonblocking && bdi_write_congested(bdi)) { 1445 if (wbc->nonblocking && bdi_write_congested(bdi)) {
1447 wbc->encountered_congestion = 1; 1446 wbc->encountered_congestion = 1;
1448 kfree(iov); 1447 kfree(iov);
1449 return 0; 1448 return 0;
1450 } 1449 }
1451 1450
1452 xid = GetXid(); 1451 xid = GetXid();
1453 1452
1454 pagevec_init(&pvec, 0); 1453 pagevec_init(&pvec, 0);
1455 if (wbc->range_cyclic) { 1454 if (wbc->range_cyclic) {
1456 index = mapping->writeback_index; /* Start from prev offset */ 1455 index = mapping->writeback_index; /* Start from prev offset */
1457 end = -1; 1456 end = -1;
1458 } else { 1457 } else {
1459 index = wbc->range_start >> PAGE_CACHE_SHIFT; 1458 index = wbc->range_start >> PAGE_CACHE_SHIFT;
1460 end = wbc->range_end >> PAGE_CACHE_SHIFT; 1459 end = wbc->range_end >> PAGE_CACHE_SHIFT;
1461 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 1460 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1462 range_whole = 1; 1461 range_whole = 1;
1463 scanned = 1; 1462 scanned = 1;
1464 } 1463 }
1465 retry: 1464 retry:
1466 while (!done && (index <= end) && 1465 while (!done && (index <= end) &&
1467 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 1466 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1468 PAGECACHE_TAG_DIRTY, 1467 PAGECACHE_TAG_DIRTY,
1469 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1))) { 1468 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1))) {
1470 int first; 1469 int first;
1471 unsigned int i; 1470 unsigned int i;
1472 1471
1473 first = -1; 1472 first = -1;
1474 next = 0; 1473 next = 0;
1475 n_iov = 0; 1474 n_iov = 0;
1476 bytes_to_write = 0; 1475 bytes_to_write = 0;
1477 1476
1478 for (i = 0; i < nr_pages; i++) { 1477 for (i = 0; i < nr_pages; i++) {
1479 page = pvec.pages[i]; 1478 page = pvec.pages[i];
1480 /* 1479 /*
1481 * At this point we hold neither mapping->tree_lock nor 1480 * At this point we hold neither mapping->tree_lock nor
1482 * lock on the page itself: the page may be truncated or 1481 * lock on the page itself: the page may be truncated or
1483 * invalidated (changing page->mapping to NULL), or even 1482 * invalidated (changing page->mapping to NULL), or even
1484 * swizzled back from swapper_space to tmpfs file 1483 * swizzled back from swapper_space to tmpfs file
1485 * mapping 1484 * mapping
1486 */ 1485 */
1487 1486
1488 if (first < 0) 1487 if (first < 0)
1489 lock_page(page); 1488 lock_page(page);
1490 else if (!trylock_page(page)) 1489 else if (!trylock_page(page))
1491 break; 1490 break;
1492 1491
1493 if (unlikely(page->mapping != mapping)) { 1492 if (unlikely(page->mapping != mapping)) {
1494 unlock_page(page); 1493 unlock_page(page);
1495 break; 1494 break;
1496 } 1495 }
1497 1496
1498 if (!wbc->range_cyclic && page->index > end) { 1497 if (!wbc->range_cyclic && page->index > end) {
1499 done = 1; 1498 done = 1;
1500 unlock_page(page); 1499 unlock_page(page);
1501 break; 1500 break;
1502 } 1501 }
1503 1502
1504 if (next && (page->index != next)) { 1503 if (next && (page->index != next)) {
1505 /* Not next consecutive page */ 1504 /* Not next consecutive page */
1506 unlock_page(page); 1505 unlock_page(page);
1507 break; 1506 break;
1508 } 1507 }
1509 1508
1510 if (wbc->sync_mode != WB_SYNC_NONE) 1509 if (wbc->sync_mode != WB_SYNC_NONE)
1511 wait_on_page_writeback(page); 1510 wait_on_page_writeback(page);
1512 1511
1513 if (PageWriteback(page) || 1512 if (PageWriteback(page) ||
1514 !clear_page_dirty_for_io(page)) { 1513 !clear_page_dirty_for_io(page)) {
1515 unlock_page(page); 1514 unlock_page(page);
1516 break; 1515 break;
1517 } 1516 }
1518 1517
1519 /* 1518 /*
1520 * This actually clears the dirty bit in the radix tree. 1519 * This actually clears the dirty bit in the radix tree.
1521 * See cifs_writepage() for more commentary. 1520 * See cifs_writepage() for more commentary.
1522 */ 1521 */
1523 set_page_writeback(page); 1522 set_page_writeback(page);
1524 1523
1525 if (page_offset(page) >= mapping->host->i_size) { 1524 if (page_offset(page) >= mapping->host->i_size) {
1526 done = 1; 1525 done = 1;
1527 unlock_page(page); 1526 unlock_page(page);
1528 end_page_writeback(page); 1527 end_page_writeback(page);
1529 break; 1528 break;
1530 } 1529 }
1531 1530
1532 /* 1531 /*
1533 * BB can we get rid of this? pages are held by pvec 1532 * BB can we get rid of this? pages are held by pvec
1534 */ 1533 */
1535 page_cache_get(page); 1534 page_cache_get(page);
1536 1535
1537 len = min(mapping->host->i_size - page_offset(page), 1536 len = min(mapping->host->i_size - page_offset(page),
1538 (loff_t)PAGE_CACHE_SIZE); 1537 (loff_t)PAGE_CACHE_SIZE);
1539 1538
1540 /* reserve iov[0] for the smb header */ 1539 /* reserve iov[0] for the smb header */
1541 n_iov++; 1540 n_iov++;
1542 iov[n_iov].iov_base = kmap(page); 1541 iov[n_iov].iov_base = kmap(page);
1543 iov[n_iov].iov_len = len; 1542 iov[n_iov].iov_len = len;
1544 bytes_to_write += len; 1543 bytes_to_write += len;
1545 1544
1546 if (first < 0) { 1545 if (first < 0) {
1547 first = i; 1546 first = i;
1548 offset = page_offset(page); 1547 offset = page_offset(page);
1549 } 1548 }
1550 next = page->index + 1; 1549 next = page->index + 1;
1551 if (bytes_to_write + PAGE_CACHE_SIZE > cifs_sb->wsize) 1550 if (bytes_to_write + PAGE_CACHE_SIZE > cifs_sb->wsize)
1552 break; 1551 break;
1553 } 1552 }
1554 if (n_iov) { 1553 if (n_iov) {
1555 /* Search for a writable handle every time we call 1554 /* Search for a writable handle every time we call
1556 * CIFSSMBWrite2. We can't rely on the last handle 1555 * CIFSSMBWrite2. We can't rely on the last handle
1557 * we used to still be valid 1556 * we used to still be valid
1558 */ 1557 */
1559 open_file = find_writable_file(CIFS_I(mapping->host)); 1558 open_file = find_writable_file(CIFS_I(mapping->host));
1560 if (!open_file) { 1559 if (!open_file) {
1561 cERROR(1, ("No writable handles for inode")); 1560 cERROR(1, ("No writable handles for inode"));
1562 rc = -EBADF; 1561 rc = -EBADF;
1563 } else { 1562 } else {
1564 long_op = cifs_write_timeout(cifsi, offset); 1563 long_op = cifs_write_timeout(cifsi, offset);
1565 rc = CIFSSMBWrite2(xid, cifs_sb->tcon, 1564 rc = CIFSSMBWrite2(xid, cifs_sb->tcon,
1566 open_file->netfid, 1565 open_file->netfid,
1567 bytes_to_write, offset, 1566 bytes_to_write, offset,
1568 &bytes_written, iov, n_iov, 1567 &bytes_written, iov, n_iov,
1569 long_op); 1568 long_op);
1570 atomic_dec(&open_file->wrtPending); 1569 atomic_dec(&open_file->wrtPending);
1571 cifs_update_eof(cifsi, offset, bytes_written); 1570 cifs_update_eof(cifsi, offset, bytes_written);
1572 1571
1573 if (rc || bytes_written < bytes_to_write) { 1572 if (rc || bytes_written < bytes_to_write) {
1574 cERROR(1, ("Write2 ret %d, wrote %d", 1573 cERROR(1, ("Write2 ret %d, wrote %d",
1575 rc, bytes_written)); 1574 rc, bytes_written));
1576 /* BB what if continued retry is 1575 /* BB what if continued retry is
1577 requested via mount flags? */ 1576 requested via mount flags? */
1578 if (rc == -ENOSPC) 1577 if (rc == -ENOSPC)
1579 set_bit(AS_ENOSPC, &mapping->flags); 1578 set_bit(AS_ENOSPC, &mapping->flags);
1580 else 1579 else
1581 set_bit(AS_EIO, &mapping->flags); 1580 set_bit(AS_EIO, &mapping->flags);
1582 } else { 1581 } else {
1583 cifs_stats_bytes_written(cifs_sb->tcon, 1582 cifs_stats_bytes_written(cifs_sb->tcon,
1584 bytes_written); 1583 bytes_written);
1585 } 1584 }
1586 } 1585 }
1587 for (i = 0; i < n_iov; i++) { 1586 for (i = 0; i < n_iov; i++) {
1588 page = pvec.pages[first + i]; 1587 page = pvec.pages[first + i];
1589 /* Should we also set page error on 1588 /* Should we also set page error on
1590 success rc but too little data written? */ 1589 success rc but too little data written? */
1591 /* BB investigate retry logic on temporary 1590 /* BB investigate retry logic on temporary
1592 server crash cases and how recovery works 1591 server crash cases and how recovery works
1593 when page marked as error */ 1592 when page marked as error */
1594 if (rc) 1593 if (rc)
1595 SetPageError(page); 1594 SetPageError(page);
1596 kunmap(page); 1595 kunmap(page);
1597 unlock_page(page); 1596 unlock_page(page);
1598 end_page_writeback(page); 1597 end_page_writeback(page);
1599 page_cache_release(page); 1598 page_cache_release(page);
1600 } 1599 }
1601 if ((wbc->nr_to_write -= n_iov) <= 0) 1600 if ((wbc->nr_to_write -= n_iov) <= 0)
1602 done = 1; 1601 done = 1;
1603 index = next; 1602 index = next;
1604 } else 1603 } else
1605 /* Need to re-find the pages we skipped */ 1604 /* Need to re-find the pages we skipped */
1606 index = pvec.pages[0]->index + 1; 1605 index = pvec.pages[0]->index + 1;
1607 1606
1608 pagevec_release(&pvec); 1607 pagevec_release(&pvec);
1609 } 1608 }
1610 if (!scanned && !done) { 1609 if (!scanned && !done) {
1611 /* 1610 /*
1612 * We hit the last page and there is more work to be done: wrap 1611 * We hit the last page and there is more work to be done: wrap
1613 * back to the start of the file 1612 * back to the start of the file
1614 */ 1613 */
1615 scanned = 1; 1614 scanned = 1;
1616 index = 0; 1615 index = 0;
1617 goto retry; 1616 goto retry;
1618 } 1617 }
1619 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 1618 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1620 mapping->writeback_index = index; 1619 mapping->writeback_index = index;
1621 1620
1622 FreeXid(xid); 1621 FreeXid(xid);
1623 kfree(iov); 1622 kfree(iov);
1624 return rc; 1623 return rc;
1625 } 1624 }
1626 1625
1627 static int cifs_writepage(struct page *page, struct writeback_control *wbc) 1626 static int cifs_writepage(struct page *page, struct writeback_control *wbc)
1628 { 1627 {
1629 int rc = -EFAULT; 1628 int rc = -EFAULT;
1630 int xid; 1629 int xid;
1631 1630
1632 xid = GetXid(); 1631 xid = GetXid();
1633 /* BB add check for wbc flags */ 1632 /* BB add check for wbc flags */
1634 page_cache_get(page); 1633 page_cache_get(page);
1635 if (!PageUptodate(page)) 1634 if (!PageUptodate(page))
1636 cFYI(1, ("ppw - page not up to date")); 1635 cFYI(1, ("ppw - page not up to date"));
1637 1636
1638 /* 1637 /*
1639 * Set the "writeback" flag, and clear "dirty" in the radix tree. 1638 * Set the "writeback" flag, and clear "dirty" in the radix tree.
1640 * 1639 *
1641 * A writepage() implementation always needs to do either this, 1640 * A writepage() implementation always needs to do either this,
1642 * or re-dirty the page with "redirty_page_for_writepage()" in 1641 * or re-dirty the page with "redirty_page_for_writepage()" in
1643 * the case of a failure. 1642 * the case of a failure.
1644 * 1643 *
1645 * Just unlocking the page will cause the radix tree tag-bits 1644 * Just unlocking the page will cause the radix tree tag-bits
1646 * to fail to update with the state of the page correctly. 1645 * to fail to update with the state of the page correctly.
1647 */ 1646 */
1648 set_page_writeback(page); 1647 set_page_writeback(page);
1649 rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE); 1648 rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE);
1650 SetPageUptodate(page); /* BB add check for error and Clearuptodate? */ 1649 SetPageUptodate(page); /* BB add check for error and Clearuptodate? */
1651 unlock_page(page); 1650 unlock_page(page);
1652 end_page_writeback(page); 1651 end_page_writeback(page);
1653 page_cache_release(page); 1652 page_cache_release(page);
1654 FreeXid(xid); 1653 FreeXid(xid);
1655 return rc; 1654 return rc;
1656 } 1655 }
1657 1656
1658 static int cifs_write_end(struct file *file, struct address_space *mapping, 1657 static int cifs_write_end(struct file *file, struct address_space *mapping,
1659 loff_t pos, unsigned len, unsigned copied, 1658 loff_t pos, unsigned len, unsigned copied,
1660 struct page *page, void *fsdata) 1659 struct page *page, void *fsdata)
1661 { 1660 {
1662 int rc; 1661 int rc;
1663 struct inode *inode = mapping->host; 1662 struct inode *inode = mapping->host;
1664 1663
1665 cFYI(1, ("write_end for page %p from pos %lld with %d bytes", 1664 cFYI(1, ("write_end for page %p from pos %lld with %d bytes",
1666 page, pos, copied)); 1665 page, pos, copied));
1667 1666
1668 if (PageChecked(page)) { 1667 if (PageChecked(page)) {
1669 if (copied == len) 1668 if (copied == len)
1670 SetPageUptodate(page); 1669 SetPageUptodate(page);
1671 ClearPageChecked(page); 1670 ClearPageChecked(page);
1672 } else if (!PageUptodate(page) && copied == PAGE_CACHE_SIZE) 1671 } else if (!PageUptodate(page) && copied == PAGE_CACHE_SIZE)
1673 SetPageUptodate(page); 1672 SetPageUptodate(page);
1674 1673
1675 if (!PageUptodate(page)) { 1674 if (!PageUptodate(page)) {
1676 char *page_data; 1675 char *page_data;
1677 unsigned offset = pos & (PAGE_CACHE_SIZE - 1); 1676 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1678 int xid; 1677 int xid;
1679 1678
1680 xid = GetXid(); 1679 xid = GetXid();
1681 /* this is probably better than directly calling 1680 /* this is probably better than directly calling
1682 partialpage_write since in this function the file handle is 1681 partialpage_write since in this function the file handle is
1683 known which we might as well leverage */ 1682 known which we might as well leverage */
1684 /* BB check if anything else missing out of ppw 1683 /* BB check if anything else missing out of ppw
1685 such as updating last write time */ 1684 such as updating last write time */
1686 page_data = kmap(page); 1685 page_data = kmap(page);
1687 rc = cifs_write(file, page_data + offset, copied, &pos); 1686 rc = cifs_write(file, page_data + offset, copied, &pos);
1688 /* if (rc < 0) should we set writebehind rc? */ 1687 /* if (rc < 0) should we set writebehind rc? */
1689 kunmap(page); 1688 kunmap(page);
1690 1689
1691 FreeXid(xid); 1690 FreeXid(xid);
1692 } else { 1691 } else {
1693 rc = copied; 1692 rc = copied;
1694 pos += copied; 1693 pos += copied;
1695 set_page_dirty(page); 1694 set_page_dirty(page);
1696 } 1695 }
1697 1696
1698 if (rc > 0) { 1697 if (rc > 0) {
1699 spin_lock(&inode->i_lock); 1698 spin_lock(&inode->i_lock);
1700 if (pos > inode->i_size) 1699 if (pos > inode->i_size)
1701 i_size_write(inode, pos); 1700 i_size_write(inode, pos);
1702 spin_unlock(&inode->i_lock); 1701 spin_unlock(&inode->i_lock);
1703 } 1702 }
1704 1703
1705 unlock_page(page); 1704 unlock_page(page);
1706 page_cache_release(page); 1705 page_cache_release(page);
1707 1706
1708 return rc; 1707 return rc;
1709 } 1708 }
1710 1709
1711 int cifs_fsync(struct file *file, struct dentry *dentry, int datasync) 1710 int cifs_fsync(struct file *file, struct dentry *dentry, int datasync)
1712 { 1711 {
1713 int xid; 1712 int xid;
1714 int rc = 0; 1713 int rc = 0;
1715 struct cifsTconInfo *tcon; 1714 struct cifsTconInfo *tcon;
1716 struct cifsFileInfo *smbfile = 1715 struct cifsFileInfo *smbfile =
1717 (struct cifsFileInfo *)file->private_data; 1716 (struct cifsFileInfo *)file->private_data;
1718 struct inode *inode = file->f_path.dentry->d_inode; 1717 struct inode *inode = file->f_path.dentry->d_inode;
1719 1718
1720 xid = GetXid(); 1719 xid = GetXid();
1721 1720
1722 cFYI(1, ("Sync file - name: %s datasync: 0x%x", 1721 cFYI(1, ("Sync file - name: %s datasync: 0x%x",
1723 dentry->d_name.name, datasync)); 1722 dentry->d_name.name, datasync));
1724 1723
1725 rc = filemap_write_and_wait(inode->i_mapping); 1724 rc = filemap_write_and_wait(inode->i_mapping);
1726 if (rc == 0) { 1725 if (rc == 0) {
1727 rc = CIFS_I(inode)->write_behind_rc; 1726 rc = CIFS_I(inode)->write_behind_rc;
1728 CIFS_I(inode)->write_behind_rc = 0; 1727 CIFS_I(inode)->write_behind_rc = 0;
1729 tcon = CIFS_SB(inode->i_sb)->tcon; 1728 tcon = CIFS_SB(inode->i_sb)->tcon;
1730 if (!rc && tcon && smbfile && 1729 if (!rc && tcon && smbfile &&
1731 !(CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) 1730 !(CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC))
1732 rc = CIFSSMBFlush(xid, tcon, smbfile->netfid); 1731 rc = CIFSSMBFlush(xid, tcon, smbfile->netfid);
1733 } 1732 }
1734 1733
1735 FreeXid(xid); 1734 FreeXid(xid);
1736 return rc; 1735 return rc;
1737 } 1736 }
1738 1737
1739 /* static void cifs_sync_page(struct page *page) 1738 /* static void cifs_sync_page(struct page *page)
1740 { 1739 {
1741 struct address_space *mapping; 1740 struct address_space *mapping;
1742 struct inode *inode; 1741 struct inode *inode;
1743 unsigned long index = page->index; 1742 unsigned long index = page->index;
1744 unsigned int rpages = 0; 1743 unsigned int rpages = 0;
1745 int rc = 0; 1744 int rc = 0;
1746 1745
1747 cFYI(1, ("sync page %p",page)); 1746 cFYI(1, ("sync page %p",page));
1748 mapping = page->mapping; 1747 mapping = page->mapping;
1749 if (!mapping) 1748 if (!mapping)
1750 return 0; 1749 return 0;
1751 inode = mapping->host; 1750 inode = mapping->host;
1752 if (!inode) 1751 if (!inode)
1753 return; */ 1752 return; */
1754 1753
1755 /* fill in rpages then 1754 /* fill in rpages then
1756 result = cifs_pagein_inode(inode, index, rpages); */ /* BB finish */ 1755 result = cifs_pagein_inode(inode, index, rpages); */ /* BB finish */
1757 1756
1758 /* cFYI(1, ("rpages is %d for sync page of Index %ld", rpages, index)); 1757 /* cFYI(1, ("rpages is %d for sync page of Index %ld", rpages, index));
1759 1758
1760 #if 0 1759 #if 0
1761 if (rc < 0) 1760 if (rc < 0)
1762 return rc; 1761 return rc;
1763 return 0; 1762 return 0;
1764 #endif 1763 #endif
1765 } */ 1764 } */
1766 1765
1767 /* 1766 /*
1768 * As file closes, flush all cached write data for this inode checking 1767 * As file closes, flush all cached write data for this inode checking
1769 * for write behind errors. 1768 * for write behind errors.
1770 */ 1769 */
1771 int cifs_flush(struct file *file, fl_owner_t id) 1770 int cifs_flush(struct file *file, fl_owner_t id)
1772 { 1771 {
1773 struct inode *inode = file->f_path.dentry->d_inode; 1772 struct inode *inode = file->f_path.dentry->d_inode;
1774 int rc = 0; 1773 int rc = 0;
1775 1774
1776 /* Rather than do the steps manually: 1775 /* Rather than do the steps manually:
1777 lock the inode for writing 1776 lock the inode for writing
1778 loop through pages looking for write behind data (dirty pages) 1777 loop through pages looking for write behind data (dirty pages)
1779 coalesce into contiguous 16K (or smaller) chunks to write to server 1778 coalesce into contiguous 16K (or smaller) chunks to write to server
1780 send to server (prefer in parallel) 1779 send to server (prefer in parallel)
1781 deal with writebehind errors 1780 deal with writebehind errors
1782 unlock inode for writing 1781 unlock inode for writing
1783 filemapfdatawrite appears easier for the time being */ 1782 filemapfdatawrite appears easier for the time being */
1784 1783
1785 rc = filemap_fdatawrite(inode->i_mapping); 1784 rc = filemap_fdatawrite(inode->i_mapping);
1786 /* reset wb rc if we were able to write out dirty pages */ 1785 /* reset wb rc if we were able to write out dirty pages */
1787 if (!rc) { 1786 if (!rc) {
1788 rc = CIFS_I(inode)->write_behind_rc; 1787 rc = CIFS_I(inode)->write_behind_rc;
1789 CIFS_I(inode)->write_behind_rc = 0; 1788 CIFS_I(inode)->write_behind_rc = 0;
1790 } 1789 }
1791 1790
1792 cFYI(1, ("Flush inode %p file %p rc %d", inode, file, rc)); 1791 cFYI(1, ("Flush inode %p file %p rc %d", inode, file, rc));
1793 1792
1794 return rc; 1793 return rc;
1795 } 1794 }
1796 1795
1797 ssize_t cifs_user_read(struct file *file, char __user *read_data, 1796 ssize_t cifs_user_read(struct file *file, char __user *read_data,
1798 size_t read_size, loff_t *poffset) 1797 size_t read_size, loff_t *poffset)
1799 { 1798 {
1800 int rc = -EACCES; 1799 int rc = -EACCES;
1801 unsigned int bytes_read = 0; 1800 unsigned int bytes_read = 0;
1802 unsigned int total_read = 0; 1801 unsigned int total_read = 0;
1803 unsigned int current_read_size; 1802 unsigned int current_read_size;
1804 struct cifs_sb_info *cifs_sb; 1803 struct cifs_sb_info *cifs_sb;
1805 struct cifsTconInfo *pTcon; 1804 struct cifsTconInfo *pTcon;
1806 int xid; 1805 int xid;
1807 struct cifsFileInfo *open_file; 1806 struct cifsFileInfo *open_file;
1808 char *smb_read_data; 1807 char *smb_read_data;
1809 char __user *current_offset; 1808 char __user *current_offset;
1810 struct smb_com_read_rsp *pSMBr; 1809 struct smb_com_read_rsp *pSMBr;
1811 1810
1812 xid = GetXid(); 1811 xid = GetXid();
1813 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); 1812 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1814 pTcon = cifs_sb->tcon; 1813 pTcon = cifs_sb->tcon;
1815 1814
1816 if (file->private_data == NULL) { 1815 if (file->private_data == NULL) {
1817 FreeXid(xid); 1816 FreeXid(xid);
1818 return -EBADF; 1817 return -EBADF;
1819 } 1818 }
1820 open_file = (struct cifsFileInfo *)file->private_data; 1819 open_file = (struct cifsFileInfo *)file->private_data;
1821 1820
1822 if ((file->f_flags & O_ACCMODE) == O_WRONLY) 1821 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
1823 cFYI(1, ("attempting read on write only file instance")); 1822 cFYI(1, ("attempting read on write only file instance"));
1824 1823
1825 for (total_read = 0, current_offset = read_data; 1824 for (total_read = 0, current_offset = read_data;
1826 read_size > total_read; 1825 read_size > total_read;
1827 total_read += bytes_read, current_offset += bytes_read) { 1826 total_read += bytes_read, current_offset += bytes_read) {
1828 current_read_size = min_t(const int, read_size - total_read, 1827 current_read_size = min_t(const int, read_size - total_read,
1829 cifs_sb->rsize); 1828 cifs_sb->rsize);
1830 rc = -EAGAIN; 1829 rc = -EAGAIN;
1831 smb_read_data = NULL; 1830 smb_read_data = NULL;
1832 while (rc == -EAGAIN) { 1831 while (rc == -EAGAIN) {
1833 int buf_type = CIFS_NO_BUFFER; 1832 int buf_type = CIFS_NO_BUFFER;
1834 if ((open_file->invalidHandle) && 1833 if ((open_file->invalidHandle) &&
1835 (!open_file->closePend)) { 1834 (!open_file->closePend)) {
1836 rc = cifs_reopen_file(file, true); 1835 rc = cifs_reopen_file(file, true);
1837 if (rc != 0) 1836 if (rc != 0)
1838 break; 1837 break;
1839 } 1838 }
1840 rc = CIFSSMBRead(xid, pTcon, 1839 rc = CIFSSMBRead(xid, pTcon,
1841 open_file->netfid, 1840 open_file->netfid,
1842 current_read_size, *poffset, 1841 current_read_size, *poffset,
1843 &bytes_read, &smb_read_data, 1842 &bytes_read, &smb_read_data,
1844 &buf_type); 1843 &buf_type);
1845 pSMBr = (struct smb_com_read_rsp *)smb_read_data; 1844 pSMBr = (struct smb_com_read_rsp *)smb_read_data;
1846 if (smb_read_data) { 1845 if (smb_read_data) {
1847 if (copy_to_user(current_offset, 1846 if (copy_to_user(current_offset,
1848 smb_read_data + 1847 smb_read_data +
1849 4 /* RFC1001 length field */ + 1848 4 /* RFC1001 length field */ +
1850 le16_to_cpu(pSMBr->DataOffset), 1849 le16_to_cpu(pSMBr->DataOffset),
1851 bytes_read)) 1850 bytes_read))
1852 rc = -EFAULT; 1851 rc = -EFAULT;
1853 1852
1854 if (buf_type == CIFS_SMALL_BUFFER) 1853 if (buf_type == CIFS_SMALL_BUFFER)
1855 cifs_small_buf_release(smb_read_data); 1854 cifs_small_buf_release(smb_read_data);
1856 else if (buf_type == CIFS_LARGE_BUFFER) 1855 else if (buf_type == CIFS_LARGE_BUFFER)
1857 cifs_buf_release(smb_read_data); 1856 cifs_buf_release(smb_read_data);
1858 smb_read_data = NULL; 1857 smb_read_data = NULL;
1859 } 1858 }
1860 } 1859 }
1861 if (rc || (bytes_read == 0)) { 1860 if (rc || (bytes_read == 0)) {
1862 if (total_read) { 1861 if (total_read) {
1863 break; 1862 break;
1864 } else { 1863 } else {
1865 FreeXid(xid); 1864 FreeXid(xid);
1866 return rc; 1865 return rc;
1867 } 1866 }
1868 } else { 1867 } else {
1869 cifs_stats_bytes_read(pTcon, bytes_read); 1868 cifs_stats_bytes_read(pTcon, bytes_read);
1870 *poffset += bytes_read; 1869 *poffset += bytes_read;
1871 } 1870 }
1872 } 1871 }
1873 FreeXid(xid); 1872 FreeXid(xid);
1874 return total_read; 1873 return total_read;
1875 } 1874 }
1876 1875
1877 1876
1878 static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size, 1877 static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
1879 loff_t *poffset) 1878 loff_t *poffset)
1880 { 1879 {
1881 int rc = -EACCES; 1880 int rc = -EACCES;
1882 unsigned int bytes_read = 0; 1881 unsigned int bytes_read = 0;
1883 unsigned int total_read; 1882 unsigned int total_read;
1884 unsigned int current_read_size; 1883 unsigned int current_read_size;
1885 struct cifs_sb_info *cifs_sb; 1884 struct cifs_sb_info *cifs_sb;
1886 struct cifsTconInfo *pTcon; 1885 struct cifsTconInfo *pTcon;
1887 int xid; 1886 int xid;
1888 char *current_offset; 1887 char *current_offset;
1889 struct cifsFileInfo *open_file; 1888 struct cifsFileInfo *open_file;
1890 int buf_type = CIFS_NO_BUFFER; 1889 int buf_type = CIFS_NO_BUFFER;
1891 1890
1892 xid = GetXid(); 1891 xid = GetXid();
1893 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); 1892 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1894 pTcon = cifs_sb->tcon; 1893 pTcon = cifs_sb->tcon;
1895 1894
1896 if (file->private_data == NULL) { 1895 if (file->private_data == NULL) {
1897 FreeXid(xid); 1896 FreeXid(xid);
1898 return -EBADF; 1897 return -EBADF;
1899 } 1898 }
1900 open_file = (struct cifsFileInfo *)file->private_data; 1899 open_file = (struct cifsFileInfo *)file->private_data;
1901 1900
1902 if ((file->f_flags & O_ACCMODE) == O_WRONLY) 1901 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
1903 cFYI(1, ("attempting read on write only file instance")); 1902 cFYI(1, ("attempting read on write only file instance"));
1904 1903
1905 for (total_read = 0, current_offset = read_data; 1904 for (total_read = 0, current_offset = read_data;
1906 read_size > total_read; 1905 read_size > total_read;
1907 total_read += bytes_read, current_offset += bytes_read) { 1906 total_read += bytes_read, current_offset += bytes_read) {
1908 current_read_size = min_t(const int, read_size - total_read, 1907 current_read_size = min_t(const int, read_size - total_read,
1909 cifs_sb->rsize); 1908 cifs_sb->rsize);
1910 /* For windows me and 9x we do not want to request more 1909 /* For windows me and 9x we do not want to request more
1911 than it negotiated since it will refuse the read then */ 1910 than it negotiated since it will refuse the read then */
1912 if ((pTcon->ses) && 1911 if ((pTcon->ses) &&
1913 !(pTcon->ses->capabilities & CAP_LARGE_FILES)) { 1912 !(pTcon->ses->capabilities & CAP_LARGE_FILES)) {
1914 current_read_size = min_t(const int, current_read_size, 1913 current_read_size = min_t(const int, current_read_size,
1915 pTcon->ses->server->maxBuf - 128); 1914 pTcon->ses->server->maxBuf - 128);
1916 } 1915 }
1917 rc = -EAGAIN; 1916 rc = -EAGAIN;
1918 while (rc == -EAGAIN) { 1917 while (rc == -EAGAIN) {
1919 if ((open_file->invalidHandle) && 1918 if ((open_file->invalidHandle) &&
1920 (!open_file->closePend)) { 1919 (!open_file->closePend)) {
1921 rc = cifs_reopen_file(file, true); 1920 rc = cifs_reopen_file(file, true);
1922 if (rc != 0) 1921 if (rc != 0)
1923 break; 1922 break;
1924 } 1923 }
1925 rc = CIFSSMBRead(xid, pTcon, 1924 rc = CIFSSMBRead(xid, pTcon,
1926 open_file->netfid, 1925 open_file->netfid,
1927 current_read_size, *poffset, 1926 current_read_size, *poffset,
1928 &bytes_read, &current_offset, 1927 &bytes_read, &current_offset,
1929 &buf_type); 1928 &buf_type);
1930 } 1929 }
1931 if (rc || (bytes_read == 0)) { 1930 if (rc || (bytes_read == 0)) {
1932 if (total_read) { 1931 if (total_read) {
1933 break; 1932 break;
1934 } else { 1933 } else {
1935 FreeXid(xid); 1934 FreeXid(xid);
1936 return rc; 1935 return rc;
1937 } 1936 }
1938 } else { 1937 } else {
1939 cifs_stats_bytes_read(pTcon, total_read); 1938 cifs_stats_bytes_read(pTcon, total_read);
1940 *poffset += bytes_read; 1939 *poffset += bytes_read;
1941 } 1940 }
1942 } 1941 }
1943 FreeXid(xid); 1942 FreeXid(xid);
1944 return total_read; 1943 return total_read;
1945 } 1944 }
1946 1945
1947 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma) 1946 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
1948 { 1947 {
1949 struct dentry *dentry = file->f_path.dentry; 1948 struct dentry *dentry = file->f_path.dentry;
1950 int rc, xid; 1949 int rc, xid;
1951 1950
1952 xid = GetXid(); 1951 xid = GetXid();
1953 rc = cifs_revalidate(dentry); 1952 rc = cifs_revalidate(dentry);
1954 if (rc) { 1953 if (rc) {
1955 cFYI(1, ("Validation prior to mmap failed, error=%d", rc)); 1954 cFYI(1, ("Validation prior to mmap failed, error=%d", rc));
1956 FreeXid(xid); 1955 FreeXid(xid);
1957 return rc; 1956 return rc;
1958 } 1957 }
1959 rc = generic_file_mmap(file, vma); 1958 rc = generic_file_mmap(file, vma);
1960 FreeXid(xid); 1959 FreeXid(xid);
1961 return rc; 1960 return rc;
1962 } 1961 }
1963 1962
1964 1963
1965 static void cifs_copy_cache_pages(struct address_space *mapping, 1964 static void cifs_copy_cache_pages(struct address_space *mapping,
1966 struct list_head *pages, int bytes_read, char *data, 1965 struct list_head *pages, int bytes_read, char *data,
1967 struct pagevec *plru_pvec) 1966 struct pagevec *plru_pvec)
1968 { 1967 {
1969 struct page *page; 1968 struct page *page;
1970 char *target; 1969 char *target;
1971 1970
1972 while (bytes_read > 0) { 1971 while (bytes_read > 0) {
1973 if (list_empty(pages)) 1972 if (list_empty(pages))
1974 break; 1973 break;
1975 1974
1976 page = list_entry(pages->prev, struct page, lru); 1975 page = list_entry(pages->prev, struct page, lru);
1977 list_del(&page->lru); 1976 list_del(&page->lru);
1978 1977
1979 if (add_to_page_cache(page, mapping, page->index, 1978 if (add_to_page_cache(page, mapping, page->index,
1980 GFP_KERNEL)) { 1979 GFP_KERNEL)) {
1981 page_cache_release(page); 1980 page_cache_release(page);
1982 cFYI(1, ("Add page cache failed")); 1981 cFYI(1, ("Add page cache failed"));
1983 data += PAGE_CACHE_SIZE; 1982 data += PAGE_CACHE_SIZE;
1984 bytes_read -= PAGE_CACHE_SIZE; 1983 bytes_read -= PAGE_CACHE_SIZE;
1985 continue; 1984 continue;
1986 } 1985 }
1987 1986
1988 target = kmap_atomic(page, KM_USER0); 1987 target = kmap_atomic(page, KM_USER0);
1989 1988
1990 if (PAGE_CACHE_SIZE > bytes_read) { 1989 if (PAGE_CACHE_SIZE > bytes_read) {
1991 memcpy(target, data, bytes_read); 1990 memcpy(target, data, bytes_read);
1992 /* zero the tail end of this partial page */ 1991 /* zero the tail end of this partial page */
1993 memset(target + bytes_read, 0, 1992 memset(target + bytes_read, 0,
1994 PAGE_CACHE_SIZE - bytes_read); 1993 PAGE_CACHE_SIZE - bytes_read);
1995 bytes_read = 0; 1994 bytes_read = 0;
1996 } else { 1995 } else {
1997 memcpy(target, data, PAGE_CACHE_SIZE); 1996 memcpy(target, data, PAGE_CACHE_SIZE);
1998 bytes_read -= PAGE_CACHE_SIZE; 1997 bytes_read -= PAGE_CACHE_SIZE;
1999 } 1998 }
2000 kunmap_atomic(target, KM_USER0); 1999 kunmap_atomic(target, KM_USER0);
2001 2000
2002 flush_dcache_page(page); 2001 flush_dcache_page(page);
2003 SetPageUptodate(page); 2002 SetPageUptodate(page);
2004 unlock_page(page); 2003 unlock_page(page);
2005 if (!pagevec_add(plru_pvec, page)) 2004 if (!pagevec_add(plru_pvec, page))
2006 __pagevec_lru_add_file(plru_pvec); 2005 __pagevec_lru_add_file(plru_pvec);
2007 data += PAGE_CACHE_SIZE; 2006 data += PAGE_CACHE_SIZE;
2008 } 2007 }
2009 return; 2008 return;
2010 } 2009 }
2011 2010
2012 static int cifs_readpages(struct file *file, struct address_space *mapping, 2011 static int cifs_readpages(struct file *file, struct address_space *mapping,
2013 struct list_head *page_list, unsigned num_pages) 2012 struct list_head *page_list, unsigned num_pages)
2014 { 2013 {
2015 int rc = -EACCES; 2014 int rc = -EACCES;
2016 int xid; 2015 int xid;
2017 loff_t offset; 2016 loff_t offset;
2018 struct page *page; 2017 struct page *page;
2019 struct cifs_sb_info *cifs_sb; 2018 struct cifs_sb_info *cifs_sb;
2020 struct cifsTconInfo *pTcon; 2019 struct cifsTconInfo *pTcon;
2021 unsigned int bytes_read = 0; 2020 unsigned int bytes_read = 0;
2022 unsigned int read_size, i; 2021 unsigned int read_size, i;
2023 char *smb_read_data = NULL; 2022 char *smb_read_data = NULL;
2024 struct smb_com_read_rsp *pSMBr; 2023 struct smb_com_read_rsp *pSMBr;
2025 struct pagevec lru_pvec; 2024 struct pagevec lru_pvec;
2026 struct cifsFileInfo *open_file; 2025 struct cifsFileInfo *open_file;
2027 int buf_type = CIFS_NO_BUFFER; 2026 int buf_type = CIFS_NO_BUFFER;
2028 2027
2029 xid = GetXid(); 2028 xid = GetXid();
2030 if (file->private_data == NULL) { 2029 if (file->private_data == NULL) {
2031 FreeXid(xid); 2030 FreeXid(xid);
2032 return -EBADF; 2031 return -EBADF;
2033 } 2032 }
2034 open_file = (struct cifsFileInfo *)file->private_data; 2033 open_file = (struct cifsFileInfo *)file->private_data;
2035 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); 2034 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
2036 pTcon = cifs_sb->tcon; 2035 pTcon = cifs_sb->tcon;
2037 2036
2038 pagevec_init(&lru_pvec, 0); 2037 pagevec_init(&lru_pvec, 0);
2039 cFYI(DBG2, ("rpages: num pages %d", num_pages)); 2038 cFYI(DBG2, ("rpages: num pages %d", num_pages));
2040 for (i = 0; i < num_pages; ) { 2039 for (i = 0; i < num_pages; ) {
2041 unsigned contig_pages; 2040 unsigned contig_pages;
2042 struct page *tmp_page; 2041 struct page *tmp_page;
2043 unsigned long expected_index; 2042 unsigned long expected_index;
2044 2043
2045 if (list_empty(page_list)) 2044 if (list_empty(page_list))
2046 break; 2045 break;
2047 2046
2048 page = list_entry(page_list->prev, struct page, lru); 2047 page = list_entry(page_list->prev, struct page, lru);
2049 offset = (loff_t)page->index << PAGE_CACHE_SHIFT; 2048 offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
2050 2049
2051 /* count adjacent pages that we will read into */ 2050 /* count adjacent pages that we will read into */
2052 contig_pages = 0; 2051 contig_pages = 0;
2053 expected_index = 2052 expected_index =
2054 list_entry(page_list->prev, struct page, lru)->index; 2053 list_entry(page_list->prev, struct page, lru)->index;
2055 list_for_each_entry_reverse(tmp_page, page_list, lru) { 2054 list_for_each_entry_reverse(tmp_page, page_list, lru) {
2056 if (tmp_page->index == expected_index) { 2055 if (tmp_page->index == expected_index) {
2057 contig_pages++; 2056 contig_pages++;
2058 expected_index++; 2057 expected_index++;
2059 } else 2058 } else
2060 break; 2059 break;
2061 } 2060 }
2062 if (contig_pages + i > num_pages) 2061 if (contig_pages + i > num_pages)
2063 contig_pages = num_pages - i; 2062 contig_pages = num_pages - i;
2064 2063
2065 /* for reads over a certain size could initiate async 2064 /* for reads over a certain size could initiate async
2066 read ahead */ 2065 read ahead */
2067 2066
2068 read_size = contig_pages * PAGE_CACHE_SIZE; 2067 read_size = contig_pages * PAGE_CACHE_SIZE;
2069 /* Read size needs to be in multiples of one page */ 2068 /* Read size needs to be in multiples of one page */
2070 read_size = min_t(const unsigned int, read_size, 2069 read_size = min_t(const unsigned int, read_size,
2071 cifs_sb->rsize & PAGE_CACHE_MASK); 2070 cifs_sb->rsize & PAGE_CACHE_MASK);
2072 cFYI(DBG2, ("rpages: read size 0x%x contiguous pages %d", 2071 cFYI(DBG2, ("rpages: read size 0x%x contiguous pages %d",
2073 read_size, contig_pages)); 2072 read_size, contig_pages));
2074 rc = -EAGAIN; 2073 rc = -EAGAIN;
2075 while (rc == -EAGAIN) { 2074 while (rc == -EAGAIN) {
2076 if ((open_file->invalidHandle) && 2075 if ((open_file->invalidHandle) &&
2077 (!open_file->closePend)) { 2076 (!open_file->closePend)) {
2078 rc = cifs_reopen_file(file, true); 2077 rc = cifs_reopen_file(file, true);
2079 if (rc != 0) 2078 if (rc != 0)
2080 break; 2079 break;
2081 } 2080 }
2082 2081
2083 rc = CIFSSMBRead(xid, pTcon, 2082 rc = CIFSSMBRead(xid, pTcon,
2084 open_file->netfid, 2083 open_file->netfid,
2085 read_size, offset, 2084 read_size, offset,
2086 &bytes_read, &smb_read_data, 2085 &bytes_read, &smb_read_data,
2087 &buf_type); 2086 &buf_type);
2088 /* BB more RC checks ? */ 2087 /* BB more RC checks ? */
2089 if (rc == -EAGAIN) { 2088 if (rc == -EAGAIN) {
2090 if (smb_read_data) { 2089 if (smb_read_data) {
2091 if (buf_type == CIFS_SMALL_BUFFER) 2090 if (buf_type == CIFS_SMALL_BUFFER)
2092 cifs_small_buf_release(smb_read_data); 2091 cifs_small_buf_release(smb_read_data);
2093 else if (buf_type == CIFS_LARGE_BUFFER) 2092 else if (buf_type == CIFS_LARGE_BUFFER)
2094 cifs_buf_release(smb_read_data); 2093 cifs_buf_release(smb_read_data);
2095 smb_read_data = NULL; 2094 smb_read_data = NULL;
2096 } 2095 }
2097 } 2096 }
2098 } 2097 }
2099 if ((rc < 0) || (smb_read_data == NULL)) { 2098 if ((rc < 0) || (smb_read_data == NULL)) {
2100 cFYI(1, ("Read error in readpages: %d", rc)); 2099 cFYI(1, ("Read error in readpages: %d", rc));
2101 break; 2100 break;
2102 } else if (bytes_read > 0) { 2101 } else if (bytes_read > 0) {
2103 task_io_account_read(bytes_read); 2102 task_io_account_read(bytes_read);
2104 pSMBr = (struct smb_com_read_rsp *)smb_read_data; 2103 pSMBr = (struct smb_com_read_rsp *)smb_read_data;
2105 cifs_copy_cache_pages(mapping, page_list, bytes_read, 2104 cifs_copy_cache_pages(mapping, page_list, bytes_read,
2106 smb_read_data + 4 /* RFC1001 hdr */ + 2105 smb_read_data + 4 /* RFC1001 hdr */ +
2107 le16_to_cpu(pSMBr->DataOffset), &lru_pvec); 2106 le16_to_cpu(pSMBr->DataOffset), &lru_pvec);
2108 2107
2109 i += bytes_read >> PAGE_CACHE_SHIFT; 2108 i += bytes_read >> PAGE_CACHE_SHIFT;
2110 cifs_stats_bytes_read(pTcon, bytes_read); 2109 cifs_stats_bytes_read(pTcon, bytes_read);
2111 if ((bytes_read & PAGE_CACHE_MASK) != bytes_read) { 2110 if ((bytes_read & PAGE_CACHE_MASK) != bytes_read) {
2112 i++; /* account for partial page */ 2111 i++; /* account for partial page */
2113 2112
2114 /* server copy of file can have smaller size 2113 /* server copy of file can have smaller size
2115 than client */ 2114 than client */
2116 /* BB do we need to verify this common case ? 2115 /* BB do we need to verify this common case ?
2117 this case is ok - if we are at server EOF 2116 this case is ok - if we are at server EOF
2118 we will hit it on next read */ 2117 we will hit it on next read */
2119 2118
2120 /* break; */ 2119 /* break; */
2121 } 2120 }
2122 } else { 2121 } else {
2123 cFYI(1, ("No bytes read (%d) at offset %lld . " 2122 cFYI(1, ("No bytes read (%d) at offset %lld . "
2124 "Cleaning remaining pages from readahead list", 2123 "Cleaning remaining pages from readahead list",
2125 bytes_read, offset)); 2124 bytes_read, offset));
2126 /* BB turn off caching and do new lookup on 2125 /* BB turn off caching and do new lookup on
2127 file size at server? */ 2126 file size at server? */
2128 break; 2127 break;
2129 } 2128 }
2130 if (smb_read_data) { 2129 if (smb_read_data) {
2131 if (buf_type == CIFS_SMALL_BUFFER) 2130 if (buf_type == CIFS_SMALL_BUFFER)
2132 cifs_small_buf_release(smb_read_data); 2131 cifs_small_buf_release(smb_read_data);
2133 else if (buf_type == CIFS_LARGE_BUFFER) 2132 else if (buf_type == CIFS_LARGE_BUFFER)
2134 cifs_buf_release(smb_read_data); 2133 cifs_buf_release(smb_read_data);
2135 smb_read_data = NULL; 2134 smb_read_data = NULL;
2136 } 2135 }
2137 bytes_read = 0; 2136 bytes_read = 0;
2138 } 2137 }
2139 2138
2140 pagevec_lru_add_file(&lru_pvec); 2139 pagevec_lru_add_file(&lru_pvec);
2141 2140
2142 /* need to free smb_read_data buf before exit */ 2141 /* need to free smb_read_data buf before exit */
2143 if (smb_read_data) { 2142 if (smb_read_data) {
2144 if (buf_type == CIFS_SMALL_BUFFER) 2143 if (buf_type == CIFS_SMALL_BUFFER)
2145 cifs_small_buf_release(smb_read_data); 2144 cifs_small_buf_release(smb_read_data);
2146 else if (buf_type == CIFS_LARGE_BUFFER) 2145 else if (buf_type == CIFS_LARGE_BUFFER)
2147 cifs_buf_release(smb_read_data); 2146 cifs_buf_release(smb_read_data);
2148 smb_read_data = NULL; 2147 smb_read_data = NULL;
2149 } 2148 }
2150 2149
2151 FreeXid(xid); 2150 FreeXid(xid);
2152 return rc; 2151 return rc;
2153 } 2152 }
2154 2153
2155 static int cifs_readpage_worker(struct file *file, struct page *page, 2154 static int cifs_readpage_worker(struct file *file, struct page *page,
2156 loff_t *poffset) 2155 loff_t *poffset)
2157 { 2156 {
2158 char *read_data; 2157 char *read_data;
2159 int rc; 2158 int rc;
2160 2159
2161 page_cache_get(page); 2160 page_cache_get(page);
2162 read_data = kmap(page); 2161 read_data = kmap(page);
2163 /* for reads over a certain size could initiate async read ahead */ 2162 /* for reads over a certain size could initiate async read ahead */
2164 2163
2165 rc = cifs_read(file, read_data, PAGE_CACHE_SIZE, poffset); 2164 rc = cifs_read(file, read_data, PAGE_CACHE_SIZE, poffset);
2166 2165
2167 if (rc < 0) 2166 if (rc < 0)
2168 goto io_error; 2167 goto io_error;
2169 else 2168 else
2170 cFYI(1, ("Bytes read %d", rc)); 2169 cFYI(1, ("Bytes read %d", rc));
2171 2170
2172 file->f_path.dentry->d_inode->i_atime = 2171 file->f_path.dentry->d_inode->i_atime =
2173 current_fs_time(file->f_path.dentry->d_inode->i_sb); 2172 current_fs_time(file->f_path.dentry->d_inode->i_sb);
2174 2173
2175 if (PAGE_CACHE_SIZE > rc) 2174 if (PAGE_CACHE_SIZE > rc)
2176 memset(read_data + rc, 0, PAGE_CACHE_SIZE - rc); 2175 memset(read_data + rc, 0, PAGE_CACHE_SIZE - rc);
2177 2176
2178 flush_dcache_page(page); 2177 flush_dcache_page(page);
2179 SetPageUptodate(page); 2178 SetPageUptodate(page);
2180 rc = 0; 2179 rc = 0;
2181 2180
2182 io_error: 2181 io_error:
2183 kunmap(page); 2182 kunmap(page);
2184 page_cache_release(page); 2183 page_cache_release(page);
2185 return rc; 2184 return rc;
2186 } 2185 }
2187 2186
2188 static int cifs_readpage(struct file *file, struct page *page) 2187 static int cifs_readpage(struct file *file, struct page *page)
2189 { 2188 {
2190 loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT; 2189 loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
2191 int rc = -EACCES; 2190 int rc = -EACCES;
2192 int xid; 2191 int xid;
2193 2192
2194 xid = GetXid(); 2193 xid = GetXid();
2195 2194
2196 if (file->private_data == NULL) { 2195 if (file->private_data == NULL) {
2197 FreeXid(xid); 2196 FreeXid(xid);
2198 return -EBADF; 2197 return -EBADF;
2199 } 2198 }
2200 2199
2201 cFYI(1, ("readpage %p at offset %d 0x%x\n", 2200 cFYI(1, ("readpage %p at offset %d 0x%x\n",
2202 page, (int)offset, (int)offset)); 2201 page, (int)offset, (int)offset));
2203 2202
2204 rc = cifs_readpage_worker(file, page, &offset); 2203 rc = cifs_readpage_worker(file, page, &offset);
2205 2204
2206 unlock_page(page); 2205 unlock_page(page);
2207 2206
2208 FreeXid(xid); 2207 FreeXid(xid);
2209 return rc; 2208 return rc;
2210 } 2209 }
2211 2210
2212 static int is_inode_writable(struct cifsInodeInfo *cifs_inode) 2211 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
2213 { 2212 {
2214 struct cifsFileInfo *open_file; 2213 struct cifsFileInfo *open_file;
2215 2214
2216 read_lock(&GlobalSMBSeslock); 2215 read_lock(&GlobalSMBSeslock);
2217 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 2216 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2218 if (open_file->closePend) 2217 if (open_file->closePend)
2219 continue; 2218 continue;
2220 if (open_file->pfile && 2219 if (open_file->pfile &&
2221 ((open_file->pfile->f_flags & O_RDWR) || 2220 ((open_file->pfile->f_flags & O_RDWR) ||
2222 (open_file->pfile->f_flags & O_WRONLY))) { 2221 (open_file->pfile->f_flags & O_WRONLY))) {
2223 read_unlock(&GlobalSMBSeslock); 2222 read_unlock(&GlobalSMBSeslock);
2224 return 1; 2223 return 1;
2225 } 2224 }
2226 } 2225 }
2227 read_unlock(&GlobalSMBSeslock); 2226 read_unlock(&GlobalSMBSeslock);
2228 return 0; 2227 return 0;
2229 } 2228 }
2230 2229
2231 /* We do not want to update the file size from server for inodes 2230 /* We do not want to update the file size from server for inodes
2232 open for write - to avoid races with writepage extending 2231 open for write - to avoid races with writepage extending
2233 the file - in the future we could consider allowing 2232 the file - in the future we could consider allowing
2234 refreshing the inode only on increases in the file size 2233 refreshing the inode only on increases in the file size
2235 but this is tricky to do without racing with writebehind 2234 but this is tricky to do without racing with writebehind
2236 page caching in the current Linux kernel design */ 2235 page caching in the current Linux kernel design */
2237 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file) 2236 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
2238 { 2237 {
2239 if (!cifsInode) 2238 if (!cifsInode)
2240 return true; 2239 return true;
2241 2240
2242 if (is_inode_writable(cifsInode)) { 2241 if (is_inode_writable(cifsInode)) {
2243 /* This inode is open for write at least once */ 2242 /* This inode is open for write at least once */
2244 struct cifs_sb_info *cifs_sb; 2243 struct cifs_sb_info *cifs_sb;
2245 2244
2246 cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb); 2245 cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
2247 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { 2246 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
2248 /* since no page cache to corrupt on directio 2247 /* since no page cache to corrupt on directio
2249 we can change size safely */ 2248 we can change size safely */
2250 return true; 2249 return true;
2251 } 2250 }
2252 2251
2253 if (i_size_read(&cifsInode->vfs_inode) < end_of_file) 2252 if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
2254 return true; 2253 return true;
2255 2254
2256 return false; 2255 return false;
2257 } else 2256 } else
2258 return true; 2257 return true;
2259 } 2258 }
2260 2259
2261 static int cifs_write_begin(struct file *file, struct address_space *mapping, 2260 static int cifs_write_begin(struct file *file, struct address_space *mapping,
2262 loff_t pos, unsigned len, unsigned flags, 2261 loff_t pos, unsigned len, unsigned flags,
2263 struct page **pagep, void **fsdata) 2262 struct page **pagep, void **fsdata)
2264 { 2263 {
2265 pgoff_t index = pos >> PAGE_CACHE_SHIFT; 2264 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2266 loff_t offset = pos & (PAGE_CACHE_SIZE - 1); 2265 loff_t offset = pos & (PAGE_CACHE_SIZE - 1);
2267 loff_t page_start = pos & PAGE_MASK; 2266 loff_t page_start = pos & PAGE_MASK;
2268 loff_t i_size; 2267 loff_t i_size;
2269 struct page *page; 2268 struct page *page;
2270 int rc = 0; 2269 int rc = 0;
2271 2270
2272 cFYI(1, ("write_begin from %lld len %d", (long long)pos, len)); 2271 cFYI(1, ("write_begin from %lld len %d", (long long)pos, len));
2273 2272
2274 page = grab_cache_page_write_begin(mapping, index, flags); 2273 page = grab_cache_page_write_begin(mapping, index, flags);
2275 if (!page) { 2274 if (!page) {
2276 rc = -ENOMEM; 2275 rc = -ENOMEM;
2277 goto out; 2276 goto out;
2278 } 2277 }
2279 2278
2280 if (PageUptodate(page)) 2279 if (PageUptodate(page))
2281 goto out; 2280 goto out;
2282 2281
2283 /* 2282 /*
2284 * If we write a full page it will be up to date, no need to read from 2283 * If we write a full page it will be up to date, no need to read from
2285 * the server. If the write is short, we'll end up doing a sync write 2284 * the server. If the write is short, we'll end up doing a sync write
2286 * instead. 2285 * instead.
2287 */ 2286 */
2288 if (len == PAGE_CACHE_SIZE) 2287 if (len == PAGE_CACHE_SIZE)
2289 goto out; 2288 goto out;
2290 2289
2291 /* 2290 /*
2292 * optimize away the read when we have an oplock, and we're not 2291 * optimize away the read when we have an oplock, and we're not
2293 * expecting to use any of the data we'd be reading in. That 2292 * expecting to use any of the data we'd be reading in. That
2294 * is, when the page lies beyond the EOF, or straddles the EOF 2293 * is, when the page lies beyond the EOF, or straddles the EOF
2295 * and the write will cover all of the existing data. 2294 * and the write will cover all of the existing data.
2296 */ 2295 */
2297 if (CIFS_I(mapping->host)->clientCanCacheRead) { 2296 if (CIFS_I(mapping->host)->clientCanCacheRead) {
2298 i_size = i_size_read(mapping->host); 2297 i_size = i_size_read(mapping->host);
2299 if (page_start >= i_size || 2298 if (page_start >= i_size ||
2300 (offset == 0 && (pos + len) >= i_size)) { 2299 (offset == 0 && (pos + len) >= i_size)) {
2301 zero_user_segments(page, 0, offset, 2300 zero_user_segments(page, 0, offset,
2302 offset + len, 2301 offset + len,
2303 PAGE_CACHE_SIZE); 2302 PAGE_CACHE_SIZE);
2304 /* 2303 /*
2305 * PageChecked means that the parts of the page 2304 * PageChecked means that the parts of the page
2306 * to which we're not writing are considered up 2305 * to which we're not writing are considered up
2307 * to date. Once the data is copied to the 2306 * to date. Once the data is copied to the
2308 * page, it can be set uptodate. 2307 * page, it can be set uptodate.
2309 */ 2308 */
2310 SetPageChecked(page); 2309 SetPageChecked(page);
2311 goto out; 2310 goto out;
2312 } 2311 }
2313 } 2312 }
2314 2313
2315 if ((file->f_flags & O_ACCMODE) != O_WRONLY) { 2314 if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
2316 /* 2315 /*
2317 * might as well read a page, it is fast enough. If we get 2316 * might as well read a page, it is fast enough. If we get
2318 * an error, we don't need to return it. cifs_write_end will 2317 * an error, we don't need to return it. cifs_write_end will
2319 * do a sync write instead since PG_uptodate isn't set. 2318 * do a sync write instead since PG_uptodate isn't set.
2320 */ 2319 */
2321 cifs_readpage_worker(file, page, &page_start); 2320 cifs_readpage_worker(file, page, &page_start);
2322 } else { 2321 } else {
2323 /* we could try using another file handle if there is one - 2322 /* we could try using another file handle if there is one -
2324 but how would we lock it to prevent close of that handle 2323 but how would we lock it to prevent close of that handle
2325 racing with this read? In any case 2324 racing with this read? In any case
2326 this will be written out by write_end so is fine */ 2325 this will be written out by write_end so is fine */
2327 } 2326 }
2328 out: 2327 out:
2329 *pagep = page; 2328 *pagep = page;
2330 return rc; 2329 return rc;
2331 } 2330 }
2332 2331
2333 const struct address_space_operations cifs_addr_ops = { 2332 const struct address_space_operations cifs_addr_ops = {
2334 .readpage = cifs_readpage, 2333 .readpage = cifs_readpage,
2335 .readpages = cifs_readpages, 2334 .readpages = cifs_readpages,
2336 .writepage = cifs_writepage, 2335 .writepage = cifs_writepage,
2337 .writepages = cifs_writepages, 2336 .writepages = cifs_writepages,
2338 .write_begin = cifs_write_begin, 2337 .write_begin = cifs_write_begin,
2339 .write_end = cifs_write_end, 2338 .write_end = cifs_write_end,
2340 .set_page_dirty = __set_page_dirty_nobuffers, 2339 .set_page_dirty = __set_page_dirty_nobuffers,
2341 /* .sync_page = cifs_sync_page, */ 2340 /* .sync_page = cifs_sync_page, */
2342 /* .direct_IO = */ 2341 /* .direct_IO = */
2343 }; 2342 };
2344 2343
2345 /* 2344 /*
2346 * cifs_readpages requires the server to support a buffer large enough to 2345 * cifs_readpages requires the server to support a buffer large enough to
2347 * contain the header plus one complete page of data. Otherwise, we need 2346 * contain the header plus one complete page of data. Otherwise, we need
2348 * to leave cifs_readpages out of the address space operations. 2347 * to leave cifs_readpages out of the address space operations.
2349 */ 2348 */