cifsfs.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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. /* Until the file is open and we have gotten oplock
  237. info back from the server, can not assume caching of
  238. file data or metadata */
  239. cifs_inode->clientCanCacheRead = FALSE;
  240. cifs_inode->clientCanCacheAll = FALSE;
  241. cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
  242. /* Can not set i_flags here - they get immediately overwritten
  243. to zero by the VFS */
  244. /* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;*/
  245. INIT_LIST_HEAD(&cifs_inode->openFileList);
  246. return &cifs_inode->vfs_inode;
  247. }
  248. static void
  249. cifs_destroy_inode(struct inode *inode)
  250. {
  251. kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
  252. }
  253. /*
  254. * cifs_show_options() is for displaying mount options in /proc/mounts.
  255. * Not all settable options are displayed but most of the important
  256. * ones are.
  257. */
  258. static int
  259. cifs_show_options(struct seq_file *s, struct vfsmount *m)
  260. {
  261. struct cifs_sb_info *cifs_sb;
  262. cifs_sb = CIFS_SB(m->mnt_sb);
  263. if (cifs_sb) {
  264. if (cifs_sb->tcon) {
  265. /* BB add prepath to mount options displayed */
  266. seq_printf(s, ",unc=%s", cifs_sb->tcon->treeName);
  267. if (cifs_sb->tcon->ses) {
  268. if (cifs_sb->tcon->ses->userName)
  269. seq_printf(s, ",username=%s",
  270. cifs_sb->tcon->ses->userName);
  271. if (cifs_sb->tcon->ses->domainName)
  272. seq_printf(s, ",domain=%s",
  273. cifs_sb->tcon->ses->domainName);
  274. }
  275. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) ||
  276. !(cifs_sb->tcon->unix_ext))
  277. seq_printf(s, ",uid=%d", cifs_sb->mnt_uid);
  278. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) ||
  279. !(cifs_sb->tcon->unix_ext))
  280. seq_printf(s, ",gid=%d", cifs_sb->mnt_gid);
  281. }
  282. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
  283. seq_printf(s, ",posixpaths");
  284. seq_printf(s, ",rsize=%d", cifs_sb->rsize);
  285. seq_printf(s, ",wsize=%d", cifs_sb->wsize);
  286. }
  287. return 0;
  288. }
  289. #ifdef CONFIG_CIFS_QUOTA
  290. int cifs_xquota_set(struct super_block *sb, int quota_type, qid_t qid,
  291. struct fs_disk_quota *pdquota)
  292. {
  293. int xid;
  294. int rc = 0;
  295. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  296. struct cifsTconInfo *pTcon;
  297. if (cifs_sb)
  298. pTcon = cifs_sb->tcon;
  299. else
  300. return -EIO;
  301. xid = GetXid();
  302. if (pTcon) {
  303. cFYI(1, ("set type: 0x%x id: %d", quota_type, qid));
  304. } else {
  305. rc = -EIO;
  306. }
  307. FreeXid(xid);
  308. return rc;
  309. }
  310. int cifs_xquota_get(struct super_block *sb, int quota_type, qid_t qid,
  311. struct fs_disk_quota *pdquota)
  312. {
  313. int xid;
  314. int rc = 0;
  315. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  316. struct cifsTconInfo *pTcon;
  317. if (cifs_sb)
  318. pTcon = cifs_sb->tcon;
  319. else
  320. return -EIO;
  321. xid = GetXid();
  322. if (pTcon) {
  323. cFYI(1, ("set type: 0x%x id: %d", quota_type, qid));
  324. } else {
  325. rc = -EIO;
  326. }
  327. FreeXid(xid);
  328. return rc;
  329. }
  330. int cifs_xstate_set(struct super_block *sb, unsigned int flags, int operation)
  331. {
  332. int xid;
  333. int rc = 0;
  334. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  335. struct cifsTconInfo *pTcon;
  336. if (cifs_sb)
  337. pTcon = cifs_sb->tcon;
  338. else
  339. return -EIO;
  340. xid = GetXid();
  341. if (pTcon) {
  342. cFYI(1, ("flags: 0x%x operation: 0x%x", flags, operation));
  343. } else {
  344. rc = -EIO;
  345. }
  346. FreeXid(xid);
  347. return rc;
  348. }
  349. int cifs_xstate_get(struct super_block *sb, struct fs_quota_stat *qstats)
  350. {
  351. int xid;
  352. int rc = 0;
  353. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  354. struct cifsTconInfo *pTcon;
  355. if (cifs_sb) {
  356. pTcon = cifs_sb->tcon;
  357. } else {
  358. return -EIO;
  359. }
  360. xid = GetXid();
  361. if (pTcon) {
  362. cFYI(1, ("pqstats %p", qstats));
  363. } else {
  364. rc = -EIO;
  365. }
  366. FreeXid(xid);
  367. return rc;
  368. }
  369. static struct quotactl_ops cifs_quotactl_ops = {
  370. .set_xquota = cifs_xquota_set,
  371. .get_xquota = cifs_xquota_set,
  372. .set_xstate = cifs_xstate_set,
  373. .get_xstate = cifs_xstate_get,
  374. };
  375. #endif
  376. static void cifs_umount_begin(struct vfsmount *vfsmnt, int flags)
  377. {
  378. struct cifs_sb_info *cifs_sb;
  379. struct cifsTconInfo *tcon;
  380. if (!(flags & MNT_FORCE))
  381. return;
  382. cifs_sb = CIFS_SB(vfsmnt->mnt_sb);
  383. if (cifs_sb == NULL)
  384. return;
  385. tcon = cifs_sb->tcon;
  386. if (tcon == NULL)
  387. return;
  388. down(&tcon->tconSem);
  389. if (atomic_read(&tcon->useCount) == 1)
  390. tcon->tidStatus = CifsExiting;
  391. up(&tcon->tconSem);
  392. /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
  393. /* cancel_notify_requests(tcon); */
  394. if (tcon->ses && tcon->ses->server) {
  395. cFYI(1, ("wake up tasks now - umount begin not complete"));
  396. wake_up_all(&tcon->ses->server->request_q);
  397. wake_up_all(&tcon->ses->server->response_q);
  398. msleep(1); /* yield */
  399. /* we have to kick the requests once more */
  400. wake_up_all(&tcon->ses->server->response_q);
  401. msleep(1);
  402. }
  403. /* BB FIXME - finish add checks for tidStatus BB */
  404. return;
  405. }
  406. #ifdef CONFIG_CIFS_STATS2
  407. static int cifs_show_stats(struct seq_file *s, struct vfsmount *mnt)
  408. {
  409. /* BB FIXME */
  410. return 0;
  411. }
  412. #endif
  413. static int cifs_remount(struct super_block *sb, int *flags, char *data)
  414. {
  415. *flags |= MS_NODIRATIME;
  416. return 0;
  417. }
  418. static const struct super_operations cifs_super_ops = {
  419. .read_inode = cifs_read_inode,
  420. .put_super = cifs_put_super,
  421. .statfs = cifs_statfs,
  422. .alloc_inode = cifs_alloc_inode,
  423. .destroy_inode = cifs_destroy_inode,
  424. /* .drop_inode = generic_delete_inode,
  425. .delete_inode = cifs_delete_inode, */ /* Do not need above two
  426. functions unless later we add lazy close of inodes or unless the
  427. kernel forgets to call us with the same number of releases (closes)
  428. as opens */
  429. .show_options = cifs_show_options,
  430. .umount_begin = cifs_umount_begin,
  431. .remount_fs = cifs_remount,
  432. #ifdef CONFIG_CIFS_STATS2
  433. .show_stats = cifs_show_stats,
  434. #endif
  435. };
  436. static int
  437. cifs_get_sb(struct file_system_type *fs_type,
  438. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  439. {
  440. int rc;
  441. struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
  442. cFYI(1, ("Devname: %s flags: %d ", dev_name, flags));
  443. if (IS_ERR(sb))
  444. return PTR_ERR(sb);
  445. sb->s_flags = flags;
  446. rc = cifs_read_super(sb, data, dev_name, flags & MS_SILENT ? 1 : 0);
  447. if (rc) {
  448. up_write(&sb->s_umount);
  449. deactivate_super(sb);
  450. return rc;
  451. }
  452. sb->s_flags |= MS_ACTIVE;
  453. return simple_set_mnt(mnt, sb);
  454. }
  455. static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  456. unsigned long nr_segs, loff_t pos)
  457. {
  458. struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
  459. ssize_t written;
  460. written = generic_file_aio_write(iocb, iov, nr_segs, pos);
  461. if (!CIFS_I(inode)->clientCanCacheAll)
  462. filemap_fdatawrite(inode->i_mapping);
  463. return written;
  464. }
  465. static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
  466. {
  467. /* origin == SEEK_END => we must revalidate the cached file length */
  468. if (origin == SEEK_END) {
  469. int retval;
  470. /* some applications poll for the file length in this strange
  471. way so we must seek to end on non-oplocked files by
  472. setting the revalidate time to zero */
  473. CIFS_I(file->f_path.dentry->d_inode)->time = 0;
  474. retval = cifs_revalidate(file->f_path.dentry);
  475. if (retval < 0)
  476. return (loff_t)retval;
  477. }
  478. return remote_llseek(file, offset, origin);
  479. }
  480. static struct file_system_type cifs_fs_type = {
  481. .owner = THIS_MODULE,
  482. .name = "cifs",
  483. .get_sb = cifs_get_sb,
  484. .kill_sb = kill_anon_super,
  485. /* .fs_flags */
  486. };
  487. const struct inode_operations cifs_dir_inode_ops = {
  488. .create = cifs_create,
  489. .lookup = cifs_lookup,
  490. .getattr = cifs_getattr,
  491. .unlink = cifs_unlink,
  492. .link = cifs_hardlink,
  493. .mkdir = cifs_mkdir,
  494. .rmdir = cifs_rmdir,
  495. .rename = cifs_rename,
  496. .permission = cifs_permission,
  497. /* revalidate:cifs_revalidate, */
  498. .setattr = cifs_setattr,
  499. .symlink = cifs_symlink,
  500. .mknod = cifs_mknod,
  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 inode_operations cifs_file_inode_ops = {
  509. /* revalidate:cifs_revalidate, */
  510. .setattr = cifs_setattr,
  511. .getattr = cifs_getattr, /* do we need this anymore? */
  512. .rename = cifs_rename,
  513. .permission = cifs_permission,
  514. #ifdef CONFIG_CIFS_XATTR
  515. .setxattr = cifs_setxattr,
  516. .getxattr = cifs_getxattr,
  517. .listxattr = cifs_listxattr,
  518. .removexattr = cifs_removexattr,
  519. #endif
  520. };
  521. const struct inode_operations cifs_symlink_inode_ops = {
  522. .readlink = generic_readlink,
  523. .follow_link = cifs_follow_link,
  524. .put_link = cifs_put_link,
  525. .permission = cifs_permission,
  526. /* BB add the following two eventually */
  527. /* revalidate: cifs_revalidate,
  528. setattr: cifs_notify_change, *//* BB do we need notify change */
  529. #ifdef CONFIG_CIFS_XATTR
  530. .setxattr = cifs_setxattr,
  531. .getxattr = cifs_getxattr,
  532. .listxattr = cifs_listxattr,
  533. .removexattr = cifs_removexattr,
  534. #endif
  535. };
  536. const struct file_operations cifs_file_ops = {
  537. .read = do_sync_read,
  538. .write = do_sync_write,
  539. .aio_read = generic_file_aio_read,
  540. .aio_write = cifs_file_aio_write,
  541. .open = cifs_open,
  542. .release = cifs_close,
  543. .lock = cifs_lock,
  544. .fsync = cifs_fsync,
  545. .flush = cifs_flush,
  546. .mmap = cifs_file_mmap,
  547. .splice_read = generic_file_splice_read,
  548. .llseek = cifs_llseek,
  549. #ifdef CONFIG_CIFS_POSIX
  550. .ioctl = cifs_ioctl,
  551. #endif /* CONFIG_CIFS_POSIX */
  552. #ifdef CONFIG_CIFS_EXPERIMENTAL
  553. .dir_notify = cifs_dir_notify,
  554. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  555. };
  556. const struct file_operations cifs_file_direct_ops = {
  557. /* no mmap, no aio, no readv -
  558. BB reevaluate whether they can be done with directio, no cache */
  559. .read = cifs_user_read,
  560. .write = cifs_user_write,
  561. .open = cifs_open,
  562. .release = cifs_close,
  563. .lock = cifs_lock,
  564. .fsync = cifs_fsync,
  565. .flush = cifs_flush,
  566. .splice_read = generic_file_splice_read,
  567. #ifdef CONFIG_CIFS_POSIX
  568. .ioctl = cifs_ioctl,
  569. #endif /* CONFIG_CIFS_POSIX */
  570. .llseek = cifs_llseek,
  571. #ifdef CONFIG_CIFS_EXPERIMENTAL
  572. .dir_notify = cifs_dir_notify,
  573. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  574. };
  575. const struct file_operations cifs_file_nobrl_ops = {
  576. .read = do_sync_read,
  577. .write = do_sync_write,
  578. .aio_read = generic_file_aio_read,
  579. .aio_write = cifs_file_aio_write,
  580. .open = cifs_open,
  581. .release = cifs_close,
  582. .fsync = cifs_fsync,
  583. .flush = cifs_flush,
  584. .mmap = cifs_file_mmap,
  585. .splice_read = generic_file_splice_read,
  586. .llseek = cifs_llseek,
  587. #ifdef CONFIG_CIFS_POSIX
  588. .ioctl = cifs_ioctl,
  589. #endif /* CONFIG_CIFS_POSIX */
  590. #ifdef CONFIG_CIFS_EXPERIMENTAL
  591. .dir_notify = cifs_dir_notify,
  592. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  593. };
  594. const struct file_operations cifs_file_direct_nobrl_ops = {
  595. /* no mmap, no aio, no readv -
  596. BB reevaluate whether they can be done with directio, no cache */
  597. .read = cifs_user_read,
  598. .write = cifs_user_write,
  599. .open = cifs_open,
  600. .release = cifs_close,
  601. .fsync = cifs_fsync,
  602. .flush = cifs_flush,
  603. .splice_read = generic_file_splice_read,
  604. #ifdef CONFIG_CIFS_POSIX
  605. .ioctl = cifs_ioctl,
  606. #endif /* CONFIG_CIFS_POSIX */
  607. .llseek = cifs_llseek,
  608. #ifdef CONFIG_CIFS_EXPERIMENTAL
  609. .dir_notify = cifs_dir_notify,
  610. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  611. };
  612. const struct file_operations cifs_dir_ops = {
  613. .readdir = cifs_readdir,
  614. .release = cifs_closedir,
  615. .read = generic_read_dir,
  616. #ifdef CONFIG_CIFS_EXPERIMENTAL
  617. .dir_notify = cifs_dir_notify,
  618. #endif /* CONFIG_CIFS_EXPERIMENTAL */
  619. .ioctl = cifs_ioctl,
  620. };
  621. static void
  622. cifs_init_once(struct kmem_cache *cachep, void *inode)
  623. {
  624. struct cifsInodeInfo *cifsi = inode;
  625. inode_init_once(&cifsi->vfs_inode);
  626. INIT_LIST_HEAD(&cifsi->lockList);
  627. }
  628. static int
  629. cifs_init_inodecache(void)
  630. {
  631. cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
  632. sizeof(struct cifsInodeInfo),
  633. 0, (SLAB_RECLAIM_ACCOUNT|
  634. SLAB_MEM_SPREAD),
  635. cifs_init_once);
  636. if (cifs_inode_cachep == NULL)
  637. return -ENOMEM;
  638. return 0;
  639. }
  640. static void
  641. cifs_destroy_inodecache(void)
  642. {
  643. kmem_cache_destroy(cifs_inode_cachep);
  644. }
  645. static int
  646. cifs_init_request_bufs(void)
  647. {
  648. if (CIFSMaxBufSize < 8192) {
  649. /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
  650. Unicode path name has to fit in any SMB/CIFS path based frames */
  651. CIFSMaxBufSize = 8192;
  652. } else if (CIFSMaxBufSize > 1024*127) {
  653. CIFSMaxBufSize = 1024 * 127;
  654. } else {
  655. CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
  656. }
  657. /* cERROR(1,("CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize)); */
  658. cifs_req_cachep = kmem_cache_create("cifs_request",
  659. CIFSMaxBufSize +
  660. MAX_CIFS_HDR_SIZE, 0,
  661. SLAB_HWCACHE_ALIGN, NULL);
  662. if (cifs_req_cachep == NULL)
  663. return -ENOMEM;
  664. if (cifs_min_rcv < 1)
  665. cifs_min_rcv = 1;
  666. else if (cifs_min_rcv > 64) {
  667. cifs_min_rcv = 64;
  668. cERROR(1, ("cifs_min_rcv set to maximum (64)"));
  669. }
  670. cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
  671. cifs_req_cachep);
  672. if (cifs_req_poolp == NULL) {
  673. kmem_cache_destroy(cifs_req_cachep);
  674. return -ENOMEM;
  675. }
  676. /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
  677. almost all handle based requests (but not write response, nor is it
  678. sufficient for path based requests). A smaller size would have
  679. been more efficient (compacting multiple slab items on one 4k page)
  680. for the case in which debug was on, but this larger size allows
  681. more SMBs to use small buffer alloc and is still much more
  682. efficient to alloc 1 per page off the slab compared to 17K (5page)
  683. alloc of large cifs buffers even when page debugging is on */
  684. cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
  685. MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
  686. NULL);
  687. if (cifs_sm_req_cachep == NULL) {
  688. mempool_destroy(cifs_req_poolp);
  689. kmem_cache_destroy(cifs_req_cachep);
  690. return -ENOMEM;
  691. }
  692. if (cifs_min_small < 2)
  693. cifs_min_small = 2;
  694. else if (cifs_min_small > 256) {
  695. cifs_min_small = 256;
  696. cFYI(1, ("cifs_min_small set to maximum (256)"));
  697. }
  698. cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
  699. cifs_sm_req_cachep);
  700. if (cifs_sm_req_poolp == NULL) {
  701. mempool_destroy(cifs_req_poolp);
  702. kmem_cache_destroy(cifs_req_cachep);
  703. kmem_cache_destroy(cifs_sm_req_cachep);
  704. return -ENOMEM;
  705. }
  706. return 0;
  707. }
  708. static void
  709. cifs_destroy_request_bufs(void)
  710. {
  711. mempool_destroy(cifs_req_poolp);
  712. kmem_cache_destroy(cifs_req_cachep);
  713. mempool_destroy(cifs_sm_req_poolp);
  714. kmem_cache_destroy(cifs_sm_req_cachep);
  715. }
  716. static int
  717. cifs_init_mids(void)
  718. {
  719. cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
  720. sizeof(struct mid_q_entry), 0,
  721. SLAB_HWCACHE_ALIGN, NULL);
  722. if (cifs_mid_cachep == NULL)
  723. return -ENOMEM;
  724. /* 3 is a reasonable minimum number of simultaneous operations */
  725. cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
  726. if (cifs_mid_poolp == NULL) {
  727. kmem_cache_destroy(cifs_mid_cachep);
  728. return -ENOMEM;
  729. }
  730. cifs_oplock_cachep = kmem_cache_create("cifs_oplock_structs",
  731. sizeof(struct oplock_q_entry), 0,
  732. SLAB_HWCACHE_ALIGN, NULL);
  733. if (cifs_oplock_cachep == NULL) {
  734. mempool_destroy(cifs_mid_poolp);
  735. kmem_cache_destroy(cifs_mid_cachep);
  736. return -ENOMEM;
  737. }
  738. return 0;
  739. }
  740. static void
  741. cifs_destroy_mids(void)
  742. {
  743. mempool_destroy(cifs_mid_poolp);
  744. kmem_cache_destroy(cifs_mid_cachep);
  745. kmem_cache_destroy(cifs_oplock_cachep);
  746. }
  747. static int cifs_oplock_thread(void *dummyarg)
  748. {
  749. struct oplock_q_entry *oplock_item;
  750. struct cifsTconInfo *pTcon;
  751. struct inode *inode;
  752. __u16 netfid;
  753. int rc;
  754. set_freezable();
  755. do {
  756. if (try_to_freeze())
  757. continue;
  758. spin_lock(&GlobalMid_Lock);
  759. if (list_empty(&GlobalOplock_Q)) {
  760. spin_unlock(&GlobalMid_Lock);
  761. set_current_state(TASK_INTERRUPTIBLE);
  762. schedule_timeout(39*HZ);
  763. } else {
  764. oplock_item = list_entry(GlobalOplock_Q.next,
  765. struct oplock_q_entry, qhead);
  766. if (oplock_item) {
  767. cFYI(1, ("found oplock item to write out"));
  768. pTcon = oplock_item->tcon;
  769. inode = oplock_item->pinode;
  770. netfid = oplock_item->netfid;
  771. spin_unlock(&GlobalMid_Lock);
  772. DeleteOplockQEntry(oplock_item);
  773. /* can not grab inode sem here since it would
  774. deadlock when oplock received on delete
  775. since vfs_unlink holds the i_mutex across
  776. the call */
  777. /* mutex_lock(&inode->i_mutex);*/
  778. if (S_ISREG(inode->i_mode)) {
  779. rc =
  780. filemap_fdatawrite(inode->i_mapping);
  781. if (CIFS_I(inode)->clientCanCacheRead
  782. == 0) {
  783. filemap_fdatawait(inode->i_mapping);
  784. invalidate_remote_inode(inode);
  785. }
  786. } else
  787. rc = 0;
  788. /* mutex_unlock(&inode->i_mutex);*/
  789. if (rc)
  790. CIFS_I(inode)->write_behind_rc = rc;
  791. cFYI(1, ("Oplock flush inode %p rc %d",
  792. inode, rc));
  793. /* releasing stale oplock after recent reconnect
  794. of smb session using a now incorrect file
  795. handle is not a data integrity issue but do
  796. not bother sending an oplock release if session
  797. to server still is disconnected since oplock
  798. already released by the server in that case */
  799. if (pTcon->tidStatus != CifsNeedReconnect) {
  800. rc = CIFSSMBLock(0, pTcon, netfid,
  801. 0 /* len */ , 0 /* offset */, 0,
  802. 0, LOCKING_ANDX_OPLOCK_RELEASE,
  803. 0 /* wait flag */);
  804. cFYI(1, ("Oplock release rc = %d", rc));
  805. }
  806. } else
  807. spin_unlock(&GlobalMid_Lock);
  808. set_current_state(TASK_INTERRUPTIBLE);
  809. schedule_timeout(1); /* yield in case q were corrupt */
  810. }
  811. } while (!kthread_should_stop());
  812. return 0;
  813. }
  814. static int cifs_dnotify_thread(void *dummyarg)
  815. {
  816. struct list_head *tmp;
  817. struct cifsSesInfo *ses;
  818. do {
  819. if (try_to_freeze())
  820. continue;
  821. set_current_state(TASK_INTERRUPTIBLE);
  822. schedule_timeout(15*HZ);
  823. read_lock(&GlobalSMBSeslock);
  824. /* check if any stuck requests that need
  825. to be woken up and wakeq so the
  826. thread can wake up and error out */
  827. list_for_each(tmp, &GlobalSMBSessionList) {
  828. ses = list_entry(tmp, struct cifsSesInfo,
  829. cifsSessionList);
  830. if (ses && ses->server &&
  831. atomic_read(&ses->server->inFlight))
  832. wake_up_all(&ses->server->response_q);
  833. }
  834. read_unlock(&GlobalSMBSeslock);
  835. } while (!kthread_should_stop());
  836. return 0;
  837. }
  838. static int __init
  839. init_cifs(void)
  840. {
  841. int rc = 0;
  842. #ifdef CONFIG_PROC_FS
  843. cifs_proc_init();
  844. #endif
  845. /* INIT_LIST_HEAD(&GlobalServerList);*/ /* BB not implemented yet */
  846. INIT_LIST_HEAD(&GlobalSMBSessionList);
  847. INIT_LIST_HEAD(&GlobalTreeConnectionList);
  848. INIT_LIST_HEAD(&GlobalOplock_Q);
  849. #ifdef CONFIG_CIFS_EXPERIMENTAL
  850. INIT_LIST_HEAD(&GlobalDnotifyReqList);
  851. INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
  852. #endif
  853. /*
  854. * Initialize Global counters
  855. */
  856. atomic_set(&sesInfoAllocCount, 0);
  857. atomic_set(&tconInfoAllocCount, 0);
  858. atomic_set(&tcpSesAllocCount, 0);
  859. atomic_set(&tcpSesReconnectCount, 0);
  860. atomic_set(&tconInfoReconnectCount, 0);
  861. atomic_set(&bufAllocCount, 0);
  862. atomic_set(&smBufAllocCount, 0);
  863. #ifdef CONFIG_CIFS_STATS2
  864. atomic_set(&totBufAllocCount, 0);
  865. atomic_set(&totSmBufAllocCount, 0);
  866. #endif /* CONFIG_CIFS_STATS2 */
  867. atomic_set(&midCount, 0);
  868. GlobalCurrentXid = 0;
  869. GlobalTotalActiveXid = 0;
  870. GlobalMaxActiveXid = 0;
  871. memset(Local_System_Name, 0, 15);
  872. rwlock_init(&GlobalSMBSeslock);
  873. spin_lock_init(&GlobalMid_Lock);
  874. if (cifs_max_pending < 2) {
  875. cifs_max_pending = 2;
  876. cFYI(1, ("cifs_max_pending set to min of 2"));
  877. } else if (cifs_max_pending > 256) {
  878. cifs_max_pending = 256;
  879. cFYI(1, ("cifs_max_pending set to max of 256"));
  880. }
  881. rc = cifs_init_inodecache();
  882. if (rc)
  883. goto out_clean_proc;
  884. rc = cifs_init_mids();
  885. if (rc)
  886. goto out_destroy_inodecache;
  887. rc = cifs_init_request_bufs();
  888. if (rc)
  889. goto out_destroy_mids;
  890. rc = register_filesystem(&cifs_fs_type);
  891. if (rc)
  892. goto out_destroy_request_bufs;
  893. #ifdef CONFIG_CIFS_UPCALL
  894. rc = register_key_type(&cifs_spnego_key_type);
  895. if (rc)
  896. goto out_unregister_filesystem;
  897. #endif
  898. oplockThread = kthread_run(cifs_oplock_thread, NULL, "cifsoplockd");
  899. if (IS_ERR(oplockThread)) {
  900. rc = PTR_ERR(oplockThread);
  901. cERROR(1, ("error %d create oplock thread", rc));
  902. goto out_unregister_key_type;
  903. }
  904. dnotifyThread = kthread_run(cifs_dnotify_thread, NULL, "cifsdnotifyd");
  905. if (IS_ERR(dnotifyThread)) {
  906. rc = PTR_ERR(dnotifyThread);
  907. cERROR(1, ("error %d create dnotify thread", rc));
  908. goto out_stop_oplock_thread;
  909. }
  910. return 0;
  911. out_stop_oplock_thread:
  912. kthread_stop(oplockThread);
  913. out_unregister_key_type:
  914. #ifdef CONFIG_CIFS_UPCALL
  915. unregister_key_type(&cifs_spnego_key_type);
  916. out_unregister_filesystem:
  917. #endif
  918. unregister_filesystem(&cifs_fs_type);
  919. out_destroy_request_bufs:
  920. cifs_destroy_request_bufs();
  921. out_destroy_mids:
  922. cifs_destroy_mids();
  923. out_destroy_inodecache:
  924. cifs_destroy_inodecache();
  925. out_clean_proc:
  926. #ifdef CONFIG_PROC_FS
  927. cifs_proc_clean();
  928. #endif
  929. return rc;
  930. }
  931. static void __exit
  932. exit_cifs(void)
  933. {
  934. cFYI(0, ("exit_cifs"));
  935. #ifdef CONFIG_PROC_FS
  936. cifs_proc_clean();
  937. #endif
  938. #ifdef CONFIG_CIFS_UPCALL
  939. unregister_key_type(&cifs_spnego_key_type);
  940. #endif
  941. unregister_filesystem(&cifs_fs_type);
  942. cifs_destroy_inodecache();
  943. cifs_destroy_mids();
  944. cifs_destroy_request_bufs();
  945. kthread_stop(oplockThread);
  946. kthread_stop(dnotifyThread);
  947. }
  948. MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
  949. MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
  950. MODULE_DESCRIPTION
  951. ("VFS to access servers complying with the SNIA CIFS Specification "
  952. "e.g. Samba and Windows");
  953. MODULE_VERSION(CIFS_VERSION);
  954. module_init(init_cifs)
  955. module_exit(exit_cifs)