Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 52 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 82 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 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 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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 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 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 434 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 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 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 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 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 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 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 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 1017 1018 1019 1020 1021 1022 1023 1024 1025 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 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 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 | /*
* 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 Wolfram Pienkoss for NLS
* Modified 1999 Wolfram Pienkoss for directory caching
*
*/
#include <linux/config.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/stat.h>
#include <linux/kernel.h>
#include <linux/malloc.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>
#include <asm/uaccess.h>
#include <asm/byteorder.h>
#include <linux/locks.h>
#include <linux/ncp_fs.h>
#include "ncplib_kernel.h"
static int ncp_read_volume_list(struct file *, void *, filldir_t);
static int ncp_do_readdir(struct file *, void *, filldir_t);
static ssize_t ncp_dir_read(struct file *, char *, size_t, loff_t *);
static int ncp_readdir(struct file *, void *, filldir_t);
static int ncp_create(struct inode *, struct dentry *, int);
static struct dentry *ncp_lookup(struct inode *, struct dentry *);
static int ncp_unlink(struct inode *, struct dentry *);
static int ncp_mkdir(struct inode *, struct dentry *, int);
static int ncp_rmdir(struct inode *, struct dentry *);
static int ncp_rename(struct inode *, struct dentry *,
struct inode *, struct dentry *);
#ifdef CONFIG_NCPFS_EXTRAS
extern int ncp_symlink(struct inode *, struct dentry *, const char *);
#endif
static struct file_operations ncp_dir_operations =
{
NULL, /* lseek - default */
ncp_dir_read, /* read - bad */
NULL, /* write - bad */
ncp_readdir, /* readdir */
NULL, /* poll - default */
ncp_ioctl, /* ioctl */
NULL, /* mmap */
NULL, /* no special open code */
NULL, /* flush */
NULL, /* no special release code */
NULL /* fsync */
};
struct inode_operations ncp_dir_inode_operations =
{
&ncp_dir_operations, /* default directory file ops */
ncp_create, /* create */
ncp_lookup, /* lookup */
NULL, /* link */
ncp_unlink, /* unlink */
#ifdef CONFIG_NCPFS_EXTRAS
ncp_symlink, /* symlink */
#else
NULL, /* symlink */
#endif
ncp_mkdir, /* mkdir */
ncp_rmdir, /* rmdir */
NULL, /* mknod */
ncp_rename, /* rename */
NULL, /* readlink */
NULL, /* follow link */
NULL, /* get_block */
NULL, /* readpage */
NULL, /* writepage */
NULL, /* flushpage */
NULL, /* truncate */
NULL, /* permission */
NULL, /* smap */
NULL, /* revalidate */
};
static ssize_t
ncp_dir_read(struct file *filp, char *buf, size_t count, loff_t *ppos)
{
return -EISDIR;
}
/*
* Dentry operations routines
*/
static int ncp_lookup_validate(struct dentry *, int);
static int ncp_hash_dentry(struct dentry *, struct qstr *);
static int ncp_compare_dentry (struct dentry *, struct qstr *, struct qstr *);
static void ncp_delete_dentry(struct dentry *);
struct dentry_operations ncp_dentry_operations =
{
ncp_lookup_validate, /* d_revalidate(struct dentry *, int) */
ncp_hash_dentry, /* d_hash */
ncp_compare_dentry, /* d_compare */
ncp_delete_dentry /* d_delete(struct dentry *) */
};
/*
* XXX: It would be better to use the tolower from linux/ctype.h,
* but _ctype is needed and it is not exported.
*/
#define tolower(c) (((c) >= 'A' && (c) <= 'Z') ? (c)-('A'-'a') : (c))
/*
* Note: leave the hash unchanged if the directory
* is case-sensitive.
*/
static int
ncp_hash_dentry(struct dentry *dentry, struct qstr *this)
{
unsigned long hash;
int i;
if (!ncp_case_sensitive(dentry->d_inode)) {
hash = init_name_hash();
for (i=0; i<this->len ; i++)
hash = partial_name_hash(tolower(this->name[i]),hash);
this->hash = end_name_hash(hash);
}
return 0;
}
static int
ncp_compare_dentry(struct dentry *dentry, struct qstr *a, struct qstr *b)
{
int i;
if (a->len != b->len) return 1;
if (ncp_case_sensitive(dentry->d_inode))
return strncmp(a->name, b->name, a->len);
for (i=0; i<a->len; i++)
if (tolower(a->name[i]) != tolower(b->name[i]))
return 1;
return 0;
}
/*
* This is the callback from dput() when d_count is going to 0.
* We use this to unhash dentries with bad inodes and close files.
*/
static void
ncp_delete_dentry(struct dentry * dentry)
{
struct inode *inode = dentry->d_inode;
if (inode)
{
if (is_bad_inode(inode))
{
d_drop(dentry);
}
/*
* Lock the superblock, then recheck the dentry count.
* (Somebody might have used it again ...)
*/
if (dentry->d_count == 1 && NCP_FINFO(inode)->opened) {
#ifdef NCPFS_PARANOIA
printk(KERN_DEBUG "ncp_delete_dentry: closing file %s/%s\n",
dentry->d_parent->d_name.name, dentry->d_name.name);
#endif
ncp_make_closed(inode);
}
} else
{
/* N.B. Unhash negative dentries? */
}
}
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;
__u32 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;
__u32 old_nwattr = NCP_FINFO(old_inode)->nwattr;
__u32 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
ncp_lookup_validate(struct dentry * dentry, int flags)
{
struct ncp_server *server;
struct inode *dir = dentry->d_parent->d_inode;
int res, val = 0;
int len = dentry->d_name.len;
struct ncp_entry_info finfo;
__u8 __name[dentry->d_name.len + 1];
if (!dentry->d_inode || !dir)
goto finished;
server = NCP_SERVER(dir);
if (!ncp_conn_valid(server))
goto finished;
/*
* 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;
#ifdef NCPFS_PARANOIA
printk(KERN_DEBUG "ncp_lookup_validate: %s/%s not valid, age=%ld\n",
dentry->d_parent->d_name.name, dentry->d_name.name,
NCP_GET_AGE(dentry));
#endif
memcpy(__name, dentry->d_name.name, len);
__name[len] = '\0';
#ifdef NCPFS_PARANOIA
printk(KERN_DEBUG "ncp_lookup_validate: %s, len %d\n", __name, len);
printk(KERN_DEBUG "ncp_lookup_validate: server lookup for %s/%s\n",
dentry->d_parent->d_name.name, __name);
#endif
if (ncp_is_server_root(dir)) {
io2vol(server, __name, 1);
res = ncp_lookup_volume(server, __name, &(finfo.i));
} else {
io2vol(server, __name, !ncp_preserve_case(dir));
res = ncp_obtain_info(server, dir, __name, &(finfo.i));
}
#ifdef NCPFS_PARANOIA
printk(KERN_DEBUG "ncp_lookup_validate: looked for %s/%s, res=%d\n",
dentry->d_parent->d_name.name, __name, res);
#endif
/*
* 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) {
if (finfo.i.dirEntNum == NCP_FINFO(dentry->d_inode)->dirEntNum)
val=1;
#ifdef NCPFS_PARANOIA
else
printk(KERN_DEBUG "ncp_lookup_validate: found, but dirEntNum changed\n");
#endif
vol2io(server, finfo.i.entryName,
!ncp_preserve_entry_case(dir, finfo.i.NSCreator));
ncp_update_inode2(dentry->d_inode, &finfo);
ncp_new_dentry(dentry);
}
finished:
#ifdef NCPFS_PARANOIA
printk(KERN_DEBUG "ncp_lookup_validate: result=%d\n", val);
#endif
return val;
}
static int ncp_readdir(struct file *filp, void *dirent, filldir_t filldir)
{
struct dentry *dentry = filp->f_dentry;
struct inode *inode = dentry->d_inode;
struct ncp_server *server = NCP_SERVER(inode);
int entries, result;
DDPRINTK(KERN_DEBUG "ncp_readdir: reading %s/%s, pos=%d\n",
dentry->d_parent->d_name.name, dentry->d_name.name,
(int) filp->f_pos);
result = -EIO;
if (!ncp_conn_valid(server))
goto finished;
result = 0;
if (filp->f_pos == 0) {
if (filldir(dirent, ".", 1, 0, inode->i_ino))
goto finished;
filp->f_pos = 1;
}
if (filp->f_pos == 1) {
if (filldir(dirent, "..", 2, 1,
dentry->d_parent->d_inode->i_ino))
goto finished;
filp->f_pos = 2;
}
if (ncp_is_server_root(inode)) {
entries = ncp_read_volume_list(filp, dirent, filldir);
DPRINTK(KERN_DEBUG "ncp_read_volume_list returned %d\n", entries);
} else {
entries = ncp_do_readdir(filp, dirent, filldir);
DPRINTK(KERN_DEBUG "ncp_readdir: returned %d\n", entries);
}
if (entries < 0)
result = entries;
finished:
return result;
}
static int
ncp_do_simple_filldir(struct file *filp, char* name, int len,
void* dirent, filldir_t filldir)
{
struct dentry *dentry = filp->f_dentry;
struct qstr qname;
ino_t ino = 0;
int result;
qname.name = name;
qname.len = len;
ino = find_inode_number(dentry, &qname);
if (!ino)
ino = iunique(dentry->d_inode->i_sb, 2);
result = filldir(dirent, name, len, filp->f_pos, ino);
if (!result)
filp->f_pos += 1;
return result;
}
static int
ncp_do_filldir(struct file *filp, struct ncp_entry_info *entry, void *dirent,
filldir_t filldir)
{
struct dentry *dentry = filp->f_dentry;
struct inode *inode = dentry->d_inode;
struct qstr qname;
ino_t ino = 0;
int result;
/* For getwd() we have to return the correct inode in d_ino if the
* inode is currently in use. Otherwise the inode number does not
* matter. (You can argue a lot about this..)
*/
qname.name = entry->i.entryName;
qname.len = entry->i.nameLen;
{
struct dentry *newdent;
struct inode *newino;
qname.hash = full_name_hash(qname.name, qname.len);
if (dentry->d_op && dentry->d_op->d_hash)
if (dentry->d_op->d_hash(dentry, &qname) != 0)
goto end_advance;
newdent = d_lookup(dentry, &qname);
if (!newdent) {
newdent = d_alloc(dentry, &qname);
if (!newdent)
goto end_advance;
}
if (!newdent->d_inode) {
entry->opened = 0;
entry->ino = iunique(inode->i_sb, 2);
newino = ncp_iget(inode->i_sb, entry);
if (newino) {
newdent->d_op = &ncp_dentry_operations;
d_add(newdent, newino);
ncp_new_dentry(newdent);
}
} else {
ncp_update_inode2(newdent->d_inode, entry);
ncp_new_dentry(newdent);
}
if (newdent->d_inode)
ino = newdent->d_inode->i_ino;
dput(newdent);
end_advance:
}
if (!ino)
ino = find_inode_number(dentry, &qname);
if (!ino)
ino = iunique(inode->i_sb, 2);
result = filldir(dirent, entry->i.entryName, entry->i.nameLen,
filp->f_pos, ino);
if (!result)
filp->f_pos += 1;
return result;
}
static int
ncp_read_volume_list(struct file *filp, void *dirent, filldir_t filldir)
{
struct dentry *dentry = filp->f_dentry;
struct inode *inode = dentry->d_inode;
struct ncp_server *server = NCP_SERVER(inode);
struct ncp_volume_info info;
struct ncp_entry_info entry;
unsigned long total_count = 2, fpos = filp->f_pos;
int i;
DPRINTK(KERN_DEBUG "ncp_read_volume_list: pos=%ld\n", fpos);
for (i = 0; i < NCP_NUMBER_OF_VOLUMES; i++) {
if (ncp_get_volume_info_with_number(server, i, &info) != 0)
break;
if (!strlen(info.volume_name))
continue;
if (total_count < fpos) {
DPRINTK(KERN_DEBUG "ncp_read_volume_list: skipped vol: %s\n",
info.volume_name);
} else {
DPRINTK(KERN_DEBUG "ncp_read_volume_list: found vol: %s\n",
info.volume_name);
if (ncp_lookup_volume(server, info.volume_name,
&entry.i)) {
DPRINTK(KERN_DEBUG "ncpfs: could not lookup vol %s\n",
info.volume_name);
continue;
}
vol2io(server, entry.i.entryName,
!ncp_preserve_entry_case(inode, entry.i.NSCreator));
if (ncp_do_filldir(filp, &entry, dirent, filldir))
break;
}
total_count += 1;
}
return (total_count - fpos);
}
static int ncp_do_readdir(struct file *filp, void *dirent, filldir_t filldir)
{
struct dentry *dentry = filp->f_dentry;
struct inode *dir = dentry->d_inode;
struct ncp_server *server = NCP_SERVER(dir);
struct ncp_seq_cache *cache = NULL;
struct ncp_cache_control ctl;
struct ncp_entry_info entry;
struct page *page, **hash;
unsigned long total_count = 0, fpos = filp->f_pos;
int err;
DPRINTK(KERN_DEBUG "ncp_do_readdir: %s/%s, fpos=%ld\n",
dentry->d_parent->d_name.name, dentry->d_name.name, fpos);
#ifdef NCPFS_DEBUG_VERBOSE
printk("ncp_do_readdir: finding cache for %s/%s\n",
dentry->d_parent->d_name.name, dentry->d_name.name);
#endif
/* cache using inspired by smbfs and nfs */
hash = page_hash(&dir->i_data, 0);
page = __find_lock_page(&dir->i_data, 0, hash);
if (!page) {
unsigned long page_cache;
page_cache = page_cache_alloc();
if (page_cache) {
page = page_cache_entry(page_cache);
if (add_to_page_cache_unique(page, &dir->i_data, 0, hash)) {
page_cache_release(page);
page = NULL;
page_cache_free(page_cache);
}
}
}
if (!page)
goto start_search;
cache = (struct ncp_seq_cache *) page_address(page);
ctl = cache->ctl;
if (!Page_Uptodate(page))
ctl.currentpos = NCP_FPOS_EMPTY;
if ((fpos == 2) || (fpos < ctl.firstcache))
ctl.currentpos = NCP_FPOS_EMPTY;
if (ctl.currentpos == NCP_FPOS_EMPTY)
goto start_search;
{
int fetchpos = ctl.cachehead;
int readpos = ctl.firstcache;
while (readpos < fpos) {
fetchpos += cache->cache[fetchpos] + 1;
readpos++;
}
while (fpos < ctl.currentpos) {
err = ncp_do_simple_filldir(filp,
(char*)(cache->cache+fetchpos+1),
cache->cache[fetchpos],
dirent, filldir);
if (err)
goto out;
fetchpos += cache->cache[fetchpos] + 1;
fpos++;
total_count++;
}
}
start_search:
DDPRINTK(KERN_DEBUG "ncp_do_readdir: %s: f_pos=%ld,total_count=%ld\n",
dentry->d_name.name, fpos, total_count);
if (ctl.currentpos == NCP_FPOS_EMPTY) {
#ifdef NCPFS_PARANOIA
printk(KERN_DEBUG "ncp_do_readdir: init %s, volnum=%d, dirent=%u\n",
dentry->d_name.name, NCP_FINFO(dir)->volNumber, NCP_FINFO(dir)->dirEntNum);
#endif
err = ncp_initialize_search(server, dir, &ctl.seq);
if (err) {
DPRINTK(KERN_DEBUG "ncp_do_readdir: init failed, err=%d\n", err);
goto out;
}
ctl.eof = 0;
ctl.cachehead = ctl.cachetail = 0;
ctl.firstcache = ctl.currentpos = 2;
} else
DDPRINTK(KERN_DEBUG "ncp_do_readdir: reused seq for %s, fpos=%li\n",
dentry->d_name.name, total_count);
for (;;) {
err = ncp_search_for_file_or_subdir(server, &ctl.seq,
&entry.i);
if (err) {
DPRINTK(KERN_DEBUG "ncp_do_readdir: search failed, err=%d\n", err);
ctl.eof = 1;
break;
}
ctl.currentpos++;
vol2io(server, entry.i.entryName,
!ncp_preserve_entry_case(dir, entry.i.NSCreator));
if (page) {
int tlen = ctl.cachetail + entry.i.nameLen + 1;
if (tlen > NCP_DIRCACHE_SIZE) {
int ofs = ctl.cachehead;
while (tlen - ofs > NCP_DIRCACHE_SIZE) {
ofs += cache->cache[ofs] + 1;
ctl.firstcache++;
}
ctl.cachetail -= ofs;
memmove(cache->cache+0,
cache->cache+ofs,
ctl.cachetail);
}
cache->cache[ctl.cachetail++] = entry.i.nameLen;
memcpy(cache->cache+ctl.cachetail,
entry.i.entryName, entry.i.nameLen);
ctl.cachetail += entry.i.nameLen;
}
if (ctl.currentpos < fpos) {
DPRINTK(KERN_DEBUG "ncp_do_readdir: skipped file: %s/%s\n",
dentry->d_name.name, entry.i.entryName);
} else {
DDPRINTK(KERN_DEBUG "ncp_do_r: file: %s, f_pos=%ld,total_count=%ld",
entry.i.entryName, fpos, total_count);
if (ncp_do_filldir(filp, &entry, dirent, filldir))
break;
}
total_count++;
fpos++;
}
out:
if (page) {
cache->ctl = ctl;
SetPageUptodate(page);
UnlockPage(page);
page_cache_release(page);
}
DDPRINTK(KERN_DEBUG "ncp_do_readdir: %s: return=%ld\n",
dentry->d_name.name, total_count);
return total_count;
}
int ncp_conn_logged_in(struct super_block *sb)
{
struct ncp_server* server = NCP_SBP(sb);
struct nw_info_struct i;
int result;
if (ncp_single_volume(server)) {
struct dentry* dent;
result = -ENOENT;
io2vol(server, server->m.mounted_vol, 1);
if (ncp_lookup_volume(server, server->m.mounted_vol, &i)) {
#ifdef NCPFS_PARANOIA
printk(KERN_DEBUG "ncp_conn_logged_in: %s not found\n", server->m.mounted_vol);
#endif
goto out;
}
vol2io(server, i.entryName, 1);
dent = sb->s_root;
if (dent) {
struct inode* ino = dent->d_inode;
if (ino) {
NCP_FINFO(ino)->volNumber = i.volNumber;
NCP_FINFO(ino)->dirEntNum = i.dirEntNum;
NCP_FINFO(ino)->DosDirNum = i.DosDirNum;
} else {
DPRINTK(KERN_DEBUG "ncpfs: sb->s_root->d_inode == NULL!\n");
}
} else {
DPRINTK(KERN_DEBUG "ncpfs: sb->s_root == NULL!\n");
}
}
result = 0;
out:
return result;
}
static struct dentry *ncp_lookup(struct inode *dir, struct dentry *dentry)
{
struct ncp_server *server;
struct inode *inode = NULL;
int error, res, len = dentry->d_name.len;
struct ncp_entry_info finfo;
__u8 __name[dentry->d_name.len + 1];
server = NCP_SERVER(dir);
error = -EIO;
if (!ncp_conn_valid(server))
goto finished;
memcpy(__name, dentry->d_name.name, len);
__name[len] = '\0';
#ifdef NCPFS_PARANOIA
printk(KERN_DEBUG "ncp_lookup: %s, len %d\n", __name, len);
printk(KERN_DEBUG "ncp_lookup: server lookup for %s/%s\n",
dentry->d_parent->d_name.name, __name);
#endif
if (ncp_is_server_root(dir)) {
io2vol(server, __name, 1);
res = ncp_lookup_volume(server, __name, &(finfo.i));
} else {
io2vol(server, __name, !ncp_preserve_case(dir));
res = ncp_obtain_info(server, dir, __name, &(finfo.i));
}
#ifdef NCPFS_PARANOIA
printk(KERN_DEBUG "ncp_lookup: looked for %s/%s, res=%d\n",
dentry->d_parent->d_name.name, __name, res);
#endif
/*
* If we didn't find an entry, make a negative dentry.
*/
if (res)
goto add_entry;
vol2io(server, finfo.i.entryName,
!ncp_preserve_entry_case(dir, finfo.i.NSCreator));
/*
* Create an inode for the entry.
*/
finfo.opened = 0;
finfo.ino = iunique(dir->i_sb, 2);
error = -EACCES;
inode = ncp_iget(dir->i_sb, &finfo);
if (inode) {
add_entry:
dentry->d_op = &ncp_dentry_operations;
d_add(dentry, inode);
ncp_new_dentry(dentry);
error = 0;
}
finished:
#ifdef NCPFS_PARANOIA
printk(KERN_DEBUG "ncp_lookup: result=%d\n", error);
#endif
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:
#ifdef NCPFS_PARANOIA
printk(KERN_DEBUG "ncp_instantiate: %s/%s failed, closing file\n",
dentry->d_parent->d_name.name, dentry->d_name.name);
#endif
ncp_close_file(NCP_SERVER(dir), finfo->file_handle);
goto out;
}
int ncp_create_new(struct inode *dir, struct dentry *dentry, int mode,
int attributes)
{
int error, result;
struct ncp_entry_info finfo;
struct ncp_server *server = NCP_SERVER(dir);
__u8 _name[dentry->d_name.len + 1];
#ifdef NCPFS_PARANOIA
printk(KERN_DEBUG "ncp_create_new: creating %s/%s, mode=%x\n",
dentry->d_parent->d_name.name, dentry->d_name.name, mode);
#endif
error = -EIO;
if (!ncp_conn_valid(server))
goto out;
ncp_age_dentry(server, dentry);
memcpy(_name, dentry->d_name.name, dentry->d_name.len);
_name[dentry->d_name.len] = '\0';
io2vol(server, _name, !ncp_preserve_case(dir));
error = -EACCES;
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);
if (!result) {
finfo.access = O_RDWR;
error = ncp_instantiate(dir, dentry, &finfo);
} else {
if (result == 0x87) error = -ENAMETOOLONG;
DPRINTK(KERN_DEBUG "ncp_create: %s/%s failed\n",
dentry->d_parent->d_name.name, dentry->d_name.name);
}
out:
return error;
}
static int ncp_create(struct inode *dir, struct dentry *dentry, int mode)
{
return ncp_create_new(dir, dentry, mode, 0);
}
static int ncp_mkdir(struct inode *dir, struct dentry *dentry, int mode)
{
int error;
struct ncp_entry_info finfo;
struct ncp_server *server = NCP_SERVER(dir);
__u8 _name[dentry->d_name.len + 1];
DPRINTK(KERN_DEBUG "ncp_mkdir: making %s/%s\n",
dentry->d_parent->d_name.name, dentry->d_name.name);
error = -EIO;
if (!ncp_conn_valid(server))
goto out;
ncp_age_dentry(server, dentry);
memcpy(_name, dentry->d_name.name, dentry->d_name.len);
_name[dentry->d_name.len] = '\0';
io2vol(server, _name, !ncp_preserve_case(dir));
error = -EACCES;
if (ncp_open_create_file_or_subdir(server, dir, _name,
OC_MODE_CREATE, aDIR, 0xffff,
&finfo) == 0)
{
error = ncp_instantiate(dir, dentry, &finfo);
}
out:
return error;
}
static int ncp_rmdir(struct inode *dir, struct dentry *dentry)
{
int error, result;
struct ncp_server *server = NCP_SERVER(dir);
__u8 _name[dentry->d_name.len + 1];
DPRINTK(KERN_DEBUG "ncp_rmdir: removing %s/%s\n",
dentry->d_parent->d_name.name, dentry->d_name.name);
error = -EIO;
if (!ncp_conn_valid(server))
goto out;
error = -EBUSY;
if (!list_empty(&dentry->d_hash))
goto out;
memcpy(_name, dentry->d_name.name, dentry->d_name.len);
_name[dentry->d_name.len] = '\0';
io2vol(server, _name, !ncp_preserve_case(dir));
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:
error = -EACCES;
break;
}
out:
return error;
}
static int ncp_unlink(struct inode *dir, struct dentry *dentry)
{
struct inode *inode = dentry->d_inode;
struct ncp_server *server = NCP_SERVER(dir);
int error;
DPRINTK(KERN_DEBUG "ncp_unlink: unlinking %s/%s\n",
dentry->d_parent->d_name.name, dentry->d_name.name);
error = -EIO;
if (!ncp_conn_valid(server))
goto out;
/*
* Check whether to close the file ...
*/
if (inode && NCP_FINFO(inode)->opened) {
#ifdef NCPFS_PARANOIA
printk(KERN_DEBUG "ncp_unlink: closing file\n");
#endif
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 && server->m.flags & NCP_MOUNT_STRONG) { /* R/O */
error = ncp_force_unlink(dir, dentry);
}
#endif
switch (error) {
case 0x00:
DPRINTK(KERN_DEBUG "ncp: removed %s/%s\n",
dentry->d_parent->d_name.name, dentry->d_name.name);
d_delete(dentry);
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:
error = -EACCES;
break;
}
out:
return error;
}
static int ncp_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry)
{
int old_len = old_dentry->d_name.len;
int new_len = new_dentry->d_name.len;
int error;
char _old_name[old_dentry->d_name.len + 1];
char _new_name[new_dentry->d_name.len + 1];
DPRINTK(KERN_DEBUG "ncp_rename: %s/%s to %s/%s\n",
old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
new_dentry->d_parent->d_name.name, new_dentry->d_name.name);
error = -EIO;
if (!ncp_conn_valid(NCP_SERVER(old_dir)))
goto out;
ncp_age_dentry(NCP_SERVER(old_dir), old_dentry);
ncp_age_dentry(NCP_SERVER(new_dir), new_dentry);
memcpy(_old_name, old_dentry->d_name.name, old_len);
_old_name[old_len] = '\0';
io2vol(NCP_SERVER(old_dir), _old_name, !ncp_preserve_case(old_dir));
memcpy(_new_name, new_dentry->d_name.name, new_len);
_new_name[new_len] = '\0';
io2vol(NCP_SERVER(new_dir), _new_name, !ncp_preserve_case(new_dir));
error = ncp_ren_or_mov_file_or_subdir(NCP_SERVER(old_dir),
old_dir, _old_name,
new_dir, _new_name);
#ifdef CONFIG_NCPFS_STRONG
if ((error == 0x90 || error == -EACCES) && NCP_SERVER(old_dir)->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:
DPRINTK(KERN_DEBUG "ncp renamed %s -> %s.\n",
old_dentry->d_name.name,new_dentry->d_name.name);
/* d_move(old_dentry, new_dentry); */
break;
case 0x9E:
error = -ENAMETOOLONG;
break;
case 0xFF:
error = -ENOENT;
break;
default:
error = -EACCES;
break;
}
out:
return error;
}
/* 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(unsigned short time, unsigned short date)
{
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, unsigned short *time, unsigned short *date)
{
int day, year, nl_day, month;
unix_date = utc2local(unix_date);
*time = (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;
for (month = 0; month < 12; month++)
if (day_n[month] > nl_day)
break;
}
*date = nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9);
}
|