]> Pileus Git - ~andy/linux/blob - fs/cifs/cifsfs.c
f653835d067b3323a1eea5f32774326a4937ac09
[~andy/linux] / 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 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, uint, 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, uint, 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, uint, 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, uint, 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. Default: y/Y/1");
83
84 extern mempool_t *cifs_sm_req_poolp;
85 extern mempool_t *cifs_req_poolp;
86 extern mempool_t *cifs_mid_poolp;
87
88 struct workqueue_struct *cifsiod_wq;
89
90 #ifdef CONFIG_CIFS_SMB2
91 __u8 cifs_client_guid[SMB2_CLIENT_GUID_SIZE];
92 #endif
93
94 static int
95 cifs_read_super(struct super_block *sb)
96 {
97         struct inode *inode;
98         struct cifs_sb_info *cifs_sb;
99         int rc = 0;
100
101         cifs_sb = CIFS_SB(sb);
102
103         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
104                 sb->s_flags |= MS_POSIXACL;
105
106         if (cifs_sb_master_tcon(cifs_sb)->ses->capabilities & CAP_LARGE_FILES)
107                 sb->s_maxbytes = MAX_LFS_FILESIZE;
108         else
109                 sb->s_maxbytes = MAX_NON_LFS;
110
111         /* BB FIXME fix time_gran to be larger for LANMAN sessions */
112         sb->s_time_gran = 100;
113
114         sb->s_magic = CIFS_MAGIC_NUMBER;
115         sb->s_op = &cifs_super_ops;
116         sb->s_bdi = &cifs_sb->bdi;
117         sb->s_blocksize = CIFS_MAX_MSGSIZE;
118         sb->s_blocksize_bits = 14;      /* default 2**14 = CIFS_MAX_MSGSIZE */
119         inode = cifs_root_iget(sb);
120
121         if (IS_ERR(inode)) {
122                 rc = PTR_ERR(inode);
123                 goto out_no_root;
124         }
125
126         sb->s_root = d_make_root(inode);
127         if (!sb->s_root) {
128                 rc = -ENOMEM;
129                 goto out_no_root;
130         }
131
132         /* do that *after* d_make_root() - we want NULL ->d_op for root here */
133         if (cifs_sb_master_tcon(cifs_sb)->nocase)
134                 sb->s_d_op = &cifs_ci_dentry_ops;
135         else
136                 sb->s_d_op = &cifs_dentry_ops;
137
138 #ifdef CONFIG_CIFS_NFSD_EXPORT
139         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
140                 cFYI(1, "export ops supported");
141                 sb->s_export_op = &cifs_export_ops;
142         }
143 #endif /* CONFIG_CIFS_NFSD_EXPORT */
144
145         return 0;
146
147 out_no_root:
148         cERROR(1, "cifs_read_super: get root inode failed");
149         return rc;
150 }
151
152 static void cifs_kill_sb(struct super_block *sb)
153 {
154         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
155         kill_anon_super(sb);
156         cifs_umount(cifs_sb);
157 }
158
159 static int
160 cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
161 {
162         struct super_block *sb = dentry->d_sb;
163         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
164         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
165         struct TCP_Server_Info *server = tcon->ses->server;
166         unsigned int xid;
167         int rc = 0;
168
169         xid = get_xid();
170
171         /*
172          * PATH_MAX may be too long - it would presumably be total path,
173          * but note that some servers (includinng Samba 3) have a shorter
174          * maximum path.
175          *
176          * Instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO.
177          */
178         buf->f_namelen = PATH_MAX;
179         buf->f_files = 0;       /* undefined */
180         buf->f_ffree = 0;       /* unlimited */
181
182         if (server->ops->queryfs)
183                 rc = server->ops->queryfs(xid, tcon, buf);
184
185         free_xid(xid);
186         return 0;
187 }
188
189 static int cifs_permission(struct inode *inode, int mask)
190 {
191         struct cifs_sb_info *cifs_sb;
192
193         cifs_sb = CIFS_SB(inode->i_sb);
194
195         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
196                 if ((mask & MAY_EXEC) && !execute_ok(inode))
197                         return -EACCES;
198                 else
199                         return 0;
200         } else /* file mode might have been restricted at mount time
201                 on the client (above and beyond ACL on servers) for
202                 servers which do not support setting and viewing mode bits,
203                 so allowing client to check permissions is useful */
204                 return generic_permission(inode, mask);
205 }
206
207 static struct kmem_cache *cifs_inode_cachep;
208 static struct kmem_cache *cifs_req_cachep;
209 static struct kmem_cache *cifs_mid_cachep;
210 static struct kmem_cache *cifs_sm_req_cachep;
211 mempool_t *cifs_sm_req_poolp;
212 mempool_t *cifs_req_poolp;
213 mempool_t *cifs_mid_poolp;
214
215 static struct inode *
216 cifs_alloc_inode(struct super_block *sb)
217 {
218         struct cifsInodeInfo *cifs_inode;
219         cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
220         if (!cifs_inode)
221                 return NULL;
222         cifs_inode->cifsAttrs = 0x20;   /* default */
223         cifs_inode->time = 0;
224         /*
225          * Until the file is open and we have gotten oplock info back from the
226          * server, can not assume caching of file data or metadata.
227          */
228         cifs_set_oplock_level(cifs_inode, 0);
229         cifs_inode->delete_pending = false;
230         cifs_inode->invalid_mapping = false;
231         cifs_inode->leave_pages_clean = false;
232         cifs_inode->vfs_inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
233         cifs_inode->server_eof = 0;
234         cifs_inode->uniqueid = 0;
235         cifs_inode->createtime = 0;
236 #ifdef CONFIG_CIFS_SMB2
237         get_random_bytes(cifs_inode->lease_key, SMB2_LEASE_KEY_SIZE);
238 #endif
239         /*
240          * Can not set i_flags here - they get immediately overwritten to zero
241          * by the VFS.
242          */
243         /* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME; */
244         INIT_LIST_HEAD(&cifs_inode->openFileList);
245         INIT_LIST_HEAD(&cifs_inode->llist);
246         return &cifs_inode->vfs_inode;
247 }
248
249 static void cifs_i_callback(struct rcu_head *head)
250 {
251         struct inode *inode = container_of(head, struct inode, i_rcu);
252         kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
253 }
254
255 static void
256 cifs_destroy_inode(struct inode *inode)
257 {
258         call_rcu(&inode->i_rcu, cifs_i_callback);
259 }
260
261 static void
262 cifs_evict_inode(struct inode *inode)
263 {
264         truncate_inode_pages(&inode->i_data, 0);
265         clear_inode(inode);
266         cifs_fscache_release_inode_cookie(inode);
267 }
268
269 static void
270 cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
271 {
272         struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
273         struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
274
275         seq_printf(s, ",addr=");
276
277         switch (server->dstaddr.ss_family) {
278         case AF_INET:
279                 seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
280                 break;
281         case AF_INET6:
282                 seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
283                 if (sa6->sin6_scope_id)
284                         seq_printf(s, "%%%u", sa6->sin6_scope_id);
285                 break;
286         default:
287                 seq_printf(s, "(unknown)");
288         }
289 }
290
291 static void
292 cifs_show_security(struct seq_file *s, struct TCP_Server_Info *server)
293 {
294         seq_printf(s, ",sec=");
295
296         switch (server->secType) {
297         case LANMAN:
298                 seq_printf(s, "lanman");
299                 break;
300         case NTLMv2:
301                 seq_printf(s, "ntlmv2");
302                 break;
303         case NTLM:
304                 seq_printf(s, "ntlm");
305                 break;
306         case Kerberos:
307                 seq_printf(s, "krb5");
308                 break;
309         case RawNTLMSSP:
310                 seq_printf(s, "ntlmssp");
311                 break;
312         default:
313                 /* shouldn't ever happen */
314                 seq_printf(s, "unknown");
315                 break;
316         }
317
318         if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
319                 seq_printf(s, "i");
320 }
321
322 static void
323 cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
324 {
325         seq_printf(s, ",cache=");
326
327         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
328                 seq_printf(s, "strict");
329         else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
330                 seq_printf(s, "none");
331         else
332                 seq_printf(s, "loose");
333 }
334
335 /*
336  * cifs_show_options() is for displaying mount options in /proc/mounts.
337  * Not all settable options are displayed but most of the important
338  * ones are.
339  */
340 static int
341 cifs_show_options(struct seq_file *s, struct dentry *root)
342 {
343         struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
344         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
345         struct sockaddr *srcaddr;
346         srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
347
348         seq_printf(s, ",vers=%s", tcon->ses->server->vals->version_string);
349         cifs_show_security(s, tcon->ses->server);
350         cifs_show_cache_flavor(s, cifs_sb);
351
352         seq_printf(s, ",unc=");
353         seq_escape(s, tcon->treeName, " \t\n\\");
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 = cifs_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 whence)
698 {
699         /*
700          * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
701          * the cached file length
702          */
703         if (whence != SEEK_SET && whence != 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, whence);
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         /*
971          * Make sure all delayed rcu free inodes are flushed before we
972          * destroy cache.
973          */
974         rcu_barrier();
975         kmem_cache_destroy(cifs_inode_cachep);
976 }
977
978 static int
979 cifs_init_request_bufs(void)
980 {
981         size_t max_hdr_size = MAX_CIFS_HDR_SIZE;
982 #ifdef CONFIG_CIFS_SMB2
983         /*
984          * SMB2 maximum header size is bigger than CIFS one - no problems to
985          * allocate some more bytes for CIFS.
986          */
987         max_hdr_size = MAX_SMB2_HDR_SIZE;
988 #endif
989         if (CIFSMaxBufSize < 8192) {
990         /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
991         Unicode path name has to fit in any SMB/CIFS path based frames */
992                 CIFSMaxBufSize = 8192;
993         } else if (CIFSMaxBufSize > 1024*127) {
994                 CIFSMaxBufSize = 1024 * 127;
995         } else {
996                 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
997         }
998 /*      cERROR(1, "CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize); */
999         cifs_req_cachep = kmem_cache_create("cifs_request",
1000                                             CIFSMaxBufSize + max_hdr_size, 0,
1001                                             SLAB_HWCACHE_ALIGN, NULL);
1002         if (cifs_req_cachep == NULL)
1003                 return -ENOMEM;
1004
1005         if (cifs_min_rcv < 1)
1006                 cifs_min_rcv = 1;
1007         else if (cifs_min_rcv > 64) {
1008                 cifs_min_rcv = 64;
1009                 cERROR(1, "cifs_min_rcv set to maximum (64)");
1010         }
1011
1012         cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1013                                                   cifs_req_cachep);
1014
1015         if (cifs_req_poolp == NULL) {
1016                 kmem_cache_destroy(cifs_req_cachep);
1017                 return -ENOMEM;
1018         }
1019         /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1020         almost all handle based requests (but not write response, nor is it
1021         sufficient for path based requests).  A smaller size would have
1022         been more efficient (compacting multiple slab items on one 4k page)
1023         for the case in which debug was on, but this larger size allows
1024         more SMBs to use small buffer alloc and is still much more
1025         efficient to alloc 1 per page off the slab compared to 17K (5page)
1026         alloc of large cifs buffers even when page debugging is on */
1027         cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
1028                         MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
1029                         NULL);
1030         if (cifs_sm_req_cachep == NULL) {
1031                 mempool_destroy(cifs_req_poolp);
1032                 kmem_cache_destroy(cifs_req_cachep);
1033                 return -ENOMEM;
1034         }
1035
1036         if (cifs_min_small < 2)
1037                 cifs_min_small = 2;
1038         else if (cifs_min_small > 256) {
1039                 cifs_min_small = 256;
1040                 cFYI(1, "cifs_min_small set to maximum (256)");
1041         }
1042
1043         cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1044                                                      cifs_sm_req_cachep);
1045
1046         if (cifs_sm_req_poolp == NULL) {
1047                 mempool_destroy(cifs_req_poolp);
1048                 kmem_cache_destroy(cifs_req_cachep);
1049                 kmem_cache_destroy(cifs_sm_req_cachep);
1050                 return -ENOMEM;
1051         }
1052
1053         return 0;
1054 }
1055
1056 static void
1057 cifs_destroy_request_bufs(void)
1058 {
1059         mempool_destroy(cifs_req_poolp);
1060         kmem_cache_destroy(cifs_req_cachep);
1061         mempool_destroy(cifs_sm_req_poolp);
1062         kmem_cache_destroy(cifs_sm_req_cachep);
1063 }
1064
1065 static int
1066 cifs_init_mids(void)
1067 {
1068         cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
1069                                             sizeof(struct mid_q_entry), 0,
1070                                             SLAB_HWCACHE_ALIGN, NULL);
1071         if (cifs_mid_cachep == NULL)
1072                 return -ENOMEM;
1073
1074         /* 3 is a reasonable minimum number of simultaneous operations */
1075         cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
1076         if (cifs_mid_poolp == NULL) {
1077                 kmem_cache_destroy(cifs_mid_cachep);
1078                 return -ENOMEM;
1079         }
1080
1081         return 0;
1082 }
1083
1084 static void
1085 cifs_destroy_mids(void)
1086 {
1087         mempool_destroy(cifs_mid_poolp);
1088         kmem_cache_destroy(cifs_mid_cachep);
1089 }
1090
1091 static int __init
1092 init_cifs(void)
1093 {
1094         int rc = 0;
1095         cifs_proc_init();
1096         INIT_LIST_HEAD(&cifs_tcp_ses_list);
1097 #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* unused temporarily */
1098         INIT_LIST_HEAD(&GlobalDnotifyReqList);
1099         INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
1100 #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */
1101 /*
1102  *  Initialize Global counters
1103  */
1104         atomic_set(&sesInfoAllocCount, 0);
1105         atomic_set(&tconInfoAllocCount, 0);
1106         atomic_set(&tcpSesAllocCount, 0);
1107         atomic_set(&tcpSesReconnectCount, 0);
1108         atomic_set(&tconInfoReconnectCount, 0);
1109
1110         atomic_set(&bufAllocCount, 0);
1111         atomic_set(&smBufAllocCount, 0);
1112 #ifdef CONFIG_CIFS_STATS2
1113         atomic_set(&totBufAllocCount, 0);
1114         atomic_set(&totSmBufAllocCount, 0);
1115 #endif /* CONFIG_CIFS_STATS2 */
1116
1117         atomic_set(&midCount, 0);
1118         GlobalCurrentXid = 0;
1119         GlobalTotalActiveXid = 0;
1120         GlobalMaxActiveXid = 0;
1121         spin_lock_init(&cifs_tcp_ses_lock);
1122         spin_lock_init(&cifs_file_list_lock);
1123         spin_lock_init(&GlobalMid_Lock);
1124
1125 #ifdef CONFIG_CIFS_SMB2
1126         get_random_bytes(cifs_client_guid, SMB2_CLIENT_GUID_SIZE);
1127 #endif
1128
1129         if (cifs_max_pending < 2) {
1130                 cifs_max_pending = 2;
1131                 cFYI(1, "cifs_max_pending set to min of 2");
1132         } else if (cifs_max_pending > CIFS_MAX_REQ) {
1133                 cifs_max_pending = CIFS_MAX_REQ;
1134                 cFYI(1, "cifs_max_pending set to max of %u", CIFS_MAX_REQ);
1135         }
1136
1137         cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1138         if (!cifsiod_wq) {
1139                 rc = -ENOMEM;
1140                 goto out_clean_proc;
1141         }
1142
1143         rc = cifs_fscache_register();
1144         if (rc)
1145                 goto out_destroy_wq;
1146
1147         rc = cifs_init_inodecache();
1148         if (rc)
1149                 goto out_unreg_fscache;
1150
1151         rc = cifs_init_mids();
1152         if (rc)
1153                 goto out_destroy_inodecache;
1154
1155         rc = cifs_init_request_bufs();
1156         if (rc)
1157                 goto out_destroy_mids;
1158
1159 #ifdef CONFIG_CIFS_UPCALL
1160         rc = register_key_type(&cifs_spnego_key_type);
1161         if (rc)
1162                 goto out_destroy_request_bufs;
1163 #endif /* CONFIG_CIFS_UPCALL */
1164
1165 #ifdef CONFIG_CIFS_ACL
1166         rc = init_cifs_idmap();
1167         if (rc)
1168                 goto out_register_key_type;
1169 #endif /* CONFIG_CIFS_ACL */
1170
1171         rc = register_filesystem(&cifs_fs_type);
1172         if (rc)
1173                 goto out_init_cifs_idmap;
1174
1175         return 0;
1176
1177 out_init_cifs_idmap:
1178 #ifdef CONFIG_CIFS_ACL
1179         exit_cifs_idmap();
1180 out_register_key_type:
1181 #endif
1182 #ifdef CONFIG_CIFS_UPCALL
1183         unregister_key_type(&cifs_spnego_key_type);
1184 out_destroy_request_bufs:
1185 #endif
1186         cifs_destroy_request_bufs();
1187 out_destroy_mids:
1188         cifs_destroy_mids();
1189 out_destroy_inodecache:
1190         cifs_destroy_inodecache();
1191 out_unreg_fscache:
1192         cifs_fscache_unregister();
1193 out_destroy_wq:
1194         destroy_workqueue(cifsiod_wq);
1195 out_clean_proc:
1196         cifs_proc_clean();
1197         return rc;
1198 }
1199
1200 static void __exit
1201 exit_cifs(void)
1202 {
1203         cFYI(DBG2, "exit_cifs");
1204         unregister_filesystem(&cifs_fs_type);
1205         cifs_dfs_release_automount_timer();
1206 #ifdef CONFIG_CIFS_ACL
1207         exit_cifs_idmap();
1208 #endif
1209 #ifdef CONFIG_CIFS_UPCALL
1210         unregister_key_type(&cifs_spnego_key_type);
1211 #endif
1212         cifs_destroy_request_bufs();
1213         cifs_destroy_mids();
1214         cifs_destroy_inodecache();
1215         cifs_fscache_unregister();
1216         destroy_workqueue(cifsiod_wq);
1217         cifs_proc_clean();
1218 }
1219
1220 MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
1221 MODULE_LICENSE("GPL");  /* combination of LGPL + GPL source behaves as GPL */
1222 MODULE_DESCRIPTION
1223     ("VFS to access servers complying with the SNIA CIFS Specification "
1224      "e.g. Samba and Windows");
1225 MODULE_VERSION(CIFS_VERSION);
1226 module_init(init_cifs)
1227 module_exit(exit_cifs)