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
13 #include <linux/config.h>
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>
22 #include <asm/uaccess.h>
23 #include <asm/byteorder.h>
24 #include <linux/smp_lock.h>
26 #include <linux/ncp_fs.h>
28 #include "ncplib_kernel.h"
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
*);
35 static int ncp_readdir(struct file
*, void *, filldir_t
);
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 *);
49 #define ncp_symlink NULL
52 const struct file_operations ncp_dir_operations
=
54 .read
= generic_read_dir
,
55 .readdir
= ncp_readdir
,
59 struct inode_operations ncp_dir_inode_operations
=
64 .symlink
= ncp_symlink
,
69 .setattr
= ncp_notify_change
,
73 * Dentry operations routines
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
*);
80 static struct dentry_operations ncp_dentry_operations
=
82 .d_revalidate
= ncp_lookup_validate
,
83 .d_hash
= ncp_hash_dentry
,
84 .d_compare
= ncp_compare_dentry
,
85 .d_delete
= ncp_delete_dentry
,
88 struct dentry_operations ncp_root_dentry_operations
=
90 .d_hash
= ncp_hash_dentry
,
91 .d_compare
= ncp_compare_dentry
,
92 .d_delete
= ncp_delete_dentry
,
97 * Note: leave the hash unchanged if the directory
101 ncp_hash_dentry(struct dentry
*dentry
, struct qstr
*this)
107 t
= NCP_IO_TABLE(dentry
);
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
]),
114 this->hash
= end_name_hash(hash
);
120 ncp_compare_dentry(struct dentry
*dentry
, struct qstr
*a
, struct qstr
*b
)
122 if (a
->len
!= b
->len
)
125 if (ncp_case_sensitive(dentry
->d_inode
))
126 return strncmp(a
->name
, b
->name
, a
->len
);
128 return ncp_strnicmp(NCP_IO_TABLE(dentry
), a
->name
, b
->name
, a
->len
);
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.
137 ncp_delete_dentry(struct dentry
* dentry
)
139 struct inode
*inode
= dentry
->d_inode
;
142 if (is_bad_inode(inode
))
146 /* N.B. Unhash negative dentries? */
152 ncp_single_volume(struct ncp_server
*server
)
154 return (server
->m
.mounted_vol
[0] != '\0');
157 static inline int ncp_is_server_root(struct inode
*inode
)
159 return (!ncp_single_volume(NCP_SERVER(inode
)) &&
160 inode
== inode
->i_sb
->s_root
->d_inode
);
165 * This is the callback when the dcache has a lookup hit.
169 #ifdef CONFIG_NCPFS_STRONG
170 /* try to delete a readonly file (NW R bit set) */
173 ncp_force_unlink(struct inode
*dir
, struct dentry
* dentry
)
176 struct nw_modify_dos_info info
;
180 memset(&info
, 0, sizeof(info
));
182 /* remove the Read-Only flag on the NW server */
183 inode
= dentry
->d_inode
;
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
);
191 /* now try again the delete operation */
192 res
= ncp_del_file_or_subdir2(NCP_SERVER(dir
), dentry
);
194 if (res
) /* delete failed, set R bit again */
196 info
.attributes
= old_nwattr
;
197 res2
= ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(inode
), inode
, NULL
, DM_ATTRIBUTES
, &info
);
204 #endif /* CONFIG_NCPFS_STRONG */
206 #ifdef CONFIG_NCPFS_STRONG
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
)
211 struct nw_modify_dos_info info
;
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;
219 memset(&info
, 0, sizeof(info
));
221 /* remove the Read-Only flag on the NW server */
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
);
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
);
232 new_nwattr_changed
= 1;
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
),
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;
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
);
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
);
263 #endif /* CONFIG_NCPFS_STRONG */
267 __ncp_lookup_validate(struct dentry
* dentry
, struct nameidata
*nd
)
269 struct ncp_server
*server
;
270 struct dentry
*parent
;
272 struct ncp_entry_info finfo
;
273 int res
, val
= 0, len
;
274 __u8 __name
[NCP_MAXPATHLEN
+ 1];
276 parent
= dget_parent(dentry
);
277 dir
= parent
->d_inode
;
279 if (!dentry
->d_inode
)
282 server
= NCP_SERVER(dir
);
284 if (!ncp_conn_valid(server
))
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.)
293 val
= NCP_TEST_AGE(server
, dentry
);
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
));
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);
306 res
= ncp_lookup_volume(server
, __name
, &(finfo
.i
));
308 res
= ncp_io2vol(server
, __name
, &len
, dentry
->d_name
.name
,
309 dentry
->d_name
.len
, !ncp_preserve_case(dir
));
311 res
= ncp_obtain_info(server
, dir
, __name
, &(finfo
.i
));
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
);
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.
321 if (finfo
.i
.dirEntNum
== NCP_FINFO(dentry
->d_inode
)->dirEntNum
) {
322 ncp_new_dentry(dentry
);
325 DDPRINTK("ncp_lookup_validate: found, but dirEntNum changed\n");
327 ncp_update_inode2(dentry
->d_inode
, &finfo
);
331 DDPRINTK("ncp_lookup_validate: result=%d\n", val
);
337 ncp_lookup_validate(struct dentry
* dentry
, struct nameidata
*nd
)
341 res
= __ncp_lookup_validate(dentry
, nd
);
346 static struct dentry
*
347 ncp_dget_fpos(struct dentry
*dentry
, struct dentry
*parent
, unsigned long fpos
)
349 struct dentry
*dent
= dentry
;
350 struct list_head
*next
;
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
) {
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_u
.d_child
);
369 if ((unsigned long)dent
->d_fsdata
== fpos
) {
374 spin_unlock(&dcache_lock
);
379 spin_unlock(&dcache_lock
);
386 static time_t ncp_obtain_mtime(struct dentry
*dentry
)
388 struct inode
*inode
= dentry
->d_inode
;
389 struct ncp_server
*server
= NCP_SERVER(inode
);
390 struct nw_info_struct i
;
392 if (!ncp_conn_valid(server
) || ncp_is_server_root(inode
))
395 if (ncp_obtain_info(server
, inode
, NULL
, &i
))
398 return ncp_date_dos2unix(i
.modifyTime
, i
.modifyDate
);
401 static int ncp_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
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;
417 DDPRINTK("ncp_readdir: reading %s/%s, pos=%d\n",
418 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
422 if (!ncp_conn_valid(server
))
426 if (filp
->f_pos
== 0) {
427 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
))
431 if (filp
->f_pos
== 1) {
432 if (filldir(dirent
, "..", 2, 1, parent_ino(dentry
), DT_DIR
))
437 page
= grab_cache_page(&inode
->i_data
, 0);
441 ctl
.cache
= cache
= kmap(page
);
442 ctl
.head
= cache
->head
;
444 if (!PageUptodate(page
) || !ctl
.head
.eof
)
447 if (filp
->f_pos
== 2) {
448 if (jiffies
- ctl
.head
.time
>= NCP_MAX_AGE(server
))
451 mtime
= ncp_obtain_mtime(dentry
);
453 if ((!mtime
) || (mtime
!= ctl
.head
.mtime
))
457 if (filp
->f_pos
> ctl
.head
.end
)
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
;
466 ctl
.page
= find_lock_page(&inode
->i_data
, ctl
.ofs
);
469 ctl
.cache
= kmap(ctl
.page
);
470 if (!PageUptodate(ctl
.page
))
473 while (ctl
.idx
< NCP_DIRCACHE_SIZE
) {
477 dent
= ncp_dget_fpos(ctl
.cache
->dentry
[ctl
.idx
],
478 dentry
, filp
->f_pos
);
481 res
= filldir(dirent
, dent
->d_name
.name
,
482 dent
->d_name
.len
, filp
->f_pos
,
483 dent
->d_inode
->i_ino
, DT_UNKNOWN
);
489 if (filp
->f_pos
> ctl
.head
.end
)
494 SetPageUptodate(ctl
.page
);
495 unlock_page(ctl
.page
);
496 page_cache_release(ctl
.page
);
505 unlock_page(ctl
.page
);
506 page_cache_release(ctl
.page
);
511 ncp_invalidate_dircache_entries(dentry
);
513 mtime
= ncp_obtain_mtime(dentry
);
516 ctl
.head
.mtime
= mtime
;
517 ctl
.head
.time
= jiffies
;
521 ctl
.idx
= NCP_DIRCACHE_START
;
525 if (ncp_is_server_root(inode
)) {
526 ncp_read_volume_list(filp
, dirent
, filldir
, &ctl
);
528 ncp_do_readdir(filp
, dirent
, filldir
, &ctl
);
530 ctl
.head
.end
= ctl
.fpos
- 1;
531 ctl
.head
.eof
= ctl
.valid
;
534 cache
->head
= ctl
.head
;
536 SetPageUptodate(page
);
538 page_cache_release(page
);
542 SetPageUptodate(ctl
.page
);
543 unlock_page(ctl
.page
);
544 page_cache_release(ctl
.page
);
552 ncp_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
553 struct ncp_cache_control
*ctrl
, struct ncp_entry_info
*entry
)
555 struct dentry
*newdent
, *dentry
= filp
->f_dentry
;
556 struct inode
*newino
, *inode
= dentry
->d_inode
;
557 struct ncp_cache_control ctl
= *ctrl
;
562 __u8 __name
[NCP_MAXPATHLEN
+ 1];
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 */
571 qname
.hash
= full_name_hash(qname
.name
, qname
.len
);
573 if (dentry
->d_op
&& dentry
->d_op
->d_hash
)
574 if (dentry
->d_op
->d_hash(dentry
, &qname
) != 0)
577 newdent
= d_lookup(dentry
, &qname
);
580 newdent
= d_alloc(dentry
, &qname
);
585 memcpy((char *) newdent
->d_name
.name
, qname
.name
,
586 newdent
->d_name
.len
);
589 if (!newdent
->d_inode
) {
591 entry
->ino
= iunique(inode
->i_sb
, 2);
592 newino
= ncp_iget(inode
->i_sb
, entry
);
594 newdent
->d_op
= &ncp_dentry_operations
;
595 d_instantiate(newdent
, newino
);
600 ncp_update_inode2(newdent
->d_inode
, entry
);
602 if (newdent
->d_inode
) {
603 ino
= newdent
->d_inode
->i_ino
;
604 newdent
->d_fsdata
= (void *) ctl
.fpos
;
605 ncp_new_dentry(newdent
);
608 if (ctl
.idx
>= NCP_DIRCACHE_SIZE
) {
611 SetPageUptodate(ctl
.page
);
612 unlock_page(ctl
.page
);
613 page_cache_release(ctl
.page
);
616 ctl
.idx
-= NCP_DIRCACHE_SIZE
;
618 ctl
.page
= grab_cache_page(&inode
->i_data
, ctl
.ofs
);
620 ctl
.cache
= kmap(ctl
.page
);
623 ctl
.cache
->dentry
[ctl
.idx
] = newdent
;
630 if (!ctl
.filled
&& (ctl
.fpos
== filp
->f_pos
)) {
632 ino
= find_inode_number(dentry
, &qname
);
634 ino
= iunique(inode
->i_sb
, 2);
635 ctl
.filled
= filldir(dirent
, qname
.name
, qname
.len
,
636 filp
->f_pos
, ino
, DT_UNKNOWN
);
643 return (ctl
.valid
|| !ctl
.filled
);
647 ncp_read_volume_list(struct file
*filp
, void *dirent
, filldir_t filldir
,
648 struct ncp_cache_control
*ctl
)
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
;
657 DPRINTK("ncp_read_volume_list: pos=%ld\n",
658 (unsigned long) filp
->f_pos
);
660 for (i
= 0; i
< NCP_NUMBER_OF_VOLUMES
; i
++) {
662 if (ncp_get_volume_info_with_number(server
, i
, &info
) != 0)
664 if (!strlen(info
.volume_name
))
667 DPRINTK("ncp_read_volume_list: found vol: %s\n",
670 if (ncp_lookup_volume(server
, info
.volume_name
,
672 DPRINTK("ncpfs: could not lookup vol %s\n",
676 entry
.volume
= entry
.i
.volNumber
;
677 if (!ncp_fill_cache(filp
, dirent
, filldir
, ctl
, &entry
))
683 ncp_do_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
,
684 struct ncp_cache_control
*ctl
)
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
;
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
);
703 err
= ncp_initialize_search(server
, dir
, &seq
);
705 DPRINTK("ncp_do_readdir: init failed, err=%d\n", err
);
708 /* We MUST NOT use server->buffer_size handshaked with server if we are
709 using UDP, as for UDP server uses max. buffer size determined by
710 MTU, and for TCP server uses hardwired value 65KB (== 66560 bytes).
711 So we use 128KB, just to be sure, as there is no way how to know
712 this value in advance. */
714 buf
= vmalloc(bufsize
);
722 err
= ncp_search_for_fileset(server
, &seq
, &more
, &cnt
, buf
, bufsize
, &rpl
, &rpls
);
725 if (!cnt
) /* prevent endless loop */
730 if (rpls
< offsetof(struct nw_info_struct
, entryName
))
731 break; /* short packet */
732 ncp_extract_file_info(rpl
, &entry
.i
);
733 onerpl
= offsetof(struct nw_info_struct
, entryName
) + entry
.i
.nameLen
;
735 break; /* short packet */
736 (void)ncp_obtain_nfs_info(server
, &entry
.i
);
739 entry
.volume
= entry
.i
.volNumber
;
740 if (!ncp_fill_cache(filp
, dirent
, filldir
, ctl
, &entry
))
748 int ncp_conn_logged_in(struct super_block
*sb
)
750 struct ncp_server
* server
= NCP_SBP(sb
);
753 if (ncp_single_volume(server
)) {
759 __u8 __name
[NCP_MAXPATHLEN
+ 1];
761 len
= sizeof(__name
);
762 result
= ncp_io2vol(server
, __name
, &len
, server
->m
.mounted_vol
,
763 strlen(server
->m
.mounted_vol
), 1);
767 if (ncp_get_volume_root(server
, __name
, &volNumber
, &dirEntNum
, &DosDirNum
)) {
768 PPRINTK("ncp_conn_logged_in: %s not found\n",
769 server
->m
.mounted_vol
);
774 struct inode
* ino
= dent
->d_inode
;
776 NCP_FINFO(ino
)->volNumber
= volNumber
;
777 NCP_FINFO(ino
)->dirEntNum
= dirEntNum
;
778 NCP_FINFO(ino
)->DosDirNum
= DosDirNum
;
780 DPRINTK("ncpfs: sb->s_root->d_inode == NULL!\n");
783 DPRINTK("ncpfs: sb->s_root == NULL!\n");
792 static struct dentry
*ncp_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
794 struct ncp_server
*server
= NCP_SERVER(dir
);
795 struct inode
*inode
= NULL
;
796 struct ncp_entry_info finfo
;
798 __u8 __name
[NCP_MAXPATHLEN
+ 1];
802 if (!ncp_conn_valid(server
))
805 PPRINTK("ncp_lookup: server lookup for %s/%s\n",
806 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
808 len
= sizeof(__name
);
809 if (ncp_is_server_root(dir
)) {
810 res
= ncp_io2vol(server
, __name
, &len
, dentry
->d_name
.name
,
811 dentry
->d_name
.len
, 1);
813 res
= ncp_lookup_volume(server
, __name
, &(finfo
.i
));
815 res
= ncp_io2vol(server
, __name
, &len
, dentry
->d_name
.name
,
816 dentry
->d_name
.len
, !ncp_preserve_case(dir
));
818 res
= ncp_obtain_info(server
, dir
, __name
, &(finfo
.i
));
820 PPRINTK("ncp_lookup: looked for %s/%s, res=%d\n",
821 dentry
->d_parent
->d_name
.name
, __name
, res
);
823 * If we didn't find an entry, make a negative dentry.
829 * Create an inode for the entry.
832 finfo
.ino
= iunique(dir
->i_sb
, 2);
833 finfo
.volume
= finfo
.i
.volNumber
;
835 inode
= ncp_iget(dir
->i_sb
, &finfo
);
838 ncp_new_dentry(dentry
);
840 dentry
->d_op
= &ncp_dentry_operations
;
841 d_add(dentry
, inode
);
846 PPRINTK("ncp_lookup: result=%d\n", error
);
848 return ERR_PTR(error
);
852 * This code is common to create, mkdir, and mknod.
854 static int ncp_instantiate(struct inode
*dir
, struct dentry
*dentry
,
855 struct ncp_entry_info
*finfo
)
860 finfo
->ino
= iunique(dir
->i_sb
, 2);
861 inode
= ncp_iget(dir
->i_sb
, finfo
);
864 d_instantiate(dentry
,inode
);
870 PPRINTK("ncp_instantiate: %s/%s failed, closing file\n",
871 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
872 ncp_close_file(NCP_SERVER(dir
), finfo
->file_handle
);
876 int ncp_create_new(struct inode
*dir
, struct dentry
*dentry
, int mode
,
877 dev_t rdev
, __le32 attributes
)
879 struct ncp_server
*server
= NCP_SERVER(dir
);
880 struct ncp_entry_info finfo
;
881 int error
, result
, len
;
883 __u8 __name
[NCP_MAXPATHLEN
+ 1];
885 PPRINTK("ncp_create_new: creating %s/%s, mode=%x\n",
886 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, mode
);
890 if (!ncp_conn_valid(server
))
893 ncp_age_dentry(server
, dentry
);
894 len
= sizeof(__name
);
895 error
= ncp_io2vol(server
, __name
, &len
, dentry
->d_name
.name
,
896 dentry
->d_name
.len
, !ncp_preserve_case(dir
));
903 (server
->m
.flags
& NCP_MOUNT_EXTRAS
) &&
905 attributes
|= aSYSTEM
| aSHARED
;
907 result
= ncp_open_create_file_or_subdir(server
, dir
, __name
,
908 OC_MODE_CREATE
| OC_MODE_OPEN
| OC_MODE_REPLACE
,
909 attributes
, AR_READ
| AR_WRITE
, &finfo
);
912 result
= ncp_open_create_file_or_subdir(server
, dir
, __name
,
913 OC_MODE_CREATE
| OC_MODE_OPEN
| OC_MODE_REPLACE
,
914 attributes
, AR_WRITE
, &finfo
);
917 error
= -ENAMETOOLONG
;
918 DPRINTK("ncp_create: %s/%s failed\n",
919 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
924 finfo
.access
= opmode
;
925 if (ncp_is_nfs_extras(server
, finfo
.volume
)) {
926 finfo
.i
.nfs
.mode
= mode
;
927 finfo
.i
.nfs
.rdev
= new_encode_dev(rdev
);
928 if (ncp_modify_nfs_info(server
, finfo
.volume
,
930 mode
, new_encode_dev(rdev
)) != 0)
934 error
= ncp_instantiate(dir
, dentry
, &finfo
);
940 static int ncp_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
941 struct nameidata
*nd
)
943 return ncp_create_new(dir
, dentry
, mode
, 0, 0);
946 static int ncp_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
948 struct ncp_entry_info finfo
;
949 struct ncp_server
*server
= NCP_SERVER(dir
);
951 __u8 __name
[NCP_MAXPATHLEN
+ 1];
953 DPRINTK("ncp_mkdir: making %s/%s\n",
954 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
958 if (!ncp_conn_valid(server
))
961 ncp_age_dentry(server
, dentry
);
962 len
= sizeof(__name
);
963 error
= ncp_io2vol(server
, __name
, &len
, dentry
->d_name
.name
,
964 dentry
->d_name
.len
, !ncp_preserve_case(dir
));
969 if (ncp_open_create_file_or_subdir(server
, dir
, __name
,
970 OC_MODE_CREATE
, aDIR
,
974 if (ncp_is_nfs_extras(server
, finfo
.volume
)) {
976 finfo
.i
.nfs
.mode
= mode
;
977 if (ncp_modify_nfs_info(server
,
983 error
= ncp_instantiate(dir
, dentry
, &finfo
);
990 static int ncp_rmdir(struct inode
*dir
, struct dentry
*dentry
)
992 struct ncp_server
*server
= NCP_SERVER(dir
);
993 int error
, result
, len
;
994 __u8 __name
[NCP_MAXPATHLEN
+ 1];
996 DPRINTK("ncp_rmdir: removing %s/%s\n",
997 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1001 if (!ncp_conn_valid(server
))
1005 if (!d_unhashed(dentry
))
1008 len
= sizeof(__name
);
1009 error
= ncp_io2vol(server
, __name
, &len
, dentry
->d_name
.name
,
1010 dentry
->d_name
.len
, !ncp_preserve_case(dir
));
1014 result
= ncp_del_file_or_subdir(server
, dir
, __name
);
1019 case 0x85: /* unauthorized to delete file */
1020 case 0x8A: /* unauthorized to delete file */
1024 case 0x90: /* read only */
1027 case 0x9F: /* in use by another client */
1030 case 0xA0: /* directory not empty */
1033 case 0xFF: /* someone deleted file */
1045 static int ncp_unlink(struct inode
*dir
, struct dentry
*dentry
)
1047 struct inode
*inode
= dentry
->d_inode
;
1048 struct ncp_server
*server
;
1052 server
= NCP_SERVER(dir
);
1053 DPRINTK("ncp_unlink: unlinking %s/%s\n",
1054 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1057 if (!ncp_conn_valid(server
))
1061 * Check whether to close the file ...
1064 PPRINTK("ncp_unlink: closing file\n");
1065 ncp_make_closed(inode
);
1068 error
= ncp_del_file_or_subdir2(server
, dentry
);
1069 #ifdef CONFIG_NCPFS_STRONG
1070 /* 9C is Invalid path.. It should be 8F, 90 - read only, but
1072 if ((error
== 0x9C || error
== 0x90) && server
->m
.flags
& NCP_MOUNT_STRONG
) { /* R/O */
1073 error
= ncp_force_unlink(dir
, dentry
);
1078 DPRINTK("ncp: removed %s/%s\n",
1079 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
1085 case 0x8D: /* some files in use */
1086 case 0x8E: /* all files in use */
1089 case 0x8F: /* some read only */
1090 case 0x90: /* all read only */
1091 case 0x9C: /* !!! returned when in-use or read-only by NW4 */
1107 static int ncp_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1108 struct inode
*new_dir
, struct dentry
*new_dentry
)
1110 struct ncp_server
*server
= NCP_SERVER(old_dir
);
1112 int old_len
, new_len
;
1113 __u8 __old_name
[NCP_MAXPATHLEN
+ 1], __new_name
[NCP_MAXPATHLEN
+ 1];
1115 DPRINTK("ncp_rename: %s/%s to %s/%s\n",
1116 old_dentry
->d_parent
->d_name
.name
, old_dentry
->d_name
.name
,
1117 new_dentry
->d_parent
->d_name
.name
, new_dentry
->d_name
.name
);
1121 if (!ncp_conn_valid(server
))
1124 ncp_age_dentry(server
, old_dentry
);
1125 ncp_age_dentry(server
, new_dentry
);
1127 old_len
= sizeof(__old_name
);
1128 error
= ncp_io2vol(server
, __old_name
, &old_len
,
1129 old_dentry
->d_name
.name
, old_dentry
->d_name
.len
,
1130 !ncp_preserve_case(old_dir
));
1134 new_len
= sizeof(__new_name
);
1135 error
= ncp_io2vol(server
, __new_name
, &new_len
,
1136 new_dentry
->d_name
.name
, new_dentry
->d_name
.len
,
1137 !ncp_preserve_case(new_dir
));
1141 error
= ncp_ren_or_mov_file_or_subdir(server
, old_dir
, __old_name
,
1142 new_dir
, __new_name
);
1143 #ifdef CONFIG_NCPFS_STRONG
1144 if ((error
== 0x90 || error
== 0x8B || error
== -EACCES
) &&
1145 server
->m
.flags
& NCP_MOUNT_STRONG
) { /* RO */
1146 error
= ncp_force_rename(old_dir
, old_dentry
, __old_name
,
1147 new_dir
, new_dentry
, __new_name
);
1152 DPRINTK("ncp renamed %s -> %s.\n",
1153 old_dentry
->d_name
.name
,new_dentry
->d_name
.name
);
1156 error
= -ENAMETOOLONG
;
1170 static int ncp_mknod(struct inode
* dir
, struct dentry
*dentry
,
1171 int mode
, dev_t rdev
)
1173 if (!new_valid_dev(rdev
))
1175 if (ncp_is_nfs_extras(NCP_SERVER(dir
), NCP_FINFO(dir
)->volNumber
)) {
1176 DPRINTK(KERN_DEBUG
"ncp_mknod: mode = 0%o\n", mode
);
1177 return ncp_create_new(dir
, dentry
, mode
, rdev
, 0);
1179 return -EPERM
; /* Strange, but true */
1182 /* The following routines are taken directly from msdos-fs */
1184 /* Linear day numbers of the respective 1sts in non-leap years. */
1186 static int day_n
[] =
1187 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0};
1188 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
1191 extern struct timezone sys_tz
;
1193 static int utc2local(int time
)
1195 return time
- sys_tz
.tz_minuteswest
* 60;
1198 static int local2utc(int time
)
1200 return time
+ sys_tz
.tz_minuteswest
* 60;
1203 /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
1205 ncp_date_dos2unix(__le16 t
, __le16 d
)
1207 unsigned short time
= le16_to_cpu(t
), date
= le16_to_cpu(d
);
1208 int month
, year
, secs
;
1210 /* first subtract and mask after that... Otherwise, if
1211 date == 0, bad things happen */
1212 month
= ((date
>> 5) - 1) & 15;
1214 secs
= (time
& 31) * 2 + 60 * ((time
>> 5) & 63) + (time
>> 11) * 3600 +
1215 86400 * ((date
& 31) - 1 + day_n
[month
] + (year
/ 4) +
1216 year
* 365 - ((year
& 3) == 0 && month
< 2 ? 1 : 0) + 3653);
1217 /* days since 1.1.70 plus 80's leap day */
1218 return local2utc(secs
);
1222 /* Convert linear UNIX date to a MS-DOS time/date pair. */
1224 ncp_date_unix2dos(int unix_date
, __le16
*time
, __le16
*date
)
1226 int day
, year
, nl_day
, month
;
1228 unix_date
= utc2local(unix_date
);
1229 *time
= cpu_to_le16(
1230 (unix_date
% 60) / 2 + (((unix_date
/ 60) % 60) << 5) +
1231 (((unix_date
/ 3600) % 24) << 11));
1232 day
= unix_date
/ 86400 - 3652;
1234 if ((year
+ 3) / 4 + 365 * year
> day
)
1236 day
-= (year
+ 3) / 4 + 365 * year
;
1237 if (day
== 59 && !(year
& 3)) {
1241 nl_day
= (year
& 3) || day
<= 59 ? day
: day
- 1;
1242 for (month
= 0; month
< 12; month
++)
1243 if (day_n
[month
] > nl_day
)
1246 *date
= cpu_to_le16(nl_day
- day_n
[month
- 1] + 1 + (month
<< 5) + (year
<< 9));