misc.c 25 KB

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