cifsfs.c 26 KB

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