smb2ops.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  1. /*
  2. * SMB2 version specific operations
  3. *
  4. * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
  5. *
  6. * This library is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License v2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/pagemap.h>
  20. #include <linux/vfs.h>
  21. #include <linux/falloc.h>
  22. #include "cifsglob.h"
  23. #include "smb2pdu.h"
  24. #include "smb2proto.h"
  25. #include "cifsproto.h"
  26. #include "cifs_debug.h"
  27. #include "cifs_unicode.h"
  28. #include "smb2status.h"
  29. #include "smb2glob.h"
  30. static int
  31. change_conf(struct TCP_Server_Info *server)
  32. {
  33. server->credits += server->echo_credits + server->oplock_credits;
  34. server->oplock_credits = server->echo_credits = 0;
  35. switch (server->credits) {
  36. case 0:
  37. return -1;
  38. case 1:
  39. server->echoes = false;
  40. server->oplocks = false;
  41. cifs_dbg(VFS, "disabling echoes and oplocks\n");
  42. break;
  43. case 2:
  44. server->echoes = true;
  45. server->oplocks = false;
  46. server->echo_credits = 1;
  47. cifs_dbg(FYI, "disabling oplocks\n");
  48. break;
  49. default:
  50. server->echoes = true;
  51. if (enable_oplocks) {
  52. server->oplocks = true;
  53. server->oplock_credits = 1;
  54. } else
  55. server->oplocks = false;
  56. server->echo_credits = 1;
  57. }
  58. server->credits -= server->echo_credits + server->oplock_credits;
  59. return 0;
  60. }
  61. static void
  62. smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
  63. const int optype)
  64. {
  65. int *val, rc = 0;
  66. spin_lock(&server->req_lock);
  67. val = server->ops->get_credits_field(server, optype);
  68. *val += add;
  69. server->in_flight--;
  70. if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
  71. rc = change_conf(server);
  72. /*
  73. * Sometimes server returns 0 credits on oplock break ack - we need to
  74. * rebalance credits in this case.
  75. */
  76. else if (server->in_flight > 0 && server->oplock_credits == 0 &&
  77. server->oplocks) {
  78. if (server->credits > 1) {
  79. server->credits--;
  80. server->oplock_credits++;
  81. }
  82. }
  83. spin_unlock(&server->req_lock);
  84. wake_up(&server->request_q);
  85. if (rc)
  86. cifs_reconnect(server);
  87. }
  88. static void
  89. smb2_set_credits(struct TCP_Server_Info *server, const int val)
  90. {
  91. spin_lock(&server->req_lock);
  92. server->credits = val;
  93. spin_unlock(&server->req_lock);
  94. }
  95. static int *
  96. smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
  97. {
  98. switch (optype) {
  99. case CIFS_ECHO_OP:
  100. return &server->echo_credits;
  101. case CIFS_OBREAK_OP:
  102. return &server->oplock_credits;
  103. default:
  104. return &server->credits;
  105. }
  106. }
  107. static unsigned int
  108. smb2_get_credits(struct mid_q_entry *mid)
  109. {
  110. return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
  111. }
  112. static int
  113. smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
  114. unsigned int *num, unsigned int *credits)
  115. {
  116. int rc = 0;
  117. unsigned int scredits;
  118. spin_lock(&server->req_lock);
  119. while (1) {
  120. if (server->credits <= 0) {
  121. spin_unlock(&server->req_lock);
  122. cifs_num_waiters_inc(server);
  123. rc = wait_event_killable(server->request_q,
  124. has_credits(server, &server->credits));
  125. cifs_num_waiters_dec(server);
  126. if (rc)
  127. return rc;
  128. spin_lock(&server->req_lock);
  129. } else {
  130. if (server->tcpStatus == CifsExiting) {
  131. spin_unlock(&server->req_lock);
  132. return -ENOENT;
  133. }
  134. scredits = server->credits;
  135. /* can deadlock with reopen */
  136. if (scredits <= 8) {
  137. *num = SMB2_MAX_BUFFER_SIZE;
  138. *credits = 0;
  139. break;
  140. }
  141. /* leave some credits for reopen and other ops */
  142. scredits -= 8;
  143. *num = min_t(unsigned int, size,
  144. scredits * SMB2_MAX_BUFFER_SIZE);
  145. *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
  146. server->credits -= *credits;
  147. server->in_flight++;
  148. break;
  149. }
  150. }
  151. spin_unlock(&server->req_lock);
  152. return rc;
  153. }
  154. static __u64
  155. smb2_get_next_mid(struct TCP_Server_Info *server)
  156. {
  157. __u64 mid;
  158. /* for SMB2 we need the current value */
  159. spin_lock(&GlobalMid_Lock);
  160. mid = server->CurrentMid++;
  161. spin_unlock(&GlobalMid_Lock);
  162. return mid;
  163. }
  164. static struct mid_q_entry *
  165. smb2_find_mid(struct TCP_Server_Info *server, char *buf)
  166. {
  167. struct mid_q_entry *mid;
  168. struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
  169. __u64 wire_mid = le64_to_cpu(hdr->MessageId);
  170. spin_lock(&GlobalMid_Lock);
  171. list_for_each_entry(mid, &server->pending_mid_q, qhead) {
  172. if ((mid->mid == wire_mid) &&
  173. (mid->mid_state == MID_REQUEST_SUBMITTED) &&
  174. (mid->command == hdr->Command)) {
  175. spin_unlock(&GlobalMid_Lock);
  176. return mid;
  177. }
  178. }
  179. spin_unlock(&GlobalMid_Lock);
  180. return NULL;
  181. }
  182. static void
  183. smb2_dump_detail(void *buf)
  184. {
  185. #ifdef CONFIG_CIFS_DEBUG2
  186. struct smb2_hdr *smb = (struct smb2_hdr *)buf;
  187. cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
  188. smb->Command, smb->Status, smb->Flags, smb->MessageId,
  189. smb->ProcessId);
  190. cifs_dbg(VFS, "smb buf %p len %u\n", smb, smb2_calc_size(smb));
  191. #endif
  192. }
  193. static bool
  194. smb2_need_neg(struct TCP_Server_Info *server)
  195. {
  196. return server->max_read == 0;
  197. }
  198. static int
  199. smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
  200. {
  201. int rc;
  202. ses->server->CurrentMid = 0;
  203. rc = SMB2_negotiate(xid, ses);
  204. /* BB we probably don't need to retry with modern servers */
  205. if (rc == -EAGAIN)
  206. rc = -EHOSTDOWN;
  207. return rc;
  208. }
  209. static unsigned int
  210. smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  211. {
  212. struct TCP_Server_Info *server = tcon->ses->server;
  213. unsigned int wsize;
  214. /* start with specified wsize, or default */
  215. wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
  216. wsize = min_t(unsigned int, wsize, server->max_write);
  217. if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
  218. wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
  219. return wsize;
  220. }
  221. static unsigned int
  222. smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  223. {
  224. struct TCP_Server_Info *server = tcon->ses->server;
  225. unsigned int rsize;
  226. /* start with specified rsize, or default */
  227. rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
  228. rsize = min_t(unsigned int, rsize, server->max_read);
  229. if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
  230. rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
  231. return rsize;
  232. }
  233. #ifdef CONFIG_CIFS_STATS2
  234. static int
  235. SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
  236. {
  237. int rc;
  238. unsigned int ret_data_len = 0;
  239. struct network_interface_info_ioctl_rsp *out_buf;
  240. rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
  241. FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
  242. NULL /* no data input */, 0 /* no data input */,
  243. (char **)&out_buf, &ret_data_len);
  244. if (rc != 0)
  245. cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
  246. else if (ret_data_len < sizeof(struct network_interface_info_ioctl_rsp)) {
  247. cifs_dbg(VFS, "server returned bad net interface info buf\n");
  248. rc = -EINVAL;
  249. } else {
  250. /* Dump info on first interface */
  251. cifs_dbg(FYI, "Adapter Capability 0x%x\t",
  252. le32_to_cpu(out_buf->Capability));
  253. cifs_dbg(FYI, "Link Speed %lld\n",
  254. le64_to_cpu(out_buf->LinkSpeed));
  255. }
  256. kfree(out_buf);
  257. return rc;
  258. }
  259. #endif /* STATS2 */
  260. static void
  261. smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
  262. {
  263. int rc;
  264. __le16 srch_path = 0; /* Null - open root of share */
  265. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  266. struct cifs_open_parms oparms;
  267. struct cifs_fid fid;
  268. oparms.tcon = tcon;
  269. oparms.desired_access = FILE_READ_ATTRIBUTES;
  270. oparms.disposition = FILE_OPEN;
  271. oparms.create_options = 0;
  272. oparms.fid = &fid;
  273. oparms.reconnect = false;
  274. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
  275. if (rc)
  276. return;
  277. #ifdef CONFIG_CIFS_STATS2
  278. SMB3_request_interfaces(xid, tcon);
  279. #endif /* STATS2 */
  280. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  281. FS_ATTRIBUTE_INFORMATION);
  282. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  283. FS_DEVICE_INFORMATION);
  284. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  285. FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
  286. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  287. return;
  288. }
  289. static void
  290. smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
  291. {
  292. int rc;
  293. __le16 srch_path = 0; /* Null - open root of share */
  294. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  295. struct cifs_open_parms oparms;
  296. struct cifs_fid fid;
  297. oparms.tcon = tcon;
  298. oparms.desired_access = FILE_READ_ATTRIBUTES;
  299. oparms.disposition = FILE_OPEN;
  300. oparms.create_options = 0;
  301. oparms.fid = &fid;
  302. oparms.reconnect = false;
  303. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
  304. if (rc)
  305. return;
  306. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  307. FS_ATTRIBUTE_INFORMATION);
  308. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  309. FS_DEVICE_INFORMATION);
  310. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  311. return;
  312. }
  313. static int
  314. smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
  315. struct cifs_sb_info *cifs_sb, const char *full_path)
  316. {
  317. int rc;
  318. __le16 *utf16_path;
  319. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  320. struct cifs_open_parms oparms;
  321. struct cifs_fid fid;
  322. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  323. if (!utf16_path)
  324. return -ENOMEM;
  325. oparms.tcon = tcon;
  326. oparms.desired_access = FILE_READ_ATTRIBUTES;
  327. oparms.disposition = FILE_OPEN;
  328. oparms.create_options = 0;
  329. oparms.fid = &fid;
  330. oparms.reconnect = false;
  331. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
  332. if (rc) {
  333. kfree(utf16_path);
  334. return rc;
  335. }
  336. rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  337. kfree(utf16_path);
  338. return rc;
  339. }
  340. static int
  341. smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
  342. struct cifs_sb_info *cifs_sb, const char *full_path,
  343. u64 *uniqueid, FILE_ALL_INFO *data)
  344. {
  345. *uniqueid = le64_to_cpu(data->IndexNumber);
  346. return 0;
  347. }
  348. static int
  349. smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
  350. struct cifs_fid *fid, FILE_ALL_INFO *data)
  351. {
  352. int rc;
  353. struct smb2_file_all_info *smb2_data;
  354. smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
  355. GFP_KERNEL);
  356. if (smb2_data == NULL)
  357. return -ENOMEM;
  358. rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
  359. smb2_data);
  360. if (!rc)
  361. move_smb2_info_to_cifs(data, smb2_data);
  362. kfree(smb2_data);
  363. return rc;
  364. }
  365. static bool
  366. smb2_can_echo(struct TCP_Server_Info *server)
  367. {
  368. return server->echoes;
  369. }
  370. static void
  371. smb2_clear_stats(struct cifs_tcon *tcon)
  372. {
  373. #ifdef CONFIG_CIFS_STATS
  374. int i;
  375. for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
  376. atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
  377. atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
  378. }
  379. #endif
  380. }
  381. static void
  382. smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
  383. {
  384. seq_puts(m, "\n\tShare Capabilities:");
  385. if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
  386. seq_puts(m, " DFS,");
  387. if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
  388. seq_puts(m, " CONTINUOUS AVAILABILITY,");
  389. if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
  390. seq_puts(m, " SCALEOUT,");
  391. if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
  392. seq_puts(m, " CLUSTER,");
  393. if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
  394. seq_puts(m, " ASYMMETRIC,");
  395. if (tcon->capabilities == 0)
  396. seq_puts(m, " None");
  397. if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
  398. seq_puts(m, " Aligned,");
  399. if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
  400. seq_puts(m, " Partition Aligned,");
  401. if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
  402. seq_puts(m, " SSD,");
  403. if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
  404. seq_puts(m, " TRIM-support,");
  405. seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
  406. if (tcon->perf_sector_size)
  407. seq_printf(m, "\tOptimal sector size: 0x%x",
  408. tcon->perf_sector_size);
  409. }
  410. static void
  411. smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
  412. {
  413. #ifdef CONFIG_CIFS_STATS
  414. atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
  415. atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
  416. seq_printf(m, "\nNegotiates: %d sent %d failed",
  417. atomic_read(&sent[SMB2_NEGOTIATE_HE]),
  418. atomic_read(&failed[SMB2_NEGOTIATE_HE]));
  419. seq_printf(m, "\nSessionSetups: %d sent %d failed",
  420. atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
  421. atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
  422. seq_printf(m, "\nLogoffs: %d sent %d failed",
  423. atomic_read(&sent[SMB2_LOGOFF_HE]),
  424. atomic_read(&failed[SMB2_LOGOFF_HE]));
  425. seq_printf(m, "\nTreeConnects: %d sent %d failed",
  426. atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
  427. atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
  428. seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
  429. atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
  430. atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
  431. seq_printf(m, "\nCreates: %d sent %d failed",
  432. atomic_read(&sent[SMB2_CREATE_HE]),
  433. atomic_read(&failed[SMB2_CREATE_HE]));
  434. seq_printf(m, "\nCloses: %d sent %d failed",
  435. atomic_read(&sent[SMB2_CLOSE_HE]),
  436. atomic_read(&failed[SMB2_CLOSE_HE]));
  437. seq_printf(m, "\nFlushes: %d sent %d failed",
  438. atomic_read(&sent[SMB2_FLUSH_HE]),
  439. atomic_read(&failed[SMB2_FLUSH_HE]));
  440. seq_printf(m, "\nReads: %d sent %d failed",
  441. atomic_read(&sent[SMB2_READ_HE]),
  442. atomic_read(&failed[SMB2_READ_HE]));
  443. seq_printf(m, "\nWrites: %d sent %d failed",
  444. atomic_read(&sent[SMB2_WRITE_HE]),
  445. atomic_read(&failed[SMB2_WRITE_HE]));
  446. seq_printf(m, "\nLocks: %d sent %d failed",
  447. atomic_read(&sent[SMB2_LOCK_HE]),
  448. atomic_read(&failed[SMB2_LOCK_HE]));
  449. seq_printf(m, "\nIOCTLs: %d sent %d failed",
  450. atomic_read(&sent[SMB2_IOCTL_HE]),
  451. atomic_read(&failed[SMB2_IOCTL_HE]));
  452. seq_printf(m, "\nCancels: %d sent %d failed",
  453. atomic_read(&sent[SMB2_CANCEL_HE]),
  454. atomic_read(&failed[SMB2_CANCEL_HE]));
  455. seq_printf(m, "\nEchos: %d sent %d failed",
  456. atomic_read(&sent[SMB2_ECHO_HE]),
  457. atomic_read(&failed[SMB2_ECHO_HE]));
  458. seq_printf(m, "\nQueryDirectories: %d sent %d failed",
  459. atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
  460. atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
  461. seq_printf(m, "\nChangeNotifies: %d sent %d failed",
  462. atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
  463. atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
  464. seq_printf(m, "\nQueryInfos: %d sent %d failed",
  465. atomic_read(&sent[SMB2_QUERY_INFO_HE]),
  466. atomic_read(&failed[SMB2_QUERY_INFO_HE]));
  467. seq_printf(m, "\nSetInfos: %d sent %d failed",
  468. atomic_read(&sent[SMB2_SET_INFO_HE]),
  469. atomic_read(&failed[SMB2_SET_INFO_HE]));
  470. seq_printf(m, "\nOplockBreaks: %d sent %d failed",
  471. atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
  472. atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
  473. #endif
  474. }
  475. static void
  476. smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
  477. {
  478. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  479. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  480. cfile->fid.persistent_fid = fid->persistent_fid;
  481. cfile->fid.volatile_fid = fid->volatile_fid;
  482. server->ops->set_oplock_level(cinode, oplock, fid->epoch,
  483. &fid->purge_cache);
  484. cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
  485. memcpy(cfile->fid.create_guid, fid->create_guid, 16);
  486. }
  487. static void
  488. smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
  489. struct cifs_fid *fid)
  490. {
  491. SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  492. }
  493. static int
  494. SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
  495. u64 persistent_fid, u64 volatile_fid,
  496. struct copychunk_ioctl *pcchunk)
  497. {
  498. int rc;
  499. unsigned int ret_data_len;
  500. struct resume_key_req *res_key;
  501. rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
  502. FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
  503. NULL, 0 /* no input */,
  504. (char **)&res_key, &ret_data_len);
  505. if (rc) {
  506. cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
  507. goto req_res_key_exit;
  508. }
  509. if (ret_data_len < sizeof(struct resume_key_req)) {
  510. cifs_dbg(VFS, "Invalid refcopy resume key length\n");
  511. rc = -EINVAL;
  512. goto req_res_key_exit;
  513. }
  514. memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
  515. req_res_key_exit:
  516. kfree(res_key);
  517. return rc;
  518. }
  519. static int
  520. smb2_clone_range(const unsigned int xid,
  521. struct cifsFileInfo *srcfile,
  522. struct cifsFileInfo *trgtfile, u64 src_off,
  523. u64 len, u64 dest_off)
  524. {
  525. int rc;
  526. unsigned int ret_data_len;
  527. struct copychunk_ioctl *pcchunk;
  528. struct copychunk_ioctl_rsp *retbuf = NULL;
  529. struct cifs_tcon *tcon;
  530. int chunks_copied = 0;
  531. bool chunk_sizes_updated = false;
  532. pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
  533. if (pcchunk == NULL)
  534. return -ENOMEM;
  535. cifs_dbg(FYI, "in smb2_clone_range - about to call request res key\n");
  536. /* Request a key from the server to identify the source of the copy */
  537. rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
  538. srcfile->fid.persistent_fid,
  539. srcfile->fid.volatile_fid, pcchunk);
  540. /* Note: request_res_key sets res_key null only if rc !=0 */
  541. if (rc)
  542. goto cchunk_out;
  543. /* For now array only one chunk long, will make more flexible later */
  544. pcchunk->ChunkCount = cpu_to_le32(1);
  545. pcchunk->Reserved = 0;
  546. pcchunk->Reserved2 = 0;
  547. tcon = tlink_tcon(trgtfile->tlink);
  548. while (len > 0) {
  549. pcchunk->SourceOffset = cpu_to_le64(src_off);
  550. pcchunk->TargetOffset = cpu_to_le64(dest_off);
  551. pcchunk->Length =
  552. cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
  553. /* Request server copy to target from src identified by key */
  554. rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
  555. trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
  556. true /* is_fsctl */, (char *)pcchunk,
  557. sizeof(struct copychunk_ioctl), (char **)&retbuf,
  558. &ret_data_len);
  559. if (rc == 0) {
  560. if (ret_data_len !=
  561. sizeof(struct copychunk_ioctl_rsp)) {
  562. cifs_dbg(VFS, "invalid cchunk response size\n");
  563. rc = -EIO;
  564. goto cchunk_out;
  565. }
  566. if (retbuf->TotalBytesWritten == 0) {
  567. cifs_dbg(FYI, "no bytes copied\n");
  568. rc = -EIO;
  569. goto cchunk_out;
  570. }
  571. /*
  572. * Check if server claimed to write more than we asked
  573. */
  574. if (le32_to_cpu(retbuf->TotalBytesWritten) >
  575. le32_to_cpu(pcchunk->Length)) {
  576. cifs_dbg(VFS, "invalid copy chunk response\n");
  577. rc = -EIO;
  578. goto cchunk_out;
  579. }
  580. if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
  581. cifs_dbg(VFS, "invalid num chunks written\n");
  582. rc = -EIO;
  583. goto cchunk_out;
  584. }
  585. chunks_copied++;
  586. src_off += le32_to_cpu(retbuf->TotalBytesWritten);
  587. dest_off += le32_to_cpu(retbuf->TotalBytesWritten);
  588. len -= le32_to_cpu(retbuf->TotalBytesWritten);
  589. cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %d\n",
  590. le32_to_cpu(retbuf->ChunksWritten),
  591. le32_to_cpu(retbuf->ChunkBytesWritten),
  592. le32_to_cpu(retbuf->TotalBytesWritten));
  593. } else if (rc == -EINVAL) {
  594. if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
  595. goto cchunk_out;
  596. cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
  597. le32_to_cpu(retbuf->ChunksWritten),
  598. le32_to_cpu(retbuf->ChunkBytesWritten),
  599. le32_to_cpu(retbuf->TotalBytesWritten));
  600. /*
  601. * Check if this is the first request using these sizes,
  602. * (ie check if copy succeed once with original sizes
  603. * and check if the server gave us different sizes after
  604. * we already updated max sizes on previous request).
  605. * if not then why is the server returning an error now
  606. */
  607. if ((chunks_copied != 0) || chunk_sizes_updated)
  608. goto cchunk_out;
  609. /* Check that server is not asking us to grow size */
  610. if (le32_to_cpu(retbuf->ChunkBytesWritten) <
  611. tcon->max_bytes_chunk)
  612. tcon->max_bytes_chunk =
  613. le32_to_cpu(retbuf->ChunkBytesWritten);
  614. else
  615. goto cchunk_out; /* server gave us bogus size */
  616. /* No need to change MaxChunks since already set to 1 */
  617. chunk_sizes_updated = true;
  618. } else
  619. goto cchunk_out;
  620. }
  621. cchunk_out:
  622. kfree(pcchunk);
  623. kfree(retbuf);
  624. return rc;
  625. }
  626. static int
  627. smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
  628. struct cifs_fid *fid)
  629. {
  630. return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  631. }
  632. static unsigned int
  633. smb2_read_data_offset(char *buf)
  634. {
  635. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  636. return rsp->DataOffset;
  637. }
  638. static unsigned int
  639. smb2_read_data_length(char *buf)
  640. {
  641. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  642. return le32_to_cpu(rsp->DataLength);
  643. }
  644. static int
  645. smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
  646. struct cifs_io_parms *parms, unsigned int *bytes_read,
  647. char **buf, int *buf_type)
  648. {
  649. parms->persistent_fid = pfid->persistent_fid;
  650. parms->volatile_fid = pfid->volatile_fid;
  651. return SMB2_read(xid, parms, bytes_read, buf, buf_type);
  652. }
  653. static int
  654. smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
  655. struct cifs_io_parms *parms, unsigned int *written,
  656. struct kvec *iov, unsigned long nr_segs)
  657. {
  658. parms->persistent_fid = pfid->persistent_fid;
  659. parms->volatile_fid = pfid->volatile_fid;
  660. return SMB2_write(xid, parms, written, iov, nr_segs);
  661. }
  662. /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
  663. static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
  664. struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
  665. {
  666. struct cifsInodeInfo *cifsi;
  667. int rc;
  668. cifsi = CIFS_I(inode);
  669. /* if file already sparse don't bother setting sparse again */
  670. if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
  671. return true; /* already sparse */
  672. if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
  673. return true; /* already not sparse */
  674. /*
  675. * Can't check for sparse support on share the usual way via the
  676. * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
  677. * since Samba server doesn't set the flag on the share, yet
  678. * supports the set sparse FSCTL and returns sparse correctly
  679. * in the file attributes. If we fail setting sparse though we
  680. * mark that server does not support sparse files for this share
  681. * to avoid repeatedly sending the unsupported fsctl to server
  682. * if the file is repeatedly extended.
  683. */
  684. if (tcon->broken_sparse_sup)
  685. return false;
  686. rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  687. cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
  688. true /* is_fctl */, &setsparse, 1, NULL, NULL);
  689. if (rc) {
  690. tcon->broken_sparse_sup = true;
  691. cifs_dbg(FYI, "set sparse rc = %d\n", rc);
  692. return false;
  693. }
  694. if (setsparse)
  695. cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
  696. else
  697. cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
  698. return true;
  699. }
  700. static int
  701. smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
  702. struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
  703. {
  704. __le64 eof = cpu_to_le64(size);
  705. struct inode *inode;
  706. /*
  707. * If extending file more than one page make sparse. Many Linux fs
  708. * make files sparse by default when extending via ftruncate
  709. */
  710. inode = d_inode(cfile->dentry);
  711. if (!set_alloc && (size > inode->i_size + 8192)) {
  712. __u8 set_sparse = 1;
  713. /* whether set sparse succeeds or not, extend the file */
  714. smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
  715. }
  716. return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
  717. cfile->fid.volatile_fid, cfile->pid, &eof, false);
  718. }
  719. static int
  720. smb2_duplicate_extents(const unsigned int xid,
  721. struct cifsFileInfo *srcfile,
  722. struct cifsFileInfo *trgtfile, u64 src_off,
  723. u64 len, u64 dest_off)
  724. {
  725. int rc;
  726. unsigned int ret_data_len;
  727. struct duplicate_extents_to_file dup_ext_buf;
  728. struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
  729. /* server fileays advertise duplicate extent support with this flag */
  730. if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
  731. FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
  732. return -EOPNOTSUPP;
  733. dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
  734. dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
  735. dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
  736. dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
  737. dup_ext_buf.ByteCount = cpu_to_le64(len);
  738. cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
  739. src_off, dest_off, len);
  740. rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
  741. if (rc)
  742. goto duplicate_extents_out;
  743. rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
  744. trgtfile->fid.volatile_fid,
  745. FSCTL_DUPLICATE_EXTENTS_TO_FILE,
  746. true /* is_fsctl */, (char *)&dup_ext_buf,
  747. sizeof(struct duplicate_extents_to_file),
  748. NULL,
  749. &ret_data_len);
  750. if (ret_data_len > 0)
  751. cifs_dbg(FYI, "non-zero response length in duplicate extents");
  752. duplicate_extents_out:
  753. return rc;
  754. }
  755. static int
  756. smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
  757. struct cifsFileInfo *cfile)
  758. {
  759. return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
  760. cfile->fid.volatile_fid);
  761. }
  762. static int
  763. smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
  764. struct cifsFileInfo *cfile)
  765. {
  766. struct fsctl_set_integrity_information_req integr_info;
  767. unsigned int ret_data_len;
  768. integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
  769. integr_info.Flags = 0;
  770. integr_info.Reserved = 0;
  771. return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  772. cfile->fid.volatile_fid,
  773. FSCTL_SET_INTEGRITY_INFORMATION,
  774. true /* is_fsctl */, (char *)&integr_info,
  775. sizeof(struct fsctl_set_integrity_information_req),
  776. NULL,
  777. &ret_data_len);
  778. }
  779. static int
  780. smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
  781. const char *path, struct cifs_sb_info *cifs_sb,
  782. struct cifs_fid *fid, __u16 search_flags,
  783. struct cifs_search_info *srch_inf)
  784. {
  785. __le16 *utf16_path;
  786. int rc;
  787. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  788. struct cifs_open_parms oparms;
  789. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  790. if (!utf16_path)
  791. return -ENOMEM;
  792. oparms.tcon = tcon;
  793. oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
  794. oparms.disposition = FILE_OPEN;
  795. oparms.create_options = 0;
  796. oparms.fid = fid;
  797. oparms.reconnect = false;
  798. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
  799. kfree(utf16_path);
  800. if (rc) {
  801. cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
  802. return rc;
  803. }
  804. srch_inf->entries_in_buffer = 0;
  805. srch_inf->index_of_last_entry = 2;
  806. rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
  807. fid->volatile_fid, 0, srch_inf);
  808. if (rc) {
  809. cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
  810. SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  811. }
  812. return rc;
  813. }
  814. static int
  815. smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
  816. struct cifs_fid *fid, __u16 search_flags,
  817. struct cifs_search_info *srch_inf)
  818. {
  819. return SMB2_query_directory(xid, tcon, fid->persistent_fid,
  820. fid->volatile_fid, 0, srch_inf);
  821. }
  822. static int
  823. smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
  824. struct cifs_fid *fid)
  825. {
  826. return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  827. }
  828. /*
  829. * If we negotiate SMB2 protocol and get STATUS_PENDING - update
  830. * the number of credits and return true. Otherwise - return false.
  831. */
  832. static bool
  833. smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
  834. {
  835. struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
  836. if (hdr->Status != STATUS_PENDING)
  837. return false;
  838. if (!length) {
  839. spin_lock(&server->req_lock);
  840. server->credits += le16_to_cpu(hdr->CreditRequest);
  841. spin_unlock(&server->req_lock);
  842. wake_up(&server->request_q);
  843. }
  844. return true;
  845. }
  846. static bool
  847. smb2_is_session_expired(char *buf)
  848. {
  849. struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
  850. if (hdr->Status != STATUS_NETWORK_SESSION_EXPIRED)
  851. return false;
  852. cifs_dbg(FYI, "Session expired\n");
  853. return true;
  854. }
  855. static int
  856. smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
  857. struct cifsInodeInfo *cinode)
  858. {
  859. if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
  860. return SMB2_lease_break(0, tcon, cinode->lease_key,
  861. smb2_get_lease_state(cinode));
  862. return SMB2_oplock_break(0, tcon, fid->persistent_fid,
  863. fid->volatile_fid,
  864. CIFS_CACHE_READ(cinode) ? 1 : 0);
  865. }
  866. static int
  867. smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
  868. struct kstatfs *buf)
  869. {
  870. int rc;
  871. __le16 srch_path = 0; /* Null - open root of share */
  872. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  873. struct cifs_open_parms oparms;
  874. struct cifs_fid fid;
  875. oparms.tcon = tcon;
  876. oparms.desired_access = FILE_READ_ATTRIBUTES;
  877. oparms.disposition = FILE_OPEN;
  878. oparms.create_options = 0;
  879. oparms.fid = &fid;
  880. oparms.reconnect = false;
  881. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
  882. if (rc)
  883. return rc;
  884. buf->f_type = SMB2_MAGIC_NUMBER;
  885. rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  886. buf);
  887. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  888. return rc;
  889. }
  890. static bool
  891. smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
  892. {
  893. return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
  894. ob1->fid.volatile_fid == ob2->fid.volatile_fid;
  895. }
  896. static int
  897. smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
  898. __u64 length, __u32 type, int lock, int unlock, bool wait)
  899. {
  900. if (unlock && !lock)
  901. type = SMB2_LOCKFLAG_UNLOCK;
  902. return SMB2_lock(xid, tlink_tcon(cfile->tlink),
  903. cfile->fid.persistent_fid, cfile->fid.volatile_fid,
  904. current->tgid, length, offset, type, wait);
  905. }
  906. static void
  907. smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
  908. {
  909. memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
  910. }
  911. static void
  912. smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
  913. {
  914. memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
  915. }
  916. static void
  917. smb2_new_lease_key(struct cifs_fid *fid)
  918. {
  919. generate_random_uuid(fid->lease_key);
  920. }
  921. #define SMB2_SYMLINK_STRUCT_SIZE \
  922. (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
  923. static int
  924. smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
  925. const char *full_path, char **target_path,
  926. struct cifs_sb_info *cifs_sb)
  927. {
  928. int rc;
  929. __le16 *utf16_path;
  930. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  931. struct cifs_open_parms oparms;
  932. struct cifs_fid fid;
  933. struct smb2_err_rsp *err_buf = NULL;
  934. struct smb2_symlink_err_rsp *symlink;
  935. unsigned int sub_len;
  936. unsigned int sub_offset;
  937. unsigned int print_len;
  938. unsigned int print_offset;
  939. cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
  940. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  941. if (!utf16_path)
  942. return -ENOMEM;
  943. oparms.tcon = tcon;
  944. oparms.desired_access = FILE_READ_ATTRIBUTES;
  945. oparms.disposition = FILE_OPEN;
  946. oparms.create_options = 0;
  947. oparms.fid = &fid;
  948. oparms.reconnect = false;
  949. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
  950. if (!rc || !err_buf) {
  951. kfree(utf16_path);
  952. return -ENOENT;
  953. }
  954. if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
  955. get_rfc1002_length(err_buf) + 4 < SMB2_SYMLINK_STRUCT_SIZE) {
  956. kfree(utf16_path);
  957. return -ENOENT;
  958. }
  959. /* open must fail on symlink - reset rc */
  960. rc = 0;
  961. symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
  962. sub_len = le16_to_cpu(symlink->SubstituteNameLength);
  963. sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
  964. print_len = le16_to_cpu(symlink->PrintNameLength);
  965. print_offset = le16_to_cpu(symlink->PrintNameOffset);
  966. if (get_rfc1002_length(err_buf) + 4 <
  967. SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
  968. kfree(utf16_path);
  969. return -ENOENT;
  970. }
  971. if (get_rfc1002_length(err_buf) + 4 <
  972. SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
  973. kfree(utf16_path);
  974. return -ENOENT;
  975. }
  976. *target_path = cifs_strndup_from_utf16(
  977. (char *)symlink->PathBuffer + sub_offset,
  978. sub_len, true, cifs_sb->local_nls);
  979. if (!(*target_path)) {
  980. kfree(utf16_path);
  981. return -ENOMEM;
  982. }
  983. convert_delimiter(*target_path, '/');
  984. cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
  985. kfree(utf16_path);
  986. return rc;
  987. }
  988. static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
  989. loff_t offset, loff_t len, bool keep_size)
  990. {
  991. struct inode *inode;
  992. struct cifsInodeInfo *cifsi;
  993. struct cifsFileInfo *cfile = file->private_data;
  994. struct file_zero_data_information fsctl_buf;
  995. long rc;
  996. unsigned int xid;
  997. xid = get_xid();
  998. inode = d_inode(cfile->dentry);
  999. cifsi = CIFS_I(inode);
  1000. /* if file not oplocked can't be sure whether asking to extend size */
  1001. if (!CIFS_CACHE_READ(cifsi))
  1002. if (keep_size == false)
  1003. return -EOPNOTSUPP;
  1004. /*
  1005. * Must check if file sparse since fallocate -z (zero range) assumes
  1006. * non-sparse allocation
  1007. */
  1008. if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE))
  1009. return -EOPNOTSUPP;
  1010. /*
  1011. * need to make sure we are not asked to extend the file since the SMB3
  1012. * fsctl does not change the file size. In the future we could change
  1013. * this to zero the first part of the range then set the file size
  1014. * which for a non sparse file would zero the newly extended range
  1015. */
  1016. if (keep_size == false)
  1017. if (i_size_read(inode) < offset + len)
  1018. return -EOPNOTSUPP;
  1019. cifs_dbg(FYI, "offset %lld len %lld", offset, len);
  1020. fsctl_buf.FileOffset = cpu_to_le64(offset);
  1021. fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
  1022. rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  1023. cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
  1024. true /* is_fctl */, (char *)&fsctl_buf,
  1025. sizeof(struct file_zero_data_information), NULL, NULL);
  1026. free_xid(xid);
  1027. return rc;
  1028. }
  1029. static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
  1030. loff_t offset, loff_t len)
  1031. {
  1032. struct inode *inode;
  1033. struct cifsInodeInfo *cifsi;
  1034. struct cifsFileInfo *cfile = file->private_data;
  1035. struct file_zero_data_information fsctl_buf;
  1036. long rc;
  1037. unsigned int xid;
  1038. __u8 set_sparse = 1;
  1039. xid = get_xid();
  1040. inode = d_inode(cfile->dentry);
  1041. cifsi = CIFS_I(inode);
  1042. /* Need to make file sparse, if not already, before freeing range. */
  1043. /* Consider adding equivalent for compressed since it could also work */
  1044. if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse))
  1045. return -EOPNOTSUPP;
  1046. cifs_dbg(FYI, "offset %lld len %lld", offset, len);
  1047. fsctl_buf.FileOffset = cpu_to_le64(offset);
  1048. fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
  1049. rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  1050. cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
  1051. true /* is_fctl */, (char *)&fsctl_buf,
  1052. sizeof(struct file_zero_data_information), NULL, NULL);
  1053. free_xid(xid);
  1054. return rc;
  1055. }
  1056. static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
  1057. loff_t off, loff_t len, bool keep_size)
  1058. {
  1059. struct inode *inode;
  1060. struct cifsInodeInfo *cifsi;
  1061. struct cifsFileInfo *cfile = file->private_data;
  1062. long rc = -EOPNOTSUPP;
  1063. unsigned int xid;
  1064. xid = get_xid();
  1065. inode = d_inode(cfile->dentry);
  1066. cifsi = CIFS_I(inode);
  1067. /* if file not oplocked can't be sure whether asking to extend size */
  1068. if (!CIFS_CACHE_READ(cifsi))
  1069. if (keep_size == false)
  1070. return -EOPNOTSUPP;
  1071. /*
  1072. * Files are non-sparse by default so falloc may be a no-op
  1073. * Must check if file sparse. If not sparse, and not extending
  1074. * then no need to do anything since file already allocated
  1075. */
  1076. if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
  1077. if (keep_size == true)
  1078. return 0;
  1079. /* check if extending file */
  1080. else if (i_size_read(inode) >= off + len)
  1081. /* not extending file and already not sparse */
  1082. return 0;
  1083. /* BB: in future add else clause to extend file */
  1084. else
  1085. return -EOPNOTSUPP;
  1086. }
  1087. if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
  1088. /*
  1089. * Check if falloc starts within first few pages of file
  1090. * and ends within a few pages of the end of file to
  1091. * ensure that most of file is being forced to be
  1092. * fallocated now. If so then setting whole file sparse
  1093. * ie potentially making a few extra pages at the beginning
  1094. * or end of the file non-sparse via set_sparse is harmless.
  1095. */
  1096. if ((off > 8192) || (off + len + 8192 < i_size_read(inode)))
  1097. return -EOPNOTSUPP;
  1098. rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
  1099. }
  1100. /* BB: else ... in future add code to extend file and set sparse */
  1101. free_xid(xid);
  1102. return rc;
  1103. }
  1104. static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
  1105. loff_t off, loff_t len)
  1106. {
  1107. /* KEEP_SIZE already checked for by do_fallocate */
  1108. if (mode & FALLOC_FL_PUNCH_HOLE)
  1109. return smb3_punch_hole(file, tcon, off, len);
  1110. else if (mode & FALLOC_FL_ZERO_RANGE) {
  1111. if (mode & FALLOC_FL_KEEP_SIZE)
  1112. return smb3_zero_range(file, tcon, off, len, true);
  1113. return smb3_zero_range(file, tcon, off, len, false);
  1114. } else if (mode == FALLOC_FL_KEEP_SIZE)
  1115. return smb3_simple_falloc(file, tcon, off, len, true);
  1116. else if (mode == 0)
  1117. return smb3_simple_falloc(file, tcon, off, len, false);
  1118. return -EOPNOTSUPP;
  1119. }
  1120. static void
  1121. smb2_downgrade_oplock(struct TCP_Server_Info *server,
  1122. struct cifsInodeInfo *cinode, bool set_level2)
  1123. {
  1124. if (set_level2)
  1125. server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
  1126. 0, NULL);
  1127. else
  1128. server->ops->set_oplock_level(cinode, 0, 0, NULL);
  1129. }
  1130. static void
  1131. smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  1132. unsigned int epoch, bool *purge_cache)
  1133. {
  1134. oplock &= 0xFF;
  1135. if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
  1136. return;
  1137. if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
  1138. cinode->oplock = CIFS_CACHE_RHW_FLG;
  1139. cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
  1140. &cinode->vfs_inode);
  1141. } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
  1142. cinode->oplock = CIFS_CACHE_RW_FLG;
  1143. cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
  1144. &cinode->vfs_inode);
  1145. } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
  1146. cinode->oplock = CIFS_CACHE_READ_FLG;
  1147. cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
  1148. &cinode->vfs_inode);
  1149. } else
  1150. cinode->oplock = 0;
  1151. }
  1152. static void
  1153. smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  1154. unsigned int epoch, bool *purge_cache)
  1155. {
  1156. char message[5] = {0};
  1157. oplock &= 0xFF;
  1158. if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
  1159. return;
  1160. cinode->oplock = 0;
  1161. if (oplock & SMB2_LEASE_READ_CACHING_HE) {
  1162. cinode->oplock |= CIFS_CACHE_READ_FLG;
  1163. strcat(message, "R");
  1164. }
  1165. if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
  1166. cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
  1167. strcat(message, "H");
  1168. }
  1169. if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
  1170. cinode->oplock |= CIFS_CACHE_WRITE_FLG;
  1171. strcat(message, "W");
  1172. }
  1173. if (!cinode->oplock)
  1174. strcat(message, "None");
  1175. cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
  1176. &cinode->vfs_inode);
  1177. }
  1178. static void
  1179. smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  1180. unsigned int epoch, bool *purge_cache)
  1181. {
  1182. unsigned int old_oplock = cinode->oplock;
  1183. smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
  1184. if (purge_cache) {
  1185. *purge_cache = false;
  1186. if (old_oplock == CIFS_CACHE_READ_FLG) {
  1187. if (cinode->oplock == CIFS_CACHE_READ_FLG &&
  1188. (epoch - cinode->epoch > 0))
  1189. *purge_cache = true;
  1190. else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
  1191. (epoch - cinode->epoch > 1))
  1192. *purge_cache = true;
  1193. else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
  1194. (epoch - cinode->epoch > 1))
  1195. *purge_cache = true;
  1196. else if (cinode->oplock == 0 &&
  1197. (epoch - cinode->epoch > 0))
  1198. *purge_cache = true;
  1199. } else if (old_oplock == CIFS_CACHE_RH_FLG) {
  1200. if (cinode->oplock == CIFS_CACHE_RH_FLG &&
  1201. (epoch - cinode->epoch > 0))
  1202. *purge_cache = true;
  1203. else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
  1204. (epoch - cinode->epoch > 1))
  1205. *purge_cache = true;
  1206. }
  1207. cinode->epoch = epoch;
  1208. }
  1209. }
  1210. static bool
  1211. smb2_is_read_op(__u32 oplock)
  1212. {
  1213. return oplock == SMB2_OPLOCK_LEVEL_II;
  1214. }
  1215. static bool
  1216. smb21_is_read_op(__u32 oplock)
  1217. {
  1218. return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
  1219. !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
  1220. }
  1221. static __le32
  1222. map_oplock_to_lease(u8 oplock)
  1223. {
  1224. if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
  1225. return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
  1226. else if (oplock == SMB2_OPLOCK_LEVEL_II)
  1227. return SMB2_LEASE_READ_CACHING;
  1228. else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
  1229. return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
  1230. SMB2_LEASE_WRITE_CACHING;
  1231. return 0;
  1232. }
  1233. static char *
  1234. smb2_create_lease_buf(u8 *lease_key, u8 oplock)
  1235. {
  1236. struct create_lease *buf;
  1237. buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
  1238. if (!buf)
  1239. return NULL;
  1240. buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
  1241. buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
  1242. buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
  1243. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1244. (struct create_lease, lcontext));
  1245. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
  1246. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1247. (struct create_lease, Name));
  1248. buf->ccontext.NameLength = cpu_to_le16(4);
  1249. /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
  1250. buf->Name[0] = 'R';
  1251. buf->Name[1] = 'q';
  1252. buf->Name[2] = 'L';
  1253. buf->Name[3] = 's';
  1254. return (char *)buf;
  1255. }
  1256. static char *
  1257. smb3_create_lease_buf(u8 *lease_key, u8 oplock)
  1258. {
  1259. struct create_lease_v2 *buf;
  1260. buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
  1261. if (!buf)
  1262. return NULL;
  1263. buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
  1264. buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
  1265. buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
  1266. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1267. (struct create_lease_v2, lcontext));
  1268. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
  1269. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1270. (struct create_lease_v2, Name));
  1271. buf->ccontext.NameLength = cpu_to_le16(4);
  1272. /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
  1273. buf->Name[0] = 'R';
  1274. buf->Name[1] = 'q';
  1275. buf->Name[2] = 'L';
  1276. buf->Name[3] = 's';
  1277. return (char *)buf;
  1278. }
  1279. static __u8
  1280. smb2_parse_lease_buf(void *buf, unsigned int *epoch)
  1281. {
  1282. struct create_lease *lc = (struct create_lease *)buf;
  1283. *epoch = 0; /* not used */
  1284. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
  1285. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  1286. return le32_to_cpu(lc->lcontext.LeaseState);
  1287. }
  1288. static __u8
  1289. smb3_parse_lease_buf(void *buf, unsigned int *epoch)
  1290. {
  1291. struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
  1292. *epoch = le16_to_cpu(lc->lcontext.Epoch);
  1293. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
  1294. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  1295. return le32_to_cpu(lc->lcontext.LeaseState);
  1296. }
  1297. static unsigned int
  1298. smb2_wp_retry_size(struct inode *inode)
  1299. {
  1300. return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
  1301. SMB2_MAX_BUFFER_SIZE);
  1302. }
  1303. static bool
  1304. smb2_dir_needs_close(struct cifsFileInfo *cfile)
  1305. {
  1306. return !cfile->invalidHandle;
  1307. }
  1308. struct smb_version_operations smb20_operations = {
  1309. .compare_fids = smb2_compare_fids,
  1310. .setup_request = smb2_setup_request,
  1311. .setup_async_request = smb2_setup_async_request,
  1312. .check_receive = smb2_check_receive,
  1313. .add_credits = smb2_add_credits,
  1314. .set_credits = smb2_set_credits,
  1315. .get_credits_field = smb2_get_credits_field,
  1316. .get_credits = smb2_get_credits,
  1317. .wait_mtu_credits = cifs_wait_mtu_credits,
  1318. .get_next_mid = smb2_get_next_mid,
  1319. .read_data_offset = smb2_read_data_offset,
  1320. .read_data_length = smb2_read_data_length,
  1321. .map_error = map_smb2_to_linux_error,
  1322. .find_mid = smb2_find_mid,
  1323. .check_message = smb2_check_message,
  1324. .dump_detail = smb2_dump_detail,
  1325. .clear_stats = smb2_clear_stats,
  1326. .print_stats = smb2_print_stats,
  1327. .is_oplock_break = smb2_is_valid_oplock_break,
  1328. .handle_cancelled_mid = smb2_handle_cancelled_mid,
  1329. .downgrade_oplock = smb2_downgrade_oplock,
  1330. .need_neg = smb2_need_neg,
  1331. .negotiate = smb2_negotiate,
  1332. .negotiate_wsize = smb2_negotiate_wsize,
  1333. .negotiate_rsize = smb2_negotiate_rsize,
  1334. .sess_setup = SMB2_sess_setup,
  1335. .logoff = SMB2_logoff,
  1336. .tree_connect = SMB2_tcon,
  1337. .tree_disconnect = SMB2_tdis,
  1338. .qfs_tcon = smb2_qfs_tcon,
  1339. .is_path_accessible = smb2_is_path_accessible,
  1340. .can_echo = smb2_can_echo,
  1341. .echo = SMB2_echo,
  1342. .query_path_info = smb2_query_path_info,
  1343. .get_srv_inum = smb2_get_srv_inum,
  1344. .query_file_info = smb2_query_file_info,
  1345. .set_path_size = smb2_set_path_size,
  1346. .set_file_size = smb2_set_file_size,
  1347. .set_file_info = smb2_set_file_info,
  1348. .set_compression = smb2_set_compression,
  1349. .mkdir = smb2_mkdir,
  1350. .mkdir_setinfo = smb2_mkdir_setinfo,
  1351. .rmdir = smb2_rmdir,
  1352. .unlink = smb2_unlink,
  1353. .rename = smb2_rename_path,
  1354. .create_hardlink = smb2_create_hardlink,
  1355. .query_symlink = smb2_query_symlink,
  1356. .open = smb2_open_file,
  1357. .set_fid = smb2_set_fid,
  1358. .close = smb2_close_file,
  1359. .flush = smb2_flush_file,
  1360. .async_readv = smb2_async_readv,
  1361. .async_writev = smb2_async_writev,
  1362. .sync_read = smb2_sync_read,
  1363. .sync_write = smb2_sync_write,
  1364. .query_dir_first = smb2_query_dir_first,
  1365. .query_dir_next = smb2_query_dir_next,
  1366. .close_dir = smb2_close_dir,
  1367. .calc_smb_size = smb2_calc_size,
  1368. .is_status_pending = smb2_is_status_pending,
  1369. .is_session_expired = smb2_is_session_expired,
  1370. .oplock_response = smb2_oplock_response,
  1371. .queryfs = smb2_queryfs,
  1372. .mand_lock = smb2_mand_lock,
  1373. .mand_unlock_range = smb2_unlock_range,
  1374. .push_mand_locks = smb2_push_mandatory_locks,
  1375. .get_lease_key = smb2_get_lease_key,
  1376. .set_lease_key = smb2_set_lease_key,
  1377. .new_lease_key = smb2_new_lease_key,
  1378. .calc_signature = smb2_calc_signature,
  1379. .is_read_op = smb2_is_read_op,
  1380. .set_oplock_level = smb2_set_oplock_level,
  1381. .create_lease_buf = smb2_create_lease_buf,
  1382. .parse_lease_buf = smb2_parse_lease_buf,
  1383. .clone_range = smb2_clone_range,
  1384. .wp_retry_size = smb2_wp_retry_size,
  1385. .dir_needs_close = smb2_dir_needs_close,
  1386. };
  1387. struct smb_version_operations smb21_operations = {
  1388. .compare_fids = smb2_compare_fids,
  1389. .setup_request = smb2_setup_request,
  1390. .setup_async_request = smb2_setup_async_request,
  1391. .check_receive = smb2_check_receive,
  1392. .add_credits = smb2_add_credits,
  1393. .set_credits = smb2_set_credits,
  1394. .get_credits_field = smb2_get_credits_field,
  1395. .get_credits = smb2_get_credits,
  1396. .wait_mtu_credits = smb2_wait_mtu_credits,
  1397. .get_next_mid = smb2_get_next_mid,
  1398. .read_data_offset = smb2_read_data_offset,
  1399. .read_data_length = smb2_read_data_length,
  1400. .map_error = map_smb2_to_linux_error,
  1401. .find_mid = smb2_find_mid,
  1402. .check_message = smb2_check_message,
  1403. .dump_detail = smb2_dump_detail,
  1404. .clear_stats = smb2_clear_stats,
  1405. .print_stats = smb2_print_stats,
  1406. .is_oplock_break = smb2_is_valid_oplock_break,
  1407. .handle_cancelled_mid = smb2_handle_cancelled_mid,
  1408. .downgrade_oplock = smb2_downgrade_oplock,
  1409. .need_neg = smb2_need_neg,
  1410. .negotiate = smb2_negotiate,
  1411. .negotiate_wsize = smb2_negotiate_wsize,
  1412. .negotiate_rsize = smb2_negotiate_rsize,
  1413. .sess_setup = SMB2_sess_setup,
  1414. .logoff = SMB2_logoff,
  1415. .tree_connect = SMB2_tcon,
  1416. .tree_disconnect = SMB2_tdis,
  1417. .qfs_tcon = smb2_qfs_tcon,
  1418. .is_path_accessible = smb2_is_path_accessible,
  1419. .can_echo = smb2_can_echo,
  1420. .echo = SMB2_echo,
  1421. .query_path_info = smb2_query_path_info,
  1422. .get_srv_inum = smb2_get_srv_inum,
  1423. .query_file_info = smb2_query_file_info,
  1424. .set_path_size = smb2_set_path_size,
  1425. .set_file_size = smb2_set_file_size,
  1426. .set_file_info = smb2_set_file_info,
  1427. .set_compression = smb2_set_compression,
  1428. .mkdir = smb2_mkdir,
  1429. .mkdir_setinfo = smb2_mkdir_setinfo,
  1430. .rmdir = smb2_rmdir,
  1431. .unlink = smb2_unlink,
  1432. .rename = smb2_rename_path,
  1433. .create_hardlink = smb2_create_hardlink,
  1434. .query_symlink = smb2_query_symlink,
  1435. .query_mf_symlink = smb3_query_mf_symlink,
  1436. .create_mf_symlink = smb3_create_mf_symlink,
  1437. .open = smb2_open_file,
  1438. .set_fid = smb2_set_fid,
  1439. .close = smb2_close_file,
  1440. .flush = smb2_flush_file,
  1441. .async_readv = smb2_async_readv,
  1442. .async_writev = smb2_async_writev,
  1443. .sync_read = smb2_sync_read,
  1444. .sync_write = smb2_sync_write,
  1445. .query_dir_first = smb2_query_dir_first,
  1446. .query_dir_next = smb2_query_dir_next,
  1447. .close_dir = smb2_close_dir,
  1448. .calc_smb_size = smb2_calc_size,
  1449. .is_status_pending = smb2_is_status_pending,
  1450. .is_session_expired = smb2_is_session_expired,
  1451. .oplock_response = smb2_oplock_response,
  1452. .queryfs = smb2_queryfs,
  1453. .mand_lock = smb2_mand_lock,
  1454. .mand_unlock_range = smb2_unlock_range,
  1455. .push_mand_locks = smb2_push_mandatory_locks,
  1456. .get_lease_key = smb2_get_lease_key,
  1457. .set_lease_key = smb2_set_lease_key,
  1458. .new_lease_key = smb2_new_lease_key,
  1459. .calc_signature = smb2_calc_signature,
  1460. .is_read_op = smb21_is_read_op,
  1461. .set_oplock_level = smb21_set_oplock_level,
  1462. .create_lease_buf = smb2_create_lease_buf,
  1463. .parse_lease_buf = smb2_parse_lease_buf,
  1464. .clone_range = smb2_clone_range,
  1465. .wp_retry_size = smb2_wp_retry_size,
  1466. .dir_needs_close = smb2_dir_needs_close,
  1467. };
  1468. struct smb_version_operations smb30_operations = {
  1469. .compare_fids = smb2_compare_fids,
  1470. .setup_request = smb2_setup_request,
  1471. .setup_async_request = smb2_setup_async_request,
  1472. .check_receive = smb2_check_receive,
  1473. .add_credits = smb2_add_credits,
  1474. .set_credits = smb2_set_credits,
  1475. .get_credits_field = smb2_get_credits_field,
  1476. .get_credits = smb2_get_credits,
  1477. .wait_mtu_credits = smb2_wait_mtu_credits,
  1478. .get_next_mid = smb2_get_next_mid,
  1479. .read_data_offset = smb2_read_data_offset,
  1480. .read_data_length = smb2_read_data_length,
  1481. .map_error = map_smb2_to_linux_error,
  1482. .find_mid = smb2_find_mid,
  1483. .check_message = smb2_check_message,
  1484. .dump_detail = smb2_dump_detail,
  1485. .clear_stats = smb2_clear_stats,
  1486. .print_stats = smb2_print_stats,
  1487. .dump_share_caps = smb2_dump_share_caps,
  1488. .is_oplock_break = smb2_is_valid_oplock_break,
  1489. .handle_cancelled_mid = smb2_handle_cancelled_mid,
  1490. .downgrade_oplock = smb2_downgrade_oplock,
  1491. .need_neg = smb2_need_neg,
  1492. .negotiate = smb2_negotiate,
  1493. .negotiate_wsize = smb2_negotiate_wsize,
  1494. .negotiate_rsize = smb2_negotiate_rsize,
  1495. .sess_setup = SMB2_sess_setup,
  1496. .logoff = SMB2_logoff,
  1497. .tree_connect = SMB2_tcon,
  1498. .tree_disconnect = SMB2_tdis,
  1499. .qfs_tcon = smb3_qfs_tcon,
  1500. .is_path_accessible = smb2_is_path_accessible,
  1501. .can_echo = smb2_can_echo,
  1502. .echo = SMB2_echo,
  1503. .query_path_info = smb2_query_path_info,
  1504. .get_srv_inum = smb2_get_srv_inum,
  1505. .query_file_info = smb2_query_file_info,
  1506. .set_path_size = smb2_set_path_size,
  1507. .set_file_size = smb2_set_file_size,
  1508. .set_file_info = smb2_set_file_info,
  1509. .set_compression = smb2_set_compression,
  1510. .mkdir = smb2_mkdir,
  1511. .mkdir_setinfo = smb2_mkdir_setinfo,
  1512. .rmdir = smb2_rmdir,
  1513. .unlink = smb2_unlink,
  1514. .rename = smb2_rename_path,
  1515. .create_hardlink = smb2_create_hardlink,
  1516. .query_symlink = smb2_query_symlink,
  1517. .query_mf_symlink = smb3_query_mf_symlink,
  1518. .create_mf_symlink = smb3_create_mf_symlink,
  1519. .open = smb2_open_file,
  1520. .set_fid = smb2_set_fid,
  1521. .close = smb2_close_file,
  1522. .flush = smb2_flush_file,
  1523. .async_readv = smb2_async_readv,
  1524. .async_writev = smb2_async_writev,
  1525. .sync_read = smb2_sync_read,
  1526. .sync_write = smb2_sync_write,
  1527. .query_dir_first = smb2_query_dir_first,
  1528. .query_dir_next = smb2_query_dir_next,
  1529. .close_dir = smb2_close_dir,
  1530. .calc_smb_size = smb2_calc_size,
  1531. .is_status_pending = smb2_is_status_pending,
  1532. .is_session_expired = smb2_is_session_expired,
  1533. .oplock_response = smb2_oplock_response,
  1534. .queryfs = smb2_queryfs,
  1535. .mand_lock = smb2_mand_lock,
  1536. .mand_unlock_range = smb2_unlock_range,
  1537. .push_mand_locks = smb2_push_mandatory_locks,
  1538. .get_lease_key = smb2_get_lease_key,
  1539. .set_lease_key = smb2_set_lease_key,
  1540. .new_lease_key = smb2_new_lease_key,
  1541. .generate_signingkey = generate_smb3signingkey,
  1542. .calc_signature = smb3_calc_signature,
  1543. .set_integrity = smb3_set_integrity,
  1544. .is_read_op = smb21_is_read_op,
  1545. .set_oplock_level = smb3_set_oplock_level,
  1546. .create_lease_buf = smb3_create_lease_buf,
  1547. .parse_lease_buf = smb3_parse_lease_buf,
  1548. .clone_range = smb2_clone_range,
  1549. .duplicate_extents = smb2_duplicate_extents,
  1550. .validate_negotiate = smb3_validate_negotiate,
  1551. .wp_retry_size = smb2_wp_retry_size,
  1552. .dir_needs_close = smb2_dir_needs_close,
  1553. .fallocate = smb3_fallocate,
  1554. };
  1555. #ifdef CONFIG_CIFS_SMB311
  1556. struct smb_version_operations smb311_operations = {
  1557. .compare_fids = smb2_compare_fids,
  1558. .setup_request = smb2_setup_request,
  1559. .setup_async_request = smb2_setup_async_request,
  1560. .check_receive = smb2_check_receive,
  1561. .add_credits = smb2_add_credits,
  1562. .set_credits = smb2_set_credits,
  1563. .get_credits_field = smb2_get_credits_field,
  1564. .get_credits = smb2_get_credits,
  1565. .wait_mtu_credits = smb2_wait_mtu_credits,
  1566. .get_next_mid = smb2_get_next_mid,
  1567. .read_data_offset = smb2_read_data_offset,
  1568. .read_data_length = smb2_read_data_length,
  1569. .map_error = map_smb2_to_linux_error,
  1570. .find_mid = smb2_find_mid,
  1571. .check_message = smb2_check_message,
  1572. .dump_detail = smb2_dump_detail,
  1573. .clear_stats = smb2_clear_stats,
  1574. .print_stats = smb2_print_stats,
  1575. .dump_share_caps = smb2_dump_share_caps,
  1576. .is_oplock_break = smb2_is_valid_oplock_break,
  1577. .handle_cancelled_mid = smb2_handle_cancelled_mid,
  1578. .downgrade_oplock = smb2_downgrade_oplock,
  1579. .need_neg = smb2_need_neg,
  1580. .negotiate = smb2_negotiate,
  1581. .negotiate_wsize = smb2_negotiate_wsize,
  1582. .negotiate_rsize = smb2_negotiate_rsize,
  1583. .sess_setup = SMB2_sess_setup,
  1584. .logoff = SMB2_logoff,
  1585. .tree_connect = SMB2_tcon,
  1586. .tree_disconnect = SMB2_tdis,
  1587. .qfs_tcon = smb3_qfs_tcon,
  1588. .is_path_accessible = smb2_is_path_accessible,
  1589. .can_echo = smb2_can_echo,
  1590. .echo = SMB2_echo,
  1591. .query_path_info = smb2_query_path_info,
  1592. .get_srv_inum = smb2_get_srv_inum,
  1593. .query_file_info = smb2_query_file_info,
  1594. .set_path_size = smb2_set_path_size,
  1595. .set_file_size = smb2_set_file_size,
  1596. .set_file_info = smb2_set_file_info,
  1597. .set_compression = smb2_set_compression,
  1598. .mkdir = smb2_mkdir,
  1599. .mkdir_setinfo = smb2_mkdir_setinfo,
  1600. .rmdir = smb2_rmdir,
  1601. .unlink = smb2_unlink,
  1602. .rename = smb2_rename_path,
  1603. .create_hardlink = smb2_create_hardlink,
  1604. .query_symlink = smb2_query_symlink,
  1605. .query_mf_symlink = smb3_query_mf_symlink,
  1606. .create_mf_symlink = smb3_create_mf_symlink,
  1607. .open = smb2_open_file,
  1608. .set_fid = smb2_set_fid,
  1609. .close = smb2_close_file,
  1610. .flush = smb2_flush_file,
  1611. .async_readv = smb2_async_readv,
  1612. .async_writev = smb2_async_writev,
  1613. .sync_read = smb2_sync_read,
  1614. .sync_write = smb2_sync_write,
  1615. .query_dir_first = smb2_query_dir_first,
  1616. .query_dir_next = smb2_query_dir_next,
  1617. .close_dir = smb2_close_dir,
  1618. .calc_smb_size = smb2_calc_size,
  1619. .is_status_pending = smb2_is_status_pending,
  1620. .is_session_expired = smb2_is_session_expired,
  1621. .oplock_response = smb2_oplock_response,
  1622. .queryfs = smb2_queryfs,
  1623. .mand_lock = smb2_mand_lock,
  1624. .mand_unlock_range = smb2_unlock_range,
  1625. .push_mand_locks = smb2_push_mandatory_locks,
  1626. .get_lease_key = smb2_get_lease_key,
  1627. .set_lease_key = smb2_set_lease_key,
  1628. .new_lease_key = smb2_new_lease_key,
  1629. .generate_signingkey = generate_smb3signingkey,
  1630. .calc_signature = smb3_calc_signature,
  1631. .set_integrity = smb3_set_integrity,
  1632. .is_read_op = smb21_is_read_op,
  1633. .set_oplock_level = smb3_set_oplock_level,
  1634. .create_lease_buf = smb3_create_lease_buf,
  1635. .parse_lease_buf = smb3_parse_lease_buf,
  1636. .clone_range = smb2_clone_range,
  1637. .duplicate_extents = smb2_duplicate_extents,
  1638. /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
  1639. .wp_retry_size = smb2_wp_retry_size,
  1640. .dir_needs_close = smb2_dir_needs_close,
  1641. .fallocate = smb3_fallocate,
  1642. };
  1643. #endif /* CIFS_SMB311 */
  1644. struct smb_version_values smb20_values = {
  1645. .version_string = SMB20_VERSION_STRING,
  1646. .protocol_id = SMB20_PROT_ID,
  1647. .req_capabilities = 0, /* MBZ */
  1648. .large_lock_type = 0,
  1649. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  1650. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  1651. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  1652. .header_size = sizeof(struct smb2_hdr),
  1653. .max_header_size = MAX_SMB2_HDR_SIZE,
  1654. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  1655. .lock_cmd = SMB2_LOCK,
  1656. .cap_unix = 0,
  1657. .cap_nt_find = SMB2_NT_FIND,
  1658. .cap_large_files = SMB2_LARGE_FILES,
  1659. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1660. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1661. .create_lease_size = sizeof(struct create_lease),
  1662. };
  1663. struct smb_version_values smb21_values = {
  1664. .version_string = SMB21_VERSION_STRING,
  1665. .protocol_id = SMB21_PROT_ID,
  1666. .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
  1667. .large_lock_type = 0,
  1668. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  1669. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  1670. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  1671. .header_size = sizeof(struct smb2_hdr),
  1672. .max_header_size = MAX_SMB2_HDR_SIZE,
  1673. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  1674. .lock_cmd = SMB2_LOCK,
  1675. .cap_unix = 0,
  1676. .cap_nt_find = SMB2_NT_FIND,
  1677. .cap_large_files = SMB2_LARGE_FILES,
  1678. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1679. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1680. .create_lease_size = sizeof(struct create_lease),
  1681. };
  1682. struct smb_version_values smb30_values = {
  1683. .version_string = SMB30_VERSION_STRING,
  1684. .protocol_id = SMB30_PROT_ID,
  1685. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES,
  1686. .large_lock_type = 0,
  1687. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  1688. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  1689. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  1690. .header_size = sizeof(struct smb2_hdr),
  1691. .max_header_size = MAX_SMB2_HDR_SIZE,
  1692. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  1693. .lock_cmd = SMB2_LOCK,
  1694. .cap_unix = 0,
  1695. .cap_nt_find = SMB2_NT_FIND,
  1696. .cap_large_files = SMB2_LARGE_FILES,
  1697. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1698. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1699. .create_lease_size = sizeof(struct create_lease_v2),
  1700. };
  1701. struct smb_version_values smb302_values = {
  1702. .version_string = SMB302_VERSION_STRING,
  1703. .protocol_id = SMB302_PROT_ID,
  1704. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES,
  1705. .large_lock_type = 0,
  1706. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  1707. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  1708. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  1709. .header_size = sizeof(struct smb2_hdr),
  1710. .max_header_size = MAX_SMB2_HDR_SIZE,
  1711. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  1712. .lock_cmd = SMB2_LOCK,
  1713. .cap_unix = 0,
  1714. .cap_nt_find = SMB2_NT_FIND,
  1715. .cap_large_files = SMB2_LARGE_FILES,
  1716. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1717. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1718. .create_lease_size = sizeof(struct create_lease_v2),
  1719. };
  1720. #ifdef CONFIG_CIFS_SMB311
  1721. struct smb_version_values smb311_values = {
  1722. .version_string = SMB311_VERSION_STRING,
  1723. .protocol_id = SMB311_PROT_ID,
  1724. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES,
  1725. .large_lock_type = 0,
  1726. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  1727. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  1728. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  1729. .header_size = sizeof(struct smb2_hdr),
  1730. .max_header_size = MAX_SMB2_HDR_SIZE,
  1731. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  1732. .lock_cmd = SMB2_LOCK,
  1733. .cap_unix = 0,
  1734. .cap_nt_find = SMB2_NT_FIND,
  1735. .cap_large_files = SMB2_LARGE_FILES,
  1736. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1737. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1738. .create_lease_size = sizeof(struct create_lease_v2),
  1739. };
  1740. #endif /* SMB311 */