nbd.c
来自「linux 内核源代码」· C语言 代码 · 共 740 行 · 第 1/2 页
C
740 行
} return req;harderror: lo->harderror = result; return NULL;}static ssize_t pid_show(struct gendisk *disk, char *page){ return sprintf(page, "%ld\n", (long) ((struct nbd_device *)disk->private_data)->pid);}static struct disk_attribute pid_attr = { .attr = { .name = "pid", .mode = S_IRUGO }, .show = pid_show,};static int nbd_do_it(struct nbd_device *lo){ struct request *req; int ret; BUG_ON(lo->magic != LO_MAGIC); lo->pid = current->pid; ret = sysfs_create_file(&lo->disk->kobj, &pid_attr.attr); if (ret) { printk(KERN_ERR "nbd: sysfs_create_file failed!"); return ret; } while ((req = nbd_read_stat(lo)) != NULL) nbd_end_request(req); sysfs_remove_file(&lo->disk->kobj, &pid_attr.attr); return 0;}static void nbd_clear_que(struct nbd_device *lo){ struct request *req; BUG_ON(lo->magic != LO_MAGIC); /* * Because we have set lo->sock to NULL under the tx_lock, all * modifications to the list must have completed by now. For * the same reason, the active_req must be NULL. * * As a consequence, we don't need to take the spin lock while * purging the list here. */ BUG_ON(lo->sock); BUG_ON(lo->active_req); while (!list_empty(&lo->queue_head)) { req = list_entry(lo->queue_head.next, struct request, queuelist); list_del_init(&req->queuelist); req->errors++; nbd_end_request(req); }}/* * We always wait for result of write, for now. It would be nice to make it optional * in future * if ((rq_data_dir(req) == WRITE) && (lo->flags & NBD_WRITE_NOCHK)) * { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); } */static void do_nbd_request(struct request_queue * q){ struct request *req; while ((req = elv_next_request(q)) != NULL) { struct nbd_device *lo; blkdev_dequeue_request(req); dprintk(DBG_BLKDEV, "%s: request %p: dequeued (flags=%x)\n", req->rq_disk->disk_name, req, req->cmd_type); if (!blk_fs_request(req)) goto error_out; lo = req->rq_disk->private_data; BUG_ON(lo->magic != LO_MAGIC); nbd_cmd(req) = NBD_CMD_READ; if (rq_data_dir(req) == WRITE) { nbd_cmd(req) = NBD_CMD_WRITE; if (lo->flags & NBD_READ_ONLY) { printk(KERN_ERR "%s: Write on read-only\n", lo->disk->disk_name); goto error_out; } } req->errors = 0; spin_unlock_irq(q->queue_lock); mutex_lock(&lo->tx_lock); if (unlikely(!lo->sock)) { mutex_unlock(&lo->tx_lock); printk(KERN_ERR "%s: Attempted send on closed socket\n", lo->disk->disk_name); req->errors++; nbd_end_request(req); spin_lock_irq(q->queue_lock); continue; } lo->active_req = req; if (nbd_send_req(lo, req) != 0) { printk(KERN_ERR "%s: Request send failed\n", lo->disk->disk_name); req->errors++; nbd_end_request(req); } else { spin_lock(&lo->queue_lock); list_add(&req->queuelist, &lo->queue_head); spin_unlock(&lo->queue_lock); } lo->active_req = NULL; mutex_unlock(&lo->tx_lock); wake_up_all(&lo->active_wq); spin_lock_irq(q->queue_lock); continue;error_out: req->errors++; spin_unlock(q->queue_lock); nbd_end_request(req); spin_lock(q->queue_lock); }}static int nbd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ struct nbd_device *lo = inode->i_bdev->bd_disk->private_data; int error; struct request sreq ; if (!capable(CAP_SYS_ADMIN)) return -EPERM; BUG_ON(lo->magic != LO_MAGIC); /* Anyone capable of this syscall can do *real bad* things */ dprintk(DBG_IOCTL, "%s: nbd_ioctl cmd=%s(0x%x) arg=%lu\n", lo->disk->disk_name, ioctl_cmd_to_ascii(cmd), cmd, arg); switch (cmd) { case NBD_DISCONNECT: printk(KERN_INFO "%s: NBD_DISCONNECT\n", lo->disk->disk_name); sreq.cmd_type = REQ_TYPE_SPECIAL; nbd_cmd(&sreq) = NBD_CMD_DISC; /* * Set these to sane values in case server implementation * fails to check the request type first and also to keep * debugging output cleaner. */ sreq.sector = 0; sreq.nr_sectors = 0; if (!lo->sock) return -EINVAL; mutex_lock(&lo->tx_lock); nbd_send_req(lo, &sreq); mutex_unlock(&lo->tx_lock); return 0; case NBD_CLEAR_SOCK: error = 0; mutex_lock(&lo->tx_lock); lo->sock = NULL; mutex_unlock(&lo->tx_lock); file = lo->file; lo->file = NULL; nbd_clear_que(lo); BUG_ON(!list_empty(&lo->queue_head)); if (file) fput(file); return error; case NBD_SET_SOCK: if (lo->file) return -EBUSY; error = -EINVAL; file = fget(arg); if (file) { inode = file->f_path.dentry->d_inode; if (S_ISSOCK(inode->i_mode)) { lo->file = file; lo->sock = SOCKET_I(inode); error = 0; } else { fput(file); } } return error; case NBD_SET_BLKSIZE: lo->blksize = arg; lo->bytesize &= ~(lo->blksize-1); inode->i_bdev->bd_inode->i_size = lo->bytesize; set_blocksize(inode->i_bdev, lo->blksize); set_capacity(lo->disk, lo->bytesize >> 9); return 0; case NBD_SET_SIZE: lo->bytesize = arg & ~(lo->blksize-1); inode->i_bdev->bd_inode->i_size = lo->bytesize; set_blocksize(inode->i_bdev, lo->blksize); set_capacity(lo->disk, lo->bytesize >> 9); return 0; case NBD_SET_TIMEOUT: lo->xmit_timeout = arg * HZ; return 0; case NBD_SET_SIZE_BLOCKS: lo->bytesize = ((u64) arg) * lo->blksize; inode->i_bdev->bd_inode->i_size = lo->bytesize; set_blocksize(inode->i_bdev, lo->blksize); set_capacity(lo->disk, lo->bytesize >> 9); return 0; case NBD_DO_IT: if (!lo->file) return -EINVAL; error = nbd_do_it(lo); if (error) return error; sock_shutdown(lo, 1); file = lo->file; lo->file = NULL; nbd_clear_que(lo); printk(KERN_WARNING "%s: queue cleared\n", lo->disk->disk_name); if (file) fput(file); lo->bytesize = 0; inode->i_bdev->bd_inode->i_size = 0; set_capacity(lo->disk, 0); return lo->harderror; case NBD_CLEAR_QUE: /* * This is for compatibility only. The queue is always cleared * by NBD_DO_IT or NBD_CLEAR_SOCK. */ BUG_ON(!lo->sock && !list_empty(&lo->queue_head)); return 0; case NBD_PRINT_DEBUG: printk(KERN_INFO "%s: next = %p, prev = %p, head = %p\n", inode->i_bdev->bd_disk->disk_name, lo->queue_head.next, lo->queue_head.prev, &lo->queue_head); return 0; } return -EINVAL;}static struct block_device_operations nbd_fops ={ .owner = THIS_MODULE, .ioctl = nbd_ioctl,};/* * And here should be modules and kernel interface * (Just smiley confuses emacs :-) */static int __init nbd_init(void){ int err = -ENOMEM; int i; BUILD_BUG_ON(sizeof(struct nbd_request) != 28); if (nbds_max > MAX_NBD) { printk(KERN_CRIT "nbd: cannot allocate more than %u nbds; %u requested.\n", MAX_NBD, nbds_max); return -EINVAL; } for (i = 0; i < nbds_max; i++) { struct gendisk *disk = alloc_disk(1); if (!disk) goto out; nbd_dev[i].disk = disk; /* * The new linux 2.5 block layer implementation requires * every gendisk to have its very own request_queue struct. * These structs are big so we dynamically allocate them. */ disk->queue = blk_init_queue(do_nbd_request, &nbd_lock); if (!disk->queue) { put_disk(disk); goto out; } } if (register_blkdev(NBD_MAJOR, "nbd")) { err = -EIO; goto out; } printk(KERN_INFO "nbd: registered device at major %d\n", NBD_MAJOR); dprintk(DBG_INIT, "nbd: debugflags=0x%x\n", debugflags); for (i = 0; i < nbds_max; i++) { struct gendisk *disk = nbd_dev[i].disk; nbd_dev[i].file = NULL; nbd_dev[i].magic = LO_MAGIC; nbd_dev[i].flags = 0; spin_lock_init(&nbd_dev[i].queue_lock); INIT_LIST_HEAD(&nbd_dev[i].queue_head); mutex_init(&nbd_dev[i].tx_lock); init_waitqueue_head(&nbd_dev[i].active_wq); nbd_dev[i].blksize = 1024; nbd_dev[i].bytesize = 0; disk->major = NBD_MAJOR; disk->first_minor = i; disk->fops = &nbd_fops; disk->private_data = &nbd_dev[i]; disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO; sprintf(disk->disk_name, "nbd%d", i); set_capacity(disk, 0); add_disk(disk); } return 0;out: while (i--) { blk_cleanup_queue(nbd_dev[i].disk->queue); put_disk(nbd_dev[i].disk); } return err;}static void __exit nbd_cleanup(void){ int i; for (i = 0; i < nbds_max; i++) { struct gendisk *disk = nbd_dev[i].disk; nbd_dev[i].magic = 0; if (disk) { del_gendisk(disk); blk_cleanup_queue(disk->queue); put_disk(disk); } } unregister_blkdev(NBD_MAJOR, "nbd"); printk(KERN_INFO "nbd: unregistered device at major %d\n", NBD_MAJOR);}module_init(nbd_init);module_exit(nbd_cleanup);MODULE_DESCRIPTION("Network Block Device");MODULE_LICENSE("GPL");module_param(nbds_max, int, 0444);MODULE_PARM_DESC(nbds_max, "How many network block devices to initialize.");#ifndef NDEBUGmodule_param(debugflags, int, 0644);MODULE_PARM_DESC(debugflags, "flags for controlling debug output");#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?