Linux-2.6.12-rc2
[linux-2.6.git] / fs / ncpfs / dir.c
1 /*
2  *  dir.c
3  *
4  *  Copyright (C) 1995, 1996 by Volker Lendecke
5  *  Modified for big endian by J.F. Chadima and David S. Miller
6  *  Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
7  *  Modified 1998, 1999 Wolfram Pienkoss for NLS
8  *  Modified 1999 Wolfram Pienkoss for directory caching
9  *  Modified 2000 Ben Harris, University of Cambridge for NFS NS meta-info
10  *
11  */
12
13 #include <linux/config.h>
14
15 #include <linux/time.h>
16 #include <linux/errno.h>
17 #include <linux/stat.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
21 #include <linux/mm.h>
22 #include <asm/uaccess.h>
23 #include <asm/byteorder.h>
24 #include <linux/smp_lock.h>
25
26 #include <linux/ncp_fs.h>
27
28 #include "ncplib_kernel.h"
29
30 static void ncp_read_volume_list(struct file *, void *, filldir_t,
31                                 struct ncp_cache_control *);
32 static void ncp_do_readdir(struct file *, void *, filldir_t,
33                                 struct ncp_cache_control *);
34
35 static int ncp_readdir(struct file *, void *, filldir_t);
36
37 static int ncp_create(struct inode *, struct dentry *, int, struct nameidata *);
38 static struct dentry *ncp_lookup(struct inode *, struct dentry *, struct nameidata *);
39 static int ncp_unlink(struct inode *, struct dentry *);
40 static int ncp_mkdir(struct inode *, struct dentry *, int);
41 static int ncp_rmdir(struct inode *, struct dentry *);
42 static int ncp_rename(struct inode *, struct dentry *,
43                       struct inode *, struct dentry *);
44 static int ncp_mknod(struct inode * dir, struct dentry *dentry,
45                      int mode, dev_t rdev);
46 #if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
47 extern int ncp_symlink(struct inode *, struct dentry *, const char *);
48 #else
49 #define ncp_symlink NULL
50 #endif
51                       
52 struct file_operations ncp_dir_operations =
53 {
54         .read           = generic_read_dir,
55         .readdir        = ncp_readdir,
56         .ioctl          = ncp_ioctl,
57 };
58
59 struct inode_operations ncp_dir_inode_operations =
60 {
61         .create         = ncp_create,
62         .lookup         = ncp_lookup,
63         .unlink         = ncp_unlink,
64         .symlink        = ncp_symlink,
65         .mkdir          = ncp_mkdir,
66         .rmdir          = ncp_rmdir,
67         .mknod          = ncp_mknod,
68         .rename         = ncp_rename,
69         .setattr        = ncp_notify_change,
70 };
71
72 /*
73  * Dentry operations routines
74  */
75 static int ncp_lookup_validate(struct dentry *, struct nameidata *);
76 static int ncp_hash_dentry(struct dentry *, struct qstr *);
77 static int ncp_compare_dentry (struct dentry *, struct qstr *, struct qstr *);
78 static int ncp_delete_dentry(struct dentry *);
79
80 static struct dentry_operations ncp_dentry_operations =
81 {
82         .d_revalidate   = ncp_lookup_validate,
83         .d_hash         = ncp_hash_dentry,
84         .d_compare      = ncp_compare_dentry,
85         .d_delete       = ncp_delete_dentry,
86 };
87
88 struct dentry_operations ncp_root_dentry_operations =
89 {
90         .d_hash         = ncp_hash_dentry,
91         .d_compare      = ncp_compare_dentry,
92         .d_delete       = ncp_delete_dentry,
93 };
94
95
96 /*
97  * Note: leave the hash unchanged if the directory
98  * is case-sensitive.
99  */
100 static int 
101 ncp_hash_dentry(struct dentry *dentry, struct qstr *this)
102 {
103         struct nls_table *t;
104         unsigned long hash;
105         int i;
106
107         t = NCP_IO_TABLE(dentry);
108
109         if (!ncp_case_sensitive(dentry->d_inode)) {
110                 hash = init_name_hash();
111                 for (i=0; i<this->len ; i++)
112                         hash = partial_name_hash(ncp_tolower(t, this->name[i]),
113                                                                         hash);
114                 this->hash = end_name_hash(hash);
115         }
116         return 0;
117 }
118
119 static int
120 ncp_compare_dentry(struct dentry *dentry, struct qstr *a, struct qstr *b)
121 {
122         if (a->len != b->len)
123                 return 1;
124
125         if (ncp_case_sensitive(dentry->d_inode))
126                 return strncmp(a->name, b->name, a->len);
127
128         return ncp_strnicmp(NCP_IO_TABLE(dentry), a->name, b->name, a->len);
129 }
130
131 /*
132  * This is the callback from dput() when d_count is going to 0.
133  * We use this to unhash dentries with bad inodes.
134  * Closing files can be safely postponed until iput() - it's done there anyway.
135  */
136 static int
137 ncp_delete_dentry(struct dentry * dentry)
138 {
139         struct inode *inode = dentry->d_inode;
140
141         if (inode) {
142                 if (is_bad_inode(inode))
143                         return 1;
144         } else
145         {
146         /* N.B. Unhash negative dentries? */
147         }
148         return 0;
149 }
150
151 static inline int
152 ncp_single_volume(struct ncp_server *server)
153 {
154         return (server->m.mounted_vol[0] != '\0');
155 }
156
157 static inline int ncp_is_server_root(struct inode *inode)
158 {
159         return (!ncp_single_volume(NCP_SERVER(inode)) &&
160                 inode == inode->i_sb->s_root->d_inode);
161 }
162
163
164 /*
165  * This is the callback when the dcache has a lookup hit.
166  */
167
168
169 #ifdef CONFIG_NCPFS_STRONG
170 /* try to delete a readonly file (NW R bit set) */
171
172 static int
173 ncp_force_unlink(struct inode *dir, struct dentry* dentry)
174 {
175         int res=0x9c,res2;
176         struct nw_modify_dos_info info;
177         __le32 old_nwattr;
178         struct inode *inode;
179
180         memset(&info, 0, sizeof(info));
181         
182         /* remove the Read-Only flag on the NW server */
183         inode = dentry->d_inode;
184
185         old_nwattr = NCP_FINFO(inode)->nwattr;
186         info.attributes = old_nwattr & ~(aRONLY|aDELETEINHIBIT|aRENAMEINHIBIT);
187         res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(inode), inode, NULL, DM_ATTRIBUTES, &info);
188         if (res2)
189                 goto leave_me;
190
191         /* now try again the delete operation */
192         res = ncp_del_file_or_subdir2(NCP_SERVER(dir), dentry);
193
194         if (res)  /* delete failed, set R bit again */
195         {
196                 info.attributes = old_nwattr;
197                 res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(inode), inode, NULL, DM_ATTRIBUTES, &info);
198                 if (res2)
199                         goto leave_me;
200         }
201 leave_me:
202         return(res);
203 }
204 #endif  /* CONFIG_NCPFS_STRONG */
205
206 #ifdef CONFIG_NCPFS_STRONG
207 static int
208 ncp_force_rename(struct inode *old_dir, struct dentry* old_dentry, char *_old_name,
209                  struct inode *new_dir, struct dentry* new_dentry, char *_new_name)
210 {
211         struct nw_modify_dos_info info;
212         int res=0x90,res2;
213         struct inode *old_inode = old_dentry->d_inode;
214         __le32 old_nwattr = NCP_FINFO(old_inode)->nwattr;
215         __le32 new_nwattr = 0; /* shut compiler warning */
216         int old_nwattr_changed = 0;
217         int new_nwattr_changed = 0;
218
219         memset(&info, 0, sizeof(info));
220         
221         /* remove the Read-Only flag on the NW server */
222
223         info.attributes = old_nwattr & ~(aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT);
224         res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(old_inode), old_inode, NULL, DM_ATTRIBUTES, &info);
225         if (!res2)
226                 old_nwattr_changed = 1;
227         if (new_dentry && new_dentry->d_inode) {
228                 new_nwattr = NCP_FINFO(new_dentry->d_inode)->nwattr;
229                 info.attributes = new_nwattr & ~(aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT);
230                 res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(new_dir), new_dir, _new_name, DM_ATTRIBUTES, &info);
231                 if (!res2)
232                         new_nwattr_changed = 1;
233         }
234         /* now try again the rename operation */
235         /* but only if something really happened */
236         if (new_nwattr_changed || old_nwattr_changed) {
237                 res = ncp_ren_or_mov_file_or_subdir(NCP_SERVER(old_dir),
238                                                     old_dir, _old_name,
239                                                     new_dir, _new_name);
240         } 
241         if (res)
242                 goto leave_me;
243         /* file was successfully renamed, so:
244            do not set attributes on old file - it no longer exists
245            copy attributes from old file to new */
246         new_nwattr_changed = old_nwattr_changed;
247         new_nwattr = old_nwattr;
248         old_nwattr_changed = 0;
249         
250 leave_me:;
251         if (old_nwattr_changed) {
252                 info.attributes = old_nwattr;
253                 res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(old_inode), old_inode, NULL, DM_ATTRIBUTES, &info);
254                 /* ignore errors */
255         }
256         if (new_nwattr_changed) {
257                 info.attributes = new_nwattr;
258                 res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(new_dir), new_dir, _new_name, DM_ATTRIBUTES, &info);
259                 /* ignore errors */
260         }
261         return(res);
262 }
263 #endif  /* CONFIG_NCPFS_STRONG */
264
265
266 static int
267 __ncp_lookup_validate(struct dentry * dentry, struct nameidata *nd)
268 {
269         struct ncp_server *server;
270         struct dentry *parent;
271         struct inode *dir;
272         struct ncp_entry_info finfo;
273         int res, val = 0, len;
274         __u8 __name[NCP_MAXPATHLEN + 1];
275
276         parent = dget_parent(dentry);
277         dir = parent->d_inode;
278
279         if (!dentry->d_inode)
280                 goto finished;
281
282         server = NCP_SERVER(dir);
283
284         if (!ncp_conn_valid(server))
285                 goto finished;
286
287         /*
288          * Inspired by smbfs:
289          * The default validation is based on dentry age:
290          * We set the max age at mount time.  (But each
291          * successful server lookup renews the timestamp.)
292          */
293         val = NCP_TEST_AGE(server, dentry);
294         if (val)
295                 goto finished;
296
297         DDPRINTK("ncp_lookup_validate: %s/%s not valid, age=%ld, server lookup\n",
298                 dentry->d_parent->d_name.name, dentry->d_name.name,
299                 NCP_GET_AGE(dentry));
300
301         len = sizeof(__name);
302         if (ncp_is_server_root(dir)) {
303                 res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
304                                  dentry->d_name.len, 1);
305                 if (!res)
306                         res = ncp_lookup_volume(server, __name, &(finfo.i));
307         } else {
308                 res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
309                                  dentry->d_name.len, !ncp_preserve_case(dir));
310                 if (!res)
311                         res = ncp_obtain_info(server, dir, __name, &(finfo.i));
312         }
313         finfo.volume = finfo.i.volNumber;
314         DDPRINTK("ncp_lookup_validate: looked for %s/%s, res=%d\n",
315                 dentry->d_parent->d_name.name, __name, res);
316         /*
317          * If we didn't find it, or if it has a different dirEntNum to
318          * what we remember, it's not valid any more.
319          */
320         if (!res) {
321                 if (finfo.i.dirEntNum == NCP_FINFO(dentry->d_inode)->dirEntNum) {
322                         ncp_new_dentry(dentry);
323                         val=1;
324                 } else
325                         DDPRINTK("ncp_lookup_validate: found, but dirEntNum changed\n");
326
327                 ncp_update_inode2(dentry->d_inode, &finfo);
328         }
329
330 finished:
331         DDPRINTK("ncp_lookup_validate: result=%d\n", val);
332         dput(parent);
333         return val;
334 }
335
336 static int
337 ncp_lookup_validate(struct dentry * dentry, struct nameidata *nd)
338 {
339         int res;
340         lock_kernel();
341         res = __ncp_lookup_validate(dentry, nd);
342         unlock_kernel();
343         return res;
344 }
345
346 static struct dentry *
347 ncp_dget_fpos(struct dentry *dentry, struct dentry *parent, unsigned long fpos)
348 {
349         struct dentry *dent = dentry;
350         struct list_head *next;
351
352         if (d_validate(dent, parent)) {
353                 if (dent->d_name.len <= NCP_MAXPATHLEN &&
354                     (unsigned long)dent->d_fsdata == fpos) {
355                         if (!dent->d_inode) {
356                                 dput(dent);
357                                 dent = NULL;
358                         }
359                         return dent;
360                 }
361                 dput(dent);
362         }
363
364         /* If a pointer is invalid, we search the dentry. */
365         spin_lock(&dcache_lock);
366         next = parent->d_subdirs.next;
367         while (next != &parent->d_subdirs) {
368                 dent = list_entry(next, struct dentry, d_child);
369                 if ((unsigned long)dent->d_fsdata == fpos) {
370                         if (dent->d_inode)
371                                 dget_locked(dent);
372                         else
373                                 dent = NULL;
374                         spin_unlock(&dcache_lock);
375                         goto out;
376                 }
377                 next = next->next;
378         }
379         spin_unlock(&dcache_lock);
380         return NULL;
381
382 out:
383         return dent;
384 }
385
386 static time_t ncp_obtain_mtime(struct dentry *dentry)
387 {
388         struct inode *inode = dentry->d_inode;
389         struct ncp_server *server = NCP_SERVER(inode);
390         struct nw_info_struct i;
391
392         if (!ncp_conn_valid(server) || ncp_is_server_root(inode))
393                 return 0;
394
395         if (ncp_obtain_info(server, inode, NULL, &i))
396                 return 0;
397
398         return ncp_date_dos2unix(i.modifyTime, i.modifyDate);
399 }
400
401 static int ncp_readdir(struct file *filp, void *dirent, filldir_t filldir)
402 {
403         struct dentry *dentry = filp->f_dentry;
404         struct inode *inode = dentry->d_inode;
405         struct page *page = NULL;
406         struct ncp_server *server = NCP_SERVER(inode);
407         union  ncp_dir_cache *cache = NULL;
408         struct ncp_cache_control ctl;
409         int result, mtime_valid = 0;
410         time_t mtime = 0;
411
412         lock_kernel();
413
414         ctl.page  = NULL;
415         ctl.cache = NULL;
416
417         DDPRINTK("ncp_readdir: reading %s/%s, pos=%d\n",
418                 dentry->d_parent->d_name.name, dentry->d_name.name,
419                 (int) filp->f_pos);
420
421         result = -EIO;
422         if (!ncp_conn_valid(server))
423                 goto out;
424
425         result = 0;
426         if (filp->f_pos == 0) {
427                 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR))
428                         goto out;
429                 filp->f_pos = 1;
430         }
431         if (filp->f_pos == 1) {
432                 if (filldir(dirent, "..", 2, 1, parent_ino(dentry), DT_DIR))
433                         goto out;
434                 filp->f_pos = 2;
435         }
436
437         page = grab_cache_page(&inode->i_data, 0);
438         if (!page)
439                 goto read_really;
440
441         ctl.cache = cache = kmap(page);
442         ctl.head  = cache->head;
443
444         if (!PageUptodate(page) || !ctl.head.eof)
445                 goto init_cache;
446
447         if (filp->f_pos == 2) {
448                 if (jiffies - ctl.head.time >= NCP_MAX_AGE(server))
449                         goto init_cache;
450
451                 mtime = ncp_obtain_mtime(dentry);
452                 mtime_valid = 1;
453                 if ((!mtime) || (mtime != ctl.head.mtime))
454                         goto init_cache;
455         }
456
457         if (filp->f_pos > ctl.head.end)
458                 goto finished;
459
460         ctl.fpos = filp->f_pos + (NCP_DIRCACHE_START - 2);
461         ctl.ofs  = ctl.fpos / NCP_DIRCACHE_SIZE;
462         ctl.idx  = ctl.fpos % NCP_DIRCACHE_SIZE;
463
464         for (;;) {
465                 if (ctl.ofs != 0) {
466                         ctl.page = find_lock_page(&inode->i_data, ctl.ofs);
467                         if (!ctl.page)
468                                 goto invalid_cache;
469                         ctl.cache = kmap(ctl.page);
470                         if (!PageUptodate(ctl.page))
471                                 goto invalid_cache;
472                 }
473                 while (ctl.idx < NCP_DIRCACHE_SIZE) {
474                         struct dentry *dent;
475                         int res;
476
477                         dent = ncp_dget_fpos(ctl.cache->dentry[ctl.idx],
478                                                 dentry, filp->f_pos);
479                         if (!dent)
480                                 goto invalid_cache;
481                         res = filldir(dirent, dent->d_name.name,
482                                         dent->d_name.len, filp->f_pos,
483                                         dent->d_inode->i_ino, DT_UNKNOWN);
484                         dput(dent);
485                         if (res)
486                                 goto finished;
487                         filp->f_pos += 1;
488                         ctl.idx += 1;
489                         if (filp->f_pos > ctl.head.end)
490                                 goto finished;
491                 }
492                 if (ctl.page) {
493                         kunmap(ctl.page);
494                         SetPageUptodate(ctl.page);
495                         unlock_page(ctl.page);
496                         page_cache_release(ctl.page);
497                         ctl.page = NULL;
498                 }
499                 ctl.idx  = 0;
500                 ctl.ofs += 1;
501         }
502 invalid_cache:
503         if (ctl.page) {
504                 kunmap(ctl.page);
505                 unlock_page(ctl.page);
506                 page_cache_release(ctl.page);
507                 ctl.page = NULL;
508         }
509         ctl.cache = cache;
510 init_cache:
511         ncp_invalidate_dircache_entries(dentry);
512         if (!mtime_valid) {
513                 mtime = ncp_obtain_mtime(dentry);
514                 mtime_valid = 1;
515         }
516         ctl.head.mtime = mtime;
517         ctl.head.time = jiffies;
518         ctl.head.eof = 0;
519         ctl.fpos = 2;
520         ctl.ofs = 0;
521         ctl.idx = NCP_DIRCACHE_START;
522         ctl.filled = 0;
523         ctl.valid  = 1;
524 read_really:
525         if (ncp_is_server_root(inode)) {
526                 ncp_read_volume_list(filp, dirent, filldir, &ctl);
527         } else {
528                 ncp_do_readdir(filp, dirent, filldir, &ctl);
529         }
530         ctl.head.end = ctl.fpos - 1;
531         ctl.head.eof = ctl.valid;
532 finished:
533         if (page) {
534                 cache->head = ctl.head;
535                 kunmap(page);
536                 SetPageUptodate(page);
537                 unlock_page(page);
538                 page_cache_release(page);
539         }
540         if (ctl.page) {
541                 kunmap(ctl.page);
542                 SetPageUptodate(ctl.page);
543                 unlock_page(ctl.page);
544                 page_cache_release(ctl.page);
545         }
546 out:
547         unlock_kernel();
548         return result;
549 }
550
551 static int
552 ncp_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
553                 struct ncp_cache_control *ctrl, struct ncp_entry_info *entry)
554 {
555         struct dentry *newdent, *dentry = filp->f_dentry;
556         struct inode *newino, *inode = dentry->d_inode;
557         struct ncp_cache_control ctl = *ctrl;
558         struct qstr qname;
559         int valid = 0;
560         int hashed = 0;
561         ino_t ino = 0;
562         __u8 __name[NCP_MAXPATHLEN + 1];
563
564         qname.len = sizeof(__name);
565         if (ncp_vol2io(NCP_SERVER(inode), __name, &qname.len,
566                         entry->i.entryName, entry->i.nameLen,
567                         !ncp_preserve_entry_case(inode, entry->i.NSCreator)))
568                 return 1; /* I'm not sure */
569
570         qname.name = __name;
571         qname.hash = full_name_hash(qname.name, qname.len);
572
573         if (dentry->d_op && dentry->d_op->d_hash)
574                 if (dentry->d_op->d_hash(dentry, &qname) != 0)
575                         goto end_advance;
576
577         newdent = d_lookup(dentry, &qname);
578
579         if (!newdent) {
580                 newdent = d_alloc(dentry, &qname);
581                 if (!newdent)
582                         goto end_advance;
583         } else {
584                 hashed = 1;
585                 memcpy((char *) newdent->d_name.name, qname.name,
586                                                         newdent->d_name.len);
587         }
588
589         if (!newdent->d_inode) {
590                 entry->opened = 0;
591                 entry->ino = iunique(inode->i_sb, 2);
592                 newino = ncp_iget(inode->i_sb, entry);
593                 if (newino) {
594                         newdent->d_op = &ncp_dentry_operations;
595                         d_instantiate(newdent, newino);
596                         if (!hashed)
597                                 d_rehash(newdent);
598                 }
599         } else
600                 ncp_update_inode2(newdent->d_inode, entry);
601
602         if (newdent->d_inode) {
603                 ino = newdent->d_inode->i_ino;
604                 newdent->d_fsdata = (void *) ctl.fpos;
605                 ncp_new_dentry(newdent);
606         }
607
608         if (ctl.idx >= NCP_DIRCACHE_SIZE) {
609                 if (ctl.page) {
610                         kunmap(ctl.page);
611                         SetPageUptodate(ctl.page);
612                         unlock_page(ctl.page);
613                         page_cache_release(ctl.page);
614                 }
615                 ctl.cache = NULL;
616                 ctl.idx  -= NCP_DIRCACHE_SIZE;
617                 ctl.ofs  += 1;
618                 ctl.page  = grab_cache_page(&inode->i_data, ctl.ofs);
619                 if (ctl.page)
620                         ctl.cache = kmap(ctl.page);
621         }
622         if (ctl.cache) {
623                 ctl.cache->dentry[ctl.idx] = newdent;
624                 valid = 1;
625         }
626         dput(newdent);
627 end_advance:
628         if (!valid)
629                 ctl.valid = 0;
630         if (!ctl.filled && (ctl.fpos == filp->f_pos)) {
631                 if (!ino)
632                         ino = find_inode_number(dentry, &qname);
633                 if (!ino)
634                         ino = iunique(inode->i_sb, 2);
635                 ctl.filled = filldir(dirent, qname.name, qname.len,
636                                      filp->f_pos, ino, DT_UNKNOWN);
637                 if (!ctl.filled)
638                         filp->f_pos += 1;
639         }
640         ctl.fpos += 1;
641         ctl.idx  += 1;
642         *ctrl = ctl;
643         return (ctl.valid || !ctl.filled);
644 }
645
646 static void
647 ncp_read_volume_list(struct file *filp, void *dirent, filldir_t filldir,
648                         struct ncp_cache_control *ctl)
649 {
650         struct dentry *dentry = filp->f_dentry;
651         struct inode *inode = dentry->d_inode;
652         struct ncp_server *server = NCP_SERVER(inode);
653         struct ncp_volume_info info;
654         struct ncp_entry_info entry;
655         int i;
656
657         DPRINTK("ncp_read_volume_list: pos=%ld\n",
658                         (unsigned long) filp->f_pos);
659
660         for (i = 0; i < NCP_NUMBER_OF_VOLUMES; i++) {
661
662                 if (ncp_get_volume_info_with_number(server, i, &info) != 0)
663                         return;
664                 if (!strlen(info.volume_name))
665                         continue;
666
667                 DPRINTK("ncp_read_volume_list: found vol: %s\n",
668                         info.volume_name);
669
670                 if (ncp_lookup_volume(server, info.volume_name,
671                                         &entry.i)) {
672                         DPRINTK("ncpfs: could not lookup vol %s\n",
673                                 info.volume_name);
674                         continue;
675                 }
676                 entry.volume = entry.i.volNumber;
677                 if (!ncp_fill_cache(filp, dirent, filldir, ctl, &entry))
678                         return;
679         }
680 }
681
682 static void
683 ncp_do_readdir(struct file *filp, void *dirent, filldir_t filldir,
684                                                 struct ncp_cache_control *ctl)
685 {
686         struct dentry *dentry = filp->f_dentry;
687         struct inode *dir = dentry->d_inode;
688         struct ncp_server *server = NCP_SERVER(dir);
689         struct nw_search_sequence seq;
690         struct ncp_entry_info entry;
691         int err;
692         void* buf;
693         int more;
694         size_t bufsize;
695
696         DPRINTK("ncp_do_readdir: %s/%s, fpos=%ld\n",
697                 dentry->d_parent->d_name.name, dentry->d_name.name,
698                 (unsigned long) filp->f_pos);
699         PPRINTK("ncp_do_readdir: init %s, volnum=%d, dirent=%u\n",
700                 dentry->d_name.name, NCP_FINFO(dir)->volNumber,
701                 NCP_FINFO(dir)->dirEntNum);
702
703         err = ncp_initialize_search(server, dir, &seq);
704         if (err) {
705                 DPRINTK("ncp_do_readdir: init failed, err=%d\n", err);
706                 return;
707         }
708 #ifdef USE_OLD_SLOW_DIRECTORY_LISTING
709         for (;;) {
710                 err = ncp_search_for_file_or_subdir(server, &seq, &entry.i);
711                 if (err) {
712                         DPRINTK("ncp_do_readdir: search failed, err=%d\n", err);
713                         break;
714                 }
715                 entry.volume = entry.i.volNumber;
716                 if (!ncp_fill_cache(filp, dirent, filldir, ctl, &entry))
717                         break;
718         }
719 #else
720         /* We MUST NOT use server->buffer_size handshaked with server if we are
721            using UDP, as for UDP server uses max. buffer size determined by
722            MTU, and for TCP server uses hardwired value 65KB (== 66560 bytes). 
723            So we use 128KB, just to be sure, as there is no way how to know
724            this value in advance. */
725         bufsize = 131072;
726         buf = vmalloc(bufsize);
727         if (!buf)
728                 return;
729         do {
730                 int cnt;
731                 char* rpl;
732                 size_t rpls;
733
734                 err = ncp_search_for_fileset(server, &seq, &more, &cnt, buf, bufsize, &rpl, &rpls);
735                 if (err)                /* Error */
736                         break;
737                 if (!cnt)               /* prevent endless loop */
738                         break;
739                 while (cnt--) {
740                         size_t onerpl;
741                         
742                         if (rpls < offsetof(struct nw_info_struct, entryName))
743                                 break;  /* short packet */
744                         ncp_extract_file_info(rpl, &entry.i);
745                         onerpl = offsetof(struct nw_info_struct, entryName) + entry.i.nameLen;
746                         if (rpls < onerpl)
747                                 break;  /* short packet */
748                         (void)ncp_obtain_nfs_info(server, &entry.i);
749                         rpl += onerpl;
750                         rpls -= onerpl;
751                         entry.volume = entry.i.volNumber;
752                         if (!ncp_fill_cache(filp, dirent, filldir, ctl, &entry))
753                                 break;
754                 }
755         } while (more);
756         vfree(buf);
757 #endif
758         return;
759 }
760
761 int ncp_conn_logged_in(struct super_block *sb)
762 {
763         struct ncp_server* server = NCP_SBP(sb);
764         int result;
765
766         if (ncp_single_volume(server)) {
767                 int len;
768                 struct dentry* dent;
769                 __u32 volNumber;
770                 __le32 dirEntNum;
771                 __le32 DosDirNum;
772                 __u8 __name[NCP_MAXPATHLEN + 1];
773
774                 len = sizeof(__name);
775                 result = ncp_io2vol(server, __name, &len, server->m.mounted_vol,
776                                     strlen(server->m.mounted_vol), 1);
777                 if (result)
778                         goto out;
779                 result = -ENOENT;
780                 if (ncp_get_volume_root(server, __name, &volNumber, &dirEntNum, &DosDirNum)) {
781                         PPRINTK("ncp_conn_logged_in: %s not found\n",
782                                 server->m.mounted_vol);
783                         goto out;
784                 }
785                 dent = sb->s_root;
786                 if (dent) {
787                         struct inode* ino = dent->d_inode;
788                         if (ino) {
789                                 NCP_FINFO(ino)->volNumber = volNumber;
790                                 NCP_FINFO(ino)->dirEntNum = dirEntNum;
791                                 NCP_FINFO(ino)->DosDirNum = DosDirNum;
792                         } else {
793                                 DPRINTK("ncpfs: sb->s_root->d_inode == NULL!\n");
794                         }
795                 } else {
796                         DPRINTK("ncpfs: sb->s_root == NULL!\n");
797                 }
798         }
799         result = 0;
800
801 out:
802         return result;
803 }
804
805 static struct dentry *ncp_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
806 {
807         struct ncp_server *server = NCP_SERVER(dir);
808         struct inode *inode = NULL;
809         struct ncp_entry_info finfo;
810         int error, res, len;
811         __u8 __name[NCP_MAXPATHLEN + 1];
812
813         lock_kernel();
814         error = -EIO;
815         if (!ncp_conn_valid(server))
816                 goto finished;
817
818         PPRINTK("ncp_lookup: server lookup for %s/%s\n",
819                 dentry->d_parent->d_name.name, dentry->d_name.name);
820
821         len = sizeof(__name);
822         if (ncp_is_server_root(dir)) {
823                 res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
824                                  dentry->d_name.len, 1);
825                 if (!res)
826                         res = ncp_lookup_volume(server, __name, &(finfo.i));
827         } else {
828                 res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
829                                  dentry->d_name.len, !ncp_preserve_case(dir));
830                 if (!res)
831                         res = ncp_obtain_info(server, dir, __name, &(finfo.i));
832         }
833         PPRINTK("ncp_lookup: looked for %s/%s, res=%d\n",
834                 dentry->d_parent->d_name.name, __name, res);
835         /*
836          * If we didn't find an entry, make a negative dentry.
837          */
838         if (res)
839                 goto add_entry;
840
841         /*
842          * Create an inode for the entry.
843          */
844         finfo.opened = 0;
845         finfo.ino = iunique(dir->i_sb, 2);
846         finfo.volume = finfo.i.volNumber;
847         error = -EACCES;
848         inode = ncp_iget(dir->i_sb, &finfo);
849
850         if (inode) {
851                 ncp_new_dentry(dentry);
852 add_entry:
853                 dentry->d_op = &ncp_dentry_operations;
854                 d_add(dentry, inode);
855                 error = 0;
856         }
857
858 finished:
859         PPRINTK("ncp_lookup: result=%d\n", error);
860         unlock_kernel();
861         return ERR_PTR(error);
862 }
863
864 /*
865  * This code is common to create, mkdir, and mknod.
866  */
867 static int ncp_instantiate(struct inode *dir, struct dentry *dentry,
868                         struct ncp_entry_info *finfo)
869 {
870         struct inode *inode;
871         int error = -EINVAL;
872
873         finfo->ino = iunique(dir->i_sb, 2);
874         inode = ncp_iget(dir->i_sb, finfo);
875         if (!inode)
876                 goto out_close;
877         d_instantiate(dentry,inode);
878         error = 0;
879 out:
880         return error;
881
882 out_close:
883         PPRINTK("ncp_instantiate: %s/%s failed, closing file\n",
884                 dentry->d_parent->d_name.name, dentry->d_name.name);
885         ncp_close_file(NCP_SERVER(dir), finfo->file_handle);
886         goto out;
887 }
888
889 int ncp_create_new(struct inode *dir, struct dentry *dentry, int mode,
890                    dev_t rdev, __le32 attributes)
891 {
892         struct ncp_server *server = NCP_SERVER(dir);
893         struct ncp_entry_info finfo;
894         int error, result, len;
895         int opmode;
896         __u8 __name[NCP_MAXPATHLEN + 1];
897         
898         PPRINTK("ncp_create_new: creating %s/%s, mode=%x\n",
899                 dentry->d_parent->d_name.name, dentry->d_name.name, mode);
900
901         error = -EIO;
902         lock_kernel();
903         if (!ncp_conn_valid(server))
904                 goto out;
905
906         ncp_age_dentry(server, dentry);
907         len = sizeof(__name);
908         error = ncp_io2vol(server, __name, &len, dentry->d_name.name,
909                            dentry->d_name.len, !ncp_preserve_case(dir));
910         if (error)
911                 goto out;
912
913         error = -EACCES;
914         
915         if (S_ISREG(mode) && 
916             (server->m.flags & NCP_MOUNT_EXTRAS) && 
917             (mode & S_IXUGO))
918                 attributes |= aSYSTEM | aSHARED;
919         
920         result = ncp_open_create_file_or_subdir(server, dir, __name,
921                                 OC_MODE_CREATE | OC_MODE_OPEN | OC_MODE_REPLACE,
922                                 attributes, AR_READ | AR_WRITE, &finfo);
923         opmode = O_RDWR;
924         if (result) {
925                 result = ncp_open_create_file_or_subdir(server, dir, __name,
926                                 OC_MODE_CREATE | OC_MODE_OPEN | OC_MODE_REPLACE,
927                                 attributes, AR_WRITE, &finfo);
928                 if (result) {
929                         if (result == 0x87)
930                                 error = -ENAMETOOLONG;
931                         DPRINTK("ncp_create: %s/%s failed\n",
932                                 dentry->d_parent->d_name.name, dentry->d_name.name);
933                         goto out;
934                 }
935                 opmode = O_WRONLY;
936         }
937         finfo.access = opmode;
938         if (ncp_is_nfs_extras(server, finfo.volume)) {
939                 finfo.i.nfs.mode = mode;
940                 finfo.i.nfs.rdev = new_encode_dev(rdev);
941                 if (ncp_modify_nfs_info(server, finfo.volume,
942                                         finfo.i.dirEntNum,
943                                         mode, new_encode_dev(rdev)) != 0)
944                         goto out;
945         }
946
947         error = ncp_instantiate(dir, dentry, &finfo);
948 out:
949         unlock_kernel();
950         return error;
951 }
952
953 static int ncp_create(struct inode *dir, struct dentry *dentry, int mode,
954                 struct nameidata *nd)
955 {
956         return ncp_create_new(dir, dentry, mode, 0, 0);
957 }
958
959 static int ncp_mkdir(struct inode *dir, struct dentry *dentry, int mode)
960 {
961         struct ncp_entry_info finfo;
962         struct ncp_server *server = NCP_SERVER(dir);
963         int error, len;
964         __u8 __name[NCP_MAXPATHLEN + 1];
965
966         DPRINTK("ncp_mkdir: making %s/%s\n",
967                 dentry->d_parent->d_name.name, dentry->d_name.name);
968
969         error = -EIO;
970         lock_kernel();
971         if (!ncp_conn_valid(server))
972                 goto out;
973
974         ncp_age_dentry(server, dentry);
975         len = sizeof(__name);
976         error = ncp_io2vol(server, __name, &len, dentry->d_name.name,
977                            dentry->d_name.len, !ncp_preserve_case(dir));
978         if (error)
979                 goto out;
980
981         error = -EACCES;
982         if (ncp_open_create_file_or_subdir(server, dir, __name,
983                                            OC_MODE_CREATE, aDIR,
984                                            cpu_to_le16(0xffff),
985                                            &finfo) == 0)
986         {
987                 if (ncp_is_nfs_extras(server, finfo.volume)) {
988                         mode |= S_IFDIR;
989                         finfo.i.nfs.mode = mode;
990                         if (ncp_modify_nfs_info(server,
991                                                 finfo.volume,
992                                                 finfo.i.dirEntNum,
993                                                 mode, 0) != 0)
994                                 goto out;
995                 }
996                 error = ncp_instantiate(dir, dentry, &finfo);
997         }
998 out:
999         unlock_kernel();
1000         return error;
1001 }
1002
1003 static int ncp_rmdir(struct inode *dir, struct dentry *dentry)
1004 {
1005         struct ncp_server *server = NCP_SERVER(dir);
1006         int error, result, len;
1007         __u8 __name[NCP_MAXPATHLEN + 1];
1008
1009         DPRINTK("ncp_rmdir: removing %s/%s\n",
1010                 dentry->d_parent->d_name.name, dentry->d_name.name);
1011
1012         error = -EIO;
1013         lock_kernel();
1014         if (!ncp_conn_valid(server))
1015                 goto out;
1016
1017         error = -EBUSY;
1018         if (!d_unhashed(dentry))
1019                 goto out;
1020
1021         len = sizeof(__name);
1022         error = ncp_io2vol(server, __name, &len, dentry->d_name.name,
1023                            dentry->d_name.len, !ncp_preserve_case(dir));
1024         if (error)
1025                 goto out;
1026
1027         result = ncp_del_file_or_subdir(server, dir, __name);
1028         switch (result) {
1029                 case 0x00:
1030                         error = 0;
1031                         break;
1032                 case 0x85:      /* unauthorized to delete file */
1033                 case 0x8A:      /* unauthorized to delete file */
1034                         error = -EACCES;
1035                         break;
1036                 case 0x8F:
1037                 case 0x90:      /* read only */
1038                         error = -EPERM;
1039                         break;
1040                 case 0x9F:      /* in use by another client */
1041                         error = -EBUSY;
1042                         break;
1043                 case 0xA0:      /* directory not empty */
1044                         error = -ENOTEMPTY;
1045                         break;
1046                 case 0xFF:      /* someone deleted file */
1047                         error = -ENOENT;
1048                         break;
1049                 default:
1050                         error = -EACCES;
1051                         break;
1052         }
1053 out:
1054         unlock_kernel();
1055         return error;
1056 }
1057
1058 static int ncp_unlink(struct inode *dir, struct dentry *dentry)
1059 {
1060         struct inode *inode = dentry->d_inode;
1061         struct ncp_server *server;
1062         int error;
1063
1064         lock_kernel();
1065         server = NCP_SERVER(dir);
1066         DPRINTK("ncp_unlink: unlinking %s/%s\n",
1067                 dentry->d_parent->d_name.name, dentry->d_name.name);
1068         
1069         error = -EIO;
1070         if (!ncp_conn_valid(server))
1071                 goto out;
1072
1073         /*
1074          * Check whether to close the file ...
1075          */
1076         if (inode) {
1077                 PPRINTK("ncp_unlink: closing file\n");
1078                 ncp_make_closed(inode);
1079         }
1080
1081         error = ncp_del_file_or_subdir2(server, dentry);
1082 #ifdef CONFIG_NCPFS_STRONG
1083         /* 9C is Invalid path.. It should be 8F, 90 - read only, but
1084            it is not :-( */
1085         if ((error == 0x9C || error == 0x90) && server->m.flags & NCP_MOUNT_STRONG) { /* R/O */
1086                 error = ncp_force_unlink(dir, dentry);
1087         }
1088 #endif
1089         switch (error) {
1090                 case 0x00:
1091                         DPRINTK("ncp: removed %s/%s\n",
1092                                 dentry->d_parent->d_name.name, dentry->d_name.name);
1093                         break;
1094                 case 0x85:
1095                 case 0x8A:
1096                         error = -EACCES;
1097                         break;
1098                 case 0x8D:      /* some files in use */
1099                 case 0x8E:      /* all files in use */
1100                         error = -EBUSY;
1101                         break;
1102                 case 0x8F:      /* some read only */
1103                 case 0x90:      /* all read only */
1104                 case 0x9C:      /* !!! returned when in-use or read-only by NW4 */
1105                         error = -EPERM;
1106                         break;
1107                 case 0xFF:
1108                         error = -ENOENT;
1109                         break;
1110                 default:
1111                         error = -EACCES;
1112                         break;
1113         }
1114                 
1115 out:
1116         unlock_kernel();
1117         return error;
1118 }
1119
1120 static int ncp_rename(struct inode *old_dir, struct dentry *old_dentry,
1121                       struct inode *new_dir, struct dentry *new_dentry)
1122 {
1123         struct ncp_server *server = NCP_SERVER(old_dir);
1124         int error;
1125         int old_len, new_len;
1126         __u8 __old_name[NCP_MAXPATHLEN + 1], __new_name[NCP_MAXPATHLEN + 1];
1127
1128         DPRINTK("ncp_rename: %s/%s to %s/%s\n",
1129                 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1130                 new_dentry->d_parent->d_name.name, new_dentry->d_name.name);
1131
1132         error = -EIO;
1133         lock_kernel();
1134         if (!ncp_conn_valid(server))
1135                 goto out;
1136
1137         ncp_age_dentry(server, old_dentry);
1138         ncp_age_dentry(server, new_dentry);
1139
1140         old_len = sizeof(__old_name);
1141         error = ncp_io2vol(server, __old_name, &old_len,
1142                            old_dentry->d_name.name, old_dentry->d_name.len,
1143                            !ncp_preserve_case(old_dir));
1144         if (error)
1145                 goto out;
1146
1147         new_len = sizeof(__new_name);
1148         error = ncp_io2vol(server, __new_name, &new_len,
1149                            new_dentry->d_name.name, new_dentry->d_name.len,
1150                            !ncp_preserve_case(new_dir));
1151         if (error)
1152                 goto out;
1153
1154         error = ncp_ren_or_mov_file_or_subdir(server, old_dir, __old_name,
1155                                                       new_dir, __new_name);
1156 #ifdef CONFIG_NCPFS_STRONG
1157         if ((error == 0x90 || error == 0x8B || error == -EACCES) &&
1158                         server->m.flags & NCP_MOUNT_STRONG) {   /* RO */
1159                 error = ncp_force_rename(old_dir, old_dentry, __old_name,
1160                                          new_dir, new_dentry, __new_name);
1161         }
1162 #endif
1163         switch (error) {
1164                 case 0x00:
1165                         DPRINTK("ncp renamed %s -> %s.\n",
1166                                 old_dentry->d_name.name,new_dentry->d_name.name);
1167                         break;
1168                 case 0x9E:
1169                         error = -ENAMETOOLONG;
1170                         break;
1171                 case 0xFF:
1172                         error = -ENOENT;
1173                         break;
1174                 default:
1175                         error = -EACCES;
1176                         break;
1177         }
1178 out:
1179         unlock_kernel();
1180         return error;
1181 }
1182
1183 static int ncp_mknod(struct inode * dir, struct dentry *dentry,
1184                      int mode, dev_t rdev)
1185 {
1186         if (!new_valid_dev(rdev))
1187                 return -EINVAL;
1188         if (ncp_is_nfs_extras(NCP_SERVER(dir), NCP_FINFO(dir)->volNumber)) {
1189                 DPRINTK(KERN_DEBUG "ncp_mknod: mode = 0%o\n", mode);
1190                 return ncp_create_new(dir, dentry, mode, rdev, 0);
1191         }
1192         return -EPERM; /* Strange, but true */
1193 }
1194
1195 /* The following routines are taken directly from msdos-fs */
1196
1197 /* Linear day numbers of the respective 1sts in non-leap years. */
1198
1199 static int day_n[] =
1200 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0};
1201 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
1202
1203
1204 extern struct timezone sys_tz;
1205
1206 static int utc2local(int time)
1207 {
1208         return time - sys_tz.tz_minuteswest * 60;
1209 }
1210
1211 static int local2utc(int time)
1212 {
1213         return time + sys_tz.tz_minuteswest * 60;
1214 }
1215
1216 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
1217 int
1218 ncp_date_dos2unix(__le16 t, __le16 d)
1219 {
1220         unsigned short time = le16_to_cpu(t), date = le16_to_cpu(d);
1221         int month, year, secs;
1222
1223         /* first subtract and mask after that... Otherwise, if
1224            date == 0, bad things happen */
1225         month = ((date >> 5) - 1) & 15;
1226         year = date >> 9;
1227         secs = (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 +
1228                 86400 * ((date & 31) - 1 + day_n[month] + (year / 4) + 
1229                 year * 365 - ((year & 3) == 0 && month < 2 ? 1 : 0) + 3653);
1230         /* days since 1.1.70 plus 80's leap day */
1231         return local2utc(secs);
1232 }
1233
1234
1235 /* Convert linear UNIX date to a MS-DOS time/date pair. */
1236 void
1237 ncp_date_unix2dos(int unix_date, __le16 *time, __le16 *date)
1238 {
1239         int day, year, nl_day, month;
1240
1241         unix_date = utc2local(unix_date);
1242         *time = cpu_to_le16(
1243                 (unix_date % 60) / 2 + (((unix_date / 60) % 60) << 5) +
1244                 (((unix_date / 3600) % 24) << 11));
1245         day = unix_date / 86400 - 3652;
1246         year = day / 365;
1247         if ((year + 3) / 4 + 365 * year > day)
1248                 year--;
1249         day -= (year + 3) / 4 + 365 * year;
1250         if (day == 59 && !(year & 3)) {
1251                 nl_day = day;
1252                 month = 2;
1253         } else {
1254                 nl_day = (year & 3) || day <= 59 ? day : day - 1;
1255                 for (month = 0; month < 12; month++)
1256                         if (day_n[month] > nl_day)
1257                                 break;
1258         }
1259         *date = cpu_to_le16(nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9));
1260 }