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