xen mmu: fix a race window causing leave_mm BUG()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / cifs / cifsfs.c
blobc0def4f5602a7cfdeea8f32c79abc88b7931a0cb
1 /*
2 * fs/cifs/cifsfs.c
4 * Copyright (C) International Business Machines Corp., 2002,2008
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Common Internet FileSystem (CIFS) client
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* Note that BB means BUGBUG (ie something to fix eventually) */
26 #include <linux/module.h>
27 #include <linux/fs.h>
28 #include <linux/mount.h>
29 #include <linux/slab.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/seq_file.h>
33 #include <linux/vfs.h>
34 #include <linux/mempool.h>
35 #include <linux/delay.h>
36 #include <linux/kthread.h>
37 #include <linux/freezer.h>
38 #include <net/ipv6.h>
39 #include "cifsfs.h"
40 #include "cifspdu.h"
41 #define DECLARE_GLOBALS_HERE
42 #include "cifsglob.h"
43 #include "cifsproto.h"
44 #include "cifs_debug.h"
45 #include "cifs_fs_sb.h"
46 #include <linux/mm.h>
47 #include <linux/key-type.h>
48 #include "cifs_spnego.h"
49 #include "fscache.h"
50 #define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDUs */
52 int cifsFYI = 0;
53 int cifsERROR = 1;
54 int traceSMB = 0;
55 unsigned int oplockEnabled = 1;
56 unsigned int experimEnabled = 0;
57 unsigned int linuxExtEnabled = 1;
58 unsigned int lookupCacheEnabled = 1;
59 unsigned int multiuser_mount = 0;
60 unsigned int global_secflags = CIFSSEC_DEF;
61 /* unsigned int ntlmv2_support = 0; */
62 unsigned int sign_CIFS_PDUs = 1;
63 static const struct super_operations cifs_super_ops;
64 unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
65 module_param(CIFSMaxBufSize, int, 0);
66 MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header). "
67 "Default: 16384 Range: 8192 to 130048");
68 unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
69 module_param(cifs_min_rcv, int, 0);
70 MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
71 "1 to 64");
72 unsigned int cifs_min_small = 30;
73 module_param(cifs_min_small, int, 0);
74 MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
75 "Range: 2 to 256");
76 unsigned int cifs_max_pending = CIFS_MAX_REQ;
77 module_param(cifs_max_pending, int, 0);
78 MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server. "
79 "Default: 50 Range: 2 to 256");
80 unsigned short echo_retries = 5;
81 module_param(echo_retries, ushort, 0644);
82 MODULE_PARM_DESC(echo_retries, "Number of echo attempts before giving up and "
83 "reconnecting server. Default: 5. 0 means "
84 "never reconnect.");
85 extern mempool_t *cifs_sm_req_poolp;
86 extern mempool_t *cifs_req_poolp;
87 extern mempool_t *cifs_mid_poolp;
89 void
90 cifs_sb_active(struct super_block *sb)
92 struct cifs_sb_info *server = CIFS_SB(sb);
94 if (atomic_inc_return(&server->active) == 1)
95 atomic_inc(&sb->s_active);
98 void
99 cifs_sb_deactive(struct super_block *sb)
101 struct cifs_sb_info *server = CIFS_SB(sb);
103 if (atomic_dec_and_test(&server->active))
104 deactivate_super(sb);
107 static int
108 cifs_read_super(struct super_block *sb, void *data,
109 const char *devname, int silent)
111 struct inode *inode;
112 struct cifs_sb_info *cifs_sb;
113 int rc = 0;
115 /* BB should we make this contingent on mount parm? */
116 sb->s_flags |= MS_NODIRATIME | MS_NOATIME;
117 sb->s_fs_info = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
118 cifs_sb = CIFS_SB(sb);
119 if (cifs_sb == NULL)
120 return -ENOMEM;
122 spin_lock_init(&cifs_sb->tlink_tree_lock);
123 cifs_sb->tlink_tree = RB_ROOT;
125 rc = bdi_setup_and_register(&cifs_sb->bdi, "cifs", BDI_CAP_MAP_COPY);
126 if (rc) {
127 kfree(cifs_sb);
128 return rc;
130 cifs_sb->bdi.ra_pages = default_backing_dev_info.ra_pages;
132 #ifdef CONFIG_CIFS_DFS_UPCALL
133 /* copy mount params to sb for use in submounts */
134 /* BB: should we move this after the mount so we
135 * do not have to do the copy on failed mounts?
136 * BB: May be it is better to do simple copy before
137 * complex operation (mount), and in case of fail
138 * just exit instead of doing mount and attempting
139 * undo it if this copy fails?*/
140 if (data) {
141 int len = strlen(data);
142 cifs_sb->mountdata = kzalloc(len + 1, GFP_KERNEL);
143 if (cifs_sb->mountdata == NULL) {
144 bdi_destroy(&cifs_sb->bdi);
145 kfree(sb->s_fs_info);
146 sb->s_fs_info = NULL;
147 return -ENOMEM;
149 strncpy(cifs_sb->mountdata, data, len + 1);
150 cifs_sb->mountdata[len] = '\0';
152 #endif
154 rc = cifs_mount(sb, cifs_sb, data, devname);
156 if (rc) {
157 if (!silent)
158 cERROR(1, "cifs_mount failed w/return code = %d", rc);
159 goto out_mount_failed;
162 sb->s_magic = CIFS_MAGIC_NUMBER;
163 sb->s_op = &cifs_super_ops;
164 sb->s_bdi = &cifs_sb->bdi;
165 sb->s_blocksize = CIFS_MAX_MSGSIZE;
166 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
167 inode = cifs_root_iget(sb, ROOT_I);
169 if (IS_ERR(inode)) {
170 rc = PTR_ERR(inode);
171 inode = NULL;
172 goto out_no_root;
175 sb->s_root = d_alloc_root(inode);
177 if (!sb->s_root) {
178 rc = -ENOMEM;
179 goto out_no_root;
182 /* do that *after* d_alloc_root() - we want NULL ->d_op for root here */
183 if (cifs_sb_master_tcon(cifs_sb)->nocase)
184 sb->s_d_op = &cifs_ci_dentry_ops;
185 else
186 sb->s_d_op = &cifs_dentry_ops;
188 #ifdef CONFIG_CIFS_EXPERIMENTAL
189 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
190 cFYI(1, "export ops supported");
191 sb->s_export_op = &cifs_export_ops;
193 #endif /* EXPERIMENTAL */
195 return 0;
197 out_no_root:
198 cERROR(1, "cifs_read_super: get root inode failed");
199 if (inode)
200 iput(inode);
202 cifs_umount(sb, cifs_sb);
204 out_mount_failed:
205 if (cifs_sb) {
206 #ifdef CONFIG_CIFS_DFS_UPCALL
207 if (cifs_sb->mountdata) {
208 kfree(cifs_sb->mountdata);
209 cifs_sb->mountdata = NULL;
211 #endif
212 unload_nls(cifs_sb->local_nls);
213 bdi_destroy(&cifs_sb->bdi);
214 kfree(cifs_sb);
216 return rc;
219 static void
220 cifs_put_super(struct super_block *sb)
222 int rc = 0;
223 struct cifs_sb_info *cifs_sb;
225 cFYI(1, "In cifs_put_super");
226 cifs_sb = CIFS_SB(sb);
227 if (cifs_sb == NULL) {
228 cFYI(1, "Empty cifs superblock info passed to unmount");
229 return;
232 rc = cifs_umount(sb, cifs_sb);
233 if (rc)
234 cERROR(1, "cifs_umount failed with return code %d", rc);
235 #ifdef CONFIG_CIFS_DFS_UPCALL
236 if (cifs_sb->mountdata) {
237 kfree(cifs_sb->mountdata);
238 cifs_sb->mountdata = NULL;
240 #endif
242 unload_nls(cifs_sb->local_nls);
243 bdi_destroy(&cifs_sb->bdi);
244 kfree(cifs_sb);
247 static int
248 cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
250 struct super_block *sb = dentry->d_sb;
251 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
252 struct cifsTconInfo *tcon = cifs_sb_master_tcon(cifs_sb);
253 int rc = -EOPNOTSUPP;
254 int xid;
256 xid = GetXid();
258 buf->f_type = CIFS_MAGIC_NUMBER;
261 * PATH_MAX may be too long - it would presumably be total path,
262 * but note that some servers (includinng Samba 3) have a shorter
263 * maximum path.
265 * Instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO.
267 buf->f_namelen = PATH_MAX;
268 buf->f_files = 0; /* undefined */
269 buf->f_ffree = 0; /* unlimited */
272 * We could add a second check for a QFS Unix capability bit
274 if ((tcon->ses->capabilities & CAP_UNIX) &&
275 (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
276 rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
279 * Only need to call the old QFSInfo if failed on newer one,
280 * e.g. by OS/2.
282 if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
283 rc = CIFSSMBQFSInfo(xid, tcon, buf);
286 * Some old Windows servers also do not support level 103, retry with
287 * older level one if old server failed the previous call or we
288 * bypassed it because we detected that this was an older LANMAN sess
290 if (rc)
291 rc = SMBOldQFSInfo(xid, tcon, buf);
293 FreeXid(xid);
294 return 0;
297 static int cifs_permission(struct inode *inode, int mask, unsigned int flags)
299 struct cifs_sb_info *cifs_sb;
301 if (flags & IPERM_FLAG_RCU)
302 return -ECHILD;
304 cifs_sb = CIFS_SB(inode->i_sb);
306 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
307 if ((mask & MAY_EXEC) && !execute_ok(inode))
308 return -EACCES;
309 else
310 return 0;
311 } else /* file mode might have been restricted at mount time
312 on the client (above and beyond ACL on servers) for
313 servers which do not support setting and viewing mode bits,
314 so allowing client to check permissions is useful */
315 return generic_permission(inode, mask, flags, NULL);
318 static struct kmem_cache *cifs_inode_cachep;
319 static struct kmem_cache *cifs_req_cachep;
320 static struct kmem_cache *cifs_mid_cachep;
321 static struct kmem_cache *cifs_sm_req_cachep;
322 mempool_t *cifs_sm_req_poolp;
323 mempool_t *cifs_req_poolp;
324 mempool_t *cifs_mid_poolp;
326 static struct inode *
327 cifs_alloc_inode(struct super_block *sb)
329 struct cifsInodeInfo *cifs_inode;
330 cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
331 if (!cifs_inode)
332 return NULL;
333 cifs_inode->cifsAttrs = 0x20; /* default */
334 cifs_inode->time = 0;
335 /* Until the file is open and we have gotten oplock
336 info back from the server, can not assume caching of
337 file data or metadata */
338 cifs_set_oplock_level(cifs_inode, 0);
339 cifs_inode->delete_pending = false;
340 cifs_inode->invalid_mapping = false;
341 cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
342 cifs_inode->server_eof = 0;
343 cifs_inode->uniqueid = 0;
344 cifs_inode->createtime = 0;
346 /* Can not set i_flags here - they get immediately overwritten
347 to zero by the VFS */
348 /* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;*/
349 INIT_LIST_HEAD(&cifs_inode->openFileList);
350 return &cifs_inode->vfs_inode;
353 static void cifs_i_callback(struct rcu_head *head)
355 struct inode *inode = container_of(head, struct inode, i_rcu);
356 INIT_LIST_HEAD(&inode->i_dentry);
357 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
360 static void
361 cifs_destroy_inode(struct inode *inode)
363 call_rcu(&inode->i_rcu, cifs_i_callback);
366 static void
367 cifs_evict_inode(struct inode *inode)
369 truncate_inode_pages(&inode->i_data, 0);
370 end_writeback(inode);
371 cifs_fscache_release_inode_cookie(inode);
374 static void
375 cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
377 struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
378 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
380 seq_printf(s, ",addr=");
382 switch (server->dstaddr.ss_family) {
383 case AF_INET:
384 seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
385 break;
386 case AF_INET6:
387 seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
388 if (sa6->sin6_scope_id)
389 seq_printf(s, "%%%u", sa6->sin6_scope_id);
390 break;
391 default:
392 seq_printf(s, "(unknown)");
397 * cifs_show_options() is for displaying mount options in /proc/mounts.
398 * Not all settable options are displayed but most of the important
399 * ones are.
401 static int
402 cifs_show_options(struct seq_file *s, struct vfsmount *m)
404 struct cifs_sb_info *cifs_sb = CIFS_SB(m->mnt_sb);
405 struct cifsTconInfo *tcon = cifs_sb_master_tcon(cifs_sb);
406 struct sockaddr *srcaddr;
407 srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
409 seq_printf(s, ",unc=%s", tcon->treeName);
411 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)
412 seq_printf(s, ",multiuser");
413 else if (tcon->ses->userName)
414 seq_printf(s, ",username=%s", tcon->ses->userName);
416 if (tcon->ses->domainName)
417 seq_printf(s, ",domain=%s", tcon->ses->domainName);
419 if (srcaddr->sa_family != AF_UNSPEC) {
420 struct sockaddr_in *saddr4;
421 struct sockaddr_in6 *saddr6;
422 saddr4 = (struct sockaddr_in *)srcaddr;
423 saddr6 = (struct sockaddr_in6 *)srcaddr;
424 if (srcaddr->sa_family == AF_INET6)
425 seq_printf(s, ",srcaddr=%pI6c",
426 &saddr6->sin6_addr);
427 else if (srcaddr->sa_family == AF_INET)
428 seq_printf(s, ",srcaddr=%pI4",
429 &saddr4->sin_addr.s_addr);
430 else
431 seq_printf(s, ",srcaddr=BAD-AF:%i",
432 (int)(srcaddr->sa_family));
435 seq_printf(s, ",uid=%d", cifs_sb->mnt_uid);
436 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
437 seq_printf(s, ",forceuid");
438 else
439 seq_printf(s, ",noforceuid");
441 seq_printf(s, ",gid=%d", cifs_sb->mnt_gid);
442 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
443 seq_printf(s, ",forcegid");
444 else
445 seq_printf(s, ",noforcegid");
447 cifs_show_address(s, tcon->ses->server);
449 if (!tcon->unix_ext)
450 seq_printf(s, ",file_mode=0%o,dir_mode=0%o",
451 cifs_sb->mnt_file_mode,
452 cifs_sb->mnt_dir_mode);
453 if (tcon->seal)
454 seq_printf(s, ",seal");
455 if (tcon->nocase)
456 seq_printf(s, ",nocase");
457 if (tcon->retry)
458 seq_printf(s, ",hard");
459 if (cifs_sb->prepath)
460 seq_printf(s, ",prepath=%s", cifs_sb->prepath);
461 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
462 seq_printf(s, ",posixpaths");
463 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
464 seq_printf(s, ",setuids");
465 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
466 seq_printf(s, ",serverino");
467 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
468 seq_printf(s, ",directio");
469 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
470 seq_printf(s, ",nouser_xattr");
471 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
472 seq_printf(s, ",mapchars");
473 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
474 seq_printf(s, ",sfu");
475 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
476 seq_printf(s, ",nobrl");
477 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
478 seq_printf(s, ",cifsacl");
479 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
480 seq_printf(s, ",dynperm");
481 if (m->mnt_sb->s_flags & MS_POSIXACL)
482 seq_printf(s, ",acl");
483 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
484 seq_printf(s, ",mfsymlinks");
485 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
486 seq_printf(s, ",fsc");
488 seq_printf(s, ",rsize=%d", cifs_sb->rsize);
489 seq_printf(s, ",wsize=%d", cifs_sb->wsize);
490 /* convert actimeo and display it in seconds */
491 seq_printf(s, ",actimeo=%lu", cifs_sb->actimeo / HZ);
493 return 0;
496 static void cifs_umount_begin(struct super_block *sb)
498 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
499 struct cifsTconInfo *tcon;
501 if (cifs_sb == NULL)
502 return;
504 tcon = cifs_sb_master_tcon(cifs_sb);
506 spin_lock(&cifs_tcp_ses_lock);
507 if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
508 /* we have other mounts to same share or we have
509 already tried to force umount this and woken up
510 all waiting network requests, nothing to do */
511 spin_unlock(&cifs_tcp_ses_lock);
512 return;
513 } else if (tcon->tc_count == 1)
514 tcon->tidStatus = CifsExiting;
515 spin_unlock(&cifs_tcp_ses_lock);
517 /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
518 /* cancel_notify_requests(tcon); */
519 if (tcon->ses && tcon->ses->server) {
520 cFYI(1, "wake up tasks now - umount begin not complete");
521 wake_up_all(&tcon->ses->server->request_q);
522 wake_up_all(&tcon->ses->server->response_q);
523 msleep(1); /* yield */
524 /* we have to kick the requests once more */
525 wake_up_all(&tcon->ses->server->response_q);
526 msleep(1);
529 return;
532 #ifdef CONFIG_CIFS_STATS2
533 static int cifs_show_stats(struct seq_file *s, struct vfsmount *mnt)
535 /* BB FIXME */
536 return 0;
538 #endif
540 static int cifs_remount(struct super_block *sb, int *flags, char *data)
542 *flags |= MS_NODIRATIME;
543 return 0;
546 static int cifs_drop_inode(struct inode *inode)
548 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
550 /* no serverino => unconditional eviction */
551 return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
552 generic_drop_inode(inode);
555 static const struct super_operations cifs_super_ops = {
556 .put_super = cifs_put_super,
557 .statfs = cifs_statfs,
558 .alloc_inode = cifs_alloc_inode,
559 .destroy_inode = cifs_destroy_inode,
560 .drop_inode = cifs_drop_inode,
561 .evict_inode = cifs_evict_inode,
562 /* .delete_inode = cifs_delete_inode, */ /* Do not need above
563 function unless later we add lazy close of inodes or unless the
564 kernel forgets to call us with the same number of releases (closes)
565 as opens */
566 .show_options = cifs_show_options,
567 .umount_begin = cifs_umount_begin,
568 .remount_fs = cifs_remount,
569 #ifdef CONFIG_CIFS_STATS2
570 .show_stats = cifs_show_stats,
571 #endif
574 static struct dentry *
575 cifs_do_mount(struct file_system_type *fs_type,
576 int flags, const char *dev_name, void *data)
578 int rc;
579 struct super_block *sb;
581 sb = sget(fs_type, NULL, set_anon_super, NULL);
583 cFYI(1, "Devname: %s flags: %d ", dev_name, flags);
585 if (IS_ERR(sb))
586 return ERR_CAST(sb);
588 sb->s_flags = flags;
590 rc = cifs_read_super(sb, data, dev_name, flags & MS_SILENT ? 1 : 0);
591 if (rc) {
592 deactivate_locked_super(sb);
593 return ERR_PTR(rc);
595 sb->s_flags |= MS_ACTIVE;
596 return dget(sb->s_root);
599 static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
600 unsigned long nr_segs, loff_t pos)
602 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
603 ssize_t written;
604 int rc;
606 written = generic_file_aio_write(iocb, iov, nr_segs, pos);
608 if (CIFS_I(inode)->clientCanCacheAll)
609 return written;
611 rc = filemap_fdatawrite(inode->i_mapping);
612 if (rc)
613 cFYI(1, "cifs_file_aio_write: %d rc on %p inode", rc, inode);
615 return written;
618 static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
620 /* origin == SEEK_END => we must revalidate the cached file length */
621 if (origin == SEEK_END) {
622 int retval;
624 /* some applications poll for the file length in this strange
625 way so we must seek to end on non-oplocked files by
626 setting the revalidate time to zero */
627 CIFS_I(file->f_path.dentry->d_inode)->time = 0;
629 retval = cifs_revalidate_file(file);
630 if (retval < 0)
631 return (loff_t)retval;
633 return generic_file_llseek_unlocked(file, offset, origin);
636 static int cifs_setlease(struct file *file, long arg, struct file_lock **lease)
638 /* note that this is called by vfs setlease with lock_flocks held
639 to protect *lease from going away */
640 struct inode *inode = file->f_path.dentry->d_inode;
641 struct cifsFileInfo *cfile = file->private_data;
643 if (!(S_ISREG(inode->i_mode)))
644 return -EINVAL;
646 /* check if file is oplocked */
647 if (((arg == F_RDLCK) &&
648 (CIFS_I(inode)->clientCanCacheRead)) ||
649 ((arg == F_WRLCK) &&
650 (CIFS_I(inode)->clientCanCacheAll)))
651 return generic_setlease(file, arg, lease);
652 else if (tlink_tcon(cfile->tlink)->local_lease &&
653 !CIFS_I(inode)->clientCanCacheRead)
654 /* If the server claims to support oplock on this
655 file, then we still need to check oplock even
656 if the local_lease mount option is set, but there
657 are servers which do not support oplock for which
658 this mount option may be useful if the user
659 knows that the file won't be changed on the server
660 by anyone else */
661 return generic_setlease(file, arg, lease);
662 else
663 return -EAGAIN;
666 struct file_system_type cifs_fs_type = {
667 .owner = THIS_MODULE,
668 .name = "cifs",
669 .mount = cifs_do_mount,
670 .kill_sb = kill_anon_super,
671 /* .fs_flags */
673 const struct inode_operations cifs_dir_inode_ops = {
674 .create = cifs_create,
675 .lookup = cifs_lookup,
676 .getattr = cifs_getattr,
677 .unlink = cifs_unlink,
678 .link = cifs_hardlink,
679 .mkdir = cifs_mkdir,
680 .rmdir = cifs_rmdir,
681 .rename = cifs_rename,
682 .permission = cifs_permission,
683 /* revalidate:cifs_revalidate, */
684 .setattr = cifs_setattr,
685 .symlink = cifs_symlink,
686 .mknod = cifs_mknod,
687 #ifdef CONFIG_CIFS_XATTR
688 .setxattr = cifs_setxattr,
689 .getxattr = cifs_getxattr,
690 .listxattr = cifs_listxattr,
691 .removexattr = cifs_removexattr,
692 #endif
695 const struct inode_operations cifs_file_inode_ops = {
696 /* revalidate:cifs_revalidate, */
697 .setattr = cifs_setattr,
698 .getattr = cifs_getattr, /* do we need this anymore? */
699 .rename = cifs_rename,
700 .permission = cifs_permission,
701 #ifdef CONFIG_CIFS_XATTR
702 .setxattr = cifs_setxattr,
703 .getxattr = cifs_getxattr,
704 .listxattr = cifs_listxattr,
705 .removexattr = cifs_removexattr,
706 #endif
709 const struct inode_operations cifs_symlink_inode_ops = {
710 .readlink = generic_readlink,
711 .follow_link = cifs_follow_link,
712 .put_link = cifs_put_link,
713 .permission = cifs_permission,
714 /* BB add the following two eventually */
715 /* revalidate: cifs_revalidate,
716 setattr: cifs_notify_change, *//* BB do we need notify change */
717 #ifdef CONFIG_CIFS_XATTR
718 .setxattr = cifs_setxattr,
719 .getxattr = cifs_getxattr,
720 .listxattr = cifs_listxattr,
721 .removexattr = cifs_removexattr,
722 #endif
725 const struct file_operations cifs_file_ops = {
726 .read = do_sync_read,
727 .write = do_sync_write,
728 .aio_read = generic_file_aio_read,
729 .aio_write = cifs_file_aio_write,
730 .open = cifs_open,
731 .release = cifs_close,
732 .lock = cifs_lock,
733 .fsync = cifs_fsync,
734 .flush = cifs_flush,
735 .mmap = cifs_file_mmap,
736 .splice_read = generic_file_splice_read,
737 .llseek = cifs_llseek,
738 #ifdef CONFIG_CIFS_POSIX
739 .unlocked_ioctl = cifs_ioctl,
740 #endif /* CONFIG_CIFS_POSIX */
741 .setlease = cifs_setlease,
744 const struct file_operations cifs_file_strict_ops = {
745 .read = do_sync_read,
746 .write = do_sync_write,
747 .aio_read = cifs_strict_readv,
748 .aio_write = cifs_strict_writev,
749 .open = cifs_open,
750 .release = cifs_close,
751 .lock = cifs_lock,
752 .fsync = cifs_strict_fsync,
753 .flush = cifs_flush,
754 .mmap = cifs_file_strict_mmap,
755 .splice_read = generic_file_splice_read,
756 .llseek = cifs_llseek,
757 #ifdef CONFIG_CIFS_POSIX
758 .unlocked_ioctl = cifs_ioctl,
759 #endif /* CONFIG_CIFS_POSIX */
760 .setlease = cifs_setlease,
763 const struct file_operations cifs_file_direct_ops = {
764 /* no aio, no readv -
765 BB reevaluate whether they can be done with directio, no cache */
766 .read = cifs_user_read,
767 .write = cifs_user_write,
768 .open = cifs_open,
769 .release = cifs_close,
770 .lock = cifs_lock,
771 .fsync = cifs_fsync,
772 .flush = cifs_flush,
773 .mmap = cifs_file_mmap,
774 .splice_read = generic_file_splice_read,
775 #ifdef CONFIG_CIFS_POSIX
776 .unlocked_ioctl = cifs_ioctl,
777 #endif /* CONFIG_CIFS_POSIX */
778 .llseek = cifs_llseek,
779 .setlease = cifs_setlease,
782 const struct file_operations cifs_file_nobrl_ops = {
783 .read = do_sync_read,
784 .write = do_sync_write,
785 .aio_read = generic_file_aio_read,
786 .aio_write = cifs_file_aio_write,
787 .open = cifs_open,
788 .release = cifs_close,
789 .fsync = cifs_fsync,
790 .flush = cifs_flush,
791 .mmap = cifs_file_mmap,
792 .splice_read = generic_file_splice_read,
793 .llseek = cifs_llseek,
794 #ifdef CONFIG_CIFS_POSIX
795 .unlocked_ioctl = cifs_ioctl,
796 #endif /* CONFIG_CIFS_POSIX */
797 .setlease = cifs_setlease,
800 const struct file_operations cifs_file_strict_nobrl_ops = {
801 .read = do_sync_read,
802 .write = do_sync_write,
803 .aio_read = cifs_strict_readv,
804 .aio_write = cifs_strict_writev,
805 .open = cifs_open,
806 .release = cifs_close,
807 .fsync = cifs_strict_fsync,
808 .flush = cifs_flush,
809 .mmap = cifs_file_strict_mmap,
810 .splice_read = generic_file_splice_read,
811 .llseek = cifs_llseek,
812 #ifdef CONFIG_CIFS_POSIX
813 .unlocked_ioctl = cifs_ioctl,
814 #endif /* CONFIG_CIFS_POSIX */
815 .setlease = cifs_setlease,
818 const struct file_operations cifs_file_direct_nobrl_ops = {
819 /* no mmap, no aio, no readv -
820 BB reevaluate whether they can be done with directio, no cache */
821 .read = cifs_user_read,
822 .write = cifs_user_write,
823 .open = cifs_open,
824 .release = cifs_close,
825 .fsync = cifs_fsync,
826 .flush = cifs_flush,
827 .mmap = cifs_file_mmap,
828 .splice_read = generic_file_splice_read,
829 #ifdef CONFIG_CIFS_POSIX
830 .unlocked_ioctl = cifs_ioctl,
831 #endif /* CONFIG_CIFS_POSIX */
832 .llseek = cifs_llseek,
833 .setlease = cifs_setlease,
836 const struct file_operations cifs_dir_ops = {
837 .readdir = cifs_readdir,
838 .release = cifs_closedir,
839 .read = generic_read_dir,
840 .unlocked_ioctl = cifs_ioctl,
841 .llseek = generic_file_llseek,
844 static void
845 cifs_init_once(void *inode)
847 struct cifsInodeInfo *cifsi = inode;
849 inode_init_once(&cifsi->vfs_inode);
850 INIT_LIST_HEAD(&cifsi->lockList);
853 static int
854 cifs_init_inodecache(void)
856 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
857 sizeof(struct cifsInodeInfo),
858 0, (SLAB_RECLAIM_ACCOUNT|
859 SLAB_MEM_SPREAD),
860 cifs_init_once);
861 if (cifs_inode_cachep == NULL)
862 return -ENOMEM;
864 return 0;
867 static void
868 cifs_destroy_inodecache(void)
870 kmem_cache_destroy(cifs_inode_cachep);
873 static int
874 cifs_init_request_bufs(void)
876 if (CIFSMaxBufSize < 8192) {
877 /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
878 Unicode path name has to fit in any SMB/CIFS path based frames */
879 CIFSMaxBufSize = 8192;
880 } else if (CIFSMaxBufSize > 1024*127) {
881 CIFSMaxBufSize = 1024 * 127;
882 } else {
883 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
885 /* cERROR(1, "CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize); */
886 cifs_req_cachep = kmem_cache_create("cifs_request",
887 CIFSMaxBufSize +
888 MAX_CIFS_HDR_SIZE, 0,
889 SLAB_HWCACHE_ALIGN, NULL);
890 if (cifs_req_cachep == NULL)
891 return -ENOMEM;
893 if (cifs_min_rcv < 1)
894 cifs_min_rcv = 1;
895 else if (cifs_min_rcv > 64) {
896 cifs_min_rcv = 64;
897 cERROR(1, "cifs_min_rcv set to maximum (64)");
900 cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
901 cifs_req_cachep);
903 if (cifs_req_poolp == NULL) {
904 kmem_cache_destroy(cifs_req_cachep);
905 return -ENOMEM;
907 /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
908 almost all handle based requests (but not write response, nor is it
909 sufficient for path based requests). A smaller size would have
910 been more efficient (compacting multiple slab items on one 4k page)
911 for the case in which debug was on, but this larger size allows
912 more SMBs to use small buffer alloc and is still much more
913 efficient to alloc 1 per page off the slab compared to 17K (5page)
914 alloc of large cifs buffers even when page debugging is on */
915 cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
916 MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
917 NULL);
918 if (cifs_sm_req_cachep == NULL) {
919 mempool_destroy(cifs_req_poolp);
920 kmem_cache_destroy(cifs_req_cachep);
921 return -ENOMEM;
924 if (cifs_min_small < 2)
925 cifs_min_small = 2;
926 else if (cifs_min_small > 256) {
927 cifs_min_small = 256;
928 cFYI(1, "cifs_min_small set to maximum (256)");
931 cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
932 cifs_sm_req_cachep);
934 if (cifs_sm_req_poolp == NULL) {
935 mempool_destroy(cifs_req_poolp);
936 kmem_cache_destroy(cifs_req_cachep);
937 kmem_cache_destroy(cifs_sm_req_cachep);
938 return -ENOMEM;
941 return 0;
944 static void
945 cifs_destroy_request_bufs(void)
947 mempool_destroy(cifs_req_poolp);
948 kmem_cache_destroy(cifs_req_cachep);
949 mempool_destroy(cifs_sm_req_poolp);
950 kmem_cache_destroy(cifs_sm_req_cachep);
953 static int
954 cifs_init_mids(void)
956 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
957 sizeof(struct mid_q_entry), 0,
958 SLAB_HWCACHE_ALIGN, NULL);
959 if (cifs_mid_cachep == NULL)
960 return -ENOMEM;
962 /* 3 is a reasonable minimum number of simultaneous operations */
963 cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
964 if (cifs_mid_poolp == NULL) {
965 kmem_cache_destroy(cifs_mid_cachep);
966 return -ENOMEM;
969 return 0;
972 static void
973 cifs_destroy_mids(void)
975 mempool_destroy(cifs_mid_poolp);
976 kmem_cache_destroy(cifs_mid_cachep);
979 static int __init
980 init_cifs(void)
982 int rc = 0;
983 cifs_proc_init();
984 INIT_LIST_HEAD(&cifs_tcp_ses_list);
985 #ifdef CONFIG_CIFS_EXPERIMENTAL
986 INIT_LIST_HEAD(&GlobalDnotifyReqList);
987 INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
988 #endif
990 * Initialize Global counters
992 atomic_set(&sesInfoAllocCount, 0);
993 atomic_set(&tconInfoAllocCount, 0);
994 atomic_set(&tcpSesAllocCount, 0);
995 atomic_set(&tcpSesReconnectCount, 0);
996 atomic_set(&tconInfoReconnectCount, 0);
998 atomic_set(&bufAllocCount, 0);
999 atomic_set(&smBufAllocCount, 0);
1000 #ifdef CONFIG_CIFS_STATS2
1001 atomic_set(&totBufAllocCount, 0);
1002 atomic_set(&totSmBufAllocCount, 0);
1003 #endif /* CONFIG_CIFS_STATS2 */
1005 atomic_set(&midCount, 0);
1006 GlobalCurrentXid = 0;
1007 GlobalTotalActiveXid = 0;
1008 GlobalMaxActiveXid = 0;
1009 spin_lock_init(&cifs_tcp_ses_lock);
1010 spin_lock_init(&cifs_file_list_lock);
1011 spin_lock_init(&GlobalMid_Lock);
1013 if (cifs_max_pending < 2) {
1014 cifs_max_pending = 2;
1015 cFYI(1, "cifs_max_pending set to min of 2");
1016 } else if (cifs_max_pending > 256) {
1017 cifs_max_pending = 256;
1018 cFYI(1, "cifs_max_pending set to max of 256");
1021 rc = cifs_fscache_register();
1022 if (rc)
1023 goto out_clean_proc;
1025 rc = cifs_init_inodecache();
1026 if (rc)
1027 goto out_unreg_fscache;
1029 rc = cifs_init_mids();
1030 if (rc)
1031 goto out_destroy_inodecache;
1033 rc = cifs_init_request_bufs();
1034 if (rc)
1035 goto out_destroy_mids;
1037 rc = register_filesystem(&cifs_fs_type);
1038 if (rc)
1039 goto out_destroy_request_bufs;
1040 #ifdef CONFIG_CIFS_UPCALL
1041 rc = register_key_type(&cifs_spnego_key_type);
1042 if (rc)
1043 goto out_unregister_filesystem;
1044 #endif
1046 return 0;
1048 #ifdef CONFIG_CIFS_UPCALL
1049 out_unregister_filesystem:
1050 unregister_filesystem(&cifs_fs_type);
1051 #endif
1052 out_destroy_request_bufs:
1053 cifs_destroy_request_bufs();
1054 out_destroy_mids:
1055 cifs_destroy_mids();
1056 out_destroy_inodecache:
1057 cifs_destroy_inodecache();
1058 out_unreg_fscache:
1059 cifs_fscache_unregister();
1060 out_clean_proc:
1061 cifs_proc_clean();
1062 return rc;
1065 static void __exit
1066 exit_cifs(void)
1068 cFYI(DBG2, "exit_cifs");
1069 cifs_proc_clean();
1070 cifs_fscache_unregister();
1071 #ifdef CONFIG_CIFS_DFS_UPCALL
1072 cifs_dfs_release_automount_timer();
1073 #endif
1074 #ifdef CONFIG_CIFS_UPCALL
1075 unregister_key_type(&cifs_spnego_key_type);
1076 #endif
1077 unregister_filesystem(&cifs_fs_type);
1078 cifs_destroy_inodecache();
1079 cifs_destroy_mids();
1080 cifs_destroy_request_bufs();
1083 MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
1084 MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
1085 MODULE_DESCRIPTION
1086 ("VFS to access servers complying with the SNIA CIFS Specification "
1087 "e.g. Samba and Windows");
1088 MODULE_VERSION(CIFS_VERSION);
1089 module_init(init_cifs)
1090 module_exit(exit_cifs)