target_core_device.c 29 KB

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