forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
kernel/fs/nfs/file.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/fs/nfs/file.c
34 *
....@@ -89,8 +90,8 @@
8990
9091 /**
9192 * nfs_revalidate_size - Revalidate the file size
92
- * @inode - pointer to inode struct
93
- * @file - pointer to struct file
93
+ * @inode: pointer to inode struct
94
+ * @filp: pointer to struct file
9495 *
9596 * Revalidates the file length. This is basically a wrapper around
9697 * nfs_revalidate_inode() that takes into account the fact that we may
....@@ -139,6 +140,7 @@
139140 nfs_file_flush(struct file *file, fl_owner_t id)
140141 {
141142 struct inode *inode = file_inode(file);
143
+ errseq_t since;
142144
143145 dprintk("NFS: flush(%pD2)\n", file);
144146
....@@ -147,7 +149,9 @@
147149 return 0;
148150
149151 /* Flush writes to the server and return any errors */
150
- return vfs_fsync(file, 0);
152
+ since = filemap_sample_wb_err(file->f_mapping);
153
+ nfs_wb_all(inode);
154
+ return filemap_check_wb_err(file->f_mapping, since);
151155 }
152156
153157 ssize_t
....@@ -157,7 +161,7 @@
157161 ssize_t result;
158162
159163 if (iocb->ki_flags & IOCB_DIRECT)
160
- return nfs_file_direct_read(iocb, to);
164
+ return nfs_file_direct_read(iocb, to, false);
161165
162166 dprintk("NFS: read(%pD2, %zu@%lu)\n",
163167 iocb->ki_filp,
....@@ -199,64 +203,44 @@
199203 * Flush any dirty pages for this process, and check for write errors.
200204 * The return status from this call provides a reliable indication of
201205 * whether any write errors occurred for this process.
202
- *
203
- * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
204
- * disk, but it retrieves and clears ctx->error after synching, despite
205
- * the two being set at the same time in nfs_context_set_write_error().
206
- * This is because the former is used to notify the _next_ call to
207
- * nfs_file_write() that a write error occurred, and hence cause it to
208
- * fall back to doing a synchronous write.
209206 */
210207 static int
211208 nfs_file_fsync_commit(struct file *file, int datasync)
212209 {
213
- struct nfs_open_context *ctx = nfs_file_open_context(file);
214210 struct inode *inode = file_inode(file);
215
- int do_resend, status;
216
- int ret = 0;
211
+ int ret, ret2;
217212
218213 dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync);
219214
220215 nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
221
- do_resend = test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
222
- status = nfs_commit_inode(inode, FLUSH_SYNC);
223
- if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags)) {
224
- ret = xchg(&ctx->error, 0);
225
- if (ret)
226
- goto out;
227
- }
228
- if (status < 0) {
229
- ret = status;
230
- goto out;
231
- }
232
- do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
233
- if (do_resend)
234
- ret = -EAGAIN;
235
-out:
216
+ ret = nfs_commit_inode(inode, FLUSH_SYNC);
217
+ ret2 = file_check_and_advance_wb_err(file);
218
+ if (ret2 < 0)
219
+ return ret2;
236220 return ret;
237221 }
238222
239223 int
240224 nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
241225 {
242
- int ret;
226
+ struct nfs_open_context *ctx = nfs_file_open_context(file);
243227 struct inode *inode = file_inode(file);
228
+ int ret;
244229
245230 trace_nfs_fsync_enter(inode);
246231
247
- do {
248
- struct nfs_open_context *ctx = nfs_file_open_context(file);
249
- ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
250
- if (test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags)) {
251
- int ret2 = xchg(&ctx->error, 0);
252
- if (ret2)
253
- ret = ret2;
254
- }
232
+ for (;;) {
233
+ ret = file_write_and_wait_range(file, start, end);
255234 if (ret != 0)
256235 break;
257236 ret = nfs_file_fsync_commit(file, datasync);
258
- if (!ret)
259
- ret = pnfs_sync_inode(inode, !!datasync);
237
+ if (ret != 0)
238
+ break;
239
+ ret = pnfs_sync_inode(inode, !!datasync);
240
+ if (ret != 0)
241
+ break;
242
+ if (!test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags))
243
+ break;
260244 /*
261245 * If nfs_file_fsync_commit detected a server reboot, then
262246 * resend all dirty pages that might have been covered by
....@@ -264,7 +248,7 @@
264248 */
265249 start = 0;
266250 end = LLONG_MAX;
267
- } while (ret == -EAGAIN);
251
+ }
268252
269253 trace_nfs_fsync_exit(inode, ret);
270254 return ret;
....@@ -275,6 +259,12 @@
275259 * Decide whether a read/modify/write cycle may be more efficient
276260 * then a modify/write/read cycle when writing to a page in the
277261 * page cache.
262
+ *
263
+ * Some pNFS layout drivers can only read/write at a certain block
264
+ * granularity like all block devices and therefore we must perform
265
+ * read/modify/write whenever a page hasn't read yet and the data
266
+ * to be written there is not aligned to a block boundary and/or
267
+ * smaller than the block size.
278268 *
279269 * The modify/write/read cycle may occur if a page is read before
280270 * being completely filled by the writer. In this situation, the
....@@ -291,26 +281,32 @@
291281 * and that the new data won't completely replace the old data in
292282 * that range of the file.
293283 */
294
-static int nfs_want_read_modify_write(struct file *file, struct page *page,
295
- loff_t pos, unsigned len)
284
+static bool nfs_full_page_write(struct page *page, loff_t pos, unsigned int len)
296285 {
297286 unsigned int pglen = nfs_page_length(page);
298287 unsigned int offset = pos & (PAGE_SIZE - 1);
299288 unsigned int end = offset + len;
300289
301
- if (pnfs_ld_read_whole_page(file->f_mapping->host)) {
302
- if (!PageUptodate(page))
303
- return 1;
304
- return 0;
305
- }
290
+ return !pglen || (end >= pglen && !offset);
291
+}
306292
307
- if ((file->f_mode & FMODE_READ) && /* open for read? */
308
- !PageUptodate(page) && /* Uptodate? */
309
- !PagePrivate(page) && /* i/o request already? */
310
- pglen && /* valid bytes of file? */
311
- (end < pglen || offset)) /* replace all valid bytes? */
312
- return 1;
313
- return 0;
293
+static bool nfs_want_read_modify_write(struct file *file, struct page *page,
294
+ loff_t pos, unsigned int len)
295
+{
296
+ /*
297
+ * Up-to-date pages, those with ongoing or full-page write
298
+ * don't need read/modify/write
299
+ */
300
+ if (PageUptodate(page) || PagePrivate(page) ||
301
+ nfs_full_page_write(page, pos, len))
302
+ return false;
303
+
304
+ if (pnfs_ld_read_whole_page(file->f_mapping->host))
305
+ return true;
306
+ /* Open for reading too? */
307
+ if (file->f_mode & FMODE_READ)
308
+ return true;
309
+ return false;
314310 }
315311
316312 /*
....@@ -394,11 +390,8 @@
394390 return status;
395391 NFS_I(mapping->host)->write_io += copied;
396392
397
- if (nfs_ctx_key_to_expire(ctx, mapping->host)) {
398
- status = nfs_wb_all(mapping->host);
399
- if (status < 0)
400
- return status;
401
- }
393
+ if (nfs_ctx_key_to_expire(ctx, mapping->host))
394
+ nfs_wb_all(mapping->host);
402395
403396 return copied;
404397 }
....@@ -492,7 +485,19 @@
492485 static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
493486 sector_t *span)
494487 {
488
+ unsigned long blocks;
489
+ long long isize;
495490 struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
491
+ struct inode *inode = file->f_mapping->host;
492
+
493
+ spin_lock(&inode->i_lock);
494
+ blocks = inode->i_blocks;
495
+ isize = inode->i_size;
496
+ spin_unlock(&inode->i_lock);
497
+ if (blocks*512 < isize) {
498
+ pr_warn("swap activate: swapfile has holes\n");
499
+ return -EINVAL;
500
+ }
496501
497502 *span = sis->pages;
498503
....@@ -583,12 +588,13 @@
583588 .page_mkwrite = nfs_vm_page_mkwrite,
584589 };
585590
586
-static int nfs_need_check_write(struct file *filp, struct inode *inode)
591
+static int nfs_need_check_write(struct file *filp, struct inode *inode,
592
+ int error)
587593 {
588594 struct nfs_open_context *ctx;
589595
590596 ctx = nfs_file_open_context(filp);
591
- if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags) ||
597
+ if (nfs_error_is_fatal_on_server(error) ||
592598 nfs_ctx_key_to_expire(ctx, inode))
593599 return 1;
594600 return 0;
....@@ -600,13 +606,15 @@
600606 struct inode *inode = file_inode(file);
601607 unsigned long written = 0;
602608 ssize_t result;
609
+ errseq_t since;
610
+ int error;
603611
604612 result = nfs_key_timeout_notify(file, inode);
605613 if (result)
606614 return result;
607615
608616 if (iocb->ki_flags & IOCB_DIRECT)
609
- return nfs_file_direct_write(iocb, from);
617
+ return nfs_file_direct_write(iocb, from, false);
610618
611619 dprintk("NFS: write(%pD2, %zu@%Ld)\n",
612620 file, iov_iter_count(from), (long long) iocb->ki_pos);
....@@ -624,6 +632,7 @@
624632 if (iocb->ki_pos > i_size_read(inode))
625633 nfs_revalidate_mapping(inode, file->f_mapping);
626634
635
+ since = filemap_sample_wb_err(file->f_mapping);
627636 nfs_start_io_write(inode);
628637 result = generic_write_checks(iocb, from);
629638 if (result > 0) {
....@@ -642,8 +651,9 @@
642651 goto out;
643652
644653 /* Return error values */
645
- if (nfs_need_check_write(file, inode)) {
646
- int err = vfs_fsync(file, 0);
654
+ error = filemap_check_wb_err(file->f_mapping, since);
655
+ if (nfs_need_check_write(file, inode, error)) {
656
+ int err = nfs_wb_all(inode);
647657 if (err < 0)
648658 result = err;
649659 }
....@@ -653,7 +663,7 @@
653663
654664 out_swapfile:
655665 printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
656
- return -EBUSY;
666
+ return -ETXTBSY;
657667 }
658668 EXPORT_SYMBOL_GPL(nfs_file_write);
659669
....@@ -697,7 +707,7 @@
697707 * Flush all pending writes before doing anything
698708 * with locks..
699709 */
700
- vfs_fsync(filp, 0);
710
+ nfs_wb_all(inode);
701711
702712 l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
703713 if (!IS_ERR(l_ctx)) {