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