ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / cifs / misc.c
1 /*
2  *   fs/cifs/misc.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2003
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *
7  *   This library is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU Lesser General Public License as published
9  *   by the Free Software Foundation; either version 2.1 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This library is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
15  *   the GNU Lesser General Public License for more details.
16  *
17  *   You should have received a copy of the GNU Lesser General Public License
18  *   along with this library; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
20  */
21
22 #include <linux/slab.h>
23 #include <linux/ctype.h>
24 #include "cifspdu.h"
25 #include "cifsglob.h"
26 #include "cifsproto.h"
27 #include "cifs_debug.h"
28 #include "smberr.h"
29 #include "nterr.h"
30
31 extern kmem_cache_t *cifs_req_cachep;
32 extern struct task_struct * oplockThread;
33
34 __u16 GlobalMid;                /* multiplex id - rotating counter */
35
36 /* The xid serves as a useful identifier for each incoming vfs request, 
37    in a similar way to the mid which is useful to track each sent smb, 
38    and CurrentXid can also provide a running counter (although it 
39    will eventually wrap past zero) of the total vfs operations handled 
40    since the cifs fs was mounted */
41
42 unsigned int
43 _GetXid(void)
44 {
45         unsigned int xid;
46
47         spin_lock(&GlobalMid_Lock);
48         GlobalTotalActiveXid++;
49         if (GlobalTotalActiveXid > GlobalMaxActiveXid)
50                 GlobalMaxActiveXid = GlobalTotalActiveXid;      /* keep high water mark for number of simultaneous vfs ops in our filesystem */
51         xid = GlobalCurrentXid++;
52         spin_unlock(&GlobalMid_Lock);
53         return xid;
54 }
55
56 void
57 _FreeXid(unsigned int xid)
58 {
59         spin_lock(&GlobalMid_Lock);
60         /* if(GlobalTotalActiveXid == 0)
61                 BUG(); */
62         GlobalTotalActiveXid--;
63         spin_unlock(&GlobalMid_Lock);
64 }
65
66 struct cifsSesInfo *
67 sesInfoAlloc(void)
68 {
69         struct cifsSesInfo *ret_buf;
70
71         ret_buf =
72             (struct cifsSesInfo *) kmalloc(sizeof (struct cifsSesInfo),
73                                            GFP_KERNEL);
74         if (ret_buf) {
75                 memset(ret_buf, 0, sizeof (struct cifsSesInfo));
76                 write_lock(&GlobalSMBSeslock);
77                 atomic_inc(&sesInfoAllocCount);
78                 ret_buf->status = CifsNew;
79                 list_add(&ret_buf->cifsSessionList, &GlobalSMBSessionList);
80                 init_MUTEX(&ret_buf->sesSem);
81                 write_unlock(&GlobalSMBSeslock);
82         }
83         return ret_buf;
84 }
85
86 void
87 sesInfoFree(struct cifsSesInfo *buf_to_free)
88 {
89         if (buf_to_free == NULL) {
90                 cFYI(1, ("Null buffer passed to sesInfoFree"));
91                 return;
92         }
93
94         write_lock(&GlobalSMBSeslock);
95         atomic_dec(&sesInfoAllocCount);
96         list_del(&buf_to_free->cifsSessionList);
97         write_unlock(&GlobalSMBSeslock);
98         if (buf_to_free->serverOS)
99                 kfree(buf_to_free->serverOS);
100         if (buf_to_free->serverDomain)
101                 kfree(buf_to_free->serverDomain);
102         if (buf_to_free->serverNOS)
103                 kfree(buf_to_free->serverNOS);
104         if (buf_to_free->password)
105                 kfree(buf_to_free->password);
106         kfree(buf_to_free);
107 }
108
109 struct cifsTconInfo *
110 tconInfoAlloc(void)
111 {
112         struct cifsTconInfo *ret_buf;
113         ret_buf =
114             (struct cifsTconInfo *) kmalloc(sizeof (struct cifsTconInfo),
115                                             GFP_KERNEL);
116         if (ret_buf) {
117                 memset(ret_buf, 0, sizeof (struct cifsTconInfo));
118                 write_lock(&GlobalSMBSeslock);
119                 atomic_inc(&tconInfoAllocCount);
120                 list_add(&ret_buf->cifsConnectionList,
121                          &GlobalTreeConnectionList);
122                 ret_buf->tidStatus = CifsNew;
123                 INIT_LIST_HEAD(&ret_buf->openFileList);
124                 init_MUTEX(&ret_buf->tconSem);
125                 write_unlock(&GlobalSMBSeslock);
126         }
127         return ret_buf;
128 }
129
130 void
131 tconInfoFree(struct cifsTconInfo *buf_to_free)
132 {
133         if (buf_to_free == NULL) {
134                 cFYI(1, ("Null buffer passed to tconInfoFree"));
135                 return;
136         }
137         write_lock(&GlobalSMBSeslock);
138         atomic_dec(&tconInfoAllocCount);
139         list_del(&buf_to_free->cifsConnectionList);
140         write_unlock(&GlobalSMBSeslock);
141         if (buf_to_free->nativeFileSystem)
142                 kfree(buf_to_free->nativeFileSystem);
143         kfree(buf_to_free);
144 }
145
146 struct smb_hdr *
147 cifs_buf_get(void)
148 {
149         struct smb_hdr *ret_buf = 0;
150
151 /* We could use negotiated size instead of max_msgsize - 
152    but it may be more efficient to always alloc same size 
153    albeit slightly larger than necessary and maxbuffersize 
154    defaults to this and can not be bigger */
155         ret_buf =
156             (struct smb_hdr *) kmem_cache_alloc(cifs_req_cachep, SLAB_KERNEL | SLAB_NOFS);
157
158         /* clear the first few header bytes */
159         if (ret_buf) {
160                 memset(ret_buf, 0, sizeof (struct smb_hdr));
161                 atomic_inc(&bufAllocCount);
162         }
163
164         return ret_buf;
165 }
166
167 void
168 cifs_buf_release(void *buf_to_free)
169 {
170
171         if (buf_to_free == NULL) {
172                 cFYI(1, ("Null buffer passed to cifs_buf_release"));
173                 return;
174         }
175         kmem_cache_free(cifs_req_cachep, buf_to_free);
176
177         atomic_dec(&bufAllocCount);
178         return;
179 }
180
181 void
182 header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
183                 const struct cifsTconInfo *treeCon, int word_count
184                 /* length of fixed section (word count) in two byte units  */
185     )
186 {
187         int i;
188         __u32 tmp;
189         struct list_head* temp_item;
190         struct cifsSesInfo * ses;
191         char *temp = (char *) buffer;
192
193         for (i = 0; i < MAX_CIFS_HDR_SIZE; i++) {
194                 temp[i] = 0;    /* BB is this needed ?? */
195         }
196
197         buffer->smb_buf_length =
198             (2 * word_count) + sizeof (struct smb_hdr) -
199             4 /*  RFC 1001 length field does not count */  +
200             2 /* for bcc field itself */ ;
201         /* Note that this is the only network field that has to be converted to big endian and it is done just before we send it */
202
203         buffer->Protocol[0] = 0xFF;
204         buffer->Protocol[1] = 'S';
205         buffer->Protocol[2] = 'M';
206         buffer->Protocol[3] = 'B';
207         buffer->Command = smb_command;
208         buffer->Flags = 0x00;   /* case sensitive */
209         buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
210         tmp = cpu_to_le32(current->tgid);
211         buffer->Pid = tmp & 0xFFFF;
212         tmp >>= 16;
213         buffer->PidHigh = tmp & 0xFFFF;
214         spin_lock(&GlobalMid_Lock);
215         GlobalMid++;
216         buffer->Mid = GlobalMid;
217         spin_unlock(&GlobalMid_Lock);
218         if (treeCon) {
219                 buffer->Tid = treeCon->tid;
220                 if (treeCon->ses) {
221                         if (treeCon->ses->capabilities & CAP_UNICODE)
222                                 buffer->Flags2 |= SMBFLG2_UNICODE;
223                         if (treeCon->ses->capabilities & CAP_STATUS32) {
224                                 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
225                         }
226
227                         buffer->Uid = treeCon->ses->Suid;       /* always in LE format */
228                         if(multiuser_mount != 0) {
229                 /* For the multiuser case, there are few obvious technically  */
230                 /* possible mechanisms to match the local linux user (uid)    */
231                 /* to a valid remote smb user (smb_uid):                      */
232                 /*      1) Query Winbind (or other local pam/nss daemon       */
233                 /*        for userid/password/logon_domain or credential      */
234                 /*      2) Query Winbind for uid to sid to username mapping   */
235                 /*         and see if we have a matching password for existing*/
236                 /*         session for that user perhas getting password by   */
237                 /*         adding a new pam_cifs module that stores passwords */
238                 /*         so that the cifs vfs can get at that for all logged*/
239                 /*         on users                                           */
240                 /*      3) (Which is the mechanism we have chosen)            */
241                 /*         Search through sessions to the same server for a   */
242                 /*         a match on the uid that was passed in on mount     */
243                 /*         with the current processes uid (or euid?) and use  */
244                 /*         that smb uid.   If no existing smb session for     */
245                 /*         that uid found, use the default smb session ie     */
246                 /*         the smb session for the volume mounted which is    */
247                 /*         the same as would be used if the multiuser mount   */
248                 /*         flag were disabled.  */
249
250                 /*  BB Add support for establishing new tCon and SMB Session  */
251                 /*      with userid/password pairs found on the smb session   */ 
252                 /*      for other target tcp/ip addresses               BB    */
253                                 if(current->uid != treeCon->ses->linux_uid) {
254                                         cFYI(1,("Multiuser mode and UID did not match tcon uid "));
255                                         read_lock(&GlobalSMBSeslock);
256                                         list_for_each(temp_item, &GlobalSMBSessionList) {
257                                                 ses = list_entry(temp_item, struct cifsSesInfo, cifsSessionList);
258                                                 if(ses->linux_uid == current->uid) {
259                                                         if(ses->server == treeCon->ses->server) {
260                                                                 cFYI(1,("found matching uid substitute right smb_uid"));  
261                                                                 buffer->Uid = ses->Suid;
262                                                                 break;
263                                                         } else {
264                                                                 /* BB eventually call cifs_setup_session here */
265                                                                 cFYI(1,("local UID found but smb sess with this server does not exist"));  
266                                                         }
267                                                 }
268                                         }
269                                         read_unlock(&GlobalSMBSeslock);
270                                 }
271                         }
272                 }
273                 if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
274                         buffer->Flags2 |= SMBFLG2_DFS;
275                 if(treeCon->ses->server)
276                         if(treeCon->ses->server->secMode & 
277                           (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
278                                 buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
279         }
280
281 /*  endian conversion of flags is now done just before sending */
282         buffer->WordCount = (char) word_count;
283         return;
284 }
285
286 int
287 checkSMBhdr(struct smb_hdr *smb, __u16 mid)
288 {
289         /* Make sure that this really is an SMB, that it is a response, 
290            and that the message ids match */
291         if ((*(unsigned int *) smb->Protocol == cpu_to_le32(0x424d53ff)) && 
292                 (mid == smb->Mid)) {    
293                 if(smb->Flags & SMBFLG_RESPONSE)
294                         return 0;                    
295                 else {        
296                 /* only one valid case where server sends us request */
297                         if(smb->Command == SMB_COM_LOCKING_ANDX)
298                                 return 0;
299                         else
300                                 cERROR(1, ("Rcvd Request not response "));         
301                 }
302         } else { /* bad signature or mid */
303                 if (*(unsigned int *) smb->Protocol != cpu_to_le32(0x424d53ff))
304                         cERROR(1,
305                                ("Bad protocol string signature header %x ",
306                                 *(unsigned int *) smb->Protocol));
307                 if (mid != smb->Mid)
308                         cERROR(1, ("Mids do not match"));
309         }
310         cERROR(1, ("bad smb detected. The Mid=%d", smb->Mid));
311         return 1;
312 }
313
314 int
315 checkSMB(struct smb_hdr *smb, __u16 mid, int length)
316 {
317         cFYI(0,
318              ("Entering checkSMB with Length: %x, smb_buf_length: %x ",
319               length, ntohl(smb->smb_buf_length)));
320         if (((unsigned int)length < 2 + sizeof (struct smb_hdr))
321             || (ntohl(smb->smb_buf_length) >
322                 CIFS_MAX_MSGSIZE + MAX_CIFS_HDR_SIZE - 4)) {
323                 if ((unsigned int)length < 2 + sizeof (struct smb_hdr)) {
324                         cERROR(1, ("Length less than 2 + sizeof smb_hdr "));
325                         if (((unsigned int)length >= sizeof (struct smb_hdr) - 1)
326                             && (smb->Status.CifsError != 0))
327                                 return 0;       /* some error cases do not return wct and bcc */
328
329                 }
330                 if (ntohl(smb->smb_buf_length) >
331                     CIFS_MAX_MSGSIZE + MAX_CIFS_HDR_SIZE - 4)
332                         cERROR(1,
333                                ("smb_buf_length greater than CIFS_MAX_MSGSIZE ... "));
334                 cERROR(1,
335                        ("bad smb detected. Illegal length. The mid=%d",
336                         smb->Mid));
337                 return 1;
338         }
339
340         if (checkSMBhdr(smb, mid))
341                 return 1;
342
343         if ((4 + ntohl(smb->smb_buf_length) != smbCalcSize(smb))
344             || (4 + ntohl(smb->smb_buf_length) != (unsigned int)length)) {
345                 return 0;
346         } else {
347                 cERROR(1, ("smbCalcSize %x ", smbCalcSize(smb)));
348                 cERROR(1,
349                        ("bad smb size detected. The Mid=%d", smb->Mid));
350                 return 1;
351         }
352 }
353 int
354 is_valid_oplock_break(struct smb_hdr *buf)
355 {    
356         struct smb_com_lock_req * pSMB = (struct smb_com_lock_req *)buf;
357         struct list_head *tmp;
358         struct list_head *tmp1;
359         struct cifsTconInfo *tcon;
360         struct cifsFileInfo *netfile;
361
362         /* could add check for smb response flag 0x80 */
363         cFYI(1,("Checking for oplock break"));    
364         if(pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
365                 return FALSE;
366         if(pSMB->hdr.Flags & SMBFLG_RESPONSE) {
367                 /* no sense logging error on invalid handle on oplock
368                    break - harmless race between close request and oplock
369                    break response is expected from time to time writing out
370                    large dirty files cached on the client */
371                 if ((NT_STATUS_INVALID_HANDLE) == 
372                    le32_to_cpu(pSMB->hdr.Status.CifsError)) { 
373                         cFYI(1,("invalid handle on oplock break"));
374                         return TRUE;
375                 } else if (ERRbadfid == 
376                    le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
377                         return TRUE;      
378                 } else {
379                         return FALSE; /* on valid oplock brk we get "request" */
380                 }
381         }
382         if(pSMB->hdr.WordCount != 8)
383                 return FALSE;
384
385         cFYI(1,(" oplock type 0x%d level 0x%d",pSMB->LockType,pSMB->OplockLevel));
386         if(!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
387                 return FALSE;    
388
389         /* look up tcon based on tid & uid */
390         read_lock(&GlobalSMBSeslock);
391         list_for_each(tmp, &GlobalTreeConnectionList) {
392                 tcon = list_entry(tmp, struct cifsTconInfo, cifsConnectionList);
393                 if (tcon->tid == buf->Tid) {
394                         list_for_each(tmp1,&tcon->openFileList){
395                                 netfile = list_entry(tmp1,struct cifsFileInfo,tlist);
396                                 if(pSMB->Fid == netfile->netfid) {
397                                         struct cifsInodeInfo *pCifsInode;
398                                         read_unlock(&GlobalSMBSeslock);
399                                         cFYI(1,("Matching file id, processing oplock break"));
400                                         pCifsInode = 
401                                                 CIFS_I(netfile->pInode);
402                                         pCifsInode->clientCanCacheAll = FALSE;
403                                         if(pSMB->OplockLevel == 0)
404                                                 pCifsInode->clientCanCacheRead = FALSE;
405                                         pCifsInode->oplockPending = TRUE;
406                                         AllocOplockQEntry(netfile->pInode, netfile->netfid, tcon);
407                                         cFYI(1,("about to wake up oplock thd"));
408                                         wake_up_process(oplockThread);               
409                                         return TRUE;
410                                 }
411                         }
412                         read_unlock(&GlobalSMBSeslock);
413                         cFYI(1,("No matching file for oplock break on connection"));
414                         return TRUE;
415                 }
416         }
417         read_unlock(&GlobalSMBSeslock);
418         cFYI(1,("Can not process oplock break for non-existent connection"));
419         return TRUE;
420 }
421
422 void
423 dump_smb(struct smb_hdr *smb_buf, int smb_buf_length)
424 {
425         int i, j;
426         char debug_line[17];
427         unsigned char *buffer;
428
429         if (traceSMB == 0)
430                 return;
431
432         buffer = (unsigned char *) smb_buf;
433         for (i = 0, j = 0; i < smb_buf_length; i++, j++) {
434                 if (i % 8 == 0) {       /* we have reached the beginning of line  */
435                         printk(KERN_DEBUG "| ");
436                         j = 0;
437                 }
438                 printk("%0#4x ", buffer[i]);
439                 debug_line[2 * j] = ' ';
440                 if (isprint(buffer[i]))
441                         debug_line[1 + (2 * j)] = buffer[i];
442                 else
443                         debug_line[1 + (2 * j)] = '_';
444
445                 if (i % 8 == 7) {       /* we have reached end of line, time to print ascii */
446                         debug_line[16] = 0;
447                         printk(" | %s\n", debug_line);
448                 }
449         }
450         for (; j < 8; j++) {
451                 printk("     ");
452                 debug_line[2 * j] = ' ';
453                 debug_line[1 + (2 * j)] = ' ';
454         }
455         printk( " | %s\n", debug_line);
456         return;
457 }