target_core_device.c 28 KB

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