4a37c2bbfa3f80bc0ffeebb438de05548ee1e4b9
[safe/jmp/linux-2.6] / 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/time.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/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>
32
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
35
36 #include "smb_debug.h"
37 #include "getopt.h"
38 #include "proto.h"
39
40 /* Always pick a default string */
41 #ifdef CONFIG_SMB_NLS_REMOTE
42 #define SMB_NLS_REMOTE CONFIG_SMB_NLS_REMOTE
43 #else
44 #define SMB_NLS_REMOTE ""
45 #endif
46
47 #define SMB_TTL_DEFAULT 1000
48
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 *);
53
54 static kmem_cache_t *smb_inode_cachep;
55
56 static struct inode *smb_alloc_inode(struct super_block *sb)
57 {
58         struct smb_inode_info *ei;
59         ei = (struct smb_inode_info *)kmem_cache_alloc(smb_inode_cachep, SLAB_KERNEL);
60         if (!ei)
61                 return NULL;
62         return &ei->vfs_inode;
63 }
64
65 static void smb_destroy_inode(struct inode *inode)
66 {
67         kmem_cache_free(smb_inode_cachep, SMB_I(inode));
68 }
69
70 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
71 {
72         struct smb_inode_info *ei = (struct smb_inode_info *) foo;
73         unsigned long flagmask = SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR;
74
75         if ((flags & flagmask) == SLAB_CTOR_CONSTRUCTOR)
76                 inode_init_once(&ei->vfs_inode);
77 }
78  
79 static int init_inodecache(void)
80 {
81         smb_inode_cachep = kmem_cache_create("smb_inode_cache",
82                                              sizeof(struct smb_inode_info),
83                                              0, (SLAB_RECLAIM_ACCOUNT|
84                                                 SLAB_MEM_SPREAD),
85                                              init_once, NULL);
86         if (smb_inode_cachep == NULL)
87                 return -ENOMEM;
88         return 0;
89 }
90
91 static void destroy_inodecache(void)
92 {
93         if (kmem_cache_destroy(smb_inode_cachep))
94                 printk(KERN_INFO "smb_inode_cache: not all structures were freed\n");
95 }
96
97 static int smb_remount(struct super_block *sb, int *flags, char *data)
98 {
99         *flags |= MS_NODIRATIME;
100         return 0;
101 }
102
103 static struct super_operations smb_sops =
104 {
105         .alloc_inode    = smb_alloc_inode,
106         .destroy_inode  = smb_destroy_inode,
107         .drop_inode     = generic_delete_inode,
108         .delete_inode   = smb_delete_inode,
109         .put_super      = smb_put_super,
110         .statfs         = smb_statfs,
111         .show_options   = smb_show_options,
112         .remount_fs     = smb_remount,
113 };
114
115
116 /* We are always generating a new inode here */
117 struct inode *
118 smb_iget(struct super_block *sb, struct smb_fattr *fattr)
119 {
120         struct smb_sb_info *server = SMB_SB(sb);
121         struct inode *result;
122
123         DEBUG1("smb_iget: %p\n", fattr);
124
125         result = new_inode(sb);
126         if (!result)
127                 return result;
128         result->i_ino = fattr->f_ino;
129         SMB_I(result)->open = 0;
130         SMB_I(result)->fileid = 0;
131         SMB_I(result)->access = 0;
132         SMB_I(result)->flags = 0;
133         SMB_I(result)->closed = 0;
134         SMB_I(result)->openers = 0;
135         smb_set_inode_attr(result, fattr);
136         if (S_ISREG(result->i_mode)) {
137                 result->i_op = &smb_file_inode_operations;
138                 result->i_fop = &smb_file_operations;
139                 result->i_data.a_ops = &smb_file_aops;
140         } else if (S_ISDIR(result->i_mode)) {
141                 if (server->opt.capabilities & SMB_CAP_UNIX)
142                         result->i_op = &smb_dir_inode_operations_unix;
143                 else
144                         result->i_op = &smb_dir_inode_operations;
145                 result->i_fop = &smb_dir_operations;
146         } else if (S_ISLNK(result->i_mode)) {
147                 result->i_op = &smb_link_inode_operations;
148         } else {
149                 init_special_inode(result, result->i_mode, fattr->f_rdev);
150         }
151         insert_inode_hash(result);
152         return result;
153 }
154
155 /*
156  * Copy the inode data to a smb_fattr structure.
157  */
158 void
159 smb_get_inode_attr(struct inode *inode, struct smb_fattr *fattr)
160 {
161         memset(fattr, 0, sizeof(struct smb_fattr));
162         fattr->f_mode   = inode->i_mode;
163         fattr->f_nlink  = inode->i_nlink;
164         fattr->f_ino    = inode->i_ino;
165         fattr->f_uid    = inode->i_uid;
166         fattr->f_gid    = inode->i_gid;
167         fattr->f_size   = inode->i_size;
168         fattr->f_mtime  = inode->i_mtime;
169         fattr->f_ctime  = inode->i_ctime;
170         fattr->f_atime  = inode->i_atime;
171         fattr->f_blksize= inode->i_blksize;
172         fattr->f_blocks = inode->i_blocks;
173
174         fattr->attr     = SMB_I(inode)->attr;
175         /*
176          * Keep the attributes in sync with the inode permissions.
177          */
178         if (fattr->f_mode & S_IWUSR)
179                 fattr->attr &= ~aRONLY;
180         else
181                 fattr->attr |= aRONLY;
182 }
183
184 /*
185  * Update the inode, possibly causing it to invalidate its pages if mtime/size
186  * is different from last time.
187  */
188 void
189 smb_set_inode_attr(struct inode *inode, struct smb_fattr *fattr)
190 {
191         struct smb_inode_info *ei = SMB_I(inode);
192
193         /*
194          * A size change should have a different mtime, or same mtime
195          * but different size.
196          */
197         time_t last_time = inode->i_mtime.tv_sec;
198         loff_t last_sz = inode->i_size;
199
200         inode->i_mode   = fattr->f_mode;
201         inode->i_nlink  = fattr->f_nlink;
202         inode->i_uid    = fattr->f_uid;
203         inode->i_gid    = fattr->f_gid;
204         inode->i_ctime  = fattr->f_ctime;
205         inode->i_blksize= fattr->f_blksize;
206         inode->i_blocks = fattr->f_blocks;
207         inode->i_size   = fattr->f_size;
208         inode->i_mtime  = fattr->f_mtime;
209         inode->i_atime  = fattr->f_atime;
210         ei->attr = fattr->attr;
211
212         /*
213          * Update the "last time refreshed" field for revalidation.
214          */
215         ei->oldmtime = jiffies;
216
217         if (inode->i_mtime.tv_sec != last_time || inode->i_size != last_sz) {
218                 VERBOSE("%ld changed, old=%ld, new=%ld, oz=%ld, nz=%ld\n",
219                         inode->i_ino,
220                         (long) last_time, (long) inode->i_mtime.tv_sec,
221                         (long) last_sz, (long) inode->i_size);
222
223                 if (!S_ISDIR(inode->i_mode))
224                         invalidate_remote_inode(inode);
225         }
226 }
227
228 /*
229  * This is called if the connection has gone bad ...
230  * try to kill off all the current inodes.
231  */
232 void
233 smb_invalidate_inodes(struct smb_sb_info *server)
234 {
235         VERBOSE("\n");
236         shrink_dcache_sb(SB_of(server));
237         invalidate_inodes(SB_of(server));
238 }
239
240 /*
241  * This is called to update the inode attributes after
242  * we've made changes to a file or directory.
243  */
244 static int
245 smb_refresh_inode(struct dentry *dentry)
246 {
247         struct inode *inode = dentry->d_inode;
248         int error;
249         struct smb_fattr fattr;
250
251         error = smb_proc_getattr(dentry, &fattr);
252         if (!error) {
253                 smb_renew_times(dentry);
254                 /*
255                  * Check whether the type part of the mode changed,
256                  * and don't update the attributes if it did.
257                  *
258                  * And don't dick with the root inode
259                  */
260                 if (inode->i_ino == 2)
261                         return error;
262                 if (S_ISLNK(inode->i_mode))
263                         return error;   /* VFS will deal with it */
264
265                 if ((inode->i_mode & S_IFMT) == (fattr.f_mode & S_IFMT)) {
266                         smb_set_inode_attr(inode, &fattr);
267                 } else {
268                         /*
269                          * Big trouble! The inode has become a new object,
270                          * so any operations attempted on it are invalid.
271                          *
272                          * To limit damage, mark the inode as bad so that
273                          * subsequent lookup validations will fail.
274                          */
275                         PARANOIA("%s/%s changed mode, %07o to %07o\n",
276                                  DENTRY_PATH(dentry),
277                                  inode->i_mode, fattr.f_mode);
278
279                         fattr.f_mode = inode->i_mode; /* save mode */
280                         make_bad_inode(inode);
281                         inode->i_mode = fattr.f_mode; /* restore mode */
282                         /*
283                          * No need to worry about unhashing the dentry: the
284                          * lookup validation will see that the inode is bad.
285                          * But we do want to invalidate the caches ...
286                          */
287                         if (!S_ISDIR(inode->i_mode))
288                                 invalidate_remote_inode(inode);
289                         else
290                                 smb_invalid_dir_cache(inode);
291                         error = -EIO;
292                 }
293         }
294         return error;
295 }
296
297 /*
298  * This is called when we want to check whether the inode
299  * has changed on the server.  If it has changed, we must
300  * invalidate our local caches.
301  */
302 int
303 smb_revalidate_inode(struct dentry *dentry)
304 {
305         struct smb_sb_info *s = server_from_dentry(dentry);
306         struct inode *inode = dentry->d_inode;
307         int error = 0;
308
309         DEBUG1("smb_revalidate_inode\n");
310         lock_kernel();
311
312         /*
313          * Check whether we've recently refreshed the inode.
314          */
315         if (time_before(jiffies, SMB_I(inode)->oldmtime + SMB_MAX_AGE(s))) {
316                 VERBOSE("up-to-date, ino=%ld, jiffies=%lu, oldtime=%lu\n",
317                         inode->i_ino, jiffies, SMB_I(inode)->oldmtime);
318                 goto out;
319         }
320
321         error = smb_refresh_inode(dentry);
322 out:
323         unlock_kernel();
324         return error;
325 }
326
327 /*
328  * This routine is called when i_nlink == 0 and i_count goes to 0.
329  * All blocking cleanup operations need to go here to avoid races.
330  */
331 static void
332 smb_delete_inode(struct inode *ino)
333 {
334         DEBUG1("ino=%ld\n", ino->i_ino);
335         truncate_inode_pages(&ino->i_data, 0);
336         lock_kernel();
337         if (smb_close(ino))
338                 PARANOIA("could not close inode %ld\n", ino->i_ino);
339         unlock_kernel();
340         clear_inode(ino);
341 }
342
343 static struct option opts[] = {
344         { "version",    0, 'v' },
345         { "win95",      SMB_MOUNT_WIN95, 1 },
346         { "oldattr",    SMB_MOUNT_OLDATTR, 1 },
347         { "dirattr",    SMB_MOUNT_DIRATTR, 1 },
348         { "case",       SMB_MOUNT_CASE, 1 },
349         { "uid",        0, 'u' },
350         { "gid",        0, 'g' },
351         { "file_mode",  0, 'f' },
352         { "dir_mode",   0, 'd' },
353         { "iocharset",  0, 'i' },
354         { "codepage",   0, 'c' },
355         { "ttl",        0, 't' },
356         { NULL,         0, 0}
357 };
358
359 static int
360 parse_options(struct smb_mount_data_kernel *mnt, char *options)
361 {
362         int c;
363         unsigned long flags;
364         unsigned long value;
365         char *optarg;
366         char *optopt;
367
368         flags = 0;
369         while ( (c = smb_getopt("smbfs", &options, opts,
370                                 &optopt, &optarg, &flags, &value)) > 0) {
371
372                 VERBOSE("'%s' -> '%s'\n", optopt, optarg ? optarg : "<none>");
373                 switch (c) {
374                 case 1:
375                         /* got a "flag" option */
376                         break;
377                 case 'v':
378                         if (value != SMB_MOUNT_VERSION) {
379                         printk ("smbfs: Bad mount version %ld, expected %d\n",
380                                 value, SMB_MOUNT_VERSION);
381                                 return 0;
382                         }
383                         mnt->version = value;
384                         break;
385                 case 'u':
386                         mnt->uid = value;
387                         flags |= SMB_MOUNT_UID;
388                         break;
389                 case 'g':
390                         mnt->gid = value;
391                         flags |= SMB_MOUNT_GID;
392                         break;
393                 case 'f':
394                         mnt->file_mode = (value & S_IRWXUGO) | S_IFREG;
395                         flags |= SMB_MOUNT_FMODE;
396                         break;
397                 case 'd':
398                         mnt->dir_mode = (value & S_IRWXUGO) | S_IFDIR;
399                         flags |= SMB_MOUNT_DMODE;
400                         break;
401                 case 'i':
402                         strlcpy(mnt->codepage.local_name, optarg, 
403                                 SMB_NLS_MAXNAMELEN);
404                         break;
405                 case 'c':
406                         strlcpy(mnt->codepage.remote_name, optarg,
407                                 SMB_NLS_MAXNAMELEN);
408                         break;
409                 case 't':
410                         mnt->ttl = value;
411                         break;
412                 default:
413                         printk ("smbfs: Unrecognized mount option %s\n",
414                                 optopt);
415                         return -1;
416                 }
417         }
418         mnt->flags = flags;
419         return c;
420 }
421
422 /*
423  * smb_show_options() is for displaying mount options in /proc/mounts.
424  * It tries to avoid showing settings that were not changed from their
425  * defaults.
426  */
427 static int
428 smb_show_options(struct seq_file *s, struct vfsmount *m)
429 {
430         struct smb_mount_data_kernel *mnt = SMB_SB(m->mnt_sb)->mnt;
431         int i;
432
433         for (i = 0; opts[i].name != NULL; i++)
434                 if (mnt->flags & opts[i].flag)
435                         seq_printf(s, ",%s", opts[i].name);
436
437         if (mnt->flags & SMB_MOUNT_UID)
438                 seq_printf(s, ",uid=%d", mnt->uid);
439         if (mnt->flags & SMB_MOUNT_GID)
440                 seq_printf(s, ",gid=%d", mnt->gid);
441         if (mnt->mounted_uid != 0)
442                 seq_printf(s, ",mounted_uid=%d", mnt->mounted_uid);
443
444         /* 
445          * Defaults for file_mode and dir_mode are unknown to us; they
446          * depend on the current umask of the user doing the mount.
447          */
448         if (mnt->flags & SMB_MOUNT_FMODE)
449                 seq_printf(s, ",file_mode=%04o", mnt->file_mode & S_IRWXUGO);
450         if (mnt->flags & SMB_MOUNT_DMODE)
451                 seq_printf(s, ",dir_mode=%04o", mnt->dir_mode & S_IRWXUGO);
452
453         if (strcmp(mnt->codepage.local_name, CONFIG_NLS_DEFAULT))
454                 seq_printf(s, ",iocharset=%s", mnt->codepage.local_name);
455         if (strcmp(mnt->codepage.remote_name, SMB_NLS_REMOTE))
456                 seq_printf(s, ",codepage=%s", mnt->codepage.remote_name);
457
458         if (mnt->ttl != SMB_TTL_DEFAULT)
459                 seq_printf(s, ",ttl=%d", mnt->ttl);
460
461         return 0;
462 }
463
464 static void
465 smb_unload_nls(struct smb_sb_info *server)
466 {
467         if (server->remote_nls) {
468                 unload_nls(server->remote_nls);
469                 server->remote_nls = NULL;
470         }
471         if (server->local_nls) {
472                 unload_nls(server->local_nls);
473                 server->local_nls = NULL;
474         }
475 }
476
477 static void
478 smb_put_super(struct super_block *sb)
479 {
480         struct smb_sb_info *server = SMB_SB(sb);
481
482         smb_lock_server(server);
483         server->state = CONN_INVALID;
484         smbiod_unregister_server(server);
485
486         smb_close_socket(server);
487
488         if (server->conn_pid)
489                 kill_proc(server->conn_pid, SIGTERM, 1);
490
491         kfree(server->ops);
492         smb_unload_nls(server);
493         sb->s_fs_info = NULL;
494         smb_unlock_server(server);
495         kfree(server);
496 }
497
498 static int smb_fill_super(struct super_block *sb, void *raw_data, int silent)
499 {
500         struct smb_sb_info *server;
501         struct smb_mount_data_kernel *mnt;
502         struct smb_mount_data *oldmnt;
503         struct inode *root_inode;
504         struct smb_fattr root;
505         int ver;
506         void *mem;
507
508         if (!raw_data)
509                 goto out_no_data;
510
511         oldmnt = (struct smb_mount_data *) raw_data;
512         ver = oldmnt->version;
513         if (ver != SMB_MOUNT_OLDVERSION && cpu_to_be32(ver) != SMB_MOUNT_ASCII)
514                 goto out_wrong_data;
515
516         sb->s_flags |= MS_NODIRATIME;
517         sb->s_blocksize = 1024; /* Eh...  Is this correct? */
518         sb->s_blocksize_bits = 10;
519         sb->s_magic = SMB_SUPER_MAGIC;
520         sb->s_op = &smb_sops;
521         sb->s_time_gran = 100;
522
523         server = kzalloc(sizeof(struct smb_sb_info), GFP_KERNEL);
524         if (!server)
525                 goto out_no_server;
526         sb->s_fs_info = server;
527
528         server->super_block = sb;
529         server->mnt = NULL;
530         server->sock_file = NULL;
531         init_waitqueue_head(&server->conn_wq);
532         init_MUTEX(&server->sem);
533         INIT_LIST_HEAD(&server->entry);
534         INIT_LIST_HEAD(&server->xmitq);
535         INIT_LIST_HEAD(&server->recvq);
536         server->conn_error = 0;
537         server->conn_pid = 0;
538         server->state = CONN_INVALID; /* no connection yet */
539         server->generation = 0;
540
541         /* Allocate the global temp buffer and some superblock helper structs */
542         /* FIXME: move these to the smb_sb_info struct */
543         VERBOSE("alloc chunk = %d\n", sizeof(struct smb_ops) +
544                 sizeof(struct smb_mount_data_kernel));
545         mem = kmalloc(sizeof(struct smb_ops) +
546                       sizeof(struct smb_mount_data_kernel), GFP_KERNEL);
547         if (!mem)
548                 goto out_no_mem;
549
550         server->ops = mem;
551         smb_install_null_ops(server->ops);
552         server->mnt = mem + sizeof(struct smb_ops);
553
554         /* Setup NLS stuff */
555         server->remote_nls = NULL;
556         server->local_nls = NULL;
557
558         mnt = server->mnt;
559
560         memset(mnt, 0, sizeof(struct smb_mount_data_kernel));
561         strlcpy(mnt->codepage.local_name, CONFIG_NLS_DEFAULT,
562                 SMB_NLS_MAXNAMELEN);
563         strlcpy(mnt->codepage.remote_name, SMB_NLS_REMOTE,
564                 SMB_NLS_MAXNAMELEN);
565
566         mnt->ttl = SMB_TTL_DEFAULT;
567         if (ver == SMB_MOUNT_OLDVERSION) {
568                 mnt->version = oldmnt->version;
569
570                 SET_UID(mnt->uid, oldmnt->uid);
571                 SET_GID(mnt->gid, oldmnt->gid);
572
573                 mnt->file_mode = (oldmnt->file_mode & S_IRWXUGO) | S_IFREG;
574                 mnt->dir_mode = (oldmnt->dir_mode & S_IRWXUGO) | S_IFDIR;
575
576                 mnt->flags = (oldmnt->file_mode >> 9) | SMB_MOUNT_UID |
577                         SMB_MOUNT_GID | SMB_MOUNT_FMODE | SMB_MOUNT_DMODE;
578         } else {
579                 mnt->file_mode = S_IRWXU | S_IRGRP | S_IXGRP |
580                                 S_IROTH | S_IXOTH | S_IFREG;
581                 mnt->dir_mode = S_IRWXU | S_IRGRP | S_IXGRP |
582                                 S_IROTH | S_IXOTH | S_IFDIR;
583                 if (parse_options(mnt, raw_data))
584                         goto out_bad_option;
585         }
586         mnt->mounted_uid = current->uid;
587         smb_setcodepage(server, &mnt->codepage);
588
589         /*
590          * Display the enabled options
591          * Note: smb_proc_getattr uses these in 2.4 (but was changed in 2.2)
592          */
593         if (mnt->flags & SMB_MOUNT_OLDATTR)
594                 printk("SMBFS: Using core getattr (Win 95 speedup)\n");
595         else if (mnt->flags & SMB_MOUNT_DIRATTR)
596                 printk("SMBFS: Using dir ff getattr\n");
597
598         if (smbiod_register_server(server) < 0) {
599                 printk(KERN_ERR "smbfs: failed to start smbiod\n");
600                 goto out_no_smbiod;
601         }
602
603         /*
604          * Keep the super block locked while we get the root inode.
605          */
606         smb_init_root_dirent(server, &root, sb);
607         root_inode = smb_iget(sb, &root);
608         if (!root_inode)
609                 goto out_no_root;
610
611         sb->s_root = d_alloc_root(root_inode);
612         if (!sb->s_root)
613                 goto out_no_root;
614
615         smb_new_dentry(sb->s_root);
616
617         return 0;
618
619 out_no_root:
620         iput(root_inode);
621 out_no_smbiod:
622         smb_unload_nls(server);
623 out_bad_option:
624         kfree(mem);
625 out_no_mem:
626         if (!server->mnt)
627                 printk(KERN_ERR "smb_fill_super: allocation failure\n");
628         sb->s_fs_info = NULL;
629         kfree(server);
630         goto out_fail;
631 out_wrong_data:
632         printk(KERN_ERR "smbfs: mount_data version %d is not supported\n", ver);
633         goto out_fail;
634 out_no_data:
635         printk(KERN_ERR "smb_fill_super: missing data argument\n");
636 out_fail:
637         return -EINVAL;
638 out_no_server:
639         printk(KERN_ERR "smb_fill_super: cannot allocate struct smb_sb_info\n");
640         return -ENOMEM;
641 }
642
643 static int
644 smb_statfs(struct super_block *sb, struct kstatfs *buf)
645 {
646         int result;
647         
648         lock_kernel();
649
650         result = smb_proc_dskattr(sb, buf);
651
652         unlock_kernel();
653
654         buf->f_type = SMB_SUPER_MAGIC;
655         buf->f_namelen = SMB_MAXPATHLEN;
656         return result;
657 }
658
659 int smb_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
660 {
661         int err = smb_revalidate_inode(dentry);
662         if (!err)
663                 generic_fillattr(dentry->d_inode, stat);
664         return err;
665 }
666
667 int
668 smb_notify_change(struct dentry *dentry, struct iattr *attr)
669 {
670         struct inode *inode = dentry->d_inode;
671         struct smb_sb_info *server = server_from_dentry(dentry);
672         unsigned int mask = (S_IFREG | S_IFDIR | S_IRWXUGO);
673         int error, changed, refresh = 0;
674         struct smb_fattr fattr;
675
676         lock_kernel();
677
678         error = smb_revalidate_inode(dentry);
679         if (error)
680                 goto out;
681
682         if ((error = inode_change_ok(inode, attr)) < 0)
683                 goto out;
684
685         error = -EPERM;
686         if ((attr->ia_valid & ATTR_UID) && (attr->ia_uid != server->mnt->uid))
687                 goto out;
688
689         if ((attr->ia_valid & ATTR_GID) && (attr->ia_uid != server->mnt->gid))
690                 goto out;
691
692         if ((attr->ia_valid & ATTR_MODE) && (attr->ia_mode & ~mask))
693                 goto out;
694
695         if ((attr->ia_valid & ATTR_SIZE) != 0) {
696                 VERBOSE("changing %s/%s, old size=%ld, new size=%ld\n",
697                         DENTRY_PATH(dentry),
698                         (long) inode->i_size, (long) attr->ia_size);
699
700                 filemap_write_and_wait(inode->i_mapping);
701
702                 error = smb_open(dentry, O_WRONLY);
703                 if (error)
704                         goto out;
705                 error = server->ops->truncate(inode, attr->ia_size);
706                 if (error)
707                         goto out;
708                 error = vmtruncate(inode, attr->ia_size);
709                 if (error)
710                         goto out;
711                 refresh = 1;
712         }
713
714         if (server->opt.capabilities & SMB_CAP_UNIX) {
715                 /* For now we don't want to set the size with setattr_unix */
716                 attr->ia_valid &= ~ATTR_SIZE;
717                 /* FIXME: only call if we actually want to set something? */
718                 error = smb_proc_setattr_unix(dentry, attr, 0, 0);
719                 if (!error)
720                         refresh = 1;
721
722                 goto out;
723         }
724
725         /*
726          * Initialize the fattr and check for changed fields.
727          * Note: CTIME under SMB is creation time rather than
728          * change time, so we don't attempt to change it.
729          */
730         smb_get_inode_attr(inode, &fattr);
731
732         changed = 0;
733         if ((attr->ia_valid & ATTR_MTIME) != 0) {
734                 fattr.f_mtime = attr->ia_mtime;
735                 changed = 1;
736         }
737         if ((attr->ia_valid & ATTR_ATIME) != 0) {
738                 fattr.f_atime = attr->ia_atime;
739                 /* Earlier protocols don't have an access time */
740                 if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2)
741                         changed = 1;
742         }
743         if (changed) {
744                 error = smb_proc_settime(dentry, &fattr);
745                 if (error)
746                         goto out;
747                 refresh = 1;
748         }
749
750         /*
751          * Check for mode changes ... we're extremely limited in
752          * what can be set for SMB servers: just the read-only bit.
753          */
754         if ((attr->ia_valid & ATTR_MODE) != 0) {
755                 VERBOSE("%s/%s mode change, old=%x, new=%x\n",
756                         DENTRY_PATH(dentry), fattr.f_mode, attr->ia_mode);
757                 changed = 0;
758                 if (attr->ia_mode & S_IWUSR) {
759                         if (fattr.attr & aRONLY) {
760                                 fattr.attr &= ~aRONLY;
761                                 changed = 1;
762                         }
763                 } else {
764                         if (!(fattr.attr & aRONLY)) {
765                                 fattr.attr |= aRONLY;
766                                 changed = 1;
767                         }
768                 }
769                 if (changed) {
770                         error = smb_proc_setattr(dentry, &fattr);
771                         if (error)
772                                 goto out;
773                         refresh = 1;
774                 }
775         }
776         error = 0;
777
778 out:
779         if (refresh)
780                 smb_refresh_inode(dentry);
781         unlock_kernel();
782         return error;
783 }
784
785 static int smb_get_sb(struct file_system_type *fs_type,
786         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
787 {
788         return get_sb_nodev(fs_type, flags, data, smb_fill_super, mnt);
789 }
790
791 static struct file_system_type smb_fs_type = {
792         .owner          = THIS_MODULE,
793         .name           = "smbfs",
794         .get_sb         = smb_get_sb,
795         .kill_sb        = kill_anon_super,
796         .fs_flags       = FS_BINARY_MOUNTDATA,
797 };
798
799 static int __init init_smb_fs(void)
800 {
801         int err;
802         DEBUG1("registering ...\n");
803
804         err = init_inodecache();
805         if (err)
806                 goto out_inode;
807         err = smb_init_request_cache();
808         if (err)
809                 goto out_request;
810         err = register_filesystem(&smb_fs_type);
811         if (err)
812                 goto out;
813         return 0;
814 out:
815         smb_destroy_request_cache();
816 out_request:
817         destroy_inodecache();
818 out_inode:
819         return err;
820 }
821
822 static void __exit exit_smb_fs(void)
823 {
824         DEBUG1("unregistering ...\n");
825         unregister_filesystem(&smb_fs_type);
826         smb_destroy_request_cache();
827         destroy_inodecache();
828 }
829
830 module_init(init_smb_fs)
831 module_exit(exit_smb_fs)
832 MODULE_LICENSE("GPL");