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