vfs_addr.c 9.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * This file contians vfs address (mmap) ops for 9P2000.
 *
 *  Copyright (C) 2005 by Eric Van Hensbergen <ericvh@gmail.com>
 *  Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
 */

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/inet.h>
#include <linux/pagemap.h>
#include <linux/idr.h>
A
Alexey Dobriyan 已提交
18
#include <linux/sched.h>
19
#include <linux/swap.h>
20
#include <linux/uio.h>
21
#include <linux/netfs.h>
22 23
#include <net/9p/9p.h>
#include <net/9p/client.h>
24 25 26

#include "v9fs.h"
#include "v9fs_vfs.h"
27
#include "cache.h"
28
#include "fid.h"
29 30

/**
31
 * v9fs_issue_read - Issue a read from 9P
32
 * @subreq: The read to make
33
 */
34
static void v9fs_issue_read(struct netfs_io_subrequest *subreq)
35
{
36
	struct netfs_io_request *rreq = subreq->rreq;
37
	struct p9_fid *fid = rreq->netfs_priv;
38
	struct iov_iter to;
39 40 41
	loff_t pos = subreq->start + subreq->transferred;
	size_t len = subreq->len   - subreq->transferred;
	int total, err;
42

43
	iov_iter_xarray(&to, READ, &rreq->mapping->i_pages, pos, len);
44

45
	total = p9_client_read(fid, pos, &to, &err);
46 47 48 49 50

	/* if we just extended the file size, any portion not in
	 * cache won't be on server and is zeroes */
	__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);

51 52
	netfs_subreq_terminated(subreq, err ?: total, false);
}
53

54
/**
55
 * v9fs_init_request - Initialise a read request
56 57 58
 * @rreq: The read request
 * @file: The file being read from
 */
59
static int v9fs_init_request(struct netfs_io_request *rreq, struct file *file)
60
{
61 62
	struct inode *inode = file_inode(file);
	struct v9fs_inode *v9inode = V9FS_I(inode);
63
	struct p9_fid *fid = file->private_data;
64

65 66 67 68 69 70 71 72 73 74 75
	BUG_ON(!fid);

	/* we might need to read from a fid that was opened write-only
	 * for read-modify-write of page cache, use the writeback fid
	 * for that */
	if (rreq->origin == NETFS_READ_FOR_WRITE &&
			(fid->mode & O_ACCMODE) == O_WRONLY) {
		fid = v9inode->writeback_fid;
		BUG_ON(!fid);
	}

76 77
	refcount_inc(&fid->count);
	rreq->netfs_priv = fid;
78
	return 0;
79
}
80

81
/**
82 83
 * v9fs_free_request - Cleanup request initialized by v9fs_init_rreq
 * @rreq: The I/O request to clean up
84
 */
85
static void v9fs_free_request(struct netfs_io_request *rreq)
86
{
87
	struct p9_fid *fid = rreq->netfs_priv;
88

89 90
	p9_client_clunk(fid);
}
91

92 93 94 95
/**
 * v9fs_begin_cache_operation - Begin a cache operation for a read
 * @rreq: The read request
 */
96
static int v9fs_begin_cache_operation(struct netfs_io_request *rreq)
97
{
98
#ifdef CONFIG_9P_FSCACHE
99 100
	struct fscache_cookie *cookie = v9fs_inode_cookie(V9FS_I(rreq->inode));

101
	return fscache_begin_read_operation(&rreq->cache_resources, cookie);
102 103 104
#else
	return -ENOBUFS;
#endif
105 106
}

D
David Howells 已提交
107
const struct netfs_request_ops v9fs_req_ops = {
108
	.init_request		= v9fs_init_request,
109
	.free_request		= v9fs_free_request,
110
	.begin_cache_operation	= v9fs_begin_cache_operation,
111
	.issue_read		= v9fs_issue_read,
112 113
};

114
/**
115 116
 * v9fs_release_folio - release the private state associated with a folio
 * @folio: The folio to be released
117
 * @gfp: The caller's allocation restrictions
118
 *
119
 * Returns true if the page can be released, false otherwise.
120 121
 */

122
static bool v9fs_release_folio(struct folio *folio, gfp_t gfp)
123
{
124
	struct inode *inode = folio_inode(folio);
D
David Howells 已提交
125 126

	if (folio_test_private(folio))
127
		return false;
128
#ifdef CONFIG_9P_FSCACHE
D
David Howells 已提交
129
	if (folio_test_fscache(folio)) {
130
		if (current_is_kswapd() || !(gfp & __GFP_FS))
131
			return false;
D
David Howells 已提交
132
		folio_wait_fscache(folio);
133 134
	}
#endif
135
	fscache_note_page_release(v9fs_inode_cookie(V9FS_I(inode)));
136
	return true;
137 138
}

139 140
static void v9fs_invalidate_folio(struct folio *folio, size_t offset,
				 size_t length)
141
{
D
David Howells 已提交
142
	folio_wait_fscache(folio);
143 144
}

145 146 147 148 149 150 151 152 153 154
static void v9fs_write_to_cache_done(void *priv, ssize_t transferred_or_error,
				     bool was_async)
{
	struct v9fs_inode *v9inode = priv;
	__le32 version;

	if (IS_ERR_VALUE(transferred_or_error) &&
	    transferred_or_error != -ENOBUFS) {
		version = cpu_to_le32(v9inode->qid.version);
		fscache_invalidate(v9fs_inode_cookie(v9inode), &version,
155
				   i_size_read(&v9inode->netfs.inode), 0);
156 157 158
	}
}

D
David Howells 已提交
159
static int v9fs_vfs_write_folio_locked(struct folio *folio)
160
{
D
David Howells 已提交
161
	struct inode *inode = folio_inode(folio);
162
	struct v9fs_inode *v9inode = V9FS_I(inode);
163
	struct fscache_cookie *cookie = v9fs_inode_cookie(v9inode);
D
David Howells 已提交
164 165
	loff_t start = folio_pos(folio);
	loff_t i_size = i_size_read(inode);
166
	struct iov_iter from;
D
David Howells 已提交
167 168 169 170 171
	size_t len = folio_size(folio);
	int err;

	if (start >= i_size)
		return 0; /* Simultaneous truncation occurred */
172

D
David Howells 已提交
173
	len = min_t(loff_t, i_size - start, len);
174

D
David Howells 已提交
175
	iov_iter_xarray(&from, WRITE, &folio_mapping(folio)->i_pages, start, len);
176

177 178
	/* We should have writeback_fid always set */
	BUG_ON(!v9inode->writeback_fid);
179

180
	folio_wait_fscache(folio);
D
David Howells 已提交
181
	folio_start_writeback(folio);
182

183
	p9_client_write(v9inode->writeback_fid, start, &from, &err);
184

185 186 187 188 189 190 191 192 193 194
	if (err == 0 &&
	    fscache_cookie_enabled(cookie) &&
	    test_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags)) {
		folio_start_fscache(folio);
		fscache_write_to_cache(v9fs_inode_cookie(v9inode),
				       folio_mapping(folio), start, len, i_size,
				       v9fs_write_to_cache_done, v9inode,
				       true);
	}

D
David Howells 已提交
195
	folio_end_writeback(folio);
196
	return err;
197 198 199 200
}

static int v9fs_vfs_writepage(struct page *page, struct writeback_control *wbc)
{
D
David Howells 已提交
201
	struct folio *folio = page_folio(page);
202 203
	int retval;

D
David Howells 已提交
204
	p9_debug(P9_DEBUG_VFS, "folio %p\n", folio);
205

D
David Howells 已提交
206
	retval = v9fs_vfs_write_folio_locked(folio);
207 208
	if (retval < 0) {
		if (retval == -EAGAIN) {
D
David Howells 已提交
209
			folio_redirty_for_writepage(wbc, folio);
210 211
			retval = 0;
		} else {
D
David Howells 已提交
212
			mapping_set_error(folio_mapping(folio), retval);
213 214 215 216
		}
	} else
		retval = 0;

D
David Howells 已提交
217
	folio_unlock(folio);
218 219 220
	return retval;
}

221
static int v9fs_launder_folio(struct folio *folio)
222
{
223 224
	int retval;

D
David Howells 已提交
225 226
	if (folio_clear_dirty_for_io(folio)) {
		retval = v9fs_vfs_write_folio_locked(folio);
227 228 229
		if (retval)
			return retval;
	}
D
David Howells 已提交
230
	folio_wait_fscache(folio);
231 232 233
	return 0;
}

234 235 236
/**
 * v9fs_direct_IO - 9P address space operation for direct I/O
 * @iocb: target I/O control block
237
 * @iter: The data/buffer to use
238 239 240 241 242 243 244 245 246 247 248 249
 *
 * The presence of v9fs_direct_IO() in the address space ops vector
 * allowes open() O_DIRECT flags which would have failed otherwise.
 *
 * In the non-cached mode, we shunt off direct read and write requests before
 * the VFS gets them, so this method should never be called.
 *
 * Direct IO is not 'yet' supported in the cached mode. Hence when
 * this routine is called through generic_file_aio_read(), the read/write fails
 * with an error.
 *
 */
250
static ssize_t
251
v9fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
252
{
253
	struct file *file = iocb->ki_filp;
254
	loff_t pos = iocb->ki_pos;
A
Al Viro 已提交
255 256
	ssize_t n;
	int err = 0;
257

258
	if (iov_iter_rw(iter) == WRITE) {
A
Al Viro 已提交
259 260
		n = p9_client_write(file->private_data, pos, iter, &err);
		if (n) {
261 262
			struct inode *inode = file_inode(file);
			loff_t i_size = i_size_read(inode);
263

A
Al Viro 已提交
264 265
			if (pos + n > i_size)
				inode_add_bytes(inode, pos + n - i_size);
266
		}
A
Al Viro 已提交
267 268
	} else {
		n = p9_client_read(file->private_data, pos, iter, &err);
269
	}
A
Al Viro 已提交
270
	return n ? n : err;
271
}
272 273

static int v9fs_write_begin(struct file *filp, struct address_space *mapping,
274
			    loff_t pos, unsigned int len,
D
David Howells 已提交
275
			    struct page **subpagep, void **fsdata)
276
{
277
	int retval;
D
David Howells 已提交
278
	struct folio *folio;
279
	struct v9fs_inode *v9inode = V9FS_I(mapping->host);
280 281 282

	p9_debug(P9_DEBUG_VFS, "filp %p, mapping %p\n", filp, mapping);

283
	BUG_ON(!v9inode->writeback_fid);
284

285 286 287 288
	/* Prefetch area to be written into the cache if we're caching this
	 * file.  We need to do this before we get a lock on the page in case
	 * there's more than one writer competing for the same cache block.
	 */
289
	retval = netfs_write_begin(&v9inode->netfs, filp, mapping, pos, len, &folio, fsdata);
290 291
	if (retval < 0)
		return retval;
292

D
David Howells 已提交
293
	*subpagep = &folio->page;
294 295 296 297
	return retval;
}

static int v9fs_write_end(struct file *filp, struct address_space *mapping,
298
			  loff_t pos, unsigned int len, unsigned int copied,
D
David Howells 已提交
299
			  struct page *subpage, void *fsdata)
300 301
{
	loff_t last_pos = pos + copied;
D
David Howells 已提交
302 303
	struct folio *folio = page_folio(subpage);
	struct inode *inode = mapping->host;
304
	struct v9fs_inode *v9inode = V9FS_I(inode);
305

306 307
	p9_debug(P9_DEBUG_VFS, "filp %p, mapping %p\n", filp, mapping);

D
David Howells 已提交
308
	if (!folio_test_uptodate(folio)) {
309 310 311 312
		if (unlikely(copied < len)) {
			copied = 0;
			goto out;
		}
313

D
David Howells 已提交
314
		folio_mark_uptodate(folio);
315
	}
316

317 318 319 320 321 322 323
	/*
	 * No need to use i_size_read() here, the i_size
	 * cannot change under us because we hold the i_mutex.
	 */
	if (last_pos > inode->i_size) {
		inode_add_bytes(inode, last_pos - inode->i_size);
		i_size_write(inode, last_pos);
324
		fscache_update_cookie(v9fs_inode_cookie(v9inode), NULL, &last_pos);
325
	}
D
David Howells 已提交
326
	folio_mark_dirty(folio);
327
out:
D
David Howells 已提交
328 329
	folio_unlock(folio);
	folio_put(folio);
330 331 332 333

	return copied;
}

334 335 336 337 338
#ifdef CONFIG_9P_FSCACHE
/*
 * Mark a page as having been made dirty and thus needing writeback.  We also
 * need to pin the cache object to write back to.
 */
339
static bool v9fs_dirty_folio(struct address_space *mapping, struct folio *folio)
340
{
341
	struct v9fs_inode *v9inode = V9FS_I(mapping->host);
342

343
	return fscache_dirty_folio(mapping, folio, v9fs_inode_cookie(v9inode));
344 345
}
#else
346
#define v9fs_dirty_folio filemap_dirty_folio
347
#endif
348

349
const struct address_space_operations v9fs_addr_operations = {
350
	.read_folio = netfs_read_folio,
D
David Howells 已提交
351
	.readahead = netfs_readahead,
352
	.dirty_folio = v9fs_dirty_folio,
353 354 355
	.writepage = v9fs_vfs_writepage,
	.write_begin = v9fs_write_begin,
	.write_end = v9fs_write_end,
356
	.release_folio = v9fs_release_folio,
357
	.invalidate_folio = v9fs_invalidate_folio,
358
	.launder_folio = v9fs_launder_folio,
359
	.direct_IO = v9fs_direct_IO,
360
};