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