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