smb1ops.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. /*
  2. * SMB1 (CIFS) version specific operations
  3. *
  4. * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
  5. *
  6. * This library is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License v2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/pagemap.h>
  20. #include "cifsglob.h"
  21. #include "cifsproto.h"
  22. #include "cifs_debug.h"
  23. #include "cifspdu.h"
  24. /*
  25. * An NT cancel request header looks just like the original request except:
  26. *
  27. * The Command is SMB_COM_NT_CANCEL
  28. * The WordCount is zeroed out
  29. * The ByteCount is zeroed out
  30. *
  31. * This function mangles an existing request buffer into a
  32. * SMB_COM_NT_CANCEL request and then sends it.
  33. */
  34. static int
  35. send_nt_cancel(struct TCP_Server_Info *server, void *buf,
  36. struct mid_q_entry *mid)
  37. {
  38. int rc = 0;
  39. struct smb_hdr *in_buf = (struct smb_hdr *)buf;
  40. /* -4 for RFC1001 length and +2 for BCC field */
  41. in_buf->smb_buf_length = cpu_to_be32(sizeof(struct smb_hdr) - 4 + 2);
  42. in_buf->Command = SMB_COM_NT_CANCEL;
  43. in_buf->WordCount = 0;
  44. put_bcc(0, in_buf);
  45. mutex_lock(&server->srv_mutex);
  46. rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
  47. if (rc) {
  48. mutex_unlock(&server->srv_mutex);
  49. return rc;
  50. }
  51. rc = smb_send(server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
  52. mutex_unlock(&server->srv_mutex);
  53. cFYI(1, "issued NT_CANCEL for mid %u, rc = %d",
  54. in_buf->Mid, 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)
  70. {
  71. READ_RSP *rsp = (READ_RSP *)buf;
  72. return (le16_to_cpu(rsp->DataLengthHigh) << 16) +
  73. le16_to_cpu(rsp->DataLength);
  74. }
  75. static struct mid_q_entry *
  76. cifs_find_mid(struct TCP_Server_Info *server, char *buffer)
  77. {
  78. struct smb_hdr *buf = (struct smb_hdr *)buffer;
  79. struct mid_q_entry *mid;
  80. spin_lock(&GlobalMid_Lock);
  81. list_for_each_entry(mid, &server->pending_mid_q, qhead) {
  82. if (mid->mid == buf->Mid &&
  83. mid->mid_state == MID_REQUEST_SUBMITTED &&
  84. le16_to_cpu(mid->command) == buf->Command) {
  85. spin_unlock(&GlobalMid_Lock);
  86. return mid;
  87. }
  88. }
  89. spin_unlock(&GlobalMid_Lock);
  90. return NULL;
  91. }
  92. static void
  93. cifs_add_credits(struct TCP_Server_Info *server, const unsigned int add,
  94. const int optype)
  95. {
  96. spin_lock(&server->req_lock);
  97. server->credits += add;
  98. server->in_flight--;
  99. spin_unlock(&server->req_lock);
  100. wake_up(&server->request_q);
  101. }
  102. static void
  103. cifs_set_credits(struct TCP_Server_Info *server, const int val)
  104. {
  105. spin_lock(&server->req_lock);
  106. server->credits = val;
  107. server->oplocks = val > 1 ? enable_oplocks : false;
  108. spin_unlock(&server->req_lock);
  109. }
  110. static int *
  111. cifs_get_credits_field(struct TCP_Server_Info *server, const int optype)
  112. {
  113. return &server->credits;
  114. }
  115. static unsigned int
  116. cifs_get_credits(struct mid_q_entry *mid)
  117. {
  118. return 1;
  119. }
  120. /*
  121. * Find a free multiplex id (SMB mid). Otherwise there could be
  122. * mid collisions which might cause problems, demultiplexing the
  123. * wrong response to this request. Multiplex ids could collide if
  124. * one of a series requests takes much longer than the others, or
  125. * if a very large number of long lived requests (byte range
  126. * locks or FindNotify requests) are pending. No more than
  127. * 64K-1 requests can be outstanding at one time. If no
  128. * mids are available, return zero. A future optimization
  129. * could make the combination of mids and uid the key we use
  130. * to demultiplex on (rather than mid alone).
  131. * In addition to the above check, the cifs demultiplex
  132. * code already used the command code as a secondary
  133. * check of the frame and if signing is negotiated the
  134. * response would be discarded if the mid were the same
  135. * but the signature was wrong. Since the mid is not put in the
  136. * pending queue until later (when it is about to be dispatched)
  137. * we do have to limit the number of outstanding requests
  138. * to somewhat less than 64K-1 although it is hard to imagine
  139. * so many threads being in the vfs at one time.
  140. */
  141. static __u64
  142. cifs_get_next_mid(struct TCP_Server_Info *server)
  143. {
  144. __u64 mid = 0;
  145. __u16 last_mid, cur_mid;
  146. bool collision;
  147. spin_lock(&GlobalMid_Lock);
  148. /* mid is 16 bit only for CIFS/SMB */
  149. cur_mid = (__u16)((server->CurrentMid) & 0xffff);
  150. /* we do not want to loop forever */
  151. last_mid = cur_mid;
  152. cur_mid++;
  153. /*
  154. * This nested loop looks more expensive than it is.
  155. * In practice the list of pending requests is short,
  156. * fewer than 50, and the mids are likely to be unique
  157. * on the first pass through the loop unless some request
  158. * takes longer than the 64 thousand requests before it
  159. * (and it would also have to have been a request that
  160. * did not time out).
  161. */
  162. while (cur_mid != last_mid) {
  163. struct mid_q_entry *mid_entry;
  164. unsigned int num_mids;
  165. collision = false;
  166. if (cur_mid == 0)
  167. cur_mid++;
  168. num_mids = 0;
  169. list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
  170. ++num_mids;
  171. if (mid_entry->mid == cur_mid &&
  172. mid_entry->mid_state == MID_REQUEST_SUBMITTED) {
  173. /* This mid is in use, try a different one */
  174. collision = true;
  175. break;
  176. }
  177. }
  178. /*
  179. * if we have more than 32k mids in the list, then something
  180. * is very wrong. Possibly a local user is trying to DoS the
  181. * box by issuing long-running calls and SIGKILL'ing them. If
  182. * we get to 2^16 mids then we're in big trouble as this
  183. * function could loop forever.
  184. *
  185. * Go ahead and assign out the mid in this situation, but force
  186. * an eventual reconnect to clean out the pending_mid_q.
  187. */
  188. if (num_mids > 32768)
  189. server->tcpStatus = CifsNeedReconnect;
  190. if (!collision) {
  191. mid = (__u64)cur_mid;
  192. server->CurrentMid = mid;
  193. break;
  194. }
  195. cur_mid++;
  196. }
  197. spin_unlock(&GlobalMid_Lock);
  198. return mid;
  199. }
  200. /*
  201. return codes:
  202. 0 not a transact2, or all data present
  203. >0 transact2 with that much data missing
  204. -EINVAL invalid transact2
  205. */
  206. static int
  207. check2ndT2(char *buf)
  208. {
  209. struct smb_hdr *pSMB = (struct smb_hdr *)buf;
  210. struct smb_t2_rsp *pSMBt;
  211. int remaining;
  212. __u16 total_data_size, data_in_this_rsp;
  213. if (pSMB->Command != SMB_COM_TRANSACTION2)
  214. return 0;
  215. /* check for plausible wct, bcc and t2 data and parm sizes */
  216. /* check for parm and data offset going beyond end of smb */
  217. if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */
  218. cFYI(1, "invalid transact2 word count");
  219. return -EINVAL;
  220. }
  221. pSMBt = (struct smb_t2_rsp *)pSMB;
  222. total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
  223. data_in_this_rsp = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
  224. if (total_data_size == data_in_this_rsp)
  225. return 0;
  226. else if (total_data_size < data_in_this_rsp) {
  227. cFYI(1, "total data %d smaller than data in frame %d",
  228. total_data_size, data_in_this_rsp);
  229. return -EINVAL;
  230. }
  231. remaining = total_data_size - data_in_this_rsp;
  232. cFYI(1, "missing %d bytes from transact2, check next response",
  233. remaining);
  234. if (total_data_size > CIFSMaxBufSize) {
  235. cERROR(1, "TotalDataSize %d is over maximum buffer %d",
  236. total_data_size, CIFSMaxBufSize);
  237. return -EINVAL;
  238. }
  239. return remaining;
  240. }
  241. static int
  242. coalesce_t2(char *second_buf, struct smb_hdr *target_hdr)
  243. {
  244. struct smb_t2_rsp *pSMBs = (struct smb_t2_rsp *)second_buf;
  245. struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)target_hdr;
  246. char *data_area_of_tgt;
  247. char *data_area_of_src;
  248. int remaining;
  249. unsigned int byte_count, total_in_tgt;
  250. __u16 tgt_total_cnt, src_total_cnt, total_in_src;
  251. src_total_cnt = get_unaligned_le16(&pSMBs->t2_rsp.TotalDataCount);
  252. tgt_total_cnt = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
  253. if (tgt_total_cnt != src_total_cnt)
  254. cFYI(1, "total data count of primary and secondary t2 differ "
  255. "source=%hu target=%hu", src_total_cnt, tgt_total_cnt);
  256. total_in_tgt = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
  257. remaining = tgt_total_cnt - total_in_tgt;
  258. if (remaining < 0) {
  259. cFYI(1, "Server sent too much data. tgt_total_cnt=%hu "
  260. "total_in_tgt=%hu", tgt_total_cnt, total_in_tgt);
  261. return -EPROTO;
  262. }
  263. if (remaining == 0) {
  264. /* nothing to do, ignore */
  265. cFYI(1, "no more data remains");
  266. return 0;
  267. }
  268. total_in_src = get_unaligned_le16(&pSMBs->t2_rsp.DataCount);
  269. if (remaining < total_in_src)
  270. cFYI(1, "transact2 2nd response contains too much data");
  271. /* find end of first SMB data area */
  272. data_area_of_tgt = (char *)&pSMBt->hdr.Protocol +
  273. get_unaligned_le16(&pSMBt->t2_rsp.DataOffset);
  274. /* validate target area */
  275. data_area_of_src = (char *)&pSMBs->hdr.Protocol +
  276. get_unaligned_le16(&pSMBs->t2_rsp.DataOffset);
  277. data_area_of_tgt += total_in_tgt;
  278. total_in_tgt += total_in_src;
  279. /* is the result too big for the field? */
  280. if (total_in_tgt > USHRT_MAX) {
  281. cFYI(1, "coalesced DataCount too large (%u)", total_in_tgt);
  282. return -EPROTO;
  283. }
  284. put_unaligned_le16(total_in_tgt, &pSMBt->t2_rsp.DataCount);
  285. /* fix up the BCC */
  286. byte_count = get_bcc(target_hdr);
  287. byte_count += total_in_src;
  288. /* is the result too big for the field? */
  289. if (byte_count > USHRT_MAX) {
  290. cFYI(1, "coalesced BCC too large (%u)", byte_count);
  291. return -EPROTO;
  292. }
  293. put_bcc(byte_count, target_hdr);
  294. byte_count = be32_to_cpu(target_hdr->smb_buf_length);
  295. byte_count += total_in_src;
  296. /* don't allow buffer to overflow */
  297. if (byte_count > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  298. cFYI(1, "coalesced BCC exceeds buffer size (%u)", byte_count);
  299. return -ENOBUFS;
  300. }
  301. target_hdr->smb_buf_length = cpu_to_be32(byte_count);
  302. /* copy second buffer into end of first buffer */
  303. memcpy(data_area_of_tgt, data_area_of_src, total_in_src);
  304. if (remaining != total_in_src) {
  305. /* more responses to go */
  306. cFYI(1, "waiting for more secondary responses");
  307. return 1;
  308. }
  309. /* we are done */
  310. cFYI(1, "found the last secondary response");
  311. return 0;
  312. }
  313. static bool
  314. cifs_check_trans2(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  315. char *buf, int malformed)
  316. {
  317. if (malformed)
  318. return false;
  319. if (check2ndT2(buf) <= 0)
  320. return false;
  321. mid->multiRsp = true;
  322. if (mid->resp_buf) {
  323. /* merge response - fix up 1st*/
  324. malformed = coalesce_t2(buf, mid->resp_buf);
  325. if (malformed > 0)
  326. return true;
  327. /* All parts received or packet is malformed. */
  328. mid->multiEnd = true;
  329. dequeue_mid(mid, malformed);
  330. return true;
  331. }
  332. if (!server->large_buf) {
  333. /*FIXME: switch to already allocated largebuf?*/
  334. cERROR(1, "1st trans2 resp needs bigbuf");
  335. } else {
  336. /* Have first buffer */
  337. mid->resp_buf = buf;
  338. mid->large_buf = true;
  339. server->bigbuf = NULL;
  340. }
  341. return true;
  342. }
  343. static bool
  344. cifs_need_neg(struct TCP_Server_Info *server)
  345. {
  346. return server->maxBuf == 0;
  347. }
  348. static int
  349. cifs_negotiate(const unsigned int xid, struct cifs_ses *ses)
  350. {
  351. int rc;
  352. rc = CIFSSMBNegotiate(xid, ses);
  353. if (rc == -EAGAIN) {
  354. /* retry only once on 1st time connection */
  355. set_credits(ses->server, 1);
  356. rc = CIFSSMBNegotiate(xid, ses);
  357. if (rc == -EAGAIN)
  358. rc = -EHOSTDOWN;
  359. }
  360. return rc;
  361. }
  362. static unsigned int
  363. cifs_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  364. {
  365. __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  366. struct TCP_Server_Info *server = tcon->ses->server;
  367. unsigned int wsize;
  368. /* start with specified wsize, or default */
  369. if (volume_info->wsize)
  370. wsize = volume_info->wsize;
  371. else if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
  372. wsize = CIFS_DEFAULT_IOSIZE;
  373. else
  374. wsize = CIFS_DEFAULT_NON_POSIX_WSIZE;
  375. /* can server support 24-bit write sizes? (via UNIX extensions) */
  376. if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
  377. wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1002_WSIZE);
  378. /*
  379. * no CAP_LARGE_WRITE_X or is signing enabled without CAP_UNIX set?
  380. * Limit it to max buffer offered by the server, minus the size of the
  381. * WRITEX header, not including the 4 byte RFC1001 length.
  382. */
  383. if (!(server->capabilities & CAP_LARGE_WRITE_X) ||
  384. (!(server->capabilities & CAP_UNIX) &&
  385. (server->sec_mode & (SECMODE_SIGN_ENABLED|SECMODE_SIGN_REQUIRED))))
  386. wsize = min_t(unsigned int, wsize,
  387. server->maxBuf - sizeof(WRITE_REQ) + 4);
  388. /* limit to the amount that we can kmap at once */
  389. wsize = min_t(unsigned int, wsize, CIFS_KMAP_SIZE_LIMIT);
  390. /* hard limit of CIFS_MAX_WSIZE */
  391. wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE);
  392. return wsize;
  393. }
  394. static unsigned int
  395. cifs_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  396. {
  397. __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  398. struct TCP_Server_Info *server = tcon->ses->server;
  399. unsigned int rsize, defsize;
  400. /*
  401. * Set default value...
  402. *
  403. * HACK alert! Ancient servers have very small buffers. Even though
  404. * MS-CIFS indicates that servers are only limited by the client's
  405. * bufsize for reads, testing against win98se shows that it throws
  406. * INVALID_PARAMETER errors if you try to request too large a read.
  407. * OS/2 just sends back short reads.
  408. *
  409. * If the server doesn't advertise CAP_LARGE_READ_X, then assume that
  410. * it can't handle a read request larger than its MaxBufferSize either.
  411. */
  412. if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_READ_CAP))
  413. defsize = CIFS_DEFAULT_IOSIZE;
  414. else if (server->capabilities & CAP_LARGE_READ_X)
  415. defsize = CIFS_DEFAULT_NON_POSIX_RSIZE;
  416. else
  417. defsize = server->maxBuf - sizeof(READ_RSP);
  418. rsize = volume_info->rsize ? volume_info->rsize : defsize;
  419. /*
  420. * no CAP_LARGE_READ_X? Then MS-CIFS states that we must limit this to
  421. * the client's MaxBufferSize.
  422. */
  423. if (!(server->capabilities & CAP_LARGE_READ_X))
  424. rsize = min_t(unsigned int, CIFSMaxBufSize, rsize);
  425. /* limit to the amount that we can kmap at once */
  426. rsize = min_t(unsigned int, rsize, CIFS_KMAP_SIZE_LIMIT);
  427. /* hard limit of CIFS_MAX_RSIZE */
  428. rsize = min_t(unsigned int, rsize, CIFS_MAX_RSIZE);
  429. return rsize;
  430. }
  431. static void
  432. cifs_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
  433. {
  434. CIFSSMBQFSDeviceInfo(xid, tcon);
  435. CIFSSMBQFSAttributeInfo(xid, tcon);
  436. }
  437. static int
  438. cifs_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
  439. struct cifs_sb_info *cifs_sb, const char *full_path)
  440. {
  441. int rc;
  442. FILE_ALL_INFO *file_info;
  443. file_info = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  444. if (file_info == NULL)
  445. return -ENOMEM;
  446. rc = CIFSSMBQPathInfo(xid, tcon, full_path, file_info,
  447. 0 /* not legacy */, cifs_sb->local_nls,
  448. cifs_sb->mnt_cifs_flags &
  449. CIFS_MOUNT_MAP_SPECIAL_CHR);
  450. if (rc == -EOPNOTSUPP || rc == -EINVAL)
  451. rc = SMBQueryInformation(xid, tcon, full_path, file_info,
  452. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  453. CIFS_MOUNT_MAP_SPECIAL_CHR);
  454. kfree(file_info);
  455. return rc;
  456. }
  457. static int
  458. cifs_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
  459. struct cifs_sb_info *cifs_sb, const char *full_path,
  460. FILE_ALL_INFO *data, bool *adjustTZ)
  461. {
  462. int rc;
  463. /* could do find first instead but this returns more info */
  464. rc = CIFSSMBQPathInfo(xid, tcon, full_path, data, 0 /* not legacy */,
  465. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  466. CIFS_MOUNT_MAP_SPECIAL_CHR);
  467. /*
  468. * BB optimize code so we do not make the above call when server claims
  469. * no NT SMB support and the above call failed at least once - set flag
  470. * in tcon or mount.
  471. */
  472. if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
  473. rc = SMBQueryInformation(xid, tcon, full_path, data,
  474. cifs_sb->local_nls,
  475. cifs_sb->mnt_cifs_flags &
  476. CIFS_MOUNT_MAP_SPECIAL_CHR);
  477. *adjustTZ = true;
  478. }
  479. return rc;
  480. }
  481. static int
  482. cifs_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
  483. struct cifs_sb_info *cifs_sb, const char *full_path,
  484. u64 *uniqueid, FILE_ALL_INFO *data)
  485. {
  486. /*
  487. * We can not use the IndexNumber field by default from Windows or
  488. * Samba (in ALL_INFO buf) but we can request it explicitly. The SNIA
  489. * CIFS spec claims that this value is unique within the scope of a
  490. * share, and the windows docs hint that it's actually unique
  491. * per-machine.
  492. *
  493. * There may be higher info levels that work but are there Windows
  494. * server or network appliances for which IndexNumber field is not
  495. * guaranteed unique?
  496. */
  497. return CIFSGetSrvInodeNumber(xid, tcon, full_path, uniqueid,
  498. cifs_sb->local_nls,
  499. cifs_sb->mnt_cifs_flags &
  500. CIFS_MOUNT_MAP_SPECIAL_CHR);
  501. }
  502. static int
  503. cifs_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
  504. struct cifs_fid *fid, FILE_ALL_INFO *data)
  505. {
  506. return CIFSSMBQFileInfo(xid, tcon, fid->netfid, data);
  507. }
  508. static char *
  509. cifs_build_path_to_root(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
  510. struct cifs_tcon *tcon)
  511. {
  512. int pplen = vol->prepath ? strlen(vol->prepath) : 0;
  513. int dfsplen;
  514. char *full_path = NULL;
  515. /* if no prefix path, simply set path to the root of share to "" */
  516. if (pplen == 0) {
  517. full_path = kzalloc(1, GFP_KERNEL);
  518. return full_path;
  519. }
  520. if (tcon->Flags & SMB_SHARE_IS_IN_DFS)
  521. dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
  522. else
  523. dfsplen = 0;
  524. full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
  525. if (full_path == NULL)
  526. return full_path;
  527. if (dfsplen)
  528. strncpy(full_path, tcon->treeName, dfsplen);
  529. strncpy(full_path + dfsplen, vol->prepath, pplen);
  530. convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
  531. full_path[dfsplen + pplen] = 0; /* add trailing null */
  532. return full_path;
  533. }
  534. static void
  535. cifs_clear_stats(struct cifs_tcon *tcon)
  536. {
  537. #ifdef CONFIG_CIFS_STATS
  538. atomic_set(&tcon->stats.cifs_stats.num_writes, 0);
  539. atomic_set(&tcon->stats.cifs_stats.num_reads, 0);
  540. atomic_set(&tcon->stats.cifs_stats.num_flushes, 0);
  541. atomic_set(&tcon->stats.cifs_stats.num_oplock_brks, 0);
  542. atomic_set(&tcon->stats.cifs_stats.num_opens, 0);
  543. atomic_set(&tcon->stats.cifs_stats.num_posixopens, 0);
  544. atomic_set(&tcon->stats.cifs_stats.num_posixmkdirs, 0);
  545. atomic_set(&tcon->stats.cifs_stats.num_closes, 0);
  546. atomic_set(&tcon->stats.cifs_stats.num_deletes, 0);
  547. atomic_set(&tcon->stats.cifs_stats.num_mkdirs, 0);
  548. atomic_set(&tcon->stats.cifs_stats.num_rmdirs, 0);
  549. atomic_set(&tcon->stats.cifs_stats.num_renames, 0);
  550. atomic_set(&tcon->stats.cifs_stats.num_t2renames, 0);
  551. atomic_set(&tcon->stats.cifs_stats.num_ffirst, 0);
  552. atomic_set(&tcon->stats.cifs_stats.num_fnext, 0);
  553. atomic_set(&tcon->stats.cifs_stats.num_fclose, 0);
  554. atomic_set(&tcon->stats.cifs_stats.num_hardlinks, 0);
  555. atomic_set(&tcon->stats.cifs_stats.num_symlinks, 0);
  556. atomic_set(&tcon->stats.cifs_stats.num_locks, 0);
  557. atomic_set(&tcon->stats.cifs_stats.num_acl_get, 0);
  558. atomic_set(&tcon->stats.cifs_stats.num_acl_set, 0);
  559. #endif
  560. }
  561. static void
  562. cifs_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
  563. {
  564. #ifdef CONFIG_CIFS_STATS
  565. seq_printf(m, " Oplocks breaks: %d",
  566. atomic_read(&tcon->stats.cifs_stats.num_oplock_brks));
  567. seq_printf(m, "\nReads: %d Bytes: %llu",
  568. atomic_read(&tcon->stats.cifs_stats.num_reads),
  569. (long long)(tcon->bytes_read));
  570. seq_printf(m, "\nWrites: %d Bytes: %llu",
  571. atomic_read(&tcon->stats.cifs_stats.num_writes),
  572. (long long)(tcon->bytes_written));
  573. seq_printf(m, "\nFlushes: %d",
  574. atomic_read(&tcon->stats.cifs_stats.num_flushes));
  575. seq_printf(m, "\nLocks: %d HardLinks: %d Symlinks: %d",
  576. atomic_read(&tcon->stats.cifs_stats.num_locks),
  577. atomic_read(&tcon->stats.cifs_stats.num_hardlinks),
  578. atomic_read(&tcon->stats.cifs_stats.num_symlinks));
  579. seq_printf(m, "\nOpens: %d Closes: %d Deletes: %d",
  580. atomic_read(&tcon->stats.cifs_stats.num_opens),
  581. atomic_read(&tcon->stats.cifs_stats.num_closes),
  582. atomic_read(&tcon->stats.cifs_stats.num_deletes));
  583. seq_printf(m, "\nPosix Opens: %d Posix Mkdirs: %d",
  584. atomic_read(&tcon->stats.cifs_stats.num_posixopens),
  585. atomic_read(&tcon->stats.cifs_stats.num_posixmkdirs));
  586. seq_printf(m, "\nMkdirs: %d Rmdirs: %d",
  587. atomic_read(&tcon->stats.cifs_stats.num_mkdirs),
  588. atomic_read(&tcon->stats.cifs_stats.num_rmdirs));
  589. seq_printf(m, "\nRenames: %d T2 Renames %d",
  590. atomic_read(&tcon->stats.cifs_stats.num_renames),
  591. atomic_read(&tcon->stats.cifs_stats.num_t2renames));
  592. seq_printf(m, "\nFindFirst: %d FNext %d FClose %d",
  593. atomic_read(&tcon->stats.cifs_stats.num_ffirst),
  594. atomic_read(&tcon->stats.cifs_stats.num_fnext),
  595. atomic_read(&tcon->stats.cifs_stats.num_fclose));
  596. #endif
  597. }
  598. static void
  599. cifs_mkdir_setinfo(struct inode *inode, const char *full_path,
  600. struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
  601. const unsigned int xid)
  602. {
  603. FILE_BASIC_INFO info;
  604. struct cifsInodeInfo *cifsInode;
  605. u32 dosattrs;
  606. int rc;
  607. memset(&info, 0, sizeof(info));
  608. cifsInode = CIFS_I(inode);
  609. dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
  610. info.Attributes = cpu_to_le32(dosattrs);
  611. rc = CIFSSMBSetPathInfo(xid, tcon, full_path, &info, cifs_sb->local_nls,
  612. cifs_sb->mnt_cifs_flags &
  613. CIFS_MOUNT_MAP_SPECIAL_CHR);
  614. if (rc == 0)
  615. cifsInode->cifsAttrs = dosattrs;
  616. }
  617. static int
  618. cifs_open_file(const unsigned int xid, struct cifs_tcon *tcon, const char *path,
  619. int disposition, int desired_access, int create_options,
  620. struct cifs_fid *fid, __u32 *oplock, FILE_ALL_INFO *buf,
  621. struct cifs_sb_info *cifs_sb)
  622. {
  623. if (!(tcon->ses->capabilities & CAP_NT_SMBS))
  624. return SMBLegacyOpen(xid, tcon, path, disposition,
  625. desired_access, create_options,
  626. &fid->netfid, oplock, buf,
  627. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags
  628. & CIFS_MOUNT_MAP_SPECIAL_CHR);
  629. return CIFSSMBOpen(xid, tcon, path, disposition, desired_access,
  630. create_options, &fid->netfid, oplock, buf,
  631. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  632. CIFS_MOUNT_MAP_SPECIAL_CHR);
  633. }
  634. static void
  635. cifs_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
  636. {
  637. struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
  638. cfile->fid.netfid = fid->netfid;
  639. cifs_set_oplock_level(cinode, oplock);
  640. cinode->can_cache_brlcks = cinode->clientCanCacheAll;
  641. }
  642. static int
  643. cifs_close_file(const unsigned int xid, struct cifs_tcon *tcon,
  644. struct cifs_fid *fid)
  645. {
  646. return CIFSSMBClose(xid, tcon, fid->netfid);
  647. }
  648. static int
  649. cifs_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
  650. struct cifs_fid *fid)
  651. {
  652. return CIFSSMBFlush(xid, tcon, fid->netfid);
  653. }
  654. static int
  655. cifs_sync_read(const unsigned int xid, struct cifsFileInfo *cfile,
  656. struct cifs_io_parms *parms, unsigned int *bytes_read,
  657. char **buf, int *buf_type)
  658. {
  659. parms->netfid = cfile->fid.netfid;
  660. return CIFSSMBRead(xid, parms, bytes_read, buf, buf_type);
  661. }
  662. static int
  663. cifs_sync_write(const unsigned int xid, struct cifsFileInfo *cfile,
  664. struct cifs_io_parms *parms, unsigned int *written,
  665. struct kvec *iov, unsigned long nr_segs)
  666. {
  667. parms->netfid = cfile->fid.netfid;
  668. return CIFSSMBWrite2(xid, parms, written, iov, nr_segs);
  669. }
  670. static int
  671. smb_set_file_info(struct inode *inode, const char *full_path,
  672. FILE_BASIC_INFO *buf, const unsigned int xid)
  673. {
  674. int oplock = 0;
  675. int rc;
  676. __u16 netfid;
  677. __u32 netpid;
  678. struct cifsFileInfo *open_file;
  679. struct cifsInodeInfo *cinode = CIFS_I(inode);
  680. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  681. struct tcon_link *tlink = NULL;
  682. struct cifs_tcon *tcon;
  683. FILE_BASIC_INFO info_buf;
  684. /* if the file is already open for write, just use that fileid */
  685. open_file = find_writable_file(cinode, true);
  686. if (open_file) {
  687. netfid = open_file->fid.netfid;
  688. netpid = open_file->pid;
  689. tcon = tlink_tcon(open_file->tlink);
  690. goto set_via_filehandle;
  691. }
  692. tlink = cifs_sb_tlink(cifs_sb);
  693. if (IS_ERR(tlink)) {
  694. rc = PTR_ERR(tlink);
  695. tlink = NULL;
  696. goto out;
  697. }
  698. tcon = tlink_tcon(tlink);
  699. /*
  700. * NT4 apparently returns success on this call, but it doesn't really
  701. * work.
  702. */
  703. if (!(tcon->ses->flags & CIFS_SES_NT4)) {
  704. rc = CIFSSMBSetPathInfo(xid, tcon, full_path, buf,
  705. cifs_sb->local_nls,
  706. cifs_sb->mnt_cifs_flags &
  707. CIFS_MOUNT_MAP_SPECIAL_CHR);
  708. if (rc == 0) {
  709. cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
  710. goto out;
  711. } else if (rc != -EOPNOTSUPP && rc != -EINVAL)
  712. goto out;
  713. }
  714. cFYI(1, "calling SetFileInfo since SetPathInfo for times not supported "
  715. "by this server");
  716. rc = CIFSSMBOpen(xid, tcon, full_path, FILE_OPEN,
  717. SYNCHRONIZE | FILE_WRITE_ATTRIBUTES, CREATE_NOT_DIR,
  718. &netfid, &oplock, NULL, cifs_sb->local_nls,
  719. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  720. if (rc != 0) {
  721. if (rc == -EIO)
  722. rc = -EINVAL;
  723. goto out;
  724. }
  725. netpid = current->tgid;
  726. set_via_filehandle:
  727. rc = CIFSSMBSetFileInfo(xid, tcon, &info_buf, netfid, netpid);
  728. if (!rc)
  729. cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
  730. if (open_file == NULL)
  731. CIFSSMBClose(xid, tcon, netfid);
  732. else
  733. cifsFileInfo_put(open_file);
  734. out:
  735. if (tlink != NULL)
  736. cifs_put_tlink(tlink);
  737. return rc;
  738. }
  739. static int
  740. cifs_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
  741. const char *path, struct cifs_sb_info *cifs_sb,
  742. struct cifs_fid *fid, __u16 search_flags,
  743. struct cifs_search_info *srch_inf)
  744. {
  745. return CIFSFindFirst(xid, tcon, path, cifs_sb->local_nls,
  746. &fid->netfid, search_flags, srch_inf,
  747. cifs_sb->mnt_cifs_flags &
  748. CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
  749. }
  750. static int
  751. cifs_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
  752. struct cifs_fid *fid, __u16 search_flags,
  753. struct cifs_search_info *srch_inf)
  754. {
  755. return CIFSFindNext(xid, tcon, fid->netfid, search_flags, srch_inf);
  756. }
  757. static int
  758. cifs_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
  759. struct cifs_fid *fid)
  760. {
  761. return CIFSFindClose(xid, tcon, fid->netfid);
  762. }
  763. static int
  764. cifs_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
  765. struct cifsInodeInfo *cinode)
  766. {
  767. return CIFSSMBLock(0, tcon, fid->netfid, current->tgid, 0, 0, 0, 0,
  768. LOCKING_ANDX_OPLOCK_RELEASE, false,
  769. cinode->clientCanCacheRead ? 1 : 0);
  770. }
  771. struct smb_version_operations smb1_operations = {
  772. .send_cancel = send_nt_cancel,
  773. .compare_fids = cifs_compare_fids,
  774. .setup_request = cifs_setup_request,
  775. .setup_async_request = cifs_setup_async_request,
  776. .check_receive = cifs_check_receive,
  777. .add_credits = cifs_add_credits,
  778. .set_credits = cifs_set_credits,
  779. .get_credits_field = cifs_get_credits_field,
  780. .get_credits = cifs_get_credits,
  781. .get_next_mid = cifs_get_next_mid,
  782. .read_data_offset = cifs_read_data_offset,
  783. .read_data_length = cifs_read_data_length,
  784. .map_error = map_smb_to_linux_error,
  785. .find_mid = cifs_find_mid,
  786. .check_message = checkSMB,
  787. .dump_detail = cifs_dump_detail,
  788. .clear_stats = cifs_clear_stats,
  789. .print_stats = cifs_print_stats,
  790. .is_oplock_break = is_valid_oplock_break,
  791. .check_trans2 = cifs_check_trans2,
  792. .need_neg = cifs_need_neg,
  793. .negotiate = cifs_negotiate,
  794. .negotiate_wsize = cifs_negotiate_wsize,
  795. .negotiate_rsize = cifs_negotiate_rsize,
  796. .sess_setup = CIFS_SessSetup,
  797. .logoff = CIFSSMBLogoff,
  798. .tree_connect = CIFSTCon,
  799. .tree_disconnect = CIFSSMBTDis,
  800. .get_dfs_refer = CIFSGetDFSRefer,
  801. .qfs_tcon = cifs_qfs_tcon,
  802. .is_path_accessible = cifs_is_path_accessible,
  803. .query_path_info = cifs_query_path_info,
  804. .query_file_info = cifs_query_file_info,
  805. .get_srv_inum = cifs_get_srv_inum,
  806. .set_path_size = CIFSSMBSetEOF,
  807. .set_file_size = CIFSSMBSetFileSize,
  808. .set_file_info = smb_set_file_info,
  809. .build_path_to_root = cifs_build_path_to_root,
  810. .echo = CIFSSMBEcho,
  811. .mkdir = CIFSSMBMkDir,
  812. .mkdir_setinfo = cifs_mkdir_setinfo,
  813. .rmdir = CIFSSMBRmDir,
  814. .unlink = CIFSSMBDelFile,
  815. .rename_pending_delete = cifs_rename_pending_delete,
  816. .rename = CIFSSMBRename,
  817. .create_hardlink = CIFSCreateHardLink,
  818. .open = cifs_open_file,
  819. .set_fid = cifs_set_fid,
  820. .close = cifs_close_file,
  821. .flush = cifs_flush_file,
  822. .async_readv = cifs_async_readv,
  823. .async_writev = cifs_async_writev,
  824. .sync_read = cifs_sync_read,
  825. .sync_write = cifs_sync_write,
  826. .query_dir_first = cifs_query_dir_first,
  827. .query_dir_next = cifs_query_dir_next,
  828. .close_dir = cifs_close_dir,
  829. .calc_smb_size = smbCalcSize,
  830. .oplock_response = cifs_oplock_response,
  831. };
  832. struct smb_version_values smb1_values = {
  833. .version_string = SMB1_VERSION_STRING,
  834. .large_lock_type = LOCKING_ANDX_LARGE_FILES,
  835. .exclusive_lock_type = 0,
  836. .shared_lock_type = LOCKING_ANDX_SHARED_LOCK,
  837. .unlock_lock_type = 0,
  838. .header_size = sizeof(struct smb_hdr),
  839. .max_header_size = MAX_CIFS_HDR_SIZE,
  840. .read_rsp_size = sizeof(READ_RSP),
  841. .lock_cmd = cpu_to_le16(SMB_COM_LOCKING_ANDX),
  842. .cap_unix = CAP_UNIX,
  843. .cap_nt_find = CAP_NT_SMBS | CAP_NT_FIND,
  844. .cap_large_files = CAP_LARGE_FILES,
  845. };