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 max_credits if it was 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
626 if (tcon->snapshot_time)
627 seq_printf(s, ",snapshot=%llu", tcon->snapshot_time);
628 if (tcon->handle_timeout)
629 seq_printf(s, ",handletimeout=%u", tcon->handle_timeout);
630 /* convert actimeo and display it in seconds */
631 seq_printf(s, ",actimeo=%lu", cifs_sb->actimeo / HZ);
632
633 if (tcon->ses->chan_max > 1)
634 seq_printf(s, ",multichannel,max_channels=%zu",
635 tcon->ses->chan_max);
636
637 return 0;
638 }
639
cifs_umount_begin(struct super_block * sb)640 static void cifs_umount_begin(struct super_block *sb)
641 {
642 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
643 struct cifs_tcon *tcon;
644
645 if (cifs_sb == NULL)
646 return;
647
648 tcon = cifs_sb_master_tcon(cifs_sb);
649
650 spin_lock(&cifs_tcp_ses_lock);
651 if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
652 /* we have other mounts to same share or we have
653 already tried to force umount this and woken up
654 all waiting network requests, nothing to do */
655 spin_unlock(&cifs_tcp_ses_lock);
656 return;
657 } else if (tcon->tc_count == 1)
658 tcon->tidStatus = CifsExiting;
659 spin_unlock(&cifs_tcp_ses_lock);
660
661 /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
662 /* cancel_notify_requests(tcon); */
663 if (tcon->ses && tcon->ses->server) {
664 cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
665 wake_up_all(&tcon->ses->server->request_q);
666 wake_up_all(&tcon->ses->server->response_q);
667 msleep(1); /* yield */
668 /* we have to kick the requests once more */
669 wake_up_all(&tcon->ses->server->response_q);
670 msleep(1);
671 }
672
673 return;
674 }
675
676 #ifdef CONFIG_CIFS_STATS2
cifs_show_stats(struct seq_file * s,struct dentry * root)677 static int cifs_show_stats(struct seq_file *s, struct dentry *root)
678 {
679 /* BB FIXME */
680 return 0;
681 }
682 #endif
683
cifs_remount(struct super_block * sb,int * flags,char * data)684 static int cifs_remount(struct super_block *sb, int *flags, char *data)
685 {
686 sync_filesystem(sb);
687 *flags |= SB_NODIRATIME;
688 return 0;
689 }
690
cifs_drop_inode(struct inode * inode)691 static int cifs_drop_inode(struct inode *inode)
692 {
693 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
694
695 /* no serverino => unconditional eviction */
696 return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
697 generic_drop_inode(inode);
698 }
699
700 static const struct super_operations cifs_super_ops = {
701 .statfs = cifs_statfs,
702 .alloc_inode = cifs_alloc_inode,
703 .free_inode = cifs_free_inode,
704 .drop_inode = cifs_drop_inode,
705 .evict_inode = cifs_evict_inode,
706 /* .delete_inode = cifs_delete_inode, */ /* Do not need above
707 function unless later we add lazy close of inodes or unless the
708 kernel forgets to call us with the same number of releases (closes)
709 as opens */
710 .show_options = cifs_show_options,
711 .umount_begin = cifs_umount_begin,
712 .remount_fs = cifs_remount,
713 #ifdef CONFIG_CIFS_STATS2
714 .show_stats = cifs_show_stats,
715 #endif
716 };
717
718 /*
719 * Get root dentry from superblock according to prefix path mount option.
720 * Return dentry with refcount + 1 on success and NULL otherwise.
721 */
722 static struct dentry *
cifs_get_root(struct smb_vol * vol,struct super_block * sb)723 cifs_get_root(struct smb_vol *vol, struct super_block *sb)
724 {
725 struct dentry *dentry;
726 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
727 char *full_path = NULL;
728 char *s, *p;
729 char sep;
730
731 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
732 return dget(sb->s_root);
733
734 full_path = cifs_build_path_to_root(vol, cifs_sb,
735 cifs_sb_master_tcon(cifs_sb), 0);
736 if (full_path == NULL)
737 return ERR_PTR(-ENOMEM);
738
739 cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
740
741 sep = CIFS_DIR_SEP(cifs_sb);
742 dentry = dget(sb->s_root);
743 p = s = full_path;
744
745 do {
746 struct inode *dir = d_inode(dentry);
747 struct dentry *child;
748
749 if (!S_ISDIR(dir->i_mode)) {
750 dput(dentry);
751 dentry = ERR_PTR(-ENOTDIR);
752 break;
753 }
754
755 /* skip separators */
756 while (*s == sep)
757 s++;
758 if (!*s)
759 break;
760 p = s++;
761 /* next separator */
762 while (*s && *s != sep)
763 s++;
764
765 child = lookup_positive_unlocked(p, dentry, s - p);
766 dput(dentry);
767 dentry = child;
768 } while (!IS_ERR(dentry));
769 kfree(full_path);
770 return dentry;
771 }
772
cifs_set_super(struct super_block * sb,void * data)773 static int cifs_set_super(struct super_block *sb, void *data)
774 {
775 struct cifs_mnt_data *mnt_data = data;
776 sb->s_fs_info = mnt_data->cifs_sb;
777 return set_anon_super(sb, NULL);
778 }
779
780 static struct dentry *
cifs_smb3_do_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data,bool is_smb3)781 cifs_smb3_do_mount(struct file_system_type *fs_type,
782 int flags, const char *dev_name, void *data, bool is_smb3)
783 {
784 int rc;
785 struct super_block *sb;
786 struct cifs_sb_info *cifs_sb;
787 struct smb_vol *volume_info;
788 struct cifs_mnt_data mnt_data;
789 struct dentry *root;
790
791 /*
792 * Prints in Kernel / CIFS log the attempted mount operation
793 * If CIFS_DEBUG && cifs_FYI
794 */
795 if (cifsFYI)
796 cifs_dbg(FYI, "Devname: %s flags: %d\n", dev_name, flags);
797 else
798 cifs_info("Attempting to mount %s\n", dev_name);
799
800 volume_info = cifs_get_volume_info((char *)data, dev_name, is_smb3);
801 if (IS_ERR(volume_info))
802 return ERR_CAST(volume_info);
803
804 cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
805 if (cifs_sb == NULL) {
806 root = ERR_PTR(-ENOMEM);
807 goto out_nls;
808 }
809
810 cifs_sb->mountdata = kstrndup(data, PAGE_SIZE, GFP_KERNEL);
811 if (cifs_sb->mountdata == NULL) {
812 root = ERR_PTR(-ENOMEM);
813 goto out_free;
814 }
815
816 rc = cifs_setup_cifs_sb(volume_info, cifs_sb);
817 if (rc) {
818 root = ERR_PTR(rc);
819 goto out_free;
820 }
821
822 rc = cifs_mount(cifs_sb, volume_info);
823 if (rc) {
824 if (!(flags & SB_SILENT))
825 cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
826 rc);
827 root = ERR_PTR(rc);
828 goto out_free;
829 }
830
831 mnt_data.vol = volume_info;
832 mnt_data.cifs_sb = cifs_sb;
833 mnt_data.flags = flags;
834
835 /* BB should we make this contingent on mount parm? */
836 flags |= SB_NODIRATIME | SB_NOATIME;
837
838 sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
839 if (IS_ERR(sb)) {
840 root = ERR_CAST(sb);
841 cifs_umount(cifs_sb);
842 goto out;
843 }
844
845 if (sb->s_root) {
846 cifs_dbg(FYI, "Use existing superblock\n");
847 cifs_umount(cifs_sb);
848 } else {
849 rc = cifs_read_super(sb);
850 if (rc) {
851 root = ERR_PTR(rc);
852 goto out_super;
853 }
854
855 sb->s_flags |= SB_ACTIVE;
856 }
857
858 root = cifs_get_root(volume_info, sb);
859 if (IS_ERR(root))
860 goto out_super;
861
862 cifs_dbg(FYI, "dentry root is: %p\n", root);
863 goto out;
864
865 out_super:
866 deactivate_locked_super(sb);
867 return root;
868 out:
869 cifs_cleanup_volume_info(volume_info);
870 return root;
871
872 out_free:
873 kfree(cifs_sb->prepath);
874 kfree(cifs_sb->mountdata);
875 kfree(cifs_sb);
876 out_nls:
877 unload_nls(volume_info->local_nls);
878 goto out;
879 }
880
881 static struct dentry *
smb3_do_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)882 smb3_do_mount(struct file_system_type *fs_type,
883 int flags, const char *dev_name, void *data)
884 {
885 return cifs_smb3_do_mount(fs_type, flags, dev_name, data, true);
886 }
887
888 static struct dentry *
cifs_do_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)889 cifs_do_mount(struct file_system_type *fs_type,
890 int flags, const char *dev_name, void *data)
891 {
892 return cifs_smb3_do_mount(fs_type, flags, dev_name, data, false);
893 }
894
895 static ssize_t
cifs_loose_read_iter(struct kiocb * iocb,struct iov_iter * iter)896 cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
897 {
898 ssize_t rc;
899 struct inode *inode = file_inode(iocb->ki_filp);
900
901 if (iocb->ki_flags & IOCB_DIRECT)
902 return cifs_user_readv(iocb, iter);
903
904 rc = cifs_revalidate_mapping(inode);
905 if (rc)
906 return rc;
907
908 return generic_file_read_iter(iocb, iter);
909 }
910
cifs_file_write_iter(struct kiocb * iocb,struct iov_iter * from)911 static ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
912 {
913 struct inode *inode = file_inode(iocb->ki_filp);
914 struct cifsInodeInfo *cinode = CIFS_I(inode);
915 ssize_t written;
916 int rc;
917
918 if (iocb->ki_filp->f_flags & O_DIRECT) {
919 written = cifs_user_writev(iocb, from);
920 if (written > 0 && CIFS_CACHE_READ(cinode)) {
921 cifs_zap_mapping(inode);
922 cifs_dbg(FYI,
923 "Set no oplock for inode=%p after a write operation\n",
924 inode);
925 cinode->oplock = 0;
926 }
927 return written;
928 }
929
930 written = cifs_get_writer(cinode);
931 if (written)
932 return written;
933
934 written = generic_file_write_iter(iocb, from);
935
936 if (CIFS_CACHE_WRITE(CIFS_I(inode)))
937 goto out;
938
939 rc = filemap_fdatawrite(inode->i_mapping);
940 if (rc)
941 cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
942 rc, inode);
943
944 out:
945 cifs_put_writer(cinode);
946 return written;
947 }
948
cifs_llseek(struct file * file,loff_t offset,int whence)949 static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
950 {
951 struct cifsFileInfo *cfile = file->private_data;
952 struct cifs_tcon *tcon;
953
954 /*
955 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
956 * the cached file length
957 */
958 if (whence != SEEK_SET && whence != SEEK_CUR) {
959 int rc;
960 struct inode *inode = file_inode(file);
961
962 /*
963 * We need to be sure that all dirty pages are written and the
964 * server has the newest file length.
965 */
966 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
967 inode->i_mapping->nrpages != 0) {
968 rc = filemap_fdatawait(inode->i_mapping);
969 if (rc) {
970 mapping_set_error(inode->i_mapping, rc);
971 return rc;
972 }
973 }
974 /*
975 * Some applications poll for the file length in this strange
976 * way so we must seek to end on non-oplocked files by
977 * setting the revalidate time to zero.
978 */
979 CIFS_I(inode)->time = 0;
980
981 rc = cifs_revalidate_file_attr(file);
982 if (rc < 0)
983 return (loff_t)rc;
984 }
985 if (cfile && cfile->tlink) {
986 tcon = tlink_tcon(cfile->tlink);
987 if (tcon->ses->server->ops->llseek)
988 return tcon->ses->server->ops->llseek(file, tcon,
989 offset, whence);
990 }
991 return generic_file_llseek(file, offset, whence);
992 }
993
994 static int
cifs_setlease(struct file * file,long arg,struct file_lock ** lease,void ** priv)995 cifs_setlease(struct file *file, long arg, struct file_lock **lease, void **priv)
996 {
997 /*
998 * Note that this is called by vfs setlease with i_lock held to
999 * protect *lease from going away.
1000 */
1001 struct inode *inode = file_inode(file);
1002 struct cifsFileInfo *cfile = file->private_data;
1003
1004 if (!(S_ISREG(inode->i_mode)))
1005 return -EINVAL;
1006
1007 /* Check if file is oplocked if this is request for new lease */
1008 if (arg == F_UNLCK ||
1009 ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) ||
1010 ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
1011 return generic_setlease(file, arg, lease, priv);
1012 else if (tlink_tcon(cfile->tlink)->local_lease &&
1013 !CIFS_CACHE_READ(CIFS_I(inode)))
1014 /*
1015 * If the server claims to support oplock on this file, then we
1016 * still need to check oplock even if the local_lease mount
1017 * option is set, but there are servers which do not support
1018 * oplock for which this mount option may be useful if the user
1019 * knows that the file won't be changed on the server by anyone
1020 * else.
1021 */
1022 return generic_setlease(file, arg, lease, priv);
1023 else
1024 return -EAGAIN;
1025 }
1026
1027 struct file_system_type cifs_fs_type = {
1028 .owner = THIS_MODULE,
1029 .name = "cifs",
1030 .mount = cifs_do_mount,
1031 .kill_sb = cifs_kill_sb,
1032 .fs_flags = FS_RENAME_DOES_D_MOVE,
1033 };
1034 MODULE_ALIAS_FS("cifs");
1035
1036 struct file_system_type smb3_fs_type = {
1037 .owner = THIS_MODULE,
1038 .name = "smb3",
1039 .mount = smb3_do_mount,
1040 .kill_sb = cifs_kill_sb,
1041 .fs_flags = FS_RENAME_DOES_D_MOVE,
1042 };
1043 MODULE_ALIAS_FS("smb3");
1044 MODULE_ALIAS("smb3");
1045
1046 const struct inode_operations cifs_dir_inode_ops = {
1047 .create = cifs_create,
1048 .atomic_open = cifs_atomic_open,
1049 .lookup = cifs_lookup,
1050 .getattr = cifs_getattr,
1051 .unlink = cifs_unlink,
1052 .link = cifs_hardlink,
1053 .mkdir = cifs_mkdir,
1054 .rmdir = cifs_rmdir,
1055 .rename = cifs_rename2,
1056 .permission = cifs_permission,
1057 .setattr = cifs_setattr,
1058 .symlink = cifs_symlink,
1059 .mknod = cifs_mknod,
1060 .listxattr = cifs_listxattr,
1061 };
1062
1063 const struct inode_operations cifs_file_inode_ops = {
1064 .setattr = cifs_setattr,
1065 .getattr = cifs_getattr,
1066 .permission = cifs_permission,
1067 .listxattr = cifs_listxattr,
1068 .fiemap = cifs_fiemap,
1069 };
1070
1071 const struct inode_operations cifs_symlink_inode_ops = {
1072 .get_link = cifs_get_link,
1073 .permission = cifs_permission,
1074 .listxattr = cifs_listxattr,
1075 };
1076
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)1077 static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,
1078 struct file *dst_file, loff_t destoff, loff_t len,
1079 unsigned int remap_flags)
1080 {
1081 struct inode *src_inode = file_inode(src_file);
1082 struct inode *target_inode = file_inode(dst_file);
1083 struct cifsFileInfo *smb_file_src = src_file->private_data;
1084 struct cifsFileInfo *smb_file_target;
1085 struct cifs_tcon *target_tcon;
1086 unsigned int xid;
1087 int rc;
1088
1089 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
1090 return -EINVAL;
1091
1092 cifs_dbg(FYI, "clone range\n");
1093
1094 xid = get_xid();
1095
1096 if (!src_file->private_data || !dst_file->private_data) {
1097 rc = -EBADF;
1098 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1099 goto out;
1100 }
1101
1102 smb_file_target = dst_file->private_data;
1103 target_tcon = tlink_tcon(smb_file_target->tlink);
1104
1105 /*
1106 * Note: cifs case is easier than btrfs since server responsible for
1107 * checks for proper open modes and file type and if it wants
1108 * server could even support copy of range where source = target
1109 */
1110 lock_two_nondirectories(target_inode, src_inode);
1111
1112 if (len == 0)
1113 len = src_inode->i_size - off;
1114
1115 cifs_dbg(FYI, "about to flush pages\n");
1116 /* should we flush first and last page first */
1117 truncate_inode_pages_range(&target_inode->i_data, destoff,
1118 PAGE_ALIGN(destoff + len)-1);
1119
1120 if (target_tcon->ses->server->ops->duplicate_extents)
1121 rc = target_tcon->ses->server->ops->duplicate_extents(xid,
1122 smb_file_src, smb_file_target, off, len, destoff);
1123 else
1124 rc = -EOPNOTSUPP;
1125
1126 /* force revalidate of size and timestamps of target file now
1127 that target is updated on the server */
1128 CIFS_I(target_inode)->time = 0;
1129 /* although unlocking in the reverse order from locking is not
1130 strictly necessary here it is a little cleaner to be consistent */
1131 unlock_two_nondirectories(src_inode, target_inode);
1132 out:
1133 free_xid(xid);
1134 return rc < 0 ? rc : len;
1135 }
1136
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)1137 ssize_t cifs_file_copychunk_range(unsigned int xid,
1138 struct file *src_file, loff_t off,
1139 struct file *dst_file, loff_t destoff,
1140 size_t len, unsigned int flags)
1141 {
1142 struct inode *src_inode = file_inode(src_file);
1143 struct inode *target_inode = file_inode(dst_file);
1144 struct cifsFileInfo *smb_file_src;
1145 struct cifsFileInfo *smb_file_target;
1146 struct cifs_tcon *src_tcon;
1147 struct cifs_tcon *target_tcon;
1148 ssize_t rc;
1149
1150 cifs_dbg(FYI, "copychunk range\n");
1151
1152 if (!src_file->private_data || !dst_file->private_data) {
1153 rc = -EBADF;
1154 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1155 goto out;
1156 }
1157
1158 rc = -EXDEV;
1159 smb_file_target = dst_file->private_data;
1160 smb_file_src = src_file->private_data;
1161 src_tcon = tlink_tcon(smb_file_src->tlink);
1162 target_tcon = tlink_tcon(smb_file_target->tlink);
1163
1164 if (src_tcon->ses != target_tcon->ses) {
1165 cifs_dbg(VFS, "source and target of copy not on same server\n");
1166 goto out;
1167 }
1168
1169 rc = -EOPNOTSUPP;
1170 if (!target_tcon->ses->server->ops->copychunk_range)
1171 goto out;
1172
1173 /*
1174 * Note: cifs case is easier than btrfs since server responsible for
1175 * checks for proper open modes and file type and if it wants
1176 * server could even support copy of range where source = target
1177 */
1178 lock_two_nondirectories(target_inode, src_inode);
1179
1180 cifs_dbg(FYI, "about to flush pages\n");
1181 /* should we flush first and last page first */
1182 truncate_inode_pages(&target_inode->i_data, 0);
1183
1184 rc = file_modified(dst_file);
1185 if (!rc)
1186 rc = target_tcon->ses->server->ops->copychunk_range(xid,
1187 smb_file_src, smb_file_target, off, len, destoff);
1188
1189 file_accessed(src_file);
1190
1191 /* force revalidate of size and timestamps of target file now
1192 * that target is updated on the server
1193 */
1194 CIFS_I(target_inode)->time = 0;
1195 /* although unlocking in the reverse order from locking is not
1196 * strictly necessary here it is a little cleaner to be consistent
1197 */
1198 unlock_two_nondirectories(src_inode, target_inode);
1199
1200 out:
1201 return rc;
1202 }
1203
1204 /*
1205 * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync()
1206 * is a dummy operation.
1207 */
cifs_dir_fsync(struct file * file,loff_t start,loff_t end,int datasync)1208 static int cifs_dir_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1209 {
1210 cifs_dbg(FYI, "Sync directory - name: %pD datasync: 0x%x\n",
1211 file, datasync);
1212
1213 return 0;
1214 }
1215
cifs_copy_file_range(struct file * src_file,loff_t off,struct file * dst_file,loff_t destoff,size_t len,unsigned int flags)1216 static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off,
1217 struct file *dst_file, loff_t destoff,
1218 size_t len, unsigned int flags)
1219 {
1220 unsigned int xid = get_xid();
1221 ssize_t rc;
1222 struct cifsFileInfo *cfile = dst_file->private_data;
1223
1224 if (cfile->swapfile) {
1225 rc = -EOPNOTSUPP;
1226 free_xid(xid);
1227 return rc;
1228 }
1229
1230 rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
1231 len, flags);
1232 free_xid(xid);
1233
1234 if (rc == -EOPNOTSUPP || rc == -EXDEV)
1235 rc = generic_copy_file_range(src_file, off, dst_file,
1236 destoff, len, flags);
1237 return rc;
1238 }
1239
1240 const struct file_operations cifs_file_ops = {
1241 .read_iter = cifs_loose_read_iter,
1242 .write_iter = cifs_file_write_iter,
1243 .open = cifs_open,
1244 .release = cifs_close,
1245 .lock = cifs_lock,
1246 .flock = cifs_flock,
1247 .fsync = cifs_fsync,
1248 .flush = cifs_flush,
1249 .mmap = cifs_file_mmap,
1250 .splice_read = generic_file_splice_read,
1251 .splice_write = iter_file_splice_write,
1252 .llseek = cifs_llseek,
1253 .unlocked_ioctl = cifs_ioctl,
1254 .copy_file_range = cifs_copy_file_range,
1255 .remap_file_range = cifs_remap_file_range,
1256 .setlease = cifs_setlease,
1257 .fallocate = cifs_fallocate,
1258 };
1259
1260 const struct file_operations cifs_file_strict_ops = {
1261 .read_iter = cifs_strict_readv,
1262 .write_iter = cifs_strict_writev,
1263 .open = cifs_open,
1264 .release = cifs_close,
1265 .lock = cifs_lock,
1266 .flock = cifs_flock,
1267 .fsync = cifs_strict_fsync,
1268 .flush = cifs_flush,
1269 .mmap = cifs_file_strict_mmap,
1270 .splice_read = generic_file_splice_read,
1271 .splice_write = iter_file_splice_write,
1272 .llseek = cifs_llseek,
1273 .unlocked_ioctl = cifs_ioctl,
1274 .copy_file_range = cifs_copy_file_range,
1275 .remap_file_range = cifs_remap_file_range,
1276 .setlease = cifs_setlease,
1277 .fallocate = cifs_fallocate,
1278 };
1279
1280 const struct file_operations cifs_file_direct_ops = {
1281 .read_iter = cifs_direct_readv,
1282 .write_iter = cifs_direct_writev,
1283 .open = cifs_open,
1284 .release = cifs_close,
1285 .lock = cifs_lock,
1286 .flock = cifs_flock,
1287 .fsync = cifs_fsync,
1288 .flush = cifs_flush,
1289 .mmap = cifs_file_mmap,
1290 .splice_read = generic_file_splice_read,
1291 .splice_write = iter_file_splice_write,
1292 .unlocked_ioctl = cifs_ioctl,
1293 .copy_file_range = cifs_copy_file_range,
1294 .remap_file_range = cifs_remap_file_range,
1295 .llseek = cifs_llseek,
1296 .setlease = cifs_setlease,
1297 .fallocate = cifs_fallocate,
1298 };
1299
1300 const struct file_operations cifs_file_nobrl_ops = {
1301 .read_iter = cifs_loose_read_iter,
1302 .write_iter = cifs_file_write_iter,
1303 .open = cifs_open,
1304 .release = cifs_close,
1305 .fsync = cifs_fsync,
1306 .flush = cifs_flush,
1307 .mmap = cifs_file_mmap,
1308 .splice_read = generic_file_splice_read,
1309 .splice_write = iter_file_splice_write,
1310 .llseek = cifs_llseek,
1311 .unlocked_ioctl = cifs_ioctl,
1312 .copy_file_range = cifs_copy_file_range,
1313 .remap_file_range = cifs_remap_file_range,
1314 .setlease = cifs_setlease,
1315 .fallocate = cifs_fallocate,
1316 };
1317
1318 const struct file_operations cifs_file_strict_nobrl_ops = {
1319 .read_iter = cifs_strict_readv,
1320 .write_iter = cifs_strict_writev,
1321 .open = cifs_open,
1322 .release = cifs_close,
1323 .fsync = cifs_strict_fsync,
1324 .flush = cifs_flush,
1325 .mmap = cifs_file_strict_mmap,
1326 .splice_read = generic_file_splice_read,
1327 .splice_write = iter_file_splice_write,
1328 .llseek = cifs_llseek,
1329 .unlocked_ioctl = cifs_ioctl,
1330 .copy_file_range = cifs_copy_file_range,
1331 .remap_file_range = cifs_remap_file_range,
1332 .setlease = cifs_setlease,
1333 .fallocate = cifs_fallocate,
1334 };
1335
1336 const struct file_operations cifs_file_direct_nobrl_ops = {
1337 .read_iter = cifs_direct_readv,
1338 .write_iter = cifs_direct_writev,
1339 .open = cifs_open,
1340 .release = cifs_close,
1341 .fsync = cifs_fsync,
1342 .flush = cifs_flush,
1343 .mmap = cifs_file_mmap,
1344 .splice_read = generic_file_splice_read,
1345 .splice_write = iter_file_splice_write,
1346 .unlocked_ioctl = cifs_ioctl,
1347 .copy_file_range = cifs_copy_file_range,
1348 .remap_file_range = cifs_remap_file_range,
1349 .llseek = cifs_llseek,
1350 .setlease = cifs_setlease,
1351 .fallocate = cifs_fallocate,
1352 };
1353
1354 const struct file_operations cifs_dir_ops = {
1355 .iterate_shared = cifs_readdir,
1356 .release = cifs_closedir,
1357 .read = generic_read_dir,
1358 .unlocked_ioctl = cifs_ioctl,
1359 .copy_file_range = cifs_copy_file_range,
1360 .remap_file_range = cifs_remap_file_range,
1361 .llseek = generic_file_llseek,
1362 .fsync = cifs_dir_fsync,
1363 };
1364
1365 static void
cifs_init_once(void * inode)1366 cifs_init_once(void *inode)
1367 {
1368 struct cifsInodeInfo *cifsi = inode;
1369
1370 inode_init_once(&cifsi->vfs_inode);
1371 init_rwsem(&cifsi->lock_sem);
1372 }
1373
1374 static int __init
cifs_init_inodecache(void)1375 cifs_init_inodecache(void)
1376 {
1377 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
1378 sizeof(struct cifsInodeInfo),
1379 0, (SLAB_RECLAIM_ACCOUNT|
1380 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1381 cifs_init_once);
1382 if (cifs_inode_cachep == NULL)
1383 return -ENOMEM;
1384
1385 return 0;
1386 }
1387
1388 static void
cifs_destroy_inodecache(void)1389 cifs_destroy_inodecache(void)
1390 {
1391 /*
1392 * Make sure all delayed rcu free inodes are flushed before we
1393 * destroy cache.
1394 */
1395 rcu_barrier();
1396 kmem_cache_destroy(cifs_inode_cachep);
1397 }
1398
1399 static int
cifs_init_request_bufs(void)1400 cifs_init_request_bufs(void)
1401 {
1402 /*
1403 * SMB2 maximum header size is bigger than CIFS one - no problems to
1404 * allocate some more bytes for CIFS.
1405 */
1406 size_t max_hdr_size = MAX_SMB2_HDR_SIZE;
1407
1408 if (CIFSMaxBufSize < 8192) {
1409 /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
1410 Unicode path name has to fit in any SMB/CIFS path based frames */
1411 CIFSMaxBufSize = 8192;
1412 } else if (CIFSMaxBufSize > 1024*127) {
1413 CIFSMaxBufSize = 1024 * 127;
1414 } else {
1415 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
1416 }
1417 /*
1418 cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
1419 CIFSMaxBufSize, CIFSMaxBufSize);
1420 */
1421 cifs_req_cachep = kmem_cache_create_usercopy("cifs_request",
1422 CIFSMaxBufSize + max_hdr_size, 0,
1423 SLAB_HWCACHE_ALIGN, 0,
1424 CIFSMaxBufSize + max_hdr_size,
1425 NULL);
1426 if (cifs_req_cachep == NULL)
1427 return -ENOMEM;
1428
1429 if (cifs_min_rcv < 1)
1430 cifs_min_rcv = 1;
1431 else if (cifs_min_rcv > 64) {
1432 cifs_min_rcv = 64;
1433 cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
1434 }
1435
1436 cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1437 cifs_req_cachep);
1438
1439 if (cifs_req_poolp == NULL) {
1440 kmem_cache_destroy(cifs_req_cachep);
1441 return -ENOMEM;
1442 }
1443 /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1444 almost all handle based requests (but not write response, nor is it
1445 sufficient for path based requests). A smaller size would have
1446 been more efficient (compacting multiple slab items on one 4k page)
1447 for the case in which debug was on, but this larger size allows
1448 more SMBs to use small buffer alloc and is still much more
1449 efficient to alloc 1 per page off the slab compared to 17K (5page)
1450 alloc of large cifs buffers even when page debugging is on */
1451 cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq",
1452 MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
1453 0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL);
1454 if (cifs_sm_req_cachep == NULL) {
1455 mempool_destroy(cifs_req_poolp);
1456 kmem_cache_destroy(cifs_req_cachep);
1457 return -ENOMEM;
1458 }
1459
1460 if (cifs_min_small < 2)
1461 cifs_min_small = 2;
1462 else if (cifs_min_small > 256) {
1463 cifs_min_small = 256;
1464 cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
1465 }
1466
1467 cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1468 cifs_sm_req_cachep);
1469
1470 if (cifs_sm_req_poolp == NULL) {
1471 mempool_destroy(cifs_req_poolp);
1472 kmem_cache_destroy(cifs_req_cachep);
1473 kmem_cache_destroy(cifs_sm_req_cachep);
1474 return -ENOMEM;
1475 }
1476
1477 return 0;
1478 }
1479
1480 static void
cifs_destroy_request_bufs(void)1481 cifs_destroy_request_bufs(void)
1482 {
1483 mempool_destroy(cifs_req_poolp);
1484 kmem_cache_destroy(cifs_req_cachep);
1485 mempool_destroy(cifs_sm_req_poolp);
1486 kmem_cache_destroy(cifs_sm_req_cachep);
1487 }
1488
1489 static int
cifs_init_mids(void)1490 cifs_init_mids(void)
1491 {
1492 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
1493 sizeof(struct mid_q_entry), 0,
1494 SLAB_HWCACHE_ALIGN, NULL);
1495 if (cifs_mid_cachep == NULL)
1496 return -ENOMEM;
1497
1498 /* 3 is a reasonable minimum number of simultaneous operations */
1499 cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
1500 if (cifs_mid_poolp == NULL) {
1501 kmem_cache_destroy(cifs_mid_cachep);
1502 return -ENOMEM;
1503 }
1504
1505 return 0;
1506 }
1507
1508 static void
cifs_destroy_mids(void)1509 cifs_destroy_mids(void)
1510 {
1511 mempool_destroy(cifs_mid_poolp);
1512 kmem_cache_destroy(cifs_mid_cachep);
1513 }
1514
1515 static int __init
init_cifs(void)1516 init_cifs(void)
1517 {
1518 int rc = 0;
1519 cifs_proc_init();
1520 INIT_LIST_HEAD(&cifs_tcp_ses_list);
1521 #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* unused temporarily */
1522 INIT_LIST_HEAD(&GlobalDnotifyReqList);
1523 INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
1524 #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */
1525 /*
1526 * Initialize Global counters
1527 */
1528 atomic_set(&sesInfoAllocCount, 0);
1529 atomic_set(&tconInfoAllocCount, 0);
1530 atomic_set(&tcpSesAllocCount, 0);
1531 atomic_set(&tcpSesReconnectCount, 0);
1532 atomic_set(&tconInfoReconnectCount, 0);
1533
1534 atomic_set(&bufAllocCount, 0);
1535 atomic_set(&smBufAllocCount, 0);
1536 #ifdef CONFIG_CIFS_STATS2
1537 atomic_set(&totBufAllocCount, 0);
1538 atomic_set(&totSmBufAllocCount, 0);
1539 if (slow_rsp_threshold < 1)
1540 cifs_dbg(FYI, "slow_response_threshold msgs disabled\n");
1541 else if (slow_rsp_threshold > 32767)
1542 cifs_dbg(VFS,
1543 "slow response threshold set higher than recommended (0 to 32767)\n");
1544 #endif /* CONFIG_CIFS_STATS2 */
1545
1546 atomic_set(&midCount, 0);
1547 GlobalCurrentXid = 0;
1548 GlobalTotalActiveXid = 0;
1549 GlobalMaxActiveXid = 0;
1550 spin_lock_init(&cifs_tcp_ses_lock);
1551 spin_lock_init(&GlobalMid_Lock);
1552
1553 cifs_lock_secret = get_random_u32();
1554
1555 if (cifs_max_pending < 2) {
1556 cifs_max_pending = 2;
1557 cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
1558 } else if (cifs_max_pending > CIFS_MAX_REQ) {
1559 cifs_max_pending = CIFS_MAX_REQ;
1560 cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
1561 CIFS_MAX_REQ);
1562 }
1563
1564 cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1565 if (!cifsiod_wq) {
1566 rc = -ENOMEM;
1567 goto out_clean_proc;
1568 }
1569
1570 /*
1571 * Consider in future setting limit!=0 maybe to min(num_of_cores - 1, 3)
1572 * so that we don't launch too many worker threads but
1573 * Documentation/core-api/workqueue.rst recommends setting it to 0
1574 */
1575
1576 /* WQ_UNBOUND allows decrypt tasks to run on any CPU */
1577 decrypt_wq = alloc_workqueue("smb3decryptd",
1578 WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1579 if (!decrypt_wq) {
1580 rc = -ENOMEM;
1581 goto out_destroy_cifsiod_wq;
1582 }
1583
1584 fileinfo_put_wq = alloc_workqueue("cifsfileinfoput",
1585 WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1586 if (!fileinfo_put_wq) {
1587 rc = -ENOMEM;
1588 goto out_destroy_decrypt_wq;
1589 }
1590
1591 cifsoplockd_wq = alloc_workqueue("cifsoplockd",
1592 WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1593 if (!cifsoplockd_wq) {
1594 rc = -ENOMEM;
1595 goto out_destroy_fileinfo_put_wq;
1596 }
1597
1598 rc = cifs_fscache_register();
1599 if (rc)
1600 goto out_destroy_cifsoplockd_wq;
1601
1602 rc = cifs_init_inodecache();
1603 if (rc)
1604 goto out_unreg_fscache;
1605
1606 rc = cifs_init_mids();
1607 if (rc)
1608 goto out_destroy_inodecache;
1609
1610 rc = cifs_init_request_bufs();
1611 if (rc)
1612 goto out_destroy_mids;
1613
1614 #ifdef CONFIG_CIFS_DFS_UPCALL
1615 rc = dfs_cache_init();
1616 if (rc)
1617 goto out_destroy_request_bufs;
1618 #endif /* CONFIG_CIFS_DFS_UPCALL */
1619 #ifdef CONFIG_CIFS_UPCALL
1620 rc = init_cifs_spnego();
1621 if (rc)
1622 goto out_destroy_dfs_cache;
1623 #endif /* CONFIG_CIFS_UPCALL */
1624
1625 rc = init_cifs_idmap();
1626 if (rc)
1627 goto out_register_key_type;
1628
1629 rc = register_filesystem(&cifs_fs_type);
1630 if (rc)
1631 goto out_init_cifs_idmap;
1632
1633 rc = register_filesystem(&smb3_fs_type);
1634 if (rc) {
1635 unregister_filesystem(&cifs_fs_type);
1636 goto out_init_cifs_idmap;
1637 }
1638
1639 return 0;
1640
1641 out_init_cifs_idmap:
1642 exit_cifs_idmap();
1643 out_register_key_type:
1644 #ifdef CONFIG_CIFS_UPCALL
1645 exit_cifs_spnego();
1646 out_destroy_dfs_cache:
1647 #endif
1648 #ifdef CONFIG_CIFS_DFS_UPCALL
1649 dfs_cache_destroy();
1650 out_destroy_request_bufs:
1651 #endif
1652 cifs_destroy_request_bufs();
1653 out_destroy_mids:
1654 cifs_destroy_mids();
1655 out_destroy_inodecache:
1656 cifs_destroy_inodecache();
1657 out_unreg_fscache:
1658 cifs_fscache_unregister();
1659 out_destroy_cifsoplockd_wq:
1660 destroy_workqueue(cifsoplockd_wq);
1661 out_destroy_fileinfo_put_wq:
1662 destroy_workqueue(fileinfo_put_wq);
1663 out_destroy_decrypt_wq:
1664 destroy_workqueue(decrypt_wq);
1665 out_destroy_cifsiod_wq:
1666 destroy_workqueue(cifsiod_wq);
1667 out_clean_proc:
1668 cifs_proc_clean();
1669 return rc;
1670 }
1671
1672 static void __exit
exit_cifs(void)1673 exit_cifs(void)
1674 {
1675 cifs_dbg(NOISY, "exit_smb3\n");
1676 unregister_filesystem(&cifs_fs_type);
1677 unregister_filesystem(&smb3_fs_type);
1678 cifs_dfs_release_automount_timer();
1679 exit_cifs_idmap();
1680 #ifdef CONFIG_CIFS_UPCALL
1681 exit_cifs_spnego();
1682 #endif
1683 #ifdef CONFIG_CIFS_DFS_UPCALL
1684 dfs_cache_destroy();
1685 #endif
1686 cifs_destroy_request_bufs();
1687 cifs_destroy_mids();
1688 cifs_destroy_inodecache();
1689 cifs_fscache_unregister();
1690 destroy_workqueue(cifsoplockd_wq);
1691 destroy_workqueue(decrypt_wq);
1692 destroy_workqueue(fileinfo_put_wq);
1693 destroy_workqueue(cifsiod_wq);
1694 cifs_proc_clean();
1695 }
1696
1697 MODULE_AUTHOR("Steve French");
1698 MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
1699 MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
1700 MODULE_DESCRIPTION
1701 ("VFS to access SMB3 servers e.g. Samba, Macs, Azure and Windows (and "
1702 "also older servers complying with the SNIA CIFS Specification)");
1703 MODULE_VERSION(CIFS_VERSION);
1704 MODULE_SOFTDEP("ecb");
1705 MODULE_SOFTDEP("hmac");
1706 MODULE_SOFTDEP("md4");
1707 MODULE_SOFTDEP("md5");
1708 MODULE_SOFTDEP("nls");
1709 MODULE_SOFTDEP("aes");
1710 MODULE_SOFTDEP("cmac");
1711 MODULE_SOFTDEP("sha256");
1712 MODULE_SOFTDEP("sha512");
1713 MODULE_SOFTDEP("aead2");
1714 MODULE_SOFTDEP("ccm");
1715 MODULE_SOFTDEP("gcm");
1716 module_init(init_cifs)
1717 module_exit(exit_cifs)
1718