target_core_device.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*******************************************************************************
  3. * Filename: target_core_device.c (based on iscsi_target_device.c)
  4. *
  5. * This file contains the TCM Virtual Device and Disk Transport
  6. * agnostic related functions.
  7. *
  8. * (c) Copyright 2003-2013 Datera, Inc.
  9. *
  10. * Nicholas A. Bellinger <nab@kernel.org>
  11. *
  12. ******************************************************************************/
  13. #include <linux/net.h>
  14. #include <linux/string.h>
  15. #include <linux/delay.h>
  16. #include <linux/timer.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/kthread.h>
  20. #include <linux/in.h>
  21. #include <linux/export.h>
  22. #include <linux/t10-pi.h>
  23. #include <linux/unaligned.h>
  24. #include <net/sock.h>
  25. #include <net/tcp.h>
  26. #include <scsi/scsi_common.h>
  27. #include <scsi/scsi_proto.h>
  28. #include <target/target_core_base.h>
  29. #include <target/target_core_backend.h>
  30. #include <target/target_core_fabric.h>
  31. #include "target_core_internal.h"
  32. #include "target_core_alua.h"
  33. #include "target_core_pr.h"
  34. #include "target_core_ua.h"
  35. static DEFINE_MUTEX(device_mutex);
  36. static DEFINE_IDR(devices_idr);
  37. static struct se_hba *lun0_hba;
  38. /* not static, needed by tpg.c */
  39. struct se_device *g_lun0_dev;
  40. sense_reason_t
  41. transport_lookup_cmd_lun(struct se_cmd *se_cmd)
  42. {
  43. struct se_lun *se_lun = NULL;
  44. struct se_session *se_sess = se_cmd->se_sess;
  45. struct se_node_acl *nacl = se_sess->se_node_acl;
  46. struct se_dev_entry *deve;
  47. sense_reason_t ret = TCM_NO_SENSE;
  48. rcu_read_lock();
  49. deve = target_nacl_find_deve(nacl, se_cmd->orig_fe_lun);
  50. if (deve) {
  51. this_cpu_inc(deve->stats->total_cmds);
  52. if (se_cmd->data_direction == DMA_TO_DEVICE)
  53. this_cpu_add(deve->stats->write_bytes,
  54. se_cmd->data_length);
  55. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  56. this_cpu_add(deve->stats->read_bytes,
  57. se_cmd->data_length);
  58. if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
  59. deve->lun_access_ro) {
  60. pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
  61. " Access for 0x%08llx\n",
  62. se_cmd->se_tfo->fabric_name,
  63. se_cmd->orig_fe_lun);
  64. rcu_read_unlock();
  65. return TCM_WRITE_PROTECTED;
  66. }
  67. se_lun = deve->se_lun;
  68. if (!percpu_ref_tryget_live(&se_lun->lun_ref)) {
  69. se_lun = NULL;
  70. goto out_unlock;
  71. }
  72. se_cmd->se_lun = se_lun;
  73. se_cmd->pr_res_key = deve->pr_res_key;
  74. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  75. se_cmd->lun_ref_active = true;
  76. }
  77. out_unlock:
  78. rcu_read_unlock();
  79. if (!se_lun) {
  80. /*
  81. * Use the se_portal_group->tpg_virt_lun0 to allow for
  82. * REPORT_LUNS, et al to be returned when no active
  83. * MappedLUN=0 exists for this Initiator Port.
  84. */
  85. if (se_cmd->orig_fe_lun != 0) {
  86. pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  87. " Access for 0x%08llx from %s\n",
  88. se_cmd->se_tfo->fabric_name,
  89. se_cmd->orig_fe_lun,
  90. nacl->initiatorname);
  91. return TCM_NON_EXISTENT_LUN;
  92. }
  93. /*
  94. * Force WRITE PROTECT for virtual LUN 0
  95. */
  96. if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
  97. (se_cmd->data_direction != DMA_NONE))
  98. return TCM_WRITE_PROTECTED;
  99. se_lun = se_sess->se_tpg->tpg_virt_lun0;
  100. if (!percpu_ref_tryget_live(&se_lun->lun_ref))
  101. return TCM_NON_EXISTENT_LUN;
  102. se_cmd->se_lun = se_sess->se_tpg->tpg_virt_lun0;
  103. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  104. se_cmd->lun_ref_active = true;
  105. }
  106. /*
  107. * RCU reference protected by percpu se_lun->lun_ref taken above that
  108. * must drop to zero (including initial reference) before this se_lun
  109. * pointer can be kfree_rcu() by the final se_lun->lun_group put via
  110. * target_core_fabric_configfs.c:target_fabric_port_release
  111. */
  112. se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  113. this_cpu_inc(se_cmd->se_dev->stats->total_cmds);
  114. if (se_cmd->data_direction == DMA_TO_DEVICE)
  115. this_cpu_add(se_cmd->se_dev->stats->write_bytes,
  116. se_cmd->data_length);
  117. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  118. this_cpu_add(se_cmd->se_dev->stats->read_bytes,
  119. se_cmd->data_length);
  120. return ret;
  121. }
  122. EXPORT_SYMBOL(transport_lookup_cmd_lun);
  123. int transport_lookup_tmr_lun(struct se_cmd *se_cmd)
  124. {
  125. struct se_dev_entry *deve;
  126. struct se_lun *se_lun = NULL;
  127. struct se_session *se_sess = se_cmd->se_sess;
  128. struct se_node_acl *nacl = se_sess->se_node_acl;
  129. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  130. rcu_read_lock();
  131. deve = target_nacl_find_deve(nacl, se_cmd->orig_fe_lun);
  132. if (deve) {
  133. se_lun = deve->se_lun;
  134. if (!percpu_ref_tryget_live(&se_lun->lun_ref)) {
  135. se_lun = NULL;
  136. goto out_unlock;
  137. }
  138. se_cmd->se_lun = se_lun;
  139. se_cmd->pr_res_key = deve->pr_res_key;
  140. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  141. se_cmd->lun_ref_active = true;
  142. }
  143. out_unlock:
  144. rcu_read_unlock();
  145. if (!se_lun) {
  146. pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  147. " Access for 0x%08llx for %s\n",
  148. se_cmd->se_tfo->fabric_name,
  149. se_cmd->orig_fe_lun,
  150. nacl->initiatorname);
  151. return -ENODEV;
  152. }
  153. se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  154. se_tmr->tmr_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  155. return 0;
  156. }
  157. EXPORT_SYMBOL(transport_lookup_tmr_lun);
  158. bool target_lun_is_rdonly(struct se_cmd *cmd)
  159. {
  160. struct se_session *se_sess = cmd->se_sess;
  161. struct se_dev_entry *deve;
  162. bool ret;
  163. rcu_read_lock();
  164. deve = target_nacl_find_deve(se_sess->se_node_acl, cmd->orig_fe_lun);
  165. ret = deve && deve->lun_access_ro;
  166. rcu_read_unlock();
  167. return ret;
  168. }
  169. EXPORT_SYMBOL(target_lun_is_rdonly);
  170. /*
  171. * This function is called from core_scsi3_emulate_pro_register_and_move()
  172. * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_kref
  173. * when a matching rtpi is found.
  174. */
  175. struct se_dev_entry *core_get_se_deve_from_rtpi(
  176. struct se_node_acl *nacl,
  177. u16 rtpi)
  178. {
  179. struct se_dev_entry *deve;
  180. struct se_lun *lun;
  181. struct se_portal_group *tpg = nacl->se_tpg;
  182. rcu_read_lock();
  183. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  184. lun = deve->se_lun;
  185. if (!lun) {
  186. pr_err("%s device entries device pointer is"
  187. " NULL, but Initiator has access.\n",
  188. tpg->se_tpg_tfo->fabric_name);
  189. continue;
  190. }
  191. if (lun->lun_tpg->tpg_rtpi != rtpi)
  192. continue;
  193. kref_get(&deve->pr_kref);
  194. rcu_read_unlock();
  195. return deve;
  196. }
  197. rcu_read_unlock();
  198. return NULL;
  199. }
  200. void core_free_device_list_for_node(
  201. struct se_node_acl *nacl,
  202. struct se_portal_group *tpg)
  203. {
  204. struct se_dev_entry *deve;
  205. mutex_lock(&nacl->lun_entry_mutex);
  206. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
  207. core_disable_device_list_for_node(deve->se_lun, deve, nacl, tpg);
  208. mutex_unlock(&nacl->lun_entry_mutex);
  209. }
  210. void core_update_device_list_access(
  211. u64 mapped_lun,
  212. bool lun_access_ro,
  213. struct se_node_acl *nacl)
  214. {
  215. struct se_dev_entry *deve;
  216. mutex_lock(&nacl->lun_entry_mutex);
  217. deve = target_nacl_find_deve(nacl, mapped_lun);
  218. if (deve)
  219. deve->lun_access_ro = lun_access_ro;
  220. mutex_unlock(&nacl->lun_entry_mutex);
  221. }
  222. /*
  223. * Called with rcu_read_lock or nacl->device_list_lock held.
  224. */
  225. struct se_dev_entry *target_nacl_find_deve(struct se_node_acl *nacl, u64 mapped_lun)
  226. {
  227. struct se_dev_entry *deve;
  228. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
  229. if (deve->mapped_lun == mapped_lun)
  230. return deve;
  231. return NULL;
  232. }
  233. EXPORT_SYMBOL(target_nacl_find_deve);
  234. void target_pr_kref_release(struct kref *kref)
  235. {
  236. struct se_dev_entry *deve = container_of(kref, struct se_dev_entry,
  237. pr_kref);
  238. complete(&deve->pr_comp);
  239. }
  240. /*
  241. * Establish UA condition on SCSI device - all LUNs
  242. */
  243. void target_dev_ua_allocate(struct se_device *dev, u8 asc, u8 ascq)
  244. {
  245. struct se_dev_entry *se_deve;
  246. struct se_lun *lun;
  247. spin_lock(&dev->se_port_lock);
  248. list_for_each_entry(lun, &dev->dev_sep_list, lun_dev_link) {
  249. spin_lock(&lun->lun_deve_lock);
  250. list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link)
  251. core_scsi3_ua_allocate(se_deve, asc, ascq);
  252. spin_unlock(&lun->lun_deve_lock);
  253. }
  254. spin_unlock(&dev->se_port_lock);
  255. }
  256. static void
  257. target_luns_data_has_changed(struct se_node_acl *nacl, struct se_dev_entry *new,
  258. bool skip_new)
  259. {
  260. struct se_dev_entry *tmp;
  261. rcu_read_lock();
  262. hlist_for_each_entry_rcu(tmp, &nacl->lun_entry_hlist, link) {
  263. if (skip_new && tmp == new)
  264. continue;
  265. core_scsi3_ua_allocate(tmp, 0x3F,
  266. ASCQ_3FH_REPORTED_LUNS_DATA_HAS_CHANGED);
  267. }
  268. rcu_read_unlock();
  269. }
  270. int core_enable_device_list_for_node(
  271. struct se_lun *lun,
  272. struct se_lun_acl *lun_acl,
  273. u64 mapped_lun,
  274. bool lun_access_ro,
  275. struct se_node_acl *nacl,
  276. struct se_portal_group *tpg)
  277. {
  278. struct se_dev_entry *orig, *new;
  279. int ret = 0;
  280. new = kzalloc_obj(*new);
  281. if (!new) {
  282. pr_err("Unable to allocate se_dev_entry memory\n");
  283. return -ENOMEM;
  284. }
  285. new->stats = alloc_percpu(struct se_dev_entry_io_stats);
  286. if (!new->stats) {
  287. ret = -ENOMEM;
  288. goto free_deve;
  289. }
  290. spin_lock_init(&new->ua_lock);
  291. INIT_LIST_HEAD(&new->ua_list);
  292. INIT_LIST_HEAD(&new->lun_link);
  293. new->mapped_lun = mapped_lun;
  294. kref_init(&new->pr_kref);
  295. init_completion(&new->pr_comp);
  296. new->lun_access_ro = lun_access_ro;
  297. new->creation_time = get_jiffies_64();
  298. new->attach_count++;
  299. mutex_lock(&nacl->lun_entry_mutex);
  300. orig = target_nacl_find_deve(nacl, mapped_lun);
  301. if (orig && orig->se_lun) {
  302. struct se_lun *orig_lun = orig->se_lun;
  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. ret = -EINVAL;
  309. goto free_stats;
  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. ret = -EINVAL;
  318. goto free_stats;
  319. }
  320. new->se_lun = lun;
  321. 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. new->se_lun = lun;
  336. 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. free_stats:
  345. free_percpu(new->stats);
  346. free_deve:
  347. kfree(new);
  348. return ret;
  349. }
  350. static void target_free_dev_entry(struct rcu_head *head)
  351. {
  352. struct se_dev_entry *deve = container_of(head, struct se_dev_entry,
  353. rcu_head);
  354. free_percpu(deve->stats);
  355. kfree(deve);
  356. }
  357. void core_disable_device_list_for_node(
  358. struct se_lun *lun,
  359. struct se_dev_entry *orig,
  360. struct se_node_acl *nacl,
  361. struct se_portal_group *tpg)
  362. {
  363. /*
  364. * rcu_dereference_raw protected by se_lun->lun_group symlink
  365. * reference to se_device->dev_group.
  366. */
  367. struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
  368. lockdep_assert_held(&nacl->lun_entry_mutex);
  369. /*
  370. * If the MappedLUN entry is being disabled, the entry in
  371. * lun->lun_deve_list must be removed now before clearing the
  372. * struct se_dev_entry pointers below as logic in
  373. * core_alua_do_transition_tg_pt() depends on these being present.
  374. *
  375. * deve->se_lun_acl will be NULL for demo-mode created LUNs
  376. * that have not been explicitly converted to MappedLUNs ->
  377. * struct se_lun_acl, but we remove deve->lun_link from
  378. * lun->lun_deve_list. This also means that active UAs and
  379. * NodeACL context specific PR metadata for demo-mode
  380. * MappedLUN *deve will be released below..
  381. */
  382. spin_lock(&lun->lun_deve_lock);
  383. list_del(&orig->lun_link);
  384. spin_unlock(&lun->lun_deve_lock);
  385. /*
  386. * Disable struct se_dev_entry LUN ACL mapping
  387. */
  388. core_scsi3_ua_release_all(orig);
  389. hlist_del_rcu(&orig->link);
  390. clear_bit(DEF_PR_REG_ACTIVE, &orig->deve_flags);
  391. orig->lun_access_ro = false;
  392. orig->creation_time = 0;
  393. orig->attach_count--;
  394. /*
  395. * Before firing off RCU callback, wait for any in process SPEC_I_PT=1
  396. * or REGISTER_AND_MOVE PR operation to complete.
  397. */
  398. kref_put(&orig->pr_kref, target_pr_kref_release);
  399. wait_for_completion(&orig->pr_comp);
  400. call_rcu(&orig->rcu_head, target_free_dev_entry);
  401. core_scsi3_free_pr_reg_from_nacl(dev, nacl);
  402. target_luns_data_has_changed(nacl, NULL, false);
  403. }
  404. /* core_clear_lun_from_tpg():
  405. *
  406. *
  407. */
  408. void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
  409. {
  410. struct se_node_acl *nacl;
  411. struct se_dev_entry *deve;
  412. mutex_lock(&tpg->acl_node_mutex);
  413. list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
  414. mutex_lock(&nacl->lun_entry_mutex);
  415. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  416. if (lun != deve->se_lun)
  417. continue;
  418. core_disable_device_list_for_node(lun, deve, nacl, tpg);
  419. }
  420. mutex_unlock(&nacl->lun_entry_mutex);
  421. }
  422. mutex_unlock(&tpg->acl_node_mutex);
  423. }
  424. static void se_release_vpd_for_dev(struct se_device *dev)
  425. {
  426. struct t10_vpd *vpd, *vpd_tmp;
  427. spin_lock(&dev->t10_wwn.t10_vpd_lock);
  428. list_for_each_entry_safe(vpd, vpd_tmp,
  429. &dev->t10_wwn.t10_vpd_list, vpd_list) {
  430. list_del(&vpd->vpd_list);
  431. kfree(vpd);
  432. }
  433. spin_unlock(&dev->t10_wwn.t10_vpd_lock);
  434. }
  435. static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
  436. {
  437. u32 aligned_max_sectors;
  438. u32 alignment;
  439. /*
  440. * Limit max_sectors to a PAGE_SIZE aligned value for modern
  441. * transport_allocate_data_tasks() operation.
  442. */
  443. alignment = max(1ul, PAGE_SIZE / block_size);
  444. aligned_max_sectors = rounddown(max_sectors, alignment);
  445. if (max_sectors != aligned_max_sectors)
  446. pr_info("Rounding down aligned max_sectors from %u to %u\n",
  447. max_sectors, aligned_max_sectors);
  448. return aligned_max_sectors;
  449. }
  450. int core_dev_add_lun(
  451. struct se_portal_group *tpg,
  452. struct se_device *dev,
  453. struct se_lun *lun)
  454. {
  455. int rc;
  456. rc = core_tpg_add_lun(tpg, lun, false, dev);
  457. if (rc < 0)
  458. return rc;
  459. pr_debug("%s_TPG[%u]_LUN[%llu] - Activated %s Logical Unit from"
  460. " CORE HBA: %u\n", tpg->se_tpg_tfo->fabric_name,
  461. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  462. tpg->se_tpg_tfo->fabric_name, dev->se_hba->hba_id);
  463. /*
  464. * Update LUN maps for dynamically added initiators when
  465. * generate_node_acl is enabled.
  466. */
  467. if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
  468. struct se_node_acl *acl;
  469. mutex_lock(&tpg->acl_node_mutex);
  470. list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
  471. if (acl->dynamic_node_acl &&
  472. (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
  473. !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
  474. core_tpg_add_node_to_devs(acl, tpg, lun);
  475. }
  476. }
  477. mutex_unlock(&tpg->acl_node_mutex);
  478. }
  479. return 0;
  480. }
  481. /* core_dev_del_lun():
  482. *
  483. *
  484. */
  485. void core_dev_del_lun(
  486. struct se_portal_group *tpg,
  487. struct se_lun *lun)
  488. {
  489. pr_debug("%s_TPG[%u]_LUN[%llu] - Deactivating %s Logical Unit from"
  490. " device object\n", tpg->se_tpg_tfo->fabric_name,
  491. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  492. tpg->se_tpg_tfo->fabric_name);
  493. core_tpg_remove_lun(tpg, lun);
  494. }
  495. struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
  496. struct se_portal_group *tpg,
  497. struct se_node_acl *nacl,
  498. u64 mapped_lun,
  499. int *ret)
  500. {
  501. struct se_lun_acl *lacl;
  502. if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
  503. pr_err("%s InitiatorName exceeds maximum size.\n",
  504. tpg->se_tpg_tfo->fabric_name);
  505. *ret = -EOVERFLOW;
  506. return NULL;
  507. }
  508. lacl = kzalloc_obj(struct se_lun_acl);
  509. if (!lacl) {
  510. pr_err("Unable to allocate memory for struct se_lun_acl.\n");
  511. *ret = -ENOMEM;
  512. return NULL;
  513. }
  514. lacl->mapped_lun = mapped_lun;
  515. lacl->se_lun_nacl = nacl;
  516. return lacl;
  517. }
  518. int core_dev_add_initiator_node_lun_acl(
  519. struct se_portal_group *tpg,
  520. struct se_lun_acl *lacl,
  521. struct se_lun *lun,
  522. bool lun_access_ro)
  523. {
  524. struct se_node_acl *nacl = lacl->se_lun_nacl;
  525. /*
  526. * rcu_dereference_raw protected by se_lun->lun_group symlink
  527. * reference to se_device->dev_group.
  528. */
  529. struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
  530. if (!nacl)
  531. return -EINVAL;
  532. if (lun->lun_access_ro)
  533. lun_access_ro = true;
  534. lacl->se_lun = lun;
  535. if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
  536. lun_access_ro, nacl, tpg) < 0)
  537. return -EINVAL;
  538. pr_debug("%s_TPG[%hu]_LUN[%llu->%llu] - Added %s ACL for "
  539. " InitiatorNode: %s\n", tpg->se_tpg_tfo->fabric_name,
  540. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun, lacl->mapped_lun,
  541. lun_access_ro ? "RO" : "RW",
  542. nacl->initiatorname);
  543. /*
  544. * Check to see if there are any existing persistent reservation APTPL
  545. * pre-registrations that need to be enabled for this LUN ACL..
  546. */
  547. core_scsi3_check_aptpl_registration(dev, tpg, lun, nacl,
  548. lacl->mapped_lun);
  549. return 0;
  550. }
  551. int core_dev_del_initiator_node_lun_acl(
  552. struct se_lun *lun,
  553. struct se_lun_acl *lacl)
  554. {
  555. struct se_portal_group *tpg = lun->lun_tpg;
  556. struct se_node_acl *nacl;
  557. struct se_dev_entry *deve;
  558. nacl = lacl->se_lun_nacl;
  559. if (!nacl)
  560. return -EINVAL;
  561. mutex_lock(&nacl->lun_entry_mutex);
  562. deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
  563. if (deve)
  564. core_disable_device_list_for_node(lun, deve, nacl, tpg);
  565. mutex_unlock(&nacl->lun_entry_mutex);
  566. pr_debug("%s_TPG[%hu]_LUN[%llu] - Removed ACL for"
  567. " InitiatorNode: %s Mapped LUN: %llu\n",
  568. tpg->se_tpg_tfo->fabric_name,
  569. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  570. nacl->initiatorname, lacl->mapped_lun);
  571. return 0;
  572. }
  573. void core_dev_free_initiator_node_lun_acl(
  574. struct se_portal_group *tpg,
  575. struct se_lun_acl *lacl)
  576. {
  577. pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
  578. " Mapped LUN: %llu\n", tpg->se_tpg_tfo->fabric_name,
  579. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  580. tpg->se_tpg_tfo->fabric_name,
  581. lacl->se_lun_nacl->initiatorname, lacl->mapped_lun);
  582. kfree(lacl);
  583. }
  584. static void scsi_dump_inquiry(struct se_device *dev)
  585. {
  586. struct t10_wwn *wwn = &dev->t10_wwn;
  587. int device_type = dev->transport->get_device_type(dev);
  588. /*
  589. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  590. */
  591. pr_debug(" Vendor: %-" __stringify(INQUIRY_VENDOR_LEN) "s\n",
  592. wwn->vendor);
  593. pr_debug(" Model: %-" __stringify(INQUIRY_MODEL_LEN) "s\n",
  594. wwn->model);
  595. pr_debug(" Revision: %-" __stringify(INQUIRY_REVISION_LEN) "s\n",
  596. wwn->revision);
  597. pr_debug(" Type: %s ", scsi_device_type(device_type));
  598. }
  599. static void target_non_ordered_release(struct percpu_ref *ref)
  600. {
  601. struct se_device *dev = container_of(ref, struct se_device,
  602. non_ordered);
  603. unsigned long flags;
  604. spin_lock_irqsave(&dev->delayed_cmd_lock, flags);
  605. if (!list_empty(&dev->delayed_cmd_list))
  606. schedule_work(&dev->delayed_cmd_work);
  607. spin_unlock_irqrestore(&dev->delayed_cmd_lock, flags);
  608. }
  609. struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
  610. {
  611. struct se_device *dev;
  612. struct se_lun *xcopy_lun;
  613. int i;
  614. dev = hba->backend->ops->alloc_device(hba, name);
  615. if (!dev)
  616. return NULL;
  617. dev->stats = alloc_percpu(struct se_dev_io_stats);
  618. if (!dev->stats)
  619. goto free_device;
  620. dev->queues = kzalloc_objs(*dev->queues, nr_cpu_ids);
  621. if (!dev->queues)
  622. goto free_stats;
  623. dev->queue_cnt = nr_cpu_ids;
  624. for (i = 0; i < dev->queue_cnt; i++) {
  625. struct se_device_queue *q;
  626. q = &dev->queues[i];
  627. INIT_LIST_HEAD(&q->state_list);
  628. spin_lock_init(&q->lock);
  629. init_llist_head(&q->sq.cmd_list);
  630. INIT_WORK(&q->sq.work, target_queued_submit_work);
  631. }
  632. if (percpu_ref_init(&dev->non_ordered, target_non_ordered_release,
  633. PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
  634. goto free_queues;
  635. dev->se_hba = hba;
  636. dev->transport = hba->backend->ops;
  637. dev->transport_flags = dev->transport->transport_flags_default;
  638. dev->prot_length = sizeof(struct t10_pi_tuple);
  639. dev->hba_index = hba->hba_index;
  640. INIT_LIST_HEAD(&dev->dev_sep_list);
  641. INIT_LIST_HEAD(&dev->dev_tmr_list);
  642. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  643. INIT_LIST_HEAD(&dev->qf_cmd_list);
  644. spin_lock_init(&dev->delayed_cmd_lock);
  645. spin_lock_init(&dev->dev_reservation_lock);
  646. spin_lock_init(&dev->se_port_lock);
  647. spin_lock_init(&dev->se_tmr_lock);
  648. spin_lock_init(&dev->qf_cmd_lock);
  649. sema_init(&dev->caw_sem, 1);
  650. INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
  651. spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
  652. INIT_LIST_HEAD(&dev->t10_pr.registration_list);
  653. INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
  654. spin_lock_init(&dev->t10_pr.registration_lock);
  655. spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
  656. INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
  657. spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
  658. INIT_LIST_HEAD(&dev->t10_alua.lba_map_list);
  659. spin_lock_init(&dev->t10_alua.lba_map_lock);
  660. INIT_WORK(&dev->delayed_cmd_work, target_do_delayed_work);
  661. mutex_init(&dev->lun_reset_mutex);
  662. dev->t10_wwn.t10_dev = dev;
  663. /*
  664. * Use OpenFabrics IEEE Company ID: 00 14 05
  665. */
  666. dev->t10_wwn.company_id = 0x001405;
  667. dev->t10_alua.t10_dev = dev;
  668. dev->dev_attrib.da_dev = dev;
  669. dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
  670. dev->dev_attrib.emulate_dpo = 1;
  671. dev->dev_attrib.emulate_fua_write = 1;
  672. dev->dev_attrib.emulate_fua_read = 1;
  673. dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
  674. dev->dev_attrib.emulate_ua_intlck_ctrl = TARGET_UA_INTLCK_CTRL_CLEAR;
  675. dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
  676. dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
  677. dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
  678. dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
  679. dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
  680. dev->dev_attrib.emulate_pr = DA_EMULATE_PR;
  681. dev->dev_attrib.emulate_rsoc = DA_EMULATE_RSOC;
  682. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE0_PROT;
  683. dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
  684. dev->dev_attrib.force_pr_aptpl = DA_FORCE_PR_APTPL;
  685. dev->dev_attrib.is_nonrot = DA_IS_NONROT;
  686. dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
  687. dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
  688. dev->dev_attrib.max_unmap_block_desc_count =
  689. DA_MAX_UNMAP_BLOCK_DESC_COUNT;
  690. dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
  691. dev->dev_attrib.unmap_granularity_alignment =
  692. DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
  693. dev->dev_attrib.unmap_zeroes_data =
  694. DA_UNMAP_ZEROES_DATA_DEFAULT;
  695. dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
  696. dev->dev_attrib.submit_type = TARGET_FABRIC_DEFAULT_SUBMIT;
  697. /* Skip allocating lun_stats since we can't export them. */
  698. xcopy_lun = &dev->xcopy_lun;
  699. rcu_assign_pointer(xcopy_lun->lun_se_dev, dev);
  700. init_completion(&xcopy_lun->lun_shutdown_comp);
  701. INIT_LIST_HEAD(&xcopy_lun->lun_deve_list);
  702. INIT_LIST_HEAD(&xcopy_lun->lun_dev_link);
  703. mutex_init(&xcopy_lun->lun_tg_pt_md_mutex);
  704. xcopy_lun->lun_tpg = &xcopy_pt_tpg;
  705. /* Preload the default INQUIRY const values */
  706. strscpy(dev->t10_wwn.vendor, "LIO-ORG", sizeof(dev->t10_wwn.vendor));
  707. strscpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
  708. sizeof(dev->t10_wwn.model));
  709. strscpy(dev->t10_wwn.revision, dev->transport->inquiry_rev,
  710. sizeof(dev->t10_wwn.revision));
  711. return dev;
  712. free_queues:
  713. kfree(dev->queues);
  714. free_stats:
  715. free_percpu(dev->stats);
  716. free_device:
  717. hba->backend->ops->free_device(dev);
  718. return NULL;
  719. }
  720. void target_configure_write_atomic_from_bdev(struct se_dev_attrib *attrib,
  721. struct block_device *bdev)
  722. {
  723. struct request_queue *q = bdev_get_queue(bdev);
  724. int block_size = bdev_logical_block_size(bdev);
  725. if (!bdev_can_atomic_write(bdev))
  726. return;
  727. attrib->atomic_max_len = queue_atomic_write_max_bytes(q) / block_size;
  728. attrib->atomic_granularity = attrib->atomic_alignment =
  729. queue_atomic_write_unit_min_bytes(q) / block_size;
  730. attrib->atomic_max_with_boundary = 0;
  731. attrib->atomic_max_boundary = 0;
  732. }
  733. EXPORT_SYMBOL_GPL(target_configure_write_atomic_from_bdev);
  734. /*
  735. * Check if the underlying struct block_device supports discard and if yes
  736. * configure the UNMAP parameters.
  737. */
  738. bool target_configure_unmap_from_bdev(struct se_dev_attrib *attrib,
  739. struct block_device *bdev)
  740. {
  741. int block_size = bdev_logical_block_size(bdev);
  742. if (!bdev_max_discard_sectors(bdev))
  743. return false;
  744. attrib->max_unmap_lba_count =
  745. bdev_max_discard_sectors(bdev) >> (ilog2(block_size) - 9);
  746. /*
  747. * Currently hardcoded to 1 in Linux/SCSI code..
  748. */
  749. attrib->max_unmap_block_desc_count = 1;
  750. attrib->unmap_granularity = bdev_discard_granularity(bdev) / block_size;
  751. attrib->unmap_granularity_alignment =
  752. bdev_discard_alignment(bdev) / block_size;
  753. return true;
  754. }
  755. EXPORT_SYMBOL(target_configure_unmap_from_bdev);
  756. /*
  757. * Convert from blocksize advertised to the initiator to the 512 byte
  758. * units unconditionally used by the Linux block layer.
  759. */
  760. sector_t target_to_linux_sector(struct se_device *dev, sector_t lb)
  761. {
  762. switch (dev->dev_attrib.block_size) {
  763. case 4096:
  764. return lb << 3;
  765. case 2048:
  766. return lb << 2;
  767. case 1024:
  768. return lb << 1;
  769. default:
  770. return lb;
  771. }
  772. }
  773. EXPORT_SYMBOL(target_to_linux_sector);
  774. struct devices_idr_iter {
  775. int (*fn)(struct se_device *dev, void *data);
  776. void *data;
  777. };
  778. static int target_devices_idr_iter(int id, void *p, void *data)
  779. __must_hold(&device_mutex)
  780. {
  781. struct devices_idr_iter *iter = data;
  782. struct se_device *dev = p;
  783. struct config_item *item;
  784. int ret;
  785. /*
  786. * We add the device early to the idr, so it can be used
  787. * by backend modules during configuration. We do not want
  788. * to allow other callers to access partially setup devices,
  789. * so we skip them here.
  790. */
  791. if (!target_dev_configured(dev))
  792. return 0;
  793. item = config_item_get_unless_zero(&dev->dev_group.cg_item);
  794. if (!item)
  795. return 0;
  796. mutex_unlock(&device_mutex);
  797. ret = iter->fn(dev, iter->data);
  798. config_item_put(item);
  799. mutex_lock(&device_mutex);
  800. return ret;
  801. }
  802. /**
  803. * target_for_each_device - iterate over configured devices
  804. * @fn: iterator function
  805. * @data: pointer to data that will be passed to fn
  806. *
  807. * fn must return 0 to continue looping over devices. non-zero will break
  808. * from the loop and return that value to the caller.
  809. */
  810. int target_for_each_device(int (*fn)(struct se_device *dev, void *data),
  811. void *data)
  812. {
  813. struct devices_idr_iter iter = { .fn = fn, .data = data };
  814. int ret;
  815. mutex_lock(&device_mutex);
  816. ret = idr_for_each(&devices_idr, target_devices_idr_iter, &iter);
  817. mutex_unlock(&device_mutex);
  818. return ret;
  819. }
  820. int target_configure_device(struct se_device *dev)
  821. {
  822. struct se_hba *hba = dev->se_hba;
  823. int ret, id;
  824. if (target_dev_configured(dev)) {
  825. pr_err("se_dev->se_dev_ptr already set for storage"
  826. " object\n");
  827. return -EEXIST;
  828. }
  829. /*
  830. * Add early so modules like tcmu can use during its
  831. * configuration.
  832. */
  833. mutex_lock(&device_mutex);
  834. /*
  835. * Use cyclic to try and avoid collisions with devices
  836. * that were recently removed.
  837. */
  838. id = idr_alloc_cyclic(&devices_idr, dev, 0, INT_MAX, GFP_KERNEL);
  839. mutex_unlock(&device_mutex);
  840. if (id < 0) {
  841. ret = -ENOMEM;
  842. goto out;
  843. }
  844. dev->dev_index = id;
  845. ret = dev->transport->configure_device(dev);
  846. if (ret)
  847. goto out_free_index;
  848. if (dev->transport->configure_unmap &&
  849. dev->transport->configure_unmap(dev)) {
  850. pr_debug("Discard support available, but disabled by default.\n");
  851. }
  852. /*
  853. * XXX: there is not much point to have two different values here..
  854. */
  855. dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
  856. dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
  857. /*
  858. * Align max_hw_sectors down to PAGE_SIZE I/O transfers
  859. */
  860. dev->dev_attrib.hw_max_sectors =
  861. se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
  862. dev->dev_attrib.hw_block_size);
  863. dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors;
  864. dev->creation_time = get_jiffies_64();
  865. ret = core_setup_alua(dev);
  866. if (ret)
  867. goto out_destroy_device;
  868. /*
  869. * Setup work_queue for QUEUE_FULL
  870. */
  871. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  872. scsi_dump_inquiry(dev);
  873. spin_lock(&hba->device_lock);
  874. hba->dev_count++;
  875. spin_unlock(&hba->device_lock);
  876. dev->dev_flags |= DF_CONFIGURED;
  877. return 0;
  878. out_destroy_device:
  879. dev->transport->destroy_device(dev);
  880. out_free_index:
  881. mutex_lock(&device_mutex);
  882. idr_remove(&devices_idr, dev->dev_index);
  883. mutex_unlock(&device_mutex);
  884. out:
  885. se_release_vpd_for_dev(dev);
  886. return ret;
  887. }
  888. void target_free_device(struct se_device *dev)
  889. {
  890. struct se_hba *hba = dev->se_hba;
  891. WARN_ON(!list_empty(&dev->dev_sep_list));
  892. percpu_ref_exit(&dev->non_ordered);
  893. cancel_work_sync(&dev->delayed_cmd_work);
  894. if (target_dev_configured(dev)) {
  895. dev->transport->destroy_device(dev);
  896. mutex_lock(&device_mutex);
  897. idr_remove(&devices_idr, dev->dev_index);
  898. mutex_unlock(&device_mutex);
  899. spin_lock(&hba->device_lock);
  900. hba->dev_count--;
  901. spin_unlock(&hba->device_lock);
  902. }
  903. core_alua_free_lu_gp_mem(dev);
  904. core_alua_set_lba_map(dev, NULL, 0, 0);
  905. core_scsi3_free_all_registrations(dev);
  906. se_release_vpd_for_dev(dev);
  907. if (dev->transport->free_prot)
  908. dev->transport->free_prot(dev);
  909. kfree(dev->queues);
  910. free_percpu(dev->stats);
  911. dev->transport->free_device(dev);
  912. }
  913. int core_dev_setup_virtual_lun0(void)
  914. {
  915. struct se_hba *hba;
  916. struct se_device *dev;
  917. char buf[] = "rd_pages=8,rd_nullio=1,rd_dummy=1";
  918. int ret;
  919. hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
  920. if (IS_ERR(hba))
  921. return PTR_ERR(hba);
  922. dev = target_alloc_device(hba, "virt_lun0");
  923. if (!dev) {
  924. ret = -ENOMEM;
  925. goto out_free_hba;
  926. }
  927. hba->backend->ops->set_configfs_dev_params(dev, buf, sizeof(buf));
  928. ret = target_configure_device(dev);
  929. if (ret)
  930. goto out_free_se_dev;
  931. lun0_hba = hba;
  932. g_lun0_dev = dev;
  933. return 0;
  934. out_free_se_dev:
  935. target_free_device(dev);
  936. out_free_hba:
  937. core_delete_hba(hba);
  938. return ret;
  939. }
  940. void core_dev_release_virtual_lun0(void)
  941. {
  942. struct se_hba *hba = lun0_hba;
  943. if (!hba)
  944. return;
  945. if (g_lun0_dev)
  946. target_free_device(g_lun0_dev);
  947. core_delete_hba(hba);
  948. }
  949. /*
  950. * Common CDB parsing for kernel and user passthrough.
  951. */
  952. sense_reason_t
  953. passthrough_parse_cdb(struct se_cmd *cmd,
  954. sense_reason_t (*exec_cmd)(struct se_cmd *cmd))
  955. {
  956. unsigned char *cdb = cmd->t_task_cdb;
  957. struct se_device *dev = cmd->se_dev;
  958. unsigned int size;
  959. /*
  960. * For REPORT LUNS we always need to emulate the response, for everything
  961. * else, pass it up.
  962. */
  963. if (cdb[0] == REPORT_LUNS) {
  964. cmd->execute_cmd = spc_emulate_report_luns;
  965. return TCM_NO_SENSE;
  966. }
  967. /*
  968. * With emulate_pr disabled, all reservation requests should fail,
  969. * regardless of whether or not TRANSPORT_FLAG_PASSTHROUGH_PGR is set.
  970. */
  971. if (!dev->dev_attrib.emulate_pr &&
  972. ((cdb[0] == PERSISTENT_RESERVE_IN) ||
  973. (cdb[0] == PERSISTENT_RESERVE_OUT) ||
  974. (cdb[0] == RELEASE_6 || cdb[0] == RELEASE_10) ||
  975. (cdb[0] == RESERVE_6 || cdb[0] == RESERVE_10))) {
  976. return TCM_UNSUPPORTED_SCSI_OPCODE;
  977. }
  978. /*
  979. * For PERSISTENT RESERVE IN/OUT, RELEASE, and RESERVE we need to
  980. * emulate the response, since tcmu does not have the information
  981. * required to process these commands.
  982. */
  983. if (!(dev->transport_flags &
  984. TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
  985. if (cdb[0] == PERSISTENT_RESERVE_IN) {
  986. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  987. size = get_unaligned_be16(&cdb[7]);
  988. return target_cmd_size_check(cmd, size);
  989. }
  990. if (cdb[0] == PERSISTENT_RESERVE_OUT) {
  991. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  992. size = get_unaligned_be32(&cdb[5]);
  993. return target_cmd_size_check(cmd, size);
  994. }
  995. if (cdb[0] == RELEASE_6 || cdb[0] == RELEASE_10) {
  996. cmd->execute_cmd = target_scsi2_reservation_release;
  997. if (cdb[0] == RELEASE_10)
  998. size = get_unaligned_be16(&cdb[7]);
  999. else
  1000. size = cmd->data_length;
  1001. return target_cmd_size_check(cmd, size);
  1002. }
  1003. if (cdb[0] == RESERVE_6 || cdb[0] == RESERVE_10) {
  1004. cmd->execute_cmd = target_scsi2_reservation_reserve;
  1005. if (cdb[0] == RESERVE_10)
  1006. size = get_unaligned_be16(&cdb[7]);
  1007. else
  1008. size = cmd->data_length;
  1009. return target_cmd_size_check(cmd, size);
  1010. }
  1011. }
  1012. /* Set DATA_CDB flag for ops that should have it */
  1013. switch (cdb[0]) {
  1014. case READ_6:
  1015. case READ_10:
  1016. case READ_12:
  1017. case READ_16:
  1018. case WRITE_6:
  1019. case WRITE_10:
  1020. case WRITE_12:
  1021. case WRITE_16:
  1022. case WRITE_VERIFY:
  1023. case WRITE_VERIFY_12:
  1024. case WRITE_VERIFY_16:
  1025. case COMPARE_AND_WRITE:
  1026. case XDWRITEREAD_10:
  1027. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  1028. break;
  1029. case VARIABLE_LENGTH_CMD:
  1030. switch (get_unaligned_be16(&cdb[8])) {
  1031. case READ_32:
  1032. case WRITE_32:
  1033. case WRITE_VERIFY_32:
  1034. case XDWRITEREAD_32:
  1035. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  1036. break;
  1037. }
  1038. }
  1039. cmd->execute_cmd = exec_cmd;
  1040. return TCM_NO_SENSE;
  1041. }
  1042. EXPORT_SYMBOL(passthrough_parse_cdb);