cifsfs.c 26 KB

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