Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/file.c |
| 3 | * |
| 4 | * vfs operations that deal with files |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 5 | * |
Steve French | f19159d | 2010-04-21 04:12:10 +0000 | [diff] [blame] | 6 | * Copyright (C) International Business Machines Corp., 2002,2010 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Author(s): Steve French (sfrench@us.ibm.com) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 8 | * Jeremy Allison (jra@samba.org) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * This library is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as published |
| 12 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 18 | * the GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public License |
| 21 | * along with this library; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | */ |
| 24 | #include <linux/fs.h> |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 25 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/stat.h> |
| 27 | #include <linux/fcntl.h> |
| 28 | #include <linux/pagemap.h> |
| 29 | #include <linux/pagevec.h> |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 30 | #include <linux/writeback.h> |
Andrew Morton | 6f88cc2 | 2006-12-10 02:19:44 -0800 | [diff] [blame] | 31 | #include <linux/task_io_accounting_ops.h> |
Steve French | 23e7dd7 | 2005-10-20 13:44:56 -0700 | [diff] [blame] | 32 | #include <linux/delay.h> |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 33 | #include <linux/mount.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 35 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <asm/div64.h> |
| 37 | #include "cifsfs.h" |
| 38 | #include "cifspdu.h" |
| 39 | #include "cifsglob.h" |
| 40 | #include "cifsproto.h" |
| 41 | #include "cifs_unicode.h" |
| 42 | #include "cifs_debug.h" |
| 43 | #include "cifs_fs_sb.h" |
Suresh Jayaraman | 9451a9a | 2010-07-05 18:12:45 +0530 | [diff] [blame] | 44 | #include "fscache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Steve French | 07b92d0 | 2013-02-18 10:34:26 -0600 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | static inline int cifs_convert_flags(unsigned int flags) |
| 48 | { |
| 49 | if ((flags & O_ACCMODE) == O_RDONLY) |
| 50 | return GENERIC_READ; |
| 51 | else if ((flags & O_ACCMODE) == O_WRONLY) |
| 52 | return GENERIC_WRITE; |
| 53 | else if ((flags & O_ACCMODE) == O_RDWR) { |
| 54 | /* GENERIC_ALL is too much permission to request |
| 55 | can cause unnecessary access denied on create */ |
| 56 | /* return GENERIC_ALL; */ |
| 57 | return (GENERIC_READ | GENERIC_WRITE); |
| 58 | } |
| 59 | |
Jeff Layton | e10f7b5 | 2008-05-14 10:21:33 -0700 | [diff] [blame] | 60 | return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | |
| 61 | FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA | |
| 62 | FILE_READ_DATA); |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 63 | } |
Jeff Layton | e10f7b5 | 2008-05-14 10:21:33 -0700 | [diff] [blame] | 64 | |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 65 | static u32 cifs_posix_convert_flags(unsigned int flags) |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 66 | { |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 67 | u32 posix_flags = 0; |
Jeff Layton | e10f7b5 | 2008-05-14 10:21:33 -0700 | [diff] [blame] | 68 | |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 69 | if ((flags & O_ACCMODE) == O_RDONLY) |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 70 | posix_flags = SMB_O_RDONLY; |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 71 | else if ((flags & O_ACCMODE) == O_WRONLY) |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 72 | posix_flags = SMB_O_WRONLY; |
| 73 | else if ((flags & O_ACCMODE) == O_RDWR) |
| 74 | posix_flags = SMB_O_RDWR; |
| 75 | |
Steve French | 07b92d0 | 2013-02-18 10:34:26 -0600 | [diff] [blame] | 76 | if (flags & O_CREAT) { |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 77 | posix_flags |= SMB_O_CREAT; |
Steve French | 07b92d0 | 2013-02-18 10:34:26 -0600 | [diff] [blame] | 78 | if (flags & O_EXCL) |
| 79 | posix_flags |= SMB_O_EXCL; |
| 80 | } else if (flags & O_EXCL) |
| 81 | cFYI(1, "Application %s pid %d has incorrectly set O_EXCL flag" |
| 82 | "but not O_CREAT on file open. Ignoring O_EXCL", |
| 83 | current->comm, current->tgid); |
| 84 | |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 85 | if (flags & O_TRUNC) |
| 86 | posix_flags |= SMB_O_TRUNC; |
| 87 | /* be safe and imply O_SYNC for O_DSYNC */ |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 88 | if (flags & O_DSYNC) |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 89 | posix_flags |= SMB_O_SYNC; |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 90 | if (flags & O_DIRECTORY) |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 91 | posix_flags |= SMB_O_DIRECTORY; |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 92 | if (flags & O_NOFOLLOW) |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 93 | posix_flags |= SMB_O_NOFOLLOW; |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 94 | if (flags & O_DIRECT) |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 95 | posix_flags |= SMB_O_DIRECT; |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 96 | |
| 97 | return posix_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static inline int cifs_get_disposition(unsigned int flags) |
| 101 | { |
| 102 | if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) |
| 103 | return FILE_CREATE; |
| 104 | else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) |
| 105 | return FILE_OVERWRITE_IF; |
| 106 | else if ((flags & O_CREAT) == O_CREAT) |
| 107 | return FILE_OPEN_IF; |
Steve French | 55aa2e0 | 2006-05-30 18:09:31 +0000 | [diff] [blame] | 108 | else if ((flags & O_TRUNC) == O_TRUNC) |
| 109 | return FILE_OVERWRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | else |
| 111 | return FILE_OPEN; |
| 112 | } |
| 113 | |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 114 | int cifs_posix_open(char *full_path, struct inode **pinode, |
| 115 | struct super_block *sb, int mode, unsigned int f_flags, |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 116 | __u32 *poplock, __u16 *pnetfid, unsigned int xid) |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 117 | { |
| 118 | int rc; |
| 119 | FILE_UNIX_BASIC_INFO *presp_data; |
| 120 | __u32 posix_flags = 0; |
| 121 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 122 | struct cifs_fattr fattr; |
| 123 | struct tcon_link *tlink; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 124 | struct cifs_tcon *tcon; |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 125 | |
| 126 | cFYI(1, "posix open %s", full_path); |
| 127 | |
| 128 | presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); |
| 129 | if (presp_data == NULL) |
| 130 | return -ENOMEM; |
| 131 | |
| 132 | tlink = cifs_sb_tlink(cifs_sb); |
| 133 | if (IS_ERR(tlink)) { |
| 134 | rc = PTR_ERR(tlink); |
| 135 | goto posix_open_ret; |
| 136 | } |
| 137 | |
| 138 | tcon = tlink_tcon(tlink); |
| 139 | mode &= ~current_umask(); |
| 140 | |
| 141 | posix_flags = cifs_posix_convert_flags(f_flags); |
| 142 | rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data, |
| 143 | poplock, full_path, cifs_sb->local_nls, |
| 144 | cifs_sb->mnt_cifs_flags & |
| 145 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 146 | cifs_put_tlink(tlink); |
| 147 | |
| 148 | if (rc) |
| 149 | goto posix_open_ret; |
| 150 | |
| 151 | if (presp_data->Type == cpu_to_le32(-1)) |
| 152 | goto posix_open_ret; /* open ok, caller does qpathinfo */ |
| 153 | |
| 154 | if (!pinode) |
| 155 | goto posix_open_ret; /* caller does not need info */ |
| 156 | |
| 157 | cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb); |
| 158 | |
| 159 | /* get new inode and set it up */ |
| 160 | if (*pinode == NULL) { |
| 161 | cifs_fill_uniqueid(sb, &fattr); |
| 162 | *pinode = cifs_iget(sb, &fattr); |
| 163 | if (!*pinode) { |
| 164 | rc = -ENOMEM; |
| 165 | goto posix_open_ret; |
| 166 | } |
| 167 | } else { |
| 168 | cifs_fattr_to_inode(*pinode, &fattr); |
| 169 | } |
| 170 | |
| 171 | posix_open_ret: |
| 172 | kfree(presp_data); |
| 173 | return rc; |
| 174 | } |
| 175 | |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 176 | static int |
| 177 | cifs_nt_open(char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb, |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 178 | struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock, |
| 179 | struct cifs_fid *fid, unsigned int xid) |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 180 | { |
| 181 | int rc; |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 182 | int desired_access; |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 183 | int disposition; |
Shirish Pargaonkar | 3d3ea8e | 2011-09-26 09:56:44 -0500 | [diff] [blame] | 184 | int create_options = CREATE_NOT_DIR; |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 185 | FILE_ALL_INFO *buf; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 186 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 187 | |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 188 | if (!server->ops->open) |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 189 | return -ENOSYS; |
| 190 | |
| 191 | desired_access = cifs_convert_flags(f_flags); |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 192 | |
| 193 | /********************************************************************* |
| 194 | * open flag mapping table: |
| 195 | * |
| 196 | * POSIX Flag CIFS Disposition |
| 197 | * ---------- ---------------- |
| 198 | * O_CREAT FILE_OPEN_IF |
| 199 | * O_CREAT | O_EXCL FILE_CREATE |
| 200 | * O_CREAT | O_TRUNC FILE_OVERWRITE_IF |
| 201 | * O_TRUNC FILE_OVERWRITE |
| 202 | * none of the above FILE_OPEN |
| 203 | * |
| 204 | * Note that there is not a direct match between disposition |
| 205 | * FILE_SUPERSEDE (ie create whether or not file exists although |
| 206 | * O_CREAT | O_TRUNC is similar but truncates the existing |
| 207 | * file rather than creating a new file as FILE_SUPERSEDE does |
| 208 | * (which uses the attributes / metadata passed in on open call) |
| 209 | *? |
| 210 | *? O_SYNC is a reasonable match to CIFS writethrough flag |
| 211 | *? and the read write flags match reasonably. O_LARGEFILE |
| 212 | *? is irrelevant because largefile support is always used |
| 213 | *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY, |
| 214 | * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation |
| 215 | *********************************************************************/ |
| 216 | |
| 217 | disposition = cifs_get_disposition(f_flags); |
| 218 | |
| 219 | /* BB pass O_SYNC flag through on file attributes .. BB */ |
| 220 | |
| 221 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
| 222 | if (!buf) |
| 223 | return -ENOMEM; |
| 224 | |
Shirish Pargaonkar | 3d3ea8e | 2011-09-26 09:56:44 -0500 | [diff] [blame] | 225 | if (backup_cred(cifs_sb)) |
| 226 | create_options |= CREATE_OPEN_BACKUP_INTENT; |
| 227 | |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 228 | rc = server->ops->open(xid, tcon, full_path, disposition, |
| 229 | desired_access, create_options, fid, oplock, buf, |
| 230 | cifs_sb); |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 231 | |
| 232 | if (rc) |
| 233 | goto out; |
| 234 | |
| 235 | if (tcon->unix_ext) |
| 236 | rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb, |
| 237 | xid); |
| 238 | else |
| 239 | rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb, |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 240 | xid, &fid->netfid); |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 241 | |
| 242 | out: |
| 243 | kfree(buf); |
| 244 | return rc; |
| 245 | } |
| 246 | |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 247 | static bool |
| 248 | cifs_has_mand_locks(struct cifsInodeInfo *cinode) |
| 249 | { |
| 250 | struct cifs_fid_locks *cur; |
| 251 | bool has_locks = false; |
| 252 | |
| 253 | down_read(&cinode->lock_sem); |
| 254 | list_for_each_entry(cur, &cinode->llist, llist) { |
| 255 | if (!list_empty(&cur->locks)) { |
| 256 | has_locks = true; |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | up_read(&cinode->lock_sem); |
| 261 | return has_locks; |
| 262 | } |
| 263 | |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 264 | struct cifsFileInfo * |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 265 | cifs_new_fileinfo(struct cifs_fid *fid, struct file *file, |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 266 | struct tcon_link *tlink, __u32 oplock) |
| 267 | { |
| 268 | struct dentry *dentry = file->f_path.dentry; |
| 269 | struct inode *inode = dentry->d_inode; |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 270 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
| 271 | struct cifsFileInfo *cfile; |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 272 | struct cifs_fid_locks *fdlocks; |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 273 | struct cifs_tcon *tcon = tlink_tcon(tlink); |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 274 | struct TCP_Server_Info *server = tcon->ses->server; |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 275 | |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 276 | cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); |
| 277 | if (cfile == NULL) |
| 278 | return cfile; |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 279 | |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 280 | fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL); |
| 281 | if (!fdlocks) { |
| 282 | kfree(cfile); |
| 283 | return NULL; |
| 284 | } |
| 285 | |
| 286 | INIT_LIST_HEAD(&fdlocks->locks); |
| 287 | fdlocks->cfile = cfile; |
| 288 | cfile->llist = fdlocks; |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 289 | down_write(&cinode->lock_sem); |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 290 | list_add(&fdlocks->llist, &cinode->llist); |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 291 | up_write(&cinode->lock_sem); |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 292 | |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 293 | cfile->count = 1; |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 294 | cfile->pid = current->tgid; |
| 295 | cfile->uid = current_fsuid(); |
| 296 | cfile->dentry = dget(dentry); |
| 297 | cfile->f_flags = file->f_flags; |
| 298 | cfile->invalidHandle = false; |
| 299 | cfile->tlink = cifs_get_tlink(tlink); |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 300 | INIT_WORK(&cfile->oplock_break, cifs_oplock_break); |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 301 | mutex_init(&cfile->fh_mutex); |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 302 | |
Mateusz Guzik | 24261fc | 2013-03-08 16:30:03 +0100 | [diff] [blame] | 303 | cifs_sb_active(inode->i_sb); |
| 304 | |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 305 | /* |
| 306 | * If the server returned a read oplock and we have mandatory brlocks, |
| 307 | * set oplock level to None. |
| 308 | */ |
| 309 | if (oplock == server->vals->oplock_read && |
| 310 | cifs_has_mand_locks(cinode)) { |
| 311 | cFYI(1, "Reset oplock val from read to None due to mand locks"); |
| 312 | oplock = 0; |
| 313 | } |
| 314 | |
Jeff Layton | 4477288 | 2010-10-15 15:34:03 -0400 | [diff] [blame] | 315 | spin_lock(&cifs_file_list_lock); |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 316 | if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock) |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 317 | oplock = fid->pending_open->oplock; |
| 318 | list_del(&fid->pending_open->olist); |
| 319 | |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 320 | server->ops->set_fid(cfile, fid, oplock); |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 321 | |
| 322 | list_add(&cfile->tlist, &tcon->openFileList); |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 323 | /* if readable file instance put first in list*/ |
| 324 | if (file->f_mode & FMODE_READ) |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 325 | list_add(&cfile->flist, &cinode->openFileList); |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 326 | else |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 327 | list_add_tail(&cfile->flist, &cinode->openFileList); |
Jeff Layton | 4477288 | 2010-10-15 15:34:03 -0400 | [diff] [blame] | 328 | spin_unlock(&cifs_file_list_lock); |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 329 | |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 330 | file->private_data = cfile; |
| 331 | return cfile; |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 332 | } |
| 333 | |
Jeff Layton | 764a1b1 | 2012-07-25 14:59:54 -0400 | [diff] [blame] | 334 | struct cifsFileInfo * |
| 335 | cifsFileInfo_get(struct cifsFileInfo *cifs_file) |
| 336 | { |
| 337 | spin_lock(&cifs_file_list_lock); |
| 338 | cifsFileInfo_get_locked(cifs_file); |
| 339 | spin_unlock(&cifs_file_list_lock); |
| 340 | return cifs_file; |
| 341 | } |
| 342 | |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 343 | /* |
| 344 | * Release a reference on the file private data. This may involve closing |
Jeff Layton | 5f6dbc9 | 2010-10-15 15:34:06 -0400 | [diff] [blame] | 345 | * the filehandle out on the server. Must be called without holding |
| 346 | * cifs_file_list_lock. |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 347 | */ |
Jeff Layton | b33879a | 2010-10-15 15:34:04 -0400 | [diff] [blame] | 348 | void cifsFileInfo_put(struct cifsFileInfo *cifs_file) |
| 349 | { |
Pavel Shilovsky | e66673e | 2010-11-02 12:00:42 +0300 | [diff] [blame] | 350 | struct inode *inode = cifs_file->dentry->d_inode; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 351 | struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink); |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 352 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | e66673e | 2010-11-02 12:00:42 +0300 | [diff] [blame] | 353 | struct cifsInodeInfo *cifsi = CIFS_I(inode); |
Mateusz Guzik | 24261fc | 2013-03-08 16:30:03 +0100 | [diff] [blame] | 354 | struct super_block *sb = inode->i_sb; |
| 355 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 356 | struct cifsLockInfo *li, *tmp; |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 357 | struct cifs_fid fid; |
| 358 | struct cifs_pending_open open; |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 359 | |
| 360 | spin_lock(&cifs_file_list_lock); |
Jeff Layton | 5f6dbc9 | 2010-10-15 15:34:06 -0400 | [diff] [blame] | 361 | if (--cifs_file->count > 0) { |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 362 | spin_unlock(&cifs_file_list_lock); |
| 363 | return; |
Jeff Layton | b33879a | 2010-10-15 15:34:04 -0400 | [diff] [blame] | 364 | } |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 365 | |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 366 | if (server->ops->get_lease_key) |
| 367 | server->ops->get_lease_key(inode, &fid); |
| 368 | |
| 369 | /* store open in pending opens to make sure we don't miss lease break */ |
| 370 | cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open); |
| 371 | |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 372 | /* remove it from the lists */ |
| 373 | list_del(&cifs_file->flist); |
| 374 | list_del(&cifs_file->tlist); |
| 375 | |
| 376 | if (list_empty(&cifsi->openFileList)) { |
| 377 | cFYI(1, "closing last open instance for inode %p", |
| 378 | cifs_file->dentry->d_inode); |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 379 | /* |
| 380 | * In strict cache mode we need invalidate mapping on the last |
| 381 | * close because it may cause a error when we open this file |
| 382 | * again and get at least level II oplock. |
| 383 | */ |
Pavel Shilovsky | 4f8ba8a | 2010-11-21 22:36:12 +0300 | [diff] [blame] | 384 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) |
| 385 | CIFS_I(inode)->invalid_mapping = true; |
Pavel Shilovsky | c672362 | 2010-11-03 10:58:57 +0300 | [diff] [blame] | 386 | cifs_set_oplock_level(cifsi, 0); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 387 | } |
| 388 | spin_unlock(&cifs_file_list_lock); |
| 389 | |
Jeff Layton | ad63594 | 2011-07-26 12:20:17 -0400 | [diff] [blame] | 390 | cancel_work_sync(&cifs_file->oplock_break); |
| 391 | |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 392 | if (!tcon->need_reconnect && !cifs_file->invalidHandle) { |
Pavel Shilovsky | 0ff78a2 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 393 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 394 | unsigned int xid; |
Pavel Shilovsky | 0ff78a2 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 395 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 396 | xid = get_xid(); |
Pavel Shilovsky | 0ff78a2 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 397 | if (server->ops->close) |
Pavel Shilovsky | 760ad0c | 2012-09-25 11:00:07 +0400 | [diff] [blame] | 398 | server->ops->close(xid, tcon, &cifs_file->fid); |
| 399 | _free_xid(xid); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 402 | cifs_del_pending_open(&open); |
| 403 | |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 404 | /* |
| 405 | * Delete any outstanding lock records. We'll lose them when the file |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 406 | * is closed anyway. |
| 407 | */ |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 408 | down_write(&cifsi->lock_sem); |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 409 | list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) { |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 410 | list_del(&li->llist); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 411 | cifs_del_lock_waiters(li); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 412 | kfree(li); |
| 413 | } |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 414 | list_del(&cifs_file->llist->llist); |
| 415 | kfree(cifs_file->llist); |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 416 | up_write(&cifsi->lock_sem); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 417 | |
| 418 | cifs_put_tlink(cifs_file->tlink); |
| 419 | dput(cifs_file->dentry); |
Mateusz Guzik | 24261fc | 2013-03-08 16:30:03 +0100 | [diff] [blame] | 420 | cifs_sb_deactive(sb); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 421 | kfree(cifs_file); |
Jeff Layton | b33879a | 2010-10-15 15:34:04 -0400 | [diff] [blame] | 422 | } |
| 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | int cifs_open(struct inode *inode, struct file *file) |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
| 427 | int rc = -EACCES; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 428 | unsigned int xid; |
Jeff Layton | 590a3fe | 2009-09-12 11:54:28 -0400 | [diff] [blame] | 429 | __u32 oplock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | struct cifs_sb_info *cifs_sb; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 431 | struct TCP_Server_Info *server; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 432 | struct cifs_tcon *tcon; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 433 | struct tcon_link *tlink; |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 434 | struct cifsFileInfo *cfile = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | char *full_path = NULL; |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 436 | bool posix_open_ok = false; |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 437 | struct cifs_fid fid; |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 438 | struct cifs_pending_open open; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 440 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
| 442 | cifs_sb = CIFS_SB(inode->i_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 443 | tlink = cifs_sb_tlink(cifs_sb); |
| 444 | if (IS_ERR(tlink)) { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 445 | free_xid(xid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 446 | return PTR_ERR(tlink); |
| 447 | } |
| 448 | tcon = tlink_tcon(tlink); |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 449 | server = tcon->ses->server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 451 | full_path = build_path_from_dentry(file->f_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 453 | rc = -ENOMEM; |
Jeff Layton | 232341b | 2010-08-05 13:58:38 -0400 | [diff] [blame] | 454 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 457 | cFYI(1, "inode = 0x%p file flags are 0x%x for %s", |
| 458 | inode, file->f_flags, full_path); |
Steve French | 276a74a | 2009-03-03 18:00:34 +0000 | [diff] [blame] | 459 | |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 460 | if (server->oplocks) |
Steve French | 276a74a | 2009-03-03 18:00:34 +0000 | [diff] [blame] | 461 | oplock = REQ_OPLOCK; |
| 462 | else |
| 463 | oplock = 0; |
| 464 | |
Steve French | 64cc2c6 | 2009-03-04 19:54:08 +0000 | [diff] [blame] | 465 | if (!tcon->broken_posix_open && tcon->unix_ext && |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 466 | cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
| 467 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
Steve French | 276a74a | 2009-03-03 18:00:34 +0000 | [diff] [blame] | 468 | /* can not refresh inode info since size could be stale */ |
Jeff Layton | 2422f67 | 2010-06-16 13:40:16 -0400 | [diff] [blame] | 469 | rc = cifs_posix_open(full_path, &inode, inode->i_sb, |
Steve French | fa588e0 | 2010-04-22 19:21:55 +0000 | [diff] [blame] | 470 | cifs_sb->mnt_file_mode /* ignored */, |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 471 | file->f_flags, &oplock, &fid.netfid, xid); |
Steve French | 276a74a | 2009-03-03 18:00:34 +0000 | [diff] [blame] | 472 | if (rc == 0) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 473 | cFYI(1, "posix open succeeded"); |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 474 | posix_open_ok = true; |
Steve French | 64cc2c6 | 2009-03-04 19:54:08 +0000 | [diff] [blame] | 475 | } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { |
| 476 | if (tcon->ses->serverNOS) |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 477 | cERROR(1, "server %s of type %s returned" |
Steve French | 64cc2c6 | 2009-03-04 19:54:08 +0000 | [diff] [blame] | 478 | " unexpected error on SMB posix open" |
| 479 | ", disabling posix open support." |
| 480 | " Check if server update available.", |
| 481 | tcon->ses->serverName, |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 482 | tcon->ses->serverNOS); |
Steve French | 64cc2c6 | 2009-03-04 19:54:08 +0000 | [diff] [blame] | 483 | tcon->broken_posix_open = true; |
Steve French | 276a74a | 2009-03-03 18:00:34 +0000 | [diff] [blame] | 484 | } else if ((rc != -EIO) && (rc != -EREMOTE) && |
| 485 | (rc != -EOPNOTSUPP)) /* path not found or net err */ |
| 486 | goto out; |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 487 | /* |
| 488 | * Else fallthrough to retry open the old way on network i/o |
| 489 | * or DFS errors. |
| 490 | */ |
Steve French | 276a74a | 2009-03-03 18:00:34 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 493 | if (server->ops->get_lease_key) |
| 494 | server->ops->get_lease_key(inode, &fid); |
| 495 | |
| 496 | cifs_add_pending_open(&fid, tlink, &open); |
| 497 | |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 498 | if (!posix_open_ok) { |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 499 | if (server->ops->get_lease_key) |
| 500 | server->ops->get_lease_key(inode, &fid); |
| 501 | |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 502 | rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 503 | file->f_flags, &oplock, &fid, xid); |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 504 | if (rc) { |
| 505 | cifs_del_pending_open(&open); |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 506 | goto out; |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 507 | } |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 508 | } |
Jeff Layton | 47c78b7 | 2010-06-16 13:40:17 -0400 | [diff] [blame] | 509 | |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 510 | cfile = cifs_new_fileinfo(&fid, file, tlink, oplock); |
| 511 | if (cfile == NULL) { |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 512 | if (server->ops->close) |
| 513 | server->ops->close(xid, tcon, &fid); |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 514 | cifs_del_pending_open(&open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | rc = -ENOMEM; |
| 516 | goto out; |
| 517 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
Suresh Jayaraman | 9451a9a | 2010-07-05 18:12:45 +0530 | [diff] [blame] | 519 | cifs_fscache_set_inode_cookie(inode, file); |
| 520 | |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 521 | if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) { |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 522 | /* |
| 523 | * Time to set mode which we can not set earlier due to |
| 524 | * problems creating new read-only files. |
| 525 | */ |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 526 | struct cifs_unix_set_info_args args = { |
| 527 | .mode = inode->i_mode, |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 528 | .uid = INVALID_UID, /* no change */ |
| 529 | .gid = INVALID_GID, /* no change */ |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 530 | .ctime = NO_CHANGE_64, |
| 531 | .atime = NO_CHANGE_64, |
| 532 | .mtime = NO_CHANGE_64, |
| 533 | .device = 0, |
| 534 | }; |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 535 | CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid, |
| 536 | cfile->pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | kfree(full_path); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 541 | free_xid(xid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 542 | cifs_put_tlink(tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | return rc; |
| 544 | } |
| 545 | |
Pavel Shilovsky | f152fd5 | 2012-11-22 17:10:57 +0400 | [diff] [blame] | 546 | static int cifs_push_posix_locks(struct cifsFileInfo *cfile); |
| 547 | |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 548 | /* |
| 549 | * Try to reacquire byte range locks that were released when session |
Pavel Shilovsky | f152fd5 | 2012-11-22 17:10:57 +0400 | [diff] [blame] | 550 | * to server was lost. |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 551 | */ |
Pavel Shilovsky | f152fd5 | 2012-11-22 17:10:57 +0400 | [diff] [blame] | 552 | static int |
| 553 | cifs_relock_file(struct cifsFileInfo *cfile) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | { |
Pavel Shilovsky | f152fd5 | 2012-11-22 17:10:57 +0400 | [diff] [blame] | 555 | struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); |
| 556 | struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode); |
| 557 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | int rc = 0; |
| 559 | |
Pavel Shilovsky | f152fd5 | 2012-11-22 17:10:57 +0400 | [diff] [blame] | 560 | /* we are going to update can_cache_brlcks here - need a write access */ |
| 561 | down_write(&cinode->lock_sem); |
| 562 | if (cinode->can_cache_brlcks) { |
| 563 | /* can cache locks - no need to push them */ |
| 564 | up_write(&cinode->lock_sem); |
| 565 | return rc; |
| 566 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Pavel Shilovsky | f152fd5 | 2012-11-22 17:10:57 +0400 | [diff] [blame] | 568 | if (cap_unix(tcon->ses) && |
| 569 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && |
| 570 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) |
| 571 | rc = cifs_push_posix_locks(cfile); |
| 572 | else |
| 573 | rc = tcon->ses->server->ops->push_mand_locks(cfile); |
| 574 | |
| 575 | up_write(&cinode->lock_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | return rc; |
| 577 | } |
| 578 | |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 579 | static int |
| 580 | cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
| 582 | int rc = -EACCES; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 583 | unsigned int xid; |
Jeff Layton | 590a3fe | 2009-09-12 11:54:28 -0400 | [diff] [blame] | 584 | __u32 oplock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | struct cifs_sb_info *cifs_sb; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 586 | struct cifs_tcon *tcon; |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 587 | struct TCP_Server_Info *server; |
| 588 | struct cifsInodeInfo *cinode; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 589 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | char *full_path = NULL; |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 591 | int desired_access; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | int disposition = FILE_OPEN; |
Shirish Pargaonkar | 3d3ea8e | 2011-09-26 09:56:44 -0500 | [diff] [blame] | 593 | int create_options = CREATE_NOT_DIR; |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 594 | struct cifs_fid fid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 596 | xid = get_xid(); |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 597 | mutex_lock(&cfile->fh_mutex); |
| 598 | if (!cfile->invalidHandle) { |
| 599 | mutex_unlock(&cfile->fh_mutex); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 600 | rc = 0; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 601 | free_xid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 602 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 605 | inode = cfile->dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | cifs_sb = CIFS_SB(inode->i_sb); |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 607 | tcon = tlink_tcon(cfile->tlink); |
| 608 | server = tcon->ses->server; |
Steve French | 3a9f462 | 2007-04-04 17:10:24 +0000 | [diff] [blame] | 609 | |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 610 | /* |
| 611 | * Can not grab rename sem here because various ops, including those |
| 612 | * that already have the rename sem can end up causing writepage to get |
| 613 | * called and if the server was down that means we end up here, and we |
| 614 | * can never tell if the caller already has the rename_sem. |
| 615 | */ |
| 616 | full_path = build_path_from_dentry(cfile->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | if (full_path == NULL) { |
Steve French | 3a9f462 | 2007-04-04 17:10:24 +0000 | [diff] [blame] | 618 | rc = -ENOMEM; |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 619 | mutex_unlock(&cfile->fh_mutex); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 620 | free_xid(xid); |
Steve French | 3a9f462 | 2007-04-04 17:10:24 +0000 | [diff] [blame] | 621 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 624 | cFYI(1, "inode = 0x%p file flags 0x%x for %s", inode, cfile->f_flags, |
| 625 | full_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
Pavel Shilovsky | 10b9b98 | 2012-03-20 12:55:09 +0300 | [diff] [blame] | 627 | if (tcon->ses->server->oplocks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | oplock = REQ_OPLOCK; |
| 629 | else |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 630 | oplock = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 632 | if (tcon->unix_ext && cap_unix(tcon->ses) && |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 633 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 634 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 635 | /* |
| 636 | * O_CREAT, O_EXCL and O_TRUNC already had their effect on the |
| 637 | * original open. Must mask them off for a reopen. |
| 638 | */ |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 639 | unsigned int oflags = cfile->f_flags & |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 640 | ~(O_CREAT | O_EXCL | O_TRUNC); |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 641 | |
Jeff Layton | 2422f67 | 2010-06-16 13:40:16 -0400 | [diff] [blame] | 642 | rc = cifs_posix_open(full_path, NULL, inode->i_sb, |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 643 | cifs_sb->mnt_file_mode /* ignored */, |
| 644 | oflags, &oplock, &fid.netfid, xid); |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 645 | if (rc == 0) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 646 | cFYI(1, "posix reopen succeeded"); |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 647 | goto reopen_success; |
| 648 | } |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 649 | /* |
| 650 | * fallthrough to retry open the old way on errors, especially |
| 651 | * in the reconnect path it is important to retry hard |
| 652 | */ |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 655 | desired_access = cifs_convert_flags(cfile->f_flags); |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 656 | |
Shirish Pargaonkar | 3d3ea8e | 2011-09-26 09:56:44 -0500 | [diff] [blame] | 657 | if (backup_cred(cifs_sb)) |
| 658 | create_options |= CREATE_OPEN_BACKUP_INTENT; |
| 659 | |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 660 | if (server->ops->get_lease_key) |
| 661 | server->ops->get_lease_key(inode, &fid); |
| 662 | |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 663 | /* |
| 664 | * Can not refresh inode by passing in file_info buf to be returned by |
| 665 | * CIFSSMBOpen and then calling get_inode_info with returned buf since |
| 666 | * file might have write behind data that needs to be flushed and server |
| 667 | * version of file size can be stale. If we knew for sure that inode was |
| 668 | * not dirty locally we could do this. |
| 669 | */ |
| 670 | rc = server->ops->open(xid, tcon, full_path, disposition, |
| 671 | desired_access, create_options, &fid, &oplock, |
| 672 | NULL, cifs_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | if (rc) { |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 674 | mutex_unlock(&cfile->fh_mutex); |
| 675 | cFYI(1, "cifs_reopen returned 0x%x", rc); |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 676 | cFYI(1, "oplock: %d", oplock); |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 677 | goto reopen_error_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | } |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 679 | |
| 680 | reopen_success: |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 681 | cfile->invalidHandle = false; |
| 682 | mutex_unlock(&cfile->fh_mutex); |
| 683 | cinode = CIFS_I(inode); |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 684 | |
| 685 | if (can_flush) { |
| 686 | rc = filemap_write_and_wait(inode->i_mapping); |
Jeff Layton | eb4b756 | 2010-10-22 14:52:29 -0400 | [diff] [blame] | 687 | mapping_set_error(inode->i_mapping, rc); |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 688 | |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 689 | if (tcon->unix_ext) |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 690 | rc = cifs_get_inode_info_unix(&inode, full_path, |
| 691 | inode->i_sb, xid); |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 692 | else |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 693 | rc = cifs_get_inode_info(&inode, full_path, NULL, |
| 694 | inode->i_sb, xid, NULL); |
| 695 | } |
| 696 | /* |
| 697 | * Else we are writing out data to server already and could deadlock if |
| 698 | * we tried to flush data, and since we do not know if we have data that |
| 699 | * would invalidate the current end of file on the server we can not go |
| 700 | * to the server to get the new inode info. |
| 701 | */ |
Pavel Shilovsky | e66673e | 2010-11-02 12:00:42 +0300 | [diff] [blame] | 702 | |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 703 | server->ops->set_fid(cfile, &fid, oplock); |
| 704 | cifs_relock_file(cfile); |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 705 | |
| 706 | reopen_error_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | kfree(full_path); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 708 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | return rc; |
| 710 | } |
| 711 | |
| 712 | int cifs_close(struct inode *inode, struct file *file) |
| 713 | { |
Jeff Layton | 7797069 | 2011-04-05 16:23:47 -0700 | [diff] [blame] | 714 | if (file->private_data != NULL) { |
| 715 | cifsFileInfo_put(file->private_data); |
| 716 | file->private_data = NULL; |
| 717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 719 | /* return code from the ->release op is always ignored */ |
| 720 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | int cifs_closedir(struct inode *inode, struct file *file) |
| 724 | { |
| 725 | int rc = 0; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 726 | unsigned int xid; |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 727 | struct cifsFileInfo *cfile = file->private_data; |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 728 | struct cifs_tcon *tcon; |
| 729 | struct TCP_Server_Info *server; |
| 730 | char *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 732 | cFYI(1, "Closedir inode = 0x%p", inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 734 | if (cfile == NULL) |
| 735 | return rc; |
| 736 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 737 | xid = get_xid(); |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 738 | tcon = tlink_tcon(cfile->tlink); |
| 739 | server = tcon->ses->server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 741 | cFYI(1, "Freeing private data in close dir"); |
| 742 | spin_lock(&cifs_file_list_lock); |
| 743 | if (!cfile->srch_inf.endOfSearch && !cfile->invalidHandle) { |
| 744 | cfile->invalidHandle = true; |
| 745 | spin_unlock(&cifs_file_list_lock); |
| 746 | if (server->ops->close_dir) |
| 747 | rc = server->ops->close_dir(xid, tcon, &cfile->fid); |
| 748 | else |
| 749 | rc = -ENOSYS; |
| 750 | cFYI(1, "Closing uncompleted readdir with rc %d", rc); |
| 751 | /* not much we can do if it fails anyway, ignore rc */ |
| 752 | rc = 0; |
| 753 | } else |
| 754 | spin_unlock(&cifs_file_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 756 | buf = cfile->srch_inf.ntwrk_buf_start; |
| 757 | if (buf) { |
| 758 | cFYI(1, "closedir free smb buf in srch struct"); |
| 759 | cfile->srch_inf.ntwrk_buf_start = NULL; |
| 760 | if (cfile->srch_inf.smallBuf) |
| 761 | cifs_small_buf_release(buf); |
| 762 | else |
| 763 | cifs_buf_release(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 765 | |
| 766 | cifs_put_tlink(cfile->tlink); |
| 767 | kfree(file->private_data); |
| 768 | file->private_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | /* BB can we lock the filestruct while this is going on? */ |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 770 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | return rc; |
| 772 | } |
| 773 | |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 774 | static struct cifsLockInfo * |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 775 | cifs_lock_init(__u64 offset, __u64 length, __u8 type) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 776 | { |
Pavel Shilovsky | a88b470 | 2011-10-29 17:17:59 +0400 | [diff] [blame] | 777 | struct cifsLockInfo *lock = |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 778 | kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL); |
Pavel Shilovsky | a88b470 | 2011-10-29 17:17:59 +0400 | [diff] [blame] | 779 | if (!lock) |
| 780 | return lock; |
| 781 | lock->offset = offset; |
| 782 | lock->length = length; |
| 783 | lock->type = type; |
Pavel Shilovsky | a88b470 | 2011-10-29 17:17:59 +0400 | [diff] [blame] | 784 | lock->pid = current->tgid; |
| 785 | INIT_LIST_HEAD(&lock->blist); |
| 786 | init_waitqueue_head(&lock->block_q); |
| 787 | return lock; |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 788 | } |
| 789 | |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 790 | void |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 791 | cifs_del_lock_waiters(struct cifsLockInfo *lock) |
| 792 | { |
| 793 | struct cifsLockInfo *li, *tmp; |
| 794 | list_for_each_entry_safe(li, tmp, &lock->blist, blist) { |
| 795 | list_del_init(&li->blist); |
| 796 | wake_up(&li->block_q); |
| 797 | } |
| 798 | } |
| 799 | |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 800 | #define CIFS_LOCK_OP 0 |
| 801 | #define CIFS_READ_OP 1 |
| 802 | #define CIFS_WRITE_OP 2 |
| 803 | |
| 804 | /* @rw_check : 0 - no op, 1 - read, 2 - write */ |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 805 | static bool |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 806 | cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset, |
| 807 | __u64 length, __u8 type, struct cifsFileInfo *cfile, |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 808 | struct cifsLockInfo **conf_lock, int rw_check) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 809 | { |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 810 | struct cifsLockInfo *li; |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 811 | struct cifsFileInfo *cur_cfile = fdlocks->cfile; |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 812 | struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 813 | |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 814 | list_for_each_entry(li, &fdlocks->locks, llist) { |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 815 | if (offset + length <= li->offset || |
| 816 | offset >= li->offset + li->length) |
| 817 | continue; |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 818 | if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid && |
| 819 | server->ops->compare_fids(cfile, cur_cfile)) { |
| 820 | /* shared lock prevents write op through the same fid */ |
| 821 | if (!(li->type & server->vals->shared_lock_type) || |
| 822 | rw_check != CIFS_WRITE_OP) |
| 823 | continue; |
| 824 | } |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 825 | if ((type & server->vals->shared_lock_type) && |
| 826 | ((server->ops->compare_fids(cfile, cur_cfile) && |
| 827 | current->tgid == li->pid) || type == li->type)) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 828 | continue; |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 829 | if (conf_lock) |
| 830 | *conf_lock = li; |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 831 | return true; |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 832 | } |
| 833 | return false; |
| 834 | } |
| 835 | |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 836 | bool |
Pavel Shilovsky | 55157df | 2012-02-28 14:04:17 +0300 | [diff] [blame] | 837 | cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length, |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 838 | __u8 type, struct cifsLockInfo **conf_lock, |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 839 | int rw_check) |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 840 | { |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 841 | bool rc = false; |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 842 | struct cifs_fid_locks *cur; |
Pavel Shilovsky | 55157df | 2012-02-28 14:04:17 +0300 | [diff] [blame] | 843 | struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode); |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 844 | |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 845 | list_for_each_entry(cur, &cinode->llist, llist) { |
| 846 | rc = cifs_find_fid_lock_conflict(cur, offset, length, type, |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 847 | cfile, conf_lock, rw_check); |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 848 | if (rc) |
| 849 | break; |
| 850 | } |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 851 | |
| 852 | return rc; |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 853 | } |
| 854 | |
Pavel Shilovsky | 9a5101c | 2011-11-07 16:11:24 +0300 | [diff] [blame] | 855 | /* |
| 856 | * Check if there is another lock that prevents us to set the lock (mandatory |
| 857 | * style). If such a lock exists, update the flock structure with its |
| 858 | * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks |
| 859 | * or leave it the same if we can't. Returns 0 if we don't need to request to |
| 860 | * the server or 1 otherwise. |
| 861 | */ |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 862 | static int |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 863 | cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length, |
| 864 | __u8 type, struct file_lock *flock) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 865 | { |
| 866 | int rc = 0; |
| 867 | struct cifsLockInfo *conf_lock; |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 868 | struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 869 | struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 870 | bool exist; |
| 871 | |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 872 | down_read(&cinode->lock_sem); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 873 | |
Pavel Shilovsky | 55157df | 2012-02-28 14:04:17 +0300 | [diff] [blame] | 874 | exist = cifs_find_lock_conflict(cfile, offset, length, type, |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 875 | &conf_lock, CIFS_LOCK_OP); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 876 | if (exist) { |
| 877 | flock->fl_start = conf_lock->offset; |
| 878 | flock->fl_end = conf_lock->offset + conf_lock->length - 1; |
| 879 | flock->fl_pid = conf_lock->pid; |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 880 | if (conf_lock->type & server->vals->shared_lock_type) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 881 | flock->fl_type = F_RDLCK; |
| 882 | else |
| 883 | flock->fl_type = F_WRLCK; |
| 884 | } else if (!cinode->can_cache_brlcks) |
| 885 | rc = 1; |
| 886 | else |
| 887 | flock->fl_type = F_UNLCK; |
| 888 | |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 889 | up_read(&cinode->lock_sem); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 890 | return rc; |
| 891 | } |
| 892 | |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 893 | static void |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 894 | cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 895 | { |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 896 | struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode); |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 897 | down_write(&cinode->lock_sem); |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 898 | list_add_tail(&lock->llist, &cfile->llist->locks); |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 899 | up_write(&cinode->lock_sem); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Pavel Shilovsky | 9a5101c | 2011-11-07 16:11:24 +0300 | [diff] [blame] | 902 | /* |
| 903 | * Set the byte-range lock (mandatory style). Returns: |
| 904 | * 1) 0, if we set the lock and don't need to request to the server; |
| 905 | * 2) 1, if no locks prevent us but we need to request to the server; |
| 906 | * 3) -EACCESS, if there is a lock that prevents us and wait is false. |
| 907 | */ |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 908 | static int |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 909 | cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock, |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 910 | bool wait) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 911 | { |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 912 | struct cifsLockInfo *conf_lock; |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 913 | struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 914 | bool exist; |
| 915 | int rc = 0; |
| 916 | |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 917 | try_again: |
| 918 | exist = false; |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 919 | down_write(&cinode->lock_sem); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 920 | |
Pavel Shilovsky | 55157df | 2012-02-28 14:04:17 +0300 | [diff] [blame] | 921 | exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length, |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 922 | lock->type, &conf_lock, CIFS_LOCK_OP); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 923 | if (!exist && cinode->can_cache_brlcks) { |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 924 | list_add_tail(&lock->llist, &cfile->llist->locks); |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 925 | up_write(&cinode->lock_sem); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 926 | return rc; |
| 927 | } |
| 928 | |
| 929 | if (!exist) |
| 930 | rc = 1; |
| 931 | else if (!wait) |
| 932 | rc = -EACCES; |
| 933 | else { |
| 934 | list_add_tail(&lock->blist, &conf_lock->blist); |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 935 | up_write(&cinode->lock_sem); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 936 | rc = wait_event_interruptible(lock->block_q, |
| 937 | (lock->blist.prev == &lock->blist) && |
| 938 | (lock->blist.next == &lock->blist)); |
| 939 | if (!rc) |
| 940 | goto try_again; |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 941 | down_write(&cinode->lock_sem); |
Pavel Shilovsky | a88b470 | 2011-10-29 17:17:59 +0400 | [diff] [blame] | 942 | list_del_init(&lock->blist); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 943 | } |
| 944 | |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 945 | up_write(&cinode->lock_sem); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 946 | return rc; |
| 947 | } |
| 948 | |
Pavel Shilovsky | 9a5101c | 2011-11-07 16:11:24 +0300 | [diff] [blame] | 949 | /* |
| 950 | * Check if there is another lock that prevents us to set the lock (posix |
| 951 | * style). If such a lock exists, update the flock structure with its |
| 952 | * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks |
| 953 | * or leave it the same if we can't. Returns 0 if we don't need to request to |
| 954 | * the server or 1 otherwise. |
| 955 | */ |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 956 | static int |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 957 | cifs_posix_lock_test(struct file *file, struct file_lock *flock) |
| 958 | { |
| 959 | int rc = 0; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 960 | struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 961 | unsigned char saved_type = flock->fl_type; |
| 962 | |
Pavel Shilovsky | 5079276 | 2011-10-29 17:17:57 +0400 | [diff] [blame] | 963 | if ((flock->fl_flags & FL_POSIX) == 0) |
| 964 | return 1; |
| 965 | |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 966 | down_read(&cinode->lock_sem); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 967 | posix_test_lock(file, flock); |
| 968 | |
| 969 | if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) { |
| 970 | flock->fl_type = saved_type; |
| 971 | rc = 1; |
| 972 | } |
| 973 | |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 974 | up_read(&cinode->lock_sem); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 975 | return rc; |
| 976 | } |
| 977 | |
Pavel Shilovsky | 9a5101c | 2011-11-07 16:11:24 +0300 | [diff] [blame] | 978 | /* |
| 979 | * Set the byte-range lock (posix style). Returns: |
| 980 | * 1) 0, if we set the lock and don't need to request to the server; |
| 981 | * 2) 1, if we need to request to the server; |
| 982 | * 3) <0, if the error occurs while setting the lock. |
| 983 | */ |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 984 | static int |
| 985 | cifs_posix_lock_set(struct file *file, struct file_lock *flock) |
| 986 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 987 | struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); |
Pavel Shilovsky | 5079276 | 2011-10-29 17:17:57 +0400 | [diff] [blame] | 988 | int rc = 1; |
| 989 | |
| 990 | if ((flock->fl_flags & FL_POSIX) == 0) |
| 991 | return rc; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 992 | |
Pavel Shilovsky | 66189be | 2012-03-28 21:56:19 +0400 | [diff] [blame] | 993 | try_again: |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 994 | down_write(&cinode->lock_sem); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 995 | if (!cinode->can_cache_brlcks) { |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 996 | up_write(&cinode->lock_sem); |
Pavel Shilovsky | 5079276 | 2011-10-29 17:17:57 +0400 | [diff] [blame] | 997 | return rc; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 998 | } |
Pavel Shilovsky | 66189be | 2012-03-28 21:56:19 +0400 | [diff] [blame] | 999 | |
| 1000 | rc = posix_lock_file(file, flock, NULL); |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1001 | up_write(&cinode->lock_sem); |
Pavel Shilovsky | 66189be | 2012-03-28 21:56:19 +0400 | [diff] [blame] | 1002 | if (rc == FILE_LOCK_DEFERRED) { |
| 1003 | rc = wait_event_interruptible(flock->fl_wait, !flock->fl_next); |
| 1004 | if (!rc) |
| 1005 | goto try_again; |
| 1006 | locks_delete_block(flock); |
| 1007 | } |
Steve French | 9ebb389 | 2012-04-01 13:52:54 -0500 | [diff] [blame] | 1008 | return rc; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1009 | } |
| 1010 | |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1011 | int |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1012 | cifs_push_mandatory_locks(struct cifsFileInfo *cfile) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1013 | { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1014 | unsigned int xid; |
| 1015 | int rc = 0, stored_rc; |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1016 | struct cifsLockInfo *li, *tmp; |
| 1017 | struct cifs_tcon *tcon; |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1018 | unsigned int num, max_num, max_buf; |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1019 | LOCKING_ANDX_RANGE *buf, *cur; |
| 1020 | int types[] = {LOCKING_ANDX_LARGE_FILES, |
| 1021 | LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES}; |
| 1022 | int i; |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1023 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1024 | xid = get_xid(); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1025 | tcon = tlink_tcon(cfile->tlink); |
| 1026 | |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1027 | /* |
| 1028 | * Accessing maxBuf is racy with cifs_reconnect - need to store value |
| 1029 | * and check it for zero before using. |
| 1030 | */ |
| 1031 | max_buf = tcon->ses->server->maxBuf; |
| 1032 | if (!max_buf) { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1033 | free_xid(xid); |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1034 | return -EINVAL; |
| 1035 | } |
| 1036 | |
| 1037 | max_num = (max_buf - sizeof(struct smb_hdr)) / |
| 1038 | sizeof(LOCKING_ANDX_RANGE); |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1039 | buf = kzalloc(max_num * sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL); |
| 1040 | if (!buf) { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1041 | free_xid(xid); |
Pavel Shilovsky | e2f2886 | 2012-08-29 21:13:38 +0400 | [diff] [blame] | 1042 | return -ENOMEM; |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | for (i = 0; i < 2; i++) { |
| 1046 | cur = buf; |
| 1047 | num = 0; |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1048 | list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1049 | if (li->type != types[i]) |
| 1050 | continue; |
| 1051 | cur->Pid = cpu_to_le16(li->pid); |
| 1052 | cur->LengthLow = cpu_to_le32((u32)li->length); |
| 1053 | cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); |
| 1054 | cur->OffsetLow = cpu_to_le32((u32)li->offset); |
| 1055 | cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); |
| 1056 | if (++num == max_num) { |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1057 | stored_rc = cifs_lockv(xid, tcon, |
| 1058 | cfile->fid.netfid, |
Pavel Shilovsky | 04a6aa8 | 2012-02-28 14:16:55 +0300 | [diff] [blame] | 1059 | (__u8)li->type, 0, num, |
| 1060 | buf); |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1061 | if (stored_rc) |
| 1062 | rc = stored_rc; |
| 1063 | cur = buf; |
| 1064 | num = 0; |
| 1065 | } else |
| 1066 | cur++; |
| 1067 | } |
| 1068 | |
| 1069 | if (num) { |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1070 | stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, |
Pavel Shilovsky | 04a6aa8 | 2012-02-28 14:16:55 +0300 | [diff] [blame] | 1071 | (__u8)types[i], 0, num, buf); |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1072 | if (stored_rc) |
| 1073 | rc = stored_rc; |
| 1074 | } |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1075 | } |
| 1076 | |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1077 | kfree(buf); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1078 | free_xid(xid); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1079 | return rc; |
| 1080 | } |
| 1081 | |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1082 | /* copied from fs/locks.c with a name change */ |
| 1083 | #define cifs_for_each_lock(inode, lockp) \ |
| 1084 | for (lockp = &inode->i_flock; *lockp != NULL; \ |
| 1085 | lockp = &(*lockp)->fl_next) |
| 1086 | |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1087 | struct lock_to_push { |
| 1088 | struct list_head llist; |
| 1089 | __u64 offset; |
| 1090 | __u64 length; |
| 1091 | __u32 pid; |
| 1092 | __u16 netfid; |
| 1093 | __u8 type; |
| 1094 | }; |
| 1095 | |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1096 | static int |
Pavel Shilovsky | b8db928 | 2012-11-22 17:07:16 +0400 | [diff] [blame] | 1097 | cifs_push_posix_locks(struct cifsFileInfo *cfile) |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1098 | { |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1099 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
| 1100 | struct file_lock *flock, **before; |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1101 | unsigned int count = 0, i = 0; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1102 | int rc = 0, xid, type; |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1103 | struct list_head locks_to_send, *el; |
| 1104 | struct lock_to_push *lck, *tmp; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1105 | __u64 length; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1106 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1107 | xid = get_xid(); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1108 | |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1109 | lock_flocks(); |
| 1110 | cifs_for_each_lock(cfile->dentry->d_inode, before) { |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1111 | if ((*before)->fl_flags & FL_POSIX) |
| 1112 | count++; |
| 1113 | } |
| 1114 | unlock_flocks(); |
| 1115 | |
| 1116 | INIT_LIST_HEAD(&locks_to_send); |
| 1117 | |
| 1118 | /* |
Pavel Shilovsky | ce85852 | 2012-03-17 09:46:55 +0300 | [diff] [blame] | 1119 | * Allocating count locks is enough because no FL_POSIX locks can be |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1120 | * added to the list while we are holding cinode->lock_sem that |
Pavel Shilovsky | ce85852 | 2012-03-17 09:46:55 +0300 | [diff] [blame] | 1121 | * protects locking operations of this inode. |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1122 | */ |
| 1123 | for (; i < count; i++) { |
| 1124 | lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL); |
| 1125 | if (!lck) { |
| 1126 | rc = -ENOMEM; |
| 1127 | goto err_out; |
| 1128 | } |
| 1129 | list_add_tail(&lck->llist, &locks_to_send); |
| 1130 | } |
| 1131 | |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1132 | el = locks_to_send.next; |
| 1133 | lock_flocks(); |
| 1134 | cifs_for_each_lock(cfile->dentry->d_inode, before) { |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1135 | flock = *before; |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1136 | if ((flock->fl_flags & FL_POSIX) == 0) |
| 1137 | continue; |
Pavel Shilovsky | ce85852 | 2012-03-17 09:46:55 +0300 | [diff] [blame] | 1138 | if (el == &locks_to_send) { |
| 1139 | /* |
| 1140 | * The list ended. We don't have enough allocated |
| 1141 | * structures - something is really wrong. |
| 1142 | */ |
| 1143 | cERROR(1, "Can't push all brlocks!"); |
| 1144 | break; |
| 1145 | } |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1146 | length = 1 + flock->fl_end - flock->fl_start; |
| 1147 | if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK) |
| 1148 | type = CIFS_RDLCK; |
| 1149 | else |
| 1150 | type = CIFS_WRLCK; |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1151 | lck = list_entry(el, struct lock_to_push, llist); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1152 | lck->pid = flock->fl_pid; |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1153 | lck->netfid = cfile->fid.netfid; |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1154 | lck->length = length; |
| 1155 | lck->type = type; |
| 1156 | lck->offset = flock->fl_start; |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1157 | el = el->next; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1158 | } |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1159 | unlock_flocks(); |
| 1160 | |
| 1161 | list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1162 | int stored_rc; |
| 1163 | |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1164 | stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid, |
Jeff Layton | c5fd363 | 2012-07-23 13:28:37 -0400 | [diff] [blame] | 1165 | lck->offset, lck->length, NULL, |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1166 | lck->type, 0); |
| 1167 | if (stored_rc) |
| 1168 | rc = stored_rc; |
| 1169 | list_del(&lck->llist); |
| 1170 | kfree(lck); |
| 1171 | } |
| 1172 | |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1173 | out: |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1174 | free_xid(xid); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1175 | return rc; |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1176 | err_out: |
| 1177 | list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { |
| 1178 | list_del(&lck->llist); |
| 1179 | kfree(lck); |
| 1180 | } |
| 1181 | goto out; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | static int |
Pavel Shilovsky | b8db928 | 2012-11-22 17:07:16 +0400 | [diff] [blame] | 1185 | cifs_push_locks(struct cifsFileInfo *cfile) |
Pavel Shilovsky | 9ec3c88 | 2012-11-22 17:00:10 +0400 | [diff] [blame] | 1186 | { |
Pavel Shilovsky | b8db928 | 2012-11-22 17:07:16 +0400 | [diff] [blame] | 1187 | struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); |
Pavel Shilovsky | 9ec3c88 | 2012-11-22 17:00:10 +0400 | [diff] [blame] | 1188 | struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode); |
Pavel Shilovsky | b8db928 | 2012-11-22 17:07:16 +0400 | [diff] [blame] | 1189 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Pavel Shilovsky | 9ec3c88 | 2012-11-22 17:00:10 +0400 | [diff] [blame] | 1190 | int rc = 0; |
| 1191 | |
| 1192 | /* we are going to update can_cache_brlcks here - need a write access */ |
| 1193 | down_write(&cinode->lock_sem); |
| 1194 | if (!cinode->can_cache_brlcks) { |
| 1195 | up_write(&cinode->lock_sem); |
| 1196 | return rc; |
| 1197 | } |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1198 | |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 1199 | if (cap_unix(tcon->ses) && |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1200 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && |
| 1201 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) |
Pavel Shilovsky | b8db928 | 2012-11-22 17:07:16 +0400 | [diff] [blame] | 1202 | rc = cifs_push_posix_locks(cfile); |
| 1203 | else |
| 1204 | rc = tcon->ses->server->ops->push_mand_locks(cfile); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1205 | |
Pavel Shilovsky | b8db928 | 2012-11-22 17:07:16 +0400 | [diff] [blame] | 1206 | cinode->can_cache_brlcks = false; |
| 1207 | up_write(&cinode->lock_sem); |
| 1208 | return rc; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1209 | } |
| 1210 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1211 | static void |
Pavel Shilovsky | 04a6aa8 | 2012-02-28 14:16:55 +0300 | [diff] [blame] | 1212 | cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock, |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1213 | bool *wait_flag, struct TCP_Server_Info *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | { |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1215 | if (flock->fl_flags & FL_POSIX) |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1216 | cFYI(1, "Posix"); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1217 | if (flock->fl_flags & FL_FLOCK) |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1218 | cFYI(1, "Flock"); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1219 | if (flock->fl_flags & FL_SLEEP) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1220 | cFYI(1, "Blocking lock"); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1221 | *wait_flag = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | } |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1223 | if (flock->fl_flags & FL_ACCESS) |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1224 | cFYI(1, "Process suspended by mandatory locking - " |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1225 | "not implemented yet"); |
| 1226 | if (flock->fl_flags & FL_LEASE) |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1227 | cFYI(1, "Lease on file - not implemented yet"); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1228 | if (flock->fl_flags & |
Jeff Layton | 3d6d854 | 2012-09-19 06:22:46 -0700 | [diff] [blame] | 1229 | (~(FL_POSIX | FL_FLOCK | FL_SLEEP | |
| 1230 | FL_ACCESS | FL_LEASE | FL_CLOSE))) |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1231 | cFYI(1, "Unknown lock flags 0x%x", flock->fl_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1233 | *type = server->vals->large_lock_type; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1234 | if (flock->fl_type == F_WRLCK) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1235 | cFYI(1, "F_WRLCK "); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1236 | *type |= server->vals->exclusive_lock_type; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1237 | *lock = 1; |
| 1238 | } else if (flock->fl_type == F_UNLCK) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1239 | cFYI(1, "F_UNLCK"); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1240 | *type |= server->vals->unlock_lock_type; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1241 | *unlock = 1; |
| 1242 | /* Check if unlock includes more than one lock range */ |
| 1243 | } else if (flock->fl_type == F_RDLCK) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1244 | cFYI(1, "F_RDLCK"); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1245 | *type |= server->vals->shared_lock_type; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1246 | *lock = 1; |
| 1247 | } else if (flock->fl_type == F_EXLCK) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1248 | cFYI(1, "F_EXLCK"); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1249 | *type |= server->vals->exclusive_lock_type; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1250 | *lock = 1; |
| 1251 | } else if (flock->fl_type == F_SHLCK) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1252 | cFYI(1, "F_SHLCK"); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1253 | *type |= server->vals->shared_lock_type; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1254 | *lock = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | } else |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1256 | cFYI(1, "Unknown type of lock"); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1259 | static int |
Pavel Shilovsky | 04a6aa8 | 2012-02-28 14:16:55 +0300 | [diff] [blame] | 1260 | cifs_getlk(struct file *file, struct file_lock *flock, __u32 type, |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1261 | bool wait_flag, bool posix_lck, unsigned int xid) |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1262 | { |
| 1263 | int rc = 0; |
| 1264 | __u64 length = 1 + flock->fl_end - flock->fl_start; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1265 | struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; |
| 1266 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1267 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1268 | __u16 netfid = cfile->fid.netfid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1270 | if (posix_lck) { |
| 1271 | int posix_lock_type; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1272 | |
| 1273 | rc = cifs_posix_lock_test(file, flock); |
| 1274 | if (!rc) |
| 1275 | return rc; |
| 1276 | |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1277 | if (type & server->vals->shared_lock_type) |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1278 | posix_lock_type = CIFS_RDLCK; |
| 1279 | else |
| 1280 | posix_lock_type = CIFS_WRLCK; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1281 | rc = CIFSSMBPosixLock(xid, tcon, netfid, current->tgid, |
Jeff Layton | c5fd363 | 2012-07-23 13:28:37 -0400 | [diff] [blame] | 1282 | flock->fl_start, length, flock, |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1283 | posix_lock_type, wait_flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | return rc; |
| 1285 | } |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1286 | |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 1287 | rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1288 | if (!rc) |
| 1289 | return rc; |
| 1290 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1291 | /* BB we could chain these into one lock request BB */ |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1292 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type, |
| 1293 | 1, 0, false); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1294 | if (rc == 0) { |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1295 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, |
| 1296 | type, 0, 1, false); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1297 | flock->fl_type = F_UNLCK; |
| 1298 | if (rc != 0) |
| 1299 | cERROR(1, "Error unlocking previously locked " |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1300 | "range %d during test of lock", rc); |
Pavel Shilovsky | a88b470 | 2011-10-29 17:17:59 +0400 | [diff] [blame] | 1301 | return 0; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1304 | if (type & server->vals->shared_lock_type) { |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1305 | flock->fl_type = F_WRLCK; |
Pavel Shilovsky | a88b470 | 2011-10-29 17:17:59 +0400 | [diff] [blame] | 1306 | return 0; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1307 | } |
| 1308 | |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1309 | type &= ~server->vals->exclusive_lock_type; |
| 1310 | |
| 1311 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, |
| 1312 | type | server->vals->shared_lock_type, |
| 1313 | 1, 0, false); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1314 | if (rc == 0) { |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1315 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, |
| 1316 | type | server->vals->shared_lock_type, 0, 1, false); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1317 | flock->fl_type = F_RDLCK; |
| 1318 | if (rc != 0) |
| 1319 | cERROR(1, "Error unlocking previously locked " |
| 1320 | "range %d during test of lock", rc); |
| 1321 | } else |
| 1322 | flock->fl_type = F_WRLCK; |
| 1323 | |
Pavel Shilovsky | a88b470 | 2011-10-29 17:17:59 +0400 | [diff] [blame] | 1324 | return 0; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1325 | } |
| 1326 | |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1327 | void |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1328 | cifs_move_llist(struct list_head *source, struct list_head *dest) |
| 1329 | { |
| 1330 | struct list_head *li, *tmp; |
| 1331 | list_for_each_safe(li, tmp, source) |
| 1332 | list_move(li, dest); |
| 1333 | } |
| 1334 | |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1335 | void |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1336 | cifs_free_llist(struct list_head *llist) |
| 1337 | { |
| 1338 | struct cifsLockInfo *li, *tmp; |
| 1339 | list_for_each_entry_safe(li, tmp, llist, llist) { |
| 1340 | cifs_del_lock_waiters(li); |
| 1341 | list_del(&li->llist); |
| 1342 | kfree(li); |
| 1343 | } |
| 1344 | } |
| 1345 | |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1346 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1347 | cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock, |
| 1348 | unsigned int xid) |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1349 | { |
| 1350 | int rc = 0, stored_rc; |
| 1351 | int types[] = {LOCKING_ANDX_LARGE_FILES, |
| 1352 | LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES}; |
| 1353 | unsigned int i; |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1354 | unsigned int max_num, num, max_buf; |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1355 | LOCKING_ANDX_RANGE *buf, *cur; |
| 1356 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
| 1357 | struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode); |
| 1358 | struct cifsLockInfo *li, *tmp; |
| 1359 | __u64 length = 1 + flock->fl_end - flock->fl_start; |
| 1360 | struct list_head tmp_llist; |
| 1361 | |
| 1362 | INIT_LIST_HEAD(&tmp_llist); |
| 1363 | |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1364 | /* |
| 1365 | * Accessing maxBuf is racy with cifs_reconnect - need to store value |
| 1366 | * and check it for zero before using. |
| 1367 | */ |
| 1368 | max_buf = tcon->ses->server->maxBuf; |
| 1369 | if (!max_buf) |
| 1370 | return -EINVAL; |
| 1371 | |
| 1372 | max_num = (max_buf - sizeof(struct smb_hdr)) / |
| 1373 | sizeof(LOCKING_ANDX_RANGE); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1374 | buf = kzalloc(max_num * sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL); |
| 1375 | if (!buf) |
| 1376 | return -ENOMEM; |
| 1377 | |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1378 | down_write(&cinode->lock_sem); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1379 | for (i = 0; i < 2; i++) { |
| 1380 | cur = buf; |
| 1381 | num = 0; |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1382 | list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1383 | if (flock->fl_start > li->offset || |
| 1384 | (flock->fl_start + length) < |
| 1385 | (li->offset + li->length)) |
| 1386 | continue; |
| 1387 | if (current->tgid != li->pid) |
| 1388 | continue; |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1389 | if (types[i] != li->type) |
| 1390 | continue; |
Pavel Shilovsky | ea319d5 | 2012-05-31 13:59:36 +0400 | [diff] [blame] | 1391 | if (cinode->can_cache_brlcks) { |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1392 | /* |
| 1393 | * We can cache brlock requests - simply remove |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 1394 | * a lock from the file's list. |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1395 | */ |
| 1396 | list_del(&li->llist); |
| 1397 | cifs_del_lock_waiters(li); |
| 1398 | kfree(li); |
Pavel Shilovsky | ea319d5 | 2012-05-31 13:59:36 +0400 | [diff] [blame] | 1399 | continue; |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1400 | } |
Pavel Shilovsky | ea319d5 | 2012-05-31 13:59:36 +0400 | [diff] [blame] | 1401 | cur->Pid = cpu_to_le16(li->pid); |
| 1402 | cur->LengthLow = cpu_to_le32((u32)li->length); |
| 1403 | cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); |
| 1404 | cur->OffsetLow = cpu_to_le32((u32)li->offset); |
| 1405 | cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); |
| 1406 | /* |
| 1407 | * We need to save a lock here to let us add it again to |
| 1408 | * the file's list if the unlock range request fails on |
| 1409 | * the server. |
| 1410 | */ |
| 1411 | list_move(&li->llist, &tmp_llist); |
| 1412 | if (++num == max_num) { |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1413 | stored_rc = cifs_lockv(xid, tcon, |
| 1414 | cfile->fid.netfid, |
Pavel Shilovsky | ea319d5 | 2012-05-31 13:59:36 +0400 | [diff] [blame] | 1415 | li->type, num, 0, buf); |
| 1416 | if (stored_rc) { |
| 1417 | /* |
| 1418 | * We failed on the unlock range |
| 1419 | * request - add all locks from the tmp |
| 1420 | * list to the head of the file's list. |
| 1421 | */ |
| 1422 | cifs_move_llist(&tmp_llist, |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1423 | &cfile->llist->locks); |
Pavel Shilovsky | ea319d5 | 2012-05-31 13:59:36 +0400 | [diff] [blame] | 1424 | rc = stored_rc; |
| 1425 | } else |
| 1426 | /* |
| 1427 | * The unlock range request succeed - |
| 1428 | * free the tmp list. |
| 1429 | */ |
| 1430 | cifs_free_llist(&tmp_llist); |
| 1431 | cur = buf; |
| 1432 | num = 0; |
| 1433 | } else |
| 1434 | cur++; |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1435 | } |
| 1436 | if (num) { |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1437 | stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1438 | types[i], num, 0, buf); |
| 1439 | if (stored_rc) { |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1440 | cifs_move_llist(&tmp_llist, |
| 1441 | &cfile->llist->locks); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1442 | rc = stored_rc; |
| 1443 | } else |
| 1444 | cifs_free_llist(&tmp_llist); |
| 1445 | } |
| 1446 | } |
| 1447 | |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1448 | up_write(&cinode->lock_sem); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1449 | kfree(buf); |
| 1450 | return rc; |
| 1451 | } |
| 1452 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1453 | static int |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1454 | cifs_setlk(struct file *file, struct file_lock *flock, __u32 type, |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1455 | bool wait_flag, bool posix_lck, int lock, int unlock, |
| 1456 | unsigned int xid) |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1457 | { |
| 1458 | int rc = 0; |
| 1459 | __u64 length = 1 + flock->fl_end - flock->fl_start; |
| 1460 | struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; |
| 1461 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1462 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 1463 | struct inode *inode = cfile->dentry->d_inode; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1464 | |
| 1465 | if (posix_lck) { |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 1466 | int posix_lock_type; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1467 | |
| 1468 | rc = cifs_posix_lock_set(file, flock); |
| 1469 | if (!rc || rc < 0) |
| 1470 | return rc; |
| 1471 | |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1472 | if (type & server->vals->shared_lock_type) |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 1473 | posix_lock_type = CIFS_RDLCK; |
| 1474 | else |
| 1475 | posix_lock_type = CIFS_WRLCK; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1476 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1477 | if (unlock == 1) |
Steve French | beb84dc | 2006-03-03 23:36:34 +0000 | [diff] [blame] | 1478 | posix_lock_type = CIFS_UNLCK; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1479 | |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1480 | rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid, |
| 1481 | current->tgid, flock->fl_start, length, |
| 1482 | NULL, posix_lock_type, wait_flag); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1483 | goto out; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1486 | if (lock) { |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 1487 | struct cifsLockInfo *lock; |
| 1488 | |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 1489 | lock = cifs_lock_init(flock->fl_start, length, type); |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 1490 | if (!lock) |
| 1491 | return -ENOMEM; |
| 1492 | |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 1493 | rc = cifs_lock_add_if(cfile, lock, wait_flag); |
Pavel Shilovsky | 21cb2d9 | 2012-11-22 18:56:39 +0400 | [diff] [blame] | 1494 | if (rc < 0) { |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 1495 | kfree(lock); |
Pavel Shilovsky | 21cb2d9 | 2012-11-22 18:56:39 +0400 | [diff] [blame] | 1496 | return rc; |
| 1497 | } |
| 1498 | if (!rc) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1499 | goto out; |
| 1500 | |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 1501 | /* |
| 1502 | * Windows 7 server can delay breaking lease from read to None |
| 1503 | * if we set a byte-range lock on a file - break it explicitly |
| 1504 | * before sending the lock to the server to be sure the next |
| 1505 | * read won't conflict with non-overlapted locks due to |
| 1506 | * pagereading. |
| 1507 | */ |
| 1508 | if (!CIFS_I(inode)->clientCanCacheAll && |
| 1509 | CIFS_I(inode)->clientCanCacheRead) { |
| 1510 | cifs_invalidate_mapping(inode); |
| 1511 | cFYI(1, "Set no oplock for inode=%p due to mand locks", |
| 1512 | inode); |
| 1513 | CIFS_I(inode)->clientCanCacheRead = false; |
| 1514 | } |
| 1515 | |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1516 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, |
| 1517 | type, 1, 0, wait_flag); |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 1518 | if (rc) { |
| 1519 | kfree(lock); |
Pavel Shilovsky | 21cb2d9 | 2012-11-22 18:56:39 +0400 | [diff] [blame] | 1520 | return rc; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1521 | } |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 1522 | |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 1523 | cifs_lock_add(cfile, lock); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1524 | } else if (unlock) |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1525 | rc = server->ops->mand_unlock_range(cfile, flock, xid); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1526 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1527 | out: |
| 1528 | if (flock->fl_flags & FL_POSIX) |
Steve French | 9ebb389 | 2012-04-01 13:52:54 -0500 | [diff] [blame] | 1529 | posix_lock_file_wait(file, flock); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1530 | return rc; |
| 1531 | } |
| 1532 | |
| 1533 | int cifs_lock(struct file *file, int cmd, struct file_lock *flock) |
| 1534 | { |
| 1535 | int rc, xid; |
| 1536 | int lock = 0, unlock = 0; |
| 1537 | bool wait_flag = false; |
| 1538 | bool posix_lck = false; |
| 1539 | struct cifs_sb_info *cifs_sb; |
| 1540 | struct cifs_tcon *tcon; |
| 1541 | struct cifsInodeInfo *cinode; |
| 1542 | struct cifsFileInfo *cfile; |
| 1543 | __u16 netfid; |
Pavel Shilovsky | 04a6aa8 | 2012-02-28 14:16:55 +0300 | [diff] [blame] | 1544 | __u32 type; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1545 | |
| 1546 | rc = -EACCES; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1547 | xid = get_xid(); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1548 | |
| 1549 | cFYI(1, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld " |
| 1550 | "end: %lld", cmd, flock->fl_flags, flock->fl_type, |
| 1551 | flock->fl_start, flock->fl_end); |
| 1552 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1553 | cfile = (struct cifsFileInfo *)file->private_data; |
| 1554 | tcon = tlink_tcon(cfile->tlink); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1555 | |
| 1556 | cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag, |
| 1557 | tcon->ses->server); |
| 1558 | |
| 1559 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1560 | netfid = cfile->fid.netfid; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1561 | cinode = CIFS_I(file_inode(file)); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1562 | |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 1563 | if (cap_unix(tcon->ses) && |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1564 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && |
| 1565 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) |
| 1566 | posix_lck = true; |
| 1567 | /* |
| 1568 | * BB add code here to normalize offset and length to account for |
| 1569 | * negative length which we can not accept over the wire. |
| 1570 | */ |
| 1571 | if (IS_GETLK(cmd)) { |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1572 | rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1573 | free_xid(xid); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1574 | return rc; |
| 1575 | } |
| 1576 | |
| 1577 | if (!lock && !unlock) { |
| 1578 | /* |
| 1579 | * if no lock or unlock then nothing to do since we do not |
| 1580 | * know what it is |
| 1581 | */ |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1582 | free_xid(xid); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1583 | return -EOPNOTSUPP; |
| 1584 | } |
| 1585 | |
| 1586 | rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock, |
| 1587 | xid); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1588 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | return rc; |
| 1590 | } |
| 1591 | |
Jeff Layton | 597b027 | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 1592 | /* |
| 1593 | * update the file size (if needed) after a write. Should be called with |
| 1594 | * the inode->i_lock held |
| 1595 | */ |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 1596 | void |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 1597 | cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset, |
| 1598 | unsigned int bytes_written) |
| 1599 | { |
| 1600 | loff_t end_of_write = offset + bytes_written; |
| 1601 | |
| 1602 | if (end_of_write > cifsi->server_eof) |
| 1603 | cifsi->server_eof = end_of_write; |
| 1604 | } |
| 1605 | |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1606 | static ssize_t |
| 1607 | cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data, |
| 1608 | size_t write_size, loff_t *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | { |
| 1610 | int rc = 0; |
| 1611 | unsigned int bytes_written = 0; |
| 1612 | unsigned int total_written; |
| 1613 | struct cifs_sb_info *cifs_sb; |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1614 | struct cifs_tcon *tcon; |
| 1615 | struct TCP_Server_Info *server; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1616 | unsigned int xid; |
Jeff Layton | 7da4b49 | 2010-10-15 15:34:00 -0400 | [diff] [blame] | 1617 | struct dentry *dentry = open_file->dentry; |
| 1618 | struct cifsInodeInfo *cifsi = CIFS_I(dentry->d_inode); |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 1619 | struct cifs_io_parms io_parms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | |
Jeff Layton | 7da4b49 | 2010-10-15 15:34:00 -0400 | [diff] [blame] | 1621 | cifs_sb = CIFS_SB(dentry->d_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1623 | cFYI(1, "write %zd bytes to offset %lld of %s", write_size, |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1624 | *offset, dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1626 | tcon = tlink_tcon(open_file->tlink); |
| 1627 | server = tcon->ses->server; |
| 1628 | |
| 1629 | if (!server->ops->sync_write) |
| 1630 | return -ENOSYS; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1631 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1632 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | for (total_written = 0; write_size > total_written; |
| 1635 | total_written += bytes_written) { |
| 1636 | rc = -EAGAIN; |
| 1637 | while (rc == -EAGAIN) { |
Jeff Layton | ca83ce3 | 2011-04-12 09:13:44 -0400 | [diff] [blame] | 1638 | struct kvec iov[2]; |
| 1639 | unsigned int len; |
| 1640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | if (open_file->invalidHandle) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | /* we could deadlock if we called |
| 1643 | filemap_fdatawait from here so tell |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1644 | reopen_file not to flush data to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | server now */ |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 1646 | rc = cifs_reopen_file(open_file, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | if (rc != 0) |
| 1648 | break; |
| 1649 | } |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 1650 | |
Jeff Layton | ca83ce3 | 2011-04-12 09:13:44 -0400 | [diff] [blame] | 1651 | len = min((size_t)cifs_sb->wsize, |
| 1652 | write_size - total_written); |
| 1653 | /* iov[0] is reserved for smb header */ |
| 1654 | iov[1].iov_base = (char *)write_data + total_written; |
| 1655 | iov[1].iov_len = len; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 1656 | io_parms.pid = pid; |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1657 | io_parms.tcon = tcon; |
| 1658 | io_parms.offset = *offset; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 1659 | io_parms.length = len; |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1660 | rc = server->ops->sync_write(xid, open_file, &io_parms, |
| 1661 | &bytes_written, iov, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | } |
| 1663 | if (rc || (bytes_written == 0)) { |
| 1664 | if (total_written) |
| 1665 | break; |
| 1666 | else { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1667 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | return rc; |
| 1669 | } |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 1670 | } else { |
Jeff Layton | 597b027 | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 1671 | spin_lock(&dentry->d_inode->i_lock); |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1672 | cifs_update_eof(cifsi, *offset, bytes_written); |
Jeff Layton | 597b027 | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 1673 | spin_unlock(&dentry->d_inode->i_lock); |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1674 | *offset += bytes_written; |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 1675 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | } |
| 1677 | |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1678 | cifs_stats_bytes_written(tcon, total_written); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
Jeff Layton | 7da4b49 | 2010-10-15 15:34:00 -0400 | [diff] [blame] | 1680 | if (total_written > 0) { |
| 1681 | spin_lock(&dentry->d_inode->i_lock); |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1682 | if (*offset > dentry->d_inode->i_size) |
| 1683 | i_size_write(dentry->d_inode, *offset); |
Jeff Layton | 7da4b49 | 2010-10-15 15:34:00 -0400 | [diff] [blame] | 1684 | spin_unlock(&dentry->d_inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | } |
Jeff Layton | 7da4b49 | 2010-10-15 15:34:00 -0400 | [diff] [blame] | 1686 | mark_inode_dirty_sync(dentry->d_inode); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1687 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | return total_written; |
| 1689 | } |
| 1690 | |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1691 | struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode, |
| 1692 | bool fsuid_only) |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 1693 | { |
| 1694 | struct cifsFileInfo *open_file = NULL; |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1695 | struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb); |
| 1696 | |
| 1697 | /* only filter by fsuid on multiuser mounts */ |
| 1698 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) |
| 1699 | fsuid_only = false; |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 1700 | |
Jeff Layton | 4477288 | 2010-10-15 15:34:03 -0400 | [diff] [blame] | 1701 | spin_lock(&cifs_file_list_lock); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 1702 | /* we could simply get the first_list_entry since write-only entries |
| 1703 | are always at the end of the list but since the first entry might |
| 1704 | have a close pending, we go through the whole list */ |
| 1705 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { |
Eric W. Biederman | fef59fd | 2013-02-06 02:23:02 -0800 | [diff] [blame] | 1706 | if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1707 | continue; |
Jeff Layton | 2e396b8 | 2010-10-15 15:34:01 -0400 | [diff] [blame] | 1708 | if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) { |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 1709 | if (!open_file->invalidHandle) { |
| 1710 | /* found a good file */ |
| 1711 | /* lock it so it will not be closed on us */ |
Jeff Layton | 764a1b1 | 2012-07-25 14:59:54 -0400 | [diff] [blame] | 1712 | cifsFileInfo_get_locked(open_file); |
Jeff Layton | 4477288 | 2010-10-15 15:34:03 -0400 | [diff] [blame] | 1713 | spin_unlock(&cifs_file_list_lock); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 1714 | return open_file; |
| 1715 | } /* else might as well continue, and look for |
| 1716 | another, or simply have the caller reopen it |
| 1717 | again rather than trying to fix this handle */ |
| 1718 | } else /* write only file */ |
| 1719 | break; /* write only files are last so must be done */ |
| 1720 | } |
Jeff Layton | 4477288 | 2010-10-15 15:34:03 -0400 | [diff] [blame] | 1721 | spin_unlock(&cifs_file_list_lock); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 1722 | return NULL; |
| 1723 | } |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 1724 | |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1725 | struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode, |
| 1726 | bool fsuid_only) |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1727 | { |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1728 | struct cifsFileInfo *open_file, *inv_file = NULL; |
Jeff Layton | d389229 | 2010-11-02 16:22:50 -0400 | [diff] [blame] | 1729 | struct cifs_sb_info *cifs_sb; |
Jeff Layton | 2846d38 | 2008-09-22 21:33:33 -0400 | [diff] [blame] | 1730 | bool any_available = false; |
Steve French | dd99cd8 | 2005-10-05 19:32:49 -0700 | [diff] [blame] | 1731 | int rc; |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1732 | unsigned int refind = 0; |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1733 | |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 1734 | /* Having a null inode here (because mapping->host was set to zero by |
| 1735 | the VFS or MM) should not happen but we had reports of on oops (due to |
| 1736 | it being zero) during stress testcases so we need to check for it */ |
| 1737 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1738 | if (cifs_inode == NULL) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1739 | cERROR(1, "Null inode passed to cifs_writeable_file"); |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 1740 | dump_stack(); |
| 1741 | return NULL; |
| 1742 | } |
| 1743 | |
Jeff Layton | d389229 | 2010-11-02 16:22:50 -0400 | [diff] [blame] | 1744 | cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb); |
| 1745 | |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1746 | /* only filter by fsuid on multiuser mounts */ |
| 1747 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) |
| 1748 | fsuid_only = false; |
| 1749 | |
Jeff Layton | 4477288 | 2010-10-15 15:34:03 -0400 | [diff] [blame] | 1750 | spin_lock(&cifs_file_list_lock); |
Steve French | 9b22b0b | 2007-10-02 01:11:08 +0000 | [diff] [blame] | 1751 | refind_writable: |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1752 | if (refind > MAX_REOPEN_ATT) { |
| 1753 | spin_unlock(&cifs_file_list_lock); |
| 1754 | return NULL; |
| 1755 | } |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1756 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1757 | if (!any_available && open_file->pid != current->tgid) |
| 1758 | continue; |
Eric W. Biederman | fef59fd | 2013-02-06 02:23:02 -0800 | [diff] [blame] | 1759 | if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1760 | continue; |
Jeff Layton | 2e396b8 | 2010-10-15 15:34:01 -0400 | [diff] [blame] | 1761 | if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { |
Steve French | 9b22b0b | 2007-10-02 01:11:08 +0000 | [diff] [blame] | 1762 | if (!open_file->invalidHandle) { |
| 1763 | /* found a good writable file */ |
Jeff Layton | 764a1b1 | 2012-07-25 14:59:54 -0400 | [diff] [blame] | 1764 | cifsFileInfo_get_locked(open_file); |
Jeff Layton | 4477288 | 2010-10-15 15:34:03 -0400 | [diff] [blame] | 1765 | spin_unlock(&cifs_file_list_lock); |
Steve French | 9b22b0b | 2007-10-02 01:11:08 +0000 | [diff] [blame] | 1766 | return open_file; |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1767 | } else { |
| 1768 | if (!inv_file) |
| 1769 | inv_file = open_file; |
Steve French | 9b22b0b | 2007-10-02 01:11:08 +0000 | [diff] [blame] | 1770 | } |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1771 | } |
| 1772 | } |
Jeff Layton | 2846d38 | 2008-09-22 21:33:33 -0400 | [diff] [blame] | 1773 | /* couldn't find useable FH with same pid, try any available */ |
| 1774 | if (!any_available) { |
| 1775 | any_available = true; |
| 1776 | goto refind_writable; |
| 1777 | } |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1778 | |
| 1779 | if (inv_file) { |
| 1780 | any_available = false; |
Jeff Layton | 764a1b1 | 2012-07-25 14:59:54 -0400 | [diff] [blame] | 1781 | cifsFileInfo_get_locked(inv_file); |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1782 | } |
| 1783 | |
Jeff Layton | 4477288 | 2010-10-15 15:34:03 -0400 | [diff] [blame] | 1784 | spin_unlock(&cifs_file_list_lock); |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1785 | |
| 1786 | if (inv_file) { |
| 1787 | rc = cifs_reopen_file(inv_file, false); |
| 1788 | if (!rc) |
| 1789 | return inv_file; |
| 1790 | else { |
| 1791 | spin_lock(&cifs_file_list_lock); |
| 1792 | list_move_tail(&inv_file->flist, |
| 1793 | &cifs_inode->openFileList); |
| 1794 | spin_unlock(&cifs_file_list_lock); |
| 1795 | cifsFileInfo_put(inv_file); |
| 1796 | spin_lock(&cifs_file_list_lock); |
| 1797 | ++refind; |
| 1798 | goto refind_writable; |
| 1799 | } |
| 1800 | } |
| 1801 | |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1802 | return NULL; |
| 1803 | } |
| 1804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to) |
| 1806 | { |
| 1807 | struct address_space *mapping = page->mapping; |
| 1808 | loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT; |
| 1809 | char *write_data; |
| 1810 | int rc = -EFAULT; |
| 1811 | int bytes_written = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | struct inode *inode; |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1813 | struct cifsFileInfo *open_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | |
| 1815 | if (!mapping || !mapping->host) |
| 1816 | return -EFAULT; |
| 1817 | |
| 1818 | inode = page->mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | |
| 1820 | offset += (loff_t)from; |
| 1821 | write_data = kmap(page); |
| 1822 | write_data += from; |
| 1823 | |
| 1824 | if ((to > PAGE_CACHE_SIZE) || (from > to)) { |
| 1825 | kunmap(page); |
| 1826 | return -EIO; |
| 1827 | } |
| 1828 | |
| 1829 | /* racing with truncate? */ |
| 1830 | if (offset > mapping->host->i_size) { |
| 1831 | kunmap(page); |
| 1832 | return 0; /* don't care */ |
| 1833 | } |
| 1834 | |
| 1835 | /* check to make sure that we are not extending the file */ |
| 1836 | if (mapping->host->i_size - offset < (loff_t)to) |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1837 | to = (unsigned)(mapping->host->i_size - offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1839 | open_file = find_writable_file(CIFS_I(mapping->host), false); |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1840 | if (open_file) { |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 1841 | bytes_written = cifs_write(open_file, open_file->pid, |
| 1842 | write_data, to - from, &offset); |
Dave Kleikamp | 6ab409b | 2009-08-31 11:07:12 -0400 | [diff] [blame] | 1843 | cifsFileInfo_put(open_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | /* Does mm or vfs already set times? */ |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1845 | inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb); |
Steve French | bb5a9a0 | 2007-12-31 04:21:29 +0000 | [diff] [blame] | 1846 | if ((bytes_written > 0) && (offset)) |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1847 | rc = 0; |
Steve French | bb5a9a0 | 2007-12-31 04:21:29 +0000 | [diff] [blame] | 1848 | else if (bytes_written < 0) |
| 1849 | rc = bytes_written; |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1850 | } else { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1851 | cFYI(1, "No writeable filehandles for inode"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | rc = -EIO; |
| 1853 | } |
| 1854 | |
| 1855 | kunmap(page); |
| 1856 | return rc; |
| 1857 | } |
| 1858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | static int cifs_writepages(struct address_space *mapping, |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1860 | struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | { |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 1862 | struct cifs_sb_info *cifs_sb = CIFS_SB(mapping->host->i_sb); |
| 1863 | bool done = false, scanned = false, range_whole = false; |
| 1864 | pgoff_t end, index; |
| 1865 | struct cifs_writedata *wdata; |
Pavel Shilovsky | c9de5c8 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 1866 | struct TCP_Server_Info *server; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1867 | struct page *page; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1868 | int rc = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1869 | |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1870 | /* |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 1871 | * If wsize is smaller than the page cache size, default to writing |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1872 | * one page at a time via cifs_writepage |
| 1873 | */ |
| 1874 | if (cifs_sb->wsize < PAGE_CACHE_SIZE) |
| 1875 | return generic_writepages(mapping, wbc); |
| 1876 | |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 1877 | if (wbc->range_cyclic) { |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1878 | index = mapping->writeback_index; /* Start from prev offset */ |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 1879 | end = -1; |
| 1880 | } else { |
| 1881 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 1882 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 1883 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 1884 | range_whole = true; |
| 1885 | scanned = true; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1886 | } |
| 1887 | retry: |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 1888 | while (!done && index <= end) { |
| 1889 | unsigned int i, nr_pages, found_pages; |
| 1890 | pgoff_t next = 0, tofind; |
| 1891 | struct page **pages; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1892 | |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 1893 | tofind = min((cifs_sb->wsize / PAGE_CACHE_SIZE) - 1, |
| 1894 | end - index) + 1; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1895 | |
Jeff Layton | c2e8764 | 2012-03-23 14:40:55 -0400 | [diff] [blame] | 1896 | wdata = cifs_writedata_alloc((unsigned int)tofind, |
| 1897 | cifs_writev_complete); |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 1898 | if (!wdata) { |
| 1899 | rc = -ENOMEM; |
| 1900 | break; |
| 1901 | } |
| 1902 | |
| 1903 | /* |
| 1904 | * find_get_pages_tag seems to return a max of 256 on each |
| 1905 | * iteration, so we must call it several times in order to |
| 1906 | * fill the array or the wsize is effectively limited to |
| 1907 | * 256 * PAGE_CACHE_SIZE. |
| 1908 | */ |
| 1909 | found_pages = 0; |
| 1910 | pages = wdata->pages; |
| 1911 | do { |
| 1912 | nr_pages = find_get_pages_tag(mapping, &index, |
| 1913 | PAGECACHE_TAG_DIRTY, |
| 1914 | tofind, pages); |
| 1915 | found_pages += nr_pages; |
| 1916 | tofind -= nr_pages; |
| 1917 | pages += nr_pages; |
| 1918 | } while (nr_pages && tofind && index <= end); |
| 1919 | |
| 1920 | if (found_pages == 0) { |
| 1921 | kref_put(&wdata->refcount, cifs_writedata_release); |
| 1922 | break; |
| 1923 | } |
| 1924 | |
| 1925 | nr_pages = 0; |
| 1926 | for (i = 0; i < found_pages; i++) { |
| 1927 | page = wdata->pages[i]; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1928 | /* |
| 1929 | * At this point we hold neither mapping->tree_lock nor |
| 1930 | * lock on the page itself: the page may be truncated or |
| 1931 | * invalidated (changing page->mapping to NULL), or even |
| 1932 | * swizzled back from swapper_space to tmpfs file |
| 1933 | * mapping |
| 1934 | */ |
| 1935 | |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 1936 | if (nr_pages == 0) |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1937 | lock_page(page); |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 1938 | else if (!trylock_page(page)) |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1939 | break; |
| 1940 | |
| 1941 | if (unlikely(page->mapping != mapping)) { |
| 1942 | unlock_page(page); |
| 1943 | break; |
| 1944 | } |
| 1945 | |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 1946 | if (!wbc->range_cyclic && page->index > end) { |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 1947 | done = true; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1948 | unlock_page(page); |
| 1949 | break; |
| 1950 | } |
| 1951 | |
| 1952 | if (next && (page->index != next)) { |
| 1953 | /* Not next consecutive page */ |
| 1954 | unlock_page(page); |
| 1955 | break; |
| 1956 | } |
| 1957 | |
| 1958 | if (wbc->sync_mode != WB_SYNC_NONE) |
| 1959 | wait_on_page_writeback(page); |
| 1960 | |
| 1961 | if (PageWriteback(page) || |
Linus Torvalds | cb876f4 | 2006-12-23 16:19:07 -0800 | [diff] [blame] | 1962 | !clear_page_dirty_for_io(page)) { |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1963 | unlock_page(page); |
| 1964 | break; |
| 1965 | } |
Steve French | 84d2f07 | 2005-10-12 15:32:05 -0700 | [diff] [blame] | 1966 | |
Linus Torvalds | cb876f4 | 2006-12-23 16:19:07 -0800 | [diff] [blame] | 1967 | /* |
| 1968 | * This actually clears the dirty bit in the radix tree. |
| 1969 | * See cifs_writepage() for more commentary. |
| 1970 | */ |
| 1971 | set_page_writeback(page); |
| 1972 | |
Jeff Layton | 3a98b86 | 2012-11-26 09:48:41 -0500 | [diff] [blame] | 1973 | if (page_offset(page) >= i_size_read(mapping->host)) { |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 1974 | done = true; |
Steve French | 84d2f07 | 2005-10-12 15:32:05 -0700 | [diff] [blame] | 1975 | unlock_page(page); |
Linus Torvalds | cb876f4 | 2006-12-23 16:19:07 -0800 | [diff] [blame] | 1976 | end_page_writeback(page); |
Steve French | 84d2f07 | 2005-10-12 15:32:05 -0700 | [diff] [blame] | 1977 | break; |
| 1978 | } |
| 1979 | |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 1980 | wdata->pages[i] = page; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1981 | next = page->index + 1; |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 1982 | ++nr_pages; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1983 | } |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 1984 | |
| 1985 | /* reset index to refind any pages skipped */ |
| 1986 | if (nr_pages == 0) |
| 1987 | index = wdata->pages[0]->index + 1; |
| 1988 | |
| 1989 | /* put any pages we aren't going to use */ |
| 1990 | for (i = nr_pages; i < found_pages; i++) { |
| 1991 | page_cache_release(wdata->pages[i]); |
| 1992 | wdata->pages[i] = NULL; |
| 1993 | } |
| 1994 | |
| 1995 | /* nothing to write? */ |
| 1996 | if (nr_pages == 0) { |
| 1997 | kref_put(&wdata->refcount, cifs_writedata_release); |
| 1998 | continue; |
| 1999 | } |
| 2000 | |
| 2001 | wdata->sync_mode = wbc->sync_mode; |
| 2002 | wdata->nr_pages = nr_pages; |
| 2003 | wdata->offset = page_offset(wdata->pages[0]); |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2004 | wdata->pagesz = PAGE_CACHE_SIZE; |
| 2005 | wdata->tailsz = |
Jeff Layton | 3a98b86 | 2012-11-26 09:48:41 -0500 | [diff] [blame] | 2006 | min(i_size_read(mapping->host) - |
| 2007 | page_offset(wdata->pages[nr_pages - 1]), |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2008 | (loff_t)PAGE_CACHE_SIZE); |
| 2009 | wdata->bytes = ((nr_pages - 1) * PAGE_CACHE_SIZE) + |
| 2010 | wdata->tailsz; |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2011 | |
| 2012 | do { |
| 2013 | if (wdata->cfile != NULL) |
| 2014 | cifsFileInfo_put(wdata->cfile); |
| 2015 | wdata->cfile = find_writable_file(CIFS_I(mapping->host), |
| 2016 | false); |
| 2017 | if (!wdata->cfile) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 2018 | cERROR(1, "No writable handles for inode"); |
Steve French | 23e7dd7 | 2005-10-20 13:44:56 -0700 | [diff] [blame] | 2019 | rc = -EBADF; |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2020 | break; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2021 | } |
Jeff Layton | fe5f5d2 | 2012-03-23 14:40:55 -0400 | [diff] [blame] | 2022 | wdata->pid = wdata->cfile->pid; |
Pavel Shilovsky | c9de5c8 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 2023 | server = tlink_tcon(wdata->cfile->tlink)->ses->server; |
| 2024 | rc = server->ops->async_writev(wdata); |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2025 | } while (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN); |
Jeff Layton | f3983c2 | 2010-09-22 16:17:40 -0700 | [diff] [blame] | 2026 | |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2027 | for (i = 0; i < nr_pages; ++i) |
| 2028 | unlock_page(wdata->pages[i]); |
Jeff Layton | 941b853 | 2011-01-11 07:24:01 -0500 | [diff] [blame] | 2029 | |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2030 | /* send failure -- clean up the mess */ |
| 2031 | if (rc != 0) { |
| 2032 | for (i = 0; i < nr_pages; ++i) { |
Jeff Layton | 941b853 | 2011-01-11 07:24:01 -0500 | [diff] [blame] | 2033 | if (rc == -EAGAIN) |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2034 | redirty_page_for_writepage(wbc, |
| 2035 | wdata->pages[i]); |
| 2036 | else |
| 2037 | SetPageError(wdata->pages[i]); |
| 2038 | end_page_writeback(wdata->pages[i]); |
| 2039 | page_cache_release(wdata->pages[i]); |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2040 | } |
Jeff Layton | 941b853 | 2011-01-11 07:24:01 -0500 | [diff] [blame] | 2041 | if (rc != -EAGAIN) |
| 2042 | mapping_set_error(mapping, rc); |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2043 | } |
| 2044 | kref_put(&wdata->refcount, cifs_writedata_release); |
Jeff Layton | 941b853 | 2011-01-11 07:24:01 -0500 | [diff] [blame] | 2045 | |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2046 | wbc->nr_to_write -= nr_pages; |
| 2047 | if (wbc->nr_to_write <= 0) |
| 2048 | done = true; |
Dave Kleikamp | b066a48 | 2008-11-18 03:49:05 +0000 | [diff] [blame] | 2049 | |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2050 | index = next; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2051 | } |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2052 | |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2053 | if (!scanned && !done) { |
| 2054 | /* |
| 2055 | * We hit the last page and there is more work to be done: wrap |
| 2056 | * back to the start of the file |
| 2057 | */ |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2058 | scanned = true; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2059 | index = 0; |
| 2060 | goto retry; |
| 2061 | } |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2062 | |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 2063 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2064 | mapping->writeback_index = index; |
| 2065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | return rc; |
| 2067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 2069 | static int |
| 2070 | cifs_writepage_locked(struct page *page, struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | { |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 2072 | int rc; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2073 | unsigned int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2075 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | /* BB add check for wbc flags */ |
| 2077 | page_cache_get(page); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2078 | if (!PageUptodate(page)) |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 2079 | cFYI(1, "ppw - page not up to date"); |
Linus Torvalds | cb876f4 | 2006-12-23 16:19:07 -0800 | [diff] [blame] | 2080 | |
| 2081 | /* |
| 2082 | * Set the "writeback" flag, and clear "dirty" in the radix tree. |
| 2083 | * |
| 2084 | * A writepage() implementation always needs to do either this, |
| 2085 | * or re-dirty the page with "redirty_page_for_writepage()" in |
| 2086 | * the case of a failure. |
| 2087 | * |
| 2088 | * Just unlocking the page will cause the radix tree tag-bits |
| 2089 | * to fail to update with the state of the page correctly. |
| 2090 | */ |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 2091 | set_page_writeback(page); |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 2092 | retry_write: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE); |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 2094 | if (rc == -EAGAIN && wbc->sync_mode == WB_SYNC_ALL) |
| 2095 | goto retry_write; |
| 2096 | else if (rc == -EAGAIN) |
| 2097 | redirty_page_for_writepage(wbc, page); |
| 2098 | else if (rc != 0) |
| 2099 | SetPageError(page); |
| 2100 | else |
| 2101 | SetPageUptodate(page); |
Linus Torvalds | cb876f4 | 2006-12-23 16:19:07 -0800 | [diff] [blame] | 2102 | end_page_writeback(page); |
| 2103 | page_cache_release(page); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2104 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | return rc; |
| 2106 | } |
| 2107 | |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 2108 | static int cifs_writepage(struct page *page, struct writeback_control *wbc) |
| 2109 | { |
| 2110 | int rc = cifs_writepage_locked(page, wbc); |
| 2111 | unlock_page(page); |
| 2112 | return rc; |
| 2113 | } |
| 2114 | |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2115 | static int cifs_write_end(struct file *file, struct address_space *mapping, |
| 2116 | loff_t pos, unsigned len, unsigned copied, |
| 2117 | struct page *page, void *fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | { |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2119 | int rc; |
| 2120 | struct inode *inode = mapping->host; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 2121 | struct cifsFileInfo *cfile = file->private_data; |
| 2122 | struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); |
| 2123 | __u32 pid; |
| 2124 | |
| 2125 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
| 2126 | pid = cfile->pid; |
| 2127 | else |
| 2128 | pid = current->tgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 2130 | cFYI(1, "write_end for page %p from pos %lld with %d bytes", |
| 2131 | page, pos, copied); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2132 | |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 2133 | if (PageChecked(page)) { |
| 2134 | if (copied == len) |
| 2135 | SetPageUptodate(page); |
| 2136 | ClearPageChecked(page); |
| 2137 | } else if (!PageUptodate(page) && copied == PAGE_CACHE_SIZE) |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2138 | SetPageUptodate(page); |
| 2139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | if (!PageUptodate(page)) { |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2141 | char *page_data; |
| 2142 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2143 | unsigned int xid; |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2144 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2145 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | /* this is probably better than directly calling |
| 2147 | partialpage_write since in this function the file handle is |
| 2148 | known which we might as well leverage */ |
| 2149 | /* BB check if anything else missing out of ppw |
| 2150 | such as updating last write time */ |
| 2151 | page_data = kmap(page); |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 2152 | rc = cifs_write(cfile, pid, page_data + offset, copied, &pos); |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2153 | /* if (rc < 0) should we set writebehind rc? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | kunmap(page); |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2155 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2156 | free_xid(xid); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 2157 | } else { |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2158 | rc = copied; |
| 2159 | pos += copied; |
Pavel Shilovsky | ca8aa29 | 2012-12-21 15:05:47 +0400 | [diff] [blame] | 2160 | set_page_dirty(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | } |
| 2162 | |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2163 | if (rc > 0) { |
| 2164 | spin_lock(&inode->i_lock); |
| 2165 | if (pos > inode->i_size) |
| 2166 | i_size_write(inode, pos); |
| 2167 | spin_unlock(&inode->i_lock); |
| 2168 | } |
| 2169 | |
| 2170 | unlock_page(page); |
| 2171 | page_cache_release(page); |
| 2172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | return rc; |
| 2174 | } |
| 2175 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2176 | int cifs_strict_fsync(struct file *file, loff_t start, loff_t end, |
| 2177 | int datasync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2179 | unsigned int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | int rc = 0; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2181 | struct cifs_tcon *tcon; |
Pavel Shilovsky | 1d8c4c0 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 2182 | struct TCP_Server_Info *server; |
Joe Perches | c21dfb6 | 2010-07-12 13:50:14 -0700 | [diff] [blame] | 2183 | struct cifsFileInfo *smbfile = file->private_data; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2184 | struct inode *inode = file_inode(file); |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2185 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2187 | rc = filemap_write_and_wait_range(inode->i_mapping, start, end); |
| 2188 | if (rc) |
| 2189 | return rc; |
| 2190 | mutex_lock(&inode->i_mutex); |
| 2191 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2192 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 2194 | cFYI(1, "Sync file - name: %s datasync: 0x%x", |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 2195 | file->f_path.dentry->d_name.name, datasync); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2196 | |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2197 | if (!CIFS_I(inode)->clientCanCacheRead) { |
| 2198 | rc = cifs_invalidate_mapping(inode); |
| 2199 | if (rc) { |
| 2200 | cFYI(1, "rc: %d during invalidate phase", rc); |
| 2201 | rc = 0; /* don't care about it in fsync */ |
| 2202 | } |
| 2203 | } |
Jeff Layton | eb4b756 | 2010-10-22 14:52:29 -0400 | [diff] [blame] | 2204 | |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2205 | tcon = tlink_tcon(smbfile->tlink); |
Pavel Shilovsky | 1d8c4c0 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 2206 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) { |
| 2207 | server = tcon->ses->server; |
| 2208 | if (server->ops->flush) |
| 2209 | rc = server->ops->flush(xid, tcon, &smbfile->fid); |
| 2210 | else |
| 2211 | rc = -ENOSYS; |
| 2212 | } |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2213 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2214 | free_xid(xid); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2215 | mutex_unlock(&inode->i_mutex); |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2216 | return rc; |
| 2217 | } |
| 2218 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2219 | int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2220 | { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2221 | unsigned int xid; |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2222 | int rc = 0; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2223 | struct cifs_tcon *tcon; |
Pavel Shilovsky | 1d8c4c0 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 2224 | struct TCP_Server_Info *server; |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2225 | struct cifsFileInfo *smbfile = file->private_data; |
| 2226 | struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2227 | struct inode *inode = file->f_mapping->host; |
| 2228 | |
| 2229 | rc = filemap_write_and_wait_range(inode->i_mapping, start, end); |
| 2230 | if (rc) |
| 2231 | return rc; |
| 2232 | mutex_lock(&inode->i_mutex); |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2233 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2234 | xid = get_xid(); |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2235 | |
| 2236 | cFYI(1, "Sync file - name: %s datasync: 0x%x", |
| 2237 | file->f_path.dentry->d_name.name, datasync); |
| 2238 | |
| 2239 | tcon = tlink_tcon(smbfile->tlink); |
Pavel Shilovsky | 1d8c4c0 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 2240 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) { |
| 2241 | server = tcon->ses->server; |
| 2242 | if (server->ops->flush) |
| 2243 | rc = server->ops->flush(xid, tcon, &smbfile->fid); |
| 2244 | else |
| 2245 | rc = -ENOSYS; |
| 2246 | } |
Steve French | b298f22 | 2009-02-21 21:17:43 +0000 | [diff] [blame] | 2247 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2248 | free_xid(xid); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2249 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | return rc; |
| 2251 | } |
| 2252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | /* |
| 2254 | * As file closes, flush all cached write data for this inode checking |
| 2255 | * for write behind errors. |
| 2256 | */ |
Miklos Szeredi | 75e1fcc | 2006-06-23 02:05:12 -0700 | [diff] [blame] | 2257 | int cifs_flush(struct file *file, fl_owner_t id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2259 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | int rc = 0; |
| 2261 | |
Jeff Layton | eb4b756 | 2010-10-22 14:52:29 -0400 | [diff] [blame] | 2262 | if (file->f_mode & FMODE_WRITE) |
Jeff Layton | d3f1322 | 2010-10-15 15:34:07 -0400 | [diff] [blame] | 2263 | rc = filemap_write_and_wait(inode->i_mapping); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2264 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 2265 | cFYI(1, "Flush inode %p file %p rc %d", inode, file, rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | |
| 2267 | return rc; |
| 2268 | } |
| 2269 | |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2270 | static int |
| 2271 | cifs_write_allocate_pages(struct page **pages, unsigned long num_pages) |
| 2272 | { |
| 2273 | int rc = 0; |
| 2274 | unsigned long i; |
| 2275 | |
| 2276 | for (i = 0; i < num_pages; i++) { |
Jeff Layton | e94f7ba | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2277 | pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM); |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2278 | if (!pages[i]) { |
| 2279 | /* |
| 2280 | * save number of pages we have already allocated and |
| 2281 | * return with ENOMEM error |
| 2282 | */ |
| 2283 | num_pages = i; |
| 2284 | rc = -ENOMEM; |
Jeff Layton | e94f7ba | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2285 | break; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2286 | } |
| 2287 | } |
| 2288 | |
Jeff Layton | e94f7ba | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2289 | if (rc) { |
| 2290 | for (i = 0; i < num_pages; i++) |
| 2291 | put_page(pages[i]); |
| 2292 | } |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2293 | return rc; |
| 2294 | } |
| 2295 | |
| 2296 | static inline |
| 2297 | size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len) |
| 2298 | { |
| 2299 | size_t num_pages; |
| 2300 | size_t clen; |
| 2301 | |
| 2302 | clen = min_t(const size_t, len, wsize); |
Jeff Layton | a7103b9 | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2303 | num_pages = DIV_ROUND_UP(clen, PAGE_SIZE); |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2304 | |
| 2305 | if (cur_len) |
| 2306 | *cur_len = clen; |
| 2307 | |
| 2308 | return num_pages; |
| 2309 | } |
| 2310 | |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2311 | static void |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2312 | cifs_uncached_writev_complete(struct work_struct *work) |
| 2313 | { |
| 2314 | int i; |
| 2315 | struct cifs_writedata *wdata = container_of(work, |
| 2316 | struct cifs_writedata, work); |
| 2317 | struct inode *inode = wdata->cfile->dentry->d_inode; |
| 2318 | struct cifsInodeInfo *cifsi = CIFS_I(inode); |
| 2319 | |
| 2320 | spin_lock(&inode->i_lock); |
| 2321 | cifs_update_eof(cifsi, wdata->offset, wdata->bytes); |
| 2322 | if (cifsi->server_eof > inode->i_size) |
| 2323 | i_size_write(inode, cifsi->server_eof); |
| 2324 | spin_unlock(&inode->i_lock); |
| 2325 | |
| 2326 | complete(&wdata->done); |
| 2327 | |
| 2328 | if (wdata->result != -EAGAIN) { |
| 2329 | for (i = 0; i < wdata->nr_pages; i++) |
| 2330 | put_page(wdata->pages[i]); |
| 2331 | } |
| 2332 | |
| 2333 | kref_put(&wdata->refcount, cifs_writedata_release); |
| 2334 | } |
| 2335 | |
| 2336 | /* attempt to send write to server, retry on any -EAGAIN errors */ |
| 2337 | static int |
| 2338 | cifs_uncached_retry_writev(struct cifs_writedata *wdata) |
| 2339 | { |
| 2340 | int rc; |
Pavel Shilovsky | c9de5c8 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 2341 | struct TCP_Server_Info *server; |
| 2342 | |
| 2343 | server = tlink_tcon(wdata->cfile->tlink)->ses->server; |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2344 | |
| 2345 | do { |
| 2346 | if (wdata->cfile->invalidHandle) { |
| 2347 | rc = cifs_reopen_file(wdata->cfile, false); |
| 2348 | if (rc != 0) |
| 2349 | continue; |
| 2350 | } |
Pavel Shilovsky | c9de5c8 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 2351 | rc = server->ops->async_writev(wdata); |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2352 | } while (rc == -EAGAIN); |
| 2353 | |
| 2354 | return rc; |
| 2355 | } |
| 2356 | |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2357 | static ssize_t |
| 2358 | cifs_iovec_write(struct file *file, const struct iovec *iov, |
| 2359 | unsigned long nr_segs, loff_t *poffset) |
| 2360 | { |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2361 | unsigned long nr_pages, i; |
Pavel Shilovsky | 76429c1 | 2011-01-31 16:03:08 +0300 | [diff] [blame] | 2362 | size_t copied, len, cur_len; |
| 2363 | ssize_t total_written = 0; |
Jeff Layton | 3af9d8f | 2012-04-13 17:16:59 -0400 | [diff] [blame] | 2364 | loff_t offset; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2365 | struct iov_iter it; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2366 | struct cifsFileInfo *open_file; |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2367 | struct cifs_tcon *tcon; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2368 | struct cifs_sb_info *cifs_sb; |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2369 | struct cifs_writedata *wdata, *tmp; |
| 2370 | struct list_head wdata_list; |
| 2371 | int rc; |
| 2372 | pid_t pid; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2373 | |
| 2374 | len = iov_length(iov, nr_segs); |
| 2375 | if (!len) |
| 2376 | return 0; |
| 2377 | |
| 2378 | rc = generic_write_checks(file, poffset, &len, 0); |
| 2379 | if (rc) |
| 2380 | return rc; |
| 2381 | |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2382 | INIT_LIST_HEAD(&wdata_list); |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2383 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2384 | open_file = file->private_data; |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2385 | tcon = tlink_tcon(open_file->tlink); |
Pavel Shilovsky | c9de5c8 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 2386 | |
| 2387 | if (!tcon->ses->server->ops->async_writev) |
| 2388 | return -ENOSYS; |
| 2389 | |
Jeff Layton | 3af9d8f | 2012-04-13 17:16:59 -0400 | [diff] [blame] | 2390 | offset = *poffset; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 2391 | |
| 2392 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
| 2393 | pid = open_file->pid; |
| 2394 | else |
| 2395 | pid = current->tgid; |
| 2396 | |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2397 | iov_iter_init(&it, iov, nr_segs, len, 0); |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2398 | do { |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2399 | size_t save_len; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2400 | |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2401 | nr_pages = get_numpages(cifs_sb->wsize, len, &cur_len); |
| 2402 | wdata = cifs_writedata_alloc(nr_pages, |
| 2403 | cifs_uncached_writev_complete); |
| 2404 | if (!wdata) { |
| 2405 | rc = -ENOMEM; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2406 | break; |
| 2407 | } |
| 2408 | |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2409 | rc = cifs_write_allocate_pages(wdata->pages, nr_pages); |
| 2410 | if (rc) { |
| 2411 | kfree(wdata); |
| 2412 | break; |
| 2413 | } |
| 2414 | |
| 2415 | save_len = cur_len; |
| 2416 | for (i = 0; i < nr_pages; i++) { |
| 2417 | copied = min_t(const size_t, cur_len, PAGE_SIZE); |
| 2418 | copied = iov_iter_copy_from_user(wdata->pages[i], &it, |
| 2419 | 0, copied); |
| 2420 | cur_len -= copied; |
| 2421 | iov_iter_advance(&it, copied); |
| 2422 | } |
| 2423 | cur_len = save_len - cur_len; |
| 2424 | |
| 2425 | wdata->sync_mode = WB_SYNC_ALL; |
| 2426 | wdata->nr_pages = nr_pages; |
| 2427 | wdata->offset = (__u64)offset; |
| 2428 | wdata->cfile = cifsFileInfo_get(open_file); |
| 2429 | wdata->pid = pid; |
| 2430 | wdata->bytes = cur_len; |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2431 | wdata->pagesz = PAGE_SIZE; |
| 2432 | wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE); |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2433 | rc = cifs_uncached_retry_writev(wdata); |
| 2434 | if (rc) { |
| 2435 | kref_put(&wdata->refcount, cifs_writedata_release); |
| 2436 | break; |
| 2437 | } |
| 2438 | |
| 2439 | list_add_tail(&wdata->list, &wdata_list); |
| 2440 | offset += cur_len; |
| 2441 | len -= cur_len; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2442 | } while (len > 0); |
| 2443 | |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2444 | /* |
| 2445 | * If at least one write was successfully sent, then discard any rc |
| 2446 | * value from the later writes. If the other write succeeds, then |
| 2447 | * we'll end up returning whatever was written. If it fails, then |
| 2448 | * we'll get a new rc value from that. |
| 2449 | */ |
| 2450 | if (!list_empty(&wdata_list)) |
| 2451 | rc = 0; |
| 2452 | |
| 2453 | /* |
| 2454 | * Wait for and collect replies for any successful sends in order of |
| 2455 | * increasing offset. Once an error is hit or we get a fatal signal |
| 2456 | * while waiting, then return without waiting for any more replies. |
| 2457 | */ |
| 2458 | restart_loop: |
| 2459 | list_for_each_entry_safe(wdata, tmp, &wdata_list, list) { |
| 2460 | if (!rc) { |
| 2461 | /* FIXME: freezable too? */ |
| 2462 | rc = wait_for_completion_killable(&wdata->done); |
| 2463 | if (rc) |
| 2464 | rc = -EINTR; |
| 2465 | else if (wdata->result) |
| 2466 | rc = wdata->result; |
| 2467 | else |
| 2468 | total_written += wdata->bytes; |
| 2469 | |
| 2470 | /* resend call if it's a retryable error */ |
| 2471 | if (rc == -EAGAIN) { |
| 2472 | rc = cifs_uncached_retry_writev(wdata); |
| 2473 | goto restart_loop; |
| 2474 | } |
| 2475 | } |
| 2476 | list_del_init(&wdata->list); |
| 2477 | kref_put(&wdata->refcount, cifs_writedata_release); |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2478 | } |
| 2479 | |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2480 | if (total_written > 0) |
| 2481 | *poffset += total_written; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2482 | |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2483 | cifs_stats_bytes_written(tcon, total_written); |
| 2484 | return total_written ? total_written : (ssize_t)rc; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2485 | } |
| 2486 | |
Pavel Shilovsky | 0b81c1c | 2011-03-10 10:11:05 +0300 | [diff] [blame] | 2487 | ssize_t cifs_user_writev(struct kiocb *iocb, const struct iovec *iov, |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2488 | unsigned long nr_segs, loff_t pos) |
| 2489 | { |
| 2490 | ssize_t written; |
| 2491 | struct inode *inode; |
| 2492 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2493 | inode = file_inode(iocb->ki_filp); |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2494 | |
| 2495 | /* |
| 2496 | * BB - optimize the way when signing is disabled. We can drop this |
| 2497 | * extra memory-to-memory copying and use iovec buffers for constructing |
| 2498 | * write request. |
| 2499 | */ |
| 2500 | |
| 2501 | written = cifs_iovec_write(iocb->ki_filp, iov, nr_segs, &pos); |
| 2502 | if (written > 0) { |
| 2503 | CIFS_I(inode)->invalid_mapping = true; |
| 2504 | iocb->ki_pos = pos; |
| 2505 | } |
| 2506 | |
| 2507 | return written; |
| 2508 | } |
| 2509 | |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2510 | static ssize_t |
Pavel Shilovsky | ca8aa29 | 2012-12-21 15:05:47 +0400 | [diff] [blame] | 2511 | cifs_writev(struct kiocb *iocb, const struct iovec *iov, |
| 2512 | unsigned long nr_segs, loff_t pos) |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2513 | { |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2514 | struct file *file = iocb->ki_filp; |
| 2515 | struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; |
| 2516 | struct inode *inode = file->f_mapping->host; |
| 2517 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
| 2518 | struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; |
| 2519 | ssize_t rc = -EACCES; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2520 | |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2521 | BUG_ON(iocb->ki_pos != pos); |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2522 | |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2523 | /* |
| 2524 | * We need to hold the sem to be sure nobody modifies lock list |
| 2525 | * with a brlock that prevents writing. |
| 2526 | */ |
| 2527 | down_read(&cinode->lock_sem); |
| 2528 | if (!cifs_find_lock_conflict(cfile, pos, iov_length(iov, nr_segs), |
| 2529 | server->vals->exclusive_lock_type, NULL, |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 2530 | CIFS_WRITE_OP)) { |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2531 | mutex_lock(&inode->i_mutex); |
| 2532 | rc = __generic_file_aio_write(iocb, iov, nr_segs, |
Pavel Shilovsky | ca8aa29 | 2012-12-21 15:05:47 +0400 | [diff] [blame] | 2533 | &iocb->ki_pos); |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2534 | mutex_unlock(&inode->i_mutex); |
| 2535 | } |
| 2536 | |
| 2537 | if (rc > 0 || rc == -EIOCBQUEUED) { |
| 2538 | ssize_t err; |
| 2539 | |
| 2540 | err = generic_write_sync(file, pos, rc); |
| 2541 | if (err < 0 && rc > 0) |
| 2542 | rc = err; |
| 2543 | } |
| 2544 | |
| 2545 | up_read(&cinode->lock_sem); |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2546 | return rc; |
| 2547 | } |
| 2548 | |
| 2549 | ssize_t |
| 2550 | cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov, |
| 2551 | unsigned long nr_segs, loff_t pos) |
| 2552 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2553 | struct inode *inode = file_inode(iocb->ki_filp); |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2554 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
| 2555 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 2556 | struct cifsFileInfo *cfile = (struct cifsFileInfo *) |
| 2557 | iocb->ki_filp->private_data; |
| 2558 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Pavel Shilovsky | 88cf75a | 2012-12-21 15:07:52 +0400 | [diff] [blame] | 2559 | ssize_t written; |
Pavel Shilovsky | ca8aa29 | 2012-12-21 15:05:47 +0400 | [diff] [blame] | 2560 | |
Pavel Shilovsky | 88cf75a | 2012-12-21 15:07:52 +0400 | [diff] [blame] | 2561 | if (cinode->clientCanCacheAll) { |
| 2562 | if (cap_unix(tcon->ses) && |
| 2563 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) |
| 2564 | && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) |
| 2565 | return generic_file_aio_write(iocb, iov, nr_segs, pos); |
| 2566 | return cifs_writev(iocb, iov, nr_segs, pos); |
Pavel Shilovsky | c299dd0 | 2012-12-06 22:07:52 +0400 | [diff] [blame] | 2567 | } |
Pavel Shilovsky | ca8aa29 | 2012-12-21 15:05:47 +0400 | [diff] [blame] | 2568 | /* |
| 2569 | * For non-oplocked files in strict cache mode we need to write the data |
| 2570 | * to the server exactly from the pos to pos+len-1 rather than flush all |
| 2571 | * affected pages because it may cause a error with mandatory locks on |
| 2572 | * these pages but not on the region from pos to ppos+len-1. |
| 2573 | */ |
Pavel Shilovsky | 88cf75a | 2012-12-21 15:07:52 +0400 | [diff] [blame] | 2574 | written = cifs_user_writev(iocb, iov, nr_segs, pos); |
| 2575 | if (written > 0 && cinode->clientCanCacheRead) { |
| 2576 | /* |
| 2577 | * Windows 7 server can delay breaking level2 oplock if a write |
| 2578 | * request comes - break it on the client to prevent reading |
| 2579 | * an old data. |
| 2580 | */ |
| 2581 | cifs_invalidate_mapping(inode); |
| 2582 | cFYI(1, "Set no oplock for inode=%p after a write operation", |
| 2583 | inode); |
| 2584 | cinode->clientCanCacheRead = false; |
| 2585 | } |
| 2586 | return written; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2587 | } |
| 2588 | |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2589 | static struct cifs_readdata * |
Jeff Layton | f4e49cd | 2012-09-18 16:20:36 -0700 | [diff] [blame] | 2590 | cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete) |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2591 | { |
| 2592 | struct cifs_readdata *rdata; |
Jeff Layton | f4e49cd | 2012-09-18 16:20:36 -0700 | [diff] [blame] | 2593 | |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2594 | rdata = kzalloc(sizeof(*rdata) + (sizeof(struct page *) * nr_pages), |
| 2595 | GFP_KERNEL); |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2596 | if (rdata != NULL) { |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2597 | kref_init(&rdata->refcount); |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2598 | INIT_LIST_HEAD(&rdata->list); |
| 2599 | init_completion(&rdata->done); |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2600 | INIT_WORK(&rdata->work, complete); |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2601 | } |
Jeff Layton | f4e49cd | 2012-09-18 16:20:36 -0700 | [diff] [blame] | 2602 | |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2603 | return rdata; |
| 2604 | } |
| 2605 | |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2606 | void |
| 2607 | cifs_readdata_release(struct kref *refcount) |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2608 | { |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2609 | struct cifs_readdata *rdata = container_of(refcount, |
| 2610 | struct cifs_readdata, refcount); |
| 2611 | |
| 2612 | if (rdata->cfile) |
| 2613 | cifsFileInfo_put(rdata->cfile); |
| 2614 | |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2615 | kfree(rdata); |
| 2616 | } |
| 2617 | |
Jeff Layton | 2a1bb13 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2618 | static int |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2619 | cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages) |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2620 | { |
| 2621 | int rc = 0; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2622 | struct page *page; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2623 | unsigned int i; |
| 2624 | |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2625 | for (i = 0; i < nr_pages; i++) { |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2626 | page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM); |
| 2627 | if (!page) { |
| 2628 | rc = -ENOMEM; |
| 2629 | break; |
| 2630 | } |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2631 | rdata->pages[i] = page; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | if (rc) { |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2635 | for (i = 0; i < nr_pages; i++) { |
| 2636 | put_page(rdata->pages[i]); |
| 2637 | rdata->pages[i] = NULL; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2638 | } |
| 2639 | } |
| 2640 | return rc; |
| 2641 | } |
| 2642 | |
| 2643 | static void |
| 2644 | cifs_uncached_readdata_release(struct kref *refcount) |
| 2645 | { |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2646 | struct cifs_readdata *rdata = container_of(refcount, |
| 2647 | struct cifs_readdata, refcount); |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2648 | unsigned int i; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2649 | |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2650 | for (i = 0; i < rdata->nr_pages; i++) { |
| 2651 | put_page(rdata->pages[i]); |
| 2652 | rdata->pages[i] = NULL; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2653 | } |
| 2654 | cifs_readdata_release(refcount); |
| 2655 | } |
| 2656 | |
| 2657 | static int |
Jeff Layton | 2a1bb13 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2658 | cifs_retry_async_readv(struct cifs_readdata *rdata) |
| 2659 | { |
| 2660 | int rc; |
Pavel Shilovsky | fc9c596 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 2661 | struct TCP_Server_Info *server; |
| 2662 | |
| 2663 | server = tlink_tcon(rdata->cfile->tlink)->ses->server; |
Jeff Layton | 2a1bb13 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2664 | |
| 2665 | do { |
| 2666 | if (rdata->cfile->invalidHandle) { |
| 2667 | rc = cifs_reopen_file(rdata->cfile, true); |
| 2668 | if (rc != 0) |
| 2669 | continue; |
| 2670 | } |
Pavel Shilovsky | fc9c596 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 2671 | rc = server->ops->async_readv(rdata); |
Jeff Layton | 2a1bb13 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2672 | } while (rc == -EAGAIN); |
| 2673 | |
| 2674 | return rc; |
| 2675 | } |
| 2676 | |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2677 | /** |
| 2678 | * cifs_readdata_to_iov - copy data from pages in response to an iovec |
| 2679 | * @rdata: the readdata response with list of pages holding data |
| 2680 | * @iov: vector in which we should copy the data |
| 2681 | * @nr_segs: number of segments in vector |
| 2682 | * @offset: offset into file of the first iovec |
| 2683 | * @copied: used to return the amount of data copied to the iov |
| 2684 | * |
| 2685 | * This function copies data from a list of pages in a readdata response into |
| 2686 | * an array of iovecs. It will first calculate where the data should go |
| 2687 | * based on the info in the readdata and then copy the data into that spot. |
| 2688 | */ |
| 2689 | static ssize_t |
| 2690 | cifs_readdata_to_iov(struct cifs_readdata *rdata, const struct iovec *iov, |
| 2691 | unsigned long nr_segs, loff_t offset, ssize_t *copied) |
| 2692 | { |
| 2693 | int rc = 0; |
| 2694 | struct iov_iter ii; |
| 2695 | size_t pos = rdata->offset - offset; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2696 | ssize_t remaining = rdata->bytes; |
| 2697 | unsigned char *pdata; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2698 | unsigned int i; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2699 | |
| 2700 | /* set up iov_iter and advance to the correct offset */ |
| 2701 | iov_iter_init(&ii, iov, nr_segs, iov_length(iov, nr_segs), 0); |
| 2702 | iov_iter_advance(&ii, pos); |
| 2703 | |
| 2704 | *copied = 0; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2705 | for (i = 0; i < rdata->nr_pages; i++) { |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2706 | ssize_t copy; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2707 | struct page *page = rdata->pages[i]; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2708 | |
| 2709 | /* copy a whole page or whatever's left */ |
| 2710 | copy = min_t(ssize_t, remaining, PAGE_SIZE); |
| 2711 | |
| 2712 | /* ...but limit it to whatever space is left in the iov */ |
| 2713 | copy = min_t(ssize_t, copy, iov_iter_count(&ii)); |
| 2714 | |
| 2715 | /* go while there's data to be copied and no errors */ |
| 2716 | if (copy && !rc) { |
| 2717 | pdata = kmap(page); |
| 2718 | rc = memcpy_toiovecend(ii.iov, pdata, ii.iov_offset, |
| 2719 | (int)copy); |
| 2720 | kunmap(page); |
| 2721 | if (!rc) { |
| 2722 | *copied += copy; |
| 2723 | remaining -= copy; |
| 2724 | iov_iter_advance(&ii, copy); |
| 2725 | } |
| 2726 | } |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | return rc; |
| 2730 | } |
| 2731 | |
| 2732 | static void |
| 2733 | cifs_uncached_readv_complete(struct work_struct *work) |
| 2734 | { |
| 2735 | struct cifs_readdata *rdata = container_of(work, |
| 2736 | struct cifs_readdata, work); |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2737 | |
| 2738 | complete(&rdata->done); |
| 2739 | kref_put(&rdata->refcount, cifs_uncached_readdata_release); |
| 2740 | } |
| 2741 | |
| 2742 | static int |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 2743 | cifs_uncached_read_into_pages(struct TCP_Server_Info *server, |
| 2744 | struct cifs_readdata *rdata, unsigned int len) |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2745 | { |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 2746 | int total_read = 0, result = 0; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2747 | unsigned int i; |
| 2748 | unsigned int nr_pages = rdata->nr_pages; |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 2749 | struct kvec iov; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2750 | |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 2751 | rdata->tailsz = PAGE_SIZE; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2752 | for (i = 0; i < nr_pages; i++) { |
| 2753 | struct page *page = rdata->pages[i]; |
| 2754 | |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 2755 | if (len >= PAGE_SIZE) { |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2756 | /* enough data to fill the page */ |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 2757 | iov.iov_base = kmap(page); |
| 2758 | iov.iov_len = PAGE_SIZE; |
| 2759 | cFYI(1, "%u: iov_base=%p iov_len=%zu", |
| 2760 | i, iov.iov_base, iov.iov_len); |
| 2761 | len -= PAGE_SIZE; |
| 2762 | } else if (len > 0) { |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2763 | /* enough for partial page, fill and zero the rest */ |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 2764 | iov.iov_base = kmap(page); |
| 2765 | iov.iov_len = len; |
| 2766 | cFYI(1, "%u: iov_base=%p iov_len=%zu", |
| 2767 | i, iov.iov_base, iov.iov_len); |
| 2768 | memset(iov.iov_base + len, '\0', PAGE_SIZE - len); |
| 2769 | rdata->tailsz = len; |
| 2770 | len = 0; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2771 | } else { |
| 2772 | /* no need to hold page hostage */ |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2773 | rdata->pages[i] = NULL; |
| 2774 | rdata->nr_pages--; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2775 | put_page(page); |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 2776 | continue; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2777 | } |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 2778 | |
| 2779 | result = cifs_readv_from_socket(server, &iov, 1, iov.iov_len); |
| 2780 | kunmap(page); |
| 2781 | if (result < 0) |
| 2782 | break; |
| 2783 | |
| 2784 | total_read += result; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2785 | } |
| 2786 | |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 2787 | return total_read > 0 ? total_read : result; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2788 | } |
| 2789 | |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 2790 | static ssize_t |
| 2791 | cifs_iovec_read(struct file *file, const struct iovec *iov, |
| 2792 | unsigned long nr_segs, loff_t *poffset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | { |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2794 | ssize_t rc; |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 2795 | size_t len, cur_len; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2796 | ssize_t total_read = 0; |
| 2797 | loff_t offset = *poffset; |
| 2798 | unsigned int npages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | struct cifs_sb_info *cifs_sb; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2800 | struct cifs_tcon *tcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | struct cifsFileInfo *open_file; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2802 | struct cifs_readdata *rdata, *tmp; |
| 2803 | struct list_head rdata_list; |
| 2804 | pid_t pid; |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 2805 | |
| 2806 | if (!nr_segs) |
| 2807 | return 0; |
| 2808 | |
| 2809 | len = iov_length(iov, nr_segs); |
| 2810 | if (!len) |
| 2811 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2813 | INIT_LIST_HEAD(&rdata_list); |
Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 2814 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); |
Joe Perches | c21dfb6 | 2010-07-12 13:50:14 -0700 | [diff] [blame] | 2815 | open_file = file->private_data; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2816 | tcon = tlink_tcon(open_file->tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | |
Pavel Shilovsky | fc9c596 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 2818 | if (!tcon->ses->server->ops->async_readv) |
| 2819 | return -ENOSYS; |
| 2820 | |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 2821 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
| 2822 | pid = open_file->pid; |
| 2823 | else |
| 2824 | pid = current->tgid; |
| 2825 | |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2826 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 2827 | cFYI(1, "attempting read on write only file instance"); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2828 | |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2829 | do { |
| 2830 | cur_len = min_t(const size_t, len - total_read, cifs_sb->rsize); |
| 2831 | npages = DIV_ROUND_UP(cur_len, PAGE_SIZE); |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 2832 | |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2833 | /* allocate a readdata struct */ |
| 2834 | rdata = cifs_readdata_alloc(npages, |
| 2835 | cifs_uncached_readv_complete); |
| 2836 | if (!rdata) { |
| 2837 | rc = -ENOMEM; |
| 2838 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | } |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 2840 | |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2841 | rc = cifs_read_allocate_pages(rdata, npages); |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2842 | if (rc) |
| 2843 | goto error; |
| 2844 | |
| 2845 | rdata->cfile = cifsFileInfo_get(open_file); |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 2846 | rdata->nr_pages = npages; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2847 | rdata->offset = offset; |
| 2848 | rdata->bytes = cur_len; |
| 2849 | rdata->pid = pid; |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 2850 | rdata->pagesz = PAGE_SIZE; |
| 2851 | rdata->read_into_pages = cifs_uncached_read_into_pages; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2852 | |
| 2853 | rc = cifs_retry_async_readv(rdata); |
| 2854 | error: |
| 2855 | if (rc) { |
| 2856 | kref_put(&rdata->refcount, |
| 2857 | cifs_uncached_readdata_release); |
| 2858 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | } |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2860 | |
| 2861 | list_add_tail(&rdata->list, &rdata_list); |
| 2862 | offset += cur_len; |
| 2863 | len -= cur_len; |
| 2864 | } while (len > 0); |
| 2865 | |
| 2866 | /* if at least one read request send succeeded, then reset rc */ |
| 2867 | if (!list_empty(&rdata_list)) |
| 2868 | rc = 0; |
| 2869 | |
| 2870 | /* the loop below should proceed in the order of increasing offsets */ |
| 2871 | restart_loop: |
| 2872 | list_for_each_entry_safe(rdata, tmp, &rdata_list, list) { |
| 2873 | if (!rc) { |
| 2874 | ssize_t copied; |
| 2875 | |
| 2876 | /* FIXME: freezable sleep too? */ |
| 2877 | rc = wait_for_completion_killable(&rdata->done); |
| 2878 | if (rc) |
| 2879 | rc = -EINTR; |
| 2880 | else if (rdata->result) |
| 2881 | rc = rdata->result; |
| 2882 | else { |
| 2883 | rc = cifs_readdata_to_iov(rdata, iov, |
| 2884 | nr_segs, *poffset, |
| 2885 | &copied); |
| 2886 | total_read += copied; |
| 2887 | } |
| 2888 | |
| 2889 | /* resend call if it's a retryable error */ |
| 2890 | if (rc == -EAGAIN) { |
| 2891 | rc = cifs_retry_async_readv(rdata); |
| 2892 | goto restart_loop; |
| 2893 | } |
| 2894 | } |
| 2895 | list_del_init(&rdata->list); |
| 2896 | kref_put(&rdata->refcount, cifs_uncached_readdata_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2897 | } |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 2898 | |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2899 | cifs_stats_bytes_read(tcon, total_read); |
| 2900 | *poffset += total_read; |
| 2901 | |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 2902 | /* mask nodata case */ |
| 2903 | if (rc == -ENODATA) |
| 2904 | rc = 0; |
| 2905 | |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2906 | return total_read ? total_read : rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | } |
| 2908 | |
Pavel Shilovsky | 0b81c1c | 2011-03-10 10:11:05 +0300 | [diff] [blame] | 2909 | ssize_t cifs_user_readv(struct kiocb *iocb, const struct iovec *iov, |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 2910 | unsigned long nr_segs, loff_t pos) |
| 2911 | { |
| 2912 | ssize_t read; |
| 2913 | |
| 2914 | read = cifs_iovec_read(iocb->ki_filp, iov, nr_segs, &pos); |
| 2915 | if (read > 0) |
| 2916 | iocb->ki_pos = pos; |
| 2917 | |
| 2918 | return read; |
| 2919 | } |
| 2920 | |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2921 | ssize_t |
| 2922 | cifs_strict_readv(struct kiocb *iocb, const struct iovec *iov, |
| 2923 | unsigned long nr_segs, loff_t pos) |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 2924 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2925 | struct inode *inode = file_inode(iocb->ki_filp); |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2926 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
| 2927 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 2928 | struct cifsFileInfo *cfile = (struct cifsFileInfo *) |
| 2929 | iocb->ki_filp->private_data; |
| 2930 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
| 2931 | int rc = -EACCES; |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 2932 | |
| 2933 | /* |
| 2934 | * In strict cache mode we need to read from the server all the time |
| 2935 | * if we don't have level II oplock because the server can delay mtime |
| 2936 | * change - so we can't make a decision about inode invalidating. |
| 2937 | * And we can also fail with pagereading if there are mandatory locks |
| 2938 | * on pages affected by this read but not on the region from pos to |
| 2939 | * pos+len-1. |
| 2940 | */ |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2941 | if (!cinode->clientCanCacheRead) |
| 2942 | return cifs_user_readv(iocb, iov, nr_segs, pos); |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 2943 | |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2944 | if (cap_unix(tcon->ses) && |
| 2945 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && |
| 2946 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) |
| 2947 | return generic_file_aio_read(iocb, iov, nr_segs, pos); |
| 2948 | |
| 2949 | /* |
| 2950 | * We need to hold the sem to be sure nobody modifies lock list |
| 2951 | * with a brlock that prevents reading. |
| 2952 | */ |
| 2953 | down_read(&cinode->lock_sem); |
| 2954 | if (!cifs_find_lock_conflict(cfile, pos, iov_length(iov, nr_segs), |
| 2955 | tcon->ses->server->vals->shared_lock_type, |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 2956 | NULL, CIFS_READ_OP)) |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2957 | rc = generic_file_aio_read(iocb, iov, nr_segs, pos); |
| 2958 | up_read(&cinode->lock_sem); |
| 2959 | return rc; |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 2960 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2961 | |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 2962 | static ssize_t |
| 2963 | cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2964 | { |
| 2965 | int rc = -EACCES; |
| 2966 | unsigned int bytes_read = 0; |
| 2967 | unsigned int total_read; |
| 2968 | unsigned int current_read_size; |
Jeff Layton | 5eba8ab | 2011-10-19 15:30:26 -0400 | [diff] [blame] | 2969 | unsigned int rsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | struct cifs_sb_info *cifs_sb; |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 2971 | struct cifs_tcon *tcon; |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 2972 | struct TCP_Server_Info *server; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2973 | unsigned int xid; |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 2974 | char *cur_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | struct cifsFileInfo *open_file; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 2976 | struct cifs_io_parms io_parms; |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 2977 | int buf_type = CIFS_NO_BUFFER; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 2978 | __u32 pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2980 | xid = get_xid(); |
Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 2981 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | |
Jeff Layton | 5eba8ab | 2011-10-19 15:30:26 -0400 | [diff] [blame] | 2983 | /* FIXME: set up handlers for larger reads and/or convert to async */ |
| 2984 | rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize); |
| 2985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | if (file->private_data == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 2987 | rc = -EBADF; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2988 | free_xid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 2989 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | } |
Joe Perches | c21dfb6 | 2010-07-12 13:50:14 -0700 | [diff] [blame] | 2991 | open_file = file->private_data; |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 2992 | tcon = tlink_tcon(open_file->tlink); |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 2993 | server = tcon->ses->server; |
| 2994 | |
| 2995 | if (!server->ops->sync_read) { |
| 2996 | free_xid(xid); |
| 2997 | return -ENOSYS; |
| 2998 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 3000 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
| 3001 | pid = open_file->pid; |
| 3002 | else |
| 3003 | pid = current->tgid; |
| 3004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 3006 | cFYI(1, "attempting read on write only file instance"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3007 | |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3008 | for (total_read = 0, cur_offset = read_data; read_size > total_read; |
| 3009 | total_read += bytes_read, cur_offset += bytes_read) { |
Jeff Layton | 5eba8ab | 2011-10-19 15:30:26 -0400 | [diff] [blame] | 3010 | current_read_size = min_t(uint, read_size - total_read, rsize); |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 3011 | /* |
| 3012 | * For windows me and 9x we do not want to request more than it |
| 3013 | * negotiated since it will refuse the read then. |
| 3014 | */ |
| 3015 | if ((tcon->ses) && !(tcon->ses->capabilities & |
| 3016 | tcon->ses->server->vals->cap_large_files)) { |
Dan Carpenter | 7748dd6 | 2011-10-18 12:41:35 +0300 | [diff] [blame] | 3017 | current_read_size = min_t(uint, current_read_size, |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 3018 | CIFSMaxBufSize); |
Steve French | f9f5c817 | 2005-09-15 23:06:38 -0700 | [diff] [blame] | 3019 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | rc = -EAGAIN; |
| 3021 | while (rc == -EAGAIN) { |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 3022 | if (open_file->invalidHandle) { |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 3023 | rc = cifs_reopen_file(open_file, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | if (rc != 0) |
| 3025 | break; |
| 3026 | } |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 3027 | io_parms.pid = pid; |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 3028 | io_parms.tcon = tcon; |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3029 | io_parms.offset = *offset; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 3030 | io_parms.length = current_read_size; |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3031 | rc = server->ops->sync_read(xid, open_file, &io_parms, |
| 3032 | &bytes_read, &cur_offset, |
| 3033 | &buf_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | } |
| 3035 | if (rc || (bytes_read == 0)) { |
| 3036 | if (total_read) { |
| 3037 | break; |
| 3038 | } else { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3039 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | return rc; |
| 3041 | } |
| 3042 | } else { |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 3043 | cifs_stats_bytes_read(tcon, total_read); |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3044 | *offset += bytes_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | } |
| 3046 | } |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3047 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | return total_read; |
| 3049 | } |
| 3050 | |
Jeff Layton | ca83ce3 | 2011-04-12 09:13:44 -0400 | [diff] [blame] | 3051 | /* |
| 3052 | * If the page is mmap'ed into a process' page tables, then we need to make |
| 3053 | * sure that it doesn't change while being written back. |
| 3054 | */ |
| 3055 | static int |
| 3056 | cifs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 3057 | { |
| 3058 | struct page *page = vmf->page; |
| 3059 | |
| 3060 | lock_page(page); |
| 3061 | return VM_FAULT_LOCKED; |
| 3062 | } |
| 3063 | |
| 3064 | static struct vm_operations_struct cifs_file_vm_ops = { |
| 3065 | .fault = filemap_fault, |
| 3066 | .page_mkwrite = cifs_page_mkwrite, |
Konstantin Khlebnikov | 0b173bc | 2012-10-08 16:28:46 -0700 | [diff] [blame] | 3067 | .remap_pages = generic_file_remap_pages, |
Jeff Layton | ca83ce3 | 2011-04-12 09:13:44 -0400 | [diff] [blame] | 3068 | }; |
| 3069 | |
Pavel Shilovsky | 7a6a19b | 2010-12-14 11:29:51 +0300 | [diff] [blame] | 3070 | int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma) |
| 3071 | { |
| 3072 | int rc, xid; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3073 | struct inode *inode = file_inode(file); |
Pavel Shilovsky | 7a6a19b | 2010-12-14 11:29:51 +0300 | [diff] [blame] | 3074 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3075 | xid = get_xid(); |
Pavel Shilovsky | 7a6a19b | 2010-12-14 11:29:51 +0300 | [diff] [blame] | 3076 | |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 3077 | if (!CIFS_I(inode)->clientCanCacheRead) { |
| 3078 | rc = cifs_invalidate_mapping(inode); |
| 3079 | if (rc) |
| 3080 | return rc; |
| 3081 | } |
Pavel Shilovsky | 7a6a19b | 2010-12-14 11:29:51 +0300 | [diff] [blame] | 3082 | |
| 3083 | rc = generic_file_mmap(file, vma); |
Jeff Layton | ca83ce3 | 2011-04-12 09:13:44 -0400 | [diff] [blame] | 3084 | if (rc == 0) |
| 3085 | vma->vm_ops = &cifs_file_vm_ops; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3086 | free_xid(xid); |
Pavel Shilovsky | 7a6a19b | 2010-12-14 11:29:51 +0300 | [diff] [blame] | 3087 | return rc; |
| 3088 | } |
| 3089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | int cifs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 3091 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | int rc, xid; |
| 3093 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3094 | xid = get_xid(); |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 3095 | rc = cifs_revalidate_file(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | if (rc) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 3097 | cFYI(1, "Validation prior to mmap failed, error=%d", rc); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3098 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3099 | return rc; |
| 3100 | } |
| 3101 | rc = generic_file_mmap(file, vma); |
Jeff Layton | ca83ce3 | 2011-04-12 09:13:44 -0400 | [diff] [blame] | 3102 | if (rc == 0) |
| 3103 | vma->vm_ops = &cifs_file_vm_ops; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3104 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | return rc; |
| 3106 | } |
| 3107 | |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3108 | static void |
| 3109 | cifs_readv_complete(struct work_struct *work) |
| 3110 | { |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3111 | unsigned int i; |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3112 | struct cifs_readdata *rdata = container_of(work, |
| 3113 | struct cifs_readdata, work); |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3114 | |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3115 | for (i = 0; i < rdata->nr_pages; i++) { |
| 3116 | struct page *page = rdata->pages[i]; |
| 3117 | |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3118 | lru_cache_add_file(page); |
| 3119 | |
| 3120 | if (rdata->result == 0) { |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3121 | flush_dcache_page(page); |
| 3122 | SetPageUptodate(page); |
| 3123 | } |
| 3124 | |
| 3125 | unlock_page(page); |
| 3126 | |
| 3127 | if (rdata->result == 0) |
| 3128 | cifs_readpage_to_fscache(rdata->mapping->host, page); |
| 3129 | |
| 3130 | page_cache_release(page); |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3131 | rdata->pages[i] = NULL; |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3132 | } |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3133 | kref_put(&rdata->refcount, cifs_readdata_release); |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3134 | } |
| 3135 | |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3136 | static int |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3137 | cifs_readpages_read_into_pages(struct TCP_Server_Info *server, |
| 3138 | struct cifs_readdata *rdata, unsigned int len) |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3139 | { |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3140 | int total_read = 0, result = 0; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3141 | unsigned int i; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3142 | u64 eof; |
| 3143 | pgoff_t eof_index; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3144 | unsigned int nr_pages = rdata->nr_pages; |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3145 | struct kvec iov; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3146 | |
| 3147 | /* determine the eof that the server (probably) has */ |
| 3148 | eof = CIFS_I(rdata->mapping->host)->server_eof; |
| 3149 | eof_index = eof ? (eof - 1) >> PAGE_CACHE_SHIFT : 0; |
| 3150 | cFYI(1, "eof=%llu eof_index=%lu", eof, eof_index); |
| 3151 | |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3152 | rdata->tailsz = PAGE_CACHE_SIZE; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3153 | for (i = 0; i < nr_pages; i++) { |
| 3154 | struct page *page = rdata->pages[i]; |
| 3155 | |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3156 | if (len >= PAGE_CACHE_SIZE) { |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3157 | /* enough data to fill the page */ |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3158 | iov.iov_base = kmap(page); |
| 3159 | iov.iov_len = PAGE_CACHE_SIZE; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3160 | cFYI(1, "%u: idx=%lu iov_base=%p iov_len=%zu", |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3161 | i, page->index, iov.iov_base, iov.iov_len); |
| 3162 | len -= PAGE_CACHE_SIZE; |
| 3163 | } else if (len > 0) { |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3164 | /* enough for partial page, fill and zero the rest */ |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3165 | iov.iov_base = kmap(page); |
| 3166 | iov.iov_len = len; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3167 | cFYI(1, "%u: idx=%lu iov_base=%p iov_len=%zu", |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3168 | i, page->index, iov.iov_base, iov.iov_len); |
| 3169 | memset(iov.iov_base + len, |
| 3170 | '\0', PAGE_CACHE_SIZE - len); |
| 3171 | rdata->tailsz = len; |
| 3172 | len = 0; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3173 | } else if (page->index > eof_index) { |
| 3174 | /* |
| 3175 | * The VFS will not try to do readahead past the |
| 3176 | * i_size, but it's possible that we have outstanding |
| 3177 | * writes with gaps in the middle and the i_size hasn't |
| 3178 | * caught up yet. Populate those with zeroed out pages |
| 3179 | * to prevent the VFS from repeatedly attempting to |
| 3180 | * fill them until the writes are flushed. |
| 3181 | */ |
| 3182 | zero_user(page, 0, PAGE_CACHE_SIZE); |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3183 | lru_cache_add_file(page); |
| 3184 | flush_dcache_page(page); |
| 3185 | SetPageUptodate(page); |
| 3186 | unlock_page(page); |
| 3187 | page_cache_release(page); |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3188 | rdata->pages[i] = NULL; |
| 3189 | rdata->nr_pages--; |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3190 | continue; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3191 | } else { |
| 3192 | /* no need to hold page hostage */ |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3193 | lru_cache_add_file(page); |
| 3194 | unlock_page(page); |
| 3195 | page_cache_release(page); |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3196 | rdata->pages[i] = NULL; |
| 3197 | rdata->nr_pages--; |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3198 | continue; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3199 | } |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3200 | |
| 3201 | result = cifs_readv_from_socket(server, &iov, 1, iov.iov_len); |
| 3202 | kunmap(page); |
| 3203 | if (result < 0) |
| 3204 | break; |
| 3205 | |
| 3206 | total_read += result; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3207 | } |
| 3208 | |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3209 | return total_read > 0 ? total_read : result; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3210 | } |
| 3211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | static int cifs_readpages(struct file *file, struct address_space *mapping, |
| 3213 | struct list_head *page_list, unsigned num_pages) |
| 3214 | { |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3215 | int rc; |
| 3216 | struct list_head tmplist; |
| 3217 | struct cifsFileInfo *open_file = file->private_data; |
| 3218 | struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); |
| 3219 | unsigned int rsize = cifs_sb->rsize; |
| 3220 | pid_t pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3222 | /* |
| 3223 | * Give up immediately if rsize is too small to read an entire page. |
| 3224 | * The VFS will fall back to readpage. We should never reach this |
| 3225 | * point however since we set ra_pages to 0 when the rsize is smaller |
| 3226 | * than a cache page. |
| 3227 | */ |
| 3228 | if (unlikely(rsize < PAGE_CACHE_SIZE)) |
| 3229 | return 0; |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 3230 | |
Suresh Jayaraman | 5669823 | 2010-07-05 18:13:25 +0530 | [diff] [blame] | 3231 | /* |
| 3232 | * Reads as many pages as possible from fscache. Returns -ENOBUFS |
| 3233 | * immediately if the cookie is negative |
| 3234 | */ |
| 3235 | rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list, |
| 3236 | &num_pages); |
| 3237 | if (rc == 0) |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3238 | return rc; |
Suresh Jayaraman | 5669823 | 2010-07-05 18:13:25 +0530 | [diff] [blame] | 3239 | |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 3240 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
| 3241 | pid = open_file->pid; |
| 3242 | else |
| 3243 | pid = current->tgid; |
| 3244 | |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3245 | rc = 0; |
| 3246 | INIT_LIST_HEAD(&tmplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3247 | |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3248 | cFYI(1, "%s: file=%p mapping=%p num_pages=%u", __func__, file, |
| 3249 | mapping, num_pages); |
| 3250 | |
| 3251 | /* |
| 3252 | * Start with the page at end of list and move it to private |
| 3253 | * list. Do the same with any following pages until we hit |
| 3254 | * the rsize limit, hit an index discontinuity, or run out of |
| 3255 | * pages. Issue the async read and then start the loop again |
| 3256 | * until the list is empty. |
| 3257 | * |
| 3258 | * Note that list order is important. The page_list is in |
| 3259 | * the order of declining indexes. When we put the pages in |
| 3260 | * the rdata->pages, then we want them in increasing order. |
| 3261 | */ |
| 3262 | while (!list_empty(page_list)) { |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3263 | unsigned int i; |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3264 | unsigned int bytes = PAGE_CACHE_SIZE; |
| 3265 | unsigned int expected_index; |
| 3266 | unsigned int nr_pages = 1; |
| 3267 | loff_t offset; |
| 3268 | struct page *page, *tpage; |
| 3269 | struct cifs_readdata *rdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | |
| 3271 | page = list_entry(page_list->prev, struct page, lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3273 | /* |
| 3274 | * Lock the page and put it in the cache. Since no one else |
| 3275 | * should have access to this page, we're safe to simply set |
| 3276 | * PG_locked without checking it first. |
| 3277 | */ |
| 3278 | __set_page_locked(page); |
| 3279 | rc = add_to_page_cache_locked(page, mapping, |
| 3280 | page->index, GFP_KERNEL); |
| 3281 | |
| 3282 | /* give up if we can't stick it in the cache */ |
| 3283 | if (rc) { |
| 3284 | __clear_page_locked(page); |
| 3285 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3288 | /* move first page to the tmplist */ |
| 3289 | offset = (loff_t)page->index << PAGE_CACHE_SHIFT; |
| 3290 | list_move_tail(&page->lru, &tmplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3292 | /* now try and add more pages onto the request */ |
| 3293 | expected_index = page->index + 1; |
| 3294 | list_for_each_entry_safe_reverse(page, tpage, page_list, lru) { |
| 3295 | /* discontinuity ? */ |
| 3296 | if (page->index != expected_index) |
| 3297 | break; |
| 3298 | |
| 3299 | /* would this page push the read over the rsize? */ |
| 3300 | if (bytes + PAGE_CACHE_SIZE > rsize) |
| 3301 | break; |
| 3302 | |
| 3303 | __set_page_locked(page); |
| 3304 | if (add_to_page_cache_locked(page, mapping, |
| 3305 | page->index, GFP_KERNEL)) { |
| 3306 | __clear_page_locked(page); |
| 3307 | break; |
| 3308 | } |
| 3309 | list_move_tail(&page->lru, &tmplist); |
| 3310 | bytes += PAGE_CACHE_SIZE; |
| 3311 | expected_index++; |
| 3312 | nr_pages++; |
| 3313 | } |
| 3314 | |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3315 | rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete); |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3316 | if (!rdata) { |
| 3317 | /* best to give up if we're out of mem */ |
| 3318 | list_for_each_entry_safe(page, tpage, &tmplist, lru) { |
| 3319 | list_del(&page->lru); |
| 3320 | lru_cache_add_file(page); |
| 3321 | unlock_page(page); |
| 3322 | page_cache_release(page); |
| 3323 | } |
| 3324 | rc = -ENOMEM; |
| 3325 | break; |
| 3326 | } |
| 3327 | |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3328 | rdata->cfile = cifsFileInfo_get(open_file); |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3329 | rdata->mapping = mapping; |
| 3330 | rdata->offset = offset; |
| 3331 | rdata->bytes = bytes; |
| 3332 | rdata->pid = pid; |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3333 | rdata->pagesz = PAGE_CACHE_SIZE; |
| 3334 | rdata->read_into_pages = cifs_readpages_read_into_pages; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3335 | |
| 3336 | list_for_each_entry_safe(page, tpage, &tmplist, lru) { |
| 3337 | list_del(&page->lru); |
| 3338 | rdata->pages[rdata->nr_pages++] = page; |
| 3339 | } |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3340 | |
Jeff Layton | 2a1bb13 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3341 | rc = cifs_retry_async_readv(rdata); |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3342 | if (rc != 0) { |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3343 | for (i = 0; i < rdata->nr_pages; i++) { |
| 3344 | page = rdata->pages[i]; |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3345 | lru_cache_add_file(page); |
| 3346 | unlock_page(page); |
| 3347 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | } |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3349 | kref_put(&rdata->refcount, cifs_readdata_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | break; |
| 3351 | } |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3352 | |
| 3353 | kref_put(&rdata->refcount, cifs_readdata_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | } |
| 3355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | return rc; |
| 3357 | } |
| 3358 | |
| 3359 | static int cifs_readpage_worker(struct file *file, struct page *page, |
| 3360 | loff_t *poffset) |
| 3361 | { |
| 3362 | char *read_data; |
| 3363 | int rc; |
| 3364 | |
Suresh Jayaraman | 5669823 | 2010-07-05 18:13:25 +0530 | [diff] [blame] | 3365 | /* Is the page cached? */ |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3366 | rc = cifs_readpage_from_fscache(file_inode(file), page); |
Suresh Jayaraman | 5669823 | 2010-07-05 18:13:25 +0530 | [diff] [blame] | 3367 | if (rc == 0) |
| 3368 | goto read_complete; |
| 3369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3370 | page_cache_get(page); |
| 3371 | read_data = kmap(page); |
| 3372 | /* for reads over a certain size could initiate async read ahead */ |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3374 | rc = cifs_read(file, read_data, PAGE_CACHE_SIZE, poffset); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3376 | if (rc < 0) |
| 3377 | goto io_error; |
| 3378 | else |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 3379 | cFYI(1, "Bytes read %d", rc); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3380 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3381 | file_inode(file)->i_atime = |
| 3382 | current_fs_time(file_inode(file)->i_sb); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | if (PAGE_CACHE_SIZE > rc) |
| 3385 | memset(read_data + rc, 0, PAGE_CACHE_SIZE - rc); |
| 3386 | |
| 3387 | flush_dcache_page(page); |
| 3388 | SetPageUptodate(page); |
Suresh Jayaraman | 9dc0655 | 2010-07-05 18:13:11 +0530 | [diff] [blame] | 3389 | |
| 3390 | /* send this page to the cache */ |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3391 | cifs_readpage_to_fscache(file_inode(file), page); |
Suresh Jayaraman | 9dc0655 | 2010-07-05 18:13:11 +0530 | [diff] [blame] | 3392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | rc = 0; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | io_error: |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3396 | kunmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3397 | page_cache_release(page); |
Suresh Jayaraman | 5669823 | 2010-07-05 18:13:25 +0530 | [diff] [blame] | 3398 | |
| 3399 | read_complete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | return rc; |
| 3401 | } |
| 3402 | |
| 3403 | static int cifs_readpage(struct file *file, struct page *page) |
| 3404 | { |
| 3405 | loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT; |
| 3406 | int rc = -EACCES; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3407 | unsigned int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3408 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3409 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | |
| 3411 | if (file->private_data == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 3412 | rc = -EBADF; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3413 | free_xid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 3414 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | } |
| 3416 | |
Jeff Layton | ac3aa2f | 2012-07-23 13:14:28 -0400 | [diff] [blame] | 3417 | cFYI(1, "readpage %p at offset %d 0x%x", |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 3418 | page, (int)offset, (int)offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3419 | |
| 3420 | rc = cifs_readpage_worker(file, page, &offset); |
| 3421 | |
| 3422 | unlock_page(page); |
| 3423 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3424 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | return rc; |
| 3426 | } |
| 3427 | |
Steve French | a403a0a | 2007-07-26 15:54:16 +0000 | [diff] [blame] | 3428 | static int is_inode_writable(struct cifsInodeInfo *cifs_inode) |
| 3429 | { |
| 3430 | struct cifsFileInfo *open_file; |
| 3431 | |
Jeff Layton | 4477288 | 2010-10-15 15:34:03 -0400 | [diff] [blame] | 3432 | spin_lock(&cifs_file_list_lock); |
Steve French | a403a0a | 2007-07-26 15:54:16 +0000 | [diff] [blame] | 3433 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { |
Jeff Layton | 2e396b8 | 2010-10-15 15:34:01 -0400 | [diff] [blame] | 3434 | if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { |
Jeff Layton | 4477288 | 2010-10-15 15:34:03 -0400 | [diff] [blame] | 3435 | spin_unlock(&cifs_file_list_lock); |
Steve French | a403a0a | 2007-07-26 15:54:16 +0000 | [diff] [blame] | 3436 | return 1; |
| 3437 | } |
| 3438 | } |
Jeff Layton | 4477288 | 2010-10-15 15:34:03 -0400 | [diff] [blame] | 3439 | spin_unlock(&cifs_file_list_lock); |
Steve French | a403a0a | 2007-07-26 15:54:16 +0000 | [diff] [blame] | 3440 | return 0; |
| 3441 | } |
| 3442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3443 | /* We do not want to update the file size from server for inodes |
| 3444 | open for write - to avoid races with writepage extending |
| 3445 | the file - in the future we could consider allowing |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3446 | refreshing the inode only on increases in the file size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | but this is tricky to do without racing with writebehind |
| 3448 | page caching in the current Linux kernel design */ |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 3449 | bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3450 | { |
Steve French | a403a0a | 2007-07-26 15:54:16 +0000 | [diff] [blame] | 3451 | if (!cifsInode) |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 3452 | return true; |
Steve French | 23e7dd7 | 2005-10-20 13:44:56 -0700 | [diff] [blame] | 3453 | |
Steve French | a403a0a | 2007-07-26 15:54:16 +0000 | [diff] [blame] | 3454 | if (is_inode_writable(cifsInode)) { |
| 3455 | /* This inode is open for write at least once */ |
Steve French | c32a0b6 | 2006-01-12 14:41:28 -0800 | [diff] [blame] | 3456 | struct cifs_sb_info *cifs_sb; |
| 3457 | |
Steve French | c32a0b6 | 2006-01-12 14:41:28 -0800 | [diff] [blame] | 3458 | cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 3459 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3460 | /* since no page cache to corrupt on directio |
Steve French | c32a0b6 | 2006-01-12 14:41:28 -0800 | [diff] [blame] | 3461 | we can change size safely */ |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 3462 | return true; |
Steve French | c32a0b6 | 2006-01-12 14:41:28 -0800 | [diff] [blame] | 3463 | } |
| 3464 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3465 | if (i_size_read(&cifsInode->vfs_inode) < end_of_file) |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 3466 | return true; |
Steve French | 7ba5263 | 2007-02-08 18:14:13 +0000 | [diff] [blame] | 3467 | |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 3468 | return false; |
Steve French | 23e7dd7 | 2005-10-20 13:44:56 -0700 | [diff] [blame] | 3469 | } else |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 3470 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | } |
| 3472 | |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 3473 | static int cifs_write_begin(struct file *file, struct address_space *mapping, |
| 3474 | loff_t pos, unsigned len, unsigned flags, |
| 3475 | struct page **pagep, void **fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3476 | { |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 3477 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
| 3478 | loff_t offset = pos & (PAGE_CACHE_SIZE - 1); |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 3479 | loff_t page_start = pos & PAGE_MASK; |
| 3480 | loff_t i_size; |
| 3481 | struct page *page; |
| 3482 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3483 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 3484 | cFYI(1, "write_begin from %lld len %d", (long long)pos, len); |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 3485 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 3486 | page = grab_cache_page_write_begin(mapping, index, flags); |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 3487 | if (!page) { |
| 3488 | rc = -ENOMEM; |
| 3489 | goto out; |
| 3490 | } |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 3491 | |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 3492 | if (PageUptodate(page)) |
| 3493 | goto out; |
Steve French | 8a23626 | 2007-03-06 00:31:00 +0000 | [diff] [blame] | 3494 | |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 3495 | /* |
| 3496 | * If we write a full page it will be up to date, no need to read from |
| 3497 | * the server. If the write is short, we'll end up doing a sync write |
| 3498 | * instead. |
| 3499 | */ |
| 3500 | if (len == PAGE_CACHE_SIZE) |
| 3501 | goto out; |
| 3502 | |
| 3503 | /* |
| 3504 | * optimize away the read when we have an oplock, and we're not |
| 3505 | * expecting to use any of the data we'd be reading in. That |
| 3506 | * is, when the page lies beyond the EOF, or straddles the EOF |
| 3507 | * and the write will cover all of the existing data. |
| 3508 | */ |
| 3509 | if (CIFS_I(mapping->host)->clientCanCacheRead) { |
| 3510 | i_size = i_size_read(mapping->host); |
| 3511 | if (page_start >= i_size || |
| 3512 | (offset == 0 && (pos + len) >= i_size)) { |
| 3513 | zero_user_segments(page, 0, offset, |
| 3514 | offset + len, |
| 3515 | PAGE_CACHE_SIZE); |
| 3516 | /* |
| 3517 | * PageChecked means that the parts of the page |
| 3518 | * to which we're not writing are considered up |
| 3519 | * to date. Once the data is copied to the |
| 3520 | * page, it can be set uptodate. |
| 3521 | */ |
| 3522 | SetPageChecked(page); |
| 3523 | goto out; |
| 3524 | } |
| 3525 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 3527 | if ((file->f_flags & O_ACCMODE) != O_WRONLY) { |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 3528 | /* |
| 3529 | * might as well read a page, it is fast enough. If we get |
| 3530 | * an error, we don't need to return it. cifs_write_end will |
| 3531 | * do a sync write instead since PG_uptodate isn't set. |
| 3532 | */ |
| 3533 | cifs_readpage_worker(file, page, &page_start); |
Steve French | 8a23626 | 2007-03-06 00:31:00 +0000 | [diff] [blame] | 3534 | } else { |
| 3535 | /* we could try using another file handle if there is one - |
| 3536 | but how would we lock it to prevent close of that handle |
| 3537 | racing with this read? In any case |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 3538 | this will be written out by write_end so is fine */ |
Steve French | 8a23626 | 2007-03-06 00:31:00 +0000 | [diff] [blame] | 3539 | } |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 3540 | out: |
| 3541 | *pagep = page; |
| 3542 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | } |
| 3544 | |
Suresh Jayaraman | 85f2d6b | 2010-07-05 18:13:00 +0530 | [diff] [blame] | 3545 | static int cifs_release_page(struct page *page, gfp_t gfp) |
| 3546 | { |
| 3547 | if (PagePrivate(page)) |
| 3548 | return 0; |
| 3549 | |
| 3550 | return cifs_fscache_release_page(page, gfp); |
| 3551 | } |
| 3552 | |
| 3553 | static void cifs_invalidate_page(struct page *page, unsigned long offset) |
| 3554 | { |
| 3555 | struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host); |
| 3556 | |
| 3557 | if (offset == 0) |
| 3558 | cifs_fscache_invalidate_page(page, &cifsi->vfs_inode); |
| 3559 | } |
| 3560 | |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 3561 | static int cifs_launder_page(struct page *page) |
| 3562 | { |
| 3563 | int rc = 0; |
| 3564 | loff_t range_start = page_offset(page); |
| 3565 | loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); |
| 3566 | struct writeback_control wbc = { |
| 3567 | .sync_mode = WB_SYNC_ALL, |
| 3568 | .nr_to_write = 0, |
| 3569 | .range_start = range_start, |
| 3570 | .range_end = range_end, |
| 3571 | }; |
| 3572 | |
| 3573 | cFYI(1, "Launder page: %p", page); |
| 3574 | |
| 3575 | if (clear_page_dirty_for_io(page)) |
| 3576 | rc = cifs_writepage_locked(page, &wbc); |
| 3577 | |
| 3578 | cifs_fscache_invalidate_page(page, page->mapping->host); |
| 3579 | return rc; |
| 3580 | } |
| 3581 | |
Tejun Heo | 9b64697 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 3582 | void cifs_oplock_break(struct work_struct *work) |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 3583 | { |
| 3584 | struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo, |
| 3585 | oplock_break); |
Jeff Layton | a5e18bc | 2010-10-11 15:07:18 -0400 | [diff] [blame] | 3586 | struct inode *inode = cfile->dentry->d_inode; |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 3587 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
Pavel Shilovsky | 95a3f2f | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 3588 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Jeff Layton | eb4b756 | 2010-10-22 14:52:29 -0400 | [diff] [blame] | 3589 | int rc = 0; |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 3590 | |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 3591 | if (!cinode->clientCanCacheAll && cinode->clientCanCacheRead && |
| 3592 | cifs_has_mand_locks(cinode)) { |
| 3593 | cFYI(1, "Reset oplock to None for inode=%p due to mand locks", |
| 3594 | inode); |
| 3595 | cinode->clientCanCacheRead = false; |
| 3596 | } |
| 3597 | |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 3598 | if (inode && S_ISREG(inode->i_mode)) { |
Steve French | d54ff73 | 2010-04-27 04:38:15 +0000 | [diff] [blame] | 3599 | if (cinode->clientCanCacheRead) |
Al Viro | 8737c93 | 2009-12-24 06:47:55 -0500 | [diff] [blame] | 3600 | break_lease(inode, O_RDONLY); |
Steve French | d54ff73 | 2010-04-27 04:38:15 +0000 | [diff] [blame] | 3601 | else |
Al Viro | 8737c93 | 2009-12-24 06:47:55 -0500 | [diff] [blame] | 3602 | break_lease(inode, O_WRONLY); |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 3603 | rc = filemap_fdatawrite(inode->i_mapping); |
| 3604 | if (cinode->clientCanCacheRead == 0) { |
Jeff Layton | eb4b756 | 2010-10-22 14:52:29 -0400 | [diff] [blame] | 3605 | rc = filemap_fdatawait(inode->i_mapping); |
| 3606 | mapping_set_error(inode->i_mapping, rc); |
Pavel Shilovsky | 03eca70 | 2012-12-06 21:24:33 +0400 | [diff] [blame] | 3607 | cifs_invalidate_mapping(inode); |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 3608 | } |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 3609 | cFYI(1, "Oplock flush inode %p rc %d", inode, rc); |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 3610 | } |
| 3611 | |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 3612 | rc = cifs_push_locks(cfile); |
| 3613 | if (rc) |
| 3614 | cERROR(1, "Push locks rc = %d", rc); |
| 3615 | |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 3616 | /* |
| 3617 | * releasing stale oplock after recent reconnect of smb session using |
| 3618 | * a now incorrect file handle is not a data integrity issue but do |
| 3619 | * not bother sending an oplock release if session to server still is |
| 3620 | * disconnected since oplock already released by the server |
| 3621 | */ |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 3622 | if (!cfile->oplock_break_cancelled) { |
Pavel Shilovsky | 95a3f2f | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 3623 | rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid, |
| 3624 | cinode); |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 3625 | cFYI(1, "Oplock release rc = %d", rc); |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 3626 | } |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 3627 | } |
| 3628 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 3629 | const struct address_space_operations cifs_addr_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | .readpage = cifs_readpage, |
| 3631 | .readpages = cifs_readpages, |
| 3632 | .writepage = cifs_writepage, |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 3633 | .writepages = cifs_writepages, |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 3634 | .write_begin = cifs_write_begin, |
| 3635 | .write_end = cifs_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3636 | .set_page_dirty = __set_page_dirty_nobuffers, |
Suresh Jayaraman | 85f2d6b | 2010-07-05 18:13:00 +0530 | [diff] [blame] | 3637 | .releasepage = cifs_release_page, |
| 3638 | .invalidatepage = cifs_invalidate_page, |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 3639 | .launder_page = cifs_launder_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | }; |
Dave Kleikamp | 273d81d | 2006-06-01 19:41:23 +0000 | [diff] [blame] | 3641 | |
| 3642 | /* |
| 3643 | * cifs_readpages requires the server to support a buffer large enough to |
| 3644 | * contain the header plus one complete page of data. Otherwise, we need |
| 3645 | * to leave cifs_readpages out of the address space operations. |
| 3646 | */ |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 3647 | const struct address_space_operations cifs_addr_ops_smallbuf = { |
Dave Kleikamp | 273d81d | 2006-06-01 19:41:23 +0000 | [diff] [blame] | 3648 | .readpage = cifs_readpage, |
| 3649 | .writepage = cifs_writepage, |
| 3650 | .writepages = cifs_writepages, |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 3651 | .write_begin = cifs_write_begin, |
| 3652 | .write_end = cifs_write_end, |
Dave Kleikamp | 273d81d | 2006-06-01 19:41:23 +0000 | [diff] [blame] | 3653 | .set_page_dirty = __set_page_dirty_nobuffers, |
Suresh Jayaraman | 85f2d6b | 2010-07-05 18:13:00 +0530 | [diff] [blame] | 3654 | .releasepage = cifs_release_page, |
| 3655 | .invalidatepage = cifs_invalidate_page, |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 3656 | .launder_page = cifs_launder_page, |
Dave Kleikamp | 273d81d | 2006-06-01 19:41:23 +0000 | [diff] [blame] | 3657 | }; |