misc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * fs/cifs/misc.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2008
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/slab.h>
  22. #include <linux/ctype.h>
  23. #include <linux/mempool.h>
  24. #include "cifspdu.h"
  25. #include "cifsglob.h"
  26. #include "cifsproto.h"
  27. #include "cifs_debug.h"
  28. #include "smberr.h"
  29. #include "nterr.h"
  30. #include "cifs_unicode.h"
  31. extern mempool_t *cifs_sm_req_poolp;
  32. extern mempool_t *cifs_req_poolp;
  33. /* The xid serves as a useful identifier for each incoming vfs request,
  34. in a similar way to the mid which is useful to track each sent smb,
  35. and CurrentXid can also provide a running counter (although it
  36. will eventually wrap past zero) of the total vfs operations handled
  37. since the cifs fs was mounted */
  38. unsigned int
  39. _GetXid(void)
  40. {
  41. unsigned int xid;
  42. spin_lock(&GlobalMid_Lock);
  43. GlobalTotalActiveXid++;
  44. /* keep high water mark for number of simultaneous ops in filesystem */
  45. if (GlobalTotalActiveXid > GlobalMaxActiveXid)
  46. GlobalMaxActiveXid = GlobalTotalActiveXid;
  47. if (GlobalTotalActiveXid > 65000)
  48. cFYI(1, "warning: more than 65000 requests active");
  49. xid = GlobalCurrentXid++;
  50. spin_unlock(&GlobalMid_Lock);
  51. return xid;
  52. }
  53. void
  54. _FreeXid(unsigned int xid)
  55. {
  56. spin_lock(&GlobalMid_Lock);
  57. /* if (GlobalTotalActiveXid == 0)
  58. BUG(); */
  59. GlobalTotalActiveXid--;
  60. spin_unlock(&GlobalMid_Lock);
  61. }
  62. struct cifs_ses *
  63. sesInfoAlloc(void)
  64. {
  65. struct cifs_ses *ret_buf;
  66. ret_buf = kzalloc(sizeof(struct cifs_ses), GFP_KERNEL);
  67. if (ret_buf) {
  68. atomic_inc(&sesInfoAllocCount);
  69. ret_buf->status = CifsNew;
  70. ++ret_buf->ses_count;
  71. INIT_LIST_HEAD(&ret_buf->smb_ses_list);
  72. INIT_LIST_HEAD(&ret_buf->tcon_list);
  73. mutex_init(&ret_buf->session_mutex);
  74. }
  75. return ret_buf;
  76. }
  77. void
  78. sesInfoFree(struct cifs_ses *buf_to_free)
  79. {
  80. if (buf_to_free == NULL) {
  81. cFYI(1, "Null buffer passed to sesInfoFree");
  82. return;
  83. }
  84. atomic_dec(&sesInfoAllocCount);
  85. kfree(buf_to_free->serverOS);
  86. kfree(buf_to_free->serverDomain);
  87. kfree(buf_to_free->serverNOS);
  88. if (buf_to_free->password) {
  89. memset(buf_to_free->password, 0, strlen(buf_to_free->password));
  90. kfree(buf_to_free->password);
  91. }
  92. kfree(buf_to_free->user_name);
  93. kfree(buf_to_free->domainName);
  94. kfree(buf_to_free);
  95. }
  96. struct cifs_tcon *
  97. tconInfoAlloc(void)
  98. {
  99. struct cifs_tcon *ret_buf;
  100. ret_buf = kzalloc(sizeof(struct cifs_tcon), GFP_KERNEL);
  101. if (ret_buf) {
  102. atomic_inc(&tconInfoAllocCount);
  103. ret_buf->tidStatus = CifsNew;
  104. ++ret_buf->tc_count;
  105. INIT_LIST_HEAD(&ret_buf->openFileList);
  106. INIT_LIST_HEAD(&ret_buf->tcon_list);
  107. #ifdef CONFIG_CIFS_STATS
  108. spin_lock_init(&ret_buf->stat_lock);
  109. #endif
  110. }
  111. return ret_buf;
  112. }
  113. void
  114. tconInfoFree(struct cifs_tcon *buf_to_free)
  115. {
  116. if (buf_to_free == NULL) {
  117. cFYI(1, "Null buffer passed to tconInfoFree");
  118. return;
  119. }
  120. atomic_dec(&tconInfoAllocCount);
  121. kfree(buf_to_free->nativeFileSystem);
  122. if (buf_to_free->password) {
  123. memset(buf_to_free->password, 0, strlen(buf_to_free->password));
  124. kfree(buf_to_free->password);
  125. }
  126. kfree(buf_to_free);
  127. }
  128. struct smb_hdr *
  129. cifs_buf_get(void)
  130. {
  131. struct smb_hdr *ret_buf = NULL;
  132. /* We could use negotiated size instead of max_msgsize -
  133. but it may be more efficient to always alloc same size
  134. albeit slightly larger than necessary and maxbuffersize
  135. defaults to this and can not be bigger */
  136. ret_buf = mempool_alloc(cifs_req_poolp, GFP_NOFS);
  137. /* clear the first few header bytes */
  138. /* for most paths, more is cleared in header_assemble */
  139. if (ret_buf) {
  140. memset(ret_buf, 0, sizeof(struct smb_hdr) + 3);
  141. atomic_inc(&bufAllocCount);
  142. #ifdef CONFIG_CIFS_STATS2
  143. atomic_inc(&totBufAllocCount);
  144. #endif /* CONFIG_CIFS_STATS2 */
  145. }
  146. return ret_buf;
  147. }
  148. void
  149. cifs_buf_release(void *buf_to_free)
  150. {
  151. if (buf_to_free == NULL) {
  152. /* cFYI(1, "Null buffer passed to cifs_buf_release");*/
  153. return;
  154. }
  155. mempool_free(buf_to_free, cifs_req_poolp);
  156. atomic_dec(&bufAllocCount);
  157. return;
  158. }
  159. struct smb_hdr *
  160. cifs_small_buf_get(void)
  161. {
  162. struct smb_hdr *ret_buf = NULL;
  163. /* We could use negotiated size instead of max_msgsize -
  164. but it may be more efficient to always alloc same size
  165. albeit slightly larger than necessary and maxbuffersize
  166. defaults to this and can not be bigger */
  167. ret_buf = mempool_alloc(cifs_sm_req_poolp, GFP_NOFS);
  168. if (ret_buf) {
  169. /* No need to clear memory here, cleared in header assemble */
  170. /* memset(ret_buf, 0, sizeof(struct smb_hdr) + 27);*/
  171. atomic_inc(&smBufAllocCount);
  172. #ifdef CONFIG_CIFS_STATS2
  173. atomic_inc(&totSmBufAllocCount);
  174. #endif /* CONFIG_CIFS_STATS2 */
  175. }
  176. return ret_buf;
  177. }
  178. void
  179. cifs_small_buf_release(void *buf_to_free)
  180. {
  181. if (buf_to_free == NULL) {
  182. cFYI(1, "Null buffer passed to cifs_small_buf_release");
  183. return;
  184. }
  185. mempool_free(buf_to_free, cifs_sm_req_poolp);
  186. atomic_dec(&smBufAllocCount);
  187. return;
  188. }
  189. /*
  190. Find a free multiplex id (SMB mid). Otherwise there could be
  191. mid collisions which might cause problems, demultiplexing the
  192. wrong response to this request. Multiplex ids could collide if
  193. one of a series requests takes much longer than the others, or
  194. if a very large number of long lived requests (byte range
  195. locks or FindNotify requests) are pending. No more than
  196. 64K-1 requests can be outstanding at one time. If no
  197. mids are available, return zero. A future optimization
  198. could make the combination of mids and uid the key we use
  199. to demultiplex on (rather than mid alone).
  200. In addition to the above check, the cifs demultiplex
  201. code already used the command code as a secondary
  202. check of the frame and if signing is negotiated the
  203. response would be discarded if the mid were the same
  204. but the signature was wrong. Since the mid is not put in the
  205. pending queue until later (when it is about to be dispatched)
  206. we do have to limit the number of outstanding requests
  207. to somewhat less than 64K-1 although it is hard to imagine
  208. so many threads being in the vfs at one time.
  209. */
  210. __u16 GetNextMid(struct TCP_Server_Info *server)
  211. {
  212. __u16 mid = 0;
  213. __u16 last_mid;
  214. bool collision;
  215. spin_lock(&GlobalMid_Lock);
  216. last_mid = server->CurrentMid; /* we do not want to loop forever */
  217. server->CurrentMid++;
  218. /* This nested loop looks more expensive than it is.
  219. In practice the list of pending requests is short,
  220. fewer than 50, and the mids are likely to be unique
  221. on the first pass through the loop unless some request
  222. takes longer than the 64 thousand requests before it
  223. (and it would also have to have been a request that
  224. did not time out) */
  225. while (server->CurrentMid != last_mid) {
  226. struct mid_q_entry *mid_entry;
  227. unsigned int num_mids;
  228. collision = false;
  229. if (server->CurrentMid == 0)
  230. server->CurrentMid++;
  231. num_mids = 0;
  232. list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
  233. ++num_mids;
  234. if (mid_entry->mid == server->CurrentMid &&
  235. mid_entry->midState == MID_REQUEST_SUBMITTED) {
  236. /* This mid is in use, try a different one */
  237. collision = true;
  238. break;
  239. }
  240. }
  241. /*
  242. * if we have more than 32k mids in the list, then something
  243. * is very wrong. Possibly a local user is trying to DoS the
  244. * box by issuing long-running calls and SIGKILL'ing them. If
  245. * we get to 2^16 mids then we're in big trouble as this
  246. * function could loop forever.
  247. *
  248. * Go ahead and assign out the mid in this situation, but force
  249. * an eventual reconnect to clean out the pending_mid_q.
  250. */
  251. if (num_mids > 32768)
  252. server->tcpStatus = CifsNeedReconnect;
  253. if (!collision) {
  254. mid = server->CurrentMid;
  255. break;
  256. }
  257. server->CurrentMid++;
  258. }
  259. spin_unlock(&GlobalMid_Lock);
  260. return mid;
  261. }
  262. /* NB: MID can not be set if treeCon not passed in, in that
  263. case it is responsbility of caller to set the mid */
  264. void
  265. header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
  266. const struct cifs_tcon *treeCon, int word_count
  267. /* length of fixed section (word count) in two byte units */)
  268. {
  269. struct list_head *temp_item;
  270. struct cifs_ses *ses;
  271. char *temp = (char *) buffer;
  272. memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
  273. buffer->smb_buf_length = cpu_to_be32(
  274. (2 * word_count) + sizeof(struct smb_hdr) -
  275. 4 /* RFC 1001 length field does not count */ +
  276. 2 /* for bcc field itself */) ;
  277. buffer->Protocol[0] = 0xFF;
  278. buffer->Protocol[1] = 'S';
  279. buffer->Protocol[2] = 'M';
  280. buffer->Protocol[3] = 'B';
  281. buffer->Command = smb_command;
  282. buffer->Flags = 0x00; /* case sensitive */
  283. buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
  284. buffer->Pid = cpu_to_le16((__u16)current->tgid);
  285. buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
  286. if (treeCon) {
  287. buffer->Tid = treeCon->tid;
  288. if (treeCon->ses) {
  289. if (treeCon->ses->capabilities & CAP_UNICODE)
  290. buffer->Flags2 |= SMBFLG2_UNICODE;
  291. if (treeCon->ses->capabilities & CAP_STATUS32)
  292. buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  293. /* Uid is not converted */
  294. buffer->Uid = treeCon->ses->Suid;
  295. buffer->Mid = GetNextMid(treeCon->ses->server);
  296. if (multiuser_mount != 0) {
  297. /* For the multiuser case, there are few obvious technically */
  298. /* possible mechanisms to match the local linux user (uid) */
  299. /* to a valid remote smb user (smb_uid): */
  300. /* 1) Query Winbind (or other local pam/nss daemon */
  301. /* for userid/password/logon_domain or credential */
  302. /* 2) Query Winbind for uid to sid to username mapping */
  303. /* and see if we have a matching password for existing*/
  304. /* session for that user perhas getting password by */
  305. /* adding a new pam_cifs module that stores passwords */
  306. /* so that the cifs vfs can get at that for all logged*/
  307. /* on users */
  308. /* 3) (Which is the mechanism we have chosen) */
  309. /* Search through sessions to the same server for a */
  310. /* a match on the uid that was passed in on mount */
  311. /* with the current processes uid (or euid?) and use */
  312. /* that smb uid. If no existing smb session for */
  313. /* that uid found, use the default smb session ie */
  314. /* the smb session for the volume mounted which is */
  315. /* the same as would be used if the multiuser mount */
  316. /* flag were disabled. */
  317. /* BB Add support for establishing new tCon and SMB Session */
  318. /* with userid/password pairs found on the smb session */
  319. /* for other target tcp/ip addresses BB */
  320. if (current_fsuid() != treeCon->ses->linux_uid) {
  321. cFYI(1, "Multiuser mode and UID "
  322. "did not match tcon uid");
  323. spin_lock(&cifs_tcp_ses_lock);
  324. list_for_each(temp_item, &treeCon->ses->server->smb_ses_list) {
  325. ses = list_entry(temp_item, struct cifs_ses, smb_ses_list);
  326. if (ses->linux_uid == current_fsuid()) {
  327. if (ses->server == treeCon->ses->server) {
  328. cFYI(1, "found matching uid substitute right smb_uid");
  329. buffer->Uid = ses->Suid;
  330. break;
  331. } else {
  332. /* BB eventually call cifs_setup_session here */
  333. cFYI(1, "local UID found but no smb sess with this server exists");
  334. }
  335. }
  336. }
  337. spin_unlock(&cifs_tcp_ses_lock);
  338. }
  339. }
  340. }
  341. if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
  342. buffer->Flags2 |= SMBFLG2_DFS;
  343. if (treeCon->nocase)
  344. buffer->Flags |= SMBFLG_CASELESS;
  345. if ((treeCon->ses) && (treeCon->ses->server))
  346. if (treeCon->ses->server->sec_mode &
  347. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  348. buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  349. }
  350. /* endian conversion of flags is now done just before sending */
  351. buffer->WordCount = (char) word_count;
  352. return;
  353. }
  354. static int
  355. check_smb_hdr(struct smb_hdr *smb, __u16 mid)
  356. {
  357. /* does it have the right SMB "signature" ? */
  358. if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff)) {
  359. cERROR(1, "Bad protocol string signature header 0x%x",
  360. *(unsigned int *)smb->Protocol);
  361. return 1;
  362. }
  363. /* Make sure that message ids match */
  364. if (mid != smb->Mid) {
  365. cERROR(1, "Mids do not match. received=%u expected=%u",
  366. smb->Mid, mid);
  367. return 1;
  368. }
  369. /* if it's a response then accept */
  370. if (smb->Flags & SMBFLG_RESPONSE)
  371. return 0;
  372. /* only one valid case where server sends us request */
  373. if (smb->Command == SMB_COM_LOCKING_ANDX)
  374. return 0;
  375. cERROR(1, "Server sent request, not response. mid=%u", smb->Mid);
  376. return 1;
  377. }
  378. int
  379. checkSMB(struct smb_hdr *smb, __u16 mid, unsigned int length)
  380. {
  381. __u32 len = be32_to_cpu(smb->smb_buf_length);
  382. __u32 clc_len; /* calculated length */
  383. cFYI(0, "checkSMB Length: 0x%x, smb_buf_length: 0x%x", length, len);
  384. if (length < 2 + sizeof(struct smb_hdr)) {
  385. if ((length >= sizeof(struct smb_hdr) - 1)
  386. && (smb->Status.CifsError != 0)) {
  387. smb->WordCount = 0;
  388. /* some error cases do not return wct and bcc */
  389. return 0;
  390. } else if ((length == sizeof(struct smb_hdr) + 1) &&
  391. (smb->WordCount == 0)) {
  392. char *tmp = (char *)smb;
  393. /* Need to work around a bug in two servers here */
  394. /* First, check if the part of bcc they sent was zero */
  395. if (tmp[sizeof(struct smb_hdr)] == 0) {
  396. /* some servers return only half of bcc
  397. * on simple responses (wct, bcc both zero)
  398. * in particular have seen this on
  399. * ulogoffX and FindClose. This leaves
  400. * one byte of bcc potentially unitialized
  401. */
  402. /* zero rest of bcc */
  403. tmp[sizeof(struct smb_hdr)+1] = 0;
  404. return 0;
  405. }
  406. cERROR(1, "rcvd invalid byte count (bcc)");
  407. } else {
  408. cERROR(1, "Length less than smb header size");
  409. }
  410. return 1;
  411. }
  412. if (len > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  413. cERROR(1, "smb length greater than MaxBufSize, mid=%d",
  414. smb->Mid);
  415. return 1;
  416. }
  417. if (check_smb_hdr(smb, mid))
  418. return 1;
  419. clc_len = smbCalcSize(smb);
  420. if (4 + len != length) {
  421. cERROR(1, "Length read does not match RFC1001 length %d",
  422. len);
  423. return 1;
  424. }
  425. if (4 + len != clc_len) {
  426. /* check if bcc wrapped around for large read responses */
  427. if ((len > 64 * 1024) && (len > clc_len)) {
  428. /* check if lengths match mod 64K */
  429. if (((4 + len) & 0xFFFF) == (clc_len & 0xFFFF))
  430. return 0; /* bcc wrapped */
  431. }
  432. cFYI(1, "Calculated size %u vs length %u mismatch for mid=%u",
  433. clc_len, 4 + len, smb->Mid);
  434. if (4 + len < clc_len) {
  435. cERROR(1, "RFC1001 size %u smaller than SMB for mid=%u",
  436. len, smb->Mid);
  437. return 1;
  438. } else if (len > clc_len + 512) {
  439. /*
  440. * Some servers (Windows XP in particular) send more
  441. * data than the lengths in the SMB packet would
  442. * indicate on certain calls (byte range locks and
  443. * trans2 find first calls in particular). While the
  444. * client can handle such a frame by ignoring the
  445. * trailing data, we choose limit the amount of extra
  446. * data to 512 bytes.
  447. */
  448. cERROR(1, "RFC1001 size %u more than 512 bytes larger "
  449. "than SMB for mid=%u", len, smb->Mid);
  450. return 1;
  451. }
  452. }
  453. return 0;
  454. }
  455. bool
  456. is_valid_oplock_break(struct smb_hdr *buf, struct TCP_Server_Info *srv)
  457. {
  458. struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
  459. struct list_head *tmp, *tmp1, *tmp2;
  460. struct cifs_ses *ses;
  461. struct cifs_tcon *tcon;
  462. struct cifsInodeInfo *pCifsInode;
  463. struct cifsFileInfo *netfile;
  464. cFYI(1, "Checking for oplock break or dnotify response");
  465. if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
  466. (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
  467. struct smb_com_transaction_change_notify_rsp *pSMBr =
  468. (struct smb_com_transaction_change_notify_rsp *)buf;
  469. struct file_notify_information *pnotify;
  470. __u32 data_offset = 0;
  471. if (get_bcc(buf) > sizeof(struct file_notify_information)) {
  472. data_offset = le32_to_cpu(pSMBr->DataOffset);
  473. pnotify = (struct file_notify_information *)
  474. ((char *)&pSMBr->hdr.Protocol + data_offset);
  475. cFYI(1, "dnotify on %s Action: 0x%x",
  476. pnotify->FileName, pnotify->Action);
  477. /* cifs_dump_mem("Rcvd notify Data: ",buf,
  478. sizeof(struct smb_hdr)+60); */
  479. return true;
  480. }
  481. if (pSMBr->hdr.Status.CifsError) {
  482. cFYI(1, "notify err 0x%d",
  483. pSMBr->hdr.Status.CifsError);
  484. return true;
  485. }
  486. return false;
  487. }
  488. if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
  489. return false;
  490. if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
  491. /* no sense logging error on invalid handle on oplock
  492. break - harmless race between close request and oplock
  493. break response is expected from time to time writing out
  494. large dirty files cached on the client */
  495. if ((NT_STATUS_INVALID_HANDLE) ==
  496. le32_to_cpu(pSMB->hdr.Status.CifsError)) {
  497. cFYI(1, "invalid handle on oplock break");
  498. return true;
  499. } else if (ERRbadfid ==
  500. le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
  501. return true;
  502. } else {
  503. return false; /* on valid oplock brk we get "request" */
  504. }
  505. }
  506. if (pSMB->hdr.WordCount != 8)
  507. return false;
  508. cFYI(1, "oplock type 0x%d level 0x%d",
  509. pSMB->LockType, pSMB->OplockLevel);
  510. if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
  511. return false;
  512. /* look up tcon based on tid & uid */
  513. spin_lock(&cifs_tcp_ses_lock);
  514. list_for_each(tmp, &srv->smb_ses_list) {
  515. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  516. list_for_each(tmp1, &ses->tcon_list) {
  517. tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
  518. if (tcon->tid != buf->Tid)
  519. continue;
  520. cifs_stats_inc(&tcon->num_oplock_brks);
  521. spin_lock(&cifs_file_list_lock);
  522. list_for_each(tmp2, &tcon->openFileList) {
  523. netfile = list_entry(tmp2, struct cifsFileInfo,
  524. tlist);
  525. if (pSMB->Fid != netfile->netfid)
  526. continue;
  527. cFYI(1, "file id match, oplock break");
  528. pCifsInode = CIFS_I(netfile->dentry->d_inode);
  529. cifs_set_oplock_level(pCifsInode,
  530. pSMB->OplockLevel ? OPLOCK_READ : 0);
  531. /*
  532. * cifs_oplock_break_put() can't be called
  533. * from here. Get reference after queueing
  534. * succeeded. cifs_oplock_break() will
  535. * synchronize using cifs_file_list_lock.
  536. */
  537. if (queue_work(system_nrt_wq,
  538. &netfile->oplock_break))
  539. cifs_oplock_break_get(netfile);
  540. netfile->oplock_break_cancelled = false;
  541. spin_unlock(&cifs_file_list_lock);
  542. spin_unlock(&cifs_tcp_ses_lock);
  543. return true;
  544. }
  545. spin_unlock(&cifs_file_list_lock);
  546. spin_unlock(&cifs_tcp_ses_lock);
  547. cFYI(1, "No matching file for oplock break");
  548. return true;
  549. }
  550. }
  551. spin_unlock(&cifs_tcp_ses_lock);
  552. cFYI(1, "Can not process oplock break for non-existent connection");
  553. return true;
  554. }
  555. void
  556. dump_smb(struct smb_hdr *smb_buf, int smb_buf_length)
  557. {
  558. int i, j;
  559. char debug_line[17];
  560. unsigned char *buffer;
  561. if (traceSMB == 0)
  562. return;
  563. buffer = (unsigned char *) smb_buf;
  564. for (i = 0, j = 0; i < smb_buf_length; i++, j++) {
  565. if (i % 8 == 0) {
  566. /* have reached the beginning of line */
  567. printk(KERN_DEBUG "| ");
  568. j = 0;
  569. }
  570. printk("%0#4x ", buffer[i]);
  571. debug_line[2 * j] = ' ';
  572. if (isprint(buffer[i]))
  573. debug_line[1 + (2 * j)] = buffer[i];
  574. else
  575. debug_line[1 + (2 * j)] = '_';
  576. if (i % 8 == 7) {
  577. /* reached end of line, time to print ascii */
  578. debug_line[16] = 0;
  579. printk(" | %s\n", debug_line);
  580. }
  581. }
  582. for (; j < 8; j++) {
  583. printk(" ");
  584. debug_line[2 * j] = ' ';
  585. debug_line[1 + (2 * j)] = ' ';
  586. }
  587. printk(" | %s\n", debug_line);
  588. return;
  589. }
  590. void
  591. cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb)
  592. {
  593. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  594. cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
  595. cERROR(1, "Autodisabling the use of server inode numbers on "
  596. "%s. This server doesn't seem to support them "
  597. "properly. Hardlinks will not be recognized on this "
  598. "mount. Consider mounting with the \"noserverino\" "
  599. "option to silence this message.",
  600. cifs_sb_master_tcon(cifs_sb)->treeName);
  601. }
  602. }
  603. void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
  604. {
  605. oplock &= 0xF;
  606. if (oplock == OPLOCK_EXCLUSIVE) {
  607. cinode->clientCanCacheAll = true;
  608. cinode->clientCanCacheRead = true;
  609. cFYI(1, "Exclusive Oplock granted on inode %p",
  610. &cinode->vfs_inode);
  611. } else if (oplock == OPLOCK_READ) {
  612. cinode->clientCanCacheAll = false;
  613. cinode->clientCanCacheRead = true;
  614. cFYI(1, "Level II Oplock granted on inode %p",
  615. &cinode->vfs_inode);
  616. } else {
  617. cinode->clientCanCacheAll = false;
  618. cinode->clientCanCacheRead = false;
  619. }
  620. }