cifsfs.c 41 KB

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