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/slab.h>
19 #include <linux/vmalloc.h>
21 #include <asm/uaccess.h>
22 #include <asm/byteorder.h>
23 #include <linux/smp_lock.h>
25 #include <linux/ncp_fs.h>
27 #include "ncplib_kernel.h"
29 static void ncp_read_volume_list(struct file
*, void *, filldir_t
,
30 struct ncp_cache_control
*);
31 static void ncp_do_readdir(struct file
*, void *, filldir_t
,
32 struct ncp_cache_control
*);
34 static int ncp_readdir(struct file
*, void *, filldir_t
);
36 static int ncp_create(struct inode
*, struct dentry
*, int, struct nameidata
*);
37 static struct dentry
*ncp_lookup(struct inode
*, struct dentry
*, struct nameidata
*);
38 static int ncp_unlink(struct inode
*, struct dentry
*);
39 static int ncp_mkdir(struct inode
*, struct dentry
*, int);
40 static int ncp_rmdir(struct inode
*, struct dentry
*);
41 static int ncp_rename(struct inode
*, struct dentry
*,
42 struct inode
*, struct dentry
*);
43 static int ncp_mknod(struct inode
* dir
, struct dentry
*dentry
,
44 int mode
, dev_t rdev
);
45 #if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
46 extern int ncp_symlink(struct inode
*, struct dentry
*, const char *);
48 #define ncp_symlink NULL
51 const struct file_operations ncp_dir_operations
=
53 .read
= generic_read_dir
,
54 .readdir
= ncp_readdir
,
58 struct inode_operations ncp_dir_inode_operations
=
63 .symlink
= ncp_symlink
,
68 .setattr
= ncp_notify_change
,
72 * Dentry operations routines
74 static int ncp_lookup_validate(struct dentry
*, struct nameidata
*);
75 static int ncp_hash_dentry(struct dentry
*, struct qstr
*);
76 static int ncp_compare_dentry (struct dentry
*, struct qstr
*, struct qstr
*);
77 static int ncp_delete_dentry(struct dentry
*);
79 static struct dentry_operations ncp_dentry_operations
=
81 .d_revalidate
= ncp_lookup_validate
,
82 .d_hash
= ncp_hash_dentry
,
83 .d_compare
= ncp_compare_dentry
,
84 .d_delete
= ncp_delete_dentry
,
87 struct dentry_operations ncp_root_dentry_operations
=
89 .d_hash
= ncp_hash_dentry
,
90 .d_compare
= ncp_compare_dentry
,
91 .d_delete
= ncp_delete_dentry
,
96 * Note: leave the hash unchanged if the directory
100 ncp_hash_dentry(struct dentry
*dentry
, struct qstr
*this)
106 t
= NCP_IO_TABLE(dentry
);
108 if (!ncp_case_sensitive(dentry
->d_inode
)) {
109 hash
= init_name_hash();
110 for (i
=0; i
<this->len
; i
++)
111 hash
= partial_name_hash(ncp_tolower(t
, this->name
[i
]),
113 this->hash
= end_name_hash(hash
);
119 ncp_compare_dentry(struct dentry
*dentry
, struct qstr
*a
, struct qstr
*b
)
121 if (a
->len
!= b
->len
)
124 if (ncp_case_sensitive(dentry
->d_inode
))
125 return strncmp(a
->name
, b
->name
, a
->len
);
127 return ncp_strnicmp(NCP_IO_TABLE(dentry
), a
->name
, b
->name
, a
->len
);
131 * This is the callback from dput() when d_count is going to 0.
132 * We use this to unhash dentries with bad inodes.
133 * Closing files can be safely postponed until iput() - it's done there anyway.
136 ncp_delete_dentry(struct dentry
* dentry
)
138 struct inode
*inode
= dentry
->d_inode
;
141 if (is_bad_inode(inode
))
145 /* N.B. Unhash negative dentries? */
151 ncp_single_volume(struct ncp_server
*server
)
153 return (server
->m
.mounted_vol
[0] != '\0');
156 static inline int ncp_is_server_root(struct inode
*inode
)
158 return (!ncp_single_volume(NCP_SERVER(inode
)) &&
159 inode
== inode
->i_sb
->s_root
->d_inode
);
164 * This is the callback when the dcache has a lookup hit.
168 #ifdef CONFIG_NCPFS_STRONG
169 /* try to delete a readonly file (NW R bit set) */
172 ncp_force_unlink(struct inode
*dir
, struct dentry
* dentry
)
175 struct nw_modify_dos_info info
;
179 memset(&info
, 0, sizeof(info
));
181 /* remove the Read-Only flag on the NW server */
182 inode
= dentry
->d_inode
;
184 old_nwattr
= NCP_FINFO(inode
)->nwattr
;
185 info
.attributes
= old_nwattr
& ~(aRONLY
|aDELETEINHIBIT
|aRENAMEINHIBIT
);
186 res2
= ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(inode
), inode
, NULL
, DM_ATTRIBUTES
, &info
);
190 /* now try again the delete operation */
191 res
= ncp_del_file_or_subdir2(NCP_SERVER(dir
), dentry
);
193 if (res
) /* delete failed, set R bit again */
195 info
.attributes
= old_nwattr
;
196 res2
= ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(inode
), inode
, NULL
, DM_ATTRIBUTES
, &info
);
203 #endif /* CONFIG_NCPFS_STRONG */
205 #ifdef CONFIG_NCPFS_STRONG
207 ncp_force_rename(struct inode
*old_dir
, struct dentry
* old_dentry
, char *_old_name
,
208 struct inode
*new_dir
, struct dentry
* new_dentry
, char *_new_name
)
210 struct nw_modify_dos_info info
;
212 struct inode
*old_inode
= old_dentry
->d_inode
;
213 __le32 old_nwattr
= NCP_FINFO(old_inode
)->nwattr
;
214 __le32 new_nwattr
= 0; /* shut compiler warning */
215 int old_nwattr_changed
= 0;
216 int new_nwattr_changed
= 0;
218 memset(&info
, 0, sizeof(info
));
220 /* remove the Read-Only flag on the NW server */
222 info
.attributes
= old_nwattr
& ~(aRONLY
|aRENAMEINHIBIT
|aDELETEINHIBIT
);
223 res2
= ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(old_inode
), old_inode
, NULL
, DM_ATTRIBUTES
, &info
);
225 old_nwattr_changed
= 1;
226 if (new_dentry
&& new_dentry
->d_inode
) {
227 new_nwattr
= NCP_FINFO(new_dentry
->d_inode
)->nwattr
;
228 info
.attributes
= new_nwattr
& ~(aRONLY
|aRENAMEINHIBIT
|aDELETEINHIBIT
);
229 res2
= ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(new_dir
), new_dir
, _new_name
, DM_ATTRIBUTES
, &info
);
231 new_nwattr_changed
= 1;
233 /* now try again the rename operation */
234 /* but only if something really happened */
235 if (new_nwattr_changed
|| old_nwattr_changed
) {
236 res
= ncp_ren_or_mov_file_or_subdir(NCP_SERVER(old_dir
),
242 /* file was successfully renamed, so:
243 do not set attributes on old file - it no longer exists
244 copy attributes from old file to new */
245 new_nwattr_changed
= old_nwattr_changed
;
246 new_nwattr
= old_nwattr
;
247 old_nwattr_changed
= 0;
250 if (old_nwattr_changed
) {
251 info
.attributes
= old_nwattr
;
252 res2
= ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(old_inode
), old_inode
, NULL
, DM_ATTRIBUTES
, &info
);
255 if (new_nwattr_changed
) {
256 info
.attributes
= new_nwattr
;
257 res2
= ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(new_dir
), new_dir
, _new_name
, DM_ATTRIBUTES
, &info
);
262 #endif /* CONFIG_NCPFS_STRONG */
266 __ncp_lookup_validate(struct dentry
* dentry
, struct nameidata
*nd
)
268 struct ncp_server
*server
;
269 struct dentry
*parent
;
271 struct ncp_entry_info finfo
;
272 int res
, val
= 0, len
;
273 __u8 __name
[NCP_MAXPATHLEN
+ 1];
275 parent
= dget_parent(dentry
);
276 dir
= parent
->d_inode
;
278 if (!dentry
->d_inode
)
281 server
= NCP_SERVER(dir
);
283 if (!ncp_conn_valid(server
))
288 * The default validation is based on dentry age:
289 * We set the max age at mount time. (But each
290 * successful server lookup renews the timestamp.)
292 val
= NCP_TEST_AGE(server
, dentry
);
296 DDPRINTK("ncp_lookup_validate: %s/%s not valid, age=%ld, server lookup\n",
297 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
298 NCP_GET_AGE(dentry
));
300 len
= sizeof(__name
);
301 if (ncp_is_server_root(dir
)) {
302 res
= ncp_io2vol(server
, __name
, &len
, dentry
->d_name
.name
,
303 dentry
->d_name
.len
, 1);
305 res
= ncp_lookup_volume(server
, __name
, &(finfo
.i
));
307 res
= ncp_io2vol(server
, __name
, &len
, dentry
->d_name
.name
,
308 dentry
->d_name
.len
, !ncp_preserve_case(dir
));
310 res
= ncp_obtain_info(server
, dir
, __name
, &(finfo
.i
));
312 finfo
.volume
= finfo
.i
.volNumber
;
313 DDPRINTK("ncp_lookup_validate: looked for %s/%s, res=%d\n",
314 dentry
->d_parent
->d_name
.name
, __name
, res
);
316 * If we didn't find it, or if it has a different dirEntNum to
317 * what we remember, it's not valid any more.
320 if (finfo
.i
.dirEntNum
== NCP_FINFO(dentry
->d_inode
)->dirEntNum
) {
321 ncp_new_dentry(dentry
);
324 DDPRINTK("ncp_lookup_validate: found, but dirEntNum changed\n");
326 ncp_update_inode2(dentry
->d_inode
, &finfo
);
330 DDPRINTK("ncp_lookup_validate: result=%d\n", val
);
336 ncp_lookup_validate(struct dentry
* dentry
, struct nameidata
*nd
)
340 res
= __ncp_lookup_validate(dentry
, nd
);
345 static struct dentry
*
346 ncp_dget_fpos(struct dentry
*dentry
, struct dentry
*parent
, unsigned long fpos
)
348 struct dentry
*dent
= dentry
;
349 struct list_head
*next
;
351 if (d_validate(dent
, parent
)) {
352 if (dent
->d_name
.len
<= NCP_MAXPATHLEN
&&
353 (unsigned long)dent
->d_fsdata
== fpos
) {
354 if (!dent
->d_inode
) {
363 /* If a pointer is invalid, we search the dentry. */
364 spin_lock(&dcache_lock
);
365 next
= parent
->d_subdirs
.next
;
366 while (next
!= &parent
->d_subdirs
) {
367 dent
= list_entry(next
, struct dentry
, d_u
.d_child
);
368 if ((unsigned long)dent
->d_fsdata
== fpos
) {
373 spin_unlock(&dcache_lock
);
378 spin_unlock(&dcache_lock
);
385 static time_t ncp_obtain_mtime(struct dentry
*dentry
)
387 struct inode
*inode
= dentry
->d_inode
;
388 struct ncp_server
*server
= NCP_SERVER(inode
);
389 struct nw_info_struct i
;
391 if (!ncp_conn_valid(server
) || ncp_is_server_root(inode
))
394 if (ncp_obtain_info(server
, inode
, NULL
, &i
))
397 return ncp_date_dos2unix(i
.modifyTime
, i
.modifyDate
);
400 static int ncp_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
402 struct dentry
*dentry
= filp
->f_dentry
;
403 struct inode
*inode
= dentry
->d_inode
;
404 struct page
*page
= NULL
;
405 struct ncp_server
*server
= NCP_SERVER(inode
);
406 union ncp_dir_cache
*cache
= NULL
;
407 struct ncp_cache_control ctl
;
408 int result
, mtime_valid
= 0;
416 DDPRINTK("ncp_readdir: reading %s/%s, pos=%d\n",
417 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
421 if (!ncp_conn_valid(server
))
425 if (filp
->f_pos
== 0) {
426 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
))
430 if (filp
->f_pos
== 1) {
431 if (filldir(dirent
, "..", 2, 1, parent_ino(dentry
), DT_DIR
))
436 page
= grab_cache_page(&inode
->i_data
, 0);
440 ctl
.cache
= cache
= kmap(page
);
441 ctl
.head
= cache
->head
;
443 if (!PageUptodate(page
) || !ctl
.head
.eof
)
446 if (filp
->f_pos
== 2) {
447 if (jiffies
- ctl
.head
.time
>= NCP_MAX_AGE(server
))
450 mtime
= ncp_obtain_mtime(dentry
);
452 if ((!mtime
) || (mtime
!= ctl
.head
.mtime
))
456 if (filp
->f_pos
> ctl
.head
.end
)
459 ctl
.fpos
= filp
->f_pos
+ (NCP_DIRCACHE_START
- 2);
460 ctl
.ofs
= ctl
.fpos
/ NCP_DIRCACHE_SIZE
;
461 ctl
.idx
= ctl
.fpos
% NCP_DIRCACHE_SIZE
;
465 ctl
.page
= find_lock_page(&inode
->i_data
, ctl
.ofs
);
468 ctl
.cache
= kmap(ctl
.page
);
469 if (!PageUptodate(ctl
.page
))
472 while (ctl
.idx
< NCP_DIRCACHE_SIZE
) {
476 dent
= ncp_dget_fpos(ctl
.cache
->dentry
[ctl
.idx
],
477 dentry
, filp
->f_pos
);
480 res
= filldir(dirent
, dent
->d_name
.name
,
481 dent
->d_name
.len
, filp
->f_pos
,
482 dent
->d_inode
->i_ino
, DT_UNKNOWN
);
488 if (filp
->f_pos
> ctl
.head
.end
)
493 SetPageUptodate(ctl
.page
);
494 unlock_page(ctl
.page
);
495 page_cache_release(ctl
.page
);
504 unlock_page(ctl
.page
);
505 page_cache_release(ctl
.page
);
510 ncp_invalidate_dircache_entries(dentry
);
512 mtime
= ncp_obtain_mtime(dentry
);
515 ctl
.head
.mtime
= mtime
;
516 ctl
.head
.time
= jiffies
;
520 ctl
.idx
= NCP_DIRCACHE_START
;
524 if (ncp_is_server_root(inode
)) {
525 ncp_read_volume_list(filp
, dirent
, filldir
, &ctl
);
527 ncp_do_readdir(filp
, dirent
, filldir
, &ctl
);
529 ctl
.head
.end
= ctl
.fpos
- 1;
530 ctl
.head
.eof
= ctl
.valid
;
533 cache
->head
= ctl
.head
;
535 SetPageUptodate(page
);
537 page_cache_release(page
);
541 SetPageUptodate(ctl
.page
);
542 unlock_page(ctl
.page
);
543 page_cache_release(ctl
.page
);
551 ncp_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
552 struct ncp_cache_control
*ctrl
, struct ncp_entry_info
*entry
)
554 struct dentry
*newdent
, *dentry
= filp
->f_dentry
;
555 struct inode
*newino
, *inode
= dentry
->d_inode
;
556 struct ncp_cache_control ctl
= *ctrl
;
561 __u8 __name
[NCP_MAXPATHLEN
+ 1];
563 qname
.len
= sizeof(__name
);
564 if (ncp_vol2io(NCP_SERVER(inode
), __name
, &qname
.len
,
565 entry
->i
.entryName
, entry
->i
.nameLen
,
566 !ncp_preserve_entry_case(inode
, entry
->i
.NSCreator
)))
567 return 1; /* I'm not sure */
570 qname
.hash
= full_name_hash(qname
.name
, qname
.len
);
572 if (dentry
->d_op
&& dentry
->d_op
->d_hash
)
573 if (dentry
->d_op
->d_hash(dentry
, &qname
) != 0)
576 newdent
= d_lookup(dentry
, &qname
);
579 newdent
= d_alloc(dentry
, &qname
);
584 memcpy((char *) newdent
->d_name
.name
, qname
.name
,
585 newdent
->d_name
.len
);
588 if (!newdent
->d_inode
) {
590 entry
->ino
= iunique(inode
->i_sb
, 2);
591 newino
= ncp_iget(inode
->i_sb
, entry
);
593 newdent
->d_op
= &ncp_dentry_operations
;
594 d_instantiate(newdent
, newino
);
599 ncp_update_inode2(newdent
->d_inode
, entry
);
601 if (newdent
->d_inode
) {
602 ino
= newdent
->d_inode
->i_ino
;
603 newdent
->d_fsdata
= (void *) ctl
.fpos
;
604 ncp_new_dentry(newdent
);
607 if (ctl
.idx
>= NCP_DIRCACHE_SIZE
) {
610 SetPageUptodate(ctl
.page
);
611 unlock_page(ctl
.page
);
612 page_cache_release(ctl
.page
);
615 ctl
.idx
-= NCP_DIRCACHE_SIZE
;
617 ctl
.page
= grab_cache_page(&inode
->i_data
, ctl
.ofs
);
619 ctl
.cache
= kmap(ctl
.page
);
622 ctl
.cache
->dentry
[ctl
.idx
] = newdent
;
629 if (!ctl
.filled
&& (ctl
.fpos
== filp
->f_pos
)) {
631 ino
= find_inode_number(dentry
, &qname
);
633 ino
= iunique(inode
->i_sb
, 2);
634 ctl
.filled
= filldir(dirent
, qname
.name
, qname
.len
,
635 filp
->f_pos
, ino
, DT_UNKNOWN
);
642 return (ctl
.valid
|| !ctl
.filled
);
646 ncp_read_volume_list(struct file
*filp
, void *dirent
, filldir_t filldir
,
647 struct ncp_cache_control
*ctl
)
649 struct dentry
*dentry
= filp
->f_dentry
;
650 struct inode
*inode
= dentry
->d_inode
;
651 struct ncp_server
*server
= NCP_SERVER(inode
);
652 struct ncp_volume_info info
;
653 struct ncp_entry_info entry
;
656 DPRINTK("ncp_read_volume_list: pos=%ld\n",
657 (unsigned long) filp
->f_pos
);
659 for (i
= 0; i
< NCP_NUMBER_OF_VOLUMES
; i
++) {
661 if (ncp_get_volume_info_with_number(server
, i
, &info
) != 0)
663 if (!strlen(info
.volume_name
))
666 DPRINTK("ncp_read_volume_list: found vol: %s\n",
669 if (ncp_lookup_volume(server
, info
.volume_name
,
671 DPRINTK("ncpfs: could not lookup vol %s\n",
675 entry
.volume
= entry
.i
.volNumber
;
676 if (!ncp_fill_cache(filp
, dirent
, filldir
, ctl
, &entry
))
682 ncp_do_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
,
683 struct ncp_cache_control
*ctl
)
685 struct dentry
*dentry
= filp
->f_dentry
;
686 struct inode
*dir
= dentry
->d_inode
;
687 struct ncp_server
*server
= NCP_SERVER(dir
);
688 struct nw_search_sequence seq
;
689 struct ncp_entry_info entry
;
695 DPRINTK("ncp_do_readdir: %s/%s, fpos=%ld\n",
696 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
697 (unsigned long) filp
->f_pos
);
698 PPRINTK("ncp_do_readdir: init %s, volnum=%d, dirent=%u\n",
699 dentry
->d_name
.name
, NCP_FINFO(dir
)->volNumber
,
700 NCP_FINFO(dir
)->dirEntNum
);
702 err
= ncp_initialize_search(server
, dir
, &seq
);
704 DPRINTK("ncp_do_readdir: init failed, err=%d\n", err
);
707 /* We MUST NOT use server->buffer_size handshaked with server if we are
708 using UDP, as for UDP server uses max. buffer size determined by
709 MTU, and for TCP server uses hardwired value 65KB (== 66560 bytes).
710 So we use 128KB, just to be sure, as there is no way how to know
711 this value in advance. */
713 buf
= vmalloc(bufsize
);
721 err
= ncp_search_for_fileset(server
, &seq
, &more
, &cnt
, buf
, bufsize
, &rpl
, &rpls
);
724 if (!cnt
) /* prevent endless loop */
729 if (rpls
< offsetof(struct nw_info_struct
, entryName
))
730 break; /* short packet */
731 ncp_extract_file_info(rpl
, &entry
.i
);
732 onerpl
= offsetof(struct nw_info_struct
, entryName
) + entry
.i
.nameLen
;
734 break; /* short packet */
735 (void)ncp_obtain_nfs_info(server
, &entry
.i
);
738 entry
.volume
= entry
.i
.volNumber
;
739 if (!ncp_fill_cache(filp
, dirent
, filldir
, ctl
, &entry
))
747 int ncp_conn_logged_in(struct super_block
*sb
)
749 struct ncp_server
* server
= NCP_SBP(sb
);
752 if (ncp_single_volume(server
)) {
758 __u8 __name
[NCP_MAXPATHLEN
+ 1];
760 len
= sizeof(__name
);
761 result
= ncp_io2vol(server
, __name
, &len
, server
->m
.mounted_vol
,
762 strlen(server
->m
.mounted_vol
), 1);
766 if (ncp_get_volume_root(server
, __name
, &volNumber
, &dirEntNum
, &DosDirNum
)) {
767 PPRINTK("ncp_conn_logged_in: %s not found\n",
768 server
->m
.mounted_vol
);
773 struct inode
* ino
= dent
->d_inode
;
775 NCP_FINFO(ino
)->volNumber
= volNumber
;
776 NCP_FINFO(ino
)->dirEntNum
= dirEntNum
;
777 NCP_FINFO(ino
)->DosDirNum
= DosDirNum
;
779 DPRINTK("ncpfs: sb->s_root->d_inode == NULL!\n");
782 DPRINTK("ncpfs: sb->s_root == NULL!\n");
791 static struct dentry
*ncp_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
793 struct ncp_server
*server
= NCP_SERVER(dir
);
794 struct inode
*inode
= NULL
;
795 struct ncp_entry_info finfo
;
797 __u8 __name
[NCP_MAXPATHLEN
+ 1];
801 if (!ncp_conn_valid(server
))
804 PPRINTK("ncp_lookup: server lookup for %s/%s\n",
805 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
807 len
= sizeof(__name
);
808 if (ncp_is_server_root(dir
)) {
809 res
= ncp_io2vol(server
, __name
, &len
, dentry
->d_name
.name
,
810 dentry
->d_name
.len
, 1);
812 res
= ncp_lookup_volume(server
, __name
, &(finfo
.i
));
814 res
= ncp_io2vol(server
, __name
, &len
, dentry
->d_name
.name
,
815 dentry
->d_name
.len
, !ncp_preserve_case(dir
));
817 res
= ncp_obtain_info(server
, dir
, __name
, &(finfo
.i
));
819 PPRINTK("ncp_lookup: looked for %s/%s, res=%d\n",
820 dentry
->d_parent
->d_name
.name
, __name
, res
);
822 * If we didn't find an entry, make a negative dentry.
828 * Create an inode for the entry.
831 finfo
.ino
= iunique(dir
->i_sb
, 2);
832 finfo
.volume
= finfo
.i
.volNumber
;
834 inode
= ncp_iget(dir
->i_sb
, &finfo
);
837 ncp_new_dentry(dentry
);
839 dentry
->d_op
= &ncp_dentry_operations
;
840 d_add(dentry
, inode
);
845 PPRINTK("ncp_lookup: result=%d\n", error
);
847 return ERR_PTR(error
);
851 * This code is common to create, mkdir, and mknod.
853 static int ncp_instantiate(struct inode
*dir
, struct dentry
*dentry
,
854 struct ncp_entry_info
*finfo
)
859 finfo
->ino
= iunique(dir
->i_sb
, 2);
860 inode
= ncp_iget(dir
->i_sb
, finfo
);
863 d_instantiate(dentry
,inode
);
869 PPRINTK("ncp_instantiate: %s/%s failed, closing file\n",
870 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
871 ncp_close_file(NCP_SERVER(dir
), finfo
->file_handle
);
875 int ncp_create_new(struct inode
*dir
, struct dentry
*dentry
, int mode
,
876 dev_t rdev
, __le32 attributes
)
878 struct ncp_server
*server
= NCP_SERVER(dir
);
879 struct ncp_entry_info finfo
;
880 int error
, result
, len
;
882 __u8 __name
[NCP_MAXPATHLEN
+ 1];
884 PPRINTK("ncp_create_new: creating %s/%s, mode=%x\n",
885 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, mode
);
889 if (!ncp_conn_valid(server
))
892 ncp_age_dentry(server
, dentry
);
893 len
= sizeof(__name
);
894 error
= ncp_io2vol(server
, __name
, &len
, dentry
->d_name
.name
,
895 dentry
->d_name
.len
, !ncp_preserve_case(dir
));
902 (server
->m
.flags
& NCP_MOUNT_EXTRAS
) &&
904 attributes
|= aSYSTEM
| aSHARED
;
906 result
= ncp_open_create_file_or_subdir(server
, dir
, __name
,
907 OC_MODE_CREATE
| OC_MODE_OPEN
| OC_MODE_REPLACE
,
908 attributes
, AR_READ
| AR_WRITE
, &finfo
);
911 result
= ncp_open_create_file_or_subdir(server
, dir
, __name
,
912 OC_MODE_CREATE
| OC_MODE_OPEN
| OC_MODE_REPLACE
,
913 attributes
, AR_WRITE
, &finfo
);
916 error
= -ENAMETOOLONG
;
917 DPRINTK("ncp_create: %s/%s failed\n",
918 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
923 finfo
.access
= opmode
;
924 if (ncp_is_nfs_extras(server
, finfo
.volume
)) {
925 finfo
.i
.nfs
.mode
= mode
;
926 finfo
.i
.nfs
.rdev
= new_encode_dev(rdev
);
927 if (ncp_modify_nfs_info(server
, finfo
.volume
,
929 mode
, new_encode_dev(rdev
)) != 0)
933 error
= ncp_instantiate(dir
, dentry
, &finfo
);
939 static int ncp_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
940 struct nameidata
*nd
)
942 return ncp_create_new(dir
, dentry
, mode
, 0, 0);
945 static int ncp_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
947 struct ncp_entry_info finfo
;
948 struct ncp_server
*server
= NCP_SERVER(dir
);
950 __u8 __name
[NCP_MAXPATHLEN
+ 1];
952 DPRINTK("ncp_mkdir: making %s/%s\n",
953 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
957 if (!ncp_conn_valid(server
))
960 ncp_age_dentry(server
, dentry
);
961 len
= sizeof(__name
);
962 error
= ncp_io2vol(server
, __name
, &len
, dentry
->d_name
.name
,
963 dentry
->d_name
.len
, !ncp_preserve_case(dir
));
968 if (ncp_open_create_file_or_subdir(server
, dir
, __name
,
969 OC_MODE_CREATE
, aDIR
,
973 if (ncp_is_nfs_extras(server
, finfo
.volume
)) {
975 finfo
.i
.nfs
.mode
= mode
;
976 if (ncp_modify_nfs_info(server
,
982 error
= ncp_instantiate(dir
, dentry
, &finfo
);
989 static int ncp_rmdir(struct inode
*dir
, struct dentry
*dentry
)
991 struct ncp_server
*server
= NCP_SERVER(dir
);
992 int error
, result
, len
;
993 __u8 __name
[NCP_MAXPATHLEN
+ 1];
995 DPRINTK("ncp_rmdir: removing %s/%s\n",
996 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1000 if (!ncp_conn_valid(server
))
1004 if (!d_unhashed(dentry
))
1007 len
= sizeof(__name
);
1008 error
= ncp_io2vol(server
, __name
, &len
, dentry
->d_name
.name
,
1009 dentry
->d_name
.len
, !ncp_preserve_case(dir
));
1013 result
= ncp_del_file_or_subdir(server
, dir
, __name
);
1018 case 0x85: /* unauthorized to delete file */
1019 case 0x8A: /* unauthorized to delete file */
1023 case 0x90: /* read only */
1026 case 0x9F: /* in use by another client */
1029 case 0xA0: /* directory not empty */
1032 case 0xFF: /* someone deleted file */
1044 static int ncp_unlink(struct inode
*dir
, struct dentry
*dentry
)
1046 struct inode
*inode
= dentry
->d_inode
;
1047 struct ncp_server
*server
;
1051 server
= NCP_SERVER(dir
);
1052 DPRINTK("ncp_unlink: unlinking %s/%s\n",
1053 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1056 if (!ncp_conn_valid(server
))
1060 * Check whether to close the file ...
1063 PPRINTK("ncp_unlink: closing file\n");
1064 ncp_make_closed(inode
);
1067 error
= ncp_del_file_or_subdir2(server
, dentry
);
1068 #ifdef CONFIG_NCPFS_STRONG
1069 /* 9C is Invalid path.. It should be 8F, 90 - read only, but
1071 if ((error
== 0x9C || error
== 0x90) && server
->m
.flags
& NCP_MOUNT_STRONG
) { /* R/O */
1072 error
= ncp_force_unlink(dir
, dentry
);
1077 DPRINTK("ncp: removed %s/%s\n",
1078 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1084 case 0x8D: /* some files in use */
1085 case 0x8E: /* all files in use */
1088 case 0x8F: /* some read only */
1089 case 0x90: /* all read only */
1090 case 0x9C: /* !!! returned when in-use or read-only by NW4 */
1106 static int ncp_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1107 struct inode
*new_dir
, struct dentry
*new_dentry
)
1109 struct ncp_server
*server
= NCP_SERVER(old_dir
);
1111 int old_len
, new_len
;
1112 __u8 __old_name
[NCP_MAXPATHLEN
+ 1], __new_name
[NCP_MAXPATHLEN
+ 1];
1114 DPRINTK("ncp_rename: %s/%s to %s/%s\n",
1115 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1116 new_dentry
->d_parent
->d_name
.name
, new_dentry
->d_name
.name
);
1120 if (!ncp_conn_valid(server
))
1123 ncp_age_dentry(server
, old_dentry
);
1124 ncp_age_dentry(server
, new_dentry
);
1126 old_len
= sizeof(__old_name
);
1127 error
= ncp_io2vol(server
, __old_name
, &old_len
,
1128 old_dentry
->d_name
.name
, old_dentry
->d_name
.len
,
1129 !ncp_preserve_case(old_dir
));
1133 new_len
= sizeof(__new_name
);
1134 error
= ncp_io2vol(server
, __new_name
, &new_len
,
1135 new_dentry
->d_name
.name
, new_dentry
->d_name
.len
,
1136 !ncp_preserve_case(new_dir
));
1140 error
= ncp_ren_or_mov_file_or_subdir(server
, old_dir
, __old_name
,
1141 new_dir
, __new_name
);
1142 #ifdef CONFIG_NCPFS_STRONG
1143 if ((error
== 0x90 || error
== 0x8B || error
== -EACCES
) &&
1144 server
->m
.flags
& NCP_MOUNT_STRONG
) { /* RO */
1145 error
= ncp_force_rename(old_dir
, old_dentry
, __old_name
,
1146 new_dir
, new_dentry
, __new_name
);
1151 DPRINTK("ncp renamed %s -> %s.\n",
1152 old_dentry
->d_name
.name
,new_dentry
->d_name
.name
);
1155 error
= -ENAMETOOLONG
;
1169 static int ncp_mknod(struct inode
* dir
, struct dentry
*dentry
,
1170 int mode
, dev_t rdev
)
1172 if (!new_valid_dev(rdev
))
1174 if (ncp_is_nfs_extras(NCP_SERVER(dir
), NCP_FINFO(dir
)->volNumber
)) {
1175 DPRINTK(KERN_DEBUG
"ncp_mknod: mode = 0%o\n", mode
);
1176 return ncp_create_new(dir
, dentry
, mode
, rdev
, 0);
1178 return -EPERM
; /* Strange, but true */
1181 /* The following routines are taken directly from msdos-fs */
1183 /* Linear day numbers of the respective 1sts in non-leap years. */
1185 static int day_n
[] =
1186 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0};
1187 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
1190 extern struct timezone sys_tz
;
1192 static int utc2local(int time
)
1194 return time
- sys_tz
.tz_minuteswest
* 60;
1197 static int local2utc(int time
)
1199 return time
+ sys_tz
.tz_minuteswest
* 60;
1202 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
1204 ncp_date_dos2unix(__le16 t
, __le16 d
)
1206 unsigned short time
= le16_to_cpu(t
), date
= le16_to_cpu(d
);
1207 int month
, year
, secs
;
1209 /* first subtract and mask after that... Otherwise, if
1210 date == 0, bad things happen */
1211 month
= ((date
>> 5) - 1) & 15;
1213 secs
= (time
& 31) * 2 + 60 * ((time
>> 5) & 63) + (time
>> 11) * 3600 +
1214 86400 * ((date
& 31) - 1 + day_n
[month
] + (year
/ 4) +
1215 year
* 365 - ((year
& 3) == 0 && month
< 2 ? 1 : 0) + 3653);
1216 /* days since 1.1.70 plus 80's leap day */
1217 return local2utc(secs
);
1221 /* Convert linear UNIX date to a MS-DOS time/date pair. */
1223 ncp_date_unix2dos(int unix_date
, __le16
*time
, __le16
*date
)
1225 int day
, year
, nl_day
, month
;
1227 unix_date
= utc2local(unix_date
);
1228 *time
= cpu_to_le16(
1229 (unix_date
% 60) / 2 + (((unix_date
/ 60) % 60) << 5) +
1230 (((unix_date
/ 3600) % 24) << 11));
1231 day
= unix_date
/ 86400 - 3652;
1233 if ((year
+ 3) / 4 + 365 * year
> day
)
1235 day
-= (year
+ 3) / 4 + 365 * year
;
1236 if (day
== 59 && !(year
& 3)) {
1240 nl_day
= (year
& 3) || day
<= 59 ? day
: day
- 1;
1241 for (month
= 0; month
< 12; month
++)
1242 if (day_n
[month
] > nl_day
)
1245 *date
= cpu_to_le16(nl_day
- day_n
[month
- 1] + 1 + (month
<< 5) + (year
<< 9));