misc.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2008
  5. * Author(s): Steve French ([email protected])
  6. *
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/ctype.h>
  10. #include <linux/mempool.h>
  11. #include <linux/vmalloc.h>
  12. #include "cifspdu.h"
  13. #include "cifsglob.h"
  14. #include "cifsproto.h"
  15. #include "cifs_debug.h"
  16. #include "smberr.h"
  17. #include "nterr.h"
  18. #include "cifs_unicode.h"
  19. #include "smb2pdu.h"
  20. #include "cifsfs.h"
  21. #ifdef CONFIG_CIFS_DFS_UPCALL
  22. #include "dns_resolve.h"
  23. #include "dfs_cache.h"
  24. #endif
  25. #include "fs_context.h"
  26. #include "cached_dir.h"
  27. extern mempool_t *cifs_sm_req_poolp;
  28. extern mempool_t *cifs_req_poolp;
  29. /* The xid serves as a useful identifier for each incoming vfs request,
  30. in a similar way to the mid which is useful to track each sent smb,
  31. and CurrentXid can also provide a running counter (although it
  32. will eventually wrap past zero) of the total vfs operations handled
  33. since the cifs fs was mounted */
  34. unsigned int
  35. _get_xid(void)
  36. {
  37. unsigned int xid;
  38. spin_lock(&GlobalMid_Lock);
  39. GlobalTotalActiveXid++;
  40. /* keep high water mark for number of simultaneous ops in filesystem */
  41. if (GlobalTotalActiveXid > GlobalMaxActiveXid)
  42. GlobalMaxActiveXid = GlobalTotalActiveXid;
  43. if (GlobalTotalActiveXid > 65000)
  44. cifs_dbg(FYI, "warning: more than 65000 requests active\n");
  45. xid = GlobalCurrentXid++;
  46. spin_unlock(&GlobalMid_Lock);
  47. return xid;
  48. }
  49. void
  50. _free_xid(unsigned int xid)
  51. {
  52. spin_lock(&GlobalMid_Lock);
  53. /* if (GlobalTotalActiveXid == 0)
  54. BUG(); */
  55. GlobalTotalActiveXid--;
  56. spin_unlock(&GlobalMid_Lock);
  57. }
  58. struct cifs_ses *
  59. sesInfoAlloc(void)
  60. {
  61. struct cifs_ses *ret_buf;
  62. ret_buf = kzalloc(sizeof(struct cifs_ses), GFP_KERNEL);
  63. if (ret_buf) {
  64. atomic_inc(&sesInfoAllocCount);
  65. spin_lock_init(&ret_buf->ses_lock);
  66. ret_buf->ses_status = SES_NEW;
  67. ++ret_buf->ses_count;
  68. INIT_LIST_HEAD(&ret_buf->smb_ses_list);
  69. INIT_LIST_HEAD(&ret_buf->tcon_list);
  70. mutex_init(&ret_buf->session_mutex);
  71. spin_lock_init(&ret_buf->iface_lock);
  72. INIT_LIST_HEAD(&ret_buf->iface_list);
  73. spin_lock_init(&ret_buf->chan_lock);
  74. }
  75. return ret_buf;
  76. }
  77. void
  78. sesInfoFree(struct cifs_ses *buf_to_free)
  79. {
  80. struct cifs_server_iface *iface = NULL, *niface = NULL;
  81. if (buf_to_free == NULL) {
  82. cifs_dbg(FYI, "Null buffer passed to sesInfoFree\n");
  83. return;
  84. }
  85. atomic_dec(&sesInfoAllocCount);
  86. kfree(buf_to_free->serverOS);
  87. kfree(buf_to_free->serverDomain);
  88. kfree(buf_to_free->serverNOS);
  89. kfree_sensitive(buf_to_free->password);
  90. kfree(buf_to_free->user_name);
  91. kfree(buf_to_free->domainName);
  92. kfree_sensitive(buf_to_free->auth_key.response);
  93. spin_lock(&buf_to_free->iface_lock);
  94. list_for_each_entry_safe(iface, niface, &buf_to_free->iface_list,
  95. iface_head)
  96. kref_put(&iface->refcount, release_iface);
  97. spin_unlock(&buf_to_free->iface_lock);
  98. kfree_sensitive(buf_to_free);
  99. }
  100. struct cifs_tcon *
  101. tconInfoAlloc(void)
  102. {
  103. struct cifs_tcon *ret_buf;
  104. ret_buf = kzalloc(sizeof(*ret_buf), GFP_KERNEL);
  105. if (!ret_buf)
  106. return NULL;
  107. ret_buf->cfids = init_cached_dirs();
  108. if (!ret_buf->cfids) {
  109. kfree(ret_buf);
  110. return NULL;
  111. }
  112. atomic_inc(&tconInfoAllocCount);
  113. ret_buf->status = TID_NEW;
  114. ++ret_buf->tc_count;
  115. spin_lock_init(&ret_buf->tc_lock);
  116. INIT_LIST_HEAD(&ret_buf->openFileList);
  117. INIT_LIST_HEAD(&ret_buf->tcon_list);
  118. spin_lock_init(&ret_buf->open_file_lock);
  119. spin_lock_init(&ret_buf->stat_lock);
  120. atomic_set(&ret_buf->num_local_opens, 0);
  121. atomic_set(&ret_buf->num_remote_opens, 0);
  122. return ret_buf;
  123. }
  124. void
  125. tconInfoFree(struct cifs_tcon *tcon)
  126. {
  127. if (tcon == NULL) {
  128. cifs_dbg(FYI, "Null buffer passed to tconInfoFree\n");
  129. return;
  130. }
  131. free_cached_dirs(tcon->cfids);
  132. atomic_dec(&tconInfoAllocCount);
  133. kfree(tcon->nativeFileSystem);
  134. kfree_sensitive(tcon->password);
  135. kfree(tcon);
  136. }
  137. struct smb_hdr *
  138. cifs_buf_get(void)
  139. {
  140. struct smb_hdr *ret_buf = NULL;
  141. /*
  142. * SMB2 header is bigger than CIFS one - no problems to clean some
  143. * more bytes for CIFS.
  144. */
  145. size_t buf_size = sizeof(struct smb2_hdr);
  146. /*
  147. * We could use negotiated size instead of max_msgsize -
  148. * but it may be more efficient to always alloc same size
  149. * albeit slightly larger than necessary and maxbuffersize
  150. * defaults to this and can not be bigger.
  151. */
  152. ret_buf = mempool_alloc(cifs_req_poolp, GFP_NOFS);
  153. /* clear the first few header bytes */
  154. /* for most paths, more is cleared in header_assemble */
  155. memset(ret_buf, 0, buf_size + 3);
  156. atomic_inc(&buf_alloc_count);
  157. #ifdef CONFIG_CIFS_STATS2
  158. atomic_inc(&total_buf_alloc_count);
  159. #endif /* CONFIG_CIFS_STATS2 */
  160. return ret_buf;
  161. }
  162. void
  163. cifs_buf_release(void *buf_to_free)
  164. {
  165. if (buf_to_free == NULL) {
  166. /* cifs_dbg(FYI, "Null buffer passed to cifs_buf_release\n");*/
  167. return;
  168. }
  169. mempool_free(buf_to_free, cifs_req_poolp);
  170. atomic_dec(&buf_alloc_count);
  171. return;
  172. }
  173. struct smb_hdr *
  174. cifs_small_buf_get(void)
  175. {
  176. struct smb_hdr *ret_buf = NULL;
  177. /* We could use negotiated size instead of max_msgsize -
  178. but it may be more efficient to always alloc same size
  179. albeit slightly larger than necessary and maxbuffersize
  180. defaults to this and can not be bigger */
  181. ret_buf = mempool_alloc(cifs_sm_req_poolp, GFP_NOFS);
  182. /* No need to clear memory here, cleared in header assemble */
  183. /* memset(ret_buf, 0, sizeof(struct smb_hdr) + 27);*/
  184. atomic_inc(&small_buf_alloc_count);
  185. #ifdef CONFIG_CIFS_STATS2
  186. atomic_inc(&total_small_buf_alloc_count);
  187. #endif /* CONFIG_CIFS_STATS2 */
  188. return ret_buf;
  189. }
  190. void
  191. cifs_small_buf_release(void *buf_to_free)
  192. {
  193. if (buf_to_free == NULL) {
  194. cifs_dbg(FYI, "Null buffer passed to cifs_small_buf_release\n");
  195. return;
  196. }
  197. mempool_free(buf_to_free, cifs_sm_req_poolp);
  198. atomic_dec(&small_buf_alloc_count);
  199. return;
  200. }
  201. void
  202. free_rsp_buf(int resp_buftype, void *rsp)
  203. {
  204. if (resp_buftype == CIFS_SMALL_BUFFER)
  205. cifs_small_buf_release(rsp);
  206. else if (resp_buftype == CIFS_LARGE_BUFFER)
  207. cifs_buf_release(rsp);
  208. }
  209. /* NB: MID can not be set if treeCon not passed in, in that
  210. case it is responsbility of caller to set the mid */
  211. void
  212. header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
  213. const struct cifs_tcon *treeCon, int word_count
  214. /* length of fixed section (word count) in two byte units */)
  215. {
  216. char *temp = (char *) buffer;
  217. memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
  218. buffer->smb_buf_length = cpu_to_be32(
  219. (2 * word_count) + sizeof(struct smb_hdr) -
  220. 4 /* RFC 1001 length field does not count */ +
  221. 2 /* for bcc field itself */) ;
  222. buffer->Protocol[0] = 0xFF;
  223. buffer->Protocol[1] = 'S';
  224. buffer->Protocol[2] = 'M';
  225. buffer->Protocol[3] = 'B';
  226. buffer->Command = smb_command;
  227. buffer->Flags = 0x00; /* case sensitive */
  228. buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
  229. buffer->Pid = cpu_to_le16((__u16)current->tgid);
  230. buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
  231. if (treeCon) {
  232. buffer->Tid = treeCon->tid;
  233. if (treeCon->ses) {
  234. if (treeCon->ses->capabilities & CAP_UNICODE)
  235. buffer->Flags2 |= SMBFLG2_UNICODE;
  236. if (treeCon->ses->capabilities & CAP_STATUS32)
  237. buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  238. /* Uid is not converted */
  239. buffer->Uid = treeCon->ses->Suid;
  240. if (treeCon->ses->server)
  241. buffer->Mid = get_next_mid(treeCon->ses->server);
  242. }
  243. if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
  244. buffer->Flags2 |= SMBFLG2_DFS;
  245. if (treeCon->nocase)
  246. buffer->Flags |= SMBFLG_CASELESS;
  247. if ((treeCon->ses) && (treeCon->ses->server))
  248. if (treeCon->ses->server->sign)
  249. buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  250. }
  251. /* endian conversion of flags is now done just before sending */
  252. buffer->WordCount = (char) word_count;
  253. return;
  254. }
  255. static int
  256. check_smb_hdr(struct smb_hdr *smb)
  257. {
  258. /* does it have the right SMB "signature" ? */
  259. if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff)) {
  260. cifs_dbg(VFS, "Bad protocol string signature header 0x%x\n",
  261. *(unsigned int *)smb->Protocol);
  262. return 1;
  263. }
  264. /* if it's a response then accept */
  265. if (smb->Flags & SMBFLG_RESPONSE)
  266. return 0;
  267. /* only one valid case where server sends us request */
  268. if (smb->Command == SMB_COM_LOCKING_ANDX)
  269. return 0;
  270. cifs_dbg(VFS, "Server sent request, not response. mid=%u\n",
  271. get_mid(smb));
  272. return 1;
  273. }
  274. int
  275. checkSMB(char *buf, unsigned int total_read, struct TCP_Server_Info *server)
  276. {
  277. struct smb_hdr *smb = (struct smb_hdr *)buf;
  278. __u32 rfclen = be32_to_cpu(smb->smb_buf_length);
  279. __u32 clc_len; /* calculated length */
  280. cifs_dbg(FYI, "checkSMB Length: 0x%x, smb_buf_length: 0x%x\n",
  281. total_read, rfclen);
  282. /* is this frame too small to even get to a BCC? */
  283. if (total_read < 2 + sizeof(struct smb_hdr)) {
  284. if ((total_read >= sizeof(struct smb_hdr) - 1)
  285. && (smb->Status.CifsError != 0)) {
  286. /* it's an error return */
  287. smb->WordCount = 0;
  288. /* some error cases do not return wct and bcc */
  289. return 0;
  290. } else if ((total_read == sizeof(struct smb_hdr) + 1) &&
  291. (smb->WordCount == 0)) {
  292. char *tmp = (char *)smb;
  293. /* Need to work around a bug in two servers here */
  294. /* First, check if the part of bcc they sent was zero */
  295. if (tmp[sizeof(struct smb_hdr)] == 0) {
  296. /* some servers return only half of bcc
  297. * on simple responses (wct, bcc both zero)
  298. * in particular have seen this on
  299. * ulogoffX and FindClose. This leaves
  300. * one byte of bcc potentially unitialized
  301. */
  302. /* zero rest of bcc */
  303. tmp[sizeof(struct smb_hdr)+1] = 0;
  304. return 0;
  305. }
  306. cifs_dbg(VFS, "rcvd invalid byte count (bcc)\n");
  307. } else {
  308. cifs_dbg(VFS, "Length less than smb header size\n");
  309. }
  310. return -EIO;
  311. }
  312. /* otherwise, there is enough to get to the BCC */
  313. if (check_smb_hdr(smb))
  314. return -EIO;
  315. clc_len = smbCalcSize(smb);
  316. if (4 + rfclen != total_read) {
  317. cifs_dbg(VFS, "Length read does not match RFC1001 length %d\n",
  318. rfclen);
  319. return -EIO;
  320. }
  321. if (4 + rfclen != clc_len) {
  322. __u16 mid = get_mid(smb);
  323. /* check if bcc wrapped around for large read responses */
  324. if ((rfclen > 64 * 1024) && (rfclen > clc_len)) {
  325. /* check if lengths match mod 64K */
  326. if (((4 + rfclen) & 0xFFFF) == (clc_len & 0xFFFF))
  327. return 0; /* bcc wrapped */
  328. }
  329. cifs_dbg(FYI, "Calculated size %u vs length %u mismatch for mid=%u\n",
  330. clc_len, 4 + rfclen, mid);
  331. if (4 + rfclen < clc_len) {
  332. cifs_dbg(VFS, "RFC1001 size %u smaller than SMB for mid=%u\n",
  333. rfclen, mid);
  334. return -EIO;
  335. } else if (rfclen > clc_len + 512) {
  336. /*
  337. * Some servers (Windows XP in particular) send more
  338. * data than the lengths in the SMB packet would
  339. * indicate on certain calls (byte range locks and
  340. * trans2 find first calls in particular). While the
  341. * client can handle such a frame by ignoring the
  342. * trailing data, we choose limit the amount of extra
  343. * data to 512 bytes.
  344. */
  345. cifs_dbg(VFS, "RFC1001 size %u more than 512 bytes larger than SMB for mid=%u\n",
  346. rfclen, mid);
  347. return -EIO;
  348. }
  349. }
  350. return 0;
  351. }
  352. bool
  353. is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
  354. {
  355. struct smb_hdr *buf = (struct smb_hdr *)buffer;
  356. struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
  357. struct TCP_Server_Info *pserver;
  358. struct cifs_ses *ses;
  359. struct cifs_tcon *tcon;
  360. struct cifsInodeInfo *pCifsInode;
  361. struct cifsFileInfo *netfile;
  362. cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
  363. if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
  364. (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
  365. struct smb_com_transaction_change_notify_rsp *pSMBr =
  366. (struct smb_com_transaction_change_notify_rsp *)buf;
  367. struct file_notify_information *pnotify;
  368. __u32 data_offset = 0;
  369. size_t len = srv->total_read - sizeof(pSMBr->hdr.smb_buf_length);
  370. if (get_bcc(buf) > sizeof(struct file_notify_information)) {
  371. data_offset = le32_to_cpu(pSMBr->DataOffset);
  372. if (data_offset >
  373. len - sizeof(struct file_notify_information)) {
  374. cifs_dbg(FYI, "Invalid data_offset %u\n",
  375. data_offset);
  376. return true;
  377. }
  378. pnotify = (struct file_notify_information *)
  379. ((char *)&pSMBr->hdr.Protocol + data_offset);
  380. cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
  381. pnotify->FileName, pnotify->Action);
  382. /* cifs_dump_mem("Rcvd notify Data: ",buf,
  383. sizeof(struct smb_hdr)+60); */
  384. return true;
  385. }
  386. if (pSMBr->hdr.Status.CifsError) {
  387. cifs_dbg(FYI, "notify err 0x%x\n",
  388. pSMBr->hdr.Status.CifsError);
  389. return true;
  390. }
  391. return false;
  392. }
  393. if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
  394. return false;
  395. if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
  396. /* no sense logging error on invalid handle on oplock
  397. break - harmless race between close request and oplock
  398. break response is expected from time to time writing out
  399. large dirty files cached on the client */
  400. if ((NT_STATUS_INVALID_HANDLE) ==
  401. le32_to_cpu(pSMB->hdr.Status.CifsError)) {
  402. cifs_dbg(FYI, "Invalid handle on oplock break\n");
  403. return true;
  404. } else if (ERRbadfid ==
  405. le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
  406. return true;
  407. } else {
  408. return false; /* on valid oplock brk we get "request" */
  409. }
  410. }
  411. if (pSMB->hdr.WordCount != 8)
  412. return false;
  413. cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
  414. pSMB->LockType, pSMB->OplockLevel);
  415. if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
  416. return false;
  417. /* If server is a channel, select the primary channel */
  418. pserver = CIFS_SERVER_IS_CHAN(srv) ? srv->primary_server : srv;
  419. /* look up tcon based on tid & uid */
  420. spin_lock(&cifs_tcp_ses_lock);
  421. list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
  422. list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
  423. if (tcon->tid != buf->Tid)
  424. continue;
  425. cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
  426. spin_lock(&tcon->open_file_lock);
  427. list_for_each_entry(netfile, &tcon->openFileList, tlist) {
  428. if (pSMB->Fid != netfile->fid.netfid)
  429. continue;
  430. cifs_dbg(FYI, "file id match, oplock break\n");
  431. pCifsInode = CIFS_I(d_inode(netfile->dentry));
  432. set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
  433. &pCifsInode->flags);
  434. netfile->oplock_epoch = 0;
  435. netfile->oplock_level = pSMB->OplockLevel;
  436. netfile->oplock_break_cancelled = false;
  437. cifs_queue_oplock_break(netfile);
  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. struct cifs_tcon *tcon = NULL;
  465. if (cifs_sb->master_tlink)
  466. tcon = cifs_sb_master_tcon(cifs_sb);
  467. cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
  468. cifs_sb->mnt_cifs_serverino_autodisabled = true;
  469. cifs_dbg(VFS, "Autodisabling the use of server inode numbers on %s\n",
  470. tcon ? tcon->tree_name : "new server");
  471. cifs_dbg(VFS, "The server doesn't seem to support them properly or the files might be on different servers (DFS)\n");
  472. cifs_dbg(VFS, "Hardlinks will not be recognized on this mount. Consider mounting with the \"noserverino\" option to silence this message.\n");
  473. }
  474. }
  475. void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
  476. {
  477. oplock &= 0xF;
  478. if (oplock == OPLOCK_EXCLUSIVE) {
  479. cinode->oplock = CIFS_CACHE_WRITE_FLG | CIFS_CACHE_READ_FLG;
  480. cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
  481. &cinode->netfs.inode);
  482. } else if (oplock == OPLOCK_READ) {
  483. cinode->oplock = CIFS_CACHE_READ_FLG;
  484. cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
  485. &cinode->netfs.inode);
  486. } else
  487. cinode->oplock = 0;
  488. }
  489. /*
  490. * We wait for oplock breaks to be processed before we attempt to perform
  491. * writes.
  492. */
  493. int cifs_get_writer(struct cifsInodeInfo *cinode)
  494. {
  495. int rc;
  496. start:
  497. rc = wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK,
  498. TASK_KILLABLE);
  499. if (rc)
  500. return rc;
  501. spin_lock(&cinode->writers_lock);
  502. if (!cinode->writers)
  503. set_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  504. cinode->writers++;
  505. /* Check to see if we have started servicing an oplock break */
  506. if (test_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags)) {
  507. cinode->writers--;
  508. if (cinode->writers == 0) {
  509. clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  510. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
  511. }
  512. spin_unlock(&cinode->writers_lock);
  513. goto start;
  514. }
  515. spin_unlock(&cinode->writers_lock);
  516. return 0;
  517. }
  518. void cifs_put_writer(struct cifsInodeInfo *cinode)
  519. {
  520. spin_lock(&cinode->writers_lock);
  521. cinode->writers--;
  522. if (cinode->writers == 0) {
  523. clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
  524. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
  525. }
  526. spin_unlock(&cinode->writers_lock);
  527. }
  528. /**
  529. * cifs_queue_oplock_break - queue the oplock break handler for cfile
  530. * @cfile: The file to break the oplock on
  531. *
  532. * This function is called from the demultiplex thread when it
  533. * receives an oplock break for @cfile.
  534. *
  535. * Assumes the tcon->open_file_lock is held.
  536. * Assumes cfile->file_info_lock is NOT held.
  537. */
  538. void cifs_queue_oplock_break(struct cifsFileInfo *cfile)
  539. {
  540. /*
  541. * Bump the handle refcount now while we hold the
  542. * open_file_lock to enforce the validity of it for the oplock
  543. * break handler. The matching put is done at the end of the
  544. * handler.
  545. */
  546. cifsFileInfo_get(cfile);
  547. queue_work(cifsoplockd_wq, &cfile->oplock_break);
  548. }
  549. void cifs_done_oplock_break(struct cifsInodeInfo *cinode)
  550. {
  551. clear_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags);
  552. wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK);
  553. }
  554. bool
  555. backup_cred(struct cifs_sb_info *cifs_sb)
  556. {
  557. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID) {
  558. if (uid_eq(cifs_sb->ctx->backupuid, current_fsuid()))
  559. return true;
  560. }
  561. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID) {
  562. if (in_group_p(cifs_sb->ctx->backupgid))
  563. return true;
  564. }
  565. return false;
  566. }
  567. void
  568. cifs_del_pending_open(struct cifs_pending_open *open)
  569. {
  570. spin_lock(&tlink_tcon(open->tlink)->open_file_lock);
  571. list_del(&open->olist);
  572. spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
  573. }
  574. void
  575. cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink,
  576. struct cifs_pending_open *open)
  577. {
  578. memcpy(open->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
  579. open->oplock = CIFS_OPLOCK_NO_CHANGE;
  580. open->tlink = tlink;
  581. fid->pending_open = open;
  582. list_add_tail(&open->olist, &tlink_tcon(tlink)->pending_opens);
  583. }
  584. void
  585. cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink,
  586. struct cifs_pending_open *open)
  587. {
  588. spin_lock(&tlink_tcon(tlink)->open_file_lock);
  589. cifs_add_pending_open_locked(fid, tlink, open);
  590. spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
  591. }
  592. /*
  593. * Critical section which runs after acquiring deferred_lock.
  594. * As there is no reference count on cifs_deferred_close, pdclose
  595. * should not be used outside deferred_lock.
  596. */
  597. bool
  598. cifs_is_deferred_close(struct cifsFileInfo *cfile, struct cifs_deferred_close **pdclose)
  599. {
  600. struct cifs_deferred_close *dclose;
  601. list_for_each_entry(dclose, &CIFS_I(d_inode(cfile->dentry))->deferred_closes, dlist) {
  602. if ((dclose->netfid == cfile->fid.netfid) &&
  603. (dclose->persistent_fid == cfile->fid.persistent_fid) &&
  604. (dclose->volatile_fid == cfile->fid.volatile_fid)) {
  605. *pdclose = dclose;
  606. return true;
  607. }
  608. }
  609. return false;
  610. }
  611. /*
  612. * Critical section which runs after acquiring deferred_lock.
  613. */
  614. void
  615. cifs_add_deferred_close(struct cifsFileInfo *cfile, struct cifs_deferred_close *dclose)
  616. {
  617. bool is_deferred = false;
  618. struct cifs_deferred_close *pdclose;
  619. is_deferred = cifs_is_deferred_close(cfile, &pdclose);
  620. if (is_deferred) {
  621. kfree(dclose);
  622. return;
  623. }
  624. dclose->tlink = cfile->tlink;
  625. dclose->netfid = cfile->fid.netfid;
  626. dclose->persistent_fid = cfile->fid.persistent_fid;
  627. dclose->volatile_fid = cfile->fid.volatile_fid;
  628. list_add_tail(&dclose->dlist, &CIFS_I(d_inode(cfile->dentry))->deferred_closes);
  629. }
  630. /*
  631. * Critical section which runs after acquiring deferred_lock.
  632. */
  633. void
  634. cifs_del_deferred_close(struct cifsFileInfo *cfile)
  635. {
  636. bool is_deferred = false;
  637. struct cifs_deferred_close *dclose;
  638. is_deferred = cifs_is_deferred_close(cfile, &dclose);
  639. if (!is_deferred)
  640. return;
  641. list_del(&dclose->dlist);
  642. kfree(dclose);
  643. }
  644. void
  645. cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode)
  646. {
  647. struct cifsFileInfo *cfile = NULL;
  648. struct file_list *tmp_list, *tmp_next_list;
  649. struct list_head file_head;
  650. if (cifs_inode == NULL)
  651. return;
  652. INIT_LIST_HEAD(&file_head);
  653. spin_lock(&cifs_inode->open_file_lock);
  654. list_for_each_entry(cfile, &cifs_inode->openFileList, flist) {
  655. if (delayed_work_pending(&cfile->deferred)) {
  656. if (cancel_delayed_work(&cfile->deferred)) {
  657. spin_lock(&cifs_inode->deferred_lock);
  658. cifs_del_deferred_close(cfile);
  659. spin_unlock(&cifs_inode->deferred_lock);
  660. tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
  661. if (tmp_list == NULL)
  662. break;
  663. tmp_list->cfile = cfile;
  664. list_add_tail(&tmp_list->list, &file_head);
  665. }
  666. }
  667. }
  668. spin_unlock(&cifs_inode->open_file_lock);
  669. list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
  670. _cifsFileInfo_put(tmp_list->cfile, false, false);
  671. list_del(&tmp_list->list);
  672. kfree(tmp_list);
  673. }
  674. }
  675. void
  676. cifs_close_all_deferred_files(struct cifs_tcon *tcon)
  677. {
  678. struct cifsFileInfo *cfile;
  679. struct file_list *tmp_list, *tmp_next_list;
  680. struct list_head file_head;
  681. INIT_LIST_HEAD(&file_head);
  682. spin_lock(&tcon->open_file_lock);
  683. list_for_each_entry(cfile, &tcon->openFileList, tlist) {
  684. if (delayed_work_pending(&cfile->deferred)) {
  685. if (cancel_delayed_work(&cfile->deferred)) {
  686. spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
  687. cifs_del_deferred_close(cfile);
  688. spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
  689. tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
  690. if (tmp_list == NULL)
  691. break;
  692. tmp_list->cfile = cfile;
  693. list_add_tail(&tmp_list->list, &file_head);
  694. }
  695. }
  696. }
  697. spin_unlock(&tcon->open_file_lock);
  698. list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
  699. _cifsFileInfo_put(tmp_list->cfile, true, false);
  700. list_del(&tmp_list->list);
  701. kfree(tmp_list);
  702. }
  703. }
  704. void
  705. cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon, const char *path)
  706. {
  707. struct cifsFileInfo *cfile;
  708. struct file_list *tmp_list, *tmp_next_list;
  709. struct list_head file_head;
  710. void *page;
  711. const char *full_path;
  712. INIT_LIST_HEAD(&file_head);
  713. page = alloc_dentry_path();
  714. spin_lock(&tcon->open_file_lock);
  715. list_for_each_entry(cfile, &tcon->openFileList, tlist) {
  716. full_path = build_path_from_dentry(cfile->dentry, page);
  717. if (strstr(full_path, path)) {
  718. if (delayed_work_pending(&cfile->deferred)) {
  719. if (cancel_delayed_work(&cfile->deferred)) {
  720. spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
  721. cifs_del_deferred_close(cfile);
  722. spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
  723. tmp_list = kmalloc(sizeof(struct file_list), GFP_ATOMIC);
  724. if (tmp_list == NULL)
  725. break;
  726. tmp_list->cfile = cfile;
  727. list_add_tail(&tmp_list->list, &file_head);
  728. }
  729. }
  730. }
  731. }
  732. spin_unlock(&tcon->open_file_lock);
  733. list_for_each_entry_safe(tmp_list, tmp_next_list, &file_head, list) {
  734. _cifsFileInfo_put(tmp_list->cfile, true, false);
  735. list_del(&tmp_list->list);
  736. kfree(tmp_list);
  737. }
  738. free_dentry_path(page);
  739. }
  740. /* parses DFS referral V3 structure
  741. * caller is responsible for freeing target_nodes
  742. * returns:
  743. * - on success - 0
  744. * - on failure - errno
  745. */
  746. int
  747. parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size,
  748. unsigned int *num_of_nodes,
  749. struct dfs_info3_param **target_nodes,
  750. const struct nls_table *nls_codepage, int remap,
  751. const char *searchName, bool is_unicode)
  752. {
  753. int i, rc = 0;
  754. char *data_end;
  755. struct dfs_referral_level_3 *ref;
  756. *num_of_nodes = le16_to_cpu(rsp->NumberOfReferrals);
  757. if (*num_of_nodes < 1) {
  758. cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
  759. *num_of_nodes);
  760. rc = -EINVAL;
  761. goto parse_DFS_referrals_exit;
  762. }
  763. ref = (struct dfs_referral_level_3 *) &(rsp->referrals);
  764. if (ref->VersionNumber != cpu_to_le16(3)) {
  765. cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
  766. le16_to_cpu(ref->VersionNumber));
  767. rc = -EINVAL;
  768. goto parse_DFS_referrals_exit;
  769. }
  770. /* get the upper boundary of the resp buffer */
  771. data_end = (char *)rsp + rsp_size;
  772. cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
  773. *num_of_nodes, le32_to_cpu(rsp->DFSFlags));
  774. *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
  775. GFP_KERNEL);
  776. if (*target_nodes == NULL) {
  777. rc = -ENOMEM;
  778. goto parse_DFS_referrals_exit;
  779. }
  780. /* collect necessary data from referrals */
  781. for (i = 0; i < *num_of_nodes; i++) {
  782. char *temp;
  783. int max_len;
  784. struct dfs_info3_param *node = (*target_nodes)+i;
  785. node->flags = le32_to_cpu(rsp->DFSFlags);
  786. if (is_unicode) {
  787. __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
  788. GFP_KERNEL);
  789. if (tmp == NULL) {
  790. rc = -ENOMEM;
  791. goto parse_DFS_referrals_exit;
  792. }
  793. cifsConvertToUTF16((__le16 *) tmp, searchName,
  794. PATH_MAX, nls_codepage, remap);
  795. node->path_consumed = cifs_utf16_bytes(tmp,
  796. le16_to_cpu(rsp->PathConsumed),
  797. nls_codepage);
  798. kfree(tmp);
  799. } else
  800. node->path_consumed = le16_to_cpu(rsp->PathConsumed);
  801. node->server_type = le16_to_cpu(ref->ServerType);
  802. node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
  803. /* copy DfsPath */
  804. temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
  805. max_len = data_end - temp;
  806. node->path_name = cifs_strndup_from_utf16(temp, max_len,
  807. is_unicode, nls_codepage);
  808. if (!node->path_name) {
  809. rc = -ENOMEM;
  810. goto parse_DFS_referrals_exit;
  811. }
  812. /* copy link target UNC */
  813. temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
  814. max_len = data_end - temp;
  815. node->node_name = cifs_strndup_from_utf16(temp, max_len,
  816. is_unicode, nls_codepage);
  817. if (!node->node_name) {
  818. rc = -ENOMEM;
  819. goto parse_DFS_referrals_exit;
  820. }
  821. node->ttl = le32_to_cpu(ref->TimeToLive);
  822. ref++;
  823. }
  824. parse_DFS_referrals_exit:
  825. if (rc) {
  826. free_dfs_info_array(*target_nodes, *num_of_nodes);
  827. *target_nodes = NULL;
  828. *num_of_nodes = 0;
  829. }
  830. return rc;
  831. }
  832. struct cifs_aio_ctx *
  833. cifs_aio_ctx_alloc(void)
  834. {
  835. struct cifs_aio_ctx *ctx;
  836. /*
  837. * Must use kzalloc to initialize ctx->bv to NULL and ctx->direct_io
  838. * to false so that we know when we have to unreference pages within
  839. * cifs_aio_ctx_release()
  840. */
  841. ctx = kzalloc(sizeof(struct cifs_aio_ctx), GFP_KERNEL);
  842. if (!ctx)
  843. return NULL;
  844. INIT_LIST_HEAD(&ctx->list);
  845. mutex_init(&ctx->aio_mutex);
  846. init_completion(&ctx->done);
  847. kref_init(&ctx->refcount);
  848. return ctx;
  849. }
  850. void
  851. cifs_aio_ctx_release(struct kref *refcount)
  852. {
  853. struct cifs_aio_ctx *ctx = container_of(refcount,
  854. struct cifs_aio_ctx, refcount);
  855. cifsFileInfo_put(ctx->cfile);
  856. /*
  857. * ctx->bv is only set if setup_aio_ctx_iter() was call successfuly
  858. * which means that iov_iter_get_pages() was a success and thus that
  859. * we have taken reference on pages.
  860. */
  861. if (ctx->bv) {
  862. unsigned i;
  863. for (i = 0; i < ctx->npages; i++) {
  864. if (ctx->should_dirty)
  865. set_page_dirty(ctx->bv[i].bv_page);
  866. put_page(ctx->bv[i].bv_page);
  867. }
  868. kvfree(ctx->bv);
  869. }
  870. kfree(ctx);
  871. }
  872. #define CIFS_AIO_KMALLOC_LIMIT (1024 * 1024)
  873. int
  874. setup_aio_ctx_iter(struct cifs_aio_ctx *ctx, struct iov_iter *iter, int rw)
  875. {
  876. ssize_t rc;
  877. unsigned int cur_npages;
  878. unsigned int npages = 0;
  879. unsigned int i;
  880. size_t len;
  881. size_t count = iov_iter_count(iter);
  882. unsigned int saved_len;
  883. size_t start;
  884. unsigned int max_pages = iov_iter_npages(iter, INT_MAX);
  885. struct page **pages = NULL;
  886. struct bio_vec *bv = NULL;
  887. if (iov_iter_is_kvec(iter)) {
  888. memcpy(&ctx->iter, iter, sizeof(*iter));
  889. ctx->len = count;
  890. iov_iter_advance(iter, count);
  891. return 0;
  892. }
  893. if (array_size(max_pages, sizeof(*bv)) <= CIFS_AIO_KMALLOC_LIMIT)
  894. bv = kmalloc_array(max_pages, sizeof(*bv), GFP_KERNEL);
  895. if (!bv) {
  896. bv = vmalloc(array_size(max_pages, sizeof(*bv)));
  897. if (!bv)
  898. return -ENOMEM;
  899. }
  900. if (array_size(max_pages, sizeof(*pages)) <= CIFS_AIO_KMALLOC_LIMIT)
  901. pages = kmalloc_array(max_pages, sizeof(*pages), GFP_KERNEL);
  902. if (!pages) {
  903. pages = vmalloc(array_size(max_pages, sizeof(*pages)));
  904. if (!pages) {
  905. kvfree(bv);
  906. return -ENOMEM;
  907. }
  908. }
  909. saved_len = count;
  910. while (count && npages < max_pages) {
  911. rc = iov_iter_get_pages2(iter, pages, count, max_pages, &start);
  912. if (rc < 0) {
  913. cifs_dbg(VFS, "Couldn't get user pages (rc=%zd)\n", rc);
  914. break;
  915. }
  916. if (rc > count) {
  917. cifs_dbg(VFS, "get pages rc=%zd more than %zu\n", rc,
  918. count);
  919. break;
  920. }
  921. count -= rc;
  922. rc += start;
  923. cur_npages = DIV_ROUND_UP(rc, PAGE_SIZE);
  924. if (npages + cur_npages > max_pages) {
  925. cifs_dbg(VFS, "out of vec array capacity (%u vs %u)\n",
  926. npages + cur_npages, max_pages);
  927. break;
  928. }
  929. for (i = 0; i < cur_npages; i++) {
  930. len = rc > PAGE_SIZE ? PAGE_SIZE : rc;
  931. bv[npages + i].bv_page = pages[i];
  932. bv[npages + i].bv_offset = start;
  933. bv[npages + i].bv_len = len - start;
  934. rc -= len;
  935. start = 0;
  936. }
  937. npages += cur_npages;
  938. }
  939. kvfree(pages);
  940. ctx->bv = bv;
  941. ctx->len = saved_len - count;
  942. ctx->npages = npages;
  943. iov_iter_bvec(&ctx->iter, rw, ctx->bv, npages, ctx->len);
  944. return 0;
  945. }
  946. /**
  947. * cifs_alloc_hash - allocate hash and hash context together
  948. * @name: The name of the crypto hash algo
  949. * @sdesc: SHASH descriptor where to put the pointer to the hash TFM
  950. *
  951. * The caller has to make sure @sdesc is initialized to either NULL or
  952. * a valid context. It can be freed via cifs_free_hash().
  953. */
  954. int
  955. cifs_alloc_hash(const char *name, struct shash_desc **sdesc)
  956. {
  957. int rc = 0;
  958. struct crypto_shash *alg = NULL;
  959. if (*sdesc)
  960. return 0;
  961. alg = crypto_alloc_shash(name, 0, 0);
  962. if (IS_ERR(alg)) {
  963. cifs_dbg(VFS, "Could not allocate shash TFM '%s'\n", name);
  964. rc = PTR_ERR(alg);
  965. *sdesc = NULL;
  966. return rc;
  967. }
  968. *sdesc = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(alg), GFP_KERNEL);
  969. if (*sdesc == NULL) {
  970. cifs_dbg(VFS, "no memory left to allocate shash TFM '%s'\n", name);
  971. crypto_free_shash(alg);
  972. return -ENOMEM;
  973. }
  974. (*sdesc)->tfm = alg;
  975. return 0;
  976. }
  977. /**
  978. * cifs_free_hash - free hash and hash context together
  979. * @sdesc: Where to find the pointer to the hash TFM
  980. *
  981. * Freeing a NULL descriptor is safe.
  982. */
  983. void
  984. cifs_free_hash(struct shash_desc **sdesc)
  985. {
  986. if (unlikely(!sdesc) || !*sdesc)
  987. return;
  988. if ((*sdesc)->tfm) {
  989. crypto_free_shash((*sdesc)->tfm);
  990. (*sdesc)->tfm = NULL;
  991. }
  992. kfree_sensitive(*sdesc);
  993. *sdesc = NULL;
  994. }
  995. /**
  996. * rqst_page_get_length - obtain the length and offset for a page in smb_rqst
  997. * @rqst: The request descriptor
  998. * @page: The index of the page to query
  999. * @len: Where to store the length for this page:
  1000. * @offset: Where to store the offset for this page
  1001. */
  1002. void rqst_page_get_length(const struct smb_rqst *rqst, unsigned int page,
  1003. unsigned int *len, unsigned int *offset)
  1004. {
  1005. *len = rqst->rq_pagesz;
  1006. *offset = (page == 0) ? rqst->rq_offset : 0;
  1007. if (rqst->rq_npages == 1 || page == rqst->rq_npages-1)
  1008. *len = rqst->rq_tailsz;
  1009. else if (page == 0)
  1010. *len = rqst->rq_pagesz - rqst->rq_offset;
  1011. }
  1012. void extract_unc_hostname(const char *unc, const char **h, size_t *len)
  1013. {
  1014. const char *end;
  1015. /* skip initial slashes */
  1016. while (*unc && (*unc == '\\' || *unc == '/'))
  1017. unc++;
  1018. end = unc;
  1019. while (*end && !(*end == '\\' || *end == '/'))
  1020. end++;
  1021. *h = unc;
  1022. *len = end - unc;
  1023. }
  1024. /**
  1025. * copy_path_name - copy src path to dst, possibly truncating
  1026. * @dst: The destination buffer
  1027. * @src: The source name
  1028. *
  1029. * returns number of bytes written (including trailing nul)
  1030. */
  1031. int copy_path_name(char *dst, const char *src)
  1032. {
  1033. int name_len;
  1034. /*
  1035. * PATH_MAX includes nul, so if strlen(src) >= PATH_MAX it
  1036. * will truncate and strlen(dst) will be PATH_MAX-1
  1037. */
  1038. name_len = strscpy(dst, src, PATH_MAX);
  1039. if (WARN_ON_ONCE(name_len < 0))
  1040. name_len = PATH_MAX-1;
  1041. /* we count the trailing nul */
  1042. name_len++;
  1043. return name_len;
  1044. }
  1045. struct super_cb_data {
  1046. void *data;
  1047. struct super_block *sb;
  1048. };
  1049. static void tcp_super_cb(struct super_block *sb, void *arg)
  1050. {
  1051. struct super_cb_data *sd = arg;
  1052. struct TCP_Server_Info *server = sd->data;
  1053. struct cifs_sb_info *cifs_sb;
  1054. struct cifs_tcon *tcon;
  1055. if (sd->sb)
  1056. return;
  1057. cifs_sb = CIFS_SB(sb);
  1058. tcon = cifs_sb_master_tcon(cifs_sb);
  1059. if (tcon->ses->server == server)
  1060. sd->sb = sb;
  1061. }
  1062. static struct super_block *__cifs_get_super(void (*f)(struct super_block *, void *),
  1063. void *data)
  1064. {
  1065. struct super_cb_data sd = {
  1066. .data = data,
  1067. .sb = NULL,
  1068. };
  1069. struct file_system_type **fs_type = (struct file_system_type *[]) {
  1070. &cifs_fs_type, &smb3_fs_type, NULL,
  1071. };
  1072. for (; *fs_type; fs_type++) {
  1073. iterate_supers_type(*fs_type, f, &sd);
  1074. if (sd.sb) {
  1075. /*
  1076. * Grab an active reference in order to prevent automounts (DFS links)
  1077. * of expiring and then freeing up our cifs superblock pointer while
  1078. * we're doing failover.
  1079. */
  1080. cifs_sb_active(sd.sb);
  1081. return sd.sb;
  1082. }
  1083. }
  1084. return ERR_PTR(-EINVAL);
  1085. }
  1086. static void __cifs_put_super(struct super_block *sb)
  1087. {
  1088. if (!IS_ERR_OR_NULL(sb))
  1089. cifs_sb_deactive(sb);
  1090. }
  1091. struct super_block *cifs_get_tcp_super(struct TCP_Server_Info *server)
  1092. {
  1093. return __cifs_get_super(tcp_super_cb, server);
  1094. }
  1095. void cifs_put_tcp_super(struct super_block *sb)
  1096. {
  1097. __cifs_put_super(sb);
  1098. }
  1099. #ifdef CONFIG_CIFS_DFS_UPCALL
  1100. int match_target_ip(struct TCP_Server_Info *server,
  1101. const char *share, size_t share_len,
  1102. bool *result)
  1103. {
  1104. int rc;
  1105. char *target, *tip = NULL;
  1106. struct sockaddr tipaddr;
  1107. *result = false;
  1108. target = kzalloc(share_len + 3, GFP_KERNEL);
  1109. if (!target) {
  1110. rc = -ENOMEM;
  1111. goto out;
  1112. }
  1113. scnprintf(target, share_len + 3, "\\\\%.*s", (int)share_len, share);
  1114. cifs_dbg(FYI, "%s: target name: %s\n", __func__, target + 2);
  1115. rc = dns_resolve_server_name_to_ip(target, &tip, NULL);
  1116. if (rc < 0)
  1117. goto out;
  1118. cifs_dbg(FYI, "%s: target ip: %s\n", __func__, tip);
  1119. if (!cifs_convert_address(&tipaddr, tip, strlen(tip))) {
  1120. cifs_dbg(VFS, "%s: failed to convert target ip address\n",
  1121. __func__);
  1122. rc = -EINVAL;
  1123. goto out;
  1124. }
  1125. *result = cifs_match_ipaddr((struct sockaddr *)&server->dstaddr,
  1126. &tipaddr);
  1127. cifs_dbg(FYI, "%s: ip addresses match: %u\n", __func__, *result);
  1128. rc = 0;
  1129. out:
  1130. kfree(target);
  1131. kfree(tip);
  1132. return rc;
  1133. }
  1134. int cifs_update_super_prepath(struct cifs_sb_info *cifs_sb, char *prefix)
  1135. {
  1136. kfree(cifs_sb->prepath);
  1137. if (prefix && *prefix) {
  1138. cifs_sb->prepath = cifs_sanitize_prepath(prefix, GFP_ATOMIC);
  1139. if (!cifs_sb->prepath)
  1140. return -ENOMEM;
  1141. convert_delimiter(cifs_sb->prepath, CIFS_DIR_SEP(cifs_sb));
  1142. } else
  1143. cifs_sb->prepath = NULL;
  1144. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
  1145. return 0;
  1146. }
  1147. /*
  1148. * Handle weird Windows SMB server behaviour. It responds with
  1149. * STATUS_OBJECT_NAME_INVALID code to SMB2 QUERY_INFO request for
  1150. * "\<server>\<dfsname>\<linkpath>" DFS reference, where <dfsname> contains
  1151. * non-ASCII unicode symbols.
  1152. */
  1153. int cifs_inval_name_dfs_link_error(const unsigned int xid,
  1154. struct cifs_tcon *tcon,
  1155. struct cifs_sb_info *cifs_sb,
  1156. const char *full_path,
  1157. bool *islink)
  1158. {
  1159. struct cifs_ses *ses = tcon->ses;
  1160. size_t len;
  1161. char *path;
  1162. char *ref_path;
  1163. *islink = false;
  1164. /*
  1165. * Fast path - skip check when @full_path doesn't have a prefix path to
  1166. * look up or tcon is not DFS.
  1167. */
  1168. if (strlen(full_path) < 2 || !cifs_sb ||
  1169. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) ||
  1170. !is_tcon_dfs(tcon) || !ses->server->origin_fullpath)
  1171. return 0;
  1172. /*
  1173. * Slow path - tcon is DFS and @full_path has prefix path, so attempt
  1174. * to get a referral to figure out whether it is an DFS link.
  1175. */
  1176. len = strnlen(tcon->tree_name, MAX_TREE_SIZE + 1) + strlen(full_path) + 1;
  1177. path = kmalloc(len, GFP_KERNEL);
  1178. if (!path)
  1179. return -ENOMEM;
  1180. scnprintf(path, len, "%s%s", tcon->tree_name, full_path);
  1181. ref_path = dfs_cache_canonical_path(path + 1, cifs_sb->local_nls,
  1182. cifs_remap(cifs_sb));
  1183. kfree(path);
  1184. if (IS_ERR(ref_path)) {
  1185. if (PTR_ERR(ref_path) != -EINVAL)
  1186. return PTR_ERR(ref_path);
  1187. } else {
  1188. struct dfs_info3_param *refs = NULL;
  1189. int num_refs = 0;
  1190. /*
  1191. * XXX: we are not using dfs_cache_find() here because we might
  1192. * end filling all the DFS cache and thus potentially
  1193. * removing cached DFS targets that the client would eventually
  1194. * need during failover.
  1195. */
  1196. if (ses->server->ops->get_dfs_refer &&
  1197. !ses->server->ops->get_dfs_refer(xid, ses, ref_path, &refs,
  1198. &num_refs, cifs_sb->local_nls,
  1199. cifs_remap(cifs_sb)))
  1200. *islink = refs[0].server_type == DFS_TYPE_LINK;
  1201. free_dfs_info_array(refs, num_refs);
  1202. kfree(ref_path);
  1203. }
  1204. return 0;
  1205. }
  1206. #endif
  1207. int cifs_wait_for_server_reconnect(struct TCP_Server_Info *server, bool retry)
  1208. {
  1209. int timeout = 10;
  1210. int rc;
  1211. spin_lock(&server->srv_lock);
  1212. if (server->tcpStatus != CifsNeedReconnect) {
  1213. spin_unlock(&server->srv_lock);
  1214. return 0;
  1215. }
  1216. timeout *= server->nr_targets;
  1217. spin_unlock(&server->srv_lock);
  1218. /*
  1219. * Give demultiplex thread up to 10 seconds to each target available for
  1220. * reconnect -- should be greater than cifs socket timeout which is 7
  1221. * seconds.
  1222. *
  1223. * On "soft" mounts we wait once. Hard mounts keep retrying until
  1224. * process is killed or server comes back on-line.
  1225. */
  1226. do {
  1227. rc = wait_event_interruptible_timeout(server->response_q,
  1228. (server->tcpStatus != CifsNeedReconnect),
  1229. timeout * HZ);
  1230. if (rc < 0) {
  1231. cifs_dbg(FYI, "%s: aborting reconnect due to received signal\n",
  1232. __func__);
  1233. return -ERESTARTSYS;
  1234. }
  1235. /* are we still trying to reconnect? */
  1236. spin_lock(&server->srv_lock);
  1237. if (server->tcpStatus != CifsNeedReconnect) {
  1238. spin_unlock(&server->srv_lock);
  1239. return 0;
  1240. }
  1241. spin_unlock(&server->srv_lock);
  1242. } while (retry);
  1243. cifs_dbg(FYI, "%s: gave up waiting on reconnect\n", __func__);
  1244. return -EHOSTDOWN;
  1245. }