File: /usr/src/linux/fs/smbfs/inode.c

1     /*
2      *  inode.c
3      *
4      *  Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
5      *  Copyright (C) 1997 by Volker Lendecke
6      *
7      *  Please add a note about your changes to smbfs in the ChangeLog file.
8      */
9     
10     #include <linux/config.h>
11     #include <linux/module.h>
12     #include <linux/sched.h>
13     #include <linux/kernel.h>
14     #include <linux/mm.h>
15     #include <linux/string.h>
16     #include <linux/stat.h>
17     #include <linux/errno.h>
18     #include <linux/locks.h>
19     #include <linux/slab.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>
25     
26     #include <linux/smb_fs.h>
27     #include <linux/smbno.h>
28     #include <linux/smb_mount.h>
29     
30     #include <asm/system.h>
31     #include <asm/uaccess.h>
32     
33     #include "smb_debug.h"
34     #include "getopt.h"
35     
36     /* Always pick a default string */
37     #ifdef CONFIG_SMB_NLS_REMOTE
38     #define SMB_NLS_REMOTE CONFIG_SMB_NLS_REMOTE
39     #else
40     #define SMB_NLS_REMOTE ""
41     #endif
42     
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     
47     static struct super_operations smb_sops =
48     {
49     	put_inode:	force_delete,
50     	delete_inode:	smb_delete_inode,
51     	put_super:	smb_put_super,
52     	statfs:		smb_statfs,
53     };
54     
55     
56     /* We are always generating a new inode here */
57     struct inode *
58     smb_iget(struct super_block *sb, struct smb_fattr *fattr)
59     {
60     	struct inode *result;
61     
62     	DEBUG1("smb_iget: %p\n", fattr);
63     
64     	result = new_inode(sb);
65     	if (!result)
66     		return result;
67     	result->i_ino = fattr->f_ino;
68     	memset(&(result->u.smbfs_i), 0, sizeof(result->u.smbfs_i));
69     	smb_set_inode_attr(result, fattr);
70     	if (S_ISREG(result->i_mode)) {
71     		result->i_op = &smb_file_inode_operations;
72     		result->i_fop = &smb_file_operations;
73     		result->i_data.a_ops = &smb_file_aops;
74     	} else if (S_ISDIR(result->i_mode)) {
75     		result->i_op = &smb_dir_inode_operations;
76     		result->i_fop = &smb_dir_operations;
77     	}
78     	insert_inode_hash(result);
79     	return result;
80     }
81     
82     /*
83      * Copy the inode data to a smb_fattr structure.
84      */
85     void
86     smb_get_inode_attr(struct inode *inode, struct smb_fattr *fattr)
87     {
88     	memset(fattr, 0, sizeof(struct smb_fattr));
89     	fattr->f_mode	= inode->i_mode;
90     	fattr->f_nlink	= inode->i_nlink;
91     	fattr->f_ino	= inode->i_ino;
92     	fattr->f_uid	= inode->i_uid;
93     	fattr->f_gid	= inode->i_gid;
94     	fattr->f_rdev	= inode->i_rdev;
95     	fattr->f_size	= inode->i_size;
96     	fattr->f_mtime	= inode->i_mtime;
97     	fattr->f_ctime	= inode->i_ctime;
98     	fattr->f_atime	= inode->i_atime;
99     	fattr->f_blksize= inode->i_blksize;
100     	fattr->f_blocks	= inode->i_blocks;
101     
102     	fattr->attr	= inode->u.smbfs_i.attr;
103     	/*
104     	 * Keep the attributes in sync with the inode permissions.
105     	 */
106     	if (fattr->f_mode & S_IWUSR)
107     		fattr->attr &= ~aRONLY;
108     	else
109     		fattr->attr |= aRONLY;
110     }
111     
112     /*
113      * Update the inode, possibly causing it to invalidate its pages if mtime/size
114      * is different from last time.
115      */
116     void
117     smb_set_inode_attr(struct inode *inode, struct smb_fattr *fattr)
118     {
119     	/*
120     	 * A size change should have a different mtime, or same mtime
121     	 * but different size.
122     	 */
123     	time_t last_time = inode->i_mtime;
124     	loff_t last_sz = inode->i_size;
125     
126     	inode->i_mode	= fattr->f_mode;
127     	inode->i_nlink	= fattr->f_nlink;
128     	inode->i_uid	= fattr->f_uid;
129     	inode->i_gid	= fattr->f_gid;
130     	inode->i_rdev	= fattr->f_rdev;
131     	inode->i_ctime	= fattr->f_ctime;
132     	inode->i_blksize= fattr->f_blksize;
133     	inode->i_blocks = fattr->f_blocks;
134     	inode->i_size	= fattr->f_size;
135     	inode->i_mtime	= fattr->f_mtime;
136     	inode->i_atime	= fattr->f_atime;
137     	inode->u.smbfs_i.attr = fattr->attr;
138     	/*
139     	 * Update the "last time refreshed" field for revalidation.
140     	 */
141     	inode->u.smbfs_i.oldmtime = jiffies;
142     
143     	if (inode->i_mtime != last_time || inode->i_size != last_sz) {
144     		VERBOSE("%ld changed, old=%ld, new=%ld, oz=%ld, nz=%ld\n",
145     			inode->i_ino,
146     			(long) last_time, (long) inode->i_mtime,
147     			(long) last_sz, (long) inode->i_size);
148     
149     		if (!S_ISDIR(inode->i_mode))
150     			invalidate_inode_pages(inode);
151     	}
152     }
153     
154     /*
155      * This is called if the connection has gone bad ...
156      * try to kill off all the current inodes.
157      */
158     void
159     smb_invalidate_inodes(struct smb_sb_info *server)
160     {
161     	VERBOSE("\n");
162     	shrink_dcache_sb(SB_of(server));
163     	invalidate_inodes(SB_of(server));
164     }
165     
166     /*
167      * This is called to update the inode attributes after
168      * we've made changes to a file or directory.
169      */
170     static int
171     smb_refresh_inode(struct dentry *dentry)
172     {
173     	struct inode *inode = dentry->d_inode;
174     	int error;
175     	struct smb_fattr fattr;
176     
177     	error = smb_proc_getattr(dentry, &fattr);
178     	if (!error) {
179     		smb_renew_times(dentry);
180     		/*
181     		 * Check whether the type part of the mode changed,
182     		 * and don't update the attributes if it did.
183     		 */
184     		if ((inode->i_mode & S_IFMT) == (fattr.f_mode & S_IFMT)) {
185     			smb_set_inode_attr(inode, &fattr);
186     		} else {
187     			/*
188     			 * Big trouble! The inode has become a new object,
189     			 * so any operations attempted on it are invalid.
190     			 *
191     			 * To limit damage, mark the inode as bad so that
192     			 * subsequent lookup validations will fail.
193     			 */
194     			PARANOIA("%s/%s changed mode, %07o to %07o\n",
195     				 DENTRY_PATH(dentry),
196     				 inode->i_mode, fattr.f_mode);
197     
198     			fattr.f_mode = inode->i_mode; /* save mode */
199     			make_bad_inode(inode);
200     			inode->i_mode = fattr.f_mode; /* restore mode */
201     			/*
202     			 * No need to worry about unhashing the dentry: the
203     			 * lookup validation will see that the inode is bad.
204     			 * But we do want to invalidate the caches ...
205     			 */
206     			if (!S_ISDIR(inode->i_mode))
207     				invalidate_inode_pages(inode);
208     			else
209     				smb_invalid_dir_cache(inode);
210     			error = -EIO;
211     		}
212     	}
213     	return error;
214     }
215     
216     /*
217      * This is called when we want to check whether the inode
218      * has changed on the server.  If it has changed, we must
219      * invalidate our local caches.
220      */
221     int
222     smb_revalidate_inode(struct dentry *dentry)
223     {
224     	struct smb_sb_info *s = server_from_dentry(dentry);
225     	struct inode *inode = dentry->d_inode;
226     	int error = 0;
227     
228     	DEBUG1("smb_revalidate_inode\n");
229     	lock_kernel();
230     
231     	/*
232     	 * Check whether we've recently refreshed the inode.
233     	 */
234     	if (time_before(jiffies, inode->u.smbfs_i.oldmtime + SMB_MAX_AGE(s))) {
235     		VERBOSE("up-to-date, ino=%ld, jiffies=%lu, oldtime=%lu\n",
236     			inode->i_ino, jiffies, inode->u.smbfs_i.oldmtime);
237     		goto out;
238     	}
239     
240     	error = smb_refresh_inode(dentry);
241     out:
242     	unlock_kernel();
243     	return error;
244     }
245     
246     /*
247      * This routine is called when i_nlink == 0 and i_count goes to 0.
248      * All blocking cleanup operations need to go here to avoid races.
249      */
250     static void
251     smb_delete_inode(struct inode *ino)
252     {
253     	DEBUG1("ino=%ld\n", ino->i_ino);
254     	lock_kernel();
255     	if (smb_close(ino))
256     		PARANOIA("could not close inode %ld\n", ino->i_ino);
257     	unlock_kernel();
258     	clear_inode(ino);
259     }
260     
261     /* FIXME: flags and has_arg could probably be merged. */
262     struct option opts[] = {
263     	{ "version",	1, 0, 'v' },
264     	{ "win95",	0, SMB_MOUNT_WIN95, 1 },
265     	{ "oldattr",	0, SMB_MOUNT_OLDATTR, 1 },
266     	{ "dirattr",	0, SMB_MOUNT_DIRATTR, 1 },
267     	{ "case",	0, SMB_MOUNT_CASE, 1 },
268     	{ "uid",	1, 0, 'u' },
269     	{ "gid",	1, 0, 'g' },
270     	{ "file_mode",	1, 0, 'f' },
271     	{ "dir_mode",	1, 0, 'd' },
272     	{ "iocharset",	1, 0, 'i' },
273     	{ "codepage",	1, 0, 'c' },
274     	{ "ttl",	1, 0, 't' },
275     	{ NULL,		0, 0, 0}
276     };
277     
278     static int
279     parse_options(struct smb_mount_data_kernel *mnt, char *options)
280     {
281     	int c;
282     	unsigned long flags;
283     	unsigned long value;
284     	char *optarg;
285     	char *optopt;
286     
287     	flags = 0;
288     	while ( (c = smb_getopt("smbfs", &options, opts,
289     				&optopt, &optarg, &flags, &value)) > 0) {
290     
291     		VERBOSE("'%s' -> '%s'\n", optopt, optarg ? optarg : "<none>");
292     
293     		switch (c) {
294     		case 1:
295     			/* got a "flag" option */
296     			break;
297     		case 'v':
298     			if (value != SMB_MOUNT_VERSION) {
299     			printk ("smbfs: Bad mount version %ld, expected %d\n",
300     				value, SMB_MOUNT_VERSION);
301     				return 0;
302     			}
303     			mnt->version = value;
304     			break;
305     		case 'u':
306     			mnt->uid = value;
307     			break;
308     		case 'g':
309     			mnt->gid = value;
310     			break;
311     		case 'f':
312     			mnt->file_mode = value & (S_IRWXU | S_IRWXG | S_IRWXO);
313     			mnt->file_mode |= S_IFREG;
314     			break;
315     		case 'd':
316     			mnt->dir_mode = value & (S_IRWXU | S_IRWXG | S_IRWXO);
317     			mnt->dir_mode |= S_IFDIR;
318     			break;
319     		case 'i':
320     			strncpy(mnt->codepage.local_name, optarg, 
321     				SMB_NLS_MAXNAMELEN);
322     			break;
323     		case 'c':
324     			strncpy(mnt->codepage.remote_name, optarg,
325     				SMB_NLS_MAXNAMELEN);
326     			break;
327     		case 't':
328     			mnt->ttl = value;
329     			break;
330     		default:
331     			printk ("smbfs: Unrecognized mount option %s\n",
332     				optopt);
333     			return -1;
334     		}
335     	}
336     	mnt->flags = flags;
337     	return c;
338     }
339     
340     
341     static void
342     smb_put_super(struct super_block *sb)
343     {
344     	struct smb_sb_info *server = &(sb->u.smbfs_sb);
345     
346     	if (server->sock_file) {
347     		smb_proc_disconnect(server);
348     		smb_dont_catch_keepalive(server);
349     		fput(server->sock_file);
350     	}
351     
352     	if (server->conn_pid)
353     	       kill_proc(server->conn_pid, SIGTERM, 1);
354     
355     	smb_kfree(server->mnt);
356     	smb_kfree(sb->u.smbfs_sb.temp_buf);
357     	if (server->packet)
358     		smb_vfree(server->packet);
359     
360     	if(sb->u.smbfs_sb.remote_nls) {
361     		unload_nls(sb->u.smbfs_sb.remote_nls);
362     		sb->u.smbfs_sb.remote_nls = NULL;
363     	}
364     	if(sb->u.smbfs_sb.local_nls) {
365     		unload_nls(sb->u.smbfs_sb.local_nls);
366     		sb->u.smbfs_sb.local_nls = NULL;
367     	}
368     }
369     
370     struct super_block *
371     smb_read_super(struct super_block *sb, void *raw_data, int silent)
372     {
373     	struct smb_mount_data_kernel *mnt;
374     	struct smb_mount_data *oldmnt;
375     	struct inode *root_inode;
376     	struct smb_fattr root;
377     	int ver;
378     
379     	if (!raw_data)
380     		goto out_no_data;
381     
382     	oldmnt = (struct smb_mount_data *) raw_data;
383     	ver = oldmnt->version;
384     	if (ver != SMB_MOUNT_OLDVERSION && cpu_to_be32(ver) != SMB_MOUNT_ASCII)
385     		goto out_wrong_data;
386     
387     	sb->s_blocksize = 1024;	/* Eh...  Is this correct? */
388     	sb->s_blocksize_bits = 10;
389     	sb->s_magic = SMB_SUPER_MAGIC;
390     	sb->s_op = &smb_sops;
391     
392     	sb->u.smbfs_sb.mnt = NULL;
393     	sb->u.smbfs_sb.sock_file = NULL;
394     	init_MUTEX(&sb->u.smbfs_sb.sem);
395     	init_waitqueue_head(&sb->u.smbfs_sb.wait);
396     	sb->u.smbfs_sb.conn_pid = 0;
397     	sb->u.smbfs_sb.state = CONN_INVALID; /* no connection yet */
398     	sb->u.smbfs_sb.generation = 0;
399     	sb->u.smbfs_sb.packet_size = smb_round_length(SMB_INITIAL_PACKET_SIZE);
400     	sb->u.smbfs_sb.packet = smb_vmalloc(sb->u.smbfs_sb.packet_size);
401     	if (!sb->u.smbfs_sb.packet)
402     		goto out_no_mem;
403     
404     	/* Allocate the global temp buffer */
405     	sb->u.smbfs_sb.temp_buf = smb_kmalloc(2*SMB_MAXPATHLEN+20, GFP_KERNEL);
406     	if (!sb->u.smbfs_sb.temp_buf)
407     		goto out_no_temp;
408     
409     	/* Setup NLS stuff */
410     	sb->u.smbfs_sb.remote_nls = NULL;
411     	sb->u.smbfs_sb.local_nls = NULL;
412     	sb->u.smbfs_sb.name_buf = sb->u.smbfs_sb.temp_buf + SMB_MAXPATHLEN + 20;
413     
414     	/* Allocate the mount data structure */
415     	/* FIXME: merge this with the other malloc and get a whole page? */
416     	mnt = smb_kmalloc(sizeof(struct smb_mount_data_kernel), GFP_KERNEL);
417     	if (!mnt)
418     		goto out_no_mount;
419     	sb->u.smbfs_sb.mnt = mnt;
420     
421     	memset(mnt, 0, sizeof(struct smb_mount_data_kernel));
422     	strncpy(mnt->codepage.local_name, CONFIG_NLS_DEFAULT,
423     		SMB_NLS_MAXNAMELEN);
424     	strncpy(mnt->codepage.remote_name, SMB_NLS_REMOTE,
425     		SMB_NLS_MAXNAMELEN);
426     
427     	mnt->ttl = 1000;
428     	if (ver == SMB_MOUNT_OLDVERSION) {
429     		mnt->version = oldmnt->version;
430     
431     		/* FIXME: is this enough to convert uid/gid's ? */
432     		mnt->mounted_uid = oldmnt->mounted_uid;
433     		mnt->uid = oldmnt->uid;
434     		mnt->gid = oldmnt->gid;
435     
436     		mnt->file_mode =
437     			oldmnt->file_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
438     		mnt->dir_mode =
439     			oldmnt->dir_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
440     		mnt->file_mode |= S_IFREG;
441     		mnt->dir_mode  |= S_IFDIR;
442     
443     		mnt->flags = (oldmnt->file_mode >> 9);
444     	} else {
445     		if (parse_options(mnt, raw_data))
446     			goto out_bad_option;
447     
448     		mnt->mounted_uid = current->uid;
449     	}
450     	smb_setcodepage(&sb->u.smbfs_sb, &mnt->codepage);
451     	if (!sb->u.smbfs_sb.convert)
452     		PARANOIA("convert funcptr was NULL!\n");
453     
454     	/*
455     	 * Display the enabled options
456     	 * Note: smb_proc_getattr uses these in 2.4 (but was changed in 2.2)
457     	 */
458     	if (mnt->flags & SMB_MOUNT_OLDATTR)
459     		printk("SMBFS: Using core getattr (Win 95 speedup)\n");
460     	else if (mnt->flags & SMB_MOUNT_DIRATTR)
461     		printk("SMBFS: Using dir ff getattr\n");
462     
463     	/*
464     	 * Keep the super block locked while we get the root inode.
465     	 */
466     	smb_init_root_dirent(&(sb->u.smbfs_sb), &root);
467     	root_inode = smb_iget(sb, &root);
468     	if (!root_inode)
469     		goto out_no_root;
470     
471     	sb->s_root = d_alloc_root(root_inode);
472     	if (!sb->s_root)
473     		goto out_no_root;
474     	smb_new_dentry(sb->s_root);
475     
476     	return sb;
477     
478     out_no_root:
479     	iput(root_inode);
480     out_bad_option:
481     	smb_kfree(sb->u.smbfs_sb.mnt);
482     out_no_mount:
483     	smb_kfree(sb->u.smbfs_sb.temp_buf);
484     out_no_temp:
485     	smb_vfree(sb->u.smbfs_sb.packet);
486     out_no_mem:
487     	if (!sb->u.smbfs_sb.mnt)
488     		printk(KERN_ERR "smb_read_super: allocation failure\n");
489     	goto out_fail;
490     out_wrong_data:
491     	printk(KERN_ERR "smbfs: mount_data version %d is not supported\n", ver);
492     	goto out_fail;
493     out_no_data:
494     	printk(KERN_ERR "smb_read_super: missing data argument\n");
495     out_fail:
496     	return NULL;
497     }
498     
499     static int
500     smb_statfs(struct super_block *sb, struct statfs *buf)
501     {
502     	smb_proc_dskattr(sb, buf);
503     
504     	buf->f_type = SMB_SUPER_MAGIC;
505     	buf->f_namelen = SMB_MAXPATHLEN;
506     	return 0;
507     }
508     
509     int
510     smb_notify_change(struct dentry *dentry, struct iattr *attr)
511     {
512     	struct inode *inode = dentry->d_inode;
513     	struct smb_sb_info *server = server_from_dentry(dentry);
514     	unsigned int mask = (S_IFREG | S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO);
515     	int error, changed, refresh = 0;
516     	struct smb_fattr fattr;
517     
518     	error = smb_revalidate_inode(dentry);
519     	if (error)
520     		goto out;
521     
522     	if ((error = inode_change_ok(inode, attr)) < 0)
523     		goto out;
524     
525     	error = -EPERM;
526     	if ((attr->ia_valid & ATTR_UID) && (attr->ia_uid != server->mnt->uid))
527     		goto out;
528     
529     	if ((attr->ia_valid & ATTR_GID) && (attr->ia_uid != server->mnt->gid))
530     		goto out;
531     
532     	if ((attr->ia_valid & ATTR_MODE) && (attr->ia_mode & ~mask))
533     		goto out;
534     
535     	if ((attr->ia_valid & ATTR_SIZE) != 0)
536     	{
537     		VERBOSE("changing %s/%s, old size=%ld, new size=%ld\n",
538     			DENTRY_PATH(dentry),
539     			(long) inode->i_size, (long) attr->ia_size);
540     		error = smb_open(dentry, O_WRONLY);
541     		if (error)
542     			goto out;
543     		error = smb_proc_trunc(server, inode->u.smbfs_i.fileid,
544     					 attr->ia_size);
545     		if (error)
546     			goto out;
547     		error = vmtruncate(inode, attr->ia_size);
548     		if (error)
549     			goto out;
550     		refresh = 1;
551     	}
552     
553     	/*
554     	 * Initialize the fattr and check for changed fields.
555     	 * Note: CTIME under SMB is creation time rather than
556     	 * change time, so we don't attempt to change it.
557     	 */
558     	smb_get_inode_attr(inode, &fattr);
559     
560     	changed = 0;
561     	if ((attr->ia_valid & ATTR_MTIME) != 0)
562     	{
563     		fattr.f_mtime = attr->ia_mtime;
564     		changed = 1;
565     	}
566     	if ((attr->ia_valid & ATTR_ATIME) != 0)
567     	{
568     		fattr.f_atime = attr->ia_atime;
569     		/* Earlier protocols don't have an access time */
570     		if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2)
571     			changed = 1;
572     	}
573     	if (changed)
574     	{
575     		error = smb_proc_settime(dentry, &fattr);
576     		if (error)
577     			goto out;
578     		refresh = 1;
579     	}
580     
581     	/*
582     	 * Check for mode changes ... we're extremely limited in
583     	 * what can be set for SMB servers: just the read-only bit.
584     	 */
585     	if ((attr->ia_valid & ATTR_MODE) != 0)
586     	{
587     		VERBOSE("%s/%s mode change, old=%x, new=%x\n",
588     			DENTRY_PATH(dentry), fattr.f_mode, attr->ia_mode);
589     		changed = 0;
590     		if (attr->ia_mode & S_IWUSR)
591     		{
592     			if (fattr.attr & aRONLY)
593     			{
594     				fattr.attr &= ~aRONLY;
595     				changed = 1;
596     			}
597     		} else {
598     			if (!(fattr.attr & aRONLY))
599     			{
600     				fattr.attr |= aRONLY;
601     				changed = 1;
602     			}
603     		}
604     		if (changed)
605     		{
606     			error = smb_proc_setattr(dentry, &fattr);
607     			if (error)
608     				goto out;
609     			refresh = 1;
610     		}
611     	}
612     	error = 0;
613     
614     out:
615     	if (refresh)
616     		smb_refresh_inode(dentry);
617     	return error;
618     }
619     
620     #ifdef DEBUG_SMB_MALLOC
621     int smb_malloced;
622     int smb_current_kmalloced;
623     int smb_current_vmalloced;
624     #endif
625     
626     static DECLARE_FSTYPE( smb_fs_type, "smbfs", smb_read_super, 0);
627     
628     static int __init init_smb_fs(void)
629     {
630     	DEBUG1("registering ...\n");
631     
632     #ifdef DEBUG_SMB_MALLOC
633     	smb_malloced = 0;
634     	smb_current_kmalloced = 0;
635     	smb_current_vmalloced = 0;
636     #endif
637     
638     	return register_filesystem(&smb_fs_type);
639     }
640     
641     static void __exit exit_smb_fs(void)
642     {
643     	DEBUG1("unregistering ...\n");
644     	unregister_filesystem(&smb_fs_type);
645     #ifdef DEBUG_SMB_MALLOC
646     	printk(KERN_DEBUG "smb_malloced: %d\n", smb_malloced);
647     	printk(KERN_DEBUG "smb_current_kmalloced: %d\n",smb_current_kmalloced);
648     	printk(KERN_DEBUG "smb_current_vmalloced: %d\n",smb_current_vmalloced);
649     #endif
650     }
651     
652     EXPORT_NO_SYMBOLS;
653     
654     module_init(init_smb_fs)
655     module_exit(exit_smb_fs)
656