cifsfs.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2008
  5. * Author(s): Steve French ([email protected])
  6. *
  7. * Common Internet FileSystem (CIFS) client
  8. *
  9. */
  10. /* Note that BB means BUGBUG (ie something to fix eventually) */
  11. #include <linux/module.h>
  12. #include <linux/fs.h>
  13. #include <linux/mount.h>
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/list.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/vfs.h>
  19. #include <linux/mempool.h>
  20. #include <linux/delay.h>
  21. #include <linux/kthread.h>
  22. #include <linux/freezer.h>
  23. #include <linux/namei.h>
  24. #include <linux/random.h>
  25. #include <linux/uuid.h>
  26. #include <linux/xattr.h>
  27. #include <uapi/linux/magic.h>
  28. #include <net/ipv6.h>
  29. #include "cifsfs.h"
  30. #include "cifspdu.h"
  31. #define DECLARE_GLOBALS_HERE
  32. #include "cifsglob.h"
  33. #include "cifsproto.h"
  34. #include "cifs_debug.h"
  35. #include "cifs_fs_sb.h"
  36. #include <linux/mm.h>
  37. #include <linux/key-type.h>
  38. #include "cifs_spnego.h"
  39. #include "fscache.h"
  40. #ifdef CONFIG_CIFS_DFS_UPCALL
  41. #include "dfs_cache.h"
  42. #endif
  43. #ifdef CONFIG_CIFS_SWN_UPCALL
  44. #include "netlink.h"
  45. #endif
  46. #include "fs_context.h"
  47. #include "cached_dir.h"
  48. /*
  49. * DOS dates from 1980/1/1 through 2107/12/31
  50. * Protocol specifications indicate the range should be to 119, which
  51. * limits maximum year to 2099. But this range has not been checked.
  52. */
  53. #define SMB_DATE_MAX (127<<9 | 12<<5 | 31)
  54. #define SMB_DATE_MIN (0<<9 | 1<<5 | 1)
  55. #define SMB_TIME_MAX (23<<11 | 59<<5 | 29)
  56. int cifsFYI = 0;
  57. bool traceSMB;
  58. bool enable_oplocks = true;
  59. bool linuxExtEnabled = true;
  60. bool lookupCacheEnabled = true;
  61. bool disable_legacy_dialects; /* false by default */
  62. bool enable_gcm_256 = true;
  63. bool require_gcm_256; /* false by default */
  64. bool enable_negotiate_signing; /* false by default */
  65. unsigned int global_secflags = CIFSSEC_DEF;
  66. /* unsigned int ntlmv2_support = 0; */
  67. unsigned int sign_CIFS_PDUs = 1;
  68. /*
  69. * Global transaction id (XID) information
  70. */
  71. unsigned int GlobalCurrentXid; /* protected by GlobalMid_Sem */
  72. unsigned int GlobalTotalActiveXid; /* prot by GlobalMid_Sem */
  73. unsigned int GlobalMaxActiveXid; /* prot by GlobalMid_Sem */
  74. spinlock_t GlobalMid_Lock; /* protects above & list operations on midQ entries */
  75. /*
  76. * Global counters, updated atomically
  77. */
  78. atomic_t sesInfoAllocCount;
  79. atomic_t tconInfoAllocCount;
  80. atomic_t tcpSesNextId;
  81. atomic_t tcpSesAllocCount;
  82. atomic_t tcpSesReconnectCount;
  83. atomic_t tconInfoReconnectCount;
  84. atomic_t mid_count;
  85. atomic_t buf_alloc_count;
  86. atomic_t small_buf_alloc_count;
  87. #ifdef CONFIG_CIFS_STATS2
  88. atomic_t total_buf_alloc_count;
  89. atomic_t total_small_buf_alloc_count;
  90. #endif/* STATS2 */
  91. struct list_head cifs_tcp_ses_list;
  92. spinlock_t cifs_tcp_ses_lock;
  93. static const struct super_operations cifs_super_ops;
  94. unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
  95. module_param(CIFSMaxBufSize, uint, 0444);
  96. MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header) "
  97. "for CIFS requests. "
  98. "Default: 16384 Range: 8192 to 130048");
  99. unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
  100. module_param(cifs_min_rcv, uint, 0444);
  101. MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
  102. "1 to 64");
  103. unsigned int cifs_min_small = 30;
  104. module_param(cifs_min_small, uint, 0444);
  105. MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
  106. "Range: 2 to 256");
  107. unsigned int cifs_max_pending = CIFS_MAX_REQ;
  108. module_param(cifs_max_pending, uint, 0444);
  109. MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server for "
  110. "CIFS/SMB1 dialect (N/A for SMB3) "
  111. "Default: 32767 Range: 2 to 32767.");
  112. #ifdef CONFIG_CIFS_STATS2
  113. unsigned int slow_rsp_threshold = 1;
  114. module_param(slow_rsp_threshold, uint, 0644);
  115. MODULE_PARM_DESC(slow_rsp_threshold, "Amount of time (in seconds) to wait "
  116. "before logging that a response is delayed. "
  117. "Default: 1 (if set to 0 disables msg).");
  118. #endif /* STATS2 */
  119. module_param(enable_oplocks, bool, 0644);
  120. MODULE_PARM_DESC(enable_oplocks, "Enable or disable oplocks. Default: y/Y/1");
  121. module_param(enable_gcm_256, bool, 0644);
  122. MODULE_PARM_DESC(enable_gcm_256, "Enable requesting strongest (256 bit) GCM encryption. Default: n/N/0");
  123. module_param(require_gcm_256, bool, 0644);
  124. MODULE_PARM_DESC(require_gcm_256, "Require strongest (256 bit) GCM encryption. Default: n/N/0");
  125. module_param(enable_negotiate_signing, bool, 0644);
  126. MODULE_PARM_DESC(enable_negotiate_signing, "Enable negotiating packet signing algorithm with server. Default: n/N/0");
  127. module_param(disable_legacy_dialects, bool, 0644);
  128. MODULE_PARM_DESC(disable_legacy_dialects, "To improve security it may be "
  129. "helpful to restrict the ability to "
  130. "override the default dialects (SMB2.1, "
  131. "SMB3 and SMB3.02) on mount with old "
  132. "dialects (CIFS/SMB1 and SMB2) since "
  133. "vers=1.0 (CIFS/SMB1) and vers=2.0 are weaker"
  134. " and less secure. Default: n/N/0");
  135. extern mempool_t *cifs_sm_req_poolp;
  136. extern mempool_t *cifs_req_poolp;
  137. extern mempool_t *cifs_mid_poolp;
  138. struct workqueue_struct *cifsiod_wq;
  139. struct workqueue_struct *decrypt_wq;
  140. struct workqueue_struct *fileinfo_put_wq;
  141. struct workqueue_struct *cifsoplockd_wq;
  142. struct workqueue_struct *deferredclose_wq;
  143. __u32 cifs_lock_secret;
  144. /*
  145. * Bumps refcount for cifs super block.
  146. * Note that it should be only called if a referece to VFS super block is
  147. * already held, e.g. in open-type syscalls context. Otherwise it can race with
  148. * atomic_dec_and_test in deactivate_locked_super.
  149. */
  150. void
  151. cifs_sb_active(struct super_block *sb)
  152. {
  153. struct cifs_sb_info *server = CIFS_SB(sb);
  154. if (atomic_inc_return(&server->active) == 1)
  155. atomic_inc(&sb->s_active);
  156. }
  157. void
  158. cifs_sb_deactive(struct super_block *sb)
  159. {
  160. struct cifs_sb_info *server = CIFS_SB(sb);
  161. if (atomic_dec_and_test(&server->active))
  162. deactivate_super(sb);
  163. }
  164. static int
  165. cifs_read_super(struct super_block *sb)
  166. {
  167. struct inode *inode;
  168. struct cifs_sb_info *cifs_sb;
  169. struct cifs_tcon *tcon;
  170. struct timespec64 ts;
  171. int rc = 0;
  172. cifs_sb = CIFS_SB(sb);
  173. tcon = cifs_sb_master_tcon(cifs_sb);
  174. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIXACL)
  175. sb->s_flags |= SB_POSIXACL;
  176. if (tcon->snapshot_time)
  177. sb->s_flags |= SB_RDONLY;
  178. if (tcon->ses->capabilities & tcon->ses->server->vals->cap_large_files)
  179. sb->s_maxbytes = MAX_LFS_FILESIZE;
  180. else
  181. sb->s_maxbytes = MAX_NON_LFS;
  182. /*
  183. * Some very old servers like DOS and OS/2 used 2 second granularity
  184. * (while all current servers use 100ns granularity - see MS-DTYP)
  185. * but 1 second is the maximum allowed granularity for the VFS
  186. * so for old servers set time granularity to 1 second while for
  187. * everything else (current servers) set it to 100ns.
  188. */
  189. if ((tcon->ses->server->vals->protocol_id == SMB10_PROT_ID) &&
  190. ((tcon->ses->capabilities &
  191. tcon->ses->server->vals->cap_nt_find) == 0) &&
  192. !tcon->unix_ext) {
  193. sb->s_time_gran = 1000000000; /* 1 second is max allowed gran */
  194. ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MIN), 0, 0);
  195. sb->s_time_min = ts.tv_sec;
  196. ts = cnvrtDosUnixTm(cpu_to_le16(SMB_DATE_MAX),
  197. cpu_to_le16(SMB_TIME_MAX), 0);
  198. sb->s_time_max = ts.tv_sec;
  199. } else {
  200. /*
  201. * Almost every server, including all SMB2+, uses DCE TIME
  202. * ie 100 nanosecond units, since 1601. See MS-DTYP and MS-FSCC
  203. */
  204. sb->s_time_gran = 100;
  205. ts = cifs_NTtimeToUnix(0);
  206. sb->s_time_min = ts.tv_sec;
  207. ts = cifs_NTtimeToUnix(cpu_to_le64(S64_MAX));
  208. sb->s_time_max = ts.tv_sec;
  209. }
  210. sb->s_magic = CIFS_SUPER_MAGIC;
  211. sb->s_op = &cifs_super_ops;
  212. sb->s_xattr = cifs_xattr_handlers;
  213. rc = super_setup_bdi(sb);
  214. if (rc)
  215. goto out_no_root;
  216. /* tune readahead according to rsize if readahead size not set on mount */
  217. if (cifs_sb->ctx->rsize == 0)
  218. cifs_sb->ctx->rsize =
  219. tcon->ses->server->ops->negotiate_rsize(tcon, cifs_sb->ctx);
  220. if (cifs_sb->ctx->rasize)
  221. sb->s_bdi->ra_pages = cifs_sb->ctx->rasize / PAGE_SIZE;
  222. else
  223. sb->s_bdi->ra_pages = cifs_sb->ctx->rsize / PAGE_SIZE;
  224. sb->s_blocksize = CIFS_MAX_MSGSIZE;
  225. sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
  226. inode = cifs_root_iget(sb);
  227. if (IS_ERR(inode)) {
  228. rc = PTR_ERR(inode);
  229. goto out_no_root;
  230. }
  231. if (tcon->nocase)
  232. sb->s_d_op = &cifs_ci_dentry_ops;
  233. else
  234. sb->s_d_op = &cifs_dentry_ops;
  235. sb->s_root = d_make_root(inode);
  236. if (!sb->s_root) {
  237. rc = -ENOMEM;
  238. goto out_no_root;
  239. }
  240. #ifdef CONFIG_CIFS_NFSD_EXPORT
  241. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  242. cifs_dbg(FYI, "export ops supported\n");
  243. sb->s_export_op = &cifs_export_ops;
  244. }
  245. #endif /* CONFIG_CIFS_NFSD_EXPORT */
  246. return 0;
  247. out_no_root:
  248. cifs_dbg(VFS, "%s: get root inode failed\n", __func__);
  249. return rc;
  250. }
  251. static void cifs_kill_sb(struct super_block *sb)
  252. {
  253. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  254. /*
  255. * We ned to release all dentries for the cached directories
  256. * before we kill the sb.
  257. */
  258. if (cifs_sb->root) {
  259. close_all_cached_dirs(cifs_sb);
  260. /* finally release root dentry */
  261. dput(cifs_sb->root);
  262. cifs_sb->root = NULL;
  263. }
  264. kill_anon_super(sb);
  265. cifs_umount(cifs_sb);
  266. }
  267. static int
  268. cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
  269. {
  270. struct super_block *sb = dentry->d_sb;
  271. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  272. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  273. struct TCP_Server_Info *server = tcon->ses->server;
  274. unsigned int xid;
  275. int rc = 0;
  276. xid = get_xid();
  277. if (le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength) > 0)
  278. buf->f_namelen =
  279. le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength);
  280. else
  281. buf->f_namelen = PATH_MAX;
  282. buf->f_fsid.val[0] = tcon->vol_serial_number;
  283. /* are using part of create time for more randomness, see man statfs */
  284. buf->f_fsid.val[1] = (int)le64_to_cpu(tcon->vol_create_time);
  285. buf->f_files = 0; /* undefined */
  286. buf->f_ffree = 0; /* unlimited */
  287. if (server->ops->queryfs)
  288. rc = server->ops->queryfs(xid, tcon, cifs_sb, buf);
  289. free_xid(xid);
  290. return rc;
  291. }
  292. static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
  293. {
  294. struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
  295. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  296. struct TCP_Server_Info *server = tcon->ses->server;
  297. if (server->ops->fallocate)
  298. return server->ops->fallocate(file, tcon, mode, off, len);
  299. return -EOPNOTSUPP;
  300. }
  301. static int cifs_permission(struct user_namespace *mnt_userns,
  302. struct inode *inode, int mask)
  303. {
  304. struct cifs_sb_info *cifs_sb;
  305. cifs_sb = CIFS_SB(inode->i_sb);
  306. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
  307. if ((mask & MAY_EXEC) && !execute_ok(inode))
  308. return -EACCES;
  309. else
  310. return 0;
  311. } else /* file mode might have been restricted at mount time
  312. on the client (above and beyond ACL on servers) for
  313. servers which do not support setting and viewing mode bits,
  314. so allowing client to check permissions is useful */
  315. return generic_permission(&init_user_ns, inode, mask);
  316. }
  317. static struct kmem_cache *cifs_inode_cachep;
  318. static struct kmem_cache *cifs_req_cachep;
  319. static struct kmem_cache *cifs_mid_cachep;
  320. static struct kmem_cache *cifs_sm_req_cachep;
  321. mempool_t *cifs_sm_req_poolp;
  322. mempool_t *cifs_req_poolp;
  323. mempool_t *cifs_mid_poolp;
  324. static struct inode *
  325. cifs_alloc_inode(struct super_block *sb)
  326. {
  327. struct cifsInodeInfo *cifs_inode;
  328. cifs_inode = alloc_inode_sb(sb, cifs_inode_cachep, GFP_KERNEL);
  329. if (!cifs_inode)
  330. return NULL;
  331. cifs_inode->cifsAttrs = 0x20; /* default */
  332. cifs_inode->time = 0;
  333. /*
  334. * Until the file is open and we have gotten oplock info back from the
  335. * server, can not assume caching of file data or metadata.
  336. */
  337. cifs_set_oplock_level(cifs_inode, 0);
  338. cifs_inode->flags = 0;
  339. spin_lock_init(&cifs_inode->writers_lock);
  340. cifs_inode->writers = 0;
  341. cifs_inode->netfs.inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
  342. cifs_inode->server_eof = 0;
  343. cifs_inode->uniqueid = 0;
  344. cifs_inode->createtime = 0;
  345. cifs_inode->epoch = 0;
  346. spin_lock_init(&cifs_inode->open_file_lock);
  347. generate_random_uuid(cifs_inode->lease_key);
  348. cifs_inode->symlink_target = NULL;
  349. /*
  350. * Can not set i_flags here - they get immediately overwritten to zero
  351. * by the VFS.
  352. */
  353. /* cifs_inode->netfs.inode.i_flags = S_NOATIME | S_NOCMTIME; */
  354. INIT_LIST_HEAD(&cifs_inode->openFileList);
  355. INIT_LIST_HEAD(&cifs_inode->llist);
  356. INIT_LIST_HEAD(&cifs_inode->deferred_closes);
  357. spin_lock_init(&cifs_inode->deferred_lock);
  358. return &cifs_inode->netfs.inode;
  359. }
  360. static void
  361. cifs_free_inode(struct inode *inode)
  362. {
  363. struct cifsInodeInfo *cinode = CIFS_I(inode);
  364. if (S_ISLNK(inode->i_mode))
  365. kfree(cinode->symlink_target);
  366. kmem_cache_free(cifs_inode_cachep, cinode);
  367. }
  368. static void
  369. cifs_evict_inode(struct inode *inode)
  370. {
  371. truncate_inode_pages_final(&inode->i_data);
  372. if (inode->i_state & I_PINNING_FSCACHE_WB)
  373. cifs_fscache_unuse_inode_cookie(inode, true);
  374. cifs_fscache_release_inode_cookie(inode);
  375. clear_inode(inode);
  376. }
  377. static void
  378. cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
  379. {
  380. struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
  381. struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
  382. seq_puts(s, ",addr=");
  383. switch (server->dstaddr.ss_family) {
  384. case AF_INET:
  385. seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
  386. break;
  387. case AF_INET6:
  388. seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
  389. if (sa6->sin6_scope_id)
  390. seq_printf(s, "%%%u", sa6->sin6_scope_id);
  391. break;
  392. default:
  393. seq_puts(s, "(unknown)");
  394. }
  395. if (server->rdma)
  396. seq_puts(s, ",rdma");
  397. }
  398. static void
  399. cifs_show_security(struct seq_file *s, struct cifs_ses *ses)
  400. {
  401. if (ses->sectype == Unspecified) {
  402. if (ses->user_name == NULL)
  403. seq_puts(s, ",sec=none");
  404. return;
  405. }
  406. seq_puts(s, ",sec=");
  407. switch (ses->sectype) {
  408. case NTLMv2:
  409. seq_puts(s, "ntlmv2");
  410. break;
  411. case Kerberos:
  412. seq_puts(s, "krb5");
  413. break;
  414. case RawNTLMSSP:
  415. seq_puts(s, "ntlmssp");
  416. break;
  417. default:
  418. /* shouldn't ever happen */
  419. seq_puts(s, "unknown");
  420. break;
  421. }
  422. if (ses->sign)
  423. seq_puts(s, "i");
  424. if (ses->sectype == Kerberos)
  425. seq_printf(s, ",cruid=%u",
  426. from_kuid_munged(&init_user_ns, ses->cred_uid));
  427. }
  428. static void
  429. cifs_show_cache_flavor(struct seq_file *s, struct cifs_sb_info *cifs_sb)
  430. {
  431. seq_puts(s, ",cache=");
  432. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
  433. seq_puts(s, "strict");
  434. else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
  435. seq_puts(s, "none");
  436. else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
  437. seq_puts(s, "singleclient"); /* assume only one client access */
  438. else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE)
  439. seq_puts(s, "ro"); /* read only caching assumed */
  440. else
  441. seq_puts(s, "loose");
  442. }
  443. /*
  444. * cifs_show_devname() is used so we show the mount device name with correct
  445. * format (e.g. forward slashes vs. back slashes) in /proc/mounts
  446. */
  447. static int cifs_show_devname(struct seq_file *m, struct dentry *root)
  448. {
  449. struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
  450. char *devname = kstrdup(cifs_sb->ctx->source, GFP_KERNEL);
  451. if (devname == NULL)
  452. seq_puts(m, "none");
  453. else {
  454. convert_delimiter(devname, '/');
  455. /* escape all spaces in share names */
  456. seq_escape(m, devname, " \t");
  457. kfree(devname);
  458. }
  459. return 0;
  460. }
  461. /*
  462. * cifs_show_options() is for displaying mount options in /proc/mounts.
  463. * Not all settable options are displayed but most of the important
  464. * ones are.
  465. */
  466. static int
  467. cifs_show_options(struct seq_file *s, struct dentry *root)
  468. {
  469. struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
  470. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  471. struct sockaddr *srcaddr;
  472. srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
  473. seq_show_option(s, "vers", tcon->ses->server->vals->version_string);
  474. cifs_show_security(s, tcon->ses);
  475. cifs_show_cache_flavor(s, cifs_sb);
  476. if (tcon->no_lease)
  477. seq_puts(s, ",nolease");
  478. if (cifs_sb->ctx->multiuser)
  479. seq_puts(s, ",multiuser");
  480. else if (tcon->ses->user_name)
  481. seq_show_option(s, "username", tcon->ses->user_name);
  482. if (tcon->ses->domainName && tcon->ses->domainName[0] != 0)
  483. seq_show_option(s, "domain", tcon->ses->domainName);
  484. if (srcaddr->sa_family != AF_UNSPEC) {
  485. struct sockaddr_in *saddr4;
  486. struct sockaddr_in6 *saddr6;
  487. saddr4 = (struct sockaddr_in *)srcaddr;
  488. saddr6 = (struct sockaddr_in6 *)srcaddr;
  489. if (srcaddr->sa_family == AF_INET6)
  490. seq_printf(s, ",srcaddr=%pI6c",
  491. &saddr6->sin6_addr);
  492. else if (srcaddr->sa_family == AF_INET)
  493. seq_printf(s, ",srcaddr=%pI4",
  494. &saddr4->sin_addr.s_addr);
  495. else
  496. seq_printf(s, ",srcaddr=BAD-AF:%i",
  497. (int)(srcaddr->sa_family));
  498. }
  499. seq_printf(s, ",uid=%u",
  500. from_kuid_munged(&init_user_ns, cifs_sb->ctx->linux_uid));
  501. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
  502. seq_puts(s, ",forceuid");
  503. else
  504. seq_puts(s, ",noforceuid");
  505. seq_printf(s, ",gid=%u",
  506. from_kgid_munged(&init_user_ns, cifs_sb->ctx->linux_gid));
  507. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
  508. seq_puts(s, ",forcegid");
  509. else
  510. seq_puts(s, ",noforcegid");
  511. cifs_show_address(s, tcon->ses->server);
  512. if (!tcon->unix_ext)
  513. seq_printf(s, ",file_mode=0%ho,dir_mode=0%ho",
  514. cifs_sb->ctx->file_mode,
  515. cifs_sb->ctx->dir_mode);
  516. if (cifs_sb->ctx->iocharset)
  517. seq_printf(s, ",iocharset=%s", cifs_sb->ctx->iocharset);
  518. if (tcon->seal)
  519. seq_puts(s, ",seal");
  520. else if (tcon->ses->server->ignore_signature)
  521. seq_puts(s, ",signloosely");
  522. if (tcon->nocase)
  523. seq_puts(s, ",nocase");
  524. if (tcon->nodelete)
  525. seq_puts(s, ",nodelete");
  526. if (cifs_sb->ctx->no_sparse)
  527. seq_puts(s, ",nosparse");
  528. if (tcon->local_lease)
  529. seq_puts(s, ",locallease");
  530. if (tcon->retry)
  531. seq_puts(s, ",hard");
  532. else
  533. seq_puts(s, ",soft");
  534. if (tcon->use_persistent)
  535. seq_puts(s, ",persistenthandles");
  536. else if (tcon->use_resilient)
  537. seq_puts(s, ",resilienthandles");
  538. if (tcon->posix_extensions)
  539. seq_puts(s, ",posix");
  540. else if (tcon->unix_ext)
  541. seq_puts(s, ",unix");
  542. else
  543. seq_puts(s, ",nounix");
  544. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
  545. seq_puts(s, ",nodfs");
  546. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
  547. seq_puts(s, ",posixpaths");
  548. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
  549. seq_puts(s, ",setuids");
  550. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
  551. seq_puts(s, ",idsfromsid");
  552. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  553. seq_puts(s, ",serverino");
  554. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  555. seq_puts(s, ",rwpidforward");
  556. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL)
  557. seq_puts(s, ",forcemand");
  558. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  559. seq_puts(s, ",nouser_xattr");
  560. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
  561. seq_puts(s, ",mapchars");
  562. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR)
  563. seq_puts(s, ",mapposix");
  564. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
  565. seq_puts(s, ",sfu");
  566. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  567. seq_puts(s, ",nobrl");
  568. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_HANDLE_CACHE)
  569. seq_puts(s, ",nohandlecache");
  570. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)
  571. seq_puts(s, ",modefromsid");
  572. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
  573. seq_puts(s, ",cifsacl");
  574. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  575. seq_puts(s, ",dynperm");
  576. if (root->d_sb->s_flags & SB_POSIXACL)
  577. seq_puts(s, ",acl");
  578. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
  579. seq_puts(s, ",mfsymlinks");
  580. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
  581. seq_puts(s, ",fsc");
  582. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)
  583. seq_puts(s, ",nostrictsync");
  584. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
  585. seq_puts(s, ",noperm");
  586. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID)
  587. seq_printf(s, ",backupuid=%u",
  588. from_kuid_munged(&init_user_ns,
  589. cifs_sb->ctx->backupuid));
  590. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID)
  591. seq_printf(s, ",backupgid=%u",
  592. from_kgid_munged(&init_user_ns,
  593. cifs_sb->ctx->backupgid));
  594. seq_printf(s, ",rsize=%u", cifs_sb->ctx->rsize);
  595. seq_printf(s, ",wsize=%u", cifs_sb->ctx->wsize);
  596. seq_printf(s, ",bsize=%u", cifs_sb->ctx->bsize);
  597. if (cifs_sb->ctx->rasize)
  598. seq_printf(s, ",rasize=%u", cifs_sb->ctx->rasize);
  599. if (tcon->ses->server->min_offload)
  600. seq_printf(s, ",esize=%u", tcon->ses->server->min_offload);
  601. seq_printf(s, ",echo_interval=%lu",
  602. tcon->ses->server->echo_interval / HZ);
  603. /* Only display the following if overridden on mount */
  604. if (tcon->ses->server->max_credits != SMB2_MAX_CREDITS_AVAILABLE)
  605. seq_printf(s, ",max_credits=%u", tcon->ses->server->max_credits);
  606. if (tcon->ses->server->tcp_nodelay)
  607. seq_puts(s, ",tcpnodelay");
  608. if (tcon->ses->server->noautotune)
  609. seq_puts(s, ",noautotune");
  610. if (tcon->ses->server->noblocksnd)
  611. seq_puts(s, ",noblocksend");
  612. if (tcon->snapshot_time)
  613. seq_printf(s, ",snapshot=%llu", tcon->snapshot_time);
  614. if (tcon->handle_timeout)
  615. seq_printf(s, ",handletimeout=%u", tcon->handle_timeout);
  616. /*
  617. * Display file and directory attribute timeout in seconds.
  618. * If file and directory attribute timeout the same then actimeo
  619. * was likely specified on mount
  620. */
  621. if (cifs_sb->ctx->acdirmax == cifs_sb->ctx->acregmax)
  622. seq_printf(s, ",actimeo=%lu", cifs_sb->ctx->acregmax / HZ);
  623. else {
  624. seq_printf(s, ",acdirmax=%lu", cifs_sb->ctx->acdirmax / HZ);
  625. seq_printf(s, ",acregmax=%lu", cifs_sb->ctx->acregmax / HZ);
  626. }
  627. seq_printf(s, ",closetimeo=%lu", cifs_sb->ctx->closetimeo / HZ);
  628. if (tcon->ses->chan_max > 1)
  629. seq_printf(s, ",multichannel,max_channels=%zu",
  630. tcon->ses->chan_max);
  631. if (tcon->use_witness)
  632. seq_puts(s, ",witness");
  633. return 0;
  634. }
  635. static void cifs_umount_begin(struct super_block *sb)
  636. {
  637. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  638. struct cifs_tcon *tcon;
  639. if (cifs_sb == NULL)
  640. return;
  641. tcon = cifs_sb_master_tcon(cifs_sb);
  642. spin_lock(&cifs_tcp_ses_lock);
  643. spin_lock(&tcon->tc_lock);
  644. if ((tcon->tc_count > 1) || (tcon->status == TID_EXITING)) {
  645. /* we have other mounts to same share or we have
  646. already tried to umount this and woken up
  647. all waiting network requests, nothing to do */
  648. spin_unlock(&tcon->tc_lock);
  649. spin_unlock(&cifs_tcp_ses_lock);
  650. return;
  651. }
  652. /*
  653. * can not set tcon->status to TID_EXITING yet since we don't know if umount -f will
  654. * fail later (e.g. due to open files). TID_EXITING will be set just before tdis req sent
  655. */
  656. spin_unlock(&tcon->tc_lock);
  657. spin_unlock(&cifs_tcp_ses_lock);
  658. cifs_close_all_deferred_files(tcon);
  659. /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
  660. /* cancel_notify_requests(tcon); */
  661. if (tcon->ses && tcon->ses->server) {
  662. cifs_dbg(FYI, "wake up tasks now - umount begin not complete\n");
  663. wake_up_all(&tcon->ses->server->request_q);
  664. wake_up_all(&tcon->ses->server->response_q);
  665. msleep(1); /* yield */
  666. /* we have to kick the requests once more */
  667. wake_up_all(&tcon->ses->server->response_q);
  668. msleep(1);
  669. }
  670. return;
  671. }
  672. static int cifs_freeze(struct super_block *sb)
  673. {
  674. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  675. struct cifs_tcon *tcon;
  676. if (cifs_sb == NULL)
  677. return 0;
  678. tcon = cifs_sb_master_tcon(cifs_sb);
  679. cifs_close_all_deferred_files(tcon);
  680. return 0;
  681. }
  682. #ifdef CONFIG_CIFS_STATS2
  683. static int cifs_show_stats(struct seq_file *s, struct dentry *root)
  684. {
  685. /* BB FIXME */
  686. return 0;
  687. }
  688. #endif
  689. static int cifs_write_inode(struct inode *inode, struct writeback_control *wbc)
  690. {
  691. fscache_unpin_writeback(wbc, cifs_inode_cookie(inode));
  692. return 0;
  693. }
  694. static int cifs_drop_inode(struct inode *inode)
  695. {
  696. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  697. /* no serverino => unconditional eviction */
  698. return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
  699. generic_drop_inode(inode);
  700. }
  701. static const struct super_operations cifs_super_ops = {
  702. .statfs = cifs_statfs,
  703. .alloc_inode = cifs_alloc_inode,
  704. .write_inode = cifs_write_inode,
  705. .free_inode = cifs_free_inode,
  706. .drop_inode = cifs_drop_inode,
  707. .evict_inode = cifs_evict_inode,
  708. /* .show_path = cifs_show_path, */ /* Would we ever need show path? */
  709. .show_devname = cifs_show_devname,
  710. /* .delete_inode = cifs_delete_inode, */ /* Do not need above
  711. function unless later we add lazy close of inodes or unless the
  712. kernel forgets to call us with the same number of releases (closes)
  713. as opens */
  714. .show_options = cifs_show_options,
  715. .umount_begin = cifs_umount_begin,
  716. .freeze_fs = cifs_freeze,
  717. #ifdef CONFIG_CIFS_STATS2
  718. .show_stats = cifs_show_stats,
  719. #endif
  720. };
  721. /*
  722. * Get root dentry from superblock according to prefix path mount option.
  723. * Return dentry with refcount + 1 on success and NULL otherwise.
  724. */
  725. static struct dentry *
  726. cifs_get_root(struct smb3_fs_context *ctx, struct super_block *sb)
  727. {
  728. struct dentry *dentry;
  729. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  730. char *full_path = NULL;
  731. char *s, *p;
  732. char sep;
  733. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
  734. return dget(sb->s_root);
  735. full_path = cifs_build_path_to_root(ctx, cifs_sb,
  736. cifs_sb_master_tcon(cifs_sb), 0);
  737. if (full_path == NULL)
  738. return ERR_PTR(-ENOMEM);
  739. cifs_dbg(FYI, "Get root dentry for %s\n", full_path);
  740. sep = CIFS_DIR_SEP(cifs_sb);
  741. dentry = dget(sb->s_root);
  742. s = full_path;
  743. do {
  744. struct inode *dir = d_inode(dentry);
  745. struct dentry *child;
  746. if (!S_ISDIR(dir->i_mode)) {
  747. dput(dentry);
  748. dentry = ERR_PTR(-ENOTDIR);
  749. break;
  750. }
  751. /* skip separators */
  752. while (*s == sep)
  753. s++;
  754. if (!*s)
  755. break;
  756. p = s++;
  757. /* next separator */
  758. while (*s && *s != sep)
  759. s++;
  760. child = lookup_positive_unlocked(p, dentry, s - p);
  761. dput(dentry);
  762. dentry = child;
  763. } while (!IS_ERR(dentry));
  764. kfree(full_path);
  765. return dentry;
  766. }
  767. static int cifs_set_super(struct super_block *sb, void *data)
  768. {
  769. struct cifs_mnt_data *mnt_data = data;
  770. sb->s_fs_info = mnt_data->cifs_sb;
  771. return set_anon_super(sb, NULL);
  772. }
  773. struct dentry *
  774. cifs_smb3_do_mount(struct file_system_type *fs_type,
  775. int flags, struct smb3_fs_context *old_ctx)
  776. {
  777. struct cifs_mnt_data mnt_data;
  778. struct cifs_sb_info *cifs_sb;
  779. struct super_block *sb;
  780. struct dentry *root;
  781. int rc;
  782. /*
  783. * Prints in Kernel / CIFS log the attempted mount operation
  784. * If CIFS_DEBUG && cifs_FYI
  785. */
  786. if (cifsFYI)
  787. cifs_dbg(FYI, "Devname: %s flags: %d\n", old_ctx->UNC, flags);
  788. else
  789. cifs_info("Attempting to mount %s\n", old_ctx->UNC);
  790. cifs_sb = kzalloc(sizeof(*cifs_sb), GFP_KERNEL);
  791. if (!cifs_sb)
  792. return ERR_PTR(-ENOMEM);
  793. cifs_sb->ctx = kzalloc(sizeof(struct smb3_fs_context), GFP_KERNEL);
  794. if (!cifs_sb->ctx) {
  795. root = ERR_PTR(-ENOMEM);
  796. goto out;
  797. }
  798. rc = smb3_fs_context_dup(cifs_sb->ctx, old_ctx);
  799. if (rc) {
  800. root = ERR_PTR(rc);
  801. goto out;
  802. }
  803. rc = cifs_setup_volume_info(cifs_sb->ctx, NULL, NULL);
  804. if (rc) {
  805. root = ERR_PTR(rc);
  806. goto out;
  807. }
  808. rc = cifs_setup_cifs_sb(cifs_sb);
  809. if (rc) {
  810. root = ERR_PTR(rc);
  811. goto out;
  812. }
  813. rc = cifs_mount(cifs_sb, cifs_sb->ctx);
  814. if (rc) {
  815. if (!(flags & SB_SILENT))
  816. cifs_dbg(VFS, "cifs_mount failed w/return code = %d\n",
  817. rc);
  818. root = ERR_PTR(rc);
  819. goto out;
  820. }
  821. mnt_data.ctx = cifs_sb->ctx;
  822. mnt_data.cifs_sb = cifs_sb;
  823. mnt_data.flags = flags;
  824. /* BB should we make this contingent on mount parm? */
  825. flags |= SB_NODIRATIME | SB_NOATIME;
  826. sb = sget(fs_type, cifs_match_super, cifs_set_super, flags, &mnt_data);
  827. if (IS_ERR(sb)) {
  828. cifs_umount(cifs_sb);
  829. return ERR_CAST(sb);
  830. }
  831. if (sb->s_root) {
  832. cifs_dbg(FYI, "Use existing superblock\n");
  833. cifs_umount(cifs_sb);
  834. cifs_sb = NULL;
  835. } else {
  836. rc = cifs_read_super(sb);
  837. if (rc) {
  838. root = ERR_PTR(rc);
  839. goto out_super;
  840. }
  841. sb->s_flags |= SB_ACTIVE;
  842. }
  843. root = cifs_get_root(cifs_sb ? cifs_sb->ctx : old_ctx, sb);
  844. if (IS_ERR(root))
  845. goto out_super;
  846. if (cifs_sb)
  847. cifs_sb->root = dget(root);
  848. cifs_dbg(FYI, "dentry root is: %p\n", root);
  849. return root;
  850. out_super:
  851. deactivate_locked_super(sb);
  852. return root;
  853. out:
  854. kfree(cifs_sb->prepath);
  855. smb3_cleanup_fs_context(cifs_sb->ctx);
  856. kfree(cifs_sb);
  857. return root;
  858. }
  859. static ssize_t
  860. cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
  861. {
  862. ssize_t rc;
  863. struct inode *inode = file_inode(iocb->ki_filp);
  864. if (iocb->ki_flags & IOCB_DIRECT)
  865. return cifs_user_readv(iocb, iter);
  866. rc = cifs_revalidate_mapping(inode);
  867. if (rc)
  868. return rc;
  869. return generic_file_read_iter(iocb, iter);
  870. }
  871. static ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  872. {
  873. struct inode *inode = file_inode(iocb->ki_filp);
  874. struct cifsInodeInfo *cinode = CIFS_I(inode);
  875. ssize_t written;
  876. int rc;
  877. if (iocb->ki_filp->f_flags & O_DIRECT) {
  878. written = cifs_user_writev(iocb, from);
  879. if (written > 0 && CIFS_CACHE_READ(cinode)) {
  880. cifs_zap_mapping(inode);
  881. cifs_dbg(FYI,
  882. "Set no oplock for inode=%p after a write operation\n",
  883. inode);
  884. cinode->oplock = 0;
  885. }
  886. return written;
  887. }
  888. written = cifs_get_writer(cinode);
  889. if (written)
  890. return written;
  891. written = generic_file_write_iter(iocb, from);
  892. if (CIFS_CACHE_WRITE(CIFS_I(inode)))
  893. goto out;
  894. rc = filemap_fdatawrite(inode->i_mapping);
  895. if (rc)
  896. cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
  897. rc, inode);
  898. out:
  899. cifs_put_writer(cinode);
  900. return written;
  901. }
  902. static loff_t cifs_llseek(struct file *file, loff_t offset, int whence)
  903. {
  904. struct cifsFileInfo *cfile = file->private_data;
  905. struct cifs_tcon *tcon;
  906. /*
  907. * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
  908. * the cached file length
  909. */
  910. if (whence != SEEK_SET && whence != SEEK_CUR) {
  911. int rc;
  912. struct inode *inode = file_inode(file);
  913. /*
  914. * We need to be sure that all dirty pages are written and the
  915. * server has the newest file length.
  916. */
  917. if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
  918. inode->i_mapping->nrpages != 0) {
  919. rc = filemap_fdatawait(inode->i_mapping);
  920. if (rc) {
  921. mapping_set_error(inode->i_mapping, rc);
  922. return rc;
  923. }
  924. }
  925. /*
  926. * Some applications poll for the file length in this strange
  927. * way so we must seek to end on non-oplocked files by
  928. * setting the revalidate time to zero.
  929. */
  930. CIFS_I(inode)->time = 0;
  931. rc = cifs_revalidate_file_attr(file);
  932. if (rc < 0)
  933. return (loff_t)rc;
  934. }
  935. if (cfile && cfile->tlink) {
  936. tcon = tlink_tcon(cfile->tlink);
  937. if (tcon->ses->server->ops->llseek)
  938. return tcon->ses->server->ops->llseek(file, tcon,
  939. offset, whence);
  940. }
  941. return generic_file_llseek(file, offset, whence);
  942. }
  943. static int
  944. cifs_setlease(struct file *file, long arg, struct file_lock **lease, void **priv)
  945. {
  946. /*
  947. * Note that this is called by vfs setlease with i_lock held to
  948. * protect *lease from going away.
  949. */
  950. struct inode *inode = file_inode(file);
  951. struct cifsFileInfo *cfile = file->private_data;
  952. if (!(S_ISREG(inode->i_mode)))
  953. return -EINVAL;
  954. /* Check if file is oplocked if this is request for new lease */
  955. if (arg == F_UNLCK ||
  956. ((arg == F_RDLCK) && CIFS_CACHE_READ(CIFS_I(inode))) ||
  957. ((arg == F_WRLCK) && CIFS_CACHE_WRITE(CIFS_I(inode))))
  958. return generic_setlease(file, arg, lease, priv);
  959. else if (tlink_tcon(cfile->tlink)->local_lease &&
  960. !CIFS_CACHE_READ(CIFS_I(inode)))
  961. /*
  962. * If the server claims to support oplock on this file, then we
  963. * still need to check oplock even if the local_lease mount
  964. * option is set, but there are servers which do not support
  965. * oplock for which this mount option may be useful if the user
  966. * knows that the file won't be changed on the server by anyone
  967. * else.
  968. */
  969. return generic_setlease(file, arg, lease, priv);
  970. else
  971. return -EAGAIN;
  972. }
  973. struct file_system_type cifs_fs_type = {
  974. .owner = THIS_MODULE,
  975. .name = "cifs",
  976. .init_fs_context = smb3_init_fs_context,
  977. .parameters = smb3_fs_parameters,
  978. .kill_sb = cifs_kill_sb,
  979. .fs_flags = FS_RENAME_DOES_D_MOVE,
  980. };
  981. MODULE_ALIAS_FS("cifs");
  982. struct file_system_type smb3_fs_type = {
  983. .owner = THIS_MODULE,
  984. .name = "smb3",
  985. .init_fs_context = smb3_init_fs_context,
  986. .parameters = smb3_fs_parameters,
  987. .kill_sb = cifs_kill_sb,
  988. .fs_flags = FS_RENAME_DOES_D_MOVE,
  989. };
  990. MODULE_ALIAS_FS("smb3");
  991. MODULE_ALIAS("smb3");
  992. const struct inode_operations cifs_dir_inode_ops = {
  993. .create = cifs_create,
  994. .atomic_open = cifs_atomic_open,
  995. .lookup = cifs_lookup,
  996. .getattr = cifs_getattr,
  997. .unlink = cifs_unlink,
  998. .link = cifs_hardlink,
  999. .mkdir = cifs_mkdir,
  1000. .rmdir = cifs_rmdir,
  1001. .rename = cifs_rename2,
  1002. .permission = cifs_permission,
  1003. .setattr = cifs_setattr,
  1004. .symlink = cifs_symlink,
  1005. .mknod = cifs_mknod,
  1006. .listxattr = cifs_listxattr,
  1007. };
  1008. const struct inode_operations cifs_file_inode_ops = {
  1009. .setattr = cifs_setattr,
  1010. .getattr = cifs_getattr,
  1011. .permission = cifs_permission,
  1012. .listxattr = cifs_listxattr,
  1013. .fiemap = cifs_fiemap,
  1014. };
  1015. const char *cifs_get_link(struct dentry *dentry, struct inode *inode,
  1016. struct delayed_call *done)
  1017. {
  1018. char *target_path;
  1019. target_path = kmalloc(PATH_MAX, GFP_KERNEL);
  1020. if (!target_path)
  1021. return ERR_PTR(-ENOMEM);
  1022. spin_lock(&inode->i_lock);
  1023. if (likely(CIFS_I(inode)->symlink_target)) {
  1024. strscpy(target_path, CIFS_I(inode)->symlink_target, PATH_MAX);
  1025. } else {
  1026. kfree(target_path);
  1027. target_path = ERR_PTR(-EOPNOTSUPP);
  1028. }
  1029. spin_unlock(&inode->i_lock);
  1030. if (!IS_ERR(target_path))
  1031. set_delayed_call(done, kfree_link, target_path);
  1032. return target_path;
  1033. }
  1034. const struct inode_operations cifs_symlink_inode_ops = {
  1035. .get_link = cifs_get_link,
  1036. .setattr = cifs_setattr,
  1037. .permission = cifs_permission,
  1038. .listxattr = cifs_listxattr,
  1039. };
  1040. /*
  1041. * Advance the EOF marker to after the source range.
  1042. */
  1043. static int cifs_precopy_set_eof(struct inode *src_inode, struct cifsInodeInfo *src_cifsi,
  1044. struct cifs_tcon *src_tcon,
  1045. unsigned int xid, loff_t src_end)
  1046. {
  1047. struct cifsFileInfo *writeable_srcfile;
  1048. int rc = -EINVAL;
  1049. writeable_srcfile = find_writable_file(src_cifsi, FIND_WR_FSUID_ONLY);
  1050. if (writeable_srcfile) {
  1051. if (src_tcon->ses->server->ops->set_file_size)
  1052. rc = src_tcon->ses->server->ops->set_file_size(
  1053. xid, src_tcon, writeable_srcfile,
  1054. src_inode->i_size, true /* no need to set sparse */);
  1055. else
  1056. rc = -ENOSYS;
  1057. cifsFileInfo_put(writeable_srcfile);
  1058. cifs_dbg(FYI, "SetFSize for copychunk rc = %d\n", rc);
  1059. }
  1060. if (rc < 0)
  1061. goto set_failed;
  1062. netfs_resize_file(&src_cifsi->netfs, src_end);
  1063. fscache_resize_cookie(cifs_inode_cookie(src_inode), src_end);
  1064. return 0;
  1065. set_failed:
  1066. return filemap_write_and_wait(src_inode->i_mapping);
  1067. }
  1068. /*
  1069. * Flush out either the folio that overlaps the beginning of a range in which
  1070. * pos resides or the folio that overlaps the end of a range unless that folio
  1071. * is entirely within the range we're going to invalidate. We extend the flush
  1072. * bounds to encompass the folio.
  1073. */
  1074. static int cifs_flush_folio(struct inode *inode, loff_t pos, loff_t *_fstart, loff_t *_fend,
  1075. bool first)
  1076. {
  1077. struct folio *folio;
  1078. unsigned long long fpos, fend;
  1079. pgoff_t index = pos / PAGE_SIZE;
  1080. size_t size;
  1081. int rc = 0;
  1082. folio = filemap_get_folio(inode->i_mapping, index);
  1083. if (IS_ERR(folio))
  1084. return 0;
  1085. size = folio_size(folio);
  1086. fpos = folio_pos(folio);
  1087. fend = fpos + size - 1;
  1088. *_fstart = min_t(unsigned long long, *_fstart, fpos);
  1089. *_fend = max_t(unsigned long long, *_fend, fend);
  1090. if ((first && pos == fpos) || (!first && pos == fend))
  1091. goto out;
  1092. rc = filemap_write_and_wait_range(inode->i_mapping, fpos, fend);
  1093. out:
  1094. folio_put(folio);
  1095. return rc;
  1096. }
  1097. static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,
  1098. struct file *dst_file, loff_t destoff, loff_t len,
  1099. unsigned int remap_flags)
  1100. {
  1101. struct inode *src_inode = file_inode(src_file);
  1102. struct inode *target_inode = file_inode(dst_file);
  1103. struct cifsInodeInfo *src_cifsi = CIFS_I(src_inode);
  1104. struct cifsInodeInfo *target_cifsi = CIFS_I(target_inode);
  1105. struct cifsFileInfo *smb_file_src = src_file->private_data;
  1106. struct cifsFileInfo *smb_file_target = dst_file->private_data;
  1107. struct cifs_tcon *target_tcon, *src_tcon;
  1108. unsigned long long destend, fstart, fend, new_size;
  1109. unsigned int xid;
  1110. int rc;
  1111. if (remap_flags & REMAP_FILE_DEDUP)
  1112. return -EOPNOTSUPP;
  1113. if (remap_flags & ~REMAP_FILE_ADVISORY)
  1114. return -EINVAL;
  1115. cifs_dbg(FYI, "clone range\n");
  1116. xid = get_xid();
  1117. if (!smb_file_src || !smb_file_target) {
  1118. rc = -EBADF;
  1119. cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
  1120. goto out;
  1121. }
  1122. src_tcon = tlink_tcon(smb_file_src->tlink);
  1123. target_tcon = tlink_tcon(smb_file_target->tlink);
  1124. /*
  1125. * Note: cifs case is easier than btrfs since server responsible for
  1126. * checks for proper open modes and file type and if it wants
  1127. * server could even support copy of range where source = target
  1128. */
  1129. lock_two_nondirectories(target_inode, src_inode);
  1130. if (len == 0)
  1131. len = src_inode->i_size - off;
  1132. cifs_dbg(FYI, "clone range\n");
  1133. /* Flush the source buffer */
  1134. rc = filemap_write_and_wait_range(src_inode->i_mapping, off,
  1135. off + len - 1);
  1136. if (rc)
  1137. goto unlock;
  1138. /* The server-side copy will fail if the source crosses the EOF marker.
  1139. * Advance the EOF marker after the flush above to the end of the range
  1140. * if it's short of that.
  1141. */
  1142. if (src_cifsi->netfs.remote_i_size < off + len) {
  1143. rc = cifs_precopy_set_eof(src_inode, src_cifsi, src_tcon, xid, off + len);
  1144. if (rc < 0)
  1145. goto unlock;
  1146. }
  1147. new_size = destoff + len;
  1148. destend = destoff + len - 1;
  1149. /* Flush the folios at either end of the destination range to prevent
  1150. * accidental loss of dirty data outside of the range.
  1151. */
  1152. fstart = destoff;
  1153. fend = destend;
  1154. rc = cifs_flush_folio(target_inode, destoff, &fstart, &fend, true);
  1155. if (rc)
  1156. goto unlock;
  1157. rc = cifs_flush_folio(target_inode, destend, &fstart, &fend, false);
  1158. if (rc)
  1159. goto unlock;
  1160. /* Discard all the folios that overlap the destination region. */
  1161. cifs_dbg(FYI, "about to discard pages %llx-%llx\n", fstart, fend);
  1162. truncate_inode_pages_range(&target_inode->i_data, fstart, fend);
  1163. fscache_invalidate(cifs_inode_cookie(target_inode), NULL,
  1164. i_size_read(target_inode), 0);
  1165. rc = -EOPNOTSUPP;
  1166. if (target_tcon->ses->server->ops->duplicate_extents) {
  1167. rc = target_tcon->ses->server->ops->duplicate_extents(xid,
  1168. smb_file_src, smb_file_target, off, len, destoff);
  1169. if (rc == 0 && new_size > i_size_read(target_inode)) {
  1170. truncate_setsize(target_inode, new_size);
  1171. netfs_resize_file(&target_cifsi->netfs, new_size);
  1172. fscache_resize_cookie(cifs_inode_cookie(target_inode),
  1173. new_size);
  1174. }
  1175. }
  1176. /* force revalidate of size and timestamps of target file now
  1177. that target is updated on the server */
  1178. CIFS_I(target_inode)->time = 0;
  1179. unlock:
  1180. /* although unlocking in the reverse order from locking is not
  1181. strictly necessary here it is a little cleaner to be consistent */
  1182. unlock_two_nondirectories(src_inode, target_inode);
  1183. out:
  1184. free_xid(xid);
  1185. return rc < 0 ? rc : len;
  1186. }
  1187. ssize_t cifs_file_copychunk_range(unsigned int xid,
  1188. struct file *src_file, loff_t off,
  1189. struct file *dst_file, loff_t destoff,
  1190. size_t len, unsigned int flags)
  1191. {
  1192. struct inode *src_inode = file_inode(src_file);
  1193. struct inode *target_inode = file_inode(dst_file);
  1194. struct cifsInodeInfo *src_cifsi = CIFS_I(src_inode);
  1195. struct cifsFileInfo *smb_file_src;
  1196. struct cifsFileInfo *smb_file_target;
  1197. struct cifs_tcon *src_tcon;
  1198. struct cifs_tcon *target_tcon;
  1199. unsigned long long destend, fstart, fend;
  1200. ssize_t rc;
  1201. cifs_dbg(FYI, "copychunk range\n");
  1202. if (!src_file->private_data || !dst_file->private_data) {
  1203. rc = -EBADF;
  1204. cifs_dbg(VFS, "missing cifsFileInfo on copy range src file\n");
  1205. goto out;
  1206. }
  1207. rc = -EXDEV;
  1208. smb_file_target = dst_file->private_data;
  1209. smb_file_src = src_file->private_data;
  1210. src_tcon = tlink_tcon(smb_file_src->tlink);
  1211. target_tcon = tlink_tcon(smb_file_target->tlink);
  1212. if (src_tcon->ses != target_tcon->ses) {
  1213. cifs_dbg(VFS, "source and target of copy not on same server\n");
  1214. goto out;
  1215. }
  1216. rc = -EOPNOTSUPP;
  1217. if (!target_tcon->ses->server->ops->copychunk_range)
  1218. goto out;
  1219. /*
  1220. * Note: cifs case is easier than btrfs since server responsible for
  1221. * checks for proper open modes and file type and if it wants
  1222. * server could even support copy of range where source = target
  1223. */
  1224. lock_two_nondirectories(target_inode, src_inode);
  1225. cifs_dbg(FYI, "about to flush pages\n");
  1226. rc = filemap_write_and_wait_range(src_inode->i_mapping, off,
  1227. off + len - 1);
  1228. if (rc)
  1229. goto unlock;
  1230. /* The server-side copy will fail if the source crosses the EOF marker.
  1231. * Advance the EOF marker after the flush above to the end of the range
  1232. * if it's short of that.
  1233. */
  1234. if (src_cifsi->server_eof < off + len) {
  1235. rc = cifs_precopy_set_eof(src_inode, src_cifsi, src_tcon, xid, off + len);
  1236. if (rc < 0)
  1237. goto unlock;
  1238. }
  1239. destend = destoff + len - 1;
  1240. /* Flush the folios at either end of the destination range to prevent
  1241. * accidental loss of dirty data outside of the range.
  1242. */
  1243. fstart = destoff;
  1244. fend = destend;
  1245. rc = cifs_flush_folio(target_inode, destoff, &fstart, &fend, true);
  1246. if (rc)
  1247. goto unlock;
  1248. rc = cifs_flush_folio(target_inode, destend, &fstart, &fend, false);
  1249. if (rc)
  1250. goto unlock;
  1251. /* Discard all the folios that overlap the destination region. */
  1252. truncate_inode_pages_range(&target_inode->i_data, fstart, fend);
  1253. rc = file_modified(dst_file);
  1254. if (!rc) {
  1255. rc = target_tcon->ses->server->ops->copychunk_range(xid,
  1256. smb_file_src, smb_file_target, off, len, destoff);
  1257. if (rc > 0 && destoff + rc > i_size_read(target_inode))
  1258. truncate_setsize(target_inode, destoff + rc);
  1259. }
  1260. file_accessed(src_file);
  1261. /* force revalidate of size and timestamps of target file now
  1262. * that target is updated on the server
  1263. */
  1264. CIFS_I(target_inode)->time = 0;
  1265. unlock:
  1266. /* although unlocking in the reverse order from locking is not
  1267. * strictly necessary here it is a little cleaner to be consistent
  1268. */
  1269. unlock_two_nondirectories(src_inode, target_inode);
  1270. out:
  1271. return rc;
  1272. }
  1273. /*
  1274. * Directory operations under CIFS/SMB2/SMB3 are synchronous, so fsync()
  1275. * is a dummy operation.
  1276. */
  1277. static int cifs_dir_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  1278. {
  1279. cifs_dbg(FYI, "Sync directory - name: %pD datasync: 0x%x\n",
  1280. file, datasync);
  1281. return 0;
  1282. }
  1283. static ssize_t cifs_copy_file_range(struct file *src_file, loff_t off,
  1284. struct file *dst_file, loff_t destoff,
  1285. size_t len, unsigned int flags)
  1286. {
  1287. unsigned int xid = get_xid();
  1288. ssize_t rc;
  1289. struct cifsFileInfo *cfile = dst_file->private_data;
  1290. if (cfile->swapfile) {
  1291. rc = -EOPNOTSUPP;
  1292. free_xid(xid);
  1293. return rc;
  1294. }
  1295. rc = cifs_file_copychunk_range(xid, src_file, off, dst_file, destoff,
  1296. len, flags);
  1297. free_xid(xid);
  1298. if (rc == -EOPNOTSUPP || rc == -EXDEV)
  1299. rc = generic_copy_file_range(src_file, off, dst_file,
  1300. destoff, len, flags);
  1301. return rc;
  1302. }
  1303. const struct file_operations cifs_file_ops = {
  1304. .read_iter = cifs_loose_read_iter,
  1305. .write_iter = cifs_file_write_iter,
  1306. .open = cifs_open,
  1307. .release = cifs_close,
  1308. .lock = cifs_lock,
  1309. .flock = cifs_flock,
  1310. .fsync = cifs_fsync,
  1311. .flush = cifs_flush,
  1312. .mmap = cifs_file_mmap,
  1313. .splice_read = generic_file_splice_read,
  1314. .splice_write = iter_file_splice_write,
  1315. .llseek = cifs_llseek,
  1316. .unlocked_ioctl = cifs_ioctl,
  1317. .copy_file_range = cifs_copy_file_range,
  1318. .remap_file_range = cifs_remap_file_range,
  1319. .setlease = cifs_setlease,
  1320. .fallocate = cifs_fallocate,
  1321. };
  1322. const struct file_operations cifs_file_strict_ops = {
  1323. .read_iter = cifs_strict_readv,
  1324. .write_iter = cifs_strict_writev,
  1325. .open = cifs_open,
  1326. .release = cifs_close,
  1327. .lock = cifs_lock,
  1328. .flock = cifs_flock,
  1329. .fsync = cifs_strict_fsync,
  1330. .flush = cifs_flush,
  1331. .mmap = cifs_file_strict_mmap,
  1332. .splice_read = generic_file_splice_read,
  1333. .splice_write = iter_file_splice_write,
  1334. .llseek = cifs_llseek,
  1335. .unlocked_ioctl = cifs_ioctl,
  1336. .copy_file_range = cifs_copy_file_range,
  1337. .remap_file_range = cifs_remap_file_range,
  1338. .setlease = cifs_setlease,
  1339. .fallocate = cifs_fallocate,
  1340. };
  1341. const struct file_operations cifs_file_direct_ops = {
  1342. .read_iter = cifs_direct_readv,
  1343. .write_iter = cifs_direct_writev,
  1344. .open = cifs_open,
  1345. .release = cifs_close,
  1346. .lock = cifs_lock,
  1347. .flock = cifs_flock,
  1348. .fsync = cifs_fsync,
  1349. .flush = cifs_flush,
  1350. .mmap = cifs_file_mmap,
  1351. .splice_read = generic_file_splice_read,
  1352. .splice_write = iter_file_splice_write,
  1353. .unlocked_ioctl = cifs_ioctl,
  1354. .copy_file_range = cifs_copy_file_range,
  1355. .remap_file_range = cifs_remap_file_range,
  1356. .llseek = cifs_llseek,
  1357. .setlease = cifs_setlease,
  1358. .fallocate = cifs_fallocate,
  1359. };
  1360. const struct file_operations cifs_file_nobrl_ops = {
  1361. .read_iter = cifs_loose_read_iter,
  1362. .write_iter = cifs_file_write_iter,
  1363. .open = cifs_open,
  1364. .release = cifs_close,
  1365. .fsync = cifs_fsync,
  1366. .flush = cifs_flush,
  1367. .mmap = cifs_file_mmap,
  1368. .splice_read = generic_file_splice_read,
  1369. .splice_write = iter_file_splice_write,
  1370. .llseek = cifs_llseek,
  1371. .unlocked_ioctl = cifs_ioctl,
  1372. .copy_file_range = cifs_copy_file_range,
  1373. .remap_file_range = cifs_remap_file_range,
  1374. .setlease = cifs_setlease,
  1375. .fallocate = cifs_fallocate,
  1376. };
  1377. const struct file_operations cifs_file_strict_nobrl_ops = {
  1378. .read_iter = cifs_strict_readv,
  1379. .write_iter = cifs_strict_writev,
  1380. .open = cifs_open,
  1381. .release = cifs_close,
  1382. .fsync = cifs_strict_fsync,
  1383. .flush = cifs_flush,
  1384. .mmap = cifs_file_strict_mmap,
  1385. .splice_read = generic_file_splice_read,
  1386. .splice_write = iter_file_splice_write,
  1387. .llseek = cifs_llseek,
  1388. .unlocked_ioctl = cifs_ioctl,
  1389. .copy_file_range = cifs_copy_file_range,
  1390. .remap_file_range = cifs_remap_file_range,
  1391. .setlease = cifs_setlease,
  1392. .fallocate = cifs_fallocate,
  1393. };
  1394. const struct file_operations cifs_file_direct_nobrl_ops = {
  1395. .read_iter = cifs_direct_readv,
  1396. .write_iter = cifs_direct_writev,
  1397. .open = cifs_open,
  1398. .release = cifs_close,
  1399. .fsync = cifs_fsync,
  1400. .flush = cifs_flush,
  1401. .mmap = cifs_file_mmap,
  1402. .splice_read = generic_file_splice_read,
  1403. .splice_write = iter_file_splice_write,
  1404. .unlocked_ioctl = cifs_ioctl,
  1405. .copy_file_range = cifs_copy_file_range,
  1406. .remap_file_range = cifs_remap_file_range,
  1407. .llseek = cifs_llseek,
  1408. .setlease = cifs_setlease,
  1409. .fallocate = cifs_fallocate,
  1410. };
  1411. const struct file_operations cifs_dir_ops = {
  1412. .iterate_shared = cifs_readdir,
  1413. .release = cifs_closedir,
  1414. .read = generic_read_dir,
  1415. .unlocked_ioctl = cifs_ioctl,
  1416. .copy_file_range = cifs_copy_file_range,
  1417. .remap_file_range = cifs_remap_file_range,
  1418. .llseek = generic_file_llseek,
  1419. .fsync = cifs_dir_fsync,
  1420. };
  1421. static void
  1422. cifs_init_once(void *inode)
  1423. {
  1424. struct cifsInodeInfo *cifsi = inode;
  1425. inode_init_once(&cifsi->netfs.inode);
  1426. init_rwsem(&cifsi->lock_sem);
  1427. }
  1428. static int __init
  1429. cifs_init_inodecache(void)
  1430. {
  1431. cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
  1432. sizeof(struct cifsInodeInfo),
  1433. 0, (SLAB_RECLAIM_ACCOUNT|
  1434. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  1435. cifs_init_once);
  1436. if (cifs_inode_cachep == NULL)
  1437. return -ENOMEM;
  1438. return 0;
  1439. }
  1440. static void
  1441. cifs_destroy_inodecache(void)
  1442. {
  1443. /*
  1444. * Make sure all delayed rcu free inodes are flushed before we
  1445. * destroy cache.
  1446. */
  1447. rcu_barrier();
  1448. kmem_cache_destroy(cifs_inode_cachep);
  1449. }
  1450. static int
  1451. cifs_init_request_bufs(void)
  1452. {
  1453. /*
  1454. * SMB2 maximum header size is bigger than CIFS one - no problems to
  1455. * allocate some more bytes for CIFS.
  1456. */
  1457. size_t max_hdr_size = MAX_SMB2_HDR_SIZE;
  1458. if (CIFSMaxBufSize < 8192) {
  1459. /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
  1460. Unicode path name has to fit in any SMB/CIFS path based frames */
  1461. CIFSMaxBufSize = 8192;
  1462. } else if (CIFSMaxBufSize > 1024*127) {
  1463. CIFSMaxBufSize = 1024 * 127;
  1464. } else {
  1465. CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
  1466. }
  1467. /*
  1468. cifs_dbg(VFS, "CIFSMaxBufSize %d 0x%x\n",
  1469. CIFSMaxBufSize, CIFSMaxBufSize);
  1470. */
  1471. cifs_req_cachep = kmem_cache_create_usercopy("cifs_request",
  1472. CIFSMaxBufSize + max_hdr_size, 0,
  1473. SLAB_HWCACHE_ALIGN, 0,
  1474. CIFSMaxBufSize + max_hdr_size,
  1475. NULL);
  1476. if (cifs_req_cachep == NULL)
  1477. return -ENOMEM;
  1478. if (cifs_min_rcv < 1)
  1479. cifs_min_rcv = 1;
  1480. else if (cifs_min_rcv > 64) {
  1481. cifs_min_rcv = 64;
  1482. cifs_dbg(VFS, "cifs_min_rcv set to maximum (64)\n");
  1483. }
  1484. cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
  1485. cifs_req_cachep);
  1486. if (cifs_req_poolp == NULL) {
  1487. kmem_cache_destroy(cifs_req_cachep);
  1488. return -ENOMEM;
  1489. }
  1490. /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
  1491. almost all handle based requests (but not write response, nor is it
  1492. sufficient for path based requests). A smaller size would have
  1493. been more efficient (compacting multiple slab items on one 4k page)
  1494. for the case in which debug was on, but this larger size allows
  1495. more SMBs to use small buffer alloc and is still much more
  1496. efficient to alloc 1 per page off the slab compared to 17K (5page)
  1497. alloc of large cifs buffers even when page debugging is on */
  1498. cifs_sm_req_cachep = kmem_cache_create_usercopy("cifs_small_rq",
  1499. MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
  1500. 0, MAX_CIFS_SMALL_BUFFER_SIZE, NULL);
  1501. if (cifs_sm_req_cachep == NULL) {
  1502. mempool_destroy(cifs_req_poolp);
  1503. kmem_cache_destroy(cifs_req_cachep);
  1504. return -ENOMEM;
  1505. }
  1506. if (cifs_min_small < 2)
  1507. cifs_min_small = 2;
  1508. else if (cifs_min_small > 256) {
  1509. cifs_min_small = 256;
  1510. cifs_dbg(FYI, "cifs_min_small set to maximum (256)\n");
  1511. }
  1512. cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
  1513. cifs_sm_req_cachep);
  1514. if (cifs_sm_req_poolp == NULL) {
  1515. mempool_destroy(cifs_req_poolp);
  1516. kmem_cache_destroy(cifs_req_cachep);
  1517. kmem_cache_destroy(cifs_sm_req_cachep);
  1518. return -ENOMEM;
  1519. }
  1520. return 0;
  1521. }
  1522. static void
  1523. cifs_destroy_request_bufs(void)
  1524. {
  1525. mempool_destroy(cifs_req_poolp);
  1526. kmem_cache_destroy(cifs_req_cachep);
  1527. mempool_destroy(cifs_sm_req_poolp);
  1528. kmem_cache_destroy(cifs_sm_req_cachep);
  1529. }
  1530. static int init_mids(void)
  1531. {
  1532. cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
  1533. sizeof(struct mid_q_entry), 0,
  1534. SLAB_HWCACHE_ALIGN, NULL);
  1535. if (cifs_mid_cachep == NULL)
  1536. return -ENOMEM;
  1537. /* 3 is a reasonable minimum number of simultaneous operations */
  1538. cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
  1539. if (cifs_mid_poolp == NULL) {
  1540. kmem_cache_destroy(cifs_mid_cachep);
  1541. return -ENOMEM;
  1542. }
  1543. return 0;
  1544. }
  1545. static void destroy_mids(void)
  1546. {
  1547. mempool_destroy(cifs_mid_poolp);
  1548. kmem_cache_destroy(cifs_mid_cachep);
  1549. }
  1550. static int __init
  1551. init_cifs(void)
  1552. {
  1553. int rc = 0;
  1554. cifs_proc_init();
  1555. INIT_LIST_HEAD(&cifs_tcp_ses_list);
  1556. /*
  1557. * Initialize Global counters
  1558. */
  1559. atomic_set(&sesInfoAllocCount, 0);
  1560. atomic_set(&tconInfoAllocCount, 0);
  1561. atomic_set(&tcpSesNextId, 0);
  1562. atomic_set(&tcpSesAllocCount, 0);
  1563. atomic_set(&tcpSesReconnectCount, 0);
  1564. atomic_set(&tconInfoReconnectCount, 0);
  1565. atomic_set(&buf_alloc_count, 0);
  1566. atomic_set(&small_buf_alloc_count, 0);
  1567. #ifdef CONFIG_CIFS_STATS2
  1568. atomic_set(&total_buf_alloc_count, 0);
  1569. atomic_set(&total_small_buf_alloc_count, 0);
  1570. if (slow_rsp_threshold < 1)
  1571. cifs_dbg(FYI, "slow_response_threshold msgs disabled\n");
  1572. else if (slow_rsp_threshold > 32767)
  1573. cifs_dbg(VFS,
  1574. "slow response threshold set higher than recommended (0 to 32767)\n");
  1575. #endif /* CONFIG_CIFS_STATS2 */
  1576. atomic_set(&mid_count, 0);
  1577. GlobalCurrentXid = 0;
  1578. GlobalTotalActiveXid = 0;
  1579. GlobalMaxActiveXid = 0;
  1580. spin_lock_init(&cifs_tcp_ses_lock);
  1581. spin_lock_init(&GlobalMid_Lock);
  1582. cifs_lock_secret = get_random_u32();
  1583. if (cifs_max_pending < 2) {
  1584. cifs_max_pending = 2;
  1585. cifs_dbg(FYI, "cifs_max_pending set to min of 2\n");
  1586. } else if (cifs_max_pending > CIFS_MAX_REQ) {
  1587. cifs_max_pending = CIFS_MAX_REQ;
  1588. cifs_dbg(FYI, "cifs_max_pending set to max of %u\n",
  1589. CIFS_MAX_REQ);
  1590. }
  1591. cifsiod_wq = alloc_workqueue("cifsiod", WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
  1592. if (!cifsiod_wq) {
  1593. rc = -ENOMEM;
  1594. goto out_clean_proc;
  1595. }
  1596. /*
  1597. * Consider in future setting limit!=0 maybe to min(num_of_cores - 1, 3)
  1598. * so that we don't launch too many worker threads but
  1599. * Documentation/core-api/workqueue.rst recommends setting it to 0
  1600. */
  1601. /* WQ_UNBOUND allows decrypt tasks to run on any CPU */
  1602. decrypt_wq = alloc_workqueue("smb3decryptd",
  1603. WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
  1604. if (!decrypt_wq) {
  1605. rc = -ENOMEM;
  1606. goto out_destroy_cifsiod_wq;
  1607. }
  1608. fileinfo_put_wq = alloc_workqueue("cifsfileinfoput",
  1609. WQ_UNBOUND|WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
  1610. if (!fileinfo_put_wq) {
  1611. rc = -ENOMEM;
  1612. goto out_destroy_decrypt_wq;
  1613. }
  1614. cifsoplockd_wq = alloc_workqueue("cifsoplockd",
  1615. WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
  1616. if (!cifsoplockd_wq) {
  1617. rc = -ENOMEM;
  1618. goto out_destroy_fileinfo_put_wq;
  1619. }
  1620. deferredclose_wq = alloc_workqueue("deferredclose",
  1621. WQ_FREEZABLE|WQ_MEM_RECLAIM, 0);
  1622. if (!deferredclose_wq) {
  1623. rc = -ENOMEM;
  1624. goto out_destroy_cifsoplockd_wq;
  1625. }
  1626. rc = cifs_init_inodecache();
  1627. if (rc)
  1628. goto out_destroy_deferredclose_wq;
  1629. rc = init_mids();
  1630. if (rc)
  1631. goto out_destroy_inodecache;
  1632. rc = cifs_init_request_bufs();
  1633. if (rc)
  1634. goto out_destroy_mids;
  1635. #ifdef CONFIG_CIFS_DFS_UPCALL
  1636. rc = dfs_cache_init();
  1637. if (rc)
  1638. goto out_destroy_request_bufs;
  1639. #endif /* CONFIG_CIFS_DFS_UPCALL */
  1640. #ifdef CONFIG_CIFS_UPCALL
  1641. rc = init_cifs_spnego();
  1642. if (rc)
  1643. goto out_destroy_dfs_cache;
  1644. #endif /* CONFIG_CIFS_UPCALL */
  1645. #ifdef CONFIG_CIFS_SWN_UPCALL
  1646. rc = cifs_genl_init();
  1647. if (rc)
  1648. goto out_register_key_type;
  1649. #endif /* CONFIG_CIFS_SWN_UPCALL */
  1650. rc = init_cifs_idmap();
  1651. if (rc)
  1652. goto out_cifs_swn_init;
  1653. rc = register_filesystem(&cifs_fs_type);
  1654. if (rc)
  1655. goto out_init_cifs_idmap;
  1656. rc = register_filesystem(&smb3_fs_type);
  1657. if (rc) {
  1658. unregister_filesystem(&cifs_fs_type);
  1659. goto out_init_cifs_idmap;
  1660. }
  1661. return 0;
  1662. out_init_cifs_idmap:
  1663. exit_cifs_idmap();
  1664. out_cifs_swn_init:
  1665. #ifdef CONFIG_CIFS_SWN_UPCALL
  1666. cifs_genl_exit();
  1667. out_register_key_type:
  1668. #endif
  1669. #ifdef CONFIG_CIFS_UPCALL
  1670. exit_cifs_spnego();
  1671. out_destroy_dfs_cache:
  1672. #endif
  1673. #ifdef CONFIG_CIFS_DFS_UPCALL
  1674. dfs_cache_destroy();
  1675. out_destroy_request_bufs:
  1676. #endif
  1677. cifs_destroy_request_bufs();
  1678. out_destroy_mids:
  1679. destroy_mids();
  1680. out_destroy_inodecache:
  1681. cifs_destroy_inodecache();
  1682. out_destroy_deferredclose_wq:
  1683. destroy_workqueue(deferredclose_wq);
  1684. out_destroy_cifsoplockd_wq:
  1685. destroy_workqueue(cifsoplockd_wq);
  1686. out_destroy_fileinfo_put_wq:
  1687. destroy_workqueue(fileinfo_put_wq);
  1688. out_destroy_decrypt_wq:
  1689. destroy_workqueue(decrypt_wq);
  1690. out_destroy_cifsiod_wq:
  1691. destroy_workqueue(cifsiod_wq);
  1692. out_clean_proc:
  1693. cifs_proc_clean();
  1694. return rc;
  1695. }
  1696. static void __exit
  1697. exit_cifs(void)
  1698. {
  1699. cifs_dbg(NOISY, "exit_smb3\n");
  1700. unregister_filesystem(&cifs_fs_type);
  1701. unregister_filesystem(&smb3_fs_type);
  1702. cifs_dfs_release_automount_timer();
  1703. exit_cifs_idmap();
  1704. #ifdef CONFIG_CIFS_SWN_UPCALL
  1705. cifs_genl_exit();
  1706. #endif
  1707. #ifdef CONFIG_CIFS_UPCALL
  1708. exit_cifs_spnego();
  1709. #endif
  1710. #ifdef CONFIG_CIFS_DFS_UPCALL
  1711. dfs_cache_destroy();
  1712. #endif
  1713. cifs_destroy_request_bufs();
  1714. destroy_mids();
  1715. cifs_destroy_inodecache();
  1716. destroy_workqueue(deferredclose_wq);
  1717. destroy_workqueue(cifsoplockd_wq);
  1718. destroy_workqueue(decrypt_wq);
  1719. destroy_workqueue(fileinfo_put_wq);
  1720. destroy_workqueue(cifsiod_wq);
  1721. cifs_proc_clean();
  1722. }
  1723. MODULE_AUTHOR("Steve French");
  1724. MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
  1725. MODULE_DESCRIPTION
  1726. ("VFS to access SMB3 servers e.g. Samba, Macs, Azure and Windows (and "
  1727. "also older servers complying with the SNIA CIFS Specification)");
  1728. MODULE_VERSION(CIFS_VERSION);
  1729. MODULE_SOFTDEP("ecb");
  1730. MODULE_SOFTDEP("hmac");
  1731. MODULE_SOFTDEP("md5");
  1732. MODULE_SOFTDEP("nls");
  1733. MODULE_SOFTDEP("aes");
  1734. MODULE_SOFTDEP("cmac");
  1735. MODULE_SOFTDEP("sha256");
  1736. MODULE_SOFTDEP("sha512");
  1737. MODULE_SOFTDEP("aead2");
  1738. MODULE_SOFTDEP("ccm");
  1739. MODULE_SOFTDEP("gcm");
  1740. module_init(init_cifs)
  1741. module_exit(exit_cifs)