cifsfs.c 36 KB

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