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