cifsfs.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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 struct file_system_type cifs_fs_type = {
  445. .owner = THIS_MODULE,
  446. .name = "cifs",
  447. .get_sb = cifs_get_sb,
  448. .kill_sb = kill_anon_super,
  449. /* .fs_flags */
  450. };
  451. struct inode_operations cifs_dir_inode_ops = {
  452. .create = cifs_create,
  453. .lookup = cifs_lookup,
  454. .getattr = cifs_getattr,
  455. .unlink = cifs_unlink,
  456. .link = cifs_hardlink,
  457. .mkdir = cifs_mkdir,
  458. .rmdir = cifs_rmdir,
  459. .rename = cifs_rename,
  460. .permission = cifs_permission,
  461. /* revalidate:cifs_revalidate, */
  462. .setattr = cifs_setattr,
  463. .symlink = cifs_symlink,
  464. .mknod = cifs_mknod,
  465. #ifdef CONFIG_CIFS_XATTR
  466. .setxattr = cifs_setxattr,
  467. .getxattr = cifs_getxattr,
  468. .listxattr = cifs_listxattr,
  469. .removexattr = cifs_removexattr,
  470. #endif
  471. };
  472. struct inode_operations cifs_file_inode_ops = {
  473. /* revalidate:cifs_revalidate, */
  474. .setattr = cifs_setattr,
  475. .getattr = cifs_getattr, /* do we need this anymore? */
  476. .rename = cifs_rename,
  477. .permission = cifs_permission,
  478. #ifdef CONFIG_CIFS_XATTR
  479. .setxattr = cifs_setxattr,
  480. .getxattr = cifs_getxattr,
  481. .listxattr = cifs_listxattr,
  482. .removexattr = cifs_removexattr,
  483. #endif
  484. };
  485. struct inode_operations cifs_symlink_inode_ops = {
  486. .readlink = generic_readlink,
  487. .follow_link = cifs_follow_link,
  488. .put_link = cifs_put_link,
  489. .permission = cifs_permission,
  490. /* BB add the following two eventually */
  491. /* revalidate: cifs_revalidate,
  492. setattr: cifs_notify_change, *//* BB do we need notify change */
  493. #ifdef CONFIG_CIFS_XATTR
  494. .setxattr = cifs_setxattr,
  495. .getxattr = cifs_getxattr,
  496. .listxattr = cifs_listxattr,
  497. .removexattr = cifs_removexattr,
  498. #endif
  499. };
  500. struct file_operations cifs_file_ops = {
  501. .read = do_sync_read,
  502. .write = do_sync_write,
  503. .readv = generic_file_readv,
  504. .writev = cifs_file_writev,
  505. .aio_read = generic_file_aio_read,
  506. .aio_write = cifs_file_aio_write,
  507. .open = cifs_open,
  508. .release = cifs_close,
  509. .lock = cifs_lock,
  510. .fsync = cifs_fsync,
  511. .flush = cifs_flush,
  512. .mmap = cifs_file_mmap,
  513. .sendfile = generic_file_sendfile,
  514. #ifdef CONFIG_CIFS_POSIX
  515. .ioctl = cifs_ioctl,
  516. #endif /* CONFIG_CIFS_POSIX */
  517. #ifdef CONFIG_CIFS_EXPERIMENTAL
  518. .dir_notify = cifs_dir_notify,
  519. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  520. };
  521. struct file_operations cifs_file_direct_ops = {
  522. /* no mmap, no aio, no readv -
  523. BB reevaluate whether they can be done with directio, no cache */
  524. .read = cifs_user_read,
  525. .write = cifs_user_write,
  526. .open = cifs_open,
  527. .release = cifs_close,
  528. .lock = cifs_lock,
  529. .fsync = cifs_fsync,
  530. .flush = cifs_flush,
  531. .sendfile = generic_file_sendfile, /* BB removeme BB */
  532. #ifdef CONFIG_CIFS_POSIX
  533. .ioctl = cifs_ioctl,
  534. #endif /* CONFIG_CIFS_POSIX */
  535. #ifdef CONFIG_CIFS_EXPERIMENTAL
  536. .dir_notify = cifs_dir_notify,
  537. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  538. };
  539. struct file_operations cifs_file_nobrl_ops = {
  540. .read = do_sync_read,
  541. .write = do_sync_write,
  542. .readv = generic_file_readv,
  543. .writev = cifs_file_writev,
  544. .aio_read = generic_file_aio_read,
  545. .aio_write = cifs_file_aio_write,
  546. .open = cifs_open,
  547. .release = cifs_close,
  548. .fsync = cifs_fsync,
  549. .flush = cifs_flush,
  550. .mmap = cifs_file_mmap,
  551. .sendfile = generic_file_sendfile,
  552. #ifdef CONFIG_CIFS_POSIX
  553. .ioctl = cifs_ioctl,
  554. #endif /* CONFIG_CIFS_POSIX */
  555. #ifdef CONFIG_CIFS_EXPERIMENTAL
  556. .dir_notify = cifs_dir_notify,
  557. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  558. };
  559. struct file_operations cifs_file_direct_nobrl_ops = {
  560. /* no mmap, no aio, no readv -
  561. BB reevaluate whether they can be done with directio, no cache */
  562. .read = cifs_user_read,
  563. .write = cifs_user_write,
  564. .open = cifs_open,
  565. .release = cifs_close,
  566. .fsync = cifs_fsync,
  567. .flush = cifs_flush,
  568. .sendfile = generic_file_sendfile, /* BB removeme BB */
  569. #ifdef CONFIG_CIFS_POSIX
  570. .ioctl = cifs_ioctl,
  571. #endif /* CONFIG_CIFS_POSIX */
  572. #ifdef CONFIG_CIFS_EXPERIMENTAL
  573. .dir_notify = cifs_dir_notify,
  574. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  575. };
  576. struct file_operations cifs_dir_ops = {
  577. .readdir = cifs_readdir,
  578. .release = cifs_closedir,
  579. .read = generic_read_dir,
  580. #ifdef CONFIG_CIFS_EXPERIMENTAL
  581. .dir_notify = cifs_dir_notify,
  582. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  583. .ioctl = cifs_ioctl,
  584. };
  585. static void
  586. cifs_init_once(void *inode, kmem_cache_t * cachep, unsigned long flags)
  587. {
  588. struct cifsInodeInfo *cifsi = inode;
  589. if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
  590. SLAB_CTOR_CONSTRUCTOR) {
  591. inode_init_once(&cifsi->vfs_inode);
  592. INIT_LIST_HEAD(&cifsi->lockList);
  593. }
  594. }
  595. static int
  596. cifs_init_inodecache(void)
  597. {
  598. cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
  599. sizeof (struct cifsInodeInfo),
  600. 0, SLAB_RECLAIM_ACCOUNT,
  601. cifs_init_once, NULL);
  602. if (cifs_inode_cachep == NULL)
  603. return -ENOMEM;
  604. return 0;
  605. }
  606. static void
  607. cifs_destroy_inodecache(void)
  608. {
  609. if (kmem_cache_destroy(cifs_inode_cachep))
  610. printk(KERN_WARNING "cifs_inode_cache: error freeing\n");
  611. }
  612. static int
  613. cifs_init_request_bufs(void)
  614. {
  615. if(CIFSMaxBufSize < 8192) {
  616. /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
  617. Unicode path name has to fit in any SMB/CIFS path based frames */
  618. CIFSMaxBufSize = 8192;
  619. } else if (CIFSMaxBufSize > 1024*127) {
  620. CIFSMaxBufSize = 1024 * 127;
  621. } else {
  622. CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
  623. }
  624. /* cERROR(1,("CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize)); */
  625. cifs_req_cachep = kmem_cache_create("cifs_request",
  626. CIFSMaxBufSize +
  627. MAX_CIFS_HDR_SIZE, 0,
  628. SLAB_HWCACHE_ALIGN, NULL, NULL);
  629. if (cifs_req_cachep == NULL)
  630. return -ENOMEM;
  631. if(cifs_min_rcv < 1)
  632. cifs_min_rcv = 1;
  633. else if (cifs_min_rcv > 64) {
  634. cifs_min_rcv = 64;
  635. cERROR(1,("cifs_min_rcv set to maximum (64)"));
  636. }
  637. cifs_req_poolp = mempool_create(cifs_min_rcv,
  638. mempool_alloc_slab,
  639. mempool_free_slab,
  640. cifs_req_cachep);
  641. if(cifs_req_poolp == NULL) {
  642. kmem_cache_destroy(cifs_req_cachep);
  643. return -ENOMEM;
  644. }
  645. /* 256 (MAX_CIFS_HDR_SIZE bytes is enough for most SMB responses and
  646. almost all handle based requests (but not write response, nor is it
  647. sufficient for path based requests). A smaller size would have
  648. been more efficient (compacting multiple slab items on one 4k page)
  649. for the case in which debug was on, but this larger size allows
  650. more SMBs to use small buffer alloc and is still much more
  651. efficient to alloc 1 per page off the slab compared to 17K (5page)
  652. alloc of large cifs buffers even when page debugging is on */
  653. cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
  654. MAX_CIFS_HDR_SIZE, 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
  655. if (cifs_sm_req_cachep == NULL) {
  656. mempool_destroy(cifs_req_poolp);
  657. kmem_cache_destroy(cifs_req_cachep);
  658. return -ENOMEM;
  659. }
  660. if(cifs_min_small < 2)
  661. cifs_min_small = 2;
  662. else if (cifs_min_small > 256) {
  663. cifs_min_small = 256;
  664. cFYI(1,("cifs_min_small set to maximum (256)"));
  665. }
  666. cifs_sm_req_poolp = mempool_create(cifs_min_small,
  667. mempool_alloc_slab,
  668. mempool_free_slab,
  669. cifs_sm_req_cachep);
  670. if(cifs_sm_req_poolp == NULL) {
  671. mempool_destroy(cifs_req_poolp);
  672. kmem_cache_destroy(cifs_req_cachep);
  673. kmem_cache_destroy(cifs_sm_req_cachep);
  674. return -ENOMEM;
  675. }
  676. return 0;
  677. }
  678. static void
  679. cifs_destroy_request_bufs(void)
  680. {
  681. mempool_destroy(cifs_req_poolp);
  682. if (kmem_cache_destroy(cifs_req_cachep))
  683. printk(KERN_WARNING
  684. "cifs_destroy_request_cache: error not all structures were freed\n");
  685. mempool_destroy(cifs_sm_req_poolp);
  686. if (kmem_cache_destroy(cifs_sm_req_cachep))
  687. printk(KERN_WARNING
  688. "cifs_destroy_request_cache: cifs_small_rq free error\n");
  689. }
  690. static int
  691. cifs_init_mids(void)
  692. {
  693. cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
  694. sizeof (struct mid_q_entry), 0,
  695. SLAB_HWCACHE_ALIGN, NULL, NULL);
  696. if (cifs_mid_cachep == NULL)
  697. return -ENOMEM;
  698. cifs_mid_poolp = mempool_create(3 /* a reasonable min simultan opers */,
  699. mempool_alloc_slab,
  700. mempool_free_slab,
  701. cifs_mid_cachep);
  702. if(cifs_mid_poolp == NULL) {
  703. kmem_cache_destroy(cifs_mid_cachep);
  704. return -ENOMEM;
  705. }
  706. cifs_oplock_cachep = kmem_cache_create("cifs_oplock_structs",
  707. sizeof (struct oplock_q_entry), 0,
  708. SLAB_HWCACHE_ALIGN, NULL, NULL);
  709. if (cifs_oplock_cachep == NULL) {
  710. kmem_cache_destroy(cifs_mid_cachep);
  711. mempool_destroy(cifs_mid_poolp);
  712. return -ENOMEM;
  713. }
  714. return 0;
  715. }
  716. static void
  717. cifs_destroy_mids(void)
  718. {
  719. mempool_destroy(cifs_mid_poolp);
  720. if (kmem_cache_destroy(cifs_mid_cachep))
  721. printk(KERN_WARNING
  722. "cifs_destroy_mids: error not all structures were freed\n");
  723. if (kmem_cache_destroy(cifs_oplock_cachep))
  724. printk(KERN_WARNING
  725. "error not all oplock structures were freed\n");
  726. }
  727. static int cifs_oplock_thread(void * dummyarg)
  728. {
  729. struct oplock_q_entry * oplock_item;
  730. struct cifsTconInfo *pTcon;
  731. struct inode * inode;
  732. __u16 netfid;
  733. int rc;
  734. daemonize("cifsoplockd");
  735. allow_signal(SIGTERM);
  736. oplockThread = current;
  737. do {
  738. if (try_to_freeze())
  739. continue;
  740. spin_lock(&GlobalMid_Lock);
  741. if(list_empty(&GlobalOplock_Q)) {
  742. spin_unlock(&GlobalMid_Lock);
  743. set_current_state(TASK_INTERRUPTIBLE);
  744. schedule_timeout(39*HZ);
  745. } else {
  746. oplock_item = list_entry(GlobalOplock_Q.next,
  747. struct oplock_q_entry, qhead);
  748. if(oplock_item) {
  749. cFYI(1,("found oplock item to write out"));
  750. pTcon = oplock_item->tcon;
  751. inode = oplock_item->pinode;
  752. netfid = oplock_item->netfid;
  753. spin_unlock(&GlobalMid_Lock);
  754. DeleteOplockQEntry(oplock_item);
  755. /* can not grab inode sem here since it would
  756. deadlock when oplock received on delete
  757. since vfs_unlink holds the i_mutex across
  758. the call */
  759. /* mutex_lock(&inode->i_mutex);*/
  760. if (S_ISREG(inode->i_mode)) {
  761. rc = filemap_fdatawrite(inode->i_mapping);
  762. if(CIFS_I(inode)->clientCanCacheRead == 0) {
  763. filemap_fdatawait(inode->i_mapping);
  764. invalidate_remote_inode(inode);
  765. }
  766. } else
  767. rc = 0;
  768. /* mutex_unlock(&inode->i_mutex);*/
  769. if (rc)
  770. CIFS_I(inode)->write_behind_rc = rc;
  771. cFYI(1,("Oplock flush inode %p rc %d",inode,rc));
  772. /* releasing a stale oplock after recent reconnection
  773. of smb session using a now incorrect file
  774. handle is not a data integrity issue but do
  775. not bother sending an oplock release if session
  776. to server still is disconnected since oplock
  777. already released by the server in that case */
  778. if(pTcon->tidStatus != CifsNeedReconnect) {
  779. rc = CIFSSMBLock(0, pTcon, netfid,
  780. 0 /* len */ , 0 /* offset */, 0,
  781. 0, LOCKING_ANDX_OPLOCK_RELEASE,
  782. 0 /* wait flag */);
  783. cFYI(1,("Oplock release rc = %d ",rc));
  784. }
  785. } else
  786. spin_unlock(&GlobalMid_Lock);
  787. set_current_state(TASK_INTERRUPTIBLE);
  788. schedule_timeout(1); /* yield in case q were corrupt */
  789. }
  790. } while(!signal_pending(current));
  791. oplockThread = NULL;
  792. complete_and_exit (&cifs_oplock_exited, 0);
  793. }
  794. static int cifs_dnotify_thread(void * dummyarg)
  795. {
  796. struct list_head *tmp;
  797. struct cifsSesInfo *ses;
  798. daemonize("cifsdnotifyd");
  799. allow_signal(SIGTERM);
  800. dnotifyThread = current;
  801. do {
  802. if(try_to_freeze())
  803. continue;
  804. set_current_state(TASK_INTERRUPTIBLE);
  805. schedule_timeout(15*HZ);
  806. read_lock(&GlobalSMBSeslock);
  807. /* check if any stuck requests that need
  808. to be woken up and wakeq so the
  809. thread can wake up and error out */
  810. list_for_each(tmp, &GlobalSMBSessionList) {
  811. ses = list_entry(tmp, struct cifsSesInfo,
  812. cifsSessionList);
  813. if(ses && ses->server &&
  814. atomic_read(&ses->server->inFlight))
  815. wake_up_all(&ses->server->response_q);
  816. }
  817. read_unlock(&GlobalSMBSeslock);
  818. } while(!signal_pending(current));
  819. complete_and_exit (&cifs_dnotify_exited, 0);
  820. }
  821. static int __init
  822. init_cifs(void)
  823. {
  824. int rc = 0;
  825. #ifdef CONFIG_PROC_FS
  826. cifs_proc_init();
  827. #endif
  828. INIT_LIST_HEAD(&GlobalServerList); /* BB not implemented yet */
  829. INIT_LIST_HEAD(&GlobalSMBSessionList);
  830. INIT_LIST_HEAD(&GlobalTreeConnectionList);
  831. INIT_LIST_HEAD(&GlobalOplock_Q);
  832. #ifdef CONFIG_CIFS_EXPERIMENTAL
  833. INIT_LIST_HEAD(&GlobalDnotifyReqList);
  834. INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
  835. #endif
  836. /*
  837. * Initialize Global counters
  838. */
  839. atomic_set(&sesInfoAllocCount, 0);
  840. atomic_set(&tconInfoAllocCount, 0);
  841. atomic_set(&tcpSesAllocCount,0);
  842. atomic_set(&tcpSesReconnectCount, 0);
  843. atomic_set(&tconInfoReconnectCount, 0);
  844. atomic_set(&bufAllocCount, 0);
  845. atomic_set(&midCount, 0);
  846. GlobalCurrentXid = 0;
  847. GlobalTotalActiveXid = 0;
  848. GlobalMaxActiveXid = 0;
  849. rwlock_init(&GlobalSMBSeslock);
  850. spin_lock_init(&GlobalMid_Lock);
  851. if(cifs_max_pending < 2) {
  852. cifs_max_pending = 2;
  853. cFYI(1,("cifs_max_pending set to min of 2"));
  854. } else if(cifs_max_pending > 256) {
  855. cifs_max_pending = 256;
  856. cFYI(1,("cifs_max_pending set to max of 256"));
  857. }
  858. rc = cifs_init_inodecache();
  859. if (!rc) {
  860. rc = cifs_init_mids();
  861. if (!rc) {
  862. rc = cifs_init_request_bufs();
  863. if (!rc) {
  864. rc = register_filesystem(&cifs_fs_type);
  865. if (!rc) {
  866. rc = (int)kernel_thread(cifs_oplock_thread, NULL,
  867. CLONE_FS | CLONE_FILES | CLONE_VM);
  868. if(rc > 0) {
  869. rc = (int)kernel_thread(cifs_dnotify_thread, NULL,
  870. CLONE_FS | CLONE_FILES | CLONE_VM);
  871. if(rc > 0)
  872. return 0;
  873. else
  874. cERROR(1,("error %d create dnotify thread", rc));
  875. } else {
  876. cERROR(1,("error %d create oplock thread",rc));
  877. }
  878. }
  879. cifs_destroy_request_bufs();
  880. }
  881. cifs_destroy_mids();
  882. }
  883. cifs_destroy_inodecache();
  884. }
  885. #ifdef CONFIG_PROC_FS
  886. cifs_proc_clean();
  887. #endif
  888. return rc;
  889. }
  890. static void __exit
  891. exit_cifs(void)
  892. {
  893. cFYI(0, ("In unregister ie exit_cifs"));
  894. #ifdef CONFIG_PROC_FS
  895. cifs_proc_clean();
  896. #endif
  897. unregister_filesystem(&cifs_fs_type);
  898. cifs_destroy_inodecache();
  899. cifs_destroy_mids();
  900. cifs_destroy_request_bufs();
  901. if(oplockThread) {
  902. send_sig(SIGTERM, oplockThread, 1);
  903. wait_for_completion(&cifs_oplock_exited);
  904. }
  905. if(dnotifyThread) {
  906. send_sig(SIGTERM, dnotifyThread, 1);
  907. wait_for_completion(&cifs_dnotify_exited);
  908. }
  909. }
  910. MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
  911. MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
  912. MODULE_DESCRIPTION
  913. ("VFS to access servers complying with the SNIA CIFS Specification e.g. Samba and Windows");
  914. MODULE_VERSION(CIFS_VERSION);
  915. module_init(init_cifs)
  916. module_exit(exit_cifs)