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/time.h>
13 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/stat.h>
17 #include <linux/errno.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/file.h>
21 #include <linux/dcache.h>
22 #include <linux/smp_lock.h>
23 #include <linux/nls.h>
24 #include <linux/seq_file.h>
25 #include <linux/mount.h>
26 #include <linux/net.h>
27 #include <linux/vfs.h>
28 #include <linux/highuid.h>
29 #include <linux/smb_fs.h>
30 #include <linux/smbno.h>
31 #include <linux/smb_mount.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
36 #include "smb_debug.h"
40 /* Always pick a default string */
41 #ifdef CONFIG_SMB_NLS_REMOTE
42 #define SMB_NLS_REMOTE CONFIG_SMB_NLS_REMOTE
44 #define SMB_NLS_REMOTE ""
47 #define SMB_TTL_DEFAULT 1000
49 static void smb_delete_inode(struct inode *);
50 static void smb_put_super(struct super_block *);
51 static int smb_statfs(struct super_block *, struct kstatfs *);
52 static int smb_show_options(struct seq_file *, struct vfsmount *);
54 static kmem_cache_t *smb_inode_cachep;
56 static struct inode *smb_alloc_inode(struct super_block *sb)
58 struct smb_inode_info *ei;
59 ei = (struct smb_inode_info *)kmem_cache_alloc(smb_inode_cachep, SLAB_KERNEL);
62 return &ei->vfs_inode;
65 static void smb_destroy_inode(struct inode *inode)
67 kmem_cache_free(smb_inode_cachep, SMB_I(inode));
70 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
72 struct smb_inode_info *ei = (struct smb_inode_info *) foo;
73 unsigned long flagmask = SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR;
75 if ((flags & flagmask) == SLAB_CTOR_CONSTRUCTOR)
76 inode_init_once(&ei->vfs_inode);
79 static int init_inodecache(void)
81 smb_inode_cachep = kmem_cache_create("smb_inode_cache",
82 sizeof(struct smb_inode_info),
83 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
85 if (smb_inode_cachep == NULL)
90 static void destroy_inodecache(void)
92 if (kmem_cache_destroy(smb_inode_cachep))
93 printk(KERN_INFO "smb_inode_cache: not all structures were freed\n");
96 static int smb_remount(struct super_block *sb, int *flags, char *data)
98 *flags |= MS_NODIRATIME;
102 static struct super_operations smb_sops =
104 .alloc_inode = smb_alloc_inode,
105 .destroy_inode = smb_destroy_inode,
106 .drop_inode = generic_delete_inode,
107 .delete_inode = smb_delete_inode,
108 .put_super = smb_put_super,
109 .statfs = smb_statfs,
110 .show_options = smb_show_options,
111 .remount_fs = smb_remount,
115 /* We are always generating a new inode here */
117 smb_iget(struct super_block *sb, struct smb_fattr *fattr)
119 struct smb_sb_info *server = SMB_SB(sb);
120 struct inode *result;
122 DEBUG1("smb_iget: %p\n", fattr);
124 result = new_inode(sb);
127 result->i_ino = fattr->f_ino;
128 SMB_I(result)->open = 0;
129 SMB_I(result)->fileid = 0;
130 SMB_I(result)->access = 0;
131 SMB_I(result)->flags = 0;
132 SMB_I(result)->closed = 0;
133 SMB_I(result)->openers = 0;
134 smb_set_inode_attr(result, fattr);
135 if (S_ISREG(result->i_mode)) {
136 result->i_op = &smb_file_inode_operations;
137 result->i_fop = &smb_file_operations;
138 result->i_data.a_ops = &smb_file_aops;
139 } else if (S_ISDIR(result->i_mode)) {
140 if (server->opt.capabilities & SMB_CAP_UNIX)
141 result->i_op = &smb_dir_inode_operations_unix;
143 result->i_op = &smb_dir_inode_operations;
144 result->i_fop = &smb_dir_operations;
145 } else if (S_ISLNK(result->i_mode)) {
146 result->i_op = &smb_link_inode_operations;
148 init_special_inode(result, result->i_mode, fattr->f_rdev);
150 insert_inode_hash(result);
155 * Copy the inode data to a smb_fattr structure.
158 smb_get_inode_attr(struct inode *inode, struct smb_fattr *fattr)
160 memset(fattr, 0, sizeof(struct smb_fattr));
161 fattr->f_mode = inode->i_mode;
162 fattr->f_nlink = inode->i_nlink;
163 fattr->f_ino = inode->i_ino;
164 fattr->f_uid = inode->i_uid;
165 fattr->f_gid = inode->i_gid;
166 fattr->f_size = inode->i_size;
167 fattr->f_mtime = inode->i_mtime;
168 fattr->f_ctime = inode->i_ctime;
169 fattr->f_atime = inode->i_atime;
170 fattr->f_blksize= inode->i_blksize;
171 fattr->f_blocks = inode->i_blocks;
173 fattr->attr = SMB_I(inode)->attr;
175 * Keep the attributes in sync with the inode permissions.
177 if (fattr->f_mode & S_IWUSR)
178 fattr->attr &= ~aRONLY;
180 fattr->attr |= aRONLY;
184 * Update the inode, possibly causing it to invalidate its pages if mtime/size
185 * is different from last time.
188 smb_set_inode_attr(struct inode *inode, struct smb_fattr *fattr)
190 struct smb_inode_info *ei = SMB_I(inode);
193 * A size change should have a different mtime, or same mtime
194 * but different size.
196 time_t last_time = inode->i_mtime.tv_sec;
197 loff_t last_sz = inode->i_size;
199 inode->i_mode = fattr->f_mode;
200 inode->i_nlink = fattr->f_nlink;
201 inode->i_uid = fattr->f_uid;
202 inode->i_gid = fattr->f_gid;
203 inode->i_ctime = fattr->f_ctime;
204 inode->i_blksize= fattr->f_blksize;
205 inode->i_blocks = fattr->f_blocks;
206 inode->i_size = fattr->f_size;
207 inode->i_mtime = fattr->f_mtime;
208 inode->i_atime = fattr->f_atime;
209 ei->attr = fattr->attr;
212 * Update the "last time refreshed" field for revalidation.
214 ei->oldmtime = jiffies;
216 if (inode->i_mtime.tv_sec != last_time || inode->i_size != last_sz) {
217 VERBOSE("%ld changed, old=%ld, new=%ld, oz=%ld, nz=%ld\n",
219 (long) last_time, (long) inode->i_mtime,
220 (long) last_sz, (long) inode->i_size);
222 if (!S_ISDIR(inode->i_mode))
223 invalidate_remote_inode(inode);
228 * This is called if the connection has gone bad ...
229 * try to kill off all the current inodes.
232 smb_invalidate_inodes(struct smb_sb_info *server)
235 shrink_dcache_sb(SB_of(server));
236 invalidate_inodes(SB_of(server));
240 * This is called to update the inode attributes after
241 * we've made changes to a file or directory.
244 smb_refresh_inode(struct dentry *dentry)
246 struct inode *inode = dentry->d_inode;
248 struct smb_fattr fattr;
250 error = smb_proc_getattr(dentry, &fattr);
252 smb_renew_times(dentry);
254 * Check whether the type part of the mode changed,
255 * and don't update the attributes if it did.
257 * And don't dick with the root inode
259 if (inode->i_ino == 2)
261 if (S_ISLNK(inode->i_mode))
262 return error; /* VFS will deal with it */
264 if ((inode->i_mode & S_IFMT) == (fattr.f_mode & S_IFMT)) {
265 smb_set_inode_attr(inode, &fattr);
268 * Big trouble! The inode has become a new object,
269 * so any operations attempted on it are invalid.
271 * To limit damage, mark the inode as bad so that
272 * subsequent lookup validations will fail.
274 PARANOIA("%s/%s changed mode, %07o to %07o\n",
276 inode->i_mode, fattr.f_mode);
278 fattr.f_mode = inode->i_mode; /* save mode */
279 make_bad_inode(inode);
280 inode->i_mode = fattr.f_mode; /* restore mode */
282 * No need to worry about unhashing the dentry: the
283 * lookup validation will see that the inode is bad.
284 * But we do want to invalidate the caches ...
286 if (!S_ISDIR(inode->i_mode))
287 invalidate_remote_inode(inode);
289 smb_invalid_dir_cache(inode);
297 * This is called when we want to check whether the inode
298 * has changed on the server. If it has changed, we must
299 * invalidate our local caches.
302 smb_revalidate_inode(struct dentry *dentry)
304 struct smb_sb_info *s = server_from_dentry(dentry);
305 struct inode *inode = dentry->d_inode;
308 DEBUG1("smb_revalidate_inode\n");
312 * Check whether we've recently refreshed the inode.
314 if (time_before(jiffies, SMB_I(inode)->oldmtime + SMB_MAX_AGE(s))) {
315 VERBOSE("up-to-date, ino=%ld, jiffies=%lu, oldtime=%lu\n",
316 inode->i_ino, jiffies, SMB_I(inode)->oldmtime);
320 error = smb_refresh_inode(dentry);
327 * This routine is called when i_nlink == 0 and i_count goes to 0.
328 * All blocking cleanup operations need to go here to avoid races.
331 smb_delete_inode(struct inode *ino)
333 DEBUG1("ino=%ld\n", ino->i_ino);
336 PARANOIA("could not close inode %ld\n", ino->i_ino);
341 static struct option opts[] = {
342 { "version", 0, 'v' },
343 { "win95", SMB_MOUNT_WIN95, 1 },
344 { "oldattr", SMB_MOUNT_OLDATTR, 1 },
345 { "dirattr", SMB_MOUNT_DIRATTR, 1 },
346 { "case", SMB_MOUNT_CASE, 1 },
349 { "file_mode", 0, 'f' },
350 { "dir_mode", 0, 'd' },
351 { "iocharset", 0, 'i' },
352 { "codepage", 0, 'c' },
358 parse_options(struct smb_mount_data_kernel *mnt, char *options)
367 while ( (c = smb_getopt("smbfs", &options, opts,
368 &optopt, &optarg, &flags, &value)) > 0) {
370 VERBOSE("'%s' -> '%s'\n", optopt, optarg ? optarg : "<none>");
374 /* got a "flag" option */
377 if (value != SMB_MOUNT_VERSION) {
378 printk ("smbfs: Bad mount version %ld, expected %d\n",
379 value, SMB_MOUNT_VERSION);
382 mnt->version = value;
391 mnt->file_mode = (value & S_IRWXUGO) | S_IFREG;
394 mnt->dir_mode = (value & S_IRWXUGO) | S_IFDIR;
397 strlcpy(mnt->codepage.local_name, optarg,
401 strlcpy(mnt->codepage.remote_name, optarg,
408 printk ("smbfs: Unrecognized mount option %s\n",
418 * smb_show_options() is for displaying mount options in /proc/mounts.
419 * It tries to avoid showing settings that were not changed from their
423 smb_show_options(struct seq_file *s, struct vfsmount *m)
425 struct smb_mount_data_kernel *mnt = SMB_SB(m->mnt_sb)->mnt;
428 for (i = 0; opts[i].name != NULL; i++)
429 if (mnt->flags & opts[i].flag)
430 seq_printf(s, ",%s", opts[i].name);
433 seq_printf(s, ",uid=%d", mnt->uid);
435 seq_printf(s, ",gid=%d", mnt->gid);
436 if (mnt->mounted_uid != 0)
437 seq_printf(s, ",mounted_uid=%d", mnt->mounted_uid);
440 * Defaults for file_mode and dir_mode are unknown to us; they
441 * depend on the current umask of the user doing the mount.
443 seq_printf(s, ",file_mode=%04o", mnt->file_mode & S_IRWXUGO);
444 seq_printf(s, ",dir_mode=%04o", mnt->dir_mode & S_IRWXUGO);
446 if (strcmp(mnt->codepage.local_name, CONFIG_NLS_DEFAULT))
447 seq_printf(s, ",iocharset=%s", mnt->codepage.local_name);
448 if (strcmp(mnt->codepage.remote_name, SMB_NLS_REMOTE))
449 seq_printf(s, ",codepage=%s", mnt->codepage.remote_name);
451 if (mnt->ttl != SMB_TTL_DEFAULT)
452 seq_printf(s, ",ttl=%d", mnt->ttl);
458 smb_unload_nls(struct smb_sb_info *server)
460 if (server->remote_nls) {
461 unload_nls(server->remote_nls);
462 server->remote_nls = NULL;
464 if (server->local_nls) {
465 unload_nls(server->local_nls);
466 server->local_nls = NULL;
471 smb_put_super(struct super_block *sb)
473 struct smb_sb_info *server = SMB_SB(sb);
475 smb_lock_server(server);
476 server->state = CONN_INVALID;
477 smbiod_unregister_server(server);
479 smb_close_socket(server);
481 if (server->conn_pid)
482 kill_proc(server->conn_pid, SIGTERM, 1);
484 smb_kfree(server->ops);
485 smb_unload_nls(server);
486 sb->s_fs_info = NULL;
487 smb_unlock_server(server);
491 int smb_fill_super(struct super_block *sb, void *raw_data, int silent)
493 struct smb_sb_info *server;
494 struct smb_mount_data_kernel *mnt;
495 struct smb_mount_data *oldmnt;
496 struct inode *root_inode;
497 struct smb_fattr root;
504 oldmnt = (struct smb_mount_data *) raw_data;
505 ver = oldmnt->version;
506 if (ver != SMB_MOUNT_OLDVERSION && cpu_to_be32(ver) != SMB_MOUNT_ASCII)
509 sb->s_flags |= MS_NODIRATIME;
510 sb->s_blocksize = 1024; /* Eh... Is this correct? */
511 sb->s_blocksize_bits = 10;
512 sb->s_magic = SMB_SUPER_MAGIC;
513 sb->s_op = &smb_sops;
515 server = smb_kmalloc(sizeof(struct smb_sb_info), GFP_KERNEL);
518 sb->s_fs_info = server;
519 memset(server, 0, sizeof(struct smb_sb_info));
521 server->super_block = sb;
523 server->sock_file = NULL;
524 init_MUTEX(&server->sem);
525 INIT_LIST_HEAD(&server->entry);
526 INIT_LIST_HEAD(&server->xmitq);
527 INIT_LIST_HEAD(&server->recvq);
528 server->conn_error = 0;
529 server->conn_pid = 0;
530 server->state = CONN_INVALID; /* no connection yet */
531 server->generation = 0;
533 /* Allocate the global temp buffer and some superblock helper structs */
534 /* FIXME: move these to the smb_sb_info struct */
535 VERBOSE("alloc chunk = %d\n", sizeof(struct smb_ops) +
536 sizeof(struct smb_mount_data_kernel));
537 mem = smb_kmalloc(sizeof(struct smb_ops) +
538 sizeof(struct smb_mount_data_kernel), GFP_KERNEL);
543 smb_install_null_ops(server->ops);
544 server->mnt = mem + sizeof(struct smb_ops);
546 /* Setup NLS stuff */
547 server->remote_nls = NULL;
548 server->local_nls = NULL;
552 memset(mnt, 0, sizeof(struct smb_mount_data_kernel));
553 strlcpy(mnt->codepage.local_name, CONFIG_NLS_DEFAULT,
555 strlcpy(mnt->codepage.remote_name, SMB_NLS_REMOTE,
558 mnt->ttl = SMB_TTL_DEFAULT;
559 if (ver == SMB_MOUNT_OLDVERSION) {
560 mnt->version = oldmnt->version;
562 SET_UID(mnt->uid, oldmnt->uid);
563 SET_GID(mnt->gid, oldmnt->gid);
565 mnt->file_mode = (oldmnt->file_mode & S_IRWXUGO) | S_IFREG;
566 mnt->dir_mode = (oldmnt->dir_mode & S_IRWXUGO) | S_IFDIR;
568 mnt->flags = (oldmnt->file_mode >> 9);
570 if (parse_options(mnt, raw_data))
573 mnt->mounted_uid = current->uid;
574 smb_setcodepage(server, &mnt->codepage);
577 * Display the enabled options
578 * Note: smb_proc_getattr uses these in 2.4 (but was changed in 2.2)
580 if (mnt->flags & SMB_MOUNT_OLDATTR)
581 printk("SMBFS: Using core getattr (Win 95 speedup)\n");
582 else if (mnt->flags & SMB_MOUNT_DIRATTR)
583 printk("SMBFS: Using dir ff getattr\n");
585 if (smbiod_register_server(server) < 0) {
586 printk(KERN_ERR "smbfs: failed to start smbiod\n");
591 * Keep the super block locked while we get the root inode.
593 smb_init_root_dirent(server, &root);
594 root_inode = smb_iget(sb, &root);
598 sb->s_root = d_alloc_root(root_inode);
601 smb_new_dentry(sb->s_root);
608 smb_unload_nls(server);
613 printk(KERN_ERR "smb_fill_super: allocation failure\n");
614 sb->s_fs_info = NULL;
618 printk(KERN_ERR "smbfs: mount_data version %d is not supported\n", ver);
621 printk(KERN_ERR "smb_fill_super: missing data argument\n");
625 printk(KERN_ERR "smb_fill_super: cannot allocate struct smb_sb_info\n");
630 smb_statfs(struct super_block *sb, struct kstatfs *buf)
636 result = smb_proc_dskattr(sb, buf);
640 buf->f_type = SMB_SUPER_MAGIC;
641 buf->f_namelen = SMB_MAXPATHLEN;
645 int smb_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
647 int err = smb_revalidate_inode(dentry);
649 generic_fillattr(dentry->d_inode, stat);
654 smb_notify_change(struct dentry *dentry, struct iattr *attr)
656 struct inode *inode = dentry->d_inode;
657 struct smb_sb_info *server = server_from_dentry(dentry);
658 unsigned int mask = (S_IFREG | S_IFDIR | S_IRWXUGO);
659 int error, changed, refresh = 0;
660 struct smb_fattr fattr;
664 error = smb_revalidate_inode(dentry);
668 if ((error = inode_change_ok(inode, attr)) < 0)
672 if ((attr->ia_valid & ATTR_UID) && (attr->ia_uid != server->mnt->uid))
675 if ((attr->ia_valid & ATTR_GID) && (attr->ia_uid != server->mnt->gid))
678 if ((attr->ia_valid & ATTR_MODE) && (attr->ia_mode & ~mask))
681 if ((attr->ia_valid & ATTR_SIZE) != 0) {
682 VERBOSE("changing %s/%s, old size=%ld, new size=%ld\n",
684 (long) inode->i_size, (long) attr->ia_size);
686 filemap_fdatawrite(inode->i_mapping);
687 filemap_fdatawait(inode->i_mapping);
689 error = smb_open(dentry, O_WRONLY);
692 error = server->ops->truncate(inode, attr->ia_size);
695 error = vmtruncate(inode, attr->ia_size);
701 if (server->opt.capabilities & SMB_CAP_UNIX) {
702 /* For now we don't want to set the size with setattr_unix */
703 attr->ia_valid &= ~ATTR_SIZE;
704 /* FIXME: only call if we actually want to set something? */
705 error = smb_proc_setattr_unix(dentry, attr, 0, 0);
713 * Initialize the fattr and check for changed fields.
714 * Note: CTIME under SMB is creation time rather than
715 * change time, so we don't attempt to change it.
717 smb_get_inode_attr(inode, &fattr);
720 if ((attr->ia_valid & ATTR_MTIME) != 0) {
721 fattr.f_mtime = attr->ia_mtime;
724 if ((attr->ia_valid & ATTR_ATIME) != 0) {
725 fattr.f_atime = attr->ia_atime;
726 /* Earlier protocols don't have an access time */
727 if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2)
731 error = smb_proc_settime(dentry, &fattr);
738 * Check for mode changes ... we're extremely limited in
739 * what can be set for SMB servers: just the read-only bit.
741 if ((attr->ia_valid & ATTR_MODE) != 0) {
742 VERBOSE("%s/%s mode change, old=%x, new=%x\n",
743 DENTRY_PATH(dentry), fattr.f_mode, attr->ia_mode);
745 if (attr->ia_mode & S_IWUSR) {
746 if (fattr.attr & aRONLY) {
747 fattr.attr &= ~aRONLY;
751 if (!(fattr.attr & aRONLY)) {
752 fattr.attr |= aRONLY;
757 error = smb_proc_setattr(dentry, &fattr);
767 smb_refresh_inode(dentry);
772 #ifdef DEBUG_SMB_MALLOC
774 int smb_current_kmalloced;
775 int smb_current_vmalloced;
778 static struct super_block *smb_get_sb(struct file_system_type *fs_type,
779 int flags, const char *dev_name, void *data)
781 return get_sb_nodev(fs_type, flags, data, smb_fill_super);
784 static struct file_system_type smb_fs_type = {
785 .owner = THIS_MODULE,
787 .get_sb = smb_get_sb,
788 .kill_sb = kill_anon_super,
789 .fs_flags = FS_BINARY_MOUNTDATA,
792 static int __init init_smb_fs(void)
795 DEBUG1("registering ...\n");
797 #ifdef DEBUG_SMB_MALLOC
799 smb_current_kmalloced = 0;
800 smb_current_vmalloced = 0;
803 err = init_inodecache();
806 err = smb_init_request_cache();
809 err = register_filesystem(&smb_fs_type);
814 smb_destroy_request_cache();
816 destroy_inodecache();
821 static void __exit exit_smb_fs(void)
823 DEBUG1("unregistering ...\n");
824 unregister_filesystem(&smb_fs_type);
825 smb_destroy_request_cache();
826 destroy_inodecache();
827 #ifdef DEBUG_SMB_MALLOC
828 printk(KERN_DEBUG "smb_malloced: %d\n", smb_malloced);
829 printk(KERN_DEBUG "smb_current_kmalloced: %d\n",smb_current_kmalloced);
830 printk(KERN_DEBUG "smb_current_vmalloced: %d\n",smb_current_vmalloced);
834 module_init(init_smb_fs)
835 module_exit(exit_smb_fs)
836 MODULE_LICENSE("GPL");