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
14 #include <linux/time.h>
15 #include <linux/errno.h>
16 #include <linux/stat.h>
17 #include <linux/kernel.h>
18 #include <linux/vmalloc.h>
20 #include <asm/uaccess.h>
21 #include <asm/byteorder.h>
23 #include <linux/ncp_fs.h>
25 #include "ncplib_kernel.h"
27 static void ncp_read_volume_list(struct file *, void *, filldir_t,
28 struct ncp_cache_control *);
29 static void ncp_do_readdir(struct file *, void *, filldir_t,
30 struct ncp_cache_control *);
32 static int ncp_readdir(struct file *, void *, filldir_t);
34 static int ncp_create(struct inode *, struct dentry *, int, struct nameidata *);
35 static struct dentry *ncp_lookup(struct inode *, struct dentry *, struct nameidata *);
36 static int ncp_unlink(struct inode *, struct dentry *);
37 static int ncp_mkdir(struct inode *, struct dentry *, int);
38 static int ncp_rmdir(struct inode *, struct dentry *);
39 static int ncp_rename(struct inode *, struct dentry *,
40 struct inode *, struct dentry *);
41 static int ncp_mknod(struct inode * dir, struct dentry *dentry,
42 int mode, dev_t rdev);
43 #if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
44 extern int ncp_symlink(struct inode *, struct dentry *, const char *);
46 #define ncp_symlink NULL
49 const struct file_operations ncp_dir_operations =
51 .llseek = generic_file_llseek,
52 .read = generic_read_dir,
53 .readdir = ncp_readdir,
54 .unlocked_ioctl = ncp_ioctl,
56 .compat_ioctl = ncp_compat_ioctl,
60 const struct inode_operations ncp_dir_inode_operations =
65 .symlink = ncp_symlink,
70 .setattr = ncp_notify_change,
74 * Dentry operations routines
76 static int ncp_lookup_validate(struct dentry *, struct nameidata *);
77 static int ncp_hash_dentry(const struct dentry *, const struct inode *,
79 static int ncp_compare_dentry(const struct dentry *, const struct inode *,
80 const struct dentry *, const struct inode *,
81 unsigned int, const char *, const struct qstr *);
82 static int ncp_delete_dentry(const struct dentry *);
84 static const struct dentry_operations ncp_dentry_operations =
86 .d_revalidate = ncp_lookup_validate,
87 .d_hash = ncp_hash_dentry,
88 .d_compare = ncp_compare_dentry,
89 .d_delete = ncp_delete_dentry,
92 const struct dentry_operations ncp_root_dentry_operations =
94 .d_hash = ncp_hash_dentry,
95 .d_compare = ncp_compare_dentry,
96 .d_delete = ncp_delete_dentry,
100 #define ncp_namespace(i) (NCP_SERVER(i)->name_space[NCP_FINFO(i)->volNumber])
102 static inline int ncp_preserve_entry_case(struct inode *i, __u32 nscreator)
104 #ifdef CONFIG_NCPFS_SMALLDOS
105 int ns = ncp_namespace(i);
107 if ((ns == NW_NS_DOS)
108 #ifdef CONFIG_NCPFS_OS2_NS
109 || ((ns == NW_NS_OS2) && (nscreator == NW_NS_DOS))
110 #endif /* CONFIG_NCPFS_OS2_NS */
113 #endif /* CONFIG_NCPFS_SMALLDOS */
117 #define ncp_preserve_case(i) (ncp_namespace(i) != NW_NS_DOS)
119 static inline int ncp_case_sensitive(const struct inode *i)
121 #ifdef CONFIG_NCPFS_NFS_NS
122 return ncp_namespace(i) == NW_NS_NFS;
125 #endif /* CONFIG_NCPFS_NFS_NS */
129 * Note: leave the hash unchanged if the directory
133 ncp_hash_dentry(const struct dentry *dentry, const struct inode *inode,
136 if (!ncp_case_sensitive(inode)) {
137 struct super_block *sb = dentry->d_sb;
142 t = NCP_IO_TABLE(sb);
143 hash = init_name_hash();
144 for (i=0; i<this->len ; i++)
145 hash = partial_name_hash(ncp_tolower(t, this->name[i]),
147 this->hash = end_name_hash(hash);
153 ncp_compare_dentry(const struct dentry *parent, const struct inode *pinode,
154 const struct dentry *dentry, const struct inode *inode,
155 unsigned int len, const char *str, const struct qstr *name)
157 if (len != name->len)
160 if (ncp_case_sensitive(pinode))
161 return strncmp(str, name->name, len);
163 return ncp_strnicmp(NCP_IO_TABLE(pinode->i_sb), str, name->name, len);
167 * This is the callback from dput() when d_count is going to 0.
168 * We use this to unhash dentries with bad inodes.
169 * Closing files can be safely postponed until iput() - it's done there anyway.
172 ncp_delete_dentry(const struct dentry * dentry)
174 struct inode *inode = dentry->d_inode;
177 if (is_bad_inode(inode))
181 /* N.B. Unhash negative dentries? */
187 ncp_single_volume(struct ncp_server *server)
189 return (server->m.mounted_vol[0] != '\0');
192 static inline int ncp_is_server_root(struct inode *inode)
194 return (!ncp_single_volume(NCP_SERVER(inode)) &&
195 inode == inode->i_sb->s_root->d_inode);
200 * This is the callback when the dcache has a lookup hit.
204 #ifdef CONFIG_NCPFS_STRONG
205 /* try to delete a readonly file (NW R bit set) */
208 ncp_force_unlink(struct inode *dir, struct dentry* dentry)
211 struct nw_modify_dos_info info;
215 memset(&info, 0, sizeof(info));
217 /* remove the Read-Only flag on the NW server */
218 inode = dentry->d_inode;
220 old_nwattr = NCP_FINFO(inode)->nwattr;
221 info.attributes = old_nwattr & ~(aRONLY|aDELETEINHIBIT|aRENAMEINHIBIT);
222 res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(inode), inode, NULL, DM_ATTRIBUTES, &info);
226 /* now try again the delete operation */
227 res = ncp_del_file_or_subdir2(NCP_SERVER(dir), dentry);
229 if (res) /* delete failed, set R bit again */
231 info.attributes = old_nwattr;
232 res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(inode), inode, NULL, DM_ATTRIBUTES, &info);
239 #endif /* CONFIG_NCPFS_STRONG */
241 #ifdef CONFIG_NCPFS_STRONG
243 ncp_force_rename(struct inode *old_dir, struct dentry* old_dentry, char *_old_name,
244 struct inode *new_dir, struct dentry* new_dentry, char *_new_name)
246 struct nw_modify_dos_info info;
248 struct inode *old_inode = old_dentry->d_inode;
249 __le32 old_nwattr = NCP_FINFO(old_inode)->nwattr;
250 __le32 new_nwattr = 0; /* shut compiler warning */
251 int old_nwattr_changed = 0;
252 int new_nwattr_changed = 0;
254 memset(&info, 0, sizeof(info));
256 /* remove the Read-Only flag on the NW server */
258 info.attributes = old_nwattr & ~(aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT);
259 res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(old_inode), old_inode, NULL, DM_ATTRIBUTES, &info);
261 old_nwattr_changed = 1;
262 if (new_dentry && new_dentry->d_inode) {
263 new_nwattr = NCP_FINFO(new_dentry->d_inode)->nwattr;
264 info.attributes = new_nwattr & ~(aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT);
265 res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(new_dir), new_dir, _new_name, DM_ATTRIBUTES, &info);
267 new_nwattr_changed = 1;
269 /* now try again the rename operation */
270 /* but only if something really happened */
271 if (new_nwattr_changed || old_nwattr_changed) {
272 res = ncp_ren_or_mov_file_or_subdir(NCP_SERVER(old_dir),
278 /* file was successfully renamed, so:
279 do not set attributes on old file - it no longer exists
280 copy attributes from old file to new */
281 new_nwattr_changed = old_nwattr_changed;
282 new_nwattr = old_nwattr;
283 old_nwattr_changed = 0;
286 if (old_nwattr_changed) {
287 info.attributes = old_nwattr;
288 res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(old_inode), old_inode, NULL, DM_ATTRIBUTES, &info);
291 if (new_nwattr_changed) {
292 info.attributes = new_nwattr;
293 res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(new_dir), new_dir, _new_name, DM_ATTRIBUTES, &info);
298 #endif /* CONFIG_NCPFS_STRONG */
302 ncp_lookup_validate(struct dentry *dentry, struct nameidata *nd)
304 struct ncp_server *server;
305 struct dentry *parent;
307 struct ncp_entry_info finfo;
308 int res, val = 0, len;
309 __u8 __name[NCP_MAXPATHLEN + 1];
311 parent = dget_parent(dentry);
312 dir = parent->d_inode;
314 if (!dentry->d_inode)
317 server = NCP_SERVER(dir);
321 * The default validation is based on dentry age:
322 * We set the max age at mount time. (But each
323 * successful server lookup renews the timestamp.)
325 val = NCP_TEST_AGE(server, dentry);
329 DDPRINTK("ncp_lookup_validate: %s/%s not valid, age=%ld, server lookup\n",
330 dentry->d_parent->d_name.name, dentry->d_name.name,
331 NCP_GET_AGE(dentry));
333 len = sizeof(__name);
334 if (ncp_is_server_root(dir)) {
335 res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
336 dentry->d_name.len, 1);
338 res = ncp_lookup_volume(server, __name, &(finfo.i));
340 ncp_update_known_namespace(server, finfo.i.volNumber, NULL);
343 res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
344 dentry->d_name.len, !ncp_preserve_case(dir));
346 res = ncp_obtain_info(server, dir, __name, &(finfo.i));
348 finfo.volume = finfo.i.volNumber;
349 DDPRINTK("ncp_lookup_validate: looked for %s/%s, res=%d\n",
350 dentry->d_parent->d_name.name, __name, res);
352 * If we didn't find it, or if it has a different dirEntNum to
353 * what we remember, it's not valid any more.
356 struct inode *inode = dentry->d_inode;
358 mutex_lock(&inode->i_mutex);
359 if (finfo.i.dirEntNum == NCP_FINFO(inode)->dirEntNum) {
360 ncp_new_dentry(dentry);
363 DDPRINTK("ncp_lookup_validate: found, but dirEntNum changed\n");
365 ncp_update_inode2(inode, &finfo);
366 mutex_unlock(&inode->i_mutex);
370 DDPRINTK("ncp_lookup_validate: result=%d\n", val);
375 static struct dentry *
376 ncp_dget_fpos(struct dentry *dentry, struct dentry *parent, unsigned long fpos)
378 struct dentry *dent = dentry;
379 struct list_head *next;
381 if (d_validate(dent, parent)) {
382 if (dent->d_name.len <= NCP_MAXPATHLEN &&
383 (unsigned long)dent->d_fsdata == fpos) {
384 if (!dent->d_inode) {
393 /* If a pointer is invalid, we search the dentry. */
394 spin_lock(&parent->d_lock);
395 next = parent->d_subdirs.next;
396 while (next != &parent->d_subdirs) {
397 dent = list_entry(next, struct dentry, d_u.d_child);
398 if ((unsigned long)dent->d_fsdata == fpos) {
403 spin_unlock(&parent->d_lock);
408 spin_unlock(&parent->d_lock);
415 static time_t ncp_obtain_mtime(struct dentry *dentry)
417 struct inode *inode = dentry->d_inode;
418 struct ncp_server *server = NCP_SERVER(inode);
419 struct nw_info_struct i;
421 if (!ncp_conn_valid(server) || ncp_is_server_root(inode))
424 if (ncp_obtain_info(server, inode, NULL, &i))
427 return ncp_date_dos2unix(i.modifyTime, i.modifyDate);
430 static int ncp_readdir(struct file *filp, void *dirent, filldir_t filldir)
432 struct dentry *dentry = filp->f_path.dentry;
433 struct inode *inode = dentry->d_inode;
434 struct page *page = NULL;
435 struct ncp_server *server = NCP_SERVER(inode);
436 union ncp_dir_cache *cache = NULL;
437 struct ncp_cache_control ctl;
438 int result, mtime_valid = 0;
444 DDPRINTK("ncp_readdir: reading %s/%s, pos=%d\n",
445 dentry->d_parent->d_name.name, dentry->d_name.name,
449 /* Do not generate '.' and '..' when server is dead. */
450 if (!ncp_conn_valid(server))
454 if (filp->f_pos == 0) {
455 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR))
459 if (filp->f_pos == 1) {
460 if (filldir(dirent, "..", 2, 1, parent_ino(dentry), DT_DIR))
465 page = grab_cache_page(&inode->i_data, 0);
469 ctl.cache = cache = kmap(page);
470 ctl.head = cache->head;
472 if (!PageUptodate(page) || !ctl.head.eof)
475 if (filp->f_pos == 2) {
476 if (jiffies - ctl.head.time >= NCP_MAX_AGE(server))
479 mtime = ncp_obtain_mtime(dentry);
481 if ((!mtime) || (mtime != ctl.head.mtime))
485 if (filp->f_pos > ctl.head.end)
488 ctl.fpos = filp->f_pos + (NCP_DIRCACHE_START - 2);
489 ctl.ofs = ctl.fpos / NCP_DIRCACHE_SIZE;
490 ctl.idx = ctl.fpos % NCP_DIRCACHE_SIZE;
494 ctl.page = find_lock_page(&inode->i_data, ctl.ofs);
497 ctl.cache = kmap(ctl.page);
498 if (!PageUptodate(ctl.page))
501 while (ctl.idx < NCP_DIRCACHE_SIZE) {
505 dent = ncp_dget_fpos(ctl.cache->dentry[ctl.idx],
506 dentry, filp->f_pos);
509 res = filldir(dirent, dent->d_name.name,
510 dent->d_name.len, filp->f_pos,
511 dent->d_inode->i_ino, DT_UNKNOWN);
517 if (filp->f_pos > ctl.head.end)
522 SetPageUptodate(ctl.page);
523 unlock_page(ctl.page);
524 page_cache_release(ctl.page);
533 unlock_page(ctl.page);
534 page_cache_release(ctl.page);
539 ncp_invalidate_dircache_entries(dentry);
541 mtime = ncp_obtain_mtime(dentry);
544 ctl.head.mtime = mtime;
545 ctl.head.time = jiffies;
549 ctl.idx = NCP_DIRCACHE_START;
553 if (ncp_is_server_root(inode)) {
554 ncp_read_volume_list(filp, dirent, filldir, &ctl);
556 ncp_do_readdir(filp, dirent, filldir, &ctl);
558 ctl.head.end = ctl.fpos - 1;
559 ctl.head.eof = ctl.valid;
563 SetPageUptodate(ctl.page);
564 unlock_page(ctl.page);
565 page_cache_release(ctl.page);
568 cache->head = ctl.head;
570 SetPageUptodate(page);
572 page_cache_release(page);
579 ncp_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
580 struct ncp_cache_control *ctrl, struct ncp_entry_info *entry,
583 struct dentry *newdent, *dentry = filp->f_path.dentry;
584 struct inode *dir = dentry->d_inode;
585 struct ncp_cache_control ctl = *ctrl;
590 __u8 __name[NCP_MAXPATHLEN + 1];
592 qname.len = sizeof(__name);
593 if (ncp_vol2io(NCP_SERVER(dir), __name, &qname.len,
594 entry->i.entryName, entry->i.nameLen,
595 !ncp_preserve_entry_case(dir, entry->i.NSCreator)))
596 return 1; /* I'm not sure */
599 qname.hash = full_name_hash(qname.name, qname.len);
601 if (dentry->d_op && dentry->d_op->d_hash)
602 if (dentry->d_op->d_hash(dentry, dentry->d_inode, &qname) != 0)
605 newdent = d_lookup(dentry, &qname);
608 newdent = d_alloc(dentry, &qname);
614 /* If case sensitivity changed for this volume, all entries below this one
615 should be thrown away. This entry itself is not affected, as its case
616 sensitivity is controlled by its own parent. */
618 shrink_dcache_parent(newdent);
621 * NetWare's OS2 namespace is case preserving yet case
622 * insensitive. So we update dentry's name as received from
623 * server. Parent dir's i_mutex is locked because we're in
626 dentry_update_name_case(newdent, &qname);
629 if (!newdent->d_inode) {
633 entry->ino = iunique(dir->i_sb, 2);
634 inode = ncp_iget(dir->i_sb, entry);
636 d_set_d_op(newdent, &ncp_dentry_operations);
637 d_instantiate(newdent, inode);
642 struct inode *inode = newdent->d_inode;
644 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
645 ncp_update_inode2(inode, entry);
646 mutex_unlock(&inode->i_mutex);
649 if (newdent->d_inode) {
650 ino = newdent->d_inode->i_ino;
651 newdent->d_fsdata = (void *) ctl.fpos;
652 ncp_new_dentry(newdent);
655 if (ctl.idx >= NCP_DIRCACHE_SIZE) {
658 SetPageUptodate(ctl.page);
659 unlock_page(ctl.page);
660 page_cache_release(ctl.page);
663 ctl.idx -= NCP_DIRCACHE_SIZE;
665 ctl.page = grab_cache_page(&dir->i_data, ctl.ofs);
667 ctl.cache = kmap(ctl.page);
670 ctl.cache->dentry[ctl.idx] = newdent;
677 if (!ctl.filled && (ctl.fpos == filp->f_pos)) {
679 ino = find_inode_number(dentry, &qname);
681 ino = iunique(dir->i_sb, 2);
682 ctl.filled = filldir(dirent, qname.name, qname.len,
683 filp->f_pos, ino, DT_UNKNOWN);
690 return (ctl.valid || !ctl.filled);
694 ncp_read_volume_list(struct file *filp, void *dirent, filldir_t filldir,
695 struct ncp_cache_control *ctl)
697 struct dentry *dentry = filp->f_path.dentry;
698 struct inode *inode = dentry->d_inode;
699 struct ncp_server *server = NCP_SERVER(inode);
700 struct ncp_volume_info info;
701 struct ncp_entry_info entry;
704 DPRINTK("ncp_read_volume_list: pos=%ld\n",
705 (unsigned long) filp->f_pos);
707 for (i = 0; i < NCP_NUMBER_OF_VOLUMES; i++) {
710 if (ncp_get_volume_info_with_number(server, i, &info) != 0)
712 if (!strlen(info.volume_name))
715 DPRINTK("ncp_read_volume_list: found vol: %s\n",
718 if (ncp_lookup_volume(server, info.volume_name,
720 DPRINTK("ncpfs: could not lookup vol %s\n",
724 inval_dentry = ncp_update_known_namespace(server, entry.i.volNumber, NULL);
725 entry.volume = entry.i.volNumber;
726 if (!ncp_fill_cache(filp, dirent, filldir, ctl, &entry, inval_dentry))
732 ncp_do_readdir(struct file *filp, void *dirent, filldir_t filldir,
733 struct ncp_cache_control *ctl)
735 struct dentry *dentry = filp->f_path.dentry;
736 struct inode *dir = dentry->d_inode;
737 struct ncp_server *server = NCP_SERVER(dir);
738 struct nw_search_sequence seq;
739 struct ncp_entry_info entry;
745 DPRINTK("ncp_do_readdir: %s/%s, fpos=%ld\n",
746 dentry->d_parent->d_name.name, dentry->d_name.name,
747 (unsigned long) filp->f_pos);
748 PPRINTK("ncp_do_readdir: init %s, volnum=%d, dirent=%u\n",
749 dentry->d_name.name, NCP_FINFO(dir)->volNumber,
750 NCP_FINFO(dir)->dirEntNum);
752 err = ncp_initialize_search(server, dir, &seq);
754 DPRINTK("ncp_do_readdir: init failed, err=%d\n", err);
757 /* We MUST NOT use server->buffer_size handshaked with server if we are
758 using UDP, as for UDP server uses max. buffer size determined by
759 MTU, and for TCP server uses hardwired value 65KB (== 66560 bytes).
760 So we use 128KB, just to be sure, as there is no way how to know
761 this value in advance. */
763 buf = vmalloc(bufsize);
771 err = ncp_search_for_fileset(server, &seq, &more, &cnt, buf, bufsize, &rpl, &rpls);
774 if (!cnt) /* prevent endless loop */
779 if (rpls < offsetof(struct nw_info_struct, entryName))
780 break; /* short packet */
781 ncp_extract_file_info(rpl, &entry.i);
782 onerpl = offsetof(struct nw_info_struct, entryName) + entry.i.nameLen;
784 break; /* short packet */
785 (void)ncp_obtain_nfs_info(server, &entry.i);
788 entry.volume = entry.i.volNumber;
789 if (!ncp_fill_cache(filp, dirent, filldir, ctl, &entry, 0))
797 int ncp_conn_logged_in(struct super_block *sb)
799 struct ncp_server* server = NCP_SBP(sb);
802 if (ncp_single_volume(server)) {
808 __u8 __name[NCP_MAXPATHLEN + 1];
810 len = sizeof(__name);
811 result = ncp_io2vol(server, __name, &len, server->m.mounted_vol,
812 strlen(server->m.mounted_vol), 1);
816 if (ncp_get_volume_root(server, __name, &volNumber, &dirEntNum, &DosDirNum)) {
817 PPRINTK("ncp_conn_logged_in: %s not found\n",
818 server->m.mounted_vol);
823 struct inode* ino = dent->d_inode;
825 ncp_update_known_namespace(server, volNumber, NULL);
826 NCP_FINFO(ino)->volNumber = volNumber;
827 NCP_FINFO(ino)->dirEntNum = dirEntNum;
828 NCP_FINFO(ino)->DosDirNum = DosDirNum;
831 DPRINTK("ncpfs: sb->s_root->d_inode == NULL!\n");
834 DPRINTK("ncpfs: sb->s_root == NULL!\n");
843 static struct dentry *ncp_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
845 struct ncp_server *server = NCP_SERVER(dir);
846 struct inode *inode = NULL;
847 struct ncp_entry_info finfo;
849 __u8 __name[NCP_MAXPATHLEN + 1];
852 if (!ncp_conn_valid(server))
855 PPRINTK("ncp_lookup: server lookup for %s/%s\n",
856 dentry->d_parent->d_name.name, dentry->d_name.name);
858 len = sizeof(__name);
859 if (ncp_is_server_root(dir)) {
860 res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
861 dentry->d_name.len, 1);
863 res = ncp_lookup_volume(server, __name, &(finfo.i));
865 ncp_update_known_namespace(server, finfo.i.volNumber, NULL);
867 res = ncp_io2vol(server, __name, &len, dentry->d_name.name,
868 dentry->d_name.len, !ncp_preserve_case(dir));
870 res = ncp_obtain_info(server, dir, __name, &(finfo.i));
872 PPRINTK("ncp_lookup: looked for %s/%s, res=%d\n",
873 dentry->d_parent->d_name.name, __name, res);
875 * If we didn't find an entry, make a negative dentry.
881 * Create an inode for the entry.
884 finfo.ino = iunique(dir->i_sb, 2);
885 finfo.volume = finfo.i.volNumber;
887 inode = ncp_iget(dir->i_sb, &finfo);
890 ncp_new_dentry(dentry);
892 d_set_d_op(dentry, &ncp_dentry_operations);
893 d_add(dentry, inode);
898 PPRINTK("ncp_lookup: result=%d\n", error);
899 return ERR_PTR(error);
903 * This code is common to create, mkdir, and mknod.
905 static int ncp_instantiate(struct inode *dir, struct dentry *dentry,
906 struct ncp_entry_info *finfo)
911 finfo->ino = iunique(dir->i_sb, 2);
912 inode = ncp_iget(dir->i_sb, finfo);
915 d_instantiate(dentry,inode);
921 PPRINTK("ncp_instantiate: %s/%s failed, closing file\n",
922 dentry->d_parent->d_name.name, dentry->d_name.name);
923 ncp_close_file(NCP_SERVER(dir), finfo->file_handle);
927 int ncp_create_new(struct inode *dir, struct dentry *dentry, int mode,
928 dev_t rdev, __le32 attributes)
930 struct ncp_server *server = NCP_SERVER(dir);
931 struct ncp_entry_info finfo;
932 int error, result, len;
934 __u8 __name[NCP_MAXPATHLEN + 1];
936 PPRINTK("ncp_create_new: creating %s/%s, mode=%x\n",
937 dentry->d_parent->d_name.name, dentry->d_name.name, mode);
939 ncp_age_dentry(server, dentry);
940 len = sizeof(__name);
941 error = ncp_io2vol(server, __name, &len, dentry->d_name.name,
942 dentry->d_name.len, !ncp_preserve_case(dir));
949 (server->m.flags & NCP_MOUNT_EXTRAS) &&
951 attributes |= aSYSTEM | aSHARED;
953 result = ncp_open_create_file_or_subdir(server, dir, __name,
954 OC_MODE_CREATE | OC_MODE_OPEN | OC_MODE_REPLACE,
955 attributes, AR_READ | AR_WRITE, &finfo);
958 result = ncp_open_create_file_or_subdir(server, dir, __name,
959 OC_MODE_CREATE | OC_MODE_OPEN | OC_MODE_REPLACE,
960 attributes, AR_WRITE, &finfo);
963 error = -ENAMETOOLONG;
966 DPRINTK("ncp_create: %s/%s failed\n",
967 dentry->d_parent->d_name.name, dentry->d_name.name);
972 finfo.access = opmode;
973 if (ncp_is_nfs_extras(server, finfo.volume)) {
974 finfo.i.nfs.mode = mode;
975 finfo.i.nfs.rdev = new_encode_dev(rdev);
976 if (ncp_modify_nfs_info(server, finfo.volume,
978 mode, new_encode_dev(rdev)) != 0)
982 error = ncp_instantiate(dir, dentry, &finfo);
987 static int ncp_create(struct inode *dir, struct dentry *dentry, int mode,
988 struct nameidata *nd)
990 return ncp_create_new(dir, dentry, mode, 0, 0);
993 static int ncp_mkdir(struct inode *dir, struct dentry *dentry, int mode)
995 struct ncp_entry_info finfo;
996 struct ncp_server *server = NCP_SERVER(dir);
998 __u8 __name[NCP_MAXPATHLEN + 1];
1000 DPRINTK("ncp_mkdir: making %s/%s\n",
1001 dentry->d_parent->d_name.name, dentry->d_name.name);
1003 ncp_age_dentry(server, dentry);
1004 len = sizeof(__name);
1005 error = ncp_io2vol(server, __name, &len, dentry->d_name.name,
1006 dentry->d_name.len, !ncp_preserve_case(dir));
1010 error = ncp_open_create_file_or_subdir(server, dir, __name,
1011 OC_MODE_CREATE, aDIR,
1012 cpu_to_le16(0xffff),
1015 if (ncp_is_nfs_extras(server, finfo.volume)) {
1017 finfo.i.nfs.mode = mode;
1018 if (ncp_modify_nfs_info(server,
1024 error = ncp_instantiate(dir, dentry, &finfo);
1025 } else if (error > 0) {
1032 static int ncp_rmdir(struct inode *dir, struct dentry *dentry)
1034 struct ncp_server *server = NCP_SERVER(dir);
1035 int error, result, len;
1036 __u8 __name[NCP_MAXPATHLEN + 1];
1038 DPRINTK("ncp_rmdir: removing %s/%s\n",
1039 dentry->d_parent->d_name.name, dentry->d_name.name);
1042 if (!d_unhashed(dentry))
1045 len = sizeof(__name);
1046 error = ncp_io2vol(server, __name, &len, dentry->d_name.name,
1047 dentry->d_name.len, !ncp_preserve_case(dir));
1051 result = ncp_del_file_or_subdir(server, dir, __name);
1056 case 0x85: /* unauthorized to delete file */
1057 case 0x8A: /* unauthorized to delete file */
1061 case 0x90: /* read only */
1064 case 0x9F: /* in use by another client */
1067 case 0xA0: /* directory not empty */
1070 case 0xFF: /* someone deleted file */
1074 error = result < 0 ? result : -EACCES;
1081 static int ncp_unlink(struct inode *dir, struct dentry *dentry)
1083 struct inode *inode = dentry->d_inode;
1084 struct ncp_server *server;
1087 server = NCP_SERVER(dir);
1088 DPRINTK("ncp_unlink: unlinking %s/%s\n",
1089 dentry->d_parent->d_name.name, dentry->d_name.name);
1092 * Check whether to close the file ...
1095 PPRINTK("ncp_unlink: closing file\n");
1096 ncp_make_closed(inode);
1099 error = ncp_del_file_or_subdir2(server, dentry);
1100 #ifdef CONFIG_NCPFS_STRONG
1101 /* 9C is Invalid path.. It should be 8F, 90 - read only, but
1103 if ((error == 0x9C || error == 0x90) && server->m.flags & NCP_MOUNT_STRONG) { /* R/O */
1104 error = ncp_force_unlink(dir, dentry);
1109 DPRINTK("ncp: removed %s/%s\n",
1110 dentry->d_parent->d_name.name, dentry->d_name.name);
1116 case 0x8D: /* some files in use */
1117 case 0x8E: /* all files in use */
1120 case 0x8F: /* some read only */
1121 case 0x90: /* all read only */
1122 case 0x9C: /* !!! returned when in-use or read-only by NW4 */
1129 error = error < 0 ? error : -EACCES;
1135 static int ncp_rename(struct inode *old_dir, struct dentry *old_dentry,
1136 struct inode *new_dir, struct dentry *new_dentry)
1138 struct ncp_server *server = NCP_SERVER(old_dir);
1140 int old_len, new_len;
1141 __u8 __old_name[NCP_MAXPATHLEN + 1], __new_name[NCP_MAXPATHLEN + 1];
1143 DPRINTK("ncp_rename: %s/%s to %s/%s\n",
1144 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1145 new_dentry->d_parent->d_name.name, new_dentry->d_name.name);
1147 ncp_age_dentry(server, old_dentry);
1148 ncp_age_dentry(server, new_dentry);
1150 old_len = sizeof(__old_name);
1151 error = ncp_io2vol(server, __old_name, &old_len,
1152 old_dentry->d_name.name, old_dentry->d_name.len,
1153 !ncp_preserve_case(old_dir));
1157 new_len = sizeof(__new_name);
1158 error = ncp_io2vol(server, __new_name, &new_len,
1159 new_dentry->d_name.name, new_dentry->d_name.len,
1160 !ncp_preserve_case(new_dir));
1164 error = ncp_ren_or_mov_file_or_subdir(server, old_dir, __old_name,
1165 new_dir, __new_name);
1166 #ifdef CONFIG_NCPFS_STRONG
1167 if ((error == 0x90 || error == 0x8B || error == -EACCES) &&
1168 server->m.flags & NCP_MOUNT_STRONG) { /* RO */
1169 error = ncp_force_rename(old_dir, old_dentry, __old_name,
1170 new_dir, new_dentry, __new_name);
1175 DPRINTK("ncp renamed %s -> %s.\n",
1176 old_dentry->d_name.name,new_dentry->d_name.name);
1179 error = -ENAMETOOLONG;
1185 error = error < 0 ? error : -EACCES;
1192 static int ncp_mknod(struct inode * dir, struct dentry *dentry,
1193 int mode, dev_t rdev)
1195 if (!new_valid_dev(rdev))
1197 if (ncp_is_nfs_extras(NCP_SERVER(dir), NCP_FINFO(dir)->volNumber)) {
1198 DPRINTK(KERN_DEBUG "ncp_mknod: mode = 0%o\n", mode);
1199 return ncp_create_new(dir, dentry, mode, rdev, 0);
1201 return -EPERM; /* Strange, but true */
1204 /* The following routines are taken directly from msdos-fs */
1206 /* Linear day numbers of the respective 1sts in non-leap years. */
1208 static int day_n[] =
1209 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0};
1210 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
1213 extern struct timezone sys_tz;
1215 static int utc2local(int time)
1217 return time - sys_tz.tz_minuteswest * 60;
1220 static int local2utc(int time)
1222 return time + sys_tz.tz_minuteswest * 60;
1225 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
1227 ncp_date_dos2unix(__le16 t, __le16 d)
1229 unsigned short time = le16_to_cpu(t), date = le16_to_cpu(d);
1230 int month, year, secs;
1232 /* first subtract and mask after that... Otherwise, if
1233 date == 0, bad things happen */
1234 month = ((date >> 5) - 1) & 15;
1236 secs = (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 +
1237 86400 * ((date & 31) - 1 + day_n[month] + (year / 4) +
1238 year * 365 - ((year & 3) == 0 && month < 2 ? 1 : 0) + 3653);
1239 /* days since 1.1.70 plus 80's leap day */
1240 return local2utc(secs);
1244 /* Convert linear UNIX date to a MS-DOS time/date pair. */
1246 ncp_date_unix2dos(int unix_date, __le16 *time, __le16 *date)
1248 int day, year, nl_day, month;
1250 unix_date = utc2local(unix_date);
1251 *time = cpu_to_le16(
1252 (unix_date % 60) / 2 + (((unix_date / 60) % 60) << 5) +
1253 (((unix_date / 3600) % 24) << 11));
1254 day = unix_date / 86400 - 3652;
1256 if ((year + 3) / 4 + 365 * year > day)
1258 day -= (year + 3) / 4 + 365 * year;
1259 if (day == 59 && !(year & 3)) {
1263 nl_day = (year & 3) || day <= 59 ? day : day - 1;
1264 for (month = 1; month < 12; month++)
1265 if (day_n[month] > nl_day)
1268 *date = cpu_to_le16(nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9));