cifsfs.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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 "cifsfs.h"
  37. #include "cifspdu.h"
  38. #define DECLARE_GLOBALS_HERE
  39. #include "cifsglob.h"
  40. #include "cifsproto.h"
  41. #include "cifs_debug.h"
  42. #include "cifs_fs_sb.h"
  43. #include <linux/mm.h>
  44. #include <linux/key-type.h>
  45. #include "dns_resolve.h"
  46. #include "cifs_spnego.h"
  47. #define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDUs */
  48. #ifdef CONFIG_CIFS_QUOTA
  49. static struct quotactl_ops cifs_quotactl_ops;
  50. #endif /* QUOTA */
  51. int cifsFYI = 0;
  52. int cifsERROR = 1;
  53. int traceSMB = 0;
  54. unsigned int oplockEnabled = 1;
  55. unsigned int experimEnabled = 0;
  56. unsigned int linuxExtEnabled = 1;
  57. unsigned int lookupCacheEnabled = 1;
  58. unsigned int multiuser_mount = 0;
  59. unsigned int extended_security = CIFSSEC_DEF;
  60. /* unsigned int ntlmv2_support = 0; */
  61. unsigned int sign_CIFS_PDUs = 1;
  62. extern struct task_struct *oplockThread; /* remove sparse warning */
  63. struct task_struct *oplockThread = NULL;
  64. /* extern struct task_struct * dnotifyThread; remove sparse warning */
  65. static struct task_struct *dnotifyThread = NULL;
  66. static const struct super_operations cifs_super_ops;
  67. unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
  68. module_param(CIFSMaxBufSize, int, 0);
  69. MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header). "
  70. "Default: 16384 Range: 8192 to 130048");
  71. unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
  72. module_param(cifs_min_rcv, int, 0);
  73. MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
  74. "1 to 64");
  75. unsigned int cifs_min_small = 30;
  76. module_param(cifs_min_small, int, 0);
  77. MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
  78. "Range: 2 to 256");
  79. unsigned int cifs_max_pending = CIFS_MAX_REQ;
  80. module_param(cifs_max_pending, int, 0);
  81. MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server. "
  82. "Default: 50 Range: 2 to 256");
  83. extern mempool_t *cifs_sm_req_poolp;
  84. extern mempool_t *cifs_req_poolp;
  85. extern mempool_t *cifs_mid_poolp;
  86. extern struct kmem_cache *cifs_oplock_cachep;
  87. static int
  88. cifs_read_super(struct super_block *sb, void *data,
  89. const char *devname, int silent)
  90. {
  91. struct inode *inode;
  92. struct cifs_sb_info *cifs_sb;
  93. #ifdef CONFIG_CIFS_DFS_UPCALL
  94. int len;
  95. #endif
  96. int rc = 0;
  97. /* BB should we make this contingent on mount parm? */
  98. sb->s_flags |= MS_NODIRATIME | MS_NOATIME;
  99. sb->s_fs_info = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
  100. cifs_sb = CIFS_SB(sb);
  101. if (cifs_sb == NULL)
  102. return -ENOMEM;
  103. #ifdef CONFIG_CIFS_DFS_UPCALL
  104. /* copy mount params to sb for use in submounts */
  105. /* BB: should we move this after the mount so we
  106. * do not have to do the copy on failed mounts?
  107. * BB: May be it is better to do simple copy before
  108. * complex operation (mount), and in case of fail
  109. * just exit instead of doing mount and attempting
  110. * undo it if this copy fails?*/
  111. len = strlen(data);
  112. cifs_sb->mountdata = kzalloc(len + 1, GFP_KERNEL);
  113. if (cifs_sb->mountdata == NULL) {
  114. kfree(sb->s_fs_info);
  115. sb->s_fs_info = NULL;
  116. return -ENOMEM;
  117. }
  118. strncpy(cifs_sb->mountdata, data, len + 1);
  119. cifs_sb->mountdata[len] = '\0';
  120. #endif
  121. rc = cifs_mount(sb, cifs_sb, data, devname);
  122. if (rc) {
  123. if (!silent)
  124. cERROR(1,
  125. ("cifs_mount failed w/return code = %d", rc));
  126. goto out_mount_failed;
  127. }
  128. sb->s_magic = CIFS_MAGIC_NUMBER;
  129. sb->s_op = &cifs_super_ops;
  130. /* if (cifs_sb->tcon->ses->server->maxBuf > MAX_CIFS_HDR_SIZE + 512)
  131. sb->s_blocksize =
  132. cifs_sb->tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE; */
  133. #ifdef CONFIG_CIFS_QUOTA
  134. sb->s_qcop = &cifs_quotactl_ops;
  135. #endif
  136. sb->s_blocksize = CIFS_MAX_MSGSIZE;
  137. sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
  138. inode = cifs_iget(sb, ROOT_I);
  139. if (IS_ERR(inode)) {
  140. rc = PTR_ERR(inode);
  141. inode = NULL;
  142. goto out_no_root;
  143. }
  144. sb->s_root = d_alloc_root(inode);
  145. if (!sb->s_root) {
  146. rc = -ENOMEM;
  147. goto out_no_root;
  148. }
  149. #ifdef CONFIG_CIFS_EXPERIMENTAL
  150. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  151. cFYI(1, ("export ops supported"));
  152. sb->s_export_op = &cifs_export_ops;
  153. }
  154. #endif /* EXPERIMENTAL */
  155. return 0;
  156. out_no_root:
  157. cERROR(1, ("cifs_read_super: get root inode failed"));
  158. if (inode)
  159. iput(inode);
  160. out_mount_failed:
  161. if (cifs_sb) {
  162. #ifdef CONFIG_CIFS_DFS_UPCALL
  163. if (cifs_sb->mountdata) {
  164. kfree(cifs_sb->mountdata);
  165. cifs_sb->mountdata = NULL;
  166. }
  167. #endif
  168. if (cifs_sb->local_nls)
  169. unload_nls(cifs_sb->local_nls);
  170. kfree(cifs_sb);
  171. }
  172. return rc;
  173. }
  174. static void
  175. cifs_put_super(struct super_block *sb)
  176. {
  177. int rc = 0;
  178. struct cifs_sb_info *cifs_sb;
  179. cFYI(1, ("In cifs_put_super"));
  180. cifs_sb = CIFS_SB(sb);
  181. if (cifs_sb == NULL) {
  182. cFYI(1, ("Empty cifs superblock info passed to unmount"));
  183. return;
  184. }
  185. rc = cifs_umount(sb, cifs_sb);
  186. if (rc)
  187. cERROR(1, ("cifs_umount failed with return code %d", rc));
  188. #ifdef CONFIG_CIFS_DFS_UPCALL
  189. if (cifs_sb->mountdata) {
  190. kfree(cifs_sb->mountdata);
  191. cifs_sb->mountdata = NULL;
  192. }
  193. #endif
  194. unload_nls(cifs_sb->local_nls);
  195. kfree(cifs_sb);
  196. return;
  197. }
  198. static int
  199. cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
  200. {
  201. struct super_block *sb = dentry->d_sb;
  202. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  203. struct cifsTconInfo *tcon = cifs_sb->tcon;
  204. int rc = -EOPNOTSUPP;
  205. int xid;
  206. xid = GetXid();
  207. buf->f_type = CIFS_MAGIC_NUMBER;
  208. /*
  209. * PATH_MAX may be too long - it would presumably be total path,
  210. * but note that some servers (includinng Samba 3) have a shorter
  211. * maximum path.
  212. *
  213. * Instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO.
  214. */
  215. buf->f_namelen = PATH_MAX;
  216. buf->f_files = 0; /* undefined */
  217. buf->f_ffree = 0; /* unlimited */
  218. /*
  219. * We could add a second check for a QFS Unix capability bit
  220. */
  221. if ((tcon->ses->capabilities & CAP_UNIX) &&
  222. (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
  223. rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
  224. /*
  225. * Only need to call the old QFSInfo if failed on newer one,
  226. * e.g. by OS/2.
  227. **/
  228. if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
  229. rc = CIFSSMBQFSInfo(xid, tcon, buf);
  230. /*
  231. * Some old Windows servers also do not support level 103, retry with
  232. * older level one if old server failed the previous call or we
  233. * bypassed it because we detected that this was an older LANMAN sess
  234. */
  235. if (rc)
  236. rc = SMBOldQFSInfo(xid, tcon, buf);
  237. FreeXid(xid);
  238. return 0;
  239. }
  240. static int cifs_permission(struct inode *inode, int mask, struct nameidata *nd)
  241. {
  242. struct cifs_sb_info *cifs_sb;
  243. cifs_sb = CIFS_SB(inode->i_sb);
  244. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
  245. return 0;
  246. else /* file mode might have been restricted at mount time
  247. on the client (above and beyond ACL on servers) for
  248. servers which do not support setting and viewing mode bits,
  249. so allowing client to check permissions is useful */
  250. return generic_permission(inode, mask, NULL);
  251. }
  252. static struct kmem_cache *cifs_inode_cachep;
  253. static struct kmem_cache *cifs_req_cachep;
  254. static struct kmem_cache *cifs_mid_cachep;
  255. struct kmem_cache *cifs_oplock_cachep;
  256. static struct kmem_cache *cifs_sm_req_cachep;
  257. mempool_t *cifs_sm_req_poolp;
  258. mempool_t *cifs_req_poolp;
  259. mempool_t *cifs_mid_poolp;
  260. static struct inode *
  261. cifs_alloc_inode(struct super_block *sb)
  262. {
  263. struct cifsInodeInfo *cifs_inode;
  264. cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
  265. if (!cifs_inode)
  266. return NULL;
  267. cifs_inode->cifsAttrs = 0x20; /* default */
  268. atomic_set(&cifs_inode->inUse, 0);
  269. cifs_inode->time = 0;
  270. cifs_inode->write_behind_rc = 0;
  271. /* Until the file is open and we have gotten oplock
  272. info back from the server, can not assume caching of
  273. file data or metadata */
  274. cifs_inode->clientCanCacheRead = false;
  275. cifs_inode->clientCanCacheAll = false;
  276. cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
  277. /* Can not set i_flags here - they get immediately overwritten
  278. to zero by the VFS */
  279. /* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;*/
  280. INIT_LIST_HEAD(&cifs_inode->openFileList);
  281. return &cifs_inode->vfs_inode;
  282. }
  283. static void
  284. cifs_destroy_inode(struct inode *inode)
  285. {
  286. kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
  287. }
  288. /*
  289. * cifs_show_options() is for displaying mount options in /proc/mounts.
  290. * Not all settable options are displayed but most of the important
  291. * ones are.
  292. */
  293. static int
  294. cifs_show_options(struct seq_file *s, struct vfsmount *m)
  295. {
  296. struct cifs_sb_info *cifs_sb;
  297. cifs_sb = CIFS_SB(m->mnt_sb);
  298. if (cifs_sb) {
  299. if (cifs_sb->tcon) {
  300. /* BB add prepath to mount options displayed */
  301. seq_printf(s, ",unc=%s", cifs_sb->tcon->treeName);
  302. if (cifs_sb->tcon->ses) {
  303. if (cifs_sb->tcon->ses->userName)
  304. seq_printf(s, ",username=%s",
  305. cifs_sb->tcon->ses->userName);
  306. if (cifs_sb->tcon->ses->domainName)
  307. seq_printf(s, ",domain=%s",
  308. cifs_sb->tcon->ses->domainName);
  309. }
  310. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) ||
  311. !(cifs_sb->tcon->unix_ext))
  312. seq_printf(s, ",uid=%d", cifs_sb->mnt_uid);
  313. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) ||
  314. !(cifs_sb->tcon->unix_ext))
  315. seq_printf(s, ",gid=%d", cifs_sb->mnt_gid);
  316. if (!cifs_sb->tcon->unix_ext) {
  317. seq_printf(s, ",file_mode=0%o,dir_mode=0%o",
  318. cifs_sb->mnt_file_mode,
  319. cifs_sb->mnt_dir_mode);
  320. }
  321. if (cifs_sb->tcon->seal)
  322. seq_printf(s, ",seal");
  323. if (cifs_sb->tcon->nocase)
  324. seq_printf(s, ",nocase");
  325. if (cifs_sb->tcon->retry)
  326. seq_printf(s, ",hard");
  327. }
  328. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
  329. seq_printf(s, ",posixpaths");
  330. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
  331. seq_printf(s, ",setuids");
  332. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  333. seq_printf(s, ",serverino");
  334. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
  335. seq_printf(s, ",directio");
  336. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  337. seq_printf(s, ",nouser_xattr");
  338. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
  339. seq_printf(s, ",mapchars");
  340. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
  341. seq_printf(s, ",sfu");
  342. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  343. seq_printf(s, ",nobrl");
  344. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
  345. seq_printf(s, ",cifsacl");
  346. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  347. seq_printf(s, ",dynperm");
  348. if (m->mnt_sb->s_flags & MS_POSIXACL)
  349. seq_printf(s, ",acl");
  350. seq_printf(s, ",rsize=%d", cifs_sb->rsize);
  351. seq_printf(s, ",wsize=%d", cifs_sb->wsize);
  352. }
  353. return 0;
  354. }
  355. #ifdef CONFIG_CIFS_QUOTA
  356. int cifs_xquota_set(struct super_block *sb, int quota_type, qid_t qid,
  357. struct fs_disk_quota *pdquota)
  358. {
  359. int xid;
  360. int rc = 0;
  361. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  362. struct cifsTconInfo *pTcon;
  363. if (cifs_sb)
  364. pTcon = cifs_sb->tcon;
  365. else
  366. return -EIO;
  367. xid = GetXid();
  368. if (pTcon) {
  369. cFYI(1, ("set type: 0x%x id: %d", quota_type, qid));
  370. } else {
  371. rc = -EIO;
  372. }
  373. FreeXid(xid);
  374. return rc;
  375. }
  376. int cifs_xquota_get(struct super_block *sb, int quota_type, qid_t qid,
  377. struct fs_disk_quota *pdquota)
  378. {
  379. int xid;
  380. int rc = 0;
  381. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  382. struct cifsTconInfo *pTcon;
  383. if (cifs_sb)
  384. pTcon = cifs_sb->tcon;
  385. else
  386. return -EIO;
  387. xid = GetXid();
  388. if (pTcon) {
  389. cFYI(1, ("set type: 0x%x id: %d", quota_type, qid));
  390. } else {
  391. rc = -EIO;
  392. }
  393. FreeXid(xid);
  394. return rc;
  395. }
  396. int cifs_xstate_set(struct super_block *sb, unsigned int flags, int operation)
  397. {
  398. int xid;
  399. int rc = 0;
  400. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  401. struct cifsTconInfo *pTcon;
  402. if (cifs_sb)
  403. pTcon = cifs_sb->tcon;
  404. else
  405. return -EIO;
  406. xid = GetXid();
  407. if (pTcon) {
  408. cFYI(1, ("flags: 0x%x operation: 0x%x", flags, operation));
  409. } else {
  410. rc = -EIO;
  411. }
  412. FreeXid(xid);
  413. return rc;
  414. }
  415. int cifs_xstate_get(struct super_block *sb, struct fs_quota_stat *qstats)
  416. {
  417. int xid;
  418. int rc = 0;
  419. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  420. struct cifsTconInfo *pTcon;
  421. if (cifs_sb) {
  422. pTcon = cifs_sb->tcon;
  423. } else {
  424. return -EIO;
  425. }
  426. xid = GetXid();
  427. if (pTcon) {
  428. cFYI(1, ("pqstats %p", qstats));
  429. } else {
  430. rc = -EIO;
  431. }
  432. FreeXid(xid);
  433. return rc;
  434. }
  435. static struct quotactl_ops cifs_quotactl_ops = {
  436. .set_xquota = cifs_xquota_set,
  437. .get_xquota = cifs_xquota_get,
  438. .set_xstate = cifs_xstate_set,
  439. .get_xstate = cifs_xstate_get,
  440. };
  441. #endif
  442. static void cifs_umount_begin(struct super_block *sb)
  443. {
  444. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  445. struct cifsTconInfo *tcon;
  446. if (cifs_sb == NULL)
  447. return;
  448. tcon = cifs_sb->tcon;
  449. if (tcon == NULL)
  450. return;
  451. down(&tcon->tconSem);
  452. if (atomic_read(&tcon->useCount) == 1)
  453. tcon->tidStatus = CifsExiting;
  454. up(&tcon->tconSem);
  455. /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
  456. /* cancel_notify_requests(tcon); */
  457. if (tcon->ses && tcon->ses->server) {
  458. cFYI(1, ("wake up tasks now - umount begin not complete"));
  459. wake_up_all(&tcon->ses->server->request_q);
  460. wake_up_all(&tcon->ses->server->response_q);
  461. msleep(1); /* yield */
  462. /* we have to kick the requests once more */
  463. wake_up_all(&tcon->ses->server->response_q);
  464. msleep(1);
  465. }
  466. /* BB FIXME - finish add checks for tidStatus BB */
  467. return;
  468. }
  469. #ifdef CONFIG_CIFS_STATS2
  470. static int cifs_show_stats(struct seq_file *s, struct vfsmount *mnt)
  471. {
  472. /* BB FIXME */
  473. return 0;
  474. }
  475. #endif
  476. static int cifs_remount(struct super_block *sb, int *flags, char *data)
  477. {
  478. *flags |= MS_NODIRATIME;
  479. return 0;
  480. }
  481. static const struct super_operations cifs_super_ops = {
  482. .put_super = cifs_put_super,
  483. .statfs = cifs_statfs,
  484. .alloc_inode = cifs_alloc_inode,
  485. .destroy_inode = cifs_destroy_inode,
  486. /* .drop_inode = generic_delete_inode,
  487. .delete_inode = cifs_delete_inode, */ /* Do not need above two
  488. functions unless later we add lazy close of inodes or unless the
  489. kernel forgets to call us with the same number of releases (closes)
  490. as opens */
  491. .show_options = cifs_show_options,
  492. .umount_begin = cifs_umount_begin,
  493. .remount_fs = cifs_remount,
  494. #ifdef CONFIG_CIFS_STATS2
  495. .show_stats = cifs_show_stats,
  496. #endif
  497. };
  498. static int
  499. cifs_get_sb(struct file_system_type *fs_type,
  500. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  501. {
  502. int rc;
  503. struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
  504. cFYI(1, ("Devname: %s flags: %d ", dev_name, flags));
  505. if (IS_ERR(sb))
  506. return PTR_ERR(sb);
  507. sb->s_flags = flags;
  508. rc = cifs_read_super(sb, data, dev_name, flags & MS_SILENT ? 1 : 0);
  509. if (rc) {
  510. up_write(&sb->s_umount);
  511. deactivate_super(sb);
  512. return rc;
  513. }
  514. sb->s_flags |= MS_ACTIVE;
  515. return simple_set_mnt(mnt, sb);
  516. }
  517. static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  518. unsigned long nr_segs, loff_t pos)
  519. {
  520. struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
  521. ssize_t written;
  522. written = generic_file_aio_write(iocb, iov, nr_segs, pos);
  523. if (!CIFS_I(inode)->clientCanCacheAll)
  524. filemap_fdatawrite(inode->i_mapping);
  525. return written;
  526. }
  527. static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
  528. {
  529. /* origin == SEEK_END => we must revalidate the cached file length */
  530. if (origin == SEEK_END) {
  531. int retval;
  532. /* some applications poll for the file length in this strange
  533. way so we must seek to end on non-oplocked files by
  534. setting the revalidate time to zero */
  535. CIFS_I(file->f_path.dentry->d_inode)->time = 0;
  536. retval = cifs_revalidate(file->f_path.dentry);
  537. if (retval < 0)
  538. return (loff_t)retval;
  539. }
  540. return remote_llseek(file, offset, origin);
  541. }
  542. struct file_system_type cifs_fs_type = {
  543. .owner = THIS_MODULE,
  544. .name = "cifs",
  545. .get_sb = cifs_get_sb,
  546. .kill_sb = kill_anon_super,
  547. /* .fs_flags */
  548. };
  549. const struct inode_operations cifs_dir_inode_ops = {
  550. .create = cifs_create,
  551. .lookup = cifs_lookup,
  552. .getattr = cifs_getattr,
  553. .unlink = cifs_unlink,
  554. .link = cifs_hardlink,
  555. .mkdir = cifs_mkdir,
  556. .rmdir = cifs_rmdir,
  557. .rename = cifs_rename,
  558. .permission = cifs_permission,
  559. /* revalidate:cifs_revalidate, */
  560. .setattr = cifs_setattr,
  561. .symlink = cifs_symlink,
  562. .mknod = cifs_mknod,
  563. #ifdef CONFIG_CIFS_XATTR
  564. .setxattr = cifs_setxattr,
  565. .getxattr = cifs_getxattr,
  566. .listxattr = cifs_listxattr,
  567. .removexattr = cifs_removexattr,
  568. #endif
  569. };
  570. const struct inode_operations cifs_file_inode_ops = {
  571. /* revalidate:cifs_revalidate, */
  572. .setattr = cifs_setattr,
  573. .getattr = cifs_getattr, /* do we need this anymore? */
  574. .rename = cifs_rename,
  575. .permission = cifs_permission,
  576. #ifdef CONFIG_CIFS_XATTR
  577. .setxattr = cifs_setxattr,
  578. .getxattr = cifs_getxattr,
  579. .listxattr = cifs_listxattr,
  580. .removexattr = cifs_removexattr,
  581. #endif
  582. };
  583. const struct inode_operations cifs_symlink_inode_ops = {
  584. .readlink = generic_readlink,
  585. .follow_link = cifs_follow_link,
  586. .put_link = cifs_put_link,
  587. .permission = cifs_permission,
  588. /* BB add the following two eventually */
  589. /* revalidate: cifs_revalidate,
  590. setattr: cifs_notify_change, *//* BB do we need notify change */
  591. #ifdef CONFIG_CIFS_XATTR
  592. .setxattr = cifs_setxattr,
  593. .getxattr = cifs_getxattr,
  594. .listxattr = cifs_listxattr,
  595. .removexattr = cifs_removexattr,
  596. #endif
  597. };
  598. const struct file_operations cifs_file_ops = {
  599. .read = do_sync_read,
  600. .write = do_sync_write,
  601. .aio_read = generic_file_aio_read,
  602. .aio_write = cifs_file_aio_write,
  603. .open = cifs_open,
  604. .release = cifs_close,
  605. .lock = cifs_lock,
  606. .fsync = cifs_fsync,
  607. .flush = cifs_flush,
  608. .mmap = cifs_file_mmap,
  609. .splice_read = generic_file_splice_read,
  610. .llseek = cifs_llseek,
  611. #ifdef CONFIG_CIFS_POSIX
  612. .unlocked_ioctl = cifs_ioctl,
  613. #endif /* CONFIG_CIFS_POSIX */
  614. #ifdef CONFIG_CIFS_EXPERIMENTAL
  615. .dir_notify = cifs_dir_notify,
  616. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  617. };
  618. const struct file_operations cifs_file_direct_ops = {
  619. /* no mmap, no aio, no readv -
  620. BB reevaluate whether they can be done with directio, no cache */
  621. .read = cifs_user_read,
  622. .write = cifs_user_write,
  623. .open = cifs_open,
  624. .release = cifs_close,
  625. .lock = cifs_lock,
  626. .fsync = cifs_fsync,
  627. .flush = cifs_flush,
  628. .splice_read = generic_file_splice_read,
  629. #ifdef CONFIG_CIFS_POSIX
  630. .unlocked_ioctl = cifs_ioctl,
  631. #endif /* CONFIG_CIFS_POSIX */
  632. .llseek = cifs_llseek,
  633. #ifdef CONFIG_CIFS_EXPERIMENTAL
  634. .dir_notify = cifs_dir_notify,
  635. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  636. };
  637. const struct file_operations cifs_file_nobrl_ops = {
  638. .read = do_sync_read,
  639. .write = do_sync_write,
  640. .aio_read = generic_file_aio_read,
  641. .aio_write = cifs_file_aio_write,
  642. .open = cifs_open,
  643. .release = cifs_close,
  644. .fsync = cifs_fsync,
  645. .flush = cifs_flush,
  646. .mmap = cifs_file_mmap,
  647. .splice_read = generic_file_splice_read,
  648. .llseek = cifs_llseek,
  649. #ifdef CONFIG_CIFS_POSIX
  650. .unlocked_ioctl = cifs_ioctl,
  651. #endif /* CONFIG_CIFS_POSIX */
  652. #ifdef CONFIG_CIFS_EXPERIMENTAL
  653. .dir_notify = cifs_dir_notify,
  654. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  655. };
  656. const struct file_operations cifs_file_direct_nobrl_ops = {
  657. /* no mmap, no aio, no readv -
  658. BB reevaluate whether they can be done with directio, no cache */
  659. .read = cifs_user_read,
  660. .write = cifs_user_write,
  661. .open = cifs_open,
  662. .release = cifs_close,
  663. .fsync = cifs_fsync,
  664. .flush = cifs_flush,
  665. .splice_read = generic_file_splice_read,
  666. #ifdef CONFIG_CIFS_POSIX
  667. .unlocked_ioctl = cifs_ioctl,
  668. #endif /* CONFIG_CIFS_POSIX */
  669. .llseek = cifs_llseek,
  670. #ifdef CONFIG_CIFS_EXPERIMENTAL
  671. .dir_notify = cifs_dir_notify,
  672. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  673. };
  674. const struct file_operations cifs_dir_ops = {
  675. .readdir = cifs_readdir,
  676. .release = cifs_closedir,
  677. .read = generic_read_dir,
  678. #ifdef CONFIG_CIFS_EXPERIMENTAL
  679. .dir_notify = cifs_dir_notify,
  680. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  681. .unlocked_ioctl = cifs_ioctl,
  682. };
  683. static void
  684. cifs_init_once(struct kmem_cache *cachep, void *inode)
  685. {
  686. struct cifsInodeInfo *cifsi = inode;
  687. inode_init_once(&cifsi->vfs_inode);
  688. INIT_LIST_HEAD(&cifsi->lockList);
  689. }
  690. static int
  691. cifs_init_inodecache(void)
  692. {
  693. cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
  694. sizeof(struct cifsInodeInfo),
  695. 0, (SLAB_RECLAIM_ACCOUNT|
  696. SLAB_MEM_SPREAD),
  697. cifs_init_once);
  698. if (cifs_inode_cachep == NULL)
  699. return -ENOMEM;
  700. return 0;
  701. }
  702. static void
  703. cifs_destroy_inodecache(void)
  704. {
  705. kmem_cache_destroy(cifs_inode_cachep);
  706. }
  707. static int
  708. cifs_init_request_bufs(void)
  709. {
  710. if (CIFSMaxBufSize < 8192) {
  711. /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
  712. Unicode path name has to fit in any SMB/CIFS path based frames */
  713. CIFSMaxBufSize = 8192;
  714. } else if (CIFSMaxBufSize > 1024*127) {
  715. CIFSMaxBufSize = 1024 * 127;
  716. } else {
  717. CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
  718. }
  719. /* cERROR(1,("CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize)); */
  720. cifs_req_cachep = kmem_cache_create("cifs_request",
  721. CIFSMaxBufSize +
  722. MAX_CIFS_HDR_SIZE, 0,
  723. SLAB_HWCACHE_ALIGN, NULL);
  724. if (cifs_req_cachep == NULL)
  725. return -ENOMEM;
  726. if (cifs_min_rcv < 1)
  727. cifs_min_rcv = 1;
  728. else if (cifs_min_rcv > 64) {
  729. cifs_min_rcv = 64;
  730. cERROR(1, ("cifs_min_rcv set to maximum (64)"));
  731. }
  732. cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
  733. cifs_req_cachep);
  734. if (cifs_req_poolp == NULL) {
  735. kmem_cache_destroy(cifs_req_cachep);
  736. return -ENOMEM;
  737. }
  738. /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
  739. almost all handle based requests (but not write response, nor is it
  740. sufficient for path based requests). A smaller size would have
  741. been more efficient (compacting multiple slab items on one 4k page)
  742. for the case in which debug was on, but this larger size allows
  743. more SMBs to use small buffer alloc and is still much more
  744. efficient to alloc 1 per page off the slab compared to 17K (5page)
  745. alloc of large cifs buffers even when page debugging is on */
  746. cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
  747. MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
  748. NULL);
  749. if (cifs_sm_req_cachep == NULL) {
  750. mempool_destroy(cifs_req_poolp);
  751. kmem_cache_destroy(cifs_req_cachep);
  752. return -ENOMEM;
  753. }
  754. if (cifs_min_small < 2)
  755. cifs_min_small = 2;
  756. else if (cifs_min_small > 256) {
  757. cifs_min_small = 256;
  758. cFYI(1, ("cifs_min_small set to maximum (256)"));
  759. }
  760. cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
  761. cifs_sm_req_cachep);
  762. if (cifs_sm_req_poolp == NULL) {
  763. mempool_destroy(cifs_req_poolp);
  764. kmem_cache_destroy(cifs_req_cachep);
  765. kmem_cache_destroy(cifs_sm_req_cachep);
  766. return -ENOMEM;
  767. }
  768. return 0;
  769. }
  770. static void
  771. cifs_destroy_request_bufs(void)
  772. {
  773. mempool_destroy(cifs_req_poolp);
  774. kmem_cache_destroy(cifs_req_cachep);
  775. mempool_destroy(cifs_sm_req_poolp);
  776. kmem_cache_destroy(cifs_sm_req_cachep);
  777. }
  778. static int
  779. cifs_init_mids(void)
  780. {
  781. cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
  782. sizeof(struct mid_q_entry), 0,
  783. SLAB_HWCACHE_ALIGN, NULL);
  784. if (cifs_mid_cachep == NULL)
  785. return -ENOMEM;
  786. /* 3 is a reasonable minimum number of simultaneous operations */
  787. cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
  788. if (cifs_mid_poolp == NULL) {
  789. kmem_cache_destroy(cifs_mid_cachep);
  790. return -ENOMEM;
  791. }
  792. cifs_oplock_cachep = kmem_cache_create("cifs_oplock_structs",
  793. sizeof(struct oplock_q_entry), 0,
  794. SLAB_HWCACHE_ALIGN, NULL);
  795. if (cifs_oplock_cachep == NULL) {
  796. mempool_destroy(cifs_mid_poolp);
  797. kmem_cache_destroy(cifs_mid_cachep);
  798. return -ENOMEM;
  799. }
  800. return 0;
  801. }
  802. static void
  803. cifs_destroy_mids(void)
  804. {
  805. mempool_destroy(cifs_mid_poolp);
  806. kmem_cache_destroy(cifs_mid_cachep);
  807. kmem_cache_destroy(cifs_oplock_cachep);
  808. }
  809. static int cifs_oplock_thread(void *dummyarg)
  810. {
  811. struct oplock_q_entry *oplock_item;
  812. struct cifsTconInfo *pTcon;
  813. struct inode *inode;
  814. __u16 netfid;
  815. int rc, waitrc = 0;
  816. set_freezable();
  817. do {
  818. if (try_to_freeze())
  819. continue;
  820. spin_lock(&GlobalMid_Lock);
  821. if (list_empty(&GlobalOplock_Q)) {
  822. spin_unlock(&GlobalMid_Lock);
  823. set_current_state(TASK_INTERRUPTIBLE);
  824. schedule_timeout(39*HZ);
  825. } else {
  826. oplock_item = list_entry(GlobalOplock_Q.next,
  827. struct oplock_q_entry, qhead);
  828. if (oplock_item) {
  829. cFYI(1, ("found oplock item to write out"));
  830. pTcon = oplock_item->tcon;
  831. inode = oplock_item->pinode;
  832. netfid = oplock_item->netfid;
  833. spin_unlock(&GlobalMid_Lock);
  834. DeleteOplockQEntry(oplock_item);
  835. /* can not grab inode sem here since it would
  836. deadlock when oplock received on delete
  837. since vfs_unlink holds the i_mutex across
  838. the call */
  839. /* mutex_lock(&inode->i_mutex);*/
  840. if (S_ISREG(inode->i_mode)) {
  841. rc =
  842. filemap_fdatawrite(inode->i_mapping);
  843. if (CIFS_I(inode)->clientCanCacheRead
  844. == 0) {
  845. waitrc = filemap_fdatawait(inode->i_mapping);
  846. invalidate_remote_inode(inode);
  847. }
  848. if (rc == 0)
  849. rc = waitrc;
  850. } else
  851. rc = 0;
  852. /* mutex_unlock(&inode->i_mutex);*/
  853. if (rc)
  854. CIFS_I(inode)->write_behind_rc = rc;
  855. cFYI(1, ("Oplock flush inode %p rc %d",
  856. inode, rc));
  857. /* releasing stale oplock after recent reconnect
  858. of smb session using a now incorrect file
  859. handle is not a data integrity issue but do
  860. not bother sending an oplock release if session
  861. to server still is disconnected since oplock
  862. already released by the server in that case */
  863. if (pTcon->tidStatus != CifsNeedReconnect) {
  864. rc = CIFSSMBLock(0, pTcon, netfid,
  865. 0 /* len */ , 0 /* offset */, 0,
  866. 0, LOCKING_ANDX_OPLOCK_RELEASE,
  867. false /* wait flag */);
  868. cFYI(1, ("Oplock release rc = %d", rc));
  869. }
  870. } else
  871. spin_unlock(&GlobalMid_Lock);
  872. set_current_state(TASK_INTERRUPTIBLE);
  873. schedule_timeout(1); /* yield in case q were corrupt */
  874. }
  875. } while (!kthread_should_stop());
  876. return 0;
  877. }
  878. static int cifs_dnotify_thread(void *dummyarg)
  879. {
  880. struct list_head *tmp;
  881. struct cifsSesInfo *ses;
  882. do {
  883. if (try_to_freeze())
  884. continue;
  885. set_current_state(TASK_INTERRUPTIBLE);
  886. schedule_timeout(15*HZ);
  887. read_lock(&GlobalSMBSeslock);
  888. /* check if any stuck requests that need
  889. to be woken up and wakeq so the
  890. thread can wake up and error out */
  891. list_for_each(tmp, &GlobalSMBSessionList) {
  892. ses = list_entry(tmp, struct cifsSesInfo,
  893. cifsSessionList);
  894. if (ses && ses->server &&
  895. atomic_read(&ses->server->inFlight))
  896. wake_up_all(&ses->server->response_q);
  897. }
  898. read_unlock(&GlobalSMBSeslock);
  899. } while (!kthread_should_stop());
  900. return 0;
  901. }
  902. static int __init
  903. init_cifs(void)
  904. {
  905. int rc = 0;
  906. cifs_proc_init();
  907. /* INIT_LIST_HEAD(&GlobalServerList);*/ /* BB not implemented yet */
  908. INIT_LIST_HEAD(&GlobalSMBSessionList);
  909. INIT_LIST_HEAD(&GlobalTreeConnectionList);
  910. INIT_LIST_HEAD(&GlobalOplock_Q);
  911. #ifdef CONFIG_CIFS_EXPERIMENTAL
  912. INIT_LIST_HEAD(&GlobalDnotifyReqList);
  913. INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
  914. #endif
  915. /*
  916. * Initialize Global counters
  917. */
  918. atomic_set(&sesInfoAllocCount, 0);
  919. atomic_set(&tconInfoAllocCount, 0);
  920. atomic_set(&tcpSesAllocCount, 0);
  921. atomic_set(&tcpSesReconnectCount, 0);
  922. atomic_set(&tconInfoReconnectCount, 0);
  923. atomic_set(&bufAllocCount, 0);
  924. atomic_set(&smBufAllocCount, 0);
  925. #ifdef CONFIG_CIFS_STATS2
  926. atomic_set(&totBufAllocCount, 0);
  927. atomic_set(&totSmBufAllocCount, 0);
  928. #endif /* CONFIG_CIFS_STATS2 */
  929. atomic_set(&midCount, 0);
  930. GlobalCurrentXid = 0;
  931. GlobalTotalActiveXid = 0;
  932. GlobalMaxActiveXid = 0;
  933. memset(Local_System_Name, 0, 15);
  934. rwlock_init(&GlobalSMBSeslock);
  935. spin_lock_init(&GlobalMid_Lock);
  936. if (cifs_max_pending < 2) {
  937. cifs_max_pending = 2;
  938. cFYI(1, ("cifs_max_pending set to min of 2"));
  939. } else if (cifs_max_pending > 256) {
  940. cifs_max_pending = 256;
  941. cFYI(1, ("cifs_max_pending set to max of 256"));
  942. }
  943. rc = cifs_init_inodecache();
  944. if (rc)
  945. goto out_clean_proc;
  946. rc = cifs_init_mids();
  947. if (rc)
  948. goto out_destroy_inodecache;
  949. rc = cifs_init_request_bufs();
  950. if (rc)
  951. goto out_destroy_mids;
  952. rc = register_filesystem(&cifs_fs_type);
  953. if (rc)
  954. goto out_destroy_request_bufs;
  955. #ifdef CONFIG_CIFS_UPCALL
  956. rc = register_key_type(&cifs_spnego_key_type);
  957. if (rc)
  958. goto out_unregister_filesystem;
  959. #endif
  960. #ifdef CONFIG_CIFS_DFS_UPCALL
  961. rc = register_key_type(&key_type_dns_resolver);
  962. if (rc)
  963. goto out_unregister_key_type;
  964. #endif
  965. oplockThread = kthread_run(cifs_oplock_thread, NULL, "cifsoplockd");
  966. if (IS_ERR(oplockThread)) {
  967. rc = PTR_ERR(oplockThread);
  968. cERROR(1, ("error %d create oplock thread", rc));
  969. goto out_unregister_dfs_key_type;
  970. }
  971. dnotifyThread = kthread_run(cifs_dnotify_thread, NULL, "cifsdnotifyd");
  972. if (IS_ERR(dnotifyThread)) {
  973. rc = PTR_ERR(dnotifyThread);
  974. cERROR(1, ("error %d create dnotify thread", rc));
  975. goto out_stop_oplock_thread;
  976. }
  977. return 0;
  978. out_stop_oplock_thread:
  979. kthread_stop(oplockThread);
  980. out_unregister_dfs_key_type:
  981. #ifdef CONFIG_CIFS_DFS_UPCALL
  982. unregister_key_type(&key_type_dns_resolver);
  983. out_unregister_key_type:
  984. #endif
  985. #ifdef CONFIG_CIFS_UPCALL
  986. unregister_key_type(&cifs_spnego_key_type);
  987. out_unregister_filesystem:
  988. #endif
  989. unregister_filesystem(&cifs_fs_type);
  990. out_destroy_request_bufs:
  991. cifs_destroy_request_bufs();
  992. out_destroy_mids:
  993. cifs_destroy_mids();
  994. out_destroy_inodecache:
  995. cifs_destroy_inodecache();
  996. out_clean_proc:
  997. cifs_proc_clean();
  998. return rc;
  999. }
  1000. static void __exit
  1001. exit_cifs(void)
  1002. {
  1003. cFYI(DBG2, ("exit_cifs"));
  1004. cifs_proc_clean();
  1005. #ifdef CONFIG_CIFS_DFS_UPCALL
  1006. cifs_dfs_release_automount_timer();
  1007. unregister_key_type(&key_type_dns_resolver);
  1008. #endif
  1009. #ifdef CONFIG_CIFS_UPCALL
  1010. unregister_key_type(&cifs_spnego_key_type);
  1011. #endif
  1012. unregister_filesystem(&cifs_fs_type);
  1013. cifs_destroy_inodecache();
  1014. cifs_destroy_mids();
  1015. cifs_destroy_request_bufs();
  1016. kthread_stop(oplockThread);
  1017. kthread_stop(dnotifyThread);
  1018. }
  1019. MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
  1020. MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
  1021. MODULE_DESCRIPTION
  1022. ("VFS to access servers complying with the SNIA CIFS Specification "
  1023. "e.g. Samba and Windows");
  1024. MODULE_VERSION(CIFS_VERSION);
  1025. module_init(init_cifs)
  1026. module_exit(exit_cifs)