target_core_device.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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 <[email protected]>
  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. if (!percpu_ref_tryget_live(&se_lun->lun_ref)) {
  72. se_lun = NULL;
  73. goto out_unlock;
  74. }
  75. se_cmd->se_lun = rcu_dereference(deve->se_lun);
  76. se_cmd->pr_res_key = deve->pr_res_key;
  77. se_cmd->orig_fe_lun = unpacked_lun;
  78. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  79. se_cmd->lun_ref_active = true;
  80. if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
  81. deve->lun_access_ro) {
  82. pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
  83. " Access for 0x%08llx\n",
  84. se_cmd->se_tfo->get_fabric_name(),
  85. unpacked_lun);
  86. rcu_read_unlock();
  87. ret = TCM_WRITE_PROTECTED;
  88. goto ref_dev;
  89. }
  90. }
  91. out_unlock:
  92. rcu_read_unlock();
  93. if (!se_lun) {
  94. /*
  95. * Use the se_portal_group->tpg_virt_lun0 to allow for
  96. * REPORT_LUNS, et al to be returned when no active
  97. * MappedLUN=0 exists for this Initiator Port.
  98. */
  99. if (unpacked_lun != 0) {
  100. pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  101. " Access for 0x%08llx\n",
  102. se_cmd->se_tfo->get_fabric_name(),
  103. unpacked_lun);
  104. return TCM_NON_EXISTENT_LUN;
  105. }
  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. * Force WRITE PROTECT for virtual LUN 0
  114. */
  115. if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
  116. (se_cmd->data_direction != DMA_NONE)) {
  117. ret = TCM_WRITE_PROTECTED;
  118. goto ref_dev;
  119. }
  120. }
  121. /*
  122. * RCU reference protected by percpu se_lun->lun_ref taken above that
  123. * must drop to zero (including initial reference) before this se_lun
  124. * pointer can be kfree_rcu() by the final se_lun->lun_group put via
  125. * target_core_fabric_configfs.c:target_fabric_port_release
  126. */
  127. ref_dev:
  128. se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  129. atomic_long_inc(&se_cmd->se_dev->num_cmds);
  130. if (se_cmd->data_direction == DMA_TO_DEVICE)
  131. atomic_long_add(se_cmd->data_length,
  132. &se_cmd->se_dev->write_bytes);
  133. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  134. atomic_long_add(se_cmd->data_length,
  135. &se_cmd->se_dev->read_bytes);
  136. return ret;
  137. }
  138. EXPORT_SYMBOL(transport_lookup_cmd_lun);
  139. int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u64 unpacked_lun)
  140. {
  141. struct se_dev_entry *deve;
  142. struct se_lun *se_lun = NULL;
  143. struct se_session *se_sess = se_cmd->se_sess;
  144. struct se_node_acl *nacl = se_sess->se_node_acl;
  145. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  146. unsigned long flags;
  147. rcu_read_lock();
  148. deve = target_nacl_find_deve(nacl, unpacked_lun);
  149. if (deve) {
  150. se_tmr->tmr_lun = rcu_dereference(deve->se_lun);
  151. se_cmd->se_lun = rcu_dereference(deve->se_lun);
  152. se_lun = rcu_dereference(deve->se_lun);
  153. se_cmd->pr_res_key = deve->pr_res_key;
  154. se_cmd->orig_fe_lun = unpacked_lun;
  155. }
  156. rcu_read_unlock();
  157. if (!se_lun) {
  158. pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  159. " Access for 0x%08llx\n",
  160. se_cmd->se_tfo->get_fabric_name(),
  161. unpacked_lun);
  162. return -ENODEV;
  163. }
  164. /*
  165. * XXX: Add percpu se_lun->lun_ref reference count for TMR
  166. */
  167. se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  168. se_tmr->tmr_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  169. spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
  170. list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
  171. spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
  172. return 0;
  173. }
  174. EXPORT_SYMBOL(transport_lookup_tmr_lun);
  175. bool target_lun_is_rdonly(struct se_cmd *cmd)
  176. {
  177. struct se_session *se_sess = cmd->se_sess;
  178. struct se_dev_entry *deve;
  179. bool ret;
  180. rcu_read_lock();
  181. deve = target_nacl_find_deve(se_sess->se_node_acl, cmd->orig_fe_lun);
  182. ret = deve && deve->lun_access_ro;
  183. rcu_read_unlock();
  184. return ret;
  185. }
  186. EXPORT_SYMBOL(target_lun_is_rdonly);
  187. /*
  188. * This function is called from core_scsi3_emulate_pro_register_and_move()
  189. * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_kref
  190. * when a matching rtpi is found.
  191. */
  192. struct se_dev_entry *core_get_se_deve_from_rtpi(
  193. struct se_node_acl *nacl,
  194. u16 rtpi)
  195. {
  196. struct se_dev_entry *deve;
  197. struct se_lun *lun;
  198. struct se_portal_group *tpg = nacl->se_tpg;
  199. rcu_read_lock();
  200. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  201. lun = rcu_dereference(deve->se_lun);
  202. if (!lun) {
  203. pr_err("%s device entries device pointer is"
  204. " NULL, but Initiator has access.\n",
  205. tpg->se_tpg_tfo->get_fabric_name());
  206. continue;
  207. }
  208. if (lun->lun_rtpi != rtpi)
  209. continue;
  210. kref_get(&deve->pr_kref);
  211. rcu_read_unlock();
  212. return deve;
  213. }
  214. rcu_read_unlock();
  215. return NULL;
  216. }
  217. void core_free_device_list_for_node(
  218. struct se_node_acl *nacl,
  219. struct se_portal_group *tpg)
  220. {
  221. struct se_dev_entry *deve;
  222. mutex_lock(&nacl->lun_entry_mutex);
  223. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  224. struct se_lun *lun = rcu_dereference_check(deve->se_lun,
  225. lockdep_is_held(&nacl->lun_entry_mutex));
  226. core_disable_device_list_for_node(lun, deve, nacl, tpg);
  227. }
  228. mutex_unlock(&nacl->lun_entry_mutex);
  229. }
  230. void core_update_device_list_access(
  231. u64 mapped_lun,
  232. bool lun_access_ro,
  233. struct se_node_acl *nacl)
  234. {
  235. struct se_dev_entry *deve;
  236. mutex_lock(&nacl->lun_entry_mutex);
  237. deve = target_nacl_find_deve(nacl, mapped_lun);
  238. if (deve)
  239. deve->lun_access_ro = lun_access_ro;
  240. mutex_unlock(&nacl->lun_entry_mutex);
  241. }
  242. /*
  243. * Called with rcu_read_lock or nacl->device_list_lock held.
  244. */
  245. struct se_dev_entry *target_nacl_find_deve(struct se_node_acl *nacl, u64 mapped_lun)
  246. {
  247. struct se_dev_entry *deve;
  248. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
  249. if (deve->mapped_lun == mapped_lun)
  250. return deve;
  251. return NULL;
  252. }
  253. EXPORT_SYMBOL(target_nacl_find_deve);
  254. void target_pr_kref_release(struct kref *kref)
  255. {
  256. struct se_dev_entry *deve = container_of(kref, struct se_dev_entry,
  257. pr_kref);
  258. complete(&deve->pr_comp);
  259. }
  260. static void
  261. target_luns_data_has_changed(struct se_node_acl *nacl, struct se_dev_entry *new,
  262. bool skip_new)
  263. {
  264. struct se_dev_entry *tmp;
  265. rcu_read_lock();
  266. hlist_for_each_entry_rcu(tmp, &nacl->lun_entry_hlist, link) {
  267. if (skip_new && tmp == new)
  268. continue;
  269. core_scsi3_ua_allocate(tmp, 0x3F,
  270. ASCQ_3FH_REPORTED_LUNS_DATA_HAS_CHANGED);
  271. }
  272. rcu_read_unlock();
  273. }
  274. int core_enable_device_list_for_node(
  275. struct se_lun *lun,
  276. struct se_lun_acl *lun_acl,
  277. u64 mapped_lun,
  278. bool lun_access_ro,
  279. struct se_node_acl *nacl,
  280. struct se_portal_group *tpg)
  281. {
  282. struct se_dev_entry *orig, *new;
  283. new = kzalloc(sizeof(*new), GFP_KERNEL);
  284. if (!new) {
  285. pr_err("Unable to allocate se_dev_entry memory\n");
  286. return -ENOMEM;
  287. }
  288. atomic_set(&new->ua_count, 0);
  289. spin_lock_init(&new->ua_lock);
  290. INIT_LIST_HEAD(&new->ua_list);
  291. INIT_LIST_HEAD(&new->lun_link);
  292. new->mapped_lun = mapped_lun;
  293. kref_init(&new->pr_kref);
  294. init_completion(&new->pr_comp);
  295. new->lun_access_ro = lun_access_ro;
  296. new->creation_time = get_jiffies_64();
  297. new->attach_count++;
  298. mutex_lock(&nacl->lun_entry_mutex);
  299. orig = target_nacl_find_deve(nacl, mapped_lun);
  300. if (orig && orig->se_lun) {
  301. struct se_lun *orig_lun = rcu_dereference_check(orig->se_lun,
  302. lockdep_is_held(&nacl->lun_entry_mutex));
  303. if (orig_lun != lun) {
  304. pr_err("Existing orig->se_lun doesn't match new lun"
  305. " for dynamic -> explicit NodeACL conversion:"
  306. " %s\n", nacl->initiatorname);
  307. mutex_unlock(&nacl->lun_entry_mutex);
  308. kfree(new);
  309. return -EINVAL;
  310. }
  311. if (orig->se_lun_acl != NULL) {
  312. pr_warn_ratelimited("Detected existing explicit"
  313. " se_lun_acl->se_lun_group reference for %s"
  314. " mapped_lun: %llu, failing\n",
  315. nacl->initiatorname, mapped_lun);
  316. mutex_unlock(&nacl->lun_entry_mutex);
  317. kfree(new);
  318. return -EINVAL;
  319. }
  320. rcu_assign_pointer(new->se_lun, lun);
  321. rcu_assign_pointer(new->se_lun_acl, lun_acl);
  322. hlist_del_rcu(&orig->link);
  323. hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
  324. mutex_unlock(&nacl->lun_entry_mutex);
  325. spin_lock(&lun->lun_deve_lock);
  326. list_del(&orig->lun_link);
  327. list_add_tail(&new->lun_link, &lun->lun_deve_list);
  328. spin_unlock(&lun->lun_deve_lock);
  329. kref_put(&orig->pr_kref, target_pr_kref_release);
  330. wait_for_completion(&orig->pr_comp);
  331. target_luns_data_has_changed(nacl, new, true);
  332. kfree_rcu(orig, rcu_head);
  333. return 0;
  334. }
  335. rcu_assign_pointer(new->se_lun, lun);
  336. rcu_assign_pointer(new->se_lun_acl, lun_acl);
  337. hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
  338. mutex_unlock(&nacl->lun_entry_mutex);
  339. spin_lock(&lun->lun_deve_lock);
  340. list_add_tail(&new->lun_link, &lun->lun_deve_list);
  341. spin_unlock(&lun->lun_deve_lock);
  342. target_luns_data_has_changed(nacl, new, true);
  343. return 0;
  344. }
  345. /*
  346. * Called with se_node_acl->lun_entry_mutex held.
  347. */
  348. void core_disable_device_list_for_node(
  349. struct se_lun *lun,
  350. struct se_dev_entry *orig,
  351. struct se_node_acl *nacl,
  352. struct se_portal_group *tpg)
  353. {
  354. /*
  355. * rcu_dereference_raw protected by se_lun->lun_group symlink
  356. * reference to se_device->dev_group.
  357. */
  358. struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
  359. /*
  360. * If the MappedLUN entry is being disabled, the entry in
  361. * lun->lun_deve_list must be removed now before clearing the
  362. * struct se_dev_entry pointers below as logic in
  363. * core_alua_do_transition_tg_pt() depends on these being present.
  364. *
  365. * deve->se_lun_acl will be NULL for demo-mode created LUNs
  366. * that have not been explicitly converted to MappedLUNs ->
  367. * struct se_lun_acl, but we remove deve->lun_link from
  368. * lun->lun_deve_list. This also means that active UAs and
  369. * NodeACL context specific PR metadata for demo-mode
  370. * MappedLUN *deve will be released below..
  371. */
  372. spin_lock(&lun->lun_deve_lock);
  373. list_del(&orig->lun_link);
  374. spin_unlock(&lun->lun_deve_lock);
  375. /*
  376. * Disable struct se_dev_entry LUN ACL mapping
  377. */
  378. core_scsi3_ua_release_all(orig);
  379. hlist_del_rcu(&orig->link);
  380. clear_bit(DEF_PR_REG_ACTIVE, &orig->deve_flags);
  381. orig->lun_access_ro = false;
  382. orig->creation_time = 0;
  383. orig->attach_count--;
  384. /*
  385. * Before firing off RCU callback, wait for any in process SPEC_I_PT=1
  386. * or REGISTER_AND_MOVE PR operation to complete.
  387. */
  388. kref_put(&orig->pr_kref, target_pr_kref_release);
  389. wait_for_completion(&orig->pr_comp);
  390. rcu_assign_pointer(orig->se_lun, NULL);
  391. rcu_assign_pointer(orig->se_lun_acl, NULL);
  392. kfree_rcu(orig, rcu_head);
  393. core_scsi3_free_pr_reg_from_nacl(dev, nacl);
  394. target_luns_data_has_changed(nacl, NULL, false);
  395. }
  396. /* core_clear_lun_from_tpg():
  397. *
  398. *
  399. */
  400. void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
  401. {
  402. struct se_node_acl *nacl;
  403. struct se_dev_entry *deve;
  404. mutex_lock(&tpg->acl_node_mutex);
  405. list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
  406. mutex_lock(&nacl->lun_entry_mutex);
  407. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  408. struct se_lun *tmp_lun = rcu_dereference_check(deve->se_lun,
  409. lockdep_is_held(&nacl->lun_entry_mutex));
  410. if (lun != tmp_lun)
  411. continue;
  412. core_disable_device_list_for_node(lun, deve, nacl, tpg);
  413. }
  414. mutex_unlock(&nacl->lun_entry_mutex);
  415. }
  416. mutex_unlock(&tpg->acl_node_mutex);
  417. }
  418. int core_alloc_rtpi(struct se_lun *lun, struct se_device *dev)
  419. {
  420. struct se_lun *tmp;
  421. spin_lock(&dev->se_port_lock);
  422. if (dev->export_count == 0x0000ffff) {
  423. pr_warn("Reached dev->dev_port_count =="
  424. " 0x0000ffff\n");
  425. spin_unlock(&dev->se_port_lock);
  426. return -ENOSPC;
  427. }
  428. again:
  429. /*
  430. * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
  431. * Here is the table from spc4r17 section 7.7.3.8.
  432. *
  433. * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
  434. *
  435. * Code Description
  436. * 0h Reserved
  437. * 1h Relative port 1, historically known as port A
  438. * 2h Relative port 2, historically known as port B
  439. * 3h to FFFFh Relative port 3 through 65 535
  440. */
  441. lun->lun_rtpi = dev->dev_rpti_counter++;
  442. if (!lun->lun_rtpi)
  443. goto again;
  444. list_for_each_entry(tmp, &dev->dev_sep_list, lun_dev_link) {
  445. /*
  446. * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
  447. * for 16-bit wrap..
  448. */
  449. if (lun->lun_rtpi == tmp->lun_rtpi)
  450. goto again;
  451. }
  452. spin_unlock(&dev->se_port_lock);
  453. return 0;
  454. }
  455. static void se_release_vpd_for_dev(struct se_device *dev)
  456. {
  457. struct t10_vpd *vpd, *vpd_tmp;
  458. spin_lock(&dev->t10_wwn.t10_vpd_lock);
  459. list_for_each_entry_safe(vpd, vpd_tmp,
  460. &dev->t10_wwn.t10_vpd_list, vpd_list) {
  461. list_del(&vpd->vpd_list);
  462. kfree(vpd);
  463. }
  464. spin_unlock(&dev->t10_wwn.t10_vpd_lock);
  465. }
  466. static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
  467. {
  468. u32 aligned_max_sectors;
  469. u32 alignment;
  470. /*
  471. * Limit max_sectors to a PAGE_SIZE aligned value for modern
  472. * transport_allocate_data_tasks() operation.
  473. */
  474. alignment = max(1ul, PAGE_SIZE / block_size);
  475. aligned_max_sectors = rounddown(max_sectors, alignment);
  476. if (max_sectors != aligned_max_sectors)
  477. pr_info("Rounding down aligned max_sectors from %u to %u\n",
  478. max_sectors, aligned_max_sectors);
  479. return aligned_max_sectors;
  480. }
  481. int core_dev_add_lun(
  482. struct se_portal_group *tpg,
  483. struct se_device *dev,
  484. struct se_lun *lun)
  485. {
  486. int rc;
  487. rc = core_tpg_add_lun(tpg, lun, false, dev);
  488. if (rc < 0)
  489. return rc;
  490. pr_debug("%s_TPG[%u]_LUN[%llu] - Activated %s Logical Unit from"
  491. " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  492. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  493. tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
  494. /*
  495. * Update LUN maps for dynamically added initiators when
  496. * generate_node_acl is enabled.
  497. */
  498. if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
  499. struct se_node_acl *acl;
  500. mutex_lock(&tpg->acl_node_mutex);
  501. list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
  502. if (acl->dynamic_node_acl &&
  503. (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
  504. !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
  505. core_tpg_add_node_to_devs(acl, tpg, lun);
  506. }
  507. }
  508. mutex_unlock(&tpg->acl_node_mutex);
  509. }
  510. return 0;
  511. }
  512. /* core_dev_del_lun():
  513. *
  514. *
  515. */
  516. void core_dev_del_lun(
  517. struct se_portal_group *tpg,
  518. struct se_lun *lun)
  519. {
  520. pr_debug("%s_TPG[%u]_LUN[%llu] - Deactivating %s Logical Unit from"
  521. " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
  522. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  523. tpg->se_tpg_tfo->get_fabric_name());
  524. core_tpg_remove_lun(tpg, lun);
  525. }
  526. struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
  527. struct se_portal_group *tpg,
  528. struct se_node_acl *nacl,
  529. u64 mapped_lun,
  530. int *ret)
  531. {
  532. struct se_lun_acl *lacl;
  533. if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
  534. pr_err("%s InitiatorName exceeds maximum size.\n",
  535. tpg->se_tpg_tfo->get_fabric_name());
  536. *ret = -EOVERFLOW;
  537. return NULL;
  538. }
  539. lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
  540. if (!lacl) {
  541. pr_err("Unable to allocate memory for struct se_lun_acl.\n");
  542. *ret = -ENOMEM;
  543. return NULL;
  544. }
  545. lacl->mapped_lun = mapped_lun;
  546. lacl->se_lun_nacl = nacl;
  547. return lacl;
  548. }
  549. int core_dev_add_initiator_node_lun_acl(
  550. struct se_portal_group *tpg,
  551. struct se_lun_acl *lacl,
  552. struct se_lun *lun,
  553. bool lun_access_ro)
  554. {
  555. struct se_node_acl *nacl = lacl->se_lun_nacl;
  556. /*
  557. * rcu_dereference_raw protected by se_lun->lun_group symlink
  558. * reference to se_device->dev_group.
  559. */
  560. struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
  561. if (!nacl)
  562. return -EINVAL;
  563. if (lun->lun_access_ro)
  564. lun_access_ro = true;
  565. lacl->se_lun = lun;
  566. if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
  567. lun_access_ro, 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_ro ? "RO" : "RW",
  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_completion(&xcopy_lun->lun_shutdown_comp);
  715. INIT_LIST_HEAD(&xcopy_lun->lun_deve_list);
  716. INIT_LIST_HEAD(&xcopy_lun->lun_dev_link);
  717. mutex_init(&xcopy_lun->lun_tg_pt_md_mutex);
  718. xcopy_lun->lun_tpg = &xcopy_pt_tpg;
  719. return dev;
  720. }
  721. /*
  722. * Check if the underlying struct block_device request_queue supports
  723. * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
  724. * in ATA and we need to set TPE=1
  725. */
  726. bool target_configure_unmap_from_queue(struct se_dev_attrib *attrib,
  727. struct request_queue *q)
  728. {
  729. int block_size = queue_logical_block_size(q);
  730. if (!blk_queue_discard(q))
  731. return false;
  732. attrib->max_unmap_lba_count =
  733. q->limits.max_discard_sectors >> (ilog2(block_size) - 9);
  734. /*
  735. * Currently hardcoded to 1 in Linux/SCSI code..
  736. */
  737. attrib->max_unmap_block_desc_count = 1;
  738. attrib->unmap_granularity = q->limits.discard_granularity / block_size;
  739. attrib->unmap_granularity_alignment = q->limits.discard_alignment /
  740. block_size;
  741. attrib->unmap_zeroes_data = q->limits.discard_zeroes_data;
  742. return true;
  743. }
  744. EXPORT_SYMBOL(target_configure_unmap_from_queue);
  745. /*
  746. * Convert from blocksize advertised to the initiator to the 512 byte
  747. * units unconditionally used by the Linux block layer.
  748. */
  749. sector_t target_to_linux_sector(struct se_device *dev, sector_t lb)
  750. {
  751. switch (dev->dev_attrib.block_size) {
  752. case 4096:
  753. return lb << 3;
  754. case 2048:
  755. return lb << 2;
  756. case 1024:
  757. return lb << 1;
  758. default:
  759. return lb;
  760. }
  761. }
  762. EXPORT_SYMBOL(target_to_linux_sector);
  763. int target_configure_device(struct se_device *dev)
  764. {
  765. struct se_hba *hba = dev->se_hba;
  766. int ret;
  767. if (dev->dev_flags & DF_CONFIGURED) {
  768. pr_err("se_dev->se_dev_ptr already set for storage"
  769. " object\n");
  770. return -EEXIST;
  771. }
  772. ret = dev->transport->configure_device(dev);
  773. if (ret)
  774. goto out;
  775. /*
  776. * XXX: there is not much point to have two different values here..
  777. */
  778. dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
  779. dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
  780. /*
  781. * Align max_hw_sectors down to PAGE_SIZE I/O transfers
  782. */
  783. dev->dev_attrib.hw_max_sectors =
  784. se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
  785. dev->dev_attrib.hw_block_size);
  786. dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors;
  787. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  788. dev->creation_time = get_jiffies_64();
  789. ret = core_setup_alua(dev);
  790. if (ret)
  791. goto out;
  792. /*
  793. * Startup the struct se_device processing thread
  794. */
  795. dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
  796. dev->transport->name);
  797. if (!dev->tmr_wq) {
  798. pr_err("Unable to create tmr workqueue for %s\n",
  799. dev->transport->name);
  800. ret = -ENOMEM;
  801. goto out_free_alua;
  802. }
  803. /*
  804. * Setup work_queue for QUEUE_FULL
  805. */
  806. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  807. /*
  808. * Preload the initial INQUIRY const values if we are doing
  809. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  810. * passthrough because this is being provided by the backend LLD.
  811. */
  812. if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)) {
  813. strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
  814. strncpy(&dev->t10_wwn.model[0],
  815. dev->transport->inquiry_prod, 16);
  816. strncpy(&dev->t10_wwn.revision[0],
  817. dev->transport->inquiry_rev, 4);
  818. }
  819. scsi_dump_inquiry(dev);
  820. spin_lock(&hba->device_lock);
  821. hba->dev_count++;
  822. spin_unlock(&hba->device_lock);
  823. mutex_lock(&g_device_mutex);
  824. list_add_tail(&dev->g_dev_node, &g_device_list);
  825. mutex_unlock(&g_device_mutex);
  826. dev->dev_flags |= DF_CONFIGURED;
  827. return 0;
  828. out_free_alua:
  829. core_alua_free_lu_gp_mem(dev);
  830. out:
  831. se_release_vpd_for_dev(dev);
  832. return ret;
  833. }
  834. void target_free_device(struct se_device *dev)
  835. {
  836. struct se_hba *hba = dev->se_hba;
  837. WARN_ON(!list_empty(&dev->dev_sep_list));
  838. if (dev->dev_flags & DF_CONFIGURED) {
  839. destroy_workqueue(dev->tmr_wq);
  840. mutex_lock(&g_device_mutex);
  841. list_del(&dev->g_dev_node);
  842. mutex_unlock(&g_device_mutex);
  843. spin_lock(&hba->device_lock);
  844. hba->dev_count--;
  845. spin_unlock(&hba->device_lock);
  846. }
  847. core_alua_free_lu_gp_mem(dev);
  848. core_alua_set_lba_map(dev, NULL, 0, 0);
  849. core_scsi3_free_all_registrations(dev);
  850. se_release_vpd_for_dev(dev);
  851. if (dev->transport->free_prot)
  852. dev->transport->free_prot(dev);
  853. dev->transport->free_device(dev);
  854. }
  855. int core_dev_setup_virtual_lun0(void)
  856. {
  857. struct se_hba *hba;
  858. struct se_device *dev;
  859. char buf[] = "rd_pages=8,rd_nullio=1";
  860. int ret;
  861. hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
  862. if (IS_ERR(hba))
  863. return PTR_ERR(hba);
  864. dev = target_alloc_device(hba, "virt_lun0");
  865. if (!dev) {
  866. ret = -ENOMEM;
  867. goto out_free_hba;
  868. }
  869. hba->backend->ops->set_configfs_dev_params(dev, buf, sizeof(buf));
  870. ret = target_configure_device(dev);
  871. if (ret)
  872. goto out_free_se_dev;
  873. lun0_hba = hba;
  874. g_lun0_dev = dev;
  875. return 0;
  876. out_free_se_dev:
  877. target_free_device(dev);
  878. out_free_hba:
  879. core_delete_hba(hba);
  880. return ret;
  881. }
  882. void core_dev_release_virtual_lun0(void)
  883. {
  884. struct se_hba *hba = lun0_hba;
  885. if (!hba)
  886. return;
  887. if (g_lun0_dev)
  888. target_free_device(g_lun0_dev);
  889. core_delete_hba(hba);
  890. }
  891. /*
  892. * Common CDB parsing for kernel and user passthrough.
  893. */
  894. sense_reason_t
  895. passthrough_parse_cdb(struct se_cmd *cmd,
  896. sense_reason_t (*exec_cmd)(struct se_cmd *cmd))
  897. {
  898. unsigned char *cdb = cmd->t_task_cdb;
  899. /*
  900. * For REPORT LUNS we always need to emulate the response, for everything
  901. * else, pass it up.
  902. */
  903. if (cdb[0] == REPORT_LUNS) {
  904. cmd->execute_cmd = spc_emulate_report_luns;
  905. return TCM_NO_SENSE;
  906. }
  907. /* Set DATA_CDB flag for ops that should have it */
  908. switch (cdb[0]) {
  909. case READ_6:
  910. case READ_10:
  911. case READ_12:
  912. case READ_16:
  913. case WRITE_6:
  914. case WRITE_10:
  915. case WRITE_12:
  916. case WRITE_16:
  917. case WRITE_VERIFY:
  918. case WRITE_VERIFY_12:
  919. case 0x8e: /* WRITE_VERIFY_16 */
  920. case COMPARE_AND_WRITE:
  921. case XDWRITEREAD_10:
  922. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  923. break;
  924. case VARIABLE_LENGTH_CMD:
  925. switch (get_unaligned_be16(&cdb[8])) {
  926. case READ_32:
  927. case WRITE_32:
  928. case 0x0c: /* WRITE_VERIFY_32 */
  929. case XDWRITEREAD_32:
  930. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  931. break;
  932. }
  933. }
  934. cmd->execute_cmd = exec_cmd;
  935. return TCM_NO_SENSE;
  936. }
  937. EXPORT_SYMBOL(passthrough_parse_cdb);