~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/cifs/cifsfs.c

Version: ~ [ linux-5.10-rc5 ] ~ [ linux-5.9.10 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.79 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.159 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.208 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.245 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.245 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp