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 |
| 5 | * |
| 6 | * Copyright (C) International Business Machines Corp., 2002,2003 |
| 7 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Lesser General Public License as published |
| 11 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 17 | * the GNU Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public License |
| 20 | * along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | #include <linux/fs.h> |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 24 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/stat.h> |
| 26 | #include <linux/fcntl.h> |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 27 | #include <linux/mpage.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/pagemap.h> |
| 29 | #include <linux/pagevec.h> |
| 30 | #include <linux/smp_lock.h> |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 31 | #include <linux/writeback.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/div64.h> |
| 33 | #include "cifsfs.h" |
| 34 | #include "cifspdu.h" |
| 35 | #include "cifsglob.h" |
| 36 | #include "cifsproto.h" |
| 37 | #include "cifs_unicode.h" |
| 38 | #include "cifs_debug.h" |
| 39 | #include "cifs_fs_sb.h" |
| 40 | |
| 41 | static inline struct cifsFileInfo *cifs_init_private( |
| 42 | struct cifsFileInfo *private_data, struct inode *inode, |
| 43 | struct file *file, __u16 netfid) |
| 44 | { |
| 45 | memset(private_data, 0, sizeof(struct cifsFileInfo)); |
| 46 | private_data->netfid = netfid; |
| 47 | private_data->pid = current->tgid; |
| 48 | init_MUTEX(&private_data->fh_sem); |
| 49 | private_data->pfile = file; /* needed for writepage */ |
| 50 | private_data->pInode = inode; |
| 51 | private_data->invalidHandle = FALSE; |
| 52 | private_data->closePend = FALSE; |
| 53 | |
| 54 | return private_data; |
| 55 | } |
| 56 | |
| 57 | static inline int cifs_convert_flags(unsigned int flags) |
| 58 | { |
| 59 | if ((flags & O_ACCMODE) == O_RDONLY) |
| 60 | return GENERIC_READ; |
| 61 | else if ((flags & O_ACCMODE) == O_WRONLY) |
| 62 | return GENERIC_WRITE; |
| 63 | else if ((flags & O_ACCMODE) == O_RDWR) { |
| 64 | /* GENERIC_ALL is too much permission to request |
| 65 | can cause unnecessary access denied on create */ |
| 66 | /* return GENERIC_ALL; */ |
| 67 | return (GENERIC_READ | GENERIC_WRITE); |
| 68 | } |
| 69 | |
| 70 | return 0x20197; |
| 71 | } |
| 72 | |
| 73 | static inline int cifs_get_disposition(unsigned int flags) |
| 74 | { |
| 75 | if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) |
| 76 | return FILE_CREATE; |
| 77 | else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) |
| 78 | return FILE_OVERWRITE_IF; |
| 79 | else if ((flags & O_CREAT) == O_CREAT) |
| 80 | return FILE_OPEN_IF; |
| 81 | else |
| 82 | return FILE_OPEN; |
| 83 | } |
| 84 | |
| 85 | /* all arguments to this function must be checked for validity in caller */ |
| 86 | static inline int cifs_open_inode_helper(struct inode *inode, struct file *file, |
| 87 | struct cifsInodeInfo *pCifsInode, struct cifsFileInfo *pCifsFile, |
| 88 | struct cifsTconInfo *pTcon, int *oplock, FILE_ALL_INFO *buf, |
| 89 | char *full_path, int xid) |
| 90 | { |
| 91 | struct timespec temp; |
| 92 | int rc; |
| 93 | |
| 94 | /* want handles we can use to read with first |
| 95 | in the list so we do not have to walk the |
| 96 | list to search for one in prepare_write */ |
| 97 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) { |
| 98 | list_add_tail(&pCifsFile->flist, |
| 99 | &pCifsInode->openFileList); |
| 100 | } else { |
| 101 | list_add(&pCifsFile->flist, |
| 102 | &pCifsInode->openFileList); |
| 103 | } |
| 104 | write_unlock(&GlobalSMBSeslock); |
| 105 | write_unlock(&file->f_owner.lock); |
| 106 | if (pCifsInode->clientCanCacheRead) { |
| 107 | /* we have the inode open somewhere else |
| 108 | no need to discard cache data */ |
| 109 | goto client_can_cache; |
| 110 | } |
| 111 | |
| 112 | /* BB need same check in cifs_create too? */ |
| 113 | /* if not oplocked, invalidate inode pages if mtime or file |
| 114 | size changed */ |
| 115 | temp = cifs_NTtimeToUnix(le64_to_cpu(buf->LastWriteTime)); |
| 116 | if (timespec_equal(&file->f_dentry->d_inode->i_mtime, &temp) && |
| 117 | (file->f_dentry->d_inode->i_size == |
| 118 | (loff_t)le64_to_cpu(buf->EndOfFile))) { |
| 119 | cFYI(1, ("inode unchanged on server")); |
| 120 | } else { |
| 121 | if (file->f_dentry->d_inode->i_mapping) { |
| 122 | /* BB no need to lock inode until after invalidate |
| 123 | since namei code should already have it locked? */ |
| 124 | filemap_fdatawrite(file->f_dentry->d_inode->i_mapping); |
| 125 | filemap_fdatawait(file->f_dentry->d_inode->i_mapping); |
| 126 | } |
| 127 | cFYI(1, ("invalidating remote inode since open detected it " |
| 128 | "changed")); |
| 129 | invalidate_remote_inode(file->f_dentry->d_inode); |
| 130 | } |
| 131 | |
| 132 | client_can_cache: |
| 133 | if (pTcon->ses->capabilities & CAP_UNIX) |
| 134 | rc = cifs_get_inode_info_unix(&file->f_dentry->d_inode, |
| 135 | full_path, inode->i_sb, xid); |
| 136 | else |
| 137 | rc = cifs_get_inode_info(&file->f_dentry->d_inode, |
| 138 | full_path, buf, inode->i_sb, xid); |
| 139 | |
| 140 | if ((*oplock & 0xF) == OPLOCK_EXCLUSIVE) { |
| 141 | pCifsInode->clientCanCacheAll = TRUE; |
| 142 | pCifsInode->clientCanCacheRead = TRUE; |
| 143 | cFYI(1, ("Exclusive Oplock granted on inode %p", |
| 144 | file->f_dentry->d_inode)); |
| 145 | } else if ((*oplock & 0xF) == OPLOCK_READ) |
| 146 | pCifsInode->clientCanCacheRead = TRUE; |
| 147 | |
| 148 | return rc; |
| 149 | } |
| 150 | |
| 151 | int cifs_open(struct inode *inode, struct file *file) |
| 152 | { |
| 153 | int rc = -EACCES; |
| 154 | int xid, oplock; |
| 155 | struct cifs_sb_info *cifs_sb; |
| 156 | struct cifsTconInfo *pTcon; |
| 157 | struct cifsFileInfo *pCifsFile; |
| 158 | struct cifsInodeInfo *pCifsInode; |
| 159 | struct list_head *tmp; |
| 160 | char *full_path = NULL; |
| 161 | int desiredAccess; |
| 162 | int disposition; |
| 163 | __u16 netfid; |
| 164 | FILE_ALL_INFO *buf = NULL; |
| 165 | |
| 166 | xid = GetXid(); |
| 167 | |
| 168 | cifs_sb = CIFS_SB(inode->i_sb); |
| 169 | pTcon = cifs_sb->tcon; |
| 170 | |
| 171 | if (file->f_flags & O_CREAT) { |
| 172 | /* search inode for this file and fill in file->private_data */ |
| 173 | pCifsInode = CIFS_I(file->f_dentry->d_inode); |
| 174 | read_lock(&GlobalSMBSeslock); |
| 175 | list_for_each(tmp, &pCifsInode->openFileList) { |
| 176 | pCifsFile = list_entry(tmp, struct cifsFileInfo, |
| 177 | flist); |
| 178 | if ((pCifsFile->pfile == NULL) && |
| 179 | (pCifsFile->pid == current->tgid)) { |
| 180 | /* mode set in cifs_create */ |
| 181 | |
| 182 | /* needed for writepage */ |
| 183 | pCifsFile->pfile = file; |
| 184 | |
| 185 | file->private_data = pCifsFile; |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | read_unlock(&GlobalSMBSeslock); |
| 190 | if (file->private_data != NULL) { |
| 191 | rc = 0; |
| 192 | FreeXid(xid); |
| 193 | return rc; |
| 194 | } else { |
| 195 | if (file->f_flags & O_EXCL) |
| 196 | cERROR(1, ("could not find file instance for " |
| 197 | "new file %p ", file)); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | down(&inode->i_sb->s_vfs_rename_sem); |
Steve French | 7f57356 | 2005-08-30 11:32:14 -0700 | [diff] [blame] | 202 | full_path = build_path_from_dentry(file->f_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | up(&inode->i_sb->s_vfs_rename_sem); |
| 204 | if (full_path == NULL) { |
| 205 | FreeXid(xid); |
| 206 | return -ENOMEM; |
| 207 | } |
| 208 | |
| 209 | cFYI(1, (" inode = 0x%p file flags are 0x%x for %s", |
| 210 | inode, file->f_flags, full_path)); |
| 211 | desiredAccess = cifs_convert_flags(file->f_flags); |
| 212 | |
| 213 | /********************************************************************* |
| 214 | * open flag mapping table: |
| 215 | * |
| 216 | * POSIX Flag CIFS Disposition |
| 217 | * ---------- ---------------- |
| 218 | * O_CREAT FILE_OPEN_IF |
| 219 | * O_CREAT | O_EXCL FILE_CREATE |
| 220 | * O_CREAT | O_TRUNC FILE_OVERWRITE_IF |
| 221 | * O_TRUNC FILE_OVERWRITE |
| 222 | * none of the above FILE_OPEN |
| 223 | * |
| 224 | * Note that there is not a direct match between disposition |
| 225 | * FILE_SUPERSEDE (ie create whether or not file exists although |
| 226 | * O_CREAT | O_TRUNC is similar but truncates the existing |
| 227 | * file rather than creating a new file as FILE_SUPERSEDE does |
| 228 | * (which uses the attributes / metadata passed in on open call) |
| 229 | *? |
| 230 | *? O_SYNC is a reasonable match to CIFS writethrough flag |
| 231 | *? and the read write flags match reasonably. O_LARGEFILE |
| 232 | *? is irrelevant because largefile support is always used |
| 233 | *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY, |
| 234 | * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation |
| 235 | *********************************************************************/ |
| 236 | |
| 237 | disposition = cifs_get_disposition(file->f_flags); |
| 238 | |
| 239 | if (oplockEnabled) |
| 240 | oplock = REQ_OPLOCK; |
| 241 | else |
| 242 | oplock = FALSE; |
| 243 | |
| 244 | /* BB pass O_SYNC flag through on file attributes .. BB */ |
| 245 | |
| 246 | /* Also refresh inode by passing in file_info buf returned by SMBOpen |
| 247 | and calling get_inode_info with returned buf (at least helps |
| 248 | non-Unix server case) */ |
| 249 | |
| 250 | /* BB we can not do this if this is the second open of a file |
| 251 | and the first handle has writebehind data, we might be |
| 252 | able to simply do a filemap_fdatawrite/filemap_fdatawait first */ |
| 253 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
| 254 | if (!buf) { |
| 255 | rc = -ENOMEM; |
| 256 | goto out; |
| 257 | } |
| 258 | rc = CIFSSMBOpen(xid, pTcon, full_path, disposition, desiredAccess, |
| 259 | CREATE_NOT_DIR, &netfid, &oplock, buf, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 260 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags |
| 261 | & CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 262 | if (rc == -EIO) { |
| 263 | /* Old server, try legacy style OpenX */ |
| 264 | rc = SMBLegacyOpen(xid, pTcon, full_path, disposition, |
| 265 | desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf, |
| 266 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags |
| 267 | & CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | if (rc) { |
| 270 | cFYI(1, ("cifs_open returned 0x%x ", rc)); |
| 271 | goto out; |
| 272 | } |
| 273 | file->private_data = |
| 274 | kmalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); |
| 275 | if (file->private_data == NULL) { |
| 276 | rc = -ENOMEM; |
| 277 | goto out; |
| 278 | } |
| 279 | pCifsFile = cifs_init_private(file->private_data, inode, file, netfid); |
| 280 | write_lock(&file->f_owner.lock); |
| 281 | write_lock(&GlobalSMBSeslock); |
| 282 | list_add(&pCifsFile->tlist, &pTcon->openFileList); |
| 283 | |
| 284 | pCifsInode = CIFS_I(file->f_dentry->d_inode); |
| 285 | if (pCifsInode) { |
| 286 | rc = cifs_open_inode_helper(inode, file, pCifsInode, |
| 287 | pCifsFile, pTcon, |
| 288 | &oplock, buf, full_path, xid); |
| 289 | } else { |
| 290 | write_unlock(&GlobalSMBSeslock); |
| 291 | write_unlock(&file->f_owner.lock); |
| 292 | } |
| 293 | |
| 294 | if (oplock & CIFS_CREATE_ACTION) { |
| 295 | /* time to set mode which we can not set earlier due to |
| 296 | problems creating new read-only files */ |
| 297 | if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) { |
| 298 | CIFSSMBUnixSetPerms(xid, pTcon, full_path, |
| 299 | inode->i_mode, |
| 300 | (__u64)-1, (__u64)-1, 0 /* dev */, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 301 | cifs_sb->local_nls, |
| 302 | cifs_sb->mnt_cifs_flags & |
| 303 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } else { |
| 305 | /* BB implement via Windows security descriptors eg |
| 306 | CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, |
| 307 | -1, -1, local_nls); |
| 308 | in the meantime could set r/o dos attribute when |
| 309 | perms are eg: mode & 0222 == 0 */ |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | out: |
| 314 | kfree(buf); |
| 315 | kfree(full_path); |
| 316 | FreeXid(xid); |
| 317 | return rc; |
| 318 | } |
| 319 | |
| 320 | /* Try to reaquire byte range locks that were released when session */ |
| 321 | /* to server was lost */ |
| 322 | static int cifs_relock_file(struct cifsFileInfo *cifsFile) |
| 323 | { |
| 324 | int rc = 0; |
| 325 | |
| 326 | /* BB list all locks open on this file and relock */ |
| 327 | |
| 328 | return rc; |
| 329 | } |
| 330 | |
| 331 | static int cifs_reopen_file(struct inode *inode, struct file *file, |
| 332 | int can_flush) |
| 333 | { |
| 334 | int rc = -EACCES; |
| 335 | int xid, oplock; |
| 336 | struct cifs_sb_info *cifs_sb; |
| 337 | struct cifsTconInfo *pTcon; |
| 338 | struct cifsFileInfo *pCifsFile; |
| 339 | struct cifsInodeInfo *pCifsInode; |
| 340 | char *full_path = NULL; |
| 341 | int desiredAccess; |
| 342 | int disposition = FILE_OPEN; |
| 343 | __u16 netfid; |
| 344 | |
| 345 | if (inode == NULL) |
| 346 | return -EBADF; |
| 347 | if (file->private_data) { |
| 348 | pCifsFile = (struct cifsFileInfo *)file->private_data; |
| 349 | } else |
| 350 | return -EBADF; |
| 351 | |
| 352 | xid = GetXid(); |
| 353 | down(&pCifsFile->fh_sem); |
| 354 | if (pCifsFile->invalidHandle == FALSE) { |
| 355 | up(&pCifsFile->fh_sem); |
| 356 | FreeXid(xid); |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | if (file->f_dentry == NULL) { |
| 361 | up(&pCifsFile->fh_sem); |
| 362 | cFYI(1, ("failed file reopen, no valid name if dentry freed")); |
| 363 | FreeXid(xid); |
| 364 | return -EBADF; |
| 365 | } |
| 366 | cifs_sb = CIFS_SB(inode->i_sb); |
| 367 | pTcon = cifs_sb->tcon; |
| 368 | /* can not grab rename sem here because various ops, including |
| 369 | those that already have the rename sem can end up causing writepage |
| 370 | to get called and if the server was down that means we end up here, |
| 371 | and we can never tell if the caller already has the rename_sem */ |
Steve French | 7f57356 | 2005-08-30 11:32:14 -0700 | [diff] [blame] | 372 | full_path = build_path_from_dentry(file->f_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | if (full_path == NULL) { |
| 374 | up(&pCifsFile->fh_sem); |
| 375 | FreeXid(xid); |
| 376 | return -ENOMEM; |
| 377 | } |
| 378 | |
| 379 | cFYI(1, (" inode = 0x%p file flags are 0x%x for %s", |
| 380 | inode, file->f_flags,full_path)); |
| 381 | desiredAccess = cifs_convert_flags(file->f_flags); |
| 382 | |
| 383 | if (oplockEnabled) |
| 384 | oplock = REQ_OPLOCK; |
| 385 | else |
| 386 | oplock = FALSE; |
| 387 | |
| 388 | /* Can not refresh inode by passing in file_info buf to be returned |
| 389 | by SMBOpen and then calling get_inode_info with returned buf |
| 390 | since file might have write behind data that needs to be flushed |
| 391 | and server version of file size can be stale. If we knew for sure |
| 392 | that inode was not dirty locally we could do this */ |
| 393 | |
| 394 | /* buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
| 395 | if (buf == 0) { |
| 396 | up(&pCifsFile->fh_sem); |
| 397 | kfree(full_path); |
| 398 | FreeXid(xid); |
| 399 | return -ENOMEM; |
| 400 | } */ |
| 401 | rc = CIFSSMBOpen(xid, pTcon, full_path, disposition, desiredAccess, |
| 402 | CREATE_NOT_DIR, &netfid, &oplock, NULL, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 403 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
| 404 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | if (rc) { |
| 406 | up(&pCifsFile->fh_sem); |
| 407 | cFYI(1, ("cifs_open returned 0x%x ", rc)); |
| 408 | cFYI(1, ("oplock: %d ", oplock)); |
| 409 | } else { |
| 410 | pCifsFile->netfid = netfid; |
| 411 | pCifsFile->invalidHandle = FALSE; |
| 412 | up(&pCifsFile->fh_sem); |
| 413 | pCifsInode = CIFS_I(inode); |
| 414 | if (pCifsInode) { |
| 415 | if (can_flush) { |
| 416 | filemap_fdatawrite(inode->i_mapping); |
| 417 | filemap_fdatawait(inode->i_mapping); |
| 418 | /* temporarily disable caching while we |
| 419 | go to server to get inode info */ |
| 420 | pCifsInode->clientCanCacheAll = FALSE; |
| 421 | pCifsInode->clientCanCacheRead = FALSE; |
| 422 | if (pTcon->ses->capabilities & CAP_UNIX) |
| 423 | rc = cifs_get_inode_info_unix(&inode, |
| 424 | full_path, inode->i_sb, xid); |
| 425 | else |
| 426 | rc = cifs_get_inode_info(&inode, |
| 427 | full_path, NULL, inode->i_sb, |
| 428 | xid); |
| 429 | } /* else we are writing out data to server already |
| 430 | and could deadlock if we tried to flush data, and |
| 431 | since we do not know if we have data that would |
| 432 | invalidate the current end of file on the server |
| 433 | we can not go to the server to get the new inod |
| 434 | info */ |
| 435 | if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) { |
| 436 | pCifsInode->clientCanCacheAll = TRUE; |
| 437 | pCifsInode->clientCanCacheRead = TRUE; |
| 438 | cFYI(1, ("Exclusive Oplock granted on inode %p", |
| 439 | file->f_dentry->d_inode)); |
| 440 | } else if ((oplock & 0xF) == OPLOCK_READ) { |
| 441 | pCifsInode->clientCanCacheRead = TRUE; |
| 442 | pCifsInode->clientCanCacheAll = FALSE; |
| 443 | } else { |
| 444 | pCifsInode->clientCanCacheRead = FALSE; |
| 445 | pCifsInode->clientCanCacheAll = FALSE; |
| 446 | } |
| 447 | cifs_relock_file(pCifsFile); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | kfree(full_path); |
| 452 | FreeXid(xid); |
| 453 | return rc; |
| 454 | } |
| 455 | |
| 456 | int cifs_close(struct inode *inode, struct file *file) |
| 457 | { |
| 458 | int rc = 0; |
| 459 | int xid; |
| 460 | struct cifs_sb_info *cifs_sb; |
| 461 | struct cifsTconInfo *pTcon; |
| 462 | struct cifsFileInfo *pSMBFile = |
| 463 | (struct cifsFileInfo *)file->private_data; |
| 464 | |
| 465 | xid = GetXid(); |
| 466 | |
| 467 | cifs_sb = CIFS_SB(inode->i_sb); |
| 468 | pTcon = cifs_sb->tcon; |
| 469 | if (pSMBFile) { |
| 470 | pSMBFile->closePend = TRUE; |
| 471 | write_lock(&file->f_owner.lock); |
| 472 | if (pTcon) { |
| 473 | /* no sense reconnecting to close a file that is |
| 474 | already closed */ |
| 475 | if (pTcon->tidStatus != CifsNeedReconnect) { |
| 476 | write_unlock(&file->f_owner.lock); |
| 477 | rc = CIFSSMBClose(xid, pTcon, |
| 478 | pSMBFile->netfid); |
| 479 | write_lock(&file->f_owner.lock); |
| 480 | } |
| 481 | } |
Steve French | cbe0476 | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 482 | write_lock(&GlobalSMBSeslock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | list_del(&pSMBFile->flist); |
| 484 | list_del(&pSMBFile->tlist); |
Steve French | cbe0476 | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 485 | write_unlock(&GlobalSMBSeslock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | write_unlock(&file->f_owner.lock); |
| 487 | kfree(pSMBFile->search_resume_name); |
| 488 | kfree(file->private_data); |
| 489 | file->private_data = NULL; |
| 490 | } else |
| 491 | rc = -EBADF; |
| 492 | |
| 493 | if (list_empty(&(CIFS_I(inode)->openFileList))) { |
| 494 | cFYI(1, ("closing last open instance for inode %p", inode)); |
| 495 | /* if the file is not open we do not know if we can cache info |
| 496 | on this inode, much less write behind and read ahead */ |
| 497 | CIFS_I(inode)->clientCanCacheRead = FALSE; |
| 498 | CIFS_I(inode)->clientCanCacheAll = FALSE; |
| 499 | } |
| 500 | if ((rc ==0) && CIFS_I(inode)->write_behind_rc) |
| 501 | rc = CIFS_I(inode)->write_behind_rc; |
| 502 | FreeXid(xid); |
| 503 | return rc; |
| 504 | } |
| 505 | |
| 506 | int cifs_closedir(struct inode *inode, struct file *file) |
| 507 | { |
| 508 | int rc = 0; |
| 509 | int xid; |
| 510 | struct cifsFileInfo *pCFileStruct = |
| 511 | (struct cifsFileInfo *)file->private_data; |
| 512 | char *ptmp; |
| 513 | |
| 514 | cFYI(1, ("Closedir inode = 0x%p with ", inode)); |
| 515 | |
| 516 | xid = GetXid(); |
| 517 | |
| 518 | if (pCFileStruct) { |
| 519 | struct cifsTconInfo *pTcon; |
| 520 | struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_dentry->d_sb); |
| 521 | |
| 522 | pTcon = cifs_sb->tcon; |
| 523 | |
| 524 | cFYI(1, ("Freeing private data in close dir")); |
Steve French | 31ca3bc | 2005-04-28 22:41:11 -0700 | [diff] [blame] | 525 | if ((pCFileStruct->srch_inf.endOfSearch == FALSE) && |
| 526 | (pCFileStruct->invalidHandle == FALSE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | pCFileStruct->invalidHandle = TRUE; |
| 528 | rc = CIFSFindClose(xid, pTcon, pCFileStruct->netfid); |
| 529 | cFYI(1, ("Closing uncompleted readdir with rc %d", |
| 530 | rc)); |
| 531 | /* not much we can do if it fails anyway, ignore rc */ |
| 532 | rc = 0; |
| 533 | } |
| 534 | ptmp = pCFileStruct->srch_inf.ntwrk_buf_start; |
| 535 | if (ptmp) { |
| 536 | /* BB removeme BB */ cFYI(1, ("freeing smb buf in srch struct in closedir")); |
| 537 | pCFileStruct->srch_inf.ntwrk_buf_start = NULL; |
| 538 | cifs_buf_release(ptmp); |
| 539 | } |
| 540 | ptmp = pCFileStruct->search_resume_name; |
| 541 | if (ptmp) { |
| 542 | /* BB removeme BB */ cFYI(1, ("freeing resume name in closedir")); |
| 543 | pCFileStruct->search_resume_name = NULL; |
| 544 | kfree(ptmp); |
| 545 | } |
| 546 | kfree(file->private_data); |
| 547 | file->private_data = NULL; |
| 548 | } |
| 549 | /* BB can we lock the filestruct while this is going on? */ |
| 550 | FreeXid(xid); |
| 551 | return rc; |
| 552 | } |
| 553 | |
| 554 | int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock) |
| 555 | { |
| 556 | int rc, xid; |
| 557 | __u32 lockType = LOCKING_ANDX_LARGE_FILES; |
| 558 | __u32 numLock = 0; |
| 559 | __u32 numUnlock = 0; |
| 560 | __u64 length; |
| 561 | int wait_flag = FALSE; |
| 562 | struct cifs_sb_info *cifs_sb; |
| 563 | struct cifsTconInfo *pTcon; |
| 564 | |
| 565 | length = 1 + pfLock->fl_end - pfLock->fl_start; |
| 566 | rc = -EACCES; |
| 567 | xid = GetXid(); |
| 568 | |
| 569 | cFYI(1, ("Lock parm: 0x%x flockflags: " |
| 570 | "0x%x flocktype: 0x%x start: %lld end: %lld", |
| 571 | cmd, pfLock->fl_flags, pfLock->fl_type, pfLock->fl_start, |
| 572 | pfLock->fl_end)); |
| 573 | |
| 574 | if (pfLock->fl_flags & FL_POSIX) |
| 575 | cFYI(1, ("Posix ")); |
| 576 | if (pfLock->fl_flags & FL_FLOCK) |
| 577 | cFYI(1, ("Flock ")); |
| 578 | if (pfLock->fl_flags & FL_SLEEP) { |
| 579 | cFYI(1, ("Blocking lock ")); |
| 580 | wait_flag = TRUE; |
| 581 | } |
| 582 | if (pfLock->fl_flags & FL_ACCESS) |
| 583 | cFYI(1, ("Process suspended by mandatory locking - " |
| 584 | "not implemented yet ")); |
| 585 | if (pfLock->fl_flags & FL_LEASE) |
| 586 | cFYI(1, ("Lease on file - not implemented yet")); |
| 587 | if (pfLock->fl_flags & |
| 588 | (~(FL_POSIX | FL_FLOCK | FL_SLEEP | FL_ACCESS | FL_LEASE))) |
| 589 | cFYI(1, ("Unknown lock flags 0x%x", pfLock->fl_flags)); |
| 590 | |
| 591 | if (pfLock->fl_type == F_WRLCK) { |
| 592 | cFYI(1, ("F_WRLCK ")); |
| 593 | numLock = 1; |
| 594 | } else if (pfLock->fl_type == F_UNLCK) { |
| 595 | cFYI(1, ("F_UNLCK ")); |
| 596 | numUnlock = 1; |
| 597 | } else if (pfLock->fl_type == F_RDLCK) { |
| 598 | cFYI(1, ("F_RDLCK ")); |
| 599 | lockType |= LOCKING_ANDX_SHARED_LOCK; |
| 600 | numLock = 1; |
| 601 | } else if (pfLock->fl_type == F_EXLCK) { |
| 602 | cFYI(1, ("F_EXLCK ")); |
| 603 | numLock = 1; |
| 604 | } else if (pfLock->fl_type == F_SHLCK) { |
| 605 | cFYI(1, ("F_SHLCK ")); |
| 606 | lockType |= LOCKING_ANDX_SHARED_LOCK; |
| 607 | numLock = 1; |
| 608 | } else |
| 609 | cFYI(1, ("Unknown type of lock ")); |
| 610 | |
| 611 | cifs_sb = CIFS_SB(file->f_dentry->d_sb); |
| 612 | pTcon = cifs_sb->tcon; |
| 613 | |
| 614 | if (file->private_data == NULL) { |
| 615 | FreeXid(xid); |
| 616 | return -EBADF; |
| 617 | } |
| 618 | |
| 619 | if (IS_GETLK(cmd)) { |
| 620 | rc = CIFSSMBLock(xid, pTcon, |
| 621 | ((struct cifsFileInfo *)file-> |
| 622 | private_data)->netfid, |
| 623 | length, |
| 624 | pfLock->fl_start, 0, 1, lockType, |
| 625 | 0 /* wait flag */ ); |
| 626 | if (rc == 0) { |
| 627 | rc = CIFSSMBLock(xid, pTcon, |
| 628 | ((struct cifsFileInfo *) file-> |
| 629 | private_data)->netfid, |
| 630 | length, |
| 631 | pfLock->fl_start, 1 /* numUnlock */ , |
| 632 | 0 /* numLock */ , lockType, |
| 633 | 0 /* wait flag */ ); |
| 634 | pfLock->fl_type = F_UNLCK; |
| 635 | if (rc != 0) |
| 636 | cERROR(1, ("Error unlocking previously locked " |
| 637 | "range %d during test of lock ", |
| 638 | rc)); |
| 639 | rc = 0; |
| 640 | |
| 641 | } else { |
| 642 | /* if rc == ERR_SHARING_VIOLATION ? */ |
| 643 | rc = 0; /* do not change lock type to unlock |
| 644 | since range in use */ |
| 645 | } |
| 646 | |
| 647 | FreeXid(xid); |
| 648 | return rc; |
| 649 | } |
| 650 | |
| 651 | rc = CIFSSMBLock(xid, pTcon, |
| 652 | ((struct cifsFileInfo *) file->private_data)-> |
| 653 | netfid, length, |
| 654 | pfLock->fl_start, numUnlock, numLock, lockType, |
| 655 | wait_flag); |
Steve French | d634cc1 | 2005-08-26 14:42:59 -0500 | [diff] [blame] | 656 | if (pfLock->fl_flags & FL_POSIX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | posix_lock_file_wait(file, pfLock); |
| 658 | FreeXid(xid); |
| 659 | return rc; |
| 660 | } |
| 661 | |
| 662 | ssize_t cifs_user_write(struct file *file, const char __user *write_data, |
| 663 | size_t write_size, loff_t *poffset) |
| 664 | { |
| 665 | int rc = 0; |
| 666 | unsigned int bytes_written = 0; |
| 667 | unsigned int total_written; |
| 668 | struct cifs_sb_info *cifs_sb; |
| 669 | struct cifsTconInfo *pTcon; |
| 670 | int xid, long_op; |
| 671 | struct cifsFileInfo *open_file; |
| 672 | |
| 673 | if (file->f_dentry == NULL) |
| 674 | return -EBADF; |
| 675 | |
| 676 | cifs_sb = CIFS_SB(file->f_dentry->d_sb); |
| 677 | if (cifs_sb == NULL) |
| 678 | return -EBADF; |
| 679 | |
| 680 | pTcon = cifs_sb->tcon; |
| 681 | |
| 682 | /* cFYI(1, |
| 683 | (" write %d bytes to offset %lld of %s", write_size, |
| 684 | *poffset, file->f_dentry->d_name.name)); */ |
| 685 | |
| 686 | if (file->private_data == NULL) |
| 687 | return -EBADF; |
| 688 | else |
| 689 | open_file = (struct cifsFileInfo *) file->private_data; |
| 690 | |
| 691 | xid = GetXid(); |
| 692 | if (file->f_dentry->d_inode == NULL) { |
| 693 | FreeXid(xid); |
| 694 | return -EBADF; |
| 695 | } |
| 696 | |
| 697 | if (*poffset > file->f_dentry->d_inode->i_size) |
| 698 | long_op = 2; /* writes past end of file can take a long time */ |
| 699 | else |
| 700 | long_op = 1; |
| 701 | |
| 702 | for (total_written = 0; write_size > total_written; |
| 703 | total_written += bytes_written) { |
| 704 | rc = -EAGAIN; |
| 705 | while (rc == -EAGAIN) { |
| 706 | if (file->private_data == NULL) { |
| 707 | /* file has been closed on us */ |
| 708 | FreeXid(xid); |
| 709 | /* if we have gotten here we have written some data |
| 710 | and blocked, and the file has been freed on us while |
| 711 | we blocked so return what we managed to write */ |
| 712 | return total_written; |
| 713 | } |
| 714 | if (open_file->closePend) { |
| 715 | FreeXid(xid); |
| 716 | if (total_written) |
| 717 | return total_written; |
| 718 | else |
| 719 | return -EBADF; |
| 720 | } |
| 721 | if (open_file->invalidHandle) { |
| 722 | if ((file->f_dentry == NULL) || |
| 723 | (file->f_dentry->d_inode == NULL)) { |
| 724 | FreeXid(xid); |
| 725 | return total_written; |
| 726 | } |
| 727 | /* we could deadlock if we called |
| 728 | filemap_fdatawait from here so tell |
| 729 | reopen_file not to flush data to server |
| 730 | now */ |
| 731 | rc = cifs_reopen_file(file->f_dentry->d_inode, |
| 732 | file, FALSE); |
| 733 | if (rc != 0) |
| 734 | break; |
| 735 | } |
| 736 | |
| 737 | rc = CIFSSMBWrite(xid, pTcon, |
| 738 | open_file->netfid, |
| 739 | min_t(const int, cifs_sb->wsize, |
| 740 | write_size - total_written), |
| 741 | *poffset, &bytes_written, |
| 742 | NULL, write_data + total_written, long_op); |
| 743 | } |
| 744 | if (rc || (bytes_written == 0)) { |
| 745 | if (total_written) |
| 746 | break; |
| 747 | else { |
| 748 | FreeXid(xid); |
| 749 | return rc; |
| 750 | } |
| 751 | } else |
| 752 | *poffset += bytes_written; |
| 753 | long_op = FALSE; /* subsequent writes fast - |
| 754 | 15 seconds is plenty */ |
| 755 | } |
| 756 | |
Steve French | a454434 | 2005-08-24 13:59:35 -0700 | [diff] [blame] | 757 | cifs_stats_bytes_written(pTcon, total_written); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
| 759 | /* since the write may have blocked check these pointers again */ |
| 760 | if (file->f_dentry) { |
| 761 | if (file->f_dentry->d_inode) { |
| 762 | struct inode *inode = file->f_dentry->d_inode; |
| 763 | inode->i_ctime = inode->i_mtime = |
| 764 | current_fs_time(inode->i_sb); |
| 765 | if (total_written > 0) { |
| 766 | if (*poffset > file->f_dentry->d_inode->i_size) |
| 767 | i_size_write(file->f_dentry->d_inode, |
| 768 | *poffset); |
| 769 | } |
| 770 | mark_inode_dirty_sync(file->f_dentry->d_inode); |
| 771 | } |
| 772 | } |
| 773 | FreeXid(xid); |
| 774 | return total_written; |
| 775 | } |
| 776 | |
| 777 | static ssize_t cifs_write(struct file *file, const char *write_data, |
| 778 | size_t write_size, loff_t *poffset) |
| 779 | { |
| 780 | int rc = 0; |
| 781 | unsigned int bytes_written = 0; |
| 782 | unsigned int total_written; |
| 783 | struct cifs_sb_info *cifs_sb; |
| 784 | struct cifsTconInfo *pTcon; |
| 785 | int xid, long_op; |
| 786 | struct cifsFileInfo *open_file; |
| 787 | |
| 788 | if (file->f_dentry == NULL) |
| 789 | return -EBADF; |
| 790 | |
| 791 | cifs_sb = CIFS_SB(file->f_dentry->d_sb); |
| 792 | if (cifs_sb == NULL) |
| 793 | return -EBADF; |
| 794 | |
| 795 | pTcon = cifs_sb->tcon; |
| 796 | |
Steve French | ab2f218 | 2005-09-15 20:44:50 -0700 | [diff] [blame] | 797 | cFYI(1,("write %zd bytes to offset %lld of %s", write_size, |
| 798 | *poffset, file->f_dentry->d_name.name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
| 800 | if (file->private_data == NULL) |
| 801 | return -EBADF; |
| 802 | else |
| 803 | open_file = (struct cifsFileInfo *)file->private_data; |
| 804 | |
| 805 | xid = GetXid(); |
| 806 | if (file->f_dentry->d_inode == NULL) { |
| 807 | FreeXid(xid); |
| 808 | return -EBADF; |
| 809 | } |
| 810 | |
| 811 | if (*poffset > file->f_dentry->d_inode->i_size) |
| 812 | long_op = 2; /* writes past end of file can take a long time */ |
| 813 | else |
| 814 | long_op = 1; |
| 815 | |
| 816 | for (total_written = 0; write_size > total_written; |
| 817 | total_written += bytes_written) { |
| 818 | rc = -EAGAIN; |
| 819 | while (rc == -EAGAIN) { |
| 820 | if (file->private_data == NULL) { |
| 821 | /* file has been closed on us */ |
| 822 | FreeXid(xid); |
| 823 | /* if we have gotten here we have written some data |
| 824 | and blocked, and the file has been freed on us |
| 825 | while we blocked so return what we managed to |
| 826 | write */ |
| 827 | return total_written; |
| 828 | } |
| 829 | if (open_file->closePend) { |
| 830 | FreeXid(xid); |
| 831 | if (total_written) |
| 832 | return total_written; |
| 833 | else |
| 834 | return -EBADF; |
| 835 | } |
| 836 | if (open_file->invalidHandle) { |
| 837 | if ((file->f_dentry == NULL) || |
| 838 | (file->f_dentry->d_inode == NULL)) { |
| 839 | FreeXid(xid); |
| 840 | return total_written; |
| 841 | } |
| 842 | /* we could deadlock if we called |
| 843 | filemap_fdatawait from here so tell |
| 844 | reopen_file not to flush data to |
| 845 | server now */ |
| 846 | rc = cifs_reopen_file(file->f_dentry->d_inode, |
| 847 | file, FALSE); |
| 848 | if (rc != 0) |
| 849 | break; |
| 850 | } |
Steve French | 0c0ff09 | 2005-06-23 19:31:17 -0500 | [diff] [blame] | 851 | #ifdef CONFIG_CIFS_EXPERIMENTAL |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 852 | /* BB FIXME We can not sign across two buffers yet */ |
Steve French | 0c0ff09 | 2005-06-23 19:31:17 -0500 | [diff] [blame] | 853 | if((experimEnabled) && ((pTcon->ses->server->secMode & |
| 854 | (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) == 0)) { |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 855 | struct kvec iov[2]; |
| 856 | unsigned int len; |
| 857 | |
Steve French | 0ae0efa | 2005-10-10 10:57:19 -0700 | [diff] [blame] | 858 | len = min((size_t)cifs_sb->wsize, |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 859 | write_size - total_written); |
| 860 | /* iov[0] is reserved for smb header */ |
| 861 | iov[1].iov_base = (char *)write_data + |
| 862 | total_written; |
| 863 | iov[1].iov_len = len; |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 864 | rc = CIFSSMBWrite2(xid, pTcon, |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 865 | open_file->netfid, len, |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 866 | *poffset, &bytes_written, |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 867 | iov, 1, long_op); |
Steve French | d6e04ae | 2005-06-13 13:24:43 -0500 | [diff] [blame] | 868 | } else |
| 869 | /* BB FIXME fixup indentation of line below */ |
| 870 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | rc = CIFSSMBWrite(xid, pTcon, |
| 872 | open_file->netfid, |
| 873 | min_t(const int, cifs_sb->wsize, |
| 874 | write_size - total_written), |
| 875 | *poffset, &bytes_written, |
| 876 | write_data + total_written, NULL, long_op); |
| 877 | } |
| 878 | if (rc || (bytes_written == 0)) { |
| 879 | if (total_written) |
| 880 | break; |
| 881 | else { |
| 882 | FreeXid(xid); |
| 883 | return rc; |
| 884 | } |
| 885 | } else |
| 886 | *poffset += bytes_written; |
| 887 | long_op = FALSE; /* subsequent writes fast - |
| 888 | 15 seconds is plenty */ |
| 889 | } |
| 890 | |
Steve French | a454434 | 2005-08-24 13:59:35 -0700 | [diff] [blame] | 891 | cifs_stats_bytes_written(pTcon, total_written); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
| 893 | /* since the write may have blocked check these pointers again */ |
| 894 | if (file->f_dentry) { |
| 895 | if (file->f_dentry->d_inode) { |
| 896 | file->f_dentry->d_inode->i_ctime = |
| 897 | file->f_dentry->d_inode->i_mtime = CURRENT_TIME; |
| 898 | if (total_written > 0) { |
| 899 | if (*poffset > file->f_dentry->d_inode->i_size) |
| 900 | i_size_write(file->f_dentry->d_inode, |
| 901 | *poffset); |
| 902 | } |
| 903 | mark_inode_dirty_sync(file->f_dentry->d_inode); |
| 904 | } |
| 905 | } |
| 906 | FreeXid(xid); |
| 907 | return total_written; |
| 908 | } |
| 909 | |
Steve French | dd99cd8 | 2005-10-05 19:32:49 -0700 | [diff] [blame] | 910 | struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode) |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 911 | { |
| 912 | struct cifsFileInfo *open_file; |
Steve French | dd99cd8 | 2005-10-05 19:32:49 -0700 | [diff] [blame] | 913 | int rc; |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 914 | |
| 915 | read_lock(&GlobalSMBSeslock); |
| 916 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { |
| 917 | if (open_file->closePend) |
| 918 | continue; |
| 919 | if (open_file->pfile && |
| 920 | ((open_file->pfile->f_flags & O_RDWR) || |
| 921 | (open_file->pfile->f_flags & O_WRONLY))) { |
| 922 | read_unlock(&GlobalSMBSeslock); |
Steve French | 0ae0efa | 2005-10-10 10:57:19 -0700 | [diff] [blame] | 923 | if((open_file->invalidHandle) && |
| 924 | (!open_file->closePend)) { |
Steve French | dd99cd8 | 2005-10-05 19:32:49 -0700 | [diff] [blame] | 925 | rc = cifs_reopen_file(&cifs_inode->vfs_inode, |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 926 | open_file->pfile, FALSE); |
| 927 | /* if it fails, try another handle - might be */ |
| 928 | /* dangerous to hold up writepages with retry */ |
| 929 | if(rc) { |
Steve French | 4a77118 | 2005-10-05 15:14:33 -0700 | [diff] [blame] | 930 | cFYI(1,("failed on reopen file in wp")); |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 931 | read_lock(&GlobalSMBSeslock); |
| 932 | continue; |
| 933 | } |
| 934 | } |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 935 | return open_file; |
| 936 | } |
| 937 | } |
| 938 | read_unlock(&GlobalSMBSeslock); |
| 939 | return NULL; |
| 940 | } |
| 941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to) |
| 943 | { |
| 944 | struct address_space *mapping = page->mapping; |
| 945 | loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT; |
| 946 | char *write_data; |
| 947 | int rc = -EFAULT; |
| 948 | int bytes_written = 0; |
| 949 | struct cifs_sb_info *cifs_sb; |
| 950 | struct cifsTconInfo *pTcon; |
| 951 | struct inode *inode; |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 952 | struct cifsFileInfo *open_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
| 954 | if (!mapping || !mapping->host) |
| 955 | return -EFAULT; |
| 956 | |
| 957 | inode = page->mapping->host; |
| 958 | cifs_sb = CIFS_SB(inode->i_sb); |
| 959 | pTcon = cifs_sb->tcon; |
| 960 | |
| 961 | offset += (loff_t)from; |
| 962 | write_data = kmap(page); |
| 963 | write_data += from; |
| 964 | |
| 965 | if ((to > PAGE_CACHE_SIZE) || (from > to)) { |
| 966 | kunmap(page); |
| 967 | return -EIO; |
| 968 | } |
| 969 | |
| 970 | /* racing with truncate? */ |
| 971 | if (offset > mapping->host->i_size) { |
| 972 | kunmap(page); |
| 973 | return 0; /* don't care */ |
| 974 | } |
| 975 | |
| 976 | /* check to make sure that we are not extending the file */ |
| 977 | if (mapping->host->i_size - offset < (loff_t)to) |
| 978 | to = (unsigned)(mapping->host->i_size - offset); |
| 979 | |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 980 | open_file = find_writable_file(CIFS_I(mapping->host)); |
| 981 | if (open_file) { |
| 982 | bytes_written = cifs_write(open_file->pfile, write_data, |
| 983 | to-from, &offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | /* Does mm or vfs already set times? */ |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 985 | inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb); |
| 986 | if ((bytes_written > 0) && (offset)) { |
| 987 | rc = 0; |
| 988 | } else if (bytes_written < 0) { |
| 989 | if (rc != -EBADF) |
| 990 | rc = bytes_written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | } |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 992 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | cFYI(1, ("No writeable filehandles for inode")); |
| 994 | rc = -EIO; |
| 995 | } |
| 996 | |
| 997 | kunmap(page); |
| 998 | return rc; |
| 999 | } |
| 1000 | |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1001 | #ifdef CONFIG_CIFS_EXPERIMENTAL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | static int cifs_writepages(struct address_space *mapping, |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1003 | struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | { |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1005 | struct backing_dev_info *bdi = mapping->backing_dev_info; |
| 1006 | unsigned int bytes_to_write; |
| 1007 | unsigned int bytes_written; |
| 1008 | struct cifs_sb_info *cifs_sb; |
| 1009 | int done = 0; |
| 1010 | pgoff_t end = -1; |
| 1011 | pgoff_t index; |
| 1012 | int is_range = 0; |
| 1013 | struct kvec iov[32]; |
Steve French | 84d2f07 | 2005-10-12 15:32:05 -0700 | [diff] [blame^] | 1014 | int len; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1015 | int n_iov = 0; |
| 1016 | pgoff_t next; |
| 1017 | int nr_pages; |
| 1018 | __u64 offset = 0; |
| 1019 | struct cifsFileInfo *open_file = NULL; |
| 1020 | struct page *page; |
| 1021 | struct pagevec pvec; |
| 1022 | int rc = 0; |
| 1023 | int scanned = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | int xid; |
| 1025 | |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1026 | cifs_sb = CIFS_SB(mapping->host->i_sb); |
| 1027 | |
| 1028 | /* |
| 1029 | * If wsize is smaller that the page cache size, default to writing |
| 1030 | * one page at a time via cifs_writepage |
| 1031 | */ |
| 1032 | if (cifs_sb->wsize < PAGE_CACHE_SIZE) |
| 1033 | return generic_writepages(mapping, wbc); |
| 1034 | |
Steve French | 4a77118 | 2005-10-05 15:14:33 -0700 | [diff] [blame] | 1035 | /* BB FIXME we do not have code to sign across multiple buffers yet, |
| 1036 | so go to older writepage style write which we can sign if needed */ |
| 1037 | if((cifs_sb->tcon->ses) && (cifs_sb->tcon->ses->server)) |
| 1038 | if(cifs_sb->tcon->ses->server->secMode & |
| 1039 | (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) |
| 1040 | return generic_writepages(mapping, wbc); |
| 1041 | |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1042 | /* |
| 1043 | * BB: Is this meaningful for a non-block-device file system? |
| 1044 | * If it is, we should test it again after we do I/O |
| 1045 | */ |
| 1046 | if (wbc->nonblocking && bdi_write_congested(bdi)) { |
| 1047 | wbc->encountered_congestion = 1; |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | xid = GetXid(); |
| 1052 | |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1053 | pagevec_init(&pvec, 0); |
| 1054 | if (wbc->sync_mode == WB_SYNC_NONE) |
| 1055 | index = mapping->writeback_index; /* Start from prev offset */ |
| 1056 | else { |
| 1057 | index = 0; |
| 1058 | scanned = 1; |
| 1059 | } |
| 1060 | if (wbc->start || wbc->end) { |
| 1061 | index = wbc->start >> PAGE_CACHE_SHIFT; |
| 1062 | end = wbc->end >> PAGE_CACHE_SHIFT; |
| 1063 | is_range = 1; |
| 1064 | scanned = 1; |
| 1065 | } |
| 1066 | retry: |
| 1067 | while (!done && (index <= end) && |
| 1068 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, |
| 1069 | PAGECACHE_TAG_DIRTY, |
| 1070 | min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1))) { |
| 1071 | int first; |
| 1072 | unsigned int i; |
| 1073 | |
| 1074 | if (!open_file) { |
| 1075 | open_file = find_writable_file(CIFS_I(mapping->host)); |
| 1076 | if (!open_file) { |
| 1077 | pagevec_release(&pvec); |
| 1078 | cERROR(1, ("No writable handles for inode")); |
| 1079 | return -EIO; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | first = -1; |
| 1084 | next = 0; |
| 1085 | n_iov = 0; |
| 1086 | bytes_to_write = 0; |
| 1087 | |
| 1088 | for (i = 0; i < nr_pages; i++) { |
| 1089 | page = pvec.pages[i]; |
| 1090 | /* |
| 1091 | * At this point we hold neither mapping->tree_lock nor |
| 1092 | * lock on the page itself: the page may be truncated or |
| 1093 | * invalidated (changing page->mapping to NULL), or even |
| 1094 | * swizzled back from swapper_space to tmpfs file |
| 1095 | * mapping |
| 1096 | */ |
| 1097 | |
| 1098 | if (first < 0) |
| 1099 | lock_page(page); |
| 1100 | else if (TestSetPageLocked(page)) |
| 1101 | break; |
| 1102 | |
| 1103 | if (unlikely(page->mapping != mapping)) { |
| 1104 | unlock_page(page); |
| 1105 | break; |
| 1106 | } |
| 1107 | |
| 1108 | if (unlikely(is_range) && (page->index > end)) { |
| 1109 | done = 1; |
| 1110 | unlock_page(page); |
| 1111 | break; |
| 1112 | } |
| 1113 | |
| 1114 | if (next && (page->index != next)) { |
| 1115 | /* Not next consecutive page */ |
| 1116 | unlock_page(page); |
| 1117 | break; |
| 1118 | } |
| 1119 | |
| 1120 | if (wbc->sync_mode != WB_SYNC_NONE) |
| 1121 | wait_on_page_writeback(page); |
| 1122 | |
| 1123 | if (PageWriteback(page) || |
| 1124 | !test_clear_page_dirty(page)) { |
| 1125 | unlock_page(page); |
| 1126 | break; |
| 1127 | } |
Steve French | 84d2f07 | 2005-10-12 15:32:05 -0700 | [diff] [blame^] | 1128 | |
| 1129 | if (page_offset(page) >= mapping->host->i_size) { |
| 1130 | done = 1; |
| 1131 | unlock_page(page); |
| 1132 | break; |
| 1133 | } |
| 1134 | |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1135 | /* |
| 1136 | * BB can we get rid of this? pages are held by pvec |
| 1137 | */ |
| 1138 | page_cache_get(page); |
| 1139 | |
Steve French | 84d2f07 | 2005-10-12 15:32:05 -0700 | [diff] [blame^] | 1140 | len = min(mapping->host->i_size - page_offset(page), |
| 1141 | (loff_t)PAGE_CACHE_SIZE); |
| 1142 | |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1143 | /* reserve iov[0] for the smb header */ |
| 1144 | n_iov++; |
| 1145 | iov[n_iov].iov_base = kmap(page); |
Steve French | 84d2f07 | 2005-10-12 15:32:05 -0700 | [diff] [blame^] | 1146 | iov[n_iov].iov_len = len; |
| 1147 | bytes_to_write += len; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1148 | |
| 1149 | if (first < 0) { |
| 1150 | first = i; |
| 1151 | offset = page_offset(page); |
| 1152 | } |
| 1153 | next = page->index + 1; |
| 1154 | if (bytes_to_write + PAGE_CACHE_SIZE > cifs_sb->wsize) |
| 1155 | break; |
| 1156 | } |
| 1157 | if (n_iov) { |
| 1158 | rc = CIFSSMBWrite2(xid, cifs_sb->tcon, |
| 1159 | open_file->netfid, bytes_to_write, |
| 1160 | offset, &bytes_written, iov, n_iov, |
| 1161 | 1); |
| 1162 | if (rc || bytes_written < bytes_to_write) { |
| 1163 | cERROR(1,("CIFSSMBWrite2 returned %d, written = %x", |
| 1164 | rc, bytes_written)); |
| 1165 | set_bit(AS_EIO, &mapping->flags); |
| 1166 | SetPageError(page); |
Steve French | 1047abc | 2005-10-11 19:58:06 -0700 | [diff] [blame] | 1167 | } else { |
| 1168 | cifs_stats_bytes_written(cifs_sb->tcon, |
| 1169 | bytes_written); |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1170 | } |
| 1171 | for (i = 0; i < n_iov; i++) { |
| 1172 | page = pvec.pages[first + i]; |
| 1173 | kunmap(page); |
| 1174 | unlock_page(page); |
| 1175 | page_cache_release(page); |
| 1176 | } |
| 1177 | if ((wbc->nr_to_write -= n_iov) <= 0) |
| 1178 | done = 1; |
| 1179 | index = next; |
| 1180 | } |
| 1181 | pagevec_release(&pvec); |
| 1182 | } |
| 1183 | if (!scanned && !done) { |
| 1184 | /* |
| 1185 | * We hit the last page and there is more work to be done: wrap |
| 1186 | * back to the start of the file |
| 1187 | */ |
| 1188 | scanned = 1; |
| 1189 | index = 0; |
| 1190 | goto retry; |
| 1191 | } |
| 1192 | if (!is_range) |
| 1193 | mapping->writeback_index = index; |
| 1194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | FreeXid(xid); |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | return rc; |
| 1198 | } |
| 1199 | #endif |
| 1200 | |
| 1201 | static int cifs_writepage(struct page* page, struct writeback_control *wbc) |
| 1202 | { |
| 1203 | int rc = -EFAULT; |
| 1204 | int xid; |
| 1205 | |
| 1206 | xid = GetXid(); |
| 1207 | /* BB add check for wbc flags */ |
| 1208 | page_cache_get(page); |
| 1209 | if (!PageUptodate(page)) { |
| 1210 | cFYI(1, ("ppw - page not up to date")); |
| 1211 | } |
| 1212 | |
| 1213 | rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE); |
| 1214 | SetPageUptodate(page); /* BB add check for error and Clearuptodate? */ |
| 1215 | unlock_page(page); |
| 1216 | page_cache_release(page); |
| 1217 | FreeXid(xid); |
| 1218 | return rc; |
| 1219 | } |
| 1220 | |
| 1221 | static int cifs_commit_write(struct file *file, struct page *page, |
| 1222 | unsigned offset, unsigned to) |
| 1223 | { |
| 1224 | int xid; |
| 1225 | int rc = 0; |
| 1226 | struct inode *inode = page->mapping->host; |
| 1227 | loff_t position = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; |
| 1228 | char *page_data; |
| 1229 | |
| 1230 | xid = GetXid(); |
| 1231 | cFYI(1, ("commit write for page %p up to position %lld for %d", |
| 1232 | page, position, to)); |
| 1233 | if (position > inode->i_size) { |
| 1234 | i_size_write(inode, position); |
| 1235 | /* if (file->private_data == NULL) { |
| 1236 | rc = -EBADF; |
| 1237 | } else { |
| 1238 | open_file = (struct cifsFileInfo *)file->private_data; |
| 1239 | cifs_sb = CIFS_SB(inode->i_sb); |
| 1240 | rc = -EAGAIN; |
| 1241 | while (rc == -EAGAIN) { |
| 1242 | if ((open_file->invalidHandle) && |
| 1243 | (!open_file->closePend)) { |
| 1244 | rc = cifs_reopen_file( |
| 1245 | file->f_dentry->d_inode, file); |
| 1246 | if (rc != 0) |
| 1247 | break; |
| 1248 | } |
| 1249 | if (!open_file->closePend) { |
| 1250 | rc = CIFSSMBSetFileSize(xid, |
| 1251 | cifs_sb->tcon, position, |
| 1252 | open_file->netfid, |
| 1253 | open_file->pid, FALSE); |
| 1254 | } else { |
| 1255 | rc = -EBADF; |
| 1256 | break; |
| 1257 | } |
| 1258 | } |
| 1259 | cFYI(1, (" SetEOF (commit write) rc = %d", rc)); |
| 1260 | } */ |
| 1261 | } |
| 1262 | if (!PageUptodate(page)) { |
| 1263 | position = ((loff_t)page->index << PAGE_CACHE_SHIFT) + offset; |
| 1264 | /* can not rely on (or let) writepage write this data */ |
| 1265 | if (to < offset) { |
| 1266 | cFYI(1, ("Illegal offsets, can not copy from %d to %d", |
| 1267 | offset, to)); |
| 1268 | FreeXid(xid); |
| 1269 | return rc; |
| 1270 | } |
| 1271 | /* this is probably better than directly calling |
| 1272 | partialpage_write since in this function the file handle is |
| 1273 | known which we might as well leverage */ |
| 1274 | /* BB check if anything else missing out of ppw |
| 1275 | such as updating last write time */ |
| 1276 | page_data = kmap(page); |
| 1277 | rc = cifs_write(file, page_data + offset, to-offset, |
| 1278 | &position); |
| 1279 | if (rc > 0) |
| 1280 | rc = 0; |
| 1281 | /* else if (rc < 0) should we set writebehind rc? */ |
| 1282 | kunmap(page); |
| 1283 | } else { |
| 1284 | set_page_dirty(page); |
| 1285 | } |
| 1286 | |
| 1287 | FreeXid(xid); |
| 1288 | return rc; |
| 1289 | } |
| 1290 | |
| 1291 | int cifs_fsync(struct file *file, struct dentry *dentry, int datasync) |
| 1292 | { |
| 1293 | int xid; |
| 1294 | int rc = 0; |
| 1295 | struct inode *inode = file->f_dentry->d_inode; |
| 1296 | |
| 1297 | xid = GetXid(); |
| 1298 | |
| 1299 | cFYI(1, ("Sync file - name: %s datasync: 0x%x ", |
| 1300 | dentry->d_name.name, datasync)); |
| 1301 | |
| 1302 | rc = filemap_fdatawrite(inode->i_mapping); |
| 1303 | if (rc == 0) |
| 1304 | CIFS_I(inode)->write_behind_rc = 0; |
| 1305 | FreeXid(xid); |
| 1306 | return rc; |
| 1307 | } |
| 1308 | |
| 1309 | /* static int cifs_sync_page(struct page *page) |
| 1310 | { |
| 1311 | struct address_space *mapping; |
| 1312 | struct inode *inode; |
| 1313 | unsigned long index = page->index; |
| 1314 | unsigned int rpages = 0; |
| 1315 | int rc = 0; |
| 1316 | |
| 1317 | cFYI(1, ("sync page %p",page)); |
| 1318 | mapping = page->mapping; |
| 1319 | if (!mapping) |
| 1320 | return 0; |
| 1321 | inode = mapping->host; |
| 1322 | if (!inode) |
| 1323 | return 0; */ |
| 1324 | |
| 1325 | /* fill in rpages then |
| 1326 | result = cifs_pagein_inode(inode, index, rpages); */ /* BB finish */ |
| 1327 | |
| 1328 | /* cFYI(1, ("rpages is %d for sync page of Index %ld ", rpages, index)); |
| 1329 | |
| 1330 | if (rc < 0) |
| 1331 | return rc; |
| 1332 | return 0; |
| 1333 | } */ |
| 1334 | |
| 1335 | /* |
| 1336 | * As file closes, flush all cached write data for this inode checking |
| 1337 | * for write behind errors. |
| 1338 | */ |
| 1339 | int cifs_flush(struct file *file) |
| 1340 | { |
| 1341 | struct inode * inode = file->f_dentry->d_inode; |
| 1342 | int rc = 0; |
| 1343 | |
| 1344 | /* Rather than do the steps manually: |
| 1345 | lock the inode for writing |
| 1346 | loop through pages looking for write behind data (dirty pages) |
| 1347 | coalesce into contiguous 16K (or smaller) chunks to write to server |
| 1348 | send to server (prefer in parallel) |
| 1349 | deal with writebehind errors |
| 1350 | unlock inode for writing |
| 1351 | filemapfdatawrite appears easier for the time being */ |
| 1352 | |
| 1353 | rc = filemap_fdatawrite(inode->i_mapping); |
| 1354 | if (!rc) /* reset wb rc if we were able to write out dirty pages */ |
| 1355 | CIFS_I(inode)->write_behind_rc = 0; |
| 1356 | |
| 1357 | cFYI(1, ("Flush inode %p file %p rc %d",inode,file,rc)); |
| 1358 | |
| 1359 | return rc; |
| 1360 | } |
| 1361 | |
| 1362 | ssize_t cifs_user_read(struct file *file, char __user *read_data, |
| 1363 | size_t read_size, loff_t *poffset) |
| 1364 | { |
| 1365 | int rc = -EACCES; |
| 1366 | unsigned int bytes_read = 0; |
| 1367 | unsigned int total_read = 0; |
| 1368 | unsigned int current_read_size; |
| 1369 | struct cifs_sb_info *cifs_sb; |
| 1370 | struct cifsTconInfo *pTcon; |
| 1371 | int xid; |
| 1372 | struct cifsFileInfo *open_file; |
| 1373 | char *smb_read_data; |
| 1374 | char __user *current_offset; |
| 1375 | struct smb_com_read_rsp *pSMBr; |
| 1376 | |
| 1377 | xid = GetXid(); |
| 1378 | cifs_sb = CIFS_SB(file->f_dentry->d_sb); |
| 1379 | pTcon = cifs_sb->tcon; |
| 1380 | |
| 1381 | if (file->private_data == NULL) { |
| 1382 | FreeXid(xid); |
| 1383 | return -EBADF; |
| 1384 | } |
| 1385 | open_file = (struct cifsFileInfo *)file->private_data; |
| 1386 | |
| 1387 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) { |
| 1388 | cFYI(1, ("attempting read on write only file instance")); |
| 1389 | } |
| 1390 | for (total_read = 0, current_offset = read_data; |
| 1391 | read_size > total_read; |
| 1392 | total_read += bytes_read, current_offset += bytes_read) { |
| 1393 | current_read_size = min_t(const int, read_size - total_read, |
| 1394 | cifs_sb->rsize); |
| 1395 | rc = -EAGAIN; |
| 1396 | smb_read_data = NULL; |
| 1397 | while (rc == -EAGAIN) { |
| 1398 | if ((open_file->invalidHandle) && |
| 1399 | (!open_file->closePend)) { |
| 1400 | rc = cifs_reopen_file(file->f_dentry->d_inode, |
| 1401 | file, TRUE); |
| 1402 | if (rc != 0) |
| 1403 | break; |
| 1404 | } |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1405 | rc = CIFSSMBRead(xid, pTcon, |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1406 | open_file->netfid, |
| 1407 | current_read_size, *poffset, |
| 1408 | &bytes_read, &smb_read_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | pSMBr = (struct smb_com_read_rsp *)smb_read_data; |
| 1410 | if (copy_to_user(current_offset, |
| 1411 | smb_read_data + 4 /* RFC1001 hdr */ |
| 1412 | + le16_to_cpu(pSMBr->DataOffset), |
| 1413 | bytes_read)) { |
| 1414 | rc = -EFAULT; |
| 1415 | FreeXid(xid); |
| 1416 | return rc; |
| 1417 | } |
| 1418 | if (smb_read_data) { |
| 1419 | cifs_buf_release(smb_read_data); |
| 1420 | smb_read_data = NULL; |
| 1421 | } |
| 1422 | } |
| 1423 | if (rc || (bytes_read == 0)) { |
| 1424 | if (total_read) { |
| 1425 | break; |
| 1426 | } else { |
| 1427 | FreeXid(xid); |
| 1428 | return rc; |
| 1429 | } |
| 1430 | } else { |
Steve French | a454434 | 2005-08-24 13:59:35 -0700 | [diff] [blame] | 1431 | cifs_stats_bytes_read(pTcon, bytes_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | *poffset += bytes_read; |
| 1433 | } |
| 1434 | } |
| 1435 | FreeXid(xid); |
| 1436 | return total_read; |
| 1437 | } |
| 1438 | |
| 1439 | |
| 1440 | static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size, |
| 1441 | loff_t *poffset) |
| 1442 | { |
| 1443 | int rc = -EACCES; |
| 1444 | unsigned int bytes_read = 0; |
| 1445 | unsigned int total_read; |
| 1446 | unsigned int current_read_size; |
| 1447 | struct cifs_sb_info *cifs_sb; |
| 1448 | struct cifsTconInfo *pTcon; |
| 1449 | int xid; |
| 1450 | char *current_offset; |
| 1451 | struct cifsFileInfo *open_file; |
| 1452 | |
| 1453 | xid = GetXid(); |
| 1454 | cifs_sb = CIFS_SB(file->f_dentry->d_sb); |
| 1455 | pTcon = cifs_sb->tcon; |
| 1456 | |
| 1457 | if (file->private_data == NULL) { |
| 1458 | FreeXid(xid); |
| 1459 | return -EBADF; |
| 1460 | } |
| 1461 | open_file = (struct cifsFileInfo *)file->private_data; |
| 1462 | |
| 1463 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) |
| 1464 | cFYI(1, ("attempting read on write only file instance")); |
| 1465 | |
| 1466 | for (total_read = 0, current_offset = read_data; |
| 1467 | read_size > total_read; |
| 1468 | total_read += bytes_read, current_offset += bytes_read) { |
| 1469 | current_read_size = min_t(const int, read_size - total_read, |
| 1470 | cifs_sb->rsize); |
Steve French | f9f5c817 | 2005-09-15 23:06:38 -0700 | [diff] [blame] | 1471 | /* For windows me and 9x we do not want to request more |
| 1472 | than it negotiated since it will refuse the read then */ |
| 1473 | if((pTcon->ses) && |
| 1474 | !(pTcon->ses->capabilities & CAP_LARGE_FILES)) { |
| 1475 | current_read_size = min_t(const int, current_read_size, |
| 1476 | pTcon->ses->server->maxBuf - 128); |
| 1477 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | rc = -EAGAIN; |
| 1479 | while (rc == -EAGAIN) { |
| 1480 | if ((open_file->invalidHandle) && |
| 1481 | (!open_file->closePend)) { |
| 1482 | rc = cifs_reopen_file(file->f_dentry->d_inode, |
| 1483 | file, TRUE); |
| 1484 | if (rc != 0) |
| 1485 | break; |
| 1486 | } |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1487 | rc = CIFSSMBRead(xid, pTcon, |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1488 | open_file->netfid, |
| 1489 | current_read_size, *poffset, |
| 1490 | &bytes_read, ¤t_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | } |
| 1492 | if (rc || (bytes_read == 0)) { |
| 1493 | if (total_read) { |
| 1494 | break; |
| 1495 | } else { |
| 1496 | FreeXid(xid); |
| 1497 | return rc; |
| 1498 | } |
| 1499 | } else { |
Steve French | a454434 | 2005-08-24 13:59:35 -0700 | [diff] [blame] | 1500 | cifs_stats_bytes_read(pTcon, total_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | *poffset += bytes_read; |
| 1502 | } |
| 1503 | } |
| 1504 | FreeXid(xid); |
| 1505 | return total_read; |
| 1506 | } |
| 1507 | |
| 1508 | int cifs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 1509 | { |
| 1510 | struct dentry *dentry = file->f_dentry; |
| 1511 | int rc, xid; |
| 1512 | |
| 1513 | xid = GetXid(); |
| 1514 | rc = cifs_revalidate(dentry); |
| 1515 | if (rc) { |
| 1516 | cFYI(1, ("Validation prior to mmap failed, error=%d", rc)); |
| 1517 | FreeXid(xid); |
| 1518 | return rc; |
| 1519 | } |
| 1520 | rc = generic_file_mmap(file, vma); |
| 1521 | FreeXid(xid); |
| 1522 | return rc; |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | static void cifs_copy_cache_pages(struct address_space *mapping, |
| 1527 | struct list_head *pages, int bytes_read, char *data, |
| 1528 | struct pagevec *plru_pvec) |
| 1529 | { |
| 1530 | struct page *page; |
| 1531 | char *target; |
| 1532 | |
| 1533 | while (bytes_read > 0) { |
| 1534 | if (list_empty(pages)) |
| 1535 | break; |
| 1536 | |
| 1537 | page = list_entry(pages->prev, struct page, lru); |
| 1538 | list_del(&page->lru); |
| 1539 | |
| 1540 | if (add_to_page_cache(page, mapping, page->index, |
| 1541 | GFP_KERNEL)) { |
| 1542 | page_cache_release(page); |
| 1543 | cFYI(1, ("Add page cache failed")); |
Steve French | 3079ca6 | 2005-06-09 14:44:07 -0700 | [diff] [blame] | 1544 | data += PAGE_CACHE_SIZE; |
| 1545 | bytes_read -= PAGE_CACHE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | continue; |
| 1547 | } |
| 1548 | |
| 1549 | target = kmap_atomic(page,KM_USER0); |
| 1550 | |
| 1551 | if (PAGE_CACHE_SIZE > bytes_read) { |
| 1552 | memcpy(target, data, bytes_read); |
| 1553 | /* zero the tail end of this partial page */ |
| 1554 | memset(target + bytes_read, 0, |
| 1555 | PAGE_CACHE_SIZE - bytes_read); |
| 1556 | bytes_read = 0; |
| 1557 | } else { |
| 1558 | memcpy(target, data, PAGE_CACHE_SIZE); |
| 1559 | bytes_read -= PAGE_CACHE_SIZE; |
| 1560 | } |
| 1561 | kunmap_atomic(target, KM_USER0); |
| 1562 | |
| 1563 | flush_dcache_page(page); |
| 1564 | SetPageUptodate(page); |
| 1565 | unlock_page(page); |
| 1566 | if (!pagevec_add(plru_pvec, page)) |
| 1567 | __pagevec_lru_add(plru_pvec); |
| 1568 | data += PAGE_CACHE_SIZE; |
| 1569 | } |
| 1570 | return; |
| 1571 | } |
| 1572 | |
| 1573 | static int cifs_readpages(struct file *file, struct address_space *mapping, |
| 1574 | struct list_head *page_list, unsigned num_pages) |
| 1575 | { |
| 1576 | int rc = -EACCES; |
| 1577 | int xid; |
| 1578 | loff_t offset; |
| 1579 | struct page *page; |
| 1580 | struct cifs_sb_info *cifs_sb; |
| 1581 | struct cifsTconInfo *pTcon; |
| 1582 | int bytes_read = 0; |
| 1583 | unsigned int read_size,i; |
| 1584 | char *smb_read_data = NULL; |
| 1585 | struct smb_com_read_rsp *pSMBr; |
| 1586 | struct pagevec lru_pvec; |
| 1587 | struct cifsFileInfo *open_file; |
| 1588 | |
| 1589 | xid = GetXid(); |
| 1590 | if (file->private_data == NULL) { |
| 1591 | FreeXid(xid); |
| 1592 | return -EBADF; |
| 1593 | } |
| 1594 | open_file = (struct cifsFileInfo *)file->private_data; |
| 1595 | cifs_sb = CIFS_SB(file->f_dentry->d_sb); |
| 1596 | pTcon = cifs_sb->tcon; |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | pagevec_init(&lru_pvec, 0); |
| 1599 | |
| 1600 | for (i = 0; i < num_pages; ) { |
| 1601 | unsigned contig_pages; |
| 1602 | struct page *tmp_page; |
| 1603 | unsigned long expected_index; |
| 1604 | |
| 1605 | if (list_empty(page_list)) |
| 1606 | break; |
| 1607 | |
| 1608 | page = list_entry(page_list->prev, struct page, lru); |
| 1609 | offset = (loff_t)page->index << PAGE_CACHE_SHIFT; |
| 1610 | |
| 1611 | /* count adjacent pages that we will read into */ |
| 1612 | contig_pages = 0; |
| 1613 | expected_index = |
| 1614 | list_entry(page_list->prev, struct page, lru)->index; |
| 1615 | list_for_each_entry_reverse(tmp_page,page_list,lru) { |
| 1616 | if (tmp_page->index == expected_index) { |
| 1617 | contig_pages++; |
| 1618 | expected_index++; |
| 1619 | } else |
| 1620 | break; |
| 1621 | } |
| 1622 | if (contig_pages + i > num_pages) |
| 1623 | contig_pages = num_pages - i; |
| 1624 | |
| 1625 | /* for reads over a certain size could initiate async |
| 1626 | read ahead */ |
| 1627 | |
| 1628 | read_size = contig_pages * PAGE_CACHE_SIZE; |
| 1629 | /* Read size needs to be in multiples of one page */ |
| 1630 | read_size = min_t(const unsigned int, read_size, |
| 1631 | cifs_sb->rsize & PAGE_CACHE_MASK); |
| 1632 | |
| 1633 | rc = -EAGAIN; |
| 1634 | while (rc == -EAGAIN) { |
| 1635 | if ((open_file->invalidHandle) && |
| 1636 | (!open_file->closePend)) { |
| 1637 | rc = cifs_reopen_file(file->f_dentry->d_inode, |
| 1638 | file, TRUE); |
| 1639 | if (rc != 0) |
| 1640 | break; |
| 1641 | } |
| 1642 | |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 1643 | rc = CIFSSMBRead(xid, pTcon, |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1644 | open_file->netfid, |
| 1645 | read_size, offset, |
| 1646 | &bytes_read, &smb_read_data); |
Steve French | a9d02ad | 2005-08-24 23:06:05 -0700 | [diff] [blame] | 1647 | |
| 1648 | /* BB more RC checks ? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | if (rc== -EAGAIN) { |
| 1650 | if (smb_read_data) { |
| 1651 | cifs_buf_release(smb_read_data); |
| 1652 | smb_read_data = NULL; |
| 1653 | } |
| 1654 | } |
| 1655 | } |
| 1656 | if ((rc < 0) || (smb_read_data == NULL)) { |
| 1657 | cFYI(1, ("Read error in readpages: %d", rc)); |
| 1658 | /* clean up remaing pages off list */ |
| 1659 | while (!list_empty(page_list) && (i < num_pages)) { |
| 1660 | page = list_entry(page_list->prev, struct page, |
| 1661 | lru); |
| 1662 | list_del(&page->lru); |
| 1663 | page_cache_release(page); |
| 1664 | } |
| 1665 | break; |
| 1666 | } else if (bytes_read > 0) { |
| 1667 | pSMBr = (struct smb_com_read_rsp *)smb_read_data; |
| 1668 | cifs_copy_cache_pages(mapping, page_list, bytes_read, |
| 1669 | smb_read_data + 4 /* RFC1001 hdr */ + |
| 1670 | le16_to_cpu(pSMBr->DataOffset), &lru_pvec); |
| 1671 | |
| 1672 | i += bytes_read >> PAGE_CACHE_SHIFT; |
Steve French | a454434 | 2005-08-24 13:59:35 -0700 | [diff] [blame] | 1673 | cifs_stats_bytes_read(pTcon, bytes_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | if ((int)(bytes_read & PAGE_CACHE_MASK) != bytes_read) { |
| 1675 | i++; /* account for partial page */ |
| 1676 | |
| 1677 | /* server copy of file can have smaller size |
| 1678 | than client */ |
| 1679 | /* BB do we need to verify this common case ? |
| 1680 | this case is ok - if we are at server EOF |
| 1681 | we will hit it on next read */ |
| 1682 | |
| 1683 | /* while (!list_empty(page_list) && (i < num_pages)) { |
| 1684 | page = list_entry(page_list->prev, |
| 1685 | struct page, list); |
| 1686 | list_del(&page->list); |
| 1687 | page_cache_release(page); |
| 1688 | } |
| 1689 | break; */ |
| 1690 | } |
| 1691 | } else { |
| 1692 | cFYI(1, ("No bytes read (%d) at offset %lld . " |
| 1693 | "Cleaning remaining pages from readahead list", |
| 1694 | bytes_read, offset)); |
| 1695 | /* BB turn off caching and do new lookup on |
| 1696 | file size at server? */ |
| 1697 | while (!list_empty(page_list) && (i < num_pages)) { |
| 1698 | page = list_entry(page_list->prev, struct page, |
| 1699 | lru); |
| 1700 | list_del(&page->lru); |
| 1701 | |
| 1702 | /* BB removeme - replace with zero of page? */ |
| 1703 | page_cache_release(page); |
| 1704 | } |
| 1705 | break; |
| 1706 | } |
| 1707 | if (smb_read_data) { |
| 1708 | cifs_buf_release(smb_read_data); |
| 1709 | smb_read_data = NULL; |
| 1710 | } |
| 1711 | bytes_read = 0; |
| 1712 | } |
| 1713 | |
| 1714 | pagevec_lru_add(&lru_pvec); |
| 1715 | |
| 1716 | /* need to free smb_read_data buf before exit */ |
| 1717 | if (smb_read_data) { |
| 1718 | cifs_buf_release(smb_read_data); |
| 1719 | smb_read_data = NULL; |
| 1720 | } |
| 1721 | |
| 1722 | FreeXid(xid); |
| 1723 | return rc; |
| 1724 | } |
| 1725 | |
| 1726 | static int cifs_readpage_worker(struct file *file, struct page *page, |
| 1727 | loff_t *poffset) |
| 1728 | { |
| 1729 | char *read_data; |
| 1730 | int rc; |
| 1731 | |
| 1732 | page_cache_get(page); |
| 1733 | read_data = kmap(page); |
| 1734 | /* for reads over a certain size could initiate async read ahead */ |
| 1735 | |
| 1736 | rc = cifs_read(file, read_data, PAGE_CACHE_SIZE, poffset); |
| 1737 | |
| 1738 | if (rc < 0) |
| 1739 | goto io_error; |
| 1740 | else |
| 1741 | cFYI(1, ("Bytes read %d ",rc)); |
| 1742 | |
| 1743 | file->f_dentry->d_inode->i_atime = |
| 1744 | current_fs_time(file->f_dentry->d_inode->i_sb); |
| 1745 | |
| 1746 | if (PAGE_CACHE_SIZE > rc) |
| 1747 | memset(read_data + rc, 0, PAGE_CACHE_SIZE - rc); |
| 1748 | |
| 1749 | flush_dcache_page(page); |
| 1750 | SetPageUptodate(page); |
| 1751 | rc = 0; |
| 1752 | |
| 1753 | io_error: |
| 1754 | kunmap(page); |
| 1755 | page_cache_release(page); |
| 1756 | return rc; |
| 1757 | } |
| 1758 | |
| 1759 | static int cifs_readpage(struct file *file, struct page *page) |
| 1760 | { |
| 1761 | loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT; |
| 1762 | int rc = -EACCES; |
| 1763 | int xid; |
| 1764 | |
| 1765 | xid = GetXid(); |
| 1766 | |
| 1767 | if (file->private_data == NULL) { |
| 1768 | FreeXid(xid); |
| 1769 | return -EBADF; |
| 1770 | } |
| 1771 | |
| 1772 | cFYI(1, ("readpage %p at offset %d 0x%x\n", |
| 1773 | page, (int)offset, (int)offset)); |
| 1774 | |
| 1775 | rc = cifs_readpage_worker(file, page, &offset); |
| 1776 | |
| 1777 | unlock_page(page); |
| 1778 | |
| 1779 | FreeXid(xid); |
| 1780 | return rc; |
| 1781 | } |
| 1782 | |
| 1783 | /* We do not want to update the file size from server for inodes |
| 1784 | open for write - to avoid races with writepage extending |
| 1785 | the file - in the future we could consider allowing |
| 1786 | refreshing the inode only on increases in the file size |
| 1787 | but this is tricky to do without racing with writebehind |
| 1788 | page caching in the current Linux kernel design */ |
| 1789 | int is_size_safe_to_change(struct cifsInodeInfo *cifsInode) |
| 1790 | { |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1791 | if (cifsInode && find_writable_file(cifsInode)) |
| 1792 | return 0; |
| 1793 | else |
| 1794 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | } |
| 1796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | static int cifs_prepare_write(struct file *file, struct page *page, |
| 1798 | unsigned from, unsigned to) |
| 1799 | { |
| 1800 | int rc = 0; |
| 1801 | loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT; |
| 1802 | cFYI(1, ("prepare write for page %p from %d to %d",page,from,to)); |
| 1803 | if (!PageUptodate(page)) { |
| 1804 | /* if (to - from != PAGE_CACHE_SIZE) { |
| 1805 | void *kaddr = kmap_atomic(page, KM_USER0); |
| 1806 | memset(kaddr, 0, from); |
| 1807 | memset(kaddr + to, 0, PAGE_CACHE_SIZE - to); |
| 1808 | flush_dcache_page(page); |
| 1809 | kunmap_atomic(kaddr, KM_USER0); |
| 1810 | } */ |
| 1811 | /* If we are writing a full page it will be up to date, |
| 1812 | no need to read from the server */ |
| 1813 | if ((to == PAGE_CACHE_SIZE) && (from == 0)) |
| 1814 | SetPageUptodate(page); |
| 1815 | |
| 1816 | /* might as well read a page, it is fast enough */ |
| 1817 | if ((file->f_flags & O_ACCMODE) != O_WRONLY) { |
| 1818 | rc = cifs_readpage_worker(file, page, &offset); |
| 1819 | } else { |
| 1820 | /* should we try using another file handle if there is one - |
| 1821 | how would we lock it to prevent close of that handle |
| 1822 | racing with this read? |
| 1823 | In any case this will be written out by commit_write */ |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | /* BB should we pass any errors back? |
| 1828 | e.g. if we do not have read access to the file */ |
| 1829 | return 0; |
| 1830 | } |
| 1831 | |
| 1832 | struct address_space_operations cifs_addr_ops = { |
| 1833 | .readpage = cifs_readpage, |
| 1834 | .readpages = cifs_readpages, |
| 1835 | .writepage = cifs_writepage, |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 1836 | #ifdef CONFIG_CIFS_EXPERIMENTAL |
| 1837 | .writepages = cifs_writepages, |
| 1838 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | .prepare_write = cifs_prepare_write, |
| 1840 | .commit_write = cifs_commit_write, |
| 1841 | .set_page_dirty = __set_page_dirty_nobuffers, |
| 1842 | /* .sync_page = cifs_sync_page, */ |
| 1843 | /* .direct_IO = */ |
| 1844 | }; |