cifsfs.c 28 KB

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