pr.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NVMe over Fabrics Persist Reservation.
  4. * Copyright (c) 2024 Guixin Liu, Alibaba Group.
  5. * All rights reserved.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/unaligned.h>
  9. #include "nvmet.h"
  10. #define NVMET_PR_NOTIFI_MASK_ALL \
  11. (1 << NVME_PR_NOTIFY_BIT_REG_PREEMPTED | \
  12. 1 << NVME_PR_NOTIFY_BIT_RESV_RELEASED | \
  13. 1 << NVME_PR_NOTIFY_BIT_RESV_PREEMPTED)
  14. static inline bool nvmet_pr_parse_ignore_key(u32 cdw10)
  15. {
  16. /* Ignore existing key, bit 03. */
  17. return (cdw10 >> 3) & 1;
  18. }
  19. static inline struct nvmet_ns *nvmet_pr_to_ns(struct nvmet_pr *pr)
  20. {
  21. return container_of(pr, struct nvmet_ns, pr);
  22. }
  23. static struct nvmet_pr_registrant *
  24. nvmet_pr_find_registrant(struct nvmet_pr *pr, uuid_t *hostid)
  25. {
  26. struct nvmet_pr_registrant *reg;
  27. list_for_each_entry_rcu(reg, &pr->registrant_list, entry) {
  28. if (uuid_equal(&reg->hostid, hostid))
  29. return reg;
  30. }
  31. return NULL;
  32. }
  33. u16 nvmet_set_feat_resv_notif_mask(struct nvmet_req *req, u32 mask)
  34. {
  35. u32 nsid = le32_to_cpu(req->cmd->common.nsid);
  36. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  37. struct nvmet_ns *ns;
  38. unsigned long idx;
  39. u16 status;
  40. if (mask & ~(NVMET_PR_NOTIFI_MASK_ALL)) {
  41. req->error_loc = offsetof(struct nvme_common_command, cdw11);
  42. return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
  43. }
  44. if (nsid != U32_MAX) {
  45. status = nvmet_req_find_ns(req);
  46. if (status)
  47. return status;
  48. if (!req->ns->pr.enable)
  49. return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
  50. WRITE_ONCE(req->ns->pr.notify_mask, mask);
  51. goto success;
  52. }
  53. nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
  54. if (ns->pr.enable)
  55. WRITE_ONCE(ns->pr.notify_mask, mask);
  56. }
  57. success:
  58. nvmet_set_result(req, mask);
  59. return NVME_SC_SUCCESS;
  60. }
  61. u16 nvmet_get_feat_resv_notif_mask(struct nvmet_req *req)
  62. {
  63. u16 status;
  64. status = nvmet_req_find_ns(req);
  65. if (status)
  66. return status;
  67. if (!req->ns->pr.enable)
  68. return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
  69. nvmet_set_result(req, READ_ONCE(req->ns->pr.notify_mask));
  70. return status;
  71. }
  72. void nvmet_execute_get_log_page_resv(struct nvmet_req *req)
  73. {
  74. struct nvmet_pr_log_mgr *log_mgr = &req->sq->ctrl->pr_log_mgr;
  75. struct nvme_pr_log next_log = {0};
  76. struct nvme_pr_log log = {0};
  77. u16 status = NVME_SC_SUCCESS;
  78. u64 lost_count;
  79. u64 cur_count;
  80. u64 next_count;
  81. mutex_lock(&log_mgr->lock);
  82. if (!kfifo_get(&log_mgr->log_queue, &log))
  83. goto out;
  84. /*
  85. * We can't get the last in kfifo.
  86. * Utilize the current count and the count from the next log to
  87. * calculate the number of lost logs, while also addressing cases
  88. * of overflow. If there is no subsequent log, the number of lost
  89. * logs is equal to the lost_count within the nvmet_pr_log_mgr.
  90. */
  91. cur_count = le64_to_cpu(log.count);
  92. if (kfifo_peek(&log_mgr->log_queue, &next_log)) {
  93. next_count = le64_to_cpu(next_log.count);
  94. if (next_count > cur_count)
  95. lost_count = next_count - cur_count - 1;
  96. else
  97. lost_count = U64_MAX - cur_count + next_count - 1;
  98. } else {
  99. lost_count = log_mgr->lost_count;
  100. }
  101. log.count = cpu_to_le64((cur_count + lost_count) == 0 ?
  102. 1 : (cur_count + lost_count));
  103. log_mgr->lost_count -= lost_count;
  104. log.nr_pages = kfifo_len(&log_mgr->log_queue);
  105. out:
  106. status = nvmet_copy_to_sgl(req, 0, &log, sizeof(log));
  107. mutex_unlock(&log_mgr->lock);
  108. nvmet_req_complete(req, status);
  109. }
  110. static void nvmet_pr_add_resv_log(struct nvmet_ctrl *ctrl, u8 log_type,
  111. u32 nsid)
  112. {
  113. struct nvmet_pr_log_mgr *log_mgr = &ctrl->pr_log_mgr;
  114. struct nvme_pr_log log = {0};
  115. mutex_lock(&log_mgr->lock);
  116. log_mgr->counter++;
  117. if (log_mgr->counter == 0)
  118. log_mgr->counter = 1;
  119. log.count = cpu_to_le64(log_mgr->counter);
  120. log.type = log_type;
  121. log.nsid = cpu_to_le32(nsid);
  122. if (!kfifo_put(&log_mgr->log_queue, log)) {
  123. pr_info("a reservation log lost, cntlid:%d, log_type:%d, nsid:%d\n",
  124. ctrl->cntlid, log_type, nsid);
  125. log_mgr->lost_count++;
  126. }
  127. mutex_unlock(&log_mgr->lock);
  128. }
  129. static void nvmet_pr_resv_released(struct nvmet_pr *pr, uuid_t *hostid)
  130. {
  131. struct nvmet_ns *ns = nvmet_pr_to_ns(pr);
  132. struct nvmet_subsys *subsys = ns->subsys;
  133. struct nvmet_ctrl *ctrl;
  134. if (test_bit(NVME_PR_NOTIFY_BIT_RESV_RELEASED, &pr->notify_mask))
  135. return;
  136. mutex_lock(&subsys->lock);
  137. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
  138. if (!uuid_equal(&ctrl->hostid, hostid) &&
  139. nvmet_pr_find_registrant(pr, &ctrl->hostid)) {
  140. nvmet_pr_add_resv_log(ctrl,
  141. NVME_PR_LOG_RESERVATION_RELEASED, ns->nsid);
  142. nvmet_add_async_event(ctrl, NVME_AER_CSS,
  143. NVME_AEN_RESV_LOG_PAGE_AVALIABLE,
  144. NVME_LOG_RESERVATION);
  145. }
  146. }
  147. mutex_unlock(&subsys->lock);
  148. }
  149. static void nvmet_pr_send_event_to_host(struct nvmet_pr *pr, uuid_t *hostid,
  150. u8 log_type)
  151. {
  152. struct nvmet_ns *ns = nvmet_pr_to_ns(pr);
  153. struct nvmet_subsys *subsys = ns->subsys;
  154. struct nvmet_ctrl *ctrl;
  155. mutex_lock(&subsys->lock);
  156. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
  157. if (uuid_equal(hostid, &ctrl->hostid)) {
  158. nvmet_pr_add_resv_log(ctrl, log_type, ns->nsid);
  159. nvmet_add_async_event(ctrl, NVME_AER_CSS,
  160. NVME_AEN_RESV_LOG_PAGE_AVALIABLE,
  161. NVME_LOG_RESERVATION);
  162. }
  163. }
  164. mutex_unlock(&subsys->lock);
  165. }
  166. static void nvmet_pr_resv_preempted(struct nvmet_pr *pr, uuid_t *hostid)
  167. {
  168. if (test_bit(NVME_PR_NOTIFY_BIT_RESV_PREEMPTED, &pr->notify_mask))
  169. return;
  170. nvmet_pr_send_event_to_host(pr, hostid,
  171. NVME_PR_LOG_RESERVATOIN_PREEMPTED);
  172. }
  173. static void nvmet_pr_registration_preempted(struct nvmet_pr *pr,
  174. uuid_t *hostid)
  175. {
  176. if (test_bit(NVME_PR_NOTIFY_BIT_REG_PREEMPTED, &pr->notify_mask))
  177. return;
  178. nvmet_pr_send_event_to_host(pr, hostid,
  179. NVME_PR_LOG_REGISTRATION_PREEMPTED);
  180. }
  181. static inline void nvmet_pr_set_new_holder(struct nvmet_pr *pr, u8 new_rtype,
  182. struct nvmet_pr_registrant *reg)
  183. {
  184. reg->rtype = new_rtype;
  185. rcu_assign_pointer(pr->holder, reg);
  186. }
  187. static u16 nvmet_pr_register(struct nvmet_req *req,
  188. struct nvmet_pr_register_data *d)
  189. {
  190. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  191. struct nvmet_pr_registrant *new, *reg;
  192. struct nvmet_pr *pr = &req->ns->pr;
  193. u16 status = NVME_SC_SUCCESS;
  194. u64 nrkey = le64_to_cpu(d->nrkey);
  195. new = kmalloc_obj(*new);
  196. if (!new)
  197. return NVME_SC_INTERNAL;
  198. down(&pr->pr_sem);
  199. reg = nvmet_pr_find_registrant(pr, &ctrl->hostid);
  200. if (reg) {
  201. if (reg->rkey != nrkey)
  202. status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
  203. kfree(new);
  204. goto out;
  205. }
  206. memset(new, 0, sizeof(*new));
  207. INIT_LIST_HEAD(&new->entry);
  208. new->rkey = nrkey;
  209. uuid_copy(&new->hostid, &ctrl->hostid);
  210. list_add_tail_rcu(&new->entry, &pr->registrant_list);
  211. out:
  212. up(&pr->pr_sem);
  213. return status;
  214. }
  215. static void nvmet_pr_unregister_one(struct nvmet_pr *pr,
  216. struct nvmet_pr_registrant *reg)
  217. {
  218. struct nvmet_pr_registrant *first_reg;
  219. struct nvmet_pr_registrant *holder;
  220. u8 original_rtype;
  221. list_del_rcu(&reg->entry);
  222. holder = rcu_dereference_protected(pr->holder, 1);
  223. if (reg != holder)
  224. goto out;
  225. original_rtype = holder->rtype;
  226. if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
  227. original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
  228. first_reg = list_first_or_null_rcu(&pr->registrant_list,
  229. struct nvmet_pr_registrant, entry);
  230. if (first_reg)
  231. first_reg->rtype = original_rtype;
  232. rcu_assign_pointer(pr->holder, first_reg);
  233. } else {
  234. rcu_assign_pointer(pr->holder, NULL);
  235. if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_REG_ONLY ||
  236. original_rtype == NVME_PR_EXCLUSIVE_ACCESS_REG_ONLY)
  237. nvmet_pr_resv_released(pr, &reg->hostid);
  238. }
  239. out:
  240. kfree_rcu(reg, rcu);
  241. }
  242. static u16 nvmet_pr_unregister(struct nvmet_req *req,
  243. struct nvmet_pr_register_data *d,
  244. bool ignore_key)
  245. {
  246. u16 status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
  247. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  248. struct nvmet_pr *pr = &req->ns->pr;
  249. struct nvmet_pr_registrant *reg;
  250. down(&pr->pr_sem);
  251. list_for_each_entry_rcu(reg, &pr->registrant_list, entry) {
  252. if (uuid_equal(&reg->hostid, &ctrl->hostid)) {
  253. if (ignore_key || reg->rkey == le64_to_cpu(d->crkey)) {
  254. status = NVME_SC_SUCCESS;
  255. nvmet_pr_unregister_one(pr, reg);
  256. }
  257. break;
  258. }
  259. }
  260. up(&pr->pr_sem);
  261. return status;
  262. }
  263. static void nvmet_pr_update_reg_rkey(struct nvmet_pr_registrant *reg,
  264. void *attr)
  265. {
  266. reg->rkey = *(u64 *)attr;
  267. }
  268. static u16 nvmet_pr_update_reg_attr(struct nvmet_pr *pr,
  269. struct nvmet_pr_registrant *reg,
  270. void (*change_attr)(struct nvmet_pr_registrant *reg,
  271. void *attr),
  272. void *attr)
  273. {
  274. struct nvmet_pr_registrant *holder;
  275. struct nvmet_pr_registrant *new;
  276. holder = rcu_dereference_protected(pr->holder, 1);
  277. if (reg != holder) {
  278. change_attr(reg, attr);
  279. return NVME_SC_SUCCESS;
  280. }
  281. new = kmalloc_obj(*new, GFP_ATOMIC);
  282. if (!new)
  283. return NVME_SC_INTERNAL;
  284. new->rkey = holder->rkey;
  285. new->rtype = holder->rtype;
  286. uuid_copy(&new->hostid, &holder->hostid);
  287. INIT_LIST_HEAD(&new->entry);
  288. change_attr(new, attr);
  289. list_replace_rcu(&holder->entry, &new->entry);
  290. rcu_assign_pointer(pr->holder, new);
  291. kfree_rcu(holder, rcu);
  292. return NVME_SC_SUCCESS;
  293. }
  294. static u16 nvmet_pr_replace(struct nvmet_req *req,
  295. struct nvmet_pr_register_data *d,
  296. bool ignore_key)
  297. {
  298. u16 status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
  299. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  300. struct nvmet_pr *pr = &req->ns->pr;
  301. struct nvmet_pr_registrant *reg;
  302. u64 nrkey = le64_to_cpu(d->nrkey);
  303. down(&pr->pr_sem);
  304. list_for_each_entry_rcu(reg, &pr->registrant_list, entry) {
  305. if (uuid_equal(&reg->hostid, &ctrl->hostid)) {
  306. if (ignore_key || reg->rkey == le64_to_cpu(d->crkey))
  307. status = nvmet_pr_update_reg_attr(pr, reg,
  308. nvmet_pr_update_reg_rkey,
  309. &nrkey);
  310. break;
  311. }
  312. }
  313. up(&pr->pr_sem);
  314. return status;
  315. }
  316. static void nvmet_execute_pr_register(struct nvmet_req *req)
  317. {
  318. u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
  319. bool ignore_key = nvmet_pr_parse_ignore_key(cdw10);
  320. struct nvmet_pr_register_data *d;
  321. u8 reg_act = cdw10 & 0x07; /* Reservation Register Action, bit 02:00 */
  322. u16 status;
  323. d = kmalloc_obj(*d);
  324. if (!d) {
  325. status = NVME_SC_INTERNAL;
  326. goto out;
  327. }
  328. status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
  329. if (status)
  330. goto free_data;
  331. switch (reg_act) {
  332. case NVME_PR_REGISTER_ACT_REG:
  333. status = nvmet_pr_register(req, d);
  334. break;
  335. case NVME_PR_REGISTER_ACT_UNREG:
  336. status = nvmet_pr_unregister(req, d, ignore_key);
  337. break;
  338. case NVME_PR_REGISTER_ACT_REPLACE:
  339. status = nvmet_pr_replace(req, d, ignore_key);
  340. break;
  341. default:
  342. req->error_loc = offsetof(struct nvme_common_command, cdw10);
  343. status = NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
  344. break;
  345. }
  346. free_data:
  347. kfree(d);
  348. out:
  349. if (!status)
  350. atomic_inc(&req->ns->pr.generation);
  351. nvmet_req_complete(req, status);
  352. }
  353. static u16 nvmet_pr_acquire(struct nvmet_req *req,
  354. struct nvmet_pr_registrant *reg,
  355. u8 rtype)
  356. {
  357. struct nvmet_pr *pr = &req->ns->pr;
  358. struct nvmet_pr_registrant *holder;
  359. holder = rcu_dereference_protected(pr->holder, 1);
  360. if (holder && reg != holder)
  361. return NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
  362. if (holder && reg == holder) {
  363. if (holder->rtype == rtype)
  364. return NVME_SC_SUCCESS;
  365. return NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
  366. }
  367. nvmet_pr_set_new_holder(pr, rtype, reg);
  368. return NVME_SC_SUCCESS;
  369. }
  370. static void nvmet_pr_confirm_ns_pc_ref(struct percpu_ref *ref)
  371. {
  372. struct nvmet_pr_per_ctrl_ref *pc_ref =
  373. container_of(ref, struct nvmet_pr_per_ctrl_ref, ref);
  374. complete(&pc_ref->confirm_done);
  375. }
  376. static void nvmet_pr_set_ctrl_to_abort(struct nvmet_req *req, uuid_t *hostid)
  377. {
  378. struct nvmet_pr_per_ctrl_ref *pc_ref;
  379. struct nvmet_ns *ns = req->ns;
  380. unsigned long idx;
  381. xa_for_each(&ns->pr_per_ctrl_refs, idx, pc_ref) {
  382. if (uuid_equal(&pc_ref->hostid, hostid)) {
  383. percpu_ref_kill_and_confirm(&pc_ref->ref,
  384. nvmet_pr_confirm_ns_pc_ref);
  385. wait_for_completion(&pc_ref->confirm_done);
  386. }
  387. }
  388. }
  389. static u16 nvmet_pr_unreg_all_host_by_prkey(struct nvmet_req *req, u64 prkey,
  390. uuid_t *send_hostid,
  391. bool abort)
  392. {
  393. u16 status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
  394. struct nvmet_pr_registrant *reg, *tmp;
  395. struct nvmet_pr *pr = &req->ns->pr;
  396. uuid_t hostid;
  397. list_for_each_entry_safe(reg, tmp, &pr->registrant_list, entry) {
  398. if (reg->rkey == prkey) {
  399. status = NVME_SC_SUCCESS;
  400. uuid_copy(&hostid, &reg->hostid);
  401. if (abort)
  402. nvmet_pr_set_ctrl_to_abort(req, &hostid);
  403. nvmet_pr_unregister_one(pr, reg);
  404. if (!uuid_equal(&hostid, send_hostid))
  405. nvmet_pr_registration_preempted(pr, &hostid);
  406. }
  407. }
  408. return status;
  409. }
  410. static void nvmet_pr_unreg_all_others_by_prkey(struct nvmet_req *req,
  411. u64 prkey,
  412. uuid_t *send_hostid,
  413. bool abort)
  414. {
  415. struct nvmet_pr_registrant *reg, *tmp;
  416. struct nvmet_pr *pr = &req->ns->pr;
  417. uuid_t hostid;
  418. list_for_each_entry_safe(reg, tmp, &pr->registrant_list, entry) {
  419. if (reg->rkey == prkey &&
  420. !uuid_equal(&reg->hostid, send_hostid)) {
  421. uuid_copy(&hostid, &reg->hostid);
  422. if (abort)
  423. nvmet_pr_set_ctrl_to_abort(req, &hostid);
  424. nvmet_pr_unregister_one(pr, reg);
  425. nvmet_pr_registration_preempted(pr, &hostid);
  426. }
  427. }
  428. }
  429. static void nvmet_pr_unreg_all_others(struct nvmet_req *req,
  430. uuid_t *send_hostid,
  431. bool abort)
  432. {
  433. struct nvmet_pr_registrant *reg, *tmp;
  434. struct nvmet_pr *pr = &req->ns->pr;
  435. uuid_t hostid;
  436. list_for_each_entry_safe(reg, tmp, &pr->registrant_list, entry) {
  437. if (!uuid_equal(&reg->hostid, send_hostid)) {
  438. uuid_copy(&hostid, &reg->hostid);
  439. if (abort)
  440. nvmet_pr_set_ctrl_to_abort(req, &hostid);
  441. nvmet_pr_unregister_one(pr, reg);
  442. nvmet_pr_registration_preempted(pr, &hostid);
  443. }
  444. }
  445. }
  446. static void nvmet_pr_update_holder_rtype(struct nvmet_pr_registrant *reg,
  447. void *attr)
  448. {
  449. u8 new_rtype = *(u8 *)attr;
  450. reg->rtype = new_rtype;
  451. }
  452. static u16 nvmet_pr_preempt(struct nvmet_req *req,
  453. struct nvmet_pr_registrant *reg,
  454. u8 rtype,
  455. struct nvmet_pr_acquire_data *d,
  456. bool abort)
  457. {
  458. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  459. struct nvmet_pr *pr = &req->ns->pr;
  460. struct nvmet_pr_registrant *holder;
  461. enum nvme_pr_type original_rtype;
  462. u64 prkey = le64_to_cpu(d->prkey);
  463. u16 status;
  464. holder = rcu_dereference_protected(pr->holder, 1);
  465. if (!holder)
  466. return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
  467. &ctrl->hostid, abort);
  468. original_rtype = holder->rtype;
  469. if (original_rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
  470. original_rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
  471. if (!prkey) {
  472. /*
  473. * To prevent possible access from other hosts, and
  474. * avoid terminate the holder, set the new holder
  475. * first before unregistering.
  476. */
  477. nvmet_pr_set_new_holder(pr, rtype, reg);
  478. nvmet_pr_unreg_all_others(req, &ctrl->hostid, abort);
  479. return NVME_SC_SUCCESS;
  480. }
  481. return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
  482. &ctrl->hostid, abort);
  483. }
  484. if (holder == reg) {
  485. status = nvmet_pr_update_reg_attr(pr, holder,
  486. nvmet_pr_update_holder_rtype, &rtype);
  487. if (!status && original_rtype != rtype)
  488. nvmet_pr_resv_released(pr, &reg->hostid);
  489. return status;
  490. }
  491. if (prkey == holder->rkey) {
  492. /*
  493. * Same as before, set the new holder first.
  494. */
  495. nvmet_pr_set_new_holder(pr, rtype, reg);
  496. nvmet_pr_unreg_all_others_by_prkey(req, prkey, &ctrl->hostid,
  497. abort);
  498. if (original_rtype != rtype)
  499. nvmet_pr_resv_released(pr, &reg->hostid);
  500. return NVME_SC_SUCCESS;
  501. }
  502. if (prkey)
  503. return nvmet_pr_unreg_all_host_by_prkey(req, prkey,
  504. &ctrl->hostid, abort);
  505. return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
  506. }
  507. static void nvmet_pr_do_abort(struct work_struct *w)
  508. {
  509. struct nvmet_req *req = container_of(w, struct nvmet_req, r.abort_work);
  510. struct nvmet_pr_per_ctrl_ref *pc_ref;
  511. struct nvmet_ns *ns = req->ns;
  512. unsigned long idx;
  513. /*
  514. * The target does not support abort, just wait per-controller ref to 0.
  515. */
  516. xa_for_each(&ns->pr_per_ctrl_refs, idx, pc_ref) {
  517. if (percpu_ref_is_dying(&pc_ref->ref)) {
  518. wait_for_completion(&pc_ref->free_done);
  519. reinit_completion(&pc_ref->confirm_done);
  520. reinit_completion(&pc_ref->free_done);
  521. percpu_ref_resurrect(&pc_ref->ref);
  522. }
  523. }
  524. up(&ns->pr.pr_sem);
  525. nvmet_req_complete(req, NVME_SC_SUCCESS);
  526. }
  527. static u16 __nvmet_execute_pr_acquire(struct nvmet_req *req,
  528. struct nvmet_pr_registrant *reg,
  529. u8 acquire_act,
  530. u8 rtype,
  531. struct nvmet_pr_acquire_data *d)
  532. {
  533. u16 status;
  534. switch (acquire_act) {
  535. case NVME_PR_ACQUIRE_ACT_ACQUIRE:
  536. status = nvmet_pr_acquire(req, reg, rtype);
  537. goto out;
  538. case NVME_PR_ACQUIRE_ACT_PREEMPT:
  539. status = nvmet_pr_preempt(req, reg, rtype, d, false);
  540. goto inc_gen;
  541. case NVME_PR_ACQUIRE_ACT_PREEMPT_AND_ABORT:
  542. status = nvmet_pr_preempt(req, reg, rtype, d, true);
  543. goto inc_gen;
  544. default:
  545. req->error_loc = offsetof(struct nvme_common_command, cdw10);
  546. status = NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
  547. goto out;
  548. }
  549. inc_gen:
  550. if (!status)
  551. atomic_inc(&req->ns->pr.generation);
  552. out:
  553. return status;
  554. }
  555. static void nvmet_execute_pr_acquire(struct nvmet_req *req)
  556. {
  557. u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
  558. bool ignore_key = nvmet_pr_parse_ignore_key(cdw10);
  559. /* Reservation type, bit 15:08 */
  560. u8 rtype = (u8)((cdw10 >> 8) & 0xff);
  561. /* Reservation acquire action, bit 02:00 */
  562. u8 acquire_act = cdw10 & 0x07;
  563. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  564. struct nvmet_pr_acquire_data *d = NULL;
  565. struct nvmet_pr *pr = &req->ns->pr;
  566. struct nvmet_pr_registrant *reg;
  567. u16 status = NVME_SC_SUCCESS;
  568. if (ignore_key ||
  569. rtype < NVME_PR_WRITE_EXCLUSIVE ||
  570. rtype > NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS) {
  571. status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
  572. goto out;
  573. }
  574. d = kmalloc_obj(*d);
  575. if (!d) {
  576. status = NVME_SC_INTERNAL;
  577. goto out;
  578. }
  579. status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
  580. if (status)
  581. goto free_data;
  582. status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
  583. down(&pr->pr_sem);
  584. list_for_each_entry_rcu(reg, &pr->registrant_list, entry) {
  585. if (uuid_equal(&reg->hostid, &ctrl->hostid) &&
  586. reg->rkey == le64_to_cpu(d->crkey)) {
  587. status = __nvmet_execute_pr_acquire(req, reg,
  588. acquire_act, rtype, d);
  589. break;
  590. }
  591. }
  592. if (!status && acquire_act == NVME_PR_ACQUIRE_ACT_PREEMPT_AND_ABORT) {
  593. kfree(d);
  594. INIT_WORK(&req->r.abort_work, nvmet_pr_do_abort);
  595. queue_work(nvmet_wq, &req->r.abort_work);
  596. return;
  597. }
  598. up(&pr->pr_sem);
  599. free_data:
  600. kfree(d);
  601. out:
  602. nvmet_req_complete(req, status);
  603. }
  604. static u16 nvmet_pr_release(struct nvmet_req *req,
  605. struct nvmet_pr_registrant *reg,
  606. u8 rtype)
  607. {
  608. struct nvmet_pr *pr = &req->ns->pr;
  609. struct nvmet_pr_registrant *holder;
  610. u8 original_rtype;
  611. holder = rcu_dereference_protected(pr->holder, 1);
  612. if (!holder || reg != holder)
  613. return NVME_SC_SUCCESS;
  614. original_rtype = holder->rtype;
  615. if (original_rtype != rtype)
  616. return NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
  617. rcu_assign_pointer(pr->holder, NULL);
  618. if (original_rtype != NVME_PR_WRITE_EXCLUSIVE &&
  619. original_rtype != NVME_PR_EXCLUSIVE_ACCESS)
  620. nvmet_pr_resv_released(pr, &reg->hostid);
  621. return NVME_SC_SUCCESS;
  622. }
  623. static void nvmet_pr_clear(struct nvmet_req *req)
  624. {
  625. struct nvmet_pr_registrant *reg, *tmp;
  626. struct nvmet_pr *pr = &req->ns->pr;
  627. rcu_assign_pointer(pr->holder, NULL);
  628. list_for_each_entry_safe(reg, tmp, &pr->registrant_list, entry) {
  629. list_del_rcu(&reg->entry);
  630. if (!uuid_equal(&req->sq->ctrl->hostid, &reg->hostid))
  631. nvmet_pr_resv_preempted(pr, &reg->hostid);
  632. kfree_rcu(reg, rcu);
  633. }
  634. atomic_inc(&pr->generation);
  635. }
  636. static u16 __nvmet_execute_pr_release(struct nvmet_req *req,
  637. struct nvmet_pr_registrant *reg,
  638. u8 release_act, u8 rtype)
  639. {
  640. switch (release_act) {
  641. case NVME_PR_RELEASE_ACT_RELEASE:
  642. return nvmet_pr_release(req, reg, rtype);
  643. case NVME_PR_RELEASE_ACT_CLEAR:
  644. nvmet_pr_clear(req);
  645. return NVME_SC_SUCCESS;
  646. default:
  647. req->error_loc = offsetof(struct nvme_common_command, cdw10);
  648. return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
  649. }
  650. }
  651. static void nvmet_execute_pr_release(struct nvmet_req *req)
  652. {
  653. u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
  654. bool ignore_key = nvmet_pr_parse_ignore_key(cdw10);
  655. u8 rtype = (u8)((cdw10 >> 8) & 0xff); /* Reservation type, bit 15:08 */
  656. u8 release_act = cdw10 & 0x07; /* Reservation release action, bit 02:00 */
  657. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  658. struct nvmet_pr *pr = &req->ns->pr;
  659. struct nvmet_pr_release_data *d;
  660. struct nvmet_pr_registrant *reg;
  661. u16 status;
  662. if (ignore_key) {
  663. status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
  664. goto out;
  665. }
  666. d = kmalloc_obj(*d);
  667. if (!d) {
  668. status = NVME_SC_INTERNAL;
  669. goto out;
  670. }
  671. status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
  672. if (status)
  673. goto free_data;
  674. status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
  675. down(&pr->pr_sem);
  676. list_for_each_entry_rcu(reg, &pr->registrant_list, entry) {
  677. if (uuid_equal(&reg->hostid, &ctrl->hostid) &&
  678. reg->rkey == le64_to_cpu(d->crkey)) {
  679. status = __nvmet_execute_pr_release(req, reg,
  680. release_act, rtype);
  681. break;
  682. }
  683. }
  684. up(&pr->pr_sem);
  685. free_data:
  686. kfree(d);
  687. out:
  688. nvmet_req_complete(req, status);
  689. }
  690. static void nvmet_execute_pr_report(struct nvmet_req *req)
  691. {
  692. u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11);
  693. u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
  694. u32 num_bytes = 4 * (cdw10 + 1); /* cdw10 is number of dwords */
  695. u8 eds = cdw11 & 1; /* Extended data structure, bit 00 */
  696. struct nvme_registered_ctrl_ext *ctrl_eds;
  697. struct nvme_reservation_status_ext *data;
  698. struct nvmet_pr *pr = &req->ns->pr;
  699. struct nvmet_pr_registrant *holder;
  700. struct nvmet_pr_registrant *reg;
  701. u16 num_ctrls = 0;
  702. u16 status;
  703. u8 rtype;
  704. /* nvmet hostid(uuid_t) is 128 bit. */
  705. if (!eds) {
  706. req->error_loc = offsetof(struct nvme_common_command, cdw11);
  707. status = NVME_SC_HOST_ID_INCONSIST | NVME_STATUS_DNR;
  708. goto out;
  709. }
  710. if (num_bytes < sizeof(struct nvme_reservation_status_ext)) {
  711. req->error_loc = offsetof(struct nvme_common_command, cdw10);
  712. status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
  713. goto out;
  714. }
  715. data = kzalloc(num_bytes, GFP_KERNEL);
  716. if (!data) {
  717. status = NVME_SC_INTERNAL;
  718. goto out;
  719. }
  720. data->gen = cpu_to_le32(atomic_read(&pr->generation));
  721. data->ptpls = 0;
  722. ctrl_eds = data->regctl_eds;
  723. rcu_read_lock();
  724. holder = rcu_dereference(pr->holder);
  725. rtype = holder ? holder->rtype : 0;
  726. data->rtype = rtype;
  727. list_for_each_entry_rcu(reg, &pr->registrant_list, entry) {
  728. num_ctrls++;
  729. /*
  730. * continue to get the number of all registrans.
  731. */
  732. if (((void *)ctrl_eds + sizeof(*ctrl_eds)) >
  733. ((void *)data + num_bytes))
  734. continue;
  735. /*
  736. * Dynamic controller, set cntlid to 0xffff.
  737. */
  738. ctrl_eds->cntlid = cpu_to_le16(NVME_CNTLID_DYNAMIC);
  739. if (rtype == NVME_PR_WRITE_EXCLUSIVE_ALL_REGS ||
  740. rtype == NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS)
  741. ctrl_eds->rcsts = 1;
  742. if (reg == holder)
  743. ctrl_eds->rcsts = 1;
  744. uuid_copy((uuid_t *)&ctrl_eds->hostid, &reg->hostid);
  745. ctrl_eds->rkey = cpu_to_le64(reg->rkey);
  746. ctrl_eds++;
  747. }
  748. rcu_read_unlock();
  749. put_unaligned_le16(num_ctrls, data->regctl);
  750. status = nvmet_copy_to_sgl(req, 0, data, num_bytes);
  751. kfree(data);
  752. out:
  753. nvmet_req_complete(req, status);
  754. }
  755. u16 nvmet_parse_pr_cmd(struct nvmet_req *req)
  756. {
  757. struct nvme_command *cmd = req->cmd;
  758. switch (cmd->common.opcode) {
  759. case nvme_cmd_resv_register:
  760. req->execute = nvmet_execute_pr_register;
  761. break;
  762. case nvme_cmd_resv_acquire:
  763. req->execute = nvmet_execute_pr_acquire;
  764. break;
  765. case nvme_cmd_resv_release:
  766. req->execute = nvmet_execute_pr_release;
  767. break;
  768. case nvme_cmd_resv_report:
  769. req->execute = nvmet_execute_pr_report;
  770. break;
  771. default:
  772. return 1;
  773. }
  774. return NVME_SC_SUCCESS;
  775. }
  776. static bool nvmet_is_req_write_cmd_group(struct nvmet_req *req)
  777. {
  778. u8 opcode = req->cmd->common.opcode;
  779. if (req->sq->qid) {
  780. switch (opcode) {
  781. case nvme_cmd_flush:
  782. case nvme_cmd_write:
  783. case nvme_cmd_write_zeroes:
  784. case nvme_cmd_dsm:
  785. case nvme_cmd_zone_append:
  786. case nvme_cmd_zone_mgmt_send:
  787. return true;
  788. default:
  789. return false;
  790. }
  791. }
  792. return false;
  793. }
  794. static bool nvmet_is_req_read_cmd_group(struct nvmet_req *req)
  795. {
  796. u8 opcode = req->cmd->common.opcode;
  797. if (req->sq->qid) {
  798. switch (opcode) {
  799. case nvme_cmd_read:
  800. case nvme_cmd_zone_mgmt_recv:
  801. return true;
  802. default:
  803. return false;
  804. }
  805. }
  806. return false;
  807. }
  808. u16 nvmet_pr_check_cmd_access(struct nvmet_req *req)
  809. {
  810. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  811. struct nvmet_pr_registrant *holder;
  812. struct nvmet_ns *ns = req->ns;
  813. struct nvmet_pr *pr = &ns->pr;
  814. u16 status = NVME_SC_SUCCESS;
  815. rcu_read_lock();
  816. holder = rcu_dereference(pr->holder);
  817. if (!holder)
  818. goto unlock;
  819. if (uuid_equal(&ctrl->hostid, &holder->hostid))
  820. goto unlock;
  821. /*
  822. * The Reservation command group is checked in executing,
  823. * allow it here.
  824. */
  825. switch (holder->rtype) {
  826. case NVME_PR_WRITE_EXCLUSIVE:
  827. if (nvmet_is_req_write_cmd_group(req))
  828. status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
  829. break;
  830. case NVME_PR_EXCLUSIVE_ACCESS:
  831. if (nvmet_is_req_read_cmd_group(req) ||
  832. nvmet_is_req_write_cmd_group(req))
  833. status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
  834. break;
  835. case NVME_PR_WRITE_EXCLUSIVE_REG_ONLY:
  836. case NVME_PR_WRITE_EXCLUSIVE_ALL_REGS:
  837. if ((nvmet_is_req_write_cmd_group(req)) &&
  838. !nvmet_pr_find_registrant(pr, &ctrl->hostid))
  839. status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
  840. break;
  841. case NVME_PR_EXCLUSIVE_ACCESS_REG_ONLY:
  842. case NVME_PR_EXCLUSIVE_ACCESS_ALL_REGS:
  843. if ((nvmet_is_req_read_cmd_group(req) ||
  844. nvmet_is_req_write_cmd_group(req)) &&
  845. !nvmet_pr_find_registrant(pr, &ctrl->hostid))
  846. status = NVME_SC_RESERVATION_CONFLICT | NVME_STATUS_DNR;
  847. break;
  848. default:
  849. pr_warn("the reservation type is set wrong, type:%d\n",
  850. holder->rtype);
  851. break;
  852. }
  853. unlock:
  854. rcu_read_unlock();
  855. if (status)
  856. req->error_loc = offsetof(struct nvme_common_command, opcode);
  857. return status;
  858. }
  859. u16 nvmet_pr_get_ns_pc_ref(struct nvmet_req *req)
  860. {
  861. struct nvmet_pr_per_ctrl_ref *pc_ref;
  862. pc_ref = xa_load(&req->ns->pr_per_ctrl_refs,
  863. req->sq->ctrl->cntlid);
  864. if (unlikely(!percpu_ref_tryget_live(&pc_ref->ref)))
  865. return NVME_SC_INTERNAL;
  866. req->pc_ref = pc_ref;
  867. return NVME_SC_SUCCESS;
  868. }
  869. static void nvmet_pr_ctrl_ns_all_cmds_done(struct percpu_ref *ref)
  870. {
  871. struct nvmet_pr_per_ctrl_ref *pc_ref =
  872. container_of(ref, struct nvmet_pr_per_ctrl_ref, ref);
  873. complete(&pc_ref->free_done);
  874. }
  875. static int nvmet_pr_alloc_and_insert_pc_ref(struct nvmet_ns *ns,
  876. unsigned long idx,
  877. uuid_t *hostid)
  878. {
  879. struct nvmet_pr_per_ctrl_ref *pc_ref;
  880. int ret;
  881. pc_ref = kmalloc_obj(*pc_ref, GFP_ATOMIC);
  882. if (!pc_ref)
  883. return -ENOMEM;
  884. ret = percpu_ref_init(&pc_ref->ref, nvmet_pr_ctrl_ns_all_cmds_done,
  885. PERCPU_REF_ALLOW_REINIT, GFP_KERNEL);
  886. if (ret)
  887. goto free;
  888. init_completion(&pc_ref->free_done);
  889. init_completion(&pc_ref->confirm_done);
  890. uuid_copy(&pc_ref->hostid, hostid);
  891. ret = xa_insert(&ns->pr_per_ctrl_refs, idx, pc_ref, GFP_KERNEL);
  892. if (ret)
  893. goto exit;
  894. return ret;
  895. exit:
  896. percpu_ref_exit(&pc_ref->ref);
  897. free:
  898. kfree(pc_ref);
  899. return ret;
  900. }
  901. int nvmet_ctrl_init_pr(struct nvmet_ctrl *ctrl)
  902. {
  903. struct nvmet_subsys *subsys = ctrl->subsys;
  904. struct nvmet_pr_per_ctrl_ref *pc_ref;
  905. struct nvmet_ns *ns = NULL;
  906. unsigned long idx;
  907. int ret;
  908. ctrl->pr_log_mgr.counter = 0;
  909. ctrl->pr_log_mgr.lost_count = 0;
  910. mutex_init(&ctrl->pr_log_mgr.lock);
  911. INIT_KFIFO(ctrl->pr_log_mgr.log_queue);
  912. /*
  913. * Here we are under subsys lock, if an ns not in subsys->namespaces,
  914. * we can make sure that ns is not enabled, and not call
  915. * nvmet_pr_init_ns(), see more details in nvmet_ns_enable().
  916. * So just check ns->pr.enable.
  917. */
  918. nvmet_for_each_enabled_ns(&subsys->namespaces, idx, ns) {
  919. if (ns->pr.enable) {
  920. ret = nvmet_pr_alloc_and_insert_pc_ref(ns, ctrl->cntlid,
  921. &ctrl->hostid);
  922. if (ret)
  923. goto free_per_ctrl_refs;
  924. }
  925. }
  926. return 0;
  927. free_per_ctrl_refs:
  928. nvmet_for_each_enabled_ns(&subsys->namespaces, idx, ns) {
  929. if (ns->pr.enable) {
  930. pc_ref = xa_erase(&ns->pr_per_ctrl_refs, ctrl->cntlid);
  931. if (pc_ref)
  932. percpu_ref_exit(&pc_ref->ref);
  933. kfree(pc_ref);
  934. }
  935. }
  936. return ret;
  937. }
  938. void nvmet_ctrl_destroy_pr(struct nvmet_ctrl *ctrl)
  939. {
  940. struct nvmet_pr_per_ctrl_ref *pc_ref;
  941. struct nvmet_ns *ns;
  942. unsigned long idx;
  943. kfifo_free(&ctrl->pr_log_mgr.log_queue);
  944. mutex_destroy(&ctrl->pr_log_mgr.lock);
  945. nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
  946. if (ns->pr.enable) {
  947. pc_ref = xa_erase(&ns->pr_per_ctrl_refs, ctrl->cntlid);
  948. if (pc_ref)
  949. percpu_ref_exit(&pc_ref->ref);
  950. kfree(pc_ref);
  951. }
  952. }
  953. }
  954. int nvmet_pr_init_ns(struct nvmet_ns *ns)
  955. {
  956. struct nvmet_subsys *subsys = ns->subsys;
  957. struct nvmet_pr_per_ctrl_ref *pc_ref;
  958. struct nvmet_ctrl *ctrl = NULL;
  959. unsigned long idx;
  960. int ret;
  961. ns->pr.holder = NULL;
  962. atomic_set(&ns->pr.generation, 0);
  963. sema_init(&ns->pr.pr_sem, 1);
  964. INIT_LIST_HEAD(&ns->pr.registrant_list);
  965. ns->pr.notify_mask = 0;
  966. xa_init(&ns->pr_per_ctrl_refs);
  967. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
  968. ret = nvmet_pr_alloc_and_insert_pc_ref(ns, ctrl->cntlid,
  969. &ctrl->hostid);
  970. if (ret)
  971. goto free_per_ctrl_refs;
  972. }
  973. return 0;
  974. free_per_ctrl_refs:
  975. xa_for_each(&ns->pr_per_ctrl_refs, idx, pc_ref) {
  976. xa_erase(&ns->pr_per_ctrl_refs, idx);
  977. percpu_ref_exit(&pc_ref->ref);
  978. kfree(pc_ref);
  979. }
  980. return ret;
  981. }
  982. void nvmet_pr_exit_ns(struct nvmet_ns *ns)
  983. {
  984. struct nvmet_pr_registrant *reg, *tmp;
  985. struct nvmet_pr_per_ctrl_ref *pc_ref;
  986. struct nvmet_pr *pr = &ns->pr;
  987. unsigned long idx;
  988. list_for_each_entry_safe(reg, tmp, &pr->registrant_list, entry) {
  989. list_del(&reg->entry);
  990. kfree(reg);
  991. }
  992. xa_for_each(&ns->pr_per_ctrl_refs, idx, pc_ref) {
  993. /*
  994. * No command on ns here, we can safely free pc_ref.
  995. */
  996. pc_ref = xa_erase(&ns->pr_per_ctrl_refs, idx);
  997. percpu_ref_exit(&pc_ref->ref);
  998. kfree(pc_ref);
  999. }
  1000. xa_destroy(&ns->pr_per_ctrl_refs);
  1001. }