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