target_core_device.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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, int block_size)
  715. {
  716. if (!blk_queue_discard(q))
  717. return false;
  718. attrib->max_unmap_lba_count = (q->limits.max_discard_sectors << 9) /
  719. block_size;
  720. /*
  721. * Currently hardcoded to 1 in Linux/SCSI code..
  722. */
  723. attrib->max_unmap_block_desc_count = 1;
  724. attrib->unmap_granularity = q->limits.discard_granularity / block_size;
  725. attrib->unmap_granularity_alignment = q->limits.discard_alignment /
  726. block_size;
  727. attrib->unmap_zeroes_data = q->limits.discard_zeroes_data;
  728. return true;
  729. }
  730. EXPORT_SYMBOL(target_configure_unmap_from_queue);
  731. /*
  732. * Convert from blocksize advertised to the initiator to the 512 byte
  733. * units unconditionally used by the Linux block layer.
  734. */
  735. sector_t target_to_linux_sector(struct se_device *dev, sector_t lb)
  736. {
  737. switch (dev->dev_attrib.block_size) {
  738. case 4096:
  739. return lb << 3;
  740. case 2048:
  741. return lb << 2;
  742. case 1024:
  743. return lb << 1;
  744. default:
  745. return lb;
  746. }
  747. }
  748. EXPORT_SYMBOL(target_to_linux_sector);
  749. int target_configure_device(struct se_device *dev)
  750. {
  751. struct se_hba *hba = dev->se_hba;
  752. int ret;
  753. if (dev->dev_flags & DF_CONFIGURED) {
  754. pr_err("se_dev->se_dev_ptr already set for storage"
  755. " object\n");
  756. return -EEXIST;
  757. }
  758. ret = dev->transport->configure_device(dev);
  759. if (ret)
  760. goto out;
  761. /*
  762. * XXX: there is not much point to have two different values here..
  763. */
  764. dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
  765. dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
  766. /*
  767. * Align max_hw_sectors down to PAGE_SIZE I/O transfers
  768. */
  769. dev->dev_attrib.hw_max_sectors =
  770. se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
  771. dev->dev_attrib.hw_block_size);
  772. dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors;
  773. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  774. dev->creation_time = get_jiffies_64();
  775. ret = core_setup_alua(dev);
  776. if (ret)
  777. goto out;
  778. /*
  779. * Startup the struct se_device processing thread
  780. */
  781. dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
  782. dev->transport->name);
  783. if (!dev->tmr_wq) {
  784. pr_err("Unable to create tmr workqueue for %s\n",
  785. dev->transport->name);
  786. ret = -ENOMEM;
  787. goto out_free_alua;
  788. }
  789. /*
  790. * Setup work_queue for QUEUE_FULL
  791. */
  792. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  793. /*
  794. * Preload the initial INQUIRY const values if we are doing
  795. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  796. * passthrough because this is being provided by the backend LLD.
  797. */
  798. if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)) {
  799. strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
  800. strncpy(&dev->t10_wwn.model[0],
  801. dev->transport->inquiry_prod, 16);
  802. strncpy(&dev->t10_wwn.revision[0],
  803. dev->transport->inquiry_rev, 4);
  804. }
  805. scsi_dump_inquiry(dev);
  806. spin_lock(&hba->device_lock);
  807. hba->dev_count++;
  808. spin_unlock(&hba->device_lock);
  809. mutex_lock(&g_device_mutex);
  810. list_add_tail(&dev->g_dev_node, &g_device_list);
  811. mutex_unlock(&g_device_mutex);
  812. dev->dev_flags |= DF_CONFIGURED;
  813. return 0;
  814. out_free_alua:
  815. core_alua_free_lu_gp_mem(dev);
  816. out:
  817. se_release_vpd_for_dev(dev);
  818. return ret;
  819. }
  820. void target_free_device(struct se_device *dev)
  821. {
  822. struct se_hba *hba = dev->se_hba;
  823. WARN_ON(!list_empty(&dev->dev_sep_list));
  824. if (dev->dev_flags & DF_CONFIGURED) {
  825. destroy_workqueue(dev->tmr_wq);
  826. mutex_lock(&g_device_mutex);
  827. list_del(&dev->g_dev_node);
  828. mutex_unlock(&g_device_mutex);
  829. spin_lock(&hba->device_lock);
  830. hba->dev_count--;
  831. spin_unlock(&hba->device_lock);
  832. }
  833. core_alua_free_lu_gp_mem(dev);
  834. core_alua_set_lba_map(dev, NULL, 0, 0);
  835. core_scsi3_free_all_registrations(dev);
  836. se_release_vpd_for_dev(dev);
  837. if (dev->transport->free_prot)
  838. dev->transport->free_prot(dev);
  839. dev->transport->free_device(dev);
  840. }
  841. int core_dev_setup_virtual_lun0(void)
  842. {
  843. struct se_hba *hba;
  844. struct se_device *dev;
  845. char buf[] = "rd_pages=8,rd_nullio=1";
  846. int ret;
  847. hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
  848. if (IS_ERR(hba))
  849. return PTR_ERR(hba);
  850. dev = target_alloc_device(hba, "virt_lun0");
  851. if (!dev) {
  852. ret = -ENOMEM;
  853. goto out_free_hba;
  854. }
  855. hba->backend->ops->set_configfs_dev_params(dev, buf, sizeof(buf));
  856. ret = target_configure_device(dev);
  857. if (ret)
  858. goto out_free_se_dev;
  859. lun0_hba = hba;
  860. g_lun0_dev = dev;
  861. return 0;
  862. out_free_se_dev:
  863. target_free_device(dev);
  864. out_free_hba:
  865. core_delete_hba(hba);
  866. return ret;
  867. }
  868. void core_dev_release_virtual_lun0(void)
  869. {
  870. struct se_hba *hba = lun0_hba;
  871. if (!hba)
  872. return;
  873. if (g_lun0_dev)
  874. target_free_device(g_lun0_dev);
  875. core_delete_hba(hba);
  876. }
  877. /*
  878. * Common CDB parsing for kernel and user passthrough.
  879. */
  880. sense_reason_t
  881. passthrough_parse_cdb(struct se_cmd *cmd,
  882. sense_reason_t (*exec_cmd)(struct se_cmd *cmd))
  883. {
  884. unsigned char *cdb = cmd->t_task_cdb;
  885. /*
  886. * Clear a lun set in the cdb if the initiator talking to use spoke
  887. * and old standards version, as we can't assume the underlying device
  888. * won't choke up on it.
  889. */
  890. switch (cdb[0]) {
  891. case READ_10: /* SBC - RDProtect */
  892. case READ_12: /* SBC - RDProtect */
  893. case READ_16: /* SBC - RDProtect */
  894. case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
  895. case VERIFY: /* SBC - VRProtect */
  896. case VERIFY_16: /* SBC - VRProtect */
  897. case WRITE_VERIFY: /* SBC - VRProtect */
  898. case WRITE_VERIFY_12: /* SBC - VRProtect */
  899. case MAINTENANCE_IN: /* SPC - Parameter Data Format for SA RTPG */
  900. break;
  901. default:
  902. cdb[1] &= 0x1f; /* clear logical unit number */
  903. break;
  904. }
  905. /*
  906. * For REPORT LUNS we always need to emulate the response, for everything
  907. * else, pass it up.
  908. */
  909. if (cdb[0] == REPORT_LUNS) {
  910. cmd->execute_cmd = spc_emulate_report_luns;
  911. return TCM_NO_SENSE;
  912. }
  913. /* Set DATA_CDB flag for ops that should have it */
  914. switch (cdb[0]) {
  915. case READ_6:
  916. case READ_10:
  917. case READ_12:
  918. case READ_16:
  919. case WRITE_6:
  920. case WRITE_10:
  921. case WRITE_12:
  922. case WRITE_16:
  923. case WRITE_VERIFY:
  924. case WRITE_VERIFY_12:
  925. case 0x8e: /* WRITE_VERIFY_16 */
  926. case COMPARE_AND_WRITE:
  927. case XDWRITEREAD_10:
  928. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  929. break;
  930. case VARIABLE_LENGTH_CMD:
  931. switch (get_unaligned_be16(&cdb[8])) {
  932. case READ_32:
  933. case WRITE_32:
  934. case 0x0c: /* WRITE_VERIFY_32 */
  935. case XDWRITEREAD_32:
  936. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  937. break;
  938. }
  939. }
  940. cmd->execute_cmd = exec_cmd;
  941. return TCM_NO_SENSE;
  942. }
  943. EXPORT_SYMBOL(passthrough_parse_cdb);