1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3 *
4 * vfs operations that deal with files
5 *
6 * Copyright (C) International Business Machines Corp., 2002,2010
7 * Author(s): Steve French (sfrench@us.ibm.com)
8 * Jeremy Allison (jra@samba.org)
9 *
10 */
11 #include <linux/fs.h>
12 #include <linux/fs_struct.h>
13 #include <linux/filelock.h>
14 #include <linux/backing-dev.h>
15 #include <linux/stat.h>
16 #include <linux/fcntl.h>
17 #include <linux/pagemap.h>
18 #include <linux/writeback.h>
19 #include <linux/task_io_accounting_ops.h>
20 #include <linux/delay.h>
21 #include <linux/mount.h>
22 #include <linux/slab.h>
23 #include <linux/swap_ops.h>
24 #include <linux/mm.h>
25 #include <asm/div64.h>
26 #include "cifsfs.h"
27 #include "cifsglob.h"
28 #include "cifsproto.h"
29 #include "smb2proto.h"
30 #include "cifs_unicode.h"
31 #include "cifs_debug.h"
32 #include "cifs_fs_sb.h"
33 #include "fscache.h"
34 #include "smbdirect.h"
35 #include "fs_context.h"
36 #include "cifs_ioctl.h"
37 #include "cached_dir.h"
38 #include <trace/events/netfs.h>
39
40 static int cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush);
41
42 /*
43 * Prepare a subrequest to upload to the server. We need to allocate credits
44 * so that we know the maximum amount of data that we can include in it.
45 */
cifs_prepare_write(struct netfs_io_subrequest * subreq)46 static void cifs_prepare_write(struct netfs_io_subrequest *subreq)
47 {
48 struct cifs_io_subrequest *wdata =
49 container_of(subreq, struct cifs_io_subrequest, subreq);
50 struct cifs_io_request *req = wdata->req;
51 struct netfs_io_stream *stream = &req->rreq.io_streams[subreq->stream_nr];
52 struct TCP_Server_Info *server;
53 struct cifsFileInfo *open_file = req->cfile;
54 struct cifs_sb_info *cifs_sb = CIFS_SB(wdata->rreq->inode->i_sb);
55 size_t wsize = req->rreq.wsize;
56 int rc;
57
58 if (!wdata->have_xid) {
59 wdata->xid = get_xid();
60 wdata->have_xid = true;
61 }
62
63 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
64 wdata->server = server;
65
66 if (cifs_sb->ctx->wsize == 0)
67 cifs_negotiate_wsize(server, cifs_sb->ctx,
68 tlink_tcon(req->cfile->tlink));
69
70 retry:
71 if (open_file->invalidHandle) {
72 rc = cifs_reopen_file(open_file, false);
73 if (rc < 0) {
74 if (rc == -EAGAIN)
75 goto retry;
76 subreq->error = rc;
77 return netfs_prepare_write_failed(subreq);
78 }
79 }
80
81 rc = server->ops->wait_mtu_credits(server, wsize, &stream->sreq_max_len,
82 &wdata->credits);
83 if (rc < 0) {
84 subreq->error = rc;
85 return netfs_prepare_write_failed(subreq);
86 }
87
88 wdata->credits.rreq_debug_id = subreq->rreq->debug_id;
89 wdata->credits.rreq_debug_index = subreq->debug_index;
90 wdata->credits.in_flight_check = 1;
91 trace_smb3_rw_credits(wdata->rreq->debug_id,
92 wdata->subreq.debug_index,
93 wdata->credits.value,
94 server->credits, server->in_flight,
95 wdata->credits.value,
96 cifs_trace_rw_credits_write_prepare);
97
98 #ifdef CONFIG_CIFS_SMB_DIRECT
99 if (server->smbd_conn) {
100 const struct smbdirect_socket_parameters *sp =
101 smbd_get_parameters(server->smbd_conn);
102
103 stream->sreq_max_segs = sp->max_frmr_depth;
104 }
105 #endif
106 }
107
108 /*
109 * Issue a subrequest to upload to the server.
110 */
cifs_issue_write(struct netfs_io_subrequest * subreq)111 static void cifs_issue_write(struct netfs_io_subrequest *subreq)
112 {
113 struct cifs_io_subrequest *wdata =
114 container_of(subreq, struct cifs_io_subrequest, subreq);
115 struct cifs_sb_info *sbi = CIFS_SB(subreq->rreq->inode->i_sb);
116 int rc;
117
118 if (cifs_forced_shutdown(sbi)) {
119 rc = smb_EIO(smb_eio_trace_forced_shutdown);
120 goto fail;
121 }
122
123 rc = adjust_credits(wdata->server, wdata, cifs_trace_rw_credits_issue_write_adjust);
124 if (rc)
125 goto fail;
126
127 rc = -EAGAIN;
128 if (wdata->req->cfile->invalidHandle)
129 goto fail;
130
131 wdata->server->ops->async_writev(wdata);
132 out:
133 return;
134
135 fail:
136 if (rc == -EAGAIN)
137 trace_netfs_sreq(subreq, netfs_sreq_trace_retry);
138 else
139 trace_netfs_sreq(subreq, netfs_sreq_trace_fail);
140 add_credits_and_wake_if(wdata->server, &wdata->credits, 0);
141 cifs_write_subrequest_terminated(wdata, rc);
142 goto out;
143 }
144
cifs_netfs_invalidate_cache(struct netfs_io_request * wreq)145 static void cifs_netfs_invalidate_cache(struct netfs_io_request *wreq)
146 {
147 cifs_invalidate_cache(wreq->inode, 0);
148 }
149
150 /*
151 * Negotiate the size of a read operation on behalf of the netfs library.
152 */
cifs_prepare_read(struct netfs_io_subrequest * subreq)153 static int cifs_prepare_read(struct netfs_io_subrequest *subreq)
154 {
155 struct netfs_io_request *rreq = subreq->rreq;
156 struct cifs_io_subrequest *rdata = container_of(subreq, struct cifs_io_subrequest, subreq);
157 struct cifs_io_request *req = container_of(subreq->rreq, struct cifs_io_request, rreq);
158 struct TCP_Server_Info *server;
159 struct cifs_sb_info *cifs_sb = CIFS_SB(rreq->inode->i_sb);
160 size_t size;
161 int rc = 0;
162
163 if (!rdata->have_xid) {
164 rdata->xid = get_xid();
165 rdata->have_xid = true;
166 }
167
168 server = cifs_pick_channel(tlink_tcon(req->cfile->tlink)->ses);
169 rdata->server = server;
170
171 if (cifs_sb->ctx->rsize == 0)
172 cifs_negotiate_rsize(server, cifs_sb->ctx,
173 tlink_tcon(req->cfile->tlink));
174
175 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
176 &size, &rdata->credits);
177 if (rc)
178 return rc;
179
180 rreq->io_streams[0].sreq_max_len = size;
181
182 rdata->credits.in_flight_check = 1;
183 rdata->credits.rreq_debug_id = rreq->debug_id;
184 rdata->credits.rreq_debug_index = subreq->debug_index;
185
186 trace_smb3_rw_credits(rdata->rreq->debug_id,
187 rdata->subreq.debug_index,
188 rdata->credits.value,
189 server->credits, server->in_flight, 0,
190 cifs_trace_rw_credits_read_submit);
191
192 #ifdef CONFIG_CIFS_SMB_DIRECT
193 if (server->smbd_conn) {
194 const struct smbdirect_socket_parameters *sp =
195 smbd_get_parameters(server->smbd_conn);
196
197 rreq->io_streams[0].sreq_max_segs = sp->max_frmr_depth;
198 }
199 #endif
200 return 0;
201 }
202
203 /*
204 * Issue a read operation on behalf of the netfs helper functions. We're asked
205 * to make a read of a certain size at a point in the file. We are permitted
206 * to only read a portion of that, but as long as we read something, the netfs
207 * helper will call us again so that we can issue another read.
208 */
cifs_issue_read(struct netfs_io_subrequest * subreq)209 static void cifs_issue_read(struct netfs_io_subrequest *subreq)
210 {
211 struct netfs_io_request *rreq = subreq->rreq;
212 struct cifs_io_subrequest *rdata = container_of(subreq, struct cifs_io_subrequest, subreq);
213 struct cifs_io_request *req = container_of(subreq->rreq, struct cifs_io_request, rreq);
214 struct TCP_Server_Info *server = rdata->server;
215 int rc = 0;
216
217 cifs_dbg(FYI, "%s: op=%08x[%x] mapping=%p len=%zu/%zu\n",
218 __func__, rreq->debug_id, subreq->debug_index, rreq->mapping,
219 subreq->transferred, subreq->len);
220
221 rc = adjust_credits(server, rdata, cifs_trace_rw_credits_issue_read_adjust);
222 if (rc)
223 goto failed;
224
225 if (req->cfile->invalidHandle) {
226 do {
227 rc = cifs_reopen_file(req->cfile, true);
228 } while (rc == -EAGAIN);
229 if (rc)
230 goto failed;
231 }
232
233 if (subreq->rreq->origin != NETFS_UNBUFFERED_READ &&
234 subreq->rreq->origin != NETFS_DIO_READ)
235 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
236
237 trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
238 rc = rdata->server->ops->async_readv(rdata);
239 if (rc)
240 goto failed;
241 return;
242
243 failed:
244 add_credits_and_wake_if(rdata->server, &rdata->credits, 0);
245 subreq->error = rc;
246 netfs_read_subreq_terminated(subreq);
247 }
248
249 /*
250 * Writeback calls this when it finds a folio that needs uploading. This isn't
251 * called if writeback only has copy-to-cache to deal with.
252 */
cifs_begin_writeback(struct netfs_io_request * wreq)253 static void cifs_begin_writeback(struct netfs_io_request *wreq)
254 {
255 struct cifs_io_request *req = container_of(wreq, struct cifs_io_request, rreq);
256 int ret;
257
258 ret = cifs_get_writable_file(CIFS_I(wreq->inode), FIND_ANY, &req->cfile);
259 if (ret) {
260 cifs_dbg(VFS, "No writable handle in writepages ret=%d\n", ret);
261 return;
262 }
263
264 wreq->io_streams[0].avail = true;
265 }
266
267 /*
268 * Initialise a request.
269 */
cifs_init_request(struct netfs_io_request * rreq,struct file * file)270 static int cifs_init_request(struct netfs_io_request *rreq, struct file *file)
271 {
272 struct cifs_io_request *req = container_of(rreq, struct cifs_io_request, rreq);
273 struct cifs_sb_info *cifs_sb = CIFS_SB(rreq->inode);
274 struct cifsFileInfo *open_file = NULL;
275
276 rreq->rsize = cifs_sb->ctx->rsize;
277 rreq->wsize = cifs_sb->ctx->wsize;
278 req->pid = current->tgid; // Ummm... This may be a workqueue
279
280 if (file) {
281 open_file = file->private_data;
282 rreq->netfs_priv = file->private_data;
283 req->cfile = cifsFileInfo_get(open_file);
284 if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_RWPIDFORWARD)
285 req->pid = req->cfile->pid;
286 } else if (rreq->origin != NETFS_WRITEBACK) {
287 WARN_ON_ONCE(1);
288 return smb_EIO1(smb_eio_trace_not_netfs_writeback, rreq->origin);
289 }
290
291 atomic_inc(&cifs_sb->outstanding_rreq);
292 return 0;
293 }
294
295 /*
296 * Completion of a request operation.
297 */
cifs_rreq_done(struct netfs_io_request * rreq)298 static void cifs_rreq_done(struct netfs_io_request *rreq)
299 {
300 struct timespec64 atime, mtime;
301 struct inode *inode = rreq->inode;
302
303 /* we do not want atime to be less than mtime, it broke some apps */
304 atime = inode_set_atime_to_ts(inode, current_time(inode));
305 mtime = inode_get_mtime(inode);
306 if (timespec64_compare(&atime, &mtime) < 0)
307 inode_set_atime_to_ts(inode, inode_get_mtime(inode));
308 }
309
cifs_free_request(struct netfs_io_request * rreq)310 static void cifs_free_request(struct netfs_io_request *rreq)
311 {
312 struct cifs_io_request *req = container_of(rreq, struct cifs_io_request, rreq);
313 struct cifs_sb_info *cifs_sb = CIFS_SB(rreq->inode->i_sb);
314
315 if (req->cfile)
316 cifsFileInfo_put(req->cfile);
317
318 if (atomic_dec_and_test(&cifs_sb->outstanding_rreq))
319 wake_up_var(&cifs_sb->outstanding_rreq);
320 }
321
cifs_free_subrequest(struct netfs_io_subrequest * subreq)322 static void cifs_free_subrequest(struct netfs_io_subrequest *subreq)
323 {
324 struct cifs_io_subrequest *rdata =
325 container_of(subreq, struct cifs_io_subrequest, subreq);
326 int rc = subreq->error;
327
328 if (rdata->subreq.source == NETFS_DOWNLOAD_FROM_SERVER) {
329 #ifdef CONFIG_CIFS_SMB_DIRECT
330 if (rdata->mr) {
331 smbd_deregister_mr(rdata->mr);
332 rdata->mr = NULL;
333 }
334 #endif
335 }
336
337 if (rdata->credits.value != 0) {
338 trace_smb3_rw_credits(rdata->rreq->debug_id,
339 rdata->subreq.debug_index,
340 rdata->credits.value,
341 rdata->server ? rdata->server->credits : 0,
342 rdata->server ? rdata->server->in_flight : 0,
343 -rdata->credits.value,
344 cifs_trace_rw_credits_free_subreq);
345 if (rdata->server)
346 add_credits_and_wake_if(rdata->server, &rdata->credits, 0);
347 else
348 rdata->credits.value = 0;
349 }
350
351 if (rdata->have_xid)
352 free_xid(rdata->xid);
353 }
354
355 const struct netfs_request_ops cifs_req_ops = {
356 .request_pool = &cifs_io_request_pool,
357 .subrequest_pool = &cifs_io_subrequest_pool,
358 .init_request = cifs_init_request,
359 .free_request = cifs_free_request,
360 .free_subrequest = cifs_free_subrequest,
361 .prepare_read = cifs_prepare_read,
362 .issue_read = cifs_issue_read,
363 .done = cifs_rreq_done,
364 .begin_writeback = cifs_begin_writeback,
365 .prepare_write = cifs_prepare_write,
366 .issue_write = cifs_issue_write,
367 .invalidate_cache = cifs_netfs_invalidate_cache,
368 };
369
370 /*
371 * Mark as invalid, all open files on tree connections since they
372 * were closed when session to server was lost.
373 */
374 void
cifs_mark_open_files_invalid(struct cifs_tcon * tcon)375 cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
376 {
377 struct cifsFileInfo *open_file = NULL;
378 struct list_head *tmp;
379 struct list_head *tmp1;
380
381 /* only send once per connect */
382 spin_lock(&tcon->tc_lock);
383 if (tcon->need_reconnect)
384 tcon->status = TID_NEED_RECON;
385
386 if (tcon->status != TID_NEED_RECON) {
387 spin_unlock(&tcon->tc_lock);
388 return;
389 }
390 tcon->status = TID_IN_FILES_INVALIDATE;
391 spin_unlock(&tcon->tc_lock);
392
393 /* list all files open on tree connection and mark them invalid */
394 spin_lock(&tcon->open_file_lock);
395 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
396 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
397 open_file->invalidHandle = true;
398 open_file->oplock_break_cancelled = true;
399 }
400 spin_unlock(&tcon->open_file_lock);
401
402 invalidate_all_cached_dirs(tcon, true);
403 spin_lock(&tcon->tc_lock);
404 if (tcon->status == TID_IN_FILES_INVALIDATE)
405 tcon->status = TID_NEED_TCON;
406 spin_unlock(&tcon->tc_lock);
407
408 /*
409 * BB Add call to evict_inodes(sb) for all superblocks mounted
410 * to this tcon.
411 */
412 }
413
cifs_convert_flags(unsigned int oflags,int rdwr_for_fscache)414 static inline int cifs_convert_flags(unsigned int oflags, int rdwr_for_fscache)
415 {
416 int flags = 0;
417
418 if (oflags & O_TMPFILE)
419 flags |= DELETE;
420
421 if ((oflags & O_ACCMODE) == O_RDONLY)
422 return flags | GENERIC_READ;
423 if ((oflags & O_ACCMODE) == O_WRONLY) {
424 return flags | (rdwr_for_fscache == 1 ?
425 (GENERIC_READ | GENERIC_WRITE) : GENERIC_WRITE);
426 }
427 if ((oflags & O_ACCMODE) == O_RDWR) {
428 /* GENERIC_ALL is too much permission to request
429 can cause unnecessary access denied on create */
430 /* return GENERIC_ALL; */
431 return flags | GENERIC_READ | GENERIC_WRITE;
432 }
433
434 return flags | READ_CONTROL | FILE_WRITE_ATTRIBUTES |
435 FILE_READ_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA |
436 FILE_WRITE_DATA | FILE_READ_DATA;
437 }
438
439 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
cifs_posix_convert_flags(unsigned int flags)440 static u32 cifs_posix_convert_flags(unsigned int flags)
441 {
442 u32 posix_flags = 0;
443
444 if ((flags & O_ACCMODE) == O_RDONLY)
445 posix_flags = SMB_O_RDONLY;
446 else if ((flags & O_ACCMODE) == O_WRONLY)
447 posix_flags = SMB_O_WRONLY;
448 else if ((flags & O_ACCMODE) == O_RDWR)
449 posix_flags = SMB_O_RDWR;
450
451 if (flags & O_CREAT) {
452 posix_flags |= SMB_O_CREAT;
453 if (flags & O_EXCL)
454 posix_flags |= SMB_O_EXCL;
455 } else if (flags & O_EXCL)
456 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
457 current->comm, current->tgid);
458
459 if (flags & O_TRUNC)
460 posix_flags |= SMB_O_TRUNC;
461 /* be safe and imply O_SYNC for O_DSYNC */
462 if (flags & O_DSYNC)
463 posix_flags |= SMB_O_SYNC;
464 if (flags & O_DIRECTORY)
465 posix_flags |= SMB_O_DIRECTORY;
466 if (flags & O_NOFOLLOW)
467 posix_flags |= SMB_O_NOFOLLOW;
468 if (flags & O_DIRECT)
469 posix_flags |= SMB_O_DIRECT;
470
471 return posix_flags;
472 }
473 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
474
cifs_get_disposition(unsigned int flags)475 static inline int cifs_get_disposition(unsigned int flags)
476 {
477 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
478 return FILE_CREATE;
479 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
480 return FILE_OVERWRITE_IF;
481 else if ((flags & O_CREAT) == O_CREAT)
482 return FILE_OPEN_IF;
483 else if ((flags & O_TRUNC) == O_TRUNC)
484 return FILE_OVERWRITE;
485 else
486 return FILE_OPEN;
487 }
488
489 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
cifs_posix_open(const char * full_path,struct inode ** pinode,struct super_block * sb,int mode,unsigned int f_flags,__u32 * poplock,__u16 * pnetfid,unsigned int xid)490 int cifs_posix_open(const char *full_path, struct inode **pinode,
491 struct super_block *sb, int mode, unsigned int f_flags,
492 __u32 *poplock, __u16 *pnetfid, unsigned int xid)
493 {
494 int rc;
495 FILE_UNIX_BASIC_INFO *presp_data;
496 __u32 posix_flags = 0;
497 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
498 struct cifs_fattr fattr;
499 struct tcon_link *tlink;
500 struct cifs_tcon *tcon;
501
502 cifs_dbg(FYI, "posix open %s\n", full_path);
503
504 presp_data = kzalloc_obj(FILE_UNIX_BASIC_INFO);
505 if (presp_data == NULL)
506 return -ENOMEM;
507
508 tlink = cifs_sb_tlink(cifs_sb);
509 if (IS_ERR(tlink)) {
510 rc = PTR_ERR(tlink);
511 goto posix_open_ret;
512 }
513
514 tcon = tlink_tcon(tlink);
515 mode &= ~current_umask();
516
517 posix_flags = cifs_posix_convert_flags(f_flags);
518 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
519 poplock, full_path, cifs_sb->local_nls,
520 cifs_remap(cifs_sb));
521 cifs_put_tlink(tlink);
522
523 if (rc)
524 goto posix_open_ret;
525
526 if (presp_data->Type == cpu_to_le32(-1))
527 goto posix_open_ret; /* open ok, caller does qpathinfo */
528
529 if (!pinode)
530 goto posix_open_ret; /* caller does not need info */
531
532 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
533
534 /* get new inode and set it up */
535 if (*pinode == NULL) {
536 cifs_fill_uniqueid(sb, &fattr);
537 *pinode = cifs_iget(sb, &fattr);
538 if (!*pinode) {
539 rc = -ENOMEM;
540 goto posix_open_ret;
541 }
542 } else {
543 cifs_revalidate_mapping(*pinode);
544 rc = cifs_fattr_to_inode(*pinode, &fattr, false);
545 }
546
547 posix_open_ret:
548 kfree(presp_data);
549 return rc;
550 }
551 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
552
cifs_nt_open(const char * full_path,struct inode * inode,struct cifs_sb_info * cifs_sb,struct cifs_tcon * tcon,unsigned int f_flags,__u32 * oplock,struct cifs_fid * fid,unsigned int xid,struct cifs_open_info_data * buf)553 static int cifs_nt_open(const char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
554 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
555 struct cifs_fid *fid, unsigned int xid, struct cifs_open_info_data *buf)
556 {
557 int rc;
558 int desired_access;
559 int disposition;
560 int create_options = CREATE_NOT_DIR;
561 struct TCP_Server_Info *server = tcon->ses->server;
562 struct cifs_open_parms oparms;
563 int rdwr_for_fscache = 0;
564
565 if (!server->ops->open)
566 return -ENOSYS;
567
568 /* If we're caching, we need to be able to fill in around partial writes. */
569 if (cifs_fscache_enabled(inode) && (f_flags & O_ACCMODE) == O_WRONLY)
570 rdwr_for_fscache = 1;
571
572 desired_access = cifs_convert_flags(f_flags, rdwr_for_fscache);
573
574 /*********************************************************************
575 * open flag mapping table:
576 *
577 * POSIX Flag CIFS Disposition
578 * ---------- ----------------
579 * O_CREAT FILE_OPEN_IF
580 * O_CREAT | O_EXCL FILE_CREATE
581 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
582 * O_TRUNC FILE_OVERWRITE
583 * none of the above FILE_OPEN
584 *
585 * Note that there is not a direct match between disposition
586 * FILE_SUPERSEDE (ie create whether or not file exists although
587 * O_CREAT | O_TRUNC is similar but truncates the existing
588 * file rather than creating a new file as FILE_SUPERSEDE does
589 * (which uses the attributes / metadata passed in on open call)
590 *?
591 *? O_SYNC is a reasonable match to CIFS writethrough flag
592 *? and the read write flags match reasonably. O_LARGEFILE
593 *? is irrelevant because largefile support is always used
594 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
595 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
596 *********************************************************************/
597
598 disposition = cifs_get_disposition(f_flags);
599 /* BB pass O_SYNC flag through on file attributes .. BB */
600 create_options |= cifs_open_create_options(f_flags, create_options);
601
602 retry_open:
603 oparms = (struct cifs_open_parms) {
604 .tcon = tcon,
605 .cifs_sb = cifs_sb,
606 .desired_access = desired_access,
607 .create_options = cifs_create_options(cifs_sb, create_options),
608 .disposition = disposition,
609 .path = full_path,
610 .fid = fid,
611 };
612
613 rc = server->ops->open(xid, &oparms, oplock, buf);
614 if (rc) {
615 if (rc == -EACCES && rdwr_for_fscache == 1) {
616 desired_access = cifs_convert_flags(f_flags, 0);
617 rdwr_for_fscache = 2;
618 goto retry_open;
619 }
620 return rc;
621 }
622 if (rdwr_for_fscache == 2)
623 cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE);
624
625 /* TODO: Add support for calling posix query info but with passing in fid */
626 if (tcon->unix_ext)
627 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
628 xid);
629 else
630 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
631 xid, fid);
632
633 if (rc) {
634 server->ops->close(xid, tcon, fid);
635 if (rc == -ESTALE)
636 rc = -EOPENSTALE;
637 }
638
639 return rc;
640 }
641
642 static bool
cifs_has_mand_locks(struct cifsInodeInfo * cinode)643 cifs_has_mand_locks(struct cifsInodeInfo *cinode)
644 {
645 struct cifs_fid_locks *cur;
646 bool has_locks = false;
647
648 down_read(&cinode->lock_sem);
649 list_for_each_entry(cur, &cinode->llist, llist) {
650 if (!list_empty(&cur->locks)) {
651 has_locks = true;
652 break;
653 }
654 }
655 up_read(&cinode->lock_sem);
656 return has_locks;
657 }
658
659 void
cifs_down_write(struct rw_semaphore * sem)660 cifs_down_write(struct rw_semaphore *sem)
661 {
662 while (!down_write_trylock(sem))
663 msleep(10);
664 }
665
666 static void cifsFileInfo_put_work(struct work_struct *work);
667 void serverclose_work(struct work_struct *work);
668
cifs_new_fileinfo(struct cifs_fid * fid,struct file * file,struct tcon_link * tlink,__u32 oplock,const char * symlink_target)669 struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
670 struct tcon_link *tlink, __u32 oplock,
671 const char *symlink_target)
672 {
673 struct dentry *dentry = file_dentry(file);
674 struct inode *inode = d_inode(dentry);
675 struct cifsInodeInfo *cinode = CIFS_I(inode);
676 struct cifsFileInfo *cfile;
677 struct cifs_fid_locks *fdlocks;
678 struct cifs_tcon *tcon = tlink_tcon(tlink);
679 struct TCP_Server_Info *server = tcon->ses->server;
680
681 cfile = kzalloc_obj(struct cifsFileInfo);
682 if (cfile == NULL)
683 return cfile;
684
685 fdlocks = kzalloc_obj(struct cifs_fid_locks);
686 if (!fdlocks) {
687 kfree(cfile);
688 return NULL;
689 }
690
691 if (symlink_target) {
692 cfile->symlink_target = kstrdup(symlink_target, GFP_KERNEL);
693 if (!cfile->symlink_target) {
694 kfree(fdlocks);
695 kfree(cfile);
696 return NULL;
697 }
698 }
699
700 INIT_LIST_HEAD(&fdlocks->locks);
701 fdlocks->cfile = cfile;
702 cfile->llist = fdlocks;
703
704 cfile->count = 1;
705 cfile->pid = current->tgid;
706 cfile->uid = current_fsuid();
707 cfile->dentry = dget(dentry);
708 cfile->f_flags = file->f_flags;
709 cfile->invalidHandle = false;
710 cfile->deferred_close_scheduled = false;
711 cfile->status_file_deleted = file->f_flags & O_TMPFILE;
712 cfile->tlink = cifs_get_tlink(tlink);
713 INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
714 INIT_WORK(&cfile->put, cifsFileInfo_put_work);
715 INIT_WORK(&cfile->serverclose, serverclose_work);
716 INIT_DELAYED_WORK(&cfile->deferred, smb2_deferred_work_close);
717 mutex_init(&cfile->fh_mutex);
718 spin_lock_init(&cfile->file_info_lock);
719
720 /*
721 * If the server returned a read oplock and we have mandatory brlocks,
722 * set oplock level to None.
723 */
724 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
725 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
726 oplock = 0;
727 }
728
729 cifs_down_write(&cinode->lock_sem);
730 list_add(&fdlocks->llist, &cinode->llist);
731 up_write(&cinode->lock_sem);
732
733 spin_lock(&tcon->open_file_lock);
734 if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
735 oplock = fid->pending_open->oplock;
736 list_del(&fid->pending_open->olist);
737
738 list_add(&cfile->tlist, &tcon->openFileList);
739 atomic_inc(&tcon->num_local_opens);
740
741 /* if readable file instance put first in list*/
742 spin_lock(&cinode->open_file_lock);
743 if (file->f_flags & O_TMPFILE)
744 set_bit(CIFS_INO_TMPFILE, &cinode->flags);
745 fid->purge_cache = false;
746 server->ops->set_fid(cfile, fid, oplock);
747
748 if (file->f_mode & FMODE_READ)
749 list_add(&cfile->flist, &cinode->openFileList);
750 else
751 list_add_tail(&cfile->flist, &cinode->openFileList);
752 spin_unlock(&cinode->open_file_lock);
753 spin_unlock(&tcon->open_file_lock);
754
755 if (fid->purge_cache)
756 cifs_zap_mapping(inode);
757
758 file->private_data = cfile;
759 return cfile;
760 }
761
762 struct cifsFileInfo *
cifsFileInfo_get(struct cifsFileInfo * cifs_file)763 cifsFileInfo_get(struct cifsFileInfo *cifs_file)
764 {
765 spin_lock(&cifs_file->file_info_lock);
766 cifsFileInfo_get_locked(cifs_file);
767 spin_unlock(&cifs_file->file_info_lock);
768 return cifs_file;
769 }
770
cifsFileInfo_put_final(struct cifsFileInfo * cifs_file)771 static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
772 {
773 struct inode *inode = d_inode(cifs_file->dentry);
774 struct cifsInodeInfo *cifsi = CIFS_I(inode);
775 struct cifsLockInfo *li, *tmp;
776
777 /*
778 * Delete any outstanding lock records. We'll lose them when the file
779 * is closed anyway.
780 */
781 cifs_down_write(&cifsi->lock_sem);
782 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
783 list_del(&li->llist);
784 cifs_del_lock_waiters(li);
785 kfree(li);
786 }
787 list_del(&cifs_file->llist->llist);
788 kfree(cifs_file->llist);
789 up_write(&cifsi->lock_sem);
790
791 cifs_put_tlink(cifs_file->tlink);
792 dput(cifs_file->dentry);
793 kfree(cifs_file->symlink_target);
794 kfree(cifs_file);
795 }
796
cifsFileInfo_put_work(struct work_struct * work)797 static void cifsFileInfo_put_work(struct work_struct *work)
798 {
799 struct cifsFileInfo *cifs_file = container_of(work,
800 struct cifsFileInfo, put);
801
802 cifsFileInfo_put_final(cifs_file);
803 }
804
serverclose_work(struct work_struct * work)805 void serverclose_work(struct work_struct *work)
806 {
807 struct cifsFileInfo *cifs_file = container_of(work,
808 struct cifsFileInfo, serverclose);
809
810 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
811
812 struct TCP_Server_Info *server = tcon->ses->server;
813 int rc = 0;
814 int retries = 0;
815 int MAX_RETRIES = 4;
816
817 do {
818 if (server->ops->close_getattr)
819 rc = server->ops->close_getattr(0, tcon, cifs_file);
820 else if (server->ops->close)
821 rc = server->ops->close(0, tcon, &cifs_file->fid);
822
823 if (rc == -EBUSY || rc == -EAGAIN) {
824 retries++;
825 msleep(250);
826 }
827 } while ((rc == -EBUSY || rc == -EAGAIN) && (retries < MAX_RETRIES)
828 );
829
830 if (retries == MAX_RETRIES)
831 pr_warn("Serverclose failed %d times, giving up\n", MAX_RETRIES);
832
833 if (cifs_file->offload)
834 queue_work(fileinfo_put_wq, &cifs_file->put);
835 else
836 cifsFileInfo_put_final(cifs_file);
837 }
838
839 /**
840 * cifsFileInfo_put - release a reference of file priv data
841 *
842 * Always potentially wait for oplock handler. See _cifsFileInfo_put().
843 *
844 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
845 */
cifsFileInfo_put(struct cifsFileInfo * cifs_file)846 void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
847 {
848 _cifsFileInfo_put(cifs_file, true, true);
849 }
850
851 /**
852 * _cifsFileInfo_put - release a reference of file priv data
853 *
854 * This may involve closing the filehandle @cifs_file out on the
855 * server. Must be called without holding tcon->open_file_lock,
856 * cinode->open_file_lock and cifs_file->file_info_lock.
857 *
858 * If @wait_for_oplock_handler is true and we are releasing the last
859 * reference, wait for any running oplock break handler of the file
860 * and cancel any pending one.
861 *
862 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
863 * @wait_oplock_handler: must be false if called from oplock_break_handler
864 * @offload: not offloaded on close and oplock breaks
865 *
866 */
_cifsFileInfo_put(struct cifsFileInfo * cifs_file,bool wait_oplock_handler,bool offload)867 void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
868 bool wait_oplock_handler, bool offload)
869 {
870 struct inode *inode = d_inode(cifs_file->dentry);
871 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
872 struct TCP_Server_Info *server = tcon->ses->server;
873 struct cifsInodeInfo *cifsi = CIFS_I(inode);
874 struct super_block *sb = inode->i_sb;
875 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
876 struct cifs_fid fid = {};
877 struct cifs_pending_open open;
878 bool oplock_break_cancelled;
879 bool serverclose_offloaded = false;
880
881 spin_lock(&tcon->open_file_lock);
882 spin_lock(&cifsi->open_file_lock);
883 spin_lock(&cifs_file->file_info_lock);
884
885 cifs_file->offload = offload;
886 if (--cifs_file->count > 0) {
887 spin_unlock(&cifs_file->file_info_lock);
888 spin_unlock(&cifsi->open_file_lock);
889 spin_unlock(&tcon->open_file_lock);
890 return;
891 }
892 spin_unlock(&cifs_file->file_info_lock);
893
894 if (server->ops->get_lease_key)
895 server->ops->get_lease_key(inode, &fid);
896
897 /* store open in pending opens to make sure we don't miss lease break */
898 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
899
900 /* remove it from the lists */
901 list_del(&cifs_file->flist);
902 list_del(&cifs_file->tlist);
903 atomic_dec(&tcon->num_local_opens);
904
905 if (list_empty(&cifsi->openFileList)) {
906 cifs_dbg(FYI, "closing last open instance for inode %p\n",
907 d_inode(cifs_file->dentry));
908 /*
909 * In strict cache mode we need invalidate mapping on the last
910 * close because it may cause a error when we open this file
911 * again and get at least level II oplock.
912 */
913 if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_STRICT_IO)
914 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
915 cifs_set_oplock_level(cifsi, 0);
916 }
917
918 if (OPEN_FMODE(cifs_file->f_flags) & FMODE_WRITE) {
919 /* Stamp while open_file_lock is held; covers all close paths
920 * including background I/O. Pairs with smp_load_acquire() in
921 * is_size_safe_to_change().
922 */
923 smp_store_release(&cifsi->time_last_write, jiffies);
924 }
925
926 spin_unlock(&cifsi->open_file_lock);
927 spin_unlock(&tcon->open_file_lock);
928
929 oplock_break_cancelled = wait_oplock_handler ?
930 cancel_work_sync(&cifs_file->oplock_break) : false;
931
932 if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
933 struct TCP_Server_Info *server = tcon->ses->server;
934 unsigned int xid;
935 int rc = 0;
936
937 xid = get_xid();
938 if (server->ops->close_getattr)
939 rc = server->ops->close_getattr(xid, tcon, cifs_file);
940 else if (server->ops->close)
941 rc = server->ops->close(xid, tcon, &cifs_file->fid);
942 _free_xid(xid);
943
944 if (rc == -EBUSY || rc == -EAGAIN) {
945 // Server close failed, hence offloading it as an async op
946 queue_work(serverclose_wq, &cifs_file->serverclose);
947 serverclose_offloaded = true;
948 }
949 }
950
951 if (oplock_break_cancelled)
952 cifs_done_oplock_break(cifsi);
953
954 cifs_del_pending_open(&open);
955
956 // if serverclose has been offloaded to wq (on failure), it will
957 // handle offloading put as well. If serverclose not offloaded,
958 // we need to handle offloading put here.
959 if (!serverclose_offloaded) {
960 if (offload)
961 queue_work(fileinfo_put_wq, &cifs_file->put);
962 else
963 cifsFileInfo_put_final(cifs_file);
964 }
965 }
966
cifs_file_flush(const unsigned int xid,struct inode * inode,struct cifsFileInfo * cfile)967 int cifs_file_flush(const unsigned int xid, struct inode *inode,
968 struct cifsFileInfo *cfile)
969 {
970 struct cifs_sb_info *cifs_sb = CIFS_SB(inode);
971 struct cifs_tcon *tcon;
972 int rc;
973
974 if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOSSYNC)
975 return 0;
976
977 if (cfile && (OPEN_FMODE(cfile->f_flags) & FMODE_WRITE)) {
978 tcon = tlink_tcon(cfile->tlink);
979 return tcon->ses->server->ops->flush(xid, tcon,
980 &cfile->fid);
981 }
982 rc = cifs_get_writable_file(CIFS_I(inode), FIND_ANY, &cfile);
983 if (!rc) {
984 tcon = tlink_tcon(cfile->tlink);
985 rc = tcon->ses->server->ops->flush(xid, tcon, &cfile->fid);
986 cifsFileInfo_put(cfile);
987 } else if (rc == -EBADF) {
988 rc = 0;
989 }
990 return rc;
991 }
992
cifs_do_truncate(const unsigned int xid,struct dentry * dentry)993 static int cifs_do_truncate(const unsigned int xid, struct dentry *dentry)
994 {
995 struct cifsInodeInfo *cinode = CIFS_I(d_inode(dentry));
996 struct inode *inode = d_inode(dentry);
997 struct cifsFileInfo *cfile = NULL;
998 struct TCP_Server_Info *server;
999 struct cifs_tcon *tcon;
1000 int rc;
1001
1002 rc = inode_lock_killable(inode);
1003 if (rc)
1004 return -ERESTARTSYS;
1005
1006 filemap_invalidate_lock(inode->i_mapping);
1007
1008 rc = filemap_write_and_wait(inode->i_mapping);
1009 if (is_interrupt_error(rc)) {
1010 rc = -ERESTARTSYS;
1011 goto out;
1012 }
1013 mapping_set_error(inode->i_mapping, rc);
1014
1015 cfile = find_writable_file(cinode, FIND_FSUID_ONLY);
1016 rc = cifs_file_flush(xid, inode, cfile);
1017 if (!rc) {
1018 if (cfile) {
1019 struct netfs_inode *ictx = netfs_inode(inode);
1020
1021 tcon = tlink_tcon(cfile->tlink);
1022 server = tcon->ses->server;
1023 netfs_wb_begin(ictx, false);
1024 rc = server->ops->set_file_size(xid, tcon,
1025 cfile, 0, false);
1026 if (!rc) {
1027 netfs_resize_file(&cinode->netfs, 0, true);
1028 cifs_setsize(inode, 0);
1029 cifs_invalidate_cache(inode, 0);
1030 }
1031 netfs_wb_end(ictx);
1032 } else {
1033 /*
1034 * No cached handle; evict stale pages so they can't
1035 * be served after the file is later extended; let
1036 * the server's O_TRUNC open response set the i_size
1037 */
1038 truncate_inode_pages(inode->i_mapping, 0);
1039 cifs_invalidate_cache(inode, 0);
1040 }
1041 }
1042
1043 out:
1044 filemap_invalidate_unlock(inode->i_mapping);
1045 inode_unlock(inode);
1046 if (cfile)
1047 cifsFileInfo_put(cfile);
1048 return rc;
1049 }
1050
cifs_open(struct inode * inode,struct file * file)1051 int cifs_open(struct inode *inode, struct file *file)
1052
1053 {
1054 struct cifs_sb_info *cifs_sb = CIFS_SB(inode);
1055 struct cifs_open_info_data data = {};
1056 struct cifsFileInfo *cfile = NULL;
1057 struct TCP_Server_Info *server;
1058 struct cifs_pending_open open;
1059 bool posix_open_ok = false;
1060 struct cifs_fid fid = {};
1061 struct tcon_link *tlink;
1062 struct cifs_tcon *tcon;
1063 const char *full_path;
1064 unsigned int sbflags;
1065 int rc = -EACCES;
1066 unsigned int xid;
1067 __u32 oplock;
1068 void *page;
1069
1070 xid = get_xid();
1071
1072 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
1073 free_xid(xid);
1074 return smb_EIO(smb_eio_trace_forced_shutdown);
1075 }
1076
1077 tlink = cifs_sb_tlink(cifs_sb);
1078 if (IS_ERR(tlink)) {
1079 free_xid(xid);
1080 return PTR_ERR(tlink);
1081 }
1082 tcon = tlink_tcon(tlink);
1083 server = tcon->ses->server;
1084
1085 page = alloc_dentry_path();
1086 full_path = build_path_from_dentry(file_dentry(file), page);
1087 if (IS_ERR(full_path)) {
1088 rc = PTR_ERR(full_path);
1089 goto out;
1090 }
1091
1092 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
1093 inode, file->f_flags, full_path);
1094
1095 sbflags = cifs_sb_flags(cifs_sb);
1096 if ((file->f_flags & O_DIRECT) && (sbflags & CIFS_MOUNT_STRICT_IO)) {
1097 if (sbflags & CIFS_MOUNT_NO_BRL)
1098 file->f_op = &cifs_file_direct_nobrl_ops;
1099 else
1100 file->f_op = &cifs_file_direct_ops;
1101 }
1102
1103 if (file->f_flags & O_TRUNC) {
1104 rc = cifs_do_truncate(xid, file_dentry(file));
1105 if (rc)
1106 goto out;
1107 }
1108
1109 /* Get the cached handle as SMB2 close is deferred */
1110 if (OPEN_FMODE(file->f_flags) & FMODE_WRITE) {
1111 rc = __cifs_get_writable_file(CIFS_I(inode),
1112 FIND_FSUID_ONLY |
1113 FIND_NO_PENDING_DELETE |
1114 FIND_OPEN_FLAGS,
1115 file->f_flags, &cfile);
1116 } else {
1117 cfile = __find_readable_file(CIFS_I(inode),
1118 FIND_NO_PENDING_DELETE |
1119 FIND_OPEN_FLAGS,
1120 file->f_flags);
1121 rc = cfile ? 0 : -ENOENT;
1122 }
1123 if (rc == 0) {
1124 trace_smb3_open_cached(xid, tcon->tid, tcon->ses->Suid,
1125 cfile->fid.persistent_fid,
1126 file->f_flags, cfile->f_flags);
1127 file->private_data = cfile;
1128 spin_lock(&CIFS_I(inode)->deferred_lock);
1129 cifs_del_deferred_close(cfile);
1130 spin_unlock(&CIFS_I(inode)->deferred_lock);
1131 goto use_cache;
1132 }
1133 /* hard link on the deferred close file */
1134 rc = cifs_get_hardlink_path(tcon, inode, file);
1135 if (rc)
1136 cifs_close_deferred_file(CIFS_I(inode));
1137
1138 if (server->oplocks)
1139 oplock = REQ_OPLOCK;
1140 else
1141 oplock = 0;
1142
1143 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1144 if (!tcon->broken_posix_open && tcon->unix_ext &&
1145 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1146 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1147 /* can not refresh inode info since size could be stale */
1148 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
1149 cifs_sb->ctx->file_mode /* ignored */,
1150 file->f_flags, &oplock, &fid.netfid, xid);
1151 if (rc == 0) {
1152 cifs_dbg(FYI, "posix open succeeded\n");
1153 posix_open_ok = true;
1154 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1155 if (tcon->ses->serverNOS)
1156 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",
1157 tcon->ses->ip_addr,
1158 tcon->ses->serverNOS);
1159 tcon->broken_posix_open = true;
1160 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
1161 (rc != -EOPNOTSUPP)) /* path not found or net err */
1162 goto out;
1163 /*
1164 * Else fallthrough to retry open the old way on network i/o
1165 * or DFS errors.
1166 */
1167 }
1168 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1169
1170 if (server->ops->get_lease_key)
1171 server->ops->get_lease_key(inode, &fid);
1172
1173 cifs_add_pending_open(&fid, tlink, &open);
1174
1175 if (!posix_open_ok) {
1176 if (server->ops->get_lease_key)
1177 server->ops->get_lease_key(inode, &fid);
1178
1179 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, file->f_flags, &oplock, &fid,
1180 xid, &data);
1181 if (rc) {
1182 cifs_del_pending_open(&open);
1183 goto out;
1184 }
1185 }
1186
1187 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, data.symlink_target);
1188 if (cfile == NULL) {
1189 if (server->ops->close)
1190 server->ops->close(xid, tcon, &fid);
1191 cifs_del_pending_open(&open);
1192 rc = -ENOMEM;
1193 goto out;
1194 }
1195
1196 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1197 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
1198 /*
1199 * Time to set mode which we can not set earlier due to
1200 * problems creating new read-only files.
1201 */
1202 struct cifs_unix_set_info_args args = {
1203 .mode = inode->i_mode,
1204 .uid = INVALID_UID, /* no change */
1205 .gid = INVALID_GID, /* no change */
1206 .ctime = NO_CHANGE_64,
1207 .atime = NO_CHANGE_64,
1208 .mtime = NO_CHANGE_64,
1209 .device = 0,
1210 };
1211 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
1212 cfile->pid);
1213 }
1214 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1215
1216 use_cache:
1217 fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
1218 file->f_mode & FMODE_WRITE);
1219 if (!(file->f_flags & O_DIRECT))
1220 goto out;
1221 if ((file->f_flags & (O_ACCMODE | O_APPEND)) == O_RDONLY)
1222 goto out;
1223 cifs_invalidate_cache(file_inode(file), FSCACHE_INVAL_DIO_WRITE);
1224
1225 out:
1226 free_dentry_path(page);
1227 free_xid(xid);
1228 cifs_put_tlink(tlink);
1229 cifs_free_open_info(&data);
1230 return rc;
1231 }
1232
1233 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1234 static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
1235 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1236
1237 /*
1238 * Try to reacquire byte range locks that were released when session
1239 * to server was lost.
1240 */
1241 static int
cifs_relock_file(struct cifsFileInfo * cfile)1242 cifs_relock_file(struct cifsFileInfo *cfile)
1243 {
1244 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1245 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1246 int rc = 0;
1247 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1248 struct cifs_sb_info *cifs_sb = CIFS_SB(cinode);
1249 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1250
1251 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
1252 if (cinode->can_cache_brlcks) {
1253 /* can cache locks - no need to relock */
1254 up_read(&cinode->lock_sem);
1255 return rc;
1256 }
1257
1258 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1259 if (cap_unix(tcon->ses) &&
1260 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1261 ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOPOSIXBRL) == 0))
1262 rc = cifs_push_posix_locks(cfile);
1263 else
1264 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1265 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1266
1267 up_read(&cinode->lock_sem);
1268 return rc;
1269 }
1270
1271 static int
cifs_reopen_file(struct cifsFileInfo * cfile,bool can_flush)1272 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
1273 {
1274 int rc = -EACCES;
1275 unsigned int xid;
1276 __u32 oplock;
1277 struct cifs_sb_info *cifs_sb;
1278 struct cifs_tcon *tcon;
1279 struct TCP_Server_Info *server;
1280 struct cifsInodeInfo *cinode;
1281 struct inode *inode;
1282 void *page;
1283 const char *full_path;
1284 int desired_access;
1285 int disposition = FILE_OPEN;
1286 int create_options = CREATE_NOT_DIR;
1287 struct cifs_open_parms oparms;
1288 int rdwr_for_fscache = 0;
1289
1290 xid = get_xid();
1291 mutex_lock(&cfile->fh_mutex);
1292 if (!cfile->invalidHandle) {
1293 mutex_unlock(&cfile->fh_mutex);
1294 free_xid(xid);
1295 return 0;
1296 }
1297
1298 inode = d_inode(cfile->dentry);
1299 cifs_sb = CIFS_SB(inode->i_sb);
1300 tcon = tlink_tcon(cfile->tlink);
1301 server = tcon->ses->server;
1302
1303 /*
1304 * Can not grab rename sem here because various ops, including those
1305 * that already have the rename sem can end up causing writepage to get
1306 * called and if the server was down that means we end up here, and we
1307 * can never tell if the caller already has the rename_sem.
1308 */
1309 page = alloc_dentry_path();
1310 full_path = build_path_from_dentry(cfile->dentry, page);
1311 if (IS_ERR(full_path)) {
1312 mutex_unlock(&cfile->fh_mutex);
1313 free_dentry_path(page);
1314 free_xid(xid);
1315 return PTR_ERR(full_path);
1316 }
1317
1318 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
1319 inode, cfile->f_flags, full_path);
1320
1321 if (tcon->ses->server->oplocks)
1322 oplock = REQ_OPLOCK;
1323 else
1324 oplock = 0;
1325
1326 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1327 if (tcon->unix_ext && cap_unix(tcon->ses) &&
1328 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1329 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1330 /*
1331 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
1332 * original open. Must mask them off for a reopen.
1333 */
1334 unsigned int oflags = cfile->f_flags &
1335 ~(O_CREAT | O_EXCL | O_TRUNC);
1336
1337 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
1338 cifs_sb->ctx->file_mode /* ignored */,
1339 oflags, &oplock, &cfile->fid.netfid, xid);
1340 if (rc == 0) {
1341 cifs_dbg(FYI, "posix reopen succeeded\n");
1342 oparms.reconnect = true;
1343 goto reopen_success;
1344 }
1345 /*
1346 * fallthrough to retry open the old way on errors, especially
1347 * in the reconnect path it is important to retry hard
1348 */
1349 }
1350 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1351
1352 /* If we're caching, we need to be able to fill in around partial writes. */
1353 if (cifs_fscache_enabled(inode) && (cfile->f_flags & O_ACCMODE) == O_WRONLY)
1354 rdwr_for_fscache = 1;
1355
1356 desired_access = cifs_convert_flags(cfile->f_flags, rdwr_for_fscache);
1357 create_options |= cifs_open_create_options(cfile->f_flags,
1358 create_options);
1359
1360 if (server->ops->get_lease_key)
1361 server->ops->get_lease_key(inode, &cfile->fid);
1362
1363 retry_open:
1364 oparms = (struct cifs_open_parms) {
1365 .tcon = tcon,
1366 .cifs_sb = cifs_sb,
1367 .desired_access = desired_access,
1368 .create_options = cifs_create_options(cifs_sb, create_options),
1369 .disposition = disposition,
1370 .path = full_path,
1371 .fid = &cfile->fid,
1372 .reconnect = true,
1373 };
1374
1375 /*
1376 * Can not refresh inode by passing in file_info buf to be returned by
1377 * ops->open and then calling get_inode_info with returned buf since
1378 * file might have write behind data that needs to be flushed and server
1379 * version of file size can be stale. If we knew for sure that inode was
1380 * not dirty locally we could do this.
1381 */
1382 rc = server->ops->open(xid, &oparms, &oplock, NULL);
1383 if (rc == -ENOENT && oparms.reconnect == false) {
1384 /* durable handle timeout is expired - open the file again */
1385 rc = server->ops->open(xid, &oparms, &oplock, NULL);
1386 /* indicate that we need to relock the file */
1387 oparms.reconnect = true;
1388 }
1389 if (rc == -EACCES && rdwr_for_fscache == 1) {
1390 desired_access = cifs_convert_flags(cfile->f_flags, 0);
1391 rdwr_for_fscache = 2;
1392 goto retry_open;
1393 }
1394
1395 if (rc) {
1396 mutex_unlock(&cfile->fh_mutex);
1397 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
1398 cifs_dbg(FYI, "oplock: %d\n", oplock);
1399 goto reopen_error_exit;
1400 }
1401
1402 if (rdwr_for_fscache == 2)
1403 cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE);
1404
1405 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1406 reopen_success:
1407 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1408 cfile->invalidHandle = false;
1409 mutex_unlock(&cfile->fh_mutex);
1410 cinode = CIFS_I(inode);
1411
1412 if (can_flush) {
1413 rc = filemap_write_and_wait(inode->i_mapping);
1414 if (!is_interrupt_error(rc))
1415 mapping_set_error(inode->i_mapping, rc);
1416
1417 if (tcon->posix_extensions) {
1418 rc = smb311_posix_get_inode_info(&inode, full_path,
1419 NULL, inode->i_sb, xid);
1420 } else if (tcon->unix_ext) {
1421 rc = cifs_get_inode_info_unix(&inode, full_path,
1422 inode->i_sb, xid);
1423 } else {
1424 rc = cifs_get_inode_info(&inode, full_path, NULL,
1425 inode->i_sb, xid, NULL);
1426 }
1427 }
1428 /*
1429 * Else we are writing out data to server already and could deadlock if
1430 * we tried to flush data, and since we do not know if we have data that
1431 * would invalidate the current end of file on the server we can not go
1432 * to the server to get the new inode info.
1433 */
1434
1435 /*
1436 * If the server returned a read oplock and we have mandatory brlocks,
1437 * set oplock level to None.
1438 */
1439 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
1440 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
1441 oplock = 0;
1442 }
1443
1444 scoped_guard(spinlock, &cinode->open_file_lock)
1445 server->ops->set_fid(cfile, &cfile->fid, oplock);
1446 if (oparms.reconnect)
1447 cifs_relock_file(cfile);
1448
1449 reopen_error_exit:
1450 free_dentry_path(page);
1451 free_xid(xid);
1452 return rc;
1453 }
1454
smb2_deferred_work_close(struct work_struct * work)1455 void smb2_deferred_work_close(struct work_struct *work)
1456 {
1457 struct cifsFileInfo *cfile = container_of(work,
1458 struct cifsFileInfo, deferred.work);
1459 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1460
1461 spin_lock(&cinode->deferred_lock);
1462 cifs_del_deferred_close(cfile);
1463 cfile->deferred_close_scheduled = false;
1464 spin_unlock(&cinode->deferred_lock);
1465 _cifsFileInfo_put(cfile, true, false);
1466 }
1467
1468 static bool
smb2_can_defer_close(struct inode * inode,struct cifs_deferred_close * dclose)1469 smb2_can_defer_close(struct inode *inode, struct cifs_deferred_close *dclose)
1470 {
1471 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1472 struct cifsInodeInfo *cinode = CIFS_I(inode);
1473 unsigned int oplock = READ_ONCE(cinode->oplock);
1474
1475 return cifs_sb->ctx->closetimeo && cinode->lease_granted && dclose &&
1476 (oplock == CIFS_CACHE_RHW_FLG || oplock == CIFS_CACHE_RH_FLG) &&
1477 !test_bit(CIFS_INO_CLOSE_ON_LOCK, &cinode->flags);
1478
1479 }
1480
cifs_close(struct inode * inode,struct file * file)1481 int cifs_close(struct inode *inode, struct file *file)
1482 {
1483 struct cifsFileInfo *cfile;
1484 struct cifsInodeInfo *cinode = CIFS_I(inode);
1485 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1486 struct cifs_deferred_close *dclose;
1487 struct cifs_tcon *tcon;
1488
1489 cifs_fscache_unuse_inode_cookie(inode, file->f_mode & FMODE_WRITE);
1490
1491 if (file->private_data != NULL) {
1492 cfile = file->private_data;
1493 file->private_data = NULL;
1494 dclose = kmalloc_obj(struct cifs_deferred_close);
1495 if ((cfile->status_file_deleted == false) &&
1496 (smb2_can_defer_close(inode, dclose))) {
1497 if (test_and_clear_bit(NETFS_ICTX_MODIFIED_ATTR, &cinode->netfs.flags)) {
1498 inode_set_mtime_to_ts(inode,
1499 inode_set_ctime_current(inode));
1500 }
1501 spin_lock(&cinode->deferred_lock);
1502 cifs_add_deferred_close(cfile, dclose);
1503 if (cfile->deferred_close_scheduled &&
1504 delayed_work_pending(&cfile->deferred)) {
1505 /*
1506 * If there is no pending work, mod_delayed_work queues new work.
1507 * So, Increase the ref count to avoid use-after-free.
1508 */
1509 if (!mod_delayed_work(deferredclose_wq,
1510 &cfile->deferred, cifs_sb->ctx->closetimeo))
1511 cifsFileInfo_get(cfile);
1512 } else {
1513 /* Deferred close for files */
1514 tcon = tlink_tcon(cfile->tlink);
1515 trace_smb3_close_cached(tcon->tid, tcon->ses->Suid,
1516 cfile->fid.persistent_fid,
1517 cifs_sb->ctx->closetimeo);
1518 queue_delayed_work(deferredclose_wq,
1519 &cfile->deferred, cifs_sb->ctx->closetimeo);
1520 cfile->deferred_close_scheduled = true;
1521 spin_unlock(&cinode->deferred_lock);
1522 return 0;
1523 }
1524 spin_unlock(&cinode->deferred_lock);
1525 _cifsFileInfo_put(cfile, true, false);
1526 } else {
1527 _cifsFileInfo_put(cfile, true, false);
1528 kfree(dclose);
1529 }
1530 }
1531
1532 /* return code from the ->release op is always ignored */
1533 return 0;
1534 }
1535
1536 void
cifs_reopen_persistent_handles(struct cifs_tcon * tcon)1537 cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
1538 {
1539 struct cifsFileInfo *open_file, *tmp;
1540 LIST_HEAD(tmp_list);
1541
1542 if (!tcon->use_persistent || !tcon->need_reopen_files)
1543 return;
1544
1545 tcon->need_reopen_files = false;
1546
1547 cifs_dbg(FYI, "Reopen persistent handles\n");
1548
1549 /* list all files open on tree connection, reopen resilient handles */
1550 spin_lock(&tcon->open_file_lock);
1551 list_for_each_entry(open_file, &tcon->openFileList, tlist) {
1552 if (!open_file->invalidHandle)
1553 continue;
1554 cifsFileInfo_get(open_file);
1555 list_add_tail(&open_file->rlist, &tmp_list);
1556 }
1557 spin_unlock(&tcon->open_file_lock);
1558
1559 list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) {
1560 if (cifs_reopen_file(open_file, false /* do not flush */))
1561 tcon->need_reopen_files = true;
1562 list_del_init(&open_file->rlist);
1563 cifsFileInfo_put(open_file);
1564 }
1565 }
1566
cifs_closedir(struct inode * inode,struct file * file)1567 int cifs_closedir(struct inode *inode, struct file *file)
1568 {
1569 int rc = 0;
1570 unsigned int xid;
1571 struct cifsFileInfo *cfile = file->private_data;
1572 struct cifs_tcon *tcon;
1573 struct TCP_Server_Info *server;
1574 char *buf;
1575
1576 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
1577
1578 if (cfile == NULL)
1579 return rc;
1580
1581 xid = get_xid();
1582 tcon = tlink_tcon(cfile->tlink);
1583 server = tcon->ses->server;
1584
1585 cifs_dbg(FYI, "Freeing private data in close dir\n");
1586 spin_lock(&cfile->file_info_lock);
1587 if (server->ops->dir_needs_close(cfile)) {
1588 cfile->invalidHandle = true;
1589 spin_unlock(&cfile->file_info_lock);
1590 if (server->ops->close_dir)
1591 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
1592 else
1593 rc = -ENOSYS;
1594 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
1595 /* not much we can do if it fails anyway, ignore rc */
1596 rc = 0;
1597 } else
1598 spin_unlock(&cfile->file_info_lock);
1599
1600 buf = cfile->srch_inf.ntwrk_buf_start;
1601 if (buf) {
1602 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
1603 cfile->srch_inf.ntwrk_buf_start = NULL;
1604 if (cfile->srch_inf.smallBuf)
1605 cifs_small_buf_release(buf);
1606 else if (cfile->srch_inf.is_dynamic_buf)
1607 kfree(buf);
1608 else
1609 cifs_buf_release(buf);
1610 }
1611
1612 cifs_put_tlink(cfile->tlink);
1613 kfree(file->private_data);
1614 file->private_data = NULL;
1615 /* BB can we lock the filestruct while this is going on? */
1616 free_xid(xid);
1617 return rc;
1618 }
1619
1620 static struct cifsLockInfo *
cifs_lock_init(__u64 offset,__u64 length,__u8 type,__u16 flags)1621 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
1622 {
1623 struct cifsLockInfo *lock =
1624 kmalloc_obj(struct cifsLockInfo);
1625 if (!lock)
1626 return lock;
1627 lock->offset = offset;
1628 lock->length = length;
1629 lock->type = type;
1630 lock->pid = current->tgid;
1631 lock->flags = flags;
1632 INIT_LIST_HEAD(&lock->blist);
1633 init_waitqueue_head(&lock->block_q);
1634 return lock;
1635 }
1636
1637 void
cifs_del_lock_waiters(struct cifsLockInfo * lock)1638 cifs_del_lock_waiters(struct cifsLockInfo *lock)
1639 {
1640 struct cifsLockInfo *li, *tmp;
1641 list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
1642 list_del_init(&li->blist);
1643 wake_up(&li->block_q);
1644 }
1645 }
1646
1647 #define CIFS_LOCK_OP 0
1648 #define CIFS_READ_OP 1
1649 #define CIFS_WRITE_OP 2
1650
1651 /* @rw_check : 0 - no op, 1 - read, 2 - write */
1652 static bool
cifs_find_fid_lock_conflict(struct cifs_fid_locks * fdlocks,__u64 offset,__u64 length,__u8 type,__u16 flags,struct cifsFileInfo * cfile,struct cifsLockInfo ** conf_lock,int rw_check)1653 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
1654 __u64 length, __u8 type, __u16 flags,
1655 struct cifsFileInfo *cfile,
1656 struct cifsLockInfo **conf_lock, int rw_check)
1657 {
1658 struct cifsLockInfo *li;
1659 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
1660 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1661
1662 list_for_each_entry(li, &fdlocks->locks, llist) {
1663 if (offset + length <= li->offset ||
1664 offset >= li->offset + li->length)
1665 continue;
1666 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
1667 server->ops->compare_fids(cfile, cur_cfile)) {
1668 /* shared lock prevents write op through the same fid */
1669 if (!(li->type & server->vals->shared_lock_type) ||
1670 rw_check != CIFS_WRITE_OP)
1671 continue;
1672 }
1673 if ((type & server->vals->shared_lock_type) &&
1674 ((server->ops->compare_fids(cfile, cur_cfile) &&
1675 current->tgid == li->pid) || type == li->type))
1676 continue;
1677 if (rw_check == CIFS_LOCK_OP &&
1678 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
1679 server->ops->compare_fids(cfile, cur_cfile))
1680 continue;
1681 if (conf_lock)
1682 *conf_lock = li;
1683 trace_smb3_lock_conflict(cfile->fid.persistent_fid,
1684 offset, length, type,
1685 li->offset, li->length, li->type, li->pid);
1686 return true;
1687 }
1688 return false;
1689 }
1690
1691 bool
cifs_find_lock_conflict(struct cifsFileInfo * cfile,__u64 offset,__u64 length,__u8 type,__u16 flags,struct cifsLockInfo ** conf_lock,int rw_check)1692 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1693 __u8 type, __u16 flags,
1694 struct cifsLockInfo **conf_lock, int rw_check)
1695 {
1696 bool rc = false;
1697 struct cifs_fid_locks *cur;
1698 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1699
1700 list_for_each_entry(cur, &cinode->llist, llist) {
1701 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
1702 flags, cfile, conf_lock,
1703 rw_check);
1704 if (rc)
1705 break;
1706 }
1707
1708 return rc;
1709 }
1710
1711 /*
1712 * Check if there is another lock that prevents us to set the lock (mandatory
1713 * style). If such a lock exists, update the flock structure with its
1714 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1715 * or leave it the same if we can't. Returns 0 if we don't need to request to
1716 * the server or 1 otherwise.
1717 */
1718 static int
cifs_lock_test(struct cifsFileInfo * cfile,__u64 offset,__u64 length,__u8 type,struct file_lock * flock)1719 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1720 __u8 type, struct file_lock *flock)
1721 {
1722 int rc = 0;
1723 struct cifsLockInfo *conf_lock;
1724 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1725 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1726 bool exist;
1727
1728 down_read(&cinode->lock_sem);
1729
1730 exist = cifs_find_lock_conflict(cfile, offset, length, type,
1731 flock->c.flc_flags, &conf_lock,
1732 CIFS_LOCK_OP);
1733 if (exist) {
1734 flock->fl_start = conf_lock->offset;
1735 flock->fl_end = conf_lock->offset + conf_lock->length - 1;
1736 flock->c.flc_pid = conf_lock->pid;
1737 if (conf_lock->type & server->vals->shared_lock_type)
1738 flock->c.flc_type = F_RDLCK;
1739 else
1740 flock->c.flc_type = F_WRLCK;
1741 } else if (!cinode->can_cache_brlcks)
1742 rc = 1;
1743 else
1744 flock->c.flc_type = F_UNLCK;
1745
1746 up_read(&cinode->lock_sem);
1747 return rc;
1748 }
1749
1750 static void
cifs_lock_add(struct cifsFileInfo * cfile,struct cifsLockInfo * lock)1751 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
1752 {
1753 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1754 cifs_down_write(&cinode->lock_sem);
1755 list_add_tail(&lock->llist, &cfile->llist->locks);
1756 up_write(&cinode->lock_sem);
1757 }
1758
1759 /*
1760 * Set the byte-range lock (mandatory style). Returns:
1761 * 1) 0, if we set the lock and don't need to request to the server;
1762 * 2) 1, if no locks prevent us but we need to request to the server;
1763 * 3) -EACCES, if there is a lock that prevents us and wait is false.
1764 */
1765 static int
cifs_lock_add_if(struct cifsFileInfo * cfile,struct cifsLockInfo * lock,bool wait,unsigned int xid)1766 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
1767 bool wait, unsigned int xid)
1768 {
1769 struct cifsLockInfo *conf_lock;
1770 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1771 bool exist;
1772 int rc = 0;
1773
1774 try_again:
1775 exist = false;
1776 cifs_down_write(&cinode->lock_sem);
1777
1778 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
1779 lock->type, lock->flags, &conf_lock,
1780 CIFS_LOCK_OP);
1781 if (!exist && cinode->can_cache_brlcks) {
1782 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1783
1784 list_add_tail(&lock->llist, &cfile->llist->locks);
1785 trace_smb3_lock_cached(xid, cfile->fid.persistent_fid,
1786 tcon->tid, tcon->ses->Suid,
1787 lock->offset, lock->length,
1788 lock->type, 1, 0);
1789 up_write(&cinode->lock_sem);
1790 return rc;
1791 }
1792
1793 if (!exist)
1794 rc = 1;
1795 else if (!wait)
1796 rc = -EACCES;
1797 else {
1798 list_add_tail(&lock->blist, &conf_lock->blist);
1799 up_write(&cinode->lock_sem);
1800 rc = wait_event_interruptible(lock->block_q,
1801 (lock->blist.prev == &lock->blist) &&
1802 (lock->blist.next == &lock->blist));
1803 if (!rc)
1804 goto try_again;
1805 cifs_down_write(&cinode->lock_sem);
1806 list_del_init(&lock->blist);
1807 }
1808
1809 up_write(&cinode->lock_sem);
1810 return rc;
1811 }
1812
1813 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1814 /*
1815 * Check if there is another lock that prevents us to set the lock (posix
1816 * style). If such a lock exists, update the flock structure with its
1817 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1818 * or leave it the same if we can't. Returns 0 if we don't need to request to
1819 * the server or 1 otherwise.
1820 */
1821 static int
cifs_posix_lock_test(struct file * file,struct file_lock * flock)1822 cifs_posix_lock_test(struct file *file, struct file_lock *flock)
1823 {
1824 int rc = 0;
1825 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1826 unsigned char saved_type = flock->c.flc_type;
1827
1828 if ((flock->c.flc_flags & FL_POSIX) == 0)
1829 return 1;
1830
1831 down_read(&cinode->lock_sem);
1832 posix_test_lock(file, flock);
1833
1834 if (lock_is_unlock(flock) && !cinode->can_cache_brlcks) {
1835 flock->c.flc_type = saved_type;
1836 rc = 1;
1837 }
1838
1839 up_read(&cinode->lock_sem);
1840 return rc;
1841 }
1842
1843 /*
1844 * Set the byte-range lock (posix style). Returns:
1845 * 1) <0, if the error occurs while setting the lock;
1846 * 2) 0, if we set the lock and don't need to request to the server;
1847 * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
1848 * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
1849 */
1850 static int
cifs_posix_lock_set(struct file * file,struct file_lock * flock)1851 cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1852 {
1853 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1854 int rc = FILE_LOCK_DEFERRED + 1;
1855
1856 if ((flock->c.flc_flags & FL_POSIX) == 0)
1857 return rc;
1858
1859 cifs_down_write(&cinode->lock_sem);
1860 if (!cinode->can_cache_brlcks) {
1861 up_write(&cinode->lock_sem);
1862 return rc;
1863 }
1864
1865 rc = posix_lock_file(file, flock, NULL);
1866 up_write(&cinode->lock_sem);
1867 return rc;
1868 }
1869
1870 int
cifs_push_mandatory_locks(struct cifsFileInfo * cfile)1871 cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
1872 {
1873 unsigned int xid;
1874 int rc = 0, stored_rc;
1875 struct cifsLockInfo *li, *tmp;
1876 struct cifs_tcon *tcon;
1877 unsigned int num, max_num, max_buf;
1878 LOCKING_ANDX_RANGE *buf, *cur;
1879 static const int types[] = {
1880 LOCKING_ANDX_LARGE_FILES,
1881 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1882 };
1883 int i;
1884
1885 xid = get_xid();
1886 tcon = tlink_tcon(cfile->tlink);
1887
1888 /*
1889 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1890 * and check it before using.
1891 */
1892 max_buf = tcon->ses->server->maxBuf;
1893 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
1894 free_xid(xid);
1895 return -EINVAL;
1896 }
1897
1898 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1899 PAGE_SIZE);
1900 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1901 PAGE_SIZE);
1902 max_num = (max_buf - sizeof(struct smb_hdr)) /
1903 sizeof(LOCKING_ANDX_RANGE);
1904 buf = kzalloc_objs(LOCKING_ANDX_RANGE, max_num);
1905 if (!buf) {
1906 free_xid(xid);
1907 return -ENOMEM;
1908 }
1909
1910 for (i = 0; i < 2; i++) {
1911 cur = buf;
1912 num = 0;
1913 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1914 if (li->type != types[i])
1915 continue;
1916 cur->Pid = cpu_to_le16(li->pid);
1917 cur->LengthLow = cpu_to_le32((u32)li->length);
1918 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1919 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1920 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1921 if (++num == max_num) {
1922 stored_rc = cifs_lockv(xid, tcon,
1923 cfile->fid.netfid,
1924 (__u8)li->type, 0, num,
1925 buf);
1926 if (stored_rc)
1927 rc = stored_rc;
1928 cur = buf;
1929 num = 0;
1930 } else
1931 cur++;
1932 }
1933
1934 if (num) {
1935 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1936 (__u8)types[i], 0, num, buf);
1937 if (stored_rc)
1938 rc = stored_rc;
1939 }
1940 }
1941
1942 kfree(buf);
1943 free_xid(xid);
1944 return rc;
1945 }
1946
1947 static __u32
hash_lockowner(fl_owner_t owner)1948 hash_lockowner(fl_owner_t owner)
1949 {
1950 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1951 }
1952 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1953
1954 struct lock_to_push {
1955 struct list_head llist;
1956 __u64 offset;
1957 __u64 length;
1958 __u32 pid;
1959 __u16 netfid;
1960 __u8 type;
1961 };
1962
1963 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1964 static int
cifs_push_posix_locks(struct cifsFileInfo * cfile)1965 cifs_push_posix_locks(struct cifsFileInfo *cfile)
1966 {
1967 struct inode *inode = d_inode(cfile->dentry);
1968 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1969 struct file_lock *flock;
1970 struct file_lock_context *flctx = locks_inode_context(inode);
1971 unsigned int count = 0, i;
1972 int rc = 0, xid, type;
1973 struct list_head locks_to_send, *el;
1974 struct lock_to_push *lck, *tmp;
1975 __u64 length;
1976
1977 xid = get_xid();
1978
1979 if (!flctx)
1980 goto out;
1981
1982 spin_lock(&flctx->flc_lock);
1983 list_for_each(el, &flctx->flc_posix) {
1984 count++;
1985 }
1986 spin_unlock(&flctx->flc_lock);
1987
1988 INIT_LIST_HEAD(&locks_to_send);
1989
1990 /*
1991 * Allocating count locks is enough because no FL_POSIX locks can be
1992 * added to the list while we are holding cinode->lock_sem that
1993 * protects locking operations of this inode.
1994 */
1995 for (i = 0; i < count; i++) {
1996 lck = kmalloc_obj(struct lock_to_push);
1997 if (!lck) {
1998 rc = -ENOMEM;
1999 goto err_out;
2000 }
2001 list_add_tail(&lck->llist, &locks_to_send);
2002 }
2003
2004 el = locks_to_send.next;
2005 spin_lock(&flctx->flc_lock);
2006 for_each_file_lock(flock, &flctx->flc_posix) {
2007 unsigned char ftype = flock->c.flc_type;
2008
2009 if (el == &locks_to_send) {
2010 /*
2011 * The list ended. We don't have enough allocated
2012 * structures - something is really wrong.
2013 */
2014 cifs_dbg(VFS, "Can't push all brlocks!\n");
2015 break;
2016 }
2017 length = cifs_flock_len(flock);
2018 if (ftype == F_RDLCK || ftype == F_SHLCK)
2019 type = CIFS_RDLCK;
2020 else
2021 type = CIFS_WRLCK;
2022 lck = list_entry(el, struct lock_to_push, llist);
2023 lck->pid = hash_lockowner(flock->c.flc_owner);
2024 lck->netfid = cfile->fid.netfid;
2025 lck->length = length;
2026 lck->type = type;
2027 lck->offset = flock->fl_start;
2028 }
2029 spin_unlock(&flctx->flc_lock);
2030
2031 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
2032 int stored_rc;
2033
2034 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
2035 lck->offset, lck->length, NULL,
2036 lck->type, 0);
2037 if (stored_rc)
2038 rc = stored_rc;
2039 list_del(&lck->llist);
2040 kfree(lck);
2041 }
2042
2043 out:
2044 free_xid(xid);
2045 return rc;
2046 err_out:
2047 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
2048 list_del(&lck->llist);
2049 kfree(lck);
2050 }
2051 goto out;
2052 }
2053 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2054
2055 static int
cifs_push_locks(struct cifsFileInfo * cfile)2056 cifs_push_locks(struct cifsFileInfo *cfile)
2057 {
2058 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
2059 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
2060 int rc = 0;
2061 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2062 struct cifs_sb_info *cifs_sb = CIFS_SB(cinode);
2063 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2064
2065 /* we are going to update can_cache_brlcks here - need a write access */
2066 cifs_down_write(&cinode->lock_sem);
2067 if (!cinode->can_cache_brlcks) {
2068 up_write(&cinode->lock_sem);
2069 return rc;
2070 }
2071
2072 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2073 if (cap_unix(tcon->ses) &&
2074 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
2075 ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOPOSIXBRL) == 0))
2076 rc = cifs_push_posix_locks(cfile);
2077 else
2078 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2079 rc = tcon->ses->server->ops->push_mand_locks(cfile);
2080
2081 cinode->can_cache_brlcks = false;
2082 up_write(&cinode->lock_sem);
2083 return rc;
2084 }
2085
2086 static void
cifs_read_flock(struct file_lock * flock,__u32 * type,int * lock,int * unlock,bool * wait_flag,struct TCP_Server_Info * server)2087 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
2088 bool *wait_flag, struct TCP_Server_Info *server)
2089 {
2090 if (flock->c.flc_flags & FL_POSIX)
2091 cifs_dbg(FYI, "Posix\n");
2092 if (flock->c.flc_flags & FL_FLOCK)
2093 cifs_dbg(FYI, "Flock\n");
2094 if (flock->c.flc_flags & FL_SLEEP) {
2095 cifs_dbg(FYI, "Blocking lock\n");
2096 *wait_flag = true;
2097 }
2098 if (flock->c.flc_flags & FL_ACCESS)
2099 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
2100 if (flock->c.flc_flags & FL_LEASE)
2101 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
2102 if (flock->c.flc_flags &
2103 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
2104 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
2105 cifs_dbg(FYI, "Unknown lock flags 0x%x\n",
2106 flock->c.flc_flags);
2107
2108 *type = server->vals->large_lock_type;
2109 if (lock_is_write(flock)) {
2110 cifs_dbg(FYI, "F_WRLCK\n");
2111 *type |= server->vals->exclusive_lock_type;
2112 *lock = 1;
2113 } else if (lock_is_unlock(flock)) {
2114 cifs_dbg(FYI, "F_UNLCK\n");
2115 *type |= server->vals->unlock_lock_type;
2116 *unlock = 1;
2117 /* Check if unlock includes more than one lock range */
2118 } else if (lock_is_read(flock)) {
2119 cifs_dbg(FYI, "F_RDLCK\n");
2120 *type |= server->vals->shared_lock_type;
2121 *lock = 1;
2122 } else if (flock->c.flc_type == F_EXLCK) {
2123 cifs_dbg(FYI, "F_EXLCK\n");
2124 *type |= server->vals->exclusive_lock_type;
2125 *lock = 1;
2126 } else if (flock->c.flc_type == F_SHLCK) {
2127 cifs_dbg(FYI, "F_SHLCK\n");
2128 *type |= server->vals->shared_lock_type;
2129 *lock = 1;
2130 } else
2131 cifs_dbg(FYI, "Unknown type of lock\n");
2132 }
2133
2134 static int
cifs_getlk(struct file * file,struct file_lock * flock,__u32 type,bool wait_flag,bool posix_lck,unsigned int xid)2135 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
2136 bool wait_flag, bool posix_lck, unsigned int xid)
2137 {
2138 int rc = 0;
2139 __u64 length = cifs_flock_len(flock);
2140 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
2141 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
2142 struct TCP_Server_Info *server = tcon->ses->server;
2143 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2144 __u16 netfid = cfile->fid.netfid;
2145
2146 if (posix_lck) {
2147 int posix_lock_type;
2148
2149 rc = cifs_posix_lock_test(file, flock);
2150 if (!rc)
2151 return rc;
2152
2153 if (type & server->vals->shared_lock_type)
2154 posix_lock_type = CIFS_RDLCK;
2155 else
2156 posix_lock_type = CIFS_WRLCK;
2157 rc = CIFSSMBPosixLock(xid, tcon, netfid,
2158 hash_lockowner(flock->c.flc_owner),
2159 flock->fl_start, length, flock,
2160 posix_lock_type, wait_flag);
2161 return rc;
2162 }
2163 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2164
2165 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
2166 if (!rc)
2167 return rc;
2168
2169 /* BB we could chain these into one lock request BB */
2170 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
2171 1, 0, false);
2172 if (rc == 0) {
2173 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
2174 type, 0, 1, false);
2175 flock->c.flc_type = F_UNLCK;
2176 if (rc != 0)
2177 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
2178 rc);
2179 return 0;
2180 }
2181
2182 if (type & server->vals->shared_lock_type) {
2183 flock->c.flc_type = F_WRLCK;
2184 return 0;
2185 }
2186
2187 type &= ~server->vals->exclusive_lock_type;
2188
2189 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
2190 type | server->vals->shared_lock_type,
2191 1, 0, false);
2192 if (rc == 0) {
2193 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
2194 type | server->vals->shared_lock_type, 0, 1, false);
2195 flock->c.flc_type = F_RDLCK;
2196 if (rc != 0)
2197 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
2198 rc);
2199 } else
2200 flock->c.flc_type = F_WRLCK;
2201
2202 return 0;
2203 }
2204
2205 void
cifs_move_llist(struct list_head * source,struct list_head * dest)2206 cifs_move_llist(struct list_head *source, struct list_head *dest)
2207 {
2208 struct list_head *li, *tmp;
2209 list_for_each_safe(li, tmp, source)
2210 list_move(li, dest);
2211 }
2212
2213 int
cifs_get_hardlink_path(struct cifs_tcon * tcon,struct inode * inode,struct file * file)2214 cifs_get_hardlink_path(struct cifs_tcon *tcon, struct inode *inode,
2215 struct file *file)
2216 {
2217 struct cifsFileInfo *open_file = NULL;
2218 struct cifsInodeInfo *cinode = CIFS_I(inode);
2219 int rc = 0;
2220
2221 spin_lock(&tcon->open_file_lock);
2222 spin_lock(&cinode->open_file_lock);
2223
2224 list_for_each_entry(open_file, &cinode->openFileList, flist) {
2225 if (file->f_flags == open_file->f_flags) {
2226 rc = -EINVAL;
2227 break;
2228 }
2229 }
2230
2231 spin_unlock(&cinode->open_file_lock);
2232 spin_unlock(&tcon->open_file_lock);
2233 return rc;
2234 }
2235
2236 void
cifs_free_llist(struct list_head * llist)2237 cifs_free_llist(struct list_head *llist)
2238 {
2239 struct cifsLockInfo *li, *tmp;
2240 list_for_each_entry_safe(li, tmp, llist, llist) {
2241 cifs_del_lock_waiters(li);
2242 list_del(&li->llist);
2243 kfree(li);
2244 }
2245 }
2246
2247 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2248 int
cifs_unlock_range(struct cifsFileInfo * cfile,struct file_lock * flock,unsigned int xid)2249 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
2250 unsigned int xid)
2251 {
2252 int rc = 0, stored_rc;
2253 static const int types[] = {
2254 LOCKING_ANDX_LARGE_FILES,
2255 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
2256 };
2257 unsigned int i;
2258 unsigned int max_num, num, max_buf;
2259 LOCKING_ANDX_RANGE *buf, *cur;
2260 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
2261 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
2262 struct cifsLockInfo *li, *tmp;
2263 __u64 length = cifs_flock_len(flock);
2264 LIST_HEAD(tmp_llist);
2265
2266 /*
2267 * Accessing maxBuf is racy with cifs_reconnect - need to store value
2268 * and check it before using.
2269 */
2270 max_buf = tcon->ses->server->maxBuf;
2271 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
2272 return -EINVAL;
2273
2274 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
2275 PAGE_SIZE);
2276 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
2277 PAGE_SIZE);
2278 max_num = (max_buf - sizeof(struct smb_hdr)) /
2279 sizeof(LOCKING_ANDX_RANGE);
2280 buf = kzalloc_objs(LOCKING_ANDX_RANGE, max_num);
2281 if (!buf)
2282 return -ENOMEM;
2283
2284 cifs_down_write(&cinode->lock_sem);
2285 for (i = 0; i < 2; i++) {
2286 cur = buf;
2287 num = 0;
2288 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
2289 if (flock->fl_start > li->offset ||
2290 (flock->fl_start + length) <
2291 (li->offset + li->length))
2292 continue;
2293 if (current->tgid != li->pid)
2294 continue;
2295 if (types[i] != li->type)
2296 continue;
2297 if (cinode->can_cache_brlcks) {
2298 /*
2299 * We can cache brlock requests - simply remove
2300 * a lock from the file's list.
2301 */
2302 list_del(&li->llist);
2303 cifs_del_lock_waiters(li);
2304 kfree(li);
2305 continue;
2306 }
2307 cur->Pid = cpu_to_le16(li->pid);
2308 cur->LengthLow = cpu_to_le32((u32)li->length);
2309 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
2310 cur->OffsetLow = cpu_to_le32((u32)li->offset);
2311 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
2312 /*
2313 * We need to save a lock here to let us add it again to
2314 * the file's list if the unlock range request fails on
2315 * the server.
2316 */
2317 list_move(&li->llist, &tmp_llist);
2318 if (++num == max_num) {
2319 stored_rc = cifs_lockv(xid, tcon,
2320 cfile->fid.netfid,
2321 li->type, num, 0, buf);
2322 if (stored_rc) {
2323 /*
2324 * We failed on the unlock range
2325 * request - add all locks from the tmp
2326 * list to the head of the file's list.
2327 */
2328 cifs_move_llist(&tmp_llist,
2329 &cfile->llist->locks);
2330 rc = stored_rc;
2331 } else
2332 /*
2333 * The unlock range request succeed -
2334 * free the tmp list.
2335 */
2336 cifs_free_llist(&tmp_llist);
2337 cur = buf;
2338 num = 0;
2339 } else
2340 cur++;
2341 }
2342 if (num) {
2343 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
2344 types[i], num, 0, buf);
2345 if (stored_rc) {
2346 cifs_move_llist(&tmp_llist,
2347 &cfile->llist->locks);
2348 rc = stored_rc;
2349 } else
2350 cifs_free_llist(&tmp_llist);
2351 }
2352 }
2353
2354 up_write(&cinode->lock_sem);
2355 kfree(buf);
2356 return rc;
2357 }
2358 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2359
2360 static int
cifs_setlk(struct file * file,struct file_lock * flock,__u32 type,bool wait_flag,bool posix_lck,int lock,int unlock,unsigned int xid)2361 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
2362 bool wait_flag, bool posix_lck, int lock, int unlock,
2363 unsigned int xid)
2364 {
2365 int rc = 0;
2366 __u64 length = cifs_flock_len(flock);
2367 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
2368 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
2369 struct TCP_Server_Info *server = tcon->ses->server;
2370 struct inode *inode = d_inode(cfile->dentry);
2371
2372 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2373 if (posix_lck) {
2374 int posix_lock_type;
2375
2376 rc = cifs_posix_lock_set(file, flock);
2377 if (rc <= FILE_LOCK_DEFERRED)
2378 return rc;
2379
2380 if (type & server->vals->shared_lock_type)
2381 posix_lock_type = CIFS_RDLCK;
2382 else
2383 posix_lock_type = CIFS_WRLCK;
2384
2385 if (unlock == 1)
2386 posix_lock_type = CIFS_UNLCK;
2387
2388 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
2389 hash_lockowner(flock->c.flc_owner),
2390 flock->fl_start, length,
2391 NULL, posix_lock_type, wait_flag);
2392 goto out;
2393 }
2394 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2395 if (lock) {
2396 struct cifsLockInfo *lock;
2397
2398 lock = cifs_lock_init(flock->fl_start, length, type,
2399 flock->c.flc_flags);
2400 if (!lock)
2401 return -ENOMEM;
2402
2403 rc = cifs_lock_add_if(cfile, lock, wait_flag, xid);
2404 if (rc < 0) {
2405 kfree(lock);
2406 return rc;
2407 }
2408 if (!rc)
2409 goto out;
2410
2411 /*
2412 * Windows 7 server can delay breaking lease from read to None
2413 * if we set a byte-range lock on a file - break it explicitly
2414 * before sending the lock to the server to be sure the next
2415 * read won't conflict with non-overlapted locks due to
2416 * pagereading.
2417 */
2418 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
2419 CIFS_CACHE_READ(CIFS_I(inode))) {
2420 cifs_zap_mapping(inode);
2421 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
2422 inode);
2423 cifs_reset_oplock(CIFS_I(inode));
2424 }
2425
2426 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
2427 type, 1, 0, wait_flag);
2428 if (rc) {
2429 kfree(lock);
2430 return rc;
2431 }
2432
2433 cifs_lock_add(cfile, lock);
2434 } else if (unlock)
2435 rc = server->ops->mand_unlock_range(cfile, flock, xid);
2436
2437 out:
2438 if ((flock->c.flc_flags & FL_POSIX) || (flock->c.flc_flags & FL_FLOCK)) {
2439 /*
2440 * If this is a request to remove all locks because we
2441 * are closing the file, it doesn't matter if the
2442 * unlocking failed as both cifs.ko and the SMB server
2443 * remove the lock on file close
2444 */
2445 if (rc) {
2446 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
2447 if (!(flock->c.flc_flags & FL_CLOSE))
2448 return rc;
2449 }
2450 rc = locks_lock_file_wait(file, flock);
2451 }
2452 return rc;
2453 }
2454
cifs_flock(struct file * file,int cmd,struct file_lock * fl)2455 int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
2456 {
2457 int rc, xid;
2458 int lock = 0, unlock = 0;
2459 bool wait_flag = false;
2460 bool posix_lck = false;
2461 struct cifs_sb_info *cifs_sb;
2462 struct cifs_tcon *tcon;
2463 struct cifsFileInfo *cfile;
2464 __u32 type;
2465
2466 xid = get_xid();
2467
2468 if (!(fl->c.flc_flags & FL_FLOCK)) {
2469 rc = -ENOLCK;
2470 free_xid(xid);
2471 return rc;
2472 }
2473
2474 cfile = (struct cifsFileInfo *)file->private_data;
2475 tcon = tlink_tcon(cfile->tlink);
2476
2477 cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
2478 tcon->ses->server);
2479 cifs_sb = CIFS_SB(file);
2480
2481 if (cap_unix(tcon->ses) &&
2482 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
2483 ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOPOSIXBRL) == 0))
2484 posix_lck = true;
2485
2486 if (!lock && !unlock) {
2487 /*
2488 * if no lock or unlock then nothing to do since we do not
2489 * know what it is
2490 */
2491 rc = -EOPNOTSUPP;
2492 free_xid(xid);
2493 return rc;
2494 }
2495
2496 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
2497 xid);
2498 free_xid(xid);
2499 return rc;
2500
2501
2502 }
2503
cifs_lock(struct file * file,int cmd,struct file_lock * flock)2504 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
2505 {
2506 struct cifs_sb_info *cifs_sb = CIFS_SB(file);
2507 struct cifsFileInfo *cfile;
2508 int lock = 0, unlock = 0;
2509 bool wait_flag = false;
2510 bool posix_lck = false;
2511 struct cifs_tcon *tcon;
2512 __u32 type;
2513 int rc, xid;
2514
2515 rc = -EACCES;
2516 xid = get_xid();
2517
2518 cifs_dbg(FYI, "%s: %pD2 cmd=0x%x type=0x%x flags=0x%x r=%lld:%lld\n", __func__, file, cmd,
2519 flock->c.flc_flags, flock->c.flc_type,
2520 (long long)flock->fl_start,
2521 (long long)flock->fl_end);
2522
2523 cfile = (struct cifsFileInfo *)file->private_data;
2524 tcon = tlink_tcon(cfile->tlink);
2525
2526 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
2527 tcon->ses->server);
2528 set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS_I(d_inode(cfile->dentry))->flags);
2529
2530 if (cap_unix(tcon->ses) &&
2531 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
2532 ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOPOSIXBRL) == 0))
2533 posix_lck = true;
2534 /*
2535 * BB add code here to normalize offset and length to account for
2536 * negative length which we can not accept over the wire.
2537 */
2538 if (IS_GETLK(cmd)) {
2539 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
2540 free_xid(xid);
2541 return rc;
2542 }
2543
2544 if (!lock && !unlock) {
2545 /*
2546 * if no lock or unlock then nothing to do since we do not
2547 * know what it is
2548 */
2549 free_xid(xid);
2550 return -EOPNOTSUPP;
2551 }
2552
2553 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
2554 xid);
2555 free_xid(xid);
2556 return rc;
2557 }
2558
cifs_update_i_blocks_for_write(struct inode * inode,loff_t start,loff_t end)2559 static void cifs_update_i_blocks_for_write(struct inode *inode, loff_t start,
2560 loff_t end)
2561 {
2562 struct cifsInodeInfo *cinode = CIFS_I(inode);
2563 u64 allocated_end = CIFS_INO_BYTES(inode->i_blocks);
2564 u64 blocks;
2565
2566 if (cinode->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)
2567 return;
2568
2569 /*
2570 * Grow the local estimate only across the currently known allocated
2571 * prefix. A write beyond that may leave a hole.
2572 */
2573 if ((u64)start > allocated_end)
2574 return;
2575
2576 blocks = CIFS_INO_BLOCKS(end);
2577 if ((u64)inode->i_blocks < blocks)
2578 inode->i_blocks = blocks;
2579 }
2580
cifs_update_i_blocks_after_write(struct kiocb * iocb,ssize_t written)2581 static void cifs_update_i_blocks_after_write(struct kiocb *iocb,
2582 ssize_t written)
2583 {
2584 struct inode *inode = file_inode(iocb->ki_filp);
2585 loff_t end = iocb->ki_pos;
2586
2587 if (written <= 0)
2588 return;
2589
2590 spin_lock(&inode->i_lock);
2591 cifs_update_i_blocks_for_write(inode, end - written, end);
2592 spin_unlock(&inode->i_lock);
2593 }
2594
cifs_write_subrequest_terminated(struct cifs_io_subrequest * wdata,ssize_t result)2595 void cifs_write_subrequest_terminated(struct cifs_io_subrequest *wdata, ssize_t result)
2596 {
2597 struct netfs_io_request *wreq = wdata->rreq;
2598 struct inode *inode = wreq->inode;
2599 struct netfs_inode *ictx = netfs_inode(inode);
2600 loff_t wrend;
2601
2602 if (result > 0) {
2603 spin_lock(&inode->i_lock);
2604
2605 wrend = wdata->subreq.start + wdata->subreq.transferred + result;
2606
2607 if (wrend > ictx->_zero_point &&
2608 (wdata->rreq->origin == NETFS_UNBUFFERED_WRITE ||
2609 wdata->rreq->origin == NETFS_DIO_WRITE))
2610 netfs_write_zero_point(inode, wrend);
2611 if (wrend > ictx->_remote_i_size)
2612 netfs_resize_file(ictx, wrend, true);
2613 cifs_update_i_blocks_for_write(inode, wdata->subreq.start,
2614 wrend);
2615
2616 spin_unlock(&inode->i_lock);
2617 }
2618
2619 netfs_write_subrequest_terminated(&wdata->subreq, result);
2620 }
2621
open_flags_match(struct cifsInodeInfo * cinode,unsigned int oflags,unsigned int cflags)2622 static bool open_flags_match(struct cifsInodeInfo *cinode,
2623 unsigned int oflags, unsigned int cflags)
2624 {
2625 struct inode *inode = &cinode->netfs.inode;
2626 int crw = 0, orw = 0;
2627
2628 oflags &= ~(O_CREAT | O_EXCL | O_TRUNC);
2629 cflags &= ~(O_CREAT | O_EXCL | O_TRUNC);
2630
2631 if (cifs_fscache_enabled(inode)) {
2632 if (OPEN_FMODE(cflags) & FMODE_WRITE)
2633 crw = 1;
2634 if (OPEN_FMODE(oflags) & FMODE_WRITE)
2635 orw = 1;
2636 }
2637 if (cifs_convert_flags(oflags, orw) != cifs_convert_flags(cflags, crw))
2638 return false;
2639
2640 return (oflags & (O_SYNC | O_DIRECT)) == (cflags & (O_SYNC | O_DIRECT));
2641 }
2642
__find_readable_file(struct cifsInodeInfo * cifs_inode,unsigned int find_flags,unsigned int open_flags)2643 struct cifsFileInfo *__find_readable_file(struct cifsInodeInfo *cifs_inode,
2644 unsigned int find_flags,
2645 unsigned int open_flags)
2646 {
2647 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode);
2648 bool fsuid_only = find_flags & FIND_FSUID_ONLY;
2649 struct cifsFileInfo *open_file = NULL;
2650
2651 /* only filter by fsuid on multiuser mounts */
2652 if (!(cifs_sb_flags(cifs_sb) & CIFS_MOUNT_MULTIUSER))
2653 fsuid_only = false;
2654
2655 spin_lock(&cifs_inode->open_file_lock);
2656 /* we could simply get the first_list_entry since write-only entries
2657 are always at the end of the list but since the first entry might
2658 have a close pending, we go through the whole list */
2659 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2660 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2661 continue;
2662 if ((find_flags & FIND_NO_PENDING_DELETE) &&
2663 open_file->status_file_deleted)
2664 continue;
2665 if ((find_flags & FIND_OPEN_FLAGS) &&
2666 !open_flags_match(cifs_inode, open_flags,
2667 open_file->f_flags))
2668 continue;
2669 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
2670 if ((!open_file->invalidHandle)) {
2671 /* found a good file */
2672 /* lock it so it will not be closed on us */
2673 cifsFileInfo_get(open_file);
2674 spin_unlock(&cifs_inode->open_file_lock);
2675 return open_file;
2676 } /* else might as well continue, and look for
2677 another, or simply have the caller reopen it
2678 again rather than trying to fix this handle */
2679 } else /* write only file */
2680 break; /* write only files are last so must be done */
2681 }
2682 spin_unlock(&cifs_inode->open_file_lock);
2683 return NULL;
2684 }
2685
2686 /* Return -EBADF if no handle is found and general rc otherwise */
__cifs_get_writable_file(struct cifsInodeInfo * cifs_inode,unsigned int find_flags,unsigned int open_flags,struct cifsFileInfo ** ret_file)2687 int __cifs_get_writable_file(struct cifsInodeInfo *cifs_inode,
2688 unsigned int find_flags, unsigned int open_flags,
2689 struct cifsFileInfo **ret_file)
2690 {
2691 struct cifsFileInfo *open_file, *inv_file = NULL;
2692 bool fsuid_only, with_delete;
2693 struct cifs_sb_info *cifs_sb;
2694 bool any_available = false;
2695 unsigned int refind = 0;
2696 *ret_file = NULL;
2697 int rc = -EBADF;
2698
2699 /*
2700 * Having a null inode here (because mapping->host was set to zero by
2701 * the VFS or MM) should not happen but we had reports of on oops (due
2702 * to it being zero) during stress testcases so we need to check for it
2703 */
2704
2705 if (cifs_inode == NULL) {
2706 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
2707 dump_stack();
2708 return rc;
2709 }
2710
2711 if (test_bit(CIFS_INO_TMPFILE, &cifs_inode->flags))
2712 find_flags = FIND_ANY;
2713
2714 cifs_sb = CIFS_SB(cifs_inode);
2715
2716 with_delete = find_flags & FIND_WITH_DELETE;
2717 fsuid_only = find_flags & FIND_FSUID_ONLY;
2718 /* only filter by fsuid on multiuser mounts */
2719 if (!(cifs_sb_flags(cifs_sb) & CIFS_MOUNT_MULTIUSER))
2720 fsuid_only = false;
2721
2722 spin_lock(&cifs_inode->open_file_lock);
2723 refind_writable:
2724 if (refind > MAX_REOPEN_ATT) {
2725 spin_unlock(&cifs_inode->open_file_lock);
2726 return rc;
2727 }
2728 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2729 if (!any_available && open_file->pid != current->tgid)
2730 continue;
2731 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2732 continue;
2733 if (with_delete && !(open_file->fid.access & DELETE))
2734 continue;
2735 if ((find_flags & FIND_NO_PENDING_DELETE) &&
2736 open_file->status_file_deleted)
2737 continue;
2738 if ((find_flags & FIND_OPEN_FLAGS) &&
2739 !open_flags_match(cifs_inode, open_flags,
2740 open_file->f_flags))
2741 continue;
2742 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
2743 if (!open_file->invalidHandle) {
2744 /* found a good writable file */
2745 cifsFileInfo_get(open_file);
2746 spin_unlock(&cifs_inode->open_file_lock);
2747 *ret_file = open_file;
2748 return 0;
2749 } else {
2750 if (!inv_file)
2751 inv_file = open_file;
2752 }
2753 }
2754 }
2755 /* couldn't find usable FH with same pid, try any available */
2756 if (!any_available) {
2757 any_available = true;
2758 goto refind_writable;
2759 }
2760
2761 if (inv_file) {
2762 any_available = false;
2763 cifsFileInfo_get(inv_file);
2764 }
2765
2766 spin_unlock(&cifs_inode->open_file_lock);
2767
2768 if (inv_file) {
2769 rc = cifs_reopen_file(inv_file, false);
2770 if (!rc) {
2771 *ret_file = inv_file;
2772 return 0;
2773 }
2774
2775 spin_lock(&cifs_inode->open_file_lock);
2776 list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
2777 spin_unlock(&cifs_inode->open_file_lock);
2778 cifsFileInfo_put(inv_file);
2779 ++refind;
2780 inv_file = NULL;
2781 spin_lock(&cifs_inode->open_file_lock);
2782 goto refind_writable;
2783 }
2784
2785 return rc;
2786 }
2787
2788 struct cifsFileInfo *
find_writable_file(struct cifsInodeInfo * cifs_inode,int flags)2789 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
2790 {
2791 struct cifsFileInfo *cfile;
2792 int rc;
2793
2794 rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
2795 if (rc)
2796 cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
2797
2798 return cfile;
2799 }
2800
cifs_get_writable_path(struct cifs_tcon * tcon,const char * name,struct inode * inode,int flags,struct cifsFileInfo ** ret_file)2801 int cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
2802 struct inode *inode, int flags,
2803 struct cifsFileInfo **ret_file)
2804 {
2805 struct cifsFileInfo *cfile;
2806 void *page;
2807
2808 *ret_file = NULL;
2809
2810 if (inode)
2811 return cifs_get_writable_file(CIFS_I(inode), flags, ret_file);
2812
2813 page = alloc_dentry_path();
2814 spin_lock(&tcon->open_file_lock);
2815 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2816 struct cifsInodeInfo *cinode;
2817 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2818 if (IS_ERR(full_path)) {
2819 spin_unlock(&tcon->open_file_lock);
2820 free_dentry_path(page);
2821 return PTR_ERR(full_path);
2822 }
2823 if (strcmp(full_path, name))
2824 continue;
2825
2826 cinode = CIFS_I(d_inode(cfile->dentry));
2827 spin_unlock(&tcon->open_file_lock);
2828 free_dentry_path(page);
2829 return cifs_get_writable_file(cinode, flags, ret_file);
2830 }
2831
2832 spin_unlock(&tcon->open_file_lock);
2833 free_dentry_path(page);
2834 return -ENOENT;
2835 }
2836
2837 int
cifs_get_readable_path(struct cifs_tcon * tcon,const char * name,struct cifsFileInfo ** ret_file)2838 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
2839 struct cifsFileInfo **ret_file)
2840 {
2841 struct cifsFileInfo *cfile;
2842 void *page = alloc_dentry_path();
2843
2844 *ret_file = NULL;
2845
2846 spin_lock(&tcon->open_file_lock);
2847 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2848 struct cifsInodeInfo *cinode;
2849 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2850 if (IS_ERR(full_path)) {
2851 spin_unlock(&tcon->open_file_lock);
2852 free_dentry_path(page);
2853 return PTR_ERR(full_path);
2854 }
2855 if (strcmp(full_path, name))
2856 continue;
2857
2858 cinode = CIFS_I(d_inode(cfile->dentry));
2859 spin_unlock(&tcon->open_file_lock);
2860 free_dentry_path(page);
2861 *ret_file = find_readable_file(cinode, FIND_ANY);
2862 return *ret_file ? 0 : -ENOENT;
2863 }
2864
2865 spin_unlock(&tcon->open_file_lock);
2866 free_dentry_path(page);
2867 return -ENOENT;
2868 }
2869
2870 /*
2871 * Flush data on a strict file.
2872 */
cifs_strict_fsync(struct file * file,loff_t start,loff_t end,int datasync)2873 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
2874 int datasync)
2875 {
2876 struct cifsFileInfo *smbfile = file->private_data;
2877 struct inode *inode = file_inode(file);
2878 unsigned int xid;
2879 int rc;
2880
2881 rc = file_write_and_wait_range(file, start, end);
2882 if (rc) {
2883 trace_cifs_fsync_err(inode->i_ino, rc);
2884 return rc;
2885 }
2886
2887 cifs_dbg(FYI, "%s: name=%pD datasync=0x%x\n", __func__, file, datasync);
2888
2889 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
2890 rc = cifs_zap_mapping(inode);
2891 cifs_dbg(FYI, "%s: invalidate mapping: rc = %d\n", __func__, rc);
2892 }
2893
2894 xid = get_xid();
2895 rc = cifs_file_flush(xid, inode, smbfile);
2896 free_xid(xid);
2897 return rc;
2898 }
2899
2900 /*
2901 * Flush data on a non-strict data.
2902 */
cifs_fsync(struct file * file,loff_t start,loff_t end,int datasync)2903 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2904 {
2905 unsigned int xid;
2906 int rc = 0;
2907 struct cifs_tcon *tcon;
2908 struct TCP_Server_Info *server;
2909 struct cifsFileInfo *smbfile = file->private_data;
2910 struct inode *inode = file_inode(file);
2911 struct cifs_sb_info *cifs_sb = CIFS_SB(file);
2912
2913 rc = file_write_and_wait_range(file, start, end);
2914 if (rc) {
2915 trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
2916 return rc;
2917 }
2918
2919 xid = get_xid();
2920
2921 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2922 file, datasync);
2923
2924 tcon = tlink_tcon(smbfile->tlink);
2925 if (!(cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOSSYNC)) {
2926 server = tcon->ses->server;
2927 if (server->ops->flush == NULL) {
2928 rc = -ENOSYS;
2929 goto fsync_exit;
2930 }
2931
2932 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
2933 smbfile = find_writable_file(CIFS_I(inode), FIND_ANY);
2934 if (smbfile) {
2935 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2936 cifsFileInfo_put(smbfile);
2937 } else
2938 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
2939 } else
2940 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2941 }
2942
2943 fsync_exit:
2944 free_xid(xid);
2945 return rc;
2946 }
2947
2948 /*
2949 * As file closes, flush all cached write data for this inode checking
2950 * for write behind errors.
2951 */
cifs_flush(struct file * file,fl_owner_t id)2952 int cifs_flush(struct file *file, fl_owner_t id)
2953 {
2954 struct inode *inode = file_inode(file);
2955 int rc = 0;
2956
2957 if (file->f_mode & FMODE_WRITE)
2958 rc = filemap_write_and_wait(inode->i_mapping);
2959
2960 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
2961 if (rc) {
2962 /* get more nuanced writeback errors */
2963 rc = filemap_check_wb_err(file->f_mapping, 0);
2964 trace_cifs_flush_err(inode->i_ino, rc);
2965 }
2966 return rc;
2967 }
2968
2969 static ssize_t
cifs_writev(struct kiocb * iocb,struct iov_iter * from)2970 cifs_writev(struct kiocb *iocb, struct iov_iter *from)
2971 {
2972 struct file *file = iocb->ki_filp;
2973 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
2974 struct inode *inode = file->f_mapping->host;
2975 struct cifsInodeInfo *cinode = CIFS_I(inode);
2976 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
2977 struct cifs_sb_info *cifs_sb = CIFS_SB(inode);
2978 ssize_t rc;
2979
2980 rc = netfs_start_io_write(inode);
2981 if (rc < 0)
2982 return rc;
2983
2984 /*
2985 * We need to hold the sem to be sure nobody modifies lock list
2986 * with a brlock that prevents writing.
2987 */
2988 down_read(&cinode->lock_sem);
2989
2990 rc = generic_write_checks(iocb, from);
2991 if (rc <= 0)
2992 goto out;
2993
2994 if ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOPOSIXBRL) &&
2995 (cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
2996 server->vals->exclusive_lock_type, 0,
2997 NULL, CIFS_WRITE_OP))) {
2998 rc = -EACCES;
2999 goto out;
3000 }
3001
3002 rc = netfs_buffered_write_iter_locked(iocb, from, NULL);
3003 cifs_update_i_blocks_after_write(iocb, rc);
3004
3005 out:
3006 up_read(&cinode->lock_sem);
3007 netfs_end_io_write(inode);
3008 if (rc > 0)
3009 rc = generic_write_sync(iocb, rc);
3010 return rc;
3011 }
3012
3013 ssize_t
cifs_strict_writev(struct kiocb * iocb,struct iov_iter * from)3014 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
3015 {
3016 struct inode *inode = file_inode(iocb->ki_filp);
3017 struct cifsInodeInfo *cinode = CIFS_I(inode);
3018 struct cifs_sb_info *cifs_sb = CIFS_SB(inode);
3019 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3020 iocb->ki_filp->private_data;
3021 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3022 ssize_t written;
3023
3024 written = cifs_get_writer(cinode);
3025 if (written)
3026 return written;
3027
3028 if (CIFS_CACHE_WRITE(cinode)) {
3029 if (cap_unix(tcon->ses) &&
3030 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
3031 ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3032 written = netfs_file_write_iter(iocb, from);
3033 cifs_update_i_blocks_after_write(iocb, written);
3034 goto out;
3035 }
3036 written = cifs_writev(iocb, from);
3037 goto out;
3038 }
3039 /*
3040 * For non-oplocked files in strict cache mode we need to write the data
3041 * to the server exactly from the pos to pos+len-1 rather than flush all
3042 * affected pages because it may cause a error with mandatory locks on
3043 * these pages but not on the region from pos to ppos+len-1.
3044 */
3045 written = netfs_file_write_iter(iocb, from);
3046 cifs_update_i_blocks_after_write(iocb, written);
3047 if (CIFS_CACHE_READ(cinode)) {
3048 /*
3049 * We have read level caching and we have just sent a write
3050 * request to the server thus making data in the cache stale.
3051 * Zap the cache and set oplock/lease level to NONE to avoid
3052 * reading stale data from the cache. All subsequent read
3053 * operations will read new data from the server.
3054 */
3055 cifs_zap_mapping(inode);
3056 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
3057 inode);
3058 cifs_reset_oplock(cinode);
3059 }
3060 out:
3061 cifs_put_writer(cinode);
3062 return written;
3063 }
3064
cifs_direct_write_iter(struct kiocb * iocb,struct iov_iter * from)3065 ssize_t cifs_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
3066 {
3067 ssize_t written;
3068
3069 written = netfs_file_write_iter(iocb, from);
3070 cifs_update_i_blocks_after_write(iocb, written);
3071 return written;
3072 }
3073
cifs_loose_read_iter(struct kiocb * iocb,struct iov_iter * iter)3074 ssize_t cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
3075 {
3076 ssize_t rc;
3077 struct inode *inode = file_inode(iocb->ki_filp);
3078
3079 if (iocb->ki_flags & IOCB_DIRECT)
3080 return netfs_unbuffered_read_iter(iocb, iter);
3081
3082 rc = cifs_revalidate_mapping(inode);
3083 if (rc)
3084 return rc;
3085
3086 return netfs_file_read_iter(iocb, iter);
3087 }
3088
cifs_file_write_iter(struct kiocb * iocb,struct iov_iter * from)3089 ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
3090 {
3091 struct inode *inode = file_inode(iocb->ki_filp);
3092 struct cifsInodeInfo *cinode = CIFS_I(inode);
3093 ssize_t written;
3094 int rc;
3095
3096 if (iocb->ki_filp->f_flags & O_DIRECT) {
3097 written = netfs_unbuffered_write_iter(iocb, from);
3098 cifs_update_i_blocks_after_write(iocb, written);
3099 if (written > 0 && CIFS_CACHE_READ(cinode)) {
3100 cifs_zap_mapping(inode);
3101 cifs_dbg(FYI,
3102 "Set no oplock for inode=%p after a write operation\n",
3103 inode);
3104 cifs_reset_oplock(cinode);
3105 }
3106 return written;
3107 }
3108
3109 written = cifs_get_writer(cinode);
3110 if (written)
3111 return written;
3112
3113 written = netfs_file_write_iter(iocb, from);
3114 cifs_update_i_blocks_after_write(iocb, written);
3115
3116 if (!CIFS_CACHE_WRITE(CIFS_I(inode))) {
3117 rc = filemap_fdatawrite(inode->i_mapping);
3118 if (rc)
3119 cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
3120 rc, inode);
3121 }
3122
3123 cifs_put_writer(cinode);
3124 return written;
3125 }
3126
3127 ssize_t
cifs_strict_readv(struct kiocb * iocb,struct iov_iter * to)3128 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
3129 {
3130 struct inode *inode = file_inode(iocb->ki_filp);
3131 struct cifsInodeInfo *cinode = CIFS_I(inode);
3132 struct cifs_sb_info *cifs_sb = CIFS_SB(inode);
3133 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3134 iocb->ki_filp->private_data;
3135 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3136 int rc = -EACCES;
3137
3138 /*
3139 * In strict cache mode we need to read from the server all the time
3140 * if we don't have level II oplock because the server can delay mtime
3141 * change - so we can't make a decision about inode invalidating.
3142 * And we can also fail with pagereading if there are mandatory locks
3143 * on pages affected by this read but not on the region from pos to
3144 * pos+len-1.
3145 */
3146 if (!CIFS_CACHE_READ(cinode))
3147 return netfs_unbuffered_read_iter(iocb, to);
3148
3149 if ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOPOSIXBRL) == 0) {
3150 if (iocb->ki_flags & IOCB_DIRECT)
3151 return netfs_unbuffered_read_iter(iocb, to);
3152 return netfs_buffered_read_iter(iocb, to);
3153 }
3154
3155 /*
3156 * We need to hold the sem to be sure nobody modifies lock list
3157 * with a brlock that prevents reading.
3158 */
3159 if (iocb->ki_flags & IOCB_DIRECT) {
3160 rc = netfs_start_io_direct(inode);
3161 if (rc < 0)
3162 goto out;
3163 rc = -EACCES;
3164 down_read(&cinode->lock_sem);
3165 if (!cifs_find_lock_conflict(
3166 cfile, iocb->ki_pos, iov_iter_count(to),
3167 tcon->ses->server->vals->shared_lock_type,
3168 0, NULL, CIFS_READ_OP))
3169 rc = netfs_unbuffered_read_iter_locked(iocb, to);
3170 up_read(&cinode->lock_sem);
3171 netfs_end_io_direct(inode);
3172 } else {
3173 rc = netfs_start_io_read(inode);
3174 if (rc < 0)
3175 goto out;
3176 rc = -EACCES;
3177 down_read(&cinode->lock_sem);
3178 if (!cifs_find_lock_conflict(
3179 cfile, iocb->ki_pos, iov_iter_count(to),
3180 tcon->ses->server->vals->shared_lock_type,
3181 0, NULL, CIFS_READ_OP))
3182 rc = filemap_read(iocb, to, 0);
3183 up_read(&cinode->lock_sem);
3184 netfs_end_io_read(inode);
3185 }
3186 out:
3187 return rc;
3188 }
3189
cifs_page_mkwrite(struct vm_fault * vmf)3190 static vm_fault_t cifs_page_mkwrite(struct vm_fault *vmf)
3191 {
3192 return netfs_page_mkwrite(vmf, NULL);
3193 }
3194
3195 static const struct vm_operations_struct cifs_file_vm_ops = {
3196 .fault = filemap_fault,
3197 .map_pages = filemap_map_pages,
3198 .page_mkwrite = cifs_page_mkwrite,
3199 };
3200
cifs_file_strict_mmap_prepare(struct vm_area_desc * desc)3201 int cifs_file_strict_mmap_prepare(struct vm_area_desc *desc)
3202 {
3203 int xid, rc = 0;
3204 struct inode *inode = file_inode(desc->file);
3205
3206 xid = get_xid();
3207
3208 if (!CIFS_CACHE_READ(CIFS_I(inode)))
3209 rc = cifs_zap_mapping(inode);
3210 if (!rc)
3211 rc = generic_file_mmap_prepare(desc);
3212 if (!rc)
3213 desc->vm_ops = &cifs_file_vm_ops;
3214
3215 free_xid(xid);
3216 return rc;
3217 }
3218
cifs_file_mmap_prepare(struct vm_area_desc * desc)3219 int cifs_file_mmap_prepare(struct vm_area_desc *desc)
3220 {
3221 int rc, xid;
3222
3223 xid = get_xid();
3224
3225 rc = cifs_revalidate_file(desc->file);
3226 if (rc)
3227 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
3228 rc);
3229 if (!rc)
3230 rc = generic_file_mmap_prepare(desc);
3231 if (!rc)
3232 desc->vm_ops = &cifs_file_vm_ops;
3233
3234 free_xid(xid);
3235 return rc;
3236 }
3237
is_inode_writable(struct cifsInodeInfo * cifs_inode)3238 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
3239 {
3240 struct cifsFileInfo *open_file;
3241
3242 spin_lock(&cifs_inode->open_file_lock);
3243 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
3244 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
3245 spin_unlock(&cifs_inode->open_file_lock);
3246 return 1;
3247 }
3248 }
3249 spin_unlock(&cifs_inode->open_file_lock);
3250 return 0;
3251 }
3252
3253 /* We do not want to update the file size from server for inodes
3254 open for write - to avoid races with writepage extending
3255 the file - in the future we could consider allowing
3256 refreshing the inode only on increases in the file size
3257 but this is tricky to do without racing with writebehind
3258 page caching in the current Linux kernel design */
is_size_safe_to_change(struct cifsInodeInfo * cifsInode,__u64 end_of_file,bool from_readdir)3259 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file,
3260 bool from_readdir)
3261 {
3262 struct cifs_sb_info *cifs_sb;
3263 unsigned long tlw;
3264
3265 if (!cifsInode)
3266 return true;
3267
3268 cifs_sb = CIFS_SB(cifsInode);
3269
3270 if (is_inode_writable(cifsInode) ||
3271 ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0 && from_readdir)) {
3272 /* This inode is open for write at least once */
3273
3274 /*
3275 * Readdir data is unreliable when we have writable handles or
3276 * an exclusive lease -- never allow it to change i_size, even
3277 * on direct-IO mounts where the server's directory metadata
3278 * can still lag behind the actual file state.
3279 */
3280 if (from_readdir)
3281 return false;
3282
3283 if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_DIRECT_IO) {
3284 /* since no page cache to corrupt on directio
3285 we can change size safely */
3286 return true;
3287 }
3288
3289 if (i_size_read(&cifsInode->netfs.inode) < end_of_file)
3290 return true;
3291
3292 return false;
3293 }
3294
3295 /*
3296 * No writable handles open. Check whether we are within the attribute
3297 * cache validity window of a recent local modification.
3298 *
3299 * For the close() path: _cifsFileInfo_put() stamps time_last_write
3300 * (via smp_store_release()) before releasing open_file_lock. That
3301 * spin_unlock() is a store-release that pairs with the spin_lock()
3302 * (load-acquire) in is_inode_writable() above, so if
3303 * is_inode_writable() returned false the smp_load_acquire() below is
3304 * guaranteed to observe any time_last_write update from a concurrent
3305 * close(), covering all close paths including background I/O.
3306 *
3307 * For the setattr/truncate paths: those callers use smp_store_release()
3308 * directly; the smp_load_acquire() below pairs with that store. There
3309 * is no shared lock between setattr and readdir, so this relies on
3310 * acquire-release semantics alone. The store propagation latency on
3311 * weakly-ordered architectures (nanoseconds) is negligible relative to
3312 * the acregmax window (seconds) and the readdir RPC round-trip
3313 * (milliseconds), making this a sound design choice in practice.
3314 *
3315 * time_last_write == 0 means the inode has never been written locally;
3316 * skip the window check to avoid false positives near boot time when
3317 * jiffies is still close to INITIAL_JIFFIES on 32-bit systems.
3318 */
3319 if (from_readdir) {
3320 /* Pairs with smp_store_release() in _cifsFileInfo_put() and setattr. */
3321 tlw = smp_load_acquire(&cifsInode->time_last_write);
3322 if (tlw && time_before(jiffies, tlw + cifs_sb->ctx->acregmax))
3323 return false;
3324 }
3325
3326 return true;
3327 }
3328
cifs_oplock_break(struct work_struct * work)3329 void cifs_oplock_break(struct work_struct *work)
3330 {
3331 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
3332 oplock_break);
3333 struct inode *inode = d_inode(cfile->dentry);
3334 struct super_block *sb = inode->i_sb;
3335 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
3336 struct cifsInodeInfo *cinode = CIFS_I(inode);
3337 bool cache_read, cache_write, cache_handle;
3338 struct cifs_tcon *tcon;
3339 struct TCP_Server_Info *server;
3340 struct tcon_link *tlink;
3341 unsigned int oplock;
3342 int rc = 0;
3343 bool purge_cache = false, oplock_break_cancelled;
3344 __u64 persistent_fid, volatile_fid;
3345 __u16 net_fid;
3346
3347 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
3348 TASK_UNINTERRUPTIBLE);
3349
3350 tlink = cifs_sb_tlink(cifs_sb);
3351 if (IS_ERR(tlink))
3352 goto out;
3353 tcon = tlink_tcon(tlink);
3354 server = tcon->ses->server;
3355
3356 scoped_guard(spinlock, &cinode->open_file_lock) {
3357 unsigned int sbflags = cifs_sb_flags(cifs_sb);
3358
3359 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
3360 cfile->oplock_epoch, &purge_cache);
3361 oplock = READ_ONCE(cinode->oplock);
3362 cache_read = (oplock & CIFS_CACHE_READ_FLG) ||
3363 (sbflags & CIFS_MOUNT_RO_CACHE);
3364 cache_write = (oplock & CIFS_CACHE_WRITE_FLG) ||
3365 (sbflags & CIFS_MOUNT_RW_CACHE);
3366 cache_handle = oplock & CIFS_CACHE_HANDLE_FLG;
3367 }
3368
3369 if (!cache_write && cache_read && cifs_has_mand_locks(cinode)) {
3370 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
3371 inode);
3372 cifs_reset_oplock(cinode);
3373 oplock = 0;
3374 cache_read = cache_write = cache_handle = false;
3375 }
3376
3377 if (S_ISREG(inode->i_mode)) {
3378 if (cache_read)
3379 break_lease(inode, O_RDONLY);
3380 else
3381 break_lease(inode, O_WRONLY);
3382 rc = filemap_fdatawrite(inode->i_mapping);
3383 if (!cache_read || purge_cache) {
3384 rc = filemap_fdatawait(inode->i_mapping);
3385 mapping_set_error(inode->i_mapping, rc);
3386 cifs_zap_mapping(inode);
3387 }
3388 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
3389 if (cache_write)
3390 goto oplock_break_ack;
3391 }
3392
3393 rc = cifs_push_locks(cfile);
3394 if (rc)
3395 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
3396
3397 oplock_break_ack:
3398 /*
3399 * When oplock break is received and there are no active
3400 * file handles but cached, then schedule deferred close immediately.
3401 * So, new open will not use cached handle.
3402 */
3403
3404 if (!cache_handle && !list_empty(&cinode->deferred_closes))
3405 cifs_close_deferred_file(cinode);
3406
3407 persistent_fid = cfile->fid.persistent_fid;
3408 volatile_fid = cfile->fid.volatile_fid;
3409 net_fid = cfile->fid.netfid;
3410 oplock_break_cancelled = cfile->oplock_break_cancelled;
3411
3412 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
3413 /*
3414 * MS-SMB2 3.2.5.19.1 and 3.2.5.19.2 (and MS-CIFS 3.2.5.42) do not require
3415 * an acknowledgment to be sent when the file has already been closed.
3416 */
3417 spin_lock(&cinode->open_file_lock);
3418 /* check list empty since can race with kill_sb calling tree disconnect */
3419 if (!oplock_break_cancelled && !list_empty(&cinode->openFileList)) {
3420 spin_unlock(&cinode->open_file_lock);
3421 rc = server->ops->oplock_response(tcon, persistent_fid,
3422 volatile_fid, net_fid,
3423 cinode, oplock);
3424 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
3425 } else
3426 spin_unlock(&cinode->open_file_lock);
3427
3428 cifs_put_tlink(tlink);
3429 out:
3430 cifs_done_oplock_break(cinode);
3431 }
3432
3433 #ifdef CONFIG_SWAP
cifs_swap_submit_write(struct swap_io_ctx * ctx)3434 static void cifs_swap_submit_write(struct swap_io_ctx *ctx)
3435 {
3436 struct swap_iocb *sio = ctx->sio;
3437 struct iov_iter iter;
3438 int ret;
3439
3440 swap_fs_prepare_rw(ctx, WRITE, &iter);
3441 ret = netfs_unbuffered_write_iter_locked(&sio->iocb, &iter, NULL);
3442 if (ret != -EIOCBQUEUED)
3443 sio->iocb.ki_complete(&sio->iocb, ret);
3444 }
3445
cifs_swap_submit_read(struct swap_io_ctx * ctx)3446 static void cifs_swap_submit_read(struct swap_io_ctx *ctx)
3447 {
3448 struct swap_iocb *sio = ctx->sio;
3449 struct iov_iter iter;
3450 int ret;
3451
3452 swap_fs_prepare_rw(ctx, READ, &iter);
3453 ret = netfs_unbuffered_read_iter_locked(&sio->iocb, &iter);
3454 if (ret != -EIOCBQUEUED)
3455 sio->iocb.ki_complete(&sio->iocb, ret);
3456 }
3457
3458 static const struct swap_ops cifs_swap_ops = {
3459 .flags = SWAP_OPS_F_REQUIRE_NOFS,
3460 .submit_write = cifs_swap_submit_write,
3461 .submit_read = cifs_swap_submit_read,
3462 .can_merge = swap_fs_can_merge,
3463 };
3464
cifs_swap_activate(struct swap_info_struct * sis,struct file * swap_file,sector_t * span)3465 static int cifs_swap_activate(struct swap_info_struct *sis,
3466 struct file *swap_file, sector_t *span)
3467 {
3468 struct cifsFileInfo *cfile = swap_file->private_data;
3469 struct inode *inode = swap_file->f_mapping->host;
3470 unsigned long blocks;
3471 long long isize;
3472
3473 cifs_dbg(FYI, "swap activate\n");
3474
3475 if (swap_file->f_mapping->a_ops != &cifs_addr_ops)
3476 /* Cannot support swap */
3477 return -EINVAL;
3478
3479 spin_lock(&inode->i_lock);
3480 blocks = inode->i_blocks;
3481 isize = inode->i_size;
3482 spin_unlock(&inode->i_lock);
3483 if (blocks*512 < isize) {
3484 pr_warn("swap activate: swapfile has holes\n");
3485 return -EINVAL;
3486 }
3487 *span = sis->pages;
3488
3489 pr_warn_once("Swap support over SMB3 is experimental\n");
3490
3491 /*
3492 * TODO: consider adding ACL (or documenting how) to prevent other
3493 * users (on this or other systems) from reading it
3494 */
3495
3496
3497 /* TODO: add sk_set_memalloc(inet) or similar */
3498
3499 if (cfile)
3500 cfile->swapfile = true;
3501 /*
3502 * TODO: Since file already open, we can't open with DENY_ALL here
3503 * but we could add call to grab a byte range lock to prevent others
3504 * from reading or writing the file
3505 */
3506 return swap_fs_activate(sis, &cifs_swap_ops);
3507 }
3508
cifs_swap_deactivate(struct file * file)3509 static void cifs_swap_deactivate(struct file *file)
3510 {
3511 struct cifsFileInfo *cfile = file->private_data;
3512
3513 cifs_dbg(FYI, "swap deactivate\n");
3514
3515 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */
3516
3517 if (cfile)
3518 cfile->swapfile = false;
3519
3520 /* do we need to unpin (or unlock) the file */
3521 }
3522 #else
3523 #define cifs_swap_activate NULL
3524 #define cifs_swap_deactivate NULL
3525 #endif /* CONFIG_SWAP */
3526
3527 const struct address_space_operations cifs_addr_ops = {
3528 .read_folio = netfs_read_folio,
3529 .readahead = netfs_readahead,
3530 .writepages = netfs_writepages,
3531 .dirty_folio = netfs_dirty_folio,
3532 .release_folio = netfs_release_folio,
3533 .direct_IO = noop_direct_IO,
3534 .invalidate_folio = netfs_invalidate_folio,
3535 .migrate_folio = filemap_migrate_folio,
3536 /*
3537 * TODO: investigate and if useful we could add an is_dirty_writeback
3538 * helper if needed
3539 */
3540 .swap_activate = cifs_swap_activate,
3541 .swap_deactivate = cifs_swap_deactivate,
3542 };
3543
3544 /*
3545 * cifs_readahead requires the server to support a buffer large enough to
3546 * contain the header plus one complete page of data. Otherwise, we need
3547 * to leave cifs_readahead out of the address space operations.
3548 */
3549 const struct address_space_operations cifs_addr_ops_smallbuf = {
3550 .read_folio = netfs_read_folio,
3551 .writepages = netfs_writepages,
3552 .dirty_folio = netfs_dirty_folio,
3553 .release_folio = netfs_release_folio,
3554 .invalidate_folio = netfs_invalidate_folio,
3555 .migrate_folio = filemap_migrate_folio,
3556 };
3557