cifsfs.c 24 KB

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