target_core_device.c 28 KB

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