misc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. /* NB: MID can not be set if treeCon not passed in, in that
  190. case it is responsbility of caller to set the mid */
  191. void
  192. header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
  193. const struct cifs_tcon *treeCon, int word_count
  194. /* length of fixed section (word count) in two byte units */)
  195. {
  196. char *temp = (char *) buffer;
  197. memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
  198. buffer->smb_buf_length = cpu_to_be32(
  199. (2 * word_count) + sizeof(struct smb_hdr) -
  200. 4 /* RFC 1001 length field does not count */ +
  201. 2 /* for bcc field itself */) ;
  202. buffer->Protocol[0] = 0xFF;
  203. buffer->Protocol[1] = 'S';
  204. buffer->Protocol[2] = 'M';
  205. buffer->Protocol[3] = 'B';
  206. buffer->Command = smb_command;
  207. buffer->Flags = 0x00; /* case sensitive */
  208. buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
  209. buffer->Pid = cpu_to_le16((__u16)current->tgid);
  210. buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
  211. if (treeCon) {
  212. buffer->Tid = treeCon->tid;
  213. if (treeCon->ses) {
  214. if (treeCon->ses->capabilities & CAP_UNICODE)
  215. buffer->Flags2 |= SMBFLG2_UNICODE;
  216. if (treeCon->ses->capabilities & CAP_STATUS32)
  217. buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  218. /* Uid is not converted */
  219. buffer->Uid = treeCon->ses->Suid;
  220. buffer->Mid = get_next_mid(treeCon->ses->server);
  221. }
  222. if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
  223. buffer->Flags2 |= SMBFLG2_DFS;
  224. if (treeCon->nocase)
  225. buffer->Flags |= SMBFLG_CASELESS;
  226. if ((treeCon->ses) && (treeCon->ses->server))
  227. if (treeCon->ses->server->sec_mode &
  228. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  229. buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  230. }
  231. /* endian conversion of flags is now done just before sending */
  232. buffer->WordCount = (char) word_count;
  233. return;
  234. }
  235. static int
  236. check_smb_hdr(struct smb_hdr *smb, __u16 mid)
  237. {
  238. /* does it have the right SMB "signature" ? */
  239. if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff)) {
  240. cERROR(1, "Bad protocol string signature header 0x%x",
  241. *(unsigned int *)smb->Protocol);
  242. return 1;
  243. }
  244. /* Make sure that message ids match */
  245. if (mid != smb->Mid) {
  246. cERROR(1, "Mids do not match. received=%u expected=%u",
  247. smb->Mid, mid);
  248. return 1;
  249. }
  250. /* if it's a response then accept */
  251. if (smb->Flags & SMBFLG_RESPONSE)
  252. return 0;
  253. /* only one valid case where server sends us request */
  254. if (smb->Command == SMB_COM_LOCKING_ANDX)
  255. return 0;
  256. cERROR(1, "Server sent request, not response. mid=%u", smb->Mid);
  257. return 1;
  258. }
  259. int
  260. checkSMB(char *buf, unsigned int total_read)
  261. {
  262. struct smb_hdr *smb = (struct smb_hdr *)buf;
  263. __u16 mid = smb->Mid;
  264. __u32 rfclen = be32_to_cpu(smb->smb_buf_length);
  265. __u32 clc_len; /* calculated length */
  266. cFYI(0, "checkSMB Length: 0x%x, smb_buf_length: 0x%x",
  267. total_read, rfclen);
  268. /* is this frame too small to even get to a BCC? */
  269. if (total_read < 2 + sizeof(struct smb_hdr)) {
  270. if ((total_read >= sizeof(struct smb_hdr) - 1)
  271. && (smb->Status.CifsError != 0)) {
  272. /* it's an error return */
  273. smb->WordCount = 0;
  274. /* some error cases do not return wct and bcc */
  275. return 0;
  276. } else if ((total_read == sizeof(struct smb_hdr) + 1) &&
  277. (smb->WordCount == 0)) {
  278. char *tmp = (char *)smb;
  279. /* Need to work around a bug in two servers here */
  280. /* First, check if the part of bcc they sent was zero */
  281. if (tmp[sizeof(struct smb_hdr)] == 0) {
  282. /* some servers return only half of bcc
  283. * on simple responses (wct, bcc both zero)
  284. * in particular have seen this on
  285. * ulogoffX and FindClose. This leaves
  286. * one byte of bcc potentially unitialized
  287. */
  288. /* zero rest of bcc */
  289. tmp[sizeof(struct smb_hdr)+1] = 0;
  290. return 0;
  291. }
  292. cERROR(1, "rcvd invalid byte count (bcc)");
  293. } else {
  294. cERROR(1, "Length less than smb header size");
  295. }
  296. return -EIO;
  297. }
  298. /* otherwise, there is enough to get to the BCC */
  299. if (check_smb_hdr(smb, mid))
  300. return -EIO;
  301. clc_len = smbCalcSize(smb);
  302. if (4 + rfclen != total_read) {
  303. cERROR(1, "Length read does not match RFC1001 length %d",
  304. rfclen);
  305. return -EIO;
  306. }
  307. if (4 + rfclen != clc_len) {
  308. /* check if bcc wrapped around for large read responses */
  309. if ((rfclen > 64 * 1024) && (rfclen > clc_len)) {
  310. /* check if lengths match mod 64K */
  311. if (((4 + rfclen) & 0xFFFF) == (clc_len & 0xFFFF))
  312. return 0; /* bcc wrapped */
  313. }
  314. cFYI(1, "Calculated size %u vs length %u mismatch for mid=%u",
  315. clc_len, 4 + rfclen, smb->Mid);
  316. if (4 + rfclen < clc_len) {
  317. cERROR(1, "RFC1001 size %u smaller than SMB for mid=%u",
  318. rfclen, smb->Mid);
  319. return -EIO;
  320. } else if (rfclen > clc_len + 512) {
  321. /*
  322. * Some servers (Windows XP in particular) send more
  323. * data than the lengths in the SMB packet would
  324. * indicate on certain calls (byte range locks and
  325. * trans2 find first calls in particular). While the
  326. * client can handle such a frame by ignoring the
  327. * trailing data, we choose limit the amount of extra
  328. * data to 512 bytes.
  329. */
  330. cERROR(1, "RFC1001 size %u more than 512 bytes larger "
  331. "than SMB for mid=%u", rfclen, smb->Mid);
  332. return -EIO;
  333. }
  334. }
  335. return 0;
  336. }
  337. bool
  338. is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
  339. {
  340. struct smb_hdr *buf = (struct smb_hdr *)buffer;
  341. struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
  342. struct list_head *tmp, *tmp1, *tmp2;
  343. struct cifs_ses *ses;
  344. struct cifs_tcon *tcon;
  345. struct cifsInodeInfo *pCifsInode;
  346. struct cifsFileInfo *netfile;
  347. cFYI(1, "Checking for oplock break or dnotify response");
  348. if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
  349. (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
  350. struct smb_com_transaction_change_notify_rsp *pSMBr =
  351. (struct smb_com_transaction_change_notify_rsp *)buf;
  352. struct file_notify_information *pnotify;
  353. __u32 data_offset = 0;
  354. if (get_bcc(buf) > sizeof(struct file_notify_information)) {
  355. data_offset = le32_to_cpu(pSMBr->DataOffset);
  356. pnotify = (struct file_notify_information *)
  357. ((char *)&pSMBr->hdr.Protocol + data_offset);
  358. cFYI(1, "dnotify on %s Action: 0x%x",
  359. pnotify->FileName, pnotify->Action);
  360. /* cifs_dump_mem("Rcvd notify Data: ",buf,
  361. sizeof(struct smb_hdr)+60); */
  362. return true;
  363. }
  364. if (pSMBr->hdr.Status.CifsError) {
  365. cFYI(1, "notify err 0x%d",
  366. pSMBr->hdr.Status.CifsError);
  367. return true;
  368. }
  369. return false;
  370. }
  371. if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
  372. return false;
  373. if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
  374. /* no sense logging error on invalid handle on oplock
  375. break - harmless race between close request and oplock
  376. break response is expected from time to time writing out
  377. large dirty files cached on the client */
  378. if ((NT_STATUS_INVALID_HANDLE) ==
  379. le32_to_cpu(pSMB->hdr.Status.CifsError)) {
  380. cFYI(1, "invalid handle on oplock break");
  381. return true;
  382. } else if (ERRbadfid ==
  383. le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
  384. return true;
  385. } else {
  386. return false; /* on valid oplock brk we get "request" */
  387. }
  388. }
  389. if (pSMB->hdr.WordCount != 8)
  390. return false;
  391. cFYI(1, "oplock type 0x%d level 0x%d",
  392. pSMB->LockType, pSMB->OplockLevel);
  393. if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
  394. return false;
  395. /* look up tcon based on tid & uid */
  396. spin_lock(&cifs_tcp_ses_lock);
  397. list_for_each(tmp, &srv->smb_ses_list) {
  398. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  399. list_for_each(tmp1, &ses->tcon_list) {
  400. tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
  401. if (tcon->tid != buf->Tid)
  402. continue;
  403. cifs_stats_inc(&tcon->num_oplock_brks);
  404. spin_lock(&cifs_file_list_lock);
  405. list_for_each(tmp2, &tcon->openFileList) {
  406. netfile = list_entry(tmp2, struct cifsFileInfo,
  407. tlist);
  408. if (pSMB->Fid != netfile->netfid)
  409. continue;
  410. cFYI(1, "file id match, oplock break");
  411. pCifsInode = CIFS_I(netfile->dentry->d_inode);
  412. cifs_set_oplock_level(pCifsInode,
  413. pSMB->OplockLevel ? OPLOCK_READ : 0);
  414. queue_work(cifsiod_wq,
  415. &netfile->oplock_break);
  416. netfile->oplock_break_cancelled = false;
  417. spin_unlock(&cifs_file_list_lock);
  418. spin_unlock(&cifs_tcp_ses_lock);
  419. return true;
  420. }
  421. spin_unlock(&cifs_file_list_lock);
  422. spin_unlock(&cifs_tcp_ses_lock);
  423. cFYI(1, "No matching file for oplock break");
  424. return true;
  425. }
  426. }
  427. spin_unlock(&cifs_tcp_ses_lock);
  428. cFYI(1, "Can not process oplock break for non-existent connection");
  429. return true;
  430. }
  431. void
  432. dump_smb(void *buf, int smb_buf_length)
  433. {
  434. int i, j;
  435. char debug_line[17];
  436. unsigned char *buffer = buf;
  437. if (traceSMB == 0)
  438. return;
  439. for (i = 0, j = 0; i < smb_buf_length; i++, j++) {
  440. if (i % 8 == 0) {
  441. /* have reached the beginning of line */
  442. printk(KERN_DEBUG "| ");
  443. j = 0;
  444. }
  445. printk("%0#4x ", buffer[i]);
  446. debug_line[2 * j] = ' ';
  447. if (isprint(buffer[i]))
  448. debug_line[1 + (2 * j)] = buffer[i];
  449. else
  450. debug_line[1 + (2 * j)] = '_';
  451. if (i % 8 == 7) {
  452. /* reached end of line, time to print ascii */
  453. debug_line[16] = 0;
  454. printk(" | %s\n", debug_line);
  455. }
  456. }
  457. for (; j < 8; j++) {
  458. printk(" ");
  459. debug_line[2 * j] = ' ';
  460. debug_line[1 + (2 * j)] = ' ';
  461. }
  462. printk(" | %s\n", debug_line);
  463. return;
  464. }
  465. void
  466. cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb)
  467. {
  468. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  469. cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
  470. cERROR(1, "Autodisabling the use of server inode numbers on "
  471. "%s. This server doesn't seem to support them "
  472. "properly. Hardlinks will not be recognized on this "
  473. "mount. Consider mounting with the \"noserverino\" "
  474. "option to silence this message.",
  475. cifs_sb_master_tcon(cifs_sb)->treeName);
  476. }
  477. }
  478. void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
  479. {
  480. oplock &= 0xF;
  481. if (oplock == OPLOCK_EXCLUSIVE) {
  482. cinode->clientCanCacheAll = true;
  483. cinode->clientCanCacheRead = true;
  484. cFYI(1, "Exclusive Oplock granted on inode %p",
  485. &cinode->vfs_inode);
  486. } else if (oplock == OPLOCK_READ) {
  487. cinode->clientCanCacheAll = false;
  488. cinode->clientCanCacheRead = true;
  489. cFYI(1, "Level II Oplock granted on inode %p",
  490. &cinode->vfs_inode);
  491. } else {
  492. cinode->clientCanCacheAll = false;
  493. cinode->clientCanCacheRead = false;
  494. }
  495. }
  496. bool
  497. backup_cred(struct cifs_sb_info *cifs_sb)
  498. {
  499. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID) {
  500. if (cifs_sb->mnt_backupuid == current_fsuid())
  501. return true;
  502. }
  503. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID) {
  504. if (in_group_p(cifs_sb->mnt_backupgid))
  505. return true;
  506. }
  507. return false;
  508. }