target_core_device.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*******************************************************************************
  3. * Filename: target_core_device.c (based on iscsi_target_device.c)
  4. *
  5. * This file contains the TCM Virtual Device and Disk Transport
  6. * agnostic related functions.
  7. *
  8. * (c) Copyright 2003-2013 Datera, Inc.
  9. *
  10. * Nicholas A. Bellinger <nab@kernel.org>
  11. *
  12. ******************************************************************************/
  13. #include <linux/net.h>
  14. #include <linux/string.h>
  15. #include <linux/delay.h>
  16. #include <linux/timer.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/kthread.h>
  20. #include <linux/in.h>
  21. #include <linux/export.h>
  22. #include <linux/t10-pi.h>
  23. #include <linux/unaligned.h>
  24. #include <net/sock.h>
  25. #include <net/tcp.h>
  26. #include <scsi/scsi_common.h>
  27. #include <scsi/scsi_proto.h>
  28. #include <target/target_core_base.h>
  29. #include <target/target_core_backend.h>
  30. #include <target/target_core_fabric.h>
  31. #include "target_core_internal.h"
  32. #include "target_core_alua.h"
  33. #include "target_core_pr.h"
  34. #include "target_core_ua.h"
  35. static DEFINE_MUTEX(device_mutex);
  36. static DEFINE_IDR(devices_idr);
  37. static struct se_hba *lun0_hba;
  38. /* not static, needed by tpg.c */
  39. struct se_device *g_lun0_dev;
  40. sense_reason_t
  41. transport_lookup_cmd_lun(struct se_cmd *se_cmd)
  42. {
  43. struct se_lun *se_lun = NULL;
  44. struct se_session *se_sess = se_cmd->se_sess;
  45. struct se_node_acl *nacl = se_sess->se_node_acl;
  46. struct se_dev_entry *deve;
  47. sense_reason_t ret = TCM_NO_SENSE;
  48. rcu_read_lock();
  49. deve = target_nacl_find_deve(nacl, se_cmd->orig_fe_lun);
  50. if (deve) {
  51. atomic_long_inc(&deve->total_cmds);
  52. if (se_cmd->data_direction == DMA_TO_DEVICE)
  53. atomic_long_add(se_cmd->data_length,
  54. &deve->write_bytes);
  55. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  56. atomic_long_add(se_cmd->data_length,
  57. &deve->read_bytes);
  58. if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
  59. deve->lun_access_ro) {
  60. pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
  61. " Access for 0x%08llx\n",
  62. se_cmd->se_tfo->fabric_name,
  63. se_cmd->orig_fe_lun);
  64. rcu_read_unlock();
  65. return TCM_WRITE_PROTECTED;
  66. }
  67. se_lun = deve->se_lun;
  68. if (!percpu_ref_tryget_live(&se_lun->lun_ref)) {
  69. se_lun = NULL;
  70. goto out_unlock;
  71. }
  72. se_cmd->se_lun = se_lun;
  73. se_cmd->pr_res_key = deve->pr_res_key;
  74. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  75. se_cmd->lun_ref_active = true;
  76. }
  77. out_unlock:
  78. rcu_read_unlock();
  79. if (!se_lun) {
  80. /*
  81. * Use the se_portal_group->tpg_virt_lun0 to allow for
  82. * REPORT_LUNS, et al to be returned when no active
  83. * MappedLUN=0 exists for this Initiator Port.
  84. */
  85. if (se_cmd->orig_fe_lun != 0) {
  86. pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  87. " Access for 0x%08llx from %s\n",
  88. se_cmd->se_tfo->fabric_name,
  89. se_cmd->orig_fe_lun,
  90. nacl->initiatorname);
  91. return TCM_NON_EXISTENT_LUN;
  92. }
  93. /*
  94. * Force WRITE PROTECT for virtual LUN 0
  95. */
  96. if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
  97. (se_cmd->data_direction != DMA_NONE))
  98. return TCM_WRITE_PROTECTED;
  99. se_lun = se_sess->se_tpg->tpg_virt_lun0;
  100. if (!percpu_ref_tryget_live(&se_lun->lun_ref))
  101. return TCM_NON_EXISTENT_LUN;
  102. se_cmd->se_lun = se_sess->se_tpg->tpg_virt_lun0;
  103. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  104. se_cmd->lun_ref_active = true;
  105. }
  106. /*
  107. * RCU reference protected by percpu se_lun->lun_ref taken above that
  108. * must drop to zero (including initial reference) before this se_lun
  109. * pointer can be kfree_rcu() by the final se_lun->lun_group put via
  110. * target_core_fabric_configfs.c:target_fabric_port_release
  111. */
  112. se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  113. atomic_long_inc(&se_cmd->se_dev->num_cmds);
  114. if (se_cmd->data_direction == DMA_TO_DEVICE)
  115. atomic_long_add(se_cmd->data_length,
  116. &se_cmd->se_dev->write_bytes);
  117. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  118. atomic_long_add(se_cmd->data_length,
  119. &se_cmd->se_dev->read_bytes);
  120. return ret;
  121. }
  122. EXPORT_SYMBOL(transport_lookup_cmd_lun);
  123. int transport_lookup_tmr_lun(struct se_cmd *se_cmd)
  124. {
  125. struct se_dev_entry *deve;
  126. struct se_lun *se_lun = NULL;
  127. struct se_session *se_sess = se_cmd->se_sess;
  128. struct se_node_acl *nacl = se_sess->se_node_acl;
  129. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  130. rcu_read_lock();
  131. deve = target_nacl_find_deve(nacl, se_cmd->orig_fe_lun);
  132. if (deve) {
  133. se_lun = deve->se_lun;
  134. if (!percpu_ref_tryget_live(&se_lun->lun_ref)) {
  135. se_lun = NULL;
  136. goto out_unlock;
  137. }
  138. se_cmd->se_lun = se_lun;
  139. se_cmd->pr_res_key = deve->pr_res_key;
  140. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  141. se_cmd->lun_ref_active = true;
  142. }
  143. out_unlock:
  144. rcu_read_unlock();
  145. if (!se_lun) {
  146. pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  147. " Access for 0x%08llx for %s\n",
  148. se_cmd->se_tfo->fabric_name,
  149. se_cmd->orig_fe_lun,
  150. nacl->initiatorname);
  151. return -ENODEV;
  152. }
  153. se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  154. se_tmr->tmr_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  155. return 0;
  156. }
  157. EXPORT_SYMBOL(transport_lookup_tmr_lun);
  158. bool target_lun_is_rdonly(struct se_cmd *cmd)
  159. {
  160. struct se_session *se_sess = cmd->se_sess;
  161. struct se_dev_entry *deve;
  162. bool ret;
  163. rcu_read_lock();
  164. deve = target_nacl_find_deve(se_sess->se_node_acl, cmd->orig_fe_lun);
  165. ret = deve && deve->lun_access_ro;
  166. rcu_read_unlock();
  167. return ret;
  168. }
  169. EXPORT_SYMBOL(target_lun_is_rdonly);
  170. /*
  171. * This function is called from core_scsi3_emulate_pro_register_and_move()
  172. * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_kref
  173. * when a matching rtpi is found.
  174. */
  175. struct se_dev_entry *core_get_se_deve_from_rtpi(
  176. struct se_node_acl *nacl,
  177. u16 rtpi)
  178. {
  179. struct se_dev_entry *deve;
  180. struct se_lun *lun;
  181. struct se_portal_group *tpg = nacl->se_tpg;
  182. rcu_read_lock();
  183. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  184. lun = deve->se_lun;
  185. if (!lun) {
  186. pr_err("%s device entries device pointer is"
  187. " NULL, but Initiator has access.\n",
  188. tpg->se_tpg_tfo->fabric_name);
  189. continue;
  190. }
  191. if (lun->lun_tpg->tpg_rtpi != rtpi)
  192. continue;
  193. kref_get(&deve->pr_kref);
  194. rcu_read_unlock();
  195. return deve;
  196. }
  197. rcu_read_unlock();
  198. return NULL;
  199. }
  200. void core_free_device_list_for_node(
  201. struct se_node_acl *nacl,
  202. struct se_portal_group *tpg)
  203. {
  204. struct se_dev_entry *deve;
  205. mutex_lock(&nacl->lun_entry_mutex);
  206. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
  207. core_disable_device_list_for_node(deve->se_lun, deve, nacl, tpg);
  208. mutex_unlock(&nacl->lun_entry_mutex);
  209. }
  210. void core_update_device_list_access(
  211. u64 mapped_lun,
  212. bool lun_access_ro,
  213. struct se_node_acl *nacl)
  214. {
  215. struct se_dev_entry *deve;
  216. mutex_lock(&nacl->lun_entry_mutex);
  217. deve = target_nacl_find_deve(nacl, mapped_lun);
  218. if (deve)
  219. deve->lun_access_ro = lun_access_ro;
  220. mutex_unlock(&nacl->lun_entry_mutex);
  221. }
  222. /*
  223. * Called with rcu_read_lock or nacl->device_list_lock held.
  224. */
  225. struct se_dev_entry *target_nacl_find_deve(struct se_node_acl *nacl, u64 mapped_lun)
  226. {
  227. struct se_dev_entry *deve;
  228. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
  229. if (deve->mapped_lun == mapped_lun)
  230. return deve;
  231. return NULL;
  232. }
  233. EXPORT_SYMBOL(target_nacl_find_deve);
  234. void target_pr_kref_release(struct kref *kref)
  235. {
  236. struct se_dev_entry *deve = container_of(kref, struct se_dev_entry,
  237. pr_kref);
  238. complete(&deve->pr_comp);
  239. }
  240. /*
  241. * Establish UA condition on SCSI device - all LUNs
  242. */
  243. void target_dev_ua_allocate(struct se_device *dev, u8 asc, u8 ascq)
  244. {
  245. struct se_dev_entry *se_deve;
  246. struct se_lun *lun;
  247. spin_lock(&dev->se_port_lock);
  248. list_for_each_entry(lun, &dev->dev_sep_list, lun_dev_link) {
  249. spin_lock(&lun->lun_deve_lock);
  250. list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link)
  251. core_scsi3_ua_allocate(se_deve, asc, ascq);
  252. spin_unlock(&lun->lun_deve_lock);
  253. }
  254. spin_unlock(&dev->se_port_lock);
  255. }
  256. static void
  257. target_luns_data_has_changed(struct se_node_acl *nacl, struct se_dev_entry *new,
  258. bool skip_new)
  259. {
  260. struct se_dev_entry *tmp;
  261. rcu_read_lock();
  262. hlist_for_each_entry_rcu(tmp, &nacl->lun_entry_hlist, link) {
  263. if (skip_new && tmp == new)
  264. continue;
  265. core_scsi3_ua_allocate(tmp, 0x3F,
  266. ASCQ_3FH_REPORTED_LUNS_DATA_HAS_CHANGED);
  267. }
  268. rcu_read_unlock();
  269. }
  270. int core_enable_device_list_for_node(
  271. struct se_lun *lun,
  272. struct se_lun_acl *lun_acl,
  273. u64 mapped_lun,
  274. bool lun_access_ro,
  275. struct se_node_acl *nacl,
  276. struct se_portal_group *tpg)
  277. {
  278. struct se_dev_entry *orig, *new;
  279. new = kzalloc(sizeof(*new), GFP_KERNEL);
  280. if (!new) {
  281. pr_err("Unable to allocate se_dev_entry memory\n");
  282. return -ENOMEM;
  283. }
  284. spin_lock_init(&new->ua_lock);
  285. INIT_LIST_HEAD(&new->ua_list);
  286. INIT_LIST_HEAD(&new->lun_link);
  287. new->mapped_lun = mapped_lun;
  288. kref_init(&new->pr_kref);
  289. init_completion(&new->pr_comp);
  290. new->lun_access_ro = lun_access_ro;
  291. new->creation_time = get_jiffies_64();
  292. new->attach_count++;
  293. mutex_lock(&nacl->lun_entry_mutex);
  294. orig = target_nacl_find_deve(nacl, mapped_lun);
  295. if (orig && orig->se_lun) {
  296. struct se_lun *orig_lun = orig->se_lun;
  297. if (orig_lun != lun) {
  298. pr_err("Existing orig->se_lun doesn't match new lun"
  299. " for dynamic -> explicit NodeACL conversion:"
  300. " %s\n", nacl->initiatorname);
  301. mutex_unlock(&nacl->lun_entry_mutex);
  302. kfree(new);
  303. return -EINVAL;
  304. }
  305. if (orig->se_lun_acl != NULL) {
  306. pr_warn_ratelimited("Detected existing explicit"
  307. " se_lun_acl->se_lun_group reference for %s"
  308. " mapped_lun: %llu, failing\n",
  309. nacl->initiatorname, mapped_lun);
  310. mutex_unlock(&nacl->lun_entry_mutex);
  311. kfree(new);
  312. return -EINVAL;
  313. }
  314. new->se_lun = lun;
  315. new->se_lun_acl = lun_acl;
  316. hlist_del_rcu(&orig->link);
  317. hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
  318. mutex_unlock(&nacl->lun_entry_mutex);
  319. spin_lock(&lun->lun_deve_lock);
  320. list_del(&orig->lun_link);
  321. list_add_tail(&new->lun_link, &lun->lun_deve_list);
  322. spin_unlock(&lun->lun_deve_lock);
  323. kref_put(&orig->pr_kref, target_pr_kref_release);
  324. wait_for_completion(&orig->pr_comp);
  325. target_luns_data_has_changed(nacl, new, true);
  326. kfree_rcu(orig, rcu_head);
  327. return 0;
  328. }
  329. new->se_lun = lun;
  330. new->se_lun_acl = lun_acl;
  331. hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
  332. mutex_unlock(&nacl->lun_entry_mutex);
  333. spin_lock(&lun->lun_deve_lock);
  334. list_add_tail(&new->lun_link, &lun->lun_deve_list);
  335. spin_unlock(&lun->lun_deve_lock);
  336. target_luns_data_has_changed(nacl, new, true);
  337. return 0;
  338. }
  339. void core_disable_device_list_for_node(
  340. struct se_lun *lun,
  341. struct se_dev_entry *orig,
  342. struct se_node_acl *nacl,
  343. struct se_portal_group *tpg)
  344. {
  345. /*
  346. * rcu_dereference_raw protected by se_lun->lun_group symlink
  347. * reference to se_device->dev_group.
  348. */
  349. struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
  350. lockdep_assert_held(&nacl->lun_entry_mutex);
  351. /*
  352. * If the MappedLUN entry is being disabled, the entry in
  353. * lun->lun_deve_list must be removed now before clearing the
  354. * struct se_dev_entry pointers below as logic in
  355. * core_alua_do_transition_tg_pt() depends on these being present.
  356. *
  357. * deve->se_lun_acl will be NULL for demo-mode created LUNs
  358. * that have not been explicitly converted to MappedLUNs ->
  359. * struct se_lun_acl, but we remove deve->lun_link from
  360. * lun->lun_deve_list. This also means that active UAs and
  361. * NodeACL context specific PR metadata for demo-mode
  362. * MappedLUN *deve will be released below..
  363. */
  364. spin_lock(&lun->lun_deve_lock);
  365. list_del(&orig->lun_link);
  366. spin_unlock(&lun->lun_deve_lock);
  367. /*
  368. * Disable struct se_dev_entry LUN ACL mapping
  369. */
  370. core_scsi3_ua_release_all(orig);
  371. hlist_del_rcu(&orig->link);
  372. clear_bit(DEF_PR_REG_ACTIVE, &orig->deve_flags);
  373. orig->lun_access_ro = false;
  374. orig->creation_time = 0;
  375. orig->attach_count--;
  376. /*
  377. * Before firing off RCU callback, wait for any in process SPEC_I_PT=1
  378. * or REGISTER_AND_MOVE PR operation to complete.
  379. */
  380. kref_put(&orig->pr_kref, target_pr_kref_release);
  381. wait_for_completion(&orig->pr_comp);
  382. kfree_rcu(orig, rcu_head);
  383. core_scsi3_free_pr_reg_from_nacl(dev, nacl);
  384. target_luns_data_has_changed(nacl, NULL, false);
  385. }
  386. /* core_clear_lun_from_tpg():
  387. *
  388. *
  389. */
  390. void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
  391. {
  392. struct se_node_acl *nacl;
  393. struct se_dev_entry *deve;
  394. mutex_lock(&tpg->acl_node_mutex);
  395. list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
  396. mutex_lock(&nacl->lun_entry_mutex);
  397. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  398. if (lun != deve->se_lun)
  399. continue;
  400. core_disable_device_list_for_node(lun, deve, nacl, tpg);
  401. }
  402. mutex_unlock(&nacl->lun_entry_mutex);
  403. }
  404. mutex_unlock(&tpg->acl_node_mutex);
  405. }
  406. static void se_release_vpd_for_dev(struct se_device *dev)
  407. {
  408. struct t10_vpd *vpd, *vpd_tmp;
  409. spin_lock(&dev->t10_wwn.t10_vpd_lock);
  410. list_for_each_entry_safe(vpd, vpd_tmp,
  411. &dev->t10_wwn.t10_vpd_list, vpd_list) {
  412. list_del(&vpd->vpd_list);
  413. kfree(vpd);
  414. }
  415. spin_unlock(&dev->t10_wwn.t10_vpd_lock);
  416. }
  417. static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
  418. {
  419. u32 aligned_max_sectors;
  420. u32 alignment;
  421. /*
  422. * Limit max_sectors to a PAGE_SIZE aligned value for modern
  423. * transport_allocate_data_tasks() operation.
  424. */
  425. alignment = max(1ul, PAGE_SIZE / block_size);
  426. aligned_max_sectors = rounddown(max_sectors, alignment);
  427. if (max_sectors != aligned_max_sectors)
  428. pr_info("Rounding down aligned max_sectors from %u to %u\n",
  429. max_sectors, aligned_max_sectors);
  430. return aligned_max_sectors;
  431. }
  432. int core_dev_add_lun(
  433. struct se_portal_group *tpg,
  434. struct se_device *dev,
  435. struct se_lun *lun)
  436. {
  437. int rc;
  438. rc = core_tpg_add_lun(tpg, lun, false, dev);
  439. if (rc < 0)
  440. return rc;
  441. pr_debug("%s_TPG[%u]_LUN[%llu] - Activated %s Logical Unit from"
  442. " CORE HBA: %u\n", tpg->se_tpg_tfo->fabric_name,
  443. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  444. tpg->se_tpg_tfo->fabric_name, dev->se_hba->hba_id);
  445. /*
  446. * Update LUN maps for dynamically added initiators when
  447. * generate_node_acl is enabled.
  448. */
  449. if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
  450. struct se_node_acl *acl;
  451. mutex_lock(&tpg->acl_node_mutex);
  452. list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
  453. if (acl->dynamic_node_acl &&
  454. (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
  455. !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
  456. core_tpg_add_node_to_devs(acl, tpg, lun);
  457. }
  458. }
  459. mutex_unlock(&tpg->acl_node_mutex);
  460. }
  461. return 0;
  462. }
  463. /* core_dev_del_lun():
  464. *
  465. *
  466. */
  467. void core_dev_del_lun(
  468. struct se_portal_group *tpg,
  469. struct se_lun *lun)
  470. {
  471. pr_debug("%s_TPG[%u]_LUN[%llu] - Deactivating %s Logical Unit from"
  472. " device object\n", tpg->se_tpg_tfo->fabric_name,
  473. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  474. tpg->se_tpg_tfo->fabric_name);
  475. core_tpg_remove_lun(tpg, lun);
  476. }
  477. struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
  478. struct se_portal_group *tpg,
  479. struct se_node_acl *nacl,
  480. u64 mapped_lun,
  481. int *ret)
  482. {
  483. struct se_lun_acl *lacl;
  484. if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
  485. pr_err("%s InitiatorName exceeds maximum size.\n",
  486. tpg->se_tpg_tfo->fabric_name);
  487. *ret = -EOVERFLOW;
  488. return NULL;
  489. }
  490. lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
  491. if (!lacl) {
  492. pr_err("Unable to allocate memory for struct se_lun_acl.\n");
  493. *ret = -ENOMEM;
  494. return NULL;
  495. }
  496. lacl->mapped_lun = mapped_lun;
  497. lacl->se_lun_nacl = nacl;
  498. return lacl;
  499. }
  500. int core_dev_add_initiator_node_lun_acl(
  501. struct se_portal_group *tpg,
  502. struct se_lun_acl *lacl,
  503. struct se_lun *lun,
  504. bool lun_access_ro)
  505. {
  506. struct se_node_acl *nacl = lacl->se_lun_nacl;
  507. /*
  508. * rcu_dereference_raw protected by se_lun->lun_group symlink
  509. * reference to se_device->dev_group.
  510. */
  511. struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
  512. if (!nacl)
  513. return -EINVAL;
  514. if (lun->lun_access_ro)
  515. lun_access_ro = true;
  516. lacl->se_lun = lun;
  517. if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
  518. lun_access_ro, nacl, tpg) < 0)
  519. return -EINVAL;
  520. pr_debug("%s_TPG[%hu]_LUN[%llu->%llu] - Added %s ACL for "
  521. " InitiatorNode: %s\n", tpg->se_tpg_tfo->fabric_name,
  522. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun, lacl->mapped_lun,
  523. lun_access_ro ? "RO" : "RW",
  524. nacl->initiatorname);
  525. /*
  526. * Check to see if there are any existing persistent reservation APTPL
  527. * pre-registrations that need to be enabled for this LUN ACL..
  528. */
  529. core_scsi3_check_aptpl_registration(dev, tpg, lun, nacl,
  530. lacl->mapped_lun);
  531. return 0;
  532. }
  533. int core_dev_del_initiator_node_lun_acl(
  534. struct se_lun *lun,
  535. struct se_lun_acl *lacl)
  536. {
  537. struct se_portal_group *tpg = lun->lun_tpg;
  538. struct se_node_acl *nacl;
  539. struct se_dev_entry *deve;
  540. nacl = lacl->se_lun_nacl;
  541. if (!nacl)
  542. return -EINVAL;
  543. mutex_lock(&nacl->lun_entry_mutex);
  544. deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
  545. if (deve)
  546. core_disable_device_list_for_node(lun, deve, nacl, tpg);
  547. mutex_unlock(&nacl->lun_entry_mutex);
  548. pr_debug("%s_TPG[%hu]_LUN[%llu] - Removed ACL for"
  549. " InitiatorNode: %s Mapped LUN: %llu\n",
  550. tpg->se_tpg_tfo->fabric_name,
  551. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  552. nacl->initiatorname, lacl->mapped_lun);
  553. return 0;
  554. }
  555. void core_dev_free_initiator_node_lun_acl(
  556. struct se_portal_group *tpg,
  557. struct se_lun_acl *lacl)
  558. {
  559. pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
  560. " Mapped LUN: %llu\n", tpg->se_tpg_tfo->fabric_name,
  561. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  562. tpg->se_tpg_tfo->fabric_name,
  563. lacl->se_lun_nacl->initiatorname, lacl->mapped_lun);
  564. kfree(lacl);
  565. }
  566. static void scsi_dump_inquiry(struct se_device *dev)
  567. {
  568. struct t10_wwn *wwn = &dev->t10_wwn;
  569. int device_type = dev->transport->get_device_type(dev);
  570. /*
  571. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  572. */
  573. pr_debug(" Vendor: %-" __stringify(INQUIRY_VENDOR_LEN) "s\n",
  574. wwn->vendor);
  575. pr_debug(" Model: %-" __stringify(INQUIRY_MODEL_LEN) "s\n",
  576. wwn->model);
  577. pr_debug(" Revision: %-" __stringify(INQUIRY_REVISION_LEN) "s\n",
  578. wwn->revision);
  579. pr_debug(" Type: %s ", scsi_device_type(device_type));
  580. }
  581. struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
  582. {
  583. struct se_device *dev;
  584. struct se_lun *xcopy_lun;
  585. int i;
  586. dev = hba->backend->ops->alloc_device(hba, name);
  587. if (!dev)
  588. return NULL;
  589. dev->queues = kcalloc(nr_cpu_ids, sizeof(*dev->queues), GFP_KERNEL);
  590. if (!dev->queues) {
  591. hba->backend->ops->free_device(dev);
  592. return NULL;
  593. }
  594. dev->queue_cnt = nr_cpu_ids;
  595. for (i = 0; i < dev->queue_cnt; i++) {
  596. struct se_device_queue *q;
  597. q = &dev->queues[i];
  598. INIT_LIST_HEAD(&q->state_list);
  599. spin_lock_init(&q->lock);
  600. init_llist_head(&q->sq.cmd_list);
  601. INIT_WORK(&q->sq.work, target_queued_submit_work);
  602. }
  603. dev->se_hba = hba;
  604. dev->transport = hba->backend->ops;
  605. dev->transport_flags = dev->transport->transport_flags_default;
  606. dev->prot_length = sizeof(struct t10_pi_tuple);
  607. dev->hba_index = hba->hba_index;
  608. INIT_LIST_HEAD(&dev->dev_sep_list);
  609. INIT_LIST_HEAD(&dev->dev_tmr_list);
  610. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  611. INIT_LIST_HEAD(&dev->qf_cmd_list);
  612. spin_lock_init(&dev->delayed_cmd_lock);
  613. spin_lock_init(&dev->dev_reservation_lock);
  614. spin_lock_init(&dev->se_port_lock);
  615. spin_lock_init(&dev->se_tmr_lock);
  616. spin_lock_init(&dev->qf_cmd_lock);
  617. sema_init(&dev->caw_sem, 1);
  618. INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
  619. spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
  620. INIT_LIST_HEAD(&dev->t10_pr.registration_list);
  621. INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
  622. spin_lock_init(&dev->t10_pr.registration_lock);
  623. spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
  624. INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
  625. spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
  626. INIT_LIST_HEAD(&dev->t10_alua.lba_map_list);
  627. spin_lock_init(&dev->t10_alua.lba_map_lock);
  628. INIT_WORK(&dev->delayed_cmd_work, target_do_delayed_work);
  629. mutex_init(&dev->lun_reset_mutex);
  630. dev->t10_wwn.t10_dev = dev;
  631. /*
  632. * Use OpenFabrics IEEE Company ID: 00 14 05
  633. */
  634. dev->t10_wwn.company_id = 0x001405;
  635. dev->t10_alua.t10_dev = dev;
  636. dev->dev_attrib.da_dev = dev;
  637. dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
  638. dev->dev_attrib.emulate_dpo = 1;
  639. dev->dev_attrib.emulate_fua_write = 1;
  640. dev->dev_attrib.emulate_fua_read = 1;
  641. dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
  642. dev->dev_attrib.emulate_ua_intlck_ctrl = TARGET_UA_INTLCK_CTRL_CLEAR;
  643. dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
  644. dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
  645. dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
  646. dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
  647. dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
  648. dev->dev_attrib.emulate_pr = DA_EMULATE_PR;
  649. dev->dev_attrib.emulate_rsoc = DA_EMULATE_RSOC;
  650. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE0_PROT;
  651. dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
  652. dev->dev_attrib.force_pr_aptpl = DA_FORCE_PR_APTPL;
  653. dev->dev_attrib.is_nonrot = DA_IS_NONROT;
  654. dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
  655. dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
  656. dev->dev_attrib.max_unmap_block_desc_count =
  657. DA_MAX_UNMAP_BLOCK_DESC_COUNT;
  658. dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
  659. dev->dev_attrib.unmap_granularity_alignment =
  660. DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
  661. dev->dev_attrib.unmap_zeroes_data =
  662. DA_UNMAP_ZEROES_DATA_DEFAULT;
  663. dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
  664. dev->dev_attrib.submit_type = TARGET_FABRIC_DEFAULT_SUBMIT;
  665. xcopy_lun = &dev->xcopy_lun;
  666. rcu_assign_pointer(xcopy_lun->lun_se_dev, dev);
  667. init_completion(&xcopy_lun->lun_shutdown_comp);
  668. INIT_LIST_HEAD(&xcopy_lun->lun_deve_list);
  669. INIT_LIST_HEAD(&xcopy_lun->lun_dev_link);
  670. mutex_init(&xcopy_lun->lun_tg_pt_md_mutex);
  671. xcopy_lun->lun_tpg = &xcopy_pt_tpg;
  672. /* Preload the default INQUIRY const values */
  673. strscpy(dev->t10_wwn.vendor, "LIO-ORG", sizeof(dev->t10_wwn.vendor));
  674. strscpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
  675. sizeof(dev->t10_wwn.model));
  676. strscpy(dev->t10_wwn.revision, dev->transport->inquiry_rev,
  677. sizeof(dev->t10_wwn.revision));
  678. return dev;
  679. }
  680. /*
  681. * Check if the underlying struct block_device supports discard and if yes
  682. * configure the UNMAP parameters.
  683. */
  684. bool target_configure_unmap_from_queue(struct se_dev_attrib *attrib,
  685. struct block_device *bdev)
  686. {
  687. int block_size = bdev_logical_block_size(bdev);
  688. if (!bdev_max_discard_sectors(bdev))
  689. return false;
  690. attrib->max_unmap_lba_count =
  691. bdev_max_discard_sectors(bdev) >> (ilog2(block_size) - 9);
  692. /*
  693. * Currently hardcoded to 1 in Linux/SCSI code..
  694. */
  695. attrib->max_unmap_block_desc_count = 1;
  696. attrib->unmap_granularity = bdev_discard_granularity(bdev) / block_size;
  697. attrib->unmap_granularity_alignment =
  698. bdev_discard_alignment(bdev) / block_size;
  699. return true;
  700. }
  701. EXPORT_SYMBOL(target_configure_unmap_from_queue);
  702. /*
  703. * Convert from blocksize advertised to the initiator to the 512 byte
  704. * units unconditionally used by the Linux block layer.
  705. */
  706. sector_t target_to_linux_sector(struct se_device *dev, sector_t lb)
  707. {
  708. switch (dev->dev_attrib.block_size) {
  709. case 4096:
  710. return lb << 3;
  711. case 2048:
  712. return lb << 2;
  713. case 1024:
  714. return lb << 1;
  715. default:
  716. return lb;
  717. }
  718. }
  719. EXPORT_SYMBOL(target_to_linux_sector);
  720. struct devices_idr_iter {
  721. int (*fn)(struct se_device *dev, void *data);
  722. void *data;
  723. };
  724. static int target_devices_idr_iter(int id, void *p, void *data)
  725. __must_hold(&device_mutex)
  726. {
  727. struct devices_idr_iter *iter = data;
  728. struct se_device *dev = p;
  729. struct config_item *item;
  730. int ret;
  731. /*
  732. * We add the device early to the idr, so it can be used
  733. * by backend modules during configuration. We do not want
  734. * to allow other callers to access partially setup devices,
  735. * so we skip them here.
  736. */
  737. if (!target_dev_configured(dev))
  738. return 0;
  739. item = config_item_get_unless_zero(&dev->dev_group.cg_item);
  740. if (!item)
  741. return 0;
  742. mutex_unlock(&device_mutex);
  743. ret = iter->fn(dev, iter->data);
  744. config_item_put(item);
  745. mutex_lock(&device_mutex);
  746. return ret;
  747. }
  748. /**
  749. * target_for_each_device - iterate over configured devices
  750. * @fn: iterator function
  751. * @data: pointer to data that will be passed to fn
  752. *
  753. * fn must return 0 to continue looping over devices. non-zero will break
  754. * from the loop and return that value to the caller.
  755. */
  756. int target_for_each_device(int (*fn)(struct se_device *dev, void *data),
  757. void *data)
  758. {
  759. struct devices_idr_iter iter = { .fn = fn, .data = data };
  760. int ret;
  761. mutex_lock(&device_mutex);
  762. ret = idr_for_each(&devices_idr, target_devices_idr_iter, &iter);
  763. mutex_unlock(&device_mutex);
  764. return ret;
  765. }
  766. int target_configure_device(struct se_device *dev)
  767. {
  768. struct se_hba *hba = dev->se_hba;
  769. int ret, id;
  770. if (target_dev_configured(dev)) {
  771. pr_err("se_dev->se_dev_ptr already set for storage"
  772. " object\n");
  773. return -EEXIST;
  774. }
  775. /*
  776. * Add early so modules like tcmu can use during its
  777. * configuration.
  778. */
  779. mutex_lock(&device_mutex);
  780. /*
  781. * Use cyclic to try and avoid collisions with devices
  782. * that were recently removed.
  783. */
  784. id = idr_alloc_cyclic(&devices_idr, dev, 0, INT_MAX, GFP_KERNEL);
  785. mutex_unlock(&device_mutex);
  786. if (id < 0) {
  787. ret = -ENOMEM;
  788. goto out;
  789. }
  790. dev->dev_index = id;
  791. ret = dev->transport->configure_device(dev);
  792. if (ret)
  793. goto out_free_index;
  794. if (dev->transport->configure_unmap &&
  795. dev->transport->configure_unmap(dev)) {
  796. pr_debug("Discard support available, but disabled by default.\n");
  797. }
  798. /*
  799. * XXX: there is not much point to have two different values here..
  800. */
  801. dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
  802. dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
  803. /*
  804. * Align max_hw_sectors down to PAGE_SIZE I/O transfers
  805. */
  806. dev->dev_attrib.hw_max_sectors =
  807. se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
  808. dev->dev_attrib.hw_block_size);
  809. dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors;
  810. dev->creation_time = get_jiffies_64();
  811. ret = core_setup_alua(dev);
  812. if (ret)
  813. goto out_destroy_device;
  814. /*
  815. * Setup work_queue for QUEUE_FULL
  816. */
  817. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  818. scsi_dump_inquiry(dev);
  819. spin_lock(&hba->device_lock);
  820. hba->dev_count++;
  821. spin_unlock(&hba->device_lock);
  822. dev->dev_flags |= DF_CONFIGURED;
  823. return 0;
  824. out_destroy_device:
  825. dev->transport->destroy_device(dev);
  826. out_free_index:
  827. mutex_lock(&device_mutex);
  828. idr_remove(&devices_idr, dev->dev_index);
  829. mutex_unlock(&device_mutex);
  830. out:
  831. se_release_vpd_for_dev(dev);
  832. return ret;
  833. }
  834. void target_free_device(struct se_device *dev)
  835. {
  836. struct se_hba *hba = dev->se_hba;
  837. WARN_ON(!list_empty(&dev->dev_sep_list));
  838. if (target_dev_configured(dev)) {
  839. dev->transport->destroy_device(dev);
  840. mutex_lock(&device_mutex);
  841. idr_remove(&devices_idr, dev->dev_index);
  842. mutex_unlock(&device_mutex);
  843. spin_lock(&hba->device_lock);
  844. hba->dev_count--;
  845. spin_unlock(&hba->device_lock);
  846. }
  847. core_alua_free_lu_gp_mem(dev);
  848. core_alua_set_lba_map(dev, NULL, 0, 0);
  849. core_scsi3_free_all_registrations(dev);
  850. se_release_vpd_for_dev(dev);
  851. if (dev->transport->free_prot)
  852. dev->transport->free_prot(dev);
  853. kfree(dev->queues);
  854. dev->transport->free_device(dev);
  855. }
  856. int core_dev_setup_virtual_lun0(void)
  857. {
  858. struct se_hba *hba;
  859. struct se_device *dev;
  860. char buf[] = "rd_pages=8,rd_nullio=1,rd_dummy=1";
  861. int ret;
  862. hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
  863. if (IS_ERR(hba))
  864. return PTR_ERR(hba);
  865. dev = target_alloc_device(hba, "virt_lun0");
  866. if (!dev) {
  867. ret = -ENOMEM;
  868. goto out_free_hba;
  869. }
  870. hba->backend->ops->set_configfs_dev_params(dev, buf, sizeof(buf));
  871. ret = target_configure_device(dev);
  872. if (ret)
  873. goto out_free_se_dev;
  874. lun0_hba = hba;
  875. g_lun0_dev = dev;
  876. return 0;
  877. out_free_se_dev:
  878. target_free_device(dev);
  879. out_free_hba:
  880. core_delete_hba(hba);
  881. return ret;
  882. }
  883. void core_dev_release_virtual_lun0(void)
  884. {
  885. struct se_hba *hba = lun0_hba;
  886. if (!hba)
  887. return;
  888. if (g_lun0_dev)
  889. target_free_device(g_lun0_dev);
  890. core_delete_hba(hba);
  891. }
  892. /*
  893. * Common CDB parsing for kernel and user passthrough.
  894. */
  895. sense_reason_t
  896. passthrough_parse_cdb(struct se_cmd *cmd,
  897. sense_reason_t (*exec_cmd)(struct se_cmd *cmd))
  898. {
  899. unsigned char *cdb = cmd->t_task_cdb;
  900. struct se_device *dev = cmd->se_dev;
  901. unsigned int size;
  902. /*
  903. * For REPORT LUNS we always need to emulate the response, for everything
  904. * else, pass it up.
  905. */
  906. if (cdb[0] == REPORT_LUNS) {
  907. cmd->execute_cmd = spc_emulate_report_luns;
  908. return TCM_NO_SENSE;
  909. }
  910. /*
  911. * With emulate_pr disabled, all reservation requests should fail,
  912. * regardless of whether or not TRANSPORT_FLAG_PASSTHROUGH_PGR is set.
  913. */
  914. if (!dev->dev_attrib.emulate_pr &&
  915. ((cdb[0] == PERSISTENT_RESERVE_IN) ||
  916. (cdb[0] == PERSISTENT_RESERVE_OUT) ||
  917. (cdb[0] == RELEASE || cdb[0] == RELEASE_10) ||
  918. (cdb[0] == RESERVE || cdb[0] == RESERVE_10))) {
  919. return TCM_UNSUPPORTED_SCSI_OPCODE;
  920. }
  921. /*
  922. * For PERSISTENT RESERVE IN/OUT, RELEASE, and RESERVE we need to
  923. * emulate the response, since tcmu does not have the information
  924. * required to process these commands.
  925. */
  926. if (!(dev->transport_flags &
  927. TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
  928. if (cdb[0] == PERSISTENT_RESERVE_IN) {
  929. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  930. size = get_unaligned_be16(&cdb[7]);
  931. return target_cmd_size_check(cmd, size);
  932. }
  933. if (cdb[0] == PERSISTENT_RESERVE_OUT) {
  934. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  935. size = get_unaligned_be32(&cdb[5]);
  936. return target_cmd_size_check(cmd, size);
  937. }
  938. if (cdb[0] == RELEASE || cdb[0] == RELEASE_10) {
  939. cmd->execute_cmd = target_scsi2_reservation_release;
  940. if (cdb[0] == RELEASE_10)
  941. size = get_unaligned_be16(&cdb[7]);
  942. else
  943. size = cmd->data_length;
  944. return target_cmd_size_check(cmd, size);
  945. }
  946. if (cdb[0] == RESERVE || cdb[0] == RESERVE_10) {
  947. cmd->execute_cmd = target_scsi2_reservation_reserve;
  948. if (cdb[0] == RESERVE_10)
  949. size = get_unaligned_be16(&cdb[7]);
  950. else
  951. size = cmd->data_length;
  952. return target_cmd_size_check(cmd, size);
  953. }
  954. }
  955. /* Set DATA_CDB flag for ops that should have it */
  956. switch (cdb[0]) {
  957. case READ_6:
  958. case READ_10:
  959. case READ_12:
  960. case READ_16:
  961. case WRITE_6:
  962. case WRITE_10:
  963. case WRITE_12:
  964. case WRITE_16:
  965. case WRITE_VERIFY:
  966. case WRITE_VERIFY_12:
  967. case WRITE_VERIFY_16:
  968. case COMPARE_AND_WRITE:
  969. case XDWRITEREAD_10:
  970. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  971. break;
  972. case VARIABLE_LENGTH_CMD:
  973. switch (get_unaligned_be16(&cdb[8])) {
  974. case READ_32:
  975. case WRITE_32:
  976. case WRITE_VERIFY_32:
  977. case XDWRITEREAD_32:
  978. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  979. break;
  980. }
  981. }
  982. cmd->execute_cmd = exec_cmd;
  983. return TCM_NO_SENSE;
  984. }
  985. EXPORT_SYMBOL(passthrough_parse_cdb);