usb: otg: twl4030-usb: delay status read
[pandora-kernel.git] / fs / cifs / cifsfs.c
1 /*
2  *   fs/cifs/cifsfs.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2008
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *
7  *   Common Internet FileSystem (CIFS) client
8  *
9  *   This library is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU Lesser General Public License as published
11  *   by the Free Software Foundation; either version 2.1 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This library is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
17  *   the GNU Lesser General Public License for more details.
18  *
19  *   You should have received a copy of the GNU Lesser General Public License
20  *   along with this library; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23
24 /* Note that BB means BUGBUG (ie something to fix eventually) */
25
26 #include <linux/module.h>
27 #include <linux/fs.h>
28 #include <linux/mount.h>
29 #include <linux/slab.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/seq_file.h>
33 #include <linux/vfs.h>
34 #include <linux/mempool.h>
35 #include <linux/delay.h>
36 #include <linux/kthread.h>
37 #include <linux/freezer.h>
38 #include <linux/namei.h>
39 #include <net/ipv6.h>
40 #include "cifsfs.h"
41 #include "cifspdu.h"
42 #define DECLARE_GLOBALS_HERE
43 #include "cifsglob.h"
44 #include "cifsproto.h"
45 #include "cifs_debug.h"
46 #include "cifs_fs_sb.h"
47 #include <linux/mm.h>
48 #include <linux/key-type.h>
49 #include "cifs_spnego.h"
50 #include "fscache.h"
51 #define CIFS_MAGIC_NUMBER 0xFF534D42    /* the first four bytes of SMB PDUs */
52
53 int cifsFYI = 0;
54 int cifsERROR = 1;
55 int traceSMB = 0;
56 bool enable_oplocks = true;
57 bool no_serverino_autodisable = false;
58 unsigned int linuxExtEnabled = 1;
59 unsigned int lookupCacheEnabled = 1;
60 unsigned int multiuser_mount = 0;
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 unsigned short echo_retries = 5;
82 module_param(echo_retries, ushort, 0644);
83 MODULE_PARM_DESC(echo_retries, "Number of echo attempts before giving up and "
84                                "reconnecting server. Default: 5. 0 means "
85                                "never reconnect.");
86 module_param(enable_oplocks, bool, 0644);
87 MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks (bool). Default:"
88                                  "y/Y/1");
89 module_param(no_serverino_autodisable, bool, 0644);
90
91 extern mempool_t *cifs_sm_req_poolp;
92 extern mempool_t *cifs_req_poolp;
93 extern mempool_t *cifs_mid_poolp;
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                 inode = NULL;
125                 goto out_no_root;
126         }
127
128         sb->s_root = d_alloc_root(inode);
129
130         if (!sb->s_root) {
131                 rc = -ENOMEM;
132                 goto out_no_root;
133         }
134
135         /* do that *after* d_alloc_root() - we want NULL ->d_op for root here */
136         if (cifs_sb_master_tcon(cifs_sb)->nocase)
137                 sb->s_d_op = &cifs_ci_dentry_ops;
138         else
139                 sb->s_d_op = &cifs_dentry_ops;
140
141 #ifdef CONFIG_CIFS_NFSD_EXPORT
142         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
143                 cFYI(1, "export ops supported");
144                 sb->s_export_op = &cifs_export_ops;
145         }
146 #endif /* CONFIG_CIFS_NFSD_EXPORT */
147
148         return 0;
149
150 out_no_root:
151         cERROR(1, "cifs_read_super: get root inode failed");
152         if (inode)
153                 iput(inode);
154
155         return rc;
156 }
157
158 static void cifs_kill_sb(struct super_block *sb)
159 {
160         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
161         kill_anon_super(sb);
162         cifs_umount(cifs_sb);
163 }
164
165 static int
166 cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
167 {
168         struct super_block *sb = dentry->d_sb;
169         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
170         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
171         int rc = -EOPNOTSUPP;
172         int xid;
173
174         xid = GetXid();
175
176         buf->f_type = CIFS_MAGIC_NUMBER;
177
178         /*
179          * PATH_MAX may be too long - it would presumably be total path,
180          * but note that some servers (includinng Samba 3) have a shorter
181          * maximum path.
182          *
183          * Instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO.
184          */
185         buf->f_namelen = PATH_MAX;
186         buf->f_files = 0;       /* undefined */
187         buf->f_ffree = 0;       /* unlimited */
188
189         /*
190          * We could add a second check for a QFS Unix capability bit
191          */
192         if ((tcon->ses->capabilities & CAP_UNIX) &&
193             (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
194                 rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
195
196         /*
197          * Only need to call the old QFSInfo if failed on newer one,
198          * e.g. by OS/2.
199          **/
200         if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
201                 rc = CIFSSMBQFSInfo(xid, tcon, buf);
202
203         /*
204          * Some old Windows servers also do not support level 103, retry with
205          * older level one if old server failed the previous call or we
206          * bypassed it because we detected that this was an older LANMAN sess
207          */
208         if (rc)
209                 rc = SMBOldQFSInfo(xid, tcon, buf);
210
211         FreeXid(xid);
212         return 0;
213 }
214
215 static int cifs_permission(struct inode *inode, int mask)
216 {
217         struct cifs_sb_info *cifs_sb;
218
219         cifs_sb = CIFS_SB(inode->i_sb);
220
221         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
222                 if ((mask & MAY_EXEC) && !execute_ok(inode))
223                         return -EACCES;
224                 else
225                         return 0;
226         } else /* file mode might have been restricted at mount time
227                 on the client (above and beyond ACL on servers) for
228                 servers which do not support setting and viewing mode bits,
229                 so allowing client to check permissions is useful */
230                 return generic_permission(inode, mask);
231 }
232
233 static struct kmem_cache *cifs_inode_cachep;
234 static struct kmem_cache *cifs_req_cachep;
235 static struct kmem_cache *cifs_mid_cachep;
236 static struct kmem_cache *cifs_sm_req_cachep;
237 mempool_t *cifs_sm_req_poolp;
238 mempool_t *cifs_req_poolp;
239 mempool_t *cifs_mid_poolp;
240
241 static struct inode *
242 cifs_alloc_inode(struct super_block *sb)
243 {
244         struct cifsInodeInfo *cifs_inode;
245         cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
246         if (!cifs_inode)
247                 return NULL;
248         cifs_inode->cifsAttrs = 0x20;   /* default */
249         cifs_inode->time = 0;
250         /* Until the file is open and we have gotten oplock
251         info back from the server, can not assume caching of
252         file data or metadata */
253         cifs_set_oplock_level(cifs_inode, 0);
254         cifs_inode->delete_pending = false;
255         cifs_inode->invalid_mapping = false;
256         cifs_inode->vfs_inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
257         cifs_inode->server_eof = 0;
258         cifs_inode->uniqueid = 0;
259         cifs_inode->createtime = 0;
260
261         /* Can not set i_flags here - they get immediately overwritten
262            to zero by the VFS */
263 /*      cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;*/
264         INIT_LIST_HEAD(&cifs_inode->openFileList);
265         return &cifs_inode->vfs_inode;
266 }
267
268 static void cifs_i_callback(struct rcu_head *head)
269 {
270         struct inode *inode = container_of(head, struct inode, i_rcu);
271         INIT_LIST_HEAD(&inode->i_dentry);
272         kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
273 }
274
275 static void
276 cifs_destroy_inode(struct inode *inode)
277 {
278         call_rcu(&inode->i_rcu, cifs_i_callback);
279 }
280
281 static void
282 cifs_evict_inode(struct inode *inode)
283 {
284         truncate_inode_pages(&inode->i_data, 0);
285         end_writeback(inode);
286         cifs_fscache_release_inode_cookie(inode);
287 }
288
289 static void
290 cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
291 {
292         struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
293         struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
294
295         seq_printf(s, ",addr=");
296
297         switch (server->dstaddr.ss_family) {
298         case AF_INET:
299                 seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
300                 break;
301         case AF_INET6:
302                 seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
303                 if (sa6->sin6_scope_id)
304                         seq_printf(s, "%%%u", sa6->sin6_scope_id);
305                 break;
306         default:
307                 seq_printf(s, "(unknown)");
308         }
309 }
310
311 static void
312 cifs_show_security(struct seq_file *s, struct TCP_Server_Info *server)
313 {
314         seq_printf(s, ",sec=");
315
316         switch (server->secType) {
317         case LANMAN:
318                 seq_printf(s, "lanman");
319                 break;
320         case NTLMv2:
321                 seq_printf(s, "ntlmv2");
322                 break;
323         case NTLM:
324                 seq_printf(s, "ntlm");
325                 break;
326         case Kerberos:
327                 seq_printf(s, "krb5");
328                 break;
329         case RawNTLMSSP:
330                 seq_printf(s, "ntlmssp");
331                 break;
332         default:
333                 /* shouldn't ever happen */
334                 seq_printf(s, "unknown");
335                 break;
336         }
337
338         if (server->sec_mode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
339                 seq_printf(s, "i");
340 }
341
342 /*
343  * cifs_show_options() is for displaying mount options in /proc/mounts.
344  * Not all settable options are displayed but most of the important
345  * ones are.
346  */
347 static int
348 cifs_show_options(struct seq_file *s, struct vfsmount *m)
349 {
350         struct cifs_sb_info *cifs_sb = CIFS_SB(m->mnt_sb);
351         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
352         struct sockaddr *srcaddr;
353         srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
354
355         cifs_show_security(s, tcon->ses->server);
356
357         seq_printf(s, ",unc=%s", tcon->treeName);
358
359         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)
360                 seq_printf(s, ",multiuser");
361         else if (tcon->ses->user_name)
362                 seq_printf(s, ",username=%s", tcon->ses->user_name);
363
364         if (tcon->ses->domainName)
365                 seq_printf(s, ",domain=%s", tcon->ses->domainName);
366
367         if (srcaddr->sa_family != AF_UNSPEC) {
368                 struct sockaddr_in *saddr4;
369                 struct sockaddr_in6 *saddr6;
370                 saddr4 = (struct sockaddr_in *)srcaddr;
371                 saddr6 = (struct sockaddr_in6 *)srcaddr;
372                 if (srcaddr->sa_family == AF_INET6)
373                         seq_printf(s, ",srcaddr=%pI6c",
374                                    &saddr6->sin6_addr);
375                 else if (srcaddr->sa_family == AF_INET)
376                         seq_printf(s, ",srcaddr=%pI4",
377                                    &saddr4->sin_addr.s_addr);
378                 else
379                         seq_printf(s, ",srcaddr=BAD-AF:%i",
380                                    (int)(srcaddr->sa_family));
381         }
382
383         seq_printf(s, ",uid=%d", cifs_sb->mnt_uid);
384         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
385                 seq_printf(s, ",forceuid");
386         else
387                 seq_printf(s, ",noforceuid");
388
389         seq_printf(s, ",gid=%d", cifs_sb->mnt_gid);
390         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
391                 seq_printf(s, ",forcegid");
392         else
393                 seq_printf(s, ",noforcegid");
394
395         cifs_show_address(s, tcon->ses->server);
396
397         if (!tcon->unix_ext)
398                 seq_printf(s, ",file_mode=0%o,dir_mode=0%o",
399                                            cifs_sb->mnt_file_mode,
400                                            cifs_sb->mnt_dir_mode);
401         if (tcon->seal)
402                 seq_printf(s, ",seal");
403         if (tcon->nocase)
404                 seq_printf(s, ",nocase");
405         if (tcon->retry)
406                 seq_printf(s, ",hard");
407         if (tcon->unix_ext)
408                 seq_printf(s, ",unix");
409         else
410                 seq_printf(s, ",nounix");
411         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
412                 seq_printf(s, ",posixpaths");
413         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
414                 seq_printf(s, ",setuids");
415         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
416                 seq_printf(s, ",serverino");
417         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
418                 seq_printf(s, ",rwpidforward");
419         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
420                 seq_printf(s, ",forcemand");
421         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
422                 seq_printf(s, ",directio");
423         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
424                 seq_printf(s, ",nouser_xattr");
425         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
426                 seq_printf(s, ",mapchars");
427         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
428                 seq_printf(s, ",sfu");
429         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
430                 seq_printf(s, ",nobrl");
431         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
432                 seq_printf(s, ",cifsacl");
433         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
434                 seq_printf(s, ",dynperm");
435         if (m->mnt_sb->s_flags & MS_POSIXACL)
436                 seq_printf(s, ",acl");
437         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
438                 seq_printf(s, ",mfsymlinks");
439         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
440                 seq_printf(s, ",fsc");
441         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
442                 seq_printf(s, ",nostrictsync");
443         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
444                 seq_printf(s, ",noperm");
445         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
446                 seq_printf(s, ",strictcache");
447
448         seq_printf(s, ",rsize=%d", cifs_sb->rsize);
449         seq_printf(s, ",wsize=%d", cifs_sb->wsize);
450         /* convert actimeo and display it in seconds */
451                 seq_printf(s, ",actimeo=%lu", cifs_sb->actimeo / HZ);
452
453         return 0;
454 }
455
456 static void cifs_umount_begin(struct super_block *sb)
457 {
458         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
459         struct cifs_tcon *tcon;
460
461         if (cifs_sb == NULL)
462                 return;
463
464         tcon = cifs_sb_master_tcon(cifs_sb);
465
466         spin_lock(&cifs_tcp_ses_lock);
467         if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
468                 /* we have other mounts to same share or we have
469                    already tried to force umount this and woken up
470                    all waiting network requests, nothing to do */
471                 spin_unlock(&cifs_tcp_ses_lock);
472                 return;
473         } else if (tcon->tc_count == 1)
474                 tcon->tidStatus = CifsExiting;
475         spin_unlock(&cifs_tcp_ses_lock);
476
477         /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
478         /* cancel_notify_requests(tcon); */
479         if (tcon->ses && tcon->ses->server) {
480                 cFYI(1, "wake up tasks now - umount begin not complete");
481                 wake_up_all(&tcon->ses->server->request_q);
482                 wake_up_all(&tcon->ses->server->response_q);
483                 msleep(1); /* yield */
484                 /* we have to kick the requests once more */
485                 wake_up_all(&tcon->ses->server->response_q);
486                 msleep(1);
487         }
488
489         return;
490 }
491
492 #ifdef CONFIG_CIFS_STATS2
493 static int cifs_show_stats(struct seq_file *s, struct vfsmount *mnt)
494 {
495         /* BB FIXME */
496         return 0;
497 }
498 #endif
499
500 static int cifs_remount(struct super_block *sb, int *flags, char *data)
501 {
502         *flags |= MS_NODIRATIME;
503         return 0;
504 }
505
506 static int cifs_drop_inode(struct inode *inode)
507 {
508         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
509
510         /* no serverino => unconditional eviction */
511         return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
512                 generic_drop_inode(inode);
513 }
514
515 static const struct super_operations cifs_super_ops = {
516         .statfs = cifs_statfs,
517         .alloc_inode = cifs_alloc_inode,
518         .destroy_inode = cifs_destroy_inode,
519         .drop_inode     = cifs_drop_inode,
520         .evict_inode    = cifs_evict_inode,
521 /*      .delete_inode   = cifs_delete_inode,  */  /* Do not need above
522         function unless later we add lazy close of inodes or unless the
523         kernel forgets to call us with the same number of releases (closes)
524         as opens */
525         .show_options = cifs_show_options,
526         .umount_begin   = cifs_umount_begin,
527         .remount_fs = cifs_remount,
528 #ifdef CONFIG_CIFS_STATS2
529         .show_stats = cifs_show_stats,
530 #endif
531 };
532
533 /*
534  * Get root dentry from superblock according to prefix path mount option.
535  * Return dentry with refcount + 1 on success and NULL otherwise.
536  */
537 static struct dentry *
538 cifs_get_root(struct smb_vol *vol, struct super_block *sb)
539 {
540         struct dentry *dentry;
541         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
542         char *full_path = NULL;
543         char *s, *p;
544         char sep;
545
546         full_path = cifs_build_path_to_root(vol, cifs_sb,
547                                             cifs_sb_master_tcon(cifs_sb));
548         if (full_path == NULL)
549                 return ERR_PTR(-ENOMEM);
550
551         cFYI(1, "Get root dentry for %s", full_path);
552
553         sep = CIFS_DIR_SEP(cifs_sb);
554         dentry = dget(sb->s_root);
555         p = s = full_path;
556
557         do {
558                 struct inode *dir = dentry->d_inode;
559                 struct dentry *child;
560
561                 if (!dir) {
562                         dput(dentry);
563                         dentry = ERR_PTR(-ENOENT);
564                         break;
565                 }
566
567                 /* skip separators */
568                 while (*s == sep)
569                         s++;
570                 if (!*s)
571                         break;
572                 p = s++;
573                 /* next separator */
574                 while (*s && *s != sep)
575                         s++;
576
577                 mutex_lock(&dir->i_mutex);
578                 child = lookup_one_len(p, dentry, s - p);
579                 mutex_unlock(&dir->i_mutex);
580                 dput(dentry);
581                 dentry = child;
582         } while (!IS_ERR(dentry));
583         kfree(full_path);
584         return dentry;
585 }
586
587 static int cifs_set_super(struct super_block *sb, void *data)
588 {
589         struct cifs_mnt_data *mnt_data = data;
590         sb->s_fs_info = mnt_data->cifs_sb;
591         return set_anon_super(sb, NULL);
592 }
593
594 static struct dentry *
595 cifs_do_mount(struct file_system_type *fs_type,
596               int flags, const char *dev_name, void *data)
597 {
598         int rc;
599         struct super_block *sb;
600         struct cifs_sb_info *cifs_sb;
601         struct smb_vol *volume_info;
602         struct cifs_mnt_data mnt_data;
603         struct dentry *root;
604
605         cFYI(1, "Devname: %s flags: %d ", dev_name, flags);
606
607         volume_info = cifs_get_volume_info((char *)data, dev_name);
608         if (IS_ERR(volume_info))
609                 return ERR_CAST(volume_info);
610
611         cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
612         if (cifs_sb == NULL) {
613                 root = ERR_PTR(-ENOMEM);
614                 goto out_nls;
615         }
616
617         cifs_sb->mountdata = kstrndup(data, PAGE_SIZE, GFP_KERNEL);
618         if (cifs_sb->mountdata == NULL) {
619                 root = ERR_PTR(-ENOMEM);
620                 goto out_cifs_sb;
621         }
622
623         cifs_setup_cifs_sb(volume_info, cifs_sb);
624
625         rc = cifs_mount(cifs_sb, volume_info);
626         if (rc) {
627                 if (!(flags & MS_SILENT))
628                         cERROR(1, "cifs_mount failed w/return code = %d", rc);
629                 root = ERR_PTR(rc);
630                 goto out_mountdata;
631         }
632
633         mnt_data.vol = volume_info;
634         mnt_data.cifs_sb = cifs_sb;
635         mnt_data.flags = flags;
636
637         sb = sget(fs_type, cifs_match_super, cifs_set_super, &mnt_data);
638         if (IS_ERR(sb)) {
639                 root = ERR_CAST(sb);
640                 cifs_umount(cifs_sb);
641                 goto out;
642         }
643
644         if (sb->s_root) {
645                 cFYI(1, "Use existing superblock");
646                 cifs_umount(cifs_sb);
647         } else {
648                 sb->s_flags = flags;
649                 /* BB should we make this contingent on mount parm? */
650                 sb->s_flags |= MS_NODIRATIME | MS_NOATIME;
651
652                 rc = cifs_read_super(sb);
653                 if (rc) {
654                         root = ERR_PTR(rc);
655                         goto out_super;
656                 }
657
658                 sb->s_flags |= MS_ACTIVE;
659         }
660
661         root = cifs_get_root(volume_info, sb);
662         if (IS_ERR(root))
663                 goto out_super;
664
665         cFYI(1, "dentry root is: %p", root);
666         goto out;
667
668 out_super:
669         deactivate_locked_super(sb);
670 out:
671         cifs_cleanup_volume_info(volume_info);
672         return root;
673
674 out_mountdata:
675         kfree(cifs_sb->mountdata);
676 out_cifs_sb:
677         kfree(cifs_sb);
678 out_nls:
679         unload_nls(volume_info->local_nls);
680         goto out;
681 }
682
683 static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
684                                    unsigned long nr_segs, loff_t pos)
685 {
686         struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
687         ssize_t written;
688         int rc;
689
690         written = generic_file_aio_write(iocb, iov, nr_segs, pos);
691
692         if (CIFS_I(inode)->clientCanCacheAll)
693                 return written;
694
695         rc = filemap_fdatawrite(inode->i_mapping);
696         if (rc)
697                 cFYI(1, "cifs_file_aio_write: %d rc on %p inode", rc, inode);
698
699         return written;
700 }
701
702 static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
703 {
704         /*
705          * origin == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
706          * the cached file length
707          */
708         if (origin != SEEK_SET && origin != SEEK_CUR) {
709                 int rc;
710                 struct inode *inode = file->f_path.dentry->d_inode;
711
712                 /*
713                  * We need to be sure that all dirty pages are written and the
714                  * server has the newest file length.
715                  */
716                 if (!CIFS_I(inode)->clientCanCacheRead && inode->i_mapping &&
717                     inode->i_mapping->nrpages != 0) {
718                         rc = filemap_fdatawait(inode->i_mapping);
719                         if (rc) {
720                                 mapping_set_error(inode->i_mapping, rc);
721                                 return rc;
722                         }
723                 }
724                 /*
725                  * Some applications poll for the file length in this strange
726                  * way so we must seek to end on non-oplocked files by
727                  * setting the revalidate time to zero.
728                  */
729                 CIFS_I(inode)->time = 0;
730
731                 rc = cifs_revalidate_file_attr(file);
732                 if (rc < 0)
733                         return (loff_t)rc;
734         }
735         return generic_file_llseek(file, offset, origin);
736 }
737
738 static int cifs_setlease(struct file *file, long arg, struct file_lock **lease)
739 {
740         /* note that this is called by vfs setlease with lock_flocks held
741            to protect *lease from going away */
742         struct inode *inode = file->f_path.dentry->d_inode;
743         struct cifsFileInfo *cfile = file->private_data;
744
745         if (!(S_ISREG(inode->i_mode)))
746                 return -EINVAL;
747
748         /* check if file is oplocked */
749         if (((arg == F_RDLCK) &&
750                 (CIFS_I(inode)->clientCanCacheRead)) ||
751             ((arg == F_WRLCK) &&
752                 (CIFS_I(inode)->clientCanCacheAll)))
753                 return generic_setlease(file, arg, lease);
754         else if (tlink_tcon(cfile->tlink)->local_lease &&
755                  !CIFS_I(inode)->clientCanCacheRead)
756                 /* If the server claims to support oplock on this
757                    file, then we still need to check oplock even
758                    if the local_lease mount option is set, but there
759                    are servers which do not support oplock for which
760                    this mount option may be useful if the user
761                    knows that the file won't be changed on the server
762                    by anyone else */
763                 return generic_setlease(file, arg, lease);
764         else
765                 return -EAGAIN;
766 }
767
768 struct file_system_type cifs_fs_type = {
769         .owner = THIS_MODULE,
770         .name = "cifs",
771         .mount = cifs_do_mount,
772         .kill_sb = cifs_kill_sb,
773         /*  .fs_flags */
774 };
775 const struct inode_operations cifs_dir_inode_ops = {
776         .create = cifs_create,
777         .lookup = cifs_lookup,
778         .getattr = cifs_getattr,
779         .unlink = cifs_unlink,
780         .link = cifs_hardlink,
781         .mkdir = cifs_mkdir,
782         .rmdir = cifs_rmdir,
783         .rename = cifs_rename,
784         .permission = cifs_permission,
785 /*      revalidate:cifs_revalidate,   */
786         .setattr = cifs_setattr,
787         .symlink = cifs_symlink,
788         .mknod   = cifs_mknod,
789 #ifdef CONFIG_CIFS_XATTR
790         .setxattr = cifs_setxattr,
791         .getxattr = cifs_getxattr,
792         .listxattr = cifs_listxattr,
793         .removexattr = cifs_removexattr,
794 #endif
795 };
796
797 const struct inode_operations cifs_file_inode_ops = {
798 /*      revalidate:cifs_revalidate, */
799         .setattr = cifs_setattr,
800         .getattr = cifs_getattr, /* do we need this anymore? */
801         .rename = cifs_rename,
802         .permission = cifs_permission,
803 #ifdef CONFIG_CIFS_XATTR
804         .setxattr = cifs_setxattr,
805         .getxattr = cifs_getxattr,
806         .listxattr = cifs_listxattr,
807         .removexattr = cifs_removexattr,
808 #endif
809 };
810
811 const struct inode_operations cifs_symlink_inode_ops = {
812         .readlink = generic_readlink,
813         .follow_link = cifs_follow_link,
814         .put_link = cifs_put_link,
815         .permission = cifs_permission,
816         /* BB add the following two eventually */
817         /* revalidate: cifs_revalidate,
818            setattr:    cifs_notify_change, *//* BB do we need notify change */
819 #ifdef CONFIG_CIFS_XATTR
820         .setxattr = cifs_setxattr,
821         .getxattr = cifs_getxattr,
822         .listxattr = cifs_listxattr,
823         .removexattr = cifs_removexattr,
824 #endif
825 };
826
827 const struct file_operations cifs_file_ops = {
828         .read = do_sync_read,
829         .write = do_sync_write,
830         .aio_read = generic_file_aio_read,
831         .aio_write = cifs_file_aio_write,
832         .open = cifs_open,
833         .release = cifs_close,
834         .lock = cifs_lock,
835         .fsync = cifs_fsync,
836         .flush = cifs_flush,
837         .mmap  = cifs_file_mmap,
838         .splice_read = generic_file_splice_read,
839         .llseek = cifs_llseek,
840 #ifdef CONFIG_CIFS_POSIX
841         .unlocked_ioctl = cifs_ioctl,
842 #endif /* CONFIG_CIFS_POSIX */
843         .setlease = cifs_setlease,
844 };
845
846 const struct file_operations cifs_file_strict_ops = {
847         .read = do_sync_read,
848         .write = do_sync_write,
849         .aio_read = cifs_strict_readv,
850         .aio_write = cifs_strict_writev,
851         .open = cifs_open,
852         .release = cifs_close,
853         .lock = cifs_lock,
854         .fsync = cifs_strict_fsync,
855         .flush = cifs_flush,
856         .mmap = cifs_file_strict_mmap,
857         .splice_read = generic_file_splice_read,
858         .llseek = cifs_llseek,
859 #ifdef CONFIG_CIFS_POSIX
860         .unlocked_ioctl = cifs_ioctl,
861 #endif /* CONFIG_CIFS_POSIX */
862         .setlease = cifs_setlease,
863 };
864
865 const struct file_operations cifs_file_direct_ops = {
866         /* BB reevaluate whether they can be done with directio, no cache */
867         .read = do_sync_read,
868         .write = do_sync_write,
869         .aio_read = cifs_user_readv,
870         .aio_write = cifs_user_writev,
871         .open = cifs_open,
872         .release = cifs_close,
873         .lock = cifs_lock,
874         .fsync = cifs_fsync,
875         .flush = cifs_flush,
876         .mmap = cifs_file_mmap,
877         .splice_read = generic_file_splice_read,
878 #ifdef CONFIG_CIFS_POSIX
879         .unlocked_ioctl  = cifs_ioctl,
880 #endif /* CONFIG_CIFS_POSIX */
881         .llseek = cifs_llseek,
882         .setlease = cifs_setlease,
883 };
884
885 const struct file_operations cifs_file_nobrl_ops = {
886         .read = do_sync_read,
887         .write = do_sync_write,
888         .aio_read = generic_file_aio_read,
889         .aio_write = cifs_file_aio_write,
890         .open = cifs_open,
891         .release = cifs_close,
892         .fsync = cifs_fsync,
893         .flush = cifs_flush,
894         .mmap  = cifs_file_mmap,
895         .splice_read = generic_file_splice_read,
896         .llseek = cifs_llseek,
897 #ifdef CONFIG_CIFS_POSIX
898         .unlocked_ioctl = cifs_ioctl,
899 #endif /* CONFIG_CIFS_POSIX */
900         .setlease = cifs_setlease,
901 };
902
903 const struct file_operations cifs_file_strict_nobrl_ops = {
904         .read = do_sync_read,
905         .write = do_sync_write,
906         .aio_read = cifs_strict_readv,
907         .aio_write = cifs_strict_writev,
908         .open = cifs_open,
909         .release = cifs_close,
910         .fsync = cifs_strict_fsync,
911         .flush = cifs_flush,
912         .mmap = cifs_file_strict_mmap,
913         .splice_read = generic_file_splice_read,
914         .llseek = cifs_llseek,
915 #ifdef CONFIG_CIFS_POSIX
916         .unlocked_ioctl = cifs_ioctl,
917 #endif /* CONFIG_CIFS_POSIX */
918         .setlease = cifs_setlease,
919 };
920
921 const struct file_operations cifs_file_direct_nobrl_ops = {
922         /* BB reevaluate whether they can be done with directio, no cache */
923         .read = do_sync_read,
924         .write = do_sync_write,
925         .aio_read = cifs_user_readv,
926         .aio_write = cifs_user_writev,
927         .open = cifs_open,
928         .release = cifs_close,
929         .fsync = cifs_fsync,
930         .flush = cifs_flush,
931         .mmap = cifs_file_mmap,
932         .splice_read = generic_file_splice_read,
933 #ifdef CONFIG_CIFS_POSIX
934         .unlocked_ioctl  = cifs_ioctl,
935 #endif /* CONFIG_CIFS_POSIX */
936         .llseek = cifs_llseek,
937         .setlease = cifs_setlease,
938 };
939
940 const struct file_operations cifs_dir_ops = {
941         .readdir = cifs_readdir,
942         .release = cifs_closedir,
943         .read    = generic_read_dir,
944         .unlocked_ioctl  = cifs_ioctl,
945         .llseek = generic_file_llseek,
946 };
947
948 static void
949 cifs_init_once(void *inode)
950 {
951         struct cifsInodeInfo *cifsi = inode;
952
953         inode_init_once(&cifsi->vfs_inode);
954         INIT_LIST_HEAD(&cifsi->llist);
955         mutex_init(&cifsi->lock_mutex);
956 }
957
958 static int
959 cifs_init_inodecache(void)
960 {
961         cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
962                                               sizeof(struct cifsInodeInfo),
963                                               0, (SLAB_RECLAIM_ACCOUNT|
964                                                 SLAB_MEM_SPREAD),
965                                               cifs_init_once);
966         if (cifs_inode_cachep == NULL)
967                 return -ENOMEM;
968
969         return 0;
970 }
971
972 static void
973 cifs_destroy_inodecache(void)
974 {
975         kmem_cache_destroy(cifs_inode_cachep);
976 }
977
978 static int
979 cifs_init_request_bufs(void)
980 {
981         if (CIFSMaxBufSize < 8192) {
982         /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
983         Unicode path name has to fit in any SMB/CIFS path based frames */
984                 CIFSMaxBufSize = 8192;
985         } else if (CIFSMaxBufSize > 1024*127) {
986                 CIFSMaxBufSize = 1024 * 127;
987         } else {
988                 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
989         }
990 /*      cERROR(1, "CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize); */
991         cifs_req_cachep = kmem_cache_create("cifs_request",
992                                             CIFSMaxBufSize +
993                                             MAX_CIFS_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         rc = cifs_fscache_register();
1127         if (rc)
1128                 goto out_clean_proc;
1129
1130         rc = cifs_init_inodecache();
1131         if (rc)
1132                 goto out_unreg_fscache;
1133
1134         rc = cifs_init_mids();
1135         if (rc)
1136                 goto out_destroy_inodecache;
1137
1138         rc = cifs_init_request_bufs();
1139         if (rc)
1140                 goto out_destroy_mids;
1141
1142 #ifdef CONFIG_CIFS_UPCALL
1143         rc = register_key_type(&cifs_spnego_key_type);
1144         if (rc)
1145                 goto out_destroy_request_bufs;
1146 #endif /* CONFIG_CIFS_UPCALL */
1147
1148 #ifdef CONFIG_CIFS_ACL
1149         rc = init_cifs_idmap();
1150         if (rc)
1151                 goto out_register_key_type;
1152 #endif /* CONFIG_CIFS_ACL */
1153
1154         rc = register_filesystem(&cifs_fs_type);
1155         if (rc)
1156                 goto out_init_cifs_idmap;
1157
1158         return 0;
1159
1160 out_init_cifs_idmap:
1161 #ifdef CONFIG_CIFS_ACL
1162         exit_cifs_idmap();
1163 out_register_key_type:
1164 #endif
1165 #ifdef CONFIG_CIFS_UPCALL
1166         unregister_key_type(&cifs_spnego_key_type);
1167 out_destroy_request_bufs:
1168 #endif
1169         cifs_destroy_request_bufs();
1170 out_destroy_mids:
1171         cifs_destroy_mids();
1172 out_destroy_inodecache:
1173         cifs_destroy_inodecache();
1174 out_unreg_fscache:
1175         cifs_fscache_unregister();
1176 out_clean_proc:
1177         cifs_proc_clean();
1178         return rc;
1179 }
1180
1181 static void __exit
1182 exit_cifs(void)
1183 {
1184         cFYI(DBG2, "exit_cifs");
1185         cifs_proc_clean();
1186         cifs_fscache_unregister();
1187 #ifdef CONFIG_CIFS_DFS_UPCALL
1188         cifs_dfs_release_automount_timer();
1189 #endif
1190 #ifdef CONFIG_CIFS_ACL
1191         cifs_destroy_idmaptrees();
1192         exit_cifs_idmap();
1193 #endif
1194 #ifdef CONFIG_CIFS_UPCALL
1195         unregister_key_type(&cifs_spnego_key_type);
1196 #endif
1197         unregister_filesystem(&cifs_fs_type);
1198         cifs_destroy_inodecache();
1199         cifs_destroy_mids();
1200         cifs_destroy_request_bufs();
1201 }
1202
1203 MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
1204 MODULE_LICENSE("GPL");  /* combination of LGPL + GPL source behaves as GPL */
1205 MODULE_DESCRIPTION
1206     ("VFS to access servers complying with the SNIA CIFS Specification "
1207      "e.g. Samba and Windows");
1208 MODULE_VERSION(CIFS_VERSION);
1209 module_init(init_cifs)
1210 module_exit(exit_cifs)