cifsfs.c 27 KB

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