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