4 * Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
5 * Copyright (C) 1997 by Volker Lendecke
7 * Please add a note about your changes to smbfs in the ChangeLog file.
10 #include <linux/config.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/stat.h>
17 #include <linux/errno.h>
18 #include <linux/locks.h>
19 #include <linux/malloc.h>
20 #include <linux/init.h>
21 #include <linux/file.h>
22 #include <linux/dcache.h>
23 #include <linux/smp_lock.h>
24 #include <linux/nls.h>
26 #include <linux/smb_fs.h>
27 #include <linux/smbno.h>
28 #include <linux/smb_mount.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
33 #include "smb_debug.h"
36 /* Always pick a default string */
37 #ifdef CONFIG_SMB_NLS_REMOTE
38 #define SMB_NLS_REMOTE CONFIG_SMB_NLS_REMOTE
40 #define SMB_NLS_REMOTE ""
43 static void smb_delete_inode(struct inode
*);
44 static void smb_put_super(struct super_block
*);
45 static int smb_statfs(struct super_block
*, struct statfs
*);
46 static void smb_set_inode_attr(struct inode
*, struct smb_fattr
*);
48 static struct super_operations smb_sops
=
50 put_inode
: force_delete
,
51 delete_inode
: smb_delete_inode
,
52 put_super
: smb_put_super
,
57 /* We are always generating a new inode here */
59 smb_iget(struct super_block
*sb
, struct smb_fattr
*fattr
)
63 DEBUG1("smb_iget: %p\n", fattr
);
65 result
= new_inode(sb
);
68 result
->i_ino
= fattr
->f_ino
;
69 memset(&(result
->u
.smbfs_i
), 0, sizeof(result
->u
.smbfs_i
));
70 smb_set_inode_attr(result
, fattr
);
71 if (S_ISREG(result
->i_mode
)) {
72 result
->i_op
= &smb_file_inode_operations
;
73 result
->i_fop
= &smb_file_operations
;
74 result
->i_data
.a_ops
= &smb_file_aops
;
75 } else if (S_ISDIR(result
->i_mode
)) {
76 result
->i_op
= &smb_dir_inode_operations
;
77 result
->i_fop
= &smb_dir_operations
;
79 insert_inode_hash(result
);
84 * Copy the inode data to a smb_fattr structure.
87 smb_get_inode_attr(struct inode
*inode
, struct smb_fattr
*fattr
)
89 memset(fattr
, 0, sizeof(struct smb_fattr
));
90 fattr
->f_mode
= inode
->i_mode
;
91 fattr
->f_nlink
= inode
->i_nlink
;
92 fattr
->f_ino
= inode
->i_ino
;
93 fattr
->f_uid
= inode
->i_uid
;
94 fattr
->f_gid
= inode
->i_gid
;
95 fattr
->f_rdev
= inode
->i_rdev
;
96 fattr
->f_size
= inode
->i_size
;
97 fattr
->f_mtime
= inode
->i_mtime
;
98 fattr
->f_ctime
= inode
->i_ctime
;
99 fattr
->f_atime
= inode
->i_atime
;
100 fattr
->f_blksize
= inode
->i_blksize
;
101 fattr
->f_blocks
= inode
->i_blocks
;
103 fattr
->attr
= inode
->u
.smbfs_i
.attr
;
105 * Keep the attributes in sync with the inode permissions.
107 if (fattr
->f_mode
& S_IWUSR
)
108 fattr
->attr
&= ~aRONLY
;
110 fattr
->attr
|= aRONLY
;
114 smb_set_inode_attr(struct inode
*inode
, struct smb_fattr
*fattr
)
116 inode
->i_mode
= fattr
->f_mode
;
117 inode
->i_nlink
= fattr
->f_nlink
;
118 inode
->i_uid
= fattr
->f_uid
;
119 inode
->i_gid
= fattr
->f_gid
;
120 inode
->i_rdev
= fattr
->f_rdev
;
121 inode
->i_ctime
= fattr
->f_ctime
;
122 inode
->i_blksize
= fattr
->f_blksize
;
123 inode
->i_blocks
= fattr
->f_blocks
;
125 * Don't change the size and mtime/atime fields
126 * if we're writing to the file.
128 if (!(inode
->u
.smbfs_i
.cache_valid
& SMB_F_LOCALWRITE
))
130 inode
->i_size
= fattr
->f_size
;
131 inode
->i_mtime
= fattr
->f_mtime
;
132 inode
->i_atime
= fattr
->f_atime
;
135 inode
->u
.smbfs_i
.attr
= fattr
->attr
;
137 * Update the "last time refreshed" field for revalidation.
139 inode
->u
.smbfs_i
.oldmtime
= jiffies
;
143 * This is called if the connection has gone bad ...
144 * try to kill off all the current inodes.
147 smb_invalidate_inodes(struct smb_sb_info
*server
)
150 shrink_dcache_sb(SB_of(server
));
151 invalidate_inodes(SB_of(server
));
155 * This is called to update the inode attributes after
156 * we've made changes to a file or directory.
159 smb_refresh_inode(struct dentry
*dentry
)
161 struct inode
*inode
= dentry
->d_inode
;
163 struct smb_fattr fattr
;
165 error
= smb_proc_getattr(dentry
, &fattr
);
168 smb_renew_times(dentry
);
170 * Check whether the type part of the mode changed,
171 * and don't update the attributes if it did.
173 if ((inode
->i_mode
& S_IFMT
) == (fattr
.f_mode
& S_IFMT
))
174 smb_set_inode_attr(inode
, &fattr
);
178 * Big trouble! The inode has become a new object,
179 * so any operations attempted on it are invalid.
181 * To limit damage, mark the inode as bad so that
182 * subsequent lookup validations will fail.
184 PARANOIA("%s/%s changed mode, %07o to %07o\n",
186 inode
->i_mode
, fattr
.f_mode
);
188 fattr
.f_mode
= inode
->i_mode
; /* save mode */
189 make_bad_inode(inode
);
190 inode
->i_mode
= fattr
.f_mode
; /* restore mode */
192 * No need to worry about unhashing the dentry: the
193 * lookup validation will see that the inode is bad.
194 * But we do want to invalidate the caches ...
196 if (!S_ISDIR(inode
->i_mode
))
197 invalidate_inode_pages(inode
);
199 smb_invalid_dir_cache(inode
);
207 * This is called when we want to check whether the inode
208 * has changed on the server. If it has changed, we must
209 * invalidate our local caches.
212 smb_revalidate_inode(struct dentry
*dentry
)
214 struct inode
*inode
= dentry
->d_inode
;
218 DEBUG1("smb_revalidate_inode\n");
220 * If this is a file opened with write permissions,
221 * the inode will be up-to-date.
224 if (S_ISREG(inode
->i_mode
) && smb_is_open(inode
))
226 if (inode
->u
.smbfs_i
.access
!= SMB_O_RDONLY
)
231 * Check whether we've recently refreshed the inode.
233 if (time_before(jiffies
, inode
->u
.smbfs_i
.oldmtime
+ HZ
/10))
235 VERBOSE("up-to-date, jiffies=%lu, oldtime=%lu\n",
236 jiffies
, inode
->u
.smbfs_i
.oldmtime
);
241 * Save the last modified time, then refresh the inode.
242 * (Note: a size change should have a different mtime.)
244 last_time
= inode
->i_mtime
;
245 error
= smb_refresh_inode(dentry
);
246 if (error
|| inode
->i_mtime
!= last_time
)
248 VERBOSE("%s/%s changed, old=%ld, new=%ld\n",
250 (long) last_time
, (long) inode
->i_mtime
);
252 if (!S_ISDIR(inode
->i_mode
))
253 invalidate_inode_pages(inode
);
255 smb_invalid_dir_cache(inode
);
263 * This routine is called when i_nlink == 0 and i_count goes to 0.
264 * All blocking cleanup operations need to go here to avoid races.
267 smb_delete_inode(struct inode
*ino
)
269 DEBUG1("ino=%ld\n", ino
->i_ino
);
272 PARANOIA("could not close inode %ld\n", ino
->i_ino
);
277 /* FIXME: flags and has_arg could probably be merged. */
278 struct option opts
[] = {
279 { "version", 1, 0, 'v' },
280 { "win95", 0, SMB_MOUNT_WIN95
, 1 },
281 { "oldattr", 0, SMB_MOUNT_OLDATTR
, 1 },
282 { "dirattr", 0, SMB_MOUNT_DIRATTR
, 1 },
283 { "case", 0, SMB_MOUNT_CASE
, 1 },
284 { "uid", 1, 0, 'u' },
285 { "gid", 1, 0, 'g' },
286 { "file_mode", 1, 0, 'f' },
287 { "dir_mode", 1, 0, 'd' },
288 { "iocharset", 1, 0, 'i' },
289 { "codepage", 1, 0, 'c' },
294 parse_options(struct smb_mount_data_kernel
*mnt
, char *options
)
303 while ( (c
= smb_getopt("smbfs", &options
, opts
,
304 &optopt
, &optarg
, &flags
, &value
)) > 0) {
306 VERBOSE("'%s' -> '%s'\n", optopt
, optarg
? optarg
: "<none>");
310 /* got a "flag" option */
313 if (value
!= SMB_MOUNT_VERSION
) {
314 printk ("smbfs: Bad mount version %ld, expected %d\n",
315 value
, SMB_MOUNT_VERSION
);
318 mnt
->version
= value
;
327 mnt
->file_mode
= value
& (S_IRWXU
| S_IRWXG
| S_IRWXO
);
328 mnt
->file_mode
|= S_IFREG
;
331 mnt
->dir_mode
= value
& (S_IRWXU
| S_IRWXG
| S_IRWXO
);
332 mnt
->dir_mode
|= S_IFDIR
;
335 strncpy(mnt
->codepage
.local_name
, optarg
,
339 strncpy(mnt
->codepage
.remote_name
, optarg
,
343 printk ("smbfs: Unrecognized mount option %s\n",
354 smb_put_super(struct super_block
*sb
)
356 struct smb_sb_info
*server
= &(sb
->u
.smbfs_sb
);
358 if (server
->sock_file
) {
359 smb_proc_disconnect(server
);
360 smb_dont_catch_keepalive(server
);
361 fput(server
->sock_file
);
364 if (server
->conn_pid
)
365 kill_proc(server
->conn_pid
, SIGTERM
, 1);
368 kfree(sb
->u
.smbfs_sb
.temp_buf
);
370 smb_vfree(server
->packet
);
372 if(sb
->u
.smbfs_sb
.remote_nls
) {
373 unload_nls(sb
->u
.smbfs_sb
.remote_nls
);
374 sb
->u
.smbfs_sb
.remote_nls
= NULL
;
376 if(sb
->u
.smbfs_sb
.local_nls
) {
377 unload_nls(sb
->u
.smbfs_sb
.local_nls
);
378 sb
->u
.smbfs_sb
.local_nls
= NULL
;
383 smb_read_super(struct super_block
*sb
, void *raw_data
, int silent
)
385 struct smb_mount_data_kernel
*mnt
;
386 struct smb_mount_data
*oldmnt
;
387 struct inode
*root_inode
;
388 struct smb_fattr root
;
394 oldmnt
= (struct smb_mount_data
*) raw_data
;
395 ver
= oldmnt
->version
;
396 if (ver
!= SMB_MOUNT_OLDVERSION
&& cpu_to_be32(ver
) != SMB_MOUNT_ASCII
)
399 sb
->s_blocksize
= 1024; /* Eh... Is this correct? */
400 sb
->s_blocksize_bits
= 10;
401 sb
->s_magic
= SMB_SUPER_MAGIC
;
403 sb
->s_op
= &smb_sops
;
405 sb
->u
.smbfs_sb
.mnt
= NULL
;
406 sb
->u
.smbfs_sb
.sock_file
= NULL
;
407 init_MUTEX(&sb
->u
.smbfs_sb
.sem
);
408 init_waitqueue_head(&sb
->u
.smbfs_sb
.wait
);
409 sb
->u
.smbfs_sb
.conn_pid
= 0;
410 sb
->u
.smbfs_sb
.state
= CONN_INVALID
; /* no connection yet */
411 sb
->u
.smbfs_sb
.generation
= 0;
412 sb
->u
.smbfs_sb
.packet_size
= smb_round_length(SMB_INITIAL_PACKET_SIZE
);
413 sb
->u
.smbfs_sb
.packet
= smb_vmalloc(sb
->u
.smbfs_sb
.packet_size
);
414 if (!sb
->u
.smbfs_sb
.packet
)
417 /* Allocate the global temp buffer */
418 sb
->u
.smbfs_sb
.temp_buf
= kmalloc(2*SMB_MAXPATHLEN
+ 20, GFP_KERNEL
);
419 if (!sb
->u
.smbfs_sb
.temp_buf
)
422 /* Setup NLS stuff */
423 sb
->u
.smbfs_sb
.remote_nls
= NULL
;
424 sb
->u
.smbfs_sb
.local_nls
= NULL
;
425 sb
->u
.smbfs_sb
.name_buf
= sb
->u
.smbfs_sb
.temp_buf
+ SMB_MAXPATHLEN
+ 20;
427 /* Allocate the mount data structure */
428 /* FIXME: merge this with the other malloc and get a whole page? */
429 mnt
= kmalloc(sizeof(struct smb_mount_data_kernel
), GFP_KERNEL
);
432 sb
->u
.smbfs_sb
.mnt
= mnt
;
434 memset(mnt
, 0, sizeof(struct smb_mount_data_kernel
));
435 strncpy(mnt
->codepage
.local_name
, CONFIG_NLS_DEFAULT
,
437 strncpy(mnt
->codepage
.remote_name
, SMB_NLS_REMOTE
,
440 if (ver
== SMB_MOUNT_OLDVERSION
) {
441 mnt
->version
= oldmnt
->version
;
443 /* FIXME: is this enough to convert uid/gid's ? */
444 mnt
->mounted_uid
= oldmnt
->mounted_uid
;
445 mnt
->uid
= oldmnt
->uid
;
446 mnt
->gid
= oldmnt
->gid
;
449 oldmnt
->file_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
);
451 oldmnt
->dir_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
);
452 mnt
->file_mode
|= S_IFREG
;
453 mnt
->dir_mode
|= S_IFDIR
;
455 mnt
->flags
= (oldmnt
->file_mode
>> 9);
457 if (parse_options(mnt
, raw_data
))
460 mnt
->mounted_uid
= current
->uid
;
462 smb_setcodepage(&sb
->u
.smbfs_sb
, &mnt
->codepage
);
463 if (!sb
->u
.smbfs_sb
.convert
)
464 PARANOIA("convert funcptr was NULL!\n");
467 * Display the enabled options
468 * Note: smb_proc_getattr uses these in 2.4 (but was changed in 2.2)
470 if (mnt
->flags
& SMB_MOUNT_OLDATTR
)
471 printk("SMBFS: Using core getattr (Win 95 speedup)\n");
472 else if (mnt
->flags
& SMB_MOUNT_DIRATTR
)
473 printk("SMBFS: Using dir ff getattr\n");
476 * Keep the super block locked while we get the root inode.
478 smb_init_root_dirent(&(sb
->u
.smbfs_sb
), &root
);
479 root_inode
= smb_iget(sb
, &root
);
483 sb
->s_root
= d_alloc_root(root_inode
);
492 kfree(sb
->u
.smbfs_sb
.mnt
);
494 kfree(sb
->u
.smbfs_sb
.temp_buf
);
496 smb_vfree(sb
->u
.smbfs_sb
.packet
);
498 if (!sb
->u
.smbfs_sb
.mnt
)
499 printk(KERN_ERR
"smb_read_super: allocation failure\n");
502 printk(KERN_ERR
"smbfs: mount_data version %d is not supported\n", ver
);
505 printk(KERN_ERR
"smb_read_super: missing data argument\n");
511 smb_statfs(struct super_block
*sb
, struct statfs
*buf
)
513 smb_proc_dskattr(sb
, buf
);
515 buf
->f_type
= SMB_SUPER_MAGIC
;
516 buf
->f_namelen
= SMB_MAXPATHLEN
;
521 smb_notify_change(struct dentry
*dentry
, struct iattr
*attr
)
523 struct inode
*inode
= dentry
->d_inode
;
524 struct smb_sb_info
*server
= server_from_dentry(dentry
);
525 unsigned int mask
= (S_IFREG
| S_IFDIR
| S_IRWXU
| S_IRWXG
| S_IRWXO
);
526 int error
, changed
, refresh
= 0;
527 struct smb_fattr fattr
;
529 error
= smb_revalidate_inode(dentry
);
533 if ((error
= inode_change_ok(inode
, attr
)) < 0)
537 if ((attr
->ia_valid
& ATTR_UID
) && (attr
->ia_uid
!= server
->mnt
->uid
))
540 if ((attr
->ia_valid
& ATTR_GID
) && (attr
->ia_uid
!= server
->mnt
->gid
))
543 if ((attr
->ia_valid
& ATTR_MODE
) && (attr
->ia_mode
& ~mask
))
546 if ((attr
->ia_valid
& ATTR_SIZE
) != 0)
548 VERBOSE("changing %s/%s, old size=%ld, new size=%ld\n",
550 (long) inode
->i_size
, (long) attr
->ia_size
);
551 error
= smb_open(dentry
, O_WRONLY
);
554 error
= smb_proc_trunc(server
, inode
->u
.smbfs_i
.fileid
,
558 vmtruncate(inode
, attr
->ia_size
);
563 * Initialize the fattr and check for changed fields.
564 * Note: CTIME under SMB is creation time rather than
565 * change time, so we don't attempt to change it.
567 smb_get_inode_attr(inode
, &fattr
);
570 if ((attr
->ia_valid
& ATTR_MTIME
) != 0)
572 fattr
.f_mtime
= attr
->ia_mtime
;
575 if ((attr
->ia_valid
& ATTR_ATIME
) != 0)
577 fattr
.f_atime
= attr
->ia_atime
;
578 /* Earlier protocols don't have an access time */
579 if (server
->opt
.protocol
>= SMB_PROTOCOL_LANMAN2
)
584 error
= smb_proc_settime(dentry
, &fattr
);
591 * Check for mode changes ... we're extremely limited in
592 * what can be set for SMB servers: just the read-only bit.
594 if ((attr
->ia_valid
& ATTR_MODE
) != 0)
596 VERBOSE("%s/%s mode change, old=%x, new=%x\n",
597 DENTRY_PATH(dentry
), fattr
.f_mode
, attr
->ia_mode
);
599 if (attr
->ia_mode
& S_IWUSR
)
601 if (fattr
.attr
& aRONLY
)
603 fattr
.attr
&= ~aRONLY
;
607 if (!(fattr
.attr
& aRONLY
))
609 fattr
.attr
|= aRONLY
;
615 error
= smb_proc_setattr(dentry
, &fattr
);
625 smb_refresh_inode(dentry
);
629 #ifdef DEBUG_SMB_MALLOC
631 int smb_current_kmalloced
;
632 int smb_current_vmalloced
;
635 static DECLARE_FSTYPE( smb_fs_type
, "smbfs", smb_read_super
, 0);
637 static int __init
init_smb_fs(void)
639 DEBUG1("registering ...\n");
641 #ifdef DEBUG_SMB_MALLOC
643 smb_current_kmalloced
= 0;
644 smb_current_vmalloced
= 0;
647 return register_filesystem(&smb_fs_type
);
650 static void __exit
exit_smb_fs(void)
652 DEBUG1("unregistering ...\n");
653 unregister_filesystem(&smb_fs_type
);
654 #ifdef DEBUG_SMB_MALLOC
655 printk(KERN_DEBUG
"smb_malloced: %d\n", smb_malloced
);
656 printk(KERN_DEBUG
"smb_current_kmalloced: %d\n",smb_current_kmalloced
);
657 printk(KERN_DEBUG
"smb_current_vmalloced: %d\n",smb_current_vmalloced
);
663 module_init(init_smb_fs
)
664 module_exit(exit_smb_fs
)