blob: 093beaa3900de3321a2062143daeb04f7e6b17b2 [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>
Jeff Laytone5459372007-11-03 05:11:06 +000047#include "cifs_spnego.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDUs */
49
50#ifdef CONFIG_CIFS_QUOTA
51static struct quotactl_ops cifs_quotactl_ops;
Steve French35c11fd2007-02-27 05:09:35 +000052#endif /* QUOTA */
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054int cifsFYI = 0;
55int cifsERROR = 1;
56int traceSMB = 0;
57unsigned int oplockEnabled = 1;
58unsigned int experimEnabled = 0;
59unsigned int linuxExtEnabled = 1;
60unsigned int lookupCacheEnabled = 1;
61unsigned int multiuser_mount = 0;
Steve French39798772006-05-31 22:40:51 +000062unsigned int extended_security = CIFSSEC_DEF;
63/* unsigned int ntlmv2_support = 0; */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064unsigned int sign_CIFS_PDUs = 1;
Steve French6dc0f872007-07-06 23:13:06 +000065extern struct task_struct *oplockThread; /* remove sparse warning */
66struct task_struct *oplockThread = NULL;
Steve French99ee4db2007-02-27 05:35:17 +000067/* extern struct task_struct * dnotifyThread; remove sparse warning */
Steve French6dc0f872007-07-06 23:13:06 +000068static struct task_struct *dnotifyThread = NULL;
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080069static const struct super_operations cifs_super_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
71module_param(CIFSMaxBufSize, int, 0);
Steve French63135e02007-07-17 17:34:02 +000072MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header). "
73 "Default: 16384 Range: 8192 to 130048");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
75module_param(cifs_min_rcv, int, 0);
Steve French63135e02007-07-17 17:34:02 +000076MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
77 "1 to 64");
Linus Torvalds1da177e2005-04-16 15:20:36 -070078unsigned int cifs_min_small = 30;
79module_param(cifs_min_small, int, 0);
Steve French63135e02007-07-17 17:34:02 +000080MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
81 "Range: 2 to 256");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082unsigned int cifs_max_pending = CIFS_MAX_REQ;
83module_param(cifs_max_pending, int, 0);
Steve French63135e02007-07-17 17:34:02 +000084MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server. "
85 "Default: 50 Range: 2 to 256");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087extern mempool_t *cifs_sm_req_poolp;
88extern mempool_t *cifs_req_poolp;
89extern mempool_t *cifs_mid_poolp;
90
Christoph Lametere18b8902006-12-06 20:33:20 -080091extern struct kmem_cache *cifs_oplock_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93static int
94cifs_read_super(struct super_block *sb, void *data,
95 const char *devname, int silent)
96{
97 struct inode *inode;
98 struct cifs_sb_info *cifs_sb;
99 int rc = 0;
Steve French50c2f752007-07-13 00:33:32 +0000100
Steve French1b2b2122007-02-17 04:30:54 +0000101 /* BB should we make this contingent on mount parm? */
102 sb->s_flags |= MS_NODIRATIME | MS_NOATIME;
Steve French790fe572007-07-07 19:25:05 +0000103 sb->s_fs_info = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 cifs_sb = CIFS_SB(sb);
Steve French4523cc32007-04-30 20:13:06 +0000105 if (cifs_sb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 rc = cifs_mount(sb, cifs_sb, data, devname);
109
110 if (rc) {
111 if (!silent)
112 cERROR(1,
113 ("cifs_mount failed w/return code = %d", rc));
114 goto out_mount_failed;
115 }
116
117 sb->s_magic = CIFS_MAGIC_NUMBER;
118 sb->s_op = &cifs_super_ops;
Steve French4523cc32007-04-30 20:13:06 +0000119/* if (cifs_sb->tcon->ses->server->maxBuf > MAX_CIFS_HDR_SIZE + 512)
Steve French790fe572007-07-07 19:25:05 +0000120 sb->s_blocksize =
121 cifs_sb->tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#ifdef CONFIG_CIFS_QUOTA
123 sb->s_qcop = &cifs_quotactl_ops;
124#endif
125 sb->s_blocksize = CIFS_MAX_MSGSIZE;
126 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
127 inode = iget(sb, ROOT_I);
128
129 if (!inode) {
130 rc = -ENOMEM;
131 goto out_no_root;
132 }
133
134 sb->s_root = d_alloc_root(inode);
135
136 if (!sb->s_root) {
137 rc = -ENOMEM;
138 goto out_no_root;
139 }
Steve French50c2f752007-07-13 00:33:32 +0000140
Steve French7521a3c2007-07-11 18:30:34 +0000141#ifdef CONFIG_CIFS_EXPERIMENTAL
142 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
143 cFYI(1, ("export ops supported"));
144 sb->s_export_op = &cifs_export_ops;
145 }
146#endif /* EXPERIMENTAL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 return 0;
149
150out_no_root:
151 cERROR(1, ("cifs_read_super: get root inode failed"));
152 if (inode)
153 iput(inode);
154
155out_mount_failed:
Steve French4523cc32007-04-30 20:13:06 +0000156 if (cifs_sb) {
157 if (cifs_sb->local_nls)
Steve French50c2f752007-07-13 00:33:32 +0000158 unload_nls(cifs_sb->local_nls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 kfree(cifs_sb);
160 }
161 return rc;
162}
163
164static void
165cifs_put_super(struct super_block *sb)
166{
167 int rc = 0;
168 struct cifs_sb_info *cifs_sb;
169
170 cFYI(1, ("In cifs_put_super"));
171 cifs_sb = CIFS_SB(sb);
Steve French4523cc32007-04-30 20:13:06 +0000172 if (cifs_sb == NULL) {
Steve French790fe572007-07-07 19:25:05 +0000173 cFYI(1, ("Empty cifs superblock info passed to unmount"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return;
175 }
Steve French790fe572007-07-07 19:25:05 +0000176 rc = cifs_umount(sb, cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (rc) {
178 cERROR(1, ("cifs_umount failed with return code %d", rc));
179 }
180 unload_nls(cifs_sb->local_nls);
181 kfree(cifs_sb);
182 return;
183}
184
185static int
David Howells726c3342006-06-23 02:02:58 -0700186cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
David Howells726c3342006-06-23 02:02:58 -0700188 struct super_block *sb = dentry->d_sb;
Steve French790fe572007-07-07 19:25:05 +0000189 int xid;
Steve Frenchc81156d2005-04-28 22:41:07 -0700190 int rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct cifs_sb_info *cifs_sb;
192 struct cifsTconInfo *pTcon;
193
194 xid = GetXid();
195
196 cifs_sb = CIFS_SB(sb);
197 pTcon = cifs_sb->tcon;
198
199 buf->f_type = CIFS_MAGIC_NUMBER;
200
201 /* instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO */
Steve French790fe572007-07-07 19:25:05 +0000202 buf->f_namelen = PATH_MAX; /* PATH_MAX may be too long - it would
Steve Frenchc81156d2005-04-28 22:41:07 -0700203 presumably be total path, but note
204 that some servers (includinng Samba 3)
205 have a shorter maximum path */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 buf->f_files = 0; /* undefined */
207 buf->f_ffree = 0; /* unlimited */
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/* BB we could add a second check for a QFS Unix capability bit */
Steve Frenchf28ac912005-04-28 22:41:07 -0700210/* BB FIXME check CIFS_POSIX_EXTENSIONS Unix cap first FIXME BB */
Steve Frenchc81156d2005-04-28 22:41:07 -0700211 if ((pTcon->ses->capabilities & CAP_UNIX) && (CIFS_POSIX_EXTENSIONS &
212 le64_to_cpu(pTcon->fsUnixInfo.Capability)))
Steve French737b7582005-04-28 22:41:06 -0700213 rc = CIFSSMBQFSPosixInfo(xid, pTcon, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* Only need to call the old QFSInfo if failed
216 on newer one */
Steve French4523cc32007-04-30 20:13:06 +0000217 if (rc)
218 if (pTcon->ses->capabilities & CAP_NT_SMBS)
Steve French9ac00b72006-09-30 04:13:17 +0000219 rc = CIFSSMBQFSInfo(xid, pTcon, buf); /* not supported by OS2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Steve French9ac00b72006-09-30 04:13:17 +0000221 /* Some old Windows servers also do not support level 103, retry with
222 older level one if old server failed the previous call or we
223 bypassed it because we detected that this was an older LANMAN sess */
Steve French4523cc32007-04-30 20:13:06 +0000224 if (rc)
Steve French20962432005-09-21 22:05:57 -0700225 rc = SMBOldQFSInfo(xid, pTcon, buf);
Steve French790fe572007-07-07 19:25:05 +0000226 /* int f_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 __fsid_t f_fsid;
228 int f_namelen; */
Steve Frenchc81156d2005-04-28 22:41:07 -0700229 /* BB get from info in tcon struct at mount time call to QFSAttrInfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 FreeXid(xid);
Steve Frenchc81156d2005-04-28 22:41:07 -0700231 return 0; /* always return success? what if volume is no
232 longer available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Steve French50c2f752007-07-13 00:33:32 +0000235static int cifs_permission(struct inode *inode, int mask, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct cifs_sb_info *cifs_sb;
238
239 cifs_sb = CIFS_SB(inode->i_sb);
240
Steve French26f57362007-08-30 22:09:15 +0000241 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
Steve French26f57362007-08-30 22:09:15 +0000243 else /* file mode might have been restricted at mount time
Steve French50c2f752007-07-13 00:33:32 +0000244 on the client (above and beyond ACL on servers) for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 servers which do not support setting and viewing mode bits,
Steve French50c2f752007-07-13 00:33:32 +0000246 so allowing client to check permissions is useful */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return generic_permission(inode, mask, NULL);
248}
249
Christoph Lametere18b8902006-12-06 20:33:20 -0800250static struct kmem_cache *cifs_inode_cachep;
251static struct kmem_cache *cifs_req_cachep;
252static struct kmem_cache *cifs_mid_cachep;
253struct kmem_cache *cifs_oplock_cachep;
254static struct kmem_cache *cifs_sm_req_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255mempool_t *cifs_sm_req_poolp;
256mempool_t *cifs_req_poolp;
257mempool_t *cifs_mid_poolp;
258
259static struct inode *
260cifs_alloc_inode(struct super_block *sb)
261{
262 struct cifsInodeInfo *cifs_inode;
Christoph Lametere94b1762006-12-06 20:33:17 -0800263 cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (!cifs_inode)
265 return NULL;
266 cifs_inode->cifsAttrs = 0x20; /* default */
267 atomic_set(&cifs_inode->inUse, 0);
268 cifs_inode->time = 0;
Jeff Laytoncea21802007-11-20 23:19:03 +0000269 cifs_inode->write_behind_rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* Until the file is open and we have gotten oplock
271 info back from the server, can not assume caching of
272 file data or metadata */
273 cifs_inode->clientCanCacheRead = FALSE;
274 cifs_inode->clientCanCacheAll = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
Steve French50c2f752007-07-13 00:33:32 +0000276
Steve French1b2b2122007-02-17 04:30:54 +0000277 /* Can not set i_flags here - they get immediately overwritten
278 to zero by the VFS */
279/* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 INIT_LIST_HEAD(&cifs_inode->openFileList);
281 return &cifs_inode->vfs_inode;
282}
283
284static void
285cifs_destroy_inode(struct inode *inode)
286{
287 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
288}
289
290/*
291 * cifs_show_options() is for displaying mount options in /proc/mounts.
292 * Not all settable options are displayed but most of the important
293 * ones are.
294 */
295static int
296cifs_show_options(struct seq_file *s, struct vfsmount *m)
297{
298 struct cifs_sb_info *cifs_sb;
299
300 cifs_sb = CIFS_SB(m->mnt_sb);
301
302 if (cifs_sb) {
303 if (cifs_sb->tcon) {
Steve French004c46b2007-02-17 04:34:13 +0000304/* BB add prepath to mount options displayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 seq_printf(s, ",unc=%s", cifs_sb->tcon->treeName);
306 if (cifs_sb->tcon->ses) {
307 if (cifs_sb->tcon->ses->userName)
308 seq_printf(s, ",username=%s",
309 cifs_sb->tcon->ses->userName);
Steve French4523cc32007-04-30 20:13:06 +0000310 if (cifs_sb->tcon->ses->domainName)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 seq_printf(s, ",domain=%s",
312 cifs_sb->tcon->ses->domainName);
313 }
Steve Frenchd5d18502007-10-17 21:31:52 +0000314 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) ||
315 !(cifs_sb->tcon->unix_ext))
316 seq_printf(s, ",uid=%d", cifs_sb->mnt_uid);
317 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) ||
318 !(cifs_sb->tcon->unix_ext))
319 seq_printf(s, ",gid=%d", cifs_sb->mnt_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Steve French4523cc32007-04-30 20:13:06 +0000321 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
322 seq_printf(s, ",posixpaths");
Steve French50c2f752007-07-13 00:33:32 +0000323 seq_printf(s, ",rsize=%d", cifs_sb->rsize);
324 seq_printf(s, ",wsize=%d", cifs_sb->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326 return 0;
327}
328
329#ifdef CONFIG_CIFS_QUOTA
Steve French50c2f752007-07-13 00:33:32 +0000330int cifs_xquota_set(struct super_block *sb, int quota_type, qid_t qid,
331 struct fs_disk_quota *pdquota)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 int xid;
334 int rc = 0;
335 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
336 struct cifsTconInfo *pTcon;
Steve French50c2f752007-07-13 00:33:32 +0000337
Steve French4523cc32007-04-30 20:13:06 +0000338 if (cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 pTcon = cifs_sb->tcon;
340 else
341 return -EIO;
342
343
344 xid = GetXid();
Steve French4523cc32007-04-30 20:13:06 +0000345 if (pTcon) {
Steve French50c2f752007-07-13 00:33:32 +0000346 cFYI(1, ("set type: 0x%x id: %d", quota_type, qid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 } else {
Cyrill Gorcunov95ba7362007-08-24 00:23:36 +0000348 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 FreeXid(xid);
352 return rc;
353}
354
Steve French50c2f752007-07-13 00:33:32 +0000355int cifs_xquota_get(struct super_block *sb, int quota_type, qid_t qid,
356 struct fs_disk_quota *pdquota)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 int xid;
359 int rc = 0;
360 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
361 struct cifsTconInfo *pTcon;
362
Steve French4523cc32007-04-30 20:13:06 +0000363 if (cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 pTcon = cifs_sb->tcon;
365 else
366 return -EIO;
367
368 xid = GetXid();
Steve French4523cc32007-04-30 20:13:06 +0000369 if (pTcon) {
Steve French50c2f752007-07-13 00:33:32 +0000370 cFYI(1, ("set type: 0x%x id: %d", quota_type, qid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 } else {
372 rc = -EIO;
373 }
374
375 FreeXid(xid);
376 return rc;
377}
378
Steve French50c2f752007-07-13 00:33:32 +0000379int cifs_xstate_set(struct super_block *sb, unsigned int flags, int operation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Steve French50c2f752007-07-13 00:33:32 +0000381 int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 int rc = 0;
383 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
384 struct cifsTconInfo *pTcon;
385
Steve French4523cc32007-04-30 20:13:06 +0000386 if (cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 pTcon = cifs_sb->tcon;
388 else
389 return -EIO;
390
391 xid = GetXid();
Steve French4523cc32007-04-30 20:13:06 +0000392 if (pTcon) {
Steve French50c2f752007-07-13 00:33:32 +0000393 cFYI(1, ("flags: 0x%x operation: 0x%x", flags, operation));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 } else {
395 rc = -EIO;
396 }
397
398 FreeXid(xid);
399 return rc;
400}
401
Steve French50c2f752007-07-13 00:33:32 +0000402int cifs_xstate_get(struct super_block *sb, struct fs_quota_stat *qstats)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 int xid;
405 int rc = 0;
406 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
407 struct cifsTconInfo *pTcon;
408
Steve French4523cc32007-04-30 20:13:06 +0000409 if (cifs_sb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 pTcon = cifs_sb->tcon;
411 } else {
412 return -EIO;
413 }
414 xid = GetXid();
Steve French4523cc32007-04-30 20:13:06 +0000415 if (pTcon) {
Steve French50c2f752007-07-13 00:33:32 +0000416 cFYI(1, ("pqstats %p", qstats));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 } else {
418 rc = -EIO;
419 }
420
421 FreeXid(xid);
422 return rc;
423}
424
425static struct quotactl_ops cifs_quotactl_ops = {
426 .set_xquota = cifs_xquota_set,
427 .get_xquota = cifs_xquota_set,
428 .set_xstate = cifs_xstate_set,
429 .get_xstate = cifs_xstate_get,
430};
431#endif
432
Steve French50c2f752007-07-13 00:33:32 +0000433static void cifs_umount_begin(struct vfsmount *vfsmnt, int flags)
Steve French68058e72005-10-10 10:34:22 -0700434{
Steve French5e1253b2005-10-10 14:06:37 -0700435 struct cifs_sb_info *cifs_sb;
Steve French50c2f752007-07-13 00:33:32 +0000436 struct cifsTconInfo *tcon;
Steve French68058e72005-10-10 10:34:22 -0700437
Trond Myklebust8b512d92006-06-09 09:34:18 -0400438 if (!(flags & MNT_FORCE))
439 return;
440 cifs_sb = CIFS_SB(vfsmnt->mnt_sb);
Steve French4523cc32007-04-30 20:13:06 +0000441 if (cifs_sb == NULL)
Steve French9e2e85f2005-10-10 14:28:38 -0700442 return;
443
444 tcon = cifs_sb->tcon;
Steve French4523cc32007-04-30 20:13:06 +0000445 if (tcon == NULL)
Steve French9e2e85f2005-10-10 14:28:38 -0700446 return;
Steve French5e1253b2005-10-10 14:06:37 -0700447 down(&tcon->tconSem);
448 if (atomic_read(&tcon->useCount) == 1)
449 tcon->tidStatus = CifsExiting;
450 up(&tcon->tconSem);
451
Steve French3a5ff612006-07-14 22:37:11 +0000452 /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
Steve French7b7abfe2005-11-09 15:21:09 -0800453 /* cancel_notify_requests(tcon); */
Steve French50c2f752007-07-13 00:33:32 +0000454 if (tcon->ses && tcon->ses->server) {
455 cFYI(1, ("wake up tasks now - umount begin not complete"));
Steve French9e2e85f2005-10-10 14:28:38 -0700456 wake_up_all(&tcon->ses->server->request_q);
Steve French6ab16d22005-11-29 20:55:11 -0800457 wake_up_all(&tcon->ses->server->response_q);
458 msleep(1); /* yield */
459 /* we have to kick the requests once more */
460 wake_up_all(&tcon->ses->server->response_q);
461 msleep(1);
Steve French5e1253b2005-10-10 14:06:37 -0700462 }
463/* BB FIXME - finish add checks for tidStatus BB */
Steve French68058e72005-10-10 10:34:22 -0700464
465 return;
466}
Steve French68058e72005-10-10 10:34:22 -0700467
Steve Frenchbf97d282006-09-28 21:34:06 +0000468#ifdef CONFIG_CIFS_STATS2
469static int cifs_show_stats(struct seq_file *s, struct vfsmount *mnt)
470{
471 /* BB FIXME */
472 return 0;
473}
474#endif
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476static int cifs_remount(struct super_block *sb, int *flags, char *data)
477{
478 *flags |= MS_NODIRATIME;
479 return 0;
480}
481
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800482static const struct super_operations cifs_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 .read_inode = cifs_read_inode,
484 .put_super = cifs_put_super,
485 .statfs = cifs_statfs,
486 .alloc_inode = cifs_alloc_inode,
487 .destroy_inode = cifs_destroy_inode,
Steve French50c2f752007-07-13 00:33:32 +0000488/* .drop_inode = generic_delete_inode,
489 .delete_inode = cifs_delete_inode, */ /* Do not need above two
490 functions unless later we add lazy close of inodes or unless the
491 kernel forgets to call us with the same number of releases (closes)
492 as opens */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 .show_options = cifs_show_options,
Steve French7b7abfe2005-11-09 15:21:09 -0800494 .umount_begin = cifs_umount_begin,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 .remount_fs = cifs_remount,
Steve Frenchbf97d282006-09-28 21:34:06 +0000496#ifdef CONFIG_CIFS_STATS2
Steve Frenchf46d3e12006-09-30 01:08:55 +0000497 .show_stats = cifs_show_stats,
Steve Frenchbf97d282006-09-28 21:34:06 +0000498#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499};
500
David Howells454e2392006-06-23 02:02:57 -0700501static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502cifs_get_sb(struct file_system_type *fs_type,
David Howells454e2392006-06-23 02:02:57 -0700503 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 int rc;
506 struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
507
508 cFYI(1, ("Devname: %s flags: %d ", dev_name, flags));
509
510 if (IS_ERR(sb))
David Howells454e2392006-06-23 02:02:57 -0700511 return PTR_ERR(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 sb->s_flags = flags;
514
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800515 rc = cifs_read_super(sb, data, dev_name, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (rc) {
517 up_write(&sb->s_umount);
518 deactivate_super(sb);
David Howells454e2392006-06-23 02:02:57 -0700519 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 sb->s_flags |= MS_ACTIVE;
David Howells454e2392006-06-23 02:02:57 -0700522 return simple_set_mnt(mnt, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Badari Pulavarty027445c2006-09-30 23:28:46 -0700525static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
526 unsigned long nr_segs, loff_t pos)
Steve French87c89dd2005-11-17 17:03:00 -0800527{
Josef "Jeff" Sipeke6a00292006-12-08 02:36:48 -0800528 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
Steve French87c89dd2005-11-17 17:03:00 -0800529 ssize_t written;
530
Badari Pulavarty027445c2006-09-30 23:28:46 -0700531 written = generic_file_aio_write(iocb, iov, nr_segs, pos);
Steve French87c89dd2005-11-17 17:03:00 -0800532 if (!CIFS_I(inode)->clientCanCacheAll)
533 filemap_fdatawrite(inode->i_mapping);
534 return written;
535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Steve Frenchc32a0b62006-01-12 14:41:28 -0800537static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
538{
539 /* origin == SEEK_END => we must revalidate the cached file length */
Steve French0889a942006-09-23 22:11:07 +0000540 if (origin == SEEK_END) {
Steve French030e9d82007-02-01 04:27:59 +0000541 int retval;
542
543 /* some applications poll for the file length in this strange
544 way so we must seek to end on non-oplocked files by
545 setting the revalidate time to zero */
Christoph Hellwigc33f8d32007-04-02 18:47:20 +0000546 CIFS_I(file->f_path.dentry->d_inode)->time = 0;
Steve French030e9d82007-02-01 04:27:59 +0000547
548 retval = cifs_revalidate(file->f_path.dentry);
Steve Frenchc32a0b62006-01-12 14:41:28 -0800549 if (retval < 0)
550 return (loff_t)retval;
551 }
552 return remote_llseek(file, offset, origin);
553}
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static struct file_system_type cifs_fs_type = {
556 .owner = THIS_MODULE,
557 .name = "cifs",
558 .get_sb = cifs_get_sb,
559 .kill_sb = kill_anon_super,
560 /* .fs_flags */
561};
Arjan van de Ven754661f2007-02-12 00:55:38 -0800562const struct inode_operations cifs_dir_inode_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 .create = cifs_create,
564 .lookup = cifs_lookup,
565 .getattr = cifs_getattr,
566 .unlink = cifs_unlink,
567 .link = cifs_hardlink,
568 .mkdir = cifs_mkdir,
569 .rmdir = cifs_rmdir,
570 .rename = cifs_rename,
571 .permission = cifs_permission,
572/* revalidate:cifs_revalidate, */
573 .setattr = cifs_setattr,
574 .symlink = cifs_symlink,
575 .mknod = cifs_mknod,
576#ifdef CONFIG_CIFS_XATTR
577 .setxattr = cifs_setxattr,
578 .getxattr = cifs_getxattr,
579 .listxattr = cifs_listxattr,
580 .removexattr = cifs_removexattr,
581#endif
582};
583
Arjan van de Ven754661f2007-02-12 00:55:38 -0800584const struct inode_operations cifs_file_inode_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/* revalidate:cifs_revalidate, */
586 .setattr = cifs_setattr,
587 .getattr = cifs_getattr, /* do we need this anymore? */
588 .rename = cifs_rename,
589 .permission = cifs_permission,
590#ifdef CONFIG_CIFS_XATTR
591 .setxattr = cifs_setxattr,
592 .getxattr = cifs_getxattr,
593 .listxattr = cifs_listxattr,
594 .removexattr = cifs_removexattr,
Steve French50c2f752007-07-13 00:33:32 +0000595#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596};
597
Arjan van de Ven754661f2007-02-12 00:55:38 -0800598const struct inode_operations cifs_symlink_inode_ops = {
Steve French50c2f752007-07-13 00:33:32 +0000599 .readlink = generic_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 .follow_link = cifs_follow_link,
601 .put_link = cifs_put_link,
602 .permission = cifs_permission,
603 /* BB add the following two eventually */
604 /* revalidate: cifs_revalidate,
605 setattr: cifs_notify_change, *//* BB do we need notify change */
606#ifdef CONFIG_CIFS_XATTR
607 .setxattr = cifs_setxattr,
608 .getxattr = cifs_getxattr,
609 .listxattr = cifs_listxattr,
610 .removexattr = cifs_removexattr,
Steve French50c2f752007-07-13 00:33:32 +0000611#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612};
613
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800614const struct file_operations cifs_file_ops = {
Steve French87c89dd2005-11-17 17:03:00 -0800615 .read = do_sync_read,
616 .write = do_sync_write,
Steve French87c89dd2005-11-17 17:03:00 -0800617 .aio_read = generic_file_aio_read,
618 .aio_write = cifs_file_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 .open = cifs_open,
620 .release = cifs_close,
621 .lock = cifs_lock,
622 .fsync = cifs_fsync,
623 .flush = cifs_flush,
624 .mmap = cifs_file_mmap,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200625 .splice_read = generic_file_splice_read,
Steve Frenchc32a0b62006-01-12 14:41:28 -0800626 .llseek = cifs_llseek,
Steve Frenchc67593a2005-04-28 22:41:04 -0700627#ifdef CONFIG_CIFS_POSIX
628 .ioctl = cifs_ioctl,
629#endif /* CONFIG_CIFS_POSIX */
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631#ifdef CONFIG_CIFS_EXPERIMENTAL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 .dir_notify = cifs_dir_notify,
633#endif /* CONFIG_CIFS_EXPERIMENTAL */
634};
635
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800636const struct file_operations cifs_file_direct_ops = {
Steve French50c2f752007-07-13 00:33:32 +0000637 /* no mmap, no aio, no readv -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 BB reevaluate whether they can be done with directio, no cache */
639 .read = cifs_user_read,
640 .write = cifs_user_write,
641 .open = cifs_open,
642 .release = cifs_close,
643 .lock = cifs_lock,
644 .fsync = cifs_fsync,
645 .flush = cifs_flush,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200646 .splice_read = generic_file_splice_read,
Steve Frenchc67593a2005-04-28 22:41:04 -0700647#ifdef CONFIG_CIFS_POSIX
648 .ioctl = cifs_ioctl,
649#endif /* CONFIG_CIFS_POSIX */
Steve Frenchc32a0b62006-01-12 14:41:28 -0800650 .llseek = cifs_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651#ifdef CONFIG_CIFS_EXPERIMENTAL
652 .dir_notify = cifs_dir_notify,
653#endif /* CONFIG_CIFS_EXPERIMENTAL */
654};
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800655const struct file_operations cifs_file_nobrl_ops = {
Steve French87c89dd2005-11-17 17:03:00 -0800656 .read = do_sync_read,
657 .write = do_sync_write,
Steve French87c89dd2005-11-17 17:03:00 -0800658 .aio_read = generic_file_aio_read,
659 .aio_write = cifs_file_aio_write,
660 .open = cifs_open,
661 .release = cifs_close,
662 .fsync = cifs_fsync,
663 .flush = cifs_flush,
664 .mmap = cifs_file_mmap,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200665 .splice_read = generic_file_splice_read,
Steve Frenchc32a0b62006-01-12 14:41:28 -0800666 .llseek = cifs_llseek,
Steve French8b94bcb2005-11-11 11:41:00 -0800667#ifdef CONFIG_CIFS_POSIX
Steve French87c89dd2005-11-17 17:03:00 -0800668 .ioctl = cifs_ioctl,
Steve French8b94bcb2005-11-11 11:41:00 -0800669#endif /* CONFIG_CIFS_POSIX */
670
671#ifdef CONFIG_CIFS_EXPERIMENTAL
Steve French87c89dd2005-11-17 17:03:00 -0800672 .dir_notify = cifs_dir_notify,
Steve French8b94bcb2005-11-11 11:41:00 -0800673#endif /* CONFIG_CIFS_EXPERIMENTAL */
674};
675
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800676const struct file_operations cifs_file_direct_nobrl_ops = {
Steve French50c2f752007-07-13 00:33:32 +0000677 /* no mmap, no aio, no readv -
Steve French87c89dd2005-11-17 17:03:00 -0800678 BB reevaluate whether they can be done with directio, no cache */
679 .read = cifs_user_read,
680 .write = cifs_user_write,
681 .open = cifs_open,
682 .release = cifs_close,
683 .fsync = cifs_fsync,
684 .flush = cifs_flush,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200685 .splice_read = generic_file_splice_read,
Steve French8b94bcb2005-11-11 11:41:00 -0800686#ifdef CONFIG_CIFS_POSIX
Steve French87c89dd2005-11-17 17:03:00 -0800687 .ioctl = cifs_ioctl,
Steve French8b94bcb2005-11-11 11:41:00 -0800688#endif /* CONFIG_CIFS_POSIX */
Steve Frenchc32a0b62006-01-12 14:41:28 -0800689 .llseek = cifs_llseek,
Steve French8b94bcb2005-11-11 11:41:00 -0800690#ifdef CONFIG_CIFS_EXPERIMENTAL
Steve French87c89dd2005-11-17 17:03:00 -0800691 .dir_notify = cifs_dir_notify,
Steve French8b94bcb2005-11-11 11:41:00 -0800692#endif /* CONFIG_CIFS_EXPERIMENTAL */
693};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800695const struct file_operations cifs_dir_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 .readdir = cifs_readdir,
697 .release = cifs_closedir,
698 .read = generic_read_dir,
699#ifdef CONFIG_CIFS_EXPERIMENTAL
700 .dir_notify = cifs_dir_notify,
701#endif /* CONFIG_CIFS_EXPERIMENTAL */
Steve French50c2f752007-07-13 00:33:32 +0000702 .ioctl = cifs_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703};
704
705static void
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700706cifs_init_once(struct kmem_cache *cachep, void *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 struct cifsInodeInfo *cifsi = inode;
709
Christoph Lametera35afb82007-05-16 22:10:57 -0700710 inode_init_once(&cifsi->vfs_inode);
711 INIT_LIST_HEAD(&cifsi->lockList);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714static int
715cifs_init_inodecache(void)
716{
717 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
Steve French26f57362007-08-30 22:09:15 +0000718 sizeof(struct cifsInodeInfo),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800719 0, (SLAB_RECLAIM_ACCOUNT|
720 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900721 cifs_init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (cifs_inode_cachep == NULL)
723 return -ENOMEM;
724
725 return 0;
726}
727
728static void
729cifs_destroy_inodecache(void)
730{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700731 kmem_cache_destroy(cifs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734static int
735cifs_init_request_bufs(void)
736{
Steve French4523cc32007-04-30 20:13:06 +0000737 if (CIFSMaxBufSize < 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
739 Unicode path name has to fit in any SMB/CIFS path based frames */
740 CIFSMaxBufSize = 8192;
741 } else if (CIFSMaxBufSize > 1024*127) {
742 CIFSMaxBufSize = 1024 * 127;
743 } else {
744 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
745 }
746/* cERROR(1,("CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize)); */
747 cifs_req_cachep = kmem_cache_create("cifs_request",
748 CIFSMaxBufSize +
749 MAX_CIFS_HDR_SIZE, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900750 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (cifs_req_cachep == NULL)
752 return -ENOMEM;
753
Steve French4523cc32007-04-30 20:13:06 +0000754 if (cifs_min_rcv < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 cifs_min_rcv = 1;
756 else if (cifs_min_rcv > 64) {
757 cifs_min_rcv = 64;
Steve French50c2f752007-07-13 00:33:32 +0000758 cERROR(1, ("cifs_min_rcv set to maximum (64)"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
Matthew Dobson93d23412006-03-26 01:37:50 -0800761 cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
762 cifs_req_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Steve French4523cc32007-04-30 20:13:06 +0000764 if (cifs_req_poolp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 kmem_cache_destroy(cifs_req_cachep);
766 return -ENOMEM;
767 }
Steve Frenchec637e32005-12-12 20:53:18 -0800768 /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 almost all handle based requests (but not write response, nor is it
770 sufficient for path based requests). A smaller size would have
Steve French50c2f752007-07-13 00:33:32 +0000771 been more efficient (compacting multiple slab items on one 4k page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 for the case in which debug was on, but this larger size allows
773 more SMBs to use small buffer alloc and is still much more
Steve French6dc0f872007-07-06 23:13:06 +0000774 efficient to alloc 1 per page off the slab compared to 17K (5page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 alloc of large cifs buffers even when page debugging is on */
776 cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
Steve French6dc0f872007-07-06 23:13:06 +0000777 MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900778 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (cifs_sm_req_cachep == NULL) {
780 mempool_destroy(cifs_req_poolp);
781 kmem_cache_destroy(cifs_req_cachep);
Steve French6dc0f872007-07-06 23:13:06 +0000782 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
Steve French4523cc32007-04-30 20:13:06 +0000785 if (cifs_min_small < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 cifs_min_small = 2;
787 else if (cifs_min_small > 256) {
788 cifs_min_small = 256;
Steve French6dc0f872007-07-06 23:13:06 +0000789 cFYI(1, ("cifs_min_small set to maximum (256)"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
Matthew Dobson93d23412006-03-26 01:37:50 -0800792 cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
793 cifs_sm_req_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Steve French4523cc32007-04-30 20:13:06 +0000795 if (cifs_sm_req_poolp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 mempool_destroy(cifs_req_poolp);
797 kmem_cache_destroy(cifs_req_cachep);
798 kmem_cache_destroy(cifs_sm_req_cachep);
799 return -ENOMEM;
800 }
801
802 return 0;
803}
804
805static void
806cifs_destroy_request_bufs(void)
807{
808 mempool_destroy(cifs_req_poolp);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700809 kmem_cache_destroy(cifs_req_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 mempool_destroy(cifs_sm_req_poolp);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700811 kmem_cache_destroy(cifs_sm_req_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
814static int
815cifs_init_mids(void)
816{
817 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
Steve French26f57362007-08-30 22:09:15 +0000818 sizeof(struct mid_q_entry), 0,
819 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (cifs_mid_cachep == NULL)
821 return -ENOMEM;
822
Matthew Dobson93d23412006-03-26 01:37:50 -0800823 /* 3 is a reasonable minimum number of simultaneous operations */
824 cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
Steve French4523cc32007-04-30 20:13:06 +0000825 if (cifs_mid_poolp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 kmem_cache_destroy(cifs_mid_cachep);
827 return -ENOMEM;
828 }
829
830 cifs_oplock_cachep = kmem_cache_create("cifs_oplock_structs",
Steve French26f57362007-08-30 22:09:15 +0000831 sizeof(struct oplock_q_entry), 0,
832 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (cifs_oplock_cachep == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 mempool_destroy(cifs_mid_poolp);
Akinobu Mitae6985c72007-06-04 16:14:59 +0000835 kmem_cache_destroy(cifs_mid_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return -ENOMEM;
837 }
838
839 return 0;
840}
841
842static void
843cifs_destroy_mids(void)
844{
845 mempool_destroy(cifs_mid_poolp);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700846 kmem_cache_destroy(cifs_mid_cachep);
847 kmem_cache_destroy(cifs_oplock_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Steve French50c2f752007-07-13 00:33:32 +0000850static int cifs_oplock_thread(void *dummyarg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Steve French6dc0f872007-07-06 23:13:06 +0000852 struct oplock_q_entry *oplock_item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct cifsTconInfo *pTcon;
Steve French6dc0f872007-07-06 23:13:06 +0000854 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 __u16 netfid;
Jeff Laytoncea21802007-11-20 23:19:03 +0000856 int rc, waitrc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700858 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 do {
Steve French6dc0f872007-07-06 23:13:06 +0000860 if (try_to_freeze())
Steve Frenchede13272005-08-30 20:10:14 -0700861 continue;
Steve French50c2f752007-07-13 00:33:32 +0000862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 spin_lock(&GlobalMid_Lock);
Steve French4523cc32007-04-30 20:13:06 +0000864 if (list_empty(&GlobalOplock_Q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 spin_unlock(&GlobalMid_Lock);
866 set_current_state(TASK_INTERRUPTIBLE);
867 schedule_timeout(39*HZ);
868 } else {
Steve French6dc0f872007-07-06 23:13:06 +0000869 oplock_item = list_entry(GlobalOplock_Q.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct oplock_q_entry, qhead);
Steve French4523cc32007-04-30 20:13:06 +0000871 if (oplock_item) {
Steve French50c2f752007-07-13 00:33:32 +0000872 cFYI(1, ("found oplock item to write out"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 pTcon = oplock_item->tcon;
874 inode = oplock_item->pinode;
875 netfid = oplock_item->netfid;
876 spin_unlock(&GlobalMid_Lock);
877 DeleteOplockQEntry(oplock_item);
878 /* can not grab inode sem here since it would
Steve French6dc0f872007-07-06 23:13:06 +0000879 deadlock when oplock received on delete
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800880 since vfs_unlink holds the i_mutex across
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 the call */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800882 /* mutex_lock(&inode->i_mutex);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (S_ISREG(inode->i_mode)) {
Steve French26f57362007-08-30 22:09:15 +0000884 rc =
885 filemap_fdatawrite(inode->i_mapping);
Steve French50c2f752007-07-13 00:33:32 +0000886 if (CIFS_I(inode)->clientCanCacheRead
887 == 0) {
Jeff Laytoncea21802007-11-20 23:19:03 +0000888 waitrc = filemap_fdatawait(inode->i_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 invalidate_remote_inode(inode);
890 }
Jeff Laytoncea21802007-11-20 23:19:03 +0000891 if (rc == 0)
892 rc = waitrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 } else
894 rc = 0;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800895 /* mutex_unlock(&inode->i_mutex);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (rc)
897 CIFS_I(inode)->write_behind_rc = rc;
Steve French6dc0f872007-07-06 23:13:06 +0000898 cFYI(1, ("Oplock flush inode %p rc %d",
899 inode, rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Steve French6dc0f872007-07-06 23:13:06 +0000901 /* releasing stale oplock after recent reconnect
902 of smb session using a now incorrect file
903 handle is not a data integrity issue but do
904 not bother sending an oplock release if session
905 to server still is disconnected since oplock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 already released by the server in that case */
Steve French4523cc32007-04-30 20:13:06 +0000907 if (pTcon->tidStatus != CifsNeedReconnect) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 rc = CIFSSMBLock(0, pTcon, netfid,
Steve French6dc0f872007-07-06 23:13:06 +0000909 0 /* len */ , 0 /* offset */, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 0, LOCKING_ANDX_OPLOCK_RELEASE,
911 0 /* wait flag */);
Steve French26f57362007-08-30 22:09:15 +0000912 cFYI(1, ("Oplock release rc = %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914 } else
915 spin_unlock(&GlobalMid_Lock);
Steve French68058e72005-10-10 10:34:22 -0700916 set_current_state(TASK_INTERRUPTIBLE);
917 schedule_timeout(1); /* yield in case q were corrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
Steve French45af7a02006-04-21 22:52:25 +0000919 } while (!kthread_should_stop());
920
921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Steve French50c2f752007-07-13 00:33:32 +0000924static int cifs_dnotify_thread(void *dummyarg)
Steve French8d0d5092005-08-18 09:41:43 -0700925{
Steve French6ab16d22005-11-29 20:55:11 -0800926 struct list_head *tmp;
927 struct cifsSesInfo *ses;
928
Steve French8d0d5092005-08-18 09:41:43 -0700929 do {
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000930 if (try_to_freeze())
Steve French16abbec2005-08-30 13:10:14 -0700931 continue;
Steve French8d0d5092005-08-18 09:41:43 -0700932 set_current_state(TASK_INTERRUPTIBLE);
Steve French6ab16d22005-11-29 20:55:11 -0800933 schedule_timeout(15*HZ);
934 read_lock(&GlobalSMBSeslock);
935 /* check if any stuck requests that need
936 to be woken up and wakeq so the
937 thread can wake up and error out */
938 list_for_each(tmp, &GlobalSMBSessionList) {
Steve French6dc0f872007-07-06 23:13:06 +0000939 ses = list_entry(tmp, struct cifsSesInfo,
Steve French6ab16d22005-11-29 20:55:11 -0800940 cifsSessionList);
Steve French6dc0f872007-07-06 23:13:06 +0000941 if (ses && ses->server &&
Steve French2a138ebb2005-11-29 21:22:19 -0800942 atomic_read(&ses->server->inFlight))
Steve French6ab16d22005-11-29 20:55:11 -0800943 wake_up_all(&ses->server->response_q);
944 }
945 read_unlock(&GlobalSMBSeslock);
Steve French45af7a02006-04-21 22:52:25 +0000946 } while (!kthread_should_stop());
947
948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951static int __init
952init_cifs(void)
953{
954 int rc = 0;
955#ifdef CONFIG_PROC_FS
956 cifs_proc_init();
957#endif
Steve French2cd646a2006-09-28 19:43:08 +0000958/* INIT_LIST_HEAD(&GlobalServerList);*/ /* BB not implemented yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 INIT_LIST_HEAD(&GlobalSMBSessionList);
960 INIT_LIST_HEAD(&GlobalTreeConnectionList);
961 INIT_LIST_HEAD(&GlobalOplock_Q);
Steve French4ca9c192005-10-10 19:52:13 -0700962#ifdef CONFIG_CIFS_EXPERIMENTAL
963 INIT_LIST_HEAD(&GlobalDnotifyReqList);
964 INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
Steve French6dc0f872007-07-06 23:13:06 +0000965#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966/*
967 * Initialize Global counters
968 */
969 atomic_set(&sesInfoAllocCount, 0);
970 atomic_set(&tconInfoAllocCount, 0);
Steve French6dc0f872007-07-06 23:13:06 +0000971 atomic_set(&tcpSesAllocCount, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 atomic_set(&tcpSesReconnectCount, 0);
973 atomic_set(&tconInfoReconnectCount, 0);
974
975 atomic_set(&bufAllocCount, 0);
Steve French4498eed52005-12-03 13:58:57 -0800976 atomic_set(&smBufAllocCount, 0);
977#ifdef CONFIG_CIFS_STATS2
978 atomic_set(&totBufAllocCount, 0);
979 atomic_set(&totSmBufAllocCount, 0);
980#endif /* CONFIG_CIFS_STATS2 */
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 atomic_set(&midCount, 0);
983 GlobalCurrentXid = 0;
984 GlobalTotalActiveXid = 0;
985 GlobalMaxActiveXid = 0;
Steve French2cd646a2006-09-28 19:43:08 +0000986 memset(Local_System_Name, 0, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 rwlock_init(&GlobalSMBSeslock);
988 spin_lock_init(&GlobalMid_Lock);
989
Steve French4523cc32007-04-30 20:13:06 +0000990 if (cifs_max_pending < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 cifs_max_pending = 2;
Steve French6dc0f872007-07-06 23:13:06 +0000992 cFYI(1, ("cifs_max_pending set to min of 2"));
Steve French4523cc32007-04-30 20:13:06 +0000993 } else if (cifs_max_pending > 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 cifs_max_pending = 256;
Steve French6dc0f872007-07-06 23:13:06 +0000995 cFYI(1, ("cifs_max_pending set to max of 256"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997
998 rc = cifs_init_inodecache();
Steve French45af7a02006-04-21 22:52:25 +0000999 if (rc)
1000 goto out_clean_proc;
1001
1002 rc = cifs_init_mids();
1003 if (rc)
1004 goto out_destroy_inodecache;
1005
1006 rc = cifs_init_request_bufs();
1007 if (rc)
1008 goto out_destroy_mids;
1009
1010 rc = register_filesystem(&cifs_fs_type);
1011 if (rc)
1012 goto out_destroy_request_bufs;
Jeff Layton84a15b92007-11-03 05:02:24 +00001013#ifdef CONFIG_CIFS_UPCALL
1014 rc = register_key_type(&cifs_spnego_key_type);
1015 if (rc)
1016 goto out_unregister_filesystem;
1017#endif
Steve French45af7a02006-04-21 22:52:25 +00001018 oplockThread = kthread_run(cifs_oplock_thread, NULL, "cifsoplockd");
1019 if (IS_ERR(oplockThread)) {
1020 rc = PTR_ERR(oplockThread);
Steve French6dc0f872007-07-06 23:13:06 +00001021 cERROR(1, ("error %d create oplock thread", rc));
Jeff Layton84a15b92007-11-03 05:02:24 +00001022 goto out_unregister_key_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
Steve French45af7a02006-04-21 22:52:25 +00001024
1025 dnotifyThread = kthread_run(cifs_dnotify_thread, NULL, "cifsdnotifyd");
1026 if (IS_ERR(dnotifyThread)) {
1027 rc = PTR_ERR(dnotifyThread);
Steve French6dc0f872007-07-06 23:13:06 +00001028 cERROR(1, ("error %d create dnotify thread", rc));
Steve French45af7a02006-04-21 22:52:25 +00001029 goto out_stop_oplock_thread;
1030 }
1031
1032 return 0;
1033
1034 out_stop_oplock_thread:
1035 kthread_stop(oplockThread);
Jeff Layton84a15b92007-11-03 05:02:24 +00001036 out_unregister_key_type:
1037#ifdef CONFIG_CIFS_UPCALL
1038 unregister_key_type(&cifs_spnego_key_type);
Steve French45af7a02006-04-21 22:52:25 +00001039 out_unregister_filesystem:
Jeff Layton84a15b92007-11-03 05:02:24 +00001040#endif
Steve French45af7a02006-04-21 22:52:25 +00001041 unregister_filesystem(&cifs_fs_type);
1042 out_destroy_request_bufs:
1043 cifs_destroy_request_bufs();
1044 out_destroy_mids:
1045 cifs_destroy_mids();
1046 out_destroy_inodecache:
1047 cifs_destroy_inodecache();
1048 out_clean_proc:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049#ifdef CONFIG_PROC_FS
1050 cifs_proc_clean();
1051#endif
1052 return rc;
1053}
1054
1055static void __exit
1056exit_cifs(void)
1057{
Steve French63135e02007-07-17 17:34:02 +00001058 cFYI(0, ("exit_cifs"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059#ifdef CONFIG_PROC_FS
1060 cifs_proc_clean();
1061#endif
Jeff Layton84a15b92007-11-03 05:02:24 +00001062#ifdef CONFIG_CIFS_UPCALL
1063 unregister_key_type(&cifs_spnego_key_type);
1064#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 unregister_filesystem(&cifs_fs_type);
1066 cifs_destroy_inodecache();
1067 cifs_destroy_mids();
1068 cifs_destroy_request_bufs();
Steve French45af7a02006-04-21 22:52:25 +00001069 kthread_stop(oplockThread);
1070 kthread_stop(dnotifyThread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
1073MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
Steve French6dc0f872007-07-06 23:13:06 +00001074MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075MODULE_DESCRIPTION
Steve French63135e02007-07-17 17:34:02 +00001076 ("VFS to access servers complying with the SNIA CIFS Specification "
1077 "e.g. Samba and Windows");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078MODULE_VERSION(CIFS_VERSION);
1079module_init(init_cifs)
1080module_exit(exit_cifs)