Merge branch 'upstream/tidy-xen-mmu-2.6.39' of git://git.kernel.org/pub/scm/linux...
[pandora-kernel.git] / fs / cifs / cifsfs.c
1 /*
2  *   fs/cifs/cifsfs.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2008
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>
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 */
51
52 int cifsFYI = 0;
53 int cifsERROR = 1;
54 int traceSMB = 0;
55 unsigned int oplockEnabled = 1;
56 unsigned int linuxExtEnabled = 1;
57 unsigned int lookupCacheEnabled = 1;
58 unsigned int multiuser_mount = 0;
59 unsigned int global_secflags = CIFSSEC_DEF;
60 /* unsigned int ntlmv2_support = 0; */
61 unsigned int sign_CIFS_PDUs = 1;
62 static const struct super_operations cifs_super_ops;
63 unsigned int CIFSMaxBufSize = CIFS_MAX_MSGSIZE;
64 module_param(CIFSMaxBufSize, int, 0);
65 MODULE_PARM_DESC(CIFSMaxBufSize, "Network buffer size (not including header). "
66                                  "Default: 16384 Range: 8192 to 130048");
67 unsigned int cifs_min_rcv = CIFS_MIN_RCV_POOL;
68 module_param(cifs_min_rcv, int, 0);
69 MODULE_PARM_DESC(cifs_min_rcv, "Network buffers in pool. Default: 4 Range: "
70                                 "1 to 64");
71 unsigned int cifs_min_small = 30;
72 module_param(cifs_min_small, int, 0);
73 MODULE_PARM_DESC(cifs_min_small, "Small network buffers in pool. Default: 30 "
74                                  "Range: 2 to 256");
75 unsigned int cifs_max_pending = CIFS_MAX_REQ;
76 module_param(cifs_max_pending, int, 0);
77 MODULE_PARM_DESC(cifs_max_pending, "Simultaneous requests to server. "
78                                    "Default: 50 Range: 2 to 256");
79 unsigned short echo_retries = 5;
80 module_param(echo_retries, ushort, 0644);
81 MODULE_PARM_DESC(echo_retries, "Number of echo attempts before giving up and "
82                                "reconnecting server. Default: 5. 0 means "
83                                "never reconnect.");
84 extern mempool_t *cifs_sm_req_poolp;
85 extern mempool_t *cifs_req_poolp;
86 extern mempool_t *cifs_mid_poolp;
87
88 void
89 cifs_sb_active(struct super_block *sb)
90 {
91         struct cifs_sb_info *server = CIFS_SB(sb);
92
93         if (atomic_inc_return(&server->active) == 1)
94                 atomic_inc(&sb->s_active);
95 }
96
97 void
98 cifs_sb_deactive(struct super_block *sb)
99 {
100         struct cifs_sb_info *server = CIFS_SB(sb);
101
102         if (atomic_dec_and_test(&server->active))
103                 deactivate_super(sb);
104 }
105
106 static int
107 cifs_read_super(struct super_block *sb, void *data,
108                 const char *devname, int silent)
109 {
110         struct inode *inode;
111         struct cifs_sb_info *cifs_sb;
112         int rc = 0;
113
114         /* BB should we make this contingent on mount parm? */
115         sb->s_flags |= MS_NODIRATIME | MS_NOATIME;
116         sb->s_fs_info = kzalloc(sizeof(struct cifs_sb_info), GFP_KERNEL);
117         cifs_sb = CIFS_SB(sb);
118         if (cifs_sb == NULL)
119                 return -ENOMEM;
120
121         spin_lock_init(&cifs_sb->tlink_tree_lock);
122         cifs_sb->tlink_tree = RB_ROOT;
123
124         rc = bdi_setup_and_register(&cifs_sb->bdi, "cifs", BDI_CAP_MAP_COPY);
125         if (rc) {
126                 kfree(cifs_sb);
127                 return rc;
128         }
129         cifs_sb->bdi.ra_pages = default_backing_dev_info.ra_pages;
130
131         /*
132          * Copy mount params to sb for use in submounts. Better to do
133          * the copy here and deal with the error before cleanup gets
134          * complicated post-mount.
135          */
136         if (data) {
137                 cifs_sb->mountdata = kstrndup(data, PAGE_SIZE, GFP_KERNEL);
138                 if (cifs_sb->mountdata == NULL) {
139                         bdi_destroy(&cifs_sb->bdi);
140                         kfree(sb->s_fs_info);
141                         sb->s_fs_info = NULL;
142                         return -ENOMEM;
143                 }
144         }
145
146         rc = cifs_mount(sb, cifs_sb, devname);
147
148         if (rc) {
149                 if (!silent)
150                         cERROR(1, "cifs_mount failed w/return code = %d", rc);
151                 goto out_mount_failed;
152         }
153
154         sb->s_magic = CIFS_MAGIC_NUMBER;
155         sb->s_op = &cifs_super_ops;
156         sb->s_bdi = &cifs_sb->bdi;
157         sb->s_blocksize = CIFS_MAX_MSGSIZE;
158         sb->s_blocksize_bits = 14;      /* default 2**14 = CIFS_MAX_MSGSIZE */
159         inode = cifs_root_iget(sb);
160
161         if (IS_ERR(inode)) {
162                 rc = PTR_ERR(inode);
163                 inode = NULL;
164                 goto out_no_root;
165         }
166
167         sb->s_root = d_alloc_root(inode);
168
169         if (!sb->s_root) {
170                 rc = -ENOMEM;
171                 goto out_no_root;
172         }
173
174         /* do that *after* d_alloc_root() - we want NULL ->d_op for root here */
175         if (cifs_sb_master_tcon(cifs_sb)->nocase)
176                 sb->s_d_op = &cifs_ci_dentry_ops;
177         else
178                 sb->s_d_op = &cifs_dentry_ops;
179
180 #ifdef CIFS_NFSD_EXPORT
181         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
182                 cFYI(1, "export ops supported");
183                 sb->s_export_op = &cifs_export_ops;
184         }
185 #endif /* CIFS_NFSD_EXPORT */
186
187         return 0;
188
189 out_no_root:
190         cERROR(1, "cifs_read_super: get root inode failed");
191         if (inode)
192                 iput(inode);
193
194         cifs_umount(sb, cifs_sb);
195
196 out_mount_failed:
197         if (cifs_sb) {
198                 if (cifs_sb->mountdata) {
199                         kfree(cifs_sb->mountdata);
200                         cifs_sb->mountdata = NULL;
201                 }
202                 unload_nls(cifs_sb->local_nls);
203                 bdi_destroy(&cifs_sb->bdi);
204                 kfree(cifs_sb);
205         }
206         return rc;
207 }
208
209 static void
210 cifs_put_super(struct super_block *sb)
211 {
212         int rc = 0;
213         struct cifs_sb_info *cifs_sb;
214
215         cFYI(1, "In cifs_put_super");
216         cifs_sb = CIFS_SB(sb);
217         if (cifs_sb == NULL) {
218                 cFYI(1, "Empty cifs superblock info passed to unmount");
219                 return;
220         }
221
222         rc = cifs_umount(sb, cifs_sb);
223         if (rc)
224                 cERROR(1, "cifs_umount failed with return code %d", rc);
225         if (cifs_sb->mountdata) {
226                 kfree(cifs_sb->mountdata);
227                 cifs_sb->mountdata = NULL;
228         }
229
230         unload_nls(cifs_sb->local_nls);
231         bdi_destroy(&cifs_sb->bdi);
232         kfree(cifs_sb);
233 }
234
235 static int
236 cifs_statfs(struct dentry *dentry, struct kstatfs *buf)
237 {
238         struct super_block *sb = dentry->d_sb;
239         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
240         struct cifsTconInfo *tcon = cifs_sb_master_tcon(cifs_sb);
241         int rc = -EOPNOTSUPP;
242         int xid;
243
244         xid = GetXid();
245
246         buf->f_type = CIFS_MAGIC_NUMBER;
247
248         /*
249          * PATH_MAX may be too long - it would presumably be total path,
250          * but note that some servers (includinng Samba 3) have a shorter
251          * maximum path.
252          *
253          * Instead could get the real value via SMB_QUERY_FS_ATTRIBUTE_INFO.
254          */
255         buf->f_namelen = PATH_MAX;
256         buf->f_files = 0;       /* undefined */
257         buf->f_ffree = 0;       /* unlimited */
258
259         /*
260          * We could add a second check for a QFS Unix capability bit
261          */
262         if ((tcon->ses->capabilities & CAP_UNIX) &&
263             (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
264                 rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
265
266         /*
267          * Only need to call the old QFSInfo if failed on newer one,
268          * e.g. by OS/2.
269          **/
270         if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
271                 rc = CIFSSMBQFSInfo(xid, tcon, buf);
272
273         /*
274          * Some old Windows servers also do not support level 103, retry with
275          * older level one if old server failed the previous call or we
276          * bypassed it because we detected that this was an older LANMAN sess
277          */
278         if (rc)
279                 rc = SMBOldQFSInfo(xid, tcon, buf);
280
281         FreeXid(xid);
282         return 0;
283 }
284
285 static int cifs_permission(struct inode *inode, int mask, unsigned int flags)
286 {
287         struct cifs_sb_info *cifs_sb;
288
289         if (flags & IPERM_FLAG_RCU)
290                 return -ECHILD;
291
292         cifs_sb = CIFS_SB(inode->i_sb);
293
294         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) {
295                 if ((mask & MAY_EXEC) && !execute_ok(inode))
296                         return -EACCES;
297                 else
298                         return 0;
299         } else /* file mode might have been restricted at mount time
300                 on the client (above and beyond ACL on servers) for
301                 servers which do not support setting and viewing mode bits,
302                 so allowing client to check permissions is useful */
303                 return generic_permission(inode, mask, flags, NULL);
304 }
305
306 static struct kmem_cache *cifs_inode_cachep;
307 static struct kmem_cache *cifs_req_cachep;
308 static struct kmem_cache *cifs_mid_cachep;
309 static struct kmem_cache *cifs_sm_req_cachep;
310 mempool_t *cifs_sm_req_poolp;
311 mempool_t *cifs_req_poolp;
312 mempool_t *cifs_mid_poolp;
313
314 static struct inode *
315 cifs_alloc_inode(struct super_block *sb)
316 {
317         struct cifsInodeInfo *cifs_inode;
318         cifs_inode = kmem_cache_alloc(cifs_inode_cachep, GFP_KERNEL);
319         if (!cifs_inode)
320                 return NULL;
321         cifs_inode->cifsAttrs = 0x20;   /* default */
322         cifs_inode->time = 0;
323         /* Until the file is open and we have gotten oplock
324         info back from the server, can not assume caching of
325         file data or metadata */
326         cifs_set_oplock_level(cifs_inode, 0);
327         cifs_inode->delete_pending = false;
328         cifs_inode->invalid_mapping = false;
329         cifs_inode->vfs_inode.i_blkbits = 14;  /* 2**14 = CIFS_MAX_MSGSIZE */
330         cifs_inode->server_eof = 0;
331         cifs_inode->uniqueid = 0;
332         cifs_inode->createtime = 0;
333
334         /* Can not set i_flags here - they get immediately overwritten
335            to zero by the VFS */
336 /*      cifs_inode->vfs_inode.i_flags = S_NOATIME | S_NOCMTIME;*/
337         INIT_LIST_HEAD(&cifs_inode->openFileList);
338         return &cifs_inode->vfs_inode;
339 }
340
341 static void cifs_i_callback(struct rcu_head *head)
342 {
343         struct inode *inode = container_of(head, struct inode, i_rcu);
344         INIT_LIST_HEAD(&inode->i_dentry);
345         kmem_cache_free(cifs_inode_cachep, CIFS_I(inode));
346 }
347
348 static void
349 cifs_destroy_inode(struct inode *inode)
350 {
351         call_rcu(&inode->i_rcu, cifs_i_callback);
352 }
353
354 static void
355 cifs_evict_inode(struct inode *inode)
356 {
357         truncate_inode_pages(&inode->i_data, 0);
358         end_writeback(inode);
359         cifs_fscache_release_inode_cookie(inode);
360 }
361
362 static void
363 cifs_show_address(struct seq_file *s, struct TCP_Server_Info *server)
364 {
365         struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
366         struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
367
368         seq_printf(s, ",addr=");
369
370         switch (server->dstaddr.ss_family) {
371         case AF_INET:
372                 seq_printf(s, "%pI4", &sa->sin_addr.s_addr);
373                 break;
374         case AF_INET6:
375                 seq_printf(s, "%pI6", &sa6->sin6_addr.s6_addr);
376                 if (sa6->sin6_scope_id)
377                         seq_printf(s, "%%%u", sa6->sin6_scope_id);
378                 break;
379         default:
380                 seq_printf(s, "(unknown)");
381         }
382 }
383
384 /*
385  * cifs_show_options() is for displaying mount options in /proc/mounts.
386  * Not all settable options are displayed but most of the important
387  * ones are.
388  */
389 static int
390 cifs_show_options(struct seq_file *s, struct vfsmount *m)
391 {
392         struct cifs_sb_info *cifs_sb = CIFS_SB(m->mnt_sb);
393         struct cifsTconInfo *tcon = cifs_sb_master_tcon(cifs_sb);
394         struct sockaddr *srcaddr;
395         srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
396
397         seq_printf(s, ",unc=%s", tcon->treeName);
398
399         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)
400                 seq_printf(s, ",multiuser");
401         else if (tcon->ses->user_name)
402                 seq_printf(s, ",username=%s", tcon->ses->user_name);
403
404         if (tcon->ses->domainName)
405                 seq_printf(s, ",domain=%s", tcon->ses->domainName);
406
407         if (srcaddr->sa_family != AF_UNSPEC) {
408                 struct sockaddr_in *saddr4;
409                 struct sockaddr_in6 *saddr6;
410                 saddr4 = (struct sockaddr_in *)srcaddr;
411                 saddr6 = (struct sockaddr_in6 *)srcaddr;
412                 if (srcaddr->sa_family == AF_INET6)
413                         seq_printf(s, ",srcaddr=%pI6c",
414                                    &saddr6->sin6_addr);
415                 else if (srcaddr->sa_family == AF_INET)
416                         seq_printf(s, ",srcaddr=%pI4",
417                                    &saddr4->sin_addr.s_addr);
418                 else
419                         seq_printf(s, ",srcaddr=BAD-AF:%i",
420                                    (int)(srcaddr->sa_family));
421         }
422
423         seq_printf(s, ",uid=%d", cifs_sb->mnt_uid);
424         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
425                 seq_printf(s, ",forceuid");
426         else
427                 seq_printf(s, ",noforceuid");
428
429         seq_printf(s, ",gid=%d", cifs_sb->mnt_gid);
430         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
431                 seq_printf(s, ",forcegid");
432         else
433                 seq_printf(s, ",noforcegid");
434
435         cifs_show_address(s, tcon->ses->server);
436
437         if (!tcon->unix_ext)
438                 seq_printf(s, ",file_mode=0%o,dir_mode=0%o",
439                                            cifs_sb->mnt_file_mode,
440                                            cifs_sb->mnt_dir_mode);
441         if (tcon->seal)
442                 seq_printf(s, ",seal");
443         if (tcon->nocase)
444                 seq_printf(s, ",nocase");
445         if (tcon->retry)
446                 seq_printf(s, ",hard");
447         if (cifs_sb->prepath)
448                 seq_printf(s, ",prepath=%s", cifs_sb->prepath);
449         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)
450                 seq_printf(s, ",posixpaths");
451         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)
452                 seq_printf(s, ",setuids");
453         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
454                 seq_printf(s, ",serverino");
455         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
456                 seq_printf(s, ",directio");
457         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
458                 seq_printf(s, ",nouser_xattr");
459         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
460                 seq_printf(s, ",mapchars");
461         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
462                 seq_printf(s, ",sfu");
463         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
464                 seq_printf(s, ",nobrl");
465         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
466                 seq_printf(s, ",cifsacl");
467         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
468                 seq_printf(s, ",dynperm");
469         if (m->mnt_sb->s_flags & MS_POSIXACL)
470                 seq_printf(s, ",acl");
471         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS)
472                 seq_printf(s, ",mfsymlinks");
473         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
474                 seq_printf(s, ",fsc");
475
476         seq_printf(s, ",rsize=%d", cifs_sb->rsize);
477         seq_printf(s, ",wsize=%d", cifs_sb->wsize);
478         /* convert actimeo and display it in seconds */
479                 seq_printf(s, ",actimeo=%lu", cifs_sb->actimeo / HZ);
480
481         return 0;
482 }
483
484 static void cifs_umount_begin(struct super_block *sb)
485 {
486         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
487         struct cifsTconInfo *tcon;
488
489         if (cifs_sb == NULL)
490                 return;
491
492         tcon = cifs_sb_master_tcon(cifs_sb);
493
494         spin_lock(&cifs_tcp_ses_lock);
495         if ((tcon->tc_count > 1) || (tcon->tidStatus == CifsExiting)) {
496                 /* we have other mounts to same share or we have
497                    already tried to force umount this and woken up
498                    all waiting network requests, nothing to do */
499                 spin_unlock(&cifs_tcp_ses_lock);
500                 return;
501         } else if (tcon->tc_count == 1)
502                 tcon->tidStatus = CifsExiting;
503         spin_unlock(&cifs_tcp_ses_lock);
504
505         /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
506         /* cancel_notify_requests(tcon); */
507         if (tcon->ses && tcon->ses->server) {
508                 cFYI(1, "wake up tasks now - umount begin not complete");
509                 wake_up_all(&tcon->ses->server->request_q);
510                 wake_up_all(&tcon->ses->server->response_q);
511                 msleep(1); /* yield */
512                 /* we have to kick the requests once more */
513                 wake_up_all(&tcon->ses->server->response_q);
514                 msleep(1);
515         }
516
517         return;
518 }
519
520 #ifdef CONFIG_CIFS_STATS2
521 static int cifs_show_stats(struct seq_file *s, struct vfsmount *mnt)
522 {
523         /* BB FIXME */
524         return 0;
525 }
526 #endif
527
528 static int cifs_remount(struct super_block *sb, int *flags, char *data)
529 {
530         *flags |= MS_NODIRATIME;
531         return 0;
532 }
533
534 static int cifs_drop_inode(struct inode *inode)
535 {
536         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
537
538         /* no serverino => unconditional eviction */
539         return !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) ||
540                 generic_drop_inode(inode);
541 }
542
543 static const struct super_operations cifs_super_ops = {
544         .put_super = cifs_put_super,
545         .statfs = cifs_statfs,
546         .alloc_inode = cifs_alloc_inode,
547         .destroy_inode = cifs_destroy_inode,
548         .drop_inode     = cifs_drop_inode,
549         .evict_inode    = cifs_evict_inode,
550 /*      .delete_inode   = cifs_delete_inode,  */  /* Do not need above
551         function unless later we add lazy close of inodes or unless the
552         kernel forgets to call us with the same number of releases (closes)
553         as opens */
554         .show_options = cifs_show_options,
555         .umount_begin   = cifs_umount_begin,
556         .remount_fs = cifs_remount,
557 #ifdef CONFIG_CIFS_STATS2
558         .show_stats = cifs_show_stats,
559 #endif
560 };
561
562 static struct dentry *
563 cifs_do_mount(struct file_system_type *fs_type,
564             int flags, const char *dev_name, void *data)
565 {
566         int rc;
567         struct super_block *sb;
568
569         sb = sget(fs_type, NULL, set_anon_super, NULL);
570
571         cFYI(1, "Devname: %s flags: %d ", dev_name, flags);
572
573         if (IS_ERR(sb))
574                 return ERR_CAST(sb);
575
576         sb->s_flags = flags;
577
578         rc = cifs_read_super(sb, data, dev_name, flags & MS_SILENT ? 1 : 0);
579         if (rc) {
580                 deactivate_locked_super(sb);
581                 return ERR_PTR(rc);
582         }
583         sb->s_flags |= MS_ACTIVE;
584         return dget(sb->s_root);
585 }
586
587 static ssize_t cifs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
588                                    unsigned long nr_segs, loff_t pos)
589 {
590         struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
591         ssize_t written;
592         int rc;
593
594         written = generic_file_aio_write(iocb, iov, nr_segs, pos);
595
596         if (CIFS_I(inode)->clientCanCacheAll)
597                 return written;
598
599         rc = filemap_fdatawrite(inode->i_mapping);
600         if (rc)
601                 cFYI(1, "cifs_file_aio_write: %d rc on %p inode", rc, inode);
602
603         return written;
604 }
605
606 static loff_t cifs_llseek(struct file *file, loff_t offset, int origin)
607 {
608         /* origin == SEEK_END => we must revalidate the cached file length */
609         if (origin == SEEK_END) {
610                 int rc;
611                 struct inode *inode = file->f_path.dentry->d_inode;
612
613                 /*
614                  * We need to be sure that all dirty pages are written and the
615                  * server has the newest file length.
616                  */
617                 if (!CIFS_I(inode)->clientCanCacheRead && inode->i_mapping &&
618                     inode->i_mapping->nrpages != 0) {
619                         rc = filemap_fdatawait(inode->i_mapping);
620                         if (rc) {
621                                 mapping_set_error(inode->i_mapping, rc);
622                                 return rc;
623                         }
624                 }
625                 /*
626                  * Some applications poll for the file length in this strange
627                  * way so we must seek to end on non-oplocked files by
628                  * setting the revalidate time to zero.
629                  */
630                 CIFS_I(inode)->time = 0;
631
632                 rc = cifs_revalidate_file_attr(file);
633                 if (rc < 0)
634                         return (loff_t)rc;
635         }
636         return generic_file_llseek_unlocked(file, offset, origin);
637 }
638
639 static int cifs_setlease(struct file *file, long arg, struct file_lock **lease)
640 {
641         /* note that this is called by vfs setlease with lock_flocks held
642            to protect *lease from going away */
643         struct inode *inode = file->f_path.dentry->d_inode;
644         struct cifsFileInfo *cfile = file->private_data;
645
646         if (!(S_ISREG(inode->i_mode)))
647                 return -EINVAL;
648
649         /* check if file is oplocked */
650         if (((arg == F_RDLCK) &&
651                 (CIFS_I(inode)->clientCanCacheRead)) ||
652             ((arg == F_WRLCK) &&
653                 (CIFS_I(inode)->clientCanCacheAll)))
654                 return generic_setlease(file, arg, lease);
655         else if (tlink_tcon(cfile->tlink)->local_lease &&
656                  !CIFS_I(inode)->clientCanCacheRead)
657                 /* If the server claims to support oplock on this
658                    file, then we still need to check oplock even
659                    if the local_lease mount option is set, but there
660                    are servers which do not support oplock for which
661                    this mount option may be useful if the user
662                    knows that the file won't be changed on the server
663                    by anyone else */
664                 return generic_setlease(file, arg, lease);
665         else
666                 return -EAGAIN;
667 }
668
669 struct file_system_type cifs_fs_type = {
670         .owner = THIS_MODULE,
671         .name = "cifs",
672         .mount = cifs_do_mount,
673         .kill_sb = kill_anon_super,
674         /*  .fs_flags */
675 };
676 const struct inode_operations cifs_dir_inode_ops = {
677         .create = cifs_create,
678         .lookup = cifs_lookup,
679         .getattr = cifs_getattr,
680         .unlink = cifs_unlink,
681         .link = cifs_hardlink,
682         .mkdir = cifs_mkdir,
683         .rmdir = cifs_rmdir,
684         .rename = cifs_rename,
685         .permission = cifs_permission,
686 /*      revalidate:cifs_revalidate,   */
687         .setattr = cifs_setattr,
688         .symlink = cifs_symlink,
689         .mknod   = cifs_mknod,
690 #ifdef CONFIG_CIFS_XATTR
691         .setxattr = cifs_setxattr,
692         .getxattr = cifs_getxattr,
693         .listxattr = cifs_listxattr,
694         .removexattr = cifs_removexattr,
695 #endif
696 };
697
698 const struct inode_operations cifs_file_inode_ops = {
699 /*      revalidate:cifs_revalidate, */
700         .setattr = cifs_setattr,
701         .getattr = cifs_getattr, /* do we need this anymore? */
702         .rename = cifs_rename,
703         .permission = cifs_permission,
704 #ifdef CONFIG_CIFS_XATTR
705         .setxattr = cifs_setxattr,
706         .getxattr = cifs_getxattr,
707         .listxattr = cifs_listxattr,
708         .removexattr = cifs_removexattr,
709 #endif
710 };
711
712 const struct inode_operations cifs_symlink_inode_ops = {
713         .readlink = generic_readlink,
714         .follow_link = cifs_follow_link,
715         .put_link = cifs_put_link,
716         .permission = cifs_permission,
717         /* BB add the following two eventually */
718         /* revalidate: cifs_revalidate,
719            setattr:    cifs_notify_change, *//* BB do we need notify change */
720 #ifdef CONFIG_CIFS_XATTR
721         .setxattr = cifs_setxattr,
722         .getxattr = cifs_getxattr,
723         .listxattr = cifs_listxattr,
724         .removexattr = cifs_removexattr,
725 #endif
726 };
727
728 const struct file_operations cifs_file_ops = {
729         .read = do_sync_read,
730         .write = do_sync_write,
731         .aio_read = generic_file_aio_read,
732         .aio_write = cifs_file_aio_write,
733         .open = cifs_open,
734         .release = cifs_close,
735         .lock = cifs_lock,
736         .fsync = cifs_fsync,
737         .flush = cifs_flush,
738         .mmap  = cifs_file_mmap,
739         .splice_read = generic_file_splice_read,
740         .llseek = cifs_llseek,
741 #ifdef CONFIG_CIFS_POSIX
742         .unlocked_ioctl = cifs_ioctl,
743 #endif /* CONFIG_CIFS_POSIX */
744         .setlease = cifs_setlease,
745 };
746
747 const struct file_operations cifs_file_strict_ops = {
748         .read = do_sync_read,
749         .write = do_sync_write,
750         .aio_read = cifs_strict_readv,
751         .aio_write = cifs_strict_writev,
752         .open = cifs_open,
753         .release = cifs_close,
754         .lock = cifs_lock,
755         .fsync = cifs_strict_fsync,
756         .flush = cifs_flush,
757         .mmap = cifs_file_strict_mmap,
758         .splice_read = generic_file_splice_read,
759         .llseek = cifs_llseek,
760 #ifdef CONFIG_CIFS_POSIX
761         .unlocked_ioctl = cifs_ioctl,
762 #endif /* CONFIG_CIFS_POSIX */
763         .setlease = cifs_setlease,
764 };
765
766 const struct file_operations cifs_file_direct_ops = {
767         /* BB reevaluate whether they can be done with directio, no cache */
768         .read = do_sync_read,
769         .write = do_sync_write,
770         .aio_read = cifs_user_readv,
771         .aio_write = cifs_user_writev,
772         .open = cifs_open,
773         .release = cifs_close,
774         .lock = cifs_lock,
775         .fsync = cifs_fsync,
776         .flush = cifs_flush,
777         .mmap = cifs_file_mmap,
778         .splice_read = generic_file_splice_read,
779 #ifdef CONFIG_CIFS_POSIX
780         .unlocked_ioctl  = cifs_ioctl,
781 #endif /* CONFIG_CIFS_POSIX */
782         .llseek = cifs_llseek,
783         .setlease = cifs_setlease,
784 };
785
786 const struct file_operations cifs_file_nobrl_ops = {
787         .read = do_sync_read,
788         .write = do_sync_write,
789         .aio_read = generic_file_aio_read,
790         .aio_write = cifs_file_aio_write,
791         .open = cifs_open,
792         .release = cifs_close,
793         .fsync = cifs_fsync,
794         .flush = cifs_flush,
795         .mmap  = cifs_file_mmap,
796         .splice_read = generic_file_splice_read,
797         .llseek = cifs_llseek,
798 #ifdef CONFIG_CIFS_POSIX
799         .unlocked_ioctl = cifs_ioctl,
800 #endif /* CONFIG_CIFS_POSIX */
801         .setlease = cifs_setlease,
802 };
803
804 const struct file_operations cifs_file_strict_nobrl_ops = {
805         .read = do_sync_read,
806         .write = do_sync_write,
807         .aio_read = cifs_strict_readv,
808         .aio_write = cifs_strict_writev,
809         .open = cifs_open,
810         .release = cifs_close,
811         .fsync = cifs_strict_fsync,
812         .flush = cifs_flush,
813         .mmap = cifs_file_strict_mmap,
814         .splice_read = generic_file_splice_read,
815         .llseek = cifs_llseek,
816 #ifdef CONFIG_CIFS_POSIX
817         .unlocked_ioctl = cifs_ioctl,
818 #endif /* CONFIG_CIFS_POSIX */
819         .setlease = cifs_setlease,
820 };
821
822 const struct file_operations cifs_file_direct_nobrl_ops = {
823         /* BB reevaluate whether they can be done with directio, no cache */
824         .read = do_sync_read,
825         .write = do_sync_write,
826         .aio_read = cifs_user_readv,
827         .aio_write = cifs_user_writev,
828         .open = cifs_open,
829         .release = cifs_close,
830         .fsync = cifs_fsync,
831         .flush = cifs_flush,
832         .mmap = cifs_file_mmap,
833         .splice_read = generic_file_splice_read,
834 #ifdef CONFIG_CIFS_POSIX
835         .unlocked_ioctl  = cifs_ioctl,
836 #endif /* CONFIG_CIFS_POSIX */
837         .llseek = cifs_llseek,
838         .setlease = cifs_setlease,
839 };
840
841 const struct file_operations cifs_dir_ops = {
842         .readdir = cifs_readdir,
843         .release = cifs_closedir,
844         .read    = generic_read_dir,
845         .unlocked_ioctl  = cifs_ioctl,
846         .llseek = generic_file_llseek,
847 };
848
849 static void
850 cifs_init_once(void *inode)
851 {
852         struct cifsInodeInfo *cifsi = inode;
853
854         inode_init_once(&cifsi->vfs_inode);
855         INIT_LIST_HEAD(&cifsi->lockList);
856 }
857
858 static int
859 cifs_init_inodecache(void)
860 {
861         cifs_inode_cachep = kmem_cache_create("cifs_inode_cache",
862                                               sizeof(struct cifsInodeInfo),
863                                               0, (SLAB_RECLAIM_ACCOUNT|
864                                                 SLAB_MEM_SPREAD),
865                                               cifs_init_once);
866         if (cifs_inode_cachep == NULL)
867                 return -ENOMEM;
868
869         return 0;
870 }
871
872 static void
873 cifs_destroy_inodecache(void)
874 {
875         kmem_cache_destroy(cifs_inode_cachep);
876 }
877
878 static int
879 cifs_init_request_bufs(void)
880 {
881         if (CIFSMaxBufSize < 8192) {
882         /* Buffer size can not be smaller than 2 * PATH_MAX since maximum
883         Unicode path name has to fit in any SMB/CIFS path based frames */
884                 CIFSMaxBufSize = 8192;
885         } else if (CIFSMaxBufSize > 1024*127) {
886                 CIFSMaxBufSize = 1024 * 127;
887         } else {
888                 CIFSMaxBufSize &= 0x1FE00; /* Round size to even 512 byte mult*/
889         }
890 /*      cERROR(1, "CIFSMaxBufSize %d 0x%x",CIFSMaxBufSize,CIFSMaxBufSize); */
891         cifs_req_cachep = kmem_cache_create("cifs_request",
892                                             CIFSMaxBufSize +
893                                             MAX_CIFS_HDR_SIZE, 0,
894                                             SLAB_HWCACHE_ALIGN, NULL);
895         if (cifs_req_cachep == NULL)
896                 return -ENOMEM;
897
898         if (cifs_min_rcv < 1)
899                 cifs_min_rcv = 1;
900         else if (cifs_min_rcv > 64) {
901                 cifs_min_rcv = 64;
902                 cERROR(1, "cifs_min_rcv set to maximum (64)");
903         }
904
905         cifs_req_poolp = mempool_create_slab_pool(cifs_min_rcv,
906                                                   cifs_req_cachep);
907
908         if (cifs_req_poolp == NULL) {
909                 kmem_cache_destroy(cifs_req_cachep);
910                 return -ENOMEM;
911         }
912         /* MAX_CIFS_SMALL_BUFFER_SIZE bytes is enough for most SMB responses and
913         almost all handle based requests (but not write response, nor is it
914         sufficient for path based requests).  A smaller size would have
915         been more efficient (compacting multiple slab items on one 4k page)
916         for the case in which debug was on, but this larger size allows
917         more SMBs to use small buffer alloc and is still much more
918         efficient to alloc 1 per page off the slab compared to 17K (5page)
919         alloc of large cifs buffers even when page debugging is on */
920         cifs_sm_req_cachep = kmem_cache_create("cifs_small_rq",
921                         MAX_CIFS_SMALL_BUFFER_SIZE, 0, SLAB_HWCACHE_ALIGN,
922                         NULL);
923         if (cifs_sm_req_cachep == NULL) {
924                 mempool_destroy(cifs_req_poolp);
925                 kmem_cache_destroy(cifs_req_cachep);
926                 return -ENOMEM;
927         }
928
929         if (cifs_min_small < 2)
930                 cifs_min_small = 2;
931         else if (cifs_min_small > 256) {
932                 cifs_min_small = 256;
933                 cFYI(1, "cifs_min_small set to maximum (256)");
934         }
935
936         cifs_sm_req_poolp = mempool_create_slab_pool(cifs_min_small,
937                                                      cifs_sm_req_cachep);
938
939         if (cifs_sm_req_poolp == NULL) {
940                 mempool_destroy(cifs_req_poolp);
941                 kmem_cache_destroy(cifs_req_cachep);
942                 kmem_cache_destroy(cifs_sm_req_cachep);
943                 return -ENOMEM;
944         }
945
946         return 0;
947 }
948
949 static void
950 cifs_destroy_request_bufs(void)
951 {
952         mempool_destroy(cifs_req_poolp);
953         kmem_cache_destroy(cifs_req_cachep);
954         mempool_destroy(cifs_sm_req_poolp);
955         kmem_cache_destroy(cifs_sm_req_cachep);
956 }
957
958 static int
959 cifs_init_mids(void)
960 {
961         cifs_mid_cachep = kmem_cache_create("cifs_mpx_ids",
962                                             sizeof(struct mid_q_entry), 0,
963                                             SLAB_HWCACHE_ALIGN, NULL);
964         if (cifs_mid_cachep == NULL)
965                 return -ENOMEM;
966
967         /* 3 is a reasonable minimum number of simultaneous operations */
968         cifs_mid_poolp = mempool_create_slab_pool(3, cifs_mid_cachep);
969         if (cifs_mid_poolp == NULL) {
970                 kmem_cache_destroy(cifs_mid_cachep);
971                 return -ENOMEM;
972         }
973
974         return 0;
975 }
976
977 static void
978 cifs_destroy_mids(void)
979 {
980         mempool_destroy(cifs_mid_poolp);
981         kmem_cache_destroy(cifs_mid_cachep);
982 }
983
984 static int __init
985 init_cifs(void)
986 {
987         int rc = 0;
988         cifs_proc_init();
989         INIT_LIST_HEAD(&cifs_tcp_ses_list);
990 #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* unused temporarily */
991         INIT_LIST_HEAD(&GlobalDnotifyReqList);
992         INIT_LIST_HEAD(&GlobalDnotifyRsp_Q);
993 #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */
994 /*
995  *  Initialize Global counters
996  */
997         atomic_set(&sesInfoAllocCount, 0);
998         atomic_set(&tconInfoAllocCount, 0);
999         atomic_set(&tcpSesAllocCount, 0);
1000         atomic_set(&tcpSesReconnectCount, 0);
1001         atomic_set(&tconInfoReconnectCount, 0);
1002
1003         atomic_set(&bufAllocCount, 0);
1004         atomic_set(&smBufAllocCount, 0);
1005 #ifdef CONFIG_CIFS_STATS2
1006         atomic_set(&totBufAllocCount, 0);
1007         atomic_set(&totSmBufAllocCount, 0);
1008 #endif /* CONFIG_CIFS_STATS2 */
1009
1010         atomic_set(&midCount, 0);
1011         GlobalCurrentXid = 0;
1012         GlobalTotalActiveXid = 0;
1013         GlobalMaxActiveXid = 0;
1014         spin_lock_init(&cifs_tcp_ses_lock);
1015         spin_lock_init(&cifs_file_list_lock);
1016         spin_lock_init(&GlobalMid_Lock);
1017
1018         if (cifs_max_pending < 2) {
1019                 cifs_max_pending = 2;
1020                 cFYI(1, "cifs_max_pending set to min of 2");
1021         } else if (cifs_max_pending > 256) {
1022                 cifs_max_pending = 256;
1023                 cFYI(1, "cifs_max_pending set to max of 256");
1024         }
1025
1026         rc = cifs_fscache_register();
1027         if (rc)
1028                 goto out_clean_proc;
1029
1030         rc = cifs_init_inodecache();
1031         if (rc)
1032                 goto out_unreg_fscache;
1033
1034         rc = cifs_init_mids();
1035         if (rc)
1036                 goto out_destroy_inodecache;
1037
1038         rc = cifs_init_request_bufs();
1039         if (rc)
1040                 goto out_destroy_mids;
1041
1042 #ifdef CONFIG_CIFS_UPCALL
1043         rc = register_key_type(&cifs_spnego_key_type);
1044         if (rc)
1045                 goto out_destroy_request_bufs;
1046 #endif /* CONFIG_CIFS_UPCALL */
1047
1048 #ifdef CONFIG_CIFS_ACL
1049         rc = init_cifs_idmap();
1050         if (rc)
1051                 goto out_register_key_type;
1052 #endif /* CONFIG_CIFS_ACL */
1053
1054         rc = register_filesystem(&cifs_fs_type);
1055         if (rc)
1056                 goto out_init_cifs_idmap;
1057
1058         return 0;
1059
1060 out_init_cifs_idmap:
1061 #ifdef CONFIG_CIFS_ACL
1062         exit_cifs_idmap();
1063 out_register_key_type:
1064 #endif
1065 #ifdef CONFIG_CIFS_UPCALL
1066         unregister_key_type(&cifs_spnego_key_type);
1067 out_destroy_request_bufs:
1068 #endif
1069         cifs_destroy_request_bufs();
1070 out_destroy_mids:
1071         cifs_destroy_mids();
1072 out_destroy_inodecache:
1073         cifs_destroy_inodecache();
1074 out_unreg_fscache:
1075         cifs_fscache_unregister();
1076 out_clean_proc:
1077         cifs_proc_clean();
1078         return rc;
1079 }
1080
1081 static void __exit
1082 exit_cifs(void)
1083 {
1084         cFYI(DBG2, "exit_cifs");
1085         cifs_proc_clean();
1086         cifs_fscache_unregister();
1087 #ifdef CONFIG_CIFS_DFS_UPCALL
1088         cifs_dfs_release_automount_timer();
1089 #endif
1090 #ifdef CONFIG_CIFS_ACL
1091         cifs_destroy_idmaptrees();
1092         exit_cifs_idmap();
1093 #endif
1094 #ifdef CONFIG_CIFS_UPCALL
1095         unregister_key_type(&cifs_spnego_key_type);
1096 #endif
1097         unregister_filesystem(&cifs_fs_type);
1098         cifs_destroy_inodecache();
1099         cifs_destroy_mids();
1100         cifs_destroy_request_bufs();
1101 }
1102
1103 MODULE_AUTHOR("Steve French <sfrench@us.ibm.com>");
1104 MODULE_LICENSE("GPL");  /* combination of LGPL + GPL source behaves as GPL */
1105 MODULE_DESCRIPTION
1106     ("VFS to access servers complying with the SNIA CIFS Specification "
1107      "e.g. Samba and Windows");
1108 MODULE_VERSION(CIFS_VERSION);
1109 module_init(init_cifs)
1110 module_exit(exit_cifs)