blob: 221b3334b737dc1b7d179bedf88df9258bd32b73 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/cifsfs.c
3 *
4 * Copyright (C) International Business Machines Corp., 2002,2004
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * Common Internet FileSystem (CIFS) client
8 *
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24/* Note that BB means BUGBUG (ie something to fix eventually) */
25
26#include <linux/module.h>
27#include <linux/fs.h>
28#include <linux/mount.h>
29#include <linux/slab.h>
30#include <linux/init.h>
31#include <linux/list.h>
32#include <linux/seq_file.h>
33#include <linux/vfs.h>
34#include <linux/mempool.h>
Steve French6ab16d22005-11-29 20:55:11 -080035#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "cifsfs.h"
37#include "cifspdu.h"
38#define DECLARE_GLOBALS_HERE
39#include "cifsglob.h"
40#include "cifsproto.h"
41#include "cifs_debug.h"
42#include "cifs_fs_sb.h"
43#include <linux/mm.h>
44#define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDUs */
45
46#ifdef CONFIG_CIFS_QUOTA
47static struct quotactl_ops cifs_quotactl_ops;
48#endif
49
50int cifsFYI = 0;
51int cifsERROR = 1;
52int traceSMB = 0;
53unsigned int oplockEnabled = 1;
54unsigned int experimEnabled = 0;
55unsigned int linuxExtEnabled = 1;
56unsigned int lookupCacheEnabled = 1;
57unsigned int multiuser_mount = 0;
58unsigned int extended_security = 0;
59unsigned int ntlmv2_support = 0;
60unsigned int sign_CIFS_PDUs = 1;
61extern struct task_struct * oplockThread; /* remove sparse warning */
62struct task_struct * oplockThread = NULL;
Steve French8d0d5092005-08-18 09:41:43 -070063extern struct task_struct * dnotifyThread; /* remove sparse warning */
64struct task_struct * dnotifyThread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
66module_param(CIFSMaxBufSize, int, 0);
67MODULE_PARM_DESC(CIFSMaxBufSize,"Network buffer size (not including header). Default: 16384 Range: 8192 to 130048");
68unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
69module_param(cifs_min_rcv, int, 0);
70MODULE_PARM_DESC(cifs_min_rcv,"Network buffers in pool. Default: 4 Range: 1 to 64");
71unsigned int cifs_min_small = 30;
72module_param(cifs_min_small, int, 0);
73MODULE_PARM_DESC(cifs_min_small,"Small network buffers in pool. Default: 30 Range: 2 to 256");
74unsigned int cifs_max_pending = CIFS_MAX_REQ;
75module_param(cifs_max_pending, int, 0);
76MODULE_PARM_DESC(cifs_max_pending,"Simultaneous requests to server. Default: 50 Range: 2 to 256");
77
78static DECLARE_COMPLETION(cifs_oplock_exited);
Steve French8d0d5092005-08-18 09:41:43 -070079static DECLARE_COMPLETION(cifs_dnotify_exited);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81extern mempool_t *cifs_sm_req_poolp;
82extern mempool_t *cifs_req_poolp;
83extern mempool_t *cifs_mid_poolp;
84
85extern kmem_cache_t *cifs_oplock_cachep;
86
87static int
88cifs_read_super(struct super_block *sb, void *data,
89 const char *devname, int silent)
90{
91 struct inode *inode;
92 struct cifs_sb_info *cifs_sb;
93 int rc = 0;
94
95 sb->s_flags |= MS_NODIRATIME; /* and probably even noatime */
96 sb->s_fs_info = kmalloc(sizeof(struct cifs_sb_info),GFP_KERNEL);
97 cifs_sb = CIFS_SB(sb);
98 if(cifs_sb == NULL)
99 return -ENOMEM;
100 else
101 memset(cifs_sb,0,sizeof(struct cifs_sb_info));
102
103
104 rc = cifs_mount(sb, cifs_sb, data, devname);
105
106 if (rc) {
107 if (!silent)
108 cERROR(1,
109 ("cifs_mount failed w/return code = %d", rc));
110 goto out_mount_failed;
111 }
112
113 sb->s_magic = CIFS_MAGIC_NUMBER;
114 sb->s_op = &cifs_super_ops;
115/* if(cifs_sb->tcon->ses->server->maxBuf > MAX_CIFS_HDR_SIZE + 512)
116 sb->s_blocksize = cifs_sb->tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE; */
117#ifdef CONFIG_CIFS_QUOTA
118 sb->s_qcop = &cifs_quotactl_ops;
119#endif
120 sb->s_blocksize = CIFS_MAX_MSGSIZE;
121 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
122 inode = iget(sb, ROOT_I);
123
124 if (!inode) {
125 rc = -ENOMEM;
126 goto out_no_root;
127 }
128
129 sb->s_root = d_alloc_root(inode);
130
131 if (!sb->s_root) {
132 rc = -ENOMEM;
133 goto out_no_root;
134 }
135
136 return 0;
137
138out_no_root:
139 cERROR(1, ("cifs_read_super: get root inode failed"));
140 if (inode)
141 iput(inode);
142
143out_mount_failed:
144 if(cifs_sb) {
145 if(cifs_sb->local_nls)
146 unload_nls(cifs_sb->local_nls);
147 kfree(cifs_sb);
148 }
149 return rc;
150}
151
152static void
153cifs_put_super(struct super_block *sb)
154{
155 int rc = 0;
156 struct cifs_sb_info *cifs_sb;
157
158 cFYI(1, ("In cifs_put_super"));
159 cifs_sb = CIFS_SB(sb);
160 if(cifs_sb == NULL) {
161 cFYI(1,("Empty cifs superblock info passed to unmount"));
162 return;
163 }
164 rc = cifs_umount(sb, cifs_sb);
165 if (rc) {
166 cERROR(1, ("cifs_umount failed with return code %d", rc));
167 }
168 unload_nls(cifs_sb->local_nls);
169 kfree(cifs_sb);
170 return;
171}
172
173static int
174cifs_statfs(struct super_block *sb, struct kstatfs *buf)
175{
Steve Frenchc81156d2005-04-28 22:41:07 -0700176 int xid;
177 int rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct cifs_sb_info *cifs_sb;
179 struct cifsTconInfo *pTcon;
180
181 xid = GetXid();
182
183 cifs_sb = CIFS_SB(sb);
184 pTcon = cifs_sb->tcon;
185
186 buf->f_type = CIFS_MAGIC_NUMBER;
187
188 /* instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO */
Steve Frenchc81156d2005-04-28 22:41:07 -0700189 buf->f_namelen = PATH_MAX; /* PATH_MAX may be too long - it would
190 presumably be total path, but note
191 that some servers (includinng Samba 3)
192 have a shorter maximum path */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 buf->f_files = 0; /* undefined */
194 buf->f_ffree = 0; /* unlimited */
195
196#ifdef CONFIG_CIFS_EXPERIMENTAL
197/* BB we could add a second check for a QFS Unix capability bit */
Steve Frenchf28ac912005-04-28 22:41:07 -0700198/* BB FIXME check CIFS_POSIX_EXTENSIONS Unix cap first FIXME BB */
Steve Frenchc81156d2005-04-28 22:41:07 -0700199 if ((pTcon->ses->capabilities & CAP_UNIX) && (CIFS_POSIX_EXTENSIONS &
200 le64_to_cpu(pTcon->fsUnixInfo.Capability)))
Steve French737b7582005-04-28 22:41:06 -0700201 rc = CIFSSMBQFSPosixInfo(xid, pTcon, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /* Only need to call the old QFSInfo if failed
204 on newer one */
205 if(rc)
206#endif /* CIFS_EXPERIMENTAL */
Steve French737b7582005-04-28 22:41:06 -0700207 rc = CIFSSMBQFSInfo(xid, pTcon, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Steve French20962432005-09-21 22:05:57 -0700209 /* Old Windows servers do not support level 103, retry with level
210 one if old server failed the previous call */
211 if(rc)
212 rc = SMBOldQFSInfo(xid, pTcon, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /*
214 int f_type;
215 __fsid_t f_fsid;
216 int f_namelen; */
Steve Frenchc81156d2005-04-28 22:41:07 -0700217 /* BB get from info in tcon struct at mount time call to QFSAttrInfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 FreeXid(xid);
Steve Frenchc81156d2005-04-28 22:41:07 -0700219 return 0; /* always return success? what if volume is no
220 longer available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223static int cifs_permission(struct inode * inode, int mask, struct nameidata *nd)
224{
225 struct cifs_sb_info *cifs_sb;
226
227 cifs_sb = CIFS_SB(inode->i_sb);
228
229 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
230 return 0;
231 } else /* file mode might have been restricted at mount time
232 on the client (above and beyond ACL on servers) for
233 servers which do not support setting and viewing mode bits,
234 so allowing client to check permissions is useful */
235 return generic_permission(inode, mask, NULL);
236}
237
238static kmem_cache_t *cifs_inode_cachep;
239static kmem_cache_t *cifs_req_cachep;
240static kmem_cache_t *cifs_mid_cachep;
241kmem_cache_t *cifs_oplock_cachep;
242static kmem_cache_t *cifs_sm_req_cachep;
243mempool_t *cifs_sm_req_poolp;
244mempool_t *cifs_req_poolp;
245mempool_t *cifs_mid_poolp;
246
247static struct inode *
248cifs_alloc_inode(struct super_block *sb)
249{
250 struct cifsInodeInfo *cifs_inode;
251 cifs_inode = kmem_cache_alloc(cifs_inode_cachep, SLAB_KERNEL);
252 if (!cifs_inode)
253 return NULL;
254 cifs_inode->cifsAttrs = 0x20; /* default */
255 atomic_set(&cifs_inode->inUse, 0);
256 cifs_inode->time = 0;
257 /* Until the file is open and we have gotten oplock
258 info back from the server, can not assume caching of
259 file data or metadata */
260 cifs_inode->clientCanCacheRead = FALSE;
261 cifs_inode->clientCanCacheAll = FALSE;
262 cifs_inode->vfs_inode.i_blksize = CIFS_MAX_MSGSIZE;
263 cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
Steve Frenche30dcf32005-09-20 20:49:16 -0700264 cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 INIT_LIST_HEAD(&cifs_inode->openFileList);
266 return &cifs_inode->vfs_inode;
267}
268
269static void
270cifs_destroy_inode(struct inode *inode)
271{
272 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
273}
274
275/*
276 * cifs_show_options() is for displaying mount options in /proc/mounts.
277 * Not all settable options are displayed but most of the important
278 * ones are.
279 */
280static int
281cifs_show_options(struct seq_file *s, struct vfsmount *m)
282{
283 struct cifs_sb_info *cifs_sb;
284
285 cifs_sb = CIFS_SB(m->mnt_sb);
286
287 if (cifs_sb) {
288 if (cifs_sb->tcon) {
289 seq_printf(s, ",unc=%s", cifs_sb->tcon->treeName);
290 if (cifs_sb->tcon->ses) {
291 if (cifs_sb->tcon->ses->userName)
292 seq_printf(s, ",username=%s",
293 cifs_sb->tcon->ses->userName);
294 if(cifs_sb->tcon->ses->domainName)
295 seq_printf(s, ",domain=%s",
296 cifs_sb->tcon->ses->domainName);
297 }
298 }
299 seq_printf(s, ",rsize=%d",cifs_sb->rsize);
300 seq_printf(s, ",wsize=%d",cifs_sb->wsize);
301 }
302 return 0;
303}
304
305#ifdef CONFIG_CIFS_QUOTA
306int cifs_xquota_set(struct super_block * sb, int quota_type, qid_t qid,
307 struct fs_disk_quota * pdquota)
308{
309 int xid;
310 int rc = 0;
311 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
312 struct cifsTconInfo *pTcon;
313
314 if(cifs_sb)
315 pTcon = cifs_sb->tcon;
316 else
317 return -EIO;
318
319
320 xid = GetXid();
321 if(pTcon) {
322 cFYI(1,("set type: 0x%x id: %d",quota_type,qid));
323 } else {
324 return -EIO;
325 }
326
327 FreeXid(xid);
328 return rc;
329}
330
331int cifs_xquota_get(struct super_block * sb, int quota_type, qid_t qid,
332 struct fs_disk_quota * pdquota)
333{
334 int xid;
335 int rc = 0;
336 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
337 struct cifsTconInfo *pTcon;
338
339 if(cifs_sb)
340 pTcon = cifs_sb->tcon;
341 else
342 return -EIO;
343
344 xid = GetXid();
345 if(pTcon) {
346 cFYI(1,("set type: 0x%x id: %d",quota_type,qid));
347 } else {
348 rc = -EIO;
349 }
350
351 FreeXid(xid);
352 return rc;
353}
354
355int cifs_xstate_set(struct super_block * sb, unsigned int flags, int operation)
356{
357 int xid;
358 int rc = 0;
359 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
360 struct cifsTconInfo *pTcon;
361
362 if(cifs_sb)
363 pTcon = cifs_sb->tcon;
364 else
365 return -EIO;
366
367 xid = GetXid();
368 if(pTcon) {
369 cFYI(1,("flags: 0x%x operation: 0x%x",flags,operation));
370 } else {
371 rc = -EIO;
372 }
373
374 FreeXid(xid);
375 return rc;
376}
377
378int cifs_xstate_get(struct super_block * sb, struct fs_quota_stat *qstats)
379{
380 int xid;
381 int rc = 0;
382 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
383 struct cifsTconInfo *pTcon;
384
385 if(cifs_sb) {
386 pTcon = cifs_sb->tcon;
387 } else {
388 return -EIO;
389 }
390 xid = GetXid();
391 if(pTcon) {
392 cFYI(1,("pqstats %p",qstats));
393 } else {
394 rc = -EIO;
395 }
396
397 FreeXid(xid);
398 return rc;
399}
400
401static struct quotactl_ops cifs_quotactl_ops = {
402 .set_xquota = cifs_xquota_set,
403 .get_xquota = cifs_xquota_set,
404 .set_xstate = cifs_xstate_set,
405 .get_xstate = cifs_xstate_get,
406};
407#endif
408
Steve French7b7abfe2005-11-09 15:21:09 -0800409#ifdef CONFIG_CIFS_EXPERIMENTAL
Steve French68058e72005-10-10 10:34:22 -0700410static void cifs_umount_begin(struct super_block * sblock)
411{
Steve French5e1253b2005-10-10 14:06:37 -0700412 struct cifs_sb_info *cifs_sb;
Steve French9e2e85f2005-10-10 14:28:38 -0700413 struct cifsTconInfo * tcon;
Steve French68058e72005-10-10 10:34:22 -0700414
Steve French34210f32005-10-10 14:31:13 -0700415 cifs_sb = CIFS_SB(sblock);
Steve French5e1253b2005-10-10 14:06:37 -0700416 if(cifs_sb == NULL)
Steve French9e2e85f2005-10-10 14:28:38 -0700417 return;
418
419 tcon = cifs_sb->tcon;
420 if(tcon == NULL)
421 return;
Steve French5e1253b2005-10-10 14:06:37 -0700422 down(&tcon->tconSem);
423 if (atomic_read(&tcon->useCount) == 1)
424 tcon->tidStatus = CifsExiting;
425 up(&tcon->tconSem);
426
Steve French7b7abfe2005-11-09 15:21:09 -0800427 /* cancel_brl_requests(tcon); */
428 /* cancel_notify_requests(tcon); */
Steve French9e2e85f2005-10-10 14:28:38 -0700429 if(tcon->ses && tcon->ses->server)
Steve French5e1253b2005-10-10 14:06:37 -0700430 {
Steve French7b7abfe2005-11-09 15:21:09 -0800431 cFYI(1,("wake up tasks now - umount begin not complete"));
Steve French9e2e85f2005-10-10 14:28:38 -0700432 wake_up_all(&tcon->ses->server->request_q);
Steve French6ab16d22005-11-29 20:55:11 -0800433 wake_up_all(&tcon->ses->server->response_q);
434 msleep(1); /* yield */
435 /* we have to kick the requests once more */
436 wake_up_all(&tcon->ses->server->response_q);
437 msleep(1);
Steve French5e1253b2005-10-10 14:06:37 -0700438 }
439/* BB FIXME - finish add checks for tidStatus BB */
Steve French68058e72005-10-10 10:34:22 -0700440
441 return;
442}
Steve French7b7abfe2005-11-09 15:21:09 -0800443#endif
Steve French68058e72005-10-10 10:34:22 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445static int cifs_remount(struct super_block *sb, int *flags, char *data)
446{
447 *flags |= MS_NODIRATIME;
448 return 0;
449}
450
451struct super_operations cifs_super_ops = {
452 .read_inode = cifs_read_inode,
453 .put_super = cifs_put_super,
454 .statfs = cifs_statfs,
455 .alloc_inode = cifs_alloc_inode,
456 .destroy_inode = cifs_destroy_inode,
457/* .drop_inode = generic_delete_inode,
458 .delete_inode = cifs_delete_inode, *//* Do not need the above two functions
459 unless later we add lazy close of inodes or unless the kernel forgets to call
460 us with the same number of releases (closes) as opens */
461 .show_options = cifs_show_options,
Steve French7b7abfe2005-11-09 15:21:09 -0800462#ifdef CONFIG_CIFS_EXPERIMENTAL
463 .umount_begin = cifs_umount_begin,
464#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 .remount_fs = cifs_remount,
466};
467
468static struct super_block *
469cifs_get_sb(struct file_system_type *fs_type,
470 int flags, const char *dev_name, void *data)
471{
472 int rc;
473 struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
474
475 cFYI(1, ("Devname: %s flags: %d ", dev_name, flags));
476
477 if (IS_ERR(sb))
478 return sb;
479
480 sb->s_flags = flags;
481
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800482 rc = cifs_read_super(sb, data, dev_name, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (rc) {
484 up_write(&sb->s_umount);
485 deactivate_super(sb);
486 return ERR_PTR(rc);
487 }
488 sb->s_flags |= MS_ACTIVE;
489 return sb;
490}
491
Steve French87c89dd2005-11-17 17:03:00 -0800492static ssize_t cifs_file_writev(struct file *file, const struct iovec *iov,
493 unsigned long nr_segs, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Steve French87c89dd2005-11-17 17:03:00 -0800495 struct inode *inode = file->f_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 ssize_t written;
497
Steve French87c89dd2005-11-17 17:03:00 -0800498 written = generic_file_writev(file, iov, nr_segs, ppos);
499 if (!CIFS_I(inode)->clientCanCacheAll)
500 filemap_fdatawrite(inode->i_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return written;
502}
503
Steve French87c89dd2005-11-17 17:03:00 -0800504static ssize_t cifs_file_aio_write(struct kiocb *iocb, const char __user *buf,
505 size_t count, loff_t pos)
506{
507 struct inode *inode = iocb->ki_filp->f_dentry->d_inode;
508 ssize_t written;
509
510 written = generic_file_aio_write(iocb, buf, count, pos);
511 if (!CIFS_I(inode)->clientCanCacheAll)
512 filemap_fdatawrite(inode->i_mapping);
513 return written;
514}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Steve Frenchc32a0b62006-01-12 14:41:28 -0800516static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
517{
518 /* origin == SEEK_END => we must revalidate the cached file length */
519 if (origin == 2) {
520 int retval = cifs_revalidate(file->f_dentry);
521 if (retval < 0)
522 return (loff_t)retval;
523 }
524 return remote_llseek(file, offset, origin);
525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527static struct file_system_type cifs_fs_type = {
528 .owner = THIS_MODULE,
529 .name = "cifs",
530 .get_sb = cifs_get_sb,
531 .kill_sb = kill_anon_super,
532 /* .fs_flags */
533};
534struct inode_operations cifs_dir_inode_ops = {
535 .create = cifs_create,
536 .lookup = cifs_lookup,
537 .getattr = cifs_getattr,
538 .unlink = cifs_unlink,
539 .link = cifs_hardlink,
540 .mkdir = cifs_mkdir,
541 .rmdir = cifs_rmdir,
542 .rename = cifs_rename,
543 .permission = cifs_permission,
544/* revalidate:cifs_revalidate, */
545 .setattr = cifs_setattr,
546 .symlink = cifs_symlink,
547 .mknod = cifs_mknod,
548#ifdef CONFIG_CIFS_XATTR
549 .setxattr = cifs_setxattr,
550 .getxattr = cifs_getxattr,
551 .listxattr = cifs_listxattr,
552 .removexattr = cifs_removexattr,
553#endif
554};
555
556struct inode_operations cifs_file_inode_ops = {
557/* revalidate:cifs_revalidate, */
558 .setattr = cifs_setattr,
559 .getattr = cifs_getattr, /* do we need this anymore? */
560 .rename = cifs_rename,
561 .permission = cifs_permission,
562#ifdef CONFIG_CIFS_XATTR
563 .setxattr = cifs_setxattr,
564 .getxattr = cifs_getxattr,
565 .listxattr = cifs_listxattr,
566 .removexattr = cifs_removexattr,
567#endif
568};
569
570struct inode_operations cifs_symlink_inode_ops = {
571 .readlink = generic_readlink,
572 .follow_link = cifs_follow_link,
573 .put_link = cifs_put_link,
574 .permission = cifs_permission,
575 /* BB add the following two eventually */
576 /* revalidate: cifs_revalidate,
577 setattr: cifs_notify_change, *//* BB do we need notify change */
578#ifdef CONFIG_CIFS_XATTR
579 .setxattr = cifs_setxattr,
580 .getxattr = cifs_getxattr,
581 .listxattr = cifs_listxattr,
582 .removexattr = cifs_removexattr,
583#endif
584};
585
586struct file_operations cifs_file_ops = {
Steve French87c89dd2005-11-17 17:03:00 -0800587 .read = do_sync_read,
588 .write = do_sync_write,
589 .readv = generic_file_readv,
590 .writev = cifs_file_writev,
591 .aio_read = generic_file_aio_read,
592 .aio_write = cifs_file_aio_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 .open = cifs_open,
594 .release = cifs_close,
595 .lock = cifs_lock,
596 .fsync = cifs_fsync,
597 .flush = cifs_flush,
598 .mmap = cifs_file_mmap,
599 .sendfile = generic_file_sendfile,
Steve Frenchc32a0b62006-01-12 14:41:28 -0800600 .llseek = cifs_llseek,
Steve Frenchc67593a2005-04-28 22:41:04 -0700601#ifdef CONFIG_CIFS_POSIX
602 .ioctl = cifs_ioctl,
603#endif /* CONFIG_CIFS_POSIX */
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605#ifdef CONFIG_CIFS_EXPERIMENTAL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 .dir_notify = cifs_dir_notify,
607#endif /* CONFIG_CIFS_EXPERIMENTAL */
608};
609
610struct file_operations cifs_file_direct_ops = {
611 /* no mmap, no aio, no readv -
612 BB reevaluate whether they can be done with directio, no cache */
613 .read = cifs_user_read,
614 .write = cifs_user_write,
615 .open = cifs_open,
616 .release = cifs_close,
617 .lock = cifs_lock,
618 .fsync = cifs_fsync,
619 .flush = cifs_flush,
620 .sendfile = generic_file_sendfile, /* BB removeme BB */
Steve Frenchc67593a2005-04-28 22:41:04 -0700621#ifdef CONFIG_CIFS_POSIX
622 .ioctl = cifs_ioctl,
623#endif /* CONFIG_CIFS_POSIX */
Steve Frenchc32a0b62006-01-12 14:41:28 -0800624 .llseek = cifs_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625#ifdef CONFIG_CIFS_EXPERIMENTAL
626 .dir_notify = cifs_dir_notify,
627#endif /* CONFIG_CIFS_EXPERIMENTAL */
628};
Steve French8b94bcb2005-11-11 11:41:00 -0800629struct file_operations cifs_file_nobrl_ops = {
Steve French87c89dd2005-11-17 17:03:00 -0800630 .read = do_sync_read,
631 .write = do_sync_write,
632 .readv = generic_file_readv,
633 .writev = cifs_file_writev,
634 .aio_read = generic_file_aio_read,
635 .aio_write = cifs_file_aio_write,
636 .open = cifs_open,
637 .release = cifs_close,
638 .fsync = cifs_fsync,
639 .flush = cifs_flush,
640 .mmap = cifs_file_mmap,
641 .sendfile = generic_file_sendfile,
Steve Frenchc32a0b62006-01-12 14:41:28 -0800642 .llseek = cifs_llseek,
Steve French8b94bcb2005-11-11 11:41:00 -0800643#ifdef CONFIG_CIFS_POSIX
Steve French87c89dd2005-11-17 17:03:00 -0800644 .ioctl = cifs_ioctl,
Steve French8b94bcb2005-11-11 11:41:00 -0800645#endif /* CONFIG_CIFS_POSIX */
646
647#ifdef CONFIG_CIFS_EXPERIMENTAL
Steve French87c89dd2005-11-17 17:03:00 -0800648 .dir_notify = cifs_dir_notify,
Steve French8b94bcb2005-11-11 11:41:00 -0800649#endif /* CONFIG_CIFS_EXPERIMENTAL */
650};
651
652struct file_operations cifs_file_direct_nobrl_ops = {
Steve French87c89dd2005-11-17 17:03:00 -0800653 /* no mmap, no aio, no readv -
654 BB reevaluate whether they can be done with directio, no cache */
655 .read = cifs_user_read,
656 .write = cifs_user_write,
657 .open = cifs_open,
658 .release = cifs_close,
659 .fsync = cifs_fsync,
660 .flush = cifs_flush,
661 .sendfile = generic_file_sendfile, /* BB removeme BB */
Steve French8b94bcb2005-11-11 11:41:00 -0800662#ifdef CONFIG_CIFS_POSIX
Steve French87c89dd2005-11-17 17:03:00 -0800663 .ioctl = cifs_ioctl,
Steve French8b94bcb2005-11-11 11:41:00 -0800664#endif /* CONFIG_CIFS_POSIX */
Steve Frenchc32a0b62006-01-12 14:41:28 -0800665 .llseek = cifs_llseek,
Steve French8b94bcb2005-11-11 11:41:00 -0800666#ifdef CONFIG_CIFS_EXPERIMENTAL
Steve French87c89dd2005-11-17 17:03:00 -0800667 .dir_notify = cifs_dir_notify,
Steve French8b94bcb2005-11-11 11:41:00 -0800668#endif /* CONFIG_CIFS_EXPERIMENTAL */
669};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671struct file_operations cifs_dir_ops = {
672 .readdir = cifs_readdir,
673 .release = cifs_closedir,
674 .read = generic_read_dir,
675#ifdef CONFIG_CIFS_EXPERIMENTAL
676 .dir_notify = cifs_dir_notify,
677#endif /* CONFIG_CIFS_EXPERIMENTAL */
Steve Frenchf28ac912005-04-28 22:41:07 -0700678 .ioctl = cifs_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679};
680
681static void
682cifs_init_once(void *inode, kmem_cache_t * cachep, unsigned long flags)
683{
684 struct cifsInodeInfo *cifsi = inode;
685
686 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
687 SLAB_CTOR_CONSTRUCTOR) {
688 inode_init_once(&cifsi->vfs_inode);
689 INIT_LIST_HEAD(&cifsi->lockList);
690 }
691}
692
693static int
694cifs_init_inodecache(void)
695{
696 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
697 sizeof (struct cifsInodeInfo),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800698 0, (SLAB_RECLAIM_ACCOUNT|
699 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 cifs_init_once, NULL);
701 if (cifs_inode_cachep == NULL)
702 return -ENOMEM;
703
704 return 0;
705}
706
707static void
708cifs_destroy_inodecache(void)
709{
710 if (kmem_cache_destroy(cifs_inode_cachep))
711 printk(KERN_WARNING "cifs_inode_cache: error freeing\n");
712}
713
714static int
715cifs_init_request_bufs(void)
716{
717 if(CIFSMaxBufSize < 8192) {
718 /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
719 Unicode path name has to fit in any SMB/CIFS path based frames */
720 CIFSMaxBufSize = 8192;
721 } else if (CIFSMaxBufSize > 1024*127) {
722 CIFSMaxBufSize = 1024 * 127;
723 } else {
724 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
725 }
726/* cERROR(1,("CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize)); */
727 cifs_req_cachep = kmem_cache_create("cifs_request",
728 CIFSMaxBufSize +
729 MAX_CIFS_HDR_SIZE, 0,
730 SLAB_HWCACHE_ALIGN, NULL, NULL);
731 if (cifs_req_cachep == NULL)
732 return -ENOMEM;
733
734 if(cifs_min_rcv < 1)
735 cifs_min_rcv = 1;
736 else if (cifs_min_rcv > 64) {
737 cifs_min_rcv = 64;
738 cERROR(1,("cifs_min_rcv set to maximum (64)"));
739 }
740
741 cifs_req_poolp = mempool_create(cifs_min_rcv,
742 mempool_alloc_slab,
743 mempool_free_slab,
744 cifs_req_cachep);
745
746 if(cifs_req_poolp == NULL) {
747 kmem_cache_destroy(cifs_req_cachep);
748 return -ENOMEM;
749 }
Steve Frenchec637e32005-12-12 20:53:18 -0800750 /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 almost all handle based requests (but not write response, nor is it
752 sufficient for path based requests). A smaller size would have
753 been more efficient (compacting multiple slab items on one 4k page)
754 for the case in which debug was on, but this larger size allows
755 more SMBs to use small buffer alloc and is still much more
756 efficient to alloc 1 per page off the slab compared to 17K (5page)
757 alloc of large cifs buffers even when page debugging is on */
758 cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
Steve Frenchec637e32005-12-12 20:53:18 -0800759 MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
760 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (cifs_sm_req_cachep == NULL) {
762 mempool_destroy(cifs_req_poolp);
763 kmem_cache_destroy(cifs_req_cachep);
764 return -ENOMEM;
765 }
766
767 if(cifs_min_small < 2)
768 cifs_min_small = 2;
769 else if (cifs_min_small > 256) {
770 cifs_min_small = 256;
771 cFYI(1,("cifs_min_small set to maximum (256)"));
772 }
773
774 cifs_sm_req_poolp = mempool_create(cifs_min_small,
775 mempool_alloc_slab,
776 mempool_free_slab,
777 cifs_sm_req_cachep);
778
779 if(cifs_sm_req_poolp == NULL) {
780 mempool_destroy(cifs_req_poolp);
781 kmem_cache_destroy(cifs_req_cachep);
782 kmem_cache_destroy(cifs_sm_req_cachep);
783 return -ENOMEM;
784 }
785
786 return 0;
787}
788
789static void
790cifs_destroy_request_bufs(void)
791{
792 mempool_destroy(cifs_req_poolp);
793 if (kmem_cache_destroy(cifs_req_cachep))
794 printk(KERN_WARNING
795 "cifs_destroy_request_cache: error not all structures were freed\n");
796 mempool_destroy(cifs_sm_req_poolp);
797 if (kmem_cache_destroy(cifs_sm_req_cachep))
798 printk(KERN_WARNING
799 "cifs_destroy_request_cache: cifs_small_rq free error\n");
800}
801
802static int
803cifs_init_mids(void)
804{
805 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
806 sizeof (struct mid_q_entry), 0,
807 SLAB_HWCACHE_ALIGN, NULL, NULL);
808 if (cifs_mid_cachep == NULL)
809 return -ENOMEM;
810
811 cifs_mid_poolp = mempool_create(3 /* a reasonable min simultan opers */,
812 mempool_alloc_slab,
813 mempool_free_slab,
814 cifs_mid_cachep);
815 if(cifs_mid_poolp == NULL) {
816 kmem_cache_destroy(cifs_mid_cachep);
817 return -ENOMEM;
818 }
819
820 cifs_oplock_cachep = kmem_cache_create("cifs_oplock_structs",
821 sizeof (struct oplock_q_entry), 0,
822 SLAB_HWCACHE_ALIGN, NULL, NULL);
823 if (cifs_oplock_cachep == NULL) {
824 kmem_cache_destroy(cifs_mid_cachep);
825 mempool_destroy(cifs_mid_poolp);
826 return -ENOMEM;
827 }
828
829 return 0;
830}
831
832static void
833cifs_destroy_mids(void)
834{
835 mempool_destroy(cifs_mid_poolp);
836 if (kmem_cache_destroy(cifs_mid_cachep))
837 printk(KERN_WARNING
838 "cifs_destroy_mids: error not all structures were freed\n");
839
840 if (kmem_cache_destroy(cifs_oplock_cachep))
841 printk(KERN_WARNING
842 "error not all oplock structures were freed\n");
843}
844
845static int cifs_oplock_thread(void * dummyarg)
846{
847 struct oplock_q_entry * oplock_item;
848 struct cifsTconInfo *pTcon;
849 struct inode * inode;
850 __u16 netfid;
851 int rc;
852
853 daemonize("cifsoplockd");
854 allow_signal(SIGTERM);
855
856 oplockThread = current;
857 do {
Steve Frenchede13272005-08-30 20:10:14 -0700858 if (try_to_freeze())
859 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 spin_lock(&GlobalMid_Lock);
862 if(list_empty(&GlobalOplock_Q)) {
863 spin_unlock(&GlobalMid_Lock);
864 set_current_state(TASK_INTERRUPTIBLE);
865 schedule_timeout(39*HZ);
866 } else {
867 oplock_item = list_entry(GlobalOplock_Q.next,
868 struct oplock_q_entry, qhead);
869 if(oplock_item) {
870 cFYI(1,("found oplock item to write out"));
871 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
877 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)) {
882 rc = filemap_fdatawrite(inode->i_mapping);
883 if(CIFS_I(inode)->clientCanCacheRead == 0) {
884 filemap_fdatawait(inode->i_mapping);
885 invalidate_remote_inode(inode);
886 }
887 } else
888 rc = 0;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800889 /* mutex_unlock(&inode->i_mutex);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (rc)
891 CIFS_I(inode)->write_behind_rc = rc;
892 cFYI(1,("Oplock flush inode %p rc %d",inode,rc));
893
894 /* releasing a stale oplock after recent reconnection
895 of smb session using a now incorrect file
896 handle is not a data integrity issue but do
897 not bother sending an oplock release if session
898 to server still is disconnected since oplock
899 already released by the server in that case */
900 if(pTcon->tidStatus != CifsNeedReconnect) {
901 rc = CIFSSMBLock(0, pTcon, netfid,
902 0 /* len */ , 0 /* offset */, 0,
903 0, LOCKING_ANDX_OPLOCK_RELEASE,
904 0 /* wait flag */);
905 cFYI(1,("Oplock release rc = %d ",rc));
906 }
907 } else
908 spin_unlock(&GlobalMid_Lock);
Steve French68058e72005-10-10 10:34:22 -0700909 set_current_state(TASK_INTERRUPTIBLE);
910 schedule_timeout(1); /* yield in case q were corrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912 } while(!signal_pending(current));
Steve French57337e42005-04-28 22:41:10 -0700913 oplockThread = NULL;
Steve Frenchf1914012005-08-18 09:37:34 -0700914 complete_and_exit (&cifs_oplock_exited, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Steve French8d0d5092005-08-18 09:41:43 -0700917static int cifs_dnotify_thread(void * dummyarg)
918{
Steve French6ab16d22005-11-29 20:55:11 -0800919 struct list_head *tmp;
920 struct cifsSesInfo *ses;
921
Steve French8d0d5092005-08-18 09:41:43 -0700922 daemonize("cifsdnotifyd");
923 allow_signal(SIGTERM);
924
925 dnotifyThread = current;
926 do {
Steve French16abbec2005-08-30 13:10:14 -0700927 if(try_to_freeze())
928 continue;
Steve French8d0d5092005-08-18 09:41:43 -0700929 set_current_state(TASK_INTERRUPTIBLE);
Steve French6ab16d22005-11-29 20:55:11 -0800930 schedule_timeout(15*HZ);
931 read_lock(&GlobalSMBSeslock);
932 /* check if any stuck requests that need
933 to be woken up and wakeq so the
934 thread can wake up and error out */
935 list_for_each(tmp, &GlobalSMBSessionList) {
936 ses = list_entry(tmp, struct cifsSesInfo,
937 cifsSessionList);
938 if(ses && ses->server &&
Steve French2a138ebb2005-11-29 21:22:19 -0800939 atomic_read(&ses->server->inFlight))
Steve French6ab16d22005-11-29 20:55:11 -0800940 wake_up_all(&ses->server->response_q);
941 }
942 read_unlock(&GlobalSMBSeslock);
Steve French8d0d5092005-08-18 09:41:43 -0700943 } while(!signal_pending(current));
944 complete_and_exit (&cifs_dnotify_exited, 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
954 INIT_LIST_HEAD(&GlobalServerList); /* BB not implemented yet */
955 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);
961#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962/*
963 * Initialize Global counters
964 */
965 atomic_set(&sesInfoAllocCount, 0);
966 atomic_set(&tconInfoAllocCount, 0);
967 atomic_set(&tcpSesAllocCount,0);
968 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;
982 rwlock_init(&GlobalSMBSeslock);
983 spin_lock_init(&GlobalMid_Lock);
984
985 if(cifs_max_pending < 2) {
986 cifs_max_pending = 2;
987 cFYI(1,("cifs_max_pending set to min of 2"));
988 } else if(cifs_max_pending > 256) {
989 cifs_max_pending = 256;
990 cFYI(1,("cifs_max_pending set to max of 256"));
991 }
992
993 rc = cifs_init_inodecache();
994 if (!rc) {
995 rc = cifs_init_mids();
996 if (!rc) {
997 rc = cifs_init_request_bufs();
998 if (!rc) {
999 rc = register_filesystem(&cifs_fs_type);
1000 if (!rc) {
1001 rc = (int)kernel_thread(cifs_oplock_thread, NULL,
1002 CLONE_FS | CLONE_FILES | CLONE_VM);
Steve French8d0d5092005-08-18 09:41:43 -07001003 if(rc > 0) {
1004 rc = (int)kernel_thread(cifs_dnotify_thread, NULL,
1005 CLONE_FS | CLONE_FILES | CLONE_VM);
1006 if(rc > 0)
1007 return 0;
1008 else
1009 cERROR(1,("error %d create dnotify thread", rc));
1010 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 cERROR(1,("error %d create oplock thread",rc));
Steve French8d0d5092005-08-18 09:41:43 -07001012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014 cifs_destroy_request_bufs();
1015 }
1016 cifs_destroy_mids();
1017 }
1018 cifs_destroy_inodecache();
1019 }
1020#ifdef CONFIG_PROC_FS
1021 cifs_proc_clean();
1022#endif
1023 return rc;
1024}
1025
1026static void __exit
1027exit_cifs(void)
1028{
1029 cFYI(0, ("In unregister ie exit_cifs"));
1030#ifdef CONFIG_PROC_FS
1031 cifs_proc_clean();
1032#endif
1033 unregister_filesystem(&cifs_fs_type);
1034 cifs_destroy_inodecache();
1035 cifs_destroy_mids();
1036 cifs_destroy_request_bufs();
1037 if(oplockThread) {
1038 send_sig(SIGTERM, oplockThread, 1);
1039 wait_for_completion(&cifs_oplock_exited);
1040 }
Steve French8d0d5092005-08-18 09:41:43 -07001041 if(dnotifyThread) {
1042 send_sig(SIGTERM, dnotifyThread, 1);
1043 wait_for_completion(&cifs_dnotify_exited);
1044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
1048MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
1049MODULE_DESCRIPTION
1050 ("VFS to access servers complying with the SNIA CIFS Specification e.g. Samba and Windows");
1051MODULE_VERSION(CIFS_VERSION);
1052module_init(init_cifs)
1053module_exit(exit_cifs)