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