dir.c 31.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  dir.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, 1999 Wolfram Pienkoss for NLS
 *  Modified 1999 Wolfram Pienkoss for directory caching
 *  Modified 2000 Ben Harris, University of Cambridge for NFS NS meta-info
 *
 */


#include <linux/time.h>
#include <linux/errno.h>
#include <linux/stat.h>
#include <linux/kernel.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>
20
#include <linux/namei.h>
L
Linus Torvalds 已提交
21 22 23
#include <asm/uaccess.h>
#include <asm/byteorder.h>

24
#include "ncp_fs.h"
L
Linus Torvalds 已提交
25

A
Al Viro 已提交
26
static void ncp_read_volume_list(struct file *, struct dir_context *,
L
Linus Torvalds 已提交
27
				struct ncp_cache_control *);
A
Al Viro 已提交
28
static void ncp_do_readdir(struct file *, struct dir_context *,
L
Linus Torvalds 已提交
29 30
				struct ncp_cache_control *);

A
Al Viro 已提交
31
static int ncp_readdir(struct file *, struct dir_context *);
L
Linus Torvalds 已提交
32

A
Al Viro 已提交
33
static int ncp_create(struct inode *, struct dentry *, umode_t, bool);
A
Al Viro 已提交
34
static struct dentry *ncp_lookup(struct inode *, struct dentry *, unsigned int);
L
Linus Torvalds 已提交
35
static int ncp_unlink(struct inode *, struct dentry *);
36
static int ncp_mkdir(struct inode *, struct dentry *, umode_t);
L
Linus Torvalds 已提交
37 38 39 40
static int ncp_rmdir(struct inode *, struct dentry *);
static int ncp_rename(struct inode *, struct dentry *,
	  	      struct inode *, struct dentry *);
static int ncp_mknod(struct inode * dir, struct dentry *dentry,
A
Al Viro 已提交
41
		     umode_t mode, dev_t rdev);
L
Linus Torvalds 已提交
42 43 44 45 46 47
#if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
extern int ncp_symlink(struct inode *, struct dentry *, const char *);
#else
#define ncp_symlink NULL
#endif
		      
48
const struct file_operations ncp_dir_operations =
L
Linus Torvalds 已提交
49
{
50
	.llseek		= generic_file_llseek,
L
Linus Torvalds 已提交
51
	.read		= generic_read_dir,
A
Al Viro 已提交
52
	.iterate	= ncp_readdir,
J
John Kacur 已提交
53
	.unlocked_ioctl	= ncp_ioctl,
54 55 56
#ifdef CONFIG_COMPAT
	.compat_ioctl	= ncp_compat_ioctl,
#endif
L
Linus Torvalds 已提交
57 58
};

59
const struct inode_operations ncp_dir_inode_operations =
L
Linus Torvalds 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
{
	.create		= ncp_create,
	.lookup		= ncp_lookup,
	.unlink		= ncp_unlink,
	.symlink	= ncp_symlink,
	.mkdir		= ncp_mkdir,
	.rmdir		= ncp_rmdir,
	.mknod		= ncp_mknod,
	.rename		= ncp_rename,
	.setattr	= ncp_notify_change,
};

/*
 * Dentry operations routines
 */
75
static int ncp_lookup_validate(struct dentry *, unsigned int);
76 77
static int ncp_hash_dentry(const struct dentry *, struct qstr *);
static int ncp_compare_dentry(const struct dentry *, const struct dentry *,
N
Nick Piggin 已提交
78
		unsigned int, const char *, const struct qstr *);
N
Nick Piggin 已提交
79
static int ncp_delete_dentry(const struct dentry *);
L
Linus Torvalds 已提交
80

A
Al Viro 已提交
81
const struct dentry_operations ncp_dentry_operations =
L
Linus Torvalds 已提交
82 83 84 85 86 87 88
{
	.d_revalidate	= ncp_lookup_validate,
	.d_hash		= ncp_hash_dentry,
	.d_compare	= ncp_compare_dentry,
	.d_delete	= ncp_delete_dentry,
};

P
Petr Vandrovec 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#define ncp_namespace(i)	(NCP_SERVER(i)->name_space[NCP_FINFO(i)->volNumber])

static inline int ncp_preserve_entry_case(struct inode *i, __u32 nscreator)
{
#ifdef CONFIG_NCPFS_SMALLDOS
	int ns = ncp_namespace(i);

	if ((ns == NW_NS_DOS)
#ifdef CONFIG_NCPFS_OS2_NS
		|| ((ns == NW_NS_OS2) && (nscreator == NW_NS_DOS))
#endif /* CONFIG_NCPFS_OS2_NS */
	   )
		return 0;
#endif /* CONFIG_NCPFS_SMALLDOS */
	return 1;
}

#define ncp_preserve_case(i)	(ncp_namespace(i) != NW_NS_DOS)

N
Nick Piggin 已提交
108
static inline int ncp_case_sensitive(const struct inode *i)
P
Petr Vandrovec 已提交
109 110
{
#ifdef CONFIG_NCPFS_NFS_NS
N
Nick Piggin 已提交
111
	return ncp_namespace(i) == NW_NS_NFS;
P
Petr Vandrovec 已提交
112 113 114 115 116
#else
	return 0;
#endif /* CONFIG_NCPFS_NFS_NS */
}

L
Linus Torvalds 已提交
117 118 119
/*
 * Note: leave the hash unchanged if the directory
 * is case-sensitive.
120 121 122 123
 *
 * Accessing the parent inode can be racy under RCU pathwalking.
 * Use ACCESS_ONCE() to make sure we use _one_ particular inode,
 * the callers will handle races.
L
Linus Torvalds 已提交
124 125
 */
static int 
126
ncp_hash_dentry(const struct dentry *dentry, struct qstr *this)
L
Linus Torvalds 已提交
127
{
128 129 130 131 132
	struct inode *inode = ACCESS_ONCE(dentry->d_inode);

	if (!inode)
		return 0;

N
Nick Piggin 已提交
133
	if (!ncp_case_sensitive(inode)) {
N
Nick Piggin 已提交
134
		struct super_block *sb = dentry->d_sb;
P
Petr Vandrovec 已提交
135 136 137
		struct nls_table *t;
		unsigned long hash;
		int i;
L
Linus Torvalds 已提交
138

N
Nick Piggin 已提交
139
		t = NCP_IO_TABLE(sb);
L
Linus Torvalds 已提交
140 141 142 143 144 145 146 147 148
		hash = init_name_hash();
		for (i=0; i<this->len ; i++)
			hash = partial_name_hash(ncp_tolower(t, this->name[i]),
									hash);
		this->hash = end_name_hash(hash);
	}
	return 0;
}

149 150 151 152 153
/*
 * Accessing the parent inode can be racy under RCU pathwalking.
 * Use ACCESS_ONCE() to make sure we use _one_ particular inode,
 * the callers will handle races.
 */
L
Linus Torvalds 已提交
154
static int
155
ncp_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
N
Nick Piggin 已提交
156
		unsigned int len, const char *str, const struct qstr *name)
L
Linus Torvalds 已提交
157
{
158 159
	struct inode *pinode;

N
Nick Piggin 已提交
160
	if (len != name->len)
L
Linus Torvalds 已提交
161 162
		return 1;

163 164 165 166
	pinode = ACCESS_ONCE(parent->d_inode);
	if (!pinode)
		return 1;

N
Nick Piggin 已提交
167 168
	if (ncp_case_sensitive(pinode))
		return strncmp(str, name->name, len);
L
Linus Torvalds 已提交
169

N
Nick Piggin 已提交
170
	return ncp_strnicmp(NCP_IO_TABLE(pinode->i_sb), str, name->name, len);
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178
}

/*
 * This is the callback from dput() when d_count is going to 0.
 * We use this to unhash dentries with bad inodes.
 * Closing files can be safely postponed until iput() - it's done there anyway.
 */
static int
N
Nick Piggin 已提交
179
ncp_delete_dentry(const struct dentry * dentry)
L
Linus Torvalds 已提交
180 181 182 183 184 185 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 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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
{
	struct inode *inode = dentry->d_inode;

	if (inode) {
		if (is_bad_inode(inode))
			return 1;
	} else
	{
	/* N.B. Unhash negative dentries? */
	}
	return 0;
}

static inline int
ncp_single_volume(struct ncp_server *server)
{
	return (server->m.mounted_vol[0] != '\0');
}

static inline int ncp_is_server_root(struct inode *inode)
{
	return (!ncp_single_volume(NCP_SERVER(inode)) &&
		inode == inode->i_sb->s_root->d_inode);
}


/*
 * This is the callback when the dcache has a lookup hit.
 */


#ifdef CONFIG_NCPFS_STRONG
/* try to delete a readonly file (NW R bit set) */

static int
ncp_force_unlink(struct inode *dir, struct dentry* dentry)
{
        int res=0x9c,res2;
	struct nw_modify_dos_info info;
	__le32 old_nwattr;
	struct inode *inode;

	memset(&info, 0, sizeof(info));
	
        /* remove the Read-Only flag on the NW server */
	inode = dentry->d_inode;

	old_nwattr = NCP_FINFO(inode)->nwattr;
	info.attributes = old_nwattr & ~(aRONLY|aDELETEINHIBIT|aRENAMEINHIBIT);
	res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(inode), inode, NULL, DM_ATTRIBUTES, &info);
	if (res2)
		goto leave_me;

        /* now try again the delete operation */
        res = ncp_del_file_or_subdir2(NCP_SERVER(dir), dentry);

        if (res)  /* delete failed, set R bit again */
        {
		info.attributes = old_nwattr;
		res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(inode), inode, NULL, DM_ATTRIBUTES, &info);
		if (res2)
                        goto leave_me;
        }
leave_me:
        return(res);
}
#endif	/* CONFIG_NCPFS_STRONG */

#ifdef CONFIG_NCPFS_STRONG
static int
ncp_force_rename(struct inode *old_dir, struct dentry* old_dentry, char *_old_name,
                 struct inode *new_dir, struct dentry* new_dentry, char *_new_name)
{
	struct nw_modify_dos_info info;
        int res=0x90,res2;
	struct inode *old_inode = old_dentry->d_inode;
	__le32 old_nwattr = NCP_FINFO(old_inode)->nwattr;
	__le32 new_nwattr = 0; /* shut compiler warning */
	int old_nwattr_changed = 0;
	int new_nwattr_changed = 0;

	memset(&info, 0, sizeof(info));
	
        /* remove the Read-Only flag on the NW server */

	info.attributes = old_nwattr & ~(aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT);
	res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(old_inode), old_inode, NULL, DM_ATTRIBUTES, &info);
	if (!res2)
		old_nwattr_changed = 1;
	if (new_dentry && new_dentry->d_inode) {
		new_nwattr = NCP_FINFO(new_dentry->d_inode)->nwattr;
		info.attributes = new_nwattr & ~(aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT);
		res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(new_dir), new_dir, _new_name, DM_ATTRIBUTES, &info);
		if (!res2)
			new_nwattr_changed = 1;
	}
        /* now try again the rename operation */
	/* but only if something really happened */
	if (new_nwattr_changed || old_nwattr_changed) {
	        res = ncp_ren_or_mov_file_or_subdir(NCP_SERVER(old_dir),
        	                                    old_dir, _old_name,
                	                            new_dir, _new_name);
	} 
	if (res)
		goto leave_me;
	/* file was successfully renamed, so:
	   do not set attributes on old file - it no longer exists
	   copy attributes from old file to new */
	new_nwattr_changed = old_nwattr_changed;
	new_nwattr = old_nwattr;
	old_nwattr_changed = 0;
	
leave_me:;
	if (old_nwattr_changed) {
		info.attributes = old_nwattr;
		res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(old_inode), old_inode, NULL, DM_ATTRIBUTES, &info);
		/* ignore errors */
	}
	if (new_nwattr_changed)	{
		info.attributes = new_nwattr;
		res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(new_dir), new_dir, _new_name, DM_ATTRIBUTES, &info);
		/* ignore errors */
	}
        return(res);
}
#endif	/* CONFIG_NCPFS_STRONG */


static int
309
ncp_lookup_validate(struct dentry *dentry, unsigned int flags)
L
Linus Torvalds 已提交
310 311 312 313 314 315 316 317
{
	struct ncp_server *server;
	struct dentry *parent;
	struct inode *dir;
	struct ncp_entry_info finfo;
	int res, val = 0, len;
	__u8 __name[NCP_MAXPATHLEN + 1];

A
Al Viro 已提交
318 319 320
	if (dentry == dentry->d_sb->s_root)
		return 1;

321
	if (flags & LOOKUP_RCU)
322 323
		return -ECHILD;

L
Linus Torvalds 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	parent = dget_parent(dentry);
	dir = parent->d_inode;

	if (!dentry->d_inode)
		goto finished;

	server = NCP_SERVER(dir);

	/*
	 * Inspired by smbfs:
	 * The default validation is based on dentry age:
	 * We set the max age at mount time.  (But each
	 * successful server lookup renews the timestamp.)
	 */
	val = NCP_TEST_AGE(server, dentry);
	if (val)
		goto finished;

A
Al Viro 已提交
342 343
	DDPRINTK("ncp_lookup_validate: %pd2 not valid, age=%ld, server lookup\n",
		dentry, NCP_GET_AGE(dentry));
L
Linus Torvalds 已提交
344 345 346 347 348

	len = sizeof(__name);
	if (ncp_is_server_root(dir)) {
		res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
				 dentry->d_name.len, 1);
P
Petr Vandrovec 已提交
349
		if (!res) {
L
Linus Torvalds 已提交
350
			res = ncp_lookup_volume(server, __name, &(finfo.i));
P
Petr Vandrovec 已提交
351 352 353
			if (!res)
				ncp_update_known_namespace(server, finfo.i.volNumber, NULL);
		}
L
Linus Torvalds 已提交
354 355 356 357 358 359 360
	} else {
		res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
				 dentry->d_name.len, !ncp_preserve_case(dir));
		if (!res)
			res = ncp_obtain_info(server, dir, __name, &(finfo.i));
	}
	finfo.volume = finfo.i.volNumber;
A
Al Viro 已提交
361 362
	DDPRINTK("ncp_lookup_validate: looked for %pd/%s, res=%d\n",
		dentry->d_parent, __name, res);
L
Linus Torvalds 已提交
363 364 365 366 367
	/*
	 * If we didn't find it, or if it has a different dirEntNum to
	 * what we remember, it's not valid any more.
	 */
	if (!res) {
P
Petr Vandrovec 已提交
368 369 370 371
		struct inode *inode = dentry->d_inode;

		mutex_lock(&inode->i_mutex);
		if (finfo.i.dirEntNum == NCP_FINFO(inode)->dirEntNum) {
L
Linus Torvalds 已提交
372 373 374 375 376
			ncp_new_dentry(dentry);
			val=1;
		} else
			DDPRINTK("ncp_lookup_validate: found, but dirEntNum changed\n");

P
Petr Vandrovec 已提交
377 378
		ncp_update_inode2(inode, &finfo);
		mutex_unlock(&inode->i_mutex);
L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
	}

finished:
	DDPRINTK("ncp_lookup_validate: result=%d\n", val);
	dput(parent);
	return val;
}

static struct dentry *
ncp_dget_fpos(struct dentry *dentry, struct dentry *parent, unsigned long fpos)
{
	struct dentry *dent = dentry;
	struct list_head *next;

	if (d_validate(dent, parent)) {
		if (dent->d_name.len <= NCP_MAXPATHLEN &&
		    (unsigned long)dent->d_fsdata == fpos) {
			if (!dent->d_inode) {
				dput(dent);
				dent = NULL;
			}
			return dent;
		}
		dput(dent);
	}

	/* If a pointer is invalid, we search the dentry. */
N
Nick Piggin 已提交
406
	spin_lock(&parent->d_lock);
L
Linus Torvalds 已提交
407 408
	next = parent->d_subdirs.next;
	while (next != &parent->d_subdirs) {
E
Eric Dumazet 已提交
409
		dent = list_entry(next, struct dentry, d_u.d_child);
L
Linus Torvalds 已提交
410 411
		if ((unsigned long)dent->d_fsdata == fpos) {
			if (dent->d_inode)
412
				dget(dent);
L
Linus Torvalds 已提交
413 414
			else
				dent = NULL;
N
Nick Piggin 已提交
415
			spin_unlock(&parent->d_lock);
L
Linus Torvalds 已提交
416 417 418 419
			goto out;
		}
		next = next->next;
	}
N
Nick Piggin 已提交
420
	spin_unlock(&parent->d_lock);
L
Linus Torvalds 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
	return NULL;

out:
	return dent;
}

static time_t ncp_obtain_mtime(struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;
	struct ncp_server *server = NCP_SERVER(inode);
	struct nw_info_struct i;

	if (!ncp_conn_valid(server) || ncp_is_server_root(inode))
		return 0;

	if (ncp_obtain_info(server, inode, NULL, &i))
		return 0;

	return ncp_date_dos2unix(i.modifyTime, i.modifyDate);
}

A
Al Viro 已提交
442
static int ncp_readdir(struct file *file, struct dir_context *ctx)
L
Linus Torvalds 已提交
443
{
A
Al Viro 已提交
444
	struct dentry *dentry = file->f_path.dentry;
L
Linus Torvalds 已提交
445 446 447 448 449 450 451 452 453 454 455
	struct inode *inode = dentry->d_inode;
	struct page *page = NULL;
	struct ncp_server *server = NCP_SERVER(inode);
	union  ncp_dir_cache *cache = NULL;
	struct ncp_cache_control ctl;
	int result, mtime_valid = 0;
	time_t mtime = 0;

	ctl.page  = NULL;
	ctl.cache = NULL;

A
Al Viro 已提交
456
	DDPRINTK("ncp_readdir: reading %pD2, pos=%d\n", file,
A
Al Viro 已提交
457
		(int) ctx->pos);
L
Linus Torvalds 已提交
458 459

	result = -EIO;
P
Petr Vandrovec 已提交
460
	/* Do not generate '.' and '..' when server is dead. */
L
Linus Torvalds 已提交
461 462 463 464
	if (!ncp_conn_valid(server))
		goto out;

	result = 0;
A
Al Viro 已提交
465 466
	if (!dir_emit_dots(file, ctx))
		goto out;
L
Linus Torvalds 已提交
467 468 469 470 471 472 473 474 475 476 477

	page = grab_cache_page(&inode->i_data, 0);
	if (!page)
		goto read_really;

	ctl.cache = cache = kmap(page);
	ctl.head  = cache->head;

	if (!PageUptodate(page) || !ctl.head.eof)
		goto init_cache;

A
Al Viro 已提交
478
	if (ctx->pos == 2) {
L
Linus Torvalds 已提交
479 480 481 482 483 484 485 486 487
		if (jiffies - ctl.head.time >= NCP_MAX_AGE(server))
			goto init_cache;

		mtime = ncp_obtain_mtime(dentry);
		mtime_valid = 1;
		if ((!mtime) || (mtime != ctl.head.mtime))
			goto init_cache;
	}

A
Al Viro 已提交
488
	if (ctx->pos > ctl.head.end)
L
Linus Torvalds 已提交
489 490
		goto finished;

A
Al Viro 已提交
491
	ctl.fpos = ctx->pos + (NCP_DIRCACHE_START - 2);
L
Linus Torvalds 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505
	ctl.ofs  = ctl.fpos / NCP_DIRCACHE_SIZE;
	ctl.idx  = ctl.fpos % NCP_DIRCACHE_SIZE;

	for (;;) {
		if (ctl.ofs != 0) {
			ctl.page = find_lock_page(&inode->i_data, ctl.ofs);
			if (!ctl.page)
				goto invalid_cache;
			ctl.cache = kmap(ctl.page);
			if (!PageUptodate(ctl.page))
				goto invalid_cache;
		}
		while (ctl.idx < NCP_DIRCACHE_SIZE) {
			struct dentry *dent;
A
Al Viro 已提交
506
			bool over;
L
Linus Torvalds 已提交
507 508

			dent = ncp_dget_fpos(ctl.cache->dentry[ctl.idx],
A
Al Viro 已提交
509
						dentry, ctx->pos);
L
Linus Torvalds 已提交
510 511
			if (!dent)
				goto invalid_cache;
A
Al Viro 已提交
512 513
			over = !dir_emit(ctx, dent->d_name.name,
					dent->d_name.len,
L
Linus Torvalds 已提交
514 515
					dent->d_inode->i_ino, DT_UNKNOWN);
			dput(dent);
A
Al Viro 已提交
516
			if (over)
L
Linus Torvalds 已提交
517
				goto finished;
A
Al Viro 已提交
518
			ctx->pos += 1;
L
Linus Torvalds 已提交
519
			ctl.idx += 1;
A
Al Viro 已提交
520
			if (ctx->pos > ctl.head.end)
L
Linus Torvalds 已提交
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
				goto finished;
		}
		if (ctl.page) {
			kunmap(ctl.page);
			SetPageUptodate(ctl.page);
			unlock_page(ctl.page);
			page_cache_release(ctl.page);
			ctl.page = NULL;
		}
		ctl.idx  = 0;
		ctl.ofs += 1;
	}
invalid_cache:
	if (ctl.page) {
		kunmap(ctl.page);
		unlock_page(ctl.page);
		page_cache_release(ctl.page);
		ctl.page = NULL;
	}
	ctl.cache = cache;
init_cache:
	ncp_invalidate_dircache_entries(dentry);
	if (!mtime_valid) {
		mtime = ncp_obtain_mtime(dentry);
		mtime_valid = 1;
	}
	ctl.head.mtime = mtime;
	ctl.head.time = jiffies;
	ctl.head.eof = 0;
	ctl.fpos = 2;
	ctl.ofs = 0;
	ctl.idx = NCP_DIRCACHE_START;
	ctl.filled = 0;
	ctl.valid  = 1;
read_really:
	if (ncp_is_server_root(inode)) {
A
Al Viro 已提交
557
		ncp_read_volume_list(file, ctx, &ctl);
L
Linus Torvalds 已提交
558
	} else {
A
Al Viro 已提交
559
		ncp_do_readdir(file, ctx, &ctl);
L
Linus Torvalds 已提交
560 561 562 563
	}
	ctl.head.end = ctl.fpos - 1;
	ctl.head.eof = ctl.valid;
finished:
P
Petr Vandrovec 已提交
564 565 566 567 568 569
	if (ctl.page) {
		kunmap(ctl.page);
		SetPageUptodate(ctl.page);
		unlock_page(ctl.page);
		page_cache_release(ctl.page);
	}
L
Linus Torvalds 已提交
570 571 572 573 574 575 576 577 578 579 580 581
	if (page) {
		cache->head = ctl.head;
		kunmap(page);
		SetPageUptodate(page);
		unlock_page(page);
		page_cache_release(page);
	}
out:
	return result;
}

static int
A
Al Viro 已提交
582
ncp_fill_cache(struct file *file, struct dir_context *ctx,
P
Petr Vandrovec 已提交
583 584
		struct ncp_cache_control *ctrl, struct ncp_entry_info *entry,
		int inval_childs)
L
Linus Torvalds 已提交
585
{
A
Al Viro 已提交
586
	struct dentry *newdent, *dentry = file->f_path.dentry;
P
Petr Vandrovec 已提交
587
	struct inode *dir = dentry->d_inode;
L
Linus Torvalds 已提交
588 589 590 591 592 593 594 595
	struct ncp_cache_control ctl = *ctrl;
	struct qstr qname;
	int valid = 0;
	int hashed = 0;
	ino_t ino = 0;
	__u8 __name[NCP_MAXPATHLEN + 1];

	qname.len = sizeof(__name);
P
Petr Vandrovec 已提交
596
	if (ncp_vol2io(NCP_SERVER(dir), __name, &qname.len,
L
Linus Torvalds 已提交
597
			entry->i.entryName, entry->i.nameLen,
P
Petr Vandrovec 已提交
598
			!ncp_preserve_entry_case(dir, entry->i.NSCreator)))
L
Linus Torvalds 已提交
599 600 601 602
		return 1; /* I'm not sure */

	qname.name = __name;

603 604 605
	newdent = d_hash_and_lookup(dentry, &qname);
	if (unlikely(IS_ERR(newdent)))
		goto end_advance;
L
Linus Torvalds 已提交
606 607 608 609 610 611
	if (!newdent) {
		newdent = d_alloc(dentry, &qname);
		if (!newdent)
			goto end_advance;
	} else {
		hashed = 1;
P
Petr Vandrovec 已提交
612 613 614 615 616 617 618 619

		/* If case sensitivity changed for this volume, all entries below this one
		   should be thrown away.  This entry itself is not affected, as its case
		   sensitivity is controlled by its own parent. */
		if (inval_childs)
			shrink_dcache_parent(newdent);

		/*
N
Nick Piggin 已提交
620 621 622 623
		 * NetWare's OS2 namespace is case preserving yet case
		 * insensitive.  So we update dentry's name as received from
		 * server. Parent dir's i_mutex is locked because we're in
		 * readdir.
P
Petr Vandrovec 已提交
624
		 */
N
Nick Piggin 已提交
625
		dentry_update_name_case(newdent, &qname);
L
Linus Torvalds 已提交
626 627 628
	}

	if (!newdent->d_inode) {
P
Petr Vandrovec 已提交
629 630
		struct inode *inode;

L
Linus Torvalds 已提交
631
		entry->opened = 0;
P
Petr Vandrovec 已提交
632 633 634 635
		entry->ino = iunique(dir->i_sb, 2);
		inode = ncp_iget(dir->i_sb, entry);
		if (inode) {
			d_instantiate(newdent, inode);
L
Linus Torvalds 已提交
636 637 638
			if (!hashed)
				d_rehash(newdent);
		}
P
Petr Vandrovec 已提交
639 640 641
	} else {
		struct inode *inode = newdent->d_inode;

N
Nick Piggin 已提交
642
		mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
P
Petr Vandrovec 已提交
643 644 645
		ncp_update_inode2(inode, entry);
		mutex_unlock(&inode->i_mutex);
	}
L
Linus Torvalds 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662

	if (newdent->d_inode) {
		ino = newdent->d_inode->i_ino;
		newdent->d_fsdata = (void *) ctl.fpos;
		ncp_new_dentry(newdent);
	}

	if (ctl.idx >= NCP_DIRCACHE_SIZE) {
		if (ctl.page) {
			kunmap(ctl.page);
			SetPageUptodate(ctl.page);
			unlock_page(ctl.page);
			page_cache_release(ctl.page);
		}
		ctl.cache = NULL;
		ctl.idx  -= NCP_DIRCACHE_SIZE;
		ctl.ofs  += 1;
P
Petr Vandrovec 已提交
663
		ctl.page  = grab_cache_page(&dir->i_data, ctl.ofs);
L
Linus Torvalds 已提交
664 665 666 667 668 669 670 671 672 673 674
		if (ctl.page)
			ctl.cache = kmap(ctl.page);
	}
	if (ctl.cache) {
		ctl.cache->dentry[ctl.idx] = newdent;
		valid = 1;
	}
	dput(newdent);
end_advance:
	if (!valid)
		ctl.valid = 0;
A
Al Viro 已提交
675
	if (!ctl.filled && (ctl.fpos == ctx->pos)) {
L
Linus Torvalds 已提交
676
		if (!ino)
P
Petr Vandrovec 已提交
677
			ino = iunique(dir->i_sb, 2);
A
Al Viro 已提交
678 679
		ctl.filled = !dir_emit(ctx, qname.name, qname.len,
				     ino, DT_UNKNOWN);
L
Linus Torvalds 已提交
680
		if (!ctl.filled)
A
Al Viro 已提交
681
			ctx->pos += 1;
L
Linus Torvalds 已提交
682 683 684 685 686 687 688 689
	}
	ctl.fpos += 1;
	ctl.idx  += 1;
	*ctrl = ctl;
	return (ctl.valid || !ctl.filled);
}

static void
A
Al Viro 已提交
690
ncp_read_volume_list(struct file *file, struct dir_context *ctx,
L
Linus Torvalds 已提交
691 692
			struct ncp_cache_control *ctl)
{
A
Al Viro 已提交
693
	struct dentry *dentry = file->f_path.dentry;
L
Linus Torvalds 已提交
694 695 696 697 698 699 700
	struct inode *inode = dentry->d_inode;
	struct ncp_server *server = NCP_SERVER(inode);
	struct ncp_volume_info info;
	struct ncp_entry_info entry;
	int i;

	DPRINTK("ncp_read_volume_list: pos=%ld\n",
A
Al Viro 已提交
701
			(unsigned long) ctx->pos);
L
Linus Torvalds 已提交
702 703

	for (i = 0; i < NCP_NUMBER_OF_VOLUMES; i++) {
P
Petr Vandrovec 已提交
704
		int inval_dentry;
L
Linus Torvalds 已提交
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

		if (ncp_get_volume_info_with_number(server, i, &info) != 0)
			return;
		if (!strlen(info.volume_name))
			continue;

		DPRINTK("ncp_read_volume_list: found vol: %s\n",
			info.volume_name);

		if (ncp_lookup_volume(server, info.volume_name,
					&entry.i)) {
			DPRINTK("ncpfs: could not lookup vol %s\n",
				info.volume_name);
			continue;
		}
P
Petr Vandrovec 已提交
720
		inval_dentry = ncp_update_known_namespace(server, entry.i.volNumber, NULL);
L
Linus Torvalds 已提交
721
		entry.volume = entry.i.volNumber;
A
Al Viro 已提交
722
		if (!ncp_fill_cache(file, ctx, ctl, &entry, inval_dentry))
L
Linus Torvalds 已提交
723 724 725 726 727
			return;
	}
}

static void
A
Al Viro 已提交
728
ncp_do_readdir(struct file *file, struct dir_context *ctx,
L
Linus Torvalds 已提交
729 730
						struct ncp_cache_control *ctl)
{
A
Al Viro 已提交
731
	struct dentry *dentry = file->f_path.dentry;
L
Linus Torvalds 已提交
732 733 734 735 736 737 738 739 740
	struct inode *dir = dentry->d_inode;
	struct ncp_server *server = NCP_SERVER(dir);
	struct nw_search_sequence seq;
	struct ncp_entry_info entry;
	int err;
	void* buf;
	int more;
	size_t bufsize;

A
Al Viro 已提交
741
	DPRINTK("ncp_do_readdir: %pD2, fpos=%ld\n", file,
A
Al Viro 已提交
742
		(unsigned long) ctx->pos);
A
Al Viro 已提交
743 744
	PPRINTK("ncp_do_readdir: init %pD, volnum=%d, dirent=%u\n",
		file, NCP_FINFO(dir)->volNumber, NCP_FINFO(dir)->dirEntNum);
L
Linus Torvalds 已提交
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

	err = ncp_initialize_search(server, dir, &seq);
	if (err) {
		DPRINTK("ncp_do_readdir: init failed, err=%d\n", err);
		return;
	}
	/* We MUST NOT use server->buffer_size handshaked with server if we are
	   using UDP, as for UDP server uses max. buffer size determined by
	   MTU, and for TCP server uses hardwired value 65KB (== 66560 bytes). 
	   So we use 128KB, just to be sure, as there is no way how to know
	   this value in advance. */
	bufsize = 131072;
	buf = vmalloc(bufsize);
	if (!buf)
		return;
	do {
		int cnt;
		char* rpl;
		size_t rpls;

		err = ncp_search_for_fileset(server, &seq, &more, &cnt, buf, bufsize, &rpl, &rpls);
		if (err)		/* Error */
			break;
		if (!cnt)		/* prevent endless loop */
			break;
		while (cnt--) {
			size_t onerpl;
			
			if (rpls < offsetof(struct nw_info_struct, entryName))
				break;	/* short packet */
			ncp_extract_file_info(rpl, &entry.i);
			onerpl = offsetof(struct nw_info_struct, entryName) + entry.i.nameLen;
			if (rpls < onerpl)
				break;	/* short packet */
			(void)ncp_obtain_nfs_info(server, &entry.i);
			rpl += onerpl;
			rpls -= onerpl;
			entry.volume = entry.i.volNumber;
A
Al Viro 已提交
783
			if (!ncp_fill_cache(file, ctx, ctl, &entry, 0))
L
Linus Torvalds 已提交
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
				break;
		}
	} while (more);
	vfree(buf);
	return;
}

int ncp_conn_logged_in(struct super_block *sb)
{
	struct ncp_server* server = NCP_SBP(sb);
	int result;

	if (ncp_single_volume(server)) {
		int len;
		struct dentry* dent;
		__u32 volNumber;
		__le32 dirEntNum;
		__le32 DosDirNum;
		__u8 __name[NCP_MAXPATHLEN + 1];

		len = sizeof(__name);
		result = ncp_io2vol(server, __name, &len, server->m.mounted_vol,
				    strlen(server->m.mounted_vol), 1);
		if (result)
			goto out;
		result = -ENOENT;
		if (ncp_get_volume_root(server, __name, &volNumber, &dirEntNum, &DosDirNum)) {
			PPRINTK("ncp_conn_logged_in: %s not found\n",
				server->m.mounted_vol);
			goto out;
		}
		dent = sb->s_root;
		if (dent) {
			struct inode* ino = dent->d_inode;
			if (ino) {
P
Petr Vandrovec 已提交
819
				ncp_update_known_namespace(server, volNumber, NULL);
L
Linus Torvalds 已提交
820 821 822
				NCP_FINFO(ino)->volNumber = volNumber;
				NCP_FINFO(ino)->dirEntNum = dirEntNum;
				NCP_FINFO(ino)->DosDirNum = DosDirNum;
P
Petr Vandrovec 已提交
823
				result = 0;
L
Linus Torvalds 已提交
824 825 826 827 828 829
			} else {
				DPRINTK("ncpfs: sb->s_root->d_inode == NULL!\n");
			}
		} else {
			DPRINTK("ncpfs: sb->s_root == NULL!\n");
		}
P
Petr Vandrovec 已提交
830 831
	} else
		result = 0;
L
Linus Torvalds 已提交
832 833 834 835 836

out:
	return result;
}

A
Al Viro 已提交
837
static struct dentry *ncp_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
L
Linus Torvalds 已提交
838 839 840 841 842 843 844 845 846 847 848
{
	struct ncp_server *server = NCP_SERVER(dir);
	struct inode *inode = NULL;
	struct ncp_entry_info finfo;
	int error, res, len;
	__u8 __name[NCP_MAXPATHLEN + 1];

	error = -EIO;
	if (!ncp_conn_valid(server))
		goto finished;

A
Al Viro 已提交
849
	PPRINTK("ncp_lookup: server lookup for %pd2\n", dentry);
L
Linus Torvalds 已提交
850 851 852 853 854 855 856

	len = sizeof(__name);
	if (ncp_is_server_root(dir)) {
		res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
				 dentry->d_name.len, 1);
		if (!res)
			res = ncp_lookup_volume(server, __name, &(finfo.i));
P
Petr Vandrovec 已提交
857 858
			if (!res)
				ncp_update_known_namespace(server, finfo.i.volNumber, NULL);
L
Linus Torvalds 已提交
859 860 861 862 863 864
	} else {
		res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
				 dentry->d_name.len, !ncp_preserve_case(dir));
		if (!res)
			res = ncp_obtain_info(server, dir, __name, &(finfo.i));
	}
A
Al Viro 已提交
865
	PPRINTK("ncp_lookup: looked for %pd2, res=%d\n", dentry, res);
L
Linus Torvalds 已提交
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 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
	/*
	 * If we didn't find an entry, make a negative dentry.
	 */
	if (res)
		goto add_entry;

	/*
	 * Create an inode for the entry.
	 */
	finfo.opened = 0;
	finfo.ino = iunique(dir->i_sb, 2);
	finfo.volume = finfo.i.volNumber;
	error = -EACCES;
	inode = ncp_iget(dir->i_sb, &finfo);

	if (inode) {
		ncp_new_dentry(dentry);
add_entry:
		d_add(dentry, inode);
		error = 0;
	}

finished:
	PPRINTK("ncp_lookup: result=%d\n", error);
	return ERR_PTR(error);
}

/*
 * This code is common to create, mkdir, and mknod.
 */
static int ncp_instantiate(struct inode *dir, struct dentry *dentry,
			struct ncp_entry_info *finfo)
{
	struct inode *inode;
	int error = -EINVAL;

	finfo->ino = iunique(dir->i_sb, 2);
	inode = ncp_iget(dir->i_sb, finfo);
	if (!inode)
		goto out_close;
	d_instantiate(dentry,inode);
	error = 0;
out:
	return error;

out_close:
A
Al Viro 已提交
912
	PPRINTK("ncp_instantiate: %pd2 failed, closing file\n", dentry);
L
Linus Torvalds 已提交
913 914 915 916
	ncp_close_file(NCP_SERVER(dir), finfo->file_handle);
	goto out;
}

A
Al Viro 已提交
917
int ncp_create_new(struct inode *dir, struct dentry *dentry, umode_t mode,
L
Linus Torvalds 已提交
918 919 920 921 922 923 924 925
		   dev_t rdev, __le32 attributes)
{
	struct ncp_server *server = NCP_SERVER(dir);
	struct ncp_entry_info finfo;
	int error, result, len;
	int opmode;
	__u8 __name[NCP_MAXPATHLEN + 1];
	
A
Al Viro 已提交
926
	PPRINTK("ncp_create_new: creating %pd2, mode=%hx\n", dentry, mode);
L
Linus Torvalds 已提交
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952

	ncp_age_dentry(server, dentry);
	len = sizeof(__name);
	error = ncp_io2vol(server, __name, &len, dentry->d_name.name,
			   dentry->d_name.len, !ncp_preserve_case(dir));
	if (error)
		goto out;

	error = -EACCES;
	
	if (S_ISREG(mode) && 
	    (server->m.flags & NCP_MOUNT_EXTRAS) && 
	    (mode & S_IXUGO))
		attributes |= aSYSTEM | aSHARED;
	
	result = ncp_open_create_file_or_subdir(server, dir, __name,
				OC_MODE_CREATE | OC_MODE_OPEN | OC_MODE_REPLACE,
				attributes, AR_READ | AR_WRITE, &finfo);
	opmode = O_RDWR;
	if (result) {
		result = ncp_open_create_file_or_subdir(server, dir, __name,
				OC_MODE_CREATE | OC_MODE_OPEN | OC_MODE_REPLACE,
				attributes, AR_WRITE, &finfo);
		if (result) {
			if (result == 0x87)
				error = -ENAMETOOLONG;
P
Petr Vandrovec 已提交
953 954
			else if (result < 0)
				error = result;
A
Al Viro 已提交
955
			DPRINTK("ncp_create: %pd2 failed\n", dentry);
L
Linus Torvalds 已提交
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
			goto out;
		}
		opmode = O_WRONLY;
	}
	finfo.access = opmode;
	if (ncp_is_nfs_extras(server, finfo.volume)) {
		finfo.i.nfs.mode = mode;
		finfo.i.nfs.rdev = new_encode_dev(rdev);
		if (ncp_modify_nfs_info(server, finfo.volume,
					finfo.i.dirEntNum,
					mode, new_encode_dev(rdev)) != 0)
			goto out;
	}

	error = ncp_instantiate(dir, dentry, &finfo);
out:
	return error;
}

A
Al Viro 已提交
975
static int ncp_create(struct inode *dir, struct dentry *dentry, umode_t mode,
A
Al Viro 已提交
976
		bool excl)
L
Linus Torvalds 已提交
977 978 979 980
{
	return ncp_create_new(dir, dentry, mode, 0, 0);
}

981
static int ncp_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
L
Linus Torvalds 已提交
982 983 984 985 986 987
{
	struct ncp_entry_info finfo;
	struct ncp_server *server = NCP_SERVER(dir);
	int error, len;
	__u8 __name[NCP_MAXPATHLEN + 1];

A
Al Viro 已提交
988
	DPRINTK("ncp_mkdir: making %pd2\n", dentry);
L
Linus Torvalds 已提交
989 990 991 992 993 994 995 996

	ncp_age_dentry(server, dentry);
	len = sizeof(__name);
	error = ncp_io2vol(server, __name, &len, dentry->d_name.name,
			   dentry->d_name.len, !ncp_preserve_case(dir));
	if (error)
		goto out;

P
Petr Vandrovec 已提交
997
	error = ncp_open_create_file_or_subdir(server, dir, __name,
L
Linus Torvalds 已提交
998 999
					   OC_MODE_CREATE, aDIR,
					   cpu_to_le16(0xffff),
P
Petr Vandrovec 已提交
1000 1001
					   &finfo);
	if (error == 0) {
L
Linus Torvalds 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
		if (ncp_is_nfs_extras(server, finfo.volume)) {
			mode |= S_IFDIR;
			finfo.i.nfs.mode = mode;
			if (ncp_modify_nfs_info(server,
						finfo.volume,
						finfo.i.dirEntNum,
						mode, 0) != 0)
				goto out;
		}
		error = ncp_instantiate(dir, dentry, &finfo);
P
Petr Vandrovec 已提交
1012 1013
	} else if (error > 0) {
		error = -EACCES;
L
Linus Torvalds 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	}
out:
	return error;
}

static int ncp_rmdir(struct inode *dir, struct dentry *dentry)
{
	struct ncp_server *server = NCP_SERVER(dir);
	int error, result, len;
	__u8 __name[NCP_MAXPATHLEN + 1];

A
Al Viro 已提交
1025
	DPRINTK("ncp_rmdir: removing %pd2\n", dentry);
L
Linus Torvalds 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055

	len = sizeof(__name);
	error = ncp_io2vol(server, __name, &len, dentry->d_name.name,
			   dentry->d_name.len, !ncp_preserve_case(dir));
	if (error)
		goto out;

	result = ncp_del_file_or_subdir(server, dir, __name);
	switch (result) {
		case 0x00:
			error = 0;
			break;
		case 0x85:	/* unauthorized to delete file */
		case 0x8A:	/* unauthorized to delete file */
			error = -EACCES;
			break;
		case 0x8F:
		case 0x90:	/* read only */
			error = -EPERM;
			break;
		case 0x9F:	/* in use by another client */
			error = -EBUSY;
			break;
		case 0xA0:	/* directory not empty */
			error = -ENOTEMPTY;
			break;
		case 0xFF:	/* someone deleted file */
			error = -ENOENT;
			break;
		default:
P
Petr Vandrovec 已提交
1056
			error = result < 0 ? result : -EACCES;
L
Linus Torvalds 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
			break;
       	}
out:
	return error;
}

static int ncp_unlink(struct inode *dir, struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;
	struct ncp_server *server;
	int error;

	server = NCP_SERVER(dir);
A
Al Viro 已提交
1070
	DPRINTK("ncp_unlink: unlinking %pd2\n", dentry);
L
Linus Torvalds 已提交
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
	
	/*
	 * Check whether to close the file ...
	 */
	if (inode) {
		PPRINTK("ncp_unlink: closing file\n");
		ncp_make_closed(inode);
	}

	error = ncp_del_file_or_subdir2(server, dentry);
#ifdef CONFIG_NCPFS_STRONG
	/* 9C is Invalid path.. It should be 8F, 90 - read only, but
	   it is not :-( */
	if ((error == 0x9C || error == 0x90) && server->m.flags & NCP_MOUNT_STRONG) { /* R/O */
		error = ncp_force_unlink(dir, dentry);
	}
#endif
	switch (error) {
		case 0x00:
A
Al Viro 已提交
1090
			DPRINTK("ncp: removed %pd2\n", dentry);
L
Linus Torvalds 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
			break;
		case 0x85:
		case 0x8A:
			error = -EACCES;
			break;
		case 0x8D:	/* some files in use */
		case 0x8E:	/* all files in use */
			error = -EBUSY;
			break;
		case 0x8F:	/* some read only */
		case 0x90:	/* all read only */
		case 0x9C:	/* !!! returned when in-use or read-only by NW4 */
			error = -EPERM;
			break;
		case 0xFF:
			error = -ENOENT;
			break;
		default:
P
Petr Vandrovec 已提交
1109
			error = error < 0 ? error : -EACCES;
L
Linus Torvalds 已提交
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
			break;
	}
	return error;
}

static int ncp_rename(struct inode *old_dir, struct dentry *old_dentry,
		      struct inode *new_dir, struct dentry *new_dentry)
{
	struct ncp_server *server = NCP_SERVER(old_dir);
	int error;
	int old_len, new_len;
	__u8 __old_name[NCP_MAXPATHLEN + 1], __new_name[NCP_MAXPATHLEN + 1];

A
Al Viro 已提交
1123
	DPRINTK("ncp_rename: %pd2 to %pd2\n", old_dentry, new_dentry);
L
Linus Torvalds 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152

	ncp_age_dentry(server, old_dentry);
	ncp_age_dentry(server, new_dentry);

	old_len = sizeof(__old_name);
	error = ncp_io2vol(server, __old_name, &old_len,
			   old_dentry->d_name.name, old_dentry->d_name.len,
			   !ncp_preserve_case(old_dir));
	if (error)
		goto out;

	new_len = sizeof(__new_name);
	error = ncp_io2vol(server, __new_name, &new_len,
			   new_dentry->d_name.name, new_dentry->d_name.len,
			   !ncp_preserve_case(new_dir));
	if (error)
		goto out;

	error = ncp_ren_or_mov_file_or_subdir(server, old_dir, __old_name,
						      new_dir, __new_name);
#ifdef CONFIG_NCPFS_STRONG
	if ((error == 0x90 || error == 0x8B || error == -EACCES) &&
			server->m.flags & NCP_MOUNT_STRONG) {	/* RO */
		error = ncp_force_rename(old_dir, old_dentry, __old_name,
					 new_dir, new_dentry, __new_name);
	}
#endif
	switch (error) {
		case 0x00:
A
Al Viro 已提交
1153 1154
               	        DPRINTK("ncp renamed %pd -> %pd.\n",
                                old_dentry, new_dentry);
L
Linus Torvalds 已提交
1155 1156 1157 1158 1159 1160 1161 1162
			break;
		case 0x9E:
			error = -ENAMETOOLONG;
			break;
		case 0xFF:
			error = -ENOENT;
			break;
		default:
P
Petr Vandrovec 已提交
1163
			error = error < 0 ? error : -EACCES;
L
Linus Torvalds 已提交
1164 1165 1166 1167 1168 1169 1170
			break;
	}
out:
	return error;
}

static int ncp_mknod(struct inode * dir, struct dentry *dentry,
A
Al Viro 已提交
1171
		     umode_t mode, dev_t rdev)
L
Linus Torvalds 已提交
1172 1173 1174 1175
{
	if (!new_valid_dev(rdev))
		return -EINVAL;
	if (ncp_is_nfs_extras(NCP_SERVER(dir), NCP_FINFO(dir)->volNumber)) {
A
Al Viro 已提交
1176
		DPRINTK(KERN_DEBUG "ncp_mknod: mode = 0%ho\n", mode);
L
Linus Torvalds 已提交
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
		return ncp_create_new(dir, dentry, mode, rdev, 0);
	}
	return -EPERM; /* Strange, but true */
}

/* The following routines are taken directly from msdos-fs */

/* Linear day numbers of the respective 1sts in non-leap years. */

static int day_n[] =
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0};
/* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */


extern struct timezone sys_tz;

static int utc2local(int time)
{
	return time - sys_tz.tz_minuteswest * 60;
}

static int local2utc(int time)
{
	return time + sys_tz.tz_minuteswest * 60;
}

/* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
int
ncp_date_dos2unix(__le16 t, __le16 d)
{
	unsigned short time = le16_to_cpu(t), date = le16_to_cpu(d);
	int month, year, secs;

	/* first subtract and mask after that... Otherwise, if
	   date == 0, bad things happen */
	month = ((date >> 5) - 1) & 15;
	year = date >> 9;
	secs = (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 +
		86400 * ((date & 31) - 1 + day_n[month] + (year / 4) + 
		year * 365 - ((year & 3) == 0 && month < 2 ? 1 : 0) + 3653);
	/* days since 1.1.70 plus 80's leap day */
	return local2utc(secs);
}


/* Convert linear UNIX date to a MS-DOS time/date pair. */
void
ncp_date_unix2dos(int unix_date, __le16 *time, __le16 *date)
{
	int day, year, nl_day, month;

	unix_date = utc2local(unix_date);
	*time = cpu_to_le16(
		(unix_date % 60) / 2 + (((unix_date / 60) % 60) << 5) +
		(((unix_date / 3600) % 24) << 11));
	day = unix_date / 86400 - 3652;
	year = day / 365;
	if ((year + 3) / 4 + 365 * year > day)
		year--;
	day -= (year + 3) / 4 + 365 * year;
	if (day == 59 && !(year & 3)) {
		nl_day = day;
		month = 2;
	} else {
		nl_day = (year & 3) || day <= 59 ? day : day - 1;
R
Roel Kluin 已提交
1242
		for (month = 1; month < 12; month++)
L
Linus Torvalds 已提交
1243 1244 1245 1246 1247
			if (day_n[month] > nl_day)
				break;
	}
	*date = cpu_to_le16(nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9));
}