misc.c 25 KB

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