cifsfs.c 28 KB

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