dfs_cache.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DFS referral cache routines
  4. *
  5. * Copyright (c) 2018-2019 Paulo Alcantara <palcantara@suse.de>
  6. */
  7. #include <linux/jhash.h>
  8. #include <linux/ktime.h>
  9. #include <linux/slab.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/nls.h>
  12. #include <linux/workqueue.h>
  13. #include "cifsglob.h"
  14. #include "smb2pdu.h"
  15. #include "smb2proto.h"
  16. #include "cifsproto.h"
  17. #include "cifs_debug.h"
  18. #include "cifs_unicode.h"
  19. #include "smb2glob.h"
  20. #include "dfs_cache.h"
  21. #define CACHE_HTABLE_SIZE 32
  22. #define CACHE_MAX_ENTRIES 64
  23. #define IS_INTERLINK_SET(v) ((v) & (DFSREF_REFERRAL_SERVER | \
  24. DFSREF_STORAGE_SERVER))
  25. struct cache_dfs_tgt {
  26. char *name;
  27. int path_consumed;
  28. struct list_head list;
  29. };
  30. struct cache_entry {
  31. struct hlist_node hlist;
  32. const char *path;
  33. int ttl;
  34. int srvtype;
  35. int flags;
  36. struct timespec64 etime;
  37. int path_consumed;
  38. int numtgts;
  39. struct list_head tlist;
  40. struct cache_dfs_tgt *tgthint;
  41. };
  42. struct vol_info {
  43. char *fullpath;
  44. spinlock_t smb_vol_lock;
  45. struct smb_vol smb_vol;
  46. char *mntdata;
  47. struct list_head list;
  48. struct list_head rlist;
  49. struct kref refcnt;
  50. };
  51. static struct kmem_cache *cache_slab __read_mostly;
  52. static struct workqueue_struct *dfscache_wq __read_mostly;
  53. static int cache_ttl;
  54. static DEFINE_SPINLOCK(cache_ttl_lock);
  55. static struct nls_table *cache_nlsc;
  56. /*
  57. * Number of entries in the cache
  58. */
  59. static atomic_t cache_count;
  60. static struct hlist_head cache_htable[CACHE_HTABLE_SIZE];
  61. static DECLARE_RWSEM(htable_rw_lock);
  62. static LIST_HEAD(vol_list);
  63. static DEFINE_SPINLOCK(vol_list_lock);
  64. static void refresh_cache_worker(struct work_struct *work);
  65. static DECLARE_DELAYED_WORK(refresh_task, refresh_cache_worker);
  66. static int get_normalized_path(const char *path, char **npath)
  67. {
  68. if (!path || strlen(path) < 3 || (*path != '\\' && *path != '/'))
  69. return -EINVAL;
  70. if (*path == '\\') {
  71. *npath = (char *)path;
  72. } else {
  73. *npath = kstrndup(path, strlen(path), GFP_KERNEL);
  74. if (!*npath)
  75. return -ENOMEM;
  76. convert_delimiter(*npath, '\\');
  77. }
  78. return 0;
  79. }
  80. static inline void free_normalized_path(const char *path, char *npath)
  81. {
  82. if (path != npath)
  83. kfree(npath);
  84. }
  85. static inline bool cache_entry_expired(const struct cache_entry *ce)
  86. {
  87. struct timespec64 ts;
  88. ktime_get_coarse_real_ts64(&ts);
  89. return timespec64_compare(&ts, &ce->etime) >= 0;
  90. }
  91. static inline void free_tgts(struct cache_entry *ce)
  92. {
  93. struct cache_dfs_tgt *t, *n;
  94. list_for_each_entry_safe(t, n, &ce->tlist, list) {
  95. list_del(&t->list);
  96. kfree(t->name);
  97. kfree(t);
  98. }
  99. }
  100. static inline void flush_cache_ent(struct cache_entry *ce)
  101. {
  102. hlist_del_init(&ce->hlist);
  103. kfree(ce->path);
  104. free_tgts(ce);
  105. atomic_dec(&cache_count);
  106. kmem_cache_free(cache_slab, ce);
  107. }
  108. static void flush_cache_ents(void)
  109. {
  110. int i;
  111. for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
  112. struct hlist_head *l = &cache_htable[i];
  113. struct hlist_node *n;
  114. struct cache_entry *ce;
  115. hlist_for_each_entry_safe(ce, n, l, hlist) {
  116. if (!hlist_unhashed(&ce->hlist))
  117. flush_cache_ent(ce);
  118. }
  119. }
  120. }
  121. /*
  122. * dfs cache /proc file
  123. */
  124. static int dfscache_proc_show(struct seq_file *m, void *v)
  125. {
  126. int i;
  127. struct cache_entry *ce;
  128. struct cache_dfs_tgt *t;
  129. seq_puts(m, "DFS cache\n---------\n");
  130. down_read(&htable_rw_lock);
  131. for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
  132. struct hlist_head *l = &cache_htable[i];
  133. hlist_for_each_entry(ce, l, hlist) {
  134. if (hlist_unhashed(&ce->hlist))
  135. continue;
  136. seq_printf(m,
  137. "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,"
  138. "interlink=%s,path_consumed=%d,expired=%s\n",
  139. ce->path,
  140. ce->srvtype == DFS_TYPE_ROOT ? "root" : "link",
  141. ce->ttl, ce->etime.tv_nsec,
  142. IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
  143. ce->path_consumed,
  144. cache_entry_expired(ce) ? "yes" : "no");
  145. list_for_each_entry(t, &ce->tlist, list) {
  146. seq_printf(m, " %s%s\n",
  147. t->name,
  148. ce->tgthint == t ? " (target hint)" : "");
  149. }
  150. }
  151. }
  152. up_read(&htable_rw_lock);
  153. return 0;
  154. }
  155. static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
  156. size_t count, loff_t *ppos)
  157. {
  158. char c;
  159. int rc;
  160. rc = get_user(c, buffer);
  161. if (rc)
  162. return rc;
  163. if (c != '0')
  164. return -EINVAL;
  165. cifs_dbg(FYI, "clearing dfs cache\n");
  166. down_write(&htable_rw_lock);
  167. flush_cache_ents();
  168. up_write(&htable_rw_lock);
  169. return count;
  170. }
  171. static int dfscache_proc_open(struct inode *inode, struct file *file)
  172. {
  173. return single_open(file, dfscache_proc_show, NULL);
  174. }
  175. const struct proc_ops dfscache_proc_ops = {
  176. .proc_open = dfscache_proc_open,
  177. .proc_read = seq_read,
  178. .proc_lseek = seq_lseek,
  179. .proc_release = single_release,
  180. .proc_write = dfscache_proc_write,
  181. };
  182. #ifdef CONFIG_CIFS_DEBUG2
  183. static inline void dump_tgts(const struct cache_entry *ce)
  184. {
  185. struct cache_dfs_tgt *t;
  186. cifs_dbg(FYI, "target list:\n");
  187. list_for_each_entry(t, &ce->tlist, list) {
  188. cifs_dbg(FYI, " %s%s\n", t->name,
  189. ce->tgthint == t ? " (target hint)" : "");
  190. }
  191. }
  192. static inline void dump_ce(const struct cache_entry *ce)
  193. {
  194. cifs_dbg(FYI, "cache entry: path=%s,type=%s,ttl=%d,etime=%ld,interlink=%s,path_consumed=%d,expired=%s\n",
  195. ce->path,
  196. ce->srvtype == DFS_TYPE_ROOT ? "root" : "link", ce->ttl,
  197. ce->etime.tv_nsec,
  198. IS_INTERLINK_SET(ce->flags) ? "yes" : "no",
  199. ce->path_consumed,
  200. cache_entry_expired(ce) ? "yes" : "no");
  201. dump_tgts(ce);
  202. }
  203. static inline void dump_refs(const struct dfs_info3_param *refs, int numrefs)
  204. {
  205. int i;
  206. cifs_dbg(FYI, "DFS referrals returned by the server:\n");
  207. for (i = 0; i < numrefs; i++) {
  208. const struct dfs_info3_param *ref = &refs[i];
  209. cifs_dbg(FYI,
  210. "\n"
  211. "flags: 0x%x\n"
  212. "path_consumed: %d\n"
  213. "server_type: 0x%x\n"
  214. "ref_flag: 0x%x\n"
  215. "path_name: %s\n"
  216. "node_name: %s\n"
  217. "ttl: %d (%dm)\n",
  218. ref->flags, ref->path_consumed, ref->server_type,
  219. ref->ref_flag, ref->path_name, ref->node_name,
  220. ref->ttl, ref->ttl / 60);
  221. }
  222. }
  223. #else
  224. #define dump_tgts(e)
  225. #define dump_ce(e)
  226. #define dump_refs(r, n)
  227. #endif
  228. /**
  229. * dfs_cache_init - Initialize DFS referral cache.
  230. *
  231. * Return zero if initialized successfully, otherwise non-zero.
  232. */
  233. int dfs_cache_init(void)
  234. {
  235. int rc;
  236. int i;
  237. dfscache_wq = alloc_workqueue("cifs-dfscache",
  238. WQ_FREEZABLE | WQ_MEM_RECLAIM, 1);
  239. if (!dfscache_wq)
  240. return -ENOMEM;
  241. cache_slab = kmem_cache_create("cifs_dfs_cache",
  242. sizeof(struct cache_entry), 0,
  243. SLAB_HWCACHE_ALIGN, NULL);
  244. if (!cache_slab) {
  245. rc = -ENOMEM;
  246. goto out_destroy_wq;
  247. }
  248. for (i = 0; i < CACHE_HTABLE_SIZE; i++)
  249. INIT_HLIST_HEAD(&cache_htable[i]);
  250. atomic_set(&cache_count, 0);
  251. cache_nlsc = load_nls_default();
  252. cifs_dbg(FYI, "%s: initialized DFS referral cache\n", __func__);
  253. return 0;
  254. out_destroy_wq:
  255. destroy_workqueue(dfscache_wq);
  256. return rc;
  257. }
  258. static inline unsigned int cache_entry_hash(const void *data, int size)
  259. {
  260. unsigned int h;
  261. h = jhash(data, size, 0);
  262. return h & (CACHE_HTABLE_SIZE - 1);
  263. }
  264. /* Check whether second path component of @path is SYSVOL or NETLOGON */
  265. static inline bool is_sysvol_or_netlogon(const char *path)
  266. {
  267. const char *s;
  268. char sep = path[0];
  269. s = strchr(path + 1, sep) + 1;
  270. return !strncasecmp(s, "sysvol", strlen("sysvol")) ||
  271. !strncasecmp(s, "netlogon", strlen("netlogon"));
  272. }
  273. /* Return target hint of a DFS cache entry */
  274. static inline char *get_tgt_name(const struct cache_entry *ce)
  275. {
  276. struct cache_dfs_tgt *t = ce->tgthint;
  277. return t ? t->name : ERR_PTR(-ENOENT);
  278. }
  279. /* Return expire time out of a new entry's TTL */
  280. static inline struct timespec64 get_expire_time(int ttl)
  281. {
  282. struct timespec64 ts = {
  283. .tv_sec = ttl,
  284. .tv_nsec = 0,
  285. };
  286. struct timespec64 now;
  287. ktime_get_coarse_real_ts64(&now);
  288. return timespec64_add(now, ts);
  289. }
  290. /* Allocate a new DFS target */
  291. static struct cache_dfs_tgt *alloc_target(const char *name, int path_consumed)
  292. {
  293. struct cache_dfs_tgt *t;
  294. t = kmalloc(sizeof(*t), GFP_ATOMIC);
  295. if (!t)
  296. return ERR_PTR(-ENOMEM);
  297. t->name = kstrndup(name, strlen(name), GFP_ATOMIC);
  298. if (!t->name) {
  299. kfree(t);
  300. return ERR_PTR(-ENOMEM);
  301. }
  302. t->path_consumed = path_consumed;
  303. INIT_LIST_HEAD(&t->list);
  304. return t;
  305. }
  306. /*
  307. * Copy DFS referral information to a cache entry and conditionally update
  308. * target hint.
  309. */
  310. static int copy_ref_data(const struct dfs_info3_param *refs, int numrefs,
  311. struct cache_entry *ce, const char *tgthint)
  312. {
  313. int i;
  314. ce->ttl = refs[0].ttl;
  315. ce->etime = get_expire_time(ce->ttl);
  316. ce->srvtype = refs[0].server_type;
  317. ce->flags = refs[0].ref_flag;
  318. ce->path_consumed = refs[0].path_consumed;
  319. for (i = 0; i < numrefs; i++) {
  320. struct cache_dfs_tgt *t;
  321. t = alloc_target(refs[i].node_name, refs[i].path_consumed);
  322. if (IS_ERR(t)) {
  323. free_tgts(ce);
  324. return PTR_ERR(t);
  325. }
  326. if (tgthint && !strcasecmp(t->name, tgthint)) {
  327. list_add(&t->list, &ce->tlist);
  328. tgthint = NULL;
  329. } else {
  330. list_add_tail(&t->list, &ce->tlist);
  331. }
  332. ce->numtgts++;
  333. }
  334. ce->tgthint = list_first_entry_or_null(&ce->tlist,
  335. struct cache_dfs_tgt, list);
  336. return 0;
  337. }
  338. /* Allocate a new cache entry */
  339. static struct cache_entry *alloc_cache_entry(const char *path,
  340. const struct dfs_info3_param *refs,
  341. int numrefs)
  342. {
  343. struct cache_entry *ce;
  344. int rc;
  345. ce = kmem_cache_zalloc(cache_slab, GFP_KERNEL);
  346. if (!ce)
  347. return ERR_PTR(-ENOMEM);
  348. ce->path = kstrndup(path, strlen(path), GFP_KERNEL);
  349. if (!ce->path) {
  350. kmem_cache_free(cache_slab, ce);
  351. return ERR_PTR(-ENOMEM);
  352. }
  353. INIT_HLIST_NODE(&ce->hlist);
  354. INIT_LIST_HEAD(&ce->tlist);
  355. rc = copy_ref_data(refs, numrefs, ce, NULL);
  356. if (rc) {
  357. kfree(ce->path);
  358. kmem_cache_free(cache_slab, ce);
  359. ce = ERR_PTR(rc);
  360. }
  361. return ce;
  362. }
  363. /* Must be called with htable_rw_lock held */
  364. static void remove_oldest_entry(void)
  365. {
  366. int i;
  367. struct cache_entry *ce;
  368. struct cache_entry *to_del = NULL;
  369. for (i = 0; i < CACHE_HTABLE_SIZE; i++) {
  370. struct hlist_head *l = &cache_htable[i];
  371. hlist_for_each_entry(ce, l, hlist) {
  372. if (hlist_unhashed(&ce->hlist))
  373. continue;
  374. if (!to_del || timespec64_compare(&ce->etime,
  375. &to_del->etime) < 0)
  376. to_del = ce;
  377. }
  378. }
  379. if (!to_del) {
  380. cifs_dbg(FYI, "%s: no entry to remove\n", __func__);
  381. return;
  382. }
  383. cifs_dbg(FYI, "%s: removing entry\n", __func__);
  384. dump_ce(to_del);
  385. flush_cache_ent(to_del);
  386. }
  387. /* Add a new DFS cache entry */
  388. static int add_cache_entry(const char *path, unsigned int hash,
  389. struct dfs_info3_param *refs, int numrefs)
  390. {
  391. struct cache_entry *ce;
  392. ce = alloc_cache_entry(path, refs, numrefs);
  393. if (IS_ERR(ce))
  394. return PTR_ERR(ce);
  395. spin_lock(&cache_ttl_lock);
  396. if (!cache_ttl) {
  397. cache_ttl = ce->ttl;
  398. queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
  399. } else {
  400. cache_ttl = min_t(int, cache_ttl, ce->ttl);
  401. mod_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
  402. }
  403. spin_unlock(&cache_ttl_lock);
  404. down_write(&htable_rw_lock);
  405. hlist_add_head(&ce->hlist, &cache_htable[hash]);
  406. dump_ce(ce);
  407. up_write(&htable_rw_lock);
  408. return 0;
  409. }
  410. static struct cache_entry *__lookup_cache_entry(const char *path)
  411. {
  412. struct cache_entry *ce;
  413. unsigned int h;
  414. bool found = false;
  415. h = cache_entry_hash(path, strlen(path));
  416. hlist_for_each_entry(ce, &cache_htable[h], hlist) {
  417. if (!strcasecmp(path, ce->path)) {
  418. found = true;
  419. dump_ce(ce);
  420. break;
  421. }
  422. }
  423. if (!found)
  424. ce = ERR_PTR(-ENOENT);
  425. return ce;
  426. }
  427. /*
  428. * Find a DFS cache entry in hash table and optionally check prefix path against
  429. * @path.
  430. * Use whole path components in the match.
  431. * Must be called with htable_rw_lock held.
  432. *
  433. * Return ERR_PTR(-ENOENT) if the entry is not found.
  434. */
  435. static struct cache_entry *lookup_cache_entry(const char *path, unsigned int *hash)
  436. {
  437. struct cache_entry *ce = ERR_PTR(-ENOENT);
  438. unsigned int h;
  439. int cnt = 0;
  440. char *npath;
  441. char *s, *e;
  442. char sep;
  443. npath = kstrndup(path, strlen(path), GFP_KERNEL);
  444. if (!npath)
  445. return ERR_PTR(-ENOMEM);
  446. s = npath;
  447. sep = *npath;
  448. while ((s = strchr(s, sep)) && ++cnt < 3)
  449. s++;
  450. if (cnt < 3) {
  451. h = cache_entry_hash(path, strlen(path));
  452. ce = __lookup_cache_entry(path);
  453. goto out;
  454. }
  455. /*
  456. * Handle paths that have more than two path components and are a complete prefix of the DFS
  457. * referral request path (@path).
  458. *
  459. * See MS-DFSC 3.2.5.5 "Receiving a Root Referral Request or Link Referral Request".
  460. */
  461. h = cache_entry_hash(npath, strlen(npath));
  462. e = npath + strlen(npath) - 1;
  463. while (e > s) {
  464. char tmp;
  465. /* skip separators */
  466. while (e > s && *e == sep)
  467. e--;
  468. if (e == s)
  469. goto out;
  470. tmp = *(e+1);
  471. *(e+1) = 0;
  472. ce = __lookup_cache_entry(npath);
  473. if (!IS_ERR(ce)) {
  474. h = cache_entry_hash(npath, strlen(npath));
  475. break;
  476. }
  477. *(e+1) = tmp;
  478. /* backward until separator */
  479. while (e > s && *e != sep)
  480. e--;
  481. }
  482. out:
  483. if (hash)
  484. *hash = h;
  485. kfree(npath);
  486. return ce;
  487. }
  488. static void __vol_release(struct vol_info *vi)
  489. {
  490. kfree(vi->fullpath);
  491. kfree(vi->mntdata);
  492. cifs_cleanup_volume_info_contents(&vi->smb_vol);
  493. kfree(vi);
  494. }
  495. static void vol_release(struct kref *kref)
  496. {
  497. struct vol_info *vi = container_of(kref, struct vol_info, refcnt);
  498. spin_lock(&vol_list_lock);
  499. list_del(&vi->list);
  500. spin_unlock(&vol_list_lock);
  501. __vol_release(vi);
  502. }
  503. static inline void free_vol_list(void)
  504. {
  505. struct vol_info *vi, *nvi;
  506. list_for_each_entry_safe(vi, nvi, &vol_list, list) {
  507. list_del_init(&vi->list);
  508. __vol_release(vi);
  509. }
  510. }
  511. /**
  512. * dfs_cache_destroy - destroy DFS referral cache
  513. */
  514. void dfs_cache_destroy(void)
  515. {
  516. cancel_delayed_work_sync(&refresh_task);
  517. unload_nls(cache_nlsc);
  518. free_vol_list();
  519. flush_cache_ents();
  520. kmem_cache_destroy(cache_slab);
  521. destroy_workqueue(dfscache_wq);
  522. cifs_dbg(FYI, "%s: destroyed DFS referral cache\n", __func__);
  523. }
  524. /* Must be called with htable_rw_lock held */
  525. static int __update_cache_entry(const char *path,
  526. const struct dfs_info3_param *refs,
  527. int numrefs)
  528. {
  529. int rc;
  530. struct cache_entry *ce;
  531. char *s, *th = NULL;
  532. ce = lookup_cache_entry(path, NULL);
  533. if (IS_ERR(ce))
  534. return PTR_ERR(ce);
  535. if (ce->tgthint) {
  536. s = ce->tgthint->name;
  537. th = kstrndup(s, strlen(s), GFP_ATOMIC);
  538. if (!th)
  539. return -ENOMEM;
  540. }
  541. free_tgts(ce);
  542. ce->numtgts = 0;
  543. rc = copy_ref_data(refs, numrefs, ce, th);
  544. kfree(th);
  545. return rc;
  546. }
  547. static int get_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
  548. const struct nls_table *nls_codepage, int remap,
  549. const char *path, struct dfs_info3_param **refs,
  550. int *numrefs)
  551. {
  552. cifs_dbg(FYI, "%s: get an DFS referral for %s\n", __func__, path);
  553. if (!ses || !ses->server || !ses->server->ops->get_dfs_refer)
  554. return -EOPNOTSUPP;
  555. if (unlikely(!nls_codepage))
  556. return -EINVAL;
  557. *refs = NULL;
  558. *numrefs = 0;
  559. return ses->server->ops->get_dfs_refer(xid, ses, path, refs, numrefs,
  560. nls_codepage, remap);
  561. }
  562. /* Update an expired cache entry by getting a new DFS referral from server */
  563. static int update_cache_entry(const char *path,
  564. const struct dfs_info3_param *refs,
  565. int numrefs)
  566. {
  567. int rc;
  568. down_write(&htable_rw_lock);
  569. rc = __update_cache_entry(path, refs, numrefs);
  570. up_write(&htable_rw_lock);
  571. return rc;
  572. }
  573. /*
  574. * Find, create or update a DFS cache entry.
  575. *
  576. * If the entry wasn't found, it will create a new one. Or if it was found but
  577. * expired, then it will update the entry accordingly.
  578. *
  579. * For interlinks, __cifs_dfs_mount() and expand_dfs_referral() are supposed to
  580. * handle them properly.
  581. */
  582. static int __dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
  583. const struct nls_table *nls_codepage, int remap,
  584. const char *path, bool noreq)
  585. {
  586. int rc;
  587. unsigned int hash;
  588. struct cache_entry *ce;
  589. struct dfs_info3_param *refs = NULL;
  590. int numrefs = 0;
  591. bool newent = false;
  592. cifs_dbg(FYI, "%s: search path: %s\n", __func__, path);
  593. down_read(&htable_rw_lock);
  594. ce = lookup_cache_entry(path, &hash);
  595. /*
  596. * If @noreq is set, no requests will be sent to the server. Just return
  597. * the cache entry.
  598. */
  599. if (noreq) {
  600. up_read(&htable_rw_lock);
  601. return PTR_ERR_OR_ZERO(ce);
  602. }
  603. if (!IS_ERR(ce)) {
  604. if (!cache_entry_expired(ce)) {
  605. dump_ce(ce);
  606. up_read(&htable_rw_lock);
  607. return 0;
  608. }
  609. } else {
  610. newent = true;
  611. }
  612. up_read(&htable_rw_lock);
  613. /*
  614. * No entry was found.
  615. *
  616. * Request a new DFS referral in order to create a new cache entry, or
  617. * updating an existing one.
  618. */
  619. rc = get_dfs_referral(xid, ses, nls_codepage, remap, path,
  620. &refs, &numrefs);
  621. if (rc)
  622. return rc;
  623. dump_refs(refs, numrefs);
  624. if (!newent) {
  625. rc = update_cache_entry(path, refs, numrefs);
  626. goto out_free_refs;
  627. }
  628. if (atomic_read(&cache_count) >= CACHE_MAX_ENTRIES) {
  629. cifs_dbg(FYI, "%s: reached max cache size (%d)\n",
  630. __func__, CACHE_MAX_ENTRIES);
  631. down_write(&htable_rw_lock);
  632. remove_oldest_entry();
  633. up_write(&htable_rw_lock);
  634. }
  635. rc = add_cache_entry(path, hash, refs, numrefs);
  636. if (!rc)
  637. atomic_inc(&cache_count);
  638. out_free_refs:
  639. free_dfs_info_array(refs, numrefs);
  640. return rc;
  641. }
  642. /*
  643. * Set up a DFS referral from a given cache entry.
  644. *
  645. * Must be called with htable_rw_lock held.
  646. */
  647. static int setup_referral(const char *path, struct cache_entry *ce,
  648. struct dfs_info3_param *ref, const char *target)
  649. {
  650. int rc;
  651. cifs_dbg(FYI, "%s: set up new ref\n", __func__);
  652. memset(ref, 0, sizeof(*ref));
  653. ref->path_name = kstrndup(path, strlen(path), GFP_ATOMIC);
  654. if (!ref->path_name)
  655. return -ENOMEM;
  656. ref->node_name = kstrndup(target, strlen(target), GFP_ATOMIC);
  657. if (!ref->node_name) {
  658. rc = -ENOMEM;
  659. goto err_free_path;
  660. }
  661. ref->path_consumed = ce->path_consumed;
  662. ref->ttl = ce->ttl;
  663. ref->server_type = ce->srvtype;
  664. ref->ref_flag = ce->flags;
  665. return 0;
  666. err_free_path:
  667. kfree(ref->path_name);
  668. ref->path_name = NULL;
  669. return rc;
  670. }
  671. /* Return target list of a DFS cache entry */
  672. static int get_targets(struct cache_entry *ce, struct dfs_cache_tgt_list *tl)
  673. {
  674. int rc;
  675. struct list_head *head = &tl->tl_list;
  676. struct cache_dfs_tgt *t;
  677. struct dfs_cache_tgt_iterator *it, *nit;
  678. memset(tl, 0, sizeof(*tl));
  679. INIT_LIST_HEAD(head);
  680. list_for_each_entry(t, &ce->tlist, list) {
  681. it = kzalloc(sizeof(*it), GFP_ATOMIC);
  682. if (!it) {
  683. rc = -ENOMEM;
  684. goto err_free_it;
  685. }
  686. it->it_name = kstrndup(t->name, strlen(t->name), GFP_ATOMIC);
  687. if (!it->it_name) {
  688. kfree(it);
  689. rc = -ENOMEM;
  690. goto err_free_it;
  691. }
  692. it->it_path_consumed = t->path_consumed;
  693. if (ce->tgthint == t)
  694. list_add(&it->it_list, head);
  695. else
  696. list_add_tail(&it->it_list, head);
  697. }
  698. tl->tl_numtgts = ce->numtgts;
  699. return 0;
  700. err_free_it:
  701. list_for_each_entry_safe(it, nit, head, it_list) {
  702. kfree(it->it_name);
  703. kfree(it);
  704. }
  705. return rc;
  706. }
  707. /**
  708. * dfs_cache_find - find a DFS cache entry
  709. *
  710. * If it doesn't find the cache entry, then it will get a DFS referral
  711. * for @path and create a new entry.
  712. *
  713. * In case the cache entry exists but expired, it will get a DFS referral
  714. * for @path and then update the respective cache entry.
  715. *
  716. * These parameters are passed down to the get_dfs_refer() call if it
  717. * needs to be issued:
  718. * @xid: syscall xid
  719. * @ses: smb session to issue the request on
  720. * @nls_codepage: charset conversion
  721. * @remap: path character remapping type
  722. * @path: path to lookup in DFS referral cache.
  723. *
  724. * @ref: when non-NULL, store single DFS referral result in it.
  725. * @tgt_list: when non-NULL, store complete DFS target list in it.
  726. *
  727. * Return zero if the target was found, otherwise non-zero.
  728. */
  729. int dfs_cache_find(const unsigned int xid, struct cifs_ses *ses,
  730. const struct nls_table *nls_codepage, int remap,
  731. const char *path, struct dfs_info3_param *ref,
  732. struct dfs_cache_tgt_list *tgt_list)
  733. {
  734. int rc;
  735. char *npath;
  736. struct cache_entry *ce;
  737. rc = get_normalized_path(path, &npath);
  738. if (rc)
  739. return rc;
  740. rc = __dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
  741. if (rc)
  742. goto out_free_path;
  743. down_read(&htable_rw_lock);
  744. ce = lookup_cache_entry(npath, NULL);
  745. if (IS_ERR(ce)) {
  746. up_read(&htable_rw_lock);
  747. rc = PTR_ERR(ce);
  748. goto out_free_path;
  749. }
  750. if (ref)
  751. rc = setup_referral(path, ce, ref, get_tgt_name(ce));
  752. else
  753. rc = 0;
  754. if (!rc && tgt_list)
  755. rc = get_targets(ce, tgt_list);
  756. up_read(&htable_rw_lock);
  757. out_free_path:
  758. free_normalized_path(path, npath);
  759. return rc;
  760. }
  761. /**
  762. * dfs_cache_noreq_find - find a DFS cache entry without sending any requests to
  763. * the currently connected server.
  764. *
  765. * NOTE: This function will neither update a cache entry in case it was
  766. * expired, nor create a new cache entry if @path hasn't been found. It heavily
  767. * relies on an existing cache entry.
  768. *
  769. * @path: path to lookup in the DFS referral cache.
  770. * @ref: when non-NULL, store single DFS referral result in it.
  771. * @tgt_list: when non-NULL, store complete DFS target list in it.
  772. *
  773. * Return 0 if successful.
  774. * Return -ENOENT if the entry was not found.
  775. * Return non-zero for other errors.
  776. */
  777. int dfs_cache_noreq_find(const char *path, struct dfs_info3_param *ref,
  778. struct dfs_cache_tgt_list *tgt_list)
  779. {
  780. int rc;
  781. char *npath;
  782. struct cache_entry *ce;
  783. rc = get_normalized_path(path, &npath);
  784. if (rc)
  785. return rc;
  786. cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
  787. down_read(&htable_rw_lock);
  788. ce = lookup_cache_entry(npath, NULL);
  789. if (IS_ERR(ce)) {
  790. rc = PTR_ERR(ce);
  791. goto out_unlock;
  792. }
  793. if (ref)
  794. rc = setup_referral(path, ce, ref, get_tgt_name(ce));
  795. else
  796. rc = 0;
  797. if (!rc && tgt_list)
  798. rc = get_targets(ce, tgt_list);
  799. out_unlock:
  800. up_read(&htable_rw_lock);
  801. free_normalized_path(path, npath);
  802. return rc;
  803. }
  804. /**
  805. * dfs_cache_update_tgthint - update target hint of a DFS cache entry
  806. *
  807. * If it doesn't find the cache entry, then it will get a DFS referral for @path
  808. * and create a new entry.
  809. *
  810. * In case the cache entry exists but expired, it will get a DFS referral
  811. * for @path and then update the respective cache entry.
  812. *
  813. * @xid: syscall id
  814. * @ses: smb session
  815. * @nls_codepage: charset conversion
  816. * @remap: type of character remapping for paths
  817. * @path: path to lookup in DFS referral cache.
  818. * @it: DFS target iterator
  819. *
  820. * Return zero if the target hint was updated successfully, otherwise non-zero.
  821. */
  822. int dfs_cache_update_tgthint(const unsigned int xid, struct cifs_ses *ses,
  823. const struct nls_table *nls_codepage, int remap,
  824. const char *path,
  825. const struct dfs_cache_tgt_iterator *it)
  826. {
  827. int rc;
  828. char *npath;
  829. struct cache_entry *ce;
  830. struct cache_dfs_tgt *t;
  831. rc = get_normalized_path(path, &npath);
  832. if (rc)
  833. return rc;
  834. cifs_dbg(FYI, "%s: update target hint - path: %s\n", __func__, npath);
  835. rc = __dfs_cache_find(xid, ses, nls_codepage, remap, npath, false);
  836. if (rc)
  837. goto out_free_path;
  838. down_write(&htable_rw_lock);
  839. ce = lookup_cache_entry(npath, NULL);
  840. if (IS_ERR(ce)) {
  841. rc = PTR_ERR(ce);
  842. goto out_unlock;
  843. }
  844. t = ce->tgthint;
  845. if (likely(!strcasecmp(it->it_name, t->name)))
  846. goto out_unlock;
  847. list_for_each_entry(t, &ce->tlist, list) {
  848. if (!strcasecmp(t->name, it->it_name)) {
  849. ce->tgthint = t;
  850. cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
  851. it->it_name);
  852. break;
  853. }
  854. }
  855. out_unlock:
  856. up_write(&htable_rw_lock);
  857. out_free_path:
  858. free_normalized_path(path, npath);
  859. return rc;
  860. }
  861. /**
  862. * dfs_cache_noreq_update_tgthint - update target hint of a DFS cache entry
  863. * without sending any requests to the currently connected server.
  864. *
  865. * NOTE: This function will neither update a cache entry in case it was
  866. * expired, nor create a new cache entry if @path hasn't been found. It heavily
  867. * relies on an existing cache entry.
  868. *
  869. * @path: path to lookup in DFS referral cache.
  870. * @it: target iterator which contains the target hint to update the cache
  871. * entry with.
  872. *
  873. * Return zero if the target hint was updated successfully, otherwise non-zero.
  874. */
  875. int dfs_cache_noreq_update_tgthint(const char *path,
  876. const struct dfs_cache_tgt_iterator *it)
  877. {
  878. int rc;
  879. char *npath;
  880. struct cache_entry *ce;
  881. struct cache_dfs_tgt *t;
  882. if (!it)
  883. return -EINVAL;
  884. rc = get_normalized_path(path, &npath);
  885. if (rc)
  886. return rc;
  887. cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
  888. down_write(&htable_rw_lock);
  889. ce = lookup_cache_entry(npath, NULL);
  890. if (IS_ERR(ce)) {
  891. rc = PTR_ERR(ce);
  892. goto out_unlock;
  893. }
  894. rc = 0;
  895. t = ce->tgthint;
  896. if (unlikely(!strcasecmp(it->it_name, t->name)))
  897. goto out_unlock;
  898. list_for_each_entry(t, &ce->tlist, list) {
  899. if (!strcasecmp(t->name, it->it_name)) {
  900. ce->tgthint = t;
  901. cifs_dbg(FYI, "%s: new target hint: %s\n", __func__,
  902. it->it_name);
  903. break;
  904. }
  905. }
  906. out_unlock:
  907. up_write(&htable_rw_lock);
  908. free_normalized_path(path, npath);
  909. return rc;
  910. }
  911. /**
  912. * dfs_cache_get_tgt_referral - returns a DFS referral (@ref) from a given
  913. * target iterator (@it).
  914. *
  915. * @path: path to lookup in DFS referral cache.
  916. * @it: DFS target iterator.
  917. * @ref: DFS referral pointer to set up the gathered information.
  918. *
  919. * Return zero if the DFS referral was set up correctly, otherwise non-zero.
  920. */
  921. int dfs_cache_get_tgt_referral(const char *path,
  922. const struct dfs_cache_tgt_iterator *it,
  923. struct dfs_info3_param *ref)
  924. {
  925. int rc;
  926. char *npath;
  927. struct cache_entry *ce;
  928. if (!it || !ref)
  929. return -EINVAL;
  930. rc = get_normalized_path(path, &npath);
  931. if (rc)
  932. return rc;
  933. cifs_dbg(FYI, "%s: path: %s\n", __func__, npath);
  934. down_read(&htable_rw_lock);
  935. ce = lookup_cache_entry(npath, NULL);
  936. if (IS_ERR(ce)) {
  937. rc = PTR_ERR(ce);
  938. goto out_unlock;
  939. }
  940. cifs_dbg(FYI, "%s: target name: %s\n", __func__, it->it_name);
  941. rc = setup_referral(path, ce, ref, it->it_name);
  942. out_unlock:
  943. up_read(&htable_rw_lock);
  944. free_normalized_path(path, npath);
  945. return rc;
  946. }
  947. static int dup_vol(struct smb_vol *vol, struct smb_vol *new)
  948. {
  949. memcpy(new, vol, sizeof(*new));
  950. if (vol->username) {
  951. new->username = kstrndup(vol->username, strlen(vol->username),
  952. GFP_KERNEL);
  953. if (!new->username)
  954. return -ENOMEM;
  955. }
  956. if (vol->password) {
  957. new->password = kstrndup(vol->password, strlen(vol->password),
  958. GFP_KERNEL);
  959. if (!new->password)
  960. goto err_free_username;
  961. }
  962. if (vol->UNC) {
  963. cifs_dbg(FYI, "%s: vol->UNC: %s\n", __func__, vol->UNC);
  964. new->UNC = kstrndup(vol->UNC, strlen(vol->UNC), GFP_KERNEL);
  965. if (!new->UNC)
  966. goto err_free_password;
  967. }
  968. if (vol->domainname) {
  969. new->domainname = kstrndup(vol->domainname,
  970. strlen(vol->domainname), GFP_KERNEL);
  971. if (!new->domainname)
  972. goto err_free_unc;
  973. }
  974. if (vol->iocharset) {
  975. new->iocharset = kstrndup(vol->iocharset,
  976. strlen(vol->iocharset), GFP_KERNEL);
  977. if (!new->iocharset)
  978. goto err_free_domainname;
  979. }
  980. if (vol->prepath) {
  981. cifs_dbg(FYI, "%s: vol->prepath: %s\n", __func__, vol->prepath);
  982. new->prepath = kstrndup(vol->prepath, strlen(vol->prepath),
  983. GFP_KERNEL);
  984. if (!new->prepath)
  985. goto err_free_iocharset;
  986. }
  987. return 0;
  988. err_free_iocharset:
  989. kfree(new->iocharset);
  990. err_free_domainname:
  991. kfree(new->domainname);
  992. err_free_unc:
  993. kfree(new->UNC);
  994. err_free_password:
  995. kfree_sensitive(new->password);
  996. err_free_username:
  997. kfree(new->username);
  998. kfree(new);
  999. return -ENOMEM;
  1000. }
  1001. /**
  1002. * dfs_cache_add_vol - add a cifs volume during mount() that will be handled by
  1003. * DFS cache refresh worker.
  1004. *
  1005. * @mntdata: mount data.
  1006. * @vol: cifs volume.
  1007. * @fullpath: origin full path.
  1008. *
  1009. * Return zero if volume was set up correctly, otherwise non-zero.
  1010. */
  1011. int dfs_cache_add_vol(char *mntdata, struct smb_vol *vol, const char *fullpath)
  1012. {
  1013. int rc;
  1014. struct vol_info *vi;
  1015. if (!vol || !fullpath || !mntdata)
  1016. return -EINVAL;
  1017. cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
  1018. vi = kzalloc(sizeof(*vi), GFP_KERNEL);
  1019. if (!vi)
  1020. return -ENOMEM;
  1021. vi->fullpath = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL);
  1022. if (!vi->fullpath) {
  1023. rc = -ENOMEM;
  1024. goto err_free_vi;
  1025. }
  1026. rc = dup_vol(vol, &vi->smb_vol);
  1027. if (rc)
  1028. goto err_free_fullpath;
  1029. vi->mntdata = mntdata;
  1030. spin_lock_init(&vi->smb_vol_lock);
  1031. kref_init(&vi->refcnt);
  1032. spin_lock(&vol_list_lock);
  1033. list_add_tail(&vi->list, &vol_list);
  1034. spin_unlock(&vol_list_lock);
  1035. return 0;
  1036. err_free_fullpath:
  1037. kfree(vi->fullpath);
  1038. err_free_vi:
  1039. kfree(vi);
  1040. return rc;
  1041. }
  1042. /* Must be called with vol_list_lock held */
  1043. static struct vol_info *find_vol(const char *fullpath)
  1044. {
  1045. struct vol_info *vi;
  1046. list_for_each_entry(vi, &vol_list, list) {
  1047. cifs_dbg(FYI, "%s: vi->fullpath: %s\n", __func__, vi->fullpath);
  1048. if (!strcasecmp(vi->fullpath, fullpath))
  1049. return vi;
  1050. }
  1051. return ERR_PTR(-ENOENT);
  1052. }
  1053. /**
  1054. * dfs_cache_update_vol - update vol info in DFS cache after failover
  1055. *
  1056. * @fullpath: fullpath to look up in volume list.
  1057. * @server: TCP ses pointer.
  1058. *
  1059. * Return zero if volume was updated, otherwise non-zero.
  1060. */
  1061. int dfs_cache_update_vol(const char *fullpath, struct TCP_Server_Info *server)
  1062. {
  1063. struct vol_info *vi;
  1064. if (!fullpath || !server)
  1065. return -EINVAL;
  1066. cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
  1067. spin_lock(&vol_list_lock);
  1068. vi = find_vol(fullpath);
  1069. if (IS_ERR(vi)) {
  1070. spin_unlock(&vol_list_lock);
  1071. return PTR_ERR(vi);
  1072. }
  1073. kref_get(&vi->refcnt);
  1074. spin_unlock(&vol_list_lock);
  1075. cifs_dbg(FYI, "%s: updating volume info\n", __func__);
  1076. spin_lock(&vi->smb_vol_lock);
  1077. memcpy(&vi->smb_vol.dstaddr, &server->dstaddr,
  1078. sizeof(vi->smb_vol.dstaddr));
  1079. spin_unlock(&vi->smb_vol_lock);
  1080. kref_put(&vi->refcnt, vol_release);
  1081. return 0;
  1082. }
  1083. /**
  1084. * dfs_cache_del_vol - remove volume info in DFS cache during umount()
  1085. *
  1086. * @fullpath: fullpath to look up in volume list.
  1087. */
  1088. void dfs_cache_del_vol(const char *fullpath)
  1089. {
  1090. struct vol_info *vi;
  1091. if (!fullpath || !*fullpath)
  1092. return;
  1093. cifs_dbg(FYI, "%s: fullpath: %s\n", __func__, fullpath);
  1094. spin_lock(&vol_list_lock);
  1095. vi = find_vol(fullpath);
  1096. spin_unlock(&vol_list_lock);
  1097. if (!IS_ERR(vi))
  1098. kref_put(&vi->refcnt, vol_release);
  1099. }
  1100. /**
  1101. * dfs_cache_get_tgt_share - parse a DFS target
  1102. *
  1103. * @path: DFS full path
  1104. * @it: DFS target iterator.
  1105. * @share: tree name.
  1106. * @prefix: prefix path.
  1107. *
  1108. * Return zero if target was parsed correctly, otherwise non-zero.
  1109. */
  1110. int dfs_cache_get_tgt_share(char *path, const struct dfs_cache_tgt_iterator *it,
  1111. char **share, char **prefix)
  1112. {
  1113. char *s, sep, *p;
  1114. size_t len;
  1115. size_t plen1, plen2;
  1116. if (!it || !path || !share || !prefix || strlen(path) < it->it_path_consumed)
  1117. return -EINVAL;
  1118. *share = NULL;
  1119. *prefix = NULL;
  1120. sep = it->it_name[0];
  1121. if (sep != '\\' && sep != '/')
  1122. return -EINVAL;
  1123. s = strchr(it->it_name + 1, sep);
  1124. if (!s)
  1125. return -EINVAL;
  1126. /* point to prefix in target node */
  1127. s = strchrnul(s + 1, sep);
  1128. /* extract target share */
  1129. *share = kstrndup(it->it_name, s - it->it_name, GFP_KERNEL);
  1130. if (!*share)
  1131. return -ENOMEM;
  1132. /* skip separator */
  1133. if (*s)
  1134. s++;
  1135. /* point to prefix in DFS path */
  1136. p = path + it->it_path_consumed;
  1137. if (*p == sep)
  1138. p++;
  1139. /* merge prefix paths from DFS path and target node */
  1140. plen1 = it->it_name + strlen(it->it_name) - s;
  1141. plen2 = path + strlen(path) - p;
  1142. if (plen1 || plen2) {
  1143. len = plen1 + plen2 + 2;
  1144. *prefix = kmalloc(len, GFP_KERNEL);
  1145. if (!*prefix) {
  1146. kfree(*share);
  1147. *share = NULL;
  1148. return -ENOMEM;
  1149. }
  1150. if (plen1)
  1151. scnprintf(*prefix, len, "%.*s%c%.*s", (int)plen1, s, sep, (int)plen2, p);
  1152. else
  1153. strscpy(*prefix, p, len);
  1154. }
  1155. return 0;
  1156. }
  1157. /* Get all tcons that are within a DFS namespace and can be refreshed */
  1158. static void get_tcons(struct TCP_Server_Info *server, struct list_head *head)
  1159. {
  1160. struct cifs_ses *ses;
  1161. struct cifs_tcon *tcon;
  1162. INIT_LIST_HEAD(head);
  1163. spin_lock(&cifs_tcp_ses_lock);
  1164. list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
  1165. list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
  1166. if (!tcon->need_reconnect && !tcon->need_reopen_files &&
  1167. tcon->dfs_path) {
  1168. tcon->tc_count++;
  1169. list_add_tail(&tcon->ulist, head);
  1170. }
  1171. }
  1172. if (ses->tcon_ipc && !ses->tcon_ipc->need_reconnect &&
  1173. ses->tcon_ipc->dfs_path) {
  1174. list_add_tail(&ses->tcon_ipc->ulist, head);
  1175. }
  1176. }
  1177. spin_unlock(&cifs_tcp_ses_lock);
  1178. }
  1179. static bool is_dfs_link(const char *path)
  1180. {
  1181. char *s;
  1182. s = strchr(path + 1, '\\');
  1183. if (!s)
  1184. return false;
  1185. return !!strchr(s + 1, '\\');
  1186. }
  1187. static char *get_dfs_root(const char *path)
  1188. {
  1189. char *s, *npath;
  1190. s = strchr(path + 1, '\\');
  1191. if (!s)
  1192. return ERR_PTR(-EINVAL);
  1193. s = strchr(s + 1, '\\');
  1194. if (!s)
  1195. return ERR_PTR(-EINVAL);
  1196. npath = kstrndup(path, s - path, GFP_KERNEL);
  1197. if (!npath)
  1198. return ERR_PTR(-ENOMEM);
  1199. return npath;
  1200. }
  1201. static inline void put_tcp_server(struct TCP_Server_Info *server)
  1202. {
  1203. cifs_put_tcp_session(server, 0);
  1204. }
  1205. static struct TCP_Server_Info *get_tcp_server(struct smb_vol *vol)
  1206. {
  1207. struct TCP_Server_Info *server;
  1208. server = cifs_find_tcp_session(vol);
  1209. if (IS_ERR_OR_NULL(server))
  1210. return NULL;
  1211. spin_lock(&GlobalMid_Lock);
  1212. if (server->tcpStatus != CifsGood) {
  1213. spin_unlock(&GlobalMid_Lock);
  1214. put_tcp_server(server);
  1215. return NULL;
  1216. }
  1217. spin_unlock(&GlobalMid_Lock);
  1218. return server;
  1219. }
  1220. /* Find root SMB session out of a DFS link path */
  1221. static struct cifs_ses *find_root_ses(struct vol_info *vi,
  1222. struct cifs_tcon *tcon,
  1223. const char *path)
  1224. {
  1225. char *rpath;
  1226. int rc;
  1227. struct cache_entry *ce;
  1228. struct dfs_info3_param ref = {0};
  1229. char *mdata = NULL, *devname = NULL;
  1230. struct TCP_Server_Info *server;
  1231. struct cifs_ses *ses;
  1232. struct smb_vol vol = {NULL};
  1233. rpath = get_dfs_root(path);
  1234. if (IS_ERR(rpath))
  1235. return ERR_CAST(rpath);
  1236. down_read(&htable_rw_lock);
  1237. ce = lookup_cache_entry(rpath, NULL);
  1238. if (IS_ERR(ce)) {
  1239. up_read(&htable_rw_lock);
  1240. ses = ERR_CAST(ce);
  1241. goto out;
  1242. }
  1243. rc = setup_referral(path, ce, &ref, get_tgt_name(ce));
  1244. if (rc) {
  1245. up_read(&htable_rw_lock);
  1246. ses = ERR_PTR(rc);
  1247. goto out;
  1248. }
  1249. up_read(&htable_rw_lock);
  1250. mdata = cifs_compose_mount_options(vi->mntdata, rpath, &ref,
  1251. &devname);
  1252. free_dfs_info_param(&ref);
  1253. if (IS_ERR(mdata)) {
  1254. ses = ERR_CAST(mdata);
  1255. mdata = NULL;
  1256. goto out;
  1257. }
  1258. rc = cifs_setup_volume_info(&vol, mdata, devname, false);
  1259. kfree(devname);
  1260. if (rc) {
  1261. ses = ERR_PTR(rc);
  1262. goto out;
  1263. }
  1264. server = get_tcp_server(&vol);
  1265. if (!server) {
  1266. ses = ERR_PTR(-EHOSTDOWN);
  1267. goto out;
  1268. }
  1269. ses = cifs_get_smb_ses(server, &vol);
  1270. out:
  1271. cifs_cleanup_volume_info_contents(&vol);
  1272. kfree(mdata);
  1273. kfree(rpath);
  1274. return ses;
  1275. }
  1276. /* Refresh DFS cache entry from a given tcon */
  1277. static int refresh_tcon(struct vol_info *vi, struct cifs_tcon *tcon)
  1278. {
  1279. int rc = 0;
  1280. unsigned int xid;
  1281. char *path, *npath;
  1282. struct cache_entry *ce;
  1283. struct cifs_ses *root_ses = NULL, *ses;
  1284. struct dfs_info3_param *refs = NULL;
  1285. int numrefs = 0;
  1286. xid = get_xid();
  1287. path = tcon->dfs_path + 1;
  1288. rc = get_normalized_path(path, &npath);
  1289. if (rc)
  1290. goto out_free_xid;
  1291. down_read(&htable_rw_lock);
  1292. ce = lookup_cache_entry(npath, NULL);
  1293. if (IS_ERR(ce)) {
  1294. rc = PTR_ERR(ce);
  1295. up_read(&htable_rw_lock);
  1296. goto out_free_path;
  1297. }
  1298. if (!cache_entry_expired(ce)) {
  1299. up_read(&htable_rw_lock);
  1300. goto out_free_path;
  1301. }
  1302. up_read(&htable_rw_lock);
  1303. /* If it's a DFS Link, then use root SMB session for refreshing it */
  1304. if (is_dfs_link(npath)) {
  1305. ses = root_ses = find_root_ses(vi, tcon, npath);
  1306. if (IS_ERR(ses)) {
  1307. rc = PTR_ERR(ses);
  1308. root_ses = NULL;
  1309. goto out_free_path;
  1310. }
  1311. } else {
  1312. ses = tcon->ses;
  1313. }
  1314. rc = get_dfs_referral(xid, ses, cache_nlsc, tcon->remap, npath, &refs,
  1315. &numrefs);
  1316. if (!rc) {
  1317. dump_refs(refs, numrefs);
  1318. rc = update_cache_entry(npath, refs, numrefs);
  1319. free_dfs_info_array(refs, numrefs);
  1320. }
  1321. if (root_ses)
  1322. cifs_put_smb_ses(root_ses);
  1323. out_free_path:
  1324. free_normalized_path(path, npath);
  1325. out_free_xid:
  1326. free_xid(xid);
  1327. return rc;
  1328. }
  1329. /*
  1330. * Worker that will refresh DFS cache based on lowest TTL value from a DFS
  1331. * referral.
  1332. */
  1333. static void refresh_cache_worker(struct work_struct *work)
  1334. {
  1335. struct vol_info *vi, *nvi;
  1336. struct TCP_Server_Info *server;
  1337. LIST_HEAD(vols);
  1338. LIST_HEAD(tcons);
  1339. struct cifs_tcon *tcon, *ntcon;
  1340. int rc;
  1341. /*
  1342. * Find SMB volumes that are eligible (server->tcpStatus == CifsGood)
  1343. * for refreshing.
  1344. */
  1345. spin_lock(&vol_list_lock);
  1346. list_for_each_entry(vi, &vol_list, list) {
  1347. server = get_tcp_server(&vi->smb_vol);
  1348. if (!server)
  1349. continue;
  1350. kref_get(&vi->refcnt);
  1351. list_add_tail(&vi->rlist, &vols);
  1352. put_tcp_server(server);
  1353. }
  1354. spin_unlock(&vol_list_lock);
  1355. /* Walk through all TCONs and refresh any expired cache entry */
  1356. list_for_each_entry_safe(vi, nvi, &vols, rlist) {
  1357. spin_lock(&vi->smb_vol_lock);
  1358. server = get_tcp_server(&vi->smb_vol);
  1359. spin_unlock(&vi->smb_vol_lock);
  1360. if (!server)
  1361. goto next_vol;
  1362. get_tcons(server, &tcons);
  1363. rc = 0;
  1364. list_for_each_entry_safe(tcon, ntcon, &tcons, ulist) {
  1365. /*
  1366. * Skip tcp server if any of its tcons failed to refresh
  1367. * (possibily due to reconnects).
  1368. */
  1369. if (!rc)
  1370. rc = refresh_tcon(vi, tcon);
  1371. list_del_init(&tcon->ulist);
  1372. cifs_put_tcon(tcon);
  1373. }
  1374. put_tcp_server(server);
  1375. next_vol:
  1376. list_del_init(&vi->rlist);
  1377. kref_put(&vi->refcnt, vol_release);
  1378. }
  1379. spin_lock(&cache_ttl_lock);
  1380. queue_delayed_work(dfscache_wq, &refresh_task, cache_ttl * HZ);
  1381. spin_unlock(&cache_ttl_lock);
  1382. }