* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / fs / smbfs / inode.c
blob419adf970fcc6a499d3aac4a2e72e46ca8cac64c
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/module.h>
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/string.h>
14 #include <linux/stat.h>
15 #include <linux/errno.h>
16 #include <linux/locks.h>
17 #include <linux/malloc.h>
18 #include <linux/init.h>
19 #include <linux/file.h>
20 #include <linux/dcache.h>
22 #include <linux/smb_fs.h>
23 #include <linux/smbno.h>
24 #include <linux/smb_mount.h>
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
29 #define SMBFS_PARANOIA 1
30 /* #define SMBFS_DEBUG_VERBOSE 1 */
32 static void smb_read_inode(struct inode *);
33 static void smb_put_inode(struct inode *);
34 static void smb_delete_inode(struct inode *);
35 static void smb_put_super(struct super_block *);
36 static int smb_statfs(struct super_block *, struct statfs *, int);
37 static void smb_set_inode_attr(struct inode *, struct smb_fattr *);
39 static struct super_operations smb_sops =
41 smb_read_inode, /* read inode */
42 NULL, /* write inode */
43 smb_put_inode, /* put inode */
44 smb_delete_inode, /* delete inode */
45 smb_notify_change, /* notify change */
46 smb_put_super, /* put superblock */
47 NULL, /* write superblock */
48 smb_statfs, /* stat filesystem */
49 NULL /* remount filesystem */
52 /* FIXME: Look at all inodes whether so that we do not get duplicate
53 * inode numbers. */
55 unsigned long
56 smb_invent_inos(unsigned long n)
58 static unsigned long ino = 2;
60 if (ino + 2*n < ino)
62 /* wrap around */
63 ino = 2;
65 ino += n;
66 return ino;
69 /* We are always generating a new inode here */
70 struct inode *
71 smb_iget(struct super_block *sb, struct smb_fattr *fattr)
73 struct inode *result;
75 pr_debug("smb_iget: %p\n", fattr);
77 result = get_empty_inode();
78 result->i_sb = sb;
79 result->i_dev = sb->s_dev;
80 result->i_ino = fattr->f_ino;
81 memset(&(result->u.smbfs_i), 0, sizeof(result->u.smbfs_i));
82 smb_set_inode_attr(result, fattr);
83 if (S_ISREG(result->i_mode))
84 result->i_op = &smb_file_inode_operations;
85 else if (S_ISDIR(result->i_mode))
86 result->i_op = &smb_dir_inode_operations;
87 else
88 result->i_op = NULL;
89 insert_inode_hash(result);
90 return result;
94 * Copy the inode data to a smb_fattr structure.
96 void
97 smb_get_inode_attr(struct inode *inode, struct smb_fattr *fattr)
99 memset(fattr, 0, sizeof(struct smb_fattr));
100 fattr->f_mode = inode->i_mode;
101 fattr->f_nlink = inode->i_nlink;
102 fattr->f_ino = inode->i_ino;
103 fattr->f_uid = inode->i_uid;
104 fattr->f_gid = inode->i_gid;
105 fattr->f_rdev = inode->i_rdev;
106 fattr->f_size = inode->i_size;
107 fattr->f_mtime = inode->i_mtime;
108 fattr->f_ctime = inode->i_ctime;
109 fattr->f_atime = inode->i_atime;
110 fattr->f_blksize= inode->i_blksize;
111 fattr->f_blocks = inode->i_blocks;
113 fattr->attr = inode->u.smbfs_i.attr;
115 * Keep the attributes in sync with the inode permissions.
117 if (fattr->f_mode & S_IWUSR)
118 fattr->attr &= ~aRONLY;
119 else
120 fattr->attr |= aRONLY;
123 static void
124 smb_set_inode_attr(struct inode *inode, struct smb_fattr *fattr)
126 inode->i_mode = fattr->f_mode;
127 inode->i_nlink = fattr->f_nlink;
128 inode->i_uid = fattr->f_uid;
129 inode->i_gid = fattr->f_gid;
130 inode->i_rdev = fattr->f_rdev;
131 inode->i_ctime = fattr->f_ctime;
132 inode->i_blksize= fattr->f_blksize;
133 inode->i_blocks = fattr->f_blocks;
135 * Don't change the size and mtime/atime fields
136 * if we're writing to the file.
138 if (!(inode->u.smbfs_i.cache_valid & SMB_F_LOCALWRITE))
140 inode->i_size = fattr->f_size;
141 inode->i_mtime = fattr->f_mtime;
142 inode->i_atime = fattr->f_atime;
145 inode->u.smbfs_i.attr = fattr->attr;
147 * Update the "last time refreshed" field for revalidation.
149 inode->u.smbfs_i.oldmtime = jiffies;
152 static void
153 smb_read_inode(struct inode *inode)
155 /* Now it can be called only by NFS */
156 printk("smb_read_inode called from invalid point\n");
157 return;
161 * This is called if the connection has gone bad ...
162 * try to kill off all the current inodes.
164 void
165 smb_invalidate_inodes(struct smb_sb_info *server)
167 #ifdef SMBFS_DEBUG_VERBOSE
168 printk("smb_invalidate_inodes\n");
169 #endif
170 shrink_dcache_sb(SB_of(server));
171 invalidate_inodes(SB_of(server));
175 * This is called to update the inode attributes after
176 * we've made changes to a file or directory.
178 static int
179 smb_refresh_inode(struct dentry *dentry)
181 struct inode *inode = dentry->d_inode;
182 int error;
183 struct smb_fattr fattr;
185 error = smb_proc_getattr(dentry, &fattr);
186 if (!error)
188 smb_renew_times(dentry);
190 * Check whether the type part of the mode changed,
191 * and don't update the attributes if it did.
193 if ((inode->i_mode & S_IFMT) == (fattr.f_mode & S_IFMT))
194 smb_set_inode_attr(inode, &fattr);
195 else
198 * Big trouble! The inode has become a new object,
199 * so any operations attempted on it are invalid.
201 * To limit damage, mark the inode as bad so that
202 * subsequent lookup validations will fail.
204 #ifdef SMBFS_PARANOIA
205 printk("smb_refresh_inode: %s/%s changed mode, %07o to %07o\n",
206 dentry->d_parent->d_name.name, dentry->d_name.name,
207 inode->i_mode, fattr.f_mode);
208 #endif
209 fattr.f_mode = inode->i_mode; /* save mode */
210 make_bad_inode(inode);
211 inode->i_mode = fattr.f_mode; /* restore mode */
213 * No need to worry about unhashing the dentry: the
214 * lookup validation will see that the inode is bad.
215 * But we do want to invalidate the caches ...
217 if (!S_ISDIR(inode->i_mode))
218 invalidate_inode_pages(inode);
219 else
220 smb_invalid_dir_cache(inode);
221 error = -EIO;
224 return error;
228 * This is called when we want to check whether the inode
229 * has changed on the server. If it has changed, we must
230 * invalidate our local caches.
233 smb_revalidate_inode(struct dentry *dentry)
235 struct inode *inode = dentry->d_inode;
236 time_t last_time;
237 int error = 0;
239 pr_debug("smb_revalidate_inode\n");
241 * If this is a file opened with write permissions,
242 * the inode will be up-to-date.
244 if (S_ISREG(inode->i_mode) && smb_is_open(inode))
246 if (inode->u.smbfs_i.access != SMB_O_RDONLY)
247 goto out;
251 * Check whether we've recently refreshed the inode.
253 if (time_before(jiffies, inode->u.smbfs_i.oldmtime + HZ/10))
255 #ifdef SMBFS_DEBUG_VERBOSE
256 printk("smb_revalidate_inode: up-to-date, jiffies=%lu, oldtime=%lu\n",
257 jiffies, inode->u.smbfs_i.oldmtime);
258 #endif
259 goto out;
263 * Save the last modified time, then refresh the inode.
264 * (Note: a size change should have a different mtime.)
266 last_time = inode->i_mtime;
267 error = smb_refresh_inode(dentry);
268 if (error || inode->i_mtime != last_time)
270 #ifdef SMBFS_DEBUG_VERBOSE
271 printk("smb_revalidate: %s/%s changed, old=%ld, new=%ld\n",
272 dentry->d_parent->d_name.name, dentry->d_name.name,
273 (long) last_time, (long) inode->i_mtime);
274 #endif
275 if (!S_ISDIR(inode->i_mode))
276 invalidate_inode_pages(inode);
277 else
278 smb_invalid_dir_cache(inode);
280 out:
281 return error;
285 * This routine is called for every iput(). We clear i_nlink
286 * on the last use to force a call to delete_inode.
288 static void
289 smb_put_inode(struct inode *ino)
291 pr_debug("smb_put_inode: count = %d\n", ino->i_count);
292 if (ino->i_count == 1)
293 ino->i_nlink = 0;
297 * This routine is called when i_nlink == 0 and i_count goes to 0.
298 * All blocking cleanup operations need to go here to avoid races.
300 static void
301 smb_delete_inode(struct inode *ino)
303 pr_debug("smb_delete_inode\n");
304 if (smb_close(ino))
305 printk("smb_delete_inode: could not close inode %ld\n",
306 ino->i_ino);
307 clear_inode(ino);
310 static void
311 smb_put_super(struct super_block *sb)
313 struct smb_sb_info *server = &(sb->u.smbfs_sb);
315 if (server->sock_file) {
316 smb_proc_disconnect(server);
317 smb_dont_catch_keepalive(server);
318 fput(server->sock_file);
321 if (server->conn_pid)
322 kill_proc(server->conn_pid, SIGTERM, 1);
324 kfree(server->mnt);
325 kfree(sb->u.smbfs_sb.temp_buf);
326 if (server->packet)
327 smb_vfree(server->packet);
329 MOD_DEC_USE_COUNT;
332 struct super_block *
333 smb_read_super(struct super_block *sb, void *raw_data, int silent)
335 struct smb_mount_data *mnt;
336 struct inode *root_inode;
337 struct smb_fattr root;
339 MOD_INC_USE_COUNT;
341 if (!raw_data)
342 goto out_no_data;
343 if (((struct smb_mount_data *) raw_data)->version != SMB_MOUNT_VERSION)
344 goto out_wrong_data;
346 lock_super(sb);
348 sb->s_blocksize = 1024; /* Eh... Is this correct? */
349 sb->s_blocksize_bits = 10;
350 sb->s_magic = SMB_SUPER_MAGIC;
351 sb->s_flags = 0;
352 sb->s_op = &smb_sops;
354 sb->u.smbfs_sb.sock_file = NULL;
355 init_MUTEX(&sb->u.smbfs_sb.sem);
356 init_waitqueue_head(&sb->u.smbfs_sb.wait);
357 sb->u.smbfs_sb.conn_pid = 0;
358 sb->u.smbfs_sb.state = CONN_INVALID; /* no connection yet */
359 sb->u.smbfs_sb.generation = 0;
360 sb->u.smbfs_sb.packet_size = smb_round_length(SMB_INITIAL_PACKET_SIZE);
361 sb->u.smbfs_sb.packet = smb_vmalloc(sb->u.smbfs_sb.packet_size);
362 if (!sb->u.smbfs_sb.packet)
363 goto out_no_mem;
365 /* Allocate the global temp buffer */
366 sb->u.smbfs_sb.temp_buf = kmalloc(SMB_MAXPATHLEN + 20, GFP_KERNEL);
367 if (!sb->u.smbfs_sb.temp_buf)
368 goto out_no_temp;
370 /* Allocate the mount data structure */
371 mnt = kmalloc(sizeof(struct smb_mount_data), GFP_KERNEL);
372 if (!mnt)
373 goto out_no_mount;
374 *mnt = *((struct smb_mount_data *) raw_data);
375 /* ** temp ** pass config flags in file mode */
376 mnt->version = (mnt->file_mode >> 9);
377 mnt->file_mode &= (S_IRWXU | S_IRWXG | S_IRWXO);
378 mnt->file_mode |= S_IFREG;
379 mnt->dir_mode &= (S_IRWXU | S_IRWXG | S_IRWXO);
380 mnt->dir_mode |= S_IFDIR;
381 sb->u.smbfs_sb.mnt = mnt;
383 * Display the enabled options
385 if (mnt->version & SMB_FIX_OLDATTR)
386 printk("SMBFS: Using core getattr (Win 95 speedup)\n");
387 else if (mnt->version & SMB_FIX_DIRATTR)
388 printk("SMBFS: Using dir ff getattr\n");
391 * Keep the super block locked while we get the root inode.
393 smb_init_root_dirent(&(sb->u.smbfs_sb), &root);
394 root_inode = smb_iget(sb, &root);
395 if (!root_inode)
396 goto out_no_root;
398 sb->s_root = d_alloc_root(root_inode);
399 if (!sb->s_root)
400 goto out_no_root;
402 unlock_super(sb);
403 return sb;
405 out_no_root:
406 iput(root_inode);
407 kfree(sb->u.smbfs_sb.mnt);
408 out_no_mount:
409 kfree(sb->u.smbfs_sb.temp_buf);
410 out_no_temp:
411 smb_vfree(sb->u.smbfs_sb.packet);
412 out_no_mem:
413 printk(KERN_ERR "smb_read_super: allocation failure\n");
414 unlock_super(sb);
415 goto out_fail;
416 out_wrong_data:
417 printk(KERN_ERR "SMBFS: need mount version %d\n", SMB_MOUNT_VERSION);
418 goto out_fail;
419 out_no_data:
420 printk("smb_read_super: missing data argument\n");
421 out_fail:
422 sb->s_dev = 0;
423 MOD_DEC_USE_COUNT;
424 return NULL;
427 static int
428 smb_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
430 struct statfs attr;
432 memset(&attr, 0, sizeof(attr));
434 smb_proc_dskattr(sb, &attr);
436 attr.f_type = SMB_SUPER_MAGIC;
437 attr.f_files = -1;
438 attr.f_ffree = -1;
439 attr.f_namelen = SMB_MAXPATHLEN;
440 return copy_to_user(buf, &attr, bufsiz) ? -EFAULT : 0;
444 smb_notify_change(struct dentry *dentry, struct iattr *attr)
446 struct inode *inode = dentry->d_inode;
447 struct smb_sb_info *server = server_from_dentry(dentry);
448 unsigned int mask = (S_IFREG | S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO);
449 int error, changed, refresh = 0;
450 struct smb_fattr fattr;
452 error = smb_revalidate_inode(dentry);
453 if (error)
454 goto out;
456 if ((error = inode_change_ok(inode, attr)) < 0)
457 goto out;
459 error = -EPERM;
460 if ((attr->ia_valid & ATTR_UID) && (attr->ia_uid != server->mnt->uid))
461 goto out;
463 if ((attr->ia_valid & ATTR_GID) && (attr->ia_uid != server->mnt->gid))
464 goto out;
466 if ((attr->ia_valid & ATTR_MODE) && (attr->ia_mode & ~mask))
467 goto out;
469 if ((attr->ia_valid & ATTR_SIZE) != 0)
471 #ifdef SMBFS_DEBUG_VERBOSE
472 printk("smb_notify_change: changing %s/%s, old size=%ld, new size=%ld\n",
473 dentry->d_parent->d_name.name, dentry->d_name.name,
474 (long) inode->i_size, (long) attr->ia_size);
475 #endif
476 error = smb_open(dentry, O_WRONLY);
477 if (error)
478 goto out;
479 error = smb_proc_trunc(server, inode->u.smbfs_i.fileid,
480 attr->ia_size);
481 if (error)
482 goto out;
484 * We don't implement an i_op->truncate operation,
485 * so we have to update the page cache here.
487 if (attr->ia_size < inode->i_size)
489 truncate_inode_pages(inode, attr->ia_size);
490 inode->i_size = attr->ia_size;
492 refresh = 1;
496 * Initialize the fattr and check for changed fields.
497 * Note: CTIME under SMB is creation time rather than
498 * change time, so we don't attempt to change it.
500 smb_get_inode_attr(inode, &fattr);
502 changed = 0;
503 if ((attr->ia_valid & ATTR_MTIME) != 0)
505 fattr.f_mtime = attr->ia_mtime;
506 changed = 1;
508 if ((attr->ia_valid & ATTR_ATIME) != 0)
510 fattr.f_atime = attr->ia_atime;
511 /* Earlier protocols don't have an access time */
512 if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2)
513 changed = 1;
515 if (changed)
517 error = smb_proc_settime(dentry, &fattr);
518 if (error)
519 goto out;
520 refresh = 1;
524 * Check for mode changes ... we're extremely limited in
525 * what can be set for SMB servers: just the read-only bit.
527 if ((attr->ia_valid & ATTR_MODE) != 0)
529 #ifdef SMBFS_DEBUG_VERBOSE
530 printk("smb_notify_change: %s/%s mode change, old=%x, new=%lx\n",
531 dentry->d_parent->d_name.name, dentry->d_name.name, fattr.f_mode,attr->ia_mode);
532 #endif
533 changed = 0;
534 if (attr->ia_mode & S_IWUSR)
536 if (fattr.attr & aRONLY)
538 fattr.attr &= ~aRONLY;
539 changed = 1;
541 } else
543 if (!(fattr.attr & aRONLY))
545 fattr.attr |= aRONLY;
546 changed = 1;
549 if (changed)
551 error = smb_proc_setattr(dentry, &fattr);
552 if (error)
553 goto out;
554 refresh = 1;
557 error = 0;
559 out:
560 if (refresh)
561 smb_refresh_inode(dentry);
562 return error;
565 #ifdef DEBUG_SMB_MALLOC
566 int smb_malloced;
567 int smb_current_kmalloced;
568 int smb_current_vmalloced;
569 #endif
571 static struct file_system_type smb_fs_type = {
572 "smbfs",
573 0 /* FS_NO_DCACHE doesn't work correctly */,
574 smb_read_super,
575 NULL
578 int __init init_smb_fs(void)
580 return register_filesystem(&smb_fs_type);
583 #ifdef MODULE
584 EXPORT_NO_SYMBOLS;
587 init_module(void)
589 pr_debug("smbfs: init_module called\n");
591 #ifdef DEBUG_SMB_MALLOC
592 smb_malloced = 0;
593 smb_current_kmalloced = 0;
594 smb_current_vmalloced = 0;
595 #endif
597 return init_smb_fs();
600 void
601 cleanup_module(void)
603 pr_debug("smbfs: cleanup_module called\n");
604 unregister_filesystem(&smb_fs_type);
605 #ifdef DEBUG_SMB_MALLOC
606 printk(KERN_DEBUG "smb_malloced: %d\n", smb_malloced);
607 printk(KERN_DEBUG "smb_current_kmalloced: %d\n",smb_current_kmalloced);
608 printk(KERN_DEBUG "smb_current_vmalloced: %d\n",smb_current_vmalloced);
609 #endif
612 #endif