cifsfs.c 27 KB

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