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