smb1ops.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SMB1 (CIFS) version specific operations
  4. *
  5. * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
  6. */
  7. #include <linux/pagemap.h>
  8. #include <linux/vfs.h>
  9. #include <uapi/linux/magic.h>
  10. #include "cifsglob.h"
  11. #include "cifsproto.h"
  12. #include "cifs_debug.h"
  13. #include "cifspdu.h"
  14. #include "cifs_unicode.h"
  15. #include "fs_context.h"
  16. /*
  17. * An NT cancel request header looks just like the original request except:
  18. *
  19. * The Command is SMB_COM_NT_CANCEL
  20. * The WordCount is zeroed out
  21. * The ByteCount is zeroed out
  22. *
  23. * This function mangles an existing request buffer into a
  24. * SMB_COM_NT_CANCEL request and then sends it.
  25. */
  26. static int
  27. send_nt_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
  28. struct mid_q_entry *mid)
  29. {
  30. int rc = 0;
  31. struct smb_hdr *in_buf = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
  32. /* -4 for RFC1001 length and +2 for BCC field */
  33. in_buf->smb_buf_length = cpu_to_be32(sizeof(struct smb_hdr) - 4 + 2);
  34. in_buf->Command = SMB_COM_NT_CANCEL;
  35. in_buf->WordCount = 0;
  36. put_bcc(0, in_buf);
  37. cifs_server_lock(server);
  38. rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
  39. if (rc) {
  40. cifs_server_unlock(server);
  41. return rc;
  42. }
  43. /*
  44. * The response to this call was already factored into the sequence
  45. * number when the call went out, so we must adjust it back downward
  46. * after signing here.
  47. */
  48. --server->sequence_number;
  49. rc = smb_send(server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
  50. if (rc < 0)
  51. server->sequence_number--;
  52. cifs_server_unlock(server);
  53. cifs_dbg(FYI, "issued NT_CANCEL for mid %u, rc = %d\n",
  54. get_mid(in_buf), rc);
  55. return rc;
  56. }
  57. static bool
  58. cifs_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
  59. {
  60. return ob1->fid.netfid == ob2->fid.netfid;
  61. }
  62. static unsigned int
  63. cifs_read_data_offset(char *buf)
  64. {
  65. READ_RSP *rsp = (READ_RSP *)buf;
  66. return le16_to_cpu(rsp->DataOffset);
  67. }
  68. static unsigned int
  69. cifs_read_data_length(char *buf, bool in_remaining)
  70. {
  71. READ_RSP *rsp = (READ_RSP *)buf;
  72. /* It's a bug reading remaining data for SMB1 packets */
  73. WARN_ON(in_remaining);
  74. return (le16_to_cpu(rsp->DataLengthHigh) << 16) +
  75. le16_to_cpu(rsp->DataLength);
  76. }
  77. static struct mid_q_entry *
  78. cifs_find_mid(struct TCP_Server_Info *server, char *buffer)
  79. {
  80. struct smb_hdr *buf = (struct smb_hdr *)buffer;
  81. struct mid_q_entry *mid;
  82. spin_lock(&server->mid_lock);
  83. list_for_each_entry(mid, &server->pending_mid_q, qhead) {
  84. if (compare_mid(mid->mid, buf) &&
  85. mid->mid_state == MID_REQUEST_SUBMITTED &&
  86. le16_to_cpu(mid->command) == buf->Command) {
  87. kref_get(&mid->refcount);
  88. spin_unlock(&server->mid_lock);
  89. return mid;
  90. }
  91. }
  92. spin_unlock(&server->mid_lock);
  93. return NULL;
  94. }
  95. static void
  96. cifs_add_credits(struct TCP_Server_Info *server,
  97. struct cifs_credits *credits, const int optype)
  98. {
  99. spin_lock(&server->req_lock);
  100. server->credits += credits->value;
  101. server->in_flight--;
  102. spin_unlock(&server->req_lock);
  103. wake_up(&server->request_q);
  104. }
  105. static void
  106. cifs_set_credits(struct TCP_Server_Info *server, const int val)
  107. {
  108. spin_lock(&server->req_lock);
  109. server->credits = val;
  110. server->oplocks = val > 1 ? enable_oplocks : false;
  111. spin_unlock(&server->req_lock);
  112. }
  113. static int *
  114. cifs_get_credits_field(struct TCP_Server_Info *server, const int optype)
  115. {
  116. return &server->credits;
  117. }
  118. static unsigned int
  119. cifs_get_credits(struct mid_q_entry *mid)
  120. {
  121. return 1;
  122. }
  123. /*
  124. * Find a free multiplex id (SMB mid). Otherwise there could be
  125. * mid collisions which might cause problems, demultiplexing the
  126. * wrong response to this request. Multiplex ids could collide if
  127. * one of a series requests takes much longer than the others, or
  128. * if a very large number of long lived requests (byte range
  129. * locks or FindNotify requests) are pending. No more than
  130. * 64K-1 requests can be outstanding at one time. If no
  131. * mids are available, return zero. A future optimization
  132. * could make the combination of mids and uid the key we use
  133. * to demultiplex on (rather than mid alone).
  134. * In addition to the above check, the cifs demultiplex
  135. * code already used the command code as a secondary
  136. * check of the frame and if signing is negotiated the
  137. * response would be discarded if the mid were the same
  138. * but the signature was wrong. Since the mid is not put in the
  139. * pending queue until later (when it is about to be dispatched)
  140. * we do have to limit the number of outstanding requests
  141. * to somewhat less than 64K-1 although it is hard to imagine
  142. * so many threads being in the vfs at one time.
  143. */
  144. static __u64
  145. cifs_get_next_mid(struct TCP_Server_Info *server)
  146. {
  147. __u64 mid = 0;
  148. __u16 last_mid, cur_mid;
  149. bool collision, reconnect = false;
  150. spin_lock(&server->mid_lock);
  151. /* mid is 16 bit only for CIFS/SMB */
  152. cur_mid = (__u16)((server->CurrentMid) & 0xffff);
  153. /* we do not want to loop forever */
  154. last_mid = cur_mid;
  155. cur_mid++;
  156. /* avoid 0xFFFF MID */
  157. if (cur_mid == 0xffff)
  158. cur_mid++;
  159. /*
  160. * This nested loop looks more expensive than it is.
  161. * In practice the list of pending requests is short,
  162. * fewer than 50, and the mids are likely to be unique
  163. * on the first pass through the loop unless some request
  164. * takes longer than the 64 thousand requests before it
  165. * (and it would also have to have been a request that
  166. * did not time out).
  167. */
  168. while (cur_mid != last_mid) {
  169. struct mid_q_entry *mid_entry;
  170. unsigned int num_mids;
  171. collision = false;
  172. if (cur_mid == 0)
  173. cur_mid++;
  174. num_mids = 0;
  175. list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
  176. ++num_mids;
  177. if (mid_entry->mid == cur_mid &&
  178. mid_entry->mid_state == MID_REQUEST_SUBMITTED) {
  179. /* This mid is in use, try a different one */
  180. collision = true;
  181. break;
  182. }
  183. }
  184. /*
  185. * if we have more than 32k mids in the list, then something
  186. * is very wrong. Possibly a local user is trying to DoS the
  187. * box by issuing long-running calls and SIGKILL'ing them. If
  188. * we get to 2^16 mids then we're in big trouble as this
  189. * function could loop forever.
  190. *
  191. * Go ahead and assign out the mid in this situation, but force
  192. * an eventual reconnect to clean out the pending_mid_q.
  193. */
  194. if (num_mids > 32768)
  195. reconnect = true;
  196. if (!collision) {
  197. mid = (__u64)cur_mid;
  198. server->CurrentMid = mid;
  199. break;
  200. }
  201. cur_mid++;
  202. }
  203. spin_unlock(&server->mid_lock);
  204. if (reconnect) {
  205. cifs_signal_cifsd_for_reconnect(server, false);
  206. }
  207. return mid;
  208. }
  209. /*
  210. return codes:
  211. 0 not a transact2, or all data present
  212. >0 transact2 with that much data missing
  213. -EINVAL invalid transact2
  214. */
  215. static int
  216. check2ndT2(char *buf)
  217. {
  218. struct smb_hdr *pSMB = (struct smb_hdr *)buf;
  219. struct smb_t2_rsp *pSMBt;
  220. int remaining;
  221. __u16 total_data_size, data_in_this_rsp;
  222. if (pSMB->Command != SMB_COM_TRANSACTION2)
  223. return 0;
  224. /* check for plausible wct, bcc and t2 data and parm sizes */
  225. /* check for parm and data offset going beyond end of smb */
  226. if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */
  227. cifs_dbg(FYI, "Invalid transact2 word count\n");
  228. return -EINVAL;
  229. }
  230. pSMBt = (struct smb_t2_rsp *)pSMB;
  231. total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
  232. data_in_this_rsp = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
  233. if (total_data_size == data_in_this_rsp)
  234. return 0;
  235. else if (total_data_size < data_in_this_rsp) {
  236. cifs_dbg(FYI, "total data %d smaller than data in frame %d\n",
  237. total_data_size, data_in_this_rsp);
  238. return -EINVAL;
  239. }
  240. remaining = total_data_size - data_in_this_rsp;
  241. cifs_dbg(FYI, "missing %d bytes from transact2, check next response\n",
  242. remaining);
  243. if (total_data_size > CIFSMaxBufSize) {
  244. cifs_dbg(VFS, "TotalDataSize %d is over maximum buffer %d\n",
  245. total_data_size, CIFSMaxBufSize);
  246. return -EINVAL;
  247. }
  248. return remaining;
  249. }
  250. static int
  251. coalesce_t2(char *second_buf, struct smb_hdr *target_hdr)
  252. {
  253. struct smb_t2_rsp *pSMBs = (struct smb_t2_rsp *)second_buf;
  254. struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)target_hdr;
  255. char *data_area_of_tgt;
  256. char *data_area_of_src;
  257. int remaining;
  258. unsigned int byte_count, total_in_tgt;
  259. __u16 tgt_total_cnt, src_total_cnt, total_in_src;
  260. src_total_cnt = get_unaligned_le16(&pSMBs->t2_rsp.TotalDataCount);
  261. tgt_total_cnt = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
  262. if (tgt_total_cnt != src_total_cnt)
  263. cifs_dbg(FYI, "total data count of primary and secondary t2 differ source=%hu target=%hu\n",
  264. src_total_cnt, tgt_total_cnt);
  265. total_in_tgt = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
  266. remaining = tgt_total_cnt - total_in_tgt;
  267. if (remaining < 0) {
  268. cifs_dbg(FYI, "Server sent too much data. tgt_total_cnt=%hu total_in_tgt=%u\n",
  269. tgt_total_cnt, total_in_tgt);
  270. return -EPROTO;
  271. }
  272. if (remaining == 0) {
  273. /* nothing to do, ignore */
  274. cifs_dbg(FYI, "no more data remains\n");
  275. return 0;
  276. }
  277. total_in_src = get_unaligned_le16(&pSMBs->t2_rsp.DataCount);
  278. if (remaining < total_in_src)
  279. cifs_dbg(FYI, "transact2 2nd response contains too much data\n");
  280. /* find end of first SMB data area */
  281. data_area_of_tgt = (char *)&pSMBt->hdr.Protocol +
  282. get_unaligned_le16(&pSMBt->t2_rsp.DataOffset);
  283. /* validate target area */
  284. data_area_of_src = (char *)&pSMBs->hdr.Protocol +
  285. get_unaligned_le16(&pSMBs->t2_rsp.DataOffset);
  286. data_area_of_tgt += total_in_tgt;
  287. total_in_tgt += total_in_src;
  288. /* is the result too big for the field? */
  289. if (total_in_tgt > USHRT_MAX) {
  290. cifs_dbg(FYI, "coalesced DataCount too large (%u)\n",
  291. total_in_tgt);
  292. return -EPROTO;
  293. }
  294. put_unaligned_le16(total_in_tgt, &pSMBt->t2_rsp.DataCount);
  295. /* fix up the BCC */
  296. byte_count = get_bcc(target_hdr);
  297. byte_count += total_in_src;
  298. /* is the result too big for the field? */
  299. if (byte_count > USHRT_MAX) {
  300. cifs_dbg(FYI, "coalesced BCC too large (%u)\n", byte_count);
  301. return -EPROTO;
  302. }
  303. put_bcc(byte_count, target_hdr);
  304. byte_count = be32_to_cpu(target_hdr->smb_buf_length);
  305. byte_count += total_in_src;
  306. /* don't allow buffer to overflow */
  307. if (byte_count > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  308. cifs_dbg(FYI, "coalesced BCC exceeds buffer size (%u)\n",
  309. byte_count);
  310. return -ENOBUFS;
  311. }
  312. target_hdr->smb_buf_length = cpu_to_be32(byte_count);
  313. /* copy second buffer into end of first buffer */
  314. memcpy(data_area_of_tgt, data_area_of_src, total_in_src);
  315. if (remaining != total_in_src) {
  316. /* more responses to go */
  317. cifs_dbg(FYI, "waiting for more secondary responses\n");
  318. return 1;
  319. }
  320. /* we are done */
  321. cifs_dbg(FYI, "found the last secondary response\n");
  322. return 0;
  323. }
  324. static void
  325. cifs_downgrade_oplock(struct TCP_Server_Info *server,
  326. struct cifsInodeInfo *cinode, __u32 oplock,
  327. __u16 epoch, bool *purge_cache)
  328. {
  329. cifs_set_oplock_level(cinode, oplock);
  330. }
  331. static bool
  332. cifs_check_trans2(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  333. char *buf, int malformed)
  334. {
  335. if (malformed)
  336. return false;
  337. if (check2ndT2(buf) <= 0)
  338. return false;
  339. mid->multiRsp = true;
  340. if (mid->resp_buf) {
  341. /* merge response - fix up 1st*/
  342. malformed = coalesce_t2(buf, mid->resp_buf);
  343. if (malformed > 0)
  344. return true;
  345. /* All parts received or packet is malformed. */
  346. mid->multiEnd = true;
  347. dequeue_mid(mid, malformed);
  348. return true;
  349. }
  350. if (!server->large_buf) {
  351. /*FIXME: switch to already allocated largebuf?*/
  352. cifs_dbg(VFS, "1st trans2 resp needs bigbuf\n");
  353. } else {
  354. /* Have first buffer */
  355. mid->resp_buf = buf;
  356. mid->large_buf = true;
  357. server->bigbuf = NULL;
  358. }
  359. return true;
  360. }
  361. static bool
  362. cifs_need_neg(struct TCP_Server_Info *server)
  363. {
  364. return server->maxBuf == 0;
  365. }
  366. static int
  367. cifs_negotiate(const unsigned int xid,
  368. struct cifs_ses *ses,
  369. struct TCP_Server_Info *server)
  370. {
  371. int rc;
  372. rc = CIFSSMBNegotiate(xid, ses, server);
  373. return rc;
  374. }
  375. static unsigned int
  376. cifs_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
  377. {
  378. __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  379. struct TCP_Server_Info *server = tcon->ses->server;
  380. unsigned int wsize;
  381. /* start with specified wsize, or default */
  382. if (ctx->got_wsize)
  383. wsize = ctx->vol_wsize;
  384. else if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
  385. wsize = CIFS_DEFAULT_IOSIZE;
  386. else
  387. wsize = CIFS_DEFAULT_NON_POSIX_WSIZE;
  388. /* can server support 24-bit write sizes? (via UNIX extensions) */
  389. if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
  390. wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1002_WSIZE);
  391. /*
  392. * no CAP_LARGE_WRITE_X or is signing enabled without CAP_UNIX set?
  393. * Limit it to max buffer offered by the server, minus the size of the
  394. * WRITEX header, not including the 4 byte RFC1001 length.
  395. */
  396. if (!(server->capabilities & CAP_LARGE_WRITE_X) ||
  397. (!(server->capabilities & CAP_UNIX) && server->sign))
  398. wsize = min_t(unsigned int, wsize,
  399. server->maxBuf - sizeof(WRITE_REQ) + 4);
  400. /* hard limit of CIFS_MAX_WSIZE */
  401. wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE);
  402. return wsize;
  403. }
  404. static unsigned int
  405. cifs_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
  406. {
  407. __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  408. struct TCP_Server_Info *server = tcon->ses->server;
  409. unsigned int rsize, defsize;
  410. /*
  411. * Set default value...
  412. *
  413. * HACK alert! Ancient servers have very small buffers. Even though
  414. * MS-CIFS indicates that servers are only limited by the client's
  415. * bufsize for reads, testing against win98se shows that it throws
  416. * INVALID_PARAMETER errors if you try to request too large a read.
  417. * OS/2 just sends back short reads.
  418. *
  419. * If the server doesn't advertise CAP_LARGE_READ_X, then assume that
  420. * it can't handle a read request larger than its MaxBufferSize either.
  421. */
  422. if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_READ_CAP))
  423. defsize = CIFS_DEFAULT_IOSIZE;
  424. else if (server->capabilities & CAP_LARGE_READ_X)
  425. defsize = CIFS_DEFAULT_NON_POSIX_RSIZE;
  426. else
  427. defsize = server->maxBuf - sizeof(READ_RSP);
  428. rsize = ctx->got_rsize ? ctx->vol_rsize : defsize;
  429. /*
  430. * no CAP_LARGE_READ_X? Then MS-CIFS states that we must limit this to
  431. * the client's MaxBufferSize.
  432. */
  433. if (!(server->capabilities & CAP_LARGE_READ_X))
  434. rsize = min_t(unsigned int, CIFSMaxBufSize, rsize);
  435. /* hard limit of CIFS_MAX_RSIZE */
  436. rsize = min_t(unsigned int, rsize, CIFS_MAX_RSIZE);
  437. return rsize;
  438. }
  439. static void
  440. cifs_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
  441. struct cifs_sb_info *cifs_sb)
  442. {
  443. CIFSSMBQFSDeviceInfo(xid, tcon);
  444. CIFSSMBQFSAttributeInfo(xid, tcon);
  445. }
  446. static int
  447. cifs_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
  448. struct cifs_sb_info *cifs_sb, const char *full_path)
  449. {
  450. int rc;
  451. FILE_ALL_INFO *file_info;
  452. file_info = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  453. if (file_info == NULL)
  454. return -ENOMEM;
  455. rc = CIFSSMBQPathInfo(xid, tcon, full_path, file_info,
  456. 0 /* not legacy */, cifs_sb->local_nls,
  457. cifs_remap(cifs_sb));
  458. if (rc == -EOPNOTSUPP || rc == -EINVAL)
  459. rc = SMBQueryInformation(xid, tcon, full_path, file_info,
  460. cifs_sb->local_nls, cifs_remap(cifs_sb));
  461. kfree(file_info);
  462. return rc;
  463. }
  464. static int cifs_query_path_info(const unsigned int xid,
  465. struct cifs_tcon *tcon,
  466. struct cifs_sb_info *cifs_sb,
  467. const char *full_path,
  468. struct cifs_open_info_data *data)
  469. {
  470. int rc = -EOPNOTSUPP;
  471. FILE_ALL_INFO fi = {};
  472. struct cifs_search_info search_info = {};
  473. bool non_unicode_wildcard = false;
  474. data->reparse_point = false;
  475. data->adjust_tz = false;
  476. /*
  477. * First try CIFSSMBQPathInfo() function which returns more info
  478. * (NumberOfLinks) than CIFSFindFirst() fallback function.
  479. * Some servers like Win9x do not support SMB_QUERY_FILE_ALL_INFO over
  480. * TRANS2_QUERY_PATH_INFORMATION, but supports it with filehandle over
  481. * TRANS2_QUERY_FILE_INFORMATION (function CIFSSMBQFileInfo(). But SMB
  482. * Open command on non-NT servers works only for files, does not work
  483. * for directories. And moreover Win9x SMB server returns bogus data in
  484. * SMB_QUERY_FILE_ALL_INFO Attributes field. So for non-NT servers,
  485. * do not even use CIFSSMBQPathInfo() or CIFSSMBQFileInfo() function.
  486. */
  487. if (tcon->ses->capabilities & CAP_NT_SMBS)
  488. rc = CIFSSMBQPathInfo(xid, tcon, full_path, &fi, 0 /* not legacy */,
  489. cifs_sb->local_nls, cifs_remap(cifs_sb));
  490. /*
  491. * Non-UNICODE variant of fallback functions below expands wildcards,
  492. * so they cannot be used for querying paths with wildcard characters.
  493. */
  494. if (rc && !(tcon->ses->capabilities & CAP_UNICODE) && strpbrk(full_path, "*?\"><"))
  495. non_unicode_wildcard = true;
  496. /*
  497. * Then fallback to CIFSFindFirst() which works also with non-NT servers
  498. * but does not does not provide NumberOfLinks.
  499. */
  500. if ((rc == -EOPNOTSUPP || rc == -EINVAL) &&
  501. !non_unicode_wildcard) {
  502. if (!(tcon->ses->capabilities & tcon->ses->server->vals->cap_nt_find))
  503. search_info.info_level = SMB_FIND_FILE_INFO_STANDARD;
  504. else
  505. search_info.info_level = SMB_FIND_FILE_FULL_DIRECTORY_INFO;
  506. rc = CIFSFindFirst(xid, tcon, full_path, cifs_sb, NULL,
  507. CIFS_SEARCH_CLOSE_ALWAYS | CIFS_SEARCH_CLOSE_AT_END,
  508. &search_info, false);
  509. if (rc == 0) {
  510. if (!(tcon->ses->capabilities & tcon->ses->server->vals->cap_nt_find)) {
  511. FIND_FILE_STANDARD_INFO *di;
  512. int offset = tcon->ses->server->timeAdj;
  513. di = (FIND_FILE_STANDARD_INFO *)search_info.srch_entries_start;
  514. fi.CreationTime = cpu_to_le64(cifs_UnixTimeToNT(cnvrtDosUnixTm(
  515. di->CreationDate, di->CreationTime, offset)));
  516. fi.LastAccessTime = cpu_to_le64(cifs_UnixTimeToNT(cnvrtDosUnixTm(
  517. di->LastAccessDate, di->LastAccessTime, offset)));
  518. fi.LastWriteTime = cpu_to_le64(cifs_UnixTimeToNT(cnvrtDosUnixTm(
  519. di->LastWriteDate, di->LastWriteTime, offset)));
  520. fi.ChangeTime = fi.LastWriteTime;
  521. fi.Attributes = cpu_to_le32(le16_to_cpu(di->Attributes));
  522. fi.AllocationSize = cpu_to_le64(le32_to_cpu(di->AllocationSize));
  523. fi.EndOfFile = cpu_to_le64(le32_to_cpu(di->DataSize));
  524. } else {
  525. FILE_FULL_DIRECTORY_INFO *di;
  526. di = (FILE_FULL_DIRECTORY_INFO *)search_info.srch_entries_start;
  527. fi.CreationTime = di->CreationTime;
  528. fi.LastAccessTime = di->LastAccessTime;
  529. fi.LastWriteTime = di->LastWriteTime;
  530. fi.ChangeTime = di->ChangeTime;
  531. fi.Attributes = di->ExtFileAttributes;
  532. fi.AllocationSize = di->AllocationSize;
  533. fi.EndOfFile = di->EndOfFile;
  534. fi.EASize = di->EaSize;
  535. }
  536. fi.NumberOfLinks = cpu_to_le32(1);
  537. fi.DeletePending = 0;
  538. fi.Directory = !!(le32_to_cpu(fi.Attributes) & ATTR_DIRECTORY);
  539. cifs_buf_release(search_info.ntwrk_buf_start);
  540. } else if (!full_path[0]) {
  541. /*
  542. * CIFSFindFirst() does not work on root path if the
  543. * root path was exported on the server from the top
  544. * level path (drive letter).
  545. */
  546. rc = -EOPNOTSUPP;
  547. }
  548. }
  549. /*
  550. * If everything failed then fallback to the legacy SMB command
  551. * SMB_COM_QUERY_INFORMATION which works with all servers, but
  552. * provide just few information.
  553. */
  554. if ((rc == -EOPNOTSUPP || rc == -EINVAL) && !non_unicode_wildcard) {
  555. rc = SMBQueryInformation(xid, tcon, full_path, &fi, cifs_sb->local_nls,
  556. cifs_remap(cifs_sb));
  557. data->adjust_tz = true;
  558. } else if ((rc == -EOPNOTSUPP || rc == -EINVAL) && non_unicode_wildcard) {
  559. /* Path with non-UNICODE wildcard character cannot exist. */
  560. rc = -ENOENT;
  561. }
  562. if (!rc) {
  563. int tmprc;
  564. int oplock = 0;
  565. struct cifs_fid fid;
  566. struct cifs_open_parms oparms;
  567. move_cifs_info_to_smb2(&data->fi, &fi);
  568. if (!(le32_to_cpu(fi.Attributes) & ATTR_REPARSE))
  569. return 0;
  570. oparms = (struct cifs_open_parms) {
  571. .tcon = tcon,
  572. .cifs_sb = cifs_sb,
  573. .desired_access = FILE_READ_ATTRIBUTES,
  574. .create_options = cifs_create_options(cifs_sb, 0),
  575. .disposition = FILE_OPEN,
  576. .path = full_path,
  577. .fid = &fid,
  578. };
  579. /* Need to check if this is a symbolic link or not */
  580. tmprc = CIFS_open(xid, &oparms, &oplock, NULL);
  581. if (tmprc == -EOPNOTSUPP)
  582. data->reparse_point = true;
  583. else if (tmprc == 0)
  584. CIFSSMBClose(xid, tcon, fid.netfid);
  585. }
  586. #ifdef CONFIG_CIFS_XATTR
  587. /*
  588. * For non-symlink WSL reparse points it is required to fetch
  589. * EA $LXMOD which contains in its S_DT part the mandatory file type.
  590. */
  591. if (!rc && data->reparse_point) {
  592. struct smb2_file_full_ea_info *ea;
  593. u32 next = 0;
  594. ea = (struct smb2_file_full_ea_info *)data->wsl.eas;
  595. do {
  596. ea = (void *)((u8 *)ea + next);
  597. next = le32_to_cpu(ea->next_entry_offset);
  598. } while (next);
  599. if (le16_to_cpu(ea->ea_value_length)) {
  600. ea->next_entry_offset = cpu_to_le32(ALIGN(sizeof(*ea) +
  601. ea->ea_name_length + 1 +
  602. le16_to_cpu(ea->ea_value_length), 4));
  603. ea = (void *)((u8 *)ea + le32_to_cpu(ea->next_entry_offset));
  604. }
  605. rc = CIFSSMBQAllEAs(xid, tcon, full_path, SMB2_WSL_XATTR_MODE,
  606. &ea->ea_data[SMB2_WSL_XATTR_NAME_LEN + 1],
  607. SMB2_WSL_XATTR_MODE_SIZE, cifs_sb);
  608. if (rc == SMB2_WSL_XATTR_MODE_SIZE) {
  609. ea->next_entry_offset = cpu_to_le32(0);
  610. ea->flags = 0;
  611. ea->ea_name_length = SMB2_WSL_XATTR_NAME_LEN;
  612. ea->ea_value_length = cpu_to_le16(SMB2_WSL_XATTR_MODE_SIZE);
  613. memcpy(&ea->ea_data[0], SMB2_WSL_XATTR_MODE, SMB2_WSL_XATTR_NAME_LEN + 1);
  614. data->wsl.eas_len += ALIGN(sizeof(*ea) + SMB2_WSL_XATTR_NAME_LEN + 1 +
  615. SMB2_WSL_XATTR_MODE_SIZE, 4);
  616. rc = 0;
  617. } else if (rc >= 0) {
  618. /* It is an error if EA $LXMOD has wrong size. */
  619. rc = -EINVAL;
  620. } else {
  621. /*
  622. * In all other cases ignore error if fetching
  623. * of EA $LXMOD failed. It is needed only for
  624. * non-symlink WSL reparse points and wsl_to_fattr()
  625. * handle the case when EA is missing.
  626. */
  627. rc = 0;
  628. }
  629. }
  630. /*
  631. * For WSL CHR and BLK reparse points it is required to fetch
  632. * EA $LXDEV which contains major and minor device numbers.
  633. */
  634. if (!rc && data->reparse_point) {
  635. struct smb2_file_full_ea_info *ea;
  636. u32 next = 0;
  637. ea = (struct smb2_file_full_ea_info *)data->wsl.eas;
  638. do {
  639. ea = (void *)((u8 *)ea + next);
  640. next = le32_to_cpu(ea->next_entry_offset);
  641. } while (next);
  642. if (le16_to_cpu(ea->ea_value_length)) {
  643. ea->next_entry_offset = cpu_to_le32(ALIGN(sizeof(*ea) +
  644. ea->ea_name_length + 1 +
  645. le16_to_cpu(ea->ea_value_length), 4));
  646. ea = (void *)((u8 *)ea + le32_to_cpu(ea->next_entry_offset));
  647. }
  648. rc = CIFSSMBQAllEAs(xid, tcon, full_path, SMB2_WSL_XATTR_DEV,
  649. &ea->ea_data[SMB2_WSL_XATTR_NAME_LEN + 1],
  650. SMB2_WSL_XATTR_DEV_SIZE, cifs_sb);
  651. if (rc == SMB2_WSL_XATTR_DEV_SIZE) {
  652. ea->next_entry_offset = cpu_to_le32(0);
  653. ea->flags = 0;
  654. ea->ea_name_length = SMB2_WSL_XATTR_NAME_LEN;
  655. ea->ea_value_length = cpu_to_le16(SMB2_WSL_XATTR_DEV_SIZE);
  656. memcpy(&ea->ea_data[0], SMB2_WSL_XATTR_DEV, SMB2_WSL_XATTR_NAME_LEN + 1);
  657. data->wsl.eas_len += ALIGN(sizeof(*ea) + SMB2_WSL_XATTR_NAME_LEN + 1 +
  658. SMB2_WSL_XATTR_MODE_SIZE, 4);
  659. rc = 0;
  660. } else if (rc >= 0) {
  661. /* It is an error if EA $LXDEV has wrong size. */
  662. rc = -EINVAL;
  663. } else {
  664. /*
  665. * In all other cases ignore error if fetching
  666. * of EA $LXDEV failed. It is needed only for
  667. * WSL CHR and BLK reparse points and wsl_to_fattr()
  668. * handle the case when EA is missing.
  669. */
  670. rc = 0;
  671. }
  672. }
  673. #endif
  674. return rc;
  675. }
  676. static int cifs_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
  677. struct cifs_sb_info *cifs_sb, const char *full_path,
  678. u64 *uniqueid, struct cifs_open_info_data *unused)
  679. {
  680. /*
  681. * We can not use the IndexNumber field by default from Windows or
  682. * Samba (in ALL_INFO buf) but we can request it explicitly. The SNIA
  683. * CIFS spec claims that this value is unique within the scope of a
  684. * share, and the windows docs hint that it's actually unique
  685. * per-machine.
  686. *
  687. * There may be higher info levels that work but are there Windows
  688. * server or network appliances for which IndexNumber field is not
  689. * guaranteed unique?
  690. */
  691. return CIFSGetSrvInodeNumber(xid, tcon, full_path, uniqueid,
  692. cifs_sb->local_nls,
  693. cifs_remap(cifs_sb));
  694. }
  695. static int cifs_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
  696. struct cifsFileInfo *cfile, struct cifs_open_info_data *data)
  697. {
  698. int rc;
  699. FILE_ALL_INFO fi = {};
  700. /*
  701. * CIFSSMBQFileInfo() for non-NT servers returns bogus data in
  702. * Attributes fields. So do not use this command for non-NT servers.
  703. */
  704. if (!(tcon->ses->capabilities & CAP_NT_SMBS))
  705. return -EOPNOTSUPP;
  706. if (cfile->symlink_target) {
  707. data->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
  708. if (!data->symlink_target)
  709. return -ENOMEM;
  710. }
  711. rc = CIFSSMBQFileInfo(xid, tcon, cfile->fid.netfid, &fi);
  712. if (!rc)
  713. move_cifs_info_to_smb2(&data->fi, &fi);
  714. return rc;
  715. }
  716. static void
  717. cifs_clear_stats(struct cifs_tcon *tcon)
  718. {
  719. atomic_set(&tcon->stats.cifs_stats.num_writes, 0);
  720. atomic_set(&tcon->stats.cifs_stats.num_reads, 0);
  721. atomic_set(&tcon->stats.cifs_stats.num_flushes, 0);
  722. atomic_set(&tcon->stats.cifs_stats.num_oplock_brks, 0);
  723. atomic_set(&tcon->stats.cifs_stats.num_opens, 0);
  724. atomic_set(&tcon->stats.cifs_stats.num_posixopens, 0);
  725. atomic_set(&tcon->stats.cifs_stats.num_posixmkdirs, 0);
  726. atomic_set(&tcon->stats.cifs_stats.num_closes, 0);
  727. atomic_set(&tcon->stats.cifs_stats.num_deletes, 0);
  728. atomic_set(&tcon->stats.cifs_stats.num_mkdirs, 0);
  729. atomic_set(&tcon->stats.cifs_stats.num_rmdirs, 0);
  730. atomic_set(&tcon->stats.cifs_stats.num_renames, 0);
  731. atomic_set(&tcon->stats.cifs_stats.num_t2renames, 0);
  732. atomic_set(&tcon->stats.cifs_stats.num_ffirst, 0);
  733. atomic_set(&tcon->stats.cifs_stats.num_fnext, 0);
  734. atomic_set(&tcon->stats.cifs_stats.num_fclose, 0);
  735. atomic_set(&tcon->stats.cifs_stats.num_hardlinks, 0);
  736. atomic_set(&tcon->stats.cifs_stats.num_symlinks, 0);
  737. atomic_set(&tcon->stats.cifs_stats.num_locks, 0);
  738. atomic_set(&tcon->stats.cifs_stats.num_acl_get, 0);
  739. atomic_set(&tcon->stats.cifs_stats.num_acl_set, 0);
  740. }
  741. static void
  742. cifs_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
  743. {
  744. seq_printf(m, " Oplocks breaks: %d",
  745. atomic_read(&tcon->stats.cifs_stats.num_oplock_brks));
  746. seq_printf(m, "\nReads: %d Bytes: %llu",
  747. atomic_read(&tcon->stats.cifs_stats.num_reads),
  748. (long long)(tcon->bytes_read));
  749. seq_printf(m, "\nWrites: %d Bytes: %llu",
  750. atomic_read(&tcon->stats.cifs_stats.num_writes),
  751. (long long)(tcon->bytes_written));
  752. seq_printf(m, "\nFlushes: %d",
  753. atomic_read(&tcon->stats.cifs_stats.num_flushes));
  754. seq_printf(m, "\nLocks: %d HardLinks: %d Symlinks: %d",
  755. atomic_read(&tcon->stats.cifs_stats.num_locks),
  756. atomic_read(&tcon->stats.cifs_stats.num_hardlinks),
  757. atomic_read(&tcon->stats.cifs_stats.num_symlinks));
  758. seq_printf(m, "\nOpens: %d Closes: %d Deletes: %d",
  759. atomic_read(&tcon->stats.cifs_stats.num_opens),
  760. atomic_read(&tcon->stats.cifs_stats.num_closes),
  761. atomic_read(&tcon->stats.cifs_stats.num_deletes));
  762. seq_printf(m, "\nPosix Opens: %d Posix Mkdirs: %d",
  763. atomic_read(&tcon->stats.cifs_stats.num_posixopens),
  764. atomic_read(&tcon->stats.cifs_stats.num_posixmkdirs));
  765. seq_printf(m, "\nMkdirs: %d Rmdirs: %d",
  766. atomic_read(&tcon->stats.cifs_stats.num_mkdirs),
  767. atomic_read(&tcon->stats.cifs_stats.num_rmdirs));
  768. seq_printf(m, "\nRenames: %d T2 Renames %d",
  769. atomic_read(&tcon->stats.cifs_stats.num_renames),
  770. atomic_read(&tcon->stats.cifs_stats.num_t2renames));
  771. seq_printf(m, "\nFindFirst: %d FNext %d FClose %d",
  772. atomic_read(&tcon->stats.cifs_stats.num_ffirst),
  773. atomic_read(&tcon->stats.cifs_stats.num_fnext),
  774. atomic_read(&tcon->stats.cifs_stats.num_fclose));
  775. }
  776. static void
  777. cifs_mkdir_setinfo(struct inode *inode, const char *full_path,
  778. struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
  779. const unsigned int xid)
  780. {
  781. FILE_BASIC_INFO info;
  782. struct cifsInodeInfo *cifsInode;
  783. u32 dosattrs;
  784. int rc;
  785. memset(&info, 0, sizeof(info));
  786. cifsInode = CIFS_I(inode);
  787. dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
  788. info.Attributes = cpu_to_le32(dosattrs);
  789. rc = CIFSSMBSetPathInfo(xid, tcon, full_path, &info, cifs_sb->local_nls,
  790. cifs_sb);
  791. if (rc == 0)
  792. cifsInode->cifsAttrs = dosattrs;
  793. }
  794. static int cifs_open_file(const unsigned int xid, struct cifs_open_parms *oparms, __u32 *oplock,
  795. void *buf)
  796. {
  797. struct cifs_open_info_data *data = buf;
  798. FILE_ALL_INFO fi = {};
  799. int rc;
  800. if (!(oparms->tcon->ses->capabilities & CAP_NT_SMBS))
  801. rc = SMBLegacyOpen(xid, oparms->tcon, oparms->path,
  802. oparms->disposition,
  803. oparms->desired_access,
  804. oparms->create_options,
  805. &oparms->fid->netfid, oplock, &fi,
  806. oparms->cifs_sb->local_nls,
  807. cifs_remap(oparms->cifs_sb));
  808. else
  809. rc = CIFS_open(xid, oparms, oplock, &fi);
  810. if (!rc && data)
  811. move_cifs_info_to_smb2(&data->fi, &fi);
  812. return rc;
  813. }
  814. static void
  815. cifs_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
  816. {
  817. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  818. cfile->fid.netfid = fid->netfid;
  819. cifs_set_oplock_level(cinode, oplock);
  820. cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
  821. }
  822. static int
  823. cifs_close_file(const unsigned int xid, struct cifs_tcon *tcon,
  824. struct cifs_fid *fid)
  825. {
  826. return CIFSSMBClose(xid, tcon, fid->netfid);
  827. }
  828. static int
  829. cifs_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
  830. struct cifs_fid *fid)
  831. {
  832. return CIFSSMBFlush(xid, tcon, fid->netfid);
  833. }
  834. static int
  835. cifs_sync_read(const unsigned int xid, struct cifs_fid *pfid,
  836. struct cifs_io_parms *parms, unsigned int *bytes_read,
  837. char **buf, int *buf_type)
  838. {
  839. parms->netfid = pfid->netfid;
  840. return CIFSSMBRead(xid, parms, bytes_read, buf, buf_type);
  841. }
  842. static int
  843. cifs_sync_write(const unsigned int xid, struct cifs_fid *pfid,
  844. struct cifs_io_parms *parms, unsigned int *written,
  845. struct kvec *iov, unsigned long nr_segs)
  846. {
  847. parms->netfid = pfid->netfid;
  848. return CIFSSMBWrite2(xid, parms, written, iov, nr_segs);
  849. }
  850. static int
  851. smb_set_file_info(struct inode *inode, const char *full_path,
  852. FILE_BASIC_INFO *buf, const unsigned int xid)
  853. {
  854. int oplock = 0;
  855. int rc;
  856. __u32 netpid;
  857. struct cifs_fid fid;
  858. struct cifs_open_parms oparms;
  859. struct cifsFileInfo *open_file;
  860. FILE_BASIC_INFO new_buf;
  861. struct cifs_open_info_data query_data;
  862. __le64 write_time = buf->LastWriteTime;
  863. struct cifsInodeInfo *cinode = CIFS_I(inode);
  864. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  865. struct tcon_link *tlink = NULL;
  866. struct cifs_tcon *tcon;
  867. /* if the file is already open for write, just use that fileid */
  868. open_file = find_writable_file(cinode, FIND_WR_FSUID_ONLY);
  869. if (open_file) {
  870. fid.netfid = open_file->fid.netfid;
  871. netpid = open_file->pid;
  872. tcon = tlink_tcon(open_file->tlink);
  873. } else {
  874. tlink = cifs_sb_tlink(cifs_sb);
  875. if (IS_ERR(tlink)) {
  876. rc = PTR_ERR(tlink);
  877. tlink = NULL;
  878. goto out;
  879. }
  880. tcon = tlink_tcon(tlink);
  881. }
  882. /*
  883. * Non-NT servers interprets zero time value in SMB_SET_FILE_BASIC_INFO
  884. * over TRANS2_SET_FILE_INFORMATION as a valid time value. NT servers
  885. * interprets zero time value as do not change existing value on server.
  886. * API of ->set_file_info() callback expects that zero time value has
  887. * the NT meaning - do not change. Therefore if server is non-NT and
  888. * some time values in "buf" are zero, then fetch missing time values.
  889. */
  890. if (!(tcon->ses->capabilities & CAP_NT_SMBS) &&
  891. (!buf->CreationTime || !buf->LastAccessTime ||
  892. !buf->LastWriteTime || !buf->ChangeTime)) {
  893. rc = cifs_query_path_info(xid, tcon, cifs_sb, full_path, &query_data);
  894. if (rc) {
  895. if (open_file) {
  896. cifsFileInfo_put(open_file);
  897. open_file = NULL;
  898. }
  899. goto out;
  900. }
  901. /*
  902. * Original write_time from buf->LastWriteTime is preserved
  903. * as SMBSetInformation() interprets zero as do not change.
  904. */
  905. new_buf = *buf;
  906. buf = &new_buf;
  907. if (!buf->CreationTime)
  908. buf->CreationTime = query_data.fi.CreationTime;
  909. if (!buf->LastAccessTime)
  910. buf->LastAccessTime = query_data.fi.LastAccessTime;
  911. if (!buf->LastWriteTime)
  912. buf->LastWriteTime = query_data.fi.LastWriteTime;
  913. if (!buf->ChangeTime)
  914. buf->ChangeTime = query_data.fi.ChangeTime;
  915. }
  916. if (open_file)
  917. goto set_via_filehandle;
  918. rc = CIFSSMBSetPathInfo(xid, tcon, full_path, buf, cifs_sb->local_nls,
  919. cifs_sb);
  920. if (rc == 0) {
  921. cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
  922. goto out;
  923. } else if (rc != -EOPNOTSUPP && rc != -EINVAL) {
  924. goto out;
  925. }
  926. oparms = (struct cifs_open_parms) {
  927. .tcon = tcon,
  928. .cifs_sb = cifs_sb,
  929. .desired_access = SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
  930. .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
  931. .disposition = FILE_OPEN,
  932. .path = full_path,
  933. .fid = &fid,
  934. };
  935. if (S_ISDIR(inode->i_mode) && !(tcon->ses->capabilities & CAP_NT_SMBS)) {
  936. /* Opening directory path is not possible on non-NT servers. */
  937. rc = -EOPNOTSUPP;
  938. } else {
  939. /*
  940. * Use cifs_open_file() instead of CIFS_open() as the
  941. * cifs_open_file() selects the correct function which
  942. * works also on non-NT servers.
  943. */
  944. rc = cifs_open_file(xid, &oparms, &oplock, NULL);
  945. /*
  946. * Opening path for writing on non-NT servers is not
  947. * possible when the read-only attribute is already set.
  948. * Non-NT server in this case returns -EACCES. For those
  949. * servers the only possible way how to clear the read-only
  950. * bit is via SMB_COM_SETATTR command.
  951. */
  952. if (rc == -EACCES &&
  953. (cinode->cifsAttrs & ATTR_READONLY) &&
  954. le32_to_cpu(buf->Attributes) != 0 && /* 0 = do not change attrs */
  955. !(le32_to_cpu(buf->Attributes) & ATTR_READONLY) &&
  956. !(tcon->ses->capabilities & CAP_NT_SMBS))
  957. rc = -EOPNOTSUPP;
  958. }
  959. /* Fallback to SMB_COM_SETATTR command when absolutelty needed. */
  960. if (rc == -EOPNOTSUPP) {
  961. cifs_dbg(FYI, "calling SetInformation since SetPathInfo for attrs/times not supported by this server\n");
  962. rc = SMBSetInformation(xid, tcon, full_path,
  963. buf->Attributes != 0 ? buf->Attributes : cpu_to_le32(cinode->cifsAttrs),
  964. write_time,
  965. cifs_sb->local_nls, cifs_sb);
  966. if (rc == 0)
  967. cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
  968. else
  969. rc = -EACCES;
  970. goto out;
  971. }
  972. if (rc != 0) {
  973. if (rc == -EIO)
  974. rc = -EINVAL;
  975. goto out;
  976. }
  977. netpid = current->tgid;
  978. cifs_dbg(FYI, "calling SetFileInfo since SetPathInfo for attrs/times not supported by this server\n");
  979. set_via_filehandle:
  980. rc = CIFSSMBSetFileInfo(xid, tcon, buf, fid.netfid, netpid);
  981. if (!rc)
  982. cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
  983. if (open_file == NULL)
  984. CIFSSMBClose(xid, tcon, fid.netfid);
  985. else
  986. cifsFileInfo_put(open_file);
  987. /*
  988. * Setting the read-only bit is not honered on non-NT servers when done
  989. * via open-semantics. So for setting it, use SMB_COM_SETATTR command.
  990. * This command works only after the file is closed, so use it only when
  991. * operation was called without the filehandle.
  992. */
  993. if (open_file == NULL &&
  994. !(tcon->ses->capabilities & CAP_NT_SMBS) &&
  995. le32_to_cpu(buf->Attributes) & ATTR_READONLY) {
  996. SMBSetInformation(xid, tcon, full_path,
  997. buf->Attributes,
  998. 0 /* do not change write time */,
  999. cifs_sb->local_nls, cifs_sb);
  1000. }
  1001. out:
  1002. if (tlink != NULL)
  1003. cifs_put_tlink(tlink);
  1004. return rc;
  1005. }
  1006. static int
  1007. cifs_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
  1008. struct cifsFileInfo *cfile)
  1009. {
  1010. return CIFSSMB_set_compression(xid, tcon, cfile->fid.netfid);
  1011. }
  1012. static int
  1013. cifs_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
  1014. const char *path, struct cifs_sb_info *cifs_sb,
  1015. struct cifs_fid *fid, __u16 search_flags,
  1016. struct cifs_search_info *srch_inf)
  1017. {
  1018. int rc;
  1019. rc = CIFSFindFirst(xid, tcon, path, cifs_sb,
  1020. &fid->netfid, search_flags, srch_inf, true);
  1021. if (rc)
  1022. cifs_dbg(FYI, "find first failed=%d\n", rc);
  1023. return rc;
  1024. }
  1025. static int
  1026. cifs_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
  1027. struct cifs_fid *fid, __u16 search_flags,
  1028. struct cifs_search_info *srch_inf)
  1029. {
  1030. return CIFSFindNext(xid, tcon, fid->netfid, search_flags, srch_inf);
  1031. }
  1032. static int
  1033. cifs_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
  1034. struct cifs_fid *fid)
  1035. {
  1036. return CIFSFindClose(xid, tcon, fid->netfid);
  1037. }
  1038. static int
  1039. cifs_oplock_response(struct cifs_tcon *tcon, __u64 persistent_fid,
  1040. __u64 volatile_fid, __u16 net_fid, struct cifsInodeInfo *cinode)
  1041. {
  1042. return CIFSSMBLock(0, tcon, net_fid, current->tgid, 0, 0, 0, 0,
  1043. LOCKING_ANDX_OPLOCK_RELEASE, false, CIFS_CACHE_READ(cinode) ? 1 : 0);
  1044. }
  1045. static int
  1046. cifs_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
  1047. const char *path, struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
  1048. {
  1049. int rc = -EOPNOTSUPP;
  1050. buf->f_type = CIFS_SUPER_MAGIC;
  1051. /*
  1052. * We could add a second check for a QFS Unix capability bit
  1053. */
  1054. if ((tcon->ses->capabilities & CAP_UNIX) &&
  1055. (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
  1056. rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
  1057. /*
  1058. * Only need to call the old QFSInfo if failed on newer one,
  1059. * e.g. by OS/2.
  1060. **/
  1061. if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
  1062. rc = CIFSSMBQFSInfo(xid, tcon, buf);
  1063. /*
  1064. * Some old Windows servers also do not support level 103, retry with
  1065. * older level one if old server failed the previous call or we
  1066. * bypassed it because we detected that this was an older LANMAN sess
  1067. */
  1068. if (rc)
  1069. rc = SMBOldQFSInfo(xid, tcon, buf);
  1070. return rc;
  1071. }
  1072. static int
  1073. cifs_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
  1074. __u64 length, __u32 type, int lock, int unlock, bool wait)
  1075. {
  1076. return CIFSSMBLock(xid, tlink_tcon(cfile->tlink), cfile->fid.netfid,
  1077. current->tgid, length, offset, unlock, lock,
  1078. (__u8)type, wait, 0);
  1079. }
  1080. static int
  1081. cifs_unix_dfs_readlink(const unsigned int xid, struct cifs_tcon *tcon,
  1082. const unsigned char *searchName, char **symlinkinfo,
  1083. const struct nls_table *nls_codepage)
  1084. {
  1085. #ifdef CONFIG_CIFS_DFS_UPCALL
  1086. int rc;
  1087. struct dfs_info3_param referral = {0};
  1088. rc = get_dfs_path(xid, tcon->ses, searchName, nls_codepage, &referral,
  1089. 0);
  1090. if (!rc) {
  1091. *symlinkinfo = kstrdup(referral.node_name, GFP_KERNEL);
  1092. free_dfs_info_param(&referral);
  1093. if (!*symlinkinfo)
  1094. rc = -ENOMEM;
  1095. }
  1096. return rc;
  1097. #else /* No DFS support */
  1098. return -EREMOTE;
  1099. #endif
  1100. }
  1101. static int cifs_query_symlink(const unsigned int xid,
  1102. struct cifs_tcon *tcon,
  1103. struct cifs_sb_info *cifs_sb,
  1104. const char *full_path,
  1105. char **target_path)
  1106. {
  1107. int rc;
  1108. cifs_tcon_dbg(FYI, "%s: path=%s\n", __func__, full_path);
  1109. if (!cap_unix(tcon->ses))
  1110. return -EOPNOTSUPP;
  1111. rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, target_path,
  1112. cifs_sb->local_nls, cifs_remap(cifs_sb));
  1113. if (rc == -EREMOTE)
  1114. rc = cifs_unix_dfs_readlink(xid, tcon, full_path,
  1115. target_path, cifs_sb->local_nls);
  1116. return rc;
  1117. }
  1118. static int cifs_parse_reparse_point(struct cifs_sb_info *cifs_sb,
  1119. const char *full_path,
  1120. struct kvec *rsp_iov,
  1121. struct cifs_open_info_data *data)
  1122. {
  1123. struct reparse_data_buffer *buf;
  1124. TRANSACT_IOCTL_RSP *io = rsp_iov->iov_base;
  1125. u32 plen = le16_to_cpu(io->ByteCount);
  1126. buf = (struct reparse_data_buffer *)((__u8 *)&io->hdr.Protocol +
  1127. le32_to_cpu(io->DataOffset));
  1128. return parse_reparse_point(buf, plen, cifs_sb, full_path, true, data);
  1129. }
  1130. static bool
  1131. cifs_is_read_op(__u32 oplock)
  1132. {
  1133. return oplock == OPLOCK_READ;
  1134. }
  1135. static unsigned int
  1136. cifs_wp_retry_size(struct inode *inode)
  1137. {
  1138. return CIFS_SB(inode->i_sb)->ctx->wsize;
  1139. }
  1140. static bool
  1141. cifs_dir_needs_close(struct cifsFileInfo *cfile)
  1142. {
  1143. return !cfile->srch_inf.endOfSearch && !cfile->invalidHandle;
  1144. }
  1145. static bool
  1146. cifs_can_echo(struct TCP_Server_Info *server)
  1147. {
  1148. if (server->tcpStatus == CifsGood)
  1149. return true;
  1150. return false;
  1151. }
  1152. static int
  1153. cifs_make_node(unsigned int xid, struct inode *inode,
  1154. struct dentry *dentry, struct cifs_tcon *tcon,
  1155. const char *full_path, umode_t mode, dev_t dev)
  1156. {
  1157. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1158. struct inode *newinode = NULL;
  1159. int rc;
  1160. if (tcon->unix_ext) {
  1161. /*
  1162. * SMB1 Unix Extensions: requires server support but
  1163. * works with all special files
  1164. */
  1165. struct cifs_unix_set_info_args args = {
  1166. .mode = mode & ~current_umask(),
  1167. .ctime = NO_CHANGE_64,
  1168. .atime = NO_CHANGE_64,
  1169. .mtime = NO_CHANGE_64,
  1170. .device = dev,
  1171. };
  1172. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  1173. args.uid = current_fsuid();
  1174. args.gid = current_fsgid();
  1175. } else {
  1176. args.uid = INVALID_UID; /* no change */
  1177. args.gid = INVALID_GID; /* no change */
  1178. }
  1179. rc = CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
  1180. cifs_sb->local_nls,
  1181. cifs_remap(cifs_sb));
  1182. if (rc)
  1183. return rc;
  1184. rc = cifs_get_inode_info_unix(&newinode, full_path,
  1185. inode->i_sb, xid);
  1186. if (rc == 0)
  1187. d_instantiate(dentry, newinode);
  1188. return rc;
  1189. }
  1190. /*
  1191. * Check if mounted with mount parm 'sfu' mount parm.
  1192. * SFU emulation should work with all servers, but only
  1193. * supports block and char device, socket & fifo,
  1194. * and was used by default in earlier versions of Windows
  1195. */
  1196. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
  1197. return -EPERM;
  1198. return cifs_sfu_make_node(xid, inode, dentry, tcon,
  1199. full_path, mode, dev);
  1200. }
  1201. struct smb_version_operations smb1_operations = {
  1202. .send_cancel = send_nt_cancel,
  1203. .compare_fids = cifs_compare_fids,
  1204. .setup_request = cifs_setup_request,
  1205. .setup_async_request = cifs_setup_async_request,
  1206. .check_receive = cifs_check_receive,
  1207. .add_credits = cifs_add_credits,
  1208. .set_credits = cifs_set_credits,
  1209. .get_credits_field = cifs_get_credits_field,
  1210. .get_credits = cifs_get_credits,
  1211. .wait_mtu_credits = cifs_wait_mtu_credits,
  1212. .get_next_mid = cifs_get_next_mid,
  1213. .read_data_offset = cifs_read_data_offset,
  1214. .read_data_length = cifs_read_data_length,
  1215. .map_error = map_smb_to_linux_error,
  1216. .find_mid = cifs_find_mid,
  1217. .check_message = checkSMB,
  1218. .dump_detail = cifs_dump_detail,
  1219. .clear_stats = cifs_clear_stats,
  1220. .print_stats = cifs_print_stats,
  1221. .is_oplock_break = is_valid_oplock_break,
  1222. .downgrade_oplock = cifs_downgrade_oplock,
  1223. .check_trans2 = cifs_check_trans2,
  1224. .need_neg = cifs_need_neg,
  1225. .negotiate = cifs_negotiate,
  1226. .negotiate_wsize = cifs_negotiate_wsize,
  1227. .negotiate_rsize = cifs_negotiate_rsize,
  1228. .sess_setup = CIFS_SessSetup,
  1229. .logoff = CIFSSMBLogoff,
  1230. .tree_connect = CIFSTCon,
  1231. .tree_disconnect = CIFSSMBTDis,
  1232. .get_dfs_refer = CIFSGetDFSRefer,
  1233. .qfs_tcon = cifs_qfs_tcon,
  1234. .is_path_accessible = cifs_is_path_accessible,
  1235. .can_echo = cifs_can_echo,
  1236. .query_path_info = cifs_query_path_info,
  1237. .query_reparse_point = cifs_query_reparse_point,
  1238. .query_file_info = cifs_query_file_info,
  1239. .get_srv_inum = cifs_get_srv_inum,
  1240. .set_path_size = CIFSSMBSetEOF,
  1241. .set_file_size = CIFSSMBSetFileSize,
  1242. .set_file_info = smb_set_file_info,
  1243. .set_compression = cifs_set_compression,
  1244. .echo = CIFSSMBEcho,
  1245. .mkdir = CIFSSMBMkDir,
  1246. .mkdir_setinfo = cifs_mkdir_setinfo,
  1247. .rmdir = CIFSSMBRmDir,
  1248. .unlink = CIFSSMBDelFile,
  1249. .rename_pending_delete = cifs_rename_pending_delete,
  1250. .rename = CIFSSMBRename,
  1251. .create_hardlink = CIFSCreateHardLink,
  1252. .query_symlink = cifs_query_symlink,
  1253. .parse_reparse_point = cifs_parse_reparse_point,
  1254. .open = cifs_open_file,
  1255. .set_fid = cifs_set_fid,
  1256. .close = cifs_close_file,
  1257. .flush = cifs_flush_file,
  1258. .async_readv = cifs_async_readv,
  1259. .async_writev = cifs_async_writev,
  1260. .sync_read = cifs_sync_read,
  1261. .sync_write = cifs_sync_write,
  1262. .query_dir_first = cifs_query_dir_first,
  1263. .query_dir_next = cifs_query_dir_next,
  1264. .close_dir = cifs_close_dir,
  1265. .calc_smb_size = smbCalcSize,
  1266. .oplock_response = cifs_oplock_response,
  1267. .queryfs = cifs_queryfs,
  1268. .mand_lock = cifs_mand_lock,
  1269. .mand_unlock_range = cifs_unlock_range,
  1270. .push_mand_locks = cifs_push_mandatory_locks,
  1271. .query_mf_symlink = cifs_query_mf_symlink,
  1272. .create_mf_symlink = cifs_create_mf_symlink,
  1273. .is_read_op = cifs_is_read_op,
  1274. .wp_retry_size = cifs_wp_retry_size,
  1275. .dir_needs_close = cifs_dir_needs_close,
  1276. .select_sectype = cifs_select_sectype,
  1277. #ifdef CONFIG_CIFS_XATTR
  1278. .query_all_EAs = CIFSSMBQAllEAs,
  1279. .set_EA = CIFSSMBSetEA,
  1280. #endif /* CIFS_XATTR */
  1281. .get_acl = get_cifs_acl,
  1282. .get_acl_by_fid = get_cifs_acl_by_fid,
  1283. .set_acl = set_cifs_acl,
  1284. .make_node = cifs_make_node,
  1285. };
  1286. struct smb_version_values smb1_values = {
  1287. .version_string = SMB1_VERSION_STRING,
  1288. .protocol_id = SMB10_PROT_ID,
  1289. .large_lock_type = LOCKING_ANDX_LARGE_FILES,
  1290. .exclusive_lock_type = 0,
  1291. .shared_lock_type = LOCKING_ANDX_SHARED_LOCK,
  1292. .unlock_lock_type = 0,
  1293. .header_preamble_size = 4,
  1294. .header_size = sizeof(struct smb_hdr),
  1295. .max_header_size = MAX_CIFS_HDR_SIZE,
  1296. .read_rsp_size = sizeof(READ_RSP),
  1297. .lock_cmd = cpu_to_le16(SMB_COM_LOCKING_ANDX),
  1298. .cap_unix = CAP_UNIX,
  1299. .cap_nt_find = CAP_NT_SMBS | CAP_NT_FIND,
  1300. .cap_large_files = CAP_LARGE_FILES,
  1301. .signing_enabled = SECMODE_SIGN_ENABLED,
  1302. .signing_required = SECMODE_SIGN_REQUIRED,
  1303. };