linux/fs/cifs/cifsfs.c
<<
>>
Prefs
   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 <linux/uuid.h>
  41#include <linux/xattr.h>
  42#include <net/ipv6.h>
  43#include "cifsfs.h"
  44#include "cifspdu.h"
  45#define DECLARE_GLOBALS_HERE
  46#include "cifsglob.h"
  47#include "cifsproto.h"
  48#include "cifs_debug.h"
  49#include "cifs_fs_sb.h"
  50#include <linux/mm.h>
  51#include <linux/key-type.h>
  52#include "cifs_spnego.h"
  53#include "fscache.h"
  54#include "smb2pdu.h"
  55#ifdef CONFIG_CIFS_DFS_UPCALL
  56#include "dfs_cache.h"
  57#endif
  58
  59int cifsFYI = 0;
  60bool traceSMB;
  61bool enable_oplocks = true;
  62bool linuxExtEnabled = true;
  63bool lookupCacheEnabled = true;
  64bool disable_legacy_dialects; /* false by default */
  65unsigned int global_secflags = CIFSSEC_DEF;
  66/* unsigned int ntlmv2_support = 0; */
  67unsigned int sign_CIFS_PDUs = 1;
  68static const struct super_operations cifs_super_ops;
  69unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
  70module_param(CIFSMaxBufSize, uint, 0444);
  71MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header) "
  72                                 "for CIFS requests. "
  73                                 "Default: 16384 Range: 8192 to 130048");
  74unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
  75module_param(cifs_min_rcv, uint, 0444);
  76MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
  77                                "1 to 64");
  78unsigned int cifs_min_small = 30;
  79module_param(cifs_min_small, uint, 0444);
  80MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
  81                                 "Range: 2 to 256");
  82unsigned int cifs_max_pending = CIFS_MAX_REQ;
  83module_param(cifs_max_pending, uint, 0444);
  84MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server for "
  85                                   "CIFS/SMB1 dialect (N/A for SMB3) "
  86                                   "Default: 32767 Range: 2 to 32767.");
  87#ifdef CONFIG_CIFS_STATS2
  88unsigned int slow_rsp_threshold = 1;
  89module_param(slow_rsp_threshold, uint, 0644);
  90MODULE_PARM_DESC(slow_rsp_threshold, "Amount of time (in seconds) to wait "
  91                                   "before logging that a response is delayed. "
  92                                   "Default: 1 (if set to 0 disables msg).");
  93#endif /* STATS2 */
  94
  95module_param(enable_oplocks, bool, 0644);
  96MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1");
  97
  98module_param(disable_legacy_dialects, bool, 0644);
  99MODULE_PARM_DESC(disable_legacy_dialects, "To improve security it may be "
 100                                  "helpful to restrict the ability to "
 101                                  "override the default dialects (SMB2.1, "
 102                                  "SMB3 and SMB3.02) on mount with old "
 103                                  "dialects (CIFS/SMB1 and SMB2) since "
 104                                  "vers=1.0 (CIFS/SMB1) and vers=2.0 are weaker"
 105                                  " and less secure. Default: n/N/0");
 106
 107extern mempool_t *cifs_sm_req_poolp;
 108extern mempool_t *cifs_req_poolp;
 109extern mempool_t *cifs_mid_poolp;
 110
 111struct workqueue_struct *cifsiod_wq;
 112struct workqueue_struct *cifsoplockd_wq;
 113__u32 cifs_lock_secret;
 114
 115/*
 116 * Bumps refcount for cifs super block.
 117 * Note that it should be only called if a referece to VFS super block is
 118 * already held, e.g. in open-type syscalls context. Otherwise it can race with
 119 * atomic_dec_and_test in deactivate_locked_super.
 120 */
 121void
 122cifs_sb_active(struct super_block *sb)
 123{
 124        struct cifs_sb_info *server = CIFS_SB(sb);
 125
 126        if (atomic_inc_return(&server->active) == 1)
 127                atomic_inc(&sb->s_active);
 128}
 129
 130void
 131cifs_sb_deactive(struct super_block *sb)
 132{
 133        struct cifs_sb_info *server = CIFS_SB(sb);
 134
 135        if (atomic_dec_and_test(&server->active))
 136                deactivate_super(sb);
 137}
 138
 139static int
 140cifs_read_super(struct super_block *sb)
 141{
 142        struct inode *inode;
 143        struct cifs_sb_info *cifs_sb;
 144        struct cifs_tcon *tcon;
 145        int rc = 0;
 146
 147        cifs_sb = CIFS_SB(sb);
 148        tcon = cifs_sb_master_tcon(cifs_sb);
 149
 150        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
 151                sb->s_flags |= SB_POSIXACL;
 152
 153        if (tcon->snapshot_time)
 154                sb->s_flags |= SB_RDONLY;
 155
 156        if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files)
 157                sb->s_maxbytes = MAX_LFS_FILESIZE;
 158        else
 159                sb->s_maxbytes = MAX_NON_LFS;
 160
 161        /* BB FIXME fix time_gran to be larger for LANMAN sessions */
 162        sb->s_time_gran = 100;
 163
 164        sb->s_magic = CIFS_MAGIC_NUMBER;
 165        sb->s_op = &cifs_super_ops;
 166        sb->s_xattr = cifs_xattr_handlers;
 167        rc = super_setup_bdi(sb);
 168        if (rc)
 169                goto out_no_root;
 170        /* tune readahead according to rsize */
 171        sb->s_bdi->ra_pages = cifs_sb->rsize / PAGE_SIZE;
 172
 173        sb->s_blocksize = CIFS_MAX_MSGSIZE;
 174        sb->s_blocksize_bits = 14;      /* default 2**14 = CIFS_MAX_MSGSIZE */
 175        inode = cifs_root_iget(sb);
 176
 177        if (IS_ERR(inode)) {
 178                rc = PTR_ERR(inode);
 179                goto out_no_root;
 180        }
 181
 182        if (tcon->nocase)
 183                sb->s_d_op = &cifs_ci_dentry_ops;
 184        else
 185                sb->s_d_op = &cifs_dentry_ops;
 186
 187        sb->s_root = d_make_root(inode);
 188        if (!sb->s_root) {
 189                rc = -ENOMEM;
 190                goto out_no_root;
 191        }
 192
 193#ifdef CONFIG_CIFS_NFSD_EXPORT
 194        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
 195                cifs_dbg(FYI, "export ops supported\n");
 196                sb->s_export_op = &cifs_export_ops;
 197        }
 198#endif /* CONFIG_CIFS_NFSD_EXPORT */
 199
 200        return 0;
 201
 202out_no_root:
 203        cifs_dbg(VFS, "%s: get root inode failed\n", __func__);
 204        return rc;
 205}
 206
 207static void cifs_kill_sb(struct super_block *sb)
 208{
 209        struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 210        kill_anon_super(sb);
 211        cifs_umount(cifs_sb);
 212}
 213
 214static int
 215cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
 216{
 217        struct super_block *sb = dentry->d_sb;
 218        struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 219        struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
 220        struct TCP_Server_Info *server = tcon->ses->server;
 221        unsigned int xid;
 222        int rc = 0;
 223
 224        xid = get_xid();
 225
 226        if (le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength) > 0)
 227                buf->f_namelen =
 228                       le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength);
 229        else
 230                buf->f_namelen = PATH_MAX;
 231
 232        buf->f_fsid.val[0] = tcon->vol_serial_number;
 233        /* are using part of create time for more randomness, see man statfs */
 234        buf->f_fsid.val[1] =  (int)le64_to_cpu(tcon->vol_create_time);
 235
 236        buf->f_files = 0;       /* undefined */
 237        buf->f_ffree = 0;       /* unlimited */
 238
 239        if (server->ops->queryfs)
 240                rc = server->ops->queryfs(xid, tcon, buf);
 241
 242        free_xid(xid);
 243        return 0;
 244}
 245
 246static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
 247{
 248        struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
 249        struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
 250        struct TCP_Server_Info *server = tcon->ses->server;
 251
 252        if (server->ops->fallocate)
 253                return server->ops->fallocate(file, tcon, mode, off, len);
 254
 255        return -EOPNOTSUPP;
 256}
 257
 258static int cifs_permission(struct inode *inode, int mask)
 259{
 260        struct cifs_sb_info *cifs_sb;
 261
 262        cifs_sb = CIFS_SB(inode->i_sb);
 263
 264        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
 265                if ((mask & MAY_EXEC) && !execute_ok(inode))
 266                        return -EACCES;
 267                else
 268                        return 0;
 269        } else /* file mode might have been restricted at mount time
 270                on the client (above and beyond ACL on servers) for
 271                servers which do not support setting and viewing mode bits,
 272                so allowing client to check permissions is useful */
 273                return generic_permission(inode, mask);
 274}
 275
 276static struct kmem_cache *cifs_inode_cachep;
 277static struct kmem_cache *cifs_req_cachep;
 278static struct kmem_cache *cifs_mid_cachep;
 279static struct kmem_cache *cifs_sm_req_cachep;
 280mempool_t *cifs_sm_req_poolp;
 281mempool_t *cifs_req_poolp;
 282mempool_t *cifs_mid_poolp;
 283
 284static struct inode *
 285cifs_alloc_inode(struct super_block *sb)
 286{
 287        struct cifsInodeInfo *cifs_inode;
 288        cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
 289        if (!cifs_inode)
 290                return NULL;
 291        cifs_inode->cifsAttrs = 0x20;   /* default */
 292        cifs_inode->time = 0;
 293        /*
 294         * Until the file is open and we have gotten oplock info back from the
 295         * server, can not assume caching of file data or metadata.
 296         */
 297        cifs_set_oplock_level(cifs_inode, 0);
 298        cifs_inode->flags = 0;
 299        spin_lock_init(&cifs_inode->writers_lock);
 300        cifs_inode->writers = 0;
 301        cifs_inode->vfs_inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
 302        cifs_inode->server_eof = 0;
 303        cifs_inode->uniqueid = 0;
 304        cifs_inode->createtime = 0;
 305        cifs_inode->epoch = 0;
 306        spin_lock_init(&cifs_inode->open_file_lock);
 307        generate_random_uuid(cifs_inode->lease_key);
 308
 309        /*
 310         * Can not set i_flags here - they get immediately overwritten to zero
 311         * by the VFS.
 312         */
 313        /* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME; */
 314        INIT_LIST_HEAD(&cifs_inode->openFileList);
 315        INIT_LIST_HEAD(&cifs_inode->llist);
 316        return &cifs_inode->vfs_inode;
 317}
 318
 319static void cifs_i_callback(struct rcu_head *head)
 320{
 321        struct inode *inode = container_of(head, struct inode, i_rcu);
 322        kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
 323}
 324
 325static void
 326cifs_destroy_inode(struct inode *inode)
 327{
 328        call_rcu(&inode->i_rcu, cifs_i_callback);
 329}
 330
 331static void
 332cifs_evict_inode(struct inode *inode)
 333{
 334        truncate_inode_pages_final(&inode->i_data);
 335        clear_inode(inode);
 336        cifs_fscache_release_inode_cookie(inode);
 337}
 338
 339static void
 340cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
 341{
 342        struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
 343        struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
 344
 345        seq_puts(s, ",addr=");
 346
 347        switch (server->dstaddr.ss_family) {
 348        case AF_INET:
 349                seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
 350                break;
 351        case AF_INET6:
 352                seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
 353                if (sa6->sin6_scope_id)
 354                        seq_printf(s, "%%%u", sa6->sin6_scope_id);
 355                break;
 356        default:
 357                seq_puts(s, "(unknown)");
 358        }
 359        if (server->rdma)
 360                seq_puts(s, ",rdma");
 361}
 362
 363static void
 364cifs_show_security(struct seq_file *s, struct cifs_ses *ses)
 365{
 366        if (ses->sectype == Unspecified) {
 367                if (ses->user_name == NULL)
 368                        seq_puts(s, ",sec=none");
 369                return;
 370        }
 371
 372        seq_puts(s, ",sec=");
 373
 374        switch (ses->sectype) {
 375        case LANMAN:
 376                seq_puts(s, "lanman");
 377                break;
 378        case NTLMv2:
 379                seq_puts(s, "ntlmv2");
 380                break;
 381        case NTLM:
 382                seq_puts(s, "ntlm");
 383                break;
 384        case Kerberos:
 385                seq_printf(s, "krb5,cruid=%u", from_kuid_munged(&init_user_ns,ses->cred_uid));
 386                break;
 387        case RawNTLMSSP:
 388                seq_puts(s, "ntlmssp");
 389                break;
 390        default:
 391                /* shouldn't ever happen */
 392                seq_puts(s, "unknown");
 393                break;
 394        }
 395
 396        if (ses->sign)
 397                seq_puts(s, "i");
 398}
 399
 400static void
 401cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
 402{
 403        seq_puts(s, ",cache=");
 404
 405        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
 406                seq_puts(s, "strict");
 407        else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
 408                seq_puts(s, "none");
 409        else
 410                seq_puts(s, "loose");
 411}
 412
 413static void
 414cifs_show_nls(struct seq_file *s, struct nls_table *cur)
 415{
 416        struct nls_table *def;
 417
 418        /* Display iocharset= option if it's not default charset */
 419        def = load_nls_default();
 420        if (def != cur)
 421                seq_printf(s, ",iocharset=%s", cur->charset);
 422        unload_nls(def);
 423}
 424
 425/*
 426 * cifs_show_options() is for displaying mount options in /proc/mounts.
 427 * Not all settable options are displayed but most of the important
 428 * ones are.
 429 */
 430static int
 431cifs_show_options(struct seq_file *s, struct dentry *root)
 432{
 433        struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
 434        struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
 435        struct sockaddr *srcaddr;
 436        srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
 437
 438        seq_show_option(s, "vers", tcon->ses->server->vals->version_string);
 439        cifs_show_security(s, tcon->ses);
 440        cifs_show_cache_flavor(s, cifs_sb);
 441
 442        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)
 443                seq_puts(s, ",multiuser");
 444        else if (tcon->ses->user_name)
 445                seq_show_option(s, "username", tcon->ses->user_name);
 446
 447        if (tcon->ses->domainName && tcon->ses->domainName[0] != 0)
 448                seq_show_option(s, "domain", tcon->ses->domainName);
 449
 450        if (srcaddr->sa_family != AF_UNSPEC) {
 451                struct sockaddr_in *saddr4;
 452                struct sockaddr_in6 *saddr6;
 453                saddr4 = (struct sockaddr_in *)srcaddr;
 454                saddr6 = (struct sockaddr_in6 *)srcaddr;
 455                if (srcaddr->sa_family == AF_INET6)
 456                        seq_printf(s, ",srcaddr=%pI6c",
 457                                   &saddr6->sin6_addr);
 458                else if (srcaddr->sa_family == AF_INET)
 459                        seq_printf(s, ",srcaddr=%pI4",
 460                                   &saddr4->sin_addr.s_addr);
 461                else
 462                        seq_printf(s, ",srcaddr=BAD-AF:%i",
 463                                   (int)(srcaddr->sa_family));
 464        }
 465
 466        seq_printf(s, ",uid=%u",
 467                   from_kuid_munged(&init_user_ns, cifs_sb->mnt_uid));
 468        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
 469                seq_puts(s, ",forceuid");
 470        else
 471                seq_puts(s, ",noforceuid");
 472
 473        seq_printf(s, ",gid=%u",
 474                   from_kgid_munged(&init_user_ns, cifs_sb->mnt_gid));
 475        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
 476                seq_puts(s, ",forcegid");
 477        else
 478                seq_puts(s, ",noforcegid");
 479
 480        cifs_show_address(s, tcon->ses->server);
 481
 482        if (!tcon->unix_ext)
 483                seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
 484                                           cifs_sb->mnt_file_mode,
 485                                           cifs_sb->mnt_dir_mode);
 486
 487        cifs_show_nls(s, cifs_sb->local_nls);
 488
 489        if (tcon->seal)
 490                seq_puts(s, ",seal");
 491        if (tcon->nocase)
 492                seq_puts(s, ",nocase");
 493        if (tcon->retry)
 494                seq_puts(s, ",hard");
 495        else
 496                seq_puts(s, ",soft");
 497        if (tcon->use_persistent)
 498                seq_puts(s, ",persistenthandles");
 499        else if (tcon->use_resilient)
 500                seq_puts(s, ",resilienthandles");
 501        if (tcon->posix_extensions)
 502                seq_puts(s, ",posix");
 503        else if (tcon->unix_ext)
 504                seq_puts(s, ",unix");
 505        else
 506                seq_puts(s, ",nounix");
 507        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
 508                seq_puts(s, ",nodfs");
 509        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
 510                seq_puts(s, ",posixpaths");
 511        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
 512                seq_puts(s, ",setuids");
 513        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
 514                seq_puts(s, ",idsfromsid");
 515        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
 516                seq_puts(s, ",serverino");
 517        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
 518                seq_puts(s, ",rwpidforward");
 519        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
 520                seq_puts(s, ",forcemand");
 521        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
 522                seq_puts(s, ",nouser_xattr");
 523        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
 524                seq_puts(s, ",mapchars");
 525        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
 526                seq_puts(s, ",mapposix");
 527        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
 528                seq_puts(s, ",sfu");
 529        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
 530                seq_puts(s, ",nobrl");
 531        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_HANDLE_CACHE)
 532                seq_puts(s, ",nohandlecache");
 533        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
 534                seq_puts(s, ",cifsacl");
 535        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
 536                seq_puts(s, ",dynperm");
 537        if (root->d_sb->s_flags & SB_POSIXACL)
 538                seq_puts(s, ",acl");
 539        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
 540                seq_puts(s, ",mfsymlinks");
 541        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
 542                seq_puts(s, ",fsc");
 543        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
 544                seq_puts(s, ",nostrictsync");
 545        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
 546                seq_puts(s, ",noperm");
 547        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID)
 548                seq_printf(s, ",backupuid=%u",
 549                           from_kuid_munged(&init_user_ns,
 550                                            cifs_sb->mnt_backupuid));
 551        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID)
 552                seq_printf(s, ",backupgid=%u",
 553                           from_kgid_munged(&init_user_ns,
 554                                            cifs_sb->mnt_backupgid));
 555
 556        seq_printf(s, ",rsize=%u", cifs_sb->rsize);
 557        seq_printf(s, ",wsize=%u", cifs_sb->wsize);
 558        seq_printf(s, ",bsize=%u", cifs_sb->bsize);
 559        seq_printf(s, ",echo_interval=%lu",
 560                        tcon->ses->server->echo_interval / HZ);
 561        if (tcon->snapshot_time)
 562                seq_printf(s, ",snapshot=%llu", tcon->snapshot_time);
 563        if (tcon->handle_timeout)
 564                seq_printf(s, ",handletimeout=%u", tcon->handle_timeout);
 565        /* convert actimeo and display it in seconds */
 566        seq_printf(s, ",actimeo=%lu", cifs_sb->actimeo / HZ);
 567
 568        return 0;
 569}
 570
 571static void cifs_umount_begin(struct super_block *sb)
 572{
 573        struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 574        struct cifs_tcon *tcon;
 575
 576        if (cifs_sb == NULL)
 577                return;
 578
 579        tcon = cifs_sb_master_tcon(cifs_sb);
 580
 581        spin_lock(&cifs_tcp_ses_lock);
 582        if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
 583                /* we have other mounts to same share or we have
 584                   already tried to force umount this and woken up
 585                   all waiting network requests, nothing to do */
 586                spin_unlock(&cifs_tcp_ses_lock);
 587                return;
 588        } else if (tcon->tc_count == 1)
 589                tcon->tidStatus = CifsExiting;
 590        spin_unlock(&cifs_tcp_ses_lock);
 591
 592        /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
 593        /* cancel_notify_requests(tcon); */
 594        if (tcon->ses && tcon->ses->server) {
 595                cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
 596                wake_up_all(&tcon->ses->server->request_q);
 597                wake_up_all(&tcon->ses->server->response_q);
 598                msleep(1); /* yield */
 599                /* we have to kick the requests once more */
 600                wake_up_all(&tcon->ses->server->response_q);
 601                msleep(1);
 602        }
 603
 604        return;
 605}
 606
 607#ifdef CONFIG_CIFS_STATS2
 608static int cifs_show_stats(struct seq_file *s, struct dentry *root)
 609{
 610        /* BB FIXME */
 611        return 0;
 612}
 613#endif
 614
 615static int cifs_remount(struct super_block *sb, int *flags, char *data)
 616{
 617        sync_filesystem(sb);
 618        *flags |= SB_NODIRATIME;
 619        return 0;
 620}
 621
 622static int cifs_drop_inode(struct inode *inode)
 623{
 624        struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
 625
 626        /* no serverino => unconditional eviction */
 627        return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
 628                generic_drop_inode(inode);
 629}
 630
 631static const struct super_operations cifs_super_ops = {
 632        .statfs = cifs_statfs,
 633        .alloc_inode = cifs_alloc_inode,
 634        .destroy_inode = cifs_destroy_inode,
 635        .drop_inode     = cifs_drop_inode,
 636        .evict_inode    = cifs_evict_inode,
 637/*      .delete_inode   = cifs_delete_inode,  */  /* Do not need above
 638        function unless later we add lazy close of inodes or unless the
 639        kernel forgets to call us with the same number of releases (closes)
 640        as opens */
 641        .show_options = cifs_show_options,
 642        .umount_begin   = cifs_umount_begin,
 643        .remount_fs = cifs_remount,
 644#ifdef CONFIG_CIFS_STATS2
 645        .show_stats = cifs_show_stats,
 646#endif
 647};
 648
 649/*
 650 * Get root dentry from superblock according to prefix path mount option.
 651 * Return dentry with refcount + 1 on success and NULL otherwise.
 652 */
 653static struct dentry *
 654cifs_get_root(struct smb_vol *vol, struct super_block *sb)
 655{
 656        struct dentry *dentry;
 657        struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 658        char *full_path = NULL;
 659        char *s, *p;
 660        char sep;
 661
 662        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
 663                return dget(sb->s_root);
 664
 665        full_path = cifs_build_path_to_root(vol, cifs_sb,
 666                                cifs_sb_master_tcon(cifs_sb), 0);
 667        if (full_path == NULL)
 668                return ERR_PTR(-ENOMEM);
 669
 670        cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
 671
 672        sep = CIFS_DIR_SEP(cifs_sb);
 673        dentry = dget(sb->s_root);
 674        p = s = full_path;
 675
 676        do {
 677                struct inode *dir = d_inode(dentry);
 678                struct dentry *child;
 679
 680                if (!dir) {
 681                        dput(dentry);
 682                        dentry = ERR_PTR(-ENOENT);
 683                        break;
 684                }
 685                if (!S_ISDIR(dir->i_mode)) {
 686                        dput(dentry);
 687                        dentry = ERR_PTR(-ENOTDIR);
 688                        break;
 689                }
 690
 691                /* skip separators */
 692                while (*s == sep)
 693                        s++;
 694                if (!*s)
 695                        break;
 696                p = s++;
 697                /* next separator */
 698                while (*s && *s != sep)
 699                        s++;
 700
 701                child = lookup_one_len_unlocked(p, dentry, s - p);
 702                dput(dentry);
 703                dentry = child;
 704        } while (!IS_ERR(dentry));
 705        kfree(full_path);
 706        return dentry;
 707}
 708
 709static int cifs_set_super(struct super_block *sb, void *data)
 710{
 711        struct cifs_mnt_data *mnt_data = data;
 712        sb->s_fs_info = mnt_data->cifs_sb;
 713        return set_anon_super(sb, NULL);
 714}
 715
 716static struct dentry *
 717cifs_smb3_do_mount(struct file_system_type *fs_type,
 718              int flags, const char *dev_name, void *data, bool is_smb3)
 719{
 720        int rc;
 721        struct super_block *sb;
 722        struct cifs_sb_info *cifs_sb;
 723        struct smb_vol *volume_info;
 724        struct cifs_mnt_data mnt_data;
 725        struct dentry *root;
 726
 727        /*
 728         * Prints in Kernel / CIFS log the attempted mount operation
 729         *      If CIFS_DEBUG && cifs_FYI
 730         */
 731        if (cifsFYI)
 732                cifs_dbg(FYI, "Devname: %s flags: %d\n", dev_name, flags);
 733        else
 734                cifs_info("Attempting to mount %s\n", dev_name);
 735
 736        volume_info = cifs_get_volume_info((char *)data, dev_name, is_smb3);
 737        if (IS_ERR(volume_info))
 738                return ERR_CAST(volume_info);
 739
 740        cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
 741        if (cifs_sb == NULL) {
 742                root = ERR_PTR(-ENOMEM);
 743                goto out_nls;
 744        }
 745
 746        cifs_sb->mountdata = kstrndup(data, PAGE_SIZE, GFP_KERNEL);
 747        if (cifs_sb->mountdata == NULL) {
 748                root = ERR_PTR(-ENOMEM);
 749                goto out_free;
 750        }
 751
 752        rc = cifs_setup_cifs_sb(volume_info, cifs_sb);
 753        if (rc) {
 754                root = ERR_PTR(rc);
 755                goto out_free;
 756        }
 757
 758        rc = cifs_mount(cifs_sb, volume_info);
 759        if (rc) {
 760                if (!(flags & SB_SILENT))
 761                        cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
 762                                 rc);
 763                root = ERR_PTR(rc);
 764                goto out_free;
 765        }
 766
 767        mnt_data.vol = volume_info;
 768        mnt_data.cifs_sb = cifs_sb;
 769        mnt_data.flags = flags;
 770
 771        /* BB should we make this contingent on mount parm? */
 772        flags |= SB_NODIRATIME | SB_NOATIME;
 773
 774        sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
 775        if (IS_ERR(sb)) {
 776                root = ERR_CAST(sb);
 777                cifs_umount(cifs_sb);
 778                goto out;
 779        }
 780
 781        if (sb->s_root) {
 782                cifs_dbg(FYI, "Use existing superblock\n");
 783                cifs_umount(cifs_sb);
 784        } else {
 785                rc = cifs_read_super(sb);
 786                if (rc) {
 787                        root = ERR_PTR(rc);
 788                        goto out_super;
 789                }
 790
 791                sb->s_flags |= SB_ACTIVE;
 792        }
 793
 794        root = cifs_get_root(volume_info, sb);
 795        if (IS_ERR(root))
 796                goto out_super;
 797
 798        cifs_dbg(FYI, "dentry root is: %p\n", root);
 799        goto out;
 800
 801out_super:
 802        deactivate_locked_super(sb);
 803out:
 804        cifs_cleanup_volume_info(volume_info);
 805        return root;
 806
 807out_free:
 808        kfree(cifs_sb->prepath);
 809        kfree(cifs_sb->mountdata);
 810        kfree(cifs_sb);
 811out_nls:
 812        unload_nls(volume_info->local_nls);
 813        goto out;
 814}
 815
 816static struct dentry *
 817smb3_do_mount(struct file_system_type *fs_type,
 818              int flags, const char *dev_name, void *data)
 819{
 820        return cifs_smb3_do_mount(fs_type, flags, dev_name, data, true);
 821}
 822
 823static struct dentry *
 824cifs_do_mount(struct file_system_type *fs_type,
 825              int flags, const char *dev_name, void *data)
 826{
 827        return cifs_smb3_do_mount(fs_type, flags, dev_name, data, false);
 828}
 829
 830static ssize_t
 831cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
 832{
 833        ssize_t rc;
 834        struct inode *inode = file_inode(iocb->ki_filp);
 835
 836        if (iocb->ki_filp->f_flags & O_DIRECT)
 837                return cifs_user_readv(iocb, iter);
 838
 839        rc = cifs_revalidate_mapping(inode);
 840        if (rc)
 841                return rc;
 842
 843        return generic_file_read_iter(iocb, iter);
 844}
 845
 846static ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 847{
 848        struct inode *inode = file_inode(iocb->ki_filp);
 849        struct cifsInodeInfo *cinode = CIFS_I(inode);
 850        ssize_t written;
 851        int rc;
 852
 853        if (iocb->ki_filp->f_flags & O_DIRECT) {
 854                written = cifs_user_writev(iocb, from);
 855                if (written > 0 && CIFS_CACHE_READ(cinode)) {
 856                        cifs_zap_mapping(inode);
 857                        cifs_dbg(FYI,
 858                                 "Set no oplock for inode=%p after a write operation\n",
 859                                 inode);
 860                        cinode->oplock = 0;
 861                }
 862                return written;
 863        }
 864
 865        written = cifs_get_writer(cinode);
 866        if (written)
 867                return written;
 868
 869        written = generic_file_write_iter(iocb, from);
 870
 871        if (CIFS_CACHE_WRITE(CIFS_I(inode)))
 872                goto out;
 873
 874        rc = filemap_fdatawrite(inode->i_mapping);
 875        if (rc)
 876                cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
 877                         rc, inode);
 878
 879out:
 880        cifs_put_writer(cinode);
 881        return written;
 882}
 883
 884static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
 885{
 886        /*
 887         * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
 888         * the cached file length
 889         */
 890        if (whence != SEEK_SET && whence != SEEK_CUR) {
 891                int rc;
 892                struct inode *inode = file_inode(file);
 893
 894                /*
 895                 * We need to be sure that all dirty pages are written and the
 896                 * server has the newest file length.
 897                 */
 898                if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
 899                    inode->i_mapping->nrpages != 0) {
 900                        rc = filemap_fdatawait(inode->i_mapping);
 901                        if (rc) {
 902                                mapping_set_error(inode->i_mapping, rc);
 903                                return rc;
 904                        }
 905                }
 906                /*
 907                 * Some applications poll for the file length in this strange
 908                 * way so we must seek to end on non-oplocked files by
 909                 * setting the revalidate time to zero.
 910                 */
 911                CIFS_I(inode)->time = 0;
 912
 913                rc = cifs_revalidate_file_attr(file);
 914                if (rc < 0)
 915                        return (loff_t)rc;
 916        }
 917        return generic_file_llseek(file, offset, whence);
 918}
 919
 920static int
 921cifs_setlease(struct file *file, long arg, struct file_lock **lease, void **priv)
 922{
 923        /*
 924         * Note that this is called by vfs setlease with i_lock held to
 925         * protect *lease from going away.
 926         */
 927        struct inode *inode = file_inode(file);
 928        struct cifsFileInfo *cfile = file->private_data;
 929
 930        if (!(S_ISREG(inode->i_mode)))
 931                return -EINVAL;
 932
 933        /* Check if file is oplocked if this is request for new lease */
 934        if (arg == F_UNLCK ||
 935            ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) ||
 936            ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
 937                return generic_setlease(file, arg, lease, priv);
 938        else if (tlink_tcon(cfile->tlink)->local_lease &&
 939                 !CIFS_CACHE_READ(CIFS_I(inode)))
 940                /*
 941                 * If the server claims to support oplock on this file, then we
 942                 * still need to check oplock even if the local_lease mount
 943                 * option is set, but there are servers which do not support
 944                 * oplock for which this mount option may be useful if the user
 945                 * knows that the file won't be changed on the server by anyone
 946                 * else.
 947                 */
 948                return generic_setlease(file, arg, lease, priv);
 949        else
 950                return -EAGAIN;
 951}
 952
 953struct file_system_type cifs_fs_type = {
 954        .owner = THIS_MODULE,
 955        .name = "cifs",
 956        .mount = cifs_do_mount,
 957        .kill_sb = cifs_kill_sb,
 958        /*  .fs_flags */
 959};
 960MODULE_ALIAS_FS("cifs");
 961
 962static struct file_system_type smb3_fs_type = {
 963        .owner = THIS_MODULE,
 964        .name = "smb3",
 965        .mount = smb3_do_mount,
 966        .kill_sb = cifs_kill_sb,
 967        /*  .fs_flags */
 968};
 969MODULE_ALIAS_FS("smb3");
 970MODULE_ALIAS("smb3");
 971
 972const struct inode_operations cifs_dir_inode_ops = {
 973        .create = cifs_create,
 974        .atomic_open = cifs_atomic_open,
 975        .lookup = cifs_lookup,
 976        .getattr = cifs_getattr,
 977        .unlink = cifs_unlink,
 978        .link = cifs_hardlink,
 979        .mkdir = cifs_mkdir,
 980        .rmdir = cifs_rmdir,
 981        .rename = cifs_rename2,
 982        .permission = cifs_permission,
 983        .setattr = cifs_setattr,
 984        .symlink = cifs_symlink,
 985        .mknod   = cifs_mknod,
 986        .listxattr = cifs_listxattr,
 987};
 988
 989const struct inode_operations cifs_file_inode_ops = {
 990        .setattr = cifs_setattr,
 991        .getattr = cifs_getattr,
 992        .permission = cifs_permission,
 993        .listxattr = cifs_listxattr,
 994};
 995
 996const struct inode_operations cifs_symlink_inode_ops = {
 997        .get_link = cifs_get_link,
 998        .permission = cifs_permission,
 999        .listxattr = cifs_listxattr,
1000};
1001
1002static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,
1003                struct file *dst_file, loff_t destoff, loff_t len,
1004                unsigned int remap_flags)
1005{
1006        struct inode *src_inode = file_inode(src_file);
1007        struct inode *target_inode = file_inode(dst_file);
1008        struct cifsFileInfo *smb_file_src = src_file->private_data;
1009        struct cifsFileInfo *smb_file_target;
1010        struct cifs_tcon *target_tcon;
1011        unsigned int xid;
1012        int rc;
1013
1014        if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
1015                return -EINVAL;
1016
1017        cifs_dbg(FYI, "clone range\n");
1018
1019        xid = get_xid();
1020
1021        if (!src_file->private_data || !dst_file->private_data) {
1022                rc = -EBADF;
1023                cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1024                goto out;
1025        }
1026
1027        smb_file_target = dst_file->private_data;
1028        target_tcon = tlink_tcon(smb_file_target->tlink);
1029
1030        /*
1031         * Note: cifs case is easier than btrfs since server responsible for
1032         * checks for proper open modes and file type and if it wants
1033         * server could even support copy of range where source = target
1034         */
1035        lock_two_nondirectories(target_inode, src_inode);
1036
1037        if (len == 0)
1038                len = src_inode->i_size - off;
1039
1040        cifs_dbg(FYI, "about to flush pages\n");
1041        /* should we flush first and last page first */
1042        truncate_inode_pages_range(&target_inode->i_data, destoff,
1043                                   PAGE_ALIGN(destoff + len)-1);
1044
1045        if (target_tcon->ses->server->ops->duplicate_extents)
1046                rc = target_tcon->ses->server->ops->duplicate_extents(xid,
1047                        smb_file_src, smb_file_target, off, len, destoff);
1048        else
1049                rc = -EOPNOTSUPP;
1050
1051        /* force revalidate of size and timestamps of target file now
1052           that target is updated on the server */
1053        CIFS_I(target_inode)->time = 0;
1054        /* although unlocking in the reverse order from locking is not
1055           strictly necessary here it is a little cleaner to be consistent */
1056        unlock_two_nondirectories(src_inode, target_inode);
1057out:
1058        free_xid(xid);
1059        return rc < 0 ? rc : len;
1060}
1061
1062ssize_t cifs_file_copychunk_range(unsigned int xid,
1063                                struct file *src_file, loff_t off,
1064                                struct file *dst_file, loff_t destoff,
1065                                size_t len, unsigned int flags)
1066{
1067        struct inode *src_inode = file_inode(src_file);
1068        struct inode *target_inode = file_inode(dst_file);
1069        struct cifsFileInfo *smb_file_src;
1070        struct cifsFileInfo *smb_file_target;
1071        struct cifs_tcon *src_tcon;
1072        struct cifs_tcon *target_tcon;
1073        ssize_t rc;
1074
1075        cifs_dbg(FYI, "copychunk range\n");
1076
1077        if (!src_file->private_data || !dst_file->private_data) {
1078                rc = -EBADF;
1079                cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1080                goto out;
1081        }
1082
1083        rc = -EXDEV;
1084        smb_file_target = dst_file->private_data;
1085        smb_file_src = src_file->private_data;
1086        src_tcon = tlink_tcon(smb_file_src->tlink);
1087        target_tcon = tlink_tcon(smb_file_target->tlink);
1088
1089        if (src_tcon->ses != target_tcon->ses) {
1090                cifs_dbg(VFS, "source and target of copy not on same server\n");
1091                goto out;
1092        }
1093
1094        /*
1095         * Note: cifs case is easier than btrfs since server responsible for
1096         * checks for proper open modes and file type and if it wants
1097         * server could even support copy of range where source = target
1098         */
1099        lock_two_nondirectories(target_inode, src_inode);
1100
1101        cifs_dbg(FYI, "about to flush pages\n");
1102        /* should we flush first and last page first */
1103        truncate_inode_pages(&target_inode->i_data, 0);
1104
1105        if (target_tcon->ses->server->ops->copychunk_range)
1106                rc = target_tcon->ses->server->ops->copychunk_range(xid,
1107                        smb_file_src, smb_file_target, off, len, destoff);
1108        else
1109                rc = -EOPNOTSUPP;
1110
1111        /* force revalidate of size and timestamps of target file now
1112         * that target is updated on the server
1113         */
1114        CIFS_I(target_inode)->time = 0;
1115        /* although unlocking in the reverse order from locking is not
1116         * strictly necessary here it is a little cleaner to be consistent
1117         */
1118        unlock_two_nondirectories(src_inode, target_inode);
1119
1120out:
1121        return rc;
1122}
1123
1124/*
1125 * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync()
1126 * is a dummy operation.
1127 */
1128static int cifs_dir_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1129{
1130        cifs_dbg(FYI, "Sync directory - name: %pD datasync: 0x%x\n",
1131                 file, datasync);
1132
1133        return 0;
1134}
1135
1136static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off,
1137                                struct file *dst_file, loff_t destoff,
1138                                size_t len, unsigned int flags)
1139{
1140        unsigned int xid = get_xid();
1141        ssize_t rc;
1142
1143        rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
1144                                        len, flags);
1145        free_xid(xid);
1146        return rc;
1147}
1148
1149const struct file_operations cifs_file_ops = {
1150        .read_iter = cifs_loose_read_iter,
1151        .write_iter = cifs_file_write_iter,
1152        .open = cifs_open,
1153        .release = cifs_close,
1154        .lock = cifs_lock,
1155        .fsync = cifs_fsync,
1156        .flush = cifs_flush,
1157        .mmap  = cifs_file_mmap,
1158        .splice_read = generic_file_splice_read,
1159        .splice_write = iter_file_splice_write,
1160        .llseek = cifs_llseek,
1161        .unlocked_ioctl = cifs_ioctl,
1162        .copy_file_range = cifs_copy_file_range,
1163        .remap_file_range = cifs_remap_file_range,
1164        .setlease = cifs_setlease,
1165        .fallocate = cifs_fallocate,
1166};
1167
1168const struct file_operations cifs_file_strict_ops = {
1169        .read_iter = cifs_strict_readv,
1170        .write_iter = cifs_strict_writev,
1171        .open = cifs_open,
1172        .release = cifs_close,
1173        .lock = cifs_lock,
1174        .fsync = cifs_strict_fsync,
1175        .flush = cifs_flush,
1176        .mmap = cifs_file_strict_mmap,
1177        .splice_read = generic_file_splice_read,
1178        .splice_write = iter_file_splice_write,
1179        .llseek = cifs_llseek,
1180        .unlocked_ioctl = cifs_ioctl,
1181        .copy_file_range = cifs_copy_file_range,
1182        .remap_file_range = cifs_remap_file_range,
1183        .setlease = cifs_setlease,
1184        .fallocate = cifs_fallocate,
1185};
1186
1187const struct file_operations cifs_file_direct_ops = {
1188        .read_iter = cifs_direct_readv,
1189        .write_iter = cifs_direct_writev,
1190        .open = cifs_open,
1191        .release = cifs_close,
1192        .lock = cifs_lock,
1193        .fsync = cifs_fsync,
1194        .flush = cifs_flush,
1195        .mmap = cifs_file_mmap,
1196        .splice_read = generic_file_splice_read,
1197        .splice_write = iter_file_splice_write,
1198        .unlocked_ioctl  = cifs_ioctl,
1199        .copy_file_range = cifs_copy_file_range,
1200        .remap_file_range = cifs_remap_file_range,
1201        .llseek = cifs_llseek,
1202        .setlease = cifs_setlease,
1203        .fallocate = cifs_fallocate,
1204};
1205
1206const struct file_operations cifs_file_nobrl_ops = {
1207        .read_iter = cifs_loose_read_iter,
1208        .write_iter = cifs_file_write_iter,
1209        .open = cifs_open,
1210        .release = cifs_close,
1211        .fsync = cifs_fsync,
1212        .flush = cifs_flush,
1213        .mmap  = cifs_file_mmap,
1214        .splice_read = generic_file_splice_read,
1215        .splice_write = iter_file_splice_write,
1216        .llseek = cifs_llseek,
1217        .unlocked_ioctl = cifs_ioctl,
1218        .copy_file_range = cifs_copy_file_range,
1219        .remap_file_range = cifs_remap_file_range,
1220        .setlease = cifs_setlease,
1221        .fallocate = cifs_fallocate,
1222};
1223
1224const struct file_operations cifs_file_strict_nobrl_ops = {
1225        .read_iter = cifs_strict_readv,
1226        .write_iter = cifs_strict_writev,
1227        .open = cifs_open,
1228        .release = cifs_close,
1229        .fsync = cifs_strict_fsync,
1230        .flush = cifs_flush,
1231        .mmap = cifs_file_strict_mmap,
1232        .splice_read = generic_file_splice_read,
1233        .splice_write = iter_file_splice_write,
1234        .llseek = cifs_llseek,
1235        .unlocked_ioctl = cifs_ioctl,
1236        .copy_file_range = cifs_copy_file_range,
1237        .remap_file_range = cifs_remap_file_range,
1238        .setlease = cifs_setlease,
1239        .fallocate = cifs_fallocate,
1240};
1241
1242const struct file_operations cifs_file_direct_nobrl_ops = {
1243        .read_iter = cifs_direct_readv,
1244        .write_iter = cifs_direct_writev,
1245        .open = cifs_open,
1246        .release = cifs_close,
1247        .fsync = cifs_fsync,
1248        .flush = cifs_flush,
1249        .mmap = cifs_file_mmap,
1250        .splice_read = generic_file_splice_read,
1251        .splice_write = iter_file_splice_write,
1252        .unlocked_ioctl  = cifs_ioctl,
1253        .copy_file_range = cifs_copy_file_range,
1254        .remap_file_range = cifs_remap_file_range,
1255        .llseek = cifs_llseek,
1256        .setlease = cifs_setlease,
1257        .fallocate = cifs_fallocate,
1258};
1259
1260const struct file_operations cifs_dir_ops = {
1261        .iterate_shared = cifs_readdir,
1262        .release = cifs_closedir,
1263        .read    = generic_read_dir,
1264        .unlocked_ioctl  = cifs_ioctl,
1265        .copy_file_range = cifs_copy_file_range,
1266        .remap_file_range = cifs_remap_file_range,
1267        .llseek = generic_file_llseek,
1268        .fsync = cifs_dir_fsync,
1269};
1270
1271static void
1272cifs_init_once(void *inode)
1273{
1274        struct cifsInodeInfo *cifsi = inode;
1275
1276        inode_init_once(&cifsi->vfs_inode);
1277        init_rwsem(&cifsi->lock_sem);
1278}
1279
1280static int __init
1281cifs_init_inodecache(void)
1282{
1283        cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
1284                                              sizeof(struct cifsInodeInfo),
1285                                              0, (SLAB_RECLAIM_ACCOUNT|
1286                                                SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1287                                              cifs_init_once);
1288        if (cifs_inode_cachep == NULL)
1289                return -ENOMEM;
1290
1291        return 0;
1292}
1293
1294static void
1295cifs_destroy_inodecache(void)
1296{
1297        /*
1298         * Make sure all delayed rcu free inodes are flushed before we
1299         * destroy cache.
1300         */
1301        rcu_barrier();
1302        kmem_cache_destroy(cifs_inode_cachep);
1303}
1304
1305static int
1306cifs_init_request_bufs(void)
1307{
1308        /*
1309         * SMB2 maximum header size is bigger than CIFS one - no problems to
1310         * allocate some more bytes for CIFS.
1311         */
1312        size_t max_hdr_size = MAX_SMB2_HDR_SIZE;
1313
1314        if (CIFSMaxBufSize < 8192) {
1315        /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
1316        Unicode path name has to fit in any SMB/CIFS path based frames */
1317                CIFSMaxBufSize = 8192;
1318        } else if (CIFSMaxBufSize > 1024*127) {
1319                CIFSMaxBufSize = 1024 * 127;
1320        } else {
1321                CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
1322        }
1323/*
1324        cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
1325                 CIFSMaxBufSize, CIFSMaxBufSize);
1326*/
1327        cifs_req_cachep = kmem_cache_create_usercopy("cifs_request",
1328                                            CIFSMaxBufSize + max_hdr_size, 0,
1329                                            SLAB_HWCACHE_ALIGN, 0,
1330                                            CIFSMaxBufSize + max_hdr_size,
1331                                            NULL);
1332        if (cifs_req_cachep == NULL)
1333                return -ENOMEM;
1334
1335        if (cifs_min_rcv < 1)
1336                cifs_min_rcv = 1;
1337        else if (cifs_min_rcv > 64) {
1338                cifs_min_rcv = 64;
1339                cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
1340        }
1341
1342        cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1343                                                  cifs_req_cachep);
1344
1345        if (cifs_req_poolp == NULL) {
1346                kmem_cache_destroy(cifs_req_cachep);
1347                return -ENOMEM;
1348        }
1349        /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1350        almost all handle based requests (but not write response, nor is it
1351        sufficient for path based requests).  A smaller size would have
1352        been more efficient (compacting multiple slab items on one 4k page)
1353        for the case in which debug was on, but this larger size allows
1354        more SMBs to use small buffer alloc and is still much more
1355        efficient to alloc 1 per page off the slab compared to 17K (5page)
1356        alloc of large cifs buffers even when page debugging is on */
1357        cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq",
1358                        MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
1359                        0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL);
1360        if (cifs_sm_req_cachep == NULL) {
1361                mempool_destroy(cifs_req_poolp);
1362                kmem_cache_destroy(cifs_req_cachep);
1363                return -ENOMEM;
1364        }
1365
1366        if (cifs_min_small < 2)
1367                cifs_min_small = 2;
1368        else if (cifs_min_small > 256) {
1369                cifs_min_small = 256;
1370                cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
1371        }
1372
1373        cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1374                                                     cifs_sm_req_cachep);
1375
1376        if (cifs_sm_req_poolp == NULL) {
1377                mempool_destroy(cifs_req_poolp);
1378                kmem_cache_destroy(cifs_req_cachep);
1379                kmem_cache_destroy(cifs_sm_req_cachep);
1380                return -ENOMEM;
1381        }
1382
1383        return 0;
1384}
1385
1386static void
1387cifs_destroy_request_bufs(void)
1388{
1389        mempool_destroy(cifs_req_poolp);
1390        kmem_cache_destroy(cifs_req_cachep);
1391        mempool_destroy(cifs_sm_req_poolp);
1392        kmem_cache_destroy(cifs_sm_req_cachep);
1393}
1394
1395static int
1396cifs_init_mids(void)
1397{
1398        cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
1399                                            sizeof(struct mid_q_entry), 0,
1400                                            SLAB_HWCACHE_ALIGN, NULL);
1401        if (cifs_mid_cachep == NULL)
1402                return -ENOMEM;
1403
1404        /* 3 is a reasonable minimum number of simultaneous operations */
1405        cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
1406        if (cifs_mid_poolp == NULL) {
1407                kmem_cache_destroy(cifs_mid_cachep);
1408                return -ENOMEM;
1409        }
1410
1411        return 0;
1412}
1413
1414static void
1415cifs_destroy_mids(void)
1416{
1417        mempool_destroy(cifs_mid_poolp);
1418        kmem_cache_destroy(cifs_mid_cachep);
1419}
1420
1421static int __init
1422init_cifs(void)
1423{
1424        int rc = 0;
1425        cifs_proc_init();
1426        INIT_LIST_HEAD(&cifs_tcp_ses_list);
1427#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* unused temporarily */
1428        INIT_LIST_HEAD(&GlobalDnotifyReqList);
1429        INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
1430#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */
1431/*
1432 *  Initialize Global counters
1433 */
1434        atomic_set(&sesInfoAllocCount, 0);
1435        atomic_set(&tconInfoAllocCount, 0);
1436        atomic_set(&tcpSesAllocCount, 0);
1437        atomic_set(&tcpSesReconnectCount, 0);
1438        atomic_set(&tconInfoReconnectCount, 0);
1439
1440        atomic_set(&bufAllocCount, 0);
1441        atomic_set(&smBufAllocCount, 0);
1442#ifdef CONFIG_CIFS_STATS2
1443        atomic_set(&totBufAllocCount, 0);
1444        atomic_set(&totSmBufAllocCount, 0);
1445        if (slow_rsp_threshold < 1)
1446                cifs_dbg(FYI, "slow_response_threshold msgs disabled\n");
1447        else if (slow_rsp_threshold > 32767)
1448                cifs_dbg(VFS,
1449                       "slow response threshold set higher than recommended (0 to 32767)\n");
1450#endif /* CONFIG_CIFS_STATS2 */
1451
1452        atomic_set(&midCount, 0);
1453        GlobalCurrentXid = 0;
1454        GlobalTotalActiveXid = 0;
1455        GlobalMaxActiveXid = 0;
1456        spin_lock_init(&cifs_tcp_ses_lock);
1457        spin_lock_init(&GlobalMid_Lock);
1458
1459        cifs_lock_secret = get_random_u32();
1460
1461        if (cifs_max_pending < 2) {
1462                cifs_max_pending = 2;
1463                cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
1464        } else if (cifs_max_pending > CIFS_MAX_REQ) {
1465                cifs_max_pending = CIFS_MAX_REQ;
1466                cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
1467                         CIFS_MAX_REQ);
1468        }
1469
1470        cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1471        if (!cifsiod_wq) {
1472                rc = -ENOMEM;
1473                goto out_clean_proc;
1474        }
1475
1476        cifsoplockd_wq = alloc_workqueue("cifsoplockd",
1477                                         WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1478        if (!cifsoplockd_wq) {
1479                rc = -ENOMEM;
1480                goto out_destroy_cifsiod_wq;
1481        }
1482
1483        rc = cifs_fscache_register();
1484        if (rc)
1485                goto out_destroy_cifsoplockd_wq;
1486
1487        rc = cifs_init_inodecache();
1488        if (rc)
1489                goto out_unreg_fscache;
1490
1491        rc = cifs_init_mids();
1492        if (rc)
1493                goto out_destroy_inodecache;
1494
1495        rc = cifs_init_request_bufs();
1496        if (rc)
1497                goto out_destroy_mids;
1498
1499#ifdef CONFIG_CIFS_DFS_UPCALL
1500        rc = dfs_cache_init();
1501        if (rc)
1502                goto out_destroy_request_bufs;
1503#endif /* CONFIG_CIFS_DFS_UPCALL */
1504#ifdef CONFIG_CIFS_UPCALL
1505        rc = init_cifs_spnego();
1506        if (rc)
1507                goto out_destroy_dfs_cache;
1508#endif /* CONFIG_CIFS_UPCALL */
1509
1510#ifdef CONFIG_CIFS_ACL
1511        rc = init_cifs_idmap();
1512        if (rc)
1513                goto out_register_key_type;
1514#endif /* CONFIG_CIFS_ACL */
1515
1516        rc = register_filesystem(&cifs_fs_type);
1517        if (rc)
1518                goto out_init_cifs_idmap;
1519
1520        rc = register_filesystem(&smb3_fs_type);
1521        if (rc) {
1522                unregister_filesystem(&cifs_fs_type);
1523                goto out_init_cifs_idmap;
1524        }
1525
1526        return 0;
1527
1528out_init_cifs_idmap:
1529#ifdef CONFIG_CIFS_ACL
1530        exit_cifs_idmap();
1531out_register_key_type:
1532#endif
1533#ifdef CONFIG_CIFS_UPCALL
1534        exit_cifs_spnego();
1535out_destroy_dfs_cache:
1536#endif
1537#ifdef CONFIG_CIFS_DFS_UPCALL
1538        dfs_cache_destroy();
1539out_destroy_request_bufs:
1540#endif
1541        cifs_destroy_request_bufs();
1542out_destroy_mids:
1543        cifs_destroy_mids();
1544out_destroy_inodecache:
1545        cifs_destroy_inodecache();
1546out_unreg_fscache:
1547        cifs_fscache_unregister();
1548out_destroy_cifsoplockd_wq:
1549        destroy_workqueue(cifsoplockd_wq);
1550out_destroy_cifsiod_wq:
1551        destroy_workqueue(cifsiod_wq);
1552out_clean_proc:
1553        cifs_proc_clean();
1554        return rc;
1555}
1556
1557static void __exit
1558exit_cifs(void)
1559{
1560        cifs_dbg(NOISY, "exit_smb3\n");
1561        unregister_filesystem(&cifs_fs_type);
1562        unregister_filesystem(&smb3_fs_type);
1563        cifs_dfs_release_automount_timer();
1564#ifdef CONFIG_CIFS_ACL
1565        exit_cifs_idmap();
1566#endif
1567#ifdef CONFIG_CIFS_UPCALL
1568        exit_cifs_spnego();
1569#endif
1570#ifdef CONFIG_CIFS_DFS_UPCALL
1571        dfs_cache_destroy();
1572#endif
1573        cifs_destroy_request_bufs();
1574        cifs_destroy_mids();
1575        cifs_destroy_inodecache();
1576        cifs_fscache_unregister();
1577        destroy_workqueue(cifsoplockd_wq);
1578        destroy_workqueue(cifsiod_wq);
1579        cifs_proc_clean();
1580}
1581
1582MODULE_AUTHOR("Steve French");
1583MODULE_LICENSE("GPL");  /* combination of LGPL + GPL source behaves as GPL */
1584MODULE_DESCRIPTION
1585        ("VFS to access SMB3 servers e.g. Samba, Macs, Azure and Windows (and "
1586        "also older servers complying with the SNIA CIFS Specification)");
1587MODULE_VERSION(CIFS_VERSION);
1588MODULE_SOFTDEP("pre: arc4");
1589MODULE_SOFTDEP("pre: des");
1590MODULE_SOFTDEP("pre: ecb");
1591MODULE_SOFTDEP("pre: hmac");
1592MODULE_SOFTDEP("pre: md4");
1593MODULE_SOFTDEP("pre: md5");
1594MODULE_SOFTDEP("pre: nls");
1595MODULE_SOFTDEP("pre: aes");
1596MODULE_SOFTDEP("pre: cmac");
1597MODULE_SOFTDEP("pre: sha256");
1598MODULE_SOFTDEP("pre: sha512");
1599MODULE_SOFTDEP("pre: aead2");
1600MODULE_SOFTDEP("pre: ccm");
1601module_init(init_cifs)
1602module_exit(exit_cifs)
1603