cifsfs.c 24 KB

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