misc.c 37 KB

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