]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/cifs/file.c | |
3 | * | |
4 | * vfs operations that deal with files | |
fb8c4b14 | 5 | * |
f19159dc | 6 | * Copyright (C) International Business Machines Corp., 2002,2010 |
1da177e4 | 7 | * Author(s): Steve French (sfrench@us.ibm.com) |
7ee1af76 | 8 | * Jeremy Allison (jra@samba.org) |
1da177e4 LT |
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> | |
37c0eb46 | 25 | #include <linux/backing-dev.h> |
1da177e4 LT |
26 | #include <linux/stat.h> |
27 | #include <linux/fcntl.h> | |
28 | #include <linux/pagemap.h> | |
29 | #include <linux/pagevec.h> | |
37c0eb46 | 30 | #include <linux/writeback.h> |
6f88cc2e | 31 | #include <linux/task_io_accounting_ops.h> |
23e7dd7d | 32 | #include <linux/delay.h> |
3bc303c2 | 33 | #include <linux/mount.h> |
5a0e3ad6 | 34 | #include <linux/slab.h> |
690c5e31 | 35 | #include <linux/swap.h> |
f86196ea | 36 | #include <linux/mm.h> |
1da177e4 LT |
37 | #include <asm/div64.h> |
38 | #include "cifsfs.h" | |
39 | #include "cifspdu.h" | |
40 | #include "cifsglob.h" | |
41 | #include "cifsproto.h" | |
42 | #include "cifs_unicode.h" | |
43 | #include "cifs_debug.h" | |
44 | #include "cifs_fs_sb.h" | |
9451a9a5 | 45 | #include "fscache.h" |
bd3dcc6a | 46 | #include "smbdirect.h" |
07b92d0d | 47 | |
1da177e4 LT |
48 | static inline int cifs_convert_flags(unsigned int flags) |
49 | { | |
50 | if ((flags & O_ACCMODE) == O_RDONLY) | |
51 | return GENERIC_READ; | |
52 | else if ((flags & O_ACCMODE) == O_WRONLY) | |
53 | return GENERIC_WRITE; | |
54 | else if ((flags & O_ACCMODE) == O_RDWR) { | |
55 | /* GENERIC_ALL is too much permission to request | |
56 | can cause unnecessary access denied on create */ | |
57 | /* return GENERIC_ALL; */ | |
58 | return (GENERIC_READ | GENERIC_WRITE); | |
59 | } | |
60 | ||
e10f7b55 JL |
61 | return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | |
62 | FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA | | |
63 | FILE_READ_DATA); | |
7fc8f4e9 | 64 | } |
e10f7b55 | 65 | |
608712fe | 66 | static u32 cifs_posix_convert_flags(unsigned int flags) |
7fc8f4e9 | 67 | { |
608712fe | 68 | u32 posix_flags = 0; |
e10f7b55 | 69 | |
7fc8f4e9 | 70 | if ((flags & O_ACCMODE) == O_RDONLY) |
608712fe | 71 | posix_flags = SMB_O_RDONLY; |
7fc8f4e9 | 72 | else if ((flags & O_ACCMODE) == O_WRONLY) |
608712fe JL |
73 | posix_flags = SMB_O_WRONLY; |
74 | else if ((flags & O_ACCMODE) == O_RDWR) | |
75 | posix_flags = SMB_O_RDWR; | |
76 | ||
07b92d0d | 77 | if (flags & O_CREAT) { |
608712fe | 78 | posix_flags |= SMB_O_CREAT; |
07b92d0d SF |
79 | if (flags & O_EXCL) |
80 | posix_flags |= SMB_O_EXCL; | |
81 | } else if (flags & O_EXCL) | |
f96637be JP |
82 | cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n", |
83 | current->comm, current->tgid); | |
07b92d0d | 84 | |
608712fe JL |
85 | if (flags & O_TRUNC) |
86 | posix_flags |= SMB_O_TRUNC; | |
87 | /* be safe and imply O_SYNC for O_DSYNC */ | |
6b2f3d1f | 88 | if (flags & O_DSYNC) |
608712fe | 89 | posix_flags |= SMB_O_SYNC; |
7fc8f4e9 | 90 | if (flags & O_DIRECTORY) |
608712fe | 91 | posix_flags |= SMB_O_DIRECTORY; |
7fc8f4e9 | 92 | if (flags & O_NOFOLLOW) |
608712fe | 93 | posix_flags |= SMB_O_NOFOLLOW; |
7fc8f4e9 | 94 | if (flags & O_DIRECT) |
608712fe | 95 | posix_flags |= SMB_O_DIRECT; |
7fc8f4e9 SF |
96 | |
97 | return posix_flags; | |
1da177e4 LT |
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; | |
55aa2e09 SF |
108 | else if ((flags & O_TRUNC) == O_TRUNC) |
109 | return FILE_OVERWRITE; | |
1da177e4 LT |
110 | else |
111 | return FILE_OPEN; | |
112 | } | |
113 | ||
608712fe JL |
114 | int cifs_posix_open(char *full_path, struct inode **pinode, |
115 | struct super_block *sb, int mode, unsigned int f_flags, | |
6d5786a3 | 116 | __u32 *poplock, __u16 *pnetfid, unsigned int xid) |
608712fe JL |
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; | |
96daf2b0 | 124 | struct cifs_tcon *tcon; |
608712fe | 125 | |
f96637be | 126 | cifs_dbg(FYI, "posix open %s\n", full_path); |
608712fe JL |
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, | |
bc8ebdc4 | 144 | cifs_remap(cifs_sb)); |
608712fe JL |
145 | cifs_put_tlink(tlink); |
146 | ||
147 | if (rc) | |
148 | goto posix_open_ret; | |
149 | ||
150 | if (presp_data->Type == cpu_to_le32(-1)) | |
151 | goto posix_open_ret; /* open ok, caller does qpathinfo */ | |
152 | ||
153 | if (!pinode) | |
154 | goto posix_open_ret; /* caller does not need info */ | |
155 | ||
156 | cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb); | |
157 | ||
158 | /* get new inode and set it up */ | |
159 | if (*pinode == NULL) { | |
160 | cifs_fill_uniqueid(sb, &fattr); | |
161 | *pinode = cifs_iget(sb, &fattr); | |
162 | if (!*pinode) { | |
163 | rc = -ENOMEM; | |
164 | goto posix_open_ret; | |
165 | } | |
166 | } else { | |
211729ab | 167 | cifs_revalidate_mapping(*pinode); |
608712fe JL |
168 | cifs_fattr_to_inode(*pinode, &fattr); |
169 | } | |
170 | ||
171 | posix_open_ret: | |
172 | kfree(presp_data); | |
173 | return rc; | |
174 | } | |
175 | ||
eeb910a6 PS |
176 | static int |
177 | cifs_nt_open(char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb, | |
fb1214e4 PS |
178 | struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock, |
179 | struct cifs_fid *fid, unsigned int xid) | |
eeb910a6 PS |
180 | { |
181 | int rc; | |
fb1214e4 | 182 | int desired_access; |
eeb910a6 | 183 | int disposition; |
3d3ea8e6 | 184 | int create_options = CREATE_NOT_DIR; |
eeb910a6 | 185 | FILE_ALL_INFO *buf; |
b8c32dbb | 186 | struct TCP_Server_Info *server = tcon->ses->server; |
226730b4 | 187 | struct cifs_open_parms oparms; |
eeb910a6 | 188 | |
b8c32dbb | 189 | if (!server->ops->open) |
fb1214e4 PS |
190 | return -ENOSYS; |
191 | ||
192 | desired_access = cifs_convert_flags(f_flags); | |
eeb910a6 PS |
193 | |
194 | /********************************************************************* | |
195 | * open flag mapping table: | |
196 | * | |
197 | * POSIX Flag CIFS Disposition | |
198 | * ---------- ---------------- | |
199 | * O_CREAT FILE_OPEN_IF | |
200 | * O_CREAT | O_EXCL FILE_CREATE | |
201 | * O_CREAT | O_TRUNC FILE_OVERWRITE_IF | |
202 | * O_TRUNC FILE_OVERWRITE | |
203 | * none of the above FILE_OPEN | |
204 | * | |
205 | * Note that there is not a direct match between disposition | |
206 | * FILE_SUPERSEDE (ie create whether or not file exists although | |
207 | * O_CREAT | O_TRUNC is similar but truncates the existing | |
208 | * file rather than creating a new file as FILE_SUPERSEDE does | |
209 | * (which uses the attributes / metadata passed in on open call) | |
210 | *? | |
211 | *? O_SYNC is a reasonable match to CIFS writethrough flag | |
212 | *? and the read write flags match reasonably. O_LARGEFILE | |
213 | *? is irrelevant because largefile support is always used | |
214 | *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY, | |
215 | * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation | |
216 | *********************************************************************/ | |
217 | ||
218 | disposition = cifs_get_disposition(f_flags); | |
219 | ||
220 | /* BB pass O_SYNC flag through on file attributes .. BB */ | |
221 | ||
222 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); | |
223 | if (!buf) | |
224 | return -ENOMEM; | |
225 | ||
3d3ea8e6 SP |
226 | if (backup_cred(cifs_sb)) |
227 | create_options |= CREATE_OPEN_BACKUP_INTENT; | |
228 | ||
1013e760 SF |
229 | /* O_SYNC also has bit for O_DSYNC so following check picks up either */ |
230 | if (f_flags & O_SYNC) | |
231 | create_options |= CREATE_WRITE_THROUGH; | |
232 | ||
233 | if (f_flags & O_DIRECT) | |
234 | create_options |= CREATE_NO_BUFFER; | |
235 | ||
226730b4 PS |
236 | oparms.tcon = tcon; |
237 | oparms.cifs_sb = cifs_sb; | |
238 | oparms.desired_access = desired_access; | |
239 | oparms.create_options = create_options; | |
240 | oparms.disposition = disposition; | |
241 | oparms.path = full_path; | |
242 | oparms.fid = fid; | |
9cbc0b73 | 243 | oparms.reconnect = false; |
226730b4 PS |
244 | |
245 | rc = server->ops->open(xid, &oparms, oplock, buf); | |
eeb910a6 PS |
246 | |
247 | if (rc) | |
248 | goto out; | |
249 | ||
250 | if (tcon->unix_ext) | |
251 | rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb, | |
252 | xid); | |
253 | else | |
254 | rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb, | |
42eacf9e | 255 | xid, fid); |
eeb910a6 | 256 | |
30573a82 PS |
257 | if (rc) { |
258 | server->ops->close(xid, tcon, fid); | |
259 | if (rc == -ESTALE) | |
260 | rc = -EOPENSTALE; | |
261 | } | |
262 | ||
eeb910a6 PS |
263 | out: |
264 | kfree(buf); | |
265 | return rc; | |
266 | } | |
267 | ||
63b7d3a4 PS |
268 | static bool |
269 | cifs_has_mand_locks(struct cifsInodeInfo *cinode) | |
270 | { | |
271 | struct cifs_fid_locks *cur; | |
272 | bool has_locks = false; | |
273 | ||
274 | down_read(&cinode->lock_sem); | |
275 | list_for_each_entry(cur, &cinode->llist, llist) { | |
276 | if (!list_empty(&cur->locks)) { | |
277 | has_locks = true; | |
278 | break; | |
279 | } | |
280 | } | |
281 | up_read(&cinode->lock_sem); | |
282 | return has_locks; | |
283 | } | |
284 | ||
d46b0da7 DW |
285 | void |
286 | cifs_down_write(struct rw_semaphore *sem) | |
287 | { | |
288 | while (!down_write_trylock(sem)) | |
289 | msleep(10); | |
290 | } | |
291 | ||
0a447ca0 RS |
292 | static void cifsFileInfo_put_work(struct work_struct *work); |
293 | ||
15ecb436 | 294 | struct cifsFileInfo * |
fb1214e4 | 295 | cifs_new_fileinfo(struct cifs_fid *fid, struct file *file, |
15ecb436 JL |
296 | struct tcon_link *tlink, __u32 oplock) |
297 | { | |
1f1735cb | 298 | struct dentry *dentry = file_dentry(file); |
2b0143b5 | 299 | struct inode *inode = d_inode(dentry); |
4b4de76e PS |
300 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
301 | struct cifsFileInfo *cfile; | |
f45d3416 | 302 | struct cifs_fid_locks *fdlocks; |
233839b1 | 303 | struct cifs_tcon *tcon = tlink_tcon(tlink); |
63b7d3a4 | 304 | struct TCP_Server_Info *server = tcon->ses->server; |
4b4de76e PS |
305 | |
306 | cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); | |
307 | if (cfile == NULL) | |
308 | return cfile; | |
309 | ||
f45d3416 PS |
310 | fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL); |
311 | if (!fdlocks) { | |
312 | kfree(cfile); | |
313 | return NULL; | |
314 | } | |
315 | ||
316 | INIT_LIST_HEAD(&fdlocks->locks); | |
317 | fdlocks->cfile = cfile; | |
318 | cfile->llist = fdlocks; | |
f45d3416 | 319 | |
4b4de76e | 320 | cfile->count = 1; |
4b4de76e PS |
321 | cfile->pid = current->tgid; |
322 | cfile->uid = current_fsuid(); | |
323 | cfile->dentry = dget(dentry); | |
324 | cfile->f_flags = file->f_flags; | |
325 | cfile->invalidHandle = false; | |
326 | cfile->tlink = cifs_get_tlink(tlink); | |
4b4de76e | 327 | INIT_WORK(&cfile->oplock_break, cifs_oplock_break); |
0a447ca0 | 328 | INIT_WORK(&cfile->put, cifsFileInfo_put_work); |
f45d3416 | 329 | mutex_init(&cfile->fh_mutex); |
3afca265 | 330 | spin_lock_init(&cfile->file_info_lock); |
15ecb436 | 331 | |
24261fc2 MG |
332 | cifs_sb_active(inode->i_sb); |
333 | ||
63b7d3a4 PS |
334 | /* |
335 | * If the server returned a read oplock and we have mandatory brlocks, | |
336 | * set oplock level to None. | |
337 | */ | |
53ef1016 | 338 | if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) { |
f96637be | 339 | cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n"); |
63b7d3a4 PS |
340 | oplock = 0; |
341 | } | |
342 | ||
2281043a PS |
343 | cifs_down_write(&cinode->lock_sem); |
344 | list_add(&fdlocks->llist, &cinode->llist); | |
345 | up_write(&cinode->lock_sem); | |
346 | ||
3afca265 | 347 | spin_lock(&tcon->open_file_lock); |
63b7d3a4 | 348 | if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock) |
233839b1 PS |
349 | oplock = fid->pending_open->oplock; |
350 | list_del(&fid->pending_open->olist); | |
351 | ||
42873b0a | 352 | fid->purge_cache = false; |
63b7d3a4 | 353 | server->ops->set_fid(cfile, fid, oplock); |
233839b1 PS |
354 | |
355 | list_add(&cfile->tlist, &tcon->openFileList); | |
fae8044c | 356 | atomic_inc(&tcon->num_local_opens); |
3afca265 | 357 | |
15ecb436 | 358 | /* if readable file instance put first in list*/ |
487317c9 | 359 | spin_lock(&cinode->open_file_lock); |
15ecb436 | 360 | if (file->f_mode & FMODE_READ) |
4b4de76e | 361 | list_add(&cfile->flist, &cinode->openFileList); |
15ecb436 | 362 | else |
4b4de76e | 363 | list_add_tail(&cfile->flist, &cinode->openFileList); |
487317c9 | 364 | spin_unlock(&cinode->open_file_lock); |
3afca265 | 365 | spin_unlock(&tcon->open_file_lock); |
15ecb436 | 366 | |
42873b0a | 367 | if (fid->purge_cache) |
4f73c7d3 | 368 | cifs_zap_mapping(inode); |
42873b0a | 369 | |
4b4de76e PS |
370 | file->private_data = cfile; |
371 | return cfile; | |
15ecb436 JL |
372 | } |
373 | ||
764a1b1a JL |
374 | struct cifsFileInfo * |
375 | cifsFileInfo_get(struct cifsFileInfo *cifs_file) | |
376 | { | |
3afca265 | 377 | spin_lock(&cifs_file->file_info_lock); |
764a1b1a | 378 | cifsFileInfo_get_locked(cifs_file); |
3afca265 | 379 | spin_unlock(&cifs_file->file_info_lock); |
764a1b1a JL |
380 | return cifs_file; |
381 | } | |
382 | ||
0a447ca0 RS |
383 | static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file) |
384 | { | |
385 | struct inode *inode = d_inode(cifs_file->dentry); | |
386 | struct cifsInodeInfo *cifsi = CIFS_I(inode); | |
387 | struct cifsLockInfo *li, *tmp; | |
388 | struct super_block *sb = inode->i_sb; | |
389 | ||
390 | /* | |
391 | * Delete any outstanding lock records. We'll lose them when the file | |
392 | * is closed anyway. | |
393 | */ | |
394 | cifs_down_write(&cifsi->lock_sem); | |
395 | list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) { | |
396 | list_del(&li->llist); | |
397 | cifs_del_lock_waiters(li); | |
398 | kfree(li); | |
399 | } | |
400 | list_del(&cifs_file->llist->llist); | |
401 | kfree(cifs_file->llist); | |
402 | up_write(&cifsi->lock_sem); | |
403 | ||
404 | cifs_put_tlink(cifs_file->tlink); | |
405 | dput(cifs_file->dentry); | |
406 | cifs_sb_deactive(sb); | |
407 | kfree(cifs_file); | |
408 | } | |
409 | ||
410 | static void cifsFileInfo_put_work(struct work_struct *work) | |
411 | { | |
412 | struct cifsFileInfo *cifs_file = container_of(work, | |
413 | struct cifsFileInfo, put); | |
414 | ||
415 | cifsFileInfo_put_final(cifs_file); | |
416 | } | |
417 | ||
b98749ca AA |
418 | /** |
419 | * cifsFileInfo_put - release a reference of file priv data | |
420 | * | |
421 | * Always potentially wait for oplock handler. See _cifsFileInfo_put(). | |
cdff08e7 | 422 | */ |
b33879aa | 423 | void cifsFileInfo_put(struct cifsFileInfo *cifs_file) |
b98749ca | 424 | { |
0a447ca0 | 425 | _cifsFileInfo_put(cifs_file, true, true); |
b98749ca AA |
426 | } |
427 | ||
428 | /** | |
429 | * _cifsFileInfo_put - release a reference of file priv data | |
430 | * | |
431 | * This may involve closing the filehandle @cifs_file out on the | |
0a447ca0 RS |
432 | * server. Must be called without holding tcon->open_file_lock, |
433 | * cinode->open_file_lock and cifs_file->file_info_lock. | |
b98749ca AA |
434 | * |
435 | * If @wait_for_oplock_handler is true and we are releasing the last | |
436 | * reference, wait for any running oplock break handler of the file | |
437 | * and cancel any pending one. If calling this function from the | |
438 | * oplock break handler, you need to pass false. | |
439 | * | |
440 | */ | |
0a447ca0 RS |
441 | void _cifsFileInfo_put(struct cifsFileInfo *cifs_file, |
442 | bool wait_oplock_handler, bool offload) | |
b33879aa | 443 | { |
2b0143b5 | 444 | struct inode *inode = d_inode(cifs_file->dentry); |
96daf2b0 | 445 | struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink); |
233839b1 | 446 | struct TCP_Server_Info *server = tcon->ses->server; |
e66673e3 | 447 | struct cifsInodeInfo *cifsi = CIFS_I(inode); |
24261fc2 MG |
448 | struct super_block *sb = inode->i_sb; |
449 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); | |
233839b1 PS |
450 | struct cifs_fid fid; |
451 | struct cifs_pending_open open; | |
ca7df8e0 | 452 | bool oplock_break_cancelled; |
cdff08e7 | 453 | |
3afca265 | 454 | spin_lock(&tcon->open_file_lock); |
1a67c415 | 455 | spin_lock(&cifsi->open_file_lock); |
3afca265 | 456 | spin_lock(&cifs_file->file_info_lock); |
5f6dbc9e | 457 | if (--cifs_file->count > 0) { |
3afca265 | 458 | spin_unlock(&cifs_file->file_info_lock); |
1a67c415 | 459 | spin_unlock(&cifsi->open_file_lock); |
3afca265 | 460 | spin_unlock(&tcon->open_file_lock); |
cdff08e7 SF |
461 | return; |
462 | } | |
3afca265 | 463 | spin_unlock(&cifs_file->file_info_lock); |
cdff08e7 | 464 | |
233839b1 PS |
465 | if (server->ops->get_lease_key) |
466 | server->ops->get_lease_key(inode, &fid); | |
467 | ||
468 | /* store open in pending opens to make sure we don't miss lease break */ | |
469 | cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open); | |
470 | ||
cdff08e7 SF |
471 | /* remove it from the lists */ |
472 | list_del(&cifs_file->flist); | |
473 | list_del(&cifs_file->tlist); | |
fae8044c | 474 | atomic_dec(&tcon->num_local_opens); |
cdff08e7 SF |
475 | |
476 | if (list_empty(&cifsi->openFileList)) { | |
f96637be | 477 | cifs_dbg(FYI, "closing last open instance for inode %p\n", |
2b0143b5 | 478 | d_inode(cifs_file->dentry)); |
25364138 PS |
479 | /* |
480 | * In strict cache mode we need invalidate mapping on the last | |
481 | * close because it may cause a error when we open this file | |
482 | * again and get at least level II oplock. | |
483 | */ | |
4f8ba8a0 | 484 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) |
aff8d5ca | 485 | set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags); |
c6723628 | 486 | cifs_set_oplock_level(cifsi, 0); |
cdff08e7 | 487 | } |
3afca265 | 488 | |
1a67c415 | 489 | spin_unlock(&cifsi->open_file_lock); |
3afca265 | 490 | spin_unlock(&tcon->open_file_lock); |
cdff08e7 | 491 | |
b98749ca AA |
492 | oplock_break_cancelled = wait_oplock_handler ? |
493 | cancel_work_sync(&cifs_file->oplock_break) : false; | |
ad635942 | 494 | |
cdff08e7 | 495 | if (!tcon->need_reconnect && !cifs_file->invalidHandle) { |
0ff78a22 | 496 | struct TCP_Server_Info *server = tcon->ses->server; |
6d5786a3 | 497 | unsigned int xid; |
0ff78a22 | 498 | |
6d5786a3 | 499 | xid = get_xid(); |
a159090a SF |
500 | if (server->ops->close_getattr) |
501 | server->ops->close_getattr(xid, tcon, cifs_file); | |
502 | else if (server->ops->close) | |
760ad0ca PS |
503 | server->ops->close(xid, tcon, &cifs_file->fid); |
504 | _free_xid(xid); | |
cdff08e7 SF |
505 | } |
506 | ||
ca7df8e0 SP |
507 | if (oplock_break_cancelled) |
508 | cifs_done_oplock_break(cifsi); | |
509 | ||
233839b1 PS |
510 | cifs_del_pending_open(&open); |
511 | ||
0a447ca0 RS |
512 | if (offload) |
513 | queue_work(fileinfo_put_wq, &cifs_file->put); | |
514 | else | |
515 | cifsFileInfo_put_final(cifs_file); | |
b33879aa JL |
516 | } |
517 | ||
1da177e4 | 518 | int cifs_open(struct inode *inode, struct file *file) |
233839b1 | 519 | |
1da177e4 LT |
520 | { |
521 | int rc = -EACCES; | |
6d5786a3 | 522 | unsigned int xid; |
590a3fe0 | 523 | __u32 oplock; |
1da177e4 | 524 | struct cifs_sb_info *cifs_sb; |
b8c32dbb | 525 | struct TCP_Server_Info *server; |
96daf2b0 | 526 | struct cifs_tcon *tcon; |
7ffec372 | 527 | struct tcon_link *tlink; |
fb1214e4 | 528 | struct cifsFileInfo *cfile = NULL; |
1da177e4 | 529 | char *full_path = NULL; |
7e12eddb | 530 | bool posix_open_ok = false; |
fb1214e4 | 531 | struct cifs_fid fid; |
233839b1 | 532 | struct cifs_pending_open open; |
1da177e4 | 533 | |
6d5786a3 | 534 | xid = get_xid(); |
1da177e4 LT |
535 | |
536 | cifs_sb = CIFS_SB(inode->i_sb); | |
7ffec372 JL |
537 | tlink = cifs_sb_tlink(cifs_sb); |
538 | if (IS_ERR(tlink)) { | |
6d5786a3 | 539 | free_xid(xid); |
7ffec372 JL |
540 | return PTR_ERR(tlink); |
541 | } | |
542 | tcon = tlink_tcon(tlink); | |
b8c32dbb | 543 | server = tcon->ses->server; |
1da177e4 | 544 | |
1f1735cb | 545 | full_path = build_path_from_dentry(file_dentry(file)); |
1da177e4 | 546 | if (full_path == NULL) { |
0f3bc09e | 547 | rc = -ENOMEM; |
232341ba | 548 | goto out; |
1da177e4 LT |
549 | } |
550 | ||
f96637be | 551 | cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n", |
b6b38f70 | 552 | inode, file->f_flags, full_path); |
276a74a4 | 553 | |
787aded6 NJ |
554 | if (file->f_flags & O_DIRECT && |
555 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) { | |
556 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) | |
557 | file->f_op = &cifs_file_direct_nobrl_ops; | |
558 | else | |
559 | file->f_op = &cifs_file_direct_ops; | |
560 | } | |
561 | ||
233839b1 | 562 | if (server->oplocks) |
276a74a4 SF |
563 | oplock = REQ_OPLOCK; |
564 | else | |
565 | oplock = 0; | |
566 | ||
64cc2c63 | 567 | if (!tcon->broken_posix_open && tcon->unix_ext && |
29e20f9c PS |
568 | cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
569 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { | |
276a74a4 | 570 | /* can not refresh inode info since size could be stale */ |
2422f676 | 571 | rc = cifs_posix_open(full_path, &inode, inode->i_sb, |
fa588e0c | 572 | cifs_sb->mnt_file_mode /* ignored */, |
fb1214e4 | 573 | file->f_flags, &oplock, &fid.netfid, xid); |
276a74a4 | 574 | if (rc == 0) { |
f96637be | 575 | cifs_dbg(FYI, "posix open succeeded\n"); |
7e12eddb | 576 | posix_open_ok = true; |
64cc2c63 SF |
577 | } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { |
578 | if (tcon->ses->serverNOS) | |
f96637be JP |
579 | cifs_dbg(VFS, "server %s of type %s returned unexpected error on SMB posix open, disabling posix open support. Check if server update available.\n", |
580 | tcon->ses->serverName, | |
581 | tcon->ses->serverNOS); | |
64cc2c63 | 582 | tcon->broken_posix_open = true; |
276a74a4 SF |
583 | } else if ((rc != -EIO) && (rc != -EREMOTE) && |
584 | (rc != -EOPNOTSUPP)) /* path not found or net err */ | |
585 | goto out; | |
fb1214e4 PS |
586 | /* |
587 | * Else fallthrough to retry open the old way on network i/o | |
588 | * or DFS errors. | |
589 | */ | |
276a74a4 SF |
590 | } |
591 | ||
233839b1 PS |
592 | if (server->ops->get_lease_key) |
593 | server->ops->get_lease_key(inode, &fid); | |
594 | ||
595 | cifs_add_pending_open(&fid, tlink, &open); | |
596 | ||
7e12eddb | 597 | if (!posix_open_ok) { |
b8c32dbb PS |
598 | if (server->ops->get_lease_key) |
599 | server->ops->get_lease_key(inode, &fid); | |
600 | ||
7e12eddb | 601 | rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, |
fb1214e4 | 602 | file->f_flags, &oplock, &fid, xid); |
233839b1 PS |
603 | if (rc) { |
604 | cifs_del_pending_open(&open); | |
7e12eddb | 605 | goto out; |
233839b1 | 606 | } |
7e12eddb | 607 | } |
47c78b7f | 608 | |
fb1214e4 PS |
609 | cfile = cifs_new_fileinfo(&fid, file, tlink, oplock); |
610 | if (cfile == NULL) { | |
b8c32dbb PS |
611 | if (server->ops->close) |
612 | server->ops->close(xid, tcon, &fid); | |
233839b1 | 613 | cifs_del_pending_open(&open); |
1da177e4 LT |
614 | rc = -ENOMEM; |
615 | goto out; | |
616 | } | |
1da177e4 | 617 | |
9451a9a5 SJ |
618 | cifs_fscache_set_inode_cookie(inode, file); |
619 | ||
7e12eddb | 620 | if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) { |
fb1214e4 PS |
621 | /* |
622 | * Time to set mode which we can not set earlier due to | |
623 | * problems creating new read-only files. | |
624 | */ | |
7e12eddb PS |
625 | struct cifs_unix_set_info_args args = { |
626 | .mode = inode->i_mode, | |
49418b2c EB |
627 | .uid = INVALID_UID, /* no change */ |
628 | .gid = INVALID_GID, /* no change */ | |
7e12eddb PS |
629 | .ctime = NO_CHANGE_64, |
630 | .atime = NO_CHANGE_64, | |
631 | .mtime = NO_CHANGE_64, | |
632 | .device = 0, | |
633 | }; | |
fb1214e4 PS |
634 | CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid, |
635 | cfile->pid); | |
1da177e4 LT |
636 | } |
637 | ||
638 | out: | |
1da177e4 | 639 | kfree(full_path); |
6d5786a3 | 640 | free_xid(xid); |
7ffec372 | 641 | cifs_put_tlink(tlink); |
1da177e4 LT |
642 | return rc; |
643 | } | |
644 | ||
f152fd5f PS |
645 | static int cifs_push_posix_locks(struct cifsFileInfo *cfile); |
646 | ||
2ae78ba8 PS |
647 | /* |
648 | * Try to reacquire byte range locks that were released when session | |
f152fd5f | 649 | * to server was lost. |
2ae78ba8 | 650 | */ |
f152fd5f PS |
651 | static int |
652 | cifs_relock_file(struct cifsFileInfo *cfile) | |
1da177e4 | 653 | { |
f152fd5f | 654 | struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); |
2b0143b5 | 655 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
f152fd5f | 656 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
1da177e4 LT |
657 | int rc = 0; |
658 | ||
560d3889 | 659 | down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING); |
f152fd5f | 660 | if (cinode->can_cache_brlcks) { |
689c3db4 PS |
661 | /* can cache locks - no need to relock */ |
662 | up_read(&cinode->lock_sem); | |
f152fd5f PS |
663 | return rc; |
664 | } | |
665 | ||
666 | if (cap_unix(tcon->ses) && | |
667 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && | |
668 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) | |
669 | rc = cifs_push_posix_locks(cfile); | |
670 | else | |
671 | rc = tcon->ses->server->ops->push_mand_locks(cfile); | |
1da177e4 | 672 | |
689c3db4 | 673 | up_read(&cinode->lock_sem); |
1da177e4 LT |
674 | return rc; |
675 | } | |
676 | ||
2ae78ba8 PS |
677 | static int |
678 | cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush) | |
1da177e4 LT |
679 | { |
680 | int rc = -EACCES; | |
6d5786a3 | 681 | unsigned int xid; |
590a3fe0 | 682 | __u32 oplock; |
1da177e4 | 683 | struct cifs_sb_info *cifs_sb; |
96daf2b0 | 684 | struct cifs_tcon *tcon; |
2ae78ba8 PS |
685 | struct TCP_Server_Info *server; |
686 | struct cifsInodeInfo *cinode; | |
fb8c4b14 | 687 | struct inode *inode; |
1da177e4 | 688 | char *full_path = NULL; |
2ae78ba8 | 689 | int desired_access; |
1da177e4 | 690 | int disposition = FILE_OPEN; |
3d3ea8e6 | 691 | int create_options = CREATE_NOT_DIR; |
226730b4 | 692 | struct cifs_open_parms oparms; |
1da177e4 | 693 | |
6d5786a3 | 694 | xid = get_xid(); |
2ae78ba8 PS |
695 | mutex_lock(&cfile->fh_mutex); |
696 | if (!cfile->invalidHandle) { | |
697 | mutex_unlock(&cfile->fh_mutex); | |
0f3bc09e | 698 | rc = 0; |
6d5786a3 | 699 | free_xid(xid); |
0f3bc09e | 700 | return rc; |
1da177e4 LT |
701 | } |
702 | ||
2b0143b5 | 703 | inode = d_inode(cfile->dentry); |
1da177e4 | 704 | cifs_sb = CIFS_SB(inode->i_sb); |
2ae78ba8 PS |
705 | tcon = tlink_tcon(cfile->tlink); |
706 | server = tcon->ses->server; | |
707 | ||
708 | /* | |
709 | * Can not grab rename sem here because various ops, including those | |
710 | * that already have the rename sem can end up causing writepage to get | |
711 | * called and if the server was down that means we end up here, and we | |
712 | * can never tell if the caller already has the rename_sem. | |
713 | */ | |
714 | full_path = build_path_from_dentry(cfile->dentry); | |
1da177e4 | 715 | if (full_path == NULL) { |
3a9f462f | 716 | rc = -ENOMEM; |
2ae78ba8 | 717 | mutex_unlock(&cfile->fh_mutex); |
6d5786a3 | 718 | free_xid(xid); |
3a9f462f | 719 | return rc; |
1da177e4 LT |
720 | } |
721 | ||
f96637be JP |
722 | cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n", |
723 | inode, cfile->f_flags, full_path); | |
1da177e4 | 724 | |
10b9b98e | 725 | if (tcon->ses->server->oplocks) |
1da177e4 LT |
726 | oplock = REQ_OPLOCK; |
727 | else | |
4b18f2a9 | 728 | oplock = 0; |
1da177e4 | 729 | |
29e20f9c | 730 | if (tcon->unix_ext && cap_unix(tcon->ses) && |
7fc8f4e9 | 731 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
29e20f9c | 732 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
608712fe JL |
733 | /* |
734 | * O_CREAT, O_EXCL and O_TRUNC already had their effect on the | |
735 | * original open. Must mask them off for a reopen. | |
736 | */ | |
2ae78ba8 | 737 | unsigned int oflags = cfile->f_flags & |
15886177 | 738 | ~(O_CREAT | O_EXCL | O_TRUNC); |
608712fe | 739 | |
2422f676 | 740 | rc = cifs_posix_open(full_path, NULL, inode->i_sb, |
2ae78ba8 | 741 | cifs_sb->mnt_file_mode /* ignored */, |
9cbc0b73 | 742 | oflags, &oplock, &cfile->fid.netfid, xid); |
7fc8f4e9 | 743 | if (rc == 0) { |
f96637be | 744 | cifs_dbg(FYI, "posix reopen succeeded\n"); |
fe090e4e | 745 | oparms.reconnect = true; |
7fc8f4e9 SF |
746 | goto reopen_success; |
747 | } | |
2ae78ba8 PS |
748 | /* |
749 | * fallthrough to retry open the old way on errors, especially | |
750 | * in the reconnect path it is important to retry hard | |
751 | */ | |
7fc8f4e9 SF |
752 | } |
753 | ||
2ae78ba8 | 754 | desired_access = cifs_convert_flags(cfile->f_flags); |
7fc8f4e9 | 755 | |
3d3ea8e6 SP |
756 | if (backup_cred(cifs_sb)) |
757 | create_options |= CREATE_OPEN_BACKUP_INTENT; | |
758 | ||
ea6ad21a PS |
759 | /* O_SYNC also has bit for O_DSYNC so following check picks up either */ |
760 | if (cfile->f_flags & O_SYNC) | |
761 | create_options |= CREATE_WRITE_THROUGH; | |
762 | ||
763 | if (cfile->f_flags & O_DIRECT) | |
764 | create_options |= CREATE_NO_BUFFER; | |
765 | ||
b8c32dbb | 766 | if (server->ops->get_lease_key) |
9cbc0b73 | 767 | server->ops->get_lease_key(inode, &cfile->fid); |
b8c32dbb | 768 | |
226730b4 PS |
769 | oparms.tcon = tcon; |
770 | oparms.cifs_sb = cifs_sb; | |
771 | oparms.desired_access = desired_access; | |
772 | oparms.create_options = create_options; | |
773 | oparms.disposition = disposition; | |
774 | oparms.path = full_path; | |
9cbc0b73 PS |
775 | oparms.fid = &cfile->fid; |
776 | oparms.reconnect = true; | |
226730b4 | 777 | |
2ae78ba8 PS |
778 | /* |
779 | * Can not refresh inode by passing in file_info buf to be returned by | |
d81b8a40 | 780 | * ops->open and then calling get_inode_info with returned buf since |
2ae78ba8 PS |
781 | * file might have write behind data that needs to be flushed and server |
782 | * version of file size can be stale. If we knew for sure that inode was | |
783 | * not dirty locally we could do this. | |
784 | */ | |
226730b4 | 785 | rc = server->ops->open(xid, &oparms, &oplock, NULL); |
b33fcf1c PS |
786 | if (rc == -ENOENT && oparms.reconnect == false) { |
787 | /* durable handle timeout is expired - open the file again */ | |
788 | rc = server->ops->open(xid, &oparms, &oplock, NULL); | |
789 | /* indicate that we need to relock the file */ | |
790 | oparms.reconnect = true; | |
791 | } | |
792 | ||
1da177e4 | 793 | if (rc) { |
2ae78ba8 | 794 | mutex_unlock(&cfile->fh_mutex); |
f96637be JP |
795 | cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc); |
796 | cifs_dbg(FYI, "oplock: %d\n", oplock); | |
15886177 JL |
797 | goto reopen_error_exit; |
798 | } | |
799 | ||
7fc8f4e9 | 800 | reopen_success: |
2ae78ba8 PS |
801 | cfile->invalidHandle = false; |
802 | mutex_unlock(&cfile->fh_mutex); | |
803 | cinode = CIFS_I(inode); | |
15886177 JL |
804 | |
805 | if (can_flush) { | |
806 | rc = filemap_write_and_wait(inode->i_mapping); | |
9a66396f PS |
807 | if (!is_interrupt_error(rc)) |
808 | mapping_set_error(inode->i_mapping, rc); | |
15886177 | 809 | |
15886177 | 810 | if (tcon->unix_ext) |
2ae78ba8 PS |
811 | rc = cifs_get_inode_info_unix(&inode, full_path, |
812 | inode->i_sb, xid); | |
15886177 | 813 | else |
2ae78ba8 PS |
814 | rc = cifs_get_inode_info(&inode, full_path, NULL, |
815 | inode->i_sb, xid, NULL); | |
816 | } | |
817 | /* | |
818 | * Else we are writing out data to server already and could deadlock if | |
819 | * we tried to flush data, and since we do not know if we have data that | |
820 | * would invalidate the current end of file on the server we can not go | |
821 | * to the server to get the new inode info. | |
822 | */ | |
823 | ||
de740250 PS |
824 | /* |
825 | * If the server returned a read oplock and we have mandatory brlocks, | |
826 | * set oplock level to None. | |
827 | */ | |
828 | if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) { | |
829 | cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n"); | |
830 | oplock = 0; | |
831 | } | |
832 | ||
9cbc0b73 PS |
833 | server->ops->set_fid(cfile, &cfile->fid, oplock); |
834 | if (oparms.reconnect) | |
835 | cifs_relock_file(cfile); | |
15886177 JL |
836 | |
837 | reopen_error_exit: | |
1da177e4 | 838 | kfree(full_path); |
6d5786a3 | 839 | free_xid(xid); |
1da177e4 LT |
840 | return rc; |
841 | } | |
842 | ||
843 | int cifs_close(struct inode *inode, struct file *file) | |
844 | { | |
77970693 | 845 | if (file->private_data != NULL) { |
0a447ca0 | 846 | _cifsFileInfo_put(file->private_data, true, false); |
77970693 JL |
847 | file->private_data = NULL; |
848 | } | |
7ee1af76 | 849 | |
cdff08e7 SF |
850 | /* return code from the ->release op is always ignored */ |
851 | return 0; | |
1da177e4 LT |
852 | } |
853 | ||
52ace1ef SF |
854 | void |
855 | cifs_reopen_persistent_handles(struct cifs_tcon *tcon) | |
856 | { | |
f2cca6a7 | 857 | struct cifsFileInfo *open_file; |
52ace1ef SF |
858 | struct list_head *tmp; |
859 | struct list_head *tmp1; | |
f2cca6a7 PS |
860 | struct list_head tmp_list; |
861 | ||
96a988ff PS |
862 | if (!tcon->use_persistent || !tcon->need_reopen_files) |
863 | return; | |
864 | ||
865 | tcon->need_reopen_files = false; | |
866 | ||
f2cca6a7 PS |
867 | cifs_dbg(FYI, "Reopen persistent handles"); |
868 | INIT_LIST_HEAD(&tmp_list); | |
52ace1ef SF |
869 | |
870 | /* list all files open on tree connection, reopen resilient handles */ | |
871 | spin_lock(&tcon->open_file_lock); | |
f2cca6a7 | 872 | list_for_each(tmp, &tcon->openFileList) { |
52ace1ef | 873 | open_file = list_entry(tmp, struct cifsFileInfo, tlist); |
f2cca6a7 PS |
874 | if (!open_file->invalidHandle) |
875 | continue; | |
876 | cifsFileInfo_get(open_file); | |
877 | list_add_tail(&open_file->rlist, &tmp_list); | |
52ace1ef SF |
878 | } |
879 | spin_unlock(&tcon->open_file_lock); | |
f2cca6a7 PS |
880 | |
881 | list_for_each_safe(tmp, tmp1, &tmp_list) { | |
882 | open_file = list_entry(tmp, struct cifsFileInfo, rlist); | |
96a988ff PS |
883 | if (cifs_reopen_file(open_file, false /* do not flush */)) |
884 | tcon->need_reopen_files = true; | |
f2cca6a7 PS |
885 | list_del_init(&open_file->rlist); |
886 | cifsFileInfo_put(open_file); | |
887 | } | |
52ace1ef SF |
888 | } |
889 | ||
1da177e4 LT |
890 | int cifs_closedir(struct inode *inode, struct file *file) |
891 | { | |
892 | int rc = 0; | |
6d5786a3 | 893 | unsigned int xid; |
4b4de76e | 894 | struct cifsFileInfo *cfile = file->private_data; |
92fc65a7 PS |
895 | struct cifs_tcon *tcon; |
896 | struct TCP_Server_Info *server; | |
897 | char *buf; | |
1da177e4 | 898 | |
f96637be | 899 | cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode); |
1da177e4 | 900 | |
92fc65a7 PS |
901 | if (cfile == NULL) |
902 | return rc; | |
903 | ||
6d5786a3 | 904 | xid = get_xid(); |
92fc65a7 PS |
905 | tcon = tlink_tcon(cfile->tlink); |
906 | server = tcon->ses->server; | |
1da177e4 | 907 | |
f96637be | 908 | cifs_dbg(FYI, "Freeing private data in close dir\n"); |
3afca265 | 909 | spin_lock(&cfile->file_info_lock); |
52755808 | 910 | if (server->ops->dir_needs_close(cfile)) { |
92fc65a7 | 911 | cfile->invalidHandle = true; |
3afca265 | 912 | spin_unlock(&cfile->file_info_lock); |
92fc65a7 PS |
913 | if (server->ops->close_dir) |
914 | rc = server->ops->close_dir(xid, tcon, &cfile->fid); | |
915 | else | |
916 | rc = -ENOSYS; | |
f96637be | 917 | cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc); |
92fc65a7 PS |
918 | /* not much we can do if it fails anyway, ignore rc */ |
919 | rc = 0; | |
920 | } else | |
3afca265 | 921 | spin_unlock(&cfile->file_info_lock); |
92fc65a7 PS |
922 | |
923 | buf = cfile->srch_inf.ntwrk_buf_start; | |
924 | if (buf) { | |
f96637be | 925 | cifs_dbg(FYI, "closedir free smb buf in srch struct\n"); |
92fc65a7 PS |
926 | cfile->srch_inf.ntwrk_buf_start = NULL; |
927 | if (cfile->srch_inf.smallBuf) | |
928 | cifs_small_buf_release(buf); | |
929 | else | |
930 | cifs_buf_release(buf); | |
1da177e4 | 931 | } |
92fc65a7 PS |
932 | |
933 | cifs_put_tlink(cfile->tlink); | |
934 | kfree(file->private_data); | |
935 | file->private_data = NULL; | |
1da177e4 | 936 | /* BB can we lock the filestruct while this is going on? */ |
6d5786a3 | 937 | free_xid(xid); |
1da177e4 LT |
938 | return rc; |
939 | } | |
940 | ||
85160e03 | 941 | static struct cifsLockInfo * |
9645759c | 942 | cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags) |
7ee1af76 | 943 | { |
a88b4707 | 944 | struct cifsLockInfo *lock = |
fb8c4b14 | 945 | kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL); |
a88b4707 PS |
946 | if (!lock) |
947 | return lock; | |
948 | lock->offset = offset; | |
949 | lock->length = length; | |
950 | lock->type = type; | |
a88b4707 | 951 | lock->pid = current->tgid; |
9645759c | 952 | lock->flags = flags; |
a88b4707 PS |
953 | INIT_LIST_HEAD(&lock->blist); |
954 | init_waitqueue_head(&lock->block_q); | |
955 | return lock; | |
85160e03 PS |
956 | } |
957 | ||
f7ba7fe6 | 958 | void |
85160e03 PS |
959 | cifs_del_lock_waiters(struct cifsLockInfo *lock) |
960 | { | |
961 | struct cifsLockInfo *li, *tmp; | |
962 | list_for_each_entry_safe(li, tmp, &lock->blist, blist) { | |
963 | list_del_init(&li->blist); | |
964 | wake_up(&li->block_q); | |
965 | } | |
966 | } | |
967 | ||
081c0414 PS |
968 | #define CIFS_LOCK_OP 0 |
969 | #define CIFS_READ_OP 1 | |
970 | #define CIFS_WRITE_OP 2 | |
971 | ||
972 | /* @rw_check : 0 - no op, 1 - read, 2 - write */ | |
85160e03 | 973 | static bool |
f45d3416 | 974 | cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset, |
9645759c RS |
975 | __u64 length, __u8 type, __u16 flags, |
976 | struct cifsFileInfo *cfile, | |
081c0414 | 977 | struct cifsLockInfo **conf_lock, int rw_check) |
85160e03 | 978 | { |
fbd35aca | 979 | struct cifsLockInfo *li; |
f45d3416 | 980 | struct cifsFileInfo *cur_cfile = fdlocks->cfile; |
106dc538 | 981 | struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; |
85160e03 | 982 | |
f45d3416 | 983 | list_for_each_entry(li, &fdlocks->locks, llist) { |
85160e03 PS |
984 | if (offset + length <= li->offset || |
985 | offset >= li->offset + li->length) | |
986 | continue; | |
081c0414 PS |
987 | if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid && |
988 | server->ops->compare_fids(cfile, cur_cfile)) { | |
989 | /* shared lock prevents write op through the same fid */ | |
990 | if (!(li->type & server->vals->shared_lock_type) || | |
991 | rw_check != CIFS_WRITE_OP) | |
992 | continue; | |
993 | } | |
f45d3416 PS |
994 | if ((type & server->vals->shared_lock_type) && |
995 | ((server->ops->compare_fids(cfile, cur_cfile) && | |
996 | current->tgid == li->pid) || type == li->type)) | |
85160e03 | 997 | continue; |
9645759c RS |
998 | if (rw_check == CIFS_LOCK_OP && |
999 | (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) && | |
1000 | server->ops->compare_fids(cfile, cur_cfile)) | |
1001 | continue; | |
579f9053 PS |
1002 | if (conf_lock) |
1003 | *conf_lock = li; | |
f45d3416 | 1004 | return true; |
85160e03 PS |
1005 | } |
1006 | return false; | |
1007 | } | |
1008 | ||
579f9053 | 1009 | bool |
55157dfb | 1010 | cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length, |
9645759c RS |
1011 | __u8 type, __u16 flags, |
1012 | struct cifsLockInfo **conf_lock, int rw_check) | |
161ebf9f | 1013 | { |
fbd35aca | 1014 | bool rc = false; |
f45d3416 | 1015 | struct cifs_fid_locks *cur; |
2b0143b5 | 1016 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
fbd35aca | 1017 | |
f45d3416 PS |
1018 | list_for_each_entry(cur, &cinode->llist, llist) { |
1019 | rc = cifs_find_fid_lock_conflict(cur, offset, length, type, | |
9645759c RS |
1020 | flags, cfile, conf_lock, |
1021 | rw_check); | |
fbd35aca PS |
1022 | if (rc) |
1023 | break; | |
1024 | } | |
fbd35aca PS |
1025 | |
1026 | return rc; | |
161ebf9f PS |
1027 | } |
1028 | ||
9a5101c8 PS |
1029 | /* |
1030 | * Check if there is another lock that prevents us to set the lock (mandatory | |
1031 | * style). If such a lock exists, update the flock structure with its | |
1032 | * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks | |
1033 | * or leave it the same if we can't. Returns 0 if we don't need to request to | |
1034 | * the server or 1 otherwise. | |
1035 | */ | |
85160e03 | 1036 | static int |
fbd35aca PS |
1037 | cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length, |
1038 | __u8 type, struct file_lock *flock) | |
85160e03 PS |
1039 | { |
1040 | int rc = 0; | |
1041 | struct cifsLockInfo *conf_lock; | |
2b0143b5 | 1042 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
106dc538 | 1043 | struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; |
85160e03 PS |
1044 | bool exist; |
1045 | ||
1b4b55a1 | 1046 | down_read(&cinode->lock_sem); |
85160e03 | 1047 | |
55157dfb | 1048 | exist = cifs_find_lock_conflict(cfile, offset, length, type, |
9645759c RS |
1049 | flock->fl_flags, &conf_lock, |
1050 | CIFS_LOCK_OP); | |
85160e03 PS |
1051 | if (exist) { |
1052 | flock->fl_start = conf_lock->offset; | |
1053 | flock->fl_end = conf_lock->offset + conf_lock->length - 1; | |
1054 | flock->fl_pid = conf_lock->pid; | |
106dc538 | 1055 | if (conf_lock->type & server->vals->shared_lock_type) |
85160e03 PS |
1056 | flock->fl_type = F_RDLCK; |
1057 | else | |
1058 | flock->fl_type = F_WRLCK; | |
1059 | } else if (!cinode->can_cache_brlcks) | |
1060 | rc = 1; | |
1061 | else | |
1062 | flock->fl_type = F_UNLCK; | |
1063 | ||
1b4b55a1 | 1064 | up_read(&cinode->lock_sem); |
85160e03 PS |
1065 | return rc; |
1066 | } | |
1067 | ||
161ebf9f | 1068 | static void |
fbd35aca | 1069 | cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock) |
85160e03 | 1070 | { |
2b0143b5 | 1071 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
d46b0da7 | 1072 | cifs_down_write(&cinode->lock_sem); |
f45d3416 | 1073 | list_add_tail(&lock->llist, &cfile->llist->locks); |
1b4b55a1 | 1074 | up_write(&cinode->lock_sem); |
7ee1af76 JA |
1075 | } |
1076 | ||
9a5101c8 PS |
1077 | /* |
1078 | * Set the byte-range lock (mandatory style). Returns: | |
1079 | * 1) 0, if we set the lock and don't need to request to the server; | |
1080 | * 2) 1, if no locks prevent us but we need to request to the server; | |
413d6100 | 1081 | * 3) -EACCES, if there is a lock that prevents us and wait is false. |
9a5101c8 | 1082 | */ |
85160e03 | 1083 | static int |
fbd35aca | 1084 | cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock, |
161ebf9f | 1085 | bool wait) |
85160e03 | 1086 | { |
161ebf9f | 1087 | struct cifsLockInfo *conf_lock; |
2b0143b5 | 1088 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
85160e03 PS |
1089 | bool exist; |
1090 | int rc = 0; | |
1091 | ||
85160e03 PS |
1092 | try_again: |
1093 | exist = false; | |
d46b0da7 | 1094 | cifs_down_write(&cinode->lock_sem); |
85160e03 | 1095 | |
55157dfb | 1096 | exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length, |
9645759c RS |
1097 | lock->type, lock->flags, &conf_lock, |
1098 | CIFS_LOCK_OP); | |
85160e03 | 1099 | if (!exist && cinode->can_cache_brlcks) { |
f45d3416 | 1100 | list_add_tail(&lock->llist, &cfile->llist->locks); |
1b4b55a1 | 1101 | up_write(&cinode->lock_sem); |
85160e03 PS |
1102 | return rc; |
1103 | } | |
1104 | ||
1105 | if (!exist) | |
1106 | rc = 1; | |
1107 | else if (!wait) | |
1108 | rc = -EACCES; | |
1109 | else { | |
1110 | list_add_tail(&lock->blist, &conf_lock->blist); | |
1b4b55a1 | 1111 | up_write(&cinode->lock_sem); |
85160e03 PS |
1112 | rc = wait_event_interruptible(lock->block_q, |
1113 | (lock->blist.prev == &lock->blist) && | |
1114 | (lock->blist.next == &lock->blist)); | |
1115 | if (!rc) | |
1116 | goto try_again; | |
d46b0da7 | 1117 | cifs_down_write(&cinode->lock_sem); |
a88b4707 | 1118 | list_del_init(&lock->blist); |
85160e03 PS |
1119 | } |
1120 | ||
1b4b55a1 | 1121 | up_write(&cinode->lock_sem); |
85160e03 PS |
1122 | return rc; |
1123 | } | |
1124 | ||
9a5101c8 PS |
1125 | /* |
1126 | * Check if there is another lock that prevents us to set the lock (posix | |
1127 | * style). If such a lock exists, update the flock structure with its | |
1128 | * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks | |
1129 | * or leave it the same if we can't. Returns 0 if we don't need to request to | |
1130 | * the server or 1 otherwise. | |
1131 | */ | |
85160e03 | 1132 | static int |
4f6bcec9 PS |
1133 | cifs_posix_lock_test(struct file *file, struct file_lock *flock) |
1134 | { | |
1135 | int rc = 0; | |
496ad9aa | 1136 | struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); |
4f6bcec9 PS |
1137 | unsigned char saved_type = flock->fl_type; |
1138 | ||
50792760 PS |
1139 | if ((flock->fl_flags & FL_POSIX) == 0) |
1140 | return 1; | |
1141 | ||
1b4b55a1 | 1142 | down_read(&cinode->lock_sem); |
4f6bcec9 PS |
1143 | posix_test_lock(file, flock); |
1144 | ||
1145 | if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) { | |
1146 | flock->fl_type = saved_type; | |
1147 | rc = 1; | |
1148 | } | |
1149 | ||
1b4b55a1 | 1150 | up_read(&cinode->lock_sem); |
4f6bcec9 PS |
1151 | return rc; |
1152 | } | |
1153 | ||
9a5101c8 PS |
1154 | /* |
1155 | * Set the byte-range lock (posix style). Returns: | |
1156 | * 1) 0, if we set the lock and don't need to request to the server; | |
1157 | * 2) 1, if we need to request to the server; | |
1158 | * 3) <0, if the error occurs while setting the lock. | |
1159 | */ | |
4f6bcec9 PS |
1160 | static int |
1161 | cifs_posix_lock_set(struct file *file, struct file_lock *flock) | |
1162 | { | |
496ad9aa | 1163 | struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); |
50792760 PS |
1164 | int rc = 1; |
1165 | ||
1166 | if ((flock->fl_flags & FL_POSIX) == 0) | |
1167 | return rc; | |
4f6bcec9 | 1168 | |
66189be7 | 1169 | try_again: |
d46b0da7 | 1170 | cifs_down_write(&cinode->lock_sem); |
4f6bcec9 | 1171 | if (!cinode->can_cache_brlcks) { |
1b4b55a1 | 1172 | up_write(&cinode->lock_sem); |
50792760 | 1173 | return rc; |
4f6bcec9 | 1174 | } |
66189be7 PS |
1175 | |
1176 | rc = posix_lock_file(file, flock, NULL); | |
1b4b55a1 | 1177 | up_write(&cinode->lock_sem); |
66189be7 | 1178 | if (rc == FILE_LOCK_DEFERRED) { |
0e5b6134 LT |
1179 | rc = wait_event_interruptible(flock->fl_wait, |
1180 | list_empty(&flock->fl_blocked_member)); | |
66189be7 PS |
1181 | if (!rc) |
1182 | goto try_again; | |
cb03f94f | 1183 | locks_delete_block(flock); |
66189be7 | 1184 | } |
9ebb389d | 1185 | return rc; |
4f6bcec9 PS |
1186 | } |
1187 | ||
d39a4f71 | 1188 | int |
4f6bcec9 | 1189 | cifs_push_mandatory_locks(struct cifsFileInfo *cfile) |
85160e03 | 1190 | { |
6d5786a3 PS |
1191 | unsigned int xid; |
1192 | int rc = 0, stored_rc; | |
85160e03 PS |
1193 | struct cifsLockInfo *li, *tmp; |
1194 | struct cifs_tcon *tcon; | |
0013fb4c | 1195 | unsigned int num, max_num, max_buf; |
32b9aaf1 | 1196 | LOCKING_ANDX_RANGE *buf, *cur; |
4d61eda8 CIK |
1197 | static const int types[] = { |
1198 | LOCKING_ANDX_LARGE_FILES, | |
1199 | LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES | |
1200 | }; | |
32b9aaf1 | 1201 | int i; |
85160e03 | 1202 | |
6d5786a3 | 1203 | xid = get_xid(); |
85160e03 PS |
1204 | tcon = tlink_tcon(cfile->tlink); |
1205 | ||
0013fb4c PS |
1206 | /* |
1207 | * Accessing maxBuf is racy with cifs_reconnect - need to store value | |
b9a74cde | 1208 | * and check it before using. |
0013fb4c PS |
1209 | */ |
1210 | max_buf = tcon->ses->server->maxBuf; | |
b9a74cde | 1211 | if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) { |
6d5786a3 | 1212 | free_xid(xid); |
0013fb4c PS |
1213 | return -EINVAL; |
1214 | } | |
1215 | ||
92a8109e RL |
1216 | BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) > |
1217 | PAGE_SIZE); | |
1218 | max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr), | |
1219 | PAGE_SIZE); | |
0013fb4c PS |
1220 | max_num = (max_buf - sizeof(struct smb_hdr)) / |
1221 | sizeof(LOCKING_ANDX_RANGE); | |
4b99d39b | 1222 | buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL); |
32b9aaf1 | 1223 | if (!buf) { |
6d5786a3 | 1224 | free_xid(xid); |
e2f2886a | 1225 | return -ENOMEM; |
32b9aaf1 PS |
1226 | } |
1227 | ||
1228 | for (i = 0; i < 2; i++) { | |
1229 | cur = buf; | |
1230 | num = 0; | |
f45d3416 | 1231 | list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { |
32b9aaf1 PS |
1232 | if (li->type != types[i]) |
1233 | continue; | |
1234 | cur->Pid = cpu_to_le16(li->pid); | |
1235 | cur->LengthLow = cpu_to_le32((u32)li->length); | |
1236 | cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); | |
1237 | cur->OffsetLow = cpu_to_le32((u32)li->offset); | |
1238 | cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); | |
1239 | if (++num == max_num) { | |
4b4de76e PS |
1240 | stored_rc = cifs_lockv(xid, tcon, |
1241 | cfile->fid.netfid, | |
04a6aa8a PS |
1242 | (__u8)li->type, 0, num, |
1243 | buf); | |
32b9aaf1 PS |
1244 | if (stored_rc) |
1245 | rc = stored_rc; | |
1246 | cur = buf; | |
1247 | num = 0; | |
1248 | } else | |
1249 | cur++; | |
1250 | } | |
1251 | ||
1252 | if (num) { | |
4b4de76e | 1253 | stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, |
04a6aa8a | 1254 | (__u8)types[i], 0, num, buf); |
32b9aaf1 PS |
1255 | if (stored_rc) |
1256 | rc = stored_rc; | |
1257 | } | |
85160e03 PS |
1258 | } |
1259 | ||
32b9aaf1 | 1260 | kfree(buf); |
6d5786a3 | 1261 | free_xid(xid); |
85160e03 PS |
1262 | return rc; |
1263 | } | |
1264 | ||
3d22462a JL |
1265 | static __u32 |
1266 | hash_lockowner(fl_owner_t owner) | |
1267 | { | |
1268 | return cifs_lock_secret ^ hash32_ptr((const void *)owner); | |
1269 | } | |
1270 | ||
d5751469 PS |
1271 | struct lock_to_push { |
1272 | struct list_head llist; | |
1273 | __u64 offset; | |
1274 | __u64 length; | |
1275 | __u32 pid; | |
1276 | __u16 netfid; | |
1277 | __u8 type; | |
1278 | }; | |
1279 | ||
4f6bcec9 | 1280 | static int |
b8db928b | 1281 | cifs_push_posix_locks(struct cifsFileInfo *cfile) |
4f6bcec9 | 1282 | { |
2b0143b5 | 1283 | struct inode *inode = d_inode(cfile->dentry); |
4f6bcec9 | 1284 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
bd61e0a9 JL |
1285 | struct file_lock *flock; |
1286 | struct file_lock_context *flctx = inode->i_flctx; | |
e084c1bd | 1287 | unsigned int count = 0, i; |
4f6bcec9 | 1288 | int rc = 0, xid, type; |
d5751469 PS |
1289 | struct list_head locks_to_send, *el; |
1290 | struct lock_to_push *lck, *tmp; | |
4f6bcec9 | 1291 | __u64 length; |
4f6bcec9 | 1292 | |
6d5786a3 | 1293 | xid = get_xid(); |
4f6bcec9 | 1294 | |
bd61e0a9 JL |
1295 | if (!flctx) |
1296 | goto out; | |
d5751469 | 1297 | |
e084c1bd JL |
1298 | spin_lock(&flctx->flc_lock); |
1299 | list_for_each(el, &flctx->flc_posix) { | |
1300 | count++; | |
1301 | } | |
1302 | spin_unlock(&flctx->flc_lock); | |
1303 | ||
4f6bcec9 PS |
1304 | INIT_LIST_HEAD(&locks_to_send); |
1305 | ||
d5751469 | 1306 | /* |
e084c1bd JL |
1307 | * Allocating count locks is enough because no FL_POSIX locks can be |
1308 | * added to the list while we are holding cinode->lock_sem that | |
ce85852b | 1309 | * protects locking operations of this inode. |
d5751469 | 1310 | */ |
e084c1bd | 1311 | for (i = 0; i < count; i++) { |
d5751469 PS |
1312 | lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL); |
1313 | if (!lck) { | |
1314 | rc = -ENOMEM; | |
1315 | goto err_out; | |
1316 | } | |
1317 | list_add_tail(&lck->llist, &locks_to_send); | |
1318 | } | |
1319 | ||
d5751469 | 1320 | el = locks_to_send.next; |
6109c850 | 1321 | spin_lock(&flctx->flc_lock); |
bd61e0a9 | 1322 | list_for_each_entry(flock, &flctx->flc_posix, fl_list) { |
d5751469 | 1323 | if (el == &locks_to_send) { |
ce85852b PS |
1324 | /* |
1325 | * The list ended. We don't have enough allocated | |
1326 | * structures - something is really wrong. | |
1327 | */ | |
f96637be | 1328 | cifs_dbg(VFS, "Can't push all brlocks!\n"); |
d5751469 PS |
1329 | break; |
1330 | } | |
4f6bcec9 PS |
1331 | length = 1 + flock->fl_end - flock->fl_start; |
1332 | if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK) | |
1333 | type = CIFS_RDLCK; | |
1334 | else | |
1335 | type = CIFS_WRLCK; | |
d5751469 | 1336 | lck = list_entry(el, struct lock_to_push, llist); |
3d22462a | 1337 | lck->pid = hash_lockowner(flock->fl_owner); |
4b4de76e | 1338 | lck->netfid = cfile->fid.netfid; |
d5751469 PS |
1339 | lck->length = length; |
1340 | lck->type = type; | |
1341 | lck->offset = flock->fl_start; | |
4f6bcec9 | 1342 | } |
6109c850 | 1343 | spin_unlock(&flctx->flc_lock); |
4f6bcec9 PS |
1344 | |
1345 | list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { | |
4f6bcec9 PS |
1346 | int stored_rc; |
1347 | ||
4f6bcec9 | 1348 | stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid, |
c5fd363d | 1349 | lck->offset, lck->length, NULL, |
4f6bcec9 PS |
1350 | lck->type, 0); |
1351 | if (stored_rc) | |
1352 | rc = stored_rc; | |
1353 | list_del(&lck->llist); | |
1354 | kfree(lck); | |
1355 | } | |
1356 | ||
d5751469 | 1357 | out: |
6d5786a3 | 1358 | free_xid(xid); |
4f6bcec9 | 1359 | return rc; |
d5751469 PS |
1360 | err_out: |
1361 | list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { | |
1362 | list_del(&lck->llist); | |
1363 | kfree(lck); | |
1364 | } | |
1365 | goto out; | |
4f6bcec9 PS |
1366 | } |
1367 | ||
9ec3c882 | 1368 | static int |
b8db928b | 1369 | cifs_push_locks(struct cifsFileInfo *cfile) |
9ec3c882 | 1370 | { |
b8db928b | 1371 | struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); |
2b0143b5 | 1372 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
b8db928b | 1373 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
9ec3c882 PS |
1374 | int rc = 0; |
1375 | ||
1376 | /* we are going to update can_cache_brlcks here - need a write access */ | |
d46b0da7 | 1377 | cifs_down_write(&cinode->lock_sem); |
9ec3c882 PS |
1378 | if (!cinode->can_cache_brlcks) { |
1379 | up_write(&cinode->lock_sem); | |
1380 | return rc; | |
1381 | } | |
4f6bcec9 | 1382 | |
29e20f9c | 1383 | if (cap_unix(tcon->ses) && |
4f6bcec9 PS |
1384 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && |
1385 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) | |
b8db928b PS |
1386 | rc = cifs_push_posix_locks(cfile); |
1387 | else | |
1388 | rc = tcon->ses->server->ops->push_mand_locks(cfile); | |
4f6bcec9 | 1389 | |
b8db928b PS |
1390 | cinode->can_cache_brlcks = false; |
1391 | up_write(&cinode->lock_sem); | |
1392 | return rc; | |
4f6bcec9 PS |
1393 | } |
1394 | ||
03776f45 | 1395 | static void |
04a6aa8a | 1396 | cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock, |
106dc538 | 1397 | bool *wait_flag, struct TCP_Server_Info *server) |
1da177e4 | 1398 | { |
03776f45 | 1399 | if (flock->fl_flags & FL_POSIX) |
f96637be | 1400 | cifs_dbg(FYI, "Posix\n"); |
03776f45 | 1401 | if (flock->fl_flags & FL_FLOCK) |
f96637be | 1402 | cifs_dbg(FYI, "Flock\n"); |
03776f45 | 1403 | if (flock->fl_flags & FL_SLEEP) { |
f96637be | 1404 | cifs_dbg(FYI, "Blocking lock\n"); |
03776f45 | 1405 | *wait_flag = true; |
1da177e4 | 1406 | } |
03776f45 | 1407 | if (flock->fl_flags & FL_ACCESS) |
f96637be | 1408 | cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n"); |
03776f45 | 1409 | if (flock->fl_flags & FL_LEASE) |
f96637be | 1410 | cifs_dbg(FYI, "Lease on file - not implemented yet\n"); |
03776f45 | 1411 | if (flock->fl_flags & |
3d6d854a | 1412 | (~(FL_POSIX | FL_FLOCK | FL_SLEEP | |
9645759c | 1413 | FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK))) |
f96637be | 1414 | cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags); |
1da177e4 | 1415 | |
106dc538 | 1416 | *type = server->vals->large_lock_type; |
03776f45 | 1417 | if (flock->fl_type == F_WRLCK) { |
f96637be | 1418 | cifs_dbg(FYI, "F_WRLCK\n"); |
106dc538 | 1419 | *type |= server->vals->exclusive_lock_type; |
03776f45 PS |
1420 | *lock = 1; |
1421 | } else if (flock->fl_type == F_UNLCK) { | |
f96637be | 1422 | cifs_dbg(FYI, "F_UNLCK\n"); |
106dc538 | 1423 | *type |= server->vals->unlock_lock_type; |
03776f45 PS |
1424 | *unlock = 1; |
1425 | /* Check if unlock includes more than one lock range */ | |
1426 | } else if (flock->fl_type == F_RDLCK) { | |
f96637be | 1427 | cifs_dbg(FYI, "F_RDLCK\n"); |
106dc538 | 1428 | *type |= server->vals->shared_lock_type; |
03776f45 PS |
1429 | *lock = 1; |
1430 | } else if (flock->fl_type == F_EXLCK) { | |
f96637be | 1431 | cifs_dbg(FYI, "F_EXLCK\n"); |
106dc538 | 1432 | *type |= server->vals->exclusive_lock_type; |
03776f45 PS |
1433 | *lock = 1; |
1434 | } else if (flock->fl_type == F_SHLCK) { | |
f96637be | 1435 | cifs_dbg(FYI, "F_SHLCK\n"); |
106dc538 | 1436 | *type |= server->vals->shared_lock_type; |
03776f45 | 1437 | *lock = 1; |
1da177e4 | 1438 | } else |
f96637be | 1439 | cifs_dbg(FYI, "Unknown type of lock\n"); |
03776f45 | 1440 | } |
1da177e4 | 1441 | |
03776f45 | 1442 | static int |
04a6aa8a | 1443 | cifs_getlk(struct file *file, struct file_lock *flock, __u32 type, |
6d5786a3 | 1444 | bool wait_flag, bool posix_lck, unsigned int xid) |
03776f45 PS |
1445 | { |
1446 | int rc = 0; | |
1447 | __u64 length = 1 + flock->fl_end - flock->fl_start; | |
4f6bcec9 PS |
1448 | struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; |
1449 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); | |
106dc538 | 1450 | struct TCP_Server_Info *server = tcon->ses->server; |
4b4de76e | 1451 | __u16 netfid = cfile->fid.netfid; |
f05337c6 | 1452 | |
03776f45 PS |
1453 | if (posix_lck) { |
1454 | int posix_lock_type; | |
4f6bcec9 PS |
1455 | |
1456 | rc = cifs_posix_lock_test(file, flock); | |
1457 | if (!rc) | |
1458 | return rc; | |
1459 | ||
106dc538 | 1460 | if (type & server->vals->shared_lock_type) |
03776f45 PS |
1461 | posix_lock_type = CIFS_RDLCK; |
1462 | else | |
1463 | posix_lock_type = CIFS_WRLCK; | |
3d22462a JL |
1464 | rc = CIFSSMBPosixLock(xid, tcon, netfid, |
1465 | hash_lockowner(flock->fl_owner), | |
c5fd363d | 1466 | flock->fl_start, length, flock, |
4f6bcec9 | 1467 | posix_lock_type, wait_flag); |
03776f45 PS |
1468 | return rc; |
1469 | } | |
1da177e4 | 1470 | |
fbd35aca | 1471 | rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock); |
85160e03 PS |
1472 | if (!rc) |
1473 | return rc; | |
1474 | ||
03776f45 | 1475 | /* BB we could chain these into one lock request BB */ |
d39a4f71 PS |
1476 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type, |
1477 | 1, 0, false); | |
03776f45 | 1478 | if (rc == 0) { |
d39a4f71 PS |
1479 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, |
1480 | type, 0, 1, false); | |
03776f45 PS |
1481 | flock->fl_type = F_UNLCK; |
1482 | if (rc != 0) | |
f96637be JP |
1483 | cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n", |
1484 | rc); | |
a88b4707 | 1485 | return 0; |
1da177e4 | 1486 | } |
7ee1af76 | 1487 | |
106dc538 | 1488 | if (type & server->vals->shared_lock_type) { |
03776f45 | 1489 | flock->fl_type = F_WRLCK; |
a88b4707 | 1490 | return 0; |
7ee1af76 JA |
1491 | } |
1492 | ||
d39a4f71 PS |
1493 | type &= ~server->vals->exclusive_lock_type; |
1494 | ||
1495 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, | |
1496 | type | server->vals->shared_lock_type, | |
1497 | 1, 0, false); | |
03776f45 | 1498 | if (rc == 0) { |
d39a4f71 PS |
1499 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, |
1500 | type | server->vals->shared_lock_type, 0, 1, false); | |
03776f45 PS |
1501 | flock->fl_type = F_RDLCK; |
1502 | if (rc != 0) | |
f96637be JP |
1503 | cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n", |
1504 | rc); | |
03776f45 PS |
1505 | } else |
1506 | flock->fl_type = F_WRLCK; | |
1507 | ||
a88b4707 | 1508 | return 0; |
03776f45 PS |
1509 | } |
1510 | ||
f7ba7fe6 | 1511 | void |
9ee305b7 PS |
1512 | cifs_move_llist(struct list_head *source, struct list_head *dest) |
1513 | { | |
1514 | struct list_head *li, *tmp; | |
1515 | list_for_each_safe(li, tmp, source) | |
1516 | list_move(li, dest); | |
1517 | } | |
1518 | ||
f7ba7fe6 | 1519 | void |
9ee305b7 PS |
1520 | cifs_free_llist(struct list_head *llist) |
1521 | { | |
1522 | struct cifsLockInfo *li, *tmp; | |
1523 | list_for_each_entry_safe(li, tmp, llist, llist) { | |
1524 | cifs_del_lock_waiters(li); | |
1525 | list_del(&li->llist); | |
1526 | kfree(li); | |
1527 | } | |
1528 | } | |
1529 | ||
d39a4f71 | 1530 | int |
6d5786a3 PS |
1531 | cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock, |
1532 | unsigned int xid) | |
9ee305b7 PS |
1533 | { |
1534 | int rc = 0, stored_rc; | |
4d61eda8 CIK |
1535 | static const int types[] = { |
1536 | LOCKING_ANDX_LARGE_FILES, | |
1537 | LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES | |
1538 | }; | |
9ee305b7 | 1539 | unsigned int i; |
0013fb4c | 1540 | unsigned int max_num, num, max_buf; |
9ee305b7 PS |
1541 | LOCKING_ANDX_RANGE *buf, *cur; |
1542 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); | |
2b0143b5 | 1543 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
9ee305b7 PS |
1544 | struct cifsLockInfo *li, *tmp; |
1545 | __u64 length = 1 + flock->fl_end - flock->fl_start; | |
1546 | struct list_head tmp_llist; | |
1547 | ||
1548 | INIT_LIST_HEAD(&tmp_llist); | |
1549 | ||
0013fb4c PS |
1550 | /* |
1551 | * Accessing maxBuf is racy with cifs_reconnect - need to store value | |
b9a74cde | 1552 | * and check it before using. |
0013fb4c PS |
1553 | */ |
1554 | max_buf = tcon->ses->server->maxBuf; | |
b9a74cde | 1555 | if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) |
0013fb4c PS |
1556 | return -EINVAL; |
1557 | ||
92a8109e RL |
1558 | BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) > |
1559 | PAGE_SIZE); | |
1560 | max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr), | |
1561 | PAGE_SIZE); | |
0013fb4c PS |
1562 | max_num = (max_buf - sizeof(struct smb_hdr)) / |
1563 | sizeof(LOCKING_ANDX_RANGE); | |
4b99d39b | 1564 | buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL); |
9ee305b7 PS |
1565 | if (!buf) |
1566 | return -ENOMEM; | |
1567 | ||
d46b0da7 | 1568 | cifs_down_write(&cinode->lock_sem); |
9ee305b7 PS |
1569 | for (i = 0; i < 2; i++) { |
1570 | cur = buf; | |
1571 | num = 0; | |
f45d3416 | 1572 | list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { |
9ee305b7 PS |
1573 | if (flock->fl_start > li->offset || |
1574 | (flock->fl_start + length) < | |
1575 | (li->offset + li->length)) | |
1576 | continue; | |
1577 | if (current->tgid != li->pid) | |
1578 | continue; | |
9ee305b7 PS |
1579 | if (types[i] != li->type) |
1580 | continue; | |
ea319d57 | 1581 | if (cinode->can_cache_brlcks) { |
9ee305b7 PS |
1582 | /* |
1583 | * We can cache brlock requests - simply remove | |
fbd35aca | 1584 | * a lock from the file's list. |
9ee305b7 PS |
1585 | */ |
1586 | list_del(&li->llist); | |
1587 | cifs_del_lock_waiters(li); | |
1588 | kfree(li); | |
ea319d57 | 1589 | continue; |
9ee305b7 | 1590 | } |
ea319d57 PS |
1591 | cur->Pid = cpu_to_le16(li->pid); |
1592 | cur->LengthLow = cpu_to_le32((u32)li->length); | |
1593 | cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); | |
1594 | cur->OffsetLow = cpu_to_le32((u32)li->offset); | |
1595 | cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); | |
1596 | /* | |
1597 | * We need to save a lock here to let us add it again to | |
1598 | * the file's list if the unlock range request fails on | |
1599 | * the server. | |
1600 | */ | |
1601 | list_move(&li->llist, &tmp_llist); | |
1602 | if (++num == max_num) { | |
4b4de76e PS |
1603 | stored_rc = cifs_lockv(xid, tcon, |
1604 | cfile->fid.netfid, | |
ea319d57 PS |
1605 | li->type, num, 0, buf); |
1606 | if (stored_rc) { | |
1607 | /* | |
1608 | * We failed on the unlock range | |
1609 | * request - add all locks from the tmp | |
1610 | * list to the head of the file's list. | |
1611 | */ | |
1612 | cifs_move_llist(&tmp_llist, | |
f45d3416 | 1613 | &cfile->llist->locks); |
ea319d57 PS |
1614 | rc = stored_rc; |
1615 | } else | |
1616 | /* | |
1617 | * The unlock range request succeed - | |
1618 | * free the tmp list. | |
1619 | */ | |
1620 | cifs_free_llist(&tmp_llist); | |
1621 | cur = buf; | |
1622 | num = 0; | |
1623 | } else | |
1624 | cur++; | |
9ee305b7 PS |
1625 | } |
1626 | if (num) { | |
4b4de76e | 1627 | stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, |
9ee305b7 PS |
1628 | types[i], num, 0, buf); |
1629 | if (stored_rc) { | |
f45d3416 PS |
1630 | cifs_move_llist(&tmp_llist, |
1631 | &cfile->llist->locks); | |
9ee305b7 PS |
1632 | rc = stored_rc; |
1633 | } else | |
1634 | cifs_free_llist(&tmp_llist); | |
1635 | } | |
1636 | } | |
1637 | ||
1b4b55a1 | 1638 | up_write(&cinode->lock_sem); |
9ee305b7 PS |
1639 | kfree(buf); |
1640 | return rc; | |
1641 | } | |
1642 | ||
03776f45 | 1643 | static int |
f45d3416 | 1644 | cifs_setlk(struct file *file, struct file_lock *flock, __u32 type, |
6d5786a3 PS |
1645 | bool wait_flag, bool posix_lck, int lock, int unlock, |
1646 | unsigned int xid) | |
03776f45 PS |
1647 | { |
1648 | int rc = 0; | |
1649 | __u64 length = 1 + flock->fl_end - flock->fl_start; | |
1650 | struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; | |
1651 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); | |
106dc538 | 1652 | struct TCP_Server_Info *server = tcon->ses->server; |
2b0143b5 | 1653 | struct inode *inode = d_inode(cfile->dentry); |
03776f45 PS |
1654 | |
1655 | if (posix_lck) { | |
08547b03 | 1656 | int posix_lock_type; |
4f6bcec9 PS |
1657 | |
1658 | rc = cifs_posix_lock_set(file, flock); | |
1659 | if (!rc || rc < 0) | |
1660 | return rc; | |
1661 | ||
106dc538 | 1662 | if (type & server->vals->shared_lock_type) |
08547b03 SF |
1663 | posix_lock_type = CIFS_RDLCK; |
1664 | else | |
1665 | posix_lock_type = CIFS_WRLCK; | |
50c2f753 | 1666 | |
03776f45 | 1667 | if (unlock == 1) |
beb84dc8 | 1668 | posix_lock_type = CIFS_UNLCK; |
7ee1af76 | 1669 | |
f45d3416 | 1670 | rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid, |
3d22462a JL |
1671 | hash_lockowner(flock->fl_owner), |
1672 | flock->fl_start, length, | |
f45d3416 | 1673 | NULL, posix_lock_type, wait_flag); |
03776f45 PS |
1674 | goto out; |
1675 | } | |
7ee1af76 | 1676 | |
03776f45 | 1677 | if (lock) { |
161ebf9f PS |
1678 | struct cifsLockInfo *lock; |
1679 | ||
9645759c RS |
1680 | lock = cifs_lock_init(flock->fl_start, length, type, |
1681 | flock->fl_flags); | |
161ebf9f PS |
1682 | if (!lock) |
1683 | return -ENOMEM; | |
1684 | ||
fbd35aca | 1685 | rc = cifs_lock_add_if(cfile, lock, wait_flag); |
21cb2d90 | 1686 | if (rc < 0) { |
161ebf9f | 1687 | kfree(lock); |
21cb2d90 PS |
1688 | return rc; |
1689 | } | |
1690 | if (!rc) | |
85160e03 PS |
1691 | goto out; |
1692 | ||
63b7d3a4 PS |
1693 | /* |
1694 | * Windows 7 server can delay breaking lease from read to None | |
1695 | * if we set a byte-range lock on a file - break it explicitly | |
1696 | * before sending the lock to the server to be sure the next | |
1697 | * read won't conflict with non-overlapted locks due to | |
1698 | * pagereading. | |
1699 | */ | |
18cceb6a PS |
1700 | if (!CIFS_CACHE_WRITE(CIFS_I(inode)) && |
1701 | CIFS_CACHE_READ(CIFS_I(inode))) { | |
4f73c7d3 | 1702 | cifs_zap_mapping(inode); |
f96637be JP |
1703 | cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n", |
1704 | inode); | |
18cceb6a | 1705 | CIFS_I(inode)->oplock = 0; |
63b7d3a4 PS |
1706 | } |
1707 | ||
d39a4f71 PS |
1708 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, |
1709 | type, 1, 0, wait_flag); | |
161ebf9f PS |
1710 | if (rc) { |
1711 | kfree(lock); | |
21cb2d90 | 1712 | return rc; |
03776f45 | 1713 | } |
161ebf9f | 1714 | |
fbd35aca | 1715 | cifs_lock_add(cfile, lock); |
9ee305b7 | 1716 | } else if (unlock) |
d39a4f71 | 1717 | rc = server->ops->mand_unlock_range(cfile, flock, xid); |
03776f45 | 1718 | |
03776f45 | 1719 | out: |
bc31d0cd AA |
1720 | if (flock->fl_flags & FL_POSIX) { |
1721 | /* | |
1722 | * If this is a request to remove all locks because we | |
1723 | * are closing the file, it doesn't matter if the | |
1724 | * unlocking failed as both cifs.ko and the SMB server | |
1725 | * remove the lock on file close | |
1726 | */ | |
1727 | if (rc) { | |
1728 | cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc); | |
1729 | if (!(flock->fl_flags & FL_CLOSE)) | |
1730 | return rc; | |
1731 | } | |
4f656367 | 1732 | rc = locks_lock_file_wait(file, flock); |
bc31d0cd | 1733 | } |
03776f45 PS |
1734 | return rc; |
1735 | } | |
1736 | ||
1737 | int cifs_lock(struct file *file, int cmd, struct file_lock *flock) | |
1738 | { | |
1739 | int rc, xid; | |
1740 | int lock = 0, unlock = 0; | |
1741 | bool wait_flag = false; | |
1742 | bool posix_lck = false; | |
1743 | struct cifs_sb_info *cifs_sb; | |
1744 | struct cifs_tcon *tcon; | |
03776f45 | 1745 | struct cifsFileInfo *cfile; |
04a6aa8a | 1746 | __u32 type; |
03776f45 PS |
1747 | |
1748 | rc = -EACCES; | |
6d5786a3 | 1749 | xid = get_xid(); |
03776f45 | 1750 | |
f96637be JP |
1751 | cifs_dbg(FYI, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld end: %lld\n", |
1752 | cmd, flock->fl_flags, flock->fl_type, | |
1753 | flock->fl_start, flock->fl_end); | |
03776f45 | 1754 | |
03776f45 PS |
1755 | cfile = (struct cifsFileInfo *)file->private_data; |
1756 | tcon = tlink_tcon(cfile->tlink); | |
106dc538 PS |
1757 | |
1758 | cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag, | |
1759 | tcon->ses->server); | |
7119e220 | 1760 | cifs_sb = CIFS_FILE_SB(file); |
03776f45 | 1761 | |
29e20f9c | 1762 | if (cap_unix(tcon->ses) && |
03776f45 PS |
1763 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && |
1764 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) | |
1765 | posix_lck = true; | |
1766 | /* | |
1767 | * BB add code here to normalize offset and length to account for | |
1768 | * negative length which we can not accept over the wire. | |
1769 | */ | |
1770 | if (IS_GETLK(cmd)) { | |
4f6bcec9 | 1771 | rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid); |
6d5786a3 | 1772 | free_xid(xid); |
03776f45 PS |
1773 | return rc; |
1774 | } | |
1775 | ||
1776 | if (!lock && !unlock) { | |
1777 | /* | |
1778 | * if no lock or unlock then nothing to do since we do not | |
1779 | * know what it is | |
1780 | */ | |
6d5786a3 | 1781 | free_xid(xid); |
03776f45 | 1782 | return -EOPNOTSUPP; |
7ee1af76 JA |
1783 | } |
1784 | ||
03776f45 PS |
1785 | rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock, |
1786 | xid); | |
6d5786a3 | 1787 | free_xid(xid); |
1da177e4 LT |
1788 | return rc; |
1789 | } | |
1790 | ||
597b027f JL |
1791 | /* |
1792 | * update the file size (if needed) after a write. Should be called with | |
1793 | * the inode->i_lock held | |
1794 | */ | |
72432ffc | 1795 | void |
fbec9ab9 JL |
1796 | cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset, |
1797 | unsigned int bytes_written) | |
1798 | { | |
1799 | loff_t end_of_write = offset + bytes_written; | |
1800 | ||
1801 | if (end_of_write > cifsi->server_eof) | |
1802 | cifsi->server_eof = end_of_write; | |
1803 | } | |
1804 | ||
ba9ad725 PS |
1805 | static ssize_t |
1806 | cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data, | |
1807 | size_t write_size, loff_t *offset) | |
1da177e4 LT |
1808 | { |
1809 | int rc = 0; | |
1810 | unsigned int bytes_written = 0; | |
1811 | unsigned int total_written; | |
ba9ad725 PS |
1812 | struct cifs_tcon *tcon; |
1813 | struct TCP_Server_Info *server; | |
6d5786a3 | 1814 | unsigned int xid; |
7da4b49a | 1815 | struct dentry *dentry = open_file->dentry; |
2b0143b5 | 1816 | struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry)); |
fa2989f4 | 1817 | struct cifs_io_parms io_parms; |
1da177e4 | 1818 | |
35c265e0 AV |
1819 | cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n", |
1820 | write_size, *offset, dentry); | |
1da177e4 | 1821 | |
ba9ad725 PS |
1822 | tcon = tlink_tcon(open_file->tlink); |
1823 | server = tcon->ses->server; | |
1824 | ||
1825 | if (!server->ops->sync_write) | |
1826 | return -ENOSYS; | |
50c2f753 | 1827 | |
6d5786a3 | 1828 | xid = get_xid(); |
1da177e4 | 1829 | |
1da177e4 LT |
1830 | for (total_written = 0; write_size > total_written; |
1831 | total_written += bytes_written) { | |
1832 | rc = -EAGAIN; | |
1833 | while (rc == -EAGAIN) { | |
ca83ce3d JL |
1834 | struct kvec iov[2]; |
1835 | unsigned int len; | |
1836 | ||
1da177e4 | 1837 | if (open_file->invalidHandle) { |
1da177e4 LT |
1838 | /* we could deadlock if we called |
1839 | filemap_fdatawait from here so tell | |
fb8c4b14 | 1840 | reopen_file not to flush data to |
1da177e4 | 1841 | server now */ |
15886177 | 1842 | rc = cifs_reopen_file(open_file, false); |
1da177e4 LT |
1843 | if (rc != 0) |
1844 | break; | |
1845 | } | |
ca83ce3d | 1846 | |
2b0143b5 | 1847 | len = min(server->ops->wp_retry_size(d_inode(dentry)), |
cb7e9eab | 1848 | (unsigned int)write_size - total_written); |
ca83ce3d JL |
1849 | /* iov[0] is reserved for smb header */ |
1850 | iov[1].iov_base = (char *)write_data + total_written; | |
1851 | iov[1].iov_len = len; | |
fa2989f4 | 1852 | io_parms.pid = pid; |
ba9ad725 PS |
1853 | io_parms.tcon = tcon; |
1854 | io_parms.offset = *offset; | |
fa2989f4 | 1855 | io_parms.length = len; |
db8b631d SF |
1856 | rc = server->ops->sync_write(xid, &open_file->fid, |
1857 | &io_parms, &bytes_written, iov, 1); | |
1da177e4 LT |
1858 | } |
1859 | if (rc || (bytes_written == 0)) { | |
1860 | if (total_written) | |
1861 | break; | |
1862 | else { | |
6d5786a3 | 1863 | free_xid(xid); |
1da177e4 LT |
1864 | return rc; |
1865 | } | |
fbec9ab9 | 1866 | } else { |
2b0143b5 | 1867 | spin_lock(&d_inode(dentry)->i_lock); |
ba9ad725 | 1868 | cifs_update_eof(cifsi, *offset, bytes_written); |
2b0143b5 | 1869 | spin_unlock(&d_inode(dentry)->i_lock); |
ba9ad725 | 1870 | *offset += bytes_written; |
fbec9ab9 | 1871 | } |
1da177e4 LT |
1872 | } |
1873 | ||
ba9ad725 | 1874 | cifs_stats_bytes_written(tcon, total_written); |
1da177e4 | 1875 | |
7da4b49a | 1876 | if (total_written > 0) { |
2b0143b5 DH |
1877 | spin_lock(&d_inode(dentry)->i_lock); |
1878 | if (*offset > d_inode(dentry)->i_size) | |
1879 | i_size_write(d_inode(dentry), *offset); | |
1880 | spin_unlock(&d_inode(dentry)->i_lock); | |
1da177e4 | 1881 | } |
2b0143b5 | 1882 | mark_inode_dirty_sync(d_inode(dentry)); |
6d5786a3 | 1883 | free_xid(xid); |
1da177e4 LT |
1884 | return total_written; |
1885 | } | |
1886 | ||
6508d904 JL |
1887 | struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode, |
1888 | bool fsuid_only) | |
630f3f0c SF |
1889 | { |
1890 | struct cifsFileInfo *open_file = NULL; | |
6508d904 JL |
1891 | struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb); |
1892 | ||
1893 | /* only filter by fsuid on multiuser mounts */ | |
1894 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) | |
1895 | fsuid_only = false; | |
630f3f0c | 1896 | |
cb248819 | 1897 | spin_lock(&cifs_inode->open_file_lock); |
630f3f0c SF |
1898 | /* we could simply get the first_list_entry since write-only entries |
1899 | are always at the end of the list but since the first entry might | |
1900 | have a close pending, we go through the whole list */ | |
1901 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { | |
fef59fd7 | 1902 | if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) |
6508d904 | 1903 | continue; |
2e396b83 | 1904 | if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) { |
630f3f0c SF |
1905 | if (!open_file->invalidHandle) { |
1906 | /* found a good file */ | |
1907 | /* lock it so it will not be closed on us */ | |
3afca265 | 1908 | cifsFileInfo_get(open_file); |
cb248819 | 1909 | spin_unlock(&cifs_inode->open_file_lock); |
630f3f0c SF |
1910 | return open_file; |
1911 | } /* else might as well continue, and look for | |
1912 | another, or simply have the caller reopen it | |
1913 | again rather than trying to fix this handle */ | |
1914 | } else /* write only file */ | |
1915 | break; /* write only files are last so must be done */ | |
1916 | } | |
cb248819 | 1917 | spin_unlock(&cifs_inode->open_file_lock); |
630f3f0c SF |
1918 | return NULL; |
1919 | } | |
630f3f0c | 1920 | |
fe768d51 PS |
1921 | /* Return -EBADF if no handle is found and general rc otherwise */ |
1922 | int | |
c2e9b9e9 | 1923 | cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags, |
fe768d51 | 1924 | struct cifsFileInfo **ret_file) |
6148a742 | 1925 | { |
2c0c2a08 | 1926 | struct cifsFileInfo *open_file, *inv_file = NULL; |
d3892294 | 1927 | struct cifs_sb_info *cifs_sb; |
2846d386 | 1928 | bool any_available = false; |
fe768d51 | 1929 | int rc = -EBADF; |
2c0c2a08 | 1930 | unsigned int refind = 0; |
c2e9b9e9 AA |
1931 | bool fsuid_only = flags & FIND_WR_FSUID_ONLY; |
1932 | bool with_delete = flags & FIND_WR_WITH_DELETE; | |
fe768d51 PS |
1933 | *ret_file = NULL; |
1934 | ||
1935 | /* | |
1936 | * Having a null inode here (because mapping->host was set to zero by | |
1937 | * the VFS or MM) should not happen but we had reports of on oops (due | |
1938 | * to it being zero) during stress testcases so we need to check for it | |
1939 | */ | |
60808233 | 1940 | |
fb8c4b14 | 1941 | if (cifs_inode == NULL) { |
f96637be | 1942 | cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n"); |
60808233 | 1943 | dump_stack(); |
fe768d51 | 1944 | return rc; |
60808233 SF |
1945 | } |
1946 | ||
d3892294 JL |
1947 | cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb); |
1948 | ||
6508d904 JL |
1949 | /* only filter by fsuid on multiuser mounts */ |
1950 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) | |
1951 | fsuid_only = false; | |
1952 | ||
cb248819 | 1953 | spin_lock(&cifs_inode->open_file_lock); |
9b22b0b7 | 1954 | refind_writable: |
2c0c2a08 | 1955 | if (refind > MAX_REOPEN_ATT) { |
cb248819 | 1956 | spin_unlock(&cifs_inode->open_file_lock); |
fe768d51 | 1957 | return rc; |
2c0c2a08 | 1958 | } |
6148a742 | 1959 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { |
6508d904 JL |
1960 | if (!any_available && open_file->pid != current->tgid) |
1961 | continue; | |
fef59fd7 | 1962 | if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) |
6148a742 | 1963 | continue; |
c2e9b9e9 AA |
1964 | if (with_delete && !(open_file->fid.access & DELETE)) |
1965 | continue; | |
2e396b83 | 1966 | if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { |
9b22b0b7 SF |
1967 | if (!open_file->invalidHandle) { |
1968 | /* found a good writable file */ | |
3afca265 | 1969 | cifsFileInfo_get(open_file); |
cb248819 | 1970 | spin_unlock(&cifs_inode->open_file_lock); |
fe768d51 PS |
1971 | *ret_file = open_file; |
1972 | return 0; | |
2c0c2a08 SP |
1973 | } else { |
1974 | if (!inv_file) | |
1975 | inv_file = open_file; | |
9b22b0b7 | 1976 | } |
6148a742 SF |
1977 | } |
1978 | } | |
2846d386 JL |
1979 | /* couldn't find useable FH with same pid, try any available */ |
1980 | if (!any_available) { | |
1981 | any_available = true; | |
1982 | goto refind_writable; | |
1983 | } | |
2c0c2a08 SP |
1984 | |
1985 | if (inv_file) { | |
1986 | any_available = false; | |
3afca265 | 1987 | cifsFileInfo_get(inv_file); |
2c0c2a08 SP |
1988 | } |
1989 | ||
cb248819 | 1990 | spin_unlock(&cifs_inode->open_file_lock); |
2c0c2a08 SP |
1991 | |
1992 | if (inv_file) { | |
1993 | rc = cifs_reopen_file(inv_file, false); | |
fe768d51 PS |
1994 | if (!rc) { |
1995 | *ret_file = inv_file; | |
1996 | return 0; | |
2c0c2a08 | 1997 | } |
fe768d51 | 1998 | |
487317c9 | 1999 | spin_lock(&cifs_inode->open_file_lock); |
fe768d51 | 2000 | list_move_tail(&inv_file->flist, &cifs_inode->openFileList); |
487317c9 | 2001 | spin_unlock(&cifs_inode->open_file_lock); |
fe768d51 PS |
2002 | cifsFileInfo_put(inv_file); |
2003 | ++refind; | |
2004 | inv_file = NULL; | |
cb248819 | 2005 | spin_lock(&cifs_inode->open_file_lock); |
fe768d51 | 2006 | goto refind_writable; |
2c0c2a08 SP |
2007 | } |
2008 | ||
fe768d51 PS |
2009 | return rc; |
2010 | } | |
2011 | ||
2012 | struct cifsFileInfo * | |
c2e9b9e9 | 2013 | find_writable_file(struct cifsInodeInfo *cifs_inode, int flags) |
fe768d51 PS |
2014 | { |
2015 | struct cifsFileInfo *cfile; | |
2016 | int rc; | |
2017 | ||
c2e9b9e9 | 2018 | rc = cifs_get_writable_file(cifs_inode, flags, &cfile); |
fe768d51 PS |
2019 | if (rc) |
2020 | cifs_dbg(FYI, "couldn't find writable handle rc=%d", rc); | |
2021 | ||
2022 | return cfile; | |
6148a742 SF |
2023 | } |
2024 | ||
8de9e86c RS |
2025 | int |
2026 | cifs_get_writable_path(struct cifs_tcon *tcon, const char *name, | |
c2e9b9e9 | 2027 | int flags, |
8de9e86c RS |
2028 | struct cifsFileInfo **ret_file) |
2029 | { | |
2030 | struct list_head *tmp; | |
2031 | struct cifsFileInfo *cfile; | |
2032 | struct cifsInodeInfo *cinode; | |
2033 | char *full_path; | |
2034 | ||
2035 | *ret_file = NULL; | |
2036 | ||
2037 | spin_lock(&tcon->open_file_lock); | |
2038 | list_for_each(tmp, &tcon->openFileList) { | |
2039 | cfile = list_entry(tmp, struct cifsFileInfo, | |
2040 | tlist); | |
2041 | full_path = build_path_from_dentry(cfile->dentry); | |
2042 | if (full_path == NULL) { | |
2043 | spin_unlock(&tcon->open_file_lock); | |
2044 | return -ENOMEM; | |
2045 | } | |
2046 | if (strcmp(full_path, name)) { | |
2047 | kfree(full_path); | |
2048 | continue; | |
2049 | } | |
2050 | ||
2051 | kfree(full_path); | |
2052 | cinode = CIFS_I(d_inode(cfile->dentry)); | |
2053 | spin_unlock(&tcon->open_file_lock); | |
c2e9b9e9 | 2054 | return cifs_get_writable_file(cinode, flags, ret_file); |
8de9e86c RS |
2055 | } |
2056 | ||
2057 | spin_unlock(&tcon->open_file_lock); | |
2058 | return -ENOENT; | |
2059 | } | |
2060 | ||
496902dc RS |
2061 | int |
2062 | cifs_get_readable_path(struct cifs_tcon *tcon, const char *name, | |
2063 | struct cifsFileInfo **ret_file) | |
2064 | { | |
2065 | struct list_head *tmp; | |
2066 | struct cifsFileInfo *cfile; | |
2067 | struct cifsInodeInfo *cinode; | |
2068 | char *full_path; | |
2069 | ||
2070 | *ret_file = NULL; | |
2071 | ||
2072 | spin_lock(&tcon->open_file_lock); | |
2073 | list_for_each(tmp, &tcon->openFileList) { | |
2074 | cfile = list_entry(tmp, struct cifsFileInfo, | |
2075 | tlist); | |
2076 | full_path = build_path_from_dentry(cfile->dentry); | |
2077 | if (full_path == NULL) { | |
2078 | spin_unlock(&tcon->open_file_lock); | |
2079 | return -ENOMEM; | |
2080 | } | |
2081 | if (strcmp(full_path, name)) { | |
2082 | kfree(full_path); | |
2083 | continue; | |
2084 | } | |
2085 | ||
2086 | kfree(full_path); | |
2087 | cinode = CIFS_I(d_inode(cfile->dentry)); | |
2088 | spin_unlock(&tcon->open_file_lock); | |
2089 | *ret_file = find_readable_file(cinode, 0); | |
2090 | return *ret_file ? 0 : -ENOENT; | |
2091 | } | |
2092 | ||
2093 | spin_unlock(&tcon->open_file_lock); | |
2094 | return -ENOENT; | |
2095 | } | |
2096 | ||
1da177e4 LT |
2097 | static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to) |
2098 | { | |
2099 | struct address_space *mapping = page->mapping; | |
09cbfeaf | 2100 | loff_t offset = (loff_t)page->index << PAGE_SHIFT; |
1da177e4 LT |
2101 | char *write_data; |
2102 | int rc = -EFAULT; | |
2103 | int bytes_written = 0; | |
1da177e4 | 2104 | struct inode *inode; |
6148a742 | 2105 | struct cifsFileInfo *open_file; |
1da177e4 LT |
2106 | |
2107 | if (!mapping || !mapping->host) | |
2108 | return -EFAULT; | |
2109 | ||
2110 | inode = page->mapping->host; | |
1da177e4 LT |
2111 | |
2112 | offset += (loff_t)from; | |
2113 | write_data = kmap(page); | |
2114 | write_data += from; | |
2115 | ||
09cbfeaf | 2116 | if ((to > PAGE_SIZE) || (from > to)) { |
1da177e4 LT |
2117 | kunmap(page); |
2118 | return -EIO; | |
2119 | } | |
2120 | ||
2121 | /* racing with truncate? */ | |
2122 | if (offset > mapping->host->i_size) { | |
2123 | kunmap(page); | |
2124 | return 0; /* don't care */ | |
2125 | } | |
2126 | ||
2127 | /* check to make sure that we are not extending the file */ | |
2128 | if (mapping->host->i_size - offset < (loff_t)to) | |
fb8c4b14 | 2129 | to = (unsigned)(mapping->host->i_size - offset); |
1da177e4 | 2130 | |
c2e9b9e9 AA |
2131 | rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY, |
2132 | &open_file); | |
fe768d51 | 2133 | if (!rc) { |
fa2989f4 PS |
2134 | bytes_written = cifs_write(open_file, open_file->pid, |
2135 | write_data, to - from, &offset); | |
6ab409b5 | 2136 | cifsFileInfo_put(open_file); |
1da177e4 | 2137 | /* Does mm or vfs already set times? */ |
c2050a45 | 2138 | inode->i_atime = inode->i_mtime = current_time(inode); |
bb5a9a04 | 2139 | if ((bytes_written > 0) && (offset)) |
6148a742 | 2140 | rc = 0; |
bb5a9a04 SF |
2141 | else if (bytes_written < 0) |
2142 | rc = bytes_written; | |
fe768d51 PS |
2143 | else |
2144 | rc = -EFAULT; | |
6148a742 | 2145 | } else { |
fe768d51 PS |
2146 | cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc); |
2147 | if (!is_retryable_error(rc)) | |
2148 | rc = -EIO; | |
1da177e4 LT |
2149 | } |
2150 | ||
2151 | kunmap(page); | |
2152 | return rc; | |
2153 | } | |
2154 | ||
90ac1387 PS |
2155 | static struct cifs_writedata * |
2156 | wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping, | |
2157 | pgoff_t end, pgoff_t *index, | |
2158 | unsigned int *found_pages) | |
2159 | { | |
90ac1387 PS |
2160 | struct cifs_writedata *wdata; |
2161 | ||
2162 | wdata = cifs_writedata_alloc((unsigned int)tofind, | |
2163 | cifs_writev_complete); | |
2164 | if (!wdata) | |
2165 | return NULL; | |
2166 | ||
9c19a9cb JK |
2167 | *found_pages = find_get_pages_range_tag(mapping, index, end, |
2168 | PAGECACHE_TAG_DIRTY, tofind, wdata->pages); | |
90ac1387 PS |
2169 | return wdata; |
2170 | } | |
2171 | ||
7e48ff82 PS |
2172 | static unsigned int |
2173 | wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages, | |
2174 | struct address_space *mapping, | |
2175 | struct writeback_control *wbc, | |
2176 | pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done) | |
2177 | { | |
2178 | unsigned int nr_pages = 0, i; | |
2179 | struct page *page; | |
2180 | ||
2181 | for (i = 0; i < found_pages; i++) { | |
2182 | page = wdata->pages[i]; | |
2183 | /* | |
b93b0163 MW |
2184 | * At this point we hold neither the i_pages lock nor the |
2185 | * page lock: the page may be truncated or invalidated | |
2186 | * (changing page->mapping to NULL), or even swizzled | |
2187 | * back from swapper_space to tmpfs file mapping | |
7e48ff82 PS |
2188 | */ |
2189 | ||
2190 | if (nr_pages == 0) | |
2191 | lock_page(page); | |
2192 | else if (!trylock_page(page)) | |
2193 | break; | |
2194 | ||
2195 | if (unlikely(page->mapping != mapping)) { | |
2196 | unlock_page(page); | |
2197 | break; | |
2198 | } | |
2199 | ||
2200 | if (!wbc->range_cyclic && page->index > end) { | |
2201 | *done = true; | |
2202 | unlock_page(page); | |
2203 | break; | |
2204 | } | |
2205 | ||
2206 | if (*next && (page->index != *next)) { | |
2207 | /* Not next consecutive page */ | |
2208 | unlock_page(page); | |
2209 | break; | |
2210 | } | |
2211 | ||
2212 | if (wbc->sync_mode != WB_SYNC_NONE) | |
2213 | wait_on_page_writeback(page); | |
2214 | ||
2215 | if (PageWriteback(page) || | |
2216 | !clear_page_dirty_for_io(page)) { | |
2217 | unlock_page(page); | |
2218 | break; | |
2219 | } | |
2220 | ||
2221 | /* | |
2222 | * This actually clears the dirty bit in the radix tree. | |
2223 | * See cifs_writepage() for more commentary. | |
2224 | */ | |
2225 | set_page_writeback(page); | |
2226 | if (page_offset(page) >= i_size_read(mapping->host)) { | |
2227 | *done = true; | |
2228 | unlock_page(page); | |
2229 | end_page_writeback(page); | |
2230 | break; | |
2231 | } | |
2232 | ||
2233 | wdata->pages[i] = page; | |
2234 | *next = page->index + 1; | |
2235 | ++nr_pages; | |
2236 | } | |
2237 | ||
2238 | /* reset index to refind any pages skipped */ | |
2239 | if (nr_pages == 0) | |
2240 | *index = wdata->pages[0]->index + 1; | |
2241 | ||
2242 | /* put any pages we aren't going to use */ | |
2243 | for (i = nr_pages; i < found_pages; i++) { | |
09cbfeaf | 2244 | put_page(wdata->pages[i]); |
7e48ff82 PS |
2245 | wdata->pages[i] = NULL; |
2246 | } | |
2247 | ||
2248 | return nr_pages; | |
2249 | } | |
2250 | ||
619aa48e | 2251 | static int |
c4b8f657 PS |
2252 | wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages, |
2253 | struct address_space *mapping, struct writeback_control *wbc) | |
619aa48e | 2254 | { |
258f0603 | 2255 | int rc; |
c4b8f657 PS |
2256 | struct TCP_Server_Info *server = |
2257 | tlink_tcon(wdata->cfile->tlink)->ses->server; | |
619aa48e PS |
2258 | |
2259 | wdata->sync_mode = wbc->sync_mode; | |
2260 | wdata->nr_pages = nr_pages; | |
2261 | wdata->offset = page_offset(wdata->pages[0]); | |
09cbfeaf | 2262 | wdata->pagesz = PAGE_SIZE; |
619aa48e PS |
2263 | wdata->tailsz = min(i_size_read(mapping->host) - |
2264 | page_offset(wdata->pages[nr_pages - 1]), | |
09cbfeaf KS |
2265 | (loff_t)PAGE_SIZE); |
2266 | wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz; | |
c4b8f657 | 2267 | wdata->pid = wdata->cfile->pid; |
619aa48e | 2268 | |
9a1c67e8 PS |
2269 | rc = adjust_credits(server, &wdata->credits, wdata->bytes); |
2270 | if (rc) | |
258f0603 | 2271 | return rc; |
9a1c67e8 | 2272 | |
c4b8f657 PS |
2273 | if (wdata->cfile->invalidHandle) |
2274 | rc = -EAGAIN; | |
2275 | else | |
2276 | rc = server->ops->async_writev(wdata, cifs_writedata_release); | |
619aa48e | 2277 | |
619aa48e PS |
2278 | return rc; |
2279 | } | |
2280 | ||
1da177e4 | 2281 | static int cifs_writepages(struct address_space *mapping, |
37c0eb46 | 2282 | struct writeback_control *wbc) |
1da177e4 | 2283 | { |
c7d38dbe PS |
2284 | struct inode *inode = mapping->host; |
2285 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); | |
cb7e9eab | 2286 | struct TCP_Server_Info *server; |
c3d17b63 JL |
2287 | bool done = false, scanned = false, range_whole = false; |
2288 | pgoff_t end, index; | |
2289 | struct cifs_writedata *wdata; | |
c7d38dbe | 2290 | struct cifsFileInfo *cfile = NULL; |
37c0eb46 | 2291 | int rc = 0; |
9a66396f | 2292 | int saved_rc = 0; |
0cb012d1 | 2293 | unsigned int xid; |
50c2f753 | 2294 | |
37c0eb46 | 2295 | /* |
c3d17b63 | 2296 | * If wsize is smaller than the page cache size, default to writing |
37c0eb46 SF |
2297 | * one page at a time via cifs_writepage |
2298 | */ | |
09cbfeaf | 2299 | if (cifs_sb->wsize < PAGE_SIZE) |
37c0eb46 SF |
2300 | return generic_writepages(mapping, wbc); |
2301 | ||
0cb012d1 | 2302 | xid = get_xid(); |
111ebb6e | 2303 | if (wbc->range_cyclic) { |
37c0eb46 | 2304 | index = mapping->writeback_index; /* Start from prev offset */ |
111ebb6e OH |
2305 | end = -1; |
2306 | } else { | |
09cbfeaf KS |
2307 | index = wbc->range_start >> PAGE_SHIFT; |
2308 | end = wbc->range_end >> PAGE_SHIFT; | |
111ebb6e | 2309 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
c3d17b63 JL |
2310 | range_whole = true; |
2311 | scanned = true; | |
37c0eb46 | 2312 | } |
cb7e9eab | 2313 | server = cifs_sb_master_tcon(cifs_sb)->ses->server; |
37c0eb46 | 2314 | retry: |
c3d17b63 | 2315 | while (!done && index <= end) { |
335b7b62 | 2316 | unsigned int i, nr_pages, found_pages, wsize; |
66231a47 | 2317 | pgoff_t next = 0, tofind, saved_index = index; |
335b7b62 PS |
2318 | struct cifs_credits credits_on_stack; |
2319 | struct cifs_credits *credits = &credits_on_stack; | |
fe768d51 | 2320 | int get_file_rc = 0; |
c3d17b63 | 2321 | |
c7d38dbe PS |
2322 | if (cfile) |
2323 | cifsFileInfo_put(cfile); | |
2324 | ||
c2e9b9e9 | 2325 | rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile); |
fe768d51 PS |
2326 | |
2327 | /* in case of an error store it to return later */ | |
2328 | if (rc) | |
2329 | get_file_rc = rc; | |
c7d38dbe | 2330 | |
cb7e9eab | 2331 | rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize, |
335b7b62 | 2332 | &wsize, credits); |
9a66396f PS |
2333 | if (rc != 0) { |
2334 | done = true; | |
cb7e9eab | 2335 | break; |
9a66396f | 2336 | } |
c3d17b63 | 2337 | |
09cbfeaf | 2338 | tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1; |
c3d17b63 | 2339 | |
90ac1387 PS |
2340 | wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index, |
2341 | &found_pages); | |
c3d17b63 JL |
2342 | if (!wdata) { |
2343 | rc = -ENOMEM; | |
9a66396f | 2344 | done = true; |
cb7e9eab | 2345 | add_credits_and_wake_if(server, credits, 0); |
c3d17b63 JL |
2346 | break; |
2347 | } | |
2348 | ||
c3d17b63 JL |
2349 | if (found_pages == 0) { |
2350 | kref_put(&wdata->refcount, cifs_writedata_release); | |
cb7e9eab | 2351 | add_credits_and_wake_if(server, credits, 0); |
c3d17b63 JL |
2352 | break; |
2353 | } | |
2354 | ||
7e48ff82 PS |
2355 | nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc, |
2356 | end, &index, &next, &done); | |
37c0eb46 | 2357 | |
c3d17b63 JL |
2358 | /* nothing to write? */ |
2359 | if (nr_pages == 0) { | |
2360 | kref_put(&wdata->refcount, cifs_writedata_release); | |
cb7e9eab | 2361 | add_credits_and_wake_if(server, credits, 0); |
c3d17b63 | 2362 | continue; |
37c0eb46 | 2363 | } |
fbec9ab9 | 2364 | |
335b7b62 | 2365 | wdata->credits = credits_on_stack; |
c7d38dbe PS |
2366 | wdata->cfile = cfile; |
2367 | cfile = NULL; | |
941b853d | 2368 | |
c4b8f657 | 2369 | if (!wdata->cfile) { |
fe768d51 PS |
2370 | cifs_dbg(VFS, "No writable handle in writepages rc=%d\n", |
2371 | get_file_rc); | |
2372 | if (is_retryable_error(get_file_rc)) | |
2373 | rc = get_file_rc; | |
2374 | else | |
2375 | rc = -EBADF; | |
c4b8f657 PS |
2376 | } else |
2377 | rc = wdata_send_pages(wdata, nr_pages, mapping, wbc); | |
f3983c21 | 2378 | |
258f0603 PS |
2379 | for (i = 0; i < nr_pages; ++i) |
2380 | unlock_page(wdata->pages[i]); | |
2381 | ||
c3d17b63 JL |
2382 | /* send failure -- clean up the mess */ |
2383 | if (rc != 0) { | |
335b7b62 | 2384 | add_credits_and_wake_if(server, &wdata->credits, 0); |
c3d17b63 | 2385 | for (i = 0; i < nr_pages; ++i) { |
9a66396f | 2386 | if (is_retryable_error(rc)) |
c3d17b63 JL |
2387 | redirty_page_for_writepage(wbc, |
2388 | wdata->pages[i]); | |
2389 | else | |
2390 | SetPageError(wdata->pages[i]); | |
2391 | end_page_writeback(wdata->pages[i]); | |
09cbfeaf | 2392 | put_page(wdata->pages[i]); |
37c0eb46 | 2393 | } |
9a66396f | 2394 | if (!is_retryable_error(rc)) |
941b853d | 2395 | mapping_set_error(mapping, rc); |
c3d17b63 JL |
2396 | } |
2397 | kref_put(&wdata->refcount, cifs_writedata_release); | |
941b853d | 2398 | |
66231a47 PS |
2399 | if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) { |
2400 | index = saved_index; | |
2401 | continue; | |
2402 | } | |
2403 | ||
9a66396f PS |
2404 | /* Return immediately if we received a signal during writing */ |
2405 | if (is_interrupt_error(rc)) { | |
2406 | done = true; | |
2407 | break; | |
2408 | } | |
2409 | ||
2410 | if (rc != 0 && saved_rc == 0) | |
2411 | saved_rc = rc; | |
2412 | ||
c3d17b63 JL |
2413 | wbc->nr_to_write -= nr_pages; |
2414 | if (wbc->nr_to_write <= 0) | |
2415 | done = true; | |
b066a48c | 2416 | |
c3d17b63 | 2417 | index = next; |
37c0eb46 | 2418 | } |
c3d17b63 | 2419 | |
37c0eb46 SF |
2420 | if (!scanned && !done) { |
2421 | /* | |
2422 | * We hit the last page and there is more work to be done: wrap | |
2423 | * back to the start of the file | |
2424 | */ | |
c3d17b63 | 2425 | scanned = true; |
37c0eb46 SF |
2426 | index = 0; |
2427 | goto retry; | |
2428 | } | |
c3d17b63 | 2429 | |
9a66396f PS |
2430 | if (saved_rc != 0) |
2431 | rc = saved_rc; | |
2432 | ||
111ebb6e | 2433 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
37c0eb46 SF |
2434 | mapping->writeback_index = index; |
2435 | ||
c7d38dbe PS |
2436 | if (cfile) |
2437 | cifsFileInfo_put(cfile); | |
0cb012d1 | 2438 | free_xid(xid); |
1da177e4 LT |
2439 | return rc; |
2440 | } | |
1da177e4 | 2441 | |
9ad1506b PS |
2442 | static int |
2443 | cifs_writepage_locked(struct page *page, struct writeback_control *wbc) | |
1da177e4 | 2444 | { |
9ad1506b | 2445 | int rc; |
6d5786a3 | 2446 | unsigned int xid; |
1da177e4 | 2447 | |
6d5786a3 | 2448 | xid = get_xid(); |
1da177e4 | 2449 | /* BB add check for wbc flags */ |
09cbfeaf | 2450 | get_page(page); |
ad7a2926 | 2451 | if (!PageUptodate(page)) |
f96637be | 2452 | cifs_dbg(FYI, "ppw - page not up to date\n"); |
cb876f45 LT |
2453 | |
2454 | /* | |
2455 | * Set the "writeback" flag, and clear "dirty" in the radix tree. | |
2456 | * | |
2457 | * A writepage() implementation always needs to do either this, | |
2458 | * or re-dirty the page with "redirty_page_for_writepage()" in | |
2459 | * the case of a failure. | |
2460 | * | |
2461 | * Just unlocking the page will cause the radix tree tag-bits | |
2462 | * to fail to update with the state of the page correctly. | |
2463 | */ | |
fb8c4b14 | 2464 | set_page_writeback(page); |
9ad1506b | 2465 | retry_write: |
09cbfeaf | 2466 | rc = cifs_partialpagewrite(page, 0, PAGE_SIZE); |
9a66396f PS |
2467 | if (is_retryable_error(rc)) { |
2468 | if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) | |
97b37f24 | 2469 | goto retry_write; |
9ad1506b | 2470 | redirty_page_for_writepage(wbc, page); |
97b37f24 | 2471 | } else if (rc != 0) { |
9ad1506b | 2472 | SetPageError(page); |
97b37f24 JL |
2473 | mapping_set_error(page->mapping, rc); |
2474 | } else { | |
9ad1506b | 2475 | SetPageUptodate(page); |
97b37f24 | 2476 | } |
cb876f45 | 2477 | end_page_writeback(page); |
09cbfeaf | 2478 | put_page(page); |
6d5786a3 | 2479 | free_xid(xid); |
1da177e4 LT |
2480 | return rc; |
2481 | } | |
2482 | ||
9ad1506b PS |
2483 | static int cifs_writepage(struct page *page, struct writeback_control *wbc) |
2484 | { | |
2485 | int rc = cifs_writepage_locked(page, wbc); | |
2486 | unlock_page(page); | |
2487 | return rc; | |
2488 | } | |
2489 | ||
d9414774 NP |
2490 | static int cifs_write_end(struct file *file, struct address_space *mapping, |
2491 | loff_t pos, unsigned len, unsigned copied, | |
2492 | struct page *page, void *fsdata) | |
1da177e4 | 2493 | { |
d9414774 NP |
2494 | int rc; |
2495 | struct inode *inode = mapping->host; | |
d4ffff1f PS |
2496 | struct cifsFileInfo *cfile = file->private_data; |
2497 | struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); | |
2498 | __u32 pid; | |
2499 | ||
2500 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) | |
2501 | pid = cfile->pid; | |
2502 | else | |
2503 | pid = current->tgid; | |
1da177e4 | 2504 | |
f96637be | 2505 | cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n", |
b6b38f70 | 2506 | page, pos, copied); |
d9414774 | 2507 | |
a98ee8c1 JL |
2508 | if (PageChecked(page)) { |
2509 | if (copied == len) | |
2510 | SetPageUptodate(page); | |
2511 | ClearPageChecked(page); | |
09cbfeaf | 2512 | } else if (!PageUptodate(page) && copied == PAGE_SIZE) |
d9414774 | 2513 | SetPageUptodate(page); |
ad7a2926 | 2514 | |
1da177e4 | 2515 | if (!PageUptodate(page)) { |
d9414774 | 2516 | char *page_data; |
09cbfeaf | 2517 | unsigned offset = pos & (PAGE_SIZE - 1); |
6d5786a3 | 2518 | unsigned int xid; |
d9414774 | 2519 | |
6d5786a3 | 2520 | xid = get_xid(); |
1da177e4 LT |
2521 | /* this is probably better than directly calling |
2522 | partialpage_write since in this function the file handle is | |
2523 | known which we might as well leverage */ | |
2524 | /* BB check if anything else missing out of ppw | |
2525 | such as updating last write time */ | |
2526 | page_data = kmap(page); | |
d4ffff1f | 2527 | rc = cifs_write(cfile, pid, page_data + offset, copied, &pos); |
d9414774 | 2528 | /* if (rc < 0) should we set writebehind rc? */ |
1da177e4 | 2529 | kunmap(page); |
d9414774 | 2530 | |
6d5786a3 | 2531 | free_xid(xid); |
fb8c4b14 | 2532 | } else { |
d9414774 NP |
2533 | rc = copied; |
2534 | pos += copied; | |
ca8aa29c | 2535 | set_page_dirty(page); |
1da177e4 LT |
2536 | } |
2537 | ||
d9414774 NP |
2538 | if (rc > 0) { |
2539 | spin_lock(&inode->i_lock); | |
2540 | if (pos > inode->i_size) | |
2541 | i_size_write(inode, pos); | |
2542 | spin_unlock(&inode->i_lock); | |
2543 | } | |
2544 | ||
2545 | unlock_page(page); | |
09cbfeaf | 2546 | put_page(page); |
d9414774 | 2547 | |
1da177e4 LT |
2548 | return rc; |
2549 | } | |
2550 | ||
02c24a82 JB |
2551 | int cifs_strict_fsync(struct file *file, loff_t start, loff_t end, |
2552 | int datasync) | |
1da177e4 | 2553 | { |
6d5786a3 | 2554 | unsigned int xid; |
1da177e4 | 2555 | int rc = 0; |
96daf2b0 | 2556 | struct cifs_tcon *tcon; |
1d8c4c00 | 2557 | struct TCP_Server_Info *server; |
c21dfb69 | 2558 | struct cifsFileInfo *smbfile = file->private_data; |
496ad9aa | 2559 | struct inode *inode = file_inode(file); |
8be7e6ba | 2560 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
1da177e4 | 2561 | |
3b49c9a1 | 2562 | rc = file_write_and_wait_range(file, start, end); |
02c24a82 JB |
2563 | if (rc) |
2564 | return rc; | |
02c24a82 | 2565 | |
6d5786a3 | 2566 | xid = get_xid(); |
1da177e4 | 2567 | |
35c265e0 AV |
2568 | cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n", |
2569 | file, datasync); | |
50c2f753 | 2570 | |
18cceb6a | 2571 | if (!CIFS_CACHE_READ(CIFS_I(inode))) { |
4f73c7d3 | 2572 | rc = cifs_zap_mapping(inode); |
6feb9891 | 2573 | if (rc) { |
f96637be | 2574 | cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc); |
6feb9891 PS |
2575 | rc = 0; /* don't care about it in fsync */ |
2576 | } | |
2577 | } | |
eb4b756b | 2578 | |
8be7e6ba | 2579 | tcon = tlink_tcon(smbfile->tlink); |
1d8c4c00 PS |
2580 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) { |
2581 | server = tcon->ses->server; | |
d11f93f4 | 2582 | if (server->ops->flush == NULL) { |
1d8c4c00 | 2583 | rc = -ENOSYS; |
d11f93f4 SF |
2584 | goto strict_fsync_exit; |
2585 | } | |
2586 | ||
2587 | if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) { | |
2588 | smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY); | |
2589 | if (smbfile) { | |
2590 | rc = server->ops->flush(xid, tcon, &smbfile->fid); | |
2591 | cifsFileInfo_put(smbfile); | |
2592 | } else | |
2593 | cifs_dbg(FYI, "ignore fsync for file not open for write\n"); | |
2594 | } else | |
2595 | rc = server->ops->flush(xid, tcon, &smbfile->fid); | |
1d8c4c00 | 2596 | } |
8be7e6ba | 2597 | |
d11f93f4 | 2598 | strict_fsync_exit: |
6d5786a3 | 2599 | free_xid(xid); |
8be7e6ba PS |
2600 | return rc; |
2601 | } | |
2602 | ||
02c24a82 | 2603 | int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
8be7e6ba | 2604 | { |
6d5786a3 | 2605 | unsigned int xid; |
8be7e6ba | 2606 | int rc = 0; |
96daf2b0 | 2607 | struct cifs_tcon *tcon; |
1d8c4c00 | 2608 | struct TCP_Server_Info *server; |
8be7e6ba | 2609 | struct cifsFileInfo *smbfile = file->private_data; |
d11f93f4 | 2610 | struct inode *inode = file_inode(file); |
7119e220 | 2611 | struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file); |
02c24a82 | 2612 | |
3b49c9a1 | 2613 | rc = file_write_and_wait_range(file, start, end); |
02c24a82 JB |
2614 | if (rc) |
2615 | return rc; | |
8be7e6ba | 2616 | |
6d5786a3 | 2617 | xid = get_xid(); |
8be7e6ba | 2618 | |
35c265e0 AV |
2619 | cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n", |
2620 | file, datasync); | |
8be7e6ba PS |
2621 | |
2622 | tcon = tlink_tcon(smbfile->tlink); | |
1d8c4c00 PS |
2623 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) { |
2624 | server = tcon->ses->server; | |
d11f93f4 | 2625 | if (server->ops->flush == NULL) { |
1d8c4c00 | 2626 | rc = -ENOSYS; |
d11f93f4 SF |
2627 | goto fsync_exit; |
2628 | } | |
2629 | ||
2630 | if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) { | |
2631 | smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY); | |
2632 | if (smbfile) { | |
2633 | rc = server->ops->flush(xid, tcon, &smbfile->fid); | |
2634 | cifsFileInfo_put(smbfile); | |
2635 | } else | |
2636 | cifs_dbg(FYI, "ignore fsync for file not open for write\n"); | |
2637 | } else | |
2638 | rc = server->ops->flush(xid, tcon, &smbfile->fid); | |
1d8c4c00 | 2639 | } |
b298f223 | 2640 | |
d11f93f4 | 2641 | fsync_exit: |
6d5786a3 | 2642 | free_xid(xid); |
1da177e4 LT |
2643 | return rc; |
2644 | } | |
2645 | ||
1da177e4 LT |
2646 | /* |
2647 | * As file closes, flush all cached write data for this inode checking | |
2648 | * for write behind errors. | |
2649 | */ | |
75e1fcc0 | 2650 | int cifs_flush(struct file *file, fl_owner_t id) |
1da177e4 | 2651 | { |
496ad9aa | 2652 | struct inode *inode = file_inode(file); |
1da177e4 LT |
2653 | int rc = 0; |
2654 | ||
eb4b756b | 2655 | if (file->f_mode & FMODE_WRITE) |
d3f1322a | 2656 | rc = filemap_write_and_wait(inode->i_mapping); |
50c2f753 | 2657 | |
f96637be | 2658 | cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc); |
1da177e4 LT |
2659 | |
2660 | return rc; | |
2661 | } | |
2662 | ||
72432ffc PS |
2663 | static int |
2664 | cifs_write_allocate_pages(struct page **pages, unsigned long num_pages) | |
2665 | { | |
2666 | int rc = 0; | |
2667 | unsigned long i; | |
2668 | ||
2669 | for (i = 0; i < num_pages; i++) { | |
e94f7ba1 | 2670 | pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM); |
72432ffc PS |
2671 | if (!pages[i]) { |
2672 | /* | |
2673 | * save number of pages we have already allocated and | |
2674 | * return with ENOMEM error | |
2675 | */ | |
2676 | num_pages = i; | |
2677 | rc = -ENOMEM; | |
e94f7ba1 | 2678 | break; |
72432ffc PS |
2679 | } |
2680 | } | |
2681 | ||
e94f7ba1 JL |
2682 | if (rc) { |
2683 | for (i = 0; i < num_pages; i++) | |
2684 | put_page(pages[i]); | |
2685 | } | |
72432ffc PS |
2686 | return rc; |
2687 | } | |
2688 | ||
2689 | static inline | |
2690 | size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len) | |
2691 | { | |
2692 | size_t num_pages; | |
2693 | size_t clen; | |
2694 | ||
2695 | clen = min_t(const size_t, len, wsize); | |
a7103b99 | 2696 | num_pages = DIV_ROUND_UP(clen, PAGE_SIZE); |
72432ffc PS |
2697 | |
2698 | if (cur_len) | |
2699 | *cur_len = clen; | |
2700 | ||
2701 | return num_pages; | |
2702 | } | |
2703 | ||
da82f7e7 | 2704 | static void |
4a5c80d7 | 2705 | cifs_uncached_writedata_release(struct kref *refcount) |
da82f7e7 JL |
2706 | { |
2707 | int i; | |
4a5c80d7 SF |
2708 | struct cifs_writedata *wdata = container_of(refcount, |
2709 | struct cifs_writedata, refcount); | |
2710 | ||
c610c4b6 | 2711 | kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release); |
4a5c80d7 SF |
2712 | for (i = 0; i < wdata->nr_pages; i++) |
2713 | put_page(wdata->pages[i]); | |
2714 | cifs_writedata_release(refcount); | |
2715 | } | |
2716 | ||
c610c4b6 PS |
2717 | static void collect_uncached_write_data(struct cifs_aio_ctx *ctx); |
2718 | ||
4a5c80d7 SF |
2719 | static void |
2720 | cifs_uncached_writev_complete(struct work_struct *work) | |
2721 | { | |
da82f7e7 JL |
2722 | struct cifs_writedata *wdata = container_of(work, |
2723 | struct cifs_writedata, work); | |
2b0143b5 | 2724 | struct inode *inode = d_inode(wdata->cfile->dentry); |
da82f7e7 JL |
2725 | struct cifsInodeInfo *cifsi = CIFS_I(inode); |
2726 | ||
2727 | spin_lock(&inode->i_lock); | |
2728 | cifs_update_eof(cifsi, wdata->offset, wdata->bytes); | |
2729 | if (cifsi->server_eof > inode->i_size) | |
2730 | i_size_write(inode, cifsi->server_eof); | |
2731 | spin_unlock(&inode->i_lock); | |
2732 | ||
2733 | complete(&wdata->done); | |
c610c4b6 PS |
2734 | collect_uncached_write_data(wdata->ctx); |
2735 | /* the below call can possibly free the last ref to aio ctx */ | |
4a5c80d7 | 2736 | kref_put(&wdata->refcount, cifs_uncached_writedata_release); |
da82f7e7 JL |
2737 | } |
2738 | ||
da82f7e7 | 2739 | static int |
66386c08 PS |
2740 | wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from, |
2741 | size_t *len, unsigned long *num_pages) | |
da82f7e7 | 2742 | { |
66386c08 PS |
2743 | size_t save_len, copied, bytes, cur_len = *len; |
2744 | unsigned long i, nr_pages = *num_pages; | |
c9de5c80 | 2745 | |
66386c08 PS |
2746 | save_len = cur_len; |
2747 | for (i = 0; i < nr_pages; i++) { | |
2748 | bytes = min_t(const size_t, cur_len, PAGE_SIZE); | |
2749 | copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from); | |
2750 | cur_len -= copied; | |
2751 | /* | |
2752 | * If we didn't copy as much as we expected, then that | |
2753 | * may mean we trod into an unmapped area. Stop copying | |
2754 | * at that point. On the next pass through the big | |
2755 | * loop, we'll likely end up getting a zero-length | |
2756 | * write and bailing out of it. | |
2757 | */ | |
2758 | if (copied < bytes) | |
2759 | break; | |
2760 | } | |
2761 | cur_len = save_len - cur_len; | |
2762 | *len = cur_len; | |
da82f7e7 | 2763 | |
66386c08 PS |
2764 | /* |
2765 | * If we have no data to send, then that probably means that | |
2766 | * the copy above failed altogether. That's most likely because | |
2767 | * the address in the iovec was bogus. Return -EFAULT and let | |
2768 | * the caller free anything we allocated and bail out. | |
2769 | */ | |
2770 | if (!cur_len) | |
2771 | return -EFAULT; | |
da82f7e7 | 2772 | |
66386c08 PS |
2773 | /* |
2774 | * i + 1 now represents the number of pages we actually used in | |
2775 | * the copy phase above. | |
2776 | */ | |
2777 | *num_pages = i + 1; | |
2778 | return 0; | |
da82f7e7 JL |
2779 | } |
2780 | ||
8c5f9c1a LL |
2781 | static int |
2782 | cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list, | |
2783 | struct cifs_aio_ctx *ctx) | |
2784 | { | |
335b7b62 PS |
2785 | unsigned int wsize; |
2786 | struct cifs_credits credits; | |
8c5f9c1a LL |
2787 | int rc; |
2788 | struct TCP_Server_Info *server = | |
2789 | tlink_tcon(wdata->cfile->tlink)->ses->server; | |
2790 | ||
8c5f9c1a | 2791 | do { |
d53e292f LL |
2792 | if (wdata->cfile->invalidHandle) { |
2793 | rc = cifs_reopen_file(wdata->cfile, false); | |
2794 | if (rc == -EAGAIN) | |
2795 | continue; | |
2796 | else if (rc) | |
2797 | break; | |
2798 | } | |
8c5f9c1a | 2799 | |
8c5f9c1a | 2800 | |
d53e292f LL |
2801 | /* |
2802 | * Wait for credits to resend this wdata. | |
2803 | * Note: we are attempting to resend the whole wdata not in | |
2804 | * segments | |
2805 | */ | |
2806 | do { | |
2807 | rc = server->ops->wait_mtu_credits(server, wdata->bytes, | |
2808 | &wsize, &credits); | |
2809 | if (rc) | |
2810 | goto fail; | |
2811 | ||
2812 | if (wsize < wdata->bytes) { | |
2813 | add_credits_and_wake_if(server, &credits, 0); | |
2814 | msleep(1000); | |
2815 | } | |
2816 | } while (wsize < wdata->bytes); | |
2817 | wdata->credits = credits; | |
8c5f9c1a | 2818 | |
d53e292f LL |
2819 | rc = adjust_credits(server, &wdata->credits, wdata->bytes); |
2820 | ||
2821 | if (!rc) { | |
2822 | if (wdata->cfile->invalidHandle) | |
2823 | rc = -EAGAIN; | |
2824 | else | |
2825 | rc = server->ops->async_writev(wdata, | |
8c5f9c1a | 2826 | cifs_uncached_writedata_release); |
d53e292f | 2827 | } |
8c5f9c1a | 2828 | |
d53e292f LL |
2829 | /* If the write was successfully sent, we are done */ |
2830 | if (!rc) { | |
2831 | list_add_tail(&wdata->list, wdata_list); | |
2832 | return 0; | |
2833 | } | |
8c5f9c1a | 2834 | |
d53e292f LL |
2835 | /* Roll back credits and retry if needed */ |
2836 | add_credits_and_wake_if(server, &wdata->credits, 0); | |
2837 | } while (rc == -EAGAIN); | |
8c5f9c1a | 2838 | |
d53e292f LL |
2839 | fail: |
2840 | kref_put(&wdata->refcount, cifs_uncached_writedata_release); | |
8c5f9c1a LL |
2841 | return rc; |
2842 | } | |
2843 | ||
43de94ea PS |
2844 | static int |
2845 | cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from, | |
2846 | struct cifsFileInfo *open_file, | |
c610c4b6 PS |
2847 | struct cifs_sb_info *cifs_sb, struct list_head *wdata_list, |
2848 | struct cifs_aio_ctx *ctx) | |
72432ffc | 2849 | { |
43de94ea PS |
2850 | int rc = 0; |
2851 | size_t cur_len; | |
66386c08 | 2852 | unsigned long nr_pages, num_pages, i; |
43de94ea | 2853 | struct cifs_writedata *wdata; |
fc56b983 | 2854 | struct iov_iter saved_from = *from; |
6ec0b01b | 2855 | loff_t saved_offset = offset; |
da82f7e7 | 2856 | pid_t pid; |
6ec0b01b | 2857 | struct TCP_Server_Info *server; |
8c5f9c1a LL |
2858 | struct page **pagevec; |
2859 | size_t start; | |
335b7b62 | 2860 | unsigned int xid; |
d4ffff1f PS |
2861 | |
2862 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) | |
2863 | pid = open_file->pid; | |
2864 | else | |
2865 | pid = current->tgid; | |
2866 | ||
6ec0b01b | 2867 | server = tlink_tcon(open_file->tlink)->ses->server; |
335b7b62 | 2868 | xid = get_xid(); |
6ec0b01b | 2869 | |
72432ffc | 2870 | do { |
335b7b62 PS |
2871 | unsigned int wsize; |
2872 | struct cifs_credits credits_on_stack; | |
2873 | struct cifs_credits *credits = &credits_on_stack; | |
cb7e9eab | 2874 | |
3e952994 PS |
2875 | if (open_file->invalidHandle) { |
2876 | rc = cifs_reopen_file(open_file, false); | |
2877 | if (rc == -EAGAIN) | |
2878 | continue; | |
2879 | else if (rc) | |
2880 | break; | |
2881 | } | |
2882 | ||
cb7e9eab | 2883 | rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize, |
335b7b62 | 2884 | &wsize, credits); |
cb7e9eab PS |
2885 | if (rc) |
2886 | break; | |
da82f7e7 | 2887 | |
b6bc8a7b LL |
2888 | cur_len = min_t(const size_t, len, wsize); |
2889 | ||
8c5f9c1a | 2890 | if (ctx->direct_io) { |
b98e26df SF |
2891 | ssize_t result; |
2892 | ||
2893 | result = iov_iter_get_pages_alloc( | |
b6bc8a7b | 2894 | from, &pagevec, cur_len, &start); |
b98e26df | 2895 | if (result < 0) { |
8c5f9c1a LL |
2896 | cifs_dbg(VFS, |
2897 | "direct_writev couldn't get user pages " | |
2898 | "(rc=%zd) iter type %d iov_offset %zd " | |
2899 | "count %zd\n", | |
b98e26df | 2900 | result, from->type, |
8c5f9c1a LL |
2901 | from->iov_offset, from->count); |
2902 | dump_stack(); | |
54e94ff9 LL |
2903 | |
2904 | rc = result; | |
2905 | add_credits_and_wake_if(server, credits, 0); | |
8c5f9c1a LL |
2906 | break; |
2907 | } | |
b98e26df | 2908 | cur_len = (size_t)result; |
8c5f9c1a LL |
2909 | iov_iter_advance(from, cur_len); |
2910 | ||
2911 | nr_pages = | |
2912 | (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE; | |
2913 | ||
2914 | wdata = cifs_writedata_direct_alloc(pagevec, | |
da82f7e7 | 2915 | cifs_uncached_writev_complete); |
8c5f9c1a LL |
2916 | if (!wdata) { |
2917 | rc = -ENOMEM; | |
2918 | add_credits_and_wake_if(server, credits, 0); | |
2919 | break; | |
2920 | } | |
da82f7e7 | 2921 | |
da82f7e7 | 2922 | |
8c5f9c1a LL |
2923 | wdata->page_offset = start; |
2924 | wdata->tailsz = | |
2925 | nr_pages > 1 ? | |
2926 | cur_len - (PAGE_SIZE - start) - | |
2927 | (nr_pages - 2) * PAGE_SIZE : | |
2928 | cur_len; | |
2929 | } else { | |
2930 | nr_pages = get_numpages(wsize, len, &cur_len); | |
2931 | wdata = cifs_writedata_alloc(nr_pages, | |
2932 | cifs_uncached_writev_complete); | |
2933 | if (!wdata) { | |
2934 | rc = -ENOMEM; | |
2935 | add_credits_and_wake_if(server, credits, 0); | |
2936 | break; | |
2937 | } | |
5d81de8e | 2938 | |
8c5f9c1a LL |
2939 | rc = cifs_write_allocate_pages(wdata->pages, nr_pages); |
2940 | if (rc) { | |
9bda8723 | 2941 | kvfree(wdata->pages); |
8c5f9c1a LL |
2942 | kfree(wdata); |
2943 | add_credits_and_wake_if(server, credits, 0); | |
2944 | break; | |
2945 | } | |
2946 | ||
2947 | num_pages = nr_pages; | |
2948 | rc = wdata_fill_from_iovec( | |
2949 | wdata, from, &cur_len, &num_pages); | |
2950 | if (rc) { | |
2951 | for (i = 0; i < nr_pages; i++) | |
2952 | put_page(wdata->pages[i]); | |
9bda8723 | 2953 | kvfree(wdata->pages); |
8c5f9c1a LL |
2954 | kfree(wdata); |
2955 | add_credits_and_wake_if(server, credits, 0); | |
2956 | break; | |
2957 | } | |
2958 | ||
2959 | /* | |
2960 | * Bring nr_pages down to the number of pages we | |
2961 | * actually used, and free any pages that we didn't use. | |
2962 | */ | |
2963 | for ( ; nr_pages > num_pages; nr_pages--) | |
2964 | put_page(wdata->pages[nr_pages - 1]); | |
2965 | ||
2966 | wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE); | |
2967 | } | |
5d81de8e | 2968 | |
da82f7e7 JL |
2969 | wdata->sync_mode = WB_SYNC_ALL; |
2970 | wdata->nr_pages = nr_pages; | |
2971 | wdata->offset = (__u64)offset; | |
2972 | wdata->cfile = cifsFileInfo_get(open_file); | |
2973 | wdata->pid = pid; | |
2974 | wdata->bytes = cur_len; | |
eddb079d | 2975 | wdata->pagesz = PAGE_SIZE; |
335b7b62 | 2976 | wdata->credits = credits_on_stack; |
c610c4b6 PS |
2977 | wdata->ctx = ctx; |
2978 | kref_get(&ctx->refcount); | |
6ec0b01b | 2979 | |
9a1c67e8 PS |
2980 | rc = adjust_credits(server, &wdata->credits, wdata->bytes); |
2981 | ||
2982 | if (!rc) { | |
2983 | if (wdata->cfile->invalidHandle) | |
3e952994 PS |
2984 | rc = -EAGAIN; |
2985 | else | |
9a1c67e8 | 2986 | rc = server->ops->async_writev(wdata, |
6ec0b01b | 2987 | cifs_uncached_writedata_release); |
9a1c67e8 PS |
2988 | } |
2989 | ||
da82f7e7 | 2990 | if (rc) { |
335b7b62 | 2991 | add_credits_and_wake_if(server, &wdata->credits, 0); |
4a5c80d7 SF |
2992 | kref_put(&wdata->refcount, |
2993 | cifs_uncached_writedata_release); | |
6ec0b01b | 2994 | if (rc == -EAGAIN) { |
fc56b983 | 2995 | *from = saved_from; |
6ec0b01b PS |
2996 | iov_iter_advance(from, offset - saved_offset); |
2997 | continue; | |
2998 | } | |
72432ffc PS |
2999 | break; |
3000 | } | |
3001 | ||
43de94ea | 3002 | list_add_tail(&wdata->list, wdata_list); |
da82f7e7 JL |
3003 | offset += cur_len; |
3004 | len -= cur_len; | |
72432ffc PS |
3005 | } while (len > 0); |
3006 | ||
335b7b62 | 3007 | free_xid(xid); |
43de94ea PS |
3008 | return rc; |
3009 | } | |
3010 | ||
c610c4b6 | 3011 | static void collect_uncached_write_data(struct cifs_aio_ctx *ctx) |
43de94ea | 3012 | { |
c610c4b6 | 3013 | struct cifs_writedata *wdata, *tmp; |
43de94ea PS |
3014 | struct cifs_tcon *tcon; |
3015 | struct cifs_sb_info *cifs_sb; | |
c610c4b6 | 3016 | struct dentry *dentry = ctx->cfile->dentry; |
79b6956e | 3017 | ssize_t rc; |
43de94ea | 3018 | |
c610c4b6 PS |
3019 | tcon = tlink_tcon(ctx->cfile->tlink); |
3020 | cifs_sb = CIFS_SB(dentry->d_sb); | |
43de94ea | 3021 | |
c610c4b6 | 3022 | mutex_lock(&ctx->aio_mutex); |
43de94ea | 3023 | |
c610c4b6 PS |
3024 | if (list_empty(&ctx->list)) { |
3025 | mutex_unlock(&ctx->aio_mutex); | |
3026 | return; | |
3027 | } | |
da82f7e7 | 3028 | |
c610c4b6 | 3029 | rc = ctx->rc; |
da82f7e7 JL |
3030 | /* |
3031 | * Wait for and collect replies for any successful sends in order of | |
c610c4b6 PS |
3032 | * increasing offset. Once an error is hit, then return without waiting |
3033 | * for any more replies. | |
da82f7e7 JL |
3034 | */ |
3035 | restart_loop: | |
c610c4b6 | 3036 | list_for_each_entry_safe(wdata, tmp, &ctx->list, list) { |
da82f7e7 | 3037 | if (!rc) { |
c610c4b6 PS |
3038 | if (!try_wait_for_completion(&wdata->done)) { |
3039 | mutex_unlock(&ctx->aio_mutex); | |
3040 | return; | |
3041 | } | |
3042 | ||
3043 | if (wdata->result) | |
da82f7e7 JL |
3044 | rc = wdata->result; |
3045 | else | |
c610c4b6 | 3046 | ctx->total_len += wdata->bytes; |
da82f7e7 JL |
3047 | |
3048 | /* resend call if it's a retryable error */ | |
3049 | if (rc == -EAGAIN) { | |
6ec0b01b | 3050 | struct list_head tmp_list; |
c610c4b6 | 3051 | struct iov_iter tmp_from = ctx->iter; |
6ec0b01b PS |
3052 | |
3053 | INIT_LIST_HEAD(&tmp_list); | |
3054 | list_del_init(&wdata->list); | |
3055 | ||
8c5f9c1a LL |
3056 | if (ctx->direct_io) |
3057 | rc = cifs_resend_wdata( | |
3058 | wdata, &tmp_list, ctx); | |
3059 | else { | |
3060 | iov_iter_advance(&tmp_from, | |
c610c4b6 | 3061 | wdata->offset - ctx->pos); |
6ec0b01b | 3062 | |
8c5f9c1a | 3063 | rc = cifs_write_from_iter(wdata->offset, |
6ec0b01b | 3064 | wdata->bytes, &tmp_from, |
c610c4b6 PS |
3065 | ctx->cfile, cifs_sb, &tmp_list, |
3066 | ctx); | |
d53e292f LL |
3067 | |
3068 | kref_put(&wdata->refcount, | |
3069 | cifs_uncached_writedata_release); | |
8c5f9c1a | 3070 | } |
6ec0b01b | 3071 | |
c610c4b6 | 3072 | list_splice(&tmp_list, &ctx->list); |
da82f7e7 JL |
3073 | goto restart_loop; |
3074 | } | |
3075 | } | |
3076 | list_del_init(&wdata->list); | |
4a5c80d7 | 3077 | kref_put(&wdata->refcount, cifs_uncached_writedata_release); |
72432ffc PS |
3078 | } |
3079 | ||
c610c4b6 PS |
3080 | cifs_stats_bytes_written(tcon, ctx->total_len); |
3081 | set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags); | |
3082 | ||
3083 | ctx->rc = (rc == 0) ? ctx->total_len : rc; | |
3084 | ||
3085 | mutex_unlock(&ctx->aio_mutex); | |
3086 | ||
3087 | if (ctx->iocb && ctx->iocb->ki_complete) | |
3088 | ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0); | |
3089 | else | |
3090 | complete(&ctx->done); | |
3091 | } | |
3092 | ||
8c5f9c1a LL |
3093 | static ssize_t __cifs_writev( |
3094 | struct kiocb *iocb, struct iov_iter *from, bool direct) | |
c610c4b6 PS |
3095 | { |
3096 | struct file *file = iocb->ki_filp; | |
3097 | ssize_t total_written = 0; | |
3098 | struct cifsFileInfo *cfile; | |
3099 | struct cifs_tcon *tcon; | |
3100 | struct cifs_sb_info *cifs_sb; | |
3101 | struct cifs_aio_ctx *ctx; | |
3102 | struct iov_iter saved_from = *from; | |
8c5f9c1a | 3103 | size_t len = iov_iter_count(from); |
c610c4b6 PS |
3104 | int rc; |
3105 | ||
3106 | /* | |
8c5f9c1a LL |
3107 | * iov_iter_get_pages_alloc doesn't work with ITER_KVEC. |
3108 | * In this case, fall back to non-direct write function. | |
3109 | * this could be improved by getting pages directly in ITER_KVEC | |
c610c4b6 | 3110 | */ |
8c5f9c1a LL |
3111 | if (direct && from->type & ITER_KVEC) { |
3112 | cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n"); | |
3113 | direct = false; | |
3114 | } | |
c610c4b6 PS |
3115 | |
3116 | rc = generic_write_checks(iocb, from); | |
3117 | if (rc <= 0) | |
3118 | return rc; | |
3119 | ||
3120 | cifs_sb = CIFS_FILE_SB(file); | |
3121 | cfile = file->private_data; | |
3122 | tcon = tlink_tcon(cfile->tlink); | |
3123 | ||
3124 | if (!tcon->ses->server->ops->async_writev) | |
3125 | return -ENOSYS; | |
3126 | ||
3127 | ctx = cifs_aio_ctx_alloc(); | |
3128 | if (!ctx) | |
3129 | return -ENOMEM; | |
3130 | ||
3131 | ctx->cfile = cifsFileInfo_get(cfile); | |
3132 | ||
3133 | if (!is_sync_kiocb(iocb)) | |
3134 | ctx->iocb = iocb; | |
3135 | ||
3136 | ctx->pos = iocb->ki_pos; | |
3137 | ||
8c5f9c1a LL |
3138 | if (direct) { |
3139 | ctx->direct_io = true; | |
3140 | ctx->iter = *from; | |
3141 | ctx->len = len; | |
3142 | } else { | |
3143 | rc = setup_aio_ctx_iter(ctx, from, WRITE); | |
3144 | if (rc) { | |
3145 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3146 | return rc; | |
3147 | } | |
c610c4b6 PS |
3148 | } |
3149 | ||
3150 | /* grab a lock here due to read response handlers can access ctx */ | |
3151 | mutex_lock(&ctx->aio_mutex); | |
3152 | ||
3153 | rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from, | |
3154 | cfile, cifs_sb, &ctx->list, ctx); | |
3155 | ||
3156 | /* | |
3157 | * If at least one write was successfully sent, then discard any rc | |
3158 | * value from the later writes. If the other write succeeds, then | |
3159 | * we'll end up returning whatever was written. If it fails, then | |
3160 | * we'll get a new rc value from that. | |
3161 | */ | |
3162 | if (!list_empty(&ctx->list)) | |
3163 | rc = 0; | |
3164 | ||
3165 | mutex_unlock(&ctx->aio_mutex); | |
3166 | ||
3167 | if (rc) { | |
3168 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3169 | return rc; | |
3170 | } | |
3171 | ||
3172 | if (!is_sync_kiocb(iocb)) { | |
3173 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3174 | return -EIOCBQUEUED; | |
3175 | } | |
3176 | ||
3177 | rc = wait_for_completion_killable(&ctx->done); | |
3178 | if (rc) { | |
3179 | mutex_lock(&ctx->aio_mutex); | |
3180 | ctx->rc = rc = -EINTR; | |
3181 | total_written = ctx->total_len; | |
3182 | mutex_unlock(&ctx->aio_mutex); | |
3183 | } else { | |
3184 | rc = ctx->rc; | |
3185 | total_written = ctx->total_len; | |
3186 | } | |
3187 | ||
3188 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3189 | ||
e9d1593d AV |
3190 | if (unlikely(!total_written)) |
3191 | return rc; | |
72432ffc | 3192 | |
e9d1593d | 3193 | iocb->ki_pos += total_written; |
e9d1593d | 3194 | return total_written; |
72432ffc PS |
3195 | } |
3196 | ||
8c5f9c1a LL |
3197 | ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from) |
3198 | { | |
3199 | return __cifs_writev(iocb, from, true); | |
3200 | } | |
3201 | ||
3202 | ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from) | |
3203 | { | |
3204 | return __cifs_writev(iocb, from, false); | |
3205 | } | |
3206 | ||
579f9053 | 3207 | static ssize_t |
3dae8750 | 3208 | cifs_writev(struct kiocb *iocb, struct iov_iter *from) |
72432ffc | 3209 | { |
579f9053 PS |
3210 | struct file *file = iocb->ki_filp; |
3211 | struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; | |
3212 | struct inode *inode = file->f_mapping->host; | |
3213 | struct cifsInodeInfo *cinode = CIFS_I(inode); | |
3214 | struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; | |
5f380c7f | 3215 | ssize_t rc; |
72432ffc | 3216 | |
966681c9 | 3217 | inode_lock(inode); |
579f9053 PS |
3218 | /* |
3219 | * We need to hold the sem to be sure nobody modifies lock list | |
3220 | * with a brlock that prevents writing. | |
3221 | */ | |
3222 | down_read(&cinode->lock_sem); | |
5f380c7f | 3223 | |
3309dd04 AV |
3224 | rc = generic_write_checks(iocb, from); |
3225 | if (rc <= 0) | |
5f380c7f AV |
3226 | goto out; |
3227 | ||
5f380c7f | 3228 | if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from), |
9645759c RS |
3229 | server->vals->exclusive_lock_type, 0, |
3230 | NULL, CIFS_WRITE_OP)) | |
3dae8750 | 3231 | rc = __generic_file_write_iter(iocb, from); |
5f380c7f AV |
3232 | else |
3233 | rc = -EACCES; | |
3234 | out: | |
966681c9 | 3235 | up_read(&cinode->lock_sem); |
5955102c | 3236 | inode_unlock(inode); |
19dfc1f5 | 3237 | |
e2592217 CH |
3238 | if (rc > 0) |
3239 | rc = generic_write_sync(iocb, rc); | |
579f9053 PS |
3240 | return rc; |
3241 | } | |
3242 | ||
3243 | ssize_t | |
3dae8750 | 3244 | cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from) |
579f9053 | 3245 | { |
496ad9aa | 3246 | struct inode *inode = file_inode(iocb->ki_filp); |
579f9053 PS |
3247 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
3248 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); | |
3249 | struct cifsFileInfo *cfile = (struct cifsFileInfo *) | |
3250 | iocb->ki_filp->private_data; | |
3251 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); | |
88cf75aa | 3252 | ssize_t written; |
ca8aa29c | 3253 | |
c11f1df5 SP |
3254 | written = cifs_get_writer(cinode); |
3255 | if (written) | |
3256 | return written; | |
3257 | ||
18cceb6a | 3258 | if (CIFS_CACHE_WRITE(cinode)) { |
88cf75aa PS |
3259 | if (cap_unix(tcon->ses) && |
3260 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) | |
c11f1df5 | 3261 | && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) { |
3dae8750 | 3262 | written = generic_file_write_iter(iocb, from); |
c11f1df5 SP |
3263 | goto out; |
3264 | } | |
3dae8750 | 3265 | written = cifs_writev(iocb, from); |
c11f1df5 | 3266 | goto out; |
25078105 | 3267 | } |
25078105 | 3268 | /* |
ca8aa29c PS |
3269 | * For non-oplocked files in strict cache mode we need to write the data |
3270 | * to the server exactly from the pos to pos+len-1 rather than flush all | |
3271 | * affected pages because it may cause a error with mandatory locks on | |
3272 | * these pages but not on the region from pos to ppos+len-1. | |
72432ffc | 3273 | */ |
3dae8750 | 3274 | written = cifs_user_writev(iocb, from); |
6dfbd846 | 3275 | if (CIFS_CACHE_READ(cinode)) { |
88cf75aa | 3276 | /* |
6dfbd846 PS |
3277 | * We have read level caching and we have just sent a write |
3278 | * request to the server thus making data in the cache stale. | |
3279 | * Zap the cache and set oplock/lease level to NONE to avoid | |
3280 | * reading stale data from the cache. All subsequent read | |
3281 | * operations will read new data from the server. | |
88cf75aa | 3282 | */ |
4f73c7d3 | 3283 | cifs_zap_mapping(inode); |
6dfbd846 | 3284 | cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n", |
f96637be | 3285 | inode); |
18cceb6a | 3286 | cinode->oplock = 0; |
88cf75aa | 3287 | } |
c11f1df5 SP |
3288 | out: |
3289 | cifs_put_writer(cinode); | |
88cf75aa | 3290 | return written; |
72432ffc PS |
3291 | } |
3292 | ||
0471ca3f | 3293 | static struct cifs_readdata * |
f9f5aca1 | 3294 | cifs_readdata_direct_alloc(struct page **pages, work_func_t complete) |
0471ca3f JL |
3295 | { |
3296 | struct cifs_readdata *rdata; | |
f4e49cd2 | 3297 | |
f9f5aca1 | 3298 | rdata = kzalloc(sizeof(*rdata), GFP_KERNEL); |
0471ca3f | 3299 | if (rdata != NULL) { |
f9f5aca1 | 3300 | rdata->pages = pages; |
6993f74a | 3301 | kref_init(&rdata->refcount); |
1c892549 JL |
3302 | INIT_LIST_HEAD(&rdata->list); |
3303 | init_completion(&rdata->done); | |
0471ca3f | 3304 | INIT_WORK(&rdata->work, complete); |
0471ca3f | 3305 | } |
f4e49cd2 | 3306 | |
0471ca3f JL |
3307 | return rdata; |
3308 | } | |
3309 | ||
f9f5aca1 LL |
3310 | static struct cifs_readdata * |
3311 | cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete) | |
3312 | { | |
3313 | struct page **pages = | |
6396bb22 | 3314 | kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL); |
f9f5aca1 LL |
3315 | struct cifs_readdata *ret = NULL; |
3316 | ||
3317 | if (pages) { | |
3318 | ret = cifs_readdata_direct_alloc(pages, complete); | |
3319 | if (!ret) | |
3320 | kfree(pages); | |
3321 | } | |
3322 | ||
3323 | return ret; | |
3324 | } | |
3325 | ||
6993f74a JL |
3326 | void |
3327 | cifs_readdata_release(struct kref *refcount) | |
0471ca3f | 3328 | { |
6993f74a JL |
3329 | struct cifs_readdata *rdata = container_of(refcount, |
3330 | struct cifs_readdata, refcount); | |
bd3dcc6a LL |
3331 | #ifdef CONFIG_CIFS_SMB_DIRECT |
3332 | if (rdata->mr) { | |
3333 | smbd_deregister_mr(rdata->mr); | |
3334 | rdata->mr = NULL; | |
3335 | } | |
3336 | #endif | |
6993f74a JL |
3337 | if (rdata->cfile) |
3338 | cifsFileInfo_put(rdata->cfile); | |
3339 | ||
f9f5aca1 | 3340 | kvfree(rdata->pages); |
0471ca3f JL |
3341 | kfree(rdata); |
3342 | } | |
3343 | ||
1c892549 | 3344 | static int |
c5fab6f4 | 3345 | cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages) |
1c892549 JL |
3346 | { |
3347 | int rc = 0; | |
c5fab6f4 | 3348 | struct page *page; |
1c892549 JL |
3349 | unsigned int i; |
3350 | ||
c5fab6f4 | 3351 | for (i = 0; i < nr_pages; i++) { |
1c892549 JL |
3352 | page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM); |
3353 | if (!page) { | |
3354 | rc = -ENOMEM; | |
3355 | break; | |
3356 | } | |
c5fab6f4 | 3357 | rdata->pages[i] = page; |
1c892549 JL |
3358 | } |
3359 | ||
3360 | if (rc) { | |
31fad7d4 RBC |
3361 | unsigned int nr_page_failed = i; |
3362 | ||
3363 | for (i = 0; i < nr_page_failed; i++) { | |
c5fab6f4 JL |
3364 | put_page(rdata->pages[i]); |
3365 | rdata->pages[i] = NULL; | |
1c892549 JL |
3366 | } |
3367 | } | |
3368 | return rc; | |
3369 | } | |
3370 | ||
3371 | static void | |
3372 | cifs_uncached_readdata_release(struct kref *refcount) | |
3373 | { | |
1c892549 JL |
3374 | struct cifs_readdata *rdata = container_of(refcount, |
3375 | struct cifs_readdata, refcount); | |
c5fab6f4 | 3376 | unsigned int i; |
1c892549 | 3377 | |
6685c5e2 | 3378 | kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release); |
c5fab6f4 JL |
3379 | for (i = 0; i < rdata->nr_pages; i++) { |
3380 | put_page(rdata->pages[i]); | |
1c892549 JL |
3381 | } |
3382 | cifs_readdata_release(refcount); | |
3383 | } | |
3384 | ||
1c892549 JL |
3385 | /** |
3386 | * cifs_readdata_to_iov - copy data from pages in response to an iovec | |
3387 | * @rdata: the readdata response with list of pages holding data | |
7f25bba8 | 3388 | * @iter: destination for our data |
1c892549 JL |
3389 | * |
3390 | * This function copies data from a list of pages in a readdata response into | |
3391 | * an array of iovecs. It will first calculate where the data should go | |
3392 | * based on the info in the readdata and then copy the data into that spot. | |
3393 | */ | |
7f25bba8 AV |
3394 | static int |
3395 | cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter) | |
1c892549 | 3396 | { |
34a54d61 | 3397 | size_t remaining = rdata->got_bytes; |
c5fab6f4 | 3398 | unsigned int i; |
1c892549 | 3399 | |
c5fab6f4 | 3400 | for (i = 0; i < rdata->nr_pages; i++) { |
c5fab6f4 | 3401 | struct page *page = rdata->pages[i]; |
e686bd8d | 3402 | size_t copy = min_t(size_t, remaining, PAGE_SIZE); |
9c25702c PS |
3403 | size_t written; |
3404 | ||
00e23707 | 3405 | if (unlikely(iov_iter_is_pipe(iter))) { |
9c25702c PS |
3406 | void *addr = kmap_atomic(page); |
3407 | ||
3408 | written = copy_to_iter(addr, copy, iter); | |
3409 | kunmap_atomic(addr); | |
3410 | } else | |
3411 | written = copy_page_to_iter(page, 0, copy, iter); | |
7f25bba8 AV |
3412 | remaining -= written; |
3413 | if (written < copy && iov_iter_count(iter) > 0) | |
3414 | break; | |
1c892549 | 3415 | } |
7f25bba8 | 3416 | return remaining ? -EFAULT : 0; |
1c892549 JL |
3417 | } |
3418 | ||
6685c5e2 PS |
3419 | static void collect_uncached_read_data(struct cifs_aio_ctx *ctx); |
3420 | ||
1c892549 JL |
3421 | static void |
3422 | cifs_uncached_readv_complete(struct work_struct *work) | |
3423 | { | |
3424 | struct cifs_readdata *rdata = container_of(work, | |
3425 | struct cifs_readdata, work); | |
1c892549 JL |
3426 | |
3427 | complete(&rdata->done); | |
6685c5e2 PS |
3428 | collect_uncached_read_data(rdata->ctx); |
3429 | /* the below call can possibly free the last ref to aio ctx */ | |
1c892549 JL |
3430 | kref_put(&rdata->refcount, cifs_uncached_readdata_release); |
3431 | } | |
3432 | ||
3433 | static int | |
d70b9104 PS |
3434 | uncached_fill_pages(struct TCP_Server_Info *server, |
3435 | struct cifs_readdata *rdata, struct iov_iter *iter, | |
3436 | unsigned int len) | |
1c892549 | 3437 | { |
b3160aeb | 3438 | int result = 0; |
c5fab6f4 JL |
3439 | unsigned int i; |
3440 | unsigned int nr_pages = rdata->nr_pages; | |
1dbe3466 | 3441 | unsigned int page_offset = rdata->page_offset; |
1c892549 | 3442 | |
b3160aeb | 3443 | rdata->got_bytes = 0; |
8321fec4 | 3444 | rdata->tailsz = PAGE_SIZE; |
c5fab6f4 JL |
3445 | for (i = 0; i < nr_pages; i++) { |
3446 | struct page *page = rdata->pages[i]; | |
71335664 | 3447 | size_t n; |
1dbe3466 LL |
3448 | unsigned int segment_size = rdata->pagesz; |
3449 | ||
3450 | if (i == 0) | |
3451 | segment_size -= page_offset; | |
3452 | else | |
3453 | page_offset = 0; | |
3454 | ||
c5fab6f4 | 3455 | |
71335664 | 3456 | if (len <= 0) { |
1c892549 | 3457 | /* no need to hold page hostage */ |
c5fab6f4 JL |
3458 | rdata->pages[i] = NULL; |
3459 | rdata->nr_pages--; | |
1c892549 | 3460 | put_page(page); |
8321fec4 | 3461 | continue; |
1c892549 | 3462 | } |
1dbe3466 | 3463 | |
71335664 | 3464 | n = len; |
1dbe3466 | 3465 | if (len >= segment_size) |
71335664 | 3466 | /* enough data to fill the page */ |
1dbe3466 LL |
3467 | n = segment_size; |
3468 | else | |
71335664 | 3469 | rdata->tailsz = len; |
1dbe3466 LL |
3470 | len -= n; |
3471 | ||
d70b9104 | 3472 | if (iter) |
1dbe3466 LL |
3473 | result = copy_page_from_iter( |
3474 | page, page_offset, n, iter); | |
bd3dcc6a LL |
3475 | #ifdef CONFIG_CIFS_SMB_DIRECT |
3476 | else if (rdata->mr) | |
3477 | result = n; | |
3478 | #endif | |
d70b9104 | 3479 | else |
1dbe3466 LL |
3480 | result = cifs_read_page_from_socket( |
3481 | server, page, page_offset, n); | |
8321fec4 JL |
3482 | if (result < 0) |
3483 | break; | |
3484 | ||
b3160aeb | 3485 | rdata->got_bytes += result; |
1c892549 JL |
3486 | } |
3487 | ||
b3160aeb PS |
3488 | return rdata->got_bytes > 0 && result != -ECONNABORTED ? |
3489 | rdata->got_bytes : result; | |
1c892549 JL |
3490 | } |
3491 | ||
d70b9104 PS |
3492 | static int |
3493 | cifs_uncached_read_into_pages(struct TCP_Server_Info *server, | |
3494 | struct cifs_readdata *rdata, unsigned int len) | |
3495 | { | |
3496 | return uncached_fill_pages(server, rdata, NULL, len); | |
3497 | } | |
3498 | ||
3499 | static int | |
3500 | cifs_uncached_copy_into_pages(struct TCP_Server_Info *server, | |
3501 | struct cifs_readdata *rdata, | |
3502 | struct iov_iter *iter) | |
3503 | { | |
3504 | return uncached_fill_pages(server, rdata, iter, iter->count); | |
3505 | } | |
3506 | ||
6e6e2b86 LL |
3507 | static int cifs_resend_rdata(struct cifs_readdata *rdata, |
3508 | struct list_head *rdata_list, | |
3509 | struct cifs_aio_ctx *ctx) | |
3510 | { | |
335b7b62 PS |
3511 | unsigned int rsize; |
3512 | struct cifs_credits credits; | |
6e6e2b86 LL |
3513 | int rc; |
3514 | struct TCP_Server_Info *server = | |
3515 | tlink_tcon(rdata->cfile->tlink)->ses->server; | |
3516 | ||
6e6e2b86 | 3517 | do { |
0b0dfd59 LL |
3518 | if (rdata->cfile->invalidHandle) { |
3519 | rc = cifs_reopen_file(rdata->cfile, true); | |
3520 | if (rc == -EAGAIN) | |
3521 | continue; | |
3522 | else if (rc) | |
3523 | break; | |
3524 | } | |
3525 | ||
3526 | /* | |
3527 | * Wait for credits to resend this rdata. | |
3528 | * Note: we are attempting to resend the whole rdata not in | |
3529 | * segments | |
3530 | */ | |
3531 | do { | |
3532 | rc = server->ops->wait_mtu_credits(server, rdata->bytes, | |
6e6e2b86 LL |
3533 | &rsize, &credits); |
3534 | ||
0b0dfd59 LL |
3535 | if (rc) |
3536 | goto fail; | |
6e6e2b86 | 3537 | |
0b0dfd59 LL |
3538 | if (rsize < rdata->bytes) { |
3539 | add_credits_and_wake_if(server, &credits, 0); | |
3540 | msleep(1000); | |
3541 | } | |
3542 | } while (rsize < rdata->bytes); | |
3543 | rdata->credits = credits; | |
6e6e2b86 | 3544 | |
0b0dfd59 LL |
3545 | rc = adjust_credits(server, &rdata->credits, rdata->bytes); |
3546 | if (!rc) { | |
3547 | if (rdata->cfile->invalidHandle) | |
3548 | rc = -EAGAIN; | |
3549 | else | |
3550 | rc = server->ops->async_readv(rdata); | |
3551 | } | |
6e6e2b86 | 3552 | |
0b0dfd59 LL |
3553 | /* If the read was successfully sent, we are done */ |
3554 | if (!rc) { | |
3555 | /* Add to aio pending list */ | |
3556 | list_add_tail(&rdata->list, rdata_list); | |
3557 | return 0; | |
3558 | } | |
6e6e2b86 | 3559 | |
0b0dfd59 LL |
3560 | /* Roll back credits and retry if needed */ |
3561 | add_credits_and_wake_if(server, &rdata->credits, 0); | |
3562 | } while (rc == -EAGAIN); | |
6e6e2b86 | 3563 | |
0b0dfd59 LL |
3564 | fail: |
3565 | kref_put(&rdata->refcount, cifs_uncached_readdata_release); | |
6e6e2b86 LL |
3566 | return rc; |
3567 | } | |
3568 | ||
0ada36b2 PS |
3569 | static int |
3570 | cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file, | |
6685c5e2 PS |
3571 | struct cifs_sb_info *cifs_sb, struct list_head *rdata_list, |
3572 | struct cifs_aio_ctx *ctx) | |
1da177e4 | 3573 | { |
0ada36b2 | 3574 | struct cifs_readdata *rdata; |
335b7b62 PS |
3575 | unsigned int npages, rsize; |
3576 | struct cifs_credits credits_on_stack; | |
3577 | struct cifs_credits *credits = &credits_on_stack; | |
0ada36b2 PS |
3578 | size_t cur_len; |
3579 | int rc; | |
1c892549 | 3580 | pid_t pid; |
25f40259 | 3581 | struct TCP_Server_Info *server; |
6e6e2b86 LL |
3582 | struct page **pagevec; |
3583 | size_t start; | |
3584 | struct iov_iter direct_iov = ctx->iter; | |
a70307ee | 3585 | |
25f40259 | 3586 | server = tlink_tcon(open_file->tlink)->ses->server; |
fc9c5966 | 3587 | |
d4ffff1f PS |
3588 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
3589 | pid = open_file->pid; | |
3590 | else | |
3591 | pid = current->tgid; | |
3592 | ||
6e6e2b86 LL |
3593 | if (ctx->direct_io) |
3594 | iov_iter_advance(&direct_iov, offset - ctx->pos); | |
3595 | ||
1c892549 | 3596 | do { |
3e952994 PS |
3597 | if (open_file->invalidHandle) { |
3598 | rc = cifs_reopen_file(open_file, true); | |
3599 | if (rc == -EAGAIN) | |
3600 | continue; | |
3601 | else if (rc) | |
3602 | break; | |
3603 | } | |
3604 | ||
bed9da02 | 3605 | rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize, |
335b7b62 | 3606 | &rsize, credits); |
bed9da02 PS |
3607 | if (rc) |
3608 | break; | |
3609 | ||
3610 | cur_len = min_t(const size_t, len, rsize); | |
a70307ee | 3611 | |
6e6e2b86 | 3612 | if (ctx->direct_io) { |
b98e26df | 3613 | ssize_t result; |
6e6e2b86 | 3614 | |
b98e26df | 3615 | result = iov_iter_get_pages_alloc( |
6e6e2b86 LL |
3616 | &direct_iov, &pagevec, |
3617 | cur_len, &start); | |
b98e26df | 3618 | if (result < 0) { |
6e6e2b86 | 3619 | cifs_dbg(VFS, |
54e94ff9 | 3620 | "couldn't get user pages (rc=%zd)" |
6e6e2b86 LL |
3621 | " iter type %d" |
3622 | " iov_offset %zd count %zd\n", | |
b98e26df | 3623 | result, direct_iov.type, |
6e6e2b86 LL |
3624 | direct_iov.iov_offset, |
3625 | direct_iov.count); | |
3626 | dump_stack(); | |
54e94ff9 LL |
3627 | |
3628 | rc = result; | |
3629 | add_credits_and_wake_if(server, credits, 0); | |
6e6e2b86 LL |
3630 | break; |
3631 | } | |
b98e26df | 3632 | cur_len = (size_t)result; |
6e6e2b86 LL |
3633 | iov_iter_advance(&direct_iov, cur_len); |
3634 | ||
3635 | rdata = cifs_readdata_direct_alloc( | |
3636 | pagevec, cifs_uncached_readv_complete); | |
3637 | if (!rdata) { | |
3638 | add_credits_and_wake_if(server, credits, 0); | |
3639 | rc = -ENOMEM; | |
3640 | break; | |
3641 | } | |
3642 | ||
3643 | npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE; | |
3644 | rdata->page_offset = start; | |
3645 | rdata->tailsz = npages > 1 ? | |
3646 | cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE : | |
3647 | cur_len; | |
3648 | ||
3649 | } else { | |
3650 | ||
3651 | npages = DIV_ROUND_UP(cur_len, PAGE_SIZE); | |
3652 | /* allocate a readdata struct */ | |
3653 | rdata = cifs_readdata_alloc(npages, | |
1c892549 | 3654 | cifs_uncached_readv_complete); |
6e6e2b86 LL |
3655 | if (!rdata) { |
3656 | add_credits_and_wake_if(server, credits, 0); | |
3657 | rc = -ENOMEM; | |
3658 | break; | |
3659 | } | |
a70307ee | 3660 | |
6e6e2b86 | 3661 | rc = cifs_read_allocate_pages(rdata, npages); |
9bda8723 PS |
3662 | if (rc) { |
3663 | kvfree(rdata->pages); | |
3664 | kfree(rdata); | |
3665 | add_credits_and_wake_if(server, credits, 0); | |
3666 | break; | |
3667 | } | |
6e6e2b86 LL |
3668 | |
3669 | rdata->tailsz = PAGE_SIZE; | |
3670 | } | |
1c892549 JL |
3671 | |
3672 | rdata->cfile = cifsFileInfo_get(open_file); | |
c5fab6f4 | 3673 | rdata->nr_pages = npages; |
1c892549 JL |
3674 | rdata->offset = offset; |
3675 | rdata->bytes = cur_len; | |
3676 | rdata->pid = pid; | |
8321fec4 JL |
3677 | rdata->pagesz = PAGE_SIZE; |
3678 | rdata->read_into_pages = cifs_uncached_read_into_pages; | |
d70b9104 | 3679 | rdata->copy_into_pages = cifs_uncached_copy_into_pages; |
335b7b62 | 3680 | rdata->credits = credits_on_stack; |
6685c5e2 PS |
3681 | rdata->ctx = ctx; |
3682 | kref_get(&ctx->refcount); | |
1c892549 | 3683 | |
9a1c67e8 PS |
3684 | rc = adjust_credits(server, &rdata->credits, rdata->bytes); |
3685 | ||
3686 | if (!rc) { | |
3687 | if (rdata->cfile->invalidHandle) | |
3e952994 PS |
3688 | rc = -EAGAIN; |
3689 | else | |
9a1c67e8 PS |
3690 | rc = server->ops->async_readv(rdata); |
3691 | } | |
3692 | ||
1c892549 | 3693 | if (rc) { |
335b7b62 | 3694 | add_credits_and_wake_if(server, &rdata->credits, 0); |
1c892549 | 3695 | kref_put(&rdata->refcount, |
6e6e2b86 LL |
3696 | cifs_uncached_readdata_release); |
3697 | if (rc == -EAGAIN) { | |
3698 | iov_iter_revert(&direct_iov, cur_len); | |
25f40259 | 3699 | continue; |
6e6e2b86 | 3700 | } |
1c892549 JL |
3701 | break; |
3702 | } | |
3703 | ||
0ada36b2 | 3704 | list_add_tail(&rdata->list, rdata_list); |
1c892549 JL |
3705 | offset += cur_len; |
3706 | len -= cur_len; | |
3707 | } while (len > 0); | |
3708 | ||
0ada36b2 PS |
3709 | return rc; |
3710 | } | |
3711 | ||
6685c5e2 PS |
3712 | static void |
3713 | collect_uncached_read_data(struct cifs_aio_ctx *ctx) | |
0ada36b2 | 3714 | { |
6685c5e2 PS |
3715 | struct cifs_readdata *rdata, *tmp; |
3716 | struct iov_iter *to = &ctx->iter; | |
0ada36b2 | 3717 | struct cifs_sb_info *cifs_sb; |
6685c5e2 | 3718 | int rc; |
0ada36b2 | 3719 | |
6685c5e2 | 3720 | cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb); |
0ada36b2 | 3721 | |
6685c5e2 | 3722 | mutex_lock(&ctx->aio_mutex); |
0ada36b2 | 3723 | |
6685c5e2 PS |
3724 | if (list_empty(&ctx->list)) { |
3725 | mutex_unlock(&ctx->aio_mutex); | |
3726 | return; | |
3727 | } | |
1c892549 | 3728 | |
6685c5e2 | 3729 | rc = ctx->rc; |
1c892549 | 3730 | /* the loop below should proceed in the order of increasing offsets */ |
25f40259 | 3731 | again: |
6685c5e2 | 3732 | list_for_each_entry_safe(rdata, tmp, &ctx->list, list) { |
1c892549 | 3733 | if (!rc) { |
6685c5e2 PS |
3734 | if (!try_wait_for_completion(&rdata->done)) { |
3735 | mutex_unlock(&ctx->aio_mutex); | |
3736 | return; | |
3737 | } | |
3738 | ||
3739 | if (rdata->result == -EAGAIN) { | |
74027f4a | 3740 | /* resend call if it's a retryable error */ |
fb8a3e52 | 3741 | struct list_head tmp_list; |
d913ed17 | 3742 | unsigned int got_bytes = rdata->got_bytes; |
25f40259 | 3743 | |
fb8a3e52 PS |
3744 | list_del_init(&rdata->list); |
3745 | INIT_LIST_HEAD(&tmp_list); | |
25f40259 | 3746 | |
d913ed17 PS |
3747 | /* |
3748 | * Got a part of data and then reconnect has | |
3749 | * happened -- fill the buffer and continue | |
3750 | * reading. | |
3751 | */ | |
3752 | if (got_bytes && got_bytes < rdata->bytes) { | |
6e6e2b86 LL |
3753 | rc = 0; |
3754 | if (!ctx->direct_io) | |
3755 | rc = cifs_readdata_to_iov(rdata, to); | |
d913ed17 PS |
3756 | if (rc) { |
3757 | kref_put(&rdata->refcount, | |
6e6e2b86 | 3758 | cifs_uncached_readdata_release); |
d913ed17 PS |
3759 | continue; |
3760 | } | |
74027f4a | 3761 | } |
d913ed17 | 3762 | |
6e6e2b86 LL |
3763 | if (ctx->direct_io) { |
3764 | /* | |
3765 | * Re-use rdata as this is a | |
3766 | * direct I/O | |
3767 | */ | |
3768 | rc = cifs_resend_rdata( | |
3769 | rdata, | |
3770 | &tmp_list, ctx); | |
3771 | } else { | |
3772 | rc = cifs_send_async_read( | |
d913ed17 PS |
3773 | rdata->offset + got_bytes, |
3774 | rdata->bytes - got_bytes, | |
3775 | rdata->cfile, cifs_sb, | |
6685c5e2 | 3776 | &tmp_list, ctx); |
25f40259 | 3777 | |
6e6e2b86 LL |
3778 | kref_put(&rdata->refcount, |
3779 | cifs_uncached_readdata_release); | |
3780 | } | |
3781 | ||
6685c5e2 | 3782 | list_splice(&tmp_list, &ctx->list); |
25f40259 | 3783 | |
fb8a3e52 PS |
3784 | goto again; |
3785 | } else if (rdata->result) | |
3786 | rc = rdata->result; | |
6e6e2b86 | 3787 | else if (!ctx->direct_io) |
e6a7bcb4 | 3788 | rc = cifs_readdata_to_iov(rdata, to); |
1c892549 | 3789 | |
2e8a05d8 PS |
3790 | /* if there was a short read -- discard anything left */ |
3791 | if (rdata->got_bytes && rdata->got_bytes < rdata->bytes) | |
3792 | rc = -ENODATA; | |
6e6e2b86 LL |
3793 | |
3794 | ctx->total_len += rdata->got_bytes; | |
1da177e4 | 3795 | } |
1c892549 JL |
3796 | list_del_init(&rdata->list); |
3797 | kref_put(&rdata->refcount, cifs_uncached_readdata_release); | |
1da177e4 | 3798 | } |
a70307ee | 3799 | |
13f5938d | 3800 | if (!ctx->direct_io) |
6e6e2b86 | 3801 | ctx->total_len = ctx->len - iov_iter_count(to); |
6685c5e2 | 3802 | |
09a4707e PS |
3803 | /* mask nodata case */ |
3804 | if (rc == -ENODATA) | |
3805 | rc = 0; | |
3806 | ||
9e731ddf | 3807 | ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc; |
6685c5e2 PS |
3808 | |
3809 | mutex_unlock(&ctx->aio_mutex); | |
3810 | ||
3811 | if (ctx->iocb && ctx->iocb->ki_complete) | |
3812 | ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0); | |
3813 | else | |
3814 | complete(&ctx->done); | |
3815 | } | |
3816 | ||
6e6e2b86 LL |
3817 | static ssize_t __cifs_readv( |
3818 | struct kiocb *iocb, struct iov_iter *to, bool direct) | |
6685c5e2 | 3819 | { |
6685c5e2 | 3820 | size_t len; |
6e6e2b86 | 3821 | struct file *file = iocb->ki_filp; |
6685c5e2 | 3822 | struct cifs_sb_info *cifs_sb; |
6685c5e2 | 3823 | struct cifsFileInfo *cfile; |
6e6e2b86 LL |
3824 | struct cifs_tcon *tcon; |
3825 | ssize_t rc, total_read = 0; | |
3826 | loff_t offset = iocb->ki_pos; | |
6685c5e2 PS |
3827 | struct cifs_aio_ctx *ctx; |
3828 | ||
6e6e2b86 LL |
3829 | /* |
3830 | * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC, | |
3831 | * fall back to data copy read path | |
3832 | * this could be improved by getting pages directly in ITER_KVEC | |
3833 | */ | |
3834 | if (direct && to->type & ITER_KVEC) { | |
3835 | cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n"); | |
3836 | direct = false; | |
3837 | } | |
3838 | ||
6685c5e2 PS |
3839 | len = iov_iter_count(to); |
3840 | if (!len) | |
3841 | return 0; | |
3842 | ||
3843 | cifs_sb = CIFS_FILE_SB(file); | |
3844 | cfile = file->private_data; | |
3845 | tcon = tlink_tcon(cfile->tlink); | |
3846 | ||
3847 | if (!tcon->ses->server->ops->async_readv) | |
3848 | return -ENOSYS; | |
3849 | ||
3850 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) | |
3851 | cifs_dbg(FYI, "attempting read on write only file instance\n"); | |
3852 | ||
3853 | ctx = cifs_aio_ctx_alloc(); | |
3854 | if (!ctx) | |
3855 | return -ENOMEM; | |
3856 | ||
3857 | ctx->cfile = cifsFileInfo_get(cfile); | |
3858 | ||
3859 | if (!is_sync_kiocb(iocb)) | |
3860 | ctx->iocb = iocb; | |
3861 | ||
00e23707 | 3862 | if (iter_is_iovec(to)) |
6685c5e2 PS |
3863 | ctx->should_dirty = true; |
3864 | ||
6e6e2b86 LL |
3865 | if (direct) { |
3866 | ctx->pos = offset; | |
3867 | ctx->direct_io = true; | |
3868 | ctx->iter = *to; | |
3869 | ctx->len = len; | |
3870 | } else { | |
3871 | rc = setup_aio_ctx_iter(ctx, to, READ); | |
3872 | if (rc) { | |
3873 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3874 | return rc; | |
3875 | } | |
3876 | len = ctx->len; | |
6685c5e2 PS |
3877 | } |
3878 | ||
6685c5e2 PS |
3879 | /* grab a lock here due to read response handlers can access ctx */ |
3880 | mutex_lock(&ctx->aio_mutex); | |
3881 | ||
3882 | rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx); | |
3883 | ||
3884 | /* if at least one read request send succeeded, then reset rc */ | |
3885 | if (!list_empty(&ctx->list)) | |
3886 | rc = 0; | |
3887 | ||
3888 | mutex_unlock(&ctx->aio_mutex); | |
3889 | ||
3890 | if (rc) { | |
3891 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3892 | return rc; | |
3893 | } | |
3894 | ||
3895 | if (!is_sync_kiocb(iocb)) { | |
3896 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3897 | return -EIOCBQUEUED; | |
3898 | } | |
3899 | ||
3900 | rc = wait_for_completion_killable(&ctx->done); | |
3901 | if (rc) { | |
3902 | mutex_lock(&ctx->aio_mutex); | |
3903 | ctx->rc = rc = -EINTR; | |
3904 | total_read = ctx->total_len; | |
3905 | mutex_unlock(&ctx->aio_mutex); | |
3906 | } else { | |
3907 | rc = ctx->rc; | |
3908 | total_read = ctx->total_len; | |
3909 | } | |
3910 | ||
3911 | kref_put(&ctx->refcount, cifs_aio_ctx_release); | |
3912 | ||
0165e810 | 3913 | if (total_read) { |
e6a7bcb4 | 3914 | iocb->ki_pos += total_read; |
0165e810 AV |
3915 | return total_read; |
3916 | } | |
3917 | return rc; | |
a70307ee PS |
3918 | } |
3919 | ||
6e6e2b86 LL |
3920 | ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to) |
3921 | { | |
3922 | return __cifs_readv(iocb, to, true); | |
3923 | } | |
3924 | ||
3925 | ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to) | |
3926 | { | |
3927 | return __cifs_readv(iocb, to, false); | |
3928 | } | |
3929 | ||
579f9053 | 3930 | ssize_t |
e6a7bcb4 | 3931 | cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to) |
a70307ee | 3932 | { |
496ad9aa | 3933 | struct inode *inode = file_inode(iocb->ki_filp); |
579f9053 PS |
3934 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
3935 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); | |
3936 | struct cifsFileInfo *cfile = (struct cifsFileInfo *) | |
3937 | iocb->ki_filp->private_data; | |
3938 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); | |
3939 | int rc = -EACCES; | |
a70307ee PS |
3940 | |
3941 | /* | |
3942 | * In strict cache mode we need to read from the server all the time | |
3943 | * if we don't have level II oplock because the server can delay mtime | |
3944 | * change - so we can't make a decision about inode invalidating. | |
3945 | * And we can also fail with pagereading if there are mandatory locks | |
3946 | * on pages affected by this read but not on the region from pos to | |
3947 | * pos+len-1. | |
3948 | */ | |
18cceb6a | 3949 | if (!CIFS_CACHE_READ(cinode)) |
e6a7bcb4 | 3950 | return cifs_user_readv(iocb, to); |
a70307ee | 3951 | |
579f9053 PS |
3952 | if (cap_unix(tcon->ses) && |
3953 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && | |
3954 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) | |
e6a7bcb4 | 3955 | return generic_file_read_iter(iocb, to); |
579f9053 PS |
3956 | |
3957 | /* | |
3958 | * We need to hold the sem to be sure nobody modifies lock list | |
3959 | * with a brlock that prevents reading. | |
3960 | */ | |
3961 | down_read(&cinode->lock_sem); | |
e6a7bcb4 | 3962 | if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to), |
579f9053 | 3963 | tcon->ses->server->vals->shared_lock_type, |
9645759c | 3964 | 0, NULL, CIFS_READ_OP)) |
e6a7bcb4 | 3965 | rc = generic_file_read_iter(iocb, to); |
579f9053 PS |
3966 | up_read(&cinode->lock_sem); |
3967 | return rc; | |
a70307ee | 3968 | } |
1da177e4 | 3969 | |
f9c6e234 PS |
3970 | static ssize_t |
3971 | cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset) | |
1da177e4 LT |
3972 | { |
3973 | int rc = -EACCES; | |
3974 | unsigned int bytes_read = 0; | |
3975 | unsigned int total_read; | |
3976 | unsigned int current_read_size; | |
5eba8ab3 | 3977 | unsigned int rsize; |
1da177e4 | 3978 | struct cifs_sb_info *cifs_sb; |
29e20f9c | 3979 | struct cifs_tcon *tcon; |
f9c6e234 | 3980 | struct TCP_Server_Info *server; |
6d5786a3 | 3981 | unsigned int xid; |
f9c6e234 | 3982 | char *cur_offset; |
1da177e4 | 3983 | struct cifsFileInfo *open_file; |
d4ffff1f | 3984 | struct cifs_io_parms io_parms; |
ec637e3f | 3985 | int buf_type = CIFS_NO_BUFFER; |
d4ffff1f | 3986 | __u32 pid; |
1da177e4 | 3987 | |
6d5786a3 | 3988 | xid = get_xid(); |
7119e220 | 3989 | cifs_sb = CIFS_FILE_SB(file); |
1da177e4 | 3990 | |
5eba8ab3 JL |
3991 | /* FIXME: set up handlers for larger reads and/or convert to async */ |
3992 | rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize); | |
3993 | ||
1da177e4 | 3994 | if (file->private_data == NULL) { |
0f3bc09e | 3995 | rc = -EBADF; |
6d5786a3 | 3996 | free_xid(xid); |
0f3bc09e | 3997 | return rc; |
1da177e4 | 3998 | } |
c21dfb69 | 3999 | open_file = file->private_data; |
29e20f9c | 4000 | tcon = tlink_tcon(open_file->tlink); |
f9c6e234 PS |
4001 | server = tcon->ses->server; |
4002 | ||
4003 | if (!server->ops->sync_read) { | |
4004 | free_xid(xid); | |
4005 | return -ENOSYS; | |
4006 | } | |
1da177e4 | 4007 | |
d4ffff1f PS |
4008 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
4009 | pid = open_file->pid; | |
4010 | else | |
4011 | pid = current->tgid; | |
4012 | ||
1da177e4 | 4013 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) |
f96637be | 4014 | cifs_dbg(FYI, "attempting read on write only file instance\n"); |
1da177e4 | 4015 | |
f9c6e234 PS |
4016 | for (total_read = 0, cur_offset = read_data; read_size > total_read; |
4017 | total_read += bytes_read, cur_offset += bytes_read) { | |
e374d90f PS |
4018 | do { |
4019 | current_read_size = min_t(uint, read_size - total_read, | |
4020 | rsize); | |
4021 | /* | |
4022 | * For windows me and 9x we do not want to request more | |
4023 | * than it negotiated since it will refuse the read | |
4024 | * then. | |
4025 | */ | |
d38b6d46 | 4026 | if (!(tcon->ses->capabilities & |
29e20f9c | 4027 | tcon->ses->server->vals->cap_large_files)) { |
e374d90f PS |
4028 | current_read_size = min_t(uint, |
4029 | current_read_size, CIFSMaxBufSize); | |
4030 | } | |
cdff08e7 | 4031 | if (open_file->invalidHandle) { |
15886177 | 4032 | rc = cifs_reopen_file(open_file, true); |
1da177e4 LT |
4033 | if (rc != 0) |
4034 | break; | |
4035 | } | |
d4ffff1f | 4036 | io_parms.pid = pid; |
29e20f9c | 4037 | io_parms.tcon = tcon; |
f9c6e234 | 4038 | io_parms.offset = *offset; |
d4ffff1f | 4039 | io_parms.length = current_read_size; |
db8b631d | 4040 | rc = server->ops->sync_read(xid, &open_file->fid, &io_parms, |
f9c6e234 PS |
4041 | &bytes_read, &cur_offset, |
4042 | &buf_type); | |
e374d90f PS |
4043 | } while (rc == -EAGAIN); |
4044 | ||
1da177e4 LT |
4045 | if (rc || (bytes_read == 0)) { |
4046 | if (total_read) { | |
4047 | break; | |
4048 | } else { | |
6d5786a3 | 4049 | free_xid(xid); |
1da177e4 LT |
4050 | return rc; |
4051 | } | |
4052 | } else { | |
29e20f9c | 4053 | cifs_stats_bytes_read(tcon, total_read); |
f9c6e234 | 4054 | *offset += bytes_read; |
1da177e4 LT |
4055 | } |
4056 | } | |
6d5786a3 | 4057 | free_xid(xid); |
1da177e4 LT |
4058 | return total_read; |
4059 | } | |
4060 | ||
ca83ce3d JL |
4061 | /* |
4062 | * If the page is mmap'ed into a process' page tables, then we need to make | |
4063 | * sure that it doesn't change while being written back. | |
4064 | */ | |
a5240cbd | 4065 | static vm_fault_t |
11bac800 | 4066 | cifs_page_mkwrite(struct vm_fault *vmf) |
ca83ce3d JL |
4067 | { |
4068 | struct page *page = vmf->page; | |
4069 | ||
4070 | lock_page(page); | |
4071 | return VM_FAULT_LOCKED; | |
4072 | } | |
4073 | ||
7cbea8dc | 4074 | static const struct vm_operations_struct cifs_file_vm_ops = { |
ca83ce3d | 4075 | .fault = filemap_fault, |
f1820361 | 4076 | .map_pages = filemap_map_pages, |
ca83ce3d JL |
4077 | .page_mkwrite = cifs_page_mkwrite, |
4078 | }; | |
4079 | ||
7a6a19b1 PS |
4080 | int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma) |
4081 | { | |
f04a703c | 4082 | int xid, rc = 0; |
496ad9aa | 4083 | struct inode *inode = file_inode(file); |
7a6a19b1 | 4084 | |
6d5786a3 | 4085 | xid = get_xid(); |
7a6a19b1 | 4086 | |
f04a703c | 4087 | if (!CIFS_CACHE_READ(CIFS_I(inode))) |
4f73c7d3 | 4088 | rc = cifs_zap_mapping(inode); |
f04a703c MW |
4089 | if (!rc) |
4090 | rc = generic_file_mmap(file, vma); | |
4091 | if (!rc) | |
ca83ce3d | 4092 | vma->vm_ops = &cifs_file_vm_ops; |
f04a703c | 4093 | |
6d5786a3 | 4094 | free_xid(xid); |
7a6a19b1 PS |
4095 | return rc; |
4096 | } | |
4097 | ||
1da177e4 LT |
4098 | int cifs_file_mmap(struct file *file, struct vm_area_struct *vma) |
4099 | { | |
1da177e4 LT |
4100 | int rc, xid; |
4101 | ||
6d5786a3 | 4102 | xid = get_xid(); |
f04a703c | 4103 | |
abab095d | 4104 | rc = cifs_revalidate_file(file); |
f04a703c | 4105 | if (rc) |
f96637be JP |
4106 | cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n", |
4107 | rc); | |
f04a703c MW |
4108 | if (!rc) |
4109 | rc = generic_file_mmap(file, vma); | |
4110 | if (!rc) | |
ca83ce3d | 4111 | vma->vm_ops = &cifs_file_vm_ops; |
f04a703c | 4112 | |
6d5786a3 | 4113 | free_xid(xid); |
1da177e4 LT |
4114 | return rc; |
4115 | } | |
4116 | ||
0471ca3f JL |
4117 | static void |
4118 | cifs_readv_complete(struct work_struct *work) | |
4119 | { | |
b770ddfa | 4120 | unsigned int i, got_bytes; |
0471ca3f JL |
4121 | struct cifs_readdata *rdata = container_of(work, |
4122 | struct cifs_readdata, work); | |
0471ca3f | 4123 | |
b770ddfa | 4124 | got_bytes = rdata->got_bytes; |
c5fab6f4 JL |
4125 | for (i = 0; i < rdata->nr_pages; i++) { |
4126 | struct page *page = rdata->pages[i]; | |
4127 | ||
0471ca3f JL |
4128 | lru_cache_add_file(page); |
4129 | ||
b770ddfa PS |
4130 | if (rdata->result == 0 || |
4131 | (rdata->result == -EAGAIN && got_bytes)) { | |
0471ca3f JL |
4132 | flush_dcache_page(page); |
4133 | SetPageUptodate(page); | |
4134 | } | |
4135 | ||
4136 | unlock_page(page); | |
4137 | ||
b770ddfa PS |
4138 | if (rdata->result == 0 || |
4139 | (rdata->result == -EAGAIN && got_bytes)) | |
0471ca3f JL |
4140 | cifs_readpage_to_fscache(rdata->mapping->host, page); |
4141 | ||
09cbfeaf | 4142 | got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes); |
b770ddfa | 4143 | |
09cbfeaf | 4144 | put_page(page); |
c5fab6f4 | 4145 | rdata->pages[i] = NULL; |
0471ca3f | 4146 | } |
6993f74a | 4147 | kref_put(&rdata->refcount, cifs_readdata_release); |
0471ca3f JL |
4148 | } |
4149 | ||
8d5ce4d2 | 4150 | static int |
d70b9104 PS |
4151 | readpages_fill_pages(struct TCP_Server_Info *server, |
4152 | struct cifs_readdata *rdata, struct iov_iter *iter, | |
4153 | unsigned int len) | |
8d5ce4d2 | 4154 | { |
b3160aeb | 4155 | int result = 0; |
c5fab6f4 | 4156 | unsigned int i; |
8d5ce4d2 JL |
4157 | u64 eof; |
4158 | pgoff_t eof_index; | |
c5fab6f4 | 4159 | unsigned int nr_pages = rdata->nr_pages; |
1dbe3466 | 4160 | unsigned int page_offset = rdata->page_offset; |
8d5ce4d2 JL |
4161 | |
4162 | /* determine the eof that the server (probably) has */ | |
4163 | eof = CIFS_I(rdata->mapping->host)->server_eof; | |
09cbfeaf | 4164 | eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0; |
f96637be | 4165 | cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index); |
8d5ce4d2 | 4166 | |
b3160aeb | 4167 | rdata->got_bytes = 0; |
09cbfeaf | 4168 | rdata->tailsz = PAGE_SIZE; |
c5fab6f4 JL |
4169 | for (i = 0; i < nr_pages; i++) { |
4170 | struct page *page = rdata->pages[i]; | |
1dbe3466 LL |
4171 | unsigned int to_read = rdata->pagesz; |
4172 | size_t n; | |
4173 | ||
4174 | if (i == 0) | |
4175 | to_read -= page_offset; | |
4176 | else | |
4177 | page_offset = 0; | |
4178 | ||
4179 | n = to_read; | |
c5fab6f4 | 4180 | |
1dbe3466 LL |
4181 | if (len >= to_read) { |
4182 | len -= to_read; | |
8321fec4 | 4183 | } else if (len > 0) { |
8d5ce4d2 | 4184 | /* enough for partial page, fill and zero the rest */ |
1dbe3466 | 4185 | zero_user(page, len + page_offset, to_read - len); |
71335664 | 4186 | n = rdata->tailsz = len; |
8321fec4 | 4187 | len = 0; |
8d5ce4d2 JL |
4188 | } else if (page->index > eof_index) { |
4189 | /* | |
4190 | * The VFS will not try to do readahead past the | |
4191 | * i_size, but it's possible that we have outstanding | |
4192 | * writes with gaps in the middle and the i_size hasn't | |
4193 | * caught up yet. Populate those with zeroed out pages | |
4194 | * to prevent the VFS from repeatedly attempting to | |
4195 | * fill them until the writes are flushed. | |
4196 | */ | |
09cbfeaf | 4197 | zero_user(page, 0, PAGE_SIZE); |
8d5ce4d2 JL |
4198 | lru_cache_add_file(page); |
4199 | flush_dcache_page(page); | |
4200 | SetPageUptodate(page); | |
4201 | unlock_page(page); | |
09cbfeaf | 4202 | put_page(page); |
c5fab6f4 JL |
4203 | rdata->pages[i] = NULL; |
4204 | rdata->nr_pages--; | |
8321fec4 | 4205 | continue; |
8d5ce4d2 JL |
4206 | } else { |
4207 | /* no need to hold page hostage */ | |
8d5ce4d2 JL |
4208 | lru_cache_add_file(page); |
4209 | unlock_page(page); | |
09cbfeaf | 4210 | put_page(page); |
c5fab6f4 JL |
4211 | rdata->pages[i] = NULL; |
4212 | rdata->nr_pages--; | |
8321fec4 | 4213 | continue; |
8d5ce4d2 | 4214 | } |
8321fec4 | 4215 | |
d70b9104 | 4216 | if (iter) |
1dbe3466 LL |
4217 | result = copy_page_from_iter( |
4218 | page, page_offset, n, iter); | |
bd3dcc6a LL |
4219 | #ifdef CONFIG_CIFS_SMB_DIRECT |
4220 | else if (rdata->mr) | |
4221 | result = n; | |
4222 | #endif | |
d70b9104 | 4223 | else |
1dbe3466 LL |
4224 | result = cifs_read_page_from_socket( |
4225 | server, page, page_offset, n); | |
8321fec4 JL |
4226 | if (result < 0) |
4227 | break; | |
4228 | ||
b3160aeb | 4229 | rdata->got_bytes += result; |
8d5ce4d2 JL |
4230 | } |
4231 | ||
b3160aeb PS |
4232 | return rdata->got_bytes > 0 && result != -ECONNABORTED ? |
4233 | rdata->got_bytes : result; | |
8d5ce4d2 JL |
4234 | } |
4235 | ||
d70b9104 PS |
4236 | static int |
4237 | cifs_readpages_read_into_pages(struct TCP_Server_Info *server, | |
4238 | struct cifs_readdata *rdata, unsigned int len) | |
4239 | { | |
4240 | return readpages_fill_pages(server, rdata, NULL, len); | |
4241 | } | |
4242 | ||
4243 | static int | |
4244 | cifs_readpages_copy_into_pages(struct TCP_Server_Info *server, | |
4245 | struct cifs_readdata *rdata, | |
4246 | struct iov_iter *iter) | |
4247 | { | |
4248 | return readpages_fill_pages(server, rdata, iter, iter->count); | |
4249 | } | |
4250 | ||
387eb92a PS |
4251 | static int |
4252 | readpages_get_pages(struct address_space *mapping, struct list_head *page_list, | |
4253 | unsigned int rsize, struct list_head *tmplist, | |
4254 | unsigned int *nr_pages, loff_t *offset, unsigned int *bytes) | |
4255 | { | |
4256 | struct page *page, *tpage; | |
4257 | unsigned int expected_index; | |
4258 | int rc; | |
8a5c743e | 4259 | gfp_t gfp = readahead_gfp_mask(mapping); |
387eb92a | 4260 | |
69cebd75 PS |
4261 | INIT_LIST_HEAD(tmplist); |
4262 | ||
f86196ea | 4263 | page = lru_to_page(page_list); |
387eb92a PS |
4264 | |
4265 | /* | |
4266 | * Lock the page and put it in the cache. Since no one else | |
4267 | * should have access to this page, we're safe to simply set | |
4268 | * PG_locked without checking it first. | |
4269 | */ | |
48c935ad | 4270 | __SetPageLocked(page); |
387eb92a | 4271 | rc = add_to_page_cache_locked(page, mapping, |
063d99b4 | 4272 | page->index, gfp); |
387eb92a PS |
4273 | |
4274 | /* give up if we can't stick it in the cache */ | |
4275 | if (rc) { | |
48c935ad | 4276 | __ClearPageLocked(page); |
387eb92a PS |
4277 | return rc; |
4278 | } | |
4279 | ||
4280 | /* move first page to the tmplist */ | |
09cbfeaf KS |
4281 | *offset = (loff_t)page->index << PAGE_SHIFT; |
4282 | *bytes = PAGE_SIZE; | |
387eb92a PS |
4283 | *nr_pages = 1; |
4284 | list_move_tail(&page->lru, tmplist); | |
4285 | ||
4286 | /* now try and add more pages onto the request */ | |
4287 | expected_index = page->index + 1; | |
4288 | list_for_each_entry_safe_reverse(page, tpage, page_list, lru) { | |
4289 | /* discontinuity ? */ | |
4290 | if (page->index != expected_index) | |
4291 | break; | |
4292 | ||
4293 | /* would this page push the read over the rsize? */ | |
09cbfeaf | 4294 | if (*bytes + PAGE_SIZE > rsize) |
387eb92a PS |
4295 | break; |
4296 | ||
48c935ad | 4297 | __SetPageLocked(page); |
9f453fb3 ZX |
4298 | rc = add_to_page_cache_locked(page, mapping, page->index, gfp); |
4299 | if (rc) { | |
48c935ad | 4300 | __ClearPageLocked(page); |
387eb92a PS |
4301 | break; |
4302 | } | |
4303 | list_move_tail(&page->lru, tmplist); | |
09cbfeaf | 4304 | (*bytes) += PAGE_SIZE; |
387eb92a PS |
4305 | expected_index++; |
4306 | (*nr_pages)++; | |
4307 | } | |
4308 | return rc; | |
8d5ce4d2 JL |
4309 | } |
4310 | ||
1da177e4 LT |
4311 | static int cifs_readpages(struct file *file, struct address_space *mapping, |
4312 | struct list_head *page_list, unsigned num_pages) | |
4313 | { | |
690c5e31 | 4314 | int rc; |
9f453fb3 | 4315 | int err = 0; |
690c5e31 JL |
4316 | struct list_head tmplist; |
4317 | struct cifsFileInfo *open_file = file->private_data; | |
7119e220 | 4318 | struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file); |
69cebd75 | 4319 | struct TCP_Server_Info *server; |
690c5e31 | 4320 | pid_t pid; |
0cb012d1 | 4321 | unsigned int xid; |
1da177e4 | 4322 | |
0cb012d1 | 4323 | xid = get_xid(); |
56698236 SJ |
4324 | /* |
4325 | * Reads as many pages as possible from fscache. Returns -ENOBUFS | |
4326 | * immediately if the cookie is negative | |
54afa990 DH |
4327 | * |
4328 | * After this point, every page in the list might have PG_fscache set, | |
4329 | * so we will need to clean that up off of every page we don't use. | |
56698236 SJ |
4330 | */ |
4331 | rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list, | |
4332 | &num_pages); | |
0cb012d1 SF |
4333 | if (rc == 0) { |
4334 | free_xid(xid); | |
690c5e31 | 4335 | return rc; |
0cb012d1 | 4336 | } |
56698236 | 4337 | |
d4ffff1f PS |
4338 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
4339 | pid = open_file->pid; | |
4340 | else | |
4341 | pid = current->tgid; | |
4342 | ||
690c5e31 | 4343 | rc = 0; |
69cebd75 | 4344 | server = tlink_tcon(open_file->tlink)->ses->server; |
1da177e4 | 4345 | |
f96637be JP |
4346 | cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n", |
4347 | __func__, file, mapping, num_pages); | |
690c5e31 JL |
4348 | |
4349 | /* | |
4350 | * Start with the page at end of list and move it to private | |
4351 | * list. Do the same with any following pages until we hit | |
4352 | * the rsize limit, hit an index discontinuity, or run out of | |
4353 | * pages. Issue the async read and then start the loop again | |
4354 | * until the list is empty. | |
4355 | * | |
4356 | * Note that list order is important. The page_list is in | |
4357 | * the order of declining indexes. When we put the pages in | |
4358 | * the rdata->pages, then we want them in increasing order. | |
4359 | */ | |
9f453fb3 | 4360 | while (!list_empty(page_list) && !err) { |
bed9da02 | 4361 | unsigned int i, nr_pages, bytes, rsize; |
690c5e31 JL |
4362 | loff_t offset; |
4363 | struct page *page, *tpage; | |
4364 | struct cifs_readdata *rdata; | |
335b7b62 PS |
4365 | struct cifs_credits credits_on_stack; |
4366 | struct cifs_credits *credits = &credits_on_stack; | |
1da177e4 | 4367 | |
3e952994 PS |
4368 | if (open_file->invalidHandle) { |
4369 | rc = cifs_reopen_file(open_file, true); | |
4370 | if (rc == -EAGAIN) | |
4371 | continue; | |
4372 | else if (rc) | |
4373 | break; | |
4374 | } | |
4375 | ||
bed9da02 | 4376 | rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize, |
335b7b62 | 4377 | &rsize, credits); |
bed9da02 PS |
4378 | if (rc) |
4379 | break; | |
690c5e31 JL |
4380 | |
4381 | /* | |
69cebd75 PS |
4382 | * Give up immediately if rsize is too small to read an entire |
4383 | * page. The VFS will fall back to readpage. We should never | |
4384 | * reach this point however since we set ra_pages to 0 when the | |
4385 | * rsize is smaller than a cache page. | |
690c5e31 | 4386 | */ |
09cbfeaf | 4387 | if (unlikely(rsize < PAGE_SIZE)) { |
bed9da02 | 4388 | add_credits_and_wake_if(server, credits, 0); |
0cb012d1 | 4389 | free_xid(xid); |
69cebd75 | 4390 | return 0; |
bed9da02 | 4391 | } |
690c5e31 | 4392 | |
9f453fb3 ZX |
4393 | nr_pages = 0; |
4394 | err = readpages_get_pages(mapping, page_list, rsize, &tmplist, | |
bed9da02 | 4395 | &nr_pages, &offset, &bytes); |
9f453fb3 | 4396 | if (!nr_pages) { |
bed9da02 | 4397 | add_credits_and_wake_if(server, credits, 0); |
690c5e31 JL |
4398 | break; |
4399 | } | |
4400 | ||
0471ca3f | 4401 | rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete); |
690c5e31 JL |
4402 | if (!rdata) { |
4403 | /* best to give up if we're out of mem */ | |
4404 | list_for_each_entry_safe(page, tpage, &tmplist, lru) { | |
4405 | list_del(&page->lru); | |
4406 | lru_cache_add_file(page); | |
4407 | unlock_page(page); | |
09cbfeaf | 4408 | put_page(page); |
690c5e31 JL |
4409 | } |
4410 | rc = -ENOMEM; | |
bed9da02 | 4411 | add_credits_and_wake_if(server, credits, 0); |
690c5e31 JL |
4412 | break; |
4413 | } | |
4414 | ||
6993f74a | 4415 | rdata->cfile = cifsFileInfo_get(open_file); |
690c5e31 JL |
4416 | rdata->mapping = mapping; |
4417 | rdata->offset = offset; | |
4418 | rdata->bytes = bytes; | |
4419 | rdata->pid = pid; | |
09cbfeaf | 4420 | rdata->pagesz = PAGE_SIZE; |
1dbe3466 | 4421 | rdata->tailsz = PAGE_SIZE; |
8321fec4 | 4422 | rdata->read_into_pages = cifs_readpages_read_into_pages; |
d70b9104 | 4423 | rdata->copy_into_pages = cifs_readpages_copy_into_pages; |
335b7b62 | 4424 | rdata->credits = credits_on_stack; |
c5fab6f4 JL |
4425 | |
4426 | list_for_each_entry_safe(page, tpage, &tmplist, lru) { | |
4427 | list_del(&page->lru); | |
4428 | rdata->pages[rdata->nr_pages++] = page; | |
4429 | } | |
690c5e31 | 4430 | |
9a1c67e8 PS |
4431 | rc = adjust_credits(server, &rdata->credits, rdata->bytes); |
4432 | ||
4433 | if (!rc) { | |
4434 | if (rdata->cfile->invalidHandle) | |
3e952994 PS |
4435 | rc = -EAGAIN; |
4436 | else | |
9a1c67e8 PS |
4437 | rc = server->ops->async_readv(rdata); |
4438 | } | |
4439 | ||
69cebd75 | 4440 | if (rc) { |
335b7b62 | 4441 | add_credits_and_wake_if(server, &rdata->credits, 0); |
c5fab6f4 JL |
4442 | for (i = 0; i < rdata->nr_pages; i++) { |
4443 | page = rdata->pages[i]; | |
690c5e31 JL |
4444 | lru_cache_add_file(page); |
4445 | unlock_page(page); | |
09cbfeaf | 4446 | put_page(page); |
1da177e4 | 4447 | } |
1209bbdf | 4448 | /* Fallback to the readpage in error/reconnect cases */ |
6993f74a | 4449 | kref_put(&rdata->refcount, cifs_readdata_release); |
1da177e4 LT |
4450 | break; |
4451 | } | |
6993f74a JL |
4452 | |
4453 | kref_put(&rdata->refcount, cifs_readdata_release); | |
1da177e4 LT |
4454 | } |
4455 | ||
54afa990 DH |
4456 | /* Any pages that have been shown to fscache but didn't get added to |
4457 | * the pagecache must be uncached before they get returned to the | |
4458 | * allocator. | |
4459 | */ | |
4460 | cifs_fscache_readpages_cancel(mapping->host, page_list); | |
0cb012d1 | 4461 | free_xid(xid); |
1da177e4 LT |
4462 | return rc; |
4463 | } | |
4464 | ||
a9e9b7bc SP |
4465 | /* |
4466 | * cifs_readpage_worker must be called with the page pinned | |
4467 | */ | |
1da177e4 LT |
4468 | static int cifs_readpage_worker(struct file *file, struct page *page, |
4469 | loff_t *poffset) | |
4470 | { | |
4471 | char *read_data; | |
4472 | int rc; | |
4473 | ||
56698236 | 4474 | /* Is the page cached? */ |
496ad9aa | 4475 | rc = cifs_readpage_from_fscache(file_inode(file), page); |
56698236 SJ |
4476 | if (rc == 0) |
4477 | goto read_complete; | |
4478 | ||
1da177e4 LT |
4479 | read_data = kmap(page); |
4480 | /* for reads over a certain size could initiate async read ahead */ | |
fb8c4b14 | 4481 | |
09cbfeaf | 4482 | rc = cifs_read(file, read_data, PAGE_SIZE, poffset); |
fb8c4b14 | 4483 | |
1da177e4 LT |
4484 | if (rc < 0) |
4485 | goto io_error; | |
4486 | else | |
f96637be | 4487 | cifs_dbg(FYI, "Bytes read %d\n", rc); |
fb8c4b14 | 4488 | |
9b9c5bea SF |
4489 | /* we do not want atime to be less than mtime, it broke some apps */ |
4490 | file_inode(file)->i_atime = current_time(file_inode(file)); | |
4491 | if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime))) | |
4492 | file_inode(file)->i_atime = file_inode(file)->i_mtime; | |
4493 | else | |
4494 | file_inode(file)->i_atime = current_time(file_inode(file)); | |
fb8c4b14 | 4495 | |
09cbfeaf KS |
4496 | if (PAGE_SIZE > rc) |
4497 | memset(read_data + rc, 0, PAGE_SIZE - rc); | |
1da177e4 LT |
4498 | |
4499 | flush_dcache_page(page); | |
4500 | SetPageUptodate(page); | |
9dc06558 SJ |
4501 | |
4502 | /* send this page to the cache */ | |
496ad9aa | 4503 | cifs_readpage_to_fscache(file_inode(file), page); |
9dc06558 | 4504 | |
1da177e4 | 4505 | rc = 0; |
fb8c4b14 | 4506 | |
1da177e4 | 4507 | io_error: |
fb8c4b14 | 4508 | kunmap(page); |
466bd31b | 4509 | unlock_page(page); |
56698236 SJ |
4510 | |
4511 | read_complete: | |
1da177e4 LT |
4512 | return rc; |
4513 | } | |
4514 | ||
4515 | static int cifs_readpage(struct file *file, struct page *page) | |
4516 | { | |
09cbfeaf | 4517 | loff_t offset = (loff_t)page->index << PAGE_SHIFT; |
1da177e4 | 4518 | int rc = -EACCES; |
6d5786a3 | 4519 | unsigned int xid; |
1da177e4 | 4520 | |
6d5786a3 | 4521 | xid = get_xid(); |
1da177e4 LT |
4522 | |
4523 | if (file->private_data == NULL) { | |
0f3bc09e | 4524 | rc = -EBADF; |
6d5786a3 | 4525 | free_xid(xid); |
0f3bc09e | 4526 | return rc; |
1da177e4 LT |
4527 | } |
4528 | ||
f96637be | 4529 | cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n", |
b6b38f70 | 4530 | page, (int)offset, (int)offset); |
1da177e4 LT |
4531 | |
4532 | rc = cifs_readpage_worker(file, page, &offset); | |
4533 | ||
6d5786a3 | 4534 | free_xid(xid); |
1da177e4 LT |
4535 | return rc; |
4536 | } | |
4537 | ||
a403a0a3 SF |
4538 | static int is_inode_writable(struct cifsInodeInfo *cifs_inode) |
4539 | { | |
4540 | struct cifsFileInfo *open_file; | |
4541 | ||
cb248819 | 4542 | spin_lock(&cifs_inode->open_file_lock); |
a403a0a3 | 4543 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { |
2e396b83 | 4544 | if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { |
cb248819 | 4545 | spin_unlock(&cifs_inode->open_file_lock); |
a403a0a3 SF |
4546 | return 1; |
4547 | } | |
4548 | } | |
cb248819 | 4549 | spin_unlock(&cifs_inode->open_file_lock); |
a403a0a3 SF |
4550 | return 0; |
4551 | } | |
4552 | ||
1da177e4 LT |
4553 | /* We do not want to update the file size from server for inodes |
4554 | open for write - to avoid races with writepage extending | |
4555 | the file - in the future we could consider allowing | |
fb8c4b14 | 4556 | refreshing the inode only on increases in the file size |
1da177e4 LT |
4557 | but this is tricky to do without racing with writebehind |
4558 | page caching in the current Linux kernel design */ | |
4b18f2a9 | 4559 | bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file) |
1da177e4 | 4560 | { |
a403a0a3 | 4561 | if (!cifsInode) |
4b18f2a9 | 4562 | return true; |
50c2f753 | 4563 | |
a403a0a3 SF |
4564 | if (is_inode_writable(cifsInode)) { |
4565 | /* This inode is open for write at least once */ | |
c32a0b68 SF |
4566 | struct cifs_sb_info *cifs_sb; |
4567 | ||
c32a0b68 | 4568 | cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb); |
ad7a2926 | 4569 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { |
fb8c4b14 | 4570 | /* since no page cache to corrupt on directio |
c32a0b68 | 4571 | we can change size safely */ |
4b18f2a9 | 4572 | return true; |
c32a0b68 SF |
4573 | } |
4574 | ||
fb8c4b14 | 4575 | if (i_size_read(&cifsInode->vfs_inode) < end_of_file) |
4b18f2a9 | 4576 | return true; |
7ba52631 | 4577 | |
4b18f2a9 | 4578 | return false; |
23e7dd7d | 4579 | } else |
4b18f2a9 | 4580 | return true; |
1da177e4 LT |
4581 | } |
4582 | ||
d9414774 NP |
4583 | static int cifs_write_begin(struct file *file, struct address_space *mapping, |
4584 | loff_t pos, unsigned len, unsigned flags, | |
4585 | struct page **pagep, void **fsdata) | |
1da177e4 | 4586 | { |
466bd31b | 4587 | int oncethru = 0; |
09cbfeaf KS |
4588 | pgoff_t index = pos >> PAGE_SHIFT; |
4589 | loff_t offset = pos & (PAGE_SIZE - 1); | |
a98ee8c1 JL |
4590 | loff_t page_start = pos & PAGE_MASK; |
4591 | loff_t i_size; | |
4592 | struct page *page; | |
4593 | int rc = 0; | |
d9414774 | 4594 | |
f96637be | 4595 | cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len); |
d9414774 | 4596 | |
466bd31b | 4597 | start: |
54566b2c | 4598 | page = grab_cache_page_write_begin(mapping, index, flags); |
a98ee8c1 JL |
4599 | if (!page) { |
4600 | rc = -ENOMEM; | |
4601 | goto out; | |
4602 | } | |
8a236264 | 4603 | |
a98ee8c1 JL |
4604 | if (PageUptodate(page)) |
4605 | goto out; | |
8a236264 | 4606 | |
a98ee8c1 JL |
4607 | /* |
4608 | * If we write a full page it will be up to date, no need to read from | |
4609 | * the server. If the write is short, we'll end up doing a sync write | |
4610 | * instead. | |
4611 | */ | |
09cbfeaf | 4612 | if (len == PAGE_SIZE) |
a98ee8c1 | 4613 | goto out; |
8a236264 | 4614 | |
a98ee8c1 JL |
4615 | /* |
4616 | * optimize away the read when we have an oplock, and we're not | |
4617 | * expecting to use any of the data we'd be reading in. That | |
4618 | * is, when the page lies beyond the EOF, or straddles the EOF | |
4619 | * and the write will cover all of the existing data. | |
4620 | */ | |
18cceb6a | 4621 | if (CIFS_CACHE_READ(CIFS_I(mapping->host))) { |
a98ee8c1 JL |
4622 | i_size = i_size_read(mapping->host); |
4623 | if (page_start >= i_size || | |
4624 | (offset == 0 && (pos + len) >= i_size)) { | |
4625 | zero_user_segments(page, 0, offset, | |
4626 | offset + len, | |
09cbfeaf | 4627 | PAGE_SIZE); |
a98ee8c1 JL |
4628 | /* |
4629 | * PageChecked means that the parts of the page | |
4630 | * to which we're not writing are considered up | |
4631 | * to date. Once the data is copied to the | |
4632 | * page, it can be set uptodate. | |
4633 | */ | |
4634 | SetPageChecked(page); | |
4635 | goto out; | |
4636 | } | |
4637 | } | |
d9414774 | 4638 | |
466bd31b | 4639 | if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) { |
a98ee8c1 JL |
4640 | /* |
4641 | * might as well read a page, it is fast enough. If we get | |
4642 | * an error, we don't need to return it. cifs_write_end will | |
4643 | * do a sync write instead since PG_uptodate isn't set. | |
4644 | */ | |
4645 | cifs_readpage_worker(file, page, &page_start); | |
09cbfeaf | 4646 | put_page(page); |
466bd31b SP |
4647 | oncethru = 1; |
4648 | goto start; | |
8a236264 SF |
4649 | } else { |
4650 | /* we could try using another file handle if there is one - | |
4651 | but how would we lock it to prevent close of that handle | |
4652 | racing with this read? In any case | |
d9414774 | 4653 | this will be written out by write_end so is fine */ |
1da177e4 | 4654 | } |
a98ee8c1 JL |
4655 | out: |
4656 | *pagep = page; | |
4657 | return rc; | |
1da177e4 LT |
4658 | } |
4659 | ||
85f2d6b4 SJ |
4660 | static int cifs_release_page(struct page *page, gfp_t gfp) |
4661 | { | |
4662 | if (PagePrivate(page)) | |
4663 | return 0; | |
4664 | ||
4665 | return cifs_fscache_release_page(page, gfp); | |
4666 | } | |
4667 | ||
d47992f8 LC |
4668 | static void cifs_invalidate_page(struct page *page, unsigned int offset, |
4669 | unsigned int length) | |
85f2d6b4 SJ |
4670 | { |
4671 | struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host); | |
4672 | ||
09cbfeaf | 4673 | if (offset == 0 && length == PAGE_SIZE) |
85f2d6b4 SJ |
4674 | cifs_fscache_invalidate_page(page, &cifsi->vfs_inode); |
4675 | } | |
4676 | ||
9ad1506b PS |
4677 | static int cifs_launder_page(struct page *page) |
4678 | { | |
4679 | int rc = 0; | |
4680 | loff_t range_start = page_offset(page); | |
09cbfeaf | 4681 | loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1); |
9ad1506b PS |
4682 | struct writeback_control wbc = { |
4683 | .sync_mode = WB_SYNC_ALL, | |
4684 | .nr_to_write = 0, | |
4685 | .range_start = range_start, | |
4686 | .range_end = range_end, | |
4687 | }; | |
4688 | ||
f96637be | 4689 | cifs_dbg(FYI, "Launder page: %p\n", page); |
9ad1506b PS |
4690 | |
4691 | if (clear_page_dirty_for_io(page)) | |
4692 | rc = cifs_writepage_locked(page, &wbc); | |
4693 | ||
4694 | cifs_fscache_invalidate_page(page, page->mapping->host); | |
4695 | return rc; | |
4696 | } | |
4697 | ||
9b646972 | 4698 | void cifs_oplock_break(struct work_struct *work) |
3bc303c2 JL |
4699 | { |
4700 | struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo, | |
4701 | oplock_break); | |
2b0143b5 | 4702 | struct inode *inode = d_inode(cfile->dentry); |
3bc303c2 | 4703 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
95a3f2f3 | 4704 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
c11f1df5 | 4705 | struct TCP_Server_Info *server = tcon->ses->server; |
eb4b756b | 4706 | int rc = 0; |
55b79b35 | 4707 | bool purge_cache = false; |
3bc303c2 | 4708 | |
c11f1df5 | 4709 | wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS, |
74316201 | 4710 | TASK_UNINTERRUPTIBLE); |
c11f1df5 | 4711 | |
55b79b35 PS |
4712 | server->ops->downgrade_oplock(server, cinode, cfile->oplock_level, |
4713 | cfile->oplock_epoch, &purge_cache); | |
c11f1df5 | 4714 | |
18cceb6a | 4715 | if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) && |
63b7d3a4 | 4716 | cifs_has_mand_locks(cinode)) { |
f96637be JP |
4717 | cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n", |
4718 | inode); | |
18cceb6a | 4719 | cinode->oplock = 0; |
63b7d3a4 PS |
4720 | } |
4721 | ||
3bc303c2 | 4722 | if (inode && S_ISREG(inode->i_mode)) { |
18cceb6a | 4723 | if (CIFS_CACHE_READ(cinode)) |
8737c930 | 4724 | break_lease(inode, O_RDONLY); |
d54ff732 | 4725 | else |
8737c930 | 4726 | break_lease(inode, O_WRONLY); |
3bc303c2 | 4727 | rc = filemap_fdatawrite(inode->i_mapping); |
55b79b35 | 4728 | if (!CIFS_CACHE_READ(cinode) || purge_cache) { |
eb4b756b JL |
4729 | rc = filemap_fdatawait(inode->i_mapping); |
4730 | mapping_set_error(inode->i_mapping, rc); | |
4f73c7d3 | 4731 | cifs_zap_mapping(inode); |
3bc303c2 | 4732 | } |
f96637be | 4733 | cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc); |
55b79b35 PS |
4734 | if (CIFS_CACHE_WRITE(cinode)) |
4735 | goto oplock_break_ack; | |
3bc303c2 JL |
4736 | } |
4737 | ||
85160e03 PS |
4738 | rc = cifs_push_locks(cfile); |
4739 | if (rc) | |
f96637be | 4740 | cifs_dbg(VFS, "Push locks rc = %d\n", rc); |
85160e03 | 4741 | |
55b79b35 | 4742 | oplock_break_ack: |
3bc303c2 JL |
4743 | /* |
4744 | * releasing stale oplock after recent reconnect of smb session using | |
4745 | * a now incorrect file handle is not a data integrity issue but do | |
4746 | * not bother sending an oplock release if session to server still is | |
4747 | * disconnected since oplock already released by the server | |
4748 | */ | |
cdff08e7 | 4749 | if (!cfile->oplock_break_cancelled) { |
95a3f2f3 PS |
4750 | rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid, |
4751 | cinode); | |
f96637be | 4752 | cifs_dbg(FYI, "Oplock release rc = %d\n", rc); |
3bc303c2 | 4753 | } |
0a447ca0 | 4754 | _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false); |
c11f1df5 | 4755 | cifs_done_oplock_break(cinode); |
3bc303c2 JL |
4756 | } |
4757 | ||
dca69288 SF |
4758 | /* |
4759 | * The presence of cifs_direct_io() in the address space ops vector | |
4760 | * allowes open() O_DIRECT flags which would have failed otherwise. | |
4761 | * | |
4762 | * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests | |
4763 | * so this method should never be called. | |
4764 | * | |
4765 | * Direct IO is not yet supported in the cached mode. | |
4766 | */ | |
4767 | static ssize_t | |
c8b8e32d | 4768 | cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter) |
dca69288 SF |
4769 | { |
4770 | /* | |
4771 | * FIXME | |
4772 | * Eventually need to support direct IO for non forcedirectio mounts | |
4773 | */ | |
4774 | return -EINVAL; | |
4775 | } | |
4776 | ||
4777 | ||
f5e54d6e | 4778 | const struct address_space_operations cifs_addr_ops = { |
1da177e4 LT |
4779 | .readpage = cifs_readpage, |
4780 | .readpages = cifs_readpages, | |
4781 | .writepage = cifs_writepage, | |
37c0eb46 | 4782 | .writepages = cifs_writepages, |
d9414774 NP |
4783 | .write_begin = cifs_write_begin, |
4784 | .write_end = cifs_write_end, | |
1da177e4 | 4785 | .set_page_dirty = __set_page_dirty_nobuffers, |
85f2d6b4 | 4786 | .releasepage = cifs_release_page, |
dca69288 | 4787 | .direct_IO = cifs_direct_io, |
85f2d6b4 | 4788 | .invalidatepage = cifs_invalidate_page, |
9ad1506b | 4789 | .launder_page = cifs_launder_page, |
1da177e4 | 4790 | }; |
273d81d6 DK |
4791 | |
4792 | /* | |
4793 | * cifs_readpages requires the server to support a buffer large enough to | |
4794 | * contain the header plus one complete page of data. Otherwise, we need | |
4795 | * to leave cifs_readpages out of the address space operations. | |
4796 | */ | |
f5e54d6e | 4797 | const struct address_space_operations cifs_addr_ops_smallbuf = { |
273d81d6 DK |
4798 | .readpage = cifs_readpage, |
4799 | .writepage = cifs_writepage, | |
4800 | .writepages = cifs_writepages, | |
d9414774 NP |
4801 | .write_begin = cifs_write_begin, |
4802 | .write_end = cifs_write_end, | |
273d81d6 | 4803 | .set_page_dirty = __set_page_dirty_nobuffers, |
85f2d6b4 SJ |
4804 | .releasepage = cifs_release_page, |
4805 | .invalidatepage = cifs_invalidate_page, | |
9ad1506b | 4806 | .launder_page = cifs_launder_page, |
273d81d6 | 4807 | }; |