target_core_device.c 30 KB

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