aboutsummaryrefslogblamecommitdiffstats
path: root/fs/nfsd/nfs4idmap.c
blob: 5b398421b0518130240157dd2b02aa4c77a6ed0a (plain) (tree)



































                                                                           











                              


                               























                                                                      





                                                 

                                                          
 


                                                            






                                                                 
           
                         
 

                                                               

 









                                                        




























                                                                            
                                                      





                                                                               

                                                            
 


                                                        













                                                                                
                                                
















                                                                         

                                                               
 
                                             
                                      







                                           



                                         

  
          



                                                              
                


































                                                         




                                    
                  


                                               
                         
                     
                                                      


                                     
                                                         
                        

                                         

                         
                                            







                     
























                                                                         












                                                      
           











                                                                               

                                                            
 


                                                        

















                                                                                
                                              



                            

                                                               
                                                                      
 
                                             
                                      







                                           



                                         

  
          













































                                                              




                                         

                         
                                            






                       























                                                                         




               
   

                     








                                                  




                         

                                          















































                                                                         
                                                                        


                                                               
                               





                                                           
                                                              




                                                                     
                               











                                                                        
                                       






                                    
                                                                        




                                                               
                                                

                               












                                                                           








                                                                           












                                                                                 
                                                                          





                                                                                 
                                             











                                                                        
                                                                          







                                                                                 
                                             



























                                                                              
/*
 *  fs/nfsd/nfs4idmap.c
 *
 *  Mapping of UID/GIDs to name and vice versa.
 *
 *  Copyright (c) 2002, 2003 The Regents of the University of
 *  Michigan.  All rights reserved.
 *
 *  Marius Aamodt Eriksen <marius@umich.edu>
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *  3. Neither the name of the University nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <linux/module.h>
#include <linux/init.h>

#include <linux/mm.h>
#include <linux/utsname.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/sunrpc/clnt.h>
#include <linux/nfs.h>
#include <linux/nfs4.h>
#include <linux/nfs_fs.h>
#include <linux/nfs_page.h>
#include <linux/sunrpc/cache.h>
#include <linux/nfsd_idmap.h>
#include <linux/list.h>
#include <linux/time.h>
#include <linux/seq_file.h>
#include <linux/sunrpc/svcauth.h>

/*
 * Cache entry
 */

/*
 * XXX we know that IDMAP_NAMESZ < PAGE_SIZE, but it's ugly to rely on
 * that.
 */

#define IDMAP_TYPE_USER  0
#define IDMAP_TYPE_GROUP 1

struct ent {
	struct cache_head h;
	int               type;		       /* User / Group */
	uid_t             id;
	char              name[IDMAP_NAMESZ];
	char              authname[IDMAP_NAMESZ];
};

/* Common entry handling */

#define ENT_HASHBITS          8
#define ENT_HASHMAX           (1 << ENT_HASHBITS)
#define ENT_HASHMASK          (ENT_HASHMAX - 1)

static void
ent_init(struct cache_head *cnew, struct cache_head *citm)
{
	struct ent *new = container_of(cnew, struct ent, h);
	struct ent *itm = container_of(citm, struct ent, h);

	new->id = itm->id;
	new->type = itm->type;

	strlcpy(new->name, itm->name, sizeof(new->name));
	strlcpy(new->authname, itm->authname, sizeof(new->name));
}

static void
ent_put(struct kref *ref)
{
	struct ent *map = container_of(ref, struct ent, h.ref);
	kfree(map);
}

static struct cache_head *
ent_alloc(void)
{
	struct ent *e = kmalloc(sizeof(*e), GFP_KERNEL);
	if (e)
		return &e->h;
	else
		return NULL;
}

/*
 * ID -> Name cache
 */

static struct cache_head *idtoname_table[ENT_HASHMAX];

static uint32_t
idtoname_hash(struct ent *ent)
{
	uint32_t hash;

	hash = hash_str(ent->authname, ENT_HASHBITS);
	hash = hash_long(hash ^ ent->id, ENT_HASHBITS);

	/* Flip LSB for user/group */
	if (ent->type == IDMAP_TYPE_GROUP)
		hash ^= 1;

	return hash;
}

static void
idtoname_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
    int *blen)
{
 	struct ent *ent = container_of(ch, struct ent, h);
	char idstr[11];

	qword_add(bpp, blen, ent->authname);
	snprintf(idstr, sizeof(idstr), "%u", ent->id);
	qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
	qword_add(bpp, blen, idstr);

	(*bpp)[-1] = '\n';
}

static int
idtoname_match(struct cache_head *ca, struct cache_head *cb)
{
	struct ent *a = container_of(ca, struct ent, h);
	struct ent *b = container_of(cb, struct ent, h);

	return (a->id == b->id && a->type == b->type &&
	    strcmp(a->authname, b->authname) == 0);
}

static int
idtoname_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
{
	struct ent *ent;

	if (h == NULL) {
		seq_puts(m, "#domain type id [name]\n");
		return 0;
	}
	ent = container_of(h, struct ent, h);
	seq_printf(m, "%s %s %u", ent->authname,
			ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
			ent->id);
	if (test_bit(CACHE_VALID, &h->flags))
		seq_printf(m, " %s", ent->name);
	seq_printf(m, "\n");
	return 0;
}

static void
warn_no_idmapd(struct cache_detail *detail)
{
	printk("nfsd: nfsv4 idmapping failing: has idmapd %s?\n",
			detail->last_close? "died" : "not been started");
}


static int         idtoname_parse(struct cache_detail *, char *, int);
static struct ent *idtoname_lookup(struct ent *);
static struct ent *idtoname_update(struct ent *, struct ent *);

static struct cache_detail idtoname_cache = {
	.owner		= THIS_MODULE,
	.hash_size	= ENT_HASHMAX,
	.hash_table	= idtoname_table,
	.name		= "nfs4.idtoname",
	.cache_put	= ent_put,
	.cache_request	= idtoname_request,
	.cache_parse	= idtoname_parse,
	.cache_show	= idtoname_show,
	.warn_no_listener = warn_no_idmapd,
	.match		= idtoname_match,
	.init		= ent_init,
	.update		= ent_init,
	.alloc		= ent_alloc,
};

static int
idtoname_parse(struct cache_detail *cd, char *buf, int buflen)
{
	struct ent ent, *res;
	char *buf1, *bp;
	int len;
	int error = -EINVAL;

	if (buf[buflen - 1] != '\n')
		return (-EINVAL);
	buf[buflen - 1]= '\0';

	buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if (buf1 == NULL)
		return (-ENOMEM);

	memset(&ent, 0, sizeof(ent));

	/* Authentication name */
	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
		goto out;
	memcpy(ent.authname, buf1, sizeof(ent.authname));

	/* Type */
	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
		goto out;
	ent.type = strcmp(buf1, "user") == 0 ?
		IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;

	/* ID */
	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
		goto out;
	ent.id = simple_strtoul(buf1, &bp, 10);
	if (bp == buf1)
		goto out;

	/* expiry */
	ent.h.expiry_time = get_expiry(&buf);
	if (ent.h.expiry_time == 0)
		goto out;

	error = -ENOMEM;
	res = idtoname_lookup(&ent);
	if (!res)
		goto out;

	/* Name */
	error = -EINVAL;
	len = qword_get(&buf, buf1, PAGE_SIZE);
	if (len < 0)
		goto out;
	if (len == 0)
		set_bit(CACHE_NEGATIVE, &ent.h.flags);
	else if (len >= IDMAP_NAMESZ)
		goto out;
	else
		memcpy(ent.name, buf1, sizeof(ent.name));
	error = -ENOMEM;
	res = idtoname_update(&ent, res);
	if (res == NULL)
		goto out;

	cache_put(&res->h, &idtoname_cache);

	error = 0;
out:
	kfree(buf1);

	return error;
}


static struct ent *
idtoname_lookup(struct ent *item)
{
	struct cache_head *ch = sunrpc_cache_lookup(&idtoname_cache,
						    &item->h,
						    idtoname_hash(item));
	if (ch)
		return container_of(ch, struct ent, h);
	else
		return NULL;
}

static struct ent *
idtoname_update(struct ent *new, struct ent *old)
{
	struct cache_head *ch = sunrpc_cache_update(&idtoname_cache,
						    &new->h, &old->h,
						    idtoname_hash(new));
	if (ch)
		return container_of(ch, struct ent, h);
	else
		return NULL;
}


/*
 * Name -> ID cache
 */

static struct cache_head *nametoid_table[ENT_HASHMAX];

static inline int
nametoid_hash(struct ent *ent)
{
	return hash_str(ent->name, ENT_HASHBITS);
}

static void
nametoid_request(struct cache_detail *cd, struct cache_head *ch, char **bpp,
    int *blen)
{
 	struct ent *ent = container_of(ch, struct ent, h);

	qword_add(bpp, blen, ent->authname);
	qword_add(bpp, blen, ent->type == IDMAP_TYPE_GROUP ? "group" : "user");
	qword_add(bpp, blen, ent->name);

	(*bpp)[-1] = '\n';
}

static int
nametoid_match(struct cache_head *ca, struct cache_head *cb)
{
	struct ent *a = container_of(ca, struct ent, h);
	struct ent *b = container_of(cb, struct ent, h);

	return (a->type == b->type && strcmp(a->name, b->name) == 0 &&
	    strcmp(a->authname, b->authname) == 0);
}

static int
nametoid_show(struct seq_file *m, struct cache_detail *cd, struct cache_head *h)
{
	struct ent *ent;

	if (h == NULL) {
		seq_puts(m, "#domain type name [id]\n");
		return 0;
	}
	ent = container_of(h, struct ent, h);
	seq_printf(m, "%s %s %s", ent->authname,
			ent->type == IDMAP_TYPE_GROUP ? "group" : "user",
			ent->name);
	if (test_bit(CACHE_VALID, &h->flags))
		seq_printf(m, " %u", ent->id);
	seq_printf(m, "\n");
	return 0;
}

static struct ent *nametoid_lookup(struct ent *);
static struct ent *nametoid_update(struct ent *, struct ent *);
static int         nametoid_parse(struct cache_detail *, char *, int);

static struct cache_detail nametoid_cache = {
	.owner		= THIS_MODULE,
	.hash_size	= ENT_HASHMAX,
	.hash_table	= nametoid_table,
	.name		= "nfs4.nametoid",
	.cache_put	= ent_put,
	.cache_request	= nametoid_request,
	.cache_parse	= nametoid_parse,
	.cache_show	= nametoid_show,
	.warn_no_listener = warn_no_idmapd,
	.match		= nametoid_match,
	.init		= ent_init,
	.update		= ent_init,
	.alloc		= ent_alloc,
};

static int
nametoid_parse(struct cache_detail *cd, char *buf, int buflen)
{
	struct ent ent, *res;
	char *buf1;
	int error = -EINVAL;

	if (buf[buflen - 1] != '\n')
		return (-EINVAL);
	buf[buflen - 1]= '\0';

	buf1 = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if (buf1 == NULL)
		return (-ENOMEM);

	memset(&ent, 0, sizeof(ent));

	/* Authentication name */
	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
		goto out;
	memcpy(ent.authname, buf1, sizeof(ent.authname));

	/* Type */
	if (qword_get(&buf, buf1, PAGE_SIZE) <= 0)
		goto out;
	ent.type = strcmp(buf1, "user") == 0 ?
		IDMAP_TYPE_USER : IDMAP_TYPE_GROUP;

	/* Name */
	error = qword_get(&buf, buf1, PAGE_SIZE);
	if (error <= 0 || error >= IDMAP_NAMESZ)
		goto out;
	memcpy(ent.name, buf1, sizeof(ent.name));

	/* expiry */
	ent.h.expiry_time = get_expiry(&buf);
	if (ent.h.expiry_time == 0)
		goto out;

	/* ID */
	error = get_int(&buf, &ent.id);
	if (error == -EINVAL)
		goto out;
	if (error == -ENOENT)
		set_bit(CACHE_NEGATIVE, &ent.h.flags);

	error = -ENOMEM;
	res = nametoid_lookup(&ent);
	if (res == NULL)
		goto out;
	res = nametoid_update(&ent, res);
	if (res == NULL)
		goto out;

	cache_put(&res->h, &nametoid_cache);
	error = 0;
out:
	kfree(buf1);

	return (error);
}


static struct ent *
nametoid_lookup(struct ent *item)
{
	struct cache_head *ch = sunrpc_cache_lookup(&nametoid_cache,
						    &item->h,
						    nametoid_hash(item));
	if (ch)
		return container_of(ch, struct ent, h);
	else
		return NULL;
}

static struct ent *
nametoid_update(struct ent *new, struct ent *old)
{
	struct cache_head *ch = sunrpc_cache_update(&nametoid_cache,
						    &new->h, &old->h,
						    nametoid_hash(new));
	if (ch)
		return container_of(ch, struct ent, h);
	else
		return NULL;
}

/*
 * Exported API
 */

int
nfsd_idmap_init(void)
{
	int rv;

	rv = cache_register(&idtoname_cache);
	if (rv)
		return rv;
	rv = cache_register(&nametoid_cache);
	if (rv)
		cache_unregister(&idtoname_cache);
	return rv;
}

void
nfsd_idmap_shutdown(void)
{
	cache_unregister(&idtoname_cache);
	cache_unregister(&nametoid_cache);
}

/*
 * Deferred request handling
 */

struct idmap_defer_req {
       struct cache_req		req;
       struct cache_deferred_req deferred_req;
       wait_queue_head_t	waitq;
       atomic_t			count;
};

static inline void
put_mdr(struct idmap_defer_req *mdr)
{
	if (atomic_dec_and_test(&mdr->count))
		kfree(mdr);
}

static inline void
get_mdr(struct idmap_defer_req *mdr)
{
	atomic_inc(&mdr->count);
}

static void
idmap_revisit(struct cache_deferred_req *dreq, int toomany)
{
	struct idmap_defer_req *mdr =
		container_of(dreq, struct idmap_defer_req, deferred_req);

	wake_up(&mdr->waitq);
	put_mdr(mdr);
}

static struct cache_deferred_req *
idmap_defer(struct cache_req *req)
{
	struct idmap_defer_req *mdr =
		container_of(req, struct idmap_defer_req, req);

	mdr->deferred_req.revisit = idmap_revisit;
	get_mdr(mdr);
	return (&mdr->deferred_req);
}

static inline int
do_idmap_lookup(struct ent *(*lookup_fn)(struct ent *), struct ent *key,
		struct cache_detail *detail, struct ent **item,
		struct idmap_defer_req *mdr)
{
	*item = lookup_fn(key);
	if (!*item)
		return -ENOMEM;
	return cache_check(detail, &(*item)->h, &mdr->req);
}

static inline int
do_idmap_lookup_nowait(struct ent *(*lookup_fn)(struct ent *),
			struct ent *key, struct cache_detail *detail,
			struct ent **item)
{
	int ret = -ENOMEM;

	*item = lookup_fn(key);
	if (!*item)
		goto out_err;
	ret = -ETIMEDOUT;
	if (!test_bit(CACHE_VALID, &(*item)->h.flags)
			|| (*item)->h.expiry_time < get_seconds()
			|| detail->flush_time > (*item)->h.last_refresh)
		goto out_put;
	ret = -ENOENT;
	if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
		goto out_put;
	return 0;
out_put:
	cache_put(&(*item)->h, detail);
out_err:
	*item = NULL;
	return ret;
}

static int
idmap_lookup(struct svc_rqst *rqstp,
		struct ent *(*lookup_fn)(struct ent *), struct ent *key,
		struct cache_detail *detail, struct ent **item)
{
	struct idmap_defer_req *mdr;
	int ret;

	mdr = kzalloc(sizeof(*mdr), GFP_KERNEL);
	if (!mdr)
		return -ENOMEM;
	atomic_set(&mdr->count, 1);
	init_waitqueue_head(&mdr->waitq);
	mdr->req.defer = idmap_defer;
	ret = do_idmap_lookup(lookup_fn, key, detail, item, mdr);
	if (ret == -EAGAIN) {
		wait_event_interruptible_timeout(mdr->waitq,
			test_bit(CACHE_VALID, &(*item)->h.flags), 1 * HZ);
		ret = do_idmap_lookup_nowait(lookup_fn, key, detail, item);
	}
	put_mdr(mdr);
	return ret;
}

static char *
rqst_authname(struct svc_rqst *rqstp)
{
	struct auth_domain *clp;

	clp = rqstp->rq_gssclient ? rqstp->rq_gssclient : rqstp->rq_client;
	return clp->name;
}

static int
idmap_name_to_id(struct svc_rqst *rqstp, int type, const char *name, u32 namelen,
		uid_t *id)
{
	struct ent *item, key = {
		.type = type,
	};
	int ret;

	if (namelen + 1 > sizeof(key.name))
		return -EINVAL;
	memcpy(key.name, name, namelen);
	key.name[namelen] = '\0';
	strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
	ret = idmap_lookup(rqstp, nametoid_lookup, &key, &nametoid_cache, &item);
	if (ret == -ENOENT)
		ret = -ESRCH; /* nfserr_badname */
	if (ret)
		return ret;
	*id = item->id;
	cache_put(&item->h, &nametoid_cache);
	return 0;
}

static int
idmap_id_to_name(struct svc_rqst *rqstp, int type, uid_t id, char *name)
{
	struct ent *item, key = {
		.id = id,
		.type = type,
	};
	int ret;

	strlcpy(key.authname, rqst_authname(rqstp), sizeof(key.authname));
	ret = idmap_lookup(rqstp, idtoname_lookup, &key, &idtoname_cache, &item);
	if (ret == -ENOENT)
		return sprintf(name, "%u", id);
	if (ret)
		return ret;
	ret = strlen(item->name);
	BUG_ON(ret > IDMAP_NAMESZ);
	memcpy(name, item->name, ret);
	cache_put(&item->h, &idtoname_cache);
	return ret;
}

int
nfsd_map_name_to_uid(struct svc_rqst *rqstp, const char *name, size_t namelen,
		__u32 *id)
{
	return idmap_name_to_id(rqstp, IDMAP_TYPE_USER, name, namelen, id);
}

int
nfsd_map_name_to_gid(struct svc_rqst *rqstp, const char *name, size_t namelen,
		__u32 *id)
{
	return idmap_name_to_id(rqstp, IDMAP_TYPE_GROUP, name, namelen, id);
}

int
nfsd_map_uid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
{
	return idmap_id_to_name(rqstp, IDMAP_TYPE_USER, id, name);
}

int
nfsd_map_gid_to_name(struct svc_rqst *rqstp, __u32 id, char *name)
{
	return idmap_id_to_name(rqstp, IDMAP_TYPE_GROUP, id, name);
}
f (tape->buf != NULL) { kfree(tape->buf); tape->buf = NULL; } tape->chrdev_dir = IDETAPE_DIR_NONE; } /* * Position the tape to the requested block using the LOCATE packet command. * A READ POSITION command is then issued to check where we are positioned. Like * all higher level operations, we queue the commands at the tail of the request * queue and wait for their completion. */ static int idetape_position_tape(ide_drive_t *drive, unsigned int block, u8 partition, int skip) { idetape_tape_t *tape = drive->driver_data; struct gendisk *disk = tape->disk; int ret; struct ide_atapi_pc pc; if (tape->chrdev_dir == IDETAPE_DIR_READ) __ide_tape_discard_merge_buffer(drive); idetape_wait_ready(drive, 60 * 5 * HZ); idetape_create_locate_cmd(drive, &pc, block, partition, skip); ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0); if (ret) return ret; ret = ide_tape_read_position(drive); if (ret < 0) return ret; return 0; } static void ide_tape_discard_merge_buffer(ide_drive_t *drive, int restore_position) { idetape_tape_t *tape = drive->driver_data; int seek, position; __ide_tape_discard_merge_buffer(drive); if (restore_position) { position = ide_tape_read_position(drive); seek = position > 0 ? position : 0; if (idetape_position_tape(drive, seek, 0, 0)) { printk(KERN_INFO "ide-tape: %s: position_tape failed in" " %s\n", tape->name, __func__); return; } } } /* * Generate a read/write request for the block device interface and wait for it * to be serviced. */ static int idetape_queue_rw_tail(ide_drive_t *drive, int cmd, int size) { idetape_tape_t *tape = drive->driver_data; struct request *rq; int ret; ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, size: %d", cmd, size); BUG_ON(cmd != REQ_IDETAPE_READ && cmd != REQ_IDETAPE_WRITE); BUG_ON(size < 0 || size % tape->blk_size); rq = blk_get_request(drive->queue, READ, __GFP_WAIT); rq->cmd_type = REQ_TYPE_SPECIAL; rq->cmd[13] = cmd; rq->rq_disk = tape->disk; rq->__sector = tape->first_frame; if (size) { ret = blk_rq_map_kern(drive->queue, rq, tape->buf, size, __GFP_WAIT); if (ret) goto out_put; } blk_execute_rq(drive->queue, tape->disk, rq, 0); /* calculate the number of transferred bytes and update buffer state */ size -= rq->resid_len; tape->cur = tape->buf; if (cmd == REQ_IDETAPE_READ) tape->valid = size; else tape->valid = 0; ret = size; if (rq->errors == IDE_DRV_ERROR_GENERAL) ret = -EIO; out_put: blk_put_request(rq); return ret; } static void idetape_create_inquiry_cmd(struct ide_atapi_pc *pc) { ide_init_pc(pc); pc->c[0] = INQUIRY; pc->c[4] = 254; pc->req_xfer = 254; } static void idetape_create_rewind_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc) { ide_init_pc(pc); pc->c[0] = REZERO_UNIT; pc->flags |= PC_FLAG_WAIT_FOR_DSC; } static void idetape_create_erase_cmd(struct ide_atapi_pc *pc) { ide_init_pc(pc); pc->c[0] = ERASE; pc->c[1] = 1; pc->flags |= PC_FLAG_WAIT_FOR_DSC; } static void idetape_create_space_cmd(struct ide_atapi_pc *pc, int count, u8 cmd) { ide_init_pc(pc); pc->c[0] = SPACE; put_unaligned(cpu_to_be32(count), (unsigned int *) &pc->c[1]); pc->c[1] = cmd; pc->flags |= PC_FLAG_WAIT_FOR_DSC; } static void ide_tape_flush_merge_buffer(ide_drive_t *drive) { idetape_tape_t *tape = drive->driver_data; if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { printk(KERN_ERR "ide-tape: bug: Trying to empty merge buffer" " but we are not writing.\n"); return; } if (tape->buf) { size_t aligned = roundup(tape->valid, tape->blk_size); memset(tape->cur, 0, aligned - tape->valid); idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, aligned); kfree(tape->buf); tape->buf = NULL; } tape->chrdev_dir = IDETAPE_DIR_NONE; } static int idetape_init_rw(ide_drive_t *drive, int dir) { idetape_tape_t *tape = drive->driver_data; int rc; BUG_ON(dir != IDETAPE_DIR_READ && dir != IDETAPE_DIR_WRITE); if (tape->chrdev_dir == dir) return 0; if (tape->chrdev_dir == IDETAPE_DIR_READ) ide_tape_discard_merge_buffer(drive, 1); else if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { ide_tape_flush_merge_buffer(drive); idetape_flush_tape_buffers(drive); } if (tape->buf || tape->valid) { printk(KERN_ERR "ide-tape: valid should be 0 now\n"); tape->valid = 0; } tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL); if (!tape->buf) return -ENOMEM; tape->chrdev_dir = dir; tape->cur = tape->buf; /* * Issue a 0 rw command to ensure that DSC handshake is * switched from completion mode to buffer available mode. No * point in issuing this if DSC overlap isn't supported, some * drives (Seagate STT3401A) will return an error. */ if (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP) { int cmd = dir == IDETAPE_DIR_READ ? REQ_IDETAPE_READ : REQ_IDETAPE_WRITE; rc = idetape_queue_rw_tail(drive, cmd, 0); if (rc < 0) { kfree(tape->buf); tape->buf = NULL; tape->chrdev_dir = IDETAPE_DIR_NONE; return rc; } } return 0; } static void idetape_pad_zeros(ide_drive_t *drive, int bcount) { idetape_tape_t *tape = drive->driver_data; memset(tape->buf, 0, tape->buffer_size); while (bcount) { unsigned int count = min(tape->buffer_size, bcount); idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, count); bcount -= count; } } /* * Rewinds the tape to the Beginning Of the current Partition (BOP). We * currently support only one partition. */ static int idetape_rewind_tape(ide_drive_t *drive) { struct ide_tape_obj *tape = drive->driver_data; struct gendisk *disk = tape->disk; struct ide_atapi_pc pc; int ret; ide_debug_log(IDE_DBG_FUNC, "enter"); idetape_create_rewind_cmd(drive, &pc); ret = ide_queue_pc_tail(drive, disk, &pc, NULL, 0); if (ret) return ret; ret = ide_tape_read_position(drive); if (ret < 0) return ret; return 0; } /* mtio.h compatible commands should be issued to the chrdev interface. */ static int idetape_blkdev_ioctl(ide_drive_t *drive, unsigned int cmd, unsigned long arg) { idetape_tape_t *tape = drive->driver_data; void __user *argp = (void __user *)arg; struct idetape_config { int dsc_rw_frequency; int dsc_media_access_frequency; int nr_stages; } config; ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%04x", cmd); switch (cmd) { case 0x0340: if (copy_from_user(&config, argp, sizeof(config))) return -EFAULT; tape->best_dsc_rw_freq = config.dsc_rw_frequency; break; case 0x0350: memset(&config, 0, sizeof(config)); config.dsc_rw_frequency = (int) tape->best_dsc_rw_freq; config.nr_stages = 1; if (copy_to_user(argp, &config, sizeof(config))) return -EFAULT; break; default: return -EIO; } return 0; } static int idetape_space_over_filemarks(ide_drive_t *drive, short mt_op, int mt_count) { idetape_tape_t *tape = drive->driver_data; struct gendisk *disk = tape->disk; struct ide_atapi_pc pc; int retval, count = 0; int sprev = !!(tape->caps[4] & 0x20); ide_debug_log(IDE_DBG_FUNC, "mt_op: %d, mt_count: %d", mt_op, mt_count); if (mt_count == 0) return 0; if (MTBSF == mt_op || MTBSFM == mt_op) { if (!sprev) return -EIO; mt_count = -mt_count; } if (tape->chrdev_dir == IDETAPE_DIR_READ) { tape->valid = 0; if (test_and_clear_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) ++count; ide_tape_discard_merge_buffer(drive, 0); } switch (mt_op) { case MTFSF: case MTBSF: idetape_create_space_cmd(&pc, mt_count - count, IDETAPE_SPACE_OVER_FILEMARK); return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); case MTFSFM: case MTBSFM: if (!sprev) return -EIO; retval = idetape_space_over_filemarks(drive, MTFSF, mt_count - count); if (retval) return retval; count = (MTBSFM == mt_op ? 1 : -1); return idetape_space_over_filemarks(drive, MTFSF, count); default: printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", mt_op); return -EIO; } } /* * Our character device read / write functions. * * The tape is optimized to maximize throughput when it is transferring an * integral number of the "continuous transfer limit", which is a parameter of * the specific tape (26kB on my particular tape, 32kB for Onstream). * * As of version 1.3 of the driver, the character device provides an abstract * continuous view of the media - any mix of block sizes (even 1 byte) on the * same backup/restore procedure is supported. The driver will internally * convert the requests to the recommended transfer unit, so that an unmatch * between the user's block size to the recommended size will only result in a * (slightly) increased driver overhead, but will no longer hit performance. * This is not applicable to Onstream. */ static ssize_t idetape_chrdev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { struct ide_tape_obj *tape = file->private_data; ide_drive_t *drive = tape->drive; size_t done = 0; ssize_t ret = 0; int rc; ide_debug_log(IDE_DBG_FUNC, "count %Zd", count); if (tape->chrdev_dir != IDETAPE_DIR_READ) { if (test_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags)) if (count > tape->blk_size && (count % tape->blk_size) == 0) tape->user_bs_factor = count / tape->blk_size; } rc = idetape_init_rw(drive, IDETAPE_DIR_READ); if (rc < 0) return rc; while (done < count) { size_t todo; /* refill if staging buffer is empty */ if (!tape->valid) { /* If we are at a filemark, nothing more to read */ if (test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) break; /* read */ if (idetape_queue_rw_tail(drive, REQ_IDETAPE_READ, tape->buffer_size) <= 0) break; } /* copy out */ todo = min_t(size_t, count - done, tape->valid); if (copy_to_user(buf + done, tape->cur, todo)) ret = -EFAULT; tape->cur += todo; tape->valid -= todo; done += todo; } if (!done && test_bit(ilog2(IDE_AFLAG_FILEMARK), &drive->atapi_flags)) { idetape_space_over_filemarks(drive, MTFSF, 1); return 0; } return ret ? ret : done; } static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { struct ide_tape_obj *tape = file->private_data; ide_drive_t *drive = tape->drive; size_t done = 0; ssize_t ret = 0; int rc; /* The drive is write protected. */ if (tape->write_prot) return -EACCES; ide_debug_log(IDE_DBG_FUNC, "count %Zd", count); /* Initialize write operation */ rc = idetape_init_rw(drive, IDETAPE_DIR_WRITE); if (rc < 0) return rc; while (done < count) { size_t todo; /* flush if staging buffer is full */ if (tape->valid == tape->buffer_size && idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, tape->buffer_size) <= 0) return rc; /* copy in */ todo = min_t(size_t, count - done, tape->buffer_size - tape->valid); if (copy_from_user(tape->cur, buf + done, todo)) ret = -EFAULT; tape->cur += todo; tape->valid += todo; done += todo; } return ret ? ret : done; } static int idetape_write_filemark(ide_drive_t *drive) { struct ide_tape_obj *tape = drive->driver_data; struct ide_atapi_pc pc; /* Write a filemark */ idetape_create_write_filemark_cmd(drive, &pc, 1); if (ide_queue_pc_tail(drive, tape->disk, &pc, NULL, 0)) { printk(KERN_ERR "ide-tape: Couldn't write a filemark\n"); return -EIO; } return 0; } /* * Called from idetape_chrdev_ioctl when the general mtio MTIOCTOP ioctl is * requested. * * Note: MTBSF and MTBSFM are not supported when the tape doesn't support * spacing over filemarks in the reverse direction. In this case, MTFSFM is also * usually not supported. * * The following commands are currently not supported: * * MTFSS, MTBSS, MTWSM, MTSETDENSITY, MTSETDRVBUFFER, MT_ST_BOOLEANS, * MT_ST_WRITE_THRESHOLD. */ static int idetape_mtioctop(ide_drive_t *drive, short mt_op, int mt_count) { idetape_tape_t *tape = drive->driver_data; struct gendisk *disk = tape->disk; struct ide_atapi_pc pc; int i, retval; ide_debug_log(IDE_DBG_FUNC, "MTIOCTOP ioctl: mt_op: %d, mt_count: %d", mt_op, mt_count); switch (mt_op) { case MTFSF: case MTFSFM: case MTBSF: case MTBSFM: if (!mt_count) return 0; return idetape_space_over_filemarks(drive, mt_op, mt_count); default: break; } switch (mt_op) { case MTWEOF: if (tape->write_prot) return -EACCES; ide_tape_discard_merge_buffer(drive, 1); for (i = 0; i < mt_count; i++) { retval = idetape_write_filemark(drive); if (retval) return retval; } return 0; case MTREW: ide_tape_discard_merge_buffer(drive, 0); if (idetape_rewind_tape(drive)) return -EIO; return 0; case MTLOAD: ide_tape_discard_merge_buffer(drive, 0); return ide_do_start_stop(drive, disk, IDETAPE_LU_LOAD_MASK); case MTUNLOAD: case MTOFFL: /* * If door is locked, attempt to unlock before * attempting to eject. */ if (tape->door_locked) { if (!ide_set_media_lock(drive, disk, 0)) tape->door_locked = DOOR_UNLOCKED; } ide_tape_discard_merge_buffer(drive, 0); retval = ide_do_start_stop(drive, disk, !IDETAPE_LU_LOAD_MASK); if (!retval) clear_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags); return retval; case MTNOP: ide_tape_discard_merge_buffer(drive, 0); return idetape_flush_tape_buffers(drive); case MTRETEN: ide_tape_discard_merge_buffer(drive, 0); return ide_do_start_stop(drive, disk, IDETAPE_LU_RETENSION_MASK | IDETAPE_LU_LOAD_MASK); case MTEOM: idetape_create_space_cmd(&pc, 0, IDETAPE_SPACE_TO_EOD); return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); case MTERASE: (void)idetape_rewind_tape(drive); idetape_create_erase_cmd(&pc); return ide_queue_pc_tail(drive, disk, &pc, NULL, 0); case MTSETBLK: if (mt_count) { if (mt_count < tape->blk_size || mt_count % tape->blk_size) return -EIO; tape->user_bs_factor = mt_count / tape->blk_size; clear_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags); } else set_bit(ilog2(IDE_AFLAG_DETECT_BS), &drive->atapi_flags); return 0; case MTSEEK: ide_tape_discard_merge_buffer(drive, 0); return idetape_position_tape(drive, mt_count * tape->user_bs_factor, tape->partition, 0); case MTSETPART: ide_tape_discard_merge_buffer(drive, 0); return idetape_position_tape(drive, 0, mt_count, 0); case MTFSR: case MTBSR: case MTLOCK: retval = ide_set_media_lock(drive, disk, 1); if (retval) return retval; tape->door_locked = DOOR_EXPLICITLY_LOCKED; return 0; case MTUNLOCK: retval = ide_set_media_lock(drive, disk, 0); if (retval) return retval; tape->door_locked = DOOR_UNLOCKED; return 0; default: printk(KERN_ERR "ide-tape: MTIO operation %d not supported\n", mt_op); return -EIO; } } /* * Our character device ioctls. General mtio.h magnetic io commands are * supported here, and not in the corresponding block interface. Our own * ide-tape ioctls are supported on both interfaces. */ static long do_idetape_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct ide_tape_obj *tape = file->private_data; ide_drive_t *drive = tape->drive; struct mtop mtop; struct mtget mtget; struct mtpos mtpos; int block_offset = 0, position = tape->first_frame; void __user *argp = (void __user *)arg; ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x", cmd); if (tape->chrdev_dir == IDETAPE_DIR_WRITE) { ide_tape_flush_merge_buffer(drive); idetape_flush_tape_buffers(drive); } if (cmd == MTIOCGET || cmd == MTIOCPOS) { block_offset = tape->valid / (tape->blk_size * tape->user_bs_factor); position = ide_tape_read_position(drive); if (position < 0) return -EIO; } switch (cmd) { case MTIOCTOP: if (copy_from_user(&mtop, argp, sizeof(struct mtop))) return -EFAULT; return idetape_mtioctop(drive, mtop.mt_op, mtop.mt_count); case MTIOCGET: memset(&mtget, 0, sizeof(struct mtget)); mtget.mt_type = MT_ISSCSI2; mtget.mt_blkno = position / tape->user_bs_factor - block_offset; mtget.mt_dsreg = ((tape->blk_size * tape->user_bs_factor) << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK; if (tape->drv_write_prot) mtget.mt_gstat |= GMT_WR_PROT(0xffffffff); if (copy_to_user(argp, &mtget, sizeof(struct mtget))) return -EFAULT; return 0; case MTIOCPOS: mtpos.mt_blkno = position / tape->user_bs_factor - block_offset; if (copy_to_user(argp, &mtpos, sizeof(struct mtpos))) return -EFAULT; return 0; default: if (tape->chrdev_dir == IDETAPE_DIR_READ) ide_tape_discard_merge_buffer(drive, 1); return idetape_blkdev_ioctl(drive, cmd, arg); } } static long idetape_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { long ret; lock_kernel(); ret = do_idetape_chrdev_ioctl(file, cmd, arg); unlock_kernel(); return ret; } /* * Do a mode sense page 0 with block descriptor and if it succeeds set the tape * block size with the reported value. */ static void ide_tape_get_bsize_from_bdesc(ide_drive_t *drive) { idetape_tape_t *tape = drive->driver_data; struct ide_atapi_pc pc; u8 buf[12]; idetape_create_mode_sense_cmd(&pc, IDETAPE_BLOCK_DESCRIPTOR); if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) { printk(KERN_ERR "ide-tape: Can't get block descriptor\n"); if (tape->blk_size == 0) { printk(KERN_WARNING "ide-tape: Cannot deal with zero " "block size, assuming 32k\n"); tape->blk_size = 32768; } return; } tape->blk_size = (buf[4 + 5] << 16) + (buf[4 + 6] << 8) + buf[4 + 7]; tape->drv_write_prot = (buf[2] & 0x80) >> 7; ide_debug_log(IDE_DBG_FUNC, "blk_size: %d, write_prot: %d", tape->blk_size, tape->drv_write_prot); } static int idetape_chrdev_open(struct inode *inode, struct file *filp) { unsigned int minor = iminor(inode), i = minor & ~0xc0; ide_drive_t *drive; idetape_tape_t *tape; int retval; if (i >= MAX_HWIFS * MAX_DRIVES) return -ENXIO; mutex_lock(&idetape_chrdev_mutex); tape = ide_tape_get(NULL, true, i); if (!tape) { mutex_unlock(&idetape_chrdev_mutex); return -ENXIO; } drive = tape->drive; filp->private_data = tape; ide_debug_log(IDE_DBG_FUNC, "enter"); /* * We really want to do nonseekable_open(inode, filp); here, but some * versions of tar incorrectly call lseek on tapes and bail out if that * fails. So we disallow pread() and pwrite(), but permit lseeks. */ filp->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE); if (test_and_set_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags)) { retval = -EBUSY; goto out_put_tape; } retval = idetape_wait_ready(drive, 60 * HZ); if (retval) { clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags); printk(KERN_ERR "ide-tape: %s: drive not ready\n", tape->name); goto out_put_tape; } ide_tape_read_position(drive); if (!test_bit(ilog2(IDE_AFLAG_ADDRESS_VALID), &drive->atapi_flags)) (void)idetape_rewind_tape(drive); /* Read block size and write protect status from drive. */ ide_tape_get_bsize_from_bdesc(drive); /* Set write protect flag if device is opened as read-only. */ if ((filp->f_flags & O_ACCMODE) == O_RDONLY) tape->write_prot = 1; else tape->write_prot = tape->drv_write_prot; /* Make sure drive isn't write protected if user wants to write. */ if (tape->write_prot) { if ((filp->f_flags & O_ACCMODE) == O_WRONLY || (filp->f_flags & O_ACCMODE) == O_RDWR) { clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags); retval = -EROFS; goto out_put_tape; } } /* Lock the tape drive door so user can't eject. */ if (tape->chrdev_dir == IDETAPE_DIR_NONE) { if (!ide_set_media_lock(drive, tape->disk, 1)) { if (tape->door_locked != DOOR_EXPLICITLY_LOCKED) tape->door_locked = DOOR_LOCKED; } } mutex_unlock(&idetape_chrdev_mutex); return 0; out_put_tape: ide_tape_put(tape); mutex_unlock(&idetape_chrdev_mutex); return retval; } static void idetape_write_release(ide_drive_t *drive, unsigned int minor) { idetape_tape_t *tape = drive->driver_data; ide_tape_flush_merge_buffer(drive); tape->buf = kmalloc(tape->buffer_size, GFP_KERNEL); if (tape->buf != NULL) { idetape_pad_zeros(drive, tape->blk_size * (tape->user_bs_factor - 1)); kfree(tape->buf); tape->buf = NULL; } idetape_write_filemark(drive); idetape_flush_tape_buffers(drive); idetape_flush_tape_buffers(drive); } static int idetape_chrdev_release(struct inode *inode, struct file *filp) { struct ide_tape_obj *tape = filp->private_data; ide_drive_t *drive = tape->drive; unsigned int minor = iminor(inode); mutex_lock(&idetape_chrdev_mutex); tape = drive->driver_data; ide_debug_log(IDE_DBG_FUNC, "enter"); if (tape->chrdev_dir == IDETAPE_DIR_WRITE) idetape_write_release(drive, minor); if (tape->chrdev_dir == IDETAPE_DIR_READ) { if (minor < 128) ide_tape_discard_merge_buffer(drive, 1); } if (minor < 128 && test_bit(ilog2(IDE_AFLAG_MEDIUM_PRESENT), &drive->atapi_flags)) (void) idetape_rewind_tape(drive); if (tape->chrdev_dir == IDETAPE_DIR_NONE) { if (tape->door_locked == DOOR_LOCKED) { if (!ide_set_media_lock(drive, tape->disk, 0)) tape->door_locked = DOOR_UNLOCKED; } } clear_bit(ilog2(IDE_AFLAG_BUSY), &drive->atapi_flags); ide_tape_put(tape); mutex_unlock(&idetape_chrdev_mutex); return 0; } static void idetape_get_inquiry_results(ide_drive_t *drive) { idetape_tape_t *tape = drive->driver_data; struct ide_atapi_pc pc; u8 pc_buf[256]; char fw_rev[4], vendor_id[8], product_id[16]; idetape_create_inquiry_cmd(&pc); if (ide_queue_pc_tail(drive, tape->disk, &pc, pc_buf, pc.req_xfer)) { printk(KERN_ERR "ide-tape: %s: can't get INQUIRY results\n", tape->name); return; } memcpy(vendor_id, &pc_buf[8], 8); memcpy(product_id, &pc_buf[16], 16); memcpy(fw_rev, &pc_buf[32], 4); ide_fixstring(vendor_id, 8, 0); ide_fixstring(product_id, 16, 0); ide_fixstring(fw_rev, 4, 0); printk(KERN_INFO "ide-tape: %s <-> %s: %.8s %.16s rev %.4s\n", drive->name, tape->name, vendor_id, product_id, fw_rev); } /* * Ask the tape about its various parameters. In particular, we will adjust our * data transfer buffer size to the recommended value as returned by the tape. */ static void idetape_get_mode_sense_results(ide_drive_t *drive) { idetape_tape_t *tape = drive->driver_data; struct ide_atapi_pc pc; u8 buf[24], *caps; u8 speed, max_speed; idetape_create_mode_sense_cmd(&pc, IDETAPE_CAPABILITIES_PAGE); if (ide_queue_pc_tail(drive, tape->disk, &pc, buf, pc.req_xfer)) { printk(KERN_ERR "ide-tape: Can't get tape parameters - assuming" " some default values\n"); tape->blk_size = 512; put_unaligned(52, (u16 *)&tape->caps[12]); put_unaligned(540, (u16 *)&tape->caps[14]); put_unaligned(6*52, (u16 *)&tape->caps[16]); return; } caps = buf + 4 + buf[3]; /* convert to host order and save for later use */ speed = be16_to_cpup((__be16 *)&caps[14]); max_speed = be16_to_cpup((__be16 *)&caps[8]); *(u16 *)&caps[8] = max_speed; *(u16 *)&caps[12] = be16_to_cpup((__be16 *)&caps[12]); *(u16 *)&caps[14] = speed; *(u16 *)&caps[16] = be16_to_cpup((__be16 *)&caps[16]); if (!speed) { printk(KERN_INFO "ide-tape: %s: invalid tape speed " "(assuming 650KB/sec)\n", drive->name); *(u16 *)&caps[14] = 650; } if (!max_speed) { printk(KERN_INFO "ide-tape: %s: invalid max_speed " "(assuming 650KB/sec)\n", drive->name); *(u16 *)&caps[8] = 650; } memcpy(&tape->caps, caps, 20); /* device lacks locking support according to capabilities page */ if ((caps[6] & 1) == 0) drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING; if (caps[7] & 0x02) tape->blk_size = 512; else if (caps[7] & 0x04) tape->blk_size = 1024; } #ifdef CONFIG_IDE_PROC_FS #define ide_tape_devset_get(name, field) \ static int get_##name(ide_drive_t *drive) \ { \ idetape_tape_t *tape = drive->driver_data; \ return tape->field; \ } #define ide_tape_devset_set(name, field) \ static int set_##name(ide_drive_t *drive, int arg) \ { \ idetape_tape_t *tape = drive->driver_data; \ tape->field = arg; \ return 0; \ } #define ide_tape_devset_rw_field(_name, _field) \ ide_tape_devset_get(_name, _field) \ ide_tape_devset_set(_name, _field) \ IDE_DEVSET(_name, DS_SYNC, get_##_name, set_##_name) #define ide_tape_devset_r_field(_name, _field) \ ide_tape_devset_get(_name, _field) \ IDE_DEVSET(_name, 0, get_##_name, NULL) static int mulf_tdsc(ide_drive_t *drive) { return 1000; } static int divf_tdsc(ide_drive_t *drive) { return HZ; } static int divf_buffer(ide_drive_t *drive) { return 2; } static int divf_buffer_size(ide_drive_t *drive) { return 1024; } ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP); ide_tape_devset_rw_field(tdsc, best_dsc_rw_freq); ide_tape_devset_r_field(avg_speed, avg_speed); ide_tape_devset_r_field(speed, caps[14]); ide_tape_devset_r_field(buffer, caps[16]); ide_tape_devset_r_field(buffer_size, buffer_size); static const struct ide_proc_devset idetape_settings[] = { __IDE_PROC_DEVSET(avg_speed, 0, 0xffff, NULL, NULL), __IDE_PROC_DEVSET(buffer, 0, 0xffff, NULL, divf_buffer), __IDE_PROC_DEVSET(buffer_size, 0, 0xffff, NULL, divf_buffer_size), __IDE_PROC_DEVSET(dsc_overlap, 0, 1, NULL, NULL), __IDE_PROC_DEVSET(speed, 0, 0xffff, NULL, NULL), __IDE_PROC_DEVSET(tdsc, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX, mulf_tdsc, divf_tdsc), { NULL }, }; #endif /* * The function below is called to: * * 1. Initialize our various state variables. * 2. Ask the tape for its capabilities. * 3. Allocate a buffer which will be used for data transfer. The buffer size * is chosen based on the recommendation which we received in step 2. * * Note that at this point ide.c already assigned us an irq, so that we can * queue requests here and wait for their completion. */ static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor) { unsigned long t; int speed; int buffer_size; u16 *ctl = (u16 *)&tape->caps[12]; ide_debug_log(IDE_DBG_FUNC, "minor: %d", minor); drive->pc_callback = ide_tape_callback; drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP; if (drive->hwif->host_flags & IDE_HFLAG_NO_DSC) { printk(KERN_INFO "ide-tape: %s: disabling DSC overlap\n", tape->name); drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP; } /* Seagate Travan drives do not support DSC overlap. */ if (strstr((char *)&drive->id[ATA_ID_PROD], "Seagate STT3401")) drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP; tape->minor = minor; tape->name[0] = 'h'; tape->name[1] = 't'; tape->name[2] = '0' + minor; tape->chrdev_dir = IDETAPE_DIR_NONE; idetape_get_inquiry_results(drive); idetape_get_mode_sense_results(drive); ide_tape_get_bsize_from_bdesc(drive); tape->user_bs_factor = 1; tape->buffer_size = *ctl * tape->blk_size; while (tape->buffer_size > 0xffff) { printk(KERN_NOTICE "ide-tape: decreasing stage size\n"); *ctl /= 2; tape->buffer_size = *ctl * tape->blk_size; } buffer_size = tape->buffer_size; /* select the "best" DSC read/write polling freq */ speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]); t = (IDETAPE_FIFO_THRESHOLD * tape->buffer_size * HZ) / (speed * 1000); /* * Ensure that the number we got makes sense; limit it within * IDETAPE_DSC_RW_MIN and IDETAPE_DSC_RW_MAX. */ tape->best_dsc_rw_freq = clamp_t(unsigned long, t, IDETAPE_DSC_RW_MIN, IDETAPE_DSC_RW_MAX); printk(KERN_INFO "ide-tape: %s <-> %s: %dKBps, %d*%dkB buffer, " "%lums tDSC%s\n", drive->name, tape->name, *(u16 *)&tape->caps[14], (*(u16 *)&tape->caps[16] * 512) / tape->buffer_size, tape->buffer_size / 1024, tape->best_dsc_rw_freq * 1000 / HZ, (drive->dev_flags & IDE_DFLAG_USING_DMA) ? ", DMA" : ""); ide_proc_register_driver(drive, tape->driver); } static void ide_tape_remove(ide_drive_t *drive) { idetape_tape_t *tape = drive->driver_data; ide_proc_unregister_driver(drive, tape->driver); device_del(&tape->dev); ide_unregister_region(tape->disk); mutex_lock(&idetape_ref_mutex); put_device(&tape->dev); mutex_unlock(&idetape_ref_mutex); } static void ide_tape_release(struct device *dev) { struct ide_tape_obj *tape = to_ide_drv(dev, ide_tape_obj); ide_drive_t *drive = tape->drive; struct gendisk *g = tape->disk; BUG_ON(tape->valid); drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP; drive->driver_data = NULL; device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor)); device_destroy(idetape_sysfs_class, MKDEV(IDETAPE_MAJOR, tape->minor + 128)); idetape_devs[tape->minor] = NULL; g->private_data = NULL; put_disk(g); kfree(tape); } #ifdef CONFIG_IDE_PROC_FS static int idetape_name_proc_show(struct seq_file *m, void *v) { ide_drive_t *drive = (ide_drive_t *) m->private; idetape_tape_t *tape = drive->driver_data; seq_printf(m, "%s\n", tape->name); return 0; } static int idetape_name_proc_open(struct inode *inode, struct file *file) { return single_open(file, idetape_name_proc_show, PDE(inode)->data); } static const struct file_operations idetape_name_proc_fops = { .owner = THIS_MODULE, .open = idetape_name_proc_open, .read = seq_read, .llseek = seq_lseek, .release = single_release, }; static ide_proc_entry_t idetape_proc[] = { { "capacity", S_IFREG|S_IRUGO, &ide_capacity_proc_fops }, { "name", S_IFREG|S_IRUGO, &idetape_name_proc_fops }, {} }; static ide_proc_entry_t *ide_tape_proc_entries(ide_drive_t *drive) { return idetape_proc; } static const struct ide_proc_devset *ide_tape_proc_devsets(ide_drive_t *drive) { return idetape_settings; } #endif static int ide_tape_probe(ide_drive_t *); static struct ide_driver idetape_driver = { .gen_driver = { .owner = THIS_MODULE, .name = "ide-tape", .bus = &ide_bus_type, }, .probe = ide_tape_probe, .remove = ide_tape_remove, .version = IDETAPE_VERSION, .do_request = idetape_do_request, #ifdef CONFIG_IDE_PROC_FS .proc_entries = ide_tape_proc_entries, .proc_devsets = ide_tape_proc_devsets, #endif }; /* Our character device supporting functions, passed to register_chrdev. */ static const struct file_operations idetape_fops = { .owner = THIS_MODULE, .read = idetape_chrdev_read, .write = idetape_chrdev_write, .unlocked_ioctl = idetape_chrdev_ioctl, .open = idetape_chrdev_open, .release = idetape_chrdev_release, }; static int idetape_open(struct block_device *bdev, fmode_t mode) { struct ide_tape_obj *tape = ide_tape_get(bdev->bd_disk, false, 0); if (!tape) return -ENXIO; return 0; } static int idetape_release(struct gendisk *disk, fmode_t mode) { struct ide_tape_obj *tape = ide_drv_g(disk, ide_tape_obj); ide_tape_put(tape); return 0; } static int idetape_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd, unsigned long arg) { struct ide_tape_obj *tape = ide_drv_g(bdev->bd_disk, ide_tape_obj); ide_drive_t *drive = tape->drive; int err = generic_ide_ioctl(drive, bdev, cmd, arg); if (err == -EINVAL) err = idetape_blkdev_ioctl(drive, cmd, arg); return err; } static const struct block_device_operations idetape_block_ops = { .owner = THIS_MODULE, .open = idetape_open, .release = idetape_release, .locked_ioctl = idetape_ioctl, }; static int ide_tape_probe(ide_drive_t *drive) { idetape_tape_t *tape; struct gendisk *g; int minor; ide_debug_log(IDE_DBG_FUNC, "enter"); if (!strstr(DRV_NAME, drive->driver_req)) goto failed; if (drive->media != ide_tape) goto failed; if ((drive->dev_flags & IDE_DFLAG_ID_READ) && ide_check_atapi_device(drive, DRV_NAME) == 0) { printk(KERN_ERR "ide-tape: %s: not supported by this version of" " the driver\n", drive->name); goto failed; } tape = kzalloc(sizeof(idetape_tape_t), GFP_KERNEL); if (tape == NULL) { printk(KERN_ERR "ide-tape: %s: Can't allocate a tape struct\n", drive->name); goto failed; } g = alloc_disk(1 << PARTN_BITS); if (!g) goto out_free_tape; ide_init_disk(g, drive); tape->dev.parent = &drive->gendev; tape->dev.release = ide_tape_release; dev_set_name(&tape->dev, dev_name(&drive->gendev)); if (device_register(&tape->dev)) goto out_free_disk; tape->drive = drive; tape->driver = &idetape_driver; tape->disk = g; g->private_data = &tape->driver; drive->driver_data = tape; mutex_lock(&idetape_ref_mutex); for (minor = 0; idetape_devs[minor]; minor++) ; idetape_devs[minor] = tape; mutex_unlock(&idetape_ref_mutex); idetape_setup(drive, tape, minor); device_create(idetape_sysfs_class, &drive->gendev, MKDEV(IDETAPE_MAJOR, minor), NULL, "%s", tape->name); device_create(idetape_sysfs_class, &drive->gendev, MKDEV(IDETAPE_MAJOR, minor + 128), NULL, "n%s", tape->name); g->fops = &idetape_block_ops; ide_register_region(g); return 0; out_free_disk: put_disk(g); out_free_tape: kfree(tape); failed: return -ENODEV; } static void __exit idetape_exit(void) { driver_unregister(&idetape_driver.gen_driver); class_destroy(idetape_sysfs_class); unregister_chrdev(IDETAPE_MAJOR, "ht"); } static int __init idetape_init(void) { int error = 1; idetape_sysfs_class = class_create(THIS_MODULE, "ide_tape"); if (IS_ERR(idetape_sysfs_class)) { idetape_sysfs_class = NULL; printk(KERN_ERR "Unable to create sysfs class for ide tapes\n"); error = -EBUSY; goto out; } if (register_chrdev(IDETAPE_MAJOR, "ht", &idetape_fops)) { printk(KERN_ERR "ide-tape: Failed to register chrdev" " interface\n"); error = -EBUSY; goto out_free_class; } error = driver_register(&idetape_driver.gen_driver); if (error) goto out_free_driver; return 0; out_free_driver: driver_unregister(&idetape_driver.gen_driver); out_free_class: class_destroy(idetape_sysfs_class); out: return error; } MODULE_ALIAS("ide:*m-tape*"); module_init(idetape_init); module_exit(idetape_exit); MODULE_ALIAS_CHARDEV_MAJOR(IDETAPE_MAJOR); MODULE_DESCRIPTION("ATAPI Streaming TAPE Driver"); MODULE_LICENSE("GPL");