4 * Copyright (C) International Business Machines Corp., 2002,2008
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Common Internet FileSystem (CIFS) client
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.
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.
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
24 /* Note that BB means BUGBUG (ie something to fix eventually) */
26 #include <linux/module.h>
28 #include <linux/mount.h>
29 #include <linux/slab.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/seq_file.h>
33 #include <linux/vfs.h>
34 #include <linux/mempool.h>
35 #include <linux/delay.h>
36 #include <linux/kthread.h>
37 #include <linux/freezer.h>
38 #include <linux/namei.h>
39 #include <linux/random.h>
40 #include <linux/uuid.h>
41 #include <linux/xattr.h>
45 #define DECLARE_GLOBALS_HERE
47 #include "cifsproto.h"
48 #include "cifs_debug.h"
49 #include "cifs_fs_sb.h"
51 #include <linux/key-type.h>
52 #include "cifs_spnego.h"
54 #ifdef CONFIG_CIFS_SMB2
60 bool enable_oplocks = true;
61 bool linuxExtEnabled = true;
62 bool lookupCacheEnabled = true;
63 unsigned int global_secflags = CIFSSEC_DEF;
64 /* unsigned int ntlmv2_support = 0; */
65 unsigned int sign_CIFS_PDUs = 1;
66 static const struct super_operations cifs_super_ops;
67 unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
68 module_param(CIFSMaxBufSize, uint, 0444);
69 MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header). "
70 "Default: 16384 Range: 8192 to 130048");
71 unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
72 module_param(cifs_min_rcv, uint, 0444);
73 MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
75 unsigned int cifs_min_small = 30;
76 module_param(cifs_min_small, uint, 0444);
77 MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
79 unsigned int cifs_max_pending = CIFS_MAX_REQ;
80 module_param(cifs_max_pending, uint, 0444);
81 MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server. "
82 "Default: 32767 Range: 2 to 32767.");
83 module_param(enable_oplocks, bool, 0644);
84 MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1");
86 extern mempool_t *cifs_sm_req_poolp;
87 extern mempool_t *cifs_req_poolp;
88 extern mempool_t *cifs_mid_poolp;
90 struct workqueue_struct *cifsiod_wq;
91 struct workqueue_struct *cifsoplockd_wq;
92 __u32 cifs_lock_secret;
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.
101 cifs_sb_active(struct super_block *sb)
103 struct cifs_sb_info *server = CIFS_SB(sb);
105 if (atomic_inc_return(&server->active) == 1)
106 atomic_inc(&sb->s_active);
110 cifs_sb_deactive(struct super_block *sb)
112 struct cifs_sb_info *server = CIFS_SB(sb);
114 if (atomic_dec_and_test(&server->active))
115 deactivate_super(sb);
119 cifs_read_super(struct super_block *sb)
122 struct cifs_sb_info *cifs_sb;
123 struct cifs_tcon *tcon;
126 cifs_sb = CIFS_SB(sb);
127 tcon = cifs_sb_master_tcon(cifs_sb);
129 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
130 sb->s_flags |= MS_POSIXACL;
132 if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files)
133 sb->s_maxbytes = MAX_LFS_FILESIZE;
135 sb->s_maxbytes = MAX_NON_LFS;
137 /* BB FIXME fix time_gran to be larger for LANMAN sessions */
138 sb->s_time_gran = 100;
140 sb->s_magic = CIFS_MAGIC_NUMBER;
141 sb->s_op = &cifs_super_ops;
142 sb->s_xattr = cifs_xattr_handlers;
143 rc = super_setup_bdi(sb);
146 /* tune readahead according to rsize */
147 sb->s_bdi->ra_pages = cifs_sb->rsize / PAGE_SIZE;
149 sb->s_blocksize = CIFS_MAX_MSGSIZE;
150 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
151 inode = cifs_root_iget(sb);
159 sb->s_d_op = &cifs_ci_dentry_ops;
161 sb->s_d_op = &cifs_dentry_ops;
163 sb->s_root = d_make_root(inode);
169 #ifdef CONFIG_CIFS_NFSD_EXPORT
170 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
171 cifs_dbg(FYI, "export ops supported\n");
172 sb->s_export_op = &cifs_export_ops;
174 #endif /* CONFIG_CIFS_NFSD_EXPORT */
179 cifs_dbg(VFS, "%s: get root inode failed\n", __func__);
183 static void cifs_kill_sb(struct super_block *sb)
185 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
187 cifs_umount(cifs_sb);
191 cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
193 struct super_block *sb = dentry->d_sb;
194 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
195 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
196 struct TCP_Server_Info *server = tcon->ses->server;
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
207 * Instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO.
209 buf->f_namelen = PATH_MAX;
210 buf->f_files = 0; /* undefined */
211 buf->f_ffree = 0; /* unlimited */
213 if (server->ops->queryfs)
214 rc = server->ops->queryfs(xid, tcon, buf);
220 static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
222 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
223 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
224 struct TCP_Server_Info *server = tcon->ses->server;
226 if (server->ops->fallocate)
227 return server->ops->fallocate(file, tcon, mode, off, len);
232 static int cifs_permission(struct inode *inode, int mask)
234 struct cifs_sb_info *cifs_sb;
236 cifs_sb = CIFS_SB(inode->i_sb);
238 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
239 if ((mask & MAY_EXEC) && !execute_ok(inode))
243 } else /* file mode might have been restricted at mount time
244 on the client (above and beyond ACL on servers) for
245 servers which do not support setting and viewing mode bits,
246 so allowing client to check permissions is useful */
247 return generic_permission(inode, mask);
250 static struct kmem_cache *cifs_inode_cachep;
251 static struct kmem_cache *cifs_req_cachep;
252 static struct kmem_cache *cifs_mid_cachep;
253 static struct kmem_cache *cifs_sm_req_cachep;
254 mempool_t *cifs_sm_req_poolp;
255 mempool_t *cifs_req_poolp;
256 mempool_t *cifs_mid_poolp;
258 static struct inode *
259 cifs_alloc_inode(struct super_block *sb)
261 struct cifsInodeInfo *cifs_inode;
262 cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
265 cifs_inode->cifsAttrs = 0x20; /* default */
266 cifs_inode->time = 0;
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.
271 cifs_set_oplock_level(cifs_inode, 0);
272 cifs_inode->flags = 0;
273 spin_lock_init(&cifs_inode->writers_lock);
274 cifs_inode->writers = 0;
275 cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
276 cifs_inode->server_eof = 0;
277 cifs_inode->uniqueid = 0;
278 cifs_inode->createtime = 0;
279 cifs_inode->epoch = 0;
280 #ifdef CONFIG_CIFS_SMB2
281 generate_random_uuid(cifs_inode->lease_key);
284 * Can not set i_flags here - they get immediately overwritten to zero
287 /* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME; */
288 INIT_LIST_HEAD(&cifs_inode->openFileList);
289 INIT_LIST_HEAD(&cifs_inode->llist);
290 return &cifs_inode->vfs_inode;
293 static void cifs_i_callback(struct rcu_head *head)
295 struct inode *inode = container_of(head, struct inode, i_rcu);
296 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
300 cifs_destroy_inode(struct inode *inode)
302 call_rcu(&inode->i_rcu, cifs_i_callback);
306 cifs_evict_inode(struct inode *inode)
308 truncate_inode_pages_final(&inode->i_data);
310 cifs_fscache_release_inode_cookie(inode);
314 cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
316 struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
317 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
319 seq_puts(s, ",addr=");
321 switch (server->dstaddr.ss_family) {
323 seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
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);
331 seq_puts(s, "(unknown)");
336 cifs_show_security(struct seq_file *s, struct cifs_ses *ses)
338 if (ses->sectype == Unspecified) {
339 if (ses->user_name == NULL)
340 seq_puts(s, ",sec=none");
344 seq_puts(s, ",sec=");
346 switch (ses->sectype) {
348 seq_puts(s, "lanman");
351 seq_puts(s, "ntlmv2");
360 seq_puts(s, "ntlmssp");
363 /* shouldn't ever happen */
364 seq_puts(s, "unknown");
373 cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
375 seq_puts(s, ",cache=");
377 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
378 seq_puts(s, "strict");
379 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
382 seq_puts(s, "loose");
386 cifs_show_nls(struct seq_file *s, struct nls_table *cur)
388 struct nls_table *def;
390 /* Display iocharset= option if it's not default charset */
391 def = load_nls_default();
393 seq_printf(s, ",iocharset=%s", cur->charset);
398 * cifs_show_options() is for displaying mount options in /proc/mounts.
399 * Not all settable options are displayed but most of the important
403 cifs_show_options(struct seq_file *s, struct dentry *root)
405 struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
406 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
407 struct sockaddr *srcaddr;
408 srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
410 seq_show_option(s, "vers", tcon->ses->server->vals->version_string);
411 cifs_show_security(s, tcon->ses);
412 cifs_show_cache_flavor(s, cifs_sb);
414 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)
415 seq_puts(s, ",multiuser");
416 else if (tcon->ses->user_name)
417 seq_show_option(s, "username", tcon->ses->user_name);
419 if (tcon->ses->domainName)
420 seq_show_option(s, "domain", tcon->ses->domainName);
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",
430 else if (srcaddr->sa_family == AF_INET)
431 seq_printf(s, ",srcaddr=%pI4",
432 &saddr4->sin_addr.s_addr);
434 seq_printf(s, ",srcaddr=BAD-AF:%i",
435 (int)(srcaddr->sa_family));
438 seq_printf(s, ",uid=%u",
439 from_kuid_munged(&init_user_ns, cifs_sb->mnt_uid));
440 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
441 seq_puts(s, ",forceuid");
443 seq_puts(s, ",noforceuid");
445 seq_printf(s, ",gid=%u",
446 from_kgid_munged(&init_user_ns, cifs_sb->mnt_gid));
447 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
448 seq_puts(s, ",forcegid");
450 seq_puts(s, ",noforcegid");
452 cifs_show_address(s, tcon->ses->server);
455 seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
456 cifs_sb->mnt_file_mode,
457 cifs_sb->mnt_dir_mode);
459 cifs_show_nls(s, cifs_sb->local_nls);
462 seq_puts(s, ",seal");
464 seq_puts(s, ",nocase");
466 seq_puts(s, ",hard");
467 if (tcon->use_persistent)
468 seq_puts(s, ",persistenthandles");
469 else if (tcon->use_resilient)
470 seq_puts(s, ",resilienthandles");
472 seq_puts(s, ",unix");
474 seq_puts(s, ",nounix");
475 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
476 seq_puts(s, ",posixpaths");
477 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
478 seq_puts(s, ",setuids");
479 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
480 seq_puts(s, ",idsfromsid");
481 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
482 seq_puts(s, ",serverino");
483 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
484 seq_puts(s, ",rwpidforward");
485 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
486 seq_puts(s, ",forcemand");
487 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
488 seq_puts(s, ",nouser_xattr");
489 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
490 seq_puts(s, ",mapchars");
491 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
492 seq_puts(s, ",mapposix");
493 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
495 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
496 seq_puts(s, ",nobrl");
497 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
498 seq_puts(s, ",cifsacl");
499 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
500 seq_puts(s, ",dynperm");
501 if (root->d_sb->s_flags & MS_POSIXACL)
503 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
504 seq_puts(s, ",mfsymlinks");
505 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
507 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
508 seq_puts(s, ",nostrictsync");
509 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
510 seq_puts(s, ",noperm");
511 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID)
512 seq_printf(s, ",backupuid=%u",
513 from_kuid_munged(&init_user_ns,
514 cifs_sb->mnt_backupuid));
515 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID)
516 seq_printf(s, ",backupgid=%u",
517 from_kgid_munged(&init_user_ns,
518 cifs_sb->mnt_backupgid));
520 seq_printf(s, ",rsize=%u", cifs_sb->rsize);
521 seq_printf(s, ",wsize=%u", cifs_sb->wsize);
522 seq_printf(s, ",echo_interval=%lu",
523 tcon->ses->server->echo_interval / HZ);
524 /* convert actimeo and display it in seconds */
525 seq_printf(s, ",actimeo=%lu", cifs_sb->actimeo / HZ);
530 static void cifs_umount_begin(struct super_block *sb)
532 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
533 struct cifs_tcon *tcon;
538 tcon = cifs_sb_master_tcon(cifs_sb);
540 spin_lock(&cifs_tcp_ses_lock);
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 */
545 spin_unlock(&cifs_tcp_ses_lock);
547 } else if (tcon->tc_count == 1)
548 tcon->tidStatus = CifsExiting;
549 spin_unlock(&cifs_tcp_ses_lock);
551 /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
552 /* cancel_notify_requests(tcon); */
553 if (tcon->ses && tcon->ses->server) {
554 cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
555 wake_up_all(&tcon->ses->server->request_q);
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);
566 #ifdef CONFIG_CIFS_STATS2
567 static int cifs_show_stats(struct seq_file *s, struct dentry *root)
574 static int cifs_remount(struct super_block *sb, int *flags, char *data)
577 *flags |= MS_NODIRATIME;
581 static int cifs_drop_inode(struct inode *inode)
583 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
585 /* no serverino => unconditional eviction */
586 return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
587 generic_drop_inode(inode);
590 static const struct super_operations cifs_super_ops = {
591 .statfs = cifs_statfs,
592 .alloc_inode = cifs_alloc_inode,
593 .destroy_inode = cifs_destroy_inode,
594 .drop_inode = cifs_drop_inode,
595 .evict_inode = cifs_evict_inode,
596 /* .delete_inode = cifs_delete_inode, */ /* Do not need above
597 function unless later we add lazy close of inodes or unless the
598 kernel forgets to call us with the same number of releases (closes)
600 .show_options = cifs_show_options,
601 .umount_begin = cifs_umount_begin,
602 .remount_fs = cifs_remount,
603 #ifdef CONFIG_CIFS_STATS2
604 .show_stats = cifs_show_stats,
609 * Get root dentry from superblock according to prefix path mount option.
610 * Return dentry with refcount + 1 on success and NULL otherwise.
612 static struct dentry *
613 cifs_get_root(struct smb_vol *vol, struct super_block *sb)
615 struct dentry *dentry;
616 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
617 char *full_path = NULL;
621 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
622 return dget(sb->s_root);
624 full_path = cifs_build_path_to_root(vol, cifs_sb,
625 cifs_sb_master_tcon(cifs_sb), 0);
626 if (full_path == NULL)
627 return ERR_PTR(-ENOMEM);
629 cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
631 sep = CIFS_DIR_SEP(cifs_sb);
632 dentry = dget(sb->s_root);
636 struct inode *dir = d_inode(dentry);
637 struct dentry *child;
641 dentry = ERR_PTR(-ENOENT);
644 if (!S_ISDIR(dir->i_mode)) {
646 dentry = ERR_PTR(-ENOTDIR);
650 /* skip separators */
657 while (*s && *s != sep)
660 child = lookup_one_len_unlocked(p, dentry, s - p);
663 } while (!IS_ERR(dentry));
668 static int cifs_set_super(struct super_block *sb, void *data)
670 struct cifs_mnt_data *mnt_data = data;
671 sb->s_fs_info = mnt_data->cifs_sb;
672 return set_anon_super(sb, NULL);
675 static struct dentry *
676 cifs_do_mount(struct file_system_type *fs_type,
677 int flags, const char *dev_name, void *data)
680 struct super_block *sb;
681 struct cifs_sb_info *cifs_sb;
682 struct smb_vol *volume_info;
683 struct cifs_mnt_data mnt_data;
686 cifs_dbg(FYI, "Devname: %s flags: %d\n", dev_name, flags);
688 volume_info = cifs_get_volume_info((char *)data, dev_name);
689 if (IS_ERR(volume_info))
690 return ERR_CAST(volume_info);
692 cifs_sb = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
693 if (cifs_sb == NULL) {
694 root = ERR_PTR(-ENOMEM);
698 cifs_sb->mountdata = kstrndup(data, PAGE_SIZE, GFP_KERNEL);
699 if (cifs_sb->mountdata == NULL) {
700 root = ERR_PTR(-ENOMEM);
704 rc = cifs_setup_cifs_sb(volume_info, cifs_sb);
710 rc = cifs_mount(cifs_sb, volume_info);
712 if (!(flags & MS_SILENT))
713 cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
719 mnt_data.vol = volume_info;
720 mnt_data.cifs_sb = cifs_sb;
721 mnt_data.flags = flags;
723 /* BB should we make this contingent on mount parm? */
724 flags |= MS_NODIRATIME | MS_NOATIME;
726 sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
729 cifs_umount(cifs_sb);
734 cifs_dbg(FYI, "Use existing superblock\n");
735 cifs_umount(cifs_sb);
737 rc = cifs_read_super(sb);
743 sb->s_flags |= MS_ACTIVE;
746 root = cifs_get_root(volume_info, sb);
750 cifs_dbg(FYI, "dentry root is: %p\n", root);
754 deactivate_locked_super(sb);
756 cifs_cleanup_volume_info(volume_info);
760 kfree(cifs_sb->prepath);
761 kfree(cifs_sb->mountdata);
764 unload_nls(volume_info->local_nls);
769 cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
772 struct inode *inode = file_inode(iocb->ki_filp);
774 if (iocb->ki_filp->f_flags & O_DIRECT)
775 return cifs_user_readv(iocb, iter);
777 rc = cifs_revalidate_mapping(inode);
781 return generic_file_read_iter(iocb, iter);
784 static ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
786 struct inode *inode = file_inode(iocb->ki_filp);
787 struct cifsInodeInfo *cinode = CIFS_I(inode);
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);
796 "Set no oplock for inode=%p after a write operation\n",
803 written = cifs_get_writer(cinode);
807 written = generic_file_write_iter(iocb, from);
809 if (CIFS_CACHE_WRITE(CIFS_I(inode)))
812 rc = filemap_fdatawrite(inode->i_mapping);
814 cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
818 cifs_put_writer(cinode);
822 static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
825 * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
826 * the cached file length
828 if (whence != SEEK_SET && whence != SEEK_CUR) {
830 struct inode *inode = file_inode(file);
833 * We need to be sure that all dirty pages are written and the
834 * server has the newest file length.
836 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
837 inode->i_mapping->nrpages != 0) {
838 rc = filemap_fdatawait(inode->i_mapping);
840 mapping_set_error(inode->i_mapping, rc);
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.
849 CIFS_I(inode)->time = 0;
851 rc = cifs_revalidate_file_attr(file);
855 return generic_file_llseek(file, offset, whence);
859 cifs_setlease(struct file *file, long arg, struct file_lock **lease, void **priv)
862 * Note that this is called by vfs setlease with i_lock held to
863 * protect *lease from going away.
865 struct inode *inode = file_inode(file);
866 struct cifsFileInfo *cfile = file->private_data;
868 if (!(S_ISREG(inode->i_mode)))
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))) ||
874 ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
875 return generic_setlease(file, arg, lease, priv);
876 else if (tlink_tcon(cfile->tlink)->local_lease &&
877 !CIFS_CACHE_READ(CIFS_I(inode)))
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
886 return generic_setlease(file, arg, lease, priv);
891 struct file_system_type cifs_fs_type = {
892 .owner = THIS_MODULE,
894 .mount = cifs_do_mount,
895 .kill_sb = cifs_kill_sb,
898 MODULE_ALIAS_FS("cifs");
899 const struct inode_operations cifs_dir_inode_ops = {
900 .create = cifs_create,
901 .atomic_open = cifs_atomic_open,
902 .lookup = cifs_lookup,
903 .getattr = cifs_getattr,
904 .unlink = cifs_unlink,
905 .link = cifs_hardlink,
908 .rename = cifs_rename2,
909 .permission = cifs_permission,
910 .setattr = cifs_setattr,
911 .symlink = cifs_symlink,
913 .listxattr = cifs_listxattr,
916 const struct inode_operations cifs_file_inode_ops = {
917 .setattr = cifs_setattr,
918 .getattr = cifs_getattr,
919 .permission = cifs_permission,
920 .listxattr = cifs_listxattr,
923 const struct inode_operations cifs_symlink_inode_ops = {
924 .get_link = cifs_get_link,
925 .permission = cifs_permission,
926 .listxattr = cifs_listxattr,
929 static int cifs_clone_file_range(struct file *src_file, loff_t off,
930 struct file *dst_file, loff_t destoff, u64 len)
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;
936 struct cifs_tcon *target_tcon = tlink_tcon(smb_file_target->tlink);
940 cifs_dbg(FYI, "clone range\n");
944 if (!src_file->private_data || !dst_file->private_data) {
946 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
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
955 lock_two_nondirectories(target_inode, src_inode);
958 len = src_inode->i_size - off;
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,
963 PAGE_ALIGN(destoff + len)-1);
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);
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;
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);
982 ssize_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)
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;
995 cifs_dbg(FYI, "copychunk range\n");
997 if (src_inode == target_inode) {
1002 if (!src_file->private_data || !dst_file->private_data) {
1004 cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
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);
1014 if (src_tcon->ses != target_tcon->ses) {
1015 cifs_dbg(VFS, "source and target of copy not on same server\n");
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
1024 lock_two_nondirectories(target_inode, src_inode);
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);
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);
1036 /* force revalidate of size and timestamps of target file now
1037 * that target is updated on the server
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
1043 unlock_two_nondirectories(src_inode, target_inode);
1049 static 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)
1053 unsigned int xid = get_xid();
1056 rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
1062 const struct file_operations cifs_file_ops = {
1063 .read_iter = cifs_loose_read_iter,
1064 .write_iter = cifs_file_write_iter,
1066 .release = cifs_close,
1068 .fsync = cifs_fsync,
1069 .flush = cifs_flush,
1070 .mmap = cifs_file_mmap,
1071 .splice_read = generic_file_splice_read,
1072 .llseek = cifs_llseek,
1073 .unlocked_ioctl = cifs_ioctl,
1074 .copy_file_range = cifs_copy_file_range,
1075 .clone_file_range = cifs_clone_file_range,
1076 .setlease = cifs_setlease,
1077 .fallocate = cifs_fallocate,
1080 const struct file_operations cifs_file_strict_ops = {
1081 .read_iter = cifs_strict_readv,
1082 .write_iter = cifs_strict_writev,
1084 .release = cifs_close,
1086 .fsync = cifs_strict_fsync,
1087 .flush = cifs_flush,
1088 .mmap = cifs_file_strict_mmap,
1089 .splice_read = generic_file_splice_read,
1090 .llseek = cifs_llseek,
1091 .unlocked_ioctl = cifs_ioctl,
1092 .copy_file_range = cifs_copy_file_range,
1093 .clone_file_range = cifs_clone_file_range,
1094 .setlease = cifs_setlease,
1095 .fallocate = cifs_fallocate,
1098 const struct file_operations cifs_file_direct_ops = {
1099 /* BB reevaluate whether they can be done with directio, no cache */
1100 .read_iter = cifs_user_readv,
1101 .write_iter = cifs_user_writev,
1103 .release = cifs_close,
1105 .fsync = cifs_fsync,
1106 .flush = cifs_flush,
1107 .mmap = cifs_file_mmap,
1108 .splice_read = generic_file_splice_read,
1109 .unlocked_ioctl = cifs_ioctl,
1110 .copy_file_range = cifs_copy_file_range,
1111 .clone_file_range = cifs_clone_file_range,
1112 .llseek = cifs_llseek,
1113 .setlease = cifs_setlease,
1114 .fallocate = cifs_fallocate,
1117 const struct file_operations cifs_file_nobrl_ops = {
1118 .read_iter = cifs_loose_read_iter,
1119 .write_iter = cifs_file_write_iter,
1121 .release = cifs_close,
1122 .fsync = cifs_fsync,
1123 .flush = cifs_flush,
1124 .mmap = cifs_file_mmap,
1125 .splice_read = generic_file_splice_read,
1126 .llseek = cifs_llseek,
1127 .unlocked_ioctl = cifs_ioctl,
1128 .copy_file_range = cifs_copy_file_range,
1129 .clone_file_range = cifs_clone_file_range,
1130 .setlease = cifs_setlease,
1131 .fallocate = cifs_fallocate,
1134 const struct file_operations cifs_file_strict_nobrl_ops = {
1135 .read_iter = cifs_strict_readv,
1136 .write_iter = cifs_strict_writev,
1138 .release = cifs_close,
1139 .fsync = cifs_strict_fsync,
1140 .flush = cifs_flush,
1141 .mmap = cifs_file_strict_mmap,
1142 .splice_read = generic_file_splice_read,
1143 .llseek = cifs_llseek,
1144 .unlocked_ioctl = cifs_ioctl,
1145 .copy_file_range = cifs_copy_file_range,
1146 .clone_file_range = cifs_clone_file_range,
1147 .setlease = cifs_setlease,
1148 .fallocate = cifs_fallocate,
1151 const struct file_operations cifs_file_direct_nobrl_ops = {
1152 /* BB reevaluate whether they can be done with directio, no cache */
1153 .read_iter = cifs_user_readv,
1154 .write_iter = cifs_user_writev,
1156 .release = cifs_close,
1157 .fsync = cifs_fsync,
1158 .flush = cifs_flush,
1159 .mmap = cifs_file_mmap,
1160 .splice_read = generic_file_splice_read,
1161 .unlocked_ioctl = cifs_ioctl,
1162 .copy_file_range = cifs_copy_file_range,
1163 .clone_file_range = cifs_clone_file_range,
1164 .llseek = cifs_llseek,
1165 .setlease = cifs_setlease,
1166 .fallocate = cifs_fallocate,
1169 const struct file_operations cifs_dir_ops = {
1170 .iterate_shared = cifs_readdir,
1171 .release = cifs_closedir,
1172 .read = generic_read_dir,
1173 .unlocked_ioctl = cifs_ioctl,
1174 .copy_file_range = cifs_copy_file_range,
1175 .clone_file_range = cifs_clone_file_range,
1176 .llseek = generic_file_llseek,
1180 cifs_init_once(void *inode)
1182 struct cifsInodeInfo *cifsi = inode;
1184 inode_init_once(&cifsi->vfs_inode);
1185 init_rwsem(&cifsi->lock_sem);
1189 cifs_init_inodecache(void)
1191 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
1192 sizeof(struct cifsInodeInfo),
1193 0, (SLAB_RECLAIM_ACCOUNT|
1194 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1196 if (cifs_inode_cachep == NULL)
1203 cifs_destroy_inodecache(void)
1206 * Make sure all delayed rcu free inodes are flushed before we
1210 kmem_cache_destroy(cifs_inode_cachep);
1214 cifs_init_request_bufs(void)
1216 size_t max_hdr_size = MAX_CIFS_HDR_SIZE;
1217 #ifdef CONFIG_CIFS_SMB2
1219 * SMB2 maximum header size is bigger than CIFS one - no problems to
1220 * allocate some more bytes for CIFS.
1222 max_hdr_size = MAX_SMB2_HDR_SIZE;
1224 if (CIFSMaxBufSize < 8192) {
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;
1231 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
1234 cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
1235 CIFSMaxBufSize, CIFSMaxBufSize);
1237 cifs_req_cachep = kmem_cache_create("cifs_request",
1238 CIFSMaxBufSize + max_hdr_size, 0,
1239 SLAB_HWCACHE_ALIGN, NULL);
1240 if (cifs_req_cachep == NULL)
1243 if (cifs_min_rcv < 1)
1245 else if (cifs_min_rcv > 64) {
1247 cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
1250 cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
1253 if (cifs_req_poolp == NULL) {
1254 kmem_cache_destroy(cifs_req_cachep);
1257 /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1258 almost all handle based requests (but not write response, nor is it
1259 sufficient for path based requests). A smaller size would have
1260 been more efficient (compacting multiple slab items on one 4k page)
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
1263 efficient to alloc 1 per page off the slab compared to 17K (5page)
1264 alloc of large cifs buffers even when page debugging is on */
1265 cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
1266 MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
1268 if (cifs_sm_req_cachep == NULL) {
1269 mempool_destroy(cifs_req_poolp);
1270 kmem_cache_destroy(cifs_req_cachep);
1274 if (cifs_min_small < 2)
1276 else if (cifs_min_small > 256) {
1277 cifs_min_small = 256;
1278 cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
1281 cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
1282 cifs_sm_req_cachep);
1284 if (cifs_sm_req_poolp == NULL) {
1285 mempool_destroy(cifs_req_poolp);
1286 kmem_cache_destroy(cifs_req_cachep);
1287 kmem_cache_destroy(cifs_sm_req_cachep);
1295 cifs_destroy_request_bufs(void)
1297 mempool_destroy(cifs_req_poolp);
1298 kmem_cache_destroy(cifs_req_cachep);
1299 mempool_destroy(cifs_sm_req_poolp);
1300 kmem_cache_destroy(cifs_sm_req_cachep);
1304 cifs_init_mids(void)
1306 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
1307 sizeof(struct mid_q_entry), 0,
1308 SLAB_HWCACHE_ALIGN, NULL);
1309 if (cifs_mid_cachep == NULL)
1312 /* 3 is a reasonable minimum number of simultaneous operations */
1313 cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
1314 if (cifs_mid_poolp == NULL) {
1315 kmem_cache_destroy(cifs_mid_cachep);
1323 cifs_destroy_mids(void)
1325 mempool_destroy(cifs_mid_poolp);
1326 kmem_cache_destroy(cifs_mid_cachep);
1334 INIT_LIST_HEAD(&cifs_tcp_ses_list);
1335 #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* unused temporarily */
1336 INIT_LIST_HEAD(&GlobalDnotifyReqList);
1337 INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
1338 #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */
1340 * Initialize Global counters
1342 atomic_set(&sesInfoAllocCount, 0);
1343 atomic_set(&tconInfoAllocCount, 0);
1344 atomic_set(&tcpSesAllocCount, 0);
1345 atomic_set(&tcpSesReconnectCount, 0);
1346 atomic_set(&tconInfoReconnectCount, 0);
1348 atomic_set(&bufAllocCount, 0);
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 */
1355 atomic_set(&midCount, 0);
1356 GlobalCurrentXid = 0;
1357 GlobalTotalActiveXid = 0;
1358 GlobalMaxActiveXid = 0;
1359 spin_lock_init(&cifs_tcp_ses_lock);
1360 spin_lock_init(&GlobalMid_Lock);
1362 get_random_bytes(&cifs_lock_secret, sizeof(cifs_lock_secret));
1364 if (cifs_max_pending < 2) {
1365 cifs_max_pending = 2;
1366 cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
1367 } else if (cifs_max_pending > CIFS_MAX_REQ) {
1368 cifs_max_pending = CIFS_MAX_REQ;
1369 cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
1373 cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1376 goto out_clean_proc;
1379 cifsoplockd_wq = alloc_workqueue("cifsoplockd",
1380 WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
1381 if (!cifsoplockd_wq) {
1383 goto out_destroy_cifsiod_wq;
1386 rc = cifs_fscache_register();
1388 goto out_destroy_cifsoplockd_wq;
1390 rc = cifs_init_inodecache();
1392 goto out_unreg_fscache;
1394 rc = cifs_init_mids();
1396 goto out_destroy_inodecache;
1398 rc = cifs_init_request_bufs();
1400 goto out_destroy_mids;
1402 #ifdef CONFIG_CIFS_UPCALL
1403 rc = init_cifs_spnego();
1405 goto out_destroy_request_bufs;
1406 #endif /* CONFIG_CIFS_UPCALL */
1408 #ifdef CONFIG_CIFS_ACL
1409 rc = init_cifs_idmap();
1411 goto out_register_key_type;
1412 #endif /* CONFIG_CIFS_ACL */
1414 rc = register_filesystem(&cifs_fs_type);
1416 goto out_init_cifs_idmap;
1420 out_init_cifs_idmap:
1421 #ifdef CONFIG_CIFS_ACL
1423 out_register_key_type:
1425 #ifdef CONFIG_CIFS_UPCALL
1427 out_destroy_request_bufs:
1429 cifs_destroy_request_bufs();
1431 cifs_destroy_mids();
1432 out_destroy_inodecache:
1433 cifs_destroy_inodecache();
1435 cifs_fscache_unregister();
1436 out_destroy_cifsoplockd_wq:
1437 destroy_workqueue(cifsoplockd_wq);
1438 out_destroy_cifsiod_wq:
1439 destroy_workqueue(cifsiod_wq);
1448 cifs_dbg(NOISY, "exit_cifs\n");
1449 unregister_filesystem(&cifs_fs_type);
1450 cifs_dfs_release_automount_timer();
1451 #ifdef CONFIG_CIFS_ACL
1454 #ifdef CONFIG_CIFS_UPCALL
1455 unregister_key_type(&cifs_spnego_key_type);
1457 cifs_destroy_request_bufs();
1458 cifs_destroy_mids();
1459 cifs_destroy_inodecache();
1460 cifs_fscache_unregister();
1461 destroy_workqueue(cifsoplockd_wq);
1462 destroy_workqueue(cifsiod_wq);
1466 MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
1467 MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
1469 ("VFS to access servers complying with the SNIA CIFS Specification "
1470 "e.g. Samba and Windows");
1471 MODULE_VERSION(CIFS_VERSION);
1472 MODULE_SOFTDEP("pre: arc4");
1473 MODULE_SOFTDEP("pre: des");
1474 MODULE_SOFTDEP("pre: ecb");
1475 MODULE_SOFTDEP("pre: hmac");
1476 MODULE_SOFTDEP("pre: md4");
1477 MODULE_SOFTDEP("pre: md5");
1478 MODULE_SOFTDEP("pre: nls");
1479 #ifdef CONFIG_CIFS_SMB2
1480 MODULE_SOFTDEP("pre: aes");
1481 MODULE_SOFTDEP("pre: cmac");
1482 MODULE_SOFTDEP("pre: sha256");
1483 MODULE_SOFTDEP("pre: aead2");
1484 MODULE_SOFTDEP("pre: ccm");
1485 #endif /* CONFIG_CIFS_SMB2 */
1486 module_init(init_cifs)
1487 module_exit(exit_cifs)