smb2ops.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 "cifsglob.h"
  21. #include "smb2pdu.h"
  22. #include "smb2proto.h"
  23. #include "cifsproto.h"
  24. #include "cifs_debug.h"
  25. static int
  26. change_conf(struct TCP_Server_Info *server)
  27. {
  28. server->credits += server->echo_credits + server->oplock_credits;
  29. server->oplock_credits = server->echo_credits = 0;
  30. switch (server->credits) {
  31. case 0:
  32. return -1;
  33. case 1:
  34. server->echoes = false;
  35. server->oplocks = false;
  36. cERROR(1, "disabling echoes and oplocks");
  37. break;
  38. case 2:
  39. server->echoes = true;
  40. server->oplocks = false;
  41. server->echo_credits = 1;
  42. cFYI(1, "disabling oplocks");
  43. break;
  44. default:
  45. server->echoes = true;
  46. server->oplocks = true;
  47. server->echo_credits = 1;
  48. server->oplock_credits = 1;
  49. }
  50. server->credits -= server->echo_credits + server->oplock_credits;
  51. return 0;
  52. }
  53. static void
  54. smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
  55. const int optype)
  56. {
  57. int *val, rc = 0;
  58. spin_lock(&server->req_lock);
  59. val = server->ops->get_credits_field(server, optype);
  60. *val += add;
  61. server->in_flight--;
  62. if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
  63. rc = change_conf(server);
  64. spin_unlock(&server->req_lock);
  65. wake_up(&server->request_q);
  66. if (rc)
  67. cifs_reconnect(server);
  68. }
  69. static void
  70. smb2_set_credits(struct TCP_Server_Info *server, const int val)
  71. {
  72. spin_lock(&server->req_lock);
  73. server->credits = val;
  74. spin_unlock(&server->req_lock);
  75. }
  76. static int *
  77. smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
  78. {
  79. switch (optype) {
  80. case CIFS_ECHO_OP:
  81. return &server->echo_credits;
  82. case CIFS_OBREAK_OP:
  83. return &server->oplock_credits;
  84. default:
  85. return &server->credits;
  86. }
  87. }
  88. static unsigned int
  89. smb2_get_credits(struct mid_q_entry *mid)
  90. {
  91. return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
  92. }
  93. static __u64
  94. smb2_get_next_mid(struct TCP_Server_Info *server)
  95. {
  96. __u64 mid;
  97. /* for SMB2 we need the current value */
  98. spin_lock(&GlobalMid_Lock);
  99. mid = server->CurrentMid++;
  100. spin_unlock(&GlobalMid_Lock);
  101. return mid;
  102. }
  103. static struct mid_q_entry *
  104. smb2_find_mid(struct TCP_Server_Info *server, char *buf)
  105. {
  106. struct mid_q_entry *mid;
  107. struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
  108. spin_lock(&GlobalMid_Lock);
  109. list_for_each_entry(mid, &server->pending_mid_q, qhead) {
  110. if ((mid->mid == hdr->MessageId) &&
  111. (mid->mid_state == MID_REQUEST_SUBMITTED) &&
  112. (mid->command == hdr->Command)) {
  113. spin_unlock(&GlobalMid_Lock);
  114. return mid;
  115. }
  116. }
  117. spin_unlock(&GlobalMid_Lock);
  118. return NULL;
  119. }
  120. static void
  121. smb2_dump_detail(void *buf)
  122. {
  123. #ifdef CONFIG_CIFS_DEBUG2
  124. struct smb2_hdr *smb = (struct smb2_hdr *)buf;
  125. cERROR(1, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d",
  126. smb->Command, smb->Status, smb->Flags, smb->MessageId,
  127. smb->ProcessId);
  128. cERROR(1, "smb buf %p len %u", smb, smb2_calc_size(smb));
  129. #endif
  130. }
  131. static bool
  132. smb2_need_neg(struct TCP_Server_Info *server)
  133. {
  134. return server->max_read == 0;
  135. }
  136. static int
  137. smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
  138. {
  139. int rc;
  140. ses->server->CurrentMid = 0;
  141. rc = SMB2_negotiate(xid, ses);
  142. /* BB we probably don't need to retry with modern servers */
  143. if (rc == -EAGAIN)
  144. rc = -EHOSTDOWN;
  145. return rc;
  146. }
  147. static unsigned int
  148. smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  149. {
  150. struct TCP_Server_Info *server = tcon->ses->server;
  151. unsigned int wsize;
  152. /* start with specified wsize, or default */
  153. wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
  154. wsize = min_t(unsigned int, wsize, server->max_write);
  155. /*
  156. * limit write size to 2 ** 16, because we don't support multicredit
  157. * requests now.
  158. */
  159. wsize = min_t(unsigned int, wsize, 2 << 15);
  160. /* limit to the amount that we can kmap at once */
  161. wsize = min_t(unsigned int, wsize, CIFS_KMAP_SIZE_LIMIT);
  162. return wsize;
  163. }
  164. static unsigned int
  165. smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  166. {
  167. struct TCP_Server_Info *server = tcon->ses->server;
  168. unsigned int rsize;
  169. /* start with specified rsize, or default */
  170. rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
  171. rsize = min_t(unsigned int, rsize, server->max_read);
  172. /*
  173. * limit write size to 2 ** 16, because we don't support multicredit
  174. * requests now.
  175. */
  176. rsize = min_t(unsigned int, rsize, 2 << 15);
  177. /* limit to the amount that we can kmap at once */
  178. rsize = min_t(unsigned int, rsize, CIFS_KMAP_SIZE_LIMIT);
  179. return rsize;
  180. }
  181. static int
  182. smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
  183. struct cifs_sb_info *cifs_sb, const char *full_path)
  184. {
  185. int rc;
  186. __u64 persistent_fid, volatile_fid;
  187. __le16 *utf16_path;
  188. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  189. if (!utf16_path)
  190. return -ENOMEM;
  191. rc = SMB2_open(xid, tcon, utf16_path, &persistent_fid, &volatile_fid,
  192. FILE_READ_ATTRIBUTES, FILE_OPEN, 0, 0, NULL);
  193. if (rc) {
  194. kfree(utf16_path);
  195. return rc;
  196. }
  197. rc = SMB2_close(xid, tcon, persistent_fid, volatile_fid);
  198. kfree(utf16_path);
  199. return rc;
  200. }
  201. static int
  202. smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
  203. struct cifs_sb_info *cifs_sb, const char *full_path,
  204. u64 *uniqueid, FILE_ALL_INFO *data)
  205. {
  206. *uniqueid = le64_to_cpu(data->IndexNumber);
  207. return 0;
  208. }
  209. static int
  210. smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
  211. struct cifs_fid *fid, FILE_ALL_INFO *data)
  212. {
  213. int rc;
  214. struct smb2_file_all_info *smb2_data;
  215. smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
  216. GFP_KERNEL);
  217. if (smb2_data == NULL)
  218. return -ENOMEM;
  219. rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
  220. smb2_data);
  221. if (!rc)
  222. move_smb2_info_to_cifs(data, smb2_data);
  223. kfree(smb2_data);
  224. return rc;
  225. }
  226. static char *
  227. smb2_build_path_to_root(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
  228. struct cifs_tcon *tcon)
  229. {
  230. int pplen = vol->prepath ? strlen(vol->prepath) : 0;
  231. char *full_path = NULL;
  232. /* if no prefix path, simply set path to the root of share to "" */
  233. if (pplen == 0) {
  234. full_path = kzalloc(2, GFP_KERNEL);
  235. return full_path;
  236. }
  237. cERROR(1, "prefixpath is not supported for SMB2 now");
  238. return NULL;
  239. }
  240. static bool
  241. smb2_can_echo(struct TCP_Server_Info *server)
  242. {
  243. return server->echoes;
  244. }
  245. static void
  246. smb2_clear_stats(struct cifs_tcon *tcon)
  247. {
  248. #ifdef CONFIG_CIFS_STATS
  249. int i;
  250. for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
  251. atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
  252. atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
  253. }
  254. #endif
  255. }
  256. static void
  257. smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
  258. {
  259. #ifdef CONFIG_CIFS_STATS
  260. atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
  261. atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
  262. seq_printf(m, "\nNegotiates: %d sent %d failed",
  263. atomic_read(&sent[SMB2_NEGOTIATE_HE]),
  264. atomic_read(&failed[SMB2_NEGOTIATE_HE]));
  265. seq_printf(m, "\nSessionSetups: %d sent %d failed",
  266. atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
  267. atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
  268. #define SMB2LOGOFF 0x0002 /* trivial request/resp */
  269. seq_printf(m, "\nLogoffs: %d sent %d failed",
  270. atomic_read(&sent[SMB2_LOGOFF_HE]),
  271. atomic_read(&failed[SMB2_LOGOFF_HE]));
  272. seq_printf(m, "\nTreeConnects: %d sent %d failed",
  273. atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
  274. atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
  275. seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
  276. atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
  277. atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
  278. seq_printf(m, "\nCreates: %d sent %d failed",
  279. atomic_read(&sent[SMB2_CREATE_HE]),
  280. atomic_read(&failed[SMB2_CREATE_HE]));
  281. seq_printf(m, "\nCloses: %d sent %d failed",
  282. atomic_read(&sent[SMB2_CLOSE_HE]),
  283. atomic_read(&failed[SMB2_CLOSE_HE]));
  284. seq_printf(m, "\nFlushes: %d sent %d failed",
  285. atomic_read(&sent[SMB2_FLUSH_HE]),
  286. atomic_read(&failed[SMB2_FLUSH_HE]));
  287. seq_printf(m, "\nReads: %d sent %d failed",
  288. atomic_read(&sent[SMB2_READ_HE]),
  289. atomic_read(&failed[SMB2_READ_HE]));
  290. seq_printf(m, "\nWrites: %d sent %d failed",
  291. atomic_read(&sent[SMB2_WRITE_HE]),
  292. atomic_read(&failed[SMB2_WRITE_HE]));
  293. seq_printf(m, "\nLocks: %d sent %d failed",
  294. atomic_read(&sent[SMB2_LOCK_HE]),
  295. atomic_read(&failed[SMB2_LOCK_HE]));
  296. seq_printf(m, "\nIOCTLs: %d sent %d failed",
  297. atomic_read(&sent[SMB2_IOCTL_HE]),
  298. atomic_read(&failed[SMB2_IOCTL_HE]));
  299. seq_printf(m, "\nCancels: %d sent %d failed",
  300. atomic_read(&sent[SMB2_CANCEL_HE]),
  301. atomic_read(&failed[SMB2_CANCEL_HE]));
  302. seq_printf(m, "\nEchos: %d sent %d failed",
  303. atomic_read(&sent[SMB2_ECHO_HE]),
  304. atomic_read(&failed[SMB2_ECHO_HE]));
  305. seq_printf(m, "\nQueryDirectories: %d sent %d failed",
  306. atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
  307. atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
  308. seq_printf(m, "\nChangeNotifies: %d sent %d failed",
  309. atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
  310. atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
  311. seq_printf(m, "\nQueryInfos: %d sent %d failed",
  312. atomic_read(&sent[SMB2_QUERY_INFO_HE]),
  313. atomic_read(&failed[SMB2_QUERY_INFO_HE]));
  314. seq_printf(m, "\nSetInfos: %d sent %d failed",
  315. atomic_read(&sent[SMB2_SET_INFO_HE]),
  316. atomic_read(&failed[SMB2_SET_INFO_HE]));
  317. seq_printf(m, "\nOplockBreaks: %d sent %d failed",
  318. atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
  319. atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
  320. #endif
  321. }
  322. static void
  323. smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
  324. {
  325. /* struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode); */
  326. cfile->fid.persistent_fid = fid->persistent_fid;
  327. cfile->fid.volatile_fid = fid->volatile_fid;
  328. /* cifs_set_oplock_level(cinode, oplock); */
  329. /* cinode->can_cache_brlcks = cinode->clientCanCacheAll; */
  330. }
  331. static int
  332. smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
  333. struct cifs_fid *fid)
  334. {
  335. return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  336. }
  337. static int
  338. smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
  339. struct cifs_fid *fid)
  340. {
  341. return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  342. }
  343. static unsigned int
  344. smb2_read_data_offset(char *buf)
  345. {
  346. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  347. return rsp->DataOffset;
  348. }
  349. static unsigned int
  350. smb2_read_data_length(char *buf)
  351. {
  352. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  353. return le32_to_cpu(rsp->DataLength);
  354. }
  355. static int
  356. smb2_sync_read(const unsigned int xid, struct cifsFileInfo *cfile,
  357. struct cifs_io_parms *parms, unsigned int *bytes_read,
  358. char **buf, int *buf_type)
  359. {
  360. parms->persistent_fid = cfile->fid.persistent_fid;
  361. parms->volatile_fid = cfile->fid.volatile_fid;
  362. return SMB2_read(xid, parms, bytes_read, buf, buf_type);
  363. }
  364. static int
  365. smb2_sync_write(const unsigned int xid, struct cifsFileInfo *cfile,
  366. struct cifs_io_parms *parms, unsigned int *written,
  367. struct kvec *iov, unsigned long nr_segs)
  368. {
  369. parms->persistent_fid = cfile->fid.persistent_fid;
  370. parms->volatile_fid = cfile->fid.volatile_fid;
  371. return SMB2_write(xid, parms, written, iov, nr_segs);
  372. }
  373. static int
  374. smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
  375. struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
  376. {
  377. __le64 eof = cpu_to_le64(size);
  378. return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
  379. cfile->fid.volatile_fid, cfile->pid, &eof);
  380. }
  381. static int
  382. smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
  383. const char *path, struct cifs_sb_info *cifs_sb,
  384. struct cifs_fid *fid, __u16 search_flags,
  385. struct cifs_search_info *srch_inf)
  386. {
  387. __le16 *utf16_path;
  388. int rc;
  389. __u64 persistent_fid, volatile_fid;
  390. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  391. if (!utf16_path)
  392. return -ENOMEM;
  393. rc = SMB2_open(xid, tcon, utf16_path, &persistent_fid, &volatile_fid,
  394. FILE_READ_ATTRIBUTES | FILE_READ_DATA, FILE_OPEN, 0, 0,
  395. NULL);
  396. kfree(utf16_path);
  397. if (rc) {
  398. cERROR(1, "open dir failed");
  399. return rc;
  400. }
  401. srch_inf->entries_in_buffer = 0;
  402. srch_inf->index_of_last_entry = 0;
  403. fid->persistent_fid = persistent_fid;
  404. fid->volatile_fid = volatile_fid;
  405. rc = SMB2_query_directory(xid, tcon, persistent_fid, volatile_fid, 0,
  406. srch_inf);
  407. if (rc) {
  408. cERROR(1, "query directory failed");
  409. SMB2_close(xid, tcon, persistent_fid, volatile_fid);
  410. }
  411. return rc;
  412. }
  413. static int
  414. smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
  415. struct cifs_fid *fid, __u16 search_flags,
  416. struct cifs_search_info *srch_inf)
  417. {
  418. return SMB2_query_directory(xid, tcon, fid->persistent_fid,
  419. fid->volatile_fid, 0, srch_inf);
  420. }
  421. static int
  422. smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
  423. struct cifs_fid *fid)
  424. {
  425. return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  426. }
  427. struct smb_version_operations smb21_operations = {
  428. .setup_request = smb2_setup_request,
  429. .setup_async_request = smb2_setup_async_request,
  430. .check_receive = smb2_check_receive,
  431. .add_credits = smb2_add_credits,
  432. .set_credits = smb2_set_credits,
  433. .get_credits_field = smb2_get_credits_field,
  434. .get_credits = smb2_get_credits,
  435. .get_next_mid = smb2_get_next_mid,
  436. .read_data_offset = smb2_read_data_offset,
  437. .read_data_length = smb2_read_data_length,
  438. .map_error = map_smb2_to_linux_error,
  439. .find_mid = smb2_find_mid,
  440. .check_message = smb2_check_message,
  441. .dump_detail = smb2_dump_detail,
  442. .clear_stats = smb2_clear_stats,
  443. .print_stats = smb2_print_stats,
  444. .need_neg = smb2_need_neg,
  445. .negotiate = smb2_negotiate,
  446. .negotiate_wsize = smb2_negotiate_wsize,
  447. .negotiate_rsize = smb2_negotiate_rsize,
  448. .sess_setup = SMB2_sess_setup,
  449. .logoff = SMB2_logoff,
  450. .tree_connect = SMB2_tcon,
  451. .tree_disconnect = SMB2_tdis,
  452. .is_path_accessible = smb2_is_path_accessible,
  453. .can_echo = smb2_can_echo,
  454. .echo = SMB2_echo,
  455. .query_path_info = smb2_query_path_info,
  456. .get_srv_inum = smb2_get_srv_inum,
  457. .query_file_info = smb2_query_file_info,
  458. .set_path_size = smb2_set_path_size,
  459. .set_file_size = smb2_set_file_size,
  460. .set_file_info = smb2_set_file_info,
  461. .build_path_to_root = smb2_build_path_to_root,
  462. .mkdir = smb2_mkdir,
  463. .mkdir_setinfo = smb2_mkdir_setinfo,
  464. .rmdir = smb2_rmdir,
  465. .unlink = smb2_unlink,
  466. .rename = smb2_rename_path,
  467. .create_hardlink = smb2_create_hardlink,
  468. .open = smb2_open_file,
  469. .set_fid = smb2_set_fid,
  470. .close = smb2_close_file,
  471. .flush = smb2_flush_file,
  472. .async_readv = smb2_async_readv,
  473. .async_writev = smb2_async_writev,
  474. .sync_read = smb2_sync_read,
  475. .sync_write = smb2_sync_write,
  476. .query_dir_first = smb2_query_dir_first,
  477. .query_dir_next = smb2_query_dir_next,
  478. .close_dir = smb2_close_dir,
  479. .calc_smb_size = smb2_calc_size,
  480. };
  481. struct smb_version_values smb21_values = {
  482. .version_string = SMB21_VERSION_STRING,
  483. .header_size = sizeof(struct smb2_hdr),
  484. .max_header_size = MAX_SMB2_HDR_SIZE,
  485. .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
  486. .lock_cmd = SMB2_LOCK,
  487. .cap_unix = 0,
  488. .cap_nt_find = SMB2_NT_FIND,
  489. .cap_large_files = SMB2_LARGE_FILES,
  490. };