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