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