Linux 2.2.0
[davej-history.git] / fs / smbfs / inode.c
blob1a278911ab55e2a65597a2dc151475d3473ce7bd
1 /*
2 * inode.c
4 * Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
5 * Copyright (C) 1997 by Volker Lendecke
7 */
9 #include <linux/config.h>
10 #include <linux/module.h>
12 #include <linux/sched.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/locks.h>
19 #include <linux/malloc.h>
20 #include <linux/init.h>
21 #include <linux/file.h>
22 #include <linux/dcache.h>
24 #include <linux/smb_fs.h>
25 #include <linux/smbno.h>
26 #include <linux/smb_mount.h>
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
31 #define SMBFS_PARANOIA 1
32 /* #define SMBFS_DEBUG_VERBOSE 1 */
34 static void smb_read_inode(struct inode *);
35 static void smb_put_inode(struct inode *);
36 static void smb_delete_inode(struct inode *);
37 static void smb_put_super(struct super_block *);
38 static int smb_statfs(struct super_block *, struct statfs *, int);
40 static struct super_operations smb_sops =
42 smb_read_inode, /* read inode */
43 NULL, /* write inode */
44 smb_put_inode, /* put inode */
45 smb_delete_inode, /* delete inode */
46 smb_notify_change, /* notify change */
47 smb_put_super, /* put superblock */
48 NULL, /* write superblock */
49 smb_statfs, /* stat filesystem */
50 NULL /* remount filesystem */
53 /* FIXME: Look at all inodes whether so that we do not get duplicate
54 * inode numbers. */
56 unsigned long
57 smb_invent_inos(unsigned long n)
59 static unsigned long ino = 2;
61 if (ino + 2*n < ino)
63 /* wrap around */
64 ino = 2;
66 ino += n;
67 return ino;
70 static struct smb_fattr *read_fattr = NULL;
71 static struct semaphore read_semaphore = MUTEX;
73 struct inode *
74 smb_iget(struct super_block *sb, struct smb_fattr *fattr)
76 struct inode *result;
78 pr_debug("smb_iget: %p\n", fattr);
80 down(&read_semaphore);
81 read_fattr = fattr;
82 result = iget(sb, fattr->f_ino);
83 read_fattr = NULL;
84 up(&read_semaphore);
85 return result;
89 * Copy the inode data to a smb_fattr structure.
91 void
92 smb_get_inode_attr(struct inode *inode, struct smb_fattr *fattr)
94 memset(fattr, 0, sizeof(struct smb_fattr));
95 fattr->f_mode = inode->i_mode;
96 fattr->f_nlink = inode->i_nlink;
97 fattr->f_ino = inode->i_ino;
98 fattr->f_uid = inode->i_uid;
99 fattr->f_gid = inode->i_gid;
100 fattr->f_rdev = inode->i_rdev;
101 fattr->f_size = inode->i_size;
102 fattr->f_mtime = inode->i_mtime;
103 fattr->f_ctime = inode->i_ctime;
104 fattr->f_atime = inode->i_atime;
105 fattr->f_blksize= inode->i_blksize;
106 fattr->f_blocks = inode->i_blocks;
108 fattr->attr = inode->u.smbfs_i.attr;
110 * Keep the attributes in sync with the inode permissions.
112 if (fattr->f_mode & S_IWUSR)
113 fattr->attr &= ~aRONLY;
114 else
115 fattr->attr |= aRONLY;
118 static void
119 smb_set_inode_attr(struct inode *inode, struct smb_fattr *fattr)
121 inode->i_mode = fattr->f_mode;
122 inode->i_nlink = fattr->f_nlink;
123 inode->i_uid = fattr->f_uid;
124 inode->i_gid = fattr->f_gid;
125 inode->i_rdev = fattr->f_rdev;
126 inode->i_ctime = fattr->f_ctime;
127 inode->i_blksize= fattr->f_blksize;
128 inode->i_blocks = fattr->f_blocks;
130 * Don't change the size and mtime/atime fields
131 * if we're writing to the file.
133 if (!(inode->u.smbfs_i.cache_valid & SMB_F_LOCALWRITE))
135 inode->i_size = fattr->f_size;
136 inode->i_mtime = fattr->f_mtime;
137 inode->i_atime = fattr->f_atime;
140 inode->u.smbfs_i.attr = fattr->attr;
142 * Update the "last time refreshed" field for revalidation.
144 inode->u.smbfs_i.oldmtime = jiffies;
147 static void
148 smb_read_inode(struct inode *inode)
150 pr_debug("smb_iget: %p\n", read_fattr);
152 if (!read_fattr || inode->i_ino != read_fattr->f_ino)
154 printk("smb_read_inode called from invalid point\n");
155 return;
158 inode->i_dev = inode->i_sb->s_dev;
159 memset(&(inode->u.smbfs_i), 0, sizeof(inode->u.smbfs_i));
160 smb_set_inode_attr(inode, read_fattr);
162 if (S_ISREG(inode->i_mode))
163 inode->i_op = &smb_file_inode_operations;
164 else if (S_ISDIR(inode->i_mode))
165 inode->i_op = &smb_dir_inode_operations;
166 else
167 inode->i_op = NULL;
171 * This is called if the connection has gone bad ...
172 * try to kill off all the current inodes.
174 void
175 smb_invalidate_inodes(struct smb_sb_info *server)
177 #ifdef SMBFS_DEBUG_VERBOSE
178 printk("smb_invalidate_inodes\n");
179 #endif
180 shrink_dcache_sb(SB_of(server));
181 invalidate_inodes(SB_of(server));
185 * This is called to update the inode attributes after
186 * we've made changes to a file or directory.
188 static int
189 smb_refresh_inode(struct dentry *dentry)
191 struct inode *inode = dentry->d_inode;
192 int error;
193 struct smb_fattr fattr;
195 error = smb_proc_getattr(dentry, &fattr);
196 if (!error)
198 smb_renew_times(dentry);
200 * Check whether the type part of the mode changed,
201 * and don't update the attributes if it did.
203 if ((inode->i_mode & S_IFMT) == (fattr.f_mode & S_IFMT))
204 smb_set_inode_attr(inode, &fattr);
205 else
208 * Big trouble! The inode has become a new object,
209 * so any operations attempted on it are invalid.
211 * To limit damage, mark the inode as bad so that
212 * subsequent lookup validations will fail.
214 #ifdef SMBFS_PARANOIA
215 printk("smb_refresh_inode: %s/%s changed mode, %07o to %07o\n",
216 dentry->d_parent->d_name.name, dentry->d_name.name,
217 inode->i_mode, fattr.f_mode);
218 #endif
219 fattr.f_mode = inode->i_mode; /* save mode */
220 make_bad_inode(inode);
221 inode->i_mode = fattr.f_mode; /* restore mode */
223 * No need to worry about unhashing the dentry: the
224 * lookup validation will see that the inode is bad.
225 * But we do want to invalidate the caches ...
227 if (!S_ISDIR(inode->i_mode))
228 invalidate_inode_pages(inode);
229 else
230 smb_invalid_dir_cache(inode);
231 error = -EIO;
234 return error;
238 * This is called when we want to check whether the inode
239 * has changed on the server. If it has changed, we must
240 * invalidate our local caches.
243 smb_revalidate_inode(struct dentry *dentry)
245 struct inode *inode = dentry->d_inode;
246 time_t last_time;
247 int error = 0;
249 pr_debug("smb_revalidate_inode\n");
251 * If this is a file opened with write permissions,
252 * the inode will be up-to-date.
254 if (S_ISREG(inode->i_mode) && smb_is_open(inode))
256 if (inode->u.smbfs_i.access != SMB_O_RDONLY)
257 goto out;
261 * Check whether we've recently refreshed the inode.
263 if (time_before(jiffies, inode->u.smbfs_i.oldmtime + HZ/10))
265 #ifdef SMBFS_DEBUG_VERBOSE
266 printk("smb_revalidate_inode: up-to-date, jiffies=%lu, oldtime=%lu\n",
267 jiffies, inode->u.smbfs_i.oldmtime);
268 #endif
269 goto out;
273 * Save the last modified time, then refresh the inode.
274 * (Note: a size change should have a different mtime.)
276 last_time = inode->i_mtime;
277 error = smb_refresh_inode(dentry);
278 if (error || inode->i_mtime != last_time)
280 #ifdef SMBFS_DEBUG_VERBOSE
281 printk("smb_revalidate: %s/%s changed, old=%ld, new=%ld\n",
282 dentry->d_parent->d_name.name, dentry->d_name.name,
283 (long) last_time, (long) inode->i_mtime);
284 #endif
285 if (!S_ISDIR(inode->i_mode))
286 invalidate_inode_pages(inode);
287 else
288 smb_invalid_dir_cache(inode);
290 out:
291 return error;
295 * This routine is called for every iput(). We clear i_nlink
296 * on the last use to force a call to delete_inode.
298 static void
299 smb_put_inode(struct inode *ino)
301 pr_debug("smb_put_inode: count = %d\n", ino->i_count);
302 if (ino->i_count == 1)
303 ino->i_nlink = 0;
307 * This routine is called when i_nlink == 0 and i_count goes to 0.
308 * All blocking cleanup operations need to go here to avoid races.
310 static void
311 smb_delete_inode(struct inode *ino)
313 pr_debug("smb_delete_inode\n");
314 if (smb_close(ino))
315 printk("smb_delete_inode: could not close inode %ld\n",
316 ino->i_ino);
317 clear_inode(ino);
320 static void
321 smb_put_super(struct super_block *sb)
323 struct smb_sb_info *server = &(sb->u.smbfs_sb);
325 if (server->sock_file) {
326 smb_proc_disconnect(server);
327 smb_dont_catch_keepalive(server);
328 fput(server->sock_file);
331 if (server->conn_pid)
332 kill_proc(server->conn_pid, SIGTERM, 1);
334 kfree(server->mnt);
335 kfree(sb->u.smbfs_sb.temp_buf);
336 if (server->packet)
337 smb_vfree(server->packet);
339 MOD_DEC_USE_COUNT;
342 struct super_block *
343 smb_read_super(struct super_block *sb, void *raw_data, int silent)
345 struct smb_mount_data *mnt;
346 struct inode *root_inode;
347 struct smb_fattr root;
349 MOD_INC_USE_COUNT;
351 if (!raw_data)
352 goto out_no_data;
353 if (((struct smb_mount_data *) raw_data)->version != SMB_MOUNT_VERSION)
354 goto out_wrong_data;
356 lock_super(sb);
358 sb->s_blocksize = 1024; /* Eh... Is this correct? */
359 sb->s_blocksize_bits = 10;
360 sb->s_magic = SMB_SUPER_MAGIC;
361 sb->s_flags = 0;
362 sb->s_op = &smb_sops;
364 sb->u.smbfs_sb.sock_file = NULL;
365 sb->u.smbfs_sb.sem = MUTEX;
366 sb->u.smbfs_sb.wait = NULL;
367 sb->u.smbfs_sb.conn_pid = 0;
368 sb->u.smbfs_sb.state = CONN_INVALID; /* no connection yet */
369 sb->u.smbfs_sb.generation = 0;
370 sb->u.smbfs_sb.packet_size = smb_round_length(SMB_INITIAL_PACKET_SIZE);
371 sb->u.smbfs_sb.packet = smb_vmalloc(sb->u.smbfs_sb.packet_size);
372 if (!sb->u.smbfs_sb.packet)
373 goto out_no_mem;
375 /* Allocate the global temp buffer */
376 sb->u.smbfs_sb.temp_buf = kmalloc(SMB_MAXPATHLEN + 20, GFP_KERNEL);
377 if (!sb->u.smbfs_sb.temp_buf)
378 goto out_no_temp;
380 /* Allocate the mount data structure */
381 mnt = kmalloc(sizeof(struct smb_mount_data), GFP_KERNEL);
382 if (!mnt)
383 goto out_no_mount;
384 *mnt = *((struct smb_mount_data *) raw_data);
385 /* ** temp ** pass config flags in file mode */
386 mnt->version = (mnt->file_mode >> 9);
387 #ifdef CONFIG_SMB_WIN95
388 mnt->version |= SMB_FIX_WIN95;
389 #endif
390 mnt->file_mode &= (S_IRWXU | S_IRWXG | S_IRWXO);
391 mnt->file_mode |= S_IFREG;
392 mnt->dir_mode &= (S_IRWXU | S_IRWXG | S_IRWXO);
393 mnt->dir_mode |= S_IFDIR;
394 sb->u.smbfs_sb.mnt = mnt;
396 * Display the enabled options
398 if (mnt->version & SMB_FIX_WIN95)
399 printk("SMBFS: Win 95 bug fixes enabled\n");
400 if (mnt->version & SMB_FIX_OLDATTR)
401 printk("SMBFS: Using core getattr (Win 95 speedup)\n");
402 else if (mnt->version & SMB_FIX_DIRATTR)
403 printk("SMBFS: Using dir ff getattr\n");
406 * Keep the super block locked while we get the root inode.
408 smb_init_root_dirent(&(sb->u.smbfs_sb), &root);
409 root_inode = smb_iget(sb, &root);
410 if (!root_inode)
411 goto out_no_root;
413 sb->s_root = d_alloc_root(root_inode, NULL);
414 if (!sb->s_root)
415 goto out_no_root;
417 unlock_super(sb);
418 return sb;
420 out_no_root:
421 iput(root_inode);
422 kfree(sb->u.smbfs_sb.mnt);
423 out_no_mount:
424 kfree(sb->u.smbfs_sb.temp_buf);
425 out_no_temp:
426 smb_vfree(sb->u.smbfs_sb.packet);
427 out_no_mem:
428 printk(KERN_ERR "smb_read_super: allocation failure\n");
429 unlock_super(sb);
430 goto out_fail;
431 out_wrong_data:
432 printk(KERN_ERR "SMBFS: need mount version %d\n", SMB_MOUNT_VERSION);
433 goto out_fail;
434 out_no_data:
435 printk("smb_read_super: missing data argument\n");
436 out_fail:
437 sb->s_dev = 0;
438 MOD_DEC_USE_COUNT;
439 return NULL;
442 static int
443 smb_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
445 struct statfs attr;
447 memset(&attr, 0, sizeof(attr));
449 smb_proc_dskattr(sb, &attr);
451 attr.f_type = SMB_SUPER_MAGIC;
452 attr.f_files = -1;
453 attr.f_ffree = -1;
454 attr.f_namelen = SMB_MAXPATHLEN;
455 return copy_to_user(buf, &attr, bufsiz) ? -EFAULT : 0;
459 smb_notify_change(struct dentry *dentry, struct iattr *attr)
461 struct inode *inode = dentry->d_inode;
462 struct smb_sb_info *server = server_from_dentry(dentry);
463 unsigned int mask = (S_IFREG | S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO);
464 int error, changed, refresh = 0;
465 struct smb_fattr fattr;
467 error = smb_revalidate_inode(dentry);
468 if (error)
469 goto out;
471 if ((error = inode_change_ok(inode, attr)) < 0)
472 goto out;
474 error = -EPERM;
475 if ((attr->ia_valid & ATTR_UID) && (attr->ia_uid != server->mnt->uid))
476 goto out;
478 if ((attr->ia_valid & ATTR_GID) && (attr->ia_uid != server->mnt->gid))
479 goto out;
481 if ((attr->ia_valid & ATTR_MODE) && (attr->ia_mode & ~mask))
482 goto out;
484 if ((attr->ia_valid & ATTR_SIZE) != 0)
486 #ifdef SMBFS_DEBUG_VERBOSE
487 printk("smb_notify_change: changing %s/%s, old size=%ld, new size=%ld\n",
488 dentry->d_parent->d_name.name, dentry->d_name.name,
489 (long) inode->i_size, (long) attr->ia_size);
490 #endif
491 error = smb_open(dentry, O_WRONLY);
492 if (error)
493 goto out;
494 error = smb_proc_trunc(server, inode->u.smbfs_i.fileid,
495 attr->ia_size);
496 if (error)
497 goto out;
499 * We don't implement an i_op->truncate operation,
500 * so we have to update the page cache here.
502 if (attr->ia_size < inode->i_size)
504 truncate_inode_pages(inode, attr->ia_size);
505 inode->i_size = attr->ia_size;
507 refresh = 1;
511 * Initialize the fattr and check for changed fields.
512 * Note: CTIME under SMB is creation time rather than
513 * change time, so we don't attempt to change it.
515 smb_get_inode_attr(inode, &fattr);
517 changed = 0;
518 if ((attr->ia_valid & ATTR_MTIME) != 0)
520 fattr.f_mtime = attr->ia_mtime;
521 changed = 1;
523 if ((attr->ia_valid & ATTR_ATIME) != 0)
525 fattr.f_atime = attr->ia_atime;
526 /* Earlier protocols don't have an access time */
527 if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2)
528 changed = 1;
530 if (changed)
532 error = smb_proc_settime(dentry, &fattr);
533 if (error)
534 goto out;
535 refresh = 1;
539 * Check for mode changes ... we're extremely limited in
540 * what can be set for SMB servers: just the read-only bit.
542 if ((attr->ia_valid & ATTR_MODE) != 0)
544 #ifdef SMBFS_DEBUG_VERBOSE
545 printk("smb_notify_change: %s/%s mode change, old=%x, new=%lx\n",
546 dentry->d_parent->d_name.name, dentry->d_name.name, fattr.f_mode,attr->ia_mode);
547 #endif
548 changed = 0;
549 if (attr->ia_mode & S_IWUSR)
551 if (fattr.attr & aRONLY)
553 fattr.attr &= ~aRONLY;
554 changed = 1;
556 } else
558 if (!(fattr.attr & aRONLY))
560 fattr.attr |= aRONLY;
561 changed = 1;
564 if (changed)
566 error = smb_proc_setattr(dentry, &fattr);
567 if (error)
568 goto out;
569 refresh = 1;
572 error = 0;
574 out:
575 if (refresh)
576 smb_refresh_inode(dentry);
577 return error;
580 #ifdef DEBUG_SMB_MALLOC
581 int smb_malloced;
582 int smb_current_kmalloced;
583 int smb_current_vmalloced;
584 #endif
586 static struct file_system_type smb_fs_type = {
587 "smbfs",
588 0 /* FS_NO_DCACHE doesn't work correctly */,
589 smb_read_super,
590 NULL
593 __initfunc(int init_smb_fs(void))
595 return register_filesystem(&smb_fs_type);
598 #ifdef MODULE
599 EXPORT_NO_SYMBOLS;
602 init_module(void)
604 pr_debug("smbfs: init_module called\n");
606 #ifdef DEBUG_SMB_MALLOC
607 smb_malloced = 0;
608 smb_current_kmalloced = 0;
609 smb_current_vmalloced = 0;
610 #endif
612 read_semaphore = MUTEX;
614 return init_smb_fs();
617 void
618 cleanup_module(void)
620 pr_debug("smbfs: cleanup_module called\n");
621 unregister_filesystem(&smb_fs_type);
622 #ifdef DEBUG_SMB_MALLOC
623 printk(KERN_DEBUG "smb_malloced: %d\n", smb_malloced);
624 printk(KERN_DEBUG "smb_current_kmalloced: %d\n",smb_current_kmalloced);
625 printk(KERN_DEBUG "smb_current_vmalloced: %d\n",smb_current_vmalloced);
626 #endif
629 #endif