misc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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. #include "cifsfs.h"
  34. #ifdef CONFIG_CIFS_DFS_UPCALL
  35. #include "dns_resolve.h"
  36. #endif
  37. extern mempool_t *cifs_sm_req_poolp;
  38. extern mempool_t *cifs_req_poolp;
  39. /* The xid serves as a useful identifier for each incoming vfs request,
  40. in a similar way to the mid which is useful to track each sent smb,
  41. and CurrentXid can also provide a running counter (although it
  42. will eventually wrap past zero) of the total vfs operations handled
  43. since the cifs fs was mounted */
  44. unsigned int
  45. _get_xid(void)
  46. {
  47. unsigned int xid;
  48. spin_lock(&GlobalMid_Lock);
  49. GlobalTotalActiveXid++;
  50. /* keep high water mark for number of simultaneous ops in filesystem */
  51. if (GlobalTotalActiveXid > GlobalMaxActiveXid)
  52. GlobalMaxActiveXid = GlobalTotalActiveXid;
  53. if (GlobalTotalActiveXid > 65000)
  54. cifs_dbg(FYI, "warning: more than 65000 requests active\n");
  55. xid = GlobalCurrentXid++;
  56. spin_unlock(&GlobalMid_Lock);
  57. return xid;
  58. }
  59. void
  60. _free_xid(unsigned int xid)
  61. {
  62. spin_lock(&GlobalMid_Lock);
  63. /* if (GlobalTotalActiveXid == 0)
  64. BUG(); */
  65. GlobalTotalActiveXid--;
  66. spin_unlock(&GlobalMid_Lock);
  67. }
  68. struct cifs_ses *
  69. sesInfoAlloc(void)
  70. {
  71. struct cifs_ses *ret_buf;
  72. ret_buf = kzalloc(sizeof(struct cifs_ses), GFP_KERNEL);
  73. if (ret_buf) {
  74. atomic_inc(&sesInfoAllocCount);
  75. ret_buf->status = CifsNew;
  76. ++ret_buf->ses_count;
  77. INIT_LIST_HEAD(&ret_buf->smb_ses_list);
  78. INIT_LIST_HEAD(&ret_buf->tcon_list);
  79. mutex_init(&ret_buf->session_mutex);
  80. spin_lock_init(&ret_buf->iface_lock);
  81. }
  82. return ret_buf;
  83. }
  84. void
  85. sesInfoFree(struct cifs_ses *buf_to_free)
  86. {
  87. if (buf_to_free == NULL) {
  88. cifs_dbg(FYI, "Null buffer passed to sesInfoFree\n");
  89. return;
  90. }
  91. atomic_dec(&sesInfoAllocCount);
  92. kfree(buf_to_free->serverOS);
  93. kfree(buf_to_free->serverDomain);
  94. kfree(buf_to_free->serverNOS);
  95. kfree_sensitive(buf_to_free->password);
  96. kfree(buf_to_free->user_name);
  97. kfree(buf_to_free->domainName);
  98. kfree_sensitive(buf_to_free->auth_key.response);
  99. kfree(buf_to_free->iface_list);
  100. kfree_sensitive(buf_to_free);
  101. }
  102. struct cifs_tcon *
  103. tconInfoAlloc(void)
  104. {
  105. struct cifs_tcon *ret_buf;
  106. ret_buf = kzalloc(sizeof(*ret_buf), GFP_KERNEL);
  107. if (!ret_buf)
  108. return NULL;
  109. ret_buf->crfid.fid = kzalloc(sizeof(*ret_buf->crfid.fid), GFP_KERNEL);
  110. if (!ret_buf->crfid.fid) {
  111. kfree(ret_buf);
  112. return NULL;
  113. }
  114. atomic_inc(&tconInfoAllocCount);
  115. ret_buf->tidStatus = CifsNew;
  116. ++ret_buf->tc_count;
  117. INIT_LIST_HEAD(&ret_buf->openFileList);
  118. INIT_LIST_HEAD(&ret_buf->tcon_list);
  119. spin_lock_init(&ret_buf->open_file_lock);
  120. mutex_init(&ret_buf->crfid.fid_mutex);
  121. spin_lock_init(&ret_buf->stat_lock);
  122. atomic_set(&ret_buf->num_local_opens, 0);
  123. atomic_set(&ret_buf->num_remote_opens, 0);
  124. return ret_buf;
  125. }
  126. void
  127. tconInfoFree(struct cifs_tcon *buf_to_free)
  128. {
  129. if (buf_to_free == NULL) {
  130. cifs_dbg(FYI, "Null buffer passed to tconInfoFree\n");
  131. return;
  132. }
  133. atomic_dec(&tconInfoAllocCount);
  134. kfree(buf_to_free->nativeFileSystem);
  135. kfree_sensitive(buf_to_free->password);
  136. kfree(buf_to_free->crfid.fid);
  137. #ifdef CONFIG_CIFS_DFS_UPCALL
  138. kfree(buf_to_free->dfs_path);
  139. #endif
  140. kfree(buf_to_free);
  141. }
  142. struct smb_hdr *
  143. cifs_buf_get(void)
  144. {
  145. struct smb_hdr *ret_buf = NULL;
  146. /*
  147. * SMB2 header is bigger than CIFS one - no problems to clean some
  148. * more bytes for CIFS.
  149. */
  150. size_t buf_size = sizeof(struct smb2_sync_hdr);
  151. /*
  152. * We could use negotiated size instead of max_msgsize -
  153. * but it may be more efficient to always alloc same size
  154. * albeit slightly larger than necessary and maxbuffersize
  155. * defaults to this and can not be bigger.
  156. */
  157. ret_buf = mempool_alloc(cifs_req_poolp, GFP_NOFS);
  158. /* clear the first few header bytes */
  159. /* for most paths, more is cleared in header_assemble */
  160. memset(ret_buf, 0, buf_size + 3);
  161. atomic_inc(&bufAllocCount);
  162. #ifdef CONFIG_CIFS_STATS2
  163. atomic_inc(&totBufAllocCount);
  164. #endif /* CONFIG_CIFS_STATS2 */
  165. return ret_buf;
  166. }
  167. void
  168. cifs_buf_release(void *buf_to_free)
  169. {
  170. if (buf_to_free == NULL) {
  171. /* cifs_dbg(FYI, "Null buffer passed to cifs_buf_release\n");*/
  172. return;
  173. }
  174. mempool_free(buf_to_free, cifs_req_poolp);
  175. atomic_dec(&bufAllocCount);
  176. return;
  177. }
  178. struct smb_hdr *
  179. cifs_small_buf_get(void)
  180. {
  181. struct smb_hdr *ret_buf = NULL;
  182. /* We could use negotiated size instead of max_msgsize -
  183. but it may be more efficient to always alloc same size
  184. albeit slightly larger than necessary and maxbuffersize
  185. defaults to this and can not be bigger */
  186. ret_buf = mempool_alloc(cifs_sm_req_poolp, GFP_NOFS);
  187. /* No need to clear memory here, cleared in header assemble */
  188. /* memset(ret_buf, 0, sizeof(struct smb_hdr) + 27);*/
  189. atomic_inc(&smBufAllocCount);
  190. #ifdef CONFIG_CIFS_STATS2
  191. atomic_inc(&totSmBufAllocCount);
  192. #endif /* CONFIG_CIFS_STATS2 */
  193. return ret_buf;
  194. }
  195. void
  196. cifs_small_buf_release(void *buf_to_free)
  197. {
  198. if (buf_to_free == NULL) {
  199. cifs_dbg(FYI, "Null buffer passed to cifs_small_buf_release\n");
  200. return;
  201. }
  202. mempool_free(buf_to_free, cifs_sm_req_poolp);
  203. atomic_dec(&smBufAllocCount);
  204. return;
  205. }
  206. void
  207. free_rsp_buf(int resp_buftype, void *rsp)
  208. {
  209. if (resp_buftype == CIFS_SMALL_BUFFER)
  210. cifs_small_buf_release(rsp);
  211. else if (resp_buftype == CIFS_LARGE_BUFFER)
  212. cifs_buf_release(rsp);
  213. }
  214. /* NB: MID can not be set if treeCon not passed in, in that
  215. case it is responsbility of caller to set the mid */
  216. void
  217. header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
  218. const struct cifs_tcon *treeCon, int word_count
  219. /* length of fixed section (word count) in two byte units */)
  220. {
  221. char *temp = (char *) buffer;
  222. memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
  223. buffer->smb_buf_length = cpu_to_be32(
  224. (2 * word_count) + sizeof(struct smb_hdr) -
  225. 4 /* RFC 1001 length field does not count */ +
  226. 2 /* for bcc field itself */) ;
  227. buffer->Protocol[0] = 0xFF;
  228. buffer->Protocol[1] = 'S';
  229. buffer->Protocol[2] = 'M';
  230. buffer->Protocol[3] = 'B';
  231. buffer->Command = smb_command;
  232. buffer->Flags = 0x00; /* case sensitive */
  233. buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
  234. buffer->Pid = cpu_to_le16((__u16)current->tgid);
  235. buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
  236. if (treeCon) {
  237. buffer->Tid = treeCon->tid;
  238. if (treeCon->ses) {
  239. if (treeCon->ses->capabilities & CAP_UNICODE)
  240. buffer->Flags2 |= SMBFLG2_UNICODE;
  241. if (treeCon->ses->capabilities & CAP_STATUS32)
  242. buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  243. /* Uid is not converted */
  244. buffer->Uid = treeCon->ses->Suid;
  245. buffer->Mid = get_next_mid(treeCon->ses->server);
  246. }
  247. if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
  248. buffer->Flags2 |= SMBFLG2_DFS;
  249. if (treeCon->nocase)
  250. buffer->Flags |= SMBFLG_CASELESS;
  251. if ((treeCon->ses) && (treeCon->ses->server))
  252. if (treeCon->ses->server->sign)
  253. buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  254. }
  255. /* endian conversion of flags is now done just before sending */
  256. buffer->WordCount = (char) word_count;
  257. return;
  258. }
  259. static int
  260. check_smb_hdr(struct smb_hdr *smb)
  261. {
  262. /* does it have the right SMB "signature" ? */
  263. if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff)) {
  264. cifs_dbg(VFS, "Bad protocol string signature header 0x%x\n",
  265. *(unsigned int *)smb->Protocol);
  266. return 1;
  267. }
  268. /* if it's a response then accept */
  269. if (smb->Flags & SMBFLG_RESPONSE)
  270. return 0;
  271. /* only one valid case where server sends us request */
  272. if (smb->Command == SMB_COM_LOCKING_ANDX)
  273. return 0;
  274. cifs_dbg(VFS, "Server sent request, not response. mid=%u\n",
  275. get_mid(smb));
  276. return 1;
  277. }
  278. int
  279. checkSMB(char *buf, unsigned int total_read, struct TCP_Server_Info *server)
  280. {
  281. struct smb_hdr *smb = (struct smb_hdr *)buf;
  282. __u32 rfclen = be32_to_cpu(smb->smb_buf_length);
  283. __u32 clc_len; /* calculated length */
  284. cifs_dbg(FYI, "checkSMB Length: 0x%x, smb_buf_length: 0x%x\n",
  285. total_read, rfclen);
  286. /* is this frame too small to even get to a BCC? */
  287. if (total_read < 2 + sizeof(struct smb_hdr)) {
  288. if ((total_read >= sizeof(struct smb_hdr) - 1)
  289. && (smb->Status.CifsError != 0)) {
  290. /* it's an error return */
  291. smb->WordCount = 0;
  292. /* some error cases do not return wct and bcc */
  293. return 0;
  294. } else if ((total_read == sizeof(struct smb_hdr) + 1) &&
  295. (smb->WordCount == 0)) {
  296. char *tmp = (char *)smb;
  297. /* Need to work around a bug in two servers here */
  298. /* First, check if the part of bcc they sent was zero */
  299. if (tmp[sizeof(struct smb_hdr)] == 0) {
  300. /* some servers return only half of bcc
  301. * on simple responses (wct, bcc both zero)
  302. * in particular have seen this on
  303. * ulogoffX and FindClose. This leaves
  304. * one byte of bcc potentially unitialized
  305. */
  306. /* zero rest of bcc */
  307. tmp[sizeof(struct smb_hdr)+1] = 0;
  308. return 0;
  309. }
  310. cifs_dbg(VFS, "rcvd invalid byte count (bcc)\n");
  311. } else {
  312. cifs_dbg(VFS, "Length less than smb header size\n");
  313. }
  314. return -EIO;
  315. }
  316. /* otherwise, there is enough to get to the BCC */
  317. if (check_smb_hdr(smb))
  318. return -EIO;
  319. clc_len = smbCalcSize(smb, server);
  320. if (4 + rfclen != total_read) {
  321. cifs_dbg(VFS, "Length read does not match RFC1001 length %d\n",
  322. rfclen);
  323. return -EIO;
  324. }
  325. if (4 + rfclen != clc_len) {
  326. __u16 mid = get_mid(smb);
  327. /* check if bcc wrapped around for large read responses */
  328. if ((rfclen > 64 * 1024) && (rfclen > clc_len)) {
  329. /* check if lengths match mod 64K */
  330. if (((4 + rfclen) & 0xFFFF) == (clc_len & 0xFFFF))
  331. return 0; /* bcc wrapped */
  332. }
  333. cifs_dbg(FYI, "Calculated size %u vs length %u mismatch for mid=%u\n",
  334. clc_len, 4 + rfclen, mid);
  335. if (4 + rfclen < clc_len) {
  336. cifs_dbg(VFS, "RFC1001 size %u smaller than SMB for mid=%u\n",
  337. rfclen, mid);
  338. return -EIO;
  339. } else if (rfclen > clc_len + 512) {
  340. /*
  341. * Some servers (Windows XP in particular) send more
  342. * data than the lengths in the SMB packet would
  343. * indicate on certain calls (byte range locks and
  344. * trans2 find first calls in particular). While the
  345. * client can handle such a frame by ignoring the
  346. * trailing data, we choose limit the amount of extra
  347. * data to 512 bytes.
  348. */
  349. cifs_dbg(VFS, "RFC1001 size %u more than 512 bytes larger than SMB for mid=%u\n",
  350. rfclen, mid);
  351. return -EIO;
  352. }
  353. }
  354. return 0;
  355. }
  356. bool
  357. is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
  358. {
  359. struct smb_hdr *buf = (struct smb_hdr *)buffer;
  360. struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
  361. struct list_head *tmp, *tmp1, *tmp2;
  362. struct cifs_ses *ses;
  363. struct cifs_tcon *tcon;
  364. struct cifsInodeInfo *pCifsInode;
  365. struct cifsFileInfo *netfile;
  366. cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
  367. if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
  368. (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
  369. struct smb_com_transaction_change_notify_rsp *pSMBr =
  370. (struct smb_com_transaction_change_notify_rsp *)buf;
  371. struct file_notify_information *pnotify;
  372. __u32 data_offset = 0;
  373. size_t len = srv->total_read - sizeof(pSMBr->hdr.smb_buf_length);
  374. if (get_bcc(buf) > sizeof(struct file_notify_information)) {
  375. data_offset = le32_to_cpu(pSMBr->DataOffset);
  376. if (data_offset >
  377. len - sizeof(struct file_notify_information)) {
  378. cifs_dbg(FYI, "Invalid data_offset %u\n",
  379. data_offset);
  380. return true;
  381. }
  382. pnotify = (struct file_notify_information *)
  383. ((char *)&pSMBr->hdr.Protocol + data_offset);
  384. cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
  385. pnotify->FileName, pnotify->Action);
  386. /* cifs_dump_mem("Rcvd notify Data: ",buf,
  387. sizeof(struct smb_hdr)+60); */
  388. return true;
  389. }
  390. if (pSMBr->hdr.Status.CifsError) {
  391. cifs_dbg(FYI, "notify err 0x%x\n",
  392. pSMBr->hdr.Status.CifsError);
  393. return true;
  394. }
  395. return false;
  396. }
  397. if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
  398. return false;
  399. if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
  400. /* no sense logging error on invalid handle on oplock
  401. break - harmless race between close request and oplock
  402. break response is expected from time to time writing out
  403. large dirty files cached on the client */
  404. if ((NT_STATUS_INVALID_HANDLE) ==
  405. le32_to_cpu(pSMB->hdr.Status.CifsError)) {
  406. cifs_dbg(FYI, "Invalid handle on oplock break\n");
  407. return true;
  408. } else if (ERRbadfid ==
  409. le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
  410. return true;
  411. } else {
  412. return false; /* on valid oplock brk we get "request" */
  413. }
  414. }
  415. if (pSMB->hdr.WordCount != 8)
  416. return false;
  417. cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
  418. pSMB->LockType, pSMB->OplockLevel);
  419. if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
  420. return false;
  421. /* look up tcon based on tid & uid */
  422. spin_lock(&cifs_tcp_ses_lock);
  423. list_for_each(tmp, &srv->smb_ses_list) {
  424. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  425. list_for_each(tmp1, &ses->tcon_list) {
  426. tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
  427. if (tcon->tid != buf->Tid)
  428. continue;
  429. cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
  430. spin_lock(&tcon->open_file_lock);
  431. list_for_each(tmp2, &tcon->openFileList) {
  432. netfile = list_entry(tmp2, struct cifsFileInfo,
  433. tlist);
  434. if (pSMB->Fid != netfile->fid.netfid)
  435. continue;
  436. cifs_dbg(FYI, "file id match, oplock break\n");
  437. pCifsInode = CIFS_I(d_inode(netfile->dentry));
  438. set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
  439. &pCifsInode->flags);
  440. netfile->oplock_epoch = 0;
  441. netfile->oplock_level = pSMB->OplockLevel;
  442. netfile->oplock_break_cancelled = false;
  443. cifs_queue_oplock_break(netfile);
  444. spin_unlock(&tcon->open_file_lock);
  445. spin_unlock(&cifs_tcp_ses_lock);
  446. return true;
  447. }
  448. spin_unlock(&tcon->open_file_lock);
  449. spin_unlock(&cifs_tcp_ses_lock);
  450. cifs_dbg(FYI, "No matching file for oplock break\n");
  451. return true;
  452. }
  453. }
  454. spin_unlock(&cifs_tcp_ses_lock);
  455. cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
  456. return true;
  457. }
  458. void
  459. dump_smb(void *buf, int smb_buf_length)
  460. {
  461. if (traceSMB == 0)
  462. return;
  463. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE, 8, 2, buf,
  464. smb_buf_length, true);
  465. }
  466. void
  467. cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb)
  468. {
  469. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  470. struct cifs_tcon *tcon = NULL;
  471. if (cifs_sb->master_tlink)
  472. tcon = cifs_sb_master_tcon(cifs_sb);
  473. cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
  474. cifs_sb->mnt_cifs_serverino_autodisabled = true;
  475. cifs_dbg(VFS, "Autodisabling the use of server inode numbers on %s\n",
  476. tcon ? tcon->treeName : "new server");
  477. cifs_dbg(VFS, "The server doesn't seem to support them properly or the files might be on different servers (DFS)\n");
  478. cifs_dbg(VFS, "Hardlinks will not be recognized on this mount. Consider mounting with the \"noserverino\" option to silence this message.\n");
  479. }
  480. }
  481. void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
  482. {
  483. oplock &= 0xF;
  484. if (oplock == OPLOCK_EXCLUSIVE) {
  485. cinode->oplock = CIFS_CACHE_WRITE_FLG | CIFS_CACHE_READ_FLG;
  486. cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
  487. &cinode->vfs_inode);
  488. } else if (oplock == OPLOCK_READ) {
  489. cinode->oplock = CIFS_CACHE_READ_FLG;
  490. cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
  491. &cinode->vfs_inode);
  492. } else
  493. cinode->oplock = 0;
  494. }
  495. /*
  496. * We wait for oplock breaks to be processed before we attempt to perform
  497. * writes.
  498. */
  499. int cifs_get_writer(struct cifsInodeInfo *cinode)
  500. {
  501. int rc;
  502. start:
  503. rc = wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK,
  504. TASK_KILLABLE);
  505. if (rc)
  506. return rc;
  507. spin_lock(&cinode->writers_lock);
  508. if (!cinode->writers)
  509. set_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  510. cinode->writers++;
  511. /* Check to see if we have started servicing an oplock break */
  512. if (test_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags)) {
  513. cinode->writers--;
  514. if (cinode->writers == 0) {
  515. clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  516. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
  517. }
  518. spin_unlock(&cinode->writers_lock);
  519. goto start;
  520. }
  521. spin_unlock(&cinode->writers_lock);
  522. return 0;
  523. }
  524. void cifs_put_writer(struct cifsInodeInfo *cinode)
  525. {
  526. spin_lock(&cinode->writers_lock);
  527. cinode->writers--;
  528. if (cinode->writers == 0) {
  529. clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  530. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
  531. }
  532. spin_unlock(&cinode->writers_lock);
  533. }
  534. /**
  535. * cifs_queue_oplock_break - queue the oplock break handler for cfile
  536. *
  537. * This function is called from the demultiplex thread when it
  538. * receives an oplock break for @cfile.
  539. *
  540. * Assumes the tcon->open_file_lock is held.
  541. * Assumes cfile->file_info_lock is NOT held.
  542. */
  543. void cifs_queue_oplock_break(struct cifsFileInfo *cfile)
  544. {
  545. /*
  546. * Bump the handle refcount now while we hold the
  547. * open_file_lock to enforce the validity of it for the oplock
  548. * break handler. The matching put is done at the end of the
  549. * handler.
  550. */
  551. cifsFileInfo_get(cfile);
  552. queue_work(cifsoplockd_wq, &cfile->oplock_break);
  553. }
  554. void cifs_done_oplock_break(struct cifsInodeInfo *cinode)
  555. {
  556. clear_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags);
  557. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK);
  558. }
  559. bool
  560. backup_cred(struct cifs_sb_info *cifs_sb)
  561. {
  562. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID) {
  563. if (uid_eq(cifs_sb->mnt_backupuid, current_fsuid()))
  564. return true;
  565. }
  566. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID) {
  567. if (in_group_p(cifs_sb->mnt_backupgid))
  568. return true;
  569. }
  570. return false;
  571. }
  572. void
  573. cifs_del_pending_open(struct cifs_pending_open *open)
  574. {
  575. spin_lock(&tlink_tcon(open->tlink)->open_file_lock);
  576. list_del(&open->olist);
  577. spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
  578. }
  579. void
  580. cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink,
  581. struct cifs_pending_open *open)
  582. {
  583. memcpy(open->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
  584. open->oplock = CIFS_OPLOCK_NO_CHANGE;
  585. open->tlink = tlink;
  586. fid->pending_open = open;
  587. list_add_tail(&open->olist, &tlink_tcon(tlink)->pending_opens);
  588. }
  589. void
  590. cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink,
  591. struct cifs_pending_open *open)
  592. {
  593. spin_lock(&tlink_tcon(tlink)->open_file_lock);
  594. cifs_add_pending_open_locked(fid, tlink, open);
  595. spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
  596. }
  597. /* parses DFS refferal V3 structure
  598. * caller is responsible for freeing target_nodes
  599. * returns:
  600. * - on success - 0
  601. * - on failure - errno
  602. */
  603. int
  604. parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size,
  605. unsigned int *num_of_nodes,
  606. struct dfs_info3_param **target_nodes,
  607. const struct nls_table *nls_codepage, int remap,
  608. const char *searchName, bool is_unicode)
  609. {
  610. int i, rc = 0;
  611. char *data_end;
  612. struct dfs_referral_level_3 *ref;
  613. *num_of_nodes = le16_to_cpu(rsp->NumberOfReferrals);
  614. if (*num_of_nodes < 1) {
  615. cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
  616. *num_of_nodes);
  617. rc = -EINVAL;
  618. goto parse_DFS_referrals_exit;
  619. }
  620. ref = (struct dfs_referral_level_3 *) &(rsp->referrals);
  621. if (ref->VersionNumber != cpu_to_le16(3)) {
  622. cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
  623. le16_to_cpu(ref->VersionNumber));
  624. rc = -EINVAL;
  625. goto parse_DFS_referrals_exit;
  626. }
  627. /* get the upper boundary of the resp buffer */
  628. data_end = (char *)rsp + rsp_size;
  629. cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
  630. *num_of_nodes, le32_to_cpu(rsp->DFSFlags));
  631. *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
  632. GFP_KERNEL);
  633. if (*target_nodes == NULL) {
  634. rc = -ENOMEM;
  635. goto parse_DFS_referrals_exit;
  636. }
  637. /* collect necessary data from referrals */
  638. for (i = 0; i < *num_of_nodes; i++) {
  639. char *temp;
  640. int max_len;
  641. struct dfs_info3_param *node = (*target_nodes)+i;
  642. node->flags = le32_to_cpu(rsp->DFSFlags);
  643. if (is_unicode) {
  644. __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
  645. GFP_KERNEL);
  646. if (tmp == NULL) {
  647. rc = -ENOMEM;
  648. goto parse_DFS_referrals_exit;
  649. }
  650. cifsConvertToUTF16((__le16 *) tmp, searchName,
  651. PATH_MAX, nls_codepage, remap);
  652. node->path_consumed = cifs_utf16_bytes(tmp,
  653. le16_to_cpu(rsp->PathConsumed),
  654. nls_codepage);
  655. kfree(tmp);
  656. } else
  657. node->path_consumed = le16_to_cpu(rsp->PathConsumed);
  658. node->server_type = le16_to_cpu(ref->ServerType);
  659. node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
  660. /* copy DfsPath */
  661. temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
  662. max_len = data_end - temp;
  663. node->path_name = cifs_strndup_from_utf16(temp, max_len,
  664. is_unicode, nls_codepage);
  665. if (!node->path_name) {
  666. rc = -ENOMEM;
  667. goto parse_DFS_referrals_exit;
  668. }
  669. /* copy link target UNC */
  670. temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
  671. max_len = data_end - temp;
  672. node->node_name = cifs_strndup_from_utf16(temp, max_len,
  673. is_unicode, nls_codepage);
  674. if (!node->node_name) {
  675. rc = -ENOMEM;
  676. goto parse_DFS_referrals_exit;
  677. }
  678. node->ttl = le32_to_cpu(ref->TimeToLive);
  679. ref++;
  680. }
  681. parse_DFS_referrals_exit:
  682. if (rc) {
  683. free_dfs_info_array(*target_nodes, *num_of_nodes);
  684. *target_nodes = NULL;
  685. *num_of_nodes = 0;
  686. }
  687. return rc;
  688. }
  689. struct cifs_aio_ctx *
  690. cifs_aio_ctx_alloc(void)
  691. {
  692. struct cifs_aio_ctx *ctx;
  693. /*
  694. * Must use kzalloc to initialize ctx->bv to NULL and ctx->direct_io
  695. * to false so that we know when we have to unreference pages within
  696. * cifs_aio_ctx_release()
  697. */
  698. ctx = kzalloc(sizeof(struct cifs_aio_ctx), GFP_KERNEL);
  699. if (!ctx)
  700. return NULL;
  701. INIT_LIST_HEAD(&ctx->list);
  702. mutex_init(&ctx->aio_mutex);
  703. init_completion(&ctx->done);
  704. kref_init(&ctx->refcount);
  705. return ctx;
  706. }
  707. void
  708. cifs_aio_ctx_release(struct kref *refcount)
  709. {
  710. struct cifs_aio_ctx *ctx = container_of(refcount,
  711. struct cifs_aio_ctx, refcount);
  712. cifsFileInfo_put(ctx->cfile);
  713. /*
  714. * ctx->bv is only set if setup_aio_ctx_iter() was call successfuly
  715. * which means that iov_iter_get_pages() was a success and thus that
  716. * we have taken reference on pages.
  717. */
  718. if (ctx->bv) {
  719. unsigned i;
  720. for (i = 0; i < ctx->npages; i++) {
  721. if (ctx->should_dirty)
  722. set_page_dirty(ctx->bv[i].bv_page);
  723. put_page(ctx->bv[i].bv_page);
  724. }
  725. kvfree(ctx->bv);
  726. }
  727. kfree(ctx);
  728. }
  729. #define CIFS_AIO_KMALLOC_LIMIT (1024 * 1024)
  730. int
  731. setup_aio_ctx_iter(struct cifs_aio_ctx *ctx, struct iov_iter *iter, int rw)
  732. {
  733. ssize_t rc;
  734. unsigned int cur_npages;
  735. unsigned int npages = 0;
  736. unsigned int i;
  737. size_t len;
  738. size_t count = iov_iter_count(iter);
  739. unsigned int saved_len;
  740. size_t start;
  741. unsigned int max_pages = iov_iter_npages(iter, INT_MAX);
  742. struct page **pages = NULL;
  743. struct bio_vec *bv = NULL;
  744. if (iov_iter_is_kvec(iter)) {
  745. memcpy(&ctx->iter, iter, sizeof(*iter));
  746. ctx->len = count;
  747. iov_iter_advance(iter, count);
  748. return 0;
  749. }
  750. if (array_size(max_pages, sizeof(*bv)) <= CIFS_AIO_KMALLOC_LIMIT)
  751. bv = kmalloc_array(max_pages, sizeof(*bv), GFP_KERNEL);
  752. if (!bv) {
  753. bv = vmalloc(array_size(max_pages, sizeof(*bv)));
  754. if (!bv)
  755. return -ENOMEM;
  756. }
  757. if (array_size(max_pages, sizeof(*pages)) <= CIFS_AIO_KMALLOC_LIMIT)
  758. pages = kmalloc_array(max_pages, sizeof(*pages), GFP_KERNEL);
  759. if (!pages) {
  760. pages = vmalloc(array_size(max_pages, sizeof(*pages)));
  761. if (!pages) {
  762. kvfree(bv);
  763. return -ENOMEM;
  764. }
  765. }
  766. saved_len = count;
  767. while (count && npages < max_pages) {
  768. rc = iov_iter_get_pages(iter, pages, count, max_pages, &start);
  769. if (rc < 0) {
  770. cifs_dbg(VFS, "Couldn't get user pages (rc=%zd)\n", rc);
  771. break;
  772. }
  773. if (rc > count) {
  774. cifs_dbg(VFS, "get pages rc=%zd more than %zu\n", rc,
  775. count);
  776. break;
  777. }
  778. iov_iter_advance(iter, rc);
  779. count -= rc;
  780. rc += start;
  781. cur_npages = DIV_ROUND_UP(rc, PAGE_SIZE);
  782. if (npages + cur_npages > max_pages) {
  783. cifs_dbg(VFS, "out of vec array capacity (%u vs %u)\n",
  784. npages + cur_npages, max_pages);
  785. break;
  786. }
  787. for (i = 0; i < cur_npages; i++) {
  788. len = rc > PAGE_SIZE ? PAGE_SIZE : rc;
  789. bv[npages + i].bv_page = pages[i];
  790. bv[npages + i].bv_offset = start;
  791. bv[npages + i].bv_len = len - start;
  792. rc -= len;
  793. start = 0;
  794. }
  795. npages += cur_npages;
  796. }
  797. kvfree(pages);
  798. ctx->bv = bv;
  799. ctx->len = saved_len - count;
  800. ctx->npages = npages;
  801. iov_iter_bvec(&ctx->iter, rw, ctx->bv, npages, ctx->len);
  802. return 0;
  803. }
  804. /**
  805. * cifs_alloc_hash - allocate hash and hash context together
  806. *
  807. * The caller has to make sure @sdesc is initialized to either NULL or
  808. * a valid context. Both can be freed via cifs_free_hash().
  809. */
  810. int
  811. cifs_alloc_hash(const char *name,
  812. struct crypto_shash **shash, struct sdesc **sdesc)
  813. {
  814. int rc = 0;
  815. size_t size;
  816. if (*sdesc != NULL)
  817. return 0;
  818. *shash = crypto_alloc_shash(name, 0, 0);
  819. if (IS_ERR(*shash)) {
  820. cifs_dbg(VFS, "Could not allocate crypto %s\n", name);
  821. rc = PTR_ERR(*shash);
  822. *shash = NULL;
  823. *sdesc = NULL;
  824. return rc;
  825. }
  826. size = sizeof(struct shash_desc) + crypto_shash_descsize(*shash);
  827. *sdesc = kmalloc(size, GFP_KERNEL);
  828. if (*sdesc == NULL) {
  829. cifs_dbg(VFS, "no memory left to allocate crypto %s\n", name);
  830. crypto_free_shash(*shash);
  831. *shash = NULL;
  832. return -ENOMEM;
  833. }
  834. (*sdesc)->shash.tfm = *shash;
  835. return 0;
  836. }
  837. /**
  838. * cifs_free_hash - free hash and hash context together
  839. *
  840. * Freeing a NULL hash or context is safe.
  841. */
  842. void
  843. cifs_free_hash(struct crypto_shash **shash, struct sdesc **sdesc)
  844. {
  845. kfree(*sdesc);
  846. *sdesc = NULL;
  847. if (*shash)
  848. crypto_free_shash(*shash);
  849. *shash = NULL;
  850. }
  851. /**
  852. * rqst_page_get_length - obtain the length and offset for a page in smb_rqst
  853. * Input: rqst - a smb_rqst, page - a page index for rqst
  854. * Output: *len - the length for this page, *offset - the offset for this page
  855. */
  856. void rqst_page_get_length(struct smb_rqst *rqst, unsigned int page,
  857. unsigned int *len, unsigned int *offset)
  858. {
  859. *len = rqst->rq_pagesz;
  860. *offset = (page == 0) ? rqst->rq_offset : 0;
  861. if (rqst->rq_npages == 1 || page == rqst->rq_npages-1)
  862. *len = rqst->rq_tailsz;
  863. else if (page == 0)
  864. *len = rqst->rq_pagesz - rqst->rq_offset;
  865. }
  866. void extract_unc_hostname(const char *unc, const char **h, size_t *len)
  867. {
  868. const char *end;
  869. /* skip initial slashes */
  870. while (*unc && (*unc == '\\' || *unc == '/'))
  871. unc++;
  872. end = unc;
  873. while (*end && !(*end == '\\' || *end == '/'))
  874. end++;
  875. *h = unc;
  876. *len = end - unc;
  877. }
  878. /**
  879. * copy_path_name - copy src path to dst, possibly truncating
  880. *
  881. * returns number of bytes written (including trailing nul)
  882. */
  883. int copy_path_name(char *dst, const char *src)
  884. {
  885. int name_len;
  886. /*
  887. * PATH_MAX includes nul, so if strlen(src) >= PATH_MAX it
  888. * will truncate and strlen(dst) will be PATH_MAX-1
  889. */
  890. name_len = strscpy(dst, src, PATH_MAX);
  891. if (WARN_ON_ONCE(name_len < 0))
  892. name_len = PATH_MAX-1;
  893. /* we count the trailing nul */
  894. name_len++;
  895. return name_len;
  896. }
  897. struct super_cb_data {
  898. void *data;
  899. struct super_block *sb;
  900. };
  901. static void tcp_super_cb(struct super_block *sb, void *arg)
  902. {
  903. struct super_cb_data *sd = arg;
  904. struct TCP_Server_Info *server = sd->data;
  905. struct cifs_sb_info *cifs_sb;
  906. struct cifs_tcon *tcon;
  907. if (sd->sb)
  908. return;
  909. cifs_sb = CIFS_SB(sb);
  910. tcon = cifs_sb_master_tcon(cifs_sb);
  911. if (tcon->ses->server == server)
  912. sd->sb = sb;
  913. }
  914. static struct super_block *__cifs_get_super(void (*f)(struct super_block *, void *),
  915. void *data)
  916. {
  917. struct super_cb_data sd = {
  918. .data = data,
  919. .sb = NULL,
  920. };
  921. iterate_supers_type(&cifs_fs_type, f, &sd);
  922. if (!sd.sb)
  923. return ERR_PTR(-EINVAL);
  924. /*
  925. * Grab an active reference in order to prevent automounts (DFS links)
  926. * of expiring and then freeing up our cifs superblock pointer while
  927. * we're doing failover.
  928. */
  929. cifs_sb_active(sd.sb);
  930. return sd.sb;
  931. }
  932. static void __cifs_put_super(struct super_block *sb)
  933. {
  934. if (!IS_ERR_OR_NULL(sb))
  935. cifs_sb_deactive(sb);
  936. }
  937. struct super_block *cifs_get_tcp_super(struct TCP_Server_Info *server)
  938. {
  939. return __cifs_get_super(tcp_super_cb, server);
  940. }
  941. void cifs_put_tcp_super(struct super_block *sb)
  942. {
  943. __cifs_put_super(sb);
  944. }
  945. #ifdef CONFIG_CIFS_DFS_UPCALL
  946. int match_target_ip(struct TCP_Server_Info *server,
  947. const char *share, size_t share_len,
  948. bool *result)
  949. {
  950. int rc;
  951. char *target, *tip = NULL;
  952. struct sockaddr tipaddr;
  953. *result = false;
  954. target = kzalloc(share_len + 3, GFP_KERNEL);
  955. if (!target) {
  956. rc = -ENOMEM;
  957. goto out;
  958. }
  959. scnprintf(target, share_len + 3, "\\\\%.*s", (int)share_len, share);
  960. cifs_dbg(FYI, "%s: target name: %s\n", __func__, target + 2);
  961. rc = dns_resolve_server_name_to_ip(target, &tip);
  962. if (rc < 0)
  963. goto out;
  964. cifs_dbg(FYI, "%s: target ip: %s\n", __func__, tip);
  965. if (!cifs_convert_address(&tipaddr, tip, strlen(tip))) {
  966. cifs_dbg(VFS, "%s: failed to convert target ip address\n",
  967. __func__);
  968. rc = -EINVAL;
  969. goto out;
  970. }
  971. *result = cifs_match_ipaddr((struct sockaddr *)&server->dstaddr,
  972. &tipaddr);
  973. cifs_dbg(FYI, "%s: ip addresses match: %u\n", __func__, *result);
  974. rc = 0;
  975. out:
  976. kfree(target);
  977. kfree(tip);
  978. return rc;
  979. }
  980. static void tcon_super_cb(struct super_block *sb, void *arg)
  981. {
  982. struct super_cb_data *sd = arg;
  983. struct cifs_tcon *tcon = sd->data;
  984. struct cifs_sb_info *cifs_sb;
  985. if (sd->sb)
  986. return;
  987. cifs_sb = CIFS_SB(sb);
  988. if (tcon->dfs_path && cifs_sb->origin_fullpath &&
  989. !strcasecmp(tcon->dfs_path, cifs_sb->origin_fullpath))
  990. sd->sb = sb;
  991. }
  992. static inline struct super_block *cifs_get_tcon_super(struct cifs_tcon *tcon)
  993. {
  994. return __cifs_get_super(tcon_super_cb, tcon);
  995. }
  996. static inline void cifs_put_tcon_super(struct super_block *sb)
  997. {
  998. __cifs_put_super(sb);
  999. }
  1000. #else
  1001. static inline struct super_block *cifs_get_tcon_super(struct cifs_tcon *tcon)
  1002. {
  1003. return ERR_PTR(-EOPNOTSUPP);
  1004. }
  1005. static inline void cifs_put_tcon_super(struct super_block *sb)
  1006. {
  1007. }
  1008. #endif
  1009. int update_super_prepath(struct cifs_tcon *tcon, char *prefix)
  1010. {
  1011. struct super_block *sb;
  1012. struct cifs_sb_info *cifs_sb;
  1013. int rc = 0;
  1014. sb = cifs_get_tcon_super(tcon);
  1015. if (IS_ERR(sb))
  1016. return PTR_ERR(sb);
  1017. cifs_sb = CIFS_SB(sb);
  1018. kfree(cifs_sb->prepath);
  1019. if (prefix && *prefix) {
  1020. cifs_sb->prepath = kstrndup(prefix, strlen(prefix), GFP_ATOMIC);
  1021. if (!cifs_sb->prepath) {
  1022. rc = -ENOMEM;
  1023. goto out;
  1024. }
  1025. convert_delimiter(cifs_sb->prepath, CIFS_DIR_SEP(cifs_sb));
  1026. } else
  1027. cifs_sb->prepath = NULL;
  1028. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
  1029. out:
  1030. cifs_put_tcon_super(sb);
  1031. return rc;
  1032. }