[PATCH] Fix cdrom being confused on using kdump
[linux-block.git] / fs / ncpfs / inode.c
CommitLineData
1da177e4
LT
1/*
2 * inode.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 Wolfram Pienkoss for NLS
8 * Modified 2000 Ben Harris, University of Cambridge for NFS NS meta-info
9 *
10 */
11
12#include <linux/config.h>
13#include <linux/module.h>
14
15#include <asm/system.h>
16#include <asm/uaccess.h>
17#include <asm/byteorder.h>
18
19#include <linux/time.h>
20#include <linux/kernel.h>
21#include <linux/mm.h>
22#include <linux/string.h>
23#include <linux/stat.h>
24#include <linux/errno.h>
25#include <linux/file.h>
26#include <linux/fcntl.h>
27#include <linux/slab.h>
28#include <linux/vmalloc.h>
29#include <linux/init.h>
30#include <linux/smp_lock.h>
31#include <linux/vfs.h>
32
33#include <linux/ncp_fs.h>
34
35#include <net/sock.h>
36
37#include "ncplib_kernel.h"
38#include "getopt.h"
39
40static void ncp_delete_inode(struct inode *);
41static void ncp_put_super(struct super_block *);
42static int ncp_statfs(struct super_block *, struct kstatfs *);
43
44static kmem_cache_t * ncp_inode_cachep;
45
46static struct inode *ncp_alloc_inode(struct super_block *sb)
47{
48 struct ncp_inode_info *ei;
49 ei = (struct ncp_inode_info *)kmem_cache_alloc(ncp_inode_cachep, SLAB_KERNEL);
50 if (!ei)
51 return NULL;
52 return &ei->vfs_inode;
53}
54
55static void ncp_destroy_inode(struct inode *inode)
56{
57 kmem_cache_free(ncp_inode_cachep, NCP_FINFO(inode));
58}
59
60static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
61{
62 struct ncp_inode_info *ei = (struct ncp_inode_info *) foo;
63
64 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
65 SLAB_CTOR_CONSTRUCTOR) {
8e3f9045 66 mutex_init(&ei->open_mutex);
1da177e4
LT
67 inode_init_once(&ei->vfs_inode);
68 }
69}
70
71static int init_inodecache(void)
72{
73 ncp_inode_cachep = kmem_cache_create("ncp_inode_cache",
74 sizeof(struct ncp_inode_info),
fffb60f9
PJ
75 0, (SLAB_RECLAIM_ACCOUNT|
76 SLAB_MEM_SPREAD),
1da177e4
LT
77 init_once, NULL);
78 if (ncp_inode_cachep == NULL)
79 return -ENOMEM;
80 return 0;
81}
82
83static void destroy_inodecache(void)
84{
85 if (kmem_cache_destroy(ncp_inode_cachep))
86 printk(KERN_INFO "ncp_inode_cache: not all structures were freed\n");
87}
88
89static int ncp_remount(struct super_block *sb, int *flags, char* data)
90{
91 *flags |= MS_NODIRATIME;
92 return 0;
93}
94
95static struct super_operations ncp_sops =
96{
97 .alloc_inode = ncp_alloc_inode,
98 .destroy_inode = ncp_destroy_inode,
99 .drop_inode = generic_delete_inode,
100 .delete_inode = ncp_delete_inode,
101 .put_super = ncp_put_super,
102 .statfs = ncp_statfs,
103 .remount_fs = ncp_remount,
104};
105
106extern struct dentry_operations ncp_root_dentry_operations;
107#if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
108extern struct address_space_operations ncp_symlink_aops;
109extern int ncp_symlink(struct inode*, struct dentry*, const char*);
110#endif
111
112/*
113 * Fill in the ncpfs-specific information in the inode.
114 */
115static void ncp_update_dirent(struct inode *inode, struct ncp_entry_info *nwinfo)
116{
117 NCP_FINFO(inode)->DosDirNum = nwinfo->i.DosDirNum;
118 NCP_FINFO(inode)->dirEntNum = nwinfo->i.dirEntNum;
119 NCP_FINFO(inode)->volNumber = nwinfo->volume;
120}
121
122void ncp_update_inode(struct inode *inode, struct ncp_entry_info *nwinfo)
123{
124 ncp_update_dirent(inode, nwinfo);
125 NCP_FINFO(inode)->nwattr = nwinfo->i.attributes;
126 NCP_FINFO(inode)->access = nwinfo->access;
127 memcpy(NCP_FINFO(inode)->file_handle, nwinfo->file_handle,
128 sizeof(nwinfo->file_handle));
129 DPRINTK("ncp_update_inode: updated %s, volnum=%d, dirent=%u\n",
130 nwinfo->i.entryName, NCP_FINFO(inode)->volNumber,
131 NCP_FINFO(inode)->dirEntNum);
132}
133
134static void ncp_update_dates(struct inode *inode, struct nw_info_struct *nwi)
135{
136 /* NFS namespace mode overrides others if it's set. */
137 DPRINTK(KERN_DEBUG "ncp_update_dates_and_mode: (%s) nfs.mode=0%o\n",
138 nwi->entryName, nwi->nfs.mode);
139 if (nwi->nfs.mode) {
140 /* XXX Security? */
141 inode->i_mode = nwi->nfs.mode;
142 }
143
144 inode->i_blocks = (inode->i_size + NCP_BLOCK_SIZE - 1) >> NCP_BLOCK_SHIFT;
145
146 inode->i_mtime.tv_sec = ncp_date_dos2unix(nwi->modifyTime, nwi->modifyDate);
147 inode->i_ctime.tv_sec = ncp_date_dos2unix(nwi->creationTime, nwi->creationDate);
148 inode->i_atime.tv_sec = ncp_date_dos2unix(0, nwi->lastAccessDate);
149 inode->i_atime.tv_nsec = 0;
150 inode->i_mtime.tv_nsec = 0;
151 inode->i_ctime.tv_nsec = 0;
152}
153
154static void ncp_update_attrs(struct inode *inode, struct ncp_entry_info *nwinfo)
155{
156 struct nw_info_struct *nwi = &nwinfo->i;
157 struct ncp_server *server = NCP_SERVER(inode);
158
159 if (nwi->attributes & aDIR) {
160 inode->i_mode = server->m.dir_mode;
161 /* for directories dataStreamSize seems to be some
162 Object ID ??? */
163 inode->i_size = NCP_BLOCK_SIZE;
164 } else {
165 inode->i_mode = server->m.file_mode;
166 inode->i_size = le32_to_cpu(nwi->dataStreamSize);
167#ifdef CONFIG_NCPFS_EXTRAS
168 if ((server->m.flags & (NCP_MOUNT_EXTRAS|NCP_MOUNT_SYMLINKS))
169 && (nwi->attributes & aSHARED)) {
170 switch (nwi->attributes & (aHIDDEN|aSYSTEM)) {
171 case aHIDDEN:
172 if (server->m.flags & NCP_MOUNT_SYMLINKS) {
173 if (/* (inode->i_size >= NCP_MIN_SYMLINK_SIZE)
174 && */ (inode->i_size <= NCP_MAX_SYMLINK_SIZE)) {
175 inode->i_mode = (inode->i_mode & ~S_IFMT) | S_IFLNK;
176 NCP_FINFO(inode)->flags |= NCPI_KLUDGE_SYMLINK;
177 break;
178 }
179 }
180 /* FALLTHROUGH */
181 case 0:
182 if (server->m.flags & NCP_MOUNT_EXTRAS)
183 inode->i_mode |= S_IRUGO;
184 break;
185 case aSYSTEM:
186 if (server->m.flags & NCP_MOUNT_EXTRAS)
187 inode->i_mode |= (inode->i_mode >> 2) & S_IXUGO;
188 break;
189 /* case aSYSTEM|aHIDDEN: */
190 default:
191 /* reserved combination */
192 break;
193 }
194 }
195#endif
196 }
197 if (nwi->attributes & aRONLY) inode->i_mode &= ~S_IWUGO;
198}
199
200void ncp_update_inode2(struct inode* inode, struct ncp_entry_info *nwinfo)
201{
202 NCP_FINFO(inode)->flags = 0;
203 if (!atomic_read(&NCP_FINFO(inode)->opened)) {
204 NCP_FINFO(inode)->nwattr = nwinfo->i.attributes;
205 ncp_update_attrs(inode, nwinfo);
206 }
207
208 ncp_update_dates(inode, &nwinfo->i);
209 ncp_update_dirent(inode, nwinfo);
210}
211
212/*
213 * Fill in the inode based on the ncp_entry_info structure.
214 */
215static void ncp_set_attr(struct inode *inode, struct ncp_entry_info *nwinfo)
216{
217 struct ncp_server *server = NCP_SERVER(inode);
218
219 NCP_FINFO(inode)->flags = 0;
220
221 ncp_update_attrs(inode, nwinfo);
222
223 DDPRINTK("ncp_read_inode: inode->i_mode = %u\n", inode->i_mode);
224
225 inode->i_nlink = 1;
226 inode->i_uid = server->m.uid;
227 inode->i_gid = server->m.gid;
228 inode->i_blksize = NCP_BLOCK_SIZE;
229
230 ncp_update_dates(inode, &nwinfo->i);
231 ncp_update_inode(inode, nwinfo);
232}
233
234#if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
235static struct inode_operations ncp_symlink_inode_operations = {
236 .readlink = generic_readlink,
237 .follow_link = page_follow_link_light,
238 .put_link = page_put_link,
239 .setattr = ncp_notify_change,
240};
241#endif
242
243/*
244 * Get a new inode.
245 */
246struct inode *
247ncp_iget(struct super_block *sb, struct ncp_entry_info *info)
248{
249 struct inode *inode;
250
251 if (info == NULL) {
252 printk(KERN_ERR "ncp_iget: info is NULL\n");
253 return NULL;
254 }
255
256 inode = new_inode(sb);
257 if (inode) {
258 atomic_set(&NCP_FINFO(inode)->opened, info->opened);
259
260 inode->i_ino = info->ino;
261 ncp_set_attr(inode, info);
262 if (S_ISREG(inode->i_mode)) {
263 inode->i_op = &ncp_file_inode_operations;
264 inode->i_fop = &ncp_file_operations;
265 } else if (S_ISDIR(inode->i_mode)) {
266 inode->i_op = &ncp_dir_inode_operations;
267 inode->i_fop = &ncp_dir_operations;
268#ifdef CONFIG_NCPFS_NFS_NS
269 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
270 init_special_inode(inode, inode->i_mode,
271 new_decode_dev(info->i.nfs.rdev));
272#endif
273#if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
274 } else if (S_ISLNK(inode->i_mode)) {
275 inode->i_op = &ncp_symlink_inode_operations;
276 inode->i_data.a_ops = &ncp_symlink_aops;
277#endif
278 } else {
279 make_bad_inode(inode);
280 }
281 insert_inode_hash(inode);
282 } else
283 printk(KERN_ERR "ncp_iget: iget failed!\n");
284 return inode;
285}
286
287static void
288ncp_delete_inode(struct inode *inode)
289{
fef26658
MF
290 truncate_inode_pages(&inode->i_data, 0);
291
1da177e4
LT
292 if (S_ISDIR(inode->i_mode)) {
293 DDPRINTK("ncp_delete_inode: put directory %ld\n", inode->i_ino);
294 }
295
296 if (ncp_make_closed(inode) != 0) {
297 /* We can't do anything but complain. */
298 printk(KERN_ERR "ncp_delete_inode: could not close\n");
299 }
300 clear_inode(inode);
301}
302
303static void ncp_stop_tasks(struct ncp_server *server) {
304 struct sock* sk = server->ncp_sock->sk;
305
306 sk->sk_error_report = server->error_report;
307 sk->sk_data_ready = server->data_ready;
308 sk->sk_write_space = server->write_space;
309 del_timer_sync(&server->timeout_tm);
310 flush_scheduled_work();
311}
312
313static const struct ncp_option ncp_opts[] = {
314 { "uid", OPT_INT, 'u' },
315 { "gid", OPT_INT, 'g' },
316 { "owner", OPT_INT, 'o' },
317 { "mode", OPT_INT, 'm' },
318 { "dirmode", OPT_INT, 'd' },
319 { "timeout", OPT_INT, 't' },
320 { "retry", OPT_INT, 'r' },
321 { "flags", OPT_INT, 'f' },
322 { "wdogpid", OPT_INT, 'w' },
323 { "ncpfd", OPT_INT, 'n' },
324 { "infofd", OPT_INT, 'i' }, /* v5 */
325 { "version", OPT_INT, 'v' },
326 { NULL, 0, 0 } };
327
328static int ncp_parse_options(struct ncp_mount_data_kernel *data, char *options) {
329 int optval;
330 char *optarg;
331 unsigned long optint;
332 int version = 0;
333
334 data->flags = 0;
335 data->int_flags = 0;
336 data->mounted_uid = 0;
337 data->wdog_pid = -1;
338 data->ncp_fd = ~0;
339 data->time_out = 10;
340 data->retry_count = 20;
341 data->uid = 0;
342 data->gid = 0;
343 data->file_mode = 0600;
344 data->dir_mode = 0700;
345 data->info_fd = -1;
346 data->mounted_vol[0] = 0;
347
348 while ((optval = ncp_getopt("ncpfs", &options, ncp_opts, NULL, &optarg, &optint)) != 0) {
349 if (optval < 0)
350 return optval;
351 switch (optval) {
352 case 'u':
353 data->uid = optint;
354 break;
355 case 'g':
356 data->gid = optint;
357 break;
358 case 'o':
359 data->mounted_uid = optint;
360 break;
361 case 'm':
362 data->file_mode = optint;
363 break;
364 case 'd':
365 data->dir_mode = optint;
366 break;
367 case 't':
368 data->time_out = optint;
369 break;
370 case 'r':
371 data->retry_count = optint;
372 break;
373 case 'f':
374 data->flags = optint;
375 break;
376 case 'w':
377 data->wdog_pid = optint;
378 break;
379 case 'n':
380 data->ncp_fd = optint;
381 break;
382 case 'i':
383 data->info_fd = optint;
384 break;
385 case 'v':
386 if (optint < NCP_MOUNT_VERSION_V4) {
387 return -ECHRNG;
388 }
389 if (optint > NCP_MOUNT_VERSION_V5) {
390 return -ECHRNG;
391 }
392 version = optint;
393 break;
394
395 }
396 }
397 return 0;
398}
399
400static int ncp_fill_super(struct super_block *sb, void *raw_data, int silent)
401{
402 struct ncp_mount_data_kernel data;
403 struct ncp_server *server;
404 struct file *ncp_filp;
405 struct inode *root_inode;
406 struct inode *sock_inode;
407 struct socket *sock;
408 int error;
409 int default_bufsize;
410#ifdef CONFIG_NCPFS_PACKET_SIGNING
411 int options;
412#endif
413 struct ncp_entry_info finfo;
414
415 server = kmalloc(sizeof(struct ncp_server), GFP_KERNEL);
416 if (!server)
417 return -ENOMEM;
418 sb->s_fs_info = server;
419 memset(server, 0, sizeof(struct ncp_server));
420
421 error = -EFAULT;
422 if (raw_data == NULL)
423 goto out;
424 switch (*(int*)raw_data) {
425 case NCP_MOUNT_VERSION:
426 {
427 struct ncp_mount_data* md = (struct ncp_mount_data*)raw_data;
428
429 data.flags = md->flags;
430 data.int_flags = NCP_IMOUNT_LOGGEDIN_POSSIBLE;
431 data.mounted_uid = md->mounted_uid;
432 data.wdog_pid = md->wdog_pid;
433 data.ncp_fd = md->ncp_fd;
434 data.time_out = md->time_out;
435 data.retry_count = md->retry_count;
436 data.uid = md->uid;
437 data.gid = md->gid;
438 data.file_mode = md->file_mode;
439 data.dir_mode = md->dir_mode;
440 data.info_fd = -1;
441 memcpy(data.mounted_vol, md->mounted_vol,
442 NCP_VOLNAME_LEN+1);
443 }
444 break;
445 case NCP_MOUNT_VERSION_V4:
446 {
447 struct ncp_mount_data_v4* md = (struct ncp_mount_data_v4*)raw_data;
448
449 data.flags = md->flags;
450 data.int_flags = 0;
451 data.mounted_uid = md->mounted_uid;
452 data.wdog_pid = md->wdog_pid;
453 data.ncp_fd = md->ncp_fd;
454 data.time_out = md->time_out;
455 data.retry_count = md->retry_count;
456 data.uid = md->uid;
457 data.gid = md->gid;
458 data.file_mode = md->file_mode;
459 data.dir_mode = md->dir_mode;
460 data.info_fd = -1;
461 data.mounted_vol[0] = 0;
462 }
463 break;
464 default:
465 error = -ECHRNG;
466 if (memcmp(raw_data, "vers", 4) == 0) {
467 error = ncp_parse_options(&data, raw_data);
468 }
469 if (error)
470 goto out;
471 break;
472 }
473 error = -EBADF;
474 ncp_filp = fget(data.ncp_fd);
475 if (!ncp_filp)
476 goto out;
477 error = -ENOTSOCK;
478 sock_inode = ncp_filp->f_dentry->d_inode;
479 if (!S_ISSOCK(sock_inode->i_mode))
480 goto out_fput;
481 sock = SOCKET_I(sock_inode);
482 if (!sock)
483 goto out_fput;
484
485 if (sock->type == SOCK_STREAM)
486 default_bufsize = 0xF000;
487 else
488 default_bufsize = 1024;
489
490 sb->s_flags |= MS_NODIRATIME; /* probably even noatime */
491 sb->s_maxbytes = 0xFFFFFFFFU;
492 sb->s_blocksize = 1024; /* Eh... Is this correct? */
493 sb->s_blocksize_bits = 10;
494 sb->s_magic = NCP_SUPER_MAGIC;
495 sb->s_op = &ncp_sops;
496
497 server = NCP_SBP(sb);
498 memset(server, 0, sizeof(*server));
499
500 server->ncp_filp = ncp_filp;
501 server->ncp_sock = sock;
502
503 if (data.info_fd != -1) {
504 struct socket *info_sock;
505
506 error = -EBADF;
507 server->info_filp = fget(data.info_fd);
508 if (!server->info_filp)
509 goto out_fput;
510 error = -ENOTSOCK;
511 sock_inode = server->info_filp->f_dentry->d_inode;
512 if (!S_ISSOCK(sock_inode->i_mode))
513 goto out_fput2;
514 info_sock = SOCKET_I(sock_inode);
515 if (!info_sock)
516 goto out_fput2;
517 error = -EBADFD;
518 if (info_sock->type != SOCK_STREAM)
519 goto out_fput2;
520 server->info_sock = info_sock;
521 }
522
523/* server->lock = 0; */
8e3f9045 524 mutex_init(&server->mutex);
1da177e4
LT
525 server->packet = NULL;
526/* server->buffer_size = 0; */
527/* server->conn_status = 0; */
528/* server->root_dentry = NULL; */
529/* server->root_setuped = 0; */
530#ifdef CONFIG_NCPFS_PACKET_SIGNING
531/* server->sign_wanted = 0; */
532/* server->sign_active = 0; */
533#endif
534 server->auth.auth_type = NCP_AUTH_NONE;
535/* server->auth.object_name_len = 0; */
536/* server->auth.object_name = NULL; */
537/* server->auth.object_type = 0; */
538/* server->priv.len = 0; */
539/* server->priv.data = NULL; */
540
541 server->m = data;
542 /* Althought anything producing this is buggy, it happens
543 now because of PATH_MAX changes.. */
544 if (server->m.time_out < 1) {
545 server->m.time_out = 10;
546 printk(KERN_INFO "You need to recompile your ncpfs utils..\n");
547 }
548 server->m.time_out = server->m.time_out * HZ / 100;
549 server->m.file_mode = (server->m.file_mode & S_IRWXUGO) | S_IFREG;
550 server->m.dir_mode = (server->m.dir_mode & S_IRWXUGO) | S_IFDIR;
551
552#ifdef CONFIG_NCPFS_NLS
553 /* load the default NLS charsets */
554 server->nls_vol = load_nls_default();
555 server->nls_io = load_nls_default();
556#endif /* CONFIG_NCPFS_NLS */
557
558 server->dentry_ttl = 0; /* no caching */
559
560 INIT_LIST_HEAD(&server->tx.requests);
8e3f9045 561 mutex_init(&server->rcv.creq_mutex);
1da177e4
LT
562 server->tx.creq = NULL;
563 server->rcv.creq = NULL;
564 server->data_ready = sock->sk->sk_data_ready;
565 server->write_space = sock->sk->sk_write_space;
566 server->error_report = sock->sk->sk_error_report;
567 sock->sk->sk_user_data = server;
568
569 init_timer(&server->timeout_tm);
570#undef NCP_PACKET_SIZE
571#define NCP_PACKET_SIZE 131072
572 error = -ENOMEM;
573 server->packet_size = NCP_PACKET_SIZE;
574 server->packet = vmalloc(NCP_PACKET_SIZE);
575 if (server->packet == NULL)
576 goto out_nls;
577
578 sock->sk->sk_data_ready = ncp_tcp_data_ready;
579 sock->sk->sk_error_report = ncp_tcp_error_report;
580 if (sock->type == SOCK_STREAM) {
581 server->rcv.ptr = (unsigned char*)&server->rcv.buf;
582 server->rcv.len = 10;
583 server->rcv.state = 0;
584 INIT_WORK(&server->rcv.tq, ncp_tcp_rcv_proc, server);
585 INIT_WORK(&server->tx.tq, ncp_tcp_tx_proc, server);
586 sock->sk->sk_write_space = ncp_tcp_write_space;
587 } else {
588 INIT_WORK(&server->rcv.tq, ncpdgram_rcv_proc, server);
589 INIT_WORK(&server->timeout_tq, ncpdgram_timeout_proc, server);
590 server->timeout_tm.data = (unsigned long)server;
591 server->timeout_tm.function = ncpdgram_timeout_call;
592 }
593
594 ncp_lock_server(server);
595 error = ncp_connect(server);
596 ncp_unlock_server(server);
597 if (error < 0)
598 goto out_packet;
599 DPRINTK("ncp_fill_super: NCP_SBP(sb) = %x\n", (int) NCP_SBP(sb));
600
601 error = -EMSGSIZE; /* -EREMOTESIDEINCOMPATIBLE */
602#ifdef CONFIG_NCPFS_PACKET_SIGNING
603 if (ncp_negotiate_size_and_options(server, default_bufsize,
604 NCP_DEFAULT_OPTIONS, &(server->buffer_size), &options) == 0)
605 {
606 if (options != NCP_DEFAULT_OPTIONS)
607 {
608 if (ncp_negotiate_size_and_options(server,
609 default_bufsize,
610 options & 2,
611 &(server->buffer_size), &options) != 0)
612
613 {
614 goto out_disconnect;
615 }
616 }
617 if (options & 2)
618 server->sign_wanted = 1;
619 }
620 else
621#endif /* CONFIG_NCPFS_PACKET_SIGNING */
622 if (ncp_negotiate_buffersize(server, default_bufsize,
623 &(server->buffer_size)) != 0)
624 goto out_disconnect;
625 DPRINTK("ncpfs: bufsize = %d\n", server->buffer_size);
626
627 memset(&finfo, 0, sizeof(finfo));
628 finfo.i.attributes = aDIR;
629 finfo.i.dataStreamSize = 0; /* ignored */
630 finfo.i.dirEntNum = 0;
631 finfo.i.DosDirNum = 0;
632#ifdef CONFIG_NCPFS_SMALLDOS
633 finfo.i.NSCreator = NW_NS_DOS;
634#endif
635 finfo.volume = NCP_NUMBER_OF_VOLUMES;
636 /* set dates of mountpoint to Jan 1, 1986; 00:00 */
637 finfo.i.creationTime = finfo.i.modifyTime
638 = cpu_to_le16(0x0000);
639 finfo.i.creationDate = finfo.i.modifyDate
640 = finfo.i.lastAccessDate
641 = cpu_to_le16(0x0C21);
642 finfo.i.nameLen = 0;
643 finfo.i.entryName[0] = '\0';
644
645 finfo.opened = 0;
646 finfo.ino = 2; /* tradition */
647
648 server->name_space[finfo.volume] = NW_NS_DOS;
649
650 error = -ENOMEM;
651 root_inode = ncp_iget(sb, &finfo);
652 if (!root_inode)
653 goto out_disconnect;
654 DPRINTK("ncp_fill_super: root vol=%d\n", NCP_FINFO(root_inode)->volNumber);
655 sb->s_root = d_alloc_root(root_inode);
656 if (!sb->s_root)
657 goto out_no_root;
658 sb->s_root->d_op = &ncp_root_dentry_operations;
659 return 0;
660
661out_no_root:
662 iput(root_inode);
663out_disconnect:
664 ncp_lock_server(server);
665 ncp_disconnect(server);
666 ncp_unlock_server(server);
667out_packet:
668 ncp_stop_tasks(server);
669 vfree(server->packet);
670out_nls:
671#ifdef CONFIG_NCPFS_NLS
672 unload_nls(server->nls_io);
673 unload_nls(server->nls_vol);
674#endif
675out_fput2:
676 if (server->info_filp)
677 fput(server->info_filp);
678out_fput:
679 /* 23/12/1998 Marcin Dalecki <dalecki@cs.net.pl>:
680 *
681 * The previously used put_filp(ncp_filp); was bogous, since
682 * it doesn't proper unlocking.
683 */
684 fput(ncp_filp);
685out:
686 sb->s_fs_info = NULL;
687 kfree(server);
688 return error;
689}
690
691static void ncp_put_super(struct super_block *sb)
692{
693 struct ncp_server *server = NCP_SBP(sb);
694
695 ncp_lock_server(server);
696 ncp_disconnect(server);
697 ncp_unlock_server(server);
698
699 ncp_stop_tasks(server);
700
701#ifdef CONFIG_NCPFS_NLS
702 /* unload the NLS charsets */
703 if (server->nls_vol)
704 {
705 unload_nls(server->nls_vol);
706 server->nls_vol = NULL;
707 }
708 if (server->nls_io)
709 {
710 unload_nls(server->nls_io);
711 server->nls_io = NULL;
712 }
713#endif /* CONFIG_NCPFS_NLS */
714
715 if (server->info_filp)
716 fput(server->info_filp);
717 fput(server->ncp_filp);
718 kill_proc(server->m.wdog_pid, SIGTERM, 1);
719
44db77f3
PE
720 kfree(server->priv.data);
721 kfree(server->auth.object_name);
1da177e4
LT
722 vfree(server->packet);
723 sb->s_fs_info = NULL;
724 kfree(server);
725}
726
727static int ncp_statfs(struct super_block *sb, struct kstatfs *buf)
728{
729 struct dentry* d;
730 struct inode* i;
731 struct ncp_inode_info* ni;
732 struct ncp_server* s;
733 struct ncp_volume_info vi;
734 int err;
735 __u8 dh;
736
737 d = sb->s_root;
738 if (!d) {
739 goto dflt;
740 }
741 i = d->d_inode;
742 if (!i) {
743 goto dflt;
744 }
745 ni = NCP_FINFO(i);
746 if (!ni) {
747 goto dflt;
748 }
749 s = NCP_SBP(sb);
750 if (!s) {
751 goto dflt;
752 }
753 if (!s->m.mounted_vol[0]) {
754 goto dflt;
755 }
756
757 err = ncp_dirhandle_alloc(s, ni->volNumber, ni->DosDirNum, &dh);
758 if (err) {
759 goto dflt;
760 }
761 err = ncp_get_directory_info(s, dh, &vi);
762 ncp_dirhandle_free(s, dh);
763 if (err) {
764 goto dflt;
765 }
766 buf->f_type = NCP_SUPER_MAGIC;
767 buf->f_bsize = vi.sectors_per_block * 512;
768 buf->f_blocks = vi.total_blocks;
769 buf->f_bfree = vi.free_blocks;
770 buf->f_bavail = vi.free_blocks;
771 buf->f_files = vi.total_dir_entries;
772 buf->f_ffree = vi.available_dir_entries;
773 buf->f_namelen = 12;
774 return 0;
775
776 /* We cannot say how much disk space is left on a mounted
777 NetWare Server, because free space is distributed over
778 volumes, and the current user might have disk quotas. So
779 free space is not that simple to determine. Our decision
780 here is to err conservatively. */
781
782dflt:;
783 buf->f_type = NCP_SUPER_MAGIC;
784 buf->f_bsize = NCP_BLOCK_SIZE;
785 buf->f_blocks = 0;
786 buf->f_bfree = 0;
787 buf->f_bavail = 0;
788 buf->f_namelen = 12;
789 return 0;
790}
791
792int ncp_notify_change(struct dentry *dentry, struct iattr *attr)
793{
794 struct inode *inode = dentry->d_inode;
795 int result = 0;
796 __le32 info_mask;
797 struct nw_modify_dos_info info;
798 struct ncp_server *server;
799
800 result = -EIO;
801
802 lock_kernel();
803
804 server = NCP_SERVER(inode);
805 if ((!server) || !ncp_conn_valid(server))
806 goto out;
807
808 /* ageing the dentry to force validation */
809 ncp_age_dentry(server, dentry);
810
811 result = inode_change_ok(inode, attr);
812 if (result < 0)
813 goto out;
814
815 result = -EPERM;
816 if (((attr->ia_valid & ATTR_UID) &&
817 (attr->ia_uid != server->m.uid)))
818 goto out;
819
820 if (((attr->ia_valid & ATTR_GID) &&
821 (attr->ia_gid != server->m.gid)))
822 goto out;
823
824 if (((attr->ia_valid & ATTR_MODE) &&
825 (attr->ia_mode &
826 ~(S_IFREG | S_IFDIR | S_IRWXUGO))))
827 goto out;
828
829 info_mask = 0;
830 memset(&info, 0, sizeof(info));
831
832#if 1
833 if ((attr->ia_valid & ATTR_MODE) != 0)
834 {
835 umode_t newmode = attr->ia_mode;
836
837 info_mask |= DM_ATTRIBUTES;
838
839 if (S_ISDIR(inode->i_mode)) {
840 newmode &= server->m.dir_mode;
841 } else {
842#ifdef CONFIG_NCPFS_EXTRAS
843 if (server->m.flags & NCP_MOUNT_EXTRAS) {
844 /* any non-default execute bit set */
845 if (newmode & ~server->m.file_mode & S_IXUGO)
846 info.attributes |= aSHARED | aSYSTEM;
847 /* read for group/world and not in default file_mode */
848 else if (newmode & ~server->m.file_mode & S_IRUGO)
849 info.attributes |= aSHARED;
850 } else
851#endif
852 newmode &= server->m.file_mode;
853 }
854 if (newmode & S_IWUGO)
855 info.attributes &= ~(aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT);
856 else
857 info.attributes |= (aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT);
858
859#ifdef CONFIG_NCPFS_NFS_NS
860 if (ncp_is_nfs_extras(server, NCP_FINFO(inode)->volNumber)) {
861 result = ncp_modify_nfs_info(server,
862 NCP_FINFO(inode)->volNumber,
863 NCP_FINFO(inode)->dirEntNum,
864 attr->ia_mode, 0);
865 if (result != 0)
866 goto out;
867 info.attributes &= ~(aSHARED | aSYSTEM);
868 {
869 /* mark partial success */
870 struct iattr tmpattr;
871
872 tmpattr.ia_valid = ATTR_MODE;
873 tmpattr.ia_mode = attr->ia_mode;
874
875 result = inode_setattr(inode, &tmpattr);
876 if (result)
877 goto out;
878 }
879 }
880#endif
881 }
882#endif
883
884 /* Do SIZE before attributes, otherwise mtime together with size does not work...
885 */
886 if ((attr->ia_valid & ATTR_SIZE) != 0) {
887 int written;
888
889 DPRINTK("ncpfs: trying to change size to %ld\n",
890 attr->ia_size);
891
892 if ((result = ncp_make_open(inode, O_WRONLY)) < 0) {
893 result = -EACCES;
894 goto out;
895 }
896 ncp_write_kernel(NCP_SERVER(inode), NCP_FINFO(inode)->file_handle,
897 attr->ia_size, 0, "", &written);
898
899 /* According to ndir, the changes only take effect after
900 closing the file */
901 ncp_inode_close(inode);
902 result = ncp_make_closed(inode);
903 if (result)
904 goto out;
905 {
906 struct iattr tmpattr;
907
908 tmpattr.ia_valid = ATTR_SIZE;
909 tmpattr.ia_size = attr->ia_size;
910
911 result = inode_setattr(inode, &tmpattr);
912 if (result)
913 goto out;
914 }
915 }
916 if ((attr->ia_valid & ATTR_CTIME) != 0) {
917 info_mask |= (DM_CREATE_TIME | DM_CREATE_DATE);
918 ncp_date_unix2dos(attr->ia_ctime.tv_sec,
919 &info.creationTime, &info.creationDate);
920 }
921 if ((attr->ia_valid & ATTR_MTIME) != 0) {
922 info_mask |= (DM_MODIFY_TIME | DM_MODIFY_DATE);
923 ncp_date_unix2dos(attr->ia_mtime.tv_sec,
924 &info.modifyTime, &info.modifyDate);
925 }
926 if ((attr->ia_valid & ATTR_ATIME) != 0) {
927 __le16 dummy;
928 info_mask |= (DM_LAST_ACCESS_DATE);
929 ncp_date_unix2dos(attr->ia_atime.tv_sec,
930 &dummy, &info.lastAccessDate);
931 }
932 if (info_mask != 0) {
933 result = ncp_modify_file_or_subdir_dos_info(NCP_SERVER(inode),
934 inode, info_mask, &info);
935 if (result != 0) {
936 result = -EACCES;
937
938 if (info_mask == (DM_CREATE_TIME | DM_CREATE_DATE)) {
939 /* NetWare seems not to allow this. I
940 do not know why. So, just tell the
941 user everything went fine. This is
942 a terrible hack, but I do not know
943 how to do this correctly. */
944 result = 0;
945 } else
946 goto out;
947 }
948#ifdef CONFIG_NCPFS_STRONG
949 if ((!result) && (info_mask & DM_ATTRIBUTES))
950 NCP_FINFO(inode)->nwattr = info.attributes;
951#endif
952 }
953 if (!result)
954 result = inode_setattr(inode, attr);
955out:
956 unlock_kernel();
957 return result;
958}
959
1da177e4
LT
960static struct super_block *ncp_get_sb(struct file_system_type *fs_type,
961 int flags, const char *dev_name, void *data)
962{
963 return get_sb_nodev(fs_type, flags, data, ncp_fill_super);
964}
965
966static struct file_system_type ncp_fs_type = {
967 .owner = THIS_MODULE,
968 .name = "ncpfs",
969 .get_sb = ncp_get_sb,
970 .kill_sb = kill_anon_super,
971};
972
973static int __init init_ncp_fs(void)
974{
975 int err;
976 DPRINTK("ncpfs: init_module called\n");
977
1da177e4
LT
978 err = init_inodecache();
979 if (err)
980 goto out1;
981 err = register_filesystem(&ncp_fs_type);
982 if (err)
983 goto out;
984 return 0;
985out:
986 destroy_inodecache();
987out1:
988 return err;
989}
990
991static void __exit exit_ncp_fs(void)
992{
993 DPRINTK("ncpfs: cleanup_module called\n");
994 unregister_filesystem(&ncp_fs_type);
995 destroy_inodecache();
1da177e4
LT
996}
997
998module_init(init_ncp_fs)
999module_exit(exit_ncp_fs)
1000MODULE_LICENSE("GPL");