Merge branch 'stable-3.2' into pandora-3.2
[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 <linux/namei.h>
39 #include <net/ipv6.h>
40 #include "cifsfs.h"
41 #include "cifspdu.h"
42 #define DECLARE_GLOBALS_HERE
43 #include "cifsglob.h"
44 #include "cifsproto.h"
45 #include "cifs_debug.h"
46 #include "cifs_fs_sb.h"
47 #include <linux/mm.h>
48 #include <linux/key-type.h>
49 #include "cifs_spnego.h"
50 #include "fscache.h"
51 #define CIFS_MAGIC_NUMBER 0xFF534D42    /* the first four bytes of SMB PDUs */
52
53 int cifsFYI = 0;
54 int cifsERROR = 1;
55 int traceSMB = 0;
56 bool enable_oplocks = true;
57 bool no_serverino_autodisable = false;
58 unsigned int linuxExtEnabled = 1;
59 unsigned int lookupCacheEnabled = 1;
60 unsigned int multiuser_mount = 0;
61 unsigned int global_secflags = CIFSSEC_DEF;
62 /* unsigned int ntlmv2_support = 0; */
63 unsigned int sign_CIFS_PDUs = 1;
64 static const struct super_operations cifs_super_ops;
65 unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
66 module_param(CIFSMaxBufSize, int, 0);
67 MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header). "
68                                  "Default: 16384 Range: 8192 to 130048");
69 unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
70 module_param(cifs_min_rcv, int, 0);
71 MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
72                                 "1 to 64");
73 unsigned int cifs_min_small = 30;
74 module_param(cifs_min_small, int, 0);
75 MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
76                                  "Range: 2 to 256");
77 unsigned int cifs_max_pending = CIFS_MAX_REQ;
78 module_param(cifs_max_pending, int, 0444);
79 MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server. "
80                                    "Default: 32767 Range: 2 to 32767.");
81 unsigned short echo_retries = 5;
82 module_param(echo_retries, ushort, 0644);
83 MODULE_PARM_DESC(echo_retries, "Number of echo attempts before giving up and "
84                                "reconnecting server. Default: 5. 0 means "
85                                "never reconnect.");
86 module_param(enable_oplocks, bool, 0644);
87 MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks (bool). Default:"
88                                  "y/Y/1");
89 module_param(no_serverino_autodisable, bool, 0644);
90
91 extern mempool_t *cifs_sm_req_poolp;
92 extern mempool_t *cifs_req_poolp;
93 extern mempool_t *cifs_mid_poolp;
94
95 /*
96  * Bumps refcount for cifs super block.
97  * Note that it should be only called if a referece to VFS super block is
98  * already held, e.g. in open-type syscalls context. Otherwise it can race with
99  * atomic_dec_and_test in deactivate_locked_super.
100  */
101 void
102 cifs_sb_active(struct super_block *sb)
103 {
104         struct cifs_sb_info *server = CIFS_SB(sb);
105
106         if (atomic_inc_return(&server->active) == 1)
107                 atomic_inc(&sb->s_active);
108 }
109
110 void
111 cifs_sb_deactive(struct super_block *sb)
112 {
113         struct cifs_sb_info *server = CIFS_SB(sb);
114
115         if (atomic_dec_and_test(&server->active))
116                 deactivate_super(sb);
117 }
118
119 static int
120 cifs_read_super(struct super_block *sb)
121 {
122         struct inode *inode;
123         struct cifs_sb_info *cifs_sb;
124         int rc = 0;
125
126         cifs_sb = CIFS_SB(sb);
127
128         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
129                 sb->s_flags |= MS_POSIXACL;
130
131         if (cifs_sb_master_tcon(cifs_sb)->ses->capabilities & CAP_LARGE_FILES)
132                 sb->s_maxbytes = MAX_LFS_FILESIZE;
133         else
134                 sb->s_maxbytes = MAX_NON_LFS;
135
136         /* BB FIXME fix time_gran to be larger for LANMAN sessions */
137         sb->s_time_gran = 100;
138
139         sb->s_magic = CIFS_MAGIC_NUMBER;
140         sb->s_op = &cifs_super_ops;
141         sb->s_bdi = &cifs_sb->bdi;
142         sb->s_blocksize = CIFS_MAX_MSGSIZE;
143         sb->s_blocksize_bits = 14;      /* default 2**14 = CIFS_MAX_MSGSIZE */
144         inode = cifs_root_iget(sb);
145
146         if (IS_ERR(inode)) {
147                 rc = PTR_ERR(inode);
148                 inode = NULL;
149                 goto out_no_root;
150         }
151
152         sb->s_root = d_alloc_root(inode);
153
154         if (!sb->s_root) {
155                 rc = -ENOMEM;
156                 goto out_no_root;
157         }
158
159         /* do that *after* d_alloc_root() - we want NULL ->d_op for root here */
160         if (cifs_sb_master_tcon(cifs_sb)->nocase)
161                 sb->s_d_op = &cifs_ci_dentry_ops;
162         else
163                 sb->s_d_op = &cifs_dentry_ops;
164
165 #ifdef CONFIG_CIFS_NFSD_EXPORT
166         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
167                 cFYI(1, "export ops supported");
168                 sb->s_export_op = &cifs_export_ops;
169         }
170 #endif /* CONFIG_CIFS_NFSD_EXPORT */
171
172         return 0;
173
174 out_no_root:
175         cERROR(1, "cifs_read_super: get root inode failed");
176         if (inode)
177                 iput(inode);
178
179         return rc;
180 }
181
182 static void cifs_kill_sb(struct super_block *sb)
183 {
184         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
185         kill_anon_super(sb);
186         cifs_umount(cifs_sb);
187 }
188
189 static int
190 cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
191 {
192         struct super_block *sb = dentry->d_sb;
193         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
194         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
195         int rc = -EOPNOTSUPP;
196         int xid;
197
198         xid = GetXid();
199
200         buf->f_type = CIFS_MAGIC_NUMBER;
201
202         /*
203          * PATH_MAX may be too long - it would presumably be total path,
204          * but note that some servers (includinng Samba 3) have a shorter
205          * maximum path.
206          *
207          * Instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO.
208          */
209         buf->f_namelen = PATH_MAX;
210         buf->f_files = 0;       /* undefined */
211         buf->f_ffree = 0;       /* unlimited */
212
213         /*
214          * We could add a second check for a QFS Unix capability bit
215          */
216         if ((tcon->ses->capabilities & CAP_UNIX) &&
217             (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
218                 rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
219
220         /*
221          * Only need to call the old QFSInfo if failed on newer one,
222          * e.g. by OS/2.
223          **/
224         if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
225                 rc = CIFSSMBQFSInfo(xid, tcon, buf);
226
227         /*
228          * Some old Windows servers also do not support level 103, retry with
229          * older level one if old server failed the previous call or we
230          * bypassed it because we detected that this was an older LANMAN sess
231          */
232         if (rc)
233                 rc = SMBOldQFSInfo(xid, tcon, buf);
234
235         FreeXid(xid);
236         return 0;
237 }
238
239 static int cifs_permission(struct inode *inode, int mask)
240 {
241         struct cifs_sb_info *cifs_sb;
242
243         cifs_sb = CIFS_SB(inode->i_sb);
244
245         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
246                 if ((mask & MAY_EXEC) && !execute_ok(inode))
247                         return -EACCES;
248                 else
249                         return 0;
250         } else /* file mode might have been restricted at mount time
251                 on the client (above and beyond ACL on servers) for
252                 servers which do not support setting and viewing mode bits,
253                 so allowing client to check permissions is useful */
254                 return generic_permission(inode, mask);
255 }
256
257 static struct kmem_cache *cifs_inode_cachep;
258 static struct kmem_cache *cifs_req_cachep;
259 static struct kmem_cache *cifs_mid_cachep;
260 static struct kmem_cache *cifs_sm_req_cachep;
261 mempool_t *cifs_sm_req_poolp;
262 mempool_t *cifs_req_poolp;
263 mempool_t *cifs_mid_poolp;
264
265 static struct inode *
266 cifs_alloc_inode(struct super_block *sb)
267 {
268         struct cifsInodeInfo *cifs_inode;
269         cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
270         if (!cifs_inode)
271                 return NULL;
272         cifs_inode->cifsAttrs = 0x20;   /* default */
273         cifs_inode->time = 0;
274         /* Until the file is open and we have gotten oplock
275         info back from the server, can not assume caching of
276         file data or metadata */
277         cifs_set_oplock_level(cifs_inode, 0);
278         cifs_inode->delete_pending = false;
279         cifs_inode->invalid_mapping = false;
280         cifs_inode->vfs_inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
281         cifs_inode->server_eof = 0;
282         cifs_inode->uniqueid = 0;
283         cifs_inode->createtime = 0;
284
285         /* Can not set i_flags here - they get immediately overwritten
286            to zero by the VFS */
287 /*      cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;*/
288         INIT_LIST_HEAD(&cifs_inode->openFileList);
289         return &cifs_inode->vfs_inode;
290 }
291
292 static void cifs_i_callback(struct rcu_head *head)
293 {
294         struct inode *inode = container_of(head, struct inode, i_rcu);
295         INIT_LIST_HEAD(&inode->i_dentry);
296         kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
297 }
298
299 static void
300 cifs_destroy_inode(struct inode *inode)
301 {
302         call_rcu(&inode->i_rcu, cifs_i_callback);
303 }
304
305 static void
306 cifs_evict_inode(struct inode *inode)
307 {
308         truncate_inode_pages(&inode->i_data, 0);
309         end_writeback(inode);
310         cifs_fscache_release_inode_cookie(inode);
311 }
312
313 static void
314 cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
315 {
316         struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
317         struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
318
319         seq_printf(s, ",addr=");
320
321         switch (server->dstaddr.ss_family) {
322         case AF_INET:
323                 seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
324                 break;
325         case AF_INET6:
326                 seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
327                 if (sa6->sin6_scope_id)
328                         seq_printf(s, "%%%u", sa6->sin6_scope_id);
329                 break;
330         default:
331                 seq_printf(s, "(unknown)");
332         }
333 }
334
335 static void
336 cifs_show_security(struct seq_file *s, struct TCP_Server_Info *server)
337 {
338         seq_printf(s, ",sec=");
339
340         switch (server->secType) {
341         case LANMAN:
342                 seq_printf(s, "lanman");
343                 break;
344         case NTLMv2:
345                 seq_printf(s, "ntlmv2");
346                 break;
347         case NTLM:
348                 seq_printf(s, "ntlm");
349                 break;
350         case Kerberos:
351                 seq_printf(s, "krb5");
352                 break;
353         case RawNTLMSSP:
354                 seq_printf(s, "ntlmssp");
355                 break;
356         default:
357                 /* shouldn't ever happen */
358                 seq_printf(s, "unknown");
359                 break;
360         }
361
362         if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
363                 seq_printf(s, "i");
364 }
365
366 /*
367  * cifs_show_options() is for displaying mount options in /proc/mounts.
368  * Not all settable options are displayed but most of the important
369  * ones are.
370  */
371 static int
372 cifs_show_options(struct seq_file *s, struct vfsmount *m)
373 {
374         struct cifs_sb_info *cifs_sb = CIFS_SB(m->mnt_sb);
375         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
376         struct sockaddr *srcaddr;
377         srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
378
379         cifs_show_security(s, tcon->ses->server);
380
381         seq_printf(s, ",unc=%s", tcon->treeName);
382
383         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)
384                 seq_printf(s, ",multiuser");
385         else if (tcon->ses->user_name)
386                 seq_printf(s, ",username=%s", tcon->ses->user_name);
387
388         if (tcon->ses->domainName)
389                 seq_printf(s, ",domain=%s", tcon->ses->domainName);
390
391         if (srcaddr->sa_family != AF_UNSPEC) {
392                 struct sockaddr_in *saddr4;
393                 struct sockaddr_in6 *saddr6;
394                 saddr4 = (struct sockaddr_in *)srcaddr;
395                 saddr6 = (struct sockaddr_in6 *)srcaddr;
396                 if (srcaddr->sa_family == AF_INET6)
397                         seq_printf(s, ",srcaddr=%pI6c",
398                                    &saddr6->sin6_addr);
399                 else if (srcaddr->sa_family == AF_INET)
400                         seq_printf(s, ",srcaddr=%pI4",
401                                    &saddr4->sin_addr.s_addr);
402                 else
403                         seq_printf(s, ",srcaddr=BAD-AF:%i",
404                                    (int)(srcaddr->sa_family));
405         }
406
407         seq_printf(s, ",uid=%d", cifs_sb->mnt_uid);
408         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
409                 seq_printf(s, ",forceuid");
410         else
411                 seq_printf(s, ",noforceuid");
412
413         seq_printf(s, ",gid=%d", cifs_sb->mnt_gid);
414         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
415                 seq_printf(s, ",forcegid");
416         else
417                 seq_printf(s, ",noforcegid");
418
419         cifs_show_address(s, tcon->ses->server);
420
421         if (!tcon->unix_ext)
422                 seq_printf(s, ",file_mode=0%o,dir_mode=0%o",
423                                            cifs_sb->mnt_file_mode,
424                                            cifs_sb->mnt_dir_mode);
425         if (tcon->seal)
426                 seq_printf(s, ",seal");
427         if (tcon->nocase)
428                 seq_printf(s, ",nocase");
429         if (tcon->retry)
430                 seq_printf(s, ",hard");
431         if (tcon->unix_ext)
432                 seq_printf(s, ",unix");
433         else
434                 seq_printf(s, ",nounix");
435         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
436                 seq_printf(s, ",posixpaths");
437         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
438                 seq_printf(s, ",setuids");
439         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
440                 seq_printf(s, ",serverino");
441         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
442                 seq_printf(s, ",rwpidforward");
443         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
444                 seq_printf(s, ",forcemand");
445         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
446                 seq_printf(s, ",directio");
447         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
448                 seq_printf(s, ",nouser_xattr");
449         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
450                 seq_printf(s, ",mapchars");
451         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
452                 seq_printf(s, ",sfu");
453         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
454                 seq_printf(s, ",nobrl");
455         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
456                 seq_printf(s, ",cifsacl");
457         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
458                 seq_printf(s, ",dynperm");
459         if (m->mnt_sb->s_flags & MS_POSIXACL)
460                 seq_printf(s, ",acl");
461         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
462                 seq_printf(s, ",mfsymlinks");
463         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
464                 seq_printf(s, ",fsc");
465         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
466                 seq_printf(s, ",nostrictsync");
467         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
468                 seq_printf(s, ",noperm");
469         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
470                 seq_printf(s, ",strictcache");
471
472         seq_printf(s, ",rsize=%d", cifs_sb->rsize);
473         seq_printf(s, ",wsize=%d", cifs_sb->wsize);
474         /* convert actimeo and display it in seconds */
475                 seq_printf(s, ",actimeo=%lu", cifs_sb->actimeo / HZ);
476
477         return 0;
478 }
479
480 static void cifs_umount_begin(struct super_block *sb)
481 {
482         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
483         struct cifs_tcon *tcon;
484
485         if (cifs_sb == NULL)
486                 return;
487
488         tcon = cifs_sb_master_tcon(cifs_sb);
489
490         spin_lock(&cifs_tcp_ses_lock);
491         if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
492                 /* we have other mounts to same share or we have
493                    already tried to force umount this and woken up
494                    all waiting network requests, nothing to do */
495                 spin_unlock(&cifs_tcp_ses_lock);
496                 return;
497         } else if (tcon->tc_count == 1)
498                 tcon->tidStatus = CifsExiting;
499         spin_unlock(&cifs_tcp_ses_lock);
500
501         /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
502         /* cancel_notify_requests(tcon); */
503         if (tcon->ses && tcon->ses->server) {
504                 cFYI(1, "wake up tasks now - umount begin not complete");
505                 wake_up_all(&tcon->ses->server->request_q);
506                 wake_up_all(&tcon->ses->server->response_q);
507                 msleep(1); /* yield */
508                 /* we have to kick the requests once more */
509                 wake_up_all(&tcon->ses->server->response_q);
510                 msleep(1);
511         }
512
513         return;
514 }
515
516 #ifdef CONFIG_CIFS_STATS2
517 static int cifs_show_stats(struct seq_file *s, struct vfsmount *mnt)
518 {
519         /* BB FIXME */
520         return 0;
521 }
522 #endif
523
524 static int cifs_remount(struct super_block *sb, int *flags, char *data)
525 {
526         *flags |= MS_NODIRATIME;
527         return 0;
528 }
529
530 static int cifs_drop_inode(struct inode *inode)
531 {
532         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
533
534         /* no serverino => unconditional eviction */
535         return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
536                 generic_drop_inode(inode);
537 }
538
539 static const struct super_operations cifs_super_ops = {
540         .statfs = cifs_statfs,
541         .alloc_inode = cifs_alloc_inode,
542         .destroy_inode = cifs_destroy_inode,
543         .drop_inode     = cifs_drop_inode,
544         .evict_inode    = cifs_evict_inode,
545 /*      .delete_inode   = cifs_delete_inode,  */  /* Do not need above
546         function unless later we add lazy close of inodes or unless the
547         kernel forgets to call us with the same number of releases (closes)
548         as opens */
549         .show_options = cifs_show_options,
550         .umount_begin   = cifs_umount_begin,
551         .remount_fs = cifs_remount,
552 #ifdef CONFIG_CIFS_STATS2
553         .show_stats = cifs_show_stats,
554 #endif
555 };
556
557 /*
558  * Get root dentry from superblock according to prefix path mount option.
559  * Return dentry with refcount + 1 on success and NULL otherwise.
560  */
561 static struct dentry *
562 cifs_get_root(struct smb_vol *vol, struct super_block *sb)
563 {
564         struct dentry *dentry;
565         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
566         char *full_path = NULL;
567         char *s, *p;
568         char sep;
569
570         full_path = cifs_build_path_to_root(vol, cifs_sb,
571                                             cifs_sb_master_tcon(cifs_sb));
572         if (full_path == NULL)
573                 return ERR_PTR(-ENOMEM);
574
575         cFYI(1, "Get root dentry for %s", full_path);
576
577         sep = CIFS_DIR_SEP(cifs_sb);
578         dentry = dget(sb->s_root);
579         p = s = full_path;
580
581         do {
582                 struct inode *dir = dentry->d_inode;
583                 struct dentry *child;
584
585                 if (!dir) {
586                         dput(dentry);
587                         dentry = ERR_PTR(-ENOENT);
588                         break;
589                 }
590                 if (!S_ISDIR(dir->i_mode)) {
591                         dput(dentry);
592                         dentry = ERR_PTR(-ENOTDIR);
593                         break;
594                 }
595
596                 /* skip separators */
597                 while (*s == sep)
598                         s++;
599                 if (!*s)
600                         break;
601                 p = s++;
602                 /* next separator */
603                 while (*s && *s != sep)
604                         s++;
605
606                 mutex_lock(&dir->i_mutex);
607                 child = lookup_one_len(p, dentry, s - p);
608                 mutex_unlock(&dir->i_mutex);
609                 dput(dentry);
610                 dentry = child;
611         } while (!IS_ERR(dentry));
612         kfree(full_path);
613         return dentry;
614 }
615
616 static int cifs_set_super(struct super_block *sb, void *data)
617 {
618         struct cifs_mnt_data *mnt_data = data;
619         sb->s_fs_info = mnt_data->cifs_sb;
620         return set_anon_super(sb, NULL);
621 }
622
623 static struct dentry *
624 cifs_do_mount(struct file_system_type *fs_type,
625               int flags, const char *dev_name, void *data)
626 {
627         int rc;
628         struct super_block *sb;
629         struct cifs_sb_info *cifs_sb;
630         struct smb_vol *volume_info;
631         struct cifs_mnt_data mnt_data;
632         struct dentry *root;
633
634         cFYI(1, "Devname: %s flags: %d ", dev_name, flags);
635
636         volume_info = cifs_get_volume_info((char *)data, dev_name);
637         if (IS_ERR(volume_info))
638                 return ERR_CAST(volume_info);
639
640         cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
641         if (cifs_sb == NULL) {
642                 root = ERR_PTR(-ENOMEM);
643                 goto out_nls;
644         }
645
646         cifs_sb->mountdata = kstrndup(data, PAGE_SIZE, GFP_KERNEL);
647         if (cifs_sb->mountdata == NULL) {
648                 root = ERR_PTR(-ENOMEM);
649                 goto out_cifs_sb;
650         }
651
652         cifs_setup_cifs_sb(volume_info, cifs_sb);
653
654         rc = cifs_mount(cifs_sb, volume_info);
655         if (rc) {
656                 if (!(flags & MS_SILENT))
657                         cERROR(1, "cifs_mount failed w/return code = %d", rc);
658                 root = ERR_PTR(rc);
659                 goto out_mountdata;
660         }
661
662         mnt_data.vol = volume_info;
663         mnt_data.cifs_sb = cifs_sb;
664         mnt_data.flags = flags;
665
666         sb = sget(fs_type, cifs_match_super, cifs_set_super, &mnt_data);
667         if (IS_ERR(sb)) {
668                 root = ERR_CAST(sb);
669                 cifs_umount(cifs_sb);
670                 goto out;
671         }
672
673         if (sb->s_root) {
674                 cFYI(1, "Use existing superblock");
675                 cifs_umount(cifs_sb);
676         } else {
677                 sb->s_flags = flags;
678                 /* BB should we make this contingent on mount parm? */
679                 sb->s_flags |= MS_NODIRATIME | MS_NOATIME;
680
681                 rc = cifs_read_super(sb);
682                 if (rc) {
683                         root = ERR_PTR(rc);
684                         goto out_super;
685                 }
686
687                 sb->s_flags |= MS_ACTIVE;
688         }
689
690         root = cifs_get_root(volume_info, sb);
691         if (IS_ERR(root))
692                 goto out_super;
693
694         cFYI(1, "dentry root is: %p", root);
695         goto out;
696
697 out_super:
698         deactivate_locked_super(sb);
699 out:
700         cifs_cleanup_volume_info(volume_info);
701         return root;
702
703 out_mountdata:
704         kfree(cifs_sb->mountdata);
705 out_cifs_sb:
706         kfree(cifs_sb);
707 out_nls:
708         unload_nls(volume_info->local_nls);
709         goto out;
710 }
711
712 static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
713                                    unsigned long nr_segs, loff_t pos)
714 {
715         struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
716         ssize_t written;
717         int rc;
718
719         written = generic_file_aio_write(iocb, iov, nr_segs, pos);
720
721         if (CIFS_I(inode)->clientCanCacheAll)
722                 return written;
723
724         rc = filemap_fdatawrite(inode->i_mapping);
725         if (rc)
726                 cFYI(1, "cifs_file_aio_write: %d rc on %p inode", rc, inode);
727
728         return written;
729 }
730
731 static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
732 {
733         /*
734          * origin == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
735          * the cached file length
736          */
737         if (origin != SEEK_SET && origin != SEEK_CUR) {
738                 int rc;
739                 struct inode *inode = file->f_path.dentry->d_inode;
740
741                 /*
742                  * We need to be sure that all dirty pages are written and the
743                  * server has the newest file length.
744                  */
745                 if (!CIFS_I(inode)->clientCanCacheRead && inode->i_mapping &&
746                     inode->i_mapping->nrpages != 0) {
747                         rc = filemap_fdatawait(inode->i_mapping);
748                         if (rc) {
749                                 mapping_set_error(inode->i_mapping, rc);
750                                 return rc;
751                         }
752                 }
753                 /*
754                  * Some applications poll for the file length in this strange
755                  * way so we must seek to end on non-oplocked files by
756                  * setting the revalidate time to zero.
757                  */
758                 CIFS_I(inode)->time = 0;
759
760                 rc = cifs_revalidate_file_attr(file);
761                 if (rc < 0)
762                         return (loff_t)rc;
763         }
764         return generic_file_llseek(file, offset, origin);
765 }
766
767 static int cifs_setlease(struct file *file, long arg, struct file_lock **lease)
768 {
769         /* note that this is called by vfs setlease with lock_flocks held
770            to protect *lease from going away */
771         struct inode *inode = file->f_path.dentry->d_inode;
772         struct cifsFileInfo *cfile = file->private_data;
773
774         if (!(S_ISREG(inode->i_mode)))
775                 return -EINVAL;
776
777         /* check if file is oplocked */
778         if (((arg == F_RDLCK) &&
779                 (CIFS_I(inode)->clientCanCacheRead)) ||
780             ((arg == F_WRLCK) &&
781                 (CIFS_I(inode)->clientCanCacheAll)))
782                 return generic_setlease(file, arg, lease);
783         else if (tlink_tcon(cfile->tlink)->local_lease &&
784                  !CIFS_I(inode)->clientCanCacheRead)
785                 /* If the server claims to support oplock on this
786                    file, then we still need to check oplock even
787                    if the local_lease mount option is set, but there
788                    are servers which do not support oplock for which
789                    this mount option may be useful if the user
790                    knows that the file won't be changed on the server
791                    by anyone else */
792                 return generic_setlease(file, arg, lease);
793         else
794                 return -EAGAIN;
795 }
796
797 struct file_system_type cifs_fs_type = {
798         .owner = THIS_MODULE,
799         .name = "cifs",
800         .mount = cifs_do_mount,
801         .kill_sb = cifs_kill_sb,
802         /*  .fs_flags */
803 };
804 const struct inode_operations cifs_dir_inode_ops = {
805         .create = cifs_create,
806         .lookup = cifs_lookup,
807         .getattr = cifs_getattr,
808         .unlink = cifs_unlink,
809         .link = cifs_hardlink,
810         .mkdir = cifs_mkdir,
811         .rmdir = cifs_rmdir,
812         .rename = cifs_rename,
813         .permission = cifs_permission,
814 /*      revalidate:cifs_revalidate,   */
815         .setattr = cifs_setattr,
816         .symlink = cifs_symlink,
817         .mknod   = cifs_mknod,
818 #ifdef CONFIG_CIFS_XATTR
819         .setxattr = cifs_setxattr,
820         .getxattr = cifs_getxattr,
821         .listxattr = cifs_listxattr,
822         .removexattr = cifs_removexattr,
823 #endif
824 };
825
826 const struct inode_operations cifs_file_inode_ops = {
827 /*      revalidate:cifs_revalidate, */
828         .setattr = cifs_setattr,
829         .getattr = cifs_getattr, /* do we need this anymore? */
830         .rename = cifs_rename,
831         .permission = cifs_permission,
832 #ifdef CONFIG_CIFS_XATTR
833         .setxattr = cifs_setxattr,
834         .getxattr = cifs_getxattr,
835         .listxattr = cifs_listxattr,
836         .removexattr = cifs_removexattr,
837 #endif
838 };
839
840 const struct inode_operations cifs_symlink_inode_ops = {
841         .readlink = generic_readlink,
842         .follow_link = cifs_follow_link,
843         .put_link = cifs_put_link,
844         .permission = cifs_permission,
845         /* BB add the following two eventually */
846         /* revalidate: cifs_revalidate,
847            setattr:    cifs_notify_change, *//* BB do we need notify change */
848 #ifdef CONFIG_CIFS_XATTR
849         .setxattr = cifs_setxattr,
850         .getxattr = cifs_getxattr,
851         .listxattr = cifs_listxattr,
852         .removexattr = cifs_removexattr,
853 #endif
854 };
855
856 const struct file_operations cifs_file_ops = {
857         .read = do_sync_read,
858         .write = do_sync_write,
859         .aio_read = generic_file_aio_read,
860         .aio_write = cifs_file_aio_write,
861         .open = cifs_open,
862         .release = cifs_close,
863         .lock = cifs_lock,
864         .fsync = cifs_fsync,
865         .flush = cifs_flush,
866         .mmap  = cifs_file_mmap,
867         .splice_read = generic_file_splice_read,
868         .llseek = cifs_llseek,
869 #ifdef CONFIG_CIFS_POSIX
870         .unlocked_ioctl = cifs_ioctl,
871 #endif /* CONFIG_CIFS_POSIX */
872         .setlease = cifs_setlease,
873 };
874
875 const struct file_operations cifs_file_strict_ops = {
876         .read = do_sync_read,
877         .write = do_sync_write,
878         .aio_read = cifs_strict_readv,
879         .aio_write = cifs_strict_writev,
880         .open = cifs_open,
881         .release = cifs_close,
882         .lock = cifs_lock,
883         .fsync = cifs_strict_fsync,
884         .flush = cifs_flush,
885         .mmap = cifs_file_strict_mmap,
886         .splice_read = generic_file_splice_read,
887         .llseek = cifs_llseek,
888 #ifdef CONFIG_CIFS_POSIX
889         .unlocked_ioctl = cifs_ioctl,
890 #endif /* CONFIG_CIFS_POSIX */
891         .setlease = cifs_setlease,
892 };
893
894 const struct file_operations cifs_file_direct_ops = {
895         /* BB reevaluate whether they can be done with directio, no cache */
896         .read = do_sync_read,
897         .write = do_sync_write,
898         .aio_read = cifs_user_readv,
899         .aio_write = cifs_user_writev,
900         .open = cifs_open,
901         .release = cifs_close,
902         .lock = cifs_lock,
903         .fsync = cifs_fsync,
904         .flush = cifs_flush,
905         .mmap = cifs_file_mmap,
906         .splice_read = generic_file_splice_read,
907 #ifdef CONFIG_CIFS_POSIX
908         .unlocked_ioctl  = cifs_ioctl,
909 #endif /* CONFIG_CIFS_POSIX */
910         .llseek = cifs_llseek,
911         .setlease = cifs_setlease,
912 };
913
914 const struct file_operations cifs_file_nobrl_ops = {
915         .read = do_sync_read,
916         .write = do_sync_write,
917         .aio_read = generic_file_aio_read,
918         .aio_write = cifs_file_aio_write,
919         .open = cifs_open,
920         .release = cifs_close,
921         .fsync = cifs_fsync,
922         .flush = cifs_flush,
923         .mmap  = cifs_file_mmap,
924         .splice_read = generic_file_splice_read,
925         .llseek = cifs_llseek,
926 #ifdef CONFIG_CIFS_POSIX
927         .unlocked_ioctl = cifs_ioctl,
928 #endif /* CONFIG_CIFS_POSIX */
929         .setlease = cifs_setlease,
930 };
931
932 const struct file_operations cifs_file_strict_nobrl_ops = {
933         .read = do_sync_read,
934         .write = do_sync_write,
935         .aio_read = cifs_strict_readv,
936         .aio_write = cifs_strict_writev,
937         .open = cifs_open,
938         .release = cifs_close,
939         .fsync = cifs_strict_fsync,
940         .flush = cifs_flush,
941         .mmap = cifs_file_strict_mmap,
942         .splice_read = generic_file_splice_read,
943         .llseek = cifs_llseek,
944 #ifdef CONFIG_CIFS_POSIX
945         .unlocked_ioctl = cifs_ioctl,
946 #endif /* CONFIG_CIFS_POSIX */
947         .setlease = cifs_setlease,
948 };
949
950 const struct file_operations cifs_file_direct_nobrl_ops = {
951         /* BB reevaluate whether they can be done with directio, no cache */
952         .read = do_sync_read,
953         .write = do_sync_write,
954         .aio_read = cifs_user_readv,
955         .aio_write = cifs_user_writev,
956         .open = cifs_open,
957         .release = cifs_close,
958         .fsync = cifs_fsync,
959         .flush = cifs_flush,
960         .mmap = cifs_file_mmap,
961         .splice_read = generic_file_splice_read,
962 #ifdef CONFIG_CIFS_POSIX
963         .unlocked_ioctl  = cifs_ioctl,
964 #endif /* CONFIG_CIFS_POSIX */
965         .llseek = cifs_llseek,
966         .setlease = cifs_setlease,
967 };
968
969 const struct file_operations cifs_dir_ops = {
970         .readdir = cifs_readdir,
971         .release = cifs_closedir,
972         .read    = generic_read_dir,
973         .unlocked_ioctl  = cifs_ioctl,
974         .llseek = generic_file_llseek,
975 };
976
977 static void
978 cifs_init_once(void *inode)
979 {
980         struct cifsInodeInfo *cifsi = inode;
981
982         inode_init_once(&cifsi->vfs_inode);
983         INIT_LIST_HEAD(&cifsi->llist);
984         mutex_init(&cifsi->lock_mutex);
985 }
986
987 static int
988 cifs_init_inodecache(void)
989 {
990         cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
991                                               sizeof(struct cifsInodeInfo),
992                                               0, (SLAB_RECLAIM_ACCOUNT|
993                                                 SLAB_MEM_SPREAD),
994                                               cifs_init_once);
995         if (cifs_inode_cachep == NULL)
996                 return -ENOMEM;
997
998         return 0;
999 }
1000
1001 static void
1002 cifs_destroy_inodecache(void)
1003 {
1004         kmem_cache_destroy(cifs_inode_cachep);
1005 }
1006
1007 static int
1008 cifs_init_request_bufs(void)
1009 {
1010         if (CIFSMaxBufSize < 8192) {
1011         /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
1012         Unicode path name has to fit in any SMB/CIFS path based frames */
1013                 CIFSMaxBufSize = 8192;
1014         } else if (CIFSMaxBufSize > 1024*127) {
1015                 CIFSMaxBufSize = 1024 * 127;
1016         } else {
1017                 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
1018         }
1019 /*      cERROR(1, "CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize); */
1020         cifs_req_cachep = kmem_cache_create("cifs_request",
1021                                             CIFSMaxBufSize +
1022                                             MAX_CIFS_HDR_SIZE, 0,
1023                                             SLAB_HWCACHE_ALIGN, NULL);
1024         if (cifs_req_cachep == NULL)
1025                 return -ENOMEM;
1026
1027         if (cifs_min_rcv < 1)
1028                 cifs_min_rcv = 1;
1029         else if (cifs_min_rcv > 64) {
1030                 cifs_min_rcv = 64;
1031                 cERROR(1, "cifs_min_rcv set to maximum (64)");
1032         }
1033
1034         cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1035                                                   cifs_req_cachep);
1036
1037         if (cifs_req_poolp == NULL) {
1038                 kmem_cache_destroy(cifs_req_cachep);
1039                 return -ENOMEM;
1040         }
1041         /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1042         almost all handle based requests (but not write response, nor is it
1043         sufficient for path based requests).  A smaller size would have
1044         been more efficient (compacting multiple slab items on one 4k page)
1045         for the case in which debug was on, but this larger size allows
1046         more SMBs to use small buffer alloc and is still much more
1047         efficient to alloc 1 per page off the slab compared to 17K (5page)
1048         alloc of large cifs buffers even when page debugging is on */
1049         cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
1050                         MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
1051                         NULL);
1052         if (cifs_sm_req_cachep == NULL) {
1053                 mempool_destroy(cifs_req_poolp);
1054                 kmem_cache_destroy(cifs_req_cachep);
1055                 return -ENOMEM;
1056         }
1057
1058         if (cifs_min_small < 2)
1059                 cifs_min_small = 2;
1060         else if (cifs_min_small > 256) {
1061                 cifs_min_small = 256;
1062                 cFYI(1, "cifs_min_small set to maximum (256)");
1063         }
1064
1065         cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1066                                                      cifs_sm_req_cachep);
1067
1068         if (cifs_sm_req_poolp == NULL) {
1069                 mempool_destroy(cifs_req_poolp);
1070                 kmem_cache_destroy(cifs_req_cachep);
1071                 kmem_cache_destroy(cifs_sm_req_cachep);
1072                 return -ENOMEM;
1073         }
1074
1075         return 0;
1076 }
1077
1078 static void
1079 cifs_destroy_request_bufs(void)
1080 {
1081         mempool_destroy(cifs_req_poolp);
1082         kmem_cache_destroy(cifs_req_cachep);
1083         mempool_destroy(cifs_sm_req_poolp);
1084         kmem_cache_destroy(cifs_sm_req_cachep);
1085 }
1086
1087 static int
1088 cifs_init_mids(void)
1089 {
1090         cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
1091                                             sizeof(struct mid_q_entry), 0,
1092                                             SLAB_HWCACHE_ALIGN, NULL);
1093         if (cifs_mid_cachep == NULL)
1094                 return -ENOMEM;
1095
1096         /* 3 is a reasonable minimum number of simultaneous operations */
1097         cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
1098         if (cifs_mid_poolp == NULL) {
1099                 kmem_cache_destroy(cifs_mid_cachep);
1100                 return -ENOMEM;
1101         }
1102
1103         return 0;
1104 }
1105
1106 static void
1107 cifs_destroy_mids(void)
1108 {
1109         mempool_destroy(cifs_mid_poolp);
1110         kmem_cache_destroy(cifs_mid_cachep);
1111 }
1112
1113 static int __init
1114 init_cifs(void)
1115 {
1116         int rc = 0;
1117         cifs_proc_init();
1118         INIT_LIST_HEAD(&cifs_tcp_ses_list);
1119 #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* unused temporarily */
1120         INIT_LIST_HEAD(&GlobalDnotifyReqList);
1121         INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
1122 #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */
1123 /*
1124  *  Initialize Global counters
1125  */
1126         atomic_set(&sesInfoAllocCount, 0);
1127         atomic_set(&tconInfoAllocCount, 0);
1128         atomic_set(&tcpSesAllocCount, 0);
1129         atomic_set(&tcpSesReconnectCount, 0);
1130         atomic_set(&tconInfoReconnectCount, 0);
1131
1132         atomic_set(&bufAllocCount, 0);
1133         atomic_set(&smBufAllocCount, 0);
1134 #ifdef CONFIG_CIFS_STATS2
1135         atomic_set(&totBufAllocCount, 0);
1136         atomic_set(&totSmBufAllocCount, 0);
1137 #endif /* CONFIG_CIFS_STATS2 */
1138
1139         atomic_set(&midCount, 0);
1140         GlobalCurrentXid = 0;
1141         GlobalTotalActiveXid = 0;
1142         GlobalMaxActiveXid = 0;
1143         spin_lock_init(&cifs_tcp_ses_lock);
1144         spin_lock_init(&cifs_file_list_lock);
1145         spin_lock_init(&GlobalMid_Lock);
1146
1147         if (cifs_max_pending < 2) {
1148                 cifs_max_pending = 2;
1149                 cFYI(1, "cifs_max_pending set to min of 2");
1150         } else if (cifs_max_pending > CIFS_MAX_REQ) {
1151                 cifs_max_pending = CIFS_MAX_REQ;
1152                 cFYI(1, "cifs_max_pending set to max of %u", CIFS_MAX_REQ);
1153         }
1154
1155         rc = cifs_fscache_register();
1156         if (rc)
1157                 goto out_clean_proc;
1158
1159         rc = cifs_init_inodecache();
1160         if (rc)
1161                 goto out_unreg_fscache;
1162
1163         rc = cifs_init_mids();
1164         if (rc)
1165                 goto out_destroy_inodecache;
1166
1167         rc = cifs_init_request_bufs();
1168         if (rc)
1169                 goto out_destroy_mids;
1170
1171 #ifdef CONFIG_CIFS_UPCALL
1172         rc = register_key_type(&cifs_spnego_key_type);
1173         if (rc)
1174                 goto out_destroy_request_bufs;
1175 #endif /* CONFIG_CIFS_UPCALL */
1176
1177 #ifdef CONFIG_CIFS_ACL
1178         rc = init_cifs_idmap();
1179         if (rc)
1180                 goto out_register_key_type;
1181 #endif /* CONFIG_CIFS_ACL */
1182
1183         rc = register_filesystem(&cifs_fs_type);
1184         if (rc)
1185                 goto out_init_cifs_idmap;
1186
1187         return 0;
1188
1189 out_init_cifs_idmap:
1190 #ifdef CONFIG_CIFS_ACL
1191         exit_cifs_idmap();
1192 out_register_key_type:
1193 #endif
1194 #ifdef CONFIG_CIFS_UPCALL
1195         unregister_key_type(&cifs_spnego_key_type);
1196 out_destroy_request_bufs:
1197 #endif
1198         cifs_destroy_request_bufs();
1199 out_destroy_mids:
1200         cifs_destroy_mids();
1201 out_destroy_inodecache:
1202         cifs_destroy_inodecache();
1203 out_unreg_fscache:
1204         cifs_fscache_unregister();
1205 out_clean_proc:
1206         cifs_proc_clean();
1207         return rc;
1208 }
1209
1210 static void __exit
1211 exit_cifs(void)
1212 {
1213         cFYI(DBG2, "exit_cifs");
1214         cifs_proc_clean();
1215         cifs_fscache_unregister();
1216 #ifdef CONFIG_CIFS_DFS_UPCALL
1217         cifs_dfs_release_automount_timer();
1218 #endif
1219 #ifdef CONFIG_CIFS_ACL
1220         cifs_destroy_idmaptrees();
1221         exit_cifs_idmap();
1222 #endif
1223 #ifdef CONFIG_CIFS_UPCALL
1224         unregister_key_type(&cifs_spnego_key_type);
1225 #endif
1226         unregister_filesystem(&cifs_fs_type);
1227         cifs_destroy_inodecache();
1228         cifs_destroy_mids();
1229         cifs_destroy_request_bufs();
1230 }
1231
1232 MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
1233 MODULE_LICENSE("GPL");  /* combination of LGPL + GPL source behaves as GPL */
1234 MODULE_DESCRIPTION
1235     ("VFS to access servers complying with the SNIA CIFS Specification "
1236      "e.g. Samba and Windows");
1237 MODULE_VERSION(CIFS_VERSION);
1238 module_init(init_cifs)
1239 module_exit(exit_cifs)