blob: fd5eae37f2a84de197bd3317a0bb160ae54be5fc [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>
35#include "cifsfs.h"
36#include "cifspdu.h"
37#define DECLARE_GLOBALS_HERE
38#include "cifsglob.h"
39#include "cifsproto.h"
40#include "cifs_debug.h"
41#include "cifs_fs_sb.h"
42#include <linux/mm.h>
43#define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDUs */
44
45#ifdef CONFIG_CIFS_QUOTA
46static struct quotactl_ops cifs_quotactl_ops;
47#endif
48
49int cifsFYI = 0;
50int cifsERROR = 1;
51int traceSMB = 0;
52unsigned int oplockEnabled = 1;
53unsigned int experimEnabled = 0;
54unsigned int linuxExtEnabled = 1;
55unsigned int lookupCacheEnabled = 1;
56unsigned int multiuser_mount = 0;
57unsigned int extended_security = 0;
58unsigned int ntlmv2_support = 0;
59unsigned int sign_CIFS_PDUs = 1;
60extern struct task_struct * oplockThread; /* remove sparse warning */
61struct task_struct * oplockThread = NULL;
Steve French8d0d5092005-08-18 09:41:43 -070062extern struct task_struct * dnotifyThread; /* remove sparse warning */
63struct task_struct * dnotifyThread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
65module_param(CIFSMaxBufSize, int, 0);
66MODULE_PARM_DESC(CIFSMaxBufSize,"Network buffer size (not including header). Default: 16384 Range: 8192 to 130048");
67unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
68module_param(cifs_min_rcv, int, 0);
69MODULE_PARM_DESC(cifs_min_rcv,"Network buffers in pool. Default: 4 Range: 1 to 64");
70unsigned int cifs_min_small = 30;
71module_param(cifs_min_small, int, 0);
72MODULE_PARM_DESC(cifs_min_small,"Small network buffers in pool. Default: 30 Range: 2 to 256");
73unsigned int cifs_max_pending = CIFS_MAX_REQ;
74module_param(cifs_max_pending, int, 0);
75MODULE_PARM_DESC(cifs_max_pending,"Simultaneous requests to server. Default: 50 Range: 2 to 256");
76
77static DECLARE_COMPLETION(cifs_oplock_exited);
Steve French8d0d5092005-08-18 09:41:43 -070078static DECLARE_COMPLETION(cifs_dnotify_exited);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80extern mempool_t *cifs_sm_req_poolp;
81extern mempool_t *cifs_req_poolp;
82extern mempool_t *cifs_mid_poolp;
83
84extern kmem_cache_t *cifs_oplock_cachep;
85
86static int
87cifs_read_super(struct super_block *sb, void *data,
88 const char *devname, int silent)
89{
90 struct inode *inode;
91 struct cifs_sb_info *cifs_sb;
92 int rc = 0;
93
94 sb->s_flags |= MS_NODIRATIME; /* and probably even noatime */
95 sb->s_fs_info = kmalloc(sizeof(struct cifs_sb_info),GFP_KERNEL);
96 cifs_sb = CIFS_SB(sb);
97 if(cifs_sb == NULL)
98 return -ENOMEM;
99 else
100 memset(cifs_sb,0,sizeof(struct cifs_sb_info));
101
102
103 rc = cifs_mount(sb, cifs_sb, data, devname);
104
105 if (rc) {
106 if (!silent)
107 cERROR(1,
108 ("cifs_mount failed w/return code = %d", rc));
109 goto out_mount_failed;
110 }
111
112 sb->s_magic = CIFS_MAGIC_NUMBER;
113 sb->s_op = &cifs_super_ops;
114/* if(cifs_sb->tcon->ses->server->maxBuf > MAX_CIFS_HDR_SIZE + 512)
115 sb->s_blocksize = cifs_sb->tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE; */
116#ifdef CONFIG_CIFS_QUOTA
117 sb->s_qcop = &cifs_quotactl_ops;
118#endif
119 sb->s_blocksize = CIFS_MAX_MSGSIZE;
120 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
121 inode = iget(sb, ROOT_I);
122
123 if (!inode) {
124 rc = -ENOMEM;
125 goto out_no_root;
126 }
127
128 sb->s_root = d_alloc_root(inode);
129
130 if (!sb->s_root) {
131 rc = -ENOMEM;
132 goto out_no_root;
133 }
134
135 return 0;
136
137out_no_root:
138 cERROR(1, ("cifs_read_super: get root inode failed"));
139 if (inode)
140 iput(inode);
141
142out_mount_failed:
143 if(cifs_sb) {
144 if(cifs_sb->local_nls)
145 unload_nls(cifs_sb->local_nls);
146 kfree(cifs_sb);
147 }
148 return rc;
149}
150
151static void
152cifs_put_super(struct super_block *sb)
153{
154 int rc = 0;
155 struct cifs_sb_info *cifs_sb;
156
157 cFYI(1, ("In cifs_put_super"));
158 cifs_sb = CIFS_SB(sb);
159 if(cifs_sb == NULL) {
160 cFYI(1,("Empty cifs superblock info passed to unmount"));
161 return;
162 }
163 rc = cifs_umount(sb, cifs_sb);
164 if (rc) {
165 cERROR(1, ("cifs_umount failed with return code %d", rc));
166 }
167 unload_nls(cifs_sb->local_nls);
168 kfree(cifs_sb);
169 return;
170}
171
172static int
173cifs_statfs(struct super_block *sb, struct kstatfs *buf)
174{
Steve Frenchc81156d2005-04-28 22:41:07 -0700175 int xid;
176 int rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct cifs_sb_info *cifs_sb;
178 struct cifsTconInfo *pTcon;
179
180 xid = GetXid();
181
182 cifs_sb = CIFS_SB(sb);
183 pTcon = cifs_sb->tcon;
184
185 buf->f_type = CIFS_MAGIC_NUMBER;
186
187 /* instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO */
Steve Frenchc81156d2005-04-28 22:41:07 -0700188 buf->f_namelen = PATH_MAX; /* PATH_MAX may be too long - it would
189 presumably be total path, but note
190 that some servers (includinng Samba 3)
191 have a shorter maximum path */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 buf->f_files = 0; /* undefined */
193 buf->f_ffree = 0; /* unlimited */
194
195#ifdef CONFIG_CIFS_EXPERIMENTAL
196/* BB we could add a second check for a QFS Unix capability bit */
Steve Frenchf28ac912005-04-28 22:41:07 -0700197/* BB FIXME check CIFS_POSIX_EXTENSIONS Unix cap first FIXME BB */
Steve Frenchc81156d2005-04-28 22:41:07 -0700198 if ((pTcon->ses->capabilities & CAP_UNIX) && (CIFS_POSIX_EXTENSIONS &
199 le64_to_cpu(pTcon->fsUnixInfo.Capability)))
Steve French737b7582005-04-28 22:41:06 -0700200 rc = CIFSSMBQFSPosixInfo(xid, pTcon, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /* Only need to call the old QFSInfo if failed
203 on newer one */
204 if(rc)
205#endif /* CIFS_EXPERIMENTAL */
Steve French737b7582005-04-28 22:41:06 -0700206 rc = CIFSSMBQFSInfo(xid, pTcon, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Steve French20962432005-09-21 22:05:57 -0700208 /* Old Windows servers do not support level 103, retry with level
209 one if old server failed the previous call */
210 if(rc)
211 rc = SMBOldQFSInfo(xid, pTcon, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /*
213 int f_type;
214 __fsid_t f_fsid;
215 int f_namelen; */
Steve Frenchc81156d2005-04-28 22:41:07 -0700216 /* BB get from info in tcon struct at mount time call to QFSAttrInfo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 FreeXid(xid);
Steve Frenchc81156d2005-04-28 22:41:07 -0700218 return 0; /* always return success? what if volume is no
219 longer available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222static int cifs_permission(struct inode * inode, int mask, struct nameidata *nd)
223{
224 struct cifs_sb_info *cifs_sb;
225
226 cifs_sb = CIFS_SB(inode->i_sb);
227
228 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
229 return 0;
230 } else /* file mode might have been restricted at mount time
231 on the client (above and beyond ACL on servers) for
232 servers which do not support setting and viewing mode bits,
233 so allowing client to check permissions is useful */
234 return generic_permission(inode, mask, NULL);
235}
236
237static kmem_cache_t *cifs_inode_cachep;
238static kmem_cache_t *cifs_req_cachep;
239static kmem_cache_t *cifs_mid_cachep;
240kmem_cache_t *cifs_oplock_cachep;
241static kmem_cache_t *cifs_sm_req_cachep;
242mempool_t *cifs_sm_req_poolp;
243mempool_t *cifs_req_poolp;
244mempool_t *cifs_mid_poolp;
245
246static struct inode *
247cifs_alloc_inode(struct super_block *sb)
248{
249 struct cifsInodeInfo *cifs_inode;
250 cifs_inode = kmem_cache_alloc(cifs_inode_cachep, SLAB_KERNEL);
251 if (!cifs_inode)
252 return NULL;
253 cifs_inode->cifsAttrs = 0x20; /* default */
254 atomic_set(&cifs_inode->inUse, 0);
255 cifs_inode->time = 0;
256 /* Until the file is open and we have gotten oplock
257 info back from the server, can not assume caching of
258 file data or metadata */
259 cifs_inode->clientCanCacheRead = FALSE;
260 cifs_inode->clientCanCacheAll = FALSE;
261 cifs_inode->vfs_inode.i_blksize = CIFS_MAX_MSGSIZE;
262 cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
Steve Frenche30dcf32005-09-20 20:49:16 -0700263 cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 INIT_LIST_HEAD(&cifs_inode->openFileList);
265 return &cifs_inode->vfs_inode;
266}
267
268static void
269cifs_destroy_inode(struct inode *inode)
270{
271 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
272}
273
274/*
275 * cifs_show_options() is for displaying mount options in /proc/mounts.
276 * Not all settable options are displayed but most of the important
277 * ones are.
278 */
279static int
280cifs_show_options(struct seq_file *s, struct vfsmount *m)
281{
282 struct cifs_sb_info *cifs_sb;
283
284 cifs_sb = CIFS_SB(m->mnt_sb);
285
286 if (cifs_sb) {
287 if (cifs_sb->tcon) {
288 seq_printf(s, ",unc=%s", cifs_sb->tcon->treeName);
289 if (cifs_sb->tcon->ses) {
290 if (cifs_sb->tcon->ses->userName)
291 seq_printf(s, ",username=%s",
292 cifs_sb->tcon->ses->userName);
293 if(cifs_sb->tcon->ses->domainName)
294 seq_printf(s, ",domain=%s",
295 cifs_sb->tcon->ses->domainName);
296 }
297 }
298 seq_printf(s, ",rsize=%d",cifs_sb->rsize);
299 seq_printf(s, ",wsize=%d",cifs_sb->wsize);
300 }
301 return 0;
302}
303
304#ifdef CONFIG_CIFS_QUOTA
305int cifs_xquota_set(struct super_block * sb, int quota_type, qid_t qid,
306 struct fs_disk_quota * pdquota)
307{
308 int xid;
309 int rc = 0;
310 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
311 struct cifsTconInfo *pTcon;
312
313 if(cifs_sb)
314 pTcon = cifs_sb->tcon;
315 else
316 return -EIO;
317
318
319 xid = GetXid();
320 if(pTcon) {
321 cFYI(1,("set type: 0x%x id: %d",quota_type,qid));
322 } else {
323 return -EIO;
324 }
325
326 FreeXid(xid);
327 return rc;
328}
329
330int cifs_xquota_get(struct super_block * sb, int quota_type, qid_t qid,
331 struct fs_disk_quota * pdquota)
332{
333 int xid;
334 int rc = 0;
335 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
336 struct cifsTconInfo *pTcon;
337
338 if(cifs_sb)
339 pTcon = cifs_sb->tcon;
340 else
341 return -EIO;
342
343 xid = GetXid();
344 if(pTcon) {
345 cFYI(1,("set type: 0x%x id: %d",quota_type,qid));
346 } else {
347 rc = -EIO;
348 }
349
350 FreeXid(xid);
351 return rc;
352}
353
354int cifs_xstate_set(struct super_block * sb, unsigned int flags, int operation)
355{
356 int xid;
357 int rc = 0;
358 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
359 struct cifsTconInfo *pTcon;
360
361 if(cifs_sb)
362 pTcon = cifs_sb->tcon;
363 else
364 return -EIO;
365
366 xid = GetXid();
367 if(pTcon) {
368 cFYI(1,("flags: 0x%x operation: 0x%x",flags,operation));
369 } else {
370 rc = -EIO;
371 }
372
373 FreeXid(xid);
374 return rc;
375}
376
377int cifs_xstate_get(struct super_block * sb, struct fs_quota_stat *qstats)
378{
379 int xid;
380 int rc = 0;
381 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
382 struct cifsTconInfo *pTcon;
383
384 if(cifs_sb) {
385 pTcon = cifs_sb->tcon;
386 } else {
387 return -EIO;
388 }
389 xid = GetXid();
390 if(pTcon) {
391 cFYI(1,("pqstats %p",qstats));
392 } else {
393 rc = -EIO;
394 }
395
396 FreeXid(xid);
397 return rc;
398}
399
400static struct quotactl_ops cifs_quotactl_ops = {
401 .set_xquota = cifs_xquota_set,
402 .get_xquota = cifs_xquota_set,
403 .set_xstate = cifs_xstate_set,
404 .get_xstate = cifs_xstate_get,
405};
406#endif
407
Steve French68058e72005-10-10 10:34:22 -0700408static void cifs_umount_begin(struct super_block * sblock)
409{
Steve French5e1253b2005-10-10 14:06:37 -0700410 struct cifs_sb_info *cifs_sb;
Steve French68058e72005-10-10 10:34:22 -0700411
Steve French5e1253b2005-10-10 14:06:37 -0700412 cifs_sb = CIFS_SB(sb);
413 if(cifs_sb == NULL)
414 return -EIO;
415 if(cifs_sb->tcon == NULL)
416 return -EIO;
417 down(&tcon->tconSem);
418 if (atomic_read(&tcon->useCount) == 1)
419 tcon->tidStatus = CifsExiting;
420 up(&tcon->tconSem);
421
422 if((cifs->sb->tcon->ses) && (cifs_sb->tcon->ses->server))
423 {
424 cERROR(1,("wake up tasks now - umount begin not complete"));
425 wake_up_all(&server->request_q);
426 }
427/* BB FIXME - finish add checks for tidStatus BB */
Steve French68058e72005-10-10 10:34:22 -0700428
429 return;
430}
431
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static int cifs_remount(struct super_block *sb, int *flags, char *data)
434{
435 *flags |= MS_NODIRATIME;
436 return 0;
437}
438
439struct super_operations cifs_super_ops = {
440 .read_inode = cifs_read_inode,
441 .put_super = cifs_put_super,
442 .statfs = cifs_statfs,
443 .alloc_inode = cifs_alloc_inode,
444 .destroy_inode = cifs_destroy_inode,
445/* .drop_inode = generic_delete_inode,
446 .delete_inode = cifs_delete_inode, *//* Do not need the above two functions
447 unless later we add lazy close of inodes or unless the kernel forgets to call
448 us with the same number of releases (closes) as opens */
449 .show_options = cifs_show_options,
Steve French68058e72005-10-10 10:34:22 -0700450/* .umount_begin = cifs_umount_begin, */ /* BB finish in the future */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 .remount_fs = cifs_remount,
452};
453
454static struct super_block *
455cifs_get_sb(struct file_system_type *fs_type,
456 int flags, const char *dev_name, void *data)
457{
458 int rc;
459 struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
460
461 cFYI(1, ("Devname: %s flags: %d ", dev_name, flags));
462
463 if (IS_ERR(sb))
464 return sb;
465
466 sb->s_flags = flags;
467
468 rc = cifs_read_super(sb, data, dev_name, flags & MS_VERBOSE ? 1 : 0);
469 if (rc) {
470 up_write(&sb->s_umount);
471 deactivate_super(sb);
472 return ERR_PTR(rc);
473 }
474 sb->s_flags |= MS_ACTIVE;
475 return sb;
476}
477
478static ssize_t
479cifs_read_wrapper(struct file * file, char __user *read_data, size_t read_size,
480 loff_t * poffset)
481{
482 if(file->f_dentry == NULL)
483 return -EIO;
484 else if(file->f_dentry->d_inode == NULL)
485 return -EIO;
486
487 cFYI(1,("In read_wrapper size %zd at %lld",read_size,*poffset));
488
489 if(CIFS_I(file->f_dentry->d_inode)->clientCanCacheRead) {
490 return generic_file_read(file,read_data,read_size,poffset);
491 } else {
492 /* BB do we need to lock inode from here until after invalidate? */
493/* if(file->f_dentry->d_inode->i_mapping) {
494 filemap_fdatawrite(file->f_dentry->d_inode->i_mapping);
495 filemap_fdatawait(file->f_dentry->d_inode->i_mapping);
496 }*/
497/* cifs_revalidate(file->f_dentry);*/ /* BB fixme */
498
499 /* BB we should make timer configurable - perhaps
500 by simply calling cifs_revalidate here */
501 /* invalidate_remote_inode(file->f_dentry->d_inode);*/
502 return generic_file_read(file,read_data,read_size,poffset);
503 }
504}
505
506static ssize_t
507cifs_write_wrapper(struct file * file, const char __user *write_data,
508 size_t write_size, loff_t * poffset)
509{
510 ssize_t written;
511
512 if(file->f_dentry == NULL)
513 return -EIO;
514 else if(file->f_dentry->d_inode == NULL)
515 return -EIO;
516
517 cFYI(1,("In write_wrapper size %zd at %lld",write_size,*poffset));
518
519 written = generic_file_write(file,write_data,write_size,poffset);
520 if(!CIFS_I(file->f_dentry->d_inode)->clientCanCacheAll) {
521 if(file->f_dentry->d_inode->i_mapping) {
522 filemap_fdatawrite(file->f_dentry->d_inode->i_mapping);
523 }
524 }
525 return written;
526}
527
528
529static struct file_system_type cifs_fs_type = {
530 .owner = THIS_MODULE,
531 .name = "cifs",
532 .get_sb = cifs_get_sb,
533 .kill_sb = kill_anon_super,
534 /* .fs_flags */
535};
536struct inode_operations cifs_dir_inode_ops = {
537 .create = cifs_create,
538 .lookup = cifs_lookup,
539 .getattr = cifs_getattr,
540 .unlink = cifs_unlink,
541 .link = cifs_hardlink,
542 .mkdir = cifs_mkdir,
543 .rmdir = cifs_rmdir,
544 .rename = cifs_rename,
545 .permission = cifs_permission,
546/* revalidate:cifs_revalidate, */
547 .setattr = cifs_setattr,
548 .symlink = cifs_symlink,
549 .mknod = cifs_mknod,
550#ifdef CONFIG_CIFS_XATTR
551 .setxattr = cifs_setxattr,
552 .getxattr = cifs_getxattr,
553 .listxattr = cifs_listxattr,
554 .removexattr = cifs_removexattr,
555#endif
556};
557
558struct inode_operations cifs_file_inode_ops = {
559/* revalidate:cifs_revalidate, */
560 .setattr = cifs_setattr,
561 .getattr = cifs_getattr, /* do we need this anymore? */
562 .rename = cifs_rename,
563 .permission = cifs_permission,
564#ifdef CONFIG_CIFS_XATTR
565 .setxattr = cifs_setxattr,
566 .getxattr = cifs_getxattr,
567 .listxattr = cifs_listxattr,
568 .removexattr = cifs_removexattr,
569#endif
570};
571
572struct inode_operations cifs_symlink_inode_ops = {
573 .readlink = generic_readlink,
574 .follow_link = cifs_follow_link,
575 .put_link = cifs_put_link,
576 .permission = cifs_permission,
577 /* BB add the following two eventually */
578 /* revalidate: cifs_revalidate,
579 setattr: cifs_notify_change, *//* BB do we need notify change */
580#ifdef CONFIG_CIFS_XATTR
581 .setxattr = cifs_setxattr,
582 .getxattr = cifs_getxattr,
583 .listxattr = cifs_listxattr,
584 .removexattr = cifs_removexattr,
585#endif
586};
587
588struct file_operations cifs_file_ops = {
589 .read = cifs_read_wrapper,
590 .write = cifs_write_wrapper,
591 .open = cifs_open,
592 .release = cifs_close,
593 .lock = cifs_lock,
594 .fsync = cifs_fsync,
595 .flush = cifs_flush,
596 .mmap = cifs_file_mmap,
597 .sendfile = generic_file_sendfile,
Steve Frenchc67593a2005-04-28 22:41:04 -0700598#ifdef CONFIG_CIFS_POSIX
599 .ioctl = cifs_ioctl,
600#endif /* CONFIG_CIFS_POSIX */
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602#ifdef CONFIG_CIFS_EXPERIMENTAL
603 .readv = generic_file_readv,
604 .writev = generic_file_writev,
605 .aio_read = generic_file_aio_read,
606 .aio_write = generic_file_aio_write,
607 .dir_notify = cifs_dir_notify,
608#endif /* CONFIG_CIFS_EXPERIMENTAL */
609};
610
611struct file_operations cifs_file_direct_ops = {
612 /* no mmap, no aio, no readv -
613 BB reevaluate whether they can be done with directio, no cache */
614 .read = cifs_user_read,
615 .write = cifs_user_write,
616 .open = cifs_open,
617 .release = cifs_close,
618 .lock = cifs_lock,
619 .fsync = cifs_fsync,
620 .flush = cifs_flush,
621 .sendfile = generic_file_sendfile, /* BB removeme BB */
Steve Frenchc67593a2005-04-28 22:41:04 -0700622#ifdef CONFIG_CIFS_POSIX
623 .ioctl = cifs_ioctl,
624#endif /* CONFIG_CIFS_POSIX */
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626#ifdef CONFIG_CIFS_EXPERIMENTAL
627 .dir_notify = cifs_dir_notify,
628#endif /* CONFIG_CIFS_EXPERIMENTAL */
629};
630
631struct file_operations cifs_dir_ops = {
632 .readdir = cifs_readdir,
633 .release = cifs_closedir,
634 .read = generic_read_dir,
635#ifdef CONFIG_CIFS_EXPERIMENTAL
636 .dir_notify = cifs_dir_notify,
637#endif /* CONFIG_CIFS_EXPERIMENTAL */
Steve Frenchf28ac912005-04-28 22:41:07 -0700638 .ioctl = cifs_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639};
640
641static void
642cifs_init_once(void *inode, kmem_cache_t * cachep, unsigned long flags)
643{
644 struct cifsInodeInfo *cifsi = inode;
645
646 if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
647 SLAB_CTOR_CONSTRUCTOR) {
648 inode_init_once(&cifsi->vfs_inode);
649 INIT_LIST_HEAD(&cifsi->lockList);
650 }
651}
652
653static int
654cifs_init_inodecache(void)
655{
656 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
657 sizeof (struct cifsInodeInfo),
658 0, SLAB_RECLAIM_ACCOUNT,
659 cifs_init_once, NULL);
660 if (cifs_inode_cachep == NULL)
661 return -ENOMEM;
662
663 return 0;
664}
665
666static void
667cifs_destroy_inodecache(void)
668{
669 if (kmem_cache_destroy(cifs_inode_cachep))
670 printk(KERN_WARNING "cifs_inode_cache: error freeing\n");
671}
672
673static int
674cifs_init_request_bufs(void)
675{
676 if(CIFSMaxBufSize < 8192) {
677 /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
678 Unicode path name has to fit in any SMB/CIFS path based frames */
679 CIFSMaxBufSize = 8192;
680 } else if (CIFSMaxBufSize > 1024*127) {
681 CIFSMaxBufSize = 1024 * 127;
682 } else {
683 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
684 }
685/* cERROR(1,("CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize)); */
686 cifs_req_cachep = kmem_cache_create("cifs_request",
687 CIFSMaxBufSize +
688 MAX_CIFS_HDR_SIZE, 0,
689 SLAB_HWCACHE_ALIGN, NULL, NULL);
690 if (cifs_req_cachep == NULL)
691 return -ENOMEM;
692
693 if(cifs_min_rcv < 1)
694 cifs_min_rcv = 1;
695 else if (cifs_min_rcv > 64) {
696 cifs_min_rcv = 64;
697 cERROR(1,("cifs_min_rcv set to maximum (64)"));
698 }
699
700 cifs_req_poolp = mempool_create(cifs_min_rcv,
701 mempool_alloc_slab,
702 mempool_free_slab,
703 cifs_req_cachep);
704
705 if(cifs_req_poolp == NULL) {
706 kmem_cache_destroy(cifs_req_cachep);
707 return -ENOMEM;
708 }
709 /* 256 (MAX_CIFS_HDR_SIZE bytes is enough for most SMB responses and
710 almost all handle based requests (but not write response, nor is it
711 sufficient for path based requests). A smaller size would have
712 been more efficient (compacting multiple slab items on one 4k page)
713 for the case in which debug was on, but this larger size allows
714 more SMBs to use small buffer alloc and is still much more
715 efficient to alloc 1 per page off the slab compared to 17K (5page)
716 alloc of large cifs buffers even when page debugging is on */
717 cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
718 MAX_CIFS_HDR_SIZE, 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
719 if (cifs_sm_req_cachep == NULL) {
720 mempool_destroy(cifs_req_poolp);
721 kmem_cache_destroy(cifs_req_cachep);
722 return -ENOMEM;
723 }
724
725 if(cifs_min_small < 2)
726 cifs_min_small = 2;
727 else if (cifs_min_small > 256) {
728 cifs_min_small = 256;
729 cFYI(1,("cifs_min_small set to maximum (256)"));
730 }
731
732 cifs_sm_req_poolp = mempool_create(cifs_min_small,
733 mempool_alloc_slab,
734 mempool_free_slab,
735 cifs_sm_req_cachep);
736
737 if(cifs_sm_req_poolp == NULL) {
738 mempool_destroy(cifs_req_poolp);
739 kmem_cache_destroy(cifs_req_cachep);
740 kmem_cache_destroy(cifs_sm_req_cachep);
741 return -ENOMEM;
742 }
743
744 return 0;
745}
746
747static void
748cifs_destroy_request_bufs(void)
749{
750 mempool_destroy(cifs_req_poolp);
751 if (kmem_cache_destroy(cifs_req_cachep))
752 printk(KERN_WARNING
753 "cifs_destroy_request_cache: error not all structures were freed\n");
754 mempool_destroy(cifs_sm_req_poolp);
755 if (kmem_cache_destroy(cifs_sm_req_cachep))
756 printk(KERN_WARNING
757 "cifs_destroy_request_cache: cifs_small_rq free error\n");
758}
759
760static int
761cifs_init_mids(void)
762{
763 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
764 sizeof (struct mid_q_entry), 0,
765 SLAB_HWCACHE_ALIGN, NULL, NULL);
766 if (cifs_mid_cachep == NULL)
767 return -ENOMEM;
768
769 cifs_mid_poolp = mempool_create(3 /* a reasonable min simultan opers */,
770 mempool_alloc_slab,
771 mempool_free_slab,
772 cifs_mid_cachep);
773 if(cifs_mid_poolp == NULL) {
774 kmem_cache_destroy(cifs_mid_cachep);
775 return -ENOMEM;
776 }
777
778 cifs_oplock_cachep = kmem_cache_create("cifs_oplock_structs",
779 sizeof (struct oplock_q_entry), 0,
780 SLAB_HWCACHE_ALIGN, NULL, NULL);
781 if (cifs_oplock_cachep == NULL) {
782 kmem_cache_destroy(cifs_mid_cachep);
783 mempool_destroy(cifs_mid_poolp);
784 return -ENOMEM;
785 }
786
787 return 0;
788}
789
790static void
791cifs_destroy_mids(void)
792{
793 mempool_destroy(cifs_mid_poolp);
794 if (kmem_cache_destroy(cifs_mid_cachep))
795 printk(KERN_WARNING
796 "cifs_destroy_mids: error not all structures were freed\n");
797
798 if (kmem_cache_destroy(cifs_oplock_cachep))
799 printk(KERN_WARNING
800 "error not all oplock structures were freed\n");
801}
802
803static int cifs_oplock_thread(void * dummyarg)
804{
805 struct oplock_q_entry * oplock_item;
806 struct cifsTconInfo *pTcon;
807 struct inode * inode;
808 __u16 netfid;
809 int rc;
810
811 daemonize("cifsoplockd");
812 allow_signal(SIGTERM);
813
814 oplockThread = current;
815 do {
Steve French16abbec2005-08-30 13:10:14 -0700816 if(try_to_freeze())
817 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 spin_lock(&GlobalMid_Lock);
820 if(list_empty(&GlobalOplock_Q)) {
821 spin_unlock(&GlobalMid_Lock);
822 set_current_state(TASK_INTERRUPTIBLE);
823 schedule_timeout(39*HZ);
824 } else {
825 oplock_item = list_entry(GlobalOplock_Q.next,
826 struct oplock_q_entry, qhead);
827 if(oplock_item) {
828 cFYI(1,("found oplock item to write out"));
829 pTcon = oplock_item->tcon;
830 inode = oplock_item->pinode;
831 netfid = oplock_item->netfid;
832 spin_unlock(&GlobalMid_Lock);
833 DeleteOplockQEntry(oplock_item);
834 /* can not grab inode sem here since it would
835 deadlock when oplock received on delete
836 since vfs_unlink holds the i_sem across
837 the call */
838 /* down(&inode->i_sem);*/
839 if (S_ISREG(inode->i_mode)) {
840 rc = filemap_fdatawrite(inode->i_mapping);
841 if(CIFS_I(inode)->clientCanCacheRead == 0) {
842 filemap_fdatawait(inode->i_mapping);
843 invalidate_remote_inode(inode);
844 }
845 } else
846 rc = 0;
847 /* up(&inode->i_sem);*/
848 if (rc)
849 CIFS_I(inode)->write_behind_rc = rc;
850 cFYI(1,("Oplock flush inode %p rc %d",inode,rc));
851
852 /* releasing a stale oplock after recent reconnection
853 of smb session using a now incorrect file
854 handle is not a data integrity issue but do
855 not bother sending an oplock release if session
856 to server still is disconnected since oplock
857 already released by the server in that case */
858 if(pTcon->tidStatus != CifsNeedReconnect) {
859 rc = CIFSSMBLock(0, pTcon, netfid,
860 0 /* len */ , 0 /* offset */, 0,
861 0, LOCKING_ANDX_OPLOCK_RELEASE,
862 0 /* wait flag */);
863 cFYI(1,("Oplock release rc = %d ",rc));
864 }
865 } else
866 spin_unlock(&GlobalMid_Lock);
Steve French68058e72005-10-10 10:34:22 -0700867 set_current_state(TASK_INTERRUPTIBLE);
868 schedule_timeout(1); /* yield in case q were corrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 } while(!signal_pending(current));
Steve French57337e42005-04-28 22:41:10 -0700871 oplockThread = NULL;
Steve Frenchf1914012005-08-18 09:37:34 -0700872 complete_and_exit (&cifs_oplock_exited, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Steve French8d0d5092005-08-18 09:41:43 -0700875static int cifs_dnotify_thread(void * dummyarg)
876{
877 daemonize("cifsdnotifyd");
878 allow_signal(SIGTERM);
879
880 dnotifyThread = current;
881 do {
Steve French16abbec2005-08-30 13:10:14 -0700882 if(try_to_freeze())
883 continue;
Steve French8d0d5092005-08-18 09:41:43 -0700884 set_current_state(TASK_INTERRUPTIBLE);
885 schedule_timeout(39*HZ);
886 } while(!signal_pending(current));
887 complete_and_exit (&cifs_dnotify_exited, 0);
888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890static int __init
891init_cifs(void)
892{
893 int rc = 0;
894#ifdef CONFIG_PROC_FS
895 cifs_proc_init();
896#endif
897 INIT_LIST_HEAD(&GlobalServerList); /* BB not implemented yet */
898 INIT_LIST_HEAD(&GlobalSMBSessionList);
899 INIT_LIST_HEAD(&GlobalTreeConnectionList);
900 INIT_LIST_HEAD(&GlobalOplock_Q);
901/*
902 * Initialize Global counters
903 */
904 atomic_set(&sesInfoAllocCount, 0);
905 atomic_set(&tconInfoAllocCount, 0);
906 atomic_set(&tcpSesAllocCount,0);
907 atomic_set(&tcpSesReconnectCount, 0);
908 atomic_set(&tconInfoReconnectCount, 0);
909
910 atomic_set(&bufAllocCount, 0);
911 atomic_set(&midCount, 0);
912 GlobalCurrentXid = 0;
913 GlobalTotalActiveXid = 0;
914 GlobalMaxActiveXid = 0;
915 rwlock_init(&GlobalSMBSeslock);
916 spin_lock_init(&GlobalMid_Lock);
917
918 if(cifs_max_pending < 2) {
919 cifs_max_pending = 2;
920 cFYI(1,("cifs_max_pending set to min of 2"));
921 } else if(cifs_max_pending > 256) {
922 cifs_max_pending = 256;
923 cFYI(1,("cifs_max_pending set to max of 256"));
924 }
925
926 rc = cifs_init_inodecache();
927 if (!rc) {
928 rc = cifs_init_mids();
929 if (!rc) {
930 rc = cifs_init_request_bufs();
931 if (!rc) {
932 rc = register_filesystem(&cifs_fs_type);
933 if (!rc) {
934 rc = (int)kernel_thread(cifs_oplock_thread, NULL,
935 CLONE_FS | CLONE_FILES | CLONE_VM);
Steve French8d0d5092005-08-18 09:41:43 -0700936 if(rc > 0) {
937 rc = (int)kernel_thread(cifs_dnotify_thread, NULL,
938 CLONE_FS | CLONE_FILES | CLONE_VM);
939 if(rc > 0)
940 return 0;
941 else
942 cERROR(1,("error %d create dnotify thread", rc));
943 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 cERROR(1,("error %d create oplock thread",rc));
Steve French8d0d5092005-08-18 09:41:43 -0700945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 cifs_destroy_request_bufs();
948 }
949 cifs_destroy_mids();
950 }
951 cifs_destroy_inodecache();
952 }
953#ifdef CONFIG_PROC_FS
954 cifs_proc_clean();
955#endif
956 return rc;
957}
958
959static void __exit
960exit_cifs(void)
961{
962 cFYI(0, ("In unregister ie exit_cifs"));
963#ifdef CONFIG_PROC_FS
964 cifs_proc_clean();
965#endif
966 unregister_filesystem(&cifs_fs_type);
967 cifs_destroy_inodecache();
968 cifs_destroy_mids();
969 cifs_destroy_request_bufs();
970 if(oplockThread) {
971 send_sig(SIGTERM, oplockThread, 1);
972 wait_for_completion(&cifs_oplock_exited);
973 }
Steve French8d0d5092005-08-18 09:41:43 -0700974 if(dnotifyThread) {
975 send_sig(SIGTERM, dnotifyThread, 1);
976 wait_for_completion(&cifs_dnotify_exited);
977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
980MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
981MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
982MODULE_DESCRIPTION
983 ("VFS to access servers complying with the SNIA CIFS Specification e.g. Samba and Windows");
984MODULE_VERSION(CIFS_VERSION);
985module_init(init_cifs)
986module_exit(exit_cifs)