target_core_device.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /*******************************************************************************
  2. * Filename: target_core_device.c (based on iscsi_target_device.c)
  3. *
  4. * This file contains the TCM Virtual Device and Disk Transport
  5. * agnostic related functions.
  6. *
  7. * (c) Copyright 2003-2013 Datera, Inc.
  8. *
  9. * Nicholas A. Bellinger <nab@kernel.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. *
  25. ******************************************************************************/
  26. #include <linux/net.h>
  27. #include <linux/string.h>
  28. #include <linux/delay.h>
  29. #include <linux/timer.h>
  30. #include <linux/slab.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/kthread.h>
  33. #include <linux/in.h>
  34. #include <linux/export.h>
  35. #include <asm/unaligned.h>
  36. #include <net/sock.h>
  37. #include <net/tcp.h>
  38. #include <scsi/scsi_common.h>
  39. #include <scsi/scsi_proto.h>
  40. #include <target/target_core_base.h>
  41. #include <target/target_core_backend.h>
  42. #include <target/target_core_fabric.h>
  43. #include "target_core_internal.h"
  44. #include "target_core_alua.h"
  45. #include "target_core_pr.h"
  46. #include "target_core_ua.h"
  47. DEFINE_MUTEX(g_device_mutex);
  48. LIST_HEAD(g_device_list);
  49. static struct se_hba *lun0_hba;
  50. /* not static, needed by tpg.c */
  51. struct se_device *g_lun0_dev;
  52. sense_reason_t
  53. transport_lookup_cmd_lun(struct se_cmd *se_cmd, u64 unpacked_lun)
  54. {
  55. struct se_lun *se_lun = NULL;
  56. struct se_session *se_sess = se_cmd->se_sess;
  57. struct se_node_acl *nacl = se_sess->se_node_acl;
  58. struct se_dev_entry *deve;
  59. sense_reason_t ret = TCM_NO_SENSE;
  60. rcu_read_lock();
  61. deve = target_nacl_find_deve(nacl, unpacked_lun);
  62. if (deve) {
  63. atomic_long_inc(&deve->total_cmds);
  64. if (se_cmd->data_direction == DMA_TO_DEVICE)
  65. atomic_long_add(se_cmd->data_length,
  66. &deve->write_bytes);
  67. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  68. atomic_long_add(se_cmd->data_length,
  69. &deve->read_bytes);
  70. se_lun = rcu_dereference(deve->se_lun);
  71. se_cmd->se_lun = rcu_dereference(deve->se_lun);
  72. se_cmd->pr_res_key = deve->pr_res_key;
  73. se_cmd->orig_fe_lun = unpacked_lun;
  74. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  75. percpu_ref_get(&se_lun->lun_ref);
  76. se_cmd->lun_ref_active = true;
  77. if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
  78. deve->lun_access_ro) {
  79. pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
  80. " Access for 0x%08llx\n",
  81. se_cmd->se_tfo->get_fabric_name(),
  82. unpacked_lun);
  83. rcu_read_unlock();
  84. ret = TCM_WRITE_PROTECTED;
  85. goto ref_dev;
  86. }
  87. }
  88. rcu_read_unlock();
  89. if (!se_lun) {
  90. /*
  91. * Use the se_portal_group->tpg_virt_lun0 to allow for
  92. * REPORT_LUNS, et al to be returned when no active
  93. * MappedLUN=0 exists for this Initiator Port.
  94. */
  95. if (unpacked_lun != 0) {
  96. pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  97. " Access for 0x%08llx\n",
  98. se_cmd->se_tfo->get_fabric_name(),
  99. unpacked_lun);
  100. return TCM_NON_EXISTENT_LUN;
  101. }
  102. se_lun = se_sess->se_tpg->tpg_virt_lun0;
  103. se_cmd->se_lun = se_sess->se_tpg->tpg_virt_lun0;
  104. se_cmd->orig_fe_lun = 0;
  105. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  106. percpu_ref_get(&se_lun->lun_ref);
  107. se_cmd->lun_ref_active = true;
  108. /*
  109. * Force WRITE PROTECT for virtual LUN 0
  110. */
  111. if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
  112. (se_cmd->data_direction != DMA_NONE)) {
  113. ret = TCM_WRITE_PROTECTED;
  114. goto ref_dev;
  115. }
  116. }
  117. /*
  118. * RCU reference protected by percpu se_lun->lun_ref taken above that
  119. * must drop to zero (including initial reference) before this se_lun
  120. * pointer can be kfree_rcu() by the final se_lun->lun_group put via
  121. * target_core_fabric_configfs.c:target_fabric_port_release
  122. */
  123. ref_dev:
  124. se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  125. atomic_long_inc(&se_cmd->se_dev->num_cmds);
  126. if (se_cmd->data_direction == DMA_TO_DEVICE)
  127. atomic_long_add(se_cmd->data_length,
  128. &se_cmd->se_dev->write_bytes);
  129. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  130. atomic_long_add(se_cmd->data_length,
  131. &se_cmd->se_dev->read_bytes);
  132. return ret;
  133. }
  134. EXPORT_SYMBOL(transport_lookup_cmd_lun);
  135. int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u64 unpacked_lun)
  136. {
  137. struct se_dev_entry *deve;
  138. struct se_lun *se_lun = NULL;
  139. struct se_session *se_sess = se_cmd->se_sess;
  140. struct se_node_acl *nacl = se_sess->se_node_acl;
  141. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  142. unsigned long flags;
  143. rcu_read_lock();
  144. deve = target_nacl_find_deve(nacl, unpacked_lun);
  145. if (deve) {
  146. se_tmr->tmr_lun = rcu_dereference(deve->se_lun);
  147. se_cmd->se_lun = rcu_dereference(deve->se_lun);
  148. se_lun = rcu_dereference(deve->se_lun);
  149. se_cmd->pr_res_key = deve->pr_res_key;
  150. se_cmd->orig_fe_lun = unpacked_lun;
  151. }
  152. rcu_read_unlock();
  153. if (!se_lun) {
  154. pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  155. " Access for 0x%08llx\n",
  156. se_cmd->se_tfo->get_fabric_name(),
  157. unpacked_lun);
  158. return -ENODEV;
  159. }
  160. /*
  161. * XXX: Add percpu se_lun->lun_ref reference count for TMR
  162. */
  163. se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  164. se_tmr->tmr_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  165. spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
  166. list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
  167. spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
  168. return 0;
  169. }
  170. EXPORT_SYMBOL(transport_lookup_tmr_lun);
  171. bool target_lun_is_rdonly(struct se_cmd *cmd)
  172. {
  173. struct se_session *se_sess = cmd->se_sess;
  174. struct se_dev_entry *deve;
  175. bool ret;
  176. rcu_read_lock();
  177. deve = target_nacl_find_deve(se_sess->se_node_acl, cmd->orig_fe_lun);
  178. ret = deve && deve->lun_access_ro;
  179. rcu_read_unlock();
  180. return ret;
  181. }
  182. EXPORT_SYMBOL(target_lun_is_rdonly);
  183. /*
  184. * This function is called from core_scsi3_emulate_pro_register_and_move()
  185. * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_kref
  186. * when a matching rtpi is found.
  187. */
  188. struct se_dev_entry *core_get_se_deve_from_rtpi(
  189. struct se_node_acl *nacl,
  190. u16 rtpi)
  191. {
  192. struct se_dev_entry *deve;
  193. struct se_lun *lun;
  194. struct se_portal_group *tpg = nacl->se_tpg;
  195. rcu_read_lock();
  196. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  197. lun = rcu_dereference(deve->se_lun);
  198. if (!lun) {
  199. pr_err("%s device entries device pointer is"
  200. " NULL, but Initiator has access.\n",
  201. tpg->se_tpg_tfo->get_fabric_name());
  202. continue;
  203. }
  204. if (lun->lun_rtpi != rtpi)
  205. continue;
  206. kref_get(&deve->pr_kref);
  207. rcu_read_unlock();
  208. return deve;
  209. }
  210. rcu_read_unlock();
  211. return NULL;
  212. }
  213. void core_free_device_list_for_node(
  214. struct se_node_acl *nacl,
  215. struct se_portal_group *tpg)
  216. {
  217. struct se_dev_entry *deve;
  218. mutex_lock(&nacl->lun_entry_mutex);
  219. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  220. struct se_lun *lun = rcu_dereference_check(deve->se_lun,
  221. lockdep_is_held(&nacl->lun_entry_mutex));
  222. core_disable_device_list_for_node(lun, deve, nacl, tpg);
  223. }
  224. mutex_unlock(&nacl->lun_entry_mutex);
  225. }
  226. void core_update_device_list_access(
  227. u64 mapped_lun,
  228. bool lun_access_ro,
  229. struct se_node_acl *nacl)
  230. {
  231. struct se_dev_entry *deve;
  232. mutex_lock(&nacl->lun_entry_mutex);
  233. deve = target_nacl_find_deve(nacl, mapped_lun);
  234. if (deve)
  235. deve->lun_access_ro = lun_access_ro;
  236. mutex_unlock(&nacl->lun_entry_mutex);
  237. }
  238. /*
  239. * Called with rcu_read_lock or nacl->device_list_lock held.
  240. */
  241. struct se_dev_entry *target_nacl_find_deve(struct se_node_acl *nacl, u64 mapped_lun)
  242. {
  243. struct se_dev_entry *deve;
  244. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
  245. if (deve->mapped_lun == mapped_lun)
  246. return deve;
  247. return NULL;
  248. }
  249. EXPORT_SYMBOL(target_nacl_find_deve);
  250. void target_pr_kref_release(struct kref *kref)
  251. {
  252. struct se_dev_entry *deve = container_of(kref, struct se_dev_entry,
  253. pr_kref);
  254. complete(&deve->pr_comp);
  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. atomic_set(&new->ua_count, 0);
  285. spin_lock_init(&new->ua_lock);
  286. INIT_LIST_HEAD(&new->ua_list);
  287. INIT_LIST_HEAD(&new->lun_link);
  288. new->mapped_lun = mapped_lun;
  289. kref_init(&new->pr_kref);
  290. init_completion(&new->pr_comp);
  291. new->lun_access_ro = lun_access_ro;
  292. new->creation_time = get_jiffies_64();
  293. new->attach_count++;
  294. mutex_lock(&nacl->lun_entry_mutex);
  295. orig = target_nacl_find_deve(nacl, mapped_lun);
  296. if (orig && orig->se_lun) {
  297. struct se_lun *orig_lun = rcu_dereference_check(orig->se_lun,
  298. lockdep_is_held(&nacl->lun_entry_mutex));
  299. if (orig_lun != lun) {
  300. pr_err("Existing orig->se_lun doesn't match new lun"
  301. " for dynamic -> explicit NodeACL conversion:"
  302. " %s\n", nacl->initiatorname);
  303. mutex_unlock(&nacl->lun_entry_mutex);
  304. kfree(new);
  305. return -EINVAL;
  306. }
  307. BUG_ON(orig->se_lun_acl != NULL);
  308. rcu_assign_pointer(new->se_lun, lun);
  309. rcu_assign_pointer(new->se_lun_acl, lun_acl);
  310. hlist_del_rcu(&orig->link);
  311. hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
  312. mutex_unlock(&nacl->lun_entry_mutex);
  313. spin_lock(&lun->lun_deve_lock);
  314. list_del(&orig->lun_link);
  315. list_add_tail(&new->lun_link, &lun->lun_deve_list);
  316. spin_unlock(&lun->lun_deve_lock);
  317. kref_put(&orig->pr_kref, target_pr_kref_release);
  318. wait_for_completion(&orig->pr_comp);
  319. target_luns_data_has_changed(nacl, new, true);
  320. kfree_rcu(orig, rcu_head);
  321. return 0;
  322. }
  323. rcu_assign_pointer(new->se_lun, lun);
  324. rcu_assign_pointer(new->se_lun_acl, lun_acl);
  325. hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
  326. mutex_unlock(&nacl->lun_entry_mutex);
  327. spin_lock(&lun->lun_deve_lock);
  328. list_add_tail(&new->lun_link, &lun->lun_deve_list);
  329. spin_unlock(&lun->lun_deve_lock);
  330. target_luns_data_has_changed(nacl, new, true);
  331. return 0;
  332. }
  333. /*
  334. * Called with se_node_acl->lun_entry_mutex held.
  335. */
  336. void core_disable_device_list_for_node(
  337. struct se_lun *lun,
  338. struct se_dev_entry *orig,
  339. struct se_node_acl *nacl,
  340. struct se_portal_group *tpg)
  341. {
  342. /*
  343. * rcu_dereference_raw protected by se_lun->lun_group symlink
  344. * reference to se_device->dev_group.
  345. */
  346. struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
  347. /*
  348. * If the MappedLUN entry is being disabled, the entry in
  349. * lun->lun_deve_list must be removed now before clearing the
  350. * struct se_dev_entry pointers below as logic in
  351. * core_alua_do_transition_tg_pt() depends on these being present.
  352. *
  353. * deve->se_lun_acl will be NULL for demo-mode created LUNs
  354. * that have not been explicitly converted to MappedLUNs ->
  355. * struct se_lun_acl, but we remove deve->lun_link from
  356. * lun->lun_deve_list. This also means that active UAs and
  357. * NodeACL context specific PR metadata for demo-mode
  358. * MappedLUN *deve will be released below..
  359. */
  360. spin_lock(&lun->lun_deve_lock);
  361. list_del(&orig->lun_link);
  362. spin_unlock(&lun->lun_deve_lock);
  363. /*
  364. * Disable struct se_dev_entry LUN ACL mapping
  365. */
  366. core_scsi3_ua_release_all(orig);
  367. hlist_del_rcu(&orig->link);
  368. clear_bit(DEF_PR_REG_ACTIVE, &orig->deve_flags);
  369. orig->lun_access_ro = false;
  370. orig->creation_time = 0;
  371. orig->attach_count--;
  372. /*
  373. * Before firing off RCU callback, wait for any in process SPEC_I_PT=1
  374. * or REGISTER_AND_MOVE PR operation to complete.
  375. */
  376. kref_put(&orig->pr_kref, target_pr_kref_release);
  377. wait_for_completion(&orig->pr_comp);
  378. rcu_assign_pointer(orig->se_lun, NULL);
  379. rcu_assign_pointer(orig->se_lun_acl, NULL);
  380. kfree_rcu(orig, rcu_head);
  381. core_scsi3_free_pr_reg_from_nacl(dev, nacl);
  382. target_luns_data_has_changed(nacl, NULL, false);
  383. }
  384. /* core_clear_lun_from_tpg():
  385. *
  386. *
  387. */
  388. void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
  389. {
  390. struct se_node_acl *nacl;
  391. struct se_dev_entry *deve;
  392. mutex_lock(&tpg->acl_node_mutex);
  393. list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
  394. mutex_lock(&nacl->lun_entry_mutex);
  395. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  396. struct se_lun *tmp_lun = rcu_dereference_check(deve->se_lun,
  397. lockdep_is_held(&nacl->lun_entry_mutex));
  398. if (lun != tmp_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. int core_alloc_rtpi(struct se_lun *lun, struct se_device *dev)
  407. {
  408. struct se_lun *tmp;
  409. spin_lock(&dev->se_port_lock);
  410. if (dev->export_count == 0x0000ffff) {
  411. pr_warn("Reached dev->dev_port_count =="
  412. " 0x0000ffff\n");
  413. spin_unlock(&dev->se_port_lock);
  414. return -ENOSPC;
  415. }
  416. again:
  417. /*
  418. * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
  419. * Here is the table from spc4r17 section 7.7.3.8.
  420. *
  421. * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
  422. *
  423. * Code Description
  424. * 0h Reserved
  425. * 1h Relative port 1, historically known as port A
  426. * 2h Relative port 2, historically known as port B
  427. * 3h to FFFFh Relative port 3 through 65 535
  428. */
  429. lun->lun_rtpi = dev->dev_rpti_counter++;
  430. if (!lun->lun_rtpi)
  431. goto again;
  432. list_for_each_entry(tmp, &dev->dev_sep_list, lun_dev_link) {
  433. /*
  434. * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
  435. * for 16-bit wrap..
  436. */
  437. if (lun->lun_rtpi == tmp->lun_rtpi)
  438. goto again;
  439. }
  440. spin_unlock(&dev->se_port_lock);
  441. return 0;
  442. }
  443. static void se_release_vpd_for_dev(struct se_device *dev)
  444. {
  445. struct t10_vpd *vpd, *vpd_tmp;
  446. spin_lock(&dev->t10_wwn.t10_vpd_lock);
  447. list_for_each_entry_safe(vpd, vpd_tmp,
  448. &dev->t10_wwn.t10_vpd_list, vpd_list) {
  449. list_del(&vpd->vpd_list);
  450. kfree(vpd);
  451. }
  452. spin_unlock(&dev->t10_wwn.t10_vpd_lock);
  453. }
  454. static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
  455. {
  456. u32 aligned_max_sectors;
  457. u32 alignment;
  458. /*
  459. * Limit max_sectors to a PAGE_SIZE aligned value for modern
  460. * transport_allocate_data_tasks() operation.
  461. */
  462. alignment = max(1ul, PAGE_SIZE / block_size);
  463. aligned_max_sectors = rounddown(max_sectors, alignment);
  464. if (max_sectors != aligned_max_sectors)
  465. pr_info("Rounding down aligned max_sectors from %u to %u\n",
  466. max_sectors, aligned_max_sectors);
  467. return aligned_max_sectors;
  468. }
  469. int core_dev_add_lun(
  470. struct se_portal_group *tpg,
  471. struct se_device *dev,
  472. struct se_lun *lun)
  473. {
  474. int rc;
  475. rc = core_tpg_add_lun(tpg, lun, false, dev);
  476. if (rc < 0)
  477. return rc;
  478. pr_debug("%s_TPG[%u]_LUN[%llu] - Activated %s Logical Unit from"
  479. " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  480. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  481. tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
  482. /*
  483. * Update LUN maps for dynamically added initiators when
  484. * generate_node_acl is enabled.
  485. */
  486. if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
  487. struct se_node_acl *acl;
  488. mutex_lock(&tpg->acl_node_mutex);
  489. list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
  490. if (acl->dynamic_node_acl &&
  491. (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
  492. !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
  493. core_tpg_add_node_to_devs(acl, tpg, lun);
  494. }
  495. }
  496. mutex_unlock(&tpg->acl_node_mutex);
  497. }
  498. return 0;
  499. }
  500. /* core_dev_del_lun():
  501. *
  502. *
  503. */
  504. void core_dev_del_lun(
  505. struct se_portal_group *tpg,
  506. struct se_lun *lun)
  507. {
  508. pr_debug("%s_TPG[%u]_LUN[%llu] - Deactivating %s Logical Unit from"
  509. " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
  510. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  511. tpg->se_tpg_tfo->get_fabric_name());
  512. core_tpg_remove_lun(tpg, lun);
  513. }
  514. struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
  515. struct se_portal_group *tpg,
  516. struct se_node_acl *nacl,
  517. u64 mapped_lun,
  518. int *ret)
  519. {
  520. struct se_lun_acl *lacl;
  521. if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
  522. pr_err("%s InitiatorName exceeds maximum size.\n",
  523. tpg->se_tpg_tfo->get_fabric_name());
  524. *ret = -EOVERFLOW;
  525. return NULL;
  526. }
  527. lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
  528. if (!lacl) {
  529. pr_err("Unable to allocate memory for struct se_lun_acl.\n");
  530. *ret = -ENOMEM;
  531. return NULL;
  532. }
  533. lacl->mapped_lun = mapped_lun;
  534. lacl->se_lun_nacl = nacl;
  535. return lacl;
  536. }
  537. int core_dev_add_initiator_node_lun_acl(
  538. struct se_portal_group *tpg,
  539. struct se_lun_acl *lacl,
  540. struct se_lun *lun,
  541. bool lun_access_ro)
  542. {
  543. struct se_node_acl *nacl = lacl->se_lun_nacl;
  544. /*
  545. * rcu_dereference_raw protected by se_lun->lun_group symlink
  546. * reference to se_device->dev_group.
  547. */
  548. struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
  549. if (!nacl)
  550. return -EINVAL;
  551. if (lun->lun_access_ro)
  552. lun_access_ro = true;
  553. lacl->se_lun = lun;
  554. if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
  555. lun_access_ro, nacl, tpg) < 0)
  556. return -EINVAL;
  557. pr_debug("%s_TPG[%hu]_LUN[%llu->%llu] - Added %s ACL for "
  558. " InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  559. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun, lacl->mapped_lun,
  560. lun_access_ro ? "RO" : "RW",
  561. nacl->initiatorname);
  562. /*
  563. * Check to see if there are any existing persistent reservation APTPL
  564. * pre-registrations that need to be enabled for this LUN ACL..
  565. */
  566. core_scsi3_check_aptpl_registration(dev, tpg, lun, nacl,
  567. lacl->mapped_lun);
  568. return 0;
  569. }
  570. int core_dev_del_initiator_node_lun_acl(
  571. struct se_lun *lun,
  572. struct se_lun_acl *lacl)
  573. {
  574. struct se_portal_group *tpg = lun->lun_tpg;
  575. struct se_node_acl *nacl;
  576. struct se_dev_entry *deve;
  577. nacl = lacl->se_lun_nacl;
  578. if (!nacl)
  579. return -EINVAL;
  580. mutex_lock(&nacl->lun_entry_mutex);
  581. deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
  582. if (deve)
  583. core_disable_device_list_for_node(lun, deve, nacl, tpg);
  584. mutex_unlock(&nacl->lun_entry_mutex);
  585. pr_debug("%s_TPG[%hu]_LUN[%llu] - Removed ACL for"
  586. " InitiatorNode: %s Mapped LUN: %llu\n",
  587. tpg->se_tpg_tfo->get_fabric_name(),
  588. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  589. nacl->initiatorname, lacl->mapped_lun);
  590. return 0;
  591. }
  592. void core_dev_free_initiator_node_lun_acl(
  593. struct se_portal_group *tpg,
  594. struct se_lun_acl *lacl)
  595. {
  596. pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
  597. " Mapped LUN: %llu\n", tpg->se_tpg_tfo->get_fabric_name(),
  598. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  599. tpg->se_tpg_tfo->get_fabric_name(),
  600. lacl->se_lun_nacl->initiatorname, lacl->mapped_lun);
  601. kfree(lacl);
  602. }
  603. static void scsi_dump_inquiry(struct se_device *dev)
  604. {
  605. struct t10_wwn *wwn = &dev->t10_wwn;
  606. char buf[17];
  607. int i, device_type;
  608. /*
  609. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  610. */
  611. for (i = 0; i < 8; i++)
  612. if (wwn->vendor[i] >= 0x20)
  613. buf[i] = wwn->vendor[i];
  614. else
  615. buf[i] = ' ';
  616. buf[i] = '\0';
  617. pr_debug(" Vendor: %s\n", buf);
  618. for (i = 0; i < 16; i++)
  619. if (wwn->model[i] >= 0x20)
  620. buf[i] = wwn->model[i];
  621. else
  622. buf[i] = ' ';
  623. buf[i] = '\0';
  624. pr_debug(" Model: %s\n", buf);
  625. for (i = 0; i < 4; i++)
  626. if (wwn->revision[i] >= 0x20)
  627. buf[i] = wwn->revision[i];
  628. else
  629. buf[i] = ' ';
  630. buf[i] = '\0';
  631. pr_debug(" Revision: %s\n", buf);
  632. device_type = dev->transport->get_device_type(dev);
  633. pr_debug(" Type: %s ", scsi_device_type(device_type));
  634. }
  635. struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
  636. {
  637. struct se_device *dev;
  638. struct se_lun *xcopy_lun;
  639. dev = hba->backend->ops->alloc_device(hba, name);
  640. if (!dev)
  641. return NULL;
  642. dev->dev_link_magic = SE_DEV_LINK_MAGIC;
  643. dev->se_hba = hba;
  644. dev->transport = hba->backend->ops;
  645. dev->prot_length = sizeof(struct t10_pi_tuple);
  646. dev->hba_index = hba->hba_index;
  647. INIT_LIST_HEAD(&dev->dev_list);
  648. INIT_LIST_HEAD(&dev->dev_sep_list);
  649. INIT_LIST_HEAD(&dev->dev_tmr_list);
  650. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  651. INIT_LIST_HEAD(&dev->state_list);
  652. INIT_LIST_HEAD(&dev->qf_cmd_list);
  653. INIT_LIST_HEAD(&dev->g_dev_node);
  654. spin_lock_init(&dev->execute_task_lock);
  655. spin_lock_init(&dev->delayed_cmd_lock);
  656. spin_lock_init(&dev->dev_reservation_lock);
  657. spin_lock_init(&dev->se_port_lock);
  658. spin_lock_init(&dev->se_tmr_lock);
  659. spin_lock_init(&dev->qf_cmd_lock);
  660. sema_init(&dev->caw_sem, 1);
  661. INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
  662. spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
  663. INIT_LIST_HEAD(&dev->t10_pr.registration_list);
  664. INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
  665. spin_lock_init(&dev->t10_pr.registration_lock);
  666. spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
  667. INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
  668. spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
  669. INIT_LIST_HEAD(&dev->t10_alua.lba_map_list);
  670. spin_lock_init(&dev->t10_alua.lba_map_lock);
  671. dev->t10_wwn.t10_dev = dev;
  672. dev->t10_alua.t10_dev = dev;
  673. dev->dev_attrib.da_dev = dev;
  674. dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
  675. dev->dev_attrib.emulate_dpo = 1;
  676. dev->dev_attrib.emulate_fua_write = 1;
  677. dev->dev_attrib.emulate_fua_read = 1;
  678. dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
  679. dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
  680. dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
  681. dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
  682. dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
  683. dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
  684. dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
  685. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE0_PROT;
  686. dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
  687. dev->dev_attrib.force_pr_aptpl = DA_FORCE_PR_APTPL;
  688. dev->dev_attrib.is_nonrot = DA_IS_NONROT;
  689. dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
  690. dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
  691. dev->dev_attrib.max_unmap_block_desc_count =
  692. DA_MAX_UNMAP_BLOCK_DESC_COUNT;
  693. dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
  694. dev->dev_attrib.unmap_granularity_alignment =
  695. DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
  696. dev->dev_attrib.unmap_zeroes_data =
  697. DA_UNMAP_ZEROES_DATA_DEFAULT;
  698. dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
  699. xcopy_lun = &dev->xcopy_lun;
  700. rcu_assign_pointer(xcopy_lun->lun_se_dev, dev);
  701. init_completion(&xcopy_lun->lun_ref_comp);
  702. INIT_LIST_HEAD(&xcopy_lun->lun_deve_list);
  703. INIT_LIST_HEAD(&xcopy_lun->lun_dev_link);
  704. mutex_init(&xcopy_lun->lun_tg_pt_md_mutex);
  705. xcopy_lun->lun_tpg = &xcopy_pt_tpg;
  706. return dev;
  707. }
  708. /*
  709. * Check if the underlying struct block_device request_queue supports
  710. * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
  711. * in ATA and we need to set TPE=1
  712. */
  713. bool target_configure_unmap_from_queue(struct se_dev_attrib *attrib,
  714. struct request_queue *q)
  715. {
  716. int block_size = queue_logical_block_size(q);
  717. if (!blk_queue_discard(q))
  718. return false;
  719. attrib->max_unmap_lba_count =
  720. q->limits.max_discard_sectors >> (ilog2(block_size) - 9);
  721. /*
  722. * Currently hardcoded to 1 in Linux/SCSI code..
  723. */
  724. attrib->max_unmap_block_desc_count = 1;
  725. attrib->unmap_granularity = q->limits.discard_granularity / block_size;
  726. attrib->unmap_granularity_alignment = q->limits.discard_alignment /
  727. block_size;
  728. attrib->unmap_zeroes_data = q->limits.discard_zeroes_data;
  729. return true;
  730. }
  731. EXPORT_SYMBOL(target_configure_unmap_from_queue);
  732. /*
  733. * Convert from blocksize advertised to the initiator to the 512 byte
  734. * units unconditionally used by the Linux block layer.
  735. */
  736. sector_t target_to_linux_sector(struct se_device *dev, sector_t lb)
  737. {
  738. switch (dev->dev_attrib.block_size) {
  739. case 4096:
  740. return lb << 3;
  741. case 2048:
  742. return lb << 2;
  743. case 1024:
  744. return lb << 1;
  745. default:
  746. return lb;
  747. }
  748. }
  749. EXPORT_SYMBOL(target_to_linux_sector);
  750. int target_configure_device(struct se_device *dev)
  751. {
  752. struct se_hba *hba = dev->se_hba;
  753. int ret;
  754. if (dev->dev_flags & DF_CONFIGURED) {
  755. pr_err("se_dev->se_dev_ptr already set for storage"
  756. " object\n");
  757. return -EEXIST;
  758. }
  759. ret = dev->transport->configure_device(dev);
  760. if (ret)
  761. goto out;
  762. /*
  763. * XXX: there is not much point to have two different values here..
  764. */
  765. dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
  766. dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
  767. /*
  768. * Align max_hw_sectors down to PAGE_SIZE I/O transfers
  769. */
  770. dev->dev_attrib.hw_max_sectors =
  771. se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
  772. dev->dev_attrib.hw_block_size);
  773. dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors;
  774. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  775. dev->creation_time = get_jiffies_64();
  776. ret = core_setup_alua(dev);
  777. if (ret)
  778. goto out;
  779. /*
  780. * Startup the struct se_device processing thread
  781. */
  782. dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
  783. dev->transport->name);
  784. if (!dev->tmr_wq) {
  785. pr_err("Unable to create tmr workqueue for %s\n",
  786. dev->transport->name);
  787. ret = -ENOMEM;
  788. goto out_free_alua;
  789. }
  790. /*
  791. * Setup work_queue for QUEUE_FULL
  792. */
  793. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  794. /*
  795. * Preload the initial INQUIRY const values if we are doing
  796. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  797. * passthrough because this is being provided by the backend LLD.
  798. */
  799. if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)) {
  800. strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
  801. strncpy(&dev->t10_wwn.model[0],
  802. dev->transport->inquiry_prod, 16);
  803. strncpy(&dev->t10_wwn.revision[0],
  804. dev->transport->inquiry_rev, 4);
  805. }
  806. scsi_dump_inquiry(dev);
  807. spin_lock(&hba->device_lock);
  808. hba->dev_count++;
  809. spin_unlock(&hba->device_lock);
  810. mutex_lock(&g_device_mutex);
  811. list_add_tail(&dev->g_dev_node, &g_device_list);
  812. mutex_unlock(&g_device_mutex);
  813. dev->dev_flags |= DF_CONFIGURED;
  814. return 0;
  815. out_free_alua:
  816. core_alua_free_lu_gp_mem(dev);
  817. out:
  818. se_release_vpd_for_dev(dev);
  819. return ret;
  820. }
  821. void target_free_device(struct se_device *dev)
  822. {
  823. struct se_hba *hba = dev->se_hba;
  824. WARN_ON(!list_empty(&dev->dev_sep_list));
  825. if (dev->dev_flags & DF_CONFIGURED) {
  826. destroy_workqueue(dev->tmr_wq);
  827. mutex_lock(&g_device_mutex);
  828. list_del(&dev->g_dev_node);
  829. mutex_unlock(&g_device_mutex);
  830. spin_lock(&hba->device_lock);
  831. hba->dev_count--;
  832. spin_unlock(&hba->device_lock);
  833. }
  834. core_alua_free_lu_gp_mem(dev);
  835. core_alua_set_lba_map(dev, NULL, 0, 0);
  836. core_scsi3_free_all_registrations(dev);
  837. se_release_vpd_for_dev(dev);
  838. if (dev->transport->free_prot)
  839. dev->transport->free_prot(dev);
  840. dev->transport->free_device(dev);
  841. }
  842. int core_dev_setup_virtual_lun0(void)
  843. {
  844. struct se_hba *hba;
  845. struct se_device *dev;
  846. char buf[] = "rd_pages=8,rd_nullio=1";
  847. int ret;
  848. hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
  849. if (IS_ERR(hba))
  850. return PTR_ERR(hba);
  851. dev = target_alloc_device(hba, "virt_lun0");
  852. if (!dev) {
  853. ret = -ENOMEM;
  854. goto out_free_hba;
  855. }
  856. hba->backend->ops->set_configfs_dev_params(dev, buf, sizeof(buf));
  857. ret = target_configure_device(dev);
  858. if (ret)
  859. goto out_free_se_dev;
  860. lun0_hba = hba;
  861. g_lun0_dev = dev;
  862. return 0;
  863. out_free_se_dev:
  864. target_free_device(dev);
  865. out_free_hba:
  866. core_delete_hba(hba);
  867. return ret;
  868. }
  869. void core_dev_release_virtual_lun0(void)
  870. {
  871. struct se_hba *hba = lun0_hba;
  872. if (!hba)
  873. return;
  874. if (g_lun0_dev)
  875. target_free_device(g_lun0_dev);
  876. core_delete_hba(hba);
  877. }
  878. /*
  879. * Common CDB parsing for kernel and user passthrough.
  880. */
  881. sense_reason_t
  882. passthrough_parse_cdb(struct se_cmd *cmd,
  883. sense_reason_t (*exec_cmd)(struct se_cmd *cmd))
  884. {
  885. unsigned char *cdb = cmd->t_task_cdb;
  886. /*
  887. * Clear a lun set in the cdb if the initiator talking to use spoke
  888. * and old standards version, as we can't assume the underlying device
  889. * won't choke up on it.
  890. */
  891. switch (cdb[0]) {
  892. case READ_10: /* SBC - RDProtect */
  893. case READ_12: /* SBC - RDProtect */
  894. case READ_16: /* SBC - RDProtect */
  895. case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
  896. case VERIFY: /* SBC - VRProtect */
  897. case VERIFY_16: /* SBC - VRProtect */
  898. case WRITE_VERIFY: /* SBC - VRProtect */
  899. case WRITE_VERIFY_12: /* SBC - VRProtect */
  900. case MAINTENANCE_IN: /* SPC - Parameter Data Format for SA RTPG */
  901. break;
  902. default:
  903. cdb[1] &= 0x1f; /* clear logical unit number */
  904. break;
  905. }
  906. /*
  907. * For REPORT LUNS we always need to emulate the response, for everything
  908. * else, pass it up.
  909. */
  910. if (cdb[0] == REPORT_LUNS) {
  911. cmd->execute_cmd = spc_emulate_report_luns;
  912. return TCM_NO_SENSE;
  913. }
  914. /* Set DATA_CDB flag for ops that should have it */
  915. switch (cdb[0]) {
  916. case READ_6:
  917. case READ_10:
  918. case READ_12:
  919. case READ_16:
  920. case WRITE_6:
  921. case WRITE_10:
  922. case WRITE_12:
  923. case WRITE_16:
  924. case WRITE_VERIFY:
  925. case WRITE_VERIFY_12:
  926. case 0x8e: /* WRITE_VERIFY_16 */
  927. case COMPARE_AND_WRITE:
  928. case XDWRITEREAD_10:
  929. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  930. break;
  931. case VARIABLE_LENGTH_CMD:
  932. switch (get_unaligned_be16(&cdb[8])) {
  933. case READ_32:
  934. case WRITE_32:
  935. case 0x0c: /* WRITE_VERIFY_32 */
  936. case XDWRITEREAD_32:
  937. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  938. break;
  939. }
  940. }
  941. cmd->execute_cmd = exec_cmd;
  942. return TCM_NO_SENSE;
  943. }
  944. EXPORT_SYMBOL(passthrough_parse_cdb);