blob: 94c0f55d7669e20bb9cf92adec1af5008209c138 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/cifsfs.c
3 *
Steve French35c11fd2007-02-27 05:09:35 +00004 * Copyright (C) International Business Machines Corp., 2002,2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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>
Steve French6ab16d22005-11-29 20:55:11 -080035#include <linux/delay.h>
Steve French45af7a02006-04-21 22:52:25 +000036#include <linux/kthread.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080037#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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>
Jeff Layton84a15b92007-11-03 05:02:24 +000046#include <linux/key-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDUs */
48
49#ifdef CONFIG_CIFS_QUOTA
50static struct quotactl_ops cifs_quotactl_ops;
Steve French35c11fd2007-02-27 05:09:35 +000051#endif /* QUOTA */
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053int cifsFYI = 0;
54int cifsERROR = 1;
55int traceSMB = 0;
56unsigned int oplockEnabled = 1;
57unsigned int experimEnabled = 0;
58unsigned int linuxExtEnabled = 1;
59unsigned int lookupCacheEnabled = 1;
60unsigned int multiuser_mount = 0;
Steve French39798772006-05-31 22:40:51 +000061unsigned int extended_security = CIFSSEC_DEF;
62/* unsigned int ntlmv2_support = 0; */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063unsigned int sign_CIFS_PDUs = 1;
Steve French6dc0f872007-07-06 23:13:06 +000064extern struct task_struct *oplockThread; /* remove sparse warning */
65struct task_struct *oplockThread = NULL;
Steve French99ee4db2007-02-27 05:35:17 +000066/* extern struct task_struct * dnotifyThread; remove sparse warning */
Steve French6dc0f872007-07-06 23:13:06 +000067static struct task_struct *dnotifyThread = NULL;
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080068static const struct super_operations cifs_super_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
70module_param(CIFSMaxBufSize, int, 0);
Steve French63135e02007-07-17 17:34:02 +000071MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header). "
72 "Default: 16384 Range: 8192 to 130048");
Linus Torvalds1da177e2005-04-16 15:20:36 -070073unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
74module_param(cifs_min_rcv, int, 0);
Steve French63135e02007-07-17 17:34:02 +000075MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
76 "1 to 64");
Linus Torvalds1da177e2005-04-16 15:20:36 -070077unsigned int cifs_min_small = 30;
78module_param(cifs_min_small, int, 0);
Steve French63135e02007-07-17 17:34:02 +000079MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
80 "Range: 2 to 256");
Linus Torvalds1da177e2005-04-16 15:20:36 -070081unsigned int cifs_max_pending = CIFS_MAX_REQ;
82module_param(cifs_max_pending, int, 0);
Steve French63135e02007-07-17 17:34:02 +000083MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server. "
84 "Default: 50 Range: 2 to 256");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086extern mempool_t *cifs_sm_req_poolp;
87extern mempool_t *cifs_req_poolp;
88extern mempool_t *cifs_mid_poolp;
89
Christoph Lametere18b8902006-12-06 20:33:20 -080090extern struct kmem_cache *cifs_oplock_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92static int
93cifs_read_super(struct super_block *sb, void *data,
94 const char *devname, int silent)
95{
96 struct inode *inode;
97 struct cifs_sb_info *cifs_sb;
98 int rc = 0;
Steve French50c2f752007-07-13 00:33:32 +000099
Steve French1b2b2122007-02-17 04:30:54 +0000100 /* BB should we make this contingent on mount parm? */
101 sb->s_flags |= MS_NODIRATIME | MS_NOATIME;
Steve French790fe572007-07-07 19:25:05 +0000102 sb->s_fs_info = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 cifs_sb = CIFS_SB(sb);
Steve French4523cc32007-04-30 20:13:06 +0000104 if (cifs_sb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 rc = cifs_mount(sb, cifs_sb, data, devname);
108
109 if (rc) {
110 if (!silent)
111 cERROR(1,
112 ("cifs_mount failed w/return code = %d", rc));
113 goto out_mount_failed;
114 }
115
116 sb->s_magic = CIFS_MAGIC_NUMBER;
117 sb->s_op = &cifs_super_ops;
Steve French4523cc32007-04-30 20:13:06 +0000118/* if (cifs_sb->tcon->ses->server->maxBuf > MAX_CIFS_HDR_SIZE + 512)
Steve French790fe572007-07-07 19:25:05 +0000119 sb->s_blocksize =
120 cifs_sb->tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#ifdef CONFIG_CIFS_QUOTA
122 sb->s_qcop = &cifs_quotactl_ops;
123#endif
124 sb->s_blocksize = CIFS_MAX_MSGSIZE;
125 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
126 inode = iget(sb, ROOT_I);
127
128 if (!inode) {
129 rc = -ENOMEM;
130 goto out_no_root;
131 }
132
133 sb->s_root = d_alloc_root(inode);
134
135 if (!sb->s_root) {
136 rc = -ENOMEM;
137 goto out_no_root;
138 }
Steve French50c2f752007-07-13 00:33:32 +0000139
Steve French7521a3c2007-07-11 18:30:34 +0000140#ifdef CONFIG_CIFS_EXPERIMENTAL
141 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
142 cFYI(1, ("export ops supported"));
143 sb->s_export_op = &cifs_export_ops;
144 }
145#endif /* EXPERIMENTAL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 return 0;
148
149out_no_root:
150 cERROR(1, ("cifs_read_super: get root inode failed"));
151 if (inode)
152 iput(inode);
153
154out_mount_failed:
Steve French4523cc32007-04-30 20:13:06 +0000155 if (cifs_sb) {
156 if (cifs_sb->local_nls)
Steve French50c2f752007-07-13 00:33:32 +0000157 unload_nls(cifs_sb->local_nls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 kfree(cifs_sb);
159 }
160 return rc;
161}
162
163static void
164cifs_put_super(struct super_block *sb)
165{
166 int rc = 0;
167 struct cifs_sb_info *cifs_sb;
168
169 cFYI(1, ("In cifs_put_super"));
170 cifs_sb = CIFS_SB(sb);
Steve French4523cc32007-04-30 20:13:06 +0000171 if (cifs_sb == NULL) {
Steve French790fe572007-07-07 19:25:05 +0000172 cFYI(1, ("Empty cifs superblock info passed to unmount"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return;
174 }
Steve French790fe572007-07-07 19:25:05 +0000175 rc = cifs_umount(sb, cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (rc) {
177 cERROR(1, ("cifs_umount failed with return code %d", rc));
178 }
179 unload_nls(cifs_sb->local_nls);
180 kfree(cifs_sb);
181 return;
182}
183
184static int
David Howells726c3342006-06-23 02:02:58 -0700185cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
David Howells726c3342006-06-23 02:02:58 -0700187 struct super_block *sb = dentry->d_sb;
Steve French790fe572007-07-07 19:25:05 +0000188 int xid;
Steve Frenchc81156d2005-04-28 22:41:07 -0700189 int rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 struct cifs_sb_info *cifs_sb;
191 struct cifsTconInfo *pTcon;
192
193 xid = GetXid();
194
195 cifs_sb = CIFS_SB(sb);
196 pTcon = cifs_sb->tcon;
197
198 buf->f_type = CIFS_MAGIC_NUMBER;
199
200 /* instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO */
Steve French790fe572007-07-07 19:25:05 +0000201 buf->f_namelen = PATH_MAX; /* PATH_MAX may be too long - it would
Steve Frenchc81156d2005-04-28 22:41:07 -0700202 presumably be total path, but note
203 that some servers (includinng Samba 3)
204 have a shorter maximum path */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 buf->f_files = 0; /* undefined */
206 buf->f_ffree = 0; /* unlimited */
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/* BB we could add a second check for a QFS Unix capability bit */
Steve Frenchf28ac912005-04-28 22:41:07 -0700209/* BB FIXME check CIFS_POSIX_EXTENSIONS Unix cap first FIXME BB */
Steve Frenchc81156d2005-04-28 22:41:07 -0700210 if ((pTcon->ses->capabilities & CAP_UNIX) && (CIFS_POSIX_EXTENSIONS &
211 le64_to_cpu(pTcon->fsUnixInfo.Capability)))
Steve French737b7582005-04-28 22:41:06 -0700212 rc = CIFSSMBQFSPosixInfo(xid, pTcon, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* Only need to call the old QFSInfo if failed
215 on newer one */
Steve French4523cc32007-04-30 20:13:06 +0000216 if (rc)
217 if (pTcon->ses->capabilities & CAP_NT_SMBS)
Steve French9ac00b72006-09-30 04:13:17 +0000218 rc = CIFSSMBQFSInfo(xid, pTcon, buf); /* not supported by OS2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Steve French9ac00b72006-09-30 04:13:17 +0000220 /* Some old Windows servers also do not support level 103, retry with
221 older level one if old server failed the previous call or we
222 bypassed it because we detected that this was an older LANMAN sess */
Steve French4523cc32007-04-30 20:13:06 +0000223 if (rc)
Steve French20962432005-09-21 22:05:57 -0700224 rc = SMBOldQFSInfo(xid, pTcon, buf);
Steve French790fe572007-07-07 19:25:05 +0000225 /* int f_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 __fsid_t f_fsid;
227 int f_namelen; */
Steve Frenchc81156d2005-04-28 22:41:07 -0700228 /* BB get from info in tcon struct at mount time call to QFSAttrInfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 FreeXid(xid);
Steve Frenchc81156d2005-04-28 22:41:07 -0700230 return 0; /* always return success? what if volume is no
231 longer available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Steve French50c2f752007-07-13 00:33:32 +0000234static int cifs_permission(struct inode *inode, int mask, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 struct cifs_sb_info *cifs_sb;
237
238 cifs_sb = CIFS_SB(inode->i_sb);
239
Steve French26f57362007-08-30 22:09:15 +0000240 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return 0;
Steve French26f57362007-08-30 22:09:15 +0000242 else /* file mode might have been restricted at mount time
Steve French50c2f752007-07-13 00:33:32 +0000243 on the client (above and beyond ACL on servers) for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 servers which do not support setting and viewing mode bits,
Steve French50c2f752007-07-13 00:33:32 +0000245 so allowing client to check permissions is useful */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return generic_permission(inode, mask, NULL);
247}
248
Christoph Lametere18b8902006-12-06 20:33:20 -0800249static struct kmem_cache *cifs_inode_cachep;
250static struct kmem_cache *cifs_req_cachep;
251static struct kmem_cache *cifs_mid_cachep;
252struct kmem_cache *cifs_oplock_cachep;
253static struct kmem_cache *cifs_sm_req_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254mempool_t *cifs_sm_req_poolp;
255mempool_t *cifs_req_poolp;
256mempool_t *cifs_mid_poolp;
257
258static struct inode *
259cifs_alloc_inode(struct super_block *sb)
260{
261 struct cifsInodeInfo *cifs_inode;
Christoph Lametere94b1762006-12-06 20:33:17 -0800262 cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (!cifs_inode)
264 return NULL;
265 cifs_inode->cifsAttrs = 0x20; /* default */
266 atomic_set(&cifs_inode->inUse, 0);
267 cifs_inode->time = 0;
268 /* Until the file is open and we have gotten oplock
269 info back from the server, can not assume caching of
270 file data or metadata */
271 cifs_inode->clientCanCacheRead = FALSE;
272 cifs_inode->clientCanCacheAll = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
Steve French50c2f752007-07-13 00:33:32 +0000274
Steve French1b2b2122007-02-17 04:30:54 +0000275 /* Can not set i_flags here - they get immediately overwritten
276 to zero by the VFS */
277/* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 INIT_LIST_HEAD(&cifs_inode->openFileList);
279 return &cifs_inode->vfs_inode;
280}
281
282static void
283cifs_destroy_inode(struct inode *inode)
284{
285 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
286}
287
288/*
289 * cifs_show_options() is for displaying mount options in /proc/mounts.
290 * Not all settable options are displayed but most of the important
291 * ones are.
292 */
293static int
294cifs_show_options(struct seq_file *s, struct vfsmount *m)
295{
296 struct cifs_sb_info *cifs_sb;
297
298 cifs_sb = CIFS_SB(m->mnt_sb);
299
300 if (cifs_sb) {
301 if (cifs_sb->tcon) {
Steve French004c46b2007-02-17 04:34:13 +0000302/* BB add prepath to mount options displayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 seq_printf(s, ",unc=%s", cifs_sb->tcon->treeName);
304 if (cifs_sb->tcon->ses) {
305 if (cifs_sb->tcon->ses->userName)
306 seq_printf(s, ",username=%s",
307 cifs_sb->tcon->ses->userName);
Steve French4523cc32007-04-30 20:13:06 +0000308 if (cifs_sb->tcon->ses->domainName)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 seq_printf(s, ",domain=%s",
310 cifs_sb->tcon->ses->domainName);
311 }
Steve Frenchd5d18502007-10-17 21:31:52 +0000312 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) ||
313 !(cifs_sb->tcon->unix_ext))
314 seq_printf(s, ",uid=%d", cifs_sb->mnt_uid);
315 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) ||
316 !(cifs_sb->tcon->unix_ext))
317 seq_printf(s, ",gid=%d", cifs_sb->mnt_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Steve French4523cc32007-04-30 20:13:06 +0000319 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
320 seq_printf(s, ",posixpaths");
Steve French50c2f752007-07-13 00:33:32 +0000321 seq_printf(s, ",rsize=%d", cifs_sb->rsize);
322 seq_printf(s, ",wsize=%d", cifs_sb->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 return 0;
325}
326
327#ifdef CONFIG_CIFS_QUOTA
Steve French50c2f752007-07-13 00:33:32 +0000328int cifs_xquota_set(struct super_block *sb, int quota_type, qid_t qid,
329 struct fs_disk_quota *pdquota)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 int xid;
332 int rc = 0;
333 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
334 struct cifsTconInfo *pTcon;
Steve French50c2f752007-07-13 00:33:32 +0000335
Steve French4523cc32007-04-30 20:13:06 +0000336 if (cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 pTcon = cifs_sb->tcon;
338 else
339 return -EIO;
340
341
342 xid = GetXid();
Steve French4523cc32007-04-30 20:13:06 +0000343 if (pTcon) {
Steve French50c2f752007-07-13 00:33:32 +0000344 cFYI(1, ("set type: 0x%x id: %d", quota_type, qid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 } else {
Cyrill Gorcunov95ba7362007-08-24 00:23:36 +0000346 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 FreeXid(xid);
350 return rc;
351}
352
Steve French50c2f752007-07-13 00:33:32 +0000353int cifs_xquota_get(struct super_block *sb, int quota_type, qid_t qid,
354 struct fs_disk_quota *pdquota)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 int xid;
357 int rc = 0;
358 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
359 struct cifsTconInfo *pTcon;
360
Steve French4523cc32007-04-30 20:13:06 +0000361 if (cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 pTcon = cifs_sb->tcon;
363 else
364 return -EIO;
365
366 xid = GetXid();
Steve French4523cc32007-04-30 20:13:06 +0000367 if (pTcon) {
Steve French50c2f752007-07-13 00:33:32 +0000368 cFYI(1, ("set type: 0x%x id: %d", quota_type, qid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 } else {
370 rc = -EIO;
371 }
372
373 FreeXid(xid);
374 return rc;
375}
376
Steve French50c2f752007-07-13 00:33:32 +0000377int cifs_xstate_set(struct super_block *sb, unsigned int flags, int operation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Steve French50c2f752007-07-13 00:33:32 +0000379 int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 int rc = 0;
381 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
382 struct cifsTconInfo *pTcon;
383
Steve French4523cc32007-04-30 20:13:06 +0000384 if (cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 pTcon = cifs_sb->tcon;
386 else
387 return -EIO;
388
389 xid = GetXid();
Steve French4523cc32007-04-30 20:13:06 +0000390 if (pTcon) {
Steve French50c2f752007-07-13 00:33:32 +0000391 cFYI(1, ("flags: 0x%x operation: 0x%x", flags, operation));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 } else {
393 rc = -EIO;
394 }
395
396 FreeXid(xid);
397 return rc;
398}
399
Steve French50c2f752007-07-13 00:33:32 +0000400int cifs_xstate_get(struct super_block *sb, struct fs_quota_stat *qstats)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 int xid;
403 int rc = 0;
404 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
405 struct cifsTconInfo *pTcon;
406
Steve French4523cc32007-04-30 20:13:06 +0000407 if (cifs_sb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 pTcon = cifs_sb->tcon;
409 } else {
410 return -EIO;
411 }
412 xid = GetXid();
Steve French4523cc32007-04-30 20:13:06 +0000413 if (pTcon) {
Steve French50c2f752007-07-13 00:33:32 +0000414 cFYI(1, ("pqstats %p", qstats));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 } else {
416 rc = -EIO;
417 }
418
419 FreeXid(xid);
420 return rc;
421}
422
423static struct quotactl_ops cifs_quotactl_ops = {
424 .set_xquota = cifs_xquota_set,
425 .get_xquota = cifs_xquota_set,
426 .set_xstate = cifs_xstate_set,
427 .get_xstate = cifs_xstate_get,
428};
429#endif
430
Steve French50c2f752007-07-13 00:33:32 +0000431static void cifs_umount_begin(struct vfsmount *vfsmnt, int flags)
Steve French68058e72005-10-10 10:34:22 -0700432{
Steve French5e1253b2005-10-10 14:06:37 -0700433 struct cifs_sb_info *cifs_sb;
Steve French50c2f752007-07-13 00:33:32 +0000434 struct cifsTconInfo *tcon;
Steve French68058e72005-10-10 10:34:22 -0700435
Trond Myklebust8b512d92006-06-09 09:34:18 -0400436 if (!(flags & MNT_FORCE))
437 return;
438 cifs_sb = CIFS_SB(vfsmnt->mnt_sb);
Steve French4523cc32007-04-30 20:13:06 +0000439 if (cifs_sb == NULL)
Steve French9e2e85f2005-10-10 14:28:38 -0700440 return;
441
442 tcon = cifs_sb->tcon;
Steve French4523cc32007-04-30 20:13:06 +0000443 if (tcon == NULL)
Steve French9e2e85f2005-10-10 14:28:38 -0700444 return;
Steve French5e1253b2005-10-10 14:06:37 -0700445 down(&tcon->tconSem);
446 if (atomic_read(&tcon->useCount) == 1)
447 tcon->tidStatus = CifsExiting;
448 up(&tcon->tconSem);
449
Steve French3a5ff612006-07-14 22:37:11 +0000450 /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
Steve French7b7abfe2005-11-09 15:21:09 -0800451 /* cancel_notify_requests(tcon); */
Steve French50c2f752007-07-13 00:33:32 +0000452 if (tcon->ses && tcon->ses->server) {
453 cFYI(1, ("wake up tasks now - umount begin not complete"));
Steve French9e2e85f2005-10-10 14:28:38 -0700454 wake_up_all(&tcon->ses->server->request_q);
Steve French6ab16d22005-11-29 20:55:11 -0800455 wake_up_all(&tcon->ses->server->response_q);
456 msleep(1); /* yield */
457 /* we have to kick the requests once more */
458 wake_up_all(&tcon->ses->server->response_q);
459 msleep(1);
Steve French5e1253b2005-10-10 14:06:37 -0700460 }
461/* BB FIXME - finish add checks for tidStatus BB */
Steve French68058e72005-10-10 10:34:22 -0700462
463 return;
464}
Steve French68058e72005-10-10 10:34:22 -0700465
Steve Frenchbf97d282006-09-28 21:34:06 +0000466#ifdef CONFIG_CIFS_STATS2
467static int cifs_show_stats(struct seq_file *s, struct vfsmount *mnt)
468{
469 /* BB FIXME */
470 return 0;
471}
472#endif
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474static int cifs_remount(struct super_block *sb, int *flags, char *data)
475{
476 *flags |= MS_NODIRATIME;
477 return 0;
478}
479
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800480static const struct super_operations cifs_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 .read_inode = cifs_read_inode,
482 .put_super = cifs_put_super,
483 .statfs = cifs_statfs,
484 .alloc_inode = cifs_alloc_inode,
485 .destroy_inode = cifs_destroy_inode,
Steve French50c2f752007-07-13 00:33:32 +0000486/* .drop_inode = generic_delete_inode,
487 .delete_inode = cifs_delete_inode, */ /* Do not need above two
488 functions unless later we add lazy close of inodes or unless the
489 kernel forgets to call us with the same number of releases (closes)
490 as opens */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 .show_options = cifs_show_options,
Steve French7b7abfe2005-11-09 15:21:09 -0800492 .umount_begin = cifs_umount_begin,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 .remount_fs = cifs_remount,
Steve Frenchbf97d282006-09-28 21:34:06 +0000494#ifdef CONFIG_CIFS_STATS2
Steve Frenchf46d3e12006-09-30 01:08:55 +0000495 .show_stats = cifs_show_stats,
Steve Frenchbf97d282006-09-28 21:34:06 +0000496#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497};
498
David Howells454e2392006-06-23 02:02:57 -0700499static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500cifs_get_sb(struct file_system_type *fs_type,
David Howells454e2392006-06-23 02:02:57 -0700501 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 int rc;
504 struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
505
506 cFYI(1, ("Devname: %s flags: %d ", dev_name, flags));
507
508 if (IS_ERR(sb))
David Howells454e2392006-06-23 02:02:57 -0700509 return PTR_ERR(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 sb->s_flags = flags;
512
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800513 rc = cifs_read_super(sb, data, dev_name, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (rc) {
515 up_write(&sb->s_umount);
516 deactivate_super(sb);
David Howells454e2392006-06-23 02:02:57 -0700517 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 sb->s_flags |= MS_ACTIVE;
David Howells454e2392006-06-23 02:02:57 -0700520 return simple_set_mnt(mnt, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Badari Pulavarty027445c2006-09-30 23:28:46 -0700523static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
524 unsigned long nr_segs, loff_t pos)
Steve French87c89dd2005-11-17 17:03:00 -0800525{
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800526 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
Steve French87c89dd2005-11-17 17:03:00 -0800527 ssize_t written;
528
Badari Pulavarty027445c2006-09-30 23:28:46 -0700529 written = generic_file_aio_write(iocb, iov, nr_segs, pos);
Steve French87c89dd2005-11-17 17:03:00 -0800530 if (!CIFS_I(inode)->clientCanCacheAll)
531 filemap_fdatawrite(inode->i_mapping);
532 return written;
533}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Steve Frenchc32a0b62006-01-12 14:41:28 -0800535static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
536{
537 /* origin == SEEK_END => we must revalidate the cached file length */
Steve French0889a942006-09-23 22:11:07 +0000538 if (origin == SEEK_END) {
Steve French030e9d82007-02-01 04:27:59 +0000539 int retval;
540
541 /* some applications poll for the file length in this strange
542 way so we must seek to end on non-oplocked files by
543 setting the revalidate time to zero */
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000544 CIFS_I(file->f_path.dentry->d_inode)->time = 0;
Steve French030e9d82007-02-01 04:27:59 +0000545
546 retval = cifs_revalidate(file->f_path.dentry);
Steve Frenchc32a0b62006-01-12 14:41:28 -0800547 if (retval < 0)
548 return (loff_t)retval;
549 }
550 return remote_llseek(file, offset, origin);
551}
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553static struct file_system_type cifs_fs_type = {
554 .owner = THIS_MODULE,
555 .name = "cifs",
556 .get_sb = cifs_get_sb,
557 .kill_sb = kill_anon_super,
558 /* .fs_flags */
559};
Arjan van de Ven754661f2007-02-12 00:55:38 -0800560const struct inode_operations cifs_dir_inode_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 .create = cifs_create,
562 .lookup = cifs_lookup,
563 .getattr = cifs_getattr,
564 .unlink = cifs_unlink,
565 .link = cifs_hardlink,
566 .mkdir = cifs_mkdir,
567 .rmdir = cifs_rmdir,
568 .rename = cifs_rename,
569 .permission = cifs_permission,
570/* revalidate:cifs_revalidate, */
571 .setattr = cifs_setattr,
572 .symlink = cifs_symlink,
573 .mknod = cifs_mknod,
574#ifdef CONFIG_CIFS_XATTR
575 .setxattr = cifs_setxattr,
576 .getxattr = cifs_getxattr,
577 .listxattr = cifs_listxattr,
578 .removexattr = cifs_removexattr,
579#endif
580};
581
Arjan van de Ven754661f2007-02-12 00:55:38 -0800582const struct inode_operations cifs_file_inode_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/* revalidate:cifs_revalidate, */
584 .setattr = cifs_setattr,
585 .getattr = cifs_getattr, /* do we need this anymore? */
586 .rename = cifs_rename,
587 .permission = cifs_permission,
588#ifdef CONFIG_CIFS_XATTR
589 .setxattr = cifs_setxattr,
590 .getxattr = cifs_getxattr,
591 .listxattr = cifs_listxattr,
592 .removexattr = cifs_removexattr,
Steve French50c2f752007-07-13 00:33:32 +0000593#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594};
595
Arjan van de Ven754661f2007-02-12 00:55:38 -0800596const struct inode_operations cifs_symlink_inode_ops = {
Steve French50c2f752007-07-13 00:33:32 +0000597 .readlink = generic_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 .follow_link = cifs_follow_link,
599 .put_link = cifs_put_link,
600 .permission = cifs_permission,
601 /* BB add the following two eventually */
602 /* revalidate: cifs_revalidate,
603 setattr: cifs_notify_change, *//* BB do we need notify change */
604#ifdef CONFIG_CIFS_XATTR
605 .setxattr = cifs_setxattr,
606 .getxattr = cifs_getxattr,
607 .listxattr = cifs_listxattr,
608 .removexattr = cifs_removexattr,
Steve French50c2f752007-07-13 00:33:32 +0000609#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610};
611
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800612const struct file_operations cifs_file_ops = {
Steve French87c89dd2005-11-17 17:03:00 -0800613 .read = do_sync_read,
614 .write = do_sync_write,
Steve French87c89dd2005-11-17 17:03:00 -0800615 .aio_read = generic_file_aio_read,
616 .aio_write = cifs_file_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 .open = cifs_open,
618 .release = cifs_close,
619 .lock = cifs_lock,
620 .fsync = cifs_fsync,
621 .flush = cifs_flush,
622 .mmap = cifs_file_mmap,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200623 .splice_read = generic_file_splice_read,
Steve Frenchc32a0b62006-01-12 14:41:28 -0800624 .llseek = cifs_llseek,
Steve Frenchc67593a2005-04-28 22:41:04 -0700625#ifdef CONFIG_CIFS_POSIX
626 .ioctl = cifs_ioctl,
627#endif /* CONFIG_CIFS_POSIX */
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629#ifdef CONFIG_CIFS_EXPERIMENTAL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 .dir_notify = cifs_dir_notify,
631#endif /* CONFIG_CIFS_EXPERIMENTAL */
632};
633
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800634const struct file_operations cifs_file_direct_ops = {
Steve French50c2f752007-07-13 00:33:32 +0000635 /* no mmap, no aio, no readv -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 BB reevaluate whether they can be done with directio, no cache */
637 .read = cifs_user_read,
638 .write = cifs_user_write,
639 .open = cifs_open,
640 .release = cifs_close,
641 .lock = cifs_lock,
642 .fsync = cifs_fsync,
643 .flush = cifs_flush,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200644 .splice_read = generic_file_splice_read,
Steve Frenchc67593a2005-04-28 22:41:04 -0700645#ifdef CONFIG_CIFS_POSIX
646 .ioctl = cifs_ioctl,
647#endif /* CONFIG_CIFS_POSIX */
Steve Frenchc32a0b62006-01-12 14:41:28 -0800648 .llseek = cifs_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649#ifdef CONFIG_CIFS_EXPERIMENTAL
650 .dir_notify = cifs_dir_notify,
651#endif /* CONFIG_CIFS_EXPERIMENTAL */
652};
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800653const struct file_operations cifs_file_nobrl_ops = {
Steve French87c89dd2005-11-17 17:03:00 -0800654 .read = do_sync_read,
655 .write = do_sync_write,
Steve French87c89dd2005-11-17 17:03:00 -0800656 .aio_read = generic_file_aio_read,
657 .aio_write = cifs_file_aio_write,
658 .open = cifs_open,
659 .release = cifs_close,
660 .fsync = cifs_fsync,
661 .flush = cifs_flush,
662 .mmap = cifs_file_mmap,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200663 .splice_read = generic_file_splice_read,
Steve Frenchc32a0b62006-01-12 14:41:28 -0800664 .llseek = cifs_llseek,
Steve French8b94bcb2005-11-11 11:41:00 -0800665#ifdef CONFIG_CIFS_POSIX
Steve French87c89dd2005-11-17 17:03:00 -0800666 .ioctl = cifs_ioctl,
Steve French8b94bcb2005-11-11 11:41:00 -0800667#endif /* CONFIG_CIFS_POSIX */
668
669#ifdef CONFIG_CIFS_EXPERIMENTAL
Steve French87c89dd2005-11-17 17:03:00 -0800670 .dir_notify = cifs_dir_notify,
Steve French8b94bcb2005-11-11 11:41:00 -0800671#endif /* CONFIG_CIFS_EXPERIMENTAL */
672};
673
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800674const struct file_operations cifs_file_direct_nobrl_ops = {
Steve French50c2f752007-07-13 00:33:32 +0000675 /* no mmap, no aio, no readv -
Steve French87c89dd2005-11-17 17:03:00 -0800676 BB reevaluate whether they can be done with directio, no cache */
677 .read = cifs_user_read,
678 .write = cifs_user_write,
679 .open = cifs_open,
680 .release = cifs_close,
681 .fsync = cifs_fsync,
682 .flush = cifs_flush,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200683 .splice_read = generic_file_splice_read,
Steve French8b94bcb2005-11-11 11:41:00 -0800684#ifdef CONFIG_CIFS_POSIX
Steve French87c89dd2005-11-17 17:03:00 -0800685 .ioctl = cifs_ioctl,
Steve French8b94bcb2005-11-11 11:41:00 -0800686#endif /* CONFIG_CIFS_POSIX */
Steve Frenchc32a0b62006-01-12 14:41:28 -0800687 .llseek = cifs_llseek,
Steve French8b94bcb2005-11-11 11:41:00 -0800688#ifdef CONFIG_CIFS_EXPERIMENTAL
Steve French87c89dd2005-11-17 17:03:00 -0800689 .dir_notify = cifs_dir_notify,
Steve French8b94bcb2005-11-11 11:41:00 -0800690#endif /* CONFIG_CIFS_EXPERIMENTAL */
691};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800693const struct file_operations cifs_dir_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 .readdir = cifs_readdir,
695 .release = cifs_closedir,
696 .read = generic_read_dir,
697#ifdef CONFIG_CIFS_EXPERIMENTAL
698 .dir_notify = cifs_dir_notify,
699#endif /* CONFIG_CIFS_EXPERIMENTAL */
Steve French50c2f752007-07-13 00:33:32 +0000700 .ioctl = cifs_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701};
702
703static void
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700704cifs_init_once(struct kmem_cache *cachep, void *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct cifsInodeInfo *cifsi = inode;
707
Christoph Lametera35afb82007-05-16 22:10:57 -0700708 inode_init_once(&cifsi->vfs_inode);
709 INIT_LIST_HEAD(&cifsi->lockList);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712static int
713cifs_init_inodecache(void)
714{
715 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
Steve French26f57362007-08-30 22:09:15 +0000716 sizeof(struct cifsInodeInfo),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800717 0, (SLAB_RECLAIM_ACCOUNT|
718 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900719 cifs_init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (cifs_inode_cachep == NULL)
721 return -ENOMEM;
722
723 return 0;
724}
725
726static void
727cifs_destroy_inodecache(void)
728{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700729 kmem_cache_destroy(cifs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732static int
733cifs_init_request_bufs(void)
734{
Steve French4523cc32007-04-30 20:13:06 +0000735 if (CIFSMaxBufSize < 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
737 Unicode path name has to fit in any SMB/CIFS path based frames */
738 CIFSMaxBufSize = 8192;
739 } else if (CIFSMaxBufSize > 1024*127) {
740 CIFSMaxBufSize = 1024 * 127;
741 } else {
742 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
743 }
744/* cERROR(1,("CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize)); */
745 cifs_req_cachep = kmem_cache_create("cifs_request",
746 CIFSMaxBufSize +
747 MAX_CIFS_HDR_SIZE, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900748 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (cifs_req_cachep == NULL)
750 return -ENOMEM;
751
Steve French4523cc32007-04-30 20:13:06 +0000752 if (cifs_min_rcv < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 cifs_min_rcv = 1;
754 else if (cifs_min_rcv > 64) {
755 cifs_min_rcv = 64;
Steve French50c2f752007-07-13 00:33:32 +0000756 cERROR(1, ("cifs_min_rcv set to maximum (64)"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
Matthew Dobson93d23412006-03-26 01:37:50 -0800759 cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
760 cifs_req_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Steve French4523cc32007-04-30 20:13:06 +0000762 if (cifs_req_poolp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 kmem_cache_destroy(cifs_req_cachep);
764 return -ENOMEM;
765 }
Steve Frenchec637e32005-12-12 20:53:18 -0800766 /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 almost all handle based requests (but not write response, nor is it
768 sufficient for path based requests). A smaller size would have
Steve French50c2f752007-07-13 00:33:32 +0000769 been more efficient (compacting multiple slab items on one 4k page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 for the case in which debug was on, but this larger size allows
771 more SMBs to use small buffer alloc and is still much more
Steve French6dc0f872007-07-06 23:13:06 +0000772 efficient to alloc 1 per page off the slab compared to 17K (5page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 alloc of large cifs buffers even when page debugging is on */
774 cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
Steve French6dc0f872007-07-06 23:13:06 +0000775 MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900776 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (cifs_sm_req_cachep == NULL) {
778 mempool_destroy(cifs_req_poolp);
779 kmem_cache_destroy(cifs_req_cachep);
Steve French6dc0f872007-07-06 23:13:06 +0000780 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
Steve French4523cc32007-04-30 20:13:06 +0000783 if (cifs_min_small < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 cifs_min_small = 2;
785 else if (cifs_min_small > 256) {
786 cifs_min_small = 256;
Steve French6dc0f872007-07-06 23:13:06 +0000787 cFYI(1, ("cifs_min_small set to maximum (256)"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
Matthew Dobson93d23412006-03-26 01:37:50 -0800790 cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
791 cifs_sm_req_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Steve French4523cc32007-04-30 20:13:06 +0000793 if (cifs_sm_req_poolp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 mempool_destroy(cifs_req_poolp);
795 kmem_cache_destroy(cifs_req_cachep);
796 kmem_cache_destroy(cifs_sm_req_cachep);
797 return -ENOMEM;
798 }
799
800 return 0;
801}
802
803static void
804cifs_destroy_request_bufs(void)
805{
806 mempool_destroy(cifs_req_poolp);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700807 kmem_cache_destroy(cifs_req_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 mempool_destroy(cifs_sm_req_poolp);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700809 kmem_cache_destroy(cifs_sm_req_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812static int
813cifs_init_mids(void)
814{
815 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
Steve French26f57362007-08-30 22:09:15 +0000816 sizeof(struct mid_q_entry), 0,
817 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (cifs_mid_cachep == NULL)
819 return -ENOMEM;
820
Matthew Dobson93d23412006-03-26 01:37:50 -0800821 /* 3 is a reasonable minimum number of simultaneous operations */
822 cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
Steve French4523cc32007-04-30 20:13:06 +0000823 if (cifs_mid_poolp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 kmem_cache_destroy(cifs_mid_cachep);
825 return -ENOMEM;
826 }
827
828 cifs_oplock_cachep = kmem_cache_create("cifs_oplock_structs",
Steve French26f57362007-08-30 22:09:15 +0000829 sizeof(struct oplock_q_entry), 0,
830 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (cifs_oplock_cachep == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 mempool_destroy(cifs_mid_poolp);
Akinobu Mitae6985c72007-06-04 16:14:59 +0000833 kmem_cache_destroy(cifs_mid_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return -ENOMEM;
835 }
836
837 return 0;
838}
839
840static void
841cifs_destroy_mids(void)
842{
843 mempool_destroy(cifs_mid_poolp);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700844 kmem_cache_destroy(cifs_mid_cachep);
845 kmem_cache_destroy(cifs_oplock_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Steve French50c2f752007-07-13 00:33:32 +0000848static int cifs_oplock_thread(void *dummyarg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Steve French6dc0f872007-07-06 23:13:06 +0000850 struct oplock_q_entry *oplock_item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 struct cifsTconInfo *pTcon;
Steve French6dc0f872007-07-06 23:13:06 +0000852 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 __u16 netfid;
854 int rc;
855
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700856 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 do {
Steve French6dc0f872007-07-06 23:13:06 +0000858 if (try_to_freeze())
Steve Frenchede13272005-08-30 20:10:14 -0700859 continue;
Steve French50c2f752007-07-13 00:33:32 +0000860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 spin_lock(&GlobalMid_Lock);
Steve French4523cc32007-04-30 20:13:06 +0000862 if (list_empty(&GlobalOplock_Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 spin_unlock(&GlobalMid_Lock);
864 set_current_state(TASK_INTERRUPTIBLE);
865 schedule_timeout(39*HZ);
866 } else {
Steve French6dc0f872007-07-06 23:13:06 +0000867 oplock_item = list_entry(GlobalOplock_Q.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct oplock_q_entry, qhead);
Steve French4523cc32007-04-30 20:13:06 +0000869 if (oplock_item) {
Steve French50c2f752007-07-13 00:33:32 +0000870 cFYI(1, ("found oplock item to write out"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 pTcon = oplock_item->tcon;
872 inode = oplock_item->pinode;
873 netfid = oplock_item->netfid;
874 spin_unlock(&GlobalMid_Lock);
875 DeleteOplockQEntry(oplock_item);
876 /* can not grab inode sem here since it would
Steve French6dc0f872007-07-06 23:13:06 +0000877 deadlock when oplock received on delete
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800878 since vfs_unlink holds the i_mutex across
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 the call */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800880 /* mutex_lock(&inode->i_mutex);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (S_ISREG(inode->i_mode)) {
Steve French26f57362007-08-30 22:09:15 +0000882 rc =
883 filemap_fdatawrite(inode->i_mapping);
Steve French50c2f752007-07-13 00:33:32 +0000884 if (CIFS_I(inode)->clientCanCacheRead
885 == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 filemap_fdatawait(inode->i_mapping);
887 invalidate_remote_inode(inode);
888 }
889 } else
890 rc = 0;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800891 /* mutex_unlock(&inode->i_mutex);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (rc)
893 CIFS_I(inode)->write_behind_rc = rc;
Steve French6dc0f872007-07-06 23:13:06 +0000894 cFYI(1, ("Oplock flush inode %p rc %d",
895 inode, rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Steve French6dc0f872007-07-06 23:13:06 +0000897 /* releasing stale oplock after recent reconnect
898 of smb session using a now incorrect file
899 handle is not a data integrity issue but do
900 not bother sending an oplock release if session
901 to server still is disconnected since oplock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 already released by the server in that case */
Steve French4523cc32007-04-30 20:13:06 +0000903 if (pTcon->tidStatus != CifsNeedReconnect) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 rc = CIFSSMBLock(0, pTcon, netfid,
Steve French6dc0f872007-07-06 23:13:06 +0000905 0 /* len */ , 0 /* offset */, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 0, LOCKING_ANDX_OPLOCK_RELEASE,
907 0 /* wait flag */);
Steve French26f57362007-08-30 22:09:15 +0000908 cFYI(1, ("Oplock release rc = %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910 } else
911 spin_unlock(&GlobalMid_Lock);
Steve French68058e72005-10-10 10:34:22 -0700912 set_current_state(TASK_INTERRUPTIBLE);
913 schedule_timeout(1); /* yield in case q were corrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
Steve French45af7a02006-04-21 22:52:25 +0000915 } while (!kthread_should_stop());
916
917 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Steve French50c2f752007-07-13 00:33:32 +0000920static int cifs_dnotify_thread(void *dummyarg)
Steve French8d0d5092005-08-18 09:41:43 -0700921{
Steve French6ab16d22005-11-29 20:55:11 -0800922 struct list_head *tmp;
923 struct cifsSesInfo *ses;
924
Steve French8d0d5092005-08-18 09:41:43 -0700925 do {
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000926 if (try_to_freeze())
Steve French16abbec2005-08-30 13:10:14 -0700927 continue;
Steve French8d0d5092005-08-18 09:41:43 -0700928 set_current_state(TASK_INTERRUPTIBLE);
Steve French6ab16d22005-11-29 20:55:11 -0800929 schedule_timeout(15*HZ);
930 read_lock(&GlobalSMBSeslock);
931 /* check if any stuck requests that need
932 to be woken up and wakeq so the
933 thread can wake up and error out */
934 list_for_each(tmp, &GlobalSMBSessionList) {
Steve French6dc0f872007-07-06 23:13:06 +0000935 ses = list_entry(tmp, struct cifsSesInfo,
Steve French6ab16d22005-11-29 20:55:11 -0800936 cifsSessionList);
Steve French6dc0f872007-07-06 23:13:06 +0000937 if (ses && ses->server &&
Steve French2a138ebb2005-11-29 21:22:19 -0800938 atomic_read(&ses->server->inFlight))
Steve French6ab16d22005-11-29 20:55:11 -0800939 wake_up_all(&ses->server->response_q);
940 }
941 read_unlock(&GlobalSMBSeslock);
Steve French45af7a02006-04-21 22:52:25 +0000942 } while (!kthread_should_stop());
943
944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947static int __init
948init_cifs(void)
949{
950 int rc = 0;
951#ifdef CONFIG_PROC_FS
952 cifs_proc_init();
953#endif
Steve French2cd646a2006-09-28 19:43:08 +0000954/* INIT_LIST_HEAD(&GlobalServerList);*/ /* BB not implemented yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 INIT_LIST_HEAD(&GlobalSMBSessionList);
956 INIT_LIST_HEAD(&GlobalTreeConnectionList);
957 INIT_LIST_HEAD(&GlobalOplock_Q);
Steve French4ca9c192005-10-10 19:52:13 -0700958#ifdef CONFIG_CIFS_EXPERIMENTAL
959 INIT_LIST_HEAD(&GlobalDnotifyReqList);
960 INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
Steve French6dc0f872007-07-06 23:13:06 +0000961#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962/*
963 * Initialize Global counters
964 */
965 atomic_set(&sesInfoAllocCount, 0);
966 atomic_set(&tconInfoAllocCount, 0);
Steve French6dc0f872007-07-06 23:13:06 +0000967 atomic_set(&tcpSesAllocCount, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 atomic_set(&tcpSesReconnectCount, 0);
969 atomic_set(&tconInfoReconnectCount, 0);
970
971 atomic_set(&bufAllocCount, 0);
Steve French4498eed52005-12-03 13:58:57 -0800972 atomic_set(&smBufAllocCount, 0);
973#ifdef CONFIG_CIFS_STATS2
974 atomic_set(&totBufAllocCount, 0);
975 atomic_set(&totSmBufAllocCount, 0);
976#endif /* CONFIG_CIFS_STATS2 */
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 atomic_set(&midCount, 0);
979 GlobalCurrentXid = 0;
980 GlobalTotalActiveXid = 0;
981 GlobalMaxActiveXid = 0;
Steve French2cd646a2006-09-28 19:43:08 +0000982 memset(Local_System_Name, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 rwlock_init(&GlobalSMBSeslock);
984 spin_lock_init(&GlobalMid_Lock);
985
Steve French4523cc32007-04-30 20:13:06 +0000986 if (cifs_max_pending < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 cifs_max_pending = 2;
Steve French6dc0f872007-07-06 23:13:06 +0000988 cFYI(1, ("cifs_max_pending set to min of 2"));
Steve French4523cc32007-04-30 20:13:06 +0000989 } else if (cifs_max_pending > 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 cifs_max_pending = 256;
Steve French6dc0f872007-07-06 23:13:06 +0000991 cFYI(1, ("cifs_max_pending set to max of 256"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993
994 rc = cifs_init_inodecache();
Steve French45af7a02006-04-21 22:52:25 +0000995 if (rc)
996 goto out_clean_proc;
997
998 rc = cifs_init_mids();
999 if (rc)
1000 goto out_destroy_inodecache;
1001
1002 rc = cifs_init_request_bufs();
1003 if (rc)
1004 goto out_destroy_mids;
1005
1006 rc = register_filesystem(&cifs_fs_type);
1007 if (rc)
1008 goto out_destroy_request_bufs;
Jeff Layton84a15b92007-11-03 05:02:24 +00001009#ifdef CONFIG_CIFS_UPCALL
1010 rc = register_key_type(&cifs_spnego_key_type);
1011 if (rc)
1012 goto out_unregister_filesystem;
1013#endif
Steve French45af7a02006-04-21 22:52:25 +00001014 oplockThread = kthread_run(cifs_oplock_thread, NULL, "cifsoplockd");
1015 if (IS_ERR(oplockThread)) {
1016 rc = PTR_ERR(oplockThread);
Steve French6dc0f872007-07-06 23:13:06 +00001017 cERROR(1, ("error %d create oplock thread", rc));
Jeff Layton84a15b92007-11-03 05:02:24 +00001018 goto out_unregister_key_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
Steve French45af7a02006-04-21 22:52:25 +00001020
1021 dnotifyThread = kthread_run(cifs_dnotify_thread, NULL, "cifsdnotifyd");
1022 if (IS_ERR(dnotifyThread)) {
1023 rc = PTR_ERR(dnotifyThread);
Steve French6dc0f872007-07-06 23:13:06 +00001024 cERROR(1, ("error %d create dnotify thread", rc));
Steve French45af7a02006-04-21 22:52:25 +00001025 goto out_stop_oplock_thread;
1026 }
1027
1028 return 0;
1029
1030 out_stop_oplock_thread:
1031 kthread_stop(oplockThread);
Jeff Layton84a15b92007-11-03 05:02:24 +00001032 out_unregister_key_type:
1033#ifdef CONFIG_CIFS_UPCALL
1034 unregister_key_type(&cifs_spnego_key_type);
Steve French45af7a02006-04-21 22:52:25 +00001035 out_unregister_filesystem:
Jeff Layton84a15b92007-11-03 05:02:24 +00001036#endif
Steve French45af7a02006-04-21 22:52:25 +00001037 unregister_filesystem(&cifs_fs_type);
1038 out_destroy_request_bufs:
1039 cifs_destroy_request_bufs();
1040 out_destroy_mids:
1041 cifs_destroy_mids();
1042 out_destroy_inodecache:
1043 cifs_destroy_inodecache();
1044 out_clean_proc:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045#ifdef CONFIG_PROC_FS
1046 cifs_proc_clean();
1047#endif
1048 return rc;
1049}
1050
1051static void __exit
1052exit_cifs(void)
1053{
Steve French63135e02007-07-17 17:34:02 +00001054 cFYI(0, ("exit_cifs"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055#ifdef CONFIG_PROC_FS
1056 cifs_proc_clean();
1057#endif
Jeff Layton84a15b92007-11-03 05:02:24 +00001058#ifdef CONFIG_CIFS_UPCALL
1059 unregister_key_type(&cifs_spnego_key_type);
1060#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 unregister_filesystem(&cifs_fs_type);
1062 cifs_destroy_inodecache();
1063 cifs_destroy_mids();
1064 cifs_destroy_request_bufs();
Steve French45af7a02006-04-21 22:52:25 +00001065 kthread_stop(oplockThread);
1066 kthread_stop(dnotifyThread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
Steve French6dc0f872007-07-06 23:13:06 +00001070MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071MODULE_DESCRIPTION
Steve French63135e02007-07-17 17:34:02 +00001072 ("VFS to access servers complying with the SNIA CIFS Specification "
1073 "e.g. Samba and Windows");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074MODULE_VERSION(CIFS_VERSION);
1075module_init(init_cifs)
1076module_exit(exit_cifs)