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>
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>
37 #define DECLARE_GLOBALS_HERE
39 #include "cifsproto.h"
40 #include "cifs_debug.h"
41 #include "cifs_fs_sb.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;
52 unsigned int oplockEnabled = 1;
53 unsigned int quotaEnabled = 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 unsigned int CIFSMaximumBufferSize = CIFS_MAX_MSGSIZE;
61 struct task_struct * oplockThread = NULL;
63 extern int cifs_mount(struct super_block *, struct cifs_sb_info *, char *,
65 extern int cifs_umount(struct super_block *, struct cifs_sb_info *);
66 void cifs_proc_init(void);
67 void cifs_proc_clean(void);
69 static DECLARE_COMPLETION(cifs_oplock_exited);
73 cifs_read_super(struct super_block *sb, void *data,
74 const char *devname, int silent)
77 struct cifs_sb_info *cifs_sb;
80 sb->s_flags |= MS_NODIRATIME; /* and probably even noatime */
81 sb->s_fs_info = kmalloc(sizeof(struct cifs_sb_info),GFP_KERNEL);
82 cifs_sb = CIFS_SB(sb);
86 memset(cifs_sb,0,sizeof(struct cifs_sb_info));
89 rc = cifs_mount(sb, cifs_sb, data, devname);
94 ("cifs_mount failed w/return code = %d", rc));
95 goto out_mount_failed;
98 sb->s_magic = CIFS_MAGIC_NUMBER;
99 sb->s_op = &cifs_super_ops;
100 /* if(cifs_sb->tcon->ses->server->maxBuf > MAX_CIFS_HDR_SIZE + 512)
101 sb->s_blocksize = cifs_sb->tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE; */
102 #ifdef CONFIG_CIFS_QUOTA
103 sb->s_qcop = &cifs_quotactl_ops;
105 sb->s_blocksize = CIFS_MAX_MSGSIZE;
106 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
107 inode = iget(sb, ROOT_I);
114 sb->s_root = d_alloc_root(inode);
124 cERROR(1, ("cifs_read_super: get root inode failed"));
130 if(cifs_sb->local_nls)
131 unload_nls(cifs_sb->local_nls);
138 cifs_put_super(struct super_block *sb)
141 struct cifs_sb_info *cifs_sb;
143 cFYI(1, ("In cifs_put_super"));
144 cifs_sb = CIFS_SB(sb);
145 if(cifs_sb == NULL) {
146 cFYI(1,("Empty cifs superblock info passed to unmount"));
149 rc = cifs_umount(sb, cifs_sb);
151 cERROR(1, ("cifs_umount failed with return code %d", rc));
153 unload_nls(cifs_sb->local_nls);
159 cifs_statfs(struct super_block *sb, struct kstatfs *buf)
162 struct cifs_sb_info *cifs_sb;
163 struct cifsTconInfo *pTcon;
167 cifs_sb = CIFS_SB(sb);
168 pTcon = cifs_sb->tcon;
170 buf->f_type = CIFS_MAGIC_NUMBER;
172 /* instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO */
173 buf->f_namelen = PATH_MAX; /* PATH_MAX may be too long - it would presumably
174 be length of total path, note that some servers may be
175 able to support more than this, but best to be safe
176 since Win2k and others can not handle very long filenames */
177 buf->f_files = 0; /* undefined */
178 buf->f_ffree = 0; /* unlimited */
180 rc = CIFSSMBQFSInfo(xid, pTcon, buf, cifs_sb->local_nls);
186 /* BB get from info put in tcon struct at mount time with call to QFSAttrInfo */
188 return 0; /* always return success? what if volume is no longer available? */
191 static int cifs_permission(struct inode * inode, int mask, struct nameidata *nd)
193 struct cifs_sb_info *cifs_sb;
195 cifs_sb = CIFS_SB(inode->i_sb);
197 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
199 } else /* file mode might have been restricted at mount time
200 on the client (above and beyond ACL on servers) for
201 servers which do not support setting and viewing mode bits,
202 so allowing client to check permissions is useful */
203 return vfs_permission(inode, mask);
206 static kmem_cache_t *cifs_inode_cachep;
207 static kmem_cache_t *cifs_req_cachep;
208 static kmem_cache_t *cifs_mid_cachep;
209 kmem_cache_t *cifs_oplock_cachep;
210 mempool_t *cifs_req_poolp;
211 mempool_t *cifs_mid_poolp;
213 static struct inode *
214 cifs_alloc_inode(struct super_block *sb)
216 struct cifsInodeInfo *cifs_inode;
218 (struct cifsInodeInfo *) kmem_cache_alloc(cifs_inode_cachep,
222 cifs_inode->cifsAttrs = 0x20; /* default */
223 atomic_set(&cifs_inode->inUse, 0);
224 cifs_inode->time = 0;
225 /* Until the file is open and we have gotten oplock
226 info back from the server, can not assume caching of
227 file data or metadata */
228 cifs_inode->clientCanCacheRead = FALSE;
229 cifs_inode->clientCanCacheAll = FALSE;
230 cifs_inode->vfs_inode.i_blksize = CIFS_MAX_MSGSIZE;
231 cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
233 INIT_LIST_HEAD(&cifs_inode->openFileList);
234 return &cifs_inode->vfs_inode;
238 cifs_destroy_inode(struct inode *inode)
240 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
244 * cifs_show_options() is for displaying mount options in /proc/mounts.
245 * Not all settable options are displayed but most of the important
249 cifs_show_options(struct seq_file *s, struct vfsmount *m)
251 struct cifs_sb_info *cifs_sb;
253 cifs_sb = CIFS_SB(m->mnt_sb);
257 seq_printf(s, ",unc=%s", cifs_sb->tcon->treeName);
258 if ((cifs_sb->tcon->ses) && (cifs_sb->tcon->ses->userName))
259 seq_printf(s, ",username=%s",
260 cifs_sb->tcon->ses->userName);
261 if(cifs_sb->tcon->ses->domainName)
262 seq_printf(s, ",domain=%s",
263 cifs_sb->tcon->ses->domainName);
265 seq_printf(s, ",rsize=%d",cifs_sb->rsize);
266 seq_printf(s, ",wsize=%d",cifs_sb->wsize);
271 #ifdef CONFIG_CIFS_QUOTA
272 int cifs_xquota_set(struct super_block * sb, int quota_type, qid_t qid,
273 struct fs_disk_quota * pdquota)
277 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
278 struct cifsTconInfo *pTcon;
281 pTcon = cifs_sb->tcon;
288 cFYI(1,("set type: 0x%x id: %d",quota_type,qid));
297 int cifs_xquota_get(struct super_block * sb, int quota_type, qid_t qid,
298 struct fs_disk_quota * pdquota)
302 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
303 struct cifsTconInfo *pTcon;
306 pTcon = cifs_sb->tcon;
312 cFYI(1,("set type: 0x%x id: %d",quota_type,qid));
321 int cifs_xstate_set(struct super_block * sb, unsigned int flags, int operation)
325 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
326 struct cifsTconInfo *pTcon;
329 pTcon = cifs_sb->tcon;
335 cFYI(1,("flags: 0x%x operation: 0x%x",flags,operation));
344 int cifs_xstate_get(struct super_block * sb, struct fs_quota_stat *qstats)
348 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
349 struct cifsTconInfo *pTcon;
352 pTcon = cifs_sb->tcon;
358 cFYI(1,("pqstats %p",qstats));
367 static struct quotactl_ops cifs_quotactl_ops = {
368 .set_xquota = cifs_xquota_set,
369 .get_xquota = cifs_xquota_set,
370 .set_xstate = cifs_xstate_set,
371 .get_xstate = cifs_xstate_get,
375 static int cifs_remount(struct super_block *sb, int *flags, char *data)
377 *flags |= MS_NODIRATIME;
381 struct super_operations cifs_super_ops = {
382 .read_inode = cifs_read_inode,
383 .put_super = cifs_put_super,
384 .statfs = cifs_statfs,
385 .alloc_inode = cifs_alloc_inode,
386 .destroy_inode = cifs_destroy_inode,
387 /* .drop_inode = generic_delete_inode,
388 .delete_inode = cifs_delete_inode, *//* Do not need the above two functions
389 unless later we add lazy close of inodes or unless the kernel forgets to call
390 us with the same number of releases (closes) as opens */
391 .show_options = cifs_show_options,
392 /* .umount_begin = cifs_umount_begin, *//* consider adding in the future */
393 .remount_fs = cifs_remount,
396 static struct super_block *
397 cifs_get_sb(struct file_system_type *fs_type,
398 int flags, const char *dev_name, void *data)
401 struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
403 cFYI(1, ("Devname: %s flags: %d ", dev_name, flags));
410 rc = cifs_read_super(sb, data, dev_name, flags & MS_VERBOSE ? 1 : 0);
412 up_write(&sb->s_umount);
413 deactivate_super(sb);
416 sb->s_flags |= MS_ACTIVE;
421 cifs_read_wrapper(struct file * file, char __user *read_data, size_t read_size,
426 else if(file->f_dentry == NULL)
428 else if(file->f_dentry->d_inode == NULL)
431 cFYI(1,("In read_wrapper size %zd at %lld",read_size,*poffset));
432 if(CIFS_I(file->f_dentry->d_inode)->clientCanCacheRead) {
433 return generic_file_read(file,read_data,read_size,poffset);
435 /* BB do we need to lock inode from here until after invalidate? */
436 /* if(file->f_dentry->d_inode->i_mapping) {
437 filemap_fdatawrite(file->f_dentry->d_inode->i_mapping);
438 filemap_fdatawait(file->f_dentry->d_inode->i_mapping);
440 /* cifs_revalidate(file->f_dentry);*/ /* BB fixme */
442 /* BB we should make timer configurable - perhaps
443 by simply calling cifs_revalidate here */
444 /* invalidate_remote_inode(file->f_dentry->d_inode);*/
445 return generic_file_read(file,read_data,read_size,poffset);
450 cifs_write_wrapper(struct file * file, const char __user *write_data,
451 size_t write_size, loff_t * poffset)
457 else if(file->f_dentry == NULL)
459 else if(file->f_dentry->d_inode == NULL)
462 cFYI(1,("In write_wrapper size %zd at %lld",write_size,*poffset));
464 /* check whether we can cache writes locally */
465 written = generic_file_write(file,write_data,write_size,poffset);
466 if(!CIFS_I(file->f_dentry->d_inode)->clientCanCacheAll) {
467 if(file->f_dentry->d_inode->i_mapping) {
468 filemap_fdatawrite(file->f_dentry->d_inode->i_mapping);
475 static struct file_system_type cifs_fs_type = {
476 .owner = THIS_MODULE,
478 .get_sb = cifs_get_sb,
479 .kill_sb = kill_anon_super,
482 struct inode_operations cifs_dir_inode_ops = {
483 .create = cifs_create,
484 .lookup = cifs_lookup,
485 .getattr = cifs_getattr,
486 .unlink = cifs_unlink,
487 .link = cifs_hardlink,
490 .rename = cifs_rename,
491 .permission = cifs_permission,
492 /* revalidate:cifs_revalidate, */
493 .setattr = cifs_setattr,
494 .symlink = cifs_symlink,
498 struct inode_operations cifs_file_inode_ops = {
499 /* revalidate:cifs_revalidate, */
500 .setattr = cifs_setattr,
501 .getattr = cifs_getattr, /* do we need this anymore? */
502 .rename = cifs_rename,
503 .permission = cifs_permission,
504 #ifdef CONFIG_CIFS_XATTR
505 .setxattr = cifs_setxattr,
506 .getxattr = cifs_getxattr,
507 .listxattr = cifs_listxattr,
508 .removexattr = cifs_removexattr,
512 struct inode_operations cifs_symlink_inode_ops = {
513 .readlink = cifs_readlink,
514 .follow_link = cifs_follow_link,
515 .put_link = cifs_put_link,
516 .permission = cifs_permission,
517 /* BB add the following two eventually */
518 /* revalidate: cifs_revalidate,
519 setattr: cifs_notify_change, *//* BB do we need notify change */
520 #ifdef CONFIG_CIFS_XATTR
521 .setxattr = cifs_setxattr,
522 .getxattr = cifs_getxattr,
523 .listxattr = cifs_listxattr,
524 .removexattr = cifs_removexattr,
528 struct file_operations cifs_file_ops = {
529 .read = cifs_read_wrapper,
530 .write = cifs_write_wrapper,
532 .release = cifs_close,
536 .mmap = cifs_file_mmap,
537 .sendfile = generic_file_sendfile,
538 .dir_notify = cifs_dir_notify,
541 struct file_operations cifs_dir_ops = {
542 .readdir = cifs_readdir,
543 .release = cifs_closedir,
544 .read = generic_read_dir,
545 .dir_notify = cifs_dir_notify,
549 cifs_init_once(void *inode, kmem_cache_t * cachep, unsigned long flags)
551 struct cifsInodeInfo *cifsi = (struct cifsInodeInfo *) inode;
553 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
554 SLAB_CTOR_CONSTRUCTOR) {
555 inode_init_once(&cifsi->vfs_inode);
556 INIT_LIST_HEAD(&cifsi->lockList);
561 cifs_init_inodecache(void)
563 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
564 sizeof (struct cifsInodeInfo),
565 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
566 cifs_init_once, NULL);
567 if (cifs_inode_cachep == NULL)
574 cifs_destroy_inodecache(void)
576 if (kmem_cache_destroy(cifs_inode_cachep))
577 printk(KERN_WARNING "cifs_inode_cache: error freeing\n");
581 cifs_init_request_bufs(void)
583 cifs_req_cachep = kmem_cache_create("cifs_request",
585 MAX_CIFS_HDR_SIZE, 0,
586 SLAB_HWCACHE_ALIGN, NULL, NULL);
587 if (cifs_req_cachep == NULL)
590 cifs_req_poolp = mempool_create(CIFS_MIN_RCV_POOL,
595 if(cifs_req_poolp == NULL) {
596 kmem_cache_destroy(cifs_req_cachep);
604 cifs_destroy_request_bufs(void)
606 mempool_destroy(cifs_req_poolp);
607 if (kmem_cache_destroy(cifs_req_cachep))
609 "cifs_destroy_request_cache: error not all structures were freed\n");
615 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
616 sizeof (struct mid_q_entry), 0,
617 SLAB_HWCACHE_ALIGN, NULL, NULL);
618 if (cifs_mid_cachep == NULL)
621 cifs_mid_poolp = mempool_create(3 /* a reasonable min simultan opers */,
625 if(cifs_mid_poolp == NULL) {
626 kmem_cache_destroy(cifs_mid_cachep);
630 cifs_oplock_cachep = kmem_cache_create("cifs_oplock_structs",
631 sizeof (struct oplock_q_entry), 0,
632 SLAB_HWCACHE_ALIGN, NULL, NULL);
633 if (cifs_oplock_cachep == NULL) {
634 kmem_cache_destroy(cifs_mid_cachep);
635 mempool_destroy(cifs_mid_poolp);
643 cifs_destroy_mids(void)
645 mempool_destroy(cifs_mid_poolp);
646 if (kmem_cache_destroy(cifs_mid_cachep))
648 "cifs_destroy_mids: error not all structures were freed\n");
650 if (kmem_cache_destroy(cifs_oplock_cachep))
652 "error not all oplock structures were freed\n");
655 static int cifs_oplock_thread(void * dummyarg)
657 struct oplock_q_entry * oplock_item;
658 struct cifsTconInfo *pTcon;
659 struct inode * inode;
663 daemonize("cifsoplockd");
664 allow_signal(SIGTERM);
666 oplockThread = current;
668 set_current_state(TASK_INTERRUPTIBLE);
670 schedule_timeout(1*HZ);
671 spin_lock(&GlobalMid_Lock);
672 if(list_empty(&GlobalOplock_Q)) {
673 spin_unlock(&GlobalMid_Lock);
674 set_current_state(TASK_INTERRUPTIBLE);
675 schedule_timeout(39*HZ);
677 oplock_item = list_entry(GlobalOplock_Q.next,
678 struct oplock_q_entry, qhead);
680 cFYI(1,("found oplock item to write out"));
681 pTcon = oplock_item->tcon;
682 inode = oplock_item->pinode;
683 netfid = oplock_item->netfid;
684 spin_unlock(&GlobalMid_Lock);
685 DeleteOplockQEntry(oplock_item);
686 /* can not grab inode sem here since it would
687 deadlock when oplock received on delete
688 since vfs_unlink holds the i_sem across
690 /* down(&inode->i_sem);*/
691 if (S_ISREG(inode->i_mode)) {
692 rc = filemap_fdatawrite(inode->i_mapping);
693 if(CIFS_I(inode)->clientCanCacheRead == 0) {
694 filemap_fdatawait(inode->i_mapping);
695 invalidate_remote_inode(inode);
699 /* up(&inode->i_sem);*/
701 CIFS_I(inode)->write_behind_rc = rc;
702 cFYI(1,("Oplock flush inode %p rc %d",inode,rc));
704 /* releasing a stale oplock after recent reconnection
705 of smb session using a now incorrect file
706 handle is not a data integrity issue but do
707 not bother sending an oplock release if session
708 to server still is disconnected since oplock
709 already released by the server in that case */
710 if(pTcon->tidStatus != CifsNeedReconnect) {
711 rc = CIFSSMBLock(0, pTcon, netfid,
712 0 /* len */ , 0 /* offset */, 0,
713 0, LOCKING_ANDX_OPLOCK_RELEASE,
715 cFYI(1,("Oplock release rc = %d ",rc));
718 spin_unlock(&GlobalMid_Lock);
720 } while(!signal_pending(current));
721 complete_and_exit (&cifs_oplock_exited, 0);
728 #ifdef CONFIG_PROC_FS
731 INIT_LIST_HEAD(&GlobalServerList); /* BB not implemented yet */
732 INIT_LIST_HEAD(&GlobalSMBSessionList);
733 INIT_LIST_HEAD(&GlobalTreeConnectionList);
734 INIT_LIST_HEAD(&GlobalOplock_Q);
736 * Initialize Global counters
738 atomic_set(&sesInfoAllocCount, 0);
739 atomic_set(&tconInfoAllocCount, 0);
740 atomic_set(&tcpSesAllocCount,0);
741 atomic_set(&tcpSesReconnectCount, 0);
742 atomic_set(&tconInfoReconnectCount, 0);
744 atomic_set(&bufAllocCount, 0);
745 atomic_set(&midCount, 0);
746 GlobalCurrentXid = 0;
747 GlobalTotalActiveXid = 0;
748 GlobalMaxActiveXid = 0;
749 GlobalSMBSeslock = RW_LOCK_UNLOCKED;
750 GlobalMid_Lock = SPIN_LOCK_UNLOCKED;
752 rc = cifs_init_inodecache();
754 rc = cifs_init_mids();
756 rc = cifs_init_request_bufs();
758 rc = register_filesystem(&cifs_fs_type);
760 kernel_thread(cifs_oplock_thread, NULL,
761 CLONE_FS | CLONE_FILES | CLONE_VM);
762 return rc; /* Success */
764 cifs_destroy_request_bufs();
768 cifs_destroy_inodecache();
770 #ifdef CONFIG_PROC_FS
779 cFYI(0, ("In unregister ie exit_cifs"));
780 #ifdef CONFIG_PROC_FS
783 unregister_filesystem(&cifs_fs_type);
784 cifs_destroy_inodecache();
786 cifs_destroy_request_bufs();
788 send_sig(SIGTERM, oplockThread, 1);
789 wait_for_completion(&cifs_oplock_exited);
793 MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
794 MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
796 ("VFS to access servers complying with the SNIA CIFS Specification e.g. Samba and Windows");
797 MODULE_VERSION(CIFS_VERSION);
798 module_init(init_cifs)
799 module_exit(exit_cifs)