4 * Copyright (C) 2007 - 2008 Vladislav Bolkhovitin <vst@vlnb.net>
5 * Copyright (C) 2007 - 2008 CMS Distribution Limited
7 * SCSI virtual user space device handler
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation, version 2
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/kthread.h>
21 #include <linux/delay.h>
22 #include <linux/poll.h>
24 #define LOG_PREFIX DEV_USER_NAME
27 #include "scst_user.h"
28 #include "scst_dev_handler.h"
30 #if defined(CONFIG_HIGHMEM4G) || defined(CONFIG_HIGHMEM64G)
31 #warning "HIGHMEM kernel configurations are not supported by this module, \
32 because nowadays it doesn't worth the effort. Consider change \
33 VMSPLIT option or use 64-bit configuration instead. See README file \
37 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
38 #define COMPLETION_INITIALIZER_ONSTACK(work) \
39 ({ init_completion(&work); work; })
42 * Lockdep needs to run a non-constant initializer for on-stack
43 * completions - so we use the _ONSTACK() variant for those that
44 * are on the kernel stack:
47 # define DECLARE_COMPLETION_ONSTACK(work) \
48 struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
50 # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
55 #define DEV_USER_MAJOR 237
57 #define DEV_USER_CMD_HASH_ORDER 6
59 #define DEV_USER_ATTACH_TIMEOUT (5*HZ)
61 struct scst_user_dev {
62 struct rw_semaphore dev_rwsem;
64 struct scst_cmd_lists cmd_lists;
66 /* Protected by cmd_lists.cmd_list_lock */
67 struct list_head ready_cmd_list;
69 /* Protected by dev_rwsem or don't need any protection */
70 unsigned int blocking:1;
71 unsigned int cleanup_done:1;
73 unsigned int queue_alg:4;
76 unsigned int has_own_order_mgmt:1;
78 int (*generic_parse)(struct scst_cmd *cmd,
79 int (*get_block)(struct scst_cmd *cmd));
84 struct scst_mem_lim udev_mem_lim;
85 struct sgv_pool *pool;
88 uint8_t on_free_cmd_type;
89 uint8_t memory_reuse_type;
90 uint8_t partial_transfers_type;
93 struct scst_dev_type devtype;
95 /* Both protected by cmd_lists.cmd_list_lock */
96 unsigned int handle_counter;
97 struct list_head ucmd_hash[1 << DEV_USER_CMD_HASH_ORDER];
99 struct scst_device *sdev;
102 struct list_head dev_list_entry;
103 char name[SCST_MAX_NAME];
105 struct list_head cleanup_list_entry;
106 /* ToDo: make it on-stack */
107 struct completion cleanup_cmpl;
110 /* Most fields are unprotected, since only one thread at time can access them */
111 struct scst_user_cmd {
112 struct scst_cmd *cmd;
113 struct scst_user_dev *dev;
117 unsigned int buff_cached:1;
118 unsigned int buf_dirty:1;
119 unsigned int background_exec:1;
120 unsigned int aborted:1;
122 struct scst_user_cmd *buf_ucmd;
126 int first_page_offset;
128 struct page **data_pages;
129 struct sgv_pool_obj *sgv;
132 * Special flags, which can be accessed asynchronously (hence "long").
133 * Protected by cmd_lists.cmd_list_lock.
135 unsigned long sent_to_user:1;
136 unsigned long jammed:1;
137 unsigned long this_state_unjammed:1;
138 unsigned long seen_by_user:1; /* here only as a small optimization */
142 struct list_head ready_cmd_list_entry;
145 struct list_head hash_list_entry;
147 struct scst_user_get_cmd user_cmd;
149 /* cmpl used only by ATTACH_SESS, mcmd used only by TM */
151 struct completion *cmpl;
152 struct scst_mgmt_cmd *mcmd;
157 static struct scst_user_cmd *dev_user_alloc_ucmd(struct scst_user_dev *dev,
159 static void dev_user_free_ucmd(struct scst_user_cmd *ucmd);
161 static int dev_user_parse(struct scst_cmd *cmd);
162 static int dev_user_exec(struct scst_cmd *cmd);
163 static void dev_user_on_free_cmd(struct scst_cmd *cmd);
164 static int dev_user_task_mgmt_fn(struct scst_mgmt_cmd *mcmd,
165 struct scst_tgt_dev *tgt_dev);
167 static int dev_user_disk_done(struct scst_cmd *cmd);
168 static int dev_user_tape_done(struct scst_cmd *cmd);
170 static struct page *dev_user_alloc_pages(struct scatterlist *sg,
171 gfp_t gfp_mask, void *priv);
172 static void dev_user_free_sg_entries(struct scatterlist *sg, int sg_count,
175 static void dev_user_add_to_ready(struct scst_user_cmd *ucmd);
177 static void dev_user_unjam_cmd(struct scst_user_cmd *ucmd, int busy,
178 unsigned long *flags);
180 static int dev_user_process_reply_on_free(struct scst_user_cmd *ucmd);
181 static int dev_user_process_reply_tm_exec(struct scst_user_cmd *ucmd,
183 static int dev_user_process_reply_sess(struct scst_user_cmd *ucmd, int status);
184 static int dev_user_register_dev(struct file *file,
185 const struct scst_user_dev_desc *dev_desc);
186 static int __dev_user_set_opt(struct scst_user_dev *dev,
187 const struct scst_user_opt *opt);
188 static int dev_user_set_opt(struct file *file, const struct scst_user_opt *opt);
189 static int dev_user_get_opt(struct file *file, void __user *arg);
191 static unsigned int dev_user_poll(struct file *filp, poll_table *wait);
192 static long dev_user_ioctl(struct file *file, unsigned int cmd,
194 static int dev_user_release(struct inode *inode, struct file *file);
198 static struct kmem_cache *user_cmd_cachep;
199 static struct kmem_cache *user_get_cmd_cachep;
201 static DEFINE_MUTEX(dev_priv_mutex);
203 static struct file_operations dev_user_fops = {
204 .poll = dev_user_poll,
205 .unlocked_ioctl = dev_user_ioctl,
207 .compat_ioctl = dev_user_ioctl,
209 .release = dev_user_release,
212 static struct class *dev_user_sysfs_class;
214 static DEFINE_SPINLOCK(dev_list_lock);
215 static LIST_HEAD(dev_list);
217 static DEFINE_SPINLOCK(cleanup_lock);
218 static LIST_HEAD(cleanup_list);
219 static DECLARE_WAIT_QUEUE_HEAD(cleanup_list_waitQ);
220 static struct task_struct *cleanup_thread;
223 * Skip this command if result is not 0. Must be called under
224 * cmd_lists.cmd_list_lock and IRQ off.
226 static inline bool ucmd_get_check(struct scst_user_cmd *ucmd)
228 int r = atomic_inc_return(&ucmd->ucmd_ref);
230 if (unlikely(r == 1)) {
231 TRACE_DBG("ucmd %p is being destroyed", ucmd);
232 atomic_dec(&ucmd->ucmd_ref);
235 * Necessary code is serialized by cmd_list_lock in
239 TRACE_DBG("ucmd %p, new ref_cnt %d", ucmd,
240 atomic_read(&ucmd->ucmd_ref));
246 static inline void __ucmd_get(struct scst_user_cmd *ucmd, bool barrier)
248 TRACE_DBG("ucmd %p, ucmd_ref %d", ucmd, atomic_read(&ucmd->ucmd_ref));
249 atomic_inc(&ucmd->ucmd_ref);
251 smp_mb__after_atomic_inc();
254 static inline void ucmd_get_ordered(struct scst_user_cmd *ucmd)
256 __ucmd_get(ucmd, true);
259 static inline void ucmd_get(struct scst_user_cmd *ucmd)
261 __ucmd_get(ucmd, false);
264 /* Must not be called under cmd_list_lock!! */
265 static inline void ucmd_put(struct scst_user_cmd *ucmd)
267 TRACE_DBG("ucmd %p, ucmd_ref %d", ucmd, atomic_read(&ucmd->ucmd_ref));
269 EXTRACHECKS_BUG_ON(atomic_read(&ucmd->ucmd_ref) == 0);
271 if (atomic_dec_and_test(&ucmd->ucmd_ref))
272 dev_user_free_ucmd(ucmd);
275 static inline int calc_num_pg(unsigned long buf, int len)
277 len += buf & ~PAGE_MASK;
278 return (len >> PAGE_SHIFT) + ((len & ~PAGE_MASK) != 0);
281 static inline int is_need_offs_page(unsigned long buf, int len)
283 return ((buf & ~PAGE_MASK) != 0) &&
284 ((buf & PAGE_MASK) != ((buf+len-1) & PAGE_MASK));
287 static void __dev_user_not_reg(void)
289 PRINT_ERROR("%s", "Device not registered");
293 static inline int dev_user_check_reg(struct scst_user_dev *dev)
296 __dev_user_not_reg();
302 static inline int scst_user_cmd_hashfn(int h)
304 return h & ((1 << DEV_USER_CMD_HASH_ORDER) - 1);
307 static inline struct scst_user_cmd *__ucmd_find_hash(struct scst_user_dev *dev,
310 struct list_head *head;
311 struct scst_user_cmd *ucmd;
313 head = &dev->ucmd_hash[scst_user_cmd_hashfn(h)];
314 list_for_each_entry(ucmd, head, hash_list_entry) {
316 TRACE_DBG("Found ucmd %p", ucmd);
323 static void cmd_insert_hash(struct scst_user_cmd *ucmd)
325 struct list_head *head;
326 struct scst_user_dev *dev = ucmd->dev;
327 struct scst_user_cmd *u;
330 spin_lock_irqsave(&dev->cmd_lists.cmd_list_lock, flags);
332 ucmd->h = dev->handle_counter++;
333 u = __ucmd_find_hash(dev, ucmd->h);
335 head = &dev->ucmd_hash[scst_user_cmd_hashfn(ucmd->h)];
336 list_add_tail(&ucmd->hash_list_entry, head);
337 spin_unlock_irqrestore(&dev->cmd_lists.cmd_list_lock, flags);
339 TRACE_DBG("Inserted ucmd %p, h=%d (dev %s)", ucmd, ucmd->h, dev->name);
343 static inline void cmd_remove_hash(struct scst_user_cmd *ucmd)
347 spin_lock_irqsave(&ucmd->dev->cmd_lists.cmd_list_lock, flags);
348 list_del(&ucmd->hash_list_entry);
349 spin_unlock_irqrestore(&ucmd->dev->cmd_lists.cmd_list_lock, flags);
351 TRACE_DBG("Removed ucmd %p, h=%d", ucmd, ucmd->h);
355 static void dev_user_free_ucmd(struct scst_user_cmd *ucmd)
359 TRACE_MEM("Freeing ucmd %p", ucmd);
361 cmd_remove_hash(ucmd);
362 EXTRACHECKS_BUG_ON(ucmd->cmd != NULL);
364 kmem_cache_free(user_cmd_cachep, ucmd);
370 static struct page *dev_user_alloc_pages(struct scatterlist *sg,
371 gfp_t gfp_mask, void *priv)
373 struct scst_user_cmd *ucmd = (struct scst_user_cmd *)priv;
378 /* *sg supposed to be zeroed */
380 TRACE_MEM("ucmd %p, ubuff %lx, ucmd->cur_data_page %d", ucmd,
381 ucmd->ubuff, ucmd->cur_data_page);
383 if (ucmd->cur_data_page == 0) {
384 TRACE_MEM("ucmd->first_page_offset %d",
385 ucmd->first_page_offset);
386 offset = ucmd->first_page_offset;
390 if (ucmd->cur_data_page >= ucmd->num_data_pages)
393 sg_set_page(sg, ucmd->data_pages[ucmd->cur_data_page],
394 PAGE_SIZE - offset, offset);
395 ucmd->cur_data_page++;
397 TRACE_MEM("page=%p, length=%d, offset=%d", sg_page(sg), sg->length,
399 TRACE_BUFFER("Page data", sg_virt(sg), sg->length);
406 static void dev_user_on_cached_mem_free(struct scst_user_cmd *ucmd)
410 TRACE_MEM("Preparing ON_CACHED_MEM_FREE (ucmd %p, h %d, ubuff %lx)",
411 ucmd, ucmd->h, ucmd->ubuff);
413 ucmd->user_cmd.cmd_h = ucmd->h;
414 ucmd->user_cmd.subcode = SCST_USER_ON_CACHED_MEM_FREE;
415 ucmd->user_cmd.on_cached_mem_free.pbuf = ucmd->ubuff;
417 ucmd->state = UCMD_STATE_ON_CACHE_FREEING;
419 dev_user_add_to_ready(ucmd);
425 static void dev_user_unmap_buf(struct scst_user_cmd *ucmd)
431 TRACE_MEM("Unmapping data pages (ucmd %p, ubuff %lx, num %d)", ucmd,
432 ucmd->ubuff, ucmd->num_data_pages);
434 for (i = 0; i < ucmd->num_data_pages; i++) {
435 struct page *page = ucmd->data_pages[i];
440 page_cache_release(page);
443 kfree(ucmd->data_pages);
444 ucmd->data_pages = NULL;
450 static void __dev_user_free_sg_entries(struct scst_user_cmd *ucmd)
454 sBUG_ON(ucmd->data_pages == NULL);
456 TRACE_MEM("Freeing data pages (ucmd=%p, ubuff=%lx, buff_cached=%d)",
457 ucmd, ucmd->ubuff, ucmd->buff_cached);
459 dev_user_unmap_buf(ucmd);
461 if (ucmd->buff_cached)
462 dev_user_on_cached_mem_free(ucmd);
470 static void dev_user_free_sg_entries(struct scatterlist *sg, int sg_count,
473 struct scst_user_cmd *ucmd = (struct scst_user_cmd *)priv;
475 TRACE_MEM("Freeing data pages (sg=%p, sg_count=%d, priv %p)", sg,
478 __dev_user_free_sg_entries(ucmd);
483 static inline int is_buff_cached(struct scst_user_cmd *ucmd)
485 int mem_reuse_type = ucmd->dev->memory_reuse_type;
487 if ((mem_reuse_type == SCST_USER_MEM_REUSE_ALL) ||
488 ((ucmd->cmd->data_direction == SCST_DATA_READ) &&
489 (mem_reuse_type == SCST_USER_MEM_REUSE_READ)) ||
490 ((ucmd->cmd->data_direction == SCST_DATA_WRITE) &&
491 (mem_reuse_type == SCST_USER_MEM_REUSE_WRITE)))
498 * Returns 0 for success, <0 for fatal failure, >0 - need pages.
499 * Unmaps the buffer, if needed in case of error
501 static int dev_user_alloc_sg(struct scst_user_cmd *ucmd, int cached_buff)
504 struct scst_cmd *cmd = ucmd->cmd;
505 struct scst_user_dev *dev = ucmd->dev;
508 int bufflen = cmd->bufflen;
513 sBUG_ON(bufflen == 0);
515 gfp_mask = __GFP_NOWARN;
516 gfp_mask |= (scst_cmd_atomic(cmd) ? GFP_ATOMIC : GFP_KERNEL);
519 flags |= SCST_POOL_RETURN_OBJ_ON_ALLOC_FAIL;
520 if (ucmd->ubuff == 0)
521 flags |= SCST_POOL_NO_ALLOC_ON_CACHE_MISS;
523 TRACE_MEM("%s", "Not cached buff");
524 flags |= SCST_POOL_ALLOC_NO_CACHED;
525 if (ucmd->ubuff == 0) {
529 bufflen += ucmd->first_page_offset;
530 if (is_need_offs_page(ucmd->ubuff, cmd->bufflen))
531 last_len = bufflen & ~PAGE_MASK;
533 last_len = cmd->bufflen & ~PAGE_MASK;
535 last_len = PAGE_SIZE;
537 ucmd->buff_cached = cached_buff;
539 cmd->sg = sgv_pool_alloc(dev->pool, bufflen, gfp_mask, flags,
540 &cmd->sg_cnt, &ucmd->sgv, &dev->udev_mem_lim, ucmd);
541 if (cmd->sg != NULL) {
542 struct scst_user_cmd *buf_ucmd =
543 (struct scst_user_cmd *)sgv_get_priv(ucmd->sgv);
545 TRACE_MEM("Buf ucmd %p", buf_ucmd);
547 ucmd->ubuff = buf_ucmd->ubuff;
548 ucmd->buf_ucmd = buf_ucmd;
550 EXTRACHECKS_BUG_ON((ucmd->data_pages != NULL) &&
554 /* We don't use clustering, so the assignment is safe */
555 cmd->sg[cmd->sg_cnt-1].length = last_len;
558 TRACE_MEM("Buf alloced (ucmd %p, cached_buff %d, ubuff %lx, "
559 "last_len %d, l %d)", ucmd, cached_buff, ucmd->ubuff,
560 last_len, cmd->sg[cmd->sg_cnt-1].length);
562 if (unlikely(cmd->sg_cnt > cmd->tgt_dev->max_sg_cnt)) {
565 PRINT_INFO("Unable to complete command due to "
566 "SG IO count limitation (requested %d, "
567 "available %d, tgt lim %d)",
569 cmd->tgt_dev->max_sg_cnt,
570 cmd->tgt->sg_tablesize);
574 /* sgv will be freed in dev_user_free_sgv() */
578 TRACE_MEM("Buf not alloced (ucmd %p, h %d, buff_cached, %d, "
579 "sg_cnt %d, ubuff %lx, sgv %p", ucmd, ucmd->h,
580 ucmd->buff_cached, cmd->sg_cnt, ucmd->ubuff, ucmd->sgv);
581 if (unlikely(cmd->sg_cnt == 0)) {
582 TRACE_MEM("Refused allocation (ucmd %p)", ucmd);
583 sBUG_ON(ucmd->sgv != NULL);
586 switch (ucmd->state) {
587 case UCMD_STATE_BUF_ALLOCING:
590 case UCMD_STATE_EXECING:
605 static int dev_user_alloc_space(struct scst_user_cmd *ucmd)
607 int rc, res = SCST_CMD_STATE_DEFAULT;
608 struct scst_cmd *cmd = ucmd->cmd;
612 ucmd->state = UCMD_STATE_BUF_ALLOCING;
613 cmd->dh_data_buf_alloced = 1;
615 rc = dev_user_alloc_sg(ucmd, is_buff_cached(ucmd));
620 res = scst_get_cmd_abnormal_done_state(cmd);
624 if ((cmd->data_direction != SCST_DATA_WRITE) &&
625 !scst_is_cmd_local(cmd)) {
626 TRACE_DBG("Delayed alloc, ucmd %p", ucmd);
630 ucmd->user_cmd.cmd_h = ucmd->h;
631 ucmd->user_cmd.subcode = SCST_USER_ALLOC_MEM;
632 ucmd->user_cmd.alloc_cmd.sess_h = (unsigned long)cmd->tgt_dev;
633 memcpy(ucmd->user_cmd.alloc_cmd.cdb, cmd->cdb,
634 min(sizeof(ucmd->user_cmd.alloc_cmd.cdb), sizeof(cmd->cdb)));
635 ucmd->user_cmd.alloc_cmd.cdb_len = cmd->cdb_len;
636 ucmd->user_cmd.alloc_cmd.alloc_len = ucmd->buff_cached ?
637 (cmd->sg_cnt << PAGE_SHIFT) : cmd->bufflen;
638 ucmd->user_cmd.alloc_cmd.queue_type = cmd->queue_type;
639 ucmd->user_cmd.alloc_cmd.data_direction = cmd->data_direction;
640 ucmd->user_cmd.alloc_cmd.sn = cmd->tgt_sn;
642 dev_user_add_to_ready(ucmd);
644 res = SCST_CMD_STATE_STOP;
651 static struct scst_user_cmd *dev_user_alloc_ucmd(struct scst_user_dev *dev,
654 struct scst_user_cmd *ucmd = NULL;
658 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
659 ucmd = kmem_cache_alloc(user_cmd_cachep, gfp_mask);
661 memset(ucmd, 0, sizeof(*ucmd));
663 ucmd = kmem_cache_zalloc(user_cmd_cachep, gfp_mask);
665 if (unlikely(ucmd == NULL)) {
666 TRACE(TRACE_OUT_OF_MEM, "Unable to allocate "
667 "user cmd (gfp_mask %x)", gfp_mask);
671 atomic_set(&ucmd->ucmd_ref, 1);
673 cmd_insert_hash(ucmd);
675 TRACE_MEM("ucmd %p allocated", ucmd);
678 TRACE_EXIT_HRES((unsigned long)ucmd);
682 static int dev_user_get_block(struct scst_cmd *cmd)
684 struct scst_user_dev *dev = (struct scst_user_dev *)cmd->dev->dh_priv;
686 * No need for locks here, since *_detach() can not be
687 * called, when there are existing commands.
689 TRACE_EXIT_RES(dev->block);
693 static int dev_user_parse(struct scst_cmd *cmd)
695 int rc, res = SCST_CMD_STATE_DEFAULT;
696 struct scst_user_cmd *ucmd;
697 int atomic = scst_cmd_atomic(cmd);
698 struct scst_user_dev *dev = (struct scst_user_dev *)cmd->dev->dh_priv;
699 gfp_t gfp_mask = atomic ? GFP_ATOMIC : GFP_KERNEL;
703 if (cmd->dh_priv == NULL) {
704 ucmd = dev_user_alloc_ucmd(dev, gfp_mask);
705 if (unlikely(ucmd == NULL)) {
707 res = SCST_CMD_STATE_NEED_THREAD_CTX;
717 ucmd = (struct scst_user_cmd *)cmd->dh_priv;
718 TRACE_DBG("Used ucmd %p, state %x", ucmd, ucmd->state);
721 TRACE_DBG("ucmd %p, cmd %p, state %x", ucmd, cmd, ucmd->state);
723 if (ucmd->state != UCMD_STATE_NEW)
726 switch (dev->parse_type) {
727 case SCST_USER_PARSE_STANDARD:
728 TRACE_DBG("PARSE STANDARD: ucmd %p", ucmd);
729 rc = dev->generic_parse(cmd, dev_user_get_block);
730 if ((rc != 0) || (cmd->op_flags & SCST_INFO_INVALID))
734 case SCST_USER_PARSE_EXCEPTION:
735 TRACE_DBG("PARSE EXCEPTION: ucmd %p", ucmd);
736 rc = dev->generic_parse(cmd, dev_user_get_block);
737 if ((rc == 0) && (!(cmd->op_flags & SCST_INFO_INVALID)))
739 else if (rc == SCST_CMD_STATE_NEED_THREAD_CTX) {
740 TRACE_MEM("Restarting PARSE to thread context "
742 res = SCST_CMD_STATE_NEED_THREAD_CTX;
745 /* else go through */
747 case SCST_USER_PARSE_CALL:
748 TRACE_DBG("Preparing PARSE for user space (ucmd=%p, h=%d, "
749 "bufflen %d)", ucmd, ucmd->h, cmd->bufflen);
750 ucmd->user_cmd.cmd_h = ucmd->h;
751 ucmd->user_cmd.subcode = SCST_USER_PARSE;
752 ucmd->user_cmd.parse_cmd.sess_h = (unsigned long)cmd->tgt_dev;
753 memcpy(ucmd->user_cmd.parse_cmd.cdb, cmd->cdb,
754 min(sizeof(ucmd->user_cmd.parse_cmd.cdb),
756 ucmd->user_cmd.parse_cmd.cdb_len = cmd->cdb_len;
757 ucmd->user_cmd.parse_cmd.timeout = cmd->timeout / HZ;
758 ucmd->user_cmd.parse_cmd.bufflen = cmd->bufflen;
759 ucmd->user_cmd.parse_cmd.queue_type = cmd->queue_type;
760 ucmd->user_cmd.parse_cmd.data_direction = cmd->data_direction;
761 ucmd->user_cmd.parse_cmd.expected_values_set =
762 cmd->expected_values_set;
763 ucmd->user_cmd.parse_cmd.expected_data_direction =
764 cmd->expected_data_direction;
765 ucmd->user_cmd.parse_cmd.expected_transfer_len =
766 cmd->expected_transfer_len;
767 ucmd->user_cmd.parse_cmd.sn = cmd->tgt_sn;
768 ucmd->state = UCMD_STATE_PARSING;
769 dev_user_add_to_ready(ucmd);
770 res = SCST_CMD_STATE_STOP;
779 if (cmd->data_direction != SCST_DATA_NONE)
780 res = dev_user_alloc_space(ucmd);
787 PRINT_ERROR("PARSE failed (ucmd %p, rc %d, invalid %d)", ucmd, rc,
788 cmd->op_flags & SCST_INFO_INVALID);
789 scst_set_cmd_error(cmd, SCST_LOAD_SENSE(scst_sense_invalid_opcode));
792 res = scst_get_cmd_abnormal_done_state(cmd);
796 static void dev_user_flush_dcache(struct scst_user_cmd *ucmd)
798 struct scst_user_cmd *buf_ucmd = ucmd->buf_ucmd;
799 unsigned long start = buf_ucmd->ubuff;
800 int i, bufflen = ucmd->cmd->bufflen;
808 * Possibly, flushing of all the pages from ucmd->cmd->sg can be
809 * faster, since it should be cache hot, while ucmd->buf_ucmd and
810 * buf_ucmd->data_pages are cache cold. But, from other side,
811 * sizeof(buf_ucmd->data_pages[0]) is considerably smaller, than
812 * sizeof(ucmd->cmd->sg[0]), so on big buffers going over
813 * data_pages array can lead to less cache misses. So, real numbers are
817 for (i = 0; (bufflen > 0) && (i < buf_ucmd->num_data_pages); i++) {
819 page = buf_ucmd->data_pages[i];
820 #ifdef ARCH_HAS_FLUSH_ANON_PAGE
821 struct vm_area_struct *vma = find_vma(current->mm, start);
823 flush_anon_page(vma, page, start);
825 flush_dcache_page(page);
827 bufflen -= PAGE_SIZE;
835 static int dev_user_exec(struct scst_cmd *cmd)
837 struct scst_user_cmd *ucmd = (struct scst_user_cmd *)cmd->dh_priv;
838 int res = SCST_EXEC_COMPLETED;
842 #if 0 /* We set exec_atomic in 0 to let SCST core know that we need a thread
843 * context to complete the necessary actions, but all we are going to
844 * do in this function is, in fact, atomic, so let's skip this check.
846 if (scst_cmd_atomic(cmd)) {
847 TRACE_DBG("%s", "User exec() can not be called in atomic "
848 "context, rescheduling to the thread");
849 res = SCST_EXEC_NEED_THREAD;
854 TRACE_DBG("Preparing EXEC for user space (ucmd=%p, h=%d, "
855 "bufflen %d, data_len %d, ubuff %lx)", ucmd, ucmd->h,
856 cmd->bufflen, cmd->data_len, ucmd->ubuff);
858 if (cmd->data_direction == SCST_DATA_WRITE)
859 dev_user_flush_dcache(ucmd);
861 ucmd->user_cmd.cmd_h = ucmd->h;
862 ucmd->user_cmd.subcode = SCST_USER_EXEC;
863 ucmd->user_cmd.exec_cmd.sess_h = (unsigned long)cmd->tgt_dev;
864 memcpy(ucmd->user_cmd.exec_cmd.cdb, cmd->cdb,
865 min(sizeof(ucmd->user_cmd.exec_cmd.cdb),
867 ucmd->user_cmd.exec_cmd.cdb_len = cmd->cdb_len;
868 ucmd->user_cmd.exec_cmd.bufflen = cmd->bufflen;
869 ucmd->user_cmd.exec_cmd.data_len = cmd->data_len;
870 ucmd->user_cmd.exec_cmd.pbuf = ucmd->ubuff;
871 if ((ucmd->ubuff == 0) && (cmd->data_direction != SCST_DATA_NONE)) {
872 ucmd->user_cmd.exec_cmd.alloc_len = ucmd->buff_cached ?
873 (cmd->sg_cnt << PAGE_SHIFT) : cmd->bufflen;
875 ucmd->user_cmd.exec_cmd.queue_type = cmd->queue_type;
876 ucmd->user_cmd.exec_cmd.data_direction = cmd->data_direction;
877 ucmd->user_cmd.exec_cmd.partial = 0;
878 ucmd->user_cmd.exec_cmd.timeout = cmd->timeout / HZ;
879 ucmd->user_cmd.exec_cmd.sn = cmd->tgt_sn;
881 ucmd->state = UCMD_STATE_EXECING;
883 dev_user_add_to_ready(ucmd);
889 static void dev_user_free_sgv(struct scst_user_cmd *ucmd)
891 if (ucmd->sgv != NULL) {
892 sgv_pool_free(ucmd->sgv, &ucmd->dev->udev_mem_lim);
894 } else if (ucmd->data_pages != NULL) {
895 /* We mapped pages, but for some reason didn't allocate them */
897 __dev_user_free_sg_entries(ucmd);
902 static void dev_user_on_free_cmd(struct scst_cmd *cmd)
904 struct scst_user_cmd *ucmd = (struct scst_user_cmd *)cmd->dh_priv;
908 if (unlikely(ucmd == NULL))
911 TRACE_MEM("ucmd %p, cmd %p, buff_cached %d, ubuff %lx", ucmd, ucmd->cmd,
912 ucmd->buff_cached, ucmd->ubuff);
915 if (cmd->data_direction == SCST_DATA_WRITE && ucmd->buf_ucmd != NULL)
916 ucmd->buf_ucmd->buf_dirty = 1;
918 if (ucmd->dev->on_free_cmd_type == SCST_USER_ON_FREE_CMD_IGNORE) {
919 ucmd->state = UCMD_STATE_ON_FREE_SKIPPED;
920 /* The state assignment must be before freeing sgv! */
924 if (unlikely(!ucmd->seen_by_user)) {
925 TRACE_MGMT_DBG("Not seen by user ucmd %p", ucmd);
926 sBUG_ON((ucmd->sgv != NULL) || (ucmd->data_pages != NULL));
930 ucmd->user_cmd.cmd_h = ucmd->h;
931 ucmd->user_cmd.subcode = SCST_USER_ON_FREE_CMD;
933 ucmd->user_cmd.on_free_cmd.pbuf = ucmd->ubuff;
934 ucmd->user_cmd.on_free_cmd.resp_data_len = cmd->resp_data_len;
935 ucmd->user_cmd.on_free_cmd.buffer_cached = ucmd->buff_cached;
936 ucmd->user_cmd.on_free_cmd.aborted = ucmd->aborted;
937 ucmd->user_cmd.on_free_cmd.status = cmd->status;
938 ucmd->user_cmd.on_free_cmd.delivery_status = cmd->delivery_status;
940 ucmd->state = UCMD_STATE_ON_FREEING;
942 dev_user_add_to_ready(ucmd);
949 dev_user_process_reply_on_free(ucmd);
953 static void dev_user_set_block(struct scst_cmd *cmd, int block)
955 struct scst_user_dev *dev = (struct scst_user_dev *)cmd->dev->dh_priv;
957 * No need for locks here, since *_detach() can not be
958 * called, when there are existing commands.
960 TRACE_DBG("dev %p, new block %d", dev, block);
964 dev->block = dev->def_block;
968 static int dev_user_disk_done(struct scst_cmd *cmd)
970 int res = SCST_CMD_STATE_DEFAULT;
974 res = scst_block_generic_dev_done(cmd, dev_user_set_block);
980 static int dev_user_tape_done(struct scst_cmd *cmd)
982 int res = SCST_CMD_STATE_DEFAULT;
986 res = scst_tape_generic_dev_done(cmd, dev_user_set_block);
992 static void dev_user_add_to_ready(struct scst_user_cmd *ucmd)
994 struct scst_user_dev *dev = ucmd->dev;
1000 do_wake = (in_interrupt() ||
1001 (ucmd->state == UCMD_STATE_ON_CACHE_FREEING));
1003 do_wake |= ucmd->cmd->preprocessing_only;
1005 spin_lock_irqsave(&dev->cmd_lists.cmd_list_lock, flags);
1007 ucmd->this_state_unjammed = 0;
1009 if ((ucmd->state == UCMD_STATE_PARSING) ||
1010 (ucmd->state == UCMD_STATE_BUF_ALLOCING)) {
1012 * If we don't put such commands in the queue head, then under
1013 * high load we might delay threads, waiting for memory
1014 * allocations, for too long and start loosing NOPs, which
1015 * would lead to consider us by remote initiators as
1016 * unresponsive and stuck => broken connections, etc. If none
1017 * of our commands completed in NOP timeout to allow the head
1018 * commands to go, then we are really overloaded and/or stuck.
1020 TRACE_DBG("Adding ucmd %p (state %d) to head of ready "
1021 "cmd list", ucmd, ucmd->state);
1022 list_add(&ucmd->ready_cmd_list_entry,
1023 &dev->ready_cmd_list);
1025 } else if (unlikely(ucmd->state == UCMD_STATE_TM_EXECING) ||
1026 unlikely(ucmd->state == UCMD_STATE_ATTACH_SESS) ||
1027 unlikely(ucmd->state == UCMD_STATE_DETACH_SESS)) {
1028 TRACE_MGMT_DBG("Adding mgmt ucmd %p (state %d) to head of "
1029 "ready cmd list", ucmd, ucmd->state);
1030 list_add(&ucmd->ready_cmd_list_entry,
1031 &dev->ready_cmd_list);
1033 } else if ((ucmd->cmd != NULL) &&
1034 unlikely((ucmd->cmd->queue_type == SCST_CMD_QUEUE_HEAD_OF_QUEUE))) {
1035 TRACE_DBG("Adding HQ ucmd %p to head of ready cmd list", ucmd);
1036 list_add(&ucmd->ready_cmd_list_entry, &dev->ready_cmd_list);
1038 TRACE_DBG("Adding ucmd %p to ready cmd list", ucmd);
1039 list_add_tail(&ucmd->ready_cmd_list_entry,
1040 &dev->ready_cmd_list);
1044 TRACE_DBG("Waking up dev %p", dev);
1045 wake_up(&dev->cmd_lists.cmd_list_waitQ);
1048 spin_unlock_irqrestore(&dev->cmd_lists.cmd_list_lock, flags);
1054 static int dev_user_map_buf(struct scst_user_cmd *ucmd, unsigned long ubuff,
1059 struct task_struct *tsk = current;
1063 if (unlikely(ubuff == 0))
1066 sBUG_ON(ucmd->data_pages != NULL);
1068 ucmd->num_data_pages = num_pg;
1071 kmalloc(sizeof(*ucmd->data_pages) * ucmd->num_data_pages,
1073 if (ucmd->data_pages == NULL) {
1074 TRACE(TRACE_OUT_OF_MEM, "Unable to allocate data_pages array "
1075 "(num_data_pages=%d)", ucmd->num_data_pages);
1080 TRACE_MEM("Mapping buffer (ucmd %p, ubuff %lx, ucmd->num_data_pages %d,"
1081 " first_page_offset %d, len %d)", ucmd, ubuff,
1082 ucmd->num_data_pages, (int)(ubuff & ~PAGE_MASK),
1083 ucmd->cmd->bufflen);
1085 down_read(&tsk->mm->mmap_sem);
1086 rc = get_user_pages(tsk, tsk->mm, ubuff, ucmd->num_data_pages,
1087 1/*writable*/, 0/*don't force*/, ucmd->data_pages, NULL);
1088 up_read(&tsk->mm->mmap_sem);
1090 /* get_user_pages() flushes dcache */
1092 if (rc < ucmd->num_data_pages)
1095 ucmd->ubuff = ubuff;
1096 ucmd->first_page_offset = (ubuff & ~PAGE_MASK);
1099 TRACE_EXIT_RES(res);
1103 scst_set_busy(ucmd->cmd);
1107 scst_set_cmd_abnormal_done_state(ucmd->cmd);
1111 PRINT_ERROR("Failed to get %d user pages (rc %d)",
1112 ucmd->num_data_pages, rc);
1114 for (i = 0; i < rc; i++)
1115 page_cache_release(ucmd->data_pages[i]);
1117 kfree(ucmd->data_pages);
1118 ucmd->data_pages = NULL;
1120 scst_set_cmd_error(ucmd->cmd, SCST_LOAD_SENSE(scst_sense_hardw_error));
1124 static int dev_user_process_reply_alloc(struct scst_user_cmd *ucmd,
1125 struct scst_user_reply_cmd *reply)
1128 struct scst_cmd *cmd = ucmd->cmd;
1132 TRACE_DBG("ucmd %p, pbuf %llx", ucmd, reply->alloc_reply.pbuf);
1134 if (likely(reply->alloc_reply.pbuf != 0)) {
1136 if (ucmd->buff_cached) {
1137 if (unlikely((reply->alloc_reply.pbuf & ~PAGE_MASK) != 0)) {
1138 PRINT_ERROR("Supplied pbuf %llx isn't "
1140 reply->alloc_reply.pbuf);
1143 pages = cmd->sg_cnt;
1145 pages = calc_num_pg(reply->alloc_reply.pbuf,
1147 res = dev_user_map_buf(ucmd, reply->alloc_reply.pbuf, pages);
1149 scst_set_busy(ucmd->cmd);
1150 scst_set_cmd_abnormal_done_state(ucmd->cmd);
1154 scst_process_active_cmd(cmd, false);
1156 TRACE_EXIT_RES(res);
1160 scst_set_cmd_error(cmd, SCST_LOAD_SENSE(scst_sense_hardw_error));
1161 scst_set_cmd_abnormal_done_state(ucmd->cmd);
1166 static int dev_user_process_reply_parse(struct scst_user_cmd *ucmd,
1167 struct scst_user_reply_cmd *reply)
1170 struct scst_user_scsi_cmd_reply_parse *preply =
1171 &reply->parse_reply;
1172 struct scst_cmd *cmd = ucmd->cmd;
1176 if (unlikely(preply->queue_type > SCST_CMD_QUEUE_ACA))
1179 if (unlikely((preply->data_direction != SCST_DATA_WRITE) &&
1180 (preply->data_direction != SCST_DATA_READ) &&
1181 (preply->data_direction != SCST_DATA_NONE)))
1184 if (unlikely((preply->data_direction != SCST_DATA_NONE) &&
1185 (preply->bufflen == 0)))
1188 if (unlikely((preply->bufflen < 0) || (preply->data_len < 0)))
1191 TRACE_DBG("ucmd %p, queue_type %x, data_direction, %x, bufflen %d, "
1192 "data_len %d, pbuf %llx", ucmd, preply->queue_type,
1193 preply->data_direction, preply->bufflen, preply->data_len,
1194 reply->alloc_reply.pbuf);
1196 cmd->queue_type = preply->queue_type;
1197 cmd->data_direction = preply->data_direction;
1198 cmd->bufflen = preply->bufflen;
1199 cmd->data_len = preply->data_len;
1202 scst_process_active_cmd(cmd, false);
1204 TRACE_EXIT_RES(res);
1208 PRINT_ERROR("Invalid parse_reply parameters (LUN %lld, op %x, cmd %p)",
1209 (long long unsigned int)cmd->lun, cmd->cdb[0], cmd);
1210 PRINT_BUFFER("Invalid parse_reply", reply, sizeof(*reply));
1211 scst_set_cmd_error(cmd, SCST_LOAD_SENSE(scst_sense_hardw_error));
1212 scst_set_cmd_abnormal_done_state(cmd);
1217 static int dev_user_process_reply_on_free(struct scst_user_cmd *ucmd)
1223 TRACE_DBG("ON FREE ucmd %p", ucmd);
1225 dev_user_free_sgv(ucmd);
1228 TRACE_EXIT_RES(res);
1232 static int dev_user_process_reply_on_cache_free(struct scst_user_cmd *ucmd)
1238 TRACE_DBG("ON CACHE FREE ucmd %p", ucmd);
1242 TRACE_EXIT_RES(res);
1246 static int dev_user_process_reply_exec(struct scst_user_cmd *ucmd,
1247 struct scst_user_reply_cmd *reply)
1250 struct scst_user_scsi_cmd_reply_exec *ereply =
1252 struct scst_cmd *cmd = ucmd->cmd;
1256 if (ereply->reply_type == SCST_EXEC_REPLY_COMPLETED) {
1257 if (ucmd->background_exec) {
1258 TRACE_DBG("Background ucmd %p finished", ucmd);
1262 if (unlikely(ereply->resp_data_len > cmd->bufflen))
1264 if (unlikely((cmd->data_direction != SCST_DATA_READ) &&
1265 (ereply->resp_data_len != 0)))
1267 } else if (ereply->reply_type == SCST_EXEC_REPLY_BACKGROUND) {
1268 if (unlikely(ucmd->background_exec))
1270 if (unlikely((cmd->data_direction == SCST_DATA_READ) ||
1271 (cmd->resp_data_len != 0)))
1273 ucmd_get_ordered(ucmd);
1274 ucmd->background_exec = 1;
1275 TRACE_DBG("Background ucmd %p", ucmd);
1280 TRACE_DBG("ucmd %p, status %d, resp_data_len %d", ucmd,
1281 ereply->status, ereply->resp_data_len);
1283 if (ereply->resp_data_len != 0) {
1284 if (ucmd->ubuff == 0) {
1286 if (unlikely(ereply->pbuf == 0))
1288 if (ucmd->buff_cached) {
1289 if (unlikely((ereply->pbuf & ~PAGE_MASK) != 0)) {
1290 PRINT_ERROR("Supplied pbuf %llx isn't "
1291 "page aligned", ereply->pbuf);
1294 pages = cmd->sg_cnt;
1296 pages = calc_num_pg(ereply->pbuf, cmd->bufflen);
1297 rc = dev_user_map_buf(ucmd, ereply->pbuf, pages);
1298 if ((rc != 0) || (ucmd->ubuff == 0))
1301 rc = dev_user_alloc_sg(ucmd, ucmd->buff_cached);
1302 if (unlikely(rc != 0))
1305 dev_user_flush_dcache(ucmd);
1306 cmd->may_need_dma_sync = 1;
1307 scst_set_resp_data_len(cmd, ereply->resp_data_len);
1308 } else if (cmd->resp_data_len != ereply->resp_data_len) {
1309 if (ucmd->ubuff == 0)
1310 cmd->resp_data_len = ereply->resp_data_len;
1312 scst_set_resp_data_len(cmd, ereply->resp_data_len);
1315 cmd->status = ereply->status;
1316 if (ereply->sense_len != 0) {
1317 res = scst_alloc_sense(cmd, 0);
1320 res = copy_from_user(cmd->sense,
1321 (void __user *)(unsigned long)ereply->psense_buffer,
1322 min((unsigned int)SCST_SENSE_BUFFERSIZE,
1323 (unsigned int)ereply->sense_len));
1325 PRINT_ERROR("%s", "Unable to get sense data");
1326 goto out_hwerr_res_set;
1332 cmd->scst_cmd_done(cmd, SCST_CMD_STATE_DEFAULT, SCST_CONTEXT_DIRECT);
1333 /* !! At this point cmd can be already freed !! */
1336 TRACE_EXIT_RES(res);
1340 PRINT_ERROR("Invalid exec_reply parameters (LUN %lld, op %x, cmd %p)",
1341 (long long unsigned int)cmd->lun, cmd->cdb[0], cmd);
1342 PRINT_BUFFER("Invalid exec_reply", reply, sizeof(*reply));
1348 if (ucmd->background_exec) {
1352 scst_set_cmd_error(cmd,
1353 SCST_LOAD_SENSE(scst_sense_hardw_error));
1362 static int dev_user_process_reply(struct scst_user_dev *dev,
1363 struct scst_user_reply_cmd *reply)
1366 struct scst_user_cmd *ucmd;
1371 spin_lock_irq(&dev->cmd_lists.cmd_list_lock);
1373 ucmd = __ucmd_find_hash(dev, reply->cmd_h);
1374 if (unlikely(ucmd == NULL)) {
1375 TRACE_MGMT_DBG("cmd_h %d not found", reply->cmd_h);
1380 if (unlikely(ucmd_get_check(ucmd))) {
1381 TRACE_MGMT_DBG("Found being destroyed cmd_h %d", reply->cmd_h);
1386 if (ucmd->background_exec) {
1387 state = UCMD_STATE_EXECING;
1388 goto unlock_process;
1391 if (unlikely(ucmd->this_state_unjammed)) {
1392 TRACE_MGMT_DBG("Reply on unjammed ucmd %p, ignoring",
1394 goto out_unlock_put;
1397 if (unlikely(!ucmd->sent_to_user)) {
1398 TRACE_MGMT_DBG("Ucmd %p isn't in the sent to user "
1399 "state %x", ucmd, ucmd->state);
1401 goto out_unlock_put;
1404 if (unlikely(reply->subcode != ucmd->user_cmd.subcode))
1405 goto out_wrong_state;
1407 if (unlikely(_IOC_NR(reply->subcode) != ucmd->state))
1408 goto out_wrong_state;
1410 state = ucmd->state;
1411 ucmd->sent_to_user = 0;
1414 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
1417 case UCMD_STATE_PARSING:
1418 res = dev_user_process_reply_parse(ucmd, reply);
1421 case UCMD_STATE_BUF_ALLOCING:
1422 res = dev_user_process_reply_alloc(ucmd, reply);
1425 case UCMD_STATE_EXECING:
1426 res = dev_user_process_reply_exec(ucmd, reply);
1429 case UCMD_STATE_ON_FREEING:
1430 res = dev_user_process_reply_on_free(ucmd);
1433 case UCMD_STATE_ON_CACHE_FREEING:
1434 res = dev_user_process_reply_on_cache_free(ucmd);
1437 case UCMD_STATE_TM_EXECING:
1438 res = dev_user_process_reply_tm_exec(ucmd, reply->result);
1441 case UCMD_STATE_ATTACH_SESS:
1442 case UCMD_STATE_DETACH_SESS:
1443 res = dev_user_process_reply_sess(ucmd, reply->result);
1455 TRACE_EXIT_RES(res);
1459 PRINT_ERROR("Command's %p subcode %x doesn't match internal "
1460 "command's state %x or reply->subcode (%x) != ucmd->subcode "
1461 "(%x)", ucmd, _IOC_NR(reply->subcode), ucmd->state,
1462 reply->subcode, ucmd->user_cmd.subcode);
1464 dev_user_unjam_cmd(ucmd, 0, NULL);
1467 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
1471 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
1475 static int dev_user_reply_cmd(struct file *file, void __user *arg)
1478 struct scst_user_dev *dev;
1479 struct scst_user_reply_cmd reply;
1483 mutex_lock(&dev_priv_mutex);
1484 dev = (struct scst_user_dev *)file->private_data;
1485 res = dev_user_check_reg(dev);
1487 mutex_unlock(&dev_priv_mutex);
1490 down_read(&dev->dev_rwsem);
1491 mutex_unlock(&dev_priv_mutex);
1493 res = copy_from_user(&reply, arg, sizeof(reply));
1497 TRACE_MGMT_DBG("Reply for dev %s", dev->name);
1499 TRACE_BUFFER("Reply", &reply, sizeof(reply));
1501 res = dev_user_process_reply(dev, &reply);
1506 up_read(&dev->dev_rwsem);
1509 TRACE_EXIT_RES(res);
1513 static int dev_user_process_scst_commands(struct scst_user_dev *dev)
1514 __releases(&dev->cmd_lists.cmd_list_lock)
1515 __acquires(&dev->cmd_lists.cmd_list_lock)
1521 while (!list_empty(&dev->cmd_lists.active_cmd_list)) {
1522 struct scst_cmd *cmd = list_entry(
1523 dev->cmd_lists.active_cmd_list.next, typeof(*cmd),
1525 TRACE_DBG("Deleting cmd %p from active cmd list", cmd);
1526 list_del(&cmd->cmd_list_entry);
1527 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
1528 scst_process_active_cmd(cmd, false);
1529 spin_lock_irq(&dev->cmd_lists.cmd_list_lock);
1533 TRACE_EXIT_RES(res);
1537 /* Called under cmd_lists.cmd_list_lock and IRQ off */
1538 static struct scst_user_cmd *__dev_user_get_next_cmd(struct list_head *cmd_list)
1539 __releases(&dev->cmd_lists.cmd_list_lock)
1540 __acquires(&dev->cmd_lists.cmd_list_lock)
1542 struct scst_user_cmd *u;
1546 if (!list_empty(cmd_list)) {
1547 u = list_entry(cmd_list->next, typeof(*u),
1548 ready_cmd_list_entry);
1550 TRACE_DBG("Found ready ucmd %p", u);
1551 list_del(&u->ready_cmd_list_entry);
1553 EXTRACHECKS_BUG_ON(u->this_state_unjammed);
1555 if (u->cmd != NULL) {
1556 if (u->state == UCMD_STATE_EXECING) {
1557 struct scst_user_dev *dev = u->dev;
1560 EXTRACHECKS_BUG_ON(u->jammed);
1562 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
1564 rc = scst_check_local_events(u->cmd);
1565 if (unlikely(rc != 0)) {
1566 u->cmd->scst_cmd_done(u->cmd,
1567 SCST_CMD_STATE_DEFAULT,
1568 SCST_CONTEXT_DIRECT);
1570 * !! At this point cmd & u can be !!
1571 * !! already freed !!
1574 &dev->cmd_lists.cmd_list_lock);
1578 spin_lock_irq(&dev->cmd_lists.cmd_list_lock);
1579 } else if (unlikely(test_bit(SCST_CMD_ABORTED,
1580 &u->cmd->cmd_flags))) {
1582 case UCMD_STATE_PARSING:
1583 case UCMD_STATE_BUF_ALLOCING:
1584 TRACE_MGMT_DBG("Aborting ucmd %p", u);
1585 dev_user_unjam_cmd(u, 0, NULL);
1587 case UCMD_STATE_EXECING:
1588 EXTRACHECKS_BUG_ON(1);
1592 u->sent_to_user = 1;
1593 u->seen_by_user = 1;
1598 static inline int test_cmd_lists(struct scst_user_dev *dev)
1600 int res = !list_empty(&dev->cmd_lists.active_cmd_list) ||
1601 !list_empty(&dev->ready_cmd_list) ||
1602 !dev->blocking || dev->cleanup_done ||
1603 signal_pending(current);
1607 /* Called under cmd_lists.cmd_list_lock and IRQ off */
1608 static int dev_user_get_next_cmd(struct scst_user_dev *dev,
1609 struct scst_user_cmd **ucmd)
1616 init_waitqueue_entry(&wait, current);
1619 if (!test_cmd_lists(dev)) {
1620 add_wait_queue_exclusive(&dev->cmd_lists.cmd_list_waitQ,
1623 set_current_state(TASK_INTERRUPTIBLE);
1624 if (test_cmd_lists(dev))
1626 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
1628 spin_lock_irq(&dev->cmd_lists.cmd_list_lock);
1630 set_current_state(TASK_RUNNING);
1631 remove_wait_queue(&dev->cmd_lists.cmd_list_waitQ,
1635 dev_user_process_scst_commands(dev);
1637 *ucmd = __dev_user_get_next_cmd(&dev->ready_cmd_list);
1641 if (!dev->blocking || dev->cleanup_done) {
1643 TRACE_DBG("No ready commands, returning %d", res);
1647 if (signal_pending(current)) {
1649 TRACE_DBG("Signal pending, returning %d", res);
1654 TRACE_EXIT_RES(res);
1658 static int dev_user_reply_get_cmd(struct file *file, void __user *arg)
1661 struct scst_user_dev *dev;
1662 struct scst_user_get_cmd *cmd;
1663 struct scst_user_reply_cmd *reply;
1664 struct scst_user_cmd *ucmd;
1669 mutex_lock(&dev_priv_mutex);
1670 dev = (struct scst_user_dev *)file->private_data;
1671 res = dev_user_check_reg(dev);
1673 mutex_unlock(&dev_priv_mutex);
1676 down_read(&dev->dev_rwsem);
1677 mutex_unlock(&dev_priv_mutex);
1679 res = copy_from_user(&ureply, arg, sizeof(ureply));
1683 TRACE_DBG("ureply %lld (dev %s)", (long long unsigned int)ureply,
1686 cmd = kmem_cache_alloc(user_get_cmd_cachep, GFP_KERNEL);
1693 unsigned long u = (unsigned long)ureply;
1694 reply = (struct scst_user_reply_cmd *)cmd;
1695 res = copy_from_user(reply, (void __user *)u, sizeof(*reply));
1699 TRACE_BUFFER("Reply", reply, sizeof(*reply));
1701 res = dev_user_process_reply(dev, reply);
1706 spin_lock_irq(&dev->cmd_lists.cmd_list_lock);
1707 res = dev_user_get_next_cmd(dev, &ucmd);
1709 *cmd = ucmd->user_cmd;
1710 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
1711 TRACE_BUFFER("UCMD", cmd, sizeof(*cmd));
1712 res = copy_to_user(arg, cmd, sizeof(*cmd));
1714 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
1717 kmem_cache_free(user_get_cmd_cachep, cmd);
1720 up_read(&dev->dev_rwsem);
1723 TRACE_EXIT_RES(res);
1727 static long dev_user_ioctl(struct file *file, unsigned int cmd,
1735 case SCST_USER_REPLY_AND_GET_CMD:
1736 TRACE_DBG("%s", "REPLY_AND_GET_CMD");
1737 res = dev_user_reply_get_cmd(file, (void __user *)arg);
1740 case SCST_USER_REPLY_CMD:
1741 TRACE_DBG("%s", "REPLY_CMD");
1742 res = dev_user_reply_cmd(file, (void __user *)arg);
1745 case SCST_USER_REGISTER_DEVICE:
1747 struct scst_user_dev_desc *dev_desc;
1748 TRACE_DBG("%s", "REGISTER_DEVICE");
1749 dev_desc = kmalloc(sizeof(*dev_desc), GFP_KERNEL);
1750 if (dev_desc == NULL) {
1754 res = copy_from_user(dev_desc, (void __user *)arg,
1760 TRACE_BUFFER("dev_desc", dev_desc, sizeof(*dev_desc));
1761 dev_desc->name[sizeof(dev_desc->name)-1] = '\0';
1762 res = dev_user_register_dev(file, dev_desc);
1767 case SCST_USER_SET_OPTIONS:
1769 struct scst_user_opt opt;
1770 TRACE_DBG("%s", "SET_OPTIONS");
1771 res = copy_from_user(&opt, (void __user *)arg, sizeof(opt));
1774 TRACE_BUFFER("opt", &opt, sizeof(opt));
1775 res = dev_user_set_opt(file, &opt);
1779 case SCST_USER_GET_OPTIONS:
1780 TRACE_DBG("%s", "GET_OPTIONS");
1781 res = dev_user_get_opt(file, (void __user *)arg);
1785 PRINT_ERROR("Invalid ioctl cmd %x", cmd);
1791 TRACE_EXIT_RES(res);
1795 static unsigned int dev_user_poll(struct file *file, poll_table *wait)
1798 struct scst_user_dev *dev;
1802 mutex_lock(&dev_priv_mutex);
1803 dev = (struct scst_user_dev *)file->private_data;
1804 res = dev_user_check_reg(dev);
1806 mutex_unlock(&dev_priv_mutex);
1809 down_read(&dev->dev_rwsem);
1810 mutex_unlock(&dev_priv_mutex);
1812 spin_lock_irq(&dev->cmd_lists.cmd_list_lock);
1814 if (!list_empty(&dev->ready_cmd_list) ||
1815 !list_empty(&dev->cmd_lists.active_cmd_list)) {
1816 res |= POLLIN | POLLRDNORM;
1820 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
1822 TRACE_DBG("Before poll_wait() (dev %s)", dev->name);
1823 poll_wait(file, &dev->cmd_lists.cmd_list_waitQ, wait);
1824 TRACE_DBG("After poll_wait() (dev %s)", dev->name);
1826 spin_lock_irq(&dev->cmd_lists.cmd_list_lock);
1828 if (!list_empty(&dev->ready_cmd_list) ||
1829 !list_empty(&dev->cmd_lists.active_cmd_list)) {
1830 res |= POLLIN | POLLRDNORM;
1835 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
1837 up_read(&dev->dev_rwsem);
1840 TRACE_EXIT_HRES(res);
1845 * Called under cmd_lists.cmd_list_lock, but can drop it inside, then reacquire.
1847 static void dev_user_unjam_cmd(struct scst_user_cmd *ucmd, int busy,
1848 unsigned long *flags)
1849 __releases(&dev->cmd_lists.cmd_list_lock)
1850 __acquires(&dev->cmd_lists.cmd_list_lock)
1852 int state = ucmd->state;
1853 struct scst_user_dev *dev = ucmd->dev;
1857 if (ucmd->this_state_unjammed)
1860 TRACE_MGMT_DBG("Unjamming ucmd %p (busy %d, state %x)", ucmd, busy,
1864 ucmd->this_state_unjammed = 1;
1865 ucmd->sent_to_user = 0;
1868 case UCMD_STATE_PARSING:
1869 case UCMD_STATE_BUF_ALLOCING:
1870 if (test_bit(SCST_CMD_ABORTED, &ucmd->cmd->cmd_flags))
1874 scst_set_busy(ucmd->cmd);
1876 scst_set_cmd_error(ucmd->cmd,
1877 SCST_LOAD_SENSE(scst_sense_hardw_error));
1879 scst_set_cmd_abnormal_done_state(ucmd->cmd);
1881 TRACE_MGMT_DBG("Adding ucmd %p to active list", ucmd);
1882 list_add(&ucmd->cmd->cmd_list_entry,
1883 &ucmd->cmd->cmd_lists->active_cmd_list);
1884 wake_up(&ucmd->dev->cmd_lists.cmd_list_waitQ);
1887 case UCMD_STATE_EXECING:
1889 spin_unlock_irqrestore(&dev->cmd_lists.cmd_list_lock,
1892 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
1894 TRACE_MGMT_DBG("EXEC: unjamming ucmd %p", ucmd);
1896 if (test_bit(SCST_CMD_ABORTED, &ucmd->cmd->cmd_flags))
1900 scst_set_busy(ucmd->cmd);
1902 scst_set_cmd_error(ucmd->cmd,
1903 SCST_LOAD_SENSE(scst_sense_hardw_error));
1906 ucmd->cmd->scst_cmd_done(ucmd->cmd, SCST_CMD_STATE_DEFAULT,
1907 SCST_CONTEXT_DIRECT);
1908 /* !! At this point cmd and ucmd can be already freed !! */
1911 spin_lock_irqsave(&dev->cmd_lists.cmd_list_lock,
1914 spin_lock_irq(&dev->cmd_lists.cmd_list_lock);
1917 case UCMD_STATE_ON_FREEING:
1918 case UCMD_STATE_ON_CACHE_FREEING:
1919 case UCMD_STATE_TM_EXECING:
1920 case UCMD_STATE_ATTACH_SESS:
1921 case UCMD_STATE_DETACH_SESS:
1924 spin_unlock_irqrestore(&dev->cmd_lists.cmd_list_lock,
1927 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
1930 case UCMD_STATE_ON_FREEING:
1931 dev_user_process_reply_on_free(ucmd);
1934 case UCMD_STATE_ON_CACHE_FREEING:
1935 dev_user_process_reply_on_cache_free(ucmd);
1938 case UCMD_STATE_TM_EXECING:
1939 dev_user_process_reply_tm_exec(ucmd,
1940 SCST_MGMT_STATUS_FAILED);
1943 case UCMD_STATE_ATTACH_SESS:
1944 case UCMD_STATE_DETACH_SESS:
1945 dev_user_process_reply_sess(ucmd, -EFAULT);
1950 spin_lock_irqsave(&dev->cmd_lists.cmd_list_lock,
1953 spin_lock_irq(&dev->cmd_lists.cmd_list_lock);
1958 PRINT_CRIT_ERROR("Wrong ucmd state %x", state);
1968 static void dev_user_unjam_dev(struct scst_user_dev *dev)
1969 __releases(&dev->cmd_lists.cmd_list_lock)
1970 __acquires(&dev->cmd_lists.cmd_list_lock)
1973 struct scst_user_cmd *ucmd;
1977 TRACE_MGMT_DBG("Unjamming dev %p", dev);
1979 spin_lock_irq(&dev->cmd_lists.cmd_list_lock);
1982 for (i = 0; i < (int)ARRAY_SIZE(dev->ucmd_hash); i++) {
1983 struct list_head *head = &dev->ucmd_hash[i];
1985 list_for_each_entry(ucmd, head, hash_list_entry) {
1986 if (!ucmd->sent_to_user)
1989 if (ucmd_get_check(ucmd))
1992 TRACE_MGMT_DBG("ucmd %p, state %x, scst_cmd %p", ucmd,
1993 ucmd->state, ucmd->cmd);
1995 dev_user_unjam_cmd(ucmd, 0, NULL);
1997 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
1999 spin_lock_irq(&dev->cmd_lists.cmd_list_lock);
2005 if (dev_user_process_scst_commands(dev) != 0)
2008 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
2014 static int dev_user_process_reply_tm_exec(struct scst_user_cmd *ucmd,
2021 TRACE_MGMT_DBG("TM reply (ucmd %p, fn %d, status %d)", ucmd,
2022 ucmd->user_cmd.tm_cmd.fn, status);
2024 if (status == SCST_MGMT_STATUS_TASK_NOT_EXIST) {
2026 * It is possible that user space seen TM cmd before cmd
2027 * to abort or will never see it at all, because it was
2028 * aborted on the way there. So, it is safe to return
2029 * success instead, because, if there is the TM cmd at this
2030 * point, then the cmd to abort apparrently does exist.
2032 status = SCST_MGMT_STATUS_SUCCESS;
2035 scst_async_mcmd_completed(ucmd->mcmd, status);
2039 TRACE_EXIT_RES(res);
2043 static void dev_user_abort_ready_commands(struct scst_user_dev *dev)
2045 struct scst_user_cmd *ucmd;
2046 unsigned long flags;
2050 spin_lock_irqsave(&dev->cmd_lists.cmd_list_lock, flags);
2052 list_for_each_entry(ucmd, &dev->ready_cmd_list, ready_cmd_list_entry) {
2053 if ((ucmd->cmd != NULL) && !ucmd->seen_by_user &&
2054 test_bit(SCST_CMD_ABORTED, &ucmd->cmd->cmd_flags)) {
2055 switch (ucmd->state) {
2056 case UCMD_STATE_PARSING:
2057 case UCMD_STATE_BUF_ALLOCING:
2058 case UCMD_STATE_EXECING:
2059 TRACE_MGMT_DBG("Aborting ready ucmd %p", ucmd);
2060 list_del(&ucmd->ready_cmd_list_entry);
2061 dev_user_unjam_cmd(ucmd, 0, &flags);
2067 spin_unlock_irqrestore(&dev->cmd_lists.cmd_list_lock, flags);
2073 static int dev_user_task_mgmt_fn(struct scst_mgmt_cmd *mcmd,
2074 struct scst_tgt_dev *tgt_dev)
2076 struct scst_user_cmd *ucmd;
2077 struct scst_user_dev *dev =
2078 (struct scst_user_dev *)tgt_dev->dev->dh_priv;
2079 struct scst_user_cmd *ucmd_to_abort = NULL;
2084 * In the used approach we don't do anything with hung devices, which
2085 * stopped responding and/or have stuck commands. We forcedly abort such
2086 * commands only if they not yet sent to the user space or if the device
2087 * is getting unloaded, e.g. if its handler program gets killed. This is
2088 * because it's pretty hard to distinguish between stuck and temporary
2089 * overloaded states of the device. There are several reasons for that:
2091 * 1. Some commands need a lot of time to complete (several hours),
2092 * so for an impatient user such command(s) will always look as
2095 * 2. If we forcedly abort, i.e. abort before it's actually completed
2096 * in the user space, just one command, we will have to put the whole
2097 * device offline until we are sure that no more previously aborted
2098 * commands will get executed. Otherwise, we might have a possibility
2099 * for data corruption, when aborted and reported as completed
2100 * command actually gets executed *after* new commands sent
2101 * after the force abort was done. Many journaling file systems and
2102 * databases use "provide required commands order via queue draining"
2103 * approach and not putting the whole device offline after the forced
2104 * abort will break it. This makes our decision, if a command stuck
2105 * or not, cost a lot.
2107 * So, we leave policy definition if a device stuck or not to
2108 * the user space and simply let all commands live until they are
2109 * completed or their devices get closed/killed. This approach is very
2110 * much OK, but can affect management commands, which need activity
2111 * suspending via scst_suspend_activity() function such as devices or
2112 * targets registration/removal. But during normal life such commands
2113 * should be rare. Plus, when possible, scst_suspend_activity() will
2114 * return after timeout EBUSY status to allow caller to not stuck
2117 * But, anyway, ToDo, we should reimplement that in the SCST core, so
2118 * stuck commands would affect only related devices.
2121 dev_user_abort_ready_commands(dev);
2123 /* We can't afford missing TM command due to memory shortage */
2124 ucmd = dev_user_alloc_ucmd(dev, GFP_KERNEL|__GFP_NOFAIL);
2126 ucmd->user_cmd.cmd_h = ucmd->h;
2127 ucmd->user_cmd.subcode = SCST_USER_TASK_MGMT;
2128 ucmd->user_cmd.tm_cmd.sess_h = (unsigned long)tgt_dev;
2129 ucmd->user_cmd.tm_cmd.fn = mcmd->fn;
2130 ucmd->user_cmd.tm_cmd.cmd_sn = mcmd->cmd_sn;
2131 ucmd->user_cmd.tm_cmd.cmd_sn_set = mcmd->cmd_sn_set;
2133 if (mcmd->cmd_to_abort != NULL) {
2135 (struct scst_user_cmd *)mcmd->cmd_to_abort->dh_priv;
2136 if (ucmd_to_abort != NULL)
2137 ucmd->user_cmd.tm_cmd.cmd_h_to_abort = ucmd_to_abort->h;
2140 TRACE_MGMT_DBG("Preparing TM ucmd %p (h %d, fn %d, cmd_to_abort %p, "
2141 "ucmd_to_abort %p, cmd_h_to_abort %d)", ucmd, ucmd->h,
2142 mcmd->fn, mcmd->cmd_to_abort, ucmd_to_abort,
2143 ucmd->user_cmd.tm_cmd.cmd_h_to_abort);
2146 ucmd->state = UCMD_STATE_TM_EXECING;
2148 scst_prepare_async_mcmd(mcmd);
2150 dev_user_add_to_ready(ucmd);
2153 return SCST_DEV_TM_NOT_COMPLETED;
2156 static int dev_user_attach(struct scst_device *sdev)
2159 struct scst_user_dev *dev = NULL, *d;
2163 spin_lock(&dev_list_lock);
2164 list_for_each_entry(d, &dev_list, dev_list_entry) {
2165 if (strcmp(d->name, sdev->virt_name) == 0) {
2170 spin_unlock(&dev_list_lock);
2172 PRINT_ERROR("Device %s not found", sdev->virt_name);
2177 sdev->p_cmd_lists = &dev->cmd_lists;
2178 sdev->dh_priv = dev;
2179 sdev->tst = dev->tst;
2180 sdev->queue_alg = dev->queue_alg;
2181 sdev->swp = dev->swp;
2182 sdev->tas = dev->tas;
2183 sdev->has_own_order_mgmt = dev->has_own_order_mgmt;
2187 PRINT_INFO("Attached user space SCSI target virtual device \"%s\"",
2195 static void dev_user_detach(struct scst_device *sdev)
2197 struct scst_user_dev *dev = (struct scst_user_dev *)sdev->dh_priv;
2201 TRACE_DBG("virt_id %d", sdev->virt_id);
2203 PRINT_INFO("Detached user space SCSI target virtual device \"%s\"",
2206 /* dev will be freed by the caller */
2207 sdev->dh_priv = NULL;
2214 static int dev_user_process_reply_sess(struct scst_user_cmd *ucmd, int status)
2217 unsigned long flags;
2221 TRACE_MGMT_DBG("ucmd %p, cmpl %p, status %d", ucmd, ucmd->cmpl, status);
2223 spin_lock_irqsave(&ucmd->dev->cmd_lists.cmd_list_lock, flags);
2225 if (ucmd->state == UCMD_STATE_ATTACH_SESS) {
2226 TRACE_MGMT_DBG("%s", "ATTACH_SESS finished");
2227 ucmd->result = status;
2228 } else if (ucmd->state == UCMD_STATE_DETACH_SESS) {
2229 TRACE_MGMT_DBG("%s", "DETACH_SESS finished");
2233 if (ucmd->cmpl != NULL)
2234 complete_all(ucmd->cmpl);
2236 spin_unlock_irqrestore(&ucmd->dev->cmd_lists.cmd_list_lock, flags);
2240 TRACE_EXIT_RES(res);
2244 static int dev_user_attach_tgt(struct scst_tgt_dev *tgt_dev)
2246 struct scst_user_dev *dev =
2247 (struct scst_user_dev *)tgt_dev->dev->dh_priv;
2249 struct scst_user_cmd *ucmd;
2250 DECLARE_COMPLETION_ONSTACK(cmpl);
2254 ucmd = dev_user_alloc_ucmd(dev, GFP_KERNEL);
2260 ucmd->user_cmd.cmd_h = ucmd->h;
2261 ucmd->user_cmd.subcode = SCST_USER_ATTACH_SESS;
2262 ucmd->user_cmd.sess.sess_h = (unsigned long)tgt_dev;
2263 ucmd->user_cmd.sess.lun = (uint64_t)tgt_dev->lun;
2264 ucmd->user_cmd.sess.threads_num = tgt_dev->sess->tgt->tgtt->threads_num;
2265 ucmd->user_cmd.sess.rd_only = tgt_dev->acg_dev->rd_only_flag;
2266 strncpy(ucmd->user_cmd.sess.initiator_name,
2267 tgt_dev->sess->initiator_name,
2268 sizeof(ucmd->user_cmd.sess.initiator_name)-1);
2269 ucmd->user_cmd.sess.initiator_name[
2270 sizeof(ucmd->user_cmd.sess.initiator_name)-1] = '\0';
2272 TRACE_MGMT_DBG("Preparing ATTACH_SESS %p (h %d, sess_h %llx, LUN %llx, "
2273 "threads_num %d, rd_only_flag %d, initiator %s)", ucmd, ucmd->h,
2274 ucmd->user_cmd.sess.sess_h, ucmd->user_cmd.sess.lun,
2275 ucmd->user_cmd.sess.threads_num, ucmd->user_cmd.sess.rd_only,
2276 ucmd->user_cmd.sess.initiator_name);
2278 ucmd->state = UCMD_STATE_ATTACH_SESS;
2282 dev_user_add_to_ready(ucmd);
2284 rc = wait_for_completion_timeout(ucmd->cmpl, DEV_USER_ATTACH_TIMEOUT);
2288 PRINT_ERROR("%s", "ATTACH_SESS command timeout");
2292 sBUG_ON(irqs_disabled());
2294 spin_lock_irq(&dev->cmd_lists.cmd_list_lock);
2296 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
2301 TRACE_EXIT_RES(res);
2309 static void dev_user_detach_tgt(struct scst_tgt_dev *tgt_dev)
2311 struct scst_user_dev *dev =
2312 (struct scst_user_dev *)tgt_dev->dev->dh_priv;
2313 struct scst_user_cmd *ucmd;
2318 * We can't miss TM command due to memory shortage, because it might
2319 * lead to a memory leak in the user space handler.
2321 ucmd = dev_user_alloc_ucmd(dev, GFP_KERNEL|__GFP_NOFAIL);
2325 TRACE_MGMT_DBG("Preparing DETACH_SESS %p (h %d, sess_h %llx)", ucmd,
2326 ucmd->h, ucmd->user_cmd.sess.sess_h);
2328 ucmd->user_cmd.cmd_h = ucmd->h;
2329 ucmd->user_cmd.subcode = SCST_USER_DETACH_SESS;
2330 ucmd->user_cmd.sess.sess_h = (unsigned long)tgt_dev;
2332 ucmd->state = UCMD_STATE_DETACH_SESS;
2334 dev_user_add_to_ready(ucmd);
2341 /* No locks are needed, but the activity must be suspended */
2342 static void dev_user_setup_functions(struct scst_user_dev *dev)
2346 dev->devtype.parse = dev_user_parse;
2347 dev->devtype.dev_done = NULL;
2349 if (dev->parse_type != SCST_USER_PARSE_CALL) {
2350 switch (dev->devtype.type) {
2352 dev->generic_parse = scst_sbc_generic_parse;
2353 dev->devtype.dev_done = dev_user_disk_done;
2357 dev->generic_parse = scst_tape_generic_parse;
2358 dev->devtype.dev_done = dev_user_tape_done;
2362 dev->generic_parse = scst_modisk_generic_parse;
2363 dev->devtype.dev_done = dev_user_disk_done;
2367 dev->generic_parse = scst_cdrom_generic_parse;
2368 dev->devtype.dev_done = dev_user_disk_done;
2371 case TYPE_MEDIUM_CHANGER:
2372 dev->generic_parse = scst_changer_generic_parse;
2375 case TYPE_PROCESSOR:
2376 dev->generic_parse = scst_processor_generic_parse;
2380 dev->generic_parse = scst_raid_generic_parse;
2384 PRINT_INFO("Unknown SCSI type %x, using PARSE_CALL "
2385 "for it", dev->devtype.type);
2386 dev->parse_type = SCST_USER_PARSE_CALL;
2390 dev->generic_parse = NULL;
2391 dev->devtype.dev_done = NULL;
2398 static int dev_user_check_version(const struct scst_user_dev_desc *dev_desc)
2400 char ver[sizeof(DEV_USER_VERSION)+1];
2403 res = copy_from_user(ver,
2404 (void __user *)(unsigned long)dev_desc->version_str,
2407 PRINT_ERROR("%s", "Unable to get version string");
2410 ver[sizeof(ver)-1] = '\0';
2412 if (strcmp(ver, DEV_USER_VERSION) != 0) {
2413 /* ->name already 0-terminated in dev_user_ioctl() */
2414 PRINT_ERROR("Incorrect version of user device %s (%s)",
2415 dev_desc->name, ver);
2424 static int dev_user_register_dev(struct file *file,
2425 const struct scst_user_dev_desc *dev_desc)
2427 int res = -ENOMEM, i;
2428 struct scst_user_dev *dev, *d;
2433 res = dev_user_check_version(dev_desc);
2437 switch (dev_desc->type) {
2441 if (dev_desc->block_size == 0) {
2442 PRINT_ERROR("Wrong block size %d",
2443 dev_desc->block_size);
2447 block = scst_calc_block_shift(dev_desc->block_size);
2454 block = dev_desc->block_size;
2458 if (!try_module_get(THIS_MODULE)) {
2459 PRINT_ERROR("%s", "Fail to get module");
2463 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2467 init_rwsem(&dev->dev_rwsem);
2468 spin_lock_init(&dev->cmd_lists.cmd_list_lock);
2469 INIT_LIST_HEAD(&dev->cmd_lists.active_cmd_list);
2470 init_waitqueue_head(&dev->cmd_lists.cmd_list_waitQ);
2471 INIT_LIST_HEAD(&dev->ready_cmd_list);
2472 if (file->f_flags & O_NONBLOCK) {
2473 TRACE_DBG("%s", "Non-blocking operations");
2477 for (i = 0; i < (int)ARRAY_SIZE(dev->ucmd_hash); i++)
2478 INIT_LIST_HEAD(&dev->ucmd_hash[i]);
2480 strncpy(dev->name, dev_desc->name, sizeof(dev->name)-1);
2481 dev->name[sizeof(dev->name)-1] = '\0';
2483 scst_init_mem_lim(&dev->udev_mem_lim);
2486 * We don't use clustered pool, since it implies pages reordering,
2487 * which isn't possible with user space supplied buffers. Although
2488 * it's still possible to cluster pages by the tail of each other,
2489 * seems it doesn't worth the effort.
2491 dev->pool = sgv_pool_create(dev->name, 0);
2492 if (dev->pool == NULL)
2494 sgv_pool_set_allocator(dev->pool, dev_user_alloc_pages,
2495 dev_user_free_sg_entries);
2497 scnprintf(dev->devtype.name, sizeof(dev->devtype.name), "dh-%s",
2499 dev->devtype.type = dev_desc->type;
2500 dev->devtype.threads_num = -1;
2501 dev->devtype.parse_atomic = 1;
2502 dev->devtype.exec_atomic = 0; /* no point to make it 1 */
2503 dev->devtype.dev_done_atomic = 1;
2504 dev->devtype.no_proc = 1;
2505 dev->devtype.attach = dev_user_attach;
2506 dev->devtype.detach = dev_user_detach;
2507 dev->devtype.attach_tgt = dev_user_attach_tgt;
2508 dev->devtype.detach_tgt = dev_user_detach_tgt;
2509 dev->devtype.exec = dev_user_exec;
2510 dev->devtype.on_free_cmd = dev_user_on_free_cmd;
2511 dev->devtype.task_mgmt_fn = dev_user_task_mgmt_fn;
2513 init_completion(&dev->cleanup_cmpl);
2515 dev->def_block = block;
2517 res = __dev_user_set_opt(dev, &dev_desc->opt);
2519 TRACE_MEM("dev %p, name %s", dev, dev->name);
2521 spin_lock(&dev_list_lock);
2523 list_for_each_entry(d, &dev_list, dev_list_entry) {
2524 if (strcmp(d->name, dev->name) == 0) {
2525 PRINT_ERROR("Device %s already exist",
2528 spin_unlock(&dev_list_lock);
2533 list_add_tail(&dev->dev_list_entry, &dev_list);
2535 spin_unlock(&dev_list_lock);
2540 res = scst_register_virtual_dev_driver(&dev->devtype);
2544 dev->virt_id = scst_register_virtual_device(&dev->devtype, dev->name);
2545 if (dev->virt_id < 0) {
2547 goto out_unreg_handler;
2550 mutex_lock(&dev_priv_mutex);
2551 if (file->private_data != NULL) {
2552 mutex_unlock(&dev_priv_mutex);
2553 PRINT_ERROR("%s", "Device already registered");
2557 file->private_data = dev;
2558 mutex_unlock(&dev_priv_mutex);
2561 TRACE_EXIT_RES(res);
2565 scst_unregister_virtual_device(dev->virt_id);
2568 scst_unregister_virtual_dev_driver(&dev->devtype);
2571 spin_lock(&dev_list_lock);
2572 list_del(&dev->dev_list_entry);
2573 spin_unlock(&dev_list_lock);
2576 sgv_pool_destroy(dev->pool);
2581 module_put(THIS_MODULE);
2585 static int __dev_user_set_opt(struct scst_user_dev *dev,
2586 const struct scst_user_opt *opt)
2592 TRACE_DBG("dev %s, parse_type %x, on_free_cmd_type %x, "
2593 "memory_reuse_type %x, partial_transfers_type %x, "
2594 "partial_len %d", dev->name, opt->parse_type,
2595 opt->on_free_cmd_type, opt->memory_reuse_type,
2596 opt->partial_transfers_type, opt->partial_len);
2598 if (opt->parse_type > SCST_USER_MAX_PARSE_OPT ||
2599 opt->on_free_cmd_type > SCST_USER_MAX_ON_FREE_CMD_OPT ||
2600 opt->memory_reuse_type > SCST_USER_MAX_MEM_REUSE_OPT ||
2601 opt->partial_transfers_type > SCST_USER_MAX_PARTIAL_TRANSFERS_OPT) {
2602 PRINT_ERROR("%s", "Invalid option");
2607 if (((opt->tst != SCST_CONTR_MODE_ONE_TASK_SET) &&
2608 (opt->tst != SCST_CONTR_MODE_SEP_TASK_SETS)) ||
2609 ((opt->queue_alg != SCST_CONTR_MODE_QUEUE_ALG_RESTRICTED_REORDER) &&
2610 (opt->queue_alg != SCST_CONTR_MODE_QUEUE_ALG_UNRESTRICTED_REORDER)) ||
2611 (opt->swp > 1) || (opt->tas > 1) || (opt->has_own_order_mgmt > 1)) {
2612 PRINT_ERROR("Invalid SCSI option (tst %x, queue_alg %x, swp %x,"
2613 " tas %x, has_own_order_mgmt %x)", opt->tst,
2614 opt->queue_alg, opt->swp, opt->tas,
2615 opt->has_own_order_mgmt);
2620 dev->parse_type = opt->parse_type;
2621 dev->on_free_cmd_type = opt->on_free_cmd_type;
2622 dev->memory_reuse_type = opt->memory_reuse_type;
2623 dev->partial_transfers_type = opt->partial_transfers_type;
2624 dev->partial_len = opt->partial_len;
2626 dev->tst = opt->tst;
2627 dev->queue_alg = opt->queue_alg;
2628 dev->swp = opt->swp;
2629 dev->tas = opt->tas;
2630 dev->has_own_order_mgmt = opt->has_own_order_mgmt;
2631 if (dev->sdev != NULL) {
2632 dev->sdev->tst = opt->tst;
2633 dev->sdev->queue_alg = opt->queue_alg;
2634 dev->sdev->swp = opt->swp;
2635 dev->sdev->tas = opt->tas;
2636 dev->sdev->has_own_order_mgmt = opt->has_own_order_mgmt;
2639 dev_user_setup_functions(dev);
2642 TRACE_EXIT_RES(res);
2646 static int dev_user_set_opt(struct file *file, const struct scst_user_opt *opt)
2649 struct scst_user_dev *dev;
2653 mutex_lock(&dev_priv_mutex);
2654 dev = (struct scst_user_dev *)file->private_data;
2655 res = dev_user_check_reg(dev);
2657 mutex_unlock(&dev_priv_mutex);
2660 down_write(&dev->dev_rwsem);
2661 mutex_unlock(&dev_priv_mutex);
2663 res = scst_suspend_activity(true);
2667 res = __dev_user_set_opt(dev, opt);
2669 scst_resume_activity();
2671 up_write(&dev->dev_rwsem);
2674 TRACE_EXIT_RES(res);
2678 static int dev_user_get_opt(struct file *file, void __user *arg)
2681 struct scst_user_dev *dev;
2682 struct scst_user_opt opt;
2686 mutex_lock(&dev_priv_mutex);
2687 dev = (struct scst_user_dev *)file->private_data;
2688 res = dev_user_check_reg(dev);
2690 mutex_unlock(&dev_priv_mutex);
2693 down_read(&dev->dev_rwsem);
2694 mutex_unlock(&dev_priv_mutex);
2696 opt.parse_type = dev->parse_type;
2697 opt.on_free_cmd_type = dev->on_free_cmd_type;
2698 opt.memory_reuse_type = dev->memory_reuse_type;
2699 opt.partial_transfers_type = dev->partial_transfers_type;
2700 opt.partial_len = dev->partial_len;
2702 opt.queue_alg = dev->queue_alg;
2705 opt.has_own_order_mgmt = dev->has_own_order_mgmt;
2707 TRACE_DBG("dev %s, parse_type %x, on_free_cmd_type %x, "
2708 "memory_reuse_type %x, partial_transfers_type %x, "
2709 "partial_len %d", dev->name, opt.parse_type,
2710 opt.on_free_cmd_type, opt.memory_reuse_type,
2711 opt.partial_transfers_type, opt.partial_len);
2713 res = copy_to_user(arg, &opt, sizeof(opt));
2715 up_read(&dev->dev_rwsem);
2717 TRACE_EXIT_RES(res);
2721 static int dev_usr_parse(struct scst_cmd *cmd)
2724 return SCST_CMD_STATE_DEFAULT;
2727 /* Needed only for /proc support */
2728 #define USR_TYPE { \
2729 .name = DEV_USER_NAME, \
2731 .parse = dev_usr_parse, \
2734 static struct scst_dev_type dev_user_devtype = USR_TYPE;
2736 static int dev_user_release(struct inode *inode, struct file *file)
2739 struct scst_user_dev *dev;
2743 mutex_lock(&dev_priv_mutex);
2744 dev = (struct scst_user_dev *)file->private_data;
2746 mutex_unlock(&dev_priv_mutex);
2749 file->private_data = NULL;
2751 TRACE(TRACE_MGMT, "Releasing dev %s", dev->name);
2753 spin_lock(&dev_list_lock);
2754 list_del(&dev->dev_list_entry);
2755 spin_unlock(&dev_list_lock);
2757 mutex_unlock(&dev_priv_mutex);
2759 down_write(&dev->dev_rwsem);
2761 spin_lock(&cleanup_lock);
2762 list_add_tail(&dev->cleanup_list_entry, &cleanup_list);
2763 spin_unlock(&cleanup_lock);
2765 wake_up(&cleanup_list_waitQ);
2766 wake_up(&dev->cmd_lists.cmd_list_waitQ);
2768 scst_unregister_virtual_device(dev->virt_id);
2769 scst_unregister_virtual_dev_driver(&dev->devtype);
2771 sgv_pool_destroy(dev->pool);
2773 TRACE_DBG("Unregistering finished (dev %p)", dev);
2775 dev->cleanup_done = 1;
2777 wake_up(&cleanup_list_waitQ);
2778 wake_up(&dev->cmd_lists.cmd_list_waitQ);
2780 wait_for_completion(&dev->cleanup_cmpl);
2782 up_write(&dev->dev_rwsem); /* to make the debug check happy */
2784 TRACE_DBG("Releasing completed (dev %p)", dev);
2788 module_put(THIS_MODULE);
2791 TRACE_EXIT_RES(res);
2795 static int dev_user_process_cleanup(struct scst_user_dev *dev)
2797 struct scst_user_cmd *ucmd;
2805 TRACE_DBG("Cleanuping dev %p", dev);
2807 dev_user_unjam_dev(dev);
2809 spin_lock_irq(&dev->cmd_lists.cmd_list_lock);
2811 rc = dev_user_get_next_cmd(dev, &ucmd);
2813 dev_user_unjam_cmd(ucmd, 1, NULL);
2815 spin_unlock_irq(&dev->cmd_lists.cmd_list_lock);
2817 if (rc == -EAGAIN) {
2818 if (dev->cleanup_done)
2821 TRACE_DBG("No more commands (dev %p)", dev);
2827 #ifdef CONFIG_SCST_EXTRACHECKS
2830 for (i = 0; i < (int)ARRAY_SIZE(dev->ucmd_hash); i++) {
2831 struct list_head *head = &dev->ucmd_hash[i];
2832 struct scst_user_cmd *ucmd2;
2834 list_for_each_entry(ucmd2, head, hash_list_entry) {
2835 PRINT_ERROR("Lost ucmd %p (state %x, ref %d)", ucmd2,
2836 ucmd2->state, atomic_read(&ucmd2->ucmd_ref));
2844 TRACE_DBG("Cleanuping done (dev %p)", dev);
2845 complete_all(&dev->cleanup_cmpl);
2849 TRACE_EXIT_RES(res);
2853 static inline int test_cleanup_list(void)
2855 int res = !list_empty(&cleanup_list) ||
2856 unlikely(kthread_should_stop());
2860 static int dev_user_cleanup_thread(void *arg)
2864 PRINT_INFO("Cleanup thread started, PID %d", current->pid);
2866 current->flags |= PF_NOFREEZE;
2868 spin_lock(&cleanup_lock);
2869 while (!kthread_should_stop()) {
2871 init_waitqueue_entry(&wait, current);
2873 if (!test_cleanup_list()) {
2874 add_wait_queue_exclusive(&cleanup_list_waitQ, &wait);
2876 set_current_state(TASK_INTERRUPTIBLE);
2877 if (test_cleanup_list())
2879 spin_unlock(&cleanup_lock);
2881 spin_lock(&cleanup_lock);
2883 set_current_state(TASK_RUNNING);
2884 remove_wait_queue(&cleanup_list_waitQ, &wait);
2888 * We have to poll devices, because commands can go from SCST
2889 * core on cmd_list_waitQ and we have no practical way to
2894 struct scst_user_dev *dev;
2897 while (!list_empty(&cleanup_list)) {
2900 dev = list_entry(cleanup_list.next,
2901 typeof(*dev), cleanup_list_entry);
2902 list_del(&dev->cleanup_list_entry);
2904 spin_unlock(&cleanup_lock);
2905 rc = dev_user_process_cleanup(dev);
2906 spin_lock(&cleanup_lock);
2909 list_add_tail(&dev->cleanup_list_entry,
2913 if (list_empty(&cl_devs))
2916 spin_unlock(&cleanup_lock);
2918 spin_lock(&cleanup_lock);
2920 while (!list_empty(&cl_devs)) {
2921 dev = list_entry(cl_devs.next, typeof(*dev),
2922 cleanup_list_entry);
2923 list_move_tail(&dev->cleanup_list_entry,
2928 spin_unlock(&cleanup_lock);
2931 * If kthread_should_stop() is true, we are guaranteed to be
2932 * on the module unload, so cleanup_list must be empty.
2934 sBUG_ON(!list_empty(&cleanup_list));
2936 PRINT_INFO("Cleanup thread PID %d finished", current->pid);
2942 static int __init init_scst_user(void)
2945 struct max_get_reply {
2947 struct scst_user_get_cmd g;
2948 struct scst_user_reply_cmd r;
2951 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 21)
2952 struct class_device *class_member;
2959 #if defined(CONFIG_HIGHMEM4G) || defined(CONFIG_HIGHMEM64G)
2960 PRINT_ERROR("%s", "HIGHMEM kernel configurations are not supported. "
2961 "Consider change VMSPLIT option or use 64-bit "
2962 "configuration instead. See README file for details.");
2967 user_cmd_cachep = KMEM_CACHE(scst_user_cmd, SCST_SLAB_FLAGS);
2968 if (user_cmd_cachep == NULL) {
2973 user_get_cmd_cachep = KMEM_CACHE(max_get_reply, SCST_SLAB_FLAGS);
2974 if (user_get_cmd_cachep == NULL) {
2979 dev_user_devtype.module = THIS_MODULE;
2981 res = scst_register_virtual_dev_driver(&dev_user_devtype);
2985 res = scst_dev_handler_build_std_proc(&dev_user_devtype);
2989 dev_user_sysfs_class = class_create(THIS_MODULE, DEV_USER_NAME);
2990 if (IS_ERR(dev_user_sysfs_class)) {
2991 PRINT_ERROR("%s", "Unable create sysfs class for SCST user "
2993 res = PTR_ERR(dev_user_sysfs_class);
2997 res = register_chrdev(DEV_USER_MAJOR, DEV_USER_NAME, &dev_user_fops);
2999 PRINT_ERROR("Unable to get major %d for SCSI tapes",
3004 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 21)
3005 class_member = class_device_create(dev_user_sysfs_class, NULL,
3006 MKDEV(DEV_USER_MAJOR, 0), NULL, DEV_USER_NAME);
3007 if (IS_ERR(class_member)) {
3008 res = PTR_ERR(class_member);
3012 dev = device_create(dev_user_sysfs_class, NULL,
3013 MKDEV(DEV_USER_MAJOR, 0),
3014 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
3024 cleanup_thread = kthread_run(dev_user_cleanup_thread, NULL,
3025 "scst_usr_cleanupd");
3026 if (IS_ERR(cleanup_thread)) {
3027 res = PTR_ERR(cleanup_thread);
3028 PRINT_ERROR("kthread_create() failed: %d", res);
3033 TRACE_EXIT_RES(res);
3037 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 21)
3038 class_device_destroy(dev_user_sysfs_class, MKDEV(DEV_USER_MAJOR, 0));
3040 device_destroy(dev_user_sysfs_class, MKDEV(DEV_USER_MAJOR, 0));
3044 unregister_chrdev(DEV_USER_MAJOR, DEV_USER_NAME);
3047 class_destroy(dev_user_sysfs_class);
3050 scst_dev_handler_destroy_std_proc(&dev_user_devtype);
3053 scst_unregister_dev_driver(&dev_user_devtype);
3056 kmem_cache_destroy(user_get_cmd_cachep);
3059 kmem_cache_destroy(user_cmd_cachep);
3063 static void __exit exit_scst_user(void)
3069 rc = kthread_stop(cleanup_thread);
3071 TRACE_MGMT_DBG("kthread_stop() failed: %d", rc);
3073 unregister_chrdev(DEV_USER_MAJOR, DEV_USER_NAME);
3074 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 21)
3075 class_device_destroy(dev_user_sysfs_class, MKDEV(DEV_USER_MAJOR, 0));
3077 device_destroy(dev_user_sysfs_class, MKDEV(DEV_USER_MAJOR, 0));
3079 class_destroy(dev_user_sysfs_class);
3081 scst_dev_handler_destroy_std_proc(&dev_user_devtype);
3082 scst_unregister_virtual_dev_driver(&dev_user_devtype);
3084 kmem_cache_destroy(user_get_cmd_cachep);
3085 kmem_cache_destroy(user_cmd_cachep);
3091 module_init(init_scst_user);
3092 module_exit(exit_scst_user);
3094 MODULE_AUTHOR("Vladislav Bolkhovitin");
3095 MODULE_LICENSE("GPL");
3096 MODULE_DESCRIPTION("Virtual user space device handler for SCST");
3097 MODULE_VERSION(SCST_VERSION_STRING);
3098 MODULE_ALIAS_CHARDEV_MAJOR(DEV_USER_MAJOR);