cifsfs.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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 (cfile->tcon->local_lease && !CIFS_I(inode)->clientCanCacheRead)
  523. /* If the server claims to support oplock on this
  524. file, then we still need to check oplock even
  525. if the local_lease mount option is set, but there
  526. are servers which do not support oplock for which
  527. this mount option may be useful if the user
  528. knows that the file won't be changed on the server
  529. by anyone else */
  530. return generic_setlease(file, arg, lease);
  531. else
  532. return -EAGAIN;
  533. }
  534. struct file_system_type cifs_fs_type = {
  535. .owner = THIS_MODULE,
  536. .name = "cifs",
  537. .get_sb = cifs_get_sb,
  538. .kill_sb = kill_anon_super,
  539. /* .fs_flags */
  540. };
  541. const struct inode_operations cifs_dir_inode_ops = {
  542. .create = cifs_create,
  543. .lookup = cifs_lookup,
  544. .getattr = cifs_getattr,
  545. .unlink = cifs_unlink,
  546. .link = cifs_hardlink,
  547. .mkdir = cifs_mkdir,
  548. .rmdir = cifs_rmdir,
  549. .rename = cifs_rename,
  550. .permission = cifs_permission,
  551. /* revalidate:cifs_revalidate, */
  552. .setattr = cifs_setattr,
  553. .symlink = cifs_symlink,
  554. .mknod = cifs_mknod,
  555. #ifdef CONFIG_CIFS_XATTR
  556. .setxattr = cifs_setxattr,
  557. .getxattr = cifs_getxattr,
  558. .listxattr = cifs_listxattr,
  559. .removexattr = cifs_removexattr,
  560. #endif
  561. };
  562. const struct inode_operations cifs_file_inode_ops = {
  563. /* revalidate:cifs_revalidate, */
  564. .setattr = cifs_setattr,
  565. .getattr = cifs_getattr, /* do we need this anymore? */
  566. .rename = cifs_rename,
  567. .permission = cifs_permission,
  568. #ifdef CONFIG_CIFS_XATTR
  569. .setxattr = cifs_setxattr,
  570. .getxattr = cifs_getxattr,
  571. .listxattr = cifs_listxattr,
  572. .removexattr = cifs_removexattr,
  573. #endif
  574. };
  575. const struct inode_operations cifs_symlink_inode_ops = {
  576. .readlink = generic_readlink,
  577. .follow_link = cifs_follow_link,
  578. .put_link = cifs_put_link,
  579. .permission = cifs_permission,
  580. /* BB add the following two eventually */
  581. /* revalidate: cifs_revalidate,
  582. setattr: cifs_notify_change, *//* BB do we need notify change */
  583. #ifdef CONFIG_CIFS_XATTR
  584. .setxattr = cifs_setxattr,
  585. .getxattr = cifs_getxattr,
  586. .listxattr = cifs_listxattr,
  587. .removexattr = cifs_removexattr,
  588. #endif
  589. };
  590. const struct file_operations cifs_file_ops = {
  591. .read = do_sync_read,
  592. .write = do_sync_write,
  593. .aio_read = generic_file_aio_read,
  594. .aio_write = cifs_file_aio_write,
  595. .open = cifs_open,
  596. .release = cifs_close,
  597. .lock = cifs_lock,
  598. .fsync = cifs_fsync,
  599. .flush = cifs_flush,
  600. .mmap = cifs_file_mmap,
  601. .splice_read = generic_file_splice_read,
  602. .llseek = cifs_llseek,
  603. #ifdef CONFIG_CIFS_POSIX
  604. .unlocked_ioctl = cifs_ioctl,
  605. #endif /* CONFIG_CIFS_POSIX */
  606. .setlease = cifs_setlease,
  607. };
  608. const struct file_operations cifs_file_direct_ops = {
  609. /* no aio, no readv -
  610. BB reevaluate whether they can be done with directio, no cache */
  611. .read = cifs_user_read,
  612. .write = cifs_user_write,
  613. .open = cifs_open,
  614. .release = cifs_close,
  615. .lock = cifs_lock,
  616. .fsync = cifs_fsync,
  617. .flush = cifs_flush,
  618. .mmap = cifs_file_mmap,
  619. .splice_read = generic_file_splice_read,
  620. #ifdef CONFIG_CIFS_POSIX
  621. .unlocked_ioctl = cifs_ioctl,
  622. #endif /* CONFIG_CIFS_POSIX */
  623. .llseek = cifs_llseek,
  624. .setlease = cifs_setlease,
  625. };
  626. const struct file_operations cifs_file_nobrl_ops = {
  627. .read = do_sync_read,
  628. .write = do_sync_write,
  629. .aio_read = generic_file_aio_read,
  630. .aio_write = cifs_file_aio_write,
  631. .open = cifs_open,
  632. .release = cifs_close,
  633. .fsync = cifs_fsync,
  634. .flush = cifs_flush,
  635. .mmap = cifs_file_mmap,
  636. .splice_read = generic_file_splice_read,
  637. .llseek = cifs_llseek,
  638. #ifdef CONFIG_CIFS_POSIX
  639. .unlocked_ioctl = cifs_ioctl,
  640. #endif /* CONFIG_CIFS_POSIX */
  641. .setlease = cifs_setlease,
  642. };
  643. const struct file_operations cifs_file_direct_nobrl_ops = {
  644. /* no mmap, no aio, no readv -
  645. BB reevaluate whether they can be done with directio, no cache */
  646. .read = cifs_user_read,
  647. .write = cifs_user_write,
  648. .open = cifs_open,
  649. .release = cifs_close,
  650. .fsync = cifs_fsync,
  651. .flush = cifs_flush,
  652. .mmap = cifs_file_mmap,
  653. .splice_read = generic_file_splice_read,
  654. #ifdef CONFIG_CIFS_POSIX
  655. .unlocked_ioctl = cifs_ioctl,
  656. #endif /* CONFIG_CIFS_POSIX */
  657. .llseek = cifs_llseek,
  658. .setlease = cifs_setlease,
  659. };
  660. const struct file_operations cifs_dir_ops = {
  661. .readdir = cifs_readdir,
  662. .release = cifs_closedir,
  663. .read = generic_read_dir,
  664. .unlocked_ioctl = cifs_ioctl,
  665. .llseek = generic_file_llseek,
  666. };
  667. static void
  668. cifs_init_once(void *inode)
  669. {
  670. struct cifsInodeInfo *cifsi = inode;
  671. inode_init_once(&cifsi->vfs_inode);
  672. INIT_LIST_HEAD(&cifsi->lockList);
  673. }
  674. static int
  675. cifs_init_inodecache(void)
  676. {
  677. cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
  678. sizeof(struct cifsInodeInfo),
  679. 0, (SLAB_RECLAIM_ACCOUNT|
  680. SLAB_MEM_SPREAD),
  681. cifs_init_once);
  682. if (cifs_inode_cachep == NULL)
  683. return -ENOMEM;
  684. return 0;
  685. }
  686. static void
  687. cifs_destroy_inodecache(void)
  688. {
  689. kmem_cache_destroy(cifs_inode_cachep);
  690. }
  691. static int
  692. cifs_init_request_bufs(void)
  693. {
  694. if (CIFSMaxBufSize < 8192) {
  695. /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
  696. Unicode path name has to fit in any SMB/CIFS path based frames */
  697. CIFSMaxBufSize = 8192;
  698. } else if (CIFSMaxBufSize > 1024*127) {
  699. CIFSMaxBufSize = 1024 * 127;
  700. } else {
  701. CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
  702. }
  703. /* cERROR(1, "CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize); */
  704. cifs_req_cachep = kmem_cache_create("cifs_request",
  705. CIFSMaxBufSize +
  706. MAX_CIFS_HDR_SIZE, 0,
  707. SLAB_HWCACHE_ALIGN, NULL);
  708. if (cifs_req_cachep == NULL)
  709. return -ENOMEM;
  710. if (cifs_min_rcv < 1)
  711. cifs_min_rcv = 1;
  712. else if (cifs_min_rcv > 64) {
  713. cifs_min_rcv = 64;
  714. cERROR(1, "cifs_min_rcv set to maximum (64)");
  715. }
  716. cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
  717. cifs_req_cachep);
  718. if (cifs_req_poolp == NULL) {
  719. kmem_cache_destroy(cifs_req_cachep);
  720. return -ENOMEM;
  721. }
  722. /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
  723. almost all handle based requests (but not write response, nor is it
  724. sufficient for path based requests). A smaller size would have
  725. been more efficient (compacting multiple slab items on one 4k page)
  726. for the case in which debug was on, but this larger size allows
  727. more SMBs to use small buffer alloc and is still much more
  728. efficient to alloc 1 per page off the slab compared to 17K (5page)
  729. alloc of large cifs buffers even when page debugging is on */
  730. cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
  731. MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
  732. NULL);
  733. if (cifs_sm_req_cachep == NULL) {
  734. mempool_destroy(cifs_req_poolp);
  735. kmem_cache_destroy(cifs_req_cachep);
  736. return -ENOMEM;
  737. }
  738. if (cifs_min_small < 2)
  739. cifs_min_small = 2;
  740. else if (cifs_min_small > 256) {
  741. cifs_min_small = 256;
  742. cFYI(1, "cifs_min_small set to maximum (256)");
  743. }
  744. cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
  745. cifs_sm_req_cachep);
  746. if (cifs_sm_req_poolp == NULL) {
  747. mempool_destroy(cifs_req_poolp);
  748. kmem_cache_destroy(cifs_req_cachep);
  749. kmem_cache_destroy(cifs_sm_req_cachep);
  750. return -ENOMEM;
  751. }
  752. return 0;
  753. }
  754. static void
  755. cifs_destroy_request_bufs(void)
  756. {
  757. mempool_destroy(cifs_req_poolp);
  758. kmem_cache_destroy(cifs_req_cachep);
  759. mempool_destroy(cifs_sm_req_poolp);
  760. kmem_cache_destroy(cifs_sm_req_cachep);
  761. }
  762. static int
  763. cifs_init_mids(void)
  764. {
  765. cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
  766. sizeof(struct mid_q_entry), 0,
  767. SLAB_HWCACHE_ALIGN, NULL);
  768. if (cifs_mid_cachep == NULL)
  769. return -ENOMEM;
  770. /* 3 is a reasonable minimum number of simultaneous operations */
  771. cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
  772. if (cifs_mid_poolp == NULL) {
  773. kmem_cache_destroy(cifs_mid_cachep);
  774. return -ENOMEM;
  775. }
  776. return 0;
  777. }
  778. static void
  779. cifs_destroy_mids(void)
  780. {
  781. mempool_destroy(cifs_mid_poolp);
  782. kmem_cache_destroy(cifs_mid_cachep);
  783. }
  784. static int __init
  785. init_cifs(void)
  786. {
  787. int rc = 0;
  788. cifs_proc_init();
  789. INIT_LIST_HEAD(&cifs_tcp_ses_list);
  790. #ifdef CONFIG_CIFS_EXPERIMENTAL
  791. INIT_LIST_HEAD(&GlobalDnotifyReqList);
  792. INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
  793. #endif
  794. /*
  795. * Initialize Global counters
  796. */
  797. atomic_set(&sesInfoAllocCount, 0);
  798. atomic_set(&tconInfoAllocCount, 0);
  799. atomic_set(&tcpSesAllocCount, 0);
  800. atomic_set(&tcpSesReconnectCount, 0);
  801. atomic_set(&tconInfoReconnectCount, 0);
  802. atomic_set(&bufAllocCount, 0);
  803. atomic_set(&smBufAllocCount, 0);
  804. #ifdef CONFIG_CIFS_STATS2
  805. atomic_set(&totBufAllocCount, 0);
  806. atomic_set(&totSmBufAllocCount, 0);
  807. #endif /* CONFIG_CIFS_STATS2 */
  808. atomic_set(&midCount, 0);
  809. GlobalCurrentXid = 0;
  810. GlobalTotalActiveXid = 0;
  811. GlobalMaxActiveXid = 0;
  812. memset(Local_System_Name, 0, 15);
  813. rwlock_init(&GlobalSMBSeslock);
  814. rwlock_init(&cifs_tcp_ses_lock);
  815. spin_lock_init(&GlobalMid_Lock);
  816. if (cifs_max_pending < 2) {
  817. cifs_max_pending = 2;
  818. cFYI(1, "cifs_max_pending set to min of 2");
  819. } else if (cifs_max_pending > 256) {
  820. cifs_max_pending = 256;
  821. cFYI(1, "cifs_max_pending set to max of 256");
  822. }
  823. rc = cifs_fscache_register();
  824. if (rc)
  825. goto out_clean_proc;
  826. rc = cifs_init_inodecache();
  827. if (rc)
  828. goto out_unreg_fscache;
  829. rc = cifs_init_mids();
  830. if (rc)
  831. goto out_destroy_inodecache;
  832. rc = cifs_init_request_bufs();
  833. if (rc)
  834. goto out_destroy_mids;
  835. rc = register_filesystem(&cifs_fs_type);
  836. if (rc)
  837. goto out_destroy_request_bufs;
  838. #ifdef CONFIG_CIFS_UPCALL
  839. rc = register_key_type(&cifs_spnego_key_type);
  840. if (rc)
  841. goto out_unregister_filesystem;
  842. #endif
  843. return 0;
  844. #ifdef CONFIG_CIFS_UPCALL
  845. out_unregister_filesystem:
  846. unregister_filesystem(&cifs_fs_type);
  847. #endif
  848. out_destroy_request_bufs:
  849. cifs_destroy_request_bufs();
  850. out_destroy_mids:
  851. cifs_destroy_mids();
  852. out_destroy_inodecache:
  853. cifs_destroy_inodecache();
  854. out_unreg_fscache:
  855. cifs_fscache_unregister();
  856. out_clean_proc:
  857. cifs_proc_clean();
  858. return rc;
  859. }
  860. static void __exit
  861. exit_cifs(void)
  862. {
  863. cFYI(DBG2, "exit_cifs");
  864. cifs_proc_clean();
  865. cifs_fscache_unregister();
  866. #ifdef CONFIG_CIFS_DFS_UPCALL
  867. cifs_dfs_release_automount_timer();
  868. #endif
  869. #ifdef CONFIG_CIFS_UPCALL
  870. unregister_key_type(&cifs_spnego_key_type);
  871. #endif
  872. unregister_filesystem(&cifs_fs_type);
  873. cifs_destroy_inodecache();
  874. cifs_destroy_mids();
  875. cifs_destroy_request_bufs();
  876. }
  877. MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
  878. MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
  879. MODULE_DESCRIPTION
  880. ("VFS to access servers complying with the SNIA CIFS Specification "
  881. "e.g. Samba and Windows");
  882. MODULE_VERSION(CIFS_VERSION);
  883. module_init(init_cifs)
  884. module_exit(exit_cifs)