| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * linux/fs/nfs/write.c | 
|  | 3 | * | 
|  | 4 | * Writing file data over NFS. | 
|  | 5 | * | 
|  | 6 | * We do it like this: When a (user) process wishes to write data to an | 
|  | 7 | * NFS file, a write request is allocated that contains the RPC task data | 
|  | 8 | * plus some info on the page to be written, and added to the inode's | 
|  | 9 | * write chain. If the process writes past the end of the page, an async | 
|  | 10 | * RPC call to write the page is scheduled immediately; otherwise, the call | 
|  | 11 | * is delayed for a few seconds. | 
|  | 12 | * | 
|  | 13 | * Just like readahead, no async I/O is performed if wsize < PAGE_SIZE. | 
|  | 14 | * | 
|  | 15 | * Write requests are kept on the inode's writeback list. Each entry in | 
|  | 16 | * that list references the page (portion) to be written. When the | 
|  | 17 | * cache timeout has expired, the RPC task is woken up, and tries to | 
|  | 18 | * lock the page. As soon as it manages to do so, the request is moved | 
|  | 19 | * from the writeback list to the writelock list. | 
|  | 20 | * | 
|  | 21 | * Note: we must make sure never to confuse the inode passed in the | 
|  | 22 | * write_page request with the one in page->inode. As far as I understand | 
|  | 23 | * it, these are different when doing a swap-out. | 
|  | 24 | * | 
|  | 25 | * To understand everything that goes on here and in the NFS read code, | 
|  | 26 | * one should be aware that a page is locked in exactly one of the following | 
|  | 27 | * cases: | 
|  | 28 | * | 
|  | 29 | *  -	A write request is in progress. | 
|  | 30 | *  -	A user process is in generic_file_write/nfs_update_page | 
|  | 31 | *  -	A user process is in generic_file_read | 
|  | 32 | * | 
|  | 33 | * Also note that because of the way pages are invalidated in | 
|  | 34 | * nfs_revalidate_inode, the following assertions hold: | 
|  | 35 | * | 
|  | 36 | *  -	If a page is dirty, there will be no read requests (a page will | 
|  | 37 | *	not be re-read unless invalidated by nfs_revalidate_inode). | 
|  | 38 | *  -	If the page is not uptodate, there will be no pending write | 
|  | 39 | *	requests, and no process will be in nfs_update_page. | 
|  | 40 | * | 
|  | 41 | * FIXME: Interaction with the vmscan routines is not optimal yet. | 
|  | 42 | * Either vmscan must be made nfs-savvy, or we need a different page | 
|  | 43 | * reclaim concept that supports something like FS-independent | 
|  | 44 | * buffer_heads with a b_ops-> field. | 
|  | 45 | * | 
|  | 46 | * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de> | 
|  | 47 | */ | 
|  | 48 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/types.h> | 
|  | 50 | #include <linux/slab.h> | 
|  | 51 | #include <linux/mm.h> | 
|  | 52 | #include <linux/pagemap.h> | 
|  | 53 | #include <linux/file.h> | 
|  | 54 | #include <linux/mpage.h> | 
|  | 55 | #include <linux/writeback.h> | 
|  | 56 |  | 
|  | 57 | #include <linux/sunrpc/clnt.h> | 
|  | 58 | #include <linux/nfs_fs.h> | 
|  | 59 | #include <linux/nfs_mount.h> | 
|  | 60 | #include <linux/nfs_page.h> | 
|  | 61 | #include <asm/uaccess.h> | 
|  | 62 | #include <linux/smp_lock.h> | 
|  | 63 |  | 
|  | 64 | #include "delegation.h" | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 65 | #include "iostat.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 |  | 
|  | 67 | #define NFSDBG_FACILITY		NFSDBG_PAGECACHE | 
|  | 68 |  | 
|  | 69 | #define MIN_POOL_WRITE		(32) | 
|  | 70 | #define MIN_POOL_COMMIT		(4) | 
|  | 71 |  | 
|  | 72 | /* | 
|  | 73 | * Local function declarations | 
|  | 74 | */ | 
|  | 75 | static struct nfs_page * nfs_update_request(struct nfs_open_context*, | 
|  | 76 | struct inode *, | 
|  | 77 | struct page *, | 
|  | 78 | unsigned int, unsigned int); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | static int nfs_wait_on_write_congestion(struct address_space *, int); | 
|  | 80 | static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int); | 
|  | 81 | static int nfs_flush_inode(struct inode *inode, unsigned long idx_start, | 
|  | 82 | unsigned int npages, int how); | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 83 | static const struct rpc_call_ops nfs_write_partial_ops; | 
|  | 84 | static const struct rpc_call_ops nfs_write_full_ops; | 
|  | 85 | static const struct rpc_call_ops nfs_commit_ops; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 |  | 
|  | 87 | static kmem_cache_t *nfs_wdata_cachep; | 
| Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 88 | static mempool_t *nfs_wdata_mempool; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | static mempool_t *nfs_commit_mempool; | 
|  | 90 |  | 
|  | 91 | static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion); | 
|  | 92 |  | 
| Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 93 | struct nfs_write_data *nfs_commit_alloc(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { | 
|  | 95 | struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS); | 
| Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 96 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (p) { | 
|  | 98 | memset(p, 0, sizeof(*p)); | 
|  | 99 | INIT_LIST_HEAD(&p->pages); | 
|  | 100 | } | 
|  | 101 | return p; | 
|  | 102 | } | 
|  | 103 |  | 
| Trond Myklebust | e17b1fc | 2006-03-20 13:44:35 -0500 | [diff] [blame] | 104 | void nfs_commit_free(struct nfs_write_data *p) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { | 
| Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 106 | if (p && (p->pagevec != &p->page_array[0])) | 
|  | 107 | kfree(p->pagevec); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | mempool_free(p, nfs_commit_mempool); | 
|  | 109 | } | 
|  | 110 |  | 
| Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 111 | struct nfs_write_data *nfs_writedata_alloc(size_t len) | 
| Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 112 | { | 
| Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 113 | unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; | 
| Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 114 | struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS); | 
|  | 115 |  | 
|  | 116 | if (p) { | 
|  | 117 | memset(p, 0, sizeof(*p)); | 
|  | 118 | INIT_LIST_HEAD(&p->pages); | 
| Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 119 | p->npages = pagecount; | 
| Chuck Lever | 0d0b5cb | 2006-05-25 01:40:53 -0400 | [diff] [blame] | 120 | if (pagecount <= ARRAY_SIZE(p->page_array)) | 
|  | 121 | p->pagevec = p->page_array; | 
| Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 122 | else { | 
| Chuck Lever | 0d0b5cb | 2006-05-25 01:40:53 -0400 | [diff] [blame] | 123 | p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); | 
|  | 124 | if (!p->pagevec) { | 
| Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 125 | mempool_free(p, nfs_wdata_mempool); | 
|  | 126 | p = NULL; | 
|  | 127 | } | 
|  | 128 | } | 
|  | 129 | } | 
|  | 130 | return p; | 
|  | 131 | } | 
|  | 132 |  | 
| Adrian Bunk | e4e2051 | 2006-08-03 15:07:47 -0400 | [diff] [blame] | 133 | static void nfs_writedata_free(struct nfs_write_data *p) | 
| Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 134 | { | 
|  | 135 | if (p && (p->pagevec != &p->page_array[0])) | 
|  | 136 | kfree(p->pagevec); | 
|  | 137 | mempool_free(p, nfs_wdata_mempool); | 
|  | 138 | } | 
|  | 139 |  | 
| Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 140 | void nfs_writedata_release(void *wdata) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | nfs_writedata_free(wdata); | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | /* Adjust the file length if we're writing beyond the end */ | 
|  | 146 | static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) | 
|  | 147 | { | 
|  | 148 | struct inode *inode = page->mapping->host; | 
|  | 149 | loff_t end, i_size = i_size_read(inode); | 
|  | 150 | unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; | 
|  | 151 |  | 
|  | 152 | if (i_size > 0 && page->index < end_index) | 
|  | 153 | return; | 
|  | 154 | end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); | 
|  | 155 | if (i_size >= end) | 
|  | 156 | return; | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 157 | nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | i_size_write(inode, end); | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | /* We can set the PG_uptodate flag if we see that a write request | 
|  | 162 | * covers the full page. | 
|  | 163 | */ | 
|  | 164 | static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) | 
|  | 165 | { | 
|  | 166 | loff_t end_offs; | 
|  | 167 |  | 
|  | 168 | if (PageUptodate(page)) | 
|  | 169 | return; | 
|  | 170 | if (base != 0) | 
|  | 171 | return; | 
|  | 172 | if (count == PAGE_CACHE_SIZE) { | 
|  | 173 | SetPageUptodate(page); | 
|  | 174 | return; | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | end_offs = i_size_read(page->mapping->host) - 1; | 
|  | 178 | if (end_offs < 0) | 
|  | 179 | return; | 
|  | 180 | /* Is this the last page? */ | 
|  | 181 | if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT)) | 
|  | 182 | return; | 
|  | 183 | /* This is the last page: set PG_uptodate if we cover the entire | 
|  | 184 | * extent of the data, then zero the rest of the page. | 
|  | 185 | */ | 
|  | 186 | if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) { | 
|  | 187 | memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count); | 
|  | 188 | SetPageUptodate(page); | 
|  | 189 | } | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | /* | 
|  | 193 | * Write a page synchronously. | 
|  | 194 | * Offset is the data offset within the page. | 
|  | 195 | */ | 
|  | 196 | static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode, | 
|  | 197 | struct page *page, unsigned int offset, unsigned int count, | 
|  | 198 | int how) | 
|  | 199 | { | 
|  | 200 | unsigned int	wsize = NFS_SERVER(inode)->wsize; | 
|  | 201 | int		result, written = 0; | 
|  | 202 | struct nfs_write_data *wdata; | 
|  | 203 |  | 
| Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 204 | wdata = nfs_writedata_alloc(wsize); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | if (!wdata) | 
|  | 206 | return -ENOMEM; | 
|  | 207 |  | 
|  | 208 | wdata->flags = how; | 
|  | 209 | wdata->cred = ctx->cred; | 
|  | 210 | wdata->inode = inode; | 
|  | 211 | wdata->args.fh = NFS_FH(inode); | 
|  | 212 | wdata->args.context = ctx; | 
|  | 213 | wdata->args.pages = &page; | 
|  | 214 | wdata->args.stable = NFS_FILE_SYNC; | 
|  | 215 | wdata->args.pgbase = offset; | 
|  | 216 | wdata->args.count = wsize; | 
|  | 217 | wdata->res.fattr = &wdata->fattr; | 
|  | 218 | wdata->res.verf = &wdata->verf; | 
|  | 219 |  | 
|  | 220 | dprintk("NFS:      nfs_writepage_sync(%s/%Ld %d@%Ld)\n", | 
|  | 221 | inode->i_sb->s_id, | 
|  | 222 | (long long)NFS_FILEID(inode), | 
|  | 223 | count, (long long)(page_offset(page) + offset)); | 
|  | 224 |  | 
| Trond Myklebust | bb713d6 | 2005-12-03 15:20:14 -0500 | [diff] [blame] | 225 | set_page_writeback(page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | nfs_begin_data_update(inode); | 
|  | 227 | do { | 
|  | 228 | if (count < wsize) | 
|  | 229 | wdata->args.count = count; | 
|  | 230 | wdata->args.offset = page_offset(page) + wdata->args.pgbase; | 
|  | 231 |  | 
|  | 232 | result = NFS_PROTO(inode)->write(wdata); | 
|  | 233 |  | 
|  | 234 | if (result < 0) { | 
|  | 235 | /* Must mark the page invalid after I/O error */ | 
|  | 236 | ClearPageUptodate(page); | 
|  | 237 | goto io_error; | 
|  | 238 | } | 
|  | 239 | if (result < wdata->args.count) | 
|  | 240 | printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n", | 
|  | 241 | wdata->args.count, result); | 
|  | 242 |  | 
|  | 243 | wdata->args.offset += result; | 
|  | 244 | wdata->args.pgbase += result; | 
|  | 245 | written += result; | 
|  | 246 | count -= result; | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 247 | nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } while (count); | 
|  | 249 | /* Update file length */ | 
|  | 250 | nfs_grow_file(page, offset, written); | 
|  | 251 | /* Set the PG_uptodate flag? */ | 
|  | 252 | nfs_mark_uptodate(page, offset, written); | 
|  | 253 |  | 
|  | 254 | if (PageError(page)) | 
|  | 255 | ClearPageError(page); | 
|  | 256 |  | 
|  | 257 | io_error: | 
| Trond Myklebust | 951a143 | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 258 | nfs_end_data_update(inode); | 
| Trond Myklebust | bb713d6 | 2005-12-03 15:20:14 -0500 | [diff] [blame] | 259 | end_page_writeback(page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | nfs_writedata_free(wdata); | 
|  | 261 | return written ? written : result; | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | static int nfs_writepage_async(struct nfs_open_context *ctx, | 
|  | 265 | struct inode *inode, struct page *page, | 
|  | 266 | unsigned int offset, unsigned int count) | 
|  | 267 | { | 
|  | 268 | struct nfs_page	*req; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 |  | 
|  | 270 | req = nfs_update_request(ctx, inode, page, offset, count); | 
| Trond Myklebust | abd3e64 | 2006-01-03 09:55:02 +0100 | [diff] [blame] | 271 | if (IS_ERR(req)) | 
|  | 272 | return PTR_ERR(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | /* Update file length */ | 
|  | 274 | nfs_grow_file(page, offset, count); | 
|  | 275 | /* Set the PG_uptodate flag? */ | 
|  | 276 | nfs_mark_uptodate(page, offset, count); | 
|  | 277 | nfs_unlock_request(req); | 
| Trond Myklebust | abd3e64 | 2006-01-03 09:55:02 +0100 | [diff] [blame] | 278 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } | 
|  | 280 |  | 
|  | 281 | static int wb_priority(struct writeback_control *wbc) | 
|  | 282 | { | 
|  | 283 | if (wbc->for_reclaim) | 
|  | 284 | return FLUSH_HIGHPRI; | 
|  | 285 | if (wbc->for_kupdate) | 
|  | 286 | return FLUSH_LOWPRI; | 
|  | 287 | return 0; | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | /* | 
|  | 291 | * Write an mmapped page to the server. | 
|  | 292 | */ | 
|  | 293 | int nfs_writepage(struct page *page, struct writeback_control *wbc) | 
|  | 294 | { | 
|  | 295 | struct nfs_open_context *ctx; | 
|  | 296 | struct inode *inode = page->mapping->host; | 
|  | 297 | unsigned long end_index; | 
|  | 298 | unsigned offset = PAGE_CACHE_SIZE; | 
|  | 299 | loff_t i_size = i_size_read(inode); | 
|  | 300 | int inode_referenced = 0; | 
|  | 301 | int priority = wb_priority(wbc); | 
|  | 302 | int err; | 
|  | 303 |  | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 304 | nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); | 
|  | 305 | nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); | 
|  | 306 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | /* | 
|  | 308 | * Note: We need to ensure that we have a reference to the inode | 
|  | 309 | *       if we are to do asynchronous writes. If not, waiting | 
|  | 310 | *       in nfs_wait_on_request() may deadlock with clear_inode(). | 
|  | 311 | * | 
|  | 312 | *       If igrab() fails here, then it is in any case safe to | 
|  | 313 | *       call nfs_wb_page(), since there will be no pending writes. | 
|  | 314 | */ | 
|  | 315 | if (igrab(inode) != 0) | 
|  | 316 | inode_referenced = 1; | 
|  | 317 | end_index = i_size >> PAGE_CACHE_SHIFT; | 
|  | 318 |  | 
|  | 319 | /* Ensure we've flushed out any previous writes */ | 
|  | 320 | nfs_wb_page_priority(inode, page, priority); | 
|  | 321 |  | 
|  | 322 | /* easy case */ | 
|  | 323 | if (page->index < end_index) | 
|  | 324 | goto do_it; | 
|  | 325 | /* things got complicated... */ | 
|  | 326 | offset = i_size & (PAGE_CACHE_SIZE-1); | 
|  | 327 |  | 
|  | 328 | /* OK, are we completely out? */ | 
|  | 329 | err = 0; /* potential race with truncate - ignore */ | 
|  | 330 | if (page->index >= end_index+1 || !offset) | 
|  | 331 | goto out; | 
|  | 332 | do_it: | 
| Trond Myklebust | d530838 | 2005-11-04 15:33:38 -0500 | [diff] [blame] | 333 | ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | if (ctx == NULL) { | 
|  | 335 | err = -EBADF; | 
|  | 336 | goto out; | 
|  | 337 | } | 
|  | 338 | lock_kernel(); | 
|  | 339 | if (!IS_SYNC(inode) && inode_referenced) { | 
|  | 340 | err = nfs_writepage_async(ctx, inode, page, 0, offset); | 
| Trond Myklebust | abd3e64 | 2006-01-03 09:55:02 +0100 | [diff] [blame] | 341 | if (!wbc->for_writepages) | 
|  | 342 | nfs_flush_inode(inode, 0, 0, wb_priority(wbc)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } else { | 
|  | 344 | err = nfs_writepage_sync(ctx, inode, page, 0, | 
|  | 345 | offset, priority); | 
|  | 346 | if (err >= 0) { | 
|  | 347 | if (err != offset) | 
|  | 348 | redirty_page_for_writepage(wbc, page); | 
|  | 349 | err = 0; | 
|  | 350 | } | 
|  | 351 | } | 
|  | 352 | unlock_kernel(); | 
|  | 353 | put_nfs_open_context(ctx); | 
|  | 354 | out: | 
|  | 355 | unlock_page(page); | 
|  | 356 | if (inode_referenced) | 
|  | 357 | iput(inode); | 
|  | 358 | return err; | 
|  | 359 | } | 
|  | 360 |  | 
|  | 361 | /* | 
|  | 362 | * Note: causes nfs_update_request() to block on the assumption | 
|  | 363 | * 	 that the writeback is generated due to memory pressure. | 
|  | 364 | */ | 
|  | 365 | int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) | 
|  | 366 | { | 
|  | 367 | struct backing_dev_info *bdi = mapping->backing_dev_info; | 
|  | 368 | struct inode *inode = mapping->host; | 
|  | 369 | int err; | 
|  | 370 |  | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 371 | nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); | 
|  | 372 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | err = generic_writepages(mapping, wbc); | 
|  | 374 | if (err) | 
|  | 375 | return err; | 
|  | 376 | while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) { | 
|  | 377 | if (wbc->nonblocking) | 
|  | 378 | return 0; | 
|  | 379 | nfs_wait_on_write_congestion(mapping, 0); | 
|  | 380 | } | 
|  | 381 | err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc)); | 
|  | 382 | if (err < 0) | 
|  | 383 | goto out; | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 384 | nfs_add_stats(inode, NFSIOS_WRITEPAGES, err); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | wbc->nr_to_write -= err; | 
|  | 386 | if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) { | 
|  | 387 | err = nfs_wait_on_requests(inode, 0, 0); | 
|  | 388 | if (err < 0) | 
|  | 389 | goto out; | 
|  | 390 | } | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 391 | err = nfs_commit_inode(inode, wb_priority(wbc)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | if (err > 0) { | 
|  | 393 | wbc->nr_to_write -= err; | 
|  | 394 | err = 0; | 
|  | 395 | } | 
|  | 396 | out: | 
|  | 397 | clear_bit(BDI_write_congested, &bdi->state); | 
|  | 398 | wake_up_all(&nfs_write_congestion); | 
| Trond Myklebust | 275a082 | 2006-08-22 20:06:24 -0400 | [diff] [blame] | 399 | writeback_congestion_end(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | return err; | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | /* | 
|  | 404 | * Insert a write request into an inode | 
|  | 405 | */ | 
|  | 406 | static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req) | 
|  | 407 | { | 
|  | 408 | struct nfs_inode *nfsi = NFS_I(inode); | 
|  | 409 | int error; | 
|  | 410 |  | 
|  | 411 | error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req); | 
|  | 412 | BUG_ON(error == -EEXIST); | 
|  | 413 | if (error) | 
|  | 414 | return error; | 
|  | 415 | if (!nfsi->npages) { | 
|  | 416 | igrab(inode); | 
|  | 417 | nfs_begin_data_update(inode); | 
|  | 418 | if (nfs_have_delegation(inode, FMODE_WRITE)) | 
|  | 419 | nfsi->change_attr++; | 
|  | 420 | } | 
| Trond Myklebust | deb7d63 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 421 | SetPagePrivate(req->wb_page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | nfsi->npages++; | 
|  | 423 | atomic_inc(&req->wb_count); | 
|  | 424 | return 0; | 
|  | 425 | } | 
|  | 426 |  | 
|  | 427 | /* | 
|  | 428 | * Insert a write request into an inode | 
|  | 429 | */ | 
|  | 430 | static void nfs_inode_remove_request(struct nfs_page *req) | 
|  | 431 | { | 
|  | 432 | struct inode *inode = req->wb_context->dentry->d_inode; | 
|  | 433 | struct nfs_inode *nfsi = NFS_I(inode); | 
|  | 434 |  | 
|  | 435 | BUG_ON (!NFS_WBACK_BUSY(req)); | 
|  | 436 |  | 
|  | 437 | spin_lock(&nfsi->req_lock); | 
| Trond Myklebust | deb7d63 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 438 | ClearPagePrivate(req->wb_page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index); | 
|  | 440 | nfsi->npages--; | 
|  | 441 | if (!nfsi->npages) { | 
|  | 442 | spin_unlock(&nfsi->req_lock); | 
| Trond Myklebust | 951a143 | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 443 | nfs_end_data_update(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | iput(inode); | 
|  | 445 | } else | 
|  | 446 | spin_unlock(&nfsi->req_lock); | 
|  | 447 | nfs_clear_request(req); | 
|  | 448 | nfs_release_request(req); | 
|  | 449 | } | 
|  | 450 |  | 
|  | 451 | /* | 
|  | 452 | * Find a request | 
|  | 453 | */ | 
|  | 454 | static inline struct nfs_page * | 
|  | 455 | _nfs_find_request(struct inode *inode, unsigned long index) | 
|  | 456 | { | 
|  | 457 | struct nfs_inode *nfsi = NFS_I(inode); | 
|  | 458 | struct nfs_page *req; | 
|  | 459 |  | 
|  | 460 | req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index); | 
|  | 461 | if (req) | 
|  | 462 | atomic_inc(&req->wb_count); | 
|  | 463 | return req; | 
|  | 464 | } | 
|  | 465 |  | 
|  | 466 | static struct nfs_page * | 
|  | 467 | nfs_find_request(struct inode *inode, unsigned long index) | 
|  | 468 | { | 
|  | 469 | struct nfs_page		*req; | 
|  | 470 | struct nfs_inode	*nfsi = NFS_I(inode); | 
|  | 471 |  | 
|  | 472 | spin_lock(&nfsi->req_lock); | 
|  | 473 | req = _nfs_find_request(inode, index); | 
|  | 474 | spin_unlock(&nfsi->req_lock); | 
|  | 475 | return req; | 
|  | 476 | } | 
|  | 477 |  | 
|  | 478 | /* | 
|  | 479 | * Add a request to the inode's dirty list. | 
|  | 480 | */ | 
|  | 481 | static void | 
|  | 482 | nfs_mark_request_dirty(struct nfs_page *req) | 
|  | 483 | { | 
|  | 484 | struct inode *inode = req->wb_context->dentry->d_inode; | 
|  | 485 | struct nfs_inode *nfsi = NFS_I(inode); | 
|  | 486 |  | 
|  | 487 | spin_lock(&nfsi->req_lock); | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 488 | radix_tree_tag_set(&nfsi->nfs_page_tree, | 
|  | 489 | req->wb_index, NFS_PAGE_TAG_DIRTY); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | nfs_list_add_request(req, &nfsi->dirty); | 
|  | 491 | nfsi->ndirty++; | 
|  | 492 | spin_unlock(&nfsi->req_lock); | 
| Christoph Lameter | b1e7a8f | 2006-06-30 01:55:39 -0700 | [diff] [blame] | 493 | inc_zone_page_state(req->wb_page, NR_FILE_DIRTY); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | mark_inode_dirty(inode); | 
|  | 495 | } | 
|  | 496 |  | 
|  | 497 | /* | 
|  | 498 | * Check if a request is dirty | 
|  | 499 | */ | 
|  | 500 | static inline int | 
|  | 501 | nfs_dirty_request(struct nfs_page *req) | 
|  | 502 | { | 
|  | 503 | struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode); | 
|  | 504 | return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty; | 
|  | 505 | } | 
|  | 506 |  | 
|  | 507 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | 
|  | 508 | /* | 
|  | 509 | * Add a request to the inode's commit list. | 
|  | 510 | */ | 
|  | 511 | static void | 
|  | 512 | nfs_mark_request_commit(struct nfs_page *req) | 
|  | 513 | { | 
|  | 514 | struct inode *inode = req->wb_context->dentry->d_inode; | 
|  | 515 | struct nfs_inode *nfsi = NFS_I(inode); | 
|  | 516 |  | 
|  | 517 | spin_lock(&nfsi->req_lock); | 
|  | 518 | nfs_list_add_request(req, &nfsi->commit); | 
|  | 519 | nfsi->ncommit++; | 
|  | 520 | spin_unlock(&nfsi->req_lock); | 
| Christoph Lameter | fd39fc8 | 2006-06-30 01:55:40 -0700 | [diff] [blame] | 521 | inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | mark_inode_dirty(inode); | 
|  | 523 | } | 
|  | 524 | #endif | 
|  | 525 |  | 
|  | 526 | /* | 
|  | 527 | * Wait for a request to complete. | 
|  | 528 | * | 
|  | 529 | * Interruptible by signals only if mounted with intr flag. | 
|  | 530 | */ | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 531 | static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { | 
|  | 533 | struct nfs_inode *nfsi = NFS_I(inode); | 
|  | 534 | struct nfs_page *req; | 
|  | 535 | unsigned long		idx_end, next; | 
|  | 536 | unsigned int		res = 0; | 
|  | 537 | int			error; | 
|  | 538 |  | 
|  | 539 | if (npages == 0) | 
|  | 540 | idx_end = ~0; | 
|  | 541 | else | 
|  | 542 | idx_end = idx_start + npages - 1; | 
|  | 543 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | next = idx_start; | 
| Trond Myklebust | c6a556b | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 545 | while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | if (req->wb_index > idx_end) | 
|  | 547 | break; | 
|  | 548 |  | 
|  | 549 | next = req->wb_index + 1; | 
| Trond Myklebust | c6a556b | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 550 | BUG_ON(!NFS_WBACK_BUSY(req)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 |  | 
|  | 552 | atomic_inc(&req->wb_count); | 
|  | 553 | spin_unlock(&nfsi->req_lock); | 
|  | 554 | error = nfs_wait_on_request(req); | 
|  | 555 | nfs_release_request(req); | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 556 | spin_lock(&nfsi->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (error < 0) | 
|  | 558 | return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | res++; | 
|  | 560 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | return res; | 
|  | 562 | } | 
|  | 563 |  | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 564 | static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages) | 
|  | 565 | { | 
|  | 566 | struct nfs_inode *nfsi = NFS_I(inode); | 
|  | 567 | int ret; | 
|  | 568 |  | 
|  | 569 | spin_lock(&nfsi->req_lock); | 
|  | 570 | ret = nfs_wait_on_requests_locked(inode, idx_start, npages); | 
|  | 571 | spin_unlock(&nfsi->req_lock); | 
|  | 572 | return ret; | 
|  | 573 | } | 
|  | 574 |  | 
| Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 575 | static void nfs_cancel_dirty_list(struct list_head *head) | 
| Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 576 | { | 
|  | 577 | struct nfs_page *req; | 
|  | 578 | while(!list_empty(head)) { | 
|  | 579 | req = nfs_list_entry(head->next); | 
|  | 580 | nfs_list_remove_request(req); | 
|  | 581 | nfs_inode_remove_request(req); | 
|  | 582 | nfs_clear_page_writeback(req); | 
|  | 583 | } | 
|  | 584 | } | 
|  | 585 |  | 
| Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 586 | static void nfs_cancel_commit_list(struct list_head *head) | 
|  | 587 | { | 
|  | 588 | struct nfs_page *req; | 
|  | 589 |  | 
|  | 590 | while(!list_empty(head)) { | 
|  | 591 | req = nfs_list_entry(head->next); | 
|  | 592 | nfs_list_remove_request(req); | 
|  | 593 | nfs_inode_remove_request(req); | 
| Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 594 | dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); | 
| Trond Myklebust | 5c2d97c | 2006-09-18 23:20:35 -0400 | [diff] [blame] | 595 | nfs_clear_page_writeback(req); | 
| Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 596 | } | 
|  | 597 | } | 
|  | 598 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | /* | 
|  | 600 | * nfs_scan_dirty - Scan an inode for dirty requests | 
|  | 601 | * @inode: NFS inode to scan | 
|  | 602 | * @dst: destination list | 
|  | 603 | * @idx_start: lower bound of page->index to scan. | 
|  | 604 | * @npages: idx_start + npages sets the upper bound to scan. | 
|  | 605 | * | 
|  | 606 | * Moves requests from the inode's dirty page list. | 
|  | 607 | * The requests are *not* checked to ensure that they form a contiguous set. | 
|  | 608 | */ | 
|  | 609 | static int | 
|  | 610 | nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages) | 
|  | 611 | { | 
|  | 612 | struct nfs_inode *nfsi = NFS_I(inode); | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 613 | int res = 0; | 
|  | 614 |  | 
|  | 615 | if (nfsi->ndirty != 0) { | 
|  | 616 | res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages); | 
|  | 617 | nfsi->ndirty -= res; | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 618 | if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty)) | 
|  | 619 | printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n"); | 
|  | 620 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | return res; | 
|  | 622 | } | 
|  | 623 |  | 
|  | 624 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | 
|  | 625 | /* | 
|  | 626 | * nfs_scan_commit - Scan an inode for commit requests | 
|  | 627 | * @inode: NFS inode to scan | 
|  | 628 | * @dst: destination list | 
|  | 629 | * @idx_start: lower bound of page->index to scan. | 
|  | 630 | * @npages: idx_start + npages sets the upper bound to scan. | 
|  | 631 | * | 
|  | 632 | * Moves requests from the inode's 'commit' request list. | 
|  | 633 | * The requests are *not* checked to ensure that they form a contiguous set. | 
|  | 634 | */ | 
|  | 635 | static int | 
|  | 636 | nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages) | 
|  | 637 | { | 
|  | 638 | struct nfs_inode *nfsi = NFS_I(inode); | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 639 | int res = 0; | 
|  | 640 |  | 
|  | 641 | if (nfsi->ncommit != 0) { | 
| Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 642 | res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages); | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 643 | nfsi->ncommit -= res; | 
|  | 644 | if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit)) | 
|  | 645 | printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n"); | 
|  | 646 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | return res; | 
|  | 648 | } | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 649 | #else | 
|  | 650 | static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages) | 
|  | 651 | { | 
|  | 652 | return 0; | 
|  | 653 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | #endif | 
|  | 655 |  | 
|  | 656 | static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr) | 
|  | 657 | { | 
|  | 658 | struct backing_dev_info *bdi = mapping->backing_dev_info; | 
|  | 659 | DEFINE_WAIT(wait); | 
|  | 660 | int ret = 0; | 
|  | 661 |  | 
|  | 662 | might_sleep(); | 
|  | 663 |  | 
|  | 664 | if (!bdi_write_congested(bdi)) | 
|  | 665 | return 0; | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 666 |  | 
|  | 667 | nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT); | 
|  | 668 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | if (intr) { | 
|  | 670 | struct rpc_clnt *clnt = NFS_CLIENT(mapping->host); | 
|  | 671 | sigset_t oldset; | 
|  | 672 |  | 
|  | 673 | rpc_clnt_sigmask(clnt, &oldset); | 
|  | 674 | prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE); | 
|  | 675 | if (bdi_write_congested(bdi)) { | 
|  | 676 | if (signalled()) | 
|  | 677 | ret = -ERESTARTSYS; | 
|  | 678 | else | 
|  | 679 | schedule(); | 
|  | 680 | } | 
|  | 681 | rpc_clnt_sigunmask(clnt, &oldset); | 
|  | 682 | } else { | 
|  | 683 | prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE); | 
|  | 684 | if (bdi_write_congested(bdi)) | 
|  | 685 | schedule(); | 
|  | 686 | } | 
|  | 687 | finish_wait(&nfs_write_congestion, &wait); | 
|  | 688 | return ret; | 
|  | 689 | } | 
|  | 690 |  | 
|  | 691 |  | 
|  | 692 | /* | 
|  | 693 | * Try to update any existing write request, or create one if there is none. | 
|  | 694 | * In order to match, the request's credentials must match those of | 
|  | 695 | * the calling process. | 
|  | 696 | * | 
|  | 697 | * Note: Should always be called with the Page Lock held! | 
|  | 698 | */ | 
|  | 699 | static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx, | 
|  | 700 | struct inode *inode, struct page *page, | 
|  | 701 | unsigned int offset, unsigned int bytes) | 
|  | 702 | { | 
|  | 703 | struct nfs_server *server = NFS_SERVER(inode); | 
|  | 704 | struct nfs_inode *nfsi = NFS_I(inode); | 
|  | 705 | struct nfs_page		*req, *new = NULL; | 
|  | 706 | unsigned long		rqend, end; | 
|  | 707 |  | 
|  | 708 | end = offset + bytes; | 
|  | 709 |  | 
|  | 710 | if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR)) | 
|  | 711 | return ERR_PTR(-ERESTARTSYS); | 
|  | 712 | for (;;) { | 
|  | 713 | /* Loop over all inode entries and see if we find | 
|  | 714 | * A request for the page we wish to update | 
|  | 715 | */ | 
|  | 716 | spin_lock(&nfsi->req_lock); | 
|  | 717 | req = _nfs_find_request(inode, page->index); | 
|  | 718 | if (req) { | 
|  | 719 | if (!nfs_lock_request_dontget(req)) { | 
|  | 720 | int error; | 
|  | 721 | spin_unlock(&nfsi->req_lock); | 
|  | 722 | error = nfs_wait_on_request(req); | 
|  | 723 | nfs_release_request(req); | 
| Neil Brown | 1dd594b | 2006-03-20 13:44:04 -0500 | [diff] [blame] | 724 | if (error < 0) { | 
|  | 725 | if (new) | 
|  | 726 | nfs_release_request(new); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | return ERR_PTR(error); | 
| Neil Brown | 1dd594b | 2006-03-20 13:44:04 -0500 | [diff] [blame] | 728 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | continue; | 
|  | 730 | } | 
|  | 731 | spin_unlock(&nfsi->req_lock); | 
|  | 732 | if (new) | 
|  | 733 | nfs_release_request(new); | 
|  | 734 | break; | 
|  | 735 | } | 
|  | 736 |  | 
|  | 737 | if (new) { | 
|  | 738 | int error; | 
|  | 739 | nfs_lock_request_dontget(new); | 
|  | 740 | error = nfs_inode_add_request(inode, new); | 
|  | 741 | if (error) { | 
|  | 742 | spin_unlock(&nfsi->req_lock); | 
|  | 743 | nfs_unlock_request(new); | 
|  | 744 | return ERR_PTR(error); | 
|  | 745 | } | 
|  | 746 | spin_unlock(&nfsi->req_lock); | 
|  | 747 | nfs_mark_request_dirty(new); | 
|  | 748 | return new; | 
|  | 749 | } | 
|  | 750 | spin_unlock(&nfsi->req_lock); | 
|  | 751 |  | 
|  | 752 | new = nfs_create_request(ctx, inode, page, offset, bytes); | 
|  | 753 | if (IS_ERR(new)) | 
|  | 754 | return new; | 
|  | 755 | } | 
|  | 756 |  | 
|  | 757 | /* We have a request for our page. | 
|  | 758 | * If the creds don't match, or the | 
|  | 759 | * page addresses don't match, | 
|  | 760 | * tell the caller to wait on the conflicting | 
|  | 761 | * request. | 
|  | 762 | */ | 
|  | 763 | rqend = req->wb_offset + req->wb_bytes; | 
|  | 764 | if (req->wb_context != ctx | 
|  | 765 | || req->wb_page != page | 
|  | 766 | || !nfs_dirty_request(req) | 
|  | 767 | || offset > rqend || end < req->wb_offset) { | 
|  | 768 | nfs_unlock_request(req); | 
|  | 769 | return ERR_PTR(-EBUSY); | 
|  | 770 | } | 
|  | 771 |  | 
|  | 772 | /* Okay, the request matches. Update the region */ | 
|  | 773 | if (offset < req->wb_offset) { | 
|  | 774 | req->wb_offset = offset; | 
|  | 775 | req->wb_pgbase = offset; | 
|  | 776 | req->wb_bytes = rqend - req->wb_offset; | 
|  | 777 | } | 
|  | 778 |  | 
|  | 779 | if (end > rqend) | 
|  | 780 | req->wb_bytes = end - req->wb_offset; | 
|  | 781 |  | 
|  | 782 | return req; | 
|  | 783 | } | 
|  | 784 |  | 
|  | 785 | int nfs_flush_incompatible(struct file *file, struct page *page) | 
|  | 786 | { | 
|  | 787 | struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data; | 
|  | 788 | struct inode	*inode = page->mapping->host; | 
|  | 789 | struct nfs_page	*req; | 
|  | 790 | int		status = 0; | 
|  | 791 | /* | 
|  | 792 | * Look for a request corresponding to this page. If there | 
|  | 793 | * is one, and it belongs to another file, we flush it out | 
|  | 794 | * before we try to copy anything into the page. Do this | 
|  | 795 | * due to the lack of an ACCESS-type call in NFSv2. | 
|  | 796 | * Also do the same if we find a request from an existing | 
|  | 797 | * dropped page. | 
|  | 798 | */ | 
|  | 799 | req = nfs_find_request(inode, page->index); | 
|  | 800 | if (req) { | 
|  | 801 | if (req->wb_page != page || ctx != req->wb_context) | 
|  | 802 | status = nfs_wb_page(inode, page); | 
|  | 803 | nfs_release_request(req); | 
|  | 804 | } | 
|  | 805 | return (status < 0) ? status : 0; | 
|  | 806 | } | 
|  | 807 |  | 
|  | 808 | /* | 
|  | 809 | * Update and possibly write a cached page of an NFS file. | 
|  | 810 | * | 
|  | 811 | * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad | 
|  | 812 | * things with a page scheduled for an RPC call (e.g. invalidate it). | 
|  | 813 | */ | 
|  | 814 | int nfs_updatepage(struct file *file, struct page *page, | 
|  | 815 | unsigned int offset, unsigned int count) | 
|  | 816 | { | 
|  | 817 | struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | struct inode	*inode = page->mapping->host; | 
|  | 819 | struct nfs_page	*req; | 
|  | 820 | int		status = 0; | 
|  | 821 |  | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 822 | nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); | 
|  | 823 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | dprintk("NFS:      nfs_updatepage(%s/%s %d@%Ld)\n", | 
| Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 825 | file->f_dentry->d_parent->d_name.name, | 
|  | 826 | file->f_dentry->d_name.name, count, | 
|  | 827 | (long long)(page_offset(page) +offset)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 |  | 
|  | 829 | if (IS_SYNC(inode)) { | 
|  | 830 | status = nfs_writepage_sync(ctx, inode, page, offset, count, 0); | 
|  | 831 | if (status > 0) { | 
|  | 832 | if (offset == 0 && status == PAGE_CACHE_SIZE) | 
|  | 833 | SetPageUptodate(page); | 
|  | 834 | return 0; | 
|  | 835 | } | 
|  | 836 | return status; | 
|  | 837 | } | 
|  | 838 |  | 
|  | 839 | /* If we're not using byte range locks, and we know the page | 
|  | 840 | * is entirely in cache, it may be more efficient to avoid | 
|  | 841 | * fragmenting write requests. | 
|  | 842 | */ | 
| Trond Myklebust | ab0a3db | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 843 | if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | loff_t end_offs = i_size_read(inode) - 1; | 
|  | 845 | unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT; | 
|  | 846 |  | 
|  | 847 | count += offset; | 
|  | 848 | offset = 0; | 
|  | 849 | if (unlikely(end_offs < 0)) { | 
|  | 850 | /* Do nothing */ | 
|  | 851 | } else if (page->index == end_index) { | 
|  | 852 | unsigned int pglen; | 
|  | 853 | pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1; | 
|  | 854 | if (count < pglen) | 
|  | 855 | count = pglen; | 
|  | 856 | } else if (page->index < end_index) | 
|  | 857 | count = PAGE_CACHE_SIZE; | 
|  | 858 | } | 
|  | 859 |  | 
|  | 860 | /* | 
|  | 861 | * Try to find an NFS request corresponding to this page | 
|  | 862 | * and update it. | 
|  | 863 | * If the existing request cannot be updated, we must flush | 
|  | 864 | * it out now. | 
|  | 865 | */ | 
|  | 866 | do { | 
|  | 867 | req = nfs_update_request(ctx, inode, page, offset, count); | 
|  | 868 | status = (IS_ERR(req)) ? PTR_ERR(req) : 0; | 
|  | 869 | if (status != -EBUSY) | 
|  | 870 | break; | 
|  | 871 | /* Request could not be updated. Flush it out and try again */ | 
|  | 872 | status = nfs_wb_page(inode, page); | 
|  | 873 | } while (status >= 0); | 
|  | 874 | if (status < 0) | 
|  | 875 | goto done; | 
|  | 876 |  | 
|  | 877 | status = 0; | 
|  | 878 |  | 
|  | 879 | /* Update file length */ | 
|  | 880 | nfs_grow_file(page, offset, count); | 
|  | 881 | /* Set the PG_uptodate flag? */ | 
|  | 882 | nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); | 
|  | 883 | nfs_unlock_request(req); | 
|  | 884 | done: | 
|  | 885 | dprintk("NFS:      nfs_updatepage returns %d (isize %Ld)\n", | 
|  | 886 | status, (long long)i_size_read(inode)); | 
|  | 887 | if (status < 0) | 
|  | 888 | ClearPageUptodate(page); | 
|  | 889 | return status; | 
|  | 890 | } | 
|  | 891 |  | 
|  | 892 | static void nfs_writepage_release(struct nfs_page *req) | 
|  | 893 | { | 
|  | 894 | end_page_writeback(req->wb_page); | 
|  | 895 |  | 
|  | 896 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | 
|  | 897 | if (!PageError(req->wb_page)) { | 
|  | 898 | if (NFS_NEED_RESCHED(req)) { | 
|  | 899 | nfs_mark_request_dirty(req); | 
|  | 900 | goto out; | 
|  | 901 | } else if (NFS_NEED_COMMIT(req)) { | 
|  | 902 | nfs_mark_request_commit(req); | 
|  | 903 | goto out; | 
|  | 904 | } | 
|  | 905 | } | 
|  | 906 | nfs_inode_remove_request(req); | 
|  | 907 |  | 
|  | 908 | out: | 
|  | 909 | nfs_clear_commit(req); | 
|  | 910 | nfs_clear_reschedule(req); | 
|  | 911 | #else | 
|  | 912 | nfs_inode_remove_request(req); | 
|  | 913 | #endif | 
| Trond Myklebust | c6a556b | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 914 | nfs_clear_page_writeback(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } | 
|  | 916 |  | 
|  | 917 | static inline int flush_task_priority(int how) | 
|  | 918 | { | 
|  | 919 | switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { | 
|  | 920 | case FLUSH_HIGHPRI: | 
|  | 921 | return RPC_PRIORITY_HIGH; | 
|  | 922 | case FLUSH_LOWPRI: | 
|  | 923 | return RPC_PRIORITY_LOW; | 
|  | 924 | } | 
|  | 925 | return RPC_PRIORITY_NORMAL; | 
|  | 926 | } | 
|  | 927 |  | 
|  | 928 | /* | 
|  | 929 | * Set up the argument/result storage required for the RPC call. | 
|  | 930 | */ | 
|  | 931 | static void nfs_write_rpcsetup(struct nfs_page *req, | 
|  | 932 | struct nfs_write_data *data, | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 933 | const struct rpc_call_ops *call_ops, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | unsigned int count, unsigned int offset, | 
|  | 935 | int how) | 
|  | 936 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | struct inode		*inode; | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 938 | int flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 |  | 
|  | 940 | /* Set up the RPC argument and reply structs | 
|  | 941 | * NB: take care not to mess about with data->commit et al. */ | 
|  | 942 |  | 
|  | 943 | data->req = req; | 
|  | 944 | data->inode = inode = req->wb_context->dentry->d_inode; | 
|  | 945 | data->cred = req->wb_context->cred; | 
|  | 946 |  | 
|  | 947 | data->args.fh     = NFS_FH(inode); | 
|  | 948 | data->args.offset = req_offset(req) + offset; | 
|  | 949 | data->args.pgbase = req->wb_pgbase + offset; | 
|  | 950 | data->args.pages  = data->pagevec; | 
|  | 951 | data->args.count  = count; | 
|  | 952 | data->args.context = req->wb_context; | 
|  | 953 |  | 
|  | 954 | data->res.fattr   = &data->fattr; | 
|  | 955 | data->res.count   = count; | 
|  | 956 | data->res.verf    = &data->verf; | 
| Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 957 | nfs_fattr_init(&data->fattr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 |  | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 959 | /* Set up the initial task struct.  */ | 
|  | 960 | flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; | 
|  | 961 | rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | NFS_PROTO(inode)->write_setup(data, how); | 
|  | 963 |  | 
|  | 964 | data->task.tk_priority = flush_task_priority(how); | 
|  | 965 | data->task.tk_cookie = (unsigned long)inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 |  | 
|  | 967 | dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n", | 
| Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 968 | data->task.tk_pid, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | inode->i_sb->s_id, | 
|  | 970 | (long long)NFS_FILEID(inode), | 
|  | 971 | count, | 
|  | 972 | (unsigned long long)data->args.offset); | 
|  | 973 | } | 
|  | 974 |  | 
|  | 975 | static void nfs_execute_write(struct nfs_write_data *data) | 
|  | 976 | { | 
|  | 977 | struct rpc_clnt *clnt = NFS_CLIENT(data->inode); | 
|  | 978 | sigset_t oldset; | 
|  | 979 |  | 
|  | 980 | rpc_clnt_sigmask(clnt, &oldset); | 
|  | 981 | lock_kernel(); | 
|  | 982 | rpc_execute(&data->task); | 
|  | 983 | unlock_kernel(); | 
|  | 984 | rpc_clnt_sigunmask(clnt, &oldset); | 
|  | 985 | } | 
|  | 986 |  | 
|  | 987 | /* | 
|  | 988 | * Generate multiple small requests to write out a single | 
|  | 989 | * contiguous dirty area on one page. | 
|  | 990 | */ | 
| Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 991 | static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | { | 
|  | 993 | struct nfs_page *req = nfs_list_entry(head->next); | 
|  | 994 | struct page *page = req->wb_page; | 
|  | 995 | struct nfs_write_data *data; | 
| Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 996 | size_t wsize = NFS_SERVER(inode)->wsize, nbytes; | 
|  | 997 | unsigned int offset; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | int requests = 0; | 
|  | 999 | LIST_HEAD(list); | 
|  | 1000 |  | 
|  | 1001 | nfs_list_remove_request(req); | 
|  | 1002 |  | 
|  | 1003 | nbytes = req->wb_bytes; | 
| Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 1004 | do { | 
|  | 1005 | size_t len = min(nbytes, wsize); | 
|  | 1006 |  | 
|  | 1007 | data = nfs_writedata_alloc(len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | if (!data) | 
|  | 1009 | goto out_bad; | 
|  | 1010 | list_add(&data->pages, &list); | 
|  | 1011 | requests++; | 
| Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 1012 | nbytes -= len; | 
|  | 1013 | } while (nbytes != 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | atomic_set(&req->wb_complete, requests); | 
|  | 1015 |  | 
|  | 1016 | ClearPageError(page); | 
| Trond Myklebust | bb713d6 | 2005-12-03 15:20:14 -0500 | [diff] [blame] | 1017 | set_page_writeback(page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | offset = 0; | 
|  | 1019 | nbytes = req->wb_bytes; | 
|  | 1020 | do { | 
|  | 1021 | data = list_entry(list.next, struct nfs_write_data, pages); | 
|  | 1022 | list_del_init(&data->pages); | 
|  | 1023 |  | 
|  | 1024 | data->pagevec[0] = page; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 |  | 
|  | 1026 | if (nbytes > wsize) { | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1027 | nfs_write_rpcsetup(req, data, &nfs_write_partial_ops, | 
|  | 1028 | wsize, offset, how); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | offset += wsize; | 
|  | 1030 | nbytes -= wsize; | 
|  | 1031 | } else { | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1032 | nfs_write_rpcsetup(req, data, &nfs_write_partial_ops, | 
|  | 1033 | nbytes, offset, how); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | nbytes = 0; | 
|  | 1035 | } | 
|  | 1036 | nfs_execute_write(data); | 
|  | 1037 | } while (nbytes != 0); | 
|  | 1038 |  | 
|  | 1039 | return 0; | 
|  | 1040 |  | 
|  | 1041 | out_bad: | 
|  | 1042 | while (!list_empty(&list)) { | 
|  | 1043 | data = list_entry(list.next, struct nfs_write_data, pages); | 
|  | 1044 | list_del(&data->pages); | 
|  | 1045 | nfs_writedata_free(data); | 
|  | 1046 | } | 
|  | 1047 | nfs_mark_request_dirty(req); | 
| Trond Myklebust | c6a556b | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 1048 | nfs_clear_page_writeback(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | return -ENOMEM; | 
|  | 1050 | } | 
|  | 1051 |  | 
|  | 1052 | /* | 
|  | 1053 | * Create an RPC task for the given write request and kick it. | 
|  | 1054 | * The page must have been locked by the caller. | 
|  | 1055 | * | 
|  | 1056 | * It may happen that the page we're passed is not marked dirty. | 
|  | 1057 | * This is the case if nfs_updatepage detects a conflicting request | 
|  | 1058 | * that has been written but not committed. | 
|  | 1059 | */ | 
| Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1060 | static int nfs_flush_one(struct inode *inode, struct list_head *head, int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | { | 
|  | 1062 | struct nfs_page		*req; | 
|  | 1063 | struct page		**pages; | 
|  | 1064 | struct nfs_write_data	*data; | 
|  | 1065 | unsigned int		count; | 
|  | 1066 |  | 
| Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 1067 | data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | if (!data) | 
|  | 1069 | goto out_bad; | 
|  | 1070 |  | 
|  | 1071 | pages = data->pagevec; | 
|  | 1072 | count = 0; | 
|  | 1073 | while (!list_empty(head)) { | 
|  | 1074 | req = nfs_list_entry(head->next); | 
|  | 1075 | nfs_list_remove_request(req); | 
|  | 1076 | nfs_list_add_request(req, &data->pages); | 
|  | 1077 | ClearPageError(req->wb_page); | 
| Trond Myklebust | bb713d6 | 2005-12-03 15:20:14 -0500 | [diff] [blame] | 1078 | set_page_writeback(req->wb_page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | *pages++ = req->wb_page; | 
|  | 1080 | count += req->wb_bytes; | 
|  | 1081 | } | 
|  | 1082 | req = nfs_list_entry(data->pages.next); | 
|  | 1083 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | /* Set up the argument struct */ | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1085 | nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 |  | 
|  | 1087 | nfs_execute_write(data); | 
|  | 1088 | return 0; | 
|  | 1089 | out_bad: | 
|  | 1090 | while (!list_empty(head)) { | 
|  | 1091 | struct nfs_page *req = nfs_list_entry(head->next); | 
|  | 1092 | nfs_list_remove_request(req); | 
|  | 1093 | nfs_mark_request_dirty(req); | 
| Trond Myklebust | c6a556b | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 1094 | nfs_clear_page_writeback(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | } | 
|  | 1096 | return -ENOMEM; | 
|  | 1097 | } | 
|  | 1098 |  | 
| Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1099 | static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | { | 
|  | 1101 | LIST_HEAD(one_request); | 
| Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1102 | int (*flush_one)(struct inode *, struct list_head *, int); | 
|  | 1103 | struct nfs_page	*req; | 
|  | 1104 | int wpages = NFS_SERVER(inode)->wpages; | 
|  | 1105 | int wsize = NFS_SERVER(inode)->wsize; | 
|  | 1106 | int error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 |  | 
| Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1108 | flush_one = nfs_flush_one; | 
|  | 1109 | if (wsize < PAGE_CACHE_SIZE) | 
|  | 1110 | flush_one = nfs_flush_multi; | 
|  | 1111 | /* For single writes, FLUSH_STABLE is more efficient */ | 
|  | 1112 | if (npages <= wpages && npages == NFS_I(inode)->npages | 
|  | 1113 | && nfs_list_entry(head->next)->wb_bytes <= wsize) | 
|  | 1114 | how |= FLUSH_STABLE; | 
|  | 1115 |  | 
|  | 1116 | do { | 
|  | 1117 | nfs_coalesce_requests(head, &one_request, wpages); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | req = nfs_list_entry(one_request.next); | 
| Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1119 | error = flush_one(inode, &one_request, how); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | if (error < 0) | 
| Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1121 | goto out_err; | 
|  | 1122 | } while (!list_empty(head)); | 
|  | 1123 | return 0; | 
|  | 1124 | out_err: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | while (!list_empty(head)) { | 
|  | 1126 | req = nfs_list_entry(head->next); | 
|  | 1127 | nfs_list_remove_request(req); | 
|  | 1128 | nfs_mark_request_dirty(req); | 
| Trond Myklebust | c6a556b | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 1129 | nfs_clear_page_writeback(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | } | 
|  | 1131 | return error; | 
|  | 1132 | } | 
|  | 1133 |  | 
|  | 1134 | /* | 
|  | 1135 | * Handle a write reply that flushed part of a page. | 
|  | 1136 | */ | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1137 | static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | { | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1139 | struct nfs_write_data	*data = calldata; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | struct nfs_page		*req = data->req; | 
|  | 1141 | struct page		*page = req->wb_page; | 
|  | 1142 |  | 
|  | 1143 | dprintk("NFS: write (%s/%Ld %d@%Ld)", | 
|  | 1144 | req->wb_context->dentry->d_inode->i_sb->s_id, | 
|  | 1145 | (long long)NFS_FILEID(req->wb_context->dentry->d_inode), | 
|  | 1146 | req->wb_bytes, | 
|  | 1147 | (long long)req_offset(req)); | 
|  | 1148 |  | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1149 | if (nfs_writeback_done(task, data) != 0) | 
|  | 1150 | return; | 
|  | 1151 |  | 
|  | 1152 | if (task->tk_status < 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | ClearPageUptodate(page); | 
|  | 1154 | SetPageError(page); | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1155 | req->wb_context->error = task->tk_status; | 
|  | 1156 | dprintk(", error = %d\n", task->tk_status); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | } else { | 
|  | 1158 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | 
|  | 1159 | if (data->verf.committed < NFS_FILE_SYNC) { | 
|  | 1160 | if (!NFS_NEED_COMMIT(req)) { | 
|  | 1161 | nfs_defer_commit(req); | 
|  | 1162 | memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); | 
|  | 1163 | dprintk(" defer commit\n"); | 
|  | 1164 | } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) { | 
|  | 1165 | nfs_defer_reschedule(req); | 
|  | 1166 | dprintk(" server reboot detected\n"); | 
|  | 1167 | } | 
|  | 1168 | } else | 
|  | 1169 | #endif | 
|  | 1170 | dprintk(" OK\n"); | 
|  | 1171 | } | 
|  | 1172 |  | 
|  | 1173 | if (atomic_dec_and_test(&req->wb_complete)) | 
|  | 1174 | nfs_writepage_release(req); | 
|  | 1175 | } | 
|  | 1176 |  | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1177 | static const struct rpc_call_ops nfs_write_partial_ops = { | 
|  | 1178 | .rpc_call_done = nfs_writeback_done_partial, | 
|  | 1179 | .rpc_release = nfs_writedata_release, | 
|  | 1180 | }; | 
|  | 1181 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | /* | 
|  | 1183 | * Handle a write reply that flushes a whole page. | 
|  | 1184 | * | 
|  | 1185 | * FIXME: There is an inherent race with invalidate_inode_pages and | 
|  | 1186 | *	  writebacks since the page->count is kept > 1 for as long | 
|  | 1187 | *	  as the page has a write request pending. | 
|  | 1188 | */ | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1189 | static void nfs_writeback_done_full(struct rpc_task *task, void *calldata) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | { | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1191 | struct nfs_write_data	*data = calldata; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | struct nfs_page		*req; | 
|  | 1193 | struct page		*page; | 
|  | 1194 |  | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1195 | if (nfs_writeback_done(task, data) != 0) | 
|  | 1196 | return; | 
|  | 1197 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | /* Update attributes as result of writeback. */ | 
|  | 1199 | while (!list_empty(&data->pages)) { | 
|  | 1200 | req = nfs_list_entry(data->pages.next); | 
|  | 1201 | nfs_list_remove_request(req); | 
|  | 1202 | page = req->wb_page; | 
|  | 1203 |  | 
|  | 1204 | dprintk("NFS: write (%s/%Ld %d@%Ld)", | 
|  | 1205 | req->wb_context->dentry->d_inode->i_sb->s_id, | 
|  | 1206 | (long long)NFS_FILEID(req->wb_context->dentry->d_inode), | 
|  | 1207 | req->wb_bytes, | 
|  | 1208 | (long long)req_offset(req)); | 
|  | 1209 |  | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1210 | if (task->tk_status < 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | ClearPageUptodate(page); | 
|  | 1212 | SetPageError(page); | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1213 | req->wb_context->error = task->tk_status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | end_page_writeback(page); | 
|  | 1215 | nfs_inode_remove_request(req); | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1216 | dprintk(", error = %d\n", task->tk_status); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | goto next; | 
|  | 1218 | } | 
|  | 1219 | end_page_writeback(page); | 
|  | 1220 |  | 
|  | 1221 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | 
|  | 1222 | if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) { | 
|  | 1223 | nfs_inode_remove_request(req); | 
|  | 1224 | dprintk(" OK\n"); | 
|  | 1225 | goto next; | 
|  | 1226 | } | 
|  | 1227 | memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); | 
|  | 1228 | nfs_mark_request_commit(req); | 
|  | 1229 | dprintk(" marked for commit\n"); | 
|  | 1230 | #else | 
|  | 1231 | nfs_inode_remove_request(req); | 
|  | 1232 | #endif | 
|  | 1233 | next: | 
| Trond Myklebust | c6a556b | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 1234 | nfs_clear_page_writeback(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | } | 
|  | 1236 | } | 
|  | 1237 |  | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1238 | static const struct rpc_call_ops nfs_write_full_ops = { | 
|  | 1239 | .rpc_call_done = nfs_writeback_done_full, | 
|  | 1240 | .rpc_release = nfs_writedata_release, | 
|  | 1241 | }; | 
|  | 1242 |  | 
|  | 1243 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | /* | 
|  | 1245 | * This function is called when the WRITE call is complete. | 
|  | 1246 | */ | 
| Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 1247 | int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | struct nfs_writeargs	*argp = &data->args; | 
|  | 1250 | struct nfs_writeres	*resp = &data->res; | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1251 | int status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 |  | 
|  | 1253 | dprintk("NFS: %4d nfs_writeback_done (status %d)\n", | 
|  | 1254 | task->tk_pid, task->tk_status); | 
|  | 1255 |  | 
| Chuck Lever | f551e44 | 2006-09-20 14:33:04 -0400 | [diff] [blame] | 1256 | /* | 
|  | 1257 | * ->write_done will attempt to use post-op attributes to detect | 
|  | 1258 | * conflicting writes by other clients.  A strict interpretation | 
|  | 1259 | * of close-to-open would allow us to continue caching even if | 
|  | 1260 | * another writer had changed the file, but some applications | 
|  | 1261 | * depend on tighter cache coherency when writing. | 
|  | 1262 | */ | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1263 | status = NFS_PROTO(data->inode)->write_done(task, data); | 
|  | 1264 | if (status != 0) | 
|  | 1265 | return status; | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 1266 | nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count); | 
|  | 1267 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | 
|  | 1269 | if (resp->verf->committed < argp->stable && task->tk_status >= 0) { | 
|  | 1270 | /* We tried a write call, but the server did not | 
|  | 1271 | * commit data to stable storage even though we | 
|  | 1272 | * requested it. | 
|  | 1273 | * Note: There is a known bug in Tru64 < 5.0 in which | 
|  | 1274 | *	 the server reports NFS_DATA_SYNC, but performs | 
|  | 1275 | *	 NFS_FILE_SYNC. We therefore implement this checking | 
|  | 1276 | *	 as a dprintk() in order to avoid filling syslog. | 
|  | 1277 | */ | 
|  | 1278 | static unsigned long    complain; | 
|  | 1279 |  | 
|  | 1280 | if (time_before(complain, jiffies)) { | 
|  | 1281 | dprintk("NFS: faulty NFS server %s:" | 
|  | 1282 | " (committed = %d) != (stable = %d)\n", | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1283 | NFS_SERVER(data->inode)->nfs_client->cl_hostname, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | resp->verf->committed, argp->stable); | 
|  | 1285 | complain = jiffies + 300 * HZ; | 
|  | 1286 | } | 
|  | 1287 | } | 
|  | 1288 | #endif | 
|  | 1289 | /* Is this a short write? */ | 
|  | 1290 | if (task->tk_status >= 0 && resp->count < argp->count) { | 
|  | 1291 | static unsigned long    complain; | 
|  | 1292 |  | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 1293 | nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE); | 
|  | 1294 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | /* Has the server at least made some progress? */ | 
|  | 1296 | if (resp->count != 0) { | 
|  | 1297 | /* Was this an NFSv2 write or an NFSv3 stable write? */ | 
|  | 1298 | if (resp->verf->committed != NFS_UNSTABLE) { | 
|  | 1299 | /* Resend from where the server left off */ | 
|  | 1300 | argp->offset += resp->count; | 
|  | 1301 | argp->pgbase += resp->count; | 
|  | 1302 | argp->count -= resp->count; | 
|  | 1303 | } else { | 
|  | 1304 | /* Resend as a stable write in order to avoid | 
|  | 1305 | * headaches in the case of a server crash. | 
|  | 1306 | */ | 
|  | 1307 | argp->stable = NFS_FILE_SYNC; | 
|  | 1308 | } | 
|  | 1309 | rpc_restart_call(task); | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1310 | return -EAGAIN; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | } | 
|  | 1312 | if (time_before(complain, jiffies)) { | 
|  | 1313 | printk(KERN_WARNING | 
|  | 1314 | "NFS: Server wrote zero bytes, expected %u.\n", | 
|  | 1315 | argp->count); | 
|  | 1316 | complain = jiffies + 300 * HZ; | 
|  | 1317 | } | 
|  | 1318 | /* Can't do anything about it except throw an error. */ | 
|  | 1319 | task->tk_status = -EIO; | 
|  | 1320 | } | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1321 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | } | 
|  | 1323 |  | 
|  | 1324 |  | 
|  | 1325 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | 
| Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 1326 | void nfs_commit_release(void *wdata) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | nfs_commit_free(wdata); | 
|  | 1329 | } | 
|  | 1330 |  | 
|  | 1331 | /* | 
|  | 1332 | * Set up the argument/result storage required for the RPC call. | 
|  | 1333 | */ | 
|  | 1334 | static void nfs_commit_rpcsetup(struct list_head *head, | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1335 | struct nfs_write_data *data, | 
|  | 1336 | int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | { | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1338 | struct nfs_page		*first; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | struct inode		*inode; | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1340 | int flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 |  | 
|  | 1342 | /* Set up the RPC argument and reply structs | 
|  | 1343 | * NB: take care not to mess about with data->commit et al. */ | 
|  | 1344 |  | 
|  | 1345 | list_splice_init(head, &data->pages); | 
|  | 1346 | first = nfs_list_entry(data->pages.next); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | inode = first->wb_context->dentry->d_inode; | 
|  | 1348 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | data->inode	  = inode; | 
|  | 1350 | data->cred	  = first->wb_context->cred; | 
|  | 1351 |  | 
|  | 1352 | data->args.fh     = NFS_FH(data->inode); | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1353 | /* Note: we always request a commit of the entire inode */ | 
|  | 1354 | data->args.offset = 0; | 
|  | 1355 | data->args.count  = 0; | 
|  | 1356 | data->res.count   = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | data->res.fattr   = &data->fattr; | 
|  | 1358 | data->res.verf    = &data->verf; | 
| Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1359 | nfs_fattr_init(&data->fattr); | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1360 |  | 
|  | 1361 | /* Set up the initial task struct.  */ | 
|  | 1362 | flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; | 
|  | 1363 | rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | NFS_PROTO(inode)->commit_setup(data, how); | 
|  | 1365 |  | 
|  | 1366 | data->task.tk_priority = flush_task_priority(how); | 
|  | 1367 | data->task.tk_cookie = (unsigned long)inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 |  | 
| Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 1369 | dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | } | 
|  | 1371 |  | 
|  | 1372 | /* | 
|  | 1373 | * Commit dirty pages | 
|  | 1374 | */ | 
|  | 1375 | static int | 
| Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 1376 | nfs_commit_list(struct inode *inode, struct list_head *head, int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | { | 
|  | 1378 | struct nfs_write_data	*data; | 
|  | 1379 | struct nfs_page         *req; | 
|  | 1380 |  | 
| Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 1381 | data = nfs_commit_alloc(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 |  | 
|  | 1383 | if (!data) | 
|  | 1384 | goto out_bad; | 
|  | 1385 |  | 
|  | 1386 | /* Set up the argument struct */ | 
|  | 1387 | nfs_commit_rpcsetup(head, data, how); | 
|  | 1388 |  | 
|  | 1389 | nfs_execute_write(data); | 
|  | 1390 | return 0; | 
|  | 1391 | out_bad: | 
|  | 1392 | while (!list_empty(head)) { | 
|  | 1393 | req = nfs_list_entry(head->next); | 
|  | 1394 | nfs_list_remove_request(req); | 
|  | 1395 | nfs_mark_request_commit(req); | 
| Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 1396 | dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); | 
| Trond Myklebust | 5c2d97c | 2006-09-18 23:20:35 -0400 | [diff] [blame] | 1397 | nfs_clear_page_writeback(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | } | 
|  | 1399 | return -ENOMEM; | 
|  | 1400 | } | 
|  | 1401 |  | 
|  | 1402 | /* | 
|  | 1403 | * COMMIT call returned | 
|  | 1404 | */ | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1405 | static void nfs_commit_done(struct rpc_task *task, void *calldata) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | { | 
| Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 1407 | struct nfs_write_data	*data = calldata; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | struct nfs_page		*req; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 |  | 
|  | 1410 | dprintk("NFS: %4d nfs_commit_done (status %d)\n", | 
|  | 1411 | task->tk_pid, task->tk_status); | 
|  | 1412 |  | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1413 | /* Call the NFS version-specific code */ | 
|  | 1414 | if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) | 
|  | 1415 | return; | 
|  | 1416 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | while (!list_empty(&data->pages)) { | 
|  | 1418 | req = nfs_list_entry(data->pages.next); | 
|  | 1419 | nfs_list_remove_request(req); | 
| Christoph Lameter | fd39fc8 | 2006-06-30 01:55:40 -0700 | [diff] [blame] | 1420 | dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 |  | 
|  | 1422 | dprintk("NFS: commit (%s/%Ld %d@%Ld)", | 
|  | 1423 | req->wb_context->dentry->d_inode->i_sb->s_id, | 
|  | 1424 | (long long)NFS_FILEID(req->wb_context->dentry->d_inode), | 
|  | 1425 | req->wb_bytes, | 
|  | 1426 | (long long)req_offset(req)); | 
|  | 1427 | if (task->tk_status < 0) { | 
|  | 1428 | req->wb_context->error = task->tk_status; | 
|  | 1429 | nfs_inode_remove_request(req); | 
|  | 1430 | dprintk(", error = %d\n", task->tk_status); | 
|  | 1431 | goto next; | 
|  | 1432 | } | 
|  | 1433 |  | 
|  | 1434 | /* Okay, COMMIT succeeded, apparently. Check the verifier | 
|  | 1435 | * returned by the server against all stored verfs. */ | 
|  | 1436 | if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { | 
|  | 1437 | /* We have a match */ | 
|  | 1438 | nfs_inode_remove_request(req); | 
|  | 1439 | dprintk(" OK\n"); | 
|  | 1440 | goto next; | 
|  | 1441 | } | 
|  | 1442 | /* We have a mismatch. Write the page again */ | 
|  | 1443 | dprintk(" mismatch\n"); | 
|  | 1444 | nfs_mark_request_dirty(req); | 
|  | 1445 | next: | 
| Trond Myklebust | c6a556b | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 1446 | nfs_clear_page_writeback(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | } | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1449 |  | 
|  | 1450 | static const struct rpc_call_ops nfs_commit_ops = { | 
|  | 1451 | .rpc_call_done = nfs_commit_done, | 
|  | 1452 | .rpc_release = nfs_commit_release, | 
|  | 1453 | }; | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1454 | #else | 
|  | 1455 | static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how) | 
|  | 1456 | { | 
|  | 1457 | return 0; | 
|  | 1458 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | #endif | 
|  | 1460 |  | 
|  | 1461 | static int nfs_flush_inode(struct inode *inode, unsigned long idx_start, | 
|  | 1462 | unsigned int npages, int how) | 
|  | 1463 | { | 
|  | 1464 | struct nfs_inode *nfsi = NFS_I(inode); | 
|  | 1465 | LIST_HEAD(head); | 
| Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1466 | int res; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 |  | 
|  | 1468 | spin_lock(&nfsi->req_lock); | 
|  | 1469 | res = nfs_scan_dirty(inode, &head, idx_start, npages); | 
|  | 1470 | spin_unlock(&nfsi->req_lock); | 
| Trond Myklebust | ab0a3db | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 1471 | if (res) { | 
| Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1472 | int error = nfs_flush_list(inode, &head, res, how); | 
|  | 1473 | if (error < 0) | 
|  | 1474 | return error; | 
| Trond Myklebust | ab0a3db | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 1475 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | return res; | 
|  | 1477 | } | 
|  | 1478 |  | 
|  | 1479 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1480 | int nfs_commit_inode(struct inode *inode, int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | { | 
|  | 1482 | struct nfs_inode *nfsi = NFS_I(inode); | 
|  | 1483 | LIST_HEAD(head); | 
| Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1484 | int res; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 |  | 
|  | 1486 | spin_lock(&nfsi->req_lock); | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1487 | res = nfs_scan_commit(inode, &head, 0, 0); | 
|  | 1488 | spin_unlock(&nfsi->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | if (res) { | 
| Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1490 | int error = nfs_commit_list(inode, &head, how); | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1491 | if (error < 0) | 
|  | 1492 | return error; | 
|  | 1493 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | return res; | 
|  | 1495 | } | 
|  | 1496 | #endif | 
|  | 1497 |  | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1498 | int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start, | 
|  | 1499 | unsigned int npages, int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | { | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1501 | struct nfs_inode *nfsi = NFS_I(inode); | 
|  | 1502 | LIST_HEAD(head); | 
| Trond Myklebust | 70b9ecb | 2006-01-03 09:55:34 +0100 | [diff] [blame] | 1503 | int nocommit = how & FLUSH_NOCOMMIT; | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1504 | int pages, ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 |  | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1506 | how &= ~FLUSH_NOCOMMIT; | 
|  | 1507 | spin_lock(&nfsi->req_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | do { | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1509 | ret = nfs_wait_on_requests_locked(inode, idx_start, npages); | 
|  | 1510 | if (ret != 0) | 
| Trond Myklebust | 70b9ecb | 2006-01-03 09:55:34 +0100 | [diff] [blame] | 1511 | continue; | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1512 | pages = nfs_scan_dirty(inode, &head, idx_start, npages); | 
|  | 1513 | if (pages != 0) { | 
|  | 1514 | spin_unlock(&nfsi->req_lock); | 
| Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 1515 | if (how & FLUSH_INVALIDATE) | 
| Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 1516 | nfs_cancel_dirty_list(&head); | 
| Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 1517 | else | 
|  | 1518 | ret = nfs_flush_list(inode, &head, pages, how); | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1519 | spin_lock(&nfsi->req_lock); | 
|  | 1520 | continue; | 
|  | 1521 | } | 
|  | 1522 | if (nocommit) | 
|  | 1523 | break; | 
| Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 1524 | pages = nfs_scan_commit(inode, &head, idx_start, npages); | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1525 | if (pages == 0) | 
|  | 1526 | break; | 
| Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 1527 | if (how & FLUSH_INVALIDATE) { | 
|  | 1528 | spin_unlock(&nfsi->req_lock); | 
| Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 1529 | nfs_cancel_commit_list(&head); | 
| Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 1530 | spin_lock(&nfsi->req_lock); | 
|  | 1531 | continue; | 
|  | 1532 | } | 
|  | 1533 | pages += nfs_scan_commit(inode, &head, 0, 0); | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1534 | spin_unlock(&nfsi->req_lock); | 
|  | 1535 | ret = nfs_commit_list(inode, &head, how); | 
|  | 1536 | spin_lock(&nfsi->req_lock); | 
|  | 1537 | } while (ret >= 0); | 
|  | 1538 | spin_unlock(&nfsi->req_lock); | 
|  | 1539 | return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | } | 
|  | 1541 |  | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 1542 | int __init nfs_init_writepagecache(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | { | 
|  | 1544 | nfs_wdata_cachep = kmem_cache_create("nfs_write_data", | 
|  | 1545 | sizeof(struct nfs_write_data), | 
|  | 1546 | 0, SLAB_HWCACHE_ALIGN, | 
|  | 1547 | NULL, NULL); | 
|  | 1548 | if (nfs_wdata_cachep == NULL) | 
|  | 1549 | return -ENOMEM; | 
|  | 1550 |  | 
| Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1551 | nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, | 
|  | 1552 | nfs_wdata_cachep); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | if (nfs_wdata_mempool == NULL) | 
|  | 1554 | return -ENOMEM; | 
|  | 1555 |  | 
| Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1556 | nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, | 
|  | 1557 | nfs_wdata_cachep); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | if (nfs_commit_mempool == NULL) | 
|  | 1559 | return -ENOMEM; | 
|  | 1560 |  | 
|  | 1561 | return 0; | 
|  | 1562 | } | 
|  | 1563 |  | 
| David Brownell | 266bee8 | 2006-06-27 12:59:15 -0700 | [diff] [blame] | 1564 | void nfs_destroy_writepagecache(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | { | 
|  | 1566 | mempool_destroy(nfs_commit_mempool); | 
|  | 1567 | mempool_destroy(nfs_wdata_mempool); | 
|  | 1568 | if (kmem_cache_destroy(nfs_wdata_cachep)) | 
|  | 1569 | printk(KERN_INFO "nfs_write_data: not all structures were freed\n"); | 
|  | 1570 | } | 
|  | 1571 |  |