Import 2.4.0-test2pre6
[davej-history.git] / fs / smbfs / inode.c
blob769aa7e5695d88920e6c85c439858182215c0583
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_delete_inode(struct inode *);
33 static void smb_put_super(struct super_block *);
34 static int smb_statfs(struct super_block *, struct statfs *);
35 static void smb_set_inode_attr(struct inode *, struct smb_fattr *);
37 static struct super_operations smb_sops =
39 put_inode: force_delete,
40 delete_inode: smb_delete_inode,
41 put_super: smb_put_super,
42 statfs: smb_statfs,
45 /* FIXME: Look at all inodes whether so that we do not get duplicate
46 * inode numbers. */
48 unsigned long
49 smb_invent_inos(unsigned long n)
51 static unsigned long ino = 2;
53 if (ino + 2*n < ino)
55 /* wrap around */
56 ino = 2;
58 ino += n;
59 return ino;
62 /* We are always generating a new inode here */
63 struct inode *
64 smb_iget(struct super_block *sb, struct smb_fattr *fattr)
66 struct inode *result;
68 pr_debug("smb_iget: %p\n", fattr);
70 result = get_empty_inode();
71 if (!result)
72 return result;
73 result->i_sb = sb;
74 result->i_dev = sb->s_dev;
75 result->i_ino = fattr->f_ino;
76 memset(&(result->u.smbfs_i), 0, sizeof(result->u.smbfs_i));
77 smb_set_inode_attr(result, fattr);
78 if (S_ISREG(result->i_mode)) {
79 result->i_op = &smb_file_inode_operations;
80 result->i_fop = &smb_file_operations;
81 result->i_data.a_ops = &smb_file_aops;
82 } else if (S_ISDIR(result->i_mode)) {
83 result->i_op = &smb_dir_inode_operations;
84 result->i_fop = &smb_dir_operations;
86 insert_inode_hash(result);
87 return result;
91 * Copy the inode data to a smb_fattr structure.
93 void
94 smb_get_inode_attr(struct inode *inode, struct smb_fattr *fattr)
96 memset(fattr, 0, sizeof(struct smb_fattr));
97 fattr->f_mode = inode->i_mode;
98 fattr->f_nlink = inode->i_nlink;
99 fattr->f_ino = inode->i_ino;
100 fattr->f_uid = inode->i_uid;
101 fattr->f_gid = inode->i_gid;
102 fattr->f_rdev = inode->i_rdev;
103 fattr->f_size = inode->i_size;
104 fattr->f_mtime = inode->i_mtime;
105 fattr->f_ctime = inode->i_ctime;
106 fattr->f_atime = inode->i_atime;
107 fattr->f_blksize= inode->i_blksize;
108 fattr->f_blocks = inode->i_blocks;
110 fattr->attr = inode->u.smbfs_i.attr;
112 * Keep the attributes in sync with the inode permissions.
114 if (fattr->f_mode & S_IWUSR)
115 fattr->attr &= ~aRONLY;
116 else
117 fattr->attr |= aRONLY;
120 static void
121 smb_set_inode_attr(struct inode *inode, struct smb_fattr *fattr)
123 inode->i_mode = fattr->f_mode;
124 inode->i_nlink = fattr->f_nlink;
125 inode->i_uid = fattr->f_uid;
126 inode->i_gid = fattr->f_gid;
127 inode->i_rdev = fattr->f_rdev;
128 inode->i_ctime = fattr->f_ctime;
129 inode->i_blksize= fattr->f_blksize;
130 inode->i_blocks = fattr->f_blocks;
132 * Don't change the size and mtime/atime fields
133 * if we're writing to the file.
135 if (!(inode->u.smbfs_i.cache_valid & SMB_F_LOCALWRITE))
137 inode->i_size = fattr->f_size;
138 inode->i_mtime = fattr->f_mtime;
139 inode->i_atime = fattr->f_atime;
142 inode->u.smbfs_i.attr = fattr->attr;
144 * Update the "last time refreshed" field for revalidation.
146 inode->u.smbfs_i.oldmtime = jiffies;
150 * This is called if the connection has gone bad ...
151 * try to kill off all the current inodes.
153 void
154 smb_invalidate_inodes(struct smb_sb_info *server)
156 #ifdef SMBFS_DEBUG_VERBOSE
157 printk("smb_invalidate_inodes\n");
158 #endif
159 shrink_dcache_sb(SB_of(server));
160 invalidate_inodes(SB_of(server));
164 * This is called to update the inode attributes after
165 * we've made changes to a file or directory.
167 static int
168 smb_refresh_inode(struct dentry *dentry)
170 struct inode *inode = dentry->d_inode;
171 int error;
172 struct smb_fattr fattr;
174 error = smb_proc_getattr(dentry, &fattr);
175 if (!error)
177 smb_renew_times(dentry);
179 * Check whether the type part of the mode changed,
180 * and don't update the attributes if it did.
182 if ((inode->i_mode & S_IFMT) == (fattr.f_mode & S_IFMT))
183 smb_set_inode_attr(inode, &fattr);
184 else
187 * Big trouble! The inode has become a new object,
188 * so any operations attempted on it are invalid.
190 * To limit damage, mark the inode as bad so that
191 * subsequent lookup validations will fail.
193 #ifdef SMBFS_PARANOIA
194 printk("smb_refresh_inode: %s/%s changed mode, %07o to %07o\n",
195 dentry->d_parent->d_name.name, dentry->d_name.name,
196 inode->i_mode, fattr.f_mode);
197 #endif
198 fattr.f_mode = inode->i_mode; /* save mode */
199 make_bad_inode(inode);
200 inode->i_mode = fattr.f_mode; /* restore mode */
202 * No need to worry about unhashing the dentry: the
203 * lookup validation will see that the inode is bad.
204 * But we do want to invalidate the caches ...
206 if (!S_ISDIR(inode->i_mode))
207 invalidate_inode_pages(inode);
208 else
209 smb_invalid_dir_cache(inode);
210 error = -EIO;
213 return error;
217 * This is called when we want to check whether the inode
218 * has changed on the server. If it has changed, we must
219 * invalidate our local caches.
222 smb_revalidate_inode(struct dentry *dentry)
224 struct inode *inode = dentry->d_inode;
225 time_t last_time;
226 int error = 0;
228 pr_debug("smb_revalidate_inode\n");
230 * If this is a file opened with write permissions,
231 * the inode will be up-to-date.
233 if (S_ISREG(inode->i_mode) && smb_is_open(inode))
235 if (inode->u.smbfs_i.access != SMB_O_RDONLY)
236 goto out;
240 * Check whether we've recently refreshed the inode.
242 if (time_before(jiffies, inode->u.smbfs_i.oldmtime + HZ/10))
244 #ifdef SMBFS_DEBUG_VERBOSE
245 printk("smb_revalidate_inode: up-to-date, jiffies=%lu, oldtime=%lu\n",
246 jiffies, inode->u.smbfs_i.oldmtime);
247 #endif
248 goto out;
252 * Save the last modified time, then refresh the inode.
253 * (Note: a size change should have a different mtime.)
255 last_time = inode->i_mtime;
256 error = smb_refresh_inode(dentry);
257 if (error || inode->i_mtime != last_time)
259 #ifdef SMBFS_DEBUG_VERBOSE
260 printk("smb_revalidate: %s/%s changed, old=%ld, new=%ld\n",
261 dentry->d_parent->d_name.name, dentry->d_name.name,
262 (long) last_time, (long) inode->i_mtime);
263 #endif
264 if (!S_ISDIR(inode->i_mode))
265 invalidate_inode_pages(inode);
266 else
267 smb_invalid_dir_cache(inode);
269 out:
270 return error;
274 * This routine is called when i_nlink == 0 and i_count goes to 0.
275 * All blocking cleanup operations need to go here to avoid races.
277 static void
278 smb_delete_inode(struct inode *ino)
280 pr_debug("smb_delete_inode\n");
281 if (smb_close(ino))
282 printk("smb_delete_inode: could not close inode %ld\n",
283 ino->i_ino);
284 clear_inode(ino);
287 static void
288 smb_put_super(struct super_block *sb)
290 struct smb_sb_info *server = &(sb->u.smbfs_sb);
292 if (server->sock_file) {
293 smb_proc_disconnect(server);
294 smb_dont_catch_keepalive(server);
295 fput(server->sock_file);
298 if (server->conn_pid)
299 kill_proc(server->conn_pid, SIGTERM, 1);
301 kfree(server->mnt);
302 kfree(sb->u.smbfs_sb.temp_buf);
303 if (server->packet)
304 smb_vfree(server->packet);
307 struct super_block *
308 smb_read_super(struct super_block *sb, void *raw_data, int silent)
310 struct smb_mount_data *mnt;
311 struct inode *root_inode;
312 struct smb_fattr root;
314 if (!raw_data)
315 goto out_no_data;
316 if (((struct smb_mount_data *) raw_data)->version != SMB_MOUNT_VERSION)
317 goto out_wrong_data;
319 sb->s_blocksize = 1024; /* Eh... Is this correct? */
320 sb->s_blocksize_bits = 10;
321 sb->s_magic = SMB_SUPER_MAGIC;
322 sb->s_flags = 0;
323 sb->s_op = &smb_sops;
325 sb->u.smbfs_sb.sock_file = NULL;
326 init_MUTEX(&sb->u.smbfs_sb.sem);
327 init_waitqueue_head(&sb->u.smbfs_sb.wait);
328 sb->u.smbfs_sb.conn_pid = 0;
329 sb->u.smbfs_sb.state = CONN_INVALID; /* no connection yet */
330 sb->u.smbfs_sb.generation = 0;
331 sb->u.smbfs_sb.packet_size = smb_round_length(SMB_INITIAL_PACKET_SIZE);
332 sb->u.smbfs_sb.packet = smb_vmalloc(sb->u.smbfs_sb.packet_size);
333 if (!sb->u.smbfs_sb.packet)
334 goto out_no_mem;
336 /* Allocate the global temp buffer */
337 sb->u.smbfs_sb.temp_buf = kmalloc(SMB_MAXPATHLEN + 20, GFP_KERNEL);
338 if (!sb->u.smbfs_sb.temp_buf)
339 goto out_no_temp;
341 /* Allocate the mount data structure */
342 mnt = kmalloc(sizeof(struct smb_mount_data), GFP_KERNEL);
343 if (!mnt)
344 goto out_no_mount;
345 *mnt = *((struct smb_mount_data *) raw_data);
346 /* ** temp ** pass config flags in file mode */
347 mnt->version = (mnt->file_mode >> 9);
348 mnt->file_mode &= (S_IRWXU | S_IRWXG | S_IRWXO);
349 mnt->file_mode |= S_IFREG;
350 mnt->dir_mode &= (S_IRWXU | S_IRWXG | S_IRWXO);
351 mnt->dir_mode |= S_IFDIR;
352 sb->u.smbfs_sb.mnt = mnt;
354 * Display the enabled options
356 if (mnt->version & SMB_FIX_OLDATTR)
357 printk("SMBFS: Using core getattr (Win 95 speedup)\n");
358 else if (mnt->version & SMB_FIX_DIRATTR)
359 printk("SMBFS: Using dir ff getattr\n");
362 * Keep the super block locked while we get the root inode.
364 smb_init_root_dirent(&(sb->u.smbfs_sb), &root);
365 root_inode = smb_iget(sb, &root);
366 if (!root_inode)
367 goto out_no_root;
369 sb->s_root = d_alloc_root(root_inode);
370 if (!sb->s_root)
371 goto out_no_root;
373 return sb;
375 out_no_root:
376 iput(root_inode);
377 kfree(sb->u.smbfs_sb.mnt);
378 out_no_mount:
379 kfree(sb->u.smbfs_sb.temp_buf);
380 out_no_temp:
381 smb_vfree(sb->u.smbfs_sb.packet);
382 out_no_mem:
383 printk(KERN_ERR "smb_read_super: allocation failure\n");
384 goto out_fail;
385 out_wrong_data:
386 printk(KERN_ERR "SMBFS: need mount version %d\n", SMB_MOUNT_VERSION);
387 goto out_fail;
388 out_no_data:
389 printk("smb_read_super: missing data argument\n");
390 out_fail:
391 return NULL;
394 static int
395 smb_statfs(struct super_block *sb, struct statfs *buf)
397 smb_proc_dskattr(sb, buf);
399 buf->f_type = SMB_SUPER_MAGIC;
400 buf->f_namelen = SMB_MAXPATHLEN;
401 return 0;
405 smb_notify_change(struct dentry *dentry, struct iattr *attr)
407 struct inode *inode = dentry->d_inode;
408 struct smb_sb_info *server = server_from_dentry(dentry);
409 unsigned int mask = (S_IFREG | S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO);
410 int error, changed, refresh = 0;
411 struct smb_fattr fattr;
413 error = smb_revalidate_inode(dentry);
414 if (error)
415 goto out;
417 if ((error = inode_change_ok(inode, attr)) < 0)
418 goto out;
420 error = -EPERM;
421 if ((attr->ia_valid & ATTR_UID) && (attr->ia_uid != server->mnt->uid))
422 goto out;
424 if ((attr->ia_valid & ATTR_GID) && (attr->ia_uid != server->mnt->gid))
425 goto out;
427 if ((attr->ia_valid & ATTR_MODE) && (attr->ia_mode & ~mask))
428 goto out;
430 if ((attr->ia_valid & ATTR_SIZE) != 0)
432 #ifdef SMBFS_DEBUG_VERBOSE
433 printk("smb_notify_change: changing %s/%s, old size=%ld, new size=%ld\n",
434 dentry->d_parent->d_name.name, dentry->d_name.name,
435 (long) inode->i_size, (long) attr->ia_size);
436 #endif
437 error = smb_open(dentry, O_WRONLY);
438 if (error)
439 goto out;
440 error = smb_proc_trunc(server, inode->u.smbfs_i.fileid,
441 attr->ia_size);
442 if (error)
443 goto out;
444 vmtruncate(inode, attr->ia_size);
445 refresh = 1;
449 * Initialize the fattr and check for changed fields.
450 * Note: CTIME under SMB is creation time rather than
451 * change time, so we don't attempt to change it.
453 smb_get_inode_attr(inode, &fattr);
455 changed = 0;
456 if ((attr->ia_valid & ATTR_MTIME) != 0)
458 fattr.f_mtime = attr->ia_mtime;
459 changed = 1;
461 if ((attr->ia_valid & ATTR_ATIME) != 0)
463 fattr.f_atime = attr->ia_atime;
464 /* Earlier protocols don't have an access time */
465 if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2)
466 changed = 1;
468 if (changed)
470 error = smb_proc_settime(dentry, &fattr);
471 if (error)
472 goto out;
473 refresh = 1;
477 * Check for mode changes ... we're extremely limited in
478 * what can be set for SMB servers: just the read-only bit.
480 if ((attr->ia_valid & ATTR_MODE) != 0)
482 #ifdef SMBFS_DEBUG_VERBOSE
483 printk("smb_notify_change: %s/%s mode change, old=%x, new=%lx\n",
484 dentry->d_parent->d_name.name, dentry->d_name.name, fattr.f_mode,attr->ia_mode);
485 #endif
486 changed = 0;
487 if (attr->ia_mode & S_IWUSR)
489 if (fattr.attr & aRONLY)
491 fattr.attr &= ~aRONLY;
492 changed = 1;
494 } else
496 if (!(fattr.attr & aRONLY))
498 fattr.attr |= aRONLY;
499 changed = 1;
502 if (changed)
504 error = smb_proc_setattr(dentry, &fattr);
505 if (error)
506 goto out;
507 refresh = 1;
510 error = 0;
512 out:
513 if (refresh)
514 smb_refresh_inode(dentry);
515 return error;
518 #ifdef DEBUG_SMB_MALLOC
519 int smb_malloced;
520 int smb_current_kmalloced;
521 int smb_current_vmalloced;
522 #endif
524 static DECLARE_FSTYPE( smb_fs_type, "smbfs", smb_read_super, 0);
526 static int __init init_smb_fs(void)
528 pr_debug("smbfs: init_module called\n");
530 #ifdef DEBUG_SMB_MALLOC
531 smb_malloced = 0;
532 smb_current_kmalloced = 0;
533 smb_current_vmalloced = 0;
534 #endif
536 return register_filesystem(&smb_fs_type);
539 static void __exit exit_smb_fs(void)
541 pr_debug("smbfs: cleanup_module called\n");
542 unregister_filesystem(&smb_fs_type);
543 #ifdef DEBUG_SMB_MALLOC
544 printk(KERN_DEBUG "smb_malloced: %d\n", smb_malloced);
545 printk(KERN_DEBUG "smb_current_kmalloced: %d\n",smb_current_kmalloced);
546 printk(KERN_DEBUG "smb_current_vmalloced: %d\n",smb_current_vmalloced);
547 #endif
550 EXPORT_NO_SYMBOLS;
552 module_init(init_smb_fs)
553 module_exit(exit_smb_fs)