1/*
2 *   fs/cifs/cifsfs.c
3 *
4 *   Copyright (C) International Business Machines  Corp., 2002,2007
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 <linux/delay.h>
36#include <linux/kthread.h>
37#include <linux/freezer.h>
38#include "cifsfs.h"
39#include "cifspdu.h"
40#define DECLARE_GLOBALS_HERE
41#include "cifsglob.h"
42#include "cifsproto.h"
43#include "cifs_debug.h"
44#include "cifs_fs_sb.h"
45#include <linux/mm.h>
46#define CIFS_MAGIC_NUMBER 0xFF534D42	/* the first four bytes of SMB PDUs */
47
48#ifdef CONFIG_CIFS_QUOTA
49static struct quotactl_ops cifs_quotactl_ops;
50#endif /* QUOTA */
51
52#ifdef CONFIG_CIFS_EXPERIMENTAL
53extern struct export_operations cifs_export_ops;
54#endif /* EXPERIMENTAL */
55
56int cifsFYI = 0;
57int cifsERROR = 1;
58int traceSMB = 0;
59unsigned int oplockEnabled = 1;
60unsigned int experimEnabled = 0;
61unsigned int linuxExtEnabled = 1;
62unsigned int lookupCacheEnabled = 1;
63unsigned int multiuser_mount = 0;
64unsigned int extended_security = CIFSSEC_DEF;
65/* unsigned int ntlmv2_support = 0; */
66unsigned int sign_CIFS_PDUs = 1;
67extern struct task_struct * oplockThread; /* remove sparse warning */
68struct task_struct * oplockThread = NULL;
69/* extern struct task_struct * dnotifyThread; remove sparse warning */
70static struct task_struct * dnotifyThread = NULL;
71static const struct super_operations cifs_super_ops;
72unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
73module_param(CIFSMaxBufSize, int, 0);
74MODULE_PARM_DESC(CIFSMaxBufSize,"Network buffer size (not including header). Default: 16384 Range: 8192 to 130048");
75unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
76module_param(cifs_min_rcv, int, 0);
77MODULE_PARM_DESC(cifs_min_rcv,"Network buffers in pool. Default: 4 Range: 1 to 64");
78unsigned int cifs_min_small = 30;
79module_param(cifs_min_small, int, 0);
80MODULE_PARM_DESC(cifs_min_small,"Small network buffers in pool. Default: 30 Range: 2 to 256");
81unsigned int cifs_max_pending = CIFS_MAX_REQ;
82module_param(cifs_max_pending, int, 0);
83MODULE_PARM_DESC(cifs_max_pending,"Simultaneous requests to server. Default: 50 Range: 2 to 256");
84
85extern mempool_t *cifs_sm_req_poolp;
86extern mempool_t *cifs_req_poolp;
87extern mempool_t *cifs_mid_poolp;
88
89extern struct kmem_cache *cifs_oplock_cachep;
90
91static int
92cifs_read_super(struct super_block *sb, void *data,
93		const char *devname, int silent)
94{
95	struct inode *inode;
96	struct cifs_sb_info *cifs_sb;
97	int rc = 0;
98
99	/* BB should we make this contingent on mount parm? */
100	sb->s_flags |= MS_NODIRATIME | MS_NOATIME;
101	sb->s_fs_info = kzalloc(sizeof(struct cifs_sb_info),GFP_KERNEL);
102	cifs_sb = CIFS_SB(sb);
103	if (cifs_sb == NULL)
104		return -ENOMEM;
105
106	rc = cifs_mount(sb, cifs_sb, data, devname);
107
108	if (rc) {
109		if (!silent)
110			cERROR(1,
111			       ("cifs_mount failed w/return code = %d", rc));
112		goto out_mount_failed;
113	}
114
115	sb->s_magic = CIFS_MAGIC_NUMBER;
116	sb->s_op = &cifs_super_ops;
117#ifdef CONFIG_CIFS_EXPERIMENTAL
118	if (experimEnabled != 0)
119		sb->s_export_op = &cifs_export_ops;
120#endif /* EXPERIMENTAL */
121/*	if (cifs_sb->tcon->ses->server->maxBuf > MAX_CIFS_HDR_SIZE + 512)
122	    sb->s_blocksize = cifs_sb->tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE; */
123#ifdef CONFIG_CIFS_QUOTA
124	sb->s_qcop = &cifs_quotactl_ops;
125#endif
126	sb->s_blocksize = CIFS_MAX_MSGSIZE;
127	sb->s_blocksize_bits = 14;	/* default 2**14 = CIFS_MAX_MSGSIZE */
128	inode = iget(sb, ROOT_I);
129
130	if (!inode) {
131		rc = -ENOMEM;
132		goto out_no_root;
133	}
134
135	sb->s_root = d_alloc_root(inode);
136
137	if (!sb->s_root) {
138		rc = -ENOMEM;
139		goto out_no_root;
140	}
141
142	return 0;
143
144out_no_root:
145	cERROR(1, ("cifs_read_super: get root inode failed"));
146	if (inode)
147		iput(inode);
148
149out_mount_failed:
150	if (cifs_sb) {
151		if (cifs_sb->local_nls)
152			unload_nls(cifs_sb->local_nls);
153		kfree(cifs_sb);
154	}
155	return rc;
156}
157
158static void
159cifs_put_super(struct super_block *sb)
160{
161	int rc = 0;
162	struct cifs_sb_info *cifs_sb;
163
164	cFYI(1, ("In cifs_put_super"));
165	cifs_sb = CIFS_SB(sb);
166	if (cifs_sb == NULL) {
167		cFYI(1,("Empty cifs superblock info passed to unmount"));
168		return;
169	}
170	rc = cifs_umount(sb, cifs_sb);
171	if (rc) {
172		cERROR(1, ("cifs_umount failed with return code %d", rc));
173	}
174	unload_nls(cifs_sb->local_nls);
175	kfree(cifs_sb);
176	return;
177}
178
179static int
180cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
181{
182	struct super_block *sb = dentry->d_sb;
183	int xid;
184	int rc = -EOPNOTSUPP;
185	struct cifs_sb_info *cifs_sb;
186	struct cifsTconInfo *pTcon;
187
188	xid = GetXid();
189
190	cifs_sb = CIFS_SB(sb);
191	pTcon = cifs_sb->tcon;
192
193	buf->f_type = CIFS_MAGIC_NUMBER;
194
195	/* instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO */
196	buf->f_namelen = PATH_MAX; /* PATH_MAX may be too long - it would
197				      presumably be total path, but note
198				      that some servers (includinng Samba 3)
199				      have a shorter maximum path */
200	buf->f_files = 0;	/* undefined */
201	buf->f_ffree = 0;	/* unlimited */
202
203/* BB we could add a second check for a QFS Unix capability bit */
204    if ((pTcon->ses->capabilities & CAP_UNIX) && (CIFS_POSIX_EXTENSIONS &
205			le64_to_cpu(pTcon->fsUnixInfo.Capability)))
206	    rc = CIFSSMBQFSPosixInfo(xid, pTcon, buf);
207
208    /* Only need to call the old QFSInfo if failed
209    on newer one */
210    if (rc)
211	if (pTcon->ses->capabilities & CAP_NT_SMBS)
212		rc = CIFSSMBQFSInfo(xid, pTcon, buf); /* not supported by OS2 */
213
214	/* Some old Windows servers also do not support level 103, retry with
215	   older level one if old server failed the previous call or we
216	   bypassed it because we detected that this was an older LANMAN sess */
217	if (rc)
218		rc = SMBOldQFSInfo(xid, pTcon, buf);
219	/*
220	   int f_type;
221	   __fsid_t f_fsid;
222	   int f_namelen;  */
223	/* BB get from info in tcon struct at mount time call to QFSAttrInfo */
224	FreeXid(xid);
225	return 0;		/* always return success? what if volume is no
226				   longer available? */
227}
228
229static int cifs_permission(struct inode * inode, int mask, struct nameidata *nd)
230{
231	struct cifs_sb_info *cifs_sb;
232
233	cifs_sb = CIFS_SB(inode->i_sb);
234
235	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
236		return 0;
237	} else /* file mode might have been restricted at mount time
238		on the client (above and beyond ACL on servers) for
239		servers which do not support setting and viewing mode bits,
240		so allowing client to check permissions is useful */
241		return generic_permission(inode, mask, NULL);
242}
243
244static struct kmem_cache *cifs_inode_cachep;
245static struct kmem_cache *cifs_req_cachep;
246static struct kmem_cache *cifs_mid_cachep;
247struct kmem_cache *cifs_oplock_cachep;
248static struct kmem_cache *cifs_sm_req_cachep;
249mempool_t *cifs_sm_req_poolp;
250mempool_t *cifs_req_poolp;
251mempool_t *cifs_mid_poolp;
252
253static struct inode *
254cifs_alloc_inode(struct super_block *sb)
255{
256	struct cifsInodeInfo *cifs_inode;
257	cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
258	if (!cifs_inode)
259		return NULL;
260	cifs_inode->cifsAttrs = 0x20;	/* default */
261	atomic_set(&cifs_inode->inUse, 0);
262	cifs_inode->time = 0;
263	/* Until the file is open and we have gotten oplock
264	info back from the server, can not assume caching of
265	file data or metadata */
266	cifs_inode->clientCanCacheRead = FALSE;
267	cifs_inode->clientCanCacheAll = FALSE;
268	cifs_inode->vfs_inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
269
270	/* Can not set i_flags here - they get immediately overwritten
271	   to zero by the VFS */
272/*	cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;*/
273	INIT_LIST_HEAD(&cifs_inode->openFileList);
274	return &cifs_inode->vfs_inode;
275}
276
277static void
278cifs_destroy_inode(struct inode *inode)
279{
280	kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
281}
282
283/*
284 * cifs_show_options() is for displaying mount options in /proc/mounts.
285 * Not all settable options are displayed but most of the important
286 * ones are.
287 */
288static int
289cifs_show_options(struct seq_file *s, struct vfsmount *m)
290{
291	struct cifs_sb_info *cifs_sb;
292
293	cifs_sb = CIFS_SB(m->mnt_sb);
294
295	if (cifs_sb) {
296		if (cifs_sb->tcon) {
297/* BB add prepath to mount options displayed */
298			seq_printf(s, ",unc=%s", cifs_sb->tcon->treeName);
299			if (cifs_sb->tcon->ses) {
300				if (cifs_sb->tcon->ses->userName)
301					seq_printf(s, ",username=%s",
302					   cifs_sb->tcon->ses->userName);
303				if (cifs_sb->tcon->ses->domainName)
304					seq_printf(s, ",domain=%s",
305					   cifs_sb->tcon->ses->domainName);
306			}
307		}
308		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
309			seq_printf(s, ",posixpaths");
310		if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) ||
311		   !(cifs_sb->tcon->ses->capabilities & CAP_UNIX))
312			seq_printf(s, ",uid=%d", cifs_sb->mnt_uid);
313		if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) ||
314		   !(cifs_sb->tcon->ses->capabilities & CAP_UNIX))
315			seq_printf(s, ",gid=%d", cifs_sb->mnt_gid);
316		seq_printf(s, ",rsize=%d",cifs_sb->rsize);
317		seq_printf(s, ",wsize=%d",cifs_sb->wsize);
318	}
319	return 0;
320}
321
322#ifdef CONFIG_CIFS_QUOTA
323int cifs_xquota_set(struct super_block * sb, int quota_type, qid_t qid,
324		struct fs_disk_quota * pdquota)
325{
326	int xid;
327	int rc = 0;
328	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
329	struct cifsTconInfo *pTcon;
330
331	if (cifs_sb)
332		pTcon = cifs_sb->tcon;
333	else
334		return -EIO;
335
336
337	xid = GetXid();
338	if (pTcon) {
339		cFYI(1,("set type: 0x%x id: %d",quota_type,qid));
340	} else {
341		return -EIO;
342	}
343
344	FreeXid(xid);
345	return rc;
346}
347
348int cifs_xquota_get(struct super_block * sb, int quota_type, qid_t qid,
349                struct fs_disk_quota * pdquota)
350{
351	int xid;
352	int rc = 0;
353	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
354	struct cifsTconInfo *pTcon;
355
356	if (cifs_sb)
357		pTcon = cifs_sb->tcon;
358	else
359		return -EIO;
360
361	xid = GetXid();
362	if (pTcon) {
363                cFYI(1,("set type: 0x%x id: %d",quota_type,qid));
364	} else {
365		rc = -EIO;
366	}
367
368	FreeXid(xid);
369	return rc;
370}
371
372int cifs_xstate_set(struct super_block * sb, unsigned int flags, int operation)
373{
374	int xid;
375	int rc = 0;
376	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
377	struct cifsTconInfo *pTcon;
378
379	if (cifs_sb)
380		pTcon = cifs_sb->tcon;
381	else
382		return -EIO;
383
384	xid = GetXid();
385	if (pTcon) {
386                cFYI(1,("flags: 0x%x operation: 0x%x",flags,operation));
387	} else {
388		rc = -EIO;
389	}
390
391	FreeXid(xid);
392	return rc;
393}
394
395int cifs_xstate_get(struct super_block * sb, struct fs_quota_stat *qstats)
396{
397	int xid;
398	int rc = 0;
399	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
400	struct cifsTconInfo *pTcon;
401
402	if (cifs_sb) {
403		pTcon = cifs_sb->tcon;
404	} else {
405		return -EIO;
406	}
407	xid = GetXid();
408	if (pTcon) {
409		cFYI(1,("pqstats %p",qstats));
410	} else {
411		rc = -EIO;
412	}
413
414	FreeXid(xid);
415	return rc;
416}
417
418static struct quotactl_ops cifs_quotactl_ops = {
419	.set_xquota	= cifs_xquota_set,
420	.get_xquota	= cifs_xquota_set,
421	.set_xstate	= cifs_xstate_set,
422	.get_xstate	= cifs_xstate_get,
423};
424#endif
425
426static void cifs_umount_begin(struct vfsmount * vfsmnt, int flags)
427{
428	struct cifs_sb_info *cifs_sb;
429	struct cifsTconInfo * tcon;
430
431	if (!(flags & MNT_FORCE))
432		return;
433	cifs_sb = CIFS_SB(vfsmnt->mnt_sb);
434	if (cifs_sb == NULL)
435		return;
436
437	tcon = cifs_sb->tcon;
438	if (tcon == NULL)
439		return;
440	down(&tcon->tconSem);
441	if (atomic_read(&tcon->useCount) == 1)
442		tcon->tidStatus = CifsExiting;
443	up(&tcon->tconSem);
444
445	/* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
446	/* cancel_notify_requests(tcon); */
447	if (tcon->ses && tcon->ses->server)
448	{
449		cFYI(1,("wake up tasks now - umount begin not complete"));
450		wake_up_all(&tcon->ses->server->request_q);
451		wake_up_all(&tcon->ses->server->response_q);
452		msleep(1); /* yield */
453		/* we have to kick the requests once more */
454		wake_up_all(&tcon->ses->server->response_q);
455		msleep(1);
456	}
457
458	return;
459}
460
461#ifdef CONFIG_CIFS_STATS2
462static int cifs_show_stats(struct seq_file *s, struct vfsmount *mnt)
463{
464	return 0;
465}
466#endif
467
468static int cifs_remount(struct super_block *sb, int *flags, char *data)
469{
470	*flags |= MS_NODIRATIME;
471	return 0;
472}
473
474static const struct super_operations cifs_super_ops = {
475	.read_inode = cifs_read_inode,
476	.put_super = cifs_put_super,
477	.statfs = cifs_statfs,
478	.alloc_inode = cifs_alloc_inode,
479	.destroy_inode = cifs_destroy_inode,
480/*	.drop_inode	    = generic_delete_inode,
481	.delete_inode	= cifs_delete_inode,  *//* Do not need the above two functions
482   unless later we add lazy close of inodes or unless the kernel forgets to call
483   us with the same number of releases (closes) as opens */
484	.show_options = cifs_show_options,
485	.umount_begin   = cifs_umount_begin,
486	.remount_fs = cifs_remount,
487#ifdef CONFIG_CIFS_STATS2
488	.show_stats = cifs_show_stats,
489#endif
490};
491
492static int
493cifs_get_sb(struct file_system_type *fs_type,
494	    int flags, const char *dev_name, void *data, struct vfsmount *mnt)
495{
496	int rc;
497	struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
498
499	cFYI(1, ("Devname: %s flags: %d ", dev_name, flags));
500
501	if (IS_ERR(sb))
502		return PTR_ERR(sb);
503
504	sb->s_flags = flags;
505
506	rc = cifs_read_super(sb, data, dev_name, flags & MS_SILENT ? 1 : 0);
507	if (rc) {
508		up_write(&sb->s_umount);
509		deactivate_super(sb);
510		return rc;
511	}
512	sb->s_flags |= MS_ACTIVE;
513	return simple_set_mnt(mnt, sb);
514}
515
516static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
517				   unsigned long nr_segs, loff_t pos)
518{
519	struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
520	ssize_t written;
521
522	written = generic_file_aio_write(iocb, iov, nr_segs, pos);
523	if (!CIFS_I(inode)->clientCanCacheAll)
524		filemap_fdatawrite(inode->i_mapping);
525	return written;
526}
527
528static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
529{
530	/* origin == SEEK_END => we must revalidate the cached file length */
531	if (origin == SEEK_END) {
532		int retval;
533
534		/* some applications poll for the file length in this strange
535		   way so we must seek to end on non-oplocked files by
536		   setting the revalidate time to zero */
537		CIFS_I(file->f_path.dentry->d_inode)->time = 0;
538
539		retval = cifs_revalidate(file->f_path.dentry);
540		if (retval < 0)
541			return (loff_t)retval;
542	}
543	return remote_llseek(file, offset, origin);
544}
545
546static struct file_system_type cifs_fs_type = {
547	.owner = THIS_MODULE,
548	.name = "cifs",
549	.get_sb = cifs_get_sb,
550	.kill_sb = kill_anon_super,
551	/*  .fs_flags */
552};
553const struct inode_operations cifs_dir_inode_ops = {
554	.create = cifs_create,
555	.lookup = cifs_lookup,
556	.getattr = cifs_getattr,
557	.unlink = cifs_unlink,
558	.link = cifs_hardlink,
559	.mkdir = cifs_mkdir,
560	.rmdir = cifs_rmdir,
561	.rename = cifs_rename,
562	.permission = cifs_permission,
563/*	revalidate:cifs_revalidate,   */
564	.setattr = cifs_setattr,
565	.symlink = cifs_symlink,
566	.mknod   = cifs_mknod,
567#ifdef CONFIG_CIFS_XATTR
568	.setxattr = cifs_setxattr,
569	.getxattr = cifs_getxattr,
570	.listxattr = cifs_listxattr,
571	.removexattr = cifs_removexattr,
572#endif
573};
574
575const struct inode_operations cifs_file_inode_ops = {
576/*	revalidate:cifs_revalidate, */
577	.setattr = cifs_setattr,
578	.getattr = cifs_getattr, /* do we need this anymore? */
579	.rename = cifs_rename,
580	.permission = cifs_permission,
581#ifdef CONFIG_CIFS_XATTR
582	.setxattr = cifs_setxattr,
583	.getxattr = cifs_getxattr,
584	.listxattr = cifs_listxattr,
585	.removexattr = cifs_removexattr,
586#endif
587};
588
589const struct inode_operations cifs_symlink_inode_ops = {
590	.readlink = generic_readlink,
591	.follow_link = cifs_follow_link,
592	.put_link = cifs_put_link,
593	.permission = cifs_permission,
594	/* BB add the following two eventually */
595	/* revalidate: cifs_revalidate,
596	   setattr:    cifs_notify_change, *//* BB do we need notify change */
597#ifdef CONFIG_CIFS_XATTR
598	.setxattr = cifs_setxattr,
599	.getxattr = cifs_getxattr,
600	.listxattr = cifs_listxattr,
601	.removexattr = cifs_removexattr,
602#endif
603};
604
605const struct file_operations cifs_file_ops = {
606	.read = do_sync_read,
607	.write = do_sync_write,
608	.aio_read = generic_file_aio_read,
609	.aio_write = cifs_file_aio_write,
610	.open = cifs_open,
611	.release = cifs_close,
612	.lock = cifs_lock,
613	.fsync = cifs_fsync,
614	.flush = cifs_flush,
615	.mmap  = cifs_file_mmap,
616	.sendfile = generic_file_sendfile,
617	.llseek = cifs_llseek,
618#ifdef CONFIG_CIFS_POSIX
619	.ioctl	= cifs_ioctl,
620#endif /* CONFIG_CIFS_POSIX */
621
622#ifdef CONFIG_CIFS_EXPERIMENTAL
623	.dir_notify = cifs_dir_notify,
624#endif /* CONFIG_CIFS_EXPERIMENTAL */
625};
626
627const struct file_operations cifs_file_direct_ops = {
628	/* no mmap, no aio, no readv -
629	   BB reevaluate whether they can be done with directio, no cache */
630	.read = cifs_user_read,
631	.write = cifs_user_write,
632	.open = cifs_open,
633	.release = cifs_close,
634	.lock = cifs_lock,
635	.fsync = cifs_fsync,
636	.flush = cifs_flush,
637	.sendfile = generic_file_sendfile, /* BB removeme BB */
638#ifdef CONFIG_CIFS_POSIX
639	.ioctl  = cifs_ioctl,
640#endif /* CONFIG_CIFS_POSIX */
641	.llseek = cifs_llseek,
642#ifdef CONFIG_CIFS_EXPERIMENTAL
643	.dir_notify = cifs_dir_notify,
644#endif /* CONFIG_CIFS_EXPERIMENTAL */
645};
646const struct file_operations cifs_file_nobrl_ops = {
647	.read = do_sync_read,
648	.write = do_sync_write,
649	.aio_read = generic_file_aio_read,
650	.aio_write = cifs_file_aio_write,
651	.open = cifs_open,
652	.release = cifs_close,
653	.fsync = cifs_fsync,
654	.flush = cifs_flush,
655	.mmap  = cifs_file_mmap,
656	.sendfile = generic_file_sendfile,
657	.llseek = cifs_llseek,
658#ifdef CONFIG_CIFS_POSIX
659	.ioctl	= cifs_ioctl,
660#endif /* CONFIG_CIFS_POSIX */
661
662#ifdef CONFIG_CIFS_EXPERIMENTAL
663	.dir_notify = cifs_dir_notify,
664#endif /* CONFIG_CIFS_EXPERIMENTAL */
665};
666
667const struct file_operations cifs_file_direct_nobrl_ops = {
668	/* no mmap, no aio, no readv -
669	   BB reevaluate whether they can be done with directio, no cache */
670	.read = cifs_user_read,
671	.write = cifs_user_write,
672	.open = cifs_open,
673	.release = cifs_close,
674	.fsync = cifs_fsync,
675	.flush = cifs_flush,
676	.sendfile = generic_file_sendfile, /* BB removeme BB */
677#ifdef CONFIG_CIFS_POSIX
678	.ioctl  = cifs_ioctl,
679#endif /* CONFIG_CIFS_POSIX */
680	.llseek = cifs_llseek,
681#ifdef CONFIG_CIFS_EXPERIMENTAL
682	.dir_notify = cifs_dir_notify,
683#endif /* CONFIG_CIFS_EXPERIMENTAL */
684};
685
686const struct file_operations cifs_dir_ops = {
687	.readdir = cifs_readdir,
688	.release = cifs_closedir,
689	.read    = generic_read_dir,
690#ifdef CONFIG_CIFS_EXPERIMENTAL
691	.dir_notify = cifs_dir_notify,
692#endif /* CONFIG_CIFS_EXPERIMENTAL */
693        .ioctl  = cifs_ioctl,
694};
695
696static void
697cifs_init_once(void *inode, struct kmem_cache * cachep, unsigned long flags)
698{
699	struct cifsInodeInfo *cifsi = inode;
700
701	inode_init_once(&cifsi->vfs_inode);
702	INIT_LIST_HEAD(&cifsi->lockList);
703}
704
705static int
706cifs_init_inodecache(void)
707{
708	cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
709					      sizeof (struct cifsInodeInfo),
710					      0, (SLAB_RECLAIM_ACCOUNT|
711						SLAB_MEM_SPREAD),
712					      cifs_init_once, NULL);
713	if (cifs_inode_cachep == NULL)
714		return -ENOMEM;
715
716	return 0;
717}
718
719static void
720cifs_destroy_inodecache(void)
721{
722	kmem_cache_destroy(cifs_inode_cachep);
723}
724
725static int
726cifs_init_request_bufs(void)
727{
728	if (CIFSMaxBufSize < 8192) {
729	/* Buffer size can not be smaller than 2 * PATH_MAX since maximum
730	Unicode path name has to fit in any SMB/CIFS path based frames */
731		CIFSMaxBufSize = 8192;
732	} else if (CIFSMaxBufSize > 1024*127) {
733		CIFSMaxBufSize = 1024 * 127;
734	} else {
735		CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
736	}
737/*	cERROR(1,("CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize)); */
738	cifs_req_cachep = kmem_cache_create("cifs_request",
739					    CIFSMaxBufSize +
740					    MAX_CIFS_HDR_SIZE, 0,
741					    SLAB_HWCACHE_ALIGN, NULL, NULL);
742	if (cifs_req_cachep == NULL)
743		return -ENOMEM;
744
745	if (cifs_min_rcv < 1)
746		cifs_min_rcv = 1;
747	else if (cifs_min_rcv > 64) {
748		cifs_min_rcv = 64;
749		cERROR(1,("cifs_min_rcv set to maximum (64)"));
750	}
751
752	cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
753						  cifs_req_cachep);
754
755	if (cifs_req_poolp == NULL) {
756		kmem_cache_destroy(cifs_req_cachep);
757		return -ENOMEM;
758	}
759	/* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
760	almost all handle based requests (but not write response, nor is it
761	sufficient for path based requests).  A smaller size would have
762	been more efficient (compacting multiple slab items on one 4k page)
763	for the case in which debug was on, but this larger size allows
764	more SMBs to use small buffer alloc and is still much more
765	efficient to alloc 1 per page off the slab compared to 17K (5page)
766	alloc of large cifs buffers even when page debugging is on */
767	cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
768			MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
769			NULL, NULL);
770	if (cifs_sm_req_cachep == NULL) {
771		mempool_destroy(cifs_req_poolp);
772		kmem_cache_destroy(cifs_req_cachep);
773		return -ENOMEM;
774	}
775
776	if (cifs_min_small < 2)
777		cifs_min_small = 2;
778	else if (cifs_min_small > 256) {
779		cifs_min_small = 256;
780		cFYI(1,("cifs_min_small set to maximum (256)"));
781	}
782
783	cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
784						     cifs_sm_req_cachep);
785
786	if (cifs_sm_req_poolp == NULL) {
787		mempool_destroy(cifs_req_poolp);
788		kmem_cache_destroy(cifs_req_cachep);
789		kmem_cache_destroy(cifs_sm_req_cachep);
790		return -ENOMEM;
791	}
792
793	return 0;
794}
795
796static void
797cifs_destroy_request_bufs(void)
798{
799	mempool_destroy(cifs_req_poolp);
800	kmem_cache_destroy(cifs_req_cachep);
801	mempool_destroy(cifs_sm_req_poolp);
802	kmem_cache_destroy(cifs_sm_req_cachep);
803}
804
805static int
806cifs_init_mids(void)
807{
808	cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
809				sizeof (struct mid_q_entry), 0,
810				SLAB_HWCACHE_ALIGN, NULL, NULL);
811	if (cifs_mid_cachep == NULL)
812		return -ENOMEM;
813
814	/* 3 is a reasonable minimum number of simultaneous operations */
815	cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
816	if (cifs_mid_poolp == NULL) {
817		kmem_cache_destroy(cifs_mid_cachep);
818		return -ENOMEM;
819	}
820
821	cifs_oplock_cachep = kmem_cache_create("cifs_oplock_structs",
822				sizeof (struct oplock_q_entry), 0,
823				SLAB_HWCACHE_ALIGN, NULL, NULL);
824	if (cifs_oplock_cachep == NULL) {
825		mempool_destroy(cifs_mid_poolp);
826		kmem_cache_destroy(cifs_mid_cachep);
827		return -ENOMEM;
828	}
829
830	return 0;
831}
832
833static void
834cifs_destroy_mids(void)
835{
836	mempool_destroy(cifs_mid_poolp);
837	kmem_cache_destroy(cifs_mid_cachep);
838	kmem_cache_destroy(cifs_oplock_cachep);
839}
840
841static int cifs_oplock_thread(void * dummyarg)
842{
843	struct oplock_q_entry * oplock_item;
844	struct cifsTconInfo *pTcon;
845	struct inode * inode;
846	__u16  netfid;
847	int rc;
848
849	do {
850		if (try_to_freeze())
851			continue;
852
853		spin_lock(&GlobalMid_Lock);
854		if (list_empty(&GlobalOplock_Q)) {
855			spin_unlock(&GlobalMid_Lock);
856			set_current_state(TASK_INTERRUPTIBLE);
857			schedule_timeout(39*HZ);
858		} else {
859			oplock_item = list_entry(GlobalOplock_Q.next,
860				struct oplock_q_entry, qhead);
861			if (oplock_item) {
862				cFYI(1,("found oplock item to write out"));
863				pTcon = oplock_item->tcon;
864				inode = oplock_item->pinode;
865				netfid = oplock_item->netfid;
866				spin_unlock(&GlobalMid_Lock);
867				DeleteOplockQEntry(oplock_item);
868				/* can not grab inode sem here since it would
869				deadlock when oplock received on delete
870				since vfs_unlink holds the i_mutex across
871				the call */
872				/* mutex_lock(&inode->i_mutex);*/
873				if (S_ISREG(inode->i_mode)) {
874					rc = filemap_fdatawrite(inode->i_mapping);
875					if (CIFS_I(inode)->clientCanCacheRead == 0) {
876						filemap_fdatawait(inode->i_mapping);
877						invalidate_remote_inode(inode);
878					}
879				} else
880					rc = 0;
881				/* mutex_unlock(&inode->i_mutex);*/
882				if (rc)
883					CIFS_I(inode)->write_behind_rc = rc;
884				cFYI(1,("Oplock flush inode %p rc %d",inode,rc));
885
886				/* releasing a stale oplock after recent reconnection
887				of smb session using a now incorrect file
888				handle is not a data integrity issue but do
889				not bother sending an oplock release if session
890				to server still is disconnected since oplock
891				already released by the server in that case */
892				if (pTcon->tidStatus != CifsNeedReconnect) {
893				    rc = CIFSSMBLock(0, pTcon, netfid,
894					    0 /* len */ , 0 /* offset */, 0,
895					    0, LOCKING_ANDX_OPLOCK_RELEASE,
896					    0 /* wait flag */);
897					cFYI(1,("Oplock release rc = %d ",rc));
898				}
899			} else
900				spin_unlock(&GlobalMid_Lock);
901			set_current_state(TASK_INTERRUPTIBLE);
902			schedule_timeout(1);  /* yield in case q were corrupt */
903		}
904	} while (!kthread_should_stop());
905
906	return 0;
907}
908
909static int cifs_dnotify_thread(void * dummyarg)
910{
911	struct list_head *tmp;
912	struct cifsSesInfo *ses;
913
914	do {
915		if (try_to_freeze())
916			continue;
917		set_current_state(TASK_INTERRUPTIBLE);
918		schedule_timeout(15*HZ);
919		read_lock(&GlobalSMBSeslock);
920		/* check if any stuck requests that need
921		   to be woken up and wakeq so the
922		   thread can wake up and error out */
923		list_for_each(tmp, &GlobalSMBSessionList) {
924			ses = list_entry(tmp, struct cifsSesInfo,
925				cifsSessionList);
926			if (ses && ses->server &&
927			     atomic_read(&ses->server->inFlight))
928				wake_up_all(&ses->server->response_q);
929		}
930		read_unlock(&GlobalSMBSeslock);
931	} while (!kthread_should_stop());
932
933	return 0;
934}
935
936static int __init
937init_cifs(void)
938{
939	int rc = 0;
940#ifdef CONFIG_PROC_FS
941	cifs_proc_init();
942#endif
943/*	INIT_LIST_HEAD(&GlobalServerList);*/	/* BB not implemented yet */
944	INIT_LIST_HEAD(&GlobalSMBSessionList);
945	INIT_LIST_HEAD(&GlobalTreeConnectionList);
946	INIT_LIST_HEAD(&GlobalOplock_Q);
947#ifdef CONFIG_CIFS_EXPERIMENTAL
948	INIT_LIST_HEAD(&GlobalDnotifyReqList);
949	INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
950#endif
951/*
952 *  Initialize Global counters
953 */
954	atomic_set(&sesInfoAllocCount, 0);
955	atomic_set(&tconInfoAllocCount, 0);
956	atomic_set(&tcpSesAllocCount,0);
957	atomic_set(&tcpSesReconnectCount, 0);
958	atomic_set(&tconInfoReconnectCount, 0);
959
960	atomic_set(&bufAllocCount, 0);
961	atomic_set(&smBufAllocCount, 0);
962#ifdef CONFIG_CIFS_STATS2
963	atomic_set(&totBufAllocCount, 0);
964	atomic_set(&totSmBufAllocCount, 0);
965#endif /* CONFIG_CIFS_STATS2 */
966
967	atomic_set(&midCount, 0);
968	GlobalCurrentXid = 0;
969	GlobalTotalActiveXid = 0;
970	GlobalMaxActiveXid = 0;
971	memset(Local_System_Name, 0, 15);
972	rwlock_init(&GlobalSMBSeslock);
973	spin_lock_init(&GlobalMid_Lock);
974
975	if (cifs_max_pending < 2) {
976		cifs_max_pending = 2;
977		cFYI(1,("cifs_max_pending set to min of 2"));
978	} else if (cifs_max_pending > 256) {
979		cifs_max_pending = 256;
980		cFYI(1,("cifs_max_pending set to max of 256"));
981	}
982
983	rc = cifs_init_inodecache();
984	if (rc)
985		goto out_clean_proc;
986
987	rc = cifs_init_mids();
988	if (rc)
989		goto out_destroy_inodecache;
990
991	rc = cifs_init_request_bufs();
992	if (rc)
993		goto out_destroy_mids;
994
995	rc = register_filesystem(&cifs_fs_type);
996	if (rc)
997		goto out_destroy_request_bufs;
998
999	oplockThread = kthread_run(cifs_oplock_thread, NULL, "cifsoplockd");
1000	if (IS_ERR(oplockThread)) {
1001		rc = PTR_ERR(oplockThread);
1002		cERROR(1,("error %d create oplock thread", rc));
1003		goto out_unregister_filesystem;
1004	}
1005
1006	dnotifyThread = kthread_run(cifs_dnotify_thread, NULL, "cifsdnotifyd");
1007	if (IS_ERR(dnotifyThread)) {
1008		rc = PTR_ERR(dnotifyThread);
1009		cERROR(1,("error %d create dnotify thread", rc));
1010		goto out_stop_oplock_thread;
1011	}
1012
1013	return 0;
1014
1015 out_stop_oplock_thread:
1016	kthread_stop(oplockThread);
1017 out_unregister_filesystem:
1018	unregister_filesystem(&cifs_fs_type);
1019 out_destroy_request_bufs:
1020	cifs_destroy_request_bufs();
1021 out_destroy_mids:
1022	cifs_destroy_mids();
1023 out_destroy_inodecache:
1024	cifs_destroy_inodecache();
1025 out_clean_proc:
1026#ifdef CONFIG_PROC_FS
1027	cifs_proc_clean();
1028#endif
1029	return rc;
1030}
1031
1032static void __exit
1033exit_cifs(void)
1034{
1035	cFYI(0, ("In unregister ie exit_cifs"));
1036#ifdef CONFIG_PROC_FS
1037	cifs_proc_clean();
1038#endif
1039	unregister_filesystem(&cifs_fs_type);
1040	cifs_destroy_inodecache();
1041	cifs_destroy_mids();
1042	cifs_destroy_request_bufs();
1043	kthread_stop(oplockThread);
1044	kthread_stop(dnotifyThread);
1045}
1046
1047MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
1048MODULE_LICENSE("GPL");		/* combination of LGPL + GPL source behaves as GPL */
1049MODULE_DESCRIPTION
1050    ("VFS to access servers complying with the SNIA CIFS Specification e.g. Samba and Windows");
1051MODULE_VERSION(CIFS_VERSION);
1052module_init(init_cifs)
1053module_exit(exit_cifs)
1054