cifsfs.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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 <net/ipv6.h>
  37. #include "cifsfs.h"
  38. #include "cifspdu.h"
  39. #define DECLARE_GLOBALS_HERE
  40. #include "cifsglob.h"
  41. #include "cifsproto.h"
  42. #include "cifs_debug.h"
  43. #include "cifs_fs_sb.h"
  44. #include <linux/mm.h>
  45. #include <linux/key-type.h>
  46. #include "cifs_spnego.h"
  47. #include "fscache.h"
  48. #define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDUs */
  49. int cifsFYI = 0;
  50. int cifsERROR = 1;
  51. int traceSMB = 0;
  52. unsigned int oplockEnabled = 1;
  53. unsigned int experimEnabled = 0;
  54. unsigned int linuxExtEnabled = 1;
  55. unsigned int lookupCacheEnabled = 1;
  56. unsigned int multiuser_mount = 0;
  57. unsigned int global_secflags = CIFSSEC_DEF;
  58. /* unsigned int ntlmv2_support = 0; */
  59. unsigned int sign_CIFS_PDUs = 1;
  60. static const struct super_operations cifs_super_ops;
  61. unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
  62. module_param(CIFSMaxBufSize, int, 0);
  63. MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header). "
  64. "Default: 16384 Range: 8192 to 130048");
  65. unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
  66. module_param(cifs_min_rcv, int, 0);
  67. MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
  68. "1 to 64");
  69. unsigned int cifs_min_small = 30;
  70. module_param(cifs_min_small, int, 0);
  71. MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
  72. "Range: 2 to 256");
  73. unsigned int cifs_max_pending = CIFS_MAX_REQ;
  74. module_param(cifs_max_pending, int, 0);
  75. MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server. "
  76. "Default: 50 Range: 2 to 256");
  77. extern mempool_t *cifs_sm_req_poolp;
  78. extern mempool_t *cifs_req_poolp;
  79. extern mempool_t *cifs_mid_poolp;
  80. void
  81. cifs_sb_active(struct super_block *sb)
  82. {
  83. struct cifs_sb_info *server = CIFS_SB(sb);
  84. if (atomic_inc_return(&server->active) == 1)
  85. atomic_inc(&sb->s_active);
  86. }
  87. void
  88. cifs_sb_deactive(struct super_block *sb)
  89. {
  90. struct cifs_sb_info *server = CIFS_SB(sb);
  91. if (atomic_dec_and_test(&server->active))
  92. deactivate_super(sb);
  93. }
  94. static int
  95. cifs_read_super(struct super_block *sb, void *data,
  96. const char *devname, int silent)
  97. {
  98. struct inode *inode;
  99. struct cifs_sb_info *cifs_sb;
  100. int rc = 0;
  101. /* BB should we make this contingent on mount parm? */
  102. sb->s_flags |= MS_NODIRATIME | MS_NOATIME;
  103. sb->s_fs_info = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
  104. cifs_sb = CIFS_SB(sb);
  105. if (cifs_sb == NULL)
  106. return -ENOMEM;
  107. spin_lock_init(&cifs_sb->tlink_tree_lock);
  108. cifs_sb->tlink_tree = RB_ROOT;
  109. rc = bdi_setup_and_register(&cifs_sb->bdi, "cifs", BDI_CAP_MAP_COPY);
  110. if (rc) {
  111. kfree(cifs_sb);
  112. return rc;
  113. }
  114. #ifdef CONFIG_CIFS_DFS_UPCALL
  115. /* copy mount params to sb for use in submounts */
  116. /* BB: should we move this after the mount so we
  117. * do not have to do the copy on failed mounts?
  118. * BB: May be it is better to do simple copy before
  119. * complex operation (mount), and in case of fail
  120. * just exit instead of doing mount and attempting
  121. * undo it if this copy fails?*/
  122. if (data) {
  123. int len = strlen(data);
  124. cifs_sb->mountdata = kzalloc(len + 1, GFP_KERNEL);
  125. if (cifs_sb->mountdata == NULL) {
  126. bdi_destroy(&cifs_sb->bdi);
  127. kfree(sb->s_fs_info);
  128. sb->s_fs_info = NULL;
  129. return -ENOMEM;
  130. }
  131. strncpy(cifs_sb->mountdata, data, len + 1);
  132. cifs_sb->mountdata[len] = '\0';
  133. }
  134. #endif
  135. rc = cifs_mount(sb, cifs_sb, data, devname);
  136. if (rc) {
  137. if (!silent)
  138. cERROR(1, "cifs_mount failed w/return code = %d", rc);
  139. goto out_mount_failed;
  140. }
  141. sb->s_magic = CIFS_MAGIC_NUMBER;
  142. sb->s_op = &cifs_super_ops;
  143. sb->s_bdi = &cifs_sb->bdi;
  144. sb->s_blocksize = CIFS_MAX_MSGSIZE;
  145. sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
  146. inode = cifs_root_iget(sb, ROOT_I);
  147. if (IS_ERR(inode)) {
  148. rc = PTR_ERR(inode);
  149. inode = NULL;
  150. goto out_no_root;
  151. }
  152. sb->s_root = d_alloc_root(inode);
  153. if (!sb->s_root) {
  154. rc = -ENOMEM;
  155. goto out_no_root;
  156. }
  157. /* do that *after* d_alloc_root() - we want NULL ->d_op for root here */
  158. if (cifs_sb_master_tcon(cifs_sb)->nocase)
  159. sb->s_d_op = &cifs_ci_dentry_ops;
  160. else
  161. sb->s_d_op = &cifs_dentry_ops;
  162. #ifdef CONFIG_CIFS_EXPERIMENTAL
  163. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  164. cFYI(1, "export ops supported");
  165. sb->s_export_op = &cifs_export_ops;
  166. }
  167. #endif /* EXPERIMENTAL */
  168. return 0;
  169. out_no_root:
  170. cERROR(1, "cifs_read_super: get root inode failed");
  171. if (inode)
  172. iput(inode);
  173. cifs_umount(sb, cifs_sb);
  174. out_mount_failed:
  175. if (cifs_sb) {
  176. #ifdef CONFIG_CIFS_DFS_UPCALL
  177. if (cifs_sb->mountdata) {
  178. kfree(cifs_sb->mountdata);
  179. cifs_sb->mountdata = NULL;
  180. }
  181. #endif
  182. unload_nls(cifs_sb->local_nls);
  183. bdi_destroy(&cifs_sb->bdi);
  184. kfree(cifs_sb);
  185. }
  186. return rc;
  187. }
  188. static void
  189. cifs_put_super(struct super_block *sb)
  190. {
  191. int rc = 0;
  192. struct cifs_sb_info *cifs_sb;
  193. cFYI(1, "In cifs_put_super");
  194. cifs_sb = CIFS_SB(sb);
  195. if (cifs_sb == NULL) {
  196. cFYI(1, "Empty cifs superblock info passed to unmount");
  197. return;
  198. }
  199. rc = cifs_umount(sb, cifs_sb);
  200. if (rc)
  201. cERROR(1, "cifs_umount failed with return code %d", rc);
  202. #ifdef CONFIG_CIFS_DFS_UPCALL
  203. if (cifs_sb->mountdata) {
  204. kfree(cifs_sb->mountdata);
  205. cifs_sb->mountdata = NULL;
  206. }
  207. #endif
  208. unload_nls(cifs_sb->local_nls);
  209. bdi_destroy(&cifs_sb->bdi);
  210. kfree(cifs_sb);
  211. }
  212. static int
  213. cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
  214. {
  215. struct super_block *sb = dentry->d_sb;
  216. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  217. struct cifsTconInfo *tcon = cifs_sb_master_tcon(cifs_sb);
  218. int rc = -EOPNOTSUPP;
  219. int xid;
  220. xid = GetXid();
  221. buf->f_type = CIFS_MAGIC_NUMBER;
  222. /*
  223. * PATH_MAX may be too long - it would presumably be total path,
  224. * but note that some servers (includinng Samba 3) have a shorter
  225. * maximum path.
  226. *
  227. * Instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO.
  228. */
  229. buf->f_namelen = PATH_MAX;
  230. buf->f_files = 0; /* undefined */
  231. buf->f_ffree = 0; /* unlimited */
  232. /*
  233. * We could add a second check for a QFS Unix capability bit
  234. */
  235. if ((tcon->ses->capabilities & CAP_UNIX) &&
  236. (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
  237. rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
  238. /*
  239. * Only need to call the old QFSInfo if failed on newer one,
  240. * e.g. by OS/2.
  241. **/
  242. if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
  243. rc = CIFSSMBQFSInfo(xid, tcon, buf);
  244. /*
  245. * Some old Windows servers also do not support level 103, retry with
  246. * older level one if old server failed the previous call or we
  247. * bypassed it because we detected that this was an older LANMAN sess
  248. */
  249. if (rc)
  250. rc = SMBOldQFSInfo(xid, tcon, buf);
  251. FreeXid(xid);
  252. return 0;
  253. }
  254. static int cifs_permission(struct inode *inode, int mask, unsigned int flags)
  255. {
  256. struct cifs_sb_info *cifs_sb;
  257. if (flags & IPERM_FLAG_RCU)
  258. return -ECHILD;
  259. cifs_sb = CIFS_SB(inode->i_sb);
  260. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
  261. if ((mask & MAY_EXEC) && !execute_ok(inode))
  262. return -EACCES;
  263. else
  264. return 0;
  265. } else /* file mode might have been restricted at mount time
  266. on the client (above and beyond ACL on servers) for
  267. servers which do not support setting and viewing mode bits,
  268. so allowing client to check permissions is useful */
  269. return generic_permission(inode, mask, flags, NULL);
  270. }
  271. static struct kmem_cache *cifs_inode_cachep;
  272. static struct kmem_cache *cifs_req_cachep;
  273. static struct kmem_cache *cifs_mid_cachep;
  274. static struct kmem_cache *cifs_sm_req_cachep;
  275. mempool_t *cifs_sm_req_poolp;
  276. mempool_t *cifs_req_poolp;
  277. mempool_t *cifs_mid_poolp;
  278. static struct inode *
  279. cifs_alloc_inode(struct super_block *sb)
  280. {
  281. struct cifsInodeInfo *cifs_inode;
  282. cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
  283. if (!cifs_inode)
  284. return NULL;
  285. cifs_inode->cifsAttrs = 0x20; /* default */
  286. cifs_inode->time = 0;
  287. /* Until the file is open and we have gotten oplock
  288. info back from the server, can not assume caching of
  289. file data or metadata */
  290. cifs_set_oplock_level(cifs_inode, 0);
  291. cifs_inode->delete_pending = false;
  292. cifs_inode->invalid_mapping = false;
  293. cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
  294. cifs_inode->server_eof = 0;
  295. cifs_inode->uniqueid = 0;
  296. cifs_inode->createtime = 0;
  297. /* Can not set i_flags here - they get immediately overwritten
  298. to zero by the VFS */
  299. /* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;*/
  300. INIT_LIST_HEAD(&cifs_inode->openFileList);
  301. return &cifs_inode->vfs_inode;
  302. }
  303. static void cifs_i_callback(struct rcu_head *head)
  304. {
  305. struct inode *inode = container_of(head, struct inode, i_rcu);
  306. INIT_LIST_HEAD(&inode->i_dentry);
  307. kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
  308. }
  309. static void
  310. cifs_destroy_inode(struct inode *inode)
  311. {
  312. call_rcu(&inode->i_rcu, cifs_i_callback);
  313. }
  314. static void
  315. cifs_evict_inode(struct inode *inode)
  316. {
  317. truncate_inode_pages(&inode->i_data, 0);
  318. end_writeback(inode);
  319. cifs_fscache_release_inode_cookie(inode);
  320. }
  321. static void
  322. cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
  323. {
  324. struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
  325. struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
  326. seq_printf(s, ",addr=");
  327. switch (server->dstaddr.ss_family) {
  328. case AF_INET:
  329. seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
  330. break;
  331. case AF_INET6:
  332. seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
  333. if (sa6->sin6_scope_id)
  334. seq_printf(s, "%%%u", sa6->sin6_scope_id);
  335. break;
  336. default:
  337. seq_printf(s, "(unknown)");
  338. }
  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 vfsmount *m)
  347. {
  348. struct cifs_sb_info *cifs_sb = CIFS_SB(m->mnt_sb);
  349. struct cifsTconInfo *tcon = cifs_sb_master_tcon(cifs_sb);
  350. struct sockaddr *srcaddr;
  351. srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
  352. seq_printf(s, ",unc=%s", tcon->treeName);
  353. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)
  354. seq_printf(s, ",multiuser");
  355. else if (tcon->ses->userName)
  356. seq_printf(s, ",username=%s", tcon->ses->userName);
  357. if (tcon->ses->domainName)
  358. seq_printf(s, ",domain=%s", tcon->ses->domainName);
  359. if (srcaddr->sa_family != AF_UNSPEC) {
  360. struct sockaddr_in *saddr4;
  361. struct sockaddr_in6 *saddr6;
  362. saddr4 = (struct sockaddr_in *)srcaddr;
  363. saddr6 = (struct sockaddr_in6 *)srcaddr;
  364. if (srcaddr->sa_family == AF_INET6)
  365. seq_printf(s, ",srcaddr=%pI6c",
  366. &saddr6->sin6_addr);
  367. else if (srcaddr->sa_family == AF_INET)
  368. seq_printf(s, ",srcaddr=%pI4",
  369. &saddr4->sin_addr.s_addr);
  370. else
  371. seq_printf(s, ",srcaddr=BAD-AF:%i",
  372. (int)(srcaddr->sa_family));
  373. }
  374. seq_printf(s, ",uid=%d", cifs_sb->mnt_uid);
  375. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
  376. seq_printf(s, ",forceuid");
  377. else
  378. seq_printf(s, ",noforceuid");
  379. seq_printf(s, ",gid=%d", cifs_sb->mnt_gid);
  380. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
  381. seq_printf(s, ",forcegid");
  382. else
  383. seq_printf(s, ",noforcegid");
  384. cifs_show_address(s, tcon->ses->server);
  385. if (!tcon->unix_ext)
  386. seq_printf(s, ",file_mode=0%o,dir_mode=0%o",
  387. cifs_sb->mnt_file_mode,
  388. cifs_sb->mnt_dir_mode);
  389. if (tcon->seal)
  390. seq_printf(s, ",seal");
  391. if (tcon->nocase)
  392. seq_printf(s, ",nocase");
  393. if (tcon->retry)
  394. seq_printf(s, ",hard");
  395. if (cifs_sb->prepath)
  396. seq_printf(s, ",prepath=%s", cifs_sb->prepath);
  397. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
  398. seq_printf(s, ",posixpaths");
  399. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
  400. seq_printf(s, ",setuids");
  401. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  402. seq_printf(s, ",serverino");
  403. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
  404. seq_printf(s, ",directio");
  405. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  406. seq_printf(s, ",nouser_xattr");
  407. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
  408. seq_printf(s, ",mapchars");
  409. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
  410. seq_printf(s, ",sfu");
  411. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  412. seq_printf(s, ",nobrl");
  413. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
  414. seq_printf(s, ",cifsacl");
  415. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  416. seq_printf(s, ",dynperm");
  417. if (m->mnt_sb->s_flags & MS_POSIXACL)
  418. seq_printf(s, ",acl");
  419. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
  420. seq_printf(s, ",mfsymlinks");
  421. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
  422. seq_printf(s, ",fsc");
  423. seq_printf(s, ",rsize=%d", cifs_sb->rsize);
  424. seq_printf(s, ",wsize=%d", cifs_sb->wsize);
  425. /* convert actimeo and display it in seconds */
  426. seq_printf(s, ",actimeo=%lu", cifs_sb->actimeo / HZ);
  427. return 0;
  428. }
  429. static void cifs_umount_begin(struct super_block *sb)
  430. {
  431. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  432. struct cifsTconInfo *tcon;
  433. if (cifs_sb == NULL)
  434. return;
  435. tcon = cifs_sb_master_tcon(cifs_sb);
  436. spin_lock(&cifs_tcp_ses_lock);
  437. if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
  438. /* we have other mounts to same share or we have
  439. already tried to force umount this and woken up
  440. all waiting network requests, nothing to do */
  441. spin_unlock(&cifs_tcp_ses_lock);
  442. return;
  443. } else if (tcon->tc_count == 1)
  444. tcon->tidStatus = CifsExiting;
  445. spin_unlock(&cifs_tcp_ses_lock);
  446. /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
  447. /* cancel_notify_requests(tcon); */
  448. if (tcon->ses && tcon->ses->server) {
  449. cFYI(1, "wake up tasks now - umount begin not complete");
  450. wake_up_all(&tcon->ses->server->request_q);
  451. wake_up_all(&tcon->ses->server->response_q);
  452. msleep(1); /* yield */
  453. /* we have to kick the requests once more */
  454. wake_up_all(&tcon->ses->server->response_q);
  455. msleep(1);
  456. }
  457. return;
  458. }
  459. #ifdef CONFIG_CIFS_STATS2
  460. static int cifs_show_stats(struct seq_file *s, struct vfsmount *mnt)
  461. {
  462. /* BB FIXME */
  463. return 0;
  464. }
  465. #endif
  466. static int cifs_remount(struct super_block *sb, int *flags, char *data)
  467. {
  468. *flags |= MS_NODIRATIME;
  469. return 0;
  470. }
  471. static int cifs_drop_inode(struct inode *inode)
  472. {
  473. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  474. /* no serverino => unconditional eviction */
  475. return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
  476. generic_drop_inode(inode);
  477. }
  478. static const struct super_operations cifs_super_ops = {
  479. .put_super = cifs_put_super,
  480. .statfs = cifs_statfs,
  481. .alloc_inode = cifs_alloc_inode,
  482. .destroy_inode = cifs_destroy_inode,
  483. .drop_inode = cifs_drop_inode,
  484. .evict_inode = cifs_evict_inode,
  485. /* .delete_inode = cifs_delete_inode, */ /* Do not need above
  486. function unless later we add lazy close of inodes or unless the
  487. kernel forgets to call us with the same number of releases (closes)
  488. as opens */
  489. .show_options = cifs_show_options,
  490. .umount_begin = cifs_umount_begin,
  491. .remount_fs = cifs_remount,
  492. #ifdef CONFIG_CIFS_STATS2
  493. .show_stats = cifs_show_stats,
  494. #endif
  495. };
  496. static struct dentry *
  497. cifs_do_mount(struct file_system_type *fs_type,
  498. int flags, const char *dev_name, void *data)
  499. {
  500. int rc;
  501. struct super_block *sb;
  502. sb = sget(fs_type, NULL, set_anon_super, NULL);
  503. cFYI(1, "Devname: %s flags: %d ", dev_name, flags);
  504. if (IS_ERR(sb))
  505. return ERR_CAST(sb);
  506. sb->s_flags = flags;
  507. rc = cifs_read_super(sb, data, dev_name, flags & MS_SILENT ? 1 : 0);
  508. if (rc) {
  509. deactivate_locked_super(sb);
  510. return ERR_PTR(rc);
  511. }
  512. sb->s_flags |= MS_ACTIVE;
  513. return dget(sb->s_root);
  514. }
  515. static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  516. unsigned long nr_segs, loff_t pos)
  517. {
  518. struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
  519. ssize_t written;
  520. written = generic_file_aio_write(iocb, iov, nr_segs, pos);
  521. if (!CIFS_I(inode)->clientCanCacheAll)
  522. filemap_fdatawrite(inode->i_mapping);
  523. return written;
  524. }
  525. static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
  526. {
  527. /* origin == SEEK_END => we must revalidate the cached file length */
  528. if (origin == SEEK_END) {
  529. int retval;
  530. /* some applications poll for the file length in this strange
  531. way so we must seek to end on non-oplocked files by
  532. setting the revalidate time to zero */
  533. CIFS_I(file->f_path.dentry->d_inode)->time = 0;
  534. retval = cifs_revalidate_file(file);
  535. if (retval < 0)
  536. return (loff_t)retval;
  537. }
  538. return generic_file_llseek_unlocked(file, offset, origin);
  539. }
  540. static int cifs_setlease(struct file *file, long arg, struct file_lock **lease)
  541. {
  542. /* note that this is called by vfs setlease with lock_flocks held
  543. to protect *lease from going away */
  544. struct inode *inode = file->f_path.dentry->d_inode;
  545. struct cifsFileInfo *cfile = file->private_data;
  546. if (!(S_ISREG(inode->i_mode)))
  547. return -EINVAL;
  548. /* check if file is oplocked */
  549. if (((arg == F_RDLCK) &&
  550. (CIFS_I(inode)->clientCanCacheRead)) ||
  551. ((arg == F_WRLCK) &&
  552. (CIFS_I(inode)->clientCanCacheAll)))
  553. return generic_setlease(file, arg, lease);
  554. else if (tlink_tcon(cfile->tlink)->local_lease &&
  555. !CIFS_I(inode)->clientCanCacheRead)
  556. /* If the server claims to support oplock on this
  557. file, then we still need to check oplock even
  558. if the local_lease mount option is set, but there
  559. are servers which do not support oplock for which
  560. this mount option may be useful if the user
  561. knows that the file won't be changed on the server
  562. by anyone else */
  563. return generic_setlease(file, arg, lease);
  564. else
  565. return -EAGAIN;
  566. }
  567. struct file_system_type cifs_fs_type = {
  568. .owner = THIS_MODULE,
  569. .name = "cifs",
  570. .mount = cifs_do_mount,
  571. .kill_sb = kill_anon_super,
  572. /* .fs_flags */
  573. };
  574. const struct inode_operations cifs_dir_inode_ops = {
  575. .create = cifs_create,
  576. .lookup = cifs_lookup,
  577. .getattr = cifs_getattr,
  578. .unlink = cifs_unlink,
  579. .link = cifs_hardlink,
  580. .mkdir = cifs_mkdir,
  581. .rmdir = cifs_rmdir,
  582. .rename = cifs_rename,
  583. .permission = cifs_permission,
  584. /* revalidate:cifs_revalidate, */
  585. .setattr = cifs_setattr,
  586. .symlink = cifs_symlink,
  587. .mknod = cifs_mknod,
  588. #ifdef CONFIG_CIFS_XATTR
  589. .setxattr = cifs_setxattr,
  590. .getxattr = cifs_getxattr,
  591. .listxattr = cifs_listxattr,
  592. .removexattr = cifs_removexattr,
  593. #endif
  594. };
  595. const struct inode_operations cifs_file_inode_ops = {
  596. /* revalidate:cifs_revalidate, */
  597. .setattr = cifs_setattr,
  598. .getattr = cifs_getattr, /* do we need this anymore? */
  599. .rename = cifs_rename,
  600. .permission = cifs_permission,
  601. #ifdef CONFIG_CIFS_XATTR
  602. .setxattr = cifs_setxattr,
  603. .getxattr = cifs_getxattr,
  604. .listxattr = cifs_listxattr,
  605. .removexattr = cifs_removexattr,
  606. #endif
  607. };
  608. const struct inode_operations cifs_symlink_inode_ops = {
  609. .readlink = generic_readlink,
  610. .follow_link = cifs_follow_link,
  611. .put_link = cifs_put_link,
  612. .permission = cifs_permission,
  613. /* BB add the following two eventually */
  614. /* revalidate: cifs_revalidate,
  615. setattr: cifs_notify_change, *//* BB do we need notify change */
  616. #ifdef CONFIG_CIFS_XATTR
  617. .setxattr = cifs_setxattr,
  618. .getxattr = cifs_getxattr,
  619. .listxattr = cifs_listxattr,
  620. .removexattr = cifs_removexattr,
  621. #endif
  622. };
  623. const struct file_operations cifs_file_ops = {
  624. .read = do_sync_read,
  625. .write = do_sync_write,
  626. .aio_read = generic_file_aio_read,
  627. .aio_write = cifs_file_aio_write,
  628. .open = cifs_open,
  629. .release = cifs_close,
  630. .lock = cifs_lock,
  631. .fsync = cifs_fsync,
  632. .flush = cifs_flush,
  633. .mmap = cifs_file_mmap,
  634. .splice_read = generic_file_splice_read,
  635. .llseek = cifs_llseek,
  636. #ifdef CONFIG_CIFS_POSIX
  637. .unlocked_ioctl = cifs_ioctl,
  638. #endif /* CONFIG_CIFS_POSIX */
  639. .setlease = cifs_setlease,
  640. };
  641. const struct file_operations cifs_file_direct_ops = {
  642. /* no aio, no readv -
  643. BB reevaluate whether they can be done with directio, no cache */
  644. .read = cifs_user_read,
  645. .write = cifs_user_write,
  646. .open = cifs_open,
  647. .release = cifs_close,
  648. .lock = cifs_lock,
  649. .fsync = cifs_fsync,
  650. .flush = cifs_flush,
  651. .mmap = cifs_file_mmap,
  652. .splice_read = generic_file_splice_read,
  653. #ifdef CONFIG_CIFS_POSIX
  654. .unlocked_ioctl = cifs_ioctl,
  655. #endif /* CONFIG_CIFS_POSIX */
  656. .llseek = cifs_llseek,
  657. .setlease = cifs_setlease,
  658. };
  659. const struct file_operations cifs_file_nobrl_ops = {
  660. .read = do_sync_read,
  661. .write = do_sync_write,
  662. .aio_read = generic_file_aio_read,
  663. .aio_write = cifs_file_aio_write,
  664. .open = cifs_open,
  665. .release = cifs_close,
  666. .fsync = cifs_fsync,
  667. .flush = cifs_flush,
  668. .mmap = cifs_file_mmap,
  669. .splice_read = generic_file_splice_read,
  670. .llseek = cifs_llseek,
  671. #ifdef CONFIG_CIFS_POSIX
  672. .unlocked_ioctl = cifs_ioctl,
  673. #endif /* CONFIG_CIFS_POSIX */
  674. .setlease = cifs_setlease,
  675. };
  676. const struct file_operations cifs_file_direct_nobrl_ops = {
  677. /* no mmap, no aio, no readv -
  678. BB reevaluate whether they can be done with directio, no cache */
  679. .read = cifs_user_read,
  680. .write = cifs_user_write,
  681. .open = cifs_open,
  682. .release = cifs_close,
  683. .fsync = cifs_fsync,
  684. .flush = cifs_flush,
  685. .mmap = cifs_file_mmap,
  686. .splice_read = generic_file_splice_read,
  687. #ifdef CONFIG_CIFS_POSIX
  688. .unlocked_ioctl = cifs_ioctl,
  689. #endif /* CONFIG_CIFS_POSIX */
  690. .llseek = cifs_llseek,
  691. .setlease = cifs_setlease,
  692. };
  693. const struct file_operations cifs_dir_ops = {
  694. .readdir = cifs_readdir,
  695. .release = cifs_closedir,
  696. .read = generic_read_dir,
  697. .unlocked_ioctl = cifs_ioctl,
  698. .llseek = generic_file_llseek,
  699. };
  700. static void
  701. cifs_init_once(void *inode)
  702. {
  703. struct cifsInodeInfo *cifsi = inode;
  704. inode_init_once(&cifsi->vfs_inode);
  705. INIT_LIST_HEAD(&cifsi->lockList);
  706. }
  707. static int
  708. cifs_init_inodecache(void)
  709. {
  710. cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
  711. sizeof(struct cifsInodeInfo),
  712. 0, (SLAB_RECLAIM_ACCOUNT|
  713. SLAB_MEM_SPREAD),
  714. cifs_init_once);
  715. if (cifs_inode_cachep == NULL)
  716. return -ENOMEM;
  717. return 0;
  718. }
  719. static void
  720. cifs_destroy_inodecache(void)
  721. {
  722. kmem_cache_destroy(cifs_inode_cachep);
  723. }
  724. static int
  725. cifs_init_request_bufs(void)
  726. {
  727. if (CIFSMaxBufSize < 8192) {
  728. /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
  729. Unicode path name has to fit in any SMB/CIFS path based frames */
  730. CIFSMaxBufSize = 8192;
  731. } else if (CIFSMaxBufSize > 1024*127) {
  732. CIFSMaxBufSize = 1024 * 127;
  733. } else {
  734. CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
  735. }
  736. /* cERROR(1, "CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize); */
  737. cifs_req_cachep = kmem_cache_create("cifs_request",
  738. CIFSMaxBufSize +
  739. MAX_CIFS_HDR_SIZE, 0,
  740. SLAB_HWCACHE_ALIGN, NULL);
  741. if (cifs_req_cachep == NULL)
  742. return -ENOMEM;
  743. if (cifs_min_rcv < 1)
  744. cifs_min_rcv = 1;
  745. else if (cifs_min_rcv > 64) {
  746. cifs_min_rcv = 64;
  747. cERROR(1, "cifs_min_rcv set to maximum (64)");
  748. }
  749. cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
  750. cifs_req_cachep);
  751. if (cifs_req_poolp == NULL) {
  752. kmem_cache_destroy(cifs_req_cachep);
  753. return -ENOMEM;
  754. }
  755. /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
  756. almost all handle based requests (but not write response, nor is it
  757. sufficient for path based requests). A smaller size would have
  758. been more efficient (compacting multiple slab items on one 4k page)
  759. for the case in which debug was on, but this larger size allows
  760. more SMBs to use small buffer alloc and is still much more
  761. efficient to alloc 1 per page off the slab compared to 17K (5page)
  762. alloc of large cifs buffers even when page debugging is on */
  763. cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
  764. MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
  765. NULL);
  766. if (cifs_sm_req_cachep == NULL) {
  767. mempool_destroy(cifs_req_poolp);
  768. kmem_cache_destroy(cifs_req_cachep);
  769. return -ENOMEM;
  770. }
  771. if (cifs_min_small < 2)
  772. cifs_min_small = 2;
  773. else if (cifs_min_small > 256) {
  774. cifs_min_small = 256;
  775. cFYI(1, "cifs_min_small set to maximum (256)");
  776. }
  777. cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
  778. cifs_sm_req_cachep);
  779. if (cifs_sm_req_poolp == NULL) {
  780. mempool_destroy(cifs_req_poolp);
  781. kmem_cache_destroy(cifs_req_cachep);
  782. kmem_cache_destroy(cifs_sm_req_cachep);
  783. return -ENOMEM;
  784. }
  785. return 0;
  786. }
  787. static void
  788. cifs_destroy_request_bufs(void)
  789. {
  790. mempool_destroy(cifs_req_poolp);
  791. kmem_cache_destroy(cifs_req_cachep);
  792. mempool_destroy(cifs_sm_req_poolp);
  793. kmem_cache_destroy(cifs_sm_req_cachep);
  794. }
  795. static int
  796. cifs_init_mids(void)
  797. {
  798. cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
  799. sizeof(struct mid_q_entry), 0,
  800. SLAB_HWCACHE_ALIGN, NULL);
  801. if (cifs_mid_cachep == NULL)
  802. return -ENOMEM;
  803. /* 3 is a reasonable minimum number of simultaneous operations */
  804. cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
  805. if (cifs_mid_poolp == NULL) {
  806. kmem_cache_destroy(cifs_mid_cachep);
  807. return -ENOMEM;
  808. }
  809. return 0;
  810. }
  811. static void
  812. cifs_destroy_mids(void)
  813. {
  814. mempool_destroy(cifs_mid_poolp);
  815. kmem_cache_destroy(cifs_mid_cachep);
  816. }
  817. static int __init
  818. init_cifs(void)
  819. {
  820. int rc = 0;
  821. cifs_proc_init();
  822. INIT_LIST_HEAD(&cifs_tcp_ses_list);
  823. #ifdef CONFIG_CIFS_EXPERIMENTAL
  824. INIT_LIST_HEAD(&GlobalDnotifyReqList);
  825. INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
  826. #endif
  827. /*
  828. * Initialize Global counters
  829. */
  830. atomic_set(&sesInfoAllocCount, 0);
  831. atomic_set(&tconInfoAllocCount, 0);
  832. atomic_set(&tcpSesAllocCount, 0);
  833. atomic_set(&tcpSesReconnectCount, 0);
  834. atomic_set(&tconInfoReconnectCount, 0);
  835. atomic_set(&bufAllocCount, 0);
  836. atomic_set(&smBufAllocCount, 0);
  837. #ifdef CONFIG_CIFS_STATS2
  838. atomic_set(&totBufAllocCount, 0);
  839. atomic_set(&totSmBufAllocCount, 0);
  840. #endif /* CONFIG_CIFS_STATS2 */
  841. atomic_set(&midCount, 0);
  842. GlobalCurrentXid = 0;
  843. GlobalTotalActiveXid = 0;
  844. GlobalMaxActiveXid = 0;
  845. spin_lock_init(&cifs_tcp_ses_lock);
  846. spin_lock_init(&cifs_file_list_lock);
  847. spin_lock_init(&GlobalMid_Lock);
  848. if (cifs_max_pending < 2) {
  849. cifs_max_pending = 2;
  850. cFYI(1, "cifs_max_pending set to min of 2");
  851. } else if (cifs_max_pending > 256) {
  852. cifs_max_pending = 256;
  853. cFYI(1, "cifs_max_pending set to max of 256");
  854. }
  855. rc = cifs_fscache_register();
  856. if (rc)
  857. goto out_clean_proc;
  858. rc = cifs_init_inodecache();
  859. if (rc)
  860. goto out_unreg_fscache;
  861. rc = cifs_init_mids();
  862. if (rc)
  863. goto out_destroy_inodecache;
  864. rc = cifs_init_request_bufs();
  865. if (rc)
  866. goto out_destroy_mids;
  867. rc = register_filesystem(&cifs_fs_type);
  868. if (rc)
  869. goto out_destroy_request_bufs;
  870. #ifdef CONFIG_CIFS_UPCALL
  871. rc = register_key_type(&cifs_spnego_key_type);
  872. if (rc)
  873. goto out_unregister_filesystem;
  874. #endif
  875. return 0;
  876. #ifdef CONFIG_CIFS_UPCALL
  877. out_unregister_filesystem:
  878. unregister_filesystem(&cifs_fs_type);
  879. #endif
  880. out_destroy_request_bufs:
  881. cifs_destroy_request_bufs();
  882. out_destroy_mids:
  883. cifs_destroy_mids();
  884. out_destroy_inodecache:
  885. cifs_destroy_inodecache();
  886. out_unreg_fscache:
  887. cifs_fscache_unregister();
  888. out_clean_proc:
  889. cifs_proc_clean();
  890. return rc;
  891. }
  892. static void __exit
  893. exit_cifs(void)
  894. {
  895. cFYI(DBG2, "exit_cifs");
  896. cifs_proc_clean();
  897. cifs_fscache_unregister();
  898. #ifdef CONFIG_CIFS_DFS_UPCALL
  899. cifs_dfs_release_automount_timer();
  900. #endif
  901. #ifdef CONFIG_CIFS_UPCALL
  902. unregister_key_type(&cifs_spnego_key_type);
  903. #endif
  904. unregister_filesystem(&cifs_fs_type);
  905. cifs_destroy_inodecache();
  906. cifs_destroy_mids();
  907. cifs_destroy_request_bufs();
  908. }
  909. MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
  910. MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
  911. MODULE_DESCRIPTION
  912. ("VFS to access servers complying with the SNIA CIFS Specification "
  913. "e.g. Samba and Windows");
  914. MODULE_VERSION(CIFS_VERSION);
  915. module_init(init_cifs)
  916. module_exit(exit_cifs)