cifsfs.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 = kzalloc(sizeof(struct cifs_sb_info),GFP_KERNEL);
  88. cifs_sb = CIFS_SB(sb);
  89. if(cifs_sb == NULL)
  90. return -ENOMEM;
  91. rc = cifs_mount(sb, cifs_sb, data, devname);
  92. if (rc) {
  93. if (!silent)
  94. cERROR(1,
  95. ("cifs_mount failed w/return code = %d", rc));
  96. goto out_mount_failed;
  97. }
  98. sb->s_magic = CIFS_MAGIC_NUMBER;
  99. sb->s_op = &cifs_super_ops;
  100. /* if(cifs_sb->tcon->ses->server->maxBuf > MAX_CIFS_HDR_SIZE + 512)
  101. sb->s_blocksize = cifs_sb->tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE; */
  102. #ifdef CONFIG_CIFS_QUOTA
  103. sb->s_qcop = &cifs_quotactl_ops;
  104. #endif
  105. sb->s_blocksize = CIFS_MAX_MSGSIZE;
  106. sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
  107. inode = iget(sb, ROOT_I);
  108. if (!inode) {
  109. rc = -ENOMEM;
  110. goto out_no_root;
  111. }
  112. sb->s_root = d_alloc_root(inode);
  113. if (!sb->s_root) {
  114. rc = -ENOMEM;
  115. goto out_no_root;
  116. }
  117. return 0;
  118. out_no_root:
  119. cERROR(1, ("cifs_read_super: get root inode failed"));
  120. if (inode)
  121. iput(inode);
  122. out_mount_failed:
  123. if(cifs_sb) {
  124. if(cifs_sb->local_nls)
  125. unload_nls(cifs_sb->local_nls);
  126. kfree(cifs_sb);
  127. }
  128. return rc;
  129. }
  130. static void
  131. cifs_put_super(struct super_block *sb)
  132. {
  133. int rc = 0;
  134. struct cifs_sb_info *cifs_sb;
  135. cFYI(1, ("In cifs_put_super"));
  136. cifs_sb = CIFS_SB(sb);
  137. if(cifs_sb == NULL) {
  138. cFYI(1,("Empty cifs superblock info passed to unmount"));
  139. return;
  140. }
  141. rc = cifs_umount(sb, cifs_sb);
  142. if (rc) {
  143. cERROR(1, ("cifs_umount failed with return code %d", rc));
  144. }
  145. unload_nls(cifs_sb->local_nls);
  146. kfree(cifs_sb);
  147. return;
  148. }
  149. static int
  150. cifs_statfs(struct super_block *sb, struct kstatfs *buf)
  151. {
  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 super_block * sblock)
  353. {
  354. struct cifs_sb_info *cifs_sb;
  355. struct cifsTconInfo * tcon;
  356. cifs_sb = CIFS_SB(sblock);
  357. if(cifs_sb == NULL)
  358. return;
  359. tcon = cifs_sb->tcon;
  360. if(tcon == NULL)
  361. return;
  362. down(&tcon->tconSem);
  363. if (atomic_read(&tcon->useCount) == 1)
  364. tcon->tidStatus = CifsExiting;
  365. up(&tcon->tconSem);
  366. /* cancel_brl_requests(tcon); */
  367. /* cancel_notify_requests(tcon); */
  368. if(tcon->ses && tcon->ses->server)
  369. {
  370. cFYI(1,("wake up tasks now - umount begin not complete"));
  371. wake_up_all(&tcon->ses->server->request_q);
  372. wake_up_all(&tcon->ses->server->response_q);
  373. msleep(1); /* yield */
  374. /* we have to kick the requests once more */
  375. wake_up_all(&tcon->ses->server->response_q);
  376. msleep(1);
  377. }
  378. /* BB FIXME - finish add checks for tidStatus BB */
  379. return;
  380. }
  381. #endif
  382. static int cifs_remount(struct super_block *sb, int *flags, char *data)
  383. {
  384. *flags |= MS_NODIRATIME;
  385. return 0;
  386. }
  387. struct super_operations cifs_super_ops = {
  388. .read_inode = cifs_read_inode,
  389. .put_super = cifs_put_super,
  390. .statfs = cifs_statfs,
  391. .alloc_inode = cifs_alloc_inode,
  392. .destroy_inode = cifs_destroy_inode,
  393. /* .drop_inode = generic_delete_inode,
  394. .delete_inode = cifs_delete_inode, *//* Do not need the above two functions
  395. unless later we add lazy close of inodes or unless the kernel forgets to call
  396. us with the same number of releases (closes) as opens */
  397. .show_options = cifs_show_options,
  398. #ifdef CONFIG_CIFS_EXPERIMENTAL
  399. .umount_begin = cifs_umount_begin,
  400. #endif
  401. .remount_fs = cifs_remount,
  402. };
  403. static struct super_block *
  404. cifs_get_sb(struct file_system_type *fs_type,
  405. int flags, const char *dev_name, void *data)
  406. {
  407. int rc;
  408. struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
  409. cFYI(1, ("Devname: %s flags: %d ", dev_name, flags));
  410. if (IS_ERR(sb))
  411. return sb;
  412. sb->s_flags = flags;
  413. rc = cifs_read_super(sb, data, dev_name, flags & MS_SILENT ? 1 : 0);
  414. if (rc) {
  415. up_write(&sb->s_umount);
  416. deactivate_super(sb);
  417. return ERR_PTR(rc);
  418. }
  419. sb->s_flags |= MS_ACTIVE;
  420. return sb;
  421. }
  422. static ssize_t cifs_file_writev(struct file *file, const struct iovec *iov,
  423. unsigned long nr_segs, loff_t *ppos)
  424. {
  425. struct inode *inode = file->f_dentry->d_inode;
  426. ssize_t written;
  427. written = generic_file_writev(file, iov, nr_segs, ppos);
  428. if (!CIFS_I(inode)->clientCanCacheAll)
  429. filemap_fdatawrite(inode->i_mapping);
  430. return written;
  431. }
  432. static ssize_t cifs_file_aio_write(struct kiocb *iocb, const char __user *buf,
  433. size_t count, loff_t pos)
  434. {
  435. struct inode *inode = iocb->ki_filp->f_dentry->d_inode;
  436. ssize_t written;
  437. written = generic_file_aio_write(iocb, buf, count, pos);
  438. if (!CIFS_I(inode)->clientCanCacheAll)
  439. filemap_fdatawrite(inode->i_mapping);
  440. return written;
  441. }
  442. static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
  443. {
  444. /* origin == SEEK_END => we must revalidate the cached file length */
  445. if (origin == 2) {
  446. int retval = cifs_revalidate(file->f_dentry);
  447. if (retval < 0)
  448. return (loff_t)retval;
  449. }
  450. return remote_llseek(file, offset, origin);
  451. }
  452. static struct file_system_type cifs_fs_type = {
  453. .owner = THIS_MODULE,
  454. .name = "cifs",
  455. .get_sb = cifs_get_sb,
  456. .kill_sb = kill_anon_super,
  457. /* .fs_flags */
  458. };
  459. struct inode_operations cifs_dir_inode_ops = {
  460. .create = cifs_create,
  461. .lookup = cifs_lookup,
  462. .getattr = cifs_getattr,
  463. .unlink = cifs_unlink,
  464. .link = cifs_hardlink,
  465. .mkdir = cifs_mkdir,
  466. .rmdir = cifs_rmdir,
  467. .rename = cifs_rename,
  468. .permission = cifs_permission,
  469. /* revalidate:cifs_revalidate, */
  470. .setattr = cifs_setattr,
  471. .symlink = cifs_symlink,
  472. .mknod = cifs_mknod,
  473. #ifdef CONFIG_CIFS_XATTR
  474. .setxattr = cifs_setxattr,
  475. .getxattr = cifs_getxattr,
  476. .listxattr = cifs_listxattr,
  477. .removexattr = cifs_removexattr,
  478. #endif
  479. };
  480. struct inode_operations cifs_file_inode_ops = {
  481. /* revalidate:cifs_revalidate, */
  482. .setattr = cifs_setattr,
  483. .getattr = cifs_getattr, /* do we need this anymore? */
  484. .rename = cifs_rename,
  485. .permission = cifs_permission,
  486. #ifdef CONFIG_CIFS_XATTR
  487. .setxattr = cifs_setxattr,
  488. .getxattr = cifs_getxattr,
  489. .listxattr = cifs_listxattr,
  490. .removexattr = cifs_removexattr,
  491. #endif
  492. };
  493. struct inode_operations cifs_symlink_inode_ops = {
  494. .readlink = generic_readlink,
  495. .follow_link = cifs_follow_link,
  496. .put_link = cifs_put_link,
  497. .permission = cifs_permission,
  498. /* BB add the following two eventually */
  499. /* revalidate: cifs_revalidate,
  500. setattr: cifs_notify_change, *//* BB do we need notify change */
  501. #ifdef CONFIG_CIFS_XATTR
  502. .setxattr = cifs_setxattr,
  503. .getxattr = cifs_getxattr,
  504. .listxattr = cifs_listxattr,
  505. .removexattr = cifs_removexattr,
  506. #endif
  507. };
  508. const struct file_operations cifs_file_ops = {
  509. .read = do_sync_read,
  510. .write = do_sync_write,
  511. .readv = generic_file_readv,
  512. .writev = cifs_file_writev,
  513. .aio_read = generic_file_aio_read,
  514. .aio_write = cifs_file_aio_write,
  515. .open = cifs_open,
  516. .release = cifs_close,
  517. .lock = cifs_lock,
  518. .fsync = cifs_fsync,
  519. .flush = cifs_flush,
  520. .mmap = cifs_file_mmap,
  521. .sendfile = generic_file_sendfile,
  522. .llseek = cifs_llseek,
  523. #ifdef CONFIG_CIFS_POSIX
  524. .ioctl = cifs_ioctl,
  525. #endif /* CONFIG_CIFS_POSIX */
  526. #ifdef CONFIG_CIFS_EXPERIMENTAL
  527. .dir_notify = cifs_dir_notify,
  528. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  529. };
  530. const struct file_operations cifs_file_direct_ops = {
  531. /* no mmap, no aio, no readv -
  532. BB reevaluate whether they can be done with directio, no cache */
  533. .read = cifs_user_read,
  534. .write = cifs_user_write,
  535. .open = cifs_open,
  536. .release = cifs_close,
  537. .lock = cifs_lock,
  538. .fsync = cifs_fsync,
  539. .flush = cifs_flush,
  540. .sendfile = generic_file_sendfile, /* BB removeme BB */
  541. #ifdef CONFIG_CIFS_POSIX
  542. .ioctl = cifs_ioctl,
  543. #endif /* CONFIG_CIFS_POSIX */
  544. .llseek = cifs_llseek,
  545. #ifdef CONFIG_CIFS_EXPERIMENTAL
  546. .dir_notify = cifs_dir_notify,
  547. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  548. };
  549. const struct file_operations cifs_file_nobrl_ops = {
  550. .read = do_sync_read,
  551. .write = do_sync_write,
  552. .readv = generic_file_readv,
  553. .writev = cifs_file_writev,
  554. .aio_read = generic_file_aio_read,
  555. .aio_write = cifs_file_aio_write,
  556. .open = cifs_open,
  557. .release = cifs_close,
  558. .fsync = cifs_fsync,
  559. .flush = cifs_flush,
  560. .mmap = cifs_file_mmap,
  561. .sendfile = generic_file_sendfile,
  562. .llseek = cifs_llseek,
  563. #ifdef CONFIG_CIFS_POSIX
  564. .ioctl = cifs_ioctl,
  565. #endif /* CONFIG_CIFS_POSIX */
  566. #ifdef CONFIG_CIFS_EXPERIMENTAL
  567. .dir_notify = cifs_dir_notify,
  568. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  569. };
  570. const struct file_operations cifs_file_direct_nobrl_ops = {
  571. /* no mmap, no aio, no readv -
  572. BB reevaluate whether they can be done with directio, no cache */
  573. .read = cifs_user_read,
  574. .write = cifs_user_write,
  575. .open = cifs_open,
  576. .release = cifs_close,
  577. .fsync = cifs_fsync,
  578. .flush = cifs_flush,
  579. .sendfile = generic_file_sendfile, /* BB removeme BB */
  580. #ifdef CONFIG_CIFS_POSIX
  581. .ioctl = cifs_ioctl,
  582. #endif /* CONFIG_CIFS_POSIX */
  583. .llseek = cifs_llseek,
  584. #ifdef CONFIG_CIFS_EXPERIMENTAL
  585. .dir_notify = cifs_dir_notify,
  586. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  587. };
  588. const struct file_operations cifs_dir_ops = {
  589. .readdir = cifs_readdir,
  590. .release = cifs_closedir,
  591. .read = generic_read_dir,
  592. #ifdef CONFIG_CIFS_EXPERIMENTAL
  593. .dir_notify = cifs_dir_notify,
  594. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  595. .ioctl = cifs_ioctl,
  596. };
  597. static void
  598. cifs_init_once(void *inode, kmem_cache_t * cachep, unsigned long flags)
  599. {
  600. struct cifsInodeInfo *cifsi = inode;
  601. if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
  602. SLAB_CTOR_CONSTRUCTOR) {
  603. inode_init_once(&cifsi->vfs_inode);
  604. INIT_LIST_HEAD(&cifsi->lockList);
  605. }
  606. }
  607. static int
  608. cifs_init_inodecache(void)
  609. {
  610. cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
  611. sizeof (struct cifsInodeInfo),
  612. 0, (SLAB_RECLAIM_ACCOUNT|
  613. SLAB_MEM_SPREAD),
  614. cifs_init_once, NULL);
  615. if (cifs_inode_cachep == NULL)
  616. return -ENOMEM;
  617. return 0;
  618. }
  619. static void
  620. cifs_destroy_inodecache(void)
  621. {
  622. if (kmem_cache_destroy(cifs_inode_cachep))
  623. printk(KERN_WARNING "cifs_inode_cache: error freeing\n");
  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. if (kmem_cache_destroy(cifs_req_cachep))
  693. printk(KERN_WARNING
  694. "cifs_destroy_request_cache: error not all structures were freed\n");
  695. mempool_destroy(cifs_sm_req_poolp);
  696. if (kmem_cache_destroy(cifs_sm_req_cachep))
  697. printk(KERN_WARNING
  698. "cifs_destroy_request_cache: cifs_small_rq free error\n");
  699. }
  700. static int
  701. cifs_init_mids(void)
  702. {
  703. cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
  704. sizeof (struct mid_q_entry), 0,
  705. SLAB_HWCACHE_ALIGN, NULL, NULL);
  706. if (cifs_mid_cachep == NULL)
  707. return -ENOMEM;
  708. /* 3 is a reasonable minimum number of simultaneous operations */
  709. cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
  710. if(cifs_mid_poolp == NULL) {
  711. kmem_cache_destroy(cifs_mid_cachep);
  712. return -ENOMEM;
  713. }
  714. cifs_oplock_cachep = kmem_cache_create("cifs_oplock_structs",
  715. sizeof (struct oplock_q_entry), 0,
  716. SLAB_HWCACHE_ALIGN, NULL, NULL);
  717. if (cifs_oplock_cachep == NULL) {
  718. kmem_cache_destroy(cifs_mid_cachep);
  719. mempool_destroy(cifs_mid_poolp);
  720. return -ENOMEM;
  721. }
  722. return 0;
  723. }
  724. static void
  725. cifs_destroy_mids(void)
  726. {
  727. mempool_destroy(cifs_mid_poolp);
  728. if (kmem_cache_destroy(cifs_mid_cachep))
  729. printk(KERN_WARNING
  730. "cifs_destroy_mids: error not all structures were freed\n");
  731. if (kmem_cache_destroy(cifs_oplock_cachep))
  732. printk(KERN_WARNING
  733. "error not all oplock structures were freed\n");
  734. }
  735. static int cifs_oplock_thread(void * dummyarg)
  736. {
  737. struct oplock_q_entry * oplock_item;
  738. struct cifsTconInfo *pTcon;
  739. struct inode * inode;
  740. __u16 netfid;
  741. int rc;
  742. daemonize("cifsoplockd");
  743. allow_signal(SIGTERM);
  744. oplockThread = current;
  745. do {
  746. if (try_to_freeze())
  747. continue;
  748. spin_lock(&GlobalMid_Lock);
  749. if(list_empty(&GlobalOplock_Q)) {
  750. spin_unlock(&GlobalMid_Lock);
  751. set_current_state(TASK_INTERRUPTIBLE);
  752. schedule_timeout(39*HZ);
  753. } else {
  754. oplock_item = list_entry(GlobalOplock_Q.next,
  755. struct oplock_q_entry, qhead);
  756. if(oplock_item) {
  757. cFYI(1,("found oplock item to write out"));
  758. pTcon = oplock_item->tcon;
  759. inode = oplock_item->pinode;
  760. netfid = oplock_item->netfid;
  761. spin_unlock(&GlobalMid_Lock);
  762. DeleteOplockQEntry(oplock_item);
  763. /* can not grab inode sem here since it would
  764. deadlock when oplock received on delete
  765. since vfs_unlink holds the i_mutex across
  766. the call */
  767. /* mutex_lock(&inode->i_mutex);*/
  768. if (S_ISREG(inode->i_mode)) {
  769. rc = filemap_fdatawrite(inode->i_mapping);
  770. if(CIFS_I(inode)->clientCanCacheRead == 0) {
  771. filemap_fdatawait(inode->i_mapping);
  772. invalidate_remote_inode(inode);
  773. }
  774. } else
  775. rc = 0;
  776. /* mutex_unlock(&inode->i_mutex);*/
  777. if (rc)
  778. CIFS_I(inode)->write_behind_rc = rc;
  779. cFYI(1,("Oplock flush inode %p rc %d",inode,rc));
  780. /* releasing a stale oplock after recent reconnection
  781. of smb session using a now incorrect file
  782. handle is not a data integrity issue but do
  783. not bother sending an oplock release if session
  784. to server still is disconnected since oplock
  785. already released by the server in that case */
  786. if(pTcon->tidStatus != CifsNeedReconnect) {
  787. rc = CIFSSMBLock(0, pTcon, netfid,
  788. 0 /* len */ , 0 /* offset */, 0,
  789. 0, LOCKING_ANDX_OPLOCK_RELEASE,
  790. 0 /* wait flag */);
  791. cFYI(1,("Oplock release rc = %d ",rc));
  792. }
  793. } else
  794. spin_unlock(&GlobalMid_Lock);
  795. set_current_state(TASK_INTERRUPTIBLE);
  796. schedule_timeout(1); /* yield in case q were corrupt */
  797. }
  798. } while(!signal_pending(current));
  799. oplockThread = NULL;
  800. complete_and_exit (&cifs_oplock_exited, 0);
  801. }
  802. static int cifs_dnotify_thread(void * dummyarg)
  803. {
  804. struct list_head *tmp;
  805. struct cifsSesInfo *ses;
  806. daemonize("cifsdnotifyd");
  807. allow_signal(SIGTERM);
  808. dnotifyThread = current;
  809. do {
  810. if(try_to_freeze())
  811. continue;
  812. set_current_state(TASK_INTERRUPTIBLE);
  813. schedule_timeout(15*HZ);
  814. read_lock(&GlobalSMBSeslock);
  815. /* check if any stuck requests that need
  816. to be woken up and wakeq so the
  817. thread can wake up and error out */
  818. list_for_each(tmp, &GlobalSMBSessionList) {
  819. ses = list_entry(tmp, struct cifsSesInfo,
  820. cifsSessionList);
  821. if(ses && ses->server &&
  822. atomic_read(&ses->server->inFlight))
  823. wake_up_all(&ses->server->response_q);
  824. }
  825. read_unlock(&GlobalSMBSeslock);
  826. } while(!signal_pending(current));
  827. complete_and_exit (&cifs_dnotify_exited, 0);
  828. }
  829. static int __init
  830. init_cifs(void)
  831. {
  832. int rc = 0;
  833. #ifdef CONFIG_PROC_FS
  834. cifs_proc_init();
  835. #endif
  836. INIT_LIST_HEAD(&GlobalServerList); /* BB not implemented yet */
  837. INIT_LIST_HEAD(&GlobalSMBSessionList);
  838. INIT_LIST_HEAD(&GlobalTreeConnectionList);
  839. INIT_LIST_HEAD(&GlobalOplock_Q);
  840. #ifdef CONFIG_CIFS_EXPERIMENTAL
  841. INIT_LIST_HEAD(&GlobalDnotifyReqList);
  842. INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
  843. #endif
  844. /*
  845. * Initialize Global counters
  846. */
  847. atomic_set(&sesInfoAllocCount, 0);
  848. atomic_set(&tconInfoAllocCount, 0);
  849. atomic_set(&tcpSesAllocCount,0);
  850. atomic_set(&tcpSesReconnectCount, 0);
  851. atomic_set(&tconInfoReconnectCount, 0);
  852. atomic_set(&bufAllocCount, 0);
  853. atomic_set(&smBufAllocCount, 0);
  854. #ifdef CONFIG_CIFS_STATS2
  855. atomic_set(&totBufAllocCount, 0);
  856. atomic_set(&totSmBufAllocCount, 0);
  857. #endif /* CONFIG_CIFS_STATS2 */
  858. atomic_set(&midCount, 0);
  859. GlobalCurrentXid = 0;
  860. GlobalTotalActiveXid = 0;
  861. GlobalMaxActiveXid = 0;
  862. rwlock_init(&GlobalSMBSeslock);
  863. spin_lock_init(&GlobalMid_Lock);
  864. if(cifs_max_pending < 2) {
  865. cifs_max_pending = 2;
  866. cFYI(1,("cifs_max_pending set to min of 2"));
  867. } else if(cifs_max_pending > 256) {
  868. cifs_max_pending = 256;
  869. cFYI(1,("cifs_max_pending set to max of 256"));
  870. }
  871. rc = cifs_init_inodecache();
  872. if (!rc) {
  873. rc = cifs_init_mids();
  874. if (!rc) {
  875. rc = cifs_init_request_bufs();
  876. if (!rc) {
  877. rc = register_filesystem(&cifs_fs_type);
  878. if (!rc) {
  879. rc = (int)kernel_thread(cifs_oplock_thread, NULL,
  880. CLONE_FS | CLONE_FILES | CLONE_VM);
  881. if(rc > 0) {
  882. rc = (int)kernel_thread(cifs_dnotify_thread, NULL,
  883. CLONE_FS | CLONE_FILES | CLONE_VM);
  884. if(rc > 0)
  885. return 0;
  886. else
  887. cERROR(1,("error %d create dnotify thread", rc));
  888. } else {
  889. cERROR(1,("error %d create oplock thread",rc));
  890. }
  891. }
  892. cifs_destroy_request_bufs();
  893. }
  894. cifs_destroy_mids();
  895. }
  896. cifs_destroy_inodecache();
  897. }
  898. #ifdef CONFIG_PROC_FS
  899. cifs_proc_clean();
  900. #endif
  901. return rc;
  902. }
  903. static void __exit
  904. exit_cifs(void)
  905. {
  906. cFYI(0, ("In unregister ie exit_cifs"));
  907. #ifdef CONFIG_PROC_FS
  908. cifs_proc_clean();
  909. #endif
  910. unregister_filesystem(&cifs_fs_type);
  911. cifs_destroy_inodecache();
  912. cifs_destroy_mids();
  913. cifs_destroy_request_bufs();
  914. if(oplockThread) {
  915. send_sig(SIGTERM, oplockThread, 1);
  916. wait_for_completion(&cifs_oplock_exited);
  917. }
  918. if(dnotifyThread) {
  919. send_sig(SIGTERM, dnotifyThread, 1);
  920. wait_for_completion(&cifs_dnotify_exited);
  921. }
  922. }
  923. MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
  924. MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
  925. MODULE_DESCRIPTION
  926. ("VFS to access servers complying with the SNIA CIFS Specification e.g. Samba and Windows");
  927. MODULE_VERSION(CIFS_VERSION);
  928. module_init(init_cifs)
  929. module_exit(exit_cifs)