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