[SCSI] aacraid: remove compiler warning
[sfrench/cifs-2.6.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/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                                              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         if (kmem_cache_destroy(smb_inode_cachep))
93                 printk(KERN_INFO "smb_inode_cache: not all structures were freed\n");
94 }
95
96 static int smb_remount(struct super_block *sb, int *flags, char *data)
97 {
98         *flags |= MS_NODIRATIME;
99         return 0;
100 }
101
102 static struct super_operations smb_sops =
103 {
104         .alloc_inode    = smb_alloc_inode,
105         .destroy_inode  = smb_destroy_inode,
106         .drop_inode     = generic_delete_inode,
107         .delete_inode   = smb_delete_inode,
108         .put_super      = smb_put_super,
109         .statfs         = smb_statfs,
110         .show_options   = smb_show_options,
111         .remount_fs     = smb_remount,
112 };
113
114
115 /* We are always generating a new inode here */
116 struct inode *
117 smb_iget(struct super_block *sb, struct smb_fattr *fattr)
118 {
119         struct smb_sb_info *server = SMB_SB(sb);
120         struct inode *result;
121
122         DEBUG1("smb_iget: %p\n", fattr);
123
124         result = new_inode(sb);
125         if (!result)
126                 return result;
127         result->i_ino = fattr->f_ino;
128         SMB_I(result)->open = 0;
129         SMB_I(result)->fileid = 0;
130         SMB_I(result)->access = 0;
131         SMB_I(result)->flags = 0;
132         SMB_I(result)->closed = 0;
133         SMB_I(result)->openers = 0;
134         smb_set_inode_attr(result, fattr);
135         if (S_ISREG(result->i_mode)) {
136                 result->i_op = &smb_file_inode_operations;
137                 result->i_fop = &smb_file_operations;
138                 result->i_data.a_ops = &smb_file_aops;
139         } else if (S_ISDIR(result->i_mode)) {
140                 if (server->opt.capabilities & SMB_CAP_UNIX)
141                         result->i_op = &smb_dir_inode_operations_unix;
142                 else
143                         result->i_op = &smb_dir_inode_operations;
144                 result->i_fop = &smb_dir_operations;
145         } else if (S_ISLNK(result->i_mode)) {
146                 result->i_op = &smb_link_inode_operations;
147         } else {
148                 init_special_inode(result, result->i_mode, fattr->f_rdev);
149         }
150         insert_inode_hash(result);
151         return result;
152 }
153
154 /*
155  * Copy the inode data to a smb_fattr structure.
156  */
157 void
158 smb_get_inode_attr(struct inode *inode, struct smb_fattr *fattr)
159 {
160         memset(fattr, 0, sizeof(struct smb_fattr));
161         fattr->f_mode   = inode->i_mode;
162         fattr->f_nlink  = inode->i_nlink;
163         fattr->f_ino    = inode->i_ino;
164         fattr->f_uid    = inode->i_uid;
165         fattr->f_gid    = inode->i_gid;
166         fattr->f_size   = inode->i_size;
167         fattr->f_mtime  = inode->i_mtime;
168         fattr->f_ctime  = inode->i_ctime;
169         fattr->f_atime  = inode->i_atime;
170         fattr->f_blksize= inode->i_blksize;
171         fattr->f_blocks = inode->i_blocks;
172
173         fattr->attr     = SMB_I(inode)->attr;
174         /*
175          * Keep the attributes in sync with the inode permissions.
176          */
177         if (fattr->f_mode & S_IWUSR)
178                 fattr->attr &= ~aRONLY;
179         else
180                 fattr->attr |= aRONLY;
181 }
182
183 /*
184  * Update the inode, possibly causing it to invalidate its pages if mtime/size
185  * is different from last time.
186  */
187 void
188 smb_set_inode_attr(struct inode *inode, struct smb_fattr *fattr)
189 {
190         struct smb_inode_info *ei = SMB_I(inode);
191
192         /*
193          * A size change should have a different mtime, or same mtime
194          * but different size.
195          */
196         time_t last_time = inode->i_mtime.tv_sec;
197         loff_t last_sz = inode->i_size;
198
199         inode->i_mode   = fattr->f_mode;
200         inode->i_nlink  = fattr->f_nlink;
201         inode->i_uid    = fattr->f_uid;
202         inode->i_gid    = fattr->f_gid;
203         inode->i_ctime  = fattr->f_ctime;
204         inode->i_blksize= fattr->f_blksize;
205         inode->i_blocks = fattr->f_blocks;
206         inode->i_size   = fattr->f_size;
207         inode->i_mtime  = fattr->f_mtime;
208         inode->i_atime  = fattr->f_atime;
209         ei->attr = fattr->attr;
210
211         /*
212          * Update the "last time refreshed" field for revalidation.
213          */
214         ei->oldmtime = jiffies;
215
216         if (inode->i_mtime.tv_sec != last_time || inode->i_size != last_sz) {
217                 VERBOSE("%ld changed, old=%ld, new=%ld, oz=%ld, nz=%ld\n",
218                         inode->i_ino,
219                         (long) last_time, (long) inode->i_mtime,
220                         (long) last_sz, (long) inode->i_size);
221
222                 if (!S_ISDIR(inode->i_mode))
223                         invalidate_remote_inode(inode);
224         }
225 }
226
227 /*
228  * This is called if the connection has gone bad ...
229  * try to kill off all the current inodes.
230  */
231 void
232 smb_invalidate_inodes(struct smb_sb_info *server)
233 {
234         VERBOSE("\n");
235         shrink_dcache_sb(SB_of(server));
236         invalidate_inodes(SB_of(server));
237 }
238
239 /*
240  * This is called to update the inode attributes after
241  * we've made changes to a file or directory.
242  */
243 static int
244 smb_refresh_inode(struct dentry *dentry)
245 {
246         struct inode *inode = dentry->d_inode;
247         int error;
248         struct smb_fattr fattr;
249
250         error = smb_proc_getattr(dentry, &fattr);
251         if (!error) {
252                 smb_renew_times(dentry);
253                 /*
254                  * Check whether the type part of the mode changed,
255                  * and don't update the attributes if it did.
256                  *
257                  * And don't dick with the root inode
258                  */
259                 if (inode->i_ino == 2)
260                         return error;
261                 if (S_ISLNK(inode->i_mode))
262                         return error;   /* VFS will deal with it */
263
264                 if ((inode->i_mode & S_IFMT) == (fattr.f_mode & S_IFMT)) {
265                         smb_set_inode_attr(inode, &fattr);
266                 } else {
267                         /*
268                          * Big trouble! The inode has become a new object,
269                          * so any operations attempted on it are invalid.
270                          *
271                          * To limit damage, mark the inode as bad so that
272                          * subsequent lookup validations will fail.
273                          */
274                         PARANOIA("%s/%s changed mode, %07o to %07o\n",
275                                  DENTRY_PATH(dentry),
276                                  inode->i_mode, fattr.f_mode);
277
278                         fattr.f_mode = inode->i_mode; /* save mode */
279                         make_bad_inode(inode);
280                         inode->i_mode = fattr.f_mode; /* restore mode */
281                         /*
282                          * No need to worry about unhashing the dentry: the
283                          * lookup validation will see that the inode is bad.
284                          * But we do want to invalidate the caches ...
285                          */
286                         if (!S_ISDIR(inode->i_mode))
287                                 invalidate_remote_inode(inode);
288                         else
289                                 smb_invalid_dir_cache(inode);
290                         error = -EIO;
291                 }
292         }
293         return error;
294 }
295
296 /*
297  * This is called when we want to check whether the inode
298  * has changed on the server.  If it has changed, we must
299  * invalidate our local caches.
300  */
301 int
302 smb_revalidate_inode(struct dentry *dentry)
303 {
304         struct smb_sb_info *s = server_from_dentry(dentry);
305         struct inode *inode = dentry->d_inode;
306         int error = 0;
307
308         DEBUG1("smb_revalidate_inode\n");
309         lock_kernel();
310
311         /*
312          * Check whether we've recently refreshed the inode.
313          */
314         if (time_before(jiffies, SMB_I(inode)->oldmtime + SMB_MAX_AGE(s))) {
315                 VERBOSE("up-to-date, ino=%ld, jiffies=%lu, oldtime=%lu\n",
316                         inode->i_ino, jiffies, SMB_I(inode)->oldmtime);
317                 goto out;
318         }
319
320         error = smb_refresh_inode(dentry);
321 out:
322         unlock_kernel();
323         return error;
324 }
325
326 /*
327  * This routine is called when i_nlink == 0 and i_count goes to 0.
328  * All blocking cleanup operations need to go here to avoid races.
329  */
330 static void
331 smb_delete_inode(struct inode *ino)
332 {
333         DEBUG1("ino=%ld\n", ino->i_ino);
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         smb_kfree(server->ops);
490         smb_unload_nls(server);
491         sb->s_fs_info = NULL;
492         smb_unlock_server(server);
493         smb_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 = smb_kmalloc(sizeof(struct smb_sb_info), GFP_KERNEL);
522         if (!server)
523                 goto out_no_server;
524         sb->s_fs_info = server;
525         memset(server, 0, sizeof(struct smb_sb_info));
526
527         server->super_block = sb;
528         server->mnt = NULL;
529         server->sock_file = NULL;
530         init_waitqueue_head(&server->conn_wq);
531         init_MUTEX(&server->sem);
532         INIT_LIST_HEAD(&server->entry);
533         INIT_LIST_HEAD(&server->xmitq);
534         INIT_LIST_HEAD(&server->recvq);
535         server->conn_error = 0;
536         server->conn_pid = 0;
537         server->state = CONN_INVALID; /* no connection yet */
538         server->generation = 0;
539
540         /* Allocate the global temp buffer and some superblock helper structs */
541         /* FIXME: move these to the smb_sb_info struct */
542         VERBOSE("alloc chunk = %d\n", sizeof(struct smb_ops) +
543                 sizeof(struct smb_mount_data_kernel));
544         mem = smb_kmalloc(sizeof(struct smb_ops) +
545                           sizeof(struct smb_mount_data_kernel), GFP_KERNEL);
546         if (!mem)
547                 goto out_no_mem;
548
549         server->ops = mem;
550         smb_install_null_ops(server->ops);
551         server->mnt = mem + sizeof(struct smb_ops);
552
553         /* Setup NLS stuff */
554         server->remote_nls = NULL;
555         server->local_nls = NULL;
556
557         mnt = server->mnt;
558
559         memset(mnt, 0, sizeof(struct smb_mount_data_kernel));
560         strlcpy(mnt->codepage.local_name, CONFIG_NLS_DEFAULT,
561                 SMB_NLS_MAXNAMELEN);
562         strlcpy(mnt->codepage.remote_name, SMB_NLS_REMOTE,
563                 SMB_NLS_MAXNAMELEN);
564
565         mnt->ttl = SMB_TTL_DEFAULT;
566         if (ver == SMB_MOUNT_OLDVERSION) {
567                 mnt->version = oldmnt->version;
568
569                 SET_UID(mnt->uid, oldmnt->uid);
570                 SET_GID(mnt->gid, oldmnt->gid);
571
572                 mnt->file_mode = (oldmnt->file_mode & S_IRWXUGO) | S_IFREG;
573                 mnt->dir_mode = (oldmnt->dir_mode & S_IRWXUGO) | S_IFDIR;
574
575                 mnt->flags = (oldmnt->file_mode >> 9) | SMB_MOUNT_UID |
576                         SMB_MOUNT_GID | SMB_MOUNT_FMODE | SMB_MOUNT_DMODE;
577         } else {
578                 mnt->file_mode = S_IRWXU | S_IRGRP | S_IXGRP |
579                                 S_IROTH | S_IXOTH | S_IFREG;
580                 mnt->dir_mode = S_IRWXU | S_IRGRP | S_IXGRP |
581                                 S_IROTH | S_IXOTH | S_IFDIR;
582                 if (parse_options(mnt, raw_data))
583                         goto out_bad_option;
584         }
585         mnt->mounted_uid = current->uid;
586         smb_setcodepage(server, &mnt->codepage);
587
588         /*
589          * Display the enabled options
590          * Note: smb_proc_getattr uses these in 2.4 (but was changed in 2.2)
591          */
592         if (mnt->flags & SMB_MOUNT_OLDATTR)
593                 printk("SMBFS: Using core getattr (Win 95 speedup)\n");
594         else if (mnt->flags & SMB_MOUNT_DIRATTR)
595                 printk("SMBFS: Using dir ff getattr\n");
596
597         if (smbiod_register_server(server) < 0) {
598                 printk(KERN_ERR "smbfs: failed to start smbiod\n");
599                 goto out_no_smbiod;
600         }
601
602         /*
603          * Keep the super block locked while we get the root inode.
604          */
605         smb_init_root_dirent(server, &root, sb);
606         root_inode = smb_iget(sb, &root);
607         if (!root_inode)
608                 goto out_no_root;
609
610         sb->s_root = d_alloc_root(root_inode);
611         if (!sb->s_root)
612                 goto out_no_root;
613
614         smb_new_dentry(sb->s_root);
615
616         return 0;
617
618 out_no_root:
619         iput(root_inode);
620 out_no_smbiod:
621         smb_unload_nls(server);
622 out_bad_option:
623         smb_kfree(mem);
624 out_no_mem:
625         if (!server->mnt)
626                 printk(KERN_ERR "smb_fill_super: allocation failure\n");
627         sb->s_fs_info = NULL;
628         smb_kfree(server);
629         goto out_fail;
630 out_wrong_data:
631         printk(KERN_ERR "smbfs: mount_data version %d is not supported\n", ver);
632         goto out_fail;
633 out_no_data:
634         printk(KERN_ERR "smb_fill_super: missing data argument\n");
635 out_fail:
636         return -EINVAL;
637 out_no_server:
638         printk(KERN_ERR "smb_fill_super: cannot allocate struct smb_sb_info\n");
639         return -ENOMEM;
640 }
641
642 static int
643 smb_statfs(struct super_block *sb, struct kstatfs *buf)
644 {
645         int result;
646         
647         lock_kernel();
648
649         result = smb_proc_dskattr(sb, buf);
650
651         unlock_kernel();
652
653         buf->f_type = SMB_SUPER_MAGIC;
654         buf->f_namelen = SMB_MAXPATHLEN;
655         return result;
656 }
657
658 int smb_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
659 {
660         int err = smb_revalidate_inode(dentry);
661         if (!err)
662                 generic_fillattr(dentry->d_inode, stat);
663         return err;
664 }
665
666 int
667 smb_notify_change(struct dentry *dentry, struct iattr *attr)
668 {
669         struct inode *inode = dentry->d_inode;
670         struct smb_sb_info *server = server_from_dentry(dentry);
671         unsigned int mask = (S_IFREG | S_IFDIR | S_IRWXUGO);
672         int error, changed, refresh = 0;
673         struct smb_fattr fattr;
674
675         lock_kernel();
676
677         error = smb_revalidate_inode(dentry);
678         if (error)
679                 goto out;
680
681         if ((error = inode_change_ok(inode, attr)) < 0)
682                 goto out;
683
684         error = -EPERM;
685         if ((attr->ia_valid & ATTR_UID) && (attr->ia_uid != server->mnt->uid))
686                 goto out;
687
688         if ((attr->ia_valid & ATTR_GID) && (attr->ia_uid != server->mnt->gid))
689                 goto out;
690
691         if ((attr->ia_valid & ATTR_MODE) && (attr->ia_mode & ~mask))
692                 goto out;
693
694         if ((attr->ia_valid & ATTR_SIZE) != 0) {
695                 VERBOSE("changing %s/%s, old size=%ld, new size=%ld\n",
696                         DENTRY_PATH(dentry),
697                         (long) inode->i_size, (long) attr->ia_size);
698
699                 filemap_fdatawrite(inode->i_mapping);
700                 filemap_fdatawait(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 #ifdef DEBUG_SMB_MALLOC
786 int smb_malloced;
787 int smb_current_kmalloced;
788 int smb_current_vmalloced;
789 #endif
790
791 static struct super_block *smb_get_sb(struct file_system_type *fs_type,
792         int flags, const char *dev_name, void *data)
793 {
794         return get_sb_nodev(fs_type, flags, data, smb_fill_super);
795 }
796
797 static struct file_system_type smb_fs_type = {
798         .owner          = THIS_MODULE,
799         .name           = "smbfs",
800         .get_sb         = smb_get_sb,
801         .kill_sb        = kill_anon_super,
802         .fs_flags       = FS_BINARY_MOUNTDATA,
803 };
804
805 static int __init init_smb_fs(void)
806 {
807         int err;
808         DEBUG1("registering ...\n");
809
810 #ifdef DEBUG_SMB_MALLOC
811         smb_malloced = 0;
812         smb_current_kmalloced = 0;
813         smb_current_vmalloced = 0;
814 #endif
815
816         err = init_inodecache();
817         if (err)
818                 goto out_inode;
819         err = smb_init_request_cache();
820         if (err)
821                 goto out_request;
822         err = register_filesystem(&smb_fs_type);
823         if (err)
824                 goto out;
825         return 0;
826 out:
827         smb_destroy_request_cache();
828 out_request:
829         destroy_inodecache();
830 out_inode:
831         return err;
832 }
833
834 static void __exit exit_smb_fs(void)
835 {
836         DEBUG1("unregistering ...\n");
837         unregister_filesystem(&smb_fs_type);
838         smb_destroy_request_cache();
839         destroy_inodecache();
840 #ifdef DEBUG_SMB_MALLOC
841         printk(KERN_DEBUG "smb_malloced: %d\n", smb_malloced);
842         printk(KERN_DEBUG "smb_current_kmalloced: %d\n",smb_current_kmalloced);
843         printk(KERN_DEBUG "smb_current_vmalloced: %d\n",smb_current_vmalloced);
844 #endif
845 }
846
847 module_init(init_smb_fs)
848 module_exit(exit_smb_fs)
849 MODULE_LICENSE("GPL");