Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/nfs/direct.c | |
3 | * | |
4 | * Copyright (C) 2003 by Chuck Lever <cel@netapp.com> | |
5 | * | |
6 | * High-performance uncached I/O for the Linux NFS client | |
7 | * | |
8 | * There are important applications whose performance or correctness | |
9 | * depends on uncached access to file data. Database clusters | |
88467055 | 10 | * (multiple copies of the same instance running on separate hosts) |
1da177e4 | 11 | * implement their own cache coherency protocol that subsumes file |
88467055 CL |
12 | * system cache protocols. Applications that process datasets |
13 | * considerably larger than the client's memory do not always benefit | |
14 | * from a local cache. A streaming video server, for instance, has no | |
1da177e4 LT |
15 | * need to cache the contents of a file. |
16 | * | |
17 | * When an application requests uncached I/O, all read and write requests | |
18 | * are made directly to the server; data stored or fetched via these | |
19 | * requests is not cached in the Linux page cache. The client does not | |
20 | * correct unaligned requests from applications. All requested bytes are | |
21 | * held on permanent storage before a direct write system call returns to | |
22 | * an application. | |
23 | * | |
24 | * Solaris implements an uncached I/O facility called directio() that | |
25 | * is used for backups and sequential I/O to very large files. Solaris | |
26 | * also supports uncaching whole NFS partitions with "-o forcedirectio," | |
27 | * an undocumented mount option. | |
28 | * | |
29 | * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with | |
30 | * help from Andrew Morton. | |
31 | * | |
32 | * 18 Dec 2001 Initial implementation for 2.4 --cel | |
33 | * 08 Jul 2002 Version for 2.4.19, with bug fixes --trondmy | |
34 | * 08 Jun 2003 Port to 2.5 APIs --cel | |
35 | * 31 Mar 2004 Handle direct I/O without VFS support --cel | |
36 | * 15 Sep 2004 Parallel async reads --cel | |
88467055 | 37 | * 04 May 2005 support O_DIRECT with aio --cel |
1da177e4 LT |
38 | * |
39 | */ | |
40 | ||
1da177e4 LT |
41 | #include <linux/errno.h> |
42 | #include <linux/sched.h> | |
43 | #include <linux/kernel.h> | |
1da177e4 LT |
44 | #include <linux/file.h> |
45 | #include <linux/pagemap.h> | |
46 | #include <linux/kref.h> | |
5a0e3ad6 | 47 | #include <linux/slab.h> |
7ec10f26 | 48 | #include <linux/task_io_accounting_ops.h> |
6296556f | 49 | #include <linux/module.h> |
1da177e4 LT |
50 | |
51 | #include <linux/nfs_fs.h> | |
52 | #include <linux/nfs_page.h> | |
53 | #include <linux/sunrpc/clnt.h> | |
54 | ||
1da177e4 | 55 | #include <asm/uaccess.h> |
60063497 | 56 | #include <linux/atomic.h> |
1da177e4 | 57 | |
8d5658c9 | 58 | #include "internal.h" |
91d5b470 | 59 | #include "iostat.h" |
1763da12 | 60 | #include "pnfs.h" |
91d5b470 | 61 | |
1da177e4 | 62 | #define NFSDBG_FACILITY NFSDBG_VFS |
1da177e4 | 63 | |
e18b890b | 64 | static struct kmem_cache *nfs_direct_cachep; |
1da177e4 LT |
65 | |
66 | /* | |
67 | * This represents a set of asynchronous requests that we're waiting on | |
68 | */ | |
69 | struct nfs_direct_req { | |
70 | struct kref kref; /* release manager */ | |
15ce4a0c CL |
71 | |
72 | /* I/O parameters */ | |
a8881f5a | 73 | struct nfs_open_context *ctx; /* file open context info */ |
f11ac8db | 74 | struct nfs_lock_context *l_ctx; /* Lock context info */ |
99514f8f | 75 | struct kiocb * iocb; /* controlling i/o request */ |
88467055 | 76 | struct inode * inode; /* target file of i/o */ |
15ce4a0c CL |
77 | |
78 | /* completion state */ | |
607f31e8 | 79 | atomic_t io_count; /* i/os we're waiting for */ |
15ce4a0c | 80 | spinlock_t lock; /* protect completion state */ |
15ce4a0c | 81 | ssize_t count, /* bytes actually processed */ |
35754bc0 | 82 | bytes_left, /* bytes left to be sent */ |
1da177e4 | 83 | error; /* any reported error */ |
d72b7a6b | 84 | struct completion completion; /* wait for i/o completion */ |
fad61490 TM |
85 | |
86 | /* commit state */ | |
1763da12 FI |
87 | struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */ |
88 | struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */ | |
89 | struct work_struct work; | |
fad61490 TM |
90 | int flags; |
91 | #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ | |
92 | #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ | |
93 | struct nfs_writeverf verf; /* unstable write verifier */ | |
1da177e4 LT |
94 | }; |
95 | ||
1763da12 FI |
96 | static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops; |
97 | static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops; | |
fad61490 | 98 | static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode); |
1763da12 | 99 | static void nfs_direct_write_schedule_work(struct work_struct *work); |
607f31e8 TM |
100 | |
101 | static inline void get_dreq(struct nfs_direct_req *dreq) | |
102 | { | |
103 | atomic_inc(&dreq->io_count); | |
104 | } | |
105 | ||
106 | static inline int put_dreq(struct nfs_direct_req *dreq) | |
107 | { | |
108 | return atomic_dec_and_test(&dreq->io_count); | |
109 | } | |
110 | ||
5002c586 WAA |
111 | /* |
112 | * nfs_direct_select_verf - select the right verifier | |
113 | * @dreq - direct request possibly spanning multiple servers | |
114 | * @ds_clp - nfs_client of data server or NULL if MDS / non-pnfs | |
6cccbb6f | 115 | * @commit_idx - commit bucket index for the DS |
5002c586 WAA |
116 | * |
117 | * returns the correct verifier to use given the role of the server | |
118 | */ | |
119 | static struct nfs_writeverf * | |
120 | nfs_direct_select_verf(struct nfs_direct_req *dreq, | |
121 | struct nfs_client *ds_clp, | |
6cccbb6f | 122 | int commit_idx) |
5002c586 WAA |
123 | { |
124 | struct nfs_writeverf *verfp = &dreq->verf; | |
125 | ||
126 | #ifdef CONFIG_NFS_V4_1 | |
127 | if (ds_clp) { | |
128 | /* pNFS is in use, use the DS verf */ | |
6cccbb6f WAA |
129 | if (commit_idx >= 0 && commit_idx < dreq->ds_cinfo.nbuckets) |
130 | verfp = &dreq->ds_cinfo.buckets[commit_idx].direct_verf; | |
5002c586 WAA |
131 | else |
132 | WARN_ON_ONCE(1); | |
133 | } | |
134 | #endif | |
135 | return verfp; | |
136 | } | |
137 | ||
138 | ||
139 | /* | |
140 | * nfs_direct_set_hdr_verf - set the write/commit verifier | |
141 | * @dreq - direct request possibly spanning multiple servers | |
142 | * @hdr - pageio header to validate against previously seen verfs | |
143 | * | |
144 | * Set the server's (MDS or DS) "seen" verifier | |
145 | */ | |
146 | static void nfs_direct_set_hdr_verf(struct nfs_direct_req *dreq, | |
147 | struct nfs_pgio_header *hdr) | |
148 | { | |
149 | struct nfs_writeverf *verfp; | |
150 | ||
6cccbb6f | 151 | verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx); |
5002c586 WAA |
152 | WARN_ON_ONCE(verfp->committed >= 0); |
153 | memcpy(verfp, &hdr->verf, sizeof(struct nfs_writeverf)); | |
154 | WARN_ON_ONCE(verfp->committed < 0); | |
155 | } | |
156 | ||
157 | /* | |
158 | * nfs_direct_cmp_hdr_verf - compare verifier for pgio header | |
159 | * @dreq - direct request possibly spanning multiple servers | |
160 | * @hdr - pageio header to validate against previously seen verf | |
161 | * | |
162 | * set the server's "seen" verf if not initialized. | |
163 | * returns result of comparison between @hdr->verf and the "seen" | |
164 | * verf of the server used by @hdr (DS or MDS) | |
165 | */ | |
166 | static int nfs_direct_set_or_cmp_hdr_verf(struct nfs_direct_req *dreq, | |
167 | struct nfs_pgio_header *hdr) | |
168 | { | |
169 | struct nfs_writeverf *verfp; | |
170 | ||
6cccbb6f | 171 | verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx); |
5002c586 WAA |
172 | if (verfp->committed < 0) { |
173 | nfs_direct_set_hdr_verf(dreq, hdr); | |
174 | return 0; | |
175 | } | |
176 | return memcmp(verfp, &hdr->verf, sizeof(struct nfs_writeverf)); | |
177 | } | |
178 | ||
5002c586 WAA |
179 | /* |
180 | * nfs_direct_cmp_commit_data_verf - compare verifier for commit data | |
181 | * @dreq - direct request possibly spanning multiple servers | |
182 | * @data - commit data to validate against previously seen verf | |
183 | * | |
184 | * returns result of comparison between @data->verf and the verf of | |
185 | * the server used by @data (DS or MDS) | |
186 | */ | |
187 | static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq, | |
188 | struct nfs_commit_data *data) | |
189 | { | |
190 | struct nfs_writeverf *verfp; | |
191 | ||
192 | verfp = nfs_direct_select_verf(dreq, data->ds_clp, | |
193 | data->ds_commit_index); | |
194 | WARN_ON_ONCE(verfp->committed < 0); | |
195 | return memcmp(verfp, &data->verf, sizeof(struct nfs_writeverf)); | |
196 | } | |
5002c586 | 197 | |
1da177e4 | 198 | /** |
b8a32e2b CL |
199 | * nfs_direct_IO - NFS address space operation for direct I/O |
200 | * @rw: direction (read or write) | |
201 | * @iocb: target I/O control block | |
202 | * @iov: array of vectors that define I/O buffer | |
203 | * @pos: offset in file to begin the operation | |
204 | * @nr_segs: size of iovec array | |
205 | * | |
206 | * The presence of this routine in the address space ops vector means | |
a564b8f0 MG |
207 | * the NFS client supports direct I/O. However, for most direct IO, we |
208 | * shunt off direct read and write requests before the VFS gets them, | |
209 | * so this method is only ever called for swap. | |
1da177e4 | 210 | */ |
d8d3d94b | 211 | ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter, loff_t pos) |
b8a32e2b | 212 | { |
a564b8f0 | 213 | #ifndef CONFIG_NFS_SWAP |
6de1472f | 214 | dprintk("NFS: nfs_direct_IO (%pD) off/no(%Ld/%lu) EINVAL\n", |
d8d3d94b | 215 | iocb->ki_filp, (long long) pos, iter->nr_segs); |
b8a32e2b CL |
216 | |
217 | return -EINVAL; | |
a564b8f0 | 218 | #else |
a564b8f0 MG |
219 | VM_BUG_ON(iocb->ki_nbytes != PAGE_SIZE); |
220 | ||
e19a8a0a MP |
221 | if (rw == READ) |
222 | return nfs_file_direct_read(iocb, iter, pos); | |
223 | return nfs_file_direct_write(iocb, iter, pos); | |
a564b8f0 | 224 | #endif /* CONFIG_NFS_SWAP */ |
b8a32e2b CL |
225 | } |
226 | ||
749e146e | 227 | static void nfs_direct_release_pages(struct page **pages, unsigned int npages) |
9c93ab7d | 228 | { |
749e146e | 229 | unsigned int i; |
607f31e8 TM |
230 | for (i = 0; i < npages; i++) |
231 | page_cache_release(pages[i]); | |
6b45d858 TM |
232 | } |
233 | ||
1763da12 FI |
234 | void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo, |
235 | struct nfs_direct_req *dreq) | |
236 | { | |
237 | cinfo->lock = &dreq->lock; | |
238 | cinfo->mds = &dreq->mds_cinfo; | |
239 | cinfo->ds = &dreq->ds_cinfo; | |
240 | cinfo->dreq = dreq; | |
241 | cinfo->completion_ops = &nfs_direct_commit_completion_ops; | |
242 | } | |
243 | ||
93619e59 | 244 | static inline struct nfs_direct_req *nfs_direct_req_alloc(void) |
1da177e4 | 245 | { |
93619e59 CL |
246 | struct nfs_direct_req *dreq; |
247 | ||
292f3eee | 248 | dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL); |
93619e59 CL |
249 | if (!dreq) |
250 | return NULL; | |
251 | ||
252 | kref_init(&dreq->kref); | |
607f31e8 | 253 | kref_get(&dreq->kref); |
d72b7a6b | 254 | init_completion(&dreq->completion); |
1763da12 | 255 | INIT_LIST_HEAD(&dreq->mds_cinfo.list); |
5002c586 | 256 | dreq->verf.committed = NFS_INVALID_STABLE_HOW; /* not set yet */ |
1763da12 | 257 | INIT_WORK(&dreq->work, nfs_direct_write_schedule_work); |
15ce4a0c | 258 | spin_lock_init(&dreq->lock); |
93619e59 CL |
259 | |
260 | return dreq; | |
1da177e4 LT |
261 | } |
262 | ||
b4946ffb | 263 | static void nfs_direct_req_free(struct kref *kref) |
1da177e4 LT |
264 | { |
265 | struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); | |
a8881f5a | 266 | |
8c393f9a | 267 | nfs_free_pnfs_ds_cinfo(&dreq->ds_cinfo); |
f11ac8db TM |
268 | if (dreq->l_ctx != NULL) |
269 | nfs_put_lock_context(dreq->l_ctx); | |
a8881f5a TM |
270 | if (dreq->ctx != NULL) |
271 | put_nfs_open_context(dreq->ctx); | |
1da177e4 LT |
272 | kmem_cache_free(nfs_direct_cachep, dreq); |
273 | } | |
274 | ||
b4946ffb TM |
275 | static void nfs_direct_req_release(struct nfs_direct_req *dreq) |
276 | { | |
277 | kref_put(&dreq->kref, nfs_direct_req_free); | |
278 | } | |
279 | ||
6296556f PT |
280 | ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq) |
281 | { | |
282 | return dreq->bytes_left; | |
283 | } | |
284 | EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left); | |
285 | ||
bc0fb201 CL |
286 | /* |
287 | * Collects and returns the final error value/byte-count. | |
288 | */ | |
289 | static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) | |
290 | { | |
15ce4a0c | 291 | ssize_t result = -EIOCBQUEUED; |
bc0fb201 CL |
292 | |
293 | /* Async requests don't wait here */ | |
294 | if (dreq->iocb) | |
295 | goto out; | |
296 | ||
150030b7 | 297 | result = wait_for_completion_killable(&dreq->completion); |
bc0fb201 CL |
298 | |
299 | if (!result) | |
15ce4a0c | 300 | result = dreq->error; |
bc0fb201 | 301 | if (!result) |
15ce4a0c | 302 | result = dreq->count; |
bc0fb201 CL |
303 | |
304 | out: | |
bc0fb201 CL |
305 | return (ssize_t) result; |
306 | } | |
307 | ||
63ab46ab | 308 | /* |
607f31e8 TM |
309 | * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust |
310 | * the iocb is still valid here if this is a synchronous request. | |
63ab46ab | 311 | */ |
9811cd57 | 312 | static void nfs_direct_complete(struct nfs_direct_req *dreq, bool write) |
63ab46ab | 313 | { |
9811cd57 CH |
314 | struct inode *inode = dreq->inode; |
315 | ||
2a009ec9 | 316 | if (dreq->iocb && write) { |
9811cd57 | 317 | loff_t pos = dreq->iocb->ki_pos + dreq->count; |
2a009ec9 CH |
318 | |
319 | spin_lock(&inode->i_lock); | |
320 | if (i_size_read(inode) < pos) | |
321 | i_size_write(inode, pos); | |
322 | spin_unlock(&inode->i_lock); | |
323 | } | |
324 | ||
1f90ee27 | 325 | if (write) |
2a009ec9 | 326 | nfs_zap_mapping(inode, inode->i_mapping); |
1f90ee27 CH |
327 | |
328 | inode_dio_done(inode); | |
2a009ec9 CH |
329 | |
330 | if (dreq->iocb) { | |
15ce4a0c | 331 | long res = (long) dreq->error; |
63ab46ab | 332 | if (!res) |
15ce4a0c | 333 | res = (long) dreq->count; |
63ab46ab | 334 | aio_complete(dreq->iocb, res, 0); |
d72b7a6b | 335 | } |
2a009ec9 | 336 | |
d72b7a6b | 337 | complete_all(&dreq->completion); |
63ab46ab | 338 | |
b4946ffb | 339 | nfs_direct_req_release(dreq); |
63ab46ab CL |
340 | } |
341 | ||
1385b811 | 342 | static void nfs_direct_readpage_release(struct nfs_page *req) |
1da177e4 | 343 | { |
1e8968c5 | 344 | dprintk("NFS: direct read done (%s/%llu %d@%lld)\n", |
584aa810 | 345 | req->wb_context->dentry->d_inode->i_sb->s_id, |
1e8968c5 | 346 | (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode), |
584aa810 FI |
347 | req->wb_bytes, |
348 | (long long)req_offset(req)); | |
349 | nfs_release_request(req); | |
fdd1e74c TM |
350 | } |
351 | ||
584aa810 | 352 | static void nfs_direct_read_completion(struct nfs_pgio_header *hdr) |
fdd1e74c | 353 | { |
584aa810 FI |
354 | unsigned long bytes = 0; |
355 | struct nfs_direct_req *dreq = hdr->dreq; | |
fdd1e74c | 356 | |
584aa810 FI |
357 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) |
358 | goto out_put; | |
15ce4a0c CL |
359 | |
360 | spin_lock(&dreq->lock); | |
584aa810 FI |
361 | if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0)) |
362 | dreq->error = hdr->error; | |
a7d42ddb WAA |
363 | else { |
364 | /* | |
365 | * FIXME: right now this only accounts for bytes written | |
366 | * to the first mirror | |
367 | */ | |
368 | if (hdr->pgio_mirror_idx == 0) | |
369 | dreq->count += hdr->good_bytes; | |
370 | } | |
584aa810 FI |
371 | spin_unlock(&dreq->lock); |
372 | ||
4bd8b010 TM |
373 | while (!list_empty(&hdr->pages)) { |
374 | struct nfs_page *req = nfs_list_entry(hdr->pages.next); | |
375 | struct page *page = req->wb_page; | |
376 | ||
be7e9858 JL |
377 | if (!PageCompound(page) && bytes < hdr->good_bytes) |
378 | set_page_dirty(page); | |
4bd8b010 TM |
379 | bytes += req->wb_bytes; |
380 | nfs_list_remove_request(req); | |
381 | nfs_direct_readpage_release(req); | |
d4a8f367 | 382 | } |
584aa810 | 383 | out_put: |
607f31e8 | 384 | if (put_dreq(dreq)) |
9811cd57 | 385 | nfs_direct_complete(dreq, false); |
584aa810 | 386 | hdr->release(hdr); |
1da177e4 LT |
387 | } |
388 | ||
3e9e0ca3 | 389 | static void nfs_read_sync_pgio_error(struct list_head *head) |
cd841605 | 390 | { |
584aa810 | 391 | struct nfs_page *req; |
cd841605 | 392 | |
584aa810 FI |
393 | while (!list_empty(head)) { |
394 | req = nfs_list_entry(head->next); | |
395 | nfs_list_remove_request(req); | |
396 | nfs_release_request(req); | |
397 | } | |
cd841605 FI |
398 | } |
399 | ||
584aa810 FI |
400 | static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr) |
401 | { | |
402 | get_dreq(hdr->dreq); | |
403 | } | |
404 | ||
405 | static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = { | |
3e9e0ca3 | 406 | .error_cleanup = nfs_read_sync_pgio_error, |
584aa810 FI |
407 | .init_hdr = nfs_direct_pgio_init, |
408 | .completion = nfs_direct_read_completion, | |
409 | }; | |
410 | ||
d4cc948b | 411 | /* |
607f31e8 TM |
412 | * For each rsize'd chunk of the user's buffer, dispatch an NFS READ |
413 | * operation. If nfs_readdata_alloc() or get_user_pages() fails, | |
414 | * bail and stop sending more reads. Read length accounting is | |
415 | * handled automatically by nfs_direct_read_result(). Otherwise, if | |
416 | * no requests have been sent, just return an error. | |
1da177e4 | 417 | */ |
1da177e4 | 418 | |
91f79c43 AV |
419 | static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq, |
420 | struct iov_iter *iter, | |
421 | loff_t pos) | |
422 | { | |
423 | struct nfs_pageio_descriptor desc; | |
424 | struct inode *inode = dreq->inode; | |
425 | ssize_t result = -EINVAL; | |
426 | size_t requested_bytes = 0; | |
427 | size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE); | |
e9f7bee1 | 428 | |
16b90578 | 429 | nfs_pageio_init_read(&desc, dreq->inode, false, |
91f79c43 AV |
430 | &nfs_direct_read_completion_ops); |
431 | get_dreq(dreq); | |
432 | desc.pg_dreq = dreq; | |
433 | atomic_inc(&inode->i_dio_count); | |
a564b8f0 | 434 | |
91f79c43 AV |
435 | while (iov_iter_count(iter)) { |
436 | struct page **pagevec; | |
437 | size_t bytes; | |
438 | size_t pgbase; | |
439 | unsigned npages, i; | |
607f31e8 | 440 | |
91f79c43 AV |
441 | result = iov_iter_get_pages_alloc(iter, &pagevec, |
442 | rsize, &pgbase); | |
443 | if (result < 0) | |
444 | break; | |
445 | ||
446 | bytes = result; | |
447 | iov_iter_advance(iter, bytes); | |
448 | npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; | |
584aa810 FI |
449 | for (i = 0; i < npages; i++) { |
450 | struct nfs_page *req; | |
bf5fc402 | 451 | unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); |
584aa810 | 452 | /* XXX do we need to do the eof zeroing found in async_filler? */ |
2bfc6e56 | 453 | req = nfs_create_request(dreq->ctx, pagevec[i], NULL, |
584aa810 FI |
454 | pgbase, req_len); |
455 | if (IS_ERR(req)) { | |
584aa810 FI |
456 | result = PTR_ERR(req); |
457 | break; | |
458 | } | |
459 | req->wb_index = pos >> PAGE_SHIFT; | |
460 | req->wb_offset = pos & ~PAGE_MASK; | |
91f79c43 AV |
461 | if (!nfs_pageio_add_request(&desc, req)) { |
462 | result = desc.pg_error; | |
584aa810 | 463 | nfs_release_request(req); |
584aa810 FI |
464 | break; |
465 | } | |
466 | pgbase = 0; | |
467 | bytes -= req_len; | |
91f79c43 | 468 | requested_bytes += req_len; |
584aa810 | 469 | pos += req_len; |
35754bc0 | 470 | dreq->bytes_left -= req_len; |
584aa810 | 471 | } |
6d74743b | 472 | nfs_direct_release_pages(pagevec, npages); |
91f79c43 | 473 | kvfree(pagevec); |
19f73787 CL |
474 | if (result < 0) |
475 | break; | |
19f73787 CL |
476 | } |
477 | ||
584aa810 FI |
478 | nfs_pageio_complete(&desc); |
479 | ||
839f7ad6 CL |
480 | /* |
481 | * If no bytes were started, return the error, and let the | |
482 | * generic layer handle the completion. | |
483 | */ | |
484 | if (requested_bytes == 0) { | |
1f90ee27 | 485 | inode_dio_done(inode); |
839f7ad6 CL |
486 | nfs_direct_req_release(dreq); |
487 | return result < 0 ? result : -EIO; | |
488 | } | |
489 | ||
19f73787 | 490 | if (put_dreq(dreq)) |
9811cd57 | 491 | nfs_direct_complete(dreq, false); |
839f7ad6 | 492 | return 0; |
19f73787 CL |
493 | } |
494 | ||
14a3ec79 CH |
495 | /** |
496 | * nfs_file_direct_read - file direct read operation for NFS files | |
497 | * @iocb: target I/O control block | |
619d30b4 | 498 | * @iter: vector of user buffers into which to read data |
14a3ec79 CH |
499 | * @pos: byte offset in file where reading starts |
500 | * | |
501 | * We use this function for direct reads instead of calling | |
502 | * generic_file_aio_read() in order to avoid gfar's check to see if | |
503 | * the request starts before the end of the file. For that check | |
504 | * to work, we must generate a GETATTR before each direct read, and | |
505 | * even then there is a window between the GETATTR and the subsequent | |
506 | * READ where the file size could change. Our preference is simply | |
507 | * to do all reads the application wants, and the server will take | |
508 | * care of managing the end of file boundary. | |
509 | * | |
510 | * This function also eliminates unnecessarily updating the file's | |
511 | * atime locally, as the NFS server sets the file's atime, and this | |
512 | * client must read the updated atime from the server back into its | |
513 | * cache. | |
514 | */ | |
619d30b4 | 515 | ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter, |
e19a8a0a | 516 | loff_t pos) |
1da177e4 | 517 | { |
14a3ec79 CH |
518 | struct file *file = iocb->ki_filp; |
519 | struct address_space *mapping = file->f_mapping; | |
520 | struct inode *inode = mapping->host; | |
1da177e4 | 521 | struct nfs_direct_req *dreq; |
b3c54de6 | 522 | struct nfs_lock_context *l_ctx; |
14a3ec79 | 523 | ssize_t result = -EINVAL; |
a6cbcd4a | 524 | size_t count = iov_iter_count(iter); |
14a3ec79 CH |
525 | nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count); |
526 | ||
527 | dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n", | |
528 | file, count, (long long) pos); | |
529 | ||
530 | result = 0; | |
531 | if (!count) | |
532 | goto out; | |
533 | ||
d0b9875d | 534 | mutex_lock(&inode->i_mutex); |
14a3ec79 CH |
535 | result = nfs_sync_mapping(mapping); |
536 | if (result) | |
d0b9875d | 537 | goto out_unlock; |
1da177e4 | 538 | |
14a3ec79 CH |
539 | task_io_account_read(count); |
540 | ||
541 | result = -ENOMEM; | |
607f31e8 | 542 | dreq = nfs_direct_req_alloc(); |
f11ac8db | 543 | if (dreq == NULL) |
d0b9875d | 544 | goto out_unlock; |
1da177e4 | 545 | |
91d5b470 | 546 | dreq->inode = inode; |
619d30b4 | 547 | dreq->bytes_left = count; |
cd3758e3 | 548 | dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); |
b3c54de6 TM |
549 | l_ctx = nfs_get_lock_context(dreq->ctx); |
550 | if (IS_ERR(l_ctx)) { | |
551 | result = PTR_ERR(l_ctx); | |
f11ac8db | 552 | goto out_release; |
b3c54de6 TM |
553 | } |
554 | dreq->l_ctx = l_ctx; | |
487b8372 CL |
555 | if (!is_sync_kiocb(iocb)) |
556 | dreq->iocb = iocb; | |
1da177e4 | 557 | |
619d30b4 | 558 | NFS_I(inode)->read_io += count; |
91f79c43 | 559 | result = nfs_direct_read_schedule_iovec(dreq, iter, pos); |
d0b9875d CH |
560 | |
561 | mutex_unlock(&inode->i_mutex); | |
562 | ||
14a3ec79 | 563 | if (!result) { |
607f31e8 | 564 | result = nfs_direct_wait(dreq); |
14a3ec79 CH |
565 | if (result > 0) |
566 | iocb->ki_pos = pos + result; | |
567 | } | |
d0b9875d CH |
568 | |
569 | nfs_direct_req_release(dreq); | |
570 | return result; | |
571 | ||
f11ac8db | 572 | out_release: |
b4946ffb | 573 | nfs_direct_req_release(dreq); |
d0b9875d CH |
574 | out_unlock: |
575 | mutex_unlock(&inode->i_mutex); | |
f11ac8db | 576 | out: |
1da177e4 LT |
577 | return result; |
578 | } | |
579 | ||
085d1e33 TH |
580 | static void |
581 | nfs_direct_write_scan_commit_list(struct inode *inode, | |
582 | struct list_head *list, | |
583 | struct nfs_commit_info *cinfo) | |
584 | { | |
585 | spin_lock(cinfo->lock); | |
586 | #ifdef CONFIG_NFS_V4_1 | |
587 | if (cinfo->ds != NULL && cinfo->ds->nwritten != 0) | |
588 | NFS_SERVER(inode)->pnfs_curr_ld->recover_commit_reqs(list, cinfo); | |
589 | #endif | |
590 | nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0); | |
591 | spin_unlock(cinfo->lock); | |
592 | } | |
593 | ||
fad61490 TM |
594 | static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) |
595 | { | |
1763da12 FI |
596 | struct nfs_pageio_descriptor desc; |
597 | struct nfs_page *req, *tmp; | |
598 | LIST_HEAD(reqs); | |
599 | struct nfs_commit_info cinfo; | |
600 | LIST_HEAD(failed); | |
601 | ||
602 | nfs_init_cinfo_from_dreq(&cinfo, dreq); | |
085d1e33 | 603 | nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo); |
1da177e4 | 604 | |
fad61490 | 605 | dreq->count = 0; |
607f31e8 TM |
606 | get_dreq(dreq); |
607 | ||
a20c93e3 | 608 | nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false, |
1763da12 FI |
609 | &nfs_direct_write_completion_ops); |
610 | desc.pg_dreq = dreq; | |
fedb595c | 611 | |
1763da12 FI |
612 | list_for_each_entry_safe(req, tmp, &reqs, wb_list) { |
613 | if (!nfs_pageio_add_request(&desc, req)) { | |
4035c248 | 614 | nfs_list_remove_request(req); |
1763da12 FI |
615 | nfs_list_add_request(req, &failed); |
616 | spin_lock(cinfo.lock); | |
617 | dreq->flags = 0; | |
618 | dreq->error = -EIO; | |
619 | spin_unlock(cinfo.lock); | |
620 | } | |
5a695da2 | 621 | nfs_release_request(req); |
1763da12 FI |
622 | } |
623 | nfs_pageio_complete(&desc); | |
fad61490 | 624 | |
4035c248 TM |
625 | while (!list_empty(&failed)) { |
626 | req = nfs_list_entry(failed.next); | |
627 | nfs_list_remove_request(req); | |
1d1afcbc | 628 | nfs_unlock_and_release_request(req); |
4035c248 | 629 | } |
fad61490 | 630 | |
1763da12 FI |
631 | if (put_dreq(dreq)) |
632 | nfs_direct_write_complete(dreq, dreq->inode); | |
c9d8f89d TM |
633 | } |
634 | ||
1763da12 | 635 | static void nfs_direct_commit_complete(struct nfs_commit_data *data) |
c9d8f89d | 636 | { |
0b7c0153 | 637 | struct nfs_direct_req *dreq = data->dreq; |
1763da12 FI |
638 | struct nfs_commit_info cinfo; |
639 | struct nfs_page *req; | |
c9d8f89d TM |
640 | int status = data->task.tk_status; |
641 | ||
1763da12 | 642 | nfs_init_cinfo_from_dreq(&cinfo, dreq); |
c9d8f89d | 643 | if (status < 0) { |
60fa3f76 | 644 | dprintk("NFS: %5u commit failed with error %d.\n", |
1763da12 | 645 | data->task.tk_pid, status); |
fad61490 | 646 | dreq->flags = NFS_ODIRECT_RESCHED_WRITES; |
5002c586 | 647 | } else if (nfs_direct_cmp_commit_data_verf(dreq, data)) { |
c9d8f89d | 648 | dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid); |
fad61490 | 649 | dreq->flags = NFS_ODIRECT_RESCHED_WRITES; |
1da177e4 LT |
650 | } |
651 | ||
c9d8f89d | 652 | dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status); |
1763da12 FI |
653 | while (!list_empty(&data->pages)) { |
654 | req = nfs_list_entry(data->pages.next); | |
655 | nfs_list_remove_request(req); | |
656 | if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) { | |
657 | /* Note the rewrite will go through mds */ | |
b57ff130 | 658 | nfs_mark_request_commit(req, NULL, &cinfo, 0); |
906369e4 FI |
659 | } else |
660 | nfs_release_request(req); | |
1d1afcbc | 661 | nfs_unlock_and_release_request(req); |
1763da12 FI |
662 | } |
663 | ||
664 | if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) | |
665 | nfs_direct_write_complete(dreq, data->inode); | |
1da177e4 LT |
666 | } |
667 | ||
1763da12 FI |
668 | static void nfs_direct_error_cleanup(struct nfs_inode *nfsi) |
669 | { | |
670 | /* There is no lock to clear */ | |
671 | } | |
672 | ||
673 | static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = { | |
674 | .completion = nfs_direct_commit_complete, | |
675 | .error_cleanup = nfs_direct_error_cleanup, | |
fad61490 TM |
676 | }; |
677 | ||
678 | static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) | |
1da177e4 | 679 | { |
1763da12 FI |
680 | int res; |
681 | struct nfs_commit_info cinfo; | |
682 | LIST_HEAD(mds_list); | |
683 | ||
684 | nfs_init_cinfo_from_dreq(&cinfo, dreq); | |
685 | nfs_scan_commit(dreq->inode, &mds_list, &cinfo); | |
686 | res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo); | |
687 | if (res < 0) /* res == -ENOMEM */ | |
688 | nfs_direct_write_reschedule(dreq); | |
fad61490 | 689 | } |
1da177e4 | 690 | |
1763da12 | 691 | static void nfs_direct_write_schedule_work(struct work_struct *work) |
fad61490 | 692 | { |
1763da12 | 693 | struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work); |
fad61490 | 694 | int flags = dreq->flags; |
1da177e4 | 695 | |
fad61490 TM |
696 | dreq->flags = 0; |
697 | switch (flags) { | |
698 | case NFS_ODIRECT_DO_COMMIT: | |
699 | nfs_direct_commit_schedule(dreq); | |
1da177e4 | 700 | break; |
fad61490 TM |
701 | case NFS_ODIRECT_RESCHED_WRITES: |
702 | nfs_direct_write_reschedule(dreq); | |
703 | break; | |
704 | default: | |
9811cd57 | 705 | nfs_direct_complete(dreq, true); |
fad61490 TM |
706 | } |
707 | } | |
1da177e4 | 708 | |
1763da12 | 709 | static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) |
fad61490 | 710 | { |
1763da12 | 711 | schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */ |
fad61490 | 712 | } |
1763da12 | 713 | |
1763da12 FI |
714 | static void nfs_direct_write_completion(struct nfs_pgio_header *hdr) |
715 | { | |
716 | struct nfs_direct_req *dreq = hdr->dreq; | |
717 | struct nfs_commit_info cinfo; | |
c65e6254 | 718 | bool request_commit = false; |
1763da12 FI |
719 | struct nfs_page *req = nfs_list_entry(hdr->pages.next); |
720 | ||
721 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) | |
722 | goto out_put; | |
723 | ||
724 | nfs_init_cinfo_from_dreq(&cinfo, dreq); | |
725 | ||
726 | spin_lock(&dreq->lock); | |
727 | ||
728 | if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) { | |
729 | dreq->flags = 0; | |
730 | dreq->error = hdr->error; | |
731 | } | |
c65e6254 | 732 | if (dreq->error == 0) { |
a7d42ddb WAA |
733 | /* |
734 | * FIXME: right now this only accounts for bytes written | |
735 | * to the first mirror | |
736 | */ | |
737 | if (hdr->pgio_mirror_idx == 0) | |
738 | dreq->count += hdr->good_bytes; | |
c65e6254 | 739 | if (nfs_write_need_commit(hdr)) { |
1763da12 | 740 | if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) |
c65e6254 | 741 | request_commit = true; |
1763da12 | 742 | else if (dreq->flags == 0) { |
5002c586 | 743 | nfs_direct_set_hdr_verf(dreq, hdr); |
c65e6254 | 744 | request_commit = true; |
1763da12 FI |
745 | dreq->flags = NFS_ODIRECT_DO_COMMIT; |
746 | } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) { | |
c65e6254 WAA |
747 | request_commit = true; |
748 | if (nfs_direct_set_or_cmp_hdr_verf(dreq, hdr)) | |
5002c586 WAA |
749 | dreq->flags = |
750 | NFS_ODIRECT_RESCHED_WRITES; | |
1763da12 FI |
751 | } |
752 | } | |
753 | } | |
754 | spin_unlock(&dreq->lock); | |
755 | ||
756 | while (!list_empty(&hdr->pages)) { | |
2bfc6e56 | 757 | |
1763da12 FI |
758 | req = nfs_list_entry(hdr->pages.next); |
759 | nfs_list_remove_request(req); | |
c65e6254 | 760 | if (request_commit) { |
04277086 | 761 | kref_get(&req->wb_kref); |
b57ff130 WAA |
762 | nfs_mark_request_commit(req, hdr->lseg, &cinfo, |
763 | hdr->ds_commit_idx); | |
1763da12 | 764 | } |
1d1afcbc | 765 | nfs_unlock_and_release_request(req); |
1763da12 FI |
766 | } |
767 | ||
768 | out_put: | |
769 | if (put_dreq(dreq)) | |
770 | nfs_direct_write_complete(dreq, hdr->inode); | |
771 | hdr->release(hdr); | |
772 | } | |
773 | ||
3e9e0ca3 TM |
774 | static void nfs_write_sync_pgio_error(struct list_head *head) |
775 | { | |
776 | struct nfs_page *req; | |
777 | ||
778 | while (!list_empty(head)) { | |
779 | req = nfs_list_entry(head->next); | |
780 | nfs_list_remove_request(req); | |
1d1afcbc | 781 | nfs_unlock_and_release_request(req); |
3e9e0ca3 TM |
782 | } |
783 | } | |
784 | ||
1763da12 | 785 | static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = { |
3e9e0ca3 | 786 | .error_cleanup = nfs_write_sync_pgio_error, |
1763da12 FI |
787 | .init_hdr = nfs_direct_pgio_init, |
788 | .completion = nfs_direct_write_completion, | |
789 | }; | |
790 | ||
91f79c43 AV |
791 | |
792 | /* | |
793 | * NB: Return the value of the first error return code. Subsequent | |
794 | * errors after the first one are ignored. | |
795 | */ | |
796 | /* | |
797 | * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE | |
798 | * operation. If nfs_writedata_alloc() or get_user_pages() fails, | |
799 | * bail and stop sending more writes. Write length accounting is | |
800 | * handled automatically by nfs_direct_write_result(). Otherwise, if | |
801 | * no requests have been sent, just return an error. | |
802 | */ | |
19f73787 | 803 | static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq, |
619d30b4 | 804 | struct iov_iter *iter, |
91f79c43 | 805 | loff_t pos) |
19f73787 | 806 | { |
1763da12 | 807 | struct nfs_pageio_descriptor desc; |
1d59d61f | 808 | struct inode *inode = dreq->inode; |
19f73787 CL |
809 | ssize_t result = 0; |
810 | size_t requested_bytes = 0; | |
91f79c43 | 811 | size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE); |
19f73787 | 812 | |
a20c93e3 | 813 | nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false, |
1763da12 FI |
814 | &nfs_direct_write_completion_ops); |
815 | desc.pg_dreq = dreq; | |
19f73787 | 816 | get_dreq(dreq); |
1d59d61f | 817 | atomic_inc(&inode->i_dio_count); |
19f73787 | 818 | |
91f79c43 AV |
819 | NFS_I(inode)->write_io += iov_iter_count(iter); |
820 | while (iov_iter_count(iter)) { | |
821 | struct page **pagevec; | |
822 | size_t bytes; | |
823 | size_t pgbase; | |
824 | unsigned npages, i; | |
825 | ||
826 | result = iov_iter_get_pages_alloc(iter, &pagevec, | |
827 | wsize, &pgbase); | |
19f73787 CL |
828 | if (result < 0) |
829 | break; | |
91f79c43 AV |
830 | |
831 | bytes = result; | |
832 | iov_iter_advance(iter, bytes); | |
833 | npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; | |
834 | for (i = 0; i < npages; i++) { | |
835 | struct nfs_page *req; | |
836 | unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); | |
837 | ||
16b90578 | 838 | req = nfs_create_request(dreq->ctx, pagevec[i], NULL, |
91f79c43 AV |
839 | pgbase, req_len); |
840 | if (IS_ERR(req)) { | |
841 | result = PTR_ERR(req); | |
842 | break; | |
843 | } | |
844 | nfs_lock_request(req); | |
845 | req->wb_index = pos >> PAGE_SHIFT; | |
846 | req->wb_offset = pos & ~PAGE_MASK; | |
847 | if (!nfs_pageio_add_request(&desc, req)) { | |
848 | result = desc.pg_error; | |
849 | nfs_unlock_and_release_request(req); | |
850 | break; | |
851 | } | |
852 | pgbase = 0; | |
853 | bytes -= req_len; | |
854 | requested_bytes += req_len; | |
855 | pos += req_len; | |
856 | dreq->bytes_left -= req_len; | |
857 | } | |
858 | nfs_direct_release_pages(pagevec, npages); | |
859 | kvfree(pagevec); | |
860 | if (result < 0) | |
19f73787 | 861 | break; |
19f73787 | 862 | } |
1763da12 | 863 | nfs_pageio_complete(&desc); |
19f73787 | 864 | |
839f7ad6 CL |
865 | /* |
866 | * If no bytes were started, return the error, and let the | |
867 | * generic layer handle the completion. | |
868 | */ | |
869 | if (requested_bytes == 0) { | |
1d59d61f | 870 | inode_dio_done(inode); |
839f7ad6 CL |
871 | nfs_direct_req_release(dreq); |
872 | return result < 0 ? result : -EIO; | |
873 | } | |
874 | ||
19f73787 CL |
875 | if (put_dreq(dreq)) |
876 | nfs_direct_write_complete(dreq, dreq->inode); | |
839f7ad6 | 877 | return 0; |
19f73787 CL |
878 | } |
879 | ||
1da177e4 LT |
880 | /** |
881 | * nfs_file_direct_write - file direct write operation for NFS files | |
882 | * @iocb: target I/O control block | |
619d30b4 | 883 | * @iter: vector of user buffers from which to write data |
88467055 | 884 | * @pos: byte offset in file where writing starts |
1da177e4 LT |
885 | * |
886 | * We use this function for direct writes instead of calling | |
887 | * generic_file_aio_write() in order to avoid taking the inode | |
888 | * semaphore and updating the i_size. The NFS server will set | |
889 | * the new i_size and this client must read the updated size | |
890 | * back into its cache. We let the server do generic write | |
891 | * parameter checking and report problems. | |
892 | * | |
1da177e4 LT |
893 | * We eliminate local atime updates, see direct read above. |
894 | * | |
895 | * We avoid unnecessary page cache invalidations for normal cached | |
896 | * readers of this file. | |
897 | * | |
898 | * Note that O_APPEND is not supported for NFS direct writes, as there | |
899 | * is no atomic O_APPEND write facility in the NFS protocol. | |
900 | */ | |
619d30b4 | 901 | ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter, |
e19a8a0a | 902 | loff_t pos) |
1da177e4 | 903 | { |
22cd1bf1 | 904 | ssize_t result = -EINVAL; |
1da177e4 | 905 | struct file *file = iocb->ki_filp; |
1da177e4 | 906 | struct address_space *mapping = file->f_mapping; |
22cd1bf1 CH |
907 | struct inode *inode = mapping->host; |
908 | struct nfs_direct_req *dreq; | |
909 | struct nfs_lock_context *l_ctx; | |
a9ab5e84 | 910 | loff_t end; |
a6cbcd4a | 911 | size_t count = iov_iter_count(iter); |
a9ab5e84 CH |
912 | end = (pos + count - 1) >> PAGE_CACHE_SHIFT; |
913 | ||
c216fd70 CL |
914 | nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count); |
915 | ||
6de1472f AV |
916 | dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n", |
917 | file, count, (long long) pos); | |
027445c3 | 918 | |
22cd1bf1 CH |
919 | result = generic_write_checks(file, &pos, &count, 0); |
920 | if (result) | |
1da177e4 | 921 | goto out; |
ce1a8e67 | 922 | |
22cd1bf1 | 923 | result = -EINVAL; |
ce1a8e67 | 924 | if ((ssize_t) count < 0) |
1da177e4 | 925 | goto out; |
22cd1bf1 | 926 | result = 0; |
1da177e4 LT |
927 | if (!count) |
928 | goto out; | |
ce1a8e67 | 929 | |
a9ab5e84 CH |
930 | mutex_lock(&inode->i_mutex); |
931 | ||
22cd1bf1 CH |
932 | result = nfs_sync_mapping(mapping); |
933 | if (result) | |
a9ab5e84 CH |
934 | goto out_unlock; |
935 | ||
936 | if (mapping->nrpages) { | |
937 | result = invalidate_inode_pages2_range(mapping, | |
938 | pos >> PAGE_CACHE_SHIFT, end); | |
939 | if (result) | |
940 | goto out_unlock; | |
941 | } | |
1da177e4 | 942 | |
7ec10f26 KK |
943 | task_io_account_write(count); |
944 | ||
22cd1bf1 CH |
945 | result = -ENOMEM; |
946 | dreq = nfs_direct_req_alloc(); | |
947 | if (!dreq) | |
a9ab5e84 | 948 | goto out_unlock; |
9eafa8cc | 949 | |
22cd1bf1 CH |
950 | dreq->inode = inode; |
951 | dreq->bytes_left = count; | |
952 | dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); | |
953 | l_ctx = nfs_get_lock_context(dreq->ctx); | |
954 | if (IS_ERR(l_ctx)) { | |
955 | result = PTR_ERR(l_ctx); | |
956 | goto out_release; | |
957 | } | |
958 | dreq->l_ctx = l_ctx; | |
959 | if (!is_sync_kiocb(iocb)) | |
960 | dreq->iocb = iocb; | |
961 | ||
91f79c43 | 962 | result = nfs_direct_write_schedule_iovec(dreq, iter, pos); |
a9ab5e84 CH |
963 | |
964 | if (mapping->nrpages) { | |
965 | invalidate_inode_pages2_range(mapping, | |
966 | pos >> PAGE_CACHE_SHIFT, end); | |
967 | } | |
968 | ||
969 | mutex_unlock(&inode->i_mutex); | |
970 | ||
22cd1bf1 CH |
971 | if (!result) { |
972 | result = nfs_direct_wait(dreq); | |
973 | if (result > 0) { | |
974 | struct inode *inode = mapping->host; | |
975 | ||
976 | iocb->ki_pos = pos + result; | |
977 | spin_lock(&inode->i_lock); | |
978 | if (i_size_read(inode) < iocb->ki_pos) | |
979 | i_size_write(inode, iocb->ki_pos); | |
980 | spin_unlock(&inode->i_lock); | |
981 | } | |
1763da12 | 982 | } |
a9ab5e84 CH |
983 | nfs_direct_req_release(dreq); |
984 | return result; | |
985 | ||
22cd1bf1 CH |
986 | out_release: |
987 | nfs_direct_req_release(dreq); | |
a9ab5e84 CH |
988 | out_unlock: |
989 | mutex_unlock(&inode->i_mutex); | |
1da177e4 | 990 | out: |
22cd1bf1 | 991 | return result; |
1da177e4 LT |
992 | } |
993 | ||
88467055 CL |
994 | /** |
995 | * nfs_init_directcache - create a slab cache for nfs_direct_req structures | |
996 | * | |
997 | */ | |
f7b422b1 | 998 | int __init nfs_init_directcache(void) |
1da177e4 LT |
999 | { |
1000 | nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", | |
1001 | sizeof(struct nfs_direct_req), | |
fffb60f9 PJ |
1002 | 0, (SLAB_RECLAIM_ACCOUNT| |
1003 | SLAB_MEM_SPREAD), | |
20c2df83 | 1004 | NULL); |
1da177e4 LT |
1005 | if (nfs_direct_cachep == NULL) |
1006 | return -ENOMEM; | |
1007 | ||
1008 | return 0; | |
1009 | } | |
1010 | ||
88467055 | 1011 | /** |
f7b422b1 | 1012 | * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures |
88467055 CL |
1013 | * |
1014 | */ | |
266bee88 | 1015 | void nfs_destroy_directcache(void) |
1da177e4 | 1016 | { |
1a1d92c1 | 1017 | kmem_cache_destroy(nfs_direct_cachep); |
1da177e4 | 1018 | } |