inode.c 27.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 *  inode.c
 *
 *  Copyright (C) 1995, 1996 by Volker Lendecke
 *  Modified for big endian by J.F. Chadima and David S. Miller
 *  Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
 *  Modified 1998 Wolfram Pienkoss for NLS
 *  Modified 2000 Ben Harris, University of Cambridge for NFS NS meta-info
 *
 */

12 13
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <linux/module.h>

#include <asm/uaccess.h>
#include <asm/byteorder.h>

#include <linux/time.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/stat.h>
#include <linux/errno.h>
#include <linux/file.h>
#include <linux/fcntl.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/vfs.h>
M
Miklos Szeredi 已提交
31 32
#include <linux/mount.h>
#include <linux/seq_file.h>
33
#include <linux/namei.h>
L
Linus Torvalds 已提交
34 35 36

#include <net/sock.h>

37
#include "ncp_fs.h"
L
Linus Torvalds 已提交
38 39
#include "getopt.h"

M
Miklos Szeredi 已提交
40 41 42 43 44
#define NCP_DEFAULT_FILE_MODE 0600
#define NCP_DEFAULT_DIR_MODE 0700
#define NCP_DEFAULT_TIME_OUT 10
#define NCP_DEFAULT_RETRY_COUNT 20

A
Al Viro 已提交
45
static void ncp_evict_inode(struct inode *);
L
Linus Torvalds 已提交
46
static void ncp_put_super(struct super_block *);
47
static int  ncp_statfs(struct dentry *, struct kstatfs *);
48
static int  ncp_show_options(struct seq_file *, struct dentry *);
L
Linus Torvalds 已提交
49

50
static struct kmem_cache * ncp_inode_cachep;
L
Linus Torvalds 已提交
51 52 53 54

static struct inode *ncp_alloc_inode(struct super_block *sb)
{
	struct ncp_inode_info *ei;
55
	ei = (struct ncp_inode_info *)kmem_cache_alloc(ncp_inode_cachep, GFP_KERNEL);
L
Linus Torvalds 已提交
56 57 58 59 60
	if (!ei)
		return NULL;
	return &ei->vfs_inode;
}

N
Nick Piggin 已提交
61
static void ncp_i_callback(struct rcu_head *head)
L
Linus Torvalds 已提交
62
{
N
Nick Piggin 已提交
63
	struct inode *inode = container_of(head, struct inode, i_rcu);
L
Linus Torvalds 已提交
64 65 66
	kmem_cache_free(ncp_inode_cachep, NCP_FINFO(inode));
}

N
Nick Piggin 已提交
67 68 69 70 71
static void ncp_destroy_inode(struct inode *inode)
{
	call_rcu(&inode->i_rcu, ncp_i_callback);
}

72
static void init_once(void *foo)
L
Linus Torvalds 已提交
73 74 75
{
	struct ncp_inode_info *ei = (struct ncp_inode_info *) foo;

C
Christoph Lameter 已提交
76 77
	mutex_init(&ei->open_mutex);
	inode_init_once(&ei->vfs_inode);
L
Linus Torvalds 已提交
78
}
79

L
Linus Torvalds 已提交
80 81 82 83
static int init_inodecache(void)
{
	ncp_inode_cachep = kmem_cache_create("ncp_inode_cache",
					     sizeof(struct ncp_inode_info),
84
					     0, (SLAB_RECLAIM_ACCOUNT|
85
						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
86
					     init_once);
L
Linus Torvalds 已提交
87 88 89 90 91 92 93
	if (ncp_inode_cachep == NULL)
		return -ENOMEM;
	return 0;
}

static void destroy_inodecache(void)
{
94 95 96 97 98
	/*
	 * Make sure all delayed rcu free inodes are flushed before we
	 * destroy cache.
	 */
	rcu_barrier();
99
	kmem_cache_destroy(ncp_inode_cachep);
L
Linus Torvalds 已提交
100 101 102 103
}

static int ncp_remount(struct super_block *sb, int *flags, char* data)
{
104
	sync_filesystem(sb);
L
Linus Torvalds 已提交
105 106 107 108
	*flags |= MS_NODIRATIME;
	return 0;
}

109
static const struct super_operations ncp_sops =
L
Linus Torvalds 已提交
110 111 112 113
{
	.alloc_inode	= ncp_alloc_inode,
	.destroy_inode	= ncp_destroy_inode,
	.drop_inode	= generic_delete_inode,
A
Al Viro 已提交
114
	.evict_inode	= ncp_evict_inode,
L
Linus Torvalds 已提交
115 116 117
	.put_super	= ncp_put_super,
	.statfs		= ncp_statfs,
	.remount_fs	= ncp_remount,
M
Miklos Szeredi 已提交
118
	.show_options	= ncp_show_options,
L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
};

/*
 * Fill in the ncpfs-specific information in the inode.
 */
static void ncp_update_dirent(struct inode *inode, struct ncp_entry_info *nwinfo)
{
	NCP_FINFO(inode)->DosDirNum = nwinfo->i.DosDirNum;
	NCP_FINFO(inode)->dirEntNum = nwinfo->i.dirEntNum;
	NCP_FINFO(inode)->volNumber = nwinfo->volume;
}

void ncp_update_inode(struct inode *inode, struct ncp_entry_info *nwinfo)
{
	ncp_update_dirent(inode, nwinfo);
	NCP_FINFO(inode)->nwattr = nwinfo->i.attributes;
	NCP_FINFO(inode)->access = nwinfo->access;
	memcpy(NCP_FINFO(inode)->file_handle, nwinfo->file_handle,
			sizeof(nwinfo->file_handle));
138
	ncp_dbg(1, "updated %s, volnum=%d, dirent=%u\n",
L
Linus Torvalds 已提交
139 140 141 142 143 144 145
		nwinfo->i.entryName, NCP_FINFO(inode)->volNumber,
		NCP_FINFO(inode)->dirEntNum);
}

static void ncp_update_dates(struct inode *inode, struct nw_info_struct *nwi)
{
	/* NFS namespace mode overrides others if it's set. */
146
	ncp_dbg(1, "(%s) nfs.mode=0%o\n", nwi->entryName, nwi->nfs.mode);
L
Linus Torvalds 已提交
147 148 149 150 151
	if (nwi->nfs.mode) {
		/* XXX Security? */
		inode->i_mode = nwi->nfs.mode;
	}

P
Petr Vandrovec 已提交
152
	inode->i_blocks = (i_size_read(inode) + NCP_BLOCK_SIZE - 1) >> NCP_BLOCK_SHIFT;
L
Linus Torvalds 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

	inode->i_mtime.tv_sec = ncp_date_dos2unix(nwi->modifyTime, nwi->modifyDate);
	inode->i_ctime.tv_sec = ncp_date_dos2unix(nwi->creationTime, nwi->creationDate);
	inode->i_atime.tv_sec = ncp_date_dos2unix(0, nwi->lastAccessDate);
	inode->i_atime.tv_nsec = 0;
	inode->i_mtime.tv_nsec = 0;
	inode->i_ctime.tv_nsec = 0;
}

static void ncp_update_attrs(struct inode *inode, struct ncp_entry_info *nwinfo)
{
	struct nw_info_struct *nwi = &nwinfo->i;
	struct ncp_server *server = NCP_SERVER(inode);

	if (nwi->attributes & aDIR) {
		inode->i_mode = server->m.dir_mode;
		/* for directories dataStreamSize seems to be some
		   Object ID ??? */
P
Petr Vandrovec 已提交
171
		i_size_write(inode, NCP_BLOCK_SIZE);
L
Linus Torvalds 已提交
172
	} else {
P
Petr Vandrovec 已提交
173 174
		u32 size;

L
Linus Torvalds 已提交
175
		inode->i_mode = server->m.file_mode;
P
Petr Vandrovec 已提交
176 177
		size = le32_to_cpu(nwi->dataStreamSize);
		i_size_write(inode, size);
L
Linus Torvalds 已提交
178 179 180 181 182 183
#ifdef CONFIG_NCPFS_EXTRAS
		if ((server->m.flags & (NCP_MOUNT_EXTRAS|NCP_MOUNT_SYMLINKS)) 
		 && (nwi->attributes & aSHARED)) {
			switch (nwi->attributes & (aHIDDEN|aSYSTEM)) {
				case aHIDDEN:
					if (server->m.flags & NCP_MOUNT_SYMLINKS) {
P
Petr Vandrovec 已提交
184 185
						if (/* (size >= NCP_MIN_SYMLINK_SIZE)
						 && */ (size <= NCP_MAX_SYMLINK_SIZE)) {
L
Linus Torvalds 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
							inode->i_mode = (inode->i_mode & ~S_IFMT) | S_IFLNK;
							NCP_FINFO(inode)->flags |= NCPI_KLUDGE_SYMLINK;
							break;
						}
					}
					/* FALLTHROUGH */
				case 0:
					if (server->m.flags & NCP_MOUNT_EXTRAS)
						inode->i_mode |= S_IRUGO;
					break;
				case aSYSTEM:
					if (server->m.flags & NCP_MOUNT_EXTRAS)
						inode->i_mode |= (inode->i_mode >> 2) & S_IXUGO;
					break;
				/* case aSYSTEM|aHIDDEN: */
				default:
					/* reserved combination */
					break;
			}
		}
#endif
	}
	if (nwi->attributes & aRONLY) inode->i_mode &= ~S_IWUGO;
}

void ncp_update_inode2(struct inode* inode, struct ncp_entry_info *nwinfo)
{
	NCP_FINFO(inode)->flags = 0;
	if (!atomic_read(&NCP_FINFO(inode)->opened)) {
		NCP_FINFO(inode)->nwattr = nwinfo->i.attributes;
		ncp_update_attrs(inode, nwinfo);
	}

	ncp_update_dates(inode, &nwinfo->i);
	ncp_update_dirent(inode, nwinfo);
}

/*
P
Petr Vandrovec 已提交
224
 * Fill in the inode based on the ncp_entry_info structure.  Used only for brand new inodes.
L
Linus Torvalds 已提交
225 226 227 228 229 230 231 232 233
 */
static void ncp_set_attr(struct inode *inode, struct ncp_entry_info *nwinfo)
{
	struct ncp_server *server = NCP_SERVER(inode);

	NCP_FINFO(inode)->flags = 0;
	
	ncp_update_attrs(inode, nwinfo);

234
	ncp_dbg(2, "inode->i_mode = %u\n", inode->i_mode);
L
Linus Torvalds 已提交
235

M
Miklos Szeredi 已提交
236
	set_nlink(inode, 1);
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244
	inode->i_uid = server->m.uid;
	inode->i_gid = server->m.gid;

	ncp_update_dates(inode, &nwinfo->i);
	ncp_update_inode(inode, nwinfo);
}

#if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
245
static const struct inode_operations ncp_symlink_inode_operations = {
246
	.get_link	= page_get_link,
L
Linus Torvalds 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259
	.setattr	= ncp_notify_change,
};
#endif

/*
 * Get a new inode.
 */
struct inode * 
ncp_iget(struct super_block *sb, struct ncp_entry_info *info)
{
	struct inode *inode;

	if (info == NULL) {
260
		pr_err("%s: info is NULL\n", __func__);
L
Linus Torvalds 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
		return NULL;
	}

	inode = new_inode(sb);
	if (inode) {
		atomic_set(&NCP_FINFO(inode)->opened, info->opened);

		inode->i_ino = info->ino;
		ncp_set_attr(inode, info);
		if (S_ISREG(inode->i_mode)) {
			inode->i_op = &ncp_file_inode_operations;
			inode->i_fop = &ncp_file_operations;
		} else if (S_ISDIR(inode->i_mode)) {
			inode->i_op = &ncp_dir_inode_operations;
			inode->i_fop = &ncp_dir_operations;
#ifdef CONFIG_NCPFS_NFS_NS
		} else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
			init_special_inode(inode, inode->i_mode,
				new_decode_dev(info->i.nfs.rdev));
#endif
#if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
		} else if (S_ISLNK(inode->i_mode)) {
			inode->i_op = &ncp_symlink_inode_operations;
284
			inode_nohighmem(inode);
L
Linus Torvalds 已提交
285 286 287 288 289 290 291
			inode->i_data.a_ops = &ncp_symlink_aops;
#endif
		} else {
			make_bad_inode(inode);
		}
		insert_inode_hash(inode);
	} else
292
		pr_err("%s: iget failed!\n", __func__);
L
Linus Torvalds 已提交
293 294 295 296
	return inode;
}

static void
A
Al Viro 已提交
297
ncp_evict_inode(struct inode *inode)
L
Linus Torvalds 已提交
298
{
299
	truncate_inode_pages_final(&inode->i_data);
300
	clear_inode(inode);
301

L
Linus Torvalds 已提交
302
	if (S_ISDIR(inode->i_mode)) {
303
		ncp_dbg(2, "put directory %ld\n", inode->i_ino);
L
Linus Torvalds 已提交
304 305 306 307
	}

	if (ncp_make_closed(inode) != 0) {
		/* We can't do anything but complain. */
308
		pr_err("%s: could not close\n", __func__);
L
Linus Torvalds 已提交
309 310 311 312 313
	}
}

static void ncp_stop_tasks(struct ncp_server *server) {
	struct sock* sk = server->ncp_sock->sk;
314 315

	lock_sock(sk);
L
Linus Torvalds 已提交
316 317 318
	sk->sk_error_report = server->error_report;
	sk->sk_data_ready   = server->data_ready;
	sk->sk_write_space  = server->write_space;
319
	release_sock(sk);
L
Linus Torvalds 已提交
320
	del_timer_sync(&server->timeout_tm);
321

322
	flush_work(&server->rcv.tq);
323
	if (sk->sk_socket->type == SOCK_STREAM)
324
		flush_work(&server->tx.tq);
325
	else
326
		flush_work(&server->timeout_tq);
L
Linus Torvalds 已提交
327 328
}

329
static int  ncp_show_options(struct seq_file *seq, struct dentry *root)
M
Miklos Szeredi 已提交
330
{
331
	struct ncp_server *server = NCP_SBP(root->d_sb);
M
Miklos Szeredi 已提交
332 333
	unsigned int tmp;

334 335 336 337 338 339 340 341 342
	if (!uid_eq(server->m.uid, GLOBAL_ROOT_UID))
		seq_printf(seq, ",uid=%u",
			   from_kuid_munged(&init_user_ns, server->m.uid));
	if (!gid_eq(server->m.gid, GLOBAL_ROOT_GID))
		seq_printf(seq, ",gid=%u",
			   from_kgid_munged(&init_user_ns, server->m.gid));
	if (!uid_eq(server->m.mounted_uid, GLOBAL_ROOT_UID))
		seq_printf(seq, ",owner=%u",
			   from_kuid_munged(&init_user_ns, server->m.mounted_uid));
M
Miklos Szeredi 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	tmp = server->m.file_mode & S_IALLUGO;
	if (tmp != NCP_DEFAULT_FILE_MODE)
		seq_printf(seq, ",mode=0%o", tmp);
	tmp = server->m.dir_mode & S_IALLUGO;
	if (tmp != NCP_DEFAULT_DIR_MODE)
		seq_printf(seq, ",dirmode=0%o", tmp);
	if (server->m.time_out != NCP_DEFAULT_TIME_OUT * HZ / 100) {
		tmp = server->m.time_out * 100 / HZ;
		seq_printf(seq, ",timeout=%u", tmp);
	}
	if (server->m.retry_count != NCP_DEFAULT_RETRY_COUNT)
		seq_printf(seq, ",retry=%u", server->m.retry_count);
	if (server->m.flags != 0)
		seq_printf(seq, ",flags=%lu", server->m.flags);
	if (server->m.wdog_pid != NULL)
		seq_printf(seq, ",wdogpid=%u", pid_vnr(server->m.wdog_pid));

	return 0;
}

L
Linus Torvalds 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
static const struct ncp_option ncp_opts[] = {
	{ "uid",	OPT_INT,	'u' },
	{ "gid",	OPT_INT,	'g' },
	{ "owner",	OPT_INT,	'o' },
	{ "mode",	OPT_INT,	'm' },
	{ "dirmode",	OPT_INT,	'd' },
	{ "timeout",	OPT_INT,	't' },
	{ "retry",	OPT_INT,	'r' },
	{ "flags",	OPT_INT,	'f' },
	{ "wdogpid",	OPT_INT,	'w' },
	{ "ncpfd",	OPT_INT,	'n' },
	{ "infofd",	OPT_INT,	'i' },	/* v5 */
	{ "version",	OPT_INT,	'v' },
	{ NULL,		0,		0 } };

static int ncp_parse_options(struct ncp_mount_data_kernel *data, char *options) {
	int optval;
	char *optarg;
	unsigned long optint;
	int version = 0;
383
	int ret;
L
Linus Torvalds 已提交
384 385 386

	data->flags = 0;
	data->int_flags = 0;
387
	data->mounted_uid = GLOBAL_ROOT_UID;
388
	data->wdog_pid = NULL;
L
Linus Torvalds 已提交
389
	data->ncp_fd = ~0;
M
Miklos Szeredi 已提交
390 391
	data->time_out = NCP_DEFAULT_TIME_OUT;
	data->retry_count = NCP_DEFAULT_RETRY_COUNT;
392 393
	data->uid = GLOBAL_ROOT_UID;
	data->gid = GLOBAL_ROOT_GID;
M
Miklos Szeredi 已提交
394 395
	data->file_mode = NCP_DEFAULT_FILE_MODE;
	data->dir_mode = NCP_DEFAULT_DIR_MODE;
L
Linus Torvalds 已提交
396 397 398 399
	data->info_fd = -1;
	data->mounted_vol[0] = 0;
	
	while ((optval = ncp_getopt("ncpfs", &options, ncp_opts, NULL, &optarg, &optint)) != 0) {
400 401 402
		ret = optval;
		if (ret < 0)
			goto err;
L
Linus Torvalds 已提交
403 404
		switch (optval) {
			case 'u':
405
				data->uid = make_kuid(current_user_ns(), optint);
406 407
				if (!uid_valid(data->uid)) {
					ret = -EINVAL;
408
					goto err;
409
				}
L
Linus Torvalds 已提交
410 411
				break;
			case 'g':
412
				data->gid = make_kgid(current_user_ns(), optint);
413 414
				if (!gid_valid(data->gid)) {
					ret = -EINVAL;
415
					goto err;
416
				}
L
Linus Torvalds 已提交
417 418
				break;
			case 'o':
419
				data->mounted_uid = make_kuid(current_user_ns(), optint);
420 421
				if (!uid_valid(data->mounted_uid)) {
					ret = -EINVAL;
422
					goto err;
423
				}
L
Linus Torvalds 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
				break;
			case 'm':
				data->file_mode = optint;
				break;
			case 'd':
				data->dir_mode = optint;
				break;
			case 't':
				data->time_out = optint;
				break;
			case 'r':
				data->retry_count = optint;
				break;
			case 'f':
				data->flags = optint;
				break;
			case 'w':
441
				data->wdog_pid = find_get_pid(optint);
L
Linus Torvalds 已提交
442 443 444 445 446 447 448 449
				break;
			case 'n':
				data->ncp_fd = optint;
				break;
			case 'i':
				data->info_fd = optint;
				break;
			case 'v':
450 451 452 453 454
				ret = -ECHRNG;
				if (optint < NCP_MOUNT_VERSION_V4)
					goto err;
				if (optint > NCP_MOUNT_VERSION_V5)
					goto err;
L
Linus Torvalds 已提交
455 456 457 458 459 460
				version = optint;
				break;
			
		}
	}
	return 0;
461 462 463 464
err:
	put_pid(data->wdog_pid);
	data->wdog_pid = NULL;
	return ret;
L
Linus Torvalds 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
}

static int ncp_fill_super(struct super_block *sb, void *raw_data, int silent)
{
	struct ncp_mount_data_kernel data;
	struct ncp_server *server;
	struct inode *root_inode;
	struct socket *sock;
	int error;
	int default_bufsize;
#ifdef CONFIG_NCPFS_PACKET_SIGNING
	int options;
#endif
	struct ncp_entry_info finfo;

480
	memset(&data, 0, sizeof(data));
481
	server = kzalloc(sizeof(struct ncp_server), GFP_KERNEL);
L
Linus Torvalds 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495
	if (!server)
		return -ENOMEM;
	sb->s_fs_info = server;

	error = -EFAULT;
	if (raw_data == NULL)
		goto out;
	switch (*(int*)raw_data) {
		case NCP_MOUNT_VERSION:
			{
				struct ncp_mount_data* md = (struct ncp_mount_data*)raw_data;

				data.flags = md->flags;
				data.int_flags = NCP_IMOUNT_LOGGEDIN_POSSIBLE;
496
				data.mounted_uid = make_kuid(current_user_ns(), md->mounted_uid);
497
				data.wdog_pid = find_get_pid(md->wdog_pid);
L
Linus Torvalds 已提交
498 499 500
				data.ncp_fd = md->ncp_fd;
				data.time_out = md->time_out;
				data.retry_count = md->retry_count;
501 502
				data.uid = make_kuid(current_user_ns(), md->uid);
				data.gid = make_kgid(current_user_ns(), md->gid);
L
Linus Torvalds 已提交
503 504 505 506 507 508 509 510 511 512 513 514
				data.file_mode = md->file_mode;
				data.dir_mode = md->dir_mode;
				data.info_fd = -1;
				memcpy(data.mounted_vol, md->mounted_vol,
					NCP_VOLNAME_LEN+1);
			}
			break;
		case NCP_MOUNT_VERSION_V4:
			{
				struct ncp_mount_data_v4* md = (struct ncp_mount_data_v4*)raw_data;

				data.flags = md->flags;
515
				data.mounted_uid = make_kuid(current_user_ns(), md->mounted_uid);
516
				data.wdog_pid = find_get_pid(md->wdog_pid);
L
Linus Torvalds 已提交
517 518 519
				data.ncp_fd = md->ncp_fd;
				data.time_out = md->time_out;
				data.retry_count = md->retry_count;
520 521
				data.uid = make_kuid(current_user_ns(), md->uid);
				data.gid = make_kgid(current_user_ns(), md->gid);
L
Linus Torvalds 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535
				data.file_mode = md->file_mode;
				data.dir_mode = md->dir_mode;
				data.info_fd = -1;
			}
			break;
		default:
			error = -ECHRNG;
			if (memcmp(raw_data, "vers", 4) == 0) {
				error = ncp_parse_options(&data, raw_data);
			}
			if (error)
				goto out;
			break;
	}
536 537 538 539
	error = -EINVAL;
	if (!uid_valid(data.mounted_uid) || !uid_valid(data.uid) ||
	    !gid_valid(data.gid))
		goto out;
540
	sock = sockfd_lookup(data.ncp_fd, &error);
L
Linus Torvalds 已提交
541
	if (!sock)
542 543
		goto out;

L
Linus Torvalds 已提交
544 545 546 547 548 549 550 551 552 553 554
	if (sock->type == SOCK_STREAM)
		default_bufsize = 0xF000;
	else
		default_bufsize = 1024;

	sb->s_flags |= MS_NODIRATIME;	/* probably even noatime */
	sb->s_maxbytes = 0xFFFFFFFFU;
	sb->s_blocksize = 1024;	/* Eh...  Is this correct? */
	sb->s_blocksize_bits = 10;
	sb->s_magic = NCP_SUPER_MAGIC;
	sb->s_op = &ncp_sops;
A
Al Viro 已提交
555
	sb->s_d_op = &ncp_dentry_operations;
556
	sb->s_bdi = &server->bdi;
L
Linus Torvalds 已提交
557 558 559 560

	server = NCP_SBP(sb);
	memset(server, 0, sizeof(*server));

561
	error = bdi_setup_and_register(&server->bdi, "ncpfs");
562
	if (error)
563
		goto out_fput;
564

L
Linus Torvalds 已提交
565 566 567
	server->ncp_sock = sock;
	
	if (data.info_fd != -1) {
568
		struct socket *info_sock = sockfd_lookup(data.info_fd, &error);
L
Linus Torvalds 已提交
569
		if (!info_sock)
570 571
			goto out_bdi;
		server->info_sock = info_sock;
L
Linus Torvalds 已提交
572 573 574 575 576 577
		error = -EBADFD;
		if (info_sock->type != SOCK_STREAM)
			goto out_fput2;
	}

/*	server->lock = 0;	*/
I
Ingo Molnar 已提交
578
	mutex_init(&server->mutex);
L
Linus Torvalds 已提交
579 580 581 582 583
	server->packet = NULL;
/*	server->buffer_size = 0;	*/
/*	server->conn_status = 0;	*/
/*	server->root_dentry = NULL;	*/
/*	server->root_setuped = 0;	*/
P
Petr Vandrovec 已提交
584
	mutex_init(&server->root_setup_lock);
L
Linus Torvalds 已提交
585 586 587 588
#ifdef CONFIG_NCPFS_PACKET_SIGNING
/*	server->sign_wanted = 0;	*/
/*	server->sign_active = 0;	*/
#endif
P
Petr Vandrovec 已提交
589
	init_rwsem(&server->auth_rwsem);
L
Linus Torvalds 已提交
590 591 592 593 594 595 596 597
	server->auth.auth_type = NCP_AUTH_NONE;
/*	server->auth.object_name_len = 0;	*/
/*	server->auth.object_name = NULL;	*/
/*	server->auth.object_type = 0;		*/
/*	server->priv.len = 0;			*/
/*	server->priv.data = NULL;		*/

	server->m = data;
L
Lucas De Marchi 已提交
598
	/* Although anything producing this is buggy, it happens
L
Linus Torvalds 已提交
599 600 601
	   now because of PATH_MAX changes.. */
	if (server->m.time_out < 1) {
		server->m.time_out = 10;
602
		pr_info("You need to recompile your ncpfs utils..\n");
L
Linus Torvalds 已提交
603 604 605 606 607 608 609 610 611 612 613
	}
	server->m.time_out = server->m.time_out * HZ / 100;
	server->m.file_mode = (server->m.file_mode & S_IRWXUGO) | S_IFREG;
	server->m.dir_mode = (server->m.dir_mode & S_IRWXUGO) | S_IFDIR;

#ifdef CONFIG_NCPFS_NLS
	/* load the default NLS charsets */
	server->nls_vol = load_nls_default();
	server->nls_io = load_nls_default();
#endif /* CONFIG_NCPFS_NLS */

P
Petr Vandrovec 已提交
614
	atomic_set(&server->dentry_ttl, 0);	/* no caching */
L
Linus Torvalds 已提交
615 616

	INIT_LIST_HEAD(&server->tx.requests);
I
Ingo Molnar 已提交
617
	mutex_init(&server->rcv.creq_mutex);
L
Linus Torvalds 已提交
618 619 620 621 622 623 624 625 626 627 628
	server->tx.creq		= NULL;
	server->rcv.creq	= NULL;

	init_timer(&server->timeout_tm);
#undef NCP_PACKET_SIZE
#define NCP_PACKET_SIZE 131072
	error = -ENOMEM;
	server->packet_size = NCP_PACKET_SIZE;
	server->packet = vmalloc(NCP_PACKET_SIZE);
	if (server->packet == NULL)
		goto out_nls;
629 630 631 632 633 634
	server->txbuf = vmalloc(NCP_PACKET_SIZE);
	if (server->txbuf == NULL)
		goto out_packet;
	server->rxbuf = vmalloc(NCP_PACKET_SIZE);
	if (server->rxbuf == NULL)
		goto out_txbuf;
L
Linus Torvalds 已提交
635

636 637 638 639 640
	lock_sock(sock->sk);
	server->data_ready	= sock->sk->sk_data_ready;
	server->write_space	= sock->sk->sk_write_space;
	server->error_report	= sock->sk->sk_error_report;
	sock->sk->sk_user_data	= server;
L
Linus Torvalds 已提交
641 642 643 644 645 646
	sock->sk->sk_data_ready	  = ncp_tcp_data_ready;
	sock->sk->sk_error_report = ncp_tcp_error_report;
	if (sock->type == SOCK_STREAM) {
		server->rcv.ptr = (unsigned char*)&server->rcv.buf;
		server->rcv.len = 10;
		server->rcv.state = 0;
D
David Howells 已提交
647 648
		INIT_WORK(&server->rcv.tq, ncp_tcp_rcv_proc);
		INIT_WORK(&server->tx.tq, ncp_tcp_tx_proc);
L
Linus Torvalds 已提交
649 650
		sock->sk->sk_write_space = ncp_tcp_write_space;
	} else {
D
David Howells 已提交
651 652
		INIT_WORK(&server->rcv.tq, ncpdgram_rcv_proc);
		INIT_WORK(&server->timeout_tq, ncpdgram_timeout_proc);
L
Linus Torvalds 已提交
653 654 655
		server->timeout_tm.data = (unsigned long)server;
		server->timeout_tm.function = ncpdgram_timeout_call;
	}
656
	release_sock(sock->sk);
L
Linus Torvalds 已提交
657 658 659 660 661

	ncp_lock_server(server);
	error = ncp_connect(server);
	ncp_unlock_server(server);
	if (error < 0)
662
		goto out_rxbuf;
663
	ncp_dbg(1, "NCP_SBP(sb) = %p\n", NCP_SBP(sb));
L
Linus Torvalds 已提交
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680

	error = -EMSGSIZE;	/* -EREMOTESIDEINCOMPATIBLE */
#ifdef CONFIG_NCPFS_PACKET_SIGNING
	if (ncp_negotiate_size_and_options(server, default_bufsize,
		NCP_DEFAULT_OPTIONS, &(server->buffer_size), &options) == 0)
	{
		if (options != NCP_DEFAULT_OPTIONS)
		{
			if (ncp_negotiate_size_and_options(server, 
				default_bufsize,
				options & 2, 
				&(server->buffer_size), &options) != 0)
				
			{
				goto out_disconnect;
			}
		}
P
Petr Vandrovec 已提交
681
		ncp_lock_server(server);
L
Linus Torvalds 已提交
682 683
		if (options & 2)
			server->sign_wanted = 1;
P
Petr Vandrovec 已提交
684
		ncp_unlock_server(server);
L
Linus Torvalds 已提交
685 686 687 688 689 690
	}
	else 
#endif	/* CONFIG_NCPFS_PACKET_SIGNING */
	if (ncp_negotiate_buffersize(server, default_bufsize,
  				     &(server->buffer_size)) != 0)
		goto out_disconnect;
691
	ncp_dbg(1, "bufsize = %d\n", server->buffer_size);
L
Linus Torvalds 已提交
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

	memset(&finfo, 0, sizeof(finfo));
	finfo.i.attributes	= aDIR;
	finfo.i.dataStreamSize	= 0;	/* ignored */
	finfo.i.dirEntNum	= 0;
	finfo.i.DosDirNum	= 0;
#ifdef CONFIG_NCPFS_SMALLDOS
	finfo.i.NSCreator	= NW_NS_DOS;
#endif
	finfo.volume		= NCP_NUMBER_OF_VOLUMES;
	/* set dates of mountpoint to Jan 1, 1986; 00:00 */
	finfo.i.creationTime	= finfo.i.modifyTime
				= cpu_to_le16(0x0000);
	finfo.i.creationDate	= finfo.i.modifyDate
				= finfo.i.lastAccessDate
				= cpu_to_le16(0x0C21);
	finfo.i.nameLen		= 0;
	finfo.i.entryName[0]	= '\0';

	finfo.opened		= 0;
	finfo.ino		= 2;	/* tradition */

	server->name_space[finfo.volume] = NW_NS_DOS;

	error = -ENOMEM;
        root_inode = ncp_iget(sb, &finfo);
        if (!root_inode)
		goto out_disconnect;
720
	ncp_dbg(1, "root vol=%d\n", NCP_FINFO(root_inode)->volNumber);
721
	sb->s_root = d_make_root(root_inode);
L
Linus Torvalds 已提交
722
        if (!sb->s_root)
723
		goto out_disconnect;
L
Linus Torvalds 已提交
724 725 726 727 728 729
	return 0;

out_disconnect:
	ncp_lock_server(server);
	ncp_disconnect(server);
	ncp_unlock_server(server);
730
out_rxbuf:
L
Linus Torvalds 已提交
731
	ncp_stop_tasks(server);
732 733 734 735
	vfree(server->rxbuf);
out_txbuf:
	vfree(server->txbuf);
out_packet:
L
Linus Torvalds 已提交
736 737 738 739 740 741
	vfree(server->packet);
out_nls:
#ifdef CONFIG_NCPFS_NLS
	unload_nls(server->nls_io);
	unload_nls(server->nls_vol);
#endif
P
Petr Vandrovec 已提交
742 743 744
	mutex_destroy(&server->rcv.creq_mutex);
	mutex_destroy(&server->root_setup_lock);
	mutex_destroy(&server->mutex);
L
Linus Torvalds 已提交
745
out_fput2:
746 747
	if (server->info_sock)
		sockfd_put(server->info_sock);
748
out_bdi:
749 750
	bdi_destroy(&server->bdi);
out_fput:
751
	sockfd_put(sock);
L
Linus Torvalds 已提交
752
out:
753
	put_pid(data.wdog_pid);
L
Linus Torvalds 已提交
754 755 756 757 758
	sb->s_fs_info = NULL;
	kfree(server);
	return error;
}

759 760 761 762 763 764 765 766 767 768 769
static void delayed_free(struct rcu_head *p)
{
	struct ncp_server *server = container_of(p, struct ncp_server, rcu);
#ifdef CONFIG_NCPFS_NLS
	/* unload the NLS charsets */
	unload_nls(server->nls_vol);
	unload_nls(server->nls_io);
#endif /* CONFIG_NCPFS_NLS */
	kfree(server);
}

L
Linus Torvalds 已提交
770 771 772 773 774 775 776 777 778 779
static void ncp_put_super(struct super_block *sb)
{
	struct ncp_server *server = NCP_SBP(sb);

	ncp_lock_server(server);
	ncp_disconnect(server);
	ncp_unlock_server(server);

	ncp_stop_tasks(server);

P
Petr Vandrovec 已提交
780 781 782
	mutex_destroy(&server->rcv.creq_mutex);
	mutex_destroy(&server->root_setup_lock);
	mutex_destroy(&server->mutex);
L
Linus Torvalds 已提交
783

784 785 786
	if (server->info_sock)
		sockfd_put(server->info_sock);
	sockfd_put(server->ncp_sock);
787 788
	kill_pid(server->m.wdog_pid, SIGTERM, 1);
	put_pid(server->m.wdog_pid);
L
Linus Torvalds 已提交
789

790
	bdi_destroy(&server->bdi);
791 792
	kfree(server->priv.data);
	kfree(server->auth.object_name);
793 794
	vfree(server->rxbuf);
	vfree(server->txbuf);
L
Linus Torvalds 已提交
795
	vfree(server->packet);
796
	call_rcu(&server->rcu, delayed_free);
L
Linus Torvalds 已提交
797 798
}

799
static int ncp_statfs(struct dentry *dentry, struct kstatfs *buf)
L
Linus Torvalds 已提交
800 801 802 803 804 805
{
	struct dentry* d;
	struct inode* i;
	struct ncp_inode_info* ni;
	struct ncp_server* s;
	struct ncp_volume_info vi;
806
	struct super_block *sb = dentry->d_sb;
L
Linus Torvalds 已提交
807 808 809 810 811 812 813
	int err;
	__u8 dh;
	
	d = sb->s_root;
	if (!d) {
		goto dflt;
	}
814
	i = d_inode(d);
L
Linus Torvalds 已提交
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
	if (!i) {
		goto dflt;
	}
	ni = NCP_FINFO(i);
	if (!ni) {
		goto dflt;
	}
	s = NCP_SBP(sb);
	if (!s) {
		goto dflt;
	}
	if (!s->m.mounted_vol[0]) {
		goto dflt;
	}

	err = ncp_dirhandle_alloc(s, ni->volNumber, ni->DosDirNum, &dh);
	if (err) {
		goto dflt;
	}
	err = ncp_get_directory_info(s, dh, &vi);
	ncp_dirhandle_free(s, dh);
	if (err) {
		goto dflt;
	}
	buf->f_type = NCP_SUPER_MAGIC;
	buf->f_bsize = vi.sectors_per_block * 512;
	buf->f_blocks = vi.total_blocks;
	buf->f_bfree = vi.free_blocks;
	buf->f_bavail = vi.free_blocks;
	buf->f_files = vi.total_dir_entries;
	buf->f_ffree = vi.available_dir_entries;
	buf->f_namelen = 12;
	return 0;

	/* We cannot say how much disk space is left on a mounted
	   NetWare Server, because free space is distributed over
	   volumes, and the current user might have disk quotas. So
	   free space is not that simple to determine. Our decision
	   here is to err conservatively. */

dflt:;
	buf->f_type = NCP_SUPER_MAGIC;
	buf->f_bsize = NCP_BLOCK_SIZE;
	buf->f_blocks = 0;
	buf->f_bfree = 0;
	buf->f_bavail = 0;
	buf->f_namelen = 12;
	return 0;
}

int ncp_notify_change(struct dentry *dentry, struct iattr *attr)
{
867
	struct inode *inode = d_inode(dentry);
L
Linus Torvalds 已提交
868 869 870 871 872 873 874 875
	int result = 0;
	__le32 info_mask;
	struct nw_modify_dos_info info;
	struct ncp_server *server;

	result = -EIO;

	server = NCP_SERVER(inode);
P
Petr Vandrovec 已提交
876
	if (!server)	/* How this could happen? */
L
Linus Torvalds 已提交
877 878
		goto out;

879
	result = -EPERM;
880
	if (IS_DEADDIR(d_inode(dentry)))
881 882
		goto out;

L
Linus Torvalds 已提交
883 884 885
	/* ageing the dentry to force validation */
	ncp_age_dentry(server, dentry);

886
	result = setattr_prepare(dentry, attr);
L
Linus Torvalds 已提交
887 888 889 890
	if (result < 0)
		goto out;

	result = -EPERM;
891
	if ((attr->ia_valid & ATTR_UID) && !uid_eq(attr->ia_uid, server->m.uid))
L
Linus Torvalds 已提交
892 893
		goto out;

894
	if ((attr->ia_valid & ATTR_GID) && !gid_eq(attr->ia_gid, server->m.gid))
L
Linus Torvalds 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
		goto out;

	if (((attr->ia_valid & ATTR_MODE) &&
	     (attr->ia_mode &
	      ~(S_IFREG | S_IFDIR | S_IRWXUGO))))
		goto out;

	info_mask = 0;
	memset(&info, 0, sizeof(info));

#if 1 
        if ((attr->ia_valid & ATTR_MODE) != 0)
        {
		umode_t newmode = attr->ia_mode;

		info_mask |= DM_ATTRIBUTES;

                if (S_ISDIR(inode->i_mode)) {
                	newmode &= server->m.dir_mode;
		} else {
#ifdef CONFIG_NCPFS_EXTRAS			
			if (server->m.flags & NCP_MOUNT_EXTRAS) {
				/* any non-default execute bit set */
				if (newmode & ~server->m.file_mode & S_IXUGO)
					info.attributes |= aSHARED | aSYSTEM;
				/* read for group/world and not in default file_mode */
				else if (newmode & ~server->m.file_mode & S_IRUGO)
					info.attributes |= aSHARED;
			} else
#endif
				newmode &= server->m.file_mode;			
                }
                if (newmode & S_IWUGO)
                	info.attributes &= ~(aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT);
                else
			info.attributes |=  (aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT);

#ifdef CONFIG_NCPFS_NFS_NS
		if (ncp_is_nfs_extras(server, NCP_FINFO(inode)->volNumber)) {
			result = ncp_modify_nfs_info(server,
						     NCP_FINFO(inode)->volNumber,
						     NCP_FINFO(inode)->dirEntNum,
						     attr->ia_mode, 0);
			if (result != 0)
				goto out;
			info.attributes &= ~(aSHARED | aSYSTEM);
			{
				/* mark partial success */
				struct iattr tmpattr;
				
				tmpattr.ia_valid = ATTR_MODE;
				tmpattr.ia_mode = attr->ia_mode;

C
Christoph Hellwig 已提交
948 949
				setattr_copy(inode, &tmpattr);
				mark_inode_dirty(inode);
L
Linus Torvalds 已提交
950 951 952 953 954 955 956 957 958 959 960
			}
		}
#endif
        }
#endif

	/* Do SIZE before attributes, otherwise mtime together with size does not work...
	 */
	if ((attr->ia_valid & ATTR_SIZE) != 0) {
		int written;

961
		ncp_dbg(1, "trying to change size to %llu\n", attr->ia_size);
L
Linus Torvalds 已提交
962 963 964 965 966 967 968 969 970 971 972 973 974 975

		if ((result = ncp_make_open(inode, O_WRONLY)) < 0) {
			result = -EACCES;
			goto out;
		}
		ncp_write_kernel(NCP_SERVER(inode), NCP_FINFO(inode)->file_handle,
			  attr->ia_size, 0, "", &written);

		/* According to ndir, the changes only take effect after
		   closing the file */
		ncp_inode_close(inode);
		result = ncp_make_closed(inode);
		if (result)
			goto out;
C
Christoph Hellwig 已提交
976 977

		if (attr->ia_size != i_size_read(inode)) {
M
Marco Stornelli 已提交
978
			truncate_setsize(inode, attr->ia_size);
C
Christoph Hellwig 已提交
979
			mark_inode_dirty(inode);
L
Linus Torvalds 已提交
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		}
	}
	if ((attr->ia_valid & ATTR_CTIME) != 0) {
		info_mask |= (DM_CREATE_TIME | DM_CREATE_DATE);
		ncp_date_unix2dos(attr->ia_ctime.tv_sec,
			     &info.creationTime, &info.creationDate);
	}
	if ((attr->ia_valid & ATTR_MTIME) != 0) {
		info_mask |= (DM_MODIFY_TIME | DM_MODIFY_DATE);
		ncp_date_unix2dos(attr->ia_mtime.tv_sec,
				  &info.modifyTime, &info.modifyDate);
	}
	if ((attr->ia_valid & ATTR_ATIME) != 0) {
		__le16 dummy;
		info_mask |= (DM_LAST_ACCESS_DATE);
		ncp_date_unix2dos(attr->ia_atime.tv_sec,
				  &dummy, &info.lastAccessDate);
	}
	if (info_mask != 0) {
		result = ncp_modify_file_or_subdir_dos_info(NCP_SERVER(inode),
				      inode, info_mask, &info);
		if (result != 0) {
			if (info_mask == (DM_CREATE_TIME | DM_CREATE_DATE)) {
				/* NetWare seems not to allow this. I
				   do not know why. So, just tell the
				   user everything went fine. This is
				   a terrible hack, but I do not know
				   how to do this correctly. */
				result = 0;
			} else
				goto out;
		}
#ifdef CONFIG_NCPFS_STRONG		
		if ((!result) && (info_mask & DM_ATTRIBUTES))
			NCP_FINFO(inode)->nwattr = info.attributes;
#endif
	}
C
Christoph Hellwig 已提交
1017 1018 1019 1020 1021 1022
	if (result)
		goto out;

	setattr_copy(inode, attr);
	mark_inode_dirty(inode);

L
Linus Torvalds 已提交
1023
out:
P
Petr Vandrovec 已提交
1024 1025
	if (result > 0)
		result = -EACCES;
L
Linus Torvalds 已提交
1026 1027 1028
	return result;
}

A
Al Viro 已提交
1029 1030
static struct dentry *ncp_mount(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data)
L
Linus Torvalds 已提交
1031
{
A
Al Viro 已提交
1032
	return mount_nodev(fs_type, flags, data, ncp_fill_super);
L
Linus Torvalds 已提交
1033 1034 1035 1036 1037
}

static struct file_system_type ncp_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "ncpfs",
A
Al Viro 已提交
1038
	.mount		= ncp_mount,
L
Linus Torvalds 已提交
1039
	.kill_sb	= kill_anon_super,
M
Miklos Szeredi 已提交
1040
	.fs_flags	= FS_BINARY_MOUNTDATA,
L
Linus Torvalds 已提交
1041
};
1042
MODULE_ALIAS_FS("ncpfs");
L
Linus Torvalds 已提交
1043 1044 1045 1046

static int __init init_ncp_fs(void)
{
	int err;
1047
	ncp_dbg(1, "called\n");
L
Linus Torvalds 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063

	err = init_inodecache();
	if (err)
		goto out1;
	err = register_filesystem(&ncp_fs_type);
	if (err)
		goto out;
	return 0;
out:
	destroy_inodecache();
out1:
	return err;
}

static void __exit exit_ncp_fs(void)
{
1064
	ncp_dbg(1, "called\n");
L
Linus Torvalds 已提交
1065 1066 1067 1068 1069 1070 1071
	unregister_filesystem(&ncp_fs_type);
	destroy_inodecache();
}

module_init(init_ncp_fs)
module_exit(exit_ncp_fs)
MODULE_LICENSE("GPL");