target_core_device.c 29 KB

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