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