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