smb2ops.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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 "cifsglob.h"
  22. #include "smb2pdu.h"
  23. #include "smb2proto.h"
  24. #include "cifsproto.h"
  25. #include "cifs_debug.h"
  26. #include "cifs_unicode.h"
  27. #include "smb2status.h"
  28. #include "smb2glob.h"
  29. static int
  30. change_conf(struct TCP_Server_Info *server)
  31. {
  32. server->credits += server->echo_credits + server->oplock_credits;
  33. server->oplock_credits = server->echo_credits = 0;
  34. switch (server->credits) {
  35. case 0:
  36. return -1;
  37. case 1:
  38. server->echoes = false;
  39. server->oplocks = false;
  40. cifs_dbg(VFS, "disabling echoes and oplocks\n");
  41. break;
  42. case 2:
  43. server->echoes = true;
  44. server->oplocks = false;
  45. server->echo_credits = 1;
  46. cifs_dbg(FYI, "disabling oplocks\n");
  47. break;
  48. default:
  49. server->echoes = true;
  50. server->oplocks = true;
  51. server->echo_credits = 1;
  52. server->oplock_credits = 1;
  53. }
  54. server->credits -= server->echo_credits + server->oplock_credits;
  55. return 0;
  56. }
  57. static void
  58. smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
  59. const int optype)
  60. {
  61. int *val, rc = 0;
  62. spin_lock(&server->req_lock);
  63. val = server->ops->get_credits_field(server, optype);
  64. *val += add;
  65. server->in_flight--;
  66. if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
  67. rc = change_conf(server);
  68. /*
  69. * Sometimes server returns 0 credits on oplock break ack - we need to
  70. * rebalance credits in this case.
  71. */
  72. else if (server->in_flight > 0 && server->oplock_credits == 0 &&
  73. server->oplocks) {
  74. if (server->credits > 1) {
  75. server->credits--;
  76. server->oplock_credits++;
  77. }
  78. }
  79. spin_unlock(&server->req_lock);
  80. wake_up(&server->request_q);
  81. if (rc)
  82. cifs_reconnect(server);
  83. }
  84. static void
  85. smb2_set_credits(struct TCP_Server_Info *server, const int val)
  86. {
  87. spin_lock(&server->req_lock);
  88. server->credits = val;
  89. spin_unlock(&server->req_lock);
  90. }
  91. static int *
  92. smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
  93. {
  94. switch (optype) {
  95. case CIFS_ECHO_OP:
  96. return &server->echo_credits;
  97. case CIFS_OBREAK_OP:
  98. return &server->oplock_credits;
  99. default:
  100. return &server->credits;
  101. }
  102. }
  103. static unsigned int
  104. smb2_get_credits(struct mid_q_entry *mid)
  105. {
  106. return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
  107. }
  108. static __u64
  109. smb2_get_next_mid(struct TCP_Server_Info *server)
  110. {
  111. __u64 mid;
  112. /* for SMB2 we need the current value */
  113. spin_lock(&GlobalMid_Lock);
  114. mid = server->CurrentMid++;
  115. spin_unlock(&GlobalMid_Lock);
  116. return mid;
  117. }
  118. static struct mid_q_entry *
  119. smb2_find_mid(struct TCP_Server_Info *server, char *buf)
  120. {
  121. struct mid_q_entry *mid;
  122. struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
  123. spin_lock(&GlobalMid_Lock);
  124. list_for_each_entry(mid, &server->pending_mid_q, qhead) {
  125. if ((mid->mid == hdr->MessageId) &&
  126. (mid->mid_state == MID_REQUEST_SUBMITTED) &&
  127. (mid->command == hdr->Command)) {
  128. spin_unlock(&GlobalMid_Lock);
  129. return mid;
  130. }
  131. }
  132. spin_unlock(&GlobalMid_Lock);
  133. return NULL;
  134. }
  135. static void
  136. smb2_dump_detail(void *buf)
  137. {
  138. #ifdef CONFIG_CIFS_DEBUG2
  139. struct smb2_hdr *smb = (struct smb2_hdr *)buf;
  140. cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
  141. smb->Command, smb->Status, smb->Flags, smb->MessageId,
  142. smb->ProcessId);
  143. cifs_dbg(VFS, "smb buf %p len %u\n", smb, smb2_calc_size(smb));
  144. #endif
  145. }
  146. static bool
  147. smb2_need_neg(struct TCP_Server_Info *server)
  148. {
  149. return server->max_read == 0;
  150. }
  151. static int
  152. smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
  153. {
  154. int rc;
  155. ses->server->CurrentMid = 0;
  156. rc = SMB2_negotiate(xid, ses);
  157. /* BB we probably don't need to retry with modern servers */
  158. if (rc == -EAGAIN)
  159. rc = -EHOSTDOWN;
  160. return rc;
  161. }
  162. static unsigned int
  163. smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  164. {
  165. struct TCP_Server_Info *server = tcon->ses->server;
  166. unsigned int wsize;
  167. /* start with specified wsize, or default */
  168. wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
  169. wsize = min_t(unsigned int, wsize, server->max_write);
  170. /* set it to the maximum buffer size value we can send with 1 credit */
  171. wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
  172. return wsize;
  173. }
  174. static unsigned int
  175. smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  176. {
  177. struct TCP_Server_Info *server = tcon->ses->server;
  178. unsigned int rsize;
  179. /* start with specified rsize, or default */
  180. rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
  181. rsize = min_t(unsigned int, rsize, server->max_read);
  182. /* set it to the maximum buffer size value we can send with 1 credit */
  183. rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
  184. return rsize;
  185. }
  186. #ifdef CONFIG_CIFS_STATS2
  187. static int
  188. SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
  189. {
  190. int rc;
  191. unsigned int ret_data_len = 0;
  192. struct network_interface_info_ioctl_rsp *out_buf;
  193. rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
  194. FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
  195. NULL /* no data input */, 0 /* no data input */,
  196. (char **)&out_buf, &ret_data_len);
  197. if ((rc == 0) && (ret_data_len > 0)) {
  198. /* Dump info on first interface */
  199. cifs_dbg(FYI, "Adapter Capability 0x%x\t",
  200. le32_to_cpu(out_buf->Capability));
  201. cifs_dbg(FYI, "Link Speed %lld\n",
  202. le64_to_cpu(out_buf->LinkSpeed));
  203. } else
  204. cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
  205. return rc;
  206. }
  207. #endif /* STATS2 */
  208. static void
  209. smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
  210. {
  211. int rc;
  212. __le16 srch_path = 0; /* Null - open root of share */
  213. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  214. struct cifs_open_parms oparms;
  215. struct cifs_fid fid;
  216. oparms.tcon = tcon;
  217. oparms.desired_access = FILE_READ_ATTRIBUTES;
  218. oparms.disposition = FILE_OPEN;
  219. oparms.create_options = 0;
  220. oparms.fid = &fid;
  221. oparms.reconnect = false;
  222. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
  223. if (rc)
  224. return;
  225. #ifdef CONFIG_CIFS_STATS2
  226. SMB3_request_interfaces(xid, tcon);
  227. #endif /* STATS2 */
  228. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  229. FS_ATTRIBUTE_INFORMATION);
  230. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  231. FS_DEVICE_INFORMATION);
  232. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  233. FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
  234. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  235. return;
  236. }
  237. static void
  238. smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
  239. {
  240. int rc;
  241. __le16 srch_path = 0; /* Null - open root of share */
  242. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  243. struct cifs_open_parms oparms;
  244. struct cifs_fid fid;
  245. oparms.tcon = tcon;
  246. oparms.desired_access = FILE_READ_ATTRIBUTES;
  247. oparms.disposition = FILE_OPEN;
  248. oparms.create_options = 0;
  249. oparms.fid = &fid;
  250. oparms.reconnect = false;
  251. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
  252. if (rc)
  253. return;
  254. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  255. FS_ATTRIBUTE_INFORMATION);
  256. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  257. FS_DEVICE_INFORMATION);
  258. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  259. return;
  260. }
  261. static int
  262. smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
  263. struct cifs_sb_info *cifs_sb, const char *full_path)
  264. {
  265. int rc;
  266. __le16 *utf16_path;
  267. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  268. struct cifs_open_parms oparms;
  269. struct cifs_fid fid;
  270. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  271. if (!utf16_path)
  272. return -ENOMEM;
  273. oparms.tcon = tcon;
  274. oparms.desired_access = FILE_READ_ATTRIBUTES;
  275. oparms.disposition = FILE_OPEN;
  276. oparms.create_options = 0;
  277. oparms.fid = &fid;
  278. oparms.reconnect = false;
  279. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
  280. if (rc) {
  281. kfree(utf16_path);
  282. return rc;
  283. }
  284. rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  285. kfree(utf16_path);
  286. return rc;
  287. }
  288. static int
  289. smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
  290. struct cifs_sb_info *cifs_sb, const char *full_path,
  291. u64 *uniqueid, FILE_ALL_INFO *data)
  292. {
  293. *uniqueid = le64_to_cpu(data->IndexNumber);
  294. return 0;
  295. }
  296. static int
  297. smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
  298. struct cifs_fid *fid, FILE_ALL_INFO *data)
  299. {
  300. int rc;
  301. struct smb2_file_all_info *smb2_data;
  302. smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  303. GFP_KERNEL);
  304. if (smb2_data == NULL)
  305. return -ENOMEM;
  306. rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
  307. smb2_data);
  308. if (!rc)
  309. move_smb2_info_to_cifs(data, smb2_data);
  310. kfree(smb2_data);
  311. return rc;
  312. }
  313. static bool
  314. smb2_can_echo(struct TCP_Server_Info *server)
  315. {
  316. return server->echoes;
  317. }
  318. static void
  319. smb2_clear_stats(struct cifs_tcon *tcon)
  320. {
  321. #ifdef CONFIG_CIFS_STATS
  322. int i;
  323. for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
  324. atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
  325. atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
  326. }
  327. #endif
  328. }
  329. static void
  330. smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
  331. {
  332. seq_puts(m, "\n\tShare Capabilities:");
  333. if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
  334. seq_puts(m, " DFS,");
  335. if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
  336. seq_puts(m, " CONTINUOUS AVAILABILITY,");
  337. if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
  338. seq_puts(m, " SCALEOUT,");
  339. if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
  340. seq_puts(m, " CLUSTER,");
  341. if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
  342. seq_puts(m, " ASYMMETRIC,");
  343. if (tcon->capabilities == 0)
  344. seq_puts(m, " None");
  345. if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
  346. seq_puts(m, " Aligned,");
  347. if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
  348. seq_puts(m, " Partition Aligned,");
  349. if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
  350. seq_puts(m, " SSD,");
  351. if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
  352. seq_puts(m, " TRIM-support,");
  353. seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
  354. if (tcon->perf_sector_size)
  355. seq_printf(m, "\tOptimal sector size: 0x%x",
  356. tcon->perf_sector_size);
  357. }
  358. static void
  359. smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
  360. {
  361. #ifdef CONFIG_CIFS_STATS
  362. atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
  363. atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
  364. seq_printf(m, "\nNegotiates: %d sent %d failed",
  365. atomic_read(&sent[SMB2_NEGOTIATE_HE]),
  366. atomic_read(&failed[SMB2_NEGOTIATE_HE]));
  367. seq_printf(m, "\nSessionSetups: %d sent %d failed",
  368. atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
  369. atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
  370. seq_printf(m, "\nLogoffs: %d sent %d failed",
  371. atomic_read(&sent[SMB2_LOGOFF_HE]),
  372. atomic_read(&failed[SMB2_LOGOFF_HE]));
  373. seq_printf(m, "\nTreeConnects: %d sent %d failed",
  374. atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
  375. atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
  376. seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
  377. atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
  378. atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
  379. seq_printf(m, "\nCreates: %d sent %d failed",
  380. atomic_read(&sent[SMB2_CREATE_HE]),
  381. atomic_read(&failed[SMB2_CREATE_HE]));
  382. seq_printf(m, "\nCloses: %d sent %d failed",
  383. atomic_read(&sent[SMB2_CLOSE_HE]),
  384. atomic_read(&failed[SMB2_CLOSE_HE]));
  385. seq_printf(m, "\nFlushes: %d sent %d failed",
  386. atomic_read(&sent[SMB2_FLUSH_HE]),
  387. atomic_read(&failed[SMB2_FLUSH_HE]));
  388. seq_printf(m, "\nReads: %d sent %d failed",
  389. atomic_read(&sent[SMB2_READ_HE]),
  390. atomic_read(&failed[SMB2_READ_HE]));
  391. seq_printf(m, "\nWrites: %d sent %d failed",
  392. atomic_read(&sent[SMB2_WRITE_HE]),
  393. atomic_read(&failed[SMB2_WRITE_HE]));
  394. seq_printf(m, "\nLocks: %d sent %d failed",
  395. atomic_read(&sent[SMB2_LOCK_HE]),
  396. atomic_read(&failed[SMB2_LOCK_HE]));
  397. seq_printf(m, "\nIOCTLs: %d sent %d failed",
  398. atomic_read(&sent[SMB2_IOCTL_HE]),
  399. atomic_read(&failed[SMB2_IOCTL_HE]));
  400. seq_printf(m, "\nCancels: %d sent %d failed",
  401. atomic_read(&sent[SMB2_CANCEL_HE]),
  402. atomic_read(&failed[SMB2_CANCEL_HE]));
  403. seq_printf(m, "\nEchos: %d sent %d failed",
  404. atomic_read(&sent[SMB2_ECHO_HE]),
  405. atomic_read(&failed[SMB2_ECHO_HE]));
  406. seq_printf(m, "\nQueryDirectories: %d sent %d failed",
  407. atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
  408. atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
  409. seq_printf(m, "\nChangeNotifies: %d sent %d failed",
  410. atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
  411. atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
  412. seq_printf(m, "\nQueryInfos: %d sent %d failed",
  413. atomic_read(&sent[SMB2_QUERY_INFO_HE]),
  414. atomic_read(&failed[SMB2_QUERY_INFO_HE]));
  415. seq_printf(m, "\nSetInfos: %d sent %d failed",
  416. atomic_read(&sent[SMB2_SET_INFO_HE]),
  417. atomic_read(&failed[SMB2_SET_INFO_HE]));
  418. seq_printf(m, "\nOplockBreaks: %d sent %d failed",
  419. atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
  420. atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
  421. #endif
  422. }
  423. static void
  424. smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
  425. {
  426. struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
  427. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  428. cfile->fid.persistent_fid = fid->persistent_fid;
  429. cfile->fid.volatile_fid = fid->volatile_fid;
  430. server->ops->set_oplock_level(cinode, oplock, fid->epoch,
  431. &fid->purge_cache);
  432. cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
  433. }
  434. static void
  435. smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
  436. struct cifs_fid *fid)
  437. {
  438. SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  439. }
  440. static int
  441. SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
  442. u64 persistent_fid, u64 volatile_fid,
  443. struct copychunk_ioctl *pcchunk)
  444. {
  445. int rc;
  446. unsigned int ret_data_len;
  447. struct resume_key_req *res_key;
  448. rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
  449. FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
  450. NULL, 0 /* no input */,
  451. (char **)&res_key, &ret_data_len);
  452. if (rc) {
  453. cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
  454. goto req_res_key_exit;
  455. }
  456. if (ret_data_len < sizeof(struct resume_key_req)) {
  457. cifs_dbg(VFS, "Invalid refcopy resume key length\n");
  458. rc = -EINVAL;
  459. goto req_res_key_exit;
  460. }
  461. memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
  462. req_res_key_exit:
  463. kfree(res_key);
  464. return rc;
  465. }
  466. static int
  467. smb2_clone_range(const unsigned int xid,
  468. struct cifsFileInfo *srcfile,
  469. struct cifsFileInfo *trgtfile, u64 src_off,
  470. u64 len, u64 dest_off)
  471. {
  472. int rc;
  473. unsigned int ret_data_len;
  474. struct copychunk_ioctl *pcchunk;
  475. struct copychunk_ioctl_rsp *retbuf = NULL;
  476. struct cifs_tcon *tcon;
  477. int chunks_copied = 0;
  478. bool chunk_sizes_updated = false;
  479. pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
  480. if (pcchunk == NULL)
  481. return -ENOMEM;
  482. cifs_dbg(FYI, "in smb2_clone_range - about to call request res key\n");
  483. /* Request a key from the server to identify the source of the copy */
  484. rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
  485. srcfile->fid.persistent_fid,
  486. srcfile->fid.volatile_fid, pcchunk);
  487. /* Note: request_res_key sets res_key null only if rc !=0 */
  488. if (rc)
  489. goto cchunk_out;
  490. /* For now array only one chunk long, will make more flexible later */
  491. pcchunk->ChunkCount = __constant_cpu_to_le32(1);
  492. pcchunk->Reserved = 0;
  493. pcchunk->Reserved2 = 0;
  494. tcon = tlink_tcon(trgtfile->tlink);
  495. while (len > 0) {
  496. pcchunk->SourceOffset = cpu_to_le64(src_off);
  497. pcchunk->TargetOffset = cpu_to_le64(dest_off);
  498. pcchunk->Length =
  499. cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
  500. /* Request server copy to target from src identified by key */
  501. rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
  502. trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
  503. true /* is_fsctl */, (char *)pcchunk,
  504. sizeof(struct copychunk_ioctl), (char **)&retbuf,
  505. &ret_data_len);
  506. if (rc == 0) {
  507. if (ret_data_len !=
  508. sizeof(struct copychunk_ioctl_rsp)) {
  509. cifs_dbg(VFS, "invalid cchunk response size\n");
  510. rc = -EIO;
  511. goto cchunk_out;
  512. }
  513. if (retbuf->TotalBytesWritten == 0) {
  514. cifs_dbg(FYI, "no bytes copied\n");
  515. rc = -EIO;
  516. goto cchunk_out;
  517. }
  518. /*
  519. * Check if server claimed to write more than we asked
  520. */
  521. if (le32_to_cpu(retbuf->TotalBytesWritten) >
  522. le32_to_cpu(pcchunk->Length)) {
  523. cifs_dbg(VFS, "invalid copy chunk response\n");
  524. rc = -EIO;
  525. goto cchunk_out;
  526. }
  527. if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
  528. cifs_dbg(VFS, "invalid num chunks written\n");
  529. rc = -EIO;
  530. goto cchunk_out;
  531. }
  532. chunks_copied++;
  533. src_off += le32_to_cpu(retbuf->TotalBytesWritten);
  534. dest_off += le32_to_cpu(retbuf->TotalBytesWritten);
  535. len -= le32_to_cpu(retbuf->TotalBytesWritten);
  536. cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %d\n",
  537. le32_to_cpu(retbuf->ChunksWritten),
  538. le32_to_cpu(retbuf->ChunkBytesWritten),
  539. le32_to_cpu(retbuf->TotalBytesWritten));
  540. } else if (rc == -EINVAL) {
  541. if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
  542. goto cchunk_out;
  543. cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
  544. le32_to_cpu(retbuf->ChunksWritten),
  545. le32_to_cpu(retbuf->ChunkBytesWritten),
  546. le32_to_cpu(retbuf->TotalBytesWritten));
  547. /*
  548. * Check if this is the first request using these sizes,
  549. * (ie check if copy succeed once with original sizes
  550. * and check if the server gave us different sizes after
  551. * we already updated max sizes on previous request).
  552. * if not then why is the server returning an error now
  553. */
  554. if ((chunks_copied != 0) || chunk_sizes_updated)
  555. goto cchunk_out;
  556. /* Check that server is not asking us to grow size */
  557. if (le32_to_cpu(retbuf->ChunkBytesWritten) <
  558. tcon->max_bytes_chunk)
  559. tcon->max_bytes_chunk =
  560. le32_to_cpu(retbuf->ChunkBytesWritten);
  561. else
  562. goto cchunk_out; /* server gave us bogus size */
  563. /* No need to change MaxChunks since already set to 1 */
  564. chunk_sizes_updated = true;
  565. }
  566. }
  567. cchunk_out:
  568. kfree(pcchunk);
  569. return rc;
  570. }
  571. static int
  572. smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
  573. struct cifs_fid *fid)
  574. {
  575. return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  576. }
  577. static unsigned int
  578. smb2_read_data_offset(char *buf)
  579. {
  580. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  581. return rsp->DataOffset;
  582. }
  583. static unsigned int
  584. smb2_read_data_length(char *buf)
  585. {
  586. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  587. return le32_to_cpu(rsp->DataLength);
  588. }
  589. static int
  590. smb2_sync_read(const unsigned int xid, struct cifsFileInfo *cfile,
  591. struct cifs_io_parms *parms, unsigned int *bytes_read,
  592. char **buf, int *buf_type)
  593. {
  594. parms->persistent_fid = cfile->fid.persistent_fid;
  595. parms->volatile_fid = cfile->fid.volatile_fid;
  596. return SMB2_read(xid, parms, bytes_read, buf, buf_type);
  597. }
  598. static int
  599. smb2_sync_write(const unsigned int xid, struct cifsFileInfo *cfile,
  600. struct cifs_io_parms *parms, unsigned int *written,
  601. struct kvec *iov, unsigned long nr_segs)
  602. {
  603. parms->persistent_fid = cfile->fid.persistent_fid;
  604. parms->volatile_fid = cfile->fid.volatile_fid;
  605. return SMB2_write(xid, parms, written, iov, nr_segs);
  606. }
  607. static int
  608. smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
  609. struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
  610. {
  611. __le64 eof = cpu_to_le64(size);
  612. return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
  613. cfile->fid.volatile_fid, cfile->pid, &eof);
  614. }
  615. static int
  616. smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
  617. struct cifsFileInfo *cfile)
  618. {
  619. return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
  620. cfile->fid.volatile_fid);
  621. }
  622. static int
  623. smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
  624. const char *path, struct cifs_sb_info *cifs_sb,
  625. struct cifs_fid *fid, __u16 search_flags,
  626. struct cifs_search_info *srch_inf)
  627. {
  628. __le16 *utf16_path;
  629. int rc;
  630. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  631. struct cifs_open_parms oparms;
  632. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  633. if (!utf16_path)
  634. return -ENOMEM;
  635. oparms.tcon = tcon;
  636. oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
  637. oparms.disposition = FILE_OPEN;
  638. oparms.create_options = 0;
  639. oparms.fid = fid;
  640. oparms.reconnect = false;
  641. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
  642. kfree(utf16_path);
  643. if (rc) {
  644. cifs_dbg(VFS, "open dir failed\n");
  645. return rc;
  646. }
  647. srch_inf->entries_in_buffer = 0;
  648. srch_inf->index_of_last_entry = 0;
  649. rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
  650. fid->volatile_fid, 0, srch_inf);
  651. if (rc) {
  652. cifs_dbg(VFS, "query directory failed\n");
  653. SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  654. }
  655. return rc;
  656. }
  657. static int
  658. smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
  659. struct cifs_fid *fid, __u16 search_flags,
  660. struct cifs_search_info *srch_inf)
  661. {
  662. return SMB2_query_directory(xid, tcon, fid->persistent_fid,
  663. fid->volatile_fid, 0, srch_inf);
  664. }
  665. static int
  666. smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
  667. struct cifs_fid *fid)
  668. {
  669. return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  670. }
  671. /*
  672. * If we negotiate SMB2 protocol and get STATUS_PENDING - update
  673. * the number of credits and return true. Otherwise - return false.
  674. */
  675. static bool
  676. smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
  677. {
  678. struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
  679. if (hdr->Status != STATUS_PENDING)
  680. return false;
  681. if (!length) {
  682. spin_lock(&server->req_lock);
  683. server->credits += le16_to_cpu(hdr->CreditRequest);
  684. spin_unlock(&server->req_lock);
  685. wake_up(&server->request_q);
  686. }
  687. return true;
  688. }
  689. static int
  690. smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
  691. struct cifsInodeInfo *cinode)
  692. {
  693. if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
  694. return SMB2_lease_break(0, tcon, cinode->lease_key,
  695. smb2_get_lease_state(cinode));
  696. return SMB2_oplock_break(0, tcon, fid->persistent_fid,
  697. fid->volatile_fid,
  698. CIFS_CACHE_READ(cinode) ? 1 : 0);
  699. }
  700. static int
  701. smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
  702. struct kstatfs *buf)
  703. {
  704. int rc;
  705. __le16 srch_path = 0; /* Null - open root of share */
  706. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  707. struct cifs_open_parms oparms;
  708. struct cifs_fid fid;
  709. oparms.tcon = tcon;
  710. oparms.desired_access = FILE_READ_ATTRIBUTES;
  711. oparms.disposition = FILE_OPEN;
  712. oparms.create_options = 0;
  713. oparms.fid = &fid;
  714. oparms.reconnect = false;
  715. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
  716. if (rc)
  717. return rc;
  718. buf->f_type = SMB2_MAGIC_NUMBER;
  719. rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  720. buf);
  721. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  722. return rc;
  723. }
  724. static bool
  725. smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
  726. {
  727. return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
  728. ob1->fid.volatile_fid == ob2->fid.volatile_fid;
  729. }
  730. static int
  731. smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
  732. __u64 length, __u32 type, int lock, int unlock, bool wait)
  733. {
  734. if (unlock && !lock)
  735. type = SMB2_LOCKFLAG_UNLOCK;
  736. return SMB2_lock(xid, tlink_tcon(cfile->tlink),
  737. cfile->fid.persistent_fid, cfile->fid.volatile_fid,
  738. current->tgid, length, offset, type, wait);
  739. }
  740. static void
  741. smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
  742. {
  743. memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
  744. }
  745. static void
  746. smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
  747. {
  748. memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
  749. }
  750. static void
  751. smb2_new_lease_key(struct cifs_fid *fid)
  752. {
  753. get_random_bytes(fid->lease_key, SMB2_LEASE_KEY_SIZE);
  754. }
  755. static int
  756. smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
  757. const char *full_path, char **target_path,
  758. struct cifs_sb_info *cifs_sb)
  759. {
  760. int rc;
  761. __le16 *utf16_path;
  762. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  763. struct cifs_open_parms oparms;
  764. struct cifs_fid fid;
  765. struct smb2_err_rsp *err_buf = NULL;
  766. struct smb2_symlink_err_rsp *symlink;
  767. unsigned int sub_len, sub_offset;
  768. cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
  769. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  770. if (!utf16_path)
  771. return -ENOMEM;
  772. oparms.tcon = tcon;
  773. oparms.desired_access = FILE_READ_ATTRIBUTES;
  774. oparms.disposition = FILE_OPEN;
  775. oparms.create_options = 0;
  776. oparms.fid = &fid;
  777. oparms.reconnect = false;
  778. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
  779. if (!rc || !err_buf) {
  780. kfree(utf16_path);
  781. return -ENOENT;
  782. }
  783. /* open must fail on symlink - reset rc */
  784. rc = 0;
  785. symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
  786. sub_len = le16_to_cpu(symlink->SubstituteNameLength);
  787. sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
  788. *target_path = cifs_strndup_from_utf16(
  789. (char *)symlink->PathBuffer + sub_offset,
  790. sub_len, true, cifs_sb->local_nls);
  791. if (!(*target_path)) {
  792. kfree(utf16_path);
  793. return -ENOMEM;
  794. }
  795. convert_delimiter(*target_path, '/');
  796. cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
  797. kfree(utf16_path);
  798. return rc;
  799. }
  800. static void
  801. smb2_downgrade_oplock(struct TCP_Server_Info *server,
  802. struct cifsInodeInfo *cinode, bool set_level2)
  803. {
  804. if (set_level2)
  805. server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
  806. 0, NULL);
  807. else
  808. server->ops->set_oplock_level(cinode, 0, 0, NULL);
  809. }
  810. static void
  811. smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  812. unsigned int epoch, bool *purge_cache)
  813. {
  814. oplock &= 0xFF;
  815. if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
  816. return;
  817. if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
  818. cinode->oplock = CIFS_CACHE_RHW_FLG;
  819. cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
  820. &cinode->vfs_inode);
  821. } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
  822. cinode->oplock = CIFS_CACHE_RW_FLG;
  823. cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
  824. &cinode->vfs_inode);
  825. } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
  826. cinode->oplock = CIFS_CACHE_READ_FLG;
  827. cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
  828. &cinode->vfs_inode);
  829. } else
  830. cinode->oplock = 0;
  831. }
  832. static void
  833. smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  834. unsigned int epoch, bool *purge_cache)
  835. {
  836. char message[5] = {0};
  837. oplock &= 0xFF;
  838. if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
  839. return;
  840. cinode->oplock = 0;
  841. if (oplock & SMB2_LEASE_READ_CACHING_HE) {
  842. cinode->oplock |= CIFS_CACHE_READ_FLG;
  843. strcat(message, "R");
  844. }
  845. if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
  846. cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
  847. strcat(message, "H");
  848. }
  849. if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
  850. cinode->oplock |= CIFS_CACHE_WRITE_FLG;
  851. strcat(message, "W");
  852. }
  853. if (!cinode->oplock)
  854. strcat(message, "None");
  855. cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
  856. &cinode->vfs_inode);
  857. }
  858. static void
  859. smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  860. unsigned int epoch, bool *purge_cache)
  861. {
  862. unsigned int old_oplock = cinode->oplock;
  863. smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
  864. if (purge_cache) {
  865. *purge_cache = false;
  866. if (old_oplock == CIFS_CACHE_READ_FLG) {
  867. if (cinode->oplock == CIFS_CACHE_READ_FLG &&
  868. (epoch - cinode->epoch > 0))
  869. *purge_cache = true;
  870. else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
  871. (epoch - cinode->epoch > 1))
  872. *purge_cache = true;
  873. else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
  874. (epoch - cinode->epoch > 1))
  875. *purge_cache = true;
  876. else if (cinode->oplock == 0 &&
  877. (epoch - cinode->epoch > 0))
  878. *purge_cache = true;
  879. } else if (old_oplock == CIFS_CACHE_RH_FLG) {
  880. if (cinode->oplock == CIFS_CACHE_RH_FLG &&
  881. (epoch - cinode->epoch > 0))
  882. *purge_cache = true;
  883. else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
  884. (epoch - cinode->epoch > 1))
  885. *purge_cache = true;
  886. }
  887. cinode->epoch = epoch;
  888. }
  889. }
  890. static bool
  891. smb2_is_read_op(__u32 oplock)
  892. {
  893. return oplock == SMB2_OPLOCK_LEVEL_II;
  894. }
  895. static bool
  896. smb21_is_read_op(__u32 oplock)
  897. {
  898. return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
  899. !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
  900. }
  901. static __le32
  902. map_oplock_to_lease(u8 oplock)
  903. {
  904. if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
  905. return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
  906. else if (oplock == SMB2_OPLOCK_LEVEL_II)
  907. return SMB2_LEASE_READ_CACHING;
  908. else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
  909. return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
  910. SMB2_LEASE_WRITE_CACHING;
  911. return 0;
  912. }
  913. static char *
  914. smb2_create_lease_buf(u8 *lease_key, u8 oplock)
  915. {
  916. struct create_lease *buf;
  917. buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
  918. if (!buf)
  919. return NULL;
  920. buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
  921. buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
  922. buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
  923. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  924. (struct create_lease, lcontext));
  925. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
  926. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  927. (struct create_lease, Name));
  928. buf->ccontext.NameLength = cpu_to_le16(4);
  929. /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
  930. buf->Name[0] = 'R';
  931. buf->Name[1] = 'q';
  932. buf->Name[2] = 'L';
  933. buf->Name[3] = 's';
  934. return (char *)buf;
  935. }
  936. static char *
  937. smb3_create_lease_buf(u8 *lease_key, u8 oplock)
  938. {
  939. struct create_lease_v2 *buf;
  940. buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
  941. if (!buf)
  942. return NULL;
  943. buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
  944. buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
  945. buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
  946. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  947. (struct create_lease_v2, lcontext));
  948. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
  949. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  950. (struct create_lease_v2, Name));
  951. buf->ccontext.NameLength = cpu_to_le16(4);
  952. /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
  953. buf->Name[0] = 'R';
  954. buf->Name[1] = 'q';
  955. buf->Name[2] = 'L';
  956. buf->Name[3] = 's';
  957. return (char *)buf;
  958. }
  959. static __u8
  960. smb2_parse_lease_buf(void *buf, unsigned int *epoch)
  961. {
  962. struct create_lease *lc = (struct create_lease *)buf;
  963. *epoch = 0; /* not used */
  964. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
  965. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  966. return le32_to_cpu(lc->lcontext.LeaseState);
  967. }
  968. static __u8
  969. smb3_parse_lease_buf(void *buf, unsigned int *epoch)
  970. {
  971. struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
  972. *epoch = le16_to_cpu(lc->lcontext.Epoch);
  973. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
  974. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  975. return le32_to_cpu(lc->lcontext.LeaseState);
  976. }
  977. struct smb_version_operations smb20_operations = {
  978. .compare_fids = smb2_compare_fids,
  979. .setup_request = smb2_setup_request,
  980. .setup_async_request = smb2_setup_async_request,
  981. .check_receive = smb2_check_receive,
  982. .add_credits = smb2_add_credits,
  983. .set_credits = smb2_set_credits,
  984. .get_credits_field = smb2_get_credits_field,
  985. .get_credits = smb2_get_credits,
  986. .get_next_mid = smb2_get_next_mid,
  987. .read_data_offset = smb2_read_data_offset,
  988. .read_data_length = smb2_read_data_length,
  989. .map_error = map_smb2_to_linux_error,
  990. .find_mid = smb2_find_mid,
  991. .check_message = smb2_check_message,
  992. .dump_detail = smb2_dump_detail,
  993. .clear_stats = smb2_clear_stats,
  994. .print_stats = smb2_print_stats,
  995. .is_oplock_break = smb2_is_valid_oplock_break,
  996. .downgrade_oplock = smb2_downgrade_oplock,
  997. .need_neg = smb2_need_neg,
  998. .negotiate = smb2_negotiate,
  999. .negotiate_wsize = smb2_negotiate_wsize,
  1000. .negotiate_rsize = smb2_negotiate_rsize,
  1001. .sess_setup = SMB2_sess_setup,
  1002. .logoff = SMB2_logoff,
  1003. .tree_connect = SMB2_tcon,
  1004. .tree_disconnect = SMB2_tdis,
  1005. .qfs_tcon = smb2_qfs_tcon,
  1006. .is_path_accessible = smb2_is_path_accessible,
  1007. .can_echo = smb2_can_echo,
  1008. .echo = SMB2_echo,
  1009. .query_path_info = smb2_query_path_info,
  1010. .get_srv_inum = smb2_get_srv_inum,
  1011. .query_file_info = smb2_query_file_info,
  1012. .set_path_size = smb2_set_path_size,
  1013. .set_file_size = smb2_set_file_size,
  1014. .set_file_info = smb2_set_file_info,
  1015. .set_compression = smb2_set_compression,
  1016. .mkdir = smb2_mkdir,
  1017. .mkdir_setinfo = smb2_mkdir_setinfo,
  1018. .rmdir = smb2_rmdir,
  1019. .unlink = smb2_unlink,
  1020. .rename = smb2_rename_path,
  1021. .create_hardlink = smb2_create_hardlink,
  1022. .query_symlink = smb2_query_symlink,
  1023. .open = smb2_open_file,
  1024. .set_fid = smb2_set_fid,
  1025. .close = smb2_close_file,
  1026. .flush = smb2_flush_file,
  1027. .async_readv = smb2_async_readv,
  1028. .async_writev = smb2_async_writev,
  1029. .sync_read = smb2_sync_read,
  1030. .sync_write = smb2_sync_write,
  1031. .query_dir_first = smb2_query_dir_first,
  1032. .query_dir_next = smb2_query_dir_next,
  1033. .close_dir = smb2_close_dir,
  1034. .calc_smb_size = smb2_calc_size,
  1035. .is_status_pending = smb2_is_status_pending,
  1036. .oplock_response = smb2_oplock_response,
  1037. .queryfs = smb2_queryfs,
  1038. .mand_lock = smb2_mand_lock,
  1039. .mand_unlock_range = smb2_unlock_range,
  1040. .push_mand_locks = smb2_push_mandatory_locks,
  1041. .get_lease_key = smb2_get_lease_key,
  1042. .set_lease_key = smb2_set_lease_key,
  1043. .new_lease_key = smb2_new_lease_key,
  1044. .calc_signature = smb2_calc_signature,
  1045. .is_read_op = smb2_is_read_op,
  1046. .set_oplock_level = smb2_set_oplock_level,
  1047. .create_lease_buf = smb2_create_lease_buf,
  1048. .parse_lease_buf = smb2_parse_lease_buf,
  1049. .clone_range = smb2_clone_range,
  1050. };
  1051. struct smb_version_operations smb21_operations = {
  1052. .compare_fids = smb2_compare_fids,
  1053. .setup_request = smb2_setup_request,
  1054. .setup_async_request = smb2_setup_async_request,
  1055. .check_receive = smb2_check_receive,
  1056. .add_credits = smb2_add_credits,
  1057. .set_credits = smb2_set_credits,
  1058. .get_credits_field = smb2_get_credits_field,
  1059. .get_credits = smb2_get_credits,
  1060. .get_next_mid = smb2_get_next_mid,
  1061. .read_data_offset = smb2_read_data_offset,
  1062. .read_data_length = smb2_read_data_length,
  1063. .map_error = map_smb2_to_linux_error,
  1064. .find_mid = smb2_find_mid,
  1065. .check_message = smb2_check_message,
  1066. .dump_detail = smb2_dump_detail,
  1067. .clear_stats = smb2_clear_stats,
  1068. .print_stats = smb2_print_stats,
  1069. .is_oplock_break = smb2_is_valid_oplock_break,
  1070. .downgrade_oplock = smb2_downgrade_oplock,
  1071. .need_neg = smb2_need_neg,
  1072. .negotiate = smb2_negotiate,
  1073. .negotiate_wsize = smb2_negotiate_wsize,
  1074. .negotiate_rsize = smb2_negotiate_rsize,
  1075. .sess_setup = SMB2_sess_setup,
  1076. .logoff = SMB2_logoff,
  1077. .tree_connect = SMB2_tcon,
  1078. .tree_disconnect = SMB2_tdis,
  1079. .qfs_tcon = smb2_qfs_tcon,
  1080. .is_path_accessible = smb2_is_path_accessible,
  1081. .can_echo = smb2_can_echo,
  1082. .echo = SMB2_echo,
  1083. .query_path_info = smb2_query_path_info,
  1084. .get_srv_inum = smb2_get_srv_inum,
  1085. .query_file_info = smb2_query_file_info,
  1086. .set_path_size = smb2_set_path_size,
  1087. .set_file_size = smb2_set_file_size,
  1088. .set_file_info = smb2_set_file_info,
  1089. .set_compression = smb2_set_compression,
  1090. .mkdir = smb2_mkdir,
  1091. .mkdir_setinfo = smb2_mkdir_setinfo,
  1092. .rmdir = smb2_rmdir,
  1093. .unlink = smb2_unlink,
  1094. .rename = smb2_rename_path,
  1095. .create_hardlink = smb2_create_hardlink,
  1096. .query_symlink = smb2_query_symlink,
  1097. .open = smb2_open_file,
  1098. .set_fid = smb2_set_fid,
  1099. .close = smb2_close_file,
  1100. .flush = smb2_flush_file,
  1101. .async_readv = smb2_async_readv,
  1102. .async_writev = smb2_async_writev,
  1103. .sync_read = smb2_sync_read,
  1104. .sync_write = smb2_sync_write,
  1105. .query_dir_first = smb2_query_dir_first,
  1106. .query_dir_next = smb2_query_dir_next,
  1107. .close_dir = smb2_close_dir,
  1108. .calc_smb_size = smb2_calc_size,
  1109. .is_status_pending = smb2_is_status_pending,
  1110. .oplock_response = smb2_oplock_response,
  1111. .queryfs = smb2_queryfs,
  1112. .mand_lock = smb2_mand_lock,
  1113. .mand_unlock_range = smb2_unlock_range,
  1114. .push_mand_locks = smb2_push_mandatory_locks,
  1115. .get_lease_key = smb2_get_lease_key,
  1116. .set_lease_key = smb2_set_lease_key,
  1117. .new_lease_key = smb2_new_lease_key,
  1118. .calc_signature = smb2_calc_signature,
  1119. .is_read_op = smb21_is_read_op,
  1120. .set_oplock_level = smb21_set_oplock_level,
  1121. .create_lease_buf = smb2_create_lease_buf,
  1122. .parse_lease_buf = smb2_parse_lease_buf,
  1123. .clone_range = smb2_clone_range,
  1124. };
  1125. struct smb_version_operations smb30_operations = {
  1126. .compare_fids = smb2_compare_fids,
  1127. .setup_request = smb2_setup_request,
  1128. .setup_async_request = smb2_setup_async_request,
  1129. .check_receive = smb2_check_receive,
  1130. .add_credits = smb2_add_credits,
  1131. .set_credits = smb2_set_credits,
  1132. .get_credits_field = smb2_get_credits_field,
  1133. .get_credits = smb2_get_credits,
  1134. .get_next_mid = smb2_get_next_mid,
  1135. .read_data_offset = smb2_read_data_offset,
  1136. .read_data_length = smb2_read_data_length,
  1137. .map_error = map_smb2_to_linux_error,
  1138. .find_mid = smb2_find_mid,
  1139. .check_message = smb2_check_message,
  1140. .dump_detail = smb2_dump_detail,
  1141. .clear_stats = smb2_clear_stats,
  1142. .print_stats = smb2_print_stats,
  1143. .dump_share_caps = smb2_dump_share_caps,
  1144. .is_oplock_break = smb2_is_valid_oplock_break,
  1145. .downgrade_oplock = smb2_downgrade_oplock,
  1146. .need_neg = smb2_need_neg,
  1147. .negotiate = smb2_negotiate,
  1148. .negotiate_wsize = smb2_negotiate_wsize,
  1149. .negotiate_rsize = smb2_negotiate_rsize,
  1150. .sess_setup = SMB2_sess_setup,
  1151. .logoff = SMB2_logoff,
  1152. .tree_connect = SMB2_tcon,
  1153. .tree_disconnect = SMB2_tdis,
  1154. .qfs_tcon = smb3_qfs_tcon,
  1155. .is_path_accessible = smb2_is_path_accessible,
  1156. .can_echo = smb2_can_echo,
  1157. .echo = SMB2_echo,
  1158. .query_path_info = smb2_query_path_info,
  1159. .get_srv_inum = smb2_get_srv_inum,
  1160. .query_file_info = smb2_query_file_info,
  1161. .set_path_size = smb2_set_path_size,
  1162. .set_file_size = smb2_set_file_size,
  1163. .set_file_info = smb2_set_file_info,
  1164. .set_compression = smb2_set_compression,
  1165. .mkdir = smb2_mkdir,
  1166. .mkdir_setinfo = smb2_mkdir_setinfo,
  1167. .rmdir = smb2_rmdir,
  1168. .unlink = smb2_unlink,
  1169. .rename = smb2_rename_path,
  1170. .create_hardlink = smb2_create_hardlink,
  1171. .query_symlink = smb2_query_symlink,
  1172. .open = smb2_open_file,
  1173. .set_fid = smb2_set_fid,
  1174. .close = smb2_close_file,
  1175. .flush = smb2_flush_file,
  1176. .async_readv = smb2_async_readv,
  1177. .async_writev = smb2_async_writev,
  1178. .sync_read = smb2_sync_read,
  1179. .sync_write = smb2_sync_write,
  1180. .query_dir_first = smb2_query_dir_first,
  1181. .query_dir_next = smb2_query_dir_next,
  1182. .close_dir = smb2_close_dir,
  1183. .calc_smb_size = smb2_calc_size,
  1184. .is_status_pending = smb2_is_status_pending,
  1185. .oplock_response = smb2_oplock_response,
  1186. .queryfs = smb2_queryfs,
  1187. .mand_lock = smb2_mand_lock,
  1188. .mand_unlock_range = smb2_unlock_range,
  1189. .push_mand_locks = smb2_push_mandatory_locks,
  1190. .get_lease_key = smb2_get_lease_key,
  1191. .set_lease_key = smb2_set_lease_key,
  1192. .new_lease_key = smb2_new_lease_key,
  1193. .generate_signingkey = generate_smb3signingkey,
  1194. .calc_signature = smb3_calc_signature,
  1195. .is_read_op = smb21_is_read_op,
  1196. .set_oplock_level = smb3_set_oplock_level,
  1197. .create_lease_buf = smb3_create_lease_buf,
  1198. .parse_lease_buf = smb3_parse_lease_buf,
  1199. .clone_range = smb2_clone_range,
  1200. .validate_negotiate = smb3_validate_negotiate,
  1201. };
  1202. struct smb_version_values smb20_values = {
  1203. .version_string = SMB20_VERSION_STRING,
  1204. .protocol_id = SMB20_PROT_ID,
  1205. .req_capabilities = 0, /* MBZ */
  1206. .large_lock_type = 0,
  1207. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  1208. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  1209. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  1210. .header_size = sizeof(struct smb2_hdr),
  1211. .max_header_size = MAX_SMB2_HDR_SIZE,
  1212. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  1213. .lock_cmd = SMB2_LOCK,
  1214. .cap_unix = 0,
  1215. .cap_nt_find = SMB2_NT_FIND,
  1216. .cap_large_files = SMB2_LARGE_FILES,
  1217. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1218. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1219. .create_lease_size = sizeof(struct create_lease),
  1220. };
  1221. struct smb_version_values smb21_values = {
  1222. .version_string = SMB21_VERSION_STRING,
  1223. .protocol_id = SMB21_PROT_ID,
  1224. .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
  1225. .large_lock_type = 0,
  1226. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  1227. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  1228. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  1229. .header_size = sizeof(struct smb2_hdr),
  1230. .max_header_size = MAX_SMB2_HDR_SIZE,
  1231. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  1232. .lock_cmd = SMB2_LOCK,
  1233. .cap_unix = 0,
  1234. .cap_nt_find = SMB2_NT_FIND,
  1235. .cap_large_files = SMB2_LARGE_FILES,
  1236. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1237. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1238. .create_lease_size = sizeof(struct create_lease),
  1239. };
  1240. struct smb_version_values smb30_values = {
  1241. .version_string = SMB30_VERSION_STRING,
  1242. .protocol_id = SMB30_PROT_ID,
  1243. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
  1244. .large_lock_type = 0,
  1245. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  1246. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  1247. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  1248. .header_size = sizeof(struct smb2_hdr),
  1249. .max_header_size = MAX_SMB2_HDR_SIZE,
  1250. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  1251. .lock_cmd = SMB2_LOCK,
  1252. .cap_unix = 0,
  1253. .cap_nt_find = SMB2_NT_FIND,
  1254. .cap_large_files = SMB2_LARGE_FILES,
  1255. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1256. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1257. .create_lease_size = sizeof(struct create_lease_v2),
  1258. };
  1259. struct smb_version_values smb302_values = {
  1260. .version_string = SMB302_VERSION_STRING,
  1261. .protocol_id = SMB302_PROT_ID,
  1262. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
  1263. .large_lock_type = 0,
  1264. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  1265. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  1266. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  1267. .header_size = sizeof(struct smb2_hdr),
  1268. .max_header_size = MAX_SMB2_HDR_SIZE,
  1269. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  1270. .lock_cmd = SMB2_LOCK,
  1271. .cap_unix = 0,
  1272. .cap_nt_find = SMB2_NT_FIND,
  1273. .cap_large_files = SMB2_LARGE_FILES,
  1274. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1275. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  1276. .create_lease_size = sizeof(struct create_lease_v2),
  1277. };