cifsfs.c 36 KB

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