misc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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 <linux/vmalloc.h>
  25. #include "cifspdu.h"
  26. #include "cifsglob.h"
  27. #include "cifsproto.h"
  28. #include "cifs_debug.h"
  29. #include "smberr.h"
  30. #include "nterr.h"
  31. #include "cifs_unicode.h"
  32. #include "smb2pdu.h"
  33. extern mempool_t *cifs_sm_req_poolp;
  34. extern mempool_t *cifs_req_poolp;
  35. /* The xid serves as a useful identifier for each incoming vfs request,
  36. in a similar way to the mid which is useful to track each sent smb,
  37. and CurrentXid can also provide a running counter (although it
  38. will eventually wrap past zero) of the total vfs operations handled
  39. since the cifs fs was mounted */
  40. unsigned int
  41. _get_xid(void)
  42. {
  43. unsigned int xid;
  44. spin_lock(&GlobalMid_Lock);
  45. GlobalTotalActiveXid++;
  46. /* keep high water mark for number of simultaneous ops in filesystem */
  47. if (GlobalTotalActiveXid > GlobalMaxActiveXid)
  48. GlobalMaxActiveXid = GlobalTotalActiveXid;
  49. if (GlobalTotalActiveXid > 65000)
  50. cifs_dbg(FYI, "warning: more than 65000 requests active\n");
  51. xid = GlobalCurrentXid++;
  52. spin_unlock(&GlobalMid_Lock);
  53. return xid;
  54. }
  55. void
  56. _free_xid(unsigned int xid)
  57. {
  58. spin_lock(&GlobalMid_Lock);
  59. /* if (GlobalTotalActiveXid == 0)
  60. BUG(); */
  61. GlobalTotalActiveXid--;
  62. spin_unlock(&GlobalMid_Lock);
  63. }
  64. struct cifs_ses *
  65. sesInfoAlloc(void)
  66. {
  67. struct cifs_ses *ret_buf;
  68. ret_buf = kzalloc(sizeof(struct cifs_ses), GFP_KERNEL);
  69. if (ret_buf) {
  70. atomic_inc(&sesInfoAllocCount);
  71. ret_buf->status = CifsNew;
  72. ++ret_buf->ses_count;
  73. INIT_LIST_HEAD(&ret_buf->smb_ses_list);
  74. INIT_LIST_HEAD(&ret_buf->tcon_list);
  75. mutex_init(&ret_buf->session_mutex);
  76. }
  77. return ret_buf;
  78. }
  79. void
  80. sesInfoFree(struct cifs_ses *buf_to_free)
  81. {
  82. if (buf_to_free == NULL) {
  83. cifs_dbg(FYI, "Null buffer passed to sesInfoFree\n");
  84. return;
  85. }
  86. atomic_dec(&sesInfoAllocCount);
  87. kfree(buf_to_free->serverOS);
  88. kfree(buf_to_free->serverDomain);
  89. kfree(buf_to_free->serverNOS);
  90. kzfree(buf_to_free->password);
  91. kfree(buf_to_free->user_name);
  92. kfree(buf_to_free->domainName);
  93. kzfree(buf_to_free->auth_key.response);
  94. kzfree(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. spin_lock_init(&ret_buf->open_file_lock);
  108. mutex_init(&ret_buf->prfid_mutex);
  109. ret_buf->prfid = kzalloc(sizeof(struct cifs_fid), GFP_KERNEL);
  110. #ifdef CONFIG_CIFS_STATS
  111. spin_lock_init(&ret_buf->stat_lock);
  112. #endif
  113. }
  114. return ret_buf;
  115. }
  116. void
  117. tconInfoFree(struct cifs_tcon *buf_to_free)
  118. {
  119. if (buf_to_free == NULL) {
  120. cifs_dbg(FYI, "Null buffer passed to tconInfoFree\n");
  121. return;
  122. }
  123. atomic_dec(&tconInfoAllocCount);
  124. kfree(buf_to_free->nativeFileSystem);
  125. kzfree(buf_to_free->password);
  126. kfree(buf_to_free->prfid);
  127. kfree(buf_to_free);
  128. }
  129. struct smb_hdr *
  130. cifs_buf_get(void)
  131. {
  132. struct smb_hdr *ret_buf = NULL;
  133. /*
  134. * SMB2 header is bigger than CIFS one - no problems to clean some
  135. * more bytes for CIFS.
  136. */
  137. size_t buf_size = sizeof(struct smb2_sync_hdr);
  138. /*
  139. * We could use negotiated size instead of max_msgsize -
  140. * but it may be more efficient to always alloc same size
  141. * albeit slightly larger than necessary and maxbuffersize
  142. * defaults to this and can not be bigger.
  143. */
  144. ret_buf = mempool_alloc(cifs_req_poolp, GFP_NOFS);
  145. /* clear the first few header bytes */
  146. /* for most paths, more is cleared in header_assemble */
  147. memset(ret_buf, 0, buf_size + 3);
  148. atomic_inc(&bufAllocCount);
  149. #ifdef CONFIG_CIFS_STATS2
  150. atomic_inc(&totBufAllocCount);
  151. #endif /* CONFIG_CIFS_STATS2 */
  152. return ret_buf;
  153. }
  154. void
  155. cifs_buf_release(void *buf_to_free)
  156. {
  157. if (buf_to_free == NULL) {
  158. /* cifs_dbg(FYI, "Null buffer passed to cifs_buf_release\n");*/
  159. return;
  160. }
  161. mempool_free(buf_to_free, cifs_req_poolp);
  162. atomic_dec(&bufAllocCount);
  163. return;
  164. }
  165. struct smb_hdr *
  166. cifs_small_buf_get(void)
  167. {
  168. struct smb_hdr *ret_buf = NULL;
  169. /* We could use negotiated size instead of max_msgsize -
  170. but it may be more efficient to always alloc same size
  171. albeit slightly larger than necessary and maxbuffersize
  172. defaults to this and can not be bigger */
  173. ret_buf = mempool_alloc(cifs_sm_req_poolp, GFP_NOFS);
  174. /* No need to clear memory here, cleared in header assemble */
  175. /* memset(ret_buf, 0, sizeof(struct smb_hdr) + 27);*/
  176. atomic_inc(&smBufAllocCount);
  177. #ifdef CONFIG_CIFS_STATS2
  178. atomic_inc(&totSmBufAllocCount);
  179. #endif /* CONFIG_CIFS_STATS2 */
  180. return ret_buf;
  181. }
  182. void
  183. cifs_small_buf_release(void *buf_to_free)
  184. {
  185. if (buf_to_free == NULL) {
  186. cifs_dbg(FYI, "Null buffer passed to cifs_small_buf_release\n");
  187. return;
  188. }
  189. mempool_free(buf_to_free, cifs_sm_req_poolp);
  190. atomic_dec(&smBufAllocCount);
  191. return;
  192. }
  193. void
  194. free_rsp_buf(int resp_buftype, void *rsp)
  195. {
  196. if (resp_buftype == CIFS_SMALL_BUFFER)
  197. cifs_small_buf_release(rsp);
  198. else if (resp_buftype == CIFS_LARGE_BUFFER)
  199. cifs_buf_release(rsp);
  200. }
  201. /* NB: MID can not be set if treeCon not passed in, in that
  202. case it is responsbility of caller to set the mid */
  203. void
  204. header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
  205. const struct cifs_tcon *treeCon, int word_count
  206. /* length of fixed section (word count) in two byte units */)
  207. {
  208. char *temp = (char *) buffer;
  209. memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
  210. buffer->smb_buf_length = cpu_to_be32(
  211. (2 * word_count) + sizeof(struct smb_hdr) -
  212. 4 /* RFC 1001 length field does not count */ +
  213. 2 /* for bcc field itself */) ;
  214. buffer->Protocol[0] = 0xFF;
  215. buffer->Protocol[1] = 'S';
  216. buffer->Protocol[2] = 'M';
  217. buffer->Protocol[3] = 'B';
  218. buffer->Command = smb_command;
  219. buffer->Flags = 0x00; /* case sensitive */
  220. buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
  221. buffer->Pid = cpu_to_le16((__u16)current->tgid);
  222. buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
  223. if (treeCon) {
  224. buffer->Tid = treeCon->tid;
  225. if (treeCon->ses) {
  226. if (treeCon->ses->capabilities & CAP_UNICODE)
  227. buffer->Flags2 |= SMBFLG2_UNICODE;
  228. if (treeCon->ses->capabilities & CAP_STATUS32)
  229. buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  230. /* Uid is not converted */
  231. buffer->Uid = treeCon->ses->Suid;
  232. buffer->Mid = get_next_mid(treeCon->ses->server);
  233. }
  234. if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
  235. buffer->Flags2 |= SMBFLG2_DFS;
  236. if (treeCon->nocase)
  237. buffer->Flags |= SMBFLG_CASELESS;
  238. if ((treeCon->ses) && (treeCon->ses->server))
  239. if (treeCon->ses->server->sign)
  240. buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  241. }
  242. /* endian conversion of flags is now done just before sending */
  243. buffer->WordCount = (char) word_count;
  244. return;
  245. }
  246. static int
  247. check_smb_hdr(struct smb_hdr *smb)
  248. {
  249. /* does it have the right SMB "signature" ? */
  250. if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff)) {
  251. cifs_dbg(VFS, "Bad protocol string signature header 0x%x\n",
  252. *(unsigned int *)smb->Protocol);
  253. return 1;
  254. }
  255. /* if it's a response then accept */
  256. if (smb->Flags & SMBFLG_RESPONSE)
  257. return 0;
  258. /* only one valid case where server sends us request */
  259. if (smb->Command == SMB_COM_LOCKING_ANDX)
  260. return 0;
  261. cifs_dbg(VFS, "Server sent request, not response. mid=%u\n",
  262. get_mid(smb));
  263. return 1;
  264. }
  265. int
  266. checkSMB(char *buf, unsigned int total_read, struct TCP_Server_Info *server)
  267. {
  268. struct smb_hdr *smb = (struct smb_hdr *)buf;
  269. __u32 rfclen = be32_to_cpu(smb->smb_buf_length);
  270. __u32 clc_len; /* calculated length */
  271. cifs_dbg(FYI, "checkSMB Length: 0x%x, smb_buf_length: 0x%x\n",
  272. total_read, rfclen);
  273. /* is this frame too small to even get to a BCC? */
  274. if (total_read < 2 + sizeof(struct smb_hdr)) {
  275. if ((total_read >= sizeof(struct smb_hdr) - 1)
  276. && (smb->Status.CifsError != 0)) {
  277. /* it's an error return */
  278. smb->WordCount = 0;
  279. /* some error cases do not return wct and bcc */
  280. return 0;
  281. } else if ((total_read == sizeof(struct smb_hdr) + 1) &&
  282. (smb->WordCount == 0)) {
  283. char *tmp = (char *)smb;
  284. /* Need to work around a bug in two servers here */
  285. /* First, check if the part of bcc they sent was zero */
  286. if (tmp[sizeof(struct smb_hdr)] == 0) {
  287. /* some servers return only half of bcc
  288. * on simple responses (wct, bcc both zero)
  289. * in particular have seen this on
  290. * ulogoffX and FindClose. This leaves
  291. * one byte of bcc potentially unitialized
  292. */
  293. /* zero rest of bcc */
  294. tmp[sizeof(struct smb_hdr)+1] = 0;
  295. return 0;
  296. }
  297. cifs_dbg(VFS, "rcvd invalid byte count (bcc)\n");
  298. } else {
  299. cifs_dbg(VFS, "Length less than smb header size\n");
  300. }
  301. return -EIO;
  302. }
  303. /* otherwise, there is enough to get to the BCC */
  304. if (check_smb_hdr(smb))
  305. return -EIO;
  306. clc_len = smbCalcSize(smb, server);
  307. if (4 + rfclen != total_read) {
  308. cifs_dbg(VFS, "Length read does not match RFC1001 length %d\n",
  309. rfclen);
  310. return -EIO;
  311. }
  312. if (4 + rfclen != clc_len) {
  313. __u16 mid = get_mid(smb);
  314. /* check if bcc wrapped around for large read responses */
  315. if ((rfclen > 64 * 1024) && (rfclen > clc_len)) {
  316. /* check if lengths match mod 64K */
  317. if (((4 + rfclen) & 0xFFFF) == (clc_len & 0xFFFF))
  318. return 0; /* bcc wrapped */
  319. }
  320. cifs_dbg(FYI, "Calculated size %u vs length %u mismatch for mid=%u\n",
  321. clc_len, 4 + rfclen, mid);
  322. if (4 + rfclen < clc_len) {
  323. cifs_dbg(VFS, "RFC1001 size %u smaller than SMB for mid=%u\n",
  324. rfclen, mid);
  325. return -EIO;
  326. } else if (rfclen > clc_len + 512) {
  327. /*
  328. * Some servers (Windows XP in particular) send more
  329. * data than the lengths in the SMB packet would
  330. * indicate on certain calls (byte range locks and
  331. * trans2 find first calls in particular). While the
  332. * client can handle such a frame by ignoring the
  333. * trailing data, we choose limit the amount of extra
  334. * data to 512 bytes.
  335. */
  336. cifs_dbg(VFS, "RFC1001 size %u more than 512 bytes larger than SMB for mid=%u\n",
  337. rfclen, mid);
  338. return -EIO;
  339. }
  340. }
  341. return 0;
  342. }
  343. bool
  344. is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
  345. {
  346. struct smb_hdr *buf = (struct smb_hdr *)buffer;
  347. struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
  348. struct list_head *tmp, *tmp1, *tmp2;
  349. struct cifs_ses *ses;
  350. struct cifs_tcon *tcon;
  351. struct cifsInodeInfo *pCifsInode;
  352. struct cifsFileInfo *netfile;
  353. cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
  354. if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
  355. (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
  356. struct smb_com_transaction_change_notify_rsp *pSMBr =
  357. (struct smb_com_transaction_change_notify_rsp *)buf;
  358. struct file_notify_information *pnotify;
  359. __u32 data_offset = 0;
  360. if (get_bcc(buf) > sizeof(struct file_notify_information)) {
  361. data_offset = le32_to_cpu(pSMBr->DataOffset);
  362. pnotify = (struct file_notify_information *)
  363. ((char *)&pSMBr->hdr.Protocol + data_offset);
  364. cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
  365. pnotify->FileName, pnotify->Action);
  366. /* cifs_dump_mem("Rcvd notify Data: ",buf,
  367. sizeof(struct smb_hdr)+60); */
  368. return true;
  369. }
  370. if (pSMBr->hdr.Status.CifsError) {
  371. cifs_dbg(FYI, "notify err 0x%x\n",
  372. pSMBr->hdr.Status.CifsError);
  373. return true;
  374. }
  375. return false;
  376. }
  377. if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
  378. return false;
  379. if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
  380. /* no sense logging error on invalid handle on oplock
  381. break - harmless race between close request and oplock
  382. break response is expected from time to time writing out
  383. large dirty files cached on the client */
  384. if ((NT_STATUS_INVALID_HANDLE) ==
  385. le32_to_cpu(pSMB->hdr.Status.CifsError)) {
  386. cifs_dbg(FYI, "invalid handle on oplock break\n");
  387. return true;
  388. } else if (ERRbadfid ==
  389. le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
  390. return true;
  391. } else {
  392. return false; /* on valid oplock brk we get "request" */
  393. }
  394. }
  395. if (pSMB->hdr.WordCount != 8)
  396. return false;
  397. cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
  398. pSMB->LockType, pSMB->OplockLevel);
  399. if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
  400. return false;
  401. /* look up tcon based on tid & uid */
  402. spin_lock(&cifs_tcp_ses_lock);
  403. list_for_each(tmp, &srv->smb_ses_list) {
  404. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  405. list_for_each(tmp1, &ses->tcon_list) {
  406. tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
  407. if (tcon->tid != buf->Tid)
  408. continue;
  409. cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
  410. spin_lock(&tcon->open_file_lock);
  411. list_for_each(tmp2, &tcon->openFileList) {
  412. netfile = list_entry(tmp2, struct cifsFileInfo,
  413. tlist);
  414. if (pSMB->Fid != netfile->fid.netfid)
  415. continue;
  416. cifs_dbg(FYI, "file id match, oplock break\n");
  417. pCifsInode = CIFS_I(d_inode(netfile->dentry));
  418. set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
  419. &pCifsInode->flags);
  420. /*
  421. * Set flag if the server downgrades the oplock
  422. * to L2 else clear.
  423. */
  424. if (pSMB->OplockLevel)
  425. set_bit(
  426. CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2,
  427. &pCifsInode->flags);
  428. else
  429. clear_bit(
  430. CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2,
  431. &pCifsInode->flags);
  432. queue_work(cifsoplockd_wq,
  433. &netfile->oplock_break);
  434. netfile->oplock_break_cancelled = false;
  435. spin_unlock(&tcon->open_file_lock);
  436. spin_unlock(&cifs_tcp_ses_lock);
  437. return true;
  438. }
  439. spin_unlock(&tcon->open_file_lock);
  440. spin_unlock(&cifs_tcp_ses_lock);
  441. cifs_dbg(FYI, "No matching file for oplock break\n");
  442. return true;
  443. }
  444. }
  445. spin_unlock(&cifs_tcp_ses_lock);
  446. cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
  447. return true;
  448. }
  449. void
  450. dump_smb(void *buf, int smb_buf_length)
  451. {
  452. if (traceSMB == 0)
  453. return;
  454. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE, 8, 2, buf,
  455. smb_buf_length, true);
  456. }
  457. void
  458. cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb)
  459. {
  460. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  461. cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
  462. cifs_dbg(VFS, "Autodisabling the use of server inode numbers on %s. This server doesn't seem to support them properly. Hardlinks will not be recognized on this mount. Consider mounting with the \"noserverino\" option to silence this message.\n",
  463. cifs_sb_master_tcon(cifs_sb)->treeName);
  464. }
  465. }
  466. void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
  467. {
  468. oplock &= 0xF;
  469. if (oplock == OPLOCK_EXCLUSIVE) {
  470. cinode->oplock = CIFS_CACHE_WRITE_FLG | CIFS_CACHE_READ_FLG;
  471. cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
  472. &cinode->vfs_inode);
  473. } else if (oplock == OPLOCK_READ) {
  474. cinode->oplock = CIFS_CACHE_READ_FLG;
  475. cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
  476. &cinode->vfs_inode);
  477. } else
  478. cinode->oplock = 0;
  479. }
  480. /*
  481. * We wait for oplock breaks to be processed before we attempt to perform
  482. * writes.
  483. */
  484. int cifs_get_writer(struct cifsInodeInfo *cinode)
  485. {
  486. int rc;
  487. start:
  488. rc = wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK,
  489. TASK_KILLABLE);
  490. if (rc)
  491. return rc;
  492. spin_lock(&cinode->writers_lock);
  493. if (!cinode->writers)
  494. set_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  495. cinode->writers++;
  496. /* Check to see if we have started servicing an oplock break */
  497. if (test_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags)) {
  498. cinode->writers--;
  499. if (cinode->writers == 0) {
  500. clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  501. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
  502. }
  503. spin_unlock(&cinode->writers_lock);
  504. goto start;
  505. }
  506. spin_unlock(&cinode->writers_lock);
  507. return 0;
  508. }
  509. void cifs_put_writer(struct cifsInodeInfo *cinode)
  510. {
  511. spin_lock(&cinode->writers_lock);
  512. cinode->writers--;
  513. if (cinode->writers == 0) {
  514. clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  515. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
  516. }
  517. spin_unlock(&cinode->writers_lock);
  518. }
  519. void cifs_done_oplock_break(struct cifsInodeInfo *cinode)
  520. {
  521. clear_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags);
  522. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK);
  523. }
  524. bool
  525. backup_cred(struct cifs_sb_info *cifs_sb)
  526. {
  527. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID) {
  528. if (uid_eq(cifs_sb->mnt_backupuid, current_fsuid()))
  529. return true;
  530. }
  531. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID) {
  532. if (in_group_p(cifs_sb->mnt_backupgid))
  533. return true;
  534. }
  535. return false;
  536. }
  537. void
  538. cifs_del_pending_open(struct cifs_pending_open *open)
  539. {
  540. spin_lock(&tlink_tcon(open->tlink)->open_file_lock);
  541. list_del(&open->olist);
  542. spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
  543. }
  544. void
  545. cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink,
  546. struct cifs_pending_open *open)
  547. {
  548. memcpy(open->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
  549. open->oplock = CIFS_OPLOCK_NO_CHANGE;
  550. open->tlink = tlink;
  551. fid->pending_open = open;
  552. list_add_tail(&open->olist, &tlink_tcon(tlink)->pending_opens);
  553. }
  554. void
  555. cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink,
  556. struct cifs_pending_open *open)
  557. {
  558. spin_lock(&tlink_tcon(tlink)->open_file_lock);
  559. cifs_add_pending_open_locked(fid, tlink, open);
  560. spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
  561. }
  562. /* parses DFS refferal V3 structure
  563. * caller is responsible for freeing target_nodes
  564. * returns:
  565. * - on success - 0
  566. * - on failure - errno
  567. */
  568. int
  569. parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size,
  570. unsigned int *num_of_nodes,
  571. struct dfs_info3_param **target_nodes,
  572. const struct nls_table *nls_codepage, int remap,
  573. const char *searchName, bool is_unicode)
  574. {
  575. int i, rc = 0;
  576. char *data_end;
  577. struct dfs_referral_level_3 *ref;
  578. *num_of_nodes = le16_to_cpu(rsp->NumberOfReferrals);
  579. if (*num_of_nodes < 1) {
  580. cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
  581. *num_of_nodes);
  582. rc = -EINVAL;
  583. goto parse_DFS_referrals_exit;
  584. }
  585. ref = (struct dfs_referral_level_3 *) &(rsp->referrals);
  586. if (ref->VersionNumber != cpu_to_le16(3)) {
  587. cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
  588. le16_to_cpu(ref->VersionNumber));
  589. rc = -EINVAL;
  590. goto parse_DFS_referrals_exit;
  591. }
  592. /* get the upper boundary of the resp buffer */
  593. data_end = (char *)rsp + rsp_size;
  594. cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
  595. *num_of_nodes, le32_to_cpu(rsp->DFSFlags));
  596. *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
  597. GFP_KERNEL);
  598. if (*target_nodes == NULL) {
  599. rc = -ENOMEM;
  600. goto parse_DFS_referrals_exit;
  601. }
  602. /* collect necessary data from referrals */
  603. for (i = 0; i < *num_of_nodes; i++) {
  604. char *temp;
  605. int max_len;
  606. struct dfs_info3_param *node = (*target_nodes)+i;
  607. node->flags = le32_to_cpu(rsp->DFSFlags);
  608. if (is_unicode) {
  609. __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
  610. GFP_KERNEL);
  611. if (tmp == NULL) {
  612. rc = -ENOMEM;
  613. goto parse_DFS_referrals_exit;
  614. }
  615. cifsConvertToUTF16((__le16 *) tmp, searchName,
  616. PATH_MAX, nls_codepage, remap);
  617. node->path_consumed = cifs_utf16_bytes(tmp,
  618. le16_to_cpu(rsp->PathConsumed),
  619. nls_codepage);
  620. kfree(tmp);
  621. } else
  622. node->path_consumed = le16_to_cpu(rsp->PathConsumed);
  623. node->server_type = le16_to_cpu(ref->ServerType);
  624. node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
  625. /* copy DfsPath */
  626. temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
  627. max_len = data_end - temp;
  628. node->path_name = cifs_strndup_from_utf16(temp, max_len,
  629. is_unicode, nls_codepage);
  630. if (!node->path_name) {
  631. rc = -ENOMEM;
  632. goto parse_DFS_referrals_exit;
  633. }
  634. /* copy link target UNC */
  635. temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
  636. max_len = data_end - temp;
  637. node->node_name = cifs_strndup_from_utf16(temp, max_len,
  638. is_unicode, nls_codepage);
  639. if (!node->node_name) {
  640. rc = -ENOMEM;
  641. goto parse_DFS_referrals_exit;
  642. }
  643. ref++;
  644. }
  645. parse_DFS_referrals_exit:
  646. if (rc) {
  647. free_dfs_info_array(*target_nodes, *num_of_nodes);
  648. *target_nodes = NULL;
  649. *num_of_nodes = 0;
  650. }
  651. return rc;
  652. }
  653. struct cifs_aio_ctx *
  654. cifs_aio_ctx_alloc(void)
  655. {
  656. struct cifs_aio_ctx *ctx;
  657. ctx = kzalloc(sizeof(struct cifs_aio_ctx), GFP_KERNEL);
  658. if (!ctx)
  659. return NULL;
  660. INIT_LIST_HEAD(&ctx->list);
  661. mutex_init(&ctx->aio_mutex);
  662. init_completion(&ctx->done);
  663. kref_init(&ctx->refcount);
  664. return ctx;
  665. }
  666. void
  667. cifs_aio_ctx_release(struct kref *refcount)
  668. {
  669. struct cifs_aio_ctx *ctx = container_of(refcount,
  670. struct cifs_aio_ctx, refcount);
  671. cifsFileInfo_put(ctx->cfile);
  672. kvfree(ctx->bv);
  673. kfree(ctx);
  674. }
  675. #define CIFS_AIO_KMALLOC_LIMIT (1024 * 1024)
  676. int
  677. setup_aio_ctx_iter(struct cifs_aio_ctx *ctx, struct iov_iter *iter, int rw)
  678. {
  679. ssize_t rc;
  680. unsigned int cur_npages;
  681. unsigned int npages = 0;
  682. unsigned int i;
  683. size_t len;
  684. size_t count = iov_iter_count(iter);
  685. unsigned int saved_len;
  686. size_t start;
  687. unsigned int max_pages = iov_iter_npages(iter, INT_MAX);
  688. struct page **pages = NULL;
  689. struct bio_vec *bv = NULL;
  690. if (iter->type & ITER_KVEC) {
  691. memcpy(&ctx->iter, iter, sizeof(struct iov_iter));
  692. ctx->len = count;
  693. iov_iter_advance(iter, count);
  694. return 0;
  695. }
  696. if (max_pages * sizeof(struct bio_vec) <= CIFS_AIO_KMALLOC_LIMIT)
  697. bv = kmalloc_array(max_pages, sizeof(struct bio_vec),
  698. GFP_KERNEL);
  699. if (!bv) {
  700. bv = vmalloc(array_size(max_pages, sizeof(struct bio_vec)));
  701. if (!bv)
  702. return -ENOMEM;
  703. }
  704. if (max_pages * sizeof(struct page *) <= CIFS_AIO_KMALLOC_LIMIT)
  705. pages = kmalloc_array(max_pages, sizeof(struct page *),
  706. GFP_KERNEL);
  707. if (!pages) {
  708. pages = vmalloc(array_size(max_pages, sizeof(struct page *)));
  709. if (!pages) {
  710. kvfree(bv);
  711. return -ENOMEM;
  712. }
  713. }
  714. saved_len = count;
  715. while (count && npages < max_pages) {
  716. rc = iov_iter_get_pages(iter, pages, count, max_pages, &start);
  717. if (rc < 0) {
  718. cifs_dbg(VFS, "couldn't get user pages (rc=%zd)\n", rc);
  719. break;
  720. }
  721. if (rc > count) {
  722. cifs_dbg(VFS, "get pages rc=%zd more than %zu\n", rc,
  723. count);
  724. break;
  725. }
  726. iov_iter_advance(iter, rc);
  727. count -= rc;
  728. rc += start;
  729. cur_npages = DIV_ROUND_UP(rc, PAGE_SIZE);
  730. if (npages + cur_npages > max_pages) {
  731. cifs_dbg(VFS, "out of vec array capacity (%u vs %u)\n",
  732. npages + cur_npages, max_pages);
  733. break;
  734. }
  735. for (i = 0; i < cur_npages; i++) {
  736. len = rc > PAGE_SIZE ? PAGE_SIZE : rc;
  737. bv[npages + i].bv_page = pages[i];
  738. bv[npages + i].bv_offset = start;
  739. bv[npages + i].bv_len = len - start;
  740. rc -= len;
  741. start = 0;
  742. }
  743. npages += cur_npages;
  744. }
  745. kvfree(pages);
  746. ctx->bv = bv;
  747. ctx->len = saved_len - count;
  748. ctx->npages = npages;
  749. iov_iter_bvec(&ctx->iter, ITER_BVEC | rw, ctx->bv, npages, ctx->len);
  750. return 0;
  751. }
  752. /**
  753. * cifs_alloc_hash - allocate hash and hash context together
  754. *
  755. * The caller has to make sure @sdesc is initialized to either NULL or
  756. * a valid context. Both can be freed via cifs_free_hash().
  757. */
  758. int
  759. cifs_alloc_hash(const char *name,
  760. struct crypto_shash **shash, struct sdesc **sdesc)
  761. {
  762. int rc = 0;
  763. size_t size;
  764. if (*sdesc != NULL)
  765. return 0;
  766. *shash = crypto_alloc_shash(name, 0, 0);
  767. if (IS_ERR(*shash)) {
  768. cifs_dbg(VFS, "could not allocate crypto %s\n", name);
  769. rc = PTR_ERR(*shash);
  770. *shash = NULL;
  771. *sdesc = NULL;
  772. return rc;
  773. }
  774. size = sizeof(struct shash_desc) + crypto_shash_descsize(*shash);
  775. *sdesc = kmalloc(size, GFP_KERNEL);
  776. if (*sdesc == NULL) {
  777. cifs_dbg(VFS, "no memory left to allocate crypto %s\n", name);
  778. crypto_free_shash(*shash);
  779. *shash = NULL;
  780. return -ENOMEM;
  781. }
  782. (*sdesc)->shash.tfm = *shash;
  783. (*sdesc)->shash.flags = 0x0;
  784. return 0;
  785. }
  786. /**
  787. * cifs_free_hash - free hash and hash context together
  788. *
  789. * Freeing a NULL hash or context is safe.
  790. */
  791. void
  792. cifs_free_hash(struct crypto_shash **shash, struct sdesc **sdesc)
  793. {
  794. kfree(*sdesc);
  795. *sdesc = NULL;
  796. if (*shash)
  797. crypto_free_shash(*shash);
  798. *shash = NULL;
  799. }
  800. /**
  801. * rqst_page_get_length - obtain the length and offset for a page in smb_rqst
  802. * Input: rqst - a smb_rqst, page - a page index for rqst
  803. * Output: *len - the length for this page, *offset - the offset for this page
  804. */
  805. void rqst_page_get_length(struct smb_rqst *rqst, unsigned int page,
  806. unsigned int *len, unsigned int *offset)
  807. {
  808. *len = rqst->rq_pagesz;
  809. *offset = (page == 0) ? rqst->rq_offset : 0;
  810. if (rqst->rq_npages == 1 || page == rqst->rq_npages-1)
  811. *len = rqst->rq_tailsz;
  812. else if (page == 0)
  813. *len = rqst->rq_pagesz - rqst->rq_offset;
  814. }