cifsfs.c 27 KB

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