Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/pagelist.c |
| 3 | * |
| 4 | * A set of helper functions for managing NFS read and write requests. |
| 5 | * The main purpose of these routines is to provide support for the |
| 6 | * coalescing of several requests into a single RPC call. |
| 7 | * |
| 8 | * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no> |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <linux/config.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/file.h> |
| 15 | #include <linux/sunrpc/clnt.h> |
| 16 | #include <linux/nfs3.h> |
| 17 | #include <linux/nfs4.h> |
| 18 | #include <linux/nfs_page.h> |
| 19 | #include <linux/nfs_fs.h> |
| 20 | #include <linux/nfs_mount.h> |
| 21 | |
| 22 | #define NFS_PARANOIA 1 |
| 23 | |
| 24 | static kmem_cache_t *nfs_page_cachep; |
| 25 | |
| 26 | static inline struct nfs_page * |
| 27 | nfs_page_alloc(void) |
| 28 | { |
| 29 | struct nfs_page *p; |
| 30 | p = kmem_cache_alloc(nfs_page_cachep, SLAB_KERNEL); |
| 31 | if (p) { |
| 32 | memset(p, 0, sizeof(*p)); |
| 33 | INIT_LIST_HEAD(&p->wb_list); |
| 34 | } |
| 35 | return p; |
| 36 | } |
| 37 | |
| 38 | static inline void |
| 39 | nfs_page_free(struct nfs_page *p) |
| 40 | { |
| 41 | kmem_cache_free(nfs_page_cachep, p); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * nfs_create_request - Create an NFS read/write request. |
| 46 | * @file: file descriptor to use |
| 47 | * @inode: inode to which the request is attached |
| 48 | * @page: page to write |
| 49 | * @offset: starting offset within the page for the write |
| 50 | * @count: number of bytes to read/write |
| 51 | * |
| 52 | * The page must be locked by the caller. This makes sure we never |
| 53 | * create two different requests for the same page, and avoids |
| 54 | * a possible deadlock when we reach the hard limit on the number |
| 55 | * of dirty pages. |
| 56 | * User should ensure it is safe to sleep in this function. |
| 57 | */ |
| 58 | struct nfs_page * |
| 59 | nfs_create_request(struct nfs_open_context *ctx, struct inode *inode, |
| 60 | struct page *page, |
| 61 | unsigned int offset, unsigned int count) |
| 62 | { |
| 63 | struct nfs_server *server = NFS_SERVER(inode); |
| 64 | struct nfs_page *req; |
| 65 | |
| 66 | /* Deal with hard limits. */ |
| 67 | for (;;) { |
| 68 | /* try to allocate the request struct */ |
| 69 | req = nfs_page_alloc(); |
| 70 | if (req != NULL) |
| 71 | break; |
| 72 | |
| 73 | /* Try to free up at least one request in order to stay |
| 74 | * below the hard limit |
| 75 | */ |
| 76 | if (signalled() && (server->flags & NFS_MOUNT_INTR)) |
| 77 | return ERR_PTR(-ERESTARTSYS); |
| 78 | yield(); |
| 79 | } |
| 80 | |
| 81 | /* Initialize the request struct. Initially, we assume a |
| 82 | * long write-back delay. This will be adjusted in |
| 83 | * update_nfs_request below if the region is not locked. */ |
| 84 | req->wb_page = page; |
| 85 | atomic_set(&req->wb_complete, 0); |
| 86 | req->wb_index = page->index; |
| 87 | page_cache_get(page); |
| 88 | req->wb_offset = offset; |
| 89 | req->wb_pgbase = offset; |
| 90 | req->wb_bytes = count; |
| 91 | atomic_set(&req->wb_count, 1); |
| 92 | req->wb_context = get_nfs_open_context(ctx); |
| 93 | |
| 94 | return req; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * nfs_unlock_request - Unlock request and wake up sleepers. |
| 99 | * @req: |
| 100 | */ |
| 101 | void nfs_unlock_request(struct nfs_page *req) |
| 102 | { |
| 103 | if (!NFS_WBACK_BUSY(req)) { |
| 104 | printk(KERN_ERR "NFS: Invalid unlock attempted\n"); |
| 105 | BUG(); |
| 106 | } |
| 107 | smp_mb__before_clear_bit(); |
| 108 | clear_bit(PG_BUSY, &req->wb_flags); |
| 109 | smp_mb__after_clear_bit(); |
Trond Myklebust | 464a98b | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 110 | wake_up_bit(&req->wb_flags, PG_BUSY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | nfs_release_request(req); |
| 112 | } |
| 113 | |
| 114 | /** |
Trond Myklebust | c6a556b | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 115 | * nfs_set_page_writeback_locked - Lock a request for writeback |
| 116 | * @req: |
| 117 | */ |
| 118 | int nfs_set_page_writeback_locked(struct nfs_page *req) |
| 119 | { |
| 120 | struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode); |
| 121 | |
| 122 | if (!nfs_lock_request(req)) |
| 123 | return 0; |
| 124 | radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK); |
| 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * nfs_clear_page_writeback - Unlock request and wake up sleepers |
| 130 | */ |
| 131 | void nfs_clear_page_writeback(struct nfs_page *req) |
| 132 | { |
| 133 | struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode); |
| 134 | |
| 135 | spin_lock(&nfsi->req_lock); |
| 136 | radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK); |
| 137 | spin_unlock(&nfsi->req_lock); |
| 138 | nfs_unlock_request(req); |
| 139 | } |
| 140 | |
| 141 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | * nfs_clear_request - Free up all resources allocated to the request |
| 143 | * @req: |
| 144 | * |
| 145 | * Release page resources associated with a write request after it |
| 146 | * has completed. |
| 147 | */ |
| 148 | void nfs_clear_request(struct nfs_page *req) |
| 149 | { |
| 150 | if (req->wb_page) { |
| 151 | page_cache_release(req->wb_page); |
| 152 | req->wb_page = NULL; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * nfs_release_request - Release the count on an NFS read/write request |
| 159 | * @req: request to release |
| 160 | * |
| 161 | * Note: Should never be called with the spinlock held! |
| 162 | */ |
| 163 | void |
| 164 | nfs_release_request(struct nfs_page *req) |
| 165 | { |
| 166 | if (!atomic_dec_and_test(&req->wb_count)) |
| 167 | return; |
| 168 | |
| 169 | #ifdef NFS_PARANOIA |
| 170 | BUG_ON (!list_empty(&req->wb_list)); |
| 171 | BUG_ON (NFS_WBACK_BUSY(req)); |
| 172 | #endif |
| 173 | |
| 174 | /* Release struct file or cached credential */ |
| 175 | nfs_clear_request(req); |
| 176 | put_nfs_open_context(req->wb_context); |
| 177 | nfs_page_free(req); |
| 178 | } |
| 179 | |
Trond Myklebust | 464a98b | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 180 | static int nfs_wait_bit_interruptible(void *word) |
| 181 | { |
| 182 | int ret = 0; |
| 183 | |
| 184 | if (signal_pending(current)) |
| 185 | ret = -ERESTARTSYS; |
| 186 | else |
| 187 | schedule(); |
| 188 | return ret; |
| 189 | } |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | /** |
| 192 | * nfs_wait_on_request - Wait for a request to complete. |
| 193 | * @req: request to wait upon. |
| 194 | * |
| 195 | * Interruptible by signals only if mounted with intr flag. |
| 196 | * The user is responsible for holding a count on the request. |
| 197 | */ |
| 198 | int |
| 199 | nfs_wait_on_request(struct nfs_page *req) |
| 200 | { |
Trond Myklebust | 464a98b | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 201 | struct rpc_clnt *clnt = NFS_CLIENT(req->wb_context->dentry->d_inode); |
| 202 | sigset_t oldmask; |
| 203 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Trond Myklebust | 464a98b | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 205 | if (!test_bit(PG_BUSY, &req->wb_flags)) |
| 206 | goto out; |
| 207 | /* |
| 208 | * Note: the call to rpc_clnt_sigmask() suffices to ensure that we |
| 209 | * are not interrupted if intr flag is not set |
| 210 | */ |
| 211 | rpc_clnt_sigmask(clnt, &oldmask); |
| 212 | ret = out_of_line_wait_on_bit(&req->wb_flags, PG_BUSY, |
| 213 | nfs_wait_bit_interruptible, TASK_INTERRUPTIBLE); |
| 214 | rpc_clnt_sigunmask(clnt, &oldmask); |
| 215 | out: |
| 216 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
| 220 | * nfs_coalesce_requests - Split coalesced requests out from a list. |
| 221 | * @head: source list |
| 222 | * @dst: destination list |
| 223 | * @nmax: maximum number of requests to coalesce |
| 224 | * |
| 225 | * Moves a maximum of 'nmax' elements from one list to another. |
| 226 | * The elements are checked to ensure that they form a contiguous set |
| 227 | * of pages, and that the RPC credentials are the same. |
| 228 | */ |
| 229 | int |
| 230 | nfs_coalesce_requests(struct list_head *head, struct list_head *dst, |
| 231 | unsigned int nmax) |
| 232 | { |
| 233 | struct nfs_page *req = NULL; |
| 234 | unsigned int npages = 0; |
| 235 | |
| 236 | while (!list_empty(head)) { |
| 237 | struct nfs_page *prev = req; |
| 238 | |
| 239 | req = nfs_list_entry(head->next); |
| 240 | if (prev) { |
| 241 | if (req->wb_context->cred != prev->wb_context->cred) |
| 242 | break; |
| 243 | if (req->wb_context->lockowner != prev->wb_context->lockowner) |
| 244 | break; |
| 245 | if (req->wb_context->state != prev->wb_context->state) |
| 246 | break; |
| 247 | if (req->wb_index != (prev->wb_index + 1)) |
| 248 | break; |
| 249 | |
| 250 | if (req->wb_pgbase != 0) |
| 251 | break; |
| 252 | } |
| 253 | nfs_list_remove_request(req); |
| 254 | nfs_list_add_request(req, dst); |
| 255 | npages++; |
| 256 | if (req->wb_pgbase + req->wb_bytes != PAGE_CACHE_SIZE) |
| 257 | break; |
| 258 | if (npages >= nmax) |
| 259 | break; |
| 260 | } |
| 261 | return npages; |
| 262 | } |
| 263 | |
Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 264 | #define NFS_SCAN_MAXENTRIES 16 |
| 265 | /** |
| 266 | * nfs_scan_lock_dirty - Scan the radix tree for dirty requests |
| 267 | * @nfsi: NFS inode |
| 268 | * @dst: Destination list |
| 269 | * @idx_start: lower bound of page->index to scan |
| 270 | * @npages: idx_start + npages sets the upper bound to scan. |
| 271 | * |
| 272 | * Moves elements from one of the inode request lists. |
| 273 | * If the number of requests is set to 0, the entire address_space |
| 274 | * starting at index idx_start, is scanned. |
| 275 | * The requests are *not* checked to ensure that they form a contiguous set. |
| 276 | * You must be holding the inode's req_lock when calling this function |
| 277 | */ |
| 278 | int |
| 279 | nfs_scan_lock_dirty(struct nfs_inode *nfsi, struct list_head *dst, |
| 280 | unsigned long idx_start, unsigned int npages) |
| 281 | { |
| 282 | struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES]; |
| 283 | struct nfs_page *req; |
| 284 | unsigned long idx_end; |
| 285 | int found, i; |
| 286 | int res; |
| 287 | |
| 288 | res = 0; |
| 289 | if (npages == 0) |
| 290 | idx_end = ~0; |
| 291 | else |
| 292 | idx_end = idx_start + npages - 1; |
| 293 | |
| 294 | for (;;) { |
| 295 | found = radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, |
| 296 | (void **)&pgvec[0], idx_start, NFS_SCAN_MAXENTRIES, |
| 297 | NFS_PAGE_TAG_DIRTY); |
| 298 | if (found <= 0) |
| 299 | break; |
| 300 | for (i = 0; i < found; i++) { |
| 301 | req = pgvec[i]; |
| 302 | if (req->wb_index > idx_end) |
| 303 | goto out; |
| 304 | |
| 305 | idx_start = req->wb_index + 1; |
| 306 | |
| 307 | if (nfs_set_page_writeback_locked(req)) { |
| 308 | radix_tree_tag_clear(&nfsi->nfs_page_tree, |
| 309 | req->wb_index, NFS_PAGE_TAG_DIRTY); |
| 310 | nfs_list_remove_request(req); |
| 311 | nfs_list_add_request(req, dst); |
| 312 | res++; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | out: |
| 317 | return res; |
| 318 | } |
| 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | /** |
| 321 | * nfs_scan_list - Scan a list for matching requests |
| 322 | * @head: One of the NFS inode request lists |
| 323 | * @dst: Destination list |
| 324 | * @idx_start: lower bound of page->index to scan |
| 325 | * @npages: idx_start + npages sets the upper bound to scan. |
| 326 | * |
| 327 | * Moves elements from one of the inode request lists. |
| 328 | * If the number of requests is set to 0, the entire address_space |
| 329 | * starting at index idx_start, is scanned. |
| 330 | * The requests are *not* checked to ensure that they form a contiguous set. |
| 331 | * You must be holding the inode's req_lock when calling this function |
| 332 | */ |
| 333 | int |
| 334 | nfs_scan_list(struct list_head *head, struct list_head *dst, |
| 335 | unsigned long idx_start, unsigned int npages) |
| 336 | { |
| 337 | struct list_head *pos, *tmp; |
| 338 | struct nfs_page *req; |
| 339 | unsigned long idx_end; |
| 340 | int res; |
| 341 | |
| 342 | res = 0; |
| 343 | if (npages == 0) |
| 344 | idx_end = ~0; |
| 345 | else |
| 346 | idx_end = idx_start + npages - 1; |
| 347 | |
| 348 | list_for_each_safe(pos, tmp, head) { |
| 349 | |
| 350 | req = nfs_list_entry(pos); |
| 351 | |
| 352 | if (req->wb_index < idx_start) |
| 353 | continue; |
| 354 | if (req->wb_index > idx_end) |
| 355 | break; |
| 356 | |
Trond Myklebust | c6a556b | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 357 | if (!nfs_set_page_writeback_locked(req)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | continue; |
| 359 | nfs_list_remove_request(req); |
| 360 | nfs_list_add_request(req, dst); |
| 361 | res++; |
| 362 | } |
| 363 | return res; |
| 364 | } |
| 365 | |
| 366 | int nfs_init_nfspagecache(void) |
| 367 | { |
| 368 | nfs_page_cachep = kmem_cache_create("nfs_page", |
| 369 | sizeof(struct nfs_page), |
| 370 | 0, SLAB_HWCACHE_ALIGN, |
| 371 | NULL, NULL); |
| 372 | if (nfs_page_cachep == NULL) |
| 373 | return -ENOMEM; |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | void nfs_destroy_nfspagecache(void) |
| 379 | { |
| 380 | if (kmem_cache_destroy(nfs_page_cachep)) |
| 381 | printk(KERN_INFO "nfs_page: not all structures were freed\n"); |
| 382 | } |
| 383 | |