cifsfs.c 39 KB

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