smb2ops.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. /*
  171. * limit write size to 2 ** 16, because we don't support multicredit
  172. * requests now.
  173. */
  174. wsize = min_t(unsigned int, wsize, 2 << 15);
  175. return wsize;
  176. }
  177. static unsigned int
  178. smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  179. {
  180. struct TCP_Server_Info *server = tcon->ses->server;
  181. unsigned int rsize;
  182. /* start with specified rsize, or default */
  183. rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
  184. rsize = min_t(unsigned int, rsize, server->max_read);
  185. /*
  186. * limit write size to 2 ** 16, because we don't support multicredit
  187. * requests now.
  188. */
  189. rsize = min_t(unsigned int, rsize, 2 << 15);
  190. return rsize;
  191. }
  192. static int
  193. smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
  194. struct cifs_sb_info *cifs_sb, const char *full_path)
  195. {
  196. int rc;
  197. __le16 *utf16_path;
  198. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  199. struct cifs_open_parms oparms;
  200. struct cifs_fid fid;
  201. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  202. if (!utf16_path)
  203. return -ENOMEM;
  204. oparms.tcon = tcon;
  205. oparms.desired_access = FILE_READ_ATTRIBUTES;
  206. oparms.disposition = FILE_OPEN;
  207. oparms.create_options = 0;
  208. oparms.fid = &fid;
  209. oparms.reconnect = false;
  210. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
  211. if (rc) {
  212. kfree(utf16_path);
  213. return rc;
  214. }
  215. rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  216. kfree(utf16_path);
  217. return rc;
  218. }
  219. static int
  220. smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
  221. struct cifs_sb_info *cifs_sb, const char *full_path,
  222. u64 *uniqueid, FILE_ALL_INFO *data)
  223. {
  224. *uniqueid = le64_to_cpu(data->IndexNumber);
  225. return 0;
  226. }
  227. static int
  228. smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
  229. struct cifs_fid *fid, FILE_ALL_INFO *data)
  230. {
  231. int rc;
  232. struct smb2_file_all_info *smb2_data;
  233. smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  234. GFP_KERNEL);
  235. if (smb2_data == NULL)
  236. return -ENOMEM;
  237. rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
  238. smb2_data);
  239. if (!rc)
  240. move_smb2_info_to_cifs(data, smb2_data);
  241. kfree(smb2_data);
  242. return rc;
  243. }
  244. static bool
  245. smb2_can_echo(struct TCP_Server_Info *server)
  246. {
  247. return server->echoes;
  248. }
  249. static void
  250. smb2_clear_stats(struct cifs_tcon *tcon)
  251. {
  252. #ifdef CONFIG_CIFS_STATS
  253. int i;
  254. for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
  255. atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
  256. atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
  257. }
  258. #endif
  259. }
  260. static void
  261. smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
  262. {
  263. seq_puts(m, "\n\tShare Capabilities:");
  264. if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
  265. seq_puts(m, " DFS,");
  266. if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
  267. seq_puts(m, " CONTINUOUS AVAILABILITY,");
  268. if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
  269. seq_puts(m, " SCALEOUT,");
  270. if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
  271. seq_puts(m, " CLUSTER,");
  272. if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
  273. seq_puts(m, " ASYMMETRIC,");
  274. if (tcon->capabilities == 0)
  275. seq_puts(m, " None");
  276. seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
  277. }
  278. static void
  279. smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
  280. {
  281. #ifdef CONFIG_CIFS_STATS
  282. atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
  283. atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
  284. seq_printf(m, "\nNegotiates: %d sent %d failed",
  285. atomic_read(&sent[SMB2_NEGOTIATE_HE]),
  286. atomic_read(&failed[SMB2_NEGOTIATE_HE]));
  287. seq_printf(m, "\nSessionSetups: %d sent %d failed",
  288. atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
  289. atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
  290. seq_printf(m, "\nLogoffs: %d sent %d failed",
  291. atomic_read(&sent[SMB2_LOGOFF_HE]),
  292. atomic_read(&failed[SMB2_LOGOFF_HE]));
  293. seq_printf(m, "\nTreeConnects: %d sent %d failed",
  294. atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
  295. atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
  296. seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
  297. atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
  298. atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
  299. seq_printf(m, "\nCreates: %d sent %d failed",
  300. atomic_read(&sent[SMB2_CREATE_HE]),
  301. atomic_read(&failed[SMB2_CREATE_HE]));
  302. seq_printf(m, "\nCloses: %d sent %d failed",
  303. atomic_read(&sent[SMB2_CLOSE_HE]),
  304. atomic_read(&failed[SMB2_CLOSE_HE]));
  305. seq_printf(m, "\nFlushes: %d sent %d failed",
  306. atomic_read(&sent[SMB2_FLUSH_HE]),
  307. atomic_read(&failed[SMB2_FLUSH_HE]));
  308. seq_printf(m, "\nReads: %d sent %d failed",
  309. atomic_read(&sent[SMB2_READ_HE]),
  310. atomic_read(&failed[SMB2_READ_HE]));
  311. seq_printf(m, "\nWrites: %d sent %d failed",
  312. atomic_read(&sent[SMB2_WRITE_HE]),
  313. atomic_read(&failed[SMB2_WRITE_HE]));
  314. seq_printf(m, "\nLocks: %d sent %d failed",
  315. atomic_read(&sent[SMB2_LOCK_HE]),
  316. atomic_read(&failed[SMB2_LOCK_HE]));
  317. seq_printf(m, "\nIOCTLs: %d sent %d failed",
  318. atomic_read(&sent[SMB2_IOCTL_HE]),
  319. atomic_read(&failed[SMB2_IOCTL_HE]));
  320. seq_printf(m, "\nCancels: %d sent %d failed",
  321. atomic_read(&sent[SMB2_CANCEL_HE]),
  322. atomic_read(&failed[SMB2_CANCEL_HE]));
  323. seq_printf(m, "\nEchos: %d sent %d failed",
  324. atomic_read(&sent[SMB2_ECHO_HE]),
  325. atomic_read(&failed[SMB2_ECHO_HE]));
  326. seq_printf(m, "\nQueryDirectories: %d sent %d failed",
  327. atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
  328. atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
  329. seq_printf(m, "\nChangeNotifies: %d sent %d failed",
  330. atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
  331. atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
  332. seq_printf(m, "\nQueryInfos: %d sent %d failed",
  333. atomic_read(&sent[SMB2_QUERY_INFO_HE]),
  334. atomic_read(&failed[SMB2_QUERY_INFO_HE]));
  335. seq_printf(m, "\nSetInfos: %d sent %d failed",
  336. atomic_read(&sent[SMB2_SET_INFO_HE]),
  337. atomic_read(&failed[SMB2_SET_INFO_HE]));
  338. seq_printf(m, "\nOplockBreaks: %d sent %d failed",
  339. atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
  340. atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
  341. #endif
  342. }
  343. static void
  344. smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
  345. {
  346. struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
  347. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  348. cfile->fid.persistent_fid = fid->persistent_fid;
  349. cfile->fid.volatile_fid = fid->volatile_fid;
  350. server->ops->set_oplock_level(cinode, oplock);
  351. cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
  352. }
  353. static void
  354. smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
  355. struct cifs_fid *fid)
  356. {
  357. SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  358. }
  359. static int
  360. smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
  361. struct cifs_fid *fid)
  362. {
  363. return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  364. }
  365. static unsigned int
  366. smb2_read_data_offset(char *buf)
  367. {
  368. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  369. return rsp->DataOffset;
  370. }
  371. static unsigned int
  372. smb2_read_data_length(char *buf)
  373. {
  374. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  375. return le32_to_cpu(rsp->DataLength);
  376. }
  377. static int
  378. smb2_sync_read(const unsigned int xid, struct cifsFileInfo *cfile,
  379. struct cifs_io_parms *parms, unsigned int *bytes_read,
  380. char **buf, int *buf_type)
  381. {
  382. parms->persistent_fid = cfile->fid.persistent_fid;
  383. parms->volatile_fid = cfile->fid.volatile_fid;
  384. return SMB2_read(xid, parms, bytes_read, buf, buf_type);
  385. }
  386. static int
  387. smb2_sync_write(const unsigned int xid, struct cifsFileInfo *cfile,
  388. struct cifs_io_parms *parms, unsigned int *written,
  389. struct kvec *iov, unsigned long nr_segs)
  390. {
  391. parms->persistent_fid = cfile->fid.persistent_fid;
  392. parms->volatile_fid = cfile->fid.volatile_fid;
  393. return SMB2_write(xid, parms, written, iov, nr_segs);
  394. }
  395. static int
  396. smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
  397. struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
  398. {
  399. __le64 eof = cpu_to_le64(size);
  400. return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
  401. cfile->fid.volatile_fid, cfile->pid, &eof);
  402. }
  403. static int
  404. smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
  405. const char *path, struct cifs_sb_info *cifs_sb,
  406. struct cifs_fid *fid, __u16 search_flags,
  407. struct cifs_search_info *srch_inf)
  408. {
  409. __le16 *utf16_path;
  410. int rc;
  411. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  412. struct cifs_open_parms oparms;
  413. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  414. if (!utf16_path)
  415. return -ENOMEM;
  416. oparms.tcon = tcon;
  417. oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
  418. oparms.disposition = FILE_OPEN;
  419. oparms.create_options = 0;
  420. oparms.fid = fid;
  421. oparms.reconnect = false;
  422. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
  423. kfree(utf16_path);
  424. if (rc) {
  425. cifs_dbg(VFS, "open dir failed\n");
  426. return rc;
  427. }
  428. srch_inf->entries_in_buffer = 0;
  429. srch_inf->index_of_last_entry = 0;
  430. rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
  431. fid->volatile_fid, 0, srch_inf);
  432. if (rc) {
  433. cifs_dbg(VFS, "query directory failed\n");
  434. SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  435. }
  436. return rc;
  437. }
  438. static int
  439. smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
  440. struct cifs_fid *fid, __u16 search_flags,
  441. struct cifs_search_info *srch_inf)
  442. {
  443. return SMB2_query_directory(xid, tcon, fid->persistent_fid,
  444. fid->volatile_fid, 0, srch_inf);
  445. }
  446. static int
  447. smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
  448. struct cifs_fid *fid)
  449. {
  450. return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  451. }
  452. /*
  453. * If we negotiate SMB2 protocol and get STATUS_PENDING - update
  454. * the number of credits and return true. Otherwise - return false.
  455. */
  456. static bool
  457. smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
  458. {
  459. struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
  460. if (hdr->Status != STATUS_PENDING)
  461. return false;
  462. if (!length) {
  463. spin_lock(&server->req_lock);
  464. server->credits += le16_to_cpu(hdr->CreditRequest);
  465. spin_unlock(&server->req_lock);
  466. wake_up(&server->request_q);
  467. }
  468. return true;
  469. }
  470. static int
  471. smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
  472. struct cifsInodeInfo *cinode)
  473. {
  474. if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
  475. return SMB2_lease_break(0, tcon, cinode->lease_key,
  476. smb2_get_lease_state(cinode));
  477. return SMB2_oplock_break(0, tcon, fid->persistent_fid,
  478. fid->volatile_fid,
  479. CIFS_CACHE_READ(cinode) ? 1 : 0);
  480. }
  481. static int
  482. smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
  483. struct kstatfs *buf)
  484. {
  485. int rc;
  486. __le16 srch_path = 0; /* Null - open root of share */
  487. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  488. struct cifs_open_parms oparms;
  489. struct cifs_fid fid;
  490. oparms.tcon = tcon;
  491. oparms.desired_access = FILE_READ_ATTRIBUTES;
  492. oparms.disposition = FILE_OPEN;
  493. oparms.create_options = 0;
  494. oparms.fid = &fid;
  495. oparms.reconnect = false;
  496. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
  497. if (rc)
  498. return rc;
  499. buf->f_type = SMB2_MAGIC_NUMBER;
  500. rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  501. buf);
  502. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  503. return rc;
  504. }
  505. static bool
  506. smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
  507. {
  508. return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
  509. ob1->fid.volatile_fid == ob2->fid.volatile_fid;
  510. }
  511. static int
  512. smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
  513. __u64 length, __u32 type, int lock, int unlock, bool wait)
  514. {
  515. if (unlock && !lock)
  516. type = SMB2_LOCKFLAG_UNLOCK;
  517. return SMB2_lock(xid, tlink_tcon(cfile->tlink),
  518. cfile->fid.persistent_fid, cfile->fid.volatile_fid,
  519. current->tgid, length, offset, type, wait);
  520. }
  521. static void
  522. smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
  523. {
  524. memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
  525. }
  526. static void
  527. smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
  528. {
  529. memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
  530. }
  531. static void
  532. smb2_new_lease_key(struct cifs_fid *fid)
  533. {
  534. get_random_bytes(fid->lease_key, SMB2_LEASE_KEY_SIZE);
  535. }
  536. static int
  537. smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
  538. const char *full_path, char **target_path,
  539. struct cifs_sb_info *cifs_sb)
  540. {
  541. int rc;
  542. __le16 *utf16_path;
  543. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  544. struct cifs_open_parms oparms;
  545. struct cifs_fid fid;
  546. struct smb2_err_rsp *err_buf = NULL;
  547. struct smb2_symlink_err_rsp *symlink;
  548. unsigned int sub_len, sub_offset;
  549. cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
  550. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  551. if (!utf16_path)
  552. return -ENOMEM;
  553. oparms.tcon = tcon;
  554. oparms.desired_access = FILE_READ_ATTRIBUTES;
  555. oparms.disposition = FILE_OPEN;
  556. oparms.create_options = 0;
  557. oparms.fid = &fid;
  558. oparms.reconnect = false;
  559. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
  560. if (!rc || !err_buf) {
  561. kfree(utf16_path);
  562. return -ENOENT;
  563. }
  564. /* open must fail on symlink - reset rc */
  565. rc = 0;
  566. symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
  567. sub_len = le16_to_cpu(symlink->SubstituteNameLength);
  568. sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
  569. *target_path = cifs_strndup_from_utf16(
  570. (char *)symlink->PathBuffer + sub_offset,
  571. sub_len, true, cifs_sb->local_nls);
  572. if (!(*target_path)) {
  573. kfree(utf16_path);
  574. return -ENOMEM;
  575. }
  576. convert_delimiter(*target_path, '/');
  577. cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
  578. kfree(utf16_path);
  579. return rc;
  580. }
  581. static void
  582. smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
  583. {
  584. oplock &= 0xFF;
  585. if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
  586. return;
  587. if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
  588. cinode->oplock = CIFS_CACHE_READ_FLG | CIFS_CACHE_WRITE_FLG |
  589. CIFS_CACHE_HANDLE_FLG;
  590. cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
  591. &cinode->vfs_inode);
  592. } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
  593. cinode->oplock = CIFS_CACHE_READ_FLG | CIFS_CACHE_WRITE_FLG;
  594. cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
  595. &cinode->vfs_inode);
  596. } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
  597. cinode->oplock = CIFS_CACHE_READ_FLG;
  598. cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
  599. &cinode->vfs_inode);
  600. } else
  601. cinode->oplock = 0;
  602. }
  603. static void
  604. smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
  605. {
  606. char message[5] = {0};
  607. oplock &= 0xFF;
  608. if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
  609. return;
  610. cinode->oplock = 0;
  611. if (oplock & SMB2_LEASE_READ_CACHING_HE) {
  612. cinode->oplock |= CIFS_CACHE_READ_FLG;
  613. strcat(message, "R");
  614. }
  615. if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
  616. cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
  617. strcat(message, "H");
  618. }
  619. if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
  620. cinode->oplock |= CIFS_CACHE_WRITE_FLG;
  621. strcat(message, "W");
  622. }
  623. if (!cinode->oplock)
  624. strcat(message, "None");
  625. cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
  626. &cinode->vfs_inode);
  627. }
  628. static bool
  629. smb2_is_read_op(__u32 oplock)
  630. {
  631. return oplock == SMB2_OPLOCK_LEVEL_II;
  632. }
  633. static bool
  634. smb21_is_read_op(__u32 oplock)
  635. {
  636. return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
  637. !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
  638. }
  639. struct smb_version_operations smb20_operations = {
  640. .compare_fids = smb2_compare_fids,
  641. .setup_request = smb2_setup_request,
  642. .setup_async_request = smb2_setup_async_request,
  643. .check_receive = smb2_check_receive,
  644. .add_credits = smb2_add_credits,
  645. .set_credits = smb2_set_credits,
  646. .get_credits_field = smb2_get_credits_field,
  647. .get_credits = smb2_get_credits,
  648. .get_next_mid = smb2_get_next_mid,
  649. .read_data_offset = smb2_read_data_offset,
  650. .read_data_length = smb2_read_data_length,
  651. .map_error = map_smb2_to_linux_error,
  652. .find_mid = smb2_find_mid,
  653. .check_message = smb2_check_message,
  654. .dump_detail = smb2_dump_detail,
  655. .clear_stats = smb2_clear_stats,
  656. .print_stats = smb2_print_stats,
  657. .is_oplock_break = smb2_is_valid_oplock_break,
  658. .need_neg = smb2_need_neg,
  659. .negotiate = smb2_negotiate,
  660. .negotiate_wsize = smb2_negotiate_wsize,
  661. .negotiate_rsize = smb2_negotiate_rsize,
  662. .sess_setup = SMB2_sess_setup,
  663. .logoff = SMB2_logoff,
  664. .tree_connect = SMB2_tcon,
  665. .tree_disconnect = SMB2_tdis,
  666. .is_path_accessible = smb2_is_path_accessible,
  667. .can_echo = smb2_can_echo,
  668. .echo = SMB2_echo,
  669. .query_path_info = smb2_query_path_info,
  670. .get_srv_inum = smb2_get_srv_inum,
  671. .query_file_info = smb2_query_file_info,
  672. .set_path_size = smb2_set_path_size,
  673. .set_file_size = smb2_set_file_size,
  674. .set_file_info = smb2_set_file_info,
  675. .mkdir = smb2_mkdir,
  676. .mkdir_setinfo = smb2_mkdir_setinfo,
  677. .rmdir = smb2_rmdir,
  678. .unlink = smb2_unlink,
  679. .rename = smb2_rename_path,
  680. .create_hardlink = smb2_create_hardlink,
  681. .query_symlink = smb2_query_symlink,
  682. .open = smb2_open_file,
  683. .set_fid = smb2_set_fid,
  684. .close = smb2_close_file,
  685. .flush = smb2_flush_file,
  686. .async_readv = smb2_async_readv,
  687. .async_writev = smb2_async_writev,
  688. .sync_read = smb2_sync_read,
  689. .sync_write = smb2_sync_write,
  690. .query_dir_first = smb2_query_dir_first,
  691. .query_dir_next = smb2_query_dir_next,
  692. .close_dir = smb2_close_dir,
  693. .calc_smb_size = smb2_calc_size,
  694. .is_status_pending = smb2_is_status_pending,
  695. .oplock_response = smb2_oplock_response,
  696. .queryfs = smb2_queryfs,
  697. .mand_lock = smb2_mand_lock,
  698. .mand_unlock_range = smb2_unlock_range,
  699. .push_mand_locks = smb2_push_mandatory_locks,
  700. .get_lease_key = smb2_get_lease_key,
  701. .set_lease_key = smb2_set_lease_key,
  702. .new_lease_key = smb2_new_lease_key,
  703. .calc_signature = smb2_calc_signature,
  704. .is_read_op = smb2_is_read_op,
  705. .set_oplock_level = smb2_set_oplock_level,
  706. };
  707. struct smb_version_operations smb21_operations = {
  708. .compare_fids = smb2_compare_fids,
  709. .setup_request = smb2_setup_request,
  710. .setup_async_request = smb2_setup_async_request,
  711. .check_receive = smb2_check_receive,
  712. .add_credits = smb2_add_credits,
  713. .set_credits = smb2_set_credits,
  714. .get_credits_field = smb2_get_credits_field,
  715. .get_credits = smb2_get_credits,
  716. .get_next_mid = smb2_get_next_mid,
  717. .read_data_offset = smb2_read_data_offset,
  718. .read_data_length = smb2_read_data_length,
  719. .map_error = map_smb2_to_linux_error,
  720. .find_mid = smb2_find_mid,
  721. .check_message = smb2_check_message,
  722. .dump_detail = smb2_dump_detail,
  723. .clear_stats = smb2_clear_stats,
  724. .print_stats = smb2_print_stats,
  725. .is_oplock_break = smb2_is_valid_oplock_break,
  726. .need_neg = smb2_need_neg,
  727. .negotiate = smb2_negotiate,
  728. .negotiate_wsize = smb2_negotiate_wsize,
  729. .negotiate_rsize = smb2_negotiate_rsize,
  730. .sess_setup = SMB2_sess_setup,
  731. .logoff = SMB2_logoff,
  732. .tree_connect = SMB2_tcon,
  733. .tree_disconnect = SMB2_tdis,
  734. .is_path_accessible = smb2_is_path_accessible,
  735. .can_echo = smb2_can_echo,
  736. .echo = SMB2_echo,
  737. .query_path_info = smb2_query_path_info,
  738. .get_srv_inum = smb2_get_srv_inum,
  739. .query_file_info = smb2_query_file_info,
  740. .set_path_size = smb2_set_path_size,
  741. .set_file_size = smb2_set_file_size,
  742. .set_file_info = smb2_set_file_info,
  743. .mkdir = smb2_mkdir,
  744. .mkdir_setinfo = smb2_mkdir_setinfo,
  745. .rmdir = smb2_rmdir,
  746. .unlink = smb2_unlink,
  747. .rename = smb2_rename_path,
  748. .create_hardlink = smb2_create_hardlink,
  749. .query_symlink = smb2_query_symlink,
  750. .open = smb2_open_file,
  751. .set_fid = smb2_set_fid,
  752. .close = smb2_close_file,
  753. .flush = smb2_flush_file,
  754. .async_readv = smb2_async_readv,
  755. .async_writev = smb2_async_writev,
  756. .sync_read = smb2_sync_read,
  757. .sync_write = smb2_sync_write,
  758. .query_dir_first = smb2_query_dir_first,
  759. .query_dir_next = smb2_query_dir_next,
  760. .close_dir = smb2_close_dir,
  761. .calc_smb_size = smb2_calc_size,
  762. .is_status_pending = smb2_is_status_pending,
  763. .oplock_response = smb2_oplock_response,
  764. .queryfs = smb2_queryfs,
  765. .mand_lock = smb2_mand_lock,
  766. .mand_unlock_range = smb2_unlock_range,
  767. .push_mand_locks = smb2_push_mandatory_locks,
  768. .get_lease_key = smb2_get_lease_key,
  769. .set_lease_key = smb2_set_lease_key,
  770. .new_lease_key = smb2_new_lease_key,
  771. .calc_signature = smb2_calc_signature,
  772. .is_read_op = smb21_is_read_op,
  773. .set_oplock_level = smb21_set_oplock_level,
  774. };
  775. struct smb_version_operations smb30_operations = {
  776. .compare_fids = smb2_compare_fids,
  777. .setup_request = smb2_setup_request,
  778. .setup_async_request = smb2_setup_async_request,
  779. .check_receive = smb2_check_receive,
  780. .add_credits = smb2_add_credits,
  781. .set_credits = smb2_set_credits,
  782. .get_credits_field = smb2_get_credits_field,
  783. .get_credits = smb2_get_credits,
  784. .get_next_mid = smb2_get_next_mid,
  785. .read_data_offset = smb2_read_data_offset,
  786. .read_data_length = smb2_read_data_length,
  787. .map_error = map_smb2_to_linux_error,
  788. .find_mid = smb2_find_mid,
  789. .check_message = smb2_check_message,
  790. .dump_detail = smb2_dump_detail,
  791. .clear_stats = smb2_clear_stats,
  792. .print_stats = smb2_print_stats,
  793. .dump_share_caps = smb2_dump_share_caps,
  794. .is_oplock_break = smb2_is_valid_oplock_break,
  795. .need_neg = smb2_need_neg,
  796. .negotiate = smb2_negotiate,
  797. .negotiate_wsize = smb2_negotiate_wsize,
  798. .negotiate_rsize = smb2_negotiate_rsize,
  799. .sess_setup = SMB2_sess_setup,
  800. .logoff = SMB2_logoff,
  801. .tree_connect = SMB2_tcon,
  802. .tree_disconnect = SMB2_tdis,
  803. .is_path_accessible = smb2_is_path_accessible,
  804. .can_echo = smb2_can_echo,
  805. .echo = SMB2_echo,
  806. .query_path_info = smb2_query_path_info,
  807. .get_srv_inum = smb2_get_srv_inum,
  808. .query_file_info = smb2_query_file_info,
  809. .set_path_size = smb2_set_path_size,
  810. .set_file_size = smb2_set_file_size,
  811. .set_file_info = smb2_set_file_info,
  812. .mkdir = smb2_mkdir,
  813. .mkdir_setinfo = smb2_mkdir_setinfo,
  814. .rmdir = smb2_rmdir,
  815. .unlink = smb2_unlink,
  816. .rename = smb2_rename_path,
  817. .create_hardlink = smb2_create_hardlink,
  818. .query_symlink = smb2_query_symlink,
  819. .open = smb2_open_file,
  820. .set_fid = smb2_set_fid,
  821. .close = smb2_close_file,
  822. .flush = smb2_flush_file,
  823. .async_readv = smb2_async_readv,
  824. .async_writev = smb2_async_writev,
  825. .sync_read = smb2_sync_read,
  826. .sync_write = smb2_sync_write,
  827. .query_dir_first = smb2_query_dir_first,
  828. .query_dir_next = smb2_query_dir_next,
  829. .close_dir = smb2_close_dir,
  830. .calc_smb_size = smb2_calc_size,
  831. .is_status_pending = smb2_is_status_pending,
  832. .oplock_response = smb2_oplock_response,
  833. .queryfs = smb2_queryfs,
  834. .mand_lock = smb2_mand_lock,
  835. .mand_unlock_range = smb2_unlock_range,
  836. .push_mand_locks = smb2_push_mandatory_locks,
  837. .get_lease_key = smb2_get_lease_key,
  838. .set_lease_key = smb2_set_lease_key,
  839. .new_lease_key = smb2_new_lease_key,
  840. .generate_signingkey = generate_smb3signingkey,
  841. .calc_signature = smb3_calc_signature,
  842. .is_read_op = smb21_is_read_op,
  843. .set_oplock_level = smb21_set_oplock_level,
  844. };
  845. struct smb_version_values smb20_values = {
  846. .version_string = SMB20_VERSION_STRING,
  847. .protocol_id = SMB20_PROT_ID,
  848. .req_capabilities = 0, /* MBZ */
  849. .large_lock_type = 0,
  850. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  851. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  852. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  853. .header_size = sizeof(struct smb2_hdr),
  854. .max_header_size = MAX_SMB2_HDR_SIZE,
  855. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  856. .lock_cmd = SMB2_LOCK,
  857. .cap_unix = 0,
  858. .cap_nt_find = SMB2_NT_FIND,
  859. .cap_large_files = SMB2_LARGE_FILES,
  860. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  861. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  862. };
  863. struct smb_version_values smb21_values = {
  864. .version_string = SMB21_VERSION_STRING,
  865. .protocol_id = SMB21_PROT_ID,
  866. .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
  867. .large_lock_type = 0,
  868. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  869. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  870. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  871. .header_size = sizeof(struct smb2_hdr),
  872. .max_header_size = MAX_SMB2_HDR_SIZE,
  873. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  874. .lock_cmd = SMB2_LOCK,
  875. .cap_unix = 0,
  876. .cap_nt_find = SMB2_NT_FIND,
  877. .cap_large_files = SMB2_LARGE_FILES,
  878. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  879. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  880. };
  881. struct smb_version_values smb30_values = {
  882. .version_string = SMB30_VERSION_STRING,
  883. .protocol_id = SMB30_PROT_ID,
  884. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
  885. .large_lock_type = 0,
  886. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  887. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  888. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  889. .header_size = sizeof(struct smb2_hdr),
  890. .max_header_size = MAX_SMB2_HDR_SIZE,
  891. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  892. .lock_cmd = SMB2_LOCK,
  893. .cap_unix = 0,
  894. .cap_nt_find = SMB2_NT_FIND,
  895. .cap_large_files = SMB2_LARGE_FILES,
  896. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  897. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  898. };
  899. struct smb_version_values smb302_values = {
  900. .version_string = SMB302_VERSION_STRING,
  901. .protocol_id = SMB302_PROT_ID,
  902. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
  903. .large_lock_type = 0,
  904. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
  905. .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
  906. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  907. .header_size = sizeof(struct smb2_hdr),
  908. .max_header_size = MAX_SMB2_HDR_SIZE,
  909. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  910. .lock_cmd = SMB2_LOCK,
  911. .cap_unix = 0,
  912. .cap_nt_find = SMB2_NT_FIND,
  913. .cap_large_files = SMB2_LARGE_FILES,
  914. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  915. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  916. };