cifsfs.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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 <linux/smp_lock.h>
  37. #include <net/ipv6.h>
  38. #include "cifsfs.h"
  39. #include "cifspdu.h"
  40. #define DECLARE_GLOBALS_HERE
  41. #include "cifsglob.h"
  42. #include "cifsproto.h"
  43. #include "cifs_debug.h"
  44. #include "cifs_fs_sb.h"
  45. #include <linux/mm.h>
  46. #include <linux/key-type.h>
  47. #include "cifs_spnego.h"
  48. #include "fscache.h"
  49. #define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDUs */
  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 global_secflags = CIFSSEC_DEF;
  59. /* unsigned int ntlmv2_support = 0; */
  60. unsigned int sign_CIFS_PDUs = 1;
  61. static const struct super_operations cifs_super_ops;
  62. unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
  63. module_param(CIFSMaxBufSize, int, 0);
  64. MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header). "
  65. "Default: 16384 Range: 8192 to 130048");
  66. unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
  67. module_param(cifs_min_rcv, int, 0);
  68. MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
  69. "1 to 64");
  70. unsigned int cifs_min_small = 30;
  71. module_param(cifs_min_small, int, 0);
  72. MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
  73. "Range: 2 to 256");
  74. unsigned int cifs_max_pending = CIFS_MAX_REQ;
  75. module_param(cifs_max_pending, int, 0);
  76. MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server. "
  77. "Default: 50 Range: 2 to 256");
  78. extern mempool_t *cifs_sm_req_poolp;
  79. extern mempool_t *cifs_req_poolp;
  80. extern mempool_t *cifs_mid_poolp;
  81. static int
  82. cifs_read_super(struct super_block *sb, void *data,
  83. const char *devname, int silent)
  84. {
  85. struct inode *inode;
  86. struct cifs_sb_info *cifs_sb;
  87. int rc = 0;
  88. /* BB should we make this contingent on mount parm? */
  89. sb->s_flags |= MS_NODIRATIME | MS_NOATIME;
  90. sb->s_fs_info = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
  91. cifs_sb = CIFS_SB(sb);
  92. if (cifs_sb == NULL)
  93. return -ENOMEM;
  94. rc = bdi_setup_and_register(&cifs_sb->bdi, "cifs", BDI_CAP_MAP_COPY);
  95. if (rc) {
  96. kfree(cifs_sb);
  97. return rc;
  98. }
  99. #ifdef CONFIG_CIFS_DFS_UPCALL
  100. /* copy mount params to sb for use in submounts */
  101. /* BB: should we move this after the mount so we
  102. * do not have to do the copy on failed mounts?
  103. * BB: May be it is better to do simple copy before
  104. * complex operation (mount), and in case of fail
  105. * just exit instead of doing mount and attempting
  106. * undo it if this copy fails?*/
  107. if (data) {
  108. int len = strlen(data);
  109. cifs_sb->mountdata = kzalloc(len + 1, GFP_KERNEL);
  110. if (cifs_sb->mountdata == NULL) {
  111. bdi_destroy(&cifs_sb->bdi);
  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, "cifs_mount failed w/return code = %d", rc);
  124. goto out_mount_failed;
  125. }
  126. sb->s_magic = CIFS_MAGIC_NUMBER;
  127. sb->s_op = &cifs_super_ops;
  128. sb->s_bdi = &cifs_sb->bdi;
  129. sb->s_blocksize = CIFS_MAX_MSGSIZE;
  130. sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
  131. inode = cifs_root_iget(sb, ROOT_I);
  132. if (IS_ERR(inode)) {
  133. rc = PTR_ERR(inode);
  134. inode = NULL;
  135. goto out_no_root;
  136. }
  137. sb->s_root = d_alloc_root(inode);
  138. if (!sb->s_root) {
  139. rc = -ENOMEM;
  140. goto out_no_root;
  141. }
  142. #ifdef CONFIG_CIFS_EXPERIMENTAL
  143. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  144. cFYI(1, "export ops supported");
  145. sb->s_export_op = &cifs_export_ops;
  146. }
  147. #endif /* EXPERIMENTAL */
  148. return 0;
  149. out_no_root:
  150. cERROR(1, "cifs_read_super: get root inode failed");
  151. if (inode)
  152. iput(inode);
  153. cifs_umount(sb, cifs_sb);
  154. out_mount_failed:
  155. if (cifs_sb) {
  156. #ifdef CONFIG_CIFS_DFS_UPCALL
  157. if (cifs_sb->mountdata) {
  158. kfree(cifs_sb->mountdata);
  159. cifs_sb->mountdata = NULL;
  160. }
  161. #endif
  162. unload_nls(cifs_sb->local_nls);
  163. bdi_destroy(&cifs_sb->bdi);
  164. kfree(cifs_sb);
  165. }
  166. return rc;
  167. }
  168. static void
  169. cifs_put_super(struct super_block *sb)
  170. {
  171. int rc = 0;
  172. struct cifs_sb_info *cifs_sb;
  173. cFYI(1, "In cifs_put_super");
  174. cifs_sb = CIFS_SB(sb);
  175. if (cifs_sb == NULL) {
  176. cFYI(1, "Empty cifs superblock info passed to unmount");
  177. return;
  178. }
  179. lock_kernel();
  180. rc = cifs_umount(sb, cifs_sb);
  181. if (rc)
  182. cERROR(1, "cifs_umount failed with return code %d", rc);
  183. #ifdef CONFIG_CIFS_DFS_UPCALL
  184. if (cifs_sb->mountdata) {
  185. kfree(cifs_sb->mountdata);
  186. cifs_sb->mountdata = NULL;
  187. }
  188. #endif
  189. unload_nls(cifs_sb->local_nls);
  190. bdi_destroy(&cifs_sb->bdi);
  191. kfree(cifs_sb);
  192. unlock_kernel();
  193. }
  194. static int
  195. cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
  196. {
  197. struct super_block *sb = dentry->d_sb;
  198. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  199. struct cifsTconInfo *tcon = cifs_sb_master_tcon(cifs_sb);
  200. int rc = -EOPNOTSUPP;
  201. int xid;
  202. xid = GetXid();
  203. buf->f_type = CIFS_MAGIC_NUMBER;
  204. /*
  205. * PATH_MAX may be too long - it would presumably be total path,
  206. * but note that some servers (includinng Samba 3) have a shorter
  207. * maximum path.
  208. *
  209. * Instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO.
  210. */
  211. buf->f_namelen = PATH_MAX;
  212. buf->f_files = 0; /* undefined */
  213. buf->f_ffree = 0; /* unlimited */
  214. /*
  215. * We could add a second check for a QFS Unix capability bit
  216. */
  217. if ((tcon->ses->capabilities & CAP_UNIX) &&
  218. (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
  219. rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
  220. /*
  221. * Only need to call the old QFSInfo if failed on newer one,
  222. * e.g. by OS/2.
  223. **/
  224. if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
  225. rc = CIFSSMBQFSInfo(xid, tcon, buf);
  226. /*
  227. * Some old Windows servers also do not support level 103, retry with
  228. * older level one if old server failed the previous call or we
  229. * bypassed it because we detected that this was an older LANMAN sess
  230. */
  231. if (rc)
  232. rc = SMBOldQFSInfo(xid, tcon, buf);
  233. FreeXid(xid);
  234. return 0;
  235. }
  236. static int cifs_permission(struct inode *inode, int mask)
  237. {
  238. struct cifs_sb_info *cifs_sb;
  239. cifs_sb = CIFS_SB(inode->i_sb);
  240. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
  241. if ((mask & MAY_EXEC) && !execute_ok(inode))
  242. return -EACCES;
  243. else
  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. static struct kmem_cache *cifs_sm_req_cachep;
  255. mempool_t *cifs_sm_req_poolp;
  256. mempool_t *cifs_req_poolp;
  257. mempool_t *cifs_mid_poolp;
  258. static struct inode *
  259. cifs_alloc_inode(struct super_block *sb)
  260. {
  261. struct cifsInodeInfo *cifs_inode;
  262. cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
  263. if (!cifs_inode)
  264. return NULL;
  265. cifs_inode->cifsAttrs = 0x20; /* default */
  266. cifs_inode->time = 0;
  267. cifs_inode->write_behind_rc = 0;
  268. /* Until the file is open and we have gotten oplock
  269. info back from the server, can not assume caching of
  270. file data or metadata */
  271. cifs_inode->clientCanCacheRead = false;
  272. cifs_inode->clientCanCacheAll = false;
  273. cifs_inode->delete_pending = false;
  274. cifs_inode->invalid_mapping = false;
  275. cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
  276. cifs_inode->server_eof = 0;
  277. /* Can not set i_flags here - they get immediately overwritten
  278. to zero by the VFS */
  279. /* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;*/
  280. INIT_LIST_HEAD(&cifs_inode->openFileList);
  281. return &cifs_inode->vfs_inode;
  282. }
  283. static void
  284. cifs_destroy_inode(struct inode *inode)
  285. {
  286. kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
  287. }
  288. static void
  289. cifs_evict_inode(struct inode *inode)
  290. {
  291. truncate_inode_pages(&inode->i_data, 0);
  292. end_writeback(inode);
  293. cifs_fscache_release_inode_cookie(inode);
  294. }
  295. static void
  296. cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
  297. {
  298. seq_printf(s, ",addr=");
  299. switch (server->addr.sockAddr.sin_family) {
  300. case AF_INET:
  301. seq_printf(s, "%pI4", &server->addr.sockAddr.sin_addr.s_addr);
  302. break;
  303. case AF_INET6:
  304. seq_printf(s, "%pI6",
  305. &server->addr.sockAddr6.sin6_addr.s6_addr);
  306. if (server->addr.sockAddr6.sin6_scope_id)
  307. seq_printf(s, "%%%u",
  308. server->addr.sockAddr6.sin6_scope_id);
  309. break;
  310. default:
  311. seq_printf(s, "(unknown)");
  312. }
  313. }
  314. /*
  315. * cifs_show_options() is for displaying mount options in /proc/mounts.
  316. * Not all settable options are displayed but most of the important
  317. * ones are.
  318. */
  319. static int
  320. cifs_show_options(struct seq_file *s, struct vfsmount *m)
  321. {
  322. struct cifs_sb_info *cifs_sb = CIFS_SB(m->mnt_sb);
  323. struct cifsTconInfo *tcon = cifs_sb_master_tcon(cifs_sb);
  324. struct sockaddr *srcaddr;
  325. srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
  326. seq_printf(s, ",unc=%s", tcon->treeName);
  327. if (tcon->ses->userName)
  328. seq_printf(s, ",username=%s", tcon->ses->userName);
  329. if (tcon->ses->domainName)
  330. seq_printf(s, ",domain=%s", tcon->ses->domainName);
  331. if (srcaddr->sa_family != AF_UNSPEC) {
  332. struct sockaddr_in *saddr4;
  333. struct sockaddr_in6 *saddr6;
  334. saddr4 = (struct sockaddr_in *)srcaddr;
  335. saddr6 = (struct sockaddr_in6 *)srcaddr;
  336. if (srcaddr->sa_family == AF_INET6)
  337. seq_printf(s, ",srcaddr=%pI6c",
  338. &saddr6->sin6_addr);
  339. else if (srcaddr->sa_family == AF_INET)
  340. seq_printf(s, ",srcaddr=%pI4",
  341. &saddr4->sin_addr.s_addr);
  342. else
  343. seq_printf(s, ",srcaddr=BAD-AF:%i",
  344. (int)(srcaddr->sa_family));
  345. }
  346. seq_printf(s, ",uid=%d", cifs_sb->mnt_uid);
  347. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
  348. seq_printf(s, ",forceuid");
  349. else
  350. seq_printf(s, ",noforceuid");
  351. seq_printf(s, ",gid=%d", cifs_sb->mnt_gid);
  352. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
  353. seq_printf(s, ",forcegid");
  354. else
  355. seq_printf(s, ",noforcegid");
  356. cifs_show_address(s, tcon->ses->server);
  357. if (!tcon->unix_ext)
  358. seq_printf(s, ",file_mode=0%o,dir_mode=0%o",
  359. cifs_sb->mnt_file_mode,
  360. cifs_sb->mnt_dir_mode);
  361. if (tcon->seal)
  362. seq_printf(s, ",seal");
  363. if (tcon->nocase)
  364. seq_printf(s, ",nocase");
  365. if (tcon->retry)
  366. seq_printf(s, ",hard");
  367. if (cifs_sb->prepath)
  368. seq_printf(s, ",prepath=%s", cifs_sb->prepath);
  369. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
  370. seq_printf(s, ",posixpaths");
  371. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
  372. seq_printf(s, ",setuids");
  373. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  374. seq_printf(s, ",serverino");
  375. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
  376. seq_printf(s, ",directio");
  377. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  378. seq_printf(s, ",nouser_xattr");
  379. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
  380. seq_printf(s, ",mapchars");
  381. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
  382. seq_printf(s, ",sfu");
  383. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  384. seq_printf(s, ",nobrl");
  385. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
  386. seq_printf(s, ",cifsacl");
  387. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  388. seq_printf(s, ",dynperm");
  389. if (m->mnt_sb->s_flags & MS_POSIXACL)
  390. seq_printf(s, ",acl");
  391. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
  392. seq_printf(s, ",mfsymlinks");
  393. seq_printf(s, ",rsize=%d", cifs_sb->rsize);
  394. seq_printf(s, ",wsize=%d", cifs_sb->wsize);
  395. return 0;
  396. }
  397. static void cifs_umount_begin(struct super_block *sb)
  398. {
  399. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  400. struct cifsTconInfo *tcon;
  401. if (cifs_sb == NULL)
  402. return;
  403. tcon = cifs_sb_master_tcon(cifs_sb);
  404. read_lock(&cifs_tcp_ses_lock);
  405. if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
  406. /* we have other mounts to same share or we have
  407. already tried to force umount this and woken up
  408. all waiting network requests, nothing to do */
  409. read_unlock(&cifs_tcp_ses_lock);
  410. return;
  411. } else if (tcon->tc_count == 1)
  412. tcon->tidStatus = CifsExiting;
  413. read_unlock(&cifs_tcp_ses_lock);
  414. /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
  415. /* cancel_notify_requests(tcon); */
  416. if (tcon->ses && tcon->ses->server) {
  417. cFYI(1, "wake up tasks now - umount begin not complete");
  418. wake_up_all(&tcon->ses->server->request_q);
  419. wake_up_all(&tcon->ses->server->response_q);
  420. msleep(1); /* yield */
  421. /* we have to kick the requests once more */
  422. wake_up_all(&tcon->ses->server->response_q);
  423. msleep(1);
  424. }
  425. return;
  426. }
  427. #ifdef CONFIG_CIFS_STATS2
  428. static int cifs_show_stats(struct seq_file *s, struct vfsmount *mnt)
  429. {
  430. /* BB FIXME */
  431. return 0;
  432. }
  433. #endif
  434. static int cifs_remount(struct super_block *sb, int *flags, char *data)
  435. {
  436. *flags |= MS_NODIRATIME;
  437. return 0;
  438. }
  439. static int cifs_drop_inode(struct inode *inode)
  440. {
  441. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  442. /* no serverino => unconditional eviction */
  443. return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
  444. generic_drop_inode(inode);
  445. }
  446. static const struct super_operations cifs_super_ops = {
  447. .put_super = cifs_put_super,
  448. .statfs = cifs_statfs,
  449. .alloc_inode = cifs_alloc_inode,
  450. .destroy_inode = cifs_destroy_inode,
  451. .drop_inode = cifs_drop_inode,
  452. .evict_inode = cifs_evict_inode,
  453. /* .delete_inode = cifs_delete_inode, */ /* Do not need above
  454. function unless later we add lazy close of inodes or unless the
  455. kernel forgets to call us with the same number of releases (closes)
  456. as opens */
  457. .show_options = cifs_show_options,
  458. .umount_begin = cifs_umount_begin,
  459. .remount_fs = cifs_remount,
  460. #ifdef CONFIG_CIFS_STATS2
  461. .show_stats = cifs_show_stats,
  462. #endif
  463. };
  464. static int
  465. cifs_get_sb(struct file_system_type *fs_type,
  466. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  467. {
  468. int rc;
  469. struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
  470. cFYI(1, "Devname: %s flags: %d ", dev_name, flags);
  471. if (IS_ERR(sb))
  472. return PTR_ERR(sb);
  473. sb->s_flags = flags;
  474. rc = cifs_read_super(sb, data, dev_name, flags & MS_SILENT ? 1 : 0);
  475. if (rc) {
  476. deactivate_locked_super(sb);
  477. return rc;
  478. }
  479. sb->s_flags |= MS_ACTIVE;
  480. simple_set_mnt(mnt, sb);
  481. return 0;
  482. }
  483. static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  484. unsigned long nr_segs, loff_t pos)
  485. {
  486. struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
  487. ssize_t written;
  488. written = generic_file_aio_write(iocb, iov, nr_segs, pos);
  489. if (!CIFS_I(inode)->clientCanCacheAll)
  490. filemap_fdatawrite(inode->i_mapping);
  491. return written;
  492. }
  493. static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
  494. {
  495. /* origin == SEEK_END => we must revalidate the cached file length */
  496. if (origin == SEEK_END) {
  497. int retval;
  498. /* some applications poll for the file length in this strange
  499. way so we must seek to end on non-oplocked files by
  500. setting the revalidate time to zero */
  501. CIFS_I(file->f_path.dentry->d_inode)->time = 0;
  502. retval = cifs_revalidate_file(file);
  503. if (retval < 0)
  504. return (loff_t)retval;
  505. }
  506. return generic_file_llseek_unlocked(file, offset, origin);
  507. }
  508. static int cifs_setlease(struct file *file, long arg, struct file_lock **lease)
  509. {
  510. /* note that this is called by vfs setlease with the BKL held
  511. although I doubt that BKL is needed here in cifs */
  512. struct inode *inode = file->f_path.dentry->d_inode;
  513. struct cifsFileInfo *cfile = file->private_data;
  514. if (!(S_ISREG(inode->i_mode)))
  515. return -EINVAL;
  516. /* check if file is oplocked */
  517. if (((arg == F_RDLCK) &&
  518. (CIFS_I(inode)->clientCanCacheRead)) ||
  519. ((arg == F_WRLCK) &&
  520. (CIFS_I(inode)->clientCanCacheAll)))
  521. return generic_setlease(file, arg, lease);
  522. else if (tlink_tcon(cfile->tlink)->local_lease &&
  523. !CIFS_I(inode)->clientCanCacheRead)
  524. /* If the server claims to support oplock on this
  525. file, then we still need to check oplock even
  526. if the local_lease mount option is set, but there
  527. are servers which do not support oplock for which
  528. this mount option may be useful if the user
  529. knows that the file won't be changed on the server
  530. by anyone else */
  531. return generic_setlease(file, arg, lease);
  532. else
  533. return -EAGAIN;
  534. }
  535. struct file_system_type cifs_fs_type = {
  536. .owner = THIS_MODULE,
  537. .name = "cifs",
  538. .get_sb = cifs_get_sb,
  539. .kill_sb = kill_anon_super,
  540. /* .fs_flags */
  541. };
  542. const struct inode_operations cifs_dir_inode_ops = {
  543. .create = cifs_create,
  544. .lookup = cifs_lookup,
  545. .getattr = cifs_getattr,
  546. .unlink = cifs_unlink,
  547. .link = cifs_hardlink,
  548. .mkdir = cifs_mkdir,
  549. .rmdir = cifs_rmdir,
  550. .rename = cifs_rename,
  551. .permission = cifs_permission,
  552. /* revalidate:cifs_revalidate, */
  553. .setattr = cifs_setattr,
  554. .symlink = cifs_symlink,
  555. .mknod = cifs_mknod,
  556. #ifdef CONFIG_CIFS_XATTR
  557. .setxattr = cifs_setxattr,
  558. .getxattr = cifs_getxattr,
  559. .listxattr = cifs_listxattr,
  560. .removexattr = cifs_removexattr,
  561. #endif
  562. };
  563. const struct inode_operations cifs_file_inode_ops = {
  564. /* revalidate:cifs_revalidate, */
  565. .setattr = cifs_setattr,
  566. .getattr = cifs_getattr, /* do we need this anymore? */
  567. .rename = cifs_rename,
  568. .permission = cifs_permission,
  569. #ifdef CONFIG_CIFS_XATTR
  570. .setxattr = cifs_setxattr,
  571. .getxattr = cifs_getxattr,
  572. .listxattr = cifs_listxattr,
  573. .removexattr = cifs_removexattr,
  574. #endif
  575. };
  576. const struct inode_operations cifs_symlink_inode_ops = {
  577. .readlink = generic_readlink,
  578. .follow_link = cifs_follow_link,
  579. .put_link = cifs_put_link,
  580. .permission = cifs_permission,
  581. /* BB add the following two eventually */
  582. /* revalidate: cifs_revalidate,
  583. setattr: cifs_notify_change, *//* BB do we need notify change */
  584. #ifdef CONFIG_CIFS_XATTR
  585. .setxattr = cifs_setxattr,
  586. .getxattr = cifs_getxattr,
  587. .listxattr = cifs_listxattr,
  588. .removexattr = cifs_removexattr,
  589. #endif
  590. };
  591. const struct file_operations cifs_file_ops = {
  592. .read = do_sync_read,
  593. .write = do_sync_write,
  594. .aio_read = generic_file_aio_read,
  595. .aio_write = cifs_file_aio_write,
  596. .open = cifs_open,
  597. .release = cifs_close,
  598. .lock = cifs_lock,
  599. .fsync = cifs_fsync,
  600. .flush = cifs_flush,
  601. .mmap = cifs_file_mmap,
  602. .splice_read = generic_file_splice_read,
  603. .llseek = cifs_llseek,
  604. #ifdef CONFIG_CIFS_POSIX
  605. .unlocked_ioctl = cifs_ioctl,
  606. #endif /* CONFIG_CIFS_POSIX */
  607. .setlease = cifs_setlease,
  608. };
  609. const struct file_operations cifs_file_direct_ops = {
  610. /* no aio, no readv -
  611. BB reevaluate whether they can be done with directio, no cache */
  612. .read = cifs_user_read,
  613. .write = cifs_user_write,
  614. .open = cifs_open,
  615. .release = cifs_close,
  616. .lock = cifs_lock,
  617. .fsync = cifs_fsync,
  618. .flush = cifs_flush,
  619. .mmap = cifs_file_mmap,
  620. .splice_read = generic_file_splice_read,
  621. #ifdef CONFIG_CIFS_POSIX
  622. .unlocked_ioctl = cifs_ioctl,
  623. #endif /* CONFIG_CIFS_POSIX */
  624. .llseek = cifs_llseek,
  625. .setlease = cifs_setlease,
  626. };
  627. const struct file_operations cifs_file_nobrl_ops = {
  628. .read = do_sync_read,
  629. .write = do_sync_write,
  630. .aio_read = generic_file_aio_read,
  631. .aio_write = cifs_file_aio_write,
  632. .open = cifs_open,
  633. .release = cifs_close,
  634. .fsync = cifs_fsync,
  635. .flush = cifs_flush,
  636. .mmap = cifs_file_mmap,
  637. .splice_read = generic_file_splice_read,
  638. .llseek = cifs_llseek,
  639. #ifdef CONFIG_CIFS_POSIX
  640. .unlocked_ioctl = cifs_ioctl,
  641. #endif /* CONFIG_CIFS_POSIX */
  642. .setlease = cifs_setlease,
  643. };
  644. const struct file_operations cifs_file_direct_nobrl_ops = {
  645. /* no mmap, no aio, no readv -
  646. BB reevaluate whether they can be done with directio, no cache */
  647. .read = cifs_user_read,
  648. .write = cifs_user_write,
  649. .open = cifs_open,
  650. .release = cifs_close,
  651. .fsync = cifs_fsync,
  652. .flush = cifs_flush,
  653. .mmap = cifs_file_mmap,
  654. .splice_read = generic_file_splice_read,
  655. #ifdef CONFIG_CIFS_POSIX
  656. .unlocked_ioctl = cifs_ioctl,
  657. #endif /* CONFIG_CIFS_POSIX */
  658. .llseek = cifs_llseek,
  659. .setlease = cifs_setlease,
  660. };
  661. const struct file_operations cifs_dir_ops = {
  662. .readdir = cifs_readdir,
  663. .release = cifs_closedir,
  664. .read = generic_read_dir,
  665. .unlocked_ioctl = cifs_ioctl,
  666. .llseek = generic_file_llseek,
  667. };
  668. static void
  669. cifs_init_once(void *inode)
  670. {
  671. struct cifsInodeInfo *cifsi = inode;
  672. inode_init_once(&cifsi->vfs_inode);
  673. INIT_LIST_HEAD(&cifsi->lockList);
  674. }
  675. static int
  676. cifs_init_inodecache(void)
  677. {
  678. cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
  679. sizeof(struct cifsInodeInfo),
  680. 0, (SLAB_RECLAIM_ACCOUNT|
  681. SLAB_MEM_SPREAD),
  682. cifs_init_once);
  683. if (cifs_inode_cachep == NULL)
  684. return -ENOMEM;
  685. return 0;
  686. }
  687. static void
  688. cifs_destroy_inodecache(void)
  689. {
  690. kmem_cache_destroy(cifs_inode_cachep);
  691. }
  692. static int
  693. cifs_init_request_bufs(void)
  694. {
  695. if (CIFSMaxBufSize < 8192) {
  696. /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
  697. Unicode path name has to fit in any SMB/CIFS path based frames */
  698. CIFSMaxBufSize = 8192;
  699. } else if (CIFSMaxBufSize > 1024*127) {
  700. CIFSMaxBufSize = 1024 * 127;
  701. } else {
  702. CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
  703. }
  704. /* cERROR(1, "CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize); */
  705. cifs_req_cachep = kmem_cache_create("cifs_request",
  706. CIFSMaxBufSize +
  707. MAX_CIFS_HDR_SIZE, 0,
  708. SLAB_HWCACHE_ALIGN, NULL);
  709. if (cifs_req_cachep == NULL)
  710. return -ENOMEM;
  711. if (cifs_min_rcv < 1)
  712. cifs_min_rcv = 1;
  713. else if (cifs_min_rcv > 64) {
  714. cifs_min_rcv = 64;
  715. cERROR(1, "cifs_min_rcv set to maximum (64)");
  716. }
  717. cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
  718. cifs_req_cachep);
  719. if (cifs_req_poolp == NULL) {
  720. kmem_cache_destroy(cifs_req_cachep);
  721. return -ENOMEM;
  722. }
  723. /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
  724. almost all handle based requests (but not write response, nor is it
  725. sufficient for path based requests). A smaller size would have
  726. been more efficient (compacting multiple slab items on one 4k page)
  727. for the case in which debug was on, but this larger size allows
  728. more SMBs to use small buffer alloc and is still much more
  729. efficient to alloc 1 per page off the slab compared to 17K (5page)
  730. alloc of large cifs buffers even when page debugging is on */
  731. cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
  732. MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
  733. NULL);
  734. if (cifs_sm_req_cachep == NULL) {
  735. mempool_destroy(cifs_req_poolp);
  736. kmem_cache_destroy(cifs_req_cachep);
  737. return -ENOMEM;
  738. }
  739. if (cifs_min_small < 2)
  740. cifs_min_small = 2;
  741. else if (cifs_min_small > 256) {
  742. cifs_min_small = 256;
  743. cFYI(1, "cifs_min_small set to maximum (256)");
  744. }
  745. cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
  746. cifs_sm_req_cachep);
  747. if (cifs_sm_req_poolp == NULL) {
  748. mempool_destroy(cifs_req_poolp);
  749. kmem_cache_destroy(cifs_req_cachep);
  750. kmem_cache_destroy(cifs_sm_req_cachep);
  751. return -ENOMEM;
  752. }
  753. return 0;
  754. }
  755. static void
  756. cifs_destroy_request_bufs(void)
  757. {
  758. mempool_destroy(cifs_req_poolp);
  759. kmem_cache_destroy(cifs_req_cachep);
  760. mempool_destroy(cifs_sm_req_poolp);
  761. kmem_cache_destroy(cifs_sm_req_cachep);
  762. }
  763. static int
  764. cifs_init_mids(void)
  765. {
  766. cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
  767. sizeof(struct mid_q_entry), 0,
  768. SLAB_HWCACHE_ALIGN, NULL);
  769. if (cifs_mid_cachep == NULL)
  770. return -ENOMEM;
  771. /* 3 is a reasonable minimum number of simultaneous operations */
  772. cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
  773. if (cifs_mid_poolp == NULL) {
  774. kmem_cache_destroy(cifs_mid_cachep);
  775. return -ENOMEM;
  776. }
  777. return 0;
  778. }
  779. static void
  780. cifs_destroy_mids(void)
  781. {
  782. mempool_destroy(cifs_mid_poolp);
  783. kmem_cache_destroy(cifs_mid_cachep);
  784. }
  785. static int __init
  786. init_cifs(void)
  787. {
  788. int rc = 0;
  789. cifs_proc_init();
  790. INIT_LIST_HEAD(&cifs_tcp_ses_list);
  791. #ifdef CONFIG_CIFS_EXPERIMENTAL
  792. INIT_LIST_HEAD(&GlobalDnotifyReqList);
  793. INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
  794. #endif
  795. /*
  796. * Initialize Global counters
  797. */
  798. atomic_set(&sesInfoAllocCount, 0);
  799. atomic_set(&tconInfoAllocCount, 0);
  800. atomic_set(&tcpSesAllocCount, 0);
  801. atomic_set(&tcpSesReconnectCount, 0);
  802. atomic_set(&tconInfoReconnectCount, 0);
  803. atomic_set(&bufAllocCount, 0);
  804. atomic_set(&smBufAllocCount, 0);
  805. #ifdef CONFIG_CIFS_STATS2
  806. atomic_set(&totBufAllocCount, 0);
  807. atomic_set(&totSmBufAllocCount, 0);
  808. #endif /* CONFIG_CIFS_STATS2 */
  809. atomic_set(&midCount, 0);
  810. GlobalCurrentXid = 0;
  811. GlobalTotalActiveXid = 0;
  812. GlobalMaxActiveXid = 0;
  813. memset(Local_System_Name, 0, 15);
  814. rwlock_init(&GlobalSMBSeslock);
  815. rwlock_init(&cifs_tcp_ses_lock);
  816. spin_lock_init(&GlobalMid_Lock);
  817. if (cifs_max_pending < 2) {
  818. cifs_max_pending = 2;
  819. cFYI(1, "cifs_max_pending set to min of 2");
  820. } else if (cifs_max_pending > 256) {
  821. cifs_max_pending = 256;
  822. cFYI(1, "cifs_max_pending set to max of 256");
  823. }
  824. rc = cifs_fscache_register();
  825. if (rc)
  826. goto out_clean_proc;
  827. rc = cifs_init_inodecache();
  828. if (rc)
  829. goto out_unreg_fscache;
  830. rc = cifs_init_mids();
  831. if (rc)
  832. goto out_destroy_inodecache;
  833. rc = cifs_init_request_bufs();
  834. if (rc)
  835. goto out_destroy_mids;
  836. rc = register_filesystem(&cifs_fs_type);
  837. if (rc)
  838. goto out_destroy_request_bufs;
  839. #ifdef CONFIG_CIFS_UPCALL
  840. rc = register_key_type(&cifs_spnego_key_type);
  841. if (rc)
  842. goto out_unregister_filesystem;
  843. #endif
  844. return 0;
  845. #ifdef CONFIG_CIFS_UPCALL
  846. out_unregister_filesystem:
  847. unregister_filesystem(&cifs_fs_type);
  848. #endif
  849. out_destroy_request_bufs:
  850. cifs_destroy_request_bufs();
  851. out_destroy_mids:
  852. cifs_destroy_mids();
  853. out_destroy_inodecache:
  854. cifs_destroy_inodecache();
  855. out_unreg_fscache:
  856. cifs_fscache_unregister();
  857. out_clean_proc:
  858. cifs_proc_clean();
  859. return rc;
  860. }
  861. static void __exit
  862. exit_cifs(void)
  863. {
  864. cFYI(DBG2, "exit_cifs");
  865. cifs_proc_clean();
  866. cifs_fscache_unregister();
  867. #ifdef CONFIG_CIFS_DFS_UPCALL
  868. cifs_dfs_release_automount_timer();
  869. #endif
  870. #ifdef CONFIG_CIFS_UPCALL
  871. unregister_key_type(&cifs_spnego_key_type);
  872. #endif
  873. unregister_filesystem(&cifs_fs_type);
  874. cifs_destroy_inodecache();
  875. cifs_destroy_mids();
  876. cifs_destroy_request_bufs();
  877. }
  878. MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
  879. MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
  880. MODULE_DESCRIPTION
  881. ("VFS to access servers complying with the SNIA CIFS Specification "
  882. "e.g. Samba and Windows");
  883. MODULE_VERSION(CIFS_VERSION);
  884. module_init(init_cifs)
  885. module_exit(exit_cifs)