cifsfs.c 30 KB

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