- Stephen Rothwell: APM updates
[davej-history.git] / fs / smbfs / inode.c
blobe502fb60b0e6e2174ec1263c4524a7c31599c1b0
1 /*
2 * inode.c
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.
8 */
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/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"
34 #include "getopt.h"
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
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,
53 statfs: smb_statfs,
57 /* We are always generating a new inode here */
58 struct inode *
59 smb_iget(struct super_block *sb, struct smb_fattr *fattr)
61 struct inode *result;
63 DEBUG1("smb_iget: %p\n", fattr);
65 result = new_inode(sb);
66 if (!result)
67 return result;
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);
80 return result;
84 * Copy the inode data to a smb_fattr structure.
86 void
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;
109 else
110 fattr->attr |= aRONLY;
113 static void
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.
146 void
147 smb_invalidate_inodes(struct smb_sb_info *server)
149 VERBOSE("\n");
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.
158 static int
159 smb_refresh_inode(struct dentry *dentry)
161 struct inode *inode = dentry->d_inode;
162 int error;
163 struct smb_fattr fattr;
165 error = smb_proc_getattr(dentry, &fattr);
166 if (!error)
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);
175 else
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",
185 DENTRY_PATH(dentry),
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);
198 else
199 smb_invalid_dir_cache(inode);
200 error = -EIO;
203 return error;
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;
215 time_t last_time;
216 int error = 0;
218 DEBUG1("smb_revalidate_inode\n");
220 * If this is a file opened with write permissions,
221 * the inode will be up-to-date.
223 lock_kernel();
224 if (S_ISREG(inode->i_mode) && smb_is_open(inode))
226 if (inode->u.smbfs_i.access != SMB_O_RDONLY)
227 goto out;
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);
237 goto out;
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",
249 DENTRY_PATH(dentry),
250 (long) last_time, (long) inode->i_mtime);
252 if (!S_ISDIR(inode->i_mode))
253 invalidate_inode_pages(inode);
254 else
255 smb_invalid_dir_cache(inode);
257 out:
258 unlock_kernel();
259 return error;
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.
266 static void
267 smb_delete_inode(struct inode *ino)
269 DEBUG1("ino=%ld\n", ino->i_ino);
270 lock_kernel();
271 if (smb_close(ino))
272 PARANOIA("could not close inode %ld\n", ino->i_ino);
273 unlock_kernel();
274 clear_inode(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' },
290 { NULL, 0, 0, 0}
293 static int
294 parse_options(struct smb_mount_data_kernel *mnt, char *options)
296 int c;
297 unsigned long flags;
298 unsigned long value;
299 char *optarg;
300 char *optopt;
302 flags = 0;
303 while ( (c = smb_getopt("smbfs", &options, opts,
304 &optopt, &optarg, &flags, &value)) > 0) {
306 VERBOSE("'%s' -> '%s'\n", optopt, optarg ? optarg : "<none>");
308 switch (c) {
309 case 1:
310 /* got a "flag" option */
311 break;
312 case 'v':
313 if (value != SMB_MOUNT_VERSION) {
314 printk ("smbfs: Bad mount version %ld, expected %d\n",
315 value, SMB_MOUNT_VERSION);
316 return 0;
318 mnt->version = value;
319 break;
320 case 'u':
321 mnt->uid = value;
322 break;
323 case 'g':
324 mnt->gid = value;
325 break;
326 case 'f':
327 mnt->file_mode = value & (S_IRWXU | S_IRWXG | S_IRWXO);
328 mnt->file_mode |= S_IFREG;
329 break;
330 case 'd':
331 mnt->dir_mode = value & (S_IRWXU | S_IRWXG | S_IRWXO);
332 mnt->dir_mode |= S_IFDIR;
333 break;
334 case 'i':
335 strncpy(mnt->codepage.local_name, optarg,
336 SMB_NLS_MAXNAMELEN);
337 break;
338 case 'c':
339 strncpy(mnt->codepage.remote_name, optarg,
340 SMB_NLS_MAXNAMELEN);
341 break;
342 default:
343 printk ("smbfs: Unrecognized mount option %s\n",
344 optopt);
345 return -1;
348 mnt->flags = flags;
349 return c;
353 static void
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);
367 kfree(server->mnt);
368 kfree(sb->u.smbfs_sb.temp_buf);
369 if (server->packet)
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;
382 struct super_block *
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;
389 int ver;
391 if (!raw_data)
392 goto out_no_data;
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)
397 goto out_wrong_data;
399 sb->s_blocksize = 1024; /* Eh... Is this correct? */
400 sb->s_blocksize_bits = 10;
401 sb->s_magic = SMB_SUPER_MAGIC;
402 sb->s_flags = 0;
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)
415 goto out_no_mem;
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)
420 goto out_no_temp;
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);
430 if (!mnt)
431 goto out_no_mount;
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,
436 SMB_NLS_MAXNAMELEN);
437 strncpy(mnt->codepage.remote_name, SMB_NLS_REMOTE,
438 SMB_NLS_MAXNAMELEN);
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;
448 mnt->file_mode =
449 oldmnt->file_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
450 mnt->dir_mode =
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);
456 } else {
457 if (parse_options(mnt, raw_data))
458 goto out_bad_option;
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);
480 if (!root_inode)
481 goto out_no_root;
483 sb->s_root = d_alloc_root(root_inode);
484 if (!sb->s_root)
485 goto out_no_root;
487 return sb;
489 out_no_root:
490 iput(root_inode);
491 out_bad_option:
492 kfree(sb->u.smbfs_sb.mnt);
493 out_no_mount:
494 kfree(sb->u.smbfs_sb.temp_buf);
495 out_no_temp:
496 smb_vfree(sb->u.smbfs_sb.packet);
497 out_no_mem:
498 if (!sb->u.smbfs_sb.mnt)
499 printk(KERN_ERR "smb_read_super: allocation failure\n");
500 goto out_fail;
501 out_wrong_data:
502 printk(KERN_ERR "smbfs: mount_data version %d is not supported\n", ver);
503 goto out_fail;
504 out_no_data:
505 printk(KERN_ERR "smb_read_super: missing data argument\n");
506 out_fail:
507 return NULL;
510 static int
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;
517 return 0;
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);
530 if (error)
531 goto out;
533 if ((error = inode_change_ok(inode, attr)) < 0)
534 goto out;
536 error = -EPERM;
537 if ((attr->ia_valid & ATTR_UID) && (attr->ia_uid != server->mnt->uid))
538 goto out;
540 if ((attr->ia_valid & ATTR_GID) && (attr->ia_uid != server->mnt->gid))
541 goto out;
543 if ((attr->ia_valid & ATTR_MODE) && (attr->ia_mode & ~mask))
544 goto out;
546 if ((attr->ia_valid & ATTR_SIZE) != 0)
548 VERBOSE("changing %s/%s, old size=%ld, new size=%ld\n",
549 DENTRY_PATH(dentry),
550 (long) inode->i_size, (long) attr->ia_size);
551 error = smb_open(dentry, O_WRONLY);
552 if (error)
553 goto out;
554 error = smb_proc_trunc(server, inode->u.smbfs_i.fileid,
555 attr->ia_size);
556 if (error)
557 goto out;
558 vmtruncate(inode, attr->ia_size);
559 refresh = 1;
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);
569 changed = 0;
570 if ((attr->ia_valid & ATTR_MTIME) != 0)
572 fattr.f_mtime = attr->ia_mtime;
573 changed = 1;
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)
580 changed = 1;
582 if (changed)
584 error = smb_proc_settime(dentry, &fattr);
585 if (error)
586 goto out;
587 refresh = 1;
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);
598 changed = 0;
599 if (attr->ia_mode & S_IWUSR)
601 if (fattr.attr & aRONLY)
603 fattr.attr &= ~aRONLY;
604 changed = 1;
606 } else {
607 if (!(fattr.attr & aRONLY))
609 fattr.attr |= aRONLY;
610 changed = 1;
613 if (changed)
615 error = smb_proc_setattr(dentry, &fattr);
616 if (error)
617 goto out;
618 refresh = 1;
621 error = 0;
623 out:
624 if (refresh)
625 smb_refresh_inode(dentry);
626 return error;
629 #ifdef DEBUG_SMB_MALLOC
630 int smb_malloced;
631 int smb_current_kmalloced;
632 int smb_current_vmalloced;
633 #endif
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
642 smb_malloced = 0;
643 smb_current_kmalloced = 0;
644 smb_current_vmalloced = 0;
645 #endif
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);
658 #endif
661 EXPORT_NO_SYMBOLS;
663 module_init(init_smb_fs)
664 module_exit(exit_smb_fs)