]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/cifs/cifsfs.c
Merge branch 'libnvdimm-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdim...
[mirror_ubuntu-artful-kernel.git] / fs / cifs / cifsfs.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/cifsfs.c
3 *
2b280fab 4 * Copyright (C) International Business Machines Corp., 2002,2008
1da177e4
LT
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * Common Internet FileSystem (CIFS) client
8 *
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24/* Note that BB means BUGBUG (ie something to fix eventually) */
25
26#include <linux/module.h>
27#include <linux/fs.h>
28#include <linux/mount.h>
29#include <linux/slab.h>
30#include <linux/init.h>
31#include <linux/list.h>
32#include <linux/seq_file.h>
33#include <linux/vfs.h>
34#include <linux/mempool.h>
6ab16d24 35#include <linux/delay.h>
45af7a0f 36#include <linux/kthread.h>
7dfb7103 37#include <linux/freezer.h>
fec11dd9 38#include <linux/namei.h>
b8c32dbb 39#include <linux/random.h>
c6e970a0 40#include <linux/uuid.h>
a9ae008f 41#include <linux/xattr.h>
3eb9a889 42#include <net/ipv6.h>
1da177e4
LT
43#include "cifsfs.h"
44#include "cifspdu.h"
45#define DECLARE_GLOBALS_HERE
46#include "cifsglob.h"
47#include "cifsproto.h"
48#include "cifs_debug.h"
49#include "cifs_fs_sb.h"
50#include <linux/mm.h>
84a15b93 51#include <linux/key-type.h>
e545937a 52#include "cifs_spnego.h"
f579cf3c 53#include "fscache.h"
3792c173
PS
54#ifdef CONFIG_CIFS_SMB2
55#include "smb2pdu.h"
56#endif
1da177e4 57
1da177e4 58int cifsFYI = 0;
1404297e 59bool traceSMB;
e7504734 60bool enable_oplocks = true;
1404297e
KC
61bool linuxExtEnabled = true;
62bool lookupCacheEnabled = true;
04912d6a 63unsigned int global_secflags = CIFSSEC_DEF;
3979877e 64/* unsigned int ntlmv2_support = 0; */
1da177e4 65unsigned int sign_CIFS_PDUs = 1;
ee9b6d61 66static const struct super_operations cifs_super_ops;
1da177e4 67unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
cb978ac8 68module_param(CIFSMaxBufSize, uint, 0444);
63135e08
SF
69MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header). "
70 "Default: 16384 Range: 8192 to 130048");
1da177e4 71unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
cb978ac8 72module_param(cifs_min_rcv, uint, 0444);
63135e08
SF
73MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
74 "1 to 64");
1da177e4 75unsigned int cifs_min_small = 30;
cb978ac8 76module_param(cifs_min_small, uint, 0444);
63135e08
SF
77MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
78 "Range: 2 to 256");
1da177e4 79unsigned int cifs_max_pending = CIFS_MAX_REQ;
60654ce0 80module_param(cifs_max_pending, uint, 0444);
63135e08 81MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server. "
10b9b98e 82 "Default: 32767 Range: 2 to 32767.");
e7504734 83module_param(enable_oplocks, bool, 0644);
60654ce0 84MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1");
e7504734 85
1da177e4
LT
86extern mempool_t *cifs_sm_req_poolp;
87extern mempool_t *cifs_req_poolp;
88extern mempool_t *cifs_mid_poolp;
89
da472fc8 90struct workqueue_struct *cifsiod_wq;
3998e6b8 91struct workqueue_struct *cifsoplockd_wq;
3d22462a 92__u32 cifs_lock_secret;
da472fc8 93
24261fc2
MG
94/*
95 * Bumps refcount for cifs super block.
96 * Note that it should be only called if a referece to VFS super block is
97 * already held, e.g. in open-type syscalls context. Otherwise it can race with
98 * atomic_dec_and_test in deactivate_locked_super.
99 */
100void
101cifs_sb_active(struct super_block *sb)
102{
103 struct cifs_sb_info *server = CIFS_SB(sb);
104
105 if (atomic_inc_return(&server->active) == 1)
106 atomic_inc(&sb->s_active);
107}
108
109void
110cifs_sb_deactive(struct super_block *sb)
111{
112 struct cifs_sb_info *server = CIFS_SB(sb);
113
114 if (atomic_dec_and_test(&server->active))
115 deactivate_super(sb);
116}
117
1da177e4 118static int
97d1152a 119cifs_read_super(struct super_block *sb)
1da177e4
LT
120{
121 struct inode *inode;
b2e5cd33 122 struct cifs_sb_info *cifs_sb;
2f6c9479 123 struct cifs_tcon *tcon;
1da177e4 124 int rc = 0;
50c2f753 125
b2e5cd33 126 cifs_sb = CIFS_SB(sb);
2f6c9479 127 tcon = cifs_sb_master_tcon(cifs_sb);
1da177e4 128
2c6292ae
AV
129 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
130 sb->s_flags |= MS_POSIXACL;
131
2f6c9479 132 if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files)
2c6292ae
AV
133 sb->s_maxbytes = MAX_LFS_FILESIZE;
134 else
135 sb->s_maxbytes = MAX_NON_LFS;
136
137 /* BB FIXME fix time_gran to be larger for LANMAN sessions */
138 sb->s_time_gran = 100;
139
1da177e4
LT
140 sb->s_magic = CIFS_MAGIC_NUMBER;
141 sb->s_op = &cifs_super_ops;
a9ae008f 142 sb->s_xattr = cifs_xattr_handlers;
851ea086
JK
143 rc = super_setup_bdi(sb);
144 if (rc)
145 goto out_no_root;
146 /* tune readahead according to rsize */
147 sb->s_bdi->ra_pages = cifs_sb->rsize / PAGE_SIZE;
148
1da177e4
LT
149 sb->s_blocksize = CIFS_MAX_MSGSIZE;
150 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
9b6763e0 151 inode = cifs_root_iget(sb);
1da177e4 152
ce634ab2
DH
153 if (IS_ERR(inode)) {
154 rc = PTR_ERR(inode);
1da177e4
LT
155 goto out_no_root;
156 }
157
2f6c9479 158 if (tcon->nocase)
66ffd113
JL
159 sb->s_d_op = &cifs_ci_dentry_ops;
160 else
161 sb->s_d_op = &cifs_dentry_ops;
162
48fde701 163 sb->s_root = d_make_root(inode);
1da177e4
LT
164 if (!sb->s_root) {
165 rc = -ENOMEM;
166 goto out_no_root;
167 }
50c2f753 168
f3a6a60e 169#ifdef CONFIG_CIFS_NFSD_EXPORT
7521a3c5 170 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
f96637be 171 cifs_dbg(FYI, "export ops supported\n");
7521a3c5
SF
172 sb->s_export_op = &cifs_export_ops;
173 }
f3a6a60e 174#endif /* CONFIG_CIFS_NFSD_EXPORT */
1da177e4
LT
175
176 return 0;
177
178out_no_root:
f96637be 179 cifs_dbg(VFS, "%s: get root inode failed\n", __func__);
1da177e4
LT
180 return rc;
181}
182
6d686175
AV
183static void cifs_kill_sb(struct super_block *sb)
184{
185 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
186 kill_anon_super(sb);
98ab494d 187 cifs_umount(cifs_sb);
1da177e4
LT
188}
189
190static int
726c3342 191cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 192{
726c3342 193 struct super_block *sb = dentry->d_sb;
39da9847 194 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
96daf2b0 195 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
76ec5e33 196 struct TCP_Server_Info *server = tcon->ses->server;
6d5786a3 197 unsigned int xid;
76ec5e33 198 int rc = 0;
1da177e4 199
6d5786a3 200 xid = get_xid();
1da177e4 201
39da9847
SF
202 /*
203 * PATH_MAX may be too long - it would presumably be total path,
204 * but note that some servers (includinng Samba 3) have a shorter
205 * maximum path.
206 *
207 * Instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO.
208 */
209 buf->f_namelen = PATH_MAX;
1da177e4
LT
210 buf->f_files = 0; /* undefined */
211 buf->f_ffree = 0; /* unlimited */
212
76ec5e33
PS
213 if (server->ops->queryfs)
214 rc = server->ops->queryfs(xid, tcon, buf);
39da9847 215
6d5786a3 216 free_xid(xid);
39da9847 217 return 0;
1da177e4
LT
218}
219
31742c5a
SF
220static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
221{
7119e220 222 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
31742c5a
SF
223 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
224 struct TCP_Server_Info *server = tcon->ses->server;
225
226 if (server->ops->fallocate)
227 return server->ops->fallocate(file, tcon, mode, off, len);
228
229 return -EOPNOTSUPP;
230}
231
10556cb2 232static int cifs_permission(struct inode *inode, int mask)
1da177e4
LT
233{
234 struct cifs_sb_info *cifs_sb;
235
236 cifs_sb = CIFS_SB(inode->i_sb);
237
f696a365
MS
238 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
239 if ((mask & MAY_EXEC) && !execute_ok(inode))
240 return -EACCES;
241 else
242 return 0;
243 } else /* file mode might have been restricted at mount time
50c2f753 244 on the client (above and beyond ACL on servers) for
1da177e4 245 servers which do not support setting and viewing mode bits,
50c2f753 246 so allowing client to check permissions is useful */
2830ba7f 247 return generic_permission(inode, mask);
1da177e4
LT
248}
249
e18b890b
CL
250static struct kmem_cache *cifs_inode_cachep;
251static struct kmem_cache *cifs_req_cachep;
252static struct kmem_cache *cifs_mid_cachep;
e18b890b 253static struct kmem_cache *cifs_sm_req_cachep;
1da177e4
LT
254mempool_t *cifs_sm_req_poolp;
255mempool_t *cifs_req_poolp;
256mempool_t *cifs_mid_poolp;
257
258static struct inode *
259cifs_alloc_inode(struct super_block *sb)
260{
261 struct cifsInodeInfo *cifs_inode;
e94b1766 262 cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
1da177e4
LT
263 if (!cifs_inode)
264 return NULL;
265 cifs_inode->cifsAttrs = 0x20; /* default */
1da177e4 266 cifs_inode->time = 0;
b8c32dbb
PS
267 /*
268 * Until the file is open and we have gotten oplock info back from the
269 * server, can not assume caching of file data or metadata.
270 */
c6723628 271 cifs_set_oplock_level(cifs_inode, 0);
aff8d5ca 272 cifs_inode->flags = 0;
c11f1df5
SP
273 spin_lock_init(&cifs_inode->writers_lock);
274 cifs_inode->writers = 0;
1da177e4 275 cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
fbec9ab9 276 cifs_inode->server_eof = 0;
20054bd6
JL
277 cifs_inode->uniqueid = 0;
278 cifs_inode->createtime = 0;
42873b0a 279 cifs_inode->epoch = 0;
b8c32dbb 280#ifdef CONFIG_CIFS_SMB2
fa70b87c 281 generate_random_uuid(cifs_inode->lease_key);
b8c32dbb
PS
282#endif
283 /*
284 * Can not set i_flags here - they get immediately overwritten to zero
285 * by the VFS.
286 */
287 /* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME; */
1da177e4 288 INIT_LIST_HEAD(&cifs_inode->openFileList);
f45d3416 289 INIT_LIST_HEAD(&cifs_inode->llist);
1da177e4
LT
290 return &cifs_inode->vfs_inode;
291}
292
fa0d7e3d
NP
293static void cifs_i_callback(struct rcu_head *head)
294{
295 struct inode *inode = container_of(head, struct inode, i_rcu);
fa0d7e3d
NP
296 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
297}
298
1da177e4
LT
299static void
300cifs_destroy_inode(struct inode *inode)
301{
fa0d7e3d 302 call_rcu(&inode->i_rcu, cifs_i_callback);
1da177e4
LT
303}
304
9451a9a5 305static void
b57922d9 306cifs_evict_inode(struct inode *inode)
9451a9a5 307{
91b0abe3 308 truncate_inode_pages_final(&inode->i_data);
dbd5768f 309 clear_inode(inode);
9451a9a5
SJ
310 cifs_fscache_release_inode_cookie(inode);
311}
312
61f98ffd
JL
313static void
314cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
315{
a9f1b85e
PS
316 struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
317 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
318
571d5972 319 seq_puts(s, ",addr=");
61f98ffd 320
a9f1b85e 321 switch (server->dstaddr.ss_family) {
61f98ffd 322 case AF_INET:
a9f1b85e 323 seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
61f98ffd
JL
324 break;
325 case AF_INET6:
a9f1b85e
PS
326 seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
327 if (sa6->sin6_scope_id)
328 seq_printf(s, "%%%u", sa6->sin6_scope_id);
61f98ffd
JL
329 break;
330 default:
571d5972 331 seq_puts(s, "(unknown)");
61f98ffd
JL
332 }
333}
334
3e715513 335static void
28e11bd8 336cifs_show_security(struct seq_file *s, struct cifs_ses *ses)
3e715513 337{
eda2116f
SF
338 if (ses->sectype == Unspecified) {
339 if (ses->user_name == NULL)
340 seq_puts(s, ",sec=none");
28e11bd8 341 return;
eda2116f 342 }
28e11bd8 343
571d5972 344 seq_puts(s, ",sec=");
3e715513 345
28e11bd8 346 switch (ses->sectype) {
3e715513 347 case LANMAN:
571d5972 348 seq_puts(s, "lanman");
3e715513
JL
349 break;
350 case NTLMv2:
571d5972 351 seq_puts(s, "ntlmv2");
3e715513
JL
352 break;
353 case NTLM:
571d5972 354 seq_puts(s, "ntlm");
3e715513
JL
355 break;
356 case Kerberos:
571d5972 357 seq_puts(s, "krb5");
3e715513
JL
358 break;
359 case RawNTLMSSP:
571d5972 360 seq_puts(s, "ntlmssp");
3e715513
JL
361 break;
362 default:
363 /* shouldn't ever happen */
571d5972 364 seq_puts(s, "unknown");
3e715513
JL
365 break;
366 }
367
28e11bd8 368 if (ses->sign)
571d5972 369 seq_puts(s, "i");
3e715513
JL
370}
371
d06b5056
JL
372static void
373cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
374{
571d5972 375 seq_puts(s, ",cache=");
d06b5056
JL
376
377 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
571d5972 378 seq_puts(s, "strict");
d06b5056 379 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
571d5972 380 seq_puts(s, "none");
d06b5056 381 else
571d5972 382 seq_puts(s, "loose");
d06b5056
JL
383}
384
3ae35cde
JL
385static void
386cifs_show_nls(struct seq_file *s, struct nls_table *cur)
387{
388 struct nls_table *def;
389
390 /* Display iocharset= option if it's not default charset */
391 def = load_nls_default();
392 if (def != cur)
393 seq_printf(s, ",iocharset=%s", cur->charset);
394 unload_nls(def);
395}
396
1da177e4
LT
397/*
398 * cifs_show_options() is for displaying mount options in /proc/mounts.
399 * Not all settable options are displayed but most of the important
400 * ones are.
401 */
402static int
34c80b1d 403cifs_show_options(struct seq_file *s, struct dentry *root)
1da177e4 404{
34c80b1d 405 struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
96daf2b0 406 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
3eb9a889
BG
407 struct sockaddr *srcaddr;
408 srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
8616e0fc 409
a068acf2 410 seq_show_option(s, "vers", tcon->ses->server->vals->version_string);
28e11bd8 411 cifs_show_security(s, tcon->ses);
d06b5056 412 cifs_show_cache_flavor(s, cifs_sb);
3e715513 413
29e07c82 414 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)
571d5972 415 seq_puts(s, ",multiuser");
8727c8a8 416 else if (tcon->ses->user_name)
a068acf2 417 seq_show_option(s, "username", tcon->ses->user_name);
29e07c82 418
8616e0fc 419 if (tcon->ses->domainName)
a068acf2 420 seq_show_option(s, "domain", tcon->ses->domainName);
8616e0fc 421
3eb9a889
BG
422 if (srcaddr->sa_family != AF_UNSPEC) {
423 struct sockaddr_in *saddr4;
424 struct sockaddr_in6 *saddr6;
425 saddr4 = (struct sockaddr_in *)srcaddr;
426 saddr6 = (struct sockaddr_in6 *)srcaddr;
427 if (srcaddr->sa_family == AF_INET6)
428 seq_printf(s, ",srcaddr=%pI6c",
429 &saddr6->sin6_addr);
430 else if (srcaddr->sa_family == AF_INET)
431 seq_printf(s, ",srcaddr=%pI4",
432 &saddr4->sin_addr.s_addr);
433 else
434 seq_printf(s, ",srcaddr=BAD-AF:%i",
435 (int)(srcaddr->sa_family));
436 }
437
1f68233c
EB
438 seq_printf(s, ",uid=%u",
439 from_kuid_munged(&init_user_ns, cifs_sb->mnt_uid));
340481a3 440 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
571d5972 441 seq_puts(s, ",forceuid");
4486d6ed 442 else
571d5972 443 seq_puts(s, ",noforceuid");
340481a3 444
1f68233c
EB
445 seq_printf(s, ",gid=%u",
446 from_kgid_munged(&init_user_ns, cifs_sb->mnt_gid));
340481a3 447 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
571d5972 448 seq_puts(s, ",forcegid");
4486d6ed 449 else
571d5972 450 seq_puts(s, ",noforcegid");
8616e0fc 451
61f98ffd 452 cifs_show_address(s, tcon->ses->server);
1da177e4 453
8616e0fc 454 if (!tcon->unix_ext)
5206efd6 455 seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
2b280fab
SF
456 cifs_sb->mnt_file_mode,
457 cifs_sb->mnt_dir_mode);
3ae35cde
JL
458
459 cifs_show_nls(s, cifs_sb->local_nls);
460
8616e0fc 461 if (tcon->seal)
571d5972 462 seq_puts(s, ",seal");
8616e0fc 463 if (tcon->nocase)
571d5972 464 seq_puts(s, ",nocase");
8616e0fc 465 if (tcon->retry)
571d5972 466 seq_puts(s, ",hard");
f16dfa7c
SF
467 if (tcon->use_persistent)
468 seq_puts(s, ",persistenthandles");
592fafe6
SF
469 else if (tcon->use_resilient)
470 seq_puts(s, ",resilienthandles");
d4ffff1f 471 if (tcon->unix_ext)
571d5972 472 seq_puts(s, ",unix");
d4ffff1f 473 else
571d5972 474 seq_puts(s, ",nounix");
8616e0fc 475 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
571d5972 476 seq_puts(s, ",posixpaths");
8616e0fc 477 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
571d5972 478 seq_puts(s, ",setuids");
95932655
SF
479 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
480 seq_puts(s, ",idsfromsid");
8616e0fc 481 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
571d5972 482 seq_puts(s, ",serverino");
d4ffff1f 483 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
571d5972 484 seq_puts(s, ",rwpidforward");
d4ffff1f 485 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
571d5972 486 seq_puts(s, ",forcemand");
8616e0fc 487 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
571d5972 488 seq_puts(s, ",nouser_xattr");
8616e0fc 489 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
571d5972 490 seq_puts(s, ",mapchars");
bc8ebdc4
NA
491 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
492 seq_puts(s, ",mapposix");
8616e0fc 493 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
571d5972 494 seq_puts(s, ",sfu");
8616e0fc 495 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
571d5972 496 seq_puts(s, ",nobrl");
8616e0fc 497 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
571d5972 498 seq_puts(s, ",cifsacl");
8616e0fc 499 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
571d5972 500 seq_puts(s, ",dynperm");
34c80b1d 501 if (root->d_sb->s_flags & MS_POSIXACL)
571d5972 502 seq_puts(s, ",acl");
736a3320 503 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
571d5972 504 seq_puts(s, ",mfsymlinks");
476428f8 505 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
571d5972 506 seq_puts(s, ",fsc");
71c424ba 507 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
571d5972 508 seq_puts(s, ",nostrictsync");
71c424ba 509 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
571d5972 510 seq_puts(s, ",noperm");
3c7c87fd 511 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID)
1f68233c
EB
512 seq_printf(s, ",backupuid=%u",
513 from_kuid_munged(&init_user_ns,
514 cifs_sb->mnt_backupuid));
3c7c87fd 515 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID)
1f68233c
EB
516 seq_printf(s, ",backupgid=%u",
517 from_kgid_munged(&init_user_ns,
518 cifs_sb->mnt_backupgid));
8616e0fc 519
28f88810
SP
520 seq_printf(s, ",rsize=%u", cifs_sb->rsize);
521 seq_printf(s, ",wsize=%u", cifs_sb->wsize);
adfeb3e0
SF
522 seq_printf(s, ",echo_interval=%lu",
523 tcon->ses->server->echo_interval / HZ);
6d20e840 524 /* convert actimeo and display it in seconds */
156d1790 525 seq_printf(s, ",actimeo=%lu", cifs_sb->actimeo / HZ);
8616e0fc 526
1da177e4
LT
527 return 0;
528}
529
42faad99 530static void cifs_umount_begin(struct super_block *sb)
68058e75 531{
42faad99 532 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
96daf2b0 533 struct cifs_tcon *tcon;
68058e75 534
4523cc30 535 if (cifs_sb == NULL)
9e2e85f8
SF
536 return;
537
0d424ad0 538 tcon = cifs_sb_master_tcon(cifs_sb);
f1987b44 539
3f9bcca7 540 spin_lock(&cifs_tcp_ses_lock);
ad8034f1
SF
541 if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
542 /* we have other mounts to same share or we have
543 already tried to force umount this and woken up
544 all waiting network requests, nothing to do */
3f9bcca7 545 spin_unlock(&cifs_tcp_ses_lock);
ad8034f1
SF
546 return;
547 } else if (tcon->tc_count == 1)
5e1253b5 548 tcon->tidStatus = CifsExiting;
3f9bcca7 549 spin_unlock(&cifs_tcp_ses_lock);
5e1253b5 550
3a5ff61c 551 /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
7b7abfe3 552 /* cancel_notify_requests(tcon); */
50c2f753 553 if (tcon->ses && tcon->ses->server) {
f96637be 554 cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
9e2e85f8 555 wake_up_all(&tcon->ses->server->request_q);
6ab16d24
SF
556 wake_up_all(&tcon->ses->server->response_q);
557 msleep(1); /* yield */
558 /* we have to kick the requests once more */
559 wake_up_all(&tcon->ses->server->response_q);
560 msleep(1);
5e1253b5 561 }
68058e75
SF
562
563 return;
564}
68058e75 565
bf97d287 566#ifdef CONFIG_CIFS_STATS2
64132379 567static int cifs_show_stats(struct seq_file *s, struct dentry *root)
bf97d287
SF
568{
569 /* BB FIXME */
570 return 0;
571}
572#endif
573
1da177e4
LT
574static int cifs_remount(struct super_block *sb, int *flags, char *data)
575{
02b9984d 576 sync_filesystem(sb);
1da177e4
LT
577 *flags |= MS_NODIRATIME;
578 return 0;
579}
580
45321ac5 581static int cifs_drop_inode(struct inode *inode)
12420ac3
JL
582{
583 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
584
45321ac5
AV
585 /* no serverino => unconditional eviction */
586 return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
587 generic_drop_inode(inode);
12420ac3
JL
588}
589
ee9b6d61 590static const struct super_operations cifs_super_ops = {
1da177e4
LT
591 .statfs = cifs_statfs,
592 .alloc_inode = cifs_alloc_inode,
593 .destroy_inode = cifs_destroy_inode,
12420ac3 594 .drop_inode = cifs_drop_inode,
b57922d9 595 .evict_inode = cifs_evict_inode,
12420ac3
JL
596/* .delete_inode = cifs_delete_inode, */ /* Do not need above
597 function unless later we add lazy close of inodes or unless the
50c2f753
SF
598 kernel forgets to call us with the same number of releases (closes)
599 as opens */
1da177e4 600 .show_options = cifs_show_options,
7b7abfe3 601 .umount_begin = cifs_umount_begin,
1da177e4 602 .remount_fs = cifs_remount,
bf97d287 603#ifdef CONFIG_CIFS_STATS2
f46d3e11 604 .show_stats = cifs_show_stats,
bf97d287 605#endif
1da177e4
LT
606};
607
f87d39d9
SF
608/*
609 * Get root dentry from superblock according to prefix path mount option.
610 * Return dentry with refcount + 1 on success and NULL otherwise.
611 */
612static struct dentry *
613cifs_get_root(struct smb_vol *vol, struct super_block *sb)
614{
fec11dd9 615 struct dentry *dentry;
f87d39d9 616 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
fec11dd9
AV
617 char *full_path = NULL;
618 char *s, *p;
f87d39d9
SF
619 char sep;
620
348c1bfa
SP
621 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
622 return dget(sb->s_root);
623
6d3ea7e4 624 full_path = cifs_build_path_to_root(vol, cifs_sb,
374402a2 625 cifs_sb_master_tcon(cifs_sb), 0);
f87d39d9 626 if (full_path == NULL)
9403c9c5 627 return ERR_PTR(-ENOMEM);
f87d39d9 628
f96637be 629 cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
f87d39d9 630
f87d39d9 631 sep = CIFS_DIR_SEP(cifs_sb);
fec11dd9
AV
632 dentry = dget(sb->s_root);
633 p = s = full_path;
634
635 do {
2b0143b5 636 struct inode *dir = d_inode(dentry);
fec11dd9
AV
637 struct dentry *child;
638
5b980b01
PS
639 if (!dir) {
640 dput(dentry);
641 dentry = ERR_PTR(-ENOENT);
642 break;
643 }
ce2ac521
JL
644 if (!S_ISDIR(dir->i_mode)) {
645 dput(dentry);
646 dentry = ERR_PTR(-ENOTDIR);
647 break;
648 }
5b980b01 649
fec11dd9
AV
650 /* skip separators */
651 while (*s == sep)
652 s++;
653 if (!*s)
654 break;
655 p = s++;
656 /* next separator */
657 while (*s && *s != sep)
658 s++;
659
85f40482 660 child = lookup_one_len_unlocked(p, dentry, s - p);
fec11dd9
AV
661 dput(dentry);
662 dentry = child;
663 } while (!IS_ERR(dentry));
f87d39d9 664 kfree(full_path);
fec11dd9 665 return dentry;
f87d39d9
SF
666}
667
ee01a14d
AV
668static int cifs_set_super(struct super_block *sb, void *data)
669{
670 struct cifs_mnt_data *mnt_data = data;
671 sb->s_fs_info = mnt_data->cifs_sb;
672 return set_anon_super(sb, NULL);
673}
674
d753ed97
AV
675static struct dentry *
676cifs_do_mount(struct file_system_type *fs_type,
724d9f1c 677 int flags, const char *dev_name, void *data)
1da177e4
LT
678{
679 int rc;
db719222 680 struct super_block *sb;
724d9f1c
PS
681 struct cifs_sb_info *cifs_sb;
682 struct smb_vol *volume_info;
25c7f41e 683 struct cifs_mnt_data mnt_data;
724d9f1c 684 struct dentry *root;
1da177e4 685
f96637be 686 cifs_dbg(FYI, "Devname: %s flags: %d\n", dev_name, flags);
1da177e4 687
04db79b0
JL
688 volume_info = cifs_get_volume_info((char *)data, dev_name);
689 if (IS_ERR(volume_info))
690 return ERR_CAST(volume_info);
724d9f1c
PS
691
692 cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
693 if (cifs_sb == NULL) {
694 root = ERR_PTR(-ENOMEM);
5c4f1ad7 695 goto out_nls;
724d9f1c
PS
696 }
697
5d3bc605
AV
698 cifs_sb->mountdata = kstrndup(data, PAGE_SIZE, GFP_KERNEL);
699 if (cifs_sb->mountdata == NULL) {
700 root = ERR_PTR(-ENOMEM);
4214ebf4 701 goto out_free;
5d3bc605
AV
702 }
703
4214ebf4
SP
704 rc = cifs_setup_cifs_sb(volume_info, cifs_sb);
705 if (rc) {
706 root = ERR_PTR(rc);
707 goto out_free;
a6b5058f
AA
708 }
709
97d1152a
AV
710 rc = cifs_mount(cifs_sb, volume_info);
711 if (rc) {
712 if (!(flags & MS_SILENT))
f96637be
JP
713 cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
714 rc);
97d1152a 715 root = ERR_PTR(rc);
4214ebf4 716 goto out_free;
97d1152a
AV
717 }
718
25c7f41e
PS
719 mnt_data.vol = volume_info;
720 mnt_data.cifs_sb = cifs_sb;
721 mnt_data.flags = flags;
722
9249e17f
DH
723 /* BB should we make this contingent on mount parm? */
724 flags |= MS_NODIRATIME | MS_NOATIME;
725
726 sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
724d9f1c 727 if (IS_ERR(sb)) {
724d9f1c 728 root = ERR_CAST(sb);
97d1152a 729 cifs_umount(cifs_sb);
d757d71b 730 goto out;
724d9f1c 731 }
1da177e4 732
ee01a14d 733 if (sb->s_root) {
f96637be 734 cifs_dbg(FYI, "Use existing superblock\n");
97d1152a 735 cifs_umount(cifs_sb);
5c4f1ad7 736 } else {
5c4f1ad7
AV
737 rc = cifs_read_super(sb);
738 if (rc) {
739 root = ERR_PTR(rc);
740 goto out_super;
741 }
b2e5cd33 742
5c4f1ad7 743 sb->s_flags |= MS_ACTIVE;
1da177e4 744 }
724d9f1c 745
348c1bfa 746 root = cifs_get_root(volume_info, sb);
9403c9c5 747 if (IS_ERR(root))
f87d39d9 748 goto out_super;
25c7f41e 749
f96637be 750 cifs_dbg(FYI, "dentry root is: %p\n", root);
641a58d6 751 goto out;
724d9f1c 752
641a58d6 753out_super:
641a58d6 754 deactivate_locked_super(sb);
641a58d6 755out:
f9e59bcb 756 cifs_cleanup_volume_info(volume_info);
724d9f1c 757 return root;
5c4f1ad7 758
4214ebf4
SP
759out_free:
760 kfree(cifs_sb->prepath);
5c4f1ad7 761 kfree(cifs_sb->mountdata);
5c4f1ad7
AV
762 kfree(cifs_sb);
763out_nls:
764 unload_nls(volume_info->local_nls);
765 goto out;
1da177e4
LT
766}
767
08bc0353
JL
768static ssize_t
769cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
770{
771 ssize_t rc;
772 struct inode *inode = file_inode(iocb->ki_filp);
773
882137c4
RL
774 if (iocb->ki_filp->f_flags & O_DIRECT)
775 return cifs_user_readv(iocb, iter);
776
08bc0353
JL
777 rc = cifs_revalidate_mapping(inode);
778 if (rc)
779 return rc;
780
781 return generic_file_read_iter(iocb, iter);
782}
783
3dae8750 784static ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1da177e4 785{
496ad9aa 786 struct inode *inode = file_inode(iocb->ki_filp);
c11f1df5 787 struct cifsInodeInfo *cinode = CIFS_I(inode);
1da177e4 788 ssize_t written;
72432ffc 789 int rc;
1da177e4 790
882137c4
RL
791 if (iocb->ki_filp->f_flags & O_DIRECT) {
792 written = cifs_user_writev(iocb, from);
793 if (written > 0 && CIFS_CACHE_READ(cinode)) {
794 cifs_zap_mapping(inode);
795 cifs_dbg(FYI,
796 "Set no oplock for inode=%p after a write operation\n",
797 inode);
798 cinode->oplock = 0;
799 }
800 return written;
801 }
802
c11f1df5
SP
803 written = cifs_get_writer(cinode);
804 if (written)
805 return written;
806
3dae8750 807 written = generic_file_write_iter(iocb, from);
72432ffc 808
18cceb6a 809 if (CIFS_CACHE_WRITE(CIFS_I(inode)))
c11f1df5 810 goto out;
72432ffc
PS
811
812 rc = filemap_fdatawrite(inode->i_mapping);
813 if (rc)
3dae8750 814 cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
f96637be 815 rc, inode);
72432ffc 816
c11f1df5
SP
817out:
818 cifs_put_writer(cinode);
1da177e4
LT
819 return written;
820}
821
965c8e59 822static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
c32a0b68 823{
06222e49 824 /*
965c8e59 825 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
06222e49
JB
826 * the cached file length
827 */
965c8e59 828 if (whence != SEEK_SET && whence != SEEK_CUR) {
6feb9891 829 int rc;
496ad9aa 830 struct inode *inode = file_inode(file);
6feb9891
PS
831
832 /*
833 * We need to be sure that all dirty pages are written and the
834 * server has the newest file length.
835 */
18cceb6a 836 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
6feb9891
PS
837 inode->i_mapping->nrpages != 0) {
838 rc = filemap_fdatawait(inode->i_mapping);
156ecb2d
SF
839 if (rc) {
840 mapping_set_error(inode->i_mapping, rc);
841 return rc;
842 }
6feb9891
PS
843 }
844 /*
845 * Some applications poll for the file length in this strange
846 * way so we must seek to end on non-oplocked files by
847 * setting the revalidate time to zero.
848 */
849 CIFS_I(inode)->time = 0;
850
851 rc = cifs_revalidate_file_attr(file);
852 if (rc < 0)
853 return (loff_t)rc;
c32a0b68 854 }
965c8e59 855 return generic_file_llseek(file, offset, whence);
c32a0b68
SF
856}
857
e6f5c789
JL
858static int
859cifs_setlease(struct file *file, long arg, struct file_lock **lease, void **priv)
84210e91 860{
18cceb6a
PS
861 /*
862 * Note that this is called by vfs setlease with i_lock held to
863 * protect *lease from going away.
864 */
496ad9aa 865 struct inode *inode = file_inode(file);
ba00ba64 866 struct cifsFileInfo *cfile = file->private_data;
84210e91
SF
867
868 if (!(S_ISREG(inode->i_mode)))
869 return -EINVAL;
870
02440806
JL
871 /* Check if file is oplocked if this is request for new lease */
872 if (arg == F_UNLCK ||
873 ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) ||
18cceb6a 874 ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
e6f5c789 875 return generic_setlease(file, arg, lease, priv);
13cfb733 876 else if (tlink_tcon(cfile->tlink)->local_lease &&
18cceb6a
PS
877 !CIFS_CACHE_READ(CIFS_I(inode)))
878 /*
879 * If the server claims to support oplock on this file, then we
880 * still need to check oplock even if the local_lease mount
881 * option is set, but there are servers which do not support
882 * oplock for which this mount option may be useful if the user
883 * knows that the file won't be changed on the server by anyone
884 * else.
885 */
e6f5c789 886 return generic_setlease(file, arg, lease, priv);
51ee4b84 887 else
84210e91
SF
888 return -EAGAIN;
889}
84210e91 890
e6ab1582 891struct file_system_type cifs_fs_type = {
1da177e4
LT
892 .owner = THIS_MODULE,
893 .name = "cifs",
d753ed97 894 .mount = cifs_do_mount,
6d686175 895 .kill_sb = cifs_kill_sb,
1da177e4
LT
896 /* .fs_flags */
897};
3e64fe5b 898MODULE_ALIAS_FS("cifs");
754661f1 899const struct inode_operations cifs_dir_inode_ops = {
1da177e4 900 .create = cifs_create,
d2c12719 901 .atomic_open = cifs_atomic_open,
1da177e4
LT
902 .lookup = cifs_lookup,
903 .getattr = cifs_getattr,
904 .unlink = cifs_unlink,
905 .link = cifs_hardlink,
906 .mkdir = cifs_mkdir,
907 .rmdir = cifs_rmdir,
2773bf00 908 .rename = cifs_rename2,
1da177e4 909 .permission = cifs_permission,
1da177e4
LT
910 .setattr = cifs_setattr,
911 .symlink = cifs_symlink,
912 .mknod = cifs_mknod,
1da177e4 913 .listxattr = cifs_listxattr,
1da177e4
LT
914};
915
754661f1 916const struct inode_operations cifs_file_inode_ops = {
1da177e4 917 .setattr = cifs_setattr,
48a77aa7 918 .getattr = cifs_getattr,
1da177e4 919 .permission = cifs_permission,
1da177e4 920 .listxattr = cifs_listxattr,
1da177e4
LT
921};
922
754661f1 923const struct inode_operations cifs_symlink_inode_ops = {
6b255391 924 .get_link = cifs_get_link,
1da177e4 925 .permission = cifs_permission,
1da177e4 926 .listxattr = cifs_listxattr,
1da177e4
LT
927};
928
04b38d60
CH
929static int cifs_clone_file_range(struct file *src_file, loff_t off,
930 struct file *dst_file, loff_t destoff, u64 len)
931{
932 struct inode *src_inode = file_inode(src_file);
933 struct inode *target_inode = file_inode(dst_file);
934 struct cifsFileInfo *smb_file_src = src_file->private_data;
935 struct cifsFileInfo *smb_file_target = dst_file->private_data;
04b38d60
CH
936 struct cifs_tcon *target_tcon = tlink_tcon(smb_file_target->tlink);
937 unsigned int xid;
938 int rc;
939
940 cifs_dbg(FYI, "clone range\n");
941
942 xid = get_xid();
943
944 if (!src_file->private_data || !dst_file->private_data) {
945 rc = -EBADF;
946 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
947 goto out;
948 }
949
950 /*
951 * Note: cifs case is easier than btrfs since server responsible for
952 * checks for proper open modes and file type and if it wants
953 * server could even support copy of range where source = target
954 */
955 lock_two_nondirectories(target_inode, src_inode);
956
957 if (len == 0)
958 len = src_inode->i_size - off;
959
960 cifs_dbg(FYI, "about to flush pages\n");
961 /* should we flush first and last page first */
962 truncate_inode_pages_range(&target_inode->i_data, destoff,
09cbfeaf 963 PAGE_ALIGN(destoff + len)-1);
04b38d60
CH
964
965 if (target_tcon->ses->server->ops->duplicate_extents)
966 rc = target_tcon->ses->server->ops->duplicate_extents(xid,
967 smb_file_src, smb_file_target, off, len, destoff);
968 else
969 rc = -EOPNOTSUPP;
970
971 /* force revalidate of size and timestamps of target file now
972 that target is updated on the server */
973 CIFS_I(target_inode)->time = 0;
04b38d60
CH
974 /* although unlocking in the reverse order from locking is not
975 strictly necessary here it is a little cleaner to be consistent */
976 unlock_two_nondirectories(src_inode, target_inode);
977out:
978 free_xid(xid);
979 return rc;
980}
981
620d8745
SP
982ssize_t cifs_file_copychunk_range(unsigned int xid,
983 struct file *src_file, loff_t off,
984 struct file *dst_file, loff_t destoff,
985 size_t len, unsigned int flags)
986{
987 struct inode *src_inode = file_inode(src_file);
988 struct inode *target_inode = file_inode(dst_file);
989 struct cifsFileInfo *smb_file_src;
990 struct cifsFileInfo *smb_file_target;
991 struct cifs_tcon *src_tcon;
992 struct cifs_tcon *target_tcon;
993 ssize_t rc;
994
995 cifs_dbg(FYI, "copychunk range\n");
996
997 if (src_inode == target_inode) {
998 rc = -EINVAL;
999 goto out;
1000 }
1001
1002 if (!src_file->private_data || !dst_file->private_data) {
1003 rc = -EBADF;
1004 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
1005 goto out;
1006 }
1007
1008 rc = -EXDEV;
1009 smb_file_target = dst_file->private_data;
1010 smb_file_src = src_file->private_data;
1011 src_tcon = tlink_tcon(smb_file_src->tlink);
1012 target_tcon = tlink_tcon(smb_file_target->tlink);
1013
1014 if (src_tcon->ses != target_tcon->ses) {
1015 cifs_dbg(VFS, "source and target of copy not on same server\n");
1016 goto out;
1017 }
1018
1019 /*
1020 * Note: cifs case is easier than btrfs since server responsible for
1021 * checks for proper open modes and file type and if it wants
1022 * server could even support copy of range where source = target
1023 */
1024 lock_two_nondirectories(target_inode, src_inode);
1025
1026 cifs_dbg(FYI, "about to flush pages\n");
1027 /* should we flush first and last page first */
1028 truncate_inode_pages(&target_inode->i_data, 0);
1029
1030 if (target_tcon->ses->server->ops->copychunk_range)
1031 rc = target_tcon->ses->server->ops->copychunk_range(xid,
1032 smb_file_src, smb_file_target, off, len, destoff);
1033 else
1034 rc = -EOPNOTSUPP;
1035
1036 /* force revalidate of size and timestamps of target file now
1037 * that target is updated on the server
1038 */
1039 CIFS_I(target_inode)->time = 0;
1040 /* although unlocking in the reverse order from locking is not
1041 * strictly necessary here it is a little cleaner to be consistent
1042 */
1043 unlock_two_nondirectories(src_inode, target_inode);
1044
1045out:
1046 return rc;
1047}
1048
1049static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off,
1050 struct file *dst_file, loff_t destoff,
1051 size_t len, unsigned int flags)
1052{
1053 unsigned int xid = get_xid();
1054 ssize_t rc;
1055
1056 rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
1057 len, flags);
1058 free_xid(xid);
1059 return rc;
1060}
1061
4b6f5d20 1062const struct file_operations cifs_file_ops = {
08bc0353 1063 .read_iter = cifs_loose_read_iter,
3dae8750 1064 .write_iter = cifs_file_write_iter,
1da177e4
LT
1065 .open = cifs_open,
1066 .release = cifs_close,
1067 .lock = cifs_lock,
1068 .fsync = cifs_fsync,
1069 .flush = cifs_flush,
1070 .mmap = cifs_file_mmap,
5ffc4ef4 1071 .splice_read = generic_file_splice_read,
c32a0b68 1072 .llseek = cifs_llseek,
f9ddcca4 1073 .unlocked_ioctl = cifs_ioctl,
620d8745 1074 .copy_file_range = cifs_copy_file_range,
04b38d60 1075 .clone_file_range = cifs_clone_file_range,
84210e91 1076 .setlease = cifs_setlease,
31742c5a 1077 .fallocate = cifs_fallocate,
1da177e4
LT
1078};
1079
8be7e6ba 1080const struct file_operations cifs_file_strict_ops = {
e6a7bcb4 1081 .read_iter = cifs_strict_readv,
3dae8750 1082 .write_iter = cifs_strict_writev,
8be7e6ba
PS
1083 .open = cifs_open,
1084 .release = cifs_close,
1085 .lock = cifs_lock,
1086 .fsync = cifs_strict_fsync,
1087 .flush = cifs_flush,
7a6a19b1 1088 .mmap = cifs_file_strict_mmap,
8be7e6ba
PS
1089 .splice_read = generic_file_splice_read,
1090 .llseek = cifs_llseek,
8be7e6ba 1091 .unlocked_ioctl = cifs_ioctl,
620d8745 1092 .copy_file_range = cifs_copy_file_range,
04b38d60 1093 .clone_file_range = cifs_clone_file_range,
8be7e6ba 1094 .setlease = cifs_setlease,
31742c5a 1095 .fallocate = cifs_fallocate,
8be7e6ba
PS
1096};
1097
4b6f5d20 1098const struct file_operations cifs_file_direct_ops = {
0b81c1c4 1099 /* BB reevaluate whether they can be done with directio, no cache */
e6a7bcb4 1100 .read_iter = cifs_user_readv,
3dae8750 1101 .write_iter = cifs_user_writev,
1da177e4
LT
1102 .open = cifs_open,
1103 .release = cifs_close,
1104 .lock = cifs_lock,
1105 .fsync = cifs_fsync,
1106 .flush = cifs_flush,
a994b8fa 1107 .mmap = cifs_file_mmap,
5ffc4ef4 1108 .splice_read = generic_file_splice_read,
f9ddcca4 1109 .unlocked_ioctl = cifs_ioctl,
620d8745 1110 .copy_file_range = cifs_copy_file_range,
04b38d60 1111 .clone_file_range = cifs_clone_file_range,
c32a0b68 1112 .llseek = cifs_llseek,
84210e91 1113 .setlease = cifs_setlease,
31742c5a 1114 .fallocate = cifs_fallocate,
1da177e4 1115};
8be7e6ba 1116
4b6f5d20 1117const struct file_operations cifs_file_nobrl_ops = {
08bc0353 1118 .read_iter = cifs_loose_read_iter,
3dae8750 1119 .write_iter = cifs_file_write_iter,
87c89dd7
SF
1120 .open = cifs_open,
1121 .release = cifs_close,
1122 .fsync = cifs_fsync,
1123 .flush = cifs_flush,
1124 .mmap = cifs_file_mmap,
5ffc4ef4 1125 .splice_read = generic_file_splice_read,
c32a0b68 1126 .llseek = cifs_llseek,
f9ddcca4 1127 .unlocked_ioctl = cifs_ioctl,
620d8745 1128 .copy_file_range = cifs_copy_file_range,
04b38d60 1129 .clone_file_range = cifs_clone_file_range,
84210e91 1130 .setlease = cifs_setlease,
31742c5a 1131 .fallocate = cifs_fallocate,
8b94bcb9
SF
1132};
1133
8be7e6ba 1134const struct file_operations cifs_file_strict_nobrl_ops = {
e6a7bcb4 1135 .read_iter = cifs_strict_readv,
3dae8750 1136 .write_iter = cifs_strict_writev,
8be7e6ba
PS
1137 .open = cifs_open,
1138 .release = cifs_close,
1139 .fsync = cifs_strict_fsync,
1140 .flush = cifs_flush,
7a6a19b1 1141 .mmap = cifs_file_strict_mmap,
8be7e6ba
PS
1142 .splice_read = generic_file_splice_read,
1143 .llseek = cifs_llseek,
8be7e6ba 1144 .unlocked_ioctl = cifs_ioctl,
620d8745 1145 .copy_file_range = cifs_copy_file_range,
04b38d60 1146 .clone_file_range = cifs_clone_file_range,
8be7e6ba 1147 .setlease = cifs_setlease,
31742c5a 1148 .fallocate = cifs_fallocate,
8be7e6ba
PS
1149};
1150
4b6f5d20 1151const struct file_operations cifs_file_direct_nobrl_ops = {
0b81c1c4 1152 /* BB reevaluate whether they can be done with directio, no cache */
e6a7bcb4 1153 .read_iter = cifs_user_readv,
3dae8750 1154 .write_iter = cifs_user_writev,
87c89dd7
SF
1155 .open = cifs_open,
1156 .release = cifs_close,
1157 .fsync = cifs_fsync,
1158 .flush = cifs_flush,
810627a0 1159 .mmap = cifs_file_mmap,
5ffc4ef4 1160 .splice_read = generic_file_splice_read,
f9ddcca4 1161 .unlocked_ioctl = cifs_ioctl,
620d8745 1162 .copy_file_range = cifs_copy_file_range,
04b38d60 1163 .clone_file_range = cifs_clone_file_range,
c32a0b68 1164 .llseek = cifs_llseek,
84210e91 1165 .setlease = cifs_setlease,
31742c5a 1166 .fallocate = cifs_fallocate,
8b94bcb9 1167};
1da177e4 1168
4b6f5d20 1169const struct file_operations cifs_dir_ops = {
3125d265 1170 .iterate_shared = cifs_readdir,
1da177e4
LT
1171 .release = cifs_closedir,
1172 .read = generic_read_dir,
f9ddcca4 1173 .unlocked_ioctl = cifs_ioctl,
620d8745 1174 .copy_file_range = cifs_copy_file_range,
04b38d60 1175 .clone_file_range = cifs_clone_file_range,
3222a3e5 1176 .llseek = generic_file_llseek,
1da177e4
LT
1177};
1178
1179static void
51cc5068 1180cifs_init_once(void *inode)
1da177e4
LT
1181{
1182 struct cifsInodeInfo *cifsi = inode;
1183
a35afb83 1184 inode_init_once(&cifsi->vfs_inode);
1b4b55a1 1185 init_rwsem(&cifsi->lock_sem);
1da177e4
LT
1186}
1187
9ee108b2 1188static int __init
1da177e4
LT
1189cifs_init_inodecache(void)
1190{
1191 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
26f57364 1192 sizeof(struct cifsInodeInfo),
fffb60f9 1193 0, (SLAB_RECLAIM_ACCOUNT|
5d097056 1194 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
20c2df83 1195 cifs_init_once);
1da177e4
LT
1196 if (cifs_inode_cachep == NULL)
1197 return -ENOMEM;
1198
1199 return 0;
1200}
1201
1202static void
1203cifs_destroy_inodecache(void)
1204{
8c0a8537
KS
1205 /*
1206 * Make sure all delayed rcu free inodes are flushed before we
1207 * destroy cache.
1208 */
1209 rcu_barrier();
1a1d92c1 1210 kmem_cache_destroy(cifs_inode_cachep);
1da177e4
LT
1211}
1212
1213static int
1214cifs_init_request_bufs(void)
1215{
3792c173
PS
1216 size_t max_hdr_size = MAX_CIFS_HDR_SIZE;
1217#ifdef CONFIG_CIFS_SMB2
1218 /*
1219 * SMB2 maximum header size is bigger than CIFS one - no problems to
1220 * allocate some more bytes for CIFS.
1221 */
1222 max_hdr_size = MAX_SMB2_HDR_SIZE;
1223#endif
4523cc30 1224 if (CIFSMaxBufSize < 8192) {
1da177e4
LT
1225 /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
1226 Unicode path name has to fit in any SMB/CIFS path based frames */
1227 CIFSMaxBufSize = 8192;
1228 } else if (CIFSMaxBufSize > 1024*127) {
1229 CIFSMaxBufSize = 1024 * 127;
1230 } else {
1231 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
1232 }
f96637be
JP
1233/*
1234 cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
1235 CIFSMaxBufSize, CIFSMaxBufSize);
1236*/
1da177e4 1237 cifs_req_cachep = kmem_cache_create("cifs_request",
3792c173 1238 CIFSMaxBufSize + max_hdr_size, 0,
20c2df83 1239 SLAB_HWCACHE_ALIGN, NULL);
1da177e4
LT
1240 if (cifs_req_cachep == NULL)
1241 return -ENOMEM;
1242
4523cc30 1243 if (cifs_min_rcv < 1)
1da177e4
LT
1244 cifs_min_rcv = 1;
1245 else if (cifs_min_rcv > 64) {
1246 cifs_min_rcv = 64;
f96637be 1247 cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
1da177e4
LT
1248 }
1249
93d2341c
MD
1250 cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1251 cifs_req_cachep);
1da177e4 1252
4523cc30 1253 if (cifs_req_poolp == NULL) {
1da177e4
LT
1254 kmem_cache_destroy(cifs_req_cachep);
1255 return -ENOMEM;
1256 }
ec637e3f 1257 /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1da177e4
LT
1258 almost all handle based requests (but not write response, nor is it
1259 sufficient for path based requests). A smaller size would have
50c2f753 1260 been more efficient (compacting multiple slab items on one 4k page)
1da177e4
LT
1261 for the case in which debug was on, but this larger size allows
1262 more SMBs to use small buffer alloc and is still much more
6dc0f87e 1263 efficient to alloc 1 per page off the slab compared to 17K (5page)
1da177e4
LT
1264 alloc of large cifs buffers even when page debugging is on */
1265 cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
6dc0f87e 1266 MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
20c2df83 1267 NULL);
1da177e4
LT
1268 if (cifs_sm_req_cachep == NULL) {
1269 mempool_destroy(cifs_req_poolp);
1270 kmem_cache_destroy(cifs_req_cachep);
6dc0f87e 1271 return -ENOMEM;
1da177e4
LT
1272 }
1273
4523cc30 1274 if (cifs_min_small < 2)
1da177e4
LT
1275 cifs_min_small = 2;
1276 else if (cifs_min_small > 256) {
1277 cifs_min_small = 256;
f96637be 1278 cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
1da177e4
LT
1279 }
1280
93d2341c
MD
1281 cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1282 cifs_sm_req_cachep);
1da177e4 1283
4523cc30 1284 if (cifs_sm_req_poolp == NULL) {
1da177e4
LT
1285 mempool_destroy(cifs_req_poolp);
1286 kmem_cache_destroy(cifs_req_cachep);
1287 kmem_cache_destroy(cifs_sm_req_cachep);
1288 return -ENOMEM;
1289 }
1290
1291 return 0;
1292}
1293
1294static void
1295cifs_destroy_request_bufs(void)
1296{
1297 mempool_destroy(cifs_req_poolp);
1a1d92c1 1298 kmem_cache_destroy(cifs_req_cachep);
1da177e4 1299 mempool_destroy(cifs_sm_req_poolp);
1a1d92c1 1300 kmem_cache_destroy(cifs_sm_req_cachep);
1da177e4
LT
1301}
1302
1303static int
1304cifs_init_mids(void)
1305{
1306 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
26f57364
SF
1307 sizeof(struct mid_q_entry), 0,
1308 SLAB_HWCACHE_ALIGN, NULL);
1da177e4
LT
1309 if (cifs_mid_cachep == NULL)
1310 return -ENOMEM;
1311
93d2341c
MD
1312 /* 3 is a reasonable minimum number of simultaneous operations */
1313 cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
4523cc30 1314 if (cifs_mid_poolp == NULL) {
1da177e4
LT
1315 kmem_cache_destroy(cifs_mid_cachep);
1316 return -ENOMEM;
1317 }
1318
1da177e4
LT
1319 return 0;
1320}
1321
1322static void
1323cifs_destroy_mids(void)
1324{
1325 mempool_destroy(cifs_mid_poolp);
1a1d92c1 1326 kmem_cache_destroy(cifs_mid_cachep);
1da177e4
LT
1327}
1328
1da177e4
LT
1329static int __init
1330init_cifs(void)
1331{
1332 int rc = 0;
1da177e4 1333 cifs_proc_init();
e7ddee90 1334 INIT_LIST_HEAD(&cifs_tcp_ses_list);
0eff0e26 1335#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* unused temporarily */
4ca9c190
SF
1336 INIT_LIST_HEAD(&GlobalDnotifyReqList);
1337 INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
0eff0e26 1338#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */
1da177e4
LT
1339/*
1340 * Initialize Global counters
1341 */
1342 atomic_set(&sesInfoAllocCount, 0);
1343 atomic_set(&tconInfoAllocCount, 0);
6dc0f87e 1344 atomic_set(&tcpSesAllocCount, 0);
1da177e4
LT
1345 atomic_set(&tcpSesReconnectCount, 0);
1346 atomic_set(&tconInfoReconnectCount, 0);
1347
1348 atomic_set(&bufAllocCount, 0);
4498eed5
SF
1349 atomic_set(&smBufAllocCount, 0);
1350#ifdef CONFIG_CIFS_STATS2
1351 atomic_set(&totBufAllocCount, 0);
1352 atomic_set(&totSmBufAllocCount, 0);
1353#endif /* CONFIG_CIFS_STATS2 */
1354
1da177e4
LT
1355 atomic_set(&midCount, 0);
1356 GlobalCurrentXid = 0;
1357 GlobalTotalActiveXid = 0;
1358 GlobalMaxActiveXid = 0;
3f9bcca7 1359 spin_lock_init(&cifs_tcp_ses_lock);
1da177e4
LT
1360 spin_lock_init(&GlobalMid_Lock);
1361
3d22462a
JL
1362 get_random_bytes(&cifs_lock_secret, sizeof(cifs_lock_secret));
1363
4523cc30 1364 if (cifs_max_pending < 2) {
1da177e4 1365 cifs_max_pending = 2;
f96637be 1366 cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
10b9b98e
PS
1367 } else if (cifs_max_pending > CIFS_MAX_REQ) {
1368 cifs_max_pending = CIFS_MAX_REQ;
f96637be
JP
1369 cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
1370 CIFS_MAX_REQ);
1da177e4
LT
1371 }
1372
da472fc8
JL
1373 cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1374 if (!cifsiod_wq) {
1375 rc = -ENOMEM;
1376 goto out_clean_proc;
1377 }
1378
3998e6b8
RV
1379 cifsoplockd_wq = alloc_workqueue("cifsoplockd",
1380 WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1381 if (!cifsoplockd_wq) {
1382 rc = -ENOMEM;
1383 goto out_destroy_cifsiod_wq;
1384 }
1385
f579cf3c
SJ
1386 rc = cifs_fscache_register();
1387 if (rc)
3998e6b8 1388 goto out_destroy_cifsoplockd_wq;
f579cf3c 1389
1da177e4 1390 rc = cifs_init_inodecache();
45af7a0f 1391 if (rc)
d3bf5221 1392 goto out_unreg_fscache;
45af7a0f
SF
1393
1394 rc = cifs_init_mids();
1395 if (rc)
1396 goto out_destroy_inodecache;
1397
1398 rc = cifs_init_request_bufs();
1399 if (rc)
1400 goto out_destroy_mids;
1401
84a15b93 1402#ifdef CONFIG_CIFS_UPCALL
b74cb9a8 1403 rc = init_cifs_spnego();
84a15b93 1404 if (rc)
4d79dba0
SP
1405 goto out_destroy_request_bufs;
1406#endif /* CONFIG_CIFS_UPCALL */
1407
1408#ifdef CONFIG_CIFS_ACL
1409 rc = init_cifs_idmap();
1410 if (rc)
c4aca0c0 1411 goto out_register_key_type;
4d79dba0
SP
1412#endif /* CONFIG_CIFS_ACL */
1413
1414 rc = register_filesystem(&cifs_fs_type);
1415 if (rc)
c4aca0c0 1416 goto out_init_cifs_idmap;
45af7a0f 1417
45af7a0f
SF
1418 return 0;
1419
c4aca0c0 1420out_init_cifs_idmap:
4d79dba0
SP
1421#ifdef CONFIG_CIFS_ACL
1422 exit_cifs_idmap();
c4aca0c0 1423out_register_key_type:
4d79dba0 1424#endif
84a15b93 1425#ifdef CONFIG_CIFS_UPCALL
b74cb9a8 1426 exit_cifs_spnego();
c4aca0c0 1427out_destroy_request_bufs:
1fc7995d 1428#endif
45af7a0f 1429 cifs_destroy_request_bufs();
d3bf5221 1430out_destroy_mids:
45af7a0f 1431 cifs_destroy_mids();
d3bf5221 1432out_destroy_inodecache:
45af7a0f 1433 cifs_destroy_inodecache();
d3bf5221 1434out_unreg_fscache:
f579cf3c 1435 cifs_fscache_unregister();
3998e6b8
RV
1436out_destroy_cifsoplockd_wq:
1437 destroy_workqueue(cifsoplockd_wq);
1438out_destroy_cifsiod_wq:
da472fc8 1439 destroy_workqueue(cifsiod_wq);
d3bf5221
SF
1440out_clean_proc:
1441 cifs_proc_clean();
1da177e4
LT
1442 return rc;
1443}
1444
1445static void __exit
1446exit_cifs(void)
1447{
f96637be 1448 cifs_dbg(NOISY, "exit_cifs\n");
3dd93306 1449 unregister_filesystem(&cifs_fs_type);
78d31a3a 1450 cifs_dfs_release_automount_timer();
4d79dba0 1451#ifdef CONFIG_CIFS_ACL
4d79dba0
SP
1452 exit_cifs_idmap();
1453#endif
84a15b93
JL
1454#ifdef CONFIG_CIFS_UPCALL
1455 unregister_key_type(&cifs_spnego_key_type);
1da177e4 1456#endif
1da177e4 1457 cifs_destroy_request_bufs();
3dd93306
JL
1458 cifs_destroy_mids();
1459 cifs_destroy_inodecache();
1460 cifs_fscache_unregister();
3998e6b8 1461 destroy_workqueue(cifsoplockd_wq);
da472fc8 1462 destroy_workqueue(cifsiod_wq);
3dd93306 1463 cifs_proc_clean();
1da177e4
LT
1464}
1465
1466MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
6dc0f87e 1467MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
1da177e4 1468MODULE_DESCRIPTION
63135e08
SF
1469 ("VFS to access servers complying with the SNIA CIFS Specification "
1470 "e.g. Samba and Windows");
1da177e4 1471MODULE_VERSION(CIFS_VERSION);
b9be76d5
JD
1472MODULE_SOFTDEP("pre: arc4");
1473MODULE_SOFTDEP("pre: des");
1474MODULE_SOFTDEP("pre: ecb");
1475MODULE_SOFTDEP("pre: hmac");
1476MODULE_SOFTDEP("pre: md4");
1477MODULE_SOFTDEP("pre: md5");
1478MODULE_SOFTDEP("pre: nls");
1479#ifdef CONFIG_CIFS_SMB2
1480MODULE_SOFTDEP("pre: aes");
1481MODULE_SOFTDEP("pre: cmac");
1482MODULE_SOFTDEP("pre: sha256");
026e93dc
PS
1483MODULE_SOFTDEP("pre: aead2");
1484MODULE_SOFTDEP("pre: ccm");
b9be76d5 1485#endif /* CONFIG_CIFS_SMB2 */
1da177e4
LT
1486module_init(init_cifs)
1487module_exit(exit_cifs)