File: /usr/src/linux/fs/nfs/write.c

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     
49     #include <linux/config.h>
50     #include <linux/types.h>
51     #include <linux/slab.h>
52     #include <linux/swap.h>
53     #include <linux/pagemap.h>
54     #include <linux/file.h>
55     
56     #include <linux/sunrpc/clnt.h>
57     #include <linux/nfs_fs.h>
58     #include <linux/nfs_mount.h>
59     #include <linux/nfs_flushd.h>
60     #include <linux/nfs_page.h>
61     #include <asm/uaccess.h>
62     #include <linux/smp_lock.h>
63     
64     #define NFS_PARANOIA 1
65     #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
66     
67     /*
68      * Spinlock
69      */
70     spinlock_t nfs_wreq_lock = SPIN_LOCK_UNLOCKED;
71     static atomic_t	nfs_nr_requests = ATOMIC_INIT(0);
72     
73     /*
74      * Local structures
75      *
76      * This is the struct where the WRITE/COMMIT arguments go.
77      */
78     struct nfs_write_data {
79     	struct rpc_task		task;
80     	struct inode		*inode;
81     	struct rpc_cred		*cred;
82     	struct nfs_writeargs	args;		/* argument struct */
83     	struct nfs_writeres	res;		/* result struct */
84     	struct nfs_fattr	fattr;
85     	struct nfs_writeverf	verf;
86     	struct list_head	pages;		/* Coalesced requests we wish to flush */
87     };
88     
89     /*
90      * Local function declarations
91      */
92     static struct nfs_page * nfs_update_request(struct file*, struct inode *,
93     					    struct page *,
94     					    unsigned int, unsigned int);
95     static void	nfs_strategy(struct inode *inode);
96     static void	nfs_writeback_done(struct rpc_task *);
97     #ifdef CONFIG_NFS_V3
98     static void	nfs_commit_done(struct rpc_task *);
99     #endif
100     
101     /* Hack for future NFS swap support */
102     #ifndef IS_SWAPFILE
103     # define IS_SWAPFILE(inode)	(0)
104     #endif
105     
106     static kmem_cache_t *nfs_page_cachep;
107     static kmem_cache_t *nfs_wdata_cachep;
108     
109     static __inline__ struct nfs_page *nfs_page_alloc(void)
110     {
111     	struct nfs_page	*p;
112     	p = kmem_cache_alloc(nfs_page_cachep, SLAB_KERNEL);
113     	if (p) {
114     		memset(p, 0, sizeof(*p));
115     		INIT_LIST_HEAD(&p->wb_hash);
116     		INIT_LIST_HEAD(&p->wb_list);
117     		init_waitqueue_head(&p->wb_wait);
118     	}
119     	return p;
120     }
121     
122     static __inline__ void nfs_page_free(struct nfs_page *p)
123     {
124     	kmem_cache_free(nfs_page_cachep, p);
125     }
126     
127     static __inline__ struct nfs_write_data *nfs_writedata_alloc(void)
128     {
129     	struct nfs_write_data	*p;
130     	p = kmem_cache_alloc(nfs_wdata_cachep, SLAB_NFS);
131     	if (p) {
132     		memset(p, 0, sizeof(*p));
133     		INIT_LIST_HEAD(&p->pages);
134     	}
135     	return p;
136     }
137     
138     static __inline__ void nfs_writedata_free(struct nfs_write_data *p)
139     {
140     	kmem_cache_free(nfs_wdata_cachep, p);
141     }
142     
143     static void nfs_writedata_release(struct rpc_task *task)
144     {
145     	struct nfs_write_data	*wdata = (struct nfs_write_data *)task->tk_calldata;
146     	nfs_writedata_free(wdata);
147     }
148     
149     /*
150      * This function will be used to simulate weak cache consistency
151      * under NFSv2 when the NFSv3 attribute patch is included.
152      * For the moment, we just call nfs_refresh_inode().
153      */
154     static __inline__ int
155     nfs_write_attributes(struct inode *inode, struct nfs_fattr *fattr)
156     {
157     	if ((fattr->valid & NFS_ATTR_FATTR) && !(fattr->valid & NFS_ATTR_WCC)) {
158     		fattr->pre_size  = NFS_CACHE_ISIZE(inode);
159     		fattr->pre_mtime = NFS_CACHE_MTIME(inode);
160     		fattr->pre_ctime = NFS_CACHE_CTIME(inode);
161     		fattr->valid |= NFS_ATTR_WCC;
162     	}
163     	return nfs_refresh_inode(inode, fattr);
164     }
165     
166     /*
167      * Write a page synchronously.
168      * Offset is the data offset within the page.
169      */
170     static int
171     nfs_writepage_sync(struct file *file, struct inode *inode, struct page *page,
172     		   unsigned int offset, unsigned int count)
173     {
174     	struct rpc_cred	*cred = NULL;
175     	loff_t		base;
176     	unsigned int	wsize = NFS_SERVER(inode)->wsize;
177     	int		result, refresh = 0, written = 0, flags;
178     	u8		*buffer;
179     	struct nfs_fattr fattr;
180     	struct nfs_writeverf verf;
181     
182     
183     	if (file)
184     		cred = get_rpccred(nfs_file_cred(file));
185     	if (!cred)
186     		cred = get_rpccred(NFS_I(inode)->mm_cred);
187     
188     	dprintk("NFS:      nfs_writepage_sync(%x/%Ld %d@%Ld)\n",
189     		inode->i_dev, (long long)NFS_FILEID(inode),
190     		count, (long long)(page_offset(page) + offset));
191     
192     	buffer = kmap(page) + offset;
193     	base = page_offset(page) + offset;
194     
195     	flags = ((IS_SWAPFILE(inode)) ? NFS_RW_SWAP : 0) | NFS_RW_SYNC;
196     
197     	do {
198     		if (count < wsize && !IS_SWAPFILE(inode))
199     			wsize = count;
200     
201     		result = NFS_PROTO(inode)->write(inode, cred, &fattr, flags,
202     						 base, wsize, buffer, &verf);
203     		nfs_write_attributes(inode, &fattr);
204     
205     		if (result < 0) {
206     			/* Must mark the page invalid after I/O error */
207     			ClearPageUptodate(page);
208     			goto io_error;
209     		}
210     		if (result != wsize)
211     			printk("NFS: short write, wsize=%u, result=%d\n",
212     			wsize, result);
213     		refresh = 1;
214     		buffer  += wsize;
215     	        base    += wsize;
216     		written += wsize;
217     		count   -= wsize;
218     		/*
219     		 * If we've extended the file, update the inode
220     		 * now so we don't invalidate the cache.
221     		 */
222     		if (base > inode->i_size)
223     			inode->i_size = base;
224     	} while (count);
225     
226     	if (PageError(page))
227     		ClearPageError(page);
228     
229     io_error:
230     	kunmap(page);
231     	if (cred)
232     		put_rpccred(cred);
233     
234     	return written? written : result;
235     }
236     
237     static int
238     nfs_writepage_async(struct file *file, struct inode *inode, struct page *page,
239     		    unsigned int offset, unsigned int count)
240     {
241     	struct nfs_page	*req;
242     	int		status;
243     
244     	req = nfs_update_request(file, inode, page, offset, count);
245     	status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
246     	if (status < 0)
247     		goto out;
248     	if (!req->wb_cred)
249     		req->wb_cred = get_rpccred(NFS_I(inode)->mm_cred);
250     	nfs_unlock_request(req);
251     	nfs_release_request(req);
252     	nfs_strategy(inode);
253      out:
254     	return status;
255     }
256     
257     /*
258      * Write an mmapped page to the server.
259      */
260     int
261     nfs_writepage(struct page *page)
262     {
263     	struct inode *inode;
264     	unsigned long end_index;
265     	unsigned offset = PAGE_CACHE_SIZE;
266     	int err;
267     	struct address_space *mapping = page->mapping;
268     
269     	if (!mapping)
270     		BUG();
271     	inode = mapping->host;
272     	if (!inode)
273     		BUG();
274     	end_index = inode->i_size >> PAGE_CACHE_SHIFT;
275     
276     	/* Ensure we've flushed out any previous writes */
277     	nfs_wb_page(inode,page);
278     
279     	/* easy case */
280     	if (page->index < end_index)
281     		goto do_it;
282     	/* things got complicated... */
283     	offset = inode->i_size & (PAGE_CACHE_SIZE-1);
284     
285     	/* OK, are we completely out? */
286     	err = -EIO;
287     	if (page->index >= end_index+1 || !offset)
288     		goto out;
289     do_it:
290     	lock_kernel();
291     	if (NFS_SERVER(inode)->rsize >= PAGE_CACHE_SIZE) {
292     		err = nfs_writepage_async(NULL, inode, page, 0, offset);
293     		if (err >= 0)
294     			err = 0;
295     	} else {
296     		err = nfs_writepage_sync(NULL, inode, page, 0, offset); 
297     		if (err == offset)
298     			err = 0;
299     	}
300     	unlock_kernel();
301     out:
302     	UnlockPage(page);
303     	return err; 
304     }
305     
306     /*
307      * Check whether the file range we want to write to is locked by
308      * us.
309      */
310     static int
311     region_locked(struct inode *inode, struct nfs_page *req)
312     {
313     	struct file_lock	*fl;
314     	loff_t			rqstart, rqend;
315     
316     	/* Don't optimize writes if we don't use NLM */
317     	if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
318     		return 0;
319     
320     	rqstart = page_offset(req->wb_page) + req->wb_offset;
321     	rqend = rqstart + req->wb_bytes;
322     	for (fl = inode->i_flock; fl; fl = fl->fl_next) {
323     		if (fl->fl_owner == current->files && (fl->fl_flags & FL_POSIX)
324     		    && fl->fl_type == F_WRLCK
325     		    && fl->fl_start <= rqstart && rqend <= fl->fl_end) {
326     			return 1;
327     		}
328     	}
329     
330     	return 0;
331     }
332     
333     /*
334      * Insert a write request into an inode
335      */
336     static inline void
337     nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
338     {
339     	if (!list_empty(&req->wb_hash))
340     		return;
341     	if (!NFS_WBACK_BUSY(req))
342     		printk(KERN_ERR "NFS: unlocked request attempted hashed!\n");
343     	if (list_empty(&inode->u.nfs_i.writeback))
344     		atomic_inc(&inode->i_count);
345     	inode->u.nfs_i.npages++;
346     	list_add(&req->wb_hash, &inode->u.nfs_i.writeback);
347     	req->wb_count++;
348     }
349     
350     /*
351      * Insert a write request into an inode
352      */
353     static inline void
354     nfs_inode_remove_request(struct nfs_page *req)
355     {
356     	struct inode *inode;
357     	spin_lock(&nfs_wreq_lock);
358     	if (list_empty(&req->wb_hash)) {
359     		spin_unlock(&nfs_wreq_lock);
360     		return;
361     	}
362     	if (!NFS_WBACK_BUSY(req))
363     		printk(KERN_ERR "NFS: unlocked request attempted unhashed!\n");
364     	inode = req->wb_inode;
365     	list_del(&req->wb_hash);
366     	INIT_LIST_HEAD(&req->wb_hash);
367     	inode->u.nfs_i.npages--;
368     	if ((inode->u.nfs_i.npages == 0) != list_empty(&inode->u.nfs_i.writeback))
369     		printk(KERN_ERR "NFS: desynchronized value of nfs_i.npages.\n");
370     	if (list_empty(&inode->u.nfs_i.writeback))
371     		iput(inode);
372     	if (!nfs_have_writebacks(inode) && !nfs_have_read(inode))
373     		inode_remove_flushd(inode);
374     	spin_unlock(&nfs_wreq_lock);
375     	nfs_release_request(req);
376     }
377     
378     /*
379      * Find a request
380      */
381     static inline struct nfs_page *
382     _nfs_find_request(struct inode *inode, struct page *page)
383     {
384     	struct list_head	*head, *next;
385     
386     	head = &inode->u.nfs_i.writeback;
387     	next = head->next;
388     	while (next != head) {
389     		struct nfs_page *req = nfs_inode_wb_entry(next);
390     		next = next->next;
391     		if (page_index(req->wb_page) != page_index(page))
392     			continue;
393     		req->wb_count++;
394     		return req;
395     	}
396     	return NULL;
397     }
398     
399     static struct nfs_page *
400     nfs_find_request(struct inode *inode, struct page *page)
401     {
402     	struct nfs_page		*req;
403     
404     	spin_lock(&nfs_wreq_lock);
405     	req = _nfs_find_request(inode, page);
406     	spin_unlock(&nfs_wreq_lock);
407     	return req;
408     }
409     
410     /*
411      * Insert a write request into a sorted list
412      */
413     void nfs_list_add_request(struct nfs_page *req, struct list_head *head)
414     {
415     	struct list_head *prev;
416     
417     	if (!list_empty(&req->wb_list)) {
418     		printk(KERN_ERR "NFS: Add to list failed!\n");
419     		return;
420     	}
421     	if (!NFS_WBACK_BUSY(req))
422     		printk(KERN_ERR "NFS: unlocked request attempted added to list!\n");
423     	prev = head->prev;
424     	while (prev != head) {
425     		struct nfs_page	*p = nfs_list_entry(prev);
426     		if (page_index(p->wb_page) < page_index(req->wb_page))
427     			break;
428     		prev = prev->prev;
429     	}
430     	list_add(&req->wb_list, prev);
431     	req->wb_list_head = head;
432     }
433     
434     /*
435      * Insert a write request into an inode
436      */
437     void nfs_list_remove_request(struct nfs_page *req)
438     {
439     	if (list_empty(&req->wb_list))
440     		return;
441     	if (!NFS_WBACK_BUSY(req))
442     		printk(KERN_ERR "NFS: unlocked request attempted removed from list!\n");
443     	list_del(&req->wb_list);
444     	INIT_LIST_HEAD(&req->wb_list);
445     	req->wb_list_head = NULL;
446     }
447     
448     /*
449      * Add a request to the inode's dirty list.
450      */
451     static inline void
452     nfs_mark_request_dirty(struct nfs_page *req)
453     {
454     	struct inode *inode = req->wb_inode;
455     
456     	spin_lock(&nfs_wreq_lock);
457     	if (list_empty(&req->wb_list)) {
458     		nfs_list_add_request(req, &inode->u.nfs_i.dirty);
459     		inode->u.nfs_i.ndirty++;
460     	}
461     	spin_unlock(&nfs_wreq_lock);
462     	/*
463     	 * NB: the call to inode_schedule_scan() must lie outside the
464     	 *     spinlock since it can run flushd().
465     	 */
466     	inode_schedule_scan(inode, req->wb_timeout);
467     	mark_inode_dirty(inode);
468     }
469     
470     /*
471      * Check if a request is dirty
472      */
473     static inline int
474     nfs_dirty_request(struct nfs_page *req)
475     {
476     	struct inode *inode = req->wb_inode;
477     	return !list_empty(&req->wb_list) && req->wb_list_head == &inode->u.nfs_i.dirty;
478     }
479     
480     #ifdef CONFIG_NFS_V3
481     /*
482      * Add a request to the inode's commit list.
483      */
484     static inline void
485     nfs_mark_request_commit(struct nfs_page *req)
486     {
487     	struct inode *inode = req->wb_inode;
488     
489     	spin_lock(&nfs_wreq_lock);
490     	if (list_empty(&req->wb_list)) {
491     		nfs_list_add_request(req, &inode->u.nfs_i.commit);
492     		inode->u.nfs_i.ncommit++;
493     	}
494     	spin_unlock(&nfs_wreq_lock);
495     	/*
496     	 * NB: the call to inode_schedule_scan() must lie outside the
497     	 *     spinlock since it can run flushd().
498     	 */
499     	inode_schedule_scan(inode, req->wb_timeout);
500     	mark_inode_dirty(inode);
501     }
502     #endif
503     
504     /*
505      * Create a write request.
506      * Page must be locked by the caller. This makes sure we never create
507      * two different requests for the same page, and avoids possible deadlock
508      * when we reach the hard limit on the number of dirty pages.
509      * It should be safe to sleep here.
510      */
511     struct nfs_page *nfs_create_request(struct file *file, struct inode *inode,
512     				    struct page *page,
513     				    unsigned int offset, unsigned int count)
514     {
515     	struct nfs_reqlist	*cache = NFS_REQUESTLIST(inode);
516     	struct nfs_page		*req = NULL;
517     	long			timeout;
518     
519     	/* Deal with hard/soft limits.
520     	 */
521     	do {
522     		/* If we're over the global soft limit, wake up all requests */
523     		if (atomic_read(&nfs_nr_requests) >= MAX_REQUEST_SOFT) {
524     			dprintk("NFS:      hit soft limit (%d requests)\n",
525     				atomic_read(&nfs_nr_requests));
526     			if (!cache->task)
527     				nfs_reqlist_init(NFS_SERVER(inode));
528     			nfs_wake_flushd();
529     		}
530     
531     		/* If we haven't reached the local hard limit yet,
532     		 * try to allocate the request struct */
533     		if (atomic_read(&cache->nr_requests) < MAX_REQUEST_HARD) {
534     			req = nfs_page_alloc();
535     			if (req != NULL)
536     				break;
537     		}
538     
539     		/* We're over the hard limit. Wait for better times */
540     		dprintk("NFS:      create_request sleeping (total %d pid %d)\n",
541     			atomic_read(&cache->nr_requests), current->pid);
542     
543     		timeout = 1 * HZ;
544     		if (NFS_SERVER(inode)->flags & NFS_MOUNT_INTR) {
545     			interruptible_sleep_on_timeout(&cache->request_wait,
546     						       timeout);
547     			if (signalled())
548     				break;
549     		} else
550     			sleep_on_timeout(&cache->request_wait, timeout);
551     
552     		dprintk("NFS:      create_request waking up (tot %d pid %d)\n",
553     			atomic_read(&cache->nr_requests), current->pid);
554     	} while (!req);
555     	if (!req)
556     		return NULL;
557     
558     	/* Initialize the request struct. Initially, we assume a
559     	 * long write-back delay. This will be adjusted in
560     	 * update_nfs_request below if the region is not locked. */
561     	req->wb_page    = page;
562     	page_cache_get(page);
563     	req->wb_offset  = offset;
564     	req->wb_bytes   = count;
565     	req->wb_file    = file;
566     
567     	/* If we have a struct file, use its cached credentials */
568     	if (file) {
569     		get_file(file);
570     		req->wb_cred	= nfs_file_cred(file);
571     	}
572     	req->wb_inode   = inode;
573     	req->wb_count   = 1;
574     
575     	/* register request's existence */
576     	atomic_inc(&cache->nr_requests);
577     	atomic_inc(&nfs_nr_requests);
578     	return req;
579     }
580     
581     
582     /*
583      * Release all resources associated with a write request after it
584      * has been committed to stable storage
585      *
586      * Note: Should always be called with the spinlock held!
587      */
588     void
589     nfs_release_request(struct nfs_page *req)
590     {
591     	struct inode		*inode = req->wb_inode;
592     	struct nfs_reqlist	*cache = NFS_REQUESTLIST(inode);
593     	struct page		*page = req->wb_page;
594     
595     	spin_lock(&nfs_wreq_lock);
596     	if (--req->wb_count) {
597     		spin_unlock(&nfs_wreq_lock);
598     		return;
599     	}
600     	spin_unlock(&nfs_wreq_lock);
601     
602     	if (!list_empty(&req->wb_list)) {
603     		printk(KERN_ERR "NFS: Request released while still on a list!\n");
604     		nfs_list_remove_request(req);
605     	}
606     	if (!list_empty(&req->wb_hash)) {
607     		printk(KERN_ERR "NFS: Request released while still hashed!\n");
608     		nfs_inode_remove_request(req);
609     	}
610     	if (NFS_WBACK_BUSY(req))
611     		printk(KERN_ERR "NFS: Request released while still locked!\n");
612     
613     	/* Release struct file or cached credential */
614     	if (req->wb_file)
615     		fput(req->wb_file);
616     	else if (req->wb_cred)
617     		put_rpccred(req->wb_cred);
618     	page_cache_release(page);
619     	nfs_page_free(req);
620     	/* wake up anyone waiting to allocate a request */
621     	atomic_dec(&cache->nr_requests);
622     	atomic_dec(&nfs_nr_requests);
623     	wake_up(&cache->request_wait);
624     #ifdef NFS_PARANOIA
625     	if (atomic_read(&cache->nr_requests) < 0)
626     		BUG();
627     	if (atomic_read(&nfs_nr_requests) < 0)
628     		BUG();
629     #endif
630     }
631     
632     /*
633      * Wait for a request to complete.
634      *
635      * Interruptible by signals only if mounted with intr flag.
636      */
637     static int
638     nfs_wait_on_request(struct nfs_page *req)
639     {
640     	struct inode	*inode = req->wb_inode;
641             struct rpc_clnt	*clnt = NFS_CLIENT(inode);
642     
643     	if (!NFS_WBACK_BUSY(req))
644     		return 0;
645     	return nfs_wait_event(clnt, req->wb_wait, !NFS_WBACK_BUSY(req));
646     }
647     
648     /*
649      * Wait for a request to complete.
650      *
651      * Interruptible by signals only if mounted with intr flag.
652      */
653     static int
654     nfs_wait_on_requests(struct inode *inode, struct file *file, unsigned long idx_start, unsigned int npages)
655     {
656     	struct list_head	*p, *head;
657     	unsigned long		idx_end;
658     	unsigned int		res = 0;
659     	int			error;
660     
661     	if (npages == 0)
662     		idx_end = ~0;
663     	else
664     		idx_end = idx_start + npages - 1;
665     
666     	spin_lock(&nfs_wreq_lock);
667     	head = &inode->u.nfs_i.writeback;
668     	p = head->next;
669     	while (p != head) {
670     		unsigned long pg_idx;
671     		struct nfs_page *req = nfs_inode_wb_entry(p);
672     
673     		p = p->next;
674     
675     		if (file && req->wb_file != file)
676     			continue;
677     
678     		pg_idx = page_index(req->wb_page);
679     		if (pg_idx < idx_start || pg_idx > idx_end)
680     			continue;
681     
682     		if (!NFS_WBACK_BUSY(req))
683     			continue;
684     		req->wb_count++;
685     		spin_unlock(&nfs_wreq_lock);
686     		error = nfs_wait_on_request(req);
687     		nfs_release_request(req);
688     		if (error < 0)
689     			return error;
690     		spin_lock(&nfs_wreq_lock);
691     		p = head->next;
692     		res++;
693     	}
694     	spin_unlock(&nfs_wreq_lock);
695     	return res;
696     }
697     
698     /*
699      * Scan cluster for dirty pages and send as many of them to the
700      * server as possible.
701      */
702     int nfs_scan_list_timeout(struct list_head *head, struct list_head *dst, struct inode *inode)
703     {
704     	struct list_head	*p;
705             struct nfs_page		*req;
706             int			pages = 0;
707     
708     	p = head->next;
709             while (p != head) {
710     		req = nfs_list_entry(p);
711     		p = p->next;
712     		if (time_after(req->wb_timeout, jiffies)) {
713     			if (time_after(NFS_NEXTSCAN(inode), req->wb_timeout))
714     				NFS_NEXTSCAN(inode) = req->wb_timeout;
715     			continue;
716     		}
717     		if (!nfs_lock_request(req))
718     			continue;
719     		nfs_list_remove_request(req);
720     		nfs_list_add_request(req, dst);
721     		pages++;
722     	}
723     	return pages;
724     }
725     
726     static int
727     nfs_scan_dirty_timeout(struct inode *inode, struct list_head *dst)
728     {
729     	int	pages;
730     	spin_lock(&nfs_wreq_lock);
731     	pages = nfs_scan_list_timeout(&inode->u.nfs_i.dirty, dst, inode);
732     	inode->u.nfs_i.ndirty -= pages;
733     	if ((inode->u.nfs_i.ndirty == 0) != list_empty(&inode->u.nfs_i.dirty))
734     		printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
735     	spin_unlock(&nfs_wreq_lock);
736     	return pages;
737     }
738     
739     #ifdef CONFIG_NFS_V3
740     static int
741     nfs_scan_commit_timeout(struct inode *inode, struct list_head *dst)
742     {
743     	int	pages;
744     	spin_lock(&nfs_wreq_lock);
745     	pages = nfs_scan_list_timeout(&inode->u.nfs_i.commit, dst, inode);
746     	inode->u.nfs_i.ncommit -= pages;
747     	if ((inode->u.nfs_i.ncommit == 0) != list_empty(&inode->u.nfs_i.commit))
748     		printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
749     	spin_unlock(&nfs_wreq_lock);
750     	return pages;
751     }
752     #endif
753     
754     int nfs_scan_list(struct list_head *src, struct list_head *dst, struct file *file, unsigned long idx_start, unsigned int npages)
755     {
756     	struct list_head	*p;
757     	struct nfs_page		*req;
758     	unsigned long		idx_end;
759     	int			res;
760     
761     	res = 0;
762     	if (npages == 0)
763     		idx_end = ~0;
764     	else
765     		idx_end = idx_start + npages - 1;
766     	p = src->next;
767     	while (p != src) {
768     		unsigned long pg_idx;
769     
770     		req = nfs_list_entry(p);
771     		p = p->next;
772     
773     		if (file && req->wb_file != file)
774     			continue;
775     
776     		pg_idx = page_index(req->wb_page);
777     		if (pg_idx < idx_start || pg_idx > idx_end)
778     			continue;
779     
780     		if (!nfs_lock_request(req))
781     			continue;
782     		nfs_list_remove_request(req);
783     		nfs_list_add_request(req, dst);
784     		res++;
785     	}
786     	return res;
787     }
788     
789     static int
790     nfs_scan_dirty(struct inode *inode, struct list_head *dst, struct file *file, unsigned long idx_start, unsigned int npages)
791     {
792     	int	res;
793     	spin_lock(&nfs_wreq_lock);
794     	res = nfs_scan_list(&inode->u.nfs_i.dirty, dst, file, idx_start, npages);
795     	inode->u.nfs_i.ndirty -= res;
796     	if ((inode->u.nfs_i.ndirty == 0) != list_empty(&inode->u.nfs_i.dirty))
797     		printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
798     	spin_unlock(&nfs_wreq_lock);
799     	return res;
800     }
801     
802     #ifdef CONFIG_NFS_V3
803     static int
804     nfs_scan_commit(struct inode *inode, struct list_head *dst, struct file *file, unsigned long idx_start, unsigned int npages)
805     {
806     	int	res;
807     	spin_lock(&nfs_wreq_lock);
808     	res = nfs_scan_list(&inode->u.nfs_i.commit, dst, file, idx_start, npages);
809     	inode->u.nfs_i.ncommit -= res;
810     	if ((inode->u.nfs_i.ncommit == 0) != list_empty(&inode->u.nfs_i.commit))
811     		printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
812     	spin_unlock(&nfs_wreq_lock);
813     	return res;
814     }
815     #endif
816     
817     
818     int nfs_coalesce_requests(struct list_head *src, struct list_head *dst, unsigned int maxpages)
819     {
820     	struct nfs_page		*req = NULL;
821     	unsigned int		pages = 0;
822     
823     	while (!list_empty(src)) {
824     		struct nfs_page	*prev = req;
825     
826     		req = nfs_list_entry(src->next);
827     		if (prev) {
828     			if (req->wb_file != prev->wb_file)
829     				break;
830     			if (page_index(req->wb_page) != page_index(prev->wb_page)+1)
831     				break;
832     
833     			if (req->wb_offset != 0)
834     				break;
835     		}
836     		nfs_list_remove_request(req);
837     		nfs_list_add_request(req, dst);
838     		pages++;
839     		if (req->wb_offset + req->wb_bytes != PAGE_CACHE_SIZE)
840     			break;
841     		if (pages >= maxpages)
842     			break;
843     	}
844     	return pages;
845     }
846     
847     /*
848      * Try to update any existing write request, or create one if there is none.
849      * In order to match, the request's credentials must match those of
850      * the calling process.
851      *
852      * Note: Should always be called with the Page Lock held!
853      */
854     static struct nfs_page *
855     nfs_update_request(struct file* file, struct inode *inode, struct page *page,
856     		   unsigned int offset, unsigned int bytes)
857     {
858     	struct nfs_page		*req, *new = NULL;
859     	unsigned long		rqend, end;
860     
861     	end = offset + bytes;
862     
863     	for (;;) {
864     		/* Loop over all inode entries and see if we find
865     		 * A request for the page we wish to update
866     		 */
867     		spin_lock(&nfs_wreq_lock);
868     		req = _nfs_find_request(inode, page);
869     		if (req) {
870     			if (!nfs_lock_request(req)) {
871     				int error;
872     				spin_unlock(&nfs_wreq_lock);
873     				error = nfs_wait_on_request(req);
874     				nfs_release_request(req);
875     				if (error < 0)
876     					return ERR_PTR(error);
877     				continue;
878     			}
879     			spin_unlock(&nfs_wreq_lock);
880     			if (new)
881     				nfs_release_request(new);
882     			break;
883     		}
884     
885     		req = new;
886     		if (req) {
887     			nfs_lock_request(req);
888     			nfs_inode_add_request(inode, req);
889     			spin_unlock(&nfs_wreq_lock);
890     			nfs_mark_request_dirty(req);
891     			break;
892     		}
893     		spin_unlock(&nfs_wreq_lock);
894     
895     		/*
896     		 * If we're over the soft limit, flush out old requests
897     		 */
898     		if (inode->u.nfs_i.npages >= MAX_REQUEST_SOFT)
899     			nfs_wb_file(inode, file);
900     		new = nfs_create_request(file, inode, page, offset, bytes);
901     		if (!new)
902     			return ERR_PTR(-ENOMEM);
903     		/* If the region is locked, adjust the timeout */
904     		if (region_locked(inode, new))
905     			new->wb_timeout = jiffies + NFS_WRITEBACK_LOCKDELAY;
906     		else
907     			new->wb_timeout = jiffies + NFS_WRITEBACK_DELAY;
908     	}
909     
910     	/* We have a request for our page.
911     	 * If the creds don't match, or the
912     	 * page addresses don't match,
913     	 * tell the caller to wait on the conflicting
914     	 * request.
915     	 */
916     	rqend = req->wb_offset + req->wb_bytes;
917     	if (req->wb_file != file
918     	    || req->wb_page != page
919     	    || !nfs_dirty_request(req)
920     	    || offset > rqend || end < req->wb_offset) {
921     		nfs_unlock_request(req);
922     		nfs_release_request(req);
923     		return ERR_PTR(-EBUSY);
924     	}
925     
926     	/* Okay, the request matches. Update the region */
927     	if (offset < req->wb_offset) {
928     		req->wb_offset = offset;
929     		req->wb_bytes = rqend - req->wb_offset;
930     	}
931     
932     	if (end > rqend)
933     		req->wb_bytes = end - req->wb_offset;
934     
935     	return req;
936     }
937     
938     /*
939      * This is the strategy routine for NFS.
940      * It is called by nfs_updatepage whenever the user wrote up to the end
941      * of a page.
942      *
943      * We always try to submit a set of requests in parallel so that the
944      * server's write code can gather writes. This is mainly for the benefit
945      * of NFSv2.
946      *
947      * We never submit more requests than we think the remote can handle.
948      * For UDP sockets, we make sure we don't exceed the congestion window;
949      * for TCP, we limit the number of requests to 8.
950      *
951      * NFS_STRATEGY_PAGES gives the minimum number of requests for NFSv2 that
952      * should be sent out in one go. This is for the benefit of NFSv2 servers
953      * that perform write gathering.
954      *
955      * FIXME: Different servers may have different sweet spots.
956      * Record the average congestion window in server struct?
957      */
958     #define NFS_STRATEGY_PAGES      8
959     static void
960     nfs_strategy(struct inode *inode)
961     {
962     	unsigned int	dirty, wpages;
963     
964     	dirty  = inode->u.nfs_i.ndirty;
965     	wpages = NFS_SERVER(inode)->wpages;
966     #ifdef CONFIG_NFS_V3
967     	if (NFS_PROTO(inode)->version == 2) {
968     		if (dirty >= NFS_STRATEGY_PAGES * wpages)
969     			nfs_flush_file(inode, NULL, 0, 0, 0);
970     	} else {
971     		if (dirty >= wpages)
972     			nfs_flush_file(inode, NULL, 0, 0, 0);
973     		if (inode->u.nfs_i.ncommit > NFS_STRATEGY_PAGES * wpages &&
974     		    atomic_read(&nfs_nr_requests) > MAX_REQUEST_SOFT)
975     			nfs_commit_file(inode, NULL, 0, 0, 0);
976     	}
977     #else
978     	if (dirty >= NFS_STRATEGY_PAGES * wpages)
979     		nfs_flush_file(inode, NULL, 0, 0, 0);
980     #endif
981     	/*
982     	 * If we're running out of free requests, flush out everything
983     	 * in order to reduce memory useage...
984     	 */
985     	if (inode->u.nfs_i.npages > MAX_REQUEST_SOFT)
986     		nfs_wb_all(inode);
987     }
988     
989     int
990     nfs_flush_incompatible(struct file *file, struct page *page)
991     {
992     	struct inode	*inode = file->f_dentry->d_inode;
993     	struct nfs_page	*req;
994     	int		status = 0;
995     	/*
996     	 * Look for a request corresponding to this page. If there
997     	 * is one, and it belongs to another file, we flush it out
998     	 * before we try to copy anything into the page. Do this
999     	 * due to the lack of an ACCESS-type call in NFSv2.
1000     	 * Also do the same if we find a request from an existing
1001     	 * dropped page.
1002     	 */
1003     	req = nfs_find_request(inode,page);
1004     	if (req) {
1005     		if (req->wb_file != file || req->wb_page != page)
1006     			status = nfs_wb_page(inode, page);
1007     		nfs_release_request(req);
1008     	}
1009     	return (status < 0) ? status : 0;
1010     }
1011     
1012     /*
1013      * Update and possibly write a cached page of an NFS file.
1014      *
1015      * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
1016      * things with a page scheduled for an RPC call (e.g. invalidate it).
1017      */
1018     int
1019     nfs_updatepage(struct file *file, struct page *page, unsigned int offset, unsigned int count)
1020     {
1021     	struct dentry	*dentry = file->f_dentry;
1022     	struct inode	*inode = dentry->d_inode;
1023     	struct nfs_page	*req;
1024     	int		status = 0;
1025     
1026     	dprintk("NFS:      nfs_updatepage(%s/%s %d@%Ld)\n",
1027     		dentry->d_parent->d_name.name, dentry->d_name.name,
1028     		count, (long long)(page_offset(page) +offset));
1029     
1030     	/*
1031     	 * If wsize is smaller than page size, update and write
1032     	 * page synchronously.
1033     	 */
1034     	if (NFS_SERVER(inode)->wsize < PAGE_SIZE)
1035     		return nfs_writepage_sync(file, inode, page, offset, count);
1036     
1037     	/*
1038     	 * Try to find an NFS request corresponding to this page
1039     	 * and update it.
1040     	 * If the existing request cannot be updated, we must flush
1041     	 * it out now.
1042     	 */
1043     	do {
1044     		req = nfs_update_request(file, inode, page, offset, count);
1045     		status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
1046     		if (status != -EBUSY)
1047     			break;
1048     		/* Request could not be updated. Flush it out and try again */
1049     		status = nfs_wb_page(inode, page);
1050     	} while (status >= 0);
1051     	if (status < 0)
1052     		goto done;
1053     
1054     	status = 0;
1055     	nfs_unlock_request(req);
1056     	/* If we wrote past the end of the page.
1057     	 * Call the strategy routine so it can send out a bunch
1058     	 * of requests.
1059     	 */
1060     	if (req->wb_offset == 0 && req->wb_bytes == PAGE_CACHE_SIZE) {
1061     		SetPageUptodate(page);
1062     		nfs_strategy(inode);
1063     	}
1064     	nfs_release_request(req);
1065     done:
1066             dprintk("NFS:      nfs_updatepage returns %d (isize %Ld)\n",
1067                                                     status, (long long)inode->i_size);
1068     	if (status < 0)
1069     		ClearPageUptodate(page);
1070     	return status;
1071     }
1072     
1073     /*
1074      * Set up the argument/result storage required for the RPC call.
1075      */
1076     static void
1077     nfs_write_rpcsetup(struct list_head *head, struct nfs_write_data *data)
1078     {
1079     	struct nfs_page		*req;
1080     	struct iovec		*iov;
1081     	unsigned int		count;
1082     
1083     	/* Set up the RPC argument and reply structs
1084     	 * NB: take care not to mess about with data->commit et al. */
1085     
1086     	iov = data->args.iov;
1087     	count = 0;
1088     	while (!list_empty(head)) {
1089     		struct nfs_page *req = nfs_list_entry(head->next);
1090     		nfs_list_remove_request(req);
1091     		nfs_list_add_request(req, &data->pages);
1092     		iov->iov_base = kmap(req->wb_page) + req->wb_offset;
1093     		iov->iov_len = req->wb_bytes;
1094     		count += req->wb_bytes;
1095     		iov++;
1096     		data->args.nriov++;
1097     	}
1098     	req = nfs_list_entry(data->pages.next);
1099     	data->inode = req->wb_inode;
1100     	data->cred = req->wb_cred;
1101     	data->args.fh     = NFS_FH(req->wb_inode);
1102     	data->args.offset = page_offset(req->wb_page) + req->wb_offset;
1103     	data->args.count  = count;
1104     	data->res.fattr   = &data->fattr;
1105     	data->res.count   = count;
1106     	data->res.verf    = &data->verf;
1107     }
1108     
1109     
1110     /*
1111      * Create an RPC task for the given write request and kick it.
1112      * The page must have been locked by the caller.
1113      *
1114      * It may happen that the page we're passed is not marked dirty.
1115      * This is the case if nfs_updatepage detects a conflicting request
1116      * that has been written but not committed.
1117      */
1118     static int
1119     nfs_flush_one(struct list_head *head, struct inode *inode, int how)
1120     {
1121     	struct rpc_clnt 	*clnt = NFS_CLIENT(inode);
1122     	struct nfs_write_data	*data;
1123     	struct rpc_task		*task;
1124     	struct rpc_message	msg;
1125     	int                     flags,
1126     				async = !(how & FLUSH_SYNC),
1127     				stable = (how & FLUSH_STABLE);
1128     	sigset_t		oldset;
1129     
1130     
1131     	data = nfs_writedata_alloc();
1132     	if (!data)
1133     		goto out_bad;
1134     	task = &data->task;
1135     
1136     	/* Set the initial flags for the task.  */
1137     	flags = (async) ? RPC_TASK_ASYNC : 0;
1138     
1139     	/* Set up the argument struct */
1140     	nfs_write_rpcsetup(head, data);
1141     	if (stable) {
1142     		if (!inode->u.nfs_i.ncommit)
1143     			data->args.stable = NFS_FILE_SYNC;
1144     		else
1145     			data->args.stable = NFS_DATA_SYNC;
1146     	} else
1147     		data->args.stable = NFS_UNSTABLE;
1148     
1149     	/* Finalize the task. */
1150     	rpc_init_task(task, clnt, nfs_writeback_done, flags);
1151     	task->tk_calldata = data;
1152     	/* Release requests */
1153     	task->tk_release = nfs_writedata_release;
1154     
1155     #ifdef CONFIG_NFS_V3
1156     	msg.rpc_proc = (NFS_PROTO(inode)->version == 3) ? NFS3PROC_WRITE : NFSPROC_WRITE;
1157     #else
1158     	msg.rpc_proc = NFSPROC_WRITE;
1159     #endif
1160     	msg.rpc_argp = &data->args;
1161     	msg.rpc_resp = &data->res;
1162     	msg.rpc_cred = data->cred;
1163     
1164     	dprintk("NFS: %4d initiated write call (req %x/%Ld count %d nriov %d)\n",
1165     		task->tk_pid, 
1166     		inode->i_dev,
1167     		(long long)NFS_FILEID(inode),
1168     		data->args.count, data->args.nriov);
1169     
1170     	rpc_clnt_sigmask(clnt, &oldset);
1171     	rpc_call_setup(task, &msg, 0);
1172     	lock_kernel();
1173     	rpc_execute(task);
1174     	unlock_kernel();
1175     	rpc_clnt_sigunmask(clnt, &oldset);
1176     	return 0;
1177      out_bad:
1178     	while (!list_empty(head)) {
1179     		struct nfs_page *req = nfs_list_entry(head->next);
1180     		nfs_list_remove_request(req);
1181     		nfs_mark_request_dirty(req);
1182     		nfs_unlock_request(req);
1183     	}
1184     	return -ENOMEM;
1185     }
1186     
1187     static int
1188     nfs_flush_list(struct inode *inode, struct list_head *head, int how)
1189     {
1190     	LIST_HEAD(one_request);
1191     	struct nfs_page		*req;
1192     	int			error = 0;
1193     	unsigned int		pages = 0,
1194     				wpages = NFS_SERVER(inode)->wpages;
1195     
1196     	while (!list_empty(head)) {
1197     		pages += nfs_coalesce_requests(head, &one_request, wpages);
1198     		req = nfs_list_entry(one_request.next);
1199     		error = nfs_flush_one(&one_request, req->wb_inode, how);
1200     		if (error < 0)
1201     			break;
1202     	}
1203     	if (error >= 0)
1204     		return pages;
1205     
1206     	while (!list_empty(head)) {
1207     		req = nfs_list_entry(head->next);
1208     		nfs_list_remove_request(req);
1209     		nfs_mark_request_dirty(req);
1210     		nfs_unlock_request(req);
1211     	}
1212     	return error;
1213     }
1214     
1215     
1216     /*
1217      * This function is called when the WRITE call is complete.
1218      */
1219     static void
1220     nfs_writeback_done(struct rpc_task *task)
1221     {
1222     	struct nfs_write_data	*data = (struct nfs_write_data *) task->tk_calldata;
1223     	struct nfs_writeargs	*argp = &data->args;
1224     	struct nfs_writeres	*resp = &data->res;
1225     	struct inode		*inode = data->inode;
1226     	struct nfs_page		*req;
1227     	struct page		*page;
1228     
1229     	dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1230     		task->tk_pid, task->tk_status);
1231     
1232     	/* We can't handle that yet but we check for it nevertheless */
1233     	if (resp->count < argp->count && task->tk_status >= 0) {
1234     		static unsigned long    complain;
1235     		if (time_before(complain, jiffies)) {
1236     			printk(KERN_WARNING
1237     			       "NFS: Server wrote less than requested.\n");
1238     			complain = jiffies + 300 * HZ;
1239     		}
1240     		/* Can't do anything about it right now except throw
1241     		 * an error. */
1242     		task->tk_status = -EIO;
1243     	}
1244     #ifdef CONFIG_NFS_V3
1245     	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1246     		/* We tried a write call, but the server did not
1247     		 * commit data to stable storage even though we
1248     		 * requested it.
1249     		 * Note: There is a known bug in Tru64 < 5.0 in which
1250     		 *	 the server reports NFS_DATA_SYNC, but performs
1251     		 *	 NFS_FILE_SYNC. We therefore implement this checking
1252     		 *	 as a dprintk() in order to avoid filling syslog.
1253     		 */
1254     		static unsigned long    complain;
1255     
1256     		if (time_before(complain, jiffies)) {
1257     			dprintk("NFS: faulty NFSv3 server %s:"
1258     				" (committed = %d) != (stable = %d)\n",
1259     				NFS_SERVER(inode)->hostname,
1260     				resp->verf->committed, argp->stable);
1261     			complain = jiffies + 300 * HZ;
1262     		}
1263     	}
1264     #endif
1265     
1266     	/*
1267     	 * Update attributes as result of writeback.
1268     	 * FIXME: There is an inherent race with invalidate_inode_pages and
1269     	 *	  writebacks since the page->count is kept > 1 for as long
1270     	 *	  as the page has a write request pending.
1271     	 */
1272     	nfs_write_attributes(inode, resp->fattr);
1273     	while (!list_empty(&data->pages)) {
1274     		req = nfs_list_entry(data->pages.next);
1275     		nfs_list_remove_request(req);
1276     		page = req->wb_page;
1277     
1278     		kunmap(page);
1279     
1280     		dprintk("NFS: write (%x/%Ld %d@%Ld)",
1281     			req->wb_inode->i_dev,
1282     			(long long)NFS_FILEID(req->wb_inode),
1283     			req->wb_bytes,
1284     			(long long)(page_offset(page) + req->wb_offset));
1285     
1286     		if (task->tk_status < 0) {
1287     			ClearPageUptodate(page);
1288     			SetPageError(page);
1289     			if (req->wb_file)
1290     				req->wb_file->f_error = task->tk_status;
1291     			nfs_inode_remove_request(req);
1292     			dprintk(", error = %d\n", task->tk_status);
1293     			goto next;
1294     		}
1295     
1296     #ifdef CONFIG_NFS_V3
1297     		if (resp->verf->committed != NFS_UNSTABLE) {
1298     			nfs_inode_remove_request(req);
1299     			dprintk(" OK\n");
1300     			goto next;
1301     		}
1302     		memcpy(&req->wb_verf, resp->verf, sizeof(req->wb_verf));
1303     		req->wb_timeout = jiffies + NFS_COMMIT_DELAY;
1304     		nfs_mark_request_commit(req);
1305     		dprintk(" marked for commit\n");
1306     #else
1307     		nfs_inode_remove_request(req);
1308     #endif
1309     	next:
1310     		nfs_unlock_request(req);
1311     	}
1312     }
1313     
1314     
1315     #ifdef CONFIG_NFS_V3
1316     /*
1317      * Set up the argument/result storage required for the RPC call.
1318      */
1319     static void
1320     nfs_commit_rpcsetup(struct list_head *head, struct nfs_write_data *data)
1321     {
1322     	struct nfs_page		*first, *last;
1323     	struct inode		*inode;
1324     	loff_t			start, end, len;
1325     
1326     	/* Set up the RPC argument and reply structs
1327     	 * NB: take care not to mess about with data->commit et al. */
1328     
1329     	list_splice(head, &data->pages);
1330     	INIT_LIST_HEAD(head);
1331     	first = nfs_list_entry(data->pages.next);
1332     	last = nfs_list_entry(data->pages.prev);
1333     	inode = first->wb_inode;
1334     
1335     	/*
1336     	 * Determine the offset range of requests in the COMMIT call.
1337     	 * We rely on the fact that data->pages is an ordered list...
1338     	 */
1339     	start = page_offset(first->wb_page) + first->wb_offset;
1340     	end = page_offset(last->wb_page) + (last->wb_offset + last->wb_bytes);
1341     	len = end - start;
1342     	/* If 'len' is not a 32-bit quantity, pass '0' in the COMMIT call */
1343     	if (end >= inode->i_size || len < 0 || len > (~((u32)0) >> 1))
1344     		len = 0;
1345     
1346     	data->inode	  = inode;
1347     	data->cred	  = first->wb_cred;
1348     	data->args.fh     = NFS_FH(inode);
1349     	data->args.offset = start;
1350     	data->res.count   = data->args.count = (u32)len;
1351     	data->res.fattr   = &data->fattr;
1352     	data->res.verf    = &data->verf;
1353     }
1354     
1355     /*
1356      * Commit dirty pages
1357      */
1358     static int
1359     nfs_commit_list(struct list_head *head, int how)
1360     {
1361     	struct rpc_message	msg;
1362     	struct rpc_clnt		*clnt;
1363     	struct nfs_write_data	*data;
1364     	struct rpc_task         *task;
1365     	struct nfs_page         *req;
1366     	int                     flags,
1367     				async = !(how & FLUSH_SYNC);
1368     	sigset_t		oldset;
1369     
1370     	data = nfs_writedata_alloc();
1371     
1372     	if (!data)
1373     		goto out_bad;
1374     	task = &data->task;
1375     
1376     	flags = (async) ? RPC_TASK_ASYNC : 0;
1377     
1378     	/* Set up the argument struct */
1379     	nfs_commit_rpcsetup(head, data);
1380     	req = nfs_list_entry(data->pages.next);
1381     	clnt = NFS_CLIENT(req->wb_inode);
1382     
1383     	rpc_init_task(task, clnt, nfs_commit_done, flags);
1384     	task->tk_calldata = data;
1385     	/* Release requests */
1386     	task->tk_release = nfs_writedata_release;
1387     
1388     	msg.rpc_proc = NFS3PROC_COMMIT;
1389     	msg.rpc_argp = &data->args;
1390     	msg.rpc_resp = &data->res;
1391     	msg.rpc_cred = data->cred;
1392     
1393     	dprintk("NFS: %4d initiated commit call\n", task->tk_pid);
1394     	rpc_clnt_sigmask(clnt, &oldset);
1395     	rpc_call_setup(task, &msg, 0);
1396     	lock_kernel();
1397     	rpc_execute(task);
1398     	unlock_kernel();
1399     	rpc_clnt_sigunmask(clnt, &oldset);
1400     	return 0;
1401      out_bad:
1402     	while (!list_empty(head)) {
1403     		req = nfs_list_entry(head->next);
1404     		nfs_list_remove_request(req);
1405     		nfs_mark_request_commit(req);
1406     		nfs_unlock_request(req);
1407     	}
1408     	return -ENOMEM;
1409     }
1410     
1411     /*
1412      * COMMIT call returned
1413      */
1414     static void
1415     nfs_commit_done(struct rpc_task *task)
1416     {
1417     	struct nfs_write_data	*data = (struct nfs_write_data *)task->tk_calldata;
1418     	struct nfs_writeres	*resp = &data->res;
1419     	struct nfs_page		*req;
1420     	struct inode		*inode = data->inode;
1421     
1422             dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1423                                     task->tk_pid, task->tk_status);
1424     
1425     	nfs_write_attributes(inode, resp->fattr);
1426     	while (!list_empty(&data->pages)) {
1427     		req = nfs_list_entry(data->pages.next);
1428     		nfs_list_remove_request(req);
1429     
1430     		dprintk("NFS: commit (%x/%Ld %d@%Ld)",
1431     			req->wb_inode->i_dev,
1432     			(long long)NFS_FILEID(req->wb_inode),
1433     			req->wb_bytes,
1434     			(long long)(page_offset(req->wb_page) + req->wb_offset));
1435     		if (task->tk_status < 0) {
1436     			if (req->wb_file)
1437     				req->wb_file->f_error = task->tk_status;
1438     			nfs_inode_remove_request(req);
1439     			dprintk(", error = %d\n", task->tk_status);
1440     			goto next;
1441     		}
1442     
1443     		/* Okay, COMMIT succeeded, apparently. Check the verifier
1444     		 * returned by the server against all stored verfs. */
1445     		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1446     			/* We have a match */
1447     			nfs_inode_remove_request(req);
1448     			dprintk(" OK\n");
1449     			goto next;
1450     		}
1451     		/* We have a mismatch. Write the page again */
1452     		dprintk(" mismatch\n");
1453     		nfs_mark_request_dirty(req);
1454     	next:
1455     		nfs_unlock_request(req);
1456     	}
1457     }
1458     #endif
1459     
1460     int nfs_flush_file(struct inode *inode, struct file *file, unsigned long idx_start,
1461     		   unsigned int npages, int how)
1462     {
1463     	LIST_HEAD(head);
1464     	int			res,
1465     				error = 0;
1466     
1467     	res = nfs_scan_dirty(inode, &head, file, idx_start, npages);
1468     	if (res)
1469     		error = nfs_flush_list(inode, &head, how);
1470     	if (error < 0)
1471     		return error;
1472     	return res;
1473     }
1474     
1475     int nfs_flush_timeout(struct inode *inode, int how)
1476     {
1477     	LIST_HEAD(head);
1478     	int			pages,
1479     				error = 0;
1480     
1481     	pages = nfs_scan_dirty_timeout(inode, &head);
1482     	if (pages)
1483     		error = nfs_flush_list(inode, &head, how);
1484     	if (error < 0)
1485     		return error;
1486     	return pages;
1487     }
1488     
1489     #ifdef CONFIG_NFS_V3
1490     int nfs_commit_file(struct inode *inode, struct file *file, unsigned long idx_start,
1491     		    unsigned int npages, int how)
1492     {
1493     	LIST_HEAD(head);
1494     	int			res,
1495     				error = 0;
1496     
1497     	res = nfs_scan_commit(inode, &head, file, idx_start, npages);
1498     	if (res)
1499     		error = nfs_commit_list(&head, how);
1500     	if (error < 0)
1501     		return error;
1502     	return res;
1503     }
1504     
1505     int nfs_commit_timeout(struct inode *inode, int how)
1506     {
1507     	LIST_HEAD(head);
1508     	int			pages,
1509     				error = 0;
1510     
1511     	pages = nfs_scan_commit_timeout(inode, &head);
1512     	if (pages) {
1513     		pages += nfs_scan_commit(inode, &head, NULL, 0, 0);
1514     		error = nfs_commit_list(&head, how);
1515     	}
1516     	if (error < 0)
1517     		return error;
1518     	return pages;
1519     }
1520     #endif
1521     
1522     int nfs_sync_file(struct inode *inode, struct file *file, unsigned long idx_start,
1523     		  unsigned int npages, int how)
1524     {
1525     	int	error,
1526     		wait;
1527     
1528     	wait = how & FLUSH_WAIT;
1529     	how &= ~FLUSH_WAIT;
1530     
1531     	if (!inode && file)
1532     		inode = file->f_dentry->d_inode;
1533     
1534     	do {
1535     		error = 0;
1536     		if (wait)
1537     			error = nfs_wait_on_requests(inode, file, idx_start, npages);
1538     		if (error == 0)
1539     			error = nfs_flush_file(inode, file, idx_start, npages, how);
1540     #ifdef CONFIG_NFS_V3
1541     		if (error == 0)
1542     			error = nfs_commit_file(inode, file, idx_start, npages, how);
1543     #endif
1544     	} while (error > 0);
1545     	return error;
1546     }
1547     
1548     int nfs_init_nfspagecache(void)
1549     {
1550     	nfs_page_cachep = kmem_cache_create("nfs_page",
1551     					    sizeof(struct nfs_page),
1552     					    0, SLAB_HWCACHE_ALIGN,
1553     					    NULL, NULL);
1554     	if (nfs_page_cachep == NULL)
1555     		return -ENOMEM;
1556     
1557     	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1558     					     sizeof(struct nfs_write_data),
1559     					     0, SLAB_HWCACHE_ALIGN,
1560     					     NULL, NULL);
1561     	if (nfs_wdata_cachep == NULL)
1562     		return -ENOMEM;
1563     
1564     	return 0;
1565     }
1566     
1567     void nfs_destroy_nfspagecache(void)
1568     {
1569     	if (kmem_cache_destroy(nfs_page_cachep))
1570     		printk(KERN_INFO "nfs_page: not all structures were freed\n");
1571     	if (kmem_cache_destroy(nfs_wdata_cachep))
1572     		printk(KERN_INFO "nfs_write_data: not all structures were freed\n");
1573     }
1574     
1575