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 */
44 /* BB when mempool_resize is added back in, we will resize pool on new mount */
45 #define CIFS_MIN_RCV_POOL 11 /* enough for progress to five servers */
47 #ifdef CONFIG_CIFS_QUOTA
48 static struct quotactl_ops cifs_quotactl_ops;
54 unsigned int oplockEnabled = 1;
55 unsigned int quotaEnabled = 0;
56 unsigned int linuxExtEnabled = 1;
57 unsigned int lookupCacheEnabled = 1;
58 unsigned int multiuser_mount = 0;
59 unsigned int extended_security = 0;
60 unsigned int ntlmv2_support = 0;
61 unsigned int sign_CIFS_PDUs = 1;
62 unsigned int CIFSMaximumBufferSize = CIFS_MAX_MSGSIZE;
63 struct task_struct * oplockThread = NULL;
65 extern int cifs_mount(struct super_block *, struct cifs_sb_info *, char *,
67 extern int cifs_umount(struct super_block *, struct cifs_sb_info *);
68 void cifs_proc_init(void);
69 void cifs_proc_clean(void);
71 static DECLARE_COMPLETION(cifs_oplock_exited);
75 cifs_read_super(struct super_block *sb, void *data,
76 const char *devname, int silent)
79 struct cifs_sb_info *cifs_sb;
82 sb->s_flags |= MS_NODIRATIME; /* and probably even noatime */
83 sb->s_fs_info = kmalloc(sizeof(struct cifs_sb_info),GFP_KERNEL);
84 cifs_sb = CIFS_SB(sb);
88 memset(cifs_sb,0,sizeof(struct cifs_sb_info));
91 rc = cifs_mount(sb, cifs_sb, data, devname);
96 ("cifs_mount failed w/return code = %d", rc));
97 goto out_mount_failed;
100 sb->s_magic = CIFS_MAGIC_NUMBER;
101 sb->s_op = &cifs_super_ops;
102 /* if(cifs_sb->tcon->ses->server->maxBuf > MAX_CIFS_HDR_SIZE + 512)
103 sb->s_blocksize = cifs_sb->tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE; */
104 #ifdef CONFIG_CIFS_QUOTA
105 sb->s_qcop = &cifs_quotactl_ops;
107 sb->s_blocksize = CIFS_MAX_MSGSIZE;
108 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
109 inode = iget(sb, ROOT_I);
116 sb->s_root = d_alloc_root(inode);
126 cERROR(1, ("cifs_read_super: get root inode failed"));
132 if(cifs_sb->local_nls)
133 unload_nls(cifs_sb->local_nls);
140 cifs_put_super(struct super_block *sb)
143 struct cifs_sb_info *cifs_sb;
145 cFYI(1, ("In cifs_put_super"));
146 cifs_sb = CIFS_SB(sb);
147 if(cifs_sb == NULL) {
148 cFYI(1,("Empty cifs superblock info passed to unmount"));
151 rc = cifs_umount(sb, cifs_sb);
153 cERROR(1, ("cifs_umount failed with return code %d", rc));
155 unload_nls(cifs_sb->local_nls);
161 cifs_statfs(struct super_block *sb, struct kstatfs *buf)
164 struct cifs_sb_info *cifs_sb;
165 struct cifsTconInfo *pTcon;
169 cifs_sb = CIFS_SB(sb);
170 pTcon = cifs_sb->tcon;
172 buf->f_type = CIFS_MAGIC_NUMBER;
174 /* instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO */
175 buf->f_namelen = PATH_MAX; /* PATH_MAX may be too long - it would presumably
176 be length of total path, note that some servers may be
177 able to support more than this, but best to be safe
178 since Win2k and others can not handle very long filenames */
179 buf->f_files = 0; /* undefined */
180 buf->f_ffree = 0; /* unlimited */
182 rc = CIFSSMBQFSInfo(xid, pTcon, buf, cifs_sb->local_nls);
188 /* BB get from info put in tcon struct at mount time with call to QFSAttrInfo */
190 return 0; /* always return success? what if volume is no longer available? */
193 static int cifs_permission(struct inode * inode, int mask, struct nameidata *nd)
195 struct cifs_sb_info *cifs_sb;
197 cifs_sb = CIFS_SB(inode->i_sb);
199 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) {
200 /* the server supports the Unix-like mode bits and does its
201 own permission checks, and therefore we do not allow the file
202 mode to be overriden on these mounts - so do not do perm
203 check on client side */
205 } else /* file mode might have been restricted at mount time
206 on the client (above and beyond ACL on servers) for
207 servers which do not support setting and viewing mode bits,
208 so allowing client to check permissions is useful */
209 return vfs_permission(inode, mask);
212 static kmem_cache_t *cifs_inode_cachep;
213 static kmem_cache_t *cifs_req_cachep;
214 static kmem_cache_t *cifs_mid_cachep;
215 kmem_cache_t *cifs_oplock_cachep;
216 mempool_t *cifs_req_poolp;
217 mempool_t *cifs_mid_poolp;
219 static struct inode *
220 cifs_alloc_inode(struct super_block *sb)
222 struct cifsInodeInfo *cifs_inode;
224 (struct cifsInodeInfo *) kmem_cache_alloc(cifs_inode_cachep,
228 cifs_inode->cifsAttrs = 0x20; /* default */
229 atomic_set(&cifs_inode->inUse, 0);
230 cifs_inode->time = 0;
231 /* Until the file is open and we have gotten oplock
232 info back from the server, can not assume caching of
233 file data or metadata */
234 cifs_inode->clientCanCacheRead = FALSE;
235 cifs_inode->clientCanCacheAll = FALSE;
236 cifs_inode->vfs_inode.i_blksize = CIFS_MAX_MSGSIZE;
237 cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
239 INIT_LIST_HEAD(&cifs_inode->openFileList);
240 return &cifs_inode->vfs_inode;
244 cifs_destroy_inode(struct inode *inode)
246 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
250 * cifs_show_options() is for displaying mount options in /proc/mounts.
251 * Not all settable options are displayed but most of the important
255 cifs_show_options(struct seq_file *s, struct vfsmount *m)
257 struct cifs_sb_info *cifs_sb;
259 cifs_sb = CIFS_SB(m->mnt_sb);
263 seq_printf(s, ",unc=%s", cifs_sb->tcon->treeName);
264 if ((cifs_sb->tcon->ses) && (cifs_sb->tcon->ses->userName))
265 seq_printf(s, ",username=%s",
266 cifs_sb->tcon->ses->userName);
267 if(cifs_sb->tcon->ses->domainName)
268 seq_printf(s, ",domain=%s",
269 cifs_sb->tcon->ses->domainName);
271 seq_printf(s, ",rsize=%d",cifs_sb->rsize);
272 seq_printf(s, ",wsize=%d",cifs_sb->wsize);
277 #ifdef CONFIG_CIFS_QUOTA
278 int cifs_xquota_set(struct super_block * sb, int quota_type, qid_t qid,
279 struct fs_disk_quota * pdquota)
283 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
284 struct cifsTconInfo *pTcon;
287 pTcon = cifs_sb->tcon;
294 cFYI(1,("set type: 0x%x id: %d",quota_type,qid));
303 int cifs_xquota_get(struct super_block * sb, int quota_type, qid_t qid,
304 struct fs_disk_quota * pdquota)
308 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
309 struct cifsTconInfo *pTcon;
312 pTcon = cifs_sb->tcon;
318 cFYI(1,("set type: 0x%x id: %d",quota_type,qid));
327 int cifs_xstate_set(struct super_block * sb, unsigned int flags, int operation)
331 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
332 struct cifsTconInfo *pTcon;
335 pTcon = cifs_sb->tcon;
341 cFYI(1,("flags: 0x%x operation: 0x%x",flags,operation));
350 int cifs_xstate_get(struct super_block * sb, struct fs_quota_stat *qstats)
354 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
355 struct cifsTconInfo *pTcon;
358 pTcon = cifs_sb->tcon;
364 cFYI(1,("pqstats %p",qstats));
373 static struct quotactl_ops cifs_quotactl_ops = {
374 .set_xquota = cifs_xquota_set,
375 .get_xquota = cifs_xquota_set,
376 .set_xstate = cifs_xstate_set,
377 .get_xstate = cifs_xstate_get,
381 static int cifs_remount(struct super_block *sb, int *flags, char *data)
383 *flags |= MS_NODIRATIME;
387 struct super_operations cifs_super_ops = {
388 .read_inode = cifs_read_inode,
389 .put_super = cifs_put_super,
390 .statfs = cifs_statfs,
391 .alloc_inode = cifs_alloc_inode,
392 .destroy_inode = cifs_destroy_inode,
393 /* .drop_inode = generic_delete_inode,
394 .delete_inode = cifs_delete_inode, *//* Do not need the above two functions
395 unless later we add lazy close of inodes or unless the kernel forgets to call
396 us with the same number of releases (closes) as opens */
397 .show_options = cifs_show_options,
398 /* .umount_begin = cifs_umount_begin, *//* consider adding in the future */
399 .remount_fs = cifs_remount,
402 static struct super_block *
403 cifs_get_sb(struct file_system_type *fs_type,
404 int flags, const char *dev_name, void *data)
407 struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
409 cFYI(1, ("Devname: %s flags: %d ", dev_name, flags));
416 rc = cifs_read_super(sb, data, dev_name, flags & MS_VERBOSE ? 1 : 0);
418 up_write(&sb->s_umount);
419 deactivate_super(sb);
422 sb->s_flags |= MS_ACTIVE;
427 cifs_read_wrapper(struct file * file, char __user *read_data, size_t read_size,
432 else if(file->f_dentry == NULL)
434 else if(file->f_dentry->d_inode == NULL)
437 cFYI(1,("In read_wrapper size %zd at %lld",read_size,*poffset));
438 if(CIFS_I(file->f_dentry->d_inode)->clientCanCacheRead) {
439 return generic_file_read(file,read_data,read_size,poffset);
441 /* BB do we need to lock inode from here until after invalidate? */
442 /* if(file->f_dentry->d_inode->i_mapping) {
443 filemap_fdatawrite(file->f_dentry->d_inode->i_mapping);
444 filemap_fdatawait(file->f_dentry->d_inode->i_mapping);
446 /* cifs_revalidate(file->f_dentry);*/ /* BB fixme */
448 /* BB we should make timer configurable - perhaps
449 by simply calling cifs_revalidate here */
450 /* invalidate_remote_inode(file->f_dentry->d_inode);*/
451 return generic_file_read(file,read_data,read_size,poffset);
456 cifs_write_wrapper(struct file * file, const char __user *write_data,
457 size_t write_size, loff_t * poffset)
463 else if(file->f_dentry == NULL)
465 else if(file->f_dentry->d_inode == NULL)
468 cFYI(1,("In write_wrapper size %zd at %lld",write_size,*poffset));
470 /* check whether we can cache writes locally */
471 written = generic_file_write(file,write_data,write_size,poffset);
472 if(!CIFS_I(file->f_dentry->d_inode)->clientCanCacheAll) {
473 if(file->f_dentry->d_inode->i_mapping) {
474 filemap_fdatawrite(file->f_dentry->d_inode->i_mapping);
481 static struct file_system_type cifs_fs_type = {
482 .owner = THIS_MODULE,
484 .get_sb = cifs_get_sb,
485 .kill_sb = kill_anon_super,
488 struct inode_operations cifs_dir_inode_ops = {
489 .create = cifs_create,
490 .lookup = cifs_lookup,
491 .getattr = cifs_getattr,
492 .unlink = cifs_unlink,
493 .link = cifs_hardlink,
496 .rename = cifs_rename,
497 .permission = cifs_permission,
498 /* revalidate:cifs_revalidate, */
499 .setattr = cifs_setattr,
500 .symlink = cifs_symlink,
504 struct inode_operations cifs_file_inode_ops = {
505 /* revalidate:cifs_revalidate, */
506 .setattr = cifs_setattr,
507 .getattr = cifs_getattr, /* do we need this anymore? */
508 .rename = cifs_rename,
509 .permission = cifs_permission,
510 #ifdef CONFIG_CIFS_XATTR
511 .setxattr = cifs_setxattr,
512 .getxattr = cifs_getxattr,
513 .listxattr = cifs_listxattr,
514 .removexattr = cifs_removexattr,
518 struct inode_operations cifs_symlink_inode_ops = {
519 .readlink = cifs_readlink,
520 .follow_link = cifs_follow_link,
521 .put_link = cifs_put_link,
522 .permission = cifs_permission,
523 /* BB add the following two eventually */
524 /* revalidate: cifs_revalidate,
525 setattr: cifs_notify_change, *//* BB do we need notify change */
526 #ifdef CONFIG_CIFS_XATTR
527 .setxattr = cifs_setxattr,
528 .getxattr = cifs_getxattr,
529 .listxattr = cifs_listxattr,
530 .removexattr = cifs_removexattr,
534 struct file_operations cifs_file_ops = {
535 .read = cifs_read_wrapper,
536 .write = cifs_write_wrapper,
538 .release = cifs_close,
542 .mmap = cifs_file_mmap,
543 .sendfile = generic_file_sendfile,
544 #ifdef CONFIG_CIFS_FCNTL
549 struct file_operations cifs_dir_ops = {
550 .readdir = cifs_readdir,
551 .release = cifs_closedir,
552 .read = generic_read_dir,
553 #ifdef CONFIG_CIFS_FCNTL
559 cifs_init_once(void *inode, kmem_cache_t * cachep, unsigned long flags)
561 struct cifsInodeInfo *cifsi = (struct cifsInodeInfo *) inode;
563 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
564 SLAB_CTOR_CONSTRUCTOR) {
565 inode_init_once(&cifsi->vfs_inode);
566 INIT_LIST_HEAD(&cifsi->lockList);
571 cifs_init_inodecache(void)
573 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
574 sizeof (struct cifsInodeInfo),
575 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
576 cifs_init_once, NULL);
577 if (cifs_inode_cachep == NULL)
584 cifs_destroy_inodecache(void)
586 if (kmem_cache_destroy(cifs_inode_cachep))
587 printk(KERN_WARNING "cifs_inode_cache: error freeing\n");
591 cifs_init_request_bufs(void)
593 cifs_req_cachep = kmem_cache_create("cifs_request",
595 MAX_CIFS_HDR_SIZE, 0,
596 SLAB_HWCACHE_ALIGN, NULL, NULL);
597 if (cifs_req_cachep == NULL)
600 cifs_req_poolp = mempool_create(CIFS_MIN_RCV_POOL,
605 if(cifs_req_poolp == NULL) {
606 kmem_cache_destroy(cifs_req_cachep);
614 cifs_destroy_request_bufs(void)
616 mempool_destroy(cifs_req_poolp);
617 if (kmem_cache_destroy(cifs_req_cachep))
619 "cifs_destroy_request_cache: error not all structures were freed\n");
625 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
626 sizeof (struct mid_q_entry), 0,
627 SLAB_HWCACHE_ALIGN, NULL, NULL);
628 if (cifs_mid_cachep == NULL)
631 cifs_mid_poolp = mempool_create(3 /* a reasonable min simultan opers */,
635 if(cifs_mid_poolp == NULL) {
636 kmem_cache_destroy(cifs_mid_cachep);
640 cifs_oplock_cachep = kmem_cache_create("cifs_oplock_structs",
641 sizeof (struct oplock_q_entry), 0,
642 SLAB_HWCACHE_ALIGN, NULL, NULL);
643 if (cifs_oplock_cachep == NULL) {
644 kmem_cache_destroy(cifs_mid_cachep);
645 mempool_destroy(cifs_mid_poolp);
653 cifs_destroy_mids(void)
655 mempool_destroy(cifs_mid_poolp);
656 if (kmem_cache_destroy(cifs_mid_cachep))
658 "cifs_destroy_mids: error not all structures were freed\n");
660 if (kmem_cache_destroy(cifs_oplock_cachep))
662 "error not all oplock structures were freed\n");
665 static int cifs_oplock_thread(void * dummyarg)
667 struct oplock_q_entry * oplock_item;
668 struct cifsTconInfo *pTcon;
669 struct inode * inode;
673 daemonize("cifsoplockd");
674 allow_signal(SIGTERM);
676 oplockThread = current;
678 set_current_state(TASK_INTERRUPTIBLE);
680 schedule_timeout(1*HZ);
681 spin_lock(&GlobalMid_Lock);
682 if(list_empty(&GlobalOplock_Q)) {
683 spin_unlock(&GlobalMid_Lock);
684 set_current_state(TASK_INTERRUPTIBLE);
685 schedule_timeout(39*HZ);
687 oplock_item = list_entry(GlobalOplock_Q.next,
688 struct oplock_q_entry, qhead);
690 cFYI(1,("found oplock item to write out"));
691 pTcon = oplock_item->tcon;
692 inode = oplock_item->pinode;
693 netfid = oplock_item->netfid;
694 spin_unlock(&GlobalMid_Lock);
695 DeleteOplockQEntry(oplock_item);
696 /* can not grab inode sem here since it would
697 deadlock when oplock received on delete
698 since vfs_unlink holds the i_sem across
700 /* down(&inode->i_sem);*/
701 if (S_ISREG(inode->i_mode)) {
702 rc = filemap_fdatawrite(inode->i_mapping);
703 if(CIFS_I(inode)->clientCanCacheRead == 0) {
704 filemap_fdatawait(inode->i_mapping);
705 invalidate_remote_inode(inode);
709 /* up(&inode->i_sem);*/
711 CIFS_I(inode)->write_behind_rc = rc;
712 cFYI(1,("Oplock flush inode %p rc %d",inode,rc));
714 /* releasing a stale oplock after recent reconnection
715 of smb session using a now incorrect file
716 handle is not a data integrity issue but do
717 not bother sending an oplock release if session
718 to server still is disconnected since oplock
719 already released by the server in that case */
720 if(pTcon->tidStatus != CifsNeedReconnect) {
721 rc = CIFSSMBLock(0, pTcon, netfid,
722 0 /* len */ , 0 /* offset */, 0,
723 0, LOCKING_ANDX_OPLOCK_RELEASE,
725 cFYI(1,("Oplock release rc = %d ",rc));
728 spin_unlock(&GlobalMid_Lock);
730 } while(!signal_pending(current));
731 complete_and_exit (&cifs_oplock_exited, 0);
738 #ifdef CONFIG_PROC_FS
741 INIT_LIST_HEAD(&GlobalServerList); /* BB not implemented yet */
742 INIT_LIST_HEAD(&GlobalSMBSessionList);
743 INIT_LIST_HEAD(&GlobalTreeConnectionList);
744 INIT_LIST_HEAD(&GlobalOplock_Q);
746 * Initialize Global counters
748 atomic_set(&sesInfoAllocCount, 0);
749 atomic_set(&tconInfoAllocCount, 0);
750 atomic_set(&tcpSesReconnectCount, 0);
751 atomic_set(&tconInfoReconnectCount, 0);
753 atomic_set(&bufAllocCount, 0);
754 atomic_set(&midCount, 0);
755 GlobalCurrentXid = 0;
756 GlobalTotalActiveXid = 0;
757 GlobalMaxActiveXid = 0;
758 GlobalSMBSeslock = RW_LOCK_UNLOCKED;
759 GlobalMid_Lock = SPIN_LOCK_UNLOCKED;
761 rc = cifs_init_inodecache();
763 rc = cifs_init_mids();
765 rc = cifs_init_request_bufs();
767 rc = register_filesystem(&cifs_fs_type);
769 kernel_thread(cifs_oplock_thread, NULL,
770 CLONE_FS | CLONE_FILES | CLONE_VM);
771 return rc; /* Success */
773 cifs_destroy_request_bufs();
777 cifs_destroy_inodecache();
779 #ifdef CONFIG_PROC_FS
788 cFYI(0, ("In unregister ie exit_cifs"));
789 #ifdef CONFIG_PROC_FS
792 unregister_filesystem(&cifs_fs_type);
793 cifs_destroy_inodecache();
795 cifs_destroy_request_bufs();
797 send_sig(SIGTERM, oplockThread, 1);
798 wait_for_completion(&cifs_oplock_exited);
802 MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
803 MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
805 ("VFS to access servers complying with the SNIA CIFS Specification e.g. Samba and Windows");
806 MODULE_VERSION(CIFS_VERSION);
807 module_init(init_cifs)
808 module_exit(exit_cifs)