vserver 1.9.5.x5
[linux-2.6.git] / fs / cifs / cifsfs.c
1 /*
2  *   fs/cifs/cifsfs.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2004
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *
7  *   Common Internet FileSystem (CIFS) client
8  *
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
11  *   by the Free Software Foundation; either version 2.1 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This library is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
17  *   the GNU Lesser General Public License for more details.
18  *
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
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23
24 /* Note that BB means BUGBUG (ie something to fix eventually) */
25
26 #include <linux/module.h>
27 #include <linux/fs.h>
28 #include <linux/mount.h>
29 #include <linux/slab.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/seq_file.h>
33 #include <linux/vfs.h>
34 #include <linux/mempool.h>
35 #include "cifsfs.h"
36 #include "cifspdu.h"
37 #define DECLARE_GLOBALS_HERE
38 #include "cifsglob.h"
39 #include "cifsproto.h"
40 #include "cifs_debug.h"
41 #include "cifs_fs_sb.h"
42 #include <linux/mm.h>
43 #define CIFS_MAGIC_NUMBER 0xFF534D42    /* the first four bytes of SMB PDUs */
44
45 #ifdef CONFIG_CIFS_QUOTA
46 static struct quotactl_ops cifs_quotactl_ops;
47 #endif
48
49 int cifsFYI = 0;
50 int cifsERROR = 1;
51 int traceSMB = 0;
52 unsigned int oplockEnabled = 1;
53 unsigned int experimEnabled = 0;
54 unsigned int linuxExtEnabled = 1;
55 unsigned int lookupCacheEnabled = 1;
56 unsigned int multiuser_mount = 0;
57 unsigned int extended_security = 0;
58 unsigned int ntlmv2_support = 0;
59 unsigned int sign_CIFS_PDUs = 1;
60 struct task_struct * oplockThread = NULL;
61 unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
62 module_param(CIFSMaxBufSize, int, CIFS_MAX_MSGSIZE);
63 MODULE_PARM_DESC(CIFSMaxBufSize,"Network buffer size (not including header). Default: 16384 Range: 8192 to 130048");
64 unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
65 module_param(cifs_min_rcv, int, CIFS_MIN_RCV_POOL);
66 MODULE_PARM_DESC(cifs_min_rcv,"Network buffers in pool. Default: 4 Range: 1 to 64");
67 unsigned int cifs_min_small = 30;
68 module_param(cifs_min_small, int, 30);
69 MODULE_PARM_DESC(cifs_small_rcv,"Small network buffers in pool. Default: 30 Range: 2 to 256");
70 unsigned int cifs_max_pending = CIFS_MAX_REQ;
71 module_param(cifs_max_pending, int, CIFS_MAX_REQ);
72 MODULE_PARM_DESC(cifs_max_pending,"Simultaneous requests to server. Default: 50 Range: 2 to 256");
73
74
75 extern int cifs_mount(struct super_block *, struct cifs_sb_info *, char *,
76                         const char *);
77 extern int cifs_umount(struct super_block *, struct cifs_sb_info *);
78 void cifs_proc_init(void);
79 void cifs_proc_clean(void);
80
81 static DECLARE_COMPLETION(cifs_oplock_exited);
82
83
84 static int
85 cifs_read_super(struct super_block *sb, void *data,
86                 const char *devname, int silent)
87 {
88         struct inode *inode;
89         struct cifs_sb_info *cifs_sb;
90         int rc = 0;
91
92         sb->s_flags |= MS_NODIRATIME; /* and probably even noatime */
93         sb->s_fs_info = kmalloc(sizeof(struct cifs_sb_info),GFP_KERNEL);
94         cifs_sb = CIFS_SB(sb);
95         if(cifs_sb == NULL)
96                 return -ENOMEM;
97         else
98                 memset(cifs_sb,0,sizeof(struct cifs_sb_info));
99         
100
101         rc = cifs_mount(sb, cifs_sb, data, devname);
102
103         if (rc) {
104                 if (!silent)
105                         cERROR(1,
106                                ("cifs_mount failed w/return code = %d", rc));
107                 goto out_mount_failed;
108         }
109
110         sb->s_magic = CIFS_MAGIC_NUMBER;
111         sb->s_op = &cifs_super_ops;
112 /*      if(cifs_sb->tcon->ses->server->maxBuf > MAX_CIFS_HDR_SIZE + 512)
113             sb->s_blocksize = cifs_sb->tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE; */
114 #ifdef CONFIG_CIFS_QUOTA
115         sb->s_qcop = &cifs_quotactl_ops;
116 #endif
117         sb->s_blocksize = CIFS_MAX_MSGSIZE;
118         sb->s_blocksize_bits = 14;      /* default 2**14 = CIFS_MAX_MSGSIZE */
119         inode = iget(sb, ROOT_I);
120
121         if (!inode) {
122                 rc = -ENOMEM;
123                 goto out_no_root;
124         }
125
126         sb->s_root = d_alloc_root(inode);
127
128         if (!sb->s_root) {
129                 rc = -ENOMEM;
130                 goto out_no_root;
131         }
132
133         return 0;
134
135 out_no_root:
136         cERROR(1, ("cifs_read_super: get root inode failed"));
137         if (inode)
138                 iput(inode);
139
140 out_mount_failed:
141         if(cifs_sb) {
142                 if(cifs_sb->local_nls)
143                         unload_nls(cifs_sb->local_nls); 
144                 kfree(cifs_sb);
145         }
146         return rc;
147 }
148
149 static void
150 cifs_put_super(struct super_block *sb)
151 {
152         int rc = 0;
153         struct cifs_sb_info *cifs_sb;
154
155         cFYI(1, ("In cifs_put_super"));
156         cifs_sb = CIFS_SB(sb);
157         if(cifs_sb == NULL) {
158                 cFYI(1,("Empty cifs superblock info passed to unmount"));
159                 return;
160         }
161         rc = cifs_umount(sb, cifs_sb); 
162         if (rc) {
163                 cERROR(1, ("cifs_umount failed with return code %d", rc));
164         }
165         unload_nls(cifs_sb->local_nls);
166         kfree(cifs_sb);
167         return;
168 }
169
170 static int
171 cifs_statfs(struct super_block *sb, struct kstatfs *buf)
172 {
173         int xid, rc;
174         struct cifs_sb_info *cifs_sb;
175         struct cifsTconInfo *pTcon;
176
177         xid = GetXid();
178
179         cifs_sb = CIFS_SB(sb);
180         pTcon = cifs_sb->tcon;
181
182         buf->f_type = CIFS_MAGIC_NUMBER;
183
184         /* instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO */
185         buf->f_namelen = PATH_MAX;      /* PATH_MAX may be too long - it would presumably
186                                            be length of total path, note that some servers may be 
187                                            able to support more than this, but best to be safe
188                                            since Win2k and others can not handle very long filenames */
189         buf->f_files = 0;       /* undefined */
190         buf->f_ffree = 0;       /* unlimited */
191
192         rc = CIFSSMBQFSInfo(xid, pTcon, buf, cifs_sb->local_nls);
193
194         /*     
195            int f_type;
196            __fsid_t f_fsid;
197            int f_namelen;  */
198         /* BB get from info put in tcon struct at mount time with call to QFSAttrInfo */
199         FreeXid(xid);
200         return 0;               /* always return success? what if volume is no longer available? */
201 }
202
203 static int cifs_permission(struct inode * inode, int mask, struct nameidata *nd)
204 {
205         struct cifs_sb_info *cifs_sb;
206
207         cifs_sb = CIFS_SB(inode->i_sb);
208
209         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
210                 return 0;
211         } else /* file mode might have been restricted at mount time 
212                 on the client (above and beyond ACL on servers) for  
213                 servers which do not support setting and viewing mode bits,
214                 so allowing client to check permissions is useful */ 
215                 return generic_permission(inode, mask, NULL);
216 }
217
218 static kmem_cache_t *cifs_inode_cachep;
219 static kmem_cache_t *cifs_req_cachep;
220 static kmem_cache_t *cifs_mid_cachep;
221 kmem_cache_t *cifs_oplock_cachep;
222 static kmem_cache_t *cifs_sm_req_cachep;
223 mempool_t *cifs_sm_req_poolp;
224 mempool_t *cifs_req_poolp;
225 mempool_t *cifs_mid_poolp;
226
227 static struct inode *
228 cifs_alloc_inode(struct super_block *sb)
229 {
230         struct cifsInodeInfo *cifs_inode;
231         cifs_inode =
232             (struct cifsInodeInfo *) kmem_cache_alloc(cifs_inode_cachep,
233                                                       SLAB_KERNEL);
234         if (!cifs_inode)
235                 return NULL;
236         cifs_inode->cifsAttrs = 0x20;   /* default */
237         atomic_set(&cifs_inode->inUse, 0);
238         cifs_inode->time = 0;
239         /* Until the file is open and we have gotten oplock
240         info back from the server, can not assume caching of
241         file data or metadata */
242         cifs_inode->clientCanCacheRead = FALSE;
243         cifs_inode->clientCanCacheAll = FALSE;
244         cifs_inode->vfs_inode.i_blksize = CIFS_MAX_MSGSIZE;
245         cifs_inode->vfs_inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
246
247         INIT_LIST_HEAD(&cifs_inode->openFileList);
248         return &cifs_inode->vfs_inode;
249 }
250
251 static void
252 cifs_destroy_inode(struct inode *inode)
253 {
254         kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
255 }
256
257 /*
258  * cifs_show_options() is for displaying mount options in /proc/mounts.
259  * Not all settable options are displayed but most of the important
260  * ones are.
261  */
262 static int
263 cifs_show_options(struct seq_file *s, struct vfsmount *m)
264 {
265         struct cifs_sb_info *cifs_sb;
266
267         cifs_sb = CIFS_SB(m->mnt_sb);
268
269         if (cifs_sb) {
270                 if (cifs_sb->tcon) {
271                         seq_printf(s, ",unc=%s", cifs_sb->tcon->treeName);
272                         if (cifs_sb->tcon->ses) {
273                                 if (cifs_sb->tcon->ses->userName)
274                                         seq_printf(s, ",username=%s",
275                                            cifs_sb->tcon->ses->userName);
276                                 if(cifs_sb->tcon->ses->domainName)
277                                         seq_printf(s, ",domain=%s",
278                                            cifs_sb->tcon->ses->domainName);
279                         }
280                 }
281                 seq_printf(s, ",rsize=%d",cifs_sb->rsize);
282                 seq_printf(s, ",wsize=%d",cifs_sb->wsize);
283         }
284         return 0;
285 }
286
287 #ifdef CONFIG_CIFS_QUOTA
288 int cifs_xquota_set(struct super_block * sb, int quota_type, qid_t qid,
289                 struct fs_disk_quota * pdquota)
290 {
291         int xid;
292         int rc = 0;
293         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
294         struct cifsTconInfo *pTcon;
295         
296         if(cifs_sb)
297                 pTcon = cifs_sb->tcon;
298         else
299                 return -EIO;
300
301
302         xid = GetXid();
303         if(pTcon) {
304                 cFYI(1,("set type: 0x%x id: %d",quota_type,qid));               
305         } else {
306                 return -EIO;
307         }
308
309         FreeXid(xid);
310         return rc;
311 }
312
313 int cifs_xquota_get(struct super_block * sb, int quota_type, qid_t qid,
314                 struct fs_disk_quota * pdquota)
315 {
316         int xid;
317         int rc = 0;
318         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
319         struct cifsTconInfo *pTcon;
320
321         if(cifs_sb)
322                 pTcon = cifs_sb->tcon;
323         else
324                 return -EIO;
325
326         xid = GetXid();
327         if(pTcon) {
328                 cFYI(1,("set type: 0x%x id: %d",quota_type,qid));
329         } else {
330                 rc = -EIO;
331         }
332
333         FreeXid(xid);
334         return rc;
335 }
336
337 int cifs_xstate_set(struct super_block * sb, unsigned int flags, int operation)
338 {
339         int xid; 
340         int rc = 0;
341         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
342         struct cifsTconInfo *pTcon;
343
344         if(cifs_sb)
345                 pTcon = cifs_sb->tcon;
346         else
347                 return -EIO;
348
349         xid = GetXid();
350         if(pTcon) {
351                 cFYI(1,("flags: 0x%x operation: 0x%x",flags,operation));
352         } else {
353                 rc = -EIO;
354         }
355
356         FreeXid(xid);
357         return rc;
358 }
359
360 int cifs_xstate_get(struct super_block * sb, struct fs_quota_stat *qstats)
361 {
362         int xid;
363         int rc = 0;
364         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
365         struct cifsTconInfo *pTcon;
366
367         if(cifs_sb) {
368                 pTcon = cifs_sb->tcon;
369         } else {
370                 return -EIO;
371         }
372         xid = GetXid();
373         if(pTcon) {
374                 cFYI(1,("pqstats %p",qstats));          
375         } else {
376                 rc = -EIO;
377         }
378
379         FreeXid(xid);
380         return rc;
381 }
382
383 static struct quotactl_ops cifs_quotactl_ops = {
384         .set_xquota     = cifs_xquota_set,
385         .get_xquota     = cifs_xquota_set,
386         .set_xstate     = cifs_xstate_set,
387         .get_xstate     = cifs_xstate_get,
388 };
389 #endif
390
391 static int cifs_remount(struct super_block *sb, int *flags, char *data)
392 {
393         *flags |= MS_NODIRATIME;
394         return 0;
395 }
396
397 struct super_operations cifs_super_ops = {
398         .read_inode = cifs_read_inode,
399         .put_super = cifs_put_super,
400         .statfs = cifs_statfs,
401         .alloc_inode = cifs_alloc_inode,
402         .destroy_inode = cifs_destroy_inode,
403 /*      .drop_inode         = generic_delete_inode, 
404         .delete_inode   = cifs_delete_inode,  *//* Do not need the above two functions     
405    unless later we add lazy close of inodes or unless the kernel forgets to call
406    us with the same number of releases (closes) as opens */
407         .show_options = cifs_show_options,
408 /*    .umount_begin   = cifs_umount_begin, *//* consider adding in the future */
409         .remount_fs = cifs_remount,
410 };
411
412 static struct super_block *
413 cifs_get_sb(struct file_system_type *fs_type,
414             int flags, const char *dev_name, void *data)
415 {
416         int rc;
417         struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
418
419         cFYI(1, ("Devname: %s flags: %d ", dev_name, flags));
420
421         if (IS_ERR(sb))
422                 return sb;
423
424         sb->s_flags = flags;
425
426         rc = cifs_read_super(sb, data, dev_name, flags & MS_VERBOSE ? 1 : 0);
427         if (rc) {
428                 up_write(&sb->s_umount);
429                 deactivate_super(sb);
430                 return ERR_PTR(rc);
431         }
432         sb->s_flags |= MS_ACTIVE;
433         return sb;
434 }
435
436 static ssize_t
437 cifs_read_wrapper(struct file * file, char __user *read_data, size_t read_size,
438           loff_t * poffset)
439 {
440         if(file == NULL)
441                 return -EIO;
442         else if(file->f_dentry == NULL)
443                 return -EIO;
444         else if(file->f_dentry->d_inode == NULL)
445                 return -EIO;
446
447         cFYI(1,("In read_wrapper size %zd at %lld",read_size,*poffset));
448
449 #ifdef CONFIG_CIFS_EXPERIMENTAL
450         /* check whether we can cache writes locally */
451         if(file->f_dentry->d_sb) {
452                 struct cifs_sb_info *cifs_sb;
453                 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
454                 if(cifs_sb != NULL) {
455                         if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
456                                 return cifs_user_read(file,read_data,
457                                                         read_size,poffset);
458                 }
459         }
460 #endif /* CIFS_EXPERIMENTAL */
461
462         if(CIFS_I(file->f_dentry->d_inode)->clientCanCacheRead) {
463                 return generic_file_read(file,read_data,read_size,poffset);
464         } else {
465                 /* BB do we need to lock inode from here until after invalidate? */
466 /*              if(file->f_dentry->d_inode->i_mapping) {
467                         filemap_fdatawrite(file->f_dentry->d_inode->i_mapping);
468                         filemap_fdatawait(file->f_dentry->d_inode->i_mapping);
469                 }*/
470 /*              cifs_revalidate(file->f_dentry);*/ /* BB fixme */
471
472                 /* BB we should make timer configurable - perhaps 
473                    by simply calling cifs_revalidate here */
474                 /* invalidate_remote_inode(file->f_dentry->d_inode);*/
475                 return generic_file_read(file,read_data,read_size,poffset);
476         }
477 }
478
479 static ssize_t
480 cifs_write_wrapper(struct file * file, const char __user *write_data,
481            size_t write_size, loff_t * poffset) 
482 {
483         ssize_t written;
484
485         if(file == NULL)
486                 return -EIO;
487         else if(file->f_dentry == NULL)
488                 return -EIO;
489         else if(file->f_dentry->d_inode == NULL)
490                 return -EIO;
491
492         cFYI(1,("In write_wrapper size %zd at %lld",write_size,*poffset));
493
494 #ifdef CONFIG_CIFS_EXPERIMENTAL    /* BB fixme - fix user char * to kernel char * mapping here BB */
495         /* check whether we can cache writes locally */
496         if(file->f_dentry->d_sb) {
497                 struct cifs_sb_info *cifs_sb;
498                 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
499                 if(cifs_sb != NULL) {
500                         if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
501                                 return cifs_user_write(file,write_data,
502                                                         write_size,poffset);
503                         }
504                 }
505         }
506 #endif /* CIFS_EXPERIMENTAL */
507         written = generic_file_write(file,write_data,write_size,poffset);
508         if(!CIFS_I(file->f_dentry->d_inode)->clientCanCacheAll)  {
509                 if(file->f_dentry->d_inode->i_mapping) {
510                         filemap_fdatawrite(file->f_dentry->d_inode->i_mapping);
511                 }
512         }
513         return written;
514 }
515
516
517 static struct file_system_type cifs_fs_type = {
518         .owner = THIS_MODULE,
519         .name = "cifs",
520         .get_sb = cifs_get_sb,
521         .kill_sb = kill_anon_super,
522         /*  .fs_flags */
523 };
524 struct inode_operations cifs_dir_inode_ops = {
525         .create = cifs_create,
526         .lookup = cifs_lookup,
527         .getattr = cifs_getattr,
528         .unlink = cifs_unlink,
529         .link = cifs_hardlink,
530         .mkdir = cifs_mkdir,
531         .rmdir = cifs_rmdir,
532         .rename = cifs_rename,
533         .permission = cifs_permission,
534 /*      revalidate:cifs_revalidate,   */
535         .setattr = cifs_setattr,
536         .symlink = cifs_symlink,
537         .mknod   = cifs_mknod,
538 #ifdef CONFIG_CIFS_XATTR
539         .setxattr = cifs_setxattr,
540         .getxattr = cifs_getxattr,
541         .listxattr = cifs_listxattr,
542         .removexattr = cifs_removexattr,
543 #endif
544 };
545
546 struct inode_operations cifs_file_inode_ops = {
547 /*      revalidate:cifs_revalidate, */
548         .setattr = cifs_setattr,
549         .getattr = cifs_getattr, /* do we need this anymore? */
550         .rename = cifs_rename,
551         .permission = cifs_permission,
552 #ifdef CONFIG_CIFS_XATTR
553         .setxattr = cifs_setxattr,
554         .getxattr = cifs_getxattr,
555         .listxattr = cifs_listxattr,
556         .removexattr = cifs_removexattr,
557 #endif 
558 };
559
560 struct inode_operations cifs_symlink_inode_ops = {
561         .readlink = generic_readlink, 
562         .follow_link = cifs_follow_link,
563         .put_link = cifs_put_link,
564         .permission = cifs_permission,
565         /* BB add the following two eventually */
566         /* revalidate: cifs_revalidate,
567            setattr:    cifs_notify_change, *//* BB do we need notify change */
568 #ifdef CONFIG_CIFS_XATTR
569         .setxattr = cifs_setxattr,
570         .getxattr = cifs_getxattr,
571         .listxattr = cifs_listxattr,
572         .removexattr = cifs_removexattr,
573 #endif 
574 };
575
576 struct file_operations cifs_file_ops = {
577         .read = cifs_read_wrapper,
578         .write = cifs_write_wrapper, 
579         .open = cifs_open,
580         .release = cifs_close,
581         .lock = cifs_lock,
582         .fsync = cifs_fsync,
583         .flush = cifs_flush,
584         .mmap  = cifs_file_mmap,
585         .sendfile = generic_file_sendfile,
586 #ifdef CONFIG_CIFS_EXPERIMENTAL
587         .dir_notify = cifs_dir_notify,
588 #endif /* CONFIG_CIFS_EXPERIMENTAL */
589 };
590
591 struct file_operations cifs_dir_ops = {
592         .readdir = cifs_readdir,
593         .release = cifs_closedir,
594         .read    = generic_read_dir,
595 #ifdef CONFIG_CIFS_EXPERIMENTAL
596         .dir_notify = cifs_dir_notify,
597 #endif /* CONFIG_CIFS_EXPERIMENTAL */
598 };
599
600 static void
601 cifs_init_once(void *inode, kmem_cache_t * cachep, unsigned long flags)
602 {
603         struct cifsInodeInfo *cifsi = (struct cifsInodeInfo *) inode;
604
605         if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
606             SLAB_CTOR_CONSTRUCTOR) {
607                 inode_init_once(&cifsi->vfs_inode);
608                 INIT_LIST_HEAD(&cifsi->lockList);
609         }
610 }
611
612 static int
613 cifs_init_inodecache(void)
614 {
615         cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
616                                               sizeof (struct cifsInodeInfo),
617                                               0, SLAB_RECLAIM_ACCOUNT,
618                                               cifs_init_once, NULL);
619         if (cifs_inode_cachep == NULL)
620                 return -ENOMEM;
621
622         return 0;
623 }
624
625 static void
626 cifs_destroy_inodecache(void)
627 {
628         if (kmem_cache_destroy(cifs_inode_cachep))
629                 printk(KERN_WARNING "cifs_inode_cache: error freeing\n");
630 }
631
632 static int
633 cifs_init_request_bufs(void)
634 {
635         if(CIFSMaxBufSize < 8192) {
636         /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
637         Unicode path name has to fit in any SMB/CIFS path based frames */
638                 CIFSMaxBufSize = 8192;
639         } else if (CIFSMaxBufSize > 1024*127) {
640                 CIFSMaxBufSize = 1024 * 127;
641         } else {
642                 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
643         }
644 /*      cERROR(1,("CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize)); */
645         cifs_req_cachep = kmem_cache_create("cifs_request",
646                                             CIFSMaxBufSize +
647                                             MAX_CIFS_HDR_SIZE, 0,
648                                             SLAB_HWCACHE_ALIGN, NULL, NULL);
649         if (cifs_req_cachep == NULL)
650                 return -ENOMEM;
651
652         if(cifs_min_rcv < 1)
653                 cifs_min_rcv = 1;
654         else if (cifs_min_rcv > 64) {
655                 cifs_min_rcv = 64;
656                 cFYI(1,("cifs_min_rcv set to maximum (64)"));
657         }
658
659         cifs_req_poolp = mempool_create(cifs_min_rcv,
660                                         mempool_alloc_slab,
661                                         mempool_free_slab,
662                                         cifs_req_cachep);
663
664         if(cifs_req_poolp == NULL) {
665                 kmem_cache_destroy(cifs_req_cachep);
666                 return -ENOMEM;
667         }
668         /* 256 (MAX_CIFS_HDR_SIZE bytes is enough for most SMB responses and
669         almost all handle based requests (but not write response, nor is it
670         sufficient for path based requests).  A smaller size would have
671         been more efficient (compacting multiple slab items on one 4k page) 
672         for the case in which debug was on, but this larger size allows
673         more SMBs to use small buffer alloc and is still much more
674         efficient to alloc 1 per page off the slab compared to 17K (5page) 
675         alloc of large cifs buffers even when page debugging is on */
676         cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
677                         MAX_CIFS_HDR_SIZE, 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
678         if (cifs_sm_req_cachep == NULL) {
679                 mempool_destroy(cifs_req_poolp);
680                 kmem_cache_destroy(cifs_req_cachep);
681                 return -ENOMEM;              
682         }
683
684         if(cifs_min_small < 2)
685                 cifs_min_small = 2;
686         else if (cifs_min_small > 256) {
687                 cifs_min_small = 256;
688                 cFYI(1,("cifs_min_small set to maximum (256)"));
689         }
690
691         cifs_sm_req_poolp = mempool_create(cifs_min_small,
692                                 mempool_alloc_slab,
693                                 mempool_free_slab,
694                                 cifs_sm_req_cachep);
695
696         if(cifs_sm_req_poolp == NULL) {
697                 mempool_destroy(cifs_req_poolp);
698                 kmem_cache_destroy(cifs_req_cachep);
699                 kmem_cache_destroy(cifs_sm_req_cachep);
700                 return -ENOMEM;
701         }
702
703         return 0;
704 }
705
706 static void
707 cifs_destroy_request_bufs(void)
708 {
709         mempool_destroy(cifs_req_poolp);
710         if (kmem_cache_destroy(cifs_req_cachep))
711                 printk(KERN_WARNING
712                        "cifs_destroy_request_cache: error not all structures were freed\n");
713         mempool_destroy(cifs_sm_req_poolp);
714         if (kmem_cache_destroy(cifs_sm_req_cachep))
715                 printk(KERN_WARNING
716                       "cifs_destroy_request_cache: cifs_small_rq free error\n");
717 }
718
719 static int
720 cifs_init_mids(void)
721 {
722         cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
723                                 sizeof (struct mid_q_entry), 0,
724                                 SLAB_HWCACHE_ALIGN, NULL, NULL);
725         if (cifs_mid_cachep == NULL)
726                 return -ENOMEM;
727
728         cifs_mid_poolp = mempool_create(3 /* a reasonable min simultan opers */,
729                                         mempool_alloc_slab,
730                                         mempool_free_slab,
731                                         cifs_mid_cachep);
732         if(cifs_mid_poolp == NULL) {
733                 kmem_cache_destroy(cifs_mid_cachep);
734                 return -ENOMEM;
735         }
736
737         cifs_oplock_cachep = kmem_cache_create("cifs_oplock_structs",
738                                 sizeof (struct oplock_q_entry), 0,
739                                 SLAB_HWCACHE_ALIGN, NULL, NULL);
740         if (cifs_oplock_cachep == NULL) {
741                 kmem_cache_destroy(cifs_mid_cachep);
742                 mempool_destroy(cifs_mid_poolp);
743                 return -ENOMEM;
744         }
745
746         return 0;
747 }
748
749 static void
750 cifs_destroy_mids(void)
751 {
752         mempool_destroy(cifs_mid_poolp);
753         if (kmem_cache_destroy(cifs_mid_cachep))
754                 printk(KERN_WARNING
755                        "cifs_destroy_mids: error not all structures were freed\n");
756
757         if (kmem_cache_destroy(cifs_oplock_cachep))
758                 printk(KERN_WARNING
759                        "error not all oplock structures were freed\n");
760 }
761
762 static int cifs_oplock_thread(void * dummyarg)
763 {
764         struct oplock_q_entry * oplock_item;
765         struct cifsTconInfo *pTcon;
766         struct inode * inode;
767         __u16  netfid;
768         int rc;
769
770         daemonize("cifsoplockd");
771         allow_signal(SIGTERM);
772
773         oplockThread = current;
774         do {
775                 set_current_state(TASK_INTERRUPTIBLE);
776                 
777                 schedule_timeout(1*HZ);  
778                 spin_lock(&GlobalMid_Lock);
779                 if(list_empty(&GlobalOplock_Q)) {
780                         spin_unlock(&GlobalMid_Lock);
781                         set_current_state(TASK_INTERRUPTIBLE);
782                         schedule_timeout(39*HZ);
783                 } else {
784                         oplock_item = list_entry(GlobalOplock_Q.next, 
785                                 struct oplock_q_entry, qhead);
786                         if(oplock_item) {
787                                 cFYI(1,("found oplock item to write out")); 
788                                 pTcon = oplock_item->tcon;
789                                 inode = oplock_item->pinode;
790                                 netfid = oplock_item->netfid;
791                                 spin_unlock(&GlobalMid_Lock);
792                                 DeleteOplockQEntry(oplock_item);
793                                 /* can not grab inode sem here since it would
794                                 deadlock when oplock received on delete 
795                                 since vfs_unlink holds the i_sem across
796                                 the call */
797                                 /* down(&inode->i_sem);*/
798                                 if (S_ISREG(inode->i_mode)) {
799                                         rc = filemap_fdatawrite(inode->i_mapping);
800                                         if(CIFS_I(inode)->clientCanCacheRead == 0) {
801                                                 filemap_fdatawait(inode->i_mapping);
802                                                 invalidate_remote_inode(inode);
803                                         }
804                                 } else
805                                         rc = 0;
806                                 /* up(&inode->i_sem);*/
807                                 if (rc)
808                                         CIFS_I(inode)->write_behind_rc = rc;
809                                 cFYI(1,("Oplock flush inode %p rc %d",inode,rc));
810
811                                 /* releasing a stale oplock after recent reconnection 
812                                 of smb session using a now incorrect file 
813                                 handle is not a data integrity issue but do  
814                                 not bother sending an oplock release if session 
815                                 to server still is disconnected since oplock 
816                                 already released by the server in that case */
817                                 if(pTcon->tidStatus != CifsNeedReconnect) {
818                                     rc = CIFSSMBLock(0, pTcon, netfid,
819                                             0 /* len */ , 0 /* offset */, 0, 
820                                             0, LOCKING_ANDX_OPLOCK_RELEASE,
821                                             0 /* wait flag */);
822                                         cFYI(1,("Oplock release rc = %d ",rc));
823                                 }
824                         } else
825                                 spin_unlock(&GlobalMid_Lock);
826                 }
827         } while(!signal_pending(current));
828         complete_and_exit (&cifs_oplock_exited, 0);
829 }
830
831 static int __init
832 init_cifs(void)
833 {
834         int rc = 0;
835 #ifdef CONFIG_PROC_FS
836         cifs_proc_init();
837 #endif
838         INIT_LIST_HEAD(&GlobalServerList);      /* BB not implemented yet */
839         INIT_LIST_HEAD(&GlobalSMBSessionList);
840         INIT_LIST_HEAD(&GlobalTreeConnectionList);
841         INIT_LIST_HEAD(&GlobalOplock_Q);
842 /*
843  *  Initialize Global counters
844  */
845         atomic_set(&sesInfoAllocCount, 0);
846         atomic_set(&tconInfoAllocCount, 0);
847         atomic_set(&tcpSesAllocCount,0);
848         atomic_set(&tcpSesReconnectCount, 0);
849         atomic_set(&tconInfoReconnectCount, 0);
850
851         atomic_set(&bufAllocCount, 0);
852         atomic_set(&midCount, 0);
853         GlobalCurrentXid = 0;
854         GlobalTotalActiveXid = 0;
855         GlobalMaxActiveXid = 0;
856         rwlock_init(&GlobalSMBSeslock);
857         spin_lock_init(&GlobalMid_Lock);
858
859         if(cifs_max_pending < 2) {
860                 cifs_max_pending = 2;
861                 cFYI(1,("cifs_max_pending set to min of 2"));
862         } else if(cifs_max_pending > 256) {
863                 cifs_max_pending = 256;
864                 cFYI(1,("cifs_max_pending set to max of 256"));
865         }
866
867         rc = cifs_init_inodecache();
868         if (!rc) {
869                 rc = cifs_init_mids();
870                 if (!rc) {
871                         rc = cifs_init_request_bufs();
872                         if (!rc) {
873                                 rc = register_filesystem(&cifs_fs_type);
874                                 if (!rc) {                
875                                         rc = (int)kernel_thread(cifs_oplock_thread, NULL, 
876                                                 CLONE_FS | CLONE_FILES | CLONE_VM);
877                                         if(rc > 0)
878                                                 return 0;
879                                         else 
880                                                 cERROR(1,("error %d create oplock thread",rc));
881                                 }
882                                 cifs_destroy_request_bufs();
883                         }
884                         cifs_destroy_mids();
885                 }
886                 cifs_destroy_inodecache();
887         }
888 #ifdef CONFIG_PROC_FS
889         cifs_proc_clean();
890 #endif
891         return rc;
892 }
893
894 static void __exit
895 exit_cifs(void)
896 {
897         cFYI(0, ("In unregister ie exit_cifs"));
898 #ifdef CONFIG_PROC_FS
899         cifs_proc_clean();
900 #endif
901         unregister_filesystem(&cifs_fs_type);
902         cifs_destroy_inodecache();
903         cifs_destroy_mids();
904         cifs_destroy_request_bufs();
905         if(oplockThread) {
906                 send_sig(SIGTERM, oplockThread, 1);
907                 wait_for_completion(&cifs_oplock_exited);
908         }
909 }
910
911 MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
912 MODULE_LICENSE("GPL");          /* combination of LGPL + GPL source behaves as GPL */
913 MODULE_DESCRIPTION
914     ("VFS to access servers complying with the SNIA CIFS Specification e.g. Samba and Windows");
915 MODULE_VERSION(CIFS_VERSION);
916 module_init(init_cifs)
917 module_exit(exit_cifs)