blob: ac9034fce409d27be6640a1fd1dbcea66c3abf32 [file] [log] [blame]
Steve French929be902021-06-18 00:31:49 -05001// SPDX-License-Identifier: LGPL-2.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Steve French2b280fa2008-05-17 03:12:45 +00004 * Copyright (C) International Business Machines Corp., 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * Common Internet FileSystem (CIFS) client
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11/* Note that BB means BUGBUG (ie something to fix eventually) */
12
13#include <linux/module.h>
14#include <linux/fs.h>
Jeff Layton5970e152022-11-20 09:15:34 -050015#include <linux/filelock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/mount.h>
17#include <linux/slab.h>
18#include <linux/init.h>
19#include <linux/list.h>
20#include <linux/seq_file.h>
21#include <linux/vfs.h>
22#include <linux/mempool.h>
Steve French6ab16d22005-11-29 20:55:11 -080023#include <linux/delay.h>
Steve French45af7a02006-04-21 22:52:25 +000024#include <linux/kthread.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080025#include <linux/freezer.h>
Al Virofec11dd2011-07-18 13:50:40 -040026#include <linux/namei.h>
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -070027#include <linux/random.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020028#include <linux/uuid.h>
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +020029#include <linux/xattr.h>
Jeff Laytondea29032022-01-10 19:00:02 -050030#include <uapi/linux/magic.h>
Ben Greear3eb9a882010-09-01 17:06:02 -070031#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "cifsfs.h"
33#include "cifspdu.h"
34#define DECLARE_GLOBALS_HERE
35#include "cifsglob.h"
36#include "cifsproto.h"
37#include "cifs_debug.h"
38#include "cifs_fs_sb.h"
39#include <linux/mm.h>
Jeff Layton84a15b92007-11-03 05:02:24 +000040#include <linux/key-type.h>
Jeff Laytone5459372007-11-03 05:11:06 +000041#include "cifs_spnego.h"
Suresh Jayaramanf579cf32010-07-05 18:11:50 +053042#include "fscache.h"
Paulo Alcantara1c780222018-11-14 16:24:03 -020043#ifdef CONFIG_CIFS_DFS_UPCALL
44#include "dfs_cache.h"
45#endif
Samuel Cabrero06f08da2020-11-30 19:02:49 +010046#ifdef CONFIG_CIFS_SWN_UPCALL
47#include "netlink.h"
48#endif
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -060049#include "fs_context.h"
Ronnie Sahlberg05b98fd2022-08-10 22:00:08 -050050#include "cached_dir.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Deepa Dinamanicb7a69e2019-03-22 14:32:35 -070052/*
53 * DOS dates from 1980/1/1 through 2107/12/31
54 * Protocol specifications indicate the range should be to 119, which
55 * limits maximum year to 2099. But this range has not been checked.
56 */
57#define SMB_DATE_MAX (127<<9 | 12<<5 | 31)
58#define SMB_DATE_MIN (0<<9 | 1<<5 | 1)
59#define SMB_TIME_MAX (23<<11 | 59<<5 | 29)
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061int cifsFYI = 0;
Kees Cook14042972016-03-17 14:22:54 -070062bool traceSMB;
Steve Frenche7504732011-10-12 17:47:03 -050063bool enable_oplocks = true;
Kees Cook14042972016-03-17 14:22:54 -070064bool linuxExtEnabled = true;
65bool lookupCacheEnabled = true;
Steve Frenchf92a7202018-05-24 04:11:07 -050066bool disable_legacy_dialects; /* false by default */
Steve Frenchfee742b2021-04-27 23:07:19 -050067bool enable_gcm_256 = true;
Steve Frenchfbfd0b42020-09-11 16:19:28 -050068bool require_gcm_256; /* false by default */
Steve French53d31a32021-07-05 15:05:39 -050069bool enable_negotiate_signing; /* false by default */
Jeff Layton04912d62010-04-24 07:57:45 -040070unsigned int global_secflags = CIFSSEC_DEF;
Steve French39798772006-05-31 22:40:51 +000071/* unsigned int ntlmv2_support = 0; */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072unsigned int sign_CIFS_PDUs = 1;
Steve French1bfa25e2022-07-24 22:47:59 -050073
74/*
75 * Global transaction id (XID) information
76 */
77unsigned int GlobalCurrentXid; /* protected by GlobalMid_Sem */
78unsigned int GlobalTotalActiveXid; /* prot by GlobalMid_Sem */
79unsigned int GlobalMaxActiveXid; /* prot by GlobalMid_Sem */
80spinlock_t GlobalMid_Lock; /* protects above & list operations on midQ entries */
81
82/*
83 * Global counters, updated atomically
84 */
85atomic_t sesInfoAllocCount;
86atomic_t tconInfoAllocCount;
87atomic_t tcpSesNextId;
88atomic_t tcpSesAllocCount;
89atomic_t tcpSesReconnectCount;
90atomic_t tconInfoReconnectCount;
91
Steve Frenchc2c17dd2022-07-15 23:45:45 -050092atomic_t mid_count;
93atomic_t buf_alloc_count;
94atomic_t small_buf_alloc_count;
95#ifdef CONFIG_CIFS_STATS2
96atomic_t total_buf_alloc_count;
97atomic_t total_small_buf_alloc_count;
98#endif/* STATS2 */
Steve French89e42f42022-07-15 23:57:08 -050099struct list_head cifs_tcp_ses_list;
100spinlock_t cifs_tcp_ses_lock;
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800101static const struct super_operations cifs_super_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
Germano Percossicb978ac2016-09-30 21:25:24 -0500103module_param(CIFSMaxBufSize, uint, 0444);
Steve French11911b92018-05-24 02:09:20 -0500104MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header) "
105 "for CIFS requests. "
Steve French63135e02007-07-17 17:34:02 +0000106 "Default: 16384 Range: 8192 to 130048");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
Germano Percossicb978ac2016-09-30 21:25:24 -0500108module_param(cifs_min_rcv, uint, 0444);
Steve French63135e02007-07-17 17:34:02 +0000109MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
110 "1 to 64");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111unsigned int cifs_min_small = 30;
Germano Percossicb978ac2016-09-30 21:25:24 -0500112module_param(cifs_min_small, uint, 0444);
Steve French63135e02007-07-17 17:34:02 +0000113MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
114 "Range: 2 to 256");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115unsigned int cifs_max_pending = CIFS_MAX_REQ;
Jeff Layton60654ce2012-11-25 08:00:34 -0500116module_param(cifs_max_pending, uint, 0444);
Steve French11911b92018-05-24 02:09:20 -0500117MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server for "
118 "CIFS/SMB1 dialect (N/A for SMB3) "
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300119 "Default: 32767 Range: 2 to 32767.");
Steve French00778e22018-09-18 14:05:18 -0500120#ifdef CONFIG_CIFS_STATS2
121unsigned int slow_rsp_threshold = 1;
122module_param(slow_rsp_threshold, uint, 0644);
123MODULE_PARM_DESC(slow_rsp_threshold, "Amount of time (in seconds) to wait "
124 "before logging that a response is delayed. "
125 "Default: 1 (if set to 0 disables msg).");
126#endif /* STATS2 */
127
Steve Frenche7504732011-10-12 17:47:03 -0500128module_param(enable_oplocks, bool, 0644);
Jeff Layton60654ce2012-11-25 08:00:34 -0500129MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1");
Steve Frenche7504732011-10-12 17:47:03 -0500130
Steve French29e27922020-10-14 20:24:09 -0500131module_param(enable_gcm_256, bool, 0644);
132MODULE_PARM_DESC(enable_gcm_256, "Enable requesting strongest (256 bit) GCM encryption. Default: n/N/0");
133
Steve Frenchfbfd0b42020-09-11 16:19:28 -0500134module_param(require_gcm_256, bool, 0644);
135MODULE_PARM_DESC(require_gcm_256, "Require strongest (256 bit) GCM encryption. Default: n/N/0");
136
Steve French53d31a32021-07-05 15:05:39 -0500137module_param(enable_negotiate_signing, bool, 0644);
138MODULE_PARM_DESC(enable_negotiate_signing, "Enable negotiating packet signing algorithm with server. Default: n/N/0");
139
Steve Frenchf92a7202018-05-24 04:11:07 -0500140module_param(disable_legacy_dialects, bool, 0644);
141MODULE_PARM_DESC(disable_legacy_dialects, "To improve security it may be "
142 "helpful to restrict the ability to "
143 "override the default dialects (SMB2.1, "
144 "SMB3 and SMB3.02) on mount with old "
145 "dialects (CIFS/SMB1 and SMB2) since "
146 "vers=1.0 (CIFS/SMB1) and vers=2.0 are weaker"
147 " and less secure. Default: n/N/0");
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149extern mempool_t *cifs_sm_req_poolp;
150extern mempool_t *cifs_req_poolp;
151extern mempool_t *cifs_mid_poolp;
152
Jeff Laytonda472fc2012-03-23 14:40:53 -0400153struct workqueue_struct *cifsiod_wq;
Steve French35cf94a2019-09-07 01:09:49 -0500154struct workqueue_struct *decrypt_wq;
Ronnie Sahlberg32546a92019-11-03 13:06:37 +1000155struct workqueue_struct *fileinfo_put_wq;
Rabin Vincent3998e6b82017-05-03 17:54:01 +0200156struct workqueue_struct *cifsoplockd_wq;
Rohith Surabattula860b69a2021-05-05 10:56:47 +0000157struct workqueue_struct *deferredclose_wq;
Jeff Layton3d224622016-05-24 06:27:44 -0400158__u32 cifs_lock_secret;
Jeff Laytonda472fc2012-03-23 14:40:53 -0400159
Mateusz Guzik24261fc2013-03-08 16:30:03 +0100160/*
161 * Bumps refcount for cifs super block.
162 * Note that it should be only called if a referece to VFS super block is
163 * already held, e.g. in open-type syscalls context. Otherwise it can race with
164 * atomic_dec_and_test in deactivate_locked_super.
165 */
166void
167cifs_sb_active(struct super_block *sb)
168{
169 struct cifs_sb_info *server = CIFS_SB(sb);
170
171 if (atomic_inc_return(&server->active) == 1)
172 atomic_inc(&sb->s_active);
173}
174
175void
176cifs_sb_deactive(struct super_block *sb)
177{
178 struct cifs_sb_info *server = CIFS_SB(sb);
179
180 if (atomic_dec_and_test(&server->active))
181 deactivate_super(sb);
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static int
Al Viro97d11522011-06-17 09:29:57 -0400185cifs_read_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 struct inode *inode;
Pavel Shilovskyb2e5cd32011-05-25 13:35:34 +0400188 struct cifs_sb_info *cifs_sb;
Jan Klos2f6c9472013-10-06 21:08:20 +0200189 struct cifs_tcon *tcon;
Deepa Dinamanicb7a69e2019-03-22 14:32:35 -0700190 struct timespec64 ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int rc = 0;
Steve French50c2f752007-07-13 00:33:32 +0000192
Pavel Shilovskyb2e5cd32011-05-25 13:35:34 +0400193 cifs_sb = CIFS_SB(sb);
Jan Klos2f6c9472013-10-06 21:08:20 +0200194 tcon = cifs_sb_master_tcon(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Al Viro2c6292a2011-06-17 09:05:48 -0400196 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800197 sb->s_flags |= SB_POSIXACL;
Al Viro2c6292a2011-06-17 09:05:48 -0400198
Steve French8a69e962018-06-29 16:06:15 -0500199 if (tcon->snapshot_time)
200 sb->s_flags |= SB_RDONLY;
201
Jan Klos2f6c9472013-10-06 21:08:20 +0200202 if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files)
Al Viro2c6292a2011-06-17 09:05:48 -0400203 sb->s_maxbytes = MAX_LFS_FILESIZE;
204 else
205 sb->s_maxbytes = MAX_NON_LFS;
206
Steve French553292a2019-10-11 17:36:13 -0700207 /*
208 * Some very old servers like DOS and OS/2 used 2 second granularity
209 * (while all current servers use 100ns granularity - see MS-DTYP)
210 * but 1 second is the maximum allowed granularity for the VFS
211 * so for old servers set time granularity to 1 second while for
212 * everything else (current servers) set it to 100ns.
213 */
Steve Frenchd4cfbf02019-10-08 00:27:14 -0500214 if ((tcon->ses->server->vals->protocol_id == SMB10_PROT_ID) &&
215 ((tcon->ses->capabilities &
216 tcon->ses->server->vals->cap_nt_find) == 0) &&
217 !tcon->unix_ext) {
218 sb->s_time_gran = 1000000000; /* 1 second is max allowed gran */
219 ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MIN), 0, 0);
220 sb->s_time_min = ts.tv_sec;
221 ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MAX),
222 cpu_to_le16(SMB_TIME_MAX), 0);
223 sb->s_time_max = ts.tv_sec;
224 } else {
225 /*
226 * Almost every server, including all SMB2+, uses DCE TIME
227 * ie 100 nanosecond units, since 1601. See MS-DTYP and MS-FSCC
228 */
229 sb->s_time_gran = 100;
Deepa Dinamanicb7a69e2019-03-22 14:32:35 -0700230 ts = cifs_NTtimeToUnix(0);
231 sb->s_time_min = ts.tv_sec;
232 ts = cifs_NTtimeToUnix(cpu_to_le64(S64_MAX));
233 sb->s_time_max = ts.tv_sec;
Deepa Dinamanicb7a69e2019-03-22 14:32:35 -0700234 }
235
Jeff Laytondea29032022-01-10 19:00:02 -0500236 sb->s_magic = CIFS_SUPER_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 sb->s_op = &cifs_super_ops;
Andreas Gruenbachera9ae0082016-04-22 12:11:38 +0200238 sb->s_xattr = cifs_xattr_handlers;
Jan Kara851ea082017-04-12 12:24:34 +0200239 rc = super_setup_bdi(sb);
240 if (rc)
241 goto out_no_root;
Steve Frenchb8d64f82021-04-24 21:46:23 -0500242 /* tune readahead according to rsize if readahead size not set on mount */
Rohith Surabattula06a46652022-03-07 18:37:22 +0000243 if (cifs_sb->ctx->rsize == 0)
244 cifs_sb->ctx->rsize =
245 tcon->ses->server->ops->negotiate_rsize(tcon, cifs_sb->ctx);
Steve Frenchb8d64f82021-04-24 21:46:23 -0500246 if (cifs_sb->ctx->rasize)
247 sb->s_bdi->ra_pages = cifs_sb->ctx->rasize / PAGE_SIZE;
248 else
249 sb->s_bdi->ra_pages = cifs_sb->ctx->rsize / PAGE_SIZE;
Jan Kara851ea082017-04-12 12:24:34 +0200250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 sb->s_blocksize = CIFS_MAX_MSGSIZE;
252 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
Shirish Pargaonkar9b6763e2011-02-21 23:56:59 -0600253 inode = cifs_root_iget(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
David Howellsce634ab2008-02-07 00:15:33 -0800255 if (IS_ERR(inode)) {
256 rc = PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 goto out_no_root;
258 }
259
Jan Klos2f6c9472013-10-06 21:08:20 +0200260 if (tcon->nocase)
Jeff Layton66ffd112013-07-30 11:38:44 -0400261 sb->s_d_op = &cifs_ci_dentry_ops;
262 else
263 sb->s_d_op = &cifs_dentry_ops;
264
Al Viro48fde702012-01-08 22:15:13 -0500265 sb->s_root = d_make_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!sb->s_root) {
267 rc = -ENOMEM;
268 goto out_no_root;
269 }
Steve French50c2f752007-07-13 00:33:32 +0000270
Paul Bollef3a6a602011-10-12 14:14:04 +0200271#ifdef CONFIG_CIFS_NFSD_EXPORT
Steve French7521a3c2007-07-11 18:30:34 +0000272 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500273 cifs_dbg(FYI, "export ops supported\n");
Steve French7521a3c2007-07-11 18:30:34 +0000274 sb->s_export_op = &cifs_export_ops;
275 }
Paul Bollef3a6a602011-10-12 14:14:04 +0200276#endif /* CONFIG_CIFS_NFSD_EXPORT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 return 0;
279
280out_no_root:
Joe Perchesf96637b2013-05-04 22:12:25 -0500281 cifs_dbg(VFS, "%s: get root inode failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return rc;
283}
284
Al Viro6d686172011-06-17 08:34:57 -0400285static void cifs_kill_sb(struct super_block *sb)
286{
287 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Ronnie Sahlberg269f67e2021-03-09 09:07:30 +1000288
Ronnie Sahlberg5e9c89d2021-03-09 09:07:31 +1000289 /*
290 * We ned to release all dentries for the cached directories
291 * before we kill the sb.
292 */
Ronnie Sahlberg269f67e2021-03-09 09:07:30 +1000293 if (cifs_sb->root) {
Ronnie Sahlberg05b98fd2022-08-10 22:00:08 -0500294 close_all_cached_dirs(cifs_sb);
Shyam Prasad Nd788e512022-04-01 06:25:17 +0000295
296 /* finally release root dentry */
Ronnie Sahlberg269f67e2021-03-09 09:07:30 +1000297 dput(cifs_sb->root);
298 cifs_sb->root = NULL;
299 }
300
Al Viro6d686172011-06-17 08:34:57 -0400301 kill_anon_super(sb);
Al Viro98ab4942011-06-17 09:32:10 -0400302 cifs_umount(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305static int
David Howells726c3342006-06-23 02:02:58 -0700306cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
David Howells726c3342006-06-23 02:02:58 -0700308 struct super_block *sb = dentry->d_sb;
Steve French39da9842008-04-28 04:04:34 +0000309 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Steve French96daf2b2011-05-27 04:34:02 +0000310 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
Pavel Shilovsky76ec5e32012-09-18 16:20:33 -0700311 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400312 unsigned int xid;
Pavel Shilovsky76ec5e32012-09-18 16:20:33 -0700313 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400315 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Steve French21ba3842018-06-24 23:18:52 -0500317 if (le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength) > 0)
318 buf->f_namelen =
319 le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength);
320 else
321 buf->f_namelen = PATH_MAX;
322
323 buf->f_fsid.val[0] = tcon->vol_serial_number;
324 /* are using part of create time for more randomness, see man statfs */
325 buf->f_fsid.val[1] = (int)le64_to_cpu(tcon->vol_create_time);
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 buf->f_files = 0; /* undefined */
328 buf->f_ffree = 0; /* unlimited */
329
Pavel Shilovsky76ec5e32012-09-18 16:20:33 -0700330 if (server->ops->queryfs)
Amir Goldstein0f060932020-02-03 21:46:43 +0200331 rc = server->ops->queryfs(xid, tcon, cifs_sb, buf);
Steve French39da9842008-04-28 04:04:34 +0000332
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400333 free_xid(xid);
Paulo Alcantara14302ee2021-03-08 12:00:49 -0300334 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Steve French31742c52014-08-17 08:38:47 -0500337static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
338{
Al Viro7119e222014-10-22 00:25:12 -0400339 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
Steve French31742c52014-08-17 08:38:47 -0500340 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
341 struct TCP_Server_Info *server = tcon->ses->server;
342
343 if (server->ops->fallocate)
344 return server->ops->fallocate(file, tcon, mode, off, len);
345
346 return -EOPNOTSUPP;
347}
348
Christian Brauner4609e1f2023-01-13 12:49:22 +0100349static int cifs_permission(struct mnt_idmap *idmap,
Christian Brauner549c7292021-01-21 14:19:43 +0100350 struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 struct cifs_sb_info *cifs_sb;
353
354 cifs_sb = CIFS_SB(inode->i_sb);
355
Miklos Szeredif696a362008-07-31 13:41:58 +0200356 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
357 if ((mask & MAY_EXEC) && !execute_ok(inode))
358 return -EACCES;
359 else
360 return 0;
361 } else /* file mode might have been restricted at mount time
Steve French50c2f752007-07-13 00:33:32 +0000362 on the client (above and beyond ACL on servers) for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 servers which do not support setting and viewing mode bits,
Steve French50c2f752007-07-13 00:33:32 +0000364 so allowing client to check permissions is useful */
Christian Brauner4609e1f2023-01-13 12:49:22 +0100365 return generic_permission(&nop_mnt_idmap, inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Christoph Lametere18b8902006-12-06 20:33:20 -0800368static struct kmem_cache *cifs_inode_cachep;
369static struct kmem_cache *cifs_req_cachep;
370static struct kmem_cache *cifs_mid_cachep;
Christoph Lametere18b8902006-12-06 20:33:20 -0800371static struct kmem_cache *cifs_sm_req_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372mempool_t *cifs_sm_req_poolp;
373mempool_t *cifs_req_poolp;
374mempool_t *cifs_mid_poolp;
375
376static struct inode *
377cifs_alloc_inode(struct super_block *sb)
378{
379 struct cifsInodeInfo *cifs_inode;
Muchun Songfd60b282022-03-22 14:41:03 -0700380 cifs_inode = alloc_inode_sb(sb, cifs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (!cifs_inode)
382 return NULL;
383 cifs_inode->cifsAttrs = 0x20; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 cifs_inode->time = 0;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700385 /*
386 * Until the file is open and we have gotten oplock info back from the
387 * server, can not assume caching of file data or metadata.
388 */
Pavel Shilovskyc6723622010-11-03 10:58:57 +0300389 cifs_set_oplock_level(cifs_inode, 0);
Jeff Laytonaff8d5c2014-04-30 09:31:45 -0400390 cifs_inode->flags = 0;
Sachin Prabhuc11f1df2014-03-11 16:11:47 +0000391 spin_lock_init(&cifs_inode->writers_lock);
392 cifs_inode->writers = 0;
David Howells874c8ca12022-06-09 21:46:04 +0100393 cifs_inode->netfs.inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
Jeff Laytonfbec9ab2009-04-03 13:44:00 -0400394 cifs_inode->server_eof = 0;
Jeff Layton20054bd2011-01-07 11:30:27 -0500395 cifs_inode->uniqueid = 0;
396 cifs_inode->createtime = 0;
Pavel Shilovsky42873b02013-09-05 21:30:16 +0400397 cifs_inode->epoch = 0;
Ronnie Sahlberg487317c2019-06-05 10:38:38 +1000398 spin_lock_init(&cifs_inode->open_file_lock);
Steve Frenchfa70b872016-09-22 00:39:34 -0500399 generate_random_uuid(cifs_inode->lease_key);
Paulo Alcantara76894f32022-10-03 18:43:50 -0300400 cifs_inode->symlink_target = NULL;
Steve French2a38e122017-07-08 18:48:15 -0500401
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700402 /*
403 * Can not set i_flags here - they get immediately overwritten to zero
404 * by the VFS.
405 */
David Howells874c8ca12022-06-09 21:46:04 +0100406 /* cifs_inode->netfs.inode.i_flags = S_NOATIME | S_NOCMTIME; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 INIT_LIST_HEAD(&cifs_inode->openFileList);
Pavel Shilovskyf45d3412012-09-19 06:22:43 -0700408 INIT_LIST_HEAD(&cifs_inode->llist);
Rohith Surabattulac3f207ab2021-04-13 00:26:42 -0500409 INIT_LIST_HEAD(&cifs_inode->deferred_closes);
410 spin_lock_init(&cifs_inode->deferred_lock);
David Howells874c8ca12022-06-09 21:46:04 +0100411 return &cifs_inode->netfs.inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414static void
Al Viroc2e68022019-04-14 23:18:35 -0400415cifs_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Paulo Alcantara76894f32022-10-03 18:43:50 -0300417 struct cifsInodeInfo *cinode = CIFS_I(inode);
418
419 if (S_ISLNK(inode->i_mode))
420 kfree(cinode->symlink_target);
421 kmem_cache_free(cifs_inode_cachep, cinode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Jeff Layton61f98ff2009-06-11 10:27:32 -0400424static void
Al Virob57922d2010-06-07 14:34:48 -0400425cifs_evict_inode(struct inode *inode)
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +0530426{
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700427 truncate_inode_pages_final(&inode->i_data);
David Howells70431bf2020-11-17 15:56:59 +0000428 if (inode->i_state & I_PINNING_FSCACHE_WB)
429 cifs_fscache_unuse_inode_cookie(inode, true);
430 cifs_fscache_release_inode_cookie(inode);
Jan Karadbd57682012-05-03 14:48:02 +0200431 clear_inode(inode);
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +0530432}
433
434static void
Jeff Layton61f98ff2009-06-11 10:27:32 -0400435cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
436{
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300437 struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
438 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
439
Fabian Frederick571d5972014-05-13 18:04:17 +0200440 seq_puts(s, ",addr=");
Jeff Layton61f98ff2009-06-11 10:27:32 -0400441
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300442 switch (server->dstaddr.ss_family) {
Jeff Layton61f98ff2009-06-11 10:27:32 -0400443 case AF_INET:
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300444 seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
Jeff Layton61f98ff2009-06-11 10:27:32 -0400445 break;
446 case AF_INET6:
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300447 seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
448 if (sa6->sin6_scope_id)
449 seq_printf(s, "%%%u", sa6->sin6_scope_id);
Jeff Layton61f98ff2009-06-11 10:27:32 -0400450 break;
451 default:
Fabian Frederick571d5972014-05-13 18:04:17 +0200452 seq_puts(s, "(unknown)");
Jeff Layton61f98ff2009-06-11 10:27:32 -0400453 }
Long Li8339dd32017-11-07 01:54:55 -0700454 if (server->rdma)
455 seq_puts(s, ",rdma");
Jeff Layton61f98ff2009-06-11 10:27:32 -0400456}
457
Jeff Layton3e715512011-06-13 11:50:41 -0400458static void
Jeff Layton28e11bd2013-05-26 07:01:00 -0400459cifs_show_security(struct seq_file *s, struct cifs_ses *ses)
Jeff Layton3e715512011-06-13 11:50:41 -0400460{
Steve Frencheda21162015-09-11 19:24:19 -0500461 if (ses->sectype == Unspecified) {
462 if (ses->user_name == NULL)
463 seq_puts(s, ",sec=none");
Jeff Layton28e11bd2013-05-26 07:01:00 -0400464 return;
Steve Frencheda21162015-09-11 19:24:19 -0500465 }
Jeff Layton28e11bd2013-05-26 07:01:00 -0400466
Fabian Frederick571d5972014-05-13 18:04:17 +0200467 seq_puts(s, ",sec=");
Jeff Layton3e715512011-06-13 11:50:41 -0400468
Jeff Layton28e11bd2013-05-26 07:01:00 -0400469 switch (ses->sectype) {
Jeff Layton3e715512011-06-13 11:50:41 -0400470 case NTLMv2:
Fabian Frederick571d5972014-05-13 18:04:17 +0200471 seq_puts(s, "ntlmv2");
Jeff Layton3e715512011-06-13 11:50:41 -0400472 break;
Jeff Layton3e715512011-06-13 11:50:41 -0400473 case Kerberos:
Petr Pavlu3f6166a2020-02-10 10:38:14 +0100474 seq_puts(s, "krb5");
Jeff Layton3e715512011-06-13 11:50:41 -0400475 break;
476 case RawNTLMSSP:
Fabian Frederick571d5972014-05-13 18:04:17 +0200477 seq_puts(s, "ntlmssp");
Jeff Layton3e715512011-06-13 11:50:41 -0400478 break;
479 default:
480 /* shouldn't ever happen */
Fabian Frederick571d5972014-05-13 18:04:17 +0200481 seq_puts(s, "unknown");
Jeff Layton3e715512011-06-13 11:50:41 -0400482 break;
483 }
484
Jeff Layton28e11bd2013-05-26 07:01:00 -0400485 if (ses->sign)
Fabian Frederick571d5972014-05-13 18:04:17 +0200486 seq_puts(s, "i");
Petr Pavlu3f6166a2020-02-10 10:38:14 +0100487
488 if (ses->sectype == Kerberos)
489 seq_printf(s, ",cruid=%u",
490 from_kuid_munged(&init_user_ns, ses->cred_uid));
Jeff Layton3e715512011-06-13 11:50:41 -0400491}
492
Jeff Laytond06b5052012-05-16 07:53:01 -0400493static void
494cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
495{
Fabian Frederick571d5972014-05-13 18:04:17 +0200496 seq_puts(s, ",cache=");
Jeff Laytond06b5052012-05-16 07:53:01 -0400497
498 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
Fabian Frederick571d5972014-05-13 18:04:17 +0200499 seq_puts(s, "strict");
Jeff Laytond06b5052012-05-16 07:53:01 -0400500 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
Fabian Frederick571d5972014-05-13 18:04:17 +0200501 seq_puts(s, "none");
Steve French41e033f2019-08-30 02:12:41 -0500502 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
503 seq_puts(s, "singleclient"); /* assume only one client access */
Steve French83bbfa72019-08-27 23:58:54 -0500504 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE)
505 seq_puts(s, "ro"); /* read only caching assumed */
Jeff Laytond06b5052012-05-16 07:53:01 -0400506 else
Fabian Frederick571d5972014-05-13 18:04:17 +0200507 seq_puts(s, "loose");
Jeff Laytond06b5052012-05-16 07:53:01 -0400508}
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510/*
Steve French653a5ef2020-12-14 20:08:10 -0600511 * cifs_show_devname() is used so we show the mount device name with correct
512 * format (e.g. forward slashes vs. back slashes) in /proc/mounts
513 */
514static int cifs_show_devname(struct seq_file *m, struct dentry *root)
515{
516 struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
Ronnie Sahlbergaf1a3d22021-02-11 16:06:16 +1000517 char *devname = kstrdup(cifs_sb->ctx->source, GFP_KERNEL);
Steve French653a5ef2020-12-14 20:08:10 -0600518
519 if (devname == NULL)
520 seq_puts(m, "none");
521 else {
522 convert_delimiter(devname, '/');
Maciek Borzecki0fc93222021-04-06 17:02:29 +0200523 /* escape all spaces in share names */
524 seq_escape(m, devname, " \t");
Steve French653a5ef2020-12-14 20:08:10 -0600525 kfree(devname);
526 }
527 return 0;
528}
529
530/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * cifs_show_options() is for displaying mount options in /proc/mounts.
532 * Not all settable options are displayed but most of the important
533 * ones are.
534 */
535static int
Al Viro34c80b12011-12-08 21:32:45 -0500536cifs_show_options(struct seq_file *s, struct dentry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Al Viro34c80b12011-12-08 21:32:45 -0500538 struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
Steve French96daf2b2011-05-27 04:34:02 +0000539 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
Ben Greear3eb9a882010-09-01 17:06:02 -0700540 struct sockaddr *srcaddr;
541 srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Kees Cooka068acf2015-09-04 15:44:57 -0700543 seq_show_option(s, "vers", tcon->ses->server->vals->version_string);
Jeff Layton28e11bd2013-05-26 07:01:00 -0400544 cifs_show_security(s, tcon->ses);
Jeff Laytond06b5052012-05-16 07:53:01 -0400545 cifs_show_cache_flavor(s, cifs_sb);
Jeff Layton3e715512011-06-13 11:50:41 -0400546
Steve French3e7a02d2019-09-11 21:46:20 -0500547 if (tcon->no_lease)
548 seq_puts(s, ",nolease");
Ronnie Sahlberg9ccecae2020-12-14 16:40:19 +1000549 if (cifs_sb->ctx->multiuser)
Fabian Frederick571d5972014-05-13 18:04:17 +0200550 seq_puts(s, ",multiuser");
Steve French8727c8a2011-02-25 01:11:56 -0600551 else if (tcon->ses->user_name)
Kees Cooka068acf2015-09-04 15:44:57 -0700552 seq_show_option(s, "username", tcon->ses->user_name);
Jeff Layton29e07c82010-09-29 19:51:12 -0400553
Ronnie Sahlberge55954a2018-08-10 11:31:10 +1000554 if (tcon->ses->domainName && tcon->ses->domainName[0] != 0)
Kees Cooka068acf2015-09-04 15:44:57 -0700555 seq_show_option(s, "domain", tcon->ses->domainName);
Jeff Layton8616e0f2009-06-11 10:27:28 -0400556
Ben Greear3eb9a882010-09-01 17:06:02 -0700557 if (srcaddr->sa_family != AF_UNSPEC) {
558 struct sockaddr_in *saddr4;
559 struct sockaddr_in6 *saddr6;
560 saddr4 = (struct sockaddr_in *)srcaddr;
561 saddr6 = (struct sockaddr_in6 *)srcaddr;
562 if (srcaddr->sa_family == AF_INET6)
563 seq_printf(s, ",srcaddr=%pI6c",
564 &saddr6->sin6_addr);
565 else if (srcaddr->sa_family == AF_INET)
566 seq_printf(s, ",srcaddr=%pI4",
567 &saddr4->sin_addr.s_addr);
568 else
569 seq_printf(s, ",srcaddr=BAD-AF:%i",
570 (int)(srcaddr->sa_family));
571 }
572
Eric W. Biederman1f682332013-02-06 01:20:20 -0800573 seq_printf(s, ",uid=%u",
Ronnie Sahlberg8401e932020-12-12 13:40:50 -0600574 from_kuid_munged(&init_user_ns, cifs_sb->ctx->linux_uid));
Jeff Layton340481a32009-06-11 10:27:29 -0400575 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
Fabian Frederick571d5972014-05-13 18:04:17 +0200576 seq_puts(s, ",forceuid");
Jeff Layton4486d6e2009-08-03 12:45:10 -0400577 else
Fabian Frederick571d5972014-05-13 18:04:17 +0200578 seq_puts(s, ",noforceuid");
Jeff Layton340481a32009-06-11 10:27:29 -0400579
Eric W. Biederman1f682332013-02-06 01:20:20 -0800580 seq_printf(s, ",gid=%u",
Ronnie Sahlberg8401e932020-12-12 13:40:50 -0600581 from_kgid_munged(&init_user_ns, cifs_sb->ctx->linux_gid));
Jeff Layton340481a32009-06-11 10:27:29 -0400582 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
Fabian Frederick571d5972014-05-13 18:04:17 +0200583 seq_puts(s, ",forcegid");
Jeff Layton4486d6e2009-08-03 12:45:10 -0400584 else
Fabian Frederick571d5972014-05-13 18:04:17 +0200585 seq_puts(s, ",noforcegid");
Jeff Layton8616e0f2009-06-11 10:27:28 -0400586
Jeff Layton61f98ff2009-06-11 10:27:32 -0400587 cifs_show_address(s, tcon->ses->server);
Jeff Layton8616e0f2009-06-11 10:27:28 -0400588
589 if (!tcon->unix_ext)
Al Viro5206efd2011-07-26 03:22:14 -0400590 seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
Ronnie Sahlberg8401e932020-12-12 13:40:50 -0600591 cifs_sb->ctx->file_mode,
592 cifs_sb->ctx->dir_mode);
Ronnie Sahlberg7c7ee622020-12-14 16:40:22 +1000593 if (cifs_sb->ctx->iocharset)
594 seq_printf(s, ",iocharset=%s", cifs_sb->ctx->iocharset);
Jeff Layton8616e0f2009-06-11 10:27:28 -0400595 if (tcon->seal)
Fabian Frederick571d5972014-05-13 18:04:17 +0200596 seq_puts(s, ",seal");
Steve Frenchec570102020-02-19 23:59:32 -0600597 else if (tcon->ses->server->ignore_signature)
598 seq_puts(s, ",signloosely");
Jeff Layton8616e0f2009-06-11 10:27:28 -0400599 if (tcon->nocase)
Fabian Frederick571d5972014-05-13 18:04:17 +0200600 seq_puts(s, ",nocase");
Steve French82e93672020-05-19 03:06:57 -0500601 if (tcon->nodelete)
602 seq_puts(s, ",nodelete");
Steve French52832252022-05-23 23:17:12 -0500603 if (cifs_sb->ctx->no_sparse)
604 seq_puts(s, ",nosparse");
Kenneth D'souzac8b6ac12019-01-21 11:51:59 +1000605 if (tcon->local_lease)
606 seq_puts(s, ",locallease");
Jeff Layton8616e0f2009-06-11 10:27:28 -0400607 if (tcon->retry)
Fabian Frederick571d5972014-05-13 18:04:17 +0200608 seq_puts(s, ",hard");
Ronnie Sahlberg6e82e922017-09-20 13:09:23 +1000609 else
610 seq_puts(s, ",soft");
Steve Frenchf16dfa72015-09-30 21:07:59 -0500611 if (tcon->use_persistent)
612 seq_puts(s, ",persistenthandles");
Steve French592fafe2015-11-03 10:08:53 -0600613 else if (tcon->use_resilient)
614 seq_puts(s, ",resilienthandles");
Steve Frenchb3266142018-05-20 23:41:10 -0500615 if (tcon->posix_extensions)
616 seq_puts(s, ",posix");
617 else if (tcon->unix_ext)
618 seq_puts(s, ",unix");
619 else
620 seq_puts(s, ",nounix");
Aurelien Aptel83930722018-09-20 18:10:25 -0700621 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
622 seq_puts(s, ",nodfs");
Jeff Layton8616e0f2009-06-11 10:27:28 -0400623 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
Fabian Frederick571d5972014-05-13 18:04:17 +0200624 seq_puts(s, ",posixpaths");
Jeff Layton8616e0f2009-06-11 10:27:28 -0400625 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
Fabian Frederick571d5972014-05-13 18:04:17 +0200626 seq_puts(s, ",setuids");
Steve French95932652016-09-23 01:36:34 -0500627 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
628 seq_puts(s, ",idsfromsid");
Jeff Layton8616e0f2009-06-11 10:27:28 -0400629 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
Fabian Frederick571d5972014-05-13 18:04:17 +0200630 seq_puts(s, ",serverino");
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +0000631 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
Fabian Frederick571d5972014-05-13 18:04:17 +0200632 seq_puts(s, ",rwpidforward");
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +0000633 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
Fabian Frederick571d5972014-05-13 18:04:17 +0200634 seq_puts(s, ",forcemand");
Jeff Layton8616e0f2009-06-11 10:27:28 -0400635 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
Fabian Frederick571d5972014-05-13 18:04:17 +0200636 seq_puts(s, ",nouser_xattr");
Jeff Layton8616e0f2009-06-11 10:27:28 -0400637 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
Fabian Frederick571d5972014-05-13 18:04:17 +0200638 seq_puts(s, ",mapchars");
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +0900639 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
640 seq_puts(s, ",mapposix");
Jeff Layton8616e0f2009-06-11 10:27:28 -0400641 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
Fabian Frederick571d5972014-05-13 18:04:17 +0200642 seq_puts(s, ",sfu");
Jeff Layton8616e0f2009-06-11 10:27:28 -0400643 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
Fabian Frederick571d5972014-05-13 18:04:17 +0200644 seq_puts(s, ",nobrl");
Steve French3d4ef9a12018-04-25 22:19:09 -0500645 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_HANDLE_CACHE)
646 seq_puts(s, ",nohandlecache");
Steve French412094a2019-06-24 02:01:42 -0500647 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)
648 seq_puts(s, ",modefromsid");
Jeff Layton8616e0f2009-06-11 10:27:28 -0400649 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
Fabian Frederick571d5972014-05-13 18:04:17 +0200650 seq_puts(s, ",cifsacl");
Jeff Layton8616e0f2009-06-11 10:27:28 -0400651 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
Fabian Frederick571d5972014-05-13 18:04:17 +0200652 seq_puts(s, ",dynperm");
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800653 if (root->d_sb->s_flags & SB_POSIXACL)
Fabian Frederick571d5972014-05-13 18:04:17 +0200654 seq_puts(s, ",acl");
Stefan Metzmacher736a33202010-07-30 14:56:00 +0200655 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
Fabian Frederick571d5972014-05-13 18:04:17 +0200656 seq_puts(s, ",mfsymlinks");
Suresh Jayaraman476428f2010-11-24 17:49:07 +0530657 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
Fabian Frederick571d5972014-05-13 18:04:17 +0200658 seq_puts(s, ",fsc");
Steve French71c424b2011-10-19 20:44:48 -0500659 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
Fabian Frederick571d5972014-05-13 18:04:17 +0200660 seq_puts(s, ",nostrictsync");
Steve French71c424b2011-10-19 20:44:48 -0500661 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
Fabian Frederick571d5972014-05-13 18:04:17 +0200662 seq_puts(s, ",noperm");
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +0100663 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID)
Eric W. Biederman1f682332013-02-06 01:20:20 -0800664 seq_printf(s, ",backupuid=%u",
665 from_kuid_munged(&init_user_ns,
Ronnie Sahlberg8401e932020-12-12 13:40:50 -0600666 cifs_sb->ctx->backupuid));
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +0100667 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID)
Eric W. Biederman1f682332013-02-06 01:20:20 -0800668 seq_printf(s, ",backupgid=%u",
669 from_kgid_munged(&init_user_ns,
Ronnie Sahlberg8401e932020-12-12 13:40:50 -0600670 cifs_sb->ctx->backupgid));
Steve French2b280fa2008-05-17 03:12:45 +0000671
Steve French0c2b5f72020-12-15 13:28:50 -0600672 seq_printf(s, ",rsize=%u", cifs_sb->ctx->rsize);
673 seq_printf(s, ",wsize=%u", cifs_sb->ctx->wsize);
674 seq_printf(s, ",bsize=%u", cifs_sb->ctx->bsize);
Steve Frenchb8d64f82021-04-24 21:46:23 -0500675 if (cifs_sb->ctx->rasize)
676 seq_printf(s, ",rasize=%u", cifs_sb->ctx->rasize);
Steve French563317e2019-09-08 23:22:02 -0500677 if (tcon->ses->server->min_offload)
678 seq_printf(s, ",esize=%u", tcon->ses->server->min_offload);
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600679 seq_printf(s, ",echo_interval=%lu",
680 tcon->ses->server->echo_interval / HZ);
Steve Frenchdc179262019-06-17 17:34:57 -0500681
Steve French2bfd8102022-12-11 13:54:21 -0600682 /* Only display the following if overridden on mount */
Steve Frenchdc179262019-06-17 17:34:57 -0500683 if (tcon->ses->server->max_credits != SMB2_MAX_CREDITS_AVAILABLE)
684 seq_printf(s, ",max_credits=%u", tcon->ses->server->max_credits);
Steve French2bfd8102022-12-11 13:54:21 -0600685 if (tcon->ses->server->tcp_nodelay)
686 seq_puts(s, ",tcpnodelay");
687 if (tcon->ses->server->noautotune)
688 seq_puts(s, ",noautotune");
689 if (tcon->ses->server->noblocksnd)
690 seq_puts(s, ",noblocksend");
Steve Frenchdc179262019-06-17 17:34:57 -0500691
Steve French8a69e962018-06-29 16:06:15 -0500692 if (tcon->snapshot_time)
693 seq_printf(s, ",snapshot=%llu", tcon->snapshot_time);
Steve Frenchca567eb2019-03-29 16:31:07 -0500694 if (tcon->handle_timeout)
695 seq_printf(s, ",handletimeout=%u", tcon->handle_timeout);
Steve French57804642021-02-24 12:12:53 -0600696
697 /*
698 * Display file and directory attribute timeout in seconds.
699 * If file and directory attribute timeout the same then actimeo
700 * was likely specified on mount
701 */
702 if (cifs_sb->ctx->acdirmax == cifs_sb->ctx->acregmax)
703 seq_printf(s, ",actimeo=%lu", cifs_sb->ctx->acregmax / HZ);
704 else {
705 seq_printf(s, ",acdirmax=%lu", cifs_sb->ctx->acdirmax / HZ);
706 seq_printf(s, ",acregmax=%lu", cifs_sb->ctx->acregmax / HZ);
707 }
Steve French5efdd912022-08-11 00:53:00 -0500708 seq_printf(s, ",closetimeo=%lu", cifs_sb->ctx->closetimeo / HZ);
Jeff Layton8616e0f2009-06-11 10:27:28 -0400709
Aurelien Aptelbcc88802019-09-20 04:32:20 +0200710 if (tcon->ses->chan_max > 1)
Steve French7866c172020-06-09 19:50:40 -0500711 seq_printf(s, ",multichannel,max_channels=%zu",
Aurelien Aptelbcc88802019-09-20 04:32:20 +0200712 tcon->ses->chan_max);
713
Samuel Cabrero0ac4e292020-12-11 22:59:29 -0600714 if (tcon->use_witness)
715 seq_puts(s, ",witness");
Samuel Cabrero0ac4e292020-12-11 22:59:29 -0600716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return 0;
718}
719
Al Viro42faad92008-04-24 07:21:56 -0400720static void cifs_umount_begin(struct super_block *sb)
Steve French68058e72005-10-10 10:34:22 -0700721{
Al Viro42faad92008-04-24 07:21:56 -0400722 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Steve French96daf2b2011-05-27 04:34:02 +0000723 struct cifs_tcon *tcon;
Steve French68058e72005-10-10 10:34:22 -0700724
Steve French4523cc32007-04-30 20:13:06 +0000725 if (cifs_sb == NULL)
Steve French9e2e85f2005-10-10 14:28:38 -0700726 return;
727
Jeff Layton0d424ad2010-09-20 16:01:35 -0700728 tcon = cifs_sb_master_tcon(cifs_sb);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500729
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530730 spin_lock(&cifs_tcp_ses_lock);
Shyam Prasad Nd7d7a662022-07-27 14:49:56 -0500731 spin_lock(&tcon->tc_lock);
Steve Frenchfdf59eb2022-03-27 16:07:30 -0500732 if ((tcon->tc_count > 1) || (tcon->status == TID_EXITING)) {
Steve Frenchad8034f2009-06-26 03:25:49 +0000733 /* we have other mounts to same share or we have
Steve French491eafc2023-03-23 16:20:02 -0500734 already tried to umount this and woken up
Steve Frenchad8034f2009-06-26 03:25:49 +0000735 all waiting network requests, nothing to do */
Shyam Prasad Nd7d7a662022-07-27 14:49:56 -0500736 spin_unlock(&tcon->tc_lock);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530737 spin_unlock(&cifs_tcp_ses_lock);
Steve Frenchad8034f2009-06-26 03:25:49 +0000738 return;
Steve French491eafc2023-03-23 16:20:02 -0500739 }
740 /*
741 * can not set tcon->status to TID_EXITING yet since we don't know if umount -f will
742 * fail later (e.g. due to open files). TID_EXITING will be set just before tdis req sent
743 */
Shyam Prasad Nd7d7a662022-07-27 14:49:56 -0500744 spin_unlock(&tcon->tc_lock);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530745 spin_unlock(&cifs_tcp_ses_lock);
Steve French5e1253b2005-10-10 14:06:37 -0700746
Steve French3a5ff612006-07-14 22:37:11 +0000747 /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
Steve French7b7abfe2005-11-09 15:21:09 -0800748 /* cancel_notify_requests(tcon); */
Steve French50c2f752007-07-13 00:33:32 +0000749 if (tcon->ses && tcon->ses->server) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500750 cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
Steve French9e2e85f2005-10-10 14:28:38 -0700751 wake_up_all(&tcon->ses->server->request_q);
Steve French6ab16d22005-11-29 20:55:11 -0800752 wake_up_all(&tcon->ses->server->response_q);
753 msleep(1); /* yield */
754 /* we have to kick the requests once more */
755 wake_up_all(&tcon->ses->server->response_q);
756 msleep(1);
Steve French5e1253b2005-10-10 14:06:37 -0700757 }
Steve French68058e72005-10-10 10:34:22 -0700758
759 return;
760}
Steve French68058e72005-10-10 10:34:22 -0700761
Steve Frenchbf97d282006-09-28 21:34:06 +0000762#ifdef CONFIG_CIFS_STATS2
Al Viro64132372011-12-08 20:51:13 -0500763static int cifs_show_stats(struct seq_file *s, struct dentry *root)
Steve Frenchbf97d282006-09-28 21:34:06 +0000764{
765 /* BB FIXME */
766 return 0;
767}
768#endif
769
David Howells70431bf2020-11-17 15:56:59 +0000770static int cifs_write_inode(struct inode *inode, struct writeback_control *wbc)
771{
772 fscache_unpin_writeback(wbc, cifs_inode_cookie(inode));
773 return 0;
774}
775
Al Viro45321ac2010-06-07 13:43:19 -0400776static int cifs_drop_inode(struct inode *inode)
Jeff Layton12420ac2010-06-01 14:47:40 -0400777{
778 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
779
Al Viro45321ac2010-06-07 13:43:19 -0400780 /* no serverino => unconditional eviction */
781 return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
782 generic_drop_inode(inode);
Jeff Layton12420ac2010-06-01 14:47:40 -0400783}
784
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800785static const struct super_operations cifs_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 .statfs = cifs_statfs,
787 .alloc_inode = cifs_alloc_inode,
David Howells70431bf2020-11-17 15:56:59 +0000788 .write_inode = cifs_write_inode,
Al Viroc2e68022019-04-14 23:18:35 -0400789 .free_inode = cifs_free_inode,
Jeff Layton12420ac2010-06-01 14:47:40 -0400790 .drop_inode = cifs_drop_inode,
Al Virob57922d2010-06-07 14:34:48 -0400791 .evict_inode = cifs_evict_inode,
Steve French653a5ef2020-12-14 20:08:10 -0600792/* .show_path = cifs_show_path, */ /* Would we ever need show path? */
793 .show_devname = cifs_show_devname,
Jeff Layton12420ac2010-06-01 14:47:40 -0400794/* .delete_inode = cifs_delete_inode, */ /* Do not need above
795 function unless later we add lazy close of inodes or unless the
Steve French50c2f752007-07-13 00:33:32 +0000796 kernel forgets to call us with the same number of releases (closes)
797 as opens */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 .show_options = cifs_show_options,
Steve French7b7abfe2005-11-09 15:21:09 -0800799 .umount_begin = cifs_umount_begin,
Steve Frenchbf97d282006-09-28 21:34:06 +0000800#ifdef CONFIG_CIFS_STATS2
Steve Frenchf46d3e12006-09-30 01:08:55 +0000801 .show_stats = cifs_show_stats,
Steve Frenchbf97d282006-09-28 21:34:06 +0000802#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803};
804
Steve Frenchf87d39d2011-05-27 03:50:55 +0000805/*
806 * Get root dentry from superblock according to prefix path mount option.
807 * Return dentry with refcount + 1 on success and NULL otherwise.
808 */
809static struct dentry *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600810cifs_get_root(struct smb3_fs_context *ctx, struct super_block *sb)
Steve Frenchf87d39d2011-05-27 03:50:55 +0000811{
Al Virofec11dd2011-07-18 13:50:40 -0400812 struct dentry *dentry;
Steve Frenchf87d39d2011-05-27 03:50:55 +0000813 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Al Virofec11dd2011-07-18 13:50:40 -0400814 char *full_path = NULL;
815 char *s, *p;
Steve Frenchf87d39d2011-05-27 03:50:55 +0000816 char sep;
817
Sachin Prabhu348c1bf2016-07-29 22:38:21 +0100818 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
819 return dget(sb->s_root);
820
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600821 full_path = cifs_build_path_to_root(ctx, cifs_sb,
Sachin Prabhu374402a2016-12-15 12:31:19 +0530822 cifs_sb_master_tcon(cifs_sb), 0);
Steve Frenchf87d39d2011-05-27 03:50:55 +0000823 if (full_path == NULL)
Al Viro9403c9c2011-06-17 10:02:59 -0400824 return ERR_PTR(-ENOMEM);
Steve Frenchf87d39d2011-05-27 03:50:55 +0000825
Joe Perchesf96637b2013-05-04 22:12:25 -0500826 cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
Steve Frenchf87d39d2011-05-27 03:50:55 +0000827
Steve Frenchf87d39d2011-05-27 03:50:55 +0000828 sep = CIFS_DIR_SEP(cifs_sb);
Al Virofec11dd2011-07-18 13:50:40 -0400829 dentry = dget(sb->s_root);
Colin Ian King0b66fa72021-12-21 00:48:09 +0000830 s = full_path;
Steve Frenchf87d39d2011-05-27 03:50:55 +0000831
Al Virofec11dd2011-07-18 13:50:40 -0400832 do {
David Howells2b0143b2015-03-17 22:25:59 +0000833 struct inode *dir = d_inode(dentry);
Al Virofec11dd2011-07-18 13:50:40 -0400834 struct dentry *child;
Steve Frenchf87d39d2011-05-27 03:50:55 +0000835
Jeff Laytonce2ac522013-02-01 15:11:01 -0500836 if (!S_ISDIR(dir->i_mode)) {
837 dput(dentry);
838 dentry = ERR_PTR(-ENOTDIR);
839 break;
840 }
Pavel Shilovsky5b980b02011-08-21 19:30:15 +0400841
Al Virofec11dd2011-07-18 13:50:40 -0400842 /* skip separators */
843 while (*s == sep)
844 s++;
845 if (!*s)
846 break;
847 p = s++;
848 /* next separator */
849 while (*s && *s != sep)
850 s++;
Steve Frenchf87d39d2011-05-27 03:50:55 +0000851
Al Viro6c2d47982019-10-31 01:21:58 -0400852 child = lookup_positive_unlocked(p, dentry, s - p);
Al Virofec11dd2011-07-18 13:50:40 -0400853 dput(dentry);
854 dentry = child;
855 } while (!IS_ERR(dentry));
Steve Frenchf87d39d2011-05-27 03:50:55 +0000856 kfree(full_path);
Al Virofec11dd2011-07-18 13:50:40 -0400857 return dentry;
Steve Frenchf87d39d2011-05-27 03:50:55 +0000858}
859
Al Viroee01a142011-06-17 09:47:23 -0400860static int cifs_set_super(struct super_block *sb, void *data)
861{
862 struct cifs_mnt_data *mnt_data = data;
863 sb->s_fs_info = mnt_data->cifs_sb;
864 return set_anon_super(sb, NULL);
865}
866
Ronnie Sahlberg24e0a1e2020-12-10 00:06:02 -0600867struct dentry *
Steve Frenchc7c137b2018-06-06 17:59:29 -0500868cifs_smb3_do_mount(struct file_system_type *fs_type,
Ronnie Sahlberg24e0a1e2020-12-10 00:06:02 -0600869 int flags, struct smb3_fs_context *old_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 int rc;
Ronnie Sahlberg8378a512022-05-31 13:01:17 +1000872 struct super_block *sb = NULL;
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +1000873 struct cifs_sb_info *cifs_sb = NULL;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +0400874 struct cifs_mnt_data mnt_data;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +0000875 struct dentry *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Steve French8c1beb92018-10-07 13:52:18 -0500877 /*
878 * Prints in Kernel / CIFS log the attempted mount operation
879 * If CIFS_DEBUG && cifs_FYI
880 */
Rodrigo Freiref80eaed2018-10-07 12:21:26 -0300881 if (cifsFYI)
Ronnie Sahlberg24e0a1e2020-12-10 00:06:02 -0600882 cifs_dbg(FYI, "Devname: %s flags: %d\n", old_ctx->UNC, flags);
Rodrigo Freiref80eaed2018-10-07 12:21:26 -0300883 else
Ronnie Sahlberg24e0a1e2020-12-10 00:06:02 -0600884 cifs_info("Attempting to mount %s\n", old_ctx->UNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Pavel Shilovsky724d9f12011-05-05 09:55:12 +0000886 cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
887 if (cifs_sb == NULL) {
888 root = ERR_PTR(-ENOMEM);
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +1000889 goto out;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +0000890 }
891
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +1000892 cifs_sb->ctx = kzalloc(sizeof(struct smb3_fs_context), GFP_KERNEL);
893 if (!cifs_sb->ctx) {
Al Viro5d3bc602011-06-17 09:17:28 -0400894 root = ERR_PTR(-ENOMEM);
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +1000895 goto out;
Al Viro5d3bc602011-06-17 09:17:28 -0400896 }
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +1000897 rc = smb3_fs_context_dup(cifs_sb->ctx, old_ctx);
Sachin Prabhu4214ebf2016-07-29 22:38:19 +0100898 if (rc) {
899 root = ERR_PTR(rc);
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +1000900 goto out;
Aurelien Aptela6b50582016-05-25 19:59:09 +0200901 }
902
Ronnie Sahlberg51acd202020-12-14 16:40:24 +1000903 rc = cifs_setup_cifs_sb(cifs_sb);
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +1000904 if (rc) {
905 root = ERR_PTR(rc);
906 goto out;
907 }
908
909 rc = cifs_mount(cifs_sb, cifs_sb->ctx);
Al Viro97d11522011-06-17 09:29:57 -0400910 if (rc) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800911 if (!(flags & SB_SILENT))
Joe Perchesf96637b2013-05-04 22:12:25 -0500912 cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
913 rc);
Al Viro97d11522011-06-17 09:29:57 -0400914 root = ERR_PTR(rc);
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +1000915 goto out;
Al Viro97d11522011-06-17 09:29:57 -0400916 }
917
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +1000918 mnt_data.ctx = cifs_sb->ctx;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +0400919 mnt_data.cifs_sb = cifs_sb;
920 mnt_data.flags = flags;
921
David Howells9249e172012-06-25 12:55:37 +0100922 /* BB should we make this contingent on mount parm? */
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800923 flags |= SB_NODIRATIME | SB_NOATIME;
David Howells9249e172012-06-25 12:55:37 +0100924
925 sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +0000926 if (IS_ERR(sb)) {
Pavel Shilovsky724d9f12011-05-05 09:55:12 +0000927 root = ERR_CAST(sb);
Al Viro97d11522011-06-17 09:29:57 -0400928 cifs_umount(cifs_sb);
Ronnie Sahlberg6cf5abb2020-12-16 08:51:33 +1000929 cifs_sb = NULL;
Al Virod757d712011-06-17 09:42:43 -0400930 goto out;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +0000931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Al Viroee01a142011-06-17 09:47:23 -0400933 if (sb->s_root) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500934 cifs_dbg(FYI, "Use existing superblock\n");
Al Viro97d11522011-06-17 09:29:57 -0400935 cifs_umount(cifs_sb);
Ronnie Sahlberg6cf5abb2020-12-16 08:51:33 +1000936 cifs_sb = NULL;
Al Viro5c4f1ad2011-06-17 09:56:55 -0400937 } else {
Al Viro5c4f1ad2011-06-17 09:56:55 -0400938 rc = cifs_read_super(sb);
939 if (rc) {
940 root = ERR_PTR(rc);
941 goto out_super;
942 }
943
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800944 sb->s_flags |= SB_ACTIVE;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +0400945 }
946
Ronnie Sahlberg6cf5abb2020-12-16 08:51:33 +1000947 root = cifs_get_root(cifs_sb ? cifs_sb->ctx : old_ctx, sb);
Al Viro9403c9c2011-06-17 10:02:59 -0400948 if (IS_ERR(root))
Al Virofa18f1b2011-06-17 09:50:44 -0400949 goto out_super;
Al Virofa18f1b2011-06-17 09:50:44 -0400950
Ronnie Sahlberg269f67e2021-03-09 09:07:30 +1000951 if (cifs_sb)
952 cifs_sb->root = dget(root);
953
Joe Perchesf96637b2013-05-04 22:12:25 -0500954 cifs_dbg(FYI, "dentry root is: %p\n", root);
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +1000955 return root;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +0400956
Pavel Shilovsky641a58d2011-05-26 00:02:16 +0400957out_super:
Pavel Shilovsky641a58d2011-05-26 00:02:16 +0400958 deactivate_locked_super(sb);
Ronnie Sahlberg3d6cc982022-02-11 02:59:15 +1000959 return root;
Pavel Shilovsky641a58d2011-05-26 00:02:16 +0400960out:
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +1000961 if (cifs_sb) {
Ronnie Sahlberg8378a512022-05-31 13:01:17 +1000962 if (!sb || IS_ERR(sb)) { /* otherwise kill_sb will handle */
963 kfree(cifs_sb->prepath);
964 smb3_cleanup_fs_context(cifs_sb->ctx);
965 kfree(cifs_sb);
966 }
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +1000967 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +0000968 return root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Steve Frenchc7c137b2018-06-06 17:59:29 -0500971
Jeff Layton08bc0352014-05-23 06:50:21 -0400972static ssize_t
973cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
974{
975 ssize_t rc;
976 struct inode *inode = file_inode(iocb->ki_filp);
977
David Howells994fd532022-04-07 00:03:14 +0100978 if (iocb->ki_flags & IOCB_DIRECT)
Ross Lagerwall882137c2015-12-02 14:46:07 +0000979 return cifs_user_readv(iocb, iter);
980
Jeff Layton08bc0352014-05-23 06:50:21 -0400981 rc = cifs_revalidate_mapping(inode);
982 if (rc)
983 return rc;
984
985 return generic_file_read_iter(iocb, iter);
986}
987
Al Viro3dae8752014-04-03 12:05:17 -0400988static ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Steve French87c89dd2005-11-17 17:03:00 -0800989{
Al Viro496ad9a2013-01-23 17:07:38 -0500990 struct inode *inode = file_inode(iocb->ki_filp);
Sachin Prabhuc11f1df2014-03-11 16:11:47 +0000991 struct cifsInodeInfo *cinode = CIFS_I(inode);
Steve French87c89dd2005-11-17 17:03:00 -0800992 ssize_t written;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -0500993 int rc;
Steve French87c89dd2005-11-17 17:03:00 -0800994
Ross Lagerwall882137c2015-12-02 14:46:07 +0000995 if (iocb->ki_filp->f_flags & O_DIRECT) {
996 written = cifs_user_writev(iocb, from);
997 if (written > 0 && CIFS_CACHE_READ(cinode)) {
998 cifs_zap_mapping(inode);
999 cifs_dbg(FYI,
1000 "Set no oplock for inode=%p after a write operation\n",
1001 inode);
1002 cinode->oplock = 0;
1003 }
1004 return written;
1005 }
1006
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001007 written = cifs_get_writer(cinode);
1008 if (written)
1009 return written;
1010
Al Viro3dae8752014-04-03 12:05:17 -04001011 written = generic_file_write_iter(iocb, from);
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05001012
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001013 if (CIFS_CACHE_WRITE(CIFS_I(inode)))
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001014 goto out;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05001015
1016 rc = filemap_fdatawrite(inode->i_mapping);
1017 if (rc)
Al Viro3dae8752014-04-03 12:05:17 -04001018 cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05001019 rc, inode);
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05001020
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001021out:
1022 cifs_put_writer(cinode);
Steve French87c89dd2005-11-17 17:03:00 -08001023 return written;
1024}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Andrew Morton965c8e52012-12-17 15:59:39 -08001026static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
Steve Frenchc32a0b62006-01-12 14:41:28 -08001027{
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10001028 struct cifsFileInfo *cfile = file->private_data;
1029 struct cifs_tcon *tcon;
1030
Josef Bacik06222e42011-07-18 13:21:38 -04001031 /*
Andrew Morton965c8e52012-12-17 15:59:39 -08001032 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
Josef Bacik06222e42011-07-18 13:21:38 -04001033 * the cached file length
1034 */
Andrew Morton965c8e52012-12-17 15:59:39 -08001035 if (whence != SEEK_SET && whence != SEEK_CUR) {
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001036 int rc;
Al Viro496ad9a2013-01-23 17:07:38 -05001037 struct inode *inode = file_inode(file);
Steve French030e9d82007-02-01 04:27:59 +00001038
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001039 /*
1040 * We need to be sure that all dirty pages are written and the
1041 * server has the newest file length.
1042 */
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001043 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001044 inode->i_mapping->nrpages != 0) {
1045 rc = filemap_fdatawait(inode->i_mapping);
Steve French156ecb22011-05-20 17:00:01 +00001046 if (rc) {
1047 mapping_set_error(inode->i_mapping, rc);
1048 return rc;
1049 }
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001050 }
1051 /*
1052 * Some applications poll for the file length in this strange
1053 * way so we must seek to end on non-oplocked files by
1054 * setting the revalidate time to zero.
1055 */
1056 CIFS_I(inode)->time = 0;
Steve French030e9d82007-02-01 04:27:59 +00001057
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001058 rc = cifs_revalidate_file_attr(file);
1059 if (rc < 0)
1060 return (loff_t)rc;
Steve Frenchc32a0b62006-01-12 14:41:28 -08001061 }
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10001062 if (cfile && cfile->tlink) {
1063 tcon = tlink_tcon(cfile->tlink);
1064 if (tcon->ses->server->ops->llseek)
1065 return tcon->ses->server->ops->llseek(file, tcon,
1066 offset, whence);
1067 }
Andrew Morton965c8e52012-12-17 15:59:39 -08001068 return generic_file_llseek(file, offset, whence);
Steve Frenchc32a0b62006-01-12 14:41:28 -08001069}
1070
Jeff Laytone6f5c782014-08-22 10:40:25 -04001071static int
1072cifs_setlease(struct file *file, long arg, struct file_lock **lease, void **priv)
Steve French84210e92008-10-23 04:42:37 +00001073{
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001074 /*
1075 * Note that this is called by vfs setlease with i_lock held to
1076 * protect *lease from going away.
1077 */
Al Viro496ad9a2013-01-23 17:07:38 -05001078 struct inode *inode = file_inode(file);
Jeff Laytonba00ba642010-09-20 16:01:31 -07001079 struct cifsFileInfo *cfile = file->private_data;
Steve French84210e92008-10-23 04:42:37 +00001080
1081 if (!(S_ISREG(inode->i_mode)))
1082 return -EINVAL;
1083
Jeff Layton02440802014-08-09 10:16:44 -04001084 /* Check if file is oplocked if this is request for new lease */
1085 if (arg == F_UNLCK ||
1086 ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) ||
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001087 ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
Jeff Laytone6f5c782014-08-22 10:40:25 -04001088 return generic_setlease(file, arg, lease, priv);
Jeff Layton13cfb732010-09-29 19:51:11 -04001089 else if (tlink_tcon(cfile->tlink)->local_lease &&
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001090 !CIFS_CACHE_READ(CIFS_I(inode)))
1091 /*
1092 * If the server claims to support oplock on this file, then we
1093 * still need to check oplock even if the local_lease mount
1094 * option is set, but there are servers which do not support
1095 * oplock for which this mount option may be useful if the user
1096 * knows that the file won't be changed on the server by anyone
1097 * else.
1098 */
Jeff Laytone6f5c782014-08-22 10:40:25 -04001099 return generic_setlease(file, arg, lease, priv);
Christoph Hellwig51ee4b82010-10-31 08:35:10 -04001100 else
Steve French84210e92008-10-23 04:42:37 +00001101 return -EAGAIN;
1102}
Steve French84210e92008-10-23 04:42:37 +00001103
Igor Mammedove6ab1582008-01-11 01:49:48 +00001104struct file_system_type cifs_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 .owner = THIS_MODULE,
1106 .name = "cifs",
Ronnie Sahlberg24e0a1e2020-12-10 00:06:02 -06001107 .init_fs_context = smb3_init_fs_context,
1108 .parameters = smb3_fs_parameters,
Al Viro6d686172011-06-17 08:34:57 -04001109 .kill_sb = cifs_kill_sb,
Steve Frenchc7e9f782020-02-25 18:08:54 -06001110 .fs_flags = FS_RENAME_DOES_D_MOVE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111};
Eric W. Biederman3e64fe52013-03-11 07:05:42 -07001112MODULE_ALIAS_FS("cifs");
Steve French49218b42018-05-23 21:44:53 -05001113
Paulo Alcantarac36ee7d2022-06-05 19:54:26 -03001114struct file_system_type smb3_fs_type = {
Steve French49218b42018-05-23 21:44:53 -05001115 .owner = THIS_MODULE,
1116 .name = "smb3",
Ronnie Sahlberg24e0a1e2020-12-10 00:06:02 -06001117 .init_fs_context = smb3_init_fs_context,
1118 .parameters = smb3_fs_parameters,
Steve French49218b42018-05-23 21:44:53 -05001119 .kill_sb = cifs_kill_sb,
Steve Frenchc7e9f782020-02-25 18:08:54 -06001120 .fs_flags = FS_RENAME_DOES_D_MOVE,
Steve French49218b42018-05-23 21:44:53 -05001121};
1122MODULE_ALIAS_FS("smb3");
1123MODULE_ALIAS("smb3");
1124
Arjan van de Ven754661f2007-02-12 00:55:38 -08001125const struct inode_operations cifs_dir_inode_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 .create = cifs_create,
Miklos Szeredid2c12712012-06-05 15:10:23 +02001127 .atomic_open = cifs_atomic_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 .lookup = cifs_lookup,
1129 .getattr = cifs_getattr,
1130 .unlink = cifs_unlink,
1131 .link = cifs_hardlink,
1132 .mkdir = cifs_mkdir,
1133 .rmdir = cifs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02001134 .rename = cifs_rename2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 .permission = cifs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 .setattr = cifs_setattr,
1137 .symlink = cifs_symlink,
1138 .mknod = cifs_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 .listxattr = cifs_listxattr,
Christian Braunerbd9684b2022-09-22 17:17:02 +02001140 .get_acl = cifs_get_acl,
Christian Braunerdc1af4c2022-09-22 17:17:03 +02001141 .set_acl = cifs_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142};
1143
Arjan van de Ven754661f2007-02-12 00:55:38 -08001144const struct inode_operations cifs_file_inode_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 .setattr = cifs_setattr,
Steve French48a77aa2016-05-18 20:48:32 -05001146 .getattr = cifs_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 .permission = cifs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 .listxattr = cifs_listxattr,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10001149 .fiemap = cifs_fiemap,
Christian Braunerbd9684b2022-09-22 17:17:02 +02001150 .get_acl = cifs_get_acl,
Christian Braunerdc1af4c2022-09-22 17:17:03 +02001151 .set_acl = cifs_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152};
1153
ChenXiaoSong542228d2022-11-04 15:44:41 +08001154const char *cifs_get_link(struct dentry *dentry, struct inode *inode,
1155 struct delayed_call *done)
1156{
1157 char *target_path;
1158
1159 target_path = kmalloc(PATH_MAX, GFP_KERNEL);
1160 if (!target_path)
1161 return ERR_PTR(-ENOMEM);
1162
1163 spin_lock(&inode->i_lock);
1164 if (likely(CIFS_I(inode)->symlink_target)) {
1165 strscpy(target_path, CIFS_I(inode)->symlink_target, PATH_MAX);
1166 } else {
1167 kfree(target_path);
1168 target_path = ERR_PTR(-EOPNOTSUPP);
1169 }
1170 spin_unlock(&inode->i_lock);
1171
1172 if (!IS_ERR(target_path))
1173 set_delayed_call(done, kfree_link, target_path);
1174
1175 return target_path;
1176}
1177
Arjan van de Ven754661f2007-02-12 00:55:38 -08001178const struct inode_operations cifs_symlink_inode_ops = {
ChenXiaoSong542228d2022-11-04 15:44:41 +08001179 .get_link = cifs_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 .permission = cifs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 .listxattr = cifs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182};
1183
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11001184static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,
1185 struct file *dst_file, loff_t destoff, loff_t len,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11001186 unsigned int remap_flags)
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001187{
1188 struct inode *src_inode = file_inode(src_file);
1189 struct inode *target_inode = file_inode(dst_file);
1190 struct cifsFileInfo *smb_file_src = src_file->private_data;
Colin Ian King8c6c9bed82018-11-01 13:14:30 +00001191 struct cifsFileInfo *smb_file_target;
1192 struct cifs_tcon *target_tcon;
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001193 unsigned int xid;
1194 int rc;
1195
Xiaoli Fengb073a082019-03-16 12:11:54 +08001196 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11001197 return -EINVAL;
1198
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001199 cifs_dbg(FYI, "clone range\n");
1200
1201 xid = get_xid();
1202
1203 if (!src_file->private_data || !dst_file->private_data) {
1204 rc = -EBADF;
1205 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1206 goto out;
1207 }
1208
Colin Ian King8c6c9bed82018-11-01 13:14:30 +00001209 smb_file_target = dst_file->private_data;
1210 target_tcon = tlink_tcon(smb_file_target->tlink);
1211
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001212 /*
1213 * Note: cifs case is easier than btrfs since server responsible for
1214 * checks for proper open modes and file type and if it wants
1215 * server could even support copy of range where source = target
1216 */
1217 lock_two_nondirectories(target_inode, src_inode);
1218
1219 if (len == 0)
1220 len = src_inode->i_size - off;
1221
1222 cifs_dbg(FYI, "about to flush pages\n");
1223 /* should we flush first and last page first */
1224 truncate_inode_pages_range(&target_inode->i_data, destoff,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001225 PAGE_ALIGN(destoff + len)-1);
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001226
1227 if (target_tcon->ses->server->ops->duplicate_extents)
1228 rc = target_tcon->ses->server->ops->duplicate_extents(xid,
1229 smb_file_src, smb_file_target, off, len, destoff);
1230 else
1231 rc = -EOPNOTSUPP;
1232
1233 /* force revalidate of size and timestamps of target file now
1234 that target is updated on the server */
1235 CIFS_I(target_inode)->time = 0;
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001236 /* although unlocking in the reverse order from locking is not
1237 strictly necessary here it is a little cleaner to be consistent */
1238 unlock_two_nondirectories(src_inode, target_inode);
1239out:
1240 free_xid(xid);
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11001241 return rc < 0 ? rc : len;
Christoph Hellwig04b38d62015-12-03 12:59:50 +01001242}
1243
Sachin Prabhu620d8742017-02-10 16:03:51 +05301244ssize_t cifs_file_copychunk_range(unsigned int xid,
1245 struct file *src_file, loff_t off,
1246 struct file *dst_file, loff_t destoff,
1247 size_t len, unsigned int flags)
1248{
1249 struct inode *src_inode = file_inode(src_file);
1250 struct inode *target_inode = file_inode(dst_file);
1251 struct cifsFileInfo *smb_file_src;
1252 struct cifsFileInfo *smb_file_target;
1253 struct cifs_tcon *src_tcon;
1254 struct cifs_tcon *target_tcon;
1255 ssize_t rc;
1256
1257 cifs_dbg(FYI, "copychunk range\n");
1258
Sachin Prabhu620d8742017-02-10 16:03:51 +05301259 if (!src_file->private_data || !dst_file->private_data) {
1260 rc = -EBADF;
1261 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1262 goto out;
1263 }
1264
1265 rc = -EXDEV;
1266 smb_file_target = dst_file->private_data;
1267 smb_file_src = src_file->private_data;
1268 src_tcon = tlink_tcon(smb_file_src->tlink);
1269 target_tcon = tlink_tcon(smb_file_target->tlink);
1270
1271 if (src_tcon->ses != target_tcon->ses) {
1272 cifs_dbg(VFS, "source and target of copy not on same server\n");
1273 goto out;
1274 }
1275
Amir Goldsteinbf3c90e2019-06-10 20:36:57 +03001276 rc = -EOPNOTSUPP;
1277 if (!target_tcon->ses->server->ops->copychunk_range)
1278 goto out;
1279
Sachin Prabhu620d8742017-02-10 16:03:51 +05301280 /*
1281 * Note: cifs case is easier than btrfs since server responsible for
1282 * checks for proper open modes and file type and if it wants
1283 * server could even support copy of range where source = target
1284 */
1285 lock_two_nondirectories(target_inode, src_inode);
1286
1287 cifs_dbg(FYI, "about to flush pages\n");
Steve French3e3761f2022-08-29 11:53:41 -05001288
1289 rc = filemap_write_and_wait_range(src_inode->i_mapping, off,
1290 off + len - 1);
1291 if (rc)
ChenXiaoSong50248782022-11-19 12:51:59 +08001292 goto unlock;
Steve French3e3761f2022-08-29 11:53:41 -05001293
Sachin Prabhu620d8742017-02-10 16:03:51 +05301294 /* should we flush first and last page first */
1295 truncate_inode_pages(&target_inode->i_data, 0);
1296
Amir Goldsteinbf3c90e2019-06-10 20:36:57 +03001297 rc = file_modified(dst_file);
1298 if (!rc)
Sachin Prabhu620d8742017-02-10 16:03:51 +05301299 rc = target_tcon->ses->server->ops->copychunk_range(xid,
1300 smb_file_src, smb_file_target, off, len, destoff);
Amir Goldsteinbf3c90e2019-06-10 20:36:57 +03001301
1302 file_accessed(src_file);
Sachin Prabhu620d8742017-02-10 16:03:51 +05301303
1304 /* force revalidate of size and timestamps of target file now
1305 * that target is updated on the server
1306 */
1307 CIFS_I(target_inode)->time = 0;
ChenXiaoSong50248782022-11-19 12:51:59 +08001308
1309unlock:
Sachin Prabhu620d8742017-02-10 16:03:51 +05301310 /* although unlocking in the reverse order from locking is not
1311 * strictly necessary here it is a little cleaner to be consistent
1312 */
1313 unlock_two_nondirectories(src_inode, target_inode);
1314
1315out:
1316 return rc;
1317}
1318
Steve French6e70c262018-05-10 10:59:37 -05001319/*
1320 * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync()
1321 * is a dummy operation.
1322 */
1323static int cifs_dir_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1324{
1325 cifs_dbg(FYI, "Sync directory - name: %pD datasync: 0x%x\n",
1326 file, datasync);
1327
1328 return 0;
1329}
1330
Sachin Prabhu620d8742017-02-10 16:03:51 +05301331static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off,
1332 struct file *dst_file, loff_t destoff,
1333 size_t len, unsigned int flags)
1334{
1335 unsigned int xid = get_xid();
1336 ssize_t rc;
Steve French4e8aea32020-04-09 21:42:18 -05001337 struct cifsFileInfo *cfile = dst_file->private_data;
1338
Zhang Xiaoxu9a97df42022-10-17 22:45:22 +08001339 if (cfile->swapfile) {
1340 rc = -EOPNOTSUPP;
1341 free_xid(xid);
1342 return rc;
1343 }
Sachin Prabhu620d8742017-02-10 16:03:51 +05301344
1345 rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
1346 len, flags);
1347 free_xid(xid);
Dave Chinner64bf5ff2019-06-05 08:04:47 -07001348
Amir Goldstein5dae2222019-06-05 08:04:50 -07001349 if (rc == -EOPNOTSUPP || rc == -EXDEV)
Dave Chinner64bf5ff2019-06-05 08:04:47 -07001350 rc = generic_copy_file_range(src_file, off, dst_file,
1351 destoff, len, flags);
Sachin Prabhu620d8742017-02-10 16:03:51 +05301352 return rc;
1353}
1354
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001355const struct file_operations cifs_file_ops = {
Jeff Layton08bc0352014-05-23 06:50:21 -04001356 .read_iter = cifs_loose_read_iter,
Al Viro3dae8752014-04-03 12:05:17 -04001357 .write_iter = cifs_file_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 .open = cifs_open,
1359 .release = cifs_close,
1360 .lock = cifs_lock,
Steve Frenchd0677992019-07-16 18:55:38 -05001361 .flock = cifs_flock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 .fsync = cifs_fsync,
1363 .flush = cifs_flush,
1364 .mmap = cifs_file_mmap,
David Howells4e260a82022-11-01 14:52:47 +00001365 .splice_read = cifs_splice_read,
Andrés Soutocd1aca22017-12-28 14:23:08 +01001366 .splice_write = iter_file_splice_write,
Steve Frenchc32a0b62006-01-12 14:41:28 -08001367 .llseek = cifs_llseek,
Steve Frenchf9ddcca2008-05-15 05:51:55 +00001368 .unlocked_ioctl = cifs_ioctl,
Sachin Prabhu620d8742017-02-10 16:03:51 +05301369 .copy_file_range = cifs_copy_file_range,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11001370 .remap_file_range = cifs_remap_file_range,
Steve French84210e92008-10-23 04:42:37 +00001371 .setlease = cifs_setlease,
Steve French31742c52014-08-17 08:38:47 -05001372 .fallocate = cifs_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373};
1374
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03001375const struct file_operations cifs_file_strict_ops = {
Al Viroe6a7bcb2014-04-02 19:53:36 -04001376 .read_iter = cifs_strict_readv,
Al Viro3dae8752014-04-03 12:05:17 -04001377 .write_iter = cifs_strict_writev,
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03001378 .open = cifs_open,
1379 .release = cifs_close,
1380 .lock = cifs_lock,
Steve Frenchd0677992019-07-16 18:55:38 -05001381 .flock = cifs_flock,
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03001382 .fsync = cifs_strict_fsync,
1383 .flush = cifs_flush,
Pavel Shilovsky7a6a19b2010-12-14 11:29:51 +03001384 .mmap = cifs_file_strict_mmap,
David Howells4e260a82022-11-01 14:52:47 +00001385 .splice_read = cifs_splice_read,
Andrés Soutocd1aca22017-12-28 14:23:08 +01001386 .splice_write = iter_file_splice_write,
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03001387 .llseek = cifs_llseek,
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03001388 .unlocked_ioctl = cifs_ioctl,
Sachin Prabhu620d8742017-02-10 16:03:51 +05301389 .copy_file_range = cifs_copy_file_range,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11001390 .remap_file_range = cifs_remap_file_range,
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03001391 .setlease = cifs_setlease,
Steve French31742c52014-08-17 08:38:47 -05001392 .fallocate = cifs_fallocate,
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03001393};
1394
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001395const struct file_operations cifs_file_direct_ops = {
Long Libe4eb682018-10-31 22:13:11 +00001396 .read_iter = cifs_direct_readv,
1397 .write_iter = cifs_direct_writev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 .open = cifs_open,
1399 .release = cifs_close,
1400 .lock = cifs_lock,
Steve Frenchd0677992019-07-16 18:55:38 -05001401 .flock = cifs_flock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 .fsync = cifs_fsync,
1403 .flush = cifs_flush,
Steve Frencha994b8f2009-12-07 05:44:46 +00001404 .mmap = cifs_file_mmap,
David Howells4e260a82022-11-01 14:52:47 +00001405 .splice_read = direct_splice_read,
Andrés Soutocd1aca22017-12-28 14:23:08 +01001406 .splice_write = iter_file_splice_write,
Steve Frenchf9ddcca2008-05-15 05:51:55 +00001407 .unlocked_ioctl = cifs_ioctl,
Sachin Prabhu620d8742017-02-10 16:03:51 +05301408 .copy_file_range = cifs_copy_file_range,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11001409 .remap_file_range = cifs_remap_file_range,
Steve Frenchc32a0b62006-01-12 14:41:28 -08001410 .llseek = cifs_llseek,
Steve French84210e92008-10-23 04:42:37 +00001411 .setlease = cifs_setlease,
Steve French31742c52014-08-17 08:38:47 -05001412 .fallocate = cifs_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413};
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03001414
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001415const struct file_operations cifs_file_nobrl_ops = {
Jeff Layton08bc0352014-05-23 06:50:21 -04001416 .read_iter = cifs_loose_read_iter,
Al Viro3dae8752014-04-03 12:05:17 -04001417 .write_iter = cifs_file_write_iter,
Steve French87c89dd2005-11-17 17:03:00 -08001418 .open = cifs_open,
1419 .release = cifs_close,
1420 .fsync = cifs_fsync,
1421 .flush = cifs_flush,
1422 .mmap = cifs_file_mmap,
David Howells4e260a82022-11-01 14:52:47 +00001423 .splice_read = cifs_splice_read,
Andrés Soutocd1aca22017-12-28 14:23:08 +01001424 .splice_write = iter_file_splice_write,
Steve Frenchc32a0b62006-01-12 14:41:28 -08001425 .llseek = cifs_llseek,
Steve Frenchf9ddcca2008-05-15 05:51:55 +00001426 .unlocked_ioctl = cifs_ioctl,
Sachin Prabhu620d8742017-02-10 16:03:51 +05301427 .copy_file_range = cifs_copy_file_range,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11001428 .remap_file_range = cifs_remap_file_range,
Steve French84210e92008-10-23 04:42:37 +00001429 .setlease = cifs_setlease,
Steve French31742c52014-08-17 08:38:47 -05001430 .fallocate = cifs_fallocate,
Steve French8b94bcb2005-11-11 11:41:00 -08001431};
1432
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03001433const struct file_operations cifs_file_strict_nobrl_ops = {
Al Viroe6a7bcb2014-04-02 19:53:36 -04001434 .read_iter = cifs_strict_readv,
Al Viro3dae8752014-04-03 12:05:17 -04001435 .write_iter = cifs_strict_writev,
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03001436 .open = cifs_open,
1437 .release = cifs_close,
1438 .fsync = cifs_strict_fsync,
1439 .flush = cifs_flush,
Pavel Shilovsky7a6a19b2010-12-14 11:29:51 +03001440 .mmap = cifs_file_strict_mmap,
David Howells4e260a82022-11-01 14:52:47 +00001441 .splice_read = cifs_splice_read,
Andrés Soutocd1aca22017-12-28 14:23:08 +01001442 .splice_write = iter_file_splice_write,
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03001443 .llseek = cifs_llseek,
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03001444 .unlocked_ioctl = cifs_ioctl,
Sachin Prabhu620d8742017-02-10 16:03:51 +05301445 .copy_file_range = cifs_copy_file_range,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11001446 .remap_file_range = cifs_remap_file_range,
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03001447 .setlease = cifs_setlease,
Steve French31742c52014-08-17 08:38:47 -05001448 .fallocate = cifs_fallocate,
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03001449};
1450
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001451const struct file_operations cifs_file_direct_nobrl_ops = {
Long Libe4eb682018-10-31 22:13:11 +00001452 .read_iter = cifs_direct_readv,
1453 .write_iter = cifs_direct_writev,
Steve French87c89dd2005-11-17 17:03:00 -08001454 .open = cifs_open,
1455 .release = cifs_close,
1456 .fsync = cifs_fsync,
1457 .flush = cifs_flush,
Pavel Shilovsky810627a02010-03-27 02:00:49 +00001458 .mmap = cifs_file_mmap,
David Howells4e260a82022-11-01 14:52:47 +00001459 .splice_read = direct_splice_read,
Andrés Soutocd1aca22017-12-28 14:23:08 +01001460 .splice_write = iter_file_splice_write,
Steve Frenchf9ddcca2008-05-15 05:51:55 +00001461 .unlocked_ioctl = cifs_ioctl,
Sachin Prabhu620d8742017-02-10 16:03:51 +05301462 .copy_file_range = cifs_copy_file_range,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11001463 .remap_file_range = cifs_remap_file_range,
Steve Frenchc32a0b62006-01-12 14:41:28 -08001464 .llseek = cifs_llseek,
Steve French84210e92008-10-23 04:42:37 +00001465 .setlease = cifs_setlease,
Steve French31742c52014-08-17 08:38:47 -05001466 .fallocate = cifs_fallocate,
Steve French8b94bcb2005-11-11 11:41:00 -08001467};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001469const struct file_operations cifs_dir_ops = {
Al Viro3125d262016-04-20 17:40:47 -04001470 .iterate_shared = cifs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 .release = cifs_closedir,
1472 .read = generic_read_dir,
Steve Frenchf9ddcca2008-05-15 05:51:55 +00001473 .unlocked_ioctl = cifs_ioctl,
Sachin Prabhu620d8742017-02-10 16:03:51 +05301474 .copy_file_range = cifs_copy_file_range,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11001475 .remap_file_range = cifs_remap_file_range,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +02001476 .llseek = generic_file_llseek,
Steve French6e70c262018-05-10 10:59:37 -05001477 .fsync = cifs_dir_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478};
1479
1480static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001481cifs_init_once(void *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
1483 struct cifsInodeInfo *cifsi = inode;
1484
David Howells874c8ca12022-06-09 21:46:04 +01001485 inode_init_once(&cifsi->netfs.inode);
Pavel Shilovsky1b4b55a2012-09-19 06:22:44 -07001486 init_rwsem(&cifsi->lock_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Fabian Frederick9ee108b2014-04-03 14:46:30 -07001489static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490cifs_init_inodecache(void)
1491{
1492 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
Steve French26f57362007-08-30 22:09:15 +00001493 sizeof(struct cifsInodeInfo),
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001494 0, (SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -08001495 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +09001496 cifs_init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (cifs_inode_cachep == NULL)
1498 return -ENOMEM;
1499
1500 return 0;
1501}
1502
1503static void
1504cifs_destroy_inodecache(void)
1505{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10001506 /*
1507 * Make sure all delayed rcu free inodes are flushed before we
1508 * destroy cache.
1509 */
1510 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001511 kmem_cache_destroy(cifs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
1514static int
1515cifs_init_request_bufs(void)
1516{
Pavel Shilovsky3792c172012-01-12 22:40:50 +04001517 /*
1518 * SMB2 maximum header size is bigger than CIFS one - no problems to
1519 * allocate some more bytes for CIFS.
1520 */
Steve French2a38e122017-07-08 18:48:15 -05001521 size_t max_hdr_size = MAX_SMB2_HDR_SIZE;
1522
Steve French4523cc32007-04-30 20:13:06 +00001523 if (CIFSMaxBufSize < 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
1525 Unicode path name has to fit in any SMB/CIFS path based frames */
1526 CIFSMaxBufSize = 8192;
1527 } else if (CIFSMaxBufSize > 1024*127) {
1528 CIFSMaxBufSize = 1024 * 127;
1529 } else {
1530 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
1531 }
Joe Perchesf96637b2013-05-04 22:12:25 -05001532/*
1533 cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
1534 CIFSMaxBufSize, CIFSMaxBufSize);
1535*/
David Windsorde046442017-06-10 22:50:33 -04001536 cifs_req_cachep = kmem_cache_create_usercopy("cifs_request",
Pavel Shilovsky3792c172012-01-12 22:40:50 +04001537 CIFSMaxBufSize + max_hdr_size, 0,
David Windsorde046442017-06-10 22:50:33 -04001538 SLAB_HWCACHE_ALIGN, 0,
1539 CIFSMaxBufSize + max_hdr_size,
1540 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (cifs_req_cachep == NULL)
1542 return -ENOMEM;
1543
Steve French4523cc32007-04-30 20:13:06 +00001544 if (cifs_min_rcv < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 cifs_min_rcv = 1;
1546 else if (cifs_min_rcv > 64) {
1547 cifs_min_rcv = 64;
Joe Perchesf96637b2013-05-04 22:12:25 -05001548 cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550
Matthew Dobson93d23412006-03-26 01:37:50 -08001551 cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1552 cifs_req_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Steve French4523cc32007-04-30 20:13:06 +00001554 if (cifs_req_poolp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 kmem_cache_destroy(cifs_req_cachep);
1556 return -ENOMEM;
1557 }
Steve Frenchec637e32005-12-12 20:53:18 -08001558 /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 almost all handle based requests (but not write response, nor is it
1560 sufficient for path based requests). A smaller size would have
Steve French50c2f752007-07-13 00:33:32 +00001561 been more efficient (compacting multiple slab items on one 4k page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 for the case in which debug was on, but this larger size allows
1563 more SMBs to use small buffer alloc and is still much more
Steve French6dc0f872007-07-06 23:13:06 +00001564 efficient to alloc 1 per page off the slab compared to 17K (5page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 alloc of large cifs buffers even when page debugging is on */
David Windsorde046442017-06-10 22:50:33 -04001566 cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq",
Steve French6dc0f872007-07-06 23:13:06 +00001567 MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
David Windsorde046442017-06-10 22:50:33 -04001568 0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (cifs_sm_req_cachep == NULL) {
1570 mempool_destroy(cifs_req_poolp);
1571 kmem_cache_destroy(cifs_req_cachep);
Steve French6dc0f872007-07-06 23:13:06 +00001572 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574
Steve French4523cc32007-04-30 20:13:06 +00001575 if (cifs_min_small < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 cifs_min_small = 2;
1577 else if (cifs_min_small > 256) {
1578 cifs_min_small = 256;
Joe Perchesf96637b2013-05-04 22:12:25 -05001579 cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581
Matthew Dobson93d23412006-03-26 01:37:50 -08001582 cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1583 cifs_sm_req_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Steve French4523cc32007-04-30 20:13:06 +00001585 if (cifs_sm_req_poolp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 mempool_destroy(cifs_req_poolp);
1587 kmem_cache_destroy(cifs_req_cachep);
1588 kmem_cache_destroy(cifs_sm_req_cachep);
1589 return -ENOMEM;
1590 }
1591
1592 return 0;
1593}
1594
1595static void
1596cifs_destroy_request_bufs(void)
1597{
1598 mempool_destroy(cifs_req_poolp);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001599 kmem_cache_destroy(cifs_req_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 mempool_destroy(cifs_sm_req_poolp);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001601 kmem_cache_destroy(cifs_sm_req_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
1603
Enzo Matsumiyaf5fd3f282022-08-05 11:47:39 -03001604static int init_mids(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
Steve French26f57362007-08-30 22:09:15 +00001607 sizeof(struct mid_q_entry), 0,
1608 SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (cifs_mid_cachep == NULL)
1610 return -ENOMEM;
1611
Matthew Dobson93d23412006-03-26 01:37:50 -08001612 /* 3 is a reasonable minimum number of simultaneous operations */
1613 cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
Steve French4523cc32007-04-30 20:13:06 +00001614 if (cifs_mid_poolp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 kmem_cache_destroy(cifs_mid_cachep);
1616 return -ENOMEM;
1617 }
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return 0;
1620}
1621
Enzo Matsumiyaf5fd3f282022-08-05 11:47:39 -03001622static void destroy_mids(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 mempool_destroy(cifs_mid_poolp);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001625 kmem_cache_destroy(cifs_mid_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628static int __init
1629init_cifs(void)
1630{
1631 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 cifs_proc_init();
Jeff Laytone7ddee92008-11-14 13:44:38 -05001633 INIT_LIST_HEAD(&cifs_tcp_ses_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634/*
1635 * Initialize Global counters
1636 */
1637 atomic_set(&sesInfoAllocCount, 0);
1638 atomic_set(&tconInfoAllocCount, 0);
Shyam Prasad N6d82c272021-02-03 23:20:46 -08001639 atomic_set(&tcpSesNextId, 0);
Steve French6dc0f872007-07-06 23:13:06 +00001640 atomic_set(&tcpSesAllocCount, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 atomic_set(&tcpSesReconnectCount, 0);
1642 atomic_set(&tconInfoReconnectCount, 0);
1643
Steve Frenchc2c17dd2022-07-15 23:45:45 -05001644 atomic_set(&buf_alloc_count, 0);
1645 atomic_set(&small_buf_alloc_count, 0);
Steve French4498eed52005-12-03 13:58:57 -08001646#ifdef CONFIG_CIFS_STATS2
Steve Frenchc2c17dd2022-07-15 23:45:45 -05001647 atomic_set(&total_buf_alloc_count, 0);
1648 atomic_set(&total_small_buf_alloc_count, 0);
Steve French00778e22018-09-18 14:05:18 -05001649 if (slow_rsp_threshold < 1)
1650 cifs_dbg(FYI, "slow_response_threshold msgs disabled\n");
1651 else if (slow_rsp_threshold > 32767)
1652 cifs_dbg(VFS,
1653 "slow response threshold set higher than recommended (0 to 32767)\n");
Steve French4498eed52005-12-03 13:58:57 -08001654#endif /* CONFIG_CIFS_STATS2 */
1655
Steve Frenchc2c17dd2022-07-15 23:45:45 -05001656 atomic_set(&mid_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 GlobalCurrentXid = 0;
1658 GlobalTotalActiveXid = 0;
1659 GlobalMaxActiveXid = 0;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301660 spin_lock_init(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 spin_lock_init(&GlobalMid_Lock);
1662
Jason A. Donenfeld51b08172017-06-07 20:42:50 -04001663 cifs_lock_secret = get_random_u32();
Jeff Layton3d224622016-05-24 06:27:44 -04001664
Steve French4523cc32007-04-30 20:13:06 +00001665 if (cifs_max_pending < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 cifs_max_pending = 2;
Joe Perchesf96637b2013-05-04 22:12:25 -05001667 cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
Pavel Shilovsky10b9b982012-03-20 12:55:09 +03001668 } else if (cifs_max_pending > CIFS_MAX_REQ) {
1669 cifs_max_pending = CIFS_MAX_REQ;
Joe Perchesf96637b2013-05-04 22:12:25 -05001670 cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
1671 CIFS_MAX_REQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673
Jeff Laytonda472fc2012-03-23 14:40:53 -04001674 cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1675 if (!cifsiod_wq) {
1676 rc = -ENOMEM;
1677 goto out_clean_proc;
1678 }
1679
Steve French35cf94a2019-09-07 01:09:49 -05001680 /*
Steve French10328c42019-09-09 13:30:15 -05001681 * Consider in future setting limit!=0 maybe to min(num_of_cores - 1, 3)
1682 * so that we don't launch too many worker threads but
Mauro Carvalho Chehab0ac624f2019-09-24 10:01:28 -03001683 * Documentation/core-api/workqueue.rst recommends setting it to 0
Steve French35cf94a2019-09-07 01:09:49 -05001684 */
Steve French10328c42019-09-09 13:30:15 -05001685
1686 /* WQ_UNBOUND allows decrypt tasks to run on any CPU */
Steve French35cf94a2019-09-07 01:09:49 -05001687 decrypt_wq = alloc_workqueue("smb3decryptd",
Steve French10328c42019-09-09 13:30:15 -05001688 WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
Steve French35cf94a2019-09-07 01:09:49 -05001689 if (!decrypt_wq) {
1690 rc = -ENOMEM;
1691 goto out_destroy_cifsiod_wq;
1692 }
1693
Ronnie Sahlberg32546a92019-11-03 13:06:37 +10001694 fileinfo_put_wq = alloc_workqueue("cifsfileinfoput",
1695 WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1696 if (!fileinfo_put_wq) {
1697 rc = -ENOMEM;
1698 goto out_destroy_decrypt_wq;
1699 }
1700
Rabin Vincent3998e6b82017-05-03 17:54:01 +02001701 cifsoplockd_wq = alloc_workqueue("cifsoplockd",
1702 WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1703 if (!cifsoplockd_wq) {
1704 rc = -ENOMEM;
Ronnie Sahlberg32546a92019-11-03 13:06:37 +10001705 goto out_destroy_fileinfo_put_wq;
Rabin Vincent3998e6b82017-05-03 17:54:01 +02001706 }
1707
Rohith Surabattulac3f207ab2021-04-13 00:26:42 -05001708 deferredclose_wq = alloc_workqueue("deferredclose",
1709 WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1710 if (!deferredclose_wq) {
1711 rc = -ENOMEM;
1712 goto out_destroy_cifsoplockd_wq;
1713 }
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 rc = cifs_init_inodecache();
Steve French45af7a02006-04-21 22:52:25 +00001716 if (rc)
David Howells70431bf2020-11-17 15:56:59 +00001717 goto out_destroy_deferredclose_wq;
Steve French45af7a02006-04-21 22:52:25 +00001718
Enzo Matsumiyaf5fd3f282022-08-05 11:47:39 -03001719 rc = init_mids();
Steve French45af7a02006-04-21 22:52:25 +00001720 if (rc)
1721 goto out_destroy_inodecache;
1722
1723 rc = cifs_init_request_bufs();
1724 if (rc)
1725 goto out_destroy_mids;
1726
Paulo Alcantara1c780222018-11-14 16:24:03 -02001727#ifdef CONFIG_CIFS_DFS_UPCALL
1728 rc = dfs_cache_init();
1729 if (rc)
1730 goto out_destroy_request_bufs;
1731#endif /* CONFIG_CIFS_DFS_UPCALL */
Jeff Layton84a15b92007-11-03 05:02:24 +00001732#ifdef CONFIG_CIFS_UPCALL
Sachin Prabhub74cb9a2016-05-17 18:20:13 -05001733 rc = init_cifs_spnego();
Jeff Layton84a15b92007-11-03 05:02:24 +00001734 if (rc)
Paulo Alcantara1c780222018-11-14 16:24:03 -02001735 goto out_destroy_dfs_cache;
Shirish Pargaonkar4d79dba2011-04-27 23:34:35 -05001736#endif /* CONFIG_CIFS_UPCALL */
Samuel Cabrero06f08da2020-11-30 19:02:49 +01001737#ifdef CONFIG_CIFS_SWN_UPCALL
1738 rc = cifs_genl_init();
1739 if (rc)
1740 goto out_register_key_type;
1741#endif /* CONFIG_CIFS_SWN_UPCALL */
Shirish Pargaonkar4d79dba2011-04-27 23:34:35 -05001742
Shirish Pargaonkar4d79dba2011-04-27 23:34:35 -05001743 rc = init_cifs_idmap();
1744 if (rc)
Samuel Cabrero06f08da2020-11-30 19:02:49 +01001745 goto out_cifs_swn_init;
Shirish Pargaonkar4d79dba2011-04-27 23:34:35 -05001746
1747 rc = register_filesystem(&cifs_fs_type);
1748 if (rc)
Shirish Pargaonkarc4aca0c2011-05-06 02:35:00 -05001749 goto out_init_cifs_idmap;
Steve French45af7a02006-04-21 22:52:25 +00001750
Steve French49218b42018-05-23 21:44:53 -05001751 rc = register_filesystem(&smb3_fs_type);
1752 if (rc) {
1753 unregister_filesystem(&cifs_fs_type);
1754 goto out_init_cifs_idmap;
1755 }
1756
Steve French45af7a02006-04-21 22:52:25 +00001757 return 0;
1758
Shirish Pargaonkarc4aca0c2011-05-06 02:35:00 -05001759out_init_cifs_idmap:
Shirish Pargaonkar4d79dba2011-04-27 23:34:35 -05001760 exit_cifs_idmap();
Samuel Cabrero06f08da2020-11-30 19:02:49 +01001761out_cifs_swn_init:
1762#ifdef CONFIG_CIFS_SWN_UPCALL
1763 cifs_genl_exit();
Shirish Pargaonkarc4aca0c2011-05-06 02:35:00 -05001764out_register_key_type:
Samuel Cabrero06f08da2020-11-30 19:02:49 +01001765#endif
Shirish Pargaonkar4d79dba2011-04-27 23:34:35 -05001766#ifdef CONFIG_CIFS_UPCALL
Sachin Prabhub74cb9a2016-05-17 18:20:13 -05001767 exit_cifs_spnego();
Paulo Alcantara1c780222018-11-14 16:24:03 -02001768out_destroy_dfs_cache:
1769#endif
1770#ifdef CONFIG_CIFS_DFS_UPCALL
1771 dfs_cache_destroy();
Shirish Pargaonkarc4aca0c2011-05-06 02:35:00 -05001772out_destroy_request_bufs:
Shirish Pargaonkar4d79dba2011-04-27 23:34:35 -05001773#endif
Steve French45af7a02006-04-21 22:52:25 +00001774 cifs_destroy_request_bufs();
Steve Frenchd3bf5222010-09-22 19:15:36 +00001775out_destroy_mids:
Enzo Matsumiyaf5fd3f282022-08-05 11:47:39 -03001776 destroy_mids();
Steve Frenchd3bf5222010-09-22 19:15:36 +00001777out_destroy_inodecache:
Steve French45af7a02006-04-21 22:52:25 +00001778 cifs_destroy_inodecache();
Rohith Surabattulac3f207ab2021-04-13 00:26:42 -05001779out_destroy_deferredclose_wq:
1780 destroy_workqueue(deferredclose_wq);
Rabin Vincent3998e6b82017-05-03 17:54:01 +02001781out_destroy_cifsoplockd_wq:
1782 destroy_workqueue(cifsoplockd_wq);
Ronnie Sahlberg32546a92019-11-03 13:06:37 +10001783out_destroy_fileinfo_put_wq:
1784 destroy_workqueue(fileinfo_put_wq);
Steve French35cf94a2019-09-07 01:09:49 -05001785out_destroy_decrypt_wq:
1786 destroy_workqueue(decrypt_wq);
Rabin Vincent3998e6b82017-05-03 17:54:01 +02001787out_destroy_cifsiod_wq:
Jeff Laytonda472fc2012-03-23 14:40:53 -04001788 destroy_workqueue(cifsiod_wq);
Steve Frenchd3bf5222010-09-22 19:15:36 +00001789out_clean_proc:
1790 cifs_proc_clean();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return rc;
1792}
1793
1794static void __exit
1795exit_cifs(void)
1796{
Steve French49218b42018-05-23 21:44:53 -05001797 cifs_dbg(NOISY, "exit_smb3\n");
Jeff Layton3dd93302012-03-21 06:27:55 -04001798 unregister_filesystem(&cifs_fs_type);
Steve French49218b42018-05-23 21:44:53 -05001799 unregister_filesystem(&smb3_fs_type);
Igor Mammedov78d31a32008-04-24 12:56:07 +04001800 cifs_dfs_release_automount_timer();
Shirish Pargaonkar4d79dba2011-04-27 23:34:35 -05001801 exit_cifs_idmap();
Samuel Cabrero06f08da2020-11-30 19:02:49 +01001802#ifdef CONFIG_CIFS_SWN_UPCALL
1803 cifs_genl_exit();
1804#endif
Jeff Layton84a15b92007-11-03 05:02:24 +00001805#ifdef CONFIG_CIFS_UPCALL
Shu Wang94183332017-09-07 16:03:27 +08001806 exit_cifs_spnego();
Jeff Layton84a15b92007-11-03 05:02:24 +00001807#endif
Paulo Alcantara1c780222018-11-14 16:24:03 -02001808#ifdef CONFIG_CIFS_DFS_UPCALL
1809 dfs_cache_destroy();
1810#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 cifs_destroy_request_bufs();
Enzo Matsumiyaf5fd3f282022-08-05 11:47:39 -03001812 destroy_mids();
Jeff Layton3dd93302012-03-21 06:27:55 -04001813 cifs_destroy_inodecache();
Rohith Surabattulac3f207ab2021-04-13 00:26:42 -05001814 destroy_workqueue(deferredclose_wq);
Rabin Vincent3998e6b82017-05-03 17:54:01 +02001815 destroy_workqueue(cifsoplockd_wq);
Steve French35cf94a2019-09-07 01:09:49 -05001816 destroy_workqueue(decrypt_wq);
Ronnie Sahlberg32546a92019-11-03 13:06:37 +10001817 destroy_workqueue(fileinfo_put_wq);
Jeff Laytonda472fc2012-03-23 14:40:53 -04001818 destroy_workqueue(cifsiod_wq);
Jeff Layton3dd93302012-03-21 06:27:55 -04001819 cifs_proc_clean();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
Steve French1c3a13a2018-09-18 04:07:45 -05001822MODULE_AUTHOR("Steve French");
Steve French6dc0f872007-07-06 23:13:06 +00001823MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824MODULE_DESCRIPTION
Steve French1c3a13a2018-09-18 04:07:45 -05001825 ("VFS to access SMB3 servers e.g. Samba, Macs, Azure and Windows (and "
1826 "also older servers complying with the SNIA CIFS Specification)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827MODULE_VERSION(CIFS_VERSION);
Ronnie Sahlberg3591bb82019-10-31 13:55:14 +10001828MODULE_SOFTDEP("ecb");
1829MODULE_SOFTDEP("hmac");
Ronnie Sahlberg3591bb82019-10-31 13:55:14 +10001830MODULE_SOFTDEP("md5");
1831MODULE_SOFTDEP("nls");
1832MODULE_SOFTDEP("aes");
1833MODULE_SOFTDEP("cmac");
1834MODULE_SOFTDEP("sha256");
1835MODULE_SOFTDEP("sha512");
1836MODULE_SOFTDEP("aead2");
1837MODULE_SOFTDEP("ccm");
1838MODULE_SOFTDEP("gcm");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839module_init(init_cifs)
1840module_exit(exit_cifs)