Import 2.3.48
[davej-history.git] / fs / smbfs / inode.c
blob3b13fdd8041375a79ab56d3ccb2a4625b19a27cb
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_put_inode(struct inode *);
33 static void smb_delete_inode(struct inode *);
34 static void smb_put_super(struct super_block *);
35 static int smb_statfs(struct super_block *, struct statfs *, int);
36 static void smb_set_inode_attr(struct inode *, struct smb_fattr *);
38 static struct super_operations smb_sops =
40 put_inode: smb_put_inode,
41 delete_inode: smb_delete_inode,
42 put_super: smb_put_super,
43 statfs: smb_statfs,
46 /* FIXME: Look at all inodes whether so that we do not get duplicate
47 * inode numbers. */
49 unsigned long
50 smb_invent_inos(unsigned long n)
52 static unsigned long ino = 2;
54 if (ino + 2*n < ino)
56 /* wrap around */
57 ino = 2;
59 ino += n;
60 return ino;
63 /* We are always generating a new inode here */
64 struct inode *
65 smb_iget(struct super_block *sb, struct smb_fattr *fattr)
67 struct inode *result;
69 pr_debug("smb_iget: %p\n", fattr);
71 result = get_empty_inode();
72 if (!result)
73 return result;
74 result->i_sb = sb;
75 result->i_dev = sb->s_dev;
76 result->i_ino = fattr->f_ino;
77 memset(&(result->u.smbfs_i), 0, sizeof(result->u.smbfs_i));
78 smb_set_inode_attr(result, fattr);
79 if (S_ISREG(result->i_mode)) {
80 result->i_op = &smb_file_inode_operations;
81 result->i_fop = &smb_file_operations;
82 result->i_data.a_ops = &smb_file_aops;
83 } else if (S_ISDIR(result->i_mode)) {
84 result->i_op = &smb_dir_inode_operations;
85 result->i_fop = &smb_dir_operations;
87 insert_inode_hash(result);
88 return result;
92 * Copy the inode data to a smb_fattr structure.
94 void
95 smb_get_inode_attr(struct inode *inode, struct smb_fattr *fattr)
97 memset(fattr, 0, sizeof(struct smb_fattr));
98 fattr->f_mode = inode->i_mode;
99 fattr->f_nlink = inode->i_nlink;
100 fattr->f_ino = inode->i_ino;
101 fattr->f_uid = inode->i_uid;
102 fattr->f_gid = inode->i_gid;
103 fattr->f_rdev = inode->i_rdev;
104 fattr->f_size = inode->i_size;
105 fattr->f_mtime = inode->i_mtime;
106 fattr->f_ctime = inode->i_ctime;
107 fattr->f_atime = inode->i_atime;
108 fattr->f_blksize= inode->i_blksize;
109 fattr->f_blocks = inode->i_blocks;
111 fattr->attr = inode->u.smbfs_i.attr;
113 * Keep the attributes in sync with the inode permissions.
115 if (fattr->f_mode & S_IWUSR)
116 fattr->attr &= ~aRONLY;
117 else
118 fattr->attr |= aRONLY;
121 static void
122 smb_set_inode_attr(struct inode *inode, struct smb_fattr *fattr)
124 inode->i_mode = fattr->f_mode;
125 inode->i_nlink = fattr->f_nlink;
126 inode->i_uid = fattr->f_uid;
127 inode->i_gid = fattr->f_gid;
128 inode->i_rdev = fattr->f_rdev;
129 inode->i_ctime = fattr->f_ctime;
130 inode->i_blksize= fattr->f_blksize;
131 inode->i_blocks = fattr->f_blocks;
133 * Don't change the size and mtime/atime fields
134 * if we're writing to the file.
136 if (!(inode->u.smbfs_i.cache_valid & SMB_F_LOCALWRITE))
138 inode->i_size = fattr->f_size;
139 inode->i_mtime = fattr->f_mtime;
140 inode->i_atime = fattr->f_atime;
143 inode->u.smbfs_i.attr = fattr->attr;
145 * Update the "last time refreshed" field for revalidation.
147 inode->u.smbfs_i.oldmtime = jiffies;
151 * This is called if the connection has gone bad ...
152 * try to kill off all the current inodes.
154 void
155 smb_invalidate_inodes(struct smb_sb_info *server)
157 #ifdef SMBFS_DEBUG_VERBOSE
158 printk("smb_invalidate_inodes\n");
159 #endif
160 shrink_dcache_sb(SB_of(server));
161 invalidate_inodes(SB_of(server));
165 * This is called to update the inode attributes after
166 * we've made changes to a file or directory.
168 static int
169 smb_refresh_inode(struct dentry *dentry)
171 struct inode *inode = dentry->d_inode;
172 int error;
173 struct smb_fattr fattr;
175 error = smb_proc_getattr(dentry, &fattr);
176 if (!error)
178 smb_renew_times(dentry);
180 * Check whether the type part of the mode changed,
181 * and don't update the attributes if it did.
183 if ((inode->i_mode & S_IFMT) == (fattr.f_mode & S_IFMT))
184 smb_set_inode_attr(inode, &fattr);
185 else
188 * Big trouble! The inode has become a new object,
189 * so any operations attempted on it are invalid.
191 * To limit damage, mark the inode as bad so that
192 * subsequent lookup validations will fail.
194 #ifdef SMBFS_PARANOIA
195 printk("smb_refresh_inode: %s/%s changed mode, %07o to %07o\n",
196 dentry->d_parent->d_name.name, dentry->d_name.name,
197 inode->i_mode, fattr.f_mode);
198 #endif
199 fattr.f_mode = inode->i_mode; /* save mode */
200 make_bad_inode(inode);
201 inode->i_mode = fattr.f_mode; /* restore mode */
203 * No need to worry about unhashing the dentry: the
204 * lookup validation will see that the inode is bad.
205 * But we do want to invalidate the caches ...
207 if (!S_ISDIR(inode->i_mode))
208 invalidate_inode_pages(inode);
209 else
210 smb_invalid_dir_cache(inode);
211 error = -EIO;
214 return error;
218 * This is called when we want to check whether the inode
219 * has changed on the server. If it has changed, we must
220 * invalidate our local caches.
223 smb_revalidate_inode(struct dentry *dentry)
225 struct inode *inode = dentry->d_inode;
226 time_t last_time;
227 int error = 0;
229 pr_debug("smb_revalidate_inode\n");
231 * If this is a file opened with write permissions,
232 * the inode will be up-to-date.
234 if (S_ISREG(inode->i_mode) && smb_is_open(inode))
236 if (inode->u.smbfs_i.access != SMB_O_RDONLY)
237 goto out;
241 * Check whether we've recently refreshed the inode.
243 if (time_before(jiffies, inode->u.smbfs_i.oldmtime + HZ/10))
245 #ifdef SMBFS_DEBUG_VERBOSE
246 printk("smb_revalidate_inode: up-to-date, jiffies=%lu, oldtime=%lu\n",
247 jiffies, inode->u.smbfs_i.oldmtime);
248 #endif
249 goto out;
253 * Save the last modified time, then refresh the inode.
254 * (Note: a size change should have a different mtime.)
256 last_time = inode->i_mtime;
257 error = smb_refresh_inode(dentry);
258 if (error || inode->i_mtime != last_time)
260 #ifdef SMBFS_DEBUG_VERBOSE
261 printk("smb_revalidate: %s/%s changed, old=%ld, new=%ld\n",
262 dentry->d_parent->d_name.name, dentry->d_name.name,
263 (long) last_time, (long) inode->i_mtime);
264 #endif
265 if (!S_ISDIR(inode->i_mode))
266 invalidate_inode_pages(inode);
267 else
268 smb_invalid_dir_cache(inode);
270 out:
271 return error;
275 * This routine is called for every iput(). We clear i_nlink
276 * on the last use to force a call to delete_inode.
278 static void
279 smb_put_inode(struct inode *ino)
281 pr_debug("smb_put_inode: count = %d\n", ino->i_count);
282 if (ino->i_count == 1)
283 ino->i_nlink = 0;
287 * This routine is called when i_nlink == 0 and i_count goes to 0.
288 * All blocking cleanup operations need to go here to avoid races.
290 static void
291 smb_delete_inode(struct inode *ino)
293 pr_debug("smb_delete_inode\n");
294 if (smb_close(ino))
295 printk("smb_delete_inode: could not close inode %ld\n",
296 ino->i_ino);
297 clear_inode(ino);
300 static void
301 smb_put_super(struct super_block *sb)
303 struct smb_sb_info *server = &(sb->u.smbfs_sb);
305 if (server->sock_file) {
306 smb_proc_disconnect(server);
307 smb_dont_catch_keepalive(server);
308 fput(server->sock_file);
311 if (server->conn_pid)
312 kill_proc(server->conn_pid, SIGTERM, 1);
314 kfree(server->mnt);
315 kfree(sb->u.smbfs_sb.temp_buf);
316 if (server->packet)
317 smb_vfree(server->packet);
319 MOD_DEC_USE_COUNT;
322 struct super_block *
323 smb_read_super(struct super_block *sb, void *raw_data, int silent)
325 struct smb_mount_data *mnt;
326 struct inode *root_inode;
327 struct smb_fattr root;
329 MOD_INC_USE_COUNT;
331 if (!raw_data)
332 goto out_no_data;
333 if (((struct smb_mount_data *) raw_data)->version != SMB_MOUNT_VERSION)
334 goto out_wrong_data;
336 lock_super(sb);
338 sb->s_blocksize = 1024; /* Eh... Is this correct? */
339 sb->s_blocksize_bits = 10;
340 sb->s_magic = SMB_SUPER_MAGIC;
341 sb->s_flags = 0;
342 sb->s_op = &smb_sops;
344 sb->u.smbfs_sb.sock_file = NULL;
345 init_MUTEX(&sb->u.smbfs_sb.sem);
346 init_waitqueue_head(&sb->u.smbfs_sb.wait);
347 sb->u.smbfs_sb.conn_pid = 0;
348 sb->u.smbfs_sb.state = CONN_INVALID; /* no connection yet */
349 sb->u.smbfs_sb.generation = 0;
350 sb->u.smbfs_sb.packet_size = smb_round_length(SMB_INITIAL_PACKET_SIZE);
351 sb->u.smbfs_sb.packet = smb_vmalloc(sb->u.smbfs_sb.packet_size);
352 if (!sb->u.smbfs_sb.packet)
353 goto out_no_mem;
355 /* Allocate the global temp buffer */
356 sb->u.smbfs_sb.temp_buf = kmalloc(SMB_MAXPATHLEN + 20, GFP_KERNEL);
357 if (!sb->u.smbfs_sb.temp_buf)
358 goto out_no_temp;
360 /* Allocate the mount data structure */
361 mnt = kmalloc(sizeof(struct smb_mount_data), GFP_KERNEL);
362 if (!mnt)
363 goto out_no_mount;
364 *mnt = *((struct smb_mount_data *) raw_data);
365 /* ** temp ** pass config flags in file mode */
366 mnt->version = (mnt->file_mode >> 9);
367 mnt->file_mode &= (S_IRWXU | S_IRWXG | S_IRWXO);
368 mnt->file_mode |= S_IFREG;
369 mnt->dir_mode &= (S_IRWXU | S_IRWXG | S_IRWXO);
370 mnt->dir_mode |= S_IFDIR;
371 sb->u.smbfs_sb.mnt = mnt;
373 * Display the enabled options
375 if (mnt->version & SMB_FIX_OLDATTR)
376 printk("SMBFS: Using core getattr (Win 95 speedup)\n");
377 else if (mnt->version & SMB_FIX_DIRATTR)
378 printk("SMBFS: Using dir ff getattr\n");
381 * Keep the super block locked while we get the root inode.
383 smb_init_root_dirent(&(sb->u.smbfs_sb), &root);
384 root_inode = smb_iget(sb, &root);
385 if (!root_inode)
386 goto out_no_root;
388 sb->s_root = d_alloc_root(root_inode);
389 if (!sb->s_root)
390 goto out_no_root;
392 unlock_super(sb);
393 return sb;
395 out_no_root:
396 iput(root_inode);
397 kfree(sb->u.smbfs_sb.mnt);
398 out_no_mount:
399 kfree(sb->u.smbfs_sb.temp_buf);
400 out_no_temp:
401 smb_vfree(sb->u.smbfs_sb.packet);
402 out_no_mem:
403 printk(KERN_ERR "smb_read_super: allocation failure\n");
404 unlock_super(sb);
405 goto out_fail;
406 out_wrong_data:
407 printk(KERN_ERR "SMBFS: need mount version %d\n", SMB_MOUNT_VERSION);
408 goto out_fail;
409 out_no_data:
410 printk("smb_read_super: missing data argument\n");
411 out_fail:
412 sb->s_dev = 0;
413 MOD_DEC_USE_COUNT;
414 return NULL;
417 static int
418 smb_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
420 struct statfs attr;
422 memset(&attr, 0, sizeof(attr));
424 smb_proc_dskattr(sb, &attr);
426 attr.f_type = SMB_SUPER_MAGIC;
427 attr.f_files = -1;
428 attr.f_ffree = -1;
429 attr.f_namelen = SMB_MAXPATHLEN;
430 return copy_to_user(buf, &attr, bufsiz) ? -EFAULT : 0;
434 smb_notify_change(struct dentry *dentry, struct iattr *attr)
436 struct inode *inode = dentry->d_inode;
437 struct smb_sb_info *server = server_from_dentry(dentry);
438 unsigned int mask = (S_IFREG | S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO);
439 int error, changed, refresh = 0;
440 struct smb_fattr fattr;
442 error = smb_revalidate_inode(dentry);
443 if (error)
444 goto out;
446 if ((error = inode_change_ok(inode, attr)) < 0)
447 goto out;
449 error = -EPERM;
450 if ((attr->ia_valid & ATTR_UID) && (attr->ia_uid != server->mnt->uid))
451 goto out;
453 if ((attr->ia_valid & ATTR_GID) && (attr->ia_uid != server->mnt->gid))
454 goto out;
456 if ((attr->ia_valid & ATTR_MODE) && (attr->ia_mode & ~mask))
457 goto out;
459 if ((attr->ia_valid & ATTR_SIZE) != 0)
461 #ifdef SMBFS_DEBUG_VERBOSE
462 printk("smb_notify_change: changing %s/%s, old size=%ld, new size=%ld\n",
463 dentry->d_parent->d_name.name, dentry->d_name.name,
464 (long) inode->i_size, (long) attr->ia_size);
465 #endif
466 error = smb_open(dentry, O_WRONLY);
467 if (error)
468 goto out;
469 error = smb_proc_trunc(server, inode->u.smbfs_i.fileid,
470 attr->ia_size);
471 if (error)
472 goto out;
473 vmtruncate(inode, attr->ia_size);
474 refresh = 1;
478 * Initialize the fattr and check for changed fields.
479 * Note: CTIME under SMB is creation time rather than
480 * change time, so we don't attempt to change it.
482 smb_get_inode_attr(inode, &fattr);
484 changed = 0;
485 if ((attr->ia_valid & ATTR_MTIME) != 0)
487 fattr.f_mtime = attr->ia_mtime;
488 changed = 1;
490 if ((attr->ia_valid & ATTR_ATIME) != 0)
492 fattr.f_atime = attr->ia_atime;
493 /* Earlier protocols don't have an access time */
494 if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2)
495 changed = 1;
497 if (changed)
499 error = smb_proc_settime(dentry, &fattr);
500 if (error)
501 goto out;
502 refresh = 1;
506 * Check for mode changes ... we're extremely limited in
507 * what can be set for SMB servers: just the read-only bit.
509 if ((attr->ia_valid & ATTR_MODE) != 0)
511 #ifdef SMBFS_DEBUG_VERBOSE
512 printk("smb_notify_change: %s/%s mode change, old=%x, new=%lx\n",
513 dentry->d_parent->d_name.name, dentry->d_name.name, fattr.f_mode,attr->ia_mode);
514 #endif
515 changed = 0;
516 if (attr->ia_mode & S_IWUSR)
518 if (fattr.attr & aRONLY)
520 fattr.attr &= ~aRONLY;
521 changed = 1;
523 } else
525 if (!(fattr.attr & aRONLY))
527 fattr.attr |= aRONLY;
528 changed = 1;
531 if (changed)
533 error = smb_proc_setattr(dentry, &fattr);
534 if (error)
535 goto out;
536 refresh = 1;
539 error = 0;
541 out:
542 if (refresh)
543 smb_refresh_inode(dentry);
544 return error;
547 #ifdef DEBUG_SMB_MALLOC
548 int smb_malloced;
549 int smb_current_kmalloced;
550 int smb_current_vmalloced;
551 #endif
553 static struct file_system_type smb_fs_type = {
554 "smbfs",
555 0 /* FS_NO_DCACHE doesn't work correctly */,
556 smb_read_super,
557 NULL
560 int __init init_smb_fs(void)
562 return register_filesystem(&smb_fs_type);
565 #ifdef MODULE
566 EXPORT_NO_SYMBOLS;
569 init_module(void)
571 pr_debug("smbfs: init_module called\n");
573 #ifdef DEBUG_SMB_MALLOC
574 smb_malloced = 0;
575 smb_current_kmalloced = 0;
576 smb_current_vmalloced = 0;
577 #endif
579 return init_smb_fs();
582 void
583 cleanup_module(void)
585 pr_debug("smbfs: cleanup_module called\n");
586 unregister_filesystem(&smb_fs_type);
587 #ifdef DEBUG_SMB_MALLOC
588 printk(KERN_DEBUG "smb_malloced: %d\n", smb_malloced);
589 printk(KERN_DEBUG "smb_current_kmalloced: %d\n",smb_current_kmalloced);
590 printk(KERN_DEBUG "smb_current_vmalloced: %d\n",smb_current_vmalloced);
591 #endif
594 #endif