cifsfs.c 28 KB

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