]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/cifs/cifsfs.c
Merge branch 'sched-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[net-next-2.6.git] / fs / cifs / cifsfs.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/cifsfs.c
3 *
2b280fab 4 * Copyright (C) International Business Machines Corp., 2002,2008
1da177e4
LT
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * Common Internet FileSystem (CIFS) client
8 *
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.
13 *
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.
18 *
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
22 */
23
24/* Note that BB means BUGBUG (ie something to fix eventually) */
25
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>
6ab16d24 35#include <linux/delay.h>
45af7a0f 36#include <linux/kthread.h>
7dfb7103 37#include <linux/freezer.h>
67e55205 38#include <linux/smp_lock.h>
1da177e4
LT
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>
84a15b93 47#include <linux/key-type.h>
6103335d 48#include "dns_resolve.h"
e545937a 49#include "cifs_spnego.h"
1da177e4
LT
50#define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDUs */
51
1da177e4
LT
52int cifsFYI = 0;
53int cifsERROR = 1;
54int traceSMB = 0;
55unsigned int oplockEnabled = 1;
56unsigned int experimEnabled = 0;
57unsigned int linuxExtEnabled = 1;
58unsigned int lookupCacheEnabled = 1;
59unsigned int multiuser_mount = 0;
04912d6a 60unsigned int global_secflags = CIFSSEC_DEF;
3979877e 61/* unsigned int ntlmv2_support = 0; */
1da177e4 62unsigned int sign_CIFS_PDUs = 1;
ee9b6d61 63static const struct super_operations cifs_super_ops;
1da177e4
LT
64unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
65module_param(CIFSMaxBufSize, int, 0);
63135e08
SF
66MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header). "
67 "Default: 16384 Range: 8192 to 130048");
1da177e4
LT
68unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
69module_param(cifs_min_rcv, int, 0);
63135e08
SF
70MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
71 "1 to 64");
1da177e4
LT
72unsigned int cifs_min_small = 30;
73module_param(cifs_min_small, int, 0);
63135e08
SF
74MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
75 "Range: 2 to 256");
1da177e4
LT
76unsigned int cifs_max_pending = CIFS_MAX_REQ;
77module_param(cifs_max_pending, int, 0);
63135e08
SF
78MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server. "
79 "Default: 50 Range: 2 to 256");
1da177e4 80
1da177e4
LT
81extern mempool_t *cifs_sm_req_poolp;
82extern mempool_t *cifs_req_poolp;
83extern mempool_t *cifs_mid_poolp;
84
1da177e4
LT
85static int
86cifs_read_super(struct super_block *sb, void *data,
87 const char *devname, int silent)
88{
89 struct inode *inode;
90 struct cifs_sb_info *cifs_sb;
91 int rc = 0;
50c2f753 92
1b2b2126
SF
93 /* BB should we make this contingent on mount parm? */
94 sb->s_flags |= MS_NODIRATIME | MS_NOATIME;
790fe579 95 sb->s_fs_info = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
1da177e4 96 cifs_sb = CIFS_SB(sb);
4523cc30 97 if (cifs_sb == NULL)
1da177e4 98 return -ENOMEM;
1da177e4 99
8044f7f4
JA
100 rc = bdi_setup_and_register(&cifs_sb->bdi, "cifs", BDI_CAP_MAP_COPY);
101 if (rc) {
102 kfree(cifs_sb);
103 return rc;
104 }
105
e6ab1582
IM
106#ifdef CONFIG_CIFS_DFS_UPCALL
107 /* copy mount params to sb for use in submounts */
108 /* BB: should we move this after the mount so we
109 * do not have to do the copy on failed mounts?
110 * BB: May be it is better to do simple copy before
111 * complex operation (mount), and in case of fail
112 * just exit instead of doing mount and attempting
113 * undo it if this copy fails?*/
79ee9a8b
SF
114 if (data) {
115 int len = strlen(data);
116 cifs_sb->mountdata = kzalloc(len + 1, GFP_KERNEL);
117 if (cifs_sb->mountdata == NULL) {
8044f7f4 118 bdi_destroy(&cifs_sb->bdi);
79ee9a8b
SF
119 kfree(sb->s_fs_info);
120 sb->s_fs_info = NULL;
121 return -ENOMEM;
122 }
123 strncpy(cifs_sb->mountdata, data, len + 1);
124 cifs_sb->mountdata[len] = '\0';
e6ab1582 125 }
e6ab1582
IM
126#endif
127
1da177e4
LT
128 rc = cifs_mount(sb, cifs_sb, data, devname);
129
130 if (rc) {
131 if (!silent)
b6b38f70 132 cERROR(1, "cifs_mount failed w/return code = %d", rc);
1da177e4
LT
133 goto out_mount_failed;
134 }
135
136 sb->s_magic = CIFS_MAGIC_NUMBER;
137 sb->s_op = &cifs_super_ops;
8044f7f4 138 sb->s_bdi = &cifs_sb->bdi;
4523cc30 139/* if (cifs_sb->tcon->ses->server->maxBuf > MAX_CIFS_HDR_SIZE + 512)
790fe579
SF
140 sb->s_blocksize =
141 cifs_sb->tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE; */
1da177e4
LT
142 sb->s_blocksize = CIFS_MAX_MSGSIZE;
143 sb->s_blocksize_bits = 14; /* default 2**14 = CIFS_MAX_MSGSIZE */
bd433d4c 144 inode = cifs_root_iget(sb, ROOT_I);
1da177e4 145
ce634ab2
DH
146 if (IS_ERR(inode)) {
147 rc = PTR_ERR(inode);
148 inode = NULL;
1da177e4
LT
149 goto out_no_root;
150 }
151
152 sb->s_root = d_alloc_root(inode);
153
154 if (!sb->s_root) {
155 rc = -ENOMEM;
156 goto out_no_root;
157 }
50c2f753 158
7521a3c5
SF
159#ifdef CONFIG_CIFS_EXPERIMENTAL
160 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
b6b38f70 161 cFYI(1, "export ops supported");
7521a3c5
SF
162 sb->s_export_op = &cifs_export_ops;
163 }
164#endif /* EXPERIMENTAL */
1da177e4
LT
165
166 return 0;
167
168out_no_root:
b6b38f70 169 cERROR(1, "cifs_read_super: get root inode failed");
1da177e4
LT
170 if (inode)
171 iput(inode);
54b4602d 172
2c731afb 173 cifs_umount(sb, cifs_sb);
1da177e4
LT
174
175out_mount_failed:
4523cc30 176 if (cifs_sb) {
e6ab1582
IM
177#ifdef CONFIG_CIFS_DFS_UPCALL
178 if (cifs_sb->mountdata) {
179 kfree(cifs_sb->mountdata);
180 cifs_sb->mountdata = NULL;
181 }
182#endif
6d729e44 183 unload_nls(cifs_sb->local_nls);
8044f7f4 184 bdi_destroy(&cifs_sb->bdi);
1da177e4
LT
185 kfree(cifs_sb);
186 }
187 return rc;
188}
189
190static void
191cifs_put_super(struct super_block *sb)
192{
193 int rc = 0;
194 struct cifs_sb_info *cifs_sb;
195
b6b38f70 196 cFYI(1, "In cifs_put_super");
1da177e4 197 cifs_sb = CIFS_SB(sb);
4523cc30 198 if (cifs_sb == NULL) {
b6b38f70 199 cFYI(1, "Empty cifs superblock info passed to unmount");
1da177e4
LT
200 return;
201 }
6cfd0148
CH
202
203 lock_kernel();
204
790fe579 205 rc = cifs_umount(sb, cifs_sb);
ad7a2926 206 if (rc)
b6b38f70 207 cERROR(1, "cifs_umount failed with return code %d", rc);
e6ab1582
IM
208#ifdef CONFIG_CIFS_DFS_UPCALL
209 if (cifs_sb->mountdata) {
210 kfree(cifs_sb->mountdata);
211 cifs_sb->mountdata = NULL;
212 }
213#endif
214
1da177e4 215 unload_nls(cifs_sb->local_nls);
8044f7f4 216 bdi_destroy(&cifs_sb->bdi);
1da177e4 217 kfree(cifs_sb);
6cfd0148
CH
218
219 unlock_kernel();
1da177e4
LT
220}
221
222static int
726c3342 223cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 224{
726c3342 225 struct super_block *sb = dentry->d_sb;
39da9847
SF
226 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
227 struct cifsTconInfo *tcon = cifs_sb->tcon;
c81156dd 228 int rc = -EOPNOTSUPP;
39da9847 229 int xid;
1da177e4
LT
230
231 xid = GetXid();
232
1da177e4
LT
233 buf->f_type = CIFS_MAGIC_NUMBER;
234
39da9847
SF
235 /*
236 * PATH_MAX may be too long - it would presumably be total path,
237 * but note that some servers (includinng Samba 3) have a shorter
238 * maximum path.
239 *
240 * Instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO.
241 */
242 buf->f_namelen = PATH_MAX;
1da177e4
LT
243 buf->f_files = 0; /* undefined */
244 buf->f_ffree = 0; /* unlimited */
245
39da9847
SF
246 /*
247 * We could add a second check for a QFS Unix capability bit
248 */
249 if ((tcon->ses->capabilities & CAP_UNIX) &&
250 (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
251 rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
252
253 /*
254 * Only need to call the old QFSInfo if failed on newer one,
255 * e.g. by OS/2.
256 **/
257 if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
258 rc = CIFSSMBQFSInfo(xid, tcon, buf);
259
260 /*
261 * Some old Windows servers also do not support level 103, retry with
262 * older level one if old server failed the previous call or we
263 * bypassed it because we detected that this was an older LANMAN sess
264 */
4523cc30 265 if (rc)
39da9847
SF
266 rc = SMBOldQFSInfo(xid, tcon, buf);
267
1da177e4 268 FreeXid(xid);
39da9847 269 return 0;
1da177e4
LT
270}
271
e6305c43 272static int cifs_permission(struct inode *inode, int mask)
1da177e4
LT
273{
274 struct cifs_sb_info *cifs_sb;
275
276 cifs_sb = CIFS_SB(inode->i_sb);
277
f696a365
MS
278 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
279 if ((mask & MAY_EXEC) && !execute_ok(inode))
280 return -EACCES;
281 else
282 return 0;
283 } else /* file mode might have been restricted at mount time
50c2f753 284 on the client (above and beyond ACL on servers) for
1da177e4 285 servers which do not support setting and viewing mode bits,
50c2f753 286 so allowing client to check permissions is useful */
1da177e4
LT
287 return generic_permission(inode, mask, NULL);
288}
289
e18b890b
CL
290static struct kmem_cache *cifs_inode_cachep;
291static struct kmem_cache *cifs_req_cachep;
292static struct kmem_cache *cifs_mid_cachep;
e18b890b 293static struct kmem_cache *cifs_sm_req_cachep;
1da177e4
LT
294mempool_t *cifs_sm_req_poolp;
295mempool_t *cifs_req_poolp;
296mempool_t *cifs_mid_poolp;
297
298static struct inode *
299cifs_alloc_inode(struct super_block *sb)
300{
301 struct cifsInodeInfo *cifs_inode;
e94b1766 302 cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
1da177e4
LT
303 if (!cifs_inode)
304 return NULL;
305 cifs_inode->cifsAttrs = 0x20; /* default */
1da177e4 306 cifs_inode->time = 0;
cea21805 307 cifs_inode->write_behind_rc = 0;
1da177e4
LT
308 /* Until the file is open and we have gotten oplock
309 info back from the server, can not assume caching of
310 file data or metadata */
4b18f2a9
SF
311 cifs_inode->clientCanCacheRead = false;
312 cifs_inode->clientCanCacheAll = false;
9a8165fc 313 cifs_inode->delete_pending = false;
df2cf170 314 cifs_inode->invalid_mapping = false;
1da177e4 315 cifs_inode->vfs_inode.i_blkbits = 14; /* 2**14 = CIFS_MAX_MSGSIZE */
fbec9ab9 316 cifs_inode->server_eof = 0;
50c2f753 317
1b2b2126
SF
318 /* Can not set i_flags here - they get immediately overwritten
319 to zero by the VFS */
320/* cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;*/
1da177e4
LT
321 INIT_LIST_HEAD(&cifs_inode->openFileList);
322 return &cifs_inode->vfs_inode;
323}
324
325static void
326cifs_destroy_inode(struct inode *inode)
327{
328 kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
329}
330
61f98ffd
JL
331static void
332cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
333{
334 seq_printf(s, ",addr=");
335
336 switch (server->addr.sockAddr.sin_family) {
337 case AF_INET:
338 seq_printf(s, "%pI4", &server->addr.sockAddr.sin_addr.s_addr);
339 break;
340 case AF_INET6:
341 seq_printf(s, "%pI6",
342 &server->addr.sockAddr6.sin6_addr.s6_addr);
343 if (server->addr.sockAddr6.sin6_scope_id)
344 seq_printf(s, "%%%u",
345 server->addr.sockAddr6.sin6_scope_id);
346 break;
347 default:
348 seq_printf(s, "(unknown)");
349 }
350}
351
1da177e4
LT
352/*
353 * cifs_show_options() is for displaying mount options in /proc/mounts.
354 * Not all settable options are displayed but most of the important
355 * ones are.
356 */
357static int
358cifs_show_options(struct seq_file *s, struct vfsmount *m)
359{
8e047d09
JL
360 struct cifs_sb_info *cifs_sb = CIFS_SB(m->mnt_sb);
361 struct cifsTconInfo *tcon = cifs_sb->tcon;
8616e0fc 362
8e047d09 363 seq_printf(s, ",unc=%s", tcon->treeName);
8616e0fc
JL
364 if (tcon->ses->userName)
365 seq_printf(s, ",username=%s", tcon->ses->userName);
366 if (tcon->ses->domainName)
367 seq_printf(s, ",domain=%s", tcon->ses->domainName);
368
8616e0fc 369 seq_printf(s, ",uid=%d", cifs_sb->mnt_uid);
340481a3
JL
370 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
371 seq_printf(s, ",forceuid");
4486d6ed
JL
372 else
373 seq_printf(s, ",noforceuid");
340481a3 374
8616e0fc 375 seq_printf(s, ",gid=%d", cifs_sb->mnt_gid);
340481a3
JL
376 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
377 seq_printf(s, ",forcegid");
4486d6ed
JL
378 else
379 seq_printf(s, ",noforcegid");
8616e0fc 380
61f98ffd 381 cifs_show_address(s, tcon->ses->server);
1da177e4 382
8616e0fc
JL
383 if (!tcon->unix_ext)
384 seq_printf(s, ",file_mode=0%o,dir_mode=0%o",
2b280fab
SF
385 cifs_sb->mnt_file_mode,
386 cifs_sb->mnt_dir_mode);
8616e0fc
JL
387 if (tcon->seal)
388 seq_printf(s, ",seal");
389 if (tcon->nocase)
390 seq_printf(s, ",nocase");
391 if (tcon->retry)
392 seq_printf(s, ",hard");
393 if (cifs_sb->prepath)
394 seq_printf(s, ",prepath=%s", cifs_sb->prepath);
395 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
396 seq_printf(s, ",posixpaths");
397 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
398 seq_printf(s, ",setuids");
399 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
400 seq_printf(s, ",serverino");
401 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
402 seq_printf(s, ",directio");
403 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
404 seq_printf(s, ",nouser_xattr");
405 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
406 seq_printf(s, ",mapchars");
407 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
408 seq_printf(s, ",sfu");
409 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
410 seq_printf(s, ",nobrl");
411 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
412 seq_printf(s, ",cifsacl");
413 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
414 seq_printf(s, ",dynperm");
415 if (m->mnt_sb->s_flags & MS_POSIXACL)
416 seq_printf(s, ",acl");
417
418 seq_printf(s, ",rsize=%d", cifs_sb->rsize);
419 seq_printf(s, ",wsize=%d", cifs_sb->wsize);
420
1da177e4
LT
421 return 0;
422}
423
42faad99 424static void cifs_umount_begin(struct super_block *sb)
68058e75 425{
42faad99 426 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
50c2f753 427 struct cifsTconInfo *tcon;
68058e75 428
4523cc30 429 if (cifs_sb == NULL)
9e2e85f8
SF
430 return;
431
432 tcon = cifs_sb->tcon;
4523cc30 433 if (tcon == NULL)
9e2e85f8 434 return;
f1987b44
JL
435
436 read_lock(&cifs_tcp_ses_lock);
ad8034f1
SF
437 if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
438 /* we have other mounts to same share or we have
439 already tried to force umount this and woken up
440 all waiting network requests, nothing to do */
441 read_unlock(&cifs_tcp_ses_lock);
442 return;
443 } else if (tcon->tc_count == 1)
5e1253b5 444 tcon->tidStatus = CifsExiting;
f1987b44 445 read_unlock(&cifs_tcp_ses_lock);
5e1253b5 446
3a5ff61c 447 /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
7b7abfe3 448 /* cancel_notify_requests(tcon); */
50c2f753 449 if (tcon->ses && tcon->ses->server) {
b6b38f70 450 cFYI(1, "wake up tasks now - umount begin not complete");
9e2e85f8 451 wake_up_all(&tcon->ses->server->request_q);
6ab16d24
SF
452 wake_up_all(&tcon->ses->server->response_q);
453 msleep(1); /* yield */
454 /* we have to kick the requests once more */
455 wake_up_all(&tcon->ses->server->response_q);
456 msleep(1);
5e1253b5 457 }
68058e75
SF
458
459 return;
460}
68058e75 461
bf97d287
SF
462#ifdef CONFIG_CIFS_STATS2
463static int cifs_show_stats(struct seq_file *s, struct vfsmount *mnt)
464{
465 /* BB FIXME */
466 return 0;
467}
468#endif
469
1da177e4
LT
470static int cifs_remount(struct super_block *sb, int *flags, char *data)
471{
472 *flags |= MS_NODIRATIME;
473 return 0;
474}
475
12420ac3
JL
476void cifs_drop_inode(struct inode *inode)
477{
478 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
479
480 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
481 return generic_drop_inode(inode);
482
483 return generic_delete_inode(inode);
484}
485
ee9b6d61 486static const struct super_operations cifs_super_ops = {
1da177e4
LT
487 .put_super = cifs_put_super,
488 .statfs = cifs_statfs,
489 .alloc_inode = cifs_alloc_inode,
490 .destroy_inode = cifs_destroy_inode,
12420ac3
JL
491 .drop_inode = cifs_drop_inode,
492/* .delete_inode = cifs_delete_inode, */ /* Do not need above
493 function unless later we add lazy close of inodes or unless the
50c2f753
SF
494 kernel forgets to call us with the same number of releases (closes)
495 as opens */
1da177e4 496 .show_options = cifs_show_options,
7b7abfe3 497 .umount_begin = cifs_umount_begin,
1da177e4 498 .remount_fs = cifs_remount,
bf97d287 499#ifdef CONFIG_CIFS_STATS2
f46d3e11 500 .show_stats = cifs_show_stats,
bf97d287 501#endif
1da177e4
LT
502};
503
454e2398 504static int
1da177e4 505cifs_get_sb(struct file_system_type *fs_type,
454e2398 506 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1da177e4
LT
507{
508 int rc;
509 struct super_block *sb = sget(fs_type, NULL, set_anon_super, NULL);
510
b6b38f70 511 cFYI(1, "Devname: %s flags: %d ", dev_name, flags);
1da177e4
LT
512
513 if (IS_ERR(sb))
454e2398 514 return PTR_ERR(sb);
1da177e4
LT
515
516 sb->s_flags = flags;
517
9b04c997 518 rc = cifs_read_super(sb, data, dev_name, flags & MS_SILENT ? 1 : 0);
1da177e4 519 if (rc) {
6f5bbff9 520 deactivate_locked_super(sb);
454e2398 521 return rc;
1da177e4
LT
522 }
523 sb->s_flags |= MS_ACTIVE;
a3ec947c
SB
524 simple_set_mnt(mnt, sb);
525 return 0;
1da177e4
LT
526}
527
027445c3
BP
528static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
529 unsigned long nr_segs, loff_t pos)
1da177e4 530{
e6a00296 531 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
1da177e4
LT
532 ssize_t written;
533
027445c3 534 written = generic_file_aio_write(iocb, iov, nr_segs, pos);
87c89dd7
SF
535 if (!CIFS_I(inode)->clientCanCacheAll)
536 filemap_fdatawrite(inode->i_mapping);
1da177e4
LT
537 return written;
538}
539
c32a0b68
SF
540static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
541{
542 /* origin == SEEK_END => we must revalidate the cached file length */
0889a944 543 if (origin == SEEK_END) {
030e9d81
SF
544 int retval;
545
546 /* some applications poll for the file length in this strange
547 way so we must seek to end on non-oplocked files by
548 setting the revalidate time to zero */
c33f8d32 549 CIFS_I(file->f_path.dentry->d_inode)->time = 0;
030e9d81 550
abab095d 551 retval = cifs_revalidate_file(file);
c32a0b68
SF
552 if (retval < 0)
553 return (loff_t)retval;
554 }
9465efc9 555 return generic_file_llseek_unlocked(file, offset, origin);
c32a0b68
SF
556}
557
84210e91
SF
558static int cifs_setlease(struct file *file, long arg, struct file_lock **lease)
559{
560 /* note that this is called by vfs setlease with the BKL held
561 although I doubt that BKL is needed here in cifs */
562 struct inode *inode = file->f_path.dentry->d_inode;
563
564 if (!(S_ISREG(inode->i_mode)))
565 return -EINVAL;
566
567 /* check if file is oplocked */
568 if (((arg == F_RDLCK) &&
569 (CIFS_I(inode)->clientCanCacheRead)) ||
570 ((arg == F_WRLCK) &&
571 (CIFS_I(inode)->clientCanCacheAll)))
572 return generic_setlease(file, arg, lease);
573 else if (CIFS_SB(inode->i_sb)->tcon->local_lease &&
574 !CIFS_I(inode)->clientCanCacheRead)
575 /* If the server claims to support oplock on this
576 file, then we still need to check oplock even
577 if the local_lease mount option is set, but there
578 are servers which do not support oplock for which
579 this mount option may be useful if the user
580 knows that the file won't be changed on the server
581 by anyone else */
582 return generic_setlease(file, arg, lease);
583 else
584 return -EAGAIN;
585}
84210e91 586
e6ab1582 587struct file_system_type cifs_fs_type = {
1da177e4
LT
588 .owner = THIS_MODULE,
589 .name = "cifs",
590 .get_sb = cifs_get_sb,
591 .kill_sb = kill_anon_super,
592 /* .fs_flags */
593};
754661f1 594const struct inode_operations cifs_dir_inode_ops = {
1da177e4
LT
595 .create = cifs_create,
596 .lookup = cifs_lookup,
597 .getattr = cifs_getattr,
598 .unlink = cifs_unlink,
599 .link = cifs_hardlink,
600 .mkdir = cifs_mkdir,
601 .rmdir = cifs_rmdir,
602 .rename = cifs_rename,
603 .permission = cifs_permission,
604/* revalidate:cifs_revalidate, */
605 .setattr = cifs_setattr,
606 .symlink = cifs_symlink,
607 .mknod = cifs_mknod,
608#ifdef CONFIG_CIFS_XATTR
609 .setxattr = cifs_setxattr,
610 .getxattr = cifs_getxattr,
611 .listxattr = cifs_listxattr,
612 .removexattr = cifs_removexattr,
613#endif
614};
615
754661f1 616const struct inode_operations cifs_file_inode_ops = {
1da177e4
LT
617/* revalidate:cifs_revalidate, */
618 .setattr = cifs_setattr,
619 .getattr = cifs_getattr, /* do we need this anymore? */
620 .rename = cifs_rename,
621 .permission = cifs_permission,
622#ifdef CONFIG_CIFS_XATTR
623 .setxattr = cifs_setxattr,
624 .getxattr = cifs_getxattr,
625 .listxattr = cifs_listxattr,
626 .removexattr = cifs_removexattr,
50c2f753 627#endif
1da177e4
LT
628};
629
754661f1 630const struct inode_operations cifs_symlink_inode_ops = {
50c2f753 631 .readlink = generic_readlink,
1da177e4
LT
632 .follow_link = cifs_follow_link,
633 .put_link = cifs_put_link,
634 .permission = cifs_permission,
635 /* BB add the following two eventually */
636 /* revalidate: cifs_revalidate,
637 setattr: cifs_notify_change, *//* BB do we need notify change */
638#ifdef CONFIG_CIFS_XATTR
639 .setxattr = cifs_setxattr,
640 .getxattr = cifs_getxattr,
641 .listxattr = cifs_listxattr,
642 .removexattr = cifs_removexattr,
50c2f753 643#endif
1da177e4
LT
644};
645
4b6f5d20 646const struct file_operations cifs_file_ops = {
87c89dd7
SF
647 .read = do_sync_read,
648 .write = do_sync_write,
87c89dd7
SF
649 .aio_read = generic_file_aio_read,
650 .aio_write = cifs_file_aio_write,
1da177e4
LT
651 .open = cifs_open,
652 .release = cifs_close,
653 .lock = cifs_lock,
654 .fsync = cifs_fsync,
655 .flush = cifs_flush,
656 .mmap = cifs_file_mmap,
5ffc4ef4 657 .splice_read = generic_file_splice_read,
c32a0b68 658 .llseek = cifs_llseek,
c67593a0 659#ifdef CONFIG_CIFS_POSIX
f9ddcca4 660 .unlocked_ioctl = cifs_ioctl,
c67593a0 661#endif /* CONFIG_CIFS_POSIX */
84210e91 662 .setlease = cifs_setlease,
1da177e4
LT
663};
664
4b6f5d20 665const struct file_operations cifs_file_direct_ops = {
a994b8fa 666 /* no aio, no readv -
1da177e4
LT
667 BB reevaluate whether they can be done with directio, no cache */
668 .read = cifs_user_read,
669 .write = cifs_user_write,
670 .open = cifs_open,
671 .release = cifs_close,
672 .lock = cifs_lock,
673 .fsync = cifs_fsync,
674 .flush = cifs_flush,
a994b8fa 675 .mmap = cifs_file_mmap,
5ffc4ef4 676 .splice_read = generic_file_splice_read,
c67593a0 677#ifdef CONFIG_CIFS_POSIX
f9ddcca4 678 .unlocked_ioctl = cifs_ioctl,
c67593a0 679#endif /* CONFIG_CIFS_POSIX */
c32a0b68 680 .llseek = cifs_llseek,
84210e91 681 .setlease = cifs_setlease,
1da177e4 682};
4b6f5d20 683const struct file_operations cifs_file_nobrl_ops = {
87c89dd7
SF
684 .read = do_sync_read,
685 .write = do_sync_write,
87c89dd7
SF
686 .aio_read = generic_file_aio_read,
687 .aio_write = cifs_file_aio_write,
688 .open = cifs_open,
689 .release = cifs_close,
690 .fsync = cifs_fsync,
691 .flush = cifs_flush,
692 .mmap = cifs_file_mmap,
5ffc4ef4 693 .splice_read = generic_file_splice_read,
c32a0b68 694 .llseek = cifs_llseek,
8b94bcb9 695#ifdef CONFIG_CIFS_POSIX
f9ddcca4 696 .unlocked_ioctl = cifs_ioctl,
8b94bcb9 697#endif /* CONFIG_CIFS_POSIX */
84210e91 698 .setlease = cifs_setlease,
8b94bcb9
SF
699};
700
4b6f5d20 701const struct file_operations cifs_file_direct_nobrl_ops = {
50c2f753 702 /* no mmap, no aio, no readv -
87c89dd7
SF
703 BB reevaluate whether they can be done with directio, no cache */
704 .read = cifs_user_read,
705 .write = cifs_user_write,
706 .open = cifs_open,
707 .release = cifs_close,
708 .fsync = cifs_fsync,
709 .flush = cifs_flush,
810627a0 710 .mmap = cifs_file_mmap,
5ffc4ef4 711 .splice_read = generic_file_splice_read,
8b94bcb9 712#ifdef CONFIG_CIFS_POSIX
f9ddcca4 713 .unlocked_ioctl = cifs_ioctl,
8b94bcb9 714#endif /* CONFIG_CIFS_POSIX */
c32a0b68 715 .llseek = cifs_llseek,
84210e91 716 .setlease = cifs_setlease,
8b94bcb9 717};
1da177e4 718
4b6f5d20 719const struct file_operations cifs_dir_ops = {
1da177e4
LT
720 .readdir = cifs_readdir,
721 .release = cifs_closedir,
722 .read = generic_read_dir,
f9ddcca4 723 .unlocked_ioctl = cifs_ioctl,
3222a3e5 724 .llseek = generic_file_llseek,
1da177e4
LT
725};
726
727static void
51cc5068 728cifs_init_once(void *inode)
1da177e4
LT
729{
730 struct cifsInodeInfo *cifsi = inode;
731
a35afb83
CL
732 inode_init_once(&cifsi->vfs_inode);
733 INIT_LIST_HEAD(&cifsi->lockList);
1da177e4
LT
734}
735
736static int
737cifs_init_inodecache(void)
738{
739 cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
26f57364 740 sizeof(struct cifsInodeInfo),
fffb60f9
PJ
741 0, (SLAB_RECLAIM_ACCOUNT|
742 SLAB_MEM_SPREAD),
20c2df83 743 cifs_init_once);
1da177e4
LT
744 if (cifs_inode_cachep == NULL)
745 return -ENOMEM;
746
747 return 0;
748}
749
750static void
751cifs_destroy_inodecache(void)
752{
1a1d92c1 753 kmem_cache_destroy(cifs_inode_cachep);
1da177e4
LT
754}
755
756static int
757cifs_init_request_bufs(void)
758{
4523cc30 759 if (CIFSMaxBufSize < 8192) {
1da177e4
LT
760 /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
761 Unicode path name has to fit in any SMB/CIFS path based frames */
762 CIFSMaxBufSize = 8192;
763 } else if (CIFSMaxBufSize > 1024*127) {
764 CIFSMaxBufSize = 1024 * 127;
765 } else {
766 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
767 }
b6b38f70 768/* cERROR(1, "CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize); */
1da177e4
LT
769 cifs_req_cachep = kmem_cache_create("cifs_request",
770 CIFSMaxBufSize +
771 MAX_CIFS_HDR_SIZE, 0,
20c2df83 772 SLAB_HWCACHE_ALIGN, NULL);
1da177e4
LT
773 if (cifs_req_cachep == NULL)
774 return -ENOMEM;
775
4523cc30 776 if (cifs_min_rcv < 1)
1da177e4
LT
777 cifs_min_rcv = 1;
778 else if (cifs_min_rcv > 64) {
779 cifs_min_rcv = 64;
b6b38f70 780 cERROR(1, "cifs_min_rcv set to maximum (64)");
1da177e4
LT
781 }
782
93d2341c
MD
783 cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
784 cifs_req_cachep);
1da177e4 785
4523cc30 786 if (cifs_req_poolp == NULL) {
1da177e4
LT
787 kmem_cache_destroy(cifs_req_cachep);
788 return -ENOMEM;
789 }
ec637e3f 790 /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
1da177e4
LT
791 almost all handle based requests (but not write response, nor is it
792 sufficient for path based requests). A smaller size would have
50c2f753 793 been more efficient (compacting multiple slab items on one 4k page)
1da177e4
LT
794 for the case in which debug was on, but this larger size allows
795 more SMBs to use small buffer alloc and is still much more
6dc0f87e 796 efficient to alloc 1 per page off the slab compared to 17K (5page)
1da177e4
LT
797 alloc of large cifs buffers even when page debugging is on */
798 cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
6dc0f87e 799 MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
20c2df83 800 NULL);
1da177e4
LT
801 if (cifs_sm_req_cachep == NULL) {
802 mempool_destroy(cifs_req_poolp);
803 kmem_cache_destroy(cifs_req_cachep);
6dc0f87e 804 return -ENOMEM;
1da177e4
LT
805 }
806
4523cc30 807 if (cifs_min_small < 2)
1da177e4
LT
808 cifs_min_small = 2;
809 else if (cifs_min_small > 256) {
810 cifs_min_small = 256;
b6b38f70 811 cFYI(1, "cifs_min_small set to maximum (256)");
1da177e4
LT
812 }
813
93d2341c
MD
814 cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
815 cifs_sm_req_cachep);
1da177e4 816
4523cc30 817 if (cifs_sm_req_poolp == NULL) {
1da177e4
LT
818 mempool_destroy(cifs_req_poolp);
819 kmem_cache_destroy(cifs_req_cachep);
820 kmem_cache_destroy(cifs_sm_req_cachep);
821 return -ENOMEM;
822 }
823
824 return 0;
825}
826
827static void
828cifs_destroy_request_bufs(void)
829{
830 mempool_destroy(cifs_req_poolp);
1a1d92c1 831 kmem_cache_destroy(cifs_req_cachep);
1da177e4 832 mempool_destroy(cifs_sm_req_poolp);
1a1d92c1 833 kmem_cache_destroy(cifs_sm_req_cachep);
1da177e4
LT
834}
835
836static int
837cifs_init_mids(void)
838{
839 cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
26f57364
SF
840 sizeof(struct mid_q_entry), 0,
841 SLAB_HWCACHE_ALIGN, NULL);
1da177e4
LT
842 if (cifs_mid_cachep == NULL)
843 return -ENOMEM;
844
93d2341c
MD
845 /* 3 is a reasonable minimum number of simultaneous operations */
846 cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
4523cc30 847 if (cifs_mid_poolp == NULL) {
1da177e4
LT
848 kmem_cache_destroy(cifs_mid_cachep);
849 return -ENOMEM;
850 }
851
1da177e4
LT
852 return 0;
853}
854
855static void
856cifs_destroy_mids(void)
857{
858 mempool_destroy(cifs_mid_poolp);
1a1d92c1 859 kmem_cache_destroy(cifs_mid_cachep);
1da177e4
LT
860}
861
1da177e4
LT
862static int __init
863init_cifs(void)
864{
865 int rc = 0;
1da177e4 866 cifs_proc_init();
e7ddee90 867 INIT_LIST_HEAD(&cifs_tcp_ses_list);
4ca9c190
SF
868#ifdef CONFIG_CIFS_EXPERIMENTAL
869 INIT_LIST_HEAD(&GlobalDnotifyReqList);
870 INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
6dc0f87e 871#endif
1da177e4
LT
872/*
873 * Initialize Global counters
874 */
875 atomic_set(&sesInfoAllocCount, 0);
876 atomic_set(&tconInfoAllocCount, 0);
6dc0f87e 877 atomic_set(&tcpSesAllocCount, 0);
1da177e4
LT
878 atomic_set(&tcpSesReconnectCount, 0);
879 atomic_set(&tconInfoReconnectCount, 0);
880
881 atomic_set(&bufAllocCount, 0);
4498eed5
SF
882 atomic_set(&smBufAllocCount, 0);
883#ifdef CONFIG_CIFS_STATS2
884 atomic_set(&totBufAllocCount, 0);
885 atomic_set(&totSmBufAllocCount, 0);
886#endif /* CONFIG_CIFS_STATS2 */
887
1da177e4
LT
888 atomic_set(&midCount, 0);
889 GlobalCurrentXid = 0;
890 GlobalTotalActiveXid = 0;
891 GlobalMaxActiveXid = 0;
2cd646a2 892 memset(Local_System_Name, 0, 15);
1da177e4 893 rwlock_init(&GlobalSMBSeslock);
e7ddee90 894 rwlock_init(&cifs_tcp_ses_lock);
1da177e4
LT
895 spin_lock_init(&GlobalMid_Lock);
896
4523cc30 897 if (cifs_max_pending < 2) {
1da177e4 898 cifs_max_pending = 2;
b6b38f70 899 cFYI(1, "cifs_max_pending set to min of 2");
4523cc30 900 } else if (cifs_max_pending > 256) {
1da177e4 901 cifs_max_pending = 256;
b6b38f70 902 cFYI(1, "cifs_max_pending set to max of 256");
1da177e4
LT
903 }
904
905 rc = cifs_init_inodecache();
45af7a0f
SF
906 if (rc)
907 goto out_clean_proc;
908
909 rc = cifs_init_mids();
910 if (rc)
911 goto out_destroy_inodecache;
912
913 rc = cifs_init_request_bufs();
914 if (rc)
915 goto out_destroy_mids;
916
917 rc = register_filesystem(&cifs_fs_type);
918 if (rc)
919 goto out_destroy_request_bufs;
84a15b93
JL
920#ifdef CONFIG_CIFS_UPCALL
921 rc = register_key_type(&cifs_spnego_key_type);
922 if (rc)
923 goto out_unregister_filesystem;
6103335d
SF
924#endif
925#ifdef CONFIG_CIFS_DFS_UPCALL
926 rc = register_key_type(&key_type_dns_resolver);
927 if (rc)
928 goto out_unregister_key_type;
84a15b93 929#endif
0109d7e6 930 rc = slow_work_register_user(THIS_MODULE);
3bc303c2
JL
931 if (rc)
932 goto out_unregister_resolver_key;
45af7a0f 933
45af7a0f
SF
934 return 0;
935
3bc303c2 936 out_unregister_resolver_key:
6103335d
SF
937#ifdef CONFIG_CIFS_DFS_UPCALL
938 unregister_key_type(&key_type_dns_resolver);
84a15b93 939 out_unregister_key_type:
6103335d 940#endif
84a15b93
JL
941#ifdef CONFIG_CIFS_UPCALL
942 unregister_key_type(&cifs_spnego_key_type);
45af7a0f 943 out_unregister_filesystem:
84a15b93 944#endif
45af7a0f
SF
945 unregister_filesystem(&cifs_fs_type);
946 out_destroy_request_bufs:
947 cifs_destroy_request_bufs();
948 out_destroy_mids:
949 cifs_destroy_mids();
950 out_destroy_inodecache:
951 cifs_destroy_inodecache();
952 out_clean_proc:
1da177e4 953 cifs_proc_clean();
1da177e4
LT
954 return rc;
955}
956
957static void __exit
958exit_cifs(void)
959{
b6b38f70 960 cFYI(DBG2, "exit_cifs");
1da177e4 961 cifs_proc_clean();
6103335d 962#ifdef CONFIG_CIFS_DFS_UPCALL
78d31a3a 963 cifs_dfs_release_automount_timer();
6103335d
SF
964 unregister_key_type(&key_type_dns_resolver);
965#endif
84a15b93
JL
966#ifdef CONFIG_CIFS_UPCALL
967 unregister_key_type(&cifs_spnego_key_type);
1da177e4
LT
968#endif
969 unregister_filesystem(&cifs_fs_type);
970 cifs_destroy_inodecache();
971 cifs_destroy_mids();
972 cifs_destroy_request_bufs();
1da177e4
LT
973}
974
975MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
6dc0f87e 976MODULE_LICENSE("GPL"); /* combination of LGPL + GPL source behaves as GPL */
1da177e4 977MODULE_DESCRIPTION
63135e08
SF
978 ("VFS to access servers complying with the SNIA CIFS Specification "
979 "e.g. Samba and Windows");
1da177e4
LT
980MODULE_VERSION(CIFS_VERSION);
981module_init(init_cifs)
982module_exit(exit_cifs)