[CIFS] Fix minor build problem with previous changeset
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / cifs / cifsfs.c
blob99e087d3554fb1c4fed179fa68e102650ca65cf9
1 /*
2 * fs/cifs/cifsfs.c
4 * Copyright (C) International Business Machines Corp., 2002,2004
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Common Internet FileSystem (CIFS) client
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.
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.
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
24 /* Note that BB means BUGBUG (ie something to fix eventually) */
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 */
45 #ifdef CONFIG_CIFS_QUOTA
46 static struct quotactl_ops cifs_quotactl_ops;
47 #endif
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 extern struct task_struct * oplockThread; /* remove sparse warning */
61 struct task_struct * oplockThread = NULL;
62 extern struct task_struct * dnotifyThread; /* remove sparse warning */
63 struct task_struct * dnotifyThread = NULL;
64 unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
65 module_param(CIFSMaxBufSize, int, 0);
66 MODULE_PARM_DESC(CIFSMaxBufSize,"Network buffer size (not including header). Default: 16384 Range: 8192 to 130048");
67 unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
68 module_param(cifs_min_rcv, int, 0);
69 MODULE_PARM_DESC(cifs_min_rcv,"Network buffers in pool. Default: 4 Range: 1 to 64");
70 unsigned int cifs_min_small = 30;
71 module_param(cifs_min_small, int, 0);
72 MODULE_PARM_DESC(cifs_min_small,"Small network buffers in pool. Default: 30 Range: 2 to 256");
73 unsigned int cifs_max_pending = CIFS_MAX_REQ;
74 module_param(cifs_max_pending, int, 0);
75 MODULE_PARM_DESC(cifs_max_pending,"Simultaneous requests to server. Default: 50 Range: 2 to 256");
77 static DECLARE_COMPLETION(cifs_oplock_exited);
78 static DECLARE_COMPLETION(cifs_dnotify_exited);
80 extern mempool_t *cifs_sm_req_poolp;
81 extern mempool_t *cifs_req_poolp;
82 extern mempool_t *cifs_mid_poolp;
84 extern kmem_cache_t *cifs_oplock_cachep;
86 static int
87 cifs_read_super(struct super_block *sb, void *data,
88 const char *devname, int silent)
90 struct inode *inode;
91 struct cifs_sb_info *cifs_sb;
92 int rc = 0;
94 sb->s_flags |= MS_NODIRATIME; /* and probably even noatime */
95 sb->s_fs_info = kmalloc(sizeof(struct cifs_sb_info),GFP_KERNEL);
96 cifs_sb = CIFS_SB(sb);
97 if(cifs_sb == NULL)
98 return -ENOMEM;
99 else
100 memset(cifs_sb,0,sizeof(struct cifs_sb_info));
103 rc = cifs_mount(sb, cifs_sb, data, devname);
105 if (rc) {
106 if (!silent)
107 cERROR(1,
108 ("cifs_mount failed w/return code = %d", rc));
109 goto out_mount_failed;
112 sb->s_magic = CIFS_MAGIC_NUMBER;
113 sb->s_op = &cifs_super_ops;
114 /* if(cifs_sb->tcon->ses->server->maxBuf > MAX_CIFS_HDR_SIZE + 512)
115 sb->s_blocksize = cifs_sb->tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE; */
116 #ifdef CONFIG_CIFS_QUOTA
117 sb->s_qcop = &cifs_quotactl_ops;
118 #endif
119 sb->s_blocksize = CIFS_MAX_MSGSIZE;
120 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
121 inode = iget(sb, ROOT_I);
123 if (!inode) {
124 rc = -ENOMEM;
125 goto out_no_root;
128 sb->s_root = d_alloc_root(inode);
130 if (!sb->s_root) {
131 rc = -ENOMEM;
132 goto out_no_root;
135 return 0;
137 out_no_root:
138 cERROR(1, ("cifs_read_super: get root inode failed"));
139 if (inode)
140 iput(inode);
142 out_mount_failed:
143 if(cifs_sb) {
144 if(cifs_sb->local_nls)
145 unload_nls(cifs_sb->local_nls);
146 kfree(cifs_sb);
148 return rc;
151 static void
152 cifs_put_super(struct super_block *sb)
154 int rc = 0;
155 struct cifs_sb_info *cifs_sb;
157 cFYI(1, ("In cifs_put_super"));
158 cifs_sb = CIFS_SB(sb);
159 if(cifs_sb == NULL) {
160 cFYI(1,("Empty cifs superblock info passed to unmount"));
161 return;
163 rc = cifs_umount(sb, cifs_sb);
164 if (rc) {
165 cERROR(1, ("cifs_umount failed with return code %d", rc));
167 unload_nls(cifs_sb->local_nls);
168 kfree(cifs_sb);
169 return;
172 static int
173 cifs_statfs(struct super_block *sb, struct kstatfs *buf)
175 int xid;
176 int rc = -EOPNOTSUPP;
177 struct cifs_sb_info *cifs_sb;
178 struct cifsTconInfo *pTcon;
180 xid = GetXid();
182 cifs_sb = CIFS_SB(sb);
183 pTcon = cifs_sb->tcon;
185 buf->f_type = CIFS_MAGIC_NUMBER;
187 /* instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO */
188 buf->f_namelen = PATH_MAX; /* PATH_MAX may be too long - it would
189 presumably be total path, but note
190 that some servers (includinng Samba 3)
191 have a shorter maximum path */
192 buf->f_files = 0; /* undefined */
193 buf->f_ffree = 0; /* unlimited */
195 #ifdef CONFIG_CIFS_EXPERIMENTAL
196 /* BB we could add a second check for a QFS Unix capability bit */
197 /* BB FIXME check CIFS_POSIX_EXTENSIONS Unix cap first FIXME BB */
198 if ((pTcon->ses->capabilities & CAP_UNIX) && (CIFS_POSIX_EXTENSIONS &
199 le64_to_cpu(pTcon->fsUnixInfo.Capability)))
200 rc = CIFSSMBQFSPosixInfo(xid, pTcon, buf);
202 /* Only need to call the old QFSInfo if failed
203 on newer one */
204 if(rc)
205 #endif /* CIFS_EXPERIMENTAL */
206 rc = CIFSSMBQFSInfo(xid, pTcon, buf);
208 /* Old Windows servers do not support level 103, retry with level
209 one if old server failed the previous call */
210 if(rc)
211 rc = SMBOldQFSInfo(xid, pTcon, buf);
213 int f_type;
214 __fsid_t f_fsid;
215 int f_namelen; */
216 /* BB get from info in tcon struct at mount time call to QFSAttrInfo */
217 FreeXid(xid);
218 return 0; /* always return success? what if volume is no
219 longer available? */
222 static int cifs_permission(struct inode * inode, int mask, struct nameidata *nd)
224 struct cifs_sb_info *cifs_sb;
226 cifs_sb = CIFS_SB(inode->i_sb);
228 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
229 return 0;
230 } else /* file mode might have been restricted at mount time
231 on the client (above and beyond ACL on servers) for
232 servers which do not support setting and viewing mode bits,
233 so allowing client to check permissions is useful */
234 return generic_permission(inode, mask, NULL);
237 static kmem_cache_t *cifs_inode_cachep;
238 static kmem_cache_t *cifs_req_cachep;
239 static kmem_cache_t *cifs_mid_cachep;
240 kmem_cache_t *cifs_oplock_cachep;
241 static kmem_cache_t *cifs_sm_req_cachep;
242 mempool_t *cifs_sm_req_poolp;
243 mempool_t *cifs_req_poolp;
244 mempool_t *cifs_mid_poolp;
246 static struct inode *
247 cifs_alloc_inode(struct super_block *sb)
249 struct cifsInodeInfo *cifs_inode;
250 cifs_inode = kmem_cache_alloc(cifs_inode_cachep, SLAB_KERNEL);
251 if (!cifs_inode)
252 return NULL;
253 cifs_inode->cifsAttrs = 0x20; /* default */
254 atomic_set(&cifs_inode->inUse, 0);
255 cifs_inode->time = 0;
256 /* Until the file is open and we have gotten oplock
257 info back from the server, can not assume caching of
258 file data or metadata */
259 cifs_inode->clientCanCacheRead = FALSE;
260 cifs_inode->clientCanCacheAll = FALSE;
261 cifs_inode->vfs_inode.i_blksize = CIFS_MAX_MSGSIZE;
262 cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
263 cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;
264 INIT_LIST_HEAD(&cifs_inode->openFileList);
265 return &cifs_inode->vfs_inode;
268 static void
269 cifs_destroy_inode(struct inode *inode)
271 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
275 * cifs_show_options() is for displaying mount options in /proc/mounts.
276 * Not all settable options are displayed but most of the important
277 * ones are.
279 static int
280 cifs_show_options(struct seq_file *s, struct vfsmount *m)
282 struct cifs_sb_info *cifs_sb;
284 cifs_sb = CIFS_SB(m->mnt_sb);
286 if (cifs_sb) {
287 if (cifs_sb->tcon) {
288 seq_printf(s, ",unc=%s", cifs_sb->tcon->treeName);
289 if (cifs_sb->tcon->ses) {
290 if (cifs_sb->tcon->ses->userName)
291 seq_printf(s, ",username=%s",
292 cifs_sb->tcon->ses->userName);
293 if(cifs_sb->tcon->ses->domainName)
294 seq_printf(s, ",domain=%s",
295 cifs_sb->tcon->ses->domainName);
298 seq_printf(s, ",rsize=%d",cifs_sb->rsize);
299 seq_printf(s, ",wsize=%d",cifs_sb->wsize);
301 return 0;
304 #ifdef CONFIG_CIFS_QUOTA
305 int cifs_xquota_set(struct super_block * sb, int quota_type, qid_t qid,
306 struct fs_disk_quota * pdquota)
308 int xid;
309 int rc = 0;
310 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
311 struct cifsTconInfo *pTcon;
313 if(cifs_sb)
314 pTcon = cifs_sb->tcon;
315 else
316 return -EIO;
319 xid = GetXid();
320 if(pTcon) {
321 cFYI(1,("set type: 0x%x id: %d",quota_type,qid));
322 } else {
323 return -EIO;
326 FreeXid(xid);
327 return rc;
330 int cifs_xquota_get(struct super_block * sb, int quota_type, qid_t qid,
331 struct fs_disk_quota * pdquota)
333 int xid;
334 int rc = 0;
335 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
336 struct cifsTconInfo *pTcon;
338 if(cifs_sb)
339 pTcon = cifs_sb->tcon;
340 else
341 return -EIO;
343 xid = GetXid();
344 if(pTcon) {
345 cFYI(1,("set type: 0x%x id: %d",quota_type,qid));
346 } else {
347 rc = -EIO;
350 FreeXid(xid);
351 return rc;
354 int cifs_xstate_set(struct super_block * sb, unsigned int flags, int operation)
356 int xid;
357 int rc = 0;
358 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
359 struct cifsTconInfo *pTcon;
361 if(cifs_sb)
362 pTcon = cifs_sb->tcon;
363 else
364 return -EIO;
366 xid = GetXid();
367 if(pTcon) {
368 cFYI(1,("flags: 0x%x operation: 0x%x",flags,operation));
369 } else {
370 rc = -EIO;
373 FreeXid(xid);
374 return rc;
377 int cifs_xstate_get(struct super_block * sb, struct fs_quota_stat *qstats)
379 int xid;
380 int rc = 0;
381 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
382 struct cifsTconInfo *pTcon;
384 if(cifs_sb) {
385 pTcon = cifs_sb->tcon;
386 } else {
387 return -EIO;
389 xid = GetXid();
390 if(pTcon) {
391 cFYI(1,("pqstats %p",qstats));
392 } else {
393 rc = -EIO;
396 FreeXid(xid);
397 return rc;
400 static struct quotactl_ops cifs_quotactl_ops = {
401 .set_xquota = cifs_xquota_set,
402 .get_xquota = cifs_xquota_set,
403 .set_xstate = cifs_xstate_set,
404 .get_xstate = cifs_xstate_get,
406 #endif
408 static void cifs_umount_begin(struct super_block * sblock)
410 struct cifs_sb_info *cifs_sb;
411 struct cifsTconInfo * tcon;
413 cifs_sb = CIFS_SB(sb);
414 if(cifs_sb == NULL)
415 return;
417 tcon = cifs_sb->tcon;
418 if(tcon == NULL)
419 return;
420 down(&tcon->tconSem);
421 if (atomic_read(&tcon->useCount) == 1)
422 tcon->tidStatus = CifsExiting;
423 up(&tcon->tconSem);
425 if(tcon->ses && tcon->ses->server)
427 cERROR(1,("wake up tasks now - umount begin not complete"));
428 wake_up_all(&tcon->ses->server->request_q);
430 /* BB FIXME - finish add checks for tidStatus BB */
432 return;
436 static int cifs_remount(struct super_block *sb, int *flags, char *data)
438 *flags |= MS_NODIRATIME;
439 return 0;
442 struct super_operations cifs_super_ops = {
443 .read_inode = cifs_read_inode,
444 .put_super = cifs_put_super,
445 .statfs = cifs_statfs,
446 .alloc_inode = cifs_alloc_inode,
447 .destroy_inode = cifs_destroy_inode,
448 /* .drop_inode = generic_delete_inode,
449 .delete_inode = cifs_delete_inode, *//* Do not need the above two functions
450 unless later we add lazy close of inodes or unless the kernel forgets to call
451 us with the same number of releases (closes) as opens */
452 .show_options = cifs_show_options,
453 /* .umount_begin = cifs_umount_begin, */ /* BB finish in the future */
454 .remount_fs = cifs_remount,
457 static struct super_block *
458 cifs_get_sb(struct file_system_type *fs_type,
459 int flags, const char *dev_name, void *data)
461 int rc;
462 struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
464 cFYI(1, ("Devname: %s flags: %d ", dev_name, flags));
466 if (IS_ERR(sb))
467 return sb;
469 sb->s_flags = flags;
471 rc = cifs_read_super(sb, data, dev_name, flags & MS_VERBOSE ? 1 : 0);
472 if (rc) {
473 up_write(&sb->s_umount);
474 deactivate_super(sb);
475 return ERR_PTR(rc);
477 sb->s_flags |= MS_ACTIVE;
478 return sb;
481 static ssize_t
482 cifs_read_wrapper(struct file * file, char __user *read_data, size_t read_size,
483 loff_t * poffset)
485 if(file->f_dentry == NULL)
486 return -EIO;
487 else if(file->f_dentry->d_inode == NULL)
488 return -EIO;
490 cFYI(1,("In read_wrapper size %zd at %lld",read_size,*poffset));
492 if(CIFS_I(file->f_dentry->d_inode)->clientCanCacheRead) {
493 return generic_file_read(file,read_data,read_size,poffset);
494 } else {
495 /* BB do we need to lock inode from here until after invalidate? */
496 /* if(file->f_dentry->d_inode->i_mapping) {
497 filemap_fdatawrite(file->f_dentry->d_inode->i_mapping);
498 filemap_fdatawait(file->f_dentry->d_inode->i_mapping);
500 /* cifs_revalidate(file->f_dentry);*/ /* BB fixme */
502 /* BB we should make timer configurable - perhaps
503 by simply calling cifs_revalidate here */
504 /* invalidate_remote_inode(file->f_dentry->d_inode);*/
505 return generic_file_read(file,read_data,read_size,poffset);
509 static ssize_t
510 cifs_write_wrapper(struct file * file, const char __user *write_data,
511 size_t write_size, loff_t * poffset)
513 ssize_t written;
515 if(file->f_dentry == NULL)
516 return -EIO;
517 else if(file->f_dentry->d_inode == NULL)
518 return -EIO;
520 cFYI(1,("In write_wrapper size %zd at %lld",write_size,*poffset));
522 written = generic_file_write(file,write_data,write_size,poffset);
523 if(!CIFS_I(file->f_dentry->d_inode)->clientCanCacheAll) {
524 if(file->f_dentry->d_inode->i_mapping) {
525 filemap_fdatawrite(file->f_dentry->d_inode->i_mapping);
528 return written;
532 static struct file_system_type cifs_fs_type = {
533 .owner = THIS_MODULE,
534 .name = "cifs",
535 .get_sb = cifs_get_sb,
536 .kill_sb = kill_anon_super,
537 /* .fs_flags */
539 struct inode_operations cifs_dir_inode_ops = {
540 .create = cifs_create,
541 .lookup = cifs_lookup,
542 .getattr = cifs_getattr,
543 .unlink = cifs_unlink,
544 .link = cifs_hardlink,
545 .mkdir = cifs_mkdir,
546 .rmdir = cifs_rmdir,
547 .rename = cifs_rename,
548 .permission = cifs_permission,
549 /* revalidate:cifs_revalidate, */
550 .setattr = cifs_setattr,
551 .symlink = cifs_symlink,
552 .mknod = cifs_mknod,
553 #ifdef CONFIG_CIFS_XATTR
554 .setxattr = cifs_setxattr,
555 .getxattr = cifs_getxattr,
556 .listxattr = cifs_listxattr,
557 .removexattr = cifs_removexattr,
558 #endif
561 struct inode_operations cifs_file_inode_ops = {
562 /* revalidate:cifs_revalidate, */
563 .setattr = cifs_setattr,
564 .getattr = cifs_getattr, /* do we need this anymore? */
565 .rename = cifs_rename,
566 .permission = cifs_permission,
567 #ifdef CONFIG_CIFS_XATTR
568 .setxattr = cifs_setxattr,
569 .getxattr = cifs_getxattr,
570 .listxattr = cifs_listxattr,
571 .removexattr = cifs_removexattr,
572 #endif
575 struct inode_operations cifs_symlink_inode_ops = {
576 .readlink = generic_readlink,
577 .follow_link = cifs_follow_link,
578 .put_link = cifs_put_link,
579 .permission = cifs_permission,
580 /* BB add the following two eventually */
581 /* revalidate: cifs_revalidate,
582 setattr: cifs_notify_change, *//* BB do we need notify change */
583 #ifdef CONFIG_CIFS_XATTR
584 .setxattr = cifs_setxattr,
585 .getxattr = cifs_getxattr,
586 .listxattr = cifs_listxattr,
587 .removexattr = cifs_removexattr,
588 #endif
591 struct file_operations cifs_file_ops = {
592 .read = cifs_read_wrapper,
593 .write = cifs_write_wrapper,
594 .open = cifs_open,
595 .release = cifs_close,
596 .lock = cifs_lock,
597 .fsync = cifs_fsync,
598 .flush = cifs_flush,
599 .mmap = cifs_file_mmap,
600 .sendfile = generic_file_sendfile,
601 #ifdef CONFIG_CIFS_POSIX
602 .ioctl = cifs_ioctl,
603 #endif /* CONFIG_CIFS_POSIX */
605 #ifdef CONFIG_CIFS_EXPERIMENTAL
606 .readv = generic_file_readv,
607 .writev = generic_file_writev,
608 .aio_read = generic_file_aio_read,
609 .aio_write = generic_file_aio_write,
610 .dir_notify = cifs_dir_notify,
611 #endif /* CONFIG_CIFS_EXPERIMENTAL */
614 struct file_operations cifs_file_direct_ops = {
615 /* no mmap, no aio, no readv -
616 BB reevaluate whether they can be done with directio, no cache */
617 .read = cifs_user_read,
618 .write = cifs_user_write,
619 .open = cifs_open,
620 .release = cifs_close,
621 .lock = cifs_lock,
622 .fsync = cifs_fsync,
623 .flush = cifs_flush,
624 .sendfile = generic_file_sendfile, /* BB removeme BB */
625 #ifdef CONFIG_CIFS_POSIX
626 .ioctl = cifs_ioctl,
627 #endif /* CONFIG_CIFS_POSIX */
629 #ifdef CONFIG_CIFS_EXPERIMENTAL
630 .dir_notify = cifs_dir_notify,
631 #endif /* CONFIG_CIFS_EXPERIMENTAL */
634 struct file_operations cifs_dir_ops = {
635 .readdir = cifs_readdir,
636 .release = cifs_closedir,
637 .read = generic_read_dir,
638 #ifdef CONFIG_CIFS_EXPERIMENTAL
639 .dir_notify = cifs_dir_notify,
640 #endif /* CONFIG_CIFS_EXPERIMENTAL */
641 .ioctl = cifs_ioctl,
644 static void
645 cifs_init_once(void *inode, kmem_cache_t * cachep, unsigned long flags)
647 struct cifsInodeInfo *cifsi = inode;
649 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
650 SLAB_CTOR_CONSTRUCTOR) {
651 inode_init_once(&cifsi->vfs_inode);
652 INIT_LIST_HEAD(&cifsi->lockList);
656 static int
657 cifs_init_inodecache(void)
659 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
660 sizeof (struct cifsInodeInfo),
661 0, SLAB_RECLAIM_ACCOUNT,
662 cifs_init_once, NULL);
663 if (cifs_inode_cachep == NULL)
664 return -ENOMEM;
666 return 0;
669 static void
670 cifs_destroy_inodecache(void)
672 if (kmem_cache_destroy(cifs_inode_cachep))
673 printk(KERN_WARNING "cifs_inode_cache: error freeing\n");
676 static int
677 cifs_init_request_bufs(void)
679 if(CIFSMaxBufSize < 8192) {
680 /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
681 Unicode path name has to fit in any SMB/CIFS path based frames */
682 CIFSMaxBufSize = 8192;
683 } else if (CIFSMaxBufSize > 1024*127) {
684 CIFSMaxBufSize = 1024 * 127;
685 } else {
686 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
688 /* cERROR(1,("CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize)); */
689 cifs_req_cachep = kmem_cache_create("cifs_request",
690 CIFSMaxBufSize +
691 MAX_CIFS_HDR_SIZE, 0,
692 SLAB_HWCACHE_ALIGN, NULL, NULL);
693 if (cifs_req_cachep == NULL)
694 return -ENOMEM;
696 if(cifs_min_rcv < 1)
697 cifs_min_rcv = 1;
698 else if (cifs_min_rcv > 64) {
699 cifs_min_rcv = 64;
700 cERROR(1,("cifs_min_rcv set to maximum (64)"));
703 cifs_req_poolp = mempool_create(cifs_min_rcv,
704 mempool_alloc_slab,
705 mempool_free_slab,
706 cifs_req_cachep);
708 if(cifs_req_poolp == NULL) {
709 kmem_cache_destroy(cifs_req_cachep);
710 return -ENOMEM;
712 /* 256 (MAX_CIFS_HDR_SIZE bytes is enough for most SMB responses and
713 almost all handle based requests (but not write response, nor is it
714 sufficient for path based requests). A smaller size would have
715 been more efficient (compacting multiple slab items on one 4k page)
716 for the case in which debug was on, but this larger size allows
717 more SMBs to use small buffer alloc and is still much more
718 efficient to alloc 1 per page off the slab compared to 17K (5page)
719 alloc of large cifs buffers even when page debugging is on */
720 cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
721 MAX_CIFS_HDR_SIZE, 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
722 if (cifs_sm_req_cachep == NULL) {
723 mempool_destroy(cifs_req_poolp);
724 kmem_cache_destroy(cifs_req_cachep);
725 return -ENOMEM;
728 if(cifs_min_small < 2)
729 cifs_min_small = 2;
730 else if (cifs_min_small > 256) {
731 cifs_min_small = 256;
732 cFYI(1,("cifs_min_small set to maximum (256)"));
735 cifs_sm_req_poolp = mempool_create(cifs_min_small,
736 mempool_alloc_slab,
737 mempool_free_slab,
738 cifs_sm_req_cachep);
740 if(cifs_sm_req_poolp == NULL) {
741 mempool_destroy(cifs_req_poolp);
742 kmem_cache_destroy(cifs_req_cachep);
743 kmem_cache_destroy(cifs_sm_req_cachep);
744 return -ENOMEM;
747 return 0;
750 static void
751 cifs_destroy_request_bufs(void)
753 mempool_destroy(cifs_req_poolp);
754 if (kmem_cache_destroy(cifs_req_cachep))
755 printk(KERN_WARNING
756 "cifs_destroy_request_cache: error not all structures were freed\n");
757 mempool_destroy(cifs_sm_req_poolp);
758 if (kmem_cache_destroy(cifs_sm_req_cachep))
759 printk(KERN_WARNING
760 "cifs_destroy_request_cache: cifs_small_rq free error\n");
763 static int
764 cifs_init_mids(void)
766 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
767 sizeof (struct mid_q_entry), 0,
768 SLAB_HWCACHE_ALIGN, NULL, NULL);
769 if (cifs_mid_cachep == NULL)
770 return -ENOMEM;
772 cifs_mid_poolp = mempool_create(3 /* a reasonable min simultan opers */,
773 mempool_alloc_slab,
774 mempool_free_slab,
775 cifs_mid_cachep);
776 if(cifs_mid_poolp == NULL) {
777 kmem_cache_destroy(cifs_mid_cachep);
778 return -ENOMEM;
781 cifs_oplock_cachep = kmem_cache_create("cifs_oplock_structs",
782 sizeof (struct oplock_q_entry), 0,
783 SLAB_HWCACHE_ALIGN, NULL, NULL);
784 if (cifs_oplock_cachep == NULL) {
785 kmem_cache_destroy(cifs_mid_cachep);
786 mempool_destroy(cifs_mid_poolp);
787 return -ENOMEM;
790 return 0;
793 static void
794 cifs_destroy_mids(void)
796 mempool_destroy(cifs_mid_poolp);
797 if (kmem_cache_destroy(cifs_mid_cachep))
798 printk(KERN_WARNING
799 "cifs_destroy_mids: error not all structures were freed\n");
801 if (kmem_cache_destroy(cifs_oplock_cachep))
802 printk(KERN_WARNING
803 "error not all oplock structures were freed\n");
806 static int cifs_oplock_thread(void * dummyarg)
808 struct oplock_q_entry * oplock_item;
809 struct cifsTconInfo *pTcon;
810 struct inode * inode;
811 __u16 netfid;
812 int rc;
814 daemonize("cifsoplockd");
815 allow_signal(SIGTERM);
817 oplockThread = current;
818 do {
819 if(try_to_freeze())
820 continue;
822 spin_lock(&GlobalMid_Lock);
823 if(list_empty(&GlobalOplock_Q)) {
824 spin_unlock(&GlobalMid_Lock);
825 set_current_state(TASK_INTERRUPTIBLE);
826 schedule_timeout(39*HZ);
827 } else {
828 oplock_item = list_entry(GlobalOplock_Q.next,
829 struct oplock_q_entry, qhead);
830 if(oplock_item) {
831 cFYI(1,("found oplock item to write out"));
832 pTcon = oplock_item->tcon;
833 inode = oplock_item->pinode;
834 netfid = oplock_item->netfid;
835 spin_unlock(&GlobalMid_Lock);
836 DeleteOplockQEntry(oplock_item);
837 /* can not grab inode sem here since it would
838 deadlock when oplock received on delete
839 since vfs_unlink holds the i_sem across
840 the call */
841 /* down(&inode->i_sem);*/
842 if (S_ISREG(inode->i_mode)) {
843 rc = filemap_fdatawrite(inode->i_mapping);
844 if(CIFS_I(inode)->clientCanCacheRead == 0) {
845 filemap_fdatawait(inode->i_mapping);
846 invalidate_remote_inode(inode);
848 } else
849 rc = 0;
850 /* up(&inode->i_sem);*/
851 if (rc)
852 CIFS_I(inode)->write_behind_rc = rc;
853 cFYI(1,("Oplock flush inode %p rc %d",inode,rc));
855 /* releasing a stale oplock after recent reconnection
856 of smb session using a now incorrect file
857 handle is not a data integrity issue but do
858 not bother sending an oplock release if session
859 to server still is disconnected since oplock
860 already released by the server in that case */
861 if(pTcon->tidStatus != CifsNeedReconnect) {
862 rc = CIFSSMBLock(0, pTcon, netfid,
863 0 /* len */ , 0 /* offset */, 0,
864 0, LOCKING_ANDX_OPLOCK_RELEASE,
865 0 /* wait flag */);
866 cFYI(1,("Oplock release rc = %d ",rc));
868 } else
869 spin_unlock(&GlobalMid_Lock);
870 set_current_state(TASK_INTERRUPTIBLE);
871 schedule_timeout(1); /* yield in case q were corrupt */
873 } while(!signal_pending(current));
874 oplockThread = NULL;
875 complete_and_exit (&cifs_oplock_exited, 0);
878 static int cifs_dnotify_thread(void * dummyarg)
880 daemonize("cifsdnotifyd");
881 allow_signal(SIGTERM);
883 dnotifyThread = current;
884 do {
885 if(try_to_freeze())
886 continue;
887 set_current_state(TASK_INTERRUPTIBLE);
888 schedule_timeout(39*HZ);
889 } while(!signal_pending(current));
890 complete_and_exit (&cifs_dnotify_exited, 0);
893 static int __init
894 init_cifs(void)
896 int rc = 0;
897 #ifdef CONFIG_PROC_FS
898 cifs_proc_init();
899 #endif
900 INIT_LIST_HEAD(&GlobalServerList); /* BB not implemented yet */
901 INIT_LIST_HEAD(&GlobalSMBSessionList);
902 INIT_LIST_HEAD(&GlobalTreeConnectionList);
903 INIT_LIST_HEAD(&GlobalOplock_Q);
905 * Initialize Global counters
907 atomic_set(&sesInfoAllocCount, 0);
908 atomic_set(&tconInfoAllocCount, 0);
909 atomic_set(&tcpSesAllocCount,0);
910 atomic_set(&tcpSesReconnectCount, 0);
911 atomic_set(&tconInfoReconnectCount, 0);
913 atomic_set(&bufAllocCount, 0);
914 atomic_set(&midCount, 0);
915 GlobalCurrentXid = 0;
916 GlobalTotalActiveXid = 0;
917 GlobalMaxActiveXid = 0;
918 rwlock_init(&GlobalSMBSeslock);
919 spin_lock_init(&GlobalMid_Lock);
921 if(cifs_max_pending < 2) {
922 cifs_max_pending = 2;
923 cFYI(1,("cifs_max_pending set to min of 2"));
924 } else if(cifs_max_pending > 256) {
925 cifs_max_pending = 256;
926 cFYI(1,("cifs_max_pending set to max of 256"));
929 rc = cifs_init_inodecache();
930 if (!rc) {
931 rc = cifs_init_mids();
932 if (!rc) {
933 rc = cifs_init_request_bufs();
934 if (!rc) {
935 rc = register_filesystem(&cifs_fs_type);
936 if (!rc) {
937 rc = (int)kernel_thread(cifs_oplock_thread, NULL,
938 CLONE_FS | CLONE_FILES | CLONE_VM);
939 if(rc > 0) {
940 rc = (int)kernel_thread(cifs_dnotify_thread, NULL,
941 CLONE_FS | CLONE_FILES | CLONE_VM);
942 if(rc > 0)
943 return 0;
944 else
945 cERROR(1,("error %d create dnotify thread", rc));
946 } else {
947 cERROR(1,("error %d create oplock thread",rc));
950 cifs_destroy_request_bufs();
952 cifs_destroy_mids();
954 cifs_destroy_inodecache();
956 #ifdef CONFIG_PROC_FS
957 cifs_proc_clean();
958 #endif
959 return rc;
962 static void __exit
963 exit_cifs(void)
965 cFYI(0, ("In unregister ie exit_cifs"));
966 #ifdef CONFIG_PROC_FS
967 cifs_proc_clean();
968 #endif
969 unregister_filesystem(&cifs_fs_type);
970 cifs_destroy_inodecache();
971 cifs_destroy_mids();
972 cifs_destroy_request_bufs();
973 if(oplockThread) {
974 send_sig(SIGTERM, oplockThread, 1);
975 wait_for_completion(&cifs_oplock_exited);
977 if(dnotifyThread) {
978 send_sig(SIGTERM, dnotifyThread, 1);
979 wait_for_completion(&cifs_dnotify_exited);
983 MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
984 MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
985 MODULE_DESCRIPTION
986 ("VFS to access servers complying with the SNIA CIFS Specification e.g. Samba and Windows");
987 MODULE_VERSION(CIFS_VERSION);
988 module_init(init_cifs)
989 module_exit(exit_cifs)