smb2ops.c 60 KB

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