Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4
[pandora-kernel.git] / fs / cifs / cifsfs.c
1 /*
2  *   fs/cifs/cifsfs.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2008
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 #include "dns_resolve.h"
48 #include "cifs_spnego.h"
49 #define CIFS_MAGIC_NUMBER 0xFF534D42    /* the first four bytes of SMB PDUs */
50
51 #ifdef CONFIG_CIFS_QUOTA
52 static struct quotactl_ops cifs_quotactl_ops;
53 #endif /* QUOTA */
54
55 int cifsFYI = 0;
56 int cifsERROR = 1;
57 int traceSMB = 0;
58 unsigned int oplockEnabled = 1;
59 unsigned int experimEnabled = 0;
60 unsigned int linuxExtEnabled = 1;
61 unsigned int lookupCacheEnabled = 1;
62 unsigned int multiuser_mount = 0;
63 unsigned int extended_security = CIFSSEC_DEF;
64 /* unsigned int ntlmv2_support = 0; */
65 unsigned int sign_CIFS_PDUs = 1;
66 extern struct task_struct *oplockThread; /* remove sparse warning */
67 struct task_struct *oplockThread = NULL;
68 /* extern struct task_struct * dnotifyThread; remove sparse warning */
69 static const struct super_operations cifs_super_ops;
70 unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
71 module_param(CIFSMaxBufSize, int, 0);
72 MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header). "
73                                  "Default: 16384 Range: 8192 to 130048");
74 unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
75 module_param(cifs_min_rcv, int, 0);
76 MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
77                                 "1 to 64");
78 unsigned int cifs_min_small = 30;
79 module_param(cifs_min_small, int, 0);
80 MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
81                                  "Range: 2 to 256");
82 unsigned int cifs_max_pending = CIFS_MAX_REQ;
83 module_param(cifs_max_pending, int, 0);
84 MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server. "
85                                    "Default: 50 Range: 2 to 256");
86
87 extern mempool_t *cifs_sm_req_poolp;
88 extern mempool_t *cifs_req_poolp;
89 extern mempool_t *cifs_mid_poolp;
90
91 extern struct kmem_cache *cifs_oplock_cachep;
92
93 static int
94 cifs_read_super(struct super_block *sb, void *data,
95                 const char *devname, int silent)
96 {
97         struct inode *inode;
98         struct cifs_sb_info *cifs_sb;
99         int rc = 0;
100
101         /* BB should we make this contingent on mount parm? */
102         sb->s_flags |= MS_NODIRATIME | MS_NOATIME;
103         sb->s_fs_info = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
104         cifs_sb = CIFS_SB(sb);
105         if (cifs_sb == NULL)
106                 return -ENOMEM;
107
108 #ifdef CONFIG_CIFS_DFS_UPCALL
109         /* copy mount params to sb for use in submounts */
110         /* BB: should we move this after the mount so we
111          * do not have to do the copy on failed mounts?
112          * BB: May be it is better to do simple copy before
113          * complex operation (mount), and in case of fail
114          * just exit instead of doing mount and attempting
115          * undo it if this copy fails?*/
116         if (data) {
117                 int len = strlen(data);
118                 cifs_sb->mountdata = kzalloc(len + 1, GFP_KERNEL);
119                 if (cifs_sb->mountdata == NULL) {
120                         kfree(sb->s_fs_info);
121                         sb->s_fs_info = NULL;
122                         return -ENOMEM;
123                 }
124                 strncpy(cifs_sb->mountdata, data, len + 1);
125                 cifs_sb->mountdata[len] = '\0';
126         }
127 #endif
128
129         rc = cifs_mount(sb, cifs_sb, data, devname);
130
131         if (rc) {
132                 if (!silent)
133                         cERROR(1,
134                                ("cifs_mount failed w/return code = %d", rc));
135                 goto out_mount_failed;
136         }
137
138         sb->s_magic = CIFS_MAGIC_NUMBER;
139         sb->s_op = &cifs_super_ops;
140 /*      if (cifs_sb->tcon->ses->server->maxBuf > MAX_CIFS_HDR_SIZE + 512)
141             sb->s_blocksize =
142                 cifs_sb->tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE; */
143 #ifdef CONFIG_CIFS_QUOTA
144         sb->s_qcop = &cifs_quotactl_ops;
145 #endif
146         sb->s_blocksize = CIFS_MAX_MSGSIZE;
147         sb->s_blocksize_bits = 14;      /* default 2**14 = CIFS_MAX_MSGSIZE */
148         inode = cifs_iget(sb, ROOT_I);
149
150         if (IS_ERR(inode)) {
151                 rc = PTR_ERR(inode);
152                 inode = NULL;
153                 goto out_no_root;
154         }
155
156         sb->s_root = d_alloc_root(inode);
157
158         if (!sb->s_root) {
159                 rc = -ENOMEM;
160                 goto out_no_root;
161         }
162
163 #ifdef CONFIG_CIFS_EXPERIMENTAL
164         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
165                 cFYI(1, ("export ops supported"));
166                 sb->s_export_op = &cifs_export_ops;
167         }
168 #endif /* EXPERIMENTAL */
169
170         return 0;
171
172 out_no_root:
173         cERROR(1, ("cifs_read_super: get root inode failed"));
174         if (inode)
175                 iput(inode);
176
177         cifs_umount(sb, cifs_sb);
178
179 out_mount_failed:
180         if (cifs_sb) {
181 #ifdef CONFIG_CIFS_DFS_UPCALL
182                 if (cifs_sb->mountdata) {
183                         kfree(cifs_sb->mountdata);
184                         cifs_sb->mountdata = NULL;
185                 }
186 #endif
187                 if (cifs_sb->local_nls)
188                         unload_nls(cifs_sb->local_nls);
189                 kfree(cifs_sb);
190         }
191         return rc;
192 }
193
194 static void
195 cifs_put_super(struct super_block *sb)
196 {
197         int rc = 0;
198         struct cifs_sb_info *cifs_sb;
199
200         cFYI(1, ("In cifs_put_super"));
201         cifs_sb = CIFS_SB(sb);
202         if (cifs_sb == NULL) {
203                 cFYI(1, ("Empty cifs superblock info passed to unmount"));
204                 return;
205         }
206         rc = cifs_umount(sb, cifs_sb);
207         if (rc)
208                 cERROR(1, ("cifs_umount failed with return code %d", rc));
209 #ifdef CONFIG_CIFS_DFS_UPCALL
210         if (cifs_sb->mountdata) {
211                 kfree(cifs_sb->mountdata);
212                 cifs_sb->mountdata = NULL;
213         }
214 #endif
215
216         unload_nls(cifs_sb->local_nls);
217         kfree(cifs_sb);
218         return;
219 }
220
221 static int
222 cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
223 {
224         struct super_block *sb = dentry->d_sb;
225         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
226         struct cifsTconInfo *tcon = cifs_sb->tcon;
227         int rc = -EOPNOTSUPP;
228         int xid;
229
230         xid = GetXid();
231
232         buf->f_type = CIFS_MAGIC_NUMBER;
233
234         /*
235          * PATH_MAX may be too long - it would presumably be total path,
236          * but note that some servers (includinng Samba 3) have a shorter
237          * maximum path.
238          *
239          * Instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO.
240          */
241         buf->f_namelen = PATH_MAX;
242         buf->f_files = 0;       /* undefined */
243         buf->f_ffree = 0;       /* unlimited */
244
245         /*
246          * We could add a second check for a QFS Unix capability bit
247          */
248         if ((tcon->ses->capabilities & CAP_UNIX) &&
249             (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
250                 rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
251
252         /*
253          * Only need to call the old QFSInfo if failed on newer one,
254          * e.g. by OS/2.
255          **/
256         if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
257                 rc = CIFSSMBQFSInfo(xid, tcon, buf);
258
259         /*
260          * Some old Windows servers also do not support level 103, retry with
261          * older level one if old server failed the previous call or we
262          * bypassed it because we detected that this was an older LANMAN sess
263          */
264         if (rc)
265                 rc = SMBOldQFSInfo(xid, tcon, buf);
266
267         FreeXid(xid);
268         return 0;
269 }
270
271 static int cifs_permission(struct inode *inode, int mask)
272 {
273         struct cifs_sb_info *cifs_sb;
274
275         cifs_sb = CIFS_SB(inode->i_sb);
276
277         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
278                 if ((mask & MAY_EXEC) && !execute_ok(inode))
279                         return -EACCES;
280                 else
281                         return 0;
282         } else /* file mode might have been restricted at mount time
283                 on the client (above and beyond ACL on servers) for
284                 servers which do not support setting and viewing mode bits,
285                 so allowing client to check permissions is useful */
286                 return generic_permission(inode, mask, NULL);
287 }
288
289 static struct kmem_cache *cifs_inode_cachep;
290 static struct kmem_cache *cifs_req_cachep;
291 static struct kmem_cache *cifs_mid_cachep;
292 struct kmem_cache *cifs_oplock_cachep;
293 static struct kmem_cache *cifs_sm_req_cachep;
294 mempool_t *cifs_sm_req_poolp;
295 mempool_t *cifs_req_poolp;
296 mempool_t *cifs_mid_poolp;
297
298 static struct inode *
299 cifs_alloc_inode(struct super_block *sb)
300 {
301         struct cifsInodeInfo *cifs_inode;
302         cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
303         if (!cifs_inode)
304                 return NULL;
305         cifs_inode->cifsAttrs = 0x20;   /* default */
306         atomic_set(&cifs_inode->inUse, 0);
307         cifs_inode->time = 0;
308         cifs_inode->write_behind_rc = 0;
309         /* Until the file is open and we have gotten oplock
310         info back from the server, can not assume caching of
311         file data or metadata */
312         cifs_inode->clientCanCacheRead = false;
313         cifs_inode->clientCanCacheAll = false;
314         cifs_inode->delete_pending = false;
315         cifs_inode->vfs_inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
316         cifs_inode->server_eof = 0;
317
318         /* Can not set i_flags here - they get immediately overwritten
319            to zero by the VFS */
320 /*      cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;*/
321         INIT_LIST_HEAD(&cifs_inode->openFileList);
322         return &cifs_inode->vfs_inode;
323 }
324
325 static void
326 cifs_destroy_inode(struct inode *inode)
327 {
328         kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
329 }
330
331 /*
332  * cifs_show_options() is for displaying mount options in /proc/mounts.
333  * Not all settable options are displayed but most of the important
334  * ones are.
335  */
336 static int
337 cifs_show_options(struct seq_file *s, struct vfsmount *m)
338 {
339         struct cifs_sb_info *cifs_sb;
340         struct cifsTconInfo *tcon;
341         struct TCP_Server_Info *server;
342
343         cifs_sb = CIFS_SB(m->mnt_sb);
344
345         if (cifs_sb) {
346                 tcon = cifs_sb->tcon;
347                 if (tcon) {
348                         seq_printf(s, ",unc=%s", cifs_sb->tcon->treeName);
349                         if (tcon->ses) {
350                                 if (tcon->ses->userName)
351                                         seq_printf(s, ",username=%s",
352                                            tcon->ses->userName);
353                                 if (tcon->ses->domainName)
354                                         seq_printf(s, ",domain=%s",
355                                            tcon->ses->domainName);
356                                 server = tcon->ses->server;
357                                 if (server) {
358                                         seq_printf(s, ",addr=");
359                                         switch (server->addr.sockAddr6.
360                                                 sin6_family) {
361                                         case AF_INET6:
362                                                 seq_printf(s, "%pI6",
363                                                            &server->addr.sockAddr6.sin6_addr);
364                                                 break;
365                                         case AF_INET:
366                                                 seq_printf(s, "%pI4",
367                                                            &server->addr.sockAddr.sin_addr.s_addr);
368                                                 break;
369                                         }
370                                 }
371                         }
372                         if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) ||
373                            !(tcon->unix_ext))
374                                 seq_printf(s, ",uid=%d", cifs_sb->mnt_uid);
375                         if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) ||
376                            !(tcon->unix_ext))
377                                 seq_printf(s, ",gid=%d", cifs_sb->mnt_gid);
378                         if (!tcon->unix_ext) {
379                                 seq_printf(s, ",file_mode=0%o,dir_mode=0%o",
380                                            cifs_sb->mnt_file_mode,
381                                            cifs_sb->mnt_dir_mode);
382                         }
383                         if (tcon->seal)
384                                 seq_printf(s, ",seal");
385                         if (tcon->nocase)
386                                 seq_printf(s, ",nocase");
387                         if (tcon->retry)
388                                 seq_printf(s, ",hard");
389                 }
390                 if (cifs_sb->prepath)
391                         seq_printf(s, ",prepath=%s", cifs_sb->prepath);
392                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
393                         seq_printf(s, ",posixpaths");
394                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
395                         seq_printf(s, ",setuids");
396                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
397                         seq_printf(s, ",serverino");
398                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
399                         seq_printf(s, ",directio");
400                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
401                         seq_printf(s, ",nouser_xattr");
402                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
403                         seq_printf(s, ",mapchars");
404                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
405                         seq_printf(s, ",sfu");
406                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
407                         seq_printf(s, ",nobrl");
408                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
409                         seq_printf(s, ",cifsacl");
410                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
411                         seq_printf(s, ",dynperm");
412                 if (m->mnt_sb->s_flags & MS_POSIXACL)
413                         seq_printf(s, ",acl");
414
415                 seq_printf(s, ",rsize=%d", cifs_sb->rsize);
416                 seq_printf(s, ",wsize=%d", cifs_sb->wsize);
417         }
418         return 0;
419 }
420
421 #ifdef CONFIG_CIFS_QUOTA
422 int cifs_xquota_set(struct super_block *sb, int quota_type, qid_t qid,
423                 struct fs_disk_quota *pdquota)
424 {
425         int xid;
426         int rc = 0;
427         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
428         struct cifsTconInfo *pTcon;
429
430         if (cifs_sb)
431                 pTcon = cifs_sb->tcon;
432         else
433                 return -EIO;
434
435
436         xid = GetXid();
437         if (pTcon) {
438                 cFYI(1, ("set type: 0x%x id: %d", quota_type, qid));
439         } else
440                 rc = -EIO;
441
442         FreeXid(xid);
443         return rc;
444 }
445
446 int cifs_xquota_get(struct super_block *sb, int quota_type, qid_t qid,
447                     struct fs_disk_quota *pdquota)
448 {
449         int xid;
450         int rc = 0;
451         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
452         struct cifsTconInfo *pTcon;
453
454         if (cifs_sb)
455                 pTcon = cifs_sb->tcon;
456         else
457                 return -EIO;
458
459         xid = GetXid();
460         if (pTcon) {
461                 cFYI(1, ("set type: 0x%x id: %d", quota_type, qid));
462         } else
463                 rc = -EIO;
464
465         FreeXid(xid);
466         return rc;
467 }
468
469 int cifs_xstate_set(struct super_block *sb, unsigned int flags, int operation)
470 {
471         int xid;
472         int rc = 0;
473         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
474         struct cifsTconInfo *pTcon;
475
476         if (cifs_sb)
477                 pTcon = cifs_sb->tcon;
478         else
479                 return -EIO;
480
481         xid = GetXid();
482         if (pTcon) {
483                 cFYI(1, ("flags: 0x%x operation: 0x%x", flags, operation));
484         } else
485                 rc = -EIO;
486
487         FreeXid(xid);
488         return rc;
489 }
490
491 int cifs_xstate_get(struct super_block *sb, struct fs_quota_stat *qstats)
492 {
493         int xid;
494         int rc = 0;
495         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
496         struct cifsTconInfo *pTcon;
497
498         if (cifs_sb)
499                 pTcon = cifs_sb->tcon;
500         else
501                 return -EIO;
502
503         xid = GetXid();
504         if (pTcon) {
505                 cFYI(1, ("pqstats %p", qstats));
506         } else
507                 rc = -EIO;
508
509         FreeXid(xid);
510         return rc;
511 }
512
513 static struct quotactl_ops cifs_quotactl_ops = {
514         .set_xquota     = cifs_xquota_set,
515         .get_xquota     = cifs_xquota_get,
516         .set_xstate     = cifs_xstate_set,
517         .get_xstate     = cifs_xstate_get,
518 };
519 #endif
520
521 static void cifs_umount_begin(struct super_block *sb)
522 {
523         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
524         struct cifsTconInfo *tcon;
525
526         if (cifs_sb == NULL)
527                 return;
528
529         tcon = cifs_sb->tcon;
530         if (tcon == NULL)
531                 return;
532
533         read_lock(&cifs_tcp_ses_lock);
534         if (tcon->tc_count == 1)
535                 tcon->tidStatus = CifsExiting;
536         read_unlock(&cifs_tcp_ses_lock);
537
538         /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
539         /* cancel_notify_requests(tcon); */
540         if (tcon->ses && tcon->ses->server) {
541                 cFYI(1, ("wake up tasks now - umount begin not complete"));
542                 wake_up_all(&tcon->ses->server->request_q);
543                 wake_up_all(&tcon->ses->server->response_q);
544                 msleep(1); /* yield */
545                 /* we have to kick the requests once more */
546                 wake_up_all(&tcon->ses->server->response_q);
547                 msleep(1);
548         }
549 /* BB FIXME - finish add checks for tidStatus BB */
550
551         return;
552 }
553
554 #ifdef CONFIG_CIFS_STATS2
555 static int cifs_show_stats(struct seq_file *s, struct vfsmount *mnt)
556 {
557         /* BB FIXME */
558         return 0;
559 }
560 #endif
561
562 static int cifs_remount(struct super_block *sb, int *flags, char *data)
563 {
564         *flags |= MS_NODIRATIME;
565         return 0;
566 }
567
568 static const struct super_operations cifs_super_ops = {
569         .put_super = cifs_put_super,
570         .statfs = cifs_statfs,
571         .alloc_inode = cifs_alloc_inode,
572         .destroy_inode = cifs_destroy_inode,
573 /*      .drop_inode         = generic_delete_inode,
574         .delete_inode   = cifs_delete_inode,  */  /* Do not need above two
575         functions unless later we add lazy close of inodes or unless the
576         kernel forgets to call us with the same number of releases (closes)
577         as opens */
578         .show_options = cifs_show_options,
579         .umount_begin   = cifs_umount_begin,
580         .remount_fs = cifs_remount,
581 #ifdef CONFIG_CIFS_STATS2
582         .show_stats = cifs_show_stats,
583 #endif
584 };
585
586 static int
587 cifs_get_sb(struct file_system_type *fs_type,
588             int flags, const char *dev_name, void *data, struct vfsmount *mnt)
589 {
590         int rc;
591         struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
592
593         cFYI(1, ("Devname: %s flags: %d ", dev_name, flags));
594
595         if (IS_ERR(sb))
596                 return PTR_ERR(sb);
597
598         sb->s_flags = flags;
599
600         rc = cifs_read_super(sb, data, dev_name, flags & MS_SILENT ? 1 : 0);
601         if (rc) {
602                 up_write(&sb->s_umount);
603                 deactivate_super(sb);
604                 return rc;
605         }
606         sb->s_flags |= MS_ACTIVE;
607         simple_set_mnt(mnt, sb);
608         return 0;
609 }
610
611 static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
612                                    unsigned long nr_segs, loff_t pos)
613 {
614         struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
615         ssize_t written;
616
617         written = generic_file_aio_write(iocb, iov, nr_segs, pos);
618         if (!CIFS_I(inode)->clientCanCacheAll)
619                 filemap_fdatawrite(inode->i_mapping);
620         return written;
621 }
622
623 static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
624 {
625         /* origin == SEEK_END => we must revalidate the cached file length */
626         if (origin == SEEK_END) {
627                 int retval;
628
629                 /* some applications poll for the file length in this strange
630                    way so we must seek to end on non-oplocked files by
631                    setting the revalidate time to zero */
632                 CIFS_I(file->f_path.dentry->d_inode)->time = 0;
633
634                 retval = cifs_revalidate(file->f_path.dentry);
635                 if (retval < 0)
636                         return (loff_t)retval;
637         }
638         return generic_file_llseek_unlocked(file, offset, origin);
639 }
640
641 #ifdef CONFIG_CIFS_EXPERIMENTAL
642 static int cifs_setlease(struct file *file, long arg, struct file_lock **lease)
643 {
644         /* note that this is called by vfs setlease with the BKL held
645            although I doubt that BKL is needed here in cifs */
646         struct inode *inode = file->f_path.dentry->d_inode;
647
648         if (!(S_ISREG(inode->i_mode)))
649                 return -EINVAL;
650
651         /* check if file is oplocked */
652         if (((arg == F_RDLCK) &&
653                 (CIFS_I(inode)->clientCanCacheRead)) ||
654             ((arg == F_WRLCK) &&
655                 (CIFS_I(inode)->clientCanCacheAll)))
656                 return generic_setlease(file, arg, lease);
657         else if (CIFS_SB(inode->i_sb)->tcon->local_lease &&
658                         !CIFS_I(inode)->clientCanCacheRead)
659                 /* If the server claims to support oplock on this
660                    file, then we still need to check oplock even
661                    if the local_lease mount option is set, but there
662                    are servers which do not support oplock for which
663                    this mount option may be useful if the user
664                    knows that the file won't be changed on the server
665                    by anyone else */
666                 return generic_setlease(file, arg, lease);
667         else
668                 return -EAGAIN;
669 }
670 #endif
671
672 struct file_system_type cifs_fs_type = {
673         .owner = THIS_MODULE,
674         .name = "cifs",
675         .get_sb = cifs_get_sb,
676         .kill_sb = kill_anon_super,
677         /*  .fs_flags */
678 };
679 const struct inode_operations cifs_dir_inode_ops = {
680         .create = cifs_create,
681         .lookup = cifs_lookup,
682         .getattr = cifs_getattr,
683         .unlink = cifs_unlink,
684         .link = cifs_hardlink,
685         .mkdir = cifs_mkdir,
686         .rmdir = cifs_rmdir,
687         .rename = cifs_rename,
688         .permission = cifs_permission,
689 /*      revalidate:cifs_revalidate,   */
690         .setattr = cifs_setattr,
691         .symlink = cifs_symlink,
692         .mknod   = cifs_mknod,
693 #ifdef CONFIG_CIFS_XATTR
694         .setxattr = cifs_setxattr,
695         .getxattr = cifs_getxattr,
696         .listxattr = cifs_listxattr,
697         .removexattr = cifs_removexattr,
698 #endif
699 };
700
701 const struct inode_operations cifs_file_inode_ops = {
702 /*      revalidate:cifs_revalidate, */
703         .setattr = cifs_setattr,
704         .getattr = cifs_getattr, /* do we need this anymore? */
705         .rename = cifs_rename,
706         .permission = cifs_permission,
707 #ifdef CONFIG_CIFS_XATTR
708         .setxattr = cifs_setxattr,
709         .getxattr = cifs_getxattr,
710         .listxattr = cifs_listxattr,
711         .removexattr = cifs_removexattr,
712 #endif
713 };
714
715 const struct inode_operations cifs_symlink_inode_ops = {
716         .readlink = generic_readlink,
717         .follow_link = cifs_follow_link,
718         .put_link = cifs_put_link,
719         .permission = cifs_permission,
720         /* BB add the following two eventually */
721         /* revalidate: cifs_revalidate,
722            setattr:    cifs_notify_change, *//* BB do we need notify change */
723 #ifdef CONFIG_CIFS_XATTR
724         .setxattr = cifs_setxattr,
725         .getxattr = cifs_getxattr,
726         .listxattr = cifs_listxattr,
727         .removexattr = cifs_removexattr,
728 #endif
729 };
730
731 const struct file_operations cifs_file_ops = {
732         .read = do_sync_read,
733         .write = do_sync_write,
734         .aio_read = generic_file_aio_read,
735         .aio_write = cifs_file_aio_write,
736         .open = cifs_open,
737         .release = cifs_close,
738         .lock = cifs_lock,
739         .fsync = cifs_fsync,
740         .flush = cifs_flush,
741         .mmap  = cifs_file_mmap,
742         .splice_read = generic_file_splice_read,
743         .llseek = cifs_llseek,
744 #ifdef CONFIG_CIFS_POSIX
745         .unlocked_ioctl = cifs_ioctl,
746 #endif /* CONFIG_CIFS_POSIX */
747
748 #ifdef CONFIG_CIFS_EXPERIMENTAL
749         .setlease = cifs_setlease,
750 #endif /* CONFIG_CIFS_EXPERIMENTAL */
751 };
752
753 const struct file_operations cifs_file_direct_ops = {
754         /* no mmap, no aio, no readv -
755            BB reevaluate whether they can be done with directio, no cache */
756         .read = cifs_user_read,
757         .write = cifs_user_write,
758         .open = cifs_open,
759         .release = cifs_close,
760         .lock = cifs_lock,
761         .fsync = cifs_fsync,
762         .flush = cifs_flush,
763         .splice_read = generic_file_splice_read,
764 #ifdef CONFIG_CIFS_POSIX
765         .unlocked_ioctl  = cifs_ioctl,
766 #endif /* CONFIG_CIFS_POSIX */
767         .llseek = cifs_llseek,
768 #ifdef CONFIG_CIFS_EXPERIMENTAL
769         .setlease = cifs_setlease,
770 #endif /* CONFIG_CIFS_EXPERIMENTAL */
771 };
772 const struct file_operations cifs_file_nobrl_ops = {
773         .read = do_sync_read,
774         .write = do_sync_write,
775         .aio_read = generic_file_aio_read,
776         .aio_write = cifs_file_aio_write,
777         .open = cifs_open,
778         .release = cifs_close,
779         .fsync = cifs_fsync,
780         .flush = cifs_flush,
781         .mmap  = cifs_file_mmap,
782         .splice_read = generic_file_splice_read,
783         .llseek = cifs_llseek,
784 #ifdef CONFIG_CIFS_POSIX
785         .unlocked_ioctl = cifs_ioctl,
786 #endif /* CONFIG_CIFS_POSIX */
787
788 #ifdef CONFIG_CIFS_EXPERIMENTAL
789         .setlease = cifs_setlease,
790 #endif /* CONFIG_CIFS_EXPERIMENTAL */
791 };
792
793 const struct file_operations cifs_file_direct_nobrl_ops = {
794         /* no mmap, no aio, no readv -
795            BB reevaluate whether they can be done with directio, no cache */
796         .read = cifs_user_read,
797         .write = cifs_user_write,
798         .open = cifs_open,
799         .release = cifs_close,
800         .fsync = cifs_fsync,
801         .flush = cifs_flush,
802         .splice_read = generic_file_splice_read,
803 #ifdef CONFIG_CIFS_POSIX
804         .unlocked_ioctl  = cifs_ioctl,
805 #endif /* CONFIG_CIFS_POSIX */
806         .llseek = cifs_llseek,
807 #ifdef CONFIG_CIFS_EXPERIMENTAL
808         .setlease = cifs_setlease,
809 #endif /* CONFIG_CIFS_EXPERIMENTAL */
810 };
811
812 const struct file_operations cifs_dir_ops = {
813         .readdir = cifs_readdir,
814         .release = cifs_closedir,
815         .read    = generic_read_dir,
816         .unlocked_ioctl  = cifs_ioctl,
817         .llseek = generic_file_llseek,
818 };
819
820 static void
821 cifs_init_once(void *inode)
822 {
823         struct cifsInodeInfo *cifsi = inode;
824
825         inode_init_once(&cifsi->vfs_inode);
826         INIT_LIST_HEAD(&cifsi->lockList);
827 }
828
829 static int
830 cifs_init_inodecache(void)
831 {
832         cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
833                                               sizeof(struct cifsInodeInfo),
834                                               0, (SLAB_RECLAIM_ACCOUNT|
835                                                 SLAB_MEM_SPREAD),
836                                               cifs_init_once);
837         if (cifs_inode_cachep == NULL)
838                 return -ENOMEM;
839
840         return 0;
841 }
842
843 static void
844 cifs_destroy_inodecache(void)
845 {
846         kmem_cache_destroy(cifs_inode_cachep);
847 }
848
849 static int
850 cifs_init_request_bufs(void)
851 {
852         if (CIFSMaxBufSize < 8192) {
853         /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
854         Unicode path name has to fit in any SMB/CIFS path based frames */
855                 CIFSMaxBufSize = 8192;
856         } else if (CIFSMaxBufSize > 1024*127) {
857                 CIFSMaxBufSize = 1024 * 127;
858         } else {
859                 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
860         }
861 /*      cERROR(1,("CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize)); */
862         cifs_req_cachep = kmem_cache_create("cifs_request",
863                                             CIFSMaxBufSize +
864                                             MAX_CIFS_HDR_SIZE, 0,
865                                             SLAB_HWCACHE_ALIGN, NULL);
866         if (cifs_req_cachep == NULL)
867                 return -ENOMEM;
868
869         if (cifs_min_rcv < 1)
870                 cifs_min_rcv = 1;
871         else if (cifs_min_rcv > 64) {
872                 cifs_min_rcv = 64;
873                 cERROR(1, ("cifs_min_rcv set to maximum (64)"));
874         }
875
876         cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
877                                                   cifs_req_cachep);
878
879         if (cifs_req_poolp == NULL) {
880                 kmem_cache_destroy(cifs_req_cachep);
881                 return -ENOMEM;
882         }
883         /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
884         almost all handle based requests (but not write response, nor is it
885         sufficient for path based requests).  A smaller size would have
886         been more efficient (compacting multiple slab items on one 4k page)
887         for the case in which debug was on, but this larger size allows
888         more SMBs to use small buffer alloc and is still much more
889         efficient to alloc 1 per page off the slab compared to 17K (5page)
890         alloc of large cifs buffers even when page debugging is on */
891         cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
892                         MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
893                         NULL);
894         if (cifs_sm_req_cachep == NULL) {
895                 mempool_destroy(cifs_req_poolp);
896                 kmem_cache_destroy(cifs_req_cachep);
897                 return -ENOMEM;
898         }
899
900         if (cifs_min_small < 2)
901                 cifs_min_small = 2;
902         else if (cifs_min_small > 256) {
903                 cifs_min_small = 256;
904                 cFYI(1, ("cifs_min_small set to maximum (256)"));
905         }
906
907         cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
908                                                      cifs_sm_req_cachep);
909
910         if (cifs_sm_req_poolp == NULL) {
911                 mempool_destroy(cifs_req_poolp);
912                 kmem_cache_destroy(cifs_req_cachep);
913                 kmem_cache_destroy(cifs_sm_req_cachep);
914                 return -ENOMEM;
915         }
916
917         return 0;
918 }
919
920 static void
921 cifs_destroy_request_bufs(void)
922 {
923         mempool_destroy(cifs_req_poolp);
924         kmem_cache_destroy(cifs_req_cachep);
925         mempool_destroy(cifs_sm_req_poolp);
926         kmem_cache_destroy(cifs_sm_req_cachep);
927 }
928
929 static int
930 cifs_init_mids(void)
931 {
932         cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
933                                             sizeof(struct mid_q_entry), 0,
934                                             SLAB_HWCACHE_ALIGN, NULL);
935         if (cifs_mid_cachep == NULL)
936                 return -ENOMEM;
937
938         /* 3 is a reasonable minimum number of simultaneous operations */
939         cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
940         if (cifs_mid_poolp == NULL) {
941                 kmem_cache_destroy(cifs_mid_cachep);
942                 return -ENOMEM;
943         }
944
945         cifs_oplock_cachep = kmem_cache_create("cifs_oplock_structs",
946                                         sizeof(struct oplock_q_entry), 0,
947                                         SLAB_HWCACHE_ALIGN, NULL);
948         if (cifs_oplock_cachep == NULL) {
949                 mempool_destroy(cifs_mid_poolp);
950                 kmem_cache_destroy(cifs_mid_cachep);
951                 return -ENOMEM;
952         }
953
954         return 0;
955 }
956
957 static void
958 cifs_destroy_mids(void)
959 {
960         mempool_destroy(cifs_mid_poolp);
961         kmem_cache_destroy(cifs_mid_cachep);
962         kmem_cache_destroy(cifs_oplock_cachep);
963 }
964
965 static int cifs_oplock_thread(void *dummyarg)
966 {
967         struct oplock_q_entry *oplock_item;
968         struct cifsTconInfo *pTcon;
969         struct inode *inode;
970         __u16  netfid;
971         int rc, waitrc = 0;
972
973         set_freezable();
974         do {
975                 if (try_to_freeze())
976                         continue;
977
978                 spin_lock(&GlobalMid_Lock);
979                 if (list_empty(&GlobalOplock_Q)) {
980                         spin_unlock(&GlobalMid_Lock);
981                         set_current_state(TASK_INTERRUPTIBLE);
982                         schedule_timeout(39*HZ);
983                 } else {
984                         oplock_item = list_entry(GlobalOplock_Q.next,
985                                                 struct oplock_q_entry, qhead);
986                         cFYI(1, ("found oplock item to write out"));
987                         pTcon = oplock_item->tcon;
988                         inode = oplock_item->pinode;
989                         netfid = oplock_item->netfid;
990                         spin_unlock(&GlobalMid_Lock);
991                         DeleteOplockQEntry(oplock_item);
992                         /* can not grab inode sem here since it would
993                                 deadlock when oplock received on delete
994                                 since vfs_unlink holds the i_mutex across
995                                 the call */
996                         /* mutex_lock(&inode->i_mutex);*/
997                         if (S_ISREG(inode->i_mode)) {
998 #ifdef CONFIG_CIFS_EXPERIMENTAL
999                                 if (CIFS_I(inode)->clientCanCacheAll == 0)
1000                                         break_lease(inode, FMODE_READ);
1001                                 else if (CIFS_I(inode)->clientCanCacheRead == 0)
1002                                         break_lease(inode, FMODE_WRITE);
1003 #endif
1004                                 rc = filemap_fdatawrite(inode->i_mapping);
1005                                 if (CIFS_I(inode)->clientCanCacheRead == 0) {
1006                                         waitrc = filemap_fdatawait(
1007                                                               inode->i_mapping);
1008                                         invalidate_remote_inode(inode);
1009                                 }
1010                                 if (rc == 0)
1011                                         rc = waitrc;
1012                         } else
1013                                 rc = 0;
1014                         /* mutex_unlock(&inode->i_mutex);*/
1015                         if (rc)
1016                                 CIFS_I(inode)->write_behind_rc = rc;
1017                         cFYI(1, ("Oplock flush inode %p rc %d",
1018                                 inode, rc));
1019
1020                                 /* releasing stale oplock after recent reconnect
1021                                 of smb session using a now incorrect file
1022                                 handle is not a data integrity issue but do
1023                                 not bother sending an oplock release if session
1024                                 to server still is disconnected since oplock
1025                                 already released by the server in that case */
1026                         if (!pTcon->need_reconnect) {
1027                                 rc = CIFSSMBLock(0, pTcon, netfid,
1028                                                 0 /* len */ , 0 /* offset */, 0,
1029                                                 0, LOCKING_ANDX_OPLOCK_RELEASE,
1030                                                 false /* wait flag */);
1031                                 cFYI(1, ("Oplock release rc = %d", rc));
1032                         }
1033                         set_current_state(TASK_INTERRUPTIBLE);
1034                         schedule_timeout(1);  /* yield in case q were corrupt */
1035                 }
1036         } while (!kthread_should_stop());
1037
1038         return 0;
1039 }
1040
1041 static int __init
1042 init_cifs(void)
1043 {
1044         int rc = 0;
1045         cifs_proc_init();
1046         INIT_LIST_HEAD(&cifs_tcp_ses_list);
1047         INIT_LIST_HEAD(&GlobalOplock_Q);
1048 #ifdef CONFIG_CIFS_EXPERIMENTAL
1049         INIT_LIST_HEAD(&GlobalDnotifyReqList);
1050         INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
1051 #endif
1052 /*
1053  *  Initialize Global counters
1054  */
1055         atomic_set(&sesInfoAllocCount, 0);
1056         atomic_set(&tconInfoAllocCount, 0);
1057         atomic_set(&tcpSesAllocCount, 0);
1058         atomic_set(&tcpSesReconnectCount, 0);
1059         atomic_set(&tconInfoReconnectCount, 0);
1060
1061         atomic_set(&bufAllocCount, 0);
1062         atomic_set(&smBufAllocCount, 0);
1063 #ifdef CONFIG_CIFS_STATS2
1064         atomic_set(&totBufAllocCount, 0);
1065         atomic_set(&totSmBufAllocCount, 0);
1066 #endif /* CONFIG_CIFS_STATS2 */
1067
1068         atomic_set(&midCount, 0);
1069         GlobalCurrentXid = 0;
1070         GlobalTotalActiveXid = 0;
1071         GlobalMaxActiveXid = 0;
1072         memset(Local_System_Name, 0, 15);
1073         rwlock_init(&GlobalSMBSeslock);
1074         rwlock_init(&cifs_tcp_ses_lock);
1075         spin_lock_init(&GlobalMid_Lock);
1076
1077         if (cifs_max_pending < 2) {
1078                 cifs_max_pending = 2;
1079                 cFYI(1, ("cifs_max_pending set to min of 2"));
1080         } else if (cifs_max_pending > 256) {
1081                 cifs_max_pending = 256;
1082                 cFYI(1, ("cifs_max_pending set to max of 256"));
1083         }
1084
1085         rc = cifs_init_inodecache();
1086         if (rc)
1087                 goto out_clean_proc;
1088
1089         rc = cifs_init_mids();
1090         if (rc)
1091                 goto out_destroy_inodecache;
1092
1093         rc = cifs_init_request_bufs();
1094         if (rc)
1095                 goto out_destroy_mids;
1096
1097         rc = register_filesystem(&cifs_fs_type);
1098         if (rc)
1099                 goto out_destroy_request_bufs;
1100 #ifdef CONFIG_CIFS_UPCALL
1101         rc = register_key_type(&cifs_spnego_key_type);
1102         if (rc)
1103                 goto out_unregister_filesystem;
1104 #endif
1105 #ifdef CONFIG_CIFS_DFS_UPCALL
1106         rc = register_key_type(&key_type_dns_resolver);
1107         if (rc)
1108                 goto out_unregister_key_type;
1109 #endif
1110         oplockThread = kthread_run(cifs_oplock_thread, NULL, "cifsoplockd");
1111         if (IS_ERR(oplockThread)) {
1112                 rc = PTR_ERR(oplockThread);
1113                 cERROR(1, ("error %d create oplock thread", rc));
1114                 goto out_unregister_dfs_key_type;
1115         }
1116
1117         return 0;
1118
1119  out_unregister_dfs_key_type:
1120 #ifdef CONFIG_CIFS_DFS_UPCALL
1121         unregister_key_type(&key_type_dns_resolver);
1122  out_unregister_key_type:
1123 #endif
1124 #ifdef CONFIG_CIFS_UPCALL
1125         unregister_key_type(&cifs_spnego_key_type);
1126  out_unregister_filesystem:
1127 #endif
1128         unregister_filesystem(&cifs_fs_type);
1129  out_destroy_request_bufs:
1130         cifs_destroy_request_bufs();
1131  out_destroy_mids:
1132         cifs_destroy_mids();
1133  out_destroy_inodecache:
1134         cifs_destroy_inodecache();
1135  out_clean_proc:
1136         cifs_proc_clean();
1137         return rc;
1138 }
1139
1140 static void __exit
1141 exit_cifs(void)
1142 {
1143         cFYI(DBG2, ("exit_cifs"));
1144         cifs_proc_clean();
1145 #ifdef CONFIG_CIFS_DFS_UPCALL
1146         cifs_dfs_release_automount_timer();
1147         unregister_key_type(&key_type_dns_resolver);
1148 #endif
1149 #ifdef CONFIG_CIFS_UPCALL
1150         unregister_key_type(&cifs_spnego_key_type);
1151 #endif
1152         unregister_filesystem(&cifs_fs_type);
1153         cifs_destroy_inodecache();
1154         cifs_destroy_mids();
1155         cifs_destroy_request_bufs();
1156         kthread_stop(oplockThread);
1157 }
1158
1159 MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
1160 MODULE_LICENSE("GPL");  /* combination of LGPL + GPL source behaves as GPL */
1161 MODULE_DESCRIPTION
1162     ("VFS to access servers complying with the SNIA CIFS Specification "
1163      "e.g. Samba and Windows");
1164 MODULE_VERSION(CIFS_VERSION);
1165 module_init(init_cifs)
1166 module_exit(exit_cifs)