export.c 19.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#define MSNFS	/* HACK HACK */
/*
 * linux/fs/nfsd/export.c
 *
 * NFS exporting and validation.
 *
 * We maintain a list of clients, each of which has a list of
 * exports. To export an fs to a given client, you first have
 * to create the client entry with NFSCTL_ADDCLIENT, which
 * creates a client control block and adds it to the hash
 * table. Then, you call NFSCTL_EXPORT for each fs.
 *
 *
 * Copyright (C) 1995, 1996 Olaf Kirch, <okir@monad.swb.de>
 */

#include <linux/unistd.h>
Linus Torvalds's avatar
Linus Torvalds committed
18
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
19
#include <linux/sched.h>
Linus Torvalds's avatar
Linus Torvalds committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <linux/stat.h>
#include <linux/in.h>

#include <linux/sunrpc/svc.h>
#include <linux/nfsd/nfsd.h>
#include <linux/nfsd/nfsfh.h>
#include <linux/nfsd/syscall.h>
#include <linux/lockd/bind.h>

#define NFSDDBG_FACILITY	NFSDDBG_EXPORT
#define NFSD_PARANOIA 1

typedef struct svc_client	svc_client;
typedef struct svc_export	svc_export;

static svc_export *	exp_parent(svc_client *clp, kdev_t dev,
					struct dentry *dentry);
static svc_export *	exp_child(svc_client *clp, kdev_t dev,
					struct dentry *dentry);
static void		exp_unexport_all(svc_client *clp);
static void		exp_do_unexport(svc_export *unexp);
static svc_client *	exp_getclientbyname(char *name);
static void		exp_freeclient(svc_client *clp);
static void		exp_unhashclient(svc_client *clp);
static int		exp_verify_string(char *cp, int max);

#define CLIENT_HASHBITS		6
#define CLIENT_HASHMAX		(1 << CLIENT_HASHBITS)
#define CLIENT_HASHMASK		(CLIENT_HASHMAX - 1)
#define CLIENT_HASH(a) \
		((((a)>>24) ^ ((a)>>16) ^ ((a)>>8) ^(a)) & CLIENT_HASHMASK)
/* XXX: is this adequate for 32bit kdev_t ? */
Linus Torvalds's avatar
Linus Torvalds committed
52
#define EXPORT_HASH(dev)	(minor(dev) & (NFSCLNT_EXPMAX - 1))
Linus Torvalds's avatar
Linus Torvalds committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

struct svc_clnthash {
	struct svc_clnthash *	h_next;
	struct in_addr		h_addr;
	struct svc_client *	h_client;
};
static struct svc_clnthash *	clnt_hash[CLIENT_HASHMAX];
static svc_client *		clients;
static int			initialized;

static int			hash_lock;
static int			want_lock;
static int			hash_count;
static DECLARE_WAIT_QUEUE_HEAD(	hash_wait );

/*
 * Find the client's export entry matching xdev/xino.
 */
svc_export *
exp_get(svc_client *clp, kdev_t dev, ino_t ino)
{
	svc_export *	exp;

	if (!clp)
		return NULL;

	exp = clp->cl_export[EXPORT_HASH(dev)];
	if (exp)
		do {
Linus Torvalds's avatar
Linus Torvalds committed
82
			if (exp->ex_ino == ino && kdev_same(exp->ex_dev, dev))
Linus Torvalds's avatar
Linus Torvalds committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
				goto out;
		} while (NULL != (exp = exp->ex_next));
	exp = NULL;
out:
	return exp;
}

/*
 * Find the export entry for a given dentry.  <gam3@acm.org>
 */
static svc_export *
exp_parent(svc_client *clp, kdev_t dev, struct dentry *dentry)
{
	svc_export      *exp;

	if (clp == NULL)
		return NULL;

	for (exp = clp->cl_export[EXPORT_HASH(dev)]; exp; exp = exp->ex_next)
		if (is_subdir(dentry, exp->ex_dentry))
			break;
	return exp;
}

/*
 * Find the child export entry for a given fs. This function is used
 * only by the export syscall to keep the export tree consistent.
 * <gam3@acm.org>
 */
static svc_export *
exp_child(svc_client *clp, kdev_t dev, struct dentry *dentry)
{
	svc_export      *exp;

	if (clp == NULL)
		return NULL;

	for (exp = clp->cl_export[EXPORT_HASH(dev)]; exp; exp = exp->ex_next) {
		struct dentry	*ndentry = exp->ex_dentry;
		if (ndentry && is_subdir(ndentry->d_parent, dentry))
			break;
	}
	return exp;
}

/*
 * Export a file system.
 */
int
exp_export(struct nfsctl_export *nxp)
{
	svc_client	*clp;
	svc_export	*exp, *parent;
	svc_export	**head;
	struct nameidata nd;
	struct inode	*inode = NULL;
	int		i, err;
	kdev_t		dev;
	ino_t		ino;

	/* Consistency check */
	err = -EINVAL;
	if (!exp_verify_string(nxp->ex_path, NFS_MAXPATHLEN) ||
	    !exp_verify_string(nxp->ex_client, NFSCLNT_IDMAX))
		goto out;

	dprintk("exp_export called for %s:%s (%x/%ld fl %x).\n",
			nxp->ex_client, nxp->ex_path,
			nxp->ex_dev, (long) nxp->ex_ino, nxp->ex_flags);
	dev = to_kdev_t(nxp->ex_dev);
	ino = nxp->ex_ino;

	/* Try to lock the export table for update */
	if ((err = exp_writelock()) < 0)
		goto out;

	/* Look up client info */
	err = -EINVAL;
	if (!(clp = exp_getclientbyname(nxp->ex_client)))
		goto out_unlock;

	/*
	 * If there's already an export for this file, assume this
	 * is just a flag update.
	 */
	if ((exp = exp_get(clp, dev, ino)) != NULL) {
		exp->ex_flags    = nxp->ex_flags;
		exp->ex_anon_uid = nxp->ex_anon_uid;
		exp->ex_anon_gid = nxp->ex_anon_gid;
		err = 0;
		goto out_unlock;
	}

	/* Look up the dentry */
	err = 0;
	if (path_init(nxp->ex_path, LOOKUP_POSITIVE, &nd))
		err = path_walk(nxp->ex_path, &nd);
	if (err)
		goto out_unlock;

	inode = nd.dentry->d_inode;
	err = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
185 186 187 188
	if (!kdev_same(inode->i_dev, dev) || inode->i_ino != nxp->ex_ino) {
		printk(KERN_DEBUG "exp_export: i_dev = %02x:%02x, dev = %02x:%02x\n",
			major(inode->i_dev), minor(inode->i_dev),
			major(dev), minor(dev)); 
Linus Torvalds's avatar
Linus Torvalds committed
189 190 191 192 193 194 195 196 197 198 199 200 201
		/* I'm just being paranoid... */
		goto finish;
	}

	/* We currently export only dirs and regular files.
	 * This is what umountd does.
	 */
	err = -ENOTDIR;
	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode))
		goto finish;

	err = -EINVAL;
	if (!(inode->i_sb->s_type->fs_flags & FS_REQUIRES_DEV) ||
Linus Torvalds's avatar
Linus Torvalds committed
202 203
	    (inode->i_sb->s_op->read_inode == NULL
	     && inode->i_sb->s_op->fh_to_dentry == NULL)) {
Linus Torvalds's avatar
Linus Torvalds committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
		dprintk("exp_export: export of invalid fs type.\n");
		goto finish;
	}

	if ((parent = exp_child(clp, dev, nd.dentry)) != NULL) {
		dprintk("exp_export: export not valid (Rule 3).\n");
		goto finish;
	}
	/* Is this is a sub-export, must be a proper subset of FS */
	if ((parent = exp_parent(clp, dev, nd.dentry)) != NULL) {
		dprintk("exp_export: sub-export not valid (Rule 2).\n");
		goto finish;
	}

	err = -ENOMEM;
	if (!(exp = kmalloc(sizeof(*exp), GFP_USER)))
		goto finish;
	dprintk("nfsd: created export entry %p for client %p\n", exp, clp);

	strcpy(exp->ex_path, nxp->ex_path);
	exp->ex_client = clp;
	exp->ex_parent = parent;
	exp->ex_dentry = nd.dentry;
	exp->ex_mnt = nd.mnt;
	exp->ex_flags = nxp->ex_flags;
	exp->ex_dev = dev;
	exp->ex_ino = ino;
	exp->ex_anon_uid = nxp->ex_anon_uid;
	exp->ex_anon_gid = nxp->ex_anon_gid;

	/* Update parent pointers of all exports */
	if (parent) {
		for (i = 0; i < NFSCLNT_EXPMAX; i++) {
			svc_export *temp = clp->cl_export[i];

			while (temp) {
				if (temp->ex_parent == parent)
					temp->ex_parent = exp;
				temp = temp->ex_next;
			}
		}
	}

	head = clp->cl_export + EXPORT_HASH(dev);
	exp->ex_next = *head;
	*head = exp;

	err = 0;

	/* Unlock hashtable */
out_unlock:
	exp_unlock();
out:
	return err;

	/* Release the dentry */
finish:
	path_release(&nd);
	goto out_unlock;
}

/*
 * Unexport a file system. The export entry has already
 * been removed from the client's list of exported fs's.
 */
static void
exp_do_unexport(svc_export *unexp)
{
	svc_export	*exp;
	svc_client	*clp;
	struct dentry	*dentry;
	struct vfsmount *mnt;
	struct inode	*inode;
	int		i;

	/* Update parent pointers. */
	clp = unexp->ex_client;
	for (i = 0; i < NFSCLNT_EXPMAX; i++) {
		for (exp = clp->cl_export[i]; exp; exp = exp->ex_next)
			if (exp->ex_parent == unexp)
				exp->ex_parent = unexp->ex_parent;
	}

	dentry = unexp->ex_dentry;
	mnt = unexp->ex_mnt;
	inode = dentry->d_inode;
Linus Torvalds's avatar
Linus Torvalds committed
290
	if (!kdev_same(unexp->ex_dev, inode->i_dev) || unexp->ex_ino != inode->i_ino)
Linus Torvalds's avatar
Linus Torvalds committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
		printk(KERN_WARNING "nfsd: bad dentry in unexport!\n");
	dput(dentry);
	mntput(mnt);

	kfree(unexp);
}

/*
 * Revoke all exports for a given client.
 * This may look very awkward, but we have to do it this way in order
 * to avoid race conditions (aka mind the parent pointer).
 */
static void
exp_unexport_all(svc_client *clp)
{
	svc_export	*exp;
	int		i;

	dprintk("unexporting all fs's for clnt %p\n", clp);
	for (i = 0; i < NFSCLNT_EXPMAX; i++) {
		exp = clp->cl_export[i];
		clp->cl_export[i] = NULL;
		while (exp) {
			svc_export *next = exp->ex_next;
			exp_do_unexport(exp);
			exp = next;
		}
	}
}

/*
 * unexport syscall.
 */
int
exp_unexport(struct nfsctl_export *nxp)
{
	svc_client	*clp;
	svc_export	**expp, *exp = NULL;
	int		err;

	/* Consistency check */
	if (!exp_verify_string(nxp->ex_client, NFSCLNT_IDMAX))
		return -EINVAL;

	if ((err = exp_writelock()) < 0)
		goto out;

	err = -EINVAL;
	clp = exp_getclientbyname(nxp->ex_client);
	if (clp) {
Linus Torvalds's avatar
Linus Torvalds committed
341 342
		kdev_t ex_dev = to_kdev_t(nxp->ex_dev);
		expp = clp->cl_export + EXPORT_HASH(ex_dev);
Linus Torvalds's avatar
Linus Torvalds committed
343
		while ((exp = *expp) != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
344
			if (kdev_same(exp->ex_dev, ex_dev)) {
Linus Torvalds's avatar
Linus Torvalds committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
				if (exp->ex_ino == nxp->ex_ino) {
					*expp = exp->ex_next;
					exp_do_unexport(exp);
					err = 0;
					break;
				}
			}
			expp = &(exp->ex_next);
		}
	}

	exp_unlock();
out:
	return err;
}

/*
 * Obtain the root fh on behalf of a client.
 * This could be done in user space, but I feel that it adds some safety
 * since its harder to fool a kernel module than a user space program.
 */
int
Alexander Viro's avatar
Alexander Viro committed
367
exp_rootfh(struct svc_client *clp, char *path, struct knfsd_fh *f, int maxsize)
Linus Torvalds's avatar
Linus Torvalds committed
368 369 370 371 372
{
	struct svc_export	*exp;
	struct nameidata	nd;
	struct inode		*inode;
	struct svc_fh		fh;
Alexander Viro's avatar
Alexander Viro committed
373
	kdev_t			dev;
Linus Torvalds's avatar
Linus Torvalds committed
374 375 376
	int			err;

	err = -EPERM;
Alexander Viro's avatar
Alexander Viro committed
377 378 379 380 381
	/* NB: we probably ought to check that it's NUL-terminated */
	if (path_init(path, LOOKUP_POSITIVE, &nd) &&
	    path_walk(path, &nd)) {
		printk("nfsd: exp_rootfh path not found %s", path);
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
382
	}
Alexander Viro's avatar
Alexander Viro committed
383 384 385 386 387 388 389
	inode = nd.dentry->d_inode;
	dev = inode->i_dev;

	dprintk("nfsd: exp_rootfh(%s [%p] %s:%02x:%02x/%ld)\n",
		 path, nd.dentry, clp->cl_ident,
		 major(dev), minor(dev), (long) inode->i_ino);
	exp = exp_parent(clp, dev, nd.dentry);
Linus Torvalds's avatar
Linus Torvalds committed
390 391 392 393 394 395 396 397 398
	if (!exp) {
		dprintk("nfsd: exp_rootfh export not found.\n");
		goto out;
	}

	/*
	 * fh must be initialized before calling fh_compose
	 */
	fh_init(&fh, maxsize);
Linus Torvalds's avatar
Linus Torvalds committed
399
	if (fh_compose(&fh, exp, dget(nd.dentry), NULL))
Linus Torvalds's avatar
Linus Torvalds committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
		err = -EINVAL;
	else
		err = 0;
	memcpy(f, &fh.fh_handle, sizeof(struct knfsd_fh));
	fh_put(&fh);

out:
	if (path)
		path_release(&nd);
	return err;
}

/*
 * Hashtable locking. Write locks are placed only by user processes
 * wanting to modify export information.
 */
void
exp_readlock(void)
{
	while (hash_lock || want_lock)
		sleep_on(&hash_wait);
	hash_count++;
}

int
exp_writelock(void)
{
	/* fast track */
	if (!hash_count && !hash_lock) {
	lock_it:
		hash_lock = 1;
		return 0;
	}

434
	clear_thread_flag(TIF_SIGPENDING);
Linus Torvalds's avatar
Linus Torvalds committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
	want_lock++;
	while (hash_count || hash_lock) {
		interruptible_sleep_on(&hash_wait);
		if (signal_pending(current))
			break;
	}
	want_lock--;

	/* restore the task's signals */
	spin_lock_irq(&current->sigmask_lock);
	recalc_sigpending(current);
	spin_unlock_irq(&current->sigmask_lock);

	if (!hash_count && !hash_lock)
		goto lock_it;
	return -EINTR;
}

void
exp_unlock(void)
{
	if (!hash_count && !hash_lock)
		printk(KERN_WARNING "exp_unlock: not locked!\n");
	if (hash_count)
		hash_count--;
	else
		hash_lock = 0;
	wake_up(&hash_wait);
}

/*
 * Find a valid client given an inet address. We always move the most
 * recently used client to the front of the hash chain to speed up
 * future lookups.
 * Locking against other processes is the responsibility of the caller.
 */
struct svc_client *
exp_getclient(struct sockaddr_in *sin)
{
	struct svc_clnthash	**hp, **head, *tmp;
	unsigned long		addr = sin->sin_addr.s_addr;

	if (!initialized)
		return NULL;

	head = &clnt_hash[CLIENT_HASH(addr)];

	for (hp = head; (tmp = *hp) != NULL; hp = &(tmp->h_next)) {
		if (tmp->h_addr.s_addr == addr) {
			/* Move client to the front */
			if (head != hp) {
				*hp = tmp->h_next;
				tmp->h_next = *head;
				*head = tmp;
			}

			return tmp->h_client;
		}
	}

	return NULL;
}

/*
 * Find a client given its identifier.
 */
static svc_client *
exp_getclientbyname(char *ident)
{
	svc_client *	clp;

	for (clp = clients; clp; clp = clp->cl_next) {
		if (!strcmp(clp->cl_ident, ident))
			return clp;
	}
	return NULL;
}

struct flags {
	int flag;
	char *name[2];
} expflags[] = {
	{ NFSEXP_READONLY, {"ro", "rw"}},
	{ NFSEXP_INSECURE_PORT, {"insecure", ""}},
	{ NFSEXP_ROOTSQUASH, {"root_squash", "no_root_squash"}},
	{ NFSEXP_ALLSQUASH, {"all_squash", ""}},
Linus Torvalds's avatar
Linus Torvalds committed
521 522
	{ NFSEXP_ASYNC, {"async", "sync"}},
	{ NFSEXP_GATHERED_WRITES, {"wdelay", "no_wdelay"}},
Linus Torvalds's avatar
Linus Torvalds committed
523 524 525 526 527 528
	{ NFSEXP_UIDMAP, {"uidmap", ""}},
	{ NFSEXP_KERBEROS, { "kerberos", ""}},
	{ NFSEXP_SUNSECURE, { "sunsecure", ""}},
	{ NFSEXP_CROSSMNT, {"nohide", ""}},
	{ NFSEXP_NOSUBTREECHECK, {"no_subtree_check", ""}},
	{ NFSEXP_NOAUTHNLM, {"insecure_locks", ""}},
Linus Torvalds's avatar
Linus Torvalds committed
529
#ifdef MSNFS
Linus Torvalds's avatar
Linus Torvalds committed
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
	{ NFSEXP_MSNFS, {"msnfs", ""}},
#endif
	{ 0, {"", ""}}
};

static int
exp_flags(char *buffer, int flag)
{
    int len = 0, first = 0;
    struct flags *flg = expflags;

    for (;flg->flag;flg++) {
        int state = (flg->flag & flag)?0:1;
        if (!flg->flag)
		break;
        if (*flg->name[state]) {
		len += sprintf(buffer + len, "%s%s",
                               first++?",":"", flg->name[state]);
        }
    }
    return len;
}

Linus Torvalds's avatar
Linus Torvalds committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584


/* mangling borrowed from fs/super.c */
/* Use octal escapes, like mount does, for embedded spaces etc. */
static unsigned char need_escaping[] = { ' ', '\t', '\n', '\\' };

static int
mangle(const unsigned char *s, char *buf, int len) {
        char *sp;
        int n;

        sp = buf;
        while(*s && sp-buf < len-3) {
                for (n = 0; n < sizeof(need_escaping); n++) {
                        if (*s == need_escaping[n]) {
                                *sp++ = '\\';
                                *sp++ = '0' + ((*s & 0300) >> 6);
                                *sp++ = '0' + ((*s & 070) >> 3);
                                *sp++ = '0' + (*s & 07);
                                goto next;
                        }
                }
                *sp++ = *s;
        next:
                s++;
        }
        return sp - buf;	/* no trailing NUL */
}

#define FREEROOM	((int)PAGE_SIZE-200-len)
#define MANGLE(s)	len += mangle((s), buffer+len, FREEROOM);

Linus Torvalds's avatar
Linus Torvalds committed
585 586 587 588 589 590 591 592 593 594 595 596
int
exp_procfs_exports(char *buffer, char **start, off_t offset,
                             int length, int *eof, void *data)
{
	struct svc_clnthash	**hp, **head, *tmp;
	struct svc_client	*clp;
	svc_export *exp;
	off_t	pos = 0;
        off_t	begin = 0;
        int	len = 0;
	int	i,j;

Linus Torvalds's avatar
Linus Torvalds committed
597
        len += sprintf(buffer, "# Version 1.1\n");
Linus Torvalds's avatar
Linus Torvalds committed
598 599 600 601 602 603 604
        len += sprintf(buffer+len, "# Path Client(Flags) # IPs\n");

	for (clp = clients; clp; clp = clp->cl_next) {
		for (i = 0; i < NFSCLNT_EXPMAX; i++) {
			exp = clp->cl_export[i];
			while (exp) {
				int first = 0;
Linus Torvalds's avatar
Linus Torvalds committed
605 606 607 608
				MANGLE(exp->ex_path);
				buffer[len++]='\t';
				MANGLE(clp->cl_ident);
				buffer[len++]='(';
Linus Torvalds's avatar
Linus Torvalds committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894

				len += exp_flags(buffer+len, exp->ex_flags);
				len += sprintf(buffer+len, ") # ");
				for (j = 0; j < clp->cl_naddr; j++) {
					struct in_addr	addr = clp->cl_addr[j]; 

					head = &clnt_hash[CLIENT_HASH(addr.s_addr)];
					for (hp = head; (tmp = *hp) != NULL; hp = &(tmp->h_next)) {
						if (tmp->h_addr.s_addr == addr.s_addr) {
							if (first++) len += sprintf(buffer+len, "%s", " ");
							if (tmp->h_client != clp)
								len += sprintf(buffer+len, "(");
							len += sprintf(buffer+len, "%d.%d.%d.%d",
									htonl(addr.s_addr) >> 24 & 0xff,
									htonl(addr.s_addr) >> 16 & 0xff,
									htonl(addr.s_addr) >>  8 & 0xff,
									htonl(addr.s_addr) >>  0 & 0xff);
							if (tmp->h_client != clp)
							  len += sprintf(buffer+len, ")");
							break;
						}
					}
				}
				exp = exp->ex_next;

				buffer[len++]='\n';

				pos=begin+len;
				if(pos<offset) {
					len=0;
					begin=pos;
				}
				if (pos > offset + length)
					goto done;
			}
		}
	}

	*eof = 1;

done:
	*start = buffer + (offset - begin);
	len -= (offset - begin);
	if ( len > length )
		len = length;
	return len;
}

/*
 * Add or modify a client.
 * Change requests may involve the list of host addresses. The list of
 * exports and possibly existing uid maps are left untouched.
 */
int
exp_addclient(struct nfsctl_client *ncp)
{
	struct svc_clnthash *	ch[NFSCLNT_ADDRMAX];
	svc_client *		clp;
	int			i, err, change = 0, ilen;

	/* First, consistency check. */
	err = -EINVAL;
	if (!(ilen = exp_verify_string(ncp->cl_ident, NFSCLNT_IDMAX)))
		goto out;
	if (ncp->cl_naddr > NFSCLNT_ADDRMAX)
		goto out;

	/* Lock the hashtable */
	if ((err = exp_writelock()) < 0)
		goto out;

	/* First check if this is a change request for a client. */
	for (clp = clients; clp; clp = clp->cl_next)
		if (!strcmp(clp->cl_ident, ncp->cl_ident))
			break;

	err = -ENOMEM;
	if (clp) {
		change = 1;
	} else {
		if (!(clp = kmalloc(sizeof(*clp), GFP_KERNEL)))
			goto out_unlock;
		memset(clp, 0, sizeof(*clp));

		dprintk("created client %s (%p)\n", ncp->cl_ident, clp);

		strcpy(clp->cl_ident, ncp->cl_ident);
		clp->cl_idlen = ilen;
	}

	/* Allocate hash buckets */
	for (i = 0; i < ncp->cl_naddr; i++) {
		ch[i] = kmalloc(sizeof(struct svc_clnthash), GFP_KERNEL);
		if (!ch[i]) {
			while (i--)
				kfree(ch[i]);
			if (!change)
				kfree(clp);
			goto out_unlock;
		}
	}

	/* Copy addresses. */
	for (i = 0; i < ncp->cl_naddr; i++) {
		clp->cl_addr[i] = ncp->cl_addrlist[i];
	}
	clp->cl_naddr = ncp->cl_naddr;

	/* Remove old client hash entries. */
	if (change)
		exp_unhashclient(clp);

	/* Insert client into hashtable. */
	for (i = 0; i < ncp->cl_naddr; i++) {
		struct in_addr	addr = clp->cl_addr[i];
		int		hash;

		hash = CLIENT_HASH(addr.s_addr);
		ch[i]->h_client = clp;
		ch[i]->h_addr = addr;
		ch[i]->h_next = clnt_hash[hash];
		clnt_hash[hash] = ch[i];
	}

	if (!change) {
		clp->cl_next = clients;
		clients = clp;
	}
	err = 0;

out_unlock:
	exp_unlock();
out:
	return err;
}

/*
 * Delete a client given an identifier.
 */
int
exp_delclient(struct nfsctl_client *ncp)
{
	svc_client	**clpp, *clp;
	int		err;

	err = -EINVAL;
	if (!exp_verify_string(ncp->cl_ident, NFSCLNT_IDMAX))
		goto out;

	/* Lock the hashtable */
	if ((err = exp_writelock()) < 0)
		goto out;

	err = -EINVAL;
	for (clpp = &clients; (clp = *clpp); clpp = &(clp->cl_next))
		if (!strcmp(ncp->cl_ident, clp->cl_ident))
			break;

	if (clp) {
		*clpp = clp->cl_next;
		exp_freeclient(clp);
		err = 0;
	}

	exp_unlock();
out:
	return err;
}

/*
 * Free a client. The caller has already removed it from the client list.
 */
static void
exp_freeclient(svc_client *clp)
{
	exp_unhashclient(clp);

	/* umap_free(&(clp->cl_umap)); */
	exp_unexport_all(clp);
	nfsd_lockd_unexport(clp);
	kfree (clp);
}

/*
 * Remove client from hashtable. We first collect all hashtable
 * entries and free them in one go.
 * The hash table must be writelocked by the caller.
 */
static void
exp_unhashclient(svc_client *clp)
{
	struct svc_clnthash	**hpp, *hp, *ch[NFSCLNT_ADDRMAX];
	int			i, count, err;

again:
	err = 0;
	for (i = 0, count = 0; i < CLIENT_HASHMAX && !err; i++) {
		hpp = clnt_hash + i;
		while ((hp = *hpp) && !err) {
			if (hp->h_client == clp) {
				*hpp = hp->h_next;
				ch[count++] = hp;
				err = (count >= NFSCLNT_ADDRMAX);
			} else {
				hpp = &(hp->h_next);
			}
		}
	}
	if (count != clp->cl_naddr)
		printk(KERN_WARNING "nfsd: bad address count in freeclient!\n");
	if (err)
		goto again;
	for (i = 0; i < count; i++)
		kfree (ch[i]);
}

/*
 * Lockd is shutting down and tells us to unregister all clients
 */
void
exp_nlmdetach(void)
{
	struct svc_client	*clp;

	for (clp = clients; clp; clp = clp->cl_next)
		nfsd_lockd_unexport(clp);
}

/*
 * Verify that string is non-empty and does not exceed max length.
 */
static int
exp_verify_string(char *cp, int max)
{
	int	i;

	for (i = 0; i < max; i++)
		if (!cp[i])
			return i;
	cp[i] = 0;
	printk(KERN_NOTICE "nfsd: couldn't validate string %s\n", cp);
	return 0;
}

/*
 * Initialize the exports module.
 */
void
nfsd_export_init(void)
{
	int		i;

	dprintk("nfsd: initializing export module.\n");
	if (initialized)
		return;
	for (i = 0; i < CLIENT_HASHMAX; i++)
		clnt_hash[i] = NULL;
	clients = NULL;

	initialized = 1;
}

/*
 * Shutdown the exports module.
 */
void
nfsd_export_shutdown(void)
{
	int	i;

	dprintk("nfsd: shutting down export module.\n");
	if (!initialized)
		return;
	if (exp_writelock() < 0) {
		printk(KERN_WARNING "Weird: hashtable locked in exp_shutdown");
		return;
	}
	for (i = 0; i < CLIENT_HASHMAX; i++) {
		while (clnt_hash[i])
			exp_freeclient(clnt_hash[i]->h_client);
	}
	clients = NULL; /* we may be restarted before the module unloads */
	
	exp_unlock();
	dprintk("nfsd: export shutdown complete.\n");
}